#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define fi first
#define se second
#define mp make_pair
typedef long long lli;
typedef pair<int, int> pii;
const int maxn = 3e5 + 5;
const int lim = 1e9 + 9;
int n, k;
int a[maxn];
lli pref[maxn];
lli sum = 0;
pair<lli, int> dp[maxn];
set<pair<lli, int>> S;
void read_input() {
cin >> n >> k;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
}
int calc(lli lambda) {
S.clear();
S.insert(mp(0, 0));
dp[0] = mp(0, 0);
for(int i = 1; i <= n; ++i) {
dp[i] = max(dp[i - 1], mp(S.rbegin()->fi + pref[i] - lambda, S.rbegin()->se + 1));
dp[i] = max(dp[i], mp(dp[i - 1].fi - lambda, dp[i - 1].se + 1));
S.insert(mp(dp[i].fi - pref[i], dp[i].se));
}
return dp[n].se;
}
void solve() {
pref[0] = 0;
for(int i = 1; i <= n; ++i) {
pref[i] = pref[i - 1] + a[i];
}
lli low = -maxn * 1LL * lim, high = maxn * 1LL * lim;
while(low <= high) {
lli mid = (low + high) / 2;
if(calc(mid) >= k) low = mid + 1;
else high = mid - 1;
}
calc(high);
cout << dp[n].fi + high * dp[n].se << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
read_input();
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
561 ms |
29272 KB |
Output is correct |
2 |
Correct |
567 ms |
29820 KB |
Output is correct |
3 |
Correct |
606 ms |
30232 KB |
Output is correct |
4 |
Correct |
600 ms |
29952 KB |
Output is correct |
5 |
Correct |
568 ms |
29912 KB |
Output is correct |
6 |
Correct |
546 ms |
29328 KB |
Output is correct |
7 |
Correct |
568 ms |
29284 KB |
Output is correct |
8 |
Correct |
640 ms |
29880 KB |
Output is correct |
9 |
Correct |
585 ms |
29348 KB |
Output is correct |
10 |
Correct |
584 ms |
29660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1045 ms |
27660 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1100 ms |
30004 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
324 KB |
Output is correct |
13 |
Correct |
2 ms |
320 KB |
Output is correct |
14 |
Correct |
2 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
2 ms |
336 KB |
Output is correct |
18 |
Correct |
2 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
2 ms |
336 KB |
Output is correct |
12 |
Correct |
2 ms |
324 KB |
Output is correct |
13 |
Correct |
2 ms |
320 KB |
Output is correct |
14 |
Correct |
2 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
2 ms |
336 KB |
Output is correct |
18 |
Correct |
2 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
10 ms |
536 KB |
Output is correct |
22 |
Correct |
11 ms |
524 KB |
Output is correct |
23 |
Correct |
11 ms |
532 KB |
Output is correct |
24 |
Correct |
10 ms |
464 KB |
Output is correct |
25 |
Correct |
12 ms |
536 KB |
Output is correct |
26 |
Correct |
11 ms |
548 KB |
Output is correct |
27 |
Correct |
11 ms |
540 KB |
Output is correct |
28 |
Correct |
11 ms |
540 KB |
Output is correct |
29 |
Correct |
11 ms |
544 KB |
Output is correct |
30 |
Correct |
12 ms |
528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
561 ms |
29272 KB |
Output is correct |
2 |
Correct |
567 ms |
29820 KB |
Output is correct |
3 |
Correct |
606 ms |
30232 KB |
Output is correct |
4 |
Correct |
600 ms |
29952 KB |
Output is correct |
5 |
Correct |
568 ms |
29912 KB |
Output is correct |
6 |
Correct |
546 ms |
29328 KB |
Output is correct |
7 |
Correct |
568 ms |
29284 KB |
Output is correct |
8 |
Correct |
640 ms |
29880 KB |
Output is correct |
9 |
Correct |
585 ms |
29348 KB |
Output is correct |
10 |
Correct |
584 ms |
29660 KB |
Output is correct |
11 |
Execution timed out |
1045 ms |
27660 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |