#C11527. Sum of the Smallest M Integers After Removal
Sum of the Smallest M Integers After Removal
Sum of the Smallest M Integers After Removal
You are given an array of n integers, along with two additional integers m and x. Your task is to remove all occurrences of x from the array. If, after removal, there are fewer than m integers remaining, print -1. Otherwise, sort the remaining integers in non-decreasing order and output the sum of the smallest m numbers.
In mathematical terms, let \(A = [a_1, a_2, \dots, a_n]\) and define \(A' = \{a \in A \mid a \neq x\}\). If \(|A'| < m\), the answer is -1. Otherwise, let \(A'_s\) be the sorted version of \(A'\) in increasing order, and output \[ \sum_{i=1}^{m} A'_s[i]. \]
inputFormat
The input is read from standard input (stdin) and consists of two lines.
- The first line contains three space-separated integers: n (the number of elements in the array), m (the number of elements to sum), and x (the integer to remove).
- The second line contains n space-separated integers representing the array.
outputFormat
The output is a single integer written to standard output (stdout). This integer is the sum of the smallest m integers in the modified array. If there are fewer than m integers after removal, output -1.
## sample5 2 3
3 1 5 3 2
3