#K49907. Gem Collection Optimization

    ID: 28746 Type: Default 1000ms 256MiB

Gem Collection Optimization

Gem Collection Optimization

Rita has T days to collect gems from a shop offering N distinct types. Each gem has an associated value. However, following a peculiar strategy, Rita collects the gem with the smallest value available on each day. If the number of days exceeds the number of available gem types, she collects all gems exactly once.

Your task is to compute the total sum of the values of the gems that Rita will collect under these rules.

Mathematically, let \(T\) be the number of days and \(N\) be the number of gems. Given an array of gem values \(A = [a_1, a_2, \dots, a_N]\), Rita sorts the array in ascending order and collects the first \(\min(T, N)\) gems. The answer is then given by:

[ \text{Answer} = \sum_{i=1}^{\min(T,N)} a_i ]

inputFormat

The input is read from stdin and has the following format:

  • The first line contains two integers \(T\) and \(N\): the number of days and the number of gem types respectively.
  • The second line contains \(N\) space-separated integers representing the values of the gems.

outputFormat

Output a single integer to stdout representing the total sum of the values of the gems Rita collects.

## sample
4 5
1 2 3 4 5
10