output_description: If we can make X equal to A by repeating the operation, print the minimum
  number of operations required. If we cannot, print -1.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      A_1
      :
      A_N
attempt: #include <algorithm>
  #include <bitset>
  #include <complex>
  #include <deque>
  #include <exception>
  #include <fstream>
  #include <functional>
  #include <iomanip>
  #include <ios>
  #include <iosfwd>
  #include <iostream>
  #include <istream>
  #include <iterator>
  #include <limits>
  #include <list>
  #include <locale>
  #include <map>
  #include <memory>
  #include <new>
  #include <numeric>
  #include <ostream>
  #include <queue>
  #include <set>
  #include <sstream>
  #include <stack>
  #include <stdexcept>
  #include <streambuf>
  
  #define fi first
  #define se second
  #define rep(i,n) for(int i = 0; i < (n); ++i)
  #define rrep(i,n) for(int i = 1; i <= (n); ++i)
  #define drep(i,n) for(int i = (n)-1; i >= 0; --i)
  #define srep(i,s,t) for (int i = s; i < t; ++i)
  #define rng(a) a.begin(),a.end()
  #define rrng(a) a.rbegin(),a.rend()
  #define maxs(x,y) (x = max(x,y))
  #define mins(x,y) (x = min(x,y))
  #define limit(x,l,r) max(l,min(x,r))
  #define lims(x,l,r) (x = max(l,min(x,r)))
  #define isin(x,l,r) ((l) <= (x) && (x) < (r))
  #define pb push_back
  #define eb emplace_back
  #define sz(x) (int)(x).size()
  #define pcnt __builtin_popcountll
  #define uni(x) x.erase(unique(rng(x)),x.end())
  #define show(x) cout<<#x<<" = "<<x<<endl;
  #define PQ(T) priority_queue<T,v(T),greater<T> >
  #define bn(x) ((1<<x)-1)
  #define dup(x,y) (((x)+(y)-1)/(y))
  #define newline puts("")
  #define v(T) vector<T>
  #define vv(T) v(v(T))
  using namespace std;
  typedef long long int ll;
  typedef unsigned uint;
  typedef unsigned long long ull;
  typedef pair<int,int> P;
  
  typedef vector<int> vi;
  typedef vector<vi> vvi;
  typedef vector<ll> vl;
  typedef vector<P> vp;
  
  
  int main() {
      int n;
      cin >> n;
      vl a(n);
      rep(i,n)cin >> a[i];
      if(a[0] != 0){
          cout<<-1<<endl;
          return 0;
      }
      ll ans = 0;
  
      srep(i,1,n){
          if(a[i-1]==0 || a[i]<=1){
              //show("s1");
              ans+=a[i];
          }
          else if(a[i] <=a[i-1]){
              //show("s2");
              ans+=1;
          }else{
              //show("s3");
              ans+=max(1LL,a[i]-a[i-1]);
          }
          // a[i]=max(1LL,a[i]-a[i-1]);
          // if(a[i-1] != 0 && a[i]>=a[i-1])ans-=1;
      }
      cout<<ans<<endl;
      return 0;
  }
  
problem_description: Statement
  
  There is a sequence X of length N, where every element is initially 0. Let X_i
  denote the i-th element of X.
  
  You are given a sequence A of length N. The i-th element of A is A_i.
  Determine if we can make X equal to A by repeating the operation below. If we
  can, find the minimum number of operations required.
  
    * Choose an integer i such that 1\leq i\leq N-1. Replace the value of X_{i+1} with the value of X_i plus 1.
samples: [{"input": "4\n    0\n    1\n    1\n    2", "output": "3\n    \n\nWe can make X equal to A as follows:\n\n  * Choose i=2. X becomes (0,0,1,0).\n  * Choose i=1. X becomes (0,1,1,0).\n  * Choose i=3. X becomes (0,1,1,2).\n\n* * *"}, {"input": "3\n    1\n    2\n    1", "output": "-1\n    \n\n* * *"}, {"input": "9\n    0\n    1\n    1\n    0\n    1\n    2\n    2\n    1\n    2", "output": "8"}]