L = int(input()) N = int(input()) M = map(int, input().split()) a=0 M.sort() for i in range(N): if L >= a: a = a + M[i] print(a)