#include <bits/stdc++.h>   using namespace std;   struct point {     double x, y;     point(double _x = 0, double _y = 0) {       x = _x;       y = _y;     }     point operator+(point w) { return point(x + w.x, y + w.y); }     point operator-(point w) { return point(x - w.x, y - w.y); }     point operator*(double H) { return point(x * H, y * H); }     double operator/(point w) {       if (abs(w.x) > 1E-7) return x / w.x;       return y / w.y;     }     bool operator==(point w) {       return (abs(x - w.x) < 1E-7 && abs(y - w.y) < 1E-7);     }   };   double sp(point a, point b) { return (a.x * b.x + a.y * b.y); }   int ORI(point a, point b) {     double O = a.x * b.y - a.y * b.x;     return (abs(O) < 1E-7) ? 0 : (O > 0 ? 1 : -1);   }   struct prava {     double a, b, c;     prava(point f = point(0, 0), point t = point(1, 1)) {       a = t.y - f.y;       b = f.x - t.x;       c = t.x * f.y - f.x * t.y;     }     point IP(prava w) {       point ret;       if (abs(a) < 1E-7) {         ret.y = (-c) / b;         ret.x = (-w.b * ret.y - w.c) / w.a;       } else {         ret.y = (w.a * c - a * w.c) / (a * w.b - w.a * b);         ret.x = (-b * ret.y - c) / a;       }       return ret;     }   };   point BEG;   double X1, Y1, Z1, X2, Y2, Z2, D;   int n;   point M[10000];   point L1, L2;   bool is_in(point w) {     w = w - BEG;     if (ORI(L1, L2) == 0) {       if (!(L1 == point(0, 0)) && ORI(L1, w) == 0 && (w / L1 >= 0)) return true;       if (!(L2 == point(0, 0)) && ORI(L2, w) == 0 && (w / L2 >= 0)) return true;       return false;     }     return (ORI(L1, L2) * ORI(L1, w) >= 0 && ORI(L2, L1) * ORI(L2, w) >= 0);   }   double t1 = 1E9, t2 = 1E9;   void get_time(point w) {     if (ORI(L1, L2) != 0) {       prava B(BEG, BEG + L1);       double T1, T2, T3;       point WIND(X2, Y2);       T3 = (B.a * w.x + B.b * w.y + B.c) / (B.a * WIND.x + B.b * WIND.y);       if (T3 < 0 && abs(T3) < 1E-7) T3 = 0;       if (T3 < 0) return;       T1 = point(w - WIND * T3 - BEG) / point(X1, Y1);       if (T1 < 0 && abs(T1) < 1E-7) T1 = 0;       if (T1 < 0) return;       T2 = (T1 * Z1 + T3 * Z2) / (-D);       if (T2 < 0 && abs(T2) < 1E-7) T2 = 0;       if (T2 < 0) return;       if (t1 > T1 || (abs(t1 - T1) < 1E-7 && t2 > T2)) {         t1 = T1;         t2 = T2;       }     } else {       double T1, T2, T3;       if (!(point(X1, Y1) == point(0, 0)) && point(X2, Y2) / point(X1, Y1) < 0) {         T1 = (w - BEG) / point(X1, Y1);         T2 = (-Z1 * T1) / D;         if (T1 < 0 && abs(T1) < 1E-7) T1 = 0;         if (T2 < 0 && abs(T2) < 1E-7) T2 = 0;         if (T1 < 0 || T2 < 0) goto NEXT_TYPE;         if (t1 > T1 || (abs(t1 - T1) < 1E-7 && t2 > T2)) {           t1 = T1;           t2 = T2;         }       NEXT_TYPE:         point SUM = point(X1, Y1) + point(X2, Y2) * (-Z1 / Z2);         if (SUM == point(0, 0)) return;         T1 = (w - BEG) / SUM;         T2 = 0;         if (T1 < 0 && abs(T1) < 1E-7) T1 = 0;         if (T1 < 0 || T2 < 0) return;         if (t1 > T1 || (abs(t1 - T1) < 1E-7 && t2 > T2)) {           t1 = T1;           t2 = T2;         }       } else {         point SUM = point(X1, Y1) + point(X2, Y2) * (-Z1 / Z2);         if (SUM == point(0, 0)) return;         T1 = (w - BEG) / SUM;         T2 = 0;         if (T1 < 0 && abs(T1) < 1E-7) T1 = 0;         if (T1 < 0 || T2 < 0) return;         if (t1 > T1 || (abs(t1 - T1) < 1E-7 && t2 > T2)) {           t1 = T1;           t2 = T2;         }       }     }   }   int main() {     int i, j, k, l;     scanf( %d , &n);     for (i = 0; i < n; ++i) scanf( %lf%lf , &M[i].x, &M[i].y);     scanf( %lf%lf , &BEG.x, &BEG.y);     scanf( %lf%lf%lf%lf%lf%lf%lf , &X1, &Y1, &Z1, &D, &X2, &Y2, &Z2);     L1 = point(X1, Y1);     L2 = L1 - point(X2, Y2) * (Z1 / Z2);     for (i = 0; i < n; ++i)       if (is_in(M[i])) get_time(M[i]);     for (i = 0; i < n; ++i) {       j = i + 1;       if (j >= n) j -= n;       if (ORI(L1, M[i] - BEG) * ORI(L1, M[j] - BEG) < 0) {         point T = prava(BEG, BEG + L1).IP(prava(M[i], M[j]));         if (is_in(T)) get_time(T);       }       if (ORI(L2, M[i] - BEG) * ORI(L2, M[j] - BEG) < 0) {         point T = prava(BEG, BEG + L2).IP(prava(M[i], M[j]));         if (is_in(T)) get_time(T);       }     }     if (t1 < 1E9)       printf( %.12lf %.12lf n , t1, t2);     else       printf( -1 -1 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, nrh, nrt, nod1, nod2, d[100006], x, nr1, nr2, nra;   vector<int> G[100006];   vector<int> sters;   int uz[100006], uz2[100006];   int main() {     int i, j, k;     cin >> n >> m >> nrh >> nrt;     for (i = 1; i <= m; i++) {       cin >> nod1 >> nod2;       G[nod1].push_back(nod2);       G[nod2].push_back(nod1);       d[nod1]++;       d[nod2]++;     }     for (i = 1; i <= n; i++)       if (d[i] > nrh) uz[i] = 1;     for (i = 1; i <= n; i++) {       if (d[i] > nrt) {         for (j = 0; j < G[i].size(); j++) {           if (uz[G[i][j]]) {             nra = nr1 = nr2 = 0;             x = G[i][j];             for (k = 0; k < G[i].size(); k++) {               if (k != j) {                 uz2[G[i][k]]++;                 nr1++;                 sters.push_back(G[i][k]);               }             }             for (k = 0; k < G[x].size(); k++) {               if (G[x][k] != i) {                 uz2[G[x][k]]++;                 if (uz2[G[x][k]] == 2)                   nra++;                 else                   sters.push_back(G[x][k]);                 nr2++;               }             }             if (nr1 + nr2 - nra < nrt + nrh) {               for (k = 0; k < sters.size(); k++) uz2[sters[k]] = 0;               sters.clear();               continue;             }             cout <<  YES n ;             cout << G[i][j] <<     << i <<   n ;             for (k = 0; k < G[x].size() && nrh; k++) {               if (uz2[G[x][k]] == 1) {                 cout << G[x][k] <<    ;                 nrh--;               }             }             for (k = 0; nrh; k++) {               if (uz2[G[x][k]] == 2) {                 uz2[G[x][k]] = 0;                 cout << G[x][k] <<    ;                 nrh--;               }             }             cout <<   n ;             for (k = 0; k < G[i].size() && nrt; k++) {               if (uz2[G[i][k]]) {                 cout << G[i][k] <<    ;                 nrt--;               }             }             return 0;           }         }       }     }     cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int i, j;   int main() {     int n, s, d, flag, last;     cin >> n;     cin >> s >> d;     flag = s;     n--;     while (n--) {       cin >> s >> d;       while (flag >= s) s += d;       flag = s;     }     cout << flag;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int q;     cin >> q;     unordered_map<string, string> gr;     set<string> ending;     for (int i = 0; i < q; i++) {       string prev, next;       cin >> prev >> next;       gr[prev] = next;       ending.insert(next);     }     vector<pair<string, string>> ans;     for (auto it : gr) {       if (ending.count(it.first)) continue;       string cur = it.first;       while (gr.count(cur)) {         cur = gr[cur];       }       ans.push_back({it.first, cur});     }     cout << ans.size() <<   n ;     for (auto it : ans) cout << it.first <<     << it.second <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 510;   const int MAXS = 110;   const int MAXM = 1E5 + 10;   const double EPS = 1E-8;   int siz, n, m, k, ban[MAXN], pos[MAXN], U[MAXM], V[MAXM], d[MAXN], id[MAXN];   vector<int> G[MAXN];   struct Mat {     double a[MAXS][MAXS];     Mat() { memset(a, 0, sizeof a); }     double* operator[](int x) { return a[x]; }     const double* operator[](int x) const { return a[x]; }     Mat operator*(const Mat& rhs) const {       Mat ret;       for (int i = 0; i <= siz; i++)         for (int j = 0; j <= siz; j++)           for (int k = 0; k <= siz; k++) ret[i][j] += a[i][k] * rhs[k][j];       return ret;     }   } trans;   Mat ksm(Mat a, long long b) {     Mat ret;     for (int i = 0; i <= siz; i++) ret[i][i] = 1;     for (; b; b >>= 1, a = a * a)       if (b & 1) ret = ret * a;     return ret;   }   double A[MAXN][MAXN << 1], C[MAXN], X[MAXN];   void Guass() {     for (int i = 1; i <= n; i++) {       int pos = i;       for (int j = i + 1; j <= n; j++)         if (abs(A[j][i]) > abs(A[pos][i])) pos = j;       if (abs(A[pos][i]) < EPS) throw  No Matrix Inverse ;       for (int j = 1; j <= 2 * n; j++) swap(A[pos][j], A[i][j]);       for (int j = i + 1; j <= n; j++) {         if (abs(A[j][i]) < EPS) continue;         double coef = A[j][i] / A[i][i];         for (int k = 1; k <= 2 * n; k++) A[j][k] -= A[i][k] * coef;       }     }     for (int i = n; i; i--) {       double inv = 1.0 / A[i][i];       for (int j = 1; j <= 2 * n; j++) A[i][j] = A[i][j] * inv;       for (int j = i - 1; j; j--) {         double coef = A[j][i] / A[i][i];         for (int k = 1; k <= 2 * n; k++) A[j][k] -= A[i][k] * coef;       }     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) swap(A[i][j], A[i][j + n]);   }   void solve() {     for (int i = 1; i <= n; i++) X[i] = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) X[j] += C[i] * A[j][i];   }   int main() {     scanf( %d%d%d , &n, &m, &k);     pos[++siz] = 1;     id[1] = 1;     for (int i = 1; i <= n; i++) {       scanf( %d , &ban[i]);       if (ban[i]) pos[++siz] = i, id[i] = siz;     }     for (int i = 1; i <= n; i++) A[i][i] = A[i][i + n] = 1;     for (int i = 1; i <= m; i++) {       scanf( %d%d , &U[i], &V[i]);       G[U[i]].push_back(V[i]);       G[V[i]].push_back(U[i]);       ++d[U[i]];       ++d[V[i]];     }     for (int i = 1; i <= m; i++) {       int u = U[i], v = V[i];       if (!ban[u]) A[v][u] -= 1.0 / d[u];       if (!ban[v]) A[u][v] -= 1.0 / d[v];     }     Guass();     for (int i = 1; i <= siz; i++) {       memset(C, 0, sizeof C);       int x = pos[i];       if (x == 1)         C[1] = 1, solve();       else {         C[x] = 1;         for (int v : G[x]) C[v] += 1.0 / d[x];         solve();       }       double tot = 1;       for (int j = 1; j <= n; j++)         if (ban[j] && j != x) trans[i][id[j]] = X[j], tot -= X[j];       trans[i][i] = tot;     }     trans[siz][0] = 1;     Mat res;     res[0][1] = 1;     res = res * ksm(trans, k);     printf( %.10lf n , res[0][0]);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 805;   const int INF = 0x3f3f3f3f;   int n, m, k, lim;   struct Edge {     int from, to, flow, cap;     Edge(int f, int t, int fl, int c) : from(f), to(t), flow(fl), cap(c) {}   };   struct EK {     int n;     vector<Edge> E;     vector<int> g[N];     int p[N], df[N];     bool vis[N];     void init(int n) {       this->n = n;       for (int i = 1; i <= n; i++) g[i].clear();       E.clear();     }     void addEdge(int u, int v, int c) {       E.push_back(Edge(u, v, 0, c));       E.push_back(Edge(v, u, 0, 0));       int sz = E.size();       g[u].push_back(sz - 2);       g[v].push_back(sz - 1);     }     int bfs(int S, int T) {       memset(df, 0, sizeof(df));       memset(vis, 0, sizeof(vis));       queue<int> Q;       df[S] = 0x3f3f3f3f, vis[S] = 1;       Q.push(S);       while (!Q.empty()) {         int u = Q.front();         Q.pop();         for (int i = 0; i < ((int)g[u].size()); i++) {           Edge &e = E[g[u][i]];           if (!vis[e.to] && e.cap > e.flow) {             p[e.to] = g[u][i];             vis[e.to] = 1;             df[e.to] = min(df[e.from], e.cap - e.flow);             Q.push(e.to);           }         }         if (vis[T]) break;       }       for (int u = T; u != S; u = E[p[u]].from) {         E[p[u]].flow += df[T];         E[p[u] ^ 1].flow -= df[T];       }       return df[T];     }     int maxFlow(int S, int T) {       int F = 0;       while (1) {         int D = bfs(S, T);         if (!D) break;         F += D;       }       return F;     }   } T;   bool done[N];   vector<pair<int, int> > g[N];   int dis[N][N], st[N];   void dij(int s) {     memset(dis[s], 0x3f, sizeof(dis[s]));     memset(done, 0, sizeof(done));     priority_queue<pair<int, int>, vector<pair<int, int> >,                    greater<pair<int, int> > >         Q;     Q.push(make_pair(0, s));     dis[s][s] = 0;     while (!Q.empty()) {       int u = Q.top().second;       Q.pop();       if (done[u]) continue;       done[u] = 1;       for (int i = 0; i < ((int)g[u].size()); i++) {         int v = g[u][i].first, w = g[u][i].second;         if (dis[s][v] > dis[s][u] + w) {           dis[s][v] = dis[s][u] + w;           Q.push(make_pair(dis[s][v], v));         }       }     }   }   int tot;   bool check(int x) {     T.init(n + tot + 2);     int cur = 0;     for (int i = 1; i <= n; i++) {       if (st[i]) {         T.addEdge(n + tot + 1, ++cur, st[i]);         for (int j = 1; j <= n; j++)           if (dis[i][j] <= x) {             T.addEdge(cur, j + tot, 1);           }       }       T.addEdge(i + tot, n + tot + 2, 1);     }     return T.maxFlow(n + tot + 1, n + tot + 2) >= lim;   }   int main() {     scanf( %d%d%d%d , &n, &m, &k, &lim);     for (int i = 0; i < k; i++) {       int x;       scanf( %d , &x);       st[x]++;     }     for (int i = 1; i <= n; i++)       if (st[i]) tot++;     while (m--) {       int u, v, w;       scanf( %d%d%d , &u, &v, &w);       g[u].push_back(make_pair(v, w));       g[v].push_back(make_pair(u, w));     }     for (int i = 1; i <= n; i++) dij(i);     int l = 0, r = 1731312;     while (l + 1 < r) {       int mid = (l + r) / 2;       if (check(mid))         r = mid;       else         l = mid;     }     int ans = check(l) ? l : r;     printf( %d n , ans > 1731311 ? -1 : ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, k;     cin >> n >> k;     long long a1[n], b1[n];     for (int i = 0; i < n; i++) cin >> a1[i] >> b1[i];     long long dp[n][k];     vector<long long> a, b;     vector<pair<pair<long long, long long>, long long> > e;     vector<long long> ans;     long long mx = 0;     for (int p = 0; p < n; p++) {       a.clear();       b.clear();       e.clear();       for (int i = 0; i < n; i++)         if (i == p)           continue;         else           e.push_back(make_pair(make_pair(b1[i], a1[i]), i));       sort(e.begin(), e.end());       a.push_back(0);       b.push_back(0);       for (int i = 0; i < e.size(); i++) {         a.push_back(e[i].first.second);         b.push_back(e[i].first.first);       }       for (int i = 0; i < n; i++)         for (int j = 0; j < k; j++) dp[i][j] = -1e9;       dp[0][0] = 0;       for (int i = 1; i < n; i++)         for (int j = 0; j < k; j++) {           dp[i][j] = max(dp[i][j], dp[i - 1][j] + b[i] * (k - 1));           if (j > 0)             dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + b[i] * (j - 1) + a[i]);         }       if (mx < a1[p] + b1[p] * (k - 1) + dp[n - 1][k - 1]) {         mx = a1[p] + b1[p] * (k - 1) + dp[n - 1][k - 1];         vector<long long> x, bad;         int i = n - 1, j = k - 1;         while (dp[i][j] > 0) {           if (j > 0) {             if (dp[i - 1][j - 1] + b[i] * (j - 1) + a[i] == dp[i][j]) {               x.push_back(e[i - 1].second + 1);               i--;               j--;             } else {               bad.push_back(-e[i - 1].second - 1);               bad.push_back(e[i - 1].second + 1);               i--;             }           } else {             bad.push_back(-e[i - 1].second - 1);             bad.push_back(e[i - 1].second + 1);             i--;           }         }         reverse(x.begin(), x.end());         reverse(bad.begin(), bad.end());         for (int i = 0; i < bad.size(); i++) x.push_back(bad[i]);         x.push_back(p + 1);         ans = x;       }     }     cout << ans.size() <<   n ;     for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ;     cout <<   n ;   }   int main() {     ios::sync_with_stdio(0);     int q;     cin >> q;     while (q--) solve();   }


