#include <bits/stdc++.h>
using namespace std;
long long score(long long x, vector<long long> &f) {
  long long score = 0;
  int tenPow = 1;
  for (int pow = 0; pow < 6; pow++, tenPow *= 10) {
    int rem = (x / tenPow) % 10;
    if (rem % 3 == 0) {
      score += (rem / 3) * f[pow];
    }
  }
  return score;
}
void compactify(long long weight, long long value, long long count,
                vector<pair<long long, long long> > &objList) {
  for (int i = 1; i < count; i *= 2) {
    objList.push_back(pair<long long, long long>(value * i, weight * i));
    count -= i;
  }
  objList.push_back(pair<long long, long long>(value * count, weight * count));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int k, q, n;
  vector<long long> f(6);
  cin >> k >> f[0] >> f[1] >> f[2] >> f[3] >> f[4] >> f[5] >> q >> n;
  vector<long long> dp(n + 1, -(1LL << 62));
  dp[0] = 0;
  vector<pair<long long, long long> > objects;
  int tenPow = 1;
  for (int pow = 0; pow < 6; pow++, tenPow *= 10) {
    compactify(3 * tenPow, f[pow], 3 * (k - 1), objects);
  }
  for (pair<long long, long long> p : objects) {
    for (int i = n; i >= p.second; i--) {
      dp[i] = max(dp[i], dp[i - p.second] + p.first);
    }
  }
  long long best = 0;
  for (int i = 0; i <= n; i += 3) {
    best = max(best, score(n - i, f) + dp[i]);
  }
  cout << best << endl;
  return 0;
}
