#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   const int nax = 1e6 + 10;   long long fac[nax], ifac[nax], inv[nax];   long long ncr(int n, int k) {     if (n < 0 || k < 0 || k > n) return 0;     return fac[n] * ifac[k] % mod * ifac[n - k] % mod;   }   int main() {     fac[0] = ifac[0] = 1;     for (int i = 1; i < nax; i++) {       inv[i] = i == 1 ? 1 : mod - mod / i * inv[mod % i] % mod;       fac[i] = fac[i - 1] * i % mod;       ifac[i] = ifac[i - 1] * inv[i] % mod;       assert(fac[i] * ifac[i] % mod == 1);     }     ios::sync_with_stdio(0);     cin.tie(0);     string s;     cin >> s;     int n = s.size();     int lp = 0, rp = count(s.begin(), s.end(),  ) );     int lq = 0, rq = count(s.begin(), s.end(),  ? );     long long ans = 0;     for (int i = 0; i < n; i++) {       (ans += ncr(lq + rq, rq - lp + rp) * lp +               ncr(lq + rq - 1, rq - 1 - lp + rp) * lq) %= mod;       if (s[i] ==  ( )         lp++;       else if (s[i] ==  ) )         rp--;       else if (s[i] ==  ? )         lq++, rq--;       else         assert(0);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long T, i, j, n;     char c[2], ca[4] = { v ,  < ,  ^ ,  > }, a, b;     cin >> a >> b;     map<char, long long> m;     m[ca[0]] = 1;     m[ca[1]] = 2;     m[ca[2]] = 3;     m[ca[3]] = 4;     cin >> n;     n = n % 4;     if (n == 0)       cout <<  undefined  << endl;     else if (m[a] <= m[b]) {       if (m[b] - m[a] == 2)         cout <<  undefined  << endl;       else if (m[b] - m[a] == n)         cout <<  cw  << endl;       else if (4 - m[b] + m[a] == n)         cout <<  ccw  << endl;       else         cout <<  undefined  << endl;     } else {       if (m[a] - m[b] == 2)         cout <<  undefined  << endl;       else if (4 - m[a] + m[b] == n)         cout <<  cw  << endl;       else if (m[a] - m[b] == n)         cout <<  ccw  << endl;       else         cout <<  undefined  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-9;   const double PI = acos(-1);   const int oo = 1e9, bpr = 1e9 + 7, N = 0;   long long f[110];   int a[110];   long long d[110][4][4];   int n;   inline long long calc(int ps, int v1, int v2) {     if (ps == n) return v1 == 0 && v2 == 0;     if (d[ps][v1][v2] != -1) return d[ps][v1][v2];     long long res = 0;     for (int i = 0; i < 2; ++i) {       int x = min(v1, v2 + i);       for (int j = 0; j <= x; ++j) {         if (v1 - j == a[ps]) {           res += calc(ps + 1, v2 + i - j, j);         }       }     }     return d[ps][v1][v2] = res;   }   int main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     f[0] = f[1] = 1;     for (n = 2; f[n - 1] <= 1e18; ++n) f[n] = f[n - 1] + f[n - 2];     --n;     int t;     cin >> t;     while (t--) {       long long x;       cin >> x;       int cnt = 0;       for (int i = n; i >= 1; --i) {         a[i] = (f[i] <= x);         if (a[i]) x -= f[i];       }       memset(d, -1, sizeof(d));       cout << calc(0, 0, 0) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int level[26];     for (int i = 0; i < 26; i++) cin >> level[i];     string s;     cin >> s;     long long ans = 0;     for (char ch =  a ; ch <=  z ; ch++) {       map<long long, long long> m;       long long sum = 0;       for (int i = 0; i < s.length(); i++) {         sum += level[s[i] -  a ];         if (s[i] == ch) ans += m[sum - level[ch -  a ]], m[sum]++;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   void stderr_printf(const char *fmt, ...) {     va_list ap;     va_start(ap, fmt);     vfprintf(stderr, fmt, ap);     va_end(ap);   }   using namespace std;   struct flist {     int val;     int pos;   };   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int i, j, k;     int n;     cin >> n;     ;     int stage = 0;     bool ans = true;     cin >> j;     ;     for (i = 0; i < n - 1; i++) {       int cur;       cin >> cur;       ;       if (stage == 0) {         if (cur == j) {           stage = 1;         } else if (cur < j) {           stage = 2;         } else if (cur > j) {           stage = 0;         }       } else if (stage == 1) {         if (cur == j) {           stage = 1;         } else if (cur < j) {           stage = 2;         } else if (cur > j) {           ans = false;         }       } else {         if (cur >= j) {           ans = false;         }       }       j = cur;     }     if (ans) {       printf( YES n );     } else {       printf( NO n );     }   }


#include <bits/stdc++.h>   using namespace std;   priority_queue<pair<long long, int>, vector<pair<long long, int> >,                  greater<pair<long long, int> > >       q;   struct node {     int left, right, num[3];   } tree[4 * 100010];   struct NODE {     int begin, end, value, next;   } edge[50 * 100010 + 10];   int cnt, Head[2 * 4 * 100010 + 10], lcc[3], SIZE, cc[3][100010];   long long dis[2 * 4 * 100010 + 10];   int read() {     int s = 0, fh = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) fh = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       s = s * 10 + (ch -  0 );       ch = getchar();     }     return s * fh;   }   void addedge(int bb, int ee, int vv) {     edge[++cnt].begin = bb;     edge[cnt].end = ee;     edge[cnt].value = vv;     edge[cnt].next = Head[bb];     Head[bb] = cnt;   }   void addedge1(int bb, int ee, int vv) {     addedge(bb, ee, vv);     addedge(ee, bb, vv);   }   void Build(int k, int l, int r) {     tree[k].left = l;     tree[k].right = r;     tree[k].num[1] = ++SIZE;     tree[k].num[2] = ++SIZE;     if (l == r) {       addedge(tree[k].num[1], tree[k].num[2], 0);       return;     }     int mid = (l + r) / 2;     Build(k * 2, l, mid);     Build(k * 2 + 1, mid + 1, r);     addedge(tree[k].num[1], tree[k * 2].num[1], 0);     addedge(tree[k].num[1], tree[k * 2 + 1].num[1], 0);     addedge(tree[k * 2].num[2], tree[k].num[2], 0);     addedge(tree[k * 2 + 1].num[2], tree[k].num[2], 0);   }   void Find(int k, int ql, int qr, int F) {     if (ql <= tree[k].left && tree[k].right <= qr) {       cc[F][++lcc[F]] = tree[k].num[F];       return;     }     int mid = (tree[k].left + tree[k].right) / 2;     if (qr <= mid)       Find(k * 2, ql, qr, F);     else if (ql > mid)       Find(k * 2 + 1, ql, qr, F);     else {       Find(k * 2, ql, mid, F);       Find(k * 2 + 1, mid + 1, qr, F);     }   }   void dijkstra(int start) {     int i, u, v;     for (i = 1; i <= SIZE; i++) dis[i] = 1LL << 60;     dis[start] = 0LL;     q.push(make_pair(dis[start], start));     while (!q.empty()) {       u = q.top().second;       q.pop();       for (i = Head[u]; i != -1; i = edge[i].next) {         v = edge[i].end;         if (dis[v] > dis[u] + (long long)edge[i].value) {           dis[v] = dis[u] + (long long)edge[i].value;           q.push(make_pair(dis[v], v));         }       }     }   }   int main() {     int N, Q, S, i, zs, s1, s2, s3, s4, j;     N = read();     Q = read();     S = read();     memset(Head, -1, sizeof(Head));     cnt = 1;     SIZE = 0;     Build(1, 1, N);     for (i = 1; i <= Q; i++) {       zs = read();       if (zs == 1) {         s1 = read();         s2 = read();         s3 = read();         lcc[1] = lcc[2] = 0;         Find(1, s1, s1, 2);         Find(1, s2, s2, 1);         addedge(cc[2][1], cc[1][1], s3);       } else if (zs == 2) {         s1 = read();         s2 = read();         s3 = read();         s4 = read();         lcc[1] = lcc[2] = 0;         Find(1, s1, s1, 2);         Find(1, s2, s3, 1);         for (j = 1; j <= lcc[1]; j++) addedge(cc[2][1], cc[1][j], s4);       } else {         s1 = read();         s2 = read();         s3 = read();         s4 = read();         lcc[1] = lcc[2] = 0;         Find(1, s2, s3, 2);         Find(1, s1, s1, 1);         for (j = 1; j <= lcc[2]; j++) addedge(cc[2][j], cc[1][1], s4);       }     }     lcc[2] = 0;     Find(1, S, S, 2);     dijkstra(cc[2][1]);     for (i = 1; i <= N; i++) {       lcc[2] = 0;       Find(1, i, i, 2);       printf( %lld  , dis[cc[2][1]] == 1LL << 60 ? -1LL : dis[cc[2][1]]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct lex_compare {     bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) const {       return lhs.second > rhs.second;     }   };   void solve() {     int n;     cin >> n;     vector<long long> v(n);     for (int i = 0; i < n; i++) {       cin >> v[i];     }     sort(v.begin(), v.end());     if (n >= 60) {       long long ans = 0;       for (int i = 0; i < n; i++) {         ans += abs(v[i] - 1ll);       }       cout << ans <<   n ;     } else {       int k = 1;       while (true) {         if ((long long)pow(k, n - 1) > v[n - 1]) {           break;         }         k += 1;       }       long long ans1 = 0, ans2 = 0;       for (int i = 0; i < n; i++) {         ans1 += abs(v[i] - (long long)pow(k, i));         ans2 += abs(v[i] - (long long)pow(k - 1, i));       }       cout << min(ans1, ans2) <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n = 1;     for (int i = 0; i < n; i++) {       solve();     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target(         sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native )   using namespace std;   long long read() {     long long x = 0, f = 1;     char ch = getchar();     while (ch >  9  || ch <  0 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   void write(long long x) {     if (x < 0) {       putchar( - );       x = -x;     }     if (x >= 10) write(x / 10);     putchar(x % 10 +  0 );   }   long long n, A, B, x, y;   long long ans;   string a, b;   bool flag = 0;   void DOIT() {     n = read();     A = read();     B = read();     A++;     B++;     x = 0;     y = 0;     cin >> a >> b;     if (A > B) {       if (a ==  left )         A--;       else         A++;       if (A == 0) A = 2;       if (A == n + 1) A = n - 1;       swap(A, B);       swap(a, b);     }     if (A == 1 && B == 1) {       if (1)         cout << n - 1 <<   n ;       else         cout << 0 <<   n ;       return;     }     if (A == 1 && B == 2) {       if (0)         cout << n - 1 <<   n ;       else         cout << 0 <<   n ;       return;     }     if (A == n) {       if (0)         cout << n - 1 <<   n ;       else         cout << 0 <<   n ;       return;     }     if (A == n - 1) {       if (0)         cout << n - 1 <<   n ;       else         cout << 0 <<   n ;       return;     }     if (A < B) {       if (a ==  right  && b ==  left ) {         if ((A + B + 1) % 2)           cout << n - 1 <<   n ;         else           cout << 0 <<   n ;         return;       }       if (a ==  left  && b ==  left ) {         B -= A - 1;         A = 1;         if (A == 1 && B == 1) {           if (1)             cout << n - 1 <<   n ;           else             cout << 0 <<   n ;           return;         }         if (A == 1 && B == 2) {           if (0)             cout << n - 1 <<   n ;           else             cout << 0 <<   n ;           return;         }         {           if ((A + B + 1) % 2)             cout << n - 1 <<   n ;           else             cout << 0 <<   n ;           return;         }       }       if (a ==  right  && b ==  right ) {         A += n - B;         B = n;         if (A == n) {           if (0)             cout << n - 1 <<   n ;           else             cout << 0 <<   n ;           return;         }         if (A == n - 1) {           if (0)             cout << n - 1 <<   n ;           else             cout << 0 <<   n ;           return;         }         {           if ((A + B + 1) % 2)             cout << n - 1 <<   n ;           else             cout << 0 <<   n ;           return;         }       }       if (a ==  left  && b ==  right ) {         long long t = max(A - 1, n - B);         A -= t;         if (A < 1) A = 2 - A;         B += t;         if (B > n) B = 2 * n - B;         if (A == 1 && B == 1) {           if (1)             cout << n - 1 <<   n ;           else             cout << 0 <<   n ;           return;         }         if (A == 1 && B == 2) {           if (0)             cout << n - 1 <<   n ;           else             cout << 0 <<   n ;           return;         }         if (B == n && A == n) {           if (0)             cout << n - 1 <<   n ;           else             cout << 0 <<   n ;           return;         }         if (B == n && A == n - 1) {           if (1)             cout << n - 1 <<   n ;           else             cout << 0 <<   n ;           return;         }         {           if ((A + B + 1) % 2)             cout << n - 1 <<   n ;           else             cout << 0 <<   n ;           return;         }       }     } else if (A == B) {       x = A - 1;       y = n - x - 1;       if (a ==  left  && b ==  right ) {         if (0)           cout << n - 1 <<   n ;         else           cout << 0 <<   n ;         return;       }       if (a ==  left  && b ==  left ) {         if (1)           cout << n - 1 <<   n ;         else           cout << 0 <<   n ;         return;       }       if (a ==  right  && b ==  right ) {         if (0)           cout << n - 1 <<   n ;         else           cout << 0 <<   n ;         return;       }       if (a ==  right  && b ==  left ) {         if (1)           cout << n - 1 <<   n ;         else           cout << 0 <<   n ;         return;       }     }   }   signed main() {     long long JYZ;     JYZ = read();     while (JYZ--) DOIT();   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   int main() {     ios::sync_with_stdio(false);     int n;     long long m, x;     set<long long> v1;     set<long long> v2;     v1.insert(0);     v2.insert(0);     cin >> n >> m;     long long ans = 0;     for (int i = 0; i < n / 2; i++) {       cin >> x;       set<long long> tmp;       for (auto it : v1) tmp.insert((it + x) % m), ans = max(ans, it);       for (auto it : tmp) v1.insert(it), ans = max(ans, it);     }     for (int i = n / 2; i < n; i++) {       cin >> x;       set<long long> tmp;       for (auto it : v2) tmp.insert((it + x) % m), ans = max(ans, it);       for (auto it : tmp) v2.insert(it), ans = max(ans, it);     }     for (auto it : v1) {       int precisa = (m - 1) - it;       auto ok = v2.lower_bound(precisa);       if (ok == v2.end()) ok--;       while (ok != v2.begin() and it + *ok >= m) ok--;       ans = max(ans, (it + *ok) % m);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   vector<vector<int> > v;   bool transponer;   void escribe() {     if (transponer) {       vector<vector<int> > nextv(m + 2, vector<int>(n + 2, 0));       for (int i = 1; i <= n; i++)         for (int j = 1; j <= m; j++) nextv[j][i] = v[i][j];       swap(n, m);       v = nextv;     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++)         if (v[i][j])           cout <<  * ;         else           cout <<  . ;       cout << endl;     }   }   int valor(int i, int j) {     if (i == 1) return 0;     if (j == 1) return 1;     if (j == m) return 3;     return 4;   }   void genera() {     int act = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (act + valor(i, j) > k) {           int resta = k - act;           if (resta == 1) {             if (m > j)               v[i][m] = 1;             else               v[i + 1][1] = 1;           } else if (resta == 2) {             if (m - j + 1 >= 3)               v[i][m - 1] = 1;             else if (m - j + 1 == 2)               v[i][m] = v[i + 1][m] = v[i + 1][m - 1] = 1;             else               v[i + 1][2] = 1;           } else {             if (m - j + 1 >= 4)               v[i][m] = v[i][m - 2] = 1;             else if (j == 2) {               if (i < n)                 v[i][m] = v[i + 1][m] = v[i + 1][m - 1] = v[i + 1][1] =                     v[i + 1][2] = 1;               else {                 v[i][1] = 0;                 v[i][2] = v[i][4] = 1;               }             } else               v[i][m] = v[i + 1][m] = v[i + 1][m - 1] = v[i + 1][1] = 1;           }           escribe();           return;         }         v[i][j] = 1;         act += valor(i, j);         if (act == k) {           escribe();           return;         }       }     }   }   int main() {     ios::sync_with_stdio(false);     int casos;     cin >> casos;     for (int cas = 0; cas < casos; cas++) {       if (cas > 0) cout << endl;       cin >> n >> m >> k;       transponer = false;       if (n > m) {         swap(n, m);         transponer = true;       }       v = vector<vector<int> >(n + 2, vector<int>(m + 2, 0));       int total = (n - 1) + (n - 1) * (m - 2) * 4 + (n - 1) * 3;       if (k > total or k == total - 1 or k == total - 2 or k == total - 4 or           k == total - 5) {         cout << -1 << endl;         continue;       }       if (k == total - 8) {         if (n > 3) {           cout << -1 << endl;           continue;         }         for (int i = 1; i <= n; i++)           for (int j = 1; j <= m - 1; j++) v[i][j] = 1;         escribe();         continue;       }       if (m == 3 and k == 7) {         for (int i = 1; i <= 3; i++)           for (int j = 1; j <= 3; j++) v[i][j] = 1;         v[1][1] = v[3][1] = v[1][3] = 0;         escribe();         continue;       }       genera();     }   }


#include <bits/stdc++.h>   int main() {     int a, i;     char b, c, x;     scanf( %d  , &a);     scanf( %c , &b);     for (i = 1; i <= a - 2; i++) scanf( %c , &x);     scanf( %c , &c);     if (b ==  S  && c ==  F )       printf( YES );     else       printf( NO );   }


/*              Jai Maa Saraswati!!           */   #include<bits/stdc++.h>   #include<iostream>   #include<algorithm>   #include<string.h>   #include<math.h>   #define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);   #define ll long long    #define pb push_back   #define mod 1000000007   #define F first   #define S second   #define mp make_pair   #define vi vector<int>   #define vll vector<long long>   #define all(x) x.begin(),x.end()   #define dbg(x) cout<<   << #x <<  =  << x <<   ; //for debugging       using namespace std;   bool isprime(ll n)   {       //if(n<2)       //return false;       if(n==1)       return true;       if(n==2 || n==3)       return true;       if(n%2==0)return false;       for(ll i=3;i*i<=n;i+=2)       {           if(n%i==0)           return false;                  }       return true;   }   bool checkpalindrome(ll c)   {        string p=to_string(c);           int j = p.size() - 1;           for (int i = 0; i < j; i++, j--)       {           if (p[i] != p[j])           {               return false;           }       }           return true;   }   ll gcd(ll a,ll b)   {       if(a==0)       return b;       return gcd(b%a,a);   }   ll lcm(ll a,ll b)   {       return a*(b/gcd(a,b));   }    set<ll> fac;   void allFactors(ll n)   {      // set<ll> fac;       for(ll i=1;i<=sqrt(n);i++)       {           if(n%i==0)           {               fac.insert(i);               if(i!=n/i)               {                   fac.insert(n/i);               }           }       }       //return fac.size();   }   bool threefactors(ll n)//when you have need of a no which is divisible by 1,itself,any other one no.   {       ll p=sqrt(n);       if(p*p==n)       {           if(isprime(p))           return 1;           else           return 0;       }       else       return 0;   }   unsigned int countSetBits(unsigned int n) //no. of 1 s in a binary representation of a number   {        unsigned int count = 0;        while (n) {            count += n & 1;            n >>= 1;        }        return count;    }    int computeXOR(int n)   {     if(n%4==0)     return n;                        //TO CALCULATE XOR FROM 1 TO N     if(n%4==1)     return 1;     if(n%4==2)     return n+1;     else     return 0;   }   ll largest_power(ll N)       {                                              //LARGEST POWER OF 2 JUST LESS THAN OR EQUAL TO A GIVEN 16 BIT NUMBER           //changing all right side bits to 1.           N = N| (N>>1);           N = N| (N>>2);           N = N| (N>>4);           N = N| (N>>8);           N = N| (N>>16);             //as now the number is 2 * x-1, where x is required answer, so adding 1 and dividing it by                   return (N+1)>>1;           }   ll fibo(ll n)   {       long double him=(1+sqrt(5))/2;     return round(pow(him,n)/sqrt(5));   }    //pair(weight,pair(u,v));   void kruskal(vector<pair<ll,pair<int,int>>> v)   {        sort(v.begin(),v.end());       set<int> s;       ll ans=0;       for(int i=0;i<v.size() && s.size()<8;i++)       {           if(s.count(v[i].S.F) && s.count(v[i].S.S))           {                          }           else           {               ans+=v[i].F;               s.insert(v[i].S.F);               s.insert(v[i].S.S);           }       }       cout<<ans;   }   /*                      Jai Shree Krishna!!                           Jai Shree Ram!!                        Jai Shiv Shambhu Sai Ram!!                      */        void solve()   {       ll n,x,t;       cin>>n>>x>>t;       ll a=0,b=1,cnt;        a=t/x;        if(a<=n)        {        cnt=a*(n-1);        a--;        cnt-=(a*(a+1))/2;        }        else        {            n--;            cnt=(n*(n+1))/2;        }        cout<<max(0LL,cnt)<<endl;                        }   int main()   {        #ifndef ONLINE_JUDGE           freopen( input.txt ,  r , stdin);           freopen( output.txt ,  w , stdout);        #endif        IOS        int test_cases=1;         cin>>test_cases;        while(test_cases--)        {            solve();        }        return 0;   }    //ascii of a=97   //ascii of A=65   //ascii of 0=48   // priority_queue<ll,vector<ll>,greater<ll>>pq; ascending queue   //lower_bound and upper_bound   /*   for erasing in vector       1.you MUST declare a vector with its size or fill values using push_back       2.v[]={0,1,2,3,4,5,6};       3.v.erase(v.begin()+2);=>0 1 3 4 5 6       4.Again v[]={0,1,2,3,4,5,6};       5.v.erase(v.begin()+2,v.begin()+4);=>0 1 4 5 6   */   /*              Har Har Mahadev!!               */


#include <bits/stdc++.h>   using namespace std;   inline char gc() {     static char buf[100000], *p1 = buf, *p2 = buf;     return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)                ? EOF                : *p1++;   }   inline long long read() {     long long x = 0;     char ch = getchar();     bool positive = 1;     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) positive = 0;     for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ;     return positive ? x : -x;   }   inline void write(long long a) {     if (a < 0) {       a = -a;       putchar( - );     }     if (a >= 10) write(a / 10);     putchar( 0  + a % 10);   }   inline void writeln(long long a) {     write(a);     puts(  );   }   inline void wri(long long a) {     write(a);     putchar(   );   }   inline unsigned long long rnd() {     return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4;   }   const int N = 50005, K = 16;   int n;   char ch[N], q[N];   struct data {     int m, tong[N], SA[N], y[N], x[N], rk[N], h[N], height[N], logn[N];     char ch[N];     int dp[N][K];     inline int ask(int a, int b) {       if (a == b) return n - a + 1;       a = rk[a];       b = rk[b];       if (a > b) swap(a, b);       b--;       int zs = logn[b - a + 1];       return min(dp[b][zs], dp[a + (1 << zs) - 1][zs]);     }     void init() {       int m = 150;       memset(SA, 0, sizeof(SA));       memset(rk, 0, sizeof(rk));       memset(h, 0, sizeof(h));       memset(height, 0, sizeof(height));       memset(dp, 0, sizeof(dp));       memset(tong, 0, sizeof(tong));       memset(x, 0, sizeof(x));       memset(y, 0, sizeof(y));       for (int i = 1; i <= n; i++) tong[x[i] = ch[i]]++;       for (int i = 1; i <= m; i++) tong[i] += tong[i - 1];       for (int i = n; i; i--) SA[tong[x[i]]--] = i;       logn[0] = -1;       for (int k = 1; k <= n; k <<= 1) {         int p = 0;         for (int i = n - k + 1; i <= n; i++) y[++p] = i;         for (int i = 1; i <= n; i++)           if (SA[i] > k) y[++p] = SA[i] - k;         memset(tong, 0, sizeof(tong));         for (int i = 1; i <= n; i++) tong[x[y[i]]]++;         for (int i = 1; i <= m; i++) tong[i] += tong[i - 1];         for (int i = n; i; i--) SA[tong[x[y[i]]]--] = y[i];         swap(x, y);         x[SA[1]] = p = 1;         for (int i = 2; i <= n; i++)           x[SA[i]] = y[SA[i]] == y[SA[i - 1]] && y[SA[i] + k] == y[SA[i - 1] + k]                          ? p                          : ++p;         if (p >= n) break;         m = p;       }       for (int i = 1; i <= n; i++) rk[SA[i]] = i;       for (int i = 1; i <= n; i++) {         int t = SA[rk[i] + 1];         for (h[i] = max(0, h[i - 1] - 1); ch[i + h[i]] == ch[t + h[i]]; h[i]++)           ;         height[rk[i]] = h[i];       }       for (int i = 1; i <= n; i <<= 1) logn[i] = logn[i >> 1] + 1;       for (int i = 1; i <= n; i++) {         dp[i][0] = height[i];         if (i > 1 && !logn[i]) logn[i] = logn[i - 1];       }       for (int j = 1; j < K; j++)         for (int i = 1 << j; i <= n; i++)           dp[i][j] = min(dp[i][j - 1], dp[i - (1 << (j - 1))][j - 1]);     }   } S1, S2;   bool check(int x) {     for (int i = x; i + x <= n; i += x) {       if (S1.ask(i, i + x) + S2.ask(n - i + 1, n - i - x + 1) >= x) return 1;     }     return 0;   }   void build() {     memset(S1.ch, 0, sizeof(S1.ch));     memset(S2.ch, 0, sizeof(S2.ch));     for (int i = 1; i <= n; i++) S1.ch[i] = S2.ch[n - i + 1] = ch[i];     S1.init();     S2.init();   }   int main() {     scanf( %s , ch + 1);     n = strlen(ch + 1);     build();     for (int i = 1; i <= n; i++)       if (check(i)) {         int tot = 0;         for (int j = 1; j <= i; j++) q[++tot] = ch[j];         for (int j = i + 1; j <= n; j++) {           q[++tot] = ch[j];           if (S1.ask(j - i, j) >= i) {             tot -= i;             for (int k = j + 1; k < j + i && k <= n; k++) q[++tot] = ch[k];             j = j + i - 1;             continue;           }         }         n = tot;         for (int j = 1; j <= n; j++) ch[j] = q[j];         build();       }     ch[n + 1] = 0;     puts(ch + 1);   }


