malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long pw(long long a, long long b, long long p) {     long long res = 1;     a %= p;     b %= p - 1;     while (b) {       if (b & 1) res = (1LL * a * res) % p;       a = (1LL * a * a) % p;       b >>= 1;     }     return res;   }   long long a, b, x, p, ans;   vector<pair<long long, long long> > pir;   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> a >> b >> p >> x;     for (long long i = 0; i < p - 1; i++) {       long long t = pw(a, i, p);       t = pw(t, p - 2, p);       t = (1LL * b * t) % p;       pir.push_back({i, t});     }     long long lcm = p * (p - 1);     for (auto i : pir) {       long long z = i.first - i.second;       long long w = z * p + i.second;       w %= lcm;       if (w < 0) w += lcm;       if (w > x) continue;       ans += (x - w) / lcm + 1;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const double esp = 1e-8;   const int msz = 100000;   const int mod = 2015;   struct Child {     int v, d;     long long p;   };   Child c[4444];   bool vis[4444];   queue<int> q;   int n;   void cut(int id) {     long long v = c[id].v + 1;     long long d = 0;     for (int i = id + 1; i <= n; ++i) {       if (vis[i]) continue;       if (v > 0) v--;       c[i].p -= (d + v);       if (c[i].p < 0) {         vis[i] = 1;         d += c[i].d;       }     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d %d %lld , &c[i].v, &c[i].d, &c[i].p);     }     memset(vis, 0, sizeof(vis));     for (int i = 1; i <= n; ++i) {       if (vis[i]) continue;       cut(i);       q.push(i);     }     int sz = q.size();     printf( %d n , sz);     while (sz--) {       printf( %d , q.front());       q.pop();       if (sz) putchar(   );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1005;   char s[maxn], t[maxn];   int n, m;   vector<int> id;   vector<int> ans;   int main() {     scanf( %d%d , &n, &m);     getchar();     gets(s);     gets(t);     int Ans = 0x3f3f3f3f;     for (int i = 0; i < m - n + 1; i++) {       id.clear();       int temp = 0;       for (int j = 0; j < n; j++) {         if (s[j] != t[i + j]) {           temp++;           id.push_back(j + 1);         }       }       if (temp < Ans) {         ans.clear();         for (int j = 0; j < id.size(); j++) {           ans.push_back(id[j]);         }         Ans = temp;       }     }     printf( %d n , Ans);     for (int i = 0; i < ans.size(); i++) {       printf( %d%c , ans[i], i == ans.size() - 1 ?   n  :    );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int make(int c) {     if (c == 10000000) return 1;     return 0;   }   long long bb[3][300005], cc[3][300005], dd[3][300005], ee[3][300005];   long long int a[3][300005 + 1];   int main() {     int n, fl = 0, m, q;     cin >> n;     int i;     for (i = 1; i <= n; i++) {       cin >> a[1][i];     }     for (i = 1; i <= n; i++) {       cin >> a[2][i];     }     long long ans = 0;     int j = 0;     for (i = 1; i <= n; i++) {       ans += a[1][i] * j;       j++;     }     for (i = n; i >= 1; i--) {       ans += a[2][i] * j;       j++;     }     long long ans1 = 0, s = 0;     ans1 = max(ans, ans1);     for (i = 1; i <= n; i++) {       s += a[1][i];       bb[1][i] = s;     }     for (i = n; i >= 1; i--) {       s += a[2][i];       bb[2][i] = s;     }     s = 0;     for (i = 1; i <= n; i++) {       s += a[2][i];       cc[2][i] = s;     }     for (i = n; i >= 1; i--) {       s += a[1][i];       cc[1][i] = s;     }     j = 1;     s = 0;     for (i = 1; i <= n; i++) {       s += a[1][i] * j;       dd[1][i] = s;       j++;     }     for (i = n; i >= 1; i--) {       s += a[2][i] * j;       dd[2][i] = s;       j++;     }     j = 1;     s = 0;     for (i = 1; i <= n; i++) {       s += a[2][i] * j;       ee[2][i] = s;       j++;     }     for (i = n; i >= 1; i--) {       s += a[1][i] * j;       ee[1][i] = s;       j++;     }     j = 1;     long long anss = 0;     long long g;     int curidx = 1, prev = 1, cur = 2;     ans1 = max(ans1, ans);     while (1) {       g = 0;       if (prev == 1 && cur == 2) {         anss += j * a[cur][curidx];         cur = 2;         prev = 2;         g = anss;         if (curidx < n)           g += ee[1][curidx + 1] +                (j - curidx) * (cc[1][curidx + 1] - cc[2][curidx]) - ee[2][curidx];         j++;         curidx++;       } else if (prev == 2 && cur == 2) {         anss += j * a[cur][curidx];         cur = 1;         prev = 2;         g = anss;         g += ee[1][curidx] + (j - curidx) * (cc[1][curidx] - cc[2][curidx]) -              ee[2][curidx];         j++;       } else if (prev == 2 && cur == 1) {         anss += j * a[cur][curidx];         cur = 1;         prev = 1;         g = anss;         if (curidx < n)           g += dd[2][curidx + 1] +                (j - curidx) * (bb[2][curidx + 1] - bb[1][curidx]) - dd[1][curidx];         j++;         curidx++;       } else if (prev == 1 && cur == 1) {         anss += j * a[cur][curidx];         cur = 2;         prev = 1;         g = anss;         g += dd[2][curidx] + (j - curidx) * (bb[2][curidx] - bb[1][curidx]) -              dd[1][curidx];         j++;       }       ans1 = max(ans1, g);       if (curidx > n) break;     }     cout << ans1 << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int d, n, d1 = 1, a;     long long cnt = 0;     cin >> d >> n;     for (int i = 0; i < n; i++) {       cin >> a;       d1 = a;       if (i != n - 1 && d1 != d) {         cnt += d - d1;         d1 = 1;       } else if (d1 == d) {         d1 = 1;       }     }     cout << cnt;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }   class fraction {    public:     int a, b;     fraction() { a = 0, b = 1; }     fraction(int a, int b) {       if (b < 0) {         a *= -1;         b *= -1;       }       if (a == 0) b = 1;       int g = gcd(abs(a), abs(b));       a /= g;       b /= g;       this->a = a;       this->b = b;     }     bool operator==(fraction oth) const { return a == oth.a and b == oth.b; }     bool operator!=(fraction oth) const { return !(*this == oth); }     bool operator<(fraction oth) const {       if (a < 0 and oth.a >= 0) return true;       if (oth.a < 0 and a >= 0) return false;       return (long long)a * oth.b < (long long)b * oth.a;     }     bool operator>(fraction oth) const {       return !(*this < oth) and (*this != oth);     }     bool operator<=(fraction oth) const { return !(*this > oth); }     bool operator>=(fraction oth) const { return !(*this < oth); }   };   int main() {     int n;     scanf( %d , &n);     int x1, y1, x2, y2;     scanf( %d %d %d %d , &x1, &y1, &x2, &y2);     fraction x(0, 1);     fraction y(1e9, 1);     for (int i = 1; i <= n; i++) {       int rx, ry, vx, vy;       scanf( %d %d %d %d , &rx, &ry, &vx, &vy);       if ((rx == x1 or rx == x2) and vx == 0) {         puts( -1 );         return 0;       }       if ((ry == y1 or ry == y2) and vy == 0) {         puts( -1 );         return 0;       }       fraction tox1(x1 - rx, vx);       fraction toy1(y1 - ry, vy);       fraction tox2(x2 - rx, vx);       fraction toy2(y2 - ry, vy);       if (tox2 < tox1) swap(tox1, tox2);       if (toy2 < toy1) swap(toy1, toy2);       x = max(x, tox1);       x = max(x, toy1);       y = min(y, tox2);       y = min(y, toy2);     }     if (x >= y) {       puts( -1 );     } else {       double l = (double)x.a / x.b;       printf( %.9lf n , l);     }     return 0;   }
#include <bits/stdc++.h>   const int N = 300005;   int t[N << 2], tag[N << 2], size[N << 2], st1[N], st2[N], tp1, tp2, n, x, y,       a[N];   long long ans;   void up(int x) {     int t1 = t[x << 1] + tag[x << 1], t2 = t[x << 1 | 1] + tag[x << 1 | 1];     if (t1 == t2) size[x] = size[x << 1] + size[x << 1 | 1];     if (t1 < t2) size[x] = size[x << 1];     if (t1 > t2) size[x] = size[x << 1 | 1];     t[x] = std::min(t1, t2);   }   void pushdown(int x) {     if (tag[x]) {       t[x] += tag[x];       tag[x << 1] += tag[x];       tag[x << 1 | 1] += tag[x];       tag[x] = 0;     }   }   void build(int k, int l, int r) {     if (l == r) {       t[k] = n + 1;       size[k] = 1;       return;     }     int mid = (l + r) >> 1;     build(k << 1, l, mid);     build(k << 1 | 1, mid + 1, r);     up(k);   }   void modify(int k, int L, int R, int l, int r, int val) {     if (r < l) return;     if (L == l && R == r) {       tag[k] = tag[k] + val;       return;     }     int mid = (L + R) >> 1;     pushdown(k);     if (r <= mid)       modify(k << 1, L, mid, l, r, val);     else if (l > mid)       modify(k << 1 | 1, mid + 1, R, l, r, val);     else {       modify(k << 1, L, mid, l, mid, val);       modify(k << 1 | 1, mid + 1, R, mid + 1, r, val);     }     up(k);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &x, &y);       a[x] = y;     }     int tp1 = tp1 = 0;     build(1, 1, n);     for (int i = 1; i <= n; i++) {       x = a[i];       while (tp1 && x > a[st1[tp1]])         modify(1, 1, n, st1[tp1 - 1] + 1, st1[tp1], x - a[st1[tp1]]), tp1--;       st1[++tp1] = i;       while (tp2 && x < a[st2[tp2]])         modify(1, 1, n, st2[tp2 - 1] + 1, st2[tp2], a[st2[tp2]] - x), tp2--;       st2[++tp2] = i;       modify(1, 1, n, i, i, -n - 1);       modify(1, 1, n, 1, i - 1, -1);       if (t[1] + tag[1] == 0) ans = ans + size[1];     }     printf( %lld n , ans);   }
#include <bits/stdc++.h>   using namespace std;   const int OO = 0x3f3f3f3f, N = 1e3 + 5, mod = 1e9 + 7;   string str;   vector<int> p, v;   vector<pair<int, vector<int>>> pos;   pair<int, char> occ[26];   int c[N], x = 1, n;   char ans[N];   int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }   int LCM(int a, int b) { return a / GCD(a, b) * b; }   void primes() {     for (int i = 2; i <= str.size(); ++i) {       bool flag = 1;       for (int j = 2; j * j <= i; ++j)         if (i % j == 0) flag = 0;       if (flag) p.push_back(i);     }   }   void sames() {     map<int, vector<int>> mp;     bool f[N] = {};     for (int i = 0; i < p.size(); ++i) {       if (f[c[i]]) continue;       f[c[i]] = 1;       for (int j = 0; j < p.size(); ++j)         if (c[i] == c[j]) mp[c[i]].push_back(p[j]);     }     for (auto& ptr : mp) {       bool flag[N] = {};       pair<int, vector<int>> tmp = {0, ptr.second};       for (int ch : ptr.second)         for (int i = ch; i <= n; i += ch) flag[i] = 1;       for (int i = 0; i < N; ++i) tmp.first += flag[i];       pos.push_back(tmp);     }     sort(pos.rbegin(), pos.rend());   }   void occur() {     for (int i = 0; i < 26; ++i) occ[i].second = (char)(i +  a );     for (char ch : str) ++occ[ch -  a ].first;   }   int main() {     memset(c, -1, sizeof c);     cin >> str;     n = str.size();     primes();     for (int i = 0; i < p.size(); ++i) {       if (c[i] == -1) c[i] = x;       for (int j = 0; j < p.size(); ++j)         if (LCM(p[i], p[j]) <= str.size()) c[j] = c[i];       ++x;     }     sames();     occur();     for (auto go : pos) {       bool flag = 1;       sort(occ, occ + 26);       for (int i = 0; i < 26; ++i)         if (occ[i].first >= go.first) {           flag = 0;           char chr = occ[i].second;           occ[i].first -= go.first;           for (int ch : go.second)             for (int i = ch; i <= n; i += ch) ans[i] = chr;           break;         }       if (flag) exit(puts( NO ));     }     puts( YES );     for (int i = 1; i <= n; ++i) {       if (ans[i] >=  a  && ans[i] <=  z )         cout << ans[i];       else {         for (int j = 0; j < 26; ++j)           if (occ[j].first) {             --occ[j].first;             cout << occ[j].second;             break;           }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve(long long case_no) {     long long n;     cin >> n;     vector<long long> a(n);     for (long long i = 0; i < n; ++i) cin >> a[i];     map<long long, long long> mp;     long long mx = LONG_LONG_MIN;     for (long long i = 0; i < n; ++i) {       long long x = i + a[i] % n + n;       ++mp[x % n];       if (mp[x % n] > 1) {         cout <<  NO n ;         return;       }     }     cout <<  YES n ;     return;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     long long t = 1;     cin >> t;     for (long long i = 0; i < t; ++i) {       solve(i + 1);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline void read(int &first) {     first = 0;     while (1) {       char ch = getchar();       if (ch ==     || ch ==   n ) break;       first = first * 10 + ch -  0 ;     }   }   inline void write(int first) {     char wr[12];     int k = 0;     if (!first) ++k, wr[k] =  0 ;     while (first) {       ++k;       wr[k] = char(first % 10 +  0 );       first /= 10;     }     for (int i = k; i >= 1; --i) putchar(wr[i]);   }   const int N = 1e5 + 5;   int n, k, s[N], a[N];   char ch;   bool f(int len) {     for (int i = 1; i <= n; ++i) {       if (!a[i]) continue;       int s1 = s[min(n, i + len)] - s[i - 1];       int s2 = s[i] - s[max(0, i - len - 1)];       if (s1 + s2 - a[i] >= k + 1) return 1;     }     return 0;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> k;     for (int i = 1; i <= n; ++i)       cin >> ch, s[i] = s[i - 1] + (ch ==  0 ), a[i] = ch ==  0 ;     int l = 1, r = n;     while (r - l > 1) {       int mid = (l + r) >> 1;       if (f(mid))         r = mid;       else         l = mid;     }     if (f(l))       cout << l;     else       cout << r;   }
#include <bits/stdc++.h>   int n, m, k, s;   int u[9][4];   int main() {     scanf( %d%d%d%d , &n, &m, &k, &s);     for (int i = 0; i < 9; i++) {       u[i][0] = -0x3f3f3f3f;       u[i][1] = 0x3f3f3f3f;       u[i][2] = -0x3f3f3f3f;       u[i][3] = 0x3f3f3f3f;     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         int c;         scanf( %d , &c);         c--;         u[c][0] = ((u[c][0]) > (i + j) ? (u[c][0]) : (i + j));         u[c][1] = ((u[c][1]) < (i + j) ? (u[c][1]) : (i + j));         u[c][2] = ((u[c][2]) > (i - j) ? (u[c][2]) : (i - j));         u[c][3] = ((u[c][3]) < (i - j) ? (u[c][3]) : (i - j));       }     }     int last;     scanf( %d , &last);     last--;     int res = 0;     for (int i = 1; i < s; i++) {       int cur;       scanf( %d , &cur);       cur--;       res =           ((res) > (((((u[last][0] - u[cur][1]) > (u[cur][0] - u[last][1])                            ? (u[last][0] - u[cur][1])                            : (u[cur][0] - u[last][1]))) >                              (((u[last][2] - u[cur][3]) > (u[cur][2] - u[last][3])                                    ? (u[last][2] - u[cur][3])                                    : (u[cur][2] - u[last][3])))                          ? (((u[last][0] - u[cur][1]) > (u[cur][0] - u[last][1])                                  ? (u[last][0] - u[cur][1])                                  : (u[cur][0] - u[last][1])))                          : (((u[last][2] - u[cur][3]) > (u[cur][2] - u[last][3])                                  ? (u[last][2] - u[cur][3])                                  : (u[cur][2] - u[last][3])))))                ? (res)                : (((((u[last][0] - u[cur][1]) > (u[cur][0] - u[last][1])                          ? (u[last][0] - u[cur][1])                          : (u[cur][0] - u[last][1]))) >                            (((u[last][2] - u[cur][3]) > (u[cur][2] - u[last][3])                                  ? (u[last][2] - u[cur][3])                                  : (u[cur][2] - u[last][3])))                        ? (((u[last][0] - u[cur][1]) > (u[cur][0] - u[last][1])                                ? (u[last][0] - u[cur][1])                                : (u[cur][0] - u[last][1])))                        : (((u[last][2] - u[cur][3]) > (u[cur][2] - u[last][3])                                ? (u[last][2] - u[cur][3])                                : (u[cur][2] - u[last][3]))))));       last = cur;     }     printf( %d n , res);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int prime[100010];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     int cnt = 0;     for (int i = 2; i <= n; i++) {       if (!prime[i]) {         cnt++;         for (int j = i; j <= n; j += i) prime[j] = cnt;       }     }     for (int i = 2; i <= n; i++) cout << prime[i] <<    ;     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int _ = 1e2;   const int maxn = 2e6 + _;   const int mod = 998244353;   inline int ad(int x, int y) { return x >= mod - y ? x - mod + y : x + y; }   inline int re(int x, int y) { return x < y ? x - y + mod : x - y; }   inline int mu(int x, int y) { return (long long)x * y % mod; }   inline int qp(int x, long long y) {     int r = 1;     while (y) {       if (y & 1) r = mu(r, x);       x = mu(x, x);       y >>= 1;     }     return r;   }   inline int iv(int x) { return qp(x, mod - 2); }   inline int dv(int x, int y) { return mu(x, qp(y, mod - 2)); }   inline int gcd(int x, int y) { return x ? gcd(y % x, x) : y; }   int a[maxn];   char ss[maxn];   set<int> S;   set<int>::iterator it, jt, kt;   int main() {     int n, mx = 0;     scanf( %d , &n);     for (int i = 0; i <= 2000001; i++) S.insert(i);     for (int i = 1; i <= n; i++) {       int len;       scanf( %s , ss + 1);       len = strlen(ss + 1);       int m, l, r;       scanf( %d , &m);       for (int j = 1; j <= m; j++) {         scanf( %d , &l), r = l + len - 1;         mx = max(mx, r);         it = S.lower_bound(l), jt = S.lower_bound(r + 1);         for (kt = it; kt != jt;) a[*kt] = ss[(*kt) - l + 1] -  a , S.erase(kt++);       }     }     for (int i = 1; i <= mx; i++) printf( %c ,  a  + a[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline void _redirect_streams(const string input =   ,                                 const string output =   ) {     if (not input.empty()) assert(freopen(input.c_str(),  r , stdin) != nullptr);     if (not output.empty())       assert(freopen(output.c_str(),  w , stdout) != nullptr);   }   inline void _speed_up_iostreams() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);   }   inline void _prepare() { _speed_up_iostreams(); }   long double a, b, c, d;   inline pair<long double, long double> prod(pair<long double, long double> a,                                              pair<long double, long double> b) {     vector<long double> v = {a.first * b.first, a.first * b.second,                              a.second * b.first, a.second * b.second};     return {*min_element(v.begin(), v.end()), *max_element(v.begin(), v.end())};   }   inline bool f(long double m) {     pair<long double, long double> p1 = prod({a - m, a + m}, {d - m, d + m});     pair<long double, long double> p2 = prod({b - m, b + m}, {c - m, c + m});     if (p1.first <= p2.first and p2.first <= p1.second) return true;     if (p1.first <= p2.second and p2.second <= p1.second) return true;     if (p2.first <= p1.first and p1.first <= p2.second) return true;     return p2.first <= p1.second and p1.second <= p2.second;   }   int main() {     _prepare();     cin >> a >> b >> c >> d;     long double l = 0, r = 1e18;     const long double EPS = 1e-10;     cout.precision(20);     while (r - l > EPS) {       long double m = (l + r) / 2;       if (f(m))         r = m;       else         l = m;     }     cout.precision(30);     cout << l << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, x, y, z, k, w;   char S[10005];   int dp[10005];   int ok[20005];   bool check(int x, int y, int pos) {     for (int i = (0); i < (pos); i++)       if (S[i + x] != S[i + y]) return 1;     return 0;   }   struct node {     char S[4];   } ANS[20005];   bool cmp(const node& a, const node& b) { return strcmp(a.S, b.S) < 0; }   int main() {     scanf( %s , S);     n = strlen(S);     dp[n - 2] = 2;     dp[n] = -1;     dp[n - 3] = 1;     for (int i = (n - 4); i >= (5); i--) {       if (dp[i + 3] == 1 && check(i, i + 3, 3))         dp[i] += 1;       else if (dp[i + 3] == 2 || dp[i + 3] == 3)         dp[i] += 1;       if (dp[i + 2] == 2 && check(i, i + 2, 2))         dp[i] += 2;       else if (dp[i + 2] == 1 || dp[i + 2] == 3)         dp[i] += 2;     }     for (int i = (5); i < (n); i++) {       if (dp[i] >= 2) {         dp[i] -= 2;         ANS[w].S[0] = S[i];         ANS[w].S[1] = S[i + 1];         w++;       }       if (dp[i] >= 1) {         ANS[w].S[0] = S[i];         ANS[w].S[1] = S[i + 1];         ANS[w].S[2] = S[i + 2];         w++;       }     }     sort(ANS, ANS + w, cmp);     for (int i = (1); i < (w); i++)       if (strcmp(ANS[i].S, ANS[i - 1].S) == 0) ok[i] = 1;     x = 0;     for (int i = (0); i < (w); i++)       if (ok[i] == 0) x++;     printf( %d n , x);     for (int i = (0); i < (w); i++)       if (ok[i] == 0) printf( %s n , ANS[i].S);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   vector<long long> fac, ifac;   long long fast_pow(long long a, long long b, long long mod) {     if (!b) return 1;     long long l = fast_pow(a, b >> 1, mod);     l = l * l % mod;     if (b & 1) l = l * a % mod;     return l;   }   long long nk(int n, int k) {     if (n - k < 0) return 0;     return fac[n] * ifac[k] % mod * ifac[n - k] % mod;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int h, w, n;     cin >> h >> w >> n;     int mx = 2 * max(h, w) + 1;     fac.resize(mx);     ifac.resize(mx);     fac[0] = ifac[0] = 1;     for (int i = 1; i < mx; ++i)       fac[i] = fac[i - 1] * i % mod, ifac[i] = fast_pow(fac[i], mod - 2, mod);     vector<pair<int, int> > bad(n);     for (auto &el : bad) cin >> el.first >> el.second;     sort(bad.begin(), bad.end());     auto f = [&](pair<int, int> &x, pair<int, int> &y) {       return x.first <= y.first && x.second <= y.second;     };     vector<long long> dp(n);     for (int i = 0; i < n; ++i) {       dp[i] = nk(bad[i].first + bad[i].second - 2, bad[i].first - 1);       for (int j = 0; j < i; ++j)         if (f(bad[j], bad[i])) {           long long x =               nk(bad[i].first - bad[j].first + bad[i].second - bad[j].second,                  bad[i].first - bad[j].first);           dp[i] = (dp[i] - (dp[j] * x % mod) + mod) % mod;         }     }     for (int i = 0; i < n; ++i)       dp[i] = dp[i] * nk(h + w - bad[i].first - bad[i].second, h - bad[i].first) %               mod;     long long ans = accumulate(dp.begin(), dp.end(), 0ll) % mod;     ans = (nk(h + w - 2, w - 1) - ans + mod) % mod;     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   long long int dp[503][5030];   long long int cnt[100100];   long long int fav[100100];   int t[550];   int used[100100];   long long int h[15];   int main() {     cin.tie(nullptr);     ios::sync_with_stdio(false);     cin >> n >> m;     for (int i = 0; i < n * m; i++) {       int x;       cin >> x;       cnt[x]++;     }     for (int i = 0; i < n; i++) {       cin >> t[i];       fav[t[i]]++;     }     for (int i = 0; i < m; i++) {       cin >> h[i];     }     long long int ans = 0;     for (int i = 0; i < n; i++) {       if (used[t[i]]) continue;       used[t[i]] = 1;       cnt[t[i]] = min(cnt[t[i]], fav[t[i]] * (long long int)m);       for (int j = 0; j <= fav[t[i]]; j++) {         for (int k = 0; k <= cnt[t[i]]; k++) {           dp[j][k] = -1e15;         }       }       dp[0][0] = 0;       for (int j = 0; j < fav[t[i]]; j++) {         for (int k = 0; k <= cnt[t[i]]; k++) {           if (dp[j][k] < 0) continue;           for (int l = 0; l <= min((int)m, (int)cnt[t[i]] - k); l++) {             dp[j + 1][k + l] =                 max(dp[j + 1][k + l],                     dp[j][k] + (l == 0 ? 0 : (long long int)h[l - 1]));           }         }       }       long long int ma = 0;       for (int j = 0; j <= cnt[t[i]]; j++) {         ma = max(ma, dp[fav[t[i]]][j]);       }       ans += ma;     }     cout << ans << endl;   }