#C2827. Minimum Operations to Achieve Target Sum

    ID: 46186 Type: Default 1000ms 256MiB

Minimum Operations to Achieve Target Sum

Minimum Operations to Achieve Target Sum

You are given a list of integers and a target sum (T). In a single operation, you can decrease any element of the list by 1. Your task is to determine the minimum number of such operations required to reduce the sum of the list to exactly (T). Note that if the current sum is less than (T), it is impossible to reach the target and you must output -1.

For example, if the list is [1, 2, 3, 9] and (T = 10), the sum is 15 and you need 5 operations (since (15 - 10 = 5)) to reach the target.

inputFormat

The input is read from standard input (stdin) and consists of three lines:

  1. The first line contains a single integer (n) representing the number of elements in the list.
  2. The second line contains (n) space-separated integers representing the elements of the list.
  3. The third line contains an integer representing the target sum (T).

outputFormat

Output a single integer to standard output (stdout) which is the minimum number of operations required to achieve the target sum. If it is impossible to reach the target (i.e. the current sum of the list is less than (T)), output -1.## sample

5
1 2 3 4 5
15
0