#!/usr/bin/env bash

cat - > input_file
K=$(awk 'NR==1{print $1}' input_file)
N=$(awk 'NR==1{print $2}' input_file)


max=$([ "0" -gt "$((K - N))" ] && echo 0 || echo "$((K - N))")

cat input_file|sed 1d|tr ' ' \\n|sort -n|\
awk "NR<=$max"'{max=max+$0}END{print max}' max=0