/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define int long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;
const int mxN = 3e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;
int n, k;
i64 a[mxN];
pair<int, int> dp[mxN];
pair<int, int> calc(i64 delta) {
pair<int, int> mx = {0, 0}, res = {0, 0};
for (int i = 1; i <= n; ++i) {
int tmp = mx.ff + a[i] + delta;
dp[i] = max(dp[i - 1], pair{tmp, mx.ss - 1});
mx = max(mx, pair{dp[i].ff - a[i], dp[i].ss});
res = max(res, dp[i]);
}
return res;
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
a[i] += a[i - 1];
}
i64 l = -1e12, r = 0;
while (l < r) {
int mid = (l + r + 1) >> 1;
if (-calc(mid).ss <= k) l = mid;
else r = mid - 1;
}
auto res = calc(l);
cout << res.ff + res.ss * l << endl;
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
cout << "Check array size pls sir" << endl;
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
7260 KB |
Output is correct |
2 |
Correct |
52 ms |
7512 KB |
Output is correct |
3 |
Correct |
46 ms |
7272 KB |
Output is correct |
4 |
Correct |
46 ms |
7260 KB |
Output is correct |
5 |
Correct |
48 ms |
7248 KB |
Output is correct |
6 |
Correct |
44 ms |
7380 KB |
Output is correct |
7 |
Correct |
48 ms |
7380 KB |
Output is correct |
8 |
Correct |
48 ms |
7260 KB |
Output is correct |
9 |
Correct |
52 ms |
7256 KB |
Output is correct |
10 |
Correct |
46 ms |
7488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
7256 KB |
Output is correct |
2 |
Correct |
40 ms |
8540 KB |
Output is correct |
3 |
Correct |
39 ms |
8540 KB |
Output is correct |
4 |
Correct |
39 ms |
8572 KB |
Output is correct |
5 |
Correct |
44 ms |
10064 KB |
Output is correct |
6 |
Correct |
38 ms |
8536 KB |
Output is correct |
7 |
Correct |
40 ms |
8540 KB |
Output is correct |
8 |
Correct |
46 ms |
10324 KB |
Output is correct |
9 |
Correct |
44 ms |
10076 KB |
Output is correct |
10 |
Correct |
39 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
7456 KB |
Output is correct |
2 |
Correct |
64 ms |
10376 KB |
Output is correct |
3 |
Correct |
63 ms |
10468 KB |
Output is correct |
4 |
Correct |
62 ms |
10324 KB |
Output is correct |
5 |
Correct |
65 ms |
10320 KB |
Output is correct |
6 |
Correct |
71 ms |
10504 KB |
Output is correct |
7 |
Correct |
64 ms |
10324 KB |
Output is correct |
8 |
Correct |
62 ms |
10320 KB |
Output is correct |
9 |
Correct |
62 ms |
10544 KB |
Output is correct |
10 |
Correct |
70 ms |
10332 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 |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 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 |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 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 |
0 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 |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 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 |
0 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 |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2532 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2392 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
7260 KB |
Output is correct |
2 |
Correct |
52 ms |
7512 KB |
Output is correct |
3 |
Correct |
46 ms |
7272 KB |
Output is correct |
4 |
Correct |
46 ms |
7260 KB |
Output is correct |
5 |
Correct |
48 ms |
7248 KB |
Output is correct |
6 |
Correct |
44 ms |
7380 KB |
Output is correct |
7 |
Correct |
48 ms |
7380 KB |
Output is correct |
8 |
Correct |
48 ms |
7260 KB |
Output is correct |
9 |
Correct |
52 ms |
7256 KB |
Output is correct |
10 |
Correct |
46 ms |
7488 KB |
Output is correct |
11 |
Correct |
42 ms |
7256 KB |
Output is correct |
12 |
Correct |
40 ms |
8540 KB |
Output is correct |
13 |
Correct |
39 ms |
8540 KB |
Output is correct |
14 |
Correct |
39 ms |
8572 KB |
Output is correct |
15 |
Correct |
44 ms |
10064 KB |
Output is correct |
16 |
Correct |
38 ms |
8536 KB |
Output is correct |
17 |
Correct |
40 ms |
8540 KB |
Output is correct |
18 |
Correct |
46 ms |
10324 KB |
Output is correct |
19 |
Correct |
44 ms |
10076 KB |
Output is correct |
20 |
Correct |
39 ms |
8540 KB |
Output is correct |
21 |
Correct |
67 ms |
7456 KB |
Output is correct |
22 |
Correct |
64 ms |
10376 KB |
Output is correct |
23 |
Correct |
63 ms |
10468 KB |
Output is correct |
24 |
Correct |
62 ms |
10324 KB |
Output is correct |
25 |
Correct |
65 ms |
10320 KB |
Output is correct |
26 |
Correct |
71 ms |
10504 KB |
Output is correct |
27 |
Correct |
64 ms |
10324 KB |
Output is correct |
28 |
Correct |
62 ms |
10320 KB |
Output is correct |
29 |
Correct |
62 ms |
10544 KB |
Output is correct |
30 |
Correct |
70 ms |
10332 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 |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2392 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 |
0 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 |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
2532 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
1 ms |
2396 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
1 ms |
2392 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
1 ms |
2396 KB |
Output is correct |
61 |
Correct |
94 ms |
10332 KB |
Output is correct |
62 |
Correct |
94 ms |
10324 KB |
Output is correct |
63 |
Correct |
75 ms |
10320 KB |
Output is correct |
64 |
Correct |
94 ms |
10320 KB |
Output is correct |
65 |
Correct |
96 ms |
10320 KB |
Output is correct |
66 |
Correct |
97 ms |
10324 KB |
Output is correct |
67 |
Correct |
103 ms |
10568 KB |
Output is correct |
68 |
Correct |
64 ms |
10504 KB |
Output is correct |
69 |
Correct |
62 ms |
10324 KB |
Output is correct |
70 |
Correct |
65 ms |
10320 KB |
Output is correct |