#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define rep(i,poczatek,koniec) for(__typeof(koniec) i = poczatek; i<=koniec; ++i) #define repr(i,poczatek,koniec) for(__typeof(koniec) i = poczatek; i>=koniec; --i) #define pb push_back #define pob pop_back #define eb emplace_back #define pf push_front #define pof pop_front #define st first #define nd second #define bend(X) X.begin(),X.end() #define sz(X) (int(X.size())) #define graph vector<node> #define print cout<< #ifdef LOCAL #define nl endl #define dbg(x) cout << #x << " = " << x << ' '; #define say(x) cout << x #else #define nl '\n' #define dbg(x) #define say(x) #endif using namespace std; using namespace __gnu_pbds; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair<int,int>; using pll = pair<ll,ll>; using pli = pair<ll,int>; using vb = vector<bool>; using vc = vector<char>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pii>; using vpl = vector<pll>; using vvi = vector<vi>; using vvl = vector<vl>; using vvb = vector<vb>; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; template<typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& x){ is >> x.st >> x.nd; return is; } template<typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1,T2>& x){ os << '(' << x.st << ',' << x.nd << ')'; return os; } template<typename T> istream& operator>>(istream& is, vector<T>& x){ for(T& i : x)is >> i; return is; } template<typename T> ostream& operator<<(ostream& os, vector<T> x){ for(T& i : x)os << i << ' '; return os; } template<typename T1, typename T2> pair<T1,T2> operator+(const pair<T1,T2>& A, const pair<T1,T2>& B){ return {A.st + B.st, A.nd + B.nd}; } template<typename T1, typename T2> pair<T1,T2> operator-(const pair<T1,T2>& A, const pair<T1,T2>& B){ return {A.st - B.st, A.nd - B.nd}; } template<typename T1, typename T2> pair<T1,T2> operator-(const pair<T1,T2>& A){ return {-A.st, -A.nd}; } template<typename T1, typename T2> void mine(T1& A, const T2& B){if(B < A) A = B;} template<typename T1, typename T2> void maxe(T1& A, const T2& B){if(B > A) A = B;} const int INF = (int)2e9; const ll INFll = (ll)9e18; const int MOD = (int)1e9+7; void fastDataTransfer(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } #define val first #define lazy second const int war19 = 1<<19; const int war20 = 1<<20; int MAXI = 1e6; int n, m, a, b, c, d, q, k; vi V,dp; vpi T; vl pref; map<ll,int>M; int POT=1; void propagade(int v){ if(v > POT || T[v].lazy == 0) return; for(int i : {v*2, v*2+1}){ T[i].lazy += T[v].lazy; T[i].val += T[v].lazy; } T[v].lazy=0; } void update(int v){ if(v>POT) return; for(int i : {v*2, v*2+1}) mine(T[v].val, T[i].val); } int MINI,L,R; void wartosc(int v, int l, int r){ if(l>=L && r<=R){ mine(MINI, T[v].val); return; } if(l>R || r < L) return; propagade(v); wartosc(v*2, l, (l+r)/2); wartosc(v*2+1, (l+r)/2 + 1, r); } void ustaw(int v, int l, int r){ if(l>=L && r<=R){ mine(T[v].val, MINI); return; } if(l>R || r<L) return; propagade(v); ustaw(v*2, l, (l+r)/2); ustaw(v*2+1, (l+r)/2 + 1, r); update(v); } void read(){ cin>>n; V.resize(n); pref.resize(n+1); dp.resize(n+1); cin>>V; rep(i,1,n){ pref[i]=pref[i-1]+V[i-1]; M[pref[i]]; } M[0]; for(auto& i : M) i.nd = a++; while(POT<=M.size()){ POT*=2; } T.resize(POT*2 + 5); for(auto& i : T){ i.val = MAXI; i.lazy = 0; } } void wypiszDrzewo(){ int i = 1; while(i<=POT){ rep(j,i,i+i-1) print T[j] << ' '; print nl; i*=2; } print nl; } void solve(){ if(pref.back() < 0){ print -1 << nl; exit(0); } MINI = 0; L = R = M[0]; ustaw(1,0,POT-1); rep(i,1,n){ L = 0; R = M[pref[i]]; MINI = MAXI; wartosc(1,0,POT-1); dp[i] = MINI; if(pref[i]-pref[i-1] >= 0) mine(dp[i], dp[i-1]); L = R; MINI = dp[i]; T[1].lazy++; ustaw(1,0,POT-1); } print dp.back() << nl; } int main(){ fastDataTransfer(); int testCases = 1; // cin >> testCases; rep(testNumber, 1, testCases){ read(); solve(); } }
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define rep(i,poczatek,koniec) for(__typeof(koniec) i = poczatek; i<=koniec; ++i) #define repr(i,poczatek,koniec) for(__typeof(koniec) i = poczatek; i>=koniec; --i) #define pb push_back #define pob pop_back #define eb emplace_back #define pf push_front #define pof pop_front #define st first #define nd second #define bend(X) X.begin(),X.end() #define sz(X) (int(X.size())) #define graph vector<node> #define print cout<< #ifdef LOCAL #define nl endl #define dbg(x) cout << #x << " = " << x << ' '; #define say(x) cout << x #else #define nl '\n' #define dbg(x) #define say(x) #endif using namespace std; using namespace __gnu_pbds; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair<int,int>; using pll = pair<ll,ll>; using pli = pair<ll,int>; using vb = vector<bool>; using vc = vector<char>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pii>; using vpl = vector<pll>; using vvi = vector<vi>; using vvl = vector<vl>; using vvb = vector<vb>; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; template<typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& x){ is >> x.st >> x.nd; return is; } template<typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1,T2>& x){ os << '(' << x.st << ',' << x.nd << ')'; return os; } template<typename T> istream& operator>>(istream& is, vector<T>& x){ for(T& i : x)is >> i; return is; } template<typename T> ostream& operator<<(ostream& os, vector<T> x){ for(T& i : x)os << i << ' '; return os; } template<typename T1, typename T2> pair<T1,T2> operator+(const pair<T1,T2>& A, const pair<T1,T2>& B){ return {A.st + B.st, A.nd + B.nd}; } template<typename T1, typename T2> pair<T1,T2> operator-(const pair<T1,T2>& A, const pair<T1,T2>& B){ return {A.st - B.st, A.nd - B.nd}; } template<typename T1, typename T2> pair<T1,T2> operator-(const pair<T1,T2>& A){ return {-A.st, -A.nd}; } template<typename T1, typename T2> void mine(T1& A, const T2& B){if(B < A) A = B;} template<typename T1, typename T2> void maxe(T1& A, const T2& B){if(B > A) A = B;} const int INF = (int)2e9; const ll INFll = (ll)9e18; const int MOD = (int)1e9+7; void fastDataTransfer(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } #define val first #define lazy second const int war19 = 1<<19; const int war20 = 1<<20; int MAXI = 1e6; int n, m, a, b, c, d, q, k; vi V,dp; vpi T; vl pref; map<ll,int>M; int POT=1; void propagade(int v){ if(v > POT || T[v].lazy == 0) return; for(int i : {v*2, v*2+1}){ T[i].lazy += T[v].lazy; T[i].val += T[v].lazy; } T[v].lazy=0; } void update(int v){ if(v>POT) return; for(int i : {v*2, v*2+1}) mine(T[v].val, T[i].val); } int MINI,L,R; void wartosc(int v, int l, int r){ if(l>=L && r<=R){ mine(MINI, T[v].val); return; } if(l>R || r < L) return; propagade(v); wartosc(v*2, l, (l+r)/2); wartosc(v*2+1, (l+r)/2 + 1, r); } void ustaw(int v, int l, int r){ if(l>=L && r<=R){ mine(T[v].val, MINI); return; } if(l>R || r<L) return; propagade(v); ustaw(v*2, l, (l+r)/2); ustaw(v*2+1, (l+r)/2 + 1, r); update(v); } void read(){ cin>>n; V.resize(n); pref.resize(n+1); dp.resize(n+1); cin>>V; rep(i,1,n){ pref[i]=pref[i-1]+V[i-1]; M[pref[i]]; } M[0]; for(auto& i : M) i.nd = a++; while(POT<=M.size()){ POT*=2; } T.resize(POT*2 + 5); for(auto& i : T){ i.val = MAXI; i.lazy = 0; } } void wypiszDrzewo(){ int i = 1; while(i<=POT){ rep(j,i,i+i-1) print T[j] << ' '; print nl; i*=2; } print nl; } void solve(){ if(pref.back() < 0){ print -1 << nl; exit(0); } MINI = 0; L = R = M[0]; ustaw(1,0,POT-1); rep(i,1,n){ L = 0; R = M[pref[i]]; MINI = MAXI; wartosc(1,0,POT-1); dp[i] = MINI; if(pref[i]-pref[i-1] >= 0) mine(dp[i], dp[i-1]); L = R; MINI = dp[i]; T[1].lazy++; ustaw(1,0,POT-1); } print dp.back() << nl; } int main(){ fastDataTransfer(); int testCases = 1; // cin >> testCases; rep(testNumber, 1, testCases){ read(); solve(); } } |