soj3916 ljutnja

Description

Children in a kindergarten have received a large sack containing M candies. It has been decided that the

candies are to be distributed among N children.

Each child has stated the number of candies that it wants. If a child isn’t given the amount of candy it

wants, it will get angry. In fact it’ll get angrier for each candy it is deprived of. Some speculate that it’s

anger will be equal to the square of the number of candy it is deprived of. For instance, if Mirko states

that he wants 32 candies but receives only 29, he would be missing 3 candies, so his anger would be

equal to 9.

Unfortunately, there is an insufficient amount of candy to satisfy all children. Therefore, the candies

should be distributed in such a way that the sum of the children’s anger is minimal.

Input

The first line contains two integers, M (1 ≤ M ≤ 2·10 9 ) and N (1 ≤ N ≤ 100 000).

The following N lines contain integers (one per line) which represent the wishes of the children. Those

numbers are all strictly less than 2·10 9 , and their sum always exceeds M.

Test cases worth 40% of total points have M not greater than 200 000.

Test cases worth 70% of total points have no child state that it wants more than 100 000 candies.

Test cases worth 80% of total points have at least one of the above stated constraints will be met.

Output

The first and only line of output must contain the minimum sum of the children’s anger.

Note: The test cases will ensure that the result fits in a 64-bit unsigned integer: int64 in Pascal, long long

in C/C++, long in Java.

Sample Input

5 3

1

3

2

10 4

4

5

2

3

Sample Output

1

4

Source

coci 2010/2011 contest 1

 

很荣幸WA 了一次……

这题就是尽量把缺的糖果分散给不同的人,但是第一次做的时候比较悲剧,导致有的童鞋非但木有拿到糖果,反而还要拿出糖果给别人- -||||

假设所需的糖果为s 个,但是只有 m 个,也就是说先全部满足所有小盆友的要求之后再拿走 num=s-m 个。对于每个人所需要的糖果,先进行排序,之后对每个人来说,先判断 (num/ 还没拿的人数 ) 是否大于 a[i] ,就是说每个人平均都要拿差不多一样个,但是有个前提条件是拿走的糖果数不能大于 a[i] ,最后直接扫一遍过了。

代码如下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值