#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define sc second
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
const ll nmax = 5e5+6;
const ll inf = 1e18;
ll a[nmax];
ll pref[nmax];
ll n;
struct segtre{
ll st[nmax * 4];
void init(int n){
for(int i = 1;i<=4*n;i++) st[i] = inf;
}
void update(int id,int l,int r,int i,ll x){
if(i>r ||i <l) return;
if(l==r) {
st[id] = x;
return;
}
int mid = (l+r)/2;
update(id*2,l,mid,i,x);
update(id*2+1,mid+1,r,i,x);
st[id] = min(st[id*2],st[id*2+1]);
}
ll walk(int id,int l,int r,ll x){
if (l==r) return l ;
int mid = (l+r)/2;
if(st[id*2] <= x) return walk(id*2,l,mid,x);
return walk(id*2+1,mid+1,r,x);
}
} tree;
ll dp[nmax];
ll f[nmax];
void solve(){
cin >> n;
for(int i = 1;i<=n;i++){
cin >> a[i];
// pref[i] = pref[i-1] + a[i];
}
reverse(a + 1, a + n + 1);
tree.init(n+1);
tree.update(1,1,n+1,n+1,0);
for(int i = n;i>=1;i--){
pref[i] = pref[i+1] + a[i];
int get = tree.walk(1,1,n+1,pref[i]);
// cout << get << '\n';
f[i] = pref[i] - pref[get];
dp[i] = dp[get] + 1;
tree.update(1,1,n+1,i,f[i] + pref[i]);
}
// cout << f[2];
cout << dp[1];
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6560 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6560 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6616 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6560 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6616 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
2 ms |
6628 KB |
Output is correct |
33 |
Correct |
2 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
2 ms |
6488 KB |
Output is correct |
36 |
Correct |
1 ms |
6744 KB |
Output is correct |
37 |
Correct |
1 ms |
6492 KB |
Output is correct |
38 |
Correct |
1 ms |
6496 KB |
Output is correct |
39 |
Correct |
1 ms |
6496 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
1 ms |
6496 KB |
Output is correct |
42 |
Correct |
1 ms |
6500 KB |
Output is correct |
43 |
Correct |
2 ms |
6628 KB |
Output is correct |
44 |
Correct |
2 ms |
6492 KB |
Output is correct |
45 |
Correct |
1 ms |
6496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6560 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6616 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
2 ms |
6628 KB |
Output is correct |
33 |
Correct |
2 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
2 ms |
6488 KB |
Output is correct |
36 |
Correct |
1 ms |
6744 KB |
Output is correct |
37 |
Correct |
1 ms |
6492 KB |
Output is correct |
38 |
Correct |
1 ms |
6496 KB |
Output is correct |
39 |
Correct |
1 ms |
6496 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
1 ms |
6496 KB |
Output is correct |
42 |
Correct |
1 ms |
6500 KB |
Output is correct |
43 |
Correct |
2 ms |
6628 KB |
Output is correct |
44 |
Correct |
2 ms |
6492 KB |
Output is correct |
45 |
Correct |
1 ms |
6496 KB |
Output is correct |
46 |
Correct |
18 ms |
15712 KB |
Output is correct |
47 |
Correct |
19 ms |
15712 KB |
Output is correct |
48 |
Correct |
20 ms |
16032 KB |
Output is correct |
49 |
Correct |
21 ms |
16224 KB |
Output is correct |
50 |
Correct |
22 ms |
16224 KB |
Output is correct |
51 |
Correct |
22 ms |
16216 KB |
Output is correct |
52 |
Correct |
11 ms |
13404 KB |
Output is correct |
53 |
Correct |
8 ms |
11356 KB |
Output is correct |
54 |
Correct |
19 ms |
15536 KB |
Output is correct |
55 |
Correct |
22 ms |
16208 KB |
Output is correct |
56 |
Correct |
20 ms |
15704 KB |
Output is correct |
57 |
Correct |
19 ms |
15704 KB |
Output is correct |
58 |
Correct |
19 ms |
15708 KB |
Output is correct |
59 |
Correct |
20 ms |
15952 KB |
Output is correct |
60 |
Correct |
20 ms |
15960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6560 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6488 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6616 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
2 ms |
6628 KB |
Output is correct |
33 |
Correct |
2 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
2 ms |
6488 KB |
Output is correct |
36 |
Correct |
1 ms |
6744 KB |
Output is correct |
37 |
Correct |
1 ms |
6492 KB |
Output is correct |
38 |
Correct |
1 ms |
6496 KB |
Output is correct |
39 |
Correct |
1 ms |
6496 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
1 ms |
6496 KB |
Output is correct |
42 |
Correct |
1 ms |
6500 KB |
Output is correct |
43 |
Correct |
2 ms |
6628 KB |
Output is correct |
44 |
Correct |
2 ms |
6492 KB |
Output is correct |
45 |
Correct |
1 ms |
6496 KB |
Output is correct |
46 |
Correct |
18 ms |
15712 KB |
Output is correct |
47 |
Correct |
19 ms |
15712 KB |
Output is correct |
48 |
Correct |
20 ms |
16032 KB |
Output is correct |
49 |
Correct |
21 ms |
16224 KB |
Output is correct |
50 |
Correct |
22 ms |
16224 KB |
Output is correct |
51 |
Correct |
22 ms |
16216 KB |
Output is correct |
52 |
Correct |
11 ms |
13404 KB |
Output is correct |
53 |
Correct |
8 ms |
11356 KB |
Output is correct |
54 |
Correct |
19 ms |
15536 KB |
Output is correct |
55 |
Correct |
22 ms |
16208 KB |
Output is correct |
56 |
Correct |
20 ms |
15704 KB |
Output is correct |
57 |
Correct |
19 ms |
15704 KB |
Output is correct |
58 |
Correct |
19 ms |
15708 KB |
Output is correct |
59 |
Correct |
20 ms |
15952 KB |
Output is correct |
60 |
Correct |
20 ms |
15960 KB |
Output is correct |
61 |
Correct |
101 ms |
33532 KB |
Output is correct |
62 |
Correct |
94 ms |
33496 KB |
Output is correct |
63 |
Correct |
96 ms |
33872 KB |
Output is correct |
64 |
Correct |
108 ms |
36104 KB |
Output is correct |
65 |
Correct |
112 ms |
36448 KB |
Output is correct |
66 |
Correct |
108 ms |
36432 KB |
Output is correct |
67 |
Correct |
87 ms |
32852 KB |
Output is correct |
68 |
Correct |
45 ms |
22356 KB |
Output is correct |
69 |
Correct |
98 ms |
32612 KB |
Output is correct |
70 |
Correct |
111 ms |
36432 KB |
Output is correct |
71 |
Correct |
95 ms |
33524 KB |
Output is correct |
72 |
Correct |
94 ms |
34000 KB |
Output is correct |
73 |
Correct |
98 ms |
34508 KB |
Output is correct |
74 |
Correct |
113 ms |
35008 KB |
Output is correct |
75 |
Correct |
106 ms |
34896 KB |
Output is correct |