#include <bits/stdc++.h>
using namespace std;
long long w, ans, a[10];
void dfs(long long m, long long sum) {
  if (m == 9) {
    ans = max(ans, sum);
    return;
  }
  for (long long k = 9, i = min((w - sum) / m, a[m]); k; k--, i--)
    dfs(m + 1, sum + max((long long)0, i * m));
}
int main() {
  cin >> w;
  for (int i = 1; i <= 8; i++) scanf("%lld", &a[i]);
  dfs(1, 0);
  cout << ans << endl;
  return 0;
}
