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 <stdio.h>
#define MAXK 222
#define MAXN 101101
typedef long long ll;
int N, K;
int Number[MAXN];
ll Sum[MAXN];
void INPUT() {
scanf("%d %d", &N, &K);
for(int i=1; i<=N; i++) {
scanf("%d", &Number[i]);
Sum[i] = Sum[i-1] + (ll)Number[i];
}
}
int Mom[MAXK][MAXN];
ll Dy[2][MAXN];
void Get(int k, int a, int b, int ga, int gb) {
if(a>b) return;
int now = k%2;
int past = 1 - now;
int m = (a + b)/2;
ll max = -1;
int maxp = 0;
for(int i=ga; i<=gb; i++) {
if(i>=m) break;
ll current = Dy[past][i] + Sum[i]*(Sum[m] - Sum[i]);
if(max<current) {
max = current;
maxp = i ;
}
}
Dy[now][m] = max;
Mom[k ][m] = maxp;
Get(k, a, m-1, ga, maxp);
Get(k, m+1, b, maxp, gb);
}
void Find(int k, int n) {
if(k==1) return;
Find(k-1, Mom[k][n]);
printf("%d ", Mom[k][n]);
}
int main() {
INPUT();
for(int i=2; i<=K+1; i++)
Get( i, i, N, i-1, N-1 );
printf("%lld\n", Dy[1-K%2][N]);
Find(K+1, N);
}
# | 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... |