Problem:
H:命綱
物語
北海道大学登山プログラミングサークル（HCPC）はサークル活動の競プロ登山合宿を行っていたところ険しい崖に直面してしまった!!
この崖を渡るには命綱が必要不可欠だが、残念な事に HCPC は大きな命綱一本しか持っていなかった。
そこで彼らはこの命綱を崖の対岸へ持って帰ってを繰り返すことにより全員を対岸に運ぶことにした。
問題
サークル員の数 $N$ と命綱を一度につかめる最大人数 $K$ が与えられる。最初、全てのサークル員と一本の命綱がスタート地点に存在する。
また、$i = 1,\dots,N$ 番目のサークル員の移動コストは $A_i$ である。命綱を用いて
移動
を繰り返し、全てのサークル員をゴール地点まで移動させるのにかかる最小の時間を出力せよ。
ただし、移動は以下のように行われる。
$1$ 人以上 $K$ 人以下のサークル員が、命綱をつかんで移動する。
命綱がスタート地点にある場合はゴール地点へ、ゴール地点にある場合はスタート地点へ命綱をつかんで移動する。このとき、命綱もサークル員と共に移動する。
移動にかかる時間は、移動しているサークル員の移動コストの最大値である。
命綱の受け渡しにかかる時間は無視してよい。
入力形式
入力は以下の形式で与えられる。
$N$ $K$
$A_1$ $A_2$ $\ldots$ $A_N$
制約
$1 \leq N \leq 4{,}000$
$2 \leq K \leq 4{,}000$
$1 \leq A_i \leq 10^{9}$
$N-1 \equiv 0 \mod K-1$
入力は整数で与えられる。
出力形式
移動にかかる最小の時間を一行で出力せよ。
入力例 1
4 2
1 2 5 10
出力例 1
17
以下のようにして合計時間 $17$ で渡ることができる。
$1$ 番目の人と $2$ 番目の人がゴール地点へ渡る。
$1$ 番目の人がゴール地点から命綱を持って帰る。
$3$ 番目の人と $4$ 番目の人がゴール地点へ渡る。
$2$ 番目の人がゴール地点から命綱を持って帰る。
$1$ 番目の人と $2$ 番目の人がゴール地点へ渡る。
$17$ 未満では渡れないのでこれが最小となる。
入力例 2
10 4
4 6 8 10 12 14 17 20 24 27
出力例 2
61
入力例 3
10 4
1 1 1000 1000 1000 1000 1000 1000 1000 1000
出力例 3
2007

Submitted solutions:

code_snippet: #ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#endif

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
#define int ll

bool dbg=false;

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;
using vvi=vc<vc<int>>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.a<<","<<p.b<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ";
	dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif

using uint=unsigned;
using ull=unsigned long long;

template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
	return os<<vc<t>(all(a));
}

ll rand_int(ll l, ll r) { //[l, r]
	//#ifdef LOCAL
	static mt19937_64 gen;
	/*#else
	static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
	#endif*/
	return uniform_int_distribution<ll>(l, r)(gen);
}

ll rand_int(ll k){ //[0,k)
	return rand_int(0,k-1);
}
string rand_string(int n,char lw,char up){
	string s(n,'?');
	rep(i,n)s[i]=rand_int(lw,up);
	return s;
}

int current_run_id,run_batch_size=1000;
int calc_random_limit(){
	return current_run_id/run_batch_size+1;
}
template<class t>
void generate_single(t&a){
	a=rand_int(1,calc_random_limit());
}
void generate_single(string&a){
	int n;generate_single(n);
	a=rand_string(n,'a','b');
}
template<class t,class u>
void generate_single(pair<t,u>&a){
	generate_single(a.a);
	generate_single(a.b);
}
//https://trap.jp/post/1224/
template<class... Args>
void input(Args&... a){
	if(dbg){
		(generate_single(a),...);
	}else{
		(cin >> ... >> a);
	}
}
#define INT(...) int __VA_ARGS__;input(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;input(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;input(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;input(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;input(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;input(__VA_ARGS__)
#define overload3(a,b,c,d,...) d
#define VI2(name,size) vi name(size);rep(i_##name,size)input(name[i_##name]);
#define VI3(name,size,offset) vi name(size);rep(i_##name,size)input(name[i_##name]),name[i_##name]+=offset;
#define VI(...) overload3(__VA_ARGS__,VI3,VI2)(__VA_ARGS__)
#define VPI(name,size) vc<pi> name(size);rep(i_##name,size)input(name[i_##name]);
#define VVI(name,sizeN,sizeM) vvi name(sizeN,vi(sizeM));\
rep(i_##name,sizeN)rep(j_##name,sizeM)input(name[i_##name][j_##name]);

#define overload5(a,b,c,d,e,f,...) f
#define VVC4(type,name,sizeN,sizeM) vvc<type> name(sizeN,vc<type>(sizeM));
#define VVC5(type,name,sizeN,sizeM,ini) vvc<type> name(sizeN,vc<type>(sizeM,ini));
#define VVC(...) overload5(__VA_ARGS__,VVC5,VVC4)(__VA_ARGS__)

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
	if(i)os<<",";
	os<<get<i>(t);
	print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
	os<<"{";
	print_tuple<0,tuple<Args...>,Args...>(os,t);
	return os<<"}";
}

ll read(){
	ll i;
	cin>>i;
	return i;
}

vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=read()+off;
	return v;
}

pi readpi(int off=0){
	int a,b;cin>>a>>b;
	return pi(a+off,b+off);
}

template<class t>
void print_single(t x,int suc=1){
	cout<<x;
	if(suc==1){
		if(dbg)cout<<endl;
		else cout<<"\n";
	}
	if(suc==2)
		cout<<" ";
}

template<class t,class u>
void print_single(const pair<t,u>&p,int suc=1){
	print_single(p.a,2);
	print_single(p.b,suc);
}

template<class T>
void print_single(const vector<T>&v,int suc=1){
	rep(i,v.size())
		print_single(v[i],i==int(v.size())-1?suc:2);
}

template<class T>
void print_offset(const vector<T>&v,ll off,int suc=1){
	rep(i,v.size())
		print_single(v[i]+off,i==int(v.size())-1?suc:2);
}

template<class T,size_t N>
void print_single(const array<T,N>&v,int suc=1){
	rep(i,N)
		print_single(v[i],i==int(N)-1?suc:2);
}

template<class T>
void print(const T&t){
	print_single(t);
}

template<class T,class ...Args>
void print(const T&t,const Args&...args){
	print_single(t,2);
	print(args...);
}

template<class T>
void printvv(const vvc<T>&vs){
	for(const auto&row:vs)print(row);
}

string readString(){
	string s;
	cin>>s;
	return s;
}

template<class T>
T sq(const T& t){
	return t*t;
}

