#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
long long a[100], ans, n;
void dfs(long long x, long long now) {
  if (x == 9) {
    ans = max(ans, now);
    return;
  }
  for (long long i = 9, v = min((n - now) / x, a[x]); i; i--, v--) {
    dfs(x + 1, now + max(0ll, v * x));
  }
}
void up() {
  cin >> n;
  for (int i = 1; i <= 8; i++) {
    cin >> a[i];
  }
  dfs(1, 0);
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  up();
  return 0;
}
