N = int(input()) P = list(map(int, input().split())) Q = [3, 5, 10] dp = [0] * (N + 1) dp[0] = 0 for i in range(N): for p, q in zip(P, Q): ni = i + q if N < ni: continue dp[ni] = max(dp[ni], dp[i] + p) print(dp[N])