#include <bits/stdc++.h>   using namespace std;   vector<long long> matches;   void computeLPSArray(vector<pair<char, long long> >& pat, long long M,                        long long* lps);   void KMPSearch(vector<pair<char, long long> >& pat,                  vector<pair<char, long long> >& txt) {     long long M = pat.size();     long long N = txt.size();     long long* lps = (long long*)malloc(sizeof(long long) * M);     long long j = 0;     computeLPSArray(pat, M, lps);     long long i = 0;     while (i < N) {       if (pat[j] == txt[i]) {         j++;         i++;       }       if (j == M) {         matches.push_back(i - j);         j = lps[j - 1];       } else if (i < N && pat[j] != txt[i]) {         if (j != 0)           j = lps[j - 1];         else           i = i + 1;       }     }     free(lps);   }   void computeLPSArray(vector<pair<char, long long> >& pat, long long M,                        long long* lps) {     long long len = 0;     long long i;     lps[0] = 0;     i = 1;     while (i < M) {       if (pat[i] == pat[len]) {         len++;         lps[i] = len;         i++;       } else {         if (len != 0) {           len = lps[len - 1];         } else {           lps[i] = 0;           i++;         }       }     }   }   int main() {     long long N, M;     cin >> N >> M;     bool s = false;     if (N == 199999) s = true;     vector<pair<char, long long> > txt(N), pat(M);     for (long long i = 0; i < N; i++)       scanf( %I64d-%c , &txt[i].second, &txt[i].first);     for (long long i = 0; i < M; i++)       scanf( %I64d-%c , &pat[i].second, &pat[i].first);     vector<pair<char, long long> > txtM;     char currc = txt[0].first;     long long currn = txt[0].second;     for (long long i = 1; i < txt.size(); i++) {       if (txt[i].first == currc)         currn += txt[i].second;       else {         txtM.push_back(make_pair(currc, currn));         currc = txt[i].first;         currn = txt[i].second;       }     }     txtM.push_back(make_pair(currc, currn));     vector<pair<char, long long> > patM;     currc = pat[0].first;     currn = pat[0].second;     for (long long i = 1; i < pat.size(); i++) {       if (pat[i].first == currc)         currn += pat[i].second;       else {         patM.push_back(make_pair(currc, currn));         currc = pat[i].first;         currn = pat[i].second;       }     }     patM.push_back(make_pair(currc, currn));     long long cnt = 0;     if (patM.size() == 1) {       for (long long i = 0; i < txtM.size(); i++) {         if (txtM[i].first == patM[0].first)           cnt += max(0LL, txtM[i].second - patM[0].second + 1);       }       cout << cnt << endl;       return 0;     } else if (patM.size() == 2) {       for (long long i = 0; i < txtM.size() - 1; i++) {         if (txtM[i].first == patM[0].first &&             txtM[i + 1].first == patM[1].first) {           if (txtM[i].second >= patM[0].second &&               txtM[i + 1].second >= patM[1].second)             cnt += 1;         }       }       cout << cnt << endl;       return 0;     }     vector<pair<char, long long> > mod(patM.begin() + 1,                                        patM.begin() + patM.size() - 1);     KMPSearch(mod, txtM);     for (long long i = 0; i < matches.size(); i++) {       if (matches[i] - 1 >= 0 && txtM[matches[i] - 1].first == patM[0].first &&           txtM[matches[i] - 1].second >= patM[0].second) {         long long fin = matches[i] + patM.size() - 2;         long long le = patM.size() - 1;         if (fin < txtM.size() && txtM[fin].first == patM[le].first &&             txtM[fin].second >= patM[le].second) {           cnt += 1;         }       }     }     cout << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   ostream& operator<<(ostream& os, vector<T> v) {     for (T x : v) os << x <<    ;     return os;   }   template <class T>   istream& operator>>(istream& is, vector<T> v) {     for (T& x : v) is >> x;     return is;   }   const long long INF = 1LL << 60;   bool startsWith(string s, string t) {     return s.size() >= t.size() && s.substr(0, t.size()) == t;   }   string chomp(string s) {     if (s.empty()) return   ;     long long start = -1, end = -1;     for (long long i = 0; i < (long long)(s.size()); i++) {       if (s[i] ==    ) continue;       start = i;       break;     }     for (long long i = s.size() - 1; i >= 0; i--) {       if (s[i] ==    ) continue;       end = i;       break;     }     string ans =   ;     for (long long i = start; i <= end; i++) ans.push_back(s[i]);     return ans;   }   string parseThrow(string s) {     long long a, b;     for (long long i = 0; i < (long long)(s.size()); i++) {       char c = s[i];       if (c ==  ( )         a = i;       else if (c ==  ) )         b = i;     }     return chomp(s.substr(a + 1, b - a - 1));   }   pair<string, string> parseCatch(string s) {     string a, b;     long long x, y = -1, z, w;     for (long long i = 0; i < (long long)(s.size()); i++) {       char c = s[i];       if (c ==  ( ) {         x = i;       } else if (c ==    ) {         if (y == -1)           y = i;         else           z = i;       } else if (c ==  , )         w = i;     }     a = chomp(s.substr(x + 1, w - x - 1));     b = chomp(s.substr(y + 1, z - y - 1));     return make_pair(a, b);   }   void run() {     long long n;     cin >> n;     cin.ignore();     vector<string> v;     string buf;     for (long long i = 0; i < (long long)(n); i++) {       getline(cin, buf);       v.push_back(chomp(buf));     }     string exn;     bool flg = false;     long long depth = 0;     for (long long i = 0; i < (long long)(n); i++) {       string s = v[i];       if (s.empty()) continue;       if (startsWith(s,  throw )) {         flg = true;         exn = parseThrow(s);       }       if (!flg) continue;       if (startsWith(s,  try )) {         depth++;       } else if (startsWith(s,  catch )) {         if (depth > 0) {           depth--;           continue;         } else {           pair<string, string> e = parseCatch(s);           if (e.first == exn) {             cout << e.second << endl;             return;           }         }       }     }     cout <<  Unhandled Exception  << endl;   }   int main() {     run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000 + 5;   const int mod = 1000 * 1000 * 1000 + 7;   vector<int> p[maxn];   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int k;       cin >> k;       p[i].resize(k);       for (int j = 0; j < k; j++) cin >> p[i][j];       sort(p[i].begin(), p[i].end());     }     int ans = 0;     for (int i = 0; i < n; i++) {       int pre = i ? i - 1 : n - 1;       int nxt = (i + 1) % n;       int p1 = 0, p2 = 0;       for (int j = 0; j < (int)p[i].size(); j++) {         int cnt = 0;         while (p1 < (int)p[pre].size() && p[pre][p1] < p[i][j]) cnt++, p1++;         while (p2 < (int)p[nxt].size() && p[nxt][p2] < p[i][j]) cnt--, p2++;         if (cnt != 0 && j != 0) ans++;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   int t;   long long n;   vector<int> v;   long long cnt[30];   long long pos[30];   long long fact[21];   void init() {     for (int i = 0; i < int(30); i++) cnt[i] = pos[i] = 0;     v.clear();   }   int main() {     cin.tie(0);     ios_base::sync_with_stdio(false);     fact[0] = 1;     for (int i = 1; i <= 20; i++) fact[i] = fact[i - 1] * i;     cin >> t;     while (t--) {       init();       long long n;       cin >> n;       int cur = 2;       while (n) {         v.push_back(n % cur);         n /= cur;         cur++;       }       sort(v.begin(), v.end());       for (int i = 0; i < int(v.size()); i++) cnt[v[i]]++;       pos[1] = cnt[0];       for (int i = 2; i <= v.size() + 1; i++) pos[i] = pos[i - 1] + cnt[i - 1];       long long A = 1;       for (int i = 2; i <= v.size() + 1; i++) A *= (pos[i] - (i - 2));       long long B = 1;       if (cnt[0] == 0)         B = 0;       else {         B = cnt[0];         for (int i = 2; i <= v.size(); i++) B *= (pos[i] - (i - 1));       }       long long ans = A - B;       for (int i = 0; i < int(21); i++) ans /= fact[cnt[i]];       cout << ans - 1 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   int n, m, K, w[N];   inline char nc() {     static char buf[100000], *p1 = buf, *p2 = buf;     if (p1 == p2) {       p2 = (p1 = buf) + fread(buf, 1, 100000, stdin);       if (p1 == p2) return EOF;     }     return *p1++;   }   inline void read(int& x) {     char tr = nc(), b = 1;     for (; tr <  0  || tr >  9 ; tr = nc())       if (tr ==  - ) b = -1;     for (x = 0; tr >=  0  && tr <=  9 ;          x = (x << 3) + (x << 1) + tr - 48, tr = nc())       ;     x *= b;   }   inline int count() {     int ret = 0, x;     for (int i = 0; i < n - 1; i++)       for (int j = 0; j < m - 1; j++)         x = i * m + j, ret += (w[x] & w[x + m] & w[x + 1]) +                               (w[x + m + 1] & w[x + m] & w[x + 1]) +                               (w[x] & w[x + m] & w[x + m + 1]) +                               (w[x] & w[x + 1] & w[x + m + 1]);     return ret;   }   int Q[N], l, r, ins[N];   const int dx[6] = {0, 0, 1, -1};   const int dy[6] = {1, -1, 0, 0};   inline int pd() {     l = r = -1;     for (int i = 0; i < n * m; i++) ins[i] = 0;     for (int i = 0; i < n * m; i++)       if (w[i]) {         Q[++r] = i;         break;       }     while (l < r) {       ++l;       int x = Q[l] / m, y = Q[l] % m;       for (int k = 0; k < 4; k++) {         int sx = x + dx[k], sy = y + dy[k];         if (sx < 0 || sy < 0 || sx >= n || sy >= m || ins[sx * m + sy] ||             !w[sx * m + sy])           continue;         Q[++r] = sx * m + sy;         ins[sx * m + sy] = 1;       }     }     for (int i = 0; i < n * m; i++)       if (w[i] && !ins[i]) return 0;     return 1;   }   inline int check(int mid) {     for (int i = 0; i < n * m; i++) w[i] = (i <= mid);     return count();   }   inline int Solve() {     int C = 4 * (n - 1) * (m - 1), L = -1, R = n * m, MID;     if (K > C) return 0;     if (n == 3 || m == 3) {       if (K == C - 1 || K == C - 2 || K == C - 4 || K == C - 5) return 0;     } else {       if (K == C - 1 || K == C - 2 || K == C - 4 || K == C - 5 || K == C - 8)         return 0;     }     while (L + 1 < R)       if (check(MID = (L + R) >> 1) >= K)         R = MID;       else         L = MID;     int cur = check(R);     if (cur == K) return 1;     vector<int> bl, em;     for (int i = 0; i < n * m; i++)       if (min(i / m, abs(i / m - R / m)) <= 1)         w[i] ? bl.push_back(i) : em.push_back(i);     int x, p;     while (cur != K) {       if (cur < K) {         x = em[p = rand() % ((int)em.size())];         w[x] = 1;         if (!pd()) {           w[x] = 0;           continue;         }         em.erase(em.begin() + p);         bl.push_back(x);         cur = count();       } else {         x = bl[p = rand() % ((int)bl.size())];         w[x] = 0;         if (!pd()) {           w[x] = 1;           continue;         }         bl.erase(bl.begin() + p);         em.push_back(x);         cur = count();       }     }     return 1;   }   int main() {     int T;     read(T);     while (T--) {       read(n);       read(m);       read(K);       int flag = 0;       if (n < m) swap(n, m), flag = 1;       if (Solve()) {         if (!flag) {           for (int i = 0; i < n; i++, putchar(  n ))             for (int j = 0; j < m; j++) putchar(w[i * m + j] ?  *  :  . );         } else {           for (int j = 0; j < m; j++, putchar(  n ))             for (int i = 0; i < n; i++) putchar(w[i * m + j] ?  *  :  . );         }         putchar(  n );       } else         printf( -1 n n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[12345];   int calc(int x, int y) {     int ret = 0;     vector<int> v;     for (int i = 0; i < 2 * n; ++i) {       if (i != x && i != y) v.push_back(a[i]);     }     for (int i = 0; i < v.size(); i += 2) ret += abs(v[i] - v[i + 1]);     return ret;   }   int main() {     cin >> n;     int mn = 1e9, i;     for (i = 0; i < 2 * n; i++) cin >> a[i];     sort(a, a + 2 * n);     for (int i = 0; i < n * 2; i++) {       for (int j = i + 1; j < n * 2; j++) {         mn = min(mn, calc(i, j));       }     }     cout << mn;   }


#include <iostream>   #include <cstdio>   #include <cmath>   #include <cstring>   #include <vector>   #include <map>      #include <set>   #include <cmath>   #include <queue>   #include <algorithm>   #include <numeric>   using namespace std;   typedef long long ll;   const int N=1e6+5;   const int M=1e3+5;   const int mod=1e9+7;   int n,m;   int T,a[N];   std::vector<int> ans;   int rns;   bool cal(vector<int> v,std::vector<int> v1){       for(int i=0;i<n*2;i++){           if(v[i]!=v1[i])return 0;       }       return 1;   }   map<std::vector<int>,int>mp;   void bfs(){       queue<vector<int> >q;       std::vector<int>G;       for(int i=1;i<=n*2;i++)G.push_back(i);           rns=-1;       q.push(G);       mp[G]=0;       while(!q.empty()){           std::vector<int> now=q.front();           q.pop();           if(cal(now,ans)==1){               rns=mp[now];               return ;           }           std::vector<int> v1=now;           for(int i=0;i+1<n*2;i+=2){               swap(v1[i],v1[i+1]);           }           std::vector<int> v2=now;           for(int i=0;i+n<n*2;i++){               swap(v2[i],v2[i+n]);           }           if(!mp.count(v1)){               q.push(v1);               mp[v1]=mp[now]+1;           }           if(!mp.count(v2)){               q.push(v2);               mp[v2]=mp[now]+1;           }       }               }   int main() {       scanf( %d ,&n);       for(int i=1;i<=n*2;i++)scanf( %d ,&a[i]),ans.push_back(a[i]);       bfs();   cout<<rns<<endl;                                     }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, c, i, a, m, l, r;     cin >> t >> i >> m >> a >> l;     r = i;     c = 1;     t = t - r;     while (t > 0) {       c++;       r = r + a;       if (r > m) r = m;       t = t + l - r;       if (t < 0) t = 0;     }     cout << c;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int q, n, m, u1, d1, l1, r1, u2, d2, l2, r2, u3, d3, l3, r3;   long long count(int a, int b) { return (1ll * a * b + 1) / 2; }   long long count(int u, int d, int l, int r) {     return count(d, r) + count(u - 1, l - 1) - count(d, l - 1) - count(u - 1, r);   }   int main() {     scanf( %d , &q);     while (q--) {       scanf( %d %d , &m, &n);       scanf( %d %d %d %d , &u1, &l1, &d1, &r1);       scanf( %d %d %d %d , &u2, &l2, &d2, &r2);       long long ans = count(n, m);       u3 = max(u1, u2), l3 = max(l1, l2);       d3 = min(d1, d2), r3 = min(r1, r2);       ans -= count(u2, d2, l2, r2);       ans += 1ll * (d1 - u1 + 1) * (r1 - l1 + 1) - count(u1, d1, l1, r1);       if (d3 >= u3 && r3 >= l3)         ans -= 1ll * (d3 - u3 + 1) * (r3 - l3 + 1) - count(u3, d3, l3, r3);       printf( %I64d %I64d n , ans, 1ll * n * m - ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   string in[1003];   int a[1003][1003];   int X[] = {-1, 0, 0, 1};   int Y[] = {0, -1, 1, 0};   bool d[1003][1003];   int getSm(int i, int j, int u, int v) {     return a[u][v] - a[i - 1][v] - a[u][j - 1] + a[i - 1][j - 1];   }   bool check() {     for (int i = 1; i <= n; i++) {       bool ok = 0;       int cnt = 0;       for (int j = 1; j <= m; j++) {         if (in[i][j] ==  # ) {           if (!ok) {             cnt++;             ok = 1;           }         } else {           ok = 0;         }       }       if (cnt > 1) {         return 0;       }       if (cnt == 0) {         ok = 0;         for (int j = 1; j <= m; j++) {           if (getSm(1, j, n, j) == 0) {             ok = 1;             continue;           }         }         if (!ok) {           return 0;         }       }     }     for (int j = 1; j <= m; j++) {       bool ok = 0;       int cnt = 0;       for (int i = 1; i <= n; i++) {         if (in[i][j] ==  # ) {           if (!ok) {             cnt++;             ok = 1;           }         } else {           ok = 0;         }       }       if (cnt > 1) {         return 0;       }       if (cnt == 0) {         ok = 0;         for (int i = 1; i <= n; i++) {           if (getSm(i, 1, i, m) == 0) {             ok = 1;             continue;           }         }         if (!ok) {           return 0;         }       }     }     return 1;   }   bool canC(int u, int v) {     return (1 <= u) && (u <= n) && (1 <= v) && (v <= m) && (in[u][v] ==  # ) &&            (d[u][v] == 0);   }   void DFS(int u, int v) {     d[u][v] = 1;     for (int k = 0; k < 4; k++) {       int nu = u + X[k];       int nv = v + Y[k];       if (canC(nu, nv)) {         DFS(nu, nv);       }     }   }   int main() {     ios::sync_with_stdio(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> in[i];       in[i].insert(0,    );       for (int j = 1; j <= m; j++)         a[i][j] = (in[i][j] ==  # ) + a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];     }     if (a[n][m] == 0) {       cout << 0;       return 0;     }     if (n == 1) {       int cnt = 0;       for (int j = 1; j <= m; j++) {         cnt += (in[1][j] ==  # );       }       if ((0 < cnt) && (cnt < m)) {         cout << -1;         return 0;       }     }     if (m == 1) {       int cnt = 0;       for (int i = 1; i <= n; i++) {         cnt += (in[i][1] ==  # );       }       if ((0 < cnt) && (cnt < n)) {         cout << -1;         return 0;       }     }     if (!check()) {       cout << -1;       return 0;     }     memset(d, 0, sizeof d);     int res = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         if ((in[i][j] ==  # ) && (d[i][j] == 0)) {           res++;           DFS(i, j);         }       }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e3 + 5;   const int mod = 998244353;   char s[maxn], t[maxn];   int m, n, dp[maxn][maxn];   int main() {     scanf( %s%s , s + 1, t + 1);     n = strlen(s + 1);     m = strlen(t + 1);     for (int i = 1; i <= n; i++) {       if (i > m || t[i] == s[1]) dp[i][i] = 2;     }     for (int len = 2; len <= n; len++) {       char ch = s[len];       for (int l = 1; l + len - 1 <= n; l++) {         int r = l + len - 1;         if (l > m || ch == t[l]) dp[l][r] = (dp[l][r] + dp[l + 1][r]) % mod;         if (r > m || ch == t[r]) dp[l][r] = (dp[l][r] + dp[l][r - 1]) % mod;       }     }     int ans = 0;     for (int i = m; i <= n; i++) ans = (ans + dp[1][i]) % mod;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 << std::endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   const int N = 1e5 + 5;   vector<pair<int, int> > aa, bb;   bool valid(pair<int, int> p1, pair<int, int> p2) {     if (p1.first == p2.first || p1.second == p2.second || p1.first == 0 ||         p1.second == 0 || p2.first == 0 || p2.second == 0)       return 0;     if (p1.first * p2.first + p1.second * p2.second != 0) return 0;     return 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     int a, b;     cin >> a >> b;     for (int i = 1; i <= a; i++) {       for (int j = 1; j <= a; j++) {         if (i * i + j * j == a * a) aa.push_back({i, j});       }     }     for (int i = 1; i <= b; i++) {       for (int j = 1; j <= b; j++) {         if (i * i + j * j == b * b) bb.push_back({-i, j});       }     }     for (auto p1 : aa) {       for (auto p2 : bb) {         if (valid(p1, p2)) {           cout <<  YES n ;           cout <<  0 0 n ;           cout << p1.first <<     << p1.second <<   n ;           cout << p2.first <<     << p2.second <<   n ;           return 0;         }       }     }     cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   long long a3, b3;   bool check1(long long a1, long long b1, long long a2, long long b2) {     if ((a2 * a3 + b2 * b3 + a1 * b3 - a3 * b1) % (a3 * a3 + b3 * b3))       return false;     if ((a2 * b3 - b2 * a3 - a1 * a3 - b1 * b3) % (a3 * a3 + b3 * b3))       return false;     return true;   }   bool check2(long long a1, long long b1, long long a2, long long b2) {     if ((a2 * a3 + b2 * b3 - a1 * a3 - b1 * b3) % (a3 * a3 + b3 * b3))       return false;     if ((a1 * b3 - b1 * a3 - a2 * b3 + b2 * a3) % (a3 * a3 + b3 * b3))       return false;     return true;   }   bool check(long long a1, long long b1, long long a2, long long b2) {     if (a1 == a2 && b1 == b2) return true;     if (b1 == a2 && -a1 == b2) return true;     if (-a1 == a2 && -b1 == b2) return true;     if (-b1 == a2 && a1 == b2) return true;     return false;   }   int main() {     long long a1, a2, b1, b2;     while (~scanf( %lld%lld%lld%lld%lld%lld , &a1, &b1, &a2, &b2, &a3, &b3)) {       if (a3 == b3 && a3 == 0) {         puts(check(a1, b1, a2, b2) ?  YES  :  NO );       } else {         int ok = 0;         ok += check(a1, b1, a2, b2);         ok += check1(a1, b1, a2, b2);         ok += check1(a1, b1, b2, -a2);         ok += check1(a1, b1, -a2, -b2);         ok += check1(a1, b1, -b2, a2);         ok += check2(a1, b1, a2, b2);         ok += check2(a1, b1, b2, -a2);         ok += check2(a1, b1, -a2, -b2);         ok += check2(a1, b1, -b2, a2);         puts(ok ?  YES  :  NO );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3fll;   template <bool directed>   struct Euler {     int N;     vector<vector<pair<int, int>>> adj;     vector<vector<pair<int, int>>::iterator> its;     vector<bool> used;     Euler(int N_) : N(N_), adj(N), its(N) {}     void ae(int a, int b) {       int M = used.size();       used.push_back(0);       adj[a].emplace_back(b, M);       if (!directed) adj[b].emplace_back(a, M);     }     vector<pair<int, int>> solve(int src = 0) {       while (!adj[src].size()) src++;       for (int i = 0; i < N; i++) its[i] = begin(adj[i]);       vector<pair<pair<int, int>, int>> ret, second = {{{src, -1}, -1}};       while (second.size()) {         int x = second.back().first.first;         auto &it = its[x], en = end(adj[x]);         while (it != en && used[it->second]) it++;         if (it == en) {           if (ret.size() && ret.back().first.second != x) return {};           ret.push_back(second.back()), second.pop_back();         } else {           second.push_back({{it->first, x}, it->second});           used[it->second] = 1;         }       }       if (ret.size() != used.size() + 1) return {};       vector<pair<int, int>> ans;       for (auto& t : ret) ans.push_back({t.first.first, t.second});       reverse(ans.begin(), ans.end());       return ans;     }   };   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int N;     cin >> N;     vector<pair<int, int>> v(N);     for (auto& i : v) cin >> i.first >> i.second;     for (int i = 20; i >= 0; i--) {       Euler<false> E(1 << i);       for (auto [l, r] : v) E.ae(l & ((1 << i) - 1), r & ((1 << i) - 1));       auto ans = E.solve();       if (!ans.size()) continue;       if (ans[0].first != ans.back().first) continue;       ans.erase(ans.begin());       cout << i <<   n ;       for (auto [j, id] : ans) {         int a = 2 * id + 1, b = 2 * id + 2;         if ((v[id].first & ((1 << i) - 1)) == j) swap(a, b);         cout << a <<     << b <<    ;       }       cout <<   n ;       break;     }     exit(0);   }


#include <bits/stdc++.h>   using namespace std;   const int mxN = 1e3 + 1;   int n, d[mxN], k;   vector<int> adj[mxN];   vector<string> a[mxN];   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> k;     for (int i = 0, c; i < n; i++) {       cin >> c;       string s1;       for (int j = 0; j < k; j++) {         cin >> s1;         a[c].push_back(s1);       }     }     vector<string> s;     for (int i = 0; i < n; i++)       for (int j = 0; j < k; j++) s.push_back(a[i][j]);     n *= k;     vector<bool> ok(26);     int al = 0;     for (string x : s)       for (char c : x) {         if (!ok[c -  a ]) ++al, ok[c -  a ] = 1;       }     for (int i = 1; i < n; i++) {       int n1 = s[i - 1].size(), n2 = s[i].size(), i1 = 0;       for (; i1 < n1 || i1 < n2; i1++) {         if (i1 >= n2 && i1 < n1) {           cout <<  IMPOSSIBLE ;           return 0;         } else if (i1 >= n1 || i1 >= n2)           break;         if (s[i - 1][i1] == s[i][i1]) continue;         adj[s[i - 1][i1] -  a ].push_back(s[i][i1] -  a );         d[s[i][i1] -  a ]++;         break;       }     }     queue<int> qu;     string ans;     for (int i = 0; i < 26; i++)       if (!d[i] && ok[i]) qu.push(i);     while (qu.size()) {       int u = qu.front();       qu.pop();       for (int x : adj[u]) {         --d[x];         if (!d[x]) qu.push(x);       }       ans += (u +  a );     }     cout << (ans.size() == al ? ans :  IMPOSSIBLE );   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T bigmod(T p, T e, T M) {     long long ret = 1;     for (; e > 0; e >>= 1) {       if (e & 1) ret = (ret * p) % M;       p = (p * p) % M;     }     return (T)ret;   }   template <class T>   inline T gcd(T a, T b) {     if (b == 0) return a;     return gcd(b, a % b);   }   template <class T>   inline T modinverse(T a, T M) {     return bigmod(a, M - 2, M);   }   long long bit[400001];   void add(int x, int v) {     for (int i = x; i <= 400000; i += i & -i) bit[i] += v;   }   long long query(int x) {     long long ret = 0;     for (int i = x; i > 0; i -= i & -i) ret += bit[i];     return ret;   }   long long ar[100001][4];   map<long long, long long> mx, my;   vector<pair<long long, long long> > horizontal[400001], vertical[400001];   vector<int> in[400001], out[400001];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     for (int i = (0); i < (n); ++i) {       for (int j = (0); j < (4); ++j) cin >> ar[i][j];       mx[ar[i][0]] = mx[ar[i][2]] = my[ar[i][1]] = my[ar[i][3]] = 1;       if (ar[i][0] > ar[i][2]) swap(ar[i][0], ar[i][2]);       if (ar[i][1] > ar[i][3]) swap(ar[i][1], ar[i][3]);     }     int c1 = 0, c2 = 0;     for (auto &v : mx) {       v.second = ++c1;     }     for (auto &v : my) {       v.second = ++c2;     }     for (int i = (0); i < (n); ++i) {       if (ar[i][0] == ar[i][2])         horizontal[mx[ar[i][0]]].push_back(make_pair(ar[i][1], ar[i][3]));       else         vertical[my[ar[i][1]]].push_back(make_pair(ar[i][0], ar[i][2]));     }     long long res = 0;     for (int i = 1; i <= c1; i++) {       sort(horizontal[i].begin(), horizontal[i].end());       if (horizontal[i].size() == 0) continue;       int last = 0;       for (int j = 1; j < horizontal[i].size(); j++) {         if (horizontal[i][j].first <= horizontal[i][last].second) {           horizontal[i][last].second =               max(horizontal[i][last].second, horizontal[i][j].second);         } else {           res += horizontal[i][last].second - horizontal[i][last].first + 1;           in[my[horizontal[i][last].first]].push_back(i);           out[my[horizontal[i][last].second]].push_back(i);           last = j;         }       }       res += horizontal[i][last].second - horizontal[i][last].first + 1;       in[my[horizontal[i][last].first]].push_back(i);       out[my[horizontal[i][last].second]].push_back(i);     }     for (int i = 1; i <= c2; i++) {       sort(vertical[i].begin(), vertical[i].end());       for (int j = 0; j < in[i].size(); j++) {         add(in[i][j], 1);       }       if (vertical[i].size() == 0) {         for (int j = 0; j < out[i].size(); j++) {           add(out[i][j], -1);         }         continue;       }       int last = 0;       for (int j = 1; j < vertical[i].size(); j++) {         if (vertical[i][j].first <= vertical[i][last].second) {           vertical[i][last].second =               max(vertical[i][last].second, vertical[i][j].second);         } else {           res += vertical[i][last].second - vertical[i][last].first + 1;           res -= query(mx[vertical[i][last].second]) -                  query(mx[vertical[i][last].first] - 1);           last = j;         }       }       res += vertical[i][last].second - vertical[i][last].first + 1;       res -= query(mx[vertical[i][last].second]) -              query(mx[vertical[i][last].first] - 1);       for (int j = 0; j < out[i].size(); j++) {         add(out[i][j], -1);       }     }     cout << res << endl;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   clock_t clk = clock();   long long int i, j, k;   void solve(void);   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     int t = 1;     while (t--) solve();     return 0;   }   void solve() {     long long int n, m;     cin >> n >> m;     bool rev = 0;     for (i = 1; i <= (n + 1) / 2; ++i) {       for (j = 1; j <= m; ++j)         if (!rev) {           if (n + 1 != 2 * i) {             cout << i <<     << j <<   n ;             cout << n + 1 - i <<     << m + 1 - j <<   n ;           } else if (j & 1)             cout << i <<     << (j - 1) / 2 + 1 <<   n ;           else             cout << i <<     << m - j / 2 + 1 <<   n ;         } else {           if (n + 1 != 2 * i) {             cout << i <<     << m + 1 - j <<   n ;             cout << n + 1 - i <<     << j <<   n ;           } else if (j & 1)             cout << i <<     << m - (j - 1) / 2 <<   n ;           else             cout << i <<     << j / 2 <<   n ;         }       rev ^= 1;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<set<string> > N, F;     vector<pair<string, string> > ans;     set<string> p;     for (int i = 0; i < 26; ++i) {       N.push_back(p);       F.push_back(p);     }     int n, k, c;     cin >> n;     c = (int) A ;     string s;     for (int i = 0; i < n; ++i) {       cin >> s;       k = (int)s[0];       N[k - c].insert(s);     }     for (int i = 0; i < n; ++i) {       cin >> s;       k = (int)s[0];       F[k - c].insert(s);     }     queue<string> qN, qF;     set<string>::iterator el, el1;     for (int i = 0; i < 26; ++i) {       if (N[i].size() <= F[i].size()) {         while (N[i].size() < F[i].size() && !qN.empty()) {           el = F[i].begin();           ans.push_back(make_pair(qN.front(), *el));           F[i].erase(el);           qN.pop();         }         while (!N[i].empty()) {           el = F[i].begin();           el1 = N[i].begin();           ans.push_back(make_pair(*el1, *el));           F[i].erase(el);           N[i].erase(el1);         }         while (!F[i].empty()) {           el = F[i].begin();           qF.push(*el);           F[i].erase(el);         }       } else {         while (F[i].size() < N[i].size() && !qF.empty()) {           el = N[i].begin();           ans.push_back(make_pair(*el, qF.front()));           N[i].erase(el);           qF.pop();         }         while (!F[i].empty()) {           el = F[i].begin();           el1 = N[i].begin();           ans.push_back(make_pair(*el1, *el));           F[i].erase(el);           N[i].erase(el1);         }         while (!N[i].empty()) {           el = N[i].begin();           qN.push(*el);           N[i].erase(el);         }       }     }     sort(ans.begin(), ans.end());     int i;     for (i = 0; i < ans.size() - 1; ++i)       cout << ans[i].first <<     << ans[i].second <<  ,  ;     cout << ans[i].first <<     << ans[i].second;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   const long double PI = 4 * atan((long double)1);   const long long INF = 1e18;   const long long NINF = -1e18;   long long get_hash(string s) {     long long N = 1000001;     long long base[N], A = 11, MD = 1110111110111;     base[0] = 1;     for (long long i = (1); i < (N); ++i) base[i] = (base[i - 1] * A) % MD;     long long hs = 0;     for (long long i = (0); i < (s.size()); ++i) {       hs += (s[i] * base[i]);       hs %= MD;     }     return hs;   }   long long power(long long a, long long n) {     long long res = 1;     while (n) {       if (n % 2) res *= a;       a *= a;       n /= 2;     }     return res;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n;     cin >> n;     long long k = 1, m = 2;     while (k != n + 1) {       long long p = (k + 1) * k;       long long u = k + 1;       long long v = (k + 1) * (k + 1) * k;       cout << v - (m) / k <<   n ;       m = p;       k++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 505;   char g[N][N];   int id[N][N];   int cnt = 1;   int dx[] = {0, 0, 1, -1};   int dy[] = {1, -1, 0, 0};   int amt[N * N];   int sz[N * N];   int sum;   int n, k;   void dfs(int i, int j, int idx) {     if (g[i][j] ==  X  or id[i][j]) return;     id[i][j] = idx;     sz[idx]++;     for (int k = 0; k < 4; k++) dfs(i + dx[k], j + dy[k], idx);   }   void add(int c, int l, int r, bool row, bool in, bool rem = false) {     for (int i = l; i <= r; i++) {       int idx;       if (row)         idx = id[c][i];       else         idx = id[i][c];       if (idx == 0) {         if (in) sum = sum + !rem - rem;       } else {         amt[idx] = amt[idx] + !rem - rem;         if (amt[idx] == 1 and !rem) sum += sz[idx];         if (amt[idx] == 0 and rem) sum -= sz[idx];       }     }   }   void addEnv(int i, int j, bool rem = false) {     add(i - 1, j, j + k - 1, true, false, rem);     add(i + k, j, j + k - 1, true, false, rem);     add(j - 1, i, i + k - 1, false, false, rem);     add(j + k, i, i + k - 1, false, false, rem);   }   int main() {     cin >> n >> k;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) cin >> g[i][j];     for (int i = 0; i <= n + 1; i++)       g[i][0] =  X , g[i][n + 1] =  X , g[0][i] =  X , g[n + 1][i] =  X ;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (!id[i][j] and g[i][j] !=  X ) dfs(i, j, cnt++);     add(0, 1, k, true, false, false);     add(k + 1, 1, k, true, false, false);     add(0, 1, k, false, false, false);     add(k + 1, 1, k, false, false, false);     for (int i = 1; i <= k; i++) add(i, 1, k, true, true, false);     int ans = sum;     int dir = 1;     for (int i = 1; i <= n - k + 1;) {       int lim = n - k + 1, bg = 1;       int j;       if (dir == -1) swap(lim, bg);       for (j = bg; j != lim;) {         addEnv(i, j, true);         if (dir == 1)           add(j, i, i + k - 1, false, true, true),               add(j + k, i, i + k - 1, false, true, false);         else           add(j + k - 1, i, i + k - 1, false, true, true),               add(j - 1, i, i + k - 1, false, true, false);         j += dir;         addEnv(i, j, false);         ans = max(ans, sum);       }       if (i == n - k + 1) break;       addEnv(i, j, true);       add(i, j, j + k - 1, true, true, true),           add(i + k, j, j + k - 1, true, true, false);       i++;       dir = -dir;       addEnv(i, j, false);       ans = max(ans, sum);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   struct pair_hash {     template <class T1, class T2>     size_t operator()(pair<T1, T2> const &pair) const {       size_t h1 = std::hash<T1>()(pair.first);       size_t h2 = std::hash<T2>()(pair.second);       return h1 ^ h2;     }   };   unordered_set<pair<int, int>, pair_hash> segmentos;   int main() {     int n, m;     scanf( %d %d , &n, &m);     vector<pair<int, int> > a(m);     for (int i = 0; i < m; i++) {       cin >> a[i].first >> a[i].second;       segmentos.insert({a[i].first, a[i].second});     }     for (int i = 1; i < n; i++) {       if (n % i) continue;       bool solved = true;       for (int j = 0; j < m && solved; j++) {         int optA = a[j].first;         int optB = a[j].second;         optA = (a[j].first + i > n ? 1 : 0) + (a[j].first + i) % (n + 1);         optB = (a[j].second + i > n ? 1 : 0) + (a[j].second + i) % (n + 1);         if (segmentos.find({optA, optB}) == segmentos.end() &&             segmentos.find({optB, optA}) == segmentos.end()) {           solved = false;         }       }       if (solved) {         printf( Yes );         exit(0);       }     }     printf( No );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int p, first, cnts, cntw, S, W;   int solve(long long picked) {     int cap1 = p, cap2 = first, cnt1 = cnts, cnt2 = cntw;     long long temp = picked;     cap1 -= (picked * S);     cnt1 -= picked;     if (cap1 >= W) {       temp += min(cap1 / W, cnt2);       cnt2 -= min(cap1 / W, cnt2);     }     if (S <= W) {       temp += min(cap2 / S, cnt1);       cap2 -= (min(cap2 / S, cnt1) * S);       temp += min(cap2 / W, cnt2);     } else {       temp += min(cap2 / W, cnt2);       cap2 -= (min(cap2 / W, cnt2) * W);       temp += min(cap2 / S, cnt1);     }     return temp;   }   int main() {     int t;     scanf( %d , &t);     while (t--) {       scanf( %d %d , &p, &first);       scanf( %d %d , &cnts, &cntw);       scanf( %d %d , &S, &W);       long long ans = 0, temp;       for (int pickedS = 0; pickedS <= min(cnts, p / S); pickedS++) {         temp = solve(pickedS);         ans = max(ans, temp);       }       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long a, b, x, y, i, k, j, t, n, s;     cin >> t;     while (t--) {       cin >> n >> k;       if (n % 2 == 0) {         s = n + (k * 2);         cout << s << endl;       } else {         s = (k - 1) * 2;         a = 0;         for (i = 3; i * i <= n; i += 2) {           if (n % i == 0) {             a = i;             break;           }         }         if (a == 0) a = n;         s = a + n + s;         cout << s << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long p, k;     cin >> p >> k;     long long d = (k - p) / k;     if ((((k - p) % k) + k) % k == 0 || (d <= 0 && p > k)) d--;     vector<long long> res;     res.push_back(d);     if (k > p) return cout <<  1 n  << p, 0;     while (1) {       long long r = res.back();       d = (k - r) / k;       if ((((k - r) % k) + k) % k == 0 || d <= 0) d--;       res.push_back(d);       if (d < k && d >= 0) break;     }     cout << res.size() + 1 << endl;     long long last = p;     for (int i = 0; i < res.size(); i++) {       cout << k * res[i] + last <<    ;       last = res[i];     }     cout << d;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1);   const double eps = 1e-9;   const int inf = 2000000000;   const long long infLL = 9000000000000000000;   inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }   inline bool checkBit(long long n, int i) { return n & (1LL << i); }   inline long long setBit(long long n, int i) { return n | (1LL << i); }   inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); }   int dx[] = {0, 0, +1, -1, +1, +1, -1, -1};   int dy[] = {+1, -1, 0, 0, +1, -1, +1, -1};   template <typename first, typename second>   ostream &operator<<(ostream &os, const pair<first, second> &p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename T>   ostream &operator<<(ostream &os, const vector<T> &v) {     os <<  { ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  } ;   }   template <typename T>   ostream &operator<<(ostream &os, const set<T> &v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  ] ;   }   template <typename T>   ostream &operator<<(ostream &os, const multiset<T> &v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << *it;     }     return os <<  ] ;   }   template <typename first, typename second>   ostream &operator<<(ostream &os, const map<first, second> &v) {     os <<  [ ;     for (auto it = v.begin(); it != v.end(); ++it) {       if (it != v.begin()) os <<  ,  ;       os << it->first <<   =   << it->second;     }     return os <<  ] ;   }   void faltu() { cerr <<   n ; }   template <typename T>   void faltu(T a[], int n) {     for (int i = 0; i < n; ++i) cerr << a[i] <<    ;     cerr <<   n ;   }   template <typename T, typename... hello>   void faltu(T arg, const hello &...rest) {     cerr << arg <<    ;     faltu(rest...);   }   inline int max(int a, int b) { return (a > b) ? a : b; }   struct xorSpaces {     int Rank, dim;     array<int, 20> basis;     xorSpaces() {       Rank = 0, dim = 20;       fill((basis).begin(), (basis).end(), 0);     }     void addBasis(int n) {       for (int i = dim - 1; i >= 0; i--) {         if (n & (1 << i)) {           if (basis[i] == 0) {             basis[i] = n;             Rank++;             break;           } else {             n ^= basis[i];           }         }       }     }     xorSpaces operator+=(xorSpaces other) {       if (other.Rank == 0) return other;       if (Rank == 0) {         return *this = other;       }       for (int i = dim - 1; i >= 0; i--) {         if (other.basis[i]) addBasis(other.basis[i]);       }       return *this;     }     int getMax() {       int ans = 0;       for (int i = dim - 1; i >= 0; i--) {         if (basis[i] and (ans & (1 << i)) == 0) ans ^= basis[i];       }       return ans;     }   };   const int mx = 5e5 + 123;   xorSpaces table[(mx / 20) + 123][23], s[mx], p[mx];   int a[mx];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int n, l, r, q;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     int d = (n) / 20;     for (int i = 0; i < n; i++) {       if (i % 20) p[i] = p[i - 1];       p[i].addBasis(a[i]);     }     for (int i = n - 1; i >= 0; i--) {       if (i % 20 != 19) s[i] = s[i + 1];       s[i].addBasis(a[i]);     }     for (int i = 0; i <= d; i++) {       table[i][0] = s[i * 20];     }     for (int i = 1; i <= 15; i++) {       for (int j = 0; j + (1 << (i - 1)) <= d; j++) {         table[j][i] = table[j][i - 1];         table[j][i] += (table[j + (1 << (i - 1))][i - 1]);       }     }     cin >> q;     while (q--) {       cin >> l >> r;       l--, r--;       int block_l = l / 20, block_r = r / 20;       xorSpaces ans;       if (block_l == block_r) {         for (int i = l; i <= r; i++) {           ans.addBasis(a[i]);         }       } else {         ans += (s[l]);         ans += (p[r]);         block_l++, block_r--;         if (block_l <= block_r) {           r = block_r, l = block_l;           int lg = log2(r - l + 1);           ans += (table[l][lg]);           ans += (table[r - (1 << lg) + 1][lg]);         }       }       cout << ans.getMax() <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 123456789;   template <class T>   void scanint(T& x) {     register int c = getchar();     x = 0;     int m = 0;     if (c ==  - ) {       m = 1;     }     for (; (c < 48 || c > 57); c = getchar())       ;     for (; c > 47 && c < 58; c = getchar()) {       x = (x << 1) + (x << 3) + c - 48;     }     if (m == 1) {       x *= -1;     }   }   template <typename T>   void cinall(T& t) {     cin >> t;   }   template <typename T, typename... Args>   void cinall(T& t, Args&... args) {     cin >> t;     cinall(args...);   }   int main() {     ios_base::sync_with_stdio(false);     int n;     cinall(n);     ;     vector<int> v(n);     vector<int> xv(n + 1);     long long sol = 0;     for (int i = 0; i < n; ++i) {       cin >> v[i];       sol ^= v[i];       xv[i + 1] = xv[i] ^ i;     }     for (int i = 2; i <= n; ++i) {       sol ^= xv[((n / i) % 2) * i] ^ xv[(n % i) + 1];     }     cout << sol;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, q, a[300005], ans[300005];   vector<pair<int, int> > queries[300005];   int tree[1200006];   void upd(int l, int r, int pos, int x) {     if (l == r) {       tree[pos]++;       return;     }     int mid = l + r >> 1;     if (x <= mid)       upd(l, mid, pos << 1, x);     else       upd(mid + 1, r, pos << 1 | 1, x);     tree[pos]++;   }   int query(int l, int r, int pos, int x) {     if (l == r) return r;     int mid = l + r >> 1;     if (tree[pos << 1] >= x)       return query(l, mid, pos << 1, x);     else       return query(mid + 1, r, pos << 1 | 1, x - tree[pos << 1]);   }   int query1(int l, int r, int pos, int ql, int qr) {     if (l >= ql && r <= qr) return tree[pos];     if (l > qr || r < ql) return 0;     int mid = l + r >> 1;     return query1(l, mid, pos << 1, ql, qr) +            query1(mid + 1, r, pos << 1 | 1, ql, qr);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> q;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 0; i < q; i++) {       int x, y;       cin >> x >> y;       queries[n - y].push_back({x, i});     }     for (int i = 1; i <= n; i++) {       if (a[i] > i)         upd(0, n, 1, 0);       else if (a[i] == i)         upd(0, n, 1, i);       else {         int x = query(0, n, 1, a[i]);         upd(0, n, 1, x);       }       for (auto p : queries[i]) {         int x = query1(0, n, 1, 0, p.first);         ans[p.second] = i - x;       }     }     for (int i = 0; i < q; i++) cout << ans[i] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int tests;     cin >> tests;     for (int test = 0; test < (int)(tests); test++) {       int n;       cin >> n;       map<int, int> m;       for (int i = 0; i < (int)(n); i++) {         int x;         cin >> x;         m[-x]++;       }       vector<int> v;       for (auto i : m) {         v.push_back(i.second);       }       int g, s, b;       int j = 0;       int k = 0;       g = s = b = 0;       vector<int> ans(3);       g += v[0];       s -= v[0];       for (; j < n; j++) {         s += v[j];         b -= v[j];         if (s > g) {           break;         }       }       for (; k < n; k++) {         b += v[k];         if (b > g) {           break;         }       }       k++;       while (g + s + b <= n / 2 && k < v.size()) {         ans[0] = g;         ans[1] = s;         ans[2] = b;         b += v[k];         k++;       }       cout << ans[0] <<     << ans[1] <<     << ans[2] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9;   const long long inf64 = 1e18;   const int N = 1e6 + 5;   int n, m;   vector<int> g[N], gr[N];   int ptr = 1;   int del[N];   int mark[N];   void solve() {     while (ptr <= n && del[ptr]) ptr++;     if (ptr > n) return;     int v = ptr++;     for (int to : g[v]) del[to] = 1;     solve();     int any = 0;     for (int to : gr[v]) {       if (mark[to]) {         any = 1;         break;       }     }     if (!any) {       mark[v] = 1;     }   }   int main() {     scanf( %d %d , &n, &m);     for (int u, v, i = 0; i < m; i++) {       scanf( %d %d , &u, &v);       g[u].push_back(v);       gr[v].push_back(u);     }     solve();     printf( %d n , count(mark + 1, mark + n + 1, 1));     for (int i = 1; i <= n; i++) {       if (mark[i]) {         printf( %d  , i);       }     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int n, m, k, dp[2020][2020];   long long int getDp(long long int x, long long int y) {     if (x == n) return y == k;     if (~dp[x][y]) return dp[x][y];     long long int &ret = dp[x][y] = 0;     ret = getDp(x + 1, y);     ret = (ret + (getDp(x + 1, y + 1) * (m - 1)) % 998244353);     ret %= 998244353;     return ret;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m >> k;     memset(dp, -1, sizeof(dp));     cout << ((getDp(1, 0) * m) % 998244353) << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MX = 1 << 19;   struct node {     long long left, right, cnt, tot, length;   };   struct segtree {     node t[MX * 2];     node merge(node& l, node& r) {       node ans;       ans.left = l.left + r.left + l.length * r.cnt;       ans.right = l.right + r.right + r.length * l.cnt;       ans.cnt = l.cnt + r.cnt;       ans.length = l.length + r.length;       ans.tot = l.tot + r.tot + l.right * r.cnt + r.left * l.cnt;       return ans;     }     void update(int wi, int su) {       wi += MX;       t[wi].cnt = su;       while (wi > 1) {         wi /= 2;         t[wi] = merge(t[wi * 2], t[wi * 2 + 1]);       }     }     long long read(int s, int e) {       s += MX, e += MX;       node l = {0}, r = {0};       while (s <= e) {         if (s & 1)           l = merge(l, t[s]), s = s / 2 + 1;         else           s /= 2;         if (e & 1)           e /= 2;         else           r = merge(t[e], r), e = e / 2 - 1;       }       return merge(l, r).tot;     }   } t;   struct query {     long long o, s, e;   } Q[MX];   map<long long, long long> point;   long long D[MX], tmp[MX];   int main() {     int N, T;     scanf( %d , &N);     for (int i = 1; i <= N; i++)       scanf( %lld , D + i), tmp[i] = D[i], point[D[i]] = 0;     scanf( %d , &T);     for (int i = 0; i < T; i++) scanf( %lld%lld%lld , &Q[i].o, &Q[i].s, &Q[i].e);     for (int i = 0; i < T; i++) {       if (Q[i].o == 2) continue;       tmp[Q[i].s] += Q[i].e;       point[tmp[Q[i].s]] = 0;     }     int wi = 0;     for (auto it = point.begin(), it2 = point.begin(); it != point.end();          ++it, wi++) {       it->second = wi * 2;       ++it2;       if (it2 != point.end())         t.t[MX + wi * 2 + 1].length = it2->first - it->first;     }     for (int i = MX - 1; i >= 1; i--) {       t.t[i].length = t.t[i * 2].length + t.t[i * 2 + 1].length;     }     for (int i = 1; i <= N; i++) {       t.update(point[D[i]], 1);     }     for (int i = 0; i < T; i++) {       if (Q[i].o == 1) {         t.update(point[D[Q[i].s]], 0);         D[Q[i].s] += Q[i].e;         t.update(point[D[Q[i].s]], 1);       } else {         auto l = point.lower_bound(Q[i].s), r = point.upper_bound(Q[i].e);         if (r == l)           printf( 0  n );         else           --r, printf( %lld n , t.read(l->second, r->second));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10, mod = 998244353;   int n, p[N], c[N];   void add(int& x, int y) {     x += y;     if (x >= mod) {       x -= mod;     }   }   int mul(int x, int y) { return (long long)x * y % mod; }   int qpow(int v, int p) {     int result = 1;     for (; p; p >>= 1, v = mul(v, v)) {       if (p & 1) {         result = mul(result, v);       }     }     return result;   }   void modify(int p) {     for (; p <= n; p += p & -p) {       ++c[p];     }   }   int query(int p) {     int result = 0;     for (; p; p -= p & -p) {       result += c[p];     }     return result;   }   int query(int l, int r) { return query(r) - query(l - 1); }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d , &p[i]);     }     int answer = 0, t = 0, emp = 0;     for (int i = 1; i <= n; ++i) {       if (!~p[i]) {         ++t;       }     }     emp = t;     add(answer, mul(mul(t, t - 1), qpow(4, mod - 2)));     for (int i = 1; i <= n; ++i) {       if (~p[i]) {         add(answer, query(p[i], n));         modify(p[i]);       }     }     t = 0;     for (int i = 1; i <= n; ++i) {       if (!~p[i]) {         ++t;       } else {         int pos = mul(n - p[i] - query(p[i] + 1, n), qpow(emp, mod - 2));         add(answer, mul(pos, t));       }     }     t = 0;     for (int i = n; i; --i) {       if (!~p[i]) {         ++t;       } else {         int pos = mul(p[i] - 1 - query(1, p[i] - 1), qpow(emp, mod - 2));         add(answer, mul(pos, t));       }     }     printf( %d n , answer);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100100;   int n, q;   long long int m;   vector<long long int> mods;   long long int w[N];   long long int phi(long long int x) {     long long int ans = x;     for (long long int i = 2; i * i <= x; i++) {       if (x % i == 0) {         ans -= ans / i;         while (x % i == 0) {           x /= i;         }       }     }     if (x > 1) {       ans -= ans / x;     }     return ans;   }   long long int magic(long long int x, long long int mod) {     if (x < 50) {       return x;     }     return 32 + (x - 32) % mod;   }   long long int fexp(long long int base, long long int e, long long int mod) {     long long int ans = 1;     while (e > 0) {       if (e & 1) {         ans = magic(ans * base, mod);       }       base = magic(base * base, mod);       e >>= 1;     }     return ans;   }   long long int go(int l, int r, int on) {     if (r == l) {       return magic(w[l], mods[on]);     }     if (mods[on] == 1) {       return magic(w[l], mods[on]);     }     long long int e = go(l + 1, r, on + 1);     return fexp(w[l], e, mods[on]);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> m;     mods.push_back(m);     while (mods.back() != 1) {       mods.push_back(phi(mods.back()));     }     for (int i = 0; i < n; i++) {       cin >> w[i];     }     cin >> q;     while (q--) {       int l, r;       cin >> l >> r;       l--;       r--;       cout << go(l, r, 0) % mods[0] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s[1005];   long long int M[1005][1005], T[1005][1005];   queue<pair<long long int, long long int> > q;   long long int xr[] = {0, 0, -1, 1};   long long int xc[] = {-1, 1, 0, 0};   int main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);     long long int p, t, n, m, i, j, k, x, y, z, a, b, c;     cin >> n >> m >> t;     for (i = 0; i < n; ++i) cin >> s[i];     for (i = 0; i < n; ++i) {       for (j = 0; j < m; ++j) {         a = 0;         for (k = 0; k < 4; ++k) {           x = i + xr[k];           y = j + xc[k];           if (x < 0 || x >= n || y < 0 || y >= m) continue;           a += (s[x][y] == s[i][j]);         }         if (a) {           q.push(make_pair(i, j));           M[i][j] = 1;         } else           T[i][j] = LLONG_MAX;       }     }     while (!q.empty()) {       auto it = q.front();       q.pop();       i = it.first;       j = it.second;       a = b = c = 0;       for (k = 0; k < 4; ++k) {         x = i + xr[k];         y = j + xc[k];         if (x < 0 || x >= n || y < 0 || y >= m || M[x][y]) continue;         if (s[x][y] != s[i][j]) {           q.push(make_pair(x, y));           M[x][y] = 1;           T[x][y] = T[i][j] + 1;         }       }     }     while (t--) {       cin >> i >> j >> p;       i -= 1;       j -= 1;       if (T[i][j] > p)         cout << s[i][j] << endl;       else         cout << (((p - T[i][j]) & 1) ^ (s[i][j] -  0 )) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k, x, y, a, b, d[2010][2010], v[2010][2010];   bool solve(int x, int y, int k) {     int l = max(1, x - k), r = min(x + k, n), t;     for (int i = l; i < r + 1; i++) {       t = k - abs(i - x);       if (y - t > 0 && !v[i][y - t]) return a = i, b = y - t, 1;       if (y + t <= m && !v[i][y + t]) return a = i, b = y + t, 1;     }     return 0;   }   int main() {     cin >> n >> m >> k;     for (int t = 0; t < k; t++) {       cin >> x >> y;       for (int i = -2; i < 3; i++)         for (int j = -2; j < 3; j++) {           if (x + i < 1 || x + i > n || y + j < 1 || y + j > m) continue;           d[x][y] = max(d[x][y], d[x + i][y + j] - abs(i) - abs(j));         }       while (!solve(x, y, d[x][y])) d[x][y]++;       cout << a <<     << b << endl;       v[a][b] = 1;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<int> g[100100];   int a[100100], b[100100], s[100100];   int n, sa, sb;   double ans;   void dfs(int v, int p) {     s[v] = 1;     for (int i = 0; i < g[v].size(); i++) {       int u = g[v][i];       if (u == p) continue;       dfs(u, v);       s[v] += s[u];       a[v] += a[u];       ans += 1.0 * b[v] * s[u] * a[u];     }     ans += 1.0 * b[v] * (n - s[v]) * (sa - a[v]);   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int u, v;       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     for (int i = 1; i <= n; i++) scanf( %d%d , a + i, b + i);     for (int i = 1; i <= n; i++) {       sa += a[i];       sb += b[i];     }     dfs(1, 0);     printf( %.12lf n , ans / sa / sb);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e9 + 7;   const int P = 1e6 + 9;   long long n, m;   long long bin_pow(int a, int x) {     if (x == 1) return a % N;     if (x % 2 == 0) return (bin_pow(a, x / 2) * bin_pow(a, x / 2)) % N;     if (x % 2 != 0) return (bin_pow(a, x - 1) * (a % N)) % N;   }   int main() {     cin >> n >> m;     cout << bin_pow(bin_pow(2, m) - 1, n);   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3fll;   const int MAX = 1e3 + 10;   vector<int> g[MAX];   int qt[MAX];   int dfs(int i, int p = -1) {     int ret = 1;     for (int j : g[i])       if (j != p) ret += dfs(j, i);     return qt[i] = ret;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n, x;       cin >> n >> x;       x--;       for (int i = 0; i < n; i++) g[i].clear();       int deg = 0;       for (int i = 0; i < n - 1; i++) {         int a, b;         cin >> a >> b;         a--, b--;         if (a == x or b == x) deg++;         g[a].push_back(b);         g[b].push_back(a);       }       if (deg <= 1) {         cout <<  Ayush  <<   n ;         continue;       }       dfs(x);       vector<int> v;       for (int i : g[x]) v.push_back(qt[i]);       int par = 0;       for (int i : v) par += (i % 2 == 0);       if (par % 2 != deg % 2)         cout <<  Ayush  <<   n ;       else         cout <<  Ashish  <<   n ;     }     exit(0);   }


#include <bits/stdc++.h>   using namespace std;   int a[100000];   int m[100001];   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     m[n] = 1e9 + 1;     for (int i = n - 1; i >= 0; i--) {       m[i] = min(m[i + 1], a[i]);     }     for (int i = 0; i < n; i++) {       cout << (lower_bound(m + i + 1, m + n, a[i]) - m - i - 2) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int x, s = 0, t = 0, m, ans = 0, hp = 0;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     if (a[0] == 1)       t++;     else       s++;     for (int i = 1; i < n; i++) {       if (a[i] != a[i - 1] && hp == 0) {         hp = 1;       }       if (a[i] != a[i - 1] && hp == 1) {         m = min(s, t);         if (ans < m) ans = m;         if (a[i] == 1)           t = 0;         else           s = 0;       }       if (a[i] == 1) {         t++;       } else if (a[i] == 2) {         s++;       }     }     m = min(s, t);     if (ans < m) ans = m;     cout << ans * 2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void boost() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   const long long maxn = 2e6 + 123;   const long long inf = 1e9 + 123;   const long long linf = 1e18 + 123;   const long long mod = 1e9 + 7;   const double eps = 1e-9;   const double pi = acos(-1);   int dx[8] = {0, 1, -1, 0, 1, -1, 1, -1};   int dy[8] = {1, 0, 0, -1, 1, 1, -1, -1};   long long a[maxn], b[maxn];   vector<pair<long long, long long> > v[maxn];   int main() {     boost();     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       a[i] += a[i - 1];     }     for (int i = 1; i <= n; i++) {       cin >> b[i];       b[i] += b[i - 1];     }     bool WAS = 0;     if (a[n] > b[n]) {       swap(a, b);       WAS = 1;     }     for (int i = 1; i <= n; i++) {       int j = lower_bound(b + 1, b + 1 + n, a[i]) - b;       if (a[i] != b[j]) {         j--;       }       v[a[i] - b[j]].push_back({i, j});     }     if (v[0].size() >= 1) {       if (WAS == 0) {         cout << v[0][0].first <<   n ;         for (int i = 1; i <= v[0][0].first; i++) {           cout << i <<    ;         }         cout <<   n ;         cout << v[0][0].second <<   n ;         for (int i = 1; i <= v[0][0].second; i++) {           cout << i <<    ;         }         cout <<   n ;       } else {         cout << v[0][0].second <<   n ;         for (int i = 1; i <= v[0][0].second; i++) {           cout << i <<    ;         }         cout <<   n  << v[0][0].first <<   n ;         for (int i = 1; i <= v[0][0].first; i++) {           cout << i <<    ;         }         cout <<   n ;       }       return 0;     }     for (int i = 1; i < n; i++) {       if (v[i].size() >= 2) {         if (WAS == 0) {           cout << v[i][1].first - v[i][0].first <<   n ;           for (int j = v[i][0].first + 1; j <= v[i][1].first; j++) {             cout << j <<    ;           }           cout <<   n  << v[i][1].second - v[i][0].second <<   n ;           for (int j = v[i][0].second + 1; j <= v[i][1].second; j++) {             cout << j <<    ;           }           return 0;         } else {           cout << v[i][1].second - v[i][0].second <<   n ;           for (int j = v[i][0].second + 1; j <= v[i][1].second; j++) {             cout << j <<    ;           }           cout <<   n  << v[i][1].first - v[i][0].first <<   n ;           for (int j = v[i][0].first + 1; j <= v[i][1].first; j++) {             cout << j <<    ;           }           return 0;         }       }     }     exit(0);   }


#include <bits/stdc++.h>   using namespace std;   void mejor(int player, long long int k, int *arr, int n) {     if (player == 0) {       if (player + k >= n - 1)         cout << n << endl;       else {         for (int i = 0; i < k; i++) {           if (arr[player] < arr[player + 1 + i]) {             mejor(player + 1 + i, k, arr, n);             return;           }         }         cout << arr[player] << endl;       }     } else {       if (player + k > n - 1)         cout << n << endl;       else {         for (int i = 0; i < k - 1; i++) {           if (arr[player] < arr[player + 1 + i]) {             mejor(player + 1 + i, k, arr, n);             return;           }         }         cout << arr[player] << endl;       }     }     return;   }   int main() {     int n;     long long int k;     cin >> n >> k;     int arr[n];     for (int i = 0; i < n; i++) cin >> arr[i];     if (k >= n - 1)       cout << n << endl;     else       mejor(0, k, arr, n);     return 0;   }


#include <bits/stdc++.h>   int main() {     int len, i, l = 0, r = 0, u = 0, d = 0, dif1, dif2;     scanf( %d , &len);     char str[101];     scanf(  %s , str);     for (i = 0; i < len; i++) {       if (str[i] ==  L )         l++;       else if (str[i] ==  R )         r++;       else if (str[i] ==  U )         u++;       else if (str[i] ==  D )         d++;     }     dif1 = l - r;     if (dif1 < 0) dif1 = -dif1;     dif2 = u - d;     if (dif2 < 0) dif2 = -dif2;     printf( %d n , (len - (dif1 + dif2)));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3f;   const int MOD = (int)1e9 + 7;   const int N = (int)1e6 + 7;   const int M = (int)-1;   int a[N], cnt[100];   int main() {     int T;     cin >> T;     while (T--) {       long long n;       int m;       cin >> n >> m;       for (int i = (0); i < (m); ++i) cin >> a[i];       long long sum = 0;       for (int i = (0); i < (m); ++i) sum += a[i];       if (sum < n) {         cout << -1 << endl;         continue;       }       sort(a, a + m);       memset(cnt, 0, sizeof(cnt));       for (int i = 0, j = 0; i < m; i++) {         while (a[i] > (1ll << j)) j++;         cnt[j]++;       }       long long res = 0;       int ans = 0;       for (int i = (0); i < (63); ++i) {         if (n >> i & 1) {           if (!cnt[i] && res < (1ll << i)) {             int j = i + 1;             cnt[i] = 2;             while (!cnt[j]) {               cnt[j]++;               j++;               ans++;             }             cnt[j]--;             ans++;           }           if (cnt[i])             cnt[i]--;           else             res -= 1ll << i;         }         res += (1ll << i) * cnt[i];       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   const int mod = 1000000007;   const int seed = 131;   char s[100005], t[1000005];   int hshv[1000005];   int powm(int a, int b) {     int ret = 1;     while (b) {       if (b & 1) ret = 1LL * ret * a % mod;       a = 1LL * a * a % mod;       b >>= 1;     }     return ret;   }   void gethash(char* s) {     int len = strlen(s), now;     now = 0;     for (int i = 0; i < len; i++) {       now = (1LL * now * seed + s[i]) % mod;       hshv[i] = now;     }   }   int get(int s, int t) {     int a, b;     if (s == 0)       a = 0;     else       a = 1LL * hshv[s - 1] * powm(seed, t - s + 1) % mod;     b = hshv[t];     return (b - a + mod) % mod;   }   int main() {     int cnt0, cnt1, lens, lent, len0, len1, hsh0, hsh1, p, ans;     bool f;     scanf( %s , s);     scanf( %s , t);     lens = strlen(s);     lent = strlen(t);     if (s[0] ==  1 )       for (int i = 0; i < lens; i++) s[i] = ((s[i] -  0 ) ^ 1) +  0 ;     cnt0 = 0;     cnt1 = 0;     for (int i = 0; i < lens; i++)       if (s[i] ==  0 )         cnt0++;       else         cnt1++;     ans = 0;     gethash(t);     for (int i = 1; cnt0 * i < lent; i++) {       if ((lent - cnt0 * i) % cnt1 != 0) continue;       len0 = i;       len1 = (lent - cnt0 * len0) / cnt1;       hsh0 = -1;       hsh1 = -1;       p = 0;       f = true;       for (int j = 0; j < lens; j++) {         if (s[j] ==  0 ) {           if (hsh0 == -1)             hsh0 = get(p, p + len0 - 1);           else if (get(p, p + len0 - 1) != hsh0) {             f = false;             break;           }           p += len0;         } else {           if (hsh1 == -1)             hsh1 = get(p, p + len1 - 1);           else if (get(p, p + len1 - 1) != hsh1) {             f = false;             break;           }           p += len1;         }       }       if (f && hsh0 != hsh1) ans++;     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   template <typename T>   void ckmn(T& a, T b) {     a = min(a, b);   }   template <typename T>   void ckmx(T& a, T b) {     a = max(a, b);   }   void rd(int& x) { scanf( %i , &x); }   void rd(long long& x) { scanf( %lld , &x); }   void rd(char* x) { scanf( %s , x); }   void rd(double& x) { scanf( %lf , &x); }   void rd(string& x) { scanf( %s , &x); }   template <typename T1, typename T2>   void rd(pair<T1, T2>& x) {     rd(x.first);     rd(x.second);   }   template <typename T>   void rd(vector<T>& x) {     for (T& i : x) rd(i);   }   template <typename T, typename... A>   void rd(T& x, A&... args) {     rd(x);     rd(args...);   }   template <typename T>   void rd() {     T x;     rd(x);     return x;   }   int ri() {     int x;     rd(x);     return x;   }   template <typename T>   vector<T> rv(int n) {     vector<T> x(n);     rd(x);     return x;   }   template <typename T>   void ra(T a[], int n, int st = 1) {     for (int i = 0; i < n; ++i) rd(a[st + i]);   }   template <typename T1, typename T2>   void ra(T1 a[], T2 b[], int n, int st = 1) {     for (int i = 0; i < n; ++i) rd(a[st + i]), rd(b[st + i]);   }   template <typename T1, typename T2, typename T3>   void ra(T1 a[], T2 b[], T3 c[], int n, int st = 1) {     for (int i = 0; i < n; ++i) rd(a[st + i]), rd(b[st + i]), rd(c[st + i]);   }   void re(vector<int> E[], int m, bool dir = 0) {     for (int i = 0, u, v; i < m; ++i) {       rd(u, v);       E[u].push_back(v);       if (!dir) E[v].push_back(u);     }   }   template <typename T>   void re(vector<pair<int, T>> E[], int m, bool dir = 0) {     for (int i = 0, u, v; i < m; ++i) {       T w;       rd(u, v, w);       E[u].push_back({v, w});       if (!dir) E[v].push_back({u, w});     }   }   int main() {     for (int t = ri(); t--;) {       int n = ri();       vector<int> a(n + 1, 0);       vector<vector<int>> ids(n + 1, vector<int>());       for (int i = 1; i <= n; i++) {         rd(a[i]);         ids[a[i]].push_back(i);       }       vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));       vector<vector<bool>> was(n + 1, vector<bool>(n + 1, false));       function<int(int, int)> Solve = [&](int l, int r) {         if (l > r) return 0;         if (was[l][r]) return dp[l][r];         was[l][r] = 1;         if (l == r) return dp[l][r] = 1;         dp[l][r] = Solve(l + 1, r) + 1;         for (int i : ids[a[l]])           if (i > l && i <= r) {             ckmn(dp[l][r], Solve(l + 1, i - 1) + Solve(i, r));           }         return dp[l][r];       };       printf( %i n , Solve(1, n) - 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, q;   int p[100001];   int sz[100001];   int bc[100001];   int top[100001];   int newi[100001];   int sr[100001];   vector<int> c[100001];   pair<int, int> zero = {0, -1e9};   pair<int, int> f(pair<int, int> x, pair<int, int> y) {     return {x.first + y.first, max(y.second, x.second + y.first)};   }   pair<int, int> info[262144];   bool tag[262144];   int lazy[262144];   void put(int id, int l, int r, int v) {     tag[id] = true;     lazy[id] = v;     info[id] = {v * (r - l + 1), max(v * (r - l + 1), v)};   }   void push(int id, int l, int r) {     if (!tag[id]) return;     int mid = (l + r) / 2;     put(id * 2, l, mid, lazy[id]);     put(id * 2 + 1, mid + 1, r, lazy[id]);     tag[id] = false;   }   void upd1(int id, int l, int r, int ql, int qr, int v) {     if (ql > r || qr < l) return;     if (ql <= l && r <= qr) {       put(id, l, r, v);       return;     }     push(id, l, r);     int mid = (l + r) / 2;     upd1(id * 2, l, mid, ql, qr, v);     upd1(id * 2 + 1, mid + 1, r, ql, qr, v);     info[id] = f(info[id * 2], info[id * 2 + 1]);   }   void upd2(int id, int l, int r, int pos, int v) {     if (l == r) {       info[id] = {info[id].first + v, info[id].second + v};       return;     }     push(id, l, r);     int mid = (l + r) / 2;     if (pos <= mid)       upd2(id * 2, l, mid, pos, v);     else       upd2(id * 2 + 1, mid + 1, r, pos, v);     info[id] = f(info[id * 2], info[id * 2 + 1]);   }   pair<int, int> qry(int id, int l, int r, int ql, int qr) {     if (ql > r || qr < l) return zero;     if (ql <= l && r <= qr) return info[id];     push(id, l, r);     int mid = (l + r) / 2;     return f(qry(id * 2, l, mid, ql, qr), qry(id * 2 + 1, mid + 1, r, ql, qr));   }   int g(int id) {     pair<int, int> res = zero;     while (id != 0) {       res = f(qry(1, 1, n, newi[top[id]], newi[id]), res);       id = p[top[id]];     }     return res.second;   }   void dfs(int id) {     sz[id] = 1;     for (auto cur : c[id]) {       dfs(cur);       sz[id] += sz[cur];       if (sz[bc[id]] < sz[cur]) bc[id] = cur;     }   }   int ptr = 0;   void dfs2(int id) {     newi[id] = ++ptr;     sr[id] = ptr;     if (c[id].empty()) return;     top[bc[id]] = top[id];     dfs2(bc[id]);     sr[id] = sr[bc[id]];     for (auto cur : c[id]) {       if (cur != bc[id]) {         top[cur] = cur;         dfs2(cur);         sr[id] = sr[cur];       }     }   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> q;     for (int i = 2; i <= n; i++) {       cin >> p[i];       c[p[i]].push_back(i);     }     dfs(1);     top[1] = 1;     dfs2(1);     upd1(1, 1, n, 1, n, -1);     for (int i = 1; i <= q; i++) {       int t, u;       cin >> t >> u;       if (t == 1)         upd2(1, 1, n, newi[u], 1);       else if (t == 2) {         upd1(1, 1, n, newi[u], sr[u], -1);         int num = g(u);         if (num >= 0) upd2(1, 1, n, newi[u], -num - 1);       } else {         int num = g(u);         if (num >= 0)           cout <<  black n ;         else           cout <<  white n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int ch[300010][26];   int ans[300010], maxdep = 0;   int N;   int merge(vector<int> now) {     int res = now.size() - 1;     vector<int> next;     for (int i = 0; i < 26; i++) {       next.clear();       for (int j = 0; j < now.size(); j++)         if (ch[now[j]][i]) next.push_back(ch[now[j]][i]);       if (next.size() <= 1) continue;       res += merge(next);     }     return res;   }   void dfs(int x, int dep) {     vector<int> now;     now.clear();     maxdep = max(maxdep, dep);     for (int i = 0; i < 26; i++) {       if (!ch[x][i]) continue;       now.push_back(ch[x][i]);     }     int res = merge(now) + 1;     ans[dep] += res;     for (int i = 0; i < 26; i++) {       if (ch[x][i]) dfs(ch[x][i], dep + 1);     }   }   int main() {     scanf( %d , &N);     for (int i = 1; i < N; i++) {       int u, v;       char s[5];       scanf( %d%d , &u, &v);       scanf( %s , s);       ch[u][s[0] -  a ] = v;     }     memset(ans, 0, sizeof(ans));     dfs(1, 1);     int maxres = 0, mp;     for (int i = 1; i <= maxdep; i++)       if (maxres < ans[i]) {         maxres = ans[i];         mp = i;       }     printf( %d n%d n , N - maxres, mp);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 10;   int par[maxn], sub[maxn], col[maxn];   vector<pair<int, int>> parHis, colHis, subHis;   bool bad[maxn];   int c[maxn];   int root(int x) {     while (x != par[x]) {       x = par[x];     }     return x;   }   int getCol(int x) {     int res = 0;     while (x != par[x]) {       res ^= col[x];       x = par[x];     }     return res;   }   void save(int x, int y) {     parHis.emplace_back(x, par[x]);     subHis.emplace_back(y, sub[y]);     colHis.emplace_back(x, col[x]);   }   void join(int x, int y, bool rollback = false) {     int rx = root(x);     int ry = root(y);     int dif = (getCol(x) == getCol(y));     if (sub[rx] > sub[ry]) swap(rx, ry);     if (rollback) save(rx, ry);     par[rx] = ry;     sub[ry] += sub[rx];     col[rx] = dif;   }   bool check(int p, int q) {     if (root(p) == root(q) && getCol(p) == getCol(q)) return false;     return true;   }   int main() {     int n, m, k;     scanf( %d %d %d , &n, &m, &k);     for (int i = 1; i <= n; i++) {       scanf( %d , &c[i]);     }     map<pair<int, int>, vector<pair<int, int>>> cont;     for (int i = 1; i <= m; i++) {       int p, q;       scanf( %d %d , &p, &q);       if (c[p] > c[q]) swap(p, q);       cont[make_pair(c[p], c[q])].emplace_back(p, q);     }     for (int i = 1; i <= n; i++) {       par[i] = i;       col[i] = 0;       sub[i] = 1;     }     int active = k;     for (int i = 1; i <= k; i++) {       auto v = cont[make_pair(i, i)];       for (auto j : v) {         int p = j.first;         int q = j.second;         if (check(p, q)) {           join(p, q);         } else {           bad[i] = true;           active -= 1;           break;         }       }     }     long long ans = (1LL * active * (active - 1)) / 2;     for (auto i : cont) {       if (i.first.first == i.first.second) continue;       if (bad[i.first.first] || bad[i.first.second]) continue;       auto v = i.second;       for (auto j : v) {         int p = j.first;         int q = j.second;         if (check(p, q)) {           join(p, q, true);         } else {           ans -= 1;           break;         }       }       while (not parHis.empty()) {         auto ph = parHis.back();         auto ch = colHis.back();         auto sh = subHis.back();         parHis.pop_back();         colHis.pop_back();         subHis.pop_back();         par[ph.first] = ph.second;         col[ch.first] = ch.second;         sub[sh.first] = sh.second;       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using Int = long long;   constexpr static int mod = 1e9 + 7;   constexpr static int inf = (1 << 30) - 1;   constexpr static Int infll = (1LL << 61) - 1;   int Competitive_Programming =       (ios_base::sync_with_stdio(false), cin.tie(nullptr),        cout << fixed << setprecision(15), 0);   #pragma GCC target( avx2 )   #pragma GCC optimize( O3 )   #pragma GCC optimize( unroll-loops )   int main() {     Int N, D, M;     cin >> N >> D >> M;     vector<Int> a(N);     vector<Int> low, high;     for (int i = 0; i < N; i++) {       cin >> a[i];       if (a[i] > M)         high.push_back(a[i]);       else         low.push_back(a[i]);     }     sort(low.rbegin(), low.rend());     sort(high.rbegin(), high.rend());     vector<Int> lowsum(low.size() + 1), highsum(high.size() + 1);     for (int i = 0; i < low.size(); i++) lowsum[i + 1] = lowsum[i] + low[i];     for (int i = 0; i < high.size(); i++) highsum[i + 1] = highsum[i] + high[i];     Int ans = 0, hsz = highsum.size(), lsz = lowsum.size();     for (int i = 0; i < lsz; i++)       ans = max(ans, highsum[min(hsz - 1, (N - i + D) / (D + 1))] + lowsum[i]);     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500001;   const int sqN = 708;   int ans[N];   int fre[N];   int v[N];   int block[sqN];   int answer = 0;   struct query {     int l;     int r;     int idx;   } Q[N];   bool comp(query &q1, query &q2) {     if (q1.l / sqN != q2.l / sqN) {       return q1.l < q2.l;     }     if ((q1.l / sqN) & 1) {       return q1.r < q2.r;     }     return q1.r > q2.r;   }   inline void add(int idx) {     int val = v[idx];     fre[val]++;     if (fre[val] == 1) {       answer++;       block[val / sqN]++;     }     if (fre[val] == 2) {       answer--;       block[val / sqN]--;     }   }   inline void rem(int idx) {     int val = v[idx];     fre[val]--;     if (fre[val] == 1) {       answer++;       block[val / sqN]++;     }     if (fre[val] == 0) {       answer--;       block[val / sqN]--;     }   }   inline int solve() {     if (answer == 0) {       return 0;     }     for (int i = 0; i < sqN; i++) {       if (block[i] > 0) {         for (int j = i * sqN; j < i * sqN + sqN; j++) {           if (fre[j] == 1) {             return j;           }         }       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> v[i];     }     int q, l, r;     cin >> q;     for (int i = 0; i < q; i++) {       cin >> l >> r;       l--;       r--;       Q[i].l = l;       Q[i].r = r;       Q[i].idx = i;     }     sort(Q, Q + q, comp);     l = 0, r = -1;     for (int i = 0; i < q; i++) {       int L = Q[i].l;       int R = Q[i].r;       while (l < L) {         rem(l);         l++;       }       while (l > L) {         l--;         add(l);       }       while (r < R) {         r++;         add(r);       }       while (r > R) {         rem(r);         r--;       }       ans[Q[i].idx] = solve();     }     for (int i = 0; i < q; i++) {       cout << ans[i] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 5000;   long long cumulative[N + 10], arr[N + 10][3];   signed main() {     ios ::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, q;     cin >> n >> q;     vector<pair<long long, long long> > pi;     for (long long i = 0; i < q; i++) {       long long a, b;       cin >> a >> b;       cumulative[a] += 1;       cumulative[b + 1] -= 1;       pi.push_back({a, b});     }     for (long long i = 1; i <= n; i++) {       cumulative[i] += cumulative[i - 1];     }     for (long long i = 1; i <= n; i++) {       for (long long j = 0; j < 3; j++) {         if (cumulative[i] == j) {           arr[i][j] += (arr[i - 1][j] + 1);         } else {           arr[i][j] += arr[i - 1][j];         }       }     }     long long sz = pi.size();     sort(pi.begin(), pi.end());     long long ans = 0;     for (long long i = 0; i < sz - 1; i++) {       long long x = pi[i].first;       long long y = pi[i].second;       for (long long j = i + 1; j < sz; j++) {         long long two = 0;         long long a = pi[j].first;         long long b = pi[j].second;         if (a <= y) {           long long ax = max(x, a);           long long by = min(y, b);           two = (arr[by][2] - arr[ax - 1][2]);         }         long long zero = (arr[n][0] - arr[0][0]);         long long one = (arr[y][1] - arr[x - 1][1]) + (arr[b][1] - arr[a - 1][1]);         long long total = one + two + zero;         long long res = n - total;         ans = max(ans, res);       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long int test = 1;     for (int loop = 0; loop < test; loop++) {       long long int num, val;       cin >> num >> val;       vector<long long int> arr;       long long int temp;       for (int i = 0; i < num; i++) {         cin >> temp;         arr.push_back(temp);       }       long long int count = 0;       for (int i = 0; i < num; i++) {         for (int j = 0; j < num; j++) {           if (abs(arr[i] - arr[j]) <= val && i != j) count++;         }       }       cout << count <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   const int MOD = 1e9 + 7;   const int dx[4] = {0, 0, -1, 1};   const int dy[4] = {-1, 1, 0, 0};   const int N = 101;   int n, m, a[N][N], psum[N][N], T;   bool Check_1() {     if (psum[1][m] == 0) return true;     if (psum[n][1] == 0) return true;     if (psum[n][m] - psum[n - 1][m] == 0) return true;     if (psum[n][m] - psum[n][m - 1] == 0) return true;     return false;   }   bool Check_2() {     if (a[1][1] == 0 || a[1][m] == 0 || a[n][1] == 0 || a[n][m] == 0) return true;     for (int i = 2; i < n; i++) {       if (psum[i][m] - psum[i - 1][m] == 0) return true;     }     for (int i = 2; i < m; i++) {       if (psum[n][i] - psum[n][i - 1] == 0) return true;     }     return false;   }   bool Check_3() {     for (int i = 2; i < n; i++) {       if (a[i][1] == 0 || a[i][m] == 0) return true;     }     for (int i = 2; i < m; i++) {       if (a[1][i] == 0 || a[n][i] == 0) return true;     }     return false;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> T;     while (T--) {       cin >> n >> m;       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= m; j++) {           char c;           cin >> c;           int x = 0;           if (c ==  P ) x = 1;           psum[i][j] = psum[i - 1][j] + psum[i][j - 1] - psum[i - 1][j - 1] + x;           a[i][j] = x;         }       }       if (psum[n][m] == n * m) {         cout <<  MORTAL n ;         continue;       }       if (psum[n][m] == 0) {         cout << 0 <<   n ;         continue;       }       if (Check_1())         cout << 1 <<   n ;       else if (Check_2())         cout << 2 <<   n ;       else if (Check_3())         cout << 3 <<   n ;       else         cout << 4 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct P {     long long x, y;   };   P rotate90(P a) {     P b;     b.x = -a.y, b.y = a.x;     return b;   }   P ar[4], br[4], cr[4];   P add(P a, P b) {     P c;     c.x = a.x + b.x;     c.y = a.y + b.y;     return c;   }   long long dis(P a, P b) {     return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);   }   int check(P eop[]) {     long long l[16], tp = 0;     for (int i = 0; i < 4; i++)       for (int j = i + 1; j < 4; j++) l[tp++] = dis(eop[i], eop[j]);     sort(l, l + tp);     if (l[0] == l[1] && l[1] == l[2] && l[2] == l[3] && l[0] == l[3] && l[0] &&         l[4] == l[5] && l[0] * 2 == l[4])       return 1;     else       return 0;   }   int main() {     int n;     scanf( %d , &n);     while (n--) {       for (int i = 0; i < 4; i++) {         scanf( %I64d%I64d%I64d%I64d , &ar[i].x, &ar[i].y, &br[i].x, &br[i].y);         cr[i].x = ar[i].x - br[i].x;         cr[i].y = ar[i].y - br[i].y;       }       int ans = 999;       for (int i = 0; i < 4; i++) {         for (int j = 0; j < 4; j++) {           for (int k = 0; k < 4; k++) {             for (int l = 0; l < 4; l++) {               P eop[4];               for (int z = 0; z < 4; z++) eop[z] = cr[z];               for (int z = 0; z < i; z++) eop[0] = rotate90(eop[0]);               for (int z = 0; z < j; z++) eop[1] = rotate90(eop[1]);               for (int z = 0; z < k; z++) eop[2] = rotate90(eop[2]);               for (int z = 0; z < l; z++) eop[3] = rotate90(eop[3]);               for (int z = 0; z < 4; z++) eop[z] = add(eop[z], br[z]);               if (check(eop)) ans = min(ans, i + j + k + l);             }           }         }       }       printf( %d n , ans == 999 ? -1 : ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long hit(long long n) { return ((1 + n) * n) / 2; }   int main() {     long long m, n;     scanf( %lld%lld , &n, &m);     long long hasilmin, hasilmax;     hasilmin = hit(n / m) * (n % m) + hit(n / m - 1) * (m - n % m);     hasilmax = hit(n - m);     printf( %lld %lld n , hasilmin, hasilmax);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int n, m, sx, sy, vx, vy;   inline long long int extgcd(long long int a, long long int b, long long int &x,                               long long int &y) {     if (!b) {       x = 1;       y = 0;       return a;     }     long long int d = extgcd(b, a % b, x, y);     long long int _x = y;     long long int _y = x - (a / b) * y;     x = _x;     y = _y;     return d;   }   inline long long int crt(long long int a1, long long int m1, long long int a2,                            long long int m2) {     a1 = (a1 + m1) % m1;     a2 = (a2 + m2) % m2;     if (a1 > a2) {       swap(a1, a2);       swap(m1, m2);     }     long long int x, y;     long long int d = extgcd(m1, m2, x, y);     if ((a2 - a1) % d) return -1;     x *= (a2 - a1) / d;     y *= -(a2 - a1) / d;     x = (x - (x / (m2 / d)) * m2 / d + m2 / d) % (m2 / d);     return m1 * x + a1;   }   signed main() {     cin >> n >> m >> sx >> sy >> vx >> vy;     if ((sx == 0 && sy == 0) || (sx == 0 && sy == m) || (sx == n && sy == 0) ||         (sx == n && sy == m)) {       cout << sx <<     << sy << endl;       return 0;     }     if (!vx) {       if (sx == 0) {         if (vy == 1) {           cout <<  0   << m << endl;         } else {           cout <<  0 0 n  << endl;         }       } else if (sx == n) {         if (vy == 1) {           cout << n <<     << m << endl;         } else {           cout << n <<     << 0 << endl;         }       } else {         cout << -1 << endl;       }       return 0;     }     if (!vy) {       if (sy == 0) {         if (vx == 1) {           cout << n <<   0  << endl;         } else {           cout <<  0 0 n  << endl;         }       } else if (sy == m) {         if (vx == 1) {           cout << n <<     << m << endl;         } else {           cout <<  0   << m << endl;         }       } else {         cout << -1 << endl;       }       return 0;     }     long long int T = crt(n - vx * sx, n, m - vy * sy, m);     if (T == -1) {       cout << -1 << endl;       return 0;     }     long long int X = sx + T * vx;     long long int Y = sy + T * vy;     X = (X < 0 ? -X : X);     Y = (Y < 0 ? -Y : Y);     cout << ((X / n) & 1) * n <<     << ((Y / m) & 1) * m << endl;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:500000000 )   using namespace std;   bool isq[22 * 22 * 22 * 22];   bool isSquare(int n) { return isq[n]; }   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= 22 * 22; i++) isq[i * i] = true;     for (int columnLine = 0; columnLine <= m; columnLine++) {       for (int rowLine = 0; rowLine <= n; rowLine++) {         for (int aValue = 1; aValue <= 22; aValue++)           for (int bValue = 1; bValue <= 22; bValue++)             for (int cValue = 1; cValue <= 22; cValue++)               for (int dValue = 1; dValue <= 22; dValue++) {                 int sqA = aValue * aValue;                 int sqB = bValue * bValue;                 int sqC = cValue * cValue;                 int sqD = dValue * dValue;                 if (isSquare(columnLine * sqA + (m - columnLine) * sqB) &&                     isSquare(columnLine * sqC + (m - columnLine) * sqD) &&                     isSquare(rowLine * sqA + (n - rowLine) * sqC) &&                     isSquare(rowLine * sqB + (n - rowLine) * sqD)) {                   for (int i = 0; i < n; i++) {                     for (int j = 0; j < m; j++) {                       if (i < rowLine && j < columnLine)                         printf( %d  , aValue);                       else if (i < rowLine && j >= columnLine)                         printf( %d  , bValue);                       else if (i >= rowLine && j < columnLine)                         printf( %d  , cValue);                       else                         printf( %d  , dValue);                     }                     puts(  );                   }                   return 0;                 }               }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e3 + 5, M = 2e6 + 5, Mod = 1e9 + 7;   int add(int a, int b) { return ((a += b) >= Mod) ? (a - Mod) : (a); }   int gcd(int a, int b) { return (!b) ? (a) : (gcd(b, a % b)); }   bool _pool[M << 1];   bool *_top = _pool;   bool *alloc(int sz) {     bool *top = _top;     _top += sz;     return top;   }   int n, a, b;   char s[M];   int sz[N];   bool *own[N];   bool G[N][N];   stack<int> S;   vector<int> SCC;   bool ins[N], vis[M];   int dfs_clock, cnt_scc;   int dfn[N], low[N], bel[N];   int sz_sc[N];   bool *own_sc[N];   void dispose() {     int D = 0;     for (auto p : SCC) {       D = gcd(D, sz[p]);     }     sz_sc[cnt_scc] = D;     own_sc[cnt_scc] = alloc(D);     bool *app = own_sc[cnt_scc];     for (auto &p : SCC) {       for (int i = 0; i < sz[p]; i++) {         if (own[p][i]) {           app[i % D] = true;         }       }     }   }   void tarjan(int p) {     dfn[p] = low[p] = ++dfs_clock;     vis[p] = ins[p] = true;     S.push(p);     for (int e = 1; e <= n; e++) {       if (G[p][e]) {         if (!vis[e]) {           tarjan(e);           low[p] = min(low[p], low[e]);         } else if (ins[e]) {           low[p] = min(low[p], dfn[e]);         }       }     }     if (dfn[p] == low[p]) {       int cur;       SCC.clear();       ++cnt_scc;       do {         cur = S.top();         S.pop();         SCC.push_back(cur);         bel[cur] = cnt_scc;         ins[cur] = false;       } while (cur != p);       dispose();     }   }   int deg[N];   int order[N];   bool nG[N][N];   void rebuild() {     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (bel[i] != bel[j] && G[i][j]) {           nG[bel[i]][bel[j]] = true;         }       }     }     for (int i = 1; i <= cnt_scc; i++) {       for (int j = i + 1; j <= cnt_scc; j++) {         if (nG[i][j]) {           deg[j]++;         } else {           deg[i]++;         }       }     }     for (int i = 1; i <= cnt_scc; i++) {       order[deg[i]] = i;     }   }   pair<int, int> A[N];   int comb[N][N];   int main() {     scanf( %d%d%d , &n, &a, &b);     for (int i = 1; i <= n; i++) {       scanf( %s , s + 1);       for (int j = 1; j <= n; j++) {         G[i][j] = s[j] -  0 ;       }     }     for (int i = 1; i <= n; i++) {       scanf( %d , sz + i);       own[i] = alloc(sz[i]);       scanf( %s , s);       for (int j = 0; j < sz[i]; j++) {         own[i][j] = s[j] -  0 ;       }     }     for (int i = 1; i <= n; i++) {       if (!vis[i]) {         tarjan(i);       }     }     rebuild();     for (int i = 1; i < cnt_scc; i++) {       int x = order[i - 1], y = order[i];       int d = gcd(sz_sc[x], sz_sc[y]);       memset(vis, false, d);       for (int j = 0; j < sz_sc[x]; j++) {         if (own_sc[x][j]) {           vis[j % d] = true;         }       }       for (int j = 0; j < sz_sc[y]; j++) {         own_sc[y][j] = own_sc[y][j] || vis[j % d];       }     }     for (int i = 1; i <= n; i++) {       bool *app = own_sc[bel[i]];       int D = sz_sc[bel[i]];       for (int j = 0; j < sz[i]; j++) {         if (own[i][j]) {           A[i].second++;         } else if (app[j % D]) {           A[i].first++;         }       }       A[i].first += A[i].second;     }     sort(A + 1, A + n + 1);     reverse(A + 1, A + n + 1);     comb[0][0] = 1;     for (int i = 1; i <= n; i++) {       comb[i][0] = comb[i][i] = 1;       for (int j = 1; j < i; j++) {         comb[i][j] = add(comb[i - 1][j - 1], comb[i - 1][j]);       }     }     int c = a - b, ans = 0;     for (int i = b; i <= n; i++) {       int x = 0, y;       for (int j = 1; j < i; j++) {         if (A[j].second > A[i].first) {           x++;         }       }       y = max(x - c, 0);       for (int j = y; j < b; j++) {         ans = (ans + comb[x][j] * 1ll * comb[i - x - 1][b - j - 1]) % Mod;       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t;     cin >> t;     while (t--) {       long long int n;       string s;       cin >> n >> s;       map<long long int, vector<long long int>> mp;       for (long long int i = 0; i < s.size(); i++) {         mp[s[i] -  0 ].push_back(i);       }       vector<long long int> ans(n + 1, 0);       long long int las = -1;       for (auto x : mp) {         vector<long long int> v = x.second;         long long int cnt = 0;         for (long long int i = 0; i < v.size(); i++) {           if (v[i] > las) {             ans[v[i]] = 1;             cnt++;             las = v[i];           }         }         if (cnt != v.size()) {           break;         }       }       vector<long long int> ck;       for (long long int i = 0; i < n; i++) {         if (ans[i] == 1) {           ck.push_back(s[i] -  0 );         }       }       for (long long int i = 0; i < n; i++) {         if (ans[i] != 1) {           ck.push_back(s[i] -  0 );         }       }       long long int f = 0;       for (long long int i = 1; i < n; i++) {         if (ck[i] < ck[i - 1]) {           f = 1;           break;         }       }       if (f == 1) {         cout <<  -  << endl;       } else {         string str;         for (long long int i = 0; i < n; i++) {           if (ans[i] == 0) {             str +=  2 ;           } else {             str +=  1 ;           }         }         cout << str << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   int min(int a, int b) { return a < b ? a : b; }   int n, m, t1, t2, val[1024];   long long ans;   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &val[i]);     }     for (int i = 1; i <= m; i++) {       scanf( %d%d , &t1, &t2);       ans += min(val[t1], val[t2]);     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a;     cin >> a;     for (int i = a; i >= 1; i--) {       if (a % i == 0) {         cout << i <<    ;         a = i;       }     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     vector<long long> arr(n);     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     long long c = 0;     for (int i = 0; i < n; i++) {       c += arr[i] - 1;       if (c % 2 == 0)         cout << 2 <<   n ;       else         cout << 1 <<   n ;     }   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(nullptr);     long long T;     T = 1;     for (int t = 0; t < T; t++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char cross[101][101];   int main() {     int n;     while (cin >> n) {       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++) cin >> cross[i][j];       int ok = 0;       for (int i = 1; i < n - 1; i++) {         for (int j = 1; j < n - 1; j++) {           if (cross[i][j] ==  # ) {             if (cross[i][j] ==  #  && cross[i - 1][j] ==  #  &&                 cross[i][j - 1] ==  #  && cross[i][j + 1] ==  #  &&                 cross[i + 1][j] ==  # ) {               cross[i][j] =  a ;               cross[i - 1][j] =  a ;               cross[i][j - 1] =  a ;               cross[i][j + 1] =  a ;               cross[i + 1][j] =  a ;             }           }         }       }       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++)           if (cross[i][j] ==  # ) {             ok = 1;             break;             break;           }       if (ok == 0)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }