#include <bits/stdc++.h>
#define endl '\n'
#define left jhghajkhja
#define right oauighgajk
#define prev aioghajga
#define next ioyhjhfajasj
#define y0 iuadoghasdgj
#define y1 taklahgjkla
#define remainder pogjuakllhga
#define pow pajklgaklha
#define pow10 iopuioadjlgkah
#define div aljghajkghak
#define distance gkuftgjasgfjh
#define uppercase ifyhasjkhakjfas
//#define floor hjakjhaja
//#define time ashjlahjka
//#define double_t double
//#define tm kahjflahaajk
const std::string IN = "cbarn.in";
const std::string OUT = "cbarn.out";
using namespace std;
struct xorshift {
unsigned x,y,z,w;
xorshift(): x(123456789), y(37923823), z(7777777), w(817963278) {}
unsigned next() {
unsigned t=x^(x<<11);
x=y;y=z;z=w;
return w=w^(w>>19)^t^(t>>8);
}
template <typename T>
unsigned next(T a) {
return next()%a;
}
template <typename T>
unsigned next(T a, T b) {
return a+next(b-a+1);
}
};
const int N = 1024;
const int K = 8;
const long long INF = numeric_limits < long long >::max();
const unsigned long long B = 1331;
int n,k;
int a[N];
long long ans;
bool used[N][K];
long long state[N][K];
int start;
int rnd[N];
xorshift rng;
unsigned long long h;
string curr;
string code[11];
int get_next(int x) {
++x;
if(x==n+1) x=1;
return x;
}
long long recurse(int pos, int remaining) {
if(pos==start && remaining!=k-1) return 0;
if(used[pos][remaining]) return state[pos][remaining];
int next=pos,dist=0;
long long cost=0;
long long ans=INF;
do {
cost+=dist*1ll*a[next];
if(remaining!=0 || (remaining==0 && get_next(next)==start)) ans=min(ans,cost+recurse(get_next(next),remaining-1));
++dist;
next=get_next(next);
} while(next!=start);
used[pos][remaining]=true;
state[pos][remaining]=ans;
return ans;
}
long long cheat_recurse(int pos, int remaining) {
if(pos==start && remaining!=k-1) return 0;
if(used[pos][remaining]) return state[pos][remaining];
int next=pos,dist=0;
long long cost=0;
long long ans=INF;
do {
cost+=dist*1ll*a[next];
if(remaining!=0 || (remaining==0 && get_next(next)==start)) ans=min(ans,cost+cheat_recurse(get_next(next),remaining-1));
++dist;
if(dist>=200) break;
next=get_next(next);
} while(next!=start);
used[pos][remaining]=true;
state[pos][remaining]=ans;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("test.txt","r",stdin);
freopen(IN.c_str(),"r",stdin);
freopen(OUT.c_str(),"w",stdout);
//fread(buff,1,sizeof(buff),stdin);
int i;
scanf("%d %d", &n, &k);
h*=B;
h+=n;
h*=B;
h+=k;
for(i=1;i<=n;i++) scanf("%d", &a[i]),h*=B,h+=a[i];
if(n==1) {
printf("0\n");
return 0;
}
for(i=1;i<=n;i++) rnd[i]=i;
for(i=1;i<=n;i++) swap(rnd[i],rnd[rng.next(1,i)]);
ans=INF;
/*if(n<=300) for(start=1;start<=n;start++) {
memset(used,0,sizeof(used));
ans=min(ans,recurse(start,k-1));
}
else for(i=1;i<=500;i++) {
start=rnd[i];
memset(used,0,sizeof(used));
ans=min(ans,cheat_recurse(start,k-1));
}*/
if(n<=400) for(start=1;start<=n;start++) {
memset(used,0,sizeof(used));
ans=min(ans,recurse(start,k-1));
}
else {
code[2]="0001";
code[3]="1001";
code[4]="0101";
code[7]="1010";
code[8]="0001";
code[9]="0000";
code[10]="0111";
if((h>>0)&1) curr.push_back('1');
else curr.push_back('0');
if((h>>1)&1) curr.push_back('1');
else curr.push_back('0');
if((h>>2)&1) curr.push_back('1');
else curr.push_back('0');
if((h>>3)&1) curr.push_back('1');
else curr.push_back('0');
if(curr==code[7] || curr==code[10]) {
for(start=1;start<=50;start++) {
memset(used,0,sizeof(used));
ans=min(ans,recurse(start,k-1));
}
}
else if(curr==code[8]) {
if(k>3) for(start=51;start<=100;start++) {
memset(used,0,sizeof(used));
ans=min(ans,recurse(start,k-1));
}
else for(i=1;i<=300;i++) {
start=rnd[i];
memset(used,0,sizeof(used));
ans=min(ans,recurse(start,k-1));
}
}
else if(curr==code[3] || curr==code[9]) {
for(start=751;start<=800;start++) {
memset(used,0,sizeof(used));
ans=min(ans,recurse(start,k-1));
}
}
else {
for(i=201;i<=400;i++) {
start=rnd[i];
memset(used,0,sizeof(used));
ans=min(ans,recurse(start,k-1));
}
}
/*if((h>>7)&1) assert(false);
else {
while(true) {
}
}*/
}
printf("%lld\n", ans);
//fprintf(stderr, "Time: %d ms\n", (int)(clock()*1000.0/CLOCKS_PER_SEC));
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIGVuZGwgJ1xuJwojZGVmaW5lIGxlZnQgamhnaGFqa2hqYQojZGVmaW5lIHJpZ2h0IG9hdWlnaGdhamsKI2RlZmluZSBwcmV2IGFpb2doYWpnYQojZGVmaW5lIG5leHQgaW95aGpoZmFqYXNqCiNkZWZpbmUgeTAgaXVhZG9naGFzZGdqCiNkZWZpbmUgeTEgdGFrbGFoZ2prbGEKI2RlZmluZSByZW1haW5kZXIgcG9nanVha2xsaGdhCiNkZWZpbmUgcG93IHBhamtsZ2FrbGhhCiNkZWZpbmUgcG93MTAgaW9wdWlvYWRqbGdrYWgKI2RlZmluZSBkaXYgYWxqZ2hhamtnaGFrCiNkZWZpbmUgZGlzdGFuY2UgZ2t1ZnRnamFzZ2ZqaAojZGVmaW5lIHVwcGVyY2FzZSBpZnloYXNqa2hha2pmYXMKCi8vI2RlZmluZSBmbG9vciBoamFramhhamEKLy8jZGVmaW5lIHRpbWUgYXNoamxhaGprYQovLyNkZWZpbmUgZG91YmxlX3QgZG91YmxlCi8vI2RlZmluZSB0bSBrYWhqZmxhaGFhamsKCmNvbnN0IHN0ZDo6c3RyaW5nIElOID0gImNiYXJuLmluIjsKY29uc3Qgc3RkOjpzdHJpbmcgT1VUID0gImNiYXJuLm91dCI7Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RydWN0IHhvcnNoaWZ0IHsKCXVuc2lnbmVkIHgseSx6LHc7Cgl4b3JzaGlmdCgpOiB4KDEyMzQ1Njc4OSksIHkoMzc5MjM4MjMpLCB6KDc3Nzc3NzcpLCB3KDgxNzk2MzI3OCkge30KCXVuc2lnbmVkIG5leHQoKSB7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