#include <bits/stdc++.h>
using namespace std;
void solve()
{
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
m--;
int smallest_num = INT_MAX, ans = 0;
for (int i = 0; i < m; i++)
{
int x = 0;
for (x = 0; x < n; x++)
{
smallest_num = min(smallest_num, a[x]);
}
a[x] = INT_MAX;
ans += smallest_num;
smallest_num = INT_MAX;
}
cout << ans;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2b2lkIHNvbHZlKCkKewp9CgppbnQgbWFpbigpCnsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZSgwKTsKCiAgICBpbnQgbiwgbTsKICAgIGNpbiA+PiBuID4+IG07CgogICAgdmVjdG9yPGludD4gYShuKTsKCiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykKICAgIHsKICAgICAgICBjaW4gPj4gYVtpXTsKICAgIH0KCiAgICBtLS07CgogICAgaW50IHNtYWxsZXN0X251bSA9IElOVF9NQVgsIGFucyA9IDA7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG07IGkrKykKICAgIHsKICAgICAgICBpbnQgeCA9IDA7CiAgICAgICAgZm9yICh4ID0gMDsgeCA8IG47IHgrKykKICAgICAgICB7CiAgICAgICAgICAgIHNtYWxsZXN0X251bSA9IG1pbihzbWFsbGVzdF9udW0sIGFbeF0pOwogICAgICAgIH0KCiAgICAgICAgYVt4XSA9IElOVF9NQVg7CiAgICAgICAgYW5zICs9IHNtYWxsZXN0X251bTsKICAgICAgICBzbWFsbGVzdF9udW0gPSBJTlRfTUFYOwogICAgfQoKICAgIGNvdXQgPDwgYW5zOwp9