C - Sweets Eating CodeForces - 1253C

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.
Examples
Input

9 2
6 19 3 4 4 2 6 7 8

Output

2 5 11 18 30 43 62 83 121

Input

1 1
7

Output

7

Note
Let’s analyze the answer for k=5
in the first example. Here is one of the possible ways to eat 5

sweets that minimize total sugar penalty:
Day 1: sweets 1 and 4
Day 2: sweets 5 and 3
Day 3: sweet 6

Total penalty is 1⋅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 5 sweets, hence x5=30
其实就是一个规律题:你把题目样例给的数据自己手动写一下就会发现规律。
首先得想到贪心思维,要使得k*a[i]最小,数值小的最后吃,数值大的最先吃。
你定义两个数组,一个ans[],一个前缀和sum[],
接下来看样例你会发现:
i < = m 时 , a n s [ i ] = s u m [ i ] i<=m时,ans[i]=sum[i] i<=m,ans[i]=sum[i]
i > m 时 , a n s [ i ] = a n s [ i − m ] + s u m [ i ] i>m时,ans[i]=ans[i-m]+sum[i] i>m,ans[i]=ans[im]+sum[i]

代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=200010;
int a[N];
ll sum[N],ans[N];
int main(){
	int n,m,i,t,j;
	cin>>n>>m;
	for( i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	sort(a+1,a+n+1);
	sum[0]=0;
	for(i=1;i<=n;i++){
		sum[i]=sum[i-1]+a[i];
	}
	for(i=1;i<=n;i++){
		if(i<=m)ans[i]=sum[i];
		else ans[i]=ans[i-m]+sum[i];
	}
	for(i=1;i<=n;i++){
		cout<<ans[i]<<' ';
	}
	return 0;
} 
为了在Windows安装ADB工具,你可以按照以下步骤进行操作: 1. 首先,下载ADB工具包并解压缩到你自定义的安装目录。你可以选择将其解压缩到任何你喜欢的位置。 2. 打开运行窗口,可以通过按下Win+R键来快速打开。在运行窗口中输入"sysdm.cpl"并按下回车键。 3. 在系统属性窗口中,选择"高级"选项卡,然后点击"环境变量"按钮。 4. 在环境变量窗口中,选择"系统变量"部分,并找到名为"Path"的变量。点击"编辑"按钮。 5. 在编辑环境变量窗口中,点击"新建"按钮,并将ADB工具的安装路径添加到新建的路径中。确保路径正确无误后,点击"确定"按钮。 6. 返回到桌面,打开命令提示符窗口。你可以通过按下Win+R键,然后输入"cmd"并按下回车键来快速打开命令提示符窗口。 7. 在命令提示符窗口中,输入"adb version"命令来验证ADB工具是否成功安装。如果显示版本信息,则表示安装成功。 这样,你就成功在Windows安装ADB工具。你可以使用ADB工具来执行各种操作,如枚举设备、进入/退出ADB终端、文件传输、运行命令、查看系统日志等。具体的操作方法可以参考ADB工具的官方文档或其他相关教程。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [windows环境安装adb驱动](https://blog.csdn.net/zx54633089/article/details/128533343)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Windows安装使用ADB简单易懂教程](https://blog.csdn.net/m0_37777700/article/details/129836351)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值