#include <iostream>
#define ll long long
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define fi first
#define se second
#define inf 1e18
using namespace std;
typedef pair<ll, int> li;
const int N = 3e5 + 9;
int n, k;
ll pre[N], a[N], ans;
li dp[N];
void maximize(li &a, li b){
if (a.fi < b.fi) a = b;
if (a.fi == b.fi) a = max(a, b);
}
li calc(ll cost){
//FOR(i, 1, n) dp[i] = {pre[i] - cost, 1};
li best = {0, 0};
for (int i = 1; i <= n; ++i) {
dp[i] = dp[i-1];
maximize(dp[i], {pre[i] + best.fi - cost, best.se + 1});
maximize(best, {dp[i].fi - pre[i], dp[i].se});
}
return dp[n];
}
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];
ll lo = -1e15, hi = 1e15;
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;
return 0;
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:43:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | ll mid = lo + hi >> 1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
12108 KB |
Output is correct |
2 |
Correct |
63 ms |
12396 KB |
Output is correct |
3 |
Correct |
65 ms |
12572 KB |
Output is correct |
4 |
Correct |
62 ms |
12408 KB |
Output is correct |
5 |
Correct |
62 ms |
12424 KB |
Output is correct |
6 |
Correct |
63 ms |
12216 KB |
Output is correct |
7 |
Correct |
62 ms |
12216 KB |
Output is correct |
8 |
Correct |
63 ms |
12424 KB |
Output is correct |
9 |
Correct |
64 ms |
12168 KB |
Output is correct |
10 |
Correct |
80 ms |
12364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
10576 KB |
Output is correct |
2 |
Correct |
54 ms |
10824 KB |
Output is correct |
3 |
Correct |
57 ms |
10580 KB |
Output is correct |
4 |
Correct |
53 ms |
10656 KB |
Output is correct |
5 |
Correct |
63 ms |
12172 KB |
Output is correct |
6 |
Correct |
52 ms |
10568 KB |
Output is correct |
7 |
Correct |
52 ms |
10832 KB |
Output is correct |
8 |
Correct |
64 ms |
12512 KB |
Output is correct |
9 |
Correct |
63 ms |
12108 KB |
Output is correct |
10 |
Correct |
52 ms |
10760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
12616 KB |
Output is correct |
2 |
Correct |
81 ms |
12464 KB |
Output is correct |
3 |
Correct |
77 ms |
12580 KB |
Output is correct |
4 |
Correct |
76 ms |
12460 KB |
Output is correct |
5 |
Correct |
76 ms |
12488 KB |
Output is correct |
6 |
Correct |
76 ms |
12656 KB |
Output is correct |
7 |
Correct |
77 ms |
12612 KB |
Output is correct |
8 |
Correct |
75 ms |
12580 KB |
Output is correct |
9 |
Correct |
86 ms |
12732 KB |
Output is correct |
10 |
Correct |
92 ms |
12680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
376 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
372 KB |
Output is correct |
28 |
Correct |
1 ms |
372 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
12108 KB |
Output is correct |
2 |
Correct |
63 ms |
12396 KB |
Output is correct |
3 |
Correct |
65 ms |
12572 KB |
Output is correct |
4 |
Correct |
62 ms |
12408 KB |
Output is correct |
5 |
Correct |
62 ms |
12424 KB |
Output is correct |
6 |
Correct |
63 ms |
12216 KB |
Output is correct |
7 |
Correct |
62 ms |
12216 KB |
Output is correct |
8 |
Correct |
63 ms |
12424 KB |
Output is correct |
9 |
Correct |
64 ms |
12168 KB |
Output is correct |
10 |
Correct |
80 ms |
12364 KB |
Output is correct |
11 |
Correct |
52 ms |
10576 KB |
Output is correct |
12 |
Correct |
54 ms |
10824 KB |
Output is correct |
13 |
Correct |
57 ms |
10580 KB |
Output is correct |
14 |
Correct |
53 ms |
10656 KB |
Output is correct |
15 |
Correct |
63 ms |
12172 KB |
Output is correct |
16 |
Correct |
52 ms |
10568 KB |
Output is correct |
17 |
Correct |
52 ms |
10832 KB |
Output is correct |
18 |
Correct |
64 ms |
12512 KB |
Output is correct |
19 |
Correct |
63 ms |
12108 KB |
Output is correct |
20 |
Correct |
52 ms |
10760 KB |
Output is correct |
21 |
Correct |
77 ms |
12616 KB |
Output is correct |
22 |
Correct |
81 ms |
12464 KB |
Output is correct |
23 |
Correct |
77 ms |
12580 KB |
Output is correct |
24 |
Correct |
76 ms |
12460 KB |
Output is correct |
25 |
Correct |
76 ms |
12488 KB |
Output is correct |
26 |
Correct |
76 ms |
12656 KB |
Output is correct |
27 |
Correct |
77 ms |
12612 KB |
Output is correct |
28 |
Correct |
75 ms |
12580 KB |
Output is correct |
29 |
Correct |
86 ms |
12732 KB |
Output is correct |
30 |
Correct |
92 ms |
12680 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
0 ms |
324 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
328 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
328 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
0 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
376 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
372 KB |
Output is correct |
58 |
Correct |
1 ms |
372 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
125 ms |
12396 KB |
Output is correct |
62 |
Correct |
103 ms |
12752 KB |
Output is correct |
63 |
Correct |
89 ms |
12412 KB |
Output is correct |
64 |
Correct |
105 ms |
12664 KB |
Output is correct |
65 |
Correct |
108 ms |
12644 KB |
Output is correct |
66 |
Correct |
105 ms |
12616 KB |
Output is correct |
67 |
Correct |
105 ms |
12432 KB |
Output is correct |
68 |
Correct |
77 ms |
12616 KB |
Output is correct |
69 |
Correct |
77 ms |
12364 KB |
Output is correct |
70 |
Correct |
75 ms |
12364 KB |
Output is correct |