#include <bits/stdc++.h>
using namespace std;
const long long MAX = 840;
long long arr[9], W, ans;
long long dp[7890];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> W;
  for (int i = 1; i < 9; ++i) {
    cin >> arr[i];
  }
  if (W > 840) {
    for (int i = 1; i < 9; ++i) {
      long long lef = max(arr[i] - 840 / i, (long long)0);
      long long g = (W - 840 - ans) / i;
      g = min(g, lef);
      if (g < 0) continue;
      ans += g * i;
      arr[i] -= g;
    }
  }
  dp[0] = 1;
  for (int i = 1; i < 9; ++i) {
    for (int j = 0; j < min(arr[i], (long long)840 / i); ++j) {
      for (int k = 6789; k >= i; --k) {
        if (dp[k - i]) dp[k] = 1;
      }
    }
  }
  for (int k = 6789;; --k) {
    if (dp[k] && ans + k <= W) {
      cout << ans + k << '\n';
      return 0;
    }
  }
}
