# frozen_string_literal: true N, K = gets.chomp.split.map(&:to_i) As = N.times.map { gets.to_i } result = 0 remain = K weights = As loop do weights = weights.select { |i| i <= remain }.sort break if weights.empty? w = weights.pop result += w remain -= w end puts result