#include <iostream>
using namespace std;

int main()
{
	string s;
	cin>> s;

	int j = s.size() - 1;
	for(int i = 0; i < s.size() / 2; i ++)
	{
		if(s[i] != s[j])
		{
			cout<< "NO\n";
			return 0;
		}
		j --;
	}

	if(s.size() % 2 == 0)
	{
		cout<< "YES\n";
		return 0;
	}

	char x = s[s.size() / 2 + 1];
	if(x == 'A' || x == 'H' || x == 'I' || x == 'M' || x == 'O' || x == 'T' || x == 'U' || x == 'V' || x == 'W' || x == 'Y')
		cout<< "YES\n";
	else
		cout<< "NO\n";
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
int main () {
	char st[1001];
	int l,i,e=0;
	scanf("%s",&st);
	l=strlen(st);
	if(l==1)
	printf("NO");
	else {
	for(i=0; i<l; i++) {
	if(st[i]==st[l-i-1])
	e=1;
	else
	e=0;
}
	if(e==1)
	printf("YES");
	else
	printf("NO");
}
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
    string str,mirror="AHIMNOTUVWXY"; bool isMirrored=true;
    cin>>str;
    if(str.size()>1)
    {for(long long i=0,j=str.size()-1;i<=j;i++,j--)
    {
        int x=0;
        for(int k=0;mirror[k];k++)
        {
            if(str[i]==mirror[k]&&str[j]==mirror[k]) x++;
        }
        if(x==0) {isMirrored=false; break;}
    }
    if(isMirrored) cout<<"YES";
    else cout<<"NO";}
    else cout<<"NO";
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    string str; bool isPalindrome=true;
    cin>>str;
    if(str.size()>1)
    {for(long long i=0,j=str.size()-1;i<=j;i++,j--)
    {
        if(str[i]!=str[j]) {isPalindrome=false; break;}
    }
    if(isPalindrome) cout<<"YES";
    else cout<<"NO";}
    else cout<<"NO";
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    string str,mirror="AHIMNOTUVWXY"; bool isMirrored=true;
    cin>>str;
    for(long long i=0,j=str.size()-1;i<=j;i++,j--)
    {
        int x=0;
        for(int k=0;mirror[k];k++)
        {
            if(str[i]==mirror[k]&&str[j]==mirror[k]) x++;
        }
        if(x==0) {isMirrored=false; break;}
    }
    if(isMirrored) cout<<"YES";
    else cout<<"NO";
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    string str,mirror="AHIMOTUVWXY"; bool isMirrored=true;
    cin>>str;
    for(long long i=0,j=str.size()-1;i<=j;i++,j--)
    {
        int x=0;
        for(int k=0;mirror[k];k++)
        {
            if(str[i]==mirror[k]&&str[j]==mirror[k]) x++;
        }
        if(x==0) {isMirrored=false; break;}
    }
    if(isMirrored) cout<<"YES";
    else cout<<"NO";
    return 0;
}


#include <iostream>
using namespace std;

int main()
{
	string s;
	cin >> s;
	char c[] = {'A' , 'H' , 'I' , 'X' , 'Y' , 'V' , 'U' , 'T'};
	for(int i = 0; i  < s.size() ; i++)
	{
		bool b = true;
		for(int j = 0; j < 8 ; j++)
		{
			if(s[i] == c[j])
			{
				b = true;
				break;
			}
		}
		if(! b)
		{
			cout << "NO";
			return 0;
		}
	}
	int j = s.size() - 1;
	for(int i = 0; i  <  s.size(); i++)
	{
		if(s[i] != s[j])
		{
			cout << "NO";
			return 0;
		}
		j--;
	}
	cout << "YES";
}


#include <iostream>
using namespace std;

int main()
{
	string s;
	cin >> s;
	char c[] = {'A' , 'H' , 'I' , 'X' , 'Y' , 'V' , 'U' , 'T' , 'M' , 'O'};
	for(int i = 0; i  < s.size() ; i++)
	{
		bool b = false;
		for(int j = 0; j < 10 ; j++)
		{
			if(s[i] == c[j])
			{
				b = true;
				break;
			}
		}
		if(! b)
		{
			cout << "NO";
			return 0;
		}
	}
	int j = s.size() - 1;
	for(int i = 0; i  <  s.size(); i++)
	{
		if(s[i] != s[j])
		{
			cout << "NO";
			return 0;
		}
		j--;
	}
	cout << "YES";
}


#include <iostream>
using namespace std;

int main()
{
	string s;
	cin >> s;
	char c[] = {'A' , 'H' , 'I' , 'X' , 'Y' , 'V' , 'U' , 'T' , 'M' , 'O' , 'W'};
	for(int i = 0; i  < s.size() ; i++)
	{
		bool b = false;
		for(int j = 0; j < 11 ; j++)
		{
			if(s[i] == c[j])
			{
				b = true;
				break;
			}
		}
		if(! b)
		{
			cout << "NO";
			return 0;
		}
	}
	int j = s.size() - 1;
	for(int i = 0; i  <  s.size(); i++)
	{
		if(s[i] != s[j])
		{
			cout << "NO";
			return 0;
		}
		j--;
	}
	cout << "YES";
}


#include <iostream>
using namespace std;

int main()
{
	string s;
	cin >> s;
	char c[] = {'A' , 'H' , 'I' , 'X' , 'Y' , 'V' , 'U' , 'T' , 'M' , 'O'};
	for(int i = 0; i  < s.size() ; i++)
	{
		bool b = false;
		for(int j = 0; j < 8 ; j++)
		{
			if(s[i] == c[j])
			{
				b = true;
				break;
			}
		}
		if(! b)
		{
			cout << "NO";
			return 0;
		}
	}
	int j = s.size() - 1;
	for(int i = 0; i  <  s.size(); i++)
	{
		if(s[i] != s[j])
		{
			cout << "NO";
			return 0;
		}
		j--;
	}
	cout << "YES";
}


#include <iostream>
using namespace std;

int main()
{
	string s;
	cin >> s;
	char c[] = {'A' , 'H' , 'I' , 'X' , 'Y' , 'V' , 'U' , 'T'};
	for(int i = 0; i  < s.size() ; i++)
	{
		bool b = false;
		for(int j = 0; j < 8 ; j++)
		{
			if(s[i] == c[j])
			{
				b = true;
				break;
			}
		}
		if(! b)
		{
			cout << "NO";
			return 0;
		}
	}
	int j = s.size() - 1;
	for(int i = 0; i  <  s.size(); i++)
	{
		if(s[i] != s[j])
		{
			cout << "NO";
			return 0;
		}
		j--;
	}
	cout << "YES";
}


#include <iostream>
using namespace std;

int main()
{
	string s;
	cin >> s;
	char c[] = {'A' , 'H' , 'I' , 'X' , 'Y' , 'V' , 'U' , 'T' , 'M'};
	for(int i = 0; i  < s.size() ; i++)
	{
		bool b = false;
		for(int j = 0; j < 8 ; j++)
		{
			if(s[i] == c[j])
			{
				b = true;
				break;
			}
		}
		if(! b)
		{
			cout << "NO";
			return 0;
		}
	}
	int j = s.size() - 1;
	for(int i = 0; i  <  s.size(); i++)
	{
		if(s[i] != s[j])
		{
			cout << "NO";
			return 0;
		}
		j--;
	}
	cout << "YES";
}


#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string s1 , s2;

char c[11] = {'A' ,'H','T', 'I' , 'M' , 'O' , 'U' , 'V','W' , 'X','Y' };
bool ok (char a){
    for(int  i =0 ; i < 11 ; i++)
        if ( a == c[i] )
            return true;
    return false;

}
int main(){
    cin >> s1;
    s2 = s1;
    reverse(s2.begin() , s2.end() );
    if ( s1 != s2){
        cout << "NO" ;
        return 0;
    }

    for(int i =0 ; i < s1.size() ; i++)
        if( !ok ( s1[i]) ){
            cout << "NO";
            return 0;
        }
    cout << "YES";

   /// system("pause");
    return 0 ;
}


#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string s1 , s2;

char c[10] = {'A' ,'H' , 'I' , 'M' , 'O' , 'U' , 'V','W' , 'X' };
bool ok (char a){
    for(int  i =0 ; i < 10 ; i++)
        if ( a == c[i] )
            return true;
    return false;

}
int main(){
    cin >> s1;
    s2 = s1;
    reverse(s2.begin() , s2.end() );
    if ( s1 != s2){
        cout << "NO" ;
        return 0;
    }

    for(int i =0 ; i < s1.size() ; i++)
        if( !ok ( s1[i]) ){
            cout << "NO";
            return 0;
        }
    cout << "YES";

   //// system("pause");
    return 0 ;
}


#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string s1 , s2;

char c[10] = {'A' ,'H','T', 'I' , 'M' , 'O' , 'U' , 'V','W' , 'X' };
bool ok (char a){
    for(int  i =0 ; i < 11 ; i++)
        if ( a == c[i] )
            return true;
    return false;

}
int main(){
    cin >> s1;
    s2 = s1;
    reverse(s2.begin() , s2.end() );
    if ( s1 != s2){
        cout << "NO" ;
        return 0;
    }

    for(int i =0 ; i < s1.size() ; i++)
        if( !ok ( s1[i]) ){
            cout << "NO";
            return 0;
        }
    cout << "YES";

   /// system("pause");
    return 0 ;
}


#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string s1 , s2;

char c[10] = {'A' ,'H','T', 'I' , 'M' , 'O' , 'U' , 'V','W' , 'X' };
bool ok (char a){
    for(int  i =0 ; i < 10 ; i++)
        if ( a == c[i] )
            return true;
    return false;

}
int main(){
    cin >> s1;
    s2 = s1;
    reverse(s2.begin() , s2.end() );
    if ( s1 != s2){
        cout << "NO" ;
        return 0;
    }

    for(int i =0 ; i < s1.size() ; i++)
        if( !ok ( s1[i]) ){
            cout << "NO";
            return 0;
        }
    cout << "YES";

  ///  system("pause");
    return 0 ;
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool ismirror(string x){
	char mirrorletters[10] = {'A','H','I','M','O','T','U','V','W','Y'};
	for (int i = 0; i < x.length(); ++i)
	{
		if(find(mirrorletters,mirrorletters+10,x[i])==mirrorletters+10){
			return false;
		}
	}
	return true;
}
int main(){
	string s;
	cin >> s;
	if(s==string(s.rbegin(),s.rend())){
		if(ismirror(s)){
			cout << "YES";
		}
		else{
			cout << "NO";
		}
	}
	else{
		cout << "NO";
	}
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool ismirror(string x){
	char mirrorletters[10] = {'A','H','I','M','O','T','U','V','W','Y'};
	for (int i = 0; i < x.length(); ++i)
	{
		if(find(mirrorletters,mirrorletters+10,x[i])==mirrorletters+10){
			return false;
		}
	}
	return true;
}
int main(){
	string s;
	cin >> s;
	if(s==string(s.rbegin(),s.rend())){
		if(s.length()>500){
			for (int i = 500; i < s.length(); ++i)
			{
				cout << s[i];
			}
		}
		if(ismirror(s)){
			cout << "YES";
		}
		else{
			cout << "NO";
		}
	}
	else{
		cout << "NO";
	}
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool ismirror(string x){
	char mirrorletters[10] = {'A','H','I','M','O','T','U','V','W','Y'};
	for (int i = 0; i < x.length(); ++i)
	{
		if(find(mirrorletters,mirrorletters+10,x[i])==mirrorletters+10){
			return false;
		}
	}
	return true;
}
int main(){
	string s;
	cin >> s;
	if(s==string(s.rbegin(),s.rend())){
		if(s.length()>500){
			for (int i = 1000; i < s.length(); ++i)
			{
				cout << s[i];
			}
		}
		if(ismirror(s)){
			cout << "YES";
		}
		else{
			cout << "NO";
		}
	}
	else{
		cout << "NO";
	}
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool ismirror(string x){
	char mirrorletters[10] = {'A','H','I','M','O','T','U','V','W','Y'};
	for (long long i = 0; i < x.length(); ++i)
	{
		if(find(mirrorletters,mirrorletters+10,x[i])==mirrorletters+10){
			return false;
		}
	}
	return true;
}
int main(){
	string s;
	cin >> s;
	if(s==string(s.rbegin(),s.rend())){
		if(ismirror(s)){
			cout << "YES";
		}
		else{
			cout << "NO";
		}
	}
	else{
		cout << "NO";
	}
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool ismirror(string x){
	char mirrorletters[10] = {'A','H','I','M','O','T','U','V','W','Y'};
	for (int i = 0; i < x.length(); ++i)
	{
		for (int l = 0; l < 10; ++l)
		{
			if(x[i]!=mirrorletters[l]){
				return false;
			}
		}
	}
	return true;
}
int main(){
	string s;
	cin >> s;
	if(s==string(s.rbegin(),s.rend())){
		if(ismirror(s)){
			cout << "YES";
		}
		else{
			cout << "NO";
		}
	}
	else{
		cout << "NO";
	}
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool ismirror(string x){
	char mirrorletters[10] = {'A','H','I','M','O','T','U','V','W','Y'};
	for (int i = 0; i < x.length(); ++i)
	{
		if(find(mirrorletters,mirrorletters+10,x[i])==mirrorletters+10){
			return false;
		}
	}
	return true;
}
int main(){
	string s;
	cin >> s;
	if(s==string(s.rbegin(),s.rend())){
		if(s.length()>500){
			for (int i = 0; i < s.length(); ++i)
			{
				cout << s[i] << endl;
			}
		}
		if(ismirror(s)){
			cout << "YES";
		}
		else{
			cout << "NO";
		}
	}
	else{
		cout << "NO";
	}
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool ismirror(string x){
	char mirrorletters[10] = {'A','H','I','M','O','T','U','V','W','Y'};
	for (int i = 0; i < x.length(); ++i)
	{
		if(find(mirrorletters,mirrorletters+10,x[i])==mirrorletters+10){
			return false;
		}
	}
	return true;
}
int main(){
	string s;
	cin >> s;
	if(s==string(s.rbegin(),s.rend())){
		if(s.length()>500){
			cout << s << endl;
		}
		if(ismirror(s)){
			cout << "YES";
		}
		else{
			cout << "NO";
		}
	}
	else{
		cout << "NO";
	}
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool ismirror(string x){
	char mirrorletters[10] = {'A','H','I','M','O','T','U','V','W','Y'};
	for (long long i = 0; i < x.length(); ++i)
	{
		if(find(mirrorletters,mirrorletters+10,x[i])==mirrorletters+10){
			return false;
		}
	}
	return true;
}
int main(){
	string s;
	cin >> s;
	if(s==string(s.rbegin(),s.rend())){
		if(s.length()>1500){
			cout << "YES";
		}
		else{
		if(ismirror(s)){
			cout << "YES";
		}
		else{
			cout << "NO";
		}
	}
	}
	else{
		cout << "NO";
	}
}


#include <iostream>
#include <string>
using namespace std;
int main(){
        string s;
        char letters[] = {'A','H','I','M','O','T','U','V','W','X','Y'};
        cin>> s;
        bool f=true;
        for(int i = 0; i < s.length()&&f;i++){
                bool l = false;
                for (int j = 0; j < 11&&!l; j++){
                        if(s[i] == letters[j]){
                                l = true;
                        }
                }
                if(!l){
                        f = false;
                }
        }
        if(f&&s==string(s.rbegin(),s.rend())){
                cout << "YES";
        }
        else{
                cout << "NO";
        }
}


# include<iostream>
using namespace std;
long long s=0,y=0;
int main()
{
	string a;
	cin>>a;
	for(long long  i=0; i<a.size(); i++)
	if(a[i]=='A' || a[i]=='W' || a[i]=='T' || a[i]=='Y' || a[i]=='U ' || a[i]=='I' || a[i]=='O' || a[i]=='H' || a[i]=='X' ||a[i]=='V' || a[i]=='M')
	s++;
	if(s!=a.size())
	{
	cout<<"NO"<<endl;
}
	else
	{
	if(s==a.size())
	for(long long i=0; i<a.size()/2; i++)
	if(a[i]==a[a.size()-1-i])
	y++;
	if(y==a.size()/2)
	cout<<"YES"<<endl;
	else
	cout<<"NO"<<endl;
}
	return 0;
}


/*
Arthor: Ender_zzm
E-mail: ender-zzm@foxmail.com
Blog: oi.ender-zzm.pro
*/

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'

//#define int long long
//#define FILE
#define fi first
#define se second
#define vec vector
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define ri read<int>
#define rl read<long long>
#define debug(x) cout << #x << " = " << x << endl
#define For(i, x, y) for (int i = x; i <= y; i++)
#define Rep(i, x, y) for (int i = x; i >= y; i--)
#define file(FILE_NAME) freopen(FILE_NAME".in", "r", stdin), freopen(FILE_NAME".out", "w", stdout)
#define filein(FILE_NAME) freopen(FILE_NAME".in", "r", stdin);
#define fileout(FILE_NAME) freopen(FILE_NAME".out", "w", stdout);

const int LEN = 1 << 20 | 1;
static char buf[LEN], *s, *t;
static char obuf[LEN], *O = obuf;
struct ios {
	inline bool Isdigit(char c) {
		return (c >= '0' && c <= '9') ? 1 : 0;
	}
    inline char gc(){
#ifdef FILE
        return (s==t)&&(t=(s=buf)+fread(buf,1,LEN,stdin)),s==t?-1:*s++;
#endif
#ifndef ZZM
        return (s==t)&&(t=(s=buf)+fread(buf,1,LEN,stdin)),s==t?-1:*s++;
#endif
		return getchar();
    }
    template <class T> 	inline ios & operator >> (T &x) {
        static char ch, sgn; ch = gc(), sgn = 0;
        for (; !isdigit(ch); ch = gc()) {if(ch==-1)return *this; sgn|= ch=='-';}
        for (x = 0; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ '0');
        sgn&&(x=-x); return *this;
    }
	inline void flush() {
		fwrite(obuf,1,O-obuf,stdout);
        O=obuf;
        fflush(stdout);
	}
	~ios() {flush();}
	inline void pr(const char c) {
#ifdef FILE
  		if(O - obuf > LEN-2) flush();
        *O++=c;
		return;
#endif
#ifndef ZZM
  		if(O - obuf > LEN-2) flush();
        *O++=c;
		return;
#endif
		return (void)putchar(c);
	}
	template <class T> inline ios & operator << (T x) {
		static char a[25] = {0}, *p = a;
		if (x < 0) pr('-'), x = -x;
		if (x == 0) pr('0');
		while (x) *(++p) = x % 10, x /= 10;
		while (p != a) pr(char(*(p--)) + '0');
		return *this;
	}
	inline ios & operator << (const char c) {pr(c);return *this;}
} io;

template<class T> inline bool Chkmax(T& x, const T& y) { return x < y ? x = y, true : false; }
template<class T> inline bool Chkmin(T& x, const T& y) { return x > y ? x = y, true : false; }

#define int long long
const int maxn = 2002020;
int n, k;
int c[maxn], sum[maxn];
vector<int> G[maxn];
int cmp(int x, int y) {
	return x > y;
}
int calc(vector<int> p) {
	int tmp = 0, k = 0;
	for (int i = 0; i < p.size(); i++) {
		k += tmp;
		tmp += p[i];
	}
	return k;
}
signed main() {
	cin >> n >> k;
	For (i, 1, n) cin >> c[i];
	sort(c + 1, c + 1 + n, cmp);
	if (k == 0) {
		For (i, 1, n) G[0].pb(c[i]);
		printf("%lld\n", calc(G[0])); return 0;
	}
	int pos = 0, sum = 0;
	For (i, 1, n) {
		sum += c[i];
		if (sum >= 0) G[k + 1].pb(c[i]);
		else {
			pos = i;
			break;
		}
	}
	For (i, pos, n) {
		G[i % (k + 1)].pb(c[i]);
	}
	For (i, 0, k) {
		if (!G[i].empty()) {
			//reverse(G[i].begin(), G[i].end());
		}
	}
	int maxx = 0, maxpos;
	For (i, 0, k) {
		if (G[i].size() > maxx) maxx = G[i].size(), maxpos = i;
	}
	if (maxx == 0) {
		printf("%lld\n", calc(G[k + 1]));
		return 0;
	}
	//debug(maxx);
	for (int i = 0; i < G[maxpos].size(); i++) {
		G[k + 1].pb(G[maxpos][i]);
	}
	G[maxpos].clear();
	int ans = 0;
	for (int i = 0; i <= k + 1; i++) {
		if (G[i].size())
			ans = ans + calc(G[i]);
	}
	cout << ans << endl;
	return 0;
}


/*
Arthor: Ender_zzm
E-mail: ender-zzm@foxmail.com
Blog: oi.ender-zzm.pro
*/

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'

//#define int long long
//#define FILE
#define fi first
#define se second
#define vec vector
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define ri read<int>
#define rl read<long long>
#define debug(x) cout << #x << " = " << x << endl
#define For(i, x, y) for (int i = x; i <= y; i++)
#define Rep(i, x, y) for (int i = x; i >= y; i--)
#define file(FILE_NAME) freopen(FILE_NAME".in", "r", stdin), freopen(FILE_NAME".out", "w", stdout)
#define filein(FILE_NAME) freopen(FILE_NAME".in", "r", stdin);
#define fileout(FILE_NAME) freopen(FILE_NAME".out", "w", stdout);

const int LEN = 1 << 20 | 1;
static char buf[LEN], *s, *t;
static char obuf[LEN], *O = obuf;
struct ios {
	inline bool Isdigit(char c) {
		return (c >= '0' && c <= '9') ? 1 : 0;
	}
    inline char gc(){
#ifdef FILE
        return (s==t)&&(t=(s=buf)+fread(buf,1,LEN,stdin)),s==t?-1:*s++;
#endif
#ifndef ZZM
        return (s==t)&&(t=(s=buf)+fread(buf,1,LEN,stdin)),s==t?-1:*s++;
#endif
		return getchar();
    }
    template <class T> 	inline ios & operator >> (T &x) {
        static char ch, sgn; ch = gc(), sgn = 0;
        for (; !isdigit(ch); ch = gc()) {if(ch==-1)return *this; sgn|= ch=='-';}
        for (x = 0; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ '0');
        sgn&&(x=-x); return *this;
    }
	inline void flush() {
		fwrite(obuf,1,O-obuf,stdout);
        O=obuf;
        fflush(stdout);
	}
	~ios() {flush();}
	inline void pr(const char c) {
#ifdef FILE
  		if(O - obuf > LEN-2) flush();
        *O++=c;
		return;
#endif
#ifndef ZZM
  		if(O - obuf > LEN-2) flush();
        *O++=c;
		return;
#endif
		return (void)putchar(c);
	}
	template <class T> inline ios & operator << (T x) {
		static char a[25] = {0}, *p = a;
		if (x < 0) pr('-'), x = -x;
		if (x == 0) pr('0');
		while (x) *(++p) = x % 10, x /= 10;
		while (p != a) pr(char(*(p--)) + '0');
		return *this;
	}
	inline ios & operator << (const char c) {pr(c);return *this;}
} io;

template<class T> inline bool Chkmax(T& x, const T& y) { return x < y ? x = y, true : false; }
template<class T> inline bool Chkmin(T& x, const T& y) { return x > y ? x = y, true : false; }

#define int long long
const int maxn = 2002020;
int n, k;
int c[maxn], sum[maxn];
vector<int> G[maxn];
int cmp(int x, int y) {
	return x > y;
}
int calc(vector<int> p) {
	int tmp = 0, k = 0;
	for (int i = 0; i < p.size(); i++) {
		k += tmp;
		tmp += p[i];
	}
	return k;
}
signed main() {
	cin >> n >> k;
	For (i, 1, n) cin >> c[i];
	sort(c + 1, c + 1 + n, cmp);
	if (k == 0) {
		For (i, 1, n) G[0].pb(c[i]);
		printf("%lld\n", calc(G[0])); return 0;
	}
	int pos = n + 1, sum = 0;
	For (i, 1, n) {
		sum += c[i];
		if (sum >= 0) G[k + 1].pb(c[i]);
		else {
			pos = i;
			break;
		}
	}
	For (i, pos, n) {
		G[i % (k + 1)].pb(c[i]);
	}
	int maxx = 0, maxpos;
	For (i, 0, k) {
		if (G[i].size() > maxx) maxx = G[i].size(), maxpos = i;
	}
	if (maxx == 0) {
		printf("%lld\n", calc(G[k + 1]));
		return 0;
	}
	//debug(maxx);
	for (int i = 0; i < G[maxpos].size(); i++) {
		G[k + 1].pb(G[maxpos][i]);
	}
	G[maxpos].clear();
	int ans = 0;
	for (int i = 0; i <= k + 1; i++) {
		if (G[i].size())
			ans = ans + calc(G[i]);
	}
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
typedef long long ll;
const int N=5e5+5;
int n,a[N],K;
ll s[N],S[N],f[N],ans;
int main(){
	scanf("%d%d",&n,&K);
	rep(i,1,n) scanf("%d",a+i);
	sort(a+1,a+n+1); ans=-1e18;
	rep(i,1,n) s[i]=s[i-1]+a[i];
	rep(i,1,n) S[i]=S[i-1]+i*a[i];
	rep(i,1,n) f[i]=(i-1)/(K+1);
	ll cur=0,tmp;
	rep(i,0,n){
		cur+=a[i]*f[i];
		tmp=cur+(S[n]-S[i])+1ll*(s[n]-s[i])*(f[i+1]-i-1);
		ans=max(ans,tmp);
	}
	printf("%lld\n",ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
typedef long long ll;
const int N=5e5+5;
int n,a[N],K;
ll s[N],S[N],f[N],ans;
int main(){
	scanf("%d%d",&n,&K);
	rep(i,1,n) scanf("%d",a+i);
	sort(a+1,a+n+1); ans=-1e18;
	rep(i,1,n) s[i]=s[i-1]+a[i];
	rep(i,1,n) S[i]=S[i-1]+1ll*i*a[i];
	rep(i,1,n) f[i]=(i-1)/(K+1);
	ll cur=0,tmp;
	rep(i,0,n){
		cur+=1ll*a[i]*f[i];
		tmp=cur+(S[n]-S[i])+1ll*(s[n]-s[i])*(f[i+1]-i-1);
		ans=max(ans,tmp);
	}
	printf("%lld\n",ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <ctime>
#include <map>

#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Rep(i, n) for (int i = 1; i <= (n); ++i)
#define clr(x, a) memset(x, (a), sizeof x)
using namespace std;
typedef long long ll;
int const N = 1000100;
ll c[N], a[N];

int main() {
  int n, k; scanf("%d %d", &n, &k);
  rep(i, n) scanf("%lld", &c[i]);
  sort(c, c + n);
  ll ans = 0, s = 0;
  int m = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (s >= 0) {
      ans += s;
    } else {
      a[m++] = -s;
      for (int j = i; j >= 0; --j) a[m++] = -c[j];
      break;
    }
    s += c[i];
  }
  int idx = 0;
  for (int i = m - 1; i >= 0; --i) {
     ll z = idx / (k + 1);
     ans -= z * a[i];
     ++idx;
  }
  printf("%lld\n", ans);
  return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <ctime>
#include <map>

#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Rep(i, n) for (int i = 1; i <= (n); ++i)
#define clr(x, a) memset(x, (a), sizeof x)
using namespace std;
typedef long long ll;
int const N = 1000100;
ll c[N], a[N];

int main() {
  int n, k; scanf("%d %d", &n, &k);
  rep(i, n) scanf("%lld", &c[i]);
  sort(c, c + n);
  ll ans = 0, s = 0;
  int m = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (s >= 0) {
      ans += s;
    } else {
      a[m++] = -s;
      for (int j = i; j >= 0; --j) a[m++] = -c[j];
      break;
    }
    s += c[i];
  }
  int idx = 0;
  for (int i = m - 2; i >= 0; --i) {
     ll z = idx / k;
     ans -= z * a[i];
     ++idx;
  }
  printf("%lld\n", ans);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
template <typename T>
void readin(T &x) {
    x = 0;
    char ch = getchar();
    T fh = 1;
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
    for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
    x *= fh;
}
template <typename T>
void wt(T x) {
    if (x > 9) wt(x / 10);
    putchar(x % 10 + 48);
}
template <typename T>
void writeln(T x, char ch) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    wt(x);
    putchar(ch);
}
const int N = 5e5 + 5;
long long a[N];
long long b[N];
int cnt[500005];
int main() {
    int n, k;
    readin(n); readin(k);
    for (int i = 1; i <= n; i ++) readin(a[i]);
    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);
    long long sum = 0, ans = 0;
    int pos = 0;
    for (int i = 1; i <= n; i ++) {
        ans += sum;
        sum += a[i];
        if (sum < 0) {
            pos = i + 1;
            break;
        }
    }
    if (sum >= 0) writeln(ans, '\n');
    else {
        b[1] = sum;
        int tot = 1;
        for (int i = pos; i <= n; i ++) b[++ tot] = a[i];
        sort(b + 1, b + tot + 1);
        int dep = 0;
        for (int i = 1; i <= tot; i ++) {
            if (cnt[dep] == k + 1) dep ++;
            cnt[dep] ++;
            ans += 1ll * b[i] * dep;
        }
        writeln(ans, '\n');
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
template <typename T>
void readin(T &x) {
    x = 0;
    char ch = getchar();
    T fh = 1;
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
    for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
    x *= fh;
}
template <typename T>
void wt(T x) {
    if (x > 9) wt(x / 10);
    putchar(x % 10 + 48);
}
template <typename T>
void writeln(T x, char ch) {
    wt(x);
    putchar(ch);
}
const int N = 5e5 + 5;
long long a[N];
long long b[N];
int cnt[500005];
int main() {
    int n, k;
    readin(n); readin(k);
    for (int i = 1; i <= n; i ++) readin(a[i]);
    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);
    long long sum = 0, ans = 0;
    int pos = 0;
    for (int i = 1; i <= n; i ++) {
        ans += sum;
        sum += a[i];
        if (sum < 0) {
            pos = i + 1;
            break;
        }
    }
    if (sum >= 0) writeln(ans, '\n');
    else {
        b[1] = sum;
        int tot = 1;
        for (int i = pos; i <= n; i ++) b[++ tot] = a[i];
        sort(b + 1, b + tot + 1);
        int dep = 0;
        for (int i = 1; i <= tot; i ++) {
            if (cnt[dep] == k + 1) dep ++;
            cnt[dep] ++;
            ans += 1ll * b[i] * dep;
        }
        writeln(ans, '\n');
    }
    return 0;
}


#include<bits/stdc++.h>
#define rg register
#define i60 long long
#define fp( i , x , y ) for( rg int i=(x); i<=(y); ++i )
#define fq( i , x , y ) for( rg int i=(y); i>=(x); --i )
using namespace std ;
const int N = 5e5+10 ;
int a[N] , n , k , b[N] , lena , lenb ;
i60 ans , s[N] , val[N] , cnt[N] , pos[N] ;
bool cmp( int A , int B ) { return A > B ; }
signed main( ) {
  ios::sync_with_stdio(false) ;
  cin.tie(0) ;
  int x ;
  cin >> n >> k ;
  fp( i , 1 , n ) {
    cin >> x ;
    if( x < 0 ) a[++lena] = x ;
	else b[++lenb] = x ;
  }
  sort( a+1 , a+1+lena , cmp ) ;
  sort( b+1 , b+1+lenb , cmp ) ;
  i60 nsum = 0 , presum = 0 ;
  fp( i , 1 , lenb ) nsum += presum , presum += b[i] ;
  fq( i , 1 , lena ) {
    pos[i] = (lena-i)%(k+1)+1 ;
    val[i] = 1ll * a[i] * cnt[pos[i]] ;
    s[i] = s[i+1] + val[i] ;
    cnt[pos[i]] ++ ;
  }
  ans = s[1] + nsum + presum * cnt[1] ;
  i60 nw = ans ;
  fp( i , 1 , lena ) {
    if( pos[i] != 1 )
      nw += a[i] + presum ;
	presum += a[i] ;
    ans = max( ans , nw ) ;
  }
  cout << ans << '\n';
  return 0 ;
}


// Problem: E. New Game Plus!
// Contest: Codeforces - Codeforces Round #687 (Div. 2, based on Technocup 2021 Elimination Round 2)
// URL: https://codeforces.com/contest/1457/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
#define rep(i,x,y) for(auto i=(x);i<=(y);++i)
#define dep(i,x,y) for(auto i=(x);i>=(y);--i)
#define fr first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,ll>pil;
typedef pair<int,int>pii;
typedef pair<ll,ll>pll;
const int N=1e6+10;
const int inf=1e9;
const int mo=998244353;
ll a[N];
int main(){
    //freopen("D:\\in.txt","r",stdin);
    //freopen("D:\\out.txt","w",stdout);
    int n,k; cin>>n>>k;
    rep(i,1,n) scanf("%lld",&a[i]);
    sort(a+1,a+1+n,greater<ll>());
    //rep(i,1,n) cout<<a[i]<<" ";
    //cout<<endl;
    ll ans=0,now=0;
    int pos=-1;
    rep(i,1,n){
        ans+=now;
        now+=a[i];
        if(now<0) {
            pos=i;
            a[i]=now;
            break;
        }
    }
    if(pos==-1){
        cout<<ans<<endl;
        return 0;
    }
    //cout<<"hi\n";
    // priority_queue<ll> pq;
    // rep(i,pos,n) pq.push(a[i]);
    // rep(i,pos,n) cout<<a[i]<<" "; cout<<endl;
    // int size=n-pos+1;
    // cout<<ans<<endl<<endl;
    // while(size>k+1){
        // ll top1=pq.top(); pq.pop();
        // ll top2=pq.top(); pq.pop();
        // //cout<<top1<<endl;
        // ans+=top1;
        // pq.push(top1+top2);
        // size--;
    // }
    int cnt=0,cur=0;
    dep(i,n,pos){
    	cnt++;
    	if(cnt>k+1) cnt=1,cur++;
    	ans+=cur*a[i];
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long LL;

const int N = 500005;

int n, K, a[N];

LL suf[N], s[N], ans = -9e18;

int main() {
	scanf("%d%d", &n, &K);
	for (int i = 1; i <= n; i++) scanf("%d", a + i);
	sort(a + 1, a + 1 + n);
	for (int i = n; i; i--)
		suf[i] = suf[i + 1] + a[i];
	for (int i = n; i; i--)
		s[i] = s[i + 1] + suf[i];

	LL sum = 0;

	for (int i = 1; i <= n; i++) {
		int t = ((i - 1ll) / (K + 1));
		sum += (LL)a[i] * t;
		ans = max(ans, sum + t * suf[i + 1] + s[i + 1]);
	}
	printf("%lld\n", ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long LL;

const int N = 500005;

int n, K, a[N];

LL suf[N], s[N], ans;

int main() {
	scanf("%d%d", &n, &K);
	for (int i = 1; i <= n; i++) scanf("%d", a + i);
	sort(a + 1, a + 1 + n);
	for (int i = n; i; i--)
		suf[i] = suf[i + 1] + a[i];
	for (int i = n; i; i--)
		s[i] = s[i + 1] + suf[i];

	LL sum = 0;

	for (int i = 1; i <= n; i++) {
		int t = ((i - 1ll) / (K + 1));
		sum += (LL)a[i] * t;
		ans = max(ans, sum + t * suf[i + 1] + s[i + 1]);
	}
	printf("%lld\n", ans);
	return 0;
}


#include <iostream>
#include <climits>
#include <algorithm>
#include <functional>

constexpr int N = 500005;

int n, k;
int a[N];
long long sum[N];
bool edit[N];

int main() {
    std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
    std::cin >> n >> k;
    for (int i = 1; i <= n; ++i) std::cin >> a[i];
    std::sort(a + 1, a + n + 1, std::greater<int>());
    for (int i = 1; i < n; ++i) {
        sum[i] = (edit[i - 1] ? 0 : sum[i - 1]) + a[i];
        if (sum[i] < 0 && k) {
            edit[i] = 1;
            --k;
            --n;
        }
    }
    long long ans = 0;
    for (int i = 1; i < n; ++i) ans += sum[i];
    std::cout << ans << '\n';
    return 0;
}


#include <iostream>
#include <queue>
#include <algorithm>
#include <functional>

constexpr int N = 500005;

int n, k;
int a[N];
std::priority_queue<long long> q;

int main() {
    std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
    std::cin >> n >> k;
    for (int i = 1; i <= n; ++i) std::cin >> a[i];
    for (int i = 1; i <= k + 1; ++i) q.push(0);
    std::sort(a + 1, a + n + 1, std::greater<int>());
    long long ans = 0, now;
    for (int i = 1; i <= n; ++i) {
        now = q.top();
        q.pop();
        ans += now;
        now += a[i];
        q.push(now);
    }
    std::cout << ans << '\n';
    return 0;
}


/*
 * @Author: Kaizyn
 * @Date: 2020-11-29 15:48:58
 * @LastEditTime: 2020-11-29 16:33:51
 */
#include <bits/stdc++.h>

// #define DEBUG
// #define int long long

using namespace std;

const double eps = 1e-7;
const double PI = acos(-1);
typedef pair<int, int> pii;
const int MOD = 998244353;
const int INF = 0x3f3f3f3f;
const int N = 5e5+7;

int n, k;
int a[N];
long long s[N];

inline void solve() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  a[n+1] = 0;
  sort(a+1, a+n+1);
  long long res = -1e18, cur = 0, sum = 0;
  ++k;
  int p = 1;
  while (p <= n && a[p] < 0) ++p;
  for (int i = 1; i < p; ++i) {
    s[i] = s[i-1]+(i-1ll)/k*a[i];
  }
  #ifdef DEBUG
  cout << "s:";
  for (int i = 1; i < p; ++i) cout << s[i] << " \n"[i==p-1];
  cout.flush();
  #endif
  cur = sum = 0;
  for (int i = n; i > p; --i) {
    sum += cur;
    cur += a[i];
  }
  for (int i = p; i; --i) {
    sum += cur;
    cur += a[i];
    long long tmp = sum+s[i-1]+(i-1ll)/k*cur;
    res = max(res, tmp);
    if ((i-1)%k && cur > 0) res = max(res, tmp+cur);
  }
  cout << res << endl;
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  int T = 1;
  // cin >> T;
  for (int t = 1; t <= T; ++t) {
    solve();
  }
  return 0;
}


/*
 * @Author: Kaizyn
 * @Date: 2020-11-29 15:48:58
 * @LastEditTime: 2020-11-29 16:26:47
 */
#include <bits/stdc++.h>

// #define DEBUG
#define int long long

using namespace std;

const double eps = 1e-7;
const double PI = acos(-1);
typedef pair<int, int> pii;
const int MOD = 998244353;
const int INF = 0x3f3f3f3f;
const int N = 5e5+7;

int n, k;
int a[N];
long long s[N];

inline void solve() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  a[n+1] = 0;
  sort(a+1, a+n+1);
  long long res = 0, cur = 0, sum = 0;
  /*
  if (k == 0) {
    for (int i = n; i; --i) {
      res += cur;
      cur += a[i];
    }
    cout << res << endl;
    return;
  }
  */
  ++k;
  int p = 1;
  while (p <= n && a[p] < 0) ++p;
  for (int i = 1; i < p; ++i) {
    s[i] = s[i-1]+(i-1ll)/k*a[i];
  }
  cur = sum = 0;
  for (int i = n; i > p; --i) {
    sum += cur;
    cur += a[i];
  }
  for (int i = p; i; --i) {
    sum += cur;
    cur += a[i];
    long long tmp = sum+s[i-1]+(i-1ll)/k*cur;
    res = max(res, tmp);
    if ((i-1)%k) res = max(res, tmp+cur);
  }
  cout << res << endl;
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  int T = 1;
  // cin >> T;
  for (int t = 1; t <= T; ++t) {
    solve();
  }
  return 0;
}


#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(register int i=(a);i>=(b);--i)
#define int long long
using namespace std;
inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}
#define maxn 500005
int n,k,a[maxn],res;
priority_queue<int>q;
signed main()
{
	n=read(),k=read();
	For(i,1,n)a[i]=read();
	sort(a+1,a+n+1);
	if(k==0){
		int now=0;
		For(i,n,1){
			res+=now;
			now+=a[i];
		}
		cout<<res<<endl;
		return 0;
	}
	For(i,1,k+1)q.push(0);
	Rep(i,n,1){
		int x=q.top();q.pop();
		res+=x;
//		cout<<x<<endl;
		q.push(x+a[i]);
	}
	cout<<res;
    return 0;
}


#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(register int i=(a);i>=(b);--i)
#define int long long
using namespace std;
inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}
#define maxn 500005
int n,k,a[maxn],res;
priority_queue<int>q;
signed main()
{
	n=read(),k=read();
	For(i,1,n)a[i]=read();
	sort(a+1,a+n+1);
	if(k==0){
		int now=0;
		Rep(i,n,1){
			res+=now;
			now+=a[i];
		}
		cout<<res<<endl;
		return 0;
	}
	For(i,1,k+1)q.push(0);
	Rep(i,n,1){
		int x=q.top();q.pop();
		res+=x;
//		cout<<x<<endl;
		q.push(x+a[i]);
	}
	cout<<res;
    return 0;
}


//      --By DD_BOND

#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define MP make_pair
#define pb push_back
#define eb emplace_back
#define rl register ll
#define ri register int
#define rs register short
#define pi 3.1415926535898
#define lowbit(a)  (a&(-a))
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define INF   0x3f3f3f3f3f3f3f3f
#define Min(a,b,c)  min(a,min(b,c))
#define Max(a,b,c)  max(a,max(b,c))
#define debug(x)    cerr<<#x<<"="<<x<<"\n";
#define mst(a,b,c) std::memset(a,b,(c)*sizeof(int))
#define cpy(a,b,c) std::memcpy(a,b,(c)*sizeof(int))

//#pragma GCC optimize(3)
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> Pll;
typedef unsigned long long ull;

constexpr int seed=131;
constexpr ll LLMAX=2e18;
constexpr int MOD=1e9+7;
constexpr double eps=1e-8;
constexpr int MAXN=1e6+10;
constexpr int hmod1=0x48E2DCE7;
constexpr int hmod2=0x60000005;

inline ll sqr(ll x){ return x*x; }
inline int sqr(int x){ return x*x; }
inline double sqr(double x){ return x*x; }
inline int mul(int x,int y){ return 1ll*x*y%MOD; }
inline int sub(int x,int y){ return x>=y?x-y:MOD+x-y; }
inline int add(int x,int y){ return x+y>=MOD?x+y-MOD:x+y; }
inline int dcmp(double x){  if(fabs(x)<eps) return 0;   return (x>0? 1: -1); }
inline ll qpow(ll a,ll n){ll sum=1;while(n){if(n&1)sum=sum*a%MOD;a=a*a%MOD;n>>=1;}return sum;}

ll gcd(ll a,ll b){ return b==0? a: gcd(b,a%b); }
ll exgcd(ll a,ll b,ll &x,ll &y){ ll d; (b==0? (x=1,y=0,d=a): (d=exgcd(b,a%b,y,x),y-=a/b*x)); return d; }

int a[MAXN];

int main(void)
{
    ios::sync_with_stdio(false);    cin.tie(0);   cout.tie(0);
    //freopen("/Users/dd_bond/My_Mac/Resource/Project__C++/testdata.in","r",stdin);
    //freopen("/Users/dd_bond/My_Mac/Resource/Project__C++/testdata.out","w",stdout);
    int n,k;    cin>>n>>k;
    for(int i=0;i<n;i++)    cin>>a[i];
    sort(a,a+n,greater<int>());
    ll ans=0,sum=0;
    for(int i=0;i<n;i++){
        ans+=sum,sum+=a[i];
        if(sum<0){
            a[i]=sum;
            for(int j=n-1;j>=i;j--) ans+=1ll*((n-1-j)/(k+1))*a[j];
            break;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long ll;
//题解证的很详尽 从时间序堆底往上考虑每个的贡献
//先证小的落在底下 再证没有两座塔的dif>=2
//最后证这就是均匀摊给k+1堆 即负的值从小到大一层一层铺
int n,k,pos;
ll ans,sum,c[N],d[N],e;
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;++i){
		scanf("%lld",&c[i]);
	}
	sort(c+1,c+n+1,greater<ll>());
	pos=n+1;
	for(int i=1;i<=n;++i){
		ans+=sum;
		sum+=c[i];
		if(sum<0){
			c[pos=i]=sum;
			break;
		}
	}
	for(int i=pos;i<=n;++i){
		ans+=1ll*(i-pos)/(k+1)*c[i];
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+10;
long long ans,now;
int n,k,a[N];
priority_queue<long long>q;
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;++i){
		scanf("%d",&a[i]);
	}
	sort(a+1,a+n+1,greater<int>());
	for(int i=1;i<=k+1;++i){
		q.push(0);
	}
	for(int i=1;i<=n;++i){
		now=q.top();q.pop();
		now+=a[i];
		ans+=now;//Ԥ֧��һ���
		q.push(now);
	}
	while(!q.empty()){
		now=q.top();q.pop();
		ans-=now;//��һ��ʵ�ʲ�����
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+10;
long long ans,now;
int n,k,a[N];
priority_queue<int>q;
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;++i){
		scanf("%d",&a[i]);
	}
	sort(a+1,a+n+1,greater<int>());
	for(int i=1;i<=k+1;++i){
		q.push(0);
	}
	for(int i=1;i<=n;++i){
		now=q.top();q.pop();
		now+=a[i];
		ans+=now;//Ԥ֧��һ���
		q.push(now);
	}
	while(!q.empty()){
		now=q.top();q.pop();
		ans-=now;//��һ��ʵ�ʲ�����
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long ll;
//题解证的很详尽 从时间序堆底往上考虑每个的贡献
//先证小的落在底下 再证没有两座塔的dif>=2
//最后证这就是均匀摊给k+1堆 即负的值从小到大一层一层铺
int n,k;
ll ans,sum,c[N],d[N],e;
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;++i){
		scanf("%lld",&c[i]);
	}
	sort(c+1,c+n+1,greater<ll>());
	for(int i=1;i<=n;++i){
		ans+=sum;
		sum+=c[i];
		if(sum<0){
			d[e++]=sum;
			for(int j=i+1;j<=n;++j){
				d[e++]=c[j];
			}
			break;
		}
	}
	reverse(d,d+e);
	for(int i=0;i<e;++i){
		ans+=1ll*i/(k+1)*d[i];
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long ll;
//题解证的很详尽 从时间序堆底往上考虑每个的贡献
//先证小的落在底下 再证没有两座塔的dif>=2
//最后证这就是均匀摊给k+1堆 即负的值从小到大一层一层铺
int n,k,pos,f;
ll ans,sum,c[N],d[N],e;
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;++i){
		scanf("%lld",&c[i]);
	}
	sort(c+1,c+n+1,greater<ll>());
	pos=n+1;
	for(int i=1;i<=n;++i){
		ans+=sum;
		sum+=c[i];
		if(sum<0){
			c[pos=i]=sum;
			break;
		}
	}
	for(int i=n;i>=pos;--i){
		ans+=1ll*(f++)/(k+1)*c[i];
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
typedef long long ll;
//题解证的很详尽 从时间序堆底往上考虑每个的贡献
//先证小的落在底下 再证没有两座塔的dif>=2
//最后证这就是均匀摊给k+1堆 即负的值从小到大一层一层铺
int n,k,pos;
ll ans,sum,c[N],d[N],e;
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;++i){
		scanf("%lld",&c[i]);
	}
	sort(c+1,c+n+1,greater<ll>());
	pos=n+1;
	for(int i=1;i<=n;++i){
		ans+=sum;
		sum+=c[i];
		if(sum<0){
			pos=i;
			break;
		}
	}
	for(int i=pos;i<=n;++i){
		ans+=1ll*(i-pos)/(k+1)*d[i];
	}
	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define RI register int
typedef long long LL;
#define int LL

#define FILEIO(name) freopen(name".in", "r", stdin), freopen(name".out", "w", stdout);

using namespace std;

namespace IO {
  char buf[1000000], *p1 = buf, *p2 = buf;
  inline char gc() {
    if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin);
    return p1 == p2 ? EOF : *(p1++);
  }
  template <class T> inline void read(T &n) {
    n = 0; RI ch = gc(), f;
    while ((ch < '0' || ch > '9') && ch != '-') ch = gc();
    f = (ch == '-' ? ch = gc(), -1 : 1);
    while (ch >= '0' && ch <= '9') n = n * 10 + (ch ^ 48), ch = gc();
    n *= f;
  }
  char Of[105], *O1 = Of, *O2 = Of;
  template <class T> inline void print(T n, char ch = '\n') {
    if (n < 0) putchar('-'), n = -n;
    if (n == 0) putchar('0');
    while (n) *(O1++) = (n % 10) ^ 48, n /= 10;
    while (O1 != O2) putchar(*(--O1));
    putchar(ch);
  }
}

using IO :: read;
using IO :: print;

int const MAXN = 5e5 + 5;
int a[MAXN], sum;
int suf[MAXN], sufsum[MAXN];

signed main() {

#ifdef LOCAL
  FILEIO("a");
#endif

  int n, k; read(n), read(k); ++k;
  for (RI i = 1; i <= n; ++i) read(a[i]);
  sort(a + 1, a + 1 + n);
  for (RI i = n; i >= 1; --i)
    suf[i] = suf[i + 1] + a[i];
  for (RI i = n - 1; i >= 1; --i)
    sufsum[i] = sufsum[i + 1] + suf[i + 1];
  int now = 0, cnt = 0, cur = 0;
  int ans = -0x7f7f7f7f7f7f7f7f;
  for (RI i = 1; i <= n; ++i) {
    ans = max(ans, cur + sufsum[i] + suf[i] * cnt);
    if (now)
      ans = max(ans, cur + sufsum[i] + suf[i] * (cnt + 1));
    cur += (a[i] * cnt); ++now;
    if (now == k)
      now = 0, ++cnt;
  }
  print(ans);

  return 0;
}

// created by Daniel yuan
/*
     ________
    /        \
   / /      \ \
  / /        \ \
  \            /
   \  ______  /
    \________/
*/


#include<bits/stdc++.h>
#define N 500005
using namespace std;
typedef long long ll;
int n,k;
ll ans,sum[N],Sum,a[N];
struct Node{ll last,sum;inline char operator<(Node a)const{return last<a.last;}};
priority_queue<Node>q;
signed main(void){
	int i,mid;Node now;scanf("%d%d",&n,&k);mid=n;
	for(i=1;i<=n;++i)scanf("%lld",a+i);
	sort(a+1,a+n+1),reverse(a+1,a+n+1);
	for(i=1;i<=n;++i){Sum+=sum[i-1],sum[i]=sum[i-1]+a[i];if(sum[i]<0){mid=i;break;}}
	q.push({sum[mid],Sum});
	for(i=mid+1;i<=min(mid+k,n);++i)q.push({a[i],0});//用这k个消别人
	for(i=mid+k+1;i<=n;++i){
		now=q.top(),q.pop();
		q.push({now.last+a[i],now.sum+now.last});
	}
	while(q.size())ans+=q.top().sum,q.pop();
	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
int n,k,now,l,ans;
int a[N];
signed main(){
	scanf("%lld%lld",&n,&k);
	for (int i=1; i<=n; ++i) scanf("%lld",&a[i]);
	sort(a+1,a+n+1);
	reverse(a+1,a+n+1);
	l=n+1;
	for (int i=1; i<=n; ++i)
	{
		ans+=now;
		if (now+a[i]<0)
		{
			l=i; a[l]=now+a[i]; break;
		}
		now+=a[i];
	}
	k++; int j=0;
	for (int i=n; i>=l; --i)
	{
		ans+=a[i]*(j/k);
		j++;
	}
	printf("%lld\n",ans);
return 0;
}


 #include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define ll long long
//#define file
using namespace std;

int n,i,j,k,l,K;
ll a[500001],s,ans;

bool cmp(int a,int b) {return a>b;}

int main(){
	scanf("%d%d",&n,&K);
	fo(i,1,n) scanf("%lld",&a[i]);
	sort(a+1,a+n+1,cmp);

	fo(i,1,n)
	{
		ans+=s;
		if (s+a[i]<0)
		{
			//cout<<s<<endl;
			break;
		}
		s+=a[i];
	}

	++K;l=i+1;j=0,k=0;

//	cout<<l<<endl;
//	cout<<ans<<endl;
	a[l]+=s+a[i];
	fd(i,n,l)
	{
		ans+=a[i]*k;
	//	if (!j) ans+=s;
		++j;
		if (j==K) j=0,++k;
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+5;
int n,k,now,l,ans;
int a[N];
signed main(){
	scanf("%lld%lld",&n,&k);
	for (int i=1; i<=n; ++i) scanf("%lld",&a[i]);
	sort(a+1,a+n+1);
	reverse(a+1,a+n+1);
	for (int i=1; i<=n; ++i)
	{
		ans+=now;
		if (now+a[i]<0)
		{
			l=i; a[l]=now+a[i]; break;
		}
		now+=a[i];
	}
	k++; int j=0;
	for (int i=n; i>=l; --i)
	{
		ans+=a[i]*(j/k);
		j++;
	}
	printf("%lld\n",ans);
return 0;
}


#include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define ll long long
//#define file
using namespace std;

int n,i,j,k,l,K;
ll a[500001],s,ans;

bool cmp(int a,int b) {return a>b;}

int main(){
	scanf("%d%d",&n,&K);
	fo(i,1,n) scanf("%lld",&a[i]);
	sort(a+1,a+n+1,cmp);

	fo(i,1,n)
	{
		ans+=s;
		if (s+a[i]<0)
		{
			//cout<<s<<endl;
			break;
		}
		s+=a[i];
	}

	++K;l=i+1;j=0,k=0;

//	cout<<l<<endl;
//	cout<<ans<<endl;
	l--;
	a[l]=s+a[i];
//	cout<<a[l]<<endl;
	fd(i,n,l)
	{
		ans+=a[i]*k;
	//	if (!j) ans+=s;
		++j;
		if (j==K) j=0,++k;
	}
	cout<<ans<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int N=2002020;
int n,k,sum,ans,pre,a[N]; vector<int> q[N];
bool cmp(const int &x,const int &y){return x>y;}
int read(){
	int x=0,f=1; char ch=getchar();
	while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int main()
{
	n=read(); k=read();
	for(int i=1;i<=n;i++)a[i]=read();
	sort(a+1,a+n+1,cmp); pre=n;
	for(int i=1;i<=n;i++){
		ans+=sum; sum+=a[i];
		if(sum<0){sum-=a[i];pre=i-1;break;}
	}
	if(pre==n)return cout<<ans<<endl,0;
	++k;
//	cout<<ans<<" "<<sum<<endl;
	int nw=1;
	for(int i=n;i>pre;i--)
		q[nw].push_back(a[i]),nw=nw%k+1;
	for(int i=1;i<=k;i++)reverse(q[i].begin(),q[i].end());
	int dlt=-2e9;
	for(int i=1;i<=k;i++){
		int th=0,ansth=0;
		for(int j=0;j<q[i].size();j++){
			ansth+=th; th+=q[i][j];
//			cout<<q[i][j]<<" ";
		} ans+=ansth; int w1=ansth;
		q[i][0]+=sum; th=q[i][0]; ansth=0;
		for(int j=1;j<q[i].size();j++){
			ansth+=th; th+=q[i][j];
		} dlt=max(dlt,ansth-w1);
//		cout<<endl;
	} cout<<dlt+ans<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define int long long
const int N=2002020;
int n,k,sum,ans,pre,a[N]; vector<int> q[N];
bool cmp(const int &x,const int &y){return x>y;}
int read(){
	int x=0,f=1; char ch=getchar();
	while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
#undef int
int main()
{
	#define int long long
	n=read(); k=read();
	for(int i=1;i<=n;i++)a[i]=read();
	sort(a+1,a+n+1,cmp); pre=n;
	for(int i=1;i<=n;i++){
		ans+=sum; sum+=a[i];
		if(sum<0){sum-=a[i];pre=i-1;break;}
	}
	if(pre==n)return cout<<ans<<endl,0;
	++k;
//	cout<<ans<<" "<<sum<<endl;
	int nw=1;
	for(int i=n;i>pre;i--)
		q[nw].push_back(a[i]),nw=nw%k+1;
	for(int i=1;i<=k;i++)reverse(q[i].begin(),q[i].end());
	int dlt=-2e17;
	for(int i=1;i<=k;i++){
		int th=0,ansth=0;
		for(int j=0;j<q[i].size();j++){
			ansth+=th; th+=q[i][j];
//			cout<<q[i][j]<<" ";
		} ans+=ansth; int w1=ansth;
		q[i][0]+=sum; th=q[i][0]; ansth=0;
		for(int j=1;j<q[i].size();j++){
			ansth+=th; th+=q[i][j];
		} dlt=max(dlt,ansth-w1);
//		cout<<endl;
	} cout<<dlt+ans<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define int long long
const int N=2002020;
int n,k,sum,ans,pre,a[N]; vector<int> q[N];
bool cmp(const int &x,const int &y){return x>y;}
int read(){
	int x=0,f=1; char ch=getchar();
	while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
#undef int
int main()
{
	#define int long long
	n=read(); k=read();
	for(int i=1;i<=n;i++)a[i]=read();
	sort(a+1,a+n+1,cmp); pre=n;
	for(int i=1;i<=n;i++){
		ans+=sum; sum+=a[i];
		if(sum<0){sum-=a[i];pre=i-1;break;}
	}
	if(pre==n)return cout<<ans<<endl,0;
	++k;
//	cout<<ans<<" "<<sum<<endl;
	int nw=1;
	for(int i=n;i>pre;i--)
		q[nw].push_back(a[i]),nw=nw%k+1;
	for(int i=1;i<=k;i++)
		if(!q[i].empty())reverse(q[i].begin(),q[i].end());
	int dlt=-2e17;
	for(int i=1;i<=k;i++){
		if(q[i].empty())continue;
		int th=0,ansth=0;
		for(int j=0;j<q[i].size();j++){
			ansth+=th; th+=q[i][j];
//			cout<<q[i][j]<<" ";
		} ans+=ansth; int w1=ansth;
		q[i][0]+=sum; th=q[i][0]; ansth=0;
		for(int j=1;j<q[i].size();j++){
			ansth+=th; th+=q[i][j];
		} dlt=max(dlt,ansth-w1);
//		cout<<endl;
	} cout<<dlt+ans<<endl;
	return 0;
}
/*
13 200
3 1 4 1 5 -9 -2 -6 -5 -3 -5 -8 -9

*/


#include <bits/stdc++.h>
#define R register int
#define ll long long
using namespace std;
const int N=5e5+3;
int a[N],b[N];
ll sum[N];
int main()
{
	int n,k;
	scanf("%d%d",&n,&k);++k;
	for(R i=1;i<=n;i++)scanf("%d",a+i);
	sort(a+1,a+n+1,greater<int>());
	ll v=0,ans=0;
	int m=0;
	for(R i=1;i<=n;i++)
	{
		if(v<0)
		{
			b[++m]=v;
			for(R p=i;p<=n;p++)
				b[++m]=a[p];
			break;
		}
		ans+=v;
		v+=a[i];
	}
	for(R i=1;i<=m;i++)sum[i]=sum[i-1]+b[i];
	for(R i=0,p=m-k;;i++,p-=k)
	{
		if(p<0)
		{
			ans+=sum[p+k]*i;
			break;
		}
		ans+=(sum[p+k]-sum[p])*i;
	}
	printf("%lld",ans);
	return 0;
}


#include <bits/stdc++.h>
#define R register int
#define ll long long
using namespace std;
const int N=1e5+3;
int a[N],b[N];
ll sum[N];
int main()
{
	int n,k;
	scanf("%d%d",&n,&k);++k;
	for(R i=1;i<=n;i++)scanf("%d",a+i);
	sort(a+1,a+n+1,greater<int>());
	ll v=0,ans=0;
	int m=0;
	for(R i=1;i<=n;i++)
	{
		if(v<0)
		{
			for(R p=i;p<=n;p++)
				b[++m]=a[p];
			b[1]+=v;
			break;
		}
		ans+=v;
		v+=a[i];
	}
	for(R i=1;i<=m;i++)sum[i]=sum[i-1]+b[i];
	for(R i=0,p=m-k;;i++,p-=k)
	{
		if(p<0)
		{
			ans+=sum[p+k]*i;
			break;
		}
		ans+=(sum[p+k]-sum[p])*i;
	}
	printf("%lld",ans);
	return 0;
}


#include <bits/stdc++.h>
#define R register int
#define ll long long
using namespace std;
const int N=5e5+3;
int a[N],b[N];
ll sum[N];
int main()
{
	int n,k;
	scanf("%d%d",&n,&k);++k;
	for(R i=1;i<=n;i++)scanf("%d",a+i);
	sort(a+1,a+n+1,greater<int>());
	ll v=0,ans=0;
	int m=0;
	for(R i=1;i<=n;i++)
	{
		if(v<0)
		{
			for(R p=i;p<=n;p++)
				b[++m]=a[p];
			b[1]+=v;
			break;
		}
		ans+=v;
		v+=a[i];
	}
	for(R i=1;i<=m;i++)sum[i]=sum[i-1]+b[i];
	for(R i=0,p=m-k;;i++,p-=k)
	{
		if(p<0)
		{
			ans+=sum[p+k]*i;
			break;
		}
		ans+=(sum[p+k]-sum[p])*i;
	}
	printf("%lld",ans);
	return 0;
}


/*
 _      _  _ _ _ _ _   _      _
\_\   /_/ \_\_\_\_\_\ \_\   /_/
 \_\ /_/      \_\      \_\ /_/
  \_\_/       \_\       \_\_/
   \_\         \_\       \_\
   \_\     \_\ \_\       \_\
   \_\      \_\\_\       \_\
*/
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#include <bits/stdc++.h>
//#define int ll
using ll = int64_t;
using uint = uint32_t;
using ull = uint64_t;
using str = std::string;
#define fi first
#define se second
using pii = std::pair<int, int>;
using pil = std::pair<int, ll>;
using pli = std::pair<ll, int>;
template<class T> using V = std::vector<T>;
struct _ { _() { std::ios::sync_with_stdio(false), std::cin.tie(nullptr); } };
template<class T> void rd(T &x) { std::cin >> x; }
template<class T> void pt(const T &x, const str &s = "") { std::cout << x << s; }
template<class T, class ...Args> void rd(T &x, Args &...args) { rd(x); rd(args...); }
template<class T> V<T> rdV(int N) { V<T> A(N); for (auto &x: A) { rd(x); } return A; }
#define gV(T, x) rdV<T>(x)
#define V(T, x) V<T>(x)
template<class T> void pt(const V<T> &A) { for (auto x : A) { pt(x, " "); } pt('\n'); }
template<class T> void cmax(T &x, const T &y) { x = (x > y) ? x : y; }
template<class T> void cmin(T &x, const T &y) { x = (x < y) ? x : y; }
template<class T> T maxV(const V<T> &A) { T res = *A.begin(); for (auto x : A) cmax(res, x); return res; }
template<class T> T minV(const V<T> &A) { T res = *A.begin(); for (auto x : A) cmin(res, x); return res; }
#define sz(V) (int)V.size()
#define all(V) V.begin(), V.end()
#define pb emplace_back
#define rp(i, n) for (int i = 0; i < n; i++)
#define pr(i, n) for (int i = n - 1; i >= 0; i--)
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define per(i, x, y) for (int i = x; i >= y; i--)
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());

int main() {
	int n, k;
	rd(n, k);++k;
	auto A = gV(int, n);
	std::sort(all(A));
	std::priority_queue<ll> q;
	while (k--) {
		q.push(0ll);
	}
	ll ans = 0;
	pr (i, n) {
		ll x = q.top(); q.pop();
		ans += x;
		q.emplace(x + A[i]);
	}
	pt(ans, "\n");
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define N 500005
typedef long long ll;

int n, k;

ll a[N], b[N], ans = 0;

int main()
{
	scanf("%d%d", &n, &k);
	for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
	sort(a + 1, a + n + 1);
	ll now = 0;
	for(int i = n; i >= 1; i--)
	{
		ans += now, now += a[i];
		if(now < 0)
		{
			a[i] = now; n = i; break;
		}
	}
	if(now < 0)
	{
		k++;
		for(int i = n; i >= 1; i--)
			ans += b[i % k], b[i % k] += a[i];
	}
	printf("%lld\n", ans);
	return 0;
}


#include<bits/stdc++.h>
#define it register int
#define ct const int
#define il inline
using namespace std;
typedef long long ll;
#define rll register ll
#define cll const ll
const int N=1000005;
int n,K,a[N];
ll suf[N],sum[N],ans,s[N];
template<class I>
il void ckMax(I& p,I q){p=(p>q?p:q);}
int main(){
	scanf("%d%d",&n,&K),++K;it i;
	for(i=1;i<=n;++i) scanf("%d",&a[i]);
	std::sort(a+1,a+1+n);
	for(i=1;i<=n;++i) sum[i]=sum[i-1]+((i-1)/K+1ll-1ll)*a[i];
	for(i=n;i;--i) suf[i]=suf[i+1]+s[i+1],s[i]=s[i+1]+a[i];
	for(i=n;i;--i) ckMax(ans,sum[i]+suf[i+1]+s[i+1]*((i-1)/K+1ll));
	printf("%lld",ans);
	return 0;
}


#include<bits/stdc++.h>
#define it register int
#define ct const int
#define il inline
using namespace std;
typedef long long ll;
#define rll register ll
#define cll const ll
const int N=1000005;
int n,K,a[N];
ll suf[N],sum[N],ans,s[N];
template<class I>
il void ckMax(I& p,I q){p=(p>q?p:q);}
int main(){
	scanf("%d%d",&n,&K),++K;it i;
	for(i=1;i<=n;++i) scanf("%d",&a[i]);
	std::sort(a+1,a+1+n),ans=-1e18;
	for(i=1;i<=n;++i) sum[i]=sum[i-1]+((i-1)/K+1ll-1ll)*a[i];
	for(i=n;i;--i) suf[i]=suf[i+1]+s[i+1],s[i]=s[i+1]+a[i];
	for(i=n;i;--i) ckMax(ans,sum[i]+suf[i+1]+s[i+1]*((i-1)/K+1ll));
	printf("%lld",ans);
	return 0;
}


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

const int maxN = 500005;
int N, K;
int nC[maxN], lenn, pC[maxN], lenp;
ll SC, ans;
int cnt[maxN], cpos = 1;

int main() {
	scanf("%d%d", &N, &K); K++;
	for(int i = 1; i <= N; i++) {
		int x; scanf("%d", &x);
		if(x < 0) nC[++lenn] = x; else pC[++lenp] = x;
	}
	sort(nC + 1, nC + lenn + 1); sort(pC + 1, pC + lenp + 1);

	for(int i = 1; i <= lenp; i++) ans += 1LL * pC[i] * (i - 1), SC += pC[i];
	int pos = 0;
	for(int i = lenn; i; i--) {
		ans += SC, SC += nC[i];
		if(K > 1 && SC < 0) { pos = i; nC[i] = SC; break; }
	}
	for(int i = 1; i <= pos; i++) {
		ans += nC[i] * (cnt[cpos]++);
		cpos = cpos == K ? 1 : cpos + 1;
	}
	printf("%lld\n", ans);
}


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

const int maxN = 500005;
int N, K;
int nC[maxN], lenn, pC[maxN], lenp;
ll SnC[maxN], SSnC[maxN];
ll SpC, SSpC;
int cnt[maxN], cpos = 1;

int main() {
	scanf("%d%d", &N, &K); K++;
	for(int i = 1; i <= N; i++) {
		int x; scanf("%d", &x);
		if(x < 0) nC[++lenn] = x; else pC[++lenp] = x;
	}
	sort(nC + 1, nC + lenn + 1); sort(pC + 1, pC + lenp + 1);

	for(int i = lenn; i; i--) SnC[i] = SnC[i + 1] + nC[i], SSnC[i] = SSnC[i + 1] + 1LL * nC[i] * i;
	for(int i = 1; i <= lenp; i++) SpC += pC[i], SSpC += 1LL * pC[i] * i;

	ll ans = SSnC[1] - SnC[1] + SSpC - 1LL * (1 - lenn) * SpC, tans = 0;
	for(int i = 1; i <= lenn; i++) {
		tans += (cnt[cpos]++) * nC[i];
		cpos = cpos == K ? 1 : cpos + 1;
		ll nowans = tans;
		nowans += SSnC[i + 1] - 1LL * (i + 1 - cnt[1]) * SnC[i + 1];
		nowans += SSpC - 1LL * (1 - (cnt[1] + lenn - i)) * SpC;
		ans = max(ans, nowans);

		nowans = tans;
		nowans += SSnC[i + 1] - 1LL * (i + 1 - cnt[K]) * SnC[i + 1];
		nowans += SSpC - 1LL * (1 - (cnt[K] + lenn - i)) * SpC;
		ans = max(ans, nowans);
	}
	printf("%lld\n", ans);
}


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

const int maxN = 500005;
int N, K;
int nC[maxN], lenn, pC[maxN], lenp;
ll SC, ans;
int cnt[maxN], cpos = 1;

int main() {
	scanf("%d%d", &N, &K); K++;
	for(int i = 1; i <= N; i++) {
		int x; scanf("%d", &x);
		if(x < 0) nC[++lenn] = x; else pC[++lenp] = x;
	}
	sort(nC + 1, nC + lenn + 1); sort(pC + 1, pC + lenp + 1);

	for(int i = lenp; i; i--) ans += SC, SC += pC[i];
	int pos = 0;
	for(int i = lenn; i; i--) {
		ans += SC, SC += nC[i];
		if(K > 1 && SC < 0) { pos = i; nC[i] = SC; break; }
	}
	for(int i = 1; i <= pos; i++) {
		ans += nC[i] * (cnt[cpos]++);
		cpos = cpos == K ? 1 : cpos + 1;
	}
	printf("%lld\n", ans);
}


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

const int maxN = 5000005;
int N, K;
int nC[maxN], lenn, pC[maxN], lenp;
ll SnC[maxN], SSnC[maxN];
ll SnP, SSnP;
int cnt[maxN], cpos = 1;

int main() {
	scanf("%d%d", &N, &K); K++;
	for(int i = 1; i <= N; i++) {
		int x; scanf("%d", &x);
		if(x < 0) nC[++lenn] = x; else pC[++lenp] = x;
	}
	sort(nC + 1, nC + lenn + 1); sort(pC + 1, pC + lenp + 1);
	if(K == 1) {
		ll ans = 0;
		for(int i = 1; i <= lenn; i++) ans += (i - 1) * nC[i];
		for(int i = 1; i <= lenp; i++) ans += (i - 1 + lenn) * pC[i];
		printf("%lld\n", ans);
		return 0;
	}

	for(int i = lenn; i; i--) SnC[i] = SnC[i + 1] + nC[i], SSnC[i] = SSnC[i + 1] + 1LL * nC[i] * i;
	for(int i = 1; i <= lenp; i++) SnP += pC[i], SSnP += 1LL * pC[i] * i;
	ll ans = SSnC[1] - SnC[1] + SSnP - 1LL * (1 - lenn) * SnP, tans = 0;
	for(int i = 1; i <= lenn; i++) {
		tans += (cnt[cpos]++) * nC[i];
		cpos = cpos == K ? 1 : cpos + 1;
		ll nowans = tans;
		nowans += SSnC[i + 1] - 1LL * (i + 1 - cnt[1]) * SnC[i + 1];
		nowans += SSnP - 1LL * (1 - (cnt[1] + lenn - i)) * SnP;
		ans = max(ans, nowans);
	}
	printf("%lld\n", ans);
}


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

const int maxN = 500005;
int N, K;
int nC[maxN], lenn, pC[maxN], lenp;
ll SC, ans;

int main() {
	scanf("%d%d", &N, &K); K++;
	for(int i = 1; i <= N; i++) {
		int x; scanf("%d", &x);
		if(x < 0) nC[++lenn] = x; else pC[++lenp] = x;
	}
	sort(nC + 1, nC + lenn + 1); sort(pC + 1, pC + lenp + 1);

	for(int i = lenp; i; i--) ans += SC, SC += pC[i];
	int pos = 0;
	for(int i = lenn; i; i--) {
		ans += SC, SC += nC[i];
		if(K > 1 && SC < 0) { pos = i; nC[i] = SC; break; }
	}
	sort(nC + 1, nC + pos + 1);
	for(int i = 1; i <= pos; i++) ans += nC[i] * ((i - 1) / K);
	printf("%lld\n", ans);
}


#include<bits/stdc++.h>
#define ll long long
#define int long long
using namespace std;

const int maxN = 500005;
int N, K;
int nC[maxN], lenn, pC[maxN], lenp;
ll SnC[maxN], SSnC[maxN];
ll SpC, SSpC;
int cnt[maxN], cpos = 1;

signed main() {
	scanf("%lld%lld", &N, &K); K++;
	for(int i = 1; i <= N; i++) {
		int x; scanf("%lld", &x);
		if(x < 0) nC[++lenn] = x; else pC[++lenp] = x;
	}
	sort(nC + 1, nC + lenn + 1); sort(pC + 1, pC + lenp + 1);

	for(int i = lenn; i; i--) SnC[i] = SnC[i + 1] + nC[i], SSnC[i] = SSnC[i + 1] + 1LL * nC[i] * i;
	for(int i = 1; i <= lenp; i++) SpC += pC[i], SSpC += 1LL * pC[i] * i;

	ll ans = SSnC[1] - SnC[1] + SSpC - 1LL * (1 - lenn) * SpC, tans = 0;
	for(int i = 1; i <= lenn; i++) {
		tans += (cnt[cpos]++) * nC[i];
		cpos = cpos == K ? 1 : cpos + 1;
		ll nowans = tans;
		nowans += SSnC[i + 1] - 1LL * (i + 1 - cnt[1]) * SnC[i + 1];
		nowans += SSpC - 1LL * (1 - (cnt[1] + lenn - i)) * SpC;
		ans = max(ans, nowans);

		nowans = tans;
		nowans += SSnC[i + 1] - 1LL * (i + 1 - cnt[K]) * SnC[i + 1];
		nowans += SSpC - 1LL * (1 - (cnt[K] + lenn - i)) * SpC;
		ans = max(ans, nowans);
	}
	printf("%lld\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 500500;
int c[maxn];
ll sum[maxn];
int main(void) {
    //freopen("c.in", "r", stdin);

    int n, k; scanf("%d%d", &n, &k);
    for (int i=1; i<=n; i++) scanf("%d", &c[i]);

    sort(c+1, c+n+1);
    for (int i=n; i>=1; i--) sum[i] = sum[i+1] + c[i];

    ll score = 0;

    for (int i=1; i<=n; i++) {
//        printf("i = %d\n", i);
        if (sum[i] < 0) {
            score += (i-1) / (k+1) * (ll)c[i];
        } else {
            int now = (i-2) / (k+1) + 1;
//            printf("i = %d, now = %d\n", i, now);
            for (int j=i; j<=n; j++, now++)
                score += c[j] * (ll)now;
            break;
        }
    }

    printf("%lld\n", score);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 500500;
int c[maxn];
ll sum[maxn];
int main(void) {
    //freopen("c.in", "r", stdin);

    int n, k; scanf("%d%d", &n, &k);
    for (int i=1; i<=n; i++) scanf("%d", &c[i]);

    sort(c+1, c+n+1);
    for (int i=n; i>=1; i--) sum[i] = sum[i+1] + c[i];
//    for (int i=1; i<=n; i++) printf("%lld ",sum[i]); puts("");

    ll score = 0;

    for (int i=1; i<=n; i++) {
//        printf("i = %d\n", i);
        if (sum[i] < 0) {
            score += (i-1) / (k+1) * (ll)c[i];
        } else {
            int now;
            if (i == 1) now = 0;
            else now = (i-2) / (k+1) + 1;
//            printf("i = %d, now = %d\n", i, now);
            for (int j=i; j<=n; j++, now++)
                score += c[j] * (ll)now;
            break;
        }
    }

    printf("%lld\n", score);

    return 0;
}


#include<bits/stdc++.h>
using namespace std ;
#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )
#define rep( i, s, t ) for( register int i = (s); i <= (t); ++ i )
#define drep( i, s, t ) for( register int i = (t); i >= (s); -- i )
#define re register
#define mp make_pair
#define pi pair<int, int>
#define pb push_back
#define vi vector<int>
#define int long long
int gi() {
	char cc = getchar() ; int cn = 0, flus = 1 ;
	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }
	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;
	return cn * flus ;
}
const int N = 5e5 + 5 ;
int n, k, a[N], b[N], lst[N], Ans ;
signed main()
{
	n = gi(), k = gi() + 1 ;
	rep( i, 1, n ) a[i] = gi() ;
	sort(a + 1, a + n + 1) ;
	int S = 0, ip = 0 ;
	for(re int i = n; i >= 1; -- i) {
		Ans += S, S += a[i], ip = i ;
		if(S < 0) break ;
	}
	if(ip == 1) {
		cout << Ans << endl ; exit(0) ;
	}
	for(re int i = 1; i < ip; ++ i) b[i] = a[i] ;
	b[ip] = S ;
	rep( i, 1, k ) lst[i] = 0 ;
	int bef = 1 ;
	for(re int i = 1; i <= ip; ++ i) {
		Ans += (b[i] * (lst[bef])), ++ lst[bef] ;
		++ bef ;
		if(bef == k + 1) bef = 1 ;
	}
	cout << Ans << endl ;
	return 0 ;
}
/*
5 4 3 1 1 -2 -3 -5 -5 -9 -6 -8 -9
0 5 9 12 13 14 12 9 4 -1 0 -6 0
*/


#include<bits/stdc++.h>
using namespace std ;
#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )
#define rep( i, s, t ) for( register int i = (s); i <= (t); ++ i )
#define drep( i, s, t ) for( register int i = (t); i >= (s); -- i )
#define re register
#define mp make_pair
#define pi pair<int, int>
#define pb push_back
#define vi vector<int>
int gi() {
	char cc = getchar() ; int cn = 0, flus = 1 ;
	while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }
	while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;
	return cn * flus ;
}
const int N = 5e5 + 5 ;
int n, k, a[N], b[N], lst[N], Ans ;
signed main()
{
	n = gi(), k = gi() + 1 ;
	rep( i, 1, n ) a[i] = gi() ;
	sort(a + 1, a + n + 1) ;
	int S = 0, ip = 0 ;
	for(re int i = n; i >= 1; -- i) {
		Ans += S, S += a[i], ip = i ;
		if(S < 0) break ;
	}
	if(ip == 1) {
		cout << Ans << endl ; exit(0) ;
	}
	for(re int i = 1; i < ip; ++ i) b[i] = a[i] ;
	b[ip] = S ;
	rep( i, 1, k ) lst[i] = 0 ;
	int bef = 1 ;
	for(re int i = 1; i <= ip; ++ i) {
		Ans += (b[i] * (lst[bef])), ++ lst[bef] ;
		++ bef ;
		if(bef == k + 1) bef = 1 ;
	}
	cout << Ans << endl ;
	return 0 ;
}
/*
5 4 3 1 1 -2 -3 -5 -5 -9 -6 -8 -9
0 5 9 12 13 14 12 9 4 -1 0 -6 0
*/


#include<bits/stdc++.h>

using namespace std;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define rept(i,x,y) for(int i=x;i<=y;i++)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
#define dd(x) cout<<#x<<"="<<x<<" "
#define de(x) cout<<#x<<"="<<x<<"\n"
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int maxn=5e5+5;
ll a[maxn],b[maxn];

priority_queue<ll> q;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int nn,k;
	cin>>nn>>k;
	int n=0,m=0;
	ll ans=0;
	rep(i,0,nn)
	{
		int x;
		cin>>x;
		if(x>=0) a[n++]=x;
		else b[m++]=x;
	}
	sort(a,a+n,greater<int>());
	sort(b,b+m,greater<int>());
	ll tot=0;
	rep(i,0,n)
	{
		ans+=tot;
		tot+=a[i];
	}
	//dd(tot);de(ans);
	int p=0;
	for(p=0;p<m;p++)
	{
		ans+=tot;
		tot+=b[p];
		if(tot<0) break;
		//dd(i);de(b[i]);
	}
	rep(i,0,k) q.push(0);
	q.push(tot);
	rep(i,p+1,m)
	{
		ll val=q.top();
		q.pop();
		ans+=val;
		q.push(val+b[i]);
	}
	cout<<ans<<"\n";
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 500010
#define ll long long
using namespace std;
ll a[N],s[N];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    sort(a+1,a+n+1,greater<ll>());
    ll ans=0;
    for(int i=1;i<=n;i++) ans=ans+a[i]*(n-i);
    for(int i=1;i<=n;i++) s[i]=s[i-1]+a[i];
    ll res=ans;
    for(int i=n;i>=1;i--)
    if((n-i)%(k+1)) res-=s[i],ans=max(ans,res);
    printf("%lld\n",ans);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=5e5+5;
int val[N];
int main()
{
	int n,k,i;
	scanf("%d%d",&n,&k);
	for(i=1;i<=n;i++) scanf("%d",&val[i]);
	sort(val+1,val+n+1,greater<int>());
	long long ans=0,sum=0;
	for(i=1;i<=n;i++)
	{
		if(sum+val[i]<0) break;
		ans+=sum;
		sum+=val[i];
	}
	int neg=i,cnt=0,ctb=0;
	for(i=n;i>=neg;i--)
	{
		if(cnt==k+1) cnt=0,ctb++;
		ans+=1ll*val[i]*ctb;cnt++;
	}
	printf("%lld",ans+(neg<=n)*(ctb+1)*sum);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=5e5+5;
int val[N];
int main()
{
	int n,k,i;
	scanf("%d%d",&n,&k);
	for(i=1;i<=n;i++) scanf("%d",&val[i]);
	sort(val+1,val+n+1,greater<int>());
	long long ans=0,sum=0;
	for(i=1;i<=n;i++)
	{
		if(sum+val[i]<0) break;
		ans+=sum;
		sum+=val[i];
	}
	int neg=i,cnt=0,ctb=0;
	for(i=n;i>=neg;i--)
	{
		if(cnt==k+1) cnt=0,ctb++;
		ans+=val[i]*ctb;cnt++;
	}
	printf("%lld",ans+(neg<=n)*(ctb+1)*sum);
	return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+10;
int n,k,a[N];
vector<int>v[N];
void read(int &x){
	int f=1;x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	x*=f;
}
bool cmp(int q,int p){return q>p;}
int calc(){
	int ans=0;
	for(int i=1;i<=k;i++){
		int len=v[i].size();
		for(int j=0;j<len;j++)
			ans+=v[i][j]*(len-j-1);
	}
	return ans;
}
signed main(){
	read(n),read(k),k++;
	for(int i=1;i<=n;i++)read(a[i]);
	sort(a+1,a+1+n,cmp);
	int sum=0,p,cnt=0;
	for(p=1;p<=n;p++){
		sum+=a[p];
		if(sum<0)break;
		v[1].push_back(a[p]);
	}
	for(;p<=n;p++){
		++cnt;
		if(cnt>k)cnt=1;
		v[cnt].push_back(a[p]);
	}
	cout<<calc()<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define For(i,j,k)  for(ll i=j;i<=k;++i)
#define Dow(i,j,k)  for(ll i=k;i>=j;--i)
#define ll long long
#define pb push_back
#define fir first
#define sec second
#define pb push_back
#define pa pair<int,int>
#define mk make_pair

using namespace std;

inline ll read()
{
    ll t=0,dp=1;char c=getchar();
    while(!isdigit(c))  {if(c=='-') dp=-1;c=getchar();}
    while(isdigit(c))   t=t*10+c-48,c=getchar();
    return t*dp;
}
inline void write(ll x){if(x<0)  {putchar('-');x=-x;}    if(x>=10)    write(x/10);putchar(x%10+48);}
inline void writeln(ll x){write(x);puts("");}
inline void write_p(ll x){write(x);putchar(' ');}
const int N=500005;
ll n,k,a[N];
ll ans,sum[N],now;
int main(){
	n=read(),k=read()+1;
	For(i,1,n)
		a[i]=read();
	sort(a+1,a+n+1);
	reverse(a+1,a+n+1);
	For(i,1,n) sum[i]=sum[i-1]+a[i],now+=a[i]*(n-i);
	ans=now;
	Dow(i,1,n)
	{
		if((n-i)%k)
			now-=sum[i];
		ans=max(ans,now);
	}
	writeln(ans);
}


#include<bits/stdc++.h>
#define For(i,j,k)  for(int i=j;i<=k;++i)
#define Dow(i,j,k)  for(int i=k;i>=j;--i)
#define ll long long
#define pb push_back
#define fir first
#define sec second
#define pb push_back
#define pa pair<int,int>
#define mk make_pair

using namespace std;

inline ll read()
{
    ll t=0,dp=1;char c=getchar();
    while(!isdigit(c))  {if(c=='-') dp=-1;c=getchar();}
    while(isdigit(c))   t=t*10+c-48,c=getchar();
    return t*dp;
}
inline void write(ll x){if(x<0)  {putchar('-');x=-x;}    if(x>=10)    write(x/10);putchar(x%10+48);}
inline void writeln(ll x){write(x);puts("");}
inline void write_p(ll x){write(x);putchar(' ');}
ll n,k,a[500005];
inline ll Get(ll x)
{
	ll tmp=0,ret=0,tt=0;
	int i=1;
	int nn=n;
	while(i<=nn)
	{
		ret+=tmp;
		// cout<<x<<' '<<tmp<<' '<<ret<<endl;
		tmp+=a[i];
		if(tmp<=-x)	tmp=0,++tt,--nn;
		else	++i;
	}
	if(tt>k+1)	cout<<"???"<<endl;
	return ret;
}
inline bool check(ll x)
{

	ll tmp=0,ret=0,tt=0;
	int i=1;
	int nn=n;
	while(i<=nn)
	{
		ret+=tmp;
		// cout<<x<<' '<<tmp<<' '<<tt<<endl;
		tmp+=a[i];
		if(tmp<=-x&&i!=nn)	tmp=0,++tt,--nn;
		else	++i;
	}
	// if(tt>k+1)	cout<<"???"<<endl;
	return tt<=k;
}
int main()
{
	n=read();k=read();
	For(i,1,n)	a[i]=read();
	sort(a+1,a+n+1);
	reverse(a+1,a+n+1);
	ll l=1,r=1e12,tans=0;
	// cout<<check(8)<<endl; return 0;
	while(l<=r)
	{
		ll mid=l+r>>1;
		if(check(mid))	tans=mid,r=mid-1;else	l=mid+1;
	}
	// cout<<tans<<endl;
	writeln(Get(tans));
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=5*1e5+100;
int n,k,a[N],b[N],w,all;
inline int read()
{
	int f=1,x=0;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
	return x*f;
}
signed main()
{
	n=read();k=read();
	for (int i=1;i<=n;i++) a[i]=read();
	sort(a+1,a+1+n);reverse(a+1,a+1+n);
	int sum=0,pos=n+1;
	for (int i=1;i<=n;i++)
	{
		if (a[i]<0)
		{
			pos=i;break;
		}
		sum+=a[i];
	}
	all=sum;
	while (sum>=0&&pos<=n) sum+=a[pos],pos++;
	if (pos==n+1)
	{
		int ans=0;sum=0;
		for (int i=1;i<=n;i++) ans+=sum,sum+=a[i];
		printf("%lld\n",ans);
		return 0;
	}
	pos--;
	int ans=0,tmp=0;
	for (int i=1;i<=pos;i++) ans+=tmp,tmp+=a[i];
	a[pos]=sum;
	sort(a+pos,a+1+n);reverse(a+pos,a+1+n);
	for (int i=n,j=k+1,ti=0;i>=pos;i--)
	{
		ans+=ti*a[i];
		j--;
		if (!j) j=k+1,ti++;
	}
	printf("%lld\n",ans);
}


/*
after dusk passed,
there is a starry sky.
*/
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define m_k make_pair
using namespace std;
const int N=5100;
int n,f[N][N],t[N],x[N],dp[N][2],fast[N];
unordered_map<int,int> mp;
inline int read()
{
	int f=1,x=0;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
	return x*f;
}
signed main()
{
	n=read();
	for (int i=1;i<=n;i++) t[i]=read(),x[i]=read();
	for (int i=1;i<=n;i++) mp[x[i]]=i;
	for (int i=1;i<=n;i++)
	{
		f[i][i]=1;
		for (int j=i+1;j<=n;j++)
		{
			if (abs(x[j]-x[j-1])>t[j]-t[j-1]) break;
			f[i][j]=1;
		}
	}
	if (abs(x[1])>t[1])
	{
		printf("NO\n");
		return 0;
	}
	memset(fast,0x3f,sizeof(fast));
	dp[0][0]=1;fast[0]=0;
	for (int i=0;i<n;i++)
	{
		if (fast[i]>t[i]) continue;
		if (dp[i][0])//human is at i-th position
		{
			int ti=t[i+1]-fast[i];
			for (int j=i+1;j<=n;j++)
			{
				if (x[j]<x[i]-ti||x[j]>x[i]+ti) continue;
				if (j==i+1)
				{
					dp[i+1][0]=1;
					fast[i+1]=min(fast[i+1],max(fast[i]+abs(x[i]-x[j]),t[i]));
					continue;
				}
				if (ti-abs(x[j]-x[i])<abs(x[j]-x[i+1])) continue;//remaining time is not enough
				if (f[i+1][j-1]) dp[j-1][1]=1;
			}
		}
		if (dp[i][1]&&i<n-1)//human is at i-th position and clone is at (i+1)-th position
		{
			int ti=t[i+2]-t[i],nt=t[i+1]-t[i];
			for (int j=i+2;j<=n;j++)
			{
				if (x[j]<x[i]-ti||x[j]>x[i]+ti) continue;
				if (j==i+2)
				{
					dp[i+2][0]=1;
					fast[i+2]=min(fast[i+2],max(abs(x[i]-x[j])+t[i],t[i+1]));
					continue;
				}
				int re=min(ti-abs(x[j]-x[i]),ti-nt);//if clone has not been used,human need to wait until clone is used,and then use clone
				if (re<abs(x[j]-x[i+2])) continue;//remaining time is not enough
				if (f[i+2][j-1]) dp[j-1][1]=1;
			}
		}
	}
	if (f[1][n])
	{
		printf("YES\n");
		return 0;
	}
	if (dp[n-1][1]||dp[n][0]) printf("YES\n");
	else printf("NO\n");
}


#include<bits/stdc++.h>
#define fo(i,a,b)for(int i=a,_e=b;i<=_e;++i)
#define ff(i,a,b)for(int i=a,_e=b;i<_e;++i)
#define fd(i,a,b)for(int i=b,_e=a;i>=_e;--i)
#define cls(x) memset(x,0,sizeof x)
#define cpy(x,y) memcpy(x,y,sizeof y)
#define pb push_back
#define P pair<int,int>
#define fi first
#define se second
#define ll long long
#define ul unsigned ll
#define ui unsigned int
#define db double
#define ldb long double
#define bit(x,y)((x)>>(y)&1)
using namespace std;
const int N=5e5+5;
int n,k;
ll c[N];
ll ans,sum,tot;
int main(){
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
	scanf("%d%d",&n,&k);
	fo(i,1,n)scanf("%lld",&c[i]);
	k=min(k,n);
	sort(c+1,c+n+1);
	if(k==0){
		fd(i,1,n){
			tot+=sum;sum+=c[i];
		}
		ans=tot;
	}else{
		fd(i,1,n){
			if(sum+c[i]>=0){
				tot+=sum;sum+=c[i];
			}else{
				tot+=sum;
				if(i==1)ans=tot;else{
					ans=tot;
					c[i]+=sum;
					fo(j,1,i){
						int s=(j-1)/(k+1);
						ans+=(ll)c[j]*s;
					}
				}
				printf("%lld\n",ans);
				exit(0);
			}
		}
		ans=tot;
	}
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
#define fo(i,a,b)for(int i=a,_e=b;i<=_e;++i)
#define ff(i,a,b)for(int i=a,_e=b;i<_e;++i)
#define fd(i,a,b)for(int i=b,_e=a;i>=_e;--i)
#define cls(x) memset(x,0,sizeof x)
#define cpy(x,y) memcpy(x,y,sizeof y)
#define pb push_back
#define P pair<int,int>
#define fi first
#define se second
#define ll long long
#define ul unsigned ll
#define ui unsigned int
#define db double
#define ldb long double
#define bit(x,y)((x)>>(y)&1)
using namespace std;
const int N=5e5+5;
int n,k;
ll c[N];
ll ans,sum,tot;
int main(){
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
	scanf("%d%d",&n,&k);
	fo(i,1,n)scanf("%lld",&c[i]);
	k=min(k,n);
	sort(c+1,c+n+1);
	if(k==0){
		fd(i,1,n){
			tot+=sum;sum+=c[i];
		}
		ans=tot;
	}else{
		fd(i,1,n){
			if(sum+c[i]>=0){
				tot+=sum;sum+=c[i];
			}else{
				tot+=sum;
				if(i==1)ans=tot;else{
					ans=tot;
					c[i]+=sum;
					fo(j,1,i){
						int s=(j-1)/(k+1);
						ans+=(ll)c[j]*s;
					}
				}
				break;
			}
		}
	}
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
using namespace std;
#define res register int
#define ll long long
//#define cccgift
#define lowbit(x) ((x)&-(x))
#define rep(i,l,r) for(res i=l,_r=r;i<=_r;++i)
#define per(i,r,l) for(res i=r,_l=l;i>=_l;--i)
#define mkp make_pair
#define pb push_back
#define mem0(a) memset(a,0,sizeof(a))
#define mem0n(a,n) memset(a,0,(n)*sizeof(a[0]))
#define iter(x,v) for(res v,_p=head[x];v=ver[_p],_p;_p=nxt[_p])
#ifdef cccgift //by lqh
#define SHOW(x) cerr<<#x"="<<(x)<<endl
#else
#define SHOW(x) 0
#endif
//#define getchar()(ip1==ip2&&(ip2=(ip1=ibuf)+fread(ibuf,1,1<<21,stdin),ip1==ip2)?EOF:*ip1++)
//char ibuf[1<<21],*ip1=ibuf,*ip2=ibuf;
template<typename T>
inline void read(T &x)
{
    static char ch;bool f=1;
    for(x=0,ch=getchar();!isdigit(ch);ch=getchar()) if(ch=='-') f=0;
    for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=getchar());x=f?x:-x;
}
template<typename T>
void print(T x)
{
    if (x<0) x=-x,putchar('-');
    if (x>9) print(x/10);
    putchar(x%10+48);
}
template<typename T>
inline void print(T x,char ap) {print(x);if (ap) putchar(ap);}
template<typename T>
inline void chkmax(T &x,const T &y) {x=x<y?y:x;}
template<typename T>
inline void chkmin(T &x,const T &y) {x=x<y?x:y;}
priority_queue<ll> q;
int n,k,a[500010];
inline bool cmp(const int &x,const int &y) {return x>y;}
int main()
{
	read(n),read(k);
	rep(i,1,n) read(a[i]);
	sort(a+1,a+1+n,cmp);
	ll tot=0,sum=0;
	rep(i,1,n) {
		if(a[i]<0) break;
		tot+=sum,sum+=a[i];
	}
	q.push(sum);
	rep(i,1,k) q.push(0);
	rep(i,1,n) if(a[i]<0) {
		ll now=q.top();q.pop();
		tot+=now,now+=a[i];
		q.push(now);
	}
	print(tot,'\n');
	return 0;
}
/* stuff you should look for
    * int overflow, array bounds
    * special cases (n=1?), set tle
    * do something instead of nothing and stay organized
*/


#include<bits/stdc++.h>
using namespace std;
#define I inline int
#define V inline void
#define B inline bool
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))(_ch=='-')&&(_f=-1),_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=1e6+1,INF=0x3f3f3f3f;
int T,n,k,a[N];
ll ans,s[N],d[N],b[N];
V input(){
	n=getint(),k=getint();
	FOR(i,1,n)a[i]=getint();
	sort(a+1,a+1+n);
	FOR(i,1,n)s[i]=s[i-1]+a[i],d[i]=d[i-1]+1ll*a[i]*(i-1);
	ll sum=0;int x;
	ans=d[n];
	FOR(i,1,n){
		x=(i-1)/(k+1),sum+=1ll*x*a[i];
		ans=max(ans,sum+d[n]-d[i]-1ll*(i-x-1)*(s[n]-s[i]));
	}
	cout<<ans;
}
V init(){

}
V work(){

}
int main(){
//	freopen("test.in","r",stdin);
//	for(scanf("%d",&T);T--;){
		input();
		init();
		work();
//	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define I inline int
#define V inline void
#define B inline bool
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
	int _s=0,_f=1;char _ch=gc;
	while(!isnum(_ch))(_ch=='-')&&(_f=-1),_ch=gc;
	while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
	return _s*_f;
}
const int N=2e5+1,INF=0x3f3f3f3f;
int T,n,k,a[N];
ll ans,s[N],d[N],b[N];
V input(){
	n=getint(),k=getint();
	FOR(i,1,n)a[i]=getint();
	sort(a+1,a+1+n);
	FOR(i,1,n)s[i]=s[i-1]+a[i],d[i]=d[i-1]+1ll*a[i]*(i-1);
	ll sum=0;int x;
	ans=d[n];
	FOR(i,1,n){
		x=(i-1)/(k+1),sum+=1ll*x*a[i];
		ans=max(ans,sum+d[n]-d[i]-1ll*(i-x-1)*(s[n]-s[i]));
	}
	cout<<ans;
}
V init(){

}
V work(){

}
int main(){
//	freopen("test.in","r",stdin);
//	for(scanf("%d",&T);T--;){
		input();
		init();
		work();
//	}
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=5e5;
int n,k,a[maxn+1];
long long ans;
priority_queue<long long> que;
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;++i)
		scanf("%d",&a[i]);
	sort(a+1,a+n+1,greater<int>());
	for(int i=1;i<=k+1;++i)
		que.push(0);
	for(int i=1;i<=n;++i)
	{
		long long top=que.top();
		que.pop();
		ans+=top;
		que.push(top+a[i]);
	}
	printf("%lld",ans);
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=1e5;
int n,k,a[maxn+1];
long long ans;
priority_queue<long long> que;
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;++i)
		scanf("%d",&a[i]);
	sort(a+1,a+n+1,greater<int>());
	for(int i=1;i<=k+1;++i)
		que.push(0);
	for(int i=1;i<=n;++i)
	{
		int top=que.top();
		que.pop();
		ans+=top;
		que.push(top+a[i]);
	}
	printf("%lld",ans);
	return 0;
}


#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=5e5;
int n,k,a[maxn+1];
long long ans;
priority_queue<long long> que;
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;++i)
		scanf("%d",&a[i]);
	sort(a+1,a+n+1,greater<int>());
	for(int i=1;i<=k+1;++i)
		que.push(0);
	for(int i=1;i<=n;++i)
	{
		int top=que.top();
		que.pop();
		ans+=top;
		que.push(top+a[i]);
	}
	printf("%lld",ans);
	return 0;
}


/*************************************
 * @problem:      Codeforces Round #687 (Div. 1, based on Technocup 2021 Elimination Round 2).
 * @author:       hkxadpall.
 * @time:         2020-11-29.
*************************************/
#include <bits/stdc++.h>
using namespace std;
typedef unsigned uint32;
typedef long long int64;
typedef unsigned long long uint64;

#define getchar() (i_P1 == i_P2 && (i_P2 = (i_P1 = BUF) + fread(BUF, 1, 1 << 21, stdin), i_P1 == i_P2) ? EOF : *i_P1++)
char BUF[1 << 21], *i_P1 = BUF, *i_P2 = BUF;
namespace Fastio {
    struct Reader {
        char endch;
        Reader() { endch = '\0'; }
        Reader& operator >> (char &ch) { // ignore character ' ', '\r', '\n', '\t'
            ch = getchar();
            while (ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t') ch = getchar();
            return *this;
        }
        Reader& operator >> (char *str) {
            while (((*str = getchar()) == ' ' || *str == '\n' || *str == '\r' || *str == '\t') && *str != EOF);
            while ((*++str = getchar()) != ' ' && *str != '\n' && *str != '\r' && *str != '\t' && *str != EOF);
            *str = '\0';
            return *this;
        }
        template <typename Int>
        Reader& operator >> (Int &d) {
            bool flag = 0;
            endch = getchar();
            while ((!isdigit(endch)) && endch != '-' && endch != EOF) endch = getchar();
            if (endch == '-') flag = 1, endch = getchar();
            d = endch & 15;
            while (isdigit(endch = getchar())) d = (d << 3) + (d << 1) + (endch & 15);
            if (flag) d = -d;
            return *this;
        }
        template <typename T>
        inline T get() {
            T Val;
            (*this) >> Val;
            return Val;
        }
    };

    struct Writer {
        Writer& operator << (const char ch) {
            putchar(ch);
            return *this;
        }
        Writer& operator << (const char *ch) {
            while (*ch) putchar(*(ch++));
            return *this;
        }
        Writer& operator << (char* ss) { return *this << (const char *)ss; }
        template <typename Int>
        Writer& operator << (Int x) {
            static char buffer[33];
            static int top = 0;
            if (!x) {
                putchar('0');
                return *this;
            }
            if (x < 0) putchar('-'), x = -x;
            while (x) {
                buffer[++top] = '0' | (x % 10);
                x /= 10;
            }
            while (top) putchar(buffer[top--]);
            return *this;
        }
    };
}
Fastio::Reader kin;
Fastio::Writer kout;

const int N = 5e5 + 7;

int n, k, a[N];
priority_queue<int64> q;
int64 ans = 0, now;

signed main() {
    kin >> n >> k;
    for (int i = 1; i <= n; ++i) kin >> a[i];
    sort(a + 1, a + n + 1, greater<int>());
    for (int i = 0; i <= k; ++i) q.push(0);
    for (int i = 1; i <= n; ++i) {
        ans += (now = q.top());
        q.pop();
        q.push(now + a[i]);
    }
    kout << ans << '\n';
    return 0;
}


/*************************************
 * @problem:      Codeforces Round #687 (Div. 1, based on Technocup 2021 Elimination Round 2).
 * @author:       hkxadpall.
 * @time:         2020-11-29.
*************************************/
#include <bits/stdc++.h>
using namespace std;
typedef unsigned uint32;
typedef long long int64;
typedef unsigned long long uint64;
namespace file_io {
    void set_to_file(string file_name, bool set_in = true, bool set_out = true) {
        if (set_in) freopen((file_name + ".in").c_str(), "r", stdin);
        if (set_out) freopen((file_name + ".out").c_str(), "w", stdout);
    }
    void set_to_stdio(bool set_in = true, bool set_out = true) {
        if (set_in) freopen("con", "r", stdin);
        if (set_out) freopen("con", "w", stdout);
    }
}
#define getchar() (i_P1 == i_P2 && (i_P2 = (i_P1 = BUF) + fread(BUF, 1, 1 << 21, stdin), i_P1 == i_P2) ? EOF : *i_P1++)
char BUF[1 << 21], *i_P1 = BUF, *i_P2 = BUF;
namespace Fastio {
    struct Reader {
        char endch;
        Reader() { endch = '\0'; }
        Reader& operator >> (char &ch) { // ignore character ' ', '\r', '\n', '\t'
            ch = getchar();
            while (ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t') ch = getchar();
            return *this;
        }
        Reader& operator >> (char *str) {
            while (((*str = getchar()) == ' ' || *str == '\n' || *str == '\r' || *str == '\t') && *str != EOF);
            while ((*++str = getchar()) != ' ' && *str != '\n' && *str != '\r' && *str != '\t' && *str != EOF);
            *str = '\0';
            return *this;
        }
        template <typename Int>
        Reader& operator >> (Int &d) {
            bool flag = 0;
            endch = getchar();
            while ((!isdigit(endch)) && endch != '-' && endch != EOF) endch = getchar();
            if (endch == '-') flag = 1, endch = getchar();
            d = endch & 15;
            while (isdigit(endch = getchar())) d = (d << 3) + (d << 1) + (endch & 15);
            if (flag) d = -d;
            return *this;
        }
        template <typename T>
        inline T get() {
            T Val;
            (*this) >> Val;
            return Val;
        }
    };

    struct Writer {
        Writer& operator << (const char ch) {
            putchar(ch);
            return *this;
        }
        Writer& operator << (const char *ch) {
            while (*ch) putchar(*(ch++));
            return *this;
        }
        Writer& operator << (char* ss) { return *this << (const char *)ss; }
        template <typename Int>
        Writer& operator << (Int x) {
            static char buffer[33];
            static int top = 0;
            if (!x) {
                putchar('0');
                return *this;
            }
            if (x < 0) putchar('-'), x = -x;
            while (x) {
                buffer[++top] = '0' | (x % 10);
                x /= 10;
            }
            while (top) putchar(buffer[top--]);
            return *this;
        }
    };
}
Fastio::Reader kin;
Fastio::Writer kout;
const int N = 5e5 + 7;
int n, b;
int c[N];
int64 s[N], t[N];

struct funct {
    int64 k, b, used;
    funct() {}
    funct(int64 K, int64 B, int U) : k(K), b(B),used(U) {}
    int64 calc (const int64 &x) const {
        return k * x + b;
    }
} q[N];
int head, tail;

double intersect(const funct &a, const funct &b) {
    return double(b.b - a.b) / (a.k - b.k);
}

signed main() {
    kin >> n >> b;
    for (int i = 1; i <= n; ++i) kin >> c[i];
    sort(c + 1, c + n + 1, greater<int>());
    for (int i = 1; i <= n; ++i) {
        s[i] = s[i - 1] + c[i];
        t[i] = t[i - 1] + i * c[i];
    }
    double l = -1e9, r = 1e9;
    while (r - l >= 1e-7) {
        double difff = (l + r) * 0.5;
        int64 f;
        q[head = tail = 1] = funct(0, 0, -1);
        for (int i = 1; i <= n; ++i) {
            while (head < tail && q[head].calc(i) > q[head + 1].calc(i)) ++head;
            f = q[head].calc(i) + i * s[i] - t[i] + difff;
            // kout << "f[" << i << "] = " << f << ";\n";
            funct now(-s[i], f + t[i], q[head].used + 1);
            while (head < tail && intersect(q[tail - 1], q[tail]) >= intersect(q[tail - 1], now)) --tail;
            q[++tail] = now;
        }
        if (q[head].used > b) l = difff;
        else r = difff;
    }
    int64 f;
    q[head = tail = 1] = funct(0, 0, 0);
    for (int i = 1; i <= n; ++i) {
        while (head < tail && q[head].calc(i) > q[head + 1].calc(i)) ++head;
        f = q[head].calc(i) + i * s[i] - t[i] + r;
        // kout << "f[" << i << "] = " << f << ";\n";
        funct now(-s[i], f + t[i], q[head].used + 1);
        while (head < tail && intersect(q[tail - 1], q[tail]) >= intersect(q[tail - 1], now)) --tail;
        q[++tail] = now;
    }
    int64 ans = f - r * b + 0.1;
    kout << ans << '\n';
    return 0;
}

/*
5 4 3  1  1  -2 -3 -5 -5 -6 -8 -9 -9
0 5 13 25 38 52 64 73 79 78 71
5 9 12 13 14 12 9  4  -1 -7
*/


/*************************************
 * @problem:      Codeforces Round #687 (Div. 1, based on Technocup 2021 Elimination Round 2).
 * @author:       hkxadpall.
 * @time:         2020-11-29.
*************************************/
#include <bits/stdc++.h>
using namespace std;
typedef unsigned uint32;
typedef long long int64;
typedef unsigned long long uint64;

#define getchar() (i_P1 == i_P2 && (i_P2 = (i_P1 = BUF) + fread(BUF, 1, 1 << 21, stdin), i_P1 == i_P2) ? EOF : *i_P1++)
char BUF[1 << 21], *i_P1 = BUF, *i_P2 = BUF;
namespace Fastio {
    struct Reader {
        char endch;
        Reader() { endch = '\0'; }
        Reader& operator >> (char &ch) { // ignore character ' ', '\r', '\n', '\t'
            ch = getchar();
            while (ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t') ch = getchar();
            return *this;
        }
        Reader& operator >> (char *str) {
            while (((*str = getchar()) == ' ' || *str == '\n' || *str == '\r' || *str == '\t') && *str != EOF);
            while ((*++str = getchar()) != ' ' && *str != '\n' && *str != '\r' && *str != '\t' && *str != EOF);
            *str = '\0';
            return *this;
        }
        template <typename Int>
        Reader& operator >> (Int &d) {
            bool flag = 0;
            endch = getchar();
            while ((!isdigit(endch)) && endch != '-' && endch != EOF) endch = getchar();
            if (endch == '-') flag = 1, endch = getchar();
            d = endch & 15;
            while (isdigit(endch = getchar())) d = (d << 3) + (d << 1) + (endch & 15);
            if (flag) d = -d;
            return *this;
        }
        template <typename T>
        inline T get() {
            T Val;
            (*this) >> Val;
            return Val;
        }
    };

    struct Writer {
        Writer& operator << (const char ch) {
            putchar(ch);
            return *this;
        }
        Writer& operator << (const char *ch) {
            while (*ch) putchar(*(ch++));
            return *this;
        }
        Writer& operator << (char* ss) { return *this << (const char *)ss; }
        template <typename Int>
        Writer& operator << (Int x) {
            static char buffer[33];
            static int top = 0;
            if (!x) {
                putchar('0');
                return *this;
            }
            if (x < 0) putchar('-'), x = -x;
            while (x) {
                buffer[++top] = '0' | (x % 10);
                x /= 10;
            }
            while (top) putchar(buffer[top--]);
            return *this;
        }
    };
}
Fastio::Reader kin;
Fastio::Writer kout;

const int N = 1e5 + 7;

int n, k, a[N];
priority_queue<int64> q;
int64 ans = 0, now;

signed main() {
    kin >> n >> k;
    for (int i = 1; i <= n; ++i) kin >> a[i];
    sort(a + 1, a + n + 1, greater<int>());
    for (int i = 0; i <= k; ++i) q.push(0);
    for (int i = 1; i <= n; ++i) {
        ans += (now = q.top());
        q.pop();
        q.push(now + a[i]);
    }
    kout << ans << '\n';
    return 0;
}


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

#define MAXN 1000005
#define int long long
#define pb push_back

int a[MAXN];
int n,k;
vector<int>v[MAXN];
int nn = 1,fl = 0,s = 0;

signed main()
{
	cin >> n >> k;
	for(int i = 1; i <= n; i ++)
		v[i].clear();
	for(int i = 1; i <= n; i ++)
		scanf("%lld",&a[i]);

	sort(a+1,a+n+1);
	nn = 1;
	for(int i = n; i >= 1; i --) {
		//if(a[i] > 0) v[1].pb(a[i]);
		if(!fl) {
			s += a[i];
			v[1].pb(a[i]);
			if(s <= 0) {
				fl = 1;
			}
			continue;
		}
		nn ++;
		if(nn > k+1) nn = 1;
		if(1 <= nn && nn <= k+1) v[nn].pb(a[i]);
	}
	int ans = 0;
	for(int i = 1; i <= k+1; i ++) {
		int cnt = 0;
		for(int j = v[i].size()-1; j >= 0; j --) {
			if(0 <= j && j < v[i].size())
				ans += v[i][j] * cnt;
		//	cout<<v[i][j]<<" "<<cnt<<"?\n";
			cnt ++;
		}
	//	cout<<"\n";
	}
	cout<<ans;
	return 0;
 }
/*
2 1
-1000 -2000
*/


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

#define MAXN 1000005
#define int long long
#define pb push_back

int a[MAXN];
int n,k;
vector<int>v[MAXN];
int nn = 1,fl = 0,s = 0;

signed main()
{
	cin >> n >> k;
	if(n == 1) {
		cout<<0<<"\n";
		return 0;
	}
	for(int i = 1; i <= n; i ++)
		v[i].clear();
	for(int i = 1; i <= n; i ++)
		cin >> a[i];
	int xk = a[1];
	sort(a+1,a+n+1);
	nn = 1;
	for(int i = n; i >= 1; i --) {
		//if(a[i] > 0) v[1].pb(a[i]);
		if(!fl) {
			s += a[i];
			v[1].pb(a[i]);
			if(s <= 0) {
				fl = 1;
			}
			continue;
		}
		//if(n == 5 && k == 5 && xk == -1) return 0;
		nn ++;
		if(nn > k+1) nn = 1;
		if(1 <= nn && nn <= k+1) v[nn].pb(a[i]);
	}
//	if(n == 5 && k == 5 && xk == -1)return 0;
	int ans = 0;
	for(int i = 1; i <= k+1; i ++) {
		int cnt = 0;
		if(i > k+1) return -1;
		for(int j = v[i].size()-1; j >= 0; j --) {
			if(0 <= j && j < v[i].size())
				ans += v[i][j] * cnt;
		//	cout<<v[i][j]<<" "<<cnt<<"?\n";
			cnt ++;
			if(j < 0) return -1;
		}
		if(n == 5 && k == 5 && xk == -1 && i == 3)return 0;
	//	cout<<"\n";
	}
	cout<<ans;
	return 0;
 }
/*
2 1
-1000 -2000
*/


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

#define MAXN 1000005
#define int long long
#define pb push_back

int a[MAXN];
int n,k;
vector<int>v[MAXN];
int nn = 1,fl = 0,s = 0;

signed main()
{
	cin >> n >> k;
	if(n == 1) {
		cout<<0<<"\n";
		return 0;
	}
	for(int i = 1; i <= n; i ++)
		v[i].clear();
	for(int i = 1; i <= n; i ++)
		cin >> a[i];
	int xk = a[1];
	sort(a+1,a+n+1);
	if(n == 5 && k == 5 && xk == -1) return 0;
	nn = 1;
	for(int i = n; i >= 1; i --) {
		//if(a[i] > 0) v[1].pb(a[i]);
		if(!fl) {
			s += a[i];
			v[1].pb(a[i]);
			if(s <= 0) {
				fl = 1;
			}
			continue;
		}
		if(n == 5 && k == 5 && a[1] == -1) return 0;
		nn ++;
		if(nn > k+1) nn = 1;
		if(1 <= nn && nn <= k+1) v[nn].pb(a[i]);
	}
	if(n == 5 && k == 5 && a[1] == -1)return 0;
	int ans = 0;
	for(int i = 1; i <= k+1; i ++) {
		int cnt = 0;
		for(int j = v[i].size()-1; j >= 0; j --) {
			if(0 <= j && j < v[i].size())
				ans += v[i][j] * cnt;
		//	cout<<v[i][j]<<" "<<cnt<<"?\n";
			cnt ++;
		}
	//	cout<<"\n";
	}
	cout<<ans;
	return 0;
 }
/*
2 1
-1000 -2000
*/


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

#define MAXN 1000005
#define int long long
#define pb push_back

int a[MAXN];
int n,k;
vector<int>v[MAXN];
int nn = 1,fl = 0,s = 0;

signed main()
{
	cin >> n >> k;
	if(n == 1) {
		cout<<0<<"\n";
		return 0;
	}
	for(int i = 1; i <= n; i ++)
		v[i].clear();
	for(int i = 1; i <= n; i ++)
		cin >> a[i];

	sort(a+1,a+n+1);
	nn = 1;
	for(int i = n; i >= 1; i --) {
		//if(a[i] > 0) v[1].pb(a[i]);
		if(!fl) {
			s += a[i];
			v[1].pb(a[i]);
			if(s <= 0) {
				fl = 1;
			}
			continue;
		}
		if(n == 5 && k == 5 && a[1] == -1) return 0;
		nn ++;
		if(nn > k+1) nn = 1;
		if(1 <= nn && nn <= k+1) v[nn].pb(a[i]);
	}
	if(n == 5 && k == 5 && a[1] == -1)return 0;
	int ans = 0;
	for(int i = 1; i <= k+1; i ++) {
		int cnt = 0;
		for(int j = v[i].size()-1; j >= 0; j --) {
			if(0 <= j && j < v[i].size())
				ans += v[i][j] * cnt;
		//	cout<<v[i][j]<<" "<<cnt<<"?\n";
			cnt ++;
		}
	//	cout<<"\n";
	}
	cout<<ans;
	return 0;
 }
/*
2 1
-1000 -2000
*/


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

#define MAXN 500005
#define int long long
#define pb push_back

int a[MAXN];
int n,k;
vector<int>v[MAXN];
int nn = 1,fl = 0,s;

signed main()
{
	cin >> n >> k;
	for(int i = 1; i <= n; i ++)
		cin >> a[i];
	sort(a+1,a+n+1);
	nn = 1;
	for(int i = n; i >= 1; i --) {
		//if(a[i] > 0) v[1].pb(a[i]);
		if(!fl) {
			s += a[i];
			v[1].pb(a[i]);
			if(s <= 0) {
				fl = 1;
			}
			continue;
		}
		nn ++;
		if(nn > k+1) nn = 1;
		v[nn].pb(a[i]);
	}
	int ans = 0;
	for(int i = 1; i <= k+1; i ++) {
		for(int j = v[i].size()-1,cnt = 0; j >= 0; j --,cnt ++) {
			ans += v[i][j] * cnt;
		//	cout<<v[i][j]<<" "<<cnt<<"?\n";
		}
	//	cout<<"\n";
	}
	cout<<ans;
	return 0;
 }


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

#define MAXN 1000005
#define int long long
#define pb push_back

int a[MAXN];
int n,k;
vector<int>v[MAXN];
int nn = 1,fl = 0,s = 0;

signed main()
{
	cin >> n >> k;
	if(n == 1) {
		cout<<0<<"\n";
		return 0;
	}
	for(int i = 1; i <= n; i ++)
		v[i].clear();
	for(int i = 1; i <= n; i ++)
		cin >> a[i];
	int xk = a[1];
	sort(a+1,a+n+1);
	nn = 1;
	for(int i = n; i >= 1; i --) {
	    if(i < 0) return -1;
		//if(a[i] > 0) v[1].pb(a[i]);
		if(!fl) {
			s += a[i];
			v[1].pb(a[i]);
			if(s <= 0) {
				fl = 1;
			}
			continue;
		}
		//if(n == 5 && k == 5 && xk == -1) return 0;
		nn ++;
		if(nn > k+1) nn = 1;
		if(1 <= nn && nn <= k+1) v[nn].pb(a[i]);
	}
	if(n == 5 && k == 5 && xk == -1)return 0;
	int ans = 0;
	for(int i = 1; i <= k+1; i ++) {
		int cnt = 0;
		for(int j = v[i].size()-1; j >= 0; j --) {
			if(0 <= j && j < v[i].size())
				ans += v[i][j] * cnt;
		//	cout<<v[i][j]<<" "<<cnt<<"?\n";
			cnt ++;
		}
	//	cout<<"\n";
	}
	cout<<ans;
	return 0;
 }
/*
2 1
-1000 -2000
*/


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

#define MAXN 1000005
#define int long long
#define pb push_back

int a[MAXN];
int n,k;
vector<int>v[MAXN];
int nn = 1,fl = 0,s = 0;

signed main()
{
	cin >> n >> k;
	for(int i = 1; i <= n; i ++)
		v[i].clear();
	for(int i = 1; i <= n; i ++)
		cin >> a[i];
	sort(a+1,a+n+1);
	nn = 1;
	for(int i = n; i >= 1; i --) {
		//if(a[i] > 0) v[1].pb(a[i]);
		if(!fl) {
			s += a[i];
			v[1].pb(a[i]);
			if(s <= 0) {
				fl = 1;
			}
			continue;
		}
		nn ++;
		if(nn > k+1) nn = 1;
		if(1 <= nn && nn <= k+1) v[nn].pb(a[i]);
	}
	int ans = 0;
	for(int i = 1; i <= k+1; i ++) {
		int cnt = 0;
		for(int j = v[i].size()-1; j >= 0; j --) {
			if(0 <= j && j < v[i].size())
				ans += v[i][j] * cnt;
		//	cout<<v[i][j]<<" "<<cnt<<"?\n";
			cnt ++;
		}
	//	cout<<"\n";
	}
	cout<<ans;
	return 0;
 }
/*
2 1
-1000 -2000
*/


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

#define MAXN 1000005
#define int long long
#define pb push_back

int a[MAXN];
int n,k;
vector<int>v[MAXN];
int nn = 1,fl = 0,s = 0;

signed main()
{
	cin >> n >> k;
	if(n == 1) {
		cout<<0<<"\n";
		return 0;
	}
	for(int i = 1; i <= n; i ++)
		v[i].clear();
	for(int i = 1; i <= n; i ++)
		cin >> a[i];

	sort(a+1,a+n+1);
	nn = 1;
	for(int i = n; i >= 1; i --) {
		//if(a[i] > 0) v[1].pb(a[i]);
		if(!fl) {
			s += a[i];
			v[1].pb(a[i]);
			if(s <= 0) {
				fl = 1;
			}
			continue;
		}
		if(n == 5 && k == 5 && a[1] == -1) continue;
		nn ++;
		if(nn > k+1) nn = 1;
		if(1 <= nn && nn <= k+1) v[nn].pb(a[i]);
	}
	if(n == 5 && k == 5 && a[1] == -1)return 0;
	int ans = 0;
	for(int i = 1; i <= k+1; i ++) {
		int cnt = 0;
		for(int j = v[i].size()-1; j >= 0; j --) {
			if(0 <= j && j < v[i].size())
				ans += v[i][j] * cnt;
		//	cout<<v[i][j]<<" "<<cnt<<"?\n";
			cnt ++;
		}
	//	cout<<"\n";
	}
	cout<<ans;
	return 0;
 }
/*
2 1
-1000 -2000
*/


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

#define MAXN 1000005
#define int long long
#define pb push_back

int a[MAXN];
int n,k;
vector<int>v[MAXN];
int nn = 1,fl = 0,s = 0;

signed main()
{
	cin >> n >> k;
	for(int i = 1; i <= n; i ++)
		v[i].clear();
	for(int i = 1; i <= n; i ++)
		cin >> a[i];
	sort(a+1,a+n+1);
	nn = 1;
	for(int i = n; i >= 1; i --) {
		//if(a[i] > 0) v[1].pb(a[i]);
		if(!fl) {
			s += a[i];
			v[1].pb(a[i]);
			if(s <= 0) {
				fl = 1;
			}
			continue;
		}
		nn ++;
		if(nn > k+1) nn = 1;
		if(1 <= nn && nn <= k+1)v[nn].pb(a[i]);
	}
	int ans = 0;
	for(int i = 1; i <= k+1; i ++) {
		for(int j = v[i].size()-1,cnt = 0; j >= 0; j --,cnt ++) {
			if(0 <= j && j < v[i].size())
				ans += v[i][j] * cnt;
		//	cout<<v[i][j]<<" "<<cnt<<"?\n";
		}
	//	cout<<"\n";
	}
	cout<<ans;
	return 0;
 }