#include <bits/stdc++.h> using namespace std; pair<int,int> xd[500005]; int b[500005]; int main(){ int n; ios_base::sync_with_stdio(0); cin>>n; long long s=0; for(int i=1;i<=n;i++){ cin>>b[i]; s+=b[i]; } if(s<0){ cout<<"-1"; return 0; } xd[1].first=b[1]; for(int i=2;i<=n;i++){ bool ps=0; unsigned long long bw=-1; for(int j=1;j<i;j++){ if(xd[j].second!=-1){ if(xd[j].first>=0){ ps=1; bw=min(bw,(unsigned long long)(xd[j].second)); } xd[j].first+=b[i]; xd[j].second++; } } if(ps==0){ xd[i].second=-1; }else{ xd[i].second=bw; xd[i].first=b[i]; } } unsigned int bww=-1; for(int i=1;i<=n;i++){ if(xd[i].first>=0)bww=min(bww,(unsigned int)xd[i].second); } cout<<bww; }
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 | #include <bits/stdc++.h> using namespace std; pair<int,int> xd[500005]; int b[500005]; int main(){ int n; ios_base::sync_with_stdio(0); cin>>n; long long s=0; for(int i=1;i<=n;i++){ cin>>b[i]; s+=b[i]; } if(s<0){ cout<<"-1"; return 0; } xd[1].first=b[1]; for(int i=2;i<=n;i++){ bool ps=0; unsigned long long bw=-1; for(int j=1;j<i;j++){ if(xd[j].second!=-1){ if(xd[j].first>=0){ ps=1; bw=min(bw,(unsigned long long)(xd[j].second)); } xd[j].first+=b[i]; xd[j].second++; } } if(ps==0){ xd[i].second=-1; }else{ xd[i].second=bw; xd[i].first=b[i]; } } unsigned int bww=-1; for(int i=1;i<=n;i++){ if(xd[i].first>=0)bww=min(bww,(unsigned int)xd[i].second); } cout<<bww; } |