#include <bits/stdc++.h>   using namespace std;   const long double EPS = 1e-9;   long double _A[19][100009], _b[19], _c[100009];   struct LPSolver {     int m, n;     vector<int> B, N;     vector<vector<long double> > D;     LPSolver(const vector<vector<long double> > &A, const vector<long double> &b,              const vector<long double> &c)         : m(b.size()),           n(c.size()),           N(n + 1),           B(m),           D(m + 2, vector<long double>(n + 2)) {       for (int i = 0; i < m; i++)         for (int j = 0; j < n; j++) D[i][j] = A[i][j];       for (int i = 0; i < m; i++) {         B[i] = n + i;         D[i][n] = -1;         D[i][n + 1] = b[i];       }       for (int j = 0; j < n; j++) {         N[j] = j;         D[m][j] = -c[j];       }       N[n] = -1;       D[m + 1][n] = 1;     }     void Pivot(int r, int s) {       double inv = 1.0 / D[r][s];       for (int i = 0; i < m + 2; i++)         if (i != r)           for (int j = 0; j < n + 2; j++)             if (j != s) D[i][j] -= D[r][j] * D[i][s] * inv;       for (int j = 0; j < n + 2; j++)         if (j != s) D[r][j] *= inv;       for (int i = 0; i < m + 2; i++)         if (i != r) D[i][s] *= -inv;       D[r][s] = inv;       swap(B[r], N[s]);     }     bool Simplex(int phase) {       int x = phase == 1 ? m + 1 : m;       while (true) {         int s = -1;         for (int j = 0; j <= n; j++) {           if (phase == 2 && N[j] == -1) continue;           if (s == -1 || D[x][j] < D[x][s] || D[x][j] == D[x][s] && N[j] < N[s])             s = j;         }         if (D[x][s] > -EPS) return true;         int r = -1;         for (int i = 0; i < m; i++) {           if (D[i][s] < EPS) continue;           if (r == -1 || D[i][n + 1] / D[i][s] < D[r][n + 1] / D[r][s] ||               (D[i][n + 1] / D[i][s]) == (D[r][n + 1] / D[r][s]) && B[i] < B[r])             r = i;         }         if (r == -1) return false;         Pivot(r, s);       }     }     long double Solve(vector<long double> &x) {       int r = 0;       for (int i = 1; i < m; i++)         if (D[i][n + 1] < D[r][n + 1]) r = i;       if (D[r][n + 1] < -EPS) {         Pivot(r, n);         if (!Simplex(1) || D[m + 1][n + 1] < -EPS)           return -numeric_limits<long double>::infinity();         for (int i = 0; i < m; i++)           if (B[i] == -1) {             int s = -1;             for (int j = 0; j <= n; j++)               if (s == -1 || D[i][j] < D[i][s] ||                   D[i][j] == D[i][s] && N[j] < N[s])                 s = j;             Pivot(i, s);           }       }       if (!Simplex(2)) return numeric_limits<long double>::infinity();       x = vector<long double>(n);       for (int i = 0; i < m; i++)         if (B[i] < n) x[B[i]] = D[i][n + 1];       return D[m][n + 1];     }   };   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m, i;     long double p, q;     cin >> n >> p >> q;     m = 2;     for (i = 0; i < n; i++) {       cin >> _A[0][i] >> _A[1][i];       _A[0][i] = -_A[0][i];       _A[1][i] = -_A[1][i];       _c[i] = -1;     }     _b[0] = -p;     _b[1] = -q;     vector<vector<long double> > A(m);     vector<long double> b(_b, _b + m);     vector<long double> c(_c, _c + n);     for (i = 0; i < m; i++) A[i] = vector<long double>(_A[i], _A[i] + n);     LPSolver solver(A, b, c);     vector<long double> x;     long double value = solver.Solve(x);     cout << fixed << setprecision(10) << (-value) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long a[1111], b[1111];   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int i = 1; i <= n; ++i) cin >> b[i];     long long ans = 0;     for (int i = 1; i <= n; ++i) {       long long cur1 = 0, cur2 = 0;       for (int j = i; j <= n; ++j) {         cur1 = cur1 | a[j];         cur2 = cur2 | b[j];         ans = max(ans, cur1 + cur2);       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 3e5 + 10;   const long long M = 3e5 + 1;   const long long oo = 1e9;   long long n, minn, ans;   long long bit[N], deg[N];   pair<long long, long long> nex[N];   vector<long long> vi[N];   void upd(long long pos, long long val) {     for (long long i = pos; i < N; i += (i & -i)) bit[i] += val;   }   long long get(long long pos) {     long long res = 0;     for (long long i = pos; i >= 1; i -= (i & -i)) res += bit[i];     return res;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> minn;     for (long long i = 1; i <= n; ++i) {       long long x, y;       cin >> x >> y;       nex[i] = pair<long long, long long>(x, y);       vi[x].push_back(i);       vi[y].push_back(i);       deg[x]++;       deg[y]++;     }     for (long long i = 1; i <= n; ++i) upd(M - vi[i].size(), 1);     for (long long i = 1; i <= n; ++i) {       long long cur = vi[i].size();       for (long long v : vi[i]) {         long long die = (nex[v].first == i) ? nex[v].second : nex[v].first;         upd(M - deg[die], -1);         deg[die]--;         upd(M - deg[die], 1);       }       upd(M - deg[i], -1);       long long rem = max(0ll, minn - cur);       ans += get(M - rem);       upd(M - deg[i], 1);       for (long long v : vi[i]) {         long long die = (nex[v].first == i) ? nex[v].second : nex[v].first;         upd(M - deg[die], -1);         deg[die]++;         upd(M - deg[die], 1);       }     }     cout << ans / 2;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, c[3000001], s;   set<pair<long long, long long> > d, t;   set<pair<long long, long long> >::iterator p;   int main() {     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> c[i];     for (int i = 1; i <= k; i++) d.insert({c[i], i});     for (int i = k + 1; i <= k + n; i++) {       if (i <= n) d.insert({c[i], i});       p = d.end();       p--;       long long r = (*p).first;       long long l = (*p).second;       s += ((i - l) * r);       d.erase(p);       t.insert({l, i});     }     cout << s << endl;     for (auto e : t) cout << e.second <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long sqr(long long x) { return x * x; }   int n, m, x, y;   int main() {     cin >> x >> y >> n;     long double ans = 1e9, p = 0;     for (int i = 0; i < n; ++i) {       int x1, y1, v;       cin >> x1 >> y1 >> v;       p = sqrt(sqr(x1 - x) + sqr(y1 - y)) / v;       ans = min(ans, p);     }     cout << fixed << setprecision(10) << ans;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 20;   long long n, m, x, y, ans, low, dp[maxn][1 << maxn];   bool f[maxn][maxn];   long long lowbit(long long x) {     long long ret = 0;     while (x) {       if (x & 1)         return ret;       else         ret++;       x /= 2;     }   }   int main() {     scanf( %lld%lld , &n, &m);     for (long long i = 1; i <= m; i++)       scanf( %d%d , &x, &y), x--, y--, f[x][y] = f[y][x] = true;     for (long long i = 0; i < n; i++) dp[i][1 << i] = 1;     for (long long i = 1; i < (1 << n); i++) {       long long minn = lowbit(i);       for (long long j = 0; j < n; j++)         if ((i & (1 << j)) && dp[j][i])           for (long long k = minn; k < n; k++)             if (f[j][k]) {               if ((!(i & (1 << k))))                 dp[k][i | (1 << k)] += dp[j][i];               else if (k == minn)                 ans += dp[j][i];             }     }     printf( %lld n , (ans - m) / 2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int q[2005], a[2005], cum[2005];   int cnt = 1, n;   set<int> use;   set<pair<int, int> > s;   set<pair<int, int> >::iterator it;   map<int, vector<int> > mp;   long long diff(int dif) { return (dif * 1LL * (dif + 1)) / 2; }   int main() {     scanf( %d , &n);     int cur = 0;     for (int i = 0; i < n; i++) {       int ti;       scanf( %d , &ti);       int ok = 1, ni = ti;       while (ni > 0) {         if (!(ni % 10 == 4 || ni % 10 == 7)) {           ok = 0;           break;         }         ni /= 10;       }       if (ok == 1) {         if (cur > 0) {           q[cnt] = cur;           a[cnt] = 0;           cnt++;           cur = 0;         }         mp[ti].push_back(cnt);         q[cnt] = 1;         a[cnt] = ti;         cnt++;       } else         cur++;     }     if (cur > 0) {       q[cnt] = cur;       a[cnt] = 0;       cnt++;     }     cum[0] = 0;     for (int i = 1; i < cnt; i++) {       cum[i] = cum[i - 1] + q[i];     }     long long ret = 0;     for (int i = 1; i < cnt; i++) {       if (q[i] >= 2) {         for (int j = 1; j < q[i]; j++) {           ret += j * 1LL * (j + 1) / 2 * (q[i] - j);         }         for (int j = 1; j < q[i]; j++) {           ret += j * 1LL * (cum[cnt - 1] - cum[i]) * 1LL * (q[i] - j);           ret += (q[i] - j) * 1LL * (cum[i - 1]) * 1LL * j;         }       }       s.clear();       use.clear();       long long sum = diff(cum[i - 1]);       if (i > 1) s.insert(make_pair(1, i - 1));       for (int j = i; j < cnt; j++) {         if (a[j] == 0) {           if (i != j) {             ret += sum * 1LL * q[j] * 1LL * q[i];           } else {             ret += sum * 1LL * q[i] * 1LL * (q[i] + 1) / 2;           }         } else {           if (use.find(a[j]) == use.end()) {             int len = mp[a[j]].size();             for (int k = 0; k < len; k++) {               int pos = mp[a[j]][k];               if (pos >= i) break;               it = s.lower_bound(make_pair(pos + 1, 0));               if (it != s.begin()) {                 it--;                 if ((*it).second >= pos) {                   int pa = (*it).second, pb = (*it).first;                   sum -= diff(cum[pa] - cum[pb - 1]);                   s.erase(*it);                   if (pa > pos) {                     s.insert(make_pair(pos + 1, pa));                     sum += diff(cum[pa] - cum[pos]);                   }                   if (pb < pos) {                     s.insert(make_pair(pb, pos - 1));                     sum += diff(cum[pos - 1] - cum[pb - 1]);                   }                 }               }             }             use.insert(a[j]);           }           ret += sum * 1LL * q[i];         }         if (i != j) {           if (!s.empty()) {             it = s.end();             it--;             if ((*it).second == i - 1) {               int pos = cum[i - 1] - cum[(*it).first - 1];               ret += q[j] * 1LL * pos * 1LL * q[i] * 1LL * (q[i] - 1) / 2;             }           }         }       }     }     printf( %I64d n , ret);   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 101000 * 8;   const int G = 3;   const int Mod = 998244353;   int n;   long long m;   int Fc[maxN], Ifc[maxN];   int In[maxN], A[maxN], B[maxN], C[maxN], R[maxN];   int QPow(int x, int cnt);   void NTT(int *P, int N, int opt);   int main() {     Fc[0] = Ifc[0] = 1;     for (int i = 1; i < maxN; i++) Fc[i] = 1ll * Fc[i - 1] * i % Mod;     Ifc[maxN - 1] = QPow(Fc[maxN - 1], Mod - 2);     for (int i = maxN - 2; i >= 1; i--) Ifc[i] = 1ll * Ifc[i + 1] * (i + 1) % Mod;     scanf( %d%lld , &n, &m);     int N = 1;     while (N <= n + n) N <<= 1;     m = m % (Mod - 1);     for (int i = 0; i <= n; i++)       scanf( %d , &In[i]), B[i] = 1ll * Fc[i] * In[i] % Mod;     for (int i = 0; i <= n; i++) C[i] = Ifc[n - i];     NTT(B, N, 1);     NTT(C, N, 1);     for (int i = 0; i < N; i++) B[i] = 1ll * B[i] * C[i] % Mod;     NTT(B, N, -1);     for (int i = 0; i <= n; i++) A[i] = 1ll * B[i + n] * Ifc[i] % Mod;     for (int i = 0; i <= n; i++)       A[i] = 1ll * A[i] * QPow(QPow(i + 1, Mod - 2), m) % Mod * Fc[i] % Mod;     memset(C, 0, sizeof(C));     for (int i = 0; i <= n; i++) C[i] = (i & 1) ? Mod - Ifc[i] : Ifc[i];     reverse(&C[0], &C[n + 1]);     NTT(A, N, 1);     NTT(C, N, 1);     for (int i = 0; i < N; i++) A[i] = 1ll * A[i] * C[i] % Mod;     NTT(A, N, -1);     for (int i = 0; i <= n; i++) printf( %I64d  , 1ll * A[i + n] * Ifc[i] % Mod);     printf(  n );     return 0;   }   int QPow(int x, int cnt) {     int ret = 1;     while (cnt) {       if (cnt & 1) ret = 1ll * ret * x % Mod;       cnt >>= 1;       x = 1ll * x * x % Mod;     }     return ret;   }   void NTT(int *P, int N, int opt) {     int l = -1, _n = N;     while (_n) ++l, _n >>= 1;     for (int i = 0; i < N; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (l - 1));     for (int i = 0; i < N; i++)       if (i < R[i]) swap(P[i], P[R[i]]);     for (int i = 1; i < N; i <<= 1) {       int dw = QPow(G, (Mod - 1) / (i << 1));       if (opt == -1) dw = QPow(dw, Mod - 2);       for (int j = 0; j < N; j += (i << 1))         for (int k = 0, w = 1; k < i; k++, w = 1ll * w * dw % Mod) {           int X = P[j + k], Y = 1ll * P[j + k + i] * w % Mod;           P[j + k] = (X + Y) % Mod;           P[j + k + i] = (X - Y + Mod) % Mod;         }     }     if (opt == -1) {       int inv = QPow(N, Mod - 2);       for (int i = 0; i < N; i++) P[i] = 1ll * P[i] * inv % Mod;     }     return;   }


#include <bits/stdc++.h>   using namespace std;   long long mi = 2e9;   long long ma = -2 * 1e9;   const int N = 1e5 + 5;   int main() {     std::ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     long long na, nb, ka, kb;     cin >> na >> nb >> ka >> kb;     long long a[na], b[nb];     for (int i = 0; i < na; ++i) cin >> a[i];     for (int i = 0; i < nb; ++i) cin >> b[i];     sort(a, a + na);     sort(b, b + nb);     long long x = nb - kb;     if (a[ka - 1] < b[nb - 1] && b[x] > a[ka - 1])       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   const long long Inf = 1ll << 50;   const long long N = 200010;   using namespace std;   long long cl[2 * N], b, c, d, i, j, k, l, n, ans, a[N], y, kk;   int main() {     scanf( %I64d%I64d , &n, &k);     for (i = n + 1; i > 0; i--) scanf( %I64d , &a[i]);     memset(cl, 0, sizeof(cl));     cl[0] = a[0] = 0;     for (i = 1; i <= n + 1; i++) {       cl[i] = (cl[i - 1] + a[i - 1]) << 1ll;       if ((cl[i] > Inf) || (cl[i] < -Inf)) break;     }     y = 0;     ans = 0;     i--;     for (j = n + 1; j > i; j--) {       if ((y & 1) == 0)         y >>= 1ll;       else         break;       y += a[j];     }     if (i == j) {       for (kk = i; kk > 0; kk--) {         if ((y % 2) == 0)           y /= 2ll;         else           break;         if (cl[kk] + y - y == cl[kk] && abs(cl[kk] + y) <= k &&             (!(kk == 1 && (cl[kk] + y == 0))))           ans++;         y += a[kk];       }     }     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long arr[300000];   long long temp[300000];   int main() {     int q;     cin >> q;     while (q--) {       int n;       cin >> n;       for (int i = 0; i < n; i++) {         cin >> arr[i];         temp[i] = arr[i];       }       int st = 0;       int endIdx1 = -1;       for (int i = 0; i < n; i++) {         if (arr[i] >= st) {           st++;           endIdx1 = i;         } else {           break;         }       }       if (endIdx1 == n - 1) {         cout <<  Yes  << endl;         continue;       }       st = 0;       int endIdx = n;       for (int i = n - 1; i >= 0; i--) {         if (arr[i] >= st) {           st++;           endIdx = i;         } else {           break;         }       }       if (endIdx == 0) {         cout <<  Yes  << endl;         continue;       }       if (endIdx1 >= endIdx) {         cout <<  Yes  << endl;       } else {         cout <<  No  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     long long k = 0, m = 1;     while (t > 0) {       if (t % 10 == 4) {         k = k + m;       } else {         k = k + 2 * m;       }       m = m * 2;       t = t / 10;     }     cout << k;   }


#include <bits/stdc++.h>   using namespace std;   int marked[1000006];   int f(int i, int j) {     if (j == 1) {       return marked[i];     }     return f(marked[i], j - 1);   }   vector<int> v;   int main() {     int n, a, b;     scanf( %d %d %d , &n, &a, &b);     int cut = -1;     for (int i = 0; i <= n; i++) {       if (i % a == 0 && (n - i) % b == 0) {         cut = i;         break;       }     }     if (cut == -1) {       printf( -1 n );       return 0;     }     int cnt = 0;     for (int i = 1; i <= cut; i++) {       cnt++;       v.push_back(i);       if (cnt == a) {         if ((int)v.size() >= 2) {           rotate(v.begin(), v.begin() + 1, v.end());         }         for (int j = 0; j < v.size(); j++) {           printf( %d  , v[j]);         }         cnt = 0;         v.clear();       }     }     cnt = 0;     for (int i = cut + 1; i <= n; i++) {       cnt++;       v.push_back(i);       if (cnt == b) {         if ((int)v.size() >= 2) {           rotate(v.begin(), v.begin() + 1, v.end());         }         for (int j = 0; j < v.size(); j++) {           printf( %d  , v[j]);         }         cnt = 0;         v.clear();       }     }   }


#include <bits/stdc++.h>   using namespace std;   void vin(vector<long long int> &v, long long int t) {     for (long long int i = 0; i < t; i++) cin >> v[i];   }   void vout(vector<long long int> &v, long long int t) {     for (long long int i = 0; i < t; i++) cout << v[i] <<    ;     cout << endl;   }   void ingraph(vector<vector<long long int>> &graph, long long int m) {     long long int x, y;     for (long long int i = 0; i < m; i++) {       cin >> x >> y;       x--, y--;       graph[x].push_back(y);       graph[y].push_back(x);     }   }   bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {     return (a.second < b.second);   }   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long int lcm(long long int a, long long int b) {     return a * b / gcd(a, b);   }   void solve() {     long long int n, m;     cin >> n >> m;     vector<vector<long long int>> a(n, vector<long long int>(m)),         dp(n, vector<long long int>(m));     for (long long int i = 0; i < n; i++) {       for (long long int j = 0; j < m; j++) cin >> a[i][j];     }     long long int ans = (long long int)1e18;     for (long long int i = 0; i < n; i++) {       for (long long int j = 0; j < m; j++) {         long long int b = a[i][j] - i - j;         if (a[0][0] < b) continue;         vector<vector<long long int>> dp(             n, vector<long long int>(m, (long long int)1e18));         dp[0][0] = a[0][0] - b;         for (long long int p = 0; p < n; p++) {           for (long long int q = 0; q < m; q++) {             if (a[p][q] < (b + p + q)) continue;             if (p > 0)               dp[p][q] = min(dp[p][q], dp[p - 1][q] + a[p][q] - (b + q + p));             if (q > 0)               dp[p][q] = min(dp[p][q], dp[p][q - 1] + a[p][q] - (b + q + p));           }         }         ans = min(ans, dp[n - 1][m - 1]);       }     }     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int t;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct SCC {     int N;     vector<bool> used;     vector<int> vs;     vector<int> cmp;     void dfs(int v, const vector<int> G[]) {       used[v] = true;       for (auto u : G[v]) {         if (!used[u]) dfs(u, G);       }       vs.push_back(v);     }     void rdfs(int v, int k, const vector<int> rG[]) {       used[v] = true;       cmp[v] = k;       for (auto u : rG[v]) {         if (!used[u]) rdfs(u, k, rG);       }     }     void calculate(int Nin, const vector<int> G[], const vector<int> rG[]) {       N = Nin;       used.resize(N);       fill(used.begin(), used.end(), false);       for (int v = 0; v < N; v++) {         if (!used[v]) dfs(v, G);       }       fill(used.begin(), used.end(), false);       cmp.resize(N);       int k = 0;       for (int i = vs.size() - 1; i >= 0; i--) {         if (!used[vs[i]]) rdfs(vs[i], k++, rG);       }     }     int dump_graph(const vector<int> G[], vector<int> edges2[]) {       for (int i = 0; i < N; i++) {         for (auto j : G[i]) {           if (cmp[i] != cmp[j]) {             edges2[cmp[i]].push_back(cmp[j]);           }         }       }       return *max_element(cmp.begin(), cmp.end()) + 1;     }   };   vector<int> edges[5000000], edges2[5000000];   int main() {     int N, M, D;     cin >> N >> M >> D;     for (int i = 0; i < M; i++) {       int a, b;       scanf( %d %d , &a, &b);       a--;       b--;       for (int d = 0; d < D; d++) {         int s = a * D + d, t = b * D + (d + 1) % D;         edges[s].push_back(t);         edges2[t].push_back(s);       }     }     SCC scc;     scc.calculate(D * N, edges, edges2);     for (int i = 0; i < N * D; i++) edges2[i].clear();     int N2 = scc.dump_graph(edges, edges2);     vector<int> num(N * D);     char s[50];     for (int i = 0; i < N; i++) {       scanf( %s , s);       set<int> st;       for (int d = 0; d < D; d++) {         if (s[d] ==  0 ) continue;         int c = scc.cmp[i * D + d];         if (!st.count(c)) {           num[c]++;           st.insert(c);         }       }     }     vector<int> indeg(N2);     for (int i = 0; i < N2; i++)       for (int j : edges2[i]) indeg[j]++;     vector<int> dp(N2);     queue<int> que;     for (int i = 0; i < N2; i++) {       dp[i] = -1e9;       if (indeg[i] == 0) que.push(i);     }     int st = scc.cmp[0];     dp[st] = num[st];     while (que.size()) {       int i = que.front();       que.pop();       for (int j : edges2[i]) {         dp[j] = max(dp[j], dp[i] + num[j]);         indeg[j]--;         if (indeg[j] == 0) que.push(j);       }     }     int ans = *max_element(dp.begin(), dp.end());     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   int main() {     int n(0);     scanf( %d n , &n);     std::string number;     getline(std::cin, number);     if (number.size() <= 3) {       std::cout << number << std::endl;     } else {       int index(0);       if (number.size() % 2) {         std::cout << number.substr(0, 3);         index = 3;       } else {         std::cout << number.substr(0, 2);         index = 2;       }       while (index < number.size()) {         std::cout <<  - ;         std::cout << number.substr(index, 2);         index += 2;       }       std::cout << std::endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( -O3 ,  unroll-loops )   using namespace std;   template <typename T1, typename T2>   inline void chmin(T1& a, T2 b) {     if (a > b) a = b;   }   template <typename T1, typename T2>   inline void chmax(T1& a, T2 b) {     if (a < b) a = b;   }   const long long MOD = 1e9 + 7;   const long long MAX = 200010;   const double pi = acos(-1);   vector<long long> G[200010];   long long dp[200020];   long long dp_init(long long n, long long pre) {     if (dp[n] != 0) return dp[n];     long long res = 1;     for (auto nex : G[n]) {       if (nex == pre) continue;       res += dp_init(nex, n);     }     dp[n] = res;     return res;   }   long long ans = 0;   long long N;   bool used[200020];   void calc(long long n, long long res) {     used[n] = true;     for (auto nex : G[n]) {       if (used[nex]) continue;       long long tmp = res + N - 2 * dp[nex];       chmax(ans, tmp);       used[nex] = true;       calc(nex, tmp);     }   }   void solve() {     cin >> N;     for (long long i = 0; i < N - 1; i++) {       long long u, v;       cin >> u >> v;       u--, v--;       G[u].push_back(v);       G[v].push_back(u);     }     long long root = 0;     dp_init(0, 0);     long long tmp = 0;     for (long long i = 0; i < N; i++) tmp += dp[i];     ans = tmp;     calc(0, tmp);     cout << ans <<   n ;     ;   }   signed main() {     cin.tie(0);     ios::sync_with_stdio(false);     solve();   }


#include <bits/stdc++.h>   using namespace std;   int xd[] = {0, 1, 0, -1};   int yd[] = {1, 0, -1, 0};   int xx[] = {0, 1, 0, -1, 1, -1, 1, -1};   int yy[] = {1, 0, -1, 0, 1, 1, -1, -1};   int kx[] = {-2, -1, -2, -1, 1, 2, 2, 1};   int ky[] = {1, 2, -1, -2, -2, -1, 1, 2};   bool islucky(int x) {     int four = 0, seven = 0;     while (x) {       if (x % 10 != 7 && x % 10 != 4) return false;       if (x % 10 == 7)         seven++;       else         four++;       x = x / 10;     }     return four == seven;   }   bool is_prime(long long x) {     if (x == 1) return false;     for (int i = 2; i * i <= x; i++)       if (x % i == 0) return false;     return true;   }   long long C(int k, int l) {     if (l == k) return 1;     if (l * 2 > k) l = k - l;     long long res = 1, temp = l;     for (int i = k - l + 1; i <= k; i++) {       res *= i / temp;       temp--;     }     return res;   }   long long sumOfDigit(int x) {     int res = 0;     while (x) {       res += x % 10;       x /= 10;     }     return res;   }   uint64_t bigPow(uint64_t base, uint64_t p) {     uint64_t temp = 1;     while (p--) {       temp *= base;     }     return temp;   }   vector<int> soso;   bool prime[10000001] = {0};   void SieveOfEratosthenes(int n) {     soso.push_back(2);     for (int p = 3; p * p <= n; p += 2) {       if (prime[p] == false) {         for (int i = p * 2; i <= n; i += p) prime[i] = true;       }     }     for (int i = 3; i <= n; i += 2)       if (!prime[i]) soso.push_back(i);   }   vector<vector<int> > AdjList;   bool visited[100005];   void dfs(int s) {     if (visited[s]) return;     visited[s] = 1;     for (int i = 0; i < AdjList[s].size(); i++) dfs(AdjList[s][i]);     return;   }   long long gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   int main() {     char grid[8][8];     for (int i = 0; i < 8; i++) cin >> grid[i];     int w = 0, b = 0;     for (int i = 0; i < 8; i++)       for (int j = 0; j < 8; j++) {         if (grid[i][j] !=  . ) {           char c = grid[i][j];           if (c ==  q ) b += 9;           if (c ==  Q ) w += 9;           if (c ==  r ) b += 5;           if (c ==  R ) w += 5;           if (c ==  b  || c ==  n ) b += 3;           if (c ==  B  || c ==  N ) w += 3;           if (c ==  p ) b += 1;           if (c ==  P ) w += 1;         }       }     if (b == w)       cout <<  Draw ;     else if (b > w)       cout <<  Black ;     else       cout <<  White ;   }


#include <bits/stdc++.h>   const long long N = 100100;   const long long MAX = 1000000100;   using namespace std;   long long a[N] = {0};   int main() {     long long n;     while (scanf( %lld , &n) != EOF) {       map<long long, long long> num;       for (long long i = 0; i < n; i++) {         scanf( %lld , &a[i]);         num[a[i]]++;       }       sort(a, a + n);       long long min1 = MAX + 1, min2 = MAX + 2, min3 = MAX + 3;       for (long long i = 0; i < n; i++) {         if (a[i] < min1) {           min1 = a[i];         } else if (a[i] > min1 && a[i] < min2) {           min2 = a[i];         } else if (a[i] > min2 && a[i] < min3) {           min3 = a[i];           break;         }       }       if (num[min1] >= 3) {         long long sum = 1;         for (long long i = 3; i >= 1; i--) sum *= num[min1]--;         cout << sum / 6 << endl;       } else if (num[min1] == 2) {         cout << num[min2] << endl;       } else {         if (num[min2] >= 2) {           long long sum = 1;           for (long long i = 2; i >= 1; i--) sum *= num[min2]--;           cout << sum / 2 << endl;         } else {           cout << num[min3] << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int m = 0, b = 0;   int main() {     int n;     cin >> n;     int Arrpi[n];     for (int j = 0; j < n; ++j) {       cin >> Arrpi[j];     }     int A[n];     for (int l = 0; l < n; ++l) {       m = Arrpi[l] - 1;       b = l + 1;       A[m] = b;     }     for (int y = 0; y < n; ++y) {       cout << A[y] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T read() {     register T sum = 0;     register char cc = getchar();     int sym = 1;     while (cc !=  -  && (cc >  9  || cc <  0 )) cc = getchar();     if (cc ==  - ) sym = -1, cc = getchar();     sum = sum * 10 + cc -  0 ;     cc = getchar();     while (cc >=  0  && cc <=  9 ) sum = sum * 10 + cc -  0 , cc = getchar();     return sym * sum;   }   template <typename T>   inline T read(T& a) {     a = read<T>();     return a;   }   template <typename T, typename... Others>   inline void read(T& a, Others&... b) {     a = read(a);     read(b...);   }   int n, ans = 0x7fffffff, b[200010], prim[1000010];   long long a[200010];   set<long long> s;   void init() {     for (int i = 2; i <= 1000000; i++) {       if (!prim[i]) prim[++prim[0]] = i;       for (int j = 1; j <= prim[0] && i * prim[j] <= 1000000; j++) {         prim[i * prim[j]] = true;         if (i % prim[j]) break;       }     }   }   void div(long long x) {     for (int i = 1; i <= prim[0]; i++) {       if (x % prim[i]) continue;       s.insert(prim[i]);       while (x % prim[i] == 0) x /= prim[i];     }     if (x != 1) s.insert(x);   }   int solve(long long p) {     long long res = 0;     for (int i = 1; i <= n; i++) {       long long d = a[i] < p ? p - a[i] : min(a[i] % p, p - a[i] % p);       res = min(1ll * n, res + d);     }     return res;   }   int main() {     srand(time(NULL));     init();     read(n);     for (int i = 1; i <= n; i++) {       read(a[i]);       b[i] = i;     }     random_shuffle(b + 1, b + 1 + n);     for (int i = 1; i <= 100 && i <= n; i++) {       long long t = a[b[i]];       div(t);       div(t + 1);       if (t > 1) div(t - 1);     }     for (auto i : s) ans = min(ans, solve(i));     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, c, q, i;     cin >> q;     for (i = 1; i <= q; i++) {       cin >> n;       c = 0;       while (1) {         if (n % 2 == 0) {           n = n / 2;           c++;         } else if (n % 3 == 0) {           n = (2 * n) / 3;           c++;         } else if (n % 5 == 0) {           n = (4 * n) / 5;           c++;         } else           break;       }       if (n == 1)         cout << c << endl;       else         cout << -1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1505;   const int mo = 998244353;   int n, m, a[N][N];   int fa[N], sz[N], S[N];   int f[N][N], tmp[N];   pair<int, int> e[N * N];   int get(int x) { return x == fa[x] ? x : fa[x] = get(fa[x]); }   void merge(int x, int y) {     memset(tmp, 0, sizeof(tmp));     for (int i = (int)(1); i <= (int)(sz[x]); i++)       for (int j = (int)(1); j <= (int)(sz[y]); j++)         tmp[i + j] = (tmp[i + j] + 1ll * f[x][i] * f[y][j]) % mo;     memcpy(f[x], tmp, sizeof(f[x]));     sz[x] += sz[y];     S[x] += S[y];     fa[y] = x;   }   bool cmp(pair<int, int> x, pair<int, int> y) {     return a[x.first][x.second] < a[y.first][y.second];   }   int main() {     scanf( %d , &n);     for (int i = (int)(1); i <= (int)(n); i++)       for (int j = (int)(1); j <= (int)(n); j++) scanf( %d , &a[i][j]);     for (int i = (int)(1); i <= (int)(n); i++)       for (int j = (int)(i + 1); j <= (int)(n); j++)         e[++m] = pair<int, int>(i, j);     sort(e + 1, e + m + 1, cmp);     for (int i = (int)(1); i <= (int)(n); i++)       fa[i] = i, sz[i] = 1, S[i] = 0, f[i][1] = 1;     for (int i = (int)(1); i <= (int)(m); i++) {       int x = get(e[i].first);       int y = get(e[i].second);       if (x != y) merge(x, y);       ++S[x];       if (S[x] == sz[x] * (sz[x] - 1) / 2) f[x][1] = (f[x][1] + 1) % mo;     }     for (int i = (int)(1); i <= (int)(n); i++) cout << f[get(1)][i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   string to_string(string s) { return     + s +    ; }   string to_string(const char *s) { return to_string((string)s); }   string to_string(bool b) { return (b ?  true  :  false ); }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto &x : v) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }   void debug_out() { cerr << endl; }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     debug_out(T...);   }   class segtree {    public:     struct node {       long long mn = 0;       long long add = 0;       void apply(int l, int r, long long v) {         mn += v;         add += v;       }     };     node unite(const node &a, const node &b) const {       node res;       res.mn = min(a.mn, b.mn);       return res;     }     inline void push(int x, int l, int r) {       int y = (l + r) >> 1;       int z = x + ((y - l + 1) << 1);       if (tree[x].add != 0) {         tree[x + 1].apply(l, y, tree[x].add);         tree[z].apply(y + 1, r, tree[x].add);         tree[x].add = 0;       }     }     inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); }     int n;     vector<node> tree;     void build(int x, int l, int r) {       if (l == r) {         return;       }       int y = (l + r) >> 1;       int z = x + ((y - l + 1) << 1);       build(x + 1, l, y);       build(z, y + 1, r);       pull(x, z);     }     template <typename M>     void build(int x, int l, int r, const vector<M> &v) {       if (l == r) {         tree[x].apply(l, r, v[l]);         return;       }       int y = (l + r) >> 1;       int z = x + ((y - l + 1) << 1);       build(x + 1, l, y, v);       build(z, y + 1, r, v);       pull(x, z);     }     node get(int x, int l, int r, int ll, int rr) {       if (ll <= l && r <= rr) {         return tree[x];       }       int y = (l + r) >> 1;       int z = x + ((y - l + 1) << 1);       push(x, l, r);       node res{};       if (rr <= y) {         res = get(x + 1, l, y, ll, rr);       } else {         if (ll > y) {           res = get(z, y + 1, r, ll, rr);         } else {           res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr));         }       }       pull(x, z);       return res;     }     template <typename... M>     void modify(int x, int l, int r, int ll, int rr, const M &...v) {       if (ll <= l && r <= rr) {         tree[x].apply(l, r, v...);         return;       }       int y = (l + r) >> 1;       int z = x + ((y - l + 1) << 1);       push(x, l, r);       if (ll <= y) {         modify(x + 1, l, y, ll, rr, v...);       }       if (rr > y) {         modify(z, y + 1, r, ll, rr, v...);       }       pull(x, z);     }     int find_first_knowingly(int x, int l, int r,                              const function<bool(const node &)> &f) {       if (l == r) {         return l;       }       push(x, l, r);       int y = (l + r) >> 1;       int z = x + ((y - l + 1) << 1);       int res;       if (f(tree[x + 1])) {         res = find_first_knowingly(x + 1, l, y, f);       } else {         res = find_first_knowingly(z, y + 1, r, f);       }       pull(x, z);       return res;     }     int find_first(int x, int l, int r, int ll, int rr,                    const function<bool(const node &)> &f) {       if (ll <= l && r <= rr) {         if (!f(tree[x])) {           return -1;         }         return find_first_knowingly(x, l, r, f);       }       push(x, l, r);       int y = (l + r) >> 1;       int z = x + ((y - l + 1) << 1);       int res = -1;       if (ll <= y) {         res = find_first(x + 1, l, y, ll, rr, f);       }       if (rr > y && res == -1) {         res = find_first(z, y + 1, r, ll, rr, f);       }       pull(x, z);       return res;     }     int find_last_knowingly(int x, int l, int r,                             const function<bool(const node &)> &f) {       if (l == r) {         return l;       }       push(x, l, r);       int y = (l + r) >> 1;       int z = x + ((y - l + 1) << 1);       int res;       if (f(tree[z])) {         res = find_last_knowingly(z, y + 1, r, f);       } else {         res = find_last_knowingly(x + 1, l, y, f);       }       pull(x, z);       return res;     }     int find_last(int x, int l, int r, int ll, int rr,                   const function<bool(const node &)> &f) {       if (ll <= l && r <= rr) {         if (!f(tree[x])) {           return -1;         }         return find_last_knowingly(x, l, r, f);       }       push(x, l, r);       int y = (l + r) >> 1;       int z = x + ((y - l + 1) << 1);       int res = -1;       if (rr > y) {         res = find_last(z, y + 1, r, ll, rr, f);       }       if (ll <= y && res == -1) {         res = find_last(x + 1, l, y, ll, rr, f);       }       pull(x, z);       return res;     }     segtree(int _n) : n(_n) {       assert(n > 0);       tree.resize(2 * n - 1);       build(0, 0, n - 1);     }     template <typename M>     segtree(const vector<M> &v) {       n = v.size();       assert(n > 0);       tree.resize(2 * n - 1);       build(0, 0, n - 1, v);     }     node get(int ll, int rr) {       assert(0 <= ll && ll <= rr && rr <= n - 1);       return get(0, 0, n - 1, ll, rr);     }     node get(int p) {       assert(0 <= p && p <= n - 1);       return get(0, 0, n - 1, p, p);     }     template <typename... M>     void modify(int ll, int rr, const M &...v) {       assert(0 <= ll && ll <= rr && rr <= n - 1);       modify(0, 0, n - 1, ll, rr, v...);     }     int find_first(int ll, int rr, const function<bool(const node &)> &f) {       assert(0 <= ll && ll <= rr && rr <= n - 1);       return find_first(0, 0, n - 1, ll, rr, f);     }     int find_last(int ll, int rr, const function<bool(const node &)> &f) {       assert(0 <= ll && ll <= rr && rr <= n - 1);       return find_last(0, 0, n - 1, ll, rr, f);     }   };   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, tt;     cin >> n >> tt;     vector<int> p(n);     vector<int> w(n);     for (int i = 1; i < n; i++) {       cin >> p[i] >> w[i];       p[i]--;     }     vector<vector<int>> qs(n);     vector<int> L(tt), R(tt), v(tt);     vector<long long> res(tt, -1);     for (int i = 0; i < tt; i++) {       cin >> v[i] >> L[i] >> R[i];       v[i]--;       L[i]--;       R[i]--;       qs[v[i]].push_back(i);     }     vector<long long> dst(n);     for (int i = 1; i < n; i++) {       dst[i] = dst[p[i]] + w[i];     }     vector<int> is_leaf(n, 1);     for (int i = 1; i < n; i++) {       is_leaf[p[i]] = 0;     }     const long long inf = (long long)1e18;     for (int i = 0; i < n; i++) {       if (!is_leaf[i]) {         dst[i] = inf;       }     }     segtree st(dst);     vector<int> stk;     vector<int> to(n, n - 1);     for (int i = 0; i < n; i++) {       while (!stk.empty() && stk.back() != p[i]) {         int u = stk.back();         to[u] = i - 1;         stk.pop_back();       }       stk.push_back(i);     }     42;     stk.clear();     for (int i = 0; i < n; i++) {       while (!stk.empty() && stk.back() != p[i]) {         int u = stk.back();         st.modify(0, n - 1, -w[u]);         st.modify(u, to[u], 2 * w[u]);         stk.pop_back();       }       stk.push_back(i);       st.modify(0, n - 1, w[i]);       st.modify(i, to[i], -2 * w[i]);       for (int id : qs[i]) {         res[id] = st.get(L[id], R[id]).mn;       }     }     for (int i = 0; i < tt; i++) {       cout << res[i] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N;   string s;   string result;   int main() {     cin >> N;     cin >> s;     result = s;     result[(N - 1) / 2] = s[0];     int sign = N % 2 == 0 ? 1 : -1;     int val = 1;     int start = (N - 1) / 2;     for (int i = 1; i < N; ++i, sign *= -1, val++)       start = start + val * sign, result[start] = s[i];     cout << result;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e3 + 5;   struct edge {     int u, v, id;     edge(int a = 0, int b = 0, int c = 0) { u = a, v = b, id = c; }   };   vector<edge> S, M, E;   struct dsu {     int par[N];     vector<edge> ed;     dsu() { memset(par, -1, sizeof par); }     int get_par(int u) { return ~par[u] ? par[u] = get_par(par[u]) : u; }     bool merge(edge e) {       if (get_par(e.u) == get_par(e.v)) return false;       par[get_par(e.u)] = get_par(e.v), ed.push_back(e);       return true;     }     int size() { return ed.size(); }   } A, B, C;   int n, m;   void notValid() { cout <<  -1 n , exit(0); }   void readInput() {     cin >> n >> m;     for (int i = 0; i < m; i++) {       char c;       int u, v;       cin >> u >> v >> c;       u--, v--;       if (c ==  S )         S.push_back(edge(u, v, i));       else         M.push_back(edge(u, v, i));     }   }   void solve() {     for (auto e : S) C.merge(e);     if (n & 1 ^ 1 || C.size() < n >> 1) notValid();     for (auto e : M)       if (C.merge(e)) A.merge(e);     if (C.size() != n - 1) notValid();     for (auto e : M)       if (A.size() < n >> 1) A.merge(e);     if (A.size() != n >> 1) notValid();     E = A.ed;     for (auto e : S)       if (A.merge(e)) B.merge(e);     E.insert(E.end(), B.ed.begin(), B.ed.end());   }   void writeOutput() {     cout << n - 1 << endl;     for (auto e : E) cout << e.id + 1 <<    ;   }   int main() {     ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);     readInput(), solve(), writeOutput();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const ll inf = 1e9 + 10;   const ll inf_ll = 1e18 + 10;   ll qry(ll t, ll i, ll j, ll k) {     cout << t <<     << i <<     << j <<     << k << endl;     ll r;     cin >> r;     return r;   }   int main() {     ll n;     cin >> n;     ll b = 2;     for (ll i = 2; i <= n; i++)       if (i != b && qry(2, 1, i, b) > 0) b = i;     vector<ll> h(n + 5), ord(n - 2), l, r;     for (ll i = 2; i <= n; i++)       if (i != b) h[i] = qry(1, 1, i, b);     iota((ord).begin(), (ord).end(), 3);     for (ll& i : ord)       if (i == b) i = 2;     sort((ord).begin(), (ord).end(), [&](ll i, ll j) { return h[i] < h[j]; });     ll c = ord.back();     for (ll i : ord) {       if (i != c && qry(2, 1, c, i) > 0)         l.push_back(i);       else         r.push_back(i);     }     cout <<  0 1   << b;     for (ll i : r) cout <<     << i;     reverse((l).begin(), (l).end());     for (ll i : l) cout <<     << i;     cout << endl;   }