#include <bits/stdc++.h>   using namespace std;   int read() {     int f = 1, x = 0;     char c = getchar();     while (c <  0  ||  9  < c) {       if (c ==  - ) f = -1;       c = getchar();     }     while ( 0  <= c && c <=  9 )       x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();     return f * x;   }   const int MAXN = 100000;   const int INF = 0x3f3f3f3f;   int ans;   void Query(int L, int R, int l1, int r1, int l2, int r2) {     if (l1 > r1 || l2 > r2) return;     int le = max(l1, l2), ri = min(r1, r2);     ans = max(ans, ri - le + 1);     if ((l1 <= l2 && r2 <= r1) || (l2 <= l1 && r1 <= r2)) return;     int Mid = (L + R) >> 1;     Query(L, Mid - 1, l1, min(r1, Mid - 1), l2, min(r2, Mid - 1));     Query(L, Mid - 1, l1, min(r1, Mid - 1), max(l2, Mid + 1) - Mid, r2 - Mid);     Query(L, Mid - 1, max(l1, Mid + 1) - Mid, r1 - Mid, l2, min(r2, Mid - 1));     Query(L, Mid - 1, max(l1, Mid + 1) - Mid, r1 - Mid, max(l2, Mid + 1) - Mid,           r2 - Mid);     return;   }   int main() {     int l1 = read(), r1 = read(), l2 = read(), r2 = read();     Query(1, 1073741823, l1, r1, l2, r2);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   static int N, K, M, G, U[500003], V[500003], W[500003], fa[500003], sup[500003],       dep[500003];   static bool spe[500003];   static vector<pair<bool, int>> edge[500003];   static long long ans;   int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }   void dfs(int u, int f) {     dep[u] = dep[sup[u] = f] + 1;     for (auto v : edge[u])       if (v.second != f) {         spe[v.second] = v.first;         dfs(v.second, u);       }   }   int main() {     scanf( %d%d%d , &N, &K, &M);     for (int i = 1; i <= N; i++) fa[i] = i;     for (int i = 0, x, y; i < K; i++) {       scanf( %d%d , &x, &y);       edge[x].push_back(make_pair(true, y));       edge[y].push_back(make_pair(true, x));       fa[find(x)] = find(y);     }     for (int i = 0; i < M; i++) {       scanf( %d%d%d , &U[G], &V[G], &W[G]);       int u = find(U[G]), v = find(V[G]);       if (u != v) {         edge[U[G]].push_back(make_pair(false, V[G]));         edge[V[G]].push_back(make_pair(false, U[G]));         fa[u] = v;       } else         G++;     }     dfs(1, 0);     for (int i = 1; i <= N; i++) fa[i] = i;     for (int i = 0; i < G; i++) {       int u = find(U[i]), v = find(V[i]);       while (u != v) {         if (dep[u] < dep[v]) swap(u, v);         ans += spe[u] ? W[i] : 0;         u = fa[u] = find(sup[u]);       }     }     for (int i = 1; i <= N; i++)       for (auto v : edge[i])         if (v.first && find(v.second) != find(i)) return puts( -1 ), 0;     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   int main() {     int k, a, b, v;     int sections;     scanf( %d%d%d%d , &k, &a, &b, &v);     sections = a / v;     sections += ((a % v) != 0);     int box = 0;     k--;     while (b > 0) {       box++;       if (b >= k) {         b -= k;         sections -= (k + 1);       } else {         sections -= (b + 1);         b = 0;       }       if (sections <= 0) break;     }     if (sections > 0) box += sections;     printf( %d n , box);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     vector<vector<char> > v;     v.resize(n, vector<char>(m));     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) cin >> v[i][j];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if ((i + j) % 2 == 0 && v[i][j] ==  . ) {           cout <<  B ;           if (j == m - 1) cout <<   n ;         } else if ((i + j) % 2 == 1 && v[i][j] ==  . ) {           cout <<  W ;           if (j == m - 1) cout <<   n ;         } else {           cout <<  - ;           if (j == m - 1) cout <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e6 + 5;   struct bing {     int fa[N];     void init() {       for (int i = (1); i < (N); i++) fa[i] = i;     }     int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }     void unite(int x, int y) { fa[find(x)] = find(y); }     bool same(int x, int y) { return find(x) == find(y); }   } bi[2];   int n, m1, m2;   int main() {     for (int i = (0); i < (2); i++) bi[i].init();     cin >> n >> m1 >> m2;     int op = m1 > m2 ? 0 : 1;     for (int i = (1); i < (m1 + 1); i++) {       int u, v;       scanf( %d%d , &u, &v);       bi[op].unite(u, v);     }     for (int i = (1); i < (m2 + 1); i++) {       int u, v;       scanf( %d%d , &u, &v);       bi[1 - op].unite(u, v);     }     printf( %d n , min(n - 1 - m1, n - 1 - m2));     for (int i = (1); i < (n + 1); i++)       if (!bi[0].same(1, i) && !bi[1].same(1, i)) {         bi[0].unite(1, i), bi[1].unite(1, i);         printf( %d %d n , 1, i);       }     for (int a = 2, b = 2; a <= n; a++) {       if (bi[0].same(1, a)) continue;       while (b <= n && bi[1].same(1, b)) b++;       if (b <= n) {         bi[0].unite(a, b), bi[1].unite(a, b);         printf( %d %d n , a, b);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 10;   const int MAXM = 1e6 + 10;   const int MAXLOG = 20;   struct Edge {     int x, y, w;     int id;   } edge[MAXM];   int T, n, m, i;   int val;   vector<pair<int, int> > e[MAXN];   int fa[MAXN], anc[MAXN][MAXLOG], dep[MAXN];   int ans[MAXM];   int mx[MAXN][MAXLOG];   bool on_mst[MAXM];   inline bool cmp(Edge a, Edge b) { return a.w < b.w; }   inline int get_root(int x) {     if (fa[x] == x) return x;     return fa[x] = get_root(fa[x]);   }   inline void kruskal() {     int i, x, y, sx, sy;     int w;     for (i = 1; i <= n; i++) fa[i] = i;     sort(edge + 1, edge + m + 1, cmp);     for (i = 1; i <= m; i++) {       x = edge[i].x;       y = edge[i].y;       w = edge[i].w;       sx = get_root(x);       sy = get_root(y);       if (sx != sy) {         fa[sx] = sy;         on_mst[edge[i].id] = 1;         e[x].push_back(make_pair(y, w));         e[y].push_back(make_pair(x, w));       }     }   }   inline void build(int u) {     int i, v;     for (i = 1; i < MAXLOG; i++) {       anc[u][i] = anc[anc[u][i - 1]][i - 1];       mx[u][i] = max(mx[u][i - 1], mx[anc[u][i - 1]][i - 1]);     }     for (i = 0; i < e[u].size(); i++) {       v = e[u][i].first;       if (anc[u][0] != v) {         dep[v] = dep[u] + 1;         anc[v][0] = u;         mx[v][0] = e[u][i].second;         build(v);       }     }   }   inline int get(int x, int y) {     int i, t;     int ans = 0;     if (dep[x] > dep[y]) swap(x, y);     t = dep[y] - dep[x];     for (i = 0; i < MAXLOG; i++) {       if (t & (1 << i)) {         ans = max(ans, mx[y][i]);         y = anc[y][i];       }     }     if (x == y) return ans;     for (i = MAXLOG - 1; i >= 0; i--) {       if (anc[x][i] != anc[y][i]) {         ans = max(ans, max(mx[x][i], mx[y][i]));         x = anc[x][i];         y = anc[y][i];       }     }     return max(ans, max(mx[x][0], mx[y][0]));   }   int main() {     scanf( %d%d , &n, &m);     if (m == n - 1) return 0;     for (i = 1; i <= m; i++) {       scanf( %d%d%d , &edge[i].x, &edge[i].y, &edge[i].w);       edge[i].id = i;     }     kruskal();     build(1);     for (int i = 1; i <= m; ++i) {       if (!on_mst[edge[i].id]) ans[edge[i].id] = get(edge[i].x, edge[i].y);     }     for (int i = 1; i <= m; ++i)       if (!on_mst[i]) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using namespace std;   int M1(int x) { return x >= 1000000007 ? x - 1000000007 : x; }   int M2(int x) { return x >= 1000000009 ? x - 1000000009 : x; }   struct Hash {     int a, b;     Hash() { a = b = 0; }     Hash(int a, int b) : a(a), b(b) {}     Hash(int a) : a(a), b(a) {}     bool operator<(const Hash& h) const { return a == h.a ? b < h.b : a < h.a; }     bool operator!=(const Hash& h) const { return a != h.a || b != h.b; }     bool operator==(const Hash& h) const { return a == h.a && b == h.b; }     Hash operator+(const Hash& h) const { return Hash(M1(a + h.a), M2(b + h.b)); }     Hash operator-(const Hash& h) const {       return Hash(M1(a - h.a + 1000000007), M2(b - h.b + 1000000009));     }     Hash operator*(const Hash& h) const {       return Hash(((long long)a * h.a) % 1000000007,                   ((long long)b * h.b) % 1000000009);     }     Hash inv() const { return Hash(M1(1000000007 - a), M2(1000000009 - b)); }   };   struct Hash_map {     Hash v[11009989 + 1];     int val[11009989 + 1], head[11009989 + 1], nxt[11009989 + 1], ptr;     void clear() { ptr = 0; }     void insert(Hash a) {       int x = (19260817ll * a.a + a.b) % 11009989;       for (int y = head[x]; y; y = nxt[y])         if (v[y] == a) return;       v[++ptr] = a, nxt[ptr] = head[x], head[x] = ptr;     }     int count(Hash a) {       int x = (19260817ll * a.a + a.b) % 11009989;       for (int y = head[x]; y; y = nxt[y])         if (v[y] == a) return true;       return false;     }     int& operator[](Hash a) {       int x = (19260817ll * a.a + a.b) % 11009989;       for (int y = head[x]; y; y = nxt[y])         if (v[y] == a) return val[y];       v[++ptr] = a, nxt[ptr] = head[x], head[x] = ptr;       return val[ptr];     }   } st;   Hash pw[100100], _pw[100100];   struct Data {     int len, id;     Hash h;   };   int anses[100100], prec[100100], dep[100100], fa[21][100100], n, m;   vector<pair<int, int> > G[100100];   vector<Data> Q[100100];   void dfs(int u, int f) {     fa[0][u] = f;     for (auto c : G[u])       if (c.first != f) {         dep[c.first] = dep[u] + 1;         pw[c.first] = pw[u] * Hash(19260817, 20010911) + Hash(c.second, c.second);         prec[c.first] = c.second;         dfs(c.first, u);       }   }   Hash get(int l, int r) { return pw[l] - pw[r] * _pw[dep[l] - dep[r]]; }   int lca(int u, int v) {     if (dep[u] < dep[v]) swap(u, v);     int d = dep[u] - dep[v];     for (int i = 0; d >> i; i++)       if (d >> i & 1) u = fa[i][u];     if (u == v) return u;     for (int i = 19; i >= 0; --i)       if (fa[i][u] ^ fa[i][v]) u = fa[i][u], v = fa[i][v];     return fa[0][u];   }   int jmp(int u, int d) {     for (int i = 0; d >> i; i++)       if (d >> i & 1) u = fa[i][u];     return u;   }   Hash geth(char* s) {     Hash ret = 0;     for (int i = 0; s[i]; i++) ret = ret * Hash(19260817, 20010911) + (s[i]);     return ret;   }   int kmp(char* t, char* s) {     static int f[100100];     int ns = strlen(s);     for (int i = 0; i < ns; ++i) f[i] = 0;     f[0] = -1;     for (int i = 1; s[i]; i++) {       int j = f[i - 1];       while (j != -1 && s[j + 1] != s[i]) j = f[j];       if (s[j + 1] == s[i]) j++;       f[i] = j;     }     int ans = 0;     for (int i = 0, x = -1; t[i]; i++) {       while (x != -1 && s[x + 1] != t[i]) x = f[x];       if (s[x + 1] == t[i]) x++;       if (x == ns - 1) ans++;     }     return ans;   }   void dfs2(int u, int f) {     for (int i = 1, x = f; i <= 100 && x; i++) {       st[get(u, x)]++;       x = fa[0][x];     }     for (auto c : Q[u]) {       anses[abs(c.id)] += (c.id > 0 ? 1 : -1) * st[c.h];     }     for (auto c : G[u])       if (c.first != f) dfs2(c.first, u);     for (int i = 1, x = f; i <= 100 && x; i++) st[get(u, x)]--, x = fa[0][x];   }   int main() {     scanf( %d , &n);     _pw[0] = Hash(1, 1);     for (int i = 1; i <= n; ++i) _pw[i] = _pw[i - 1] * Hash(19260817, 20010911);     for (int i = 2, u, v; i <= n; ++i) {       char s[2];       scanf( %d%d%s , &u, &v, s);       G[u].push_back(pair<int, int>(v, s[0]));       G[v].push_back(pair<int, int>(u, s[0]));     }     dfs(1, 0);     for (int i = 1; i < 20; ++i)       for (int j = 1; j <= n; ++j) fa[i][j] = fa[i - 1][fa[i - 1][j]];     scanf( %d , &m);     for (int i = 1, u, v; i <= m; ++i) {       char s[200], t[300];       int tp = 0;       scanf( %d%d%s , &u, &v, s);       int lc = lca(u, v), len = strlen(s);       int zu = jmp(u, max(0, dep[u] - dep[lc] - len));       int zv = jmp(v, max(0, dep[v] - dep[lc] - len));       Q[zv].push_back(Data{len, -i, geth(s)});       Q[v].push_back(Data{len, i, geth(s)});       reverse(s, s + len);       Q[zu].push_back(Data{len, -i, geth(s)});       Q[u].push_back(Data{len, i, geth(s)});       reverse(s, s + len);       while (zu != lc) t[tp++] = prec[zu], zu = fa[0][zu];       int xp = tp;       while (zv != lc) t[tp++] = prec[zv], zv = fa[0][zv];       reverse(t + xp, t + tp);       t[tp] = 0;       anses[i] = kmp(t, s);     }     dfs2(1, 0);     for (int i = 1; i <= m; ++i) printf( %d n , anses[i]);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int l = s.length();     vector<vector<int>> dp(5000, vector<int>(5000, 0));     vector<int> kk(s.size() + 1, 0);     for (int length = 1; length <= l; length++) {       for (int right = 0; right < s.size(); right++) {         int r = right + length - 1;         if (r >= s.size()) break;         if (s[right] != s[r]) continue;         if (right == r) {           dp[r][r] = 1;         } else if (length == 2) {           dp[right][r] = 2;         } else if (dp[right + 1][r - 1] > 0) {           int haf = length / 2;           int ml = right + haf - 1;           int mr = r - haf + 1;           dp[right][r] = 1 + min(dp[right][ml], dp[mr][r]);         }         kk[dp[right][r]]++;       }     }     for (int i = l; i >= 1; i--) {       kk[i - 1] = kk[i] + kk[i - 1];     }     for (int i = 1; i <= l; i++) {       string pt = to_string(kk[i]) +    ;       cout << pt;     }   }


