#include <bits/stdc++.h>
using namespace std;
long long w, myAns, ans;
vector<long long> arr(8), pos(8);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> w;
  for (long long i = 0; i < 8; i++) cin >> arr[i];
  for (long long j = 0; j <= 50; j++) {
    for (long long i = 0; i < 8; i++) pos[i] = i + 1;
    do {
      myAns = 0;
      for (long long i = 0; i < 8; i++) {
        long long t = min(max(arr[pos[i] - 1] - j, 0LL), (w - myAns) / pos[i]);
        myAns += t * pos[i];
      }
      ans = max(ans, myAns);
    } while (next_permutation(pos.begin(), pos.end()));
  }
  cout << ans;
  return 0;
}
