#include <bits/stdc++.h>   using namespace std;   const long double PI = acos(-1);   const int _ML = 500;   const char _inpf[] =   ;   const char _outf[] =   ;   template <typename T, typename U>   inline ostream &operator<<(ostream &_out, const pair<T, U> &_p) {     _out << _p.x <<     << _p.y;     return _out;   }   template <typename T, typename U>   inline istream &operator>>(istream &_in, pair<T, U> &_p) {     _in >> _p.x >> _p.y;     return _in;   }   template <typename T>   inline ostream &operator<<(ostream &_out, const vector<T> &_v) {     if (_v.empty()) return _out;     _out << _v.front();     for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) _out <<     << *_it;     return _out;   }   template <typename T>   inline istream &operator>>(istream &_in, vector<T> &_v) {     for (auto &_i : _v) _in >> _i;     return _in;   }   template <typename T>   inline ostream &operator<<(ostream &_out, const set<T> &_s) {     if (_s.empty()) return _out;     _out << *_s.begin();     for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out <<     << *_it;     return _out;   }   template <typename T>   inline ostream &operator<<(ostream &_out, const multiset<T> &_s) {     if (_s.empty()) return _out;     _out << *_s.begin();     for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out <<     << *_it;     return _out;   }   template <typename T>   inline ostream &operator<<(ostream &_out, const unordered_set<T> &_s) {     if (_s.empty()) return _out;     _out << *_s.begin();     for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out <<     << *_it;     return _out;   }   template <typename T>   inline ostream &operator<<(ostream &_out, const unordered_multiset<T> &_s) {     if (_s.empty()) return _out;     _out << *_s.begin();     for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out <<     << *_it;     return _out;   }   template <typename T, typename U>   inline ostream &operator<<(ostream &_out, const map<T, U> &_m) {     if (_m.empty()) return _out;     _out <<  (  << _m.begin()->x <<  :   << _m.begin()->y <<  ) ;     for (auto _it = ++_m.begin(); _it != _m.end(); ++_it)       _out <<  , (  << _it->x <<  :   << _it->y <<  ) ;     return _out;   }   template <typename T, typename U>   inline ostream &operator<<(ostream &_out, const unordered_map<T, U> &_m) {     if (_m.empty()) return _out;     _out <<  (  << _m.begin()->x <<  :   << _m.begin()->y <<  ) ;     for (auto _it = ++_m.begin(); _it != _m.end(); ++_it)       _out <<  , (  << _it->x <<  :   << _it->y <<  ) ;     return _out;   }   inline void mainFunction(void) {     int n, m, k;     cin >> n >> m >> k;     vector<string> v(n + 2, string(m + 2,  * ));     int x = 228,         y = 1488 +  m  +  i  +  s  +  h  +  k  +  a  +  =  +  g  +  a  +  y ;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         cin >> v[i][j];         if (v[i][j] ==  X ) {           v[i][j] =  . ;           x = i;           y = j;         }       }     }     const int dx[] = {1, 0, 0, -1};     const int dy[] = {0, -1, 1, 0};     const int N = 1000 * 1000 + 228;     vector<vector<int>> d(n + 2, vector<int>(m + 2, N));     static int qx[N];     static int qy[N];     int ql = 0;     int qr = 1;     qx[0] = x;     qy[0] = y;     d[x][y] = 0;     while (ql != qr) {       int xx = qx[ql];       int yy = qy[ql];       ++ql;       for (int j = 0; j < 4; ++j) {         if (v[xx + dx[j]][yy + dy[j]] ==  .  && d[xx + dx[j]][yy + dy[j]] == N) {           d[xx + dx[j]][yy + dy[j]] = d[xx][yy] + 1;           qx[qr] = xx + dx[j];           qy[qr] = yy + dy[j];           ++qr;         }       }     }     const char c[] = { D ,  L ,  R ,  U };     static char p[228];     p[ U ] =  D ;     p[ D ] =  U ;     p[ R ] =  L ;     p[ L ] =  R ;     string s;     for (int i = 0; i < k; ++i) {       int j;       for (j = 0; j < 4; ++j) {         if (v[x + dx[j]][y + dy[j]] ==  .  &&             d[x + dx[j]][y + dy[j]] <= k - i - 1) {           break;         }       }       if (j == 4) {         cout <<  IMPOSSIBLE  <<   n ;         return;       }       s += c[j];       x += dx[j];       y += dy[j];     }     cout << s <<   n ;   }   int main() {     if (_inpf[0] !=   0 ) assert(freopen(_inpf,  r , stdin) != nullptr);     if (_outf[0] !=   0 ) assert(freopen(_outf,  w , stdout) != nullptr);     cin.tie(nullptr);     ios_base::sync_with_stdio(false);     cout << setprecision(20);     mainFunction();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     if (n == 1) {       cout <<  1 n0 1 n0 n1 ;       return 0;     }     vector<long long> p1, p2;     p1.push_back(1);     p1.push_back(0);     p2.push_back(1);     for (long long i = 1; i < n; i++) {       vector<long long> temp = p1;       p1.push_back(0);       for (long long j = 0; j < p2.size(); j++) {         p1[j + 2] += p2[j];         p1[j + 2] %= 2LL;       }       p2 = temp;     }     reverse(p1.begin(), p1.end());     reverse(p2.begin(), p2.end());     cout << p1.size() - 1 <<   n ;     for (long long i = 0; i < p1.size(); i++) cout << p1[i] <<    ;     cout <<   n ;     cout << p2.size() - 1 <<   n ;     for (long long i = 0; i < p2.size(); i++) cout << p2[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long n, d, a[100000], diff;     long sum = 0;     cin >> n >> d;     for (long i = 0; i < n; i++) cin >> a[i];     for (long i = 1; i < n; i++) {       if (a[i - 1] >= a[i]) {         diff = a[i - 1] - a[i];         a[i] = a[i] + (diff / d + 1) * d;         sum = sum + diff / d + 1;       }     }     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 55, p = 1e9 + 7;   int n, m;   long long inv[Maxn], f[Maxn][Maxn], g[Maxn][Maxn], sum[Maxn][Maxn];   long long get_inv(int x) {     return x <= 1 ? 1 : (p - p / x) * get_inv(p % x) % p;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) inv[i] = get_inv(i);     f[0][1] = sum[0][1] = 1;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= i + 1; j++)         for (int k = 1; k <= i; k++)           (g[i][j] += sum[k - 1][j] * sum[i - k][j]) %= p;       for (int j = 1; j <= i + 1; j++) {         (g[i][j] += p - g[i][j + 1]) %= p;         for (int a = n; a >= 0; a--)           for (int b = n + 1; b >= 0; b--) {             long long now = 1;             for (int d = 1; i * d <= a && j * d <= b; d++) {               (now *= inv[d] * (g[i][j] + d - 1 + p) % p) %= p;               (f[a][b] += now * f[a - i * d][b - j * d]) %= p;             }           }       }       for (int j = i + 1; j >= 1; j--) sum[i][j] = (sum[i][j + 1] + f[i][j]) % p;     }     printf( %lld , f[n][m]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int m, n;   long long mas[300007];   long long ex[300007];   vector<long long> s;   int main() {     scanf( %d %d , &m, &n);     for (int i = 0; i < m; i++) {       scanf( %I64d , &mas[i]);     }     for (int i = 0; i < n; i++) {       scanf( %I64d , &ex[i]);     }     n--;     m--;     for (int i = 0; i < n; i++) {       ex[i] = ex[i + 1] - ex[i];       s.push_back(ex[i]);     }     s.push_back((long long)1000000000000);     for (int i = 0; i < m; i++) {       mas[i] = mas[i + 1] - mas[i];       s.push_back(mas[i]);     }     m = n + m + 1;     vector<int> z(m);     for (int i = 1, l = 0, r = 0; i < m; ++i) {       if (i <= r) z[i] = min(r - i + 1, z[i - l]);       while (i + z[i] < m && s[z[i]] == s[i + z[i]]) ++z[i];       if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;     }     int c = 0;     for (int i = n; i < m; i++)       if (z[i] >= n) c++;     cout << c;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 1500 + 10;   int n, x, y;   struct node {     int xx, yy, id;     bool operator<(const node &a) const {       if (xx - x >= 0 && a.xx - x <= 0) return 1;       if (xx - x <= 0 && a.xx - x >= 0) return 0;       return (yy - y) * (long long)(a.xx - x) < (a.yy - y) * (long long)(xx - x);     }   } p[M];   vector<int> g[M];   int sz[M];   int ans[M];   void dfs(int o, int u) {     sz[u] = 1;     for (int &v : g[u])       if (v != o) {         dfs(u, v);         sz[u] += sz[v];       }   }   void dfs2(int o, int u, int l, int r) {     if (sz[u] == 1) {       ans[p[l].id] = u;       return;     }     int id = l;     for (int i = l + 1; i < r; ++i) {       if (p[i].xx < p[id].xx) {         id = i;       }     }     swap(p[id], p[l]);     ans[p[l].id] = u;     x = p[l].xx, y = p[l].yy;     sort(p + l + 1, p + r);     int sum = l + 1;     for (int &v : g[u])       if (v != o) {         dfs2(u, v, sum, sum + sz[v]);         sum += sz[v];       }   }   int main() {     scanf( %d , &n);     for (int i = 1, u, v; i < n; ++i) {       scanf( %d%d , &u, &v);       g[u].emplace_back(v);       g[v].emplace_back(u);     }     for (int i = 0; i < n; ++i) {       scanf( %d%d , &p[i].xx, &p[i].yy);       p[i].id = i;     }     dfs(1, 1);     dfs2(1, 1, 0, n);     for (int i = 0; i < n; ++i) {       printf( %d%c , ans[i],    n [i == n - 1]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m;   pair<long long, long long> a[3005];   long long v[3005];   vector<long long> vec[3005];   priority_queue<long long, vector<long long>, greater<long long> > mc;   bool compare(pair<long long, long long> a, pair<long long, long long> b) {     return a > b;   }   int main() {     cin >> n >> m;     if (m == 1) {       cout << 0 << endl;       exit(0);     };     long long x, y;     for (long long i = 0; i < n; i++) {       scanf( %I64d%I64d , &x, &y);       x--;       v[x]++;       vec[x].push_back(y);       if (x) mc.push(y);     }     for (long long i = 0; i < m; i++) {       a[i].first = v[i];       a[i].second = i;       sort(vec[i].begin(), vec[i].end());     }     sort(a, a + m, compare);     if (a[0].second == 0 && a[0].first > a[1].first) {       cout << 0 << endl;       exit(0);     };     long long need = a[0].first - v[0] + 1;     long long tneed;     priority_queue<long long, vector<long long>, greater<long long> > tmc;     long long res = 541885418851154LL;     long long cost;     for (long long i = 0; i <= a[0].first; i++) {       cost = 0;       while (tmc.size()) {         tmc.pop();       }       tneed = need;       if (tneed <= 0) {         res = 0;         break;       }       long long debug = (i == 2);       for (long long j = 0; j < m; j++) {         long long tj = a[j].second;         for (long long k = max(v[tj] - i, 0LL); k < v[tj]; k++) {           if (tj != 0) tmc.push(vec[tj][k]);         }         if (v[tj] <= i) continue;         for (long long k = 0; k < v[tj] - i; k++) {           cost += vec[tj][k];           tneed--;           if (tneed == 0) break;         }         if (tneed == 0) break;       }       tneed -= a[0].first - i;       if (tneed <= 0) {         res = min(res, cost);         continue;       }       for (long long j = 0; j < tneed; j++) {         cost += tmc.top();         tmc.pop();       }       res = min(res, cost);     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int res[100005];   int main() {     long long int n;     scanf( %lld , &n);     if (n % 4 == 2 || n % 4 == 3) {       printf( -1 );       exit(0);     }     long long int c = 2;     for (long long int i = 0; i < n / 2; i += 2) {       res[i] = c;       res[i + 1] = n - i;       c += 2;     }     c = 1;     for (long long int i = n - 2; i >= n / 2; i -= 2) {       res[i] = c;       res[i + 1] = n - c;       c += 2;     }     if (n % 2) res[n / 2] = n / 2 + 1;     for (long long int i = 0; i < n; i++) printf( %lld  , res[i]);   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 7;   const int maxm = 1e5 + 7;   struct Que {     int l, r, rt, id;   };   struct Data {     int sum;     int id;     bool operator<(const Data& a) const {       return (sum == a.sum) ? (id < a.id) : (sum > a.sum);     }   };   struct Edge {     int to, last;   } E[maxm];   vector<Que> Q[maxm];   struct Node {     Data val;     int lch, rch;   } rne[maxm * 20];   int fail[maxm];   int ch[maxm][26];   int len[maxm];   int n, m;   Data ans[maxn];   char s[maxn];   char t[maxn];   int rt[maxm];   int head[maxm];   int f[20][maxm];   int pos[maxn];   int Len[maxn];   int LL[maxn];   int last;   int root;   int cnt;   int tot;   int qvq;   void Pushup(int u) {     if (!rne[u].lch && !rne[u].rch) return;     if (rne[u].lch && !rne[u].rch)       rne[u].val = rne[rne[u].lch].val;     else if (rne[u].rch && !rne[u].lch)       rne[u].val = rne[rne[u].rch].val;     else       rne[u].val = min(rne[rne[u].lch].val, rne[rne[u].rch].val);   }   void Modify(int& u, int l, int r, int x) {     if (!u) u = ++qvq;     if (l == r) {       rne[u].val.sum++;       rne[u].val.id = l;       return;     }     int mid = l + r >> 1;     if (x <= mid)       Modify(rne[u].lch, l, mid, x);     else       Modify(rne[u].rch, mid + 1, r, x);     Pushup(u);   }   void Extend(int x, int id) {     int p = last, np = ++tot;     last = np;     len[np] = len[p] + 1;     while (p && !ch[p][x]) ch[p][x] = np, p = fail[p];     if (!p)       fail[np] = root;     else {       int q = ch[p][x];       if (len[q] == len[p] + 1)         fail[np] = q;       else {         int nq = ++tot;         fail[nq] = fail[q];         len[nq] = len[p] + 1;         memcpy(ch[nq], ch[q], sizeof ch[q]);         fail[np] = fail[q] = nq;         while (p && ch[p][x] == q) ch[p][x] = nq, p = fail[p];       }     }     Modify(rt[np], 1, m, id);   }   int Merge(int x, int y, int l, int r) {     if (!x || !y) return x + y;     if (l == r) {       int re = ++qvq;       rne[re].val.sum = rne[x].val.sum + rne[y].val.sum;       rne[re].val.id = l;       return re;     }     int mid = l + r >> 1;     int re = ++qvq;     rne[re].lch = Merge(rne[x].lch, rne[y].lch, l, mid);     rne[re].rch = Merge(rne[x].rch, rne[y].rch, mid + 1, r);     Pushup(re);     return re;   }   Data Query(int u, int l, int r, int L, int R) {     if (l >= L && r <= R) return rne[u].val;     int mid = l + r >> 1;     Data re = (Data){-1, m + 1};     if (L <= mid) re = min(re, Query(rne[u].lch, l, mid, L, R));     if (R > mid) re = min(re, Query(rne[u].rch, mid + 1, r, L, R));     return re;   }   void Dfs(int x, int p) {     for (int i = head[x], y; y = E[i].to, i; i = E[i].last) {       Dfs(y, x);       rt[x] = Merge(rt[x], rt[y], 1, m);     }     for (Que cur : Q[x]) {       ans[cur.id] = Query(rt[x], 1, m, cur.l, cur.r);     }   }   void Adde(int x, int y) {     E[++cnt] = (Edge){y, head[x]};     head[x] = cnt;   }   int Read(char c = 0, int re = 0) {     while (!isdigit(c = getchar()))       ;     re = c -  0 ;     while (isdigit(c = getchar())) re = ((re << 2) + re << 1) + c -  0 ;     return re;   }   int main() {     scanf( %s , s + 1);     n = strlen(s + 1);     scanf( %d , &m);     last = tot = root = 1;     for (int i = 1; i <= m; ++i) {       scanf( %s , t + 1);       int len = strlen(t + 1);       last = 1;       for (int j = 1; j <= len; ++j) Extend(t[j] -  a , i);     }     for (int i = 1; i <= tot; ++i) f[0][i] = fail[i];     for (int i = 0; i < 19; ++i)       for (int x = 1; x <= tot; ++x) f[i + 1][x] = f[i][f[i][x]];     int x = 1, tmp = 0;     for (int i = 1; i <= n; ++i) {       int d = s[i] -  a ;       if (ch[x][d])         tmp++, x = ch[x][d];       else {         while (x && !ch[x][d]) x = fail[x];         if (x)           tmp = len[x] + 1, x = ch[x][d];         else           x = 1, tmp = 0;       }       pos[i] = x;       Len[i] = tmp;     }     for (int i = 1; i <= tot; ++i)       if (fail[i]) Adde(fail[i], i);     int q = Read();     for (int i = 1; i <= q; ++i) {       int l = Read(), r = Read(), pl = Read(), pr = Read();       LL[i] = l;       if (Len[pr] < pr - pl + 1) {         ans[i] = (Data){0, l};         continue;       }       int rt = pos[pr];       int LEN = pr - pl + 1;       for (int i = 19; ~i; --i) {         if (len[f[i][rt]] >= LEN) rt = f[i][rt];       }       Q[rt].push_back((Que){l, r, rt, i});     }     Dfs(1, 0);     for (int i = 1; i <= q; ++i) {       if (!ans[i].sum) ans[i].id = LL[i];       printf( %d %d n , ans[i].id, ans[i].sum);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5000;   int p[MAXN], size[MAXN], n, m;   int find_set(int v) { return v == p[v] ? v : p[v] = find_set(p[v]); }   void union_set(int x, int y) {     x = find_set(x);     y = find_set(y);     if (x != y) {       if (size[x] < size[y]) swap(x, y);       p[x] = y;       size[y] += size[x];     }   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) size[i] = 1, p[i] = i;     while (m--) {       int x, y;       cin >> x >> y;       union_set(x, y);     }     int mx = 0;     cin >> m;     while (m--) {       int x, y;       cin >> x >> y;       if (find_set(x) == find_set(y)) size[find_set(x)] = 0;     }     for (int i = 1; i <= n; i++) {       mx = max(mx, size[find_set(i)]);     }     cout << mx << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   const int MOD = 1000000009;   int parent[N];   int Rank[N];   int Find(int x) {     if (parent[x] != x) {       parent[x] = Find(parent[x]);     }     return parent[x];   }   bool Union(int x, int y) {     x = Find(x);     y = Find(y);     if (x == y) return false;     if (Rank[x] < Rank[y]) {       parent[x] = y;     } else if (Rank[x] > Rank[y]) {       parent[y] = x;     } else {       parent[x] = y;       Rank[y]++;     }     return true;   }   int main() {     int n, m, a, b;     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) {       parent[i] = i;     }     int ans = 1;     while (m--) {       scanf( %d %d , &a, &b);       if (!Union(a, b)) {         ans = (ans * 2) % MOD;       }       printf( %d n , ans ? ans - 1 : MOD - 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, a[100005], b[2005], d[100005], z[100005], L[100005];   inline void upd(int &a, int b) {     if (a < b) a = b;   }   int main(void) {     while (cin >> n >> m) {       for (int i = int(1); i <= int(n); i++) cin >> a[i];       for (int i = int(1); i <= int(m); i++) cin >> b[i];       sort(a + 1, a + n + 1);       sort(b + 1, b + m + 1);       memset(d, 0, sizeof d), memset(z, 0, sizeof z), memset(L, 0, sizeof L);       for (int i = int(1); i <= int(n); i++) {         if (i > 1 && a[i] - 1 == a[i - 1])           L[i] = L[i - 1];         else           L[i] = i;         int x = upper_bound(b + 1, b + m + 1, a[i]) - b;         upd(d[i], z[L[i] - 1]);         for (int j = int(x - 1); j >= int(1); j--) {           if (i <= a[i] - b[j]) break;           upd(d[i], z[L[i - a[i] + b[j]] - 1] + x - j);         }         upd(z[i], max(z[i - 1], d[i]));         for (int j = int(x); j <= int(m); j++) {           if (i + b[j] - a[i] > n) break;           upd(z[i + b[j] - a[i]], d[i] + j - x + 1);         }       }       cout << z[n] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, l, r[200005], cnt = 0;     bool valid[200005];     pair<int, int> p;     pair<int, pair<int, int> > u;     vector<int> v;     vector<pair<int, pair<int, int> > > event;     priority_queue<pair<int, int> > pq;     scanf( %d %d , &n, &k);     for (int i = 1; i <= n; i++) {       scanf( %d %d , &l, &r[i]);       event.push_back(make_pair(l, make_pair(0, i)));       event.push_back(make_pair(r[i] + 1, make_pair(1, i)));     }     sort(event.begin(), event.end());     memset(valid, true, sizeof valid);     for (int i = 0; i < (int)event.size(); i++) {       u = event[i];       l = u.first;       while (u.first == l) {         if (u.second.first == 0) {           pq.push(make_pair(r[u.second.second], u.second.second));           cnt++;         } else if (valid[u.second.second]) {           valid[u.second.second] = false;           cnt--;         }         i++;         if (i == (int)event.size()) break;         u = event[i];       }       i--;       while (cnt > k) {         p = pq.top();         pq.pop();         while (!valid[p.second]) {           p = pq.top();           pq.pop();         }         valid[p.second] = false;         v.push_back(p.second);         cnt--;       }     }     printf( %d n , (int)v.size());     for (int i = 0; i < (int)v.size(); i++) {       if (i) putchar(   );       printf( %d , v[i]);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200000;   struct Splay {     struct node {       node *fa, *ch[2];       int num, key, mx, mn, tg;     } pl[2 * MAXN + 5], *root, *NIL;     Splay() {       root = NIL = &pl[0];       NIL->ch[0] = NIL->ch[1] = NIL->fa = NIL;       NIL->mx = -MAXN, NIL->mn = MAXN, NIL->tg = 0;     }     void set_child(node *x, node *y, int d) {       if (x != NIL) x->ch[d] = y;       if (y != NIL) y->fa = x;     }     void pushdown(node *x) {       if (x->tg) {         if (x->ch[0] != NIL)           x->ch[0]->key += x->tg, x->ch[0]->mx += x->tg, x->ch[0]->mn += x->tg,               x->ch[0]->tg += x->tg;         if (x->ch[1] != NIL)           x->ch[1]->key += x->tg, x->ch[1]->mx += x->tg, x->ch[1]->mn += x->tg,               x->ch[1]->tg += x->tg;         x->tg = 0;       }     }     void pushup(node *x) {       x->mn = min(x->key, min(x->ch[0]->mn, x->ch[1]->mn));       x->mx = max(x->key, max(x->ch[0]->mx, x->ch[1]->mx));     }     void rotate(node *x) {       node *y = x->fa;       int d = (y->ch[1] == x);       pushdown(y), pushdown(x);       if (y->fa != NIL)         set_child(y->fa, x, y->fa->ch[1] == y);       else         x->fa = y->fa;       set_child(y, x->ch[!d], d);       set_child(x, y, !d);       if (y == root) root = x;       pushup(y);     }     void splay(node *x, node *rt) {       pushdown(x);       while (x->fa != rt) {         node *y = x->fa;         if (y->fa == rt)           rotate(x);         else {           if ((y->fa->ch[1] == y) == (y->ch[1] == x))             rotate(y);           else             rotate(x);           rotate(x);         }       }       pushup(x);     }     void debug(node *x) {       if (x == NIL) return;       pushdown(x);       debug(x->ch[0]);       printf( %d : %d %d %d | %d %d n , x - pl, x->ch[0] - pl, x->ch[1] - pl,              x->fa - pl, x->num, x->key);       debug(x->ch[1]);     }     int dist(node *x, node *y) {       if (x == y) return 0;       splay(x, NIL), splay(y, root);       if (x->ch[0] == y) {         int d = min(min(x->key, y->key), x->ch[0]->ch[1]->mn);         return x->key + y->key - 2 * d;       } else {         int d = min(min(x->key, y->key), x->ch[1]->ch[0]->mn);         return x->key + y->key - 2 * d;       }     }     node *query(node *x, int d) {       pushdown(x);       if (x->ch[1]->mn <= d && d <= x->ch[1]->mx)         return query(x->ch[1], d);       else if (x->key == d) {         splay(x, NIL);         return x;       } else         return query(x->ch[0], d);     }     node *prev(node *x, node *rt) {       splay(x, rt);       node *ret = x->ch[0];       while (ret->ch[1] != NIL) ret = ret->ch[1];       splay(ret, rt);       return ret;     }     node *next(node *x, node *rt) {       splay(x, rt);       node *ret = x->ch[1];       while (ret->ch[0] != NIL) ret = ret->ch[0];       splay(ret, rt);       return ret;     }   } T;   Splay::node *fir[MAXN + 5], *bac[MAXN + 5];   int n, m, root;   vector<int> G[MAXN + 5];   void addedge(int u, int v) { G[u].push_back(v); }   int dep[MAXN + 5], dfn[2 * MAXN + 5], dcnt;   void newnode(Splay::node *nw, int x, int k) {     nw->ch[0] = nw->ch[1] = nw->fa = T.NIL;     nw->num = x, nw->key = nw->mn = nw->mx = k, nw->tg = 0;   }   void dfs(int x) {     dfn[++dcnt] = x, fir[x] = bac[x] = &T.pl[dcnt],     newnode(&T.pl[dcnt], x, dep[x]);     for (int i = 0; i < G[x].size(); i++) {       int p = G[x][i];       dep[p] = dep[x] + 1, dfs(p);       dfn[++dcnt] = x, bac[x] = &T.pl[dcnt], newnode(&T.pl[dcnt], x, dep[x]);     }   }   Splay::node *build(int l, int r) {     if (l > r) return T.NIL;     int mid = (l + r) >> 1;     Splay::node *p = &T.pl[mid];     p->ch[0] = build(l, mid - 1);     if (p->ch[0] != T.NIL) p->ch[0]->fa = p;     p->ch[1] = build(mid + 1, r);     if (p->ch[1] != T.NIL) p->ch[1]->fa = p;     T.pushup(p);     return p;   }   bool tag[MAXN + 5];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       int l;       scanf( %d , &l);       for (int j = 1; j <= l; j++) {         int x;         scanf( %d , &x);         addedge(i, x);         tag[x] = true;       }     }     for (int i = 1; i <= n; i++)       if (!tag[i]) root = i;     newnode(&T.pl[++dcnt], 0, -1), dfs(root), newnode(&T.pl[++dcnt], 0, -1);     T.root = build(1, dcnt);     for (int i = 1; i <= m; i++) {       int op;       scanf( %d , &op);       if (op == 1) {         int v, u;         scanf( %d%d , &v, &u);         printf( %d n , T.dist(fir[v], fir[u]));       } else if (op == 2) {         int v, h;         scanf( %d%d , &v, &h);         T.splay(fir[v], T.NIL);         Splay::node *u = T.query(fir[v]->ch[0], fir[v]->key - h);         Splay::node *l = T.prev(fir[v], T.NIL), *r = T.next(bac[v], T.root);         if (bac[l->num] == r) bac[l->num] = l;         T.set_child(l, r->ch[1], 1);         Splay::node *p = r->ch[0];         newnode(r, u->num, u->key), r->ch[0] = p;         r->tg += 1 - h, T.pushdown(r), T.pushup(r);         T.next(bac[u->num], T.NIL), T.splay(bac[u->num], T.root);         bac[u->num] = r;         T.set_child(T.root->ch[0], r, 1);       } else if (op == 3) {         int k;         scanf( %d , &k);         printf( %d n , T.query(T.root, k)->num);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct input {     int a;     int b;   };   int myfunc(input x, input y) {     if (x.a > y.a) {       return false;     } else if (x.a < y.a) {       return true;     } else {       if (x.b > y.b) {         return false;       } else {         return true;       }     }   }   int main() {     int n;     cin >> n;     vector<input> vec;     input x;     for (int i = 0; i < n; i++) {       cin >> x.a;       x.b = i + 1;       vec.push_back(x);     }     sort(vec.begin(), vec.end(), myfunc);     int times = 0;     unsigned long long ways = 1;     int last = vec.at(0).a;     bool right = false;     int pairs = 0;     vector<int> ans1;     vector<int> ans2;     vector<int> ans3;     int i;     for (i = 0; i < n; i++) {       if (last == vec.at(i).a) {         times++;       } else {         ways *= times;         if (times == 1) {           ans1.push_back(vec.at(i - 1).b);           ans2.push_back(vec.at(i - 1).b);           ans3.push_back(vec.at(i - 1).b);         } else if (times == 2) {           pairs++;           if (pairs >= 2) {             ans1.push_back(vec.at(i - 2).b);             ans1.push_back(vec.at(i - 1).b);             ans2.push_back(vec.at(i - 1).b);             ans2.push_back(vec.at(i - 2).b);             ans3.push_back(vec.at(i - 2).b);             ans3.push_back(vec.at(i - 1).b);           } else {             ans1.push_back(vec.at(i - 2).b);             ans1.push_back(vec.at(i - 1).b);             ans2.push_back(vec.at(i - 2).b);             ans2.push_back(vec.at(i - 1).b);             ans3.push_back(vec.at(i - 1).b);             ans3.push_back(vec.at(i - 2).b);           }         } else if (times >= 3) {           for (int j = i - 4; j >= i - times; j--) {             ans1.push_back(vec.at(j).b);             ans2.push_back(vec.at(j).b);             ans3.push_back(vec.at(j).b);           }           ans1.push_back(vec.at(i - 3).b);           ans1.push_back(vec.at(i - 2).b);           ans1.push_back(vec.at(i - 1).b);           ans2.push_back(vec.at(i - 2).b);           ans2.push_back(vec.at(i - 3).b);           ans2.push_back(vec.at(i - 1).b);           ans3.push_back(vec.at(i - 3).b);           ans3.push_back(vec.at(i - 1).b);           ans3.push_back(vec.at(i - 2).b);         }         times = 1;         last = vec.at(i).a;       }       if (i == n - 1) {         ways *= times;         if (times == 1) {           ans1.push_back(vec.at(i).b);           ans2.push_back(vec.at(i).b);           ans3.push_back(vec.at(i).b);         } else if (times == 2) {           pairs++;           if (pairs >= 2) {             ans1.push_back(vec.at(i - 1).b);             ans1.push_back(vec.at(i).b);             ans2.push_back(vec.at(i).b);             ans2.push_back(vec.at(i - 1).b);             ans3.push_back(vec.at(i - 1).b);             ans3.push_back(vec.at(i).b);           } else {             ans1.push_back(vec.at(i - 1).b);             ans1.push_back(vec.at(i).b);             ans2.push_back(vec.at(i - 1).b);             ans2.push_back(vec.at(i).b);             ans3.push_back(vec.at(i).b);             ans3.push_back(vec.at(i - 1).b);           }         } else if (times >= 3) {           for (int j = i - 3; j > i - times; j--) {             ans1.push_back(vec.at(j).b);             ans2.push_back(vec.at(j).b);             ans3.push_back(vec.at(j).b);           }           ans1.push_back(vec.at(i - 2).b);           ans1.push_back(vec.at(i - 1).b);           ans1.push_back(vec.at(i).b);           ans2.push_back(vec.at(i - 2).b);           ans2.push_back(vec.at(i).b);           ans2.push_back(vec.at(i - 1).b);           ans3.push_back(vec.at(i - 1).b);           ans3.push_back(vec.at(i - 2).b);           ans3.push_back(vec.at(i).b);         }       }       if (ways >= 3) {         right = true;       }     }     if (!right) {       cout <<  NO  << endl;     } else {       cout <<  YES  << endl;       for (int j = 0; j < n; j++) {         cout << ans1.at(j) <<    ;       }       cout << endl;       for (int j = 0; j < n; j++) {         cout << ans2.at(j) <<    ;       }       cout << endl;       for (int j = 0; j < n; j++) {         cout << ans3.at(j) <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int t;     cin >> t;     while (t--) {       int n, x;       cin >> n >> x;       vector<int> a(n), b(n);       for (int i = 0; i < n; i++) cin >> a[i];       for (int i = 0; i < n; i++) cin >> b[i];       sort(a.begin(), a.end());       sort(b.rbegin(), b.rend());       int f = 0;       for (int i = 0; i < n; i++) {         if (a[i] + b[i] > x) {           f = 1;           break;         }       }       if (f)         cout <<  No  << endl;       else         cout <<  Yes  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int tc = 1;   const int M = 1e9 + 7;   const long long Inf = (long long)2e18 + 5;   const int N = 2e5 + 5;   std::vector<long long> a[N];   int solve() {     int a;     cin >> a;     int z = 0;     int bg = 0;     int xx = 0;     int mx = 0;     long long ans = 0LL;     for (z = 0; z < a; z++) {       int n;       cin >> n;       mx = 0;       for (int i = 0; i < n; i++) {         int x;         cin >> x;         mx = max(mx, x);       }       if (z > 0) {         if (xx > mx) {           ans += (long long)(xx - mx) * n;           bg += n;         } else {           ans += (long long)(mx - xx) * bg;           xx = mx;           bg += n;         }       } else {         xx = mx;         bg += n;       }     }     cout << ans << endl;     return 0;   }   int main() {     int test = 1;     while (test--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, x1, x2, y1, y2;     cin >> n >> m >> x1 >> y1 >> x2 >> y2;     int dx = abs(x2 - x1);     int dy = abs(y2 - y1);     if (dx <= 4 && dy <= 2 || dy <= 4 && dx <= 2 || dx == 3 && dy == 3) {       cout <<  First ;     } else {       cout <<  Second ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, a, b, c;   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> a >> b >> c;     if (n == 1)       cout << 0 << endl;     else if (c >= min(a, b))       cout << (n - 1) * min(a, b) << endl;     else       cout << min(a, b) + (n - 2) * c << endl;     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int l = s.length();     if ((s[l - 1] - 48) % 2 == 0) {       int temp = s[l - 2] * 10 + s[l - 1];       if (temp % 4 == 0) {         cout << 4 << endl;       } else {         cout << 0 << endl;       }     } else {       cout << 0 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int c[100010];   int x[100010], y[100010];   vector<int> t[100010];   int f[100010];   int find(int x) { return (f[x] == -1 ? x : f[x] = find(f[x])); }   int fa[100010], dep[100010];   int dfn[100010], rnk[100010], totn = 0;   int siz[100010], son[100010], top[100010];   void dfs1(int k) {     dep[k] = dep[fa[k]] + 1;     siz[k] = 1;     for (vector<int>::iterator it = t[k].begin(); it != t[k].end(); ++it) {       if (*it == fa[k]) continue;       fa[*it] = k;       dfs1(*it);       siz[k] += siz[*it];       if (son[k] == 0 || siz[*it] > siz[son[k]]) son[k] = *it;     }     return;   }   void dfs2(int k, int tp) {     dfn[k] = ++totn;     rnk[totn] = k;     top[k] = tp;     if (son[k]) dfs2(son[k], tp);     for (vector<int>::iterator it = t[k].begin(); it != t[k].end(); ++it) {       if (*it == fa[k] || *it == son[k]) continue;       dfs2(*it, *it);     }     return;   }   int LCA(int a, int b) {     while (top[a] != top[b]) {       if (dep[top[a]] < dep[top[b]]) swap(a, b);       a = fa[top[a]];     }     return (dep[a] < dep[b] ? a : b);   }   long long v[100010];   void dfs3(int k) {     v[k] = c[k];     for (vector<int>::iterator it = t[k].begin(); it != t[k].end(); ++it) {       if (*it == fa[k]) continue;       dfs3(*it);       v[k] -= v[*it];     }     return;   }   long long ve[100010];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) scanf( %d , &c[i]);     for (int i = 1; i <= m; ++i) scanf( %d%d , &x[i], &y[i]);     memset(f, -1, sizeof(f));     for (int i = 1; i <= m; ++i) {       int p = find(x[i]), q = find(y[i]);       if (p == q) continue;       f[p] = q;       t[x[i]].push_back(y[i]);       t[y[i]].push_back(x[i]);     }     dfs1(1);     dfs2(1, 1);     dfs3(1);     for (int i = 1; i <= m; ++i) {       if (fa[x[i]] == y[i] || fa[y[i]] == x[i]) continue;       if ((dep[x[i]] + dep[y[i]] - 2 * dep[LCA(x[i], y[i])]) % 2 == 0) {         int val = v[1] / 2;         val = val * (dep[x[i]] % 2 == 0 ? -1 : 1);         ve[i] = val;         c[x[i]] -= val;         c[y[i]] -= val;         break;       }     }     dfs3(1);     if (v[1] != 0) {       puts( NO );       return 0;     }     puts( YES );     for (int i = 1; i <= m; ++i) {       if (fa[x[i]] == y[i] || fa[y[i]] == x[i]) {         if (dep[x[i]] < dep[y[i]]) swap(x[i], y[i]);         ve[i] = v[x[i]];       }       printf( %lld n , ve[i]);     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( unroll-loops )   #pragma GCC optimize( O3 )   #pragma GCC optimize( fast-math )   using namespace std;   const int INF = 1e9;   const long long INFll = 2e18;   const int BASE1 = 179;   const int BASE2 = 653;   const long long MOD = 1e9 + 7;   const int MAXN = 2e5;   const long double PI = 3.1415926535;   const long double EPS = 1e-10;   void solve() {     int n;     cin >> n;     vector<vector<char>> dop(n, vector<char>(n,  * ));     for (int i = 0; i < (n); i++) {       for (int j = 0; j < (n); j++) {         if (abs(i - n / 2) + abs(j - n / 2) <= n / 2) dop[i][j] =  D ;       }     }     for (int i = 0; i < (n); i++) {       for (int j = 0; j < (n); j++) {         cout << dop[i][j];       }       cout <<   n ;     }   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout << fixed << setprecision(15);     int t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 3e5 + 10;   long long n, m;   long long res[N], sum[N], a[N];   long long len;   struct Node {     int x, p, id;   } Q[N];   inline long long read() {     long long x = 0, f = 0;     char ch;     while (((ch = getchar()) <  0  || ch >  9 ) && (ch !=  - ))       ;     while (!isdigit(ch)) f |= ch ==  - , ch = getchar();     while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();     return f ? -x : x;   }   inline bool cmp(Node a, Node b) { return a.p != b.p ? a.p < b.p : a.x > b.x; }   signed main() {     n = read();     len = sqrt(n);     for (long long i = 1; i <= n; i++) a[i] = read();     m = read();     for (int i = 1; i <= m; i++) {       long long x, p;       x = read(), p = read();       Q[i].x = x, Q[i].p = p;       Q[i].id = i;     }     sort(Q + 1, Q + m + 1, cmp);     int last = n;     for (int i = 1; i <= m; i++) {       long long ans = 0;       if (Q[i].p >= len) {         for (int j = Q[i].x; j <= n; j += Q[i].p) {           ans += a[j];         }       } else {         int p = Q[i].p, x = Q[i].x;         if (Q[i].p != Q[i - 1].p) last = n;         for (int j = last; j >= x; j--) {           sum[j] = a[j];           if (j + p <= n) sum[j] += sum[j + p];         }         last = x - 1;         ans = sum[x];       }       res[Q[i].id] = ans;     }     for (int i = 1; i <= m; i++) printf( %lld n , res[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long readi() {     long long input = 0;     char c =    ;     while (c <  - ) {       c = getchar();     }     bool negative = false;     if (c ==  - ) {       negative = true;       c = getchar();     }     while (c >=  0 ) {       input = 10 * input + (c -  0 );       c = getchar();     }     if (negative) {       input = -input;     }     return input;   }   void printi(long long output) {     if (output == 0) {       putchar( 0 );       return;     }     if (output < 0) {       putchar( - );       output = -output;     }     int aout[20];     int ilen = 0;     while (output) {       aout[ilen] = ((output % 10));       output /= 10;       ilen++;     }     for (int i = ilen - 1; i >= 0; i--) {       putchar(aout[i] +  0 );     }     return;   }   string reads() {     string input =   ;     char c =    ;     while (c <=    ) {       c = getchar();     }     while (c >    ) {       input += c;       c = getchar();     }     return input;   }   void prints(string output) {     for (int i = 0; i < output.length(); i++) {       putchar(output[i]);     }     return;   }   long long getrand() {     long long rand1 = (rand() % 536870912);     long long rand2 = (rand() % 536870912);     return (rand1 * 536870912ll + rand2);   }   int N, E;   vector<string> pretest, systest;   vector<int> badp, bads;   vector<pair<string, int> > ans;   int nump[1000010], nums[1000010];   bool used[1000010];   vector<int> vp, vs, num;   int32_t main() {     srand(time(NULL));     cout << fixed << setprecision(10);     cerr << fixed << setprecision(10);     ios_base::sync_with_stdio(false);     if (fopen( cf860c.in ,  r )) {       freopen( cf860c.in ,  r , stdin);     }     cin >> N;     for (int i = 0; i < N; i++) {       string s;       cin >> s;       bool idx;       cin >> idx;       if (idx) {         pretest.push_back(s);       } else {         systest.push_back(s);       }     }     E = pretest.size();     for (int i = 0; i < pretest.size(); i++) {       bool isnum = true;       for (int j = 0; j < pretest[i].length(); j++) {         if (!isdigit(pretest[i][j])) {           isnum = false;         }       }       if (pretest[i][0] ==  0 ) {         isnum = false;       }       if (isnum) {         nump[i] = stoi(pretest[i]);         used[nump[i]] = true;         if (E + 1 <= nump[i] && nump[i] <= N) {           badp.push_back(i);         }       }     }     for (int i = 0; i < systest.size(); i++) {       bool isnum = true;       for (int j = 0; j < systest[i].length(); j++) {         if (!isdigit(systest[i][j])) {           isnum = false;         }       }       if (systest[i][0] ==  0 ) {         isnum = false;       }       if (isnum) {         nums[i] = stoi(systest[i]);         used[nums[i]] = true;         if (1 <= nums[i] && nums[i] <= E) {           bads.push_back(i);         }       }     }     for (int i = 100000; i < 1000000; i++) {       if (!used[i]) {         num.push_back(i);       }     }     for (int i = 1; i <= E; i++) {       if (!used[i]) {         vp.push_back(i);       }     }     for (int i = E + 1; i <= N; i++) {       if (!used[i]) {         vs.push_back(i);       }     }     while (!badp.empty() || !bads.empty()) {       if (!badp.empty() && !vp.empty()) {         int idx = badp.back();         badp.pop_back();         vs.push_back(nump[idx]);         ans.push_back(make_pair(pretest[idx], vp.back()));         pretest[idx] = to_string(vp.back());         nump[idx] = vp.back();         vp.pop_back();       } else if (!bads.empty() && !vs.empty()) {         int idx = bads.back();         bads.pop_back();         vp.push_back(nums[idx]);         ans.push_back(make_pair(systest[idx], vs.back()));         systest[idx] = to_string(vs.back());         nums[idx] = vs.back();         vs.pop_back();       } else if (!badp.empty()) {         int idx = badp.back();         badp.pop_back();         vs.push_back(nump[idx]);         ans.push_back(make_pair(pretest[idx], num.back()));         pretest[idx] = to_string(num.back());         nump[idx] = num.back();         num.pop_back();       } else if (!bads.empty()) {         int idx = bads.back();         bads.pop_back();         vp.push_back(nums[idx]);         ans.push_back(make_pair(systest[idx], num.back()));         systest[idx] = to_string(num.back());         nums[idx] = num.back();         num.pop_back();       }     }     for (int i = 0; i < pretest.size(); i++) {       if (1 <= nump[i] && nump[i] <= E) {         continue;       }       ans.push_back(make_pair(pretest[i], vp.back()));       pretest[i] = vp.back();       vp.pop_back();     }     for (int i = 0; i < systest.size(); i++) {       if (E + 1 <= nums[i] && nums[i] <= N) {         continue;       }       ans.push_back(make_pair(systest[i], vs.back()));       systest[i] = vs.back();       vs.pop_back();     }     cout << ans.size() <<   n ;     for (int i = 0; i < ans.size(); i++) {       cout <<  move   << ans[i].first <<     << ans[i].second <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -1;     for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ;     return x * f;   }   const int MAXN = 200010;   char str[MAXN + 1], S[MAXN + 1];   int main() {     scanf( %s , str + 1);     int N = strlen(str + 1);     for (int i = 0; i <= N; i++)       for (char c =  a ; c <=  z ; c++) {         int top = 0;         for (int j = 1; j <= i; j++) S[++top] = str[j];         S[++top] = c;         for (int j = i + 1; j <= N; j++) S[++top] = str[j];         bool flag = 1;         for (int j = 1; j <= top; j++)           if (S[j] != S[top - j + 1]) {             flag = 0;             break;           }         if (flag) {           for (int j = 1; j <= top; j++) printf( %c , S[j]);           printf(  n );           return 0;         }       }     printf( NA n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, M;   int main() {     cin >> N >> M;     int putere = 1;     for (int i = 1; i <= M; i++) {       putere <<= 1;       if (putere >= 1000000009) putere -= 1000000009;     }     int val = 1;     for (int i = putere - 1; i >= putere - N; i--)       val = (1LL * val * i) % 1000000009;     cout << val;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long M = 4e5 + 10;   long long t, n, k, p, a[M], dp[M], s[M], ans;   long long bs(long long r, long long rem) {     long long l = 0, mid;     r++;     while (r - l > 1) {       mid = (r + l) / 2;       if (s[mid] > rem)         r = mid;       else         l = mid;     }     return l;   }   int32_t main() {     cin >> t;     while (t--) {       scanf( %lld%lld%lld , &n, &p, &k);       for (long long i = 1; i <= n; i++) {         scanf( %lld , a + i);       }       sort(a + 1, a + n + 1);       fill(s, s + n + 1, 0);       fill(dp, dp + n + 1, 0);       ans = 0;       for (long long i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];       for (long long i = k; i < k + k; i++) dp[i] = a[i];       for (long long i = k + k; i <= n; i++) dp[i] = dp[i - k] + a[i];       for (long long i = n; i >= k; i--)         if (dp[i] <= p) ans = max(ans, i - (i % k) + bs(i % k, p - dp[i]));       ans = max(ans, bs(k - 1, p));       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[100001];   long long n, k;   int main() {     long long p = 0, no = 0;     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     p = 0;     for (int i = 0; i < n; i++) {       if (p != a[i]) {         no++;         cout << a[i] - p << endl;       }       p = a[i];       if (no == k) break;     }     for (int i = 0; i < (k - no); i++) cout <<  0  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[1001], m, cnt[1001];   set<int> S;   int main() {     cin >> n >> m;     for (int i = 1; i <= m; i++) {       cin >> a[i];       cnt[a[i]]++;     }     cout << *min_element(cnt + 1, cnt + n + 1);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     int q;     cin >> q;     vector<int> ta(n, -1);     vector<int> payouts(q, 0);     for (int i = 0; i < q; i++) {       int k, p, x;       cin >> k;       if (k == 1) {         cin >> p >> x;         a[p - 1] = x;         ta[p - 1] = i;       } else {         cin >> x;         payouts[i] = x;       }     }     vector<int> maxpayouts(q, payouts[q - 1]);     for (int i = q - 2; i >= 0; i--) {       maxpayouts[i] = max(maxpayouts[i + 1], payouts[i]);     }     maxpayouts.push_back(0);     for (int i = 0; i < n; i++) {       int res = max(maxpayouts[ta[i] + 1], a[i]);       cout << res <<    ;     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const unsigned long long maxn = 200005;   unsigned long long n, m, vis[maxn];   unsigned long long sum, A, B, C, he[maxn];   struct node {     unsigned long long x, y;     bool operator<(const node &a) const { return x == a.x ? x < a.x : y < a.y; }   } chongtu[maxn];   vector<unsigned long long> to[maxn], from[maxn];   vector<unsigned long long> toq[maxn], fromq[maxn];   map<pair<unsigned long long, unsigned long long>, unsigned long long> mp;   inline unsigned long long read() {     unsigned long long x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 1) + (x << 3) + (ch ^ 48);       ch = getchar();     }     return x * f;   }   inline void write(unsigned long long a) {     if (a < 0) {       putchar( - );       putchar( 1 );     } else {       if (a >= 10) write(a / 10);       putchar(a % 10 +  0 );     }   }   signed main() {     n = read(), m = read();     A = read(), B = read(), C = read();     for (unsigned long long i = 0; i <= n - 3; ++i) {       unsigned long long len = (n - 1) - (i + 1) + 1;       sum += 1ull * len * (len - 1) / 2 * A * i;     }     for (unsigned long long j = 1; j <= n - 2; ++j) {       unsigned long long len1 = (j - 1) - 0 + 1, len2 = (n - 1) - (j + 1) + 1;       sum += 1ull * len1 * len2 * B * j;     }     for (unsigned long long k = 2; k <= n - 1; ++k) {       unsigned long long len = (k - 1) - 0 + 1;       sum += 1ull * len * (len - 1) / 2 * C * k;     }     for (unsigned long long i = 1; i < n; ++i) he[i] = he[i - 1] + i;     for (unsigned long long i = 1; i <= m; ++i) {       unsigned long long x = read(), y = read();       if (x > y) swap(x, y);       chongtu[i].x = x, chongtu[i].y = y;     }     sort(chongtu + 1, chongtu + m + 1);     for (unsigned long long i = 1; i <= m; ++i) {       unsigned long long x = chongtu[i].x, y = chongtu[i].y;       mp[make_pair(x, y)]++;       to[x].push_back(y);       toq[x].push_back(1);       from[y].push_back(x);       fromq[y].push_back(1);       if (x >= 1)         sum -= 1ull * ((x - 1) - 0 + 1) * (B * x + C * y) +                1ull * A * (he[x - 1] - he[0]);       if (y - x >= 2)         sum -= 1ull * ((y - 1) - (x + 1) + 1) * (A * x + C * y) +                1ull * B * (he[y - 1] - he[x]);       if (y <= n - 2)         sum -= 1ull * ((n - 1) - (y + 1) + 1) * (A * x + B * y) +                1ull * C * (he[n - 1] - he[y]);     }     for (unsigned long long i = 0; i < n; ++i) {       sort(to[i].begin(), to[i].end());       sort(from[i].begin(), from[i].end());     }     for (unsigned long long fu = 0; fu < n; ++fu) {       unsigned long long size11 = to[fu].size();       for (unsigned long long k = 0; k < size11; ++k) {         unsigned long long zi = to[fu][k];         toq[fu][k] = zi;         if (k) toq[fu][k] += toq[fu][k - 1];       }     }     for (unsigned long long zi = 0; zi < n; ++zi) {       unsigned long long size11 = from[zi].size();       for (unsigned long long k = 0; k < size11; ++k) {         unsigned long long fu = from[zi][k];         fromq[zi][k] = fu;         if (k) fromq[zi][k] += fromq[zi][k - 1];       }     }     for (unsigned long long fu = 0; fu < n; ++fu) {       unsigned long long size = to[fu].size();       sum += 1ull * (size - 1) * (size) / 2 * A * fu;       for (unsigned long long j = 0; j < size; ++j) {         unsigned long long zi1 = to[fu][j];         sum += 1ull * B * ((size - 1) - (j + 1) + 1) * zi1;         sum += 1ull * C * (toq[fu][size - 1] - toq[fu][j]);       }     }     for (unsigned long long zi = 0; zi < n; ++zi) {       unsigned long long size = from[zi].size();       sum += 1ull * (size - 1) * (size) / 2 * C * zi;       for (unsigned long long j = 0; j < size; ++j) {         unsigned long long fu1 = from[zi][j];         sum += 1ull * A * ((size - 1) - (j + 1) + 1) * fu1;         sum += 1ull * B * (fromq[zi][size - 1] - fromq[zi][j]);       }     }     for (unsigned long long zi = 0; zi < n; ++zi) {       unsigned long long size1 = to[zi].size(), size2 = from[zi].size();       sum += 1ull * B * (size1) * (size2)*zi;       for (unsigned long long j = 0; j < size1; ++j) {         unsigned long long sun = to[zi][j];         sum += 1ull * C * sun * size2;         if (size2) sum += 1ull * A * fromq[zi][size2 - 1];         for (unsigned long long k = 0; k < size2; ++k) {           unsigned long long fu = from[zi][k];         }       }     }     for (unsigned long long fu = 0; fu < n; ++fu) {       unsigned long long size = to[fu].size();       for (unsigned long long j = 0; j < size; ++j) vis[to[fu][j]] = 1;       for (unsigned long long j = 0; j < size; ++j) {         unsigned long long zi = to[fu][j];         unsigned long long sizez = to[zi].size();         for (unsigned long long k = 0; k < sizez; ++k) {           unsigned long long sun = to[zi][k];           if (vis[sun]) sum -= (1ull * A * fu + 1ull * B * zi + 1ull * C * sun);         }       }       for (unsigned long long j = 0; j < size; ++j) vis[to[fu][j]] = 0;     }     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[300005], key[300005];   struct node {     int cnt;     struct node *zero, *one;     node() {       cnt = 0;       zero = NULL, one = NULL;     }   } * root;   void Insert(int pat) {     struct node *curr = root;     int on = 1;     for (int i = 30; i >= 0; i--) {       int bit = (pat & (on << i));       if (bit) {         if (curr->one == NULL) curr->one = new node();         curr = curr->one;         curr->cnt++;       } else {         if (curr->zero == NULL) curr->zero = new node();         curr = curr->zero;         curr->cnt++;       }     }   }   void Search(int val) {     struct node *curr = root;     int on = 1;     long long int ans = 0;     for (int i = 30; i >= 0; i--) {       int bit = (val & (on << i));       if (bit) {         if (curr->one != NULL && curr->one->cnt > 0) {           curr = curr->one;           curr->cnt--;         } else if (curr->zero != NULL && curr->zero->cnt > 0) {           curr = curr->zero;           curr->cnt--;           ans += (on << i);         }       } else {         if (curr->zero != NULL && curr->zero->cnt > 0) {           curr = curr->zero;           curr->cnt--;         } else if (curr->one != NULL && curr->one->cnt > 0) {           curr = curr->one;           curr->cnt--;           ans += (on << i);         }       }     }     cout << ans <<    ;   }   int main() {     int n;     cin >> n;     root = new node();     for (int i = 1; i <= n; i++) cin >> arr[i];     for (int i = 1; i <= n; i++) {       cin >> key[i];       Insert(key[i]);     }     for (int i = 1; i <= n; i++) {       Search(arr[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int F[30030];   int n;   int mod;   int a[30030], id[30030];   int nxt[30030];   bool cmp(int i, int j) { return a[i] < a[j]; }   int m;   struct pnt {     int l, r, i;     pnt(int l = 0, int r = 0, int i = 0) : l(l), r(r), i(i){};   } q[30030];   bool operator<(pnt a, pnt b) { return b.l != a.l ? a.l < b.l : a.r < b.r; }   int answer[30030];   int calc(int l, int r) {     int s = 0, m = 0;     for (int j, i = 1; i <= n; i++) {       j = id[i];       if (j >= l && j <= r) {         s = (s + F[++m] * a[j]) % mod;         i = nxt[i];       }     }     return s;   }   int main() {     scanf( %d %d , &n, &mod);     F[1] = F[2] = 1 % mod;     for (int i = 3; i <= n; i++) {       F[i] = (F[i - 1] + F[i - 2]) % mod;     }     for (int i = 1; i <= n; i++) {       scanf( %d , a + i);       id[i] = i;     }     sort(id + 1, id + n + 1, cmp);     nxt[n] = n;     for (int j = n - 1; j >= 1; j--) {       if (a[id[j]] == a[id[j + 1]]) {         nxt[j] = nxt[j + 1];       } else {         nxt[j] = j;       }     }     for (int j = 1; j <= n; j++) {       a[j] %= mod;     }     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       scanf( %d %d , &q[i].l, &q[i].r);       q[i].i = i;     }     sort(q + 1, q + m + 1);     for (int i = 1; i <= m; i++) {       if (i > 1 && q[i].l == q[i - 1].l && q[i].r == q[i - 1].r) {         answer[q[i].i] = answer[q[i - 1].i];       } else {         answer[q[i].i] = calc(q[i].l, q[i].r);       }     }     for (int i = 1; i <= m; i++) {       printf( %d n , answer[i]);     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;      #define ll          long long   #define int         ll   #define ld          long double   #define ff          first   #define ss          second   #define io          ios_base::sync_with_stdio(false); cin.tie(NULL);   #define yes         cout<< YES    #define no          cout<< NO    #define loop(i,a,n) for(int i=a;i<n;i++)   #define rloop(i,n,a)for(int i=n-1;i>=a;i--)   #define test        int tt=1; cin>>tt; while(tt--)   #define pb          push_back   #define pi          3.141592653589793238   #define all(v)      v.begin(),v.end()   #define vecl        vector<int>   #define pii         pair<int,int>   #define vpii        vector<pii>   const int MOD = 1e9 + 7;   int fast_expo(int x, int p) {       if (p == 0) return 1;       else if (p % 2 == 0) { int t = fast_expo(x, p / 2) % MOD; return (t * t) % MOD;}       else return (x * (fast_expo(x, p - 1)) % MOD) % MOD;   }   int gcd(int a, int b) {       if (a == 0 || b == 0) return a + b;       else return gcd(b % a, a);   }      int lcm(int a, int b)   {       return (a * b) / gcd(a, b);   }   vecl v;   void primeFactors(int n)   {       // Print the number of 2s that divide n       while (n % 2 == 0)       {           v.pb(2);           n = n / 2;       }          // n must be odd at this point. So we can skip       // one element (Note i = i +2)       for (int i = 3; i <= sqrt(n); i = i + 2)       {           // While i divides n, print i and divide n           while (n % i == 0)           {               v.pb(i);               n = n / i;           }       }          // This condition is to handle the case when n       // is a prime number greater than 2       if (n > 2)           v.pb(n);   }   //int t[101][101];   void calc(vecl &a, int l, int r, vecl &b, int d)   {       if (l >= r)           return;          int mx = 0, ans = 0;       loop(i, l, r)       {           mx = max(mx, a[i]);       }       loop(i, l, r)       {           if (mx == a[i])           {               ans = i; break;           }       }       b[ans] = d;       calc(a, ans + 1, r, b, d + 1);       calc(a, l, ans, b, d + 1);   }      void solve()   {       int n;       cin >> n;       vecl a(n);       loop(i, 0, n)       {           cin >> a[i];       }       //memset(t,-1, sizeof t);       int l = 0, r = n;       vecl ans(n + 1, 0);       int d = 0;       calc(a, l, r, ans, d);       loop(i, 0, n)       cout << ans[i] <<    ;   }      signed main() {          io   #ifndef ONLINE_JUDGE       freopen( inputf.in ,  r , stdin);       freopen( outputf.out ,  w , stdout);   #endif       //calc();       test       {           solve();           cout << endl;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long min(unsigned long long x, unsigned long long y) {     if (x < y) return x;     return y;   }   unsigned long long max(unsigned long long x, unsigned long long y) {     if (x < y) return y;     return x;   }   long long min(long long x, long long y) {     if (x < y) return x;     return y;   }   long long max(long long x, long long y) {     if (x < y) return y;     return x;   }   double min(double x, double y) {     if (x < y) return x;     return y;   }   double max(double x, double y) {     if (x < y) return y;     return x;   }   unsigned long long gcd(unsigned long long x, unsigned long long y) {     if (!x) return y;     if (!y) return x;     if (x > y) swap(x, y);     return gcd(x, y % x);   }   unsigned long long inv(unsigned long long a, unsigned long long c) {     if (a == 1) {       return 1;     }     return ((c - (c / a)) * inv(c % a, c)) % c;   }   int n, l[300005] = {0}, r[300005] = {0}, idx[300005] = {0};   int comp(int a, int b) {     if (l[a] == l[b]) return r[a] > r[b];     return l[a] < l[b];   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> l[i] >> r[i];       idx[i] = i;     }     sort(idx, idx + n, comp);     int minr = r[idx[n - 1]], j = n - 1;     for (int i = n - 2; i >= 0; --i) {       if (minr <= r[idx[i]]) {         cout << (idx[j] + 1) <<     << (idx[i] + 1) <<   n ;         return 0;       }       if (r[idx[i]] < minr) {         j = i;         minr = r[idx[i]];       }     }     cout <<  -1 -1 n ;     return 0;   }


