#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MP make_pair
struct SegmentTree{
int size = 1;
vector<pair<int, ll>> tree;
SegmentTree(int n){
size = n;
tree.resize((1 << 20) + 50);
};
void update(int v, int tl, int tr, int pos, pair<int, ll> val){
if (tl == tr){
tree[v] = max(tree[v], val);
return;
}
int tm = (tl + tr) >> 1;
if (pos <= tm) update(v << 1, tl, tm, pos, val); else
update(v << 1 | 1, tm + 1, tr, pos, val);
tree[v] = max(tree[v << 1], tree[v << 1 | 1]);
}
void update(int pos, pair<int, ll> val){
update(1, 0, size - 1, pos, val);
}
pair<int, ll> get(int v, int tl, int tr, int l, int r){
if (l <= tl && tr <= r) return tree[v];
if (tl > r || tr < l) return MP(0, 0);
int tm = (tl + tr) >> 1;
return max(get(v << 1, tl, tm, l, r), get(v << 1 | 1, tm + 1, tr, l, r));
}
pair<int, ll> get(int l, int r){
return get(1, 0, size - 1, l, r);
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int n;
cin >> n;
vector<ll> pref(n);
for (int i = 0; i < n; i++){
cin >> pref[i];
if (i) pref[i] += pref[i - 1];
}
vector<int> dp(n, 1);
vector<ll> sum = pref;
SegmentTree st(n);
for (int i = 0; i < n; i++){
auto p = st.get(0, i);
if (p.first > dp[i] || p.second == dp[i] || pref[i] - p.second < sum[i]){
dp[i] = p.first + 1;
sum[i] = pref[i] - p.second;
}
int pos = lower_bound(pref.begin(), pref.end(), pref[i] + sum[i]) - pref.begin();
if (pos < n) st.update(pos, MP(dp[i], pref[i]));
}
cout << dp[n - 1] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16716 KB |
Output is correct |
2 |
Correct |
9 ms |
16716 KB |
Output is correct |
3 |
Correct |
8 ms |
16716 KB |
Output is correct |
4 |
Correct |
7 ms |
16684 KB |
Output is correct |
5 |
Correct |
8 ms |
16716 KB |
Output is correct |
6 |
Correct |
7 ms |
16716 KB |
Output is correct |
7 |
Correct |
7 ms |
16716 KB |
Output is correct |
8 |
Correct |
7 ms |
16716 KB |
Output is correct |
9 |
Correct |
9 ms |
16716 KB |
Output is correct |
10 |
Correct |
9 ms |
16716 KB |
Output is correct |
11 |
Correct |
9 ms |
16716 KB |
Output is correct |
12 |
Correct |
7 ms |
16716 KB |
Output is correct |
13 |
Correct |
10 ms |
16716 KB |
Output is correct |
14 |
Correct |
9 ms |
16716 KB |
Output is correct |
15 |
Correct |
7 ms |
16716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16716 KB |
Output is correct |
2 |
Correct |
9 ms |
16716 KB |
Output is correct |
3 |
Correct |
8 ms |
16716 KB |
Output is correct |
4 |
Correct |
7 ms |
16684 KB |
Output is correct |
5 |
Correct |
8 ms |
16716 KB |
Output is correct |
6 |
Correct |
7 ms |
16716 KB |
Output is correct |
7 |
Correct |
7 ms |
16716 KB |
Output is correct |
8 |
Correct |
7 ms |
16716 KB |
Output is correct |
9 |
Correct |
9 ms |
16716 KB |
Output is correct |
10 |
Correct |
9 ms |
16716 KB |
Output is correct |
11 |
Correct |
9 ms |
16716 KB |
Output is correct |
12 |
Correct |
7 ms |
16716 KB |
Output is correct |
13 |
Correct |
10 ms |
16716 KB |
Output is correct |
14 |
Correct |
9 ms |
16716 KB |
Output is correct |
15 |
Correct |
7 ms |
16716 KB |
Output is correct |
16 |
Correct |
7 ms |
16716 KB |
Output is correct |
17 |
Correct |
9 ms |
16660 KB |
Output is correct |
18 |
Correct |
7 ms |
16716 KB |
Output is correct |
19 |
Correct |
7 ms |
16716 KB |
Output is correct |
20 |
Correct |
7 ms |
16716 KB |
Output is correct |
21 |
Correct |
7 ms |
16716 KB |
Output is correct |
22 |
Correct |
9 ms |
16716 KB |
Output is correct |
23 |
Correct |
7 ms |
16716 KB |
Output is correct |
24 |
Correct |
10 ms |
16664 KB |
Output is correct |
25 |
Correct |
7 ms |
16708 KB |
Output is correct |
26 |
Correct |
7 ms |
16716 KB |
Output is correct |
27 |
Correct |
7 ms |
16688 KB |
Output is correct |
28 |
Correct |
9 ms |
16724 KB |
Output is correct |
29 |
Correct |
8 ms |
16716 KB |
Output is correct |
30 |
Correct |
8 ms |
16716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16716 KB |
Output is correct |
2 |
Correct |
9 ms |
16716 KB |
Output is correct |
3 |
Correct |
8 ms |
16716 KB |
Output is correct |
4 |
Correct |
7 ms |
16684 KB |
Output is correct |
5 |
Correct |
8 ms |
16716 KB |
Output is correct |
6 |
Correct |
7 ms |
16716 KB |
Output is correct |
7 |
Correct |
7 ms |
16716 KB |
Output is correct |
8 |
Correct |
7 ms |
16716 KB |
Output is correct |
9 |
Correct |
9 ms |
16716 KB |
Output is correct |
10 |
Correct |
9 ms |
16716 KB |
Output is correct |
11 |
Correct |
9 ms |
16716 KB |
Output is correct |
12 |
Correct |
7 ms |
16716 KB |
Output is correct |
13 |
Correct |
10 ms |
16716 KB |
Output is correct |
14 |
Correct |
9 ms |
16716 KB |
Output is correct |
15 |
Correct |
7 ms |
16716 KB |
Output is correct |
16 |
Correct |
7 ms |
16716 KB |
Output is correct |
17 |
Correct |
9 ms |
16660 KB |
Output is correct |
18 |
Correct |
7 ms |
16716 KB |
Output is correct |
19 |
Correct |
7 ms |
16716 KB |
Output is correct |
20 |
Correct |
7 ms |
16716 KB |
Output is correct |
21 |
Correct |
7 ms |
16716 KB |
Output is correct |
22 |
Correct |
9 ms |
16716 KB |
Output is correct |
23 |
Correct |
7 ms |
16716 KB |
Output is correct |
24 |
Correct |
10 ms |
16664 KB |
Output is correct |
25 |
Correct |
7 ms |
16708 KB |
Output is correct |
26 |
Correct |
7 ms |
16716 KB |
Output is correct |
27 |
Correct |
7 ms |
16688 KB |
Output is correct |
28 |
Correct |
9 ms |
16724 KB |
Output is correct |
29 |
Correct |
8 ms |
16716 KB |
Output is correct |
30 |
Correct |
8 ms |
16716 KB |
Output is correct |
31 |
Correct |
10 ms |
16744 KB |
Output is correct |
32 |
Correct |
11 ms |
16792 KB |
Output is correct |
33 |
Correct |
9 ms |
16744 KB |
Output is correct |
34 |
Correct |
10 ms |
16800 KB |
Output is correct |
35 |
Correct |
9 ms |
16716 KB |
Output is correct |
36 |
Correct |
11 ms |
16804 KB |
Output is correct |
37 |
Correct |
9 ms |
16716 KB |
Output is correct |
38 |
Correct |
10 ms |
16776 KB |
Output is correct |
39 |
Correct |
10 ms |
16716 KB |
Output is correct |
40 |
Correct |
9 ms |
16684 KB |
Output is correct |
41 |
Correct |
9 ms |
16724 KB |
Output is correct |
42 |
Correct |
9 ms |
16728 KB |
Output is correct |
43 |
Correct |
11 ms |
16800 KB |
Output is correct |
44 |
Correct |
9 ms |
16716 KB |
Output is correct |
45 |
Correct |
10 ms |
16716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16716 KB |
Output is correct |
2 |
Correct |
9 ms |
16716 KB |
Output is correct |
3 |
Correct |
8 ms |
16716 KB |
Output is correct |
4 |
Correct |
7 ms |
16684 KB |
Output is correct |
5 |
Correct |
8 ms |
16716 KB |
Output is correct |
6 |
Correct |
7 ms |
16716 KB |
Output is correct |
7 |
Correct |
7 ms |
16716 KB |
Output is correct |
8 |
Correct |
7 ms |
16716 KB |
Output is correct |
9 |
Correct |
9 ms |
16716 KB |
Output is correct |
10 |
Correct |
9 ms |
16716 KB |
Output is correct |
11 |
Correct |
9 ms |
16716 KB |
Output is correct |
12 |
Correct |
7 ms |
16716 KB |
Output is correct |
13 |
Correct |
10 ms |
16716 KB |
Output is correct |
14 |
Correct |
9 ms |
16716 KB |
Output is correct |
15 |
Correct |
7 ms |
16716 KB |
Output is correct |
16 |
Correct |
7 ms |
16716 KB |
Output is correct |
17 |
Correct |
9 ms |
16660 KB |
Output is correct |
18 |
Correct |
7 ms |
16716 KB |
Output is correct |
19 |
Correct |
7 ms |
16716 KB |
Output is correct |
20 |
Correct |
7 ms |
16716 KB |
Output is correct |
21 |
Correct |
7 ms |
16716 KB |
Output is correct |
22 |
Correct |
9 ms |
16716 KB |
Output is correct |
23 |
Correct |
7 ms |
16716 KB |
Output is correct |
24 |
Correct |
10 ms |
16664 KB |
Output is correct |
25 |
Correct |
7 ms |
16708 KB |
Output is correct |
26 |
Correct |
7 ms |
16716 KB |
Output is correct |
27 |
Correct |
7 ms |
16688 KB |
Output is correct |
28 |
Correct |
9 ms |
16724 KB |
Output is correct |
29 |
Correct |
8 ms |
16716 KB |
Output is correct |
30 |
Correct |
8 ms |
16716 KB |
Output is correct |
31 |
Correct |
10 ms |
16744 KB |
Output is correct |
32 |
Correct |
11 ms |
16792 KB |
Output is correct |
33 |
Correct |
9 ms |
16744 KB |
Output is correct |
34 |
Correct |
10 ms |
16800 KB |
Output is correct |
35 |
Correct |
9 ms |
16716 KB |
Output is correct |
36 |
Correct |
11 ms |
16804 KB |
Output is correct |
37 |
Correct |
9 ms |
16716 KB |
Output is correct |
38 |
Correct |
10 ms |
16776 KB |
Output is correct |
39 |
Correct |
10 ms |
16716 KB |
Output is correct |
40 |
Correct |
9 ms |
16684 KB |
Output is correct |
41 |
Correct |
9 ms |
16724 KB |
Output is correct |
42 |
Correct |
9 ms |
16728 KB |
Output is correct |
43 |
Correct |
11 ms |
16800 KB |
Output is correct |
44 |
Correct |
9 ms |
16716 KB |
Output is correct |
45 |
Correct |
10 ms |
16716 KB |
Output is correct |
46 |
Correct |
29 ms |
18712 KB |
Output is correct |
47 |
Correct |
39 ms |
19132 KB |
Output is correct |
48 |
Correct |
43 ms |
19512 KB |
Output is correct |
49 |
Correct |
49 ms |
19508 KB |
Output is correct |
50 |
Correct |
45 ms |
19672 KB |
Output is correct |
51 |
Correct |
60 ms |
19632 KB |
Output is correct |
52 |
Correct |
29 ms |
18200 KB |
Output is correct |
53 |
Correct |
19 ms |
17576 KB |
Output is correct |
54 |
Correct |
51 ms |
18904 KB |
Output is correct |
55 |
Correct |
51 ms |
19672 KB |
Output is correct |
56 |
Correct |
39 ms |
19120 KB |
Output is correct |
57 |
Correct |
41 ms |
19044 KB |
Output is correct |
58 |
Correct |
38 ms |
19144 KB |
Output is correct |
59 |
Correct |
40 ms |
19224 KB |
Output is correct |
60 |
Correct |
38 ms |
19268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16716 KB |
Output is correct |
2 |
Correct |
9 ms |
16716 KB |
Output is correct |
3 |
Correct |
8 ms |
16716 KB |
Output is correct |
4 |
Correct |
7 ms |
16684 KB |
Output is correct |
5 |
Correct |
8 ms |
16716 KB |
Output is correct |
6 |
Correct |
7 ms |
16716 KB |
Output is correct |
7 |
Correct |
7 ms |
16716 KB |
Output is correct |
8 |
Correct |
7 ms |
16716 KB |
Output is correct |
9 |
Correct |
9 ms |
16716 KB |
Output is correct |
10 |
Correct |
9 ms |
16716 KB |
Output is correct |
11 |
Correct |
9 ms |
16716 KB |
Output is correct |
12 |
Correct |
7 ms |
16716 KB |
Output is correct |
13 |
Correct |
10 ms |
16716 KB |
Output is correct |
14 |
Correct |
9 ms |
16716 KB |
Output is correct |
15 |
Correct |
7 ms |
16716 KB |
Output is correct |
16 |
Correct |
7 ms |
16716 KB |
Output is correct |
17 |
Correct |
9 ms |
16660 KB |
Output is correct |
18 |
Correct |
7 ms |
16716 KB |
Output is correct |
19 |
Correct |
7 ms |
16716 KB |
Output is correct |
20 |
Correct |
7 ms |
16716 KB |
Output is correct |
21 |
Correct |
7 ms |
16716 KB |
Output is correct |
22 |
Correct |
9 ms |
16716 KB |
Output is correct |
23 |
Correct |
7 ms |
16716 KB |
Output is correct |
24 |
Correct |
10 ms |
16664 KB |
Output is correct |
25 |
Correct |
7 ms |
16708 KB |
Output is correct |
26 |
Correct |
7 ms |
16716 KB |
Output is correct |
27 |
Correct |
7 ms |
16688 KB |
Output is correct |
28 |
Correct |
9 ms |
16724 KB |
Output is correct |
29 |
Correct |
8 ms |
16716 KB |
Output is correct |
30 |
Correct |
8 ms |
16716 KB |
Output is correct |
31 |
Correct |
10 ms |
16744 KB |
Output is correct |
32 |
Correct |
11 ms |
16792 KB |
Output is correct |
33 |
Correct |
9 ms |
16744 KB |
Output is correct |
34 |
Correct |
10 ms |
16800 KB |
Output is correct |
35 |
Correct |
9 ms |
16716 KB |
Output is correct |
36 |
Correct |
11 ms |
16804 KB |
Output is correct |
37 |
Correct |
9 ms |
16716 KB |
Output is correct |
38 |
Correct |
10 ms |
16776 KB |
Output is correct |
39 |
Correct |
10 ms |
16716 KB |
Output is correct |
40 |
Correct |
9 ms |
16684 KB |
Output is correct |
41 |
Correct |
9 ms |
16724 KB |
Output is correct |
42 |
Correct |
9 ms |
16728 KB |
Output is correct |
43 |
Correct |
11 ms |
16800 KB |
Output is correct |
44 |
Correct |
9 ms |
16716 KB |
Output is correct |
45 |
Correct |
10 ms |
16716 KB |
Output is correct |
46 |
Correct |
29 ms |
18712 KB |
Output is correct |
47 |
Correct |
39 ms |
19132 KB |
Output is correct |
48 |
Correct |
43 ms |
19512 KB |
Output is correct |
49 |
Correct |
49 ms |
19508 KB |
Output is correct |
50 |
Correct |
45 ms |
19672 KB |
Output is correct |
51 |
Correct |
60 ms |
19632 KB |
Output is correct |
52 |
Correct |
29 ms |
18200 KB |
Output is correct |
53 |
Correct |
19 ms |
17576 KB |
Output is correct |
54 |
Correct |
51 ms |
18904 KB |
Output is correct |
55 |
Correct |
51 ms |
19672 KB |
Output is correct |
56 |
Correct |
39 ms |
19120 KB |
Output is correct |
57 |
Correct |
41 ms |
19044 KB |
Output is correct |
58 |
Correct |
38 ms |
19144 KB |
Output is correct |
59 |
Correct |
40 ms |
19224 KB |
Output is correct |
60 |
Correct |
38 ms |
19268 KB |
Output is correct |
61 |
Correct |
106 ms |
28532 KB |
Output is correct |
62 |
Correct |
130 ms |
28396 KB |
Output is correct |
63 |
Correct |
164 ms |
28792 KB |
Output is correct |
64 |
Correct |
214 ms |
31068 KB |
Output is correct |
65 |
Correct |
203 ms |
31416 KB |
Output is correct |
66 |
Correct |
196 ms |
31356 KB |
Output is correct |
67 |
Correct |
164 ms |
28424 KB |
Output is correct |
68 |
Correct |
79 ms |
22468 KB |
Output is correct |
69 |
Correct |
169 ms |
27516 KB |
Output is correct |
70 |
Correct |
200 ms |
31300 KB |
Output is correct |
71 |
Correct |
173 ms |
28540 KB |
Output is correct |
72 |
Correct |
157 ms |
28792 KB |
Output is correct |
73 |
Correct |
175 ms |
29436 KB |
Output is correct |
74 |
Correct |
194 ms |
29836 KB |
Output is correct |
75 |
Correct |
180 ms |
29812 KB |
Output is correct |