#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
const int N = 500001;
const ll INF = 1e18;
int n;
vector <int> a;
ll dp[N], tree[4 * N], lazy[4 * N];
void push(int l, int r, int node){
tree[node] += lazy[node];
if(l != r){
lazy[2 * node] += lazy[node];
lazy[2 * node + 1] += lazy[node];
}
lazy[node] = 0;
}
void update(int l, int r, int node, int s, int e, ll v){
if(r < l) return;
push(l, r, node);
if(r < s || l > e) return;
if(s <= l && r <= e){
lazy[node] += v;
push(l, r, node);
return;
}
int mid = (l + r) / 2;
update(l, mid, 2 * node, s, e, v);
update(mid + 1, r, 2 * node + 1, s, e, v);
tree[node] = min(tree[2 * node], tree[2 * node + 1]);
}
int query(int l, int r, int node, int p){
if(r < l || l > p) return -1;
push(l, r, node);
if(r <= p && tree[node] > 0) return -1;
if(l == r) return l;
int mid = (l + r) / 2;
int ret = query(mid + 1, r, 2 * node + 1, p);
if(ret != -1) return ret;
return query(l, mid, 2 * node, p);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
for(int i = 0 ; i < 4 * N ; i++){
tree[i] = INF;
}
cin >> n;
a.resize(n + 1);
for(int i = 1 ; i <= n ; i++){
cin >> a[i];
}
for(int i = 1 ; i <= n ; i++){
ll sum = 0;
for(int j = i ; j >= 1 ; j--){
sum += a[j];
if(sum >= dp[j - 1]){
dp[i] = sum;
break;
}
}
}
update(0, n, 1, 0, 0, -INF);
vector <int> ans(n + 1);
for(int i = 1 ; i <= n ; i++){
update(0, n, 1, 0, i - 1, -a[i]);
int idx = query(0, n, 1, i - 1);
ans[i] = ans[idx] + 1;
update(0, n, 1, i, i, -INF + dp[i]);
}
cout << ans[n] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16000 KB |
Output is correct |
2 |
Correct |
9 ms |
16000 KB |
Output is correct |
3 |
Correct |
9 ms |
16000 KB |
Output is correct |
4 |
Correct |
8 ms |
16000 KB |
Output is correct |
5 |
Correct |
9 ms |
16000 KB |
Output is correct |
6 |
Correct |
9 ms |
16000 KB |
Output is correct |
7 |
Correct |
9 ms |
16000 KB |
Output is correct |
8 |
Correct |
9 ms |
16000 KB |
Output is correct |
9 |
Correct |
8 ms |
16000 KB |
Output is correct |
10 |
Correct |
9 ms |
16000 KB |
Output is correct |
11 |
Correct |
9 ms |
16000 KB |
Output is correct |
12 |
Correct |
9 ms |
16032 KB |
Output is correct |
13 |
Correct |
9 ms |
16000 KB |
Output is correct |
14 |
Correct |
9 ms |
16000 KB |
Output is correct |
15 |
Correct |
9 ms |
16000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16000 KB |
Output is correct |
2 |
Correct |
9 ms |
16000 KB |
Output is correct |
3 |
Correct |
9 ms |
16000 KB |
Output is correct |
4 |
Correct |
8 ms |
16000 KB |
Output is correct |
5 |
Correct |
9 ms |
16000 KB |
Output is correct |
6 |
Correct |
9 ms |
16000 KB |
Output is correct |
7 |
Correct |
9 ms |
16000 KB |
Output is correct |
8 |
Correct |
9 ms |
16000 KB |
Output is correct |
9 |
Correct |
8 ms |
16000 KB |
Output is correct |
10 |
Correct |
9 ms |
16000 KB |
Output is correct |
11 |
Correct |
9 ms |
16000 KB |
Output is correct |
12 |
Correct |
9 ms |
16032 KB |
Output is correct |
13 |
Correct |
9 ms |
16000 KB |
Output is correct |
14 |
Correct |
9 ms |
16000 KB |
Output is correct |
15 |
Correct |
9 ms |
16000 KB |
Output is correct |
16 |
Correct |
9 ms |
16000 KB |
Output is correct |
17 |
Correct |
9 ms |
16000 KB |
Output is correct |
18 |
Correct |
9 ms |
16000 KB |
Output is correct |
19 |
Correct |
9 ms |
16000 KB |
Output is correct |
20 |
Correct |
10 ms |
16000 KB |
Output is correct |
21 |
Correct |
10 ms |
16128 KB |
Output is correct |
22 |
Correct |
9 ms |
16000 KB |
Output is correct |
23 |
Correct |
9 ms |
16128 KB |
Output is correct |
24 |
Correct |
9 ms |
16000 KB |
Output is correct |
25 |
Correct |
9 ms |
16000 KB |
Output is correct |
26 |
Correct |
9 ms |
16000 KB |
Output is correct |
27 |
Correct |
9 ms |
16000 KB |
Output is correct |
28 |
Correct |
9 ms |
16000 KB |
Output is correct |
29 |
Correct |
9 ms |
16000 KB |
Output is correct |
30 |
Correct |
9 ms |
16000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16000 KB |
Output is correct |
2 |
Correct |
9 ms |
16000 KB |
Output is correct |
3 |
Correct |
9 ms |
16000 KB |
Output is correct |
4 |
Correct |
8 ms |
16000 KB |
Output is correct |
5 |
Correct |
9 ms |
16000 KB |
Output is correct |
6 |
Correct |
9 ms |
16000 KB |
Output is correct |
7 |
Correct |
9 ms |
16000 KB |
Output is correct |
8 |
Correct |
9 ms |
16000 KB |
Output is correct |
9 |
Correct |
8 ms |
16000 KB |
Output is correct |
10 |
Correct |
9 ms |
16000 KB |
Output is correct |
11 |
Correct |
9 ms |
16000 KB |
Output is correct |
12 |
Correct |
9 ms |
16032 KB |
Output is correct |
13 |
Correct |
9 ms |
16000 KB |
Output is correct |
14 |
Correct |
9 ms |
16000 KB |
Output is correct |
15 |
Correct |
9 ms |
16000 KB |
Output is correct |
16 |
Correct |
9 ms |
16000 KB |
Output is correct |
17 |
Correct |
9 ms |
16000 KB |
Output is correct |
18 |
Correct |
9 ms |
16000 KB |
Output is correct |
19 |
Correct |
9 ms |
16000 KB |
Output is correct |
20 |
Correct |
10 ms |
16000 KB |
Output is correct |
21 |
Correct |
10 ms |
16128 KB |
Output is correct |
22 |
Correct |
9 ms |
16000 KB |
Output is correct |
23 |
Correct |
9 ms |
16128 KB |
Output is correct |
24 |
Correct |
9 ms |
16000 KB |
Output is correct |
25 |
Correct |
9 ms |
16000 KB |
Output is correct |
26 |
Correct |
9 ms |
16000 KB |
Output is correct |
27 |
Correct |
9 ms |
16000 KB |
Output is correct |
28 |
Correct |
9 ms |
16000 KB |
Output is correct |
29 |
Correct |
9 ms |
16000 KB |
Output is correct |
30 |
Correct |
9 ms |
16000 KB |
Output is correct |
31 |
Correct |
14 ms |
16172 KB |
Output is correct |
32 |
Correct |
12 ms |
16156 KB |
Output is correct |
33 |
Correct |
11 ms |
16128 KB |
Output is correct |
34 |
Correct |
11 ms |
16128 KB |
Output is correct |
35 |
Correct |
11 ms |
16128 KB |
Output is correct |
36 |
Correct |
12 ms |
16128 KB |
Output is correct |
37 |
Correct |
11 ms |
16128 KB |
Output is correct |
38 |
Correct |
10 ms |
16000 KB |
Output is correct |
39 |
Correct |
11 ms |
16128 KB |
Output is correct |
40 |
Correct |
11 ms |
16128 KB |
Output is correct |
41 |
Correct |
11 ms |
16128 KB |
Output is correct |
42 |
Correct |
11 ms |
16128 KB |
Output is correct |
43 |
Correct |
11 ms |
16128 KB |
Output is correct |
44 |
Correct |
10 ms |
16128 KB |
Output is correct |
45 |
Correct |
11 ms |
16128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16000 KB |
Output is correct |
2 |
Correct |
9 ms |
16000 KB |
Output is correct |
3 |
Correct |
9 ms |
16000 KB |
Output is correct |
4 |
Correct |
8 ms |
16000 KB |
Output is correct |
5 |
Correct |
9 ms |
16000 KB |
Output is correct |
6 |
Correct |
9 ms |
16000 KB |
Output is correct |
7 |
Correct |
9 ms |
16000 KB |
Output is correct |
8 |
Correct |
9 ms |
16000 KB |
Output is correct |
9 |
Correct |
8 ms |
16000 KB |
Output is correct |
10 |
Correct |
9 ms |
16000 KB |
Output is correct |
11 |
Correct |
9 ms |
16000 KB |
Output is correct |
12 |
Correct |
9 ms |
16032 KB |
Output is correct |
13 |
Correct |
9 ms |
16000 KB |
Output is correct |
14 |
Correct |
9 ms |
16000 KB |
Output is correct |
15 |
Correct |
9 ms |
16000 KB |
Output is correct |
16 |
Correct |
9 ms |
16000 KB |
Output is correct |
17 |
Correct |
9 ms |
16000 KB |
Output is correct |
18 |
Correct |
9 ms |
16000 KB |
Output is correct |
19 |
Correct |
9 ms |
16000 KB |
Output is correct |
20 |
Correct |
10 ms |
16000 KB |
Output is correct |
21 |
Correct |
10 ms |
16128 KB |
Output is correct |
22 |
Correct |
9 ms |
16000 KB |
Output is correct |
23 |
Correct |
9 ms |
16128 KB |
Output is correct |
24 |
Correct |
9 ms |
16000 KB |
Output is correct |
25 |
Correct |
9 ms |
16000 KB |
Output is correct |
26 |
Correct |
9 ms |
16000 KB |
Output is correct |
27 |
Correct |
9 ms |
16000 KB |
Output is correct |
28 |
Correct |
9 ms |
16000 KB |
Output is correct |
29 |
Correct |
9 ms |
16000 KB |
Output is correct |
30 |
Correct |
9 ms |
16000 KB |
Output is correct |
31 |
Correct |
14 ms |
16172 KB |
Output is correct |
32 |
Correct |
12 ms |
16156 KB |
Output is correct |
33 |
Correct |
11 ms |
16128 KB |
Output is correct |
34 |
Correct |
11 ms |
16128 KB |
Output is correct |
35 |
Correct |
11 ms |
16128 KB |
Output is correct |
36 |
Correct |
12 ms |
16128 KB |
Output is correct |
37 |
Correct |
11 ms |
16128 KB |
Output is correct |
38 |
Correct |
10 ms |
16000 KB |
Output is correct |
39 |
Correct |
11 ms |
16128 KB |
Output is correct |
40 |
Correct |
11 ms |
16128 KB |
Output is correct |
41 |
Correct |
11 ms |
16128 KB |
Output is correct |
42 |
Correct |
11 ms |
16128 KB |
Output is correct |
43 |
Correct |
11 ms |
16128 KB |
Output is correct |
44 |
Correct |
10 ms |
16128 KB |
Output is correct |
45 |
Correct |
11 ms |
16128 KB |
Output is correct |
46 |
Execution timed out |
1581 ms |
17132 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16000 KB |
Output is correct |
2 |
Correct |
9 ms |
16000 KB |
Output is correct |
3 |
Correct |
9 ms |
16000 KB |
Output is correct |
4 |
Correct |
8 ms |
16000 KB |
Output is correct |
5 |
Correct |
9 ms |
16000 KB |
Output is correct |
6 |
Correct |
9 ms |
16000 KB |
Output is correct |
7 |
Correct |
9 ms |
16000 KB |
Output is correct |
8 |
Correct |
9 ms |
16000 KB |
Output is correct |
9 |
Correct |
8 ms |
16000 KB |
Output is correct |
10 |
Correct |
9 ms |
16000 KB |
Output is correct |
11 |
Correct |
9 ms |
16000 KB |
Output is correct |
12 |
Correct |
9 ms |
16032 KB |
Output is correct |
13 |
Correct |
9 ms |
16000 KB |
Output is correct |
14 |
Correct |
9 ms |
16000 KB |
Output is correct |
15 |
Correct |
9 ms |
16000 KB |
Output is correct |
16 |
Correct |
9 ms |
16000 KB |
Output is correct |
17 |
Correct |
9 ms |
16000 KB |
Output is correct |
18 |
Correct |
9 ms |
16000 KB |
Output is correct |
19 |
Correct |
9 ms |
16000 KB |
Output is correct |
20 |
Correct |
10 ms |
16000 KB |
Output is correct |
21 |
Correct |
10 ms |
16128 KB |
Output is correct |
22 |
Correct |
9 ms |
16000 KB |
Output is correct |
23 |
Correct |
9 ms |
16128 KB |
Output is correct |
24 |
Correct |
9 ms |
16000 KB |
Output is correct |
25 |
Correct |
9 ms |
16000 KB |
Output is correct |
26 |
Correct |
9 ms |
16000 KB |
Output is correct |
27 |
Correct |
9 ms |
16000 KB |
Output is correct |
28 |
Correct |
9 ms |
16000 KB |
Output is correct |
29 |
Correct |
9 ms |
16000 KB |
Output is correct |
30 |
Correct |
9 ms |
16000 KB |
Output is correct |
31 |
Correct |
14 ms |
16172 KB |
Output is correct |
32 |
Correct |
12 ms |
16156 KB |
Output is correct |
33 |
Correct |
11 ms |
16128 KB |
Output is correct |
34 |
Correct |
11 ms |
16128 KB |
Output is correct |
35 |
Correct |
11 ms |
16128 KB |
Output is correct |
36 |
Correct |
12 ms |
16128 KB |
Output is correct |
37 |
Correct |
11 ms |
16128 KB |
Output is correct |
38 |
Correct |
10 ms |
16000 KB |
Output is correct |
39 |
Correct |
11 ms |
16128 KB |
Output is correct |
40 |
Correct |
11 ms |
16128 KB |
Output is correct |
41 |
Correct |
11 ms |
16128 KB |
Output is correct |
42 |
Correct |
11 ms |
16128 KB |
Output is correct |
43 |
Correct |
11 ms |
16128 KB |
Output is correct |
44 |
Correct |
10 ms |
16128 KB |
Output is correct |
45 |
Correct |
11 ms |
16128 KB |
Output is correct |
46 |
Execution timed out |
1581 ms |
17132 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |