#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define mp make_pair
using namespace std;
auto IN = freopen("2019partition.in", "r", stdin);
auto OUT = freopen("2019partition.out", "w", stdout);
auto mread = [](){int x;scanf("%lld", &x);return x;};
const int N = 5e5 + 5;
int n = mread(), type = mread(), a[N], f[N][2], s[N];
signed main(){
for(int i = 1; i <= n; i ++){
cin >> a[i];
s[i] = s[i - 1] + a[i];
}
memset(f, 0x3f, sizeof(f));
f[0][0] = f[0][1] = 0;
int p = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > q;
for(int i = 1; i <= n; i ++){
while(q.size() && q.top().fi <= s[i]){
p = max(p, q.top().se + 1);
q.pop();
}
f[i][0] = s[i] - s[p];
f[i][1] = f[p][1] + f[i][0] * f[i][0];
q.push(mp(f[i][0] + s[i], i - 1));
}
printf("%lld", f[n][1]);
return 0;
}