def main(): # 大きな箱の幅入力 l = input() # ブロック数入力 n = input() # 各ブロックの幅入力 w = int(input().split(" ")) # ブロック幅をソート s = w.sort(reverse=True) for su in s: if l < m: m += su print(m)