#include <bits/stdc++.h>   using namespace std;   template <class T>   void _R(T &x) {     cin >> x;   }   void _R(int &x) { scanf( %d , &x); }   void _R(long long &x) { scanf( %lld , &x); }   void _R(double &x) { scanf( %lf , &x); }   void _R(char &x) { scanf(  %c , &x); }   void _R(char *x) { scanf( %s , x); }   void R() {}   template <class T, class... U>   void R(T &head, U &...tail) {     _R(head);     R(tail...);   }   template <class T>   void _W(const T &x) {     cout << x;   }   void _W(const int &x) { printf( %d , x); }   void _W(const long long &x) { printf( %lld , x); }   void _W(const double &x) { printf( %.16f , x); }   void _W(const char &x) { putchar(x); }   void _W(const char *x) { printf( %s , x); }   template <class T, class U>   void _W(const pair<T, U> &x) {     _W(x.first);     putchar(   );     _W(x.second);   }   template <class T>   void _W(const vector<T> &x) {     for (auto i = x.begin(); i != x.end(); _W(*i++))       if (i != x.cbegin()) putchar(   );   }   void W() {}   template <class T, class... U>   void W(const T &head, const U &...tail) {     _W(head);     putchar(sizeof...(tail) ?     :   n );     W(tail...);   }   int MOD = 1e9 + 7;   void ADD(long long &x, long long v) {     x = (x + v) % MOD;     if (x < 0) x += MOD;   }   const int SIZE = 1e6 + 10;   const int AA = 1000;   int dp[2001];   int main() {     long long target_W;     R(target_W);     long long sum = 0;     long long cnt[9], cnt2[9] = {};     for (int i = (1); i <= (8); ++i) {       R(cnt[i]);       sum += cnt[i] * i;     }     if (sum <= target_W)       W(sum);     else {       long long now = 0;       for (int i = (1); i <= (8); ++i) {         if (now + i * cnt[i] <= target_W) {           now += i * cnt[i];           cnt2[i] = cnt[i];           cnt[i] = 0;         } else {           long long v = (target_W - now) / i;           now += i * v;           cnt[i] -= v;           cnt2[i] += v;         }       }       dp[(0) + AA] = 1;       for (int v = (1); v <= (8); ++v) {         for (int j = 0; j < (min(100LL, cnt[v])); ++j) {           for (int k = 900; k >= -900; k--) {             if (dp[(k) + AA]) dp[(k + v) + AA] = 1;           }         }         for (int j = 0; j < (min(100LL, cnt2[v])); ++j) {           for (int k = -900; k <= 900; k++) {             if (dp[(k) + AA]) dp[(k - v) + AA] = 1;           }         }       }       long long an = 0;       for (int i = (0); i <= (target_W - now); ++i) {         if (dp[(i) + AA]) an = now + i;       }       W(an);     }     return 0;   }   