#include <bits/stdc++.h>
using namespace std;
int n, k, cnt[300001];
long long s[300001], dp[300001];
int calc(long long lambda) {
// dp[i]: tổng lớn nhất của các đoạn với i phần tử đầu tiên, nếu mỗi đoạn khác rỗng được trừ đi lambda
// cnt[i]: số đoạn tối thiểu
dp[0] = cnt[0] = 0;
long long dp_max = 0;
int cnt_max = 0;
for (int i = 1; i <= n; i++) {
// không dùng thêm đoạn
dp[i] = dp[i - 1];
cnt[i] = cnt[i - 1];
// sử dụng thêm đoạn
if ((dp_max + s[i] - lambda > dp[i]) || (dp_max + s[i] - lambda == dp[i] && cnt[i] > cnt_max + 1)) {
dp[i] = dp_max + s[i] - lambda;
cnt[i] = cnt_max + 1;
}
if ((dp[i] - s[i] > dp_max) || (dp[i] - s[i] == dp_max && cnt_max > cnt[i])) {
dp_max = dp[i] - s[i];
cnt_max = cnt[i];
}
}
return cnt[n];
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("test11.in", "r", stdin);
//freopen("test01.out", "w", stdout);
cin >> n >> k;
s[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> s[i];
s[i] += s[i - 1];
}
long long l = 0, r = 3e14;
while (l < r) {
long long c = (l + r) / 2;
if (calc(c) > k)
l = c + 1;
else
r = c;
}
calc(l);
cout << dp[n] + l * cnt[n] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
8772 KB |
Output is correct |
2 |
Correct |
59 ms |
8912 KB |
Output is correct |
3 |
Correct |
55 ms |
8964 KB |
Output is correct |
4 |
Correct |
61 ms |
8992 KB |
Output is correct |
5 |
Correct |
66 ms |
8948 KB |
Output is correct |
6 |
Correct |
63 ms |
8724 KB |
Output is correct |
7 |
Correct |
69 ms |
8704 KB |
Output is correct |
8 |
Correct |
64 ms |
8920 KB |
Output is correct |
9 |
Correct |
81 ms |
8784 KB |
Output is correct |
10 |
Correct |
74 ms |
8976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
7156 KB |
Output is correct |
2 |
Correct |
62 ms |
7308 KB |
Output is correct |
3 |
Correct |
62 ms |
7148 KB |
Output is correct |
4 |
Correct |
52 ms |
7212 KB |
Output is correct |
5 |
Correct |
60 ms |
8808 KB |
Output is correct |
6 |
Correct |
53 ms |
7140 KB |
Output is correct |
7 |
Correct |
58 ms |
7312 KB |
Output is correct |
8 |
Correct |
65 ms |
8936 KB |
Output is correct |
9 |
Correct |
68 ms |
8820 KB |
Output is correct |
10 |
Correct |
55 ms |
7276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
9140 KB |
Output is correct |
2 |
Correct |
96 ms |
8988 KB |
Output is correct |
3 |
Correct |
87 ms |
9120 KB |
Output is correct |
4 |
Correct |
84 ms |
9132 KB |
Output is correct |
5 |
Correct |
83 ms |
9116 KB |
Output is correct |
6 |
Correct |
86 ms |
9176 KB |
Output is correct |
7 |
Correct |
85 ms |
9208 KB |
Output is correct |
8 |
Correct |
82 ms |
9124 KB |
Output is correct |
9 |
Correct |
89 ms |
9224 KB |
Output is correct |
10 |
Correct |
101 ms |
9192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
440 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
360 KB |
Output is correct |
24 |
Correct |
1 ms |
356 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
8772 KB |
Output is correct |
2 |
Correct |
59 ms |
8912 KB |
Output is correct |
3 |
Correct |
55 ms |
8964 KB |
Output is correct |
4 |
Correct |
61 ms |
8992 KB |
Output is correct |
5 |
Correct |
66 ms |
8948 KB |
Output is correct |
6 |
Correct |
63 ms |
8724 KB |
Output is correct |
7 |
Correct |
69 ms |
8704 KB |
Output is correct |
8 |
Correct |
64 ms |
8920 KB |
Output is correct |
9 |
Correct |
81 ms |
8784 KB |
Output is correct |
10 |
Correct |
74 ms |
8976 KB |
Output is correct |
11 |
Correct |
67 ms |
7156 KB |
Output is correct |
12 |
Correct |
62 ms |
7308 KB |
Output is correct |
13 |
Correct |
62 ms |
7148 KB |
Output is correct |
14 |
Correct |
52 ms |
7212 KB |
Output is correct |
15 |
Correct |
60 ms |
8808 KB |
Output is correct |
16 |
Correct |
53 ms |
7140 KB |
Output is correct |
17 |
Correct |
58 ms |
7312 KB |
Output is correct |
18 |
Correct |
65 ms |
8936 KB |
Output is correct |
19 |
Correct |
68 ms |
8820 KB |
Output is correct |
20 |
Correct |
55 ms |
7276 KB |
Output is correct |
21 |
Correct |
93 ms |
9140 KB |
Output is correct |
22 |
Correct |
96 ms |
8988 KB |
Output is correct |
23 |
Correct |
87 ms |
9120 KB |
Output is correct |
24 |
Correct |
84 ms |
9132 KB |
Output is correct |
25 |
Correct |
83 ms |
9116 KB |
Output is correct |
26 |
Correct |
86 ms |
9176 KB |
Output is correct |
27 |
Correct |
85 ms |
9208 KB |
Output is correct |
28 |
Correct |
82 ms |
9124 KB |
Output is correct |
29 |
Correct |
89 ms |
9224 KB |
Output is correct |
30 |
Correct |
101 ms |
9192 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
324 KB |
Output is correct |
42 |
Correct |
1 ms |
324 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
328 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
328 KB |
Output is correct |
50 |
Correct |
1 ms |
440 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
2 ms |
360 KB |
Output is correct |
54 |
Correct |
1 ms |
356 KB |
Output is correct |
55 |
Correct |
2 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
2 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
128 ms |
9016 KB |
Output is correct |
62 |
Correct |
135 ms |
9236 KB |
Output is correct |
63 |
Correct |
110 ms |
8916 KB |
Output is correct |
64 |
Correct |
117 ms |
9176 KB |
Output is correct |
65 |
Correct |
118 ms |
9092 KB |
Output is correct |
66 |
Correct |
116 ms |
9152 KB |
Output is correct |
67 |
Correct |
126 ms |
9012 KB |
Output is correct |
68 |
Correct |
82 ms |
9180 KB |
Output is correct |
69 |
Correct |
83 ms |
8928 KB |
Output is correct |
70 |
Correct |
86 ms |
8852 KB |
Output is correct |