#include<bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
#define pd push_back
#define sort(v) sort(v.begin(),v.end())
#define Sort(v) sort(v.begin(),v.end(),greater<int>())
#define display(s) for(auto val:s) cout<<val<<" "; cout<<endl;
#define displayMap(mymap) for(auto val:mymap) cout<<val.first<<" -> "<<val.second<<endl;
#define Sortcom(v) sort(v.begin(),v.end(),mycompare) 
#define f(a,b,c) for(int i=a;i<b;i+=c)

bool mycompare(int a,int b)
{
    return a<b;
}

int mod(int x)
{
    return (x%MOD+MOD)%MOD;
}
int mul(int a,int b)
{return mod(mod(a)*mod(b));}
int add(int a,int b)
{return mod(mod(a)+mod(b));}

int power(int a,int b)
{
    if(b==0)
    return 1;
    int smallans = power(a,b/2);
    smallans = smallans * smallans;
    if(b&1)
    smallans = smallans * a;
    return smallans;    
}

int solve(){
    int n,k; cin>>n>>k;
    vector<int> v(n,0);
    map<int,int> mymap;
    for(int i=0;i<n;i++)
    {
        cin>>v[i];
        mymap[v[i]]++;
    }
    int ans = 0;
    int boxes = 0;
    while(boxes<k)
    {
        int val = 0;
        bool flag = true;
        while(mymap[val]!=0)
        {
            mymap[val]--;
            val++;
            flag = false;
        }
        ans += val;
        boxes++;
        if(flag)
        break;
    }
    return ans;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;  t=1;
    // int t; cin>>t;
    while(t--)
    {
        // solve();
        cout<<solve()<<endl;
    }
    return 0;
}
#define _GLIBCXX_DEBUG

#include<bits/stdc++.h>

/*
#include<vector>
#include<iostream>
//*/
using namespace std;
#define ll long long
#define yes {cout<<"Yes"<<endl;return 0;}
#define no {cout<<"No"<<endl;return 0;}
#define YES {cout<<"YES"<<endl;return 0;}
#define NO {cout<<"NO"<<endl;return 0;}
#define cyes {cout<<"Yes"<<endl;continue;}
#define cno {cout<<"No"<<endl;continue;}

#define MOD 1000000007
#define MOD2 1000000009
#define MOD3 998244353
#define INF 2147483647
constexpr double PI  = 3.141592653589793238462643383279;
#define LINF 9223372036854775807

#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rep1(i,n) for (int i = 1; i < (n); ++i)




#define PF push_front
#define PB push_back
#define POF pop_front
#define POB pop_back

#define MP make_pair
#define MT make_tuple

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define SZ(a) (int)((a).size())

#define NEXP(a) next_permutation((a).begin(),(a).end())
#define FI first
#define SE second

#define SORT(c) sort((c).begin(),(c).end())
#define SORTR(c) sort((c).rbegin(), (c).rend())
#define REVERSE(c) reverse((c).begin(),(c).end())


typedef long long LL;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;

const vector<int> dx = {1,0,-1,0,1,1,-1,-1};
const vector<int> dy = {0,1,0,-1,1,-1,1,-1};



int main(void)
{
    int i,j;
    int n,k;
    cin>>n>>k;
    VI a(n);
    VI cnt(n);
    rep(i,n){cin>>a[i];cnt[a[i]]++;}
    VI dp(n);
    int mini=cnt[0];
    rep(i,n)
    {
        dp[i]=min(mini,cnt[i]);
        mini=dp[i];
    }
    int used=0,ans=0;
    for(i=n-1;i>=0;i--)
    {
        if(min(dp[i],k)-used>0)
        {
            ans+=(i+1)*(min(k,dp[i])-used);
            used=min(dp[i],k);
        }
    }
    cout<<ans<<endl;
}
    
    