#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
typedef pair<db, int> PLI;
const LL INF = 1e18;
PLI f(VI& a, LL lambda)
{
int n = SZ(a);
PLI dp0 = {0, 0};
PLI dp1 = {-INF, 0};
FOR (i, 0, n)
{
PLI ndp0 = max(dp0, dp1);
PLI ndp1 = max(PLI(dp0.F + a[i] - lambda, dp0.S + 1), PLI(dp1.F + a[i], dp1.S));
dp0 = ndp0;
dp1 = ndp1;
//cerr << i << ' ' << dp0.F << ' ' << dp0.S << ' ' << dp1.F << ' ' << dp1.S << '\n';
}
return max(dp0, dp1);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
VI a(n);
FOR (i, 0, n)
cin >> a[i];
LL l = -1, r = INF;
while (l + 1 < r)
{
LL m = (l + r) / 2;
auto [val, cnt] = f(a, m);
if (cnt > k)
l = m;
else
r = m;
}
//cerr << r << '\n';
auto [val, cnt] = f(a, r);
cout << LL(val + cnt * r) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
1368 KB |
Output is correct |
2 |
Correct |
51 ms |
1372 KB |
Output is correct |
3 |
Correct |
57 ms |
1628 KB |
Output is correct |
4 |
Correct |
50 ms |
1372 KB |
Output is correct |
5 |
Correct |
49 ms |
1624 KB |
Output is correct |
6 |
Correct |
50 ms |
1368 KB |
Output is correct |
7 |
Correct |
50 ms |
1368 KB |
Output is correct |
8 |
Correct |
52 ms |
1628 KB |
Output is correct |
9 |
Correct |
59 ms |
1372 KB |
Output is correct |
10 |
Correct |
49 ms |
1624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
1372 KB |
Output is correct |
2 |
Correct |
54 ms |
1628 KB |
Output is correct |
3 |
Correct |
46 ms |
1368 KB |
Output is correct |
4 |
Correct |
46 ms |
1372 KB |
Output is correct |
5 |
Correct |
50 ms |
1372 KB |
Output is correct |
6 |
Correct |
58 ms |
1372 KB |
Output is correct |
7 |
Correct |
47 ms |
1628 KB |
Output is correct |
8 |
Correct |
64 ms |
1624 KB |
Output is correct |
9 |
Correct |
60 ms |
1372 KB |
Output is correct |
10 |
Correct |
45 ms |
1624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
1628 KB |
Output is correct |
2 |
Correct |
119 ms |
4548 KB |
Output is correct |
3 |
Correct |
95 ms |
4436 KB |
Output is correct |
4 |
Correct |
99 ms |
4440 KB |
Output is correct |
5 |
Correct |
116 ms |
4436 KB |
Output is correct |
6 |
Correct |
98 ms |
4444 KB |
Output is correct |
7 |
Correct |
96 ms |
4440 KB |
Output is correct |
8 |
Correct |
102 ms |
4432 KB |
Output is correct |
9 |
Correct |
101 ms |
4444 KB |
Output is correct |
10 |
Correct |
98 ms |
4424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
456 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
456 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
1368 KB |
Output is correct |
2 |
Correct |
51 ms |
1372 KB |
Output is correct |
3 |
Correct |
57 ms |
1628 KB |
Output is correct |
4 |
Correct |
50 ms |
1372 KB |
Output is correct |
5 |
Correct |
49 ms |
1624 KB |
Output is correct |
6 |
Correct |
50 ms |
1368 KB |
Output is correct |
7 |
Correct |
50 ms |
1368 KB |
Output is correct |
8 |
Correct |
52 ms |
1628 KB |
Output is correct |
9 |
Correct |
59 ms |
1372 KB |
Output is correct |
10 |
Correct |
49 ms |
1624 KB |
Output is correct |
11 |
Correct |
60 ms |
1372 KB |
Output is correct |
12 |
Correct |
54 ms |
1628 KB |
Output is correct |
13 |
Correct |
46 ms |
1368 KB |
Output is correct |
14 |
Correct |
46 ms |
1372 KB |
Output is correct |
15 |
Correct |
50 ms |
1372 KB |
Output is correct |
16 |
Correct |
58 ms |
1372 KB |
Output is correct |
17 |
Correct |
47 ms |
1628 KB |
Output is correct |
18 |
Correct |
64 ms |
1624 KB |
Output is correct |
19 |
Correct |
60 ms |
1372 KB |
Output is correct |
20 |
Correct |
45 ms |
1624 KB |
Output is correct |
21 |
Correct |
96 ms |
1628 KB |
Output is correct |
22 |
Correct |
119 ms |
4548 KB |
Output is correct |
23 |
Correct |
95 ms |
4436 KB |
Output is correct |
24 |
Correct |
99 ms |
4440 KB |
Output is correct |
25 |
Correct |
116 ms |
4436 KB |
Output is correct |
26 |
Correct |
98 ms |
4444 KB |
Output is correct |
27 |
Correct |
96 ms |
4440 KB |
Output is correct |
28 |
Correct |
102 ms |
4432 KB |
Output is correct |
29 |
Correct |
101 ms |
4444 KB |
Output is correct |
30 |
Correct |
98 ms |
4424 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
456 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
456 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
344 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
344 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
344 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
147 ms |
4444 KB |
Output is correct |
62 |
Correct |
147 ms |
4440 KB |
Output is correct |
63 |
Correct |
134 ms |
4520 KB |
Output is correct |
64 |
Correct |
156 ms |
4560 KB |
Output is correct |
65 |
Correct |
161 ms |
4436 KB |
Output is correct |
66 |
Correct |
160 ms |
4436 KB |
Output is correct |
67 |
Correct |
161 ms |
4400 KB |
Output is correct |
68 |
Correct |
100 ms |
4436 KB |
Output is correct |
69 |
Correct |
97 ms |
4436 KB |
Output is correct |
70 |
Correct |
108 ms |
4440 KB |
Output is correct |