#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll a[10], ans, w;
void knp(int n, ll s) {
  if (!n) {
    ans = max(ans, s);
    return;
  }
  for (ll k = 8, i = min((w - s) / n, a[n]); k > 0 && i >= 0; i--, k--) {
    knp(n - 1, s + max((ll)0, i * n));
  }
}
int main() {
  cin >> w;
  for (int i = 1; i <= 8; i++) {
    cin >> a[i];
  }
  knp(8, 0);
  cout << ans << endl;
  return 0;
}
