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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef uint32_t ul;
typedef int32_t l;
typedef uint64_t ull;
typedef int64_t ll;

template <typename T>
using statistics_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const l INF = 1000000000 + 9;
const ll MOD = 123456789;
const l PRIME = 200003;
const ll L_INF = 1000000000000000000LL + 7;
const double EPS = 1e-5;

#define FOR(x, y, z) for (l z = x; z < y; z++)
#define FORE(x, y, z) for (l z = x; z <= y; z++)
#define FORD(x, y, z) for (l z = x; z > y; z--)
#define FORDE(x, y, z) for (l z = x; z >= y; z--)
#define REP(x, y) for (l y = 0; y < x; y++)
#define ALL(...) (__VA_ARGS__).begin(), (__VA_ARGS__).end()

#define PF push_front
#define POF pop_front
#define PB push_back
#define POB pop_back
#define MP make_pair
#define FS first
#define SC second

const l MAXN = 1000005;

struct Node
{
    ll beg, en; //range
    ll minim = L_INF;
    l lazy = 0;

    Node *lf{nullptr}, *rt{nullptr};

    Node(ll b, ll e)
    {
        beg = b;
        en = e;
    }

    void extend()
    {
        if(!lf && en-beg > 0)
        {
            ll mid = (en-beg+1)/2LL;
            lf = new Node(beg, beg+mid-1);
            rt = new Node(beg+mid, en);
        }
    }

    void push()
    {
        if(lazy)
        {
            minim += lazy;
            minim = min(minim, L_INF);
            
            if(en-beg > 0)
            {
                lf->lazy += lazy;
                rt->lazy += lazy;
            }
        }

        lazy = 0;
    }

    void add_val(ll v)
    {
        lazy += v;
    }

    void add_elem(ll pos, ll v)
    {
        extend();
        push();

        if(beg == en && beg == pos)
        {
            minim = min(minim, v);
            return;
        }

        if(pos > en || pos < beg)
            return;
        
        lf->add_elem(pos, v);
        rt->add_elem(pos, v);

        minim = min(lf->minim, rt->minim);
    }

    ll query(ll lew, ll praw)
    {
        extend();
        push();

        if(en < lew || beg > praw)
            return L_INF;

        if(lew <= beg && en <= praw)
            return minim;

        ll res = lf->query(lew, praw);
        res = min(res, rt->query(lew, praw));

        minim = min(lf->minim, rt->minim);

        return res;
    }
};

l n, idx;
ll suma, shift;
l val[MAXN], pos[MAXN];
ll dp[MAXN], min_dp[MAXN];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    Node* root = new Node(-1e16, 1e16);

    cin >> n;
    FORE(1, n, i)
    {
        ll a;
        cin >> a;
        suma += a;

        if(a != 0)
        {
            val[++idx] = a;
            pos[idx] = i;
        }
    }

    if(suma < 0)
    {
        cout << "-1\n";
        return 0;
    }

    min_dp[1] = dp[0];
    dp[1] = L_INF;
    
    if(val[1] > 0)
        dp[1] = 0;

    root->add_elem(val[1], min_dp[1]);

    n = idx;
    FORE(2, n, i)
    {
        min_dp[i] = dp[i-1];
        if(val[i-1] > 0)
            min_dp[i] = min(min_dp[i], min_dp[i-1]);

        shift += val[i];

        dp[i] = L_INF;
        root->add_val(pos[i]-pos[i-1]);
        dp[i] = min(dp[i], root->query(0-shift, 1e9));
        root->add_elem(-shift+val[i], min_dp[i]);

        if(val[i] > 0)
            dp[i] = min(dp[i], min_dp[i]);

        //cout << i << ": " << dp[i] << " " << min_dp[i] << " " << shift << "\n";
    }

    cout << (dp[n] == L_INF ? -1 : dp[n]) << "\n";
}