#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;++A)
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
#define INF (ll)1e18

enum SegType{SUM,MIN,MAX};
template<typename T>
struct SegTree{
    int num=0, bt;
    vector<T> node, lazy;
    typedef function<T(const T &, const T &)> c1t;
    typedef function<T(const T &, const int &, const int &, const int &, const int &, const T &)> c2t;
    typedef function<T(const T &, const T &)> c3t;
    c1t calc1;c2t calc2;c3t calc3;T _init;

    void init(int num,T n,c1t calc1,c2t calc2,c3t calc3){
        int i = 0, j = 1;
        this->num = num;
        this->_init = n;
        this->calc1 = calc1;
        this->calc2 = calc2;
        this->calc3 = calc3;
        for (j = 1; j < num;)
            j *= 2;
        this->bt = j - 1;
        node.resize(j * 2 - 1, n);
        lazy.resize(j * 2 - 1, 0);
    }

    void init(int num,T n,SegType st){
        if(st==SegType::MAX){
            if(std::is_same<T,int>().value)
                _init = (T)INT_MIN;
            else if(std::is_same<T,long long>().value)
                _init = (T)LLONG_MIN;
            else
                _init = (T)n;
            init(num,n,
                [](const T &a, const T &b) { return max(a, b); },
                [](const T &a, const int &x, const int &y, const int &l, const int &r, const T &n) { return a + n; },
                [](const T &a, const T &n) { return a + n; });
        }else if(st==SegType::MIN){
            if(std::is_same<T,int>().value)
                _init = (T)INT_MAX;
            else if(std::is_same<T, long long>().value)
                _init = (T)LLONG_MAX;
            else
                _init = (T)n;
            init(num,n,
                [](const T &a, const T &b) { return min(a, b); },
                [](const T &a, const int &x, const int &y, const int &l, const int &r, const T &n) { return a + n; },
                [](const T &a, const T &n) { return a + n; });
        }else if(st==SegType::SUM){
            if(std::is_same<T,int>().value)
                _init = (T)0;
            else if(std::is_same<T,long long>().value)
                _init = (T)0;
            else
                _init = (T)n;
            init(num,n,
                [](const T &a, const T &b) { return (a + b); },
                [](const T &a, const int &x, const int &y, const int &l, const int &r, const T &n) { return (a + (r - l + 1) * n); },
                [](const T &a, const T &n) { return (a + n / 2); });
        }
    }
    void addd(int x,int y,T n){
        if(x>y)
            return;
        _addd(x, y, n, 0, 0, this->bt);
    }
    void _addd(const int& x,const int& y,const T& n,int p,int l,int r){
        if (r < x || y < l)
            return;
        if(x<=l&&r<=y){
            lazy[p] = calc2(lazy[p], x, y, l, r, n);
            evall(p);
        }else{
            if(lazy[p]!=0)
                evall(p);
            _addd(x, y, n, p * 2 + 1, l, (l + r) / 2);
            _addd(x, y, n, p * 2 + 2, (l + r) / 2 + 1, r);
            node[p] = calc1(node[p * 2 + 1], node[p * 2 + 2]);
        }
    }
    inline void evall(int x){
        node[x] += lazy[x];
        if(x<bt){
            lazy[x * 2 + 1] = calc3(lazy[x * 2 + 1], lazy[x]);
            lazy[x * 2 + 2] = calc3(lazy[x * 2 + 2], lazy[x]);
        }
        lazy[x] = 0;
    }
    void sett(int x,T n){
        gett(x);
        int i = bt+x;
        node[i] = n;
        while(i!=0){
            i = (i - 1) / 2;
            node[i] = calc1(node[i * 2 + 1],node[i * 2 + 2]);
        }
    }
    inline T gett(int x){
        T ret=_init;
        _gett(x, x, 0, 0, this->bt, ret);
        return ret;
    }
    T gett(int x,int y){
        T ret=_init;
        if(x>y)
            return ret;
        _gett(x, y, 0, 0, this->bt, ret);
        return ret;
    }
    void _gett(const int& x,const int& y,int p,int l,int r,T& ret){
        if(r<x||y<l)
            return;
        if(lazy[p]!=0)
            evall(p);
        if(x<=l&&r<=y){
            ret = calc1(ret, node[p]);
            return;
        }
        _gett(x, y, p * 2 + 1, l, (l + r) / 2, ret);
        _gett(x, y, p * 2 + 2, (l + r) / 2 + 1, r, ret);
    }
};

