QOJ.ac

QOJ

Time Limit: 1.0 s Memory Limit: 32 MB Total points: 100

#16979. 愤怒

Statistics

幼儿园的孩子们收到了一大袋糖果,里面有 $M$ 颗糖果。现在决定将这些糖果分给 $N$ 个孩子。

每个孩子都提出了自己想要的糖果数量。如果一个孩子没有得到他想要的糖果数量,他就会生气。实际上,每少给一颗糖果,他就会变得更生气。有人推测,孩子的怒气值等于他少得糖果数量的平方。例如,如果 Mirko 声明他想要 32 颗糖果,但只收到了 29 颗,那么他就少了 3 颗糖果,因此他的怒气值将等于 9。

不幸的是,糖果的总数不足以满足所有孩子。因此,应该以一种使所有孩子的怒气值之和最小的方式来分配糖果。

输入格式

第一行包含两个整数 $M$($1 \le M \le 2 \cdot 10^9$)和 $N$($1 \le N \le 100\,000$)。

接下来的 $N$ 行,每行包含一个整数,表示每个孩子想要的糖果数量。这些数字都严格小于 $2 \cdot 10^9$,且它们的总和总是大于 $M$。

输出格式

输出的第一行也是唯一的一行,必须包含所有孩子怒气值之和的最小值。

注意:测试数据将确保结果在 64 位无符号整数范围内:Pascal 中的 int64,C/C++ 中的 long long,Java 中的 long

子任务

  • 对于 $40\%$ 的测试数据,满足 $M$ 不大于 $200\,000$。
  • 对于 $70\%$ 的测试数据,满足没有孩子想要的糖果数量超过 $100\,000$。
  • 对于 $80\%$ 的测试数据,满足上述两个限制条件中至少有一个。

样例

输入样例 1

5 3
1
3
2

输出样例 1

1

输入样例 2

10 4
4
5
2
3

输出样例 2

4

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.