#include <bits/stdc++.h> n using namespace std; n long long int countInversions(long long int a[], long long int temp[], n                               long long int l, long long int r) { n   if (l >= r) { n     return 0; n   } n   long long int count1 = countInversions(a, temp, l, (l + r) / 2); n   long long int count2 = countInversions(a, temp, (l + r) / 2 + 1, r); n   long long int mergeCount = 0; n   long long int indexLeft = l; n   long long int endLeft = (l + r) / 2 + 1; n   long long int indexRight = (l + r) / 2 + 1; n   while (indexRight <= r && indexLeft < endLeft) { n     while (indexLeft < endLeft && a[indexLeft] <= a[indexRight]) { n       indexLeft++; n     } n     mergeCount += endLeft - indexLeft; n     indexRight++; n   } n   indexLeft = l, indexRight = (l + r) / 2 + 1; n   long long int idx = l; n   while (indexRight <= r && indexLeft < endLeft) { n     if (a[indexLeft] <= a[indexRight]) { n       temp[idx++] = a[indexLeft]; n       indexLeft++; n     } else { n       temp[idx++] = a[indexRight]; n       indexRight++; n     } n   } n   while (indexRight <= r) { n     temp[idx++] = a[indexRight]; n     indexRight++; n   } n   while (indexLeft < endLeft) { n     temp[idx++] = a[indexLeft]; n     indexLeft++; n   } n   for (long long int i = l; i <= r; i++) { n     a[i] = temp[i]; n   } n   return mergeCount + count1 + count2; n } n int main() { n   ios ::sync_with_stdio(false); n   cin.tie(0); n   map<char, deque<long long int>> positionsOfLetters; n   long long int i, n, count = 0; n   cin >> n; n   char s[n]; n   for (i = 0; i < n; i++) { n     cin >> s[i]; n     positionsOfLetters[s[i]].push_back(i); n   } n   reverse(s, s + n); n   long long int permuation[n]; n   for (i = 0; i < n; i++) { n     permuation[i] = positionsOfLetters[s[i]].front(); n     positionsOfLetters[s[i]].pop_front(); n   } n   long long int temp[n + 1]; n   cout << countInversions(permuation, temp, 0, n - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, t; n   cin >> s; n   bool ok = false; n   for (int i = 0; i < s.size(); i++) { n     if (s.size() - i < 26) break; n     int j = i; n     vector<int> f(26); n     int qq = 0; n     while (j < s.size() && j - i + 1 <= 26) { n       if (s[j] !=  ? ) n         f[s[j] -  A ]++; n       else n         qq++; n       if (j - i + 1 == 26) break; n       j++; n     } n     int have = 0; n     queue<char> need; n     for (int k = 0; k < 26; k++) { n       if (f[k] > 0) n         have++; n       else n         need.push(char(k +  A )); n     } n     int want = 26 - have; n     if (want == qq) { n       ok = true; n       for (int k = i; k <= j; k++) n         if (s[k] ==  ? ) { n           s[k] = need.front(); n           need.pop(); n         } n     } n   } n   if (!ok) { n     puts( -1 ); n   } else { n     for (auto c : s) { n       if (c ==  ? ) n         cout <<  A  <<   ; n       else n         cout << c <<   ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[400005]; n int q, x; n int main() { n   int aux, cnt = 0, maxim = 0; n   cin >> q >> x; n   for (int i = 1; i <= q; i++) { n     cin >> aux; n     a[aux % x]++; n     while (a[cnt] > maxim) { n       cnt++; n       if (cnt == x) { n         cnt = 0; n         maxim++; n       } n     } n     cout << maxim * x + cnt <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[4] = {-1, 0, 0, 1}; n int dy[4] = {0, -1, 1, 0}; n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.first == b.first) return a.second < b.second; n   return (a.first < b.first); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long x, y, t, c; n   cin >> x >> y; n   if (y == 0) { n     cout <<  No  << endl; n     return 0; n   } n   if (y == 1) { n     if (x > 0) { n       cout <<  No  << endl; n       return 0; n     } n   } n   t = y - 1; n   t = x - t; n   if (t % 2 == 0 && t >= 0) { n     cout <<  Yes  << endl; n     return 0; n   } n   cout <<  No  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a[6], b[6]; n int getMinWin(int idx) { n   if (idx == 3) return a[0] + a[1] + a[2]; n   int ret = INT_MAX; n   int bIdx = idx; n   int bIdxPrev = (idx - 1 + 3) % 3; n   int minIdxbIdx = min(a[idx], b[bIdx]); n   a[idx] -= minIdxbIdx; n   b[bIdx] -= minIdxbIdx; n   int minIdxbIdxPrev = min(a[idx], b[bIdxPrev]); n   a[idx] -= minIdxbIdxPrev; n   b[bIdxPrev] -= minIdxbIdxPrev; n   ret = min(ret, getMinWin(idx + 1)); n   a[idx] += minIdxbIdx; n   b[bIdx] += minIdxbIdx; n   a[idx] += minIdxbIdxPrev; n   b[bIdxPrev] += minIdxbIdxPrev; n   return ret; n } n int getMaxWin() { return min(a[0], b[1]) + min(a[1], b[2]) + min(a[2], b[0]); } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < 3; i++) scanf( %d , &a[i]); n   for (int i = 0; i < 3; i++) scanf( %d , &b[i]); n   int minWin = INT_MAX; n   for (int i = 0; i < 3; i++) { n     minWin = min(minWin, getMinWin(0)); n     for (int j = 0; j < 2; j++) { n       swap(a[j], a[j + 1]); n       swap(b[j], b[j + 1]); n     } n   } n   printf( %d %d n , minWin, getMaxWin()); n } n


