//#include <tourist>
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;

typedef pair<ll, ll> p;
const int INF = 1e9;
const double eps = 1e-7;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const long double pi = 4 * atan(1);
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v)          \
    cout << #v << ":";    \
    for (auto x : v)      \
    {                     \
        cout << x << ' '; \
    }                     \
    cout << endl;
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v)
{
    for (T &x : v)
        is >> x;
    return is;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//g++ yarudake.cpp -std=c++17 -I .
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    cin >> a;
    map<int, int> m;
    rep(i, n)
    {
        m[a[i]]++;
    }
    int ans = 0;
    int i = 1;
    int box = min(m[0],k);
    while(box){
        int h =  max(box - m[i],0);
        ans += h*i;
        chmin(box,m[i]);
        i++;
    }
    cout<<ans<<"\n";
}

#include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define all(c) (c).begin(), (c).end()

// !!READ THE PROBLEM STATEMENT CORRECTLY YOU STUPID, WORTHLESS PIECE OF SHIT!!
//Analyze testcases thoroughly before starting implementation
//Don't forget to read the stuff at the bottom

void solve() {
	int N, M;
	cin >> N >> M;
	vector <int> A(N), B(M);
	for (auto &a : A) cin >> a;
	for (auto &b : B) cin >> b;
	// this is equivalent to an edit distance problem
	vector <vector <int> > dp(N + 1, vector <int> (M + 1));
	for (int i = 1; i <= N; i++) {
		dp[i][0] = i; 
		for (int j = 1; j <= M; j++) {
			dp[0][j] = j;
			dp[i][j] = min({dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + (A[i - 1] != B[j - 1])});
		}
	}
	cout << dp[N][M];		
}

int main() {
	fastIO;
	solve();
	cout << '\n';
	return 0;
}

/*
check for edge cases : n = 0, 1 etc
check for overflows and out of bounds
*/