# Uses python3
import sys

bag_size = 10
items = 3
import numpy as np

# define a matrix of 11 columns, 3 items
# fill in the first row
# weight, value pair
# code in the lecture notes
items_list = [(4, 16), (6, 30), (3, 14), (2, 9)]
n_items = len(items_list)
n_cols = 10
matrix = np.zeros((n_items + 1, n_cols + 1))

for item_index in range(1, n_items + 1):
    for weight_index in range(1, n_cols + 1):
        # init by assuming that the most optimal choice is same as the previous choice
        # i.e no item is added since 2 items is optimal already
        matrix[item_index, weight_index] = matrix[item_index - 1, weight_index]
        # Get the current item and attributes
        current_item_weight, current_item_value = items_list[item_index - 1]
        # if the current item is less than the weight treshold,
        if current_item_weight <= weight_index:
            # get the index without the weight in the previous row
            # e.g in the 3rd row, go to the 2nd row and pick the corresponding index
            val = (
                matrix[item_index - 1, weight_index - current_item_weight]
                + current_item_value
            )
            # if the new value is better, assign to new value instead
            if matrix[item_index, weight_index] < val:
                matrix[item_index, weight_index] = val


def optimal_weight(W, w):
    # write your code here
    result = 0
    for x in w:
        if result + x <= W:
            result = result + x
    return result


if __name__ == "__main__":
    input = sys.stdin.read()
    W, n, *w = list(map(int, input.split()))
    print(optimal_weight(W, w))