void YES(bool ex=true){
	cout<<"YES\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void NO(bool ex=true){
	cout<<"NO\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void Yes(bool ex=true){
	cout<<"Yes\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void No(bool ex=true){
	cout<<"No\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
//#define CAPITAL
/*
void yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<"\n";
	#else
	cout<<"Yes"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void no(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<"\n";
	#else
	cout<<"No"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}*/
void possible(bool ex=true){
	#ifdef CAPITAL
	cout<<"POSSIBLE"<<"\n";
	#else
	cout<<"Possible"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void impossible(bool ex=true){
	#ifdef CAPITAL
	cout<<"IMPOSSIBLE"<<"\n";
	#else
	cout<<"Impossible"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}

constexpr ll ten(int n){
	return n==0?1:ten(n-1)*10;
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

int topbit(signed t){
	return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
	return t==0?-1:63-__builtin_clzll(t);
}
int topbit(ull t){
	return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
	return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
	return a==0?64:__builtin_ctzll(a);
}
int botbit(ull a){
	return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(ll t){
	return __builtin_popcountll(t);
}
int popcount(ull t){
	return __builtin_popcountll(t);
}
int bitparity(ll t){
	return __builtin_parityll(t);
}
bool ispow2(int i){
	return i&&(i&-i)==i;
}
ll mask(int i){
	return (ll(1)<<i)-1;
}
ull umask(int i){
	return (ull(1)<<i)-1;
}
ll minp2(ll n){
	if(n<=1)return 1;
	else return ll(1)<<(topbit(n-1)+1);
}

bool inc(int a,int b,int c){
	return a<=b&&b<=c;
}

template<class S> void mkuni(S&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

template<class t> bool isuni(vc<t> v){
	int s=si(v);
	mkuni(v);
	return si(v)==s;
}

template<class t>
void myshuffle(vc<t>&a){
	rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}

template<class S,class u>
int lwb(const S&v,const u&a){
	return lower_bound(all(v),a)-v.bg;
}
template<class t,class u>
bool bis(const vc<t>&v,const u&a){
	return binary_search(all(v),a);
}

//VERIFY: yosupo
//KUPC2017J
//AOJDSL1A
//without rank
struct unionfind{
	vi p,s;
	int c;
	unionfind(int n):p(n,-1),s(n,1),c(n){}
	void clear(){
		fill(all(p),-1);
		fill(all(s),1);
		c=si(p);
	}
	int find(int a){
		return p[a]==-1?a:(p[a]=find(p[a]));
	}
	//set b to a child of a
	bool unite(int a,int b){
		a=find(a);
		b=find(b);
		if(a==b)return false;
		p[b]=a;
		s[a]+=s[b];
		c--;
		return true;
	}
	bool same(int a,int b){
		return find(a)==find(b);
	}
	int sz(int a){
		return s[find(a)];
	}
};

vvc<int> readGraph(int n,int m){
	vvc<int> g(n);
	rep(i,m){
		int a,b;
		cin>>a>>b;
		//sc.read(a,b);
		a--;b--;
		g[a].pb(b);
		g[b].pb(a);
	}
	return g;
}

vvc<int> readTree(int n){
	if(dbg){
		vvc<int> t(n);
		unionfind uf(n);
		while(uf.c>1){
			int a=rand_int(n);
			int b=rand_int(n);
			if(uf.unite(a,b)){
				t[a].pb(b);
				t[b].pb(a);
			}
		}
		return t;
	}else{
		return readGraph(n,n-1);
	}
}

void printTree(const vvc<int> t){
	int n=si(t);
	int degsum=0;
	rep(i,n)degsum+=si(t[i]);
	if(degsum==n-1){
		//directed
		rep(i,si(t))for(auto j:t[i]){
			print(i+1,j+1);
		}
	}else if(degsum==2*(n-1)){
		//undirected
		rep(i,si(t))for(auto j:t[i])if(i<j){
			print(i+1,j+1);
		}
	}else{
		assert(false);
	}
}

template<class t>
vc<t> presum(const vc<t>&a){
	vc<t> s(si(a)+1);
	rep(i,si(a))s[i+1]=s[i]+a[i];
	return s;
}
vc<ll> presum(const vi&a){
	vc<ll> s(si(a)+1);
	rep(i,si(a))s[i+1]=s[i]+a[i];
	return s;
}
//BIT で数列を管理するときに使う (CF850C)
template<class t>
vc<t> predif(vc<t> a){
	gnr(i,1,si(a))a[i]-=a[i-1];
	return a;
}
template<class t>
vvc<ll> imos(const vvc<t>&a){
	int n=si(a),m=si(a[0]);
	vvc<ll> b(n+1,vc<ll>(m+1));
	rep(i,n)rep(j,m)
		b[i+1][j+1]=b[i+1][j]+b[i][j+1]-b[i][j]+a[i][j];
	return b;
}

//verify してないや
void transvvc(int&n,int&m){
	swap(n,m);
}
template<class t,class... Args>
void transvvc(int&n,int&m,vvc<t>&a,Args&...args){
	assert(si(a)==n);
	vvc<t> b(m,vi(n));
	rep(i,n){
		assert(si(a[i])==m);
		rep(j,m)b[j][i]=a[i][j];
	}
	a.swap(b);
	transvvc(n,m,args...);
}
//CF854E
void rotvvc(int&n,int&m){
	swap(n,m);
}
template<class t,class... Args>
void rotvvc(int&n,int&m,vvc<t>&a,Args&...args){
	assert(si(a)==n);
	vvc<t> b(m,vi(n));
	rep(i,n){
		assert(si(a[i])==m);
		rep(j,m)b[m-1-j][i]=a[i][j];
	}
	a.swap(b);
	rotvvc(n,m,args...);
}

//ソートして i 番目が idx[i]
//CF850C
template<class t>
vi sortidx(const vc<t>&a){
	int n=si(a);
	vi idx(n);iota(all(idx),0);
	sort(all(idx),[&](int i,int j){return a[i]<a[j];});
	return idx;
}
//vs[i]=a[idx[i]]
//例えば sortidx で得た idx を使えば単にソート列になって返ってくる
//CF850C
template<class t>
vc<t> a_idx(const vc<t>&a,const vi&idx){
	int n=si(a);
	assert(si(idx)==n);
	vc<t> vs(n);
	rep(i,n)vs[i]=a[idx[i]];
	return vs;
}
//CF850C
vi invperm(const vi&p){
	int n=si(p);
	vi q(n);
	rep(i,n)q[p[i]]=i;
	return q;
}

template<class t,class s=t>
s SUM(const vc<t>&a){
	return accumulate(all(a),s(0));
}
template<class t,size_t K,class s=t>
s SUM(const array<t,K>&a){
	return accumulate(all(a),s(0));
}

template<class t>
t MAX(const vc<t>&a){
	return *max_element(all(a));
}

template<class t>
pair<t,int> MAXi(const vc<t>&a){
	auto itr=max_element(all(a));
	return mp(*itr,itr-a.bg);
}

template<class A>
auto MIN(const A&a){
	return *min_element(all(a));
}

template<class t>
pair<t,int> MINi(const vc<t>&a){
	auto itr=min_element(all(a));
	return mp(*itr,itr-a.bg);
}

vi vid(int n){
	vi res(n);iota(all(res),0);
	return res;
}

template<class S>
void soin(S&s){
	sort(all(s));
}

template<class S,class F>
void soin(S&s,F&&f){
	sort(all(s),forward<F>(f));
}

template<class S>
S soout(S s){
	soin(s);
	return s;
}

template<class S>
void rein(S&s){
	reverse(all(s));
}

template<class S>
S reout(S s){
	rein(s);
	return s;
}

template<class t,class u>
pair<t,u>&operator+=(pair<t,u>&a,pair<t,u> b){
	a.a+=b.a;a.b+=b.b;return a;}
template<class t,class u>
pair<t,u>&operator-=(pair<t,u>&a,pair<t,u> b){
	a.a-=b.a;a.b-=b.b;return a;}
template<class t,class u>
pair<t,u> operator+(pair<t,u> a,pair<t,u> b){return mp(a.a+b.a,a.b+b.b);}
template<class t,class u>
pair<t,u> operator-(pair<t,u> a,pair<t,u> b){return mp(a.a-b.a,a.b-b.b);}
template<class t,class u,class v>
pair<t,u>&operator*=(pair<t,u>&a,v b){
	a.a*=b;a.b*=b;return a;}
template<class t,class u,class v>
pair<t,u> operator*(pair<t,u> a,v b){return a*=b;}
template<class t,class u>
pair<t,u> operator-(pair<t,u> a){return mp(-a.a,-a.b);}
namespace std{
template<class t,class u>
istream&operator>>(istream&is,pair<t,u>&a){
	return is>>a.a>>a.b;
}
}

template<class t>
t gpp(vc<t>&vs){
	assert(si(vs));
	t res=move(vs.back());
	vs.pop_back();
	return res;
}

template<class t,class u>
void pb(vc<t>&a,const vc<u>&b){
	a.insert(a.ed,all(b));
}

template<class t,class...Args>
vc<t> cat(vc<t> a,Args&&...b){
	(pb(a,forward<Args>(b)),...);
	return a;
}

template<class t,class u>
vc<t>& operator+=(vc<t>&a,u x){
	for(auto&v:a)v+=x;
	return a;
}

template<class t,class u>
vc<t> operator+(vc<t> a,u x){
	return a+=x;
}

template<class t>
vc<t> operator+(const vc<t>&a,const vc<t>&b){
	vc<t> c(max(si(a),si(b)));
	rep(i,si(a))c[i]+=a[i];
	rep(i,si(b))c[i]+=b[i];
	return c;
}

template<class t,class u>
vc<t>& operator-=(vc<t>&a,u x){
	for(auto&v:a)v-=x;
	return a;
}
template<class t,class u>
vc<t>& operator-(vc<t> a,u x){
	return a-=x;
}

template<class t,class u>
vc<t>& operator*=(vc<t>&a,u x){
	for(auto&v:a)v*=x;
	return a;
}
template<class t,class u>
vc<t>& operator*(vc<t> a,u x){
	return a*=x;
}

template<class t,class u>
void remval(vc<t>&a,const u&v){
	a.erase(remove(all(a),v),a.ed);
}
//消した要素の個数を返してくれる
//UCUP 2-8-F
template<class t,class F>
int remif(vc<t>&a,F f){
	auto itr=remove_if(all(a),f);
	int res=a.ed-itr;
	a.erase(itr,a.ed);
	return res;
}

template<class VS,class u>
void fila(VS&vs,const u&a){
	fill(all(vs),a);
}

template<class t,class u>
int findid(const vc<t>&vs,const u&a){
	auto itr=find(all(vs),a);
	if(itr==vs.ed)return -1;
	else return itr-vs.bg;
}

template<class t>
void rtt(vc<t>&vs,int i){
	rotate(vs.bg,vs.bg+i,vs.ed);
}

//Multiuni2023-8 C
//f(lw)=false,...,f(n-1)=false,f(n)=true,...,f(up)=true,
//のときに n を返す
template<class F>
int find_min_true(int lw,int up,F f){
	while(up-lw>1){
		const int mid=(lw+up)/2;
		if(f(mid))up=mid;
		else lw=mid;
	}
	return up;
}
//f(lw)=true,f(up)=false
template<class F>
int find_max_true(int lw,int up,F f){
	while(up-lw>1){
		const int mid=(lw+up)/2;
		if(f(mid))lw=mid;
		else up=mid;
	}
	return lw;
}

template<class t> using pqmin=priority_queue<t,vc<t>,greater<t>>;
template<class t> using pqmax=priority_queue<t>;
using T=tuple<int,int,int>;

void slv(){
	INT(n,c);
	VI(a,n);
	soin(a,greater<int>());
	
	vi sum=presum(a);
	
	int h=n/(c-1)+5;
	//todo update library
	VVC(int,dp,n+1,h,inf);
	dp[0][0]=0;
	
	rep(i,n)per(j,h){
		int mx=min(n-i,c);
		per(w,mx+1){
			int k=max<int>(j+1-(mx-w),0);
			if(k<h){
				dmp2(i,j,mx,w,k);
				chmin(dp[i+w][k],dp[i][j]+a[i]+(sum[n]-sum[n-(mx-w)]));
			}
		}
	}
	
	
	int ans=inf;
	rep(j,2)chmin(ans,dp[n][j]);
	print(ans);
}

signed main(signed argc,char*argv[]){
	if(argc>1&&strcmp(argv[1],"D")==0)dbg=true;
	
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	
	if(dbg){
		while(1){
			if(current_run_id%run_batch_size==0){
				cerr<<"Current Run "<<current_run_id<<endl;
			}
			slv();
			current_run_id++;
		}
	}else{
		//int t;cin>>t;rep(_,t)
		slv();
	}
}
accuracy: 1
time_ms: 110
memory_kb: 128440
final_rank: 4

code_snippet: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_set>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i, n) _rep2(i, 0, n)
#define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using i64 = long long;
template<class T, class U>
bool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; }
template<class T, class U>
bool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; }

template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}
template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}
template<typename T>string join(vector<vector<T>>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;}
template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}

int main(){
    i64 n, k;
    cin >> n >> k;
    vector<i64> a(n);
    cin >> a;
    sort(all(a));
    if (k >= n){
        cout << a.back() << endl;
        return 0;
    }
    i64 mxnpt = floor(double(n - 2) / k);
    vector m(n, vector<i64>(mxnpt + 1));
    vector nmds(n, vector<i64>(mxnpt + 1));
    
    auto cost = [&](int i0, int j0){
        i64 t = a[j0 - 1];
        rep(i, 1, i0 + 1){
            t += a[i - 1];
        }
        return t;
    };

    for (i64 i = 2; i <= k; i++){
        m[i - 1][0] = cost(0, i);
        nmds[i - 1][0] = 0;
    }

    for (i64 i = 2; i <= k; i++){
        for (i64 e = 1; e <= mxnpt; e++){
            i64 j = min(e + 1, i);
            i64 minn = cost(j, i) + m[j - 1][e - j + 1];
            nmds[i - 1][e] = j;
            j--;
            while (2 <= j){
                i64 tmp = cost(j, i) + m[j - 1][e - j + 1];
                if (tmp < minn){
                    minn = tmp;
                    nmds[i - 1][e] = j;
                }
                j--;
            }
            m[i - 1][e] = minn;
        }
    }
    for (i64 e = 0; e <= mxnpt; e++){
        i64 minn = cost(1, k + 1) + m[1][e];
        nmds[k][e] = 1;
        i64 tmp, i;
        i = 2;
        while (i <= min(e + 1, k - 1)){
            tmp = cost(i, k + 1) + m[i][e - i + 1];
            if (tmp < minn){
                minn = tmp;
                nmds[k][e] = i;
            }
            i++;
        }
        m[k][e] = minn;
    }

    for (i64 i = k + 2; i <= n; i++){
        i64 mxex = floor(double(n - i) / k);
        for (i64 e = 0; e <= mxex; e++){
            i64 minn = cost(0, i) + m[i - k - 1][e + 1];
            nmds[i - 1][e] = 0;
            i64 tmp;
            for (int j = 1; j <= min(e + 1, k - 1); j++){
                tmp = cost(j, i) + m[i - (k - j) - 1][e - j + 1];
                if (chmin(minn, tmp)){
                    nmds[i - 1][e] = j;
                }
            }
            m[i - 1][e] = minn;
        }
    }
    cout << m[n - 1][0] << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 128556
final_rank: 3

code_snippet: //#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}


ll dp[4005];
void solve() {
	int n, k; cin >> n >> k;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	vector<ll> ra(n + 1);
	sort(all(a));
	rep(i, n)ra[i + 1] = ra[i] + a[i];
	if (n == k||n==1) {
		cout << a.back() << "\n"; return;
	}
	ll ans = INF;
	for (int c = 0; c * k < n - 1; c++) {
		ll sum = 0;
		int cur = n - 1;
		rep(i, c) {
			sum += a[cur];
			cur -= k;
		}
		int r = n - c * k;
		rep(i, c + 1)dp[i] = INF;
		dp[0] = sum;
		if (c == 0) {
			vector<int> b;
			rep(j, r)b.push_back(a[j]);
			while (b.size() > k) {
				sum += b.back() + a[0];
				rep(i, k - 1)b.pop_back();
			}
			sum += b.back();
			ans = min(ans, sum);
			continue;
		}
		rep(i, c) {
			rep1(ad, min(r-1,k - 1)) {
				if (i + ad >= c) {
					{
						ll ssum = dp[i] + ra[c - i + 1] - ra[1] + a[ad] + a[0];
						vector<int> b;
						b.push_back(a[0]);
						int l = c - i;
						int las = ad;
						rep1(j, c - i)b.push_back(a[j]);
						Rep(j, ad + 1, r)b.push_back(a[j]);
						while (b.size() > k) {
							ssum += b.back() + a[0];
							rep(i, k - 1)b.pop_back();
						}
						ssum += b.back();
						ans = min(ans, ssum);
					}
				}
				else {
					dp[i + ad] = min(dp[i + ad], dp[i] + ra[ad + 1] - ra[1] + a[ad] + a[0]);
				}
			}
		}
	}
	for (int c = 0; c * k < n - 1; c++) {
		ll sum = 0;
		int cur = n - 1;
		rep(i, c) {
			sum += a[cur];
			cur -= k;
		}
		int r = n - c * k;
		int sup = min(r - 1, k - 1);
		sum += a[0] + a[sup];
		int l = 1;
		//[l,sup+1)
		rep(i, c) {
			if (l == sup + 1) {
				sum += a[0] + a[sup];
				l = 1;
			}
			sum += a[l];
			l++;
		}
		for (int las = l - 1; las <= sup; las++) {
			ll ssum = sum - a[sup] + a[las];
			//[l,las]
			vector<int> b;
			b.push_back(a[0]);
			rep1(i, l - 1)b.push_back(a[i]);
			Rep(i, las + 1, r)b.push_back(a[i]);
			while (b.size() > k) {
				ssum += b.back() + a[0];
				rep(i, k - 1)b.pop_back();
			}
			ssum += b.back();
			ans = min(ans, ssum);
		}
		/*vector<int> b;
		b.push_back(a[0]);
		rep1(i, l - 1)b.push_back(a[i]);
		Rep(i, sup + 1, r)b.push_back(a[i]);
		while (b.size() > k) {
			sum += b.back() + a[0];
			rep(i, k - 1)b.pop_back();
		}
		sum += b.back();
		ans = min(ans, sum);*/
		//if (c == 1)cout << "? " << sum << "\n";
	}
	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(8);
	init_f();
	//init();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.696969696969697
time_ms: 90
memory_kb: 19916
final_rank: 7

code_snippet: //#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}


ll dp[4005];
void solve() {
	int n, k; cin >> n >> k;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	vector<ll> ra(n + 1);
	sort(all(a));
	rep(i, n)ra[i + 1] = ra[i] + a[i];
	if (n == k||n==1) {
		cout << a.back() << "\n"; return;
	}
	ll ans = INF;
	for (int c = 0; c * k < n - 1; c++) {
		ll sum = 0;
		int cur = n - 1;
		rep(i, c) {
			sum += a[cur];
			cur -= k;
		}
		int r = n - c * k;
		rep(i, c + 1)dp[i] = INF;
		dp[0] = sum;
		if (c == 0) {
			vector<int> b;
			rep(j, r)b.push_back(a[j]);
			while (b.size() > k) {
				sum += b.back() + a[0];
				rep(i, k - 1)b.pop_back();
			}
			sum += b.back();
			ans = min(ans, sum);
			continue;
		}
		rep(i, c) {
			rep1(ad, min(r-1,k - 1)) {
				if (i + ad >= c) {
					{
						ll ssum = dp[i] + ra[c - i + 1] - ra[1] + a[ad] + a[0];
						vector<int> b;
						b.push_back(a[0]);
						int l = c - i;
						int las = ad;
						rep1(j, c - i)b.push_back(a[j]);
						Rep(j, ad + 1, r)b.push_back(a[j]);
						while (b.size() > k) {
							ssum += b.back() + a[0];
							rep(i, k - 1)b.pop_back();
						}
						ssum += b.back();
						ans = min(ans, ssum);
					}
				}
				else {
					dp[i + ad] = min(dp[i + ad], dp[i] + ra[ad + 1] - ra[1] + a[ad] + a[0]);
				}
			}
		}
	}
	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(8);
	init_f();
	//init();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.696969696969697
time_ms: 80
memory_kb: 19892
final_rank: 6

code_snippet: //#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}


ll dp[4005];
ll val[4005][4005];
void solve() {
	int n, k; cin >> n >> k;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	vector<ll> ra(n + 1);
	sort(all(a));
	rep(i, n)ra[i + 1] = ra[i] + a[i];
	if (n == k) {
		cout << a.back() << "\n"; return;
	}
	ll ans = INF;
	for (int c = 0; c * k < n - 1; c++) {
		ll sum = 0;
		int cur = n - 1;
		rep(i, c) {
			sum += a[cur];
			cur -= k;
		}
		int r = n - c * k;
		rep(i, c + 1)dp[i] = INF;
		dp[0] = sum;
		if (c == 0) {
			vector<int> b;
			rep(j, r)b.push_back(a[j]);
			while (b.size() > k) {
				sum += b.back() + a[0];
				rep(i, k - 1)b.pop_back();
			}
			sum += b.back();
			ans = min(ans, sum);
			continue;
		}
		rep(i, c) {
			rep1(ad, min(r-1,k - 1)) {
				if (i + ad >= c) {
					{
						ll ssum = dp[i] + ra[c - i + 1] - ra[1] + a[ad] + a[0];
						vector<int> b;
						b.push_back(a[0]);
						int l = c - i;
						int las = ad;
						rep1(j, c - i)b.push_back(a[j]);
						Rep(j, ad + 1, r)b.push_back(a[j]);
						while (b.size() > k) {
							ssum += b.back() + a[0];
							rep(i, k - 1)b.pop_back();
						}
						ssum += b.back();
						ans = min(ans, ssum);
					}
					{
						if (i + ad > c) {
							ll ssum = dp[i] + ra[c - i + 1] - ra[1] + a[0] + a[r - 1];
							vector<int> b;
							b.push_back(a[0]);
							rep1(j, c - i)b.push_back(a[j]);
							Rep(j, c - i + 1, r - (i + ad - c))b.push_back(a[j]);
							while (b.size() > k) {
								ssum += b.back() + a[0];
								rep(i, k - 1)b.pop_back();
							}
							ssum += b.back();
							ans = min(ans, ssum);
						}

					}
				}
				else {
					dp[i + ad] = min(dp[i + ad], dp[i] + ra[ad + 1] - ra[1] + a[ad] + a[0]);
				}
			}
		}
	}
	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(8);
	init_f();
	//init();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.696969696969697
time_ms: 170
memory_kb: 20340
final_rank: 10

code_snippet: //#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}


ll dp[4005];
ll val[4005][4005];
void solve() {
	int n, k; cin >> n >> k;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	vector<ll> ra(n + 1);
	sort(all(a));
	rep(i, n)ra[i + 1] = ra[i] + a[i];
	if (n == k) {
		cout << a.back() << "\n"; return;
	}
	ll ans = INF;
	for (int c = 0; c * k < n - 1; c++) {
		ll sum = 0;
		int cur = n - 1;
		rep(i, c) {
			sum += a[cur];
			cur -= k;
		}
		int r = n - c * k;
		rep(i, c + 1)dp[i] = INF;
		dp[0] = sum;
		if (c == 0) {
			vector<int> b;
			rep(j, r)b.push_back(a[j]);
			while (b.size() > k) {
				sum += b.back() + a[0];
				rep(i, k - 1)b.pop_back();
			}
			sum += b.back();
			ans = min(ans, sum);
			continue;
		}
		rep(i, c) {
			rep1(ad, min(r-1,k - 1)) {
				if (i + ad >= c) {
					ll ssum = dp[i] + ra[c-i + 1] - ra[1] + a[ad] + a[0];
					vector<int> b;
					b.push_back(a[0]);
					int l = c - i;
					int las = ad;
					rep1(j, c-i)b.push_back(a[j]);
					Rep(j, ad + 1, r)b.push_back(a[j]);
					while (b.size() > k) {
						ssum += b.back() + a[0];
						rep(i, k - 1)b.pop_back();
					}
					ssum += b.back();
					ans = min(ans, ssum);
				}
				else {
					dp[i + ad] = min(dp[i + ad], dp[i] + ra[ad + 1] - ra[1] + a[ad] + a[0]);
				}
			}
		}
	}
	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(8);
	init_f();
	//init();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.696969696969697
time_ms: 100
memory_kb: 20348
final_rank: 9

code_snippet: //#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}


ll dp[4005];
ll val[4005][4005];
void solve() {
	int n, k; cin >> n >> k;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	vector<ll> ra(n + 1);
	sort(all(a));
	rep(i, n)ra[i + 1] = ra[i] + a[i];
	if (n == k) {
		cout << a.back() << "\n"; return;
	}
	ll ans = INF;
	for (int c = 0; c * k < n - 1; c++) {
		ll sum = 0;
		int cur = n - 1;
		rep(i, c) {
			sum += a[cur];
			cur -= k;
		}
		int r = n - c * k;
		rep(i, c + 1)dp[i] = INF;
		dp[0] = sum;
		if (c == 0) {
			vector<int> b;
			rep(j, r)b.push_back(a[j]);
			while (b.size() > k) {
				sum += b.back() + a[0];
				rep(i, k - 1)b.pop_back();
			}
			sum += b.back();
			ans = min(ans, sum);
			continue;
		}
		rep(i, c) {
			rep1(ad, k - 1) {
				if (i + ad >= c) {
					ll ssum = dp[i] + ra[c-i + 1] - ra[1] + a[ad] + a[0];
					vector<int> b;
					b.push_back(a[0]);
					int l = c - i;
					int las = ad;
					rep1(j, c-i)b.push_back(a[j]);
					Rep(j, ad + 1, r)b.push_back(a[j]);
					while (b.size() > k) {
						ssum += b.back() + a[0];
						rep(i, k - 1)b.pop_back();
					}
					ssum += b.back();
					ans = min(ans, ssum);
				}
				else {
					dp[i + ad] = min(dp[i + ad], dp[i] + ra[ad + 1] - ra[1] + a[ad] + a[0]);
				}
			}
		}
	}
	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(8);
	init_f();
	//init();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.696969696969697
time_ms: 90
memory_kb: 20336
final_rank: 8

code_snippet: //#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}


ll dp[4005][4005];
bool used[4005][4005];
void solve() {
	int n, k; cin >> n >> k;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	sort(all(a));
	if (n == k) {
		cout << a.back() << "\n"; return;
	}
	ll ans = INF;
	for (int c = 0; c * k < n - 1; c++) {
		ll sum = 0;
		int cur = n - 1;
		rep(i, c) {
			sum += a[cur];
			cur -= k;
		}
		int r = n - c * k;
		int sup = min(r - 1, k - 1);
		int l = sup + 1;
		//[l,sup+1)
		rep(i, c) {
			if (l == sup + 1) {
				sum += a[0] + a[sup];
				l = 1;
			}
			sum += a[l];
			l++;
		}
		for (int las = l-1; las <= sup; las++) {
			ll ssum = sum - a[sup] + a[las];
			//[l,las]
			vector<int> b;
			b.push_back(a[0]);
			rep1(i, l - 1)b.push_back(a[i]);
			Rep(i, las + 1, r)b.push_back(a[i]);
			while (b.size() > k) {
				ssum += b.back() + a[0];
				rep(i, k - 1)b.pop_back();
			}
			ssum += b.back();
			ans = min(ans, ssum);
		}
		/*vector<int> b;
		b.push_back(a[0]);
		rep1(i, l - 1)b.push_back(a[i]);
		Rep(i, sup + 1, r)b.push_back(a[i]);
		while (b.size() > k) {
			sum += b.back() + a[0];
			rep(i, k - 1)b.pop_back();
		}
		sum += b.back();
		ans = min(ans, sum);*/
		//if (c == 1)cout << "? " << sum << "\n";
	}
	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(8);
	init_f();
	//init();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.6363636363636364
time_ms: 10
memory_kb: 21564
final_rank: 16

code_snippet: //#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}


ll dp[4005][4005];
bool used[4005][4005];
void solve() {
	int n, k; cin >> n >> k;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	sort(all(a));
	if (n == k) {
		cout << a.back() << "\n"; return;
	}
	ll ans = INF;
	for (int c = 0; c * k < n - 1; c++) {
		ll sum = 0;
		int cur = n - 1;
		rep(i, c) {
			sum += a[cur];
			cur -= k;
		}
		int r = n - c * k;
		int sup = min(r - 1, k - 1);
		sum += a[0] + a[sup];
		int l = 1;
		//[l,sup+1)
		rep(i, c) {
			if (l == sup + 1) {
				sum += a[0] + a[sup];
				l = 1;
			}
			sum += a[l];
			l++;
		}
		for (int las = l-1; las <= sup; las++) {
			ll ssum = sum - a[sup] + a[las];
			//[l,las]
			vector<int> b;
			b.push_back(a[0]);
			rep1(i, l - 1)b.push_back(a[i]);
			Rep(i, las + 1, r)b.push_back(a[i]);
			while (b.size() > k) {
				ssum += b.back() + a[0];
				rep(i, k - 1)b.pop_back();
			}
			ssum += b.back();
			ans = min(ans, ssum);
		}
		/*vector<int> b;
		b.push_back(a[0]);
		rep1(i, l - 1)b.push_back(a[i]);
		Rep(i, sup + 1, r)b.push_back(a[i]);
		while (b.size() > k) {
			sum += b.back() + a[0];
			rep(i, k - 1)b.pop_back();
		}
		sum += b.back();
		ans = min(ans, sum);*/
		//if (c == 1)cout << "? " << sum << "\n";
	}
	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(8);
	init_f();
	//init();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.6363636363636364
time_ms: 10
memory_kb: 21620
final_rank: 17

code_snippet: //#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}


void solve() {
	int n, k; cin >> n >> k;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	sort(all(a));
	if (n == k) {
		cout << a.back() << "\n"; return;
	}
	ll ans = INF;
	for (int c = 0; c * k < n; c++) {
		for (int sup = 1; sup <= k - 1; sup++) {
			ll sum = 0;
			int cur = n - 1;
			rep(i, c) {
				sum += a[cur];
				cur -= k;
			}
			int r = n - c * k;
			int l = sup+1;
			//[l,sup+1)
			rep(i, c) {
				if (l == sup + 1) {
					sum += a[0] + a[sup];
					l = 1;
				}
				sum += a[l];
				l++;
			}
			vector<int> b;
			b.push_back(a[0]);
			rep1(i, l - 1)if(i<r)b.push_back(a[i]);
			Rep(i, sup + 1, r)b.push_back(a[i]);
			while (b.size() > k) {
				sum += b.back() + a[0];
				rep(i, k - 1)b.pop_back();
			}
			sum += b.back();
			ans = min(ans, sum);
		}
		
		//if (c == 1)cout << "? " << sum << "\n";
	}
	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(8);
	init_f();
	//init();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.6363636363636364
time_ms: 10
memory_kb: 19880
final_rank: 15

code_snippet: //#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}


void solve() {
	int n, k; cin >> n >> k;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	sort(all(a));
	if (n == k) {
		cout << a.back() << "\n"; return;
	}
	ll ans = INF;
	for (int c = 0; c * k < n-1; c++) {
		for (int sup = 1; sup <= k - 1; sup++) {
			ll sum = 0;
			int cur = n - 1;
			rep(i, c) {
				sum += a[cur];
				cur -= k;
			}
			int r = n - c * k;
			int l = sup+1;
			//[l,sup+1)
			rep(i, c) {
				if (l == sup + 1) {
					sum += a[0] + a[sup];
					l = 1;
				}
				sum += a[l];
				l++;
			}
			vector<int> b;
			b.push_back(a[0]);
			rep1(i, l - 1)if(i<r)b.push_back(a[i]);
			Rep(i, sup + 1, r)b.push_back(a[i]);
			while (b.size() > k) {
				sum += b.back() + a[0];
				rep(i, k - 1)b.pop_back();
			}
			sum += b.back();
			ans = min(ans, sum);
		}
		
		//if (c == 1)cout << "? " << sum << "\n";
	}
	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(8);
	init_f();
	//init();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.6363636363636364
time_ms: 10
memory_kb: 19876
final_rank: 14

code_snippet: //#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}


void solve() {
	int n, k; cin >> n >> k;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	sort(all(a));
	if (n == k) {
		cout << a.back() << "\n"; return;
	}
	ll ans = INF;
	for (int c = 0; c * k < n-1; c++) {
		for (int sup = 1; sup <= k - 1; sup++) {
			ll sum = 0;
			int cur = n - 1;
			rep(i, c) {
				sum += a[cur];
				cur -= k;
			}
			int r = n - c * k;
			int l = sup+1;
			//[l,sup+1)
			rep(i, c) {
				if (l == sup + 1) {
					sum += a[0] + a[sup];
					l = 1;
				}
				sum += a[l];
				l++;
			}
			vector<int> b;
			b.push_back(a[0]);
			rep1(i, l - 1)b.push_back(a[i]);
			Rep(i, sup + 1, r)b.push_back(a[i]);
			while (b.size() > k) {
				sum += b.back() + a[0];
				rep(i, k - 1)b.pop_back();
			}
			sum += b.back();
			ans = min(ans, sum);
		}
		
		//if (c == 1)cout << "? " << sum << "\n";
	}
	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(8);
	init_f();
	//init();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.6363636363636364
time_ms: 10
memory_kb: 19832
final_rank: 13

code_snippet: //#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}


void solve() {
	int n, k; cin >> n >> k;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	sort(all(a));
	if (n == k) {
		cout << a.back() << "\n"; return;
	}
	ll ans = INF;
	for (int c = 0; c * k < n-1; c++) {
		for (int sup = 1; sup <= k - 1; sup++) {
			ll sum = 0;
			int cur = n - 1;
			rep(i, c) {
				sum += a[cur];
				cur -= k;
			}
			int r = n - c * k;
			sum += a[0] + a[sup];
			int l = 1;
			//[l,sup+1)
			rep(i, c) {
				if (l == sup + 1) {
					sum += a[0] + a[sup];
					l = 1;
				}
				sum += a[l];
				l++;
			}
			vector<int> b;
			b.push_back(a[0]);
			rep1(i, l - 1)b.push_back(a[i]);
			Rep(i, sup + 1, r)b.push_back(a[i]);
			while (b.size() > k) {
				sum += b.back() + a[0];
				rep(i, k - 1)b.pop_back();
			}
			sum += b.back();
			ans = min(ans, sum);
		}
		
		//if (c == 1)cout << "? " << sum << "\n";
	}
	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(8);
	init_f();
	//init();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 0.6363636363636364
time_ms: 10
memory_kb: 19768
final_rank: 12

code_snippet: #line 1 "main.cpp"
#include <bits/stdc++.h>

#line 3 "/home/maspy/library/my_template.hpp"

using namespace std;

using ll = long long;
using ld = long double;
using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vc<T>>;
template <class T> using vvvc = vector<vvc<T>>;
template <class T> using vvvvc = vector<vvvc<T>>;
template <class T> using vvvvvc = vector<vvvvc<T>>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size)                                                                                                                                  \
    vector<type> name(size);                                                                                                                                   \
    IN(name)
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w)                                                                                                                                   \
    vector<vector<type>> name(h, vector<type>(w));                                                                                                             \
    IN(name)
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)                                                                                                                         \
    vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define FOR(i, n) for (ll i = 0; (i) < (ll)(n); ++(i))
#define FOR3(i, m, n) for (ll i = (m); (i) < (ll)(n); ++(i))
#define FOR_R(i, n) for (ll i = (ll)(n)-1; (i) >= 0; --(i))
#define FOR3_R(i, m, n) for (ll i = (ll)(n)-1; (i) >= (ll)(m); --(i))
#define FORIN(x, A) for (auto&& x : A)
#define FOR_nCk(s, n, k) \
  for (ll s = (1 << k) - 1, tmp_var = 0; s < (1 << n); \
  tmp_var = s | (s - 1), s = (tmp_var + 1) | (((~tmp_var & -~tmp_var) - 1) >> (__builtin_ctz(s) + 1))) 
#define all(x) x.begin(), x.end()

#define elif else if

#define popcnt __builtin_popcount

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define SUM(v) accumulate(all(v), 0LL)
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))

#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())

template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}

template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
}

#define INT(...) \
  int __VA_ARGS__; \
  IN(__VA_ARGS__)
#define LL(...) \
  ll __VA_ARGS__; \
  IN(__VA_ARGS__)
#define STR(...) \
  string __VA_ARGS__; \
  IN(__VA_ARGS__)
#define CHR(...) \
  char __VA_ARGS__; \
  IN(__VA_ARGS__)
#define DBL(...) \
  long double __VA_ARGS__; \
  IN(__VA_ARGS__)
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(long double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); }
template <class T> void scan(vector<T> &a) {for(auto &i : a) scan(i);}
template <class T> void scan(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
  scan(head);
  IN(tail...);
}

vi s_to_vi(string S, char first_char='a'){
  vi A(S.size());
  FOR(i, S.size()){
    A[i] = S[i] - first_char;
  }
  return A;
}

template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& A) {
  os << A.fi << " " << A.se;
  return os;
}

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& A) {
  for (size_t i = 0; i < A.size(); i++) {
    if(i) os << " ";
    os << A[i];
  }
  return os;
}

void print() {
  cout << "\n";
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  cout << head;
  if (sizeof...(Tail)) cout << " ";
  print(forward<Tail>(tail)...);
}

const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { cout << yesno[t] << endl; }
void no(bool t = 1) { yes(!t); }

template <typename T>
vector<T> cumsum(vector<T> A) {
  ll N = A.size();
  vector<T> B(N + 1);
  B[0] = T(0);
  FOR(i, N) { B[i + 1] = B[i] + A[i]; }
  return B;
}

vi bin_count(vi& A, ll size) {
  vi C(size);
  for (auto&& x : A) {
    ++C[x];
  }
  return C;
}

template <typename T>
vi argsort(vector<T>& A){
  vi ids(A.size());
  iota(all(ids), 0);
  sort(all(ids), [&](ll i, ll j){
    return A[i] < A[j] || (A[i] == A[j] && i < j);
  });
  return ids;
}


template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }
template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }
#line 3 "/home/maspy/library/ds/unionfind.hpp"

struct UnionFind {
  int num;
  int comp;
  vi size, par;
  UnionFind(int n) : num(n), comp(n), size(n, 1), par(n) { iota(par.begin(), par.end(), 0); }
  int find(int x) {
    while (par[x] != x) {
      par[x] = par[par[x]];
      x = par[x];
    }
    return x;
  }

  int operator[](int x) {
    return find(x);
  }


  bool merge(ll x, ll y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      return false;
    }
    comp--;
    if (size[x] < size[y]) swap(x, y);
    size[x] += size[y];
    size[y] = 0;
    par[y] = x;
    return true;
  }

  vi find_all() {
    vi A(num);
    FOR(i, num) A[i] = find(i);
    return A;
  }
};
#line 3 "/home/maspy/library/graph/base.hpp"

template <typename T>
struct Edge {
  int frm, to;
  T cost;
  int id;
  Edge(int a, int b, T c, int d) : frm(a), to(b), cost(c), id(d) {}
};

template <typename T>
struct Graph {
  int N, M;
  using edge_t = Edge<T>;
  vector<edge_t> edges;
  vector<vector<edge_t>> G;
  bool directed;
  Graph(){}
  Graph(int N, bool bl = false) : N(N), M(0), G(N), directed(bl) {}

  void add_edge(int frm, int to, T cost = 1, int i = -1) {
    if(i == -1)i = M;
    auto e = edge_t(frm, to, cost, i);
    edges.eb(e);
    G[frm].eb(e);
    if (!directed) {
      auto e_rev = edge_t(to, frm, cost, i);
      G[to].eb(e_rev);
    }
    ++M;
  }

