#include<bits/stdc++.h>

int main(){
    using namespace std;
    unsigned long N;
    cin >> N;
    unsigned long ans{~0UL};
    for(unsigned long i{0}, pa{~0UL}, a; i < N; ++i){
        cin >> a;
        if(pa + 1 < a)return 0 & puts("-1");
        if(pa + 1 == a)++ans;
        else ans += a;
        pa = a;
    }
    cout << ans << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

// boost
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
using vld = vector<ld>;
using vvld = vector<vld>;

typedef pair<ll, ll> P;

#define bit(n) (1LL << (n))

//#define int long long

#define all(v) v.begin(), v.end()

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

template <class T>
inline void chmax(T& a, T b) {
  a = std::max(a, b);
}
template <class T>
inline void chmin(T& a, T b) {
  a = std::min(a, b);
}

#define mod (ll)(1e9 + 7)
// #define mod (998244353ll)

const long long INF = 1LL << 60;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll n;
  cin >> n;
  vll a(n);
  rep(i, n) cin >> a[i];

  if (a[0] != 0) {
    cout << -1 << endl;
    return 0;
  }

  REP(i, n) {
    if (a[i] >= a[i - 1] + 2) {
      cout << -1 << endl;
      return 0;
    }
  }

  ll ans = a[n - 1];
  for (int i = n - 2; i > 0; i--) {
    if (a[i] != a[i + 1] - 1) {
      ans += a[i];
    }
  }

  cout << ans << endl;

  return 0;
}
