#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
typedef long long ll;
typedef pair<int, ll> pii;
const int N = 5e5 + 2;
int n, a[N];
pii dp[N];
ll pre[N];
set<pair<ll, pii> > s;
pii cmp_dp(pii x, pii y) {
if (x.X == y.X) {
if (x.Y < y.Y) return x;
return y;
}
if (x.X > y.X) return x;
return y;
}
bool higher(pii x, pii y) {
if (x.X == y.X) return x.Y > y.Y;
return x.X > y.X;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
pre[0] = a[0];
for (int i = 1; i < n; ++i) pre[i] = a[i] + pre[i - 1];
for (int i = 0; i < n; ++i) {
dp[i] = {1, pre[i]};
auto it = s.lower_bound({pre[i] + 1, {0, 0}});
if (it != s.begin()) {
--it;
dp[i] = cmp_dp(dp[i], {(*it).Y.X + 1, pre[i] - (*it).Y.Y});
}
pii cur = {dp[i].X, pre[i]};
ll x = dp[i].Y + pre[i];
it = s.lower_bound({x, {0, 0}});
while (it != s.end() && !higher((*it).second, cur)) {
s.erase(it);
it = s.lower_bound({x, {0, 0}});
}
it = s.lower_bound({x + 1, {0, 0}});
if (it != s.begin() && !higher(cur, (*--it).second)) continue;
s.insert({x, cur});
}
cout << dp[n - 1].X << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4440 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4440 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
2 ms |
4696 KB |
Output is correct |
35 |
Correct |
1 ms |
4700 KB |
Output is correct |
36 |
Correct |
2 ms |
4700 KB |
Output is correct |
37 |
Correct |
1 ms |
4700 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
4700 KB |
Output is correct |
40 |
Correct |
1 ms |
4700 KB |
Output is correct |
41 |
Correct |
1 ms |
4700 KB |
Output is correct |
42 |
Correct |
1 ms |
4700 KB |
Output is correct |
43 |
Correct |
1 ms |
4700 KB |
Output is correct |
44 |
Correct |
1 ms |
4700 KB |
Output is correct |
45 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4440 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
2 ms |
4696 KB |
Output is correct |
35 |
Correct |
1 ms |
4700 KB |
Output is correct |
36 |
Correct |
2 ms |
4700 KB |
Output is correct |
37 |
Correct |
1 ms |
4700 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
4700 KB |
Output is correct |
40 |
Correct |
1 ms |
4700 KB |
Output is correct |
41 |
Correct |
1 ms |
4700 KB |
Output is correct |
42 |
Correct |
1 ms |
4700 KB |
Output is correct |
43 |
Correct |
1 ms |
4700 KB |
Output is correct |
44 |
Correct |
1 ms |
4700 KB |
Output is correct |
45 |
Correct |
1 ms |
4700 KB |
Output is correct |
46 |
Correct |
41 ms |
14932 KB |
Output is correct |
47 |
Correct |
36 ms |
13400 KB |
Output is correct |
48 |
Correct |
28 ms |
9916 KB |
Output is correct |
49 |
Correct |
43 ms |
13140 KB |
Output is correct |
50 |
Correct |
47 ms |
15956 KB |
Output is correct |
51 |
Correct |
48 ms |
14676 KB |
Output is correct |
52 |
Correct |
22 ms |
11604 KB |
Output is correct |
53 |
Correct |
12 ms |
10564 KB |
Output is correct |
54 |
Correct |
42 ms |
13448 KB |
Output is correct |
55 |
Correct |
54 ms |
15444 KB |
Output is correct |
56 |
Correct |
50 ms |
15384 KB |
Output is correct |
57 |
Correct |
44 ms |
14928 KB |
Output is correct |
58 |
Correct |
42 ms |
14672 KB |
Output is correct |
59 |
Correct |
45 ms |
15444 KB |
Output is correct |
60 |
Correct |
48 ms |
15440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
0 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4440 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4700 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
2 ms |
4696 KB |
Output is correct |
35 |
Correct |
1 ms |
4700 KB |
Output is correct |
36 |
Correct |
2 ms |
4700 KB |
Output is correct |
37 |
Correct |
1 ms |
4700 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
4700 KB |
Output is correct |
40 |
Correct |
1 ms |
4700 KB |
Output is correct |
41 |
Correct |
1 ms |
4700 KB |
Output is correct |
42 |
Correct |
1 ms |
4700 KB |
Output is correct |
43 |
Correct |
1 ms |
4700 KB |
Output is correct |
44 |
Correct |
1 ms |
4700 KB |
Output is correct |
45 |
Correct |
1 ms |
4700 KB |
Output is correct |
46 |
Correct |
41 ms |
14932 KB |
Output is correct |
47 |
Correct |
36 ms |
13400 KB |
Output is correct |
48 |
Correct |
28 ms |
9916 KB |
Output is correct |
49 |
Correct |
43 ms |
13140 KB |
Output is correct |
50 |
Correct |
47 ms |
15956 KB |
Output is correct |
51 |
Correct |
48 ms |
14676 KB |
Output is correct |
52 |
Correct |
22 ms |
11604 KB |
Output is correct |
53 |
Correct |
12 ms |
10564 KB |
Output is correct |
54 |
Correct |
42 ms |
13448 KB |
Output is correct |
55 |
Correct |
54 ms |
15444 KB |
Output is correct |
56 |
Correct |
50 ms |
15384 KB |
Output is correct |
57 |
Correct |
44 ms |
14928 KB |
Output is correct |
58 |
Correct |
42 ms |
14672 KB |
Output is correct |
59 |
Correct |
45 ms |
15444 KB |
Output is correct |
60 |
Correct |
48 ms |
15440 KB |
Output is correct |
61 |
Correct |
251 ms |
47184 KB |
Output is correct |
62 |
Correct |
258 ms |
47444 KB |
Output is correct |
63 |
Correct |
227 ms |
39092 KB |
Output is correct |
64 |
Correct |
239 ms |
24916 KB |
Output is correct |
65 |
Correct |
281 ms |
50260 KB |
Output is correct |
66 |
Correct |
282 ms |
43604 KB |
Output is correct |
67 |
Correct |
227 ms |
37460 KB |
Output is correct |
68 |
Correct |
102 ms |
22608 KB |
Output is correct |
69 |
Correct |
268 ms |
38076 KB |
Output is correct |
70 |
Correct |
298 ms |
47700 KB |
Output is correct |
71 |
Correct |
264 ms |
47184 KB |
Output is correct |
72 |
Correct |
252 ms |
41484 KB |
Output is correct |
73 |
Correct |
258 ms |
43600 KB |
Output is correct |
74 |
Correct |
300 ms |
48720 KB |
Output is correct |
75 |
Correct |
296 ms |
48492 KB |
Output is correct |