#t117. 数组清零

数组清零

Array Zeroing

Problem Description

Little A has an array a=[a1,a2,…,an]a = [a_1, a_2, \ldots, a_n] consisting of nn non-negative integers. He repeatedly applies the following operation to the array aa until it contains only zeros. In one operation, Little A does the following three steps in order:

  1. Find the largest integer in the array aa, and let its index be kk. If there are several maximum values, choose the one with the largest index.
  2. Among all non-zero integers in the array aa, find the smallest integer aja_j.
  3. Subtract aja_j from the aka_k found in step 1.

For example, the array a=[2,3,4]a = [2, 3, 4] needs 77 operations to become [0,0,0][0, 0, 0]:

$$[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 aa, how many operations are needed to turn all its elements into 00. It can be proved that the elements of aa will all become 00 after a finite number of operations. Can you compute the answer for him?

Input Format

The first line contains a positive integer nn, the length of the array aa.

The second line contains nn non-negative integers a1,a2,…,ana_1, a_2, \ldots, a_n, the elements of the array.

Output Format

One line, a positive integer: the number of operations needed to turn all elements of aa into 00.

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 [2,3,4][2, 3, 4], and the operations proceed as follows:

  1. Maximum is 44 (index 33), smallest non-zero is 22, 4−2=24 - 2 = 2, array becomes [2,3,2][2, 3, 2].
  2. Maximum is 33 (index 22), smallest non-zero is 22, 3−2=13 - 2 = 1, array becomes [2,1,2][2, 1, 2].
  3. Maximum is 22 (several maxima; choose the largest index, i.e. the last 22), smallest non-zero is 11, 2−1=12 - 1 = 1, array becomes [2,1,1][2, 1, 1].
  4. Maximum is 22, smallest non-zero is 11, 2−1=12 - 1 = 1, array becomes [1,1,1][1, 1, 1].
  5. Maximum is 11 (choose the largest index, i.e. the third 11), smallest non-zero is 11, 1−1=01 - 1 = 0, array becomes [1,1,0][1, 1, 0].
  6. Maximum is 11 (index 22), smallest non-zero is 11, 1−1=01 - 1 = 0, array becomes [1,0,0][1, 0, 0].
  7. Maximum is 11 (index 11), smallest non-zero is 11, 1−1=01 - 1 = 0, array becomes [0,0,0][0, 0, 0].

After 77 operations in total, everything becomes 00.

Constraints

For all test cases, 1≤n≤1001 \leq n \leq 100, 0≤ai≤1000 \leq a_i \leq 100.