#include <bits/stdc++.h>
#define int long long
#define ll long long
#define mp make_pair
#define fi first
#define se second
#define Sz(x) (int)x.size()
using namespace std;
const int N = 3005;
const int inf = INT_MAX;
int n, a[N], dp[N][N], pref[N];
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
dp[i][j] = inf;
}
}
for (int i = 1; i <= n; i++) {
dp[i][0] = 1;
}
for (int i = 2; i <= n; i++) {
for (int j = 1; j < i; j++) {
int posR = i;
int posL = j + 1;
int sum = pref[posR] - pref[posL - 1];
int l = 0, r = j - 1, best = -1;
while (l <= r) {
int mid = (l + r) / 2;
if ((pref[j] - pref[mid]) > sum) {
l = mid + 1;
continue;
}
if (dp[j][mid] == inf) {
r = mid - 1;
}
else {
l = mid + 1;
best = mid;
}
}
if (best != -1) {
dp[i][j] = dp[j][best] + 1;
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (dp[n][i] != inf) ans = max(ans, dp[n][i]);
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int tt = 1;
// cin >> tt;
while (tt--) {
solve();
cout << '\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... |