#include <bits/stdc++.h> using namespace std; #define st first #define nd second #define PB push_back #define mp make_pair #define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using ll = long long; #define mod 1000000007 #define debug() cout<<"!XD"<<endl int w[500009]; int main() { fastio int n; cin>>n; for(int i=0; i<n; ++i){ cin>>w[i]; } ll sum = 0; int res=mod; int countp=0; bool t; for(int maska=0; maska<(1<<n); ++maska){ t=true; sum=0; countp=0; for(int bit=0; bit<n; ++bit){ if(((1<<bit)&(maska))==0){ if(sum<0 || w[bit]<0){ t=false; break; } else{ sum=0; countp--; } } else{ countp++; sum+=w[bit]; } } if(t && sum>=0){ res=min(res, countp); } else{ continue; } } cout<<res+1; return 0; }
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 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define PB push_back #define mp make_pair #define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using ll = long long; #define mod 1000000007 #define debug() cout<<"!XD"<<endl int w[500009]; int main() { fastio int n; cin>>n; for(int i=0; i<n; ++i){ cin>>w[i]; } ll sum = 0; int res=mod; int countp=0; bool t; for(int maska=0; maska<(1<<n); ++maska){ t=true; sum=0; countp=0; for(int bit=0; bit<n; ++bit){ if(((1<<bit)&(maska))==0){ if(sum<0 || w[bit]<0){ t=false; break; } else{ sum=0; countp--; } } else{ countp++; sum+=w[bit]; } } if(t && sum>=0){ res=min(res, countp); } else{ continue; } } cout<<res+1; return 0; } |