#include <bits/stdc++.h>
#define ll long long
using namespace std ;
const int maxn = 5e3+5 ;
int n , type , a[maxn] ;
ll s[maxn] , dp[maxn][maxn] , ans = LLONG_MAX ;
int main(){
freopen("2019partition.in","r",stdin);
freopen("2019partition.out","w",stdout);
cin >> n >> type ;
memset(dp,0x7f,sizeof(dp)) ;
for ( int i = 1 ; i <= n ; i++ ){
cin >> a[i] ;
s[i] = s[i-1]+a[i] ;
}
for ( int i = 1 ; i <= n ; i++ ){
dp[i][1] = s[i]*s[i] ;
for ( int j = 2 ; j <= i ; j++ ){
for ( int k = 1 ; k <= j-1 ; k++ ){
if (s[i]-s[j-1]>=s[j-1]-s[k-1])
dp[i][j] = min(dp[i][j],dp[j-1][k]+(s[i]-s[j-1])*(s[i]-s[j-1])) ;
}
}
}
for ( int i = 1 ; i <= n ; i++ ) ans = min(ans,dp[n][i]) ;
cout << ans ;
return 0 ;
}