#include <bits/stdc++.h>
using namespace std;
long long cnt[88];
int dp[20001];
int main() {
  long long W;
  cin >> W;
  for (int i = 1; i <= 8; i++) cin >> cnt[i];
  long long MA = W - 100;
  MA = max(0ll, MA);
  long long sum = 0;
  for (int i = 8; i; i--) {
    long long k = (MA - sum) / i;
    k = min(k, cnt[i] - 111);
    if (k <= 0) continue;
    sum += k * i;
    cnt[i] -= k;
  }
  for (int i = 1; i <= 8; i++) {
    cnt[i] = min(cnt[i], 111ll);
  }
  dp[0] = 1;
  for (int i = 1; i <= 8; i++)
    for (int k = 20001 - 1; k; k--) {
      for (int j = 1; j <= cnt[i]; j++) {
        int first = i * j;
        if (first <= k and dp[k - first]) dp[k] = 1;
      }
    }
  long long ans = sum;
  for (int i = 1; i < 20001; i++)
    if (dp[i]) {
      long long ss = sum + i;
      if (ss <= W) ans = max(ans, ss);
    }
  cout << ans << endl;
}
