#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=3e5;
int n, k, a[mxN], cnt[mxN];
ll dp[mxN];
int solve(ll x) {
ll s=0;
ar<ll, 2> cur={0, 0};
for (int i=0; i<n; ++i) {
s+=a[i];
dp[i]=i?dp[i-1]:0;
cnt[i]=i?cnt[i-1]:0;
if (cur[0]+s-x>dp[i])
dp[i]=cur[0]+s-x, cnt[i]=cur[1]+1;
if (dp[i]-s>cur[0])
cur={dp[i]-s, cnt[i]};
}
return cnt[n-1];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i=0; i<n; ++i)
cin >> a[i];
ll lb=0, rb=1e15;
while(lb<rb) {
ll m=(lb+rb)/2;
if (solve(m)<=k)
rb=m;
else
lb=m+1;
}
solve(lb);
cout << dp[n-1]+k*lb;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
4860 KB |
Output is correct |
2 |
Correct |
79 ms |
7768 KB |
Output is correct |
3 |
Correct |
79 ms |
7868 KB |
Output is correct |
4 |
Correct |
85 ms |
7908 KB |
Output is correct |
5 |
Correct |
92 ms |
7788 KB |
Output is correct |
6 |
Correct |
82 ms |
7620 KB |
Output is correct |
7 |
Correct |
77 ms |
7660 KB |
Output is correct |
8 |
Correct |
79 ms |
7732 KB |
Output is correct |
9 |
Correct |
77 ms |
7628 KB |
Output is correct |
10 |
Correct |
83 ms |
7620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
4932 KB |
Output is correct |
2 |
Correct |
71 ms |
6068 KB |
Output is correct |
3 |
Correct |
70 ms |
5992 KB |
Output is correct |
4 |
Correct |
69 ms |
6044 KB |
Output is correct |
5 |
Correct |
80 ms |
7656 KB |
Output is correct |
6 |
Correct |
70 ms |
5988 KB |
Output is correct |
7 |
Correct |
73 ms |
6132 KB |
Output is correct |
8 |
Correct |
79 ms |
7748 KB |
Output is correct |
9 |
Correct |
78 ms |
7620 KB |
Output is correct |
10 |
Correct |
69 ms |
5984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
4944 KB |
Output is correct |
2 |
Correct |
108 ms |
7876 KB |
Output is correct |
3 |
Correct |
107 ms |
7920 KB |
Output is correct |
4 |
Correct |
104 ms |
7876 KB |
Output is correct |
5 |
Correct |
106 ms |
8044 KB |
Output is correct |
6 |
Correct |
107 ms |
7992 KB |
Output is correct |
7 |
Correct |
108 ms |
8000 KB |
Output is correct |
8 |
Correct |
105 ms |
7876 KB |
Output is correct |
9 |
Correct |
107 ms |
7992 KB |
Output is correct |
10 |
Correct |
108 ms |
7932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
328 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
4860 KB |
Output is correct |
2 |
Correct |
79 ms |
7768 KB |
Output is correct |
3 |
Correct |
79 ms |
7868 KB |
Output is correct |
4 |
Correct |
85 ms |
7908 KB |
Output is correct |
5 |
Correct |
92 ms |
7788 KB |
Output is correct |
6 |
Correct |
82 ms |
7620 KB |
Output is correct |
7 |
Correct |
77 ms |
7660 KB |
Output is correct |
8 |
Correct |
79 ms |
7732 KB |
Output is correct |
9 |
Correct |
77 ms |
7628 KB |
Output is correct |
10 |
Correct |
83 ms |
7620 KB |
Output is correct |
11 |
Correct |
71 ms |
4932 KB |
Output is correct |
12 |
Correct |
71 ms |
6068 KB |
Output is correct |
13 |
Correct |
70 ms |
5992 KB |
Output is correct |
14 |
Correct |
69 ms |
6044 KB |
Output is correct |
15 |
Correct |
80 ms |
7656 KB |
Output is correct |
16 |
Correct |
70 ms |
5988 KB |
Output is correct |
17 |
Correct |
73 ms |
6132 KB |
Output is correct |
18 |
Correct |
79 ms |
7748 KB |
Output is correct |
19 |
Correct |
78 ms |
7620 KB |
Output is correct |
20 |
Correct |
69 ms |
5984 KB |
Output is correct |
21 |
Correct |
107 ms |
4944 KB |
Output is correct |
22 |
Correct |
108 ms |
7876 KB |
Output is correct |
23 |
Correct |
107 ms |
7920 KB |
Output is correct |
24 |
Correct |
104 ms |
7876 KB |
Output is correct |
25 |
Correct |
106 ms |
8044 KB |
Output is correct |
26 |
Correct |
107 ms |
7992 KB |
Output is correct |
27 |
Correct |
108 ms |
8000 KB |
Output is correct |
28 |
Correct |
105 ms |
7876 KB |
Output is correct |
29 |
Correct |
107 ms |
7992 KB |
Output is correct |
30 |
Correct |
108 ms |
7932 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
320 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
320 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
2 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
2 ms |
332 KB |
Output is correct |
52 |
Correct |
2 ms |
332 KB |
Output is correct |
53 |
Correct |
2 ms |
332 KB |
Output is correct |
54 |
Correct |
2 ms |
332 KB |
Output is correct |
55 |
Correct |
2 ms |
332 KB |
Output is correct |
56 |
Correct |
2 ms |
328 KB |
Output is correct |
57 |
Correct |
2 ms |
332 KB |
Output is correct |
58 |
Correct |
2 ms |
332 KB |
Output is correct |
59 |
Correct |
2 ms |
332 KB |
Output is correct |
60 |
Correct |
2 ms |
332 KB |
Output is correct |
61 |
Correct |
151 ms |
7856 KB |
Output is correct |
62 |
Correct |
147 ms |
8048 KB |
Output is correct |
63 |
Correct |
137 ms |
7836 KB |
Output is correct |
64 |
Correct |
153 ms |
8028 KB |
Output is correct |
65 |
Correct |
155 ms |
7920 KB |
Output is correct |
66 |
Correct |
157 ms |
8064 KB |
Output is correct |
67 |
Correct |
153 ms |
7748 KB |
Output is correct |
68 |
Correct |
108 ms |
7984 KB |
Output is correct |
69 |
Correct |
106 ms |
7956 KB |
Output is correct |
70 |
Correct |
108 ms |
7912 KB |
Output is correct |