#C2795. Sum of Missing Numbers

    ID: 46150 Type: Default 1000ms 256MiB

Sum of Missing Numbers

Sum of Missing Numbers

Given an array of non-negative integers, determine the sum of all missing numbers in the contiguous range from \(0\) to \(\max(arr)\). That is, let \(S = \{0, 1, 2, \ldots, \max(arr)\}\) and let \(A\) be the set of numbers in the array. You need to compute \(\sum_{x \in S \setminus A} x\). If the array is empty, the output should be \(0\).

For example, for the input array [2, 3, 7, 4, 9], the full range is \(\{0,1,2,3,4,5,6,7,8,9\}\) and the missing numbers are \(\{0,1,5,6,8\}\) whose sum is \(20\).

inputFormat

The input is read from stdin as follows:

  • The first line contains an integer \(n\) representing the number of elements in the array.
  • The second line contains \(n\) space-separated non-negative integers. If \(n = 0\), the second line may be empty.

outputFormat

Output a single integer to stdout representing the sum of all missing numbers in the complete range from (0) to (\max(arr)).## sample

5
2 3 7 4 9
20