#include <bits/stdc++.h> n int n, d, m, t[200005][2], l, p, r, b; n long long ans; n std::pair<int, int> s[200005]; n bool a = true; n int main() { n   scanf( %d %d %d , &d, &n, &m); n   for (int i = 0; i < m; i++) scanf( %d %d , &s[i].first, &s[i].second); n   std::sort(s, s + m); n   t[l][0] = 0; n   t[l++][1] = n; n   s[m].first = d; n   s[m].second = 0; n   for (int i = 0; i <= m && a; i++) { n     if (s[i].first - p > n) n       a = false; n     else { n       r = s[i].first - p; n       while (s[i].first > p) { n         if (p + t[b][1] <= s[i].first) { n           ans += (long long)t[b][0] * t[b][1]; n           p += t[b][1]; n           b++; n         } else { n           ans += (long long)t[b][0] * (s[i].first - p); n           t[b][1] -= s[i].first - p; n           p = s[i].first; n         } n       } n       while (l > b && s[i].second <= t[l - 1][0]) r += t[--l][1]; n       t[l][0] = s[i].second; n       t[l++][1] = r; n     } n   } n   printf( %I64d n , a ? ans : -1); n   scanf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if ((a == 0) || (b == 0)) return a + b; n   return gcd(b, a % b); n } n long long pow_mod(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % 1000000007; n     a = (a * a) % 1000000007; n     b >>= 1; n   } n   return res; n } n long long pow_2(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a); n     a = (a * a); n     b >>= 1; n   } n   return res; n } n bool isPrime(long long a) { n   for (int i = 3; (i * i) <= a; i += 2) { n     if ((a % i) == 0) return false; n   } n   if ((a != 2) && ((a % 2) == 0)) return false; n   return true; n } n string con_ll_to_str(int a) { n   stringstream mystr; n   mystr << a; n   return mystr.str(); n } n long long con_str_to_ll(string st) { n   long long numb = 0; n   int len = st.size(), i, j = 0; n   for (i = len - 1; i >= 0; i--) { n     numb += (pow_mod(10, j) * (st[i] -  0 )); n     j++; n   } n   return numb; n } n long long baseno_to_decimal(string st, long long basee) { n   long long i = 0, j, anss = 0; n   for (j = (int)st.length() - 1; j >= 0; j--) { n     anss += (st[j] -  0 ) * pow_2(basee, i); n     i++; n   } n   return anss; n } n string decimal_to_string(long long num, long long basee) { n   long long i = 0, j, opop; n   string anss =   ; n   vector<string> stri; n   stri.push_back( 0 ); n   stri.push_back( 1 ); n   stri.push_back( 2 ); n   stri.push_back( 3 ); n   stri.push_back( 4 ); n   stri.push_back( 5 ); n   stri.push_back( 6 ); n   stri.push_back( 7 ); n   stri.push_back( 8 ); n   stri.push_back( 9 ); n   stri.push_back( A ); n   stri.push_back( B ); n   stri.push_back( C ); n   stri.push_back( D ); n   stri.push_back( E ); n   stri.push_back( F ); n   while (num) { n     opop = num % basee; n     anss += stri[opop]; n     num /= basee; n   } n   reverse(anss.begin(), anss.end()); n   return anss; n } n long long par[10010], dist[10010], N, M; n bool flag1, flag2; n pair<long long, long long> mapp[6]; n map<string, string> ma; n vector<long long> vc[10010]; n string st[110]; n vector<long long> path_d; n struct comp { n   bool operator()(pair<long long, long long> n1, n                   pair<long long, long long> n2) { n     return n1.second > n2.second; n   } n }; n void dijk(long long src) { n   priority_queue<pair<long long, long long>, n                  vector<pair<long long, long long> >, comp> n       prqu; n   prqu.push(make_pair(src, 0)); n   long long i, j, kk; n   for (i = 0; i <= 10000; i++) { n     dist[i] = INT_MAX; n   } n   memset(par, -1, sizeof(par)); n   dist[src] = 0; n   while (!prqu.empty()) { n     pair<long long, long long> xx = prqu.top(); n     prqu.pop(); n     for (kk = 0; kk <= (int)vc[xx.first].size() - 1; kk++) { n       if (dist[vc[xx.first][kk]] > (xx.second + 1)) { n         par[vc[xx.first][kk]] = xx.first; n         dist[vc[xx.first][kk]] = xx.second + 1; n         prqu.push(make_pair(vc[xx.first][kk], xx.second + 1)); n       } n     } n   } n } n void gr_cons(long long i, long long j, long long fi) { n   if (i <= 0 || i > N || j <= 0 || j > M) { n     return; n   } n   long long sec; n   if (st[i][j] ==  .  || st[i][j] ==  F ) { n     sec = ((i - 1) * M) + j; n     vc[fi].push_back(sec); n   } n } n string rev(string ch) { n   if (ch ==  U  || ch ==  D ) { n     if (ch ==  U ) return  D ; n     return  U ; n   } n   if (ch ==  L  || ch ==  R ) { n     if (ch ==  L ) return  R ; n     return  L ; n   } n } n void find_pos(long long i, long long j, pair<long long, long long> poss) { n   if ((poss.first + 1) == i && j == poss.second) { n     cout << ma[ D ] <<   n ; n   } else if ((poss.first - 1) == i && j == poss.second) { n     cout << ma[ U ] <<   n ; n   } else if (poss.first == i && j == (poss.second - 1)) { n     cout << ma[ L ] <<   n ; n   } else { n     cout << ma[ R ] <<   n ; n   } n } n int main() { n   long long i, j, fi, X, Y, o1; n   cin >> N >> M; n   for (i = 1; i <= N; i++) { n     cin >> st[i]; n     st[i] =  #  + st[i]; n     st[0] = st[i]; n     st[N + 1] = st[i]; n   } n   for (i = 0; i <= M; i++) { n     st[0][i] =  . ; n     st[i][0] =  . ; n     st[i][M + 1] =  . ; n     st[N + 1][i] =  . ; n   } n   for (i = 1; i <= N; i++) { n     for (j = 1; j <= M; j++) { n       if (st[i][j] ==  .  || st[i][j] ==  F ) { n         fi = (i - 1) * M + j; n         gr_cons(i - 1, j, fi); n         gr_cons(i + 1, j, fi); n         gr_cons(i, j - 1, fi); n         gr_cons(i, j + 1, fi); n       } n       if (st[i][j] ==  F ) { n         o1 = (i - 1) * M + j; n       } n     } n   } n   dijk(1); n   while (o1 != -1) { n     path_d.push_back(o1); n     o1 = par[o1]; n   } n   reverse(path_d.begin(), path_d.end()); n   pair<long long, long long> pos = make_pair(1, 1); n   stack<string> stac; n   if (st[pos.first + 1][pos.second] !=  *  && n       st[pos.first - 1][pos.second] !=  * ) { n     flag1 = 1; n     fflush(stdout); n     cout <<  U  n          <<   n ; n     fflush(stdout); n     cin >> X >> Y; n     if (st[X][Y] ==  F ) { n       return 0; n     } n     if (X == (pos.first - 1) || X == pos.first) { n       ma[ U ] =  U ; n       ma[ D ] =  D ; n     } else { n       ma[ U ] =  D ; n       ma[ D ] =  U ; n     } n     pos = make_pair(X, Y); n   } else if (st[pos.first][pos.second + 1] !=  *  && n              st[pos.first][pos.second - 1] !=  * ) { n     flag2 = 1; n     fflush(stdout); n     cout <<  R  n          <<   n ; n     fflush(stdout); n     cin >> X >> Y; n     if (st[X][Y] ==  F ) { n       return 0; n     } n     if (Y == (pos.second + 1)) { n       ma[ L ] =  L ; n       ma[ R ] =  R ; n     } else { n       ma[ L ] =  R ; n       ma[ R ] =  L ; n     } n     pos = make_pair(X, Y); n   } n   if (flag1) { n     if (pos != make_pair(1LL, 1LL)) { n       fflush(stdout); n       cout << rev( U ) <<   n ; n       fflush(stdout); n       cin >> X >> Y; n     } n     pos = make_pair(1, 1); n     while (1) { n       if (st[pos.first][pos.second + 1] !=  *  && n           st[pos.first][pos.second - 1] !=  * ) { n         fflush(stdout); n         cout <<  R  n              <<   n ; n         fflush(stdout); n         cin >> X >> Y; n         if (pos != make_pair(X, Y)) stac.push(rev( R )); n         if (st[X][Y] ==  F ) { n           return 0; n         } n         if (Y == (pos.second + 1)) { n           ma[ L ] =  L ; n           ma[ R ] =  R ; n         } else { n           ma[ L ] =  R ; n           ma[ R ] =  L ; n         } n         pos = make_pair(X, Y); n         break; n       } n       fflush(stdout); n       cout << ma[ D ] <<   n ; n       fflush(stdout); n       cin >> X >> Y; n       pos = make_pair(X, Y); n       stac.push(rev(ma[ D ])); n       if (st[X][Y] ==  F ) { n         return 0; n       } n     } n   } else { n     if (pos != make_pair(1LL, 1LL)) { n       fflush(stdout); n       cout << rev( R ) <<   n ; n       fflush(stdout); n       cin >> X >> Y; n     } n     while (1) { n       if (st[pos.first + 1][pos.second] !=  *  && n           st[pos.first - 1][pos.second] !=  * ) { n         fflush(stdout); n         cout <<  U  n              <<   n ; n         fflush(stdout); n         cin >> X >> Y; n         if (pos != make_pair(X, Y)) stac.push(rev( U )); n         if (st[X][Y] ==  F ) { n           return 0; n         } n         if (X == (pos.first - 1) || X == pos.first) { n           ma[ U ] =  U ; n           ma[ D ] =  D ; n         } else { n           ma[ U ] =  D ; n           ma[ D ] =  U ; n         } n         pos = make_pair(X, Y); n         break; n       } n       fflush(stdout); n       cout << ma[ R ] <<   n ; n       fflush(stdout); n       cin >> X >> Y; n       pos = make_pair(X, Y); n       stac.push(rev(ma[ R ])); n       if (st[X][Y] ==  F ) { n         return 0; n       } n     } n   } n   if (pos != make_pair(1LL, 1LL)) { n     while (stac.size()) { n       fflush(stdout); n       cout << stac.top() <<   n ; n       fflush(stdout); n       stac.pop(); n       cin >> X >> Y; n     } n   } n   pos = make_pair(1, 1); n   for (i = 1; i <= (int)path_d.size() - 1; i++) { n     fflush(stdout); n     if (!(path_d[i] % M)) { n       find_pos(path_d[i] / M, M, pos); n     } else { n       find_pos((path_d[i] / M) + 1, (path_d[i] % M), pos); n     } n     fflush(stdout); n     cin >> X >> Y; n     pos = make_pair(X, Y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i; n int p[200011]; n int aib[200011]; n int ord[2][200011]; n int ans[200011]; n int zrs(int x) { return (x & (x - 1)) ^ x; } n void add(int x, int v) { n   while (x <= n) { n     aib[x] += v; n     x += zrs(x); n   } n } n int sum(int x) { n   int ans = 0; n   while (x > 0) { n     ans += aib[x]; n     x -= zrs(x); n   } n   return ans; n } n int cb(int x) { n   int ans = 0; n   for (int step = (1 << 19); step > 0; step >>= 1) n     if (ans + step <= n) n       if (aib[ans + step] <= x) x -= aib[ans += step]; n   return ans; n } n void get_order(int n, int *p, int *ans) { n   int i; n   for (i = 1; i <= n; i++) aib[i] = zrs(i); n   memset(ans, 0, sizeof(ans)); n   for (i = 1; i <= n; i++) { n     ans[n - i] = sum(p[i] - 1); n     add(p[i], -1); n   } n } n void get_perm(int n, int *ord, int *ans) { n   int i, cnt, pos; n   for (i = 1; i <= n; i++) aib[i] = zrs(i); n   memset(ans, 0, sizeof(ans)); n   for (i = 1; i <= n; i++) { n     cnt = ord[n - i] + 1; n     pos = cb(cnt - 1) + 1; n     ans[i] = pos; n     add(pos, -1); n   } n } n int main() { n   scanf( %d , &n); n   for (i = 1; i <= n; i++) scanf( %d , &p[i]), p[i]++; n   get_order(n, p, ord[0]); n   for (i = 1; i <= n; i++) scanf( %d , &p[i]), p[i]++; n   get_order(n, p, ord[1]); n   for (i = 1; i <= n; i++) ord[0][i] += ord[1][i]; n   for (i = 1; i <= n; i++) { n     ord[0][i + 1] += ord[0][i] / (i + 1); n     ord[0][i] %= i + 1; n   } n   get_perm(n, ord[0], ans); n   for (i = 1; i <= n; i++) printf( %d  , ans[i] - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N = 0, p = 0, q = 0; n   int k = 0; n   cin >> N; n   for (int i = 0; i < N; i++) { n     cin >> p >> q; n     if (q - p >= 2) k++; n   } n   cout << k; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int nax = 1e6 + 10; n vector<int> tree(4 * nax); n long long i, j, x, y, k; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, t; n   cin >> t; n   while (t--) { n     cin >> n; n     vector<long long> a; n     set<long long> b; n     for (i = 0; i < n; i++) { n       cin >> x; n       a.push_back(x); n       b.insert(x); n     } n     if (b.size() == 1) { n       cout << 1 << endl; n       for (i = 0; i < n; i++) cout << 1 <<    ; n     } else if (a.size() % 2 == 0) { n       cout << 2 << endl; n       for (i = 0; i < n / 2; i++) cout << 1 <<     << 2 <<    ; n     } else if (a.size() % 2 != 0) { n       vector<long long> ans; n       int p = 0, q = 0; n       ans.push_back(1); n       for (i = 1; i < a.size() - 1; i++) { n         if (a[i] == a[i - 1] && p == 0) { n           ans.push_back(ans[i - 1]); n           p = 1; n         } else if (q == 0) { n           ans.push_back(2); n           q = 1; n         } else if (q == 1) { n           ans.push_back(1); n           q = 0; n         } n       } n       if (p == 1) { n         if (q == 1) n           ans.push_back(1); n         else n           ans.push_back(2); n       } else { n         if (a[n - 1] == a[n - 2]) n           ans.push_back(ans[n - 2]); n         else if (a[n - 1] == a[0]) n           ans.push_back(ans[0]); n         else n           ans.push_back(3); n       } n       if (ans[n - 1] == 3) n         cout << 3 << endl; n       else n         cout << 2 << endl; n       for (i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> lucky; n void backtrack(long long x) { n   if (x > 1e10) return; n   lucky.push_back(x); n   backtrack(x * 10 + 4); n   backtrack(x * 10 + 7); n } n int main() { n   long long ans = 0; n   backtrack(0); n   sort(lucky.begin(), lucky.end()); n   int a, b; n   cin >> a >> b; n   for (int i = 1; i < (int)lucky.size(); i++) { n     int l, r, m; n     l = a, r = b; n     while (l <= r) { n       m = (l + r) >> 1; n       if (m <= lucky[i]) { n         ans += lucky[i] * (m - l + 1); n         a = m + 1; n         i--; n         break; n       } else { n         r = m - 1; n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   vector<pair<int, int>> vp(n); n   for (int i = 0; i < n; i++) cin >> vp[i].first >> vp[i].second; n   sort(vp.begin(), vp.end()); n   long long res = 1; n   pair<int, int> pa = {vp[0].first, vp[0].second}; n   for (int i = 0; i < n; i++) { n     if (vp[i].first > pa.second) n       pa = {vp[i].first, vp[i].second}, res++; n     else if (vp[i].second < pa.second) n       pa = {vp[i].first, vp[i].second}; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   int m, c; n   int mc = 0, cc = 0; n   cin >> t; n   while (t--) { n     cin >> m >> c; n     if (m > c) n       mc++; n     else if (m < c) n       cc++; n   } n   if (mc == cc) n     cout <<  Friendship is magic!^^ ; n   else if (mc < cc) n     cout <<  Chris ; n   else n     cout <<  Mishka ; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n struct node { n   int val, pos; n } A[2005]; n int res[2005]; n int cmp1(node a, node b) { return a.val > b.val; } n int main() { n   A[0].val = 0x7fffffff; n   cin >> N; n   for (int i = 1; i <= N; i++) { n     scanf( %d , &A[i].val); n     A[i].pos = i; n   } n   sort(A + 1, A + 1 + N, cmp1); n   for (int i = 1; i <= N; i++) { n     if (A[i - 1].val == A[i].val) n       res[A[i].pos] = res[A[i - 1].pos]; n     else n       res[A[i].pos] = i; n   } n   for (int i = 1; i <= N; i++) printf( %d%c , res[i], i == N ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 5e5 + 10; n const long long M = 1e6 + 11; n const long long big = 1e17; n const long long hsh2 = 1964325029; n const long long mod = 1e9 + 7; n const double EPS = 1e-9; n const long long block = 1e14; n const long long shift = 2e3; n const double pi = acos(-1.0); n mt19937 bruh(chrono::steady_clock::now().time_since_epoch().count()); n long long n; n struct dsu { n   long long p[N], sz[N]; n   set<long long> comp; n   void init() { n     for (int i = 1; i <= n; i++) p[i] = i, sz[i] = 1, comp.insert(i); n   } n   long long fnd(long long a) { return (p[a] = (p[a] == a ? a : fnd(p[a]))); } n   void unt(long long a, long long b) { n     a = fnd(a), b = fnd(b); n     if (a == b) return; n     if (sz[a] < sz[b]) swap(a, b); n     comp.erase(b); n     sz[a] += sz[b]; n     p[b] = a; n   } n } rt[2]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n; n   rt[0].init(); n   rt[1].init(); n   long long m[2] = {0}; n   cin >> m[0] >> m[1]; n   long long sm[2] = {m[0], m[1]}; n   while (m[0]--) { n     long long a, b; n     cin >> a >> b; n     rt[0].unt(a, b); n   } n   while (m[1]--) { n     long long a, b; n     cin >> a >> b; n     rt[1].unt(a, b); n   } n   vector<pair<long long, long long> > ans; n   while (sm[0] < n - 1 && sm[1] < n - 1) { n     long long i = bruh() % n + 1, j = bruh() % n + 1; n     auto it = rt[0].comp.lower_bound(i), it2 = rt[1].comp.lower_bound(j); n     if (it == rt[0].comp.end()) it--; n     if (it2 == rt[1].comp.end()) it2--; n     i = *it, j = *it2; n     if (rt[0].fnd(i) != rt[0].fnd(j) && rt[1].fnd(i) != rt[1].fnd(j)) { n       sm[0]++, sm[1]++; n       rt[0].unt(i, j); n       rt[1].unt(i, j); n       ans.push_back(make_pair(i, j)); n     } n   } n   cout << ans.size() <<   n ; n   for (auto u : ans) cout << u.first <<     << u.second <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, arr[100005]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   int cur = min(2, n); n   int best = min(2, n); n   for (int i = 2; i < n; i++) { n     if (arr[i - 2] + arr[i - 1] == arr[i]) n       cur++; n     else n       cur = 2; n     best = max(best, cur); n   } n   cout << best << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, l; n   cin >> n; n   string s; n   for (int i = 0; i < n; i++) { n     cin >> l; n     cin >> s; n     int ans = 0; n     bool lol = false; n     int tmp = 0; n     for (int j = 0; j < l; j++) { n       if (s[j] ==  A ) { n         lol = true; n         if (tmp > ans) ans = tmp; n         tmp = 0; n       } else { n         if (lol) { n           tmp++; n         } n       } n     } n     if (tmp > ans) ans = tmp; n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, z; n   int sol; n   int s[100009]; n   cin >> x >> y; n   s[0] = 0; n   for (int i = 1; i < x; i++) { n     cin >> z; n     s[i] = z + s[i - 1]; n   } n   sol = s[y]; n   for (int i = y + 1; i < x; i++) { n     sol = min(sol, s[i] - s[i - y]); n   } n   cout << sol << endl; n } n


