#include <stdio.h>
#define MAXN 100005
#define MAXK 202
typedef long long lld;
int N,K,A[MAXN],S[MAXN],P[MAXN][MAXK];
int stk[MAXN],scnt;
lld D[MAXN],E[MAXN],F[MAXN];
bool check(int a,int b,int c)
{
return (F[a]-F[b])*(S[b]-S[c]) <= (F[b]-F[c])*(S[a]-S[b]);
}
int main()
{
int i,j,k;
scanf("%d%d",&N,&K); ++K;
for (i=1;i<=N;i++) scanf("%d",A+i), S[i] = S[i-1]+A[i];
stk[++scnt] = 0;
for (j=1;j<=K;j++){
int p = 1;
for (i=j;i<=N;i++){
while (p < scnt && stk[p+1] < i && F[stk[p]]+S[stk[p]]*S[i] <= F[stk[p+1]]+S[stk[p+1]]*S[i]) p++;
int t = stk[p];
D[i] = E[t]+(lld)(S[i]-S[t])*S[t]; P[i][j] = t;
}
for (i=j;i<=N;i++) F[i] = D[i]-S[i]*S[i];
scnt = 0; stk[++scnt] = j;
for (i=j+1;i<=N;i++) if (S[i]){
while (scnt > 1 && check(stk[scnt-1],stk[scnt],i)) scnt--;
stk[++scnt] = i;
}
for (i=1;i<=N;i++) E[i] = D[i];
}
printf("%lld\n",D[N]);
for (i=N,j=K;j;){
if (i < N) printf("%d ",i);
i = P[i][j--];
} puts("");
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
83512 KB |
Output is correct - contestant found the optimal answer: 108 == 108 |
2 |
Correct |
0 ms |
83512 KB |
Output is correct - contestant found the optimal answer: 999 == 999 |
3 |
Correct |
0 ms |
83512 KB |
Output is correct - contestant found the optimal answer: 0 == 0 |
4 |
Correct |
0 ms |
83512 KB |
Output is correct - contestant found the optimal answer: 1542524 == 1542524 |
5 |
Correct |
0 ms |
83512 KB |
Output is correct - contestant found the optimal answer: 4500000000 == 4500000000 |
6 |
Correct |
0 ms |
83512 KB |
Output is correct - contestant found the optimal answer: 1 == 1 |
7 |
Correct |
0 ms |
83512 KB |
Output is correct - contestant found the optimal answer: 1 == 1 |
8 |
Correct |
0 ms |
83512 KB |
Output is correct - contestant found the optimal answer: 1 == 1 |
9 |
Correct |
0 ms |
83512 KB |
Output is correct - contestant found the optimal answer: 100400096 == 100400096 |
10 |
Incorrect |
0 ms |
83512 KB |
Output isn't correct - contestant didn't find the optimal answer: 900240000 < 900320000 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |