/*
#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], mx.ss});
res = max(res, dp[i]);
}
// cout << mx.ff << " " << mx.ss << endl;
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 |
47 ms |
10064 KB |
Output is correct |
2 |
Correct |
46 ms |
10064 KB |
Output is correct |
3 |
Correct |
47 ms |
10324 KB |
Output is correct |
4 |
Correct |
46 ms |
10332 KB |
Output is correct |
5 |
Correct |
47 ms |
10084 KB |
Output is correct |
6 |
Correct |
47 ms |
10072 KB |
Output is correct |
7 |
Correct |
46 ms |
10068 KB |
Output is correct |
8 |
Correct |
47 ms |
10068 KB |
Output is correct |
9 |
Correct |
49 ms |
10164 KB |
Output is correct |
10 |
Correct |
48 ms |
10288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
39 ms |
8540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
93 ms |
10592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
10064 KB |
Output is correct |
2 |
Correct |
46 ms |
10064 KB |
Output is correct |
3 |
Correct |
47 ms |
10324 KB |
Output is correct |
4 |
Correct |
46 ms |
10332 KB |
Output is correct |
5 |
Correct |
47 ms |
10084 KB |
Output is correct |
6 |
Correct |
47 ms |
10072 KB |
Output is correct |
7 |
Correct |
46 ms |
10068 KB |
Output is correct |
8 |
Correct |
47 ms |
10068 KB |
Output is correct |
9 |
Correct |
49 ms |
10164 KB |
Output is correct |
10 |
Correct |
48 ms |
10288 KB |
Output is correct |
11 |
Incorrect |
39 ms |
8540 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |