1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<cmath>
5
6 #define maxn 100001
7
8 using namespace std;
9
10 long long sum[maxn],f[maxn][2];
11
12 int n,que[maxn],head=1,tail=1,k,b=0;
13
14 inline double K(int x,int y,int d)
15 {
16 return (double)(f[y][d]-f[x][d]+sum[x]*sum[x]-sum[y]*sum[y])/(sum[x]-sum[y]);
17 }
18
19 inline long long read()
20 {
21 long long x=0;
22 int f=1;char ch=getchar();
23 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
24 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
25 return x*f;
26 }
27
28 void DP()
29 {
30 int d=0;
31 for(int i=1;i<=k;i++)
32 {
33 d^=1;
34 head=tail=1;
35 for(int j=i;j<=n;j++)
36 {
37 while(head<tail && K(que[head],que[head+1],d^1)<=sum[j])head++;
38 int sd=que[head];
39 f[j][d]=f[sd][d^1]+sum[sd]*(sum[j]-sum[sd]);
40 while(head<tail && K(que[tail],j,d^1)<=K(que[tail-1],que[tail],d^1))tail--;
41 que[++tail]=j;
42 }
43 }
44 printf("%lld",f[n][k&1]);
45 }
46
47 int main()
48 {
49 n=read(),k=read();
50 for(int i=1;i<=n;i++)
51 {
52 sum[i]=read();
53 if(sum[i]==0){n--,i--;continue;}
54 sum[i]+=sum[i-1];
55 }
56 DP();
57 return 0;
58 }