#include <bits/stdc++.h>
using namespace std;
long long w, a[9];
long long solve(long long cap, int maxitem) {
  if (maxitem == 0) return 0;
  long long cnt = (cap >= maxitem * a[maxitem]) ? a[maxitem] : cap / maxitem;
  long long x = solve(cap - maxitem * cnt, maxitem - 1) + maxitem * cnt;
  long long y =
      cnt ? solve(cap - maxitem * (cnt - 1), maxitem - 1) + maxitem * (cnt - 1)
          : 0;
  long long z =
      maxitem > 1 && cnt > 2
          ? solve(cap - maxitem * (cnt - 2), maxitem - 1) + maxitem * (cnt - 2)
          : 0;
  return max(max(x, y), z);
}
int main(void) {
  cin >> w;
  for (int i = 1; i < 9; i++) cin >> a[i];
  cout << solve(w, 8);
}