  void print(bool detail=false) {
    FOR(v, N) {
      cout << v << " :";
      for (auto e : G[v]) {
        if(detail) cout << " (" << e.frm << "," << e.to << "," << e.cost << "," << e.id << ")";
        else cout << " " << e.to;
      }
      cout << "\n";
    }
  }

  int size(){return N;}

  vector<edge_t>& operator[](int v) { return G[v]; }
};
#line 6 "main.cpp"

void solve() {
  LL(N, K);
  VEC(ll, A, N);
  sort(all(A));
  auto Ac = cumsum(A);

  const ll INF = 1LL << 60;
  ll B = (N - 1) / K;
  auto Ar = A;
  reverse(all(Ar));
  vi TOP(B);
  FOR(b, B) TOP[b] = Ar[b * K];
  auto TOPc = cumsum(TOP);
  /*
  下 a >= 1人と、最上位 bK 人を左に残している状態からのコスト DP[a][b]
  */
  vv(ll, DP, N + 1, B + 1, INF);
  FOR3(a, 1, N + 1) FOR(b, B + 1) {
    if (a + b * K > N) break;
    if (a + b * K <= K) {
      DP[a][b] = (b == 0 ? A[a - 1] : TOP[0]);
      continue;
    }
    /*
    最下位 c 人を運ぶ → 全員もどす
    */
    FOR3(c, 2, min(a + 1, K + 1)) {
      if (c - 1 > b) break;
      ll cost = A[c - 1];                 // 最初に行く
      cost += Ac[c];                      // 全員 1 回ずつ戻る
      cost += TOPc[b] - TOPc[b - c + 1];  // K 人ずつ上位を送り込む
      chmin(DP[a][b], DP[a][b - c + 1] + cost);
    }
    /*
    最下位 c 人を運ぶ。下位グループ最上位まわりも運ぶ → c 人もどす
    */
    FOR3(c, 1, min(a, K)) {
      if (c - 1 > b) break;
      ll n = min(K - c, a - c);
      ll cost = A[a - 1];
      cost += Ac[c];
      cost += TOPc[b] - TOPc[b - c + 1];
      chmin(DP[a][b], DP[a - n][b - c + 1] + cost);
    }
  }

  ll ANS = INF;
  FOR(a, N + 1) FOR(b, B + 1) if (a + b * K == N) chmin(ANS, DP[a][b]);
  print(ANS);
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << setprecision(15);

  // auto T = in();
  // FOR(_, T) solve();

  solve();

  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 65908
final_rank: 2

code_snippet: //#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<random>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<utility>
#include<cassert>
#include<fstream>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef long double D;
//typedef complex<D> P;
#define F first
#define S second
const ll MOD=1000000007;
//const ll MOD=998244353;

template<typename T,typename U>istream & operator >> (istream &i,pair<T,U> &A){i>>A.F>>A.S; return i;}
template<typename T>istream & operator >> (istream &i,vector<T> &A){for(auto &I:A){i>>I;} return i;}
template<typename T,typename U>ostream & operator << (ostream &o,const pair<T,U> &A){o<<A.F<<" "<<A.S; return o;}
template<typename T>ostream & operator << (ostream &o,const vector<T> &A){int i=A.size(); for(auto &I:A){o<<I<<(--i?" ":"");} return o;}
template<typename T,typename U>T & chmax(T &a,const U &b){if(a<b){a=b;} return a;}
template<typename T,typename U>T & chmin(T &a,const U &b){if(b<a){a=b;} return a;}

const ll INF=1e15;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N,K;
  cin>>N>>K;
  vector<ll> A(N),sum(N+1,0);
  cin>>A;
  sort(A.begin(),A.end());
  for(int i=1;i<=N;i++){sum[i]=sum[i-1]+A[i-1];}

  vector<vector<ll>> tmp1(K+1,vector<ll>(N+1,INF));
  {
    vector<ll> dp1(N+1,INF);
    dp1[0]=0;
    for(int i=1;i<=K;i++){
      for(int j=0;j+i-1<=N;j++){
        chmin(dp1[j+i-1],dp1[j]+sum[i]+A[i-1]);
      }
      tmp1[i]=dp1;
      for(int j=0;j<=N;j++){
        tmp1[i][j]+=A[i-1];
      }
    }
  }
  ll ans=INF;
  {
    const int SZ=N+3;
    vector<ll> dp(SZ,INF),nx(SZ);
    dp[0]=0;
    for(int i=0;i<(N/K)*2+10;i++){
      for(int j=0;j<SZ;j++){
        int b=i*K-j;
        if(N-K<=b && b<=N && 0<=i-j && i-j<=N){
          chmin(ans,dp[j]+tmp1[N-b][i-j]);
        }
      }
      for(auto &I:nx){I=INF;}
      for(int j=0;j<SZ;j++){
        for(int k=0;k<K;k++){
          int b=i*K-j;
          if(j+k<SZ && 0<=b && b<=N-K){
            chmin(nx[j+k],dp[j]+A[N-b-1]+sum[k]);
          }
        }
      }
      swap(dp,nx);
    }
  }
  cout<<ans<<endl;
  

  return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 128308
final_rank: 5

code_snippet: //#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<random>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<utility>
#include<cassert>
#include<fstream>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef long double D;
//typedef complex<D> P;
#define F first
#define S second
const ll MOD=1000000007;
//const ll MOD=998244353;

template<typename T,typename U>istream & operator >> (istream &i,pair<T,U> &A){i>>A.F>>A.S; return i;}
template<typename T>istream & operator >> (istream &i,vector<T> &A){for(auto &I:A){i>>I;} return i;}
template<typename T,typename U>ostream & operator << (ostream &o,const pair<T,U> &A){o<<A.F<<" "<<A.S; return o;}
template<typename T>ostream & operator << (ostream &o,const vector<T> &A){int i=A.size(); for(auto &I:A){o<<I<<(--i?" ":"");} return o;}
template<typename T,typename U>T & chmax(T &a,const U &b){if(a<b){a=b;} return a;}
template<typename T,typename U>T & chmin(T &a,const U &b){if(b<a){a=b;} return a;}


const ll INF=1e17;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N,K;
  cin>>N>>K;
  vector<ll> A(N);
  cin>>A;

  assert(N<=20);

  vector<ll> dp1(1<<N,INF),dp2(1<<N,INF);
  vector<ll> sum(1<<N,0);
  for(int i=0;i<(1<<N);i++){
    for(int j=0;j<N;j++){
      if(i>>j&1){continue;}
      sum[i|(1<<j)]=max(sum[i],A[j]);
    }
  }
  dp1[0]=0;
  ll all=(1<<N)-1;
  for(int a=0;a<N;a++){
    for(ll i=0;i<1<<N;i++){
      if(__builtin_popcount(i)!=a){continue;}
      ll msk=all^i;
      for(int j=0;j<N;j++){
        if(i>>j&1){continue;}
        chmin(dp1[i],dp2[i|(1<<j)]+A[j]);
      }
      for(int k=all^i;k>0;k=(k-1)&msk){
        if(__builtin_popcount(k)>K){continue;}
        chmin(dp2[i|k],dp1[i]+sum[k]);
      }
    }
  }
  cout<<dp2[all]<<endl;
  

  return 0;
}
accuracy: 0.24242424242424243
time_ms: 320
memory_kb: 6044
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using lli = long long int;
const lli inf = 1e18;

