Distribution
Limits: 1 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Zenyk is the commander of an assault company. The company leadership has tasked him with eliminating the Russian military leadership. However, before proceeding with the mission, it is necessary to arm well. There are \(n\) platoons in Zenyk’s brigade, with the \(i\)-th platoon having \(a_i\) people. Zenyk needs to distribute ammunition to each platoon. The more people in a platoon, the more rounds they need. Ammunition is a very important thing in war, and therefore it must be saved. Thus, Zenyk will distribute the boxes of ammunition as follows:
each platoon will receive at least 1 box of ammunition,
if two platoons have the same number of people, they will receive the same number of boxes of ammunition,
if platoon \(x\) has a larger headcount than platoon \(y\), it will receive a larger number of boxes of ammunition,
among all ways to distribute the boxes of ammunition, Zenyk will choose the one where the total number of distributed boxes is minimized.
Help Zenyk, tell him how many boxes of ammunition will be needed to arm the company?
Input
The first line contains a single integer \(n\) — the number of platoons in Zenyk’s company.
The second line contains \(n\) integers \(a_i\) — the headcount of the \(i\)-th platoon.
Output
In a single line, output one integer — the minimum number of boxes needed to arm the company.
Constraints
\(1 \le n \le 10^{5}\),
\(1 \le a_i \le 10^{9}\),
In at least 7 tests, \(1 \le n \le 10^{3}\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 5 1 2 1 3 3 | 10 |
Notes
In the example, it is optimal to give 1 box of ammunition to the first and third platoons, 2 boxes to the second, and 3 boxes each to the fourth and fifth. Thus, Zenyk needs a total of 10 boxes.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Distribution - Editorial
We use a greedy approach. The platoons with the same smallest number of people get one box of ammunition each. The platoons with the second smallest value get 2 boxes each, and so on. That is, we sort the platoons by the number of people in them. The first one gets 1 box of ammunition. Then we iterate over the platoons starting from the second: if a platoon has the same number of people as the previous one, it gets as many boxes as the previous one; otherwise it gets 1 box more.
Note that the answer can be greater than \(5 \cdot 10^9\), so we need a data type that can hold such large values.
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|