#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int, int> >
#define vpll vector <pair <long long, long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define vb vector <bool>
#define vvb vector <vb>
#define mp make_pair
#define vvll vector <vll>
#define vsi vector <si>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define MANX MAXN
#define itn int
#define dbg(x); {cout << #x << "=" << x << ", ";}
#define in(x); { for (auto &to : x) cin >> to;}
#define out(x); { for (auto &to : x) cout << to << " "; cout << '\n'; }

const ll INFLL = 1e18;
const int MAXN = 1e6 + 100;
const ll INF = 1e9;
const int mod1 = 1e9 + 7;
const int mod2 = 1e9 + 21;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> cnt(n);
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        cnt[x]++;
    }
    int was = cnt[0];
    ll ans = 0;
    vector <pll> a;
    for (int i = 1; i < n; i++) {
        int now = min(was, cnt[i]);
        int stay = was - now;
        a.pb({i,stay});
        swap(was, now);
    }
    reverse(all(a));
    for (auto to:a)
    {
        int x=min(k,(int)(to.se));
        ans+=1ll*x*to.fi;
        k-=x;
    }
    cout<<ans<<'\n';
}

int main() {
#ifdef Mip182
    freopen("a.in", "r", stdin);
#else
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    int _t;
    _t = 1;
//    cin>>_t;
    while (_t--)
        solve();
#ifdef Mip182
    cout << '\n' << "Time : " << (double) (clock()) / CLOCKS_PER_SEC << '\n';
#endif
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

ll gcd(ll a, ll b) {
  if (a < b) {
    a ^= b;
    b ^= a;
    a ^= b;
  }
  return b ? gcd(b, a % b) : a;
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll f(ll h) {
  if (h == 1) {
    return 1;
  } else {
    return 2 * f(h / 2) + 1;
  }
}

ll mod = 1000000007;

ll powmod(ll a, ll b) {
  ll ret = 1;
  for (int i = 0; i < b; i++) {
    ret = (ret * a) % mod;
  }
  return ret;
}

ll comb(ll n, ll k) {
  map<string, ll> memo;
  if (n == k || k == 0) {
    return 1;
  } else {
    string key = to_string(n) + to_string(k);
    if (memo.count(key) != 0) {
      return memo[key];
    } else {
      ll v = comb(n - 1, k - 1) + comb(n - 1, k);
      memo[key] = v;
      return v;
    }
  }
}

int f(int n, int k) {
  int ret = 0;

  while (n < k) {
    n *= 2;
    ret++;
  }

  return ret;
}

int main() {
  int N, K;
  cin >> N >> K;

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

  int i = 0;
  int prev = -1;
  for (auto a : v) {
    if (prev != a) {
      i = 0;
    }

    if (box[i] + 1 == a) {
      box[i] = a;
      i++;
    }

    prev = a;
  }

  ll ans = 0;
  for (auto a : box) {
    if (a != -1) {
      ans += (a + 1);
    }
  }
  cout << ans << endl;
}
