#include <bits/stdc++.h>   using namespace std;   int n;   int x[1024], y[1024], c[1024];   int pts[1024];   int chull[1024], tp;   int chull_size;   int ans_l[1024], ans_r[1024], ans_len = 0;   void add_edge(int u, int v) {     ans_l[ans_len] = u;     ans_r[ans_len] = v;     ans_len++;   }   void print_ans() {     printf( %d n , ans_len);     for (int i = 0; i < ans_len; i++) printf( %d %d n , ans_l[i], ans_r[i]);   }   bool ccw(int i, int j, int k) {     return (x[i] * y[j] + x[j] * y[k] + x[k] * y[i] - x[j] * y[i] - x[k] * y[j] -             x[i] * y[k]) > 0;   }   int pivot;   bool comp_by_pivot(int i, int j) {     assert(i != pivot);     assert(j != pivot);     return ccw(pivot, i, j);   }   void run(int i, int j, int k, const vector<int> &inner, bool sep) {     if (inner.size() == 0) {       if (sep) {         ;       } else {         add_edge(j, k);       }     } else {       int pivot = -1;       for (int p : inner) {         if (pivot == -1 || ccw(j, p, pivot)) pivot = p;       }       vector<int> j_inner, k_inner;       for (int p : inner)         if (p != pivot) {           if (ccw(pivot, i, p))             j_inner.push_back(p);           else             k_inner.push_back(p);         }       if (c[pivot] == c[i]) {         run(j, pivot, i, j_inner, false);         run(k, i, pivot, k_inner, true);         if (!sep) add_edge(j, k);       } else {         run(i, j, pivot, j_inner, false);         run(i, pivot, k, k_inner, sep);       }     }   }   void run(int i, int j, int k, bool sep) {     assert(i != j && j != k && k != i);     assert(ccw(i, j, k));     vector<int> inner;     for (int l = 0; l < n; l++)       if (l != i && l != j && l != k && ccw(i, j, l) && ccw(j, k, l) &&           ccw(k, i, l)) {         inner.push_back(l);       }     run(i, j, k, inner, sep);   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d %d %d , &x[i], &y[i], &c[i]);     }     if (n == 1) {       printf( 0 n );     } else if (n == 2) {       if (c[0] == c[1]) {         printf( 1 n );         printf( 0 1 n );       } else {         printf( 0 n );       }     } else {       pivot = 0;       for (int i = 1; i < n; i++)         if (x[i] < x[pivot] || (x[i] == x[pivot] && y[i] < y[pivot])) pivot = i;       for (int i = 0; i < n - 1; i++) pts[i] = (i < pivot ? i : i + 1);       sort(pts, pts + n - 1, comp_by_pivot);       chull[0] = pivot;       tp = 0;       for (int i = 0; i < n - 1; i++) {         int cur = pts[i];         while (tp >= 1 && (ccw(chull[tp - 1], chull[tp], cur) == false)) tp--;         chull[++tp] = cur;       }       chull_size = tp + 1;       int num_change = 0;       for (int i = 0; i < chull_size; i++)         if (c[chull[i]] != c[chull[(i + 1) % chull_size]]) num_change++;       if (num_change == 0) {         int c_chull = c[chull[0]];         int center_pivot = -1;         for (int i = 0; i < n; i++)           if (c[i] != c_chull) {             center_pivot = i;             break;           }         if (center_pivot == -1) {           printf( %d n , n - 1);           for (int i = 0; i < n - 1; i++) printf( %d %d n , pivot, pts[i]);         } else {           for (int i = 0; i < chull_size - 1; i++) {             run(center_pivot, chull[i], chull[i + 1], false);           }           run(center_pivot, chull[chull_size - 1], chull[0], true);           print_ans();         }       } else if (num_change == 2) {         int p1 = -1;         for (int i = 1; i < chull_size; i++)           if (c[chull[i - 1]] != c[chull[i]]) {             p1 = i;             break;           }         assert(p1 >= 1 && p1 < chull_size);         rotate(chull, chull + p1, chull + chull_size);         int p2 = -1;         for (int i = 1; i < chull_size; i++)           if (c[chull[i - 1]] != c[chull[i]]) {             p2 = i;             break;           }         for (int i = 0; i < p2 - 1; i++) {           run(chull[p2], chull[i], chull[i + 1], false);         }         for (int i = p2; i < chull_size - 1; i++) {           run(chull[0], chull[i], chull[i + 1], false);         }         print_ans();       } else {         assert(num_change % 2 != 1);         printf( Impossible n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   vector<int> p, h, st;   vector<vector<int>> adj;   int gst(int curr, int prev) {     int res = p[curr];     for (int next : adj[curr]) {       if (next != prev) {         res += gst(next, curr);       }     }     return st[curr] = res;   }   int dfs(int curr, int prev, int& res) {     int g = st[curr] + h[curr];     if (g % 2) res = 0;     int gv = g / 2;     if (gv < 0 || gv > st[curr]) res = 0;     int cgv = 0;     for (int next : adj[curr]) {       if (next != prev) {         cgv += dfs(next, curr, res);       }     }     if (cgv > gv) res = 0;     return gv;   }   string solve() {     cin >> n >> m;     adj.clear();     adj.resize(n + 1);     p.assign(n + 1, 0);     h.assign(n + 1, 0);     st.assign(n + 1, 0);     for (int i = 1; i <= n; i++) cin >> p[i];     for (int i = 1; i <= n; i++) cin >> h[i];     for (int i = 0; i < n - 1; i++) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     gst(1, 0);     int res = 1;     dfs(1, 0, res);     if (!res) return  NO ;     return  YES ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       cout << solve() <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = 3.14159265;   const long long mod = 998244353;   const long long N = 1e6 + 5;   const long long st = 1e2;   const int P0 = 437;   const int P = 1003;   int main() {     cin.sync_with_stdio();     cin.tie(0);     cout.tie(0);     int q;     cin >> q;     while (q--) {       int n;       cin >> n;       vector<int> a(n);       for (int i = 0; i < (n); i++) cin >> a[i];       sort(a.begin(), a.end());       (a.resize(unique(a.begin(), a.end()) - a.begin()));       cout << ((int)(a).size()) <<   n ;     }   }


#include<bits/stdc++.h>   using namespace std;      int main(){    int t;    cin>>t;    while(t--){     int n;     cin>>n;     int o=0,e=0;     int a;     for(int i=0;i<2*n;i++){      cin>>a;      if(a%2==0)       e++;      else       o++;     }        if(o == e)      cout<< YES <<endl;     else      cout<< NO <<endl;    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, n;     int a[1005];     scanf( %d , &n);     for (i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     sort(a + 1, a + 1 + n);     if (n % 2 == 0) {       printf( %d n , a[n / 2]);     } else {       printf( %d n , a[(n + 1) / 2]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m;   long long getans(long long n) {     if (n == 1) return 3 % m;     long long k = getans(n / 2);     k *= k;     if (n % 2) k *= 3;     return k % m;   }   int main() {     while (~scanf( %I64d%I64d , &n, &m)) {       long long ans = getans(n) - 1;       if (ans == -1) ans = m - 1;       printf( %I64d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[100005];   deque<long long> q;   long long mi[100005];   void add_q(long long ae) {     while (!q.empty() && q.back() > ae) q.pop_back();     q.push_back(ae);   }   void pop_q(long long re) {     if (!q.empty() && q.front() == re) q.pop_front();   }   void solve() {     long long n, c, sum = 0;     cin >> n >> c;     for (int i = 0; i < n; i++) {       cin >> a[i];       sum += a[i];     }     for (int i = 0; i < c; i++) {       add_q(a[i]);     }     mi[0] = q.front();     for (int i = c; i < n; i++) {       pop_q(a[i - c]);       add_q(a[i]);       mi[i - c + 1] = q.front();     }     long long maxc = 0;     long long res = 0;     for (int i = 0; i < c; i++) res = max(res, mi[i]);     for (int i = c; i < n; i++) {       maxc = max(maxc, mi[i - c]);       mi[i] += maxc;       res = max(res, mi[i]);     }     cout << sum - res;   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double PI = 3.141592653589793238462643383;   int vector_init(vector<vector<long long int> > &x, int r, int c) {     x.resize(r);     for (int i = 0; i < (r); ++i) x[i].resize(c);   }   int abso(int a) {     if (a > 0) return a;     return -a;   }   void print(vector<long long> &ans) {     for (int i = 0; i < ans.size(); i++) cout << ans[i] <<    ;     cout <<   n ;   }   int equal(vector<int> &a, vector<int> &b) {     for (int i = 0; i < a.size(); i++) {       if (a[i] == b[i] && a[i] == 1) return 1;     }     return 0;   }   void print1(vector<vector<long long> > &edge) {     for (int i = 0; i < edge.size(); i++) print(edge[i]);   }   long long maxx(long long a, long long b) {     if (a > b) return a;     return b;   }   long long minn(long long a, long long b) { return a + b - maxx(a, b); }   long long Ceil(long long a, long long b) {     if (a % b == 0) return a / b;     return a / b + 1;   }   long long find(vector<long long int> &a, vector<long long int> &c, long long t,                  long long cunt) {     for (int i = 0; i < (a.size()); ++i) {       if (a[i] == t) cunt--;       if (cunt == 0) return c[i];     }   }   long long power(long long k, long long m) {     long long ans = 1;     for (int i = 0; i < (k); ++i) {       ans = (ans * 2) % m;     }     return ans;   }   int main() {     cin.sync_with_stdio(0);     cin.tie(0);     cin.exceptions(cin.failbit);     map<long long, long long> start;     map<long long, long long> end;     long long n;     cin >> n;     vector<long long int> a(n, 0);     for (int i = 0; i < (n); ++i) {       cin >> a[i];       if (start[a[i]] == 0) start[a[i]] = i + 1;       end[a[i]] = i + 1;     }     long long t = end[a[0]] - 1;     long long k = 0;     long long cunt = 0;     while (k < n) {       if (k == t + 1) {         cunt++;         t = end[a[k]] - 1;       } else {         t = maxx(t, end[a[k]] - 1);       }       k++;     }     cout << power(cunt, 998244353);   }


#include <bits/stdc++.h>   const int MAX = 1 << 17;   char s[MAX];   long long rje[2], dp[2][2][2][MAX];   int main() {     int i, l, tmpdulj, j, k, ii;     scanf( %s , s + 1);     l = strlen(s + 1);     for (i = 1; i <= l; i++) {       dp[i % 2][1][s[i] -  a ][i] = 1;       rje[1]++;       for (j = 0; j < 2; j++)         for (k = 0; k < 2; k++)           for (ii = 0; ii < 2; ii++) {             tmpdulj = k;             if (s[i] -  a  != ii) tmpdulj = (tmpdulj + 1) % 2;             dp[j][tmpdulj][s[i] -  a ][i] += dp[j][k][ii][i - 1];             if (tmpdulj % 2) rje[(i - j + 1) % 2] += dp[j][k][ii][i - 1];           }     }     printf( %I64d %I64d n , rje[0], rje[1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1000000000;   bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n, greater<int>());     int ans[n];     ans[0] = a[0];     ans[1] = a[1];     for (int i = 2; i < n; i++) {       if (i == 2) {         ans[n - 1] = a[i];       } else {         ans[i - 1] = a[i];       }     }     bool ok = true;     if (a[0] >= a[1] + a[2]) {       ok = false;     }     if (ok) {       cout <<  YES n ;       for (int i = 0; i < n; i++) cout << ans[i] <<    ;     } else {       cout <<  NO n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   vector<vector<char>> grid;   pair<int, int> start, endloc;   int memo[1000][1000][4];   bool dfs(pair<int, int> loc, int turns, int dir) {     if (loc == endloc) return true;     memo[loc.first][loc.second][dir] = turns;     vector<pair<int, int>> next;     next.push_back({loc.first - 1, loc.second});     next.push_back({loc.first + 1, loc.second});     next.push_back({loc.first, loc.second - 1});     next.push_back({loc.first, loc.second + 1});     for (int i = 0; i < 4; i++) {       pair<int, int> nextLoc = next[i];       if (i != dir && turns == 0) continue;       if (nextLoc.first < 0 || nextLoc.first >= n) continue;       if (nextLoc.second < 0 || nextLoc.second >= m) continue;       if (grid[nextLoc.first][nextLoc.second] ==  * ) continue;       int turnDiff = 0;       if (i != dir) turnDiff = 1;       if (memo[nextLoc.first][nextLoc.second][i] >= turns - turnDiff) continue;       if (dfs(nextLoc, turns - turnDiff, i)) return true;     }     return false;   }   int main() {     ios::sync_with_stdio(0);     cin >> n >> m;     memset(memo, -1, sizeof memo);     grid = vector<vector<char>>(n, vector<char>(m,    ));     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         char curr;         cin >> curr;         if (curr ==  S ) {           start = {i, j};         } else if (curr ==  T ) {           endloc = {i, j};         }         grid[i][j] = curr;       }     }     for (int i = 0; i < 4; i++) {       if (dfs(start, 2, i)) {         cout <<  YES  << endl;         return 0;       }     }     cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;      int main() {       int t;       cin >> t;       while (t--) {           int s;           cin >> s;           int k, ans = 0;              for (k = 1; s > 0 && s >= k; k += 2)               s -= k, ++ans;           for (k = min(s, k - 1); s > 0 && k >= 1; k--) {               ans += s / k;               s -= (s / k) * k;           }              cout << ans <<   n ;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100100;   int N;   long long K;   int ar[MAXN];   int main() {     cin >> N >> K;     for (int i = 0; i < N; i++) cin >> ar[i];     --K;     sort(ar, ar + N);     for (int i = 0; i < N;) {       int cur = i;       int cnt = 0;       while (i < N && ar[i] == ar[cur]) {         ++i;         ++cnt;       }       if (K < (long long)cnt * N) {         cout << ar[cur] <<     << ar[(int)(K / cnt)] << endl;         break;       }       K -= (long long)cnt * N;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void fileIO() {}   bool sortbysecdesc(const pair<long long, long long> &a,                      const pair<long long, long long> &b) {     return a.second > b.second;   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     fileIO();     long long T;     T = 1;     while (T--) {       long long n;       cin >> n;       vector<long long> a(n);       for (long long &x : a) {         cin >> x;       }       long long moves = 0;       long long curr = 0;       for (long long x : a) {         moves += abs(x - curr);         curr = x;       }       cout << moves <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 300000;   long long n, x[Maxn], w[Maxn], Index[Maxn], nx[Maxn];   set<pair<long long, long long> > poi, xx;   set<pair<long long, long long> > u;   void input() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x[i] >> w[i];       xx.insert(make_pair(x[i], i));       poi.insert(make_pair(x[i] - w[i], i));     }   }   void solve() {     long long k = 0;     for (auto c : poi) {       Index[c.second] = k;       k++;     }     for (auto c : xx) {       long long ind = c.second;       poi.erase(make_pair(x[ind] - w[ind], ind));       long long sum = x[ind] + w[ind];       auto limIt = poi.lower_bound(make_pair(sum, 0));       if (limIt != poi.end()) {         auto lim = *limIt;         nx[Index[ind]] = Index[lim.second];         u.insert(make_pair(Index[lim.second], Index[ind]));       } else         nx[Index[ind]] = -1;     }     int ans = 1, last = 0;     while (((int)u.size())) {       auto c = (*u.begin()).first;       ans++;       for (; last < c; last++)         if (nx[last] >= 0) u.erase(make_pair(nx[last], last));     }     cout << ans << endl;   }   int main() {     ios::sync_with_stdio(0);     cin.tie();     input();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, a, b, ans;     scanf( %d , &t);     while (t--) {       ans = 0;       scanf( %d %d , &a, &b);       while (a > 0 && b > 0) {         if (a >= b) {           ans += (a / b);           a = a % b;         } else if (b > a) {           ans += (b / a);           b = b % a;         }       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int n = 3;   long long r, g, b;   void reset() {     if (r >= g) swap(r, g);     if (r >= b) swap(r, b);     if (g >= b) swap(g, b);   }   int main() {     while (cin >> r >> g >> b) {       reset();       if ((r + g) * 2 <= b)         cout << r + g << endl;       else         cout << (r + g + b) / 3 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using Long = long long;   int main() {     int t;     cin >> t;     while (t--) {       int d;       cin >> d;       if (d * d < 4 * d) {         puts( N );       } else {         double dis = sqrt(d * d - 4 * d);         double x = (dis + d) / 2, y = (d - dis) / 2;         printf( Y %0.10lf %0.10lf n , x, y);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long K, A, B, V;     cin >> K >> A >> B >> V;     long long nuts = A;     long long divisors = B;     long long capacity = V;     long long maxSections = K;     int boxes = 0;     while (nuts > 0) {       boxes++;       int sections = min(divisors + 1, maxSections);       nuts -= sections * capacity;       divisors -= sections - 1;     }     cout << boxes;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, ans[(int)20 + 7][(int)20 + 7], a[500];   set<pair<int, int> > st;   map<int, int> mp;   int main() {     ios::sync_with_stdio(false);     cin >> N;     for (int i = 0; i < N * N; i++) {       cin >> a[i];       mp[a[i]]++;     }     int idx = -1;     map<int, int>::iterator itt = mp.begin();     while (itt != mp.end()) {       st.insert({itt->second, itt->first});       itt++;     }     memset(ans, -1, sizeof ans);     for (int i = 0; i < N; i++) {       for (int j = 0; j < N; j++) {         if (ans[i][j] != -1) continue;         if (!st.size()) return cout <<  NO , 0;         if (N % 2 && i == N / 2 || j == N / 2) {           continue;         }         set<pair<int, int> >::iterator it = st.lower_bound({4, -1});         ;         int c = (*it).first, n = (*it).second;         if (it == st.end()) return cout <<  NO , 0;         st.erase(it);         c -= 4;         if (c < 0) return cout <<  NO , 0;         ans[i][j] = n;         ans[i][N - j - 1] = n;         ans[N - i - 1][j] = n;         ans[N - i - 1][N - j - 1] = n;         if (c) st.insert({c, n});       }     }     for (int i = 0; i < N; i++) {       for (int j = 0; j < N; j++) {         if (ans[i][j] != -1) continue;         if (N % 2 && i == N / 2 && j == N / 2) continue;         if (N % 2 && i == N / 2) {           set<pair<int, int> >::iterator it = st.lower_bound({2, -1});           if (it == st.end()) return cout <<  NO , 0;           st.erase(it);           int c = (*it).first, n = (*it).second;           c -= 2;           if (c < 0) return cout <<  NO , 0;           ans[i][j] = n;           ans[i][N - j - 1] = n;           if (c) st.insert({c, n});         } else if (N % 2 && j == N / 2) {           set<pair<int, int> >::iterator it = st.lower_bound({2, -1});           ;           if (it == st.end()) return cout <<  NO , 0;           st.erase(it);           int c = (*it).first, n = (*it).second;           c -= 2;           if (c < 0) return cout <<  NO , 0;           ans[i][j] = n;           ans[N - i - 1][j] = n;           if (c) st.insert({c, n});         }       }     }     if (N % 2) {       ans[N / 2][N / 2] = (*st.begin()).second;     }     cout <<  YES  << endl;     for (int i = 0; i < N; i++, cout << endl)       for (int j = 0; j < N; j++) cout << ans[i][j] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long M = 1e9 + 7;   void flashSpeed() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);   }   const long long N = 2e5 + 5;   long long fact[N];   double Round(double var) {     float value = (long long)(var * 100 + .5);     return (float)value / 100;   }   long long ceils(long long x, long long y) { return x / y + ((x % y) != 0); }   long long Gcd(long long a, long long b) {     if (b > a) {       return Gcd(b, a);     }     if (b == 0)       return a;     else       return Gcd(b, a % b);   }   long long lcm(long long a, long long b) { return a / Gcd(a, b) * b; }   bool isPal(string s) {     for (long long i = 0; i < (long long)s.size() / 2; i++) {       if (s[i] != s[(long long)s.size() - 1 - i]) return false;     }     return true;   }   long long Sumdigits(long long a) {     long long total = 0;     while (a) {       total += a % 10;       a /= 10;     }     return total;   }   bool isPerfectSquare(long long n) {     for (long long i = 1; i * i <= n; i++) {       if ((n % i == 0) && (n / i == i)) {         return true;       }     }     return false;   }   bool isPowerOfTwo(long long n) { return (ceil(log2(n)) == floor(log2(n))); }   void lexosmaintest(string s, string c) {     string t1 = s;     sort(t1.begin(), t1.end());     long long index = -1;     for (long long i = 0; i < s.length(); i++) {       if (s[i] != t1[i]) {         index = i;         break;       }     }     long long j;     for (long long i = 0; i < s.length(); i++) {       if (s[i] == t1[index]) j = i;     }     swap(s[index], s[j]);   }   const long long NN = 1e4 + 5;   long long primes[NN];   vector<long long> pr;   void sieve() {     for (long long i = 2; i < NN; i++) {       if (primes[i] == 0) {         pr.push_back(i);         for (long long j = i * i; j < NN; j += i) {           primes[j] = 1;         }       }       primes[i] ^= 1;     }   }   const long long mod2 = 998244353;   long long powerr(long long x, long long y, long long p = mod2) {     long long res = 1;     x = x % p;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long iAmUseless(long long n) {}   const double PI = acos(-1);   long long lefty[N];   long long righty[N];   void chaloTryKrteHai() {     long long a, b, c;     cin >> a >> b >> c;     long long m;     cin >> m;     long long second = a + b + c;     if (m > second - 3LL) {       cout <<  NO  << endl;     } else {       long long maxi = max({a, b, c});       second -= maxi;       if (maxi - 1 > second) {         if (m < (maxi - second) - 1) {           cout <<  NO  << endl;         } else {           cout <<  YES  << endl;         }       } else {         cout <<  YES  << endl;       }     }   }   int32_t main() {     flashSpeed();     sieve();     long long t = 1;     cin >> t;     while (t--) {       chaloTryKrteHai();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   int32_t main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long t;     t = 1;     cin >> t;     for (long long tc = 0; tc < t; tc++) {       string str;       cin >> str;       long long cnt_ab = 0, cnt_ba = 0;       for (long long i = 0; i + 1 < (long long)str.size(); i++) {         cnt_ab += (str[i] ==  a  && str[i + 1] ==  b );         cnt_ba += (str[i] ==  b  && str[i + 1] ==  a );       }       if (cnt_ab == cnt_ba) {         cout << str <<   n ;         continue;       }       if (cnt_ab > cnt_ba) {         for (long long i = 0; i + 1 < (long long)str.size(); i++) {           if (str[i] ==  a ) {             str[i] =  b ;             break;           }         }       } else {         for (long long i = 0; i + 1 < (long long)str.size(); i++) {           if (str[i] ==  b ) {             str[i] =  a ;             break;           }         }       }       cout << str <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long MAXN = 9223372036854775807, mod = 998244353;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long T = 1;     for (long long t = 0; t < T; t++) {       long long n, s1, s2, p1, p2;       cin >> n >> s1 >> s2 >> p1 >> p2;       long long time1 = s1 * n + 2 * p1;       long long time2 = s2 * n + 2 * p2;       if (time1 < time2)         cout <<  First ;       else if (time2 < time1)         cout <<  Second ;       else         cout <<  Friendship ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N;   int A[100005];   int main(int argc, char *argv[]) {     scanf( %d , &N);     for (int i = (1), _n = (N); i <= _n; i++) scanf( %d , &A[i]);     int hi = -1;     for (int i = (1), _n = (N); i <= _n; i++) hi = max(hi, A[i]);     bool done[100005] = {false};     int ans = 1;     for (int i = (1), _n = (N); i <= _n; i++)       if (A[i] == hi && done[i] == false) {         int tans = 0;         for (int j = (i), _n = (N); j <= _n; j++)           if (A[j] == hi)             done[j] = true, tans++;           else             break;         ans = max(ans, tans);       }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int B, K;     cin >> B >> K;     vector<int> a(K);     for (int i = 0; i < K; ++i) cin >> a[i];     int odd = 0;     bool flag = (B % 2 ? 1 : 0);     for (int i = 0; i < K - 1; ++i) {       odd += (a[i] % 2 && flag);     }     odd += (a.back() % 2);     cout << (odd % 2 ?  odd  :  even ) <<   n ;     return 0;   }


