admin 管理员组

文章数量: 1086019

【CodeForces 1253C

【CodeForces 1253C --- Sweets Eating】DP


Description

Tsumugi brought n delicious sweets to the Light Music Club. They are numbered from 1 to n, where the i-th sweet has a sugar concentration described by an integer ai.

Yui loves sweets, but she can eat at most m sweets each day for health reasons.

Days are 1-indexed (numbered 1,2,3,…). Eating the sweet i at the d-th day will cause a sugar penalty of (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 k 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 k between 1 and n.

Input

The first line contains two integers n and m (1≤m≤n≤200 000).

The second line contains n integers a1,a2,…,an (1≤ai≤200 000).

Output

You have to output n integers x1,x2,…,xn on a single line, separed by spaces, where xk is the minimum total sugar penalty Yui can get if she eats exactly k sweets.

Sample Input

9 2
6 19 3 4 4 2 6 7 8

Sample Output

2 5 11 18 30 43 62 83 121

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
using ll = long long;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 2e5+5;
ll arr[MAXN],dp[MAXN];int main()
{SIS;ll n,m,sum=0;cin >> n >> m;for(int i=1;i<=n;i++) cin >> arr[i];sort(arr+1,arr+n+1);for(int i=1;i<=n;i++){sum+=arr[i];if(i<m) dp[i]=sum;else dp[i]=dp[i-m]+sum;cout << dp[i] << " ";}return 0;
}

本文标签: CodeForces 1253C