#!/usr/bin/env bash

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


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

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