N = gets.to_i A, B, C = gets.chomp.split(" ").map(&:to_i) dp = Array.new(N + 1, 0) for i in 0..N - 3 dp[i + 3] = [dp[i + 3], dp[i] + A].max() end for i in 0..N - 5 dp[i + 5] = [dp[i + 5], dp[i] + B].max() end for i in 0..N - 10 dp[i + 10] = [dp[i + 10], dp[i] + C].max() end p dp[N]