#include <bits/stdc++.h>
using i64 = long long;
#ifdef DEBUG
#include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
#define debug(...) void(23)
#endif
bool chmax(int& a, int b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
std::cin >> N;
std::vector<int> A(N);
for (int i = 0; i < N; ++i) {
std::cin >> A[i];
}
std::vector<i64> pre(N + 1);
for (int i = 0; i < N; ++i) {
pre[i + 1] = pre[i] + A[i];
}
std::vector<std::map<int, int>> dp(N + 1);
dp[0][0] = 0;
for (int i = 0; i < N; ++i) {
while (dp[i].size() > 2) {
dp[i].erase(dp[i].begin());
}
debug(i, dp[i]);
for (auto[ans, v] : dp[i]) {
chmax(dp[i + 1][ans], v);
auto it = std::lower_bound(pre.begin() + i + 1, pre.end(), 2 * pre[i] - pre[v]);
int idx = it - pre.begin();
if (idx <= N) {
chmax(dp[idx][ans + 1], i);
}
}
}
debug(N, dp[N]);
while (dp[N].size() > 1) {
dp[N].erase(dp[N].begin());
}
std::cout << dp[N].begin()->first << '\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... |