L = gets.to_i N = gets.to_i W = gets.split.map(&:to_i) S = W.sort def f(i, s) if i == N or s + S[i] > L i else f(i + 1, s + S[i]) end end ans = f(0, 0) puts ans