#include <bits/stdc++.h> n const int N = 1010; n double f[N][N]; n bool vis[N][N]; n inline double F(int m, int n) { n   if (!m) return 1. / (n + 1); n   if (!n) return 1; n   if (vis[m][n]) return f[m][n]; n   vis[m][n] = true; n   double p = 1 / (1 + (1. / ((n + 1) * F(n, m - 1)))); n   return f[m][n] = p * n / (n + 1) * (1 - F(n - 1, m)) + 1 - p; n } n int main() { n   int m, n; n   scanf( %d%d , &m, &n); n   printf( %.11f %.11f n , F(m, n), 1 - F(m, n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n int n, len, ct; n int a[maxn], tot[maxn], cur[maxn]; n long long ans; n void solve() { n   for (int i = 1; i <= n; i++) tot[i] = cur[i] = 0; n   for (int i = 1; i <= n; i++) tot[a[i]]++; n   int r = 1; n   for (int i = 1; i <= (n - 1) / 2; i++) { n     while (r <= (n - 1) / 2 && (cur[a[r]] + 1) * 2 <= tot[a[r]]) { n       cur[a[r]]++; n       r++; n     } n     ans += r - i + 1; n     if (a[i] != a[n - i + 1]) break; n     tot[a[i]] -= 2; n     cur[a[i]]--; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), tot[a[i]]++; n   int val = 0; n   for (int i = 1; i <= n; i++) n     if (tot[i] & 1) { n       if (val) { n         puts( 0 ); n         return 0; n       } n       val = i; n     } n   while (1 + len <= n && n - len >= 1 && a[1 + len] == a[n - len]) len++; n   if (len == n) { n     printf( %lld n , 1LL * n * (n + 1) / 2); n     return 0; n   } n   int l = n / 2, r = n / 2 + 1 + (n & 1); n   while (l - ct >= 1 && r + ct <= n && a[l - ct] == a[r + ct]) ct++; n   for (int i = 1; i <= n; i++) cur[i] = 0; n   for (int i = len + 1; i <= l - ct; i++) cur[a[i]]++; n   bool bad = false; n   for (int i = r + ct; i <= n - len; i++) { n     if (!cur[a[i]]) { n       bad = true; n       break; n     } n     cur[a[i]]--; n   } n   if (!bad) ans = 2LL * (len + 1) * (ct + 1); n   solve(); n   reverse(a + 1, a + 1 + n); n   solve(); n   ans -= len + 1; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b, c; n     cin >> a >> b >> c; n     int ans = 0; n     if (a >= 1) { n       a--; n       ans++; n     } n     if (b >= 1) { n       b--; n       ans++; n     } n     if (c >= 1) { n       c--; n       ans++; n     } n     if (a < b) swap(a, b); n     if (b < c) swap(b, c); n     if (a < b) swap(a, b); n     if (a >= 1 && b >= 1) { n       a--; n       b--; n       ans++; n     } n     if (a >= 1 && c >= 1) { n       a--; n       c--; n       ans++; n     } n     if (b >= 1 && c >= 1) { n       b--; n       c--; n       ans++; n     } n     if (a >= 1 && b >= 1 && c >= 1) ans++; n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n int a[N]; n long long ans[N]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   if (n == 1) { n     cout << 1 <<     << 1 <<   n ; n     cout << 0 <<   n ; n     cout << 1 <<     << 1 <<   n ; n     cout << 0 <<   n ; n     cout << 1 <<     << 1 <<   n ; n     cout << -a[1] <<   n ; n     return 0; n   } n   for (int i = 1; i <= n; i++) { n     long long x = a[i]; n     long long yu = x % (n - 1); n     long long byu = (n - 1 - yu) * n; n     ans[i] = byu + a[i]; n   } n   cout << 1 <<     << n <<   n ; n   for (int i = 1; i <= n; i++) { n     cout << ans[i] - a[i] <<    ; n   } n   cout <<   n ; n   cout << 1 <<     << 1 <<   n ; n   cout << -ans[1] <<   n ; n   cout << 2 <<     << n <<   n ; n   for (int i = 2; i <= n; i++) { n     cout << -ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1100000; n const long long M = 1000000007; n long long i, j, k, l, m, n, o, p, K, w, F[N], ni[N]; n inline long long fastmi(long long x, long long m) { n   long long ans = 1; n   while (m) { n     if (m & 1) ans = (ans * x) % M; n     x = (x * x) % M; n     m >>= 1; n   } n   return ans; n } n inline long long get(long long l, long long r) { n   long long ans; n   if ((r < 0) || (l < 0)) return 0; n   if (l > r) return 1; n   if (l == 0) return 0; n   ans = (F[r] * ni[l - 1]) % M; n   return ans; n } n inline long long Solve() { n   long long ans1, ans2, ans, cul, cant; n   F[0] = 1; n   ni[0] = 1; n   for (i = 1; i <= K; i++) { n     F[i] = (F[i - 1] * i) % M; n     ni[i] = fastmi(F[i], M - 2); n   } n   ans = 0; n   for (i = 0; i <= K - 1; i++) { n     cant = i + 1 - w + 1; n     if (cant < 0) cant = 0; n     k = K - cant; n     o = (get(K - i, K - 1) * min(w, i + 1)) % M; n     l = w - i - 2; n     if (l < 0) l = 0; n     j = w - 1 - l; n     p = (fastmi(k, l) * get(k - j + 1, k)) % M; n     cul = (o * p) % M; n     ans += cul; n     if (ans >= M) ans -= M; n   } n   ans1 = ans; n   ans = 0; n   for (i = 0; i <= K - 1; i++) { n     cant = i + 1 - (w - 1) + 1; n     if (cant < 0) cant = 0; n     k = K - cant; n     o = (get(K - i, K - 1) * min(w - 1, i + 1)) % M; n     l = w - 1 - i - 2; n     if (l < 0) l = 0; n     j = w - 1 - l; n     p = (fastmi(k, l) * get(k - j + 1, k)) % M; n     cul = (o * p) % M; n     ans += cul; n     if (ans >= M) ans -= M; n   } n   ans2 = ans1 - ans; n   if (ans2 < 0) ans2 += M; n   ans1 = ans1 + ans1; n   if (ans1 >= M) ans1 -= M; n   ans1 -= ans2; n   if (ans1 < 0) ans1 += M; n   ans = (ans1 * K) % M; n   return ans; n } n int main() { n   cin >> K >> w; n   cout << Solve() << endl; n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 300000 + 5; n const int MOD = 1000000009; n int s[N * 4], f[N], a[N], taga[N * 4], tagb[N * 4]; n int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) f = -1; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   return x * f; n } n void update(int x) { s[x] = (s[x << 1] + s[x << 1 | 1]) % MOD; } n void push(int x, int a, int b, int n) { n   taga[x] = (taga[x] + a) % MOD; n   tagb[x] = (tagb[x] + b) % MOD; n   s[x] = ((long long)f[n] * a + (long long)(f[n + 1] - 1) * b + s[x]) % MOD; n } n void pushdown(int x, int l, int r) { n   if (taga[x] > 0 || tagb[x] > 0) { n     int mid = (l + r) >> 1, a = taga[x], b = tagb[x], C = mid - l, n         A = ((long long)f[C] * a + (long long)f[C + 1] * b) % MOD, n         B = ((long long)f[C + 1] * a + (long long)f[C + 2] * b) % MOD; n     push(x << 1, a, b, mid - l + 1), push(x << 1 | 1, A, B, r - mid); n     taga[x] = tagb[x] = 0; n   } n } n void build(int x, int l, int r) { n   if (l == r) { n     s[x] = a[l]; n     return; n   } n   int mid = (l + r) >> 1; n   build(x << 1, l, mid); n   build(x << 1 | 1, mid + 1, r); n   update(x); n } n void change(int x, int l, int r, int p, int q) { n   if (l > q || r < p) return; n   if (l >= p && r <= q) { n     push(x, f[l - p + 1], f[l - p + 2], r - l + 1); n     return; n   } n   int mid = (l + r) >> 1; n   pushdown(x, l, r); n   change(x << 1, l, mid, p, q); n   change(x << 1 | 1, mid + 1, r, p, q); n   update(x); n } n int query(int x, int l, int r, int p, int q) { n   if (l > q || r < p) return 0; n   if (l >= p && r <= q) return s[x]; n   int mid = (l + r) >> 1; n   pushdown(x, l, r); n   return (query(x << 1, l, mid, p, q) + query(x << 1 | 1, mid + 1, r, p, q)) % n          MOD; n } n void prints(int x, int l, int r) { n   if (l == r) return; n   int mid = (l + r) >> 1; n   pushdown(x, l, r); n   prints(x << 1, l, mid); n   prints(x << 1 | 1, mid + 1, r); n } n int main() { n   int n = read(), m = read(); n   f[0] = 0, f[1] = 1; n   for (int i = 2; i <= n + 2; i++) f[i] = (f[i - 1] + f[i - 2]) % MOD; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   build(1, 1, n); n   for (int i = 1; i <= m; i++) { n     int t = read(), l = read(), r = read(); n     if (t == 1) n       change(1, 1, n, l, r); n     else n       printf( %d n , query(1, 1, n, l, r)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, fa[50005], ff[50005], h[50005], tot, e[50005 << 1], nxt[50005 << 1], n     mx[50005][256], cnt[50005][256], Ans[50005][256], ans, val[50005], n     dep[50005]; n template <class T> n inline void read(T &x) { n   x = 0; n   bool emm = 1; n   char c; n   for (c = getchar(); c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) emm = -1; n   for (; c >=  0  && c <=  9 ; c = getchar()) x = (x << 1) + (x << 3) + c -  0 ; n   x *= emm; n } n inline void add(int x, int y) { e[++tot] = y, nxt[tot] = h[x], h[x] = tot; } n inline void dfs(int x) { n   dep[x] = dep[fa[x]] + 1; n   int i; n   if (dep[x] >= 256) { n     int l, r, mid, s; n     for (i = x; dep[x] - dep[i] < 256; i = fa[i]) { n       mx[x][val[i] >> 8] = n           max(mx[x][val[i] >> 8], (dep[x] - dep[i] ^ val[i]) & 255); n       cnt[x][val[i] >> 8]++; n     } n     ff[x] = i; n     for (i = 1; i < 256; i++) cnt[x][i] += cnt[x][i - 1]; n     for (i = 0; i <= (n - 1) / 256; i++) { n       for (l = s = 0, r = 256, mid = r >> 1; l < r - 1; mid = l + r >> 1) { n         if ((s ^ i) < mid) { n           if (cnt[x][r - 1] > cnt[x][mid - 1]) n             s |= r - mid, l = mid; n           else n             r = mid; n         } else { n           if (cnt[x][mid - 1] > ((l) ? cnt[x][l - 1] : 0)) n             s |= r - mid, r = mid; n           else n             l = mid; n         } n       } n       Ans[x][i] = (s << 8) | mx[x][l]; n     } n   } n   for (i = h[x]; i; i = nxt[i]) n     if (e[i] != fa[x]) fa[e[i]] = x, dfs(e[i]); n } n int main() { n   read(n), read(q); n   for (int i = 1; i <= n; i++) read(val[i]); n   for (int x, y, i = 1; i < n; i++) read(x), read(y), add(x, y), add(y, x); n   for (dfs(1); q--;) { n     int u, v, dis; n     read(u), read(v); n     for (dis = ans = 0; dep[v] - dep[u] >= 256; v = ff[v], dis++) n       ans = max(ans, Ans[v][dis]); n     for (dis <<= 8; v != fa[u]; v = fa[v], dis++) ans = max(ans, dis ^ val[v]); n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:67108864 ) n const int MOD = 1000000007; n const long long INF = 3000000000000000001; n const double EPS = 1e-6; n const int HASH_POW = 29; n const double PI = acos(-1.0); n using namespace std; n double workTime() { return double(clock()) / CLOCKS_PER_SEC; } n void my_return(int code) { exit(code); } n int n, q; n char p[2][200010]; n const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; n int cur = 0, cmp[2][200010]; n void dfs(int x, int y) { n   if (p[x][y] ==  X ) return; n   cmp[x][y] = cur; n   for (int i = 0; i < 4; ++i) { n     int nx = x + dx[i], ny = y + dy[i]; n     if (0 <= nx && nx < 2 && 0 <= ny && ny < n && cmp[nx][ny] == 0) dfs(nx, ny); n   } n } n int sum[200010], ll[200010], rr[200010]; n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   mt19937 mt_rand(time(0)); n   scanf( %d %d n , &n, &q); n   for (int i = 0; i < 2; ++i) { n     for (int j = 0; j < n; ++j) p[i][j] = getchar(); n     getchar(); n   } n   for (int i = 0; i < 2; ++i) { n     for (int j = 0; j < n; ++j) { n       if (!cmp[i][j]) { n         ++cur; n         dfs(i, j); n       } n     } n   } n   int lastx = -1; n   for (int j = 0; j < n; ++j) { n     if (p[0][j] ==  X  && p[1][j] ==  . ) { n       if (lastx == 1) ++sum[j]; n       lastx = 0; n     } else if (p[0][j] ==  .  && p[1][j] ==  X ) { n       if (lastx == 0) ++sum[j]; n       lastx = 1; n     } else if (p[0][j] ==  X  && p[1][j] ==  X ) n       lastx = -1; n   } n   for (int j = 1; j < n; ++j) sum[j] += sum[j - 1]; n   ll[0] = -1; n   for (int j = 0; j < n; ++j) n     if ((p[0][j] ==  X  && p[1][j] ==  . ) || n         (p[0][j] ==  .  && p[1][j] ==  X )) n       ll[j] = j; n   for (int j = 1; j < n; ++j) n     if (!ll[j]) ll[j] = ll[j - 1]; n   rr[n - 1] = -1; n   for (int j = n - 1; j >= 0; --j) n     if ((p[0][j] ==  X  && p[1][j] ==  . ) || n         (p[0][j] ==  .  && p[1][j] ==  X )) n       rr[j] = j; n   for (int j = n - 2; j >= 0; --j) n     if (!rr[j]) rr[j] = rr[j + 1]; n   while (q--) { n     int foo, bar; n     scanf( %d %d , &foo, &bar); n     int x1, y1, x2, y2; n     x1 = (foo - 1) / n; n     y1 = (foo - 1) % n; n     x2 = (bar - 1) / n; n     y2 = (bar - 1) % n; n     if (y1 > y2) { n       swap(x1, x2); n       swap(y1, y2); n     } n     if (cmp[x1][y1] != cmp[x2][y2]) n       printf( -1 n ); n     else { n       if (rr[y1] == rr[y2]) n         printf( %d n , abs(y1 - y2) + abs(x1 - x2)); n       else { n         int ans = 0; n         int nxtL = rr[y1], nxtR = ll[y2]; n         ans += abs(nxtL - y1) + abs(nxtR - y2); n         if (p[x1][nxtL] ==  X ) ++ans; n         if (p[x2][nxtR] ==  X ) ++ans; n         ans += abs(nxtR - nxtL) + sum[nxtR]; n         if (nxtL) ans -= sum[nxtL]; n         printf( %d n , ans); n       } n     } n   } n   my_return(0); n } n