#include <bits/stdc++.h>   int main() {     int n;     int x;     int sum = 0;     std::cin >> n;     std::cin >> x;     unsigned int t1 = 0, t2 = 0, tcurr = 1;     for (int i = 0; i < n; i++) {       std::cin >> t1;       std::cin >> t2;       sum += (t1 - tcurr) % x + t2 - t1 + 1;       tcurr = t2 + 1;     }     std::cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> A;   vector<int> d;   vector<int> P;   vector<int> g;   vector<vector<int>> G;   int bestu, bestv, bestw, bestvv;   void dfs(int x, int v, int n) {     for (int i = 0; i < G[x].size(); ++i) {       int y = G[x][i];       int u = g[y];       if (u >= 0) {         if (d[u] < 0) {           d[u] = v;           P[u] = y;           P[y] = x;           dfs(u, v, n);         }       } else if (A[y] + A[v] > bestw) {         bestw = A[y] + A[v];         bestu = x;         bestv = y;         bestvv = v;       }     }   }   bool cmp(int a, int b) { return A[a] > A[b]; }   void solve() {     int n, k;     scanf( %d%d , &n, &k);     vector<int> a(1 << n);     for (int i = 0; i < a.size(); ++i) scanf( %d , &a[i]);     vector<array<int, 3>> edges;     {       vector<int> bc(1 << n);       for (int i = 1; i < bc.size(); ++i) bc[i] = bc[i & (i - 1)] + 1;       array<int, 3> e;       for (int i = 0; i < bc.size(); ++i) {         if (bc[i] % 2 == 0)           for (int j = 0; j < n; ++j) {             e[0] = -(a[i] + a[i ^ (1 << j)]);             e[1] = i;             e[2] = i ^ (1 << j);             edges.push_back(e);           }       }     }     int edges_cnt = min(2 * n * k + 1, (int)edges.size());     nth_element(edges.begin(), edges.begin() + edges_cnt, edges.end());     vector<int> idx(1 << n, -1);     int vert = 0;     for (int i = 0; i < edges_cnt; ++i)       if (idx[edges[i][1]] < 0) {         A.push_back(a[edges[i][1]]);         idx[edges[i][1]] = vert;         ++vert;       }     G.resize(vert);     int vert2 = vert;     for (int i = 0; i < edges_cnt; ++i) {       if (idx[edges[i][2]] < 0) {         A.push_back(a[edges[i][2]]);         idx[edges[i][2]] = vert2;         ++vert2;       }       G[idx[edges[i][1]]].push_back(idx[edges[i][2]]);     }     vector<int> idx1(vert);     for (int i = 0; i < vert; ++i) idx1[i] = i;     sort(idx1.begin(), idx1.end(), cmp);     vector<char> was(vert);     g.assign(vert2, -1);     P.resize(vert2);     int res = 0;     for (int i = 0; i < k; ++i) {       d.assign(vert, -1);       bestw = -1;       for (int j = 0; j < vert; ++j) {         int x = idx1[j];         if (!was[x] && d[x] < 0) {           P[x] = -1;           d[x] = x;           dfs(x, x, n);         }       }       if (bestw >= 0) {         res += bestw;         g[bestv] = bestu;         was[bestvv] = 1;         for (int x = P[bestu]; x >= 0;) {           int y = P[x];           g[x] = y;           x = P[y];         }       } else         break;     }     printf( %d n , res);   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:300000000 )   #pragma warning(disable : 4800)   using namespace std;   const double pi = 3.1415926535897932384626433832795;   template <class T>   T abs(const T &a) {     return a >= 0 ? a : -a;   };   template <class T>   T sqr(const T &x) {     return x * x;   }   int n, m, k;   vector<vector<int> > g;   vector<bool> visited;   vector<int> ans;   vector<pair<int, int> > traders;   pair<int, int> erased;   bool isErased(int x, int y) {     return pair<int, int>(x, y) == erased || pair<int, int>(y, x) == erased;   }   void dfs(int k) {     if (visited[k]) return;     visited[k] = true;     for (int i = 0; i < (int((g[k]).size())); i++) {       int to = g[k][i];       if (!isErased(k, to)) dfs(to);     }   }   void work(int x, int y) {     erased = pair<int, int>(x, y);     visited.assign(n, false);     dfs(x);     for (int i = 0; i < (k); i++)       if (visited[traders[i].first] && !visited[traders[i].second]) ans[i]++;   }   int main() {     cin >> n >> m;     g.resize(n);     for (int i = 0; i < (m); i++) {       int x, y;       cin >> x >> y;       x--, y--;       g[x].push_back(y);       g[y].push_back(x);     }     cin >> k;     ans.resize(k);     traders.resize(k);     for (int i = 0; i < (k); i++) {       cin >> traders[i].first >> traders[i].second;       traders[i].first--;       traders[i].second--;     }     for (int i = 0; i < (n); i++)       for (int j = 0; j < (int((g[i]).size())); j++) work(i, g[i][j]);     for (int i = 0; i < (k); i++) cout << ans[i] << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0;     bool pos = 1;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) pos = 0;     for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ;     return pos ? x : -x;   }   const int N = 1 << 18 | 2, M = N;   const int mod = 998244353, G = 3;   int sz[N], dep[N];   vector<int> v[N];   inline int ksm(int a, int b) {     int ans = 1;     for (; b; b >>= 1) {       if (b & 1) ans = (long long)ans * a % mod;       a = (long long)a * a % mod;     }     return ans;   }   struct ffter {     int wn[N], rev[N];     void init(int n) {       int t = 1, L = 0;       while (t < n) {         t <<= 1;         L++;       }       int w = ksm(G, (mod - 1) / n);       for (int i = wn[0] = 1; i < n; i++) {         wn[i] = (long long)wn[i - 1] * w % mod;         rev[i] = rev[i >> 1] >> 1 | ((i & 1) << (L - 1));       }     }     void zheng(vector<int> &a) {       for (auto &i : a) i = (i + mod) % mod;     }     void dft(vector<int> &a) {       int n = a.size();       zheng(a);       for (int i = 0; i < n; i++)         if (rev[i] < i) swap(a[rev[i]], a[i]);       for (int i = 1; i < n; i <<= 1) {         for (int j = 0; j < n; j += i << 1) {           for (int k = j, l = 0; l < i; k++, l++) {             long long t = (long long)wn[l * (n / (i << 1))] * a[k + i] % mod;             a[k + i] = (a[k] < t) ? a[k] - t + mod : a[k] - t;             a[k] = a[k] + t >= mod ? a[k] + t - mod : a[k] + t;           }         }       }     }     void idft(vector<int> &a) {       int n = a.size();       dft(a);       reverse(a.begin() + 1, a.end());       int zs = ksm(n, mod - 2);       for (auto &i : a) i = (long long)i * zs % mod;     }   } T;   void FFT(vector<int> &a, vector<int> &b) {     int n = a.size() + b.size() - 1;     int len = 1;     while (len < n) len <<= 1;     T.init(len);     a.resize(len);     b.resize(len);     T.dft(a);     T.dft(b);     for (int i = 0; i < len; i++) a[i] = (long long)a[i] * b[i] % mod;     T.idft(a);     a.resize(n);   }   int n, son[N];   vector<int> tr[M], Tr[M], dp[N], f[N];   void solve(int p);   void zy(vector<int> &a, int len) {     a.resize(a.size() + len);     for (int i = a.size() - 1; i >= len; i--) a[i] = a[i - len];     for (int i = 0; i < len; i++) a[i] = 0;   }   void bao(int p, int son) {     for (auto i : v[p])       if (dep[i] > dep[p] && i != son) solve(i);   }   vector<int> get(int p, int son) {     priority_queue<pair<int, vector<int> > > q;     while (q.size()) q.pop();     for (auto i : v[p])       if (dep[i] > dep[p] && i != son) {         q.push(make_pair(-dp[i].size(), dp[i]));       }     if (q.size() == 0) {       vector<int> jb(1);       jb[0] = 1;       return jb;     }     while (q.size() > 1) {       vector<int> a = q.top().second;       q.pop();       vector<int> b = q.top().second;       q.pop();       FFT(a, b);       q.push(make_pair(-a.size(), a));     }     vector<int> jb = q.top().second;     q.pop();     return jb;   }   vector<int> operator+(vector<int> a, vector<int> b) {     if (a.size() < b.size()) swap(a, b);     for (unsigned i = 0; i < b.size(); i++) a[i] = (a[i] + b[i]) % mod;     return a;   }   int TOT;   void bao(int l, int r, int nod) {     if (l == r) {       tr[nod] = f[l];       if (r == TOT) {         Tr[nod] = tr[nod];         zy(Tr[nod], 1);       } else         Tr[nod].resize(1);       Tr[nod][0] = 1;       return;     }     int mid = (l + r) >> 1;     bao(l, mid, nod << 1);     bao(mid + 1, r, nod << 1 | 1);     tr[nod] = tr[nod << 1];     FFT(tr[nod], tr[nod << 1 | 1]);     FFT(Tr[nod << 1 | 1], tr[nod << 1]);     zy(Tr[nod << 1 | 1], mid - l + 1);     Tr[nod] = Tr[nod << 1] + Tr[nod << 1 | 1];   }   void solve(int p) {     int tot = 0, jb = 0;     for (int i = p; i; i = son[i]) bao(i, son[i]);     for (int i = p; i; i = son[i]) {       f[++tot] = get(i, son[i]);     }     for (int i = 1; i <= tot; i++) jb += f[i].size();     TOT = tot;     bao(1, tot, 1);     dp[p] = Tr[1];   }   void init(int p, int fa) {     sz[p] = 1;     dep[p] = dep[fa] + 1;     for (auto i : v[p])       if (i != fa) {         init(i, p);         sz[p] += sz[i];         if (sz[i] > sz[son[p]]) son[p] = i;       }   }   int main() {     n = read();     int x = read() % mod;     for (int i = 1; i < n; i++) {       int x = read(), y = read();       v[x].push_back(y);       v[y].push_back(x);     }     init(1, 0);     solve(1);     vector<int> ans = dp[1];     ans[0]--;     int dq = 1, Ans = 0;     for (int i = 1; i <= n; i++) {       if (i > 1)         dq = (long long)dq * (i + x - 1) % mod * ksm(i - 1, mod - 2) % mod;       Ans = (Ans + (long long)dq * ans[i]) % mod;     }     cout << (Ans + mod) % mod << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX_N = 3e5 + 7;   const long long MOD = 998244353;   long long n;   long long deg[MAX_N], fac[MAX_N];   long long f_pow(long long base, long long b, long long mod) {     long long res = 1;     while (b) {       if (b & 1) res = res * base % mod;       base = base * base % mod;       b >>= 1;     }     return res;   }   namespace NTT {   const long long MAX_N = 3e6 + 7;   const long long MOD = 998244353;   const long long G = 3;   long long rev[MAX_N];   long long f[MAX_N], g[MAX_N];   long long n, m;   vector<vector<long long>> p;   struct Poly {     long long n;     vector<long long> p;     Poly(vector<long long> _p = {}, long long _n = 0) : n(_n) {       n = _p.size();       p.resize(n);       for (long long i = 0; i < n; ++i) p[i] = _p[i];     }     bool operator<(const Poly &a) const { return n > a.n; }   };   void ntt(long long *a, long long n, long long dft) {     for (long long i = 0; i < n; i++) {       if (i < rev[i]) swap(a[i], a[rev[i]]);     }     for (long long i = 1; i < n; i <<= 1) {       long long wn = f_pow(G, (MOD - 1) / (i << 1), MOD);       if (dft < 0) wn = f_pow(wn, MOD - 2, MOD);       for (long long j = 0; j < n; j += (i << 1)) {         long long wnk = 1;         for (long long k = j; k < j + i; k++) {           long long a1 = a[k], a2 = a[k + i];           a[k] = (a1 + wnk * a2 % MOD) % MOD;           a[k + i] = (a1 - wnk * a2 % MOD) % MOD;           wnk = wnk * wn % MOD;         }       }     }     if (dft == -1) {       long long inv = f_pow(n, MOD - 2, MOD);       for (long long i = 0; i < n; i++) a[i] = a[i] * inv % MOD;     }   }   vector<long long> merge(const vector<long long> &F,                           const vector<long long> &G) {     n = F.size() - 1, m = G.size() - 1;     long long N = 1, p = 0;     while (N < (m + n + 1)) N <<= 1, ++p;     for (long long i = 0; i < N; ++i) rev[i] = f[i] = g[i] = 0;     for (long long i = 0; i < N; ++i)       rev[i] = ((rev[i >> 1] >> 1) | ((i & 1) << (p - 1)));     for (long long i = 0; i <= n; ++i) f[i] = F[i];     for (long long i = 0; i <= m; ++i) g[i] = G[i];     ntt(f, N, 1), ntt(g, N, 1);     for (long long i = 0; i < N; ++i) f[i] = f[i] * g[i] % MOD;     ntt(f, N, -1);     vector<long long> res(m + n + 1);     for (long long i = 0; i <= n + m; ++i) res[i] = (f[i] + MOD) % MOD;     return res;   }   vector<long long> div_ntt(long long l, long long r) {     if (l == r) return p[l];     long long mid = (l + r) >> 1;     vector<long long> F = div_ntt(l, mid), G = div_ntt(mid + 1, r);     return merge(F, G);   }   void merge(long long idx_f, long long idx_g) {     vector<long long> &F = p[idx_f];     vector<long long> &G = p[idx_g];     n = F.size() - 1, m = G.size() - 1;     long long N = 1, p = 0;     while (N < (m + n + 1)) N <<= 1, p++;     for (long long i = 0; i < N; ++i) rev[i] = f[i] = g[i] = 0;     for (long long i = 0; i < N; i++)       rev[i] = ((rev[i >> 1] >> 1) | ((i & 1) << (p - 1)));     for (long long i = 0; i <= n; i++) f[i] = F[i];     for (long long i = 0; i <= m; i++) g[i] = G[i];     ntt(f, N, 1);     ntt(g, N, 1);     for (long long i = 0; i < N; i++) {       f[i] = f[i] * g[i] % MOD;     }     ntt(f, N, -1);     F.resize(m + n + 1);     for (long long i = 0; i <= (n + m); ++i) F[i] = (f[i] + MOD) % MOD;   }   vector<long long> solve() {     priority_queue<Poly> q;     for (long long i = 0; i < p.size(); ++i) q.push(Poly(p[i]));     while (q.size() > 1) {       vector<long long> F = q.top().p;       q.pop();       vector<long long> G = q.top().p;       q.pop();       q.push(Poly(merge(F, G)));     }     return q.top().p;   }   vector<long long> solve(long long l, long long r) { return div_ntt(l, r); }   }  // namespace NTT   signed main() {     scanf( %lld , &n);     for (long long i = 1; i < n; ++i) {       long long x, y;       scanf( %lld%lld , &x, &y);       ++deg[x], ++deg[y];     }     for (long long i = 2; i <= n; ++i) --deg[i];     for (long long i = 1; i <= n; ++i) {       vector<long long> t;       t.push_back(1ll);       if (deg[i]) t.push_back(deg[i]);       NTT::p.push_back(t);     }     vector<long long> F = NTT::solve();     long long ans = 0, flag = 1;     fac[0] = 1;     for (long long i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % MOD;     for (long long i = 0; i < n && i < F.size(); ++i) {       ans = (ans + flag * F[i] * fac[n - i] % MOD) % MOD;       flag = -flag;     }     printf( %lld n , (ans + MOD) % MOD);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 5, inf = 998244353;   long long n, k, i;   long long A[N], B[N];   long long in[N], f[N];   inline long long expo(long long n, long long k, long long p = inf) {     long long r = 1;     for (; k; k >>= 1) {       if (k & 1) r = r * n % p;       n = n * n % p;     }     return r;   }   inline long long inv(long long a, long long p = inf) {     return expo(a, p - 2, p);   }   vector<long long> BIT(N);   void upd(long long first, long long val) {     for (; first <= n; first += (first & -first)) BIT[first] += val;   }   long long q(long long first) {     long long res = 0;     for (; first > 0; first -= (first & -first)) res += BIT[first];     return res;   }   long long m[N];   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long i, j, first, second, ans = 0, c = 0, a;     cin >> n;     for (i = 1; i < n + 1; i++) {       cin >> A[i];       if (A[i] != -1) {         m[A[i]] = 1;         ans += c - q(A[i] - 1);         upd(A[i], 1);         c++;       }     }     vector<long long> v;     for (i = 1; i < n + 1; i++)       if (!m[i]) v.emplace_back(i);     ans += 1LL * (long long)v.size() * ((long long)v.size() - 1) % inf * inv(4) %            inf;     long long t = 0;     c = 0;     for (i = 1; i < n + 1; i++) {       if (A[i] != -1) {         a = lower_bound(v.begin(), v.end(), A[i]) - v.begin();         ans +=             c * ((long long)v.size() - a) % inf * inv((long long)v.size()) % inf;         ans +=             a * ((long long)v.size() - c) % inf * inv((long long)v.size()) % inf;       } else         c++;     }     cout << ans % inf;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 100 * 1000 + 10;   int n, m, l[Maxn], ans1, ans2;   long double a, b;   pair<long double, int> arr[Maxn], brr[Maxn];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m >> a >> b;     for (int i = 0; i < n; i++) {       cin >> arr[i].first;       arr[i].second = i;     }     for (int i = 0; i < m; i++) {       cin >> brr[i].first;       brr[i].second = i;     }     for (int i = 0; i < m; i++) cin >> l[i];     sort(arr, arr + n);     sort(brr, brr + m);     int cup;     long double Min = LLONG_MAX;     for (int i = 0; i < m; i++) {       long double p = (brr[i].first * a) / b;       int ind = lower_bound(arr, arr + n, make_pair(p, -1)) - arr;       long double c, dis;       if (ind < n) {         c = arr[ind].first,         dis = sqrt(a * a + c * c) +               sqrt((b - a) * (b - a) + (brr[i].first - c) * (brr[i].first - c));         cup = ind;       }       if (ind > 0) {         ind--;         c = arr[ind].first;         if (sqrt(a * a + c * c) + sqrt((b - a) * (b - a) +                                        (brr[i].first - c) * (brr[i].first - c)) <             dis) {           cup = ind;           dis = sqrt(a * a + c * c) +                 sqrt((b - a) * (b - a) + (brr[i].first - c) * (brr[i].first - c));         }       }       if (dis + l[brr[i].second] < Min) {         Min = dis + l[brr[i].second];         ans1 = arr[cup].second + 1, ans2 = brr[i].second + 1;       }     }     cout << ans1 <<     << ans2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   unsigned long long h[100010], bin[100010];   inline unsigned long long hsh(const int l, const int r) {     return h[r] - h[l - 1] * bin[r - l + 1];   }   inline int find(const int l1, const int r1, const int l2, const int r2) {     int l = 0, r = r1 - l1 + 1;     while (l < r) {       const int mid = l + r + 1 >> 1;       if (hsh(l1, l1 + mid - 1) == hsh(l2, l2 + mid - 1))         l = mid;       else         r = mid - 1;     }     return l + 1;   }   char s[100010];   int n, pre[100010][26], suf[100010][26], cnt[100010][26];   unordered_map<int, int> gy[100010];   long long det[26][100010], sum[100010];   int main() {     scanf( %s , s + 1);     n = strlen(s + 1);     for (int i = bin[0] = 1; i <= n; i++)       bin[i] = bin[i - 1] * 131, h[i] = h[i - 1] * 131 + s[i], s[i] -=  a ;     for (int i = 1; i <= n; i++) {       for (int j = 0; j < 26; j++)         pre[i][j] = pre[i - 1][j], cnt[i][j] = cnt[i - 1][j];       if (i != 1) pre[i][s[i - 1]] = i - 1;       cnt[i][s[i]]++;     }     for (int i = 0; i < 26; i++) suf[n + 1][i] = n + 1;     for (int i = n; i; i--) {       for (int j = 0; j < 26; j++) suf[i][j] = suf[i + 1][j];       if (i != n) suf[i][s[i + 1]] = i + 1;     }     long long lst = 0;     for (int i = 1; i <= n; i++) {       const int L = min(i - pre[i][s[i]], suf[i][s[i]] - i);       for (int j = 0; j < L; j = j << 1 | 1)         if ((!j || gy[i - j].count(i - 1)) &&             hsh(i - j, i - 1) == hsh(i + 1, i + j)) {           const long long tmp = 1ll * (j << 1 | 1) * (j << 1 | 1);           lst += tmp, gy[i - j][i + j] = 1;           sum[i - j] -= tmp;           sum[i] += tmp;           sum[i + 1] -= tmp;           sum[i + j + 1] += tmp;         }       for (int ch = 0; ch < 26; ch++) {         const int nL = min(i - pre[i][ch], suf[i][ch] - i);         for (int j = 0; j <= min(i - 1, n - i); j = j << 1 | 1)           if ((!j || gy[i - j].count(i - 1)) &&               hsh(i - j, i - 1) == hsh(i + 1, i + j))             if (j >= nL && j < L)               det[ch][i] -= 1ll * (j << 1 | 1) * (j << 1 | 1);             else if (j >= L && j < nL)               det[ch][i] += 1ll * (j << 1 | 1) * (j << 1 | 1);       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= min(i - 1, n - i); j = j << 1 | 1)         if ((gy[i - j].count(i - 1) || gy[i + 1].count(i + j)) &&             hsh(i - j, i - 1) != hsh(i + 1, i + j)) {           const int p = find(i - j, i - 1, i + 1, i + j);           if (p <= j && hsh(i - j + p, i - 1) == hsh(i + 1 + p, i + j)) {             if (gy[i - j].count(i - 1) &&                 cnt[i + j][s[i]] - cnt[i - j - 1][s[i]] - (s[i + p] == s[i]) == 1)               det[s[i - j + p - 1]][i + p] += 1ll * (j << 1 | 1) * (j << 1 | 1);             if (gy[i + 1].count(i + j) && cnt[i + j][s[i]] -                                                   cnt[i - j - 1][s[i]] -                                                   (s[i - j + p - 1] == s[i]) ==                                               1)               det[s[i + p]][i - j + p - 1] += 1ll * (j << 1 | 1) * (j << 1 | 1);           }         }     }     long long ans = 0;     for (int i = 1; i <= n; i++) sum[i] += sum[i - 1];     for (int i = 1; i <= n; i++)       for (int j = 0; j < 26; j++)         ans = max(ans, lst + det[j][i] + (j != s[i] ? sum[i] : 0));     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 4e5 + 5;   struct edge {     int v, nx;   } e[Maxn << 1];   int n, m, ne = 1, f[Maxn], dfn[Maxn], low[Maxn], scc[Maxn];   int rt, tot, cnt, top, st[Maxn];   int ans, cu[Maxn], cv[Maxn], nx[Maxn], ansu[Maxn], ansv[Maxn];   bool vis[Maxn];   void read(int u, int v) {     e[++ne].v = v;     e[ne].nx = f[u];     f[u] = ne;   }   void Tarjan(int u, int fa) {     dfn[u] = low[u] = ++tot;     vis[u] = 1;     st[++top] = u;     for (int i = f[u]; i; i = e[i].nx) {       int v = e[i].v;       if (v == fa) continue;       if (!dfn[v]) {         Tarjan(v, u);         low[u] = min(low[u], low[v]);       } else if (vis[v])         low[u] = min(low[u], low[v]);     }     if (dfn[u] == low[u]) {       cnt++;       int x, sz = 0;       do {         x = st[top];         top--;         sz++;         scc[x] = cnt;         vis[x] = 0;       } while (x != u);       if (sz > ans) ans = sz, rt = u;     }   }   void dfs2(int u) {     vis[u] = 1;     for (int i = f[u]; i; i = e[i].nx) {       int v = e[i].v;       if (vis[v]) {         ansu[i >> 1] = u;         ansv[i >> 1] = v;         continue;       }       if (low[v] != low[u]) {         ansu[i >> 1] = v;         ansv[i >> 1] = u;       } else {         ansu[i >> 1] = u;         ansv[i >> 1] = v;       }       dfs2(v);     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       int u, v;       scanf( %d%d , &u, &v);       read(u, v);       read(v, u);     }     Tarjan(1, 0);     ne = 0;     printf( %d n , ans);     dfs2(rt);     for (int i = 1; i <= m; i++) printf( %d %d n , ansu[i], ansv[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void Solve() {     string s;     cin >> s;     int n = s.size();     int cnt1 = 0, cnt2 = 0;     for (int i = 0; i < n - 1; i++) {       string t = s.substr(i, 2);       if (t ==  ab ) {         cnt1++;       } else if (t ==  ba ) {         cnt2++;       }     }     if (cnt1 == cnt2) {       cout << s <<   n ;       return;     }     if (cnt1 > cnt2) {       for (int i = n - 1; i >= 0; i--) {         if (s[i] ==  b ) {           s[i] =  a ;           break;         }       }     } else {       for (int i = n - 1; i >= 0; i--) {         if (s[i] ==  a ) {           s[i] =  b ;           break;         }       }     }     cout << s <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int TC = 1;     cin >> TC;     while (TC--) Solve();   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100 * 1000 + 5;   int a[MAXN], b[MAXN], c[MAXN], d[MAXN], k[MAXN], ans[MAXN];   pair<int, pair<char, int> > p[3 * MAXN];   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i] >> b[i];       p[i] = make_pair(a[i], make_pair(1, i));     }     int m;     cin >> m;     for (int i = 0; i < m; i++) {       cin >> c[i] >> d[i] >> k[i];       p[n + 2 * i] = make_pair(c[i], make_pair(0, i));       p[n + 2 * i + 1] = make_pair(d[i], make_pair(2, i));     }     sort(p, p + n + 2 * m);     set<pair<int, int> > st;     for (int i = 0; i < n + 2 * m; i++) {       int id = p[i].second.second;       if (p[i].second.first == 0)         st.insert(make_pair(d[id], id));       else if (p[i].second.first == 2)         st.erase(make_pair(d[id], id));       else {         set<pair<int, int> >::iterator it = st.lower_bound(make_pair(b[id], -1));         if (it == st.end()) {           cout <<  NO n ;           return 0;         }         int segId = it->second;         ans[id] = segId;         k[segId]--;         if (!k[segId]) st.erase(it);       }     }     cout <<  YES n ;     for (int i = 0; i < n; i++) cout << ans[i] + 1 <<    ;     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, l[103], a[103];   int num[103];   vector<int> v;   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) scanf( %d , &l[i]);     for (int i = 1; i < m; i++) {       int t = l[i + 1];       while (t <= l[i]) t += n;       if (a[l[i]] && a[l[i]] != t - l[i]) {         printf( -1 n );         return 0;       }       if (!a[l[i]]) {         a[l[i]] = t - l[i];         num[a[l[i]]]++;       }     }     for (int i = 1; i <= n; i++)       if (num[i] > 1) {         printf( -1 n );         return 0;       } else if (!num[i])         v.push_back(i);     for (int i = 1; i <= n; i++) {       if (!a[i]) a[i] = v.back(), v.pop_back();       printf( %d  , a[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   int x[maxn], y[maxn];   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> x[i];     }     for (int i = 1; i <= m; i++) {       cin >> y[i];     }     int u = 0, d = 0, ans = 0;     int sumu = 0, sumd = 0;     while (u <= n && d <= m) {       if (sumu && sumu == sumd) {         ans++;         sumd = y[++d];         sumu = x[++u];       } else if (sumu > sumd) {         sumd += y[++d];       } else if (sumu <= sumd) {         sumu += x[++u];       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string lower(string second) {     for (int i = 0; i < second.size(); i++)       if (second[i] >=  A  && second[i] <=  Z ) second[i] = second[i] -  A  +  a ;     return second;   }   char reg(char c, char t) {     if (c >=  A  && c <=  Z ) return t -  a  +  A ;     return t;   }   string second, a[105], sl;   bool u[105];   int n;   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       a[i] = lower(a[i]);     }     char let;     cin >> second >> let;     sl = lower(second);     for (int i = 0; i < second.size(); i++)       for (int j = 0; j < n; j++)         if (i + a[j].size() <= second.size() &&             sl.substr(i, a[j].size()) == a[j]) {           for (int k = i; k < i + a[j].size(); k++) u[k] = true;         }     for (int i = 0; i < second.size(); i++)       if (u[i]) {         if (sl[i] == let) {           if (sl[i] ==  a )             second[i] = reg(second[i],  b );           else             second[i] = reg(second[i],  a );         } else           second[i] = reg(second[i], let);       }     cout << second;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, i = 0, j = 0, x, t, w, p, q, l, r, count1 = 0, count2 = 0,                   k = 0, first = 0, second = 0, last, sum = 1e9;   const long long mod = 1000003, num = 0;   bool compare(pair<string, int> a, pair<string, int> b) {     if (a.second == b.second)       return a.first < b.first;     else       return a.second > b.second;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     vector<vector<long long>> v(n, vector<long long>(n, 0));     long long val = 1;     for (i = 0; i < n / 2; i++) {       for (j = 0; j < n; j++) {         v[j][i] = val;         val += 1;       }     }     for (i = n / 2; i < n; i++) {       for (long long j = n - 1; j >= 0; j--) {         v[j][i] = val;         val += 1;       }     }     for (i = 0; i < n; i++) {       for (j = 0; j < n; j++) cout << v[i][j] <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, t;     cin >> n >> t;     if (t == 10) {       if (n == 1)         cout << -1 << endl;       else {         for (int i = 0; i < n - 1; i++) {           cout << 1;         }         cout << 0 << endl;       }     } else {       for (int i = 0; i < n; i++) {         cout << t;       }       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   void doRoutine() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     srand(322179);   }   signed main() {     doRoutine();     long long n;     cin >> n;     long long res = 0;     while (n > 0) {       ++res;       n /= 2;     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     char input[n + 5][n + 5];     for (int i = 0; i < n; i++) scanf( %s , input[i]);     int row[n], col[n], count_row = 0, count_col = 0;     for (int i = 0; i < n; i++) row[i] = -1;     for (int j = 0; j < n; j++) col[j] = -1;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) {         if (input[i][j] ==  . ) {           row[i] = j;           col[j] = i;         }       }     for (int i = 0; i < n; i++) {       if (row[i] != -1) count_row++;       if (col[i] != -1) count_col++;     }     if (count_row != n && count_col != n)       printf( -1 n );     else if (count_row == n) {       for (int i = 0; i < n; i++) printf( %d %d n , i + 1, row[i] + 1);     } else       for (int j = 0; j < n; j++) printf( %d %d n , col[j] + 1, j + 1);   }


