#include <bits/stdc++.h>
using namespace std;
long long cnt[10];
long long w, ans;
void dfs(int pos, long long c) {
  long long rec;
  if (pos > 8) {
    ans = max(ans, w - c);
    return;
  }
  rec = min(cnt[pos], c / pos);
  for (long long i = rec; i >= max(rec - 8, 0LL); i--)
    dfs(pos + 1, c - i * pos);
}
int main() {
  cin >> w;
  for (int i = 1; i <= 8; i++) cin >> cnt[i];
  ans = 0;
  dfs(1, w);
  cout << ans << '\n';
  return 0;
}