int main(){
    int n,k;
    cin >> n >> k;
    vector<lli> a(n);
    for(lli &i: a){
        cin >> i;
    }
    sort(a.begin(), a.end());
    auto a_csum = a;
    for(int i=0; i<n-1; i++){
        a_csum[i+1] += a_csum[i];
    }

    vector<lli> dp(n+1, inf); // n人まで残っているときの最小コスト
    dp[0] = 0;
    for(int i=1; i<=n; i++){
        // 初期値
        if(i <= k){
            dp[i] = a[i-1];
            continue;
        }
        // 置いて戻る
        dp[i] = min(dp[i], dp[i-(k-1)]+a[i-1]+a[0]);
        // 軽いのを置きに行って、全員戻ってくる
        lli sum_go = 0;
        for(int j=1; j<k; j++){
            if(i-j*k < 0) break;
            sum_go += a[(i-1)-(j-1)*k];
            dp[i] = min(dp[i], a[j]+a_csum[j] +sum_go +dp[i-j*k]);
        }
        // 軽いのを置きに行って、戻ってこない
        for(int j=1; j<k; j++){
            if(j*(k-1) < i-1) continue;
            lli sum_go = 0;
            for(int l=1; l<=j; l++){
                if((i-1)-(l-1)*k < 0) break;
                sum_go += a[(i-1)-(l-1)*k];
                if(l*k < i-(j+1)+l) continue;
                dp[i] = min(dp[i], a[j]+a_csum[l-1] +sum_go);
            }   
        }
    }
    /*
    for(int i=0; i<=n; i++){
        cout << i << " " << dp[i] << endl;
    }
    */
    cout << dp[n] << endl;
    return 0;
}
accuracy: 0.6363636363636364
time_ms: 10
memory_kb: 3448
final_rank: 11

