#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> dp(n + 1);
vector<int> e(n + 1);
e[0] = 0;
dp[0] = 0;
for(int i = 1; i <= n; i++) {
int sum = 0;
int mx = 0;
for(int j = i; j >= 1; j--) {
sum += a[j];
if(sum >= e[j - 1]) {
mx = max(mx, dp[j - 1]);
}
}
sum = 0;
int idx = - 1;
int mn = 1e18;
for(int j = i; j >= 1; j--) {
sum += a[j];
if(sum >= e[j - 1] && mx == dp[j - 1]) {
if(mn > sum) {
idx = j - 1;
mn = min(mn, sum);
}
}
}
if(idx == - 1) {
dp[i] = dp[i - 1];
e[i] = e[i - 1] + a[i];
}
else {
dp[i] = dp[idx] + 1;
e[i] = mn;
}
}
cout << dp[n];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |