Submit | All submissions | Best solutions | Back to list |
C1LJUTNJ - Ljutnja |
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 its anger will be equal to the square of the number of candies 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.109) 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.109, and their sum always exceeds M.
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.
Example
Input:
5 3
1
3
2
Output:
1
Input:
10 4
4
5
2
3
Output:
4
Added by: | Race with time |
Date: | 2010-11-14 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
Resource: | COCI 2010-2011 1-st Round |
hide comments
|
||||||
2018-09-29 07:39:04
Fun to figure out, frustrating to implement without the code ending up ugly. Gave up on the latter bit, glad that ugly code can run fast too. Could have saved some time spent handling cases where there's enough candy for everyone by bothering to read the statement carefully though. |
||||||
2016-05-14 13:08:36 Dushyant Singh
Weak test cases. My code gives 2 for input 3 2 2 2 Correct answer = 1 Disappointed :( Last edit: 2016-05-14 13:13:54 |
||||||
2016-04-13 10:30:18 Dushyant Singh
@anonymous_: Take 3,4,2,1 candies from first, second, third and fourth respectively. They will be left with one candy each. So answer will be 4 |
||||||
2016-04-12 11:02:19
How could the ans to 2nd tst case be 4? Shouldn't it be 8 ? |
||||||
2016-02-26 19:43:01 Nebojsa
Great problem, took me a while... |
||||||
2016-01-24 15:52:25 Siddharth Singh
Terrific Question , Loved It <3 |
||||||
2015-04-02 19:31:20 CoNtRaDiCtIoN
yeah use unsigned long long int for test case : 8 |
||||||
2015-01-27 17:54:52 LUCIFER
this is one hell of a question |
||||||
2014-12-11 06:34:09 Kriti Joshi
Use variable type unsigned long long int for 'sum of angers'. Worked for me to pass Test Case 8 |
||||||
2014-11-01 02:12:45 Samuel Nacache
Finally, I have my revenge |