AcWing 149. 荷马史诗
原题链接
简单
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<LL,int> PLI;
int main(){
int n,k;
cin >> n >> k;
priority_queue<PLI,vector<PLI>,greater<PLI>> heap;
while(n--){
LL w;
cin >> w;
heap.push({w,0});
}
while((heap.size() - 1)%( k - 1)) heap.push({0,0});
LL res = 0;
while(heap.size() > 1){
LL s = 0;
int depth = 0;
for(int i = 0;i < k ;i++){
auto t = heap.top(); heap.pop();
s+=t.x,depth = max(depth,t.y);
}
heap.push({s,depth + 1 });
res += s;
}
cout << res <<endl << heap.top().y;
return 0;
}