#include <cstdio>
#include <cmath>
#include <climits>
#include <functional>
#include <numeric>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll=long long;
#define N 300005
#define ALL(x) x.begin(), x.end()
int n, k, a[N];
long long p[N];
pair<ll, int> dp[N][2];
const long double EPS = 1e-3;
int check(ll lambda)
{
dp[0][1] = make_pair(-1e18, 0);
for (int i = 1; i <= n; ++i)
{
dp[i][0] = max({dp[i-1][0], dp[i-1][1]});
dp[i][1] = max(make_pair(dp[i-1][0].first + a[i] - lambda, dp[i-1][0].second + 1),
make_pair(dp[i-1][1].first + a[i], dp[i-1][1].second));
}
return max(dp[n][0], dp[n][1]).second >= k;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
ll l = 0, r = 3e12+5, z = 0;
while (l <= r)
{
ll y = (l+r)/2;
if (check(y)) z = y, l = y + 1;
else r = y - 1;
}
check(z);
cout << (long long)round(k * z + max(dp[n][0], dp[n][1]).first);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
13312 KB |
Output is correct |
2 |
Correct |
145 ms |
13264 KB |
Output is correct |
3 |
Correct |
149 ms |
13136 KB |
Output is correct |
4 |
Correct |
145 ms |
13260 KB |
Output is correct |
5 |
Correct |
144 ms |
13336 KB |
Output is correct |
6 |
Correct |
144 ms |
13148 KB |
Output is correct |
7 |
Correct |
141 ms |
13524 KB |
Output is correct |
8 |
Correct |
145 ms |
13340 KB |
Output is correct |
9 |
Correct |
143 ms |
13316 KB |
Output is correct |
10 |
Correct |
142 ms |
13332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
13136 KB |
Output is correct |
2 |
Correct |
129 ms |
13352 KB |
Output is correct |
3 |
Correct |
125 ms |
13328 KB |
Output is correct |
4 |
Correct |
131 ms |
13148 KB |
Output is correct |
5 |
Correct |
143 ms |
13320 KB |
Output is correct |
6 |
Correct |
128 ms |
13324 KB |
Output is correct |
7 |
Correct |
133 ms |
13148 KB |
Output is correct |
8 |
Correct |
146 ms |
13144 KB |
Output is correct |
9 |
Correct |
142 ms |
13148 KB |
Output is correct |
10 |
Correct |
133 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
137 ms |
13144 KB |
Output is correct |
2 |
Correct |
136 ms |
13140 KB |
Output is correct |
3 |
Correct |
136 ms |
13136 KB |
Output is correct |
4 |
Correct |
139 ms |
13396 KB |
Output is correct |
5 |
Correct |
137 ms |
13140 KB |
Output is correct |
6 |
Correct |
140 ms |
13148 KB |
Output is correct |
7 |
Correct |
138 ms |
13348 KB |
Output is correct |
8 |
Correct |
145 ms |
13336 KB |
Output is correct |
9 |
Correct |
142 ms |
13140 KB |
Output is correct |
10 |
Correct |
138 ms |
13144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2576 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
2 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
13312 KB |
Output is correct |
2 |
Correct |
145 ms |
13264 KB |
Output is correct |
3 |
Correct |
149 ms |
13136 KB |
Output is correct |
4 |
Correct |
145 ms |
13260 KB |
Output is correct |
5 |
Correct |
144 ms |
13336 KB |
Output is correct |
6 |
Correct |
144 ms |
13148 KB |
Output is correct |
7 |
Correct |
141 ms |
13524 KB |
Output is correct |
8 |
Correct |
145 ms |
13340 KB |
Output is correct |
9 |
Correct |
143 ms |
13316 KB |
Output is correct |
10 |
Correct |
142 ms |
13332 KB |
Output is correct |
11 |
Correct |
125 ms |
13136 KB |
Output is correct |
12 |
Correct |
129 ms |
13352 KB |
Output is correct |
13 |
Correct |
125 ms |
13328 KB |
Output is correct |
14 |
Correct |
131 ms |
13148 KB |
Output is correct |
15 |
Correct |
143 ms |
13320 KB |
Output is correct |
16 |
Correct |
128 ms |
13324 KB |
Output is correct |
17 |
Correct |
133 ms |
13148 KB |
Output is correct |
18 |
Correct |
146 ms |
13144 KB |
Output is correct |
19 |
Correct |
142 ms |
13148 KB |
Output is correct |
20 |
Correct |
133 ms |
13148 KB |
Output is correct |
21 |
Correct |
137 ms |
13144 KB |
Output is correct |
22 |
Correct |
136 ms |
13140 KB |
Output is correct |
23 |
Correct |
136 ms |
13136 KB |
Output is correct |
24 |
Correct |
139 ms |
13396 KB |
Output is correct |
25 |
Correct |
137 ms |
13140 KB |
Output is correct |
26 |
Correct |
140 ms |
13148 KB |
Output is correct |
27 |
Correct |
138 ms |
13348 KB |
Output is correct |
28 |
Correct |
145 ms |
13336 KB |
Output is correct |
29 |
Correct |
142 ms |
13140 KB |
Output is correct |
30 |
Correct |
138 ms |
13144 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2392 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
0 ms |
2396 KB |
Output is correct |
40 |
Correct |
0 ms |
2396 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
1 ms |
2396 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2396 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
2 ms |
2396 KB |
Output is correct |
52 |
Correct |
2 ms |
2652 KB |
Output is correct |
53 |
Correct |
2 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
2576 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
2 ms |
2396 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
1 ms |
2396 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
2 ms |
2396 KB |
Output is correct |
61 |
Correct |
142 ms |
13112 KB |
Output is correct |
62 |
Correct |
142 ms |
13140 KB |
Output is correct |
63 |
Correct |
136 ms |
13320 KB |
Output is correct |
64 |
Correct |
141 ms |
13344 KB |
Output is correct |
65 |
Correct |
144 ms |
13344 KB |
Output is correct |
66 |
Correct |
141 ms |
13136 KB |
Output is correct |
67 |
Correct |
142 ms |
13328 KB |
Output is correct |
68 |
Correct |
138 ms |
13340 KB |
Output is correct |
69 |
Correct |
142 ms |
13116 KB |
Output is correct |
70 |
Correct |
137 ms |
13140 KB |
Output is correct |