#include <iostream>
#include <algorithm>
#define endl '\n'
using namespace std;
const int N = 511;
const int K = 329;
const long long INF = 1e18;
int n,k;
long long a[N],sum[N];
bool used[N][K];
long long state[N][K];
long long recurse(int pos, int blocks) {
if(pos>n) {
if(blocks==0) return 0;
else return INF;
}
if(blocks==0) return INF;
if(used[pos][blocks]) return state[pos][blocks];
int i;
long long ans = INF, curr, curr_sum, rem_sum;
for(i=pos;i<=n;i++) {
curr_sum=sum[i]-sum[pos-1];
rem_sum=sum[n]-sum[i];
curr=(k-1)*curr_sum*curr_sum-2*curr_sum*(rem_sum)+recurse(i+1,blocks-1);
ans=min(ans,curr);
}
used[pos][blocks]=true;
state[pos][blocks]=ans;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i;
cin>>n>>k;
for(i=1;i<=n;i++) {
cin>>a[i];
sum[i]=a[i]+sum[i-1];
}
cout<<recurse(1,k)<<endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojZGVmaW5lIGVuZGwgJ1xuJwoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IGludCBOID0gNTExOwpjb25zdCBpbnQgSyA9IDMyOTsKY29uc3QgbG9uZyBsb25nIElORiA9IDFlMTg7CgppbnQgbixrOwpsb25nIGxvbmcgYVtOXSxzdW1bTl07CmJvb2wgdXNlZFtOXVtLXTsKbG9uZyBsb25nIHN0YXRlW05dW0tdOwoKbG9uZyBsb25nIHJlY3Vyc2UoaW50IHBvcywgaW50IGJsb2NrcykgewoJaWYocG9zPm4pIHsKCQlpZihibG9ja3M9PTApIHJldHVybiAwOwoJCWVsc2UgcmV0dXJuIElORjsKCX0KCWlmKGJsb2Nrcz09MCkgcmV0dXJuIElORjsKCWlmKHVzZWRbcG9zXVtibG9ja3NdKSByZXR1cm4gc3RhdGVbcG9zXVtibG9ja3NdOwoJaW50IGk7Cglsb25nIGxvbmcgYW5zID0gSU5GLCBjdX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