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;
const int Nmax = 5e5 + 5;
typedef long long ll;
ll s[Nmax];
int n, a[Nmax];
pair<int, ll> dp[Nmax];
class AIB
{
int a[Nmax];
int ub(int x) { return (x&(-x)); }
public:
int query(int x)
{
int ans = 0;
for(; x; x-=ub(x)) ans = max(ans, a[x]);
return ans;
}
void update(int x, int y)
{
for(; x<=n; x+=ub(x)) a[x] = max(a[x], y);
}
} aib;
void add(int pos)
{
int leftmost = lower_bound(s+1, s+n+1, s[pos] + dp[pos].second) - s;
aib.update(leftmost, pos);
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false); cin.tie(0);
cin >> n;
int i;
for(i=1; i<=n; ++i) cin >> a[i];
for(i=1; i<=n; ++i) s[i] = s[i-1] + a[i];
for(i=1; i<=n; ++i)
{
int j = aib.query(i);
dp[i] = { dp[j].first + 1, s[i] - s[j] };
add(i);
}
cout << dp[n].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... |