#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 5;
const int down = 840;
long long readint() {
  long long x = 0, f = 1;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    (s = getchar());
  }
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + (s ^ 48);
    (s = getchar());
  }
  return x * f;
}
long long cnt[9], sum = 0;
bool dp[9][down * 2 + 5];
signed main() {
  long long w = readint();
  for (long long i = 1; i <= 8; i++) {
    cnt[i] = readint();
    sum += (cnt[i] * i);
  }
  if (sum <= w) {
    return !printf("%lld", sum);
  }
  long long ans = 0;
  for (long long i = 8; i >= 1; i--) {
    if (w <= down) break;
    long long last = w - down;
    long long maxx = min(last / i, max(0ll, cnt[i] - 8));
    cnt[i] -= maxx;
    ans += i * maxx;
    w -= i * maxx;
  }
  dp[0][0] = 1;
  for (long long i = 1; i <= 8; i++) {
    for (long long j = 0; j <= w; j++) {
      for (long long k = 0; k <= cnt[i] && k * i <= j; k++) {
        if (dp[i - 1][j - i * k]) dp[i][j] = 1;
      }
    }
  }
  for (long long i = w; i >= 0; i--) {
    if (dp[8][i]) return !printf("%lld", i + ans);
  }
  cout << ans << endl;
  return 0;
}
