#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long w, a[12], sum, ans, cur;
void solve(long long second, long long i) {
  if (i <= 0) {
    ans = max(ans, cur - second);
    return;
  }
  long long tmp = min(second / i, a[i]), mn;
  for (int j = 0; j < 8 && tmp - j >= 0; j++) {
    solve(second - tmp * i, i - 1);
    tmp--;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> w;
  for (int i = 1; i <= 8; i++) cin >> a[i], sum += 1ll * a[i] * i;
  if (sum <= w) return cout << sum << '\n', 0;
  for (int i = 0; i < 8; i++) {
    cur = w - i;
    if (i <= w) solve(w - i, 8);
  }
  cout << ans << '\n';
  return 0;
}