#include <bits/stdc++.h> n using namespace std; n using point_t = pair<int, int>; n int main() { n   size_t n; n   cin >> n; n   vector<point_t> obelisks(n), clues(n); n   for (point_t &p : obelisks) cin >> p.first >> p.second; n   for (point_t &p : clues) cin >> p.first >> p.second; n   point_t treasure; n   map<point_t, size_t> finds; n   for (point_t &ob : obelisks) n     for (point_t &cl : clues) { n       point_t p{ob.first + cl.first, ob.second + cl.second}; n       size_t &c{++finds[p]}; n       if (c == n) { n         cout << p.first <<     << p.second << endl; n         return 0; n       } n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, mini, c = 0; n   scanf( %d %d %d , &n, &m, &mini); n   for (int i = m; i < mini + 1; i += m) { n     if (i % n == 0) c++; n   } n   printf( %d n , c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long num[20]; n int cnt; n int main() { n   int n; n   long long k, x; n   long long ans = 0LL; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     cnt = 0; n     scanf( %I64d%I64d , &x, &k); n     long long r = x + k - 1LL; n     long long l = x; n     while (l % 4 != 0 && l <= r) num[++cnt] = l++; n     l--; n     while (r % 4 != 3 && l < r) num[++cnt] = r--; n     for (int j = 1; j <= cnt; j++) ans ^= num[j]; n   } n   if (ans == 0LL) n     printf( bolik n ); n   else n     printf( tolik n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000110; n string str[7] = { monday ,  tuesday ,   wednesday ,  thursday , n                   friday ,  saturday ,  sunday }; n int main() { n   string s, s1; n   while (cin >> s >> s1) { n     int i; n     int a, b; n     for (i = 0; i < 7; i++) { n       if (s == str[i]) a = i; n       if (s1 == str[i]) b = i; n     } n     if (b - a == 3 || b - a == 0 || b - a == -4 || b - a == 2 || b - a == -4 || n         b - a == 2 || b - a == 3 || b - a == -4 || b - a == 2 || b - a == 3 || n         b - a == -5) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h>  n using namespace std; n #define     ll              long long n #define     mp              make_pair n #define     rep(i,a,b)      for( i=a;i<b;++i) n #define     repr(i,a,b)     for( i=a;i>=b;i--) n #define     up              upper_bound n #define     lb              lower_bound n #define     t()             int test;cin>>test;while(test--) n #define     IOS             ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); n  n #define TRACE n #ifdef TRACE n #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) n     template <typename Arg1> n     void __f(const char* name, Arg1&& arg1){ n         cout << name <<   :   << arg1 << endl; n         //use cerr if u want to display at the bottom n     } n     template <typename Arg1, typename... Args> n     void __f(const char* names, Arg1&& arg1, Args&&... args){ n         const char* comma = strchr(names + 1,  , ); cout.write(names, comma - names) <<   :   << arg1<<  |  ;__f(comma+1, args...); n     } n #else n #define trace(...) n #endif n  n bool cmp(pair<ll,ll> a,pair<ll,ll> b){ n     if(a.first != b.first){ n         return a.first < b.first; n     }else{ n         return a.second < b.second; n     } n } n  n int main() n {   n     #ifndef ONLINE_JUDGE n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n     #endif n     IOS; n     ll t; n     cin >> t; n     while(t--){ n         ll n;  n         cin >> n; n         vector<string> s(n); n         for(ll i = 0;i < n;i++){ n                 cin >> s[i]; n         }  n         ll k= 0; n         for(ll i = 0;i < n;i++){ n             for(ll j = 0;j < n;j++){ n                 if (s[i][j] !=  . ){  n                     k++; n                 } n             } n         } n         bool flag = true; n         for(ll x = 0;x < 3;x++){ n             for(ll y = 0;y < 3;y++){ n                 if(x == y){ n                     continue; n                 }else{ n                     ll count = 0; n                     for(ll i = 0;i < n;i++){ n                         for(ll j= 0;j < n;j++){ n                             if(s[i][j] ==  X  && (i + j)%3 == x){ n                                 count++; n                             }else if(s[i][j] ==  O  && (i + j)%3 == y){ n                                 count++; n                             } n                         } n                     } n                     if(count <= k/3){ n                         for(ll i = 0;i < n;i++){ n                             for(ll j= 0;j < n;j++){ n                                 if(s[i][j] ==  X  && (i + j)%3 == x){ n                                     s[i][j] =  O ; n                                 }else if(s[i][j] ==  O  && (i + j)%3 == y){ n                                     s[i][j] =  X ; n                                 } n                             } n                         } n                         for(auto it : s){ n                             cout << it <<   n ; n                         } n                         flag = false; n                         break; n                     } n  n                 } n             } n             if(!flag){ n                 break; n             } n         } n     } n     return 0; n }    n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n int MM[500], FF[500]; n int main() { n   ios::sync_with_stdio(0); n   int N; n   cin >> N; n   for (int i = 0; i < N; i++) { n     char g; n     int from, to; n     cin >> g >> from >> to; n     int *v; n     if (g ==  M ) n       for (int i = from; i <= to; i++) MM[i]++; n     else n       for (int i = from; i <= to; i++) FF[i]++; n   } n   int MAX = 0; n   for (int i = 1; i < 400; i++) MAX = max(MAX, min(MM[i], FF[i]) * 2); n   cout << MAX; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int input() { n   int x = 0, f = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) f |= ch ==  0 , ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n int n, m, du[2100]; n bool Check() { n   for (int i = 1; i <= n; i++) { n     if (du[i] % 2 == 1) return 0; n   } n   printf( 1 n ); n   for (int i = 1; i < n; i++) printf( 1  ); n   printf( 1 n ); n   return 1; n } n bitset<2100> s[2100]; n void Gauss() { n   int x = 1; n   for (int j = 1; j <= n; j++) { n     for (int i = x; i <= n; i++) { n       if (s[i][j] == 1) { n         swap(s[i], s[x]); n         break; n       } n     } n     if (s[x][j] == 0) continue; n     for (int i = 1; i <= n; i++) { n       if (i == x) continue; n       if (s[i][j] == 1) s[i] ^= s[x]; n     } n     x++; n   } n } n int as[2100]; n void Solve() { n   int x, y; n   n = input(), m = input(); n   for (int i = 1; i <= n; i++) s[i].reset(); n   memset(du, 0, sizeof du); n   for (int i = 1; i <= m; i++) { n     x = input(), y = input(); n     du[x]++, du[y]++; n     s[x][y] = 1, s[y][x] = 1; n   } n   if (Check()) return; n   for (int i = 1; i <= n; i++) { n     if (du[i] % 2 == 1) s[i][i] = 1, s[i][n + 1] = 1; n   } n   Gauss(); n   x = 1; n   for (int j = 1; j <= n; j++) { n     if (s[x][j] == 0) { n       as[j] = 1; n       continue; n     } n     as[j] = s[x][n + 1] + 1; n     x++; n   } n   printf( 2 n ); n   for (int i = 1; i < n; i++) printf( %d  , as[i]); n   printf( %d n , as[n]); n } n int main() { n   int T; n   T = input(); n   while (T--) { n     Solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int ans[100000 + 1]; n void reverseAns(int l, int r) { n   if (l > r) return; n   int mid = (l + r) >> 1; n   for (int i = 1; i <= mid - l + 1; ++i) { n     swap(ans[i + l - 1], ans[r - i + 1]); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     ans[i] = i; n   } n   int block = round(sqrt(n)); n   int tot = n / block; n   for (int i = 1; i <= tot; ++i) { n     reverseAns((i - 1) * block + 1, i * block); n   } n   reverseAns(tot * block + 1, n); n   for (int i = 1; i <= n; ++i) { n     cout << ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, j, x[300000 + 10], a, b, sum, t, o[300000 + 10], p[300000 + 10], n     h = 1; n int main() { n   cin >> n >> q; n   for (int i = 0; i < q; i++) { n     cin >> a >> b; n     if (a == 1) { n       j++; n       x[j] = b; n       o[b]++; n       p[b]++; n       sum++; n       cout << sum << endl; n       continue; n     } n     if (a == 2) { n       sum -= o[b]; n       o[b] = 0; n       cout << sum << endl; n       continue; n     } n     if (a == 3) { n       for (h; h <= b; h++) { n         int n1 = x[h]; n         if (p[n1] > 0) p[n1]--; n         if (p[n1] < o[n1]) { n           sum--; n           o[n1]--; n         } n       } n       cout << sum << endl; n       continue; n     } n   } n } n


