C++ :
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 200010;
int n, a[MAXN], k;
int cnt = 0;
void dfs(int now, long long sum, const long long lim) {
if (now > n || cnt >= k || sum + a[now] > lim)
return;
++cnt;
dfs(now + 1, sum, lim);
dfs(now + 1, sum + a[now], lim);
}
int main() {
freopen("note.in", "r", stdin);
freopen("note.out", "w", stdout);
long long l = 0, r = 0, mid, ans = -1;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), r += a[i];
sort(a + 1, a + n + 1);
while (l <= r) {
mid = l + r >> 1;
cnt = 0;
dfs(1, 0, mid);
if (cnt >= k)
ans = mid, r = mid - 1;
else
l = mid + 1;
}
printf("%lld\n", ans);
return 0;
}
#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>
using namespace std;
const int MAXN = 200010;
int n, k, a[MAXN];
typedef pair<long long, int> p;
priority_queue<p, vector<p>, greater<p> > h;
int main() {
freopen("note.in", "r", stdin);
freopen("note.out", "w", stdout);
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
sort(a + 1, a + n + 1);
h.push(make_pair(a[1], 1));
int cnt = 0;
while (!h.empty()) {
long long val = h.top().first;
int ind = h.top().second;
++cnt;
if (cnt == k) {
printf("%lld\n", val);
break;
}
h.pop();
if (ind < n) {
h.push(make_pair(val - a[ind] + a[ind + 1], ind + 1));
h.push(make_pair(val + a[ind + 1], ind + 1));
}
}
return 0;
}