#include <bits/stdc++.h>
using namespace std;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
long long v[6], w[6], m[6];
long long F[6];
long long val[1000000];
signed main() {
  long long k;
  cin >> k;
  long long n = 6;
  for (long long i = 0; i < (long long)(n); ++i) cin >> F[i];
  long long P = 1;
  for (long long i = 0; i < (long long)(n); ++i) {
    v[i] = F[i];
    w[i] = P * 3;
    m[i] = 3 * k;
    P *= 10;
  }
  for (long long i = 0; i < (long long)(1000000); ++i) {
    long long vv = i;
    for (long long j = 0; j < (long long)(6); ++j) {
      long long kk = vv % 10;
      if (kk % 3 == 0) {
        val[i] += kk / 3 * F[j];
      }
      vv /= 10;
    }
  }
  long long dp[1000010] = {};
  long long INF = (1LL << 60);
  Fill(dp, -INF);
  dp[0] = 0;
  long long q;
  cin >> q;
  long long W;
  cin >> W;
  for (long long i = 0; i < (long long)(n); ++i) {
    for (long long k = 0; m[i] > 0; k++) {
      long long key = min(m[i], (long long)(1 << k));
      m[i] -= key;
      for (long long j = W; j >= key * w[i]; j--) {
        dp[j] = max(dp[j], dp[j - key * w[i]] + key * v[i]);
      }
    }
  }
  long long ans = dp[W];
  Fill(dp, -INF);
  dp[0] = 0;
  for (long long i = 0; i < (long long)(n); ++i) {
    m[i] = 3 * (k - 1);
  }
  for (long long i = 0; i < (long long)(n); ++i) {
    for (long long k = 0; m[i] > 0; k++) {
      long long key = min(m[i], (long long)(1 << k));
      m[i] -= key;
      for (long long j = W; j >= key * w[i]; j--) {
        dp[j] = max(dp[j], dp[j - key * w[i]] + key * v[i]);
      }
    }
  }
  for (long long i = 0; i < (long long)(W + 1); ++i) {
    ans = max(ans, dp[i] + val[W - i]);
  }
  cout << ans << "\n";
  return 0;
}
