// https://oj.uz/problem/view/NOI19_feast
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n, k;
cin >> n >> k;
vector<ll> a (n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
ll ans = 0;
ll lo = 0;
ll hi = 1e9;
while (lo < hi) {
ll mid = lo + (hi - lo)/2;
vector<ll> dp (n+1, 0);
vector<ll> p (n+1, 0);
ll best = 0;
int id = 0;
vector<int> cnt (n+1, 0);
for (int i = 0; i < n; i++) {
p[i+1] = p[i] + a[i];
dp[i+1] = max(dp[i+1], dp[i]);
cnt[i+1] = cnt[i];
if (dp[i+1] < best + p[i+1] - lo) {
dp[i+1] = best + p[i+1] - lo;
cnt[i+1] = cnt[id] + 1;
}
if (best < dp[i+1] - p[i+1]) {
best = dp[i+1] - p[i+1];
id = i+1;
}
}
if (cnt[n] <= k) {
hi = mid;
ans = dp[n];
} else {
lo = mid+1;
}
}
cout << ans + k*lo << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
8512 KB |
Output is correct |
2 |
Correct |
139 ms |
8640 KB |
Output is correct |
3 |
Correct |
167 ms |
8732 KB |
Output is correct |
4 |
Correct |
138 ms |
8676 KB |
Output is correct |
5 |
Correct |
134 ms |
10612 KB |
Output is correct |
6 |
Correct |
132 ms |
8520 KB |
Output is correct |
7 |
Correct |
137 ms |
8456 KB |
Output is correct |
8 |
Correct |
128 ms |
8712 KB |
Output is correct |
9 |
Correct |
119 ms |
8444 KB |
Output is correct |
10 |
Correct |
135 ms |
8492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
96 ms |
8600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
176 ms |
8576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
8512 KB |
Output is correct |
2 |
Correct |
139 ms |
8640 KB |
Output is correct |
3 |
Correct |
167 ms |
8732 KB |
Output is correct |
4 |
Correct |
138 ms |
8676 KB |
Output is correct |
5 |
Correct |
134 ms |
10612 KB |
Output is correct |
6 |
Correct |
132 ms |
8520 KB |
Output is correct |
7 |
Correct |
137 ms |
8456 KB |
Output is correct |
8 |
Correct |
128 ms |
8712 KB |
Output is correct |
9 |
Correct |
119 ms |
8444 KB |
Output is correct |
10 |
Correct |
135 ms |
8492 KB |
Output is correct |
11 |
Incorrect |
96 ms |
8600 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |