#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int MAXN = 1e6 + 10;
const long long MOD = 1e9 + 7;
const long long MOD2 = 998244353;
const long long INF = 8e18;
const int LOG = 22;
long long pw(long long a, long long b, long long mod) {
  return (!b ? 1
             : (b & 1 ? (a * pw(a * a % mod, b / 2, mod)) % mod
                      : pw(a * a % mod, b / 2, mod)));
}
long long v[MAXN];
long long LCM = 840;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long w, n = 0;
  cin >> w;
  for (long long i = 1; i <= 8; i++) {
    cin >> v[i];
    long long T = LCM / i;
    if (v[i] < 2 * T) continue;
    n += (v[i] - T - (v[i] % T)) / T;
    v[i] = T + (v[i] % T);
  }
  long long Ans = 0;
  bitset<20000> Can;
  Can[0] = 1;
  for (long long i = 1; i <= 8; i++) {
    for (int j = 0; j < v[i]; j++) {
      Can |= (Can << i);
    }
  }
  for (long long i = 0; i <= min(w, 19999ll); i++) {
    if (Can[i] == 0) continue;
    long long cu = w - i;
    Ans = max(Ans, (min(n, (cu / LCM)) * LCM) + i);
  }
  cout << Ans;
  return 0;
}
