#include <bits/stdc++.h>   using namespace std;   int n, m;   int A[600006];   vector<int> G[600006];   int dfn[600006], bc[600006], clo;   int g[600006][20], dep[600006];   void dfs(int u) {     dfn[u] = ++clo, bc[clo] = u;     if (u == 1) dep[u] = 1;     for (int v : G[u]) {       g[v][0] = u;       dep[v] = dep[u] + 1;       for (int k = (1), kend = (18); k <= kend; ++k)         if (g[g[v][k - 1]][k - 1])           g[v][k] = g[g[v][k - 1]][k - 1];         else           break;       dfs(v);     }   }   int lca(int u, int v) {     if (dep[u] < dep[v]) swap(u, v);     if (dep[u] != dep[v])       for (int k = (18), kend = (0); k >= kend; --k)         if (dep[g[u][k]] >= dep[v]) u = g[u][k];     if (u == v) return u;     for (int k = (18), kend = (0); k >= kend; --k)       if (g[u][k] != g[v][k]) u = g[u][k], v = g[v][k];     return g[u][0];   }   int dis(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }   set<int> S;   void solve() {     cin >> n;     ++n;     for (int i = (2), iend = (n); i <= iend; ++i) {       int p;       scanf( %d , &p);       G[p].push_back(i);     }     dfs(1);     int s = 1, t = 1, lt = 0;     auto wkr = [&](int x) { return max(dis(x, s), dis(x, t)); };     S.insert(1);     for (int i = (2), iend = (n); i <= iend; ++i) {       int is = dis(i, s), it = dis(i, t), flg = 0;       if (is >= lt && is >= it)         t = i, lt = is, flg = 1;       else if (it >= lt && it >= is)         s = i, lt = it, flg = 1;       if (!flg) {         printf( %d n , (int)S.size());         continue;       }       auto tr = S.lower_bound(dfn[i]), st = tr;       while (tr != S.end()) {         if (wkr(bc[*tr]) < lt)           ++tr;         else           break;       }       if (tr == S.end()) {         auto sr = S.begin();         while (sr != st) {           if (wkr(bc[*sr]) < lt)             ++sr;           else             break;         }         S.erase(S.begin(), sr);       }       S.erase(st, tr);       tr = S.lower_bound(dfn[i]), st = tr;       while (tr != S.begin()) {         --tr;         if (wkr(bc[*tr]) >= lt) {           ++tr;           break;         }       }       if (tr == S.begin()) {         auto sr = S.end();         while (sr != st) {           --sr;           if (wkr(bc[*sr]) >= lt) {             ++sr;             break;           }         }         S.erase(sr, S.end());       }       S.erase(tr, st);       S.insert(dfn[i]);       printf( %d n , (int)S.size());     }   }   signed main() { solve(); }


#include <bits/stdc++.h>   using namespace std;   struct fenwick {     long long n, fen[100005];     void resize(int _n) {       n = _n + 1;       memset(fen, 0, sizeof fen);     }     int query(int ind) {       ind++;       int ans = 0;       while (ind >= 1) {         ans += fen[ind];         ind -= ind & -ind;       }       return ans;     }     void update(int ind, int val) {       ind++;       while (ind <= n) {         fen[ind] += val;         ind += ind & -ind;       }     }     int query(int l, int r) { return query(r) - query(l - 1); }   };   const int maxn = 100001;   int n, l, w, x, y, sz[maxn];   long long res;   bool blocked[maxn];   vector<pair<int, int> > c, v[maxn];   fenwick tree;   void dfs(int node, int pnode, int curw, int curl) {     c.push_back(make_pair(curw, curl));     for (auto &i : v[node])       if (i.first != pnode && !blocked[i.first])         dfs(i.first, node, curw + i.second, curl + 1);   }   long long solve(int node, int curw, int curl) {     long long ret = 0;     c.clear();     dfs(node, 0, curw, curl);     sort(c.begin(), c.end());     for (auto &i : c) tree.update(i.second, 1);     for (int i = 0, j = c.size() - 1; i < c.size(); i++) {       while (i <= j && c[i].first + c[j].first > w)         tree.update(c[j--].second, -1);       if (j < i) break;       tree.update(c[i].second, -1);       ret += tree.query(l - c[i].second);     }     return ret;   }   int dfsz(int node, int pnode) {     sz[node] = 1;     for (auto &i : v[node])       if (i.first != pnode && !blocked[i.first]) sz[node] += dfsz(i.first, node);     return sz[node];   }   int find(int node, int pnode, int n) {     for (auto &i : v[node])       if (i.first != pnode && !blocked[i.first] && 2 * sz[i.first] > n)         return find(i.first, node, n);     return node;   }   void decompose(int node) {     int centroid = find(node, 0, dfsz(node, 0));     blocked[centroid] = 1;     res += solve(centroid, 0, 0);     for (auto &i : v[centroid])       if (!blocked[i.first]) res -= solve(i.first, i.second, 1);     for (auto &i : v[centroid])       if (!blocked[i.first]) decompose(i.first);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> l >> w;     tree.resize(n + 1);     for (int i = 2; i <= n && cin >> x >> y; i++) {       v[i].push_back(make_pair(x, y));       v[x].push_back(make_pair(i, y));     }     decompose(1);     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   const int maxn = 1e5 + 10;   const int N = 5e2 + 10;   using namespace std;   long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); }   long long qpow(long long p, long long q) {     long long f = 1;     while (q) {       if (q & 1) f = f * p % 1000000007;       p = p * p % 1000000007;       q >>= 1;     }     return f;   }   int n, m, k, t;   vector<int> d[4][11][10];   char a[maxn], b[maxn];   int gao(char p) {     if (p ==  A )       return 0;     else if (p ==  T )       return 1;     else if (p ==  G )       return 2;     else       return 3;   }   void add(vector<int>& d, int x, int y) {     while (x < d.size()) d[x] += y, x += x & (-x);   }   int get(vector<int>& d, int x) {     int ret = 0;     while (x) ret += d[x], x -= x & (-x);     return ret;   }   int main() {     int i, j;     scanf( %s , a + 1);     n = strlen(a + 1);     for (i = 0; i <= (int)3; i++)       for (j = 1; j <= (int)10; j++)         for (k = 0; k <= (int)j - 1; k++) d[i][j][k].resize((n + j - 1) / j + 1);     for (i = 1; i <= n; i++) {       for (j = 1; j <= 10; j++) {         add(d[gao(a[i])][j][(i - 1) % j], (i - 1) / j + 1, 1);       }     }     int q;     scanf( %d , &q);     while (q--) {       int op, x, y;       scanf( %d%d , &op, &x);       if (op == 1) {         scanf( %s , b);         for (i = 1; i <= (int)10; i++)           add(d[gao(a[x])][i][(x - 1) % i], (x - 1) / i + 1, -1);         for (i = 1; i <= (int)10; i++)           add(d[gao(b[0])][i][(x - 1) % i], (x - 1) / i + 1, 1);         a[x] = b[0];       } else {         scanf( %d%s , &y, b);         int len = strlen(b), ret = 0;         for (i = 0; i < len && i < y - x + 1; i++) {           int mo = (i + x - 1) % len;           ret += get(d[gao(b[i])][len][mo], (y - 1 - mo) / len + 1) -                  get(d[gao(b[i])][len][mo], (x - 1 + len - 1 - mo) / len);         }         printf( %d n , ret);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int i, j, t;     cin >> t;     for (long long int aa = 0; aa < t; aa++) {       long long int n, k, d;       cin >> n >> k >> d;       long long int mini = k;       vector<long long int> v(n);       map<long long int, long long int> m;       for (i = 0; i < n; i++) {         cin >> v[i];         if (i < d) {           long long int ss = m.size();           m[v[i]]++;           if (ss < m.size()) m[v[i]] = 1;         }       }       m.size() < mini ? mini = m.size() : mini = mini;       for (i = 1; i < n - d + 1; i++) {         m[v[i - 1]]--;         if (m[v[i - 1]] == 0) m.erase(v[i - 1]);         long long int ss = m.size();         m[v[i + d - 1]]++;         if (ss < m.size()) m[v[i + d - 1]] = 1;         m.size() < mini ? mini = m.size() : mini = mini;       }       m.size() < mini ? mini = m.size() : mini = mini;       cout << mini << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int *p = new int[n + 1], c = 0;     int *m = new int[n + 1];     for (int i = 0; i <= n; i++) {       p[i] = m[i] = 0;       c ^= i;       m[i] = c;     }     int f = 0;     for (int i = 1; i <= n; i++) {       cin >> p[i];       f ^= p[i];     }     int r = 0;     for (int i = 2; i <= n; i++) {       int grps = (n / i);       int rem = n % i;       if (grps & 1) r ^= m[i - 1];       r ^= m[rem];     }     cout << (r ^ f);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   long long a[maxn], b[maxn], dp[maxn];   int q[maxn];   int head = 0, tail = 0;   double A(int i, int j) { return dp[i] - dp[j]; }   double B(int i, int j) { return b[i] - b[j]; }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n; i++) cin >> b[i];     dp[1] = 0;     int head = 1, tail = 1;     q[tail] = 1;     for (int i = 2; i <= n; i++) {       while (head < tail &&              A(q[head + 1], q[head]) / B(q[head + 1], q[head]) >= -a[i])         head++;       dp[i] = dp[q[head]] + b[q[head]] * a[i];       while (head < tail && A(i, q[tail]) / B(i, q[tail]) >=                                 A(q[tail], q[tail - 1]) / B(q[tail], q[tail - 1]))         tail--;       q[++tail] = i;     }     cout << dp[n] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_INT = numeric_limits<int>::max();   const int MAX_UINT = numeric_limits<unsigned int>::max();   void get_acum(const vector<int>& v, vector<int>& acum) {     int sum = 0;     for (unsigned int i = 0; i < v.size(); ++i) {       acum.push_back(sum);       sum += v[i];     }     acum.push_back(sum);   }   int solve(vector<int>& A, vector<int>& B, vector<int>& X, unsigned int k) {     if (A.size() + X.size() < k || B.size() + X.size() < k) {       return -1;     } else {       sort(A.begin(), A.end());       sort(B.begin(), B.end());       sort(X.begin(), X.end());       vector<int> A_acum, B_acum, X_acum;       get_acum(A, A_acum);       get_acum(B, B_acum);       get_acum(X, X_acum);       int res = MAX_INT;       unsigned int max_i = min((unsigned int)X.size(), k);       for (unsigned int i = 0; i < max_i + 1; ++i) {         if (k - i < A_acum.size() && k - i < B_acum.size()) {           int total = A_acum[k - i] + B_acum[k - i] + X_acum[i];           if (total < res) res = total;         }       }       return res;     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     unsigned int n, k;     cin >> n >> k;     vector<int> A;     vector<int> B;     vector<int> X;     for (unsigned int i = 0; i < n; ++i) {       unsigned int ti;       bool ai, bi;       cin >> ti >> ai >> bi;       if (ai && bi)         X.push_back(ti);       else if (ai)         A.push_back(ti);       else if (bi)         B.push_back(ti);     }     cout << solve(A, B, X, k);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<long long int> adj[3005];   map<long long int, long long int> mq;   long long int done[3005][3005];   int main() {     ios_base::sync_with_stdio(false);     long long int n, m, i, ans = 0;     cin >> n >> m;     for (i = 0; i < m; i++) {       long long int x, y;       cin >> x >> y;       adj[x].insert(y);     }     for (i = 1; i <= n; i++) {       for (auto it : adj[i]) {         for (auto it2 : adj[it]) {           if (mq.find(it2) != mq.end() && it2 != i) {             ans += mq[it2];           }           mq[it2]++;         }       }       mq.clear();     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   int main() {     int a, b, h;     cin >> a >> b >> h;     int c = 0;     while (h != 0) {       if (c % 2 == 0) {         h = h - gcd(a, h);       } else {         h = h - gcd(b, h);       }       c++;     }     if (c % 2 == 0) {       cout << 1 << endl;     } else       cout << 0 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000 + 5;   const double EPS = 1e-3;   int n;   int t[MAXN];   int g(double q) {     bool ok = true;     for (int i = 1; i < n; i++)       if (fabs(double(t[i] - t[i - 1] * q)) > EPS) ok = false;     if (ok) return 0;     ok = true;     for (int i = 2; i < n; i++)       if (fabs(double(t[i] - t[i - 1] * q)) > EPS) ok = false;     if (ok) return 1;     ok = true;     int prev = t[0];     for (int i = 1; i < n; i++)       if (fabs(double(t[i] - prev * q)) > EPS) {         if (!ok) return 2;         ok = false;       } else         prev = t[i];     return 1;   }   int f() {     if (n == 1) return 0;     double q1 = 0, q2 = 0;     if (t[0] != 0) q1 = double(t[1]) / t[0];     if (t[n - 2] != 0) q2 = double(t[n - 1]) / t[n - 2];     return min(g(q1), g(q2));   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &t[i]);     printf( %d n , f());   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e6 + 5;   int a[MAX], run[MAX], ans[MAX];   char s[MAX];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     scanf( %d , &n);     scanf( %s , s);     for (int i = 0; i < n; ++i) {       if (s[i] ==  0 )         a[i] = 0;       else if (s[i] ==  1 )         a[i] = 1;       else         a[i] = -1;     }     vector<pair<int, int>> blocks;     int it1 = 0, it2 = 0;     while (it1 != n) {       while (it2 < n - 1 && a[it1] == a[it2 + 1]) ++it2;       blocks.emplace_back(it2 - it1 + 1, a[it1]);       ++it2;       it1 = it2;     }     for (int i = 0; i < (int)blocks.size(); ++i) {       if (blocks[i].second != -1) continue;       if (i == 0)         blocks[i].second = blocks[i + 1].second;       else if (i == (int)blocks.size() - 1)         blocks[i].second = blocks[i - 1].second;       else if (blocks[i - 1].second == blocks[i + 1].second)         blocks[i].second = blocks[i - 1].second;     }     vector<pair<int, int>> compress;     it1 = 0;     it2 = 0;     while (it1 != (int)blocks.size()) {       int len = blocks[it1].first;       while (it2 < (int)blocks.size() - 1 &&              blocks[it1].second == blocks[it2 + 1].second)         it2++, len += blocks[it2].first;       compress.emplace_back(len, blocks[it1].second);       ++it2;       it1 = it2;     }     for (auto block : compress) {       if (block.second == -1) continue;       for (int len = 1; len <= block.first; len++) ans[len] += block.first / len;     }     int max_size = -1, bef = -2;     int size = compress.size();     for (int i = 0; i < size; i++) {       int len = compress[i].first;       int type = compress[i].second;       if (bef != -1 && type != -1) {         max_size = len;         if (i != size - 1) max_size += compress[i + 1].first;         for (int l = 1; l <= max_size; l++) run[l] = len % l;       } else if (type == -1) {         for (int l = 1; l <= max_size; l++) {           if (run[l] + len >= l)             run[l] += len, ans[l] += run[l] / l, run[l] %= l;           else             run[l] = len;         }         int add = 0;         if (i != (int)size - 1) add += compress[i + 1].first;         if (i != (int)size - 2) add += compress[i + 2].first;         for (int l = max_size + 1; l <= len + add; ++l) run[l] = (len % l);         max_size = len + add;       } else {         for (int l = 1; l <= max_size; ++l) {           if (run[l] + (len % l) >= l)             run[l] += ((len % l) - l), ans[l]++;           else             run[l] += (len % l);         }       }       bef = type;     }     for (int i = 1; i <= n; ++i) printf( %d  , ans[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int f = 1, x = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) {         f = -1;       }       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   const int N = 55;   struct W {     int x, y, tx, ty, id;   } up[N], down[N];   bool cmp_down(W a, W b) { return a.tx == b.tx ? a.ty < b.ty : a.tx > b.tx; }   bool cmp_up(W a, W b) { return a.x == b.x ? a.y < b.y : a.x < b.x; }   vector<pair<int, int> > ans_fr, ans_to;   bool has[N][N];   void move_up(int& x, int y) {     int dx = x;     has[x][y] = 0;     has[x - 1][y] = 1;     ans_fr.push_back(make_pair(dx, y));     ans_to.push_back(make_pair(dx - 1, y));     --x;   }   void move_down(int& x, int y) {     int dx = x;     has[x][y] = 0;     has[x + 1][y] = 1;     ans_fr.push_back(make_pair(dx, y));     ans_to.push_back(make_pair(dx + 1, y));     ++x;   }   void move_left(int x, int& y) {     int dy = y;     has[x][y] = 0;     has[x][y - 1] = 1;     ans_fr.push_back(make_pair(x, dy));     ans_to.push_back(make_pair(x, dy - 1));     --y;   }   void move_right(int x, int& y) {     int dy = y;     has[x][y] = 0;     has[x][y + 1] = 1;     ans_fr.push_back(make_pair(x, dy));     ans_to.push_back(make_pair(x, dy + 1));     ++y;   }   void move_row(int& x, int& y, int tx, int ty) {     while (y < ty && has[x][y + 1] == 0) {       move_right(x, y);     }     while (y > ty && has[x][y - 1] == 0) {       move_left(x, y);     }   }   void move(int& x, int& y, int tx, int ty) {     while (x < tx) {       move_down(x, y);     }     while (x > tx) {       move_up(x, y);     }     while (y < ty) {       move_right(x, y);     }     while (y > ty) {       move_left(x, y);     }   }   int check(W a) {     if (a.x == a.tx) {       return a.y == a.ty ? 0 : 1;     } else {       return a.y == a.ty ? 2 : 3;     }   }   int nx[N], ny[N];   char mp[N][N];   int vis[N];   void write(int n) {     for (int i = 1; i <= n; i++) {       cout << mp[i] + 1 << endl;     }     puts( ---------------------- );   }   void deal(int n, int m) {     if (n == 1) {       puts( 0 );     } else {       if (m == 1) {         move(up[1].x, up[1].y, up[1].tx, up[1].ty);       } else {         if (cmp_up(up[1], up[2]) == 0) {           swap(up[1], up[2]);         }         if (up[1].x == 2) {           move_up(up[1].x, up[1].y);           move_up(up[2].x, up[2].y);         } else if (up[2].x == 2) {           move_up(up[2].x, up[2].y);         }         switch (check(up[1])) {           case 0:             break;           case 1:             move(up[2].x, up[2].y, 2, 1);             move_right(up[1].x, up[1].y);             break;           case 2:             move(up[1].x, up[1].y, up[1].tx, up[1].ty);             break;           case 3:             move(up[1].x, up[1].y, up[1].tx, up[1].ty);             move_left(up[2].x, up[2].y);             break;         }         move(up[2].x, up[2].y, up[2].tx, up[2].ty);       }       cout << ans_fr.size() << endl;       for (int i = 0; i < ans_fr.size(); i++) {         int xx1 = ans_fr[i].first, xx2 = ans_to[i].first;         int yy1 = ans_fr[i].second, yy2 = ans_to[i].second;         printf( %d %d %d %d n , xx1, yy1, xx2, yy2);       }     }   }   int main() {     int n = read(), m = read();     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         mp[i][j] =  . ;       }     }     for (int i = 1; i <= m; i++) {       up[i].id = i;       nx[i] = up[i].x = read();       ny[i] = up[i].y = read();       has[nx[i]][ny[i]] = 1;       mp[nx[i]][ny[i]] = i +  0 ;     }     for (int i = 1; i <= m; i++) {       up[i].tx = read();       up[i].ty = read();     }     if (n <= 2) {       deal(n, m);       return 0;     }     copy(up + 1, up + 1 + m, down + 1);     sort(up + 1, up + 1 + m, cmp_up);     int cnt = m;     while (cnt) {       for (int i = 1; i <= m; i++) {         if (vis[i] == 0) {           int id = up[i].id;           move_row(nx[id], ny[id], 1, i);           if (ny[id] == i) {             vis[i] = 1;             cnt--;           }         }       }     }     for (int i = 1; i <= m; i++) {       int id = up[i].id;       while (nx[id] > 1) {         move_up(nx[id], ny[id]);       }     }     for (int i = 1; i <= m; i++) {       move_down(nx[i], ny[i]);       move_down(nx[i], ny[i]);     }     sort(down + 1, down + 1 + m, cmp_down);     for (int i = 1; i <= m; i++) {       int id = down[i].id;       move_up(nx[id], ny[id]);       move_row(nx[id], ny[id], 1, i);       move_up(nx[id], ny[id]);     }     for (int i = 1; i <= m; i++) {       int id = down[i].id;       while (nx[id] < down[i].tx) {         move_down(nx[id], ny[id]);       }     }     cnt = m;     while (cnt) {       for (int i = 1; i <= m; i++) {         if (vis[i] == 1) {           int id = down[i].id;           move_row(nx[id], ny[id], nx[id], down[i].ty);           if (ny[id] == down[i].ty) {             vis[i] = 2;             cnt--;           }         }       }     }     assert(ans_fr.size() == ans_to.size());     assert(ans_fr.size() <= 10800);     cout << ans_fr.size() << endl;     for (int i = 0; i < ans_fr.size(); i++) {       int xx1 = ans_fr[i].first, xx2 = ans_to[i].first;       int yy1 = ans_fr[i].second, yy2 = ans_to[i].second;       printf( %d %d %d %d n , xx1, yy1, xx2, yy2);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int a[m], b[m];     int ndup = n;     for (int i = 0; i < m; i++) {       cin >> a[i];       b[i] = a[i];     }     sort(a, a + m);     sort(b, b + m, greater<int>());     int mn = 0, mx = 0;     for (int i = 0; i < m; i++) {       while (a[i] > 0 && n > 0) {         mn += a[i];         a[i]--;         n--;       }     }     while (ndup > 0) {       mx += b[0];       b[0]--;       ndup--;       sort(b, b + m, greater<int>());     }     cout << mx <<     << mn;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b, c, d, e, f, g, h, i, j, k, l, m, n, t;     cin >> t;     while (t--) {       cin >> n;       cout << (n + 1) / 2 << endl;     }   }


