#include <bits/stdc++.h>
using namespace std;
vector<int> knapsack(long long lim, vector<int> allItems) {
  vector<bool> mark;
  int mx = 0;
  for (auto it : allItems) mx += it;
  mx = min((long long)mx, lim);
  mark.assign(mx + 1, 0);
  mark[0] = true;
  for (auto it : allItems) {
    for (int i = (int)mx; i >= (int)it; i--) {
      mark[i] = max(mark[i], mark[i - it]);
    }
  }
  vector<int> ret;
  for (int i = (int)0; i <= (int)mx; i++)
    if (mark[i]) ret.push_back(i);
  return ret;
}
long long W;
long long cnt[9];
long long max840 = 0;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> W;
  for (int i = (int)1; i <= (int)8; i++) cin >> cnt[i];
  for (int i = (int)1; i <= (int)8; i++) {
    int blockSize = 840 / i;
    if (cnt[i] < blockSize) continue;
    max840 += cnt[i] / blockSize;
    cnt[i] %= blockSize;
    if (cnt[i] < blockSize) max840 -= 1, cnt[i] += blockSize;
  }
  vector<int> allItems;
  for (int i = (int)1; i <= (int)8; i++) {
    int rem = cnt[i];
    for (int k = (int)0; k <= (int)60; k++) {
      if (rem < (1LL << k)) {
        allItems.push_back(rem * i);
        break;
      } else {
        rem -= (1LL << k);
        allItems.push_back((1LL << k) * i);
      }
    }
  }
  vector<int> allCreatedValue = knapsack(W, allItems);
  long long ans = 0;
  for (auto it : allCreatedValue) {
    long long left = W - it;
    left -= 1LL * min(left / 840, max840) * 840;
    ans = max(ans, W - left);
  }
  cout << ans << '\n';
}
