// Source: Yassinezeort
#include <bits/stdc++.h>
using namespace std;
#define io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
void insert(vector<int> &v , int a){
v.push_back(a);
v[0]++;
int i = v[0] ;
while(i > 1 && v[i/2] > v[i]){
swap(v[i] , v[i/2]);
i/=2 ;
}
}
void del(vector<int> &v){
int sm = v[1] ;
v[1] = v[v[0]] ;
v[0]--;
v.pop_back();
int i = 1 ;
while (true){
int l = 2*i , r = 2*i+1 , smallest = i ;
if (l <= v[0] && v[l] < v[smallest] )
smallest = l ;
if (r <= v[0] && v[r] < v[smallest] )
smallest = r ;
if (smallest != i){
swap(v[i] , v[smallest]) ;
i = smallest ;
}
else
break ;
}
}
int main() {
io;
int n , k ; cin >> n >> k ;
vector<int> heap(1 ,0);
for (int i = 0; i < n; ++i) {
int a ; cin >> a ;
insert(heap , a) ;
if (heap[0] > k){
del(heap);
}
}
long long res = 0 ;
for (int i = 1 ; i <= heap[0] ; ++i) {
res += heap[i];
}
cout << res ;
}
Ly8gU291cmNlOiBZYXNzaW5lemVvcnQKCiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgaW8gaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7IGNpbi50aWUoTlVMTCk7IGNvdXQudGllKE5VTEwpCgoKdm9pZCBpbnNlcnQodmVjdG9yPGludD4gJnYgLCBpbnQgYSl7CiAgICB2LnB1c2hfYmFjayhhKTsKICAgIHZbMF0rKzsKICAgIGludCBpID0gdlswXSA7CiAgICB3aGlsZShpID4gMSAmJiB2W2kvMl0gPiB2W2ldKXsKICAgICAgICBzd2FwKHZbaV0gLCB2W2kvMl0pOwogICAgICAgIGkvPTIgOwogICAgfQp9Cgp2b2lkIGRlbCh2ZWN0b3I8aW50PiAmdil7CiAgICBpbnQgc20gPSB2WzFdIDsKICAgIHZbMV0gPSB2W3ZbMF1dIDsKICAgIHZbMF0tLTsKICAgIHYucG9wX2JhY2soKTsKICAgIGludCBpID0gMSA7CiAgICB3aGlsZSAodHJ1ZSl7CiAgICAgICAgaW50IGwgPSAyKmkgLCByID0gMippKzEgLCBzbWFsbGVzdCA9IGkgOwogICAgICAgIGlmIChsIDw9IHZbMF0gJiYgdltsXSA8IHZbc21hbGxlc3RdICkKICAgICAgICAgICAgc21hbGxlc3QgPSBsIDsKICAgICAgICBpZiAociA8PSB2WzBdICYmIHZbcl0gPCB2W3NtYWxsZXN0XSApCiAgICAgICAgICAgIHNtYWxsZXN0ID0gciA7CiAgICAgICAgaWYgKHNtYWxsZXN0ICE9IGkpewogICAgICAgICAgICBzd2FwKHZbaV0gLCB2W3NtYWxsZXN0XSkgOwogICAgICAgICAgICBpID0gc21hbGxlc3QgOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgICAgIGJyZWFrIDsKICAgIH0KfQoKCmludCBtYWluKCkgewogICAgaW87CiAgICBpbnQgbiAsIGsgOyBjaW4gPj4gbiA+PiBrIDsKICAgIHZlY3RvcjxpbnQ+IGhlYXAoMSAsMCk7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgICAgIGludCBhIDsgY2luID4+IGEgOwogICAgICAgIGluc2VydChoZWFwICwgYSkgOwogICAgICAgIGlmIChoZWFwWzBdID4gayl7CiAgICAgICAgICAgIGRlbChoZWFwKTsKICAgICAgICB9CiAgICB9CiAgICBsb25nIGxvbmcgcmVzID0gMCA7CiAgICBmb3IgKGludCBpID0gMSA7IGkgPD0gaGVhcFswXSA7ICsraSkgewogICAgICAgIHJlcyArPSBoZWFwW2ldOwogICAgfQogICAgY291dCA8PCByZXMgOwoKfQ==
Main.java:3: error: illegal character: '#'
#include <bits/stdc++.h>
^
Main.java:3: error: class, interface, or enum expected
#include <bits/stdc++.h>
^
Main.java:7: error: illegal character: '#'
#define io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
^
Main.java:7: error: class, interface, or enum expected
#define io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
^
Main.java:7: error: class, interface, or enum expected
#define io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
^
Main.java:7: error: class, interface, or enum expected
#define io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
^
Main.java:12: error: class, interface, or enum expected
v[0]++;
^
Main.java:13: error: class, interface, or enum expected
int i = v[0] ;
^
Main.java:14: error: class, interface, or enum expected
while(i > 1 && v[i/2] > v[i]){
^
Main.java:16: error: class, interface, or enum expected
i/=2 ;
^
Main.java:17: error: class, interface, or enum expected
}
^
Main.java:22: error: class, interface, or enum expected
v[1] = v[v[0]] ;
^
Main.java:23: error: class, interface, or enum expected
v[0]--;
^
Main.java:24: error: class, interface, or enum expected
v.pop_back();
^
Main.java:25: error: class, interface, or enum expected
int i = 1 ;
^
Main.java:26: error: class, interface, or enum expected
while (true){
^
Main.java:28: error: class, interface, or enum expected
if (l <= v[0] && v[l] < v[smallest] )
^
Main.java:30: error: class, interface, or enum expected
if (r <= v[0] && v[r] < v[smallest] )
^
Main.java:32: error: class, interface, or enum expected
if (smallest != i){
^
Main.java:34: error: class, interface, or enum expected
i = smallest ;
^
Main.java:35: error: class, interface, or enum expected
}
^
Main.java:38: error: class, interface, or enum expected
}
^
Main.java:44: error: class, interface, or enum expected
int n , k ; cin >> n >> k ;
^
Main.java:44: error: class, interface, or enum expected
int n , k ; cin >> n >> k ;
^
Main.java:45: error: class, interface, or enum expected
vector<int> heap(1 ,0);
^
Main.java:46: error: class, interface, or enum expected
for (int i = 0; i < n; ++i) {
^
Main.java:46: error: class, interface, or enum expected
for (int i = 0; i < n; ++i) {
^
Main.java:46: error: class, interface, or enum expected
for (int i = 0; i < n; ++i) {
^
Main.java:47: error: class, interface, or enum expected
int a ; cin >> a ;
^
Main.java:48: error: class, interface, or enum expected
insert(heap , a) ;
^
Main.java:49: error: class, interface, or enum expected
if (heap[0] > k){
^
Main.java:51: error: class, interface, or enum expected
}
^
Main.java:54: error: class, interface, or enum expected
for (int i = 1 ; i <= heap[0] ; ++i) {
^
Main.java:54: error: class, interface, or enum expected
for (int i = 1 ; i <= heap[0] ; ++i) {
^
Main.java:54: error: class, interface, or enum expected
for (int i = 1 ; i <= heap[0] ; ++i) {
^
Main.java:56: error: class, interface, or enum expected
}
^
Main.java:59: error: class, interface, or enum expected
}
^
37 errors