이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#define int long long
using namespace std;
const int MAX_N = 1e4;
const int MAX_K = 200;
const int INF = (1LL << 60);
int a[MAX_N + 1], sp[MAX_N + 1], dp[MAX_N + 1][MAX_K + 1];
int sol[MAX_N + 1][MAX_K + 1];
int n, k;
void divide(int k, int l, int r, int p, int q) {
if (p > q) {
return;
}
int mid = (p + q) / 2;
dp[mid][k] = -INF;
int best = 0;
for (int i = l; i <= min(mid, r); i++) {
int kek = (sp[mid] - sp[i - 1]) * (sp[n] - sp[mid]);
if (dp[i - 1][k - 1] + kek >= dp[mid][k]) {
dp[mid][k] = dp[i - 1][k - 1] + kek;
best = i;
}
}
sol[mid][k] = best - 1;
divide(k, l, best, p, mid - 1);
divide(k, best, r, mid + 1, q);
}
signed main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sp[i] = sp[i - 1] + a[i];
}
k++;
for (int i = 1; i <= n; i++) {
dp[i][1] = sp[i] * (sp[n] - sp[i]);
}
for (int i = 2; i <= k; i++) {
divide(i, 1, n, 1, n);
}
cout << dp[n][k] << "\n";
int aux = n;
for (int i = k; i >= 2; i--) {
cout << sol[aux][i] << " ";
aux = sol[aux][i];
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |