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


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


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


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


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


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


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


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


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


#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   long long powmod(long long a, long long b) {     long long res = 1;     for (; b; b >>= 1) {       if (b & 1) (res *= a) %= mod;       (a *= a) %= mod;     }     return res;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; ++i) cin >> a[i];     long long ans = 0, now = 1, x = powmod(100, mod - 2);     for (int i = 0; i < n; ++i) {       (ans += now) %= mod;       (now *= a[i]) %= mod;       (now *= x) %= mod;     }     (ans *= powmod(now, mod - 2)) %= mod;     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int T = 1;     while (T--) {       string s1, s2;       cin >> s1 >> s2;       long long int ans = 0;       long long int i, j, k;       i = j = k = 0;       long long int n = s2.size();       long long int m = s1.size();       long long int count = 0;       map<long long int, long long int> fs, ls;       while (i < n && j < m) {         if (s2[i] == s1[j]) {           fs[i] = j;           i++;           j++;           count = 0;         } else {           count++;           j++;         }         ans = max(ans, count);       }       ans = max(ans, m - (j));       i = n - 1;       j = m - 1;       while (i >= 0 && j >= 0) {         if (s2[i] == s1[j]) {           i--;           j--;           ls[i] = j;           count = 0;         } else {           count++;           j--;         }         ans = max(ans, count);       }       for (auto x : fs) {         ans = max(ans, ls[x.first] - x.second);       }       ans = max(ans, j + 1);       cout << ans <<   n ;     }     return 0;   }


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


#include <bits/stdc++.h>   using namespace std;   const int MxN = (int)15e4 + 9;   int n, Q;   int parent[MxN + 9];   vector<int> adj[MxN + 9];   int height[MxN + 9];   char label[MxN + 9];   const string YES =  Shi ;   const string NO =  Fou ;   int parent2[MxN + 9];   vector<int> adj2[MxN + 9];   array<int, 26> label2[MxN + 9];   array<int, 26> dp[MxN + 9];   int dpSum[MxN + 9];   bool valid[MxN + 9];   void noAll() {     for (int i = 0; i < Q; ++i) cout << NO <<   n ;     exit(0);   }   bool spe(const int u) { return u == 1 || 0 == (adj[u].size() & 1); }   inline void update(const int u, const int j) {     assert(0 <= j && j < 26);     assert(spe(u));     valid[u] = true;     if (adj[u].empty()) return;     dpSum[u] -= dp[u][j];     dp[u][j] = 0;     for (const int v : adj2[u]) {       valid[u] &= valid[v];       dp[u][j] = max(dp[u][j], dp[v][j] + label2[v][j]);     }     dpSum[u] += dp[u][j];     valid[u] &= dpSum[u] <= height[u];   }   inline void chainUpdate(int u, const int j) {     do {       update(u, j);       u = parent2[u];     } while (u);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr), cout.tie(nullptr);     cin >> n >> Q;     for (int i = 2; i <= n; ++i) {       cin >> parent[i] >> label[i];       adj[parent[i]].push_back(i);       assert(adj[parent[i]].size() <= 2);     }     for (int i = n; i >= 1; --i) {       if (adj[i].empty()) continue;       const int u = adj[i][0];       if (adj[i].size() == 2) {         const int v = adj[i][1];         if (height[u] != height[v]) noAll();       }       height[i] = height[u] + 1;     }     for (int i = 2; i <= n; ++i) {       if (spe(parent[i])) {         parent2[i] = parent[i];       } else {         parent2[i] = parent2[parent[i]];         label2[i] = label2[parent[i]];       }       if (label[i] !=  ? ) label2[i][label[i] -  a ]++;     }     for (int i = n; i >= 1; --i) {       for (int x : adj[i]) {         const int y = spe(x) ? x : adj2[x][0];         adj2[i].push_back(y);       }     }     for (int i = n; i >= 1; --i)       if (spe(i))         for (int j = 0; j < 26; ++j) update(i, j);     for (int rep = 0;; ++rep) {       ostream &out = rep == 0 ? cerr : cout;       if (!valid[1])         out << NO <<   n ;       else {         long long ans = 0;         for (int j = 0; j < 26; ++j)           ans += (j + 1LL) * (dp[1][j] + (height[1] - dpSum[1]));         out << YES <<     << ans <<   n ;       }       if (rep == Q) break;       int u;       char ch;       cin >> u >> ch;       const int v = spe(u) ? u : adj2[u][0];       if (label[u] !=  ? ) {         const int j = label[u] -  a ;         label2[v][j]--;         chainUpdate(v, j);       }       if (ch !=  ? ) {         const int j = ch -  a ;         label2[v][j]++;         chainUpdate(v, j);       }       label[u] = ch;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }   pair<int, int> rev[1000005];   int h[1000005];   int d[1000005];   vector<int> g[1000005];   int N, K;   void pre_dfs(int u, int p = -1) {     rev[u] = pair<int, int>(1000005, 0);     for (int v : g[u]) {       if (v == p) continue;       h[v] = h[u] + 1;       pre_dfs(v, u);       if (rev[v].first <= h[u]) {         rev[u].first = min(rev[u].first, rev[v].first);         rev[u].second += rev[v].second;       }     }     if (p >= 0 && g[u].size() == 1) {       rev[u].first = h[u] - K;       rev[u].second = 1;     }   }   void sol(int u, int p = -1) {     d[u] = p >= 0 && g[u].size() == 1;     for (int v : g[u]) {       if (v == p) continue;       sol(v, u);       int temp = rev[u].second;       if (rev[v].first <= h[u]) temp -= rev[v].second;       d[u] = max(d[u], temp + d[v]);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> N >> K;     for (int i = 1; i <= N - 1; i++) {       int u;       cin >> u;       u--;       g[i].push_back(u);       g[u].push_back(i);     }     h[0] = 0;     pre_dfs(0);     sol(0);     cout << d[0] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000 * 100 + 10;   int par[N], n, m, c;   unordered_map<int, int> adj[N];   unordered_set<int> adj1[N];   int getPar(int root) {     return par[root] == par[par[root]] ? par[root]                                        : par[root] = getPar(par[root]);   }   void Union(int v, int u) {     v = getPar(v), u = getPar(u);     if (v == u) return;     if (adj1[v].size() < adj1[u].size()) swap(v, u);     par[u] = par[v];     for (auto it : adj1[u]) adj1[v].insert(it);   }   void add(int v, int u, int c) {     if (adj[u][c]) Union(v, adj[u][c]);     if (adj[v][c]) Union(u, adj[v][c]);     int pv = getPar(v), pu = getPar(u);     adj[v][c] = pu, adj[u][c] = pv;     adj1[pv].insert(u), adj1[pu].insert(v);   }   bool check(int v, int u) {     int pv = getPar(v), pu = getPar(u);     return pv == pu || adj1[pv].count(u);   }   int main() {     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     int Q;     cin >> n >> m >> c >> Q;     for (int i = 1; i <= n; i++) par[i] = i;     for (int i = 1, v, u, z; i <= m; i++) {       cin >> v >> u >> z;       add(v, u, z);     }     while (Q--) {       char c;       int x, y, z;       cin >> c >> x >> y;       if (c ==  ? )         cout << (check(x, y) ?  Yes  :  No ) <<   n ;       else {         cin >> z;         add(x, y, z);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   using V = vector<T>;   using ll = long long;   using dbl = double;   using ii = pair<int, int>;   const ll MOD = 998244353;   ll z1[2000005], z2[2000005], dp[1000005], psum[1000005];   void solveZ(string s, ll z[]) {     z[0] = 0;     ll L = 0, R = 0, n = s.size();     for (ll i = 1; i < n; i++) {       if (i > R) {         L = R = i;         while (R < n && s[R - L] == s[R]) R++;         z[i] = R - L;         R--;       } else {         ll k = i - L;         if (z[k] < R - i + 1)           z[i] = z[k];         else {           L = i;           while (R < n && s[R - L] == s[R]) R++;           z[i] = R - L;           R--;         }       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     string a, l, r;     cin >> a >> l >> r;     string la = l +  #  + a;     string ra = r +  #  + a;     solveZ(la, z1);     solveZ(ra, z2);     ll lsize = l.size();     ll rsize = r.size();     ll ls = 0, rs = 0;     for (int i = 0; i < (int)a.size(); i++) {       if (i - ls + 1 < lsize) {         dp[i] = 0;         continue;       }       while (i - ls + 1 != lsize) ls++;       while (i - rs + 1 > rsize) rs++;       if (i - rs + 1 == rsize) {         ll matched = z2[rs + rsize + 1];         if (matched != rsize && a[rs + matched] > r[matched]) rs++;       }       if (i - ls + 1 == lsize) {         ll matched = z1[ls + lsize + 1];         if (matched != lsize && a[ls + matched] < l[matched]) {           if (ls == 0)             continue;           else             ls--;         }       }       if (rs <= ls) {         if (ls - 1 < 0)           dp[i] = 1;         else if (rs - 1 < 0)           dp[i] = (dp[i] + psum[ls - 1] + 1) % MOD;         else           dp[i] = (psum[ls - 1] - (rs - 2 < 0 ? 0 : psum[rs - 2]) + MOD) % MOD;         if (a[i] ==  0  && ls == i) dp[i] = (dp[i] + dp[i - 1]) % MOD;         if (i != a.size() - 1 && a[i + 1] !=  0 )           psum[i] = (psum[i] + (i == 0 ? 0 : psum[i - 1]) + dp[i]) % MOD;         else if (i != 0)           psum[i] = psum[i - 1];         else           psum[i] = 0;       }     }     cout << dp[a.size() - 1] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string st;   int dp[10005][4];   int n;   set<string> st_set;   int rec(int pos, int len) {     if (pos == 4 || pos == 5) {       return 1;     }     if (pos < 4) return 0;     int &ret = dp[pos][len];     if (ret != -1) return ret;     ret = 0;     if (len == 0) {       if (pos >= 7) {         ret |= rec(pos - 3, 3);         st_set.insert(st.substr(pos - 2, 3));       }       if (pos >= 6) {         ret |= rec(pos - 2, 2);         st_set.insert(st.substr(pos - 1, 2));       }     } else if (len == 2) {       if (pos >= 6 && !(st[pos] == st[pos + 2] && st[pos + 1] == st[pos - 1])) {         ret |= rec(pos - 2, 2);         st_set.insert(st.substr(pos - 1, 2));       }       if (pos >= 7) {         ret |= rec(pos - 3, 3);         st_set.insert(st.substr(pos - 2, 3));       }     } else {       if (pos >= 7 && !(st[pos] == st[pos + 3] && st[pos - 1] == st[pos + 2] &&                         st[pos - 2] == st[pos + 1])) {         ret |= rec(pos - 3, 3);         st_set.insert(st.substr(pos - 2, 3));       }       if (pos >= 6) {         ret |= rec(pos - 2, 2);         st_set.insert(st.substr(pos - 1, 2));       }     }     return ret;   }   int main() {     cin >> st;     n = st.length();     memset(dp, -1, sizeof dp);     rec(n - 1, 0);     cout << st_set.size() << endl;     for (string x : st_set) cout << x << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace io {   const int SIZE = (1 << 21) + 1;   char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,                                          qu[55];   int f, qr;   inline void flush() {     fwrite(obuf, 1, oS - obuf, stdout);     oS = obuf;   }   inline void putc(char x) {     *oS++ = x;     if (oS == oT) flush();   }   template <class I>   inline void read(I &x) {     for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                                 (iS == iT ? EOF : *iS++))                               : *iS++);          c <  0  || c >  9 ;          c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                          (iS == iT ? EOF : *iS++))                        : *iS++))       if (c ==  - ) f = -1;     for (x = 0; c <=  9  && c >=  0 ;          c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                          (iS == iT ? EOF : *iS++))                        : *iS++))       x = x * 10 + (c & 15);     x *= f;   }   template <class I>   inline void print(I &x) {     if (!x) putc( 0 );     if (x < 0) putc( - ), x = -x;     while (x) qu[++qr] = x % 10 +  0 , x /= 10;     while (qr) putc(qu[qr--]);   }   struct Flusher_ {     ~Flusher_() { flush(); }   } io_flusher_;   }  // namespace io   using io ::print;   using io ::putc;   using io ::read;   struct node {     int nxt;     long long cnt;   } s[100005];   int a[15][100005];   int main() {     int i, j, k;     int n, m;     read(n), read(m);     for (i = 1; i <= m; i++)       for (j = 1; j <= n; j++) read(a[i][j]);     for (i = 1; i < n; i++) {       s[a[1][i]].nxt = a[1][i + 1];       s[a[1][i]].cnt = 1;     }     for (int i = (int)2; i <= (int)m; i++)       for (int j = (int)1; j <= (int)n - 1; j++)         if (s[a[i][j]].nxt == a[i][j + 1]) s[a[i][j]].cnt++;     long long sum = 1, ans = 0;     for (int i = (int)1; i <= (int)n - 1; i++) {       if (s[a[1][i]].cnt != m)         ans += sum * (sum + 1) / 2, sum = 1;       else         sum++;     }     if (sum > 0) ans += sum * (sum + 1) / 2;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool cmp1(vector<int> a, vector<int> b) {     int q = a[4] + a[1] + a[2] + a[3] - a[0];     int p = b[4] + b[1] + b[2] + b[3] - b[0];     return q < p;   }   bool cmp2(vector<int> a, vector<int> b) {     int q = a[0] + a[4] + a[2] + a[3] - a[1];     int p = b[0] + b[4] + b[2] + b[3] - b[1];     return q < p;   }   bool cmp3(vector<int> a, vector<int> b) {     int q = a[0] + a[1] + a[4] + a[3] - a[2];     int p = b[0] + b[1] + b[4] + b[3] - b[2];     return q < p;   }   bool cmp4(vector<int> a, vector<int> b) {     int q = a[0] + a[1] + a[2] + a[4] - a[3];     int p = b[0] + b[1] + b[2] + b[4] - b[3];     return q < p;   }   bool cmp5(vector<int> a, vector<int> b) {     int q = a[0] + a[1] + a[2] + a[3] - a[4];     int p = b[0] + b[1] + b[2] + b[3] - b[4];     return q < p;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int T = 1;     cin >> T;     while (T--) {       int n;       cin >> n;       vector<vector<int>> fr(n, vector<int>(5, 0));       string s[n];       for (int i = 0; i < n; i++) {         cin >> s[i];         for (int j = 0; j < s[i].size(); j++) {           if (s[i][j] ==  a ) fr[i][0]++;           if (s[i][j] ==  b ) fr[i][1]++;           if (s[i][j] ==  c ) fr[i][2]++;           if (s[i][j] ==  d ) fr[i][3]++;           if (s[i][j] ==  e ) fr[i][4]++;         }       }       int ans = 0, cw = 0;       sort(fr.begin(), fr.end(), cmp1);       int cs = 0, co = 0;       for (int i = 0; i < n; i++) {         cs = cs + fr[i][0];         co = co + fr[i][1] + fr[i][2] + fr[i][3] + fr[i][4];         if (cs > co) cw++;       }       ans = max(ans, cw);       cw = 0;       cs = 0, co = 0;       sort(fr.begin(), fr.end(), cmp2);       for (int i = 0; i < n; i++) {         cs = cs + fr[i][1];         co = co + fr[i][0] + fr[i][2] + fr[i][3] + fr[i][4];         if (cs > co) cw++;       }       ans = max(ans, cw);       cw = 0;       cs = 0, co = 0;       sort(fr.begin(), fr.end(), cmp3);       for (int i = 0; i < n; i++) {         cs = cs + fr[i][2];         co = co + fr[i][1] + fr[i][0] + fr[i][3] + fr[i][4];         if (cs > co) cw++;       }       ans = max(ans, cw);       cw = 0;       cs = 0, co = 0;       sort(fr.begin(), fr.end(), cmp4);       for (int i = 0; i < n; i++) {         cs = cs + fr[i][3];         co = co + fr[i][1] + fr[i][2] + fr[i][0] + fr[i][4];         if (cs > co) cw++;       }       ans = max(ans, cw);       cw = 0;       cs = 0, co = 0;       sort(fr.begin(), fr.end(), cmp5);       for (int i = 0; i < n; i++) {         cs = cs + fr[i][4];         co = co + fr[i][1] + fr[i][2] + fr[i][3] + fr[i][0];         if (cs > co) cw++;       }       ans = max(ans, cw);       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long freq[100001];   int dp[101];   int main() {     int h, a, p;     cin >> h >> a >> p;     int mh, ma;     cin >> mh >> ma;     vector<int> s;     while (h - ma > 0 || mh <= a) {       h -= ma;       mh -= a;       s.push_back(1);       if (mh <= 0) break;     }     while (mh > 0) {       s.push_back(0);       h += p - ma;       while (h - ma > 0 || mh <= a) {         h -= ma;         mh -= a;         s.push_back(1);         if (mh <= 0) break;       }     }     cout << s.size() << endl;     for (int i = 0; i < s.size(); i++) {       if (s[i])         cout <<  STRIKE  << endl;       else         cout <<  HEAL  << endl;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 << endl;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   typedef vector<vector<int> > VVI;   inline int mult(int x, int y) { return ((long long)x * y) % 1000000007; }   int power(int x, int y) {     int ret = 1;     while (y) {       if (y & 1) ret = mult(ret, x);       x = mult(x, x);       y = y / 2;     }     return ret;   }   int gcd(int a, int b) {     if (b) return gcd(b, a % b);     return a;   }   vector<int> prime;   void generateprime(int n) {     int i, j;     vector<int> num(n + 5);     num[1] = 1;     for (i = 4; i < n; i = i + 2) num[i] = 1;     for (i = 3; i < n; i++) {       if (num[i] == 0) {         for (j = 3 * i; j < n; j = j + 2 * i) num[j] = 1;       }     }     num[0] = 0;     for (i = 2; i < n; i++) {       if (num[i] == 0) prime.push_back(i);       num.clear();     }   }   int main() {     int count1, n, i, temp;     scanf( %d , &n);     if (n == 1) {       scanf( %d , &temp);       if (temp == 1) printf( YES n );       if (temp == 0) printf( NO n );     } else {       count1 = 0;       for (i = 0; i < n; i++) {         scanf( %d , &temp);         if (temp == 1) count1++;       }       if (count1 == n - 1)         printf( YES n );       else         printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   const int N = 1005;   int n, K;   int w[N][N];   std::vector<int> ansx, ansy;   int main() {     memset(w, -1, sizeof w);     bool f = true;     scanf( %d%d , &n, &K);     for (int i = 1; i <= n; ++i) {       int cnt = K;       for (int j = 1; j <= n && cnt > 0; ++j)         if (j != i && w[i][j] == -1) {           --cnt;           w[i][j] = 1;           w[j][i] = 0;           ansx.push_back(i);           ansy.push_back(j);         }       if (cnt != 0) {         f = false;         break;       }     }     if (!f)       puts( -1 );     else {       printf( %d n , ansx.size());       for (int i = 0; i < ansx.size(); ++i) printf( %d %d n , ansx[i], ansy[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, *dot, inf = 2e9, bg = 1e9, lg, rg, add, *ar;   bool er = 0;   void push(int nu) {     dot[nu * 2] = min(dot[nu * 2], dot[nu]);     dot[nu * 2 + 1] = min(dot[nu * 2 + 1], dot[nu]);     dot[nu] = inf;   }   void cha(int nu, int l, int r) {     if (l >= lg and r <= rg)       dot[nu] = min(dot[nu], add);     else if (!(l > rg or r < lg)) {       int mid = (l + r) / 2;       push(nu);       cha(nu * 2, l, mid);       cha(nu * 2 + 1, mid + 1, r);     }   }   int que(int nu, int l, int r) {     if (l == r)       return dot[nu];     else {       int mid = (l + r) / 2;       push(nu);       if (lg <= mid)         return que(nu * 2, l, mid);       else         return que(nu * 2 + 1, mid + 1, r);     }   }   void recalc(int nu) { dot[nu] = max(dot[nu * 2], dot[nu * 2 + 1]); }   void build(int nu, int l, int r) {     if (l == r)       dot[nu] = ar[l];     else {       int mid = (l + r) / 2;       build(nu * 2, l, mid);       build(nu * 2 + 1, mid + 1, r);       recalc(nu);     }   }   void cha2(int nu, int l, int r) {     if (l == r)       dot[nu] = add;     else {       int mid = (l + r) / 2;       if (lg <= mid)         cha2(nu * 2, l, mid);       else         cha2(nu * 2 + 1, mid + 1, r);       recalc(nu);     }   }   int que2(int nu, int l, int r) {     if (l >= lg and r <= rg)       return dot[nu];     else if (l > rg or r < lg)       return 0;     else {       int mid = (l + r) / 2;       int an = max(que2(nu * 2, l, mid), que2(nu * 2 + 1, mid + 1, r));       recalc(nu);       return an;     }   }   int main() {     scanf( %d%d , &n, &m);     dot = new int[n * 4], ar = new int[n];     int za[m][4];     bool us[n];     for (int a = 0; a < n * 4; a++) dot[a] = inf;     for (int a = 0; a < n; a++) {       ar[a] = inf, us[a] = 0;     }     for (int a = 0; a < m; a++) {       scanf( %d , &za[a][0]);       if (za[a][0] == 1) {         for (int b = 1; b < 4; b++) scanf( %d , &za[a][b]);         za[a][1]--, za[a][2]--;         lg = za[a][1], rg = za[a][2], add = za[a][3];         cha(1, 0, n - 1);       } else {         for (int b = 1; b < 3; b++) scanf( %d , &za[a][b]);         za[a][1]--;         lg = za[a][1];         if (us[lg] == 0) {           ar[lg] = que(1, 0, n - 1);           us[lg] = 1;         }       }     }     for (lg = 0; lg < n; lg++) {       if (us[lg] == 0) ar[lg] = que(1, 0, n - 1);     }     build(1, 0, n - 1);     for (int a = 0; a < m; a++) {       if (za[a][0] == 1) {         lg = za[a][1], rg = za[a][2];         int an = que2(1, 0, n - 1);         if (za[a][3] != an) er = 1;       } else {         lg = za[a][1], add = za[a][2];         cha2(1, 0, n - 1);       }     }     if (er)       printf( NO );     else {       printf( YES n );       map<int, vector<int> > ma;       for (int a = 0; a < n; a++) {         ma[ar[a]].push_back(a);         if (ar[a] == inf) ar[a] = bg;       }       if (ma[inf].size() > 1) {         int nu = ma[inf][0];         ar[nu] = (1 << 29) - 1;       } else {         if (ma[inf].size() == 0) ma.erase(inf);         int an = 0;         for (map<int, vector<int> >::iterator it = ma.begin(); it != ma.end();              it++) {           int va = (*it).first, nu = (*it).second[0];           if (va == inf) {             va = 0;             for (int b = 29; b >= 0; b--) {               int ne = (1 << b);               if ((an & ne) == 0) {                 if (va + ne <= bg) va += ne;               }             }             an = (an | va);             ar[nu] = va;           } else {             an = (an | va);             if ((*it).second.size() > 1) {               for (int b = 29; b >= 1; b--) {                 int ne = (1 << b) - 1;                 if (va >= ne) {                   ar[nu] = ne, an = (an | ne);                   break;                 }               }             }           }         }       }       for (int a = 0; a < n; a++) printf( %d  , ar[a]);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int v = min(n, m);     int p = n + m - 1 - v;     cout << p <<     << v <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   struct tree {     int l, r;     int tl, tr, parent;     long long sum, nextsum, occupy;   } trees[232222];   long long a[2][2], b[2][2];   int s[111111];   int ids = 0;   long long pre = -1;   void make_matrix(long long tm) {     if (tm <= 1) {       a[0][0] = 0;       a[0][1] = a[1][0] = a[1][1] = 1;       return;     }     make_matrix(tm / 2);     for (int i = 0; i < 2; ++i)       for (int j = 0; j < 2; ++j) b[i][j] = a[i][j];     for (int i = 0; i < 2; ++i)       for (int j = 0; j < 2; ++j)         a[i][j] = (b[i][0] * b[0][j] + b[i][1] * b[1][j]) % MOD;     if (tm % 2) {       for (int i = 0; i < 2; ++i)         for (int j = 0; j < 2; ++j) b[i][j] = a[i][j];       a[0][0] = b[0][1];       a[0][1] = (b[0][0] + b[0][1]) % MOD;       a[1][0] = b[1][1];       a[1][1] = (b[1][0] + b[1][1]) % MOD;     }   }   void cal(long long tm, long long &sum, long long &nextsum) {     if (pre != tm) {       make_matrix(tm);     }     pre = tm;     long long sum_temp = (sum * a[0][0] + nextsum * a[1][0]) % MOD;     nextsum = (sum * a[0][1] + nextsum * a[1][1]) % MOD;     sum = sum_temp;   }   int make_tree(int a[], int n, int start) {     int id = ids;     ++ids;     trees[id].l = start;     trees[id].r = start + n - 1;     trees[id].parent = 0;     if (n == 1) {       trees[id].sum = 0;       trees[id].nextsum = 1;       cal(a[0], trees[id].sum, trees[id].nextsum);       trees[id].occupy = 0;       trees[id].tl = 0;       trees[id].tr = 0;     } else {       int lid = make_tree(a, n / 2, start);       int rid = make_tree(a + n / 2, n - n / 2, start + n / 2);       trees[id].tl = lid;       trees[id].tr = rid;       trees[id].sum = (trees[lid].sum + trees[rid].sum) % MOD;       trees[id].nextsum = (trees[lid].nextsum + trees[rid].nextsum) % MOD;       trees[lid].parent = id;       trees[rid].parent = id;     }     return id;   }   void add_value(tree &node, long long value) {     long long psum = node.sum;     long long pnsum = node.nextsum;     cal(value, node.sum, node.nextsum);     node.occupy = node.occupy + value;     psum = node.sum - psum;     pnsum = node.nextsum - pnsum;     int p = node.parent;     while (p != 0) {       trees[p].nextsum = (trees[p].nextsum + pnsum + MOD) % MOD;       trees[p].sum = (trees[p].sum + psum + MOD) % MOD;       p = trees[p].parent;     }   }   void refresh_from_above(int l, int r, tree &node) {     if (node.l > r || node.r < l) return;     if (l <= node.l && node.r <= r) return;     if (node.occupy > 0) {       trees[node.tl].occupy = trees[node.tl].occupy + node.occupy;       cal(node.occupy, trees[node.tl].sum, trees[node.tl].nextsum);       trees[node.tr].occupy = trees[node.tr].occupy + node.occupy;       cal(node.occupy, trees[node.tr].sum, trees[node.tr].nextsum);       node.occupy = 0;     }     refresh_from_above(l, r, trees[node.tl]);     refresh_from_above(l, r, trees[node.tr]);   }   void add_area(int l, int r, tree &node, long long value) {     if (node.l > r || node.r < l) return;     if (l <= node.l && r >= node.r) {       add_value(node, value);       return;     }     add_area(l, r, trees[node.tl], value);     add_area(l, r, trees[node.tr], value);   }   long long cal_area(int l, int r, tree &node) {     if (node.l > r || node.r < l) return 0;     if (l <= node.l && r >= node.r) {       return node.sum;     }     return (cal_area(l, r, trees[node.tl]) + cal_area(l, r, trees[node.tr])) %            MOD;   }   int main() {     int n, m, p, l, r, x;     ids = 1;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; ++i) scanf( %d , &s[i]);     int id = make_tree(s, n, 1);     for (int i = 0; i < m; ++i) {       scanf( %d , &p);       if (p == 1) {         scanf( %d%d%d , &l, &r, &x);         refresh_from_above(l, r, trees[id]);         add_area(l, r, trees[id], x);       } else {         scanf( %d%d , &l, &r);         refresh_from_above(l, r, trees[id]);         printf( %I64d n , cal_area(l, r, trees[id]));       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   struct data {     int t, g;   };   int memo[250][4][66000];   int n, t;   data in[20];   int f(int time, int genre, int bit) {     if (memo[time][genre][bit] != -1) return memo[time][genre][bit];     if (time == t) return memo[time][genre][bit] = 1;     if (time > t) return memo[time][genre][bit] = 0;     int RE = 0;     for (int i = 0; i < n; i++)       if (in[i].g != genre && !(bit & (1 << i))) {         RE = (RE + f(time + in[i].t, in[i].g, bit | (1 << i))) % mod;       }     return memo[time][genre][bit] = RE;   }   int main() {     cin >> n >> t;     for (int i = 0; i < n; i++) cin >> in[i].t >> in[i].g;     memset(memo, -1, sizeof(memo));     cout << f(0, 0, 0);   }


#include <bits/stdc++.h>   using namespace std;   int go_to_hall(string maxx, string minn, int base) {     string h =   ;     int a = 0;     for (int i = minn.size() - 1, j = maxx.size() - 1; i >= 0; j--, i--) {       int q1 = minn[i] -  0 , q2 = maxx[j] -  0 ;       int q3 = q1 + q2 + a;       if (q3 >= base) {         int a1 = q3 - base;         string w;         ostringstream convert4;         convert4 << a1;         w = convert4.str();         h += w;         a = 1;       } else {         string w;         ostringstream convert5;         convert5 << q3;         w = convert5.str();         h += w;         a = 0;       }     }     if (a == 1 && maxx.size() == minn.size()) {       h +=  1 ;       return h.size();     } else {       int s = 0;       for (int r = maxx.size() - minn.size() - 1; r >= 0; r--) {         int e = s + (maxx[r] -  0 );         int x = maxx.size() - minn.size() - 1;         if (r == x) {           int y = e + a;           if (y >= base) {             int a1 = y - base;             string w;             ostringstream convert6;             convert6 << a1;             w = convert6.str();             h += w;             s = 1;           } else {             string w;             ostringstream convert7;             convert7 << y;             w = convert7.str();             h += w;             s = 0;           }         } else {           if (e >= base) {             int a1 = e - base;             string w;             ostringstream convert8;             convert8 << a1;             w = convert8.str();             h += w;             s = 1;           } else {             string w;             ostringstream convert9;             convert9 << e;             w = convert9.str();             h += w;             s = 0;           }         }       }       if (s == 1) {         h +=  1 ;       }       return h.size();     }   }   int main() {     int x, y, base = 0;     cin >> x >> y;     int k1 = x, k2 = y;     string q1;     ostringstream convert;     convert << x;     q1 = convert.str();     string q2;     ostringstream convert1;     convert1 << y;     q2 = convert1.str();     for (int y = 0; y < q1.size(); y++) {       int r = q1[y] -  0 ;       base = max(r, base);     }     for (int y = 0; y < q2.size(); y++) {       int r = q2[y] -  0 ;       base = max(r, base);     }     base++;     if (base == 10) {       int k3 = k1 + k2;       int i = 0;       while (k3 != 0) {         k3 = k3 / 10;         i++;       }       cout << i << endl;     } else {       string maxx, minn;       if (q1.size() >= q2.size()) {         maxx = q1;         minn = q2;       } else {         maxx = q2;         minn = q1;       }       cout << go_to_hall(maxx, minn, base) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   struct BIT {     int n;     vector<T> dat;     BIT(int n = 0) { initialize(n); }     void initialize(int nin) {       n = nin;       dat.resize(n, 0);     }     T sum(int i) {       T s = 0;       while (i >= 0) {         s += dat[i];         i = (i & (i + 1)) - 1;       }       return s;     }     T sum_between(int i, int j) { return sum(j) - sum(i - 1); }     void plus(int i, T x) {       while (i < n) {         dat[i] += x;         i |= i + 1;       }     }     int lower_bound(T x) {       if (x < 0) return -1;       int ret = -1;       int k = 1;       while (2 * k <= n) k <<= 1;       for (; k > 0; k >>= 1) {         if (ret + k < n && dat[ret + k] < x) {           x -= dat[ret + k];           ret += k;         }       }       return ret + 1;     }   };   int main() {     int N, M;     cin >> N >> M;     vector<int> A(N + M), mn(N), mx(N);     for (int i = 0; i < N; i++) A[N - 1 - i] = mn[i] = mx[i] = i;     for (int i = 0; i < M; i++) {       scanf( %d , &A[i + N]);       A[i + N]--;       mn[A[i + N]] = 0;     }     M += N;     vector<vector<int>> pos(N);     for (int i = 0; i < M; i++) pos[A[i]].push_back(i);     vector<pair<int, int>> R2LI(M);     for (int i = 0; i < N; i++) {       int sz = pos[i].size();       for (int j = 0; j < sz - 1; j++) R2LI[pos[i][j + 1]] = {pos[i][j], i};     }     BIT<int> bit(M);     for (int r = N; r < M; r++) {       int l = R2LI[r].first, i = R2LI[r].second;       bit.plus(l, 1);       int res = r - l - bit.sum_between(l, r);       mx[i] = max(mx[i], res);     }     for (int i = 0; i < N; i++) {       int l = pos[i].back(), r = M - 1;       int res = r - l - bit.sum_between(l, r);       mx[i] = max(mx[i], res);     }     for (int i = 0; i < N; i++) printf( %d %d n , mn[i] + 1, mx[i] + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10;   const long long mo = 1e9 + 7;   const long long inf = 1e9;   const long long Inf = 1e18;   int a[N], b[N];   int main() {     int n, m, tot = 0;     scanf( %d%d , &n, &m);     int nw = 0;     for (int i = (1); i <= (m); ++i) {       scanf( %d , &a[i]);       int x = a[i];       if (x <= nw) {         b[i] = b[i - 1] + x;         nw -= x;       } else {         x -= nw;         tot += x / n;         x = x % n;         b[i] = x;         if (x)           nw = n - x, ++tot;         else           nw = 0;       }     }     sort(b + 1, b + m);     b[m] = n;     for (int i = (m); i >= (1); --i) b[i] = b[i] - b[i - 1];     printf( %d n , tot);     for (int i = (1); i <= (m); ++i) printf( %d  , b[i]);     printf(  n );     int x = 1;     for (int i = (1); i <= (m); ++i) {       while (a[i] > 0) {         a[i] -= b[x];         printf( %d  , i);         if (x == m)           x = 1, printf(  n );         else           ++x;       }     }     for (; x != 1 && x != m + 1; ++x) printf( %d  , m);   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   const long long MOD = 1000000007;   const int INF = 1e9;   const long long LINF = 1LL * INF * INF;   const int MAXN = 100007;   const double EPS = 1e-7;   const int DX[4] = {1, 0, -1, 0}, DY[4] = {0, 1, 0, -1};   int n, m, r, k;   set<pair<int, int> > U;   priority_queue<pair<long long, pair<int, int> > > Q;   long long f(int i, int j) {     return (min(i, n - r) - max(0, i - r + 1) + 1LL) *            (min(j, m - r) - max(0, j - r + 1) + 1LL);   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     long long s = 0;     cin >> n >> m >> r >> k;     Q.push(make_pair(f(n / 2, m / 2), make_pair(n / 2, m / 2)));     U.insert(make_pair(n / 2, m / 2));     for (int j = (0); j < (k); ++j) {       s += Q.top().first;       int x = Q.top().second.first, y = Q.top().second.second;       Q.pop();       for (int i = (0); i < (4); ++i) {         if (x + DX[i] >= 0 && x + DX[i] < n && y + DY[i] >= 0 && y + DY[i] < m &&             !U.count(make_pair(x + DX[i], y + DY[i]))) {           Q.push(make_pair(f(x + DX[i], y + DY[i]),                            make_pair(x + DX[i], y + DY[i])));           U.insert(make_pair(x + DX[i], y + DY[i]));         }       }     }     cout << fixed << setprecision(12) << s / (n - r + 1.0) / (m - r + 1.0);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     vector<int> a(2 * n);     for (int i = 0; i < 2 * n; i++) {       scanf( %d , &a[i]);     }     sort(a.begin(), a.end());     printf( %s n , (a[n - 1] != a[n]) ?  YES  :  NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, h;   long long dp[40][40];   int main() {     scanf( %d %d , &n, &h);     memset(dp, 0, sizeof(dp));     for (int i = 0; i <= n; i++) {       dp[0][i] = 1;     }     for (int h = 1; h <= n; h++) {       for (int i = 1; i <= n; i++) {         for (int j = 0; j < i; j++)           dp[i][h] += dp[j][h - 1] * dp[i - j - 1][h - 1];       }     }     printf( %I64d n , dp[n][n] - dp[n][h - 1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read(int f = 1, int x = 0, char ch =    ) {     while (!isdigit(ch = getchar()))       if (ch ==  - ) f = -1;     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     return f * x;   }   const int N = 1e5 + 5;   struct Edge {     int next, to;     Edge(int next = 0, int to = 0) : next(next), to(to){};   } edge[N << 1];   int tot, head[N];   void add(int x, int y) { edge[++tot] = Edge(head[x], y), head[x] = tot; }   int n, fa[N], dfn[N], t, f[N], to[N], d[N], ans;   void dfs(int x) {     dfn[++t] = x;     for (int i = head[x]; i; i = edge[i].next) {       int y = edge[i].to;       if (y == to[x]) continue;       dfs(y);     }     if (to[x]) dfs(to[x]);   }   int main() {     n = read();     for (int i = 2; i <= n; ++i) add(fa[i] = read() + 1, i), d[i] = d[fa[i]] + 1;     for (int i = n; i > 1; --i)       f[fa[i]] = max(f[fa[i]], f[i] + 1),       to[fa[i]] = f[i] >= f[to[fa[i]]] ? i : to[fa[i]];     dfs(1);     for (int i = 1; i <= n; ++i) printf( %d  , dfn[i] - 1);     puts(  );     for (int i = n; i > 1; --i) ans += d[dfn[i - 1]] - d[fa[dfn[i]]];     printf( %d n , ans);     for (int i = 2; i <= n; ++i)       for (int j = 1; j <= d[dfn[i - 1]] - d[fa[dfn[i]]]; ++j)         printf( %d  , dfn[i] - 1);     puts(  );     return 0;   }