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
#include<bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
using ll=long long;
using pll=pair<ll,ll>;
const int R=5e5;
int tab[R+6];
vector<pair<ll,pll>>res;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n;cin>>n;
    for(int i=1;i<=n;i++)
    {
        ll a;cin>>a;
        tab[i]=a;
    }
    res.push_back({1,{0,0}});
    for(int i=1;i<=n;i++)
    {
        if(tab[i]==0)continue;
        int rr=res.size();
        for(int j=0;j<rr;j++)
        {
            auto it=res[j];
            ll id=it.fr, e=it.sc.fr,d=it.sc.sc;
            if(e>=0)
            {
                if(tab[i]>0)
                {
                   res.push_back({id,{e,d}});
                }
                res.push_back({i,{tab[i],d}});
            }
            res[j]={i,{e+tab[i],d+(i-id)}};
        }
    }
    ll ans=LONG_MAX;
    for(auto it:res)
    {
        if(it.sc.fr>=0)ans=min(ans,it.sc.sc);
    }
    if(ans!=LONG_MAX)cout<<ans;
    else cout<<-1;
}