code_snippet: #include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h>
#include <functional>
#include <cassert>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
#define pp pair<int, int>
ll mod = 1000000007;
ll MOD = 998244353;
int inf = 2000001000;
ll INF = 1e18 + 5;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

int main() {
    int n,k;
    cin >> n >> k;
    int r=(n-1)/(k-1); //移動する命綱の回数
    vector<ll> a(n),b(n+1,0);
    rep(i,n) cin >> a[i];
    sort(all(a));
    rep(i,n) b[i+1]=b[i]+a[i];
    if (k>=n){
      cout << a[n-1] << endl;
      return 0;
    }
    ll dp[r][k+1],ep[r][k+1],fp[r][k+1];
    rep(j,r){
      rep(ii,k+1) dp[j][ii]=INF;
      rep(ii,k+1) fp[j][ii]=INF;
    }
    dp[0][0]=0;
    vector<ll> d(r+k+1,INF); //n人の運び人を運ぶのにかかるコストがd[n]
    d[0]=0;
    rep(i,r){
      for (int j=2;j<=k;j++){
        ll f=0;
        f+=b[j]; 
        d[i+j-1]=min(d[i+j-1],d[i]+a[j-1]+f);
      }
    }
    ll ans=INF;
    rep(i,r){
      rep(j,r){
        rep(ii,k+1) ep[j][ii]=fp[j][ii];
      }
      rep(j,r){
        rep(ii,k+1){
          rep(jj,ii+1){
            if (j+ii>=r) continue;
            int who=n-1-(i*k-j); //ボトルネックとなる人は誰か
            if (who>=n) continue;
            if (who-(k-ii)<=ii-1) continue; //運び人と運ばれる人に重複はないか
            ll count=a[who];
            count+=b[ii]; //先に運び人の帰還分も計算
            ep[j+ii][ii]=min(ep[j+ii][ii],dp[j][jj]+count);
          }
        }
      }
      rep(j,r){
        rep(ii,k+1) dp[j][ii]=ep[j][ii];
      }
      rep(j,r){
        rep(ii,k+1){
          int c=n-(k*(i+1)-j);
          int e=(i+1)-j; //運び人は何人足りていないか
          if (c-1<0 || c-1>=n || e<0 || e>=n) continue;
          if (c<=k && c!=0) ans=min(ans,dp[j][ii]+a[c-1]+d[e]); //最後の命綱、および運び人のみで構成された命綱を加算
        }
      }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 200
memory_kb: 3584
final_rank: 1

code_snippet: //#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<random>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<utility>
#include<cassert>
#include<fstream>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef long double D;
//typedef complex<D> P;
#define F first
#define S second
const ll MOD=1000000007;
//const ll MOD=998244353;

template<typename T,typename U>istream & operator >> (istream &i,pair<T,U> &A){i>>A.F>>A.S; return i;}
template<typename T>istream & operator >> (istream &i,vector<T> &A){for(auto &I:A){i>>I;} return i;}
template<typename T,typename U>ostream & operator << (ostream &o,const pair<T,U> &A){o<<A.F<<" "<<A.S; return o;}
template<typename T>ostream & operator << (ostream &o,const vector<T> &A){int i=A.size(); for(auto &I:A){o<<I<<(--i?" ":"");} return o;}
template<typename T,typename U>T & chmax(T &a,const U &b){if(a<b){a=b;} return a;}
template<typename T,typename U>T & chmin(T &a,const U &b){if(b<a){a=b;} return a;}


const ll INF=1e17;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N,K;
  cin>>N>>K;
  vector<ll> A(N);
  cin>>A;

  assert(N<=20);

  vector<ll> dp1(1<<N,INF),dp2(1<<N,INF);
  vector<ll> sum(1<<N,0);
  for(int i=0;i<(1<<N);i++){
    for(int j=0;j<N;j++){
      if(i>>j&1){continue;}
      sum[i|(1<<j)]=max(sum[i],A[j]);
    }
  }
  dp1[0]=0;
  ll all=(1<<N)-1;
  for(int a=0;a<N;a++){
    for(ll i=0;i<1<<N;i++){
      ll msk=all^i;
      for(int j=0;j<N;j++){
        if(i>>j&1){continue;}
        chmin(dp1[i],dp2[i|(1<<j)]+A[j]);
      }
      for(int k=all^i;k>0;k=(k-1)&msk){
        if(__builtin_popcount(k)>K){continue;}
        chmin(dp2[i|k],dp1[i]+sum[k]);
      }
    }
  }
  cout<<dp2[all]<<endl;
  

  return 0;
}
accuracy: 0.24242424242424243
time_ms: 4640
memory_kb: 6240
final_rank: 20

code_snippet: #include <cassert>
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;

template<typename T>
bool chmin(T& x, const T& y){
  if(x <= y) return false;
  x = y;
  return true;
}

template<typename T>
bool chmax(T& x, const T& y){
  if(x > y) return false;
  x = y;
  return true;
}

int main(){
  int n, k;
  cin >> n >> k;
  vector<int> A(n);
  for(int i = 0; i < n; ++i){
    cin >> A[i];
  }
  assert(n < 21);
  vector d(1<<n,0);
  for(int i = 0; i < (1<<n); ++i){
    for(int j = 0; j < n; ++j){
      if(!(i>>j&1)) continue;
      chmax(d[i],A[j]);
    }
  }

  const long long INF = 1e18;
  int mask = (1<<n) - 1;
  vector dp(1<<n,vector(2,INF));
  dp[mask][0] = 0;

  using tup = tuple<long long,int,int>;
  priority_queue<tup,vector<tup>,greater<>> pq;
  pq.emplace(0,mask,0);

  while(pq.size()){
    auto [t,i,j] = pq.top();
    pq.pop();
    for(int b = i; b > 0; b = (b-1)&i){
      if(__builtin_popcount(b) > k){
        continue;
      }
      if(chmin(dp[mask^i^b][j^1],t+d[b])){
        pq.emplace(t+d[b],mask^i^b,j^1);
      }
    }
  }
  cout << dp[mask][1] << endl;
}
accuracy: 0.24242424242424243
time_ms: 1400
memory_kb: 12640
final_rank: 19