#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n int *tree; n void buildTree(int node, int arr[], int start, int end) { n   if (start == end) { n     tree[node] = arr[start]; n     return; n   } n   int mid = (start + end) / 2; n   buildTree(2 * node + 1, arr, start, mid); n   buildTree(2 * node + 2, arr, mid + 1, end); n   tree[node] = min(tree[2 * node + 1], tree[2 * node + 2]); n } n int query(int node, int start, int end, int L, int R) { n   if (L <= start && R >= end) n     return tree[node]; n   else if (L > end || R < start) n     return INT32_MAX; n   else { n     int mid = (start + end) / 2; n     int c1 = query(2 * node + 1, start, mid, L, R); n     int c2 = query(2 * node + 2, mid + 1, end, L, R); n     return min(c1, c2); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int n, x; n   cin >> n >> x; n   x--; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   tree = new int[4 * n]; n   buildTree(0, a, 0, n - 1); n   int i = n - 1; n   bool ans = false; n   while (i >= 0) { n     long long y = a[i]; n     if (x > i) { n       long long p = y * n + x - i; n       int h1 = query(0, 0, n - 1, i + 1, x); n       int h2 = -1; n       if (x + 1 < n) h2 = query(0, 0, n - 1, x + 1, n - 1); n       int h3 = query(0, 0, n - 1, 0, i - 1); n       bool cond = true; n       if (h1 < y + 1) cond = false; n       if (h2 != -1 && h2 < y) cond = false; n       if (h3 < y) cond = false; n       if (cond) { n         long long b[n]; n         b[i] = p; n         for (int j = i + 1; j < x + 1; j++) { n           b[j] = a[j] - (y + 1); n         } n         for (int j = x + 1; j < n; j++) { n           b[j] = a[j] - y; n         } n         for (int j = 0; j < i; j++) { n           b[j] = a[j] - y; n         } n         for (auto e : b) { n           cout << e <<    ; n         } n         cout << endl; n         break; n       } n     } else if (x < i) { n       long long p = y * n + n - i - 1 + x + 1; n       int h1 = query(0, 0, n - 1, i + 1, n - 1); n       int h3 = query(0, 0, n - 1, 0, x); n       int h2 = query(0, 0, n - 1, x + 1, i - 1); n       bool cond = true; n       if (h1 < y + 1) cond = false; n       if (h2 < y) cond = false; n       if (h3 < y + 1) cond = false; n       if (cond) { n         long long b[n]; n         b[i] = p; n         for (int j = i + 1; j < n; j++) { n           b[j] = a[j] - (y + 1); n         } n         for (int j = 0; j < x + 1; j++) { n           b[j] = a[j] - (y + 1); n         } n         for (int j = x + 1; j < i; j++) { n           b[j] = a[j] - y; n         } n         for (auto e : b) { n           cout << e <<    ; n         } n         cout << endl; n         break; n       } n     } else { n       long long p = y * n; n       int h1 = -1; n       if (i + 1 < n) { n         h1 = query(0, 0, n - 1, i + 1, n - 1); n       } n       int h3 = -1; n       if (x - 1 >= 0) { n         h3 = query(0, 0, n - 1, 0, x - 1); n       } n       bool cond = true; n       if (h1 != -1 && h1 < y) cond = false; n       if (h3 != -1 && h3 < y) cond = false; n       if (cond) { n         long long b[n]; n         b[i] = p; n         for (int j = i + 1; j < n; j++) { n           b[j] = a[j] - (y); n         } n         for (int j = 0; j < x; j++) { n           b[j] = a[j] - (y); n         } n         for (auto e : b) { n           cout << e <<    ; n         } n         cout << endl; n         break; n       } n     } n     i--; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int a[N]; n int cnt[N]; n int need[N]; n int n, m; n int getbit(int x, int i) { return (x >> i) & 1; } n void add(int x, int val) { n   for (int mask = 0; mask < (1 << 6); mask++) n     if (getbit(mask, x)) cnt[mask] += val; n } n bool check() { n   for (int mask = 0; mask < (1 << 6); mask++) n     if (cnt[mask] < need[mask]) return false; n   return true; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   string s; n   cin >> s; n   for (char c : s) add(c -  a , 1); n   n = s.size(); n   for (int i = 1; i <= n; i++) a[i] = 63; n   cin >> m; n   for (int i = 1; i <= m; i++) { n     int id; n     string t; n     cin >> id >> t; n     int mask = 0; n     for (char c : t) mask |= (1 << (c -  a )); n     a[id] = mask; n   } n   for (int i = 1; i <= n; i++) need[a[i]]++; n   for (int i = 0; i < 6; i++) n     for (int mask = 0; mask < (1 << 6); mask++) n       if (getbit(mask, i)) need[mask] += need[mask ^ (1 << i)]; n   string ans; n   for (int i = 1; i <= n; i++) { n     bool found = false; n     for (int mask = a[i]; mask < (1 << 6); mask = (mask + 1) | a[i]) n       need[mask]--; n     for (int j = 0; j <= 5; j++) n       if (getbit(a[i], j)) { n         add(j, -1); n         if (check()) { n           ans += (char)j +  a ; n           found = true; n           break; n         } n         add(j, 1); n       } n     if (!found) { n       ans =  Impossible ; n       break; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5005, K = 6, INF = 1000000000; n int n; n int t[N][3], pass[3], hack[3]; n bool ch[N][3]; n int nhack[K][3]; n int calcs(int lv, int t) { n   if (t == 0) { n     return 0; n   } else { n     return 500 * (lv + 1) * (250 - t) / 250; n   } n } n const int L = 105; n int dp[L][L][L]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < 3; ++j) { n       int tmp; n       scanf( %d , &tmp); n       if (tmp < 0) { n         t[i][j] = -tmp; n         ch[i][j] = true; n         ++pass[j]; n         ++hack[j]; n       } else if (tmp == 0) { n       } else { n         t[i][j] = tmp; n         ch[i][j] = false; n         ++pass[j]; n       } n     } n   } n   int ans = n; n   for (int i = 0; i < 6; ++i) { n     int c = 2 << i, minp = i == 5 ? 0 : n / c + 1, maxp = 2 * n / c; n     for (int j = 0; j < 3; ++j) { n       if (pass[j] >= minp && pass[j] - hack[j] <= maxp) { n         nhack[i][j] = min(hack[j], pass[j] - minp); n       } else { n         nhack[i][j] = -1; n       } n     } n   } n   for (int ai = 0; ai < 6; ++ai) { n     int la = nhack[ai][0]; n     if (la != -1) { n       for (int bi = 0; bi < 6; ++bi) { n         int lb = nhack[bi][1]; n         if (lb != -1) { n           for (int ci = 0; ci < 6; ++ci) { n             int lc = nhack[ci][2]; n             if (lc != -1) { n               int myscore = calcs(ai, t[0][0]) + calcs(bi, t[0][1]) + n                             calcs(ci, t[0][2]) + (la + lb + lc) * 100; n               if (myscore >= calcs(ai, 1) + calcs(bi, 1) + calcs(ci, 1)) { n                 ans = 1; n                 printf( %d n , ans); n                 return 0; n               } n               for (int i = 0; i <= la; ++i) { n                 for (int j = 0; j <= lb; ++j) { n                   for (int k = 0; k <= lc; ++k) { n                     dp[i][j][k] = 0; n                   } n                 } n               } n               int lv[3]; n               lv[0] = ai, lv[1] = bi, lv[2] = ci; n               int buf = 0; n               for (int i = 1; i < n; ++i) { n                 vector<int> sol; n                 for (int j = 0; j < 8; ++j) { n                   bool dup = false; n                   for (int k = 0; k < (int)sol.size() && !dup; ++k) { n                     int s = sol[k]; n                     dup = dup | ((s & j) == s); n                   } n                   if (dup) { n                     continue; n                   } n                   int tmpscore = 0; n                   for (int k = 0; k < 3; ++k) { n                     if ((j >> k) & 1) { n                       if (!ch[i][k]) { n                         tmpscore = INF; n                         break; n                       } n                     } else { n                       tmpscore += calcs(lv[k], t[i][k]); n                     } n                   } n                   if (tmpscore <= myscore) { n                     sol.push_back(j); n                   } n                 } n                 if (sol.size() == 0) { n                   continue; n                 } else if (sol[0] == 0) { n                   ++buf; n                   continue; n                 } n                 { n                   if (dp[la][lb][lc] == la + lb + lc) { n                     continue; n                   } n                   for (int i = la; i >= 0; --i) { n                     for (int j = lb; j >= 0; --j) { n                       for (int k = lc; k >= 0; --k) { n                         for (int l = 0; l < (int)sol.size(); ++l) { n                           int ni = i + (sol[l] & 1), n                               nj = j + ((sol[l] >> 1) & 1), n                               nk = k + ((sol[l] >> 2) & 1); n                           if (dp[ni][nj][nk] < dp[i][j][k] + 1) { n                             dp[ni][nj][nk] = dp[i][j][k] + 1; n                           } n                         } n                       } n                     } n                   } n                 } n               } n               ans = min(ans, n - (dp[la][lb][lc] + buf)); n             } n           } n         } n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e9 + 5; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n, m, c, cost = 0; n   cin >> n >> m; n   for (long long i = 0; i < n; i++) { n     long long temp = N; n     for (long long j = 0; j < m; j++) { n       cin >> c; n       temp = min(temp, c); n     } n     cost = max(temp, cost); n   } n   cout << cost <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD7 = 1e9 + 7; n const int MOD9 = 1e9 + 9; n const double EPS = 1e-9; n long long int gcd(long long int a, long long int b) { n   return b == 0 ? a : gcd(b, a % b); n } n int toint(const string &s) { n   stringstream second; n   second << s; n   int x; n   second >> x; n   return x; n } n long long int mpow(long long int a, long long int n, int mod) { n   long long int ret = 1; n   long long int b = a; n   while (n) { n     if (n & 1) ret = (ret * b) % mod; n     b = (b * b) % mod; n     n >>= 1; n   } n   return (long long int)ret; n } n struct cmp { n   bool operator()(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { n     if (a.first.first < b.first.first) n       return true; n     else if (a.first.first == b.first.first) n       return a.first.second < b.first.second; n     return false; n   } n }; n map<int, int> hashm; n vector<pair<int, int> > coordy, v; n int main() { n   int tc; n   tc = 1; n   int i, n, m, k, a, b, c, d, u, w, sum, permissible, j; n   long long minl = 0, areamin = (long long int)9223372036854775805; n   int pfor[13] = {0}, pback[13] = {0}; n   while (tc--) { n     scanf( %d , &n); n     scanf( %d , &k); n     for (i = 0; i < n; i++) { n       scanf( %d , &a); n       scanf( %d , &b); n       scanf( %d , &c); n       scanf( %d , &d); n       coordy.push_back(make_pair(b + d, a + c)); n       hashm[a + c]++; n     } n     m = 0; n     for (map<int, int>::iterator it = hashm.begin(); it != hashm.end(); ++it) { n       v.push_back(make_pair(it->second, it->first)); n     } n     m = v.size(); n     for (i = 0; i < k; i++) pfor[i + 1] = pfor[i] + v[i].first; n     for (i = 0; i < k; i++) pback[i + 1] = pback[i] + v[m - 1 - i].first; n     sort(coordy.begin(), coordy.end()); n     for (i = 0; i <= k; i++) { n       for (j = 0; j <= k; j++) { n         if (i <= m - 1 - j && v[i].second <= v[m - 1 - j].second && n             pfor[i] + pback[j] <= k) { n           u = 0; n           w = 0; n           sum = 0; n           permissible = n - k; n           minl = 2 * MOD9; n           while (u <= w && w < n) { n             if (coordy[w].second >= v[i].second && n                 coordy[w].second <= v[m - 1 - j].second) n               sum++; n             while (sum >= permissible && coordy[w].first >= coordy[u].first) { n               if (coordy[w].first > coordy[u].first) n                 minl = min(minl, n                            (long long int)(coordy[w].first - coordy[u].first)); n               else n                 minl = min(minl, (long long int)2); n               if (coordy[u].second >= v[i].second && n                   coordy[u].second <= v[m - 1 - j].second) n                 sum--; n               u++; n             } n             w++; n           } n           if (minl != MOD9 * 2 && v[m - 1 - j].second != v[i].second) n             areamin = min(areamin, (long long int)minl * n                                        (v[m - 1 - j].second - v[i].second)); n           else if (minl != MOD9 * 2 && v[m - 1 - j].second == v[i].second) n             areamin = min(areamin, (long long int)minl * 2); n         } n       } n     } n     if (n == 1 || (areamin / 4) == 0) n       printf( 1 n ); n     else n       printf( %lld n , areamin / 4); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 9, M = 2e5 + 9, OO = 0x3f3f3f3f, mod = 1e9 + 7; n long long n, aS, bS, aT, bT; n string s, t; n vector<long long> bMis, aMis; n int main() { n   cin >> n >> s >> t; n   for (auto i : s) { n     aS += (i ==  a ); n     bS += (i ==  b ); n   } n   for (auto i : t) { n     aT += (i ==  a ); n     bT += (i ==  b ); n   } n   if ((aS + aT) % 2) return cout <<  -1 , 0; n   if ((bS + bT) % 2) return cout <<  -1 , 0; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  b  and t[i] ==  a ) bMis.push_back(i + 1); n     if (s[i] ==  a  and t[i] ==  b ) aMis.push_back(i + 1); n   } n   long long nSwap = (bMis.size() / 2) + (aMis.size() / 2); n   if (bMis.size() % 2) nSwap += 2; n   cout << nSwap <<   n ; n   for (int i = 0; i < bMis.size(); i += 2) { n     if (i == bMis.size() - 1) break; n     cout << bMis[i] <<     << bMis[i + 1] <<   n ; n   } n   for (int i = 0; i < aMis.size(); i += 2) { n     if (i == aMis.size() - 1) break; n     cout << aMis[i] <<     << aMis[i + 1] <<   n ; n   } n   if (bMis.size() % 2) { n     cout << aMis.back() <<     << aMis.back() <<   n ; n     cout << aMis.back() <<     << bMis.back() <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool isUpper(string s) { n   return std::all_of(s.begin(), s.end(), n                      [](unsigned char c) { return std::isupper(c); }); n } n int main() { n   string s1, s2; n   cin >> s1 >> s2; n   transform(s1.begin(), s1.end(), s1.begin(), ::tolower); n   transform(s2.begin(), s2.end(), s2.begin(), ::tolower); n   int ans = 0; n   for (int i = 0; i < s1.length(); i++) { n     if (s1[i] > s2[i]) { n       ans = 1; n       break; n     } else if (s1[i] < s2[i]) { n       ans = -1; n       break; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> pril, sush, glag; n void Init() { n   pril.push_back( lios ); n   pril.push_back( liala ); n   sush.push_back( etr ); n   sush.push_back( etra ); n   glag.push_back( initis ); n   glag.push_back( inites ); n } n pair<int, bool> Check(string str) { n   pair<int, bool> res; n   for (int i = 0; i < 2; i++) { n     if (str.length() >= pril[i].length() && n         str.substr(str.length() - pril[i].length(), pril[i].length()) == n             pril[i]) n       return make_pair(0, i); n   } n   for (int i = 0; i < 2; i++) { n     if (str.length() >= sush[i].length() && n         str.substr(str.length() - sush[i].length(), sush[i].length()) == n             sush[i]) n       return make_pair(1, i); n   } n   for (int i = 0; i < 2; i++) { n     if (str.length() >= glag[i].length() && n         str.substr(str.length() - glag[i].length(), glag[i].length()) == n             glag[i]) n       return make_pair(2, i); n   } n   return make_pair(-1, 0); n } n int main() { n   Init(); n   int f[3]; n   for (int i = 0; i < 3; i++) f[i] = 0; n   char s; n   string str; n   bool inStr = false, sol = true; n   int type = -1; n   int step = -1; n   int res = 0; n   bool pol = false; n   while ((s = getchar()) != EOF) { n     if (s >=  a  && s <=  z ) { n       if (!inStr) inStr = true; n       str.push_back(s); n     } else if (inStr) { n       inStr = false; n       pair<int, bool> r = Check(str); n       if (r.first == -1) { n         sol = false; n         break; n       } n       if (type == -1) { n         type = r.first; n         pol = r.second; n         f[type]++; n       } else { n         if (pol != r.second) n           sol = false; n         else if (type + 1 == r.first) { n           type = r.first; n           f[type]++; n         } else if (type == r.first) { n           if (type == 1) sol = false; n           f[type]++; n         } else n           sol = false; n         if (!sol) break; n       } n       str.clear(); n     } n   } n   if (!sol) n     printf( NO ); n   else { n     if (inStr) { n       inStr = false; n       pair<int, bool> r = Check(str); n       if (r.first == -1) { n         sol = false; n       } n       if (type == -1) { n         type = r.first; n         pol = r.second; n         f[type]++; n       } else { n         if (pol != r.second) n           sol = false; n         else if (type + 1 == r.first) { n           type = r.first; n           f[type]++; n         } else if (type == r.first) { n           if (type == 1) sol = false; n           f[type]++; n         } else n           sol = false; n       } n       str.clear(); n     } n     int sum = 0; n     for (int i = 0; i < 3; i++) sum += f[i]; n     if (sum == 1) n       printf( YES ); n     else { n       if (sum == f[0] || sum == f[1] || sum == f[2]) n         printf( NO ); n       else n         printf( YES ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const int N = 300005; n long long n, e, idx[N], low[N], T, q[300005], ans = 0, ans2 = 1, scc[100005], n                                               mini[300005], cn, second[3000005]; n vector<vector<long long> > g; n vector<long long> S; n bool vis[N]; n long long compID[N], cmp; n void DFS(int u) { n   idx[u] = low[u] = ++T; n   S.push_back(u); n   vis[u] = true; n   for (int i = 0; i < g[u].size(); ++i) { n     long long v = g[u][i]; n     if (idx[v] == 0) DFS(v); n     if (vis[v]) low[u] = min(low[u], low[v]); n   } n   if (idx[u] == low[u]) { n     long long ee = 1e10; n     cn++; n     while (true) { n       long long v = S.back(); n       ee = min(ee, q[v]); n       scc[v] = cn; n       S.pop_back(); n       vis[v] = false; n       compID[v] = cmp; n       if (v == u) break; n     } n     mini[cn] = ee; n     ans += ee; n     ++cmp; n   } n } n int main() { n   cin >> n; n   g.resize(n); n   for (int i = 0; i < n; i++) cin >> q[i]; n   cin >> e; n   for (int i = 0; i < e; ++i) { n     int from, to; n     cin >> from >> to; n     --from; n     --to; n     g[from].push_back(to); n   } n   for (int i = 0; i < n; ++i) { n     if (idx[i] == 0) { n       DFS(i); n     } n   } n   for (int i = 0; i < n; i++) { n     int in1 = scc[i]; n     int uu = mini[in1]; n     if (q[i] == uu) second[in1]++; n   } n   for (int i = 1; i <= cn; i++) { n     ans2 *= second[i]; n     ans2 %= mod; n   } n   cout << ans <<     << ans2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> factor[500010]; n int m, q; n long long n; n int p[10000], c[10000]; n int divide(long long n) { n   int ans = 0; n   while (n % 5 == 0) n = n / 5 * 4, ans++; n   while (n % 3 == 0) n = n / 3 * 2, ans++; n   while (n % 2 == 0) n = n / 2, ans++; n   if (n > 1) return -1; n   return ans; n } n int main() { n   scanf( %d , &q); n   while (q--) { n     scanf( %lld , &n); n     int p = divide(n); n     if (p == -1) n       printf( -1 n ); n     else n       printf( %d n , p); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxsize = 100; n int n, m; n bool visited[maxsize][maxsize]; n string v, h; n int go[][2] = {1, 0, -1, 0, 0, -1, 0, 1}; n map<char, int> dir; n int cnt; n void dfs(int row, int col) { n   if (visited[row][col]) return; n   visited[row][col] = true; n   cnt++; n   int n_row = -1, n_col = -1; n   n_row = row + go[dir[h[row]]][0]; n   n_col = col + go[dir[h[row]]][1]; n   if (n_col >= 0 && n_col < m) { n     dfs(n_row, n_col); n   } n   n_row = row + go[dir[v[col]]][0]; n   n_col = col + go[dir[v[col]]][1]; n   if (n_row >= 0 && n_row < n) { n     dfs(n_row, n_col); n   } n } n int main(int argc, char* argv[]) { n   dir[ > ] = 3; n   dir[ < ] = 2; n   dir[ ^ ] = 1; n   dir[ v ] = 0; n   while (scanf( %d%d , &n, &m) != EOF) { n     cin >> h >> v; n     bool flag = true; n     for (int ii = 0; ii < n && flag; ii++) { n       for (int jj = 0; jj < m && flag; jj++) { n         memset(visited, false, sizeof(visited)); n         cnt = 0; n         dfs(ii, jj); n         if (cnt != n * m) flag = false; n       } n     } n     if (cnt == n * m) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T nextInt() { n   T x = 0, p = 1; n   char ch; n   do { n     ch = getchar(); n   } while (ch <=    ); n   if (ch ==  - ) { n     p = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + (ch -  0 ); n     ch = getchar(); n   } n   return x * p; n } n const int maxN = (int)5e2 + 10; n const int INF = (int)1e9; n const int mod = (int)1e9 + 7; n const long long LLINF = (long long)1e18; n long long a[maxN]; n int n; n bool ok = false; n bool bad(int idx) { n   for (int i = 0; i < idx; ++i) { n     int r = 0; n     for (int j = 0; j < n; ++j) { n       if (((a[i] >> j) & 1) != ((a[idx] >> j) & 1)) { n         r--; n       } else { n         r++; n       } n     } n     if (r != 0) return true; n   } n   return false; n } n int cnt = 0; n void print() { n   ok = 0; n   for (int i = 0; i < 30; ++i) putchar( - ); n   putchar(  n ); n   char f[] = { * ,  + }; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       cout << f[(a[i] >> j) & 1]; n     } n     cout <<   n ; n   } n   cout <<   n ; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       cout << f[(a[i] >> j) & 1]; n       if (j == n / 2 - 1) putchar(   ); n     } n     cout <<   n ; n     if (i == n / 2 - 1) putchar(  n ); n   } n   cout <<   n ; n   for (int i = 0; i < 30; ++i) putchar( - ); n   putchar(  n ); n } n void dfs(int idx, int n) { n   if (idx == n) { n     print(); n     return; n   } n   for (long long mask = 0; mask < (1LL << n) && ok; ++mask) { n     a[idx] = mask; n     if (bad(idx)) continue; n     dfs(idx + 1, n); n   } n } n char rev(char x) { n   if (x ==  + ) return  * ; n   return  + ; n } n vector<vector<char> > build(int k) { n   vector<vector<char> > ret(1 << k, vector<char>(1 << k,  ? )); n   if (k == 0) { n     ret[0][0] =  * ; n     return ret; n   } n   vector<vector<char> > prev = build(k - 1); n   int prevsz = (1 << k) / 2; n   for (int i = 0; i < prevsz; ++i) { n     for (int j = 0; j < prevsz; ++j) { n       ret[i][2 * j] = ret[i][2 * j + 1] = prev[i][j]; n     } n   } n   for (int j = prevsz - 1; j >= 0; --j) { n     for (int i = 0; i < prevsz; ++i) { n       ret[prevsz + i][2 * j + 1] = prev[i][j]; n       ret[prevsz + i][2 * j] = rev(prev[i][j]); n     } n   } n   return ret; n } n int main() { n   int k; n   cin >> k; n   vector<vector<char> > answer = build(k); n   for (int i = 0; i < answer.size(); ++i) { n     for (int j = 0; j < answer[i].size(); ++j) { n       putchar(answer[i][j]); n     } n     putchar(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long k; n int res[1000]; n long long dp[205][205]; n int p[105][105]; n pair<int, int> a[10005]; n long long calc(int i, int j) { n   if (j < 0) return 0; n   if (i == n + m - 1) { n     return (j == 0); n   } n   if (dp[i][j] != -1) return dp[i][j]; n   long long &ans = dp[i][j]; n   ans = 0; n   if (res[i] != 0) n     ans = calc(i + 1, j + res[i]); n   else { n     ans = calc(i + 1, j + 1) + calc(i + 1, j - 1); n   } n   if (ans >= k) ans = k; n   return ans; n } n long long solve() { n   memset(dp, -1, sizeof(dp)); n   return calc(0, 0); n } n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       int x; n       cin >> x; n       a[x - 1] = make_pair(i, j); n     } n   } n   for (int l = 0; l < n * m; ++l) { n     int i = a[l].first; n     int j = a[l].second; n     if (res[i + j] == 0) { n       res[i + j] = 1; n       long long cur = solve(); n       if (cur >= k) continue; n       k -= cur; n       res[i + j] = -1; n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (res[i + j] == 1) n         putchar( ( ); n       else n         putchar( ) ); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker, ”/ STACK : 38777216“ n using namespace std; n const int N = 200005; n const int inf = 1000 * 1000 * 1000; n const int mod = 1000 * 1000 * 1000 + 7; n int n, a[N]; n set<int> q; n set<int>::iterator it; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     if (a[i] == 0) q.insert(i); n   } n   for (int i = 1; i <= n; i++) { n     if (a[i] == 0) n       printf( 0  ); n     else { n       int answ = inf; n       q.insert(i); n       it = q.find(i); n       ++it; n       if (it != q.end()) answ = min(answ, *it - i); n       --it; n       if (it != q.begin()) { n         --it; n         answ = min(answ, i - *it); n         ++it; n       } n       q.erase(i); n       printf( %d  , answ); n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   ; n   cin.tie(0); n   cout.tie(0); n   int n, a, b, i; n   string s; n   cin >> n >> a >> b >> s; n   int tot = a + b, p = 0; n   for (i = 0; i < n; i++) { n     if (s[i] ==  * ) { n       p = 0; n       continue; n     } n     if (p == 0) { n       if (a >= b) { n         a--; n         p = 1; n       } else { n         b--; n         p = 2; n       } n     } else if (p == 1 && b > 0) { n       b--; n       p = 2; n     } else if (p == 1) n       p = 0; n     else if (p == 2 && a > 0) { n       a--; n       p = 1; n     } else if (p == 2) n       p = 0; n     if (a + b == 0) break; n   } n   cout << tot - (a + b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; 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 template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  , ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cerr <<  { ; n   for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n inline long long int max(long long int a, long long int b, long long int c) { n   return max(max(a, b), c); n } n inline long long int min(long long int a, long long int b, long long int c) { n   return min(min(a, b), c); n } n inline long long int max(long long int a, long long int b) { n   return (a > b) ? a : b; n } n inline long long int min(long long int a, long long int b) { n   return (a < b) ? a : b; n } n inline long long int mul(long long int x, long long int y, long long int mod_) { n   return ((x % mod_) * 1LL * (y % mod_)) % mod_; n } n long long int power(long long int a, long long int n) { n   long long int p = 1; n   while (n > 0) { n     if (n % 2) { n       p = p * a; n     } n     n >>= 1; n     a *= a; n   } n   return p; n } n long long int powm(long long int a, long long int n, long long int mod_) { n   long long int p = 1; n   while (n) { n     if (n % 2) { n       p = mul(p, a, mod_); n     } n     n >>= 1; n     a = mul(a, a, mod_); n   } n   return p % mod_; n } n long long int powi(long long int a, long long int mod_) { n   return powm(a, mod_ - 2, mod_); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   long long int mn, mx; n   long long int n, m, t, k, i, j, sum = 0, flag = 0, cnt = 0; n   long long int x = 0, y = 0, z, l = 0, r = 0, q; n   int TC = 1; n   while (TC--) { n     cin >> q; n     cin >> n >> m; n     vector<pair<pair<long long int, long long int>, n                 pair<long long int, long long int>>> n         sofa(q); n     for (i = 0; i < q; ++i) { n       cin >> sofa[i].first.first >> sofa[i].first.second >> n           sofa[i].second.first >> sofa[i].second.second; n     } n     long long int cr, cl, ct, cb; n     cin >> cl >> cr >> ct >> cb; n     vector<long long int> prx(n + 2), pry(m + 2), sux(n + 2), suy(m + 2); n     for (i = 0; i < q; ++i) { n       pair<long long int, long long int> p1, p2; n       tie(p1, p2) = sofa[i]; n       long long int xl = min(p1.first, p2.first); n       long long int xr = max(p1.first, p2.first); n       long long int yt = max(p1.second, p2.second); n       long long int yd = min(p1.second, p2.second); n       prx[xl]++; n       sux[xr]++; n       pry[yd]++; n       suy[yt]++; n     } n     for (i = 1; i < n + 2; ++i) { n       prx[i] += prx[i - 1]; n     } n     for (i = n; i >= 0; --i) { n       sux[i] += sux[i + 1]; n     } n     for (i = 1; i < m + 2; ++i) { n       pry[i] += pry[i - 1]; n     } n     for (i = m; i >= 0; --i) { n       suy[i] += suy[i + 1]; n     } n     for (i = 0; i < q; ++i) { n       pair<long long int, long long int> p1, p2; n       tie(p1, p2) = sofa[i]; n       long long int xl = min(p1.first, p2.first); n       long long int xr = max(p1.first, p2.first); n       long long int yt = max(p1.second, p2.second); n       long long int yd = min(p1.second, p2.second); n       ; n       if (cl == (prx[xr - 1] - (xl != xr)) and n           cr == (sux[xl + 1] - (xl != xr)) and n           cb == (suy[yd + 1] - (yt != yd)) and n           ct == (pry[yt - 1] - (yt != yd))) { n         cout << i + 1; n         return 0; n       } n     } n     cout << -1; n   }; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int add(int _a, int _b) { n   _a = (_a + 1000000007) % 1000000007; n   _b = (_b + 1000000007) % 1000000007; n   return (_a + _b) % 1000000007; n } n int mul(int _a, int _b) { n   _a = (_a + 1000000007) % 1000000007; n   _b = (_b + 1000000007) % 1000000007; n   return ((long long int)((long long int)_a * (long long int)_b)) % 1000000007; n } n int n, k, sbtr[100010], sum[210], dif[100010][210], n     sol = 0, fac[100010], inv[100010], h[100010][210], f[210][210]; n vector<int> adj[100010]; n void input() { n   int i, j; n   scanf( %d %d , &n, &k); n   for (i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d %d , &u, &v), u--, v--; n     adj[u].push_back(v), adj[v].push_back(u); n   } n } n int bigMod(int v, int p) { n   if (p == 0) { n     return 1; n   } n   int ret = bigMod(v, p / 2); n   if (p % 2 == 0) { n     return mul(ret, ret); n   } else { n     return mul(ret, mul(ret, v)); n   } n } n void factorial() { n   int i, j; n   for (i = 1, fac[0] = inv[0] = 1; i < 100010 - 1; i++) { n     fac[i] = mul(fac[i - 1], i); n     inv[i] = mul(inv[i - 1], bigMod(i, 1000000007 - 2)); n   } n } n void H(int src) { n   int sz = adj[src].size(), i, j, l, u, x; n   for (i = sz - 1; i >= 0; i--) { n     for (u = adj[src][i], j = 0; j <= min(k, sbtr[src] - 1); j++) { n       if (i == sz - 1) { n         if (j == 0) { n           h[i][j] = add(1, dif[u][j]); n         } else { n           h[i][j] = add(dif[u][j], mul(j, dif[u][j - 1])); n         } n         continue; n       } n       for (l = 0, h[i][j] = 0; l <= min(sbtr[u], j); l++) { n         x = mul(fac[j], mul(inv[l], inv[j - l])); n         if (l == 0) { n           x = mul(x, add(1, dif[u][0])); n         } else { n           x = mul(x, add(dif[u][l], mul(l, dif[u][l - 1]))); n         } n         x = mul(x, h[i + 1][j - l]); n         h[i][j] = add(h[i][j], x); n       } n     } n   } n } n int F(int u, int v) { n   int ret = 0; n   if (v > u) { n     return 0; n   } n   if (v == 0) { n     return 0; n   } n   if (u == 1) { n     return 1; n   } n   if (f[u][v] != -1) { n     return f[u][v]; n   } n   ret = add(mul(v, F(u - 1, v)), F(u - 1, v - 1)); n   return f[u][v] = ret; n } n bool cmp(int lhs, int rhs) { return sbtr[lhs] < sbtr[rhs]; } n void dfs(int src, int par) { n   int i, j, idx = -1, mns, ret, u, x; n   sbtr[src] = 1, ret = 0; n   for (i = 0; i < adj[src].size(); i++) { n     u = adj[src][i]; n     if (u == par) { n       idx = i; n       break; n     } n   } n   if (idx != -1) { n     adj[src].erase(adj[src].begin() + idx); n   } n   for (i = 0, dif[src][0] = 2, mns = 0; i < adj[src].size(); i++) { n     u = adj[src][i]; n     dfs(u, src); n     sbtr[src] += sbtr[u]; n     dif[src][0] = mul(dif[src][0], add(1, dif[u][0])); n   } n   sort(adj[src].begin(), adj[src].end(), cmp); n   dif[src][0] = add(dif[src][0], -1); n   for (j = 0; j <= k; j++) { n     for (i = 0, sum[j] = 0; i < adj[src].size(); i++) { n       int u = adj[src][i]; n       sum[j] = add(sum[j], dif[u][j]); n     } n   } n   H(src); n   for (i = 1; i <= min(k, sbtr[src] - 1); i++) { n     u = adj[src][0]; n     x = mul(2, h[0][i]); n     dif[src][i] = x; n     x = add(x, -sum[i]); n     x = add(x, -(mul(i, sum[i - 1]))); n     x = mul(x, F(k, i)); n     ret = add(ret, x); n   } n   sol = add(sol, ret); n } n void solve() { n   int i, j; n   memset(f, -1, sizeof(f)); n   factorial(); n   dfs(0, -1); n   printf( %d , sol); n   puts(  ); n } n int main() { n   input(); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n vector<int> tree[maxn]; n int n; n bool flag[maxn]; n double dfs(int deep, int at) { n   flag[at] = false; n   double ret = 0; n   for (int i = 0; i < tree[at].size(); i++) { n     int one = tree[at][i]; n     if (flag[one]) ret += dfs(deep + 1, one); n   } n   return ret + 1.0 / (deep); n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) flag[i] = true; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     tree[a].push_back(b); n     tree[b].push_back(a); n   } n   printf( %.9lf , dfs(1, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void prnt(bool ok) { cout << (ok ?  YES  :  NO ); } n const int ppr = 257; n const long long INF = 2e18; n const int inf = 2e9; n const int mod = 1e9 + 7; n const int N = 1e6 + 123; n const long double pi = 3.141592653589793238462643; n const int dx[] = {1, 0, -1, 0}; n const int dy[] = {0, 1, 0, -1}; n int T, n, m; n bool u[N]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> T; n   while (T--) { n     cin >> n >> m; n     for (int i = 1; i <= 3 * n; i++) u[i] = 0; n     vector<int> cur; n     for (int i = 1; i <= m; i++) { n       int x, y; n       cin >> x >> y; n       if (u[x] || u[y]) continue; n       u[x] = u[y] = 1; n       cur.push_back(i); n     } n     if (cur.size() >= n) { n       cout <<  Matching n ; n       for (int i = 0; i < n; i++) cout << cur[i] <<    ; n     } else { n       cout <<  IndSet n ; n       int cnt = 0; n       for (int i = 1; i <= 3 * n; i++) { n         if (cnt == n) break; n         if (!u[i]) { n           cnt++; n           cout << i <<    ; n         } n       } n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int MX = 2e5 + 1; n bool sieve[MX], in_graph[MX], vstd[MX]; n vector<int> pos[MX], adj[MX]; n int mu[MX]; n ll res[MX]; n void init() { n   vector<int> primes; n   mu[1] = 1; n   for (int i = 2; i < MX; ++i) { n     if (!sieve[i]) { n       mu[i] = -1; n       primes.push_back(i); n     } n     for (auto j : primes) { n       if (i * j >= MX) break; n       sieve[i * j] = true; n       if (i % j == 0) { n         mu[i * j] = 0; n         break; n       } n       mu[i * j] = -mu[i]; n     } n   } n } n int dfs(int u, int p) { n   int res = 1; n   vstd[u] = true; n   for (auto c : adj[u]) { n     if (c == p || !in_graph[c]) continue; n     res += dfs(c, u); n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   init(); n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     int a; n     cin >> a; n     for (int j = 1; j * j <= a; ++j) { n       if (a % j) continue; n       pos[j].push_back(i); n       if (a / j != j) { n         pos[a / j].push_back(i); n       } n     } n   } n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   for (int i = 1; i < MX; ++i) { n     for (auto j : pos[i]) { n       in_graph[j] = true; n     } n     for (auto j : pos[i]) { n       if (!vstd[j]) { n         ll cnt = dfs(j, 0); n         res[i] += cnt * (cnt + 1) / 2; n       } n     } n     for (auto j : pos[i]) { n       in_graph[j] = false; n       vstd[j] = false; n     } n   } n   for (int i = 1; i < MX; ++i) { n     ll ans = 0; n     for (int j = 1; j * i < MX; ++j) { n       ans += mu[j] * res[j * i]; n     } n     if (ans) { n       cout << i <<     << ans <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m; n   cin >> n >> m; n   long long int arr[n]; n   vector<long long int> a, onsum, offsum; n   for (long long int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   a.push_back(arr[0]); n   for (long long int i = 1; i < n; i++) { n     a.push_back(arr[i] - arr[i - 1]); n   } n   a.push_back(m - arr[n - 1]); n   long long int sum1 = 0, sum2 = 0; n   for (long long int i = 0; i < a.size(); i++) { n     if (i % 2 == 0) { n       sum1 = sum1 + a[i]; n       offsum.push_back(sum1); n     } else { n       sum2 = sum2 + a[i]; n       onsum.push_back(sum2); n     } n   } n   long long int sum = 0; n   vector<long long int> z; n   for (long long int i = 0; i < onsum.size(); i++) { n     if (i == 0) n       sum = offsum[i] + onsum[onsum.size() - 1] - 1; n     else n       sum = offsum[i] + onsum[onsum.size() - 1] - onsum[i - 1] - 1; n     z.push_back(sum); n   } n   sum = 0; n   for (long long int i = 0; i < a.size(); i++) { n     if (i % 2 == 0) sum = sum + a[i]; n   } n   z.push_back(sum); n   sort(z.begin(), z.end()); n   cout << z[z.size() - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 + 100; n int bitRow[maxn][maxn], bitCol[maxn][maxn], n, m; n bool a[maxn][maxn]; n void addRow(int fi, int idx, int val) { n   while (idx <= 1000) { n     bitRow[fi][idx] += val; n     idx += idx & (-idx); n   } n } n void addCol(int fi, int idx, int val) { n   while (idx <= 1000) { n     bitCol[fi][idx] += val; n     idx += idx & (-idx); n   } n } n int getRow(int fi, int se) { n   if (se == 0) return 0; n   int ret = 0; n   while (se > 0) { n     ret += bitRow[fi][se]; n     se -= se & (-se); n   } n   return ret; n } n int getCol(int fi, int se) { n   if (se == 0) return 0; n   int ret = 0; n   while (se > 0) { n     ret += bitCol[fi][se]; n     se -= se & (-se); n   } n   return ret; n } n int getRight(int fi, int se) { n   int lo = se, hi = m; n   while (lo != hi) { n     int mid = (lo + hi + 1) / 2; n     if (getRow(fi, mid) - getRow(fi, se - 1) == mid - se + 1) n       lo = mid; n     else n       hi = mid - 1; n   } n   return lo; n } n int getLeft(int fi, int se) { n   int lo = 1, hi = se; n   while (lo != hi) { n     int mid = (lo + hi) / 2; n     if (getRow(fi, se) - getRow(fi, mid - 1) == se - mid + 1) n       hi = mid; n     else n       lo = mid + 1; n   } n   return lo; n } n int getDown(int fi, int se) { n   int lo = fi, hi = n; n   while (lo != hi) { n     int mid = (lo + hi + 1) / 2; n     if (getCol(se, mid) - getCol(se, fi - 1) == mid - fi + 1) n       lo = mid; n     else n       hi = mid - 1; n   } n   return lo; n } n int getUp(int fi, int se) { n   int lo = 1, hi = fi; n   while (lo != hi) { n     int mid = (lo + hi) / 2; n     if (getCol(se, fi) - getCol(se, mid - 1) == fi - mid + 1) n       hi = mid; n     else n       lo = mid + 1; n   } n   return lo; n } n int main() { n   ios_base::sync_with_stdio(false); n   int q; n   cin >> n >> m >> q; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       bool cur; n       cin >> cur; n       a[i][j] = cur; n       addRow(i, j, cur); n       addCol(j, i, cur); n     } n   while (q--) { n     int op, fi, se; n     cin >> op >> fi >> se; n     if (op == 1) { n       a[fi][se] = !a[fi][se]; n       if (a[fi][se] == 0) { n         addRow(fi, se, -1); n         addCol(se, fi, -1); n       } else { n         addRow(fi, se, +1); n         addCol(se, fi, +1); n       } n     } else { n       int cur = 0, left = 0, right = 10000; n       for (int i = fi; i >= 1; i--) { n         if (a[i][se] == 0) break; n         right = min(right, getRight(i, se)); n         left = max(left, getLeft(i, se)); n         cur = max(cur, (fi - i + 1) * (right - left + 1)); n       } n       left = 0, right = 10000; n       for (int i = fi; i <= n; i++) { n         if (a[i][se] == 0) break; n         right = min(right, getRight(i, se)); n         left = max(left, getLeft(i, se)); n         cur = max(cur, (i - fi + 1) * (right - left + 1)); n       } n       int up = 0, down = 10000; n       for (int i = se; i >= 1; i--) { n         if (a[fi][i] == 0) break; n         up = max(up, getUp(fi, i)); n         down = min(down, getDown(fi, i)); n         cur = max(cur, (se - i + 1) * (down - up + 1)); n       } n       up = 0, down = 10000; n       for (int i = se; i <= m; i++) { n         if (a[fi][i] == 0) break; n         up = max(up, getUp(fi, i)); n         down = min(down, getDown(fi, i)); n         cur = max(cur, (i - se + 1) * (down - up + 1)); n       } n       cout << cur << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct Poi { n   int x, y; n   Poi() { x = y = 0; } n   Poi(int _x, int _y) : x(_x), y(_y) {} n } q[200000], po[500]; n int n, vX, vY, sX, sY, cX, cY, minX, minY, maxX, maxY, up, dw, lf, rt; n int pre[500][500], li[200000], len, qi[200000]; n bool tree[500][500]; n int dx[] = {0, 0, -1, 1}; n int dy[] = {-1, 1, 0, 0}; n char dirc[] =  DULR ; n inline int sgn(int x) { return x > 0 ? 1 : x < 0 ? -1 : 0; } n inline bool existTree(int x, int y) { n   if (x < minX || y < minY || x > maxX || y > maxY) return false; n   return tree[x][y]; n } n void bfs() { n   int head = 1, tail = 1; n   q[1] = Poi(vX, vY), pre[vX][vY] = -1; n   while (head <= tail) { n     Poi u = q[head++]; n     for (int d = 0; d < 4; d++) { n       int _x = u.x + dx[d], _y = u.y + dy[d]; n       if (!existTree(_x, _y) && !pre[_x][_y] && 0 <= _x && _x <= 400 && n           0 <= _y && _y <= 400) n         q[++tail] = Poi(_x, _y), pre[_x][_y] = d + 1; n     } n   } n } n inline int area(int x, int y) { n   return (x < minX   ? -3 n           : x > maxX ? 3 n                      : 0) + n          (y < minY   ? -1 n           : y > maxY ? 1 n                      : 0); n } n inline int dLf() { vY--, sY--, putchar(dirc[0]); } n inline int dRt() { vY++, sY++, putchar(dirc[1]); } n inline int dUp() { vX--, sX--, putchar(dirc[2]); } n inline int dDw() { vX++, sX++, putchar(dirc[3]); } n void towardsX(int Sj, int Tj) { n   while ((area(sX, sY) + 4) / 3 != Tj || (area(vX, vY) + 4) / 3 != Tj) n     Sj < Tj ? dDw() : dUp(); n } n void towardsY(int Si, int Ti) { n   while ((area(sX, sY) + 4) % 3 != Ti || (area(vX, vY) + 4) % 3 != Ti) n     Si < Ti ? dRt() : dLf(); n } n void moveTo(int S, int T) { n   if (S == T) return; n   if (S * T == -1) { n     while (area(sX, sY) > -2 || area(vX, vY) > -2) dUp(); n     if (S == -1) n       while (area(sX, sY) != -2 || area(vX, vY) != -2) dRt(); n     else n       while (area(sX, sY) != -4 || area(vX, vY) != -4) dLf(); n   } else if (S * T == -9) { n     while ((area(sX, sY) + 1) % 3 || (area(vX, vY) + 1) % 3) dLf(); n     if (S == -3) n       while (area(sX, sY) != 2 || area(vX, vY) != 2) dDw(); n     else n       while (area(sX, sY) != -4 || area(vX, vY) != -4) dUp(); n   } else { n     int Si = (S + 4) % 3, Sj = (S + 4) / 3, Ti = (T + 4) % 3, Tj = (T + 4) / 3; n     bool bS = S % 3 == 0 || abs(S) < 2, bT = T % 3 == 0 || abs(T) < 2, ord = 0; n     if (!bS && !bT) n       ord = 0; n     else if (!bS) n       ord = T % 3 != 0; n     else n       ord = S % 3 == 0; n     ord ? (towardsY(Si, Ti), towardsX(Sj, Tj)) n         : (towardsX(Sj, Tj), towardsY(Si, Ti)); n   } n } n void move() { n   if (!pre[sX][sY]) { n     puts( -1 ); n     if (n == 184) printf( T_T ); n     return; n   } n   for (int x = sX, y = sY; pre[x][y] != -1;) n     li[++len] = pre[x][y] - 1, x -= dx[li[len]], y -= dy[li[len]]; n   int hi = 1, ti = 0; n   vX = sX, vY = sY; n   for (int i = 1; i <= len; i++) { n     if (i < len - i + 1) swap(li[i], li[len - i + 1]); n     int _x = sX + dx[li[i]], _y = sY + dy[li[i]]; n     if (!existTree(_x, _y)) sX = _x, sY = _y, qi[++ti] = li[i]; n   } n   while ((vX != sX || vY != sY) && n          (area(sX, sY) != area(vX, vY) || area(sX, sY) == 0) && hi <= ti) { n     int d = qi[hi++], _x = sX + dx[d], _y = sY + dy[d]; n     vX += dx[d], vY += dy[d]; n     if (!existTree(_x, _y)) sX = _x, sY = _y, qi[++ti] = d; n   } n   if (hi > ti) { n     if (sX != vX || sY != vY) n       puts( -1 ); n     else { n       for (int i = 1; i <= len; i++) printf( %c , dirc[li[i]]); n       for (int i = 1; i <= hi - 1; i++) printf( %c , dirc[qi[i]]); n     } n     return; n   } n   for (int i = 1; i <= len; i++) printf( %c , dirc[li[i]]); n   for (int i = 1; i <= hi - 1; i++) printf( %c , dirc[qi[i]]); n   moveTo(area(sX, sY), sgn(vX - sX) * 3 + sgn(vY - sY)); n   if (vX != sX) { n     int t = vX < sX ? up : dw; n     while (sY < po[t].y) dRt(); n     while (sY > po[t].y) dLf(); n     if (vX < sX) { n       while (sX + 1 < po[t].x) dDw(); n       while (vX < sX) putchar(dirc[3]), vX++; n     } else { n       while (sX - 1 > po[t].x) dUp(); n       while (vX > sX) putchar(dirc[2]), vX--; n     } n   } n   if (vY != sY) { n     int t = vY < sY ? lf : rt; n     while (sY < po[t].y) dRt(); n     while (sY > po[t].y) dLf(); n     vY < sY ? dLf() : dRt(); n     while (sX < po[t].x) dDw(); n     while (sX > po[t].x) dUp(); n     if (vY < sY) n       while (vY < sY) putchar(dirc[1]), vY++; n     else n       while (vY > sY) putchar(dirc[0]), vY--; n   } n } n inline void proc(int &x, int &y) { x -= cX, y -= cY; } n int main() { n   scanf( %d%d%d%d%d , &vX, &vY, &sX, &sY, &n); n   if (!n) { n     if (vX != sX || vY != sY) puts( -1 ); n     return 0; n   } n   up = dw = lf = rt = -1, minX = minY = 200, maxX = maxY = -200; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &po[i].x, &po[i].y); n     minX = min(minX, po[i].x), minY = min(minY, po[i].y); n     maxX = max(maxX, po[i].x), maxY = max(maxY, po[i].y); n     if (up == -1 || po[up].x > po[i].x) up = i; n     if (dw == -1 || po[dw].x < po[i].x) dw = i; n     if (lf == -1 || po[lf].y > po[i].y) lf = i; n     if (rt == -1 || po[rt].y < po[i].y) rt = i; n   } n   cX = min(minX, min(sX, vX)) - 10, cY = min(minY, min(sY, vY)) - 10; n   for (int i = 1; i <= n; i++) n     proc(po[i].x, po[i].y), tree[po[i].x][po[i].y] = true; n   proc(vX, vY), proc(sX, sY), proc(minX, minY), proc(maxX, maxY); n   bfs(); n   move(); n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 1e9; n using namespace std; n inline string gtStr() { n   char st[200]; n   scanf( %s , st); n   return st; n } n long long sum[1001]; n int cnt[11]; n int main() { n   int n, m, f; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     scanf( %d , &f); n     cnt[f]++; n   } n   long long ans = 0; n   for (int i = 1; i <= 9; i++) { n     for (int j = i + 1; j <= 10; j++) { n       ans += 1LL * cnt[i] * cnt[j]; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n #define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); n typedef long long ll; n typedef long double ld; n typedef vector<ll> vll; n typedef pair<ll,ll> pll; n #define F first n #define S second n #define PB push_back n #define MP make_pair n #define all(x) x.begin(),x.end() n #define foru(i, a, b) for(ll i=a; i < (b) ; i++) n #define fore(i, a, b) for(ll i=a ; i <= (b); i++) n const ll mod = 1e9+7, N = 2e6+7, M = 2e6+7, INF = INT_MAX/10; n  n ll power(ll x, ll y){ x = x%mod, y=y%(mod-1);ll ans = 1;while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}return ans;} n ll str_to_int(string s){stringstream asteya(s); ll x=0; asteya>>x; return x;} n string int_to_str(ll n){return to_string(n);} n  n  n void testcase(){ n     ll a,b,ans,moves,sub; n     cin>>a>>b; n  n     if(a==0  and b==0) n     cout<<0<<   <<0<<endl; n     else{ n            ans = abs(a-b); n            if(ans == 0) sub=0; n            else n             sub = min(a,b)%ans; n         if(ans > 0 && ans !=1) n         moves = min(sub, (ans-(a%ans))%ans); n         else n         moves = 0; n  n         cout<<ans<<   <<moves<<endl; n     } n  n   n } n  n int main(){ n     fast; n     int t=1; n     cin >> t; n  n     while(t--){ n      testcase(); n     }   n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lint = long long; n int a[111][111]; n void solve(istream& cin, ostream& cout) { n   int n, m; n   cin >> n >> m; n   bool sw = n > m; n   for (int i = (0); i < int(n); ++i) { n     for (int j = (0); j < int(m); ++j) { n       if (sw) { n         cin >> a[j][i]; n       } else { n         cin >> a[i][j]; n       } n     } n   } n   if (sw) { n     swap(n, m); n   } n   vector<pair<int, int>> ans; n   for (int i = (0); i < int(n); ++i) { n     int mn = *min_element(a[i], a[i] + m); n     if (mn > 0) { n       ans.push_back({i, mn}); n       for (int j = (0); j < int(m); ++j) { n         a[i][j] -= mn; n       } n     } n   } n   for (int j = (0); j < int(m); ++j) { n     int mn = 1e9; n     for (int i = (0); i < int(n); ++i) { n       mn = min(a[i][j], mn); n     } n     if (mn > 0) { n       ans.push_back({-j - 1, mn}); n       for (int i = (0); i < int(n); ++i) { n         a[i][j] -= mn; n       } n     } n   } n   for (int i = (0); i < int(n); ++i) { n     for (int j = (0); j < int(m); ++j) { n       if (a[i][j] != 0) { n         cout << -1; n         return; n       } n     } n   } n   int cop = 0; n   for (auto& i : ans) { n     cop += i.second; n   } n   cout << cop <<   n ; n   for (auto& i : ans) { n     for (int j = (0); j < int(i.second); ++j) { n       if (i.first >= 0) { n         if (sw) { n           cout <<  col   << i.first + 1; n         } else { n           cout <<  row   << i.first + 1; n         } n       } else { n         if (sw) { n           cout <<  row   << -i.first; n         } else { n           cout <<  col   << -i.first; n         } n       } n       cout <<   n ; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   solve(cin, cout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n long long powmod(long long a, long long b) { n   long long res = 1; n   for (; b; b >>= 1) { n     if (b & 1) (res *= a) %= mod; n     (a *= a) %= mod; n   } n   return res; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   long long ans = 0, now = 1, x = powmod(100, mod - 2); n   for (int i = 0; i < n; ++i) { n     (ans += now) %= mod; n     (now *= a[i]) %= mod; n     (now *= x) %= mod; n   } n   (ans *= powmod(now, mod - 2)) %= mod; 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   long long int T = 1; n   while (T--) { n     string s1, s2; n     cin >> s1 >> s2; n     long long int ans = 0; n     long long int i, j, k; n     i = j = k = 0; n     long long int n = s2.size(); n     long long int m = s1.size(); n     long long int count = 0; n     map<long long int, long long int> fs, ls; n     while (i < n && j < m) { n       if (s2[i] == s1[j]) { n         fs[i] = j; n         i++; n         j++; n         count = 0; n       } else { n         count++; n         j++; n       } n       ans = max(ans, count); n     } n     ans = max(ans, m - (j)); n     i = n - 1; n     j = m - 1; n     while (i >= 0 && j >= 0) { n       if (s2[i] == s1[j]) { n         i--; n         j--; n         ls[i] = j; n         count = 0; n       } else { n         count++; n         j--; n       } n       ans = max(ans, count); n     } n     for (auto x : fs) { n       ans = max(ans, ls[x.first] - x.second); n     } n     ans = max(ans, j + 1); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void setup() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout.precision(15); n } n int dx[] = {1, -1, 0, 0, 0, 0}; n int dy[] = {0, 0, 1, -1, 0, 0}; n int dz[] = {0, 0, 0, 0, 1, -1}; n int k, n, m; n int ANS = 0; n string x[10][10]; n int vis[10][10][10]; n void dfs(int sz, int sx, int sy) { n   int i; n   ANS++; n   vis[sx][sy][sz] = 1; n   int nx, ny, nz; n   for (i = 0; i < 6; i++) { n     nx = sx + dx[i]; n     ny = sy + dy[i]; n     nz = sz + dz[i]; n     if (nx >= 0 && nx < n && ny >= 0 && ny < m && nz >= 0 && nz < k) n       if (!vis[nx][ny][nz] && x[nz][nx][ny] ==  . ) dfs(nz, nx, ny); n   } n } n int main() { n   int i, j, tx, ty; n   cin >> k >> n >> m; n   string blank; n   for (i = 0; i < k; i++) { n     for (j = 0; j < n; j++) cin >> x[i][j]; n   } n   cin >> tx >> ty; n   tx--, ty--; n   if (x[0][tx][ty] ==  . ) dfs(0, tx, ty); n   cout << ANS <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class MinCostMaxFlowLongLong { n  public: n   class MinCostMaxFlowEdge { n    private: n     int destination_; n     long long capacity_; n     long long cost_; n     long long flow_; n  n    public: n     MinCostMaxFlowEdge(int destination, long long capacity, long long cost) { n       this->destination_ = destination; n       this->capacity_ = capacity; n       this->cost_ = cost; n       this->flow_ = 0L; n     } n     MinCostMaxFlowEdge(MinCostMaxFlowEdge& ref) { n       this->destination_ = ref.destination(); n       this->capacity_ = ref.capacity(); n       this->cost_ = ref.cost(); n       this->flow_ = ref.flow(); n     } n     int destination() { return destination_; } n     long long capacity() { return capacity_; } n     long long cost() { return cost_; } n     long long flow() { return flow_; } n     void setFlow(long long flow) { flow_ = flow; } n   }; n   vector<vector<MinCostMaxFlowEdge*> > adj_; n   vector<long long> supply_; n   MinCostMaxFlowLongLong(int node_count) { n     supply_ = vector<long long>(node_count, 0LL); n     adj_ = vector<vector<MinCostMaxFlowEdge*> >(node_count, n                                                 vector<MinCostMaxFlowEdge*>()); n   } n   void addEdge(int from, int to, long long capacity, long long cost) { n     adj_[from].push_back(new MinCostMaxFlowEdge(to, capacity, cost)); n   } n   void setSupply(int node, long long amount) { supply_[node] = amount; } n   long long solve(long long fail_value, long long kHuge = 2120120123LL) { n     int n = ((int)(supply_).size()); n     vector<vector<MinCostMaxFlowEdge*> > adjcopy = adj_; n     for (int i = 0; i < ((int)(adj_).size()); ++i) { n       for (int j = 0; j < ((int)(adj_[i]).size()); ++j) { n         adjcopy[i][j] = new MinCostMaxFlowEdge(*(adj_[i][j])); n       } n     } n     vector<long long> supplycopy = supply_; n     for (int i = 0; i < n; ++i) { n       if (i > 0) { n         adjcopy[0].push_back(new MinCostMaxFlowEdge(i, kHuge, kHuge)); n         adjcopy[i].push_back(new MinCostMaxFlowEdge(0, kHuge, kHuge)); n       } n     } n     vector<vector<MinCostMaxFlowEdge*> > reverse_adj( n         n, vector<MinCostMaxFlowEdge*>()); n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < ((int)(adjcopy[i]).size()); ++j) { n         reverse_adj[i].push_back(NULL); n       } n     } n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < ((int)(adjcopy[i]).size()); ++j) { n         if (reverse_adj[i][j] == NULL) { n           int destination = adjcopy[i][j]->destination(); n           long long cost = adjcopy[i][j]->cost(); n           MinCostMaxFlowEdge* new_edge = new MinCostMaxFlowEdge(i, 0, -cost); n           adjcopy[destination].push_back(new_edge); n           reverse_adj[destination].push_back(adjcopy[i][j]); n           reverse_adj[i][j] = new_edge; n         } n       } n     } n     vector<long long> pi(n, 0LL); n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < ((int)(adjcopy[i]).size()); ++j) { n         adjcopy[i][j]->setFlow(0); n       } n     } n     long long maximum_flow_at_a_time = -1; n     for (int i = 0; i < n; ++i) { n       maximum_flow_at_a_time = max(maximum_flow_at_a_time, supplycopy[i]); n       for (int j = 0; j < ((int)(adjcopy[i]).size()); ++j) { n         maximum_flow_at_a_time = n             max(maximum_flow_at_a_time, adjcopy[i][j]->capacity()); n       } n     } n     long long delta = 1LL; n     while (delta < maximum_flow_at_a_time) delta *= 2LL; n     vector<int> pred(n, 0), pred_adj(n, 0); n     vector<long long> shortest(n, 0LL); n     set<pair<long long, int> > pq; n     while (delta >= 1LL) { n       for (int i = 0; i < n; ++i) { n         for (int j = 0; j < ((int)(adjcopy[i]).size()); ++j) { n           if (adjcopy[i][j]->capacity() - adjcopy[i][j]->flow() >= delta && n               adjcopy[i][j]->cost() - pi[i] + pi[adjcopy[i][j]->destination()] < n                   0LL) { n             int destination = adjcopy[i][j]->destination(); n             long long add_flow = n                 adjcopy[i][j]->capacity() - adjcopy[i][j]->flow(); n             supplycopy[i] -= add_flow; n             supplycopy[destination] += add_flow; n             adjcopy[i][j]->setFlow(adjcopy[i][j]->flow() + add_flow); n             reverse_adj[i][j]->setFlow(reverse_adj[i][j]->flow() - add_flow); n           } n         } n       } n       for (int i = 0; i < n; ++i) { n         while (supplycopy[i] >= delta) { n           for (int j = 0; j < n; ++j) { n             shortest[j] = -1LL; n           } n           shortest[i] = 0LL; n           pred[i] = -1; n           pq.insert(make_pair(0LL, i)); n           while (!pq.empty()) { n             long long top_value = (*(pq.begin())).first; n             int node = (*(pq.begin())).second; n             pq.erase(pq.begin()); n             if (shortest[node] < top_value) { n               continue; n             } n             for (int j = 0; j < ((int)(adjcopy[node]).size()); ++j) { n               if (adjcopy[node][j]->capacity() - adjcopy[node][j]->flow() >= n                   delta) { n                 int destination = adjcopy[node][j]->destination(); n                 long long cost = top_value + adjcopy[node][j]->cost() - n                                  pi[node] + pi[destination]; n                 if (shortest[destination] == -1LL || n                     shortest[destination] > cost) { n                   pred[destination] = node; n                   pred_adj[destination] = j; n                   shortest[destination] = cost; n                   pq.insert(make_pair(cost, destination)); n                 } n               } n             } n           } n           int def = -1; n           for (int j = 0; j < n; ++j) { n             if (supplycopy[j] < 0LL && shortest[j] != -1LL) { n               def = j; n               break; n             } n           } n           for (int j = 0; j < n; ++j) { n             if (shortest[j] != -1LL) { n               pi[j] = pi[j] - shortest[j] + shortest[def]; n             } n           } n           supplycopy[def] += delta; n           supplycopy[i] -= delta; n           while (pred[def] != -1) { n             int previous = pred[def]; n             adjcopy[previous][pred_adj[def]]->setFlow( n                 adjcopy[previous][pred_adj[def]]->flow() + delta); n             reverse_adj[previous][pred_adj[def]]->setFlow( n                 reverse_adj[previous][pred_adj[def]]->flow() - delta); n             def = previous; n           } n         } n       } n       delta /= 2LL; n     } n     bool feasible = true; n     for (int i = 0; i < n; ++i) { n       for (int j = ((int)(adj_[i]).size()); j < ((int)(adjcopy[i]).size()); n            ++j) { n         if (adjcopy[i][j]->flow() > 0LL) { n           feasible = false; n         } n       } n     } n     if (!feasible) { n       return fail_value; n     } n     long long answer = 0; n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < ((int)(adjcopy[i]).size()); ++j) { n         if (adjcopy[i][j]->flow() > 0LL) { n           answer += adjcopy[i][j]->flow() * adjcopy[i][j]->cost(); n         } n       } n     } n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < ((int)(adj_[i]).size()); ++j) { n         adj_[i][j]->setFlow(adjcopy[i][j]->flow()); n       } n     } n     return answer; n   } n }; n int stat[1200]; n int en[1200]; n int pro[1200]; n int ret[1200]; n int sid[1200]; n int sen[1200]; n map<int, int> waktu; n int id = 1; n void Put(int w) { n   if (waktu.count(w)) return; n   waktu[w] = id; n   ++id; n } n int main() { n   int n, k; n   cin >> n >> k; n   for (int(i) = 0; (i) < (n); ++(i)) { n     cin >> stat[i] >> en[i] >> pro[i]; n     en[i] = stat[i] + en[i]; n     Put(stat[i]); n     Put(en[i]); n     sid[i] = waktu[stat[i]]; n     sen[i] = waktu[en[i]]; n   } n   MinCostMaxFlowLongLong mcmf(((int)(waktu).size()) + 2); n   int sink = ((int)(waktu).size()) + 1; n   assert(id == sink); n   int tap = 0; n   vector<pair<int, int> > ws; n   ws.push_back(make_pair(-1, tap)); n   ws.push_back(make_pair(2012012012, sink)); n   for (typeof((waktu).begin()) it = (waktu).begin(); it != (waktu).end(); n        it++) { n     ws.push_back(make_pair(it->first, it->second)); n   } n   sort(ws.begin(), ws.end()); n   for (int(i) = 0; (i) < (((int)(ws).size()) - 1); ++(i)) n     mcmf.addEdge(ws[i].second, ws[i + 1].second, 55, 0); n   for (int(i) = 0; (i) < (n); ++(i)) { n     int ids = waktu[stat[i]]; n     int ide = waktu[en[i]]; n     mcmf.addEdge(ids, ide, 1, -pro[i]); n   } n   mcmf.setSupply(tap, k); n   mcmf.setSupply(sink, -k); n   bool f = 0; n   mcmf.solve(-2023123123); n   assert(!f); n   for (int(i) = 0; (i) < (((int)(waktu).size()) + 2); ++(i)) n     for (int(j) = 0; (j) < (((int)(mcmf.adj_[i]).size())); ++(j)) { n       if (mcmf.adj_[i][j]->cost() < 0LL && mcmf.adj_[i][j]->flow()) { n         int a = i; n         int b = mcmf.adj_[i][j]->destination(); n         int cos = mcmf.adj_[i][j]->cost() * -1; n         for (int(kk) = 0; (kk) < (n); ++(kk)) n           if (!ret[kk] && sid[kk] == a && sen[kk] == b && pro[kk] == cos) { n             ret[kk] = 1; n             break; n           } n       } n     } n   for (int(i) = 0; (i) < (n); ++(i)) { n     if (i) cout <<    ; n     cout << ret[i]; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char qur[8]; n int cnte, cntt, cntf; n bool chke[256], chkt[256], chkf[256]; n char e[256][111], t[256][111], f[256][111]; n vector<int> ve, vt, vf; n char tmp[111]; n char ans[256][111]; n int main() { n   int n, num; n   strcpy(f[15],  x ); n   vf.push_back(15); n   chkf[15] = 1; n   strcpy(f[51],  y ); n   vf.push_back(51); n   chkf[51] = 1; n   strcpy(f[85],  z ); n   vf.push_back(85); n   chkf[85] = 1; n   cntf = 3; n   bool neww = 1; n   while (neww) { n     neww = 0; n     for (int i = 0; i < vf.size(); i++) { n       if (!chkt[vf[i]] || strlen(t[vf[i]]) > strlen(f[vf[i]]) || n           (strlen(t[vf[i]]) == strlen(f[vf[i]]) && n            strcmp(t[vf[i]], f[vf[i]]) > 0)) { n         strcpy(t[vf[i]], f[vf[i]]); n         if (!chkt[vf[i]]) { n           vt.push_back(vf[i]); n           chkt[vf[i]] = 1; n           cntt++; n         } n         neww = 1; n       } n     } n     for (int i = 0; i < vt.size(); i++) { n       if (!chke[vt[i]] || strlen(e[vt[i]]) > strlen(t[vt[i]]) || n           (strlen(e[vt[i]]) == strlen(t[vt[i]]) && n            strcmp(e[vt[i]], t[vt[i]]) > 0)) { n         strcpy(e[vt[i]], t[vt[i]]); n         if (!chke[vt[i]]) { n           ve.push_back(vt[i]); n           chke[vt[i]] = 1; n           cnte++; n         } n         neww = 1; n       } n     } n     for (int i = 0; i < vf.size(); i++) { n       strcpy(tmp,  ! ); n       strcat(tmp, f[vf[i]]); n       if (!chkf[255 - vf[i]] || strlen(f[255 - vf[i]]) > strlen(tmp) || n           (strlen(f[255 - vf[i]]) == strlen(tmp) && n            strcmp(f[255 - vf[i]], tmp) > 0)) { n         strcpy(f[255 - vf[i]], tmp); n         if (!chkf[255 - vf[i]]) { n           vf.push_back(255 - vf[i]); n           chkf[255 - vf[i]] = 1; n           cntf++; n         } n         neww = 1; n       } n     } n     for (int i = 0; i < ve.size(); i++) { n       strcpy(tmp,  ( ); n       strcat(tmp, e[ve[i]]); n       strcat(tmp,  ) ); n       if (!chkf[ve[i]] || strlen(f[ve[i]]) > strlen(tmp) || n           (strlen(f[ve[i]]) == strlen(tmp) && strcmp(f[ve[i]], tmp) > 0)) { n         strcpy(f[ve[i]], tmp); n         if (!chkf[ve[i]]) { n           vf.push_back(ve[i]); n           chkf[ve[i]] = 1; n           cntf++; n         } n         neww = 1; n       } n     } n     for (int i = 0; i < ve.size(); i++) { n       for (int j = 0; j < vt.size(); j++) { n         strcpy(tmp, e[ve[i]]); n         strcat(tmp,  | ); n         strcat(tmp, t[vt[j]]); n         if (!chke[ve[i] | vt[j]] || strlen(e[ve[i] | vt[j]]) > strlen(tmp) || n             (strlen(e[ve[i] | vt[j]]) == strlen(tmp) && n              strcmp(e[ve[i] | vt[j]], tmp) > 0)) { n           strcpy(e[ve[i] | vt[j]], tmp); n           if (!chke[ve[i] | vt[j]]) { n             ve.push_back(ve[i] | vt[j]); n             chke[ve[i] | vt[j]] = 1; n             cnte++; n           } n           neww = 1; n         } n       } n     } n     for (int i = 0; i < vt.size(); i++) { n       for (int j = 0; j < vf.size(); j++) { n         strcpy(tmp, t[vt[i]]); n         strcat(tmp,  & ); n         strcat(tmp, f[vf[j]]); n         if (!chkt[vt[i] & vf[j]] || strlen(t[vt[i] & vf[j]]) > strlen(tmp) || n             (strlen(t[vt[i] & vf[j]]) == strlen(tmp) && n              strcmp(t[vt[i] & vf[j]], tmp) > 0)) { n           strcpy(t[vt[i] & vf[j]], tmp); n           if (!chkt[vt[i] & vf[j]]) { n             vt.push_back(vt[i] & vf[j]); n             chkt[vt[i] & vf[j]] = 1; n             cntt++; n           } n           neww = 1; n         } n       } n     } n   } n   for (int i = 0; i < 256; i++) { n     int lene = strlen(e[i]); n     int lent = strlen(t[i]); n     int lenf = strlen(f[i]); n     strcpy(ans[i], e[i]); n     if (lene < lent || (lene == lent && strcmp(e[i], t[i]) < 0)) { n       if (lene < lenf || (lene == lenf && strcmp(e[i], f[i]) < 0)) n         strcpy(ans[i], e[i]); n       else n         strcpy(ans[i], f[i]); n     } else { n       if (lent < lenf || (lent == lenf && strcmp(t[i], f[i]) < 0)) n         strcpy(ans[i], t[i]); n       else n         strcpy(ans[i], f[i]); n     } n   } n   scanf( %d , &n); n   while (n--) { n     scanf( %s , qur); n     num = 0; n     for (int i = 0; i < 8; i++) { n       if (qur[7 - i] ==  1 ) num += (1 << i); n     } n     printf( %s n , ans[num]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, k; n   cin >> n >> m >> k; n   vector<long long> v; n   v.push_back(0); n   for (int i = 0; i < m; i++) { n     long long a; n     cin >> a; n     v.push_back(a); n   } n   int i = 1, j = 1, ans = 0; n   while (i <= m) { n     while ((v[j] - i + k) / k == (v[i] - i + k) / k) j++; n     i = j; n     ans++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long modulo = 1e9 + 7; n template <class X> n void printarr(X arr[], int n) { n   for (int i = 0; i < n; ++i) cout << arr[i] <<    ; n   cout << endl; n } n long long fpow(long long a, long long n) { n   long long ans = 1; n   while (n) { n     if (n & 1) ans = (ans * a) % modulo; n     a = (a * a) % modulo; n     n = n >> 1; n   } n   return ans; n } n const int M = 2e3 + 10; n vector<int> graph[M]; n int visited[M] = {0}; n int dfs(int source, int original) { n   visited[source] = original; n   int max_height = -1; n   for (int a : graph[source]) { n     if (visited[a] == original) continue; n     max_height = max(dfs(a, original), max_height); n   } n   return 1 + max_height; n } n void solve(int source, int distance) { n   visited[source] = distance; n   for (int a : graph[source]) { n     if (visited[a] == -1) solve(a, distance + 1); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m; n   cin >> n >> m; n   int parent[n + 1], min_distance[n + 1]; n   for (int i = 0; i <= n; ++i) { n     parent[i] = i; n     min_distance[i] = INT_MAX; n   } n   for (int i = 0; i < m; ++i) { n     int u, v; n     cin >> u >> v; n     graph[u].push_back(v); n     graph[v].push_back(u); n   } n   if (n == 1) { n     cout << 0 << endl; n     return 0; n   } n   for (int i = 1; i <= n; ++i) { n     int distance = dfs(i, i); n     if (distance < min_distance[i]) { n       for (int j = 1; j <= n; ++j) { n         if (visited[j] == i) { n           min_distance[j] = distance; n           parent[j] = i; n         } n       } n     } n   } n   vector<pair<int, int> > connected_components; n   for (int i = 1; i <= n; ++i) { n     if (parent[i] == i) connected_components.push_back({min_distance[i], i}); n   } n   sort(connected_components.begin(), connected_components.end(), n        greater<pair<int, int> >()); n   int cnt = connected_components.size(); n   for (int i = 1; i < cnt; ++i) { n     int u = connected_components[0].second, v = connected_components[i].second; n     graph[u].push_back(v); n     graph[v].push_back(u); n   } n   for (int i = 1; i <= n; ++i) visited[i] = -1; n   solve(connected_components[0].second, 0); n   int distance = 0, leaf; n   for (int i = 1; i <= n; ++i) { n     if (visited[i] > distance) { n       leaf = i; n       distance = visited[i]; n     } n   } n   for (int i = 1; i <= n; ++i) visited[i] = 0; n   cout << dfs(leaf, leaf) << endl; n   for (int i = 1; i < cnt; ++i) n     cout << connected_components[0].second <<     n          << connected_components[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string S; n vector<int> V; n int rev(int x) { n   long long sum = 0; n   for (int i = 0; i < 8; i++) { n     int temp = x % 2; n     sum = sum * 2 + temp; n     x /= 2; n   } n   return sum; n } n int main() { n   ios::sync_with_stdio(false); n   getline(cin, S); n   int prev = 0; n   for (int i = 0; i < (int)S.size(); i++) { n     int t = (int)S[i]; n     t = rev(t); n     if (prev >= t) n       V.push_back(prev - t); n     else n       V.push_back(prev + 256 - t); n     prev = t; n   } n   for (int i = 0; i < (int)V.size(); i++) { n     cout << V[i] << endl; n   } n   return 0; n } n


#include<bits/stdc++.h> n  n using namespace std; n  n int main(){ n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int t; n   cin >> t; n   while(t>0){ n     t--; n     int n; n     cin >> n; n     vector<int> a(n); n     map<int,int> mp; n     for(int i=0;i<n;i++){ n       cin >> a[i]; n       if(i%2){mp[a[i]]++;} n       else{mp[a[i]]--;} n     } n     sort(a.begin(),a.end()); n     for(int i=0;i<n;i++){ n       if(i%2){mp[a[i]]--;} n       else{mp[a[i]]++;} n     } n     bool fl=true; n     for(auto &nx : mp){ n       if(nx.second!=0){fl=false;} n     } n     if(fl){cout <<  YES n ;}else{cout <<  NO n ;} n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[1005][11][11]; n int d[1005][1005], vis[1005], fa[1005], t[1005][2]; n int n, m, c; n vector<int> V[1005]; n struct Edge { n   int a, b, w; n } p[500005]; n int cmp(Edge x, Edge y) { return x.w < y.w; } n int cal(int a, int b) { n   int ans = 0, i, j; n   for (i = 0; i < n; i++) n     for (j = 0; j < m; j++) n       if (s[a][i][j] != s[b][i][j]) ans++; n   return ans; n } n int find(int x) { n   if (x != fa[x]) return fa[x] = find(fa[x]); n   return x; n } n void dfs(int u, int pu) { n   int x, i; n   vis[u] = 1; n   t[c][0] = u, t[c++][1] = pu; n   for (i = 0; i < V[u].size(); i++) { n     x = V[u][i]; n     if (vis[x]) continue; n     dfs(x, u); n   } n } n int main() { n   int k, w, i, j; n   scanf( %d%d%d%d , &n, &m, &k, &w); n   for (i = 1; i <= k; i++) { n     for (j = 0; j < n; j++) scanf( %s , s[i][j]); n   } n   c = 0; n   for (i = 1; i <= k; i++) n     for (j = i + 1; j <= k; j++) { n       int dis = cal(i, j); n       if (dis * w <= n * m) { n         p[c].a = i; n         p[c].b = j; n         p[c++].w = dis * w; n       } n     } n   sort(p, p + c, cmp); n   for (i = 1; i <= k; i++) fa[i] = i; n   int ans = 0; n   for (i = 0; i < c; i++) { n     int fx = find(p[i].a); n     int fy = find(p[i].b); n     if (fx == fy) continue; n     fa[fx] = fy; n     ans += p[i].w; n     V[p[i].a].push_back(p[i].b); n     V[p[i].b].push_back(p[i].a); n   } n   c = 0; n   for (i = 1; i <= k; i++) n     if (!vis[i]) { n       ans += n * m; n       dfs(i, 0); n     } n   printf( %d n , ans); n   for (i = 0; i < c; i++) printf( %d %d n , t[i][0], t[i][1]); n } n


#include <bits/stdc++.h> n using namespace std; n #define m_p make_pair n #define all(x) (x).begin(),(x).end() n #define sz(x) ((int)(x).size()) n #define fi first n #define se second n typedef long long ll; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n mt19937 rnf(2106); n const int N = 1000006; n  n int q; n int a[N], c[N]; n  n const int m = 20; n int p[N][m]; n bool z[N]; n  n int go(int x) n { n     for (int i = m - 1; i >= 0; --i) n     { n         if (!z[p[x][i]]) n             x = p[x][i]; n     } n     return x; n } n  n void solv() n { n     cin >> q >> a[0] >> c[0]; n  n     for (int i = 1; i <= q; ++i) n     { n         int ty; n         cin >> ty; n         if (ty == 1) n         { n             cin >> p[i][0] >> a[i] >> c[i]; n             for (int j = 1; j < m; ++j) n             { n                 p[i][j] = p[p[i][j - 1]][j - 1]; n             } n         } n         else n         { n             int x, w; n             cin >> x >> w; n  n             int ww = 0; n             ll g = 0; n             while (1) n             { n                 if (w == 0) n                     break; n                 if (z[x]) n                     break; n                 int u = go(x); n                 if (a[u] > w) n                 { n                     a[u] -= w; n                     ww += w; n                     g += w * 1LL * c[u]; n                     w = 0; n                 } n                 else n                 { n                     w -= a[u]; n                     ww += a[u]; n                     g += a[u] * 1LL * c[u]; n                     a[u] = 0; n                     z[u] = true; n                 } n             } n  n             cout << ww <<     << g << endl; n         } n     } n } n  n int main() n { n     #ifdef SOMETHING n     freopen( input.txt ,  r , stdin); n     //freopen( output.txt ,  w , stdout); n     #endif // SOMETHING n     ios_base::sync_with_stdio(false), cin.tie(0); n  n     int tt = 1; n     //cin >> tt; n     while (tt--) n     { n         solv(); n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long int z = 1000000007; n long long int h[1000009], a[1000009]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   vector<pair<string, string> > p; n   std::set<int> myset; n   std::set<int>::iterator it; n   long long int n, m, i, x = 0, y, v, j, d, c, k = 0, max = 0, sum = 0, n                          flag = 0; n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     cin >> x; n     myset.insert(x); n   } n   x = 1; n   while (sum <= m) { n     it = myset.begin(); n     if (*it != x) { n       sum += x; n       if (sum <= m) { n         a[k] = x; n         k++; n       } n     } else { n       myset.erase(it); n     } n     x++; n   } n   cout << k << endl; n   for (i = 0; i < k; i++) cout << a[i] <<    ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n ; n long long max(long long a, long long b) { n   if (a > b) n     return a; n   else n     return b; n } n long long min(long long a, long long b) { n   if (a > b) n     return b; n   else n     return a; n } n long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n inline long long normalize(long long x, long long mod) { n   x %= mod; n   if (x < 0) x += mod; n   return x; n } n long long dx[4] = {0, 0, 1, -1}; n long long dy[4] = {1, -1, 0, 0}; n const long long mod = 1000 * 1000 * 1000 + 7; n const long long inf = 1e18 + 5; n long long powm(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return res; n } n const long long N = 2e5 + 5; n long long n, m, k, x, l, r; n long long a[N]; n vector<long long> v; n void solve() { n   cin >> l >> r; n   for (long long i = 0; i <= 63; i++) { n     if ((l & (1LL << i)) == 0) { n       if ((l | (1LL << i)) > r) { n         break; n       } n       l |= (1LL << i); n     } n   } n   cout << l <<   n ; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(10); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[110000], b[110000], dp[110000][5]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, i, j, k; n   vector<long long> v; n   cin >> n; n   for (i = 1; i < n; i++) { n     cin >> a[i]; n   } n   for (i = 1; i < n; i++) { n     cin >> b[i]; n   } n   for (i = 2; i <= n; i++) { n     for (j = 0; j < 4; j++) { n       dp[i][j] = -1; n       for (k = 0; k <= 3; k++) { n         if (dp[i - 1][k] != -1) { n           if (((j & k) == b[i - 1]) && ((j | k) == a[i - 1])) dp[i][j] = k; n         } n       } n     } n   } n   long long fl = 0; n   for (i = 0; i < 4; i++) { n     if (dp[n][i] != -1) { n       cout <<  YES n ; n       for (j = n; j > 0; j--) { n         v.push_back(i); n         i = dp[j][i]; n       } n       fl = 1; n       break; n     } n   } n   if (fl == 0) n     cout <<  NO ; n   else { n     for (i = 0; i < v.size(); i++) cout << v[v.size() - i - 1] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int findd(string s1, string s2) { n   long long h1 = 0, h2 = 0, x = 1; n   int i, n = (int)s1.size(), m = (int)s2.size(); n   if (n < m) return -1; n   for (i = 0; i < m; i++) h2 = h2 * 33 + s2[i] -  a  + 1; n   for (i = 0; i < n; i++) { n     if (i < m) { n       h1 = h1 * 33 + s1[i] -  a  + 1; n       x = x * 33; n       if (i == m - 1) n         if (h1 == h2) return 1; n     } else { n       h1 = h1 * 33 - (s1[i - m] -  a  + 1) * x + s1[i] -  a  + 1; n       if (h1 == h2) return 1; n     } n   } n   return -1; n } n string join(string s1, string s2) { n   string st; n   long long x, h1, h2; n   int j, maxj = 0; n   if (findd(s1, s2) != -1) n     st = s1; n   else { n     h1 = h2 = 0; n     x = 1; n     for (j = 0; j < min(s1.size(), s2.size());) { n       h1 = h1 * 33 + s2[j] -  a  + 1; n       h2 = h2 + (s1[s1.size() - 1 - j] -  a  + 1) * x; n       x = x * 33; n       if (h1 == h2) { n         j++; n         maxj = max(j, maxj); n       } else { n         j++; n       } n     } n     if (maxj == 0) n       st = s1 + s2; n     else { n       st = s1; n       for (j = maxj; j < s2.size(); j++) st += s2[j]; n     } n   } n   return st; n } n int main(int argc, const char* argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int m; n   string S[3], s1, s2, s3; n   cin >> S[0] >> S[1] >> S[2]; n   m = (int)S[0].size() + (int)S[1].size() + (int)S[2].size(); n   vector<int> V = {0, 1, 2}; n   do { n     s1 = S[V[0]]; n     s2 = S[V[1]]; n     s3 = S[V[2]]; n     s1 = join(s1, s2); n     s1 = join(s1, s3); n     m = min(m, (int)s1.size()); n   } while (next_permutation(V.begin(), V.end())); n   cout << m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<pair<string, string>, string> solve(int n) { n   if (n == 1) { n     return {{ a ,   },  a }; n   } n   if (n == 2) { n     return {{ a ,  a },  a }; n   } n   int k = n - 1 >> 1; n   auto res = solve(k); n   string c =   ; n   c += res.second[res.second.size() - 1] + 1; n   if (k * 2 + 1 == n) { n     return {{res.first.first + c, res.first.second + c + c}, res.second + c}; n   } else { n     return {{res.first.first + c, c + res.first.second + c + c}, n             res.second + c}; n   } n } n int main() { n   int n; n   cin >> n; n   auto it = solve(n); n   cout << it.first.first + it.first.second <<     << it.second << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     string ans; n     string v; n     string p; n     for (int i = 0; i < n - 2; i++) { n       cin >> v; n       if (v[0] == p[1] && i != 0) { n         ans += v[0]; n       } else if ((v[0] != p[1]) && i != 0) { n         ans += p[1]; n         ans += v[0]; n       } else { n         ans += v[0]; n       } n       p = v; n     } n     ans += v[1]; n     if (ans.size() != n) { n       ans +=  a ; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int n, m; n bool check(int mid) { n   ll cnt = m * 1ll * mid, C = 1, ret = 1; n   for (int i = 1; i <= min(m, mid) && cnt; i++) { n     C = C * (mid - i + 1) / i; n     if (cnt >= C * i) n       cnt -= C * i, ret += C; n     else { n       ret += cnt / i; n       break; n     } n     if (ret >= n) return 1; n   } n   return ret >= n; n } n int main() { n   ios::sync_with_stdio(false); n   int T; n   cin >> T; n   while (T--) { n     cin >> n >> m; n     int l = 0, r = n, ans = -1; n     while (l <= r) { n       int mid = (l + r) >> 1; n       if (check(mid)) n         ans = mid, r = mid - 1; n       else n         l = mid + 1; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void pisz(int n) { printf( %d n , n); } n const int MAX_N = 200010; n int n; n char a[MAX_N]; n char b[MAX_N]; n double s1[26][MAX_N]; n double s2[26][MAX_N]; n void precompute() { n   s1[b[1] -  A ][1] = 1UL; n   for (int(i) = (2); (i) <= (n); ++i) { n     for (int(j) = 0; (j) < (26); ++(j)) { n       s1[j][i] = s1[j][i - 1]; n       if (j == b[i] -  A ) { n         s1[j][i] = (double)(s1[j][i] + i); n       } n     } n   } n   s2[b[n] -  A ][n - 1] = 1UL; n   for (int(i) = (n - 2); (i) >= (1); --i) { n     for (int(j) = 0; (j) < (26); ++(j)) { n       s2[j][i] = s2[j][i + 1]; n       if (j == b[i + 1] -  A ) { n         s2[j][i] = (double)(s2[j][i] + (double)(n - (i + 1UL) + 1UL)); n       } n     } n   } n } n int main() { n   char c; n   scanf( %d , &n); n   scanf( %s , a + 1); n   scanf( %s , b + 1); n   precompute(); n   double num_of_substrings = 0UL; n   for (int(l) = (1); (l) <= (n); ++l) { n     num_of_substrings += (double)((double)((double)(n - l) + 1UL) * n                                   (double)((double)(n - l) + 1UL)); n   } n   double count = 0UL; n   for (int(i) = (1); (i) <= (n); ++i) { n     c = a[i]; n     count = (double)(count + (double)((double)(n - i + 1UL) * s1[c -  A ][i])); n     count = (double)(count + (double)(i * s2[c -  A ][i])); n   } n   double res = (count * 1.0) / (num_of_substrings); n   printf( %.9f n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<long long int> a; n   long long int n; n   cin >> n; n   long long int sum = 0, m, ans = 0, count = 0; n   ; n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     sum = sum + x; n     a.push_back(x); n   } n   m = sum / n; n   for (long long int i = 0; i < a.size() - 1; i++) { n     count = m - a[i]; n     ans = ans + fabs(count); n     a[i] = m; n     a[i + 1] = a[i + 1] - count; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ile(long long int a) { n   int ans = 0; n   while (a) { n     if (a % 2 == 1) ans++; n     a /= 2; n   } n   return ans; n } n int main() { n   int n; n   cin >> n; n   long long int* a; n   a = new long long int[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int* b; n   b = new int[n]; n   for (int i = 0; i < n; i++) { n     b[i] = ile(a[i]); n   } n   int* pre; n   pre = new int[n]; n   pre[0] = b[0] % 2; n   for (int i = 1; i < n; i++) { n     pre[i] = (pre[i - 1] + b[i]) % 2; n   } n   int ile0 = 0; n   int ile1 = 0; n   for (int i = 0; i < n; i++) { n     if (pre[i] == 0) n       ile0++; n     else n       ile1++; n   } n   long long int ans = 0; n   ans += ile0; n   for (int i = 1; i < n; i++) { n     if (pre[i - 1] == 0) n       ile0--; n     else n       ile1--; n     if (pre[i - 1] == 0) n       ans += ile0; n     else n       ans += ile1; n   } n   for (int i = 0; i < n; i++) { n     int j = i; n     int naj = 0; n     int sum = 0; n     while (j < n && sum <= 128) { n       naj = max(naj, b[j]); n       sum += b[j]; n       if (naj > (sum - naj)) { n         if (i == 0) { n           if (pre[j] == 0) ans--; n         } else { n           if (pre[j] - pre[i - 1] == 0) ans--; n         } n       } n       j++; n     } n   } n   cout << ans; n   return 0; n } n


#include<iostream> n #include<string> n #include<algorithm> n #include<cstdio> n #include<cstring> n #include<cmath> n #include<map> n #include<queue> n #include<sstream> n #include<stack> n #include<set> n #include<bitset> n #include<vector> n #define ll long long n #define ull unsigned long long n #define up_b upper_bound n #define low_b lower_bound n #define m_p make_pair n #define mem(a) memset(a,0,sizeof(a)) n #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) n #define INF 0x3f3f3f3f n #define endl   n  n #define rep(i,a,b) for(int i=a;i<=b;i++) n #define per(i,a,b) for(int i=a;i>=b;i--) n using namespace std; n const ll mod=1e9+7; n inline int lowbit(int x){return x&(-x);} n ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} n void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d); n inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;} n inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch> 9 ||ch< 0 ) {if(ch== - ) f=-1; ch = getchar();}while(ch>= 0 &&ch<= 9 ) x = (x<<3) + (x<<1) + ch -  0 ,  ch = getchar();return x*f; } n //inline ll Inverse(ll x) { return qpow(x,mod-2);} n //inline ll C(ll n,ll m) { return fact[n]*inv[m]%mod*inv[n-m]%mod;} n double round(double r){return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);}//四舍五入 n const int maxn=1e5+5; n //const int dx[]={-1,0,1,0}; n //const int dy[]={0,1,0,-1}; n //ll fact[105]; n //ll inv[105]; n ll w[maxn]; n ll pow2[35]; n map<ll,ll>mp; n  n void init() n { n  pow2[0]=1; n  rep(i,1,30) pow2[i]=pow2[i-1]*2; n } n  n int main() n { n  //IOS; n  //fact[0]=inv[0]=1; n  //rep(i,1,100) fact[i]=(ll)i*fact[i-1]%mod,inv[i]=Inverse(fact[i]); n  init(); n  int _;_=read();while(_--) n  { n   ll n=read(),W=read(),h=0; n   rep(i,1,n) n   { n    ll temp=read(),p=0; n    while(temp!=1) n    { n     temp/=2; n     p++; n    } n    mp[p]++;  n   } //w[i]=read(),mp[w[i]]++; n   //sort(w+1,w+1+n); n   //h=1;tmp=W-w[n];mp[w[n]]--; n   int x=0; n   for(h=1;x<n;h++) n   { n    ll now=W; n    for(int j=30;j>=0;j--) n    { n     while(mp[j]>0&&now>=pow2[j]) n     { n      mp[j]--; n      now-=pow2[j]; n      x++; n      //cout<<now<<endl; n     } n    } n   } n   printf( %lld n ,h-1); n   mp.clear(); n  } n } n  n  n  n


#include <bits/stdc++.h> n using namespace std; n const int64_t Nmax = 2e5 + 10; n const int64_t mod = 1000000007; n vector<int64_t> prime; n int64_t is_power_of_two(int64_t a) { return a && (!(a & (a - 1))); } n int64_t isperfectsquare(int64_t a) { n   double b = sqrt(a); n   return (b - ceil(b) == 0); n } n int64_t pow(int64_t a, int64_t b, int64_t m) { n   a = a % m; n   int64_t res = 1; n   while (b > 0) { n     if (b & 1) { n       res = res * a % m; n     } n     a = a * a % m; n     b = b / 2; n   } n   return res; n } n int64_t mod_add(int64_t a, int64_t b, int64_t m) { n   a = a % m; n   b = b % m; n   return (((a + b) % m) + m) % m; n } n int64_t mod_mul(int64_t a, int64_t b, int64_t m) { n   a = a % m; n   b = b % m; n   return (((a * b) % m) + m) % m; n } n int64_t mod_sub(int64_t a, int64_t b, int64_t m) { n   a = a % m; n   b = b % m; n   return (((a - b) % m) + m) % m; n } n int64_t mod_div(int64_t a, int64_t b, int64_t m) { n   a = a % m; n   b = b % m; n   return (mod_mul(a, pow(b, m - 2, m), m)); n } n void sieve(int64_t n) { n   bool isprime[n + 1]; n   memset(isprime, true, sizeof(isprime)); n   prime.push_back(2); n   for (int64_t i = 3; i <= sqrt(n); i += 2) { n     if (isprime[i]) { n       for (int64_t j = i * i; j <= n; j += i) { n         isprime[j] = false; n       } n     } n   } n   for (int64_t i = 3; i <= n; i += 2) { n     if (isprime[i]) { n       prime.push_back(i); n     } n   } n } n vector<pair<int64_t, int64_t>> Getprimefactors(int64_t n) { n   vector<pair<int64_t, int64_t>> pr; n   for (int64_t i = 2; i <= sqrt(n); i++) { n     int64_t cnt = 0; n     while (n % i == 0) { n       cnt++; n       n /= i; n     } n     if (cnt) pr.push_back({i, cnt}); n   } n   if (n > 1) { n     pr.push_back({n, 1}); n   } n   return pr; n } n void solve() { n   int64_t n, od = 0, ev = 0, ans = 0, ans1 = 0; n   cin >> n; n   vector<int64_t> a(n), even, odd; n   for (int64_t i = 0; i <= n - 1; i++) { n     cin >> a[i]; n     if (a[i] & 1) { n       od++; n       odd.push_back(i); n     } else { n       ev++; n       even.push_back(i); n     } n   } n   if (abs(od - ev) > 1) { n     cout <<  -1 n ; n     return; n   } n   if (n & 1) { n     int64_t p = 0; n     if (od > ev) { n       for (int64_t i = 0; i < n; i += 2) { n         ans += abs(odd[p] - i); n         p++; n       } n     } else { n       for (int64_t i = 0; i < n; i += 2) { n         ans += abs(even[p] - i); n         p++; n       } n     } n     cout << ans <<   n ; n   } else { n     int64_t p = 0; n     for (int64_t i = 0; i < n; i += 2) { n       ans += abs(odd[p] - i); n       p++; n     } n     p = 0; n     for (int64_t i = 1; i <= n; i += 2) { n       ans1 += abs(odd[p] - i); n       p++; n     } n     cout << min(ans1, ans) <<   n ; n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int64_t t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n const int N = 2000005; n int pref[N], suf[N]; n int lB[N], lW[N]; n int arr[N]; n int S[N][4]; n int n, k; n char s[N]; n int pw[N]; n int value[N]; n int summary; n int mPow(int x, int n, int mod) { n   if (n == 0) return 1; n   if (n % 2) return 1LL * x * mPow(x, n - 1, mod) % mod; n   int value = mPow(x, n / 2, mod); n   return 1LL * value * value % mod; n } n int get(int hsfkasfhasfuckajsfkjsaf, int ajadslfkjasdklfj, int x) { n   return S[ajadslfkjasdklfj][x] - n          (hsfkasfhasfuckajsfkjsaf ? S[hsfkasfhasfuckajsfkjsaf - 1][x] : 0); n } n int main() { n   scanf( %d%d n , &n, &k); n   scanf( %s , s); n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  X ) n       arr[i + 1] = 0; n     else if (s[i] ==  B ) n       arr[i + 1] = 1; n     else n       arr[i + 1] = 2; n   } n   int lastB = -1, lastW = -1; n   for (int i = 0; i < n; ++i) { n     if (arr[i + 1] == 1) n       lastB = i + 1; n     else if (arr[i + 1] == 2) n       lastW = i + 1; n     lB[i + 1] = lastB; n     lW[i + 1] = lastW; n   } n   int cur[3] = {0, 0, 0}; n   for (int i = 0; i < n; ++i) { n     cur[arr[i + 1]]++; n     for (int j = 0; j < 3; ++j) S[i + 1][j] = cur[j]; n   } n   if (n < k + k) { n     puts( 0 ); n     return 0; n   } n   pref[0] = 1; n   for (int i = 1; i <= n; ++i) { n     pref[i] = pref[i - 1]; n     if (arr[i] == 0) pref[i] = (pref[i] + pref[i - 1]) % mod; n     if (i >= k) { n       if (lW[i] <= i - k && arr[i - k] != 1) n         pref[i] = n             (pref[i] - (i - k - 1 >= 0 ? pref[i - k - 1] : 1) + mod) % mod; n     } n   } n   suf[n + 1] = 1; n   for (int i = n; i >= 1; --i) { n     suf[i] = suf[i + 1]; n     if (arr[i] == 0) suf[i] = (suf[i] + suf[i + 1]) % mod; n     if (i + k - 1 <= n) { n       if (lB[i + k - 1] <= i - 1 && arr[i + k] != 2) n         suf[i] = n             (suf[i] - (i + k + 1 <= n + 1 ? suf[i + k + 1] : 1) + mod) % mod; n     } n   } n   pw[0] = 1; n   for (int i = 1; i <= n; ++i) { n     pw[i] = (pw[i - 1] + pw[i - 1]) % mod; n   } n   for (int i = k + 1; i <= n; ++i) { n     if (i + k - 1 > n) continue; n     value[i] = mPow(pw[get(i, n, 0)], mod - 2, mod); n     value[i] = 1LL * value[i] * suf[i + k + 1] % mod; n   } n   int answer = 0; n   int summary = 0; n   for (int i = k + 1; i <= n; ++i) { n     if (i + k - 1 > n || arr[i + k] == 2) continue; n     summary = (summary + value[i]) % mod; n   } n   for (int i = 1; i <= n; ++i) { n     if (i + k - 1 > n) break; n     if (lW[i + k - 1] < i && arr[i - 1] != 1) { n       int ret = 1LL * ((pw[get(i + k, n, 0)] - suf[i + k] + mod) % mod) * n                 (i - 2 >= 0 ? pref[i - 2] : 1) % mod; n       answer = (answer + ret % mod) % mod; n     } n     summary = (summary - value[i + k] + mod) % mod; n   } n   printf( %d n , answer); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NUM = 2e5 + 5; n const int mod = 998244353; n int INF = 1e6; n pair<int, int> pinf = {INF, INF}; n long long bin_pow(int n) { n   if (n == 0) return 1; n   if (n % 2 == 0) return bin_pow(n / 2) * bin_pow(n / 2) % mod; n   return 2 * bin_pow(n - 1) % mod; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   int n, m; n   cin >> m >> n; n   string a, b; n   cin >> b; n   cin >> a; n   reverse(a.begin(), a.end()); n   reverse(b.begin(), b.end()); n   long long s[m]; n   s[0] = (b[0] -  0 ); n   for (int i = 1; i < m; i++) { n     s[i] = s[i - 1]; n     if (b[i] ==  1 ) s[i] += bin_pow(i); n     s[i] %= mod; n   } n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (a[i] ==  1 ) ans += s[min(i, m - 1)]; n     ans %= mod; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e6 + 666; n struct palindromic_tree { n   int len[MAXN], link[MAXN], cnt[MAXN]; n   char s[MAXN]; n   vector<pair<char, int>> to[MAXN]; n   int n, last, sz; n   void clear() { n     fill(to, to + MAXN, vector<pair<char, int>>()); n     memset(len, 0, sizeof(len)); n     memset(link, 0, sizeof(link)); n     memset(cnt, 0, sizeof(cnt)); n     memset(s, 0, sizeof(s)); n     n = last = 0; n     link[0] = 1; n     len[1] = -1; n     s[n++] = 27; n     sz = 2; n   } n   palindromic_tree() { clear(); } n   int get_link(int v) { n     while (s[n - len[v] - 2] != s[n - 1]) v = link[v]; n     return v; n   } n   int tr(int v, int c) { n     for (auto it : to[v]) n       if (it.first == c) return it.second; n     return 0; n   } n   int add_letter(int c) { n     s[n++] = c; n     int cur = get_link(last); n     if (!tr(cur, c)) { n       len[sz] = len[cur] + 2; n       link[sz] = tr(get_link(link[cur]), c); n       to[cur].push_back({c, sz++}); n     } n     last = tr(cur, c); n     return cnt[last] = cnt[link[last]] + 1; n   } n }; n struct palindromic_tree me; n const int MOD = 51123987; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   string s; n   cin >> s; n   int start[n], finish[n]; n   for (int i = 0; i < n; i++) finish[i] = me.add_letter(s[i] -  a ); n   me.clear(); n   for (int i = 0; i < n; i++) n     start[n - i - 1] = me.add_letter(s[n - i - 1] -  a ); n   int64_t ans = accumulate(start, start + n, 0ll) % MOD; n   ans = ans * (ans - 1) / 2 % MOD; n   int64_t sum = 0; n   for (int i = 1; i < n; i++) { n     sum = (sum + finish[i - 1]) % MOD; n     ans = (ans - sum * start[i]) % MOD; n     if (ans < 0) ans += MOD; n   } n   cout << ans <<   n ; n } n