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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int32_t main(){
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<ll> tab(n),ps(n+1);
    for(int i=0;i<n;i++) {
        cin >> tab[i];
        ps[i+1] = ps[i] + tab[i];
    }
    vector<ll> dp(n+1,-1e9);
    vector<pair<ll,int> > S = {{0,0}};
    dp[0] = 0;
    for(int i=1;i<=n;i++) {
        auto x = lower_bound(S.begin(),S.end(),make_pair(ps[i]+1,-2));
        if(x == S.begin())
            dp[i] = -1;
        else {
            x--;
            dp[i] = x->second+1;
            if(dp[i] >= S.size())
                S.push_back({ps[i],dp[i]});
            else
                S[dp[i]].first = min(S[dp[i]].first,ps[i]);
        }
    }
    cout<<(dp[n] == -1 ? -1 : n-dp[n])<<"\n";
   
}