#include <bits/stdc++.h>
using namespace std;
long long w;
long long arr[9];
int f(long long n, int i) {
  if (i == 1) {
    if (n <= arr[1])
      return 1;
    else
      return 0;
  }
  long long minn = (arr[i] >= n / i) ? n - (n / i) * i : n - arr[i] * i;
  for (int j = 0; j < i && minn + j * i <= n; j++) {
    if (f(minn + j * i, i - 1)) return 1;
  }
  return 0;
}
int main() {
  cin >> w;
  for (int i = 1; i <= 8; i++) {
    cin >> arr[i];
  }
  long long top = 0;
  for (int i = 1; i <= 8; i++) top += i * arr[i];
  if (top <= w) {
    cout << top << endl;
  } else {
    for (int i = 0; i <= 8; i++) {
      if (w - i < 0) continue;
      if (f(w - i, 8)) {
        cout << w - i << endl;
        break;
      }
    }
  }
}
