#include <bits/stdc++.h>
#define int long long
#define ll long long
#define II pair<int, int>
#define fs first
#define sc second
#define endl '\n'
#define mp(x, y) make_pair(x, y)
#define sz(x) ((int) (x.size()))
#define forlr(i, l, r) for(int i = l; i <= r; ++i)
#define forrl(i, r, l) for(int i = r; i >= l; --i)
#define show(v) for(auto i : v) cout << i << " "; cout << endl;
#define showlr(v, l, r) for (int i = l; i <= r; i++) cout << v[i] << " "; cout << endl;
#define all(v) v.begin(), v.end()
#define Unique(v) v.erase(unique(all(v)), v.end());
const long long LINF = 1ll << 60;
const int INF = 1 << 30;
const int N = 3e5 + 5;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, k;
int a[N], p[N];
II choose(II A, II B) {
if(A.fs == B.fs) return A.sc < B.sc ? A : B;
return A.fs > B.fs ? A : B;
}
II calc(int cost) {
II take = {0, 0}, best = {0, 0};
forlr(i, 1, n) {
best = choose(best, {take.fs + p[i] - cost, take.sc + 1});
take = choose(take, {best.fs - p[i], best.sc});
}
return best;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n >> k;
forlr(i, 1, n) {
cin >> a[i];
p[i] = p[i - 1] + a[i];
}
int ans = 0;
int lo = 0, hi = 3e14;
while(lo <= hi) {
int mid = (lo + hi) >> 1;
II v = calc(mid);
if(v.sc > k) lo = mid + 1;
else hi = mid - 1, ans = v.fs + k * mid;
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
4952 KB |
Output is correct |
2 |
Correct |
69 ms |
5108 KB |
Output is correct |
3 |
Correct |
68 ms |
4948 KB |
Output is correct |
4 |
Correct |
67 ms |
5004 KB |
Output is correct |
5 |
Correct |
72 ms |
5104 KB |
Output is correct |
6 |
Correct |
66 ms |
4956 KB |
Output is correct |
7 |
Correct |
65 ms |
4956 KB |
Output is correct |
8 |
Correct |
67 ms |
4956 KB |
Output is correct |
9 |
Correct |
66 ms |
4952 KB |
Output is correct |
10 |
Correct |
67 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
4952 KB |
Output is correct |
2 |
Correct |
60 ms |
4952 KB |
Output is correct |
3 |
Correct |
59 ms |
4956 KB |
Output is correct |
4 |
Correct |
60 ms |
4952 KB |
Output is correct |
5 |
Correct |
66 ms |
4956 KB |
Output is correct |
6 |
Correct |
59 ms |
5152 KB |
Output is correct |
7 |
Correct |
60 ms |
5132 KB |
Output is correct |
8 |
Correct |
67 ms |
4956 KB |
Output is correct |
9 |
Correct |
66 ms |
4952 KB |
Output is correct |
10 |
Correct |
59 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
5112 KB |
Output is correct |
2 |
Correct |
72 ms |
5204 KB |
Output is correct |
3 |
Correct |
69 ms |
5104 KB |
Output is correct |
4 |
Correct |
68 ms |
5000 KB |
Output is correct |
5 |
Correct |
69 ms |
4956 KB |
Output is correct |
6 |
Correct |
70 ms |
4948 KB |
Output is correct |
7 |
Correct |
70 ms |
5128 KB |
Output is correct |
8 |
Correct |
69 ms |
5112 KB |
Output is correct |
9 |
Correct |
69 ms |
5140 KB |
Output is correct |
10 |
Correct |
70 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2520 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2520 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2520 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2524 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2556 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
4952 KB |
Output is correct |
2 |
Correct |
69 ms |
5108 KB |
Output is correct |
3 |
Correct |
68 ms |
4948 KB |
Output is correct |
4 |
Correct |
67 ms |
5004 KB |
Output is correct |
5 |
Correct |
72 ms |
5104 KB |
Output is correct |
6 |
Correct |
66 ms |
4956 KB |
Output is correct |
7 |
Correct |
65 ms |
4956 KB |
Output is correct |
8 |
Correct |
67 ms |
4956 KB |
Output is correct |
9 |
Correct |
66 ms |
4952 KB |
Output is correct |
10 |
Correct |
67 ms |
4944 KB |
Output is correct |
11 |
Correct |
58 ms |
4952 KB |
Output is correct |
12 |
Correct |
60 ms |
4952 KB |
Output is correct |
13 |
Correct |
59 ms |
4956 KB |
Output is correct |
14 |
Correct |
60 ms |
4952 KB |
Output is correct |
15 |
Correct |
66 ms |
4956 KB |
Output is correct |
16 |
Correct |
59 ms |
5152 KB |
Output is correct |
17 |
Correct |
60 ms |
5132 KB |
Output is correct |
18 |
Correct |
67 ms |
4956 KB |
Output is correct |
19 |
Correct |
66 ms |
4952 KB |
Output is correct |
20 |
Correct |
59 ms |
4952 KB |
Output is correct |
21 |
Correct |
74 ms |
5112 KB |
Output is correct |
22 |
Correct |
72 ms |
5204 KB |
Output is correct |
23 |
Correct |
69 ms |
5104 KB |
Output is correct |
24 |
Correct |
68 ms |
5000 KB |
Output is correct |
25 |
Correct |
69 ms |
4956 KB |
Output is correct |
26 |
Correct |
70 ms |
4948 KB |
Output is correct |
27 |
Correct |
70 ms |
5128 KB |
Output is correct |
28 |
Correct |
69 ms |
5112 KB |
Output is correct |
29 |
Correct |
69 ms |
5140 KB |
Output is correct |
30 |
Correct |
70 ms |
4952 KB |
Output is correct |
31 |
Correct |
1 ms |
2392 KB |
Output is correct |
32 |
Correct |
1 ms |
2392 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2520 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2516 KB |
Output is correct |
40 |
Correct |
0 ms |
2396 KB |
Output is correct |
41 |
Correct |
1 ms |
2396 KB |
Output is correct |
42 |
Correct |
1 ms |
2396 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2396 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
2396 KB |
Output is correct |
55 |
Correct |
1 ms |
2524 KB |
Output is correct |
56 |
Correct |
1 ms |
2396 KB |
Output is correct |
57 |
Correct |
1 ms |
2556 KB |
Output is correct |
58 |
Correct |
1 ms |
2396 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
1 ms |
2548 KB |
Output is correct |
61 |
Correct |
72 ms |
8064 KB |
Output is correct |
62 |
Correct |
72 ms |
8024 KB |
Output is correct |
63 |
Correct |
72 ms |
8176 KB |
Output is correct |
64 |
Correct |
70 ms |
8156 KB |
Output is correct |
65 |
Correct |
71 ms |
8016 KB |
Output is correct |
66 |
Correct |
71 ms |
7920 KB |
Output is correct |
67 |
Correct |
69 ms |
8020 KB |
Output is correct |
68 |
Correct |
74 ms |
8168 KB |
Output is correct |
69 |
Correct |
71 ms |
8020 KB |
Output is correct |
70 |
Correct |
69 ms |
7800 KB |
Output is correct |