题目描述
瞎搞
经典题了
#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int maxn=100000+10;
int left[maxn],right[maxn];
ll heap[maxn],sum[maxn];
int i,j,k,l,r,t,n,m,tot;
int read(){
int x=0;
char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return x;
}
void add(ll x,int l,int r){
heap[++tot]=x;
left[tot]=l;right[tot]=r;
int j=tot;
while (j>1&&heap[j]>heap[j/2]){
swap(heap[j],heap[j/2]);
swap(left[j],left[j/2]);
swap(right[j],right[j/2]);
j/=2;
}
}
void pop(){
heap[1]=heap[tot];
left[1]=left[tot];right[1]=right[tot];
tot--;
int j=1,k;
while (j*2<=tot){
if (j*2+1>tot||heap[j*2]>heap[j*2+1]) k=j*2;else k=j*2+1;
if (heap[j]<heap[k]){
swap(heap[j],heap[k]);
swap(left[j],left[k]);
swap(right[j],right[k]);
j=k;
}
else break;
}
}
int main(){
freopen("ksum.in","r",stdin);freopen("ksum.out","w",stdout);
n=read();k=read();
fo(i,1,n){
t=read();
sum[i]=sum[i-1]+(ll)t;
}
fo(i,1,n) add(sum[n]-sum[i-1],i,n);
while (k--){
printf("%lld ",heap[1]);
l=left[1];r=right[1];
pop();
if (l!=r) add(sum[r-1]-sum[l-1],l,r-1);
}
printf("\n");
}