#include <bits/stdc++.h>   const double PI = acos(-1);   using namespace std;   const int mod = 1e9 + 7;   const long long oo = 0x3f3f3f3f;   const int nn = 1e2 + 15;   vector<pair<int, int>> gr[nn], shifts;   map<char, int> vis;   long long get_distance(pair<int, int> a, pair<int, int> b) {     return ((a.first - b.first) * 1LL * (a.first - b.first) +             (a.second - b.second) * (a.second - b.second));   }   int main() {     ios_base::sync_with_stdio(false);     cout.tie(NULL);     cin.tie(NULL);     ;     int n, m, x;     cin >> n >> m >> x;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         char c;         cin >> c;         if (c ==  S )           shifts.push_back(make_pair(i, j));         else           gr[c -  a ].push_back(make_pair(i, j));       }     }     int len;     cin >> len;     string s;     cin >> s;     int res = 0;     for (int i = 0; i < len; i++) {       if (isupper(s[i])) {         s[i] = tolower(s[i]);         if (!((int)((gr[s[i] -  a ]).size())) || !((int)((shifts).size())))           return cout << -1, 0;         else {           if (vis[s[i]]) {             if (vis[s[i]] > x * x) res++;             continue;           }           int mn = oo;           for (int j = 0; j < ((int)((shifts).size())); j++) {             for (int k = 0; k < ((int)((gr[s[i] -  a ]).size())); k++) {               int dist = get_distance(shifts[j], gr[s[i] -  a ][k]);               mn = min(mn, dist);             }           }           if (mn > x * x) res++;           vis[s[i]] = mn;         }       } else {         if (!((int)((gr[s[i] -  a ]).size()))) return cout << -1, 0;       }     }     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   long long solve() {     pair<long long, long long> init, final;     cin >> init.first >> init.second >> final.first >> final.second;     long long n;     cin >> n;     string s;     cin >> s;     pair<long long, long long> pre[n], cur = {0, 0};     map<char, pair<long long, long long> > dir;     dir[ L ] = make_pair(-1, 0);     dir[ R ] = make_pair(1, 0);     dir[ U ] = make_pair(0, 1);     dir[ D ] = make_pair(0, -1);     for (long long i = 0; i < n; ++i)       cur.first += dir[s[i]].first, cur.second += dir[s[i]].second, pre[i] = cur;     long long l = 0, r = 1e18;     while (r - l > 1) {       long long mid = (r + l) / 2;       long long x = init.first + (mid / n) * pre[n - 1].first,                 y = init.second + (mid / n) * pre[n - 1].second;       if (mid % n) x += (pre[mid % n - 1].first), y += (pre[mid % n - 1].second);       if (abs(x - final.first) + abs(y - final.second) <= mid)         r = mid;       else         l = mid;     }     if (r > 1e17) r = -1;     cout << r <<   n ;     return 0;   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void FAST_IO() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   int main() {     FAST_IO();     int n, m;     cin >> n >> m;     pair<int, int> ac{0, 0}, bc{0, 0};     for (int i = 0; i < n; ++i) {       int a;       cin >> a;       if (a % 2)         ac.first++;       else         ac.second++;     }     for (int i = 0; i < m; ++i) {       int a;       cin >> a;       if (a % 2)         bc.first++;       else         bc.second++;     }     cout << min(ac.first, bc.second) + min(ac.second, bc.first) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 210000, K = 60;   int n, k, p;   int a[N], sum[N], f[N][K];   int main() {     scanf( %d%d%d , &n, &k, &p);     for (int i = 1; i <= n; i++)       scanf( %d , &a[i]), sum[i] = (sum[i - 1] + a[i] % p) % p;     memset(f, -0x3f, sizeof(f));     f[0][0] = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= k; j++) {         for (int o = 0; o <= p; o++) {           f[sum[i]][j] = max(f[sum[i]][j], f[o][j - 1] + (sum[i] - o + p) % p);         }       }     }     printf( %d n , f[sum[n]][k]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 500005;   int n, m, u[MAXN], v[MAXN];   bool cho[MAXN], sel[MAXN];   void solve() {     int ans = 0;     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d , &u[i], &v[i]);       if (!cho[u[i]] && !cho[v[i]]) {         sel[i] = 1;         ++ans;         cho[u[i]] = cho[v[i]] = 1;       }     }     if (ans >= n) {       printf( Matching n );       int cnt = 0;       for (int i = 1; i <= m && cnt < n; i++)         if (sel[i]) {           printf( %d  , i);           ++cnt;         }       printf(  n );     } else {       printf( IndSet n );       int cnt = 0;       for (int i = 1; i <= 3 * n && cnt < n; i++)         if (!cho[i]) {           printf( %d  , i);           ++cnt;         }       printf(  n );     }     for (int i = 1; i <= 3 * n; i++) cho[i] = 0;     for (int i = 1; i <= m; i++) sel[i] = 0;   }   int main() {     int T;     scanf( %d , &T);     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double pi = 3.1415926535897932384626433832795l;   template <typename T>   inline auto sqr(T x) -> decltype(x * x) {     return x * x;   }   template <typename T1, typename T2>   inline bool umx(T1& a, T2 b) {     if (a < b) {       a = b;       return 1;     }     return 0;   }   template <typename T1, typename T2>   inline bool umn(T1& a, T2 b) {     if (b < a) {       a = b;       return 1;     }     return 0;   }   const int N = 1000100;   int p[N];   bool use[N];   int main() {     std::ios::sync_with_stdio(false);     cout.setf(ios::showpoint | ios::fixed);     cout.precision(20);     int n;     cin >> n;     for (int i = int(0); i < int(n); ++i) {       cin >> p[i];       p[i]--;     }     int res = n;     for (int i = int(0); i < int(n); ++i) {       if (use[i]) continue;       int cur = i;       while (!use[cur]) {         use[cur] = 1;         cur = p[cur];       }       res--;     }     if (((n & 1) ^ (res & 1)) == 0) {       cout <<  Petr ;     } else {       cout <<  Um_nik ;     }     return 0;   }


#include <bits/stdc++.h>   struct ele {     int y, st;   } tmp;   std::vector<ele> edge[100100];   std::vector<int> gr[100100];   bool vi[100100];   int co[100100], N;   int flag, sp[100100];   void dfs(int x) {     int c, t;     for (int k = 0; k < edge[x].size(); k++) {       c = edge[x][k].y;       t = edge[x][k].st;       if (c == 1) continue;       if (!vi[c]) {         vi[c] = true;         if (t == 0)           co[c] = 1 + co[x] % 2;         else           co[c] = co[x];         gr[N].push_back(c);         dfs(c);       } else {         if ((co[x] == co[c] && t == 0) || (co[x] != co[c] && t == 1)) {           flag = 0;           return;         }       }     }     return;   }   int main() {     int k, n, m, t, a, b, c, d, i;     scanf( %d%d , &n, &m);     flag = 1;     memset(sp, -1, sizeof(sp));     for (k = 1; k <= m; k++) {       scanf( %d%d%d , &a, &b, &t);       edge[a].push_back(ele{b, t});       edge[b].push_back(ele{a, t});       if (a == 1) sp[b] = t + 1;       if (b == 1) sp[a] = t + 1;     }     for (k = 2; k <= n; k++) {       if (!vi[k]) {         N++;         gr[N].push_back(k);         vi[k] = 1;         co[k] = 1;         dfs(k);       }     }     long long ans = 1, MOD = 1000000007, tm, s;     if (flag == 0) {       printf( 0 n );       return 0;     }     for (k = 1; k <= N; k++) {       tm = 2;       s = -1;       for (i = 0; i < gr[k].size(); i++) {         c = gr[k][i];         if (sp[c] == -1) continue;         if (s == -1) {           if (sp[c] == co[c])             s = 0;           else             s = 1;           tm = 1;         } else {           if (sp[c] == co[c] && s == 1) tm = 0;           if (sp[c] != co[c] && s == 0) tm = 0;         }       }       ans *= tm;       ans %= MOD;     }     printf( %I64d , ans);   }


#include <bits/stdc++.h>   using namespace std;   long long a[1010000];   inline bool read(long long &num) {     char in;     bool IsN = false;     in = getchar();     if (in == EOF) return false;     while (in !=  -  && (in <  0  || in >  9 )) in = getchar();     if (in ==  - ) {       IsN = true;       num = 0;     } else       num = in -  0 ;     while (in = getchar(), in >=  0  && in <=  9 ) {       num *= 10, num += in -  0 ;     }     if (IsN) num = -num;     return true;   }   int main() {     long long n;     scanf( %lld , &n);     long long sum = 0;     for (long long i = 1; i <= n; i++) {       read(a[i]);       sum += a[i];     }     if (n == 1) {       printf( %lld n , a[1]);       return 0;     }     if ((sum - (n * (n - 1) / 2)) % n == 0) {       long long x = (sum - (n * (n - 1) / 2)) / n;       for (long long i = 1; i < n; i++) printf( %lld  , x + i - 1);       printf( %lld n , x + n - 1);       return 0;     }     for (long long i = 1; i <= n; i++) {       if ((sum - (n * (n - 1) / 2) + i) % n == 0) {         long long x = (i + sum - (n * (n - 1) / 2)) / n, nd = x + n - i - 1;         for (long long j = 1; j < n - 1; j++) {           printf( %lld  , x);           if (x == nd) printf( %lld  , x);           x++;         }         if (x == nd) printf( %lld  , x);         printf( %lld n , x);         return 0;       }     }   }


#include <bits/stdc++.h>   int main() {     unsigned long long a, b, x;     std::cin >> a >> b;     a < b | a - b & 1         ? std::cout << -1         : (x = a - b >> 1,            x ^ a - x ^ b ? std::cout << -1 : std::cout << x <<     << a - x);   }


