#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <cstdlib>
#define ll long long
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define inf -1e18
#define fi first
#define se second
using namespace std;
typedef pair <ll, int> li;
const int N = 3e5 + 9, M = 2e3 + 9;
int n, k;
ll a[N], pre[N], dp[M][M];
void sub1(){
dp[0][1] = pre[1];
for (int t = 1; t <= k; ++t) {
ll mx = inf;
for (int i = 1; i <= n; ++i) {
mx = max(mx, dp[t-1][i] - pre[i]);
dp[t][i] = max(dp[t][i-1], pre[i] + mx);
}
}
cout << dp[k][n];
}
pair<ll, int> f[N];
void maximize(li &a, li b){
if (a.fi < b.fi) a = b;
if (a.fi == b.fi && a.se > b.se) a = b;
}
li calc(ll cost){
li best = {0, 0};
for (int i = 1; i <= n; ++i) {
f[i] = f[i-1];
maximize(f[i], {pre[i] + best.fi - cost , best.se + 1});
maximize(best, {f[i].fi - pre[i], f[i].se});
}
return f[n];
}
void sub2(){
ll lo = -1e15, hi = 1e15, ans = 0;
while (lo <= hi) {
ll mid = lo + hi >> 1;
li tmp = calc(mid);
if (tmp.se <= k) {
ans = max(ans, tmp.fi + tmp.se*mid);
hi = mid - 1;
} else lo = mid + 1;
}
cout << ans;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("nhap.inp", "r", stdin);
// freopen("xuat.out", "w", stdout);
cin >> n >> k;
FOR(i, 1, n) {
cin >> a[i];
pre[i] = pre[i-1] + a[i];
}
if (max(k, n) <= 2000) sub1(); else
sub2();
return 0;
}
Compilation message
feast.cpp: In function 'void sub2()':
feast.cpp:53:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | ll mid = lo + hi >> 1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
14160 KB |
Output is correct |
2 |
Correct |
59 ms |
14320 KB |
Output is correct |
3 |
Correct |
57 ms |
14160 KB |
Output is correct |
4 |
Correct |
57 ms |
14160 KB |
Output is correct |
5 |
Correct |
56 ms |
14160 KB |
Output is correct |
6 |
Correct |
54 ms |
14164 KB |
Output is correct |
7 |
Correct |
55 ms |
14160 KB |
Output is correct |
8 |
Correct |
46 ms |
14304 KB |
Output is correct |
9 |
Correct |
46 ms |
14160 KB |
Output is correct |
10 |
Correct |
47 ms |
14160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
12376 KB |
Output is correct |
2 |
Correct |
49 ms |
12368 KB |
Output is correct |
3 |
Correct |
48 ms |
12544 KB |
Output is correct |
4 |
Correct |
48 ms |
12376 KB |
Output is correct |
5 |
Correct |
51 ms |
14160 KB |
Output is correct |
6 |
Correct |
43 ms |
12376 KB |
Output is correct |
7 |
Correct |
48 ms |
12632 KB |
Output is correct |
8 |
Correct |
47 ms |
14160 KB |
Output is correct |
9 |
Correct |
47 ms |
14160 KB |
Output is correct |
10 |
Correct |
48 ms |
12444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
14412 KB |
Output is correct |
2 |
Correct |
72 ms |
14416 KB |
Output is correct |
3 |
Correct |
73 ms |
14416 KB |
Output is correct |
4 |
Correct |
75 ms |
14172 KB |
Output is correct |
5 |
Correct |
73 ms |
14416 KB |
Output is correct |
6 |
Correct |
72 ms |
14484 KB |
Output is correct |
7 |
Correct |
74 ms |
14284 KB |
Output is correct |
8 |
Correct |
73 ms |
14420 KB |
Output is correct |
9 |
Correct |
75 ms |
14516 KB |
Output is correct |
10 |
Correct |
75 ms |
14416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 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 |
4440 KB |
Output is correct |
16 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 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 |
4440 KB |
Output is correct |
16 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
14160 KB |
Output is correct |
2 |
Correct |
59 ms |
14320 KB |
Output is correct |
3 |
Correct |
57 ms |
14160 KB |
Output is correct |
4 |
Correct |
57 ms |
14160 KB |
Output is correct |
5 |
Correct |
56 ms |
14160 KB |
Output is correct |
6 |
Correct |
54 ms |
14164 KB |
Output is correct |
7 |
Correct |
55 ms |
14160 KB |
Output is correct |
8 |
Correct |
46 ms |
14304 KB |
Output is correct |
9 |
Correct |
46 ms |
14160 KB |
Output is correct |
10 |
Correct |
47 ms |
14160 KB |
Output is correct |
11 |
Correct |
47 ms |
12376 KB |
Output is correct |
12 |
Correct |
49 ms |
12368 KB |
Output is correct |
13 |
Correct |
48 ms |
12544 KB |
Output is correct |
14 |
Correct |
48 ms |
12376 KB |
Output is correct |
15 |
Correct |
51 ms |
14160 KB |
Output is correct |
16 |
Correct |
43 ms |
12376 KB |
Output is correct |
17 |
Correct |
48 ms |
12632 KB |
Output is correct |
18 |
Correct |
47 ms |
14160 KB |
Output is correct |
19 |
Correct |
47 ms |
14160 KB |
Output is correct |
20 |
Correct |
48 ms |
12444 KB |
Output is correct |
21 |
Correct |
73 ms |
14412 KB |
Output is correct |
22 |
Correct |
72 ms |
14416 KB |
Output is correct |
23 |
Correct |
73 ms |
14416 KB |
Output is correct |
24 |
Correct |
75 ms |
14172 KB |
Output is correct |
25 |
Correct |
73 ms |
14416 KB |
Output is correct |
26 |
Correct |
72 ms |
14484 KB |
Output is correct |
27 |
Correct |
74 ms |
14284 KB |
Output is correct |
28 |
Correct |
73 ms |
14420 KB |
Output is correct |
29 |
Correct |
75 ms |
14516 KB |
Output is correct |
30 |
Correct |
75 ms |
14416 KB |
Output is correct |
31 |
Correct |
1 ms |
4440 KB |
Output is correct |
32 |
Correct |
1 ms |
4440 KB |
Output is correct |
33 |
Correct |
1 ms |
4440 KB |
Output is correct |
34 |
Correct |
1 ms |
4440 KB |
Output is correct |
35 |
Correct |
1 ms |
4440 KB |
Output is correct |
36 |
Correct |
1 ms |
4440 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4440 KB |
Output is correct |
39 |
Correct |
1 ms |
4440 KB |
Output is correct |
40 |
Correct |
1 ms |
4440 KB |
Output is correct |
41 |
Correct |
1 ms |
4440 KB |
Output is correct |
42 |
Correct |
1 ms |
4440 KB |
Output is correct |
43 |
Correct |
1 ms |
4444 KB |
Output is correct |
44 |
Correct |
1 ms |
4444 KB |
Output is correct |
45 |
Correct |
1 ms |
4440 KB |
Output is correct |
46 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
47 |
Halted |
0 ms |
0 KB |
- |