# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
222931 | Bruteforceman | Split the sequence (APIO14_sequence) | C++11 | 401 ms | 131076 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int a[100010];
int n, k;
long long pre[100010];
long long dp[202][100010], opt[202][100010];
const long long inf = 1e17;
void solve(int row, int l, int r, int start, int fin) {
if(l > r) return ;
int mid = (l + r) >> 1;
dp[row][mid] = -inf;
for(int i = start; i <= fin; i++) {
if(i >= mid) break;
long long cost = dp[row - 1][i] + (pre[n] - pre[mid] + pre[i]) * (pre[mid] - pre[i]);
if(cost > dp[row][mid]) {
dp[row][mid] = cost;
opt[row][mid] = i;
}
}
solve(row, l, mid - 1, start, opt[row][mid]);
solve(row, mid + 1, r, opt[row][mid], fin);
}
int main() {
scanf("%d %d", &n, &k);
k += 1;
for(int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
for(int i = 1; i <= n; i++) {
pre[i] = pre[i - 1] + a[i];
}
long long sum = pre[n];
for(int j = 1; j <= n; j++) {
dp[0][j] = -inf;
}
dp[0][0] = 0;
for(int i = 1; i <= k; i++) {
dp[i][0] = -inf;
solve(i, 1, n, 0, n - 1);
}
printf("%lld\n", dp[k][n] / 2);
int cur = n;
int cnt = k;
while(cnt) {
cur = opt[cnt--][cur];
if(cur) printf("%d ", cur);
}
puts("");
return 0;
}
Compilation message (stderr)
# | 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... |