#include <iostream>   #include <bits/stdc++.h>      int i = 0, j = 0, g = 0;      #define int long long   #define float double      using namespace std;      void solve(void)   {       int n;          cin >> n;          int a[2*n], config = 0;          set <int> s;          for (i = 0; i < 2*n; i++)       {           cin >> a[i];              if (a[i] % 2 == 1)           {               config = -1;           }              s.insert(a[i]);       }          sort(a, a + 2*n);          if (config == -1 || s.size() != n)       {           cout <<  NO n ;           return;       }          j = 2;          int asum = 0, csum = 0;              for (i = 1; i <= n - 1; i++)       {           if ((a[2*i] - a[2*(i - 1)]) % j != 0)           {               cout <<  NO n ;               return;           }              asum += (a[2*i] - a[2*(i - 1)])/j;           csum += asum;              j += 2;       }          if (((a[0]/2) - csum <= 0) || ((((a[0]/2) - csum) % n) != 0))       {           cout <<  NO n ;           return;       }          cout <<  YES n ;   }      int32_t main(void)   {       ios_base::sync_with_stdio(false);       cin.tie(0);       cout.tie(0);          int t;          cin >> t;             while (t--)       {           solve();       }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2005;   int x[N], y[N], c[N], k[N], f[N];   int s[N], st, tu[N], tv[N];   struct node {     int u, v;     long long s;     bool operator<(const node &p) const { return s < p.s; }   } d[N * N + N];   int dis(int a, int b) { return abs(x[a] - x[b]) + abs(y[a] - y[b]); }   int fd(int x) {     if (f[x] == x) return x;     return f[x] = fd(f[x]);   }   void mg(int x, int y) {     int fa = fd(x), fb = fd(y);     f[fa] = fb;   }   int main() {     int n, p = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d%d , &x[i], &y[i]);     for (int i = 1; i <= n; i++) {       scanf( %d , &c[i]);       f[i] = i;       d[++p].s = c[i];       d[p].u = i;       d[p].v = n + 1;     }     for (int i = 1; i <= n; i++) scanf( %d , &k[i]);     for (int i = 1; i <= n; i++)       for (int j = i + 1; j <= n; j++)         d[++p].s = 1ll * dis(i, j) * (k[i] + k[j]), d[p].u = i, d[p].v = j;     sort(d + 1, d + p + 1);     int cnt = 0;     long long ans = 0;     for (int i = 1; i <= p; i++) {       if (fd(d[i].u) == fd(d[i].v)) continue;       if (d[i].v == n + 1)         s[++s[0]] = d[i].u;       else {         st++;         tu[st] = d[i].u;         tv[st] = d[i].v;       }       mg(d[i].u, d[i].v);       ans += d[i].s;       cnt++;       if (cnt == n) break;     }     printf( %lld n , ans);     printf( %d n , s[0]);     for (int i = 1; i <= s[0]; i++) printf( %d  , s[i]);     puts(  );     printf( %d n , st);     for (int i = 1; i <= st; i++) printf( %d %d n , tu[i], tv[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long 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;   }   inline void write(long long a) {     if (a < 0) {       printf( - );       a = -a;     }     if (a >= 10) write(a / 10);     putchar(a % 10 +  0 );   }   inline void writeln(long long a) {     write(a);     printf(  n );   }   long long bin[110];   int main() {     long long t = read(), a = read(), b = read();     if (t == a && a == b) {       if (t == 1ll) printf( inf n );       if (t != 1ll) printf( 2 n );       return 0;     }     if (t > a) {       if (a != b)         puts( 0 );       else         puts( 1 );       return 0;     }     if (a == b) {       puts( 1 );       return 0;     }     int len = 0, cnt = 0;     if (t == 1 && a == 1 && b != 1) {       puts( 0 );       return 0;     }     while (b) {       bin[++len] = b % a;       cnt += bin[len];       b /= a;     }     long long ans = 0;     for (int i = len; i >= 1; i--) {       ans = ans * t + bin[i];     }     if (cnt == 1) {       if (ans != a) {         ans = 0;         len--;         bin[len] = a;         for (int i = len; i >= 1; i--) ans = ans * t + bin[i];       }     }     printf( %d n , ans == a);   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 10;   const int MAXN = 1e4 + 10;   const int MOD = 1e9 + 7;   const int inf = 1e9;   const double pi = acos(-1.0);   const double eps = 1e-6;   int dx[] = {0, -1, 0, 1};   int dy[] = {1, 0, -1, 0};   int x, y;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int a, b, c;     cin >> x >> y;     a = b = c = y;     int res = 0;     while (a < x || b < x || c < x) {       int maxa = max(a, max(b, c));       int maxc = min(a, min(b, c));       int maxb = a + b + c - maxa - maxc;       int l = maxa + maxb - maxc - 1;       if (a == maxc)         a += l;       else if (b == maxc)         b += l;       else         c += l;       res++;     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long fp(long long x, long long y, long long m = 998244353) {     long long ANS = 1;     while (y) {       if (y & 1) ANS = (ANS * x) % 998244353;       x = (x * x) % 998244353;       y >>= 1;     }     return ANS;   }   long long inv(long long x, long long m = 998244353) { return fp(x, m - 2); }   long long testcases, arr[200001], n;   string s;   int32_t main() {     iostream::sync_with_stdio(0), cin.tie(0);     cin >> testcases;     while (testcases--) {       cin >> n;       for (long long i = 0; i < n; i++) cin >> arr[i];       cin >> s;       multiset<long long> L, S;       for (long long i = 0; i < n; i++) {         if (s[i] ==  R )           L.insert(arr[i]);         else           S.insert(arr[i]);       }       for (long long i = 1; i <= n; i++) {         auto itr = S.lower_bound(i);         if (itr == S.end()) {           auto itr2 = L.upper_bound(i);           if (itr2 == L.begin()) {             cout <<  NO  <<   n ;             goto a;           }           itr2--;           L.erase(itr2);         } else           S.erase(itr);       }       cout <<  YES  <<   n ;     a:;     }   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-9;   const int oo = (int)1e9;   const double PI = 2 * acos(0.0);   const double eps = 1e-7;   const int MAXN = 1e5 + 10;   int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};   int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};   int main() {     int n, k, a[100009];     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     set<long long> s;     for (int i = n - 1; i >= 0; i--)       if (s.find((long long)k * a[i]) == s.end()) s.insert(a[i]);     cout << ((int)((s).size())) << endl;   }


#include <bits/stdc++.h>   using namespace std;   int k, tot;   int op[4];   char s;   long long ans = 0x3f3f3f3f3f3f3f3f;   long long a[5];   template <typename T>   inline void read(T& x) {     x = 0;     T op = 1;     char c = getchar();     for (; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) op = -1;     for (; c <=  9  && c >=  0 ; c = getchar()) x = (x << 3) + (x << 1) + c -  0 ;   }   void dfs(int p, int k) {     if (p == 4) {       ans = ans < a[k] ? ans : a[k];       return;     }     for (int i = 1; i <= 4; ++i) {       if (a[i] == -1) continue;       for (int j = 1; j <= 4; ++j) {         if (i == j || a[j] == -1) continue;         if (op[p]) {           a[i] += a[j];           long long t = a[j];           a[j] = -1;           dfs(p + 1, i);           a[j] = t;           a[i] -= a[j];         } else {           a[i] *= a[j];           long long t = a[j];           a[j] = -1;           dfs(p + 1, i);           a[j] = t;           a[i] /= a[j];         }       }     }   }   int main() {     for (int i = 1; i <= 4; ++i) {       read(a[i]);       if (!a[i]) k = 1;     }     while (tot < 3) {       scanf( %c , &s);       if (s ==  + ) op[++tot] = 1;       if (s ==  * ) {         op[++tot] = 0;         if (k) {           puts( 0 );           return 0;         }       }     }     dfs(1, 0);     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double sqr(double x) { return x * x; }   struct CPoint {     double x, y, n, l;     double length() { return sqrt(sqr(x) + sqr(y)); }   };   CPoint a[100100], b[100100];   double length(CPoint p1, CPoint p2) {     return sqrt(sqr(p1.x - p2.x) + sqr(p1.y - p2.y));   }   double calc(CPoint X, CPoint Y) { return X.length() + length(X, Y) + Y.l; }   int b_search(CPoint X, int l, int r) {     if (r - l <= 10) {       int i = -1;       double v = 100000000, tmp;       for (; l <= r; l++) {         tmp = calc(a[l], X);         if (tmp < v) {           v = tmp;           i = l;         }       }       return i;     }     int mid = (l + r) / 2;     double p1 = calc(a[mid - 1], X);     double p2 = calc(a[mid], X);     double p3 = calc(a[mid + 1], X);     if (p2 <= p3 && p2 <= p1) {       int i = b_search(X, l, mid);       int j = b_search(X, mid, r);       if (calc(a[i], X) < calc(a[j], X)) return i;       return j;     }     if (p1 <= p2 && p2 <= p3) return b_search(X, l, mid);     return b_search(X, mid, r);   }   int main() {     int N, M, i, j;     double A, B;     scanf( %d%d%lf%lf , &N, &M, &A, &B);     for (i = 0; i < N; i++) {       scanf( %lf , &a[i].y);       a[i].x = A;       a[i].n = i + 1;     }     for (i = 0; i < M; i++) {       scanf( %lf , &b[i].y);       b[i].x = B;       b[i].n = i + 1;     }     for (i = 0; i < M; i++) scanf( %lf , &b[i].l);     double v = 1000000000;     int ii = -1, jj = -1;     for (i = 0; i < M; i++) {       j = b_search(b[i], 0, N - 1);       if (v > calc(a[j], b[i])) {         jj = i + 1;         ii = j + 1;         v = calc(a[j], b[i]);       }     }     printf( %d %d , ii, jj);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long int n, x, y;       cin >> n >> x >> y;       long long int maxi = x + y - 2 + 1;       maxi = min(maxi, n);       long long int mini = x + y - n;       if (mini <= 0)         mini = 1;       else         mini++;       mini = min(mini, n);       cout << mini <<     << maxi << endl;     }   }


