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
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long ll;
const ll infinity = numeric_limits<ll>::max();

inline ll infint_add(ll a, ll b){
    if(a==infinity || b==infinity)
        return infinity;
    return a+b;
}

struct ITree{
    ll *mod, *val;
    int start, size;

    void init(int els){
        start = 1;
        while(start<els)
            start *= 2;
        size = 2*start;
        mod = new ll[size];
        val = new ll[size];
        fill(mod, mod+start, 0);
        fill(mod+start, mod+size, infinity);
        fill(val, val+size, infinity);
    }
    void deinit(){
        delete []mod;
        delete []val;
    }

    void _add(int a, int b, ll value, int v, int rA, int rB){
        // cout << "_add a=" << a << " b=" << b << " value=" << value << " v=" << v << " rA=" << rA << " rB=" << rB << endl;
        if(a<=rA && rB<=b){
            mod[v] = infint_add(mod[v], value);
            val[v] = infint_add(val[v], value);
        }
        else{
            int rM = (rA+rB)/2;
            if(a<=rM)
                _add(a, b, value, 2*v, rA, rM);
            if(rM<b)
                _add(a, b, value, 2*v+1, rM+1, rB);
            val[v] = infint_add(min(val[2*v],val[2*v+1]), mod[v]);
        }
    }
    void add(int a, int b, int value){
        _add(a, b, value, 1, 0, start-1);
    }

    void _set(int p, ll value, int v, int rA, int rB, ll acc){
        if(rA==rB){
            mod[v]=val[v]=value-acc;
            //cout << "New direct val in node " << v << "  =  " << val[v] << endl;
        }
        else{
            int rM = (rA+rB)/2;
            if(p<=rM)
                _set(p, value, 2*v, rA, rM, acc+mod[v]);
            else
                _set(p, value, 2*v+1, rM+1, rB, acc+mod[v]);
            val[v] = infint_add(min(val[2*v],val[2*v+1]), mod[v]);
            //cout << "New indirect val in node " << v << "  =  " << val[v] << endl;
        }
    }
    void set(int p, ll value){
        _set(p, value, 1, 0, start-1, 0);
    }

    ll _get(int a, int b, int v, int rA, int rB){
        //cout << "GET a=" << a << " b=" << b << " v=" << v << " rA=" << rA << " rB=" << rB << endl;
        if(a<=rA && rB<=b)
            return val[v];
        int rM = (rA+rB)/2;
        ll result = infinity;
        if(a<=rM)
            result = min(result, _get(a, b, 2*v, rA, rM));
        if(rM<b)
            result = min(result, _get(a, b, 2*v+1, rM+1, rB));
        return infint_add(result, mod[v]);
    }
    ll get(int a, int b){
        return _get(a, b, 1, 0, start-1);
    }


    // void _indent(int i){
    //     while(i--){
    //         cout << "   ";
    //     }
    // }
    // void _print(int v, int indent){
    //     if(v>=size)
    //         return;
    //     _indent(indent);
    //     cout << "Node " << v << " mod=" << mod[v] << " val=" << val[v] << endl;
    //     _indent(indent);
    //     cout << "Left:" << endl;
    //     _print(2*v, indent+1);
    //     _indent(indent);
    //     cout << "Right:" << endl;
    //     _print(2*v+1, indent+1);
    //     _indent(indent);
    //     cout << "End node " << v << endl;
    // }
    // void print(){
    //     _print(1, 0);
    // }
};

struct Struct{
    vector<ll>SortedKeys;
    ITree tree;
    int n;

    Struct(const vector<ll>& Keys) : SortedKeys(Keys){
        sort(SortedKeys.begin(), SortedKeys.end());
        SortedKeys.erase(unique(SortedKeys.begin(), SortedKeys.end()), SortedKeys.end());
        n = SortedKeys.size();
        tree.init(n);
    }
    ~Struct(){
        tree.deinit();
    }

    int _getPos(ll key){
        return lower_bound(SortedKeys.begin(), SortedKeys.end(), key)-SortedKeys.begin();
    }

    ll getMinCost(ll a, ll b){
        if(a>b)
            return infinity;
        return tree.get(_getPos(a), _getPos(b));
    }
    void add(ll x){
        tree.add(0,n-1,x);
    }
    void newPrefix(ll key, ll val){
        int pos = _getPos(key);
        ll oldVal = tree.get(pos, pos);
        if(val<oldVal)
            tree.set(pos, val);
        //tree.print();
    }
};

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

    int n;
    cin >> n;
    vector<ll>A(n+1),P(n+1);
    for(int i=1;i<=n;i++){
        cin >> A[i];
        P[i] = P[i-1]+A[i];
    }

    Struct s(P);
    ll lastKey=0, lastVal=0;

    for(int i=1;i<=n;i++){
        s.add(1);
        if(lastKey>=0)
            s.newPrefix(lastKey, lastVal);
        lastKey=P[i];
        lastVal= (P[i]>=0) ? s.getMinCost(0, P[i]) : -1;
        //cout << "for i=" << i << " (" << lastKey << "," << lastVal << ')' << endl;
    }

    cout << lastVal << "\n";

    return 0;
}