#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 10;   long long n, sum, n1, pick, ans, t, a[N], b[N], f[N], f1[N], m, t1;   int main() {     scanf( %lld , &n);     for (int i = 1; i <= 8; i++) {       scanf( %lld , &a[i]);       sum = sum + a[i] * i;     }     if (sum <= n) return printf( %lld n , sum), 0;     n1 = std::max((long long)0, n - 840), n = n - n1;     for (int i = 1; i <= 8; i++) {       if (a[i] < n1 / i)         pick = a[i];       else         pick = n1 / i;       if (a[i] - pick < 10) pick = std::max((long long)0, a[i] - 10);       ans = ans + pick * i, n1 = n1 - pick * i, a[i] = a[i] - pick;     }     n += n1;     for (int i = 1; i <= 8; i++) {       t = 1;       while (a[i] > 0) {         t1 = std::min(t, a[i]);         a[i] = a[i] - t1;         f[++m] = t1 * i;         t <<= 1;       }     }     for (long long i = 1; i <= m; i++)       for (long long j = n; j >= f[i]; j--)         f1[j] = std::max(f1[j], f1[j - f[i]] + f[i]);     printf( %lld n , ans + f1[n]);     return 0;   }   