1 #include<cstdio>
2
3 #include<cstdlib>
4
5 #include<cmath>
6
7 #include<cstring>
8
9 #include<algorithm>
10
11 #include<iostream>
12
13 #include<vector>
14
15 #include<map>
16
17 #include<set>
18
19 #include<queue>
20
21 #include<string>
22
23 #define inf 1000000000
24
25 #define maxn 100000+1000
26
27 #define maxm 201
28
29 #define eps 1e-10
30
31 #define ll long long
32
33 #define pa pair<int,int>
34
35 #define for0(i,n) for(int i=0;i<=(n);i++)
36
37 #define for1(i,n) for(int i=1;i<=(n);i++)
38
39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
40
41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
42
43 #define mod 1000000007
44
45 using namespace std;
46
47 inline int read()
48
49 {
50
51 int x=0,f=1;char ch=getchar();
52
53 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
54
55 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
56
57 return x*f;
58
59 }
60 int n,m,t=0,q[maxn];
61 ll f[2][maxn],s[maxn];
62 inline double k(int i,int j)
63 {
64 if(s[i]-s[j]<eps)return inf;
65 else return (double)(f[1-t][i]-s[i]*s[i]-f[1-t][j]+s[j]*s[j])/(double)(s[i]-s[j]);
66 }
67
68 int main()
69
70 {
71 freopen("input.txt","r",stdin);
72 freopen("output.txt","w",stdout);
73 n=read();m=read();
74 s[0]=0;
75 for1(i,n)s[i]=s[i-1]+read();
76 for1(i,n)f[0][i]=s[i]*(s[n]-s[i]);
77 for1(j,m)
78 {
79 t=1-t;
80 int l=1,r=0;
81 for1(i,n)
82 {
83 while(l<r&&k(q[l+1],q[l])>s[n]-2*s[i])l++;
84 f[t][i]=f[1-t][q[l]]+(s[i]-s[q[l]])*(s[n]-s[i]+s[q[l]]);
85 if(i<j+1)f[t][i]=0;
86 if(i==j+1)f[t][i]=f[1-t][i-1]+(s[i]-s[i-1])*(s[n]-s[i]+s[i-1]);
87 while(l<r&&k(i,q[r])>k(q[r],q[r-1]))r--;
88 q[++r]=i;
89 }
90 }
91 printf("%lld\n",f[t][n]>>1);
92
93 return 0;
94
95 }