#include <iostream>
#include<string>
#include<vector>
//#include<set>
#include<algorithm>
using namespace std;
/**
 1. n > m
 2. n < m
**/

// 第三题装酒
int main(){

  int m, n;
  vector<int> w;

  // n people
  // m shuilongtou
  cin >>n>>m;
  for(int i =0 ; i< n; i++){
    int x; cin >>x ; w.push_back(x);
  }
  // whe n <=m pick the max val
  if(n<=m){
      sort(w.begin(),w.end());
      cout << *(w.end()-1)<<endl;
      return 0;
  }

  // pick first m
  vector<int> ***;
  for(int i =0; i< m ; i++)***.push_back(w[i]);

  // queue
  int cost =0;
  size_t i =m;
  while(i < w.size()){
    sort(***.begin(), ***.end());
    int tar=***[0];// smallest
    cost += tar;
    for(size_t k =0 ; k < ***.size();k++){
        ***[k]-=tar;
        if(***[k]==0&&i<w.size())***[k]=w[i++]; //pull the new one from w. no zero
    }
  }

  // rest cost;
  sort(***.begin(),***.end());
  cost+=***[m-1];
  cout <<cost <<endl;
  return 0;
}

//第二题  报数踢人
int main(){
  int m ;
  cin >>m;
  if(m<=1){cout<<"ERROR!"<<endl;return 0;}
  if(m>=100){cout<<"ERROR!"<<endl;return 0;}
  vector<bool> pp(100,true);
  int cc=100, i=0, kill=0;
  while(cc>=m){
    kill=pp[i]?(kill+1):kill;
    if(kill==m){
      pp[i]=false;
      cc--;
      kill=0;
    }
    i= (i+1)%100;
  }
  bool first= true;
  for(size_t i =0 ; i < pp.size(); i++)
    if(pp[i]&&first){
      cout <<i+1;
      first= false;
    }else if(pp[i]){
      cout <<","<<i+1;
    }
  return 0;
}

// 第一题最长连续数字串
int main()
{
    string s;
    while(cin >>s){
    size_t curr_l=0;
    string nums="";
    string ms="";
    for(size_t i =0 ; i< s.size() ; i++)
    {
        if(s[i]>='0'&&s[i]<='9')
        {
           curr_l++;
           nums+= s[i];
        }else{
           ms = curr_l>=ms.size()?nums:ms;
           curr_l =0;
           nums="";
        }
    }
    ms= curr_l>=ms.size()?nums:ms;
   if(ms.size()!=0)
      cout<< ms<<endl;
    cout << ms.size()<<endl;
    }
    return 0;
}