#include <bits/stdc++.h>
using namespace std;
long long w, a[9], ans;
void rua(long long i, long long x) {
  if (i > 8) {
    ans = max(ans, x);
    return;
  }
  for (long long j = min(a[i], (w - x) / i), k = 1; j >= 0 && k <= 8; j--, k++)
    rua(i + 1, x + i * j);
}
int main() {
  cin >> w;
  for (long long i = 1; i <= 8; i++) cin >> a[i];
  rua(1, 0);
  return printf("%I64d\n", ans), 0;
}
