#include <bits/stdc++.h>
using namespace std;
class SegTree {
public:
int sz;
vector<long long> coords;
vector<pair<int, int>> tree;
SegTree(int sz, vector<long long> coords) : sz(sz), coords(coords), tree(2 * sz, {-1e9, -1e9}) {}
void Update(long long p, pair<int, int> x) {
p = lower_bound(begin(coords), end(coords), p) - begin(coords);
if (p == sz) return;
for (p += sz; p > 0; p /= 2) tree[p] = max(tree[p], x);
}
pair<int, int> Query(long long l, long long r) {
l = lower_bound(begin(coords), end(coords), l) - begin(coords);
r = upper_bound(begin(coords), end(coords), r) - begin(coords);
pair<int, int> res = {-1e9, -1e9};
for (l += sz, r += sz; l < r; l /= 2, r /= 2) {
if (l & 1) res = max(res, tree[l++]);
if (r & 1) res = max(res, tree[--r]);
}
return res;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
vector<long long> A(N + 1);
for (int i = 1; i <= N; i++) {
cin >> A[i];
A[i] += A[i - 1];
}
// dp[i] = (max_segments, last_segment_sum)
// it is always optimal to maximize max_segments first, since
// the more segments, the smaller the last_sum.
//
// dp[i] = j < i: (dp[j].seg + 1, prefix[i] - prefix[j]) if prefix[i] - prefix[j] >= dp[j].sum
// criteria: prefix[i] >= dp[j].sum + prefix[j]
// put (dp[j]) on position (dp[j].sum + prefix[j]), then:
// We want max in position <= prefix[i]; find maximum possible j with maximum possible seg
// (since we want to minimize prefix[i] - prefix[j])
// Just use segment tree. Time complexity: O(N log N).
SegTree seg(N + 1, A);
vector<pair<int, long long>> dp(N + 1);
dp[0] = {0, 0};
seg.Update(0, {0, 0});
for (int i = 1; i <= N; i++) {
auto q = seg.Query(0, A[i]);
dp[i] = {q.first + 1, A[i] - A[q.second]};
seg.Update(dp[i].second + A[i], {dp[i].first, i});
}
cout << dp[N].first << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
316 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
316 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
316 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
316 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
456 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
460 KB |
Output is correct |
35 |
Correct |
2 ms |
460 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
460 KB |
Output is correct |
38 |
Correct |
1 ms |
328 KB |
Output is correct |
39 |
Correct |
2 ms |
460 KB |
Output is correct |
40 |
Correct |
2 ms |
460 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
2 ms |
464 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
2 ms |
460 KB |
Output is correct |
45 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
316 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
316 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
456 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
460 KB |
Output is correct |
35 |
Correct |
2 ms |
460 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
460 KB |
Output is correct |
38 |
Correct |
1 ms |
328 KB |
Output is correct |
39 |
Correct |
2 ms |
460 KB |
Output is correct |
40 |
Correct |
2 ms |
460 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
2 ms |
464 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
2 ms |
460 KB |
Output is correct |
45 |
Correct |
2 ms |
460 KB |
Output is correct |
46 |
Correct |
28 ms |
5160 KB |
Output is correct |
47 |
Correct |
34 ms |
5436 KB |
Output is correct |
48 |
Correct |
40 ms |
5824 KB |
Output is correct |
49 |
Correct |
39 ms |
5944 KB |
Output is correct |
50 |
Correct |
40 ms |
5948 KB |
Output is correct |
51 |
Correct |
40 ms |
5948 KB |
Output is correct |
52 |
Correct |
20 ms |
3140 KB |
Output is correct |
53 |
Correct |
12 ms |
2016 KB |
Output is correct |
54 |
Correct |
37 ms |
5156 KB |
Output is correct |
55 |
Correct |
40 ms |
5948 KB |
Output is correct |
56 |
Correct |
35 ms |
5436 KB |
Output is correct |
57 |
Correct |
33 ms |
5444 KB |
Output is correct |
58 |
Correct |
34 ms |
5396 KB |
Output is correct |
59 |
Correct |
36 ms |
5632 KB |
Output is correct |
60 |
Correct |
37 ms |
5564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
316 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
316 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
456 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
460 KB |
Output is correct |
35 |
Correct |
2 ms |
460 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
2 ms |
460 KB |
Output is correct |
38 |
Correct |
1 ms |
328 KB |
Output is correct |
39 |
Correct |
2 ms |
460 KB |
Output is correct |
40 |
Correct |
2 ms |
460 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
2 ms |
464 KB |
Output is correct |
43 |
Correct |
2 ms |
460 KB |
Output is correct |
44 |
Correct |
2 ms |
460 KB |
Output is correct |
45 |
Correct |
2 ms |
460 KB |
Output is correct |
46 |
Correct |
28 ms |
5160 KB |
Output is correct |
47 |
Correct |
34 ms |
5436 KB |
Output is correct |
48 |
Correct |
40 ms |
5824 KB |
Output is correct |
49 |
Correct |
39 ms |
5944 KB |
Output is correct |
50 |
Correct |
40 ms |
5948 KB |
Output is correct |
51 |
Correct |
40 ms |
5948 KB |
Output is correct |
52 |
Correct |
20 ms |
3140 KB |
Output is correct |
53 |
Correct |
12 ms |
2016 KB |
Output is correct |
54 |
Correct |
37 ms |
5156 KB |
Output is correct |
55 |
Correct |
40 ms |
5948 KB |
Output is correct |
56 |
Correct |
35 ms |
5436 KB |
Output is correct |
57 |
Correct |
33 ms |
5444 KB |
Output is correct |
58 |
Correct |
34 ms |
5396 KB |
Output is correct |
59 |
Correct |
36 ms |
5632 KB |
Output is correct |
60 |
Correct |
37 ms |
5564 KB |
Output is correct |
61 |
Correct |
153 ms |
25764 KB |
Output is correct |
62 |
Correct |
167 ms |
25768 KB |
Output is correct |
63 |
Correct |
224 ms |
26236 KB |
Output is correct |
64 |
Correct |
217 ms |
28468 KB |
Output is correct |
65 |
Correct |
202 ms |
28796 KB |
Output is correct |
66 |
Correct |
222 ms |
28732 KB |
Output is correct |
67 |
Correct |
176 ms |
22984 KB |
Output is correct |
68 |
Correct |
81 ms |
11584 KB |
Output is correct |
69 |
Correct |
210 ms |
24856 KB |
Output is correct |
70 |
Correct |
217 ms |
28724 KB |
Output is correct |
71 |
Correct |
187 ms |
25664 KB |
Output is correct |
72 |
Correct |
180 ms |
26156 KB |
Output is correct |
73 |
Correct |
223 ms |
26752 KB |
Output is correct |
74 |
Correct |
232 ms |
27008 KB |
Output is correct |
75 |
Correct |
193 ms |
27132 KB |
Output is correct |