//Zアルゴリズム
void Zalgo(ll N,ll *S,ll *ln){
    ll i = 1;
    ll j = 0;
    ll k = 0;
    while(i<N){
        if(i+ln[i-j]<j+ln[j]){
            ln[i] = ln[i - j];
        }else{
            k = j + ln[j] - i > 0 ? j + ln[j] - i : 0;
            while(i+k<N&&S[k]==S[i+k])
                k++;
            ln[i] = k;
            j = i;
        }
        i++;
    }
}

//Dinic法（最大フロー）
struct Dinic{
    int N = 0;
    vector<int> dst;
    vector<vector<pair<ll, pii>>> edge;
    int a, b, c;
    void bfs(const int& s){
        int i;
        queue<int> qq;
        qq.push(s);
        while(!qq.empty()){
            int a = qq.front();
            qq.pop();
            fornum(i,0,edge[a].size()){
                auto &b = edge[a][i];
                if(dst[b.second.first]==0&&b.first!=0ll){
                    dst[b.second.first] = dst[a] + 1;
                    qq.push(b.second.first);
                }
            }
        }
    }
    ll dfs(int a,const int& t,ll f){
        if(a==t){
            return f;
        }
        int i, es = edge[a].size();
        fornum(i,0,es){
            auto &b = edge[a][i];
            if(b.first!=0&&dst[a]<dst[b.second.first]){
                ll ff = dfs(b.second.first, t, f < b.first ? f : b.first);
                if(ff!=0){
                    edge[a][i].first -= ff;
                    edge[b.second.first][b.second.second].first += ff;
                    return ff;
                }
            }
        }
        return 0;
    }

    void init(int n)
    {
        edge.resize(n);
        dst.resize(n);
        N = n;
    }
    void addedge(int from,int to,ll cap){
        edge[from].push_back({cap, {to, edge[to].size()}});
        edge[to].push_back({0ll, {from, edge[from].size()-1}});
    }
    ll mxflow(int s,int t){
        ll i, flow = 0;
        while (true)
        {
            fornum(i,0,N){
                dst[i] = 0;
            }
            dst[s] = 1;
            bfs(s);
            if (dst[t] == 0)
                break;
            while(i=dfs(s,t,INF)){
                flow += i;
            }
        }
        return flow;
    }
};

ll N,A[202020];
ll mk[202020];
ll i, j, k,ans;

void no(){
    printf("-1\n");
    exit(0);
}

int main(){
    scanf("%lld", &N);
    fornum(i,0,N+1){
        scanf("%lld", &A[i]);
    }
    if(N==0){
        if(A[0]==1){
            printf("1\n");
            return 0;
        }else{
            no();
        }
    }
    if(A[0]!=0)
        no();
    SegTree<ll> sg;
    sg.init( N + 9,0, SegType::MAX);
    sg.sett(0, 2);
    j = 0;
    ans = 1;
    fornum(i, 1, N+1){
        
        while(j<i){
            ll a = min(A[i], sg.gett(j));
            //printf("a%lld,%lld,%lld\n", a, i, j);
            A[i] -= a;
            sg.addd(j, j, -a);
            ans += (i - j) * a;
            if(j+1!=i){
                sg.addd(j+1, i - 1, a);
            }
            if(0==sg.gett(j,j))
                ++j;
            if(A[i]==0)
                break;
        }
        
        if(i==N&&A[i]==0)
            continue;
        if(i==j)
            no();
    }
    printf("%lld\n", ans);

    return 0;
}

Measured memory_bytes: 8528