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;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
const int maxn = 5e5 + 20;
int a[maxn];
long long pref[maxn];
long long mi[maxn];
int dp[maxn];
vector<int> q[maxn];
bool cmp(long long val, int i) {
return val < pref[i] + mi[i];
}
void just_do_it() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
q[0].push_back(0);
int res = 0;
for (int i = 1; i <= n; i++) {
if (pref[q[res][0]] + mi[q[res][0]] <= pref[i]) {
res++;
}
int j = *prev(upper_bound(q[res - 1].begin(), q[res - 1].end(), pref[i], cmp));
mi[i] = pref[i] - pref[j];
while (!q[res].empty() && pref[q[res].back()] + mi[q[res].back()] >= pref[i] + mi[i]) {
q[res].pop_back();
}
q[res].push_back(i);
}
cout << res;
}
# | 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... |