#include <bits/stdc++.h>
using namespace std;
long long cnt[10];
int dp[13441];
int czy1[1681];
int czy2[1681];
int main() {
  ios_base::sync_with_stdio(0);
  long long n;
  cin >> n;
  long long suma = 0;
  long long wlk = 0;
  for (int i = 1; i <= 8; i++) {
    cin >> cnt[i];
    suma += i * cnt[i];
    int mod = 1680 / i;
    if (cnt[i] / mod == 0) {
      wlk += cnt[i] / mod;
      cnt[i] %= mod;
    } else {
      wlk += cnt[i] / mod - 1;
      cnt[i] = cnt[i] % mod + mod;
    }
  }
  if (n >= suma) {
    cout << suma;
    return 0;
  }
  dp[0] = 1;
  for (int i = 1; i <= 8; i++) {
    for (int j = 0; j < cnt[i]; j++) {
      for (int k = 13440; k >= i; k--) {
        if (dp[k - i] == 1) dp[k] = 1;
      }
    }
  }
  for (int i = 0; i < 1680; i++) czy1[i] = -1, czy2[i] = -1;
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 1680; j++) {
      if (czy1[j] == -1) {
        if (dp[i * 1680 + j] == 1) czy1[j] = i;
      }
      if (dp[i * 1680 + j] == 1) czy2[j] = i;
    }
  }
  long long wynik = 0;
  long long P = n / 1680;
  long long A = n / 1680;
  A = A * 1680 - 1680;
  for (int i = n % 1680 + 1; i < 1680; i++) {
    if (czy1[i] <= P && czy1[i] != -1) {
      wynik = max(wynik, min(A + i, (wlk + czy2[i]) * 1680 + i));
    }
  }
  A += 1680;
  for (int i = 0; i <= n % 1680; i++) {
    if (czy1[i] <= P && czy1[i] != -1) {
      wynik = max(wynik, min(A + i, (wlk + czy2[i]) * 1680 + i));
    }
  }
  cout << wynik;
}
