#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;
using ld = long double;
#define double ld
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 < 100; 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();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
4804 KB |
Output is correct |
2 |
Correct |
39 ms |
5064 KB |
Output is correct |
3 |
Correct |
39 ms |
5068 KB |
Output is correct |
4 |
Correct |
47 ms |
5012 KB |
Output is correct |
5 |
Correct |
38 ms |
5060 KB |
Output is correct |
6 |
Correct |
39 ms |
4932 KB |
Output is correct |
7 |
Correct |
38 ms |
4896 KB |
Output is correct |
8 |
Correct |
45 ms |
5060 KB |
Output is correct |
9 |
Correct |
38 ms |
4976 KB |
Output is correct |
10 |
Correct |
38 ms |
5040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
638 ms |
14048 KB |
Output is correct |
2 |
Correct |
651 ms |
14404 KB |
Output is correct |
3 |
Correct |
626 ms |
14024 KB |
Output is correct |
4 |
Correct |
633 ms |
14276 KB |
Output is correct |
5 |
Correct |
634 ms |
13972 KB |
Output is correct |
6 |
Correct |
622 ms |
14016 KB |
Output is correct |
7 |
Correct |
648 ms |
14276 KB |
Output is correct |
8 |
Correct |
653 ms |
14296 KB |
Output is correct |
9 |
Correct |
620 ms |
13928 KB |
Output is correct |
10 |
Correct |
620 ms |
14284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
719 ms |
14228 KB |
Output is correct |
2 |
Correct |
690 ms |
14044 KB |
Output is correct |
3 |
Correct |
723 ms |
14188 KB |
Output is correct |
4 |
Correct |
695 ms |
14056 KB |
Output is correct |
5 |
Correct |
733 ms |
14180 KB |
Output is correct |
6 |
Correct |
713 ms |
14276 KB |
Output is correct |
7 |
Correct |
717 ms |
14324 KB |
Output is correct |
8 |
Correct |
712 ms |
14188 KB |
Output is correct |
9 |
Correct |
743 ms |
14360 KB |
Output is correct |
10 |
Correct |
737 ms |
14324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 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 |
216 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 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 |
216 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 |
6 ms |
460 KB |
Output is correct |
22 |
Correct |
6 ms |
332 KB |
Output is correct |
23 |
Correct |
6 ms |
332 KB |
Output is correct |
24 |
Correct |
6 ms |
420 KB |
Output is correct |
25 |
Correct |
6 ms |
332 KB |
Output is correct |
26 |
Correct |
6 ms |
332 KB |
Output is correct |
27 |
Correct |
6 ms |
332 KB |
Output is correct |
28 |
Correct |
5 ms |
332 KB |
Output is correct |
29 |
Correct |
5 ms |
332 KB |
Output is correct |
30 |
Correct |
5 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
4804 KB |
Output is correct |
2 |
Correct |
39 ms |
5064 KB |
Output is correct |
3 |
Correct |
39 ms |
5068 KB |
Output is correct |
4 |
Correct |
47 ms |
5012 KB |
Output is correct |
5 |
Correct |
38 ms |
5060 KB |
Output is correct |
6 |
Correct |
39 ms |
4932 KB |
Output is correct |
7 |
Correct |
38 ms |
4896 KB |
Output is correct |
8 |
Correct |
45 ms |
5060 KB |
Output is correct |
9 |
Correct |
38 ms |
4976 KB |
Output is correct |
10 |
Correct |
38 ms |
5040 KB |
Output is correct |
11 |
Correct |
638 ms |
14048 KB |
Output is correct |
12 |
Correct |
651 ms |
14404 KB |
Output is correct |
13 |
Correct |
626 ms |
14024 KB |
Output is correct |
14 |
Correct |
633 ms |
14276 KB |
Output is correct |
15 |
Correct |
634 ms |
13972 KB |
Output is correct |
16 |
Correct |
622 ms |
14016 KB |
Output is correct |
17 |
Correct |
648 ms |
14276 KB |
Output is correct |
18 |
Correct |
653 ms |
14296 KB |
Output is correct |
19 |
Correct |
620 ms |
13928 KB |
Output is correct |
20 |
Correct |
620 ms |
14284 KB |
Output is correct |
21 |
Correct |
719 ms |
14228 KB |
Output is correct |
22 |
Correct |
690 ms |
14044 KB |
Output is correct |
23 |
Correct |
723 ms |
14188 KB |
Output is correct |
24 |
Correct |
695 ms |
14056 KB |
Output is correct |
25 |
Correct |
733 ms |
14180 KB |
Output is correct |
26 |
Correct |
713 ms |
14276 KB |
Output is correct |
27 |
Correct |
717 ms |
14324 KB |
Output is correct |
28 |
Correct |
712 ms |
14188 KB |
Output is correct |
29 |
Correct |
743 ms |
14360 KB |
Output is correct |
30 |
Correct |
737 ms |
14324 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
204 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 |
216 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 |
6 ms |
460 KB |
Output is correct |
52 |
Correct |
6 ms |
332 KB |
Output is correct |
53 |
Correct |
6 ms |
332 KB |
Output is correct |
54 |
Correct |
6 ms |
420 KB |
Output is correct |
55 |
Correct |
6 ms |
332 KB |
Output is correct |
56 |
Correct |
6 ms |
332 KB |
Output is correct |
57 |
Correct |
6 ms |
332 KB |
Output is correct |
58 |
Correct |
5 ms |
332 KB |
Output is correct |
59 |
Correct |
5 ms |
332 KB |
Output is correct |
60 |
Correct |
5 ms |
332 KB |
Output is correct |
61 |
Correct |
819 ms |
14164 KB |
Output is correct |
62 |
Correct |
820 ms |
17440 KB |
Output is correct |
63 |
Correct |
785 ms |
17092 KB |
Output is correct |
64 |
Correct |
848 ms |
17316 KB |
Output is correct |
65 |
Correct |
844 ms |
17332 KB |
Output is correct |
66 |
Correct |
899 ms |
17352 KB |
Output is correct |
67 |
Correct |
825 ms |
17092 KB |
Output is correct |
68 |
Correct |
708 ms |
17328 KB |
Output is correct |
69 |
Correct |
717 ms |
16964 KB |
Output is correct |
70 |
Correct |
699 ms |
16964 KB |
Output is correct |