/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("arch=skylake")
*/
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pii>
#define isz(x) (int)x.size()
using namespace std;
const int mxN = 3e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n, k, a[mxN];
ll pref[mxN], ans;
pair<ll, int> dp[mxN];
ll calc(int l, int r) {
if(l > r) return 0;
return pref[r] - pref[l - 1];
}
void solve() {
cin >> n >> k;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
ll l = -oo / n, r = 0;
while(l < r) {
ll mid = (l + r + 1) >> 1;
pair<ll, int> maxx = {0, 0}, doesntexist = {0, 0};
for(int i = 1; i <= n; ++i) {
ll tmp = maxx.ff + pref[i] + mid;
if(dp[i - 1].ff == tmp) dp[i] = min(dp[i - 1], pair{tmp, maxx.ss + 1});
else dp[i] = max(dp[i - 1], pair{tmp, maxx.ss + 1});
if(maxx.ff == dp[i].ff - pref[i]) maxx = min(maxx, pair{dp[i].ff - pref[i], dp[i].ss});
else maxx = max(maxx, pair{dp[i].ff - pref[i], dp[i].ss});
if(dp[i].ff == doesntexist.ff) doesntexist = min(doesntexist, dp[i]);
else doesntexist = max(doesntexist, dp[i]);
}
if(doesntexist.ss <= k) l = mid;
else r = mid - 1;
}
pair<ll, int> maxx = {0, 0}, doesntexist = {0, 0};
for(int i = 1; i <= n; ++i) {
ll tmp = maxx.ff + pref[i] + l;
if(dp[i - 1].ff == tmp) dp[i] = min(dp[i - 1], pair{tmp, maxx.ss + 1});
else dp[i] = max(dp[i - 1], pair{tmp, maxx.ss + 1});
if(maxx.ff == dp[i].ff - pref[i]) maxx = min(maxx, pair{dp[i].ff - pref[i], dp[i].ss});
else maxx = max(maxx, pair{dp[i].ff - pref[i], dp[i].ss});
if(dp[i].ff == doesntexist.ff) doesntexist = min(doesntexist, dp[i]);
else doesntexist = max(doesntexist, dp[i]);
}
cout << doesntexist.ff - doesntexist.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;
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
8196 KB |
Output is correct |
2 |
Correct |
72 ms |
8432 KB |
Output is correct |
3 |
Correct |
73 ms |
8576 KB |
Output is correct |
4 |
Correct |
71 ms |
8456 KB |
Output is correct |
5 |
Correct |
73 ms |
8416 KB |
Output is correct |
6 |
Correct |
72 ms |
8280 KB |
Output is correct |
7 |
Correct |
69 ms |
8280 KB |
Output is correct |
8 |
Correct |
72 ms |
8436 KB |
Output is correct |
9 |
Correct |
70 ms |
8272 KB |
Output is correct |
10 |
Correct |
72 ms |
8372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
8328 KB |
Output is correct |
2 |
Correct |
65 ms |
8508 KB |
Output is correct |
3 |
Correct |
62 ms |
8316 KB |
Output is correct |
4 |
Correct |
63 ms |
8404 KB |
Output is correct |
5 |
Correct |
70 ms |
8288 KB |
Output is correct |
6 |
Correct |
64 ms |
9424 KB |
Output is correct |
7 |
Correct |
64 ms |
9660 KB |
Output is correct |
8 |
Correct |
73 ms |
11336 KB |
Output is correct |
9 |
Correct |
71 ms |
11044 KB |
Output is correct |
10 |
Correct |
64 ms |
9600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
8432 KB |
Output is correct |
2 |
Correct |
78 ms |
8292 KB |
Output is correct |
3 |
Correct |
69 ms |
8404 KB |
Output is correct |
4 |
Correct |
70 ms |
8316 KB |
Output is correct |
5 |
Correct |
69 ms |
8372 KB |
Output is correct |
6 |
Correct |
70 ms |
8460 KB |
Output is correct |
7 |
Correct |
75 ms |
8484 KB |
Output is correct |
8 |
Correct |
73 ms |
8408 KB |
Output is correct |
9 |
Correct |
70 ms |
8504 KB |
Output is correct |
10 |
Correct |
71 ms |
8484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
260 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
260 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 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 |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
260 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 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 |
324 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 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 |
420 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 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 |
69 ms |
8196 KB |
Output is correct |
2 |
Correct |
72 ms |
8432 KB |
Output is correct |
3 |
Correct |
73 ms |
8576 KB |
Output is correct |
4 |
Correct |
71 ms |
8456 KB |
Output is correct |
5 |
Correct |
73 ms |
8416 KB |
Output is correct |
6 |
Correct |
72 ms |
8280 KB |
Output is correct |
7 |
Correct |
69 ms |
8280 KB |
Output is correct |
8 |
Correct |
72 ms |
8436 KB |
Output is correct |
9 |
Correct |
70 ms |
8272 KB |
Output is correct |
10 |
Correct |
72 ms |
8372 KB |
Output is correct |
11 |
Correct |
59 ms |
8328 KB |
Output is correct |
12 |
Correct |
65 ms |
8508 KB |
Output is correct |
13 |
Correct |
62 ms |
8316 KB |
Output is correct |
14 |
Correct |
63 ms |
8404 KB |
Output is correct |
15 |
Correct |
70 ms |
8288 KB |
Output is correct |
16 |
Correct |
64 ms |
9424 KB |
Output is correct |
17 |
Correct |
64 ms |
9660 KB |
Output is correct |
18 |
Correct |
73 ms |
11336 KB |
Output is correct |
19 |
Correct |
71 ms |
11044 KB |
Output is correct |
20 |
Correct |
64 ms |
9600 KB |
Output is correct |
21 |
Correct |
70 ms |
8432 KB |
Output is correct |
22 |
Correct |
78 ms |
8292 KB |
Output is correct |
23 |
Correct |
69 ms |
8404 KB |
Output is correct |
24 |
Correct |
70 ms |
8316 KB |
Output is correct |
25 |
Correct |
69 ms |
8372 KB |
Output is correct |
26 |
Correct |
70 ms |
8460 KB |
Output is correct |
27 |
Correct |
75 ms |
8484 KB |
Output is correct |
28 |
Correct |
73 ms |
8408 KB |
Output is correct |
29 |
Correct |
70 ms |
8504 KB |
Output is correct |
30 |
Correct |
71 ms |
8484 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
260 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
0 ms |
212 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 |
324 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
0 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 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 |
420 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
104 ms |
11360 KB |
Output is correct |
62 |
Correct |
103 ms |
11524 KB |
Output is correct |
63 |
Correct |
76 ms |
11232 KB |
Output is correct |
64 |
Correct |
96 ms |
11492 KB |
Output is correct |
65 |
Correct |
92 ms |
11456 KB |
Output is correct |
66 |
Correct |
91 ms |
11452 KB |
Output is correct |
67 |
Correct |
88 ms |
11200 KB |
Output is correct |
68 |
Correct |
71 ms |
11492 KB |
Output is correct |
69 |
Correct |
74 ms |
11280 KB |
Output is correct |
70 |
Correct |
69 ms |
11228 KB |
Output is correct |