#include <bits/stdc++.h>   using namespace std;   vector<long long int> v;   long long int a[120100], n, dd;   map<int, int> mp;   int main() {     for (long long int i = 1; i <= 2000000000; i *= 2) {       v.push_back(i);     }     cin >> n;     for (int i = 0; i < n; i++) {       scanf( %lld , &a[i]);       mp[a[i]]++;     }     sort(a, a + n);     int f, res = 0;     for (int i = 0; i < n; i++) {       f = 0;       for (int j = 0; j < v.size(); j++) {         if (v[j] <= a[i]) continue;         dd = v[j] - a[i];         if (dd == a[i]) {           if (mp[a[i]] >= 2) {             f = 1;             break;           }           continue;         }         if (mp[dd] >= 1) {           f = 1;           break;         }       }       if (f == 0) res++;     }     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     long long a, b, c;   } e[100005];   int cmp(node a, node b) { return a.c > b.c; }   int main() {     int n, f, i;     cin >> n >> f;     for (i = 1; i <= n; i++) {       scanf( %lld%lld , &e[i].a, &e[i].b);       if (e[i].a >= e[i].b) {         e[i].c = 0;       } else {         e[i].c = min(e[i].a * 2, e[i].b) - e[i].a;       }     }     sort(e + 1, e + 1 + n, cmp);     long long ans = 0;     for (i = 1; i <= f; i++) {       ans += min(2 * e[i].a, e[i].b);     }     for (; i <= n; i++) {       ans += min(e[i].a, e[i].b);     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MaxN = 100010;   struct adjList {     int id, nxt;   } Lst[MaxN * 2];   int LCnt, Head[MaxN];   int N, w[MaxN], root;   void addEdge(int a, int b) {     Lst[LCnt].id = b;     Lst[LCnt].nxt = Head[a];     Head[a] = LCnt++;   }   pair<long long, long long> dfs(int id, int pre) {     vector<pair<long long, long long> > v;     for (int x = Head[id]; x != -1; x = Lst[x].nxt) {       if (Lst[x].id != pre) {         v.push_back(dfs(Lst[x].id, id));       }     }     sort(v.begin(), v.end());     long long sum = 1, rem = 0;     w[id]--;     for (int i = (int)(v.size()) - 1; i >= 0; i--) {       if (w[id] == 0) break;       w[id]--;       sum += v[i].first + 1;       rem += v[i].second;     }     if (w[id]) {       sum += 2 * min((long long)w[id], rem);       w[id] -= min((long long)w[id], rem);     }     return make_pair(sum, w[id]);   }   int main() {     while (cin >> N) {       memset(Head, -1, sizeof(Head));       LCnt = 0;       for (int i = 1; i <= N; i++) {         scanf( %d , w + i);       }       for (int i = 1; i < N; i++) {         int a, b;         scanf( %d %d , &a, &b);         addEdge(a, b);         addEdge(b, a);       }       cin >> root;       w[root]++;       cout << dfs(root, -1).first - 1 << endl;       ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000000000;   map<int, int> mp, mp2;   map<int, int>::iterator it;   int n, m;   int tmp[3] = {2, 3, 5}, flag, t;   void bfs(int val, int cnt) {     queue<int> q;     mp[val] = cnt;     q.push(val);     while (!q.empty()) {       int x = q.front();       q.pop();       for (int i = 0; i < 3; ++i) {         if (x % tmp[i] == 0 && x != 0) {           if (mp.count(x / tmp[i]) == 0) {             mp[x / tmp[i]] = mp[x] + 1;             q.push(x / tmp[i]);           }         }       }     }   }   void bfs1(int val, int cnt) {     if (mp.count(val) != 0) {       flag = mp[val];       return;     }     queue<int> q;     mp2[val] = cnt;     q.push(val);     while (!q.empty() && flag == -1) {       int x = q.front();       q.pop();       for (int i = 0; i < 3 && flag == -1; ++i) {         if (x % tmp[i] == 0 && x != 0 && flag == -1) {           if (mp.count(x / tmp[i]) != 0 && flag == -1) {             flag = mp[x / tmp[i]] + mp2[x] + 1;             return;           }           if (mp2.count(x / tmp[i]) == 0) {             mp2[x / tmp[i]] = mp2[x] + 1;             q.push(x / tmp[i]);           }         }       }     }   }   int main() {     while (cin >> n >> m) {       if (n == m) {         cout <<  0 n ;         continue;       }       mp.clear();       mp2.clear();       t = 0;       bfs(n, 0);       flag = -1;       bfs1(m, 0);       cout << flag << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, m, k;     cin >> n >> m >> k;     if (n > m) swap(n, m);     set<long long> vals;     for (long long i = 1; i <= n; ++i) {       if (vals.count(n / i)) {         for (long long j = n / i; j >= 1; --j) {           vals.insert(j);         }         break;       }       vals.insert(n / i);     }     long long res = -1;     int diff = 2;     vals.insert(n);     vals.insert(m);     for (long long x : vals) {       for (long long i = n / x - diff; i <= n / x + diff; ++i) {         if (i < 0 || i >= n) continue;         long long j = k - i;         if (j < 0 || j >= m) continue;         res = max(res, 1LL * (n / (i + 1)) * (m / (j + 1)));       }       for (long long i = x - diff; i <= x + diff; ++i) {         if (i < 0 || i >= n) continue;         long long j = k - i;         if (j < 0 || j >= m) continue;         res = max(res, 1LL * (n / (i + 1)) * (m / (j + 1)));       }     }     if (k < n) res = max(res, 1LL * m * (n / (k + 1)));     if (k < m) res = max(res, 1LL * n * (m / (k + 1)));     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   const int Mod = 1000000007;   const int INF = 0x3f3f3f3f;   const long long LL_INF = 0x3f3f3f3f3f3f3f3f;   const double e = exp(1);   const double PI = acos(-1);   const double ERR = 1e-10;   int main() {     long long n;     scanf( %lld , &n);     long long ans = (1LL * (1 + n) * n) / 2;     if (ans % 2 == 0)       printf( 0 n );     else       printf( 1 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 500;   const int M = 1e6 + 500;   const int INF = 0x3f3f3f3f;   const int MOD = 1e9 + 7;   const int BASE = 31337;   const int LOG = 18;   const int OFF = (1 << LOG);   const double EPS = 1e-9;   const double PI = 3.1415926535;   int blok[M], n, bio[M], pref[M], lft[M], q, k;   int get(int l, int r) {     if (l <= 0) return pref[r];     return pref[r] - pref[l - 1];   }   int check(int x) {     int sol = 0;     for (int i = 0; i < n; i++) {       if (blok[i]) {         if (lft[i] <= i - x) {           return -1;         } else {           i = lft[i] + x - 1;           sol++;         }       } else {         i += x - 1;         sol++;       }     }     return sol;   }   int main() {     scanf( %d%d%d , &n, &q, &k);     for (int i = 0; i < q; i++) {       int x;       scanf( %d , &x);       blok[x] = 1;     }     if (blok[0])       lft[0] = -INF;     else       lft[0] = 0;     for (int i = 1; i < n; i++) {       if (!blok[i])         lft[i] = i;       else         lft[i] = lft[i - 1];     }     long long fin = (long long)INF * INF;     for (int i = 1; i <= k; i++) {       int x;       scanf( %d , &x);       int y = check(i);       if (y == -1) continue;       fin = min(fin, (long long)x * y);     }     if (fin == (long long)INF * INF)       printf( -1 n );     else       printf( %I64d n , fin);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long o = 101;   const long long p = 6e4;   struct qwq {     long long x, y;   } a[o], b[o];   long long n, m, l, r, f[p + 1], ans = -1;   bool cmp1(qwq u, qwq v) { return u.x == v.x ? u.y > v.y : u.x < v.x; }   bool cmp2(qwq u, qwq v) { return u.x + u.y > v.x + v.y; }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m;     for (long long i = 1, x, y; i <= n; i++) {       cin >> x >> y;       if (y >= 0)         a[++l] = (qwq){x, y};       else         b[++r] = (qwq){x, y};     }     memset(f, -1, sizeof(f)), f[m] = 0;     sort(a + 1, a + l + 1, cmp1), sort(b + 1, b + r + 1, cmp2);     for (long long i = 1; i <= l; i++)       for (long long j = p; j >= a[i].x; j--)         if (f[j] != -1) f[j + a[i].y] = max(f[j] + 1, f[j + a[i].y]);     for (long long i = 1; i <= r; i++)       for (long long j = max(b[i].x, -b[i].y); j <= p; j++)         if (f[j] != -1) f[j + b[i].y] = max(f[j] + 1, f[j + b[i].y]);     for (long long i = 0; i <= p; i++) ans = max(f[i], ans);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> g[5000];   int n, m;   int c[100][100];   char t[100][100];   bool used[100][100];   char clr[5000];   bool valid(int i, int j) { return (0 <= i && i < n && 0 <= j && j < m); };   int di[4] = {-1, 1, 0, 0};   int dj[4] = {0, 0, -1, 1};   void dfs(int i, int j, int color) {     used[i][j] = true;     c[i][j] = color;     for (int k = 0; k < 4; k++) {       int i1 = i + di[k];       int j1 = j + dj[k];       if (valid(i1, j1) && t[i1][j1] == t[i][j] && !used[i1][j1])         dfs(i1, j1, color);     }   }   void add_edge(int i, int j) {     g[i].push_back(j);     g[j].push_back(i);   }   void dfs1(int i, int j) {     used[i][j] = true;     for (int k = 0; k < 4; k++) {       int i1 = i + di[k];       int j1 = j + dj[k];       if (valid(i1, j1)) {         if (t[i1][j1] == t[i][j] && !used[i1][j1]) dfs1(i1, j1);         if (t[i1][j1] != t[i][j]) add_edge(c[i1][j1], c[i][j]);       }     }   }   int l = 0;   bool _used[10000];   int h[3000];   int q[10000];   int bfs(int v) {     for (int i = 0; i < l; i++) {       _used[i] = false;       h[i] = 1000 * 1000 * 1000;     };     int rp = 0;     int wp = 1;     q[0] = v;     h[v] = 0;     while (rp != wp) {       v = q[rp];       rp++;       for (int i = 0; i < g[v].size(); i++) {         int u = g[v][i];         if (h[u] > h[v] + 1) {           h[u] = h[v] + 1;           q[wp] = u;           wp++;         }       }     }     int vmax = 0;     for (int i = 0; i < l; i++) vmax = max(vmax, h[i]);     return vmax;   }   int main() {     scanf( %d%d n , &n, &m);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) scanf( %c , &t[i][j]);       scanf(  n );     }     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (!used[i][j]) {           clr[l] = t[i][j];           dfs(i, j, l++);         }     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) used[i][j] = false;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (!used[i][j]) dfs1(i, j);     int best = 1000 * 1000 * 1000;     for (int i = 0; i < l; i++) {       int cur = bfs(i);       if (clr[i] ==  W  && (cur & 1)) cur++;       if (clr[i] ==  B  && (cur % 2 == 0)) cur++;       best = min(best, cur);     }     cout << best << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1 << 22;   int h, g;   long long a[N];   vector<int> ans;   int path[25];   long long pull(int u) {     long long tp = a[u];     int cnt = 0;     path[cnt++] = u;     a[u] = 0;     while ((u << 1) < (1 << h) && max(a[u << 1], a[(u << 1) | 1]) > 0) {       if (a[u << 1] > a[(u << 1) | 1]) {         swap(a[u << 1], a[u]);         u <<= 1;       } else {         swap(a[(u << 1) | 1], a[u]);         u = (u << 1) | 1;       }       path[cnt++] = u;     }     if (u < (1 << g)) {       for (int i = cnt - 1; i >= 0; i--) {         a[path[i]] = a[path[i - 1]];       }       a[path[0]] = tp;       return 0;     }     return tp;   }   void solve() {     ans.clear();     cin >> h >> g;     long long sum = 0;     for (int i = 1; i <= (1 << h) - 1; i++) {       cin >> a[i];       sum += a[i];     }     for (int i = 1; i <= (1 << g) - 1; i++) {       while (true) {         long long v = pull(i);         if (v) {           sum -= v;           ans.push_back(i);         } else           break;       }     }     cout << sum << endl;     for (auto v : ans) cout << v <<    ;     cout << endl;   }   int main() {     int t;     ios::sync_with_stdio(0);     cin >> t;     while (t--) solve();     return 0;   }


#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;   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>   struct Node {     int sum, value;     Node *ch[2], *p;     __inline Node(int value = 0) : value(value) {       sum = 1;       ch[0] = ch[1] = p = NULL;     }     __inline void update() {       sum = 1 + (ch[0] ? ch[0]->sum : 0) + (ch[1] ? ch[1]->sum : 0);     }   } pool[211111], *cur = pool;   __inline bool cut(Node *y) {     if (!y) return 0;     Node *x = y->p;     if (x != NULL) {       bool d = y == x->ch[1];       x->ch[d] = NULL;       x->sum -= y->sum;       y->p = NULL;       return d;     }     return 0;   }   __inline void link(Node *x, Node *y, bool d) {     if (y == NULL) return;     ;     ;     if (x) {       ;       ;       y->p = x;       x->ch[d] = y;       x->sum += y->sum;     }   }   __inline void rotate(Node *x, bool d) {     ;     ;     Node *y = x->ch[d], *px = x->p, *B = y->ch[!d];     bool dd = cut(x);     cut(y);     cut(B);     link(x, B, d);     link(y, x, !d);     link(px, y, dd);   }   __inline Node *trace_root(Node *x) {     if (x == NULL) return NULL;     for (; x->p != NULL; x = x->p)       ;     return x;   }   __inline Node *trace(Node *x, bool d) {     if (x == NULL) return NULL;     for (; x->ch[d] != NULL; x = x->ch[d])       ;     return x;   }   __inline void splay(Node *x, Node *term) {     if (x == NULL) return;     for (; x->p != term;) {       Node *y = x->p;       bool dx = x == y->ch[1];       if (y->p == term)         rotate(y, dx);       else {         Node *z = y->p;         bool dy = y == z->ch[1];         if (dx == dy) {           rotate(z, dy);           rotate(y, dx);         } else {           rotate(y, dx);           rotate(z, dy);         }       }     }   }   __inline void rand_splay(Node *T) {     if (T == NULL) return;     Node *last;     for (last = T, T = T->ch[rand() & 1]; T != NULL;) T = T->ch[rand() & 1];     splay(last, NULL);   }   const int N = 222222;   std::map<int, Node *> node;   Node *tree[N];   int nxt[N], prv[N];   int n, h, m;   int main() {     std::ios::sync_with_stdio(false);     std::cin.tie(0);     std::cin >> h >> m >> n;     long long ans = 0;     for (int i = 0; i < h; ++i) nxt[i] = (i + m) % h, prv[i] = (i + h - m) % h;     for (int _ = 0; _ < n; ++_) {       char buf[3];       int id;       std::cin >> buf >> id;       if (buf[0] ==  + ) {         int th;         std::cin >> th;         Node *T = tree[th];         splay(T, NULL);         ans += T ? (T->ch[1] ? T->ch[1]->sum + 1 : 1) : 0;         if (T != NULL) {           Node *t = trace(T, 1);           th = nxt[t->value];           T = tree[th];           ;           ;         }         T = cur++;         T->value = th;         tree[th] = T;         Node *rt0 = tree[prv[th]];         splay(rt0, NULL);         Node *rt1 = trace_root(tree[nxt[th]]);         link(T, rt0, 0);         if (rt1 != rt0) link(T, rt1, 1);         node[id] = T;       } else {         Node *T = node[id];         splay(T, NULL);         Node *t = trace(T->ch[1], 1);         cut(T->ch[0]);         cut(T->ch[1]);         tree[T->value] = NULL;         node[id] = NULL;         if (t)           T = trace_root(tree[nxt[t->value]]);         else           T = NULL;         if (T) {           splay(t, NULL);           link(t, T, 1);         }       }     }     std::cout << ans << std::endl;     return 0;   }


#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;   inline char getc() {     static char buf[1 << 20], *p1 = buf, *p2 = buf;     return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)                ? EOF                : *p1++;   }   template <typename T>   void rd(T& x) {     int f = 0, c;     while (!isdigit(c = getc())) f ^= !(c ^ 45);     x = (c & 15);     while (isdigit(c = getc())) x = x * 10 + (c & 15);     if (f) x = -x;   }   template <typename T>   void pt(T x, int c = -1) {     if (x < 0) putchar( - ), x = -x;     if (x > 9) pt(x / 10);     putchar(x % 10 + 48);     if (c != -1) putchar(c);   }   const int N = 200005;   int n, m, Q, dp[N];   long long a[N];   namespace xay1 {   void sol() {     while (Q--) {       int t;       rd(t);       if (t == 1) {         int l, r, d;         rd(l), rd(r), rd(d);         if (d & 1)           for (int i = (l); i <= (r); ++i) a[i] ^= 1;       } else {         int l, r;         rd(l), rd(r);         int s = 0;         for (int i = (r); i >= (l); --i) {           dp[i] = a[i] & 1;           if (s) dp[i] = 0;           s = (s << 1 | dp[i]) & ((1 << m) - 1);         }         if (dp[l])           puts( 2 );         else           puts( 1 );       }     }   }   }  // namespace xay1   namespace xay2 {   struct node {     int w[22], len;   } t[N * 4][2];   int tg[N * 4];   void pst(int k1) { tg[k1] ^= 1, swap(t[k1][0], t[k1][1]); }   void psd(int k1) {     if (tg[k1]) {       pst(k1 * 2);       pst(k1 * 2 + 1);       tg[k1] = 0;     }   }   node mer(const node& k1, const node& k2) {     node k3;     k3.len = k1.len + k2.len;     for (int i = (1); i <= (m + 1); ++i) {       if (k2.w[i] == -1) {         if (k1.w[min(m + 1, i + k2.len)] == -1) {           k3.w[i] = -1;         } else           k3.w[i] = k1.w[min(m + 1, i + k2.len)];       } else {         if (k1.w[k2.w[i]] == -1) {           k3.w[i] = min(m + 1, k1.len + k2.w[i]);         } else           k3.w[i] = k1.w[k2.w[i]];       }     }     return k3;   }   void upd(int k1) {     t[k1][0] = mer(t[k1 * 2][0], t[k1 * 2 + 1][0]);     t[k1][1] = mer(t[k1 * 2][1], t[k1 * 2 + 1][1]);   }   void bud(int k1, int k2, int k3) {     if (k2 == k3) {       t[k1][0].len = t[k1][1].len = 1;       if (a[k2] & 1) {         for (int i = (1); i <= (m); ++i) t[k1][0].w[i] = -1;         t[k1][0].w[m + 1] = 1;         for (int i = (1); i <= (m + 1); ++i) t[k1][1].w[i] = -1;       } else {         for (int i = (1); i <= (m); ++i) t[k1][1].w[i] = -1;         t[k1][1].w[m + 1] = 1;         for (int i = (1); i <= (m + 1); ++i) t[k1][0].w[i] = -1;       }       return;     }     psd(k1);     int mid = (k2 + k3) >> 1;     bud(k1 * 2, k2, mid), bud(k1 * 2 + 1, mid + 1, k3);     upd(k1);   }   void mdf(int k1, int k2, int k3, int k4, int k5) {     if (k2 > k5 || k3 < k4) return;     if (k4 <= k2 && k3 <= k5) {       pst(k1);       return;     }     psd(k1);     int mid = (k2 + k3) >> 1;     mdf(k1 * 2, k2, mid, k4, k5), mdf(k1 * 2 + 1, mid + 1, k3, k4, k5);     upd(k1);   }   int res;   void ask(int k1, int k2, int k3, int k4, int k5) {     if (k2 > k5 || k3 < k4) return;     if (k4 <= k2 && k3 <= k5) {       if (t[k1][0].w[res] == -1) {         res = min(m + 1, res + t[k1][0].len);       } else {         res = t[k1][0].w[res];       }       return;     }     psd(k1);     int mid = (k2 + k3) >> 1;     ask(k1 * 2 + 1, mid + 1, k3, k4, k5);     ask(k1 * 2, k2, mid, k4, k5);   }   void sol() {     bud(1, 1, n);     while (Q--) {       int t;       rd(t);       if (t == 1) {         int l, r;         long long d;         rd(l), rd(r), rd(d);         if (d & 1) {           mdf(1, 1, n, l, r);         }       } else {         int l, r;         rd(l), rd(r);         res = m + 1;         ask(1, 1, n, l, r);         if (res == 1)           puts( 2 );         else           puts( 1 );       }     }   }   }  // namespace xay2   int main() {     rd(n), rd(m), rd(Q);     for (int i = (1); i <= (n); ++i) rd(a[i]), a[i] &= 1;     if (n <= 5000 && m <= 5000) {       xay1::sol();     } else {       xay2::sol();     }     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;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200200;   int n;   long long b[N], c[N];   long long a[N];   int dg[32];   void solve() {     memset((dg), (0), sizeof(dg));     long long sum = 0, suma = 0;     for (int i = (int)0; i < (int)n; ++i) sum += b[i] + c[i];     suma = sum / (n + n);     bool flag = 1;     for (int i = (int)0; i < (int)n; ++i) {       a[i] = (b[i] + c[i] - suma) / n;       if (a[i] < 0) flag = 0;     }     if (!flag) {       cout <<  -1 ;       return;     }     for (int i = (int)0; i < (int)n; ++i) {       int t = 0;       for (long long x = a[i]; x; x >>= 1) dg[t++] += (x & 1);     }     for (int i = (int)0; i < (int)n; ++i) {       long long x = a[i], sumi = 0;       for (int j = (int)0; j < (int)32; ++j)         if ((x >> j) & 1) sumi += (1 << j) * dg[j];       if (sumi != b[i]) {         flag = 0;         break;       }     }     if (!flag) {       cout <<  -1 ;       return;     }     for (int i = (int)0; i < (int)n; ++i) {       long long x = a[i], sumi = 0;       for (int j = (int)0; j < (int)32; ++j) {         if ((x >> j) & 1)           sumi += (1 << j) * n;         else           sumi += (1 << j) * dg[j];       }       if (sumi != c[i]) {         flag = 0;         break;       }     }     if (!flag) {       cout <<  -1 ;       return;     }     for (int i = (int)0; i < (int)n; ++i) cout << a[i] <<    ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (int i = (int)0; i < (int)n; ++i) cin >> b[i];     for (int i = (int)0; i < (int)n; ++i) cin >> c[i];     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (1 << 30) - 1;   const long double EPS = 1e-9;   const long double PI = fabs(atan2(0.0, -1.0));   int get(int n) {     int res = 0;     for (int b = 0; n > 0; b++) {       if (n & 1) res = b;       n >>= 1;     }     return res;   }   int p[1000100];   void solve() {     int n;     cin >> n;     vector<bool> was(n + 1, false);     for (int i = n; i >= 0; i--) {       for (int k = 23; k >= 0; k--) {         int cur = (1 << k) - 1;         int add = cur - i;         if (add < 0 || add > n) continue;         if (was[add]) continue;         was[add] = true;         p[i] = add;         break;       }     }     printf(          %lld            n ,         n * 1LL * (n + 1));     for (int i = 0; i <= n; i++) {       if (i > 0) printf(   );       printf( %d , p[i]);     }     printf(  n );   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     map<long long, long long> mp;     deque<long long> a;     long long n, k, d, m = 0;     cin >> n >> k;     for (auto i = 0; i < n; i++) {       cin >> d;       if (!mp[d]) {         mp[d] = 1;         a.push_front(d);         if (a.size() > k) {           mp[a.back()] = 0;           a.pop_back();         }       }     }     cout << a.size() <<   n ;     for (const auto val : a) cout << val <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   template <typename T>   using v2 = vector<vector<T>>;   template <typename T>   inline v2<T> fill(int r, int c, const T& t) {     return v2<T>(r, vector<T>(c, t));   }   ll a, b, c, d;   const ll INF = 1e18;   ll nc2(ll n) { return (n * (n - 1)) / 2; }   ll diff(ll n) { return a - b * d * (nc2(n) - nc2(n - 1)); }   ll value(ll n) {     ll x = nc2(n);     if (b * x > INF / d) {       return -INF;     }     return a * n - b * d * x;   }   void solve() {     cin >> a >> b >> c >> d;     if (a > b * c) {       cout <<  -1 n ;     } else {       ll lo = 1;       ll hi = c;       ll ans = -INF;       while (lo <= hi) {         ll mid = (lo + hi) / 2;         if (diff(mid) <= 0) {           hi = mid - 1;         } else {           lo = mid + 1;         }         ans = max(ans, value(mid));       }       cout << ans <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int T;     cin >> T;     for (int i = 0; i < T; ++i) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, l, r;   long long ans;   int pr[10000005];   int cnt[10000005];   void solve() {     n--;     for (int i = (1); i <= (10000000); i++) pr[i] = i;     for (int i = (2); i <= (10000000); i++)       if (pr[i] == i) {         for (int j = i + i; j <= 10000000; j += i) {           if (pr[j] == j) pr[j] = i;         }       }     for (int i = (l); i <= (r); i++) {       int temp = 1, num = i, tc = 0, tp = 1;       while (num > 1) {         int np = pr[num];         if (np == tp) {           tc++;           if (tc >= n) tc = 0;         } else {           for (int j = (1); j <= (tc); j++) temp *= tp;           tp = np;           tc = 1 % n;         }         num /= np;       }       for (int j = (1); j <= (tc); j++) temp *= tp;       cnt[temp]++;     }     for (int i = (1); i <= (r); i++) ans += 1LL * cnt[i] * (cnt[i] - 1);     printf( %lld n , ans);   }   int main() {     scanf( %d %d %d , &n, &l, &r);     if (n == 1)       printf( %d n , r - l + 1);     else       solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 200005;   int n, f[M], ans;   int tot, head[M], Next[M * 2], vet[M * 2], len[M * 2];   void add(int a, int b, int c) {     Next[++tot] = head[a], vet[tot] = b, len[tot] = c;     head[a] = tot;   }   void dfs1(int x, int pre) {     for (int i = head[x]; i; i = Next[i]) {       int y = vet[i], z = len[i];       if (y == pre) continue;       dfs1(y, x);       f[x] += f[y] + z;     }   }   void dfs2(int x, int pre) {     for (int i = head[x]; i; i = Next[i]) {       int y = vet[i], z = len[i];       if (y == pre) continue;       if (z)         z = -1;       else         z = 1;       f[y] = f[x] + z;       ans = min(ans, f[y]);       dfs2(y, x);     }   }   int main() {     scanf( %d , &n);     for (int i = 2; i <= n; i++) {       int a, b;       scanf( %d%d , &a, &b);       add(a, b, 0);       add(b, a, 1);     }     dfs1(1, 0);     ans = f[1];     dfs2(1, 0);     printf( %d n , ans);     for (int i = 1; i <= n; i++)       if (f[i] == ans) printf( %d  , i);     return 0;   }


#include <bits/stdc++.h>   const int N = 2e5 + 5;   const int inf = 2e9;   struct Edge {     int u, v, cost, isin;     int ind;     bool operator<(const Edge& x) const { return cost < x.cost; }   };   std::vector<Edge> e;   std::vector<std::pair<int, int> > l[N];   int tata[N], sz[N], ans[N];   int findDaddy(int node) {     if (node == tata[node]) return node;     return tata[node] = findDaddy(tata[node]);   }   bool areConnected(int u, int v) { return (findDaddy(u) == findDaddy(v)); }   void connect(int u, int v) {     if (sz[findDaddy(u)] > sz[findDaddy(v)]) {       sz[findDaddy(u)] += sz[findDaddy(v)];       tata[findDaddy(v)] = findDaddy(u);     } else {       sz[findDaddy(v)] += sz[findDaddy(u)];       tata[findDaddy(u)] = findDaddy(v);     }   }   int rmq[21][N], mx[21][N], h[N], mn[N];   void dfs(int node, int from = 0, int c = 0) {     rmq[0][node] = from, mx[0][node] = c, h[node] = h[from] + 1;     for (std::pair<int, int> to : l[node])       if (to.first != from) dfs(to.first, node, to.second);   }   std::pair<int, int> maxlca(int from, int to) {     int sol = 0;     if (h[from] < h[to]) std::swap(from, to);     for (int i = 20; i >= 0; i--)       if (h[from] - (1 << i) >= h[to]) {         sol = (((sol) > (mx[i][from])) ? (sol) : (mx[i][from]));         from = rmq[i][from];       }     if (from == to) return {from, sol};     for (int i = 20; i >= 0; i--)       if (rmq[i][from] != rmq[i][to]) {         sol = (((sol) >                 ((((mx[i][from]) > (mx[i][to])) ? (mx[i][from]) : (mx[i][to]))))                    ? (sol)                    : ((((mx[i][from]) > (mx[i][to])) ? (mx[i][from])                                                      : (mx[i][to]))));         from = rmq[i][from], to = rmq[i][to];       }     sol =         (((sol) > ((((mx[0][from]) > (mx[0][to])) ? (mx[0][from]) : (mx[0][to]))))              ? (sol)              : ((((mx[0][from]) > (mx[0][to])) ? (mx[0][from]) : (mx[0][to]))));     return {rmq[0][from], sol};   }   void update(int from, int to, int cost) {     to = findDaddy(to), from = findDaddy(from);     while (from != to) {       if (h[from] < h[to]) std::swap(from, to);       mn[from] = cost - 1;       tata[from] = rmq[0][from];       from = findDaddy(from);     }   }   int main() {     int n, m, from, to, cost;     std::cin >> n >> m;     for (int i = 1; i <= m; i++) {       std::cin >> from >> to >> cost;       e.push_back({from, to, cost, 0, i});     }     for (int i = 1; i <= n; i++) tata[i] = i, sz[i] = 1;     std::sort(e.begin(), e.end());     for (Edge& curr : e) {       if (!areConnected(curr.u, curr.v)) {         l[curr.u].push_back({curr.v, curr.cost});         l[curr.v].push_back({curr.u, curr.cost});         connect(curr.u, curr.v);         curr.isin = 1;       }     }     dfs(1);     for (int i = 1; i <= 20; i++)       for (int j = 1; j <= n; j++) {         rmq[i][j] = rmq[i - 1][rmq[i - 1][j]];         mx[i][j] = (((mx[i - 1][j]) > (mx[i - 1][rmq[i - 1][j]]))                         ? (mx[i - 1][j])                         : (mx[i - 1][rmq[i - 1][j]]));       }     for (Edge& curr : e)       if (!curr.isin) {         std::pair<int, int> x = maxlca(curr.u, curr.v);         ans[curr.ind] = x.second - 1;       }     for (int i = 1; i <= n; i++) tata[i] = i, mn[i] = inf;     for (Edge& curr : e)       if (!curr.isin) update(curr.u, curr.v, curr.cost);     for (Edge& curr : e) {       if (!curr.isin) continue;       if (h[curr.u] < h[curr.v]) std::swap(curr.u, curr.v);       ans[curr.ind] = -1;       if (rmq[0][curr.u] == curr.v and mn[curr.u] != inf)         ans[curr.ind] = mn[curr.u];     }     for (int i = 1; i <= m; i++) std::cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   string s;   char ok[100] = { A ,  H ,  I ,  M ,  O ,  T ,  U ,  V ,  W ,  X ,  Y };   int main() {     while (cin >> s) {       if (s.length() == 1) {         int flag = 0;         for (int i = 0; i <= 10; i++) {           if (s[0] == ok[i]) {             puts( YES );             flag = 1;             break;           }         }         if (!flag) puts( NO );         continue;       } else {         int flag = 1, half = s.length() / 2, flag1 = 0;         int len = s.length();         for (int i = 0; i < half; i++) {           if (s[i] != s[len - 1 - i]) {             flag = 0;             break;           } else {             int x = 0;             for (int j = 0; j <= 10; j++) {               x += s[i] == ok[j];             }             if (x == 0) {               flag = 0;               break;             }           }         }         if (len % 2 == 1) {           for (int i = 0; i <= 10; i++) {             if (s[half] == ok[i]) {               flag1 = 1;               break;             }           }         } else           flag1 = 1;         printf( %s n , flag == 1 && flag1 == 1 ?  YES  :  NO );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<long long int, long long int> m;   int main() {     long long int n;     cin >> n;     vector<long long int> v;     for (long long int i = 0; i < n; i++) {       long long int x;       cin >> x;       v.emplace_back(x);       m[x]++;     }     sort(v.begin(), v.end());     long long int ans = 0;     for (long long int i = 0; i < n; i++) {       ans = max(ans, m[v[i]]);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int a = 0, b = 0, c = 0, sum1 = 0, d = 0;     cin >> a >> b >> c >> d;     for (; sum1 <= d;) {       sum1 += b;       a -= c;     }     if (a > 0)       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   inline long long minOf(long long x, long long y) { return (x < y ? x : y); }   inline long long maxOf(long long x, long long y) { return (x > y ? x : y); }   char arr[50000000], *ptr;   long long ret;   string str;   inline long long get_int() {     bool isNeg = false;     ret = 0;     while (!((*ptr >=  0  && *ptr <=  9 ) || *ptr ==  - )) ptr++;     while ((*ptr >=  0  && *ptr <=  9 ) || *ptr ==  - ) {       if (*ptr ==  - )         isNeg = true;       else         ret = ret * 10 + (*ptr -  0 );       ptr++;     }     if (isNeg) ret = -ret;     return ret;   }   long long a[100005];   int bits[33];   int main() {     fread(arr, sizeof(char), 50000000, stdin);     ptr = arr;     int n = get_int();     for (int i = 0; i < 33; i++) bits[i] = -1;     for (int i = 0; i < n; i++) {       a[i] = get_int();       for (int j = 0; j < 32; j++) {         if (a[i] & (1 << j)) {           if (bits[j] == -1)             bits[j] = a[i];           else             bits[j] = a[i] & bits[j];         }       }     }     int k = 0;     vector<int> list;     int lk = 0;     for (int i = 32; i >= 0; i--) {       if (bits[i] != -1) {         for (int j = 0; j < 32; j++) {           if (bits[i] & (1 << j)) {             if (j > lk) lk = j;             break;           }         }       }     }     for (int j = 0; j < n; j++) {       if (a[j] & (1 << lk)) {         k++;         list.push_back(a[j]);       }     }     cout << k << endl;     for (int i = 0; i < k; i++) {       cout << list[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using vi = vector<int>;   using vvi = vector<vi>;   using vll = vector<ll>;   using vvll = vector<vll>;   using vb = vector<bool>;   using vd = vector<double>;   using vs = vector<string>;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   using pdd = pair<double, double>;   using vpii = vector<pii>;   using vvpii = vector<vpii>;   using vpll = vector<pll>;   using vvpll = vector<vpll>;   using vpdd = vector<pdd>;   using vvpdd = vector<vpdd>;   template <typename T>   void ckmin(T& a, const T& b) {     a = min(a, b);   }   template <typename T>   void ckmax(T& a, const T& b) {     a = max(a, b);   }   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());   namespace __input {   template <class T1, class T2>   void re(pair<T1, T2>& p);   template <class T>   void re(vector<T>& a);   template <class T, size_t SZ>   void re(array<T, SZ>& a);   template <class T>   void re(T& x) {     cin >> x;   }   void re(double& x) {     string t;     re(t);     x = stod(t);   }   template <class Arg, class... Args>   void re(Arg& first, Args&... rest) {     re(first);     re(rest...);   }   template <class T1, class T2>   void re(pair<T1, T2>& p) {     re(p.first, p.second);   }   template <class T>   void re(vector<T>& a) {     for (int i = 0; i < (int((a).size())); i++) re(a[i]);   }   template <class T, size_t SZ>   void re(array<T, SZ>& a) {     for (int i = 0; i < (SZ); i++) re(a[i]);   }   }  // namespace __input   using namespace __input;   namespace __output {   template <class T1, class T2>   void pr(const pair<T1, T2>& x);   template <class T, size_t SZ>   void pr(const array<T, SZ>& x);   template <class T>   void pr(const vector<T>& x);   template <class T>   void pr(const set<T>& x);   template <class T1, class T2>   void pr(const map<T1, T2>& x);   template <class T>   void pr(const T& x) {     cout << x;   }   template <class Arg, class... Args>   void pr(const Arg& first, const Args&... rest) {     pr(first);     pr(rest...);   }   template <class T1, class T2>   void pr(const pair<T1, T2>& x) {     pr( { , x.first,  ,  , x.second,  } );   }   template <class T, bool pretty = true>   void prContain(const T& x) {     if (pretty) pr( { );     bool fst = 1;     for (const auto& a : x) pr(!fst ? pretty ?  ,   :     :   , a), fst = 0;     if (pretty) pr( } );   }   template <class T>   void pc(const T& x) {     prContain<T, false>(x);     pr(  n );   }   template <class T, size_t SZ>   void pr(const array<T, SZ>& x) {     prContain(x);   }   template <class T>   void pr(const vector<T>& x) {     prContain(x);   }   template <class T>   void pr(const set<T>& x) {     prContain(x);   }   template <class T1, class T2>   void pr(const map<T1, T2>& x) {     prContain(x);   }   void ps() { pr(  n ); }   template <class Arg>   void ps(const Arg& first) {     pr(first);     ps();   }   template <class Arg, class... Args>   void ps(const Arg& first, const Args&... rest) {     pr(first,    );     ps(rest...);   }   }  // namespace __output   using namespace __output;   namespace __algorithm {   template <typename T>   void dedup(vector<T>& v) {     sort((v).begin(), (v).end());     v.erase(unique((v).begin(), (v).end()), v.end());   }   template <typename T>   typename vector<T>::iterator find(vector<T>& v, const T& x) {     auto it = lower_bound((v).begin(), (v).end(), x);     return it != v.end() && *it == x ? it : v.end();   }   template <typename T>   size_t index(vector<T>& v, const T& x) {     auto it = find(v, x);     assert(it != v.end() && *it == x);     return it - v.begin();   }   template <typename C, typename T>   vector<T> prefixes(const C& v, T zero) {     vector<T> res(int((v).size()) + 1, zero);     for (int i = 0; i < (int((v).size())); i++) res[i + 1] = res[i] + v[i];     return res;   }   template <typename C, typename T>   vector<T> suffixes(const C& v, T zero) {     vector<T> res(int((v).size()) + 1, zero);     for (int i = (int((v).size())) - 1; i >= 0; i--) res[i] = v[i] + res[i + 1];     return res;   }   }  // namespace __algorithm   using namespace __algorithm;   struct monostate {     friend istream& operator>>(istream& is,                                const __attribute__((unused)) monostate& ms) {       return is;     }     friend ostream& operator<<(ostream& os,                                const __attribute__((unused)) monostate& ms) {       return os;     }   } ms;   template <typename W = monostate>   struct wedge {     int u, v, i;     W w;     wedge<W>(int _u = -1, int _v = -1, int _i = -1) : u(_u), v(_v), i(_i) {}     int operator[](int loc) const { return u ^ v ^ loc; }     friend void re(wedge& e) {       re(e.u, e.v, e.w);       --e.u, --e.v;     }     friend void pr(const wedge& e) { pr(e.u,  <- , e.w,  -> , e.v); }   };   namespace __io {   void setIn(string second) { freopen(second.c_str(),  r , stdin); }   void setOut(string second) { freopen(second.c_str(),  w , stdout); }   void setIO(string second =   ) {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.precision(15);     if (int((second).size())) {       setIn(second +  .in ), setOut(second +  .out );     }   }   }  // namespace __io   using namespace __io;   template <size_t N>   struct sieve {     vi primes, least_prime, largest_proper, phi;     sieve() : least_prime(N + 1), largest_proper(N + 1), phi(N + 1) {       phi[1] = 1;       for (int i = 2; i <= N; i++) {         if (!least_prime[i]) {           least_prime[i] = i;           largest_proper[i] = 1;           primes.push_back(i);         }         phi[i] = phi[largest_proper[i]];         phi[i] *= least_prime[i] -                   int(least_prime[i] != least_prime[largest_proper[i]]);         for (int x : primes) {           if (x > least_prime[i] || i * x > N) break;           least_prime[i * x] = x;           largest_proper[i * x] = i;         }       }     }     bool is_prime(int v) {       assert(0 < v && v < N);       return least_prime[v] == v;     }     vi factor(int v) {       assert(0 < v && v <= N);       vi res;       while (v > 1) {         res.push_back(least_prime[v]);         v = largest_proper[v];       }       reverse((res).begin(), (res).end());       return res;     }     vi divisors(int v) {       if (v == 1) return {1};       int p = least_prime[v], m = 0;       while (v % p == 0) v /= p, m++;       vi res = divisors(v), fin;       for (int d : res)         for (int pc = 0, t = d; pc <= m; pc++, t *= p) fin.push_back(t);       return fin;     }   };   const int MAXV = 1e6 + 1;   sieve<MAXV> sv;   int main() {     setIO();     int N, K;     re(N, K);     if (K == 1) {       ps(3);       return 0;     }     nth_element(sv.phi.begin() + 3, sv.phi.begin() + 3 + K,                 sv.phi.begin() + N + 1);     ps(accumulate(sv.phi.begin() + 3, sv.phi.begin() + K + 3, 0ll) + 2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<unsigned long long>> g;   vector<bool> visited;   vector<unsigned long long> s;   void dfs(unsigned long long x) {     visited[x] = 1;     for (unsigned long long i : g[x])       if (!visited[i]) dfs(i);     s.push_back(x);   }   int main() {     unsigned long long n;     cin >> n;     vector<unsigned long long> v(n);     for (unsigned long long &i : v) cin >> i;     g.assign(n, vector<unsigned long long>());     visited.assign(n, 0);     for (unsigned long long i = 0; i < n; i++) {       for (unsigned long long j = 0; j < n; j++) {         if (v[i] * 2 == v[j]) {           g[i].push_back(j);         } else if (v[i] * 3 == v[j]) {           g[j].push_back(i);         }       }     }     for (unsigned long long i = 0; i < n; i++) {       if (!visited[i]) dfs(i);     }     while (!s.empty()) {       cout << v[s.back()] <<    ;       s.pop_back();     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000010;   int n, ans[N], cnt = 1;   vector<int> id[N], cur[N];   vector<int> g[N];   bool vis[N], done[N];   int sz[N], ns[N], ns2[N], comp_cnt, comp[N], allns = 0;   void DFS(int node) {     sz[comp_cnt]++;     vis[node] = true;     comp[node] = comp_cnt;     for (int i = 0; i < (int)g[node].size(); i++) {       if (!vis[g[node][i]]) {         DFS(g[node][i]);       }     }   }   void take(int idx, int needed) {     if (done[idx]) return;     done[idx] = true;     int u = cur[idx][0];     int v = cur[idx][1];     if (v % n == needed) swap(cur[idx][0], cur[idx][1]);     u = cur[idx][0];     v = cur[idx][1];     ns[comp[needed]] += cur[idx][0] / n;     allns += cur[idx][0] / n;     ns2[comp[needed]] += cur[idx][1] / n;     int a = id[v % n][0];     if (a == idx) a = id[v % n][1];     take(a, v % n);   }   int main() {     scanf( %d , &n);     if (n % 2 == 0) {       puts( First );       fflush(stdout);       if ((n / 2) % 2 == 0) {         ans[1] = ans[2] = cnt++;         ans[n + 1] = ans[n + 2] = cnt++;         for (int i = 3; i <= n; i++) {           ans[i] = ans[i + n] = cnt++;         }       } else {         int last[2] = {-1, -1};         for (int cur, i = 1; i <= 2 * n; i++) {           cur = (i & 1);           if (last[cur] == -1)             last[cur] = i;           else {             ans[i] = ans[last[cur]] = cnt++;             last[cur] = -1;           }         }       }       for (int i = 1; i <= 2 * n; i++) {         if (i > 1) putchar(   );         printf( %d , ans[i]);       }       puts(  );       fflush(stdout);       return 0;     }     puts( Second );     fflush(stdout);     for (int i = 1; i <= 2 * n; i++) {       scanf( %d , &ans[i]);       cur[ans[i]].push_back(i);       id[i % n].push_back(ans[i]);     }     for (int u, v, i = 1; i <= n; i++) {       u = cur[i][0] % n;       v = cur[i][1] % n;       g[u].push_back(v);       g[v].push_back(u);     }     for (int i = 0; i < n; i++) {       if (vis[i]) continue;       DFS(i);       comp_cnt++;     }     for (int i = 1; i <= n; i++) {       if (done[i]) continue;       take(i, cur[i][0] % n);     }     long long val = (long long)n * (n - 1) / 2;     val += (long long)allns * n;     val %= (2 * n);     if (val != 0) {       assert(val == n);       int good = -1;       for (int i = 0; i < comp_cnt; i++) {         if (abs(ns[i] - ns2[i]) % 2 == 1) {           good = i;           break;         }       }       assert(good != -1);       for (int i = 1; i <= n; i++) {         if (comp[cur[i][0] % n] == good) {           swap(cur[i][0], cur[i][1]);         }       }     }     for (int i = 1; i <= n; i++) {       if (i > 1) putchar(   );       printf( %d , cur[i][0]);     }     puts(  );     fflush(stdout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000 * 1000 * 1000 + 47;   const long long LINF = (long long)INF * INF;   const int MAX = 2e5;   const double EPS = 1e-9;   const double PI = acos(-1.);   int n;   int a[10][10];   int dx[8] = {2, 2, 1, -1, -2, -2, -1, 1};   int dy[8] = {1, -1, -2, -2, -1, 1, 2, 2};   pair<int, int> dpT[100][10][10][3][212];   bool ok(int x, int y) {     if (min(x, y) < 0) return false;     if (max(x, y) >= n) return false;     return true;   }   pair<int, int> dp(int cur, int x, int y, int fig, int steps) {     if (steps == 212) {       return make_pair(INF, INF);     }     if (cur == n * n) {       return make_pair(0, 0);     }     if (dpT[cur][x][y][fig][steps] != make_pair(-1, -1)) {       return dpT[cur][x][y][fig][steps];     }     if (a[x][y] == cur) {       return dpT[cur][x][y][fig][steps] = dp(cur + 1, x, y, fig, steps);     }     pair<int, int> best = make_pair(INF, INF);     if (fig != 0) {       pair<int, int> a = dp(cur, x, y, 0, steps + 1);       best = min(best, make_pair(a.first + 1, a.second + 1));     }     if (fig != 1) {       pair<int, int> a = dp(cur, x, y, 1, steps + 1);       best = min(best, make_pair(a.first + 1, a.second + 1));     }     if (fig != 2) {       pair<int, int> a = dp(cur, x, y, 2, steps + 1);       best = min(best, make_pair(a.first + 1, a.second + 1));     }     if (fig == 0) {       for (int i = (0); i < (8); ++i) {         int nx = x + dx[i];         int ny = y + dy[i];         if (ok(nx, ny)) {           pair<int, int> a = dp(cur, nx, ny, fig, steps + 1);           best = min(best, make_pair(a.first + 1, a.second));         }       }     }     if (fig == 1) {       for (int i = (-n); i < (n + 1); ++i) {         if (!i) continue;         int nx = x + i;         int ny = y;         if (ok(nx, ny)) {           pair<int, int> a = dp(cur, nx, ny, fig, steps + 1);           best = min(best, make_pair(a.first + 1, a.second));         }         nx = x;         ny = y + i;         if (ok(nx, ny)) {           pair<int, int> a = dp(cur, nx, ny, fig, steps + 1);           best = min(best, make_pair(a.first + 1, a.second));         }       }     }     if (fig == 2) {       for (int i = (-n); i < (n + 1); ++i) {         if (!i) continue;         int nx = x + i;         int ny = y + i;         if (ok(nx, ny)) {           pair<int, int> a = dp(cur, nx, ny, fig, steps + 1);           best = min(best, make_pair(a.first + 1, a.second));         }         nx = x - i;         ny = y + i;         if (ok(nx, ny)) {           pair<int, int> a = dp(cur, nx, ny, fig, steps + 1);           best = min(best, make_pair(a.first + 1, a.second));         }       }     }     return dpT[cur][x][y][fig][steps] = best;   }   int main() {     ios_base::sync_with_stdio(0);     memset(dpT, -1, sizeof(dpT));     cin >> n;     int x, y;     for (int i = (0); i < (n); ++i) {       for (int j = (0); j < (n); ++j) {         cin >> a[i][j];         a[i][j]--;         if (a[i][j] == 0) {           x = i;           y = j;         }       }     }     pair<int, int> best = make_pair(INF, INF);     best = min(best, dp(1, x, y, 0, 0));     best = min(best, dp(1, x, y, 1, 0));     best = min(best, dp(1, x, y, 2, 0));     cout << best.first <<     << best.second << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m, i, j, k;     cin >> n >> m;     int a[101] = {0};     for (i = 0; i < m; i++) {       cin >> j;       a[j]++;     }     vector<int> v;     for (i = 1; i <= 100; i++) {       if (a[i] != 0) v.push_back(a[i]);     }     sort(v.rbegin(), v.rend());     j = m / n;     if (j == 0)       cout << j;     else {       for (i = j; i >= 1; i--) {         int x = 0;         for (k = 0; k < v.size(); k++) {           x += (v[k] / i);         }         if (x >= n) break;       }       cout << i;     }   }


#include <bits/stdc++.h>   template <class T>   inline void read(T &x) {     x = 0;     int f = 0;     char ch = getchar();     while (!isdigit(ch)) {       f |= ch ==  - ;       ch = getchar();     }     while (isdigit(ch)) {       x = (x << 1) + (x << 3) + (ch ^ 48);       ch = getchar();     }     x = f ? -x : x;     return;   }   std::vector<long long> r, g, b;   long long ans, nr, ng, nb, t, p;   void solve(std::vector<long long> a, std::vector<long long> b,              std::vector<long long> c) {     for (auto x : a) {       auto y = std::lower_bound(b.begin(), b.end(), x),            z = std::upper_bound(c.begin(), c.end(), x);       if (y == b.end() || z == c.begin()) continue;       --z;       ans = std::min(           ans, (x - *y) * (x - *y) + (*y - *z) * (*y - *z) + (x - *z) * (x - *z));     }   }   int main() {     read(t);     while (t--) {       ans = 9e18;       r.clear(), g.clear(), b.clear();       read(nr), read(ng), read(nb);       for (int i = 1; i <= nr; ++i) read(p), r.push_back(p);       for (int i = 1; i <= ng; ++i) read(p), g.push_back(p);       for (int i = 1; i <= nb; ++i) read(p), b.push_back(p);       std::sort(r.begin(), r.end()), std::sort(g.begin(), g.end()),           std::sort(b.begin(), b.end());       solve(r, g, b), solve(r, b, g), solve(g, r, b), solve(g, b, r),           solve(b, r, g), solve(b, g, r);       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxb = 20;   const int maxn = 300000 + 7;   int dp[maxn][maxb];   int n, q;   int seq[maxn];   int last[maxb];   void init() {     cin >> n >> q;     for (int i = (1); i <= (n); ++i) cin >> seq[i];   }   void solve() {     for (int i = (0); i < (maxb); ++i) dp[n + 1][i] = n + 1;     for (int i = (0); i < (maxb); ++i) last[i] = n + 1;     for (int i = (n); i >= (1); --i) {       for (int j = (0); j < (maxb); ++j) dp[i][j] = n + 1;       int now = seq[i];       for (int j = (0); j < (maxb); ++j) {         if ((now >> j) & 1) {           for (int k = (0); k < (maxb); ++k)             dp[i][k] = min(dp[i][k], dp[last[j]][k]);           dp[i][j] = i;           last[j] = i;         }       }     }     while (q--) {       int x, y;       cin >> x >> y;       int r = seq[y];       int ok = 0;       for (int j = (0); j < (maxb); ++j) {         if (((r >> j) & 1) && dp[x][j] <= y) {           ok = 1;           break;         }       }       if (ok)         cout <<  Shi n ;       else         cout <<  Fou n ;     }   }   int main(void) {     ios::sync_with_stdio(false);     cin.tie(0);     init();     solve();     return 0;   }


#include <bits/stdc++.h>   const int maxn = 2e3 + 5;   using namespace std;   int arr[maxn];   int dp[maxn][maxn];   int check(int x, int lef, int rig) { return (lef <= x && x <= rig ? 1 : 0); }   int main() {     int n, h, l, r;     scanf( %d %d %d %d , &n, &h, &l, &r);     for (int i = 1; i <= n; i++) {       scanf( %d , &arr[i]);     }     memset((dp), (-1), sizeof(dp));     dp[0][0] = 0;     for (int i = 1; i <= n; i++) {       for (int j = 0; j < h; j++) {         if (dp[i - 1][j] != -1) {           dp[i][(j + arr[i]) % h] =               max(dp[i - 1][j] + check((j + arr[i]) % h, l, r),                   dp[i][(j + arr[i]) % h]);           dp[i][(j + arr[i] - 1) % h] =               max(dp[i - 1][j] + check((j + arr[i] - 1) % h, l, r),                   dp[i][(j + arr[i] - 1) % h]);         }       }     }     int ans = -1;     for (int i = 0; i < h; i++) {       ans = max(ans, dp[n][i]);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace ::std;   const long double PI = acos(-1);   const long long MOD = 1000000000 + 7;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long add(long long a, long long b, long long m = MOD) {     return (a % m + b % m + 2 * m) % m;   }   long long mul(long long a, long long b, long long m = MOD) {     return ((a % m + m) * (b % m + m)) % m;   }   long long pow_mod(long long a, long long b, long long m = MOD) {     long long res = 1LL;     a = a % m;     while (b) {       if (b & 1) res = mul(res, a, m);       b >>= 1;       a = mul(a, a, m);     }     return res;   }   long long fastexp(long long a, long long b) {     long long res = 1LL;     while (b) {       if (b & 1) res = res * a;       b >>= 1;       a *= a;     }     return res;   }   int gcdExtendido(int a, int b, int *x, int *y) {     if (a == 0) {       *x = 0;       *y = 1;       return b;     }     int x1, y1;     int gcd = gcdExtendido(b % a, a, &x1, &y1);     *x = y1 - (b / a) * x1;     *y = x1;     return gcd;   }   int modInverso(int a, int m) {     int x, y;     int g = gcdExtendido(a, m, &x, &y);     if (g != 1)       return -1;     else       return (x % m + m) % m;   }   const int M = 10000 + 5;   const int N = 500 + 5;   int n, m, k;   pair<int, int> edges[M];   int carry[N];   int s[N];   int sizes[3][M][N];   int parent[3][M][N];   int get(int x, int id, int type) {     return parent[type][id][x] == x                ? x                : parent[type][id][x] = get(parent[type][id][x], id, type);   }   void join(int a, int b, int id, int type) {     a = get(a, id, type);     b = get(b, id, type);     if (a == b) return;     if (sizes[type][id][a] > sizes[type][id][b]) swap(a, b);     parent[type][id][a] = b;     sizes[type][id][b] += sizes[type][id][a];   }   void initialize(int id, int type) {     for (int i = 1; i <= n; i++) {       parent[type][id][i] = i;       sizes[type][id][i] = 1;     }   }   int get2(int x) { return carry[x] == x ? x : carry[x] = get2(carry[x]); }   void join2(int a, int b) {     a = get2(a);     b = get2(b);     if (a == b) return;     if (s[a] > s[b]) swap(a, b);     carry[a] = b;     s[b] += s[a];   }   int main() {     scanf( %d %d , &(n), &(m));     initialize(0, 0);     for (int i = 1; i <= m; i++) {       for (int j = 1; j <= n; j++) {         parent[0][i][j] = parent[0][i - 1][j];         sizes[0][i][j] = sizes[0][i - 1][j];       }       scanf( %d %d , &(edges[i].first), &(edges[i].second));       join(edges[i].first, edges[i].second, i, 0);     }     initialize(m + 1, 1);     for (int i = m; i >= 1; i--) {       for (int j = 1; j <= n; j++) {         parent[1][i][j] = parent[1][i + 1][j];         sizes[1][i][j] = sizes[1][i + 1][j];       }       join(edges[i].first, edges[i].second, i, 1);     }     scanf( %d , &(k));     int l, r;     while (k--) {       scanf( %d %d , &(l), &(r));       l--;       r++;       for (int i = 1; i <= n; i++) {         carry[i] = parent[0][l][i];         s[i] = sizes[0][l][i];       }       queue<pair<int, int> > Q;       for (int i = 1; i <= n; i++) {         int x = get(i, r, 1);         join2(i, x);       }       int ans = 0;       for (int i = 1; i <= n; i++) {         if (get2(i) == i) ans++;       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s, t;     cin >> s >> t;     int n = s.size();     int m = t.size();     if (n == m && s == t)       cout << -1;     else       cout << max(n, m);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n;     cin >> n;     if (n == 0)       cout << 1;     else {       switch (n % 4) {         case 0:           cout << 6 << endl;           break;         case 1:           cout << 8 << endl;           break;         case 2:           cout << 4 << endl;           break;         case 3:           cout << 2 << endl;           break;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long max_n = INT_MAX - 1000000;   long long dp[30][30001];   pair<int, int> par[30][30001];   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int t;     cin >> t;     while (t--) {       long long n, m, k;       cin >> n >> m >> k;       if (n > m) swap(n, m);       map<char, long long> mp;       string ss =   ;       for (long long i = 0; i < k; i++) {         char a;         cin >> a;         mp[a]++;         ss += a;       }       vector<int> vv;       for (auto i : mp) {         vv.push_back(i.second);       }       long long ans = LONG_MAX;       for (int i1 = 0; i1 < vv.size(); i1++) {         vector<int> v = vv;         v.erase(v.begin() + i1);         v.insert(v.begin(), vv[i1]);         for (long long i = 0; i < v.size(); i++) {           for (long long j = 0; j <= m; j++) {             dp[i][j] = max_n;           }         }         for (int i = 0; i < v.size(); i++) {           for (long long j = 1; j <= m; j++) {             if (i == 0) {               if (j <= v[i]) {                 dp[i][j] = v[i];               }             } else {               if (dp[0][j] == max_n) {                 if (j <= v[i]) {                   dp[i][j] = v[i] + v[0];                   dp[i][j] = min(dp[i - 1][j], dp[i][j]);                 } else {                   dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v[i]] + v[i]);                 }               } else {                 dp[i][j] = dp[0][j];               }             }           }         }         if (v[0] - (dp[v.size() - 1][n] - n) >= 0)           ans = min(ans, (v[0] - (dp[v.size() - 1][n] - n)) *                              max((long long)0,                                  (dp[v.size() - 1][n] - n - (k - n - m))));         if (v[0] - (dp[v.size() - 1][m] - m) >= 0)           ans = min(ans, (v[0] - (dp[v.size() - 1][m] - m)) *                              max((long long)0,                                  (dp[v.size() - 1][m] - m - (k - n - m))));       }       cout << ans <<   n ;     }     return 0;   }