#include <bits/stdc++.h> n long long mod(long long x) { n   if (x < 0) return -x; n   return x; n } n long long expo(long long x, long long y) { n   long long res = 1; n   while (y > 0) { n     if (y % 2 == 1) res = (res * x); n     y = y >> 1; n     x = x * x; n   } n   return res; n } n using namespace std; n const int MAXN = 500005; n vector<long long> v; n long long a[1000000], b[1000000]; n int main() { n   ios_base::sync_with_stdio(false); n   long long n, t, m, k, x, y; n   cin >> n; n   string s; n   cin >> s; n   x = 0; n   y = 0; n   t = 0; n   if (n % 2 != 0) { n     cout <<  :( ; n     return 0; n   } n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  ) ) { n       y++; n     } n     if (s[i] ==  ( ) { n       x++; n     } n   } n   k = n / 2 - x; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  ? ) { n       s[i] =  ( ; n       k--; n     } n     if (k <= 0) break; n   } n   k = n / 2 - y; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  ? ) { n       s[i] =  ) ; n       k--; n     } n     if (k <= 0) break; n   } n   t = 0; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  ) ) { n       t--; n     } n     if (s[i] ==  ( ) { n       t++; n     } n     if (t <= 0 && i != n - 1) { n       cout <<  :( ; n       return 0; n     } n     if (t < 0) { n       cout <<  :( ; n       return 0; n     } n   } n   if (t != 0) { n     cout <<  :( ; n     return 0; n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[4] = {0, 0, -1, 1}; n const int dy[4] = {-1, 1, 0, 0}; n int main() { n   int i, j, k, n; n   long int mx, mn, sm = 0; n   int t; n   cin >> t; n   while (t--) { n     int f = 0; n     cin >> n; n     vector<pair<int, int>> v(n); n     for (i = 0; i < n; i++) { n       int a, b; n       cin >> a >> b; n       v[i] = (make_pair(a, b)); n     } n     if (v[0].first < v[0].second) f = 1; n     for (i = 1; i < n; i++) { n       int x = v[i].first - v[i - 1].first; n       int y = v[i].second - v[i - 1].second; n       if (x < 0 || y < 0 || (v[i].first < v[i].second) || n           (v[i - 1].first < v[i - 1].second) || y > x) { n         f = 1; n         break; n       } n     } n     if (f) n       cout <<  NO  n            <<   n ; n     else { n       cout <<  YES  n            <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n const int maxm = 100005; n typedef int arr[maxn]; n arr head, dfn, low, col, st; n int top, depth, color, tot, cnt; n pair<int, int> e[maxm * 2], bridge[maxn]; n int euler[maxn * 2], d[maxn * 2][30], pos; n void add(int x, int y) { n   e[tot] = make_pair(head[x], y); n   head[x] = tot++; n } n void tarjan(int u, int f) { n   dfn[u] = low[u] = ++depth; n   st[++top] = u; n   for (int i = head[u]; i != -1; i = e[i].first) { n     int v = e[i].second; n     if (v == f) continue; n     if (!dfn[v]) { n       tarjan(v, u); n       low[u] = min(low[v], low[u]); n     } else n       low[u] = min(low[u], dfn[v]); n     if (low[v] > dfn[u]) bridge[cnt++] = make_pair(u, v); n   } n   if (dfn[u] == low[u]) { n     int x; n     color++; n     do { n       col[x = st[top--]] = color; n     } while (x != u); n   } n } n void dfs(int u, int f) { n   euler[++pos] = u; n   low[u] = pos; n   for (int i = head[u]; i != -1; i = e[i].first) { n     int v = e[i].second; n     if (v == f) continue; n     dfn[v] = dfn[u] + 1; n     dfs(v, u); n     euler[++pos] = u; n   } n } n void RMQ_init() { n   for (int i = 1; i <= pos; i++) d[i][0] = i; n   for (int j = 1; (1 << j) <= pos; j++) { n     for (int i = 1; i + j - 1 <= pos; i++) { n       if (dfn[euler[d[i][j - 1]]] < dfn[euler[d[i + (1 << (j - 1))][j - 1]]]) { n         d[i][j] = d[i][j - 1]; n       } else n         d[i][j] = d[i + (1 << (j - 1))][j - 1]; n     } n   } n } n int RMQ(int L, int R) { n   int k = 0; n   if (L > R) swap(L, R); n   while ((1 << (k + 1)) <= R - L + 1) k++; n   return dfn[euler[d[L][k]]] < dfn[euler[d[R - (1 << k) + 1][k]]] n              ? d[L][k] n              : d[R - (1 << k) + 1][k]; n } n int main() { n   for (int n, m; scanf( %d%d , &n, &m) != EOF;) { n     depth = color = tot = top = cnt = 0; n     for (int i = 1; i <= n; i++) { n       head[i] = -1, dfn[i] = 0; n     } n     for (int i = 1, x, y; i <= m; i++) { n       scanf( %d%d , &x, &y); n       add(x, y); n       add(y, x); n     } n     tarjan(1, -1); n     tot = 0; n     for (int i = 0; i <= color; i++) { n       head[i] = -1; n       dfn[i] = 0; n     } n     for (int i = 0; i < cnt; i++) { n       add(col[bridge[i].first], col[bridge[i].second]); n       add(col[bridge[i].second], col[bridge[i].first]); n     } n     pos = 0; n     dfn[1] = 0; n     dfs(1, 0); n     RMQ_init(); n     int Q; n     scanf( %d , &Q); n     for (int i = 1, x, y; i <= Q; i++) { n       scanf( %d%d , &x, &y); n       int ance = RMQ(low[col[x]], low[col[y]]); n       ance = euler[ance]; n       printf( %d n , dfn[col[x]] + dfn[col[y]] - 2 * dfn[ance]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int zuo = 0, you = 0; n   for (int i = 0; i < n; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     zuo += x; n     you += y; n   } n   printf( %d n , min(n - zuo, zuo) + min(n - you, you)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxk = 20; n const int maxn = 3e5 + 17; n int N, Q; n int sub[maxn], ans[maxn]; n vector<int> graph[maxn]; n set<pair<int, int> > DFS(int u) { n   sub[u] = 1; n   set<pair<int, int> > cur; n   for (int v : graph[u]) { n     set<pair<int, int> > next = DFS(v); n     if (next.size() >= cur.size()) swap(cur, next); n     for (auto x : next) cur.insert(x); n     sub[u] += sub[v]; n   } n   cur.insert({sub[u], u}); n   ans[u] = cur.lower_bound({sub[u] + 1 >> 1, 0})->second; n   return cur; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> N >> Q; n   for (int i = 2; i <= N; i++) { n     int x; n     cin >> x; n     graph[x].push_back(i); n   } n   DFS(1); n   while (Q--) { n     int x; n     cin >> x; n     cout << ans[x] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long rd() { n   long long x = 0; n   int ch = getchar(), f = 1; n   while (!isdigit(ch) && (ch !=  - ) && (ch != EOF)) ch = getchar(); n   if (ch ==  - ) { n     f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 1) + (x << 3) + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline void rt(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) n     rt(x / 10), putchar(x % 10 +  0 ); n   else n     putchar(x +  0 ); n } n const int maxn = 130; n int N; n double a[maxn][maxn], b[maxn][maxn], c[maxn][maxn], p[maxn]; n void pow(int x) { n   if (x == 1) { n     for (int i = 0; i <= (int)N; i++) n       for (int j = 0; j <= (int)N; j++) c[i][j] = a[i][j]; n     return; n   } n   pow(x >> 1); n   memset(b, 0, sizeof b); n   for (int k = 0; k <= (int)N; k++) n     for (int i = 0; i <= (int)N; i++) n       for (int j = 0; j <= (int)N; j++) b[i][j] += c[i][k] * c[k][j]; n   if (x & 1) { n     memset(c, 0, sizeof c); n     for (int k = 0; k <= (int)N; k++) n       for (int i = 0; i <= (int)N; i++) n         for (int j = 0; j <= (int)N; j++) c[i][j] += b[i][k] * a[k][j]; n   } else n     memcpy(c, b, sizeof c); n } n int n, x; n int main() { n   n = rd(), x = rd(); n   for (int i = 0; i <= (int)x; i++) scanf( %lf , &p[i]); n   for (N = 1; N <= x; N <<= 1) n     ; n   N--; n   for (int i = 0; i <= (int)N; i++) n     for (int j = 0; j <= (int)x; j++) a[i][i ^ j] = p[j]; n   pow(n); n   double ans = 0; n   printf( %.8lf n , 1 - c[0][0]); n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( unroll-loops ) n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( O3 ) n const long long inf = 1e18; n long long i, j, k, m, n, qq, t, it, ma = -inf, mi = inf, x, y, col, n                                     md = 998244353; n string cmp(string second) { n   vector<long long> p(second.size(), 0); n   for (i = 1; i < second.size(); i++) { n     j = p[i - 1]; n     while (j > 0 && second[i] != second[j]) j = p[j - 1]; n     if (second[i] == second[j]) j++; n     p[i] = j; n   } n   return second.substr(0, j); n } n int32_t main() { n   cin >> qq; n   while (qq--) { n     string second; n     cin >> second; n     n = second.size(); n     k = 0; n     while (n - 1 - k >= 0 && k < n / 2 && second[k] == second[n - 1 - k]) k++; n     if (k == (n + 1) / 2) { n       cout << second <<   n ; n       continue; n     } n     for (i = 0; i < k; i++) cout << second[i]; n     string t = second.substr(k, max(0ll, n - k - k)); n     string rt = t; n     reverse(rt.begin(), rt.end()); n     string ans1 = cmp(t +  %  + rt); n     string ans2 = cmp(rt +  %  + t); n     if (ans1.size() > ans2.size()) n       cout << ans1; n     else n       cout << ans2; n     for (i = k - 1; i > -1; i--) cout << second[i]; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int rc = 0, uc = 0, c = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  R ) n       rc++; n     else n       uc++; n     if (rc == uc && s[i] == s[i + 1]) c++; n   } n   cout << c <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = n     3.141592653589793238462643383279502884197169399375105820974944; n long long dist(long long a, long long b, long long c, long long d) { n   return abs(a - c) + abs(b - d); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long a, b, c, d; n   cin >> a >> b >> c >> d; n   long long ans = 1000000000; n   for (long long i = c - 1; i <= c + 1; i++) { n     for (long long j = d - 1; j <= d + 1; j++) { n       if (i != c or j != d) { n         ans = min(ans, dist(a, b, i, j)); n       } n     } n   } n   cout << 2 * ans + 8 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, tb[100], w1, w2, l = 1; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n / 2; i++) scanf( %d , &tb[i]); n   sort(tb, tb + n / 2); n   for (int i = 0; i < n / 2; i++) { n     w1 += abs(tb[i] - l); n     l += 2; n   } n   l = 2; n   for (int i = 0; i < n / 2; i++) { n     w2 += abs(tb[i] - l); n     l += 2; n   } n   printf( %d , min(w1, w2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const long long infl = 1LL << 60; n mt19937 rnd(time(0)); n inline int myrand(int l, int r) { return rnd() % (r - l + 1) + l; } n inline int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + c -  0 , c = getchar(); n   } n   return x * f; n } n const int maxn = 3e5 + 10; n vector<int> linker[maxn]; n int n, m, q[maxn], dis[maxn], pre[maxn]; n bool mark[maxn], vis[maxn]; n vector<int> v; n void dfs(int x) { n   vis[x] = 1, v.push_back(x); n   for (int cur : linker[x]) n     if (!vis[cur]) dfs(cur); n } n void print() { n   vector<int> ans(0); n   int x = n; n   while (x) ans.push_back(x), x = pre[x]; n   reverse(ans.begin(), ans.end()); n   for (int cur : ans) printf( %d  , cur); n   puts(  ); n } n int main() { n   n = read(), m = read(); n   for (int i = 1; i <= m; i++) { n     int u = read(), v = read(); n     linker[u].push_back(v); n     linker[v].push_back(u); n   } n   int head = 1, tail = 0; n   memset(dis, 0x3f, sizeof dis); n   q[++tail] = 1, dis[1] = 0; n   while (head <= tail) { n     int x = q[head++]; n     for (int cur : linker[x]) n       if (dis[cur] == inf) dis[cur] = dis[x] + 1, q[++tail] = cur, pre[cur] = x; n   } n   if (dis[n] <= 4) return printf( %d n , dis[n]), print(), 0; n   for (int cur : linker[1]) mark[cur] = 1; n   mark[1] = 1; n   for (int cur : linker[1]) { n     for (int to : linker[cur]) n       if (!mark[to]) { n         printf( %d n%d %d %d %d %d n , 4, 1, cur, to, 1, n); n         return 0; n       } n   } n   vis[1] = 1; n   for (int x : linker[1]) n     if (!vis[x]) { n       dfs(x); n       for (int y : v) { n         if (mark[y] && linker[y].size() - 1 < v.size() - 1) { n           printf( %d n , min(dis[n], 5)); n           if (dis[n] < 5) n             print(); n           else { n             memset(mark, 0, sizeof mark); n             for (int cur : linker[y]) mark[cur] = 1; n             mark[y] = 1; n             for (int cur : linker[y]) n               if (cur != 1) n                 for (int foo : linker[cur]) n                   if (!mark[foo]) { n                     printf( %d %d %d %d %d %d n , 1, y, cur, foo, y, n); n                     return 0; n                   } n           } n           return 0; n         } n       } n       v.clear(); n     } n   if (dis[n] == inf) n     return puts( -1 ), 0; n   else { n     printf( %d n , dis[n]); n     print(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double v = 1.41421356237; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int t = 1; n   cin >> t; n   while (t--) { n     long long int x1, y1, z1, x2, y2, z2; n     cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2; n     long long int m, sum = 0; n     m = min(z1, y2); n     sum += (2 * m); n     m = min(x1, z2); n     x1 -= m; n     z2 -= m; n     m = min(y1, x2); n     y1 -= m; n     x2 -= m; n     sum -= (2 * min(y1, z2)); n     cout << sum << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 50005; n const unsigned long long MOD = 1e8 + 7; n const unsigned long long MAX = 1e15; n int X[N], Y[N], T[N], K[N]; n int n; n double P[N], dp[N], ans = 0; n bool cmp(int a, int b) { return T[a] < T[b]; } n double dist(int i, int j) { n   double ans = sqrt((X[K[i]] - X[K[j]]) * (X[K[i]] - X[K[j]]) + n                     (Y[K[i]] - Y[K[j]]) * (Y[K[i]] - Y[K[j]])); n   return ans; n } n void work() { n   for (int i = 1; i <= n; i++) { n     dp[i] = P[K[i]]; n     for (int j = 1; j < i; j++) n       if (dist(i, j) + T[K[j]] <= T[K[i]]) dp[i] = max(dp[j] + P[K[i]], dp[i]); n     ans = max(ans, dp[i]); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d%lf , &X[i], &Y[i], &T[i], &P[i]); n     K[i] = i; n   } n   sort(K + 1, K + n + 1, cmp); n   work(); n   printf( %.7f n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, a[10010]; n bool vs[3000][3000]; n int main() { n   cin >> n >> k; n   for (int i = n + 1; i <= n + k; i++) { n     a[i] = i - n; n   } n   for (int i = 1; i <= n; i++) a[i] = i; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= i + k; j++) { n       if (!vs[a[i]][a[j]] && !vs[a[j]][a[i]]) { n         vs[a[i]][a[j]] = true; n         vs[a[j]][a[i]] = true; n       } else { n         cout << -1; n         exit(0); n       } n     } n   } n   if (n == 1) { n     cout << -1; n     exit(0); n   } n   cout << n * k <<   n ; n   for (int i = 1; i <= n; i++) n     for (int j = i + 1; j <= i + k; j++) cout << a[i] <<     << a[j] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, t; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> t; n   while (t--) { n     cin >> n; n     long long int a[n], x = 0; n     vector<long long int> ans; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       if (i == 0) { n         ans.push_back(a[i]); n         continue; n       } n       long long int last = ans[ans.size() - 1]; n       if ((a[i] > 0 && last < 0) || (last > 0 && a[i] < 0)) n         ans.push_back(a[i]); n       else { n         ans[ans.size() - 1] = max(last, a[i]); n       } n     } n     long long int sum = 0; n     for (int i = 0; i < ans.size(); i++) { n       sum += ans[i]; n     } n     cout << sum << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool bin(unsigned long long a, unsigned long long b) { return a > b; } n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   long inp; n   queue<long> seg; n   map<long, long> cnt; n   set<long> once; n   for (int y = 0; y < k; y++) { n     scanf( %ld , &inp); n     seg.push(inp); n     cnt[inp]++; n     if (cnt[inp] == 1) n       once.insert(inp); n     else if (cnt[inp] > 1) n       once.erase(inp); n   } n   if (!once.empty()) n     printf( %ld n , (*once.rbegin())); n   else n     puts( Nothing ); n   for (int y = 0; y < n - k; y++) { n     if (cnt[seg.front()] == 1) { n       once.erase(seg.front()); n       cnt.erase(seg.front()); n     } else n       cnt[seg.front()]--; n     if (cnt[seg.front()] == 1) once.insert(seg.front()); n     seg.pop(); n     scanf( %ld , &inp); n     seg.push(inp); n     cnt[inp]++; n     if (cnt[inp] == 1) n       once.insert(inp); n     else n       once.erase(inp); n     if (!once.empty()) n       printf( %ld n , (*once.rbegin())); n     else n       puts( Nothing ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, in[200005], a[200005], sta[200005], top, now, root, ans, flag; n bool vis[200005], mark[200005]; n int read() { n   int x, f = 1; n   char ch; n   while (!(isdigit(ch = getchar()))) ch ==  -  ? f = -1 : f; n   x = ch - 48; n   while ((isdigit(ch = getchar()))) x = x * 10 + ch - 48; n   return f * x; n } n void Topology() { n   while (now < top) { n     now++; n     int tmp = sta[now]; n     vis[tmp] = 1; n     in[a[tmp]]--; n     if (!in[a[tmp]]) sta[++top] = a[tmp]; n   } n } n void dfs(int u) { n   vis[u] = 1; n   if (!vis[a[u]]) dfs(a[u]); n } n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     in[a[i]]++; n     if (a[i] == i) { n       flag = 1; n       root = i; n     } n   } n   for (int i = 1; i <= n; i++) n     if (!in[i]) sta[++top] = i; n   Topology(); n   for (int i = 1; i <= n; i++) { n     if (!vis[i]) { n       if (!root) root = i; n       ans++; n       dfs(a[i]); n       a[i] = root; n       mark[i] = 1; n     } n   } n   printf( %d n , ans - flag); n   for (int i = 1; i <= n; i++) { n     if (mark[i]) a[i] = root; n     printf( %d  , a[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int u[5005][10005]; n int n, m, a[5005], b[5005] = {1}, c[5005] = {1}, d[5005] = {1}, e[5005]; n int expmod(int a, int b, int mod) { n   int res = 1; n   for (; b; b >>= 1) { n     if (b & 1) res = 1ll * res * a % mod; n     a = 1ll * a * a % mod; n   } n   return res; n } n int dunc(int k, int e) { n   if (k + e > n) return 0; n   return 1ll * c[k + e - 1] * d[k - 1] % mod; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) n     scanf( %d , &a[i]), b[i] = expmod(a[i], mod - 2, mod); n   for (int i = 1; i < n; i++) c[i] = 1ll * c[i - 1] * a[i] % mod; n   for (int i = 1; i < n; i++) d[i] = 1ll * d[i - 1] * b[i] % mod; n   int m = 2 * n - 2; n   for (int i = 2; i <= n; i++) { n     u[i][1] = 1; n     for (int j = 2; j <= m; j++) { n       u[i][j] = (u[i - 1][j - 1] + n                  1ll * dunc(i - 1, j - 1) * (a[i - 1] - 1) % mod * b[i - 1]) % n                 mod; n     } n   } n   int ans; n   for (int i = 1; i <= m; i++) { n     ans = 0; n     for (int j = 1; j <= n; j++) { n       ans = (ans + 1ll * c[j - 1] * (u[j][i] + dunc(j, i))) % mod; n     } n     if (ans & 1) n       printf( %d n , ans + mod >> 1); n     else n       printf( %d n , ans >> 1); n   } n } n


#include <bits/stdc++.h> n #include <sys/types.h> n #include <stdio.h> n #include <unistd.h> n #define _USE_MATH_DEFINES n #define ll long long int n #ifndef ONLINE_JUDGE n #define debug(x...) cerr <<  [  << #x <<  ] = [ ; _print(x) n #else n #define debug(x...) n #endif n using namespace std; n //mp.reserve(1024); n //mp.max_load_factor(0.25); n void __print(int x) {cerr << x;} n void __print(long x) {cerr << x;} n void __print(long long x) {cerr << x;} n void __print(unsigned x) {cerr << x;} n void __print(unsigned long x) {cerr << x;} n void __print(unsigned long long x) {cerr << x;} n void __print(float x) {cerr << x;} n void __print(double x) {cerr << x;} n void __print(long double x) {cerr << x;} n void __print(char x) {cerr <<      << x <<     ;} n void __print(const char *x) {cerr <<     << x <<    ;} n void __print(const string &x) {cerr <<     << x <<    ;} n void __print(bool x) {cerr << (x ?  true  :  false );} n   n template<typename T, typename V> n void __print(const pair<T, V> &x) {cerr <<  { ; __print(x.first); cerr <<  , ; __print(x.second); cerr <<  } ;} n template<typename T> n void __print(const T &x) {int f = 0; cerr <<  { ; for (auto &i: x) cerr << (f++ ?  ,  :   ), __print(i); cerr <<  } ;} n void _print() {cerr <<  ] n ;} n template <typename T, typename... V> n void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr <<  ,  ; _print(v...);} n vector<ll> v(200001); n ll dp[200001]; n ll cal(ll x, ll n){ n  if(x>=n) n   return 0; n  ll now = v[x]; n  if(dp[x]!=-1) n   return dp[x]; n  ll val = now; n  if(x+v[x]<n){ n   val += cal(x+v[x], n); n  } n  dp[x] = val; n  return dp[x]; n   n } n int main() n { n  ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cout.tie(NULL); n     //freopen( input.txt ,  r , stdin); n     //freopen( output1.txt ,  w , stdout); n  ll t; n  cin>>t; n  while(t--){ n   ll n, ans = INT_MIN; n   cin>>n; n   memset(dp, -1, sizeof dp); n   for(ll i=0;i<n;i++){ n    cin>>v[i]; n   } n   for(ll i=0;i<n;i++){ n    ans = max(ans ,cal(i, n)); n   } n   cout<<ans<<endl; n  } n  return 0; n } n  n  n  n  n  n  n  n  n  n  n


