链接:https://codeforces.com/contest/1253/problem/C
Tsumugi brought nn delicious sweets to the Light Music Club. They are numbered from 11 to nn, where the ii-th sweet has a sugar concentration described by an integer aiai.
Yui loves sweets, but she can eat at most mm sweets each day for health reasons.
Days are 11-indexed (numbered 1,2,3,…1,2,3,…). Eating the sweet ii at the dd-th day will cause a sugar penalty of (d⋅ai)(d⋅ai), as sweets become more sugary with time. A sweet can be eaten at most once.
The total sugar penalty will be the sum of the individual penalties of each sweet eaten.
Suppose that Yui chooses exactly kk sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get?
Since Yui is an undecided girl, she wants you to answer this question for every value of kk between 11 and nn.
Input
The first line contains two integers nn and mm (1≤m≤n≤200 0001≤m≤n≤200 000).
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤200 0001≤ai≤200 000).
Output
You have to output nn integers x1,x2,…,xnx1,x2,…,xn on a single line, separed by spaces, where xkxk is the minimum total sugar penalty Yui can get if she eats exactly kk sweets.
Examples
input
Copy
9 2 6 19 3 4 4 2 6 7 8
output
Copy
2 5 11 18 30 43 62 83 121
input
Copy
1 1 7
output
Copy
7
Note
Let's analyze the answer for k=5k=5 in the first example. Here is one of the possible ways to eat 55 sweets that minimize total sugar penalty:
- Day 11: sweets 11 and 44
- Day 22: sweets 55 and 33
- Day 33 : sweet 66
Total penalty is 1⋅a1+1⋅a4+2⋅a5+2⋅a3+3⋅a6=6+4+8+6+6=301⋅a1+1⋅a4+2⋅a5+2⋅a3+3⋅a6=6+4+8+6+6=30. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats 55 sweets, hence x5=30x5=30.
题解:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
long long n,m,b,x,y,t,r,k,s=0;
long long a[1000001];
long long dp[1000001];
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
if(i<m)
dp[i]=a[i];
else
dp[i]=a[i]+dp[i-m];
}
for(int i=1;i<=n;i++)
{
s+=dp[i];
cout<<s<<" ";
}
return 0;
}