贪心+优先队列
每次从堆中挑出权值最小的,权值相同挑trie上深度最深的,怼到一起就行了
代码
//By AcerMo
#include<queue>
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define lli long long int
using namespace std;
struct emm
{
lli a,b;
emm(){b=0;}
bool friend operator < (emm x,emm y)
{
if (x.a==y.a) return x.b<y.b;
return x.a<y.a;
}
}e;
priority_queue<emm>q;
lli n,m,ans;
signed main()
{
cin>>n>>m;
for (int i=1;i<=n;i++) scanf("%lld",&e.a),e.a*=-1,q.push(e);
e.a=0;e.b=0;
while ((n-1)%(m-1)) q.push(e),n++;
while (q.size()>1)
{
lli qlm=0,gll=0;
for (int i=1;i<=m;i++)
qlm+=q.top().a,gll=min(gll,q.top().b),q.pop();
e.a=qlm;e.b=gll-1;ans-=qlm,q.push(e);
}
cout<<ans<<endl<<q.top().b*-1;
return 0;
}