#include <bits/stdc++.h>
using namespace std;
long long dp[9][9 * 840];
long long num[9];
long long W;
long long ans = 0;
int main() {
  memset(dp, -1, sizeof(dp));
  cin >> W;
  for (int i = 1; i <= 8; i++) cin >> num[i];
  dp[0][0] = 0;
  for (int i = 1; i <= 8; i++) {
    int mx = 840 / i;
    if (mx > num[i]) mx = num[i];
    for (int j = 0; j < 9 * 840; j++) {
      for (int k = 0; k <= mx; k++) {
        int d = j - k * i;
        if (d < 0 || dp[i - 1][d] == -1) continue;
        dp[i][j] = max(dp[i][j], dp[i - 1][d] + (num[i] - k) / (840 / (i)));
      }
    }
  }
  for (int j = 0; j < 840 * 9; j++) {
    if (dp[8][j] != -1 && j <= W)
      ans = max(ans, j + min(dp[8][j], (W - j) / 840) * 840);
  }
  cout << ans << endl;
  return 0;
}