#include <bits/stdc++.h>   using namespace std;   typedef long long ll;   const int N=4e2+2,p=998244353;   vector<int> lj[N];   int dis[N][N],dl[N],ans[N][N],rd[N][N],cnt[N],pre[N][N],lb[N*3][2];   int n,m,s,t,i,j,tou,wei,x,y,r;   bool ed[N];   int main()   {    scanf( %d%d ,&n,&m);    for (i=1;i<=m;i++) scanf( %d%d ,&x,&y),lj[lb[i][0]=x].push_back(lb[i][1]=y),lj[y].push_back(x);    for (i=1;i<=m;i++) lb[i+m][0]=lb[i][1],lb[i+m][1]=lb[i][0];m<<=1;    for (s=1;s<=n;s++)    {     dis[s][dl[tou=wei=1]=s]=rd[s][s]=1;     while (tou<=wei) for (auto v:lj[x=dl[tou++]]) if (!dis[s][v]) dis[s][dl[++wei]=v]=dis[s][pre[s][v]=x]+(rd[s][v]=1); else if (dis[s][v]==dis[s][x]+1) ++rd[s][v];    }    for (s=1;s<=n;s++)    {     for (t=1;t<s;t++) printf( %d  ,ans[s][t]);     //for (i=1;i<=n;i++) printf( %d: pre%d dis%d rd%d n ,i,pre[i],dis[i],rd[i]);     for (t=s;t<=n;t++)     {      for (x=t;x!=s&&rd[s][x]<=1;x=pre[s][x]);      if (x!=s) {printf( 0  );continue;}      for (x=t;x!=s;x=pre[s][x]) ed[x]=1;r=1;      for (i=1;i<=m;i++) if (dis[s][lb[i][0]]+1==dis[s][lb[i][1]]&&dis[t][lb[i][0]]+1==dis[t][lb[i][1]]) ++cnt[lb[i][1]];      //if (s==1&&t==1) for (i=1;i<=n;i++) printf( %d%c ,cnt[i],i==n?10:32);      for (i=1;i<=n;i++) if (!ed[i]&&i!=s) r=(ll)r*cnt[i]%p;      memset(cnt,0,sizeof(cnt));      for (x=t;x!=s;x=pre[s][x]) ed[x]=0;      ans[t][s]=r;printf( %d  ,r);     }puts(  );    }   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[1001], b[1001], c[1001], d[1001];   void solve() {     cin >> n;     int r = 1;     int i = 1;     cin >> a[i] >> b[i] >> c[i] >> d[i];     int sum = a[1] + b[1] + c[1] + d[1];     for (int i = 2; i <= n; i++) {       cin >> a[i] >> b[i] >> c[i] >> d[i];       int sum1 = a[i] + b[i] + c[i] + d[i];       if (sum1 > sum) {         r += 1;       }     }     cout << r;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     if (fopen( input.txt ,  r )) {       freopen( input.txt ,  r , stdin);       freopen( output.txt ,  w , stdout);     }     int t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, n, a[100];     cin >> t;     while (t--) {       cin >> n;       for (int i = 0; i < n; i++) {         cin >> a[i];       }       sort(a, a + n);       vector<int> v;       int dif = 0;       for (int i = 1; i < n; i++) {         dif = a[i] - a[i - 1];         v.push_back(dif);       }       sort(v.begin(), v.end());       cout << v[0] << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma optimize( Ofast )   using namespace std;   void solve() {     int n, k;     string s;     cin >> n >> k >> s;     vector<int> cnt(27);     for (char &c : s) {       cnt[c -  a ]++;     }     int t = 0;     for (int d = 1; d <= n; d++) {       vector<int> ncnt = cnt;       int curr = 0;       for (int j = 0; j < 26; j++) {         if (ncnt[j] >= d) {           ncnt[j] -= d;           curr++;           --j;         }         if (curr && k % curr == 0) {           t = max(t, curr * d);         }       }     }     cout << t <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     int q;     cin >> q;     while (q--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     cin >> a >> b;     while (b) {       int c = b % a;       if (c == 0)         b /= a;       else if (c == 1)         b--;       else if (c == a - 1)         b++;       else         return cout <<  NO , 0;     }     cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> nums(n);       vector<vector<int> > pos(n);       for (int i = 0; i < (int)n; i++) {         cin >> nums[i];         nums[i] -= 1;         pos[nums[i]].push_back(i);       }       vector<vector<int> > dp(n + 1, vector<int>(n));       for (int i = n - 1; i >= 0; i--) {         for (int j = i + 1; j < (int)n; j++) {           dp[i][j] = (dp[i + 1][j] + (nums[i] != nums[j]));           for (auto k : pos[nums[i]]) {             if (k > j) break;             if (k < i) continue;             int temp = dp[i][k] + 1 + dp[k + 1][j];             dp[i][j] = min(dp[i][j], temp);           }         }       }       cout << dp[0][n - 1] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, x;   int n;   int main() {     scanf( %d , &n);     scanf( %lld%lld , &a, &b);     while (n--) {       scanf( %lld , &x);       printf( %lld  , ((x * a) - b * ((x * a) / b)) / a);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10, logN = 20;   const int M = 350;   int d[N], h[N], floorlog[N << 1];   long long D[N], F1[N], F2[N], lft[N], rgt[N], total = 0;   map<pair<int, int>, long long> st;   vector<pair<int, int> > queries, q;   pair<int, int> rmq1[logN][N], rmq2[logN][N];   int max(int i, int j, long long f[]) { return f[i] > f[j] ? i : j; }   pair<int, int> merge(pair<int, int>& x, pair<int, int>& y, long long f[]) {     if (f[x.first] > f[y.first]) {       return {x.first, max(x.second, y.first, f)};     }     if (x.first == y.first) {       return {y.first, max(x.second, y.second, f)};     }     return {y.first, max(x.first, y.second, f)};   }   void make_rmq(long long f[], pair<int, int> rmq[][N], int n) {     for (int i = n; i >= 1; i--) {       rmq[0][i] = {i, 0};       for (int j = 1; i + (1 << j) - 1 <= n; j++) {         rmq[j][i] = merge(rmq[j - 1][i], rmq[j - 1][i + (1 << (j - 1))], f);       }     }   }   pair<int, int> get(int i, int j, pair<int, int> rmq[][N], long long f[]) {     int k = floorlog[j - i + 1];     return merge(rmq[k][i], rmq[k][j - (1 << k) + 1], f);   }   long long query(int l, int r) {     pair<int, int> P = get(l, r, rmq1, F1);     pair<int, int> Q = get(l, r, rmq2, F2);     long long mx = 0;     if (P.first != Q.first) mx = max(mx, F1[P.first] + F2[Q.first]);     if (P.second != Q.first) mx = max(mx, F1[P.second] + F2[Q.first]);     if (P.first != Q.second) mx = max(mx, F1[P.first] + F2[Q.second]);     if (P.second != Q.second) mx = max(mx, F1[P.second] + F2[Q.second]);     return mx;   }   int main() {     for (int i = 0; (1 << i) < N; i++)       for (int j = (1 << i); j < (1 << (i + 1)); j++) floorlog[j] = i;     int n, m;     scanf( %d , &(n));     scanf( %d , &(m));     for (int i = 1; i <= n; i++) {       D[i] = D[i - 1] + d[i - 1];       scanf( %d , &(d[i]));       total += d[i];     }     for (int i = 1; i <= n; i++) {       scanf( %d , &(h[i]));       F1[i] = D[i] + 2 * h[i];       F2[i] = -D[i] + 2 * h[i];       rgt[i] = max(rgt[i - 1], D[i] + 2 * h[i]);     }     for (int i = n; i >= 1; i--) {       lft[i] = max(lft[i + 1], total - D[i] + 2 * h[i]);     }     F1[0] = -1e18;     F2[0] = -1e18;     make_rmq(F1, rmq1, n);     make_rmq(F2, rmq2, n);     for (int i = 1; i <= m; i++) {       int a, b;       scanf( %d , &(a));       scanf( %d , &(b));       q.push_back({a, b});       if (a > b) {         int l = b + 1, r = a - 1;         printf( %lld n , query(l, r));       } else {         long long ans = 0;         if (b != n && a != 1) ans = max(ans, lft[b + 1] + rgt[a - 1]);         if (b != n) ans = max(ans, query(b + 1, n));         if (a != 1) ans = max(ans, query(1, a - 1));         printf( %lld n , ans);       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<int> v(4);     int total = 0;     for (int i = 0; i < 4; i++) {       scanf( %d , &v[i]);       total += v[i];     }     sort(v.begin(), v.end());     do {       int sum = 0;       for (int i = 0; i < 4; i++) {         sum += v[i];         if (2 * sum == total) {           cout <<  YES ;           return 0;         }       }     } while (next_permutation(v.begin(), v.end()));     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class c>   struct rge {     c b, e;   };   template <class c>   rge<c> range(c i, c j) {     return rge<c>{i, j};   }   template <class c>   auto dud(c* x) -> decltype(cerr << *x, 0);   template <class c>   char dud(...);   struct debug {     template <class c>     debug& operator<<(const c&) {       return *this;     }   };   const long long int N = 1e5 + 5;   const long long int INF = 1e10;   long long int mod = 998244353;   long long int dx[4] = {0, 0, +1, -1};   long long int dy[4] = {+1, -1, 0, 0};   long long int n;   void solve() {     cin >> n;     long long int c1 = 0, c2 = 0;     for (long long int i = 0; i < n; i++) {       long long int x;       cin >> x;       (x == 1 ? c1++ : c2++);     }     long long int f = 0;     for (long long int i = 0; i < n; i++) {       if (f == 0 and c2 > 0 and c1 > 0) {         cout << 2 <<     << 1 <<    ;         c2--, c1--;         f = 1;       } else if (c2 > 0) {         cout << 2 <<    ;         c2--;       } else if (c1 > 0) {         cout << 1 <<    ;         c1--;       }     }   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long int t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e5;   int ans[N], all, u, v, n, m, num, color[N], cnt[2];   vector<int> adj[N], radj[N];   bool used[N];   void output() {     printf( YES n );     for (int i = 0; i < n; ++i) printf( %d%c , ans[i] + 1,    n [i == n - 1]);     exit(0);   }   void dfs(int v, int c) {     color[v] = c, cnt[c]++;     for (int j = 0; j < ((int)(adj[v]).size()); ++j)       if (color[adj[v][j]] == -1) dfs(adj[v][j], c ^ 1);   }   void truecolor() {     int deg = 0;     for (int t = 0; t < 2; ++t) {       for (int i = 0; i < n; ++i)         if (color[i] == t) {           ans[i] = num;           ++deg;           if (deg == 3) {             ++num;             deg = 0;           }         }     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m; ++i)       scanf( %d%d , &u, &v), --u, --v, adj[u].push_back(v), adj[v].push_back(u);     memset(color, -1, sizeof(color));     for (int i = 0; i < n; ++i)       if (color[i] == -1) dfs(i, 0);     if (cnt[0] % 3 > cnt[1] % 3) {       for (int i = 0; i < n; ++i) color[i] ^= 1;       swap(cnt[0], cnt[1]);     }     all = 0;     for (int i = 0; i < n; ++i)       if (color[i] == 1) all ^= i;     num = 0;     if (cnt[0] % 3 == 0) {       truecolor();       output();     }     for (int i = 0; i < n; ++i)       if (color[i] == 0)         if (((int)(adj[i]).size()) <= cnt[1] - 2) {           ans[i] = 0, color[i] = 2;           for (int j = 0; j < ((int)(adj[i]).size()); ++j) used[adj[i][j]] = true;           int deg = 2;           for (int j = 0; deg && (j < n); ++j)             if ((color[j] == 1) && !used[j]) {               color[j] = 2;               ans[j] = 0;               --deg;             }           ++num;           truecolor();           output();         } else if (((int)(adj[i]).size()) == cnt[1] - 1) {           int x = all;           for (int j = 0; j < ((int)(adj[i]).size()); ++j) x ^= adj[i][j];           radj[x].push_back(i);         }     int res = 0;     for (int i = 0; i < n; ++i)       if (((int)(radj[i]).size()) >= 2) res++;     if (res <= 1) {       printf( NO n );       return 0;     }     res = 2;     for (int i = 0; res && (i < n); ++i)       if (((int)(radj[i]).size()) >= 2) {         ans[i] = num, color[i] = 2;         for (int j = 0; j < 2; ++j) ans[radj[i][j]] = num, color[radj[i][j]] = 2;         --res;         ++num;       }     truecolor();     output();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   const int mod = 1e9 + 7;   const int inf = 0x3f3f3f3f;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int ans = 0, a, b, c, d, e, f;     cin >> a >> b >> c >> d >> e >> f;     if (e < f) {       int now = min(d, min(b, c));       ans += now * f;       b -= now;       c -= now;       d -= now;     } else {       int now = min(d, a);       ans += now * e;       a -= now;       d -= now;     }     ans += e * min(a, d);     ans += f * min(d, min(b, c));     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int INF = (long long int)1e9 + 10;   const long long int INFLL = (long long int)1e18 + 10;   const long double EPS = 1e-8;   const long long int MOD = 1e9 + 7;   template <class T>   T &chmin(T &a, const T &b) {     return a = min(a, b);   }   template <class T>   T &chmax(T &a, const T &b) {     return a = max(a, b);   }   template <class T>   inline T sq(T a) {     return a * a;   }   int n, m;   bool f[128][2];   int memo[128][128][3][3];   bool isin(int a, int b) { return 0 <= a and a < n and 0 <= b and b < 2; }   vector<pair<int, int> > cg;   void draw(int a, int b) {     if (isin(a, b) and !f[a][b]) {       cg.emplace_back(a, b);       f[a][b] = true;     }   }   void put(int a, int b) {     cg.clear();     draw(a, b);     draw(a - 1, 1 - b);     draw(a, 1 - b);     draw(a + 1, 1 - b);   }   int mex(set<int> &x) {     for (long long int i = (0); i < (long long int)(INF); i++)       if (x.find(i) == x.end()) return i;   }   int grandy(int t, int b, int tf, int bf) {     int &res = memo[t][b][tf][bf];     if (res != -1) return res;     set<int> gs;     for (long long int i = (t); i < (long long int)(b); i++)       for (long long int j = (0); j < (long long int)(2); j++)         if (!f[i][j]) {           put(i, j);           vector<pair<int, int> > ccg = cg;           int x = grandy(t, i, tf, j) ^ grandy(i + 1, b, j, bf);           gs.insert(x);           for (auto &w : ccg) f[w.first][w.second] = false;         }     return res = mex(gs);   }   int main() {     scanf( %d %d , &n, &m);     for (long long int i = (0); i < (long long int)(m); i++) {       int a, b;       scanf( %d %d , &a, &b);       a--;       b--;       put(a, b);     }     for (long long int i = (0); i < (long long int)(128); i++)       for (long long int j = (0); j < (long long int)(128); j++)         for (long long int k = (0); k < (long long int)(3); k++)           for (long long int l = (0); l < (long long int)(3); l++)             memo[i][j][k][l] = -1;     if (grandy(0, n, 2, 2) == 0)       puts( LOSE );     else       puts( WIN );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int m, nn, o, p;   int main() {     long long int n;     cin >> n;     string a, b;     cin >> a >> b;     for (int i = 0; i < n; i++)       if (a[i] ==  0 ) {         if (b[i] ==  0 )           m++;         else           nn++;       } else {         if (b[i] ==  0 )           o++;         else           p++;       }     cout << m * p + m * o + nn * o << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, tms;   void upd(int &x, int y) { x = min(x, y); }   struct trietree {     int node[505][12];     int sum[505];     int dp[505][505][12];     int dp2[505][505][12][12];     bool vis[505][505][15];     int dep[505];     int tot;     void insert(char ch[], int val) {       int now = 0;       int l = strlen(ch);       for (int i = 0; i < l; i++) {         int rt = ch[i] -  0 ;         if (node[now][rt] == 0) node[now][rt] = ++tot, dep[tot] = dep[now] + 1;         now = node[now][rt];       }       sum[now] += val;     }     void dfs(int x, int p, int lft) {       if (vis[x][p][lft]) return;       vis[x][p][lft] = 1;       if (lft > 0) dfs(x, x, lft - 1), upd(dp[x][p][lft], dp[x][x][lft - 1]);       vector<int> ch;       for (int i = 0; i < 10; i++)         if (node[x][i]) ch.push_back(node[x][i]);       dp2[x][p][lft][ch.size()] = 0;       for (int i = ch.size() - 1; i >= 0; i--) {         int t = ch[i];         for (int j = 0; j <= lft; j++) {           dfs(t, p, j);           upd(dp2[x][p][lft][i], dp2[x][p][lft - j][i + 1] + dp[t][p][j]);         }       }       upd(dp[x][p][lft], dp2[x][p][lft][0] + (dep[x] - dep[p]) * sum[x]);     }     int solve() {       memset(dp, 0x3f, sizeof(dp));       memset(dp2, 0x3f, sizeof(dp2));       dfs(0, 0, tms);       return dp[0][0][tms];     }   } trie;   char buf[1008];   int main() {     cin >> n >> tms;     for (int i = 0; i < n; i++) {       scanf( %s , buf);       int gg;       cin >> gg;       trie.insert(buf, gg);     }     cout << trie.solve();     return 0;   }


