#include <bits/stdc++.h>
using namespace std;
const int MX = 100, MXSUM = 36 * MX + 1, MXK = 8 * MX;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long W;
  cin >> W;
  long long done = 0, total = 0;
  vector<int> coins;
  for (int i = 1; i <= 8; i++) {
    long long x;
    cin >> x;
    x = min(x, (W - done) / i);
    long long take = max(0LL, x - MX);
    done += take * i;
    for (long long ii = take; ii < x; ii++) coins.push_back(i), total += i;
  }
  assert(done <= W);
  assert(total < MXSUM);
  assert(coins.size() <= MXK);
  bitset<MXSUM> bs;
  bs[0] = 1;
  for (int x : coins) bs = bs | (bs << x);
  long long rem = min(MXSUM - 1LL, W - done);
  long long mx = 0;
  for (int i = 0; i <= rem; i++)
    if (bs[i]) mx = i;
  done += mx;
  assert(done <= W);
  cout << done << endl;
}
