#include <bits/stdc++.h>
using namespace std;
const int mod = (int)1e9 + 7;
const int inf = (int)1e9;
int main() {
  long long n;
  scanf("%I64d", &n);
  vector<long long> num(9, 0);
  for (int i = 1; i <= 8; ++i) {
    scanf("%I64d", &num[i]);
  }
  n -= 840;
  long long _plus = 0;
  for (int val = 8; val >= 1; --val) {
    if (num[val] <= 8 || val > n) {
      continue;
    }
    long long upd = min(num[val] - 8, n / val);
    num[val] -= upd;
    n -= upd * val;
    _plus += upd * val;
  }
  n += 840;
  vector<vector<int> > d(min(850ll, n) + 2, vector<int>(10, inf));
  for (int j = 8; j >= 1; --j) {
    d[0][j] = 0;
    for (int i = 1; i <= min(850ll, n); ++i) {
      if (d[i][j + 1] != inf) {
        d[i][j] = 0;
      }
      if (i - j < 0) {
        continue;
      }
      if (d[i - j][j] != inf) {
        if (d[i - j][j] < num[j]) {
          d[i][j] = min(d[i][j], d[i - j][j] + 1);
        }
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i <= min(850ll, n); ++i) {
    if (d[i][1] != inf) {
      ans = i;
    }
  }
  printf("%I64d", _plus + ans);
  return 0;
}
