malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int MX = 200005;   const int MD = 998244353;   void add(int& a, int b) {     a += b;     if (a >= MD) a -= MD;   }   void sub(int& a, int b) {     a -= b;     if (a < 0) a += MD;   }   int mod_mult(long long a, long long b) { return (a * b) % MD; }   int h[MX];   int fact[MX];   int inv_fact[MX];   int exp(int a, int b) {     int res = 1;     while (b) {       if (b % 2 == 1) res = mod_mult(res, a);       a = mod_mult(a, a);       b /= 2;     }     return res;   }   int C(int n, int k) {     int tmp = fact[n];     tmp = mod_mult(tmp, inv_fact[k]);     tmp = mod_mult(tmp, inv_fact[n - k]);     return tmp;   }   int f(int x) {     int tmp = exp(2, x);     if (x % 2 == 0) {       sub(tmp, C(x, x / 2));     }     tmp = mod_mult(tmp, inv_fact[2]);     return tmp;   }   int main() {     fact[0] = 1;     for (int i = 1; i < MX; i++) {       fact[i] = mod_mult(fact[i - 1], i);     }     inv_fact[MX - 1] = exp(fact[MX - 1], MD - 2);     for (int i = MX - 2; i >= 0; i--) {       inv_fact[i] = mod_mult(inv_fact[i + 1], i + 1);     }     int n, k;     ignore = scanf( %d %d , &n, &k);     for (int i = 0; i < n; i++) {       ignore = scanf( %d , h + i);     }     int mult_at_end = 1;     int B = 0;     for (int i = 0; i < n; i++) {       if (h[i] == h[(i + 1) % n]) {         mult_at_end = mod_mult(mult_at_end, k);       } else {         B++;       }     }     int ans = 0;     for (int c = 0; c <= B; c++) {       int tmp = exp(k - 2, c);       tmp = mod_mult(tmp, C(B, c));       tmp = mod_mult(tmp, f(B - c));       add(ans, tmp);     }     ans = mod_mult(ans, mult_at_end);     printf( %d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 10;   const int M = 2;   struct Node {     int parent, rela;     void set(int p, int r) {       parent = p;       rela = r;     }   } node[maxn];   int Find(int x) {     int tmp_p;     if (node[x].parent != x) {       tmp_p = node[x].parent;       node[x].parent = Find(node[x].parent);       node[x].rela = (node[x].rela + node[tmp_p].rela) % M;     }     return node[x].parent;   }   void Merge(int a, int b, int p, int q, int d) {     node[p].parent = q;     node[p].rela = (node[b].rela - node[a].rela - d + M) % M;   }   int n, m;   const long long mod = 1e9 + 7;   void init() {     for (int i = 0; i < maxn; ++i) {       node[i].parent = i, node[i].rela = 0;     }   }   long long powerMod(int x, int k) {     long long res = 1;     long long base = x;     while (k) {       if (k & 1) res = res * base % mod;       base = base * base % mod;       k >>= 1;     }     return res;   }   int main() {     init();     int num = 0;     cin >> n >> m;     int res = 1;     for (int i = 0; i < m; ++i) {       int a, b, c;       cin >> a >> b >> c;       int p = Find(a);       int q = Find(b);       if (p != q)         Merge(a, b, p, q, c ^ 1);       else {         if ((node[a].rela - node[b].rela + 2) % 2 != 1 - c) res = 0;       }     }     if (res == 0)       cout << res << endl;     else {       for (int i = 1; i <= n; ++i)         if (Find(i) == i) num++;       cout << powerMod(2, num - 1) << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const long long MX = 1e9;   const long long INF = 1e9;   void solve() {     string s;     cin >> s;     int n = s.length(), mx[n + 1], c = 0;     for (int i = 0; i < s.length(); ++i) {       if (s[i] ==  1 )         ++c;       else if (c)         --c;       mx[i] = c;     }     mx[n + 1] = c;     for (int i = n - 1; i >= 0; --i) mx[i] = min(mx[i], mx[i + 1]);     int nz = 0;     for (int i = 0; i < n; ++i) {       if (s[i] ==  1 ) {         if (nz < mx[i + 1]) {           s[i] =  0 ;           ++nz;         }       }     }     cout << s <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 100000 + 10;   int mod;   int n, m;   vector<int> E[MAX_N];   bool v[MAX_N];   int cnt;   void dfs(int u) {     if (v[u]) return;     v[u] = true;     ++cnt;     for (vector<int>::iterator e = E[u].begin(); e != E[u].end(); ++e) {       dfs(*e);     }   }   int main() {     cin >> n >> m >> mod;     for (int i = 0; i < m; ++i) {       int a, b;       scanf( %d%d , &a, &b);       --a, --b;       E[a].push_back(b), E[b].push_back(a);     }     memset(v, 0, sizeof v);     vector<int> comp;     for (int i = 0; i < n; ++i) {       if (!v[i]) {         cnt = 0;         dfs(i);         comp.push_back(cnt);       }     }     if (comp.size() == 1) {       cout << 1 % mod << endl;       return 0;     }     long long ans = 1, sum = 0;     for (vector<int>::iterator e = comp.begin(); e != comp.end(); ++e) {       ans = ans * (*e) % mod;       sum += *e;       sum %= mod;     }     for (int i = 0; i < comp.size() - 2; ++i) {       ans = ans * sum % mod;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Edge {     int v;     int w;     Edge* next;     void add(int v, int w, Edge*& head) {       this->v = v;       this->w = w;       this->next = head;       head = this;     }   } edges[200100];   Edge* h[100100];   struct Q {     int v;     int t;     bool operator<(const Q& q) const { return v < q.v || v == q.v && t < q.t; }     void init(int v, int t) {       this->v = v;       this->t = t;     }   } q[100100], q0;   void addEdge(int v1, int v2, int w) {     static Edge* e = edges;     (e++)->add(v2, w, h[v1]);     (e++)->add(v1, w, h[v2]);   }   int d[100010];   struct CMP {     bool operator()(int a, int b) const {       return d[a] < d[b] || d[a] == d[b] && a < b;     }   };   set<int, CMP> heap;   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int i = 0; i < m; i++) {       int v1, v2, w;       scanf( %d %d %d , &v1, &v2, &w);       v1--, v2--;       addEdge(v1, v2, w);     }     int u = 0;     for (int i = 0; i < n; i++) {       int k;       scanf( %d , &k);       for (int j = 0; j < k; j++) {         int t;         scanf( %d , &t);         q[u++].init(i, t);       }     }     memset(d, 127, sizeof(d));     d[0] = 0;     heap.insert(0);     while (!heap.empty()) {       int v = *heap.begin();       heap.erase(heap.begin());       if (v == n - 1) {         printf( %d n , d[v]);         return 0;       }       int dist = d[v];       q0.init(v, dist);       Q* qi = lower_bound(q, q + u, q0);       while (qi < q + u && qi->v == v && qi->t == dist) {         dist++;         qi++;       }       for (Edge* e = h[v]; e; e = e->next) {         if (d[e->v] > dist + e->w) {           heap.erase(e->v);           d[e->v] = dist + e->w;           heap.insert(e->v);         }       }     }     printf( -1 n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long power(long long x, long long y, long long p) {     long long res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long nCrModp_2(long long n, long long r, long long p) {     if (r > n) {       return 0;     }     if (n < 0 || r < 0) {       return 0;     }     if (r == 0) return 1;     long long fac[n + 1];     fac[0] = 1;     for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p;     return (fac[n] * power(fac[r], p - 2, p) % p * power(fac[n - r], p - 2, p) %             p) %            p;   }   int main() {     long long n, m, k;     cin >> n >> m >> k;     long long ans = 0;     long long mod = 998244353;     ans = m;     ans = ans * power(m - 1, k, mod);     ans = ans % mod;     ans = ans * nCrModp_2(n - 1, k, mod);     ans = ans % mod;     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     int t, n, c_0, c_1, h;     string s;     cin >> t;     for (int i = 0; i < t; i++) {       cin >> n >> c_0 >> c_1 >> h;       cin >> s;       int sum = 0;       for (int j = 0; j < n; j++) {         sum += s[j] -  0 ;       }       if (c_0 - c_1 > h) {         sum = c_1 * n + (n - sum) * h;       } else if (c_1 - c_0 > h) {         sum = c_0 * n + sum * h;       } else         sum = c_1 * sum + (n - sum) * c_0;       cout << sum << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int p[1010], vst[1010];   char s[1010][1010];   int main() {     int n;     scanf( %d , &n);     int x = n, ed = 0, pos = 0;     for (int i = 1; i <= n; i++) {       scanf( %d , p + i);       if (p[i] != i) ed = i;       for (int j = 1; j <= n; j++) s[i][j] =  . ;     }     for (int i = 1; i <= n; i++)       if (p[i] == ed) pos = i;     p[pos] = p[ed], p[ed] = ed;     for (int i = 1; i <= ed; i++)       if (!vst[i] && p[i] != i) {         int u = i;         char dir =  / ;         while (!vst[u]) {           vst[u] = 1;           if (dir ==  / ) {             s[x][u] =  / ;             if (p[u] > u)               s[x][p[u]] =  / ;             else {               s[x--][ed] =  / ;               s[x][ed] = s[x][p[u]] = dir =     ;             }           } else {             s[x][u] =     ;             if (p[u] < u)               s[x][p[u]] =     ;             else {               s[x--][i] =     ;               s[x][i] = s[x][p[u]] = dir =  / ;             }           }           u = p[u];         }         x--;       }     if (ed) s[x][p[pos]] = s[x][ed] =  / ;     printf( %d n , n - !!ed);     for (int i = 1; i <= n; i++) printf( %s n , s[i] + 1);   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 100;   const int maxK = 5000 + 10;   const long long INF = 1e18;   int a[maxn], n, k;   long long f[maxK][maxK];   long long gao(int i, int j, int len) {     int s = i * (n / k) + j * (n / k + 1) + 1;     int t = s + len - 1;     return a[t] - a[s];   }   void solve() {     sort(a + 1, a + 1 + n);     int ni = k - n % k;     int nj = n % k;     f[0][0] = 0;     for (int i = 0; i <= ni; i++) {       for (int j = 0; j <= nj; j++) {         if (i == 0 && j == 0) continue;         f[i][j] = INF;         if (i >= 1) {           f[i][j] = min(f[i][j], f[i - 1][j] + gao(i - 1, j, n / k));         }         if (j >= 1) {           f[i][j] = min(f[i][j], f[i][j - 1] + gao(i, j - 1, n / k + 1));         }       }     }     printf( %I64d n , f[ni][nj]);   }   int main() {     while (scanf( %d%d , &n, &k) != EOF) {       for (int i = 1; i <= n; i++) scanf( %d , &a[i]);       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long a[n];     for (int i = 0; i < n; i++) cin >> a[i];     long long mx = 1e9 + 9, r = 0;     for (int i = n - 1; i >= 0; i--) {       a[i] = min(mx, a[i]);       r += a[i];       mx = min(mx, max(0ll, a[i] - 1));     }     cout << r <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100010, M = 100010, SIZE = 26, LEN = 1000010;   int n, m, dfk = 0;   namespace BIT {   int c[LEN];   void Add(int x, int k) {     for (; x <= dfk; x += x & -x) c[x] += k;     return;   }   int Sum(int x) {     int sum = 0;     for (; x; x -= x & -x) sum += c[x];     return sum;   }   }  // namespace BIT   struct Trie_Node;   struct Edge;   struct Trie_Node {     Trie_Node() {       dfn = size = 0;       head = NULL;       fail = NULL;       for (int i = 0; i < SIZE; ++i) link[i] = NULL;       return;     }     Trie_Node *fail, *link[SIZE];     Edge *head;     int dfn, size;   } * node[M];   struct Edge {     Edge() {       p = NULL;       nxt = NULL;       return;     }     Trie_Node *p;     Edge *nxt;   };   inline void Add_Edge(Trie_Node *u, Trie_Node *v) {     Edge *w = new Edge();     w->p = v;     w->nxt = u->head;     u->head = w;     return;   }   bool exist[M];   char buff[LEN];   queue<Trie_Node *> que;   void BFS(Trie_Node *root) {     root->fail = root;     for (int i = 0; i < SIZE; ++i)       if (root->link[i]) {         Add_Edge(root->link[i]->fail = root, root->link[i]);         que.push(root->link[i]);       } else         root->link[i] = root;     while (!que.empty()) {       Trie_Node *u = que.front();       que.pop();       for (int i = 0; i < SIZE; ++i)         if (u->link[i]) {           Add_Edge(u->link[i]->fail = u->fail->link[i], u->link[i]);           que.push(u->link[i]);         } else           u->link[i] = u->fail->link[i];     }     return;   }   void DFS(Trie_Node *u) {     u->dfn = ++dfk;     u->size = 1;     for (Edge *i = u->head; i; i = i->nxt) {       Trie_Node *v = i->p;       DFS(v);       u->size += v->size;     }     return;   }   long long Query(Trie_Node *root, char *s) {     long long res = 0;     int j = 1, m = strlen(s + 1);     for (Trie_Node *i = root; j <= m; ++j) {       i = i->link[s[j] - 97];       res += BIT::Sum(i->dfn);     }     return res;   }   int main() {     scanf( %d%d , &n, &m);     Trie_Node *root = new Trie_Node();     for (int i = 1; i <= m; ++i) {       scanf( %s , buff + 1);       int k = strlen(buff + 1);       node[i] = root;       for (int j = 1; j <= k; node[i] = node[i]->link[buff[j] - 97], ++j)         if (!node[i]->link[buff[j] - 97])           node[i]->link[buff[j] - 97] = new Trie_Node();     }     BFS(root);     DFS(root);     using namespace BIT;     for (int i = 1; i <= m; ++i) {       Add(node[i]->dfn, 1);       Add(node[i]->dfn + node[i]->size, -1);     }     memset(exist, 1, sizeof(exist));     for (int i = 1; i <= n; ++i) {       char t;       cin >> t;       if (t !=  ? ) {         int x;         scanf( %d , &x);         if (t !=  - ) {           if (!exist[x]) {             exist[x] = 1;             Add(node[x]->dfn, 1);             Add(node[x]->dfn + node[x]->size, -1);           }         } else if (exist[x]) {           exist[x] = 0;           Add(node[x]->dfn, -1);           Add(node[x]->dfn + node[x]->size, 1);         }       } else {         scanf( %s , buff + 1);         printf( %lld n , Query(root, buff));       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MX = 505, mod = 998244353;   int n, m;   vector<int> c, pos[MX];   long long mem[2 * MX][2 * MX];   bool isValid(int a, int b) {     if (a != pos[c[a]][0] && a != pos[c[a]].back()) return 0;     if (b != pos[c[b]][0] && b != pos[c[b]].back()) return 0;     for (int i = 1; i <= n; i++)       if ((a <= pos[i][0] && pos[i][0] <= b) !=           (a <= pos[i].back() && pos[i].back() <= b))         return 0;     return 1;   }   long long dp(int a, int b) {     if (a > b) return 1;     long long &res = mem[a][b];     if (res != -1) return res;     if (!isValid(a, b)) return res = 0;     int mn = c[a];     for (int i = a; i <= b; i++) mn = min(mn, c[i]);     int ind = pos[mn][0];     long long x = dp(a, ind - 1);     for (int i = a; i < ind; i++) x += dp(a, i) * dp(i + 1, ind - 1) % mod;     x %= mod;     ind = pos[mn].back();     long long y = dp(ind + 1, b);     for (int i = ind + 1; i <= b; i++) y += dp(ind + 1, i) * dp(i + 1, b) % mod;     y %= mod;     res = 1;     for (int i = 0; i + 1 < pos[mn].size(); i++)       (res *= dp(pos[mn][i] + 1, pos[mn][i + 1] - 1)) %= mod;     (res *= x) %= mod;     (res *= y) %= mod;     return res;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m;     c.resize(m);     for (int i = 0; i < m; i++) cin >> c[i];     c.erase(unique(c.begin(), c.end()), c.end());     if (c.size() > 2 * n) {       cout << 0 <<   n ;       return 0;     }     m = c.size();     for (int i = 0; i < m; i++) pos[c[i]].emplace_back(i);     memset(mem, -1, sizeof(mem));     cout << dp(0, m - 1) <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18, MOD = 1e9 + 7, MOD2 = 1e6 + 3;   vector<int> g[1000001];   int p[1000001], c[1000001], a[1000001], sz[1000001], root, n;   char sub(string a, string b) {     for (int i = 0; i < b.size(); i++) {       if (a[i] != b[i]) return a[i];     }     return a.back();   }   vector<string> query(int l, int r) {     vector<string> v;     cout <<  ?   << l <<     << r << endl;     fflush(stdout);     for (int i = 0; i < (r - l + 1) * (r - l + 2) / 2; i++) {       string s;       cin >> s;       sort(s.begin(), s.end());       v.push_back(s);     }     return v;   }   int main() {     int n;     cin >> n;     if (n == 1) {       auto a = query(1, n);       cout <<  !   << a[0] << endl;       fflush(stdout);       return 0;     }     auto a = query(1, n);     auto b = query(1, n - 1);     sort(a.begin(), a.end());     sort(b.begin(), b.end());     vector<string> suf;     int l = 0, r = 0;     while (l < a.size() || r < b.size()) {       if (r == b.size() || a[l] != b[r]) {         suf.push_back(a[l]);         l++;       } else {         l++, r++;       }     }     sort(suf.begin(), suf.end(),          [&](string& a, string& b) { return a.size() < b.size(); });     string ans;     ans += suf[0];     for (int i = 1; i < suf.size(); i++) ans += sub(suf[i], suf[i - 1]);     reverse(ans.begin(), ans.end());     cout <<  !   << ans << endl;     fflush(stdout);   }
#include <bits/stdc++.h>   using namespace std;   const int siz = 1000000 + 10;   char a[siz];   int main() {     int t, n, m = 0, i, l, k;     scanf( %d , &t);     scanf( %s , a);     int len = strlen(a);     int z = 0, o = 0;     for (l = 0; l < len; l++) {       if (a[l] ==  1 )         o++;       else if (a[l] ==  0 )         z++;     }     m = min(o, z);     len -= (2 * m);     printf( %d , len);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, N, id[6100];   bool judge(int n) {     if (n < 2) return false;     for (int i = 2; i * i <= n; i++)       if (n % i == 0) return false;     return true;   }   void work() {     if (judge(N)) return;     if (N % 2 && !judge(N - 2)) N -= 3, id[3] = 3;     for (int i = 1; i <= n; i++) {       if (judge(i) && judge(N - i)) {         id[i] = 2;         break;       }     }   }   int main() {     cin >> n;     N = n * (n + 1) / 2;     for (int i = 1; i <= n; i++) id[i] = 1;     if (n == 1) puts( -1 );     work();     for (int i = 1; i <= n; i++) cout << id[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0), cin.tie(), cout.tie();     bool w = false;     int cnt = 0;     string s;     cin >> s;     for (int i = 0; i < s.size(); i++) {       int zero = 0, one = 0;       if (w && s[i] ==  0 ) {         cout << 1 <<     << 1 << endl;         w = false;       } else if (s[i] ==  0 ) {         cout << 3 <<     << 1 << endl;         w = true;       } else {         cout << 4 - cnt <<     << 2 << endl;         cnt++;         if (cnt == 4) cnt = 0;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double t[100001], v[100001], fin[100001];   int main() {     double n, a, d;     cin >> n >> a >> d;     for (int i = 0; i < n; ++i) {       cin >> t[i] >> v[i];     }     cout.precision(5);     for (int i = 0; i < n; ++i) {       fin[i] = 2 * d * a <= v[i] * v[i] ? sqrt(2.0 * d / a)                                         : (d / v[i] + v[i] / (2 * a));       fin[i] += t[i];       if (i) fin[i] = max(fin[i], fin[i - 1]);       cout << fixed << fin[i] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   int a[N], p[N], d[N];   int main(int argc, char const *argv[]) {     int n, gain = 0;     scanf( %d n , &n);     for (int i = 2; i <= n; i++) scanf( %d , p + i);     for (int i = 1; i <= n; i++) d[i] = d[p[i]] + 1;     for (int i = 1; i <= n; i++) a[d[i]] += 1;     for (int i = 1; i <= n; i++) gain += (a[i] % 2);     printf( %d n , gain);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 503;   char thien[N][N];   int n, m, visit[N][N];   vector<pair<int, int> > adj[N][N];   int hang[4] = {-1, 0, 1, 0};   int cot[4] = {0, 1, 0, -1};   struct ds {     char type;     int u, v;   };   vector<ds> Ans;   void DFS(int u, int v) {     visit[u][v] = true;     Ans.push_back({ B , u, v});     for (int dir = 0; dir < 4; ++dir) {       int nu = u + hang[dir], nv = v + cot[dir];       if (nu < 1 || nu > n || nv < 1 || nv > m || thien[nu][nv] ==  #  ||           visit[nu][nv])         continue;       adj[u][v].push_back({nu, nv}), DFS(nu, nv);     }   }   void phudo(int u, int v) {     for (auto it : adj[u][v]) {       int x = it.first, y = it.second;       phudo(x, y);       Ans.push_back({ D , x, y}), Ans.push_back({ R , x, y});     }   }   int main() {     if (fopen( trash.inp ,  r ))       freopen( trash.inp ,  r , stdin), freopen( trash.out ,  w , stdout);     cin >> n >> m;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) cin >> thien[i][j];     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         if (!visit[i][j] && thien[i][j] !=  # ) {           DFS(i, j);           phudo(i, j);         }     cout << Ans.size() <<   n ;     for (auto it : Ans) cout << it.type <<     << it.u <<     << it.v <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int a[1000010], b[1000010];   int f[1000010], g[1000010];   int tmp[1000010];   int ansf[1000010], ansg[1000010];   int main() {     int sv, n, m;     scanf( %d %d , &sv, &n);     int p = 0, q = 0;     for (int i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       x -= sv;       if (x == 0) {         puts( 1 );         return 0;       }       int y = abs(x);       if (x < 0)         a[++p] = y;       else         b[++q] = y;     }     if (!p || !q) {       puts( -1 );       return 0;     }     sort(a + 1, a + p + 1);     p = unique(a + 1, a + p + 1) - a - 1;     sort(b + 1, b + q + 1);     q = unique(b + 1, b + q + 1) - b - 1;     n = 0;     for (int i = 1; i <= p; i++) {       f[++n] = a[i];       ansf[a[i]] = 1;     }     for (int i = 1; i < 1005; i++) {       int N = 0;       for (int j = 1; j <= n; j++) {         for (int k = 1; k <= p; k++) {           int x = f[j] + a[k];           if (!ansf[x]) {             ansf[x] = i + 1;             tmp[++N] = x;           }         }       }       n = N;       for (int j = 1; j <= n; j++) f[j] = tmp[j];     }     m = 0;     for (int i = 1; i <= q; i++) {       g[++m] = b[i];       ansg[b[i]] = 1;     }     for (int i = 1; i < 1005; i++) {       int N = 0;       for (int j = 1; j <= m; j++) {         for (int k = 1; k <= q; k++) {           int x = g[j] + b[k];           if (!ansg[x]) {             ansg[x] = i + 1;             tmp[++N] = x;           }         }       }       m = N;       for (int j = 1; j <= m; j++) g[j] = tmp[j];     }     int res = 0x3f3f3f3f;     for (int i = 1; i < 1000010; i++) {       if (ansf[i] && ansg[i]) res = min(res, ansf[i] + ansg[i]);     }     cout << res << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     long long a[n + 10];     for (int i = 1; i <= n; ++i) scanf( %lld , &a[i]);     long long x, y;     cin >> x >> y;     long long h = x + y;     long long sum = 0;     for (int i = 1; i <= n; ++i) {       sum = sum + y * (a[i] / (h));       a[i] = a[i] % h;       if (a[i] > x) sum += y;     }     cout << sum;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 2e3 + 1;   int dp[MAX][4], in[MAX];   int main() {     ios::sync_with_stdio(0);     int n, ans = 1;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> in[i];       if (in[i] == 1)         dp[i][0] = 1;       else         dp[i][1] = 1;       for (int j = 0; j < i; ++j) {         if (in[i] == 1 && in[j] == 1) {           dp[i][0] = max(dp[i][0], dp[j][0] + 1);           dp[i][2] = max(dp[i][2], dp[j][2] + 1);         }         if (in[i] == 2 && in[j] == 2) {           dp[i][1] = max(dp[i][1], dp[j][1] + 1);           dp[i][3] = max(dp[i][3], dp[j][3] + 1);         }         if (in[i] == 1 && in[j] == 2) {           dp[i][2] = max(dp[i][2], dp[j][1] + 1);         }         if (in[i] == 2 && in[j] == 1) {           dp[i][1] = max(dp[i][1], dp[j][0] + 1);           dp[i][3] = max(dp[i][3], dp[j][2] + 1);         }       }       for (int j = 0; j < 4; ++j) ans = max(ans, dp[i][j]);     }     cout << ans << endl;     return 0;   }