This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<long long> pref(a.begin(), a.end());
for (int i = 1; i < n; i++) {
pref[i] += pref[i - 1];
}
const int inf = 123456789;
vector<vector<int>> dp(n, vector<int> (n, -1));
function<int(int, int)> DP = [&](int i, int j) {
if (i == n) {
return 1;
}
if (dp[i][j] != -1) {
return dp[i][j];
}
int res = -inf;
res = max(res, DP(i + 1, j));
long long tSum = pref[i] - (j > 0 ? pref[j - 1] : 0);
int low = j + 1, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
long long cSum = pref[mid] - pref[i];
if (cSum >= tSum) {
high = mid - 1;
} else {
low = mid + 1;
}
}
if (high + 1 < n) {
res = max(res, DP(high + 1, i + 1) + 1);
}
return dp[i][j] = res;
};
cout << DP(0, 0) << '\n';
return 0;
}
# | 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... |