#include <bits/stdc++.h>   const int base = 1000003;   int n, k, d[201][30001], v[201];   bool isv[30010];   int fa[30010], tmp[30010];   bool on[30010];   struct edge {     int to;     edge* next;   } E[60010], *ne = E, *first[30010];   void link(int u, int v) {     *ne = (edge){v, first[u]};     first[u] = ne++;   }   bool chk(int i, int f, int d, int* g) {     if (d != g[i]) return 0;     for (edge* e = first[i]; e; e = e->next)       if (e->to != f && !chk(e->to, i, d + 1, g)) return 0;     return 1;   }   int list[30010], len;   bool cmp(int i, int j) { return d[0][i] < d[0][j]; }   unsigned long long hval[30010], hadd;   std::map<unsigned long long, int> M;   int main() {     scanf( %d%d , &n, &k);     for (int i = 0; i < k; i++) {       int fd = 0;       for (int j = 0; j < n; j++)         scanf( %d , d[i] + j), !d[i][j] ? fd++, v[i] = j : 1;       if (fd != 1) return puts( -1 ), 0;       fd = 0;       for (int j = 0; j < i; j++)         if (v[j] == v[i]) {           fd = 1;           for (int x = 0; x < n; x++)             if (d[j][x] != d[i][x]) return puts( -1 ), 0;         }       if (fd) i--, k--;       isv[v[i]] = 1;     }     on[v[0]] = 1;     for (int i = 0; i < n; i++) fa[i] = -1;     for (int i = 1; i < k; i++) {       int x = v[i];       for (int j = 0; j <= d[0][x]; j++) tmp[j] = -1;       for (int j = 0; j < n; j++)         if (d[0][j] + d[i][j] == d[0][x]) {           if (tmp[d[0][j]] > -1) return puts( -1 ), 0;           tmp[d[0][j]] = j;         }       for (int j = 0; j <= d[0][x]; j++) {         if (tmp[j] < 0) return puts( -1 ), 0;         if (j) {           if (fa[tmp[j]] > -1 && fa[tmp[j]] != tmp[j - 1]) return puts( -1 ), 0;           fa[tmp[j]] = tmp[j - 1];           on[tmp[j]] = 1;         }       }     }     for (int i = 0; i < n; i++)       if (on[i] && fa[i] > -1) link(i, fa[i]), link(fa[i], i);     for (int i = 0; i < k; i++)       if (!chk(v[i], -1, 0, d[i])) return puts( -1 ), 0;     for (int i = 0; i < k; i++) hadd = hadd * base + 1;     for (int i = 0; i < n; i++) {       for (int j = k; j--;) hval[i] = hval[i] * base + d[j][i];       if (on[i]) M[hval[i]] = i;     }     for (int i = 0; i < n; i++)       if (!on[i]) list[len++] = i;     std::sort(list, list + len, cmp);     for (int* i = list; i < list + len; i++) {       unsigned long long h = hval[*i] - hadd;       if (!M.count(h)) return puts( -1 ), 0;       fa[*i] = M[h];       M[hval[*i]] = *i;     }     for (int i = 0; i < n; i++)       if (fa[i] > -1) printf( %d %d n , i + 1, fa[i] + 1);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100000 + 7;   int n;   int val[N];   struct edge {     int to, nex;   } e[N << 1];   int fir[N], eid;   int siz[N], fa[N], son[N];   int dfn[N], pos[N], inx;   int cnt[N], res[N];   long long mul[N], ans[N];   void addedge(int u, int v) {     e[++eid] = (edge){v, fir[u]};     fir[u] = eid;   }   void hld(int s) {     siz[s] = 1;     pos[dfn[s] = ++inx] = s;     for (int i = fir[s]; i; i = e[i].nex)       if (e[i].to != fa[s])         fa[e[i].to] = s, hld(e[i].to), siz[s] += siz[e[i].to],         son[s] = siz[e[i].to] > siz[son[s]] ? e[i].to : son[s];   }   void dfs(int s, int rem) {     for (int i = fir[s]; i; i = e[i].nex)       if (e[i].to != fa[s] && e[i].to != son[s]) dfs(e[i].to, 0);     if (son[s]) dfs(son[s], 1);     int c = val[s];     mul[cnt[c]] -= c, mul[++cnt[c]] += c, res[s] = max(res[son[s]], cnt[c]);     for (int i = fir[s]; i; i = e[i].nex) {       if (e[i].to == fa[s] || e[i].to == son[s]) continue;       for (int j = dfn[e[i].to], c; j < dfn[e[i].to] + siz[e[i].to]; ++j)         c = val[pos[j]], mul[cnt[c]] -= c, mul[++cnt[c]] += c,         res[s] = max(res[s], cnt[c]);     }     ans[s] = mul[res[s]];     if (rem) return;     memset(cnt, 0, sizeof(int) * (n + 2));     memset(mul, 0, sizeof(long long) * (res[s] + 2));   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , val + i);     for (int i = 1, u, v; i < n; ++i)       scanf( %d%d , &u, &v), addedge(u, v), addedge(v, u);     hld(1);     dfs(1, 1);     for (int i = 1; i <= n; ++i) printf( %I64d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a[1010] = {0};     int n, i, s = 0, x, m = 0;     cin >> n;     for (i = 0; i < n; i++) {       cin >> x;       a[x]++;     }     for (i = 0; i <= 1000; i++) {       if (a[i]) m++;       if (a[i] > s) s = a[i];     }     cout << s <<     << m << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = (1 << 30) - 1;   const long long linf = (1ll << 62) - 1;   int GLL(long long& x) { return scanf( %lld , &x); }   int GI(int& x) { return scanf( %d , &x); }   void IO() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin.exceptions(ios::badbit | ios::failbit);   }   void file() {     freopen( mountains.in ,  r , stdin);     freopen( mountains.out ,  w , stdout);   }   const int N = 1e5 + 7, M = 2 * N;   int to[M], nxt[M], head[N], ne, visited[N], vid;   int n, m;   void init() {     memset(head, -1, n * sizeof(head[0]));     ne = 0;   }   void addEdge(int u, int v) {     to[ne] = v;     nxt[ne] = head[u];     head[u] = ne++;   }   void BiEdge(int u, int v) {     addEdge(u, v);     addEdge(v, u);   }   long long A[100001], B[100001], C[100001];   void solve() {     int n;     cin >> n;     if (n % 2 == 0)       puts( -1 );     else {       for (int i = 0; i < n; i++) {         A[i] = B[i] = i;         C[i] = (A[i] + B[i]) % n;       }       for (int i = 0; i < n; i++) cout << A[i] <<    ;       cout << endl;       for (int j = 0; j < n; j++) cout << B[j] <<    ;       cout << endl;       for (int j = 0; j < n; j++) cout << C[j] <<    ;       cout << endl;     }   }   int main() {     IO();     long long t = 1;     solve();   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize(2)   #pragma GCC optimize(3)   const int N = 1e6 + 10, p1 = 1e9 + 7, p2 = 1e9 + 9;   int n, k, len;   char s[N << 1], ch;   int strhash[N][2], hashbase[2], hashpow[2] = {1, 1}, tmp[2];   std::map<std::pair<int, int>, int> mp;   std::set<int> st;   int main() {     int i, j, q;     srand(time(NULL));     scanf( %d%d , &n, &k);     len = n * k;     for (i = 1; i <= len; i++) {       do s[i] = getchar();       while (s[i] <  a  || s[i] >  z );       s[len + i] = s[i];     }     hashbase[0] = (long long)rand() * rand() % p1,     hashbase[1] = (long long)rand() * rand() % p2;     for (i = 1; i <= k; i++) {       if (i > 1)         hashpow[0] = (long long)hashpow[0] * hashbase[0] % p1,         hashpow[1] = (long long)hashpow[1] * hashbase[1] % p2;       strhash[1][0] = ((long long)strhash[1][0] * hashbase[0] % p1 + s[i]) % p1,       strhash[1][1] = ((long long)strhash[1][1] * hashbase[1] % p2 + s[i]) % p2;     }     for (i = 2; i <= len; i++)       strhash[i][0] =           ((strhash[i - 1][0] - (long long)s[i - 1] * hashpow[0] % p1 + p1) % p1 *                hashbase[0] % p1 +            s[i + k - 1]) %           p1,       strhash[i][1] =           ((strhash[i - 1][1] - (long long)s[i - 1] * hashpow[1] % p2 + p2) % p2 *                hashbase[1] % p2 +            s[i + k - 1]) %           p2;     scanf( %d , &q);     for (i = 1; i <= q; i++) {       tmp[0] = tmp[1] = 0;       for (j = 1; j <= k; j++) {         do ch = getchar();         while (ch <  a  || ch >  z );         tmp[0] = ((long long)tmp[0] * hashbase[0] % p1 + ch) % p1,         tmp[1] = ((long long)tmp[1] * hashbase[1] % p2 + ch) % p2;       }       if (mp[std::make_pair(tmp[0], tmp[1])]) return 233;       mp[std::make_pair(tmp[0], tmp[1])] = i;     }     int tmp;     for (i = 1; i <= k; i++) {       st.clear();       for (j = 0; j < n; j++) {         tmp = mp[std::make_pair(strhash[(i + j * k - 1) % len + 1][0],                                 strhash[(i + j * k - 1) % len + 1][1])];         if (!tmp || st.count(tmp))           break;         else           st.insert(tmp);       }       if (j == n) {         puts( YES );         for (j = 0; j < n; j++)           printf( %d  ,                  mp[std::make_pair(strhash[(i + j * k - 1) % len + 1][0],                                    strhash[(i + j * k - 1) % len + 1][1])]);         return 0;       }     }     puts( NO );     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int N = 1e3 + 5;   const long long inf = 9e18 + 9;   const int mod = 1e9 + 7;   long long res;   int indeg[N], outdeg[N], diam[N], adj[N];   void solve() {     int n, m;     cin >> n >> m;     for (int i = 0; i < m; i++) {       int u, v, val;       cin >> u >> v >> val;       outdeg[u]++;       indeg[v]++;       diam[u] = val;       adj[u] = v;     }     for (int i = 1; i <= n; i++) {       if (outdeg[i] > 0 && indeg[i] == 0) {         res++;       }     }     cout << res <<   n ;     for (int i = 1; i <= n; i++) {       if (outdeg[i] > 0 && indeg[i] == 0) {         int ans = INT_MAX;         int curr = i;         while (outdeg[curr]) {           ans = min(ans, diam[curr]);           curr = adj[curr];         }         cout << i <<     << curr <<     << ans <<   n ;       }     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long t = 1;     for (int i = 1; i <= t; i++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6, inf = 2e9 + 2;   vector<pair<int, int> > ad[N + 2], tree[N + 2];   int br[N + 2];   vector<int> bkad[N + 2];   int vis[N + 2], lev[N + 2];   void clear(int n, int m) {     for (int i = 1; i <= n; i++) {       tree[i].clear(), bkad[i].clear();       vis[i] = 0, lev[i] = 0;     }     for (int i = 1; i <= m; i++) br[i] = 0;   }   void dfs(int node, int par, int lv) {     vis[node] = 1, lev[node] = lv;     for (auto z : ad[node]) {       int x = z.first;       if (x == par) continue;       if (vis[x]) {         bkad[node].push_back(x);         bkad[x].push_back(node);       } else {         tree[node].push_back({x, z.second});         tree[x].push_back({node, z.second});         dfs(x, node, lv + 1);       }     }   }   int bridge(int node, int par, int nm) {     int mn = 1e9;     for (auto x : bkad[node]) mn = min(mn, lev[x]);     for (auto x : tree[node]) {       if (x.first == par) continue;       mn = min(mn, bridge(x.first, node, x.second));     }     if (mn >= lev[node] && par != -1) br[nm] = 1;     return mn;   }   vector<int> pth;   int vs[N + 2];   bool find(int nd, int tr) {     if (nd == tr) return true;     vs[nd] = 1;     for (auto x : ad[nd]) {       if (vs[x.first]) continue;       pth.push_back(x.second);       if (find(x.first, tr)) return true;       pth.pop_back();     }     return false;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     int s, t;     cin >> s >> t;     vector<vector<int> > ed(m + 1);     for (int i = 1; i <= m; i++) {       int u, v, w;       cin >> u >> v >> w;       ed[i] = {u, v, w};       ad[u].push_back({v, i});       ad[v].push_back({u, i});     }     find(s, t);     int ans = inf, one = -1, two = -1;     if ((int)pth.size() == 0) cout << 0 <<     << 0 << endl, exit(0);     vector<int> ok = pth;     for (auto x : ok) {       int u = ed[x][0], v = ed[x][1];       int id = 0;       for (auto z : ad[u]) {         if (z.second == x) {           ad[u].erase(ad[u].begin() + id);           break;         }         id++;       }       id = 0;       for (auto z : ad[v]) {         if (z.second == x) {           ad[v].erase(ad[v].begin() + id);           break;         }         id++;       }       pth.clear();       for (int i = 1; i <= n; i++) vs[i] = 0;       find(s, t);       if ((int)pth.size() == 0) {         if (ed[x][2] < ans) ans = ed[x][2], one = x, two = -1;       }       dfs(s, -1, 0);       bridge(s, -1, 0);       for (auto z : pth) {         if (br[z] == 0) continue;         if (ed[x][2] + ed[z][2] < ans)           ans = ed[x][2] + ed[z][2], one = x, two = z;       }       clear(n, m);       ad[ed[x][0]].push_back({ed[x][1], x});       ad[ed[x][1]].push_back({ed[x][0], x});     }     if (ans == inf)       cout << -1 << endl;     else {       cout << ans <<    ;       if (two == -1)         cout << 1 <<     << one << endl;       else         cout << 2 <<     << one <<     << two << endl;     }     return 0;   }


#include <bits/stdc++.h>   const int mod = 1000000007;   using namespace std;   long long gcd(long long a, long long b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   int prime[100007];   void SieveOfEratosthenes(int n) {     memset(prime, -1, sizeof(prime));     for (int p = 2; p * p <= n; p++) {       if (prime[p] == -1) {         for (int i = p * p; i <= n; i += p) prime[i] = p;       }     }   }   int lps(string s) {     int len = 0;     int n = s.length();     int dp[n];     dp[0] = 0;     for (int i = 1; i < n; i++) {       if (s[i] == s[len]) {         len++;         dp[i] = len;         continue;       }       if (len != 0) {         len = dp[len - 1];         i--;         continue;       }       dp[i] = 0;     }     return dp[n - 1];   }   long long power(unsigned long long x, unsigned long long y) {     long long res = 1;     while (y > 0) {       if (y & 1) res = res * x;       y = y >> 1;       x = x * x;     }     return res;   }   vector<pair<long long, long long>> pFactor;   void factorize(long long n) {     int count = 0;     while (!(n % 2)) {       n >>= 1;       count++;     }     if (count) pFactor.push_back({2, count});     for (long long i = 3; i <= sqrt(n); i += 2) {       count = 0;       while (n % i == 0) {         count++;         n = n / i;       }       if (count) pFactor.push_back({i, count});     }     if (n > 2) pFactor.push_back({n, 1});   }   int dp[405][405];   int solve(int a[], int n, int i, int t) {     if (i > n) {       return 0;     }     if (t > 2 * n) {       return INT_MAX;     }     if (dp[i][t] != -1) {       return dp[i][t];     }     int x = solve(a, n, i + 1, t + 1);     if (x != INT_MAX) {       x += abs(t - a[i]);     }     x = min(x, solve(a, n, i, t + 1));     dp[i][t] = x;     return x;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int a[n + 1];       for (long long i = 1; i < n + 1; i++) cin >> a[i];       sort(a + 1, a + n + 1);       memset(dp, -1, sizeof(dp));       int x = solve(a, n, 1, 1);       cout << x << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   using namespace std;   long long power(long long x, long long y) {     long long temp = 1;     while (y > 0) {       if (y % 2 == 1) temp = (x * temp) % 1000000007;       x = (x * x) % 1000000007;       y = y / 2;     }     return temp;   }   long long n;   string st;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     set<string> ans;     for (long long i = (long long)(1); i <= (long long)(n); i++) {       cin >> st;       while (1) {         long long len = st.length(), ind = -1;         for (long long i = (long long)(0); i <= (long long)(len - 2); i++) {           if (st[i] ==  k  && st[i + 1] ==  h ) {             ind = i;             break;           }         }         if (ind != -1) {           st = st.substr(0, ind) +  h  + st.substr(ind + 2);           continue;         }         for (long long i = (long long)(0); i <= (long long)(len - 1); i++) {           if (st[i] ==  u ) {             ind = i;             break;           }         }         if (ind != -1) {           st = st.substr(0, ind) +  oo  + st.substr(ind + 1);           continue;         } else           break;       }       ans.insert(st);     }     cout << ans.size() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void Braka() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   int main() {     Braka();     int n, k, ans = 0;     cin >> n >> k;     while (n--) {       set<char> st;       string s;       cin >> s;       for (int i = 0; i < s.length(); i++)         if (s[i] -  0  <= k) st.insert(s[i]);       if (st.size() == k + 1) ans++;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e6;   int n, seg[MAXN], r[MAXN], v[MAXN], resp;   void update(int ini, int fim, int cur, int id, int aux) {     if (id < ini || fim < id) return;     if (ini == fim) {       seg[cur] = aux;       r[cur] = aux;       return;     }     int m = (ini + fim) / 2;     update(ini, m, 2 * cur, id, aux);     update(m + 1, fim, 2 * cur + 1, id, aux);     seg[cur] = seg[2 * cur] + seg[2 * cur + 1];     r[cur] = max(r[2 * cur + 1], seg[2 * cur + 1] + r[2 * cur]);   }   int BB(int ini, int fim, int cur, int k) {     if (ini == fim) return ini;     int m = (ini + fim) / 2;     if (r[2 * cur + 1] < k) return BB(ini, m, 2 * cur, k - seg[2 * cur + 1]);     return BB(m + 1, fim, 2 * cur + 1, k);   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL);     ;     cin >> n;     for (int i = 0; i < n; i++) {       int auxid, auxx, aux;       cin >> auxid >> auxx;       if (!auxx)         update(1, n, 1, auxid, -1);       else {         cin >> aux;         v[auxid] = aux;         update(1, n, 1, auxid, 1);       }       resp = v[BB(1, n, 1, 1)];       if (r[1] < 1)         cout << -1 <<   n ;       else         cout << resp <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   void readi(int &x) {     int v = 0, f = 1;     char c = getchar();     while (!isdigit(c) && c !=  - ) c = getchar();     if (c ==  - )       f = -1;     else       v = v * 10 + c -  0 ;     while (isdigit(c = getchar())) v = v * 10 + c -  0 ;     x = v * f;   }   void readll(long long &x) {     long long v = 0ll, f = 1ll;     char c = getchar();     while (!isdigit(c) && c !=  - ) c = getchar();     if (c ==  - )       f = -1;     else       v = v * 10 + c -  0 ;     while (isdigit(c = getchar())) v = v * 10 + c -  0 ;     x = v * f;   }   void readc(char &x) {     char c;     while ((c = getchar()) ==    )       ;     x = c;   }   void writes(string s) { puts(s.c_str()); }   void writeln() { writes(  ); }   void writei(int x) {     if (x < 0) {       putchar( - );       x = abs(x);     }     if (!x) putchar( 0 );     char a[25];     int top = 0;     while (x) {       a[++top] = (x % 10) +  0 ;       x /= 10;     }     while (top) {       putchar(a[top]);       top--;     }   }   void writell(long long x) {     if (x < 0) {       putchar( - );       x = abs(x);     }     if (!x) putchar( 0 );     char a[25];     int top = 0;     while (x) {       a[++top] = (x % 10) +  0 ;       x /= 10;     }     while (top) {       putchar(a[top]);       top--;     }   }   inline long long inc(int &x) { return ++x; }   inline long long inc(long long &x) { return ++x; }   inline long long inc(int &x, long long y) { return x += y; }   inline long long inc(long long &x, long long y) { return x += y; }   inline double inc(double &x, double y) { return x += y; }   inline long long dec(int &x) { return --x; }   inline long long dec(long long &x) { return --x; }   inline long long dec(int &x, long long y) { return x -= y; }   inline long long dec(long long &x, long long y) { return x -= y; }   inline double dec(double &x, double y) { return x -= y; }   inline long long mul(int &x) { return x = ((long long)x) * x; }   inline long long mul(long long &x) { return x = x * x; }   inline long long mul(int &x, long long y) { return x *= y; }   inline long long mul(long long &x, long long y) { return x *= y; }   inline double mul(double &x, double y) { return x *= y; }   inline long long divi(const int &x) {     long long ans, l, r, mid;     ans = 0;     l = 0;     r = 0x3fffffff;     while (l < r) {       mid = (l + r) / 2;       if (mid * mid <= x) {         ans = mid;         l = mid + 1;       } else         r = mid;     }     return ans;   }   inline long long divi(const long long &x) {     long long ans, l, r, mid;     ans = 0;     l = 0;     r = 0x3fffffff;     while (l < r) {       mid = (l + r) / 2;       if (mid * mid <= x) {         ans = mid;         l = mid + 1;       } else         r = mid;     }     return ans;   }   inline long long divi(int &x, long long y) { return x /= y; }   inline long long divi(long long &x, long long y) { return x /= y; }   inline double divi(double &x, double y) { return x /= y; }   inline long long mod(int &x, long long y) { return x %= y; }   inline long long mod(long long &x, long long y) { return x %= y; }   long long n, m, i, j, l, r, ans[200005], f, lst;   map<long long, long long> cnt;   int main() {     readll(n);     for (i = 1; i <= n; i++) {       readll(l);       readll(r);       cnt[l]++;       cnt[r + 1]--;     }     lst = f = 0;     for (__typeof((cnt).begin()) it = (cnt).begin(); it != (cnt).end(); it++) {       ans[f] += (it->first - lst);       lst = it->first;       f += it->second;     }     if ((1) <= ((n)))       for (((i)) = (1); ((i)) <= ((n)); ((i))++) printf( %I64d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   int main() {     int t;     scanf( %d , &t);     while (t--) {       static int aa[100];       static char used[100];       int n, m, i, yes;       scanf( %d%d , &n, &m);       for (i = 0; i < n; i++) scanf( %d , &aa[i]);       memset(used, 0, (n - 1) * sizeof *used);       while (m--) {         scanf( %d , &i), i--;         if (!used[i]) used[i] = 1;       }       yes = 1;       for (i = 0; i < n; i++) {         int j, j_;         j_ = -1;         for (j = i; j < n; j++)           if (j_ == -1 || aa[j_] > aa[j]) j_ = j;         if (j_ != i) {           int k, good;           good = 1;           for (k = j_ - 1; k >= i; k--) {             int tmp;             if (!used[k]) {               good = 0;               break;             }             tmp = aa[k], aa[k] = aa[k + 1], aa[k + 1] = tmp;           }           if (!good) {             yes = 0;             break;           }         }       }       printf(yes ?  YES n  :  NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> g[200005];   queue<pair<int, int> > q;   int deg[200005], a, n;   bool v[200005];   set<int> s[200005];   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int a, b;       scanf( %d%d , &a, &b);       g[a].push_back(b);       g[b].push_back(a);       deg[a]++, deg[b]++;     }     for (int i = 1; i <= n; i++)       if (deg[i] == 1) q.push(pair<int, int>(i, 0));     int c = 0, lst = -1;     while (!q.empty()) {       int x = q.front().first, d = q.front().second;       q.pop();       v[x] = true;       lst = x, c++;       for (auto y : g[x]) {         if (v[y]) continue;         deg[y]--;         s[y].insert(d + 1);         if (deg[y] == 1 and s[y].size() == 1) q.push(pair<int, int>(y, d + 1));       }     }     if (c < n - 1)       a = -1;     else {       if (c == n - 1)         for (int i = 1; i <= n; i++)           if (!v[i]) lst = i;       if (s[lst].size() > 2)         a = -1;       else {         for (auto e : s[lst]) a += e;       }     }     while (a % 2 == 0) a /= 2;     printf( %d n , a);   }


#include <bits/stdc++.h>   int x[100005];   using namespace std;   bool cmp(int a, int b) { return a < b; }   int main() {     int n, i, q, k;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d , &x[i]);     }     sort(x, x + n, cmp);     scanf( %d , &q);     while (q--) {       int m;       scanf( %d , &m);       printf( %d n , upper_bound(x, x + n, m) - x);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1005], b[1005];   int x[1005], y[1005];   int main() {     int n;     while (scanf( %d , &n) != EOF) {       memset(x, 0, sizeof(x));       memset(y, 0, sizeof(y));       for (int i = 0; i < n; ++i) {         scanf( %d%d , &a[i], &b[i]);         x[a[i]]++;         y[b[i]]++;       }       int sum = 0;       for (int i = 0; i < n; ++i) {         if (x[b[i]]) {           if (a[i] != b[i]) {             sum += x[b[i]];             x[b[i]] = 0;           } else if (y[a[i]] > 1) {             sum += x[b[i]];             x[b[i]] = 0;           } else if (x[b[i]] > 1) {             sum += x[b[i]] - 1;             x[b[i]] = 1;           }         }       }       printf( %d n , n - sum);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-9;   const int INF = 0x3f3f3f3f;   int sgn(double a) { return ((a > EPS) ? (1) : ((a < -EPS) ? (-1) : (0))); }   int cmp(double a, double b = 0.0) { return sgn(a - b); }   struct node {     int x, y;     node(int x = 0, int y = 0) : x(x), y(y) {}   };   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     bool rated = false;     vector<int> rating;     for (int i = 0; i < n; i++) {       int x, y;       cin >> x >> y;       if (x != y)         rated = true;       else         rating.push_back(y);     }     if (rated)       cout <<  rated n ;     else {       for (int i = 0; i < n - 1; i++)         if (rating[i] < rating[i + 1]) {           cout <<  unrated n ;           return 0;         }       cout <<  maybe n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 5;   const long long mod = 998244353;   vector<long long> adj[N];   long long dp[N][4], f[N][3];   void dfs(long long x) {     if (adj[x].size() == 0) {       dp[x][2] = 1;       return;     } else {       dp[x][0] = 1;     }     for (long long i : adj[x]) {       dfs(i);       dp[x][2] = (dp[x][2] * (dp[i][0] + dp[i][2]) +                   (dp[x][1] + dp[x][2]) * (dp[i][1] + dp[i][2])) %                  mod;       dp[x][1] =           (dp[x][1] * (dp[i][0] + dp[i][2]) + dp[x][0] * (dp[i][1] + dp[i][2])) %           mod;       dp[x][0] = (dp[x][0] * (dp[i][0] + dp[i][2])) % mod;     }   }   signed main() {     long long n, i, j, k;     cin >> n;     for (i = 2; i <= n; i++) {       cin >> j;       adj[j].push_back(i);     }     dfs(1);     cout << (dp[1][0] + dp[1][2]) % mod;   }


#include <bits/stdc++.h>   int main() {     int x;     scanf( %d , &x);     if (x == 3)       printf( 5 n );     else {       for (int i = 1; i <= 100; i += 2) {         if (i * i - (i * i) / 2 >= x) {           printf( %d n , i);           break;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> a[111111], b[111111];   int n;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i].first);       a[i].second = i;     }     for (int i = 1; i <= n; i++) {       scanf( %d , &b[i].first);       b[i].second = i;     }     int m;     scanf( %d , &m);     sort(a + 1, a + 1 + n);     sort(b + 1, b + 1 + n);     long long ret = 1;     for (int i = 1, j = 1; i <= n || j <= n;) {       int val;       if (i <= n && (j > n || a[i].first <= b[j].first))         val = a[i].first;       else         val = b[j].first;       int cnt = 0, l2 = 0;       map<int, bool> f;       f.clear();       for (; i <= n && a[i].first == val; i++) {         cnt++;         f[a[i].second] = 1;       }       for (; j <= n && b[j].first == val; j++) {         cnt++;         if (f[b[j].second]) l2++;       }       for (int k = 2; k <= cnt; k++) {         int now = k;         while (now % 2 == 0 && l2) {           now /= 2;           l2--;         }         ret = (ret * now) % m;       }     }     printf( %I64d , ret);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int fact[300001];   int invfact[300001];   int pw(int a, int e) {     if (e == 0) return 1;     int tmp = pw(a, e / 2);     tmp = (tmp * ((long long)tmp)) % 1000000007;     if (e & 1) tmp = (tmp * ((long long)a)) % 1000000007;     return tmp;   }   void computeFactorials() {     fact[0] = invfact[0] = 1;     for (int i = 1; i <= 300000; i++) {       fact[i] = (fact[i - 1] * ((long long)i)) % 1000000007;       invfact[i] = pw(fact[i], 1000000007 - 2);     }   }   void mult(int &a, int b) { a = (a * ((long long)b)) % 1000000007; }   void add(int &a, int b) {     a = a + b;     if (a >= 1000000007) a -= 1000000007;   }   void sub(int &a, int b) {     a = a + 1000000007 - b;     if (a >= 1000000007) a -= 1000000007;   }   int chs(int n, int k) {     if (k > n) return 0;     int tmp = (fact[n] * ((long long)invfact[k])) % 1000000007;     tmp = (tmp * ((long long)invfact[n - k])) % 1000000007;     return tmp;   }   vector<int> pcs[300001];   int A[300000];   int occ[20];   int ps[20];   int N, C;   int tmp[300001];   int computePrime(int p) {     for (int i = 0; i < 20; i++) occ[i] = 0;     for (int j = 0; j < pcs[p].size(); j++) {       int l = 0;       while (pcs[p][j] % p == 0) {         l++;         pcs[p][j] /= p;       }       occ[l]++;     }     occ[0] = N - pcs[p].size();     int numLess = 0;     int numMore;     int ans = 0;     for (int k = 0; k < 19; k++) {       numLess += occ[k];       numMore = N - numLess;       tmp[0] = 0;       for (int i = 1; i < numMore; i++) {         tmp[i] = i;         mult(tmp[i], chs(numLess, i));         add(tmp[i], tmp[i - 1]);         int cur = tmp[i];         mult(cur, chs(numMore, i + 1));         add(ans, cur);       }       tmp[0] = pw(2, numLess);       for (int i = 1; i <= numMore; i++) {         tmp[i] = tmp[i - 1];         sub(tmp[i], chs(numLess, i - 1));         int cur = tmp[i];         mult(cur, chs(numMore, i));         mult(cur, i);         add(ans, cur);       }     }     return ans;   }   int main() {     cin >> N;     computeFactorials();     for (int i = 0; i < N; i++) {       cin >> A[i];       int c = A[i];       for (int j = 2; j * j <= A[i]; j++) {         if (c % j == 0) {           pcs[j].push_back(A[i]);           while (c % j == 0) c /= j;         }       }       if (c > 1) {         pcs[c].push_back(A[i]);       }     }     int ans = 0;     for (int i = 1; i <= 300000; i++)       if (pcs[i].size() > 0) add(ans, computePrime(i));     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = (1e9) + 7;   const long double EPS = 0.0000001;   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     int pa;     cin >> pa;     deque<int> arr;     int ans = 0;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       if (i == pa - 1) {         if (x != 0) {           x = 0;           ans++;         }       }       arr.push_back(x);     }     sort(arr.begin(), arr.end());     arr.pop_front();     int cur = 0;     while (arr.front() == 0) {       arr.pop_front();       arr.push_back(INT_MAX);     }     while (arr.size()) {       cur++;       int x = arr.front();       if (x == cur) {         while (arr.size() && arr.front() == cur) arr.pop_front();         continue;       }       while (arr.size()) {         x = arr.front();         if (cur == x) {           cur--;           break;         }         arr.pop_back();         cur++;         ans++;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t, n, m;   int a[10][10];   int main() {     cin >> t;     while (t--) {       cin >> n >> m;       int ok = 0;       for (int i = 1; i <= n; i++) {         cin >> a[1][1] >> a[1][2] >> a[2][1] >> a[2][2];         if (a[1][2] == a[2][1]) {           ok = 1;         }       }       if (m % 2) ok = 0;       if (ok)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     int n;     cin >> t;     for (int u = 0; u < t; u++) {       cin >> n;       int arr[n];       for (int i = 0; i < n; i++) cin >> arr[i];       vector<int> v;       int mini = arr[n - 1];       for (int i = (n - 2); i >= 0; i--) {         if (mini < arr[i])           v.push_back(arr[i]);         else           mini = arr[i];       }       cout << v.size() <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000000 + 7;   const int N = 100000 + 50;   vector<int> a;   int val[N];   int p[N];   int rp[N];   int avl[N];   pair<int, int> dp[N];   int curn;   pair<int, int> bit[N];   vector<vector<int> > fa;   void clear_bit(int n) {     for (int i = 0; i < n + 2; i++) {       bit[i] = {0, -1};     }   }   void upd(int i, int x, int y) {     while (i < curn) {       if (x > bit[i].first) {         bit[i] = {x, y};       }       i += i & (-i);     }   }   pair<int, int> get(int i) {     pair<int, int> ans = {0, 0};     while (i > 0) {       if (bit[i].first > ans.first) {         ans = bit[i];       }       i -= i & (-i);     }     return ans;   }   void build() {     int n = a.size();     if (n == 0) return;     clear_bit(n + 3);     curn = n + 2;     pair<int, int> vl = {0, 0};     for (int i = 1; i <= n; i++) {       avl[i] = 1;       auto an = get(a[i - 1]);       dp[i] = {an.first + 1, an.second};       if (dp[i].first > vl.first) {         vl = {dp[i].first, i};       }       upd(a[i - 1], dp[i].first, i);     }     if (val[n - vl.first] < val[n]) {       vector<int> ans;       int cur = vl.second;       while (cur > 0) {         avl[a[cur - 1]] = 0;         ans.push_back(p[a[cur - 1]]);         cur = dp[cur].second;       }       reverse(ans.begin(), ans.end());       fa.push_back(ans);       int c = 0;       for (int i = 1; i <= n; i++) {         c += avl[i];         if (avl[i] == 1) {           p[c] = p[i];           rp[i] = c;         }       }       vector<int> temp;       for (int i = 1; i <= n; i++) {         if (avl[a[i - 1]]) {           temp.push_back(rp[a[i - 1]]);         }       }       a = temp;       build();       return;     } else {       vector<int> v[n + 5];       for (int i = 1; i <= n; i++) {         v[dp[i].first].push_back(p[a[i - 1]]);       }       for (int i = 0; i <= n; i++) {         if (v[i].size()) fa.push_back(v[i]);       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int cur = 2;     for (int i = 1; i < N; i++) {       for (int j = 0; j < cur && i + j < N; j++) {         val[i + j] = cur - 1;       }       i += cur - 1;       cur++;     }     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       a.clear();       for (int i = 1; i <= n; i++) {         p[i] = i;         int x;         cin >> x;         a.push_back(x);       }       fa.clear();       build();       cout << fa.size() << endl;       for (auto& it : fa) {         cout << it.size() <<    ;         for (auto it1 : it) cout << it1 <<    ;         cout << endl;       }     }   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   template <typename T>   T gcd(T a, T b) {     return b == 0 ? a : gcd(b, a % b);   }   const int maxn = 1e5 + 5;   const long long mod = 1e9 + 7;   int Case = 1;   int n, m;   struct node {     int x, y, xx, yy;   } a, b, c;   vector<int> A, B;   int getid(int x, int id) {     if (id == 1)       return lower_bound(((A).begin()), ((A).end()), x) - A.begin() + 1;     else       return lower_bound(((B).begin()), ((B).end()), x) - B.begin() + 1;   }   int maze[20][20];   void solve() {     cin >> a.x >> a.y >> a.xx >> a.yy;     cin >> b.x >> b.y >> b.xx >> b.yy;     cin >> c.x >> c.y >> c.xx >> c.yy;     A.push_back(a.x * 2);     A.push_back(a.xx * 2);     A.push_back(a.x * 2 + 1);     A.push_back(a.xx * 2 + 1);     B.push_back(a.y * 2);     B.push_back(a.yy * 2);     B.push_back(a.y * 2 + 1);     B.push_back(a.yy * 2 + 1);     A.push_back(b.x * 2);     A.push_back(b.xx * 2);     A.push_back(b.x * 2 + 1);     A.push_back(b.xx * 2 + 1);     B.push_back(b.y * 2);     B.push_back(b.yy * 2);     B.push_back(b.y * 2 + 1);     B.push_back(b.yy * 2 + 1);     A.push_back(c.x * 2);     A.push_back(c.xx * 2);     A.push_back(c.x * 2 + 1);     A.push_back(c.xx * 2 + 1);     B.push_back(c.y * 2);     B.push_back(c.yy * 2);     B.push_back(c.y * 2 + 1);     B.push_back(c.yy * 2 + 1);     sort(((A).begin()), ((A).end()));     A.erase(unique(((A).begin()), ((A).end())), A.end());     sort(((B).begin()), ((B).end()));     B.erase(unique(((B).begin()), ((B).end())), B.end());     int x = getid(b.x * 2, 1), xx = getid(b.xx * 2, 1);     int y = getid(b.y * 2, 2), yy = getid(b.yy * 2, 2);     for (int i = x; i <= xx; i++) {       for (int j = y; j <= yy; j++) {         maze[i][j] = 1;       }     }     x = getid(c.x * 2, 1), xx = getid(c.xx * 2, 1);     y = getid(c.y * 2, 2), yy = getid(c.yy * 2, 2);     for (int i = x; i <= xx; i++) {       for (int j = y; j <= yy; j++) {         maze[i][j] = 1;       }     }     x = getid(a.x * 2, 1), xx = getid(a.xx * 2, 1);     y = getid(a.y * 2, 2), yy = getid(a.yy * 2, 2);     for (int i = x; i <= xx; i++) {       for (int j = y; j <= yy; j++) {         if (maze[i][j] == 0) {           cout <<  YES n ;           return;         }       }     }     cout <<  NO n ;     return;   }   int main() {     while (Case--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void calc() {     long long n, m;     cin >> n >> m;     vector<pair<long long, long long> > v;     vector<long long> a;     for (long long i = 0; i < m; i++) {       long long ai, bi;       cin >> ai >> bi;       v.push_back(make_pair(ai, bi));       a.push_back(ai);     }     sort(a.begin(), a.end());     reverse(a.begin(), a.end());     vector<long long> acc(m + 2);     for (long long i = 1; i <= m; i++) {       acc[i] = acc[i - 1] + a[i - 1];     }     long long ans = -1;     for (long long i = 0; i < m; i++) {       long long ai = v[i].first;       long long bi = v[i].second;       long long lo, hi;       lo = 0;       hi = m;       while (lo + 1 != hi) {         long long mid = (lo + hi) / 2;         if (a[mid] > bi)           lo = mid;         else           hi = mid;       }       long long curr, rem;       if (a[lo] > bi) {         curr = ai;         rem = n - 1;         if (rem > 0) {           long long used = min(lo + 1, rem);           curr += acc[used];           if (a[used - 1] <= ai) {             curr -= ai;             rem -= used;             rem++;             if (used + 1 <= m && a[used] > bi) {               rem--;               curr += a[used];             }           } else {             rem -= used;           }           if (rem > 0) curr += rem * bi;         }       } else {         curr = ai;         rem = n - 1;         if (rem > 0) curr += rem * bi;       }       ans = max(ans, curr);     }     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) calc();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 7;   const int inf = INT_MAX;   const long long inff = 5e18;   const long long mod = 1e9 + 7;   long long f[7];   long long dp[maxn], dp2[maxn];   vector<long long> value = {0, 0, 0, 1, 0, 0, 2, 0, 0, 3};   void update(long long x, long long dd) {     for (long long i = 999999; i >= 0; i--)       if (i >= x) dp[i] = max(dp[i], dp[i - x] + dd);   }   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long k;     cin >> k;     for (long long i = 0; i < 6; i++) cin >> f[i];     for (long long i = 1; i <= 999999; i++) dp[i] = -inff;     long long now = 3;     for (long long i = 0; i < 6; i++) {       long long cnt = 3 * (k - 1);       for (long long j = 1;; j <<= 1) {         if (j >= cnt) {           update(now * cnt, f[i] * cnt);           break;         } else           update(now * j, f[i] * j), cnt -= j;       }       now = now * 10;     }     now = 1;     for (long long i = 0; i < 6; i++) {       for (long long k = 0; k <= 999999; k++) dp2[k] = dp[k];       for (long long j = 0; j < 10; j++) {         for (long long k = 1; k <= 999999; k++)           if (k >= now * j)             dp2[k] = max(dp2[k], dp[k - j * now] + value[j] * f[i]);       }       for (long long k = 0; k <= 999999; k++) dp[k] = max(dp2[k], dp[k]);       now = now * 10;     }     long long q;     cin >> q;     while (q--) {       long long x;       cin >> x;       cout << dp[x] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, i;   const int maxn = 1e5 + 20;   int arr[maxn];   int main() {     cin >> n >> m;     int x = m;     if (m <= n * 2) {       for (int i = 1; i <= m; i++) {         cout << i <<    ;       }     } else {       for (i = 1; i <= (m - n * 2); i++) {         cout << i + (n * 2) <<     << i <<    ;         arr[i + (n * 2)]++, arr[i]++;       }       for (int i = 1; i <= m; i++) {         if (arr[i] == 0) cout << i <<    ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e18;   long long dp[10000005], sol = 0;   int main() {     int n, i;     long long x, y;     scanf( %d%I64d%I64d , &n, &x, &y);     for (i = 1; i <= 10000005; i++) dp[i] = inf;     dp[1] = x;     for (i = 2; i <= n; i++) {       dp[i] = min(dp[i], dp[i - 1] + x);       if (i % 2 == 0) dp[i] = min(dp[i], dp[i / 2] + y);       if (i % 2 == 1) {         dp[i] = min(dp[i], dp[(i - 1) / 2] + y + x);         dp[i] = min(dp[i], dp[(i + 1) / 2] + y + x);       }     }     printf( %I64d n , dp[n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int pen[2000005];   void update(int ind, int add) {     while (ind < 2000005) {       pen[ind] += add;       ind += ind & (-ind);     }   }   int query(int ind) {     int ret = 0;     while (ind > 0) {       ret = ret + pen[ind];       ind = ind & (ind - 1);     }     return ret;   }   pair<int, int> r[2000005];   bool vis[2000005];   int getIndex(int y) {     int sum = 0;     int ind = 0;     for (int i = 20; i >= 0; --i) {       int nind = ind + (1 << i);       if (nind < 2000005) {         int nsum = sum + pen[nind];         if (nsum < y) {           sum = nsum;           ind = nind;         }       }     }     return ind + 1;   }   int out[2000005];   int mapping[2000005];   void solve() {     int n, m;     scanf(  %d %d  , &n, &m);     int pos = m + 1;     for (int i = 1; i <= n; ++i) {       r[pos + i] = {-1, i};       update(pos + i, 1);     }     for (int i = 0; i < m; ++i) {       int x, y;       scanf( %d %d  , &x, &y);       int ind = getIndex(y);       if (vis[x]) {         if (r[ind].first != x) {           printf( -1 n );           return;         }       } else {         if (r[ind].first != -1) {           printf( -1 n );           return;         }       }       update(ind, -1);       update(pos, 1);       r[pos] = r[ind];       r[pos].first = x;       vis[x] = true;       --pos;     }     int p = 1;     for (int i = 1; i <= n; ++i) {       int ind = getIndex(i);       if (r[ind].first == -1) {         while (p <= n) {           if (vis[p]) {             ++p;           } else {             break;           }         }         r[ind].first = p++;       }       out[r[ind].second] = r[ind].first;     }     for (int i = 1; i <= n; ++i) {       printf( %d  , out[i]);     }   }   int main() {     solve();     return 0;   }


#include<bits/stdc++.h>   #define For(i,x,y) for (register int i=(x);i<=(y);i++)   #define FOR(i,x,y) for (register int i=(x);i<(y);i++)   #define Dow(i,x,y) for (register int i=(x);i>=(y);i--)   #define Debug(v) for (auto i:v) cout<<i<<   ;puts(  )   #define mp make_pair   #define fi first   #define se second   #define pb push_back   #define ep emplace_back   #define siz(x) ((int)(x).size())   #define all(x) (x).begin(),(x).end()   #define fil(a,b) memset((a),(b),sizeof(a))   using namespace std;   typedef long long ll;   typedef unsigned long long ull;   typedef pair<int,int> pa;   typedef pair<ll,ll> PA;   typedef vector<int> poly;   inline ll read(){    ll x=0,f=1;char c=getchar();    while ((c< 0 ||c> 9 )&&(c!= - )) c=getchar();    if (c== - ) f=-1,c=getchar();    while (c>= 0 &&c<= 9 ) x=x*10+c- 0 ,c=getchar();    return x*f;   }      const int N = 5010;   int n,p[N];   pa a[N];      inline pa operator - (pa a,pa b){    return mp(a.fi-b.fi,a.se-b.se);   }   inline bool check(pa a,pa b,pa c){    pa A=a-b,B=c-b;    return (1ll*A.fi*B.fi+1ll*A.se*B.se)>0;   }      int main(){    n=read();    For(i,1,n) a[i].fi=read(),a[i].se=read(),p[i]=i;    For(i,3,n){     Dow(j,i,3) if (!check(a[p[j-2]],a[p[j-1]],a[p[j]])){      swap(p[j],p[j-1]);     }    }    For(i,1,n) printf( %d  ,p[i]);   }


#include <bits/stdc++.h>   using namespace std;   bool compare(const pair<int, int> &i, const pair<int, int> &j) {     return i.second > j.second;   }   int n, d;   vector<pair<long long, long long>> a;   long long p(long long a, long long b) {     long long x = 1, y = a;     while (b > 0) {       if (b % 2 == 1) {         x = (x * y) % 998244353;       }       y = (y * y) % 998244353;       b /= 2;     }     return x % 998244353;   }   long long inv(long long a) { return p(a, 998244353 - 2); }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     string s;     cin >> s;     map<char, int> m;     vector<char> v;     for (int i = 0; i < s.length(); i++) {       m[s[i]]++;     }     for (auto x : m)       if (x.second % 2) v.push_back(x.first);     for (int i = v.size() - 1, j = 0; i > j; i--, j++) {       if (i <= j) break;       m[v[j]]++, m[v[i]]--;     }     string str =   ;     char flag =  0 ;     for (auto x : m) {       if (!x.second) continue;       if (x.second % 2) flag = x.first;       for (int i = 0; i < (x.second) / 2; i++) {         str += x.first;       }     }     string ans = str;     if (flag !=  0 ) {       ans += flag;     }     reverse(str.begin(), str.end());     ans += str;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long int> g[100005];   long long int vis[100005];   vector<double> ans;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t = 1;     cin >> t;     while (t--) {       long long int a, b, c, d;       cin >> a >> b >> c >> d;       if ((b - a) % (c + d) == 0)         cout << (b - a) / (c + d);       else         cout << -1;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0), cin.tie(0);     long long n, k;     cin >> n >> k;     long long a[k];     map<long long, bool> m;     for (long long i = 0; i < k; i++) {       cin >> a[i];       m[a[i]] = true;     }     long long cnt = 1;     for (long long i = 0; i < k; i++) {       cout << a[i];       for (long long i = 0; i < n - 1; i++) {         if (!m[cnt])           cout <<     << cnt;         else           i--;         cnt++;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   const int mx = 1e6 + 5;   const int inf = 0x3f3f3f3f;   using namespace std;   vector<int> ans;   int dfs(long long int a, long long int b) {     if (a > b) return 0;     if (a == b) return 1;     if (dfs(a * 2, b)) {       ans.push_back(2 * a);       return 1;     }     if (dfs(a * 10 + 1, b)) {       ans.push_back(a * 10 + 1);       return 1;     }     return 0;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int a, b;     cin >> a >> b;     if (dfs(a, b)) {       printf( YES n%d n%d  , int(1 + ans.size()), a);       for (int i = ans.size() - 1; i >= 0; --i) printf( %d  , ans[i]);       puts(  );     } else       puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int sqr(int x) { return x * x; }   int main() {     int n, m, x, y;     map<int, int> targets;     vector<int> ans, r;     scanf( %d , &n);     ans.assign(n, -1);     r.assign(n, 0);     for (int i = 0; i != n; ++i) {       scanf( %d%d , &x, &r[i]);       targets[x] = i;     }     scanf( %d , &m);     for (int i = 1; i <= m; ++i) {       scanf( %d%d , &x, &y);       auto target = targets.lower_bound(x);       if (target != targets.end()) {         if (ans[target->second] == -1 &&             sqr(target->first - x) + sqr(y) <= sqr(r[target->second]))           ans[target->second] = i;       }       if (target != targets.begin()) {         --target;         if (ans[target->second] == -1 &&             sqr(target->first - x) + sqr(y) <= sqr(r[target->second]))           ans[target->second] = i;       }     }     printf( %d n , n - count(ans.begin(), ans.end(), -1));     for (auto itans = ans.begin(); itans != ans.end(); ++itans)       printf( %d  , *itans);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char** argv) {     int x0, y0, x, y, n;     int i, j, r, a, b;     set<pair<int, int> > s;     queue<pair<int, pair<int, int> > > q;     cin >> x0 >> y0 >> x >> y >> n;     for (i = 0; i < n; i++) {       cin >> r >> a >> b;       for (j = a; j <= b; j++) s.insert(make_pair(r, j));     }     q.push(make_pair(0, make_pair(x0, y0)));     while (!q.empty()) {       int cr = q.front().first;       int cx = q.front().second.first;       int cy = q.front().second.second;       q.pop();       for (int i = -1; i <= 1; i++)         for (int j = -1; j <= 1; j++) {           if (cx + i == x && cy + j == y) {             cout << cr + 1 << endl;             return 0;           }           if (i == 0 && j == 0) continue;           if (s.count(make_pair(cx + i, cy + j)) > 0) {             q.push(make_pair(cr + 1, make_pair(cx + i, cy + j)));             s.erase(make_pair(cx + i, cy + j));           }         }     }     cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mx = 1e6 + 10, inf = 1e9 + 10;   long long int t, n, ans, a[mx];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> t;     while (t--) {       cin >> n;       cout << (n + 1) / 10 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[10000];   int n = 0;   void init() {     queue<string> q;     q.push( 4 );     q.push( 7 );     while (!q.empty()) {       string u = q.front();       q.pop();       arr[n++] = stoi(u);       if (u.length() < 9) {         q.push(u +  4 );         q.push(u +  7 );       }     }   }   int main() {     ios_base::sync_with_stdio(false);     init();     int t = 1;     while (t--) {       int k;       cin >> k;       cout << lower_bound(arr, arr + n, k) - arr + 1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void mod(string &s) {     for (int i = 0; i < (int)s.size(); i++) {       switch (s[i]) {         case  N :           s[i] =  S ;           break;         case  S :           s[i] =  N ;           break;         case  E :           s[i] =  W ;           break;         case  W :           s[i] =  E ;           break;       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     string a, b;     cin >> a >> b;     n = (int)a.size();     reverse(b.begin(), b.end());     mod(b);     b += a;     vector<int> pref(2 * n, 0);     int i = 1, j = 0;     while (i < 2 * n) {       if (b[j] == b[i]) {         pref[i++] = ++j;       } else {         while (j != 0 && b[j] != b[i]) j = pref[j - 1];         if (b[j] != b[i]) i++;       }     }     if (pref[2 * n - 1])       cout <<  NO  << endl;     else       cout <<  YES  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   struct fastio {     char s[100000];     int it, len;     fastio() { it = len = 0; }     inline char get() {       if (it < len) return s[it++];       it = 0;       len = fread(s, 1, 100000, stdin);       if (len == 0)         return EOF;       else         return s[it++];     }     bool notend() {       char c = get();       while (c ==     || c ==   n ) c = get();       if (it > 0) it--;       return c != EOF;     }   } _buff;   inline long long getnum() {     long long r = 0;     bool ng = 0;     char c;     c = _buff.get();     while (c !=  -  && (c <  0  || c >  9 )) c = _buff.get();     if (c ==  - ) ng = 1, c = _buff.get();     while (c >=  0  && c <=  9 ) r = r * 10 + c -  0 , c = _buff.get();     return ng ? -r : r;   }   template <class T>   inline void putnum(T x) {     if (x < 0) putchar( - ), x = -x;     register short a[20] = {}, sz = 0;     while (x) a[sz++] = x % 10, x /= 10;     if (sz == 0) putchar( 0 );     for (int i = sz - 1; i >= 0; i--) putchar( 0  + a[i]);   }   inline char getreal() {     char c = _buff.get();     while (c <= 32) c = _buff.get();     return c;   }   long long qpow(long long x, long long k) {     return k == 0 ? 1                   : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod;   }   const int maxn = 300111;   int n, q;   int a[maxn];   void makev(vector<pair<int, int> > &v) {     sort(v.begin(), v.end());     int c = 0;     vector<pair<int, int> > nv;     for (auto x : v) {       if ((c | x.second) > c) {         c = c | x.second;         nv.push_back(make_pair(x.first, c));       }     }     v = nv;   }   vector<pair<int, int> > v[maxn];   int lst[20];   int main() {     n = getnum(), q = getnum();     for (int i = 1; i <= n; i++) a[i] = getnum();     for (int i = n; i >= 1; i--) {       vector<pair<int, int> > &cur = v[i];       cur.push_back(make_pair(i, a[i]));       for (int j = 0; j < 20; j++) {         if ((a[i] >> j) & 1) {           if (lst[j]) {             for (auto x : v[lst[j]]) cur.push_back(x);           }           lst[j] = i;         }       }       makev(cur);     }     for (int i = 1; i <= q; i++) {       int x, y;       x = getnum(), y = getnum();       bool ok = false;       for (auto t : v[x]) {         ok |= t.first <= y && ((t.second & a[y]) > 0);       }       puts(ok ?  Shi  :  Fou );     }     return 0;   }