1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define ST first
#define ND second
#define MP make_pair
#define PB push_back

typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef set<int> SI;
typedef long long LL;

#ifdef DEBUG
const bool debug = true;
#else
const bool debug = false;
#endif

// #define MULTI_TASK true

const int INF = 1000 * 1000 * 1000;
const LL INFLL = 1e18;
const int MAKSN = 1000 * 1000 + 13;
const LL MOD = 123456789LL;
LL N, M, k, l, a, b, c, d, sum = 0, cnt = 0;
vector<LL> v;

vector<tuple<LL, LL, LL, int, int>> edges;  // XD
set<pair<int, int>> deletedEdges;

void zeruj() {
    v.clear();
}

void readIn() {
    cin >> N;
    int last_non_zero = -1, first = -1;

    REP(i, N) {
        cin >> k;
        v.PB(k);
        sum += k;
        if (k != 0) {
            if (last_non_zero == -1)
                first = i;

            if (last_non_zero != -1) {
                if (v[last_non_zero] > k)
                    edges.push_back({-(i - last_non_zero), v[last_non_zero], k, last_non_zero, i});
                else
                    edges.push_back({-(i - last_non_zero), k, v[last_non_zero], last_non_zero, i});
            }
            last_non_zero = i;
        }
    }
    sort(edges.begin(), edges.end());
    cnt = last_non_zero - first;

    for (int i = 0; i < first; i++) {
        deletedEdges.insert({i, i + 1});
    }
    for (int i = last_non_zero; i < N - 1; i++) {
        deletedEdges.insert({i, i + 1});
    }
}

LL calcLeft(int a) {
    LL suma = 0;
    for (int i = a; i >= 0; i--) {
        suma += v[i];
        if (deletedEdges.find({i - 1, i}) != deletedEdges.end()) {
            return suma;
        }
    }
    return suma;
}

LL calcRight(int b) {
    LL suma = 0;
    for (int i = b; i < N; i++) {
        suma += v[i];
        if (deletedEdges.find({i, i + 1}) != deletedEdges.end()) {
            return suma;
        }
    }

    return suma;
}

void deleteRange(int a, int b) {
    for (int i = a; i < b; i++) {
        deletedEdges.insert({i, i + 1});
    }
}

void solve() {
    if (sum < 0) {
        cout << "-1\n";
        return;
    }

    for (int i = 0; i < (int)edges.size(); i++) {
        LL a, b;
        a = get<3>(edges[i]);
        b = get<4>(edges[i]);

        LL left = calcLeft(a);
        LL right = calcRight(b);

        if (left >= 0 && right >= 0) {
            cnt += get<0>(edges[i]);
            deleteRange(a, b);
        }
    }

    cout << cnt << "\n";
}

int main() {
    ios_base::sync_with_stdio(0);
#ifdef MULTI_TASK
    int test;
    cin >> test;
    while (test--) {
#endif
        zeruj();
        readIn();
        solve();
#ifdef MULTI_TASK
    }
#endif
    return 0;
}