#include <bits/stdc++.h>   const int maxn = 200000;   using namespace std;   int a[maxn], b[maxn];   int l[maxn], r[maxn];   int main() {     int n, q;     scanf( %d%d , &n, &q);     a[0] = 0;     for (int i = 1; i <= n; i++)       scanf( %d , &a[i]), b[i - 1] = abs(a[i] - a[i - 1]);     for (int i = 1; i <= q; i++) {       int x, y;       long long ans = 0;       scanf( %d%d , &x, &y);       y--;       l[x] = x - 1;       r[y] = y + 1;       for (int j = x + 1; j <= y; j++) {         l[j] = j - 1;         while (l[j] >= x && b[l[j]] < b[j]) l[j] = l[l[j]];       }       for (int j = y - 1; j >= x; j--) {         r[j] = j + 1;         while (r[j] <= y && b[r[j]] <= b[j]) r[j] = r[r[j]];       }       for (int j = x; j <= y; j++)         ans += (long long)(l[j] - j) * (j - r[j]) * b[j];       printf( %I64d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       vector<long long> a(n);       long long mn = 1e5;       for (int i = 0; i < n; i++) {         cin >> a[i];         if (a[i] < mn) {           mn = a[i];         }       }       long long ans = 0;       bool is_mn = false;       for (int i = 0; i < n; i++) {         if (a[i] == mn && not is_mn) {           is_mn = true;           continue;         }         while (a[i] + mn <= k) {           a[i] += mn;           ans++;         }       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const string roman[] = { M ,   CM ,  D ,   CD ,  C ,   XC ,  L ,                            XL ,  X ,   IX ,  V ,   IV ,  I };   const int roman_num[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};   int a;   string b, c;   string ans;   long long toint(string s, int base) {     long long res = 0, add;     for (int i = 0; i < s.size(); i++) {       res = res * base;       if (isdigit(s[i]))         add = s[i] -  0 ;       else         add = s[i] -  A  + 10;       res += add;     }     return res;   }   string roman_convert(int num) {     int pos = 0;     string res =   ;     while (num > 0) {       while (roman_num[pos] <= num) {         num -= roman_num[pos];         res += roman[pos];       }       pos++;     }     return res;   }   string convert(string c, int from, int to) {     long long base_10 = 0LL;     string temp = c;     for (int i = 0; i < temp.size(); i++) {       int add;       if (isdigit(temp[i]))         add = temp[i] -  0 ;       else         add = temp[i] -  A  + 10;       base_10 = base_10 * from + add;     }     string res =   ;     while (base_10) {       int digit = base_10 % to;       if (digit >= 10)         res.push_back( A  + digit - 10);       else         res.push_back(digit +  0 );       base_10 /= to;     }     if (res.empty()) return  0 ;     reverse(res.begin(), res.end());     while (res.at(0) ==  0  && res.size() > 11) res.erase(0, 1);     return res;   }   int main() {     cin.sync_with_stdio(0);     cin >> a >> b >> c;     while (c[0] ==  0  && c.size() > 1) c.erase(0, 1);     if (b ==  R ) {       int temp = toint(c, a);       ans = roman_convert(temp);     } else {       int base = toint(b, 10);       ans = convert(c, a, base);     }     puts(ans.c_str());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100005];   int main(void) {     int n, x, k;     scanf( %d %d %d , &n, &x, &k);     for (int i = 0; i < n; i++) {       scanf( %d , a + i);     }     sort(a, a + n);     long long sum = 0;     for (int i = 0; i < n; i++) {       long long p = (long long)ceil(a[i] * 1.0 / x) * x;       sum +=           lower_bound(a, a + n, p + (long long)k * x) -           lower_bound(a, a + n, max((long long)a[i], p + (long long)(k - 1) * x));     }     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int min_answer;   long long int max_answer = -1;   void test_this_order(long long int a, long long int b, long long int c) {     long long int new_n = (a + 1) * (b + 2) * (c + 2) - a * b * c;     min_answer = std::min(min_answer, new_n);     max_answer = std::max(max_answer, new_n);   }   void test(long long int a, long long int b, long long int c) {     test_this_order(a, b, c);     test_this_order(b, a, c);     test_this_order(c, b, a);   }   int main() {     long long int n;     cin >> n;     test(n, 1, 1);     min_answer = max_answer;     for (long long int i = 1; i * i <= n; ++i) {       if (n % i == 0) {         long long new_n = n / i;         for (long long int j = 1; j * j <= new_n; ++j) {           if (new_n % j == 0) {             test(i, j, new_n / j);           }         }       }     }     cout << min_answer <<     << max_answer << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   const int level = 20;   vector<int> adj[N];   bool checked[N];   int parent[N];   int sz[N];   int centroid;   int sparse[N][level];   int depth[N];   void subtree(int cur, int prev) {     sz[cur] = 1;     for (auto &child : adj[cur]) {       if (child != prev and !checked[child]) {         subtree(child, cur);         sz[cur] += sz[child];       }     }   }   void decompose(int cur, int prev, int total_size) {     for (auto &child : adj[cur]) {       if (child != prev and !checked[child]) {         decompose(child, cur, total_size);       }     }     if (centroid != -1) {       return;     }     if (2 * sz[cur] >= total_size) {       centroid = cur;       return;     }   }   int get_centroid(int cur) {     subtree(cur, 0);     centroid = -1;     decompose(cur, 0, sz[cur]);     return centroid;   }   void dfs(int cur, int prev) {     int root = get_centroid(cur);     parent[root] = prev;     checked[root] = true;     for (auto &child : adj[root]) {       if (!checked[child]) {         dfs(child, root);       }     }   }   void dfs_sparse(int cur, int prev) {     sparse[cur][0] = prev;     for (auto &child : adj[cur]) {       if (child != prev) {         depth[child] = depth[cur] + 1;         dfs_sparse(child, cur);       }     }   }   void solve() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n - 1; i++) {       int x, y;       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     dfs(1, 0);     dfs_sparse(1, -1);     for (int l = 1; l < level; l++) {       for (int node = 1; node <= n; node++) {         if (sparse[node][l - 1] != -1) {           sparse[node][l] = sparse[sparse[node][l - 1]][l - 1];         }       }     }     auto lca = [&](int u, int v) -> int {       if (depth[v] < depth[u]) {         swap(u, v);       }       int diff = depth[v] - depth[u];       for (int i = 0; i < level; i++) {         if ((diff >> i) & 1) {           v = sparse[v][i];         }       }       if (u == v) {         return u;       }       for (int i = level - 1; i >= 0; i--) {         if (sparse[u][i] != sparse[v][i]) {           u = sparse[u][i];           v = sparse[v][i];         }       }       return sparse[u][0];     };     auto get_dist = [&](int x, int y) {       int lc = lca(x, y);       return depth[x] + depth[y] - 2 * depth[lc];     };     vector<int> dist(n + 1, n);     int node = 1;     for (int i = node; i; i = parent[i]) {       dist[i] = get_dist(i, node);     }     for (int i = 1; i <= m; i++) {       int type, node;       cin >> type >> node;       if (type == 1) {         for (int i = node; i; i = parent[i]) {           dist[i] = min(dist[i], get_dist(i, node));         }       } else {         int ans = n;         for (int i = node; i; i = parent[i]) {           ans = min(ans, get_dist(i, node) + dist[i]);         }         cout << (dist[node] = ans) <<   n ;       }     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();   }


#include <bits/stdc++.h>   using namespace std;   const int MX = 2000 + 1;   const int M = 1e9 + 7;   vector<pair<int, int>> adj[MX];   int dp[MX][MX];   int add(int a, int b) { return (a += b) < M ? a : a - M; }   int mul(int a, int b) { return 1LL * a * b % M; }   int floor_div(int a, int b) { return a / b - ((a ^ b) < 0 && a % b); }   int intersect(pair<int, int> p, pair<int, int> q) {     return floor_div(p.second - q.second, q.first - p.first);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m, q;     cin >> n >> m >> q;     for (int i = 0; i < m; ++i) {       int u, v, w;       cin >> u >> v >> w;       adj[u].emplace_back(v, w);       adj[v].emplace_back(u, w);     }     memset(dp, -1, sizeof dp);     dp[0][1] = 0;     for (int i = 0; i < m; ++i) {       for (int j = 1; j <= n; ++j) {         if (dp[i][j] != -1) {           for (auto [v, w] : adj[j]) {             dp[i + 1][v] = max(dp[i + 1][v], dp[i][j] + w);           }         }       }     }     vector<pair<int, int>> lns;     for (int i = 1; i <= n; ++i) {       if (dp[m][i] == -1) continue;       int mx = 0;       for (auto [_, w] : adj[i]) {         mx = max(mx, w);       }       lns.emplace_back(mx, dp[m][i]);     }     sort(lns.begin(), lns.end());     vector<pair<int, int>> hull;     for (auto i : lns) {       if (!hull.empty() && hull.back().first == i.first) {         hull.pop_back();       }       while (hull.size() >= 2u &&              intersect(i, hull.back()) <= intersect(i, hull[hull.size() - 2u])) {         hull.pop_back();       }       hull.push_back(i);     }     int ans = 0;     for (int i = 1; i <= m; ++i) {       ans = add(ans, *max_element(dp[i], dp[i] + n + 1) % M);     }     int left = 1;     for (int i = 0; i < int(hull.size()); ++i) {       int right = q - m;       if (i + 1 < int(hull.size())) {         right = min(right, intersect(hull[i], hull[i + 1]));       }       if (right < left) continue;       int len = right - left + 1;       ans = add(ans, mul(hull[i].second % M, len));       ans = add(ans, mul(hull[i].first % M,                          mul(mul(len, add(right, left)), (M + 1) / 2)));       left = right + 1;     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return a * b / gcd(a, b); }   struct custom_hash {     static uint64_t splitmix64(uint64_t x) {       x += 0x9e3779b97f4a7c15;       x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;       x = (x ^ (x >> 27)) * 0x94d049bb133111eb;       return x ^ (x >> 31);     }     size_t operator()(uint64_t x) const {       static const uint64_t FIXED_RANDOM =           chrono::steady_clock::now().time_since_epoch().count();       return splitmix64(x + FIXED_RANDOM);     }   };   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());   inline int64_t random_long(long long l = LLONG_MIN, long long r = LLONG_MAX) {     uniform_int_distribution<int64_t> generator(l, r);     return generator(rng);   }   void __print(int x) { cerr << x; }   void __print(long x) { cerr << x; }   void __print(long long x) { cerr << x; }   void __print(unsigned x) { cerr << x; }   void __print(unsigned long x) { cerr << x; }   void __print(unsigned long long x) { cerr << x; }   void __print(float x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(long double x) { cerr << x; }   void __print(char x) { cerr <<      << x <<     ; }   void __print(const char *x) { cerr <<     << x <<    ; }   void __print(const string &x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }   template <typename T, typename V>   void __print(const pair<T, V> &x) {     cerr <<  { ;     __print(x.first);     cerr <<  , ;     __print(x.second);     cerr <<  } ;   }   template <typename T>   void __print(const T &x) {     int f = 0;     cerr <<  { ;     for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i);     cerr <<  } ;   }   void _print() { cerr <<  ] n ; }   template <typename T, typename... V>   void _print(T t, V... v) {     __print(t);     if (sizeof...(v)) cerr <<  ,  ;     _print(v...);   }   const long long inf = 1e18;   const int32_t M = 1e9 + 7;   const int32_t MM = 998244353;   bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); }   long long binpow(long long a, long long b, long long m) {     a %= m;     long long res = 1;     while (b > 0) {       if (b & 1) res = res * a % m;       a = a * a % m;       b >>= 1;     }     return res;   }   const int N = 2e6 + 1;   void solve() {     long long a, b;     cin >> a >> b;     long long target = a * b;     long long l = 0;     long long r = 1e6, mid, d;     bool k = false;     while (l <= r) {       mid = l + (r - l) / 2;       d = mid * mid * mid;       if (d == target) {         k = true;         break;       }       if (d < target) {         l = mid + 1;       } else {         r = mid - 1;       }     }     if (k == true && a % mid == 0 && b % mid == 0) {       cout <<  Yes             <<   n ;     } else {       cout <<  No             <<   n ;     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t = 1;     cin >> t;     while (t--) {       solve();     }     cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, k;   long long read() {     long long s = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       s = s * 10 + ch -  0 ;       ch = getchar();     }     return s * f;   }   inline void print(long long *f, long long len) {     for (long long i = 0; i < len; i++) printf( %lld  , f[i]);     puts(  );   }   long long a[101010];   long long pw[101010];   long long cnt[101010];   long long dp[101010];   const long long mod = 1e9 + 7;   signed main() {     n = read();     pw[0] = 1;     for (long long i = 1, iend = n; i <= iend; ++i)       a[i] = read(), pw[i] = (pw[i - 1] * 2) % mod, cnt[a[i]]++;     for (long long i = 100000, iend = 1; i >= iend; --i) {       long long sum = 0;       for (long long j = i; j <= 100000; j += i) sum += cnt[j];       dp[i] = pw[sum] - 1;       for (long long j = i * 2; j <= 100000; j += i)         dp[i] += mod, dp[i] -= dp[j], dp[i] %= mod;     }     cout << dp[1];   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   const long long inf = 1e18;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, b;     cin >> n >> b;     long long i = 2;     long long ans = inf;     while (i <= b) {       if (i * i > b) i = b;       long long cnt = 0;       while (b % i == 0) {         b /= i;         cnt++;       }       if (cnt == 0) {         i++;         continue;       }       long long tmp = 0;       long long j = 1;       while (j <= n / i) {         j *= i;         tmp += n / j;       }       ans = min(ans, tmp / cnt);       i++;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int ss = 1000;   int n, k, ans, x;   bool flag1, flag2;   bool a[5000][5000];   int b[1002], tot;   int main() {     scanf( %d%d , &n, &k);     memset(a, false, sizeof(a));     memset(b, false, sizeof(b));     flag1 = flag2 = false;     n += ss;     for (int i = 1; i <= k; ++i) {       scanf( %d , &x);       a[0][x + ss] = true;       if (x + ss > n) flag1 = true;       if (x + ss < n) flag2 = true;     }     tot = 0;     for (int i = 0; i <= 1000 + ss; ++i) {       if (a[0][i]) b[++tot] = i;     }     if (a[0][n])       ans = 1;     else if (flag1 ^ flag2 || (k == 1))       ans = -1;     else       for (int i = 1; i <= 5010; ++i) {         for (int j = 0; j <= 3000; ++j)           if (a[i - 1][j]) {             for (int k = 1; k <= tot; ++k) {               if (j + b[k] - n >= 0 && j + b[k] - n <= 3000) {                 a[i][j + b[k] - n] = true;               }             }           }         if (a[i][n]) {           ans = i + 1;           break;         }       }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int q;     cin >> q;     while (q--) {       long long n, a, b;       cin >> n >> a >> b;       if (n == 1) {         cout << a << endl;         continue;       }       if (n % 2 == 0) {         long long gu = min(n * a, (n / 2) * b);         cout << gu << endl;       } else {         long long gu = min(n * a, (n / 2) * b + a);         cout << gu << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     double i = n / 3.0;     int in = i;     if (i - in > 0.6) in += 1;     int f = in / 12;     cout << f <<     << in - f * 12 <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   int pwr(int a, long long b) {     int res = 1;     for (; b; b >>= 1) {       if (b & 1) res = 1LL * res * a % MOD;       a = 1LL * a * a % MOD;     }     return res;   }   int T, n;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     T = 1;     while (T--) {       cin >> n;       if (n == 1) return cout << 6 <<   n , 0;       long long x = (1LL << n);       x -= 2;       cout << 1LL * 6 * pwr(4, x) % MOD <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long t = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       c = getchar();     }     while (c >=  0  && c <=  9 ) t = t * 10 + c - 48, c = getchar();     return t * f;   }   char s[10000001];   long long len, hsh[2000001][11], bas = 10;   long long mo[4] = {1000000007, 1000000009, 998244353, 1000000037};   inline long long ksm1(long long x, long long y, long long m) {     long long sum = 1;     for (; y; y /= 2) {       if (y & 1) sum = sum * x % m;       x = x * x % m;     }     return sum;   }   inline long long ksm(long long x, long long y) {     long long sum = 1;     for (; y; y /= 2) {       if (y & 1) sum = sum * x;       x = x * x;     }     return sum;   }   inline long long get(int l, int r, int k) {     long long t = 0, ret = 0;     if (k == 10)       t = ksm(bas, r - l + 1), ret = hsh[r][10] - t * hsh[l - 1][10];     else       t = ksm1(bas, r - l + 1, mo[k]),       ret = ((hsh[r][k] - t * hsh[l - 1][k]) % mo[k] + mo[k]) % mo[k];     return ret;   }   int a1[2000001], a2[2000001], a3[2000001], a4[2000001], len1, len2, len3;   bool flag = 0;   inline void solve(int x) {     int rest = len - x;     for (long long l = rest / 2; l <= rest / 2 + 1; ++l) {       if ((get(1, x, 10) + get(x + 1, len - l, 10)) ==           get(len - l + 1, len, 10)) {         flag = 1;         for (long long i = 0; i <= 3; ++i)           if (((get(1, x, i) + get(x + 1, len - l, i)) % mo[i] + mo[i]) % mo[i] !=               get(len - l + 1, len, i)) {             flag = 0;             break;           }         if (!flag) continue;         if (s[1] ==  0  && x != 1) continue;         if (s[x + 1] ==  0  && len - l != x + 1) continue;         if (s[len - l + 1] ==  0  && len != len - l + 1) continue;         for (long long i = 1; i <= x; ++i) putchar(s[i]);         putchar( + );         for (long long i = x + 1; i <= len - l; ++i) putchar(s[i]);         putchar( = );         for (long long i = len - l + 1; i <= len; ++i) putchar(s[i]);         exit(0);       }     }     for (long long l = x; l <= x + 1; ++l) {       if ((get(1, x, 10) + get(x + 1, len - l, 10)) ==           get(len - l + 1, len, 10)) {         flag = 1;         for (long long i = 0; i <= 3; ++i)           if (((get(1, x, i) + get(x + 1, len - l, i)) % mo[i] + mo[i]) % mo[i] !=               get(len - l + 1, len, i)) {             flag = 0;             break;           }         if (!flag) continue;         if (s[1] ==  0  && x != 1) continue;         if (s[x + 1] ==  0  && len - l != x + 1) continue;         if (s[len - l + 1] ==  0  && len != len - l + 1) continue;         for (long long i = 1; i <= x; ++i) putchar(s[i]);         putchar( + );         for (long long i = x + 1; i <= len - l; ++i) putchar(s[i]);         putchar( = );         for (long long i = len - l + 1; i <= len; ++i) putchar(s[i]);         exit(0);       }     }   }   int main() {     scanf( %s , s + 1);     len = strlen(s + 1);     hsh[0][4] = 0;     for (long long i = 1; i <= len; ++i)       hsh[i][10] = hsh[i - 1][10] * bas + s[i] - 48;     for (long long i = 1; i <= len; ++i)       for (long long j = 0; j <= 3; ++j)         hsh[i][j] = (hsh[i - 1][j] * bas + s[i] - 48) % mo[j];     for (long long i = 1; i <= len / 2 + 1; ++i) solve(i);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s, c;     int n, t = 0;     cin >> n;     cin >> s >> c;     for (int i = 0; i < n; i++) {       int p = s[i], k = c[i];       if (p <= k)         t += min(abs(p - k), (p + 1) + (9 - k));       else         t += min((9 - p) + (k + 1), abs(p - k));     }     cout << t << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_BUF_SIZE = 16384;   char BUFOR[MAX_BUF_SIZE];   int BUF_SIZE, BUF_POS;   char ZZZ;   const int MXN = 100010;   const int C = 262144;   const int INF = 1000000001;   string a[MXN];   int aa;   int typ[MXN], gender[MXN];   string fem[3] = { liala ,  etra ,  inites };   string masc[3] = { lios ,  etr ,  initis };   string mowa[6] = { lios ,  etr ,  initis ,  liala ,  etra ,  inites };   int n;   void test() {     string tmp;     while (cin >> tmp) a[aa++] = tmp;     n = aa;     bool czy = 1;     for (int i = 0; i < aa; i++) {       int dec = -1;       gender[i] = -1;       for (int j = 0; j < 3; j++) {         string w = masc[j];         if (w.length() <= a[i].length() &&             w == string(a[i], (int)(a[i]).size() - (int)(w).size(),                         (int)(w).size()))           dec = 1;         gender[i] = dec;       }       dec = -1;       for (int j = 0; j < 3; j++) {         string w = fem[j];         if (w.length() <= a[i].length() &&             w == string(a[i], (int)(a[i]).size() - (int)(w).size(),                         (int)(w).size()))           dec = 0;         if (gender[i] == -1) gender[i] = dec;       }       for (int j = 0; j < 6; j++) {         string w = mowa[j];         typ[i] = -1;         if (w.length() <= a[i].length() &&             w == string(a[i], (int)(a[i]).size() - (int)(w).size(),                         (int)(w).size())) {           typ[i] = j % 3;           break;         }       }       if (gender[i] == -1 || typ[i] == -1) czy = 0;     }     int iter = 0;     while (iter < n && typ[iter] == 0) iter++;     if (typ[iter] == 1)       iter++;     else if (n > 1)       czy = 0;     while (czy && iter < n)       if (typ[iter++] != 2) czy = 0;     for (int i = 1; i < n; i++)       if (gender[0] != gender[i]) czy = 0;     if (czy)       printf( YES n );     else       printf( NO n );   }   int main() {     int te = 1;     while (te--) test();   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int N = 1e5;   class union_find {     int n, no_of_comp;     vector<int> par, sz;       public:     union_find(int nv) {       n = no_of_comp = nv;       par = sz = vector<int>(n + 1, 1);       for (int i = 1; i <= n; i++) par[i] = i;     }     int get_root(int i) {       while (i != par[i]) i = par[i];       return i;     }     void take_union(int i, int j) {       int u = get_root(i);       int v = get_root(j);       if (u == v) return;       if (sz[u] < sz[v]) swap(u, v);       par[v] = u;       sz[u] += sz[v];       no_of_comp--;     }     int num_of_comp() { return no_of_comp; }   };   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     union_find dsu(n);     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       dsu.take_union(u, v);     }     cout << (long long)pow(2, n - dsu.num_of_comp());   }


