#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
constexpr int nax = 105;
constexpr int INF = 1e12 + 5;
int dp[nax][nax];
int mx[nax][nax];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
assert(n < nax);
vector<int> a(n);
for(int& i : a) {
cin >> i;
}
if (n == k) {
cout << accumulate(a.begin(), a.end(), 0LL);
exit(0);
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
mx[i][j] = INF;
dp[i][j] = INF;
}
}
for(int i = 0; i < n; i++) {
priority_queue<int> curr;
for(int j = i; j < n; j++) {
curr.push(a[j]);
mx[i][j] = curr.top();
}
}
for(int i = 0; i < n; i++) {
dp[i][0] = 0;
dp[i][1] = mx[0][i];
}
// for(int i = 0; i < n; i++) {
// for(int j = 0; j < n; j++) {
// cout << i << " " << j << ": " << mx[i][j] << "\n";
// } cout << "\n";
// }
for(int splits = 2; splits <= k; splits++) {
for(int i = splits-1; i < n; i++) {
int l = splits-1, r = i-1, ans = splits-1;
while(l <= r) {
int mid = (l + r) / 2;
int f1 = dp[mid-1][splits-1] + mx[mid][i];
int f2 = dp[mid][splits-1] + mx[mid+1][i];
if (f1 > f2) {
l = mid + 1;
ans = mid;
}
else {
r = mid - 1;
}
}
dp[i][splits] = dp[ans][splits-1] + mx[ans+1][i];
// for(int j = 0; j < i; j++) {
// if (dp[j][splits-1] == INF) continue;
// dp[i][splits] = min(dp[i][splits], dp[j][splits-1] + mx[j+1][i]);
// cout << i << " " << splits << ": " << dp[j][splits-1] << " " << mx[j+1][i] << " "
// << dp[j][splits-1] + mx[j+1][i] << "\n";
//
//// cout << i << " " << splits << " " << j << ": " << dp[j][splits-1] << " " << mx[j+1][i]
//// << " " << dp[j][splits-1] + mx[j+1][i] << "\n";
// }
// cout << i << " " << splits << ": " << dp[i][splits] << "\n";
}
}
cout << dp[n-1][k] << "\n";
return 0;
}
/*
6 5
6 9 3 8 2 5
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
320 KB |
Output is correct |
8 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |