#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
const ll NMAX = 300001;
const ll nr_of_bits = 16;
const ll MOD = 1000000007;
ll cnt[NMAX][2];
ll dp[NMAX][2];
ll n, k;
ll v[NMAX];
pii OK(ll cost){
for(ll i = 1; i <= n; i++){
if(dp[i - 1][1] - cost > dp[i - 1][0]){
cnt[i][0] = cnt[i - 1][1] + 1;
dp[i][0] = dp[i - 1][1] - cost;
}else{
cnt[i][0] = cnt[i - 1][0];
dp[i][0] = dp[i - 1][0];
}
if(dp[i - 1][1] > dp[i - 1][0]){
cnt[i][1] = cnt[i - 1][1];
dp[i][1] = dp[i - 1][1] + v[i];
}else{
cnt[i][1] = cnt[i - 1][0];
dp[i][1] = dp[i - 1][0] + v[i];
}
}
ll cfinal, dfinal;
if(dp[n][1] - cost > dp[n][0]){
dfinal = dp[n][1] - cost;
cfinal = cnt[n][1] + 1;
}else{
dfinal = dp[n][0];
cfinal = cnt[n][0];
}
return {dfinal + cost * k, cfinal};
}
int main() {
//ifstream cin(".in");
//ofstream cout(".out");
ll i;
cin >> n;
cin >> k;
for(i = 1; i <= n; i++){
cin >> v[i];
}
ll r = 0, pas = (1LL << 50);
while(pas){
if(OK(r + pas).second > k)
r += pas;
pas /= 2;
}
if(OK(r).second > k)
r++;
cout << OK(r).first;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
11564 KB |
Output is correct |
2 |
Correct |
180 ms |
11816 KB |
Output is correct |
3 |
Correct |
185 ms |
11972 KB |
Output is correct |
4 |
Correct |
182 ms |
11936 KB |
Output is correct |
5 |
Correct |
179 ms |
11848 KB |
Output is correct |
6 |
Correct |
184 ms |
11716 KB |
Output is correct |
7 |
Correct |
180 ms |
11588 KB |
Output is correct |
8 |
Correct |
181 ms |
11804 KB |
Output is correct |
9 |
Correct |
177 ms |
11628 KB |
Output is correct |
10 |
Correct |
192 ms |
11812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
11744 KB |
Output is correct |
2 |
Correct |
130 ms |
11964 KB |
Output is correct |
3 |
Correct |
124 ms |
11684 KB |
Output is correct |
4 |
Correct |
128 ms |
11716 KB |
Output is correct |
5 |
Correct |
176 ms |
11612 KB |
Output is correct |
6 |
Correct |
124 ms |
11716 KB |
Output is correct |
7 |
Correct |
131 ms |
11912 KB |
Output is correct |
8 |
Correct |
183 ms |
11860 KB |
Output is correct |
9 |
Correct |
177 ms |
11584 KB |
Output is correct |
10 |
Correct |
127 ms |
11844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
210 ms |
11864 KB |
Output is correct |
2 |
Correct |
208 ms |
14660 KB |
Output is correct |
3 |
Correct |
210 ms |
14796 KB |
Output is correct |
4 |
Correct |
216 ms |
14736 KB |
Output is correct |
5 |
Correct |
206 ms |
14768 KB |
Output is correct |
6 |
Correct |
210 ms |
14904 KB |
Output is correct |
7 |
Correct |
214 ms |
15152 KB |
Output is correct |
8 |
Correct |
208 ms |
14788 KB |
Output is correct |
9 |
Correct |
212 ms |
14944 KB |
Output is correct |
10 |
Correct |
216 ms |
15044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
208 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 |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
208 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 |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 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 |
320 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 |
320 KB |
Output is correct |
26 |
Correct |
2 ms |
324 KB |
Output is correct |
27 |
Correct |
2 ms |
316 KB |
Output is correct |
28 |
Correct |
2 ms |
344 KB |
Output is correct |
29 |
Correct |
2 ms |
316 KB |
Output is correct |
30 |
Correct |
2 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
11564 KB |
Output is correct |
2 |
Correct |
180 ms |
11816 KB |
Output is correct |
3 |
Correct |
185 ms |
11972 KB |
Output is correct |
4 |
Correct |
182 ms |
11936 KB |
Output is correct |
5 |
Correct |
179 ms |
11848 KB |
Output is correct |
6 |
Correct |
184 ms |
11716 KB |
Output is correct |
7 |
Correct |
180 ms |
11588 KB |
Output is correct |
8 |
Correct |
181 ms |
11804 KB |
Output is correct |
9 |
Correct |
177 ms |
11628 KB |
Output is correct |
10 |
Correct |
192 ms |
11812 KB |
Output is correct |
11 |
Correct |
126 ms |
11744 KB |
Output is correct |
12 |
Correct |
130 ms |
11964 KB |
Output is correct |
13 |
Correct |
124 ms |
11684 KB |
Output is correct |
14 |
Correct |
128 ms |
11716 KB |
Output is correct |
15 |
Correct |
176 ms |
11612 KB |
Output is correct |
16 |
Correct |
124 ms |
11716 KB |
Output is correct |
17 |
Correct |
131 ms |
11912 KB |
Output is correct |
18 |
Correct |
183 ms |
11860 KB |
Output is correct |
19 |
Correct |
177 ms |
11584 KB |
Output is correct |
20 |
Correct |
127 ms |
11844 KB |
Output is correct |
21 |
Correct |
210 ms |
11864 KB |
Output is correct |
22 |
Correct |
208 ms |
14660 KB |
Output is correct |
23 |
Correct |
210 ms |
14796 KB |
Output is correct |
24 |
Correct |
216 ms |
14736 KB |
Output is correct |
25 |
Correct |
206 ms |
14768 KB |
Output is correct |
26 |
Correct |
210 ms |
14904 KB |
Output is correct |
27 |
Correct |
214 ms |
15152 KB |
Output is correct |
28 |
Correct |
208 ms |
14788 KB |
Output is correct |
29 |
Correct |
212 ms |
14944 KB |
Output is correct |
30 |
Correct |
216 ms |
15044 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 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 |
208 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
304 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 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 |
320 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 |
320 KB |
Output is correct |
56 |
Correct |
2 ms |
324 KB |
Output is correct |
57 |
Correct |
2 ms |
316 KB |
Output is correct |
58 |
Correct |
2 ms |
344 KB |
Output is correct |
59 |
Correct |
2 ms |
316 KB |
Output is correct |
60 |
Correct |
2 ms |
308 KB |
Output is correct |
61 |
Correct |
250 ms |
14632 KB |
Output is correct |
62 |
Correct |
249 ms |
15040 KB |
Output is correct |
63 |
Correct |
222 ms |
14588 KB |
Output is correct |
64 |
Correct |
253 ms |
14916 KB |
Output is correct |
65 |
Correct |
262 ms |
14868 KB |
Output is correct |
66 |
Correct |
251 ms |
14912 KB |
Output is correct |
67 |
Correct |
248 ms |
14592 KB |
Output is correct |
68 |
Correct |
214 ms |
15036 KB |
Output is correct |
69 |
Correct |
204 ms |
14696 KB |
Output is correct |
70 |
Correct |
206 ms |
14532 KB |
Output is correct |