#include <bits/stdc++.h>
long long int cnt[8];
long long int use[8];
long long int min(long long int a, long long int b) {
  if (a < b) return a;
  return b;
}
long long int max(long long int a, long long int b) { return -min(-a, -b); }
long long int best = ~(1ll << 63);
int fact[8];
void serc(long long int w, int x) {
  if (x == -1) {
    best = min(best, w);
    return;
  }
  for (int i = -min(use[x], 4); i <= cnt[x] && w - (x + 1) * i >= 0 && best;
       i++) {
    serc(w - (x + 1) * i, x - 1);
  }
}
int main() {
  long long int W, w;
  scanf("%lld", &W);
  for (int i = 0; i < 8; i++) {
    if (i == 0)
      fact[i] = 1;
    else
      fact[i] = (i + 1) * fact[i - 1];
  }
  best = W;
  w = W;
  for (int i = 0; i < 8; i++) {
    scanf("%lld", &cnt[i]);
  }
  for (int i = 7; i >= 0; i--) {
    long long int mul = min(w / (i + 1), cnt[i]);
    w -= (i + 1) * mul;
    cnt[i] -= mul;
    use[i] += mul;
  }
  best = min(best, w);
  serc(w, 7);
  printf("%lld\n", W - best);
  return 0;
}
