malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     string s;     cin >> s;     char a, b;     a = s[0];     b = s[s.length() - 1];     int j = s.length() - 2;     int i = 0;     int maxcount = 0;     while (j - i >= 0) {       int k1 = j - i;       int k = s[k1];       int q1 = s.length() - 1;       int q = s[q1];       int count = 0;       while (k1 > 0 && q == k) {         count++;         q1--;         q = s[q1];         k1--;         k = s[k1];       }       if (q == k) {         count++;       } else {         k1 = -1;       }       if (maxcount < count && k1 == 0) {         maxcount = count;       }       i++;     }     string x = s.substr(0, s.length() - maxcount);     string y = s.substr(s.length() - maxcount, s.length());     for (int i = 0; i < k; i++) {       cout << x;     }     cout << y;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, d, t, result, stored = 0;     cin >> n >> d;     for (int i = 0; i < n; i++) {       cin >> t;       stored += t;     }     d -= (n - 1) * 10;     if (d - stored >= 0) {       cout << (n - 1) * 2 + (d - stored) / 5;     } else       cout << -1;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solution() {     long long n;     long long k;     cin >> n >> k;     vector<long long> v(n);     for (long long i = 0; i < n; i++) cin >> v[i];     long long sum = 0;     sort(v.begin(), v.end());     for (long long i = 0; i < (k); i++) {       sum += (v[n - k - 1 - i] / v[n - 1 - i]);     }     for (long long i = 0; i < n - 2 * k; i++) sum += v[i];     cout << sum << endl;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t;     cin >> t;     while (t--) solution();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int Get() {     char c;     while (c = getchar(), (c <  0  || c >  9 ) && c !=  - )       ;     bool Flag = (c ==  - );     if (Flag) c = getchar();     int X = 0;     while (c >=  0  && c <=  9 ) {       X = X * 10 + c - 48;       c = getchar();     }     return Flag ? -X : X;   }   int main() {     int N = Get(), Sum = 0;     for (int i = 0; i < N; i++) Sum += Get();     printf( %d n , (Sum % N) ? (N - 1) : N);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 10;   namespace StrAlgo {   vector<int> Kmp(string s) {     int n = (int(s.size()));     vector<int> ans(n + 1);     for (int i = 2; i <= n; i++) {       ans[i] = ans[i - 1];       while (ans[i] > 0 && s[ans[i]] != s[i - 1]) ans[i] = ans[ans[i]];       ans[i] += s[ans[i]] == s[i - 1];     }     return ans;   }   vector<int> Z(string s) {     int n = (int(s.size()));     vector<int> ans(n + 1);     int L = -1, R = -1;     for (int i = 2; i <= n; i++) {       if (i < R)         ans[i] = min(ans[i - L], R - i);       else         L = i, R = i;       while (i + ans[i] <= n && s[i + ans[i] - 1] == s[ans[i] - 1]) ans[i]++;       if (i + ans[i] > R) L = i, R = i + ans[i];     }     return ans;   }   };  // namespace StrAlgo   void print(string s, int len) {     if (len == 0)       cout <<  Just a legend n ;     else       cout << s.substr((int(s.size())) - len) <<   n ;     exit(0);   }   int cnt[maxn];   int main() {     ios_base::sync_with_stdio(0), cin.tie(), cout.tie();     string s;     cin >> s;     vector<int> kmp = StrAlgo::Kmp(s);     for (int x : kmp) cnt[x]++;     if (cnt[kmp.back()] > 1) print(s, kmp.back());     print(s, kmp[kmp.back()]);   }
#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const double EPS = 1e-9;   const long long MOD = 100000000;   const int N = 300009;   const int N2 = 300009;   void fast() { std::ios_base::sync_with_stdio(0); }   int main() {     long long n, m;     long long x = 0;     scanf( %lld%lld , &n, &m);     vector<long long> b(n), g(m);     for (long long &i : b) {       scanf( %lld , &i);       x += i * m;     }     for (long long &i : g) scanf( %lld , &i);     sort(b.rbegin(), b.rend());     sort(g.rbegin(), g.rend());     long long i = 0, cnt = 0;     long long ans = 0;     if (g[m - 1] < b[0] || b[0] > g[m - 1]) {       puts( -1 );       return 0;     }     for (long long j = 0; j < m; j++) {       if (cnt == m - 1) i++, cnt = 0;       if (g[j] > b[0]) {         ans += g[j] - b[i];         cnt++;       }     }     printf( %lld , ans + x);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 21;   int n;   long long s, a[N], down = 1, ans;   void exgcd(long long a, long long b, long long &x, long long &y) {     if (b == 0) {       x = 1, y = 0;       return;     }     exgcd(b, a % b, y, x);     y -= a / b * x;   }   long long inv(long long x) {     long long res, y;     exgcd(x, 1000000007, res, y);     return res;   }   long long C(long long u, long long d) {     if (d < u) return 0;     long long res = 1;     for (long long i = d; i > d - u; i--) (res *= i % 1000000007) %= 1000000007;     return res * down % 1000000007;   }   int main() {     std::ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> s;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 1; i < n; i++) (down *= i) % 1000000007;     down = inv(down);     for (int i = 0; i < (1 << n); i++) {       long long f = 1, d = s + n - 1, u = n - 1;       for (int j = 0; j < n; j++)         if (i & (1 << j)) {           d -= a[j] + 1;           f *= -1;         }       (ans += f * C(u, d)) %= 1000000007;     }     cout << (ans + 1000000007) % 1000000007;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 500001;   const int oo = 1e9;   const int LOGN = 20;   const int MOD = 1000000007;   const double PI = acos(-1);   int n, a[N];   int main() {     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     int mn = oo, idx = -1;     for (int t = 1; t <= 101; ++t) {       int cur = 0;       for (int j = 0; j < n; ++j) {         int cst = max(0, abs(a[j] - t) - 1);         cur += cst;       }       if (cur < mn) {         mn = cur;         idx = t;       }     }     cout << idx <<     << mn << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const double EPS = 1e-6;   const int MOD = 1e9 + 7;   const int MAXN = 1100001;   const double PI = acos(-1.0);   long long ipt[MAXN];   long long lsum[MAXN], rsum[MAXN];   long long sum[MAXN];   int main() {     long long n, len;     while (cin >> n >> len) {       memset(sum, 0, sizeof(sum));       memset(lsum, 0, sizeof(lsum));       memset(rsum, 0, sizeof(rsum));       for (int i = 0; i < (n); ++i) cin >> ipt[i];       for (int i = (1); i < (n); ++i) {         sum[i] += sum[i - 1] + ipt[i] - ipt[i - 1];       }       long long group = 0;       for (long long i = 0; i + len < n; i += len) {         long long l = len * group, r = l + len;         if (group) lsum[group] = lsum[group - 1];         lsum[group] += (sum[r] - sum[l]) * (group + 1);         group++;       }       group = 0;       for (long long i = n - 1; i - len >= 0; i -= len) {         long long r = len * group, l = r + len;         l = n - 1 - l;         r = n - 1 - r;         if (group) rsum[group] = rsum[group - 1];         rsum[group] += (sum[r] - sum[l]) * (group + 1);         group++;       }       long long ans = 1e18;       for (long long i = 0; i < n; i++) {         long long t = 0;         if (i / len > 0) t += lsum[i / len - 1] * 2;         if ((n - 1 - i) / len > 0) t += rsum[(n - 1 - i) / len - 1] * 2;         t += (sum[i] - sum[i / len * len]) * 2 * (i / len + 1);         t += (sum[n - 1 - (n - 1 - i) / len * len] - sum[i]) * 2 *              ((n - 1 - i) / len + 1);         ans = min(ans, t);       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using cat = long long;   int main() {     cin.sync_with_stdio(0);     cin.tie(0);     cout << fixed << setprecision(10);     int N, MX = 500000;     cin >> N;     vector<cat> A(N);     for (int i = 0; i < N; i++) cin >> A[i];     sort(begin(A), end(A));     vector<int> cnt(MX + 1, 0), cnt_ps(MX + 1, 0);     for (int i = 0; i < N; i++) cnt[A[i]]++;     for (int i = 0; i <= MX; i++) cnt_ps[i] = (i ? cnt_ps[i - 1] : 0) + cnt[i];     cat ans = 0;     for (int y = 2; y <= MX; y++) {       cat cnt_y = 0;       for (int i = 1; i <= MX / y; i++)         cnt_y += 1LL * (cnt_ps[min(MX, y * i + y - 1)] - cnt_ps[y * i - 1]) * i;       if (cnt_y < 2) continue;       int min_x = min(1LL * y, cnt_y - 2), max_x = min(MX + 1LL, cnt_y + 1);       while (max_x - min_x > 1 && 1LL * (max_x - 1) * y > ans) {         int x = (min_x + max_x) / 2;         if (2 * (x / y) + x > cnt_y) {           max_x = x;           continue;         }         if (2 * x <= MX) {           int cnt2_r = 0;           for (int i = (2 * x) / y; i <= MX / y; i++)             cnt2_r += cnt_ps[min(MX, y * i + y - 1)] -                       cnt_ps[min(MX, max(0, y * i + (2 * x) % y - 1))];           if (cnt2_r >= 1 && x + (2 * x) / y <= cnt_y) {             min_x = x;             continue;           }           if (cnt_ps[MX] > cnt_ps[2 * x - 1] &&               x + (2 * x + y - 1) / y <= cnt_y) {             min_x = x;             continue;           }         }         int cnt_r = 0;         for (int i = x / y; i <= MX / y; i++)           cnt_r += cnt_ps[min(MX, y * i + y - 1)] -                    cnt_ps[min(MX, max(0, y * i + x % y - 1))];         if (cnt_r >= 2) {           min_x = x;           continue;         }         if (cnt_ps[MX] >= 2 + cnt_ps[x - 1] &&             x + 2 * ((x + y - 1) / y) - cnt_r <= cnt_y) {           min_x = x;           continue;         }         max_x = x;       }       if (min_x >= 2) ans = max(ans, 1LL * min_x * y);     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 4e4;   int main() {     ios::sync_with_stdio(false);     int n;     while (cin >> n) {       int a[12];       for (int i = 0; i < n; ++i) cin >> a[i];       sort(a, a + n);       long long p = clock();       while (clock() - p < 1400)         ;       cout << a[0];       for (int i = 1; i < n; ++i) cout <<     << a[i];       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   class DTrie {     int N = 30;     struct node {       node* left = nullptr;       node* right = nullptr;       int cnt = 0;     };     void _add(node* cur, int a, int p) {       cur->cnt++;       if (p == -1) return;       if (a & (1 << p)) {         if (cur->right == nullptr) cur->right = new node();         _add(cur->right, a, p - 1);       } else {         if (cur->left == nullptr) cur->left = new node();         _add(cur->left, a, p - 1);       }     }     int _get(node* cur, int a, int p) {       cur->cnt--;       if (p == -1) return 0;       int result = 0;       if (a & (1 << p)) {         if (cur->right == nullptr || cur->right->cnt == 0)           result = _get(cur->left, a, p - 1);         else           result = (1 << p) + _get(cur->right, a, p - 1);       } else {         if (cur->left == nullptr || cur->left->cnt == 0)           result = (1 << p) + _get(cur->right, a, p - 1);         else           result = _get(cur->left, a, p - 1);       }       return result;     }     node* root = nullptr;       public:     DTrie() { root = new node(); }     void add(int a) { _add(root, a, N); }     int get(int a) { return _get(root, a, N); }   };   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     vector<int> a(n), p(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     for (int i = 0; i < n; i++) {       cin >> p[i];     }     DTrie dt;     for (int i = 0; i < n; i++) {       dt.add(p[i]);     }     for (int i = 0; i < n; i++) {       cout << (a[i] ^ dt.get(a[i])) <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int i = 0; i < t; i++) {       int s, tens, sum = 0;       cin >> s;       for (; s > 0;) {         tens = s / 10;         s -= tens * 10;         s += tens;         sum += tens * 10;         if (s >= 10 && s <= 18) {           s -= 10;           s++;           sum += 10;           if (s) {             sum += s;             s = 0;           }         } else if (s <= 9) {           sum += s;           s = 0;         }       }       cout << sum << endl;     }     return 0;   }
#include <bits/stdc++.h>   using std::map;   using std::max;   using std::min;   using std::set;   using std::sort;   using std::swap;   using std::vector;   inline int readInt() {     int ans = 0;     char c;     do c = getchar();     while (!isdigit(c));     while (isdigit(c)) {       ans = ans * 10 + c -  0 ;       c = getchar();     }     return ans;   }   const int N = 55;   int A[N][N];   int main() {     int n = readInt();     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) A[i][j] = readInt();     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j)         if (A[i][j] != 1) {           bool ok = false;           for (int k = 0; k < n; ++k)             if (i != k) {               for (int l = 0; l < n; ++l)                 if (j != l && A[i][j] == A[k][j] + A[i][l]) {                   ok = true;                   break;                 }               if (ok) break;             }           if (!ok) return puts( NO ), 0;         }     return puts( YES ), 0;   }
#include <bits/stdc++.h>   const int N = 4e6 + 10;   int idx[N], rank[N];   struct Point {     int x, y;   } A[N], Line[N];   inline double slope(Point a) {     return double(A[a.x].y - A[a.y].y) / double(A[a.x].x - A[a.y].x);   }   inline long long area(Point A, Point B, Point C) {     return fabs(1LL * (A.x - C.x) * (B.y - C.y) -                 1LL * (B.x - C.x) * (A.y - C.y));   }   inline int find1(int l, int r, int u, int v, long long s) {     int res = -1;     while (l <= r) {       int mid = (l + r) >> 1;       if (area(A[idx[mid]], A[u], A[v]) >= s)         l = mid + 1, res = mid;       else         r = mid - 1;     }     return res != -1 && area(A[idx[res]], A[u], A[v]) == s ? res : -1;   }   inline int find2(int l, int r, int u, int v, long long s) {     int res = -1;     while (l <= r) {       int mid = (l + r) >> 1;       if (area(A[idx[mid]], A[u], A[v]) <= s)         l = mid + 1, res = mid;       else         r = mid - 1;     }     return res != -1 && area(A[idx[res]], A[u], A[v]) == s ? res : -1;   }   inline int solve(int u, int v, int n, long long s) {     if (rank[u] > rank[v]) std::swap(u, v);     int res1 = find1(1, rank[u] - 1, u, v, s);     if (res1 != -1) return res1;     int res2 = find2(rank[v] + 1, n, u, v, s);     if (res2 != -1) return res2;     std::swap(rank[u], rank[v]), std::swap(idx[rank[u]], idx[rank[v]]);     return 0;   }   int main() {     int n, tot = 0;     long long s;     std::cin >> n >> s;     s <<= 1;     for (int i = 1; i <= n; ++i)       std::cin >> A[i].x >> A[i].y, idx[i] = i, rank[i] = i;     std::sort(A + 1, A + n + 1, [&](const Point &a, const Point &b) {       return a.x < b.x || a.x == b.x && a.y < b.y;     });     for (int i = 1; i <= n; ++i)       for (int j = i + 1; j <= n; ++j) Line[++tot] = Point{i, j};     std::sort(Line + 1, Line + tot + 1, [&](const Point &a, const Point &b) {       int x1 = A[a.y].x - A[a.x].x, y1 = A[a.y].y - A[a.x].y;       int x2 = A[b.y].x - A[b.x].x, y2 = A[b.y].y - A[b.x].y;       return 1LL * x1 * y2 - 1LL * x2 * y1 > 0;     });     for (int i = 1; i <= tot; ++i) {       int res = solve(Line[i].x, Line[i].y, n, s);       if (res)         return printf( Yes n%d %d n%d %d n%d %d n , (A[Line[i].x]).x,                       (A[Line[i].x]).y, (A[Line[i].y]).x, (A[Line[i].y]).y,                       (A[idx[res]]).x, (A[idx[res]]).y),                0;     }     return puts( No ), 0;   }
#include <bits/stdc++.h>   using namespace std;   int N;   pair<int, int> Result[100005];   bool Use[100005];   int counter, Array[100005];   void buildArray(int prime) {     Array[0] = 0;     for (int k = prime; k <= N; k += prime) {       if (Use[k] == 0) {         Array[++Array[0]] = k;         Use[k] = 1;       }     }   }   void makeArrayValid() {     if (Array[0] % 2 == 0) return;     Use[Array[2]] = 0;     swap(Array[Array[0]], Array[2]);     Array[0]--;   }   void addToResult() {     int i;     for (i = 1; i <= Array[0]; i += 2) {       Result[++counter].first = Array[i];       Result[counter].second = Array[i + 1];     }   }   void Eratostenes() {     int i, j;     int last = 3;     for (i = 3; i <= N / 2; i += 2) {       if (Use[i] == 1) continue;       buildArray(i);       makeArrayValid();       addToResult();     }     buildArray(2);     makeArrayValid();     addToResult();   }   int main() {     cin >> N;     Eratostenes();     cout << counter <<   n ;     for (int i = 1; i <= counter; i++)       cout << Result[i].first <<     << Result[i].second <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     vector<long long> a(n);     long long i, j;     for (i = 0; i < n; i++) cin >> a[i];     sort(a.begin(), a.end());     for (i = 0; i < n; i++) {       for (j = i + 1; j < n; j++) {         if (a[i] == 0) continue;         if (a[j] % a[i] == 0) {           a[j] = 0;         }       }     }     long long cnt = 0;     for (i = 0; i < n; i++)       if (a[i] > 0) cnt++;     cout << cnt << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int a[10][10];     for (int i = 0; i < 3; i++) {       for (int j = 0; j < 3; j++) {         cin >> a[i][j];       }     }     int x1 = a[0][1] + a[0][2];     int x2 = a[1][0] + a[1][2];     int x3 = a[2][0] + a[2][1];     int y = (x1 + x2 + x3) / 2;     a[0][0] = y - x1;     a[1][1] = y - x2;     a[2][2] = y - x3;     for (int i = 0; i < 3; i++) {       for (int j = 0; j < 3; j++) {         cout << a[i][j] <<    ;       }       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct point {     double x, y;     point(double x = 0, double y = 0) : x(x), y(y) {}     point operator-(const point &b) const { return point(x - b.x, y - b.y); }     double operator*(const point &b) const { return x * b.y - y * b.x; }   };   point p[200050];   int a[200050], q[200050];   double sum[200050], t[200050], E[200050], dp[55][200050];   bool check(int i, int j, double v) {     return p[j].y - p[i].y <= (p[i].x - p[j].x) * v;   }   int main() {     int n, k;     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (int i = 1; i <= n; ++i) {       sum[i] = sum[i - 1] + a[i];       t[i] = t[i - 1] + 1.0 / a[i];       E[i] = E[i - 1] + sum[i] / a[i];     }     for (int i = 1; i <= n; ++i) dp[1][i] = E[i];     for (int x = 2; x <= k; ++x) {       int head = 0, tail = 0;       q[tail++] = 0;       for (int i = 1; i <= n; ++i) {         if (i < x) {           p[i] = point(-sum[i], sum[i] * t[i] - E[i] + dp[x - 1][i]);           while (head + 1 < tail &&                  (p[q[tail - 1]] - p[q[tail - 2]]) * (p[i] - p[q[tail - 2]]) >= 0)             tail--;           q[tail++] = i;         }         while (head + 1 < tail && check(q[head], q[head + 1], t[i])) head++;         int j = q[head];         dp[x][i] = p[j].x * t[i] + p[j].y + E[i];         p[i] = point(-sum[i], sum[i] * t[i] - E[i] + dp[x - 1][i]);         while (head + 1 < tail &&                (p[q[tail - 1]] - p[q[tail - 2]]) * (p[i] - p[q[tail - 2]]) >= 0)           tail--;         q[tail++] = i;       }     }     printf( %.9f n , dp[k][n]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e9;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     int a[n + 5];     long long ans = 0;     for (int i = 1; i <= n; i++) {       cin >> a[i];       ans += a[i] * a[i];     }     sort(a + 1, a + n + 1);     for (int i = 1; i <= n / 2; i++) {       ans += 2 * a[i] * a[n - i + 1];     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const int maxn = (1 << 22) + 5;   bool vis[maxn << 2][2];   int n, m;   int a[maxn];   bool p[maxn];   void dfs(int id, int s) {     if (vis[id][s]) return;     vis[id][s] = 1;     for (int i = 0; i < n; i++) {       if (id & (1 << i)) {         dfs(id ^ (1 << i), s);       }     }     if (p[id]) {       if (vis[id][0]) return;       vis[id][0] = 1;       dfs((1 << n) - 1 - id, 1);     }   }   int main() {     scanf( %d%d , &n, &m);     memset(vis, 0, sizeof(vis));     memset(p, 0, sizeof(p));     for (int i = 1; i <= m; i++) {       int x;       scanf( %d , a + i);       p[a[i]] = 1;     }     int ans = 0;     for (int i = 1; i <= m; i++) {       if (!vis[a[i]][0]) {         vis[a[i]][0] = 1;         ans++;         dfs((1 << n) - 1 - a[i], 1);       }     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   int main(int argc, char* argv[]) {     int t, cups = 0, medals = 0, n, shelives_needed = 0;     for (int i = 0; i < 3; i++) {       scanf( %d , &t);       cups += t;     }     for (int i = 0; i < 3; i++) {       scanf( %d , &t);       medals += t;     }     scanf( %d , &n);     if (cups % 5 != 0) {       shelives_needed += cups / 5 + 1;     } else {       shelives_needed += cups / 5;     }     if (medals % 10 != 0) {       shelives_needed += medals / 10 + 1;     } else {       shelives_needed += medals / 10;     }     if (shelives_needed > n) {       printf( NO n );     } else {       printf( YES n );     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx )   using namespace std;   int a, b, c, d, n, m, k;   long long pc[1000001];   long long dp[7][333335];   int f[6];   const int N = 333333;   const long long infll = (long long)1000000000 * 1000000000;   inline void upd(long long& a, long long b) {     if (a < b) a = b;   }   long long solve(int n) {     long long ans = pc[n];     for (int _n((n)-1), i(0); i <= _n; i++) {       int rem = n - i;       if (rem % 3) continue;       ans = max(ans, pc[i] + dp[5][rem / 3]);     }     return ans;   }   int main() {     scanf( %d , &k);     --k;     for (int _n((6) - 1), i(0); i <= _n; i++) scanf( %d , &f[i]);     pc[0] = 0;     for (int _n((999999)), i(1); i <= _n; i++) {       int t = i;       long long a = 0;       for (int _n((6) - 1), j(0); j <= _n; j++) {         int dig = t % 10;         if (dig == 3 || dig == 6 || dig == 9) {           a += (long long)f[j] * (dig / 3);         }         t /= 10;       }       pc[i] = a;     }     for (int _n((7) - 1), i(0); i <= _n; i++)       for (int _n((N + 2) - 1), j(0); j <= _n; j++) dp[i][j] = -8 * infll;     for (int _n((N + 1) - 1), i(0); i <= _n; i++) {       if (i > 3 * k) break;       dp[0][i] = (long long)f[0] * i;     }     auto ev1 = [&](int i, int s) {       return (long long)i * (f[1] - 10LL * f[0]) + (long long)s * f[0];     };     for (int _n((N + 1) - 1), i(0); i <= _n; i++) {       if (i > 33LL * k) break;       int l = 0;       if (i > 3 * k) {         int rem = i - 3 * k;         l = rem / 10;         if (rem % 10) ++l;       }       int r = min(3 * k, i / 10);       if (l > r) continue;       dp[1][i] = max(ev1(l, i), ev1(r, i));     }     for (int _n(5), d(2); d <= _n; d++) {       long long o = 0;       for (int _n((d + 1) - 1), z(0); z <= _n; z++) o = o * 10 + 3;       o *= k;       o = min<long long>(o, N);       int t = 1;       for (int _n((d)-1), j(0); j <= _n; j++) t *= 10;       for (int _n((o + 1) - 1), i(0); i <= _n; i++) {         int r = min(3 * k, i / t);         long long cd = 0;         for (int _n((r + 1) - 1), j(0); j <= _n; j++) {           upd(cd, dp[d - 1][i - j * t] + (long long)f[d] * j);         }         dp[d][i] = cd;       }     }     scanf( %d , &m);     for (int _n((m)), hod(1); hod <= _n; hod++) {       scanf( %d , &n);       long long ans = solve(n);       printf( %I64d n , ans);     }   }