# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
37545 | Talant | Split the sequence (APIO14_sequence) | C++11 | 0 ms | 0 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>
#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair
#define ll long long
using namespace std;
const ll inf = (ll)1e9 + 7;
const int N = (int)1e5 + 7;
ll dp[N][2];
ll p[N];
int pr[N][201];
int n,kk,id;
ll calc (int r,int l) {
return p[l] * (p[r] - p[l]);
}
void compute (int k,int l,int r,int L,int R) {
if (l > r)
return;
int m = (l + r) >> 1;
dp[m][k & 1] = 0;
pr[m][k] = L;
for (int i = L; i <= min(m - 1,R); i ++) {
ll now = dp[i][k & 1 ^ 1] + calc(m,i);
if (now >= dp[m][k & 1]) {
dp[m][k & 1] = now;
pr[m][k] = i;
}
}
compute (k,l,m - 1,L,pr[m][k]);
compute (k,m + 1,r,pr[m][k],R);
}
main () {
scanf ("%d%d", &n,&kk);
for (int i = 1; i <= n; i ++) {
scanf ("%lld", &p[i]);
p[i] += p[i - 1];
}
for (int i = 1; i <= kk; i ++)
compute (i,1,n,1,n - 1);
printf("%lld\n", dp[n][kk & 1]);
while (k > 0) {
printf("%d ", pr[n][kk]);
n = pr[n][kk];
kk --;
}
}
/**
7 3
4 1 3 4 0 2 3
**/