#include <bits/stdc++.h>   int main() {     int n;     scanf( %d , &n);     int s = 0;     s = n * (n + 1) / 2;     if (s % 2 == 0) {       printf( 0 );     } else {       printf( 1 );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> com;   vector<long long> all;   long long c, a, n, k, mm;   void solve() {     long long s = 0, e = (long long)1e9 * 2 + 5;     long long mid = 0;     long long sum = 0;     while (s <= e) {       mid = (s + e) / 2;       for (int i = 0; i < n; i++) {         sum += max((long long)0, (com[i] * mid - all[i]));         if (sum > (long long)1e18) {           sum = k + 1;           break;         }       }       if (sum <= k) {         mm = mid;         s = mid + 1;       } else         e = mid - 1;       sum = 0;     }     cout << mm;   }   int main() {     cin >> n >> k;     for (int i = 0; i < n; i++) {       cin >> c;       com.push_back(c);     }     for (int i = 0; i < n; i++) {       cin >> a;       all.push_back(a);     }     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline bool equal(double _x, double _y) {     return _x > _y - 1e-9 && _x < _y + 1e-9;   }   int l[105], r[105], t[105], c[105], ans, mx, id;   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) scanf( %d%d%d%d , &l[i], &r[i], &t[i], &c[i]);     for (int j = 1; j <= n; j++) {       mx = 1e9;       id = -1;       for (int i = 0; i < m; i++) {         if (l[i] <= j && j <= r[i] && t[i] < mx) {           mx = t[i];           id = i;         }       }       if (~id) ans += c[id];     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   template <typename Arg1, typename Arg2>   ostream& operator<<(ostream& out, const pair<Arg1, Arg2>& x) {     return out <<  (  << x.first <<  ,  << x.second <<  ) ;   }   template <typename Arg1>   ostream& operator<<(ostream& out, const vector<Arg1>& a) {     out <<  [ ;     for (const auto& x : a) out << x <<  , ;     return out <<  ] ;   }   template <typename Arg1>   ostream& operator<<(ostream& out, const set<Arg1>& a) {     out <<  [ ;     for (const auto& x : a) out << x <<  , ;     return out <<  ] ;   }   template <typename Arg1, typename Arg2>   ostream& operator<<(ostream& out, const map<Arg1, Arg2>& a) {     out <<  [ ;     for (const auto& x : a) out << x <<  , ;     return out <<  ] ;   }   template <typename Arg1>   void __trace__(const string name, Arg1&& arg1) {     cerr << name <<   :   << arg1 <<   ]   << endl;   }   template <typename Arg1, typename... Args>   void __trace__(const string names, Arg1&& arg1, Args&&... args) {     const string name = names.substr(0, names.find( , ));     cerr << name <<   :   << arg1 <<   |  ;     __trace__(names.substr(1 + (int)name.size()), args...);   }   clock_t time_p = clock();   void time_taken() {     time_p = clock() - time_p;     cerr <<  Time Taken :   << (float)(time_p) / CLOCKS_PER_SEC <<   n ;   }   const long long mod = 1e9 + 7;   const long long INF = 1e18;   const int maxn = 2e5 + 5;   vector<int> adj[maxn];   int Q[maxn];   int n;   void bfs(int s, int* d) {     for (int i = 1; i <= n; i++) {       d[i] = -1;     }     d[s] = 0;     int N = 0;     Q[N++] = s;     for (int i = 0; i < N; i++) {       int u = Q[i];       for (int v : adj[u]) {         if (d[v] == -1) {           d[v] = d[u] + 1;           Q[N++] = v;         }       }     }   }   void solve() {     int m, a, b, c;     cin >> n >> m >> a >> b >> c;     for (int i = 1; i <= n; i++) {       adj[i].clear();     }     vector<long long> p(m);     for (int i = 0; i < m; i++) cin >> p[i];     sort(p.begin(), p.end());     for (int i = 1; i <= m - 1; i++) p[i] += p[i - 1];     for (int _ = 0; _ < m; _++) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     int da[n + 1], db[n + 1], dc[n + 1];     bfs(a, da);     bfs(b, db);     bfs(c, dc);     if (a == b) {       int foo = dc[a];       if (foo) {         cout << p[foo - 1] <<   n ;       } else {         cout << 0 <<   n ;       }       return;     }     if (b == c) {       int foo = da[b];       if (foo) {         cout << p[foo - 1] <<   n ;       } else {         cout << 0 <<   n ;       }       return;     }     if (c == a) {       int foo = da[b];       if (foo) {         cout << 2 * p[foo - 1] <<   n ;       } else {         cout << 0 <<   n ;       }       return;     }     long long ans = INF;     for (int i = 1; i <= n; i++) {       long long cur = 0;       int cnt = 0;       long long foo = 0;       if (db[i]) {         foo = p[db[i] - 1];         cur += 2 * p[db[i] - 1];       }       int tot = da[i] + db[i] + dc[i] - 1;       if (tot < m)         cur += p[tot] - foo;       else         continue;       ans = min(ans, cur);     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cout.precision(12);     cout << fixed;     int t;     cin >> t;     while (t--) {       solve();     }     time_taken();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const int mod = (int)1e+9 + 7;   const double pi = acos(-1.);   const int maxn = 100100;   int x[maxn];   int z[10 * maxn];   int a, b;   void fill(int u) {     int lpos = (u - b % u) % u;     for (; lpos <= a - b; lpos += u) {       z[lpos] = max(z[lpos], lpos + u - 1);     }   }   int main() {     srand(time(0));     const string file =   ;     if (!file.empty()) {       freopen((file +  .in ).c_str(),  r , stdin);       freopen((file +  .out ).c_str(),  w , stdout);     }     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &x[i]);     }     scanf( %d%d , &a, &b);     sort(&x[0], &x[n]);     n = unique(&x[0], &x[n]) - &x[0];     for (int i = 0; i < 10 * maxn; i++) {       z[i] = i + 1;     }     for (int i = 0; i < n; i++) {       fill(x[i]);     }     for (int i = 1; i <= a - b; i++) {       z[i] = max(z[i], z[i - 1]);     }     int cnt = 0;     for (int i = 0; i < a - b;) {       i = z[i];       cnt++;     }     cout << cnt;     return (0);   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000000;   const int MOD = 1000000007;   const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};   const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};   int n, m;   char grid[65][65];   bool row(int row, char c) {     for (int i = 0; i < m; i++)       if (grid[row][i] != c) return false;     return true;   }   bool col(int col, char c) {     for (int i = 0; i < n; i++)       if (grid[i][col] != c) return false;     return true;   }   void solve() {     cin >> n >> m;     int a = 0, b = 0;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> grid[i][j];         if (grid[i][j] ==  A )           ++a;         else           ++b;       }     if (a == 0) {       cout <<  MORTAL             <<   n ;       return;     }     if (b == 0) {       cout << 0 <<   n ;       return;     }     if (row(0,  A ) || row(n - 1,  A ) || col(0,  A ) || col(m - 1,  A )) {       cout << 1 <<   n ;       return;     }     for (int i = 0; i < n; i++)       if (row(i,  A )) {         cout << 2 <<   n ;         return;       }     for (int i = 0; i < m; i++)       if (col(i,  A )) {         cout << 2 <<   n ;         return;       }     if (grid[0][0] ==  A  || grid[n - 1][0] ==  A  || grid[0][m - 1] ==  A  ||         grid[n - 1][m - 1] ==  A ) {       cout << 2 <<   n ;       return;     }     for (int i = 0; i < n; i++) {       if (grid[i][0] ==  A  || grid[i][m - 1] ==  A ) {         cout << 3 <<   n ;         return;       }     }     for (int j = 0; j < m; j++) {       if (grid[0][j] ==  A  || grid[n - 1][j] ==  A ) {         cout << 3 <<   n ;         return;       }     }     cout << 4 <<   n ;   }   signed main() {     cin.tie(0), cout.tie(0);     ios::sync_with_stdio(0);     int q;     cin >> q;     while (q--) solve();     return 0;   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     string s, t;     cin >> s >> t;     int n = s.size(), m = t.size();     vector<vector<int> > f(n + 1);     vector<vector<int> > what(n + 1);     vector<vector<pair<int, int> > > from(n + 1);     for (int i = 0; i < int((f).size()); i++)       f[i].resize(m + 1), what[i].resize(m + 1), from[i].resize(m + 1);     for (int i = 0; i <= n; ++i)       for (int j = 0; j <= m; ++j) {         if (i == 0 && j == 0) continue;         int a, b, c, d;         a = b = c = d = 1000000000;         if (i - 1 >= 0 && j - 1 >= 0 && s[i - 1] == t[j - 1]) a = f[i - 1][j - 1];         if (i - 1 >= 0) b = f[i - 1][j] + 1;         if (j - 1 >= 0) c = f[i][j - 1] + 1;         if (i - 1 >= 0 && j - 1 >= 0) d = f[i - 1][j - 1] + 1;         int mn = min(min(a, b), min(c, d));         if (mn == b)           what[i][j] = 1, from[i][j] = make_pair(i - 1, j);         else if (mn == c)           what[i][j] = 2, from[i][j] = make_pair(i, j - 1);         else if (mn == d)           what[i][j] = 3, from[i][j] = make_pair(i - 1, j - 1);         else           from[i][j] = make_pair(i - 1, j - 1);         f[i][j] = mn;       }     cout << f[n][m] << endl;     int i = n, j = m;     while (i != 0 || j != 0) {       if (what[i][j] == 1)         cout <<  DELETE   << i << endl;       else if (what[i][j] == 2)         cout <<  INSERT   << i + 1 <<     << t[j - 1] << endl;       else if (what[i][j] == 3)         cout <<  REPLACE   << i <<     << t[j - 1] << endl;       pair<int, int> h = from[i][j];       i = h.first;       j = h.second;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int m = pow(10, 9) + 7;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     string s, t;     cin >> s >> t;     int j = 0, l = -1, r = -1;     for (int i = 0; i < t.length(); i++) {       if (s[j] == t[i]) j++;       if (j == s.length()) {         l = i + 1;         break;       }     }     reverse(s.begin(), s.end());     j = 0;     reverse(t.begin(), t.end());     for (int i = 0; i < t.length(); i++) {       if (s[j] == t[i]) j++;       if (j == s.length()) {         r = t.length() - i;         break;       }     }     if (l >= r || l == -1 || r == -1)       cout << 0 << endl;     else       cout << r - l << endl;     return 0;   }


#include <bits/stdc++.h>   bool local = false;   using namespace std;   template <class T>   string toString(T x) {     ostringstream sout;     sout << x;     return sout.str();   }   template <class T>   void print(T v) {     cout << ((int)(v).size()) <<   n ;     for (auto x : v) cout << x <<    ;     cout <<   n ;   };   template <class T>   void print1(T v) {     cout << ((int)(v).size()) <<   n ;     for (auto x : v) cout << x + 1 <<    ;     cout <<   n ;   };   template <class T1, class T2>   ostream &operator<<(ostream &o, pair<T1, T2> x) {     return o << x.first <<     << x.second;   }   template <class T1, class T2>   istream &operator>>(istream &o, pair<T1, T2> &x) {     return o >> x.first >> x.second;   }   template <class T1, class T2>   pair<T1, T2> operator+(pair<T1, T2> a, pair<T1, T2> b) {     a.first += b.first;     a.second += b.second;     return a;   }   template <class T1, class T2>   pair<T1, T2> operator-(pair<T1, T2> a, pair<T1, T2> b) {     a.first -= b.first;     a.second -= b.second;     return a;   }   template <class T1, class T2>   void operator+=(pair<T1, T2> &a, pair<T1, T2> b) {     a.first += b.first;     a.second += b.second;   }   template <class T1, class T2>   void operator-=(pair<T1, T2> &a, pair<T1, T2> b) {     a.first -= b.first;     a.second -= b.second;   }   int nint() {     int x;     cin >> x;     return x;   }   double getTime() { return clock() / (double)CLOCKS_PER_SEC; };   mt19937 rnd(0);   int rand(int n) { return rnd() % n; }   int rand(int l, int r) { return rnd() % (r - l + 1) + l; }   const int mod = 1000000000 + 7;   void initIO() {     if (local) {       freopen( input.txt ,  r , stdin);       srand((int)time(0));       rnd.seed((int)time(0));     } else {     }     {       ios::sync_with_stdio(0);       cin.tie(0);       cout.tie(0);     };   }   void solve();   int main() {     initIO();     int tc = 1;     for (int tt = 0; tt < (tc); tt++) {       solve();     }     if (local)       cout <<   n             <<  time =   << getTime() <<   n ;   }   int n;   int m;   vector<int> v1, v2;   pair<int, int> val[300500];   bool f1(int a, int b) { return val[a].second > val[b].second; }   bool f2(int a, int b) { return val[a].second < val[b].second; }   void solve() {     cin >> n;     for (int i = 0; i < (n); i++) {       pair<int, int> o;       cin >> o;       val[i] = o;       if (o.first > o.second)         v2.emplace_back(i);       else         v1.emplace_back(i);     }     sort(v1.begin(), v1.end(), f1);     sort(v2.begin(), v2.end(), f2);     if (((int)(v1).size()) > ((int)(v2).size()))       print1(v1);     else       print1(v2);   }