#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int MAXN = 3e5+10, MOD = 1e9+7;
int n, k;
ll a[MAXN], ps[MAXN];
pair<double, ll> dp[MAXN];
pair<double, ll> v(double x) { // cnt, cost
fill(dp, dp+n, make_pair(0., 0ll));
pair<double, ll> prv{0., 0};
for (int i = 0; i < n; i++) {
if (i) dp[i] = dp[i-1];
// {ps[i] - ps[j] + dp[j].first, dp[j].second + 1}
dp[i] = max(dp[i], make_pair(prv.first + ps[i] - x, prv.second + 1));
prv = max(prv, make_pair(-ps[i] + dp[i].first, dp[i].second));
}
return dp[n-1];
}
void solve() {
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) ps[i] = a[i] + (i ? ps[i-1] : 0);
bool bad = 0;
for (int i = 0; i < n; i++) if (a[i] < 0) bad = 1;
if (!bad) {
cout << ps[n-1] << '\n';
return;
}
double lo = 0, hi = 1e18;
for (int rep = 0; rep < 80; rep++) {
double mid = (lo + hi) / 2;
if (v(mid).second >= k) lo = mid;
else hi = mid;
}
double opt = lo;
auto [ans, cnt] = v(opt);
ans += opt * cnt;
cout << llround(ans) << '\n';
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
int T=1;
//cin >> T;
while (T--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
4844 KB |
Output is correct |
2 |
Correct |
40 ms |
7796 KB |
Output is correct |
3 |
Correct |
40 ms |
7776 KB |
Output is correct |
4 |
Correct |
45 ms |
7728 KB |
Output is correct |
5 |
Correct |
40 ms |
7784 KB |
Output is correct |
6 |
Correct |
39 ms |
7576 KB |
Output is correct |
7 |
Correct |
38 ms |
7552 KB |
Output is correct |
8 |
Correct |
42 ms |
7744 KB |
Output is correct |
9 |
Correct |
40 ms |
7576 KB |
Output is correct |
10 |
Correct |
48 ms |
7748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
9364 KB |
Output is correct |
2 |
Correct |
168 ms |
9608 KB |
Output is correct |
3 |
Correct |
164 ms |
9548 KB |
Output is correct |
4 |
Correct |
166 ms |
9536 KB |
Output is correct |
5 |
Correct |
174 ms |
9412 KB |
Output is correct |
6 |
Correct |
171 ms |
9440 KB |
Output is correct |
7 |
Correct |
171 ms |
9772 KB |
Output is correct |
8 |
Correct |
198 ms |
9572 KB |
Output is correct |
9 |
Correct |
183 ms |
9320 KB |
Output is correct |
10 |
Correct |
184 ms |
9628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
9500 KB |
Output is correct |
2 |
Correct |
260 ms |
9472 KB |
Output is correct |
3 |
Correct |
229 ms |
9568 KB |
Output is correct |
4 |
Correct |
230 ms |
9516 KB |
Output is correct |
5 |
Correct |
223 ms |
9560 KB |
Output is correct |
6 |
Correct |
231 ms |
9612 KB |
Output is correct |
7 |
Correct |
230 ms |
9568 KB |
Output is correct |
8 |
Correct |
221 ms |
9564 KB |
Output is correct |
9 |
Correct |
236 ms |
9728 KB |
Output is correct |
10 |
Correct |
239 ms |
9620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 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 |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 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 |
332 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 |
332 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 |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 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 |
332 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 |
332 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 |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
3 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
396 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
4844 KB |
Output is correct |
2 |
Correct |
40 ms |
7796 KB |
Output is correct |
3 |
Correct |
40 ms |
7776 KB |
Output is correct |
4 |
Correct |
45 ms |
7728 KB |
Output is correct |
5 |
Correct |
40 ms |
7784 KB |
Output is correct |
6 |
Correct |
39 ms |
7576 KB |
Output is correct |
7 |
Correct |
38 ms |
7552 KB |
Output is correct |
8 |
Correct |
42 ms |
7744 KB |
Output is correct |
9 |
Correct |
40 ms |
7576 KB |
Output is correct |
10 |
Correct |
48 ms |
7748 KB |
Output is correct |
11 |
Correct |
163 ms |
9364 KB |
Output is correct |
12 |
Correct |
168 ms |
9608 KB |
Output is correct |
13 |
Correct |
164 ms |
9548 KB |
Output is correct |
14 |
Correct |
166 ms |
9536 KB |
Output is correct |
15 |
Correct |
174 ms |
9412 KB |
Output is correct |
16 |
Correct |
171 ms |
9440 KB |
Output is correct |
17 |
Correct |
171 ms |
9772 KB |
Output is correct |
18 |
Correct |
198 ms |
9572 KB |
Output is correct |
19 |
Correct |
183 ms |
9320 KB |
Output is correct |
20 |
Correct |
184 ms |
9628 KB |
Output is correct |
21 |
Correct |
231 ms |
9500 KB |
Output is correct |
22 |
Correct |
260 ms |
9472 KB |
Output is correct |
23 |
Correct |
229 ms |
9568 KB |
Output is correct |
24 |
Correct |
230 ms |
9516 KB |
Output is correct |
25 |
Correct |
223 ms |
9560 KB |
Output is correct |
26 |
Correct |
231 ms |
9612 KB |
Output is correct |
27 |
Correct |
230 ms |
9568 KB |
Output is correct |
28 |
Correct |
221 ms |
9564 KB |
Output is correct |
29 |
Correct |
236 ms |
9728 KB |
Output is correct |
30 |
Correct |
239 ms |
9620 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 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 |
332 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 |
332 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 |
332 KB |
Output is correct |
51 |
Correct |
2 ms |
332 KB |
Output is correct |
52 |
Correct |
3 ms |
332 KB |
Output is correct |
53 |
Correct |
2 ms |
396 KB |
Output is correct |
54 |
Correct |
2 ms |
332 KB |
Output is correct |
55 |
Correct |
2 ms |
332 KB |
Output is correct |
56 |
Correct |
2 ms |
332 KB |
Output is correct |
57 |
Correct |
2 ms |
332 KB |
Output is correct |
58 |
Correct |
2 ms |
332 KB |
Output is correct |
59 |
Correct |
2 ms |
332 KB |
Output is correct |
60 |
Correct |
2 ms |
332 KB |
Output is correct |
61 |
Incorrect |
338 ms |
12428 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |