#include <bits/stdc++.h>   using namespace std;   bool vis[500005];   int low[500005], d[500005], par[500005], rnk[500005], lev[500005], timer;   vector<int> adj[500005], tree[500005];   vector<pair<int, int>> edges;   map<int, map<int, int>> ed, br;   int get(int x) { return (par[x] == x ? x : par[x] = get(par[x])); }   void link(int x, int y) {     x = get(x);     y = get(y);     if (x == y) return;     par[x] = y;   }   void bridge_find(int u, int par) {     timer++;     d[u] = low[u] = timer;     for (auto x : adj[u]) {       if (x == par) continue;       if (d[x] > 0) {         low[u] = min(low[u], d[x]);         if (ed[x][u] == 0) ed[u][x] = 1;       } else {         bridge_find(x, u);         low[u] = min(low[u], low[x]);         if (d[u] < low[x])           br[u][x] = 1, br[x][u] = 1;         else {           ed[u][x] = 1;           link(u, x);         }       }     }   }   void dfs(int child, int par) {     vis[child] = 1;     for (auto x : adj[child]) {       if (x == par) continue;       if (br[x][child]) ed[x][child] = 1;       if (!vis[x]) dfs(x, child);     }   }   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) rnk[i] = 1, par[i] = i;     for (int i = 0; i < m; i++) {       int x, y;       scanf( %d %d , &x, &y);       edges.push_back({x, y});       adj[x].push_back(y);       adj[y].push_back(x);     }     bridge_find(1, -1);     int mx = 0, cur;     for (int i = 1; i <= n; i++) {       int x;       x = get(i);       lev[x]++;       if (mx < lev[x]) {         mx = lev[x], cur = x;       }     }     dfs(cur, -1);     cout << mx << endl;     for (int i = 0; i < m; i++) {       if (ed[edges[i].first][edges[i].second])         cout << edges[i].first <<     << edges[i].second << endl;       else if (ed[edges[i].second][edges[i].first])         cout << edges[i].second <<     << edges[i].first << endl;     }   }


#include <bits/stdc++.h>   const long double eps = 1e-9;   const long double pi = fabsl(atan2(0.0, -1.0));   const int INF = 0x7f7f7f7f;   const long long LINF = 1ll << 60;   const long double LDINF = 1e42;   using namespace std;   int n, k;   string s;   int a[110];   int res[110];   int p[110];   void Load() {     cin >> n >> k;     cin >> s;     p[1] = 0;     int i, k = 0;     for (i = 2; i <= s.size(); i++) {       while (k > 0 && s[k] != s[i - 1]) k = p[k];       if (s[k] == s[i - 1]) k++;       p[i] = k;     }     {       int c = 0;       while ((c = getchar()) != 10 && c != EOF)         ;     };     for (int i = 0; i < n - s.size() + 1; i++) {       a[i] = getchar() -  0 ;     }   }   void Solve() {     int k = 0, i, j, t;     for (i = 0; i < n; i++) {       if (a[i] == 1) {         if (res[i] != 0) {           for (j = 0; j < s.size(); j++) {             if (res[i + j] != 0 && res[i + j] != s[j]) {               cout <<  No solution ;               return;             } else {               res[i + j] = s[j];             }           }         } else {           for (j = 0; j < s.size(); j++) {             res[i + j] = s[j];           }         }       }       if (res[i] == 0) {         int rr = 110, rch;         for (j =  a ; j <=  a  + ::k - 1; j++) {           t = k;           while (t > 0 && s[t] != j) t = p[t];           if (s[t] == j) t++;           if (rr > t) {             rr = t, rch = j;           }         }         cerr << rr <<     << (char)rch;         res[i] = rch;         k = rr;         if (k == s.size() && a[i - s.size() + 1] == 0) {           cout <<  No solution ;           return;         }       } else {         t = k;         while (t > 0 && s[t] != res[i]) t = p[t];         if (s[t] == res[i]) t++;         if (t == s.size() && a[i - s.size() + 1] == 0) {           cout <<  No solution ;           return;         }         k = t;       }     }     for (i = 0; i < n; i++) {       putchar(res[i]);     }   }   int main() {     Load();     Solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 2 * 300000 + 1000;   int block[maxN], start[maxN], fin[maxN];   int main() {     ios::sync_with_stdio(0);     ;     int n, m;     cin >> n >> m;     string s;     cin >> s;     set<int> st;     st.insert(-1);     st.insert(n);     for (int i = 0; i < n; i++) {       if (s[i] !=  . ) st.insert(i);     }     int f = 0;     int cur = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  . )         cur++;       else {         if (cur) f += cur - 1;         cur = 0;       }     }     if (cur) f += cur - 1;     while (m--) {       int pos;       char c;       cin >> pos >> c;       pos--;       if (c ==  . ) {         auto left = st.lower_bound(pos);         if (*left != pos) {           cout << f <<   n ;           continue;         }         left--;         int l = *left;         int r = *st.upper_bound(pos);         int sub = r - pos - 2;         if (sub > 0) f -= sub;         sub = pos - l - 2;         if (sub > 0) f -= sub;         st.erase(pos);         sub = r - l - 2;         if (sub > 0) f += sub;       } else {         auto left = st.lower_bound(pos);         if (*left == pos) {           cout << f <<   n ;           continue;         }         left--;         int l = *left;         int r = *st.lower_bound(pos);         int sub = r - l - 2;         f -= sub;         sub = pos - l - 2;         if (sub > 0) f += sub;         sub = r - pos - 2;         if (sub > 0) f += sub;         st.insert(pos);       }       cout << f <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n;   struct pt {     int ai;     int wz;   };   int cmp(pt a, pt b) { return a.ai > b.ai; }   pt cun[1005];   int ans;   int ans1[1005];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &cun[i].ai);       cun[i].wz = i;     }     sort(cun + 1, cun + 1 + n, cmp);     for (int i = 1; i <= n; i++) {       ans += cun[i].ai * (i - 1) + 1;     }     printf( %d n , ans);     for (int i = 1; i <= n; i++) {       printf( %d  , cun[i].wz);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, sum1, sum2;   string a;   int f[300011];   void solve() {     scanf( %d%d , &n, &k);     cin >> a;     memset(f, -1, sizeof(f));     for (int i = 0; i < n; i++) {       if (a[i] !=  ? ) {         if (f[i % k] == -1)           f[i % k] = a[i] -  0 ;         else if (f[i % k] != (int)(a[i] -  0 )) {           cout <<  NO  << endl;           return;         }       }     }     sum1 = sum2 = 0;     for (int i = 0; i < k; i++) {       sum1 += (f[i] == 0);       sum2 += (f[i] == 1);     }     if (sum1 > k / 2 || sum2 > k / 2)       cout <<  NO  << endl;     else       cout <<  YES  << endl;   }   int main() {     int T;     scanf( %d , &T);     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct tree {     int maxv[650001];     void set(int x, int d, int o, int l, int r) {       if (l == r) {         maxv[o] = d;         return;       }       int m = (l + r) / 2;       if (x <= m)         set(x, d, o * 2, l, m);       else         set(x, d, o * 2 + 1, m + 1, r);       maxv[o] = max(maxv[o * 2], maxv[o * 2 + 1]);     }     int query(int x, int y, int o, int l, int r) {       if (x <= l && r <= y) return maxv[o];       int m = (l + r) / 2, ans = 0;       if (x <= m) ans = max(ans, query(x, y, o * 2, l, m));       if (y > m) ans = max(ans, query(x, y, o * 2 + 1, m + 1, r));       return ans;     }   } Tx, Ty;   int n, m;   map<int, int> h, pos;   void modify(int x, int y, int val) {     Tx.set(x, val, 1, 1, n);     Ty.set(y, val, 1, 1, m + 20);   }   void remove_all(map<int, int>::iterator del) {     modify(del->first, del->second, 0);     pos.erase(pos.lower_bound(del->second));     h.erase(del);   }   int main() {     scanf( %d%d , &n, &m);     for (int T = 1; T <= m; T++) {       int op, p, v;       scanf( %d%d , &op, &p);       if (op == 1) {         scanf( %d , &v);         v += m - T;         h[p] = v;         pos[v] = p;         auto it = pos.lower_bound(v), it2 = it;         for (;; it2--) {           modify(it2->second, it2->first, 0);           if (it2 == pos.begin()) break;         }         for (;; it--) {           int x = it->second, y = it->first, val = Tx.query(x, n, 1, 1, n) + 1;           modify(x, y, val);           if (it == pos.begin()) break;         }       } else {         auto del = h.begin();         for (int i = 1; i < p; i++) del++;         if (del == h.begin())           remove_all(del);         else {           auto it = del, it2 = --it;           remove_all(del);           for (;; it2--) {             modify(it2->first, it2->second, 0);             if (it2 == h.begin()) break;           }           for (;; it--) {             int x = it->first, y = it->second,                 val = Ty.query(y + 1, m + 20, 1, 1, m + 20) + 1;             modify(x, y, val);             if (it == h.begin()) break;           }         }       }       printf( %d n , Tx.query(1, n, 1, 1, n));     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, d = 0;     long long x, amount;     char sign;     int quantity;     cin >> n >> amount;     for (int i = 0; i < n; i++) {       cin >> sign >> quantity;       if (sign ==  + )         amount += quantity;       else if (amount < quantity)         d++;       else         amount -= quantity;     }     cout << amount <<     << d;   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline bool cmin(T& a, const T& b) {     return a > b ? a = b, 1 : 0;   }   template <typename T>   inline bool cmax(T& a, const T& b) {     return a < b ? a = b, 1 : 0;   }   int inp() {     int x = 0, f = 1;     char ch;     for (ch = getchar(); !isdigit(ch); ch = getchar())       if (ch ==  - ) f = -1;     for (; isdigit(ch); x = x * 10 + ch -  0 , ch = getchar())       ;     return x * f;   }   struct comp {     double r, i;     inline comp() {}     inline comp(double a, double b) : r(a), i(b) {}   };   const int N = (1 << 19), MaxN = (N << 1) + 10;   const int K = 32767, L = 15;   int P;   inline comp add(comp a, comp b) { return comp(a.r + b.r, a.i + b.i); }   inline comp sub(comp a, comp b) { return comp(a.r - b.r, a.i - b.i); }   inline comp mul(comp a, comp b) {     return comp(a.r * b.r - a.i * b.i, a.r * b.i + a.i * b.r);   }   inline comp div(comp a, double b) { return comp(a.r / b, a.i / b); }   inline comp conj(comp x) { return comp(x.r, -x.i); }   inline int add(int x, int y) {     return x + (long long)y >= P ? x + (long long)y - P : x + y;   }   inline int sub(int x, int y) { return x >= y ? x - y : x - (long long)y + P; }   inline int mul(int x, int y) { return (long long)x * y % P; }   int exp(int x, long long n) {     int res = 1;     for (; n; n >>= 1) {       if (n & 1) res = mul(res, x);       x = mul(x, x);     }     return res;   }   comp eps[MaxN];   void init() {     double pi = acos(-1);     for (int i = 0; i < N; i++)       eps[i] = comp(cos(2 * pi * i / N), sin(2 * pi * i / N));   }   inline void trans(int n, comp x[], comp w[]) {     for (int i = 0, j = 0; i != n; i++) {       if (i < j) std::swap(x[i], x[j]);       for (int l = n >> 1; (j ^= l) < l; l >>= 1)         ;     }     for (int i = 2; i <= n; i <<= 1) {       int l = i >> 1, d = N / i;       for (int j = 0; j != n; j += i)         for (int k = 0; k != l; k++) {           comp t = mul(x[j + k + l], w[d * k]);           x[j + k + l] = sub(x[j + k], t);           x[j + k] = add(x[j + k], t);         }     }   }   inline void dft(int n, comp x[]) { trans(n, x, eps); }   inline void idft(int n, comp x[]) {     trans(n, x, eps);     reverse(x + 1, x + n);     for (int i = 0; i < n; i++) x[i] = div(x[i], n);   }   void conv(int na, int a[], int nb, int b[], int nc, int c[], int same = 0) {     int n, i;     static comp x[MaxN], y[MaxN], z[MaxN], w[MaxN];     for (n = 1; n < na + nb - 1; n <<= 1)       ;     for (i = 0; i < n; i++) {       x[i] = i < na ? comp(a[i] & K, a[i] >> L) : comp(0, 0);       y[i] = i < nb ? comp(b[i] & K, b[i] >> L) : comp(0, 0);     }     dft(n, x);     if (!same)       dft(n, y);     else       for (i = 0; i < n; i++) y[i] = x[i];     comp r0(0.5, 0), r1(0, -0.5), r(0, 1);     for (i = 0; i < n; i++) {       int j = (n - i) & (n - 1);       comp x0, x1, y0, y1;       x0 = mul(add(x[i], conj(x[j])), r0);       x1 = mul(sub(x[i], conj(x[j])), r1);       y0 = mul(add(y[i], conj(y[j])), r0);       y1 = mul(sub(y[i], conj(y[j])), r1);       z[i] = mul(x0, add(y0, mul(y1, r)));       w[i] = mul(x1, add(y0, mul(y1, r)));     }     idft(n, z);     idft(n, w);     for (i = 0; i < nc; i++) {       int c00 = (long long)(z[i].r + 0.5) % P,           c01 = (long long)(z[i].i + 0.5) % P;       int c10 = (long long)(w[i].r + 0.5) % P,           c11 = (long long)(w[i].i + 0.5) % P;       c[i] = ((((long long)c11 << L) + c01 + c10 << L) + c00) % P;     }   }   int nL;   int n, l, r, x[MaxN], y[MaxN];   void pow(int x[], int y[], int n) {     y[0] = 1;     int sN = 3;     nL = 1;     for (; n; n >>= 1) {       if (n & 1)         conv(sN, x, nL, y, min(nL + sN, 2 * ::n + 3), y),             nL = min(nL + sN, 2 * ::n + 3);       conv(sN, x, sN, x, min(sN << 1, 2 * ::n + 3), x, 1);       sN = min(sN << 1, 2 * ::n + 3);     }   }   int res(int x) { return y[x + n]; }   int main() {     int ans;     init();     cin >> n >> P >> l >> r;     x[0] = x[1] = x[2] = 1;     pow(x, y, n);     ans = ((long long)res(l) + res(l + 1) - res(r + 1) - res(r + 2)) % P;     cout << ((long long)ans + P) % P << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = (long long)1000000007;   int M, K;   int par[100000 + 5];   vector<int> adj[100000 + 5];   long long dp[100000 + 5][11][3];   struct bin_tree_type {     int le;     int ri;   };   bin_tree_type tree[100000 + 5];   long long exp(long long ret, int pow) {     if (pow == 0) {       return 1;     }     long long sav = 1;     while (pow ^ 1) {       if (pow & 1) {         sav *= ret;         sav %= mod;       }       pow >>= 1;       ret *= ret;       ret %= mod;     }     ret *= sav;     ret %= mod;     return ret;   }   long long inv(long long inp) { return exp(inp, mod - 2); }   void dfs_lcrs(int nod, int pt) {     int lastchild = -1;     par[nod] = pt;     for (int ke : adj[nod]) {       if (ke == pt) {         continue;       }       if (lastchild == -1) {         tree[nod].le = ke;       } else {         tree[lastchild].ri = ke;       }       dfs_lcrs(ke, nod);       lastchild = ke;     }   }   long long DP(int nod, int rem, int status) {     if (nod == 0) {       if (rem == 0) {         return 1;       } else {         return 0;       }     }     long long &ret = dp[nod][rem][status];     if (ret >= 0) {       return ret;     }     ret = 0;     if (rem && status != 2) {       if (status == 1) {         long long sumc = 0;         for (int i = 0; i <= rem - 1; i++) {           sumc += DP(tree[nod].le, i, 2) * DP(tree[nod].ri, rem - 1 - i, 1);           sumc %= mod;         }         ret += sumc;       } else {         long long mult;         if (nod != 1) {           mult = ((long long)(K - 1) * inv(M - 1)) % mod;         } else {           mult = 1;         }         long long sumc = 0;         for (int i = 0; i <= rem - 1; i++) {           sumc += DP(tree[nod].le, i, 2) * DP(tree[nod].ri, rem - 1 - i, 1);           sumc %= mod;         }         sumc *= mult;         sumc %= mod;         ret += sumc;       }     }     if (status == 0) {       long long mult = M - 1;       long long sumc = 0;       for (int i = 0; i <= rem; i++) {         sumc += DP(tree[nod].le, i, 0) * DP(tree[nod].ri, rem - i, 0);         sumc %= mod;       }       sumc *= mult;       sumc %= mod;       ret += sumc;     } else if (status == 1) {       long long mult = M - 1;       long long sumc = 0;       for (int i = 0; i <= rem; i++) {         sumc += DP(tree[nod].le, i, 0) * DP(tree[nod].ri, rem - i, 1);         sumc %= mod;       }       sumc *= mult;       sumc %= mod;       ret += sumc;     } else if (status == 2) {       long long mult = K - 1;       long long sumc = 0;       for (int i = 0; i <= rem; i++) {         sumc += DP(tree[nod].le, i, 1) * DP(tree[nod].ri, rem - i, 2);         sumc %= mod;       }       sumc *= mult;       sumc %= mod;       ret += sumc;     }     return ret;   }   int main() {     memset(dp, -1, sizeof(dp));     int N, a, b, X;     long long has = 0;     scanf( %d %d , &N, &M);     int edge = N - 1;     while (edge--) {       scanf( %d %d , &a, &b);       adj[a].push_back(b);       adj[b].push_back(a);     }     scanf( %d %d , &K, &X);     if (K == 1 && N > 1) {       printf( %lld n , exp(M - 1, N));       return 0;     }     dfs_lcrs(1, 1);     for (int i = X; i >= 0; i--) {       has += DP(1, i, 0);       has %= mod;     }     printf( %lld n , has);   }


#include <bits/stdc++.h>   using namespace std;   const int dir[8][2] = {{1, 0},  {0, 1},  {1, 1},   {1, -1},                          {-1, 0}, {0, -1}, {-1, -1}, {-1, 1}};   int n, m, cnt[1000 + 5][1000 + 5];   char a[1000 + 5][1000 + 5];   void work(int x, int y) {     bool flag = true;     for (int i = 0; i < 8; i++) {       int nx = x + dir[i][0], ny = y + dir[i][1];       if (a[nx][ny] ==  . ) flag = false;     }     if (flag)       for (int i = 0; i < 8; i++) {         int nx = x + dir[i][0], ny = y + dir[i][1];         cnt[nx][ny]++;       }   }   bool judge() {     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (a[i][j] ==  #  && cnt[i][j] == 0) return false;     return true;   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) cin >> a[i][j];     for (int i = 2; i <= n - 1; i++)       for (int j = 2; j <= m - 1; j++) work(i, j);     if (judge())       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void read(int &x) {     int v = 0, f = 1;     char c = getchar();     while (!isdigit(c) && c !=  - ) c = getchar();     if (c ==  - )       f = -1;     else       v = (c & 15);     while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);     x = v * f;   }   inline void read(long long &x) {     long long v = 0ll, f = 1ll;     char c = getchar();     while (!isdigit(c) && c !=  - ) c = getchar();     if (c ==  - )       f = -1;     else       v = (c & 15);     while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);     x = v * f;   }   inline void readc(char &x) {     char c;     while (((c = getchar()) ==    ) || c ==   n )       ;     x = c;   }   int n, m, i, j, vis[100005], ve[100005], dep[100005];   long long a[100005], px[100005], py[100005], res[100005];   vector<int> e[100005];   void dfs1(int x) {     vis[x] = 1;     for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) {       if (!ve[*it]) {         int y = px[*it] ^ py[*it] ^ x;         if (!vis[y]) {           ve[*it] = 1;           dep[y] = dep[x] + 1;           dfs1(y);           res[*it] += a[y];           a[x] -= a[y];           a[y] = 0;         }       }     }   }   int main() {     read(n);     read(m);     for (((i)) = (1); ((i)) <= ((n)); ((i))++) read(a[i]);     for (((i)) = (1); ((i)) <= ((m)); ((i))++) {       int x, y;       read(x);       read(y);       px[i] = x;       py[i] = y;       e[x].push_back(i);       e[y].push_back(i);     }     dfs1(1);     for (((i)) = (1); ((i)) <= ((m)); ((i))++)       if (!ve[i]) {         if (!((dep[px[i]] ^ dep[py[i]]) & 1)) {           if (dep[px[i]] & 1)             res[i] -= a[1] / 2;           else             res[i] = +a[1] / 2;           a[px[i]] -= res[i];           a[py[i]] -= res[i];           break;         }       }     memset(vis, 0, sizeof(vis));     memset(ve, 0, sizeof(ve));     dfs1(1);     if (a[1]) {       puts( NO );       return 0;     }     puts( YES );     for (((i)) = (1); ((i)) <= ((m)); ((i))++) {       printf( %lld n , res[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int t, o, c;   };   char s[4000010];   int n, o[4000010], c[4000010], t[4000010];   void build(int id, int l, int r) {     if (r - l < 1) {       if (s[r] ==  ( )         o[id] = 1;       else         c[id] = 1;       return;     }     int mid = (l + r) / 2;     build(2 * id, l, mid);     build(2 * id + 1, mid + 1, r);     int temp = min(o[2 * id], c[2 * id + 1]);     t[id] = t[2 * id] + t[2 * id + 1] + temp;     o[id] = o[2 * id] + o[2 * id + 1] - temp;     c[id] = c[2 * id] + c[2 * id + 1] - temp;   }   node query(int x, int y, int l, int r, int id) {     if (y < l || x > r || r < l) return {0, 0, 0};     if (r - l < 1) {       if (s[r] ==  ( )         return {0, 1, 0};       else         return {0, 0, 1};     }     if (x <= l && y >= r) return {t[id], o[id], c[id]};     node na, nb;     na = query(x, y, l, (l + r) / 2, 2 * id);     nb = query(x, y, (l + r) / 2 + 1, r, 2 * id + 1);     int t = min(na.o, nb.c);     return {na.t + nb.t + t, na.o + nb.o - t, na.c + nb.c - t};   }   int main() {     int i, x, y, j, num, k, m, w, l, r, p;     scanf( %s , s);     n = strlen(s);     build(1, 0, n - 1);     scanf( %d , &m);     for (i = (1); i <= (m); i++) {       scanf( %d%d , &l, &r);       node nd = query(l - 1, r - 1, 1 - 1, n - 1, 1);       printf( %d n , 2 * nd.t);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     stack<char> s;     int n, count = 0;     char c;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> c;       if (s.empty())         s.push(c);       else if (s.top() == c) {         s.pop();         s.push(c);         count++;       } else         s.push(c);     }     cout << count << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int i, j;   int main() {     string str;     int b = 0, e = 0, a = 0, r = 0, s = 0;     cin >> str;     if (str.size() >= 4)       for (i = 0; i < str.size() - 3; i++) {         for (j = i; j < str.size(); j++) {           if (str[j] ==  b  && str[j + 1] ==  e  && str[j + 2] ==  a  &&               str[j + 3] ==  r ) {             s = s + str.size() - j - 3;             break;           }         }       }     cout << s;   }


#include <bits/stdc++.h>   using namespace std;   long long MOD = 1e9 + 7;   bool debug = 1;   const int N = 1e5 + 10;   bool vis[N];   int g[N];   int dfs(int i) {     vis[i] = 1;     if (!vis[g[i]]) {       return 1 + dfs(g[i]);     }     return 1;   }   int main() {     int n, x;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &x);       x--;       g[i] = x;     }     vector<int> vc;     for (int i = 0; i < n; i++) {       if (!vis[i]) vc.push_back(dfs(i));     }     long long tot = 0;     sort(vc.begin(), vc.end(), greater<int>());     for (int i = 0; i < min((int)vc.size(), 2); i++) {       tot += vc[i];     }     tot = tot * tot;     for (int i = 2; i <= vc.size() - 1; i++) {       tot += 1ll * vc[i] * vc[i];     }     cout << tot << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long in() {     int32_t x;     scanf( %d , &x);     return x;   }   inline long long lin() {     long long x;     scanf( %lld , &x);     return x;   }   inline string get() {     char ch[2000010];     scanf( %s , ch);     return ch;   }   inline void read(long long *a, long long n) {     for (long long i = 0; i < n; i++) a[i] = in();   }   template <class blank>   inline void out(blank x) {     cout << x <<   n ;     exit(0);   }   const long long maxn = 1e5 + 10;   const long long maxm = 1e6 + 10;   const long long maxlg = 21;   const long long base = 29;   const long long mod = 1e9 + 7;   const long long INF = 1e18 + 10;   const long long SQ = 317 + 5;   vector<long long> g[maxn];   long long h[maxn], st[maxn], root[maxn][maxlg], cur;   struct cmp {     inline bool operator()(const long long &a, const long long &b) const {       return st[a] < st[b];     }   };   set<long long, cmp> SET;   inline void dfs(long long node, long long fa = 0) {     h[node] = fa == 0 ? 0 : h[fa] + 1;     root[node][0] = fa;     st[node] = cur++;     for (long long i = 1; i < maxlg; i++)       root[node][i] = root[root[node][i - 1]][i - 1];     for (auto u : g[node])       if (u - fa) dfs(u, node);   }   inline long long lca(long long p, long long q) {     if (h[p] < h[q]) swap(p, q);     for (long long i = maxlg - 1; i >= 0; i--)       if (h[p] - (1 << i) >= h[q]) p = root[p][i];     if (p == q) return p;     for (long long i = maxlg - 1; i >= 0; i--)       if (root[p][i] != root[q][i]) p = root[p][i], q = root[q][i];     return root[p][0];   }   inline long long dis(long long v, long long u) {     return h[v] + h[u] - 2 * h[lca(v, u)];   }   inline long long add(long long x, long long fl = 0, long long tp = 0) {     if (SET.empty() || (SET.size() == 1 && tp == 1)) {       if (fl) {         if (tp == 0)           SET.insert(x);         else           SET.erase(x);       }       return 1 * (tp == 0 ? 1 : -1);     }     long long pre, nxt;     set<long long>::iterator it, it2;     it = it2 = SET.upper_bound(x);     if (it == SET.end()) it = SET.begin();     if (it2 == SET.begin()) it2 = SET.end();     it2--;     if (*it2 == x) {       if (it2 == SET.begin()) it2 = SET.end();       it2--;     }     pre = *it2, nxt = *it;     if (fl) {       if (tp == 0)         SET.insert(x);       else         SET.erase(x);     }     return (dis(x, pre) + dis(x, nxt) - dis(pre, nxt)) / 2 * (tp == 0 ? 1 : -1);   }   long long tot;   long long res;   int32_t main() {     long long n = in(), k = in();     for (long long i = 0; i < n - 1; i++) {       long long v = in(), u = in();       g[v].push_back(u);       g[u].push_back(v);     }     dfs(1);     long long r = 1;     for (long long i = 1; i <= n; i++) {       while (r <= n && add(r) + tot <= k) tot += add(r++, 1);       res = max(res, r - i);       if (r == i) r++;       tot += add(i, 1, 1);     }     cout << res <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   FILE *outt, *inn;   const int N = 3e5 + 10;   int n, a[N], res[N], ans, c, l, r;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);     }     ans = -1;     for (int i = 0; i < n; i++) {       l = r = i;       while (l > 0 && a[l - 1] % a[i] == 0) l--;       while (r < n - 1 && a[r + 1] % a[i] == 0) r++;       if ((r - l) > ans) ans = r - l, c = 0;       if ((r - l) == ans) res[c++] = l;       i = r;     }     printf( %d %d n , c, ans);     for (int i = 0; i < c; i++) {       printf( %d  , res[i] + 1);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int test, i, j, count, flag = 0, n, o1 = 0, o2 = 0, b1, x, y, m, l,                                      z, max1, x1, y1, x2, y2, z1, k, sum, f, r,                                      ans1, min1;     cin >> n;     long long int a[n];     if (n == 1) {       cin >> x;       cout <<  1  n  << x;       return 0;     }     for (i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     vector<long long int> a1;     vector<long long int> a2;     flag = 0;     for (i = 0; i < n; i++) {       if (a1.size() == 0) {         a1.push_back(a[i]);       } else {         if (a2.size() == 0) {           a2.push_back(a[i]);         } else {           if (a2.back() != a[i]) {             a2.push_back(a[i]);           } else {             if (a1.back() != a[i]) {               a1.push_back(a[i]);             }           }         }       }     }     if (a1.size() >= 1 && a2.size() >= 1) {       if (a1.back() == a2.back()) {         a1.pop_back();       }     }     cout << a1.size() + a2.size() <<   n ;     for (i = 0; i < a1.size(); i++) {       cout << a1[i] <<    ;     }     for (i = a2.size() - 1; i >= 0; i--) {       cout << a2[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int n, h, p[N], q[N], bit[N];   void update(int x, int v) {     for (; x < n; x += (x + 1) & -(x + 1)) bit[x] += v;   }   int sum(int x) {     int res = 0;     for (; x >= 0; x -= (x + 1) & -(x + 1)) res += bit[x];     return res;   }   void solve() {     fill(bit, bit + n, 0);     for (int i = n - 1; i >= 0; i--) {       int x = sum(p[i] - 1);       update(p[i], 1);       p[i] = x;     }     fill(bit, bit + n, 0);     for (int i = n - 1; i >= 0; i--) {       int x = sum(q[i] - 1);       update(q[i], 1);       q[i] = x;     }     for (int i = n - 1; i >= 0; i--) {       int c = (p[i] + q[i]) / (n - i);       p[i] = (p[i] + q[i]) % (n - i);       if (i > 0) {         p[i - 1] += c;       }     }     fill(bit, bit + n, 0);     for (int i = 0; i < n; i++) update(i, 1);     for (int i = 0; i < n; i++) {       if (i) printf(   );       int lo = 0, hi = n;       while (lo < hi) {         int m = lo + (hi - lo) / 2;         if (p[i] + 1 <= sum(m))           hi = m;         else           lo = m + 1;       }       printf( %d , lo);       update(lo, -1);     }     printf(  n );   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , p + i);     for (int i = 0; i < n; i++) scanf( %d , q + i);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long ara[400005], csum1[400005], csum2[400005];   int main() {     int n;     scanf( %d , &n);     long long sum = 0;     for (int i = 0; i < n; i++)       scanf( %lld , &ara[i]), sum += ara[i], csum1[ara[i]] += ara[i],           csum2[ara[i]]++;     for (int i = 1; i <= 400000; i++)       csum1[i] += csum1[i - 1], csum2[i] += csum2[i - 1];     sort(ara, ara + n);     long long res = 0, lft = 0;     for (int i = 0; i < n; i++) {       long long s = 0;       int x = ara[i];       while (i < n && ara[i] == x) s += ara[i], i++;       i--;       long long temp = 0;       for (int j = x; j <= 200000; j += x)         temp += (csum1[j + x - 1] - csum1[j]) - (csum2[j + x - 1] - csum2[j]) * j;       res = max(res, sum - temp - lft);       lft += s;     }     printf( %lld n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using db = double;   int mod = 1000000007;   db pie = 3.1415926535;   int spf[5000005];   int mm = 5000000;   int pre[5000005];   void help() {     int i;     for (i = 1; i <= mm; i++) {       spf[i] = i;     }     for (i = 2; i <= mm; i += 2) {       spf[i] = 2;     }     int j;     for (i = 3; i * i <= mm; i += 2) {       if (spf[i] == i) {         for (j = i * i; j <= mm; j += i) {           if (spf[j] == j) spf[j] = i;         }       }     }     pre[0] = 0;     pre[1] = 0;     for (i = 2; i <= mm; i++) {       int m = i;       int cnt = 0;       while (m > 1) {         cnt++;         m = m / spf[m];       }       pre[i] = cnt;       pre[i] += pre[i - 1];     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     help();     while (t--) {       int a, b;       cin >> a >> b;       int ans = pre[a] - pre[b];       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   struct Query {     int left;     int right;     int index;     bool operator<(const Query &other) const { return right < other.right; }   };   const int N = 1e6 + 100;   int a[N];   int xorSum[N];   Query q[N];   int ans[N];   struct FenwickTree {     FenwickTree() { fill(array, array + N, 0); }     int lowbit(int a) { return a & -a; }     void update(int i, int value) {       while (i <= n) {         array[i] ^= value;         i += lowbit(i);       }     }     int query(int i) {       int res = 0;       while (i > 0) {         res ^= array[i];         i -= lowbit(i);       }       return res;     }     int n;     int array[N];   } tr;   int main() {     int n, m, i, j;     scanf( %d , &n);     tr.n = n;     for (i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     scanf( %d , &m);     for (i = 1; i <= m; i++) {       scanf( %d%d , &q[i].left, &q[i].right);       q[i].index = i;     }     sort(q + 1, q + 1 + m);     map<int, int> last;     for (i = 1, j = 1; i <= n; i++) {       int &lastTmp = last[a[i]];       if (lastTmp > 0) {         tr.update(lastTmp, a[i]);       }       lastTmp = i;       tr.update(i, a[i]);       xorSum[i] = xorSum[i - 1] ^ a[i];       for (; j <= m && q[j].right == i; j++) {         ans[q[j].index] = xorSum[i] ^ xorSum[q[j].left - 1] ^                           tr.query(q[j].right) ^ tr.query(q[j].left - 1);       }     }     for (i = 1; i <= m; i++) {       printf( %d n , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0)       return a;     else       return gcd(b, a % b);   }   long long expr(long long n, long long e) {     if (e == 0) return 1;     if (e & 1) {       return n * expr(n, e ^ 1);     } else       return expr(n, e >> 1) * expr(n, e >> 1);   }   int main() {     long long n, l, r;     scanf( %lld %lld %lld , &n, &l, &r);     long long count = 0;     long long root = 0.000001 + pow((double)r, 1.0 / ((double)n - 1.0));     if (n == 1) {       printf( %lld n , r - l + 1);       return 0;     }     if (n == 2) {       printf( %lld n , (r - l + 1) * (r - l));       return 0;     };     for (long long i = 1; i <= root; i++) {       for (long long j = i + 1; j <= root; j++) {         if (gcd(i, j) == 1) {           long long maxb = r / expr(j, n - 1);           long long minb = ceil((double)l / (double)expr(i, n - 1));           ;           count += max(0ll, maxb - minb + 1);         }       }     }     printf( %lld n , 2 * count);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100009;   const long long MOD = 1e9 + 7;   int dX[] = {0, 0, 1, -1};   int dY[] = {1, -1, 0, 0};   long long GCD(long long num, long long ones) {     return !ones ? num : GCD(ones, num % ones);   }   string s;   map<int, int> mp;   long long ans, sum;   int arr[4040];   int main() {     scanf( %lld , &sum);     cin >> s;     int n = (s.size());     for (int i = 0; i < n; ++i) arr[i + 1] = s[i] -  0 , arr[i + 1] += arr[i];     for (int i = 0; i < n; ++i) {       for (int j = i + 1; j <= n; ++j) {         mp[arr[j] - arr[i]]++;       }     }     if (sum == 0) {       if (mp.count(0))         cout << mp[0] * (n * 1LL * n - mp[0] + n);       else         puts( 0 );       return 0;     }     for (map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it) {       if (!it->first) continue;       if (sum % it->first == 0 && mp.count(sum / it->first))         ans += mp[sum / it->first] * 1LL * it->second;     }     cout << ans;     return 0;   }


#include<bits/stdc++.h>   using namespace std;   typedef long long ll;   #define int ll   typedef pair<int, int> P;   ll gcd(ll a, ll b){return b? gcd(b, a % b): a;}   ll quickpow(ll a, ll b){ll res = 1; while(b){if (b & 1) res = res * a; a = a * a; b >>= 1;} return res;}   // head      const int N = 2e3 + 5, inf = 1e18;   int n, m, w, mat[N][N];   int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};      void bfs(int x0, int y0, vector<vector<int>> &d) {    d.resize(n + 1, vector<int> (m + 1, inf));    queue<pair<int, int>> quu;    quu.push(make_pair(x0, y0));    d[x0][y0] = 0;    while (!quu.empty()) {     int px = quu.front().first, py = quu.front().second;     quu.pop();     for (int i = 0; i < 4; i++) {      int nx = px + dx[i], ny = py + dy[i];      if (1 <= nx && nx <= n && 1 <= ny && ny <= m && mat[nx][ny] != -1 && d[nx][ny] == inf) {       d[nx][ny] = d[px][py] + w;       quu.push(make_pair(nx, ny));      }     }    }   }      void show(vector<vector<int>> &vt) {    for (int i = 1; i <= n; i++) {     for (int j = 1; j <= m; j++) {      cout << vt[i][j] <<    ;     }     cout << endl;    }   }      signed main(void){    ios::sync_with_stdio(false);    cin.tie(0);    cin >> n >> m >> w;    for (int i = 1; i <= n; i++) {     for (int j = 1; j <= m; j++) {      cin >> mat[i][j];     }    }    vector<vector<int>> d1, d2;    bfs(1, 1, d1);    bfs(n, m, d2);    int ans = d1[n][m];    int mn1 = inf;    for (int i = 1; i <= n; i++) {     for (int j = 1; j <= m; j++) {      if (mat[i][j] <= 0) continue;      mn1 = min(mn1, d1[i][j] + mat[i][j]);     }    }    int mn2 = inf;    for (int i = 1; i <= n; i++) {     for (int j = 1; j <= m; j++) {      if (mat[i][j] <= 0) continue;      mn2 = min(mn2, d2[i][j] + mat[i][j]);     }    }    ans = min(ans, mn1 + mn2);    if (ans == inf) {     cout << -1 << endl;    }    else {     cout << ans << endl;    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int BUFFER_SIZE = 1 << 20;   char rb[BUFFER_SIZE], *rp = rb, *rt = rb;   inline char read_char() {     return rp == rt ? (rt = rb + fread(rb, 1, BUFFER_SIZE, stdin), rp = rb, *rp++)                     : *rp++;   }   inline int read_int() {     int x = 0;     char ch = read_char(), flag = 0;     while (ch !=  -  && (ch <  0  || ch >  9 )) {       ch = read_char();     }     if (ch ==  - ) {       flag = 1;       ch = read_char();     }     for (x = 0; ch >=  0  && ch <=  9 ; ch = read_char()) {       x = x * 10 + (ch -  0 );     }     return flag ? -x : x;   }   const int MAX_N = 200000 + 5;   int N, n[2], siz[MAX_N];   int ey[MAX_N << 1], enext[MAX_N << 1], elast[MAX_N];   void dp(int u, int fa, int dep) {     n[dep & 1]++;     siz[u] = 1;     for (int j = elast[u]; j; j = enext[j]) {       int v = ey[j];       if (v != fa) {         dp(v, u, dep ^ 1);         siz[u] += siz[v];       }     }   }   int main() {     N = read_int();     for (int i = 1, j = 1; i < N; i++) {       int x = read_int();       int y = read_int();       ey[j] = y;       enext[j] = elast[x];       elast[x] = j++;       ey[j] = x;       enext[j] = elast[y];       elast[y] = j++;     }     dp(1, 0, 0);     long long ans = 0;     for (int i = 1; i <= N; i++) {       ans += 1ll * siz[i] * (N - siz[i]);     }     ans += 1ll * n[0] * n[1];     printf( %lld n , ans >> 1);     return 0;   }


#include <bits/stdc++.h>   int main() {     int dem = 1, i, n, c;     scanf( %d %d , &n, &c);     int a[n];     for (i = 0; i < n; i++) {       scanf( %d , &a[i]);     }     for (i = 1; i < n; i++) {       if (a[i] - a[i - 1] <= c) {         dem++;       }       if (a[i] - a[i - 1] > c) {         dem = 1;       }     }     printf( %d , dem);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int m = 1e9 + 7;   const int N = 3e5 + 15;   const int64_t INF = 100000000000000018LL;   priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,                  greater<pair<long long, long long>>>       pq;   vector<vector<long long>> graph(N);   vector<long long> collect;   map<long long, long long> mpp;   set<pair<long long, long long>> s;   set<long long> second;   long long odd = 0, even = 0;   long long pow(long long a, long long b, long long m) {     long long ans = 1;     while (b) {       if (b & 1) ans = (ans * a) % m;       b /= 2;       a = (a * a) % m;     }     return ans;   }   long long inv(long long a) { return pow(a, m - 2, m); }   void dfs(long long ind, vector<bool>& visited) {     visited[ind] = true;     for (auto it : graph[ind]) {       {         if (!visited[it]) dfs(it, visited);       }     }   }   bool isPowerOfTwo(long long n) {     if (n == 0) return false;     return (ceil(log2(n)) == floor(log2(n)));   }   void solve() {     long long a, b, n, x;     cin >> a >> b >> n >> x;     long long ans = pow(a, n, m);     n = n % m;     long long aa = ans * x;     if (aa < 0) aa += m;     aa = aa % m;     long long aa1 = 1;     if (a == 1) {       long long z1 = b * n;       z1 = z1 % m;       cout << (x + z1) % m;       return;     }     aa1 = ((ans - 1 + m) % m) * inv(a - 1);     aa1 = aa1 % m;     if (aa1 < 0) aa1 += m;     aa1 = aa1 * b;     cout << (aa + aa1 + m) % m <<   n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     string str;     cin >> n >> str;     int sum = 0;     bool flag = false;     for (int i = 0; i < n - 1; i++) {       sum += str[i] - 48;       int ts = 0, ti = i + 1;       while (ti < n && ts <= sum) {         if (ts == sum && str[ti] != 48) {           ts = 0;         }         ts += str[ti] - 48;         ti++;       }       if (ti == n && ts == sum) {         flag = true;         break;       }     }     if (flag) {       cout <<  YES ;     } else {       cout <<  NO ;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, m, st;   int tmp, tmp2;   vector<int> adj[200005];   int memo[200005][5];   bool vis[200005][5];   int stc, stn;   pair<int, int> par[200005][5];   int dp(int node, int cur) {     if (adj[node].size() == 0) {       if (cur == 2) {         stc = cur;         stn = node;         return 1;       }       if (cur == 1) {         stc = cur;         stn = node;       }       return cur;     }     if (memo[node][cur] != -1) return memo[node][cur];     int ret = 0;     pair<int, int> cp;     cp.first = node;     cp.second = cur;     for (int i = 0; i < adj[node].size(); i++) {       if (cur == 2) {         if (vis[adj[node][i]][cur]) {           if (ret == 0) ret = 2;           continue;         }         vis[adj[node][i]][cur] = 1;         par[adj[node][i]][cur] = cp;         int now = dp(adj[node][i], cur);         if (now == 1) ret = 1;         vis[adj[node][i]][cur] = 0;       } else {         if (vis[adj[node][i]][(cur + 1) % 2]) {           if (ret == 0) ret = 2;           continue;         }         vis[adj[node][i]][(cur + 1) % 2] = 1;         par[adj[node][i]][(cur + 1) % 2] = cp;         int now = dp(adj[node][i], (cur + 1) % 2);         if (now == 1)           ret = 1;         else if (now == 2 && ret == 0)           ret = 2;         vis[adj[node][i]][(cur + 1) % 2] = 0;       }     }     return memo[node][cur] = ret;   }   void dfs(int node, int cur) {     if (node == st && cur == 0) {       printf( %d , node);       return;     }     dfs(par[node][cur].first, par[node][cur].second);     printf(  %d , node);     return;   }   int main() {     memset(memo, -1, sizeof(memo));     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &tmp);       for (int j = 1; j <= tmp; j++) {         scanf( %d , &tmp2);         adj[i].push_back(tmp2);       }     }     scanf( %d , &st);     int ans = dp(st, 0);     if (ans == 0) {       printf( Lose n );     } else if (ans == 2) {       printf( Draw n );     } else {       printf( Win n );       dfs(stn, stc);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   char ch[111111];   int main() {     scanf( %d%d , &n, &k);     scanf( %s , ch);     int pos = 0;     for (int act = 0; act <= 1000000 && act < k && pos < n - 1; ++act) {       if (act >= 1000000 && act % 2 == k % 2) break;       while (pos < n - 1 && !(ch[pos] ==  4  && ch[pos + 1] ==  7 )) pos++;       if (pos == n - 1) break;       if (pos % 2 == 0)         ch[pos] = ch[pos + 1] =  4 ;       else         ch[pos] = ch[pos + 1] =  7 ;       --pos;       if (pos < 0) pos = 0;     }     printf( %s n , ch);   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000;   const int MAXV = 100;   double prob[MAXN + 5][MAXV + 5];   int sz[MAXN + 5];   double temp[MAXV + 5];   int n;   double ncr(int r, int n) {     if (r > n) return 0;     double ret = 1;     for (int c = 0; c < r; c++) ret *= n - c;     for (int c = 0; c < r; c++) ret /= c + 1;     return ret;   }   double probabilityToTaketakeFromVuntasted(int take, int k, int untasted,                                             int from) {     int tasted = sz[from] - untasted;     return ncr(take, untasted) * ncr(k - take, tasted) / ncr(k, sz[from]);   }   int main() {     int c, c2;     memset(prob, 0, sizeof(prob));     double ret = 0;     scanf( %d , &n);     for (c = 0; c < n; c++) {       scanf( %d , &sz[c]);       prob[c][sz[c]] = 1.0;     }     for (c = 0; c < n; c++) ret += prob[c][0];     int qs;     for (scanf( %d , &qs); qs; qs--) {       int from, to, k;       scanf( %d%d%d , &from, &to, &k);       from--;       to--;       memset(temp, 0, sizeof(temp));       double denominator = ncr(k, sz[from]);       for (int untasted = 0; untasted <= MAXV; untasted++) {         int newk = min(k, untasted);         int tasted = sz[from] - untasted;         double n1 = 1, n2 = 1;         double d1 = 1, d2 = 1;         for (int i = 0; i < min(tasted, k); i++) {           d2 *= i + 1;           n2 *= tasted - i;         }         for (int take = 0; take <= k; take++) {           if (take > 0) {             d1 *= take;             if (take <= untasted)               n1 *= untasted - take + 1;             else               n1 = 0;           }           if (k - take > tasted) continue;           temp[untasted - take] +=               prob[from][untasted] * (n1 / d1) * (n2 / d2) / denominator;           if (k - take > 0) {             d2 /= k - take;             n2 /= tasted - (k - take) + 1;           }         }       }       ret += temp[0] - prob[from][0];       for (int i = 0; i <= MAXV; i++) prob[from][i] = temp[i];       sz[from] -= k;       sz[to] += k;       printf( %.10lf n , ret);     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:500000000 )   using namespace std;   vector<int> A[2];   int main() {     int n, i, j, t, w;     cin >> n;     for (i = 0; i < n; i++) {       cin >> t >> w;       A[t - 1].push_back(w);     }     for (i = 0; i < 2; i++) sort(A[i].begin(), A[i].end(), greater<int>());     int res = 0x3F3F3F3F;     for (int x = -1; x < (int)A[0].size(); x++) {       for (int y = -1; y < (int)A[1].size(); y++) {         w = (x + 1) + 2 * (y + 1);         int s = 0;         for (i = x + 1; i < A[0].size(); i++) s += A[0][i];         for (i = y + 1; i < A[1].size(); i++) s += A[1][i];         if (s <= w) res = min(res, w);       }     }     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int a, b, k;     cin >> a >> b >> k;     long long int total = 0;     if (k % 2 != 0) {       total -= b * (k / 2);       total += a * (k / 2 + 1);     } else {       total -= b * (k / 2);       total += a * (k / 2);     }     cout << total << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   int a[503][503];   int max_row[503];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m, q, i, j, mxi, cnt, x, y;     cin >> n >> m >> q;     for (i = 1; i <= n; i++) {       mxi = 0, cnt = 0;       for (j = 1; j <= m; j++) {         cin >> a[i][j];         if (a[i][j])           cnt++;         else           cnt = 0;         mxi = ((mxi > cnt) ? mxi : cnt);       }       max_row[i] = mxi;     }     for (i = 0; i < q; i++) {       cin >> x >> y;       a[x][y] ^= 1;       cnt = 0, mxi = 0;       for (j = 1; j <= m; j++) {         if (a[x][j])           cnt++;         else           cnt = 0;         mxi = ((mxi > cnt) ? mxi : cnt);       }       max_row[x] = mxi;       mxi = 0;       for (j = 1; j <= n; j++) mxi = ((mxi > max_row[j]) ? mxi : max_row[j]);       cout << mxi << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long A, B, D;   int a[70], b[70], len1, len2;   int main() {     cin >> A >> B;     D = A - B;     unsigned long long temp = A;     if (A < B) {       cout << -1 << endl;       return 0;     }     while (A) {       a[len1++] = A % 2;       A /= 2;     }     while (B) {       b[len2++] = B % 2;       B /= 2;     }     unsigned long long res = 0;     for (int i = len1 - 1; i >= 0; --i) {       if (!b[i]) {         unsigned long long n = (1ULL << i);         if (n <= D / 2.0) {           D -= 2 * n;           res += n;         }       }     }     if (D) {       cout << -1 << endl;       return 0;     }     cout << res <<     << temp - res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long x, d;     while (~scanf( %lld%lld , &x, &d)) {       vector<pair<long long, int> > counts;       long long sum = 0;       long long num = 1;       while (x > 0) {         long long two = 1;         int count = 0;         while (two - 1 <= x) two *= 2, count++;         two /= 2, count--;         sum += count;         counts.emplace_back(num, count);         x -= two - 1;         num += d;       }       printf( %lld n , sum);       for (int i = 0; i < counts.size(); i++)         for (int j = 0; j < counts[i].second; j++)           printf( %lld  , counts[i].first);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int long long x, y, l, r, k, i, j, ans;   vector<int long long> mex, mey, my;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     cin >> x >> y >> l >> r;     for (k = 1; k <= 1e18 / x; k *= x) mex.push_back(k);     if (k <= 1e18) mex.push_back(k);     for (k = 1; k <= 1e18 / y; k *= y) mey.push_back(k);     if (k <= 1e18) mey.push_back(k);     for (i = 0; i < mex.size(); i++) {       for (j = 0; j < mey.size(); j++) {         if (mex[i] + mey[j] > r) break;         if (mex[i] + mey[j] >= l) {           my.push_back(mex[i] + mey[j]);         }       }     }     my.push_back(l - 1);     x = l;     sort(my.begin(), my.end());     my.push_back(r + 1);     for (i = 1; i < my.size(); i++) {       ans = max(ans, my[i] - my[i - 1] - 1);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int N, P;   bool can = false;   string word;   int main() {     scanf( %d%d , &N, &P);     cin >> word;     if (P == 1) {       cout <<  NO n ;       return 0;     } else if (P == 2) {       if (N == 1) {         if (word ==  a )           cout <<  b n ;         else           cout <<  NO n ;       } else if (N == 2) {         if (word ==  ab )           cout <<  ba n ;         else           cout <<  NO n ;       } else if (N > 2)         cout <<  NO n ;       return 0;     }     for (int i = N - 1; i >= 0; --i) {       ++word[i];       while ((i >= 1 && word[i] == word[i - 1]) ||              (i >= 2 && word[i] == word[i - 2]))         ++word[i];       if ((word[i] -  a ) + 1 <= P) {         can = true;         for (int j = i + 1; j < N; ++j) {           word[j] =  a ;           while ((j >= 1 && word[j] == word[j - 1]) ||                  (j >= 2 && word[j] == word[j - 2]))             ++word[j];         }         break;       }     }     if (can)       cout << word;     else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int ts;     cin >> ts;     while (ts--) {       long long n, i, vis[500] = {0}, a;       cin >> n;       for (i = 1; i <= 2 * n; i++) {         cin >> a;         if (vis[a] == 0) {           cout << a <<    ;         }         vis[a] = 1;       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 7;   int a[1003][1003], p[maxn], num[maxn], n, m, vis[1003][1003];   long long k;   int dx[4] = {1, -1, 0, 0};   int dy[4] = {0, 0, 1, -1};   int fi(int x) { return p[x] == x ? x : p[x] = fi(p[x]); }   void uni(int x, int y) {     int p1 = fi(x), p2 = fi(y);     if (p1 == p2) return;     p[p1] = p2;     num[p2] += num[p1];     num[p1] = 0;   }   struct node {     int x, y, z;     node(int x1, int y1, int z1) { x = x1, y = y1, z = z1; }   };   bool cmp(node a, node b) { return a.x > b.x; }   vector<node> Q;   void solve(int x, int y, int z, int v) {     queue<node> Q;     Q.push(node(x, y, 0));     vis[x][y] = 1;     z--;     while (!Q.empty()) {       node now = Q.front();       Q.pop();       for (int i = 0; i < 4; i++) {         int xx = now.x + dx[i];         int yy = now.y + dy[i];         if (z == 0) continue;         if (xx <= 0 || xx > n) continue;         if (yy <= 0 || yy > m) continue;         if (vis[xx][yy]) continue;         if (a[xx][yy] < v) continue;         vis[xx][yy] = 1, z--;         Q.push(node(xx, yy, 0));       }     }     for (int i = 1; i <= n; i++, cout << endl)       for (int j = 1; j <= m; j++)         if (vis[i][j])           printf( %d  , v);         else           printf( 0  );   }   int main() {     scanf( %d%d%lld , &n, &m, &k);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         scanf( %d , &a[i][j]), Q.push_back(node(a[i][j], i, j));     for (int i = 0; i < maxn; i++) p[i] = i, num[i] = 1;     sort(Q.begin(), Q.end(), cmp);     for (int i = 0; i < Q.size(); i++) {       int v = Q[i].x;       if (v == 0) break;       int x = Q[i].y;       int y = Q[i].z;       for (int j = 0; j < 4; j++) {         int xx = x + dx[j];         int yy = y + dy[j];         if (a[xx][yy] >= v) uni((xx - 1) * m + yy, (x - 1) * m + y);       }       long long Num = k / v;       if (k % v) continue;       int fa = fi((x - 1) * m + y);       if (Num <= num[fa]) {         printf( YES n );         solve(x, y, Num, v);         return 0;       }     }     printf( NO n );   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 2e6 + 14;   int n, a[Maxn], b[Maxn], ans, m;   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i], b[a[i]] = a[i], m = max(m, a[i]);     sort(a, a + n);     for (int i = 1; i <= 2 * m; i++) {       if (b[i] == 0) b[i] = b[i - 1];     }     for (int i = m - 1; i > ans; i--) {       if (b[i] == i)         for (int j = i * 2 - 1; j < 2 * m; j += i) {           ans = max(ans, b[j] % i);         }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = 1e9;   const int MX = 1e6 + 5;   const int MD = 1e9 + 7;   int next(vector<int> &a, int pos, long long p) {     long long cnt = 2;     int l = pos, r = pos;     while (r < ((int)a.size())) {       while (r < ((int)a.size()) && cnt > 0 && a[r] == a[l]) r++, cnt--;       if (cnt == 0) return r;       if (r == ((int)a.size())) return -1;       for (int i = 0; cnt <= ((int)a.size()) && i < a[l] - a[r]; i++) cnt *= p;       if (cnt > ((int)a.size())) return -1;       l = r;     }     return -1;   }   long long pt(long long b, long long e) {     long long ans = 1;     while (e > 0) {       if (e & 1) ans = (ans * b) % MD;       b = (b * b) % MD;       e >>= 1;     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n, p;       cin >> n >> p;       vector<int> a(n);       for (int &x : a) cin >> x;       if (p == 1) {         cout << n % 2 <<   n ;         continue;       }       sort(a.begin(), a.end());       reverse(a.begin(), a.end());       int last = -1;       for (int l = 0, r = 0; l < n; l = r) {         r = next(a, l, p);         if (r == -1) {           last = l;           break;         }       }       if (last == -1) {         cout << 0 <<   n ;         continue;       }       long long ans = pt(p, a[last]);       for (int i = last + 1; i < n; i++) ans = (ans - pt(p, a[i]) + MD) % MD;       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     char a[15], b[15], c[15];     scanf( %s%s , a, b);     scanf( %d , &n);     printf( %s %s n , a, b);     for (int i = 1; i <= n; i++) {       scanf( %s , c);       int l = strlen(c);       int la = strlen(a);       int lb = strlen(b);       if (memcmp(a, c, l) == 0 && l == la) {         scanf( %s , a);         printf( %s %s n , a, b);       }       if (memcmp(b, c, l) == 0 && l == lb) {         scanf( %s , b);         printf( %s %s n , a, b);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 1;   const long long nol = 0;   long long n, k, a, cnt, ans, j, mx;   deque<long long> v, vc;   void vkoshp() {     cin >> n >> k;     for (long long i = 0; i < n; i++) {       cin >> a;       v.push_back(a);     }     for (long long i = 0; i < n; i++) {       cin >> a;       if (a == 1)         cnt += v[i], vc.push_back(0);       else         ans += v[i], vc.push_back(v[i]);       mx = max(mx, ans);       if (vc.size() == k) {         ans -= vc[0];         vc.pop_front();       }     }     cout << cnt + mx << endl;   }   int main() {     cout.tie(NULL);     cin.tie(NULL);     ios_base::sync_with_stdio(0);     long long t = 1;     while (t--) vkoshp();   }


#include <bits/stdc++.h>   using namespace std;   int f[10000001];   int main() {     int n, m, l, r;     scanf( %d%d , &n, &m);     long long ans = 0;     int Max0 = max(n * 2, m);     for (long long b = 1; b <= n; ++b) {       ans += 2 * min(b * b, (long long)m);       l = max(0, int(ceil(sqrt(b * b - m))));       r = int(floor(sqrt(b * b - 1)));       ++f[-b - r + Max0], --f[-b - l + 1 + Max0], ++f[-b + l + Max0],           --f[-b + r + 1 + Max0];     }     int s = 0;     for (int i = 0; i < Max0; ++i) {       s += f[i];       if (s > 1) ans -= s - 1;     }     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t = 0;   int ans = 1;   long long int prime(long long int n) {     while (n % 2 == 0) {       if (t == 2) break;       n = n / 2;       t++;       ans *= 2;     }     for (int i = 3; i <= sqrt(n); i += 2) {       while (n % i == 0) {         if (t == 2) break;         n = n / i;         t++;         ans *= i;       }       if (t == 2) break;     }     if (n > 2 && t != 2) {       ans *= n;       t++;       n = 1;     }     return n;   }   int main() {     cin.tie(NULL);     cout.tie(NULL);     ios_base::sync_with_stdio(false);     long long int n;     cin >> n;     if (n == 1)       cout << 1 <<   n  << 0;     else {       long long int final = prime(n);       if (final == 1 && t == 1)         cout << 1 << endl << 0;       else if (final == 1 && t == 2)         cout << 2;       else if (t == 2)         cout << 1 <<   n  << ans;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void err(istream_iterator<string> it) {}   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a <<   n ;     err(++it, args...);   }   template <class T1, class T2>   ostream &operator<<(ostream &os, const pair<T1, T2> &a) {     return os <<  (  << a.first <<  ,   << a.second <<  ) ;   }   template <class T>   ostream &operator<<(ostream &os, const vector<T> &a) {     os <<  [ ;     for (unsigned long long i = 0; i < a.size(); i++)       os << a[i] << (i < a.size() - 1 ?  ,   :   );     os <<  ] ;     return os;   }   template <class T>   ostream &operator<<(ostream &os, const set<T> &a) {     os <<  { ;     for (typename set<T>::iterator it = a.begin(); it != a.end(); it++) {       typename set<T>::iterator jt = it;       os << *it << (++jt != a.end() ?  ,   :   );     }     os <<  } ;     return os;   }   template <class T1, class T2>   ostream &operator<<(ostream &os, map<T1, T2> &a) {     os <<  { n ;     for (typename map<T1, T2>::iterator it = a.begin(); it != a.end(); it++) {       typename map<T1, T2>::iterator jt = it;       os <<      << it->first <<  :   << it->second          << (++jt != a.end() ?  , n  :   n );     }     os <<  } ;     return os;   }   long long n, m;   long long a[2005];   long long dp[2005][2];   bool check(long long lim) {     memset(dp, 127, sizeof(dp));     dp[0][0] = dp[0][1] = 0;     for (long long i = 1; i <= n; i++) {       dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + 1;       dp[i][1] = min(dp[i][1], i - 1);       for (long long j = 1; j < i; j++) {         long long diff = abs(a[i] - a[j]);         if (diff > lim * (i - j)) continue;         dp[i][1] = min(dp[i][1], dp[j][1] + (i - j - 1));       }     }     if (min(dp[n][0], dp[n][1]) <= m) return true;     return false;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (long long i = 1; i <= n; i++) cin >> a[i];     long long l = 0, r = 2e9;     while (l != r) {       long long mid = (l + r) / 2;       if (check(mid))         r = mid;       else         l = mid + 1;     }     cout << l <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = INT_MAX - 10000000;   const double eps = 1e-8;   long long t1, t2, x1, x2, t0;   int main() {     cin >> t1 >> t2 >> x1 >> x2 >> t0;     if (t1 == t2) {       cout << x1 <<     << x2 << endl;       return 0;     }     long long minp = 12345678, minq = 1;     long long y2, ans = 0;     for (long long y1 = 0; y1 <= x1; y1++) {       long long Left = 0, Right = x2, Mid;       if (y1)         while (Left <= Right) {           Mid = Left + Right >> 1;           if ((t2 - t1) * Mid < (t0 - t1) * (Mid + y1))             Left = Mid + 1;           else             Right = Mid - 1;         }       else         Left = x2;       if ((t2 - t1) * Left < (t0 - t1) * (Left + y1) || Left > x2) continue;       if (Left * (t2 - t1) * minq - minq * (t0 - t1) * (Left + y1) <           minp * (Left + y1)) {         y2 = Left;         ans = y1 + y2;         minp = Left * (t2 - t1) - (t0 - t1) * (Left + y1);         minq = Left + y1;       } else if (Left * (t2 - t1) * minq - minq * (t0 - t1) * (Left + y1) ==                  minp * (Left + y1)) {         if (Left + y1 > ans) {           y2 = Left;           ans = y1 + y2;         }       }     }     cout << ans - y2 <<     << y2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long powmod(long long n, long long q) {     long long ans = 1;     n %= 1000000007;     while (q > 0) {       if (q & 1) ans = (ans * n) % 1000000007;       n = (n * n) % 1000000007;       q >>= 1;     }     return ans % 1000000007;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int tc = 1;     while (tc--) {       int n, ans = 0;       cin >> n;       for (int i = 1; i <= n; i++) {         for (int j = i + 1; j <= n; j++) {           int k = i ^ j;           if (k < i + j) {             if (i < k + j) {               if (j < i + k) {                 if (k >= 1 && k <= n) ans++;               }             }           }         }       }       cout << ans / 3 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   int reverseChar(char input) {     const int N = 8;     int output(0);     for (int p = 0; p < N; p++) {       output = 2 * output + (input % 2);       input /= 2;     }     return output;   }   int main() {     const int N = 256;     std::string line;     getline(std::cin, line);     int previous = 0;     for (int p = 0; p < line.size(); p++) {       int current = reverseChar(line[p]);       printf( %d n , (N + previous - current) % N);       previous = current;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct box {     long long int x1, x2, y1, y2;     box() : x1(0), x2(0), y1(0), y2(0) {}     box(int x1, int x2, int y1, int y2) : x1(x1), x2(x2), y1(y1), y2(y2) {}   };   struct edge {     long long int to, c, f;     edge() : to(0), c(0), f(0) {}     edge(int to, long long int c, int f) : to(to), c(c), f(f) {}   };   bool ins(box a, box b) {     if (a.x1 >= b.x1 && a.x2 <= b.x2 && a.y1 >= b.y1 && a.y2 <= b.y2) {       return 1;     }     return 0;   }   const int M = 5000000;   edge edges[M];   vector<int> g[M];   int cnt = 0;   int nxt[M];   int d[M];   int oy[40000];   int ox[40000];   int lasty[10000];   vector<int> how;   int n1;   void addedge(int v, int u, long long int c) {     edges[cnt] = edge(u, c, 0);     g[v].push_back(cnt++);     edges[cnt] = edge(v, 0, 0);     g[u].push_back(cnt++);   }   void buildy(int v, int l, int r) {     if (l == r) {       addedge(100000, v, 1);       return;     }     int m = (l + r) / 2;     buildy(2 * v, l, m);     buildy(2 * v + 1, m + 1, r);     addedge(2 * v, v, 1000000);     addedge(2 * v + 1, v, 1000000);     return;   }   void buildx(int v, int l, int r) {     if (l == r) {       addedge(v + 40000, 100001, 1);       return;     }     int m = (l + r) / 2;     buildx(2 * v, l, m);     buildx(2 * v + 1, m + 1, r);     addedge(v + 40000, 2 * v + 40000, 1000000);     addedge(v + 40000, 2 * v + 1 + 40000, 1000000);     return;   }   void gety(int v, int l, int r, int l1, int r1) {     if (l >= l1 && r <= r1) {       how.push_back(v);       return;     }     if (l > r1 || l1 > r) return;     int m = (l + r) / 2;     gety(2 * v, l, m, l1, r1);     gety(2 * v + 1, m + 1, r, l1, r1);   }   void getx(int v, int l, int r, int l1, int r1) {     if (l >= l1 && r <= r1) {       for (auto c : how) {         addedge(c, 40000 + v, 1000000000);       }       return;     }     if (l > r1 || l1 > r) return;     int m = (l + r) / 2;     getx(2 * v, l, m, l1, r1);     getx(2 * v + 1, m + 1, r, l1, r1);   }   void makepr(int x1, int x2, int y1, int y2) {     how.clear();     gety(1, 0, n1 - 1, x1, x2);     getx(1, 0, n1 - 1, y1, y2);   }   bool bfs(int s, int t, int n) {     for (int i = 0; i < n; i++) d[i] = 1000000;     d[s] = 0;     queue<int> q;     q.push(s);     while (q.size()) {       int v = q.front();       q.pop();       for (auto c : g[v]) {         int to = edges[c].to;         if (d[to] <= d[v] + 1 || edges[c].c == edges[c].f) continue;         d[to] = d[v] + 1;         q.push(to);       }     }     if (d[t] == 1000000) return 0;     return 1;   }   long long int dfs(int v, int t, long long int f) {     if (v == t) return f;     if (f == 0) return 0;     long long int sum = 0;     for (; nxt[v] < g[v].size(); nxt[v]++) {       int id = g[v][nxt[v]];       int to = edges[id].to;       if (d[to] != d[v] + 1 || edges[id].c == edges[id].f) continue;       long long int del = dfs(to, t, min(edges[id].c - edges[id].f, f - sum));       sum += del;       edges[id].f += del;       edges[id ^ 1].f -= del;       if (sum == f) break;     }     return sum;   }   long long int dinic(int s, int t, int n) {     long long int mf = 0;     while (bfs(s, t, n)) {       for (int i = 0; i < n; i++) nxt[i] = 0;       mf += dfs(s, t, 1e9);     }     return mf;   }   bool cmp(box a, box b) { return (a.y1 < b.y1); }   void solve() {     cin >> n1;     int m;     cin >> m;     vector<box> all(m);     for (int i = 0; i < m; i++) {       cin >> all[i].x1 >> all[i].y1 >> all[i].x2 >> all[i].y2;       all[i].x1--;       all[i].x2--;       all[i].y1--;       all[i].y2--;     }     sort(begin(all), end(all), cmp);     buildy(1, 0, n1 - 1);     buildx(1, 0, n1 - 1);     for (auto c : all) {       int x1, x2, y1, y2;       y1 = c.x1;       y2 = c.x2;       x1 = c.y1;       x2 = c.y2;       int cur = 0;       for (int i = 0; i < n1; i++) {         if (lasty[cur] != lasty[i]) {           if (cur >= y1 && i - 1 <= y2) {             if (lasty[cur] != x1) makepr(lasty[cur], x1 - 1, cur, i - 1);             for (int y = cur; y <= i - 1; y++) lasty[y] = x2 + 1;           }           if (cur < y1 && i - 1 > y2) {             if (lasty[cur] != x1) makepr(lasty[cur], x1 - 1, y1, y2);             for (int y = y1; y <= y2; y++) lasty[y] = x2 + 1;             cur = i;           }           if (cur < y1 && i - 1 <= y2) {             if (lasty[cur] != x1) makepr(lasty[cur], x1 - 1, y1, i - 1);             for (int y = y1; y <= i - 1; y++) lasty[y] = x2 + 1;           }           if (cur >= y1 && i - 1 > y2) {             if (lasty[cur] != x1) makepr(lasty[cur], x1 - 1, cur, y2);             for (int y = cur; y <= y2; y++) lasty[y] = x2 + 1;           }           cur = i;         }         if (i == n1 - 1) {           i++;           if (cur >= y1 && i - 1 <= y2) {             if (lasty[cur] != x1) makepr(lasty[cur], x1 - 1, cur, i - 1);             for (int y = cur; y <= i - 1; y++) lasty[y] = x2 + 1;           }           if (cur < y1 && i - 1 > y2) {             if (lasty[cur] != x1) makepr(lasty[cur], x1 - 1, y1, y2);             for (int y = y1; y <= y2; y++) lasty[y] = x2 + 1;           }           if (cur < y1 && i - 1 <= y2) {             if (lasty[cur] != x1) makepr(lasty[cur], x1 - 1, y1, i - 1);             for (int y = y1; y <= i - 1; y++) lasty[y] = x2 + 1;           }           if (cur >= y1 && i - 1 > y2) {             if (lasty[cur] != x1) makepr(lasty[cur], x1 - 1, cur, y2);             for (int y = cur; y <= y2; y++) lasty[y] = x2 + 1;           }         }       }     }     int cur = 0;     for (int i = 0; i < n1; i++) {       if (lasty[cur] != lasty[i]) {         if (lasty[cur] <= n1 - 1) makepr(lasty[cur], n1 - 1, cur, i - 1);         cur = i;       }       if (i == n1 - 1) {         i++;         if (lasty[cur] <= n1 - 1) makepr(lasty[cur], n1 - 1, cur, i - 1);         cur = i;       }     }     cout << dinic(100000, 100001, 100002);   }   int main() {     ios_base::sync_with_stdio(0);     cout.tie(0);     cin.tie(0);     int t;     t = 1;     while (t) {       t--;       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5010;   const int MOD = 1000000007;   int n, x[N], y[N], w[N][N], vis[N];   inline void dfs(int v, int val) {     for (int u = 0; u < n; u++) {       if (w[v][u] > val) {         if (vis[u] == vis[v]) throw 0;         if (vis[u] != -1) continue;         vis[u] = !vis[v];         dfs(u, val);       }     }   }   inline int check(int val) {     memset(vis, -1, sizeof vis);     int ans = 1;     for (int i = 0; i < n; i++)       if (vis[i] == -1) {         try {           vis[i] = 0;           dfs(i, val);         } catch (...) {           return 0;         }         ans += ans;         if (ans >= MOD) ans -= MOD;       }     return ans;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d%d , x + i, y + i);     for (int i = 0; i < n; i++)       for (int j = i + 1; j < n; j++)         w[i][j] = w[j][i] = abs(x[i] - x[j]) + abs(y[i] - y[j]);     int lo = 0, hi = 10050, mid;     while (lo < hi) {       mid = lo + hi >> 1;       if (check(mid))         hi = mid;       else         lo = mid + 1;     }     cout << hi <<   n  << check(hi) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int rowblk[200005];   long long int colblk[200005];   long long int col[200005];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int l, n, i, j, k, m;     cin >> n >> m;     for (i = 1; i <= m; i++) {       cin >> k >> l;       rowblk[k] = 1;       colblk[l] = 1;     }     long long int cnt = 0;     for (i = 2; i < n; i++) {       if (colblk[i] == 0) {         cnt++;         col[i] = 1;       }     }     for (i = 2; i < n; i++) {       if (rowblk[i] == 0) {         if (n % 2 != 0 && i == n / 2 + 1) {           if (col[i] == 0) cnt++;         } else {           cnt++;         }       }     }     cout << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long arr[200001];   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> arr[i];     }     sort(arr, arr + n, greater<int>());     long long gcf = 0, total = 0;     for (int i = 0; i < n; ++i) {       gcf = gcd(arr[0] - arr[i], gcf);       total += (arr[0] - arr[i]);     }     cout << total / gcf <<     << gcf << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cout <<  ?  <<    ;     fflush(stdout);     for (int i = 0; i < 100; i++) {       cout << i <<    ;       fflush(stdout);     }     cout << endl;     int n;     cin >> n;     cout <<  ?  <<    ;     fflush(stdout);     long long a = 0;     for (int i = 0; i < 100; i++) {       a += (1 << 7);       cout << a <<    ;       fflush(stdout);     }     cout << endl;     int b;     cin >> b;     bitset<14> x(n), y(b), z;     for (int i = 0; i < 14; i++) {       if (i < 7)         z[i] = y[i];       else         z[i] = x[i];     }     cout <<  !   << z.to_ulong();   }


#include <bits/stdc++.h>   using namespace std;   struct Pair {     int a, b, c;   };   int a1[200009], a2[200009];   vector<Pair> arr;   bool rules(Pair x, Pair y) { return ((x.a - x.b) < (y.a - y.b)); }   int main() {     int n, k, sum = 0;     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) scanf( %d , &a1[i]);     for (int i = 0; i < n; i++) {       scanf( %d , &a2[i]);       Pair p;       p.a = a1[i], p.b = a2[i];       arr.push_back(p);     }     sort(arr.begin(), arr.end(), rules);     for (int i = 0; i < k; i++) sum += arr[i].a;     for (int i = k; i < n; i++) sum += min(arr[i].a, arr[i].b);     printf( %d n , sum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void umax(T &x, T y) {     if (y > x) x = y;   }   template <class T>   inline void umin(T &x, T y) {     if (y < x) x = y;   }   const int N = 2e5 + 5, B = 0x7fffffff;   map<int, int> comp;   set<int> s;   vector<int> ans;   int a[N];   int st[4 * N];   int n;   int nn;   void upd(int l, int r, int id, int u, int v) {     if (l == r) {       umax(st[id], v);       return;     }     int mid = (l + r) >> 1;     if (u <= mid)       upd(l, mid, id << 1, u, v);     else       upd(mid + 1, r, id << 1 | 1, u, v);     st[id] = max(st[id << 1], st[id << 1 | 1]);   }   int get(int l, int r, int id, int ql, int qr) {     if (r < ql || l > qr) return 0;     if (l >= ql && r <= qr) return st[id];     int mid = (l + r) >> 1;     return max(get(l, mid, id << 1, ql, qr),                get(mid + 1, r, id << 1 | 1, ql, qr));   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++)       scanf( %d , &a[i]), s.insert(a[i]), s.insert(a[i] - 1);     for (int x : s) comp[x] = ++nn;     for (int i = n; i >= 1; i--) {       int ppq = (get(1, nn, 1, 1, comp[a[i] - 1]));       ans.push_back(ppq == 0 ? -1 : ppq - i - 1);       upd(1, nn, 1, comp[a[i]], i);     }     for (int i = (int)ans.size() - 1; i >= 0; i--) printf( %d  , ans[i]);     return !1;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1);   const long long int MOD = 1000000007;   int main() {     int t, a, b;     scanf( %lld , &t);     while (t--) {       scanf( %d %d , &a, &b);       if (b >= a)         printf( 1 );       else {         vector<int> v;         bool k = false;         int c;         for (int i = 1; i * i <= a; i++) {           if (a % i == 0) {             v.push_back(i);             v.push_back(a / i);             k = true;           }         }         if (!k)           printf( %d , a);         else {           sort(v.begin(), v.end(), greater<int>());           for (int i = 0; i < (int)v.size(); i++) {             if (v[i] <= b) {               printf( %d , a / v[i]);               break;             }           }         }       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void __p(T a) {     cout << a;   }   template <typename T, typename U>   void __p(pair<T, U> a) {     cout <<  { ;     __p(a.first);     cout <<  , ;     __p(a.second);     cout <<  } ;   }   template <typename T>   void __p(std::vector<T> a) {     cout <<  { ;     for (auto it = a.begin(); it < a.end(); it++)       __p(*it), cout <<  ,} [it + 1 == a.end()];   }   void solve() {     long long n, k;     cin >> n >> k;     vector<long long> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     if (n == 2) {       cout << 2 - k * (a[0] | a[1]) << endl;       return;     }     long long ans = 2 - k * (a[0] | a[1]);     for (long long i = n - 2; i > n - 300 && i >= 0; i--) {       for (long long j = n - 1; j > i; j--) {         ans = max(ans, (i + 1) * (j + 1) - k * (a[i] | a[j]));       }     }     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int tc = 1;     cin >> tc;     for (int t = 1; t <= tc; t++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int n, x;   bool X[26];   int mat[6][6];   int niz[26];   int sum;   bool check() {     sum = 0;     for (int i = 0; i < n; i++) sum += mat[i][0];     for (int j = 1; j < n; j++) {       int st = 0;       for (int i = 0; i < n; i++) st += mat[i][j];       if (st != sum) return false;     }     for (int i = 0; i < n; i++) {       int st = 0;       for (int j = 0; j < n; j++) st += mat[i][j];       if (st != sum) return false;     }     int sz = 0;     for (int i = 0; i < n; i++) {       sz += mat[i][i];     }     if (sz != sum) return false;     sz = 0;     for (int i = 0; i < n; i++) {       sz += mat[i][n - i - 1];     }     if (sz != sum) return false;     return true;   }   vector<pair<int, int> > pos;   vector<int> vals;   int empty_cnt = 0;   int main() {     scanf( %d , &n);     for (int i = 0; i < n * n; i++) {       scanf( %d , &niz[i]);       vals.push_back(i);       pos.push_back(make_pair(i / n, i % n));     }     do {       for (int i = 0; i < pos.size(); i++) {         mat[pos[i].first][pos[i].second] = niz[vals[i]];       }       if (check()) {         break;       }     } while (next_permutation(vals.begin(), vals.end()));     printf( %d n , sum);     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         printf( %d  , mat[i][j]);       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[200001], b[200001], m;   int main() {     cin >> n >> m;     n = n + m;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     vector<int> vec;     for (int i = 1; i <= n; i++) {       scanf( %d , &b[i]);       if (b[i] == 1) {         vec.push_back(a[i]);       }     }     vec.push_back(2e9 + 1);     int l = 0, cnt = 0, r = 1, c = 0;     while (l < vec.size() - 1 && r <= n) {       if (b[r] == 0) {         while (abs(a[r] - vec[l]) > abs(a[r] - vec[l + 1])) {           cout << cnt <<    ;           c++;           l++;           cnt = 0;         }         cnt++;       }       r++;     }     cout << cnt <<    ;     for (int i = 0; i < vec.size() - 2 - c; i++) {       cout <<  0  ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bitset<1000> mask[1010];   long long sqr(int x) { return 1LL * x * x; }   int n, m, k;   int px[10010], py[10010], x[10010], y[10010], r[10010];   int main() {     int i, j;     scanf( %d%d%d , &n, &m, &k);     for (i = 0; i < n; ++i) {       scanf( %d%d , &px[i], &py[i]);     }     for (i = 0; i < m; ++i) {       scanf( %d%d%d , &r[i], &x[i], &y[i]);     }     for (i = 0; i < n; ++i) {       for (j = 0; j < m; ++j)         if (sqr(px[i] - x[j]) + sqr(py[i] - y[j]) <= sqr(r[j])) mask[i][j] = 1;     }     while (k--) {       int a, b;       scanf( %d%d , &a, &b);       printf( %d n , (mask[a - 1] ^ mask[b - 1]).count());     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   int n;   int head[N], Next[N << 1], ver[N << 1], size[N], edge[N << 1], w[N];   int tot;   long long ans;   void add(int x, int y, int z) {     ver[++tot] = y;     edge[tot] = z;     Next[tot] = head[x];     head[x] = tot;   }   void dfs(int x, int fa) {     size[x] = 1;     for (int i = head[x]; i; i = Next[i]) {       int y = ver[i];       if (y == fa) continue;       dfs(y, x);       size[x] += size[y];     }   }   void solve(int x, int fa, long long s) {     for (int i = head[x]; i; i = Next[i]) {       int y = ver[i], z = edge[i];       if (y == fa) continue;       if (s + (long long)z > (long long)w[y])         ans += (long long)size[y];       else         solve(y, x, max(0ll, s + (long long)z));     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &w[i]);     for (int i = 2; i <= n; i++) {       int p, c;       scanf( %d%d , &p, &c);       add(p, i, c);       add(i, p, c);     }     dfs(1, 0);     solve(1, 0, 0ll);     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 50;   const int mod = 1e9 + 7;   const int inf = 0x3f3f3f3f;   long long n, k;   long long a[maxn];   int vis[maxn];   long long ans = 0;   bool Overflow(long long x, long long y) {     if (x > LLONG_MAX / y)       return false;     else       return true;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     memset(vis, 0, sizeof(vis));     memset(a, 0, sizeof(a));     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     vis[n + 1] = -1;     for (int i = n; i >= 1; i--) {       if (a[i] > 1)         vis[i] = i;       else         vis[i] = vis[i + 1];     }     long long p = 1;     long long s = 0;     for (int i = 1; i <= n; i++) {       int j = i;       p = a[i];       s = a[i];       if (p == s * k) ans++;       while (1) {         int z = vis[j + 1];         if (z == -1) z = n + 1;         int ones = z - j - 1;         if (p % k == 0 && p / k - s >= 1 && p / k - s <= ones) {           ans++;         }         if (z == n + 1) break;         if (Overflow(p, a[z])) {           p = p * a[z];           j = z;           s = s + a[z] + ones;           if (p == s * k) ans++;         } else           break;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 1e5 + 5;   long long N, K;   long long v[MAXN];   int main() {     cin >> N >> K;     for (int i = 0; i < N; i++) cin >> v[i];     sort(v, v + N);     long long resp = N;     for (int i = 0; i < N; i++) {       long long value = v[i] * K;       int l = i + 1, r = N - 1;       while (l <= r) {         int mid = (l + r) / 2;         if (v[mid] == value) {           resp--;           v[mid] = -1;           break;         }         if (v[mid] < value)           l = mid + 1;         else           r = mid - 1;       }     }     cout << resp << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int i, j, k, n, c = 0, p = 0, al, bl, l = 0;     string s;     cin >> s;     for (i = 0; i < s.length(); i++) {       l++;       c += (s[i] -  0 );       if (c % 3 == 0 || (s[i] -  0 ) % 3 == 0 || l >= 3) {         p++;         c = 0;         l = 0;       }     }     if (s[0] ==  0 ) p--;     cout << p;   }


#include <bits/stdc++.h>   using namespace std;   const int NMX = 5007;   const int MMX = 20007;   long long a[NMX][NMX];   long long mi[NMX];   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int i = 1; i <= m; i++) {       int k, l;       scanf( %d %d , &k, &l);       a[k % n][l % n] += 1;     }     for (int i = 0; i < n; i++) {       bool first = false;       for (int j = i; j < (n + i); j++) {         if (first == false && a[i][j % n] >= 1) {           mi[i] += abs(j - i);           first = true;           a[i][j % n] -= 1;         }         mi[i] += a[i][j % n] * n;       }     }     for (int i = 1; i <= n; i++) {       long long ans = 0;       for (int j = 1; j <= n; j++) {         if (mi[j % n] >= 1)           ans = max(ans, mi[j % n] + ((i > j) ? n - i + j : j - i));       }       cout << ans <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int h[n];     for (int i = 0; i < n; i++) {       cin >> h[i];     }     int wys[n][2];     int rosnpr = 0;     int rosnlw = 0;     for (int i = 0; i < n; i++) {       if (h[i] > rosnpr) {         rosnpr++;         wys[i][0] = rosnpr;       } else {         rosnpr = h[i];         wys[i][0] = rosnpr;       }     }     for (int i = n - 1; i >= 0; i--) {       if (h[i] > rosnlw) {         rosnlw++;         wys[i][1] = rosnlw;       } else {         rosnlw = h[i];         wys[i][1] = rosnlw;       }     }     int maxi = 0;     for (int i = 0; i < n; i++) {       if (min(wys[i][0], wys[i][1]) > maxi) {         maxi = min(wys[i][0], wys[i][1]);       }     }     cout << maxi;   }


