#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];
void read_input() {
cin >> n >> k;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
}
int calc(lli lambda) {
pair<lli, int> best = mp(0, 0);
dp[0] = mp(0, 0);
for(int i = 1; i <= n; ++i) {
dp[i] = max(dp[i - 1], mp(best.fi + pref[i] - lambda, best.se + 1));
dp[i] = max(dp[i], mp(dp[i - 1].fi - lambda, dp[i - 1].se + 1));
best = max(best, 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 |
75 ms |
8596 KB |
Output is correct |
2 |
Correct |
76 ms |
8688 KB |
Output is correct |
3 |
Correct |
80 ms |
8904 KB |
Output is correct |
4 |
Correct |
78 ms |
8772 KB |
Output is correct |
5 |
Correct |
76 ms |
8796 KB |
Output is correct |
6 |
Correct |
83 ms |
8540 KB |
Output is correct |
7 |
Correct |
74 ms |
8652 KB |
Output is correct |
8 |
Correct |
85 ms |
8816 KB |
Output is correct |
9 |
Correct |
80 ms |
8800 KB |
Output is correct |
10 |
Correct |
75 ms |
8748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
8704 KB |
Output is correct |
2 |
Correct |
92 ms |
9616 KB |
Output is correct |
3 |
Correct |
97 ms |
9432 KB |
Output is correct |
4 |
Correct |
79 ms |
9416 KB |
Output is correct |
5 |
Correct |
103 ms |
11080 KB |
Output is correct |
6 |
Correct |
92 ms |
9400 KB |
Output is correct |
7 |
Correct |
93 ms |
9660 KB |
Output is correct |
8 |
Correct |
102 ms |
11352 KB |
Output is correct |
9 |
Correct |
100 ms |
10948 KB |
Output is correct |
10 |
Correct |
78 ms |
9520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
8812 KB |
Output is correct |
2 |
Correct |
119 ms |
11196 KB |
Output is correct |
3 |
Correct |
118 ms |
11352 KB |
Output is correct |
4 |
Correct |
119 ms |
11300 KB |
Output is correct |
5 |
Correct |
124 ms |
11400 KB |
Output is correct |
6 |
Correct |
118 ms |
11376 KB |
Output is correct |
7 |
Correct |
122 ms |
11588 KB |
Output is correct |
8 |
Correct |
120 ms |
11424 KB |
Output is correct |
9 |
Correct |
123 ms |
11560 KB |
Output is correct |
10 |
Correct |
124 ms |
11588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
8596 KB |
Output is correct |
2 |
Correct |
76 ms |
8688 KB |
Output is correct |
3 |
Correct |
80 ms |
8904 KB |
Output is correct |
4 |
Correct |
78 ms |
8772 KB |
Output is correct |
5 |
Correct |
76 ms |
8796 KB |
Output is correct |
6 |
Correct |
83 ms |
8540 KB |
Output is correct |
7 |
Correct |
74 ms |
8652 KB |
Output is correct |
8 |
Correct |
85 ms |
8816 KB |
Output is correct |
9 |
Correct |
80 ms |
8800 KB |
Output is correct |
10 |
Correct |
75 ms |
8748 KB |
Output is correct |
11 |
Correct |
98 ms |
8704 KB |
Output is correct |
12 |
Correct |
92 ms |
9616 KB |
Output is correct |
13 |
Correct |
97 ms |
9432 KB |
Output is correct |
14 |
Correct |
79 ms |
9416 KB |
Output is correct |
15 |
Correct |
103 ms |
11080 KB |
Output is correct |
16 |
Correct |
92 ms |
9400 KB |
Output is correct |
17 |
Correct |
93 ms |
9660 KB |
Output is correct |
18 |
Correct |
102 ms |
11352 KB |
Output is correct |
19 |
Correct |
100 ms |
10948 KB |
Output is correct |
20 |
Correct |
78 ms |
9520 KB |
Output is correct |
21 |
Correct |
123 ms |
8812 KB |
Output is correct |
22 |
Correct |
119 ms |
11196 KB |
Output is correct |
23 |
Correct |
118 ms |
11352 KB |
Output is correct |
24 |
Correct |
119 ms |
11300 KB |
Output is correct |
25 |
Correct |
124 ms |
11400 KB |
Output is correct |
26 |
Correct |
118 ms |
11376 KB |
Output is correct |
27 |
Correct |
122 ms |
11588 KB |
Output is correct |
28 |
Correct |
120 ms |
11424 KB |
Output is correct |
29 |
Correct |
123 ms |
11560 KB |
Output is correct |
30 |
Correct |
124 ms |
11588 KB |
Output is correct |
31 |
Correct |
1 ms |
324 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
324 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
320 KB |
Output is correct |
51 |
Correct |
2 ms |
332 KB |
Output is correct |
52 |
Correct |
2 ms |
332 KB |
Output is correct |
53 |
Correct |
2 ms |
332 KB |
Output is correct |
54 |
Correct |
2 ms |
332 KB |
Output is correct |
55 |
Correct |
1 ms |
332 KB |
Output is correct |
56 |
Correct |
1 ms |
332 KB |
Output is correct |
57 |
Correct |
2 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
154 ms |
11328 KB |
Output is correct |
62 |
Correct |
146 ms |
11552 KB |
Output is correct |
63 |
Correct |
129 ms |
11208 KB |
Output is correct |
64 |
Correct |
151 ms |
11460 KB |
Output is correct |
65 |
Correct |
155 ms |
11464 KB |
Output is correct |
66 |
Correct |
150 ms |
11336 KB |
Output is correct |
67 |
Correct |
149 ms |
11176 KB |
Output is correct |
68 |
Correct |
127 ms |
11540 KB |
Output is correct |
69 |
Correct |
119 ms |
11284 KB |
Output is correct |
70 |
Correct |
118 ms |
11336 KB |
Output is correct |