#C2937. Find Maximum and Minimum Sums

    ID: 46308 Type: Default 1000ms 256MiB

Find Maximum and Minimum Sums

Find Maximum and Minimum Sums

Given a list of integers, your task is to determine both the maximum and minimum possible sums by selecting exactly \(k\) integers from the list.

The strategy is straightforward: first, sort the list. The maximum sum is obtained by summing the largest \(k\) elements, while the minimum sum is obtained by summing the smallest \(k\) elements.

Mathematically, if the sorted list is \(a_1 \leq a_2 \leq \dots \leq a_n\), then:

  • \( \text{Maximum Sum} = \sum_{i=n-k+1}^{n} a_i \)
  • \( \text{Minimum Sum} = \sum_{i=1}^{k} a_i \)

Your solution should read input from stdin and output the two sums to stdout, separated by a space.

inputFormat

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

  1. The first line contains two space-separated integers \(n\) and \(k\), where \(n\) is the number of integers in the list and \(k\) is the number of integers to choose.
  2. The second line contains \(n\) space-separated integers.

outputFormat

Print two integers separated by a space: the maximum sum and the minimum sum calculated as described above.

## sample
5 3
1 2 3 4 5
12 6