#t117. 数组清零
数组清零
Array Zeroing
Problem Description
Little A has an array consisting of non-negative integers. He repeatedly applies the following operation to the array until it contains only zeros. In one operation, Little A does the following three steps in order:
- Find the largest integer in the array , and let its index be . If there are several maximum values, choose the one with the largest index.
- Among all non-zero integers in the array , find the smallest integer .
- Subtract from the found in step 1.
For example, the array needs operations to become :
$$[2, 3, 4] \rightarrow [2, 3, 2] \rightarrow [2, 1, 2] \rightarrow [2, 1, 1] \rightarrow [1, 1, 1] \rightarrow [1, 1, 0] \rightarrow [1, 0, 0] \rightarrow [0, 0, 0]$$Little A wants to know, for a given array , how many operations are needed to turn all its elements into . It can be proved that the elements of will all become after a finite number of operations. Can you compute the answer for him?
Input Format
The first line contains a positive integer , the length of the array .
The second line contains non-negative integers , the elements of the array.
Output Format
One line, a positive integer: the number of operations needed to turn all elements of into .
Sample Input 1
3
2 3 4
Sample Input 2
5
1 3 2 2 5
Sample Output 1
7
Sample Output 2
13
Sample Explanation
For the first sample, the array is , and the operations proceed as follows:
- Maximum is (index ), smallest non-zero is , , array becomes .
- Maximum is (index ), smallest non-zero is , , array becomes .
- Maximum is (several maxima; choose the largest index, i.e. the last ), smallest non-zero is , , array becomes .
- Maximum is , smallest non-zero is , , array becomes .
- Maximum is (choose the largest index, i.e. the third ), smallest non-zero is , , array becomes .
- Maximum is (index ), smallest non-zero is , , array becomes .
- Maximum is (index ), smallest non-zero is , , array becomes .
After operations in total, everything becomes .
Constraints
For all test cases, , .