#include <bits/stdc++.h> n using namespace std; n void err() { cout <<   n ; } n template <class T, class... Ts> n void err(const T& arg, const Ts&... args) { n   cout << arg <<    ; n   err(args...); n } n inline int read() { n   int s = 0, w = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) s = s * 10 + ch -  0 , ch = getchar(); n   return s * w; n } n void run() { n   int n = read(); n   long long sum = 0; n   long long ans = 0; n   while (n--) { n     int a = read(); n     sum += a; n     ans = ((ans) < (sum) ? (ans) : (sum)); n   } n   cout << -ans <<   n ; n } n int main() { n   int _T = read(); n   while (_T--) run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (3e5) + 10, mod = 1e9 + 7; n int n, a[maxn]; n map<int, int> mark; n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int ans = 0; n   int num = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] > num) return cout << i + 1 << endl, 0; n     if (a[i] == num) num++; n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int f[200005]; n int main() { n   string str; n   cin >> str; n   f[0] = 1; n   for (int i = 1; i < str.length(); i++) { n     if (str[i] == str[i - 1]) n       f[i] = f[i - 1] + 1; n     else n       f[i] = 1; n   } n   int last = 0, plast = 0; n   cout << str[0]; n   for (int i = 1; i < str.length(); i++) { n     if (f[i] == 1) { n       cout << str[i]; n       plast = last; n       last = i; n     } n     if (f[i] == 2 && f[plast] == 1) { n       cout << str[i]; n       plast = last; n       last = i; n     } n   } n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n void FastIO() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.precision(20); n } n int main() { n   FastIO(); n   int N; n   int A[200][200]; n   cin >> N; n   int Count = 0; n   int Cars[1000] = {0}; n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < N; j++) { n       cin >> A[i][j]; n       if (A[i][j] == 0) { n         if (Cars[i + 1] == 0) Cars[i + 1] = 100; n         if (Cars[j + 1] == 0) Cars[j + 1] = 100; n       } else if (A[i][j] == 1) { n         Cars[i + 1] = -100; n       } else if (A[i][j] == 2) n         Cars[j + 1] = -100; n       else if (A[i][j] == 3) { n         Cars[i + 1] = -100; n         Cars[j + 1] = -100; n       } n     } n   } n   for (int i = 1; i <= N; i++) { n     if (Cars[i] != -100) { n       Count++; n     } n   } n   cout << Count << endl; n   for (int i = 1; i <= N; i++) { n     if (Cars[i] != -100) cout << i <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fastio() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n } n int main() { n   fastio(); n   long long int n; n   cin >> n; n   long long int ans = 0; n   long long int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (long long int i = 0; i < n; i += 2) { n     long long int sum = 0; n     for (long long int j = i + 1; j < n; j += 2) { n       long long int x = a[j] - sum; n       if (x < 0) { n       } else { n         ans += min(a[i], x); n         if (sum) ans++; n       } n       sum -= a[j]; n       if (sum < 0) { n         if (abs(sum) > a[i]) n           break; n         else { n           a[i] += sum; n           sum = 0; n         } n       } n       if (j + 1 < n) sum += a[j + 1]; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long a, long long b) { n   long long ans = 1; n   while (b > 0) { n     if (b & 1) { n       ans = (ans * a) % 1000000007; n     } n     a = (a * a) % 1000000007; n     b >>= 1; n   } n   return ans; n } n bool prime(long long n) { n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) return false; n   } n   return true; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long i = 0; n   long long x = 1; n   bool b = true; n   vector<long long> v; n   while (i < n) { n     if (a[i] == x) { n       v.push_back(2001 + i); n       b = false; n       x++; n       i++; n     } else n       i++; n   } n   cout << v.size() << endl; n   for (long long i = 0; i < v.size(); i++) cout << v[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 998244353; n long long int hmaxii = 0; n vector<long long int> values[2 * 100000 + 1]; n long long int MAXN = 2 * 100000 + 1; n long long int spf[2 * 100000 + 1]; n long long int diff, x, y, z; n long long int flag = 1; n long long int price[1001][1001]; n long long int dp[2000][2000]; n long long int ans = 0; n long long int gcd(long long int a, long long int b) { n   return b ? gcd(b, a % b) : a; n } n long long int power(long long int a, long long int b) { n   if (b == 0) { n     return 1; n   } n   long long int temp = power(a, b / 2); n   temp = temp * temp; n } n void solution() { n   int n; n   cin >> n; n   int arr[n + 1]; n   int brr[n + 1]; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     arr[x] = i; n   } n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     brr[x] = i; n   } n   int answer = 0; n   map<int, int> values; n   for (int i = 1; i <= n; i++) { n     values[(brr[i] - arr[i] + n) % n]++; n     answer = max(answer, values[(brr[i] - arr[i] + n) % n]); n   } n   cout << answer << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   t = 1; n   while (t--) { n     solution(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s; n   while (cin >> n >> s) { n     int ans = s; n     for (int i = 0; i < n; ++i) { n       int f, t; n       cin >> f >> t; n       if (ans < f + t) { n         ans = f + t; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int m[6]; n int w[6]; n int x[6] = {0, 500, 1000, 1500, 2000, 2500}; n double max(int i) { n   double a = 0.3 * x[i]; n   double b = (1 - m[i] * 1.0 / 250) * x[i] - 1.0 * 50 * w[i]; n   if (a > b) return a; n   return b; n } n int main() { n   int i, u, v; n   while (~scanf( %d%d%d%d%d , &m[1], &m[2], &m[3], &m[4], &m[5])) { n     double sum = 0; n     for (i = 1; i <= 5; i++) { n       scanf( %d , &w[i]); n       sum += max(i); n     } n     scanf( %d%d , &u, &v); n     sum += 100 * u; n     sum -= 50 * v; n     printf( %.0lf n , sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long inf = 4e18; n const long double pai = acos(-1); n int n, q, ans; n set<pair<pair<int, int>, int> > s; n int main() { n   scanf( %d%d , &n, &q); n   ans = n; n   s.insert({{1, n}, 1}); n   while (q--) { n     int l, r, x; n     scanf( %d%d%d , &l, &r, &x), x--; n     auto it = s.lower_bound({{l, -1}, -1}); n     if (it == s.end() || it->first.first > l) it--; n     vector<pair<pair<int, int>, int> > add; n     while (it != s.end()) { n       int L = it->first.first, R = it->first.second, old = it->second; n       if (L < l) add.push_back({{L, l - 1}, old}); n       if (R > r) add.push_back({{r + 1, R}, old}); n       it = s.erase(it); n       ans -= (R - L + 1) * old; n       if (it == s.end() || it->first.first > r) break; n     } n     for (auto u : add) { n       int L = u.first.first, R = u.first.second, old = u.second; n       ans += (R - L + 1) * old; n       s.insert(u); n     } n     s.insert({{l, r}, x}); n     ans += (r - l + 1) * x; n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d, l; n   int ans[101]; n   int s = 0; n   cin >> n >> d >> l; n   for (int i = 0; i < n; ++i) { n     ans[i] = (i & 1) ? -1 : 1; n     s += ans[i]; n   } n   bool sw = false; n   while (true) { n     if (s == d) break; n     sw = false; n     if (s < d) { n       for (int i = 0; i < n; ++i) { n         if (ans[i] < l and ans[i] > 0) { n           ans[i]++; n           s++; n           sw = true; n           break; n         } n       } n     } else if (s > d) { n       for (int i = 0; i < n; ++i) { n         if (ans[i] < 0 and ans[i] > -l) { n           ans[i]--; n           s--; n           sw = true; n           break; n         } n       } n     } n     if (!sw) return cout << -1, 0; n   } n   for (int i = 0; i < n; ++i) cout << abs(ans[i]) <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fp(long long b, long long p) { n   long long res = 1; n   while (p) { n     if (p % 2) res = (res * b) % 1000000007; n     b = (b * b) % 1000000007; n     p /= 2; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, m, k; n   cin >> n >> m >> k; n   if (k == -1 && n % 2 != m % 2) n     return cout << 0, 0; n   else n     cout << fp(fp(2, (n - 1)), (m - 1)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n char b_[N], n_[N]; n long long mod, b, n; n int prime[N]; n inline void get_phi() { n   int x = mod; n   int sx = sqrt(mod); n   for (int i = 2; i <= sx; ++i) { n     if (x == 1) break; n     if (x % i) continue; n     while (x % i == 0) x /= i; n     prime[++prime[0]] = i; n   } n   if (!prime[0] || x != 1) prime[++prime[0]] = x; n } n inline long long gcd(long long a, long long b) { n   return (b == 0 ? a : gcd(b, a % b)); n } n inline long long power(long long a, long long p) { n   long long ans = 1, base = a; n   while (p) { n     if (p & 1ll) (ans = ans * base) %= mod; n     (base = base * base) %= mod; n     p >>= 1ll; n   } n   return ans; n } n int main() { n   scanf( %s%s , b_ + 1, n_ + 1); n   scanf( %lld , &mod); n   int len = strlen(b_ + 1); n   b = 0ll; n   for (register int i = 1; i <= len; ++i) { n     (b = b * 10ll + b_[i] -  0 ) %= mod; n   } n   long long ans = (b - 1ll + mod) % mod; n   get_phi(); n   long long phi = mod; n   for (register int i = 1; i <= prime[0]; ++i) n     phi = phi / prime[i] * (prime[i] - 1); n   long long gcd_ = gcd(mod, b); n   len = strlen(n_ + 1); n   n = 0ll; n   bool bigger = 0; n   for (register int i = 1; i <= len; ++i) { n     n = n * 10ll + n_[i] -  0 ; n     if (n > phi) { n       bigger = 1; n       n %= phi; n     } n   } n   if (gcd_ != 1) n     (ans *= power(b, (n - 1 + phi) % phi + (bigger ? phi : 0ll)) % mod) %= mod; n   else n     (ans *= power(b, (n - 1 + phi) % phi) % mod) %= mod; n   printf( %lld n , ((ans % mod) == 0 ? mod : ans % mod)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   sort(arr, arr + n, greater<int>()); n   deque<int> d; n   d.push_front(arr[0]); n   int i; n   for (i = 1; i < n; i++) { n     if (arr[i] != arr[0]) break; n   } n   while (i < n) { n     int a = arr[i]; n     d.push_back(a); n     if (arr[i] == arr[i + 1]) d.push_front(a); n     for (; i < n; i++) { n       if (arr[i] != a) break; n     } n   } n   cout << d.size() << endl; n   while (d.size() > 0) { n     cout << d.front() <<    ; n     d.pop_front(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxc = 22; n const int maxn = (int)5e5; n int tin[maxn], tout[maxn], _t = 0, ver[maxn], n; n vector<int> g[maxn]; n int deep[maxn], maxh[1 << maxc]; n unsigned mask[maxn]; n int sz[maxn], ans[maxn]; n unsigned char s[maxn]; n int dfs_size(int v, unsigned m) { n   mask[v] = m; n   sz[v] = 1; n   ver[tin[v] = _t++] = v; n   for (auto u : g[v]) { n     deep[u] = 1 + deep[v]; n     sz[v] += dfs_size(u, m ^ (1U << s[u])); n   } n   tout[v] = _t; n   return sz[v]; n } n const int INF = (int)1e9; n int calc(int u, int v) { n   int res = maxh[mask[u]] + deep[u] - 2 * deep[v]; n   for (int i = 0; i < maxc; ++i) { n     int m = mask[u] ^ (1U << i); n     res = max(res, maxh[m] + deep[u] - 2 * deep[v]); n   } n   return res; n } n void dfs(int v, bool flag) { n   int heavy = -1; n   for (auto u : g[v]) n     if (heavy == -1 || sz[heavy] < sz[u]) heavy = u; n   for (auto u : g[v]) n     if (heavy != u) { n       dfs(u, 0); n       ans[v] = max(ans[v], ans[u]); n     } n   if (heavy != -1) { n     dfs(heavy, 1); n     ans[v] = max(ans[v], ans[heavy]); n   } n   for (auto u : g[v]) n     if (u != heavy) { n       for (int t = tin[u]; t < tout[u]; ++t) n         ans[v] = max(ans[v], calc(ver[t], v)); n       for (int t = tin[u]; t < tout[u]; ++t) n         maxh[mask[ver[t]]] = max(maxh[mask[ver[t]]], deep[ver[t]]); n     } n   ans[v] = max(ans[v], calc(v, v)); n   maxh[mask[v]] = max(maxh[mask[v]], deep[v]); n   if (!flag) n     for (int t = tin[v]; t < tout[v]; ++t) maxh[mask[ver[t]]] = -INF; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i < n; ++i) { n     int v; n     cin >> v >> s[i]; n     g[v - 1].push_back(i); n     s[i] -=  a ; n   } n   dfs_size(0, 0); n   fill(maxh, maxh + (1 << maxc), -INF); n   dfs(0, 1); n   for (int i = 0; i < n; ++i) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n bool isSubSequence(string str1, string str2, int m, int n) { n   if (m == 0) return true; n   if (n == 0) return false; n   if (str1[m - 1] == str2[n - 1]) n     return isSubSequence(str1, str2, m - 1, n - 1); n   return isSubSequence(str1, str2, m, n - 1); n } n int main() { n   fast(); n   long long int t = 1; n   cin >> t; n   while (t--) { n     long long int n, p, k; n     cin >> n >> p >> k; n     vector<long long int> v; n     long long int i, x; n     for (i = 0; i < n; i++) { n       cin >> x; n       v.push_back(x); n     } n     sort(v.begin(), v.end()); n     long long int ans = 0; n     long long int left = p; n     vector<long long int> possible; n     if (v[0] > left) { n       cout << 0 << endl; n     } else if (v[0] == left) { n       if (n >= 2 && v[0] == v[1]) { n         cout << 2 << endl; n       } else { n         cout << 1 << endl; n       } n     } else { n       ans = 1; n       left -= v[0]; n       vector<long long int> temp; n       for (i = 2; i < n; i += 2) { n         temp.push_back(v[i]); n       } n       if (temp.size() == 0) { n         if (p >= v[1]) { n           cout << 2 << endl; n           continue; n         } else { n           cout << 1 << endl; n           continue; n         } n       } else { n         if (v[n - 1] == temp[temp.size() - 1]) { n           for (i = 0; i < temp.size(); i++) { n             if (left >= temp[i]) { n               left -= temp[i]; n               ans += 2; n             } n           } n           possible.push_back(ans); n         } else { n           for (i = 0; i < temp.size(); i++) { n             if (left >= temp[i]) { n               left -= temp[i]; n               ans += 2; n             } n           } n           if (left >= v[n - 1]) { n             ans += 1; n           } n           possible.push_back(ans); n         } n       } n       ans = 0; n       left = p; n       for (i = 1; i < n; i += 2) { n         if (left >= v[i]) { n           ans += 2; n           left -= v[i]; n         } n       } n       possible.push_back(ans); n       sort(possible.begin(), possible.end()); n       cout << possible[possible.size() - 1] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int input() { n   int res = 0; n   char c =    ; n   while (c <  0  && c !=  - ) c = getchar(); n   while (c >=  0 ) res = res * 10 + (c -  0 ), c = getchar(); n   return res; n } n const int N = 1e5 + 1; n string to_str(int x) { n   string res; n   while (x) res +=  0  + x % 10, x /= 10; n   reverse(res.begin(), res.end()); n   return res; n } n string rc(string s) { n   int i = 0, x = 0, y = 0; n   for (; i < s.size(); ++i) n     if (s[i] >=  0  && s[i] <=  9 ) n       break; n     else n       y = y * 26 + s[i] -  A  + 1; n   for (; i < s.size(); ++i) x = x * 10 + (s[i] -  0 ); n   return { R  + to_str(x) +  C  + to_str(y)}; n } n string aa(string s) { n   int i = 1, x = 0, y = 0; n   for (; s[i] !=  C ; ++i) x = x * 10 + (s[i] -  0 ); n   for (++i; i < s.size(); ++i) y = y * 10 + (s[i] -  0 ); n   string r; n   while (y) r +=  A  + (y - 1) % 26, y = (y - 1) / 26; n   reverse(r.begin(), r.end()); n   return r + to_str(x); n } n int main() { n   for (int i = input(); i; --i) { n     string s; n     cin >> s; n     bool f = false, RC = false; n     for (auto i : s) { n       if (i >=  A  && i <=  Z  && f) n         RC = true; n       else if (i >=  0  && i <=  9 ) n         f = true; n     } n     if (RC) n       cout << aa(s) << endl; n     else n       cout << rc(s) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int T, n; n char Ls[400000 + 5], ps[400000 + 5]; n long long ans; n long long read() { n   long long x = 0, F = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) F = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return x * F; n } n struct AC_automaton { n   int tr[400000 + 5][26], cnt; n   int C_end[400000 + 5], fail[400000 + 5], Ans[400000 + 5], lst[400000 + 5]; n   void Clear() { n     cnt = 0; n     fail[0] = 0; n     memset(tr[0], 0, sizeof(tr[0])); n     memset(Ans, 0, sizeof(Ans)); n   } n   void Insert(char *s, int id) { n     int p = 0, len = strlen(s); n     for (int i = 0; i < len; i++) { n       int num = s[i] -  a ; n       if (!tr[p][num]) { n         tr[p][num] = ++cnt; n         C_end[cnt] = 0; n         memset(tr[cnt], 0, sizeof(tr[cnt])); n         fail[cnt] = 0; n       } n       p = tr[p][num]; n     } n     C_end[p]++; n   } n   void Build() { n     queue<int> Q; n     for (int i = 0; i < 26; i++) n       if (tr[0][i]) Q.push(tr[0][i]); n     while (!Q.empty()) { n       int k = Q.front(); n       Q.pop(); n       for (int i = 0; i < 26; i++) n         if (tr[k][i]) { n           int nxt = tr[k][i]; n           fail[nxt] = tr[fail[k]][i]; n           Q.push(nxt); n           lst[nxt] = C_end[fail[nxt]] ? fail[nxt] : lst[fail[nxt]]; n         } else n           tr[k][i] = tr[fail[k]][i]; n     } n   } n   void Query(char *s) { n     int p = 0; n     int len = strlen(s); n     for (int i = 0; i < len; i++) { n       if (s[i] <  a  || s[i] >  z ) { n         p = 0; n         continue; n       } n       p = tr[p][s[i] -  a ]; n       for (int j = p; j; j = lst[j]) Ans[i] += C_end[j]; n     } n   } n } ac, ac2; n int main() { n   ac.Clear(); n   ac2.Clear(); n   scanf( %s , Ls); n   n = read(); n   int L; n   for (int i = 1; i <= n; i++) { n     scanf( %s , ps); n     ac.Insert(ps, i); n     L = strlen(ps); n     reverse(ps, ps + L); n     ac2.Insert(ps, i); n   } n   int len = strlen(Ls); n   ac.Build(), ac2.Build(); n   ac.Query(Ls); n   reverse(Ls, Ls + len); n   ac2.Query(Ls); n   for (int i = 0; i < len - 1; i++) n     ans += 1LL * ac.Ans[i] * ac2.Ans[len - i - 2]; n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n const int maxn = 5205; n using namespace std; n int n, a[maxn][maxn], m; n int cnt, sum[maxn][maxn], x[maxn]; n bool check(int x) { n   int now; n   for (int i = x; i <= n; i += x) { n     for (int j = x; j <= n; j += x) { n       now = sum[i][j] - sum[i - x][j] - sum[i][j - x] + sum[i - x][j - x]; n       if (now == 0 || now == x * x) n         continue; n       else n         return 0; n     } n   } n   return 1; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n / 4; j++) { n       scanf( %1x , &m); n       cnt = 0; n       while (m) { n         a[i][j * 4 - cnt++] = (m % 2); n         m >>= 1; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j]; n     } n   } n   cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (n % i == 0) { n       x[++cnt] = i; n     } n   } n   for (int k = cnt; k >= 1; k--) { n     if (check(x[k])) { n       printf( %d n , x[k]); n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1000 * 1000 + 100; n int nex[maxN]; n int st[maxN]; n int en[maxN]; n vector<int> c[maxN]; n int ind; n void dfs(int s) { n   st[s] = ind++; n   for (auto x : c[s]) dfs(x); n   en[s] = ind; n } n int main() { n   int n, m; n   string s; n   cin >> n >> m; n   cin >> s; n   int curr = 0; n   c[curr].push_back(1); n   for (int i = 1; i < ((int)(s).size()); i++) { n     while (curr && s[i] != s[curr]) curr = nex[curr]; n     if (s[curr] == s[i]) curr++; n     nex[i + 1] = curr; n     c[curr].push_back(i + 1); n   } n   dfs(0); n   int len = 0; n   if (m != 0) { n     int last; n     cin >> last; n     last--; n     len = ((int)(s).size()); n     int p = ((int)(s).size()); n     for (int i = 1; i < m; i++) { n       int num; n       cin >> num; n       num--; n       len += ((int)(s).size()); n       len -= max(0, last + p - num); n       if (last + p - num > 0) { n         int lfix = last + p - num; n         if (!(st[lfix] <= st[p] && st[p] < en[lfix])) { n           cout << 0 << endl; n           return 0; n         } n       } n       last = num; n     } n   } else n     len = 0; n   long long ans = 1; n   int mod = 1e9 + 7; n   len = n - len; n   for (int i = 0; i < len; i++) ans = ans * 26LL % mod; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const vector<int> mods = {998244353, 1000000007, 1000000009}; n const long long inf = 3e18; n const double pi = acos(-1.0); n const double eps = 0.0000001; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n template <class... K> n using umap = unordered_map<K...>; n template <class... K> n using uset = unordered_set<K...>; n int n, m, k; n int u, v, w; n const int N = 200100; n const int P = 400000; n umap<int, vector<pair<int, int> > > g; n long long pw[N][3]; n long long fc[10][10][3]; n vector<long long> goal; n long long comb[15]; n long long res; n void build() { n   for (int j = (0); j < (3); ++j) pw[0][j] = 1; n   for (int i = (1); i < (n + 10); ++i) n     for (int j = (0); j < (3); ++j) { n       pw[i][j] = (pw[i - 1][j] * 13) % mods[j]; n     } n   goal.resize(3); n   for (int i = (0); i < (n); ++i) n     for (int j = (0); j < (3); ++j) { n       goal[j] = (goal[j] + pw[i][j]) % mods[j]; n     } n } n void inc() { n   long long car = 1; n   for (int i = (1); i < (10); ++i) { n     long long nxc = (comb[i] + car) / i; n     comb[i] = (comb[i] + car) % i; n     car = nxc; n   } n } n bool alz() { n   for (int i = (1); i < (k + 1); ++i) { n     if (comb[i]) return 0; n   } n   return 1; n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   cout.precision(13); n   cin >> n >> m >> k; n   build(); n   for (int i = (0); i < (m); ++i) { n     cin >> u >> v >> w; n     u--; n     v--; n     g[u].push_back({w, v}); n   } n   for (int i = (0); i < (n); ++i) sort(begin(g[i]), end(g[i])); n   for (int i = (0); i < (n); ++i) { n     long long sg = ((int)(g[i]).size()); n     for (int j = (0); j < (sg); ++j) n       for (int k = (0); k < (3); ++k) { n         fc[sg][j][k] = (fc[sg][j][k] + pw[g[i][j].second][k]) % mods[k]; n       } n   } n   res = 0; n   for (int i = (0); i < (P); ++i) { n     vector<long long> cur(3); n     for (int i = (0); i < (k); ++i) n       for (int j = (0); j < (3); ++j) { n         cur[j] = (cur[j] + fc[i + 1][comb[i + 1]][j]) % mods[j]; n       } n     if (cur == goal) res++; n     inc(); n     if (alz()) break; n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K; n string s; n bool dp[1010][1010]; n bool dfs(int i, int j) { n   if (i == N && abs(j) == K) return true; n   if (i != N && abs(j) == K) return false; n   if (i == N && abs(j) != K) return false; n   if (dp[i][j] == 1) return false; n   dp[i][j] = 1; n   if (s[i] ==  W ) return dfs(i + 1, j + 1); n   if (s[i] ==  L ) return dfs(i + 1, j - 1); n   if (s[i] ==  D ) return dfs(i + 1, j); n   if (dfs(i + 1, j + 1) == true) { n     s[i] =  W ; n     return 1; n   } n   if (dfs(i + 1, j) == true) { n     s[i] =  D ; n     return 1; n   } n   if (dfs(i + 1, j - 1) == true) { n     s[i] =  L ; n     return 1; n   } n   return 0; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> N >> K; n   cin >> s; n   if (dfs(0, 0) == true) n     cout << s << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> adj; n vector<int> a, b, t; n vector<int> dep, sz; n void dfs(int v, int p) { n   dep[v] = dep[p] + 1; n   for (auto c : adj[v]) { n     if (c != p) { n       dfs(c, v); n       sz[v] += sz[c]; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     int k; n     cin >> k; n     a.resize(2 * k - 1); n     b.resize(2 * k - 1); n     t.resize(2 * k - 1); n     adj.assign(2 * k + 1, vector<int>()); n     for (int i = 0; i < 2 * k - 1; ++i) { n       cin >> a[i] >> b[i] >> t[i]; n       adj[a[i]].push_back(b[i]); n       adj[b[i]].push_back(a[i]); n     } n     dep.assign(2 * k + 1, 0); n     sz.assign(2 * k + 1, 1); n     dfs(1, 0); n     long long solMx = 0, solMn = 0; n     for (int i = 0; i < 2 * k - 1; ++i) { n       if (dep[a[i]] > dep[b[i]]) { n         swap(a[i], b[i]); n       } n       solMx += (long long)min(sz[b[i]], 2 * k - sz[b[i]]) * t[i]; n       solMn += sz[b[i]] % 2 * t[i]; n     } n     cout << solMn <<     << solMx <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int MAXN = 5e5 + 10; n bool bio[MAXN]; n int n; n int a[MAXN], b[MAXN], aa[MAXN], bb[MAXN]; n int deg[1 << 20]; n map<pair<int, int>, vector<int>> id; n vector<pair<int, int>> e[1 << 20]; n stack<int> path; n bool euler(int x, bool print) { n   for (int i = 0; i < n; ++i) { n     bio[i] = false; n     a[i] = aa[i] & ((1 << x) - 1); n     b[i] = bb[i] & ((1 << x) - 1); n     deg[a[i]] = deg[b[i]] = 0; n     e[a[i]].clear(); n     e[b[i]].clear(); n   } n   for (int i = 0; i < n; ++i) { n     deg[a[i]]++; n     deg[b[i]]++; n     e[a[i]].emplace_back(b[i], i); n     if (a[i] != b[i]) e[b[i]].emplace_back(a[i], i); n   } n   for (int i = 0; i < n; ++i) n     if (deg[a[i]] % 2 || deg[b[i]] % 2) return false; n   vector<int> ret; n   int node = a[0]; n   path.push(node); n   while (!path.empty()) { n     int nxt = -1; n     while (!e[node].empty() && bio[e[node].rbegin()->second]) n       e[node].pop_back(); n     if (!e[node].empty()) { n       path.push(node); n       nxt = e[node].rbegin()->first; n       bio[e[node].rbegin()->second] = true; n       e[node].pop_back(); n       node = nxt; n     } else { n       ret.push_back(node); n       node = path.top(); n       path.pop(); n     } n   } n   if (!print) return ret.size() == n + 1; n   if (ret.size() == n + 1) { n     printf( %d n , x); n     for (int i = 0; i < n; ++i) { n       int aa = a[i] & ((1 << x) - 1); n       int bb = b[i] & ((1 << x) - 1); n       id[{aa, bb}].push_back(i); n     } n     for (int i = ret.size() - 2; i >= 0; --i) { n       int a = ret[i + 1], b = ret[i]; n       if (id.find({a, b}) != id.end() && id[{a, b}].size() != 0) { n         int x = *id[{a, b}].rbegin(); n         id[{a, b}].pop_back(); n         printf( %d %d  , 2 * x + 1, 2 * x + 2); n       } else { n         int x = *id[{b, a}].rbegin(); n         id[{b, a}].pop_back(); n         printf( %d %d  , 2 * x + 2, 2 * x + 1); n       } n     } n     printf(  n ); n     exit(0); n   } n } n int main(void) { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , &aa[i], &bb[i]); n   } n   int lo = 0, hi = 20; n   while (lo < hi) { n     int mid = (lo + hi) / 2 + 1; n     if (euler(mid, false)) n       lo = mid; n     else n       hi = mid - 1; n   } n   euler(lo, true); n   return 0; n } n