malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1010;   int N;   long long c[4][maxn][maxn];   void upd(int p, int i, int j, long long x) {     int jj = j;     while (i <= N) {       j = jj;       while (j <= N) c[p][i][j] ^= x, j += j & (-j);       i += i & (-i);     }   }   long long get(int p, int i, int j) {     int jj = j;     long long ans = 0;     while (i) {       j = jj;       while (j) ans ^= c[p][i][j], j -= j & (-j);       i -= i & (-i);     }     return ans;   }   void upd(int i, int j, long long x) {     upd(0, i, j, x);     upd(1, i, j, x * (i % 2));     upd(2, i, j, x * (j % 2));     upd(3, i, j, x * (i % 2) * (j % 2));   }   long long get(int i, int j) {     long long s0 = get(0, i, j);     long long s1 = get(1, i, j);     long long s2 = get(2, i, j);     long long s3 = get(3, i, j);     int x = i, y = j;     x++, y++;     x %= 2, y %= 2;     return (x * y * s0) ^ (y * s1) ^ (x * s2) ^ (s3);   }   int main() {     int n, m, i, j, k;     int x0, x1, y0, y1;     scanf( %d%d , &n, &m);     N = n;     while (m--) {       scanf( %d , &i);       if (i == 1) {         scanf( %d%d%d%d , &x0, &y0, &x1, &y1);         printf( %I64d n , get(x1, y1) ^ get(x0 - 1, y1) ^ get(x1, y0 - 1) ^                               get(x0 - 1, y0 - 1));       } else {         scanf( %d%d%d%d , &x0, &y0, &x1, &y1);         long long v;         scanf( %I64d , &v);         upd(x0, y0, v);         upd(x0, y1 + 1, v);         upd(x1 + 1, y0, v);         upd(x1 + 1, y1 + 1, v);       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<long long int> v(n);     for (int i = 0; i < n; i++) {       cin >> v[i];     }     int i;     for (int i = 0; i < n; i++) {       while (v[i] % 2 == 0) {         v[i] /= 2;       }       while (v[i] % 3 == 0) {         v[i] /= 3;       }     }     cout << endl;     vector<long long int>::iterator it = min_element(v.begin(), v.end());     vector<long long int>::iterator it2 = max_element(v.begin(), v.end());     if (*it == *it2)       cout <<  Yes ;     else       cout <<  No ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long max(long long a, long long b) {     if (a < b) return b;     return a;   }   long long min(long long a, long long b) {     if (a < b) return a;     return b;   }   bool okay(vector<long long> &v, long long d, long long k) {     long long sum = 0;     priority_queue<long long, vector<long long>, greater<long long>> pq;     for (int i = 0; i < d; i++) pq.push(0);     for (auto x : v) {       long long ind = pq.top();       pq.pop();       sum += max(0, x - ind);       pq.push(ind + 1);     }     return sum >= k;   }   void solve() {     long long n, m;     cin >> n >> m;     vector<long long> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     sort(v.rbegin(), v.rend());     long long low = 1, high = n, ans = -1;     while (low <= high) {       long long mid = low + (high - low) / 2;       if (okay(v, mid, m)) {         ans = mid;         high = mid - 1;       } else         low = mid + 1;     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N;   bool v[2001];   vector<int> G[2001];   int cnt = 0;   int a[2001][2001];   int Dfn[2001], Low[2001];   int Dindex = 0;   int Bcnt;   bool instack[2001];   int Sta[2001];   int top = -1;   void add(int x, int y) { G[x].push_back(y); }   void read() {     scanf( %d , &N);     for (int i = 0; i < N; ++i)       for (int j = 0; j < N; ++j) {         scanf( %d , &a[i][j]);       }     for (int i = 0; i < N; ++i)       for (int j = 0; j < N; ++j)         if (a[i][j] && i != j) add(i, j);   }   void tarjan(int v) {     Dfn[v] = Low[v] = ++Dindex;     instack[v] = 1;     Sta[++top] = v;     for (int i = 0; i < G[v].size(); ++i) {       int w = G[v][i];       if (!Dfn[w]) {         tarjan(w);         Low[v] = min(Low[v], Low[w]);       } else if (instack[w])         Low[v] = min(Low[v], Dfn[w]);     }     int j;     if (Dfn[v] == Low[v]) {       Bcnt++;       do {         j = Sta[top--];         instack[j] = 0;       } while (j != v);     }   }   int main() {     read();     Bcnt = 0;     for (int i = 0; i < N; ++i)       if (!Dfn[i]) tarjan(i);     if (Bcnt > 1)       puts( NO );     else       puts( YES );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 4e6 + 10;   int n, m;   int a[N];   int find(int x) {     for (int i = 1; i <= n; i++) {       if (a[i] == x) return i;     }   }   vector<int> v;   signed main() {     int x, y, z, T;     cin >> T;     while (T--) {       scanf( %d , &n);       for (int i = 1; i <= n; i++) scanf( %d , &a[i]);       bool f = 0;       for (int i = 1; i <= n; i += 2)         if (a[i] % 2 == 0) f = 1;       if (f) {         puts( -1 );         continue;       }       v.clear();       for (int i = n; i >= 1; i -= 2) {         if (i == 1) continue;         int l = find(i), r = find(i - 1);         if (l == i && r == i - 1) continue;         if (l == 1 && r == 2) {           v.push_back(i);           reverse(a + 1, a + 1 + i);           continue;         }         v.push_back(l);         reverse(a + 1, a + 1 + l);         l = 1, r = find(i - 1);         if (l == 1 && r == 2) {           v.push_back(i);           reverse(a + 1, a + 1 + i);           continue;         }         v.push_back(r - 1);         reverse(a + 1, a + 1 + r - 1);         v.push_back(r + 1);         reverse(a + 1, a + 1 + r + 1);         v.push_back(3);         reverse(a + 1, a + 1 + 3);         v.push_back(i);         reverse(a + 1, a + 1 + i);       }       cout << v.size() << endl;       for (int i : v) cout << i <<    ;       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, t, c, d[555555], res = 0, ll = -1;   bool try_ans(int k) {     if (d[n - 1] > k) return false;     int last = 0, now = 0, res, l2 = -1;     vector<pair<int, int> > aa;     for (int i = 0; i < n; i++) {       if (d[i] > k) {         l2 = i;         if (!last) aa.push_back(make_pair(i, now));         last = 1;         now += 2;       } else if (last == 1)         last = 2, l2 = i;       else         last = 0;     }     if (ll > l2) l2 = ll;     res = now + l2 + 1;     for (int i = aa.size() - 1; i >= 0; i--)       if (2 * l2 + 1 - aa[i].first + aa[i].second < res)         res = 2 * l2 + 1 - aa[i].first + aa[i].second;     return res <= t;   }   int ans(int max, int min) {     if (max == min) return min;     int mid = (max + min) / 2;     bool a = try_ans(mid);     if (a)       return ans(mid, min);     else       return ans(max, mid + 1);   }   int main() {     cin >> n >> t;     string str;     cin >> str;     int now = 0;     for (int i = 0; i < n; i++) {       c = str[i];       if (c ==  H ) now++, ll = i;       if (c ==  S ) now--;       d[i] = now;       if (now > res) res = now;     }     if (ll + 1 > t)       cout << -1;     else {       cout << ans(res, 0);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   void read(T& x) {     x = 0;     int fl = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) fl = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 1) + (x << 3) + ch -  0 ;       ch = getchar();     }     x *= fl;   }   template <typename T, typename... Args>   inline void read(T& t, Args&... args) {     read(t);     read(args...);   }   const int N = 3005;   int T, n, a[N], b[N];   int main() {     for (read(T); T; T--) {       read(n);       for (int i = 1; i <= n; i++) read(a[i]), b[i] = a[i];       sort(b + 1, b + n + 1);       int len = unique(b + 1, b + n + 1) - (b + 1);       for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;       int now = 1, mx = 0;       while (now <= n) {         int len = 0;         for (int i = 1; i <= n; i++)           if (a[i] == now) len++, now++;         mx = max(mx, len);       }       printf( %d n , n - mx);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int MN = 200010;   int exp(int x, int n) {     int ret = 1;     while (n) {       if (n & 1) ret = 1LL * ret * x % mod;       x = 1LL * x * x % mod;       n >>= 1;     }     return ret;   }   int inv(int x) { return exp(x, mod - 2); }   int fact[MN], invf[MN];   int comb(int n, int k) {     if (n < k) return 0;     return 1LL * fact[n] * invf[k] % mod * invf[n - k] % mod;   }   int S(int n, int k) {     int ret = 0;     for (int i = 0; i < k; i++) {       ret += 1LL * comb(k, i) * (i % 2 ? mod - 1 : 1) % mod * exp(k - i, n) % mod;       ret %= mod;     }     ret = 1LL * ret * invf[k] % mod;     return ret;   }   int N, K;   int W[MN];   int main() {     fact[0] = 1;     for (int i = 1; i < MN; i++) {       fact[i] = 1LL * fact[i - 1] * i % mod;     }     for (int i = 0; i < MN; i++) {       invf[i] = inv(fact[i]);     }     scanf( %d %d , &N, &K);     int sum = 0;     for (int i = 0; i < N; i++) {       scanf( %d , &W[i]);       sum += W[i];       sum %= mod;     }     int ans =         1LL * ((S(N, K) + 1LL * (N - 1) * S(N - 1, K) % mod) % mod) * sum % mod;     printf( %d , ans);   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int MAXN = 1e5 + 10;   bool bd[MAXN];   int n;   int a[MAXN];   vector<pair<int, int>> sol;   pair<int, int> coord(pair<int, int> p) { return {(n - 1 - p.second), p.first}; }   int main(void) {     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %d , &a[i]);     for (int i = 0; i < n; ++i) bd[i] = false;     int lst = -1;     queue<int> dva;     for (int i = n - 1; i >= 0; --i) {       if (a[i] == 0) continue;       while (!dva.empty() && bd[dva.front()]) dva.pop();       if ((a[i] == 2 && dva.empty()) || (a[i] == 3 && lst == -1)) {         printf( -1 n );         return 0;       }       if (a[i] == 1) {         sol.emplace_back(i, (n - 1) - i);         lst = i;         dva.push(i);         continue;       }       if (a[i] == 2) {         int xx = dva.front();         dva.pop();         bd[i] = bd[xx] = true;         sol.emplace_back(i, (n - 1) - xx);         lst = i;       }       if (a[i] == 3) {         sol.emplace_back(i, (n - 1) - i);         sol.emplace_back(lst, (n - 1) - i);         bd[i] = bd[lst] = true;         lst = i;       }     }     printf( %d n , (int)sol.size());     for (auto &p : sol) {       auto c = coord(p);       printf( %d %d n , c.first + 1, c.second + 1);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string moves[] = { MW ,  LW ,  LM };   int sequence[25];   void listify(int k, int n) {     for (int i = 0; i != n; ++i) {       sequence[i] = k % 3;       k /= 3;     }   }   void solve(int n, vector<vector<int> >& V) {     int n1 = n / 2;     int n2 = n - n1;     int k1 = pow(3, n1), k2 = pow(3, n2);     vector<int> state(3);     map<pair<int, int>, pair<int, int> > sums;     for (int k = 0; k != k1; ++k) {       listify(k, n1);       state[0] = state[1] = state[2] = 0;       for (int i = 0; i != n1; ++i) {         state[(sequence[i] + 1) % 3] += V[i][(sequence[i] + 1) % 3];         state[(sequence[i] + 2) % 3] += V[i][(sequence[i] + 2) % 3];       }       pair<int, int> p(state[1] - state[0], state[2] - state[0]);       if (sums.find(p) == sums.end() || sums[p].first < state[0]) {         sums[p] = make_pair(state[0], k);       }     }     int max_equal_rel = -1000000000;     pair<int, int> argmax;     for (int k = 0; k != k2; ++k) {       listify(k, n2);       state[0] = state[1] = state[2] = 0;       for (int i = 0; i != n2; ++i) {         state[(sequence[i] + 1) % 3] += V[i + n1][(sequence[i] + 1) % 3];         state[(sequence[i] + 2) % 3] += V[i + n1][(sequence[i] + 2) % 3];       }       pair<int, int> p(state[0] - state[1], state[0] - state[2]);       if (sums.find(p) != sums.end() &&           sums[p].first + state[0] > max_equal_rel) {         max_equal_rel = sums[p].first + state[0];         argmax = make_pair(sums[p].second, k);       }     }     if (max_equal_rel == -1000000000) {       cout <<  Impossible  << endl;     } else {       listify(argmax.first, n1);       for (int i = 0; i != n1; ++i) {         cout << moves[sequence[i]] << endl;       }       listify(argmax.second, n2);       for (int i = 0; i != n2; ++i) {         cout << moves[sequence[i]] << endl;       }     }   }   int main() {     int n;     cin >> n;     vector<vector<int> > V(n, vector<int>(3));     for (int i = 0; i != n; ++i) {       cin >> V[i][0] >> V[i][1] >> V[i][2];     }     solve(n, V);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long powmod(long long a, long long b) {     long long res = 1;     a %= 1000000007;     for (; b; b >>= 1) {       if (b & 1) res = res * a % 1000000007;       a = a * a % 1000000007;     }     return res;   }   template <typename T>   inline bool chkmin(T &a, const T &b) {     return a > b ? a = b, 1 : 0;   }   template <typename T>   inline bool chkmax(T &a, const T &b) {     return a < b ? a = b, 1 : 0;   }   int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   const int P = 10000007;   unsigned int p[500100], l[500100], r[500100];   int m;   char ans[500100], s[500100];   int n;   bool ok(int i, int x) {     if (++i < x) return 0;     return ((r[i] - r[i - x] * p[x]) * p[i - x]) == (l[i] - l[i - x]);   }   bool dfs(int i, int x) {     if (i == n) {       puts(ans);       return 1;     }     ans[i] = x ? s[i] :  a ;     for (; ans[i] <=  z ; ans[i]++) {       l[i + 1] = l[i] + ans[i] * p[i];       r[i + 1] = r[i] * P + ans[i];       if (!ok(i, m) && !ok(i, m + 1) && dfs(i + 1, x && (ans[i] == s[i])))         return 1;     }     return 0;   }   int main() {     m = read();     scanf( %s , s);     n = strlen(s);     int i;     for (i = n - 1; i >= 0 && s[i] ==  z ; i--) s[i] =  a ;     if (i == -1) {       puts( Impossible );       return 0;     }     s[i]++;     p[0] = 1;     for (int i = 1; i < 500100; i++) p[i] = p[i - 1] * P;     if (!dfs(0, 1)) puts( Impossible );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int morh[100001], parent[100001], pos[100001], temp = 0, ans = 0;   int calculate(int i) {     int posi = i, ans = 0;     while (posi != 0) {       if (pos[posi] > 1)         break;       else {         posi = parent[posi];         ans++;       }     }     return ans;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &morh[i]);     }     for (int i = 1; i <= n; i++) {       scanf( %d , &parent[i]);       pos[parent[i]]++;     }     int temp2;     for (int i = 1; i <= n; i++) {       if (morh[i] == 1) {         temp2 = calculate(i);         if (temp2 > ans) {           ans = temp2;           temp = i;         }       }     }     temp2 = ans;     int give_ans[ans], i = 0;     for (int j = 0; j < temp2; j++) {       give_ans[i] = temp;       i++;       temp = parent[temp];     }     printf( %d n , ans);     for (int i = ans - 1; i >= 0; i--) printf( %d  , give_ans[i]);     printf(  n );   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const int maxn = 2e5 + 50;   const long long inf = 1e17;   int t;   void get() {     fflush(stdout);     scanf( %d , &t);     for (int i = 1; i <= t; i++) scanf( %*s );   }   int main() {     do {       puts( next 0 1 ), get();       puts( next 0 ), get();     } while (t == 3);     do {       puts( next 0 1 2 3 4 5 6 7 8 9 ), get();     } while (t == 2);     puts( done );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int max_n = 2e5 + 5;   long long ans[max_n];   struct no {     long long p, val;   };   vector<no> v;   bool operator<(const no &x, const no &y) { return x.p < y.p; }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       long long x, y;       scanf( %lld%lld , &x, &y);       v.push_back(no{x, 1});       v.push_back(no{y + 1, -1});     }     sort(v.begin(), v.end());     long long add = v[0].val, now = v[0].p;     for (int i = 1; i < v.size();) {       for (int j = i; j < v.size(); j++) {         if (v[j].p == now) {           add = add + v[j].val;           i++;         } else {           ans[add] += v[j].p - now;           add += v[j].val;           now = v[j].p;           i = j + 1;           break;         }       }     }     for (int i = 1; i <= n; i++) printf( %lld  , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   void read(T& x) {     x = 0;     char ch = getchar();     long long f = 1;     while (!isdigit(ch)) {       if (ch ==  - ) f *= -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch - 48;       ch = getchar();     }     x *= f;   }   template <typename T, typename... Args>   void read(T& first, Args&... args) {     read(first);     read(args...);   }   template <typename T>   void write(T arg) {     T x = arg;     if (x < 0) {       putchar( - );       x = -x;     }     if (x > 9) {       write(x / 10);     }     putchar(x % 10 +  0 );   }   template <typename T, typename... Ts>   void write(T arg, Ts... args) {     write(arg);     if (sizeof...(args) != 0) {       putchar(   );       write(args...);     }   }   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   const int N = 1e5 + 5;   int n, m;   int a[N], b[N], fa[N << 1];   int cnt = 0;   long long res = 0;   struct node {     int u, v, w;     bool operator<(const node& no) const { return w > no.w; }   } edge[N << 1];   int findroot(int x) {     if (fa[x] == x) return x;     return fa[x] = findroot(fa[x]);   }   bool merge(int x, int y) {     int fax = findroot(x);     int fay = findroot(y);     if (fax != fay) {       fa[fax] = fay;       return true;     }     return false;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> m >> n;     int tot = n;     for (int i = 1; i <= m; i++) cin >> a[i];     for (int i = 1; i <= n; i++) cin >> b[i];     for (int i = 1; i <= m; i++) {       ++tot;       int s;       cin >> s;       for (int j = 1; j <= s; j++) {         int v;         cin >> v;         edge[++cnt].u = tot, edge[cnt].v = v, edge[cnt].w = a[i] + b[v];         res += edge[cnt].w;       }     }     sort(edge + 1, edge + 1 + cnt);     for (int i = 1; i <= tot + 5; i++) fa[i] = i;     for (int i = 1; i <= cnt; i++) {       int u = edge[i].u, v = edge[i].v, w = edge[i].w;       if (merge(u, v)) res -= w;     }     cout << res << endl;   }
#include <bits/stdc++.h>   using namespace std;   long long a[100005];   long long c[100005];   int main() {     int n, k;     scanf( %d %d , &n, &k);     for (int i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       a[i] -= k;     }     long long A;     scanf( %lld , &A);     priority_queue<long long, vector<long long>, greater<long long> > P;     long long cnt = 0;     long long ans = 0;     long long now = 0;     for (int i = 1; i <= n; i++) {       scanf( %lld , &c[i]);     }     for (int i = 1; i <= n; i++) {       P.push(c[i]);       if (a[i] > cnt) {         long long t;         if (a[i] < now)           t = 0;         else           t = ((a[i] - now) % A == 0 ? (a[i] - now) / A : (a[i] - now) / A + 1);         for (int j = 1; j <= t; j++) {           if (P.empty()) {             ans = -1;             break;           }           ans += P.top();           now += A;           P.pop();         }         cnt = a[i];         if (ans == -1) break;       }     }     printf( %lld , ans);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t, n, k;     cin >> t;     while (t--) {       cin >> n >> k;       long long int a[n];       set<long long int> s;       for (long long int i = 0; i < n; i++) {         cin >> a[i];         s.insert(a[i]);       }       sort(a, a + n);       if (k == 1 && s.size() == 1) {         cout <<  1 n ;         continue;       }       if (k == 1 && s.size() != 1) {         cout <<  -1 n ;         continue;       }       long long int c = 0;       for (long long int i = 0; i < n - 1; i++) {         if (a[i + 1] - a[i] != 0) {           c++;         }       }       long long int o = 1;       cout << max(o, ((c + k - 2) / (k - 1))) <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> ways[400], a[400], num[400];   vector<int> ans;   struct edge {     int a, b;   };   vector<edge> gr;   bool dfs(int v, int f, int pr) {     if (v == f) return true;     for (int i = 0; i < a[v].size(); i++) {       if (a[v][i] != pr)         if (dfs(a[v][i], f, v)) return true;     }     return false;   }   void findway(int v, int f, int j) {     if (v == f) {       ways[j].push_back(v);       return;     }     for (int i = 0; i < a[v].size(); i++) {       if (dfs(a[v][i], f, v)) {         ways[j].push_back(v);         findway(a[v][i], f, j);         return;       }     }   }   int main() {     cin >> n;     for (int i = 0; i < n - 1; i++) {       int z, l;       cin >> z >> l;       a[l].push_back(z);       a[z].push_back(l);       num[l].push_back(i + 1);       num[z].push_back(i + 1);     }     int num = 0;     int k;     while (cin >> k) {       findway(1, k, num);       num++;     }     for (int i = 0; i < ways[0].size(); i++) {       ans.push_back(ways[0][i]);     }     for (int i = 1; i < num; i++) {       int j = 0;       while (ways[i - 1][j] == ways[i][j]) j++;       j--;       for (int z = ways[i - 1].size() - 2; z >= j; z--) {         ans.push_back(ways[i - 1][z]);       }       for (int z = j + 1; z < ways[i].size(); z++) {         ans.push_back(ways[i][z]);       }     }     for (int i = ways[num - 1].size() - 2; i >= 0; i--) {       ans.push_back(ways[num - 1][i]);     }     if (ans.size() != 2 * n - 1)       cout << -1;     else {       for (int i = 0; i < ans.size(); i++) {         cout << ans[i] <<    ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = (long long)4e18;   int n, q;   long long c[110000];   long long v[110000];   long long d[110000];   int u[110000];   int main() {     ios::sync_with_stdio(0);     cin >> n >> q;     for (int i = 0; i < n; ++i) cin >> v[i];     for (int i = 0; i < n; ++i) cin >> c[i];     while (q--) {       long long a, b;       cin >> a >> b;       for (int i = 0; i <= n; ++i) d[i] = -inf;       long long et = 0;       pair<long long, int> m1 = make_pair(-inf, -1);       pair<long long, int> m2 = make_pair(-inf, -1);       for (int i = 0; i < n; ++i) {         long long ans = max(v[i] * b, d[c[i]]);         ans = max(ans, d[c[i]] + v[i] * a);         if (m1.second != c[i])           ans = max(ans, m1.first + v[i] * b);         else           ans = max(ans, m2.first + v[i] * b);         if (ans > m1.first)           if (m1.second != c[i])             m2 = m1, m1 = make_pair(ans, c[i]);           else             m1.first = ans;         else if (ans > m2.first && c[i] != m1.second)           m2 = make_pair(ans, c[i]);         d[c[i]] = max(d[c[i]], ans);         et = max(et, ans);       }       cout << et <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   const int inf = 0x3c3c3c3c;   const long long infl = 0x3c3c3c3c3c3c3c3c;   long long dp[5003][5003];   int A, B, C;   int main() {     ios::sync_with_stdio(0), cin.tie(0);     ;     cin >> A >> B >> C;     vector<int> v({A, B, C});     sort((v).begin(), (v).end());     for (int i = 0; i <= 5000; i++) dp[i][0] = 1;     for (int i = 1; i <= 5000; i++) {       for (int j = 1; j <= 5000; j++) {         dp[i][j] = (dp[i - 1][j - 1] * i + dp[i][j - 1]) % mod;       }     }     long long ans = 1;     ans = (ans * dp[v[2]][v[1]]) % mod;     ans = (ans * dp[v[2]][v[0]]) % mod;     ans = (ans * dp[v[1]][v[0]]) % mod;     cout << ans;   }