malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int cnt[27][1505];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     string s;     int n, q, m;     char c;     cin >> n;     cin >> s;     for (int i = (0); i <= (25); i++) {       char ch = i +  a ;       for (int j = (0); j <= (n - 1); j++) {         int tot = 0;         int need = 0;         for (int k = (j); k <= (n - 1); k++) {           tot++;           if (s[k] != ch) need++;           cnt[i][need] = max(cnt[i][need], tot);         }       }       for (int j = (1); j <= (n); j++) cnt[i][j] = max(cnt[i][j], cnt[i][j - 1]);     }     cin >> q;     while (q--) {       cin >> m >> c;       cout << cnt[c -  a ][m] <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   const double eps = 1e-11;   template <class T>   inline void checkmin(T& a, T b) {     if (b < a) a = b;   }   template <class T>   inline void checkmax(T& a, T b) {     if (b > a) a = b;   }   template <class T>   inline T sqr(T x) {     return x * x;   }   template <class T>   inline T lsb(T n) {     return (n & (-n));   }   template <class T>   inline int countbit(T n) {     return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));   }   template <class T>   inline T gcd(T a, T b) {     if (a < 0) return gcd(-a, b);     if (b < 0) return gcd(a, -b);     return (b == 0) ? a : gcd(b, a % b);   }   bool isequal(vector<int> a, vector<int> b) {     for (int i = 0; i < (a.size()); i++)       if (a[i] != b[i]) return false;     return true;   }   template <class T>   bool allequal(vector<T> a) {     sort(a.begin(), a.end());     return a[0] == a[a.size() - 1];   }   template <class T>   void printarray(vector<T> a) {     for (int i = 0; i < (a.size()); i++) cout << a[i] <<    ;     cout << endl;   }   template <class T>   void printsubarray(vector<T> a, int start, int end) {     if (start < 0 or end > a.size() - 1) cout <<  Out of bounds  << endl;     for (int i = start; i <= end; i++) cout << a[i] <<    ;     cout << endl;   }   bool isprime(int n) {     if (n < 2) return false;     if (n == 2) return true;     for (int i = 2; i * i <= n; i++) {       if (n % i == 0) return false;     }     return true;   }   void split(const string& s, char c, vector<string>& result) {     stringstream ss(s);     string x;     while (getline(ss, x, c)) result.push_back(x);   }   void err(vector<string>::iterator it) {}   template <typename T, typename... Args>   void err(vector<string>::iterator it, T a, Args... args) {     cerr << it->substr((*it)[0] ==    , it->length()) <<   =   << a <<   n ;     err(++it, args...);   }   void readarray(vector<int>& a, int size) {     int tmp;     a.clear();     for (int i = 0; i < (size); i++) {       cin >> tmp;       a.push_back(tmp);     }   }   bool invalid(int n, int k) {     int nbits = countbit(n);     if (k < nbits or k > n) return true;     return false;   }   vector<int> res;   void split(int n, int k) {     if (k == 1) {       res.push_back(n);     } else {       int nbits = countbit(n);       if (nbits == 1) {         split(n >> 1, ((k >> 1) + k % 2));         split(n >> 1, (k >> 1));       } else {         int a, b;         int msb = (int)log2(n);         a = (1 << msb);         b = n ^ a;         int excess = k - nbits + 1;         int first = min(excess, a);         {           vector<string> _v;           split( a, b, excess, nbits, k, first ,  , , _v);           err(_v.begin(), a, b, excess, nbits, k, first);         };         split(a, first);         split(b, k - first);       }     }   }   void solvecase() {     int n, k;     cin >> n >> k;     if (invalid(n, k)) {       cout <<  NO  << endl;       return;     }     split(n, k);     cout <<  YES  << endl;     printarray(res);   }   int main() {     int cases = 1;     while (cases--) {       solvecase();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, cnt;     int i, ln, sz;     string tx;     vector<string> v;     cin >> n;     getchar();     getline(cin, tx);     ln = tx.size();     int s = 0;     for (i = 0; i < (ln); i++)       if (tx[i] ==  .  || tx[i] ==  ?  || tx[i] ==  ! ) {         v.push_back(tx.substr(s, i - s + 1));         if (i + 1 < ln) s = i + 1;       }     cnt = sz = 0;     for (i = 0; i < ((int)v.size()); i++) {       if ((v[i][0] ==     && v[i].size() > n + 1) ||           (v[i][0] !=     && v[i].size() > n)) {         cout <<  Impossible  << endl;         return 0;       }       if (i > 0 && sz + (int)v[i].size() <= n)         sz += (int)v[i].size();       else {         sz = (v[i][0] ==    ) ? (int)v[i].size() - 1 : (int)v[i].size();         cnt++;       }     }     cout << cnt << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long mp[2050][2050];   int main() {     for (int i = 0; i <= 1000; i++) {       mp[i][0] = 1;       mp[i][i] = 1;     }     for (int i = 0; i <= 1000; i++) {       for (int j = 1; j < i; j++) {         mp[i][j] = (mp[i - 1][j] + mp[i - 1][j - 1]) % 1000000007;       }     }     long long n, m, k;     cin >> n >> m >> k;     cout << (mp[n - 1][2 * k] * mp[m - 1][2 * k]) % 1000000007 <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const int N = 1e5 + 7, M = 1e7, OO = 0x3f3f3f3f;   map<int, int> dp;   int solve(int num) {     if (num == 0) {       return 0;     }     if (dp[num]) {       return dp[num];     }     vector<int> v;     int temp = num;     while (temp) {       v.push_back(temp % 10);       temp /= 10;     }     int ret = -OO;     for (int i = 0; i < v.size(); ++i) {       ret = max(ret, v[i]);     }     return dp[num] = 1 + solve(num - ret);   }   int main() {     dp.clear();     int num;     cin >> num;     cout << solve(num);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 5003;   long long a[N + 2], x[N + 2], dp[N + 2];   int l[N + 2], r[N + 2], cnt[N + 2];   int main() {     ios_base::sync_with_stdio(0);     for (int i = 0; i <= N; i++) {       l[i] = 1000000;     }     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i + 1];       l[a[i + 1]] = min(i + 1, l[a[i + 1]]);       r[a[i + 1]] = max(i + 1, r[a[i + 1]]);     }     for (int i = n + 1; i >= 1; i--) {       if (i == n + 1)         dp[i] = 0;       else {         dp[i] = max(dp[i], dp[i + 1]);         if (l[a[i]] == i) {           long long sum = 0;           int tope = 0;           int tt = 10000000;           for (int j = i; j <= n; j++) {             if (cnt[a[j]] == 0) {               sum ^= a[j];               cnt[a[j]] = 1;             }             tt = min(tt, l[a[j]]);             if ((r[a[j]] == j) && tope <= j && tt >= i) {               dp[i] = max(dp[j + 1] + sum, dp[i]);             }             tope = max(tope, r[a[j]]);           }           for (int j = i; j <= n; j++) {             cnt[a[j]] = 0;           }         }       }     }     cout << dp[1] << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e4 + 5;   int a[MAXN];   int dp[MAXN][2005];   int solve(int n) {     int len = 2005;     for (int i = 1; i <= n; ++i)       for (int j = 0; j < len; ++j) dp[i][j] = INT_MAX;     for (int j = 0; j < len; ++j) dp[0][j] = j;     for (int i = 1; i <= n; ++i) {       for (int j = 0; j < len; ++j) {         if (j >= a[i]) dp[i][j - a[i]] = min(dp[i][j - a[i]], dp[i - 1][j]);         if (j + a[i] < len)           dp[i][j + a[i]] = min(dp[i][j + a[i]], max(dp[i - 1][j], j + a[i]));       }     }     int ans = INT_MAX;     for (int j = 0; j < len; ++j) ans = min(ans, dp[n][j]);     return ans;   }   int main() {     int n, t;     cin >> t;     while (t--) {       cin >> n;       for (int i = 1; i <= n; ++i) cin >> a[i];       cout << solve(n) << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)1e5 + 1;   char s[maxn];   const int ci = 26;   struct vertex {     vertex *go[ci];     vertex *suf;     bool term;     int len;     int d[2];     vertex(int _len = 0) : suf(0), term(0), len(_len) {       for (int i = 0; i < ci; i++) go[i] = 0;     }   };   vertex vs[2 * maxn];   vertex *last;   int m;   void addch(int ch) {     vs[m++] = vertex();     vertex *newlast = &vs[m - 1];     newlast->len = last->len + 1;     for (; last && !last->go[ch]; last = last->suf) last->go[ch] = newlast;     if (!last)       newlast->suf = &vs[0];     else {       vertex *next = last->go[ch];       if (last->len + 1 == next->len)         newlast->suf = next;       else {         vs[m++] = *next;         vertex *clone = &vs[m - 1];         clone->len = last->len + 1;         next->suf = clone;         newlast->suf = clone;         for (; last && last->go[ch] == next; last = last->suf)           last->go[ch] = clone;       }     }     last = newlast;   }   int checked[maxn];   void dfscheck(vertex *v, string s, string now) {     if (v->term) {       assert(now == string(s, (int)s.length() - (int)now.length()));       checked[(int)now.length()]++;     }     for (int i = 0; i < ci; i++)       if (v->go[i]) dfscheck(v->go[i], s, now + (char)( a  + i));   }   vertex *fivs[2 * maxn];   const int maxl = 2 * maxn;   int cnt[maxl];   int head[maxl];   int main() {     while (scanf( %s , s) >= 1) {       m = 0;       vs[m++] = vertex();       last = &vs[0];       int n = strlen(s);       for (int i = 0; i < n; i++) {         addch(s[i] -  a );       }       for (vertex *now = last; now; now = now->suf) now->term = 1;       memset(cnt, 0, sizeof(cnt));       for (int i = 0; i < m; i++) cnt[vs[i].len]++;       head[0] = 0;       for (int i = 1; i < m; i++) head[i] = head[i - 1] + cnt[i - 1];       for (int i = 0; i < m; i++) fivs[head[vs[i].len]++] = &vs[i];       reverse(fivs, fivs + m);       for (int i = 0; i < m; i++) {         vertex *v = fivs[i];         v->d[0] = (int)v->term;         for (int j = 0; j < ci; j++)           if (v->go[j]) v->d[0] += v->go[j]->d[0];         v->d[1] = 0;       }       fivs[m - 1]->d[1] = 1;       for (int i = m - 1; i >= 0; i--) {         vertex *v = fivs[i];         for (int j = 0; j < ci; j++)           if (v->go[j]) v->go[j]->d[1] += v->d[1];       }       long long ans = 0;       for (int i = 0; i < m - 1; i++) {         int f = fivs[i]->d[0], s = fivs[i]->d[1];         ans += (long long)s * (long long)f * (f + 1) / 2;       }       printf(            %lld              n ,           ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long tc;     cin >> tc;     while (tc--) {       long long n, m;       cin >> n >> m;       vector<long long> p(n);       for (long long z = 0; (1 > 0 and z < n) or (1 < 0 and z > n); z += 1)         cin >> p[z];       ;       function<long long(long long)> num_swaps = [&](long long k) {         vector<long long> b(n);         for (long long i = 0; (1 > 0 and i < n) or (1 < 0 and i > n); i += 1)           b[i] = (i - k + n) % n + 1;         vector<vector<long long> > g(n, vector<long long>());         ;         for (long long i = 0; (1 > 0 and i < n) or (1 < 0 and i > n); i += 1) {           g[p[i] - 1].push_back(b[i] - 1);           g[b[i] - 1].push_back(p[i] - 1);         }         vector<bool> v(n, false);         function<void(long long)> dfs = [&](long long rt) {           v[rt] = true;           for (auto ch : g[rt])             if (!v[ch]) dfs(ch);         };         long long c = 0;         for (long long i = 0; (1 > 0 and i < n) or (1 < 0 and i > n); i += 1)           if (!v[i]) {             dfs(i);             ++c;           }         return n - c;       };       vector<long long> cnt(n, 0);       for (long long i = 0; (1 > 0 and i < n) or (1 < 0 and i > n); i += 1)         ++cnt[(i + 1 - p[i] + n) % n];       vector<long long> ans;       for (long long k = 0; (1 > 0 and k < n) or (1 < 0 and k > n); k += 1)         if (cnt[k] >= n - m * 2 and num_swaps(k) <= m) ans.push_back(k);       cout << ans.size();       for (auto x : ans) cout <<     << x;       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 50;   long long a[MAXN], b[MAXN], dp[MAXN];   long long la[MAXN], lb[MAXN];   int sz, cur, n;   double cross(int x, int y) { return (double)(lb[x] - lb[y]) / (la[y] - la[x]); }   void newLine(long long p, long long q) {     la[sz] = p;     lb[sz] = q;     while (sz > 1 && cross(sz - 1, sz - 2) > cross(sz, sz - 1)) {       la[sz - 1] = la[sz];       lb[sz - 1] = lb[sz];       sz--;     }     sz++;   }   long long find(long long x) {     while (cur + 1 < sz && x > cross(cur, cur + 1)) cur++;     return la[cur] * x + lb[cur];   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n; i++) cin >> b[i];     dp[1] = 0;     newLine(b[1], 0);     for (int i = 2; i <= n; i++) {       dp[i] = find(a[i]);       newLine(b[i], dp[i]);     }     cout << dp[n];   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int tc;     cin >> tc;     while (tc--) {       int n, m, x1, y1, x2, y2, w, h;       cin >> n >> m >> x1 >> y1 >> x2 >> y2 >> w >> h;       int right = n - x2;       int left = x1;       int up = m - y2;       int down = y1;       int mn = 1e9;       int w1 = x2 - x1;       int h1 = y2 - y1;       if (w1 + w <= n) {         mn = min(mn, max(0, w - right));         mn = min(mn, max(0, w - left));       }       if (h1 + h <= m) {         mn = min(mn, max(0, h - up));         mn = min(mn, max(0, h - down));       }       if (mn == 1e9) mn = -1;       cout << mn <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   vector<int> E;   vector<int> a[N];   int value[N];   long long down[N];   long long max_path[N];   int n;   long long calc_down(int edge_index) {     long long &ans = down[edge_index];     if (ans != -1) return ans;     int v = E[edge_index];     ans = value[v];     for (int e : a[v])       if (e != (edge_index ^ 1)) ans = max(ans, calc_down(e) + value[v]);     return ans;   }   long long calc_max_path(int edge_index) {     long long &ans = max_path[edge_index];     if (ans != -1) return ans;     int v = E[edge_index];     long long first = 0, second = 0;     for (int e : a[v])       if (e != (edge_index ^ 1)) {         long long cur = calc_down(e);         if (first <= cur)           second = first, first = cur;         else if (second < cur)           second = cur;         ans = max(ans, calc_max_path(e));       }     return ans = max(ans, first + second + value[v]);   }   int main() {     ios::sync_with_stdio(false);     cin >> n;     for (int i = 1; i <= n; ++i) cin >> value[i];     for (int i = 1; i < n; ++i) {       int u, v;       cin >> u >> v;       a[u].push_back(E.size());       E.push_back(v);       a[v].push_back(E.size());       E.push_back(u);     }     memset(down, -1, sizeof down);     memset(max_path, -1, sizeof max_path);     long long ans = 0;     for (int i = 0; i < E.size(); i += 2)       ans = max(ans, calc_max_path(i) + calc_max_path(i ^ 1));     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<int> v(n), inc(n, -1), dec(n, n - 1);     for (int i = 0; i < n; i++) {       cin >> v[i];       if (i > 0) {         if (v[i] > v[i - 1])           inc[i] = i;         else           inc[i] = inc[i - 1];       }     }     for (int i = n - 2; i >= 0; i--) {       if (v[i] > v[i + 1])         dec[i] = i;       else         dec[i] = dec[i + 1];     }     for (int i = 0; i < m; i++) {       int l, r;       cin >> l >> r;       l--;       r--;       if (dec[l] >= inc[r])         cout <<  Yes n ;       else         cout <<  No n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     int n, b, a;     cin >> n >> b >> a;     int a_cap = a;     int x, c = 0;     while (a + b) {       cin >> x;       if (!x) {         if (a)           a--;         else           b--;       }       if (x) {         if (b) {           if (a == a_cap)             a--;           else             b--, a++;         } else {           a--;         }       }       c++;       if (c == n) {         break;       }     }     cout << c;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     string l;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> l;       int a = l.length();       if (a > 10) {         cout << l[0] << a - 2 << l[a - 1] << endl;       } else {         cout << l << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   auto fastio = (ios::sync_with_stdio(false), cin.tie(nullptr));   void solve() {     long long n, m;     cin >> n >> m;     string s;     cin >> s;     vector<long long> p(m);     for (long long i = 0; i < (m); ++i) {       cin >> p[i];     }     sort((p).begin(), (p).end());     vector<long long> cnt(27);     long long ptr = 0;     for (long long i = 0; i < (n); ++i) {       while (i == p[ptr]) {         m--;         ptr++;       }       cnt[s[i] -  a ] += (m ? m + 1 : 1);     }     for (long long i = 0; i < (26); ++i) {       if (i) {         cout <<    ;       }       cout << cnt[i];     }     cout <<   n ;   }   int32_t main() {     long long tt;     cin >> tt;     while (tt--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   using vv = vector<vector<T>>;   template <class T>   ostream &operator<<(ostream &os, const vector<T> &t) {     os <<  { ;     for (int(i) = 0; (i) < (t.size()); ++(i)) {       os << t[i] <<  , ;     }     os <<  }  << endl;     return os;   }   template <class S, class T>   ostream &operator<<(ostream &os, const pair<S, T> &t) {     return os <<  (  << t.first <<  ,  << t.second <<  ) ;   }   const long long MOD = 1e9 + 9;   const int N = (1 << 17), r = 11451;   struct Seg {     long long h, q, r;     Seg(int v = 0) {       h = v;       q = 0;       r = (v ? ::r : 1);     }     void comp(const Seg &ope) { q = ope.q; }     void clear() { q = 0; }     static Seg Ope(int v = 0) {       Seg re;       re.q = v;       return re;     }     static Seg e;   };   Seg Seg::e = Seg();   ostream &operator<<(ostream &os, const Seg &t) {     return os <<  (  << t.h <<  ,  << t.q <<  ,  << t.r <<  ) ;   }   Seg seg[2 * N + 10];   Seg operator+(const Seg l, const Seg r) {     Seg re;     re.h = (l.h * r.r % MOD + r.h) % MOD;     re.r = l.r * r.r % MOD;     return re;   }   vector<long long> ser(N);   inline void lazy_eval(int k, int a, int b) {     if (!seg[k].q) return;     seg[k].h = seg[k].q * ser[b - a - 1] % MOD;     if (2 * k <= 2 * N + 5) {       seg[2 * k + 1].comp(seg[k]);       seg[2 * k + 2].comp(seg[k]);     }     seg[k].clear();   }   inline void update_node(int k) { seg[k] = seg[2 * k + 1] + seg[2 * k + 2]; }   void update(int l, int r, Seg ope, int k = 0, int a = 0, int b = N) {     lazy_eval(k, a, b);     if (b <= l || r <= a) return;     if (l <= a && b <= r) {       seg[k].comp(ope);       lazy_eval(k, a, b);       return;     }     int m = (a + b) / 2;     update(l, r, ope, 2 * k + 1, a, m);     update(l, r, ope, 2 * k + 2, m, b);     update_node(k);   }   Seg get(int l, int r, int k = 0, int a = 0, int b = N) {     lazy_eval(k, a, b);     if (b <= l || r <= a) return Seg::e;     if (l <= a && b <= r) {       return seg[k];     }     int m = (a + b) / 2;     Seg vl = get(l, r, 2 * k + 1, a, m);     Seg vr = get(l, r, 2 * k + 2, m, b);     update_node(k);     return vl + vr;   }   int main() {     ios_base::sync_with_stdio(false);     cout << fixed << setprecision(0);     ser[0] = 1;     for (int(i) = 0; (i) < (N - 1); ++(i))       ser[i + 1] = (1 + ser[i] * r % MOD) % MOD;     int n, m, t;     cin >> n >> m >> t;     string str;     cin >> str;     int a, l, r, x;     for (int(i) = 0; (i) < (n); ++(i)) seg[(1 << 17) - 1 + i] = Seg(str[i]);     for (int(i) = ((1 << 17) - 1) - 1; (i) >= 0; --(i)) update_node(i);     for (int(i) = 0; (i) < (t + m); ++(i)) {       cin >> a >> l >> r >> x;       --l;       if (a == 1) {         update(l, r, Seg::Ope(x +  0 ));       } else {         cout << (get(l, r - x).h == get(l + x, r).h ?  YES  :  NO ) << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   const long long INF = mod * mod;   const long double eps = 1e-8;   void solve() {     int n;     cin >> n;     vector<long long> val(n), mask(n);     long long sum = 0;     for (int i = 0; i < n; i++) {       cin >> val[i] >> mask[i];       sum += val[i];     }     if (sum < 0) {       for (int i = 0; i < n; i++) {         val[i] = -val[i];         sum = -sum;       }     }     long long ans = 0;     for (int i = 0; i < 62; i++) {       long long k = (long long)1 << i;       long long csum = 0;       for (int j = 0; j < n; j++) {         if (mask[j] < 2 * k && (mask[j] & k)) {           csum += val[j];         }       }       if (csum > 0) {         ans += k;         for (int j = 0; j < n; j++) {           if (mask[j] & k) {             val[j] = -val[j];           }         }       }     }     cout << ans << endl;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout << fixed << setprecision(10);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int xor_sum(int x) {     if (x % 4 == 0) {       return x;     }     if (x % 4 == 1) {       return 1;     }     if (x % 4 == 2) {       return x + 1;     }     if (x % 4 == 3) {       return 0;     }     return -1;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     long long tmp, q, tmp2 = 0;     vector<int> v(n);     for (int i = 0; i < n; ++i) {       cin >> v[i];     }     for (int i = 1; i <= n; ++i) {       tmp = xor_sum(i - 1);       if (!(n / i % 2)) {         tmp ^= xor_sum(i - 1);       }       tmp ^= xor_sum(n % i);       tmp ^= v[i - 1];       if (i == 1) {         q = tmp;       } else {         q ^= tmp;       }     }     cout << q <<   n ;     return 0;   }
#include<bits/stdc++.h>   using namespace std;   typedef long long ll;   typedef pair<int,int> pii;   const int MAXN = 6 + 5;   const int MAXM = 6 + 5;   const int base = 5;   const int ALL = 1.6e4 + 5;   const int inf = 0x3f3f3f3f;      int pwb[MAXM];      int n,m;   int a[MAXN], b[MAXM], c[MAXN][MAXM];   int f[MAXN][ALL];      inline int encode(int t[])   {    int res = 0;    for(int i=m; i>=1; --i)     res = res * base + t[i];    return res;   }      inline int get_dig(int mask,int k)   {    return mask / pwb[k - 1] % base;   }      inline int reduce(int mask,int k,int x)   {    return mask - x * pwb[k - 1];   }      inline void dfs_f(int dep,int mask,int curc,int rem,int lvl)   {    if(dep > m)    {     if(!rem) f[lvl][mask] = min(f[lvl][mask], curc);     return;    }        dfs_f(dep+1, mask, curc, rem, lvl);        curc += c[lvl][dep];    int lim = min(rem, get_dig(mask, dep));        for(int i=1; i<=lim; ++i)     dfs_f(dep+1, reduce(mask, dep, i), curc, rem - i, lvl);   }      int main(void)   {    pwb[0] = 1;    for(int i=1; i<MAXM; ++i) pwb[i] = pwb[i-1] * base;        scanf( %d%d ,&n,&m);    for(int i=1; i<=n; ++i) scanf( %d ,&a[i]);    for(int i=1; i<=m; ++i) scanf( %d ,&b[i]);    for(int i=1; i<=n; ++i)     for(int j=1; j<=m; ++j) scanf( %d ,&c[i][j]);        int tot = 0;    for(int i=1; i<=n; ++i) tot += a[i];    for(int i=1; i<=m; ++i) tot -= b[i];    if(tot > 0) return printf( -1 ), 0;        int all = pwb[m] - 1;    memset(f,0x3f,sizeof(f));    f[0][encode(b)] = 0;    for(int i=1; i<=n; ++i)    {     for(int mask=0; mask<=all; ++mask)      if(f[i-1][mask] < inf) dfs_f(1, mask, f[i-1][mask], a[i], i);    }        int res = *min_element(f[n], f[n]+all+1);    if(res == inf) printf( -1 );    else printf( %d ,res);    return 0;   }