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