#include <bits/stdc++.h> #define ff first #define ss second using namespace std; const int inf = 1e9 + 7; int n, m; int t[500007], dp[500007]; long long pref[500007]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> t[i]; pref[i + 1] = pref[i] + (long long)t[i]; } if (pref[n] < 0) { cout << "NIE\n"; return 0; } if (t[0] >= 0) dp[0] = 1; else dp[0] = -inf; for (int i = 1; i < n - 1; i++) { if (pref[n] - pref[i + 1] >= 0) { if (pref[i + 1] >=0) dp[i] = 0; else dp[i] = -inf; for (int j = 0; j < i; j++) { if (pref[i + 1] - pref[j + 1] >= 0) { dp[i] = max(dp[i], dp[j]); } } dp[i]++; } else { dp[i] = -inf; } //cout << dp[i] << " "; } for (int i = 0; i < n; i++) { m = max(m, dp[i]); } cout << n - 1 - m << "\n"; 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 | #include <bits/stdc++.h> #define ff first #define ss second using namespace std; const int inf = 1e9 + 7; int n, m; int t[500007], dp[500007]; long long pref[500007]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> t[i]; pref[i + 1] = pref[i] + (long long)t[i]; } if (pref[n] < 0) { cout << "NIE\n"; return 0; } if (t[0] >= 0) dp[0] = 1; else dp[0] = -inf; for (int i = 1; i < n - 1; i++) { if (pref[n] - pref[i + 1] >= 0) { if (pref[i + 1] >=0) dp[i] = 0; else dp[i] = -inf; for (int j = 0; j < i; j++) { if (pref[i + 1] - pref[j + 1] >= 0) { dp[i] = max(dp[i], dp[j]); } } dp[i]++; } else { dp[i] = -inf; } //cout << dp[i] << " "; } for (int i = 0; i < n; i++) { m = max(m, dp[i]); } cout << n - 1 - m << "\n"; return 0; } |