//...START BY DOING WHAT IS NECESSARY, THEN WHAT IS POSSIBLE AND SUDDENLY YOU ARE DOING THE IMPOSSIBLE...    #include <bits/stdc++.h>   using namespace std;   #define FAST_FURIER ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);   #define pb push_back   #define mk make_pair   #define rep(i,a,N) for(ll i=a;i<N;i++)   #define rrep(i,a,N) for(ll i=a;i>N;i--)   typedef long long ll;   #define M 1000000007   bool comp(int x,int y)   {       return x > y;   }           /*..............................code starts here........................*/   // C is first won in M           int main() {       FAST_FURIER;       int tt=1;       cin >> tt;       ll m,d,k;      //ll a,b,c;       while(tt--)       {           cin >> d >> k;           ll x = 0,y = 0;           int f = 0;           while(true){               ll d1 = pow(x+k,2)+pow(y,2), d2 = pow(x,2)+pow(y+k,2);               f = abs(f-1);               if(d1 < d2 and d1 <= d*d){                   x += k;               }               else if(d2 <= d1 and d2 <= d*d) y += k;               else break;           }           if(f) cout <<  Utkarsh ;           else cout <<  Ashish ;           cout << endl;          }   }               // Author : shivam_123       // g++ -std=c++17


#include <bits/stdc++.h>   using namespace std;   long long a, b, c, e[200000], mix, l, r, o, d[200000];   vector<long long> v[100000], v1;   set<long long> st;   map<long long, long long> mp, mp1;   string s;   pair<int, int> p[100000], p1[100000];   char ch[10];   deque<char> dq;   int main() {     cin >> a;     for (int i = 0; i < a / 4; i++)       if ((a - 2 * (i + 1)) % 2 == 0) c++;     if (a % 4 == 0) {       cout << c - 1;     } else       cout << c;   }


#include <bits/stdc++.h>   using namespace std;   char s[5000005];   int len[1000005];   int idx[1000005];   int sum[1000005];   int sz = 0;   int dp[1000005][20];   int p[1000005];   int main() {     int n, r, c;     scanf( %d%d%d , &n, &r, &c);     gets(s);     gets(s);     int cnt = 0, slen = 0;     for (int i = 0; s[i]; i++, slen++) {       if (s[i] ==     && cnt != 0) {         len[sz] = cnt;         idx[sz++] = i - cnt;         cnt = 0;       } else         cnt++;     }     if (cnt > 0) {       len[sz] = cnt;       idx[sz++] = slen - cnt;       cnt = 0;     }     sum[0] = len[0];     for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + 1 + len[i];     for (int i = n - 1; i >= 0; i--) {       int id = upper_bound(sum, sum + n, (i > 0 ? sum[i - 1] + 1 : 0) + c) - sum;       if (id > n) id = n;       dp[i][0] = id - i;     }     for (int j = 2, t = 1; j <= r; j *= 2, t++) {       for (int i = n - 1; i >= 0; i--)         dp[i][t] = dp[i][t - 1] + dp[i + dp[i][t - 1]][t - 1];     }     int maid = -1, ma = 0;     for (int i = 0; i < n; i++) {       int idx = i, rr = r, cur = 0;       for (int j = 20; j >= 0; j--) {         if (rr >= (1 << j)) {           rr -= (1 << j);           cur += dp[idx][j];           idx += dp[idx][j];         }       }       if (cur > ma) maid = i, ma = cur;     }     int cn = 1, cs = dp[maid][0];     for (int i = maid; i < maid + ma; i++) {       s[idx[i] + len[i]] = 0;       if (cn < cs) {         printf( %s  , s + idx[i]);         cn++;       } else {         printf( %s n , s + idx[i]);         cs = dp[i + 1][0];         cn = 1;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char change(char curranswer) {     if (curranswer ==  z )       curranswer =  A ;     else if (curranswer ==  Z )       curranswer =  0 ;     else if (curranswer ==  9 )       ;     else       curranswer++;     return curranswer;   }   int main() {     long long i, j, t;     cin >> t;     while (t--) {       long long r, c, k;       long long rice = 0;       char a[100][100];       cin >> r >> c >> k;       for (i = 0; i < r; i++) {         for (j = 0; j < c; j++) {           cin >> a[i][j];           if (a[i][j] ==  R ) rice++;         }       }       long long ans = rice / k;       long long maxx = rice - k * ans;       long long minn = k - maxx;       char curranswer =  a ;       long long currcount = 0;       bool flag = true;       for (i = 0; i < r; i++) {         for (flag ? (j = 0) : (j = c - 1); flag ? (j < c) : j >= 0;              flag ? j++ : j--) {           bool local = false;           if (a[i][j] ==  R ) local = true;           a[i][j] = curranswer;           if (local) {             currcount++;             if (maxx > 0) {               if (currcount == ans + 1) {                 maxx--;                 currcount = 0;                 curranswer = change(curranswer);               }             } else if (minn > 0) {               if (currcount == ans) {                 minn--;                 currcount = 0;                 if (minn) curranswer = change(curranswer);               }             }           }         }         flag = !flag;       }       for (i = 0; i < r; i++) {         for (j = 0; j < c; j++) cout << a[i][j];         cout << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long x, long long y) {     if (y == 0)       return x;     else       return gcd(y, x % y);   }   long long expo(long long n, long long m) {     long long r = 1;     while (m > 0) {       if (m % 2) r = (r * n) % 1000000009;       n = (n * n) % 1000000009;       m = m / 2;     }     return r % 1000000009;   }   long long lx = 0, ly = 0;   bool check(long long x, long long y) {}   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long a, b;     cin >> a >> b;     string s;     cin >> s;     long long p = 0, q = 0;     if (a == 0 && b == 0) {       cout <<  Yes ;       return 0;     }     for (long long i = 0; i < s.size(); i++) {       if (s[i] ==  U ) {         ly++;       } else if (s[i] ==  D )         ly--;       else if (s[i] ==  L )         lx--;       else         lx++;       if (lx == a && ly == b) {         cout <<  Yes ;         return 0;       }     }     if (lx == 0 && ly == 0) {       cout <<  No ;       return 0;     }     for (long long i = 0; i < s.size(); i++) {       if (s[i] ==  U ) {         q++;       } else if (s[i] ==  D )         q--;       else if (s[i] ==  L )         p--;       else         p++;       long long x = a - p, y = b - q, X = lx, Y = ly;       if (lx == 0) {         if (ly > 0 && y > 0 && x == 0 && y % ly == 0) {           cout <<  Yes ;           return 0;         } else if (ly < 0 && y < 0 && x == 0) {           ly = -1 * ly, y = -1 * y;           if (y % ly == 0) {             cout <<  Yes ;             return 0;           }         }       } else if (ly == 0) {         if (lx > 0 && x > 0 && y == 0 && x % lx == 0) {           cout <<  Yes ;           return 0;         } else if (lx < 0 && x < 0 && y == 0) {           lx = -1 * lx, x = -1 * x;           if (x % lx == 0) {             cout <<  Yes ;             return 0;           }         }       } else {         if (lx > 0 && x > 0) {           if (ly > 0 && y > 0) {             if (x / lx == y / ly && x % lx == 0 && y % ly == 0) {               cout <<  Yes ;               return 0;             }           } else if (ly < 0 && y < 0) {             ly = -1 * ly, y = -1 * y;             if (x / lx == y / ly && x % lx == 0 && y % ly == 0) {               cout <<  Yes ;               return 0;             }           }         } else if (lx < 0 && x < 0) {           lx = -1 * lx, x = -1 * x;           if (ly > 0 && y > 0) {             if (x / lx == y / ly && x % lx == 0 && y % ly == 0) {               cout <<  Yes ;               return 0;             }           } else if (ly < 0 && y < 0) {             ly = -1 * ly, y = -1 * y;             if (x / lx == y / ly && x % lx == 0 && y % ly == 0) {               cout <<  Yes ;               return 0;             }           }         }       }       lx = X, ly = Y;     }     cout <<  No ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = int(3e5), mod = int(1e9) + 7;   int n;   int a[N], c[N];   long long t[N];   long long x[N], pref[N];   long double p[N];   long long suf[N];   void upd(int v, int val) {     v += n - 1;     t[v] += val;     c[v]++;     while (v > 1) {       v >>= 1;       t[v] = t[v + v] + t[v + v + 1];       c[v] = c[v + v] + c[v + v + 1];     }   }   long long get(int l, int r) {     l += n - 1;     r += n - 1;     long long res = 0;     while (l <= r) {       if (l & 1) res += t[l];       if (!(r & 1)) res += t[r];       l = (l + 1) >> 1;       r = (r - 1) >> 1;     }     return res;   }   int cnt(int l, int r) {     l += n - 1;     r += n - 1;     int res = 0;     while (l <= r) {       if (l & 1) res += c[l];       if (!(r & 1)) res += c[r];       l = (l + 1) >> 1;       r = (r - 1) >> 1;     }     return res;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     long long all = 1ll * n * (n + 1) / 2;     long double res = 0;     for (int i = 1; i <= n; i++) {       long long cur = get(1, a[i] - 1);       long double p = (cur + 0.0) * (n - i + 1) / all;       res += p * 0.5;       cur = get(a[i] + 1, n);       p = (cur + 0.0) * (n - i + 1) / all;       res += cnt(a[i] + 1, n);       res -= p * 0.5;       upd(a[i], i);     }     printf( %.12lf , double(res));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b;     cin >> a >> b;     long long ans = 0;     while (true) {       long long x = max(a, b);       long long y = min(a, b);       if (x == 0 || y == 0) break;       long long n = x / y;       ans += n;       x -= n * y;       a = x;       b = y;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int x, test;     cin >> test;     while (test--) {       int n, c0, c1, h;       string s;       cin >> n >> c0 >> c1 >> h >> s;       int cnt0 = 0, cnt1 = 0;       for (auto &z : s) {         cnt0 += z ==  0 ;         cnt1 += z ==  1 ;       }       int cost = (cnt0 * c0) + (cnt1 * c1);       int cost0 = (c0 * n) + (cnt1 * h);       int cost1 = (c1 * n) + (cnt0 * h);       cout << min({cost, cost0, cost1}) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = int(1e9) + 123;   int main() {     int n, v;     cin >> n >> v;     vector<int> a;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       a.push_back(x);     }     sort(a.begin(), a.end());     int sum = 0;     for (int i = 0; i < a.size() - 1; i++) {       sum += a[i];     }     if (sum <= v) {       cout <<  YES ;     } else {       cout <<  NO ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string vow =  aeiou ;   long long month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   const long long dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};   const long long dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};   const long long dx4[] = {-1, 0, 1, 0};   const long long dy4[] = {0, 1, 0, -1};   const long long dx8[] = {-1, -1, -1, 0, 0, 1, 1, 1};   const long long dy8[] = {-1, 0, 1, -1, 1, -1, 0, 1};   const long double pie = 3.1415926535897932384626;   const long long special_prime = 982451653l;   const long long mod = 1e9 + 7;   const long long inf = 2e18;   template <class T>   istream &operator>>(istream &in, vector<T> &a) {     for (auto &i : a) cin >> i;     return in;   }   template <class T>   ostream &operator<<(ostream &out, const vector<T> &a) {     for (auto &i : a) cout << i <<    ;     return out;   }   template <class T, class U>   ostream &operator<<(ostream &out, const map<T, U> &a) {     for (auto &i : a) cout <<  (  << i.first <<  ,   << i.second <<  ) n ;     return out;   }   template <class T, class U>   ostream &operator<<(ostream &out, const unordered_map<T, U> &a) {     for (auto &i : a) cout <<  (  << i.first <<  ,   << i.second <<  ) n ;     return out;   }   long long GCD(long long a, long long b) {     if (!b) return a;     return GCD(b, a % b);   }   long long LCM(long long a, long long b) { return (a * b) / GCD(a, b); }   bool comp(long long a, long long b) { return a > b; }   void sieve(vector<long long> &prime) {     prime[0] = false;     prime[1] = false;     for (long long p = 2; p * p <= 99999; p++) {       if (prime[p] == true) {         for (long long i = p * p; i <= 99999; i += p) prime[i] = false;       }     }   }   void solve() {     long long n, g, b;     cin >> n >> g >> b;     long long d = n / 2 + n % 2;     if (d % g)       cout << max(n, (d / g) * (g + b) + d % g) <<   n ;     else       cout << max((d / g) * (g + b) - b, n) <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     cin >> t;     cout << setprecision(15) << fixed;     for (long long i = 1; i <= t; i++) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int mod = (int)1e9 + 7;   const int N = (int)1e6 + 6;   int f[30][N];   int pref[30][N];   int lp[N];   void precalc() {     pref[0][0] = f[0][0] = 1;     for (int i = 1; i < 30; i++) {       f[i][0] = 2;       pref[i][0] = f[i][0] + pref[i - 1][0];       pref[i][0] -= pref[i][0] >= mod ? mod : 0;     }     for (int level = 1; level < N; level++) {       pref[0][level] = f[0][level] = 1;       for (int pw = 1; pw < 30; pw++) {         f[pw][level] = pref[pw][level - 1];         pref[pw][level] = f[pw][level] + pref[pw - 1][level];         pref[pw][level] -= pref[pw][level] >= mod ? mod : 0;       }     }     for (int i = 2; i < N; i++) {       if (lp[i]) continue;       for (int j = i; j < N; j += i) {         lp[j] = lp[j] ? lp[j] : i;       }     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     precalc();     int T;     scanf( %d , &T);     while (T--) {       int r, n;       scanf( %d%d , &r, &n);       if (n == 1) {         printf( 1 n );         continue;       }       long long ans = 1;       while (n > 1) {         int p = lp[n];         int pw = 0;         while (lp[n] == p) {           n /= p;           ++pw;         }         ans = ans * f[pw][r] % mod;       }       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }   const int MAXN = 3e5 + 10;   const int INF = 0x3f3f3f3f;   const int MOD = 1e7 + 9;   int a[MAXN];   int main() {     int n;     scanf( %d , &n);     for (int i = (1); i < (n + 1); ++i) {       scanf( %d , &a[i]);     }     int ans1 = 0;     int t1 = 1;     for (int i = (n); i > (0); --i) {       if (a[t1] != a[i]) {         ans1 = i - t1;         break;       } else {         t1++;         i++;       }     }     int ans2 = 0;     int t2 = n;     for (int i = (1); i < (n + 1); ++i) {       if (a[t2] != a[i]) {         ans2 = t2 - i;         break;       } else {         t2--;         i--;       }     }     printf( %d n , max(ans1, ans2));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, sum = 1;     cin >> n;     sum += 6 * ((n * (n + 1)) / 2);     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 500005;   const int M = 998244353;   int n, a[N], i, s;   long long f[N], c[N], x, y, z, ans[N], tp;   long long qpow(long long a, long long b) {     long long s = a, ans = 1;     while (b) {       if (b & 1) ans = ans * s % M;       s = s * s % M;       b >>= 1;     }     return ans;   }   int main() {     scanf( %d , &n);     for (i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       s += a[i];     }     long long is = qpow(s, M - 2), in = qpow(n - 1, M - 2);     c[1] = 1 - n;     f[0] = f[1] = 1;     for (i = 1; i < s; ++i) {       x = i * is % M, y = (s - i) * is % M * in % M;       z = qpow(y, M - 2);       f[i + 1] = (f[i] * (x + y) - f[i - 1] * x) % M * z % M;       c[i + 1] = (c[i] * (x + y) - 1 - c[i - 1] * x) % M * z % M;     }     ans[0] = -c[s] * qpow(f[s], M - 2) % M;     for (i = 1; i < s; ++i) ans[i] = (f[i] * ans[0] + c[i]) % M;     for (i = 1; i <= n; ++i) tp += ans[a[i]];     tp -= ans[0] * (n - 1);     cout << (tp % M * qpow(n, M - 2) % M + M) % M;   }


#include <bits/stdc++.h>   using namespace std;   long long w[2], b[2], q[2], mod = 998244353, fac[100001];   long long modpow(long long b, long long p) {     long long ret = 1;     while (p > 0) {       if (p & 1) ret = ret * b % mod;       b = b * b % mod;       p >>= 1;     }     return ret;   }   long long choose(long long n, long long m) {     long long denom = modpow(fac[m], mod - 2) * modpow(fac[n - m], mod - 2) % mod;     return (fac[n] * denom % mod);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long n, qq = 0;     bool bw = true, wb = true, both = true;     string s;     fac[0] = 1;     for (int i = 1; i < 100001; i++) fac[i] = fac[i - 1] * i % mod;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> s;       for (int j = 0; j < 2; j++) {         if (s[j] ==  W ) w[j]++;         if (s[j] ==  B ) b[j]++;         if (s[j] ==  ? ) q[j]++;       }       if (s ==  ?? ) qq++;       if (s ==  WW  || s ==  BB ) bw = wb = both = false;       if (s ==  W?  || s ==  ?B  || s ==  WB ) bw = false;       if (s ==  B?  || s ==  ?W  || s ==  BW ) wb = false;     }     if (n == 1) {       if (s ==  WW  | s ==  BB )         cout << 0;       else if (s ==  ?? )         cout << 2;       else         cout << 1;       return 0;     }     long long ans = 0;     for (int i = 0; i < q[0] + 1; i++) {       if (w[0] + i < b[1] || w[0] + i > b[1] + q[1]) continue;       ans = (ans + choose(q[0], i) * choose(q[1], w[0] + i - b[1])) % mod;     }     if (both) {       ans -= modpow(2, qq);       if (bw) ans++;       if (wb) ans++;     }     ans = ((ans % mod) + mod) % mod;     cout << ans;   }


#include <bits/stdc++.h>   using std::abs;   using std::bitset;   using std::cerr;   using std::cin;   using std::copy;   using std::cout;   using std::endl;   using std::fill;   using std::fixed;   using std::greater;   using std::lower_bound;   using std::map;   using std::max;   using std::min;   using std::next_permutation;   using std::pair;   using std::priority_queue;   using std::queue;   using std::reverse;   using std::rotate;   using std::set;   using std::sort;   using std::string;   using std::swap;   using std::unique;   using std::unordered_map;   using std::unordered_set;   using std::upper_bound;   using std::vector;   int const INF = (int)1e9;   long long const INFL = (long long)1e18;   long double const PI = 3.1415926535897932384626433832795028;   bool isPrime(int x) {     for (int a = 2; a * a <= x; ++a) {       if (x % a == 0) {         return false;       }     }     return true;   }   int const N = 100100;   int len[N];   int g[N], next[N];   int find(int v) {     if (next[v] != v) {       int root = find(next[v]);       len[v] += len[next[v]];       next[v] = root;     }     return next[v];   }   int v[N], res[N];   bool did[N];   int calc(int a, int b) {     int ret = 0;     find(a);     b -= len[a];     a = next[a];     while (b > 0 && !did[a]) {       ++ret;       did[a] = true;       next[a] = find(g[a]);       len[a] = 1 + len[g[a]];       b -= len[a];       a = find(g[a]);     }     return ret;   }   void solve() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; ++i) {       next[i] = i;       len[i] = 0;       cin >> g[i];       --g[i];     }     for (int i = 1; i <= m; ++i) {       cin >> v[i];     }     for (int i = 1; i <= m; ++i) {       int b;       cin >> b;       int a = (v[i] + res[i - 1] - 1) % n;       res[i] = calc(a, b);     }     for (int i = 1; i <= m; ++i) {       cout << res[i] <<   n ;     }   }   int main() { solve(); }


#include <bits/stdc++.h>   using namespace std;   bool vis[305][305];   string s[305];   int len[305][305];   int d[305];   int hs[305][305];   int n;   const int mod = 9e8 + 7;   int qpow(int x, int y) {     long long ret = 1;     long long now = x;     while (y) {       if (y & 1) ret = ret * now % mod;       now = now * now % mod;       y >>= 1;     }     return ret;   }   void init() {     memset(vis, false, sizeof(vis));     for (int i = 1; i <= n; i++) {       d[i] = 0;       len[i][0] = s[i].size();       for (int j = 0; j < s[i].size(); j++) {         d[i] = (d[i] * 233LL + s[i][j] -  a  + 1) % mod;       }     }     for (int i = 1; i <= n; i++) {       hs[i][0] = d[i];       for (int j = 1; j + i <= n; j++) {         len[i][j] = len[i + j][0] + len[i][j - 1];         hs[i][j] = (1LL * hs[i][j - 1] * 2333LL + d[i + j]) % mod;       }     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) cin >> s[i];     init();     int ans = 0;     for (int i = 1; i <= n; i++) {       int ls = 0;       for (int j = 0; i + j <= n; j++) {         ls += len[i + j][0];         int nx = i + j + 1;         int cnt = 1;         while (nx + j <= n) {           if (hs[i][j] == hs[nx][j] && len[i][j] == len[nx][j]) {             cnt++;             nx += j + 1;           } else {             nx++;           }         }         if (cnt > 1) ans = max(ans, (ls - 1) * cnt);       }     }     ans = n - 1 - ans;     for (int i = 1; i <= n; i++) ans += len[i][0];     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> v[100005];   int p[100005];   int h[100005];   int happy[100005];   int sad[100005];   bool vis[100005];   bool ok;   void init(int n) {     for (int i = 1; i < n + 1; i++) {       v[i].clear();       vis[i] = false;     }     ok = true;   }   void dfs(int node) {     vis[node] = true;     long long int happyChild = 0;     long long int sadChild = p[node];     for (int child : v[node]) {       if (vis[child]) continue;       dfs(child);       happyChild += happy[child];       sadChild += sad[child];     }     long long int gg = (long long int)h[node] - (happyChild - sadChild);     if (gg >= 0 && gg % 2 == 0 && gg / 2 <= sadChild) {       long long int x = gg / 2;       happy[node] = x + happyChild;       sad[node] = sadChild - x;     } else {       ok = false;       return;     }   }   int main() {     int t, n, m, a, b;     scanf( %d , &t);     while (t--) {       scanf( %d %d , &n, &m);       init(n);       for (int i = 1; i < n + 1; i++) scanf( %d , &p[i]);       for (int i = 1; i < n + 1; i++) scanf( %d , &h[i]);       for (int i = 0; i < n - 1; i++) {         scanf( %d %d , &a, &b);         v[a].push_back(b);         v[b].push_back(a);       }       dfs(1);       if (ok)         puts( YES );       else         puts( NO );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long j, k, a, b, s, ss, l, m, n, t, d;   long long e, i, c[221];   vector<long long> p[111], q[111], r, v;   char w;   map<long long, long long> y, z;   long long too(long long i, long long j) {     long long l = 0, d = 0;     for (k = 0; k < q[i].size(); k++) {       l += q[i][k];       r.push_back(l);     }     l = 0;     for (k = 0; k < q[j].size(); k++) {       l += q[j][k];       r.push_back(l);     }     l = r.size();     sort(r.begin(), r.end());     for (k = 0; k < l; k++) c[k] = 0;     for (k = 0; k < l; k++) {       long long x, a = 0, b = 0, e = 0;       x = r[k];       while (x > q[i][e] && e < q[i].size()) {         a += p[i][e] * q[i][e];         x -= q[i][e];         e++;       }       if (e < q[i].size()) a += p[i][e] * x;       x = r[k];       e = 0;       while (x > q[j][e] && e < q[j].size()) {         b += p[j][e] * q[j][e];         x -= q[j][e];         e++;       }       if (e < q[j].size()) b += p[j][e] * x;       if (a > b) c[k] = 1;       if (a < b) c[k] = 2;     }     e = 0;     while (c[e] == 0) e++;     for (k = e + 1; k < l; k++) {       if (c[k] == 0) c[k] = c[k - 1];     }     for (k = e + 1; k < l; k++)       if (c[k] != c[k - 1]) d++;     return d;   }   int main() {     cin >> n >> s;     for (i = 0; i < n; i++) {       cin >> k;       for (j = 0; j < k; j++) {         cin >> a >> b;         p[i].push_back(a);         q[i].push_back(b);       }     }     for (i = 0; i < n; i++) {       for (j = i + 1; j < n; j++) {         ss += too(i, j);         r = v;       }     }     cout << ss;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = int(1e6) + 50;   char S[N], T[N];   int slen, tlen, lef[N], rig[N], cnt[255][255];   inline int getID(char c) {     if (c ==  R ) return 0;     if (c ==  G ) return 1;     if (c ==  B ) return 2;     throw;   }   int main() {     scanf( %s , S + 1);     slen = strlen(S + 1);     scanf( %s , T + 1);     tlen = strlen(T + 1);     int l = 1, r = 1;     while (l <= slen && r <= tlen) {       lef[l] = r;       r += S[l] == T[r];       ++l;     }     slen = min(slen, l - 1);     l = r = 1;     while (l <= slen && r <= tlen) {       rig[l] = r;       l += S[l] == T[r];       ++r;     }     tlen = min(tlen, r - 1);     for (int i = l + bool(rig[l]); i <= slen; ++i) {       rig[i] = tlen;     }     long long res = 0;     for (int i = 1; i <= slen; ++i) {       for (int j = max(1, lef[i - 1]); j < lef[i]; ++j) --cnt[T[j]][T[j - 1]];       for (int j = 1 + rig[i - 1]; j <= rig[i]; ++j) ++cnt[T[j]][T[j - 1]];       res += rig[i] - lef[i] + 1;       if (S[i - 1] != S[i]) res -= cnt[S[i - 1]][S[i]];     }     printf( %I64d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, k;   long long ans;   int main() {     cin >> n >> m >> k;     if (k > n + m - 2) {       cout <<  -1  << endl;       return 0;     }     if (n >= m) swap(n, m);     if (k <= m - 1) {       ans = (long long)(m / (k + 1)) * n;     } else {       ans = (long long)(n / (k - m + 2));     }     if (k <= n - 1) {       ans = max(ans, (long long)(n / (k + 1)) * m);     } else {       ans = max(ans, (long long)(m / (k - n + 2)));     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 110;   int n, m, k;   int h[MAXN];   void load() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 0; i < n; i++) scanf( %d , h + i);   }   bool solve() {     long long sum = m;     int ind = 0;     while (ind < n - 1) {       if (h[ind] >= h[ind + 1] - k) {         int nxt = max(h[ind + 1] - k, 0);         sum += h[ind] - nxt;       } else {         if (sum + h[ind] < h[ind + 1] - k) return false;         sum -= h[ind + 1] - k - h[ind];       }       ind++;     }     return true;   }   int main() {     int t;     scanf( %d , &t);     while (t--) {       load();       if (solve())         printf( YES n );       else         printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 3e5 + 10;   int n, k;   int a[MAXN], f[MAXN];   int bsL(int l, int r, int val) {     int pos = -1;     while (l <= r) {       int mid = (l + r) >> 1;       if (a[mid] >= val) {         pos = mid;         r = mid - 1;       } else         l = mid + 1;     }     return pos;   }   bool check(int x) {     f[1] = 1;     for (int i = 1, l = 1; i <= n; i++) {       while (a[i] - a[l] > x) l++;       f[i + 1] = (i >= k && f[i - k + 1] - f[l - 1] > 0);       f[i + 1] += f[i];     }     return f[n + 1] - f[n] > 0;   }   int bs(int l, int r) {     int pos = 0;     while (l <= r) {       int mid = (l + r) >> 1;       if (check(mid)) {         pos = mid;         r = mid - 1;       } else         l = mid + 1;     }     return pos;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> a[i];     sort(a + 1, a + 1 + n);     cout << bs(0, 1e9);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 10;   long long h[N], e[2 * N], ne[2 * N], idx;   void add(long long a, long long b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }   struct node {     long long sum, add;   };   node tr[N << 2];   long long arr[N];   long long f[N][20];   long long dep[N], fa[N], son[N], sz[N];   long long n, q;   void dfs1(long long u, long long fz) {     fa[u] = fz;     dep[u] = dep[fz] + 1;     sz[u] = 1;     f[u][0] = fz;     for (long long i = 1; i < 20; ++i) f[u][i] = f[f[u][i - 1]][i - 1];     for (long long i = h[u]; i != -1; i = ne[i]) {       long long j = e[i];       if (j == fz) continue;       dfs1(j, u);       sz[u] += sz[j];       if (sz[j] > sz[son[u]]) {         son[u] = j;       }     }   }   long long id[N], top[N], uid[N], tot;   void dfs2(long long u, long long topx) {     top[u] = topx;     id[u] = ++tot;     uid[tot] = u;     if (!son[u]) return;     dfs2(son[u], topx);     for (long long i = h[u]; i != -1; i = ne[i]) {       long long j = e[i];       if (j == fa[u] || j == son[u]) continue;       dfs2(j, j);     }   }   long long get_lca(long long a, long long b) {     while (top[a] != top[b]) {       if (dep[top[a]] > dep[top[b]])         a = fa[top[a]];       else         b = fa[top[b]];     }     return dep[a] > dep[b] ? b : a;   }   long long topC(long long x, long long c) {     for (long long i = 19; i >= 0; i--) {       if ((1 << i) <= c) {         x = f[x][i];         c -= (1 << i);       }     }     return x;   }   long long root;   void pushup(long long u) { tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum; }   void pushdown(long long l, long long r, long long u) {     if (tr[u].add) {       long long mid = l + r >> 1;       tr[u << 1].sum += (mid - l + 1) * tr[u].add;       tr[u << 1 | 1].sum += (r - mid) * tr[u].add;       tr[u << 1].add += tr[u].add;       tr[u << 1 | 1].add += tr[u].add;       tr[u].add = 0;     }   }   long long query(long long l, long long r, long long a, long long b,                   long long u) {     if (l <= a && b <= r) {       return tr[u].sum;     }     pushdown(a, b, u);     long long mid = a + b >> 1;     long long ans = 0;     if (l <= mid) ans += query(l, r, a, mid, u << 1);     if (r > mid) ans += query(l, r, mid + 1, b, u << 1 | 1);     return ans;   }   void ud(long long l, long long r, long long a, long long b, long long d,           long long u) {     if (l <= a && b <= r) {       tr[u].add += d;       tr[u].sum += (b - a + 1) * d;       return;     }     pushdown(a, b, u);     long long mid = a + b >> 1;     if (l <= mid) ud(l, r, a, mid, d, u << 1);     if (r > mid) ud(l, r, mid + 1, b, d, u << 1 | 1);     pushup(u);   }   void update(long long u, long long x) {     ud(id[u], id[u] + sz[u] - 1, 1, n, x, 1);   }   long long qry(long long u) { return query(id[u], id[u] + sz[u] - 1, 1, n, 1); }   void build(long long l, long long r, long long u) {     tr[u].add = 0;     if (l == r) {       tr[u].sum = arr[uid[l]];       return;     }     long long mid = l + r >> 1;     build(l, mid, u << 1);     build(mid + 1, r, u << 1 | 1);     pushup(u);   }   int main() {     ios::sync_with_stdio(false);     std::cin.tie(0);     memset(h, -1, sizeof h);     cin >> n >> q;     for (long long i = 1; i <= n; ++i) cin >> arr[i];     for (long long i = 1; i < n; ++i) {       long long a, b;       cin >> a >> b;       add(a, b);       add(b, a);     }     dfs1(1, 1);     dfs2(1, 1);     build(1, n, 1);     root = 1;     while (q--) {       long long t;       cin >> t;       long long v, u, x;       if (t == 1) {         cin >> v;         root = v;       } else if (t == 2) {         cin >> u >> v >> x;         long long t1 = get_lca(u, root);         long long t2 = get_lca(v, root);         long long t3 = get_lca(u, v);         long long ans = t1;         if (dep[ans] < dep[t2]) ans = t2;         if (dep[ans] < dep[t3]) ans = t3;         long long lca = get_lca(root, ans);         if (ans == root) {           update(1, x);         } else if (lca != ans) {           update(ans, x);         } else {           update(1, x);           long long s = topC(root, dep[root] - dep[ans] - 1);           update(s, -x);         }       } else {         cin >> v;         long long ans = v;         long long lca = get_lca(root, ans);         if (ans == root) {           cout << qry(1) << endl;         } else if (lca != ans) {           cout << qry(ans) << endl;         } else {           long long s = topC(root, dep[root] - dep[ans] - 1);           cout << (qry(1) - qry(s)) << endl;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const double EPS = 1e-9;   const int INF = 1 << 29;   int const NN = 5e3 + 3;   int const MX = 1e9 + 9;   int a[505][505];   int mx[505][505];   int main() {     int n, m, K, q;     cin >> n >> m >> K >> q;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) a[i][j] = MX;     }     while (q--) {       int x, y, t;       cin >> x >> y >> t;       a[x][y] = t;     }     int ans = MX;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         mx[i][j] = a[i][j];         for (int k = 2; i + k - 1 <= n && k <= K; ++k) {           mx[i][j] = max(mx[i][j], a[i + k - 1][j]);         }       }     }     for (int i = 1; i + K - 1 <= n; ++i) {       for (int j = 1; j + K - 1 <= m; ++j) {         int mxx = -1;         for (int jj = j; jj <= j + K - 1; ++jj) {           mxx = max(mxx, mx[i][jj]);         }         ans = min(ans, mxx);       }     }     if (ans < MX) {       printf( %d , ans);       ;       putchar(  n );     } else       puts( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;      void solve() {       int n, k;       cin >> n >> k;       if(2 * k + 1 > n) {           cout <<  -1 n ;           return;       }       vector<int> v(n);       int p = n;       for(int i = 1; i < 1 + 2 * k; i += 2)           v[i] = p--;       p = 1;       for(int i = 0; i < n; i++) {           if(v[i])               continue;           v[i] = p++;       }       for(auto &x : v)           cout << x <<    ;       cout <<   n ;   }      int main() {       ios_base::sync_with_stdio(false);       cin.tie(0);          int t;       cin >> t;       while(t--)           solve();       }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   char S[maxn][10];   int ju(char a[], char b[]) {     int n = strlen(a), m = strlen(b);     int x = 0, y = 0;     for (int i = 0; i < n; i++) x = x * 10 + a[i] -  0 ;     for (int j = 0; j < m; j++) y = y * 10 + (b[j] ==  ?  ? 9 : b[j] -  0 );     return x < y;   }   int f(int ind) {     int m = strlen(S[ind - 1]), n = strlen(S[ind]);     if (m < n) {       for (int i = 0; i < n; i++)         if (S[ind][i] ==  ? ) {           if (i == 0)             S[ind][i] =  1 ;           else             S[ind][i] =  0 ;         }     } else if (m > n) {       return 1;     } else {       for (int i = 0; i < n; i++) {         if (S[ind][i] ==  ? )           for (int j = 0; j <= 9; j++) {             S[ind][i] =  0  + j;             if (ju(S[ind - 1], S[ind])) break;           }       }       if (!ju(S[ind - 1], S[ind])) return 1;     }     return 0;   }   int main() {     int n;     while (scanf( %d , &n) > 0) {       int si = 1;       S[0][0] =  0 ;       S[0][1] =   0 ;       for (int i = 1; i <= n; i++) {         scanf( %s , S[i]);         if (si && f(i)) si = 0;       }       if (si) {         printf( YES n );         for (int i = 1; i <= n; i++) printf( %s n , S[i]);       } else {         printf( NO n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Friend {     string name;     int x, y, z;   } f[123];   bool Isde(char *a) {     if (a[0] <= a[1]) return false;     if (a[1] <= a[3]) return false;     if (a[3] <= a[4]) return false;     if (a[4] <= a[6]) return false;     if (a[6] <= a[7]) return false;     return true;   }   bool Isiden(char *a) {     if (a[0] != a[1]) return false;     if (a[1] != a[3]) return false;     if (a[3] != a[4]) return false;     if (a[4] != a[6]) return false;     if (a[6] != a[7]) return false;     return true;   }   int main() {     int n;     while (cin >> n) {       int mx = 0, my = 0, mz = 0;       for (int i = 0; i < n; ++i) {         int m;         cin >> m >> f[i].name;         f[i].x = f[i].y = f[i].z;         char str[100];         for (int j = 0; j < m; ++j) {           cin >> str;           if (Isiden(str))             f[i].x++;           else if (Isde(str))             f[i].y++;           else             f[i].z++;         }         mx = max(f[i].x, mx);         my = max(f[i].y, my);         mz = max(f[i].z, mz);       }       bool flag = false;       cout <<  If you want to call a taxi, you should call:  ;       for (int i = 0; i < n; ++i) {         if (f[i].x == mx) {           if (flag) cout <<  ,  ;           cout << f[i].name;           flag = true;         }       }       cout <<  .  << endl;       flag = false;       cout <<  If you want to order a pizza, you should call:  ;       for (int i = 0; i < n; ++i) {         if (f[i].y == my) {           if (flag) cout <<  ,  ;           cout << f[i].name;           flag = true;         }       }       cout <<  .  << endl;       flag = false;       cout <<  If you want to go to a cafe with a wonderful girl, you should                  call:  ;       for (int i = 0; i < n; ++i) {         if (f[i].z == mz) {           if (flag) cout <<  ,  ;           cout << f[i].name;           flag = true;         }       }       cout <<  .  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int L = 0, R = 10000;   int n, vote, tot;   vector<int> p[110000];   pair<int, int> x[110000];   long long sum[110000];   long long he[4 * 110000];   int num[4 * 110000];   long long ask(int root, int l, int r, int k) {     if (k <= 0)       return 0;     else if (num[root] == k)       return he[root];     else if (l == r)       return (long long)k * l;     else {       int mid = (l + r) >> 1;       if (num[2 * root] < k) {         return ask(2 * root, l, mid, num[2 * root]) +                ask(2 * root + 1, mid + 1, r, k - num[2 * root]);       } else {         return ask(2 * root, l, mid, k);       }     }   }   void add(int root, int l, int r, int k) {     if (l > r || k < l || k > r)       return;     else if (l == r) {       num[root]++;       he[root] += k;     } else {       num[root]++;       he[root] += k;       int mid = (l + r) >> 1;       add(2 * root, l, mid, k);       add(2 * root + 1, mid + 1, r, k);     }   }   int main() {     cin >> n;     int mx = 0;     for (int i = 0, a, b; i < n; i++) {       scanf( %d%d , &a, &b);       if (a != 0)         p[a].push_back(b);       else         vote++;     }     for (int i = 1; i < 110000; i++) {       sort(p[i].begin(), p[i].end());       reverse(p[i].begin(), p[i].end());       mx = max(mx, (int)p[i].size());     }     for (int i = 1; i < 110000; i++) {       int m = (int)p[i].size();       for (int j = 0; j < m; j++) {         x[tot++] = pair<int, int>(j + 1, p[i][j]);       }     }     sort(x, x + tot);     for (int i = 0; i < tot; i++) {       if (i == 0)         sum[i] = x[i].second;       else         sum[i] = sum[i - 1] + x[i].second;     }     long long ans = 1e10;     int pre = 0;     for (int i = 0; i <= mx + 1; i++) {       int indx = upper_bound(x, x + tot, pair<int, int>(i, 1e9)) - x;       long long cans = sum[tot - 1] - sum[indx] + x[indx].second;       if (indx == tot) cans = 0;       while (pre < indx) {         add(1, L, R, x[pre].second);         pre++;       }       indx = tot - indx + vote;       if (i >= indx) {         indx = i - indx + 1;         cans += ask(1, L, R, indx);       }       ans = min(ans, cans);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 998244353;   long long inv(long long a) {     long long u = 0, v = 1, b = mod;     while (a) {       long long t = b / a;       b -= t * a, u -= t * v;       swap(a, b);       swap(u, v);     }     u %= mod;     if (u < 0) u += mod;     return u;   }   struct mint {     int x;     mint() : x(0) {}     mint(int x) : x(x) {}     mint(long long x) : x() {       if (x >= mod || x < -mod) x %= mod;       if (x < 0) x += mod;       this->x = x;     }     mint &operator-=(const mint &rhs) {       x -= rhs.x;       if (x < 0) x += mod;       return *this;     }     mint &operator+=(const mint &rhs) {       x += rhs.x;       if (x >= mod) x -= mod;       return *this;     }     mint &operator*=(const mint &rhs) {       x = (long long)x * rhs.x % mod;       return *this;     }     mint &operator/=(const mint &rhs) {       x = (long long)x * inv(rhs.x) % mod;       return *this;     }     bool operator<(const mint &rhs) { return x < rhs.x; }     bool operator==(const mint &rhs) { return x == rhs.x; }     bool operator!=(const mint &rhs) { return x != rhs.x; }     friend mint operator+(const mint &lhs, const mint &rhs) {       mint ret(lhs);       return ret += rhs;     }     friend mint operator-(const mint &lhs, const mint &rhs) {       mint ret(lhs);       return ret -= rhs;     }     friend mint operator*(const mint &lhs, const mint &rhs) {       mint ret(lhs);       return ret *= rhs;     }     friend mint operator/(const mint &lhs, const mint &rhs) {       mint ret(lhs);       return ret /= rhs;     }     friend ostream &operator<<(ostream &os, const mint &rhs) {       os << rhs.x;       return os;     }     friend istream &operator>>(istream &is, mint &rhs) {       long long a;       is >> a;       rhs = a;       return is;     }   };   mint fpow(mint a, long long b) {     mint r = 1;     for (; b; b >>= 1) {       if (b & 1) r = r * a;       a = a * a;     }     return r;   }   const int N = 1e6 + 5;   mint pw[N];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m, k;     cin >> n >> m >> k;     pw[0] = 1;     for (int i = 1; i <= max(n, m); ++i) pw[i] = pw[i - 1] * 2;     int f0 = 1, f1 = 1, cntx = n, cnty = m, fx = 0, fy = 0;     vector<int> cnt(2);     vector<vector<int>> cntr(n, vector<int>(2, 0)), cntc(m, vector<int>(2, 0));     map<pair<int, int>, int> mp;     auto Erase = [&](int x, int y) {       if (!mp.count({x, y})) return;       int p = mp[{x, y}];       mp.erase({x, y});       if (--cntr[x][p ^ y & 1] == 0) {         if (cntr[x][p ^ y & 1 ^ 1])           fx--;         else           cntx += 1;       }       if (--cntc[y][p ^ x & 1] == 0) {         if (cntc[y][p ^ x & 1 ^ 1])           fy--;         else           cnty += 1;       }       --cnt[x & 1 ^ y & 1 ^ p];     };     auto Insert = [&](int x, int y, int p) {       mp[{x, y}] = p;       if (cntr[x][p ^ y & 1]++ == 0) {         if (cntr[x][p ^ y & 1 ^ 1])           fx++;         else           cntx -= 1;       }       if (cntc[y][p ^ x & 1]++ == 0) {         if (cntc[y][p ^ x & 1 ^ 1])           fy++;         else           cnty -= 1;       }       ++cnt[x & 1 ^ y & 1 ^ p];     };     while (k--) {       int x, y, op;       cin >> x >> y >> op;       --x, --y;       Erase(x, y);       if (op != -1) Insert(x, y, op);       mint ans = -(cnt[0] == 0) - (cnt[1] == 0);       if (fx == 0) ans += pw[cntx];       if (fy == 0) ans += pw[cnty];       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int i, a, p, b, p1, p2;     cin >> a;     b = a;     p = 0;     p1 = p2 = 1;     for (i = 2; i <= sqrt(b); i++) {       if (b % i == 0) {         b = b / i;         p++;         if (p == 1) p1 = i;         if (p == 2) p2 = i;         i--;       }     }     if (p == 0) cout << 1 << endl << 0 << endl;     if (p == 1) cout << 2 << endl;     if (p >= 2) cout << 1 << endl << p1 * p2 << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <class P, class Q>   ostream& operator<<(ostream& stream, pair<P, Q> v) {     stream <<  (  << v.first <<  ,  << v.second <<  ) ;     return stream;   }   template <class T>   ostream& operator<<(ostream& stream, const vector<T> v) {     stream <<  [  ;     for (int i = 0; i < (int)v.size(); i++) stream << v[i] <<    ;     stream <<  ] ;     return stream;   }   template <class T>   T smin(T& a, T b) {     if (a > b) a = b;     return a;   }   template <class T>   T smax(T& a, T b) {     if (a < b) a = b;     return a;   }   const long long M = 10004205361450474;   int ask(vector<long long> s) {     int sz = s.size();     cout << sz <<    ;     for (int i = 0; i < sz; ++i) cout << s[i] <<    n [i == sz - 1];     fflush(stdout);     int v = 0;     cin >> v;     if (v == -1) {       exit(0);     }     return v;   }   const int N = 1e4 + 5;   long long dp[N][6];   long long f(int A, int B) {     if (!B) return 0;     if (dp[A][B] != -1) return dp[A][B];     long long& ans = dp[A][B];     ans = 0;     const int W = A;     for (int i = 0; i < W; ++i) {       ans += f(A, B - 1) + 1;       A = min(10000ll, A + f(A, B - 1) + 1);     }     ans += f(A, B - 1);     return ans;   }   long long g(long long a, int b) { return f(min(10000ll, a), b); }   int main(void) {     memset(dp, -1, sizeof(dp));     const long long c1 = M - f(10000, 4);     const long long c2 = 2046;     const long long c3 = 6;     long long l, r;     int used = 5;     --used;     if (ask(vector<long long>{c1}) > 0) {       l = c1 + 1;       r = M;       goto shit;     }     --used;     if (ask(vector<long long>{c2}) > 0) {       l = c2 + 1;       r = c1 - 1;       goto shit;     }     --used;     if (ask(vector<long long>{c3}) > 0) {       l = c3 + 1;       r = c2 - 1;     } else {       l = 1;       r = c3 - 1;     }   shit:     for (int cs = used - 1; cs >= 0; --cs) {       vector<long long> s;       for (long long i = l; i + g(i, cs) <= r && s.size() < min(10000ll, l);            i += g(i, cs) + 1)         s.push_back(i + g(i, cs));       int v = ask(s);       if (!v) {         smin(r, s[0] - 1);       } else if (v == s.size()) {         smax(l, s.back() + 1);       } else {         smin(r, s[v] - 1);         smax(l, s[v - 1] + 1);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T inverse(T a, T m) {     T u = 0, v = 1;     while (a != 0) {       T t = m / a;       m -= t * a;       swap(a, m);       u -= t * v;       swap(u, v);     }     assert(m == 1);     return u;   }   template <typename T>   class Modular {    public:     using Type = typename decay<decltype(T::value)>::type;     constexpr Modular() : value() {}     template <typename U>     Modular(const U& x) {       value = normalize(x);     }     template <typename U>     static Type normalize(const U& x) {       Type v;       if (-mod() <= x && x < mod())         v = static_cast<Type>(x);       else         v = static_cast<Type>(x % mod());       if (v < 0) v += mod();       return v;     }     const Type& operator()() const { return value; }     template <typename U>     explicit operator U() const {       return static_cast<U>(value);     }     constexpr static Type mod() { return T::value; }     Modular& operator+=(const Modular& other) {       if ((value += other.value) >= mod()) value -= mod();       return *this;     }     Modular& operator-=(const Modular& other) {       if ((value -= other.value) < 0) value += mod();       return *this;     }     template <typename U>     Modular& operator+=(const U& other) {       return *this += Modular(other);     }     template <typename U>     Modular& operator-=(const U& other) {       return *this -= Modular(other);     }     Modular& operator++() { return *this += 1; }     Modular& operator--() { return *this -= 1; }     Modular operator++(int) {       Modular result(*this);       *this += 1;       return result;     }     Modular operator--(int) {       Modular result(*this);       *this -= 1;       return result;     }     Modular operator-() const { return Modular(-value); }     template <typename U = T>     typename enable_if<is_same<typename Modular<U>::Type, int>::value,                        Modular>::type&     operator*=(const Modular& rhs) {       value = normalize(static_cast<int64_t>(value) *                         static_cast<int64_t>(rhs.value));       return *this;     }     template <typename U = T>     typename enable_if<is_same<typename Modular<U>::Type, long long>::value,                        Modular>::type&     operator*=(const Modular& rhs) {       long long q = static_cast<long long>(static_cast<long double>(value) *                                            rhs.value / mod());       value = normalize(value * rhs.value - q * mod());       return *this;     }     template <typename U = T>     typename enable_if<!is_integral<typename Modular<U>::Type>::value,                        Modular>::type&     operator*=(const Modular& rhs) {       value = normalize(value * rhs.value);       return *this;     }     Modular& operator/=(const Modular& other) {       return *this *= Modular(inverse(other.value, mod()));     }     friend const Type& abs(const Modular& x) { return x.value; }     template <typename U>     friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);     template <typename U>     friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);     template <typename V, typename U>     friend V& operator>>(V& stream, Modular<U>& number);       private:     Type value;   };   template <typename T>   bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) {     return lhs.value == rhs.value;   }   template <typename T, typename U>   bool operator==(const Modular<T>& lhs, U rhs) {     return lhs == Modular<T>(rhs);   }   template <typename T, typename U>   bool operator==(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) == rhs;   }   template <typename T>   bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) {     return !(lhs == rhs);   }   template <typename T, typename U>   bool operator!=(const Modular<T>& lhs, U rhs) {     return !(lhs == rhs);   }   template <typename T, typename U>   bool operator!=(U lhs, const Modular<T>& rhs) {     return !(lhs == rhs);   }   template <typename T>   bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) {     return lhs.value < rhs.value;   }   template <typename T>   Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) += rhs;   }   template <typename T, typename U>   Modular<T> operator+(const Modular<T>& lhs, U rhs) {     return Modular<T>(lhs) += rhs;   }   template <typename T, typename U>   Modular<T> operator+(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) += rhs;   }   template <typename T>   Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) -= rhs;   }   template <typename T, typename U>   Modular<T> operator-(const Modular<T>& lhs, U rhs) {     return Modular<T>(lhs) -= rhs;   }   template <typename T, typename U>   Modular<T> operator-(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) -= rhs;   }   template <typename T>   Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) *= rhs;   }   template <typename T, typename U>   Modular<T> operator*(const Modular<T>& lhs, U rhs) {     return Modular<T>(lhs) *= rhs;   }   template <typename T, typename U>   Modular<T> operator*(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) *= rhs;   }   template <typename T>   Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) /= rhs;   }   template <typename T, typename U>   Modular<T> operator/(const Modular<T>& lhs, U rhs) {     return Modular<T>(lhs) /= rhs;   }   template <typename T, typename U>   Modular<T> operator/(U lhs, const Modular<T>& rhs) {     return Modular<T>(lhs) /= rhs;   }   template <typename T, typename U>   Modular<T> power(const Modular<T>& a, const U& b) {     assert(b >= 0);     Modular<T> x = a, res = 1;     U p = b;     while (p > 0) {       if (p & 1) res *= x;       x *= x;       p >>= 1;     }     return res;   }   template <typename T>   bool IsZero(const Modular<T>& number) {     return number() == 0;   }   template <typename T>   string to_string(const Modular<T>& number) {     return to_string(number());   }   template <typename U, typename T>   U& operator<<(U& stream, const Modular<T>& number) {     return stream << number();   }   template <typename U, typename T>   U& operator>>(U& stream, Modular<T>& number) {     typename common_type<typename Modular<T>::Type, long long>::type x;     stream >> x;     number.value = Modular<T>::normalize(x);     return stream;   }   constexpr int md = 998244353;   using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;   vector<Mint> fact(1, 1);   vector<Mint> inv_fact(1, 1);   Mint C(int n, int k) {     if (k < 0 || k > n) {       return 0;     }     while ((int)fact.size() < n + 1) {       fact.push_back(fact.back() * (int)fact.size());       inv_fact.push_back(1 / fact.back());     }     return fact[n] * inv_fact[k] * inv_fact[n - k];   }   template <typename T>   class NTT {    public:     using Type = typename decay<decltype(T::value)>::type;     static Type md;     static Modular<T> root;     static int base;     static int max_base;     static vector<Modular<T>> roots;     static vector<int> rev;     static void clear() {       root = 0;       base = 0;       max_base = 0;       roots.clear();       rev.clear();     }     static void init() {       md = T::value;       assert(md >= 3 && md % 2 == 1);       auto tmp = md - 1;       max_base = 0;       while (tmp % 2 == 0) {         tmp /= 2;         max_base++;       }       root = 2;       while (power(root, (md - 1) >> 1) == 1) {         root++;       }       assert(power(root, md - 1) == 1);       root = power(root, (md - 1) >> max_base);       base = 1;       rev = {0, 1};       roots = {0, 1};     }     static void ensure_base(int nbase) {       if (md != T::value) {         clear();       }       if (roots.empty()) {         init();       }       if (nbase <= base) {         return;       }       assert(nbase <= max_base);       rev.resize(1 << nbase);       for (int i = 0; i < (1 << nbase); i++) {         rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));       }       roots.resize(1 << nbase);       while (base < nbase) {         Modular<T> z = power(root, 1 << (max_base - 1 - base));         for (int i = 1 << (base - 1); i < (1 << base); i++) {           roots[i << 1] = roots[i];           roots[(i << 1) + 1] = roots[i] * z;         }         base++;       }     }     static void fft(vector<Modular<T>>& a) {       int n = (int)a.size();       assert((n & (n - 1)) == 0);       int zeros = __builtin_ctz(n);       ensure_base(zeros);       int shift = base - zeros;       for (int i = 0; i < n; i++) {         if (i < (rev[i] >> shift)) {           swap(a[i], a[rev[i] >> shift]);         }       }       for (int k = 1; k < n; k <<= 1) {         for (int i = 0; i < n; i += 2 * k) {           for (int j = 0; j < k; j++) {             Modular<T> x = a[i + j];             Modular<T> y = a[i + j + k] * roots[j + k];             a[i + j] = x + y;             a[i + j + k] = x - y;           }         }       }     }     static vector<Modular<T>> multiply(vector<Modular<T>> a,                                        vector<Modular<T>> b) {       if (a.empty() || b.empty()) {         return {};       }       int eq = (a == b);       int need = (int)a.size() + (int)b.size() - 1;       int nbase = 0;       while ((1 << nbase) < need) nbase++;       ensure_base(nbase);       int sz = 1 << nbase;       a.resize(sz);       b.resize(sz);       fft(a);       if (eq)         b = a;       else         fft(b);       Modular<T> inv_sz = 1 / static_cast<Modular<T>>(sz);       for (int i = 0; i < sz; i++) {         a[i] *= b[i] * inv_sz;       }       reverse(a.begin() + 1, a.end());       fft(a);       a.resize(need);       return a;     }   };   template <typename T>   typename NTT<T>::Type NTT<T>::md;   template <typename T>   Modular<T> NTT<T>::root;   template <typename T>   int NTT<T>::base;   template <typename T>   int NTT<T>::max_base;   template <typename T>   vector<Modular<T>> NTT<T>::roots;   template <typename T>   vector<int> NTT<T>::rev;   template <typename T>   vector<Modular<T>> inverse(const vector<Modular<T>>& a) {     assert(!a.empty());     int n = (int)a.size();     vector<Modular<T>> b = {1 / a[0]};     while ((int)b.size() < n) {       vector<Modular<T>> x(a.begin(), a.begin() + min(a.size(), b.size() << 1));       x.resize(b.size() << 1);       b.resize(b.size() << 1);       vector<Modular<T>> c = b;       NTT<T>::fft(c);       NTT<T>::fft(x);       Modular<T> inv = 1 / static_cast<Modular<T>>((int)x.size());       for (int i = 0; i < (int)x.size(); i++) {         x[i] *= c[i] * inv;       }       reverse(x.begin() + 1, x.end());       NTT<T>::fft(x);       rotate(x.begin(), x.begin() + (x.size() >> 1), x.end());       fill(x.begin() + (x.size() >> 1), x.end(), 0);       NTT<T>::fft(x);       for (int i = 0; i < (int)x.size(); i++) {         x[i] *= c[i] * inv;       }       reverse(x.begin() + 1, x.end());       NTT<T>::fft(x);       for (int i = 0; i < ((int)x.size() >> 1); i++) {         b[i + ((int)x.size() >> 1)] = -x[i];       }     }     b.resize(n);     return b;   }   template <typename T>   vector<Modular<T>> inverse_old(vector<Modular<T>> a) {     assert(!a.empty());     int n = (int)a.size();     if (n == 1) {       return {1 / a[0]};     }     int m = (n + 1) >> 1;     vector<Modular<T>> b = inverse(vector<Modular<T>>(a.begin(), a.begin() + m));     int need = n << 1;     int nbase = 0;     while ((1 << nbase) < need) {       ++nbase;     }     NTT<T>::ensure_base(nbase);     int size = 1 << nbase;     a.resize(size);     b.resize(size);     NTT<T>::fft(a);     NTT<T>::fft(b);     Modular<T> inv = 1 / static_cast<Modular<T>>(size);     for (int i = 0; i < size; ++i) {       a[i] = (2 - a[i] * b[i]) * b[i] * inv;     }     reverse(a.begin() + 1, a.end());     NTT<T>::fft(a);     a.resize(n);     return a;   }   template <typename T>   vector<Modular<T>> operator*(const vector<Modular<T>>& a,                                const vector<Modular<T>>& b) {     if (a.empty() || b.empty()) {       return {};     }     if (min(a.size(), b.size()) < 150) {       vector<Modular<T>> c(a.size() + b.size() - 1, 0);       for (int i = 0; i < (int)a.size(); i++) {         for (int j = 0; j < (int)b.size(); j++) {           c[i + j] += a[i] * b[j];         }       }       return c;     }     return NTT<T>::multiply(a, b);   }   template <typename T>   vector<Modular<T>>& operator*=(vector<Modular<T>>& a,                                  const vector<Modular<T>>& b) {     return a = a * b;   }   class graph {    public:     int n;     vector<vector<int>> adj;     vector<int> degree;     vector<pair<int, int>> edge;     vector<int> depth;     vector<bool> covered;     graph(int N)         : n(N),           adj(vector<vector<int>>(n)),           degree(vector<int>(n)),           depth(vector<int>(n)),           covered(vector<bool>(n, false)) {}     void add(int X, int Y) {       degree[X]++, degree[Y]++;       edge.emplace_back(X, Y);       adj[X].push_back(Y);       adj[Y].push_back(X);     }     void dfs(int N) {       if (covered[N] == true) return;       covered[N] = true;       for (int i : adj[N]) {         if (covered[i] == false) {           depth[i] = depth[N] + 1;           dfs(i);         }       }     }   };   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     string s, t;     cin >> s >> t;     int n = s.size(), m = t.size();     while (t.size() < s.size()) t +=  z ;     assert(t.size() == s.size());     vector<graph> gr(n - m + 1, graph(6));     for (int i = 0; i < 6; i++) {       for (int j = 0; j < 6; j++) {         vector<Mint> a(n), b(n);         for (int k = 0; k < n; k++) {           if (int(s[k] -  a ) == i) a[k] = 1;           if (int(t[k] -  a ) == j) b[k] = 1;         }         reverse(a.begin(), a.end());         for (int k = 0; k < n; k++) a.push_back(0), b.push_back(b[k]);         a *= b;         for (int k = 2 * n - 1, cnt = 0; cnt < n - m + 1; k--, cnt++) {           if (a[k]()) {             gr[cnt].add(i, j);           }         }       }     }     for (int i = 0; i < n - m + 1; i++) {       int ans = 0;       for (int j = 0; j < 6; j++) {         if (gr[i].covered[j] == false) {           gr[i].dfs(j);           ans += 1;         }       }       cout << 6 - ans <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long f[60][110][2];   long long bin[60], num[60], s, tt;   inline long long rd() {     long long x = 0;     char ch = getchar();     for (; ch <  0  || ch >  9 ; ch = getchar())       ;     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ;     return x;   }   int main() {     s = rd();     bin[0] = 1;     for (int i = 1; i <= 51; i++) bin[i] = bin[i - 1] * 2;     long long ans = 0;     for (int h = 1; bin[h] - 1 <= s; h++) {       long long hh = s % (bin[h] - 1);       for (int j = h - 1; j; j--)         if (bin[j] - 1 <= hh) hh -= bin[j] - 1;       ans += (!hh);     }     for (int h0 = 1; h0 <= 50; h0++)       for (int h1 = 1; h1 <= 50; h1++)         if (bin[h0 + 1] + bin[h1 + 1] - 3 <= s - bin[h1] + 1) {           long long hh = (s - bin[h1] + 1) % (bin[h0 + 1] + bin[h1 + 1] - 3);           for (int n = 0; n <= h0 + h1 - 2 && n <= hh; n++) {             tt = 0;             long long now = hh + n;             if (now & 1) continue;             memset(num, 0, sizeof(num));             for (now >>= 1; now; now >>= 1) num[++tt] = now & 1;             memset(f, 0, sizeof(f));             f[0][0][0] = 1;             for (int i = 1; i <= tt; i++)               for (int j = 0; j <= n; j++)                 for (int k = 0; k < 2; k++)                   if (f[i - 1][j][k]) {                     for (int l1 = 0; l1 < 2; l1++)                       for (int l2 = 0; l2 < 2; l2++) {                         if ((i >= h0 && l1) || (i >= h1 && l2)) continue;                         int hh = l1 + l2 + k, now = j + l1 + l2;                         if ((hh & 1) == num[i])                           f[i][now][hh >> 1] += f[i - 1][j][k];                       }                   }             ans += f[tt][n][0];           }         }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int shortestPaths[100][100];   int graph[100][100];   void bfs(int start, int n) {     queue<int> q;     q.push(start);     int visited[n];     for (int i = 0; i < n; i++) {       visited[i] = 0;     }     visited[start] = 1;     int dist = 1;     while (true) {       int sz = q.size();       if (sz == 0) {         break;       }       for (int i = 0; i < sz; i++) {         int cur = q.front();         q.pop();         for (int j = 0; j < 100; j++) {           if (graph[cur][j] == 1 && visited[j] == 0) {             visited[j] = 1;             shortestPaths[start][j] = dist;             q.push(j);           }         }       }       dist++;     }   }   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 0; i < 100; i++) {       for (int j = 0; j < 100; j++) {         shortestPaths[i][j] = 0;         graph[i][j] = 0;       }     }     for (int i = 0; i < n; i++) {       string s;       cin >> s;       for (int j = 0; j < n; j++) {         graph[i][j] = (int)(s[j] -  0 );       }     }     int m;     cin >> m;     vector<int> vertices;     for (int i = 0; i < m; i++) {       int a;       cin >> a;       a--;       vertices.push_back(a);     }     for (int i = 0; i < n; i++) {       bfs(i, n);     }     vector<int> result;     result.push_back(vertices[0]);     int last = vertices[0];     int ind = 0;     for (int i = 1; i < m; i++) {       if (i != 1) {         if (shortestPaths[last][vertices[i]] < i - ind) {           result.push_back(vertices[i - 1]);           ind = i - 1;           last = vertices[i - 1];         }       }     }     result.push_back(vertices[vertices.size() - 1]);     cout << result.size() << endl;     for (int i = 0; i < result.size(); i++) {       cout << result[i] + 1 <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 111111;   long long tree[maxn * 4];   void Insert(int o, int l, int r, int k, int v) {     if (l == r) {       tree[o] = v;       return;     }     int mid = (l + r) >> 1;     if (k <= mid)       Insert(o * 2, l, mid, k, v);     else       Insert(o * 2 + 1, mid + 1, r, k, v);     tree[o] = tree[o * 2] + tree[o * 2 + 1];   }   long long Query(int o, int l, int r, int L, int R) {     if (L <= l && r <= R) return tree[o];     int mid = (l + r) >> 1;     long double ans = 0;     if (L <= mid) ans += Query(o * 2, l, mid, L, R);     if (R > mid) ans += Query(o * 2 + 1, mid + 1, r, L, R);     return ans;   }   int n, x, a[maxn], f[maxn];   int main() {     cin >> n;     long double ans = 0;     for (int i = 1; i <= n; i++) cin >> a[i], f[a[i]] = i;     for (int i = 1; i <= n; i++) {       ans += Query(1, 1, n, a[i], n);       Insert(1, 1, n, a[i], 1);     }     ans *= ((long long)n * (n + 1));     memset(tree, 0, sizeof(tree));     long long last = 0;     for (int i = 1; i <= n; i++) {       ans -= (last + Query(1, 1, n, a[i], n));       last = last + Query(1, 1, n, a[i], n);       Insert(1, 1, n, a[i], f[a[i]] * 2);     }     for (int i = 1; i <= n; i++)       ans += ((long long)(n - i + 1) * i * (i - 1) / 2);     ans /= ((long long)n * (n + 1));     cout << setprecision(15) << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int a[200001][22];   int p[200001];   int d[200001];   int lca(int u, int v) {     if (d[u] < d[v]) swap(u, v);     int k = d[u] - d[v];     while (k > 0) {       int t = log2(k);       k -= (1 << t);       u = a[u][t];     }     if (u == v) return v;     for (int i = 20; i >= 0; i--) {       if (a[u][i] != a[v][i]) u = a[u][i], v = a[v][i];     }     return p[v];   }   int orobitiasvla(int x, int v) {     while (x > 0) {       int t = log2(x);       v = a[v][t];       x -= (1 << t);     }     return v;   }   int main() {     int n;     cin >> n;     int cnt = 2;     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= 20; j++) a[i][j] = -1;     }     d[1] = 0;     int d1 = 1, d2 = 1;     int mx = 0;     n--;     while (n--) {       int k;       cin >> k;       a[cnt][0] = k;       p[cnt] = k;       d[cnt] = d[k] + 1;       for (int i = 1; i <= 20; i++)         if (d[cnt] >= (1 << i)) a[cnt][i] = orobitiasvla((1 << i) - 1, k);       int y = lca(cnt, d1);       int cost = d[cnt] - d[y] + d[d1] - d[y];       y = lca(cnt, d2);       int cost1 = d[cnt] - d[y] + d[d2] - d[y];       if (cost1 > mx || cost > mx) {         if (cost > cost1)           d2 = cnt;         else           d1 = cnt;         mx = max(cost, cost1);       }       cout << mx <<    ;       cnt++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 6e5 + 7;   int a, b, n, ans;   int main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     cin >> a >> b, n = a + b;     for (int l = 1, r, s; l <= n; l = r + 1) {       s = n / l, r = n / s;       int al = (a + s) / (s + 1), ar = a / s, bl = (b + s) / (s + 1), br = b / s;       if (al <= ar && bl <= br) ans += min(ar + br, r) - max(al + bl, l) + 1;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, a, b, c, d, s, len;   vector<long long> z, t, v[2];   map<long long, int> ma;   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> a >> b >> c >> d >> s >> len;     t.assign(n + 1, -1);     v[0].assign(n + 1, s);     v[1].assign(n + 1, s);     for (int i = 0; i < n; ++i) {       int q;       cin >> t[i + 1] >> q;       v[0][i + 1] = v[0][i] + (q ? a : -b);       v[1][i + 1] = v[1][i] + (q ? c : -d);     }     int lim = 0;     ++ma[v[1][0]];     for (int i = 0; i < n + 1; ++i) {       if (v[0][i] < 0) break;       if (!(--ma[v[1][i]])) ma.erase(v[1][i]);       int be = t[i] + 1;       while (lim < n + 1 && t[lim] < be + len) {         ++ma[v[1][lim]];         ++lim;       }       if (i + 1 == lim || v[0][i] - v[1][i] + ma.begin()->first >= 0) {         cout << be << endl;         return 0;       }     }     cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int A[100], L[100], n, m;     cin >> n >> m;     for (int i = 0; i < n; ++i) cin >> A[i], A[i]--;     for (int i = 0; i < m; ++i) cin >> L[i];     for (int i = 0; i < n; ++i) {       for (int j = i + 1; j <= n; ++j) {         vector<int> cnt(m + 1, 0);         for (int k = i; k < j; ++k) cnt[A[k]]++;         bool fl = true;         for (int k = 0; k < m; ++k) fl &= (L[k] == cnt[k]);         if (fl) {           cout <<  YES n ;           return 0;         }       }     }     cout <<  NO n ;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, b[1000010];   int main() {     scanf( %d%d , &n, &k);     int i;     if (k * 3 > n || k == 1)       puts( -1 );     else {       for (i = 1; i <= (int)(k); ++i) b[i] = b[i + 2 * k] = i, b[i + k] = i - 1;       b[k + 1] = k;       for (i = 3 * k + 1; i <= n; ++i) b[i] = 1;       for (i = 1; i <= (int)(n); ++i) printf( %d  , b[i]);       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[9][5], b[8];   long long dis(int i, int j) {     long long x = a[i][0] - a[j][0], y = a[i][1] - a[j][1], z = a[i][2] - a[j][2];     return x * x + y * y + z * z;   }   bool check() {     for (int i = 0; i < 8; i++) {       for (int j = 0; j < 8; j++) {         if (i == j) continue;         if (j < i)           b[j] = dis(i, j);         else           b[j - 1] = dis(i, j);       }       sort(b, b + 7);       long long t1 = b[0], t2 = 2 * b[0], t3 = 3 * b[0];       if (t1 == 0 || t1 != b[1] || t1 != b[2] || t2 != b[3] || t2 != b[4] ||           t2 != b[5] || t3 != b[6])         return false;     }     return true;   }   bool is_ok(int cur) {     if (cur == 8) {       return check();     }     sort(a[cur], a[cur] + 3);     do {       if (is_ok(cur + 1)) return true;     } while (next_permutation(a[cur], a[cur] + 3));     return false;   }   int main() {     for (int i = 0; i < 8; i++) cin >> a[i][0] >> a[i][1] >> a[i][2];     if (!is_ok(0))       puts( NO );     else {       puts( YES );       for (int i = 0; i < 8; i++)         for (int j = 0; j < 3; j++) {           cout << a[i][j] <<    ;           if (j == 2) cout << endl;         }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int constexpr nax = 3e5 + 5;   using ll = int64_t;   const int INF = 1e9;   template <typename T>   struct fenwick {     T bit[nax];     fenwick() { memset(bit, 0, sizeof(bit)); }     void update(int x, T val) {       for (; x < nax; x += x & -x) bit[x] += val;     }     T get(int x) {       int res = 0;       for (; x > 0; x -= x & -x) res += bit[x];       return res;     }     T get(int l, int r) { return get(r) - get(l - 1); }   };   struct segtree {     static constexpr int Mx = 4 * nax;     ll sg[Mx] = {}, tag[Mx] = {};     inline void pull(int v) { sg[v] = min(sg[v * 2], sg[v * 2 + 1]); }     segtree() {       fill(sg, sg + Mx, INF);       fill(tag, tag + Mx, 0);     }     inline void push(int v) {       if (v * 2 + 1 < Mx && tag[v]) {         sg[v * 2] += tag[v];         sg[v * 2 + 1] += tag[v];         tag[v * 2] += tag[v];         tag[v * 2 + 1] += tag[v];       }       tag[v] = 0;     }     inline void update(int v, int x, int y, int l, int r, ll val) {       push(v);       if (l > r) return;       if (l == x && r == y) {         sg[v] += val;         tag[v] += val;         return push(v);       }       int mid = x + y >> 1;       update(v * 2, x, mid, l, min(r, mid), val);       update(v * 2 + 1, mid + 1, y, max(l, mid + 1), r, val);       pull(v);     }     int find_first_neg(int v, int x, int y) {       push(v);       if (x == y) {         assert(sg[v] <= 0);         return x;       } else {         int mid = x + y >> 1;         if (sg[v * 2] <= 0)           return find_first_neg(v * 2, x, mid);         else           return find_first_neg(v * 2 + 1, mid + 1, y);       }     }   };   segtree sg;   fenwick<int> sum;   int n, nq;   int ans[nax];   int b[nax];   vector<pair<int, int>> q[nax];   int main() {     cin.tie(0)->sync_with_stdio(false);     cin >> n >> nq;     for (int i = 1; i <= n; ++i) {       cin >> b[i];       b[i] = i < b[i] ? INF : i - b[i];     }     for (int i = 1; i <= nq; ++i) {       int x, y;       cin >> x >> y;       q[x + 1].emplace_back(n - y, i);     }     for (int i = n; i >= 1; --i) {       sg.update(1, 1, n, i, i, (ll)b[i] - INF);       while (sg.sg[1] <= 0) {         int v = sg.find_first_neg(1, 1, n);         sum.update(v, 1);         sg.update(1, 1, n, v, v, INF);         sg.update(1, 1, n, v, n, -1);       }       for (const auto &[r, id] : q[i]) ans[id] = sum.get(i, r);     }     for (int i = 1; i <= nq; ++i) {       cout << ans[i] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 239;   int n, m, k;   vector<set<int>> row;   vector<set<int>> column;   vector<set<int>> row_free;   vector<set<int>> column_free;   int get_row_left(int i, int to, int from) {     auto it = row[i].lower_bound(from);     if (it == row[i].begin()) {       return to;     }     it--;     if (to <= *(it) && *(it) <= from) {       return *(it) + 1;     } else {       return to;     }   }   int get_row_right(int i, int to, int from) {     auto it = row[i].lower_bound(from);     if (it == row[i].end()) {       return to;     }     if ((*it) <= to) {       return (*it) - 1;     } else {       return to;     }   }   int get_column_up(int j, int to, int from) {     auto it = column[j].lower_bound(from);     if (it == column[j].begin()) {       return to;     }     it--;     if (to <= *(it) && *(it) <= from) {       return *(it) + 1;     } else {       return to;     }   }   int get_column_down(int j, int to, int from) {     auto it = column[j].lower_bound(from);     if (it == column[j].end()) {       return to;     }     if ((*it) <= to) {       return (*it) - 1;     } else {       return to;     }   }   vector<pair<int, int>> all_next(int i, int j) {     int ff = get_row_left(i, max(0, j - k), j);     int ss = get_row_right(i, min(m - 1, j + k), j);     int tt = get_column_up(j, max(0, i - k), i);     int th = get_column_down(j, min(n - 1, i + k), i);     vector<pair<int, int>> ans;     if (j > 0) {       auto it = row_free[i].lower_bound(ff);       while (true) {         if (it == row_free[i].end()) {           break;         }         if (*(it) >= j) {           break;         }         ans.push_back({i, *(it)});         it++;       }     }     if (j + 1 < m) {       auto it = row_free[i].upper_bound(j);       while (true) {         if (it == row_free[i].end()) {           break;         }         if (*(it) > ss) {           break;         }         ans.push_back({i, *(it)});         it++;       }     }     if (i > 0) {       auto it = column_free[j].lower_bound(tt);       while (true) {         if (it == column_free[j].end()) {           break;         }         if (*(it) >= i) {           break;         }         ans.push_back({*(it), j});         it++;       }     }     if (i + 1 < n) {       auto it = column_free[j].upper_bound(i);       while (true) {         if (it == column_free[j].end()) {           break;         }         if (*(it) > th) {           break;         }         ans.push_back({*(it), j});         it++;       }     }     return ans;   }   void bfs(int si, int sj, int ei, int ej) {     vector<vector<int>> d(n, vector<int>(m, inf));     d[si][sj] = 0;     queue<pair<int, int>> q;     q.push({si, sj});     row_free[si].erase(sj);     column_free[sj].erase(si);     while (!q.empty()) {       auto tmp = q.front();       q.pop();       int ci = tmp.first;       int cj = tmp.second;       auto next = all_next(ci, cj);       for (auto [ni, nj] : next) {         if (d[ni][nj] > d[ci][cj] + 1) {           d[ni][nj] = d[ci][cj] + 1;           row_free[ni].erase(nj);           column_free[nj].erase(ni);           q.push({ni, nj});         }       }     }     int res = d[ei][ej];     if (res == inf) {       cout << -1;     } else {       cout << res;     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m >> k;     row.resize(n);     column.resize(m);     row_free.resize(n);     column_free.resize(m);     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         char c;         cin >> c;         if (c ==  # ) {           row[i].insert(j);           column[j].insert(i);         } else {           row_free[i].insert(j);           column_free[j].insert(i);         }       }     }     int si, sj, ei, ej;     cin >> si >> sj >> ei >> ej;     si--;     sj--;     ei--;     ej--;     bfs(si, sj, ei, ej);   }


#include <bits/stdc++.h>   using namespace std;   string s, t;   int dp[5005][5005];   int get(int i, int j) {     if (dp[i][j] != -1) return dp[i][j];     if (i >= s.size() or j >= t.size()) return 0;     int ret = get(i, j + 1);     if (s[i] == t[j]) ret = (ret + 1 + get(i + 1, j + 1)) % 1000000007;     return dp[i][j] = ret;     ;   }   int main() {     for (int i = 0; i < 5005; ++i)       for (int j = 0; j < 5005; ++j) dp[i][j] = -1;     cin >> s >> t;     long long ret = 0;     for (int i = 0; i < s.size(); ++i) ret = (ret + get(i, 0)) % 1000000007;     cout << ret << endl;   }