#include <bits/stdc++.h>   int main(void) {     char a[3][3];     scanf( %3s , a[0]);     scanf( %3s , a[1]);     scanf( %3s , a[2]);     for (int i = 0; i < 3; i++)       for (int j = 0; j < 3; j++)         if (a[i][j] != a[((((i)-1) * -1) + 1)][((((j)-1) * -1) + 1)]) {           puts( NO );           return EXIT_SUCCESS;         }     puts( YES );     return EXIT_SUCCESS;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 100005;   const int Maxm = 500;   int T;   int n, m, k;   vector<int> un;   int my[Maxn];   bool has[Maxm][Maxm];   set<int> neigh[Maxn];   void Solve(priority_queue<pair<int, int> > &Q) {     bool assigned = false;     int inside = n;     while (!Q.empty() && -Q.top().first < k) {       int v = Q.top().second, d = -Q.top().first;       Q.pop();       if (neigh[v].size() != d) continue;       if (neigh[v].size() == k - 1) {         if (!assigned && inside < Maxm) {           int pnt = 0;           for (int i = 1; i <= n; i++)             if (!neigh[i].empty()) my[i] = pnt++;           for (int i = 0; i < pnt; i++) fill(has[i], has[i] + pnt, false);           for (int i = 1; i <= n; i++)             for (auto u : neigh[i]) has[my[i]][my[u]] = true;           assigned = true;         }         vector<int> seq;         for (auto u : neigh[v]) seq.push_back(u);         bool ok = true;         for (int i = 0; i < seq.size() && ok; i++)           for (int j = i + 1; j < seq.size() && ok; j++)             if (assigned)               ok = has[my[seq[i]]][my[seq[j]]];             else               ok = neigh[seq[i]].find(seq[j]) != neigh[seq[i]].end();         if (ok) {           printf( 2 n );           printf( %d , v);           for (int i = 0; i < seq.size(); i++) printf(  %d , seq[i]);           printf(  n );           return;         }       }       for (auto u : neigh[v]) {         neigh[u].erase(v);         if (assigned) has[my[v]][my[u]] = has[my[u]][my[v]] = false;         Q.push(pair<int, int>(-int(neigh[u].size()), u));       }       neigh[v].clear();       inside--;     }     vector<int> seq;     while (!Q.empty()) {       int v = Q.top().second, d = -Q.top().first;       Q.pop();       if (neigh[v].size() != d) continue;       seq.push_back(v);     }     if (seq.empty())       printf( -1 n );     else {       printf( 1 %d n , int(seq.size()));       for (int i = 0; i < seq.size(); i++)         printf( %d%c , seq[i], i + 1 < seq.size() ?     :   n );     }   }   int main() {     scanf( %d , &T);     while (T--) {       scanf( %d %d %d , &n, &m, &k);       for (int i = 1; i <= n; i++) neigh[i].clear();       for (int i = 0; i < m; i++) {         int a, b;         scanf( %d %d , &a, &b);         neigh[a].insert(b);         neigh[b].insert(a);       }       priority_queue<pair<int, int> > Q;       for (int i = 1; i <= n; i++)         Q.push(pair<int, int>(-int(neigh[i].size()), i));       Solve(Q);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = (1 << 20) + 5;   bool used[MAXN];   vector<int> adj[MAXN];   int a[MAXN];   int n;   vector<int> ans;   vector<int>::iterator it[MAXN];   void dfs(int x) {     while (it[x] != adj[x].end()) {       int p = *(it[x]);       if (!used[p / 2]) {         int p = *(it[x]);         it[x]++;         used[p / 2] = true;         dfs(a[p ^ 1]);         ans.push_back(p);       } else {         it[x]++;       }     }   }   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = 0; i < 2 * n; i++) cin >> a[i];     for (int i = 0; i < 2 * n; i++) used[a[i]] = !used[a[i]];     int k;     for (k = 20; k >= 0; k--) {       bool good = true;       for (int i = 0; i < (1 << k); i++) {         if (used[i]) {           good = false;           break;         }       }       if (good) break;       for (int i = 0; i < (1 << (k - 1)); i++) {         used[i] = used[i] ^ used[i + (1 << (k - 1))];       }     }     for (; k >= 0; k--) {       for (int i = 0; i < n; i++) {         used[i] = false;       }       for (int i = 0; i < 2 * n; i++) {         a[i] = a[i] % (1 << k);       }       for (int i = 0; i < (1 << k); i++) adj[i].clear();       for (int i = 0; i < n; i++) {         adj[a[2 * i]].push_back(2 * i);         adj[a[2 * i + 1]].push_back(2 * i + 1);       }       for (int i = 0; i < (1 << k); i++) {         it[i] = adj[i].begin();       }       dfs(a[0]);       if (ans.size() == n) {         cout << k <<   n ;         for (int i = n - 1; i >= 0; i--) {           cout << 1 + ans[i] <<     << 1 + (ans[i] ^ 1) <<    ;         }         return 0;       }       ans.clear();     }   }


#include <bits/stdc++.h>   using namespace std;   const int M = 200 * 1000 + 11;   int main() {     int q;     cin >> q;     vector<int> pos(M);     int curl = 0;     int curr = 0;     for (int i = 0; i < q; ++i) {       string type;       int id;       cin >> type >> id;       if (i == 0) {         pos[id] = curl;         --curl;         ++curr;       } else {         if (type ==  L ) {           pos[id] = curl;           --curl;         } else if (type ==  R ) {           pos[id] = curr;           ++curr;         } else {           cout << min(abs(pos[id] - curl), abs(pos[id] - curr)) - 1 <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e7;   int n;   int m[100], r[100];   bool am[MAX];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) cin >> m[i];     for (int i = 1; i <= n; i++) cin >> r[i];     for (int i = 1; i <= n; i++) {       bool da = 0;       for (int k = 0; k * m[i] + r[i] < MAX; k++) am[k * m[i] + r[i]] = 1;     }     int cnt = 0;     for (int i = 1; i < MAX; i++) cnt += am[i];     cout << 1.00 * cnt / (MAX - 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long fast_pow(long long x, long long y, long long p);   void solve() {     long long a, b, c;     cin >> a >> b >> c;     long long ans = INT_MAX;     long long ansA = -1, ansB = -1, ansC = -1;     for (long long ta = 1; ta <= 2 * a; ta++) {       for (long long tb = ta; tb <= 2 * b; tb += ta) {         long long sum = abs(a - ta) + abs(b - tb);         if ((c % tb) < (tb - (c % tb))) {           sum += (c % tb);           if (sum < ans) {             ans = sum;             ansA = ta;             ansB = tb;             long long temp = c / tb;             ansC = temp * tb;           }         } else {           sum += (tb - (c % tb));           if (sum < ans) {             ans = sum;             ansA = ta;             ansB = tb;             long long temp = (c / tb) + 1;             ansC = temp * tb;           }         }       }     }     cout << ans <<   n ;     cout << ansA <<     << ansB <<     << ansC <<   n ;   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long test = 1;     cin >> test;     while (test--) {       solve();     }     return 0;   }   long long fast_pow(long long x, long long y, long long p) {     long long res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }


#include <bits/stdc++.h>   using namespace std;   int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   int n, t, k;   int a[200005], maxx, b[200005];   vector<int> v[200005];   int cnt = 1, tmp = 0;   int main() {     n = read();     t = read();     k = read();     for (int i = 1; i <= t; ++i) a[i] = read();     for (int i = t; i >= 1; --i) {       maxx += abs(min(0, a[i + 1] - a[i]));       if (i == t)         b[i] = a[i];       else         b[i] = b[i + 1] + a[i] - 1;     }     v[0].push_back(1);     if (k < maxx || k > n - t) {       printf( -1 n );       return 0;     } else {       printf( %d n , n);       for (int i = 1; i <= t; ++i) {         if (k < n - t - tmp) {           v[i].push_back(++cnt);           printf( %d %d n , v[i - 1][0], v[i][0]);           a[i]--;           for (int j = 1; j < v[i - 1].size() && a[i] && k < n - t - tmp; ++j) {             v[i].push_back(++cnt);             printf( %d %d n , v[i - 1][j], v[i][j]);             tmp++;             a[i]--;           }           if (a[i]) {             for (int j = 1; j <= a[i]; ++j) {               v[i].push_back(++cnt);               printf( %d %d n , cnt, v[i - 1][0]);             }           }         } else {           for (int j = 1; j <= a[i]; ++j) {             v[i].push_back(++cnt);             printf( %d %d n , cnt, v[i - 1][0]);           }         }       }     }   }


#include <bits/stdc++.h>   void sort(int a[], int begin, int mid, int end) {     int i = begin, j = mid + 1, k = 0, temp[end - begin + 1];     while (i <= mid && j <= end) {       if (a[i] <= a[j])         temp[k++] = a[i++];       else         temp[k++] = a[j++];     }     for (; i <= mid; ++i) temp[k++] = a[i];     for (; j <= end; ++j) temp[k++] = a[j];     for (i = 0; i < k; ++i) a[begin + i] = temp[i];   }   void partition(int a[], int begin, int end) {     if (begin == end) return;     int mid = (begin + end) / 2;     partition(a, begin, mid);     partition(a, mid + 1, end);     sort(a, begin, mid, end);   }   int main() {     int n;     scanf( %d , &n);     unsigned long long s = 0;     int i, a[n + 1];     for (i = 1; i <= n; ++i) scanf( %d , &a[i]);     partition(a, 1, n);     for (i = 1; i <= n - 1; ++i) s = s + (a[i] * 1LL) * (i + 1);     s = s + (a[n] * 1LL) * n;     printf( %I64d n , s);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   struct debugger {     static void call(string::iterator it, string::iterator ed) {}     template <typename T, typename... aT>     static void call(string::iterator it, string::iterator ed, T a, aT... rest) {       string b;       for (; *it !=  , ; ++it)         if (*it !=    ) b += *it;       cout << b <<  =  << a <<    ;       call(++it, ed, rest...);     }   };   int main() {     int n, R, r;     cin >> n >> R >> r;     if (n == 1 && r <= R) {       cout <<  YES n ;       return 0;     }     if (n == 2 && 2 * r <= R) {       cout <<  YES n ;       return 0;     }     double ans = asin((double)r / (double)(R - r));     if (ans * n <= pi + 1.0e-8) {       cout <<  YES n ;       return 0;     }     cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long Mxn = 1e6 + 7;   const long long Mod = 1e9 + 7;   const long long Inf = 1e16 + 7;   const long long Module = 998244353;   int N;   long long Ans, A[Mxn], B[Mxn], Val[Mxn];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> N;     for (int i = 1; i <= N; i++) {       cin >> A[i];     }     for (int i = 1; i <= N; i++) {       cin >> B[i];     }     sort(B + 1, B + N + 1);     for (int i = 1; i <= N; i++) {       Val[i] = (1LL * A[i] * i * (N - i + 1));     }     sort(Val + 1, Val + N + 1);     reverse(Val + 1, Val + N + 1);     for (int i = 1; i <= N; i++) {       Ans = (Ans + (1LL * B[i] * (Val[i] % Module)) % Module) % Module;     }     cout << Ans;     return false;   }