#include <bits/stdc++.h>   using namespace std;   template <typename S, typename T>   ostream& operator<<(ostream& out, pair<S, T> const& p) {     out <<  (  << p.first <<  ,   << p.second <<  ) ;     return out;   }   template <typename T>   ostream& operator<<(ostream& out, vector<T> const& v) {     int l = v.size();     for (int i = 0; i < l - 1; i++) out << v[i] <<    ;     if (l > 0) out << v[l - 1];     return out;   }   template <typename T>   void trace(const char* name, T&& arg1) {     cout << name <<   :   << arg1 << endl;   }   template <typename T, typename... Args>   void trace(const char* names, T&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cout.write(names, comma - names) <<   :   << arg1 <<   |  ;     trace(comma + 1, args...);   }   vector<int> htov(string s) {     vector<int> ret;     for (char c : s) {       if (c <=  9 )         ret.push_back(c -  0 );       else         ret.push_back(10 + c -  a );     }     reverse(ret.begin(), ret.end());     return ret;   }   void sub(vector<int>& v) {     int n = v.size();     if (n == 1 and v[0] == 0) return;     int i = 0;     while (i < n and v[i] == 0) i++;     v[i]--;     if (i > 0) {       for (int j = 0; j < i; j++) v[j] = 15;       if (v[i] == 0 and (i + 1) == v.size()) v.resize(i);     }     return;   }   const int N = 17;   long long dp[N][2][2];   long long get(vector<int> v) {     reverse(v.begin(), v.end());     int n = v.size();     long long res = 0;     for (int d = 1; d < 16; d++) {       if ((d / 4) + 1 > n) break;       memset(dp, 0, sizeof dp);       dp[0][1][0] = 1;       for (int i = 0; i < n; i++) {         for (int eq = 0; eq < 2; eq++) {           for (int flag = 0; flag < 2; flag++) {             long long val = dp[i][eq][flag];             if (!val) continue;             int max_nd = min(d, (eq == 0) ? 15 : v[i]);             for (int nd = 0; nd <= max_nd; nd++) {               if (n - i - 1 == d / 4 and (nd & (1 << (d % 4))) == 0) continue;               int neq = (eq and nd == v[i]);               int nflag = (flag or nd == d);               dp[i + 1][neq][nflag] += val;             }           }         }       }       for (int eq = 0; eq < 2; eq++) {         res += dp[n][eq][1];       }     }     return res;   }   void solve() {     string l, r;     cin >> l >> r;     vector<int> R = htov(r);     vector<int> L = htov(l);     sub(L);     cout << get(R) - get(L) << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int q;     cin >> q;     while (q--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct fenwick_tree_t {     vector<int> t;     void init(int n) { t.assign(n, 0); }     void inc(int pos, int x = 1) {       while (pos < (int)t.size()) {         t[pos] += x;         pos = pos | (pos + 1);       }     }     int get(int pos) const {       int ret = 0;       while (pos >= 0) {         ret += t[pos];         pos = (pos & (pos + 1)) - 1;       }       return ret;     }   };   vector<int> ord(vector<int> const &a) {     fenwick_tree_t tree;     tree.init(a.size() + 1);     vector<int> ret(a.size(), 0);     for (int i = (int)ret.size() - 1; i >= 0; --i) {       ret[i] = tree.get(a[i]);       tree.inc(a[i]);     }     return ret;   }   ostream &operator<<(ostream &out, vector<int> const &a) {     for (int x : a) out << x <<    ;     return out;   }   vector<int> solve(vector<int> const &a, vector<int> const &b) {     vector<int> ord_a = ord(a);     vector<int> ord_b = ord(b);     vector<int> ord_ans(ord_a.size(), 0);     for (int i = 0; i < (int)ord_ans.size(); ++i)       ord_ans[i] = ord_a[i] + ord_b[i];     reverse(ord_ans.begin(), ord_ans.end());     for (int i = 0; i < (int)ord_ans.size(); ++i) {       if (ord_ans[i] >= i + 1) {         if (i + 1 < (int)ord_ans.size()) ++ord_ans[i + 1];         ord_ans[i] -= i + 1;       }     }     reverse(ord_ans.begin(), ord_ans.end());     vector<int> ans(ord_ans.size(), 0);     fenwick_tree_t t;     t.init(ord_ans.size() + 1);     for (int i = 0; i < (int)ans.size(); ++i) t.inc(i);     for (int i = 0; i < (int)ans.size(); ++i) {       int l = -1, r = ans.size() - 1;       while (r - l > 1) {         int m = (l + r) / 2;         if (t.get(m) < ord_ans[i] + 1) {           l = m;         } else {           r = m;         }       }       t.inc(r, -1);       ans[i] = r;     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     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];     cout << solve(a, b) << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( avx,avx2,fma )   using namespace std;   bool sortbysec(const pair<char, int> &a, const pair<char, int> &b) {     return (a.second < b.second);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     string ip, hh, mm;     cin >> ip;     hh = ip.substr(0, 2);     string hr = hh;     reverse(hr.begin(), hr.end());     mm = ip.substr(3, 4);     if (hh ==  23  && (stoi(mm) >= stoi(hr)))       cout <<  00:00 ;     else if (hh ==  06  || hh ==  07  || hh ==  08  || hh ==  09 )       cout <<  10:01 ;     else if (hh ==  15  && stoi(mm) >= 51 || hh ==  16  || hh ==  17  ||              hh ==  18  || hh ==  19 )       cout <<  20:02 ;     else if (stoi(mm) < stoi(hr))       cout << hh <<  :  << hr;     else {       int x = (stoi(hh) + 1) % 10 * 10 + stoi(hh) / 10;       int y = stoi(hh) + 1;       if (y >= 10)         cout << y <<  :  << x;       else         cout <<  0  << y <<  :  << x;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const int N = 1e5 + 10;   char a[N], b[N];   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n;       scanf( %d , &n);       scanf( %s%s , a, b);       int t = 0;       int t1 = 0, t2 = 0;       for (int i = 0; i < n; i++) {         if (a[i] != b[i]) {           t++;           if (t == 1)             t1 = i;           else if (t == 2)             t2 = i;         }         if (t == 3) break;       }       if (t != 2)         printf( No n );       else {         if (t == 0 || (a[t1] == a[t2] && b[t1] == b[t2]))           printf( Yes n );         else           printf( No n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, q;   int par[26][100005], depth[100005];   vector<int> st[400005];   vector<int> G[100005];   vector<int> SS[26][100005];   int cnt, chaincnt;   void get10(vector<int> &res, const vector<int> &fir, const vector<int> sec) {     int c1 = 0, c2 = 0;     res = vector<int>(min((int)fir.size() + (int)sec.size(), 10));     for (int i = 0; i < (int)res.size(); i++) {       if (c1 != fir.size() && c2 != sec.size()) {         if (fir[c1] < sec[c2]) {           res[i] = fir[c1++];         } else {           res[i] = sec[c2++];         }       } else if (c1 == fir.size()) {         res[i] = sec[c2++];       } else {         res[i] = fir[c1++];       }     }     return;   }   set<int> ret1, ret2;   int LCA(int u, int v) {     ret1.clear(), ret2.clear();     if (depth[u] < depth[v]) swap(u, v);     int dif = depth[u] - depth[v];     for (int i = 0; i <= 24; i++) {       if (dif & (1 << i)) {         u = par[i][u];       }     }     if (u == v) return u;     for (int i = 24; i >= 0; i--) {       if (par[i][u] != par[i][v]) {         u = par[i][u], v = par[i][v];       }     }     return par[0][u];   }   vector<int> solve(int u, int v) {     int lca = LCA(u, v);     int d1 = depth[u] - depth[lca] + 1;     int d2 = depth[v] - depth[lca] + 1;     int lvl = 26;     set<int> ret1;     while (d1 > 0) {       if ((1 << lvl) <= d1) {         d1 -= (1 << lvl);         for (int i = 0; i < (int)SS[lvl][u].size() && i < 10; i++) {           ret1.insert(SS[lvl][u][i]);         }         while (ret1.size() > 10) {           ret1.erase(--ret1.end());         }         u = par[lvl][u];       }       lvl--;       if (lvl < 0) lvl = 0;     }     lvl = 26;     set<int> ret2;     while (d2 > 0) {       if ((1 << lvl) <= d2) {         d2 -= (1 << lvl);         for (int i = 0; i < (int)SS[lvl][v].size() && i < 10; i++) {           ret2.insert(SS[lvl][v][i]);         }         while (ret2.size() > 10) {           ret2.erase(--ret2.end());         }         v = par[lvl][v];       }       lvl--;       if (lvl < 0) lvl = 0;     }     ret1.insert(ret2.begin(), ret2.end());     while (ret1.size() > 10) {       ret1.erase(--ret1.end());     }     vector<int> ret(ret1.begin(), ret1.end());     return ret;   }   void dfs(int u, int p, int dep) {     depth[u] = dep;     par[0][u] = p;     for (int i = 0; i < (int)G[u].size(); i++) {       int v = G[u][i];       if (v == p) continue;       dfs(v, u, dep + 1);     }   }   int main() {     int from, to;     scanf( %d %d %d , &n, &m, &q);     for (int i = 1; i < n; i++) {       scanf( %d %d , &from, &to);       G[from].push_back(to);       G[to].push_back(from);     }     int cur;     for (int i = 1; i <= m; i++) {       scanf( %d , &cur);       SS[0][cur].push_back(i);     }     memset(par, -1, sizeof par);     chaincnt = cnt = 1;     dfs(1, -1, 0);     for (int i = 1; i <= 24; i++)       for (int j = 1; j <= n; j++)         if (par[i - 1][j] != -1) {           par[i][j] = par[i - 1][par[i - 1][j]];           get10(SS[i][j], SS[i - 1][par[i - 1][j]], SS[i - 1][j]);         }     int how;     vector<int> ans;     while (q--) {       scanf( %d %d %d , &from, &to, &how);       ans = solve(from, to);       printf( %d , min((int)ans.size(), how));       for (int i = 0; i < ans.size() && how; i++, how--) {         printf(  %d , ans[i]);       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, c;     cin >> n >> c;     int a[n], t[n];     for (int i = 0; i < n; i++) cin >> a[i];     for (int j = 0; j < n; j++) cin >> t[j];     int b = 0, e = 0, e1 = 0;     for (int i = 0; i < n; i++) {       b += t[i];       e += max(0, a[i] - c * b);     }     b = 0;     for (int i = n - 1; i >= 0; i--) {       b += t[i];       e1 += max(0, a[i] - c * b);     }     if (e > e1)       cout <<  Limak ;     else if (e == e1)       cout <<  Tie ;     else       cout <<  Radewoosh ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   void vrikodara(long long n = 12) {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout << setprecision(n) << fixed;   }   long long lb = 100000;   void solve() {     long long n;     cin >> n;     set<long long> s, so, se;     for (long long i = 1; i <= n; i++) s.insert(i);     vector<long long> p(n + 2);     for (long long i = 1; i <= n; i++) {       cin >> p[i];       if (p[i]) s.erase(p[i]);     }     if (n == 1) {       cout <<  0 ;       return;     }     for (auto i : s)       if (i & 1)         so.insert(i);       else         se.insert(i);     set<pair<long long, long long> > sso, sse;     for (long long i = 1; i <= n; i++) {       if (!p[i]) {         if (!p[i - 1]) continue;         long long x = i, y = i;         while (y <= n && !p[y]) y++;         if (y > n) break;         if (p[x - 1] & 1 && p[y] & 1) {           sso.insert({y - x, x});         } else if (p[x - 1] % 2 == 0 && p[y] % 2 == 0) {           sse.insert({y - x, x});         }         i = y;       }     }     if (s.size() >= n - 1) {       cout <<  1 ;       return;     }     while (!sso.empty() && !so.empty()) {       auto q = *sso.begin();       long long x = q.second;       long long y = q.first + q.second;       if (q.first > so.size()) break;       sso.erase(q);       while (x < y && !so.empty()) {         long long j = *so.begin();         so.erase(j);         p[x] = j;         x++;       }       if (x < y) {         sso.insert({y - x, x});         break;       }     }     while (!sse.empty() && !se.empty()) {       auto q = *sse.begin();       long long x = q.second, y = q.first + q.second;       if (q.first > se.size()) break;       sse.erase(q);       while (x < y && !se.empty()) {         long long j = *se.begin();         se.erase(j);         p[x] = j;         x++;       }       if (x < y) {         sse.insert({y - x, x});         break;       }     }     long long d1 = 1;     while (d1 <= n && !p[d1]) d1++;     d1--;     long long d2 = n;     while (d2 > 0 && !p[d2]) d2--;     if (d1 && d2 < n) {       if (d1 > n - d2) {         d2++;         if (p[d2 - 1] & 1) {           while (!so.empty() && d2 <= n) {             long long j = *so.begin();             so.erase(j);             p[d2] = j;             d2++;           }         } else {           while (!se.empty() && d2 <= n) {             long long j = *se.begin();             se.erase(j);             p[d2] = j;             d2++;           }         }       } else {         if (p[d1 + 1] & 1) {           while (!so.empty() && d1 > 0) {             long long j = *so.begin();             so.erase(j);             p[d1] = j;             d1--;           }         } else {           while (!se.empty() && d1 > 0) {             long long j = *se.begin();             se.erase(j);             p[d1] = j;             d1--;           }         }       }     }     d1 = 1;     while (d1 <= n && !p[d1]) d1++;     d1--;     d2 = n;     while (d2 > 0 && !p[d2]) d2--;     d2++;     if (p[1] == 0) {       if (p[d1 + 1] & 1) {         while (!so.empty() && d1 > 0) {           long long j = *so.begin();           so.erase(j);           p[d1] = j;           d1--;         }       } else {         while (!se.empty() && d1 > 0) {           long long j = *se.begin();           se.erase(j);           p[d1] = j;           d1--;         }       }     }     if (p[n] == 0) {       if (p[d2 - 1] & 1) {         while (!so.empty() && d2 <= n) {           long long j = *so.begin();           so.erase(j);           p[d2] = j;           d2++;         }       } else {         while (!se.empty() && d2 <= n) {           long long j = *se.begin();           se.erase(j);           p[d2] = j;           d2++;         }       }     }     for (long long i = 1; i <= n && !so.empty() && !se.empty(); i++) {       if (!p[i]) {         if (p[i - 1] & 1) {           long long j = *so.begin();           so.erase(j);           p[i] = j;         } else {           long long j = *se.begin();           se.erase(j);           p[i] = j;         }       }     }     long long i = 1;     for (auto j : se) {       while (p[i] && i <= n) i++;       p[i] = j;     }     for (auto j : so) {       while (p[i] && i <= n) i++;       p[i] = j;     }     long long ans = 0;     for (long long i = 2; i <= n; i++) {       if (p[i] % 2 != p[i - 1] % 2) ans++;     }     cout << ans;   }   int32_t main() {     vrikodara();     long long t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long vis[1009];   vector<long long> v[1000];   long long th, n;   void dfs(long long s) {     if (vis[s]) return;     vis[s] = 1;     for (long long i = 0; i < v[s].size(); i++) {       if (s <= n) th++;       dfs(v[s][i]);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long i, j, k, m, l, a, prothom = 1, kake, ans = 0;     cin >> n >> m;     for (i = 1; i <= n; i++) {       cin >> k;       while (k--) {         cin >> a;         v[a + n].push_back(i);         v[i].push_back(a + n);       }     }     for (i = 1; i <= n; i++) {       if (vis[i] == 0) {         ans++;         dfs(i);       }     }     if (ans == n && th == 0)       cout << n << endl;     else       cout << ans - 1 << endl;   }