//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
inline LL read(){
LL x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
struct node{
LL val,deep;
bool operator <(const node &x)const{
return val==x.val?deep>x.deep:val>x.val;
}
}temp;
priority_queue<node>q;
inline void Huffman(LL n,LL k){
LL ans=0;
while(n>1){
LL max_deep=0,tot=0;
for(LL i=1;i<=k;++i){
temp=q.top();q.pop();
tot+=temp.val;max_deep=max(max_deep,temp.deep);
}
temp.val=tot;temp.deep=max_deep+1;q.push(temp);
ans+=tot;n-=k-1;
}
printf("%lld\n%lld\n",ans,q.top().deep-1);
}
int main(){
LL n=read(),k=read();
for(LL i=1;i<=n;++i){
LL v=read();
temp.val=v;temp.deep=1;q.push(temp);
}
LL res=(n-1)%(k-1);
if(res!=0)res=k-1-res,n+=res;
for(LL i=1;i<=res;++i)temp.val=0,temp.deep=1,q.push(temp);
Huffman(n,k);
return 0;
}
原文:https://www.cnblogs.com/PPXppx/p/11246424.html