#include <bits/stdc++.h>   using namespace std;   const int Z = (int)3e3 + 111;   const int N = (int)1e6 + 111;   const int INF = (int)1e9 + 111;   const int MOD = (int)1e9 + 7;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string t, ans;     cin >> t;     int k;     if (t.size() % 2)       k = 1;     else       k = 0;     int po1(0), po2(t.size() - 1);     while (ans.size() != t.size()) {       if (k) {         ans = t[po1] + ans;         po1++;       } else {         ans = t[po2] + ans;         po2--;       }       k = !k;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-8;   const long long mod = (int)1e9 + 7;   int dx[] = {0, 0, -1, 1, 1, 1, -1, -1};   int dy[] = {1, -1, 0, 0, 1, -1, -1, 1};   const int N = 1000001;   void stop() {}   void file() {}   struct point {     long long x, y;   };   unsigned long long gcd(unsigned long long a, unsigned long long b) {     return !b ? a : gcd(b, a % b);   }   long long fixMod(long long a, long long b) { return ((a % b) + b) % b; }   double triangle_area(int x1, int y1, int x2, int y2, int x3, int y3) {     return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)) / 2.;   };   long long dis(pair<long long, long long> a, pair<long long, long long> b) {     long long x = a.first - b.first, y = a.second - b.second;     return abs(x) + abs(y);   }   unsigned long long LCM(unsigned long long a, unsigned long long b) {     return a / gcd(a, b) * b;   }   long long power(long long x, long long y) {     if (y == 0) return 1;     if (y == 1) return x;     long long r = power(x, y / 2);     return r * r * power(x, y % 2);   }   bool isPrime(long long x) {     if (x <= 1 || (x % 2 == 0 && x != 2)) return 0;     for (long long i = 3; i * i <= x; i += 2) {       if (x % i == 0) return 0;     }     return 1;   }   vector<vector<int>> v;   int n;   vector<int> dep;   int dfs(int node, int pr = -1) {     int ret = 1;     for (auto ch : v[node]) {       if (ch != pr) {         ret += dfs(ch, node);       }     }     dep[node] = ret;     return ret;   }   int aa = -1, bb = -1;   int x = -1;   void sol(int node, int pr = -1) {     if (v[node].size() == 1 && aa == -1) {       aa = node;       bb = pr;     }     for (auto ch : v[node]) {       if (ch != pr && ch != x) {         sol(ch, node);       }     }   }   void solve() {     cin >> n;     v.clear();     v.resize(n);     dep.clear();     map<pair<int, int>, bool> mp;     int mx = 0;     int st = 0;     dep.resize(n);     for (int i = 0; i < n - 1; i++) {       int a, b;       cin >> a >> b;       a--;       b--;       mp[{a, b}] = true;       v[a].push_back(b);       v[b].push_back(a);     }     for (int i = 0; i < n; i++) {       if (v[i].size() > mx) {         mx = v[i].size();         st = i;       }     }     int ans = dfs(st);     if (count(((dep).begin()), ((dep).end()), ans / 2) == 0 || ans & 1) {       for (auto i : mp) {         cout << i.first.first + 1 <<     << i.first.second + 1 <<   n ;         cout << i.first.first + 1 <<     << i.first.second + 1 <<   n ;         break;       }     } else {       aa = -1;       bb = -1;       x = -1;       for (int i = 0; i < n; i++) {         if (dep[i] * 2 == ans) {           x = i;           break;         }       }       sol(st);       if (mp[{aa, bb}] == true) {         cout << aa + 1 <<     << bb + 1 <<   n ;       } else {         cout << bb + 1 <<     << aa + 1 <<   n ;       }       if (v[aa].size() == 1) {         cout << x + 1 <<     << aa + 1 <<   n ;       } else {         cout << x + 1 <<     << bb + 1 <<   n ;       }     }     mp.clear();   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, first, second, third, fourth;     cin >> a >> b >> c;     first = a + b + c;     second = 2 * (a + b);     third = 2 * (b + c);     fourth = 2 * (a + c);     cout << min(first, min(second, min(third, fourth)));   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:200000000 )   using namespace std;   template <typename T>   inline T Abs(T x) {     return (x >= 0) ? x : -x;   }   template <typename T>   inline T sqr(T x) {     return x * x;   }   template <typename T>   string toStr(T x) {     stringstream st;     st << x;     string s;     st >> s;     return s;   }   inline int nextInt() {     int x;     if (scanf( %d , &x) != 1) throw;     return x;   }   inline long long nextInt64() {     long long x;     if (scanf( %I64d , &x) != 1) throw;     return x;   }   inline double nextDouble() {     double x;     if (scanf( %lf , &x) != 1) throw;     return x;   }   const int INF = (int)1E9;   const long long INF64 = (long long)1E18;   const long double EPS = 1E-9;   const long double PI = 3.1415926535897932384626433832795;   const int MAXN = 100100;   unsigned long long d[MAXN], ans;   vector<int> g[MAXN];   int n;   unsigned long long DFS(int v, int prev) {     if (prev != -1) g[v].erase(find((g[v]).begin(), (g[v]).end(), prev));     d[v] = 0;     long long sum2 = 0;     for (int i = 0; i < (int)(g[v].size()); i++) {       unsigned long long cur = DFS(g[v][i], v);       sum2 += 1LL * d[v] * cur;       d[v] += cur;     }     d[v]++;     ans -= sqr(sum2);     ans -= 2LL * sum2 * ((n - d[v] + 1) * d[v] - 1);     ans -= 2LL * (d[v] - 1) * ((n - d[v] + 1) * d[v] - 1) - sqr(d[v] - 1);     return d[v];   }   int main() {     n = nextInt();     for (int i = 0; i < (int)(n - 1); i++) {       int v = nextInt() - 1;       int u = nextInt() - 1;       g[v].push_back(u);       g[u].push_back(v);     }     ans = sqr(1LL * n * (n - 1) / 2);     DFS(0, -1);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, count = 0;     string s;     cin >> n >> s;     for (int i = 0; i < n; i++) {       if (s[i] ==  8 ) count++;     }     if (n < 11)       cout <<  0  << endl;     else {       cout << min(count, n / 11) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   int a[maxn], n, x, k, l;   long long ans = 0;   int main() {     cin >> n >> x;     for (int i = 1; i <= n; i++) {       cin >> k >> l;       ans += min(k, l);       a[i] = min(max(l - k, 0), k);     }     sort(a + 1, a + 1 + n);     for (int i = 1; i <= x; i++) ans += a[n - i + 1];     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500005, M = 60;   int q, fa[N], n = 1;   double dp[N][M + 5];   int main() {     ios::sync_with_stdio(false);     cin >> q;     int opt, pos;     for (int i = 0; i <= M; i++) dp[1][i] = 1;     for (int _ = 1; _ <= q; _++) {       cin >> opt >> pos;       if (opt == 1) {         fa[++n] = pos;         for (int i = 0; i <= M; i++) dp[n][i] = 1;         double pre = 1;         int pp = n;         for (int i = 0, cur = pos; cur && i <= M; i++, cur = fa[cur]) {           double tmp = dp[cur][i];           dp[cur][i] /= 0.5 * (pre + 1);           if (i)             dp[cur][i] *= 0.5 * (1 + dp[pp][i - 1]);           else             dp[cur][i] *= 0.5;           pp = cur;           pre = tmp;         }       } else {         double ans = 0;         for (int i = 1; i <= M; i++) ans += (dp[pos][i] - dp[pos][i - 1]) * i;         printf( %.10lf n , ans);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > all;   int isable[100005], cnt[100005];   int main() {     int i, n, k;     scanf( %d , &n);     scanf( %d , &k);     long long b;     scanf( %I64d , &b);     for (i = 0; i < n; i++) {       int x;       scanf( %d , &x);       cnt[i] = x;       if (i != n - 1) all.push_back(make_pair(x, i));     }     sort(all.begin(), all.end());     i = all.size() - 1;     long long sm = 0;     int choto;     while (k) {       sm += all[i].first;       choto = all[i].first;       isable[all[i].second] = 1;       k--;       i--;     }     for (i = 0; i < n - 1; i++) {       long long temp = sm;       if (!isable[i]) {         temp -= choto;         temp += cnt[i];       }       if (temp > b) break;     }     printf( %d n , i + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a;     string b;     cin >> a >> b;     long long int len1 = a.length();     long long int len2 = b.length();     if (a.compare(b) == 0) {       cout <<  -1 n ;     } else {       if (len1 > len2)         cout << len1 <<   n ;       else         cout << len2 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 10;   const double eps = 1e-6;   int n, m;   int sum, res;   int f[21][1 << 21], col[MAX];   char s[MAX];   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) {       scanf( %s , s);       for (int j = 0; j < m; j++)         if (s[j] ==  1 ) col[j] |= (s[j] ==  1 ) << i;     }     for (int i = 0; i < m; i++) f[0][col[i]]++;     for (int i = 1; i <= n; i++) {       for (int j = 0; j < 1 << n; j++) {         sum = i < 2 ? 0 : (i - 2 - n) * f[i - 2][j];         for (int k = 0; k < n; k++) sum += f[i - 1][j ^ (1 << k)];         f[i][j] = sum / i;       }     }     res = n * m;     for (int i = 0; i < 1 << n; i++) {       sum = 0;       for (int j = 0; j <= n; j++) sum += (j < n - j ? j : n - j) * f[j][i];       res = res < sum ? res : sum;     }     printf( %d , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = int;   namespace SEG {   static const ll STAR = 1, FINI = 2e5 + 110;   struct NODE {     ll l, r, min;     NODE() : l(0), r(0), min(0) {}   } node[1000000 * 10 + 110];   ll newnode(ll pre = 0) {     static ll cnt = 0;     node[++cnt] = node[pre];     return cnt;   }   ll& lson(ll now) { return node[now].l; }   ll& rson(ll now) { return node[now].r; }   ll ins(ll pre, ll val, ll pos, ll star = STAR, ll fini = FINI) {     ll now = newnode(pre);     if (star < fini) {       if (val <= ((star + fini) / 2))         lson(now) = ins(lson(pre), val, pos, star, ((star + fini) / 2));       else         rson(now) = ins(rson(pre), val, pos, ((star + fini) / 2) + 1, fini);       node[now].min = min(node[lson(now)].min, node[rson(now)].min);     } else {       node[now].min = pos;     }     return now;   }   ll ask(ll now, ll limit, ll star = STAR, ll fini = FINI) {     if (star == fini) return star;     if (node[lson(now)].min < limit)       return ask(lson(now), limit, star, ((star + fini) / 2));     else       return ask(rson(now), limit, ((star + fini) / 2) + 1, fini);   }   void debug(ll now, ll star = STAR, ll fini = FINI) {     if (star > fini or now == 0) return;     if (star == fini) {       if (star == STAR) cout <<  SEG::debug::  << endl;       if (node[now].min != INT64_MAX)         cout << star <<     << node[now].min << endl;       if (star == FINI) cout << endl;       return;     }     debug(lson(now), star, ((star + fini) / 2));     debug(rson(now), ((star + fini) / 2) + 1, fini);   }   }  // namespace SEG   vector<ll> pos[100000 + 110];   ll date[100000 + 110];   static ll T[100000 + 110];   bool check(ll val) {     for (ll i = 1; i < pos[val].size(); i++)       if (pos[val][i - 1] < pos[val][i] - 1) {         if (SEG::ask(T[pos[val][i] - 1], pos[val][i - 1] + 1) == val) {           return 1;         }       }     return 0;   }   int main() {     ll n;     cin >> n;     for (ll i = 1; i <= n + 10; i++) pos[i].push_back(0);     for (ll i = 1; i <= n; i++) {       cin >> date[i];       pos[date[i]].push_back(i);       T[i] = SEG::ins(T[i - 1], date[i], i);     }     for (ll i = 1; i <= n + 10; i++) pos[i].push_back(n + 1);     for (ll i = 1; i <= n + 10; i++)       if (check(i) == 0) {         cout << i <<   n ;         return 0;       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 250005;   int qr[maxn], qp[maxn], wm[maxn], loc[maxn];   int pp[maxn], pr[maxn], pm[maxn];   long long pd[maxn];   stack<int> f[maxn];   bool used[maxn];   int n, nx, ny, totm;   long long dist(int x, int y) { return (long long)x * x + (long long)y * y; };   bool cmp(int a, int b) { return pd[a] > pd[b]; };   int main() {     int i, x, y, h, t;     long long d;     scanf( %d%d%d%d%d , &nx, &ny, qp + 1, qr + 1, &n);     for (i = 1; i <= n; ++i) {       scanf( %d%d%d%d%d , &x, &y, pm + i, pp + i, pr + i);       pd[i] = dist(x - nx, y - ny);       wm[i - 1] = pm[i];       loc[i] = i;     }     sort(wm, wm + n);     totm = unique(wm, wm + n) - wm;     for (i = 1; i <= n; ++i) pm[i] = lower_bound(wm, wm + totm, pm[i]) - wm + 1;     sort(loc + 1, loc + n + 1, cmp);     for (i = 1; i <= n; ++i)       for (x = pm[loc[i]]; x <= totm; x += x & -x) f[x].push(loc[i]);     for (h = 1, t = 1; h <= t; ++h) {       x = upper_bound(wm, wm + totm, qp[h]) - wm;       d = (long long)qr[h] * qr[h];       for (; x > 0; x -= x & -x)         while (!f[x].empty() && pd[f[x].top()] <= d) {           y = f[x].top();           f[x].pop();           if (!used[y]) {             used[y] = true;             ++t;             qp[t] = pp[y];             qr[t] = pr[y];           }         }     }     printf( %d n , t - 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5 * 100000;   vector<int> g[MAXN + 1], e[MAXN + 1];   int col[MAXN + 1], cur = 1, used[MAXN + 1];   void dfs(int v, int p) {     int num = 1;     vector<int> del;     for (auto x : g[v]) {       del.push_back(col[x]);       used[col[x]] = 1;     }     for (auto x : g[v]) {       if (col[x]) continue;       while (used[num]) num++;       used[num] = 1;       del.push_back(num);       col[x] = num;     }     for (auto x : del) used[x] = 0;     for (auto x : e[v]) {       if (x == p) continue;       dfs(x, v);     }   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     int pos = 0;     for (int i = 1; i <= n; i++) {       int sz;       scanf( %d , &sz);       for (int j = 0; j < sz; j++) {         int x;         scanf( %d , &x);         g[i].push_back(x);       }       if (g[i].size() > g[pos].size()) pos = i;       sort(g[i].begin(), g[i].end());     }     for (int i = 0; i < n - 1; i++) {       int a, b;       scanf( %d%d , &a, &b);       e[a].push_back(b);       e[b].push_back(a);     }     for (auto x : g[pos]) col[x] = cur++, used[cur] = 1;     dfs(pos, -1);     cout << max(1, *max_element(col + 1, col + 1 + m)) <<   n ;     for (int i = 1; i <= m; i++) {       if (!col[i]) col[i] = 1;       printf( %d  , col[i]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Person {     int a;     int id;   };   bool operator<(Person a, Person b) { return a.a < b.a; }   class TSolver {    public:     int n;     vector<Person> persons;     explicit TSolver(std::istream& in) {       in >> n;       persons.resize(n);       for (int i = 0; i < n; ++i) {         in >> persons[i].a;         persons[i].id = i;       }     }     bool ok = true;     vector<int> ans;     void Solve() {       sort((persons).begin(), (persons).end());       ans.resize(n);       int color = 1;       for (int i = 0; ok && i < n;) {         int same = n - persons[i].a;         ok = ok && i + same <= n;         for (int j = 0; ok && j < same; ++j) {           if (persons[i + j].a != persons[i].a) {             ok = false;           }           ans[persons[i + j].id] = color;         }         i += same;         ++color;       }     }     void PrintAnswer(std::ostream& out) const {       if (ok) {         out <<  Possible n ;         for (int i = 0; i < n; ++i) {           out << ans[i] <<    n [i + 1 == n];         }       } else {         out <<  Impossible n ;       }     }   };   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(nullptr);     std::cout.tie(nullptr);     {       auto solver = std::make_shared<TSolver>(std::cin);       solver->Solve();       solver->PrintAnswer(std::cout);     }     do {     } while (false);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long binpow(long long a, long long b) {     long long exp = 10e9;     long long res = 1;     while (b > 0) {       if (b & 1) res = ((res % exp) * (a % exp)) % exp;       a = ((a % exp) * (a % exp)) % exp;       b >>= 1;     }     return res;   }   bool isprime(int x) {     for (int d = 2; d * d <= x; d++) {       if (x % d == 0) return false;     }     return true;   }   unsigned int cntsb(unsigned int n) {     unsigned int count = 0;     while (n) {       count += n & 1;       n >>= 1;     }     return count;   }   void solve(int cas) {}   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int number = 0;     long long n;     cin >> n;     int last = n % 10;     if (last <= 5)       n -= last;     else       n += (10 - last);     cout << n;   }


#include <bits/stdc++.h>    using namespace std;       #define vec vector    #define PB push_back       #define MP make_pair    #define MT make_tuple    #define F first    #define S second       using ll = long long int; // watch out for overflows in your code   using pii = pair<int,int>;   //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());      void debug() { cout <<   n ; };   template <typename T, typename... Targs>    void debug(T t, Targs... args) {       cout << t <<    ;       debug(args...);   }      template <typename T1, typename T2>    inline ostream& operator << (ostream& os, const pair<T1,T2>& p) {       return os <<  (  << p.F <<  ,   << p.S <<  ) ;   }      template <typename T>    inline ostream& operator << (ostream& os, const vec<T>& v) {       for(auto& t : v) {           os << t <<    ;       }       return os;   }      template <typename T>    inline ostream& operator << (ostream& os, const vec<vec<T>>& m) {       for(auto& v : m) {           for(auto& t : v) {               os << t <<    ;           }           os <<   n ;       }       return os;   }      template <typename T>    inline ostream& operator << (ostream& os, const set<T>& v) {       for(auto& t : v) {           os << t <<    ;       }       return os;   }      template <typename K, typename V>    inline ostream& operator << (ostream& os, const map<K,V>& m) {       for(auto& p : m) {           os << p.F <<   ->   << p.S <<   n ;       }       return os;   }      const int INF = 2 * 1e9;   int T, N, M;   vec<int> A, B;      int increment(int& i, const vec<int>& v) {       int S = v.size();       if(i >= S) {           return -INF;       }       int P = v[i++];       while(i < S && P < 0) {           P += v[i];           i++;       }       while(i < S && P <= P + v[i]) {           P = P + v[i];           ++i;       }       return P;   }      int compute() {       int j = 0, i = 0;       int pa, pb;       int S = 0;       int ans = 0;          pa = increment(i, A);       pb = increment(j, B);          while(i <= N || j <= M) {           //debug( pa , pa,  pb , pb);           if(pa < 0 && pb < 0) {               break;           }           if(pa >= pb) {               S += pa;               pa = increment(i, A);           }           else {               S += pb;               pb = increment(j, B);           }           ans = max(ans, S);       }       return ans;   }      ////////////////////////////////////////////////////////////////////////      int main() {       ios::sync_with_stdio(0);       cin.tie(0);          cin >> T;       while(T--) {           cin >> N;           A.assign(N, 0);           for(int i = 0; i < N; ++i) {               cin >> A[i];           }           cin >> M;           B.assign(M, 0);           for(int j = 0; j < M; ++j) {               cin >> B[j];           }           cout << compute() <<   n ;       }      }


#include <bits/stdc++.h>   using namespace std;   const int N = 100;   const int M = 2e6 + 7;   const int inf = 1e9;   int n, m, mx, p[N], minn[N], D[M][2], sum = 0;   int a[N][N];   bool check() {     for (int t = 0, tE = (1 << mx) - 1; t <= tE; t++) {       int ds = 0, bs = 0;       for (int i = 1, iE = mx; i <= iE; i++)         if (t & (1 << (i - 1))) ds += p[i];       if (!ds) continue;       for (int i = 1, iE = mx; i <= iE; i++)         if (t & (1 << (i - 1)))           for (int j = 1, jE = mx; j <= jE; j++)             if (t & (1 << (j - 1))) bs += a[i][j];       if (bs >= ds) return 0;     }     return 1;   }   int Cnt(int x) { return x == 0 ? 0 : Cnt(x / 10) + 1; }   char sa[N], sb[N];   bool get() {     for (int x = 1, xE = mx; x <= xE; x++)       for (int i = 1, iE = mx; i <= iE; i++)         if (a[x][i]) {           a[x][i]--, p[x]--;           if (check()) return printf( %d %d n , minn[x] + p[x], minn[i]), 1;           a[x][i]++, p[x]++;           a[x][i]--, p[i]--;           if (check()) return printf( %d %d n , minn[x], minn[i] + p[i]), 1;           a[x][i]++, p[i]++;         }     return 0;   }   int main() {     scanf( %d , &n), mx = Cnt(n);     for (int i = 1, iE = n; i <= iE; i++) p[Cnt(i)]++;     minn[1] = 1;     for (int i = 2, iE = mx; i <= iE; i++) minn[i] = minn[i - 1] * 10;     for (int i = 1, iE = n - 1; i <= iE; i++)       scanf( %s%s , sa, sb), D[i][0] = strlen(sa), D[i][1] = strlen(sb),                              a[D[i][0]][D[i][1]]++;     if (!check()) return puts( -1 ), 0;     while (get()) sum++;     for (int x = 1, xE = mx; x <= xE; x++)       for (int i = 1, iE = mx; i <= iE; i++)         if (a[x][i])           printf( %d %d n , minn[x] + p[x] - 1, minn[i] + p[i] - 1), sum++;     if (sum != n - 1) assert(0);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, k, l, n, m, A[11] = {0}, count = 0, B[2000], sum1, sum2, last,                           flag = 0;     char ch;     for (i = 1; i <= 10; i++) {       scanf( %c , &ch);       if (ch ==  1 )         A[i] = 1;       else         A[i] = 0;     }     scanf( %d , &m);     for (k = 1; k <= 10; k++) {       if (A[k] == 1) {         flag = 0;         sum1 = k;         sum2 = 0;         last = k;         B[0] = k;         i = 1;         while (i < m) {           for (j = 1; j <= 10; j++) {             if (A[j] == 1) {               if (sum2 + j > sum1 && last != j) {                 sum2 += j;                 last = j;                 B[i++] = j;                 break;               }             }           }           if (j == 11) {             flag = 1;             break;           }           if (i == m) break;           for (j = 1; j <= 10; j++) {             if (A[j] == 1) {               if (sum1 + j > sum2 && last != j) {                 sum1 += j;                 last = j;                 B[i++] = j;                 break;               }             }           }           if (j == 11) {             flag = 1;             break;           }         }       }       if (i == m) break;     }     if (k == 11)       printf( NO n );     else if (i == m) {       printf( YES n );       for (i = 0; i < m; i++) {         if (i != m - 1)           printf( %d  , B[i]);         else           printf( %d n , B[i]);       }     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;   typedef long long ll;      void solve()   {       int x,y;       cin>>x>>y;       string s;       cin>>s;       int cR=0,cU=0,cL=0,cD=0;       for(int i=0;i<s.size();i++)       {           if(s[i]== R )               cR++;           else if(s[i]== U )               cU++;           else if(s[i]== L )               cL++;           else               cD++;       }       //cout<<cR<<   <<cL<<   <<cU<<   <<cD<<  n ;       if(x>0&&y>0)       {           if(cR>=x&&cU>=y)               cout<< YES n ;           else               cout<< NO n ;       }       else if(x>0&&y<0)       {           if(cR>=x&&cD>=abs(y))               cout<< YES n ;           else               cout<< NO n ;       }       else if(x<0&&y>0)       {           if(cL>=abs(x)&&cU>=y)               cout<< YES n ;           else               cout<< NO n ;       }       else if(x<0&&y<0)       {           if(cL>=abs(x)&&cD>=abs(y))               cout<< YES n ;           else               cout<< NO n ;       }       else       {           if(x>0&&y==0)           {               if(cR>=x)                   cout<< YES n ;               else                   cout<< NO n ;           }           else if(x<0&&y==0)           {               if(cL>=abs(x))                   cout<< YES n ;               else                   cout<< NO n ;           }           else if(x==0&&y>0)           {               if(cU>=y)                   cout<< YES n ;               else                   cout<< NO n ;           }           else if(x==0&&y<0)           {               if(cD>=abs(y))                   cout<< YES n ;               else                   cout<< NO n ;           }           else               cout<< NO n ;       }   }   int main()   {      int t;      ios::sync_with_stdio(false);      cin>>t;      cin.tie(0);      cout.tie(0);      while(t--)       solve();      return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int f = 1, ans = 0;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       ans = ans * 10 + c -  0 ;       c = getchar();     }     return f * ans;   }   const int MAXN = 4e5 + 11;   vector<int> vec[MAXN];   int ch[MAXN][27], las = 1, tot = 1, TT = 0, N, Q, len[MAXN], fa[MAXN], rt[MAXN],                     all;   char S[MAXN];   struct Segment {     int ls[MAXN * 34], rs[MAXN * 34];     void Modify(int &k, int l, int r, int ps) {       if (!k) k = ++TT;       if (l == r) return;       int mid = (l + r) >> 1;       if (ps <= mid) Modify(ls[k], l, mid, ps);       if (mid < ps) Modify(rs[k], mid + 1, r, ps);       return;     }     void print(int k, int l, int r) {       if (!k) return;       if (l == r) {         printf( %d  , l);         return;       }       int mid = (l + r) >> 1;       print(ls[k], l, mid), print(rs[k], mid + 1, r);     }     int merge(int p, int q) {       if (!p || !q) return p + q;       int u = (++TT);       ls[u] = merge(ls[p], ls[q]), rs[u] = merge(rs[p], rs[q]);       return u;     }     bool Query(int k, int l, int r, int x, int y) {       if (!k) return 0;       if (x <= l && r <= y) return 1;       int mid = (l + r) >> 1;       bool res = 0;       if (x <= mid) res |= Query(ls[k], l, mid, x, y);       if (mid < y) res |= Query(rs[k], mid + 1, r, x, y);       return res;     }     void debug(int k) {       print(rt[k], 1, N);       printf(  n );       return;     }   } T;   void ins(int c) {     int p = las, np = las = ++tot;     len[np] = len[p] + 1;     T.Modify(rt[np], 1, N, len[np]);     for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = np;     if (!p) {       fa[np] = 1;       return;     }     int q = ch[p][c];     if (len[p] + 1 == len[q]) {       fa[np] = q;       return;     }     int nq = ++tot;     len[nq] = len[p] + 1, fa[nq] = fa[q];     for (int i = 0; i < 27; i++) ch[nq][i] = ch[q][i];     fa[q] = fa[np] = nq;     for (; p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq;     return;   }   void dfs(int u) {     for (auto v : vec[u]) dfs(v), rt[u] = T.merge(rt[u], rt[v]);   }   int L, R, LEN;   char Ans[MAXN];   bool ff = 1;   void calc(int now, int len, bool f) {     if (!f) {       for (int i = 1; i < len; i++) printf( %c , Ans[i]);       printf(  n );       ff = 0;       return;     }     int be;     if (!f || len > LEN)       be = 0;     else       be = S[len] -  a ;     for (int i = be; i < 26; i++) {       int u = ch[now][i];       if (!u) continue;       if (!T.Query(rt[u], 1, N, L + len - 1, R)) continue;       Ans[len] = i +  a ;       calc(u, len + 1, f & (i == (S[len] -  a )));       if (!ff) return;     }   }   int main() {     scanf( %s , S + 1);     N = strlen(S + 1);     Q = read();     for (int i = 1; i <= N; i++) ins(S[i] -  a );     for (int i = 2; i <= tot; i++) vec[fa[i]].push_back(i);     dfs(1);     while (Q--) {       L = read(), R = read();       scanf( %s , S + 1);       LEN = strlen(S + 1);       ff = 1;       calc(1, 1, 1);       if (ff) printf( -1 n );     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n, k;     cin >> n >> k;     vector<long long> a(n);     for (int i = 0; i < n; i++) cin >> a[i];     ;     sort(a.begin(), a.end());     long long mm = a[n / 2];     for (int i = n / 2; i < n - 1; i++) {       long long r = a[i + 1] - a[i];       if (k >= r * (i - n / 2 + 1))         k -= r * (i - n / 2 + 1);       else if (k > 0) {         mm += k / (i - n / 2 + 1);         k = 0;         break;       } else         break;       mm = a[i + 1];     }     if (k > 0) mm += k / (n / 2 + 1);     cout << mm <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 200005;   int n, m, tot, ans;   struct Node {     int u, v, w;   } node[M];   int vis[M], match[M];   int sum1[M], sum2[M];   int head[M], Nxt[M], ver[M];   bool v[M], flag = 0;   vector<int> e[M];   inline bool dfs(long long x) {     for (int i = 0; i < e[x].size(); i++) {       long long y = e[x][i];       if (!vis[y]) {         vis[y] = 1;         if (!match[y] || dfs(match[y])) {           match[y] = x;           return 1;         }       }     }     return 0;   }   bool cmp(Node a, Node b) { return a.w > b.w; }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d%d , &node[i].u, &node[i].v, &node[i].w);       sum1[node[i].u]++;       sum2[node[i].v]++;       e[node[i].u].push_back(node[i].v);     }     for (int i = 1; i <= n; i++) {       memset(vis, 0, sizeof(vis));       if (dfs(i)) ans++;     }     if (ans != n) {       printf( -1 n );       return 0;     }     sort(node + 1, node + 1 + m, cmp);     for (int i = 1; i <= m; i++) {       sum1[node[i].u]--;       sum2[node[i].v]--;       if (sum1[node[i].u] == 0 || sum2[node[i].v] == 0) {         printf( %d n , node[i].w);         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int z = 0; z < t; z++) {       int n;       cin >> n;       string s;       cin >> s;       bool found = false;       for (int i = 0; i <= n - 11; i++) {         if (s[i] ==  8 ) {           cout <<  YES  << endl;           found = true;           break;         }       }       if (found) {         continue;       }       cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void show(const vector<T> &a) {     for (T x : a) cout << x <<    ;     cout <<   n ;   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count());   vector<long long> xx = {1, 0, -1, 0};   vector<long long> yy = {0, 1, 0, -1};   string dir =  LRUD ;   const long long N = 3e5 + 50, oo = 2e18 + 500;   const long long mod = 998244353;   const long long M2 = 1000000093, x2 = 27162;   const long long M1 = 1000000087, x1 = 241;   const double eps = 1e-12, PI = 2 * acos(0.0);   long long n, m, k;   long long cnt = 0;   long long lg = 18;   vector<vector<long long>> g;   vector<vector<long long>> rg;   signed main() {     ios::sync_with_stdio(0);     cout.tie(0);     cin.tie(0);     string s;     cin >> s;     n = ((long long)(s.size()));     vector<vector<string>> vc(26);     for (long long i = 0; i < n; i++) {       string t = s.substr(i, n - i) + s.substr(0, i);       vc[s[i] -  a ].push_back(t);     }     double ans = 0;     for (long long i = 0; i < 26; i++) {       if (!((long long)(vc[i].size()))) continue;       long long val = 0;       for (long long j = 0; j < n; j++) {         vector<long long> count_letters(26);         for (long long z = 0; z < ((long long)(vc[i].size())); z++) {           count_letters[vc[i][z][j] -  a ]++;         }         bool fl = 1;         long long cur = 0;         for (long long z = 0; z < 26; z++) {           if (count_letters[z] == 1) {             cur++;           } else {             if (count_letters[z]) fl = 0;           }         }         if (fl) {           val = ((long long)(vc[i].size()));           break;         }         val = max(val, cur);         ;       }       cnt += val;     }     ans = (double)cnt / (double)n;     cout << fixed << setprecision(10) << ans;   }


#include<bits/stdc++.h>   using namespace std ;      # define all(v) (v).begin() , (v).end()   # define allrev(v) (v).rbegin() , (v).rend()   # define allcomp(v) (v).begin() , (v).end() , comp      # define ll long long      # define line cout <<   n  ;   # define fast ios_base :: sync_with_stdio ( false ) ; cin.tie ( 0 ) ;      # define pii pair < int , int >   # define pll pair < ll , ll >      # define F first   # define S second      const int mod = 998244353  ;      const int dx[] = { -1 , 0 , 1 , 0 , -1 , -1 , 1 , 1 } ;   const int dy[] = { 0 , 1 , 0 , -1 , -1 , 1 , -1 , 1 } ;      ll mod_pow ( ll a , ll b )   {       if ( b == 0 )           return 1 ;          if ( b == 1 )           return a%mod ;          if ( b == 2 )       {           return ((a%mod) * (a%mod))%mod ;       }          return (mod_pow ( mod_pow ( a , b>>1 ) , 2 ) * mod_pow ( a , b&1 ))%mod ;   }      void solve ( int test_case )   {       int n ;       cin >> n ;          int bak = n ;          int nr = 0 ;       if ( n <= 2 )           nr = 1 ;             else       {           n -= 2 ;           ll a = 1 , b = 1 ;              while ( n -- )           {               b += a ;               a = b - a ;                  a %= mod ;               b %= mod ;           }              nr = b ;       }          ll f = 1 ;       for ( int i = 1 ; i <= bak ; i ++ )           f <<= 1 , f %= mod ;      //    cout <<  nr :   << nr <<   and dr :   << f ; line ;          f = mod_pow ( f , mod - 2 ) ;                ll ans = nr * f ;       ans %= mod ;          cout << ans ; line ;   }      int main()   {fast       int t = 1 ;       // cin >> t ;          for ( int i = 1 ; i <= t ; i ++ )       {           solve ( i ) ;       }          return 0 ;   }


#include <bits/stdc++.h>   using namespace std;   map<long long, long long> mp;   void Hash(string str, long long B, long long mod) {     long long power = 1, h = 0;     long long m = (int)str.length();     for (long long i = m - 1; i >= 0; i--) {       h = h + (power * (str[i] -  a  + 1)) % mod;       h = h % mod;       power = (power * B) % mod;     }     mp[h]++;   }   bool match(string str, long long B, long long mod) {     long long power = 1, h = 0;     long long m = (int)str.length();     for (long long i = m - 1; i >= 0; i--) {       h = h + (power * (str[i] -  a  + 1)) % mod;       h = h % mod;       power = (power * B) % mod;     }     power = 1;     for (long long i = m - 1; i >= 0; i--) {       long long t1, t2;       if (str[i] ==  a ) {         t1 = h + power;         t1 = t1 % mod;         t2 = t1 + power;         t2 = t2 % mod;       } else if (str[i] ==  b ) {         t1 = h - power;         t1 = (t1 + mod) % mod;         t2 = h + power;         t2 = (t2 + mod) % mod;       } else {         t1 = h - power;         t1 = (t1 + mod) % mod;         t2 = t1 - power;         t2 = (t2 + mod) % mod;       }       power = (power * B) % mod;       if (mp[t1] != 0) return true;       if (mp[t2] != 0) return true;     }     return false;   }   int main() {     long long B[10], mod[10];     B[0] = 137, B[1] = 347;     mod[0] = 100404001, mod[1] = 1000000007;     int n, m;     scanf( %d %d , &n, &m);     string str[n], query[m];     for (int i = 0; i < n; i++) cin >> str[i];     for (int i = 0; i < m; i++) cin >> query[i];     bool ans[m];     memset(ans, true, sizeof ans);     for (int i = 0; i < 2; i++) {       mp.clear();       for (int j = 0; j < n; j++) {         Hash(str[j], B[i], mod[i]);       }       for (int j = 0; j < m; j++) {         if (match(query[j], B[i], mod[i]) == false) ans[j] = false;       }     }     for (int j = 0; j < m; j++) {       if (ans[j])         printf( YES n );       else         printf( NO n );     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];     }     int l = n - k;     int max_1 = 0;     for (int i = 0; i < (n - k); i++) {       max_1 = max(max_1, a[i] + a[2 * (n - k) - i - 1]);     }     max_1 = max(max_1, a[n - 1]);     cout << max_1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long max(long long a, long long b) {     if (a < b) return b;     return a;   }   long long min(long long a, long long b) {     if (a < b) return a;     return b;   }   long long X;   vector<long long> x;   long long dfs(long long node, long long par, vector<vector<long long> > &v) {     long long cnt = 0;     for (int i = 0; i < v[node].size(); i++) {       long long adj = v[node][i];       if (adj == par) continue;       long long y = dfs(adj, node, v);       if (node == X) x.push_back(y);       cnt += y;     }     return cnt + 1;   }   void solve() {     x.clear();     long long n;     cin >> n >> X;     vector<vector<long long> > v(n + 1);     long long degree[n + 1];     memset(degree, 0, sizeof(degree));     vector<vector<long long> > comp;     for (int i = 0; i < n - 1; i++) {       long long a, b;       cin >> a >> b;       degree[a]++, degree[b]++;       v[a].push_back(b);       v[b].push_back(a);     }     if (degree[X] == 1 || n == 1)       cout <<  Ayush n ;     else {       dfs(X, -1, v);       long long cnt = 0;       for (int i = 0; i < x.size(); i++) cnt += max(0, x[i] - 1);       cnt += max(0, v[X].size() - 1);       if (cnt % 2 == 0) {         cout <<  Ayush n ;       } else         cout <<  Ashish n ;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long test = 0;     cin >> test;     while (test--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t, n, a, b, p, q, z, k;     cin >> t;     while (t--) {       z = 0;       k = 0;       cin >> a >> b >> n;       p = max(a, b);       q = min(a, b);       while (k <= n) {         z++;         k = q + p;         q = p;         p = k;       }       cout << z << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int t, a[30], n, b[2010], r;   vector<int> aux[30];   int val(int x, int y) {     int hr = 0, hxt = 0;     for (int i = 1; i <= aux[x].size() / 2; i++) {       hxt = lower_bound(aux[y].begin(), aux[y].end(), aux[x][aux[x].size() - i]) -             upper_bound(aux[y].begin(), aux[y].end(), aux[x][i - 1]);       if (hxt > 0) hr = max(hr, 2 * i + hxt);     }     return hr;   }   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       for (int i = 1; i <= 26; i++) aux[i].clear();       r = 0;       for (int i = 0; i < n; i++) {         scanf( %d , &b[i]);         aux[b[i]].push_back(i);         int h = aux[b[i]].size();         r = max(r, h);       }       for (int i = 1; i < 26; i++)         for (int j = i + 1; j <= 26; j++) r = max(r, max(val(i, j), val(j, i)));       printf( %d n , r);     }     return 0;   }


#pragma GCC optimize(3,  Ofast ,  inline )      #include <bits/stdc++.h>      #define rep(i,a,b) for(int i=(a);i<=(b);i++)   using namespace std;   typedef long long ll;   typedef unsigned long long ull;   typedef pair<int, int> P;   typedef pair<double, double> Pd;   typedef pair<ll, ll> pll;   const int maxn = 1e6+10;            int n,m;   int a[111][111];   struct node   {    int x,y;    int v;   }e[10005];      int tot = 0;   bool cmp(node A,node B) {    return A.v<B.v;   }   int main()   {   #ifndef ONLINE_JUDGE       freopen( /volumes/D/acm/1.txt ,  r , stdin);   #endif            int T;       scanf( %d ,&T);       while(T--) {        tot = 0;        scanf( %d%d ,&n,&m);        rep(i,1,n) {         rep(j,1,m) {          scanf( %d ,&a[i][j]);          e[++tot]={i,j,a[i][j]};         }        }        sort(e+1,e+1+tot,cmp);        for(int i=1;i<=m;i++) {         int x = e[i].x;         int y = e[i].y;         int z = e[i].v;         for(int j=i+1;j<=m;j++) {          if(e[j].v==a[x][i]&&e[j].x==x&&e[j].y==i) {           e[j].y=y;           break;          }         }         swap(a[x][i],a[x][y]);        }        rep(i,1,n) {         rep(j,1,m) {          printf( %d ,a[i][j]);          if(j!=m) printf(   );          else printf(  n );         }        }       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200100;   mt19937 rnd;   struct Treap {     int id, y, best;     Treap *l, *r;     Treap(int id) : id(id), y(rnd()), best(id), l(NULL), r(NULL) {}   };   int n, m;   int x[MAXN];   long long len[MAXN];   int p[MAXN], b[MAXN];   set<pair<int, int> > alive;   int c[MAXN];   void update(Treap *&t) {     if (t) {       t->best = t->id;       if (t->l && x[t->l->best] < x[t->best]) t->best = t->l->best;       if (t->r && x[t->r->best] < x[t->best]) t->best = t->r->best;     }   }   void merge(Treap *&t, Treap *l, Treap *r) {     if (!l)       t = r;     else if (!r)       t = l;     else if (l->y < r->y) {       merge(l->r, l->r, r);       t = l;     } else {       merge(r->l, l, r->l);       t = r;     }     update(t);   }   void split(Treap *t, Treap *&l, Treap *&r, long long k, int id) {     if (!t) {       l = r = NULL;     } else if (x[t->id] + len[t->id] < k ||                (x[t->id] + len[t->id] == k && t->id < id)) {       split(t->r, t->r, r, k, id);       l = t;     } else {       split(t->l, l, t->l, k, id);       r = t;       update(t);     }   }   Treap *t;   void insert(int id) {     Treap *L, *R;     split(t, L, R, x[id] + len[id], id);     merge(t, L, new Treap(id));     merge(t, t, R);   }   void erase(Treap *&t, int id) {     if (t->id == id) {       merge(t, t->l, t->r);     } else if (x[t->id] + len[t->id] < x[id] + len[id] ||                (x[t->id] + len[t->id] == x[id] + len[id] && t->id < id)) {       erase(t->r, id);     } else {       erase(t->l, id);     }     update(t);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     rnd = mt19937(0xdeadbeef);     cin >> n >> m;     for (int i = 0; i < n; ++i) {       cin >> x[i] >> len[i];       insert(i);     }     for (int i = 0; i < m; ++i) {       cin >> p[i] >> b[i];       Treap *L, *R;       split(t, L, R, p[i], 0);       int best = R ? R->best : -1;       merge(t, L, R);       if (best != -1 && x[best] <= p[i]) {         erase(t, best);         len[best] += b[i];         insert(best);         ++c[best];         while (true) {           auto it = alive.upper_bound(make_pair(x[best] - 1, 1e9));           if (it != alive.end() && it->first <= x[best] + len[best]) {             ++c[best];             erase(t, best);             len[best] += b[it->second];             insert(best);             alive.erase(it);           } else             break;         }       } else {         alive.insert(make_pair(p[i], i));       }     }     for (int i = 0; i < n; ++i) cout << c[i] <<     << len[i] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       string s, s1, s2;       cin >> s >> s1 >> s2;       bool lock = false;       for (int i = 0; i < s.length(); i++) {         if (s[i] == s2[i]) {           swap(s1[i], s2[i]);         } else {           if (s1[i] == s2[i]) {             swap(s[i], s2[i]);           } else {             lock = true;             break;           }         }       }       if (lock)         cout <<  NO  << endl;       else {         if (s == s1)           cout <<  YES  << endl;         else           cout <<  NO  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int sz = 2e5;   long long int col[sz], vis[sz];   vector<long long int> adj[sz];   long long int n;   void reset() {     long long int i;     for (i = 0; i < n + 14; i++) {       col[i] = -1;       adj[i].clear();       vis[i] = 0;     }   }   void coloring(long long int n, long long int c) {     vis[n] = 1;     col[n] = c;     for (auto it : adj[n]) {       if (vis[it] == 0) coloring(it, c ^ 1);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long int i, j, k, len, x, y, z, c, f, flag, p, q, mx, mn, l, r, sum, ans,         tmp, it, pos, avg, m, cnt;     long long int t;     cin >> t;     while (t--) {       f = 0;       flag = 0;       ans = 0;       cnt = 0;       sum = 0;       mn = LLONG_MAX;       mx = LLONG_MIN;       string s;       cin >> n >> m;       reset();       for (i = 0; i < m; i++) {         cin >> l >> r;         adj[l].push_back(r);         adj[r].push_back(l);       }       coloring(1, 0);       vector<long long int> sol1, sol2;       for (i = 1; i <= n; i++) {         if (col[i])           sol1.push_back(i);         else           sol2.push_back(i);       }       if (sol2.size() < sol1.size()) {         cout << sol2.size() <<   n ;         for (auto dsp : sol2) cout << dsp <<    ;         cout <<   n ;         ;       } else {         cout << sol1.size() <<   n ;         for (auto dsp : sol1) cout << dsp <<    ;         cout <<   n ;         ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int lbracket = s.length(), rbracket = -1, lcolon = s.length(), rcolon = -1;     for (int i = 0; i < s.length(); i++)       if (s[i] ==  [ ) {         lbracket = i;         break;       }     for (int i = lbracket; i < s.length(); i++)       if (s[i] ==  : ) {         lcolon = i;         break;       }     for (int i = s.length() - 1; i > -1; i--)       if (s[i] ==  ] ) {         rbracket = i;         break;       }     for (int i = rbracket; i > -1; i--)       if (s[i] ==  : ) {         rcolon = i;         break;       }     if (lcolon < rcolon) {       int ans = 4;       for (int i = lcolon + 1; i < rcolon; i++)         if (s[i] ==  | ) ans++;       cout << ans;     } else       cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NMAX = 1001000;   long long a[NMAX * 2];   long long n, l, t;   long long cnt(int i, int j) {     long long v = (a[i] - a[j] + t * 2 + l) / l;     return v;   }   long double ans(int i) {     int lo = i + 2, hi = i + n;     long long tv = cnt(i, i + 1);     while (lo < hi) {       int m = (lo + hi) / 2;       if (cnt(i, m) < tv) {         hi = m;       } else {         lo = m + 1;       }     }     if (hi == i + n) {       long double v = (long double)tv * (n - 1) / 4;       return v;     }     long double v =         (long double)((tv - 1) * (i + n - hi) + tv * (hi - i - 1)) / 4;     return v;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0);     cin >> n >> l >> t;     for (int i = 0; i < n; ++i) {       cin >> a[i];       a[i + n] = a[i] + l;     }     long double out = 0;     for (int i = 0; i < n; ++i) {       out += ans(i);     }     cout << fixed << setprecision(5) << out << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long Mod = 1e9 + 7;   const long long maxn = 700 + 10;   long long n;   long long brac[maxn];   char s[maxn];   stack<long long> sta;   inline long long read() {     long long x = 0;     bool fopt = 1;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) fopt = 0;     for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + ch - 48;     return fopt ? x : -x;   }   long long f[maxn][maxn][3][3];   void dfs(long long l, long long r) {     if (r == l + 1)       return f[l][r][0][1] = f[l][r][1][0] = f[l][r][0][2] = f[l][r][2][0] = 1,              void();     if (brac[l] == r) {       dfs(l + 1, r - 1);       for (long long i = 0; i <= 2; i++)         for (long long j = 0; j <= 2; j++) {           if (i != 1)             f[l][r][1][0] = (f[l][r][1][0] + f[l + 1][r - 1][i][j]) % Mod;           if (i != 2)             f[l][r][2][0] = (f[l][r][2][0] + f[l + 1][r - 1][i][j]) % Mod;           if (j != 1)             f[l][r][0][1] = (f[l][r][0][1] + f[l + 1][r - 1][i][j]) % Mod;           if (j != 2)             f[l][r][0][2] = (f[l][r][0][2] + f[l + 1][r - 1][i][j]) % Mod;         }     } else {       dfs(l, brac[l]);       dfs(brac[l] + 1, r);       for (long long i = 0; i <= 2; i++)         for (long long j = 0; j <= 2; j++)           for (long long k = 0; k <= 2; k++)             for (long long q = 0; q <= 2; q++)               if (!(j && j == k))                 f[l][r][i][q] =                     (f[l][r][i][q] +                      f[l][brac[l]][i][j] * f[brac[l] + 1][r][k][q] % Mod) %                     Mod;     }   }   signed main() {     scanf( %s , s + 1);     n = strlen(s + 1);     for (long long i = 1; i <= n; i++) {       if (s[i] ==  ( )         sta.push(i);       else         brac[sta.top()] = i, sta.pop();     }     dfs(1, n);     long long ans = 0;     for (long long i = 0; i <= 2; i++)       for (long long j = 0; j <= 2; j++) ans = (ans + f[1][n][i][j]) % Mod;     cout << ans << endl;     return 0;   }


#include<bits/stdc++.h>   #pragma GCC optimize ( -O3 )   #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);   #define ll long long   #define double long double   #define pb push_back   #define mod 1000000007   #define int long long   #define endl   n    #define ss second   #define ff first   #define INF 1000000000   #define all(a) a.begin(),a.end()   #define gcd(a, b) __gcd((a), (b))   //ceil(a/b) is equal to (a+b-1)/b   using namespace std;         int power(int a,int n,int p)   {       int res=1;       while(n)       {           if(n%2)           {           res=(res*a)%p;           n--;}           else           {           a=(a*a)%p;           n=n/2;}       }       return res;   }          int binarySearch(int arr[],int l,int r,int x)    {        int mid;       while(l<=r)       {           mid=(l+r)/2;           if(arr[mid]==x)               return mid;           if(arr[mid]<x)               l=mid+1;           else               r=mid-1;       }       return -1;   }           int fact(int n,int k)       //for calculating nCk   {       int ans=1;       if(k>n-k)       {           k=n-k;       }       for(int i=1;i<=k;i++)       {           ans*=(n-i+1);           ans/=i;       }       return ans;   }   // cout << std::fixed;   // std::cout << std::setprecision(9);    // auto it = lower_bound(v.begin(), v.end(), n);           int32_t main()   {       IOS;       int t;       cin>>t;       while(t--)       {           int n;           cin>>n;           int a[n+1];              std::map<int, int> m;           for(int i=0;i<n;i++){               cin>>a[i];               m[a[i]]++;           }           set<int>s;           std::map<int, int> m1;           for(auto x:m)           {               s.insert(x.ss);               m1[x.ss]++;           }           int ans=INF;           for(int x:s)           {               int cnt=0;               for(auto y:m1)               {                   if(y.ff<x){                       cnt+=y.ss*y.ff;                   }                   else                   {                       cnt+=abs(y.ff-x)*y.ss;                   }               }               ans=min(ans,cnt);           }              cout<<ans<<endl;          }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 15e4 + 10;   long long a[N];   bool vis[N];   int main() {     int n;     scanf( %d , &n);     set<pair<long long, int> > se;     for (int i = 0; i < n; i++) {       scanf( %lld , a + i);       se.insert(pair<long long, int>(a[i], i));     }     int sz = n;     while (!se.empty()) {       pair<long long, int> x = *se.begin();       if (!se.empty()) {         se.erase(se.begin());         pair<long long, int> y = *se.begin();         if (x.first == y.first) {           se.erase(se.begin());           se.insert(pair<long long, int>(y.first * 2, y.second));           vis[x.second] = true;           sz--;           a[y.second] = y.first * 2;         }       } else         break;     }     printf( %d n , sz);     for (int i = 0; i < n; i++) {       if (vis[i]) continue;       printf( %lld  , a[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 2005;   int n;   int p[Maxn], b[Maxn];   int inb[Maxn];   int res;   int best[Maxn];   vector<pair<int, int> > seq;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &p[i]);     for (int i = 1; i <= n; i++) {       scanf( %d , &b[i]);       inb[b[i]] = i;     }     for (int i = 1; i <= n; i++) p[i] = inb[p[i]];     for (int i = 1; i <= n; i++) {       int pos = i;       while (p[pos] != i) pos++;       res += pos - i;       best[i] = p[i];       for (int j = i + 1; j < pos; j++) best[j] = max(best[j - 1], p[j]);       for (int j = pos - 1; j >= i; j--)         if (best[j] == p[j]) {           seq.push_back(pair<int, int>(j, pos));           swap(p[j], p[pos]);           pos = j;         }     }     printf( %d n , res);     printf( %d n , seq.size());     for (int i = 0; i < seq.size(); i++)       printf( %d %d n , seq[i].first, seq[i].second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<pair<int, int> > p;   set<pair<int, int> > h;   set<pair<int, int> > v;   int cut();   int cut(set<pair<int, int> >::iterator s, set<pair<int, int> >::iterator e) {     int c = 0;     vector<int> all;     while (s != e) {       all.push_back(s->second);       ++s;     }     for (int &x : all) {       h.erase(pair<int, int>(p[x].first, x));       v.erase(pair<int, int>(p[x].second, x));     }     c += cut();     h.clear();     v.clear();     for (int &x : all) {       h.insert(pair<int, int>(p[x].first, x));       v.insert(pair<int, int>(p[x].second, x));     }     c += cut();     return c;   }   int cut() {     set<pair<int, int> >::iterator xs = h.begin();     set<pair<int, int> >::iterator ys = v.begin();     set<pair<int, int> >::iterator xe = h.end();     set<pair<int, int> >::iterator ye = v.end();     --xe;     --ye;     for (int i = 0; i < (int)h.size() / 2; ++i) {       set<pair<int, int> >::iterator nxs = xs;       ++nxs;       if (xs->first + 1 < nxs->first) {         return cut(h.begin(), nxs);       }       ++xs;       set<pair<int, int> >::iterator nxe = xe;       --xe;       if (xe->first + 1 < nxe->first) {         return cut(nxe, h.end());       }       set<pair<int, int> >::iterator nys = ys;       ++nys;       if (ys->first + 1 < nys->first) {         return cut(v.begin(), nys);       }       ++ys;       set<pair<int, int> >::iterator nye = ye;       --ye;       if (ye->first + 1 < nye->first) {         return cut(nye, v.end());       }     }     return 1;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     for (int i = 0; i < n; ++i) {       int x, y;       cin >> x >> y;       p.push_back(pair<int, int>(x, y));       h.insert(pair<int, int>(x, i));       v.insert(pair<int, int>(y, i));     }     cout << cut() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, ans, x, mn = 100001;   vector<int> A, B;   map<int, int> Ps1, Ps2, Mn;   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x;       Ps1[x] = i;     }     for (int i = 0; i < n; i++) {       cin >> x;       Ps2[i] = Ps1[x];     }     for (int i = n - 1; i >= 0; i--) {       mn = min(mn, Ps2[i]);       Mn[i] = mn;     }     for (int i = 0; i < n; i++) {       if (Ps2[i] > Mn[i]) ans++;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int num[5005];   int rmq(int l, int r) {     int ans = 1 << 30, tmp = l;     for (int i = l; i <= r; i++) {       if (ans > num[i]) {         ans = num[i];         tmp = i;       }     }     return tmp;   }   int get(int l, int r, int h) {     if (l > r) return 0;     if (l == r) {       if (num[l] == h)         return 0;       else         return 1;     }     int tmp = rmq(l, r);     int ans = 0;     ans = min(r - l + 1, get(l, tmp - 1, num[tmp]) + get(tmp + 1, r, num[tmp]) +                              num[tmp] - h);     return ans;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &num[i]);     int ans = get(0, n - 1, 0);     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int g2[1002][1002];   int main() {     int n, m;     cin >> n >> m;     for (int i = n; i <= m; i++) {       int ind = 0;       string s = to_string(i);       string m = s;       sort(s.begin(), s.end());       for (int p = 1; p < s.size(); p++)         if (s[p] == s[p - 1]) {           ind = 1;           break;         }       if (ind == 1) continue;       cout << m;       return 0;     }     cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int c1, c2, x, y;   long long int comp(long long int t) {     long long int r1 = t / x, r2 = t / y, l = x * y, both = t / l;     long long int left = t - r1 - r2 + both;     r1 = r1 - both;     r2 = r2 - both;     long long int left1 = c1 - r2, left2 = c2 - r1;     if (left1 < 0) left1 = 0;     if (left2 < 0) left2 = 0;     if (left1 + left2 <= left)       return 1;     else       return 0;   }   long long int bs() {     long long int st = 0, en = 1e18;     while (en - st > 1) {       long long int mid = (st + en) / 2;       if (comp(mid))         en = mid;       else         st = mid;     }     return en;   }   int main() {     cin >> c1 >> c2 >> x >> y;     cout << bs();   }


#include <bits/stdc++.h>   using namespace std;   long long n, cnt;   long long arr[100000], h[100000];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (long long i = 0; i <= n - 1; i++) {       cin >> arr[i];       h[i] = arr[i];     }     sort(h, h + n);     for (long long i = 0; i <= n - 1; i++) {       if (arr[i] != h[i]) cnt++;     }     if (cnt > 2)       cout <<  NO n ;     else       cout <<  YES n ;     cin >> n;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:1608864 )   void ASS(bool b) {     if (!b) ++*(int*)0;   }   int main() {     int n, m, k;     scanf( %d %d %d , &n, &m, &k);     const int inf = 1 << 30;     vector<int> v(m, inf);     for (int i = 0; i < (int)(n); i++) {       int x, y;       scanf( %d %d , &x, &y);       x--;       v[x] = min(v[x], y);     }     int res = 0;     for (int i = 0; i < (int)(m); i++) res += v[i];     res = min(res, k);     printf( %d n , res);     return 0;   }