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>
#define all(x) x.begin(), x.end()
#define y second
#define x first
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
const int N = 5e5+10;
void fin() {
#ifdef AM
freopen(".in", "r", stdin);
#endif
}
ll p[N], a[N];
pii dp[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr), fin();
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) p[i] += p[i - 1] + a[i];
stack<pair<ll, ll>> s;
s.push({0, 0});
for (int i = 1; i <= n; i++) {
dp[i] = {1, p[i]};
while (!s.empty() && dp[s.top().x].y + s.top().y <= p[i]) {
if (dp[s.top().x].x+1 == dp[i].x) {
dp[i].y = min(dp[i].y, s.top().y);
}
if (dp[s.top().x].x+1 > dp[i].x) {
dp[i] = pii{dp[s.top().x].x+1, p[i]-s.top().y};
}
s.pop();
}
s.push({i, p[i]});
}
cout << dp[n].x;
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... |