C. Sweets Eating

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 (dai)(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 (1mn200 0001≤m≤n≤200 000).

The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai200 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 1a1+1a4+2a5+2a3+3a6=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 <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
using namespace std;
int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const double eps = 1e-6;
const int mod = 998244353;
const int N = 2500 + 5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
int qpow(int m, int k, int mod)
{
    int res = 1, t = m;
    while (k)
    {
        if (k & 1)
            res = res * t % mod;
        t = t * t % mod;
        k >>= 1;
    }
    return res;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
ll lcm(ll m, ll n)
{
    return m * n / gcd(m, n);
}
int main()
{
    ll m, n;
    cin >> n >> m;
    vector<ll> a(n+1), pre(n + 1,0);
    rep(i, 1, n)
    {
        cin >> a[i];
    }
    sort(a.begin() + 1, a.end());
    rep(i,1,n)
    {
        if (i > m)
            pre[i] = pre[i - m] + a[i];
        else
            pre[i] = a[i];
    }
    ll ans = 0;
    rep(i, 1, n)
    {
        ans += pre[i];
        cout << ans << " ";
    }

    return 0;
}
原文地址:https://www.cnblogs.com/dealer/p/13032505.html