#include <bits/stdc++.h>
using namespace std;
long long int sum = 1e18;
long long int a[10];
void fun(long long int cur, long long int x) {
  if (x < 0) return;
  sum = min(sum, x);
  if (cur == 0) return;
  long long int p = min((x / cur), a[cur]);
  for (long long int i = max(0LL, p - cur + 1); i <= p; i++)
    fun(cur - 1, x - (i * cur));
}
int main() {
  long long int w;
  cin >> w;
  for (int i = 1; i <= 8; i++) cin >> a[i];
  fun(8, w);
  cout << w - sum << endl;
}
