#include <bits/stdc++.h>
using namespace std;
const long long L = 840, N = 8, maxn = N * L;
long long d[N + 1][maxn + 1];
inline void Max(long long &a, long long b) {
  if (a < b) a = b;
}
int main() {
  long long w;
  scanf("%lld", &w);
  memset(d[0], -1, sizeof(d[0]));
  d[0][0] = 0;
  for (int i = 1; i <= N; ++i) {
    long long x;
    scanf("%lld", &x);
    long long mod = L / i;
    memcpy(d[i], d[0], sizeof(d[i]));
    for (long long k = min(mod - 1, x); k >= 0; --k) {
      for (long long prev = maxn - k * i; prev >= 0; --prev)
        if (d[i - 1][prev] >= 0)
          Max(d[i][prev + k * i], d[i - 1][prev] + (x - k) / mod);
    }
  }
  long long ans = 0;
  for (long long r = min(maxn, w); r >= 0; --r) {
    long long left = w - r;
    long long cnt = min(d[N][r], left / L);
    Max(ans, cnt * L + r);
  }
  printf("%lld\n", ans);
}
