#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<pair<int,int>> dp(n + 1);
dp[0] = {0, 0};
for(int i = 1; i <= n; i++) {
int sum = 0;
vector<pair<int,int>> V;
int mx = 0;
for(int j = i; j >= 1; j--) {
sum += a[j];
if(sum >= dp[j - 1].second) mx = max(mx, dp[j - 1].first);
}
sum = 0;
for(int j = i; j >= 1; j--) {
sum += a[j];
if(sum >= dp[j - 1].second && dp[j - 1].first == mx) {
V.push_back({sum, dp[j - 1].first});
}
}
sort(V.begin(), V.end());
if(!V.empty()) {
dp[i] = {V[0].second + 1, V[0].first};
}
else {
dp[i] = {dp[i - 1].first, dp[i - 1].second + a[i]};
}
}
cout << dp[n].first;
}
# | 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... |