#include <bits/stdc++.h>   #pragma comment(linker,  /stack:225450978 )   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18;   const long double Pi = 3.141592653589793116, EPS = 1e-9,                     Gold = ((1 + sqrt(5)) / 2);   long long keymod[] = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL};   long long keyCount = sizeof(keymod) / sizeof(long long);   template <class T>   int getbit(T s, int i) {     return (s >> i) & 1;   }   template <class T>   T onbit(T s, int i) {     return s | (T(1) << i);   }   template <class T>   T offbit(T s, int i) {     return s & (~(T(1) << i));   }   template <class T>   int cntbit(T s) {     return __builtin_popcountll(s);   }   auto TimeStart = chrono::steady_clock::now();   auto TimeEnd = chrono::steady_clock::now();   void ControlIO(int argc, char* argv[]);   void TimerStart();   void TimerStop();   void Exit();   long long n, k, d1, d2;   void Input() { cin >> n >> k >> d1 >> d2; }   void Solve() {     long long A = LINF, B = 0, C = 0;     for (long long i = 0; i < 2; i++) {       for (long long j = 0; j < 2; j++) {         if (i)           B = A - d1;         else           B = A + d1;         if (j)           C = B - d2;         else           C = B + d2;         if ((k - A - B - C + min(A, min(B, C)) * 3) % 3 != 0 ||             k - A - B - C + min(A, min(B, C)) * 3 < 0)           continue;         if ((A + B + C + n - k) % 3 == 0 &&             (A + B + C + n - k) / 3 >= max(A, max(B, C))) {           cout <<  yes n ;           return;         }       }     }     cout <<  no n ;   }   int main(int argc, char* argv[]) {     ControlIO(argc, argv);     ios_base::sync_with_stdio(0);     cin.tie(NULL);     int T;     cin >> T;     TimerStart();     while (T--) {       Input();       Solve();     }     TimerStop();     return 0;   }   void ControlIO(int argc, char* argv[]) {}   void TimerStart() {}   void TimerStop() {}   void Exit() {     TimerStop();     exit(0);   }


#include <bits/stdc++.h>   using namespace std;   string to_string(string s) { return     + s +    ; }   string to_string(const char *s) { return to_string((string)s); }   string to_string(bool b) { return (b ?  true  :  false ); }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> t) {     return  (  + to_string(get<0>(t)) +  ,   + to_string(get<1>(t)) +  ,   +            to_string(get<2>(t)) +  ) ;   }   template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto &x : v) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }   void _debug() { cerr <<   n ; }   template <typename Head, typename... Tail>   void _debug(Head H, Tail... T) {     cerr <<     << to_string(H);     _debug(T...);   }   using ll = long long;   using ld = long double;   using ii = pair<int, int>;   using vi = vector<int>;   using vii = vector<ii>;   using vc = vector<char>;   using vb = vector<bool>;   int main() {     cin.tie(nullptr);     ios::sync_with_stdio(false);     cout << fixed << setprecision(9);     int n, k;     cin >> n >> k;     int lo = 1, hi = n;     while (lo < hi) {       int mid = lo + (hi - lo) / 2;       int total = 0;       int cur = mid;       while (cur) {         total += cur;         cur /= k;       }       if (total >= n) {         hi = mid;       } else {         lo = mid + 1;       }     }     cout << hi;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename S>   ostream &operator<<(ostream &os, const pair<T, S> &p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename C, typename T = decay<decltype(*begin(declval<C>()))>,             typename enable_if<!is_same<C, string>::value>::type * = nullptr>   ostream &operator<<(ostream &os, const C &c) {     bool f = true;     os <<  [ ;     for (const auto &x : c) {       if (!f) os <<  ,  ;       f = false;       os << x;     }     return os <<  ] ;   }   template <typename T>   void debug(string s, T x) {     cerr <<   033[1;35m  << s <<   033[0;32m =  033[33m  << x <<   033[0m n ;   }   template <typename T, typename... Args>   void debug(string s, T x, Args... args) {     for (int i = 0, b = 0; i < (int)s.size(); i++)       if (s[i] ==  (  || s[i] ==  { )         b++;       else if (s[i] ==  )  || s[i] ==  } )         b--;       else if (s[i] ==  ,  && b == 0) {         cerr <<   033[1;35m  << s.substr(0, i) <<   033[0;32m =  033[33m  << x              <<   033[31m |  ;         debug(s.substr(s.find_first_not_of(   , i + 1)), args...);         break;       }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, d;     cin >> n >> d;     vector<array<int, 3>> x(n);     for (int i = 0; i < n; i++) {       int s, a;       cin >> s >> a;       x[i] = {max(s, a), s, a};     }     int ret = 0;     sort(x.begin(), x.end());     for (auto [_, s, a] : x)       if (s >= d) {         ret++;         d = max(d, a);       }     cout << ret <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using lli = long long;   using lld = long double;   using ulli = unsigned long long int;   using pll = pair<lli, lli>;   using ttt = pair<lli, pll>;   using vttt = vector<ttt>;   using vll = vector<pll>;   using vl = vector<lli>;   using vi = vector<int>;   using vvi = vector<vector<int>>;   using cd = complex<lld>;   void sanjeev(int kkkk) {     int t;     cin >> t;     while (t > 0) {       t--;       long long int a, b, x, y;       cin >> a >> b >> x >> y;       long long int n;       cin >> n;       long long int sa = min(n, a - x);       long long int sb = min(n, b - y);       if (a - sa < b - sb) {         a -= sa;         n -= sa;         sb = min(n, b - y);         b -= sb;         cout << a * b <<   n ;       } else {         b -= sb;         n -= sb;         sa = min(n, a - x);         a -= sa;         cout << a * b <<   n ;       }     }   }   int main() {     if (!0) ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cout << fixed << setprecision(10);     int t = 1;     for (int i = 0; i < t; i++) {       sanjeev(i + 1);     }     if (0) system( pause );   }


#include <bits/stdc++.h>   using namespace std;   vector<int> x[100000], G[100000];   int n, a[100000], col[100000], cnt, p[2001][1000];   long long ret = 1, ret2 = 0;   long long gcd(long long r, long long s) {     if (s == 0) return r;     return gcd(s, r % s);   }   void dfs(int pos) {     if (col[pos] >= 1) return;     col[pos] = cnt;     G[cnt].push_back(pos);     for (int i = 0; i < x[pos].size(); i++) dfs(x[pos][i]);   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       a[i]--;       x[i].push_back(a[i]);       x[a[i]].push_back(i);     }     for (int i = 0; i < n; i++) {       if (col[i] >= 1) continue;       cnt++;       dfs(i);     }     for (int i = 1; i <= cnt; i++) {       for (int j = 0; j <= 2000; j++) {         for (int k = 0; k < 1000; k++) p[j][k] = 0;       }       for (int j : G[i]) p[0][j] = j;       for (int j = 0; j < 2000; j++) {         for (int k : G[i]) {           p[j + 1][k] = a[p[j][k]];         }       }       long long cnt2 = 0, cnt3 = 0;       for (int j = 1; j <= 1000; j++) {         bool OK = true;         for (int k = 0; k < n; k++) {           if (p[j][k] != p[2 * j][k]) OK = false;         }         if (OK == true) {           if (cnt2 == 0)             cnt2 = j;           else {             cnt3 = j;             break;           }         }       }       ret = ret / gcd(ret, cnt3 - cnt2) * (cnt3 - cnt2);       ret2 = max(ret2, cnt2);     }     long long B = ret;     while (ret2 > B) B += ret;     cout << B << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const int MAXN = 50 + 10;   template <typename T>   inline void add(T& a, T b) {     a += b;     if (a >= MOD) a -= MOD;   }   int main() {     static long long g[MAXN][MAXN];     static long long f[MAXN][MAXN];     static long long F[MAXN][MAXN];     static long long inv[MAXN];     ios::sync_with_stdio(0);     int n, m;     cin >> n >> m;     inv[0] = inv[1] = 1;     for (int i = 2; i <= n; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;     f[0][1] = 1;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= i + 1; ++j) {         for (int p = 0; p <= i - 1; ++p) {           for (int q = j; q <= p + 1; ++q)             add(g[i][j], f[p][q] * f[i - 1 - p][j] % MOD);           for (int q = j + 1; q <= i - p; ++q)             add(g[i][j], f[p][j] * f[i - 1 - p][q] % MOD);         }         memset(F, 0, sizeof(F));         for (int k = 0; k <= n; ++k) {           for (int p = 1; p <= k + 1; ++p) {             long long c = 1;             for (int q = 1; k + q * i <= n; ++q) {               c = c * (g[i][j] + q - 1) % MOD * inv[q] % MOD;               add(F[k + q * i][p + q * j], f[k][p] * c % MOD);             }           }         }         for (int k = 0; k <= n; ++k)           for (int p = 1; p <= k + 1; ++p) add(f[k][p], F[k][p]);       }     }     cout << f[n][m] << endl;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long n;     cin >> n;     string s1, s2;     cin >> s1 >> s2;     long long a = 0, b = 0, c = 0, d = 0;     vector<long long> a1, b1, c1, d1;     for (long long i = 0; i < n; ++i) {       if (s1[i] ==  1  && s2[i] ==  1 ) {         d++;         d1.push_back(i + 1);       }       if (s1[i] ==  1  && s2[i] ==  0 ) {         c++;         c1.push_back(i + 1);       }       if (s1[i] ==  0  && s2[i] ==  1 ) {         b++;         b1.push_back(i + 1);       }       if (s1[i] ==  0  && s2[i] ==  0 ) {         a++;         a1.push_back(i + 1);       }     }     long long x;     for (long long i = 0; i <= min(b, n / 2); ++i) {       for (long long j = 0; j <= min(c, n / 2 - i); ++j) {         if (j < b - i) {           x = b - i - j;           if (x > d || (d - x) % 2) continue;           x = x + (d - x) / 2;           if (x + i + j > n / 2 || (n / 2 - (x + i + j)) > a) continue;         } else {           x = j - b + i;           if (x > d || (d - x) % 2) continue;           x = (d - x) / 2;           if (x + i + j > n / 2 || (n / 2 - (x + i + j)) > a) continue;         }         for (long long u = 0; u < i; ++u) {           cout << b1[u] <<    ;         }         for (long long u = 0; u < j; ++u) {           cout << c1[u] <<    ;         }         for (long long u = 0; u < x; ++u) {           cout << d1[u] <<    ;         }         for (long long u = 0; u < n / 2 - i - j - x; ++u) {           cout << a1[u] <<    ;         }         return 0;       }     }     cout << -1;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   int n, m;   map<int, bool> ma, mb;   int a[maxn], b[maxn];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       if (ma[a[i]] == true) {         printf( 0 n );         return 0;       }       ma[a[i]] = true;     }     int ans = 3;     for (int i = 1; i <= n; i++) {       b[i] = (a[i] & m);       if (ma[b[i]] == true && a[i] != b[i]) {         ans = min(ans, 1);         break;       }       if (mb[b[i]] == true) {         ans = min(ans, 2);       }       mb[b[i]] = true;     }     if (ans == 3) ans = -1;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1600 + 5;   const int dr[] = {0, 0, 1, -1, 1, -1, 1, -1};   const int dc[] = {1, -1, 0, 0, 1, 1, -1, -1};   int n, m, nr, nc, cnt, ct;   bool grid[MAX][MAX], x[MAX][MAX], y[MAX][MAX], z[MAX][MAX], val;   vector<int> ans;   pair<int, int> cur;   bool inside(int r, int c) {     if (r > 0 && r <= n && c > 0 && c <= m) return 1;     return 0;   }   void dfs(int r, int c) {     grid[r][c] = 0;     x[r][c] = 1;     for (int i = 0; i <= 3; i++) {       nr = r + dr[i], nc = c + dc[i];       if (!grid[nr][nc]) continue;       dfs(nr, nc);     }   }   void cs() {     for (int r = 1; r <= n; r++)       for (int c = 1; c <= m; c++) {         if (!x[r][c]) continue;         if (!inside(r - 1, c - 1)) continue;         if (!inside(r + 1, c + 1)) continue;         val = 1;         for (int k = 0; k <= 7; k++)           if (!x[r + dr[k]][c + dc[k]]) {             val = 0;             break;           }         if (val) {           y[r][c] = 1;           for (int k = 0; k <= 7; k++) y[r + dr[k]][c + dc[k]] = 1;         }       }   }   void cas() {     for (int r = 1; r <= n; r++)       for (int c = 1; c <= m; c++) {         if (!x[r][c]) continue;         if (!inside(r - 2, c - 2)) continue;         if (!inside(r + 2, c + 2)) continue;         val = 1;         for (int i = r - 2; i <= r + 2; i++)           for (int j = c - 2; j <= c + 2; j++)             if (!x[i][j]) {               val = 0;               break;             }         if (val)           for (int i = r - 2; i <= r + 2; i++)             for (int j = c - 2; j <= c + 2; j++) z[i][j] = 1;       }     for (int r = 1; r <= n; r++)       for (int c = 1; c <= m; c++) z[r][c] ^= y[r][c];   }   void cast() {     for (int r = 1; r <= n; r++)       for (int c = 1; c <= m; c++) {         if (!z[r][c]) continue;         if (!inside(r - 1, c - 1)) continue;         if (!inside(r + 1, c + 1)) continue;         val = 1;         for (int k = 0; k <= 7; k++)           if (!z[r + dr[k]][c + dc[k]]) {             val = 0;             break;           }         y[r][c] = val;       }   }   void df(int r, int c) {     ct++;     y[r][c] = 0;     for (int i = 0; i <= 7; i++) {       nr = r + dr[i], nc = c + dc[i];       if (!y[nr][nc]) continue;       df(nr, nc);     }   }   void hit() {     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         if (!y[i][j]) continue;         df(i, j);         cnt++;       }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) cin >> grid[i][j];     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) {         if (!grid[i][j]) continue;         memset(x, 0, sizeof x);         memset(y, 0, sizeof y);         memset(z, 0, sizeof z);         dfs(i, j);         cs();         cas();         memset(y, 0, sizeof y);         cast();         cnt = 0;         hit();         ans.push_back(cnt);       }     sort(ans.begin(), ans.end());     cout << ans.size() << endl;     for (auto i : ans) cout << i <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[200200], b[200200];   set<pair<long long, long long> > s;   set<pair<long long, long long> >::iterator it, it2;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n, i;     cin >> n;     for (i = 0; i < n; i++) cin >> a[i], s.insert({a[i], i});     for (it = s.begin(); it != s.end();) {       it2 = it;       it2++;       if (it2 == s.end()) break;       if ((*it).first == (*it2).first) {         s.insert({(*it).first * 2, (*it2).second});         s.erase(it2);         it2 = it;         it++;         s.erase(it2);       } else         it++;     }     for (auto x : s) {       b[x.second] = x.first;     }     cout << s.size() << endl;     for (i = 0; i < n; i++) {       if (b[i]) cout << b[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 101010 * 2;   int a[maxn], n, m;   int b[maxn];   int p[maxn];   int query(int poin, long long l, bool dir) {     int tv;     int cir;     int las;     if (dir) {       tv = upper_bound(b + 1, b + 1 + n, b[poin] + l) - b - 1;       if (tv == poin) return p[poin];       cir = l / (b[tv] - b[poin]);       las = l % (b[tv] - b[poin]);       if (cir & 1)         return query(tv, las, !dir);       else         return query(poin, las, dir);     } else {       tv = lower_bound(b + 1, b + 1 + n, b[poin] - l) - b;       if (tv == poin) return p[poin];       cir = l / (b[poin] - b[tv]);       las = l % (b[poin] - b[tv]);       if (cir & 1)         return query(tv, las, !dir);       else         return query(poin, las, dir);     }     return 0;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       b[i] = a[i];     }     sort(b + 1, b + 1 + n);     for (int i = 1; i <= n; i++) {       p[lower_bound(b + 1, b + 1 + n, a[i]) - b] = i;     }     while (m--) {       int poin, l;       scanf( %d%d , &poin, &l);       poin = lower_bound(b + 1, b + 1 + n, a[poin]) - b;       int tv = upper_bound(b + 1, b + 1 + n, b[poin] + l) - b - 1;       printf( %d n , query(tv, l - (b[tv] - b[poin]), 0));     }   }


#include <bits/stdc++.h>   using namespace std;   int64_t nearest_power_of_2(int64_t a) {     int cnt = 0;     while (a / 2 != 0) {       a = a / 2;       cnt++;     }     return cnt;   }   int main() {     int64_t t;     cin >> t;     for (int i = 0; i < t; i++) {       int64_t n;       cin >> n;       vector<int64_t> v;       for (int j = 0; j < n; j++) {         int64_t k;         cin >> k;         v.push_back(k);       }       int64_t max = 0;       int64_t b = 0;       for (int j = 0; j < n - 1; j++) {         if (v[j + 1] < v[j]) {           b++;           int64_t d = v[j] - v[j + 1];           if (max < nearest_power_of_2(d)) {             max = nearest_power_of_2(d);           }           v[j + 1] = v[j];         }       }       if (max == 0 && b == 0) {         cout << 0 << endl;       } else {         cout << max + 1 << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int maxn = 2000;   const int maxm = 1000 * 10;   struct edge {     int v, w, next;   } e[maxm * 2];   struct node {     int d, u;     bool operator<(const node &b) const { return d > b.d; }   };   int head[maxn], d[maxn], d2[maxn];   int tot;   bool vis[maxn];   int g[maxn][maxn];   void add(int u, int v, int w) {     e[tot].v = v;     e[tot].w = w;     e[tot].next = head[u];     head[u] = tot++;   }   void dijkstra(int s, int t, int n, bool dir) {     for (int i = 1; i <= n; i++) {       if (dir)         d[i] = INF;       else         d2[i] = INF;     }     if (dir)       d[s] = 0;     else       d2[s] = 0;     memset(vis, 0, sizeof(vis));     priority_queue<node> q;     q.push(node{0, s});     while (!q.empty()) {       node temp = q.top();       q.pop();       int u = temp.u;       if (vis[u]) continue;       vis[u] = 1;       for (int i = head[u]; i != -1; i = e[i].next) {         int v = e[i].v;         int w = e[i].w;         if (dir) {           if (d[v] > d[u] + w) {             d[v] = d[u] + w;             q.push(node{d[v], v});           }         } else {           if (d2[v] > d2[u] + w) {             d2[v] = d2[u] + w;             q.push(node{d2[v], v});           }         }       }     }   }   int main() {     int n, m, s, t, u, v;     cin >> n >> m >> s >> t;     memset(head, -1, sizeof(head));     for (int i = 0; i < m; i++) {       cin >> u >> v;       g[u][v] = 1;       g[v][u] = 1;       add(u, v, 1);       add(v, u, 1);     }     dijkstra(s, t, n, true);     dijkstra(t, s, n, false);     long long ans = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++)         if (!g[i][j] && i != j) {           if (d[i] + d2[j] + 1 >= d[t] && d2[i] + d[j] + 1 >= d[t]) {             ans++;             g[i][j] = g[j][i] = true;           }         }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long n, h, f[100], g[100][100];   long long fun(long long x) {     int i;     if (f[x] > -1) return 0;     f[x] = 0;     for (i = 0; i < x; ++i) {       fun(i);       fun(x - i - 1);       f[x] += f[i] * f[x - i - 1];     }     return 0;   }   long long rec(long long x, long long d) {     int i;     if (g[x][d] > -1) return 0;     g[x][d] = 0;     if (d > x) return 0;     if (d == 1) {       g[x][d] = f[x];       return 0;     }     for (i = 0; i < x; ++i) {       rec(i, d - 1);       rec(x - i - 1, d - 1);       g[x][d] += g[i][d - 1] * f[x - i - 1] + g[x - i - 1][d - 1] * f[i] -                  g[i][d - 1] * g[x - i - 1][d - 1];     }     return 0;   }   int main() {     int i, j;     cin >> n >> h;     for (i = 0; i <= n; ++i) f[i] = -1;     f[0] = 1;     for (i = 0; i <= n; ++i)       for (j = 0; j <= n; ++j) g[i][j] = -1;     g[0][0] = 1;     fun(n);     rec(n, h);     cout << g[n][h] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long r;     cin >> r;     if (r % 2 == 0 || r == 1 || r == 3)       cout <<  NO ;     else {       long long x, y;       long long up = (r - 1);       for (int i = 1; i < 100000; i++) {         if ((up / i - i - 1) % 2 == 0) {           x = i;           y = (up / i - i - 1) / 2;           break;         }       }       cout << x <<     << y;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1e9 + 7;   const long long N = 100011;   void solve() {     long long n, ans = 0;     cin >> n;     pair<long long, long long> a[n + 17];     multiset<long long> s;     for (long long i = 1; i <= n; i++) cin >> a[i].first;     for (long long i = 1; i <= n; i++) cin >> a[i].second;     sort(a + 1, a + n + 1);     long long sum = 0;     long long mn = 0;     a[n + 1].first = -1;     for (long long ind = 1; ind <= n; ind++) {       mn += 1;       if (s.empty()) mn = a[ind].first;       long long j = ind;       while (a[j].first == mn) {         s.insert(a[j].second);         sum += a[j].second;         j += 1;       }       long long mx = *(--s.end());       sum -= mx;       s.erase(s.find(mx));       ans += sum;       ind = j - 1;     }     while (s.size()) {       int mx = *(--s.end());       sum -= mx;       s.erase(s.find(mx));       ans += sum;     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) {       solve();       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b, c = 0;     string s;     cin >> n >> a >> b >> s;     bool turnA;     if (a > b)       turnA = true;     else       turnA = false;     for (int i = 0; i < s.size(); ++i) {       if (s[i] ==  . ) {         if ((i == 0 || s[i - 1] ==  * ) && a > b) {           s[i] =  a ;           c++;           a--;           turnA = false;         } else if ((i == 0 || s[i - 1] ==  * ) && a < b) {           s[i] =  b ;           c++;           b--;           turnA = true;         } else if (turnA && a > 0) {           s[i] =  a ;           c++;           a--;           turnA = false;         } else if (!turnA && b > 0) {           c++;           b--;           s[i] =  b ;           turnA = true;         } else           turnA = !turnA;       }     }     cout << c << endl;     return 0;   }


#include <bits/stdc++.h>   const int maxn = 5e5 + 5;   int fa[maxn], t[maxn], n, m, k, v, u, ev, eu;   int find(int n) { return n == fa[n] ? n : (fa[n] = find(fa[n])); }   void merge(int u, int v) {     ev = find(v);     eu = find(u);     if (ev != eu) {       fa[ev] = eu;       t[eu] += t[ev];     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) fa[i] = i, t[i] = 1;     for (int i = 0; i < m; ++i) {       scanf( %d , &k);       if (k) scanf( %d , &u);       for (int j = 0; j < k - 1; ++j) {         scanf( %d , &v);         merge(u, v);       }     }     for (int i = 1; i <= n; ++i) printf( %d  , t[find(i)]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-10;   const int inf = 0x3f3f3f3f, N = 2e5 + 10;   const double pi = acos(-1);   using namespace std;   char s[N];   int num[N];   int main() {     int n, k;     while (cin >> n >> k) {       scanf( %s , s + 1);       memset(num, 0, sizeof(num));       for (int i = 1; s[i]; i++) {         if (s[i] ==  0 )           num[i] = num[i - 1] + 1;         else           num[i] = num[i - 1];       }       int ans = inf;       int last = 0;       if (k == 1) {         for (int i = 1; i <= n; i++) {           if (!last && s[i] ==  0 )             last = i;           else if (last && s[i] ==  0 ) {             ans = i - last;             break;           }         }       }       for (int i = 1; i <= n; i++) {         if (s[i] ==  1 ) continue;         int l = 1, r = n;         while (l <= r) {           int mid = (l + r) >> 1;           int st = max(1, i - mid);           int en = min(n, i + mid);           if (num[en] - num[st - 1] >= k + 1)             r = mid - 1;           else             l = mid + 1;         }         ans = min(ans, l);       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 10;   long long a[MAXN];   long long b[MAXN];   int n, m;   long long c2(long long x) { return (x * (x - 1)) / 2; }   long long f(int l) {     if (m < l * n) {       return -1;     }     for (int i = 1; i <= n; i++) {       if (a[i] < i * l + n * c2(l)) {         return -1;       }       b[i] = a[i] - (i * l + n * c2(l));     }     long long res = 0, f = m;     for (int i = l; i >= 1; i--) {       for (int j = n; j >= 1; j--) {         b[j] += j + (i - 1) * n;         if (b[j] >= f) {           res += f;           b[j] -= f;           f--;         } else {           res += b[j];           f = b[j] - 1;           b[j] = 0;         }       }     }     return res;   }   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     int L = 1, R = (m / n) + 1;     while (R - L > 5) {       int mid = (L + R) / 2;       if (f(mid) < f(mid + 1)) {         L = mid;       } else {         R = mid;       }     }     long long res = 0;     for (int i = L; i <= R; i++) {       res = max(res, f(i));     }     cout << res;   }


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


#include <bits/stdc++.h>   using namespace std;   long long n, q, x, y, ans;   int main() {     cin >> n >> q;     while (q--) {       cin >> x >> y;       ans = 0;       if ((x & 1) == (y & 1)) {         ans += (x - 1) / 2 * n;         x = x % 2;         if (x)           ans += (y + 1) / 2;         else           ans += (y / 2) + (n / 2) + (n & 1);       } else {         ans += (n * n / 2) + (n & 1) + (x - 1) / 2 * n;         x = x % 2;         if (x)           ans += y / 2;         else           ans += (n / 2) + (y + 1) / 2;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   bool chkmax(T &x, T y) {     return x < y ? x = y, true : false;   }   template <typename T>   bool chkmin(T &x, T y) {     return x > y ? x = y, true : false;   }   int readint() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   struct node {     int val, id;     node(int val = 0, int id = 0) : val(val), id(id) {}     bool operator<(const node c) const { return val < c.val; }   };   int n, m, g, r;   int a[10005];   int d[10005][1005];   bool vis[10005][1005];   priority_queue<node> q;   int id(int x, int y) { return (x - 1) * g + y - 1; }   void dij() {     memset(d, 0x3f, sizeof(d));     q.push(node(0, id(1, g)));     d[1][g] = 0;     while (!q.empty()) {       int tid = q.top().id;       q.pop();       pair<int, int> t = make_pair(tid / g + 1, tid % g + 1);       if (vis[t.first][t.second]) continue;       if (t.first == m) {         if (t.second == g)           printf( %d n , d[t.first][t.second] - r);         else           printf( %d n , d[t.first][t.second]);         return;       }       vis[t.first][t.second] = 1;       if (t.first > 1) {         int tx = t.first - 1, ty = t.second - a[t.first] + a[t.first - 1];         if (ty > 0) {           if (chkmin(d[tx][ty],                      d[t.first][t.second] + a[t.first] - a[t.first - 1]))             q.push(node(-d[tx][ty], id(tx, ty)));         }         if (t.second == a[t.first] - a[t.first - 1]) {           ty = g;           if (chkmin(d[tx][ty],                      d[t.first][t.second] + a[t.first] - a[t.first - 1] + r))             q.push(node(-d[tx][ty], id(tx, ty)));         }       }       if (t.first < m) {         int tx = t.first + 1, ty = t.second - a[t.first + 1] + a[t.first];         if (ty > 0) {           if (chkmin(d[tx][ty],                      d[t.first][t.second] + a[t.first + 1] - a[t.first]))             q.push(node(-d[tx][ty], id(tx, ty)));         }         if (t.second == a[t.first + 1] - a[t.first]) {           ty = g;           if (chkmin(d[tx][ty],                      d[t.first][t.second] + a[t.first + 1] - a[t.first] + r))             q.push(node(-d[tx][ty], id(tx, ty)));         }       }     }     printf( -1 n );   }   int main() {     n = readint();     m = readint();     for (int i = 1; i <= m; i++) a[i] = readint();     sort(a + 1, a + m + 1);     g = readint();     r = readint();     dij();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2005;   bool vis[N];   pair<int, int> pt[N];   int dp[N];   int main() {     memset(dp, 0x3f, sizeof(dp));     int a, m, n;     cin >> a >> n >> m;     for (int i = 1; i <= n; i++) {       int l, r;       scanf( %d %d , &l, &r);       for (int j = l + 1; j <= r; j++) {         vis[j] = true;       }     }     for (int i = 1; i <= m; i++) {       scanf( %d %d , &pt[i].first, &pt[i].second);     }     sort(pt + 1, pt + 1 + m);     for (int i = 1; i <= pt[1].first; i++) {       if (vis[i] == true) {         puts( -1 );         return 0;       }     }     dp[0] = 0;     int cnt = 1;     for (int i = 1; i <= a; i++) {       while (cnt <= m && pt[cnt].first < i) cnt++;       if (vis[i] == false)         dp[i] = dp[i - 1];       else {         for (int j = 1; j < cnt; j++) {           dp[i] = min(dp[i], dp[pt[j].first] + pt[j].second * (i - pt[j].first));         }       }     }     printf( %d n , dp[a]);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC target( avx2 )   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   using namespace std;   template <class T, class U>   ostream &operator<<(ostream &os, const pair<T, U> &p) {     os <<  (  << p.first <<  ,  << p.second <<  ) ;     return os;   }   template <class T>   ostream &operator<<(ostream &os, const vector<T> &v) {     os <<  { ;     for (int i = 0; i < (int)v.size(); i++) {       if (i) {         os <<  , ;       }       os << v[i];     }     os <<  } ;     return os;   }   ostream &operator<<(ostream &os, const set<int> &se) {     os <<  { ;     int now = 0;     for (auto x : se) {       if (now) {         os <<  , ;       }       os << x;       now++;     }     os <<  } ;     return os;   }   void debugg() { cerr << endl; }   template <class T, class... Args>   void debugg(const T &x, const Args &...args) {     cerr <<     << x;     debugg(args...);   }   using namespace std;   const int inf = (1 << 30) - 1;   const long long linf = 1LL << 61;   const int MAX = 510000;   int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};   int dx[8] = {-1, 0, 1, 0, 1, -1, 1, -1};   const double pi = acos(-1);   const double eps = 1e-8;   template <typename T1, typename T2>   inline bool chmin(T1 &a, T2 b) {     if (a > b) {       a = b;       return true;     } else       return false;   }   template <typename T1, typename T2>   inline bool chmax(T1 &a, T2 b) {     if (a < b) {       a = b;       return true;     } else       return false;   }   template <typename T>   inline void print(T &a) {     int sz = a.size();     for (auto itr = a.begin(); itr != a.end(); itr++) {       cout << *itr;       sz--;       if (sz) cout <<    ;     }     cout <<   n ;   }   template <typename T1, typename T2>   inline void print2(T1 a, T2 b) {     cout << a <<     << b <<   n ;   }   template <typename T1, typename T2, typename T3>   inline void print3(T1 a, T2 b, T3 c) {     cout << a <<     << b <<     << c <<   n ;   }   void mark() {     cout <<  #           <<   n ;   }   long long pcount(long long x) { return __builtin_popcountll(x); }   const int mod = 998244353;   template <typename T>   struct BIT {     vector<T> dat;     long long sz;     BIT(long long sz) : sz(sz) { dat.assign(++sz, 0); }     T sum(long long k) {       T ret = 0;       for (++k; k > 0; k -= k & -k) ret += dat[k];       return (ret);     }     void add(long long k, T x) {       for (++k; k < dat.size(); k += k & -k) dat[k] += x;     }     long long get(T k) {       if (k <= 0) return 0;       long long ret = 0;       int n = 1;       while (n < sz) n *= 2;       for (int i = n / 2; i > 0; i /= 2) {         if (ret + i < sz && dat[ret + i] < k) {           k -= dat[ret + i];           ret += i;         }       }       return ret;     }   };   void solve() {     int n, m;     cin >> n >> m;     vector<pair<long long, long long>> a(n * m);     vector<long long> b(n * m);     for (int i = 0; i < (n * m); i++) {       cin >> a[i].first;       a[i].second = i;       b[i] = a[i].first;     }     sort((a).begin(), (a).end());     vector<vector<long long>> c(n, vector<long long>(m));     for (int i = 0; i < (n); i++)       for (int j = 0; j < (m); j++) {         c[i][j] = a[i * m + j].second;       }     for (int i = 0; i < (n); i++)       for (int j = 0; j < (m); j++) {         int k = j + 1;         while (k < m && b[c[i][k]] == b[c[i][j]]) k++;         reverse(c[i].begin() + j, c[i].begin() + k);         j = k - 1;       }     vector<pair<long long, long long>> pos(n * m);     for (int i = 0; i < (n); i++)       for (int j = 0; j < (m); j++) {         pos[c[i][j]] = {i, j};       }     long long ans = 0;     vector<BIT<long long>> bit(n, BIT<long long>(m));     for (int i = 0; i < (n * m); i++) {       ans += bit[pos[i].first].sum(pos[i].second);       bit[pos[i].first].add(pos[i].second, 1);     }     cout << ans <<   n ;   }   int main() {     cin.tie(nullptr);     ios::sync_with_stdio(false);     int t = 1;     cin >> t;     while (t--) {       solve();     }   }


#include<bits/stdc++.h>   #define ll long long   #define R return   #define pb push_back   #define SI size()      using namespace std;      long long t,n,a[100030],ans,w[5030];      int main(){       cin >> t;       while(t--){           ans = 0;           memset(w,0,sizeof w);           cin >> n;           for(int i=0;i<n;i++)               cin >> a[i];           for(int i=0;i<n;i++){               if(a[i] - w[i] > 1)                   ans += a[i] - w[i] - 1;               if(a[i] - w[i] < 1)                   w[i+1] += 1 - a[i] + w[i];               if(a[i] > 1)                   for(int j=i+2;j<=min(i+a[i],n);j++) w[j]++;           }           cout << ans << endl;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void _R(T &x) {     cin >> x;   }   void _R(int &x) { scanf( %d , &x); }   void _R(int64_t &x) { scanf( %lld , &x); }   void _R(double &x) { scanf( %lf , &x); }   void _R(char &x) { scanf(  %c , &x); }   void _R(char *x) { scanf( %s , x); }   void R() {}   template <class T, class... U>   void R(T &head, U &...tail) {     _R(head);     R(tail...);   }   template <class T>   void _W(const T &x) {     cout << x;   }   void _W(const int &x) { printf( %d , x); }   void _W(const int64_t &x) { printf( %lld , x); }   void _W(const double &x) { printf( %.16f , x); }   void _W(const char &x) { putchar(x); }   void _W(const char *x) { printf( %s , x); }   template <class T, class U>   void _W(const pair<T, U> &x) {     _W(x.first);     putchar(   );     _W(x.second);   }   template <class T>   void _W(const vector<T> &x) {     for (auto i = x.begin(); i != x.end(); _W(*i++))       if (i != x.cbegin()) putchar(   );   }   void W() {}   template <class T, class... U>   void W(const T &head, const U &...tail) {     _W(head);     putchar(sizeof...(tail) ?     :   n );     W(tail...);   }   template <class T, class... U>   void DEBUG(const T &head, const U &...tail) {}   int MOD = 1e9 + 7;   void ADD(long long &x, long long v) {     x = (x + v) % MOD;     if (x < 0) x += MOD;   }   const int SIZE = 5e5 + 10;   long long mypow(long long x, long long y) {     x %= MOD;     long long res = 1 % MOD;     while (y) {       if (y & 1) res = res * x % MOD;       y >>= 1;       x = x * x % MOD;     }     return res;   }   int ll[2][SIZE];   int d[SIZE], dn;   vector<pair<int, int> > seg[2];   template <class T>   void maa(T &x, T y) {     if (x < y) x = y;   }   template <class T>   void mii(T &x, T y) {     if (x > y) x = y;   }   int get_v(int x) { return lower_bound(d, d + dn, x) - d; }   int get_len(int x) { return d[x] - d[x - 1]; }   struct BIT {     int _n;     long long _d[SIZE];     void init(int __n) {       _n = __n;       memset(_d, 0, sizeof(long long) * (_n + 1));     }     void ins(int x, long long v) {       x++;       for (; x <= _n; x += x & -x) ADD(_d[x], v);     }     long long qq(int x) {       x++;       long long res = 0;       for (; x; x -= x & -x) ADD(res, _d[x]);       return res;     }     long long qq_range(int x, int y) {       long long ret = qq(y);       if (x) ADD(ret, -qq(x - 1));       return ret;     }   } bit[2];   int main() {     int K, N, M;     R(K, N, M);     if (N + M == 0) {       W(mypow(2, K));       return 0;     }     d[dn++] = 0;     for (int i = 0; i < (N); ++i) {       int x, y;       R(x, y);       d[dn++] = x - 1;       d[dn++] = y;       seg[0].push_back(make_pair(x - 1, y));     }     for (int i = 0; i < (M); ++i) {       int x, y;       R(x, y);       d[dn++] = x - 1;       d[dn++] = y;       seg[1].push_back(make_pair(x - 1, y));     }     sort(d, d + dn);     dn = unique(d, d + dn) - d;     for (int j = 0; j < (2); ++j) {       for (int i = 0; i < (((int)(seg[j]).size())); ++i) {         maa(ll[j][get_v(seg[j][i].second)], get_v(seg[j][i].first) + 1);       }     }     for (int i = 0; i < (2); ++i) bit[i].init(dn + 2);     bit[0].ins(1, mypow(2, get_len(1)) - 2);     if (ll[0][1] == 0) {       bit[1].ins(0, 1);     }     if (ll[1][1] == 0) {       bit[0].ins(0, 1);     }     int bb[2] = {};     for (int i = (2); i < (dn); ++i) {       long long both[2] = {};       int len = get_len(i);       for (int j = 0; j < (2); ++j) ADD(both[j], bit[j].qq(i - 1));       while (bb[0] < ll[1][i]) {         long long v = bit[0].qq_range(bb[0], bb[0]);         bit[0].ins(bb[0], -v);         bb[0]++;       }       if (ll[1][i] != i) bit[0].ins(i - 1, both[1]);       while (bb[1] < ll[0][i]) {         long long v = bit[1].qq_range(bb[1], bb[1]);         bit[1].ins(bb[1], -v);         bb[1]++;       }       if (ll[0][i] != i) bit[1].ins(i - 1, both[0]);       bit[0].ins(i, (both[0] + both[1]) * (mypow(2, len) - 2) % MOD);     }     long long ans = 0;     for (int i = 0; i < (2); ++i) ADD(ans, bit[i].qq(dn));     W(ans * mypow(2, (K - d[dn - 1])) % MOD);     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:60000000 )   using namespace std;   const long double eps = 1e-9;   const int inf = (1 << 30) - 1;   const long long inf64 = ((long long)1 << 62) - 1;   const long double pi = 3.1415926535897932384626433832795;   const string task =   ;   template <class T>   T sqr(T x) {     return x * x;   }   int n, m;   int a[100500];   int last[100500];   char ans[100500];   int x[100500], y[100500];   void solve() {     scanf( %d%d , &n, &m);     for (int i = 0; i < (int)(m); i++) {       scanf( %d , &a[i]);       ans[i] =  N ;       last[i] = -1;     }     for (int i = 0; i < (int)(n - 1); i++) {       scanf( %d%d , &x[i], &y[i]);       x[i]--;       if (x[i] >= 0) last[x[i]] = i;     }     int s = 0;     bool first = 1;     for (int i = 0; i < (int)(n - 1); i++) {       if (first && y[i] == 1) {         first = 0;         int mn = inf;         for (int j = 0; j < (int)(m); j++)           if (a[j] <= s && last[j] < i) {             ans[j] =  Y ;             mn = min(mn, a[j]);           }         s -= mn;       }       if (x[i] != -1) {         a[x[i]]--;       } else {         s++;       }     }     cerr << s << endl;     for (int i = 0; i < (int)(m); i++)       if (a[i] <= s) ans[i] =  Y ;     for (int i = 0; i < (int)(m); i++) {       printf( %c , ans[i]);     }     printf(  n );   }   int main() {     int tt;     scanf( %d , &tt);     for (int ii = 0; ii < (int)(tt); ii++) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5, oo = 0x3f3f3f3f;   pair<int, int> stop[maxn];   int nxt[maxn];   int main(void) {     int d, n, m;     scanf( %d%d%d , &d, &n, &m);     int x, y;     for (int i = 1; i <= m; i++) {       scanf( %d%d , &x, &y);       stop[i] = pair<int, int>(x, y);     }     stop[0] = pair<int, int>(0, 0);     stop[m + 1] = pair<int, int>(d, 0);     sort(stop, stop + m + 1);     stack<int> s;     memset(nxt, -1, sizeof(nxt));     for (int i = m; i >= 1; i--) {       while (!s.empty() && stop[i].second <= stop[s.top()].second) s.pop();       if (s.empty())         nxt[i] = m + 1;       else         nxt[i] = s.top();       s.push(i);     }     nxt[0] = s.top();     int after;     long long ans = 0;     int now = n;     int dist;     for (int i = 0; i <= m + 1; i++) {       if (now < 0) return puts( -1 ), 0;       dist = stop[nxt[i]].first - stop[i].first;       if (dist > now)         after = dist;       else         after = now;       if (after > n) after = n;       ans += (after - now) * 1ll * stop[i].second;       after -= stop[i + 1].first - stop[i].first;       now = after;     }     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool cmp(int a, int b) { return a > b; }   int h[2005];   int main() {     int i, n, a, b, ans = 0;     scanf( %d %d %d , &n, &a, &b);     for (i = 0; i < n; ++i) scanf( %d , &h[i]);     sort(h, h + n, cmp);     if (h[a - 1] > h[a]) ans = h[a - 1] - h[a];     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200100;   int t, n;   int MOD;   vector<pair<long long int, long long int> > v[N];   long long int kol[N];   long long int gotov[N], sad, a, b;   long long int c;   long long int sol;   vector<long long int> p;   long long int FI;   long long int inv[N], inv1[N];   long long int powmod(long long int a, long long int x) {     long long int rez = 1;     while (x) {       if (x % 2) {         rez = ((rez % MOD) * (a % MOD)) % MOD;       }       a = ((a % MOD) * (a % MOD)) % MOD;       x /= 2;     }     return rez % MOD;   }   long long int divide(long long int a, long long int dub) {     return a * inv[dub] % MOD;   }   int djeca(int node, int par) {     kol[node] = 0;     for (int i = 0; i < v[node].size(); i++) {       if (v[node][i].first != par && !gotov[v[node][i].first])         kol[node] += djeca(v[node][i].first, node);     }     kol[node]++;     return kol[node];   }   int centr(int node, int par, int sz) {     for (int i = 0; i < v[node].size(); i++) {       if (v[node][i].first != par && !gotov[v[node][i].first] &&           kol[v[node][i].first] > sz / 2) {         return centr(v[node][i].first, node, sz);       }     }     return node;   }   map<long long int, long long int> m;   map<long long int, long long int> pom[N];   void dfs(int node, int par, long long int x, long long int y, int d, int turn) {     int di = ((-y) % MOD + MOD) % MOD;     di = divide(di, d) % MOD;     if (par != -1) sol += (m[di] - pom[turn][di]);     if (!x && par != -1) sol++;     if (!y && par != -1) sol++;     for (int i = 0; i < v[node].size(); i++) {       if (v[node][i].first != par && !gotov[v[node][i].first]) {         int kamo;         if (par == -1)           kamo = i;         else           kamo = turn;         long long int novix = ((v[node][i].second * inv1[d]) % MOD + x) % MOD;         long long int noviy = ((y * 10) % MOD + v[node][i].second) % MOD;         dfs(v[node][i].first, node, novix, noviy, d + 1, kamo);         if (par == -1) pom[i].clear();       }     }     return;   }   void broji(int node, int par, long long int x, int d, int turn) {     if (par != -1) {       m[x % MOD]++;       pom[turn][x % MOD]++;     }     for (int i = 0; i < v[node].size(); i++) {       if (v[node][i].first != par && !gotov[v[node][i].first]) {         int kamo;         if (par == -1)           kamo = i;         else           kamo = turn;         long long int novix = ((v[node][i].second * inv1[d]) % MOD + x) % MOD;         broji(v[node][i].first, node, novix, d + 1, kamo);       }     }     return;   }   void decomp(int node, int par) {     kol[node] = djeca(node, par);     int sad = centr(node, par, kol[node]);     broji(sad, -1, 0, 0, 0);     dfs(sad, -1, 0, 0, 0, 0);     m.clear();     gotov[sad] = 1;     for (int i = 0; i < v[sad].size(); i++) {       if (!gotov[v[sad][i].first]) decomp(v[sad][i].first, node);     }     return;   }   void solve() {     cin >> n >> MOD;     for (int i = 0; i < n - 1; i++) {       cin >> a >> b >> c;       v[a].push_back(make_pair(b, c));       v[b].push_back(make_pair(a, c));     }     long long int k = MOD;     long long int result = MOD;     for (int p = 2; p * p <= k; ++p) {       if (k % p == 0) {         while (k % p == 0) k /= p;         result -= result / p;       }     }     if (k > 1) result -= result / k;     FI = result;     for (int i = 0; i <= n; i++) {       inv1[i] = powmod(10, i);     }     for (int i = 0; i <= n; i++) {       inv[i] = powmod(inv1[i], FI - 1);     }     decomp(0, -1);     cout << sol << endl;     return;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool debug = 0;   long long a[26], dp[26][26];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int tc = 1;     while (tc--) {       string s;       cin >> s;       int n = s.length();       long long mx = 0;       for (int i = 0; i < n; i++) {         int x = s[i] -  a ;         for (int j = 0; j < 26; j++) {           dp[j][x] += a[j];           mx = mx > dp[j][x] ? mx : dp[j][x];         }         a[x]++;         mx = mx > a[x] ? mx : a[x];       }       cout << mx;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool pal(string& s) {     int j = s.size() - 1;     int i = 0;     while (i < j) {       if (s[i++] != s[j--]) return false;     }     return true;   }   int main() {     string s;     cin >> s;     int n = s.size();     for (int len = n; len >= 0; len--) {       for (int start = 0; start + len - 1 < n; start++) {         string sub = s.substr(start, len);         if (pal(sub))           continue;         else {           cout << len << endl;           return 0;         }       }     }     cout << 0 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int MOD = 1e9 + 7;   bool isPoTwo(long long int x) { return x && (!(x & (x - 1))); }   long long int power(long long int x, unsigned long long int y,                       long long int p) {     long long int res = 1;     x = x % p;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   void solve() {     long long int n, x, y, d;     cin >> n >> x >> y >> d;     long long int ans = INT_MAX, ans1 = INT_MAX, ans2 = INT_MAX;     if ((y - x) % d == 0) ans = (abs(y - x) / d);     if ((y - 1) % d == 0 || (n - y) % d == 0) {       if ((y - 1) % d == 0) {         ans1 = (y - 1) / d;         ans1 += ((x - 1) / d) + 1;       }       if ((n - y) % d == 0) {         ans2 = (n - y) / d;         ans2 += ((n - x) / d) + 1;       }       ans = min(ans, ans2);       ans = min(ans, ans1);     }     if (ans == INT_MAX) ans = -1;     cout << ans <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int t;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, mn = INT_MAX, ans = INT_MAX;     cin >> n;     map<int, int> mp;     map<int, int> mp_ans;     for (int i = 1; i <= n; i++) {       cin >> a;       if (mp[a] == 0)         mp[a] = i;       else {         if (a <= mn) {           mn = a;           if (mp_ans[a] != 0) {             ans = min(mp_ans[a], i - mp[a]);             mp_ans[a] = ans;           } else {             mp_ans[a] = i - mp[a];           }         }         mp[a] = i;       }     }     cout << mp_ans[mn];   }


#include <bits/stdc++.h>   using namespace std;   long long pow(int a, int b, long long MOD) {     long long x = 1, y = a;     while (b > 0) {       if (b % 2 == 1) {         x = (x * y);         if (x > MOD) x %= MOD;       }       y = (y * y);       if (y > MOD) y %= MOD;       b /= 2;     }     return x;   }   long long InverseEuler(int n, long long MOD) { return pow(n, MOD - 2, MOD); }   int hcf(int n1, int n2) {     if (n2 != 0)       return hcf(n2, n1 % n2);     else       return n1;   }   int p[100090] = {};   void prime() {     for (int i = 2; i * i <= 100009; i++)       if (p[i] == 0)         for (int j = i + i; j <= 100009; j += i) p[j] = 1;     p[0] = 1;     p[1] = 1;   }   int main() {     int a[100002];     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     int b[100002], c[100002], f = -1;     for (int i = 1; i < n; i++) {       b[i] = abs(a[i] - a[i - 1]) * f;       f *= -1;     }     f = -1;     for (int i = 1; i < n; i++) {       c[i] = b[i] * -1;     }     long long sum = 0, mx = INT_MIN, k = 1;     for (int i = 1; i < n; i++) {       sum += b[i];       if (sum < 0) sum = 0;       mx = max(mx, sum);     }     sum = 0;     long long mx1 = INT_MIN, j = 1;     for (int i = 1; i < n; i++) {       sum += c[i];       if (sum < 0) sum = 0;       mx1 = max(mx1, sum);     }     cout << max(mx, mx1);   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   int fact(int n) {     long long ans = n;     for (int i = n - 1; i > 0; --i) {       ans = (ans * i) % MOD;     }     return ans;   }   int pow2(int n) {     long long ans = 1, b = 2;     while (n > 0) {       if (n % 2) {         ans = (ans * b) % MOD;       }       b = (b * b) % MOD;       n /= 2;     }     return ans;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     cout << (fact(n) - pow2(n - 1) + MOD) % MOD;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000 + 10;   vector<int> v[maxn];   int dp[maxn];   int a[10][maxn];   int main() {     int n, K;     cin >> n >> K;     for (int i = 0; i < K; i++) {       for (int j = 0; j < n; j++) scanf( %d , &a[i][j]), v[a[i][j]].push_back(j);     }     memset(dp, 0, sizeof(dp));     int ans = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < i; j++) {         int p = 0;         for (int k = 0; k < K; k++)           if (v[a[0][j]][k] > v[a[0][i]][k]) {             p = 1;             break;           }         if (p == 0) dp[i] = max(dp[i], dp[j]);       }       dp[i]++;       ans = max(ans, dp[i]);     }     printf( %d n , ans);     return 0;   }


#include <iostream>   #include <cmath>   #include <vector>   #include <algorithm>   #include <set>   #include <cstdio>   #include <map>   #include <queue>   #include <ctime>   #include <iomanip>   #include <stack>   #include <assert.h>   #include <unordered_map>      using namespace std;      #define F first   #define ll long long   #define S second   #define ld long double   #define all(x) x.begin(), x.end()   #define rep(i, n) for (int i = 0; i < (int)n; ++i)   #define PI acos(-1)      ll a, b, c, d, e, fl, maxn, cnt, k, s;   vector <ll> ar;            int main()   {       ios_base::sync_with_stdio(0);       cin.tie(0);       cout.tie(0);       ll t = 1;       cin >> t;       while(t--)       {           cin >> a;           ll last_time = -1;           ll last_pos = 0;           ll nap = 0;           ll pos = 0;           ll ans = 0;           ll lst = 0, lsp = 0;           queue <pair <ll, ll> > q;           rep(i, a + 1)           {               ll p, p1;               if (i != a)                   cin >> p >> p1;               else                   p = (ll)1e15;               if (!q.empty())               {                   pair <ll, ll> pp = q.front();                   q.pop();                   ll st = last_pos + nap * (pp.F - last_time);                   ll fin = last_pos + nap * (min(p, lst) - last_time);                   //cout << i <<     << st <<     << fin <<     << pp.S <<   n ;                   if (min(st, fin) <= pp.S && max(st, fin) >= pp.S)                       ++ans;               }               if (i == a)                   continue;               if (p >= lst)               {                   last_time = p;                   last_pos = lsp;                   lsp = p1;                   lst = abs(last_pos - p1) + p;                   if (last_pos > p1)                       nap = -1;                   else                       nap = 1;               }               q.push({p, p1});           }           cout << ans <<   n ;       }   }


#include <bits/stdc++.h>   using namespace std;   long long a[1005], b[1005];   int main() {     int n, l, r;     cin >> n;     long long bans = 1, aans = 1, mx = 0;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     for (int l = 0; l < n; l++) {       for (int r = l; r < n; r++) {         aans = 0;         bans = 0;         for (int i = l; i <= r; i++) {           aans |= a[i];           bans |= b[i];           mx = max(mx, aans + bans);         }       }     }     cout << mx << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, x, y, col[111111], used[111111], size;   vector<int> g[111111], L, R, mat[111111];   void paint(int id, int c) {     col[id] = c;     c == 1 ? L.push_back(id) : R.push_back(id);     for (vector<int>::iterator ii = g[id].begin(); ii != g[id].end(); ii++)       if (!col[*ii]) paint(*ii, c ^ 3);   }   void construct() {     int now = ++size, cnt = 0;     for (vector<int>::iterator ii = L.begin(); ii != L.end(); ii++) {       if (used[*ii]) continue;       used[*ii] = now;       cnt++;       if (cnt == 3) {         cnt = 0;         now = ++size;       }     }     for (vector<int>::iterator ii = R.begin(); ii != R.end(); ii++) {       if (used[*ii]) continue;       used[*ii] = now;       cnt++;       if (cnt == 3) {         cnt = 0;         now = ++size;       }     }   }   void print() {     printf( YES n );     for (int i = 1; i <= n; i++) printf( %d  , used[i]);   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d%d , &x, &y);       g[x].push_back(y);       g[y].push_back(x);     }     for (int i = 1; i <= n; i++)       if (!col[i]) paint(i, 1);     if (L.size() % 3 == 0 && R.size() % 3 == 0) {       construct();       print();       return 0;     } else {       if (L.size() % 3 == 2 && R.size() % 3 == 1) L.swap(R);       sort(L.begin(), L.end());       sort(R.begin(), R.end());       for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end());       for (vector<int>::iterator ii = L.begin(); ii != L.end(); ii++)         if (R.size() - g[*ii].size() > 1) {           int cnt = 0;           used[*ii] = 1;           vector<int>::iterator pp = g[*ii].begin();           for (vector<int>::iterator jj = R.begin(); jj != R.end() && cnt < 2;                jj++) {             while (pp != g[*ii].end() && *jj > *pp) pp++;             if (pp != g[*ii].end() && *pp == *jj) continue;             used[*jj] = 1;             cnt++;           }           size = 1;           construct();           print();           return 0;         }       for (vector<int>::iterator ii = R.begin(); ii != R.end(); ii++)         if (L.size() - g[*ii].size() > 1) {           int cnt = 0;           vector<int>::iterator pp = g[*ii].begin();           for (vector<int>::iterator jj = L.begin(); jj != L.end() && cnt < 2;                jj++) {             while (pp != g[*ii].end() && *jj > *pp) pp++;             if (pp != g[*ii].end() && *pp == *jj) continue;             cnt++;             mat[*ii].push_back(*jj);           }         }       for (vector<int>::iterator ii = R.begin(); ii != R.end(); ii++)         if (mat[*ii].size() > 1)           for (vector<int>::iterator jj = ii + 1; jj != R.end(); jj++)             if (mat[*jj].size() > 1) {               used[*ii] = used[mat[*ii][0]] = used[mat[*ii][1]] = 1;               used[*jj] = used[mat[*jj][0]] = used[mat[*jj][1]] = 2;               size = 2;               construct();               print();               return 0;             }     }     printf( NO n );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long m, sum, p;     cin >> m;     for (int i = 5; i < 400006; i++) {       p = 5, sum = 0;       while (i / p) sum += i / p, p *= 5;       if (sum == m) {         cout <<  5 n               << i <<     << i + 1 <<     << i + 2 <<     << i + 3 <<     << i + 4;         return 0;       }     }     cout << 0;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using lint = long long;   using ulint = unsigned long long;   const int N = 1500 + 1;   int a[N];   int bit[N];   int back[N];   int lsb(int x) { return x & -x; }   int query(int r) {     int res = 0;     for (; r > 0; r -= lsb(r)) {       res = max(res, bit[r]);     }     return res;   }   void update(int r, int k, int id) {     for (; r < N; r += lsb(r)) {       if (k > bit[r]) {         bit[r] = k;         back[r] = id;       }     }   }   int main(void) {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       a[i] += a[i - 1];     }     map<int, vector<pair<int, int>>> sums;     for (int l = 1; l <= n; l++) {       for (int r = l; r <= n; r++) {         sums[a[r] - a[l - 1]].emplace_back(l, r);       }     }     int best = -1, best_sum = -1;     for (auto &sum : sums) {       fill(begin(bit), end(bit), 0);       auto &vec = sum.second;       for (int i = 0; i < vec.size(); i++) {         auto [l, r] = vec[i];         update(r, query(l - 1) + 1, i);       }       if (query(n) > best) {         best = query(n);         best_sum = sum.first;       }     }     cout << best <<   n ;     fill(begin(bit), end(bit), 0);     fill(begin(back), end(back), -1);     auto &vec = sums[best_sum];     for (int i = 0; i < vec.size(); i++) {       auto [l, r] = vec[i];       int k = query(l - 1) + 1;       update(r, k, i);       if (k == best) {         l = n + 1;         for (int j = r; j >= 0; j--) {           if (best > 0 && bit[j] == best && vec[back[j]].second < l) {             cout << vec[back[j]].first <<     << vec[back[j]].second <<   n ;             best--;             l = vec[back[j]].first;           }         }         break;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   int ma[10][10];   bool vis[20];   int isok[20][20], tmp[20][20];   int n, m, ok = 0;   int dx[] = {1, 0, -1, 0};   int dy[] = {0, 1, 0, -1};   void solve() {     puts( YES );     if (n > 4) {       for (int i = 1; i <= m; i++) {         if (i % 2) {           for (int j = 1; j <= n; j += 2) printf( %d  , (i - 1) * n + j);           for (int j = 2; j <= n; j += 2) printf( %d  , (i - 1) * n + j);         } else {           for (int j = 2; j <= n; j += 2) printf( %d  , (i - 1) * n + j);           for (int j = 1; j <= n; j += 2) printf( %d  , (i - 1) * n + j);         }         puts(  );       }     } else {       int s1 = 1;       int s2 = 2;       for (int i = 1; i <= m; i++) {         for (int j = 1; j <= n; j++) {           if (j % 2) {             if (s1 > m) s1 = 2;             printf( %d  , (s1 - 1) * n + j);           } else {             if (s2 > m) s2 = 1;             printf( %d  , (s2 - 1) * n + j);           }         }         s1 += 2;         s2 += 2;         puts(  );       }     }   }   void init() {     for (int i = 1; i < (m + 1); ++i)       for (int j = 1; j < (n + 1); ++j) tmp[i][j] = (i - 1) * n + j;     for (int i = 1; i < (n * m + 1); ++i)       for (int j = 1; j < (m * n + 1); ++j) isok[i][j] = 1;     for (int i = 1; i < (m + 1); ++i)       for (int j = 1; j < (n + 1); ++j) {         for (int k = 0; k < (4); ++k) {           int xx = i + dx[k];           int yy = j + dy[k];           if (xx < 1 || xx > m || yy < 1 || yy > n)             continue;           else             isok[tmp[xx][yy]][tmp[i][j]] = isok[tmp[i][j]][tmp[xx][yy]] = 0;         }       }   }   void dfs(int dep) {     if (dep == n * m || ok) {       ok = 1;       return;     } else {       for (int i = 1; i < (n * m + 1); ++i) {         int flag = 1;         if (vis[i])           continue;         else {           int flag = 1;           ++dep;           int row = (dep - 1) / n + 1;           int col = (dep - 1) % n + 1;           if (row > 1) {             int nxt1 = ma[row - 1][col];             if (!isok[nxt1][i]) flag = 0;           }           if (col > 1) {             int nxt2 = ma[row][col - 1];             if (!isok[nxt2][i]) flag = 0;           }           if (flag == 0) {             dep--;             continue;           }           ma[row][col] = i;           vis[i] = 1;           dfs(dep);           if (ok) return;           ma[row][col] = 0;           vis[i] = 0;           --dep;         }       }     }   }   int main() {     scanf( %d%d , &m, &n);     if (max(n, m) > 4)       solve();     else {       init();       for (int i = 1; i < (n * m + 1); ++i)         if (!ok) {           memset(vis, 0, sizeof(vis));           memset(ma, 0, sizeof(ma));           vis[i] = 1;           ma[1][1] = i;           dfs(1);         }       if (!ok)         printf( NO );       else {         puts( YES );         for (int i = 1; i < (m + 1); ++i) {           for (int j = 1; j < (n + 1); ++j) printf( %d  , ma[i][j]);           puts(  );         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     for (int i = 0; i < 10; i++) {       cout << i << endl;       getline(cin, s);       if (s ==  great  || s ==  cool  || s ==  not bad  ||           s ==  don t touch me  || s ==  don t think so ) {         cout <<  normal n ;         return 0;       } else if (s ==  don t even  || s ==  are you serious  || s ==  no way  ||                  s ==  worse  || s ==  terrible  || s ==  go die in a hole ) {         cout <<  grumpy n ;         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<long long, pair<long long, long long> > mp;   pair<long long, long long> dfs(long long x) {     if (x == 0) return make_pair(0, 0);     if (mp.count(x)) return mp[x];     long long t = pow(x + 0.5, 1.0 / 3);     pair<long long, long long> res = dfs(t * t * t - 1);     pair<long long, long long> tmp = dfs(x - t * t * t);     res = max(res, make_pair(tmp.first + 1, tmp.second + t * t * t));     mp[x] = res;     return res;   }   int main() {     long long m;     scanf( %lld , &m);     pair<long long, long long> ans = dfs(m);     printf( %lld %lld n , ans.first, ans.second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a, b, m, t;   long long sum(int l, int r) {     long long sl = a + (l - 1) * b;     long long sr = a + (r - 1) * b;     return (sl + sr) * (r - l + 1) / 2;   }   bool chk(int l, int x) {     return a + 1LL * (x - 1) * b <= 1LL * t && sum(l, x) <= 1LL * m * t;   }   int main(void) {     int q;     cin >> a >> b >> q;     while (q--) {       int l;       cin >> l >> t >> m;       int lo = l - 1, hi = 1000000000;       while (lo != hi) {         int mi = (lo + hi + 1) / 2;         if (chk(l, mi))           lo = mi;         else           hi = mi - 1;       }       if (lo < l)         cout <<  -1 n ;       else         cout << lo <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( -O3 )   using namespace std;   template <typename T1, typename T2>   ostream& operator<<(ostream& os, pair<T1, T2> p) {     cout <<  (  << p.first <<  ,   << p.second <<  ) ;     return os;   }   template <typename T>   ostream& operator<<(ostream& os, vector<T>& vec) {     os <<  { ;     for (int i = 0; i < vec.size(); i++)       os << vec[i] << (i + 1 == vec.size() ?    :  ,  );     os <<  } ;     return os;   }   template <typename T>   ostream& operator<<(ostream& os, set<T>& st) {     cout <<  { ;     for (auto itr = st.begin(); itr != st.end(); itr++)       cout << *itr << (next(itr) != st.end() ?  ,   :   );     cout <<  } ;     return os;   }   template <typename T1, typename T2>   ostream& operator<<(ostream& os, map<T1, T2> mp) {     cout <<  { ;     for (auto itr = mp.begin(); itr != mp.end(); itr++)       cout <<  (  << (itr->first) <<  ,   << (itr->second) <<  )             << (next(itr) != mp.end() ?  ,  :   );     cout <<  } ;     return os;   }   void dump_func() { cout << endl; }   template <class Head, class... Tail>   void dump_func(Head&& head, Tail&&... tail) {     cout << head;     if (sizeof...(Tail) == 0) {       cout <<    ;     } else {       cout <<  ,  ;     }     dump_func(std::move(tail)...);   }   template <class T>   inline bool chmax(T& a, T const& b) {     if (a >= b) return false;     a = b;     return true;   }   template <class T>   inline bool chmin(T& a, T const& b) {     if (a <= b) return false;     a = b;     return true;   }   void _main();   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     _main();     return 0;   }   using ll = long long;   using P = pair<int, int>;   using Pl = pair<ll, ll>;   using vi = vector<int>;   using vvi = vector<vi>;   using vl = vector<ll>;   using vvl = vector<vl>;   const int mod = 1e9 + 7, INF = 1 << 29;   const double EPS = 1e-5, PI = 3.1415926535897932384626;   const ll lmod = 1e9 + 7, LINF = 1LL << 59;   template <int MOD>   struct ModInt {     static const int Mod = MOD;     unsigned x;     ModInt() : x(0) {}     ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }     ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }     int get() const { return (int)x; }     ModInt& operator+=(ModInt that) {       if ((x += that.x) >= MOD) x -= MOD;       return *this;     }     ModInt& operator-=(ModInt that) {       if ((x += MOD - that.x) >= MOD) x -= MOD;       return *this;     }     ModInt& operator*=(ModInt that) {       x = (unsigned long long)x * that.x % MOD;       return *this;     }     ModInt& operator/=(ModInt that) { return *this *= that.inverse(); }     ModInt operator+(ModInt that) const { return ModInt(*this) += that; }     ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }     ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }     ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }     ModInt inverse() const {       long long a = x, b = MOD, u = 1, v = 0;       while (b) {         long long t = a / b;         a -= t * b;         std::swap(a, b);         u -= t * v;         std::swap(u, v);       }       return ModInt(u);     }     bool operator==(ModInt that) const { return x == that.x; }     bool operator!=(ModInt that) const { return x != that.x; }     ModInt operator-() const {       ModInt t;       t.x = x == 0 ? 0 : Mod - x;       return t;     }   };   template <int MOD>   ostream& operator<<(ostream& st, const ModInt<MOD> a) {     st << a.get();     return st;   };   template <int MOD>   ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {     ModInt<MOD> r = 1;     while (k) {       if (k & 1) r *= a;       a *= a;       k >>= 1;     }     return r;   }   template <typename T, int FAC_MAX>   struct Comb {     vector<T> fac, ifac;     Comb() {       fac.resize(FAC_MAX, 1);       ifac.resize(FAC_MAX, 1);       for (int i = 1; i < FAC_MAX; i++) fac[i] = fac[i - 1] * i;       ifac[FAC_MAX - 1] = T(1) / fac[FAC_MAX - 1];       for (int i = FAC_MAX - 2; i >= 1; i--) ifac[i] = ifac[i + 1] * T(i + 1);     }     T aPb(int a, int b) {       if (b < 0 || a < b) return T(0);       return fac[a] * ifac[a - b];     }     T aCb(int a, int b) {       if (b < 0 || a < b) return T(0);       return fac[a] * ifac[a - b] * ifac[b];     }     T nHk(int n, int k) {       if (n == 0 && k == 0) return T(1);       if (n <= 0 || k < 0) return 0;       return aCb(n + k - 1, k);     }   };   using mint = ModInt<998244353>;   using vm = vector<mint>;   void _main() {     int N;     cin >> N;     int M;     cin >> M;     vvl g(N);     for (int i = 0; i < M; i++) {       int a, b;       cin >> a >> b;       a--;       b--;       g[a].push_back(b);     }     vl dist(N, LINF);     for (int i = 0; i < N; i++) {       for (auto u : g[i]) chmin(dist[i], (u - i + N) % N);       if (g[i].size() != 0) dist[i] += ll(g[i].size() - 1) * ll(N);       if (dist[i] == LINF) dist[i] = 0;     }     for (int i = 0; i < N; i++) {       ll ans = 0;       for (int j = 0; j < N; j++)         if (g[j].size() != 0) chmax(ans, dist[j] + ll((j - i + N) % N));       cout << ans << (i == N - 1 ?   n  :    );     }   }


#include <bits/stdc++.h>   using namespace std;   inline void PAUSE() {     char tmp;     cin >> tmp;   }   int M[100001];   int f[100001];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i <= 100000; i++) {       M[i] = -1;     }     int x, k;     for (int i = 0; i < n; i++) {       scanf( %d %d , &x, &k);       if (x - M[k] > 1) {         cout <<  NO ;         return 0;       }       M[k] = x > M[k] ? x : M[k];       f[k] = 1;     }     cout <<  YES ;     PAUSE();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long ans = 0;     long long a, b, c, s;     cin >> s >> a >> b >> c;     ans = s / c + (s / c / a) * b;     cout << ans << endl;   }   int main() {     int T;     cin >> T;     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a[256], n, k, p;     cin >> n >> k;     memset(a, -1, sizeof(a));     for (int i = 0; i < n; i++) {       cin >> p;       if (a[p] == -1) {         for (int j = max(0, p - k + 1); j <= p; j++)           if (a[j] == -1 || a[j] == j) {             for (int x = j; x <= p; x++) a[x] = j;             break;           }       }       cout << a[p] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int inf = (int)1e9;   const int mod = inf + 7;   const double eps = 1e-9;   int a[100][100];   int n, x, y;   bool u[400], u1[400];   vector<int> v;   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     for (int i = 1; i <= n * n; i++) {       cin >> x >> y;       if (!u[x] && !u1[y]) {         u[x] = 1;         u1[y] = 1;         v.push_back(i);       }     }     for (int i = 0; i < v.size(); i++) {       cout << v[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int n, k;     cin >> n >> k;     long long int a[n + 2];     a[0] = 0;     pair<long long int, long long int> min;     min.first = INT_MAX;     for (long long int i = 1; i <= n; i++) {       cin >> a[i];       a[i] += a[i - 1];       if (i >= k) {         a[i] -= a[i - k];         if (a[i] < min.first) min.first = a[i], min.second = i - k + 1;         a[i] += a[i - k];       }     }     cout << min.second;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1.01e9;   const double eps = 1e-9;   const int N = 22;   double RES = 0;   double rf, re, rs;   double df, de;   int cnt = 0;   int a[N];   int ac = 0;   int b[N];   int bc = 0;   int nf, ne, ns;   double e[N];   double FI[200];   double EN[200];   double FIRE(int p) {     double res = rf * 2;     for (int i = 0; (i) < (bc); ++i) res += FI[abs(b[i] - p)];     res *= df;     return res;   }   double ENERGY(int p) {     double res = re * 2;     for (int i = 0; (i) < (bc); ++i) res += EN[abs(b[i] - p)];     res *= de;     return res;   }   void solve() {     double val = 0;     for (int i = 0; (i) < (ac); ++i) {       double x = FIRE(a[i]);       double y = ENERGY(a[i]);       e[i] = x - y;       val += y;     }     sort(e, e + ac);     reverse(e, e + ac);     for (int i = 0; (i) < (nf); ++i) val += e[i];     RES = max(RES, val);   }   void go(int x, int y, int pos, int cnt1 = 0, int cnt2 = 0) {     if (x == 0 && y == 0) {       cnt++;       solve();       return;     }     if (x >= 1 && cnt1 < 2) {       a[ac++] = pos;       go(x - 1, y, pos + 1, cnt1 + 1, cnt2);       ac--;     }     if (x >= 2) {       a[ac++] = pos;       a[ac++] = pos;       go(x - 2, y, pos + 1, cnt1, cnt2);       ac--;       ac--;     }     if (y >= 1 && cnt2 < 1) {       b[bc++] = pos;       go(x, y - 1, pos + 1, cnt1, cnt2 + 1);       bc--;     }     if (y >= 2) {       b[bc++] = pos;       b[bc++] = pos;       go(x, y - 2, pos + 1, cnt1, cnt2);       bc--;       bc--;     }     if (x >= 1 && y >= 1) {       a[ac++] = pos;       b[bc++] = pos;       go(x - 1, y - 1, pos + 1, cnt1, cnt2);       ac--;       bc--;     }   }   int main() {     scanf( %d%d%d , &nf, &ne, &ns);     scanf( %lf%lf%lf , &rf, &re, &rs);     scanf( %lf%lf , &df, &de);     rf = sqrt(max(0., rf * rf - 1));     re = sqrt(max(0., re * re - 1));     rs = sqrt(max(0., rs * rs - 1));     for (int i = 0; i < 100; ++i) {       double l1 = -rf;       double r1 = rf;       double l2 = i - rs;       double r2 = i + rs;       FI[i] = max(0., min(r1, r2) - max(l1, l2));     }     for (int i = 0; i < 100; ++i) {       double l1 = -re;       double r1 = re;       double l2 = i - rs;       double r2 = i + rs;       EN[i] = max(0., min(r1, r2) - max(l1, l2));     }     go(nf + ne, ns, 0);     fprintf(stderr,  %d n , cnt), fflush(stderr);     printf( %.10f n , RES);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long i, j, k, l, n, m, last, res;   long long a[100009];   queue<long long> q;   int main() {     cin >> n;     for (i = 1; i <= n; i++) cin >> a[i];     sort(a + 1, a + 1 + n);     q.push(1);     for (i = 2; i <= n; i++) {       if (!q.empty() && a[q.front()] < a[i]) {         res++;         q.pop();       }       q.push(i);     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<pair<int, int> > s;   std::vector<int> vv;   std::vector<std::vector<int> > gr;   std::vector<int> vis;   int k;   pair<int, int> dfs(int u, int l) {     vis[u] = l;     vv.emplace_back(u);     for (int v : gr[u]) {       if ((vis[v]) && (vis[u] - vis[v]) >= k) {         return {vis[v], vis[u]};       } else if (!vis[v]) {         return dfs(v, l + 1);       }     }     return {-1, -1};   }   int32_t main() {     int n;     cin >> n;     int m;     cin >> m;     cin >> k;     gr.resize(n);     for (int i = 0; i < m; ++i) {       int u;       cin >> u;       u--;       int v;       cin >> v;       v--;       gr[u].emplace_back(v);       gr[v].emplace_back(u);     }     vis.resize(n, 0);     auto pp = dfs(0, 1);     cout << pp.second - pp.first + 1 <<   n ;     for (int i = pp.first - 1; i < pp.second; ++i) {       cout << vv[i] + 1 <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   inline void pisz(int n) { printf( %d n , n); }   template <typename T, typename TT>   ostream &operator<<(ostream &s, pair<T, TT> t) {     return s <<  (  << t.first <<  ,  << t.second <<  ) ;   }   template <typename T>   ostream &operator<<(ostream &s, vector<T> t) {     for (int(i) = 0; (i) < (((int)(t.size()))); ++(i)) s << t[i] <<    ;     return s;   }   template <typename T, typename Aug>   struct AugTreap {     struct Node {       T v;       int pr;       Node *le, *ri;       Aug al, ag;     };     Node *root;     AugTreap() : root(0) {}     inline static void recalc(Node *&n) {       Aug left = n->le ? n->le->ag + n->al : n->al;       n->ag = n->ri ? left + n->ri->ag : left;     }     inline void rotle(Node *&n) {       Node *syn = n->le;       n->le = syn->ri;       syn->ri = n;       n = syn;       recalc(n->ri);       recalc(n);     }     inline void rotri(Node *&n) {       Node *syn = n->ri;       n->ri = syn->le;       syn->le = n;       n = syn;       recalc(n->le);       recalc(n);     }     void insert_aux(Node *&n, const T &x, const Aug &aug) {       if (!n) {         n = new Node;         n->v = x;         n->pr = rand();         n->le = n->ri = 0;         n->al = n->ag = aug;       } else if (x < n->v) {         insert_aux(n->le, x, aug);         if (n->pr > n->le->pr)           rotle(n);         else           recalc(n);       } else if (x > n->v) {         insert_aux(n->ri, x, aug);         if (n->pr > n->ri->pr)           rotri(n);         else           recalc(n);       } else {         n->al = n->al + aug;         recalc(n);       }     }     void insert(const T &x, const Aug &aug) { insert_aux(root, x, aug); }     Aug sumgeq_aux(const T &x, Node *n) {       if (!n) return Aug();       if (x > n->v) return sumgeq_aux(x, n->ri);       if (x < n->v) {         Aug left = sumgeq_aux(x, n->le) + n->al;         return n->ri ? left + n->ri->ag : left;       }       return n->ri ? n->al + n->ri->ag : n->al;     }     Aug sumgeq(const T &x) { return sumgeq_aux(x, root); }   };   long long a[100007], prefsum[100007];   int main() {     long long n, k;     cin >> n >> k;     for (int(i) = 1; (i) <= (n); ++(i)) cin >> a[i];     prefsum[0] = 0;     for (int(i) = 1; (i) <= (n); ++(i)) prefsum[i] = prefsum[i - 1] + a[i];     long long best = -1000000000;     best *= 100000;     long long from = best + 1, to = -best;     while (from <= to) {       long long x = (from + to) / 2;       long long dobrychsum = 0, zab = 0;       AugTreap<long long, int> tr;       for (int(i) = 1; (i) <= (n); ++(i)) tr.insert(prefsum[i], 1);       for (int(st) = 1; (st) <= (n); ++(st)) {         dobrychsum += tr.sumgeq(x + zab);         zab += a[st];         tr.insert(zab, -1);       }       if (dobrychsum >= k) {         best = x;         from = x + 1;       } else {         to = x - 1;       }     }     cout << best;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int T;     cin >> T;     while (T--) {       long long n, k;       cin >> n >> k;       if (k % 3) {         if (n % 3)           cout <<  Alice n ;         else           cout <<  Bob n ;       } else {         n %= (k + 1);         if (n == k || n % 3)           cout <<  Alice n ;         else           cout <<  Bob n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int fu[2003];   int jar[2003];   int main() {     int n, refresh;     cin >> n >> refresh;     for (int(i) = 0; (i) < (n); ++(i)) {       cin >> jar[i];     }     for (int(i) = 0; (i) < (n); ++(i)) {       cin >> fu[i];     }     int best = fu[0];     int res = 0;     int gas = 0;     for (int i = 0; i < n; ++i) {       gas += fu[i];       best = max((best), (fu[i]));       while (gas < jar[i]) {         gas += best;         res += refresh;       }       gas -= jar[i];       res += jar[i];     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long cas;   long long a;   long long b;   long long c;   long long d[] = {0, 1, -1};   int main() {     scanf( %lld , &cas);     while (cas--) {       scanf( %lld %lld %lld , &a, &b, &c);       long long ans = 21413524368453;       for (long long i = 0; i < 3; ++i) {         long long aa = a + i;         for (long long j = 0; j < 3; ++j) {           long long bb = b + j;           for (long long k = 0; k < 3; ++k) {             long long cc = c + k;             ans = min(ans, abs(aa - bb) + abs(aa - cc) + abs(bb - cc));           }         }       }       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename TYPE>   void readint(TYPE &x) {     x = 0;     int f = 1;     char c;     for (c = getchar(); !isdigit(c); c = getchar())       if (c ==  - ) f = -1;     for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ;     x *= f;   }   void err(istream_iterator<string> it) {}   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a << endl;     err(++it, args...);   }   const long long MOD = 998244353LL, MAX = 3e5 + 5;   const int INF = 2e9;   int n;   long long ans;   long long a[MAX];   long long fc[MAX];   long long pot(long long x, long long y) {     if (!y) return 1LL;     if (y & 1)       return (x * pot(x, y - 1)) % MOD;     else {       long long k = pot(x, y / 2);       return (k * k) % MOD;     }   }   long long inv(long long t) { return pot(t, MOD - 2); }   void read(int w[], int n) {     for (long long i = 0; i < n; i++) {       cin >> w[i];     }   }   void solve() {     cin >> n;     fc[0] = 1LL;     for (long long i = 0; i < 2 * n; i++) cin >> a[i];     if (n == 1) {       cout << (a[1] - a[0]) * 2;       return;     }     sort(a, a + 2 * n);     for (long long i = 1; i < n * 2 + 1; i++) fc[i] = (fc[i - 1] * i) % MOD;     for (long long i = 0; i < n; i++) ans += (a[2 * n - 1 - i] - a[i]);     ans %= MOD;     {       string _s =  ans ;       replace(_s.begin(), _s.end(),  , ,    );       stringstream _ss(_s);       istream_iterator<string> _it(_ss);       err(_it, ans);     };     ans = (ans * (fc[n * 2])) % MOD;     ans *= inv((fc[n] * fc[n]) % MOD);     ans %= MOD;     ans %= MOD;     cout << ans;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<long long int, long long int> mymap, mymap1;   long long int cum[200010], a[200010];   int main() {     long long int n, i, j, k;     scanf( %lld , &n);     ;     scanf( %lld , &k);     ;     cum[0] = 0;     mymap[0]++;     for (i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       ;       cum[i] = cum[i - 1] ^ a[i];       long long int temp1 = cum[i], temp2 = ((1 << k) - 1) ^ temp1;       if (mymap[temp1] == 0)         mymap[temp2]++;       else         mymap[temp1]++;     }     long long int ans = (n * (n + 1)) / 2;     map<long long int, long long int>::iterator it;     long long int subt = 0;     for (it = mymap.begin(); it != mymap.end(); it++) {       long long int val = it->first, freq = it->second;       long long int temp = freq / 2;       subt += ((temp * (temp - 1)) / 2);       temp = freq - temp;       subt += ((temp * (temp - 1)) / 2);     }     cout << ans - subt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void pv(T a, T b) {     for (T i = a; i != b; ++i) cerr << *i <<    ;     cout << endl;   }   int ans[100000 + 10];   int main() {     for (int N; ~scanf( %d , &N);) {       memset(ans, 0, sizeof(ans));       vector<int> a(N), b(N);       for (int i = 0; i < (int)(N); ++i) scanf( %d , &a[i]);       for (int i = 0; i < (int)(N); ++i) scanf( %d , &b[i]);       vector<long long> c(N);       for (int i = 0; i < (int)(N); ++i) c[i] = a[i] - b[i];       for (int i = 1; i < N; i++) c[i] += c[i - 1];       int small = *min_element((c).begin(), (c).end());       for (int i = 0; i < (int)(N); ++i)         if (c[i] == small) ans[(i + 1) % N] = 1;       for (int i = 0; i < (int)(N); ++i) c[i] = a[i] - b[(i - 1 + N) % N];       for (int i = N - 2; i >= 0; i--) c[i] += c[i + 1];       small = *min_element((c).begin(), (c).end());       for (int i = 0; i < (int)(N); ++i)         if (c[i] == small) ans[(i - 1 + N) % N] = 1;       cout << count(ans, ans + N, 1) << endl;       for (int i = 0; i < (int)(N); ++i)         if (ans[i]) cout << i + 1 <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int val[60];   int n;   int grid[2][60];   int mask1[25][1200008 + 1], mask2[25][1200008 + 1];   void solve(int l, int r, int mask[][1200008 + 1]) {     memset(mask, -1, sizeof mask);     for (int i = 0; i <= 24; i++)       for (int j = 0; j < 1200008 + 1; j++) mask[i][j] = -1;     for (int i = 0; i < (1 << r - l); i++) {       int s = 0;       for (int j = 0; j < r - l; j++)         if ((1 << j) & i) s += val[j + l];       mask[__builtin_popcount(i)][s] = i;     }   }   signed main() {     scanf( %d , &n);     n = n * 2;     for (int i = 0; i < n; i++) scanf( %d , &val[i]);     sort(val, val + n);     int sum = 0;     int base = val[0] + val[1];     grid[0][0] = val[0], grid[1][n / 2 - 1] = val[1];     n -= 2;     for (int i = 0; i < n; i++) val[i] = val[i + 2], sum += val[i];     solve(0, n / 2, mask1);     solve(n / 2, n, mask2);     int ans = 1e9;     long long ansmask = 0;     for (int a = 0; a <= n / 2; a++) {       int b = n / 2 - a;       int j = 1200008;       while (j >= 0 and mask2[b][j] == -1) j--;       for (int i = 0; j >= 0 and i <= 1200008; i++)         if (mask1[a][i] > -1) {           while (j >= 0 and (j + i) * 2 > sum) j--;           while (j >= 0 and mask2[b][j] == -1) j--;           if (j < 0) break;           if (sum - (i + j) * 2 < ans) {             ans = sum - (i + j) * 2;             ansmask = (1ll * mask2[b][j]) << (n / 2);             ansmask += mask1[a][i];           }         }     }     ans = (sum + ans) / 2;     vector<int> v1, v2;     for (int i = 0; i < n; i++)       if ((1ll << i) & ansmask)         v1.push_back(val[i]);       else         v2.push_back(val[i]);     sort(v1.begin(), v1.end());     sort(v2.begin(), v2.end());     reverse(v2.begin(), v2.end());     n += 2;     for (int i = 1; i < n / 2; i++) grid[0][i] = v1[i - 1];     for (int i = 0; i < n / 2 - 1; i++) grid[1][i] = v2[i];     for (int i = 0; i < 2; i++) {       for (int j = 0; j < n / 2; j++) cout << grid[i][j] <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 5;   const long long NN = 1e9 + 5;   char a[150][150];   map<char, bool> mp, mp1;   bool used[150][150];   long long n, m;   vector<char> v;   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 = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         for (int l = 1; l <= m; l++) {           if (l != j && a[i][j] == a[i][l]) {             used[i][l] = true;           }         }         for (int l = 1; l <= n; l++) {           if (l != i && a[l][j] == a[i][j]) {             used[i][j] = true;           }         }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (!used[i][j]) {           v.push_back(a[i][j]);         }       }     }     for (int i = 0; i < (int)v.size(); i++) {       cout << v[i];     }   }


#include <bits/stdc++.h>   using namespace std;   long long t, n, m, k, x, y, z, a[500005], ans, val;   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n;     string s;     cin >> s;     for (long long i = 0; i < n; i++) {       if (s[i] ==  ( )         x++;       else         x--;       if (x == -1 && !z)         x = 0, z = 1;       else if (x == -1)         break;     }     if (x == z)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   int pal(string s) {     int i = 0, j = s.length() - 1;     while (i < j) {       if (s[i] != s[j]) return 0;       i++;       j--;     }     return 1;   }   int main() {     ios_base::sync_with_stdio(false);     string str, tp, s;     cin >> str;     int l = str.length();     for (int i = 0; i <= l; i++) {       s = str.substr(0, i);       for (int j = 0; j < 26; j++) {         char ch =  a  + j;         tp = s + ch + str.substr(i);         if (pal(tp)) {           cout << tp << endl;           return 0;         }       }     }     cout <<  NA  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N_MAX = 51000;   int X[N_MAX], Y[N_MAX], Z[N_MAX];   bool vis[N_MAX];   bool ff(int i, int j) {     if (X[i] != X[j]) return X[i] < X[j];     if (Y[i] != Y[j]) return Y[i] < Y[j];     return Z[i] < Z[j];   }   int main() {     int n;     cin >> n;     vector<int> P;     for (int i = 1; i <= n; i++) {       cin >> X[i] >> Y[i] >> Z[i];       P.push_back(i);     }     sort(P.begin(), P.end(), ff);     for (int i = 0; i < n - 1; i++) {       if (vis[P[i]]) continue;       if (X[P[i]] == X[P[i + 1]] and Y[P[i]] == Y[P[i + 1]]) {         vis[P[i]] = 1;         vis[P[i + 1]] = 1;         cout << P[i] <<     << P[i + 1] << endl;       }     }     for (int i = 0; i < n - 1; i++) {       if (vis[P[i]]) continue;       if (X[P[i]] != X[P[i + 1]]) continue;       int j = i + 1;       while (vis[P[j]] and j < n) {         j++;       }       if (j == n or X[P[i]] != X[P[j]]) continue;       vis[P[i]] = 1;       vis[P[j]] = 1;       cout << P[i] <<     << P[j] << endl;     }     for (int i = 0; i < n - 1; i++) {       if (vis[P[i]]) continue;       int j = i + 1;       while (vis[P[j]] and j < n) {         j++;       }       vis[P[i]] = 1;       vis[P[j]] = 1;       cout << P[i] <<     << P[j] << endl;     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int n;     scanf( %d , &n);     int B1, B2;     int count1 = 0;     int count2 = 0;     while (n--) {       scanf( %d %d , &B1, &B2);       if (B1 > B2) {         count1++;       }       if (B2 > B1) {         count2++;       }     }     if (count1 > count2) {       printf( Mishka );     }     if (count2 > count1) {       printf( Chris );     }     if (count2 == count1) {       printf( Friendship is magic!^^ );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int n, m, s, t, ds, dt, cnt, f;   vector<pair<int, int>> ans;   vector<int> G[N];   pair<int, int> xx[N], yy[N];   bool vi[N];   void dfs(int u) {     vi[u] = true;     for (int v : G[u])       if (!vi[v]) {         ans.push_back({u, v});         dfs(v);       } else if (v == s)         xx[cnt] = {u, v};       else if (v == t)         yy[cnt] = {u, v};   }   int main() {     cin >> n >> m;     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       G[x].push_back(y);       G[y].push_back(x);     }     cin >> s >> t >> ds >> dt;     vi[s] = true;     vi[t] = true;     for (int i = 1; i <= n; i++)       if (!vi[i]) {         dfs(i);         cnt++;       }     for (int i = 0; i < cnt; i++)       if (xx[i] == make_pair(0, 0) && yy[i] == make_pair(0, 0)) {         cout <<  No  << endl;         return 0;       } else if (xx[i] == make_pair(0, 0)) {         dt--;         ans.push_back(yy[i]);         f |= 2;       } else if (yy[i] == make_pair(0, 0)) {         ds--;         ans.push_back(xx[i]);         f |= 1;       }     if (ds <= 0 || dt <= 0) {       cout <<  No  << endl;       return 0;     }     for (int i = 0; i < cnt; i++)       if (xx[i] > make_pair(0, 0) && yy[i] > make_pair(0, 0)) {         ds--;         dt--;         ans.push_back(xx[i]);         ans.push_back(yy[i]);         f = 3;         yy[i] = {0, 0};         xx[i] = {0, 0};         break;       }     for (int i = 0; i < cnt; i++)       if (xx[i] > make_pair(0, 0) && yy[i] > make_pair(0, 0) && ds > 0) {         ds--;         ans.push_back(xx[i]);         f |= 1;         yy[i] = {0, 0};       }     for (int i = 0; i < cnt; i++)       if (xx[i] > make_pair(0, 0) && yy[i] > make_pair(0, 0) && dt > 0) {         dt--;         ans.push_back(yy[i]);         f |= 2;         xx[i] = {0, 0};       }     for (int i = 0; i < cnt; i++)       if (xx[i] > make_pair(0, 0) && yy[i] > make_pair(0, 0)) {         cout <<  No  << endl;         return 0;       }     if (ans.size() < n - 1) {       ans.push_back({s, t});       ds--;       dt--;     }     if (ds < 0 || dt < 0)       cout <<  No  << endl;     else {       cout <<  Yes  << endl;       for (auto z : ans) cout << z.first <<     << z.second << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 30, Mod = 1e9 + 7;   const long long SQ = 330;   int main() {     ios::sync_with_stdio(0), cin.tie(0);     long long a, b;     cin >> a >> b;     long long ans = 0;     while (a > 0 && b > 0) {       if (a <= b)         a++, b -= 2;       else         b++, a -= 2;       if (a < 0 || b < 0) ans--;       ans++;     }     cout << ans;     return (0);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout <<  2001 n ;     for (int i = 1; i <= 1000; i++) {       cout << i <<   1   << i <<   2 n ;     }     cout <<  1 1 1 2 n ;     for (int i = 1; i <= 1000; i++) {       cout << i <<   1   << i <<   2 n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Max = 1e6 + 100;   int pa[Max << 1], son[Max << 1][27];   int deep[Max << 1], sum[Max << 1], tp[Max << 1], S[Max << 1], cnt, root, last;   char str[Max << 1];   inline int Newnode(int _deep) {     deep[++cnt] = _deep;     return cnt;   }   inline void SAM(int alp) {     int np = Newnode(deep[last] + 1);     int u = last;     S[np] = 1;     memset(son[np], 0, sizeof(son[np]));     while (u && !son[u][alp]) son[u][alp] = np, u = pa[u];     if (!u)       pa[np] = root;     else {       int v = son[u][alp];       if (deep[v] == deep[u] + 1)         pa[np] = v;       else {         int nv = Newnode(deep[u] + 1);         memcpy(son[nv], son[v], sizeof(son[v]));         pa[nv] = pa[v], pa[v] = pa[np] = nv;         while (u && son[u][alp] == v) son[u][alp] = nv, u = pa[u];       }     }     last = np;   }   inline void toposort() {     for (int i = 1; i <= deep[last]; i++) sum[i] = 0;     for (int i = 1; i <= cnt; i++) sum[deep[i]]++;     for (int i = 1; i <= deep[last]; i++) sum[i] += sum[i - 1];     for (int i = 1; i <= cnt; i++) tp[sum[deep[i]]--] = i;     for (int i = cnt; i >= 1; i--) S[pa[tp[i]]] += S[tp[i]];   }   inline void pre() { root = last = Newnode(0); }   int vis[Max], Q;   void solve(char *pat, int x) {     int len = strlen(pat);     int ans = 0, l = 0, now = root;     for (int a = 0; a < len; a++) {       int c = pat[a] -  a ;       if (son[now][c]) {         now = son[now][c];         l++;       } else {         while (now && !son[now][c]) now = pa[now];         if (!now)           now = root, l = 0;         else           l = deep[now] + 1, now = son[now][c];       }       if (a >= x && l >= x) {         int tmp = now;         while (!(deep[pa[tmp]] + 1 <= x && deep[tmp] >= x)) tmp = pa[tmp];         if (vis[tmp] != Q + 1) ans += S[tmp], vis[tmp] = Q + 1;       }     }     printf( %d n , ans);   }   int main() {     scanf( %s , str);     pre();     int len = strlen(str);     for (int a = 0; a < len; a++) SAM(str[a] -  a );     toposort();     scanf( %d , &Q);     while (Q--) {       scanf( %s , str);       len = strlen(str);       for (int b = 0; b < len; b++) str[b + len] = str[b];       str[len + len] = 0;       solve(str, len);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int kN = 2e5 + 10;   const long long kInf = 1e18 + 10;   int n, q;   long long a[kN], b[kN];   vector<int> g[kN];   int in[kN], out[kN], dfs_clock;   pair<long long, long long> p1[kN], p2[kN];   void dfs(int u) {     in[u] = ++dfs_clock;     for (int v : g[u]) {       a[v] += a[u], b[v] += b[u];       dfs(v);     }     out[u] = dfs_clock;   }   struct SegmentTree {     pair<long long, long long> pos[kN << 2];     struct Node {       long long a, b;       long long wait, tg;     };     Node seg[kN << 2];     void init(pair<long long, long long> *a) {       for (int i = 1; i <= n; i++) pos[i] = a[i];     }     void push_down(int o) {       if (seg[o].tg == 0) return;       seg[(o << 1)].tg += seg[o].tg, seg[(o << 1 | 1)].tg += seg[o].tg;       seg[(o << 1)].wait -= seg[o].tg, seg[(o << 1 | 1)].wait -= seg[o].tg;       seg[(o << 1)].a += seg[o].tg, seg[(o << 1 | 1)].a += seg[o].tg;       seg[o].tg = 0;     }     void push_up(int o) {       long long a1 = seg[(o << 1)].a, b1 = seg[(o << 1)].b;       long long a2 = seg[(o << 1 | 1)].a, b2 = seg[(o << 1 | 1)].b;       if (a1 * b1 > a2 * b2)         seg[o].a = a1, seg[o].b = b1;       else         seg[o].a = a2, seg[o].b = b2;       seg[o].wait = min(seg[(o << 1)].wait, seg[(o << 1 | 1)].wait);       if (b1 != b2) {         long long tmp = (a2 * b2 - a1 * b1) / (b1 - b2);         if (tmp >= 0) seg[o].wait = min(seg[o].wait, tmp);       }     }     void build(int l = 1, int r = n, int o = 1) {       if (l == r) {         seg[o] = {pos[l].first, pos[l].second, kInf, 0};         return;       }       int m = (l + r) >> 1;       build(l, m, (o << 1)), build(m + 1, r, (o << 1 | 1));       push_up(o);     }     void update(int ql, int qr, int x, int l = 1, int r = n, int o = 1) {       if (ql > r || qr < l) return;       if (ql <= l && r <= qr && seg[o].wait >= x) {         seg[o].tg += x, seg[o].wait -= x, seg[o].a += x;         return;       }       push_down(o);       int m = (l + r) >> 1;       if (ql <= m) update(ql, qr, x, l, m, (o << 1));       if (qr > m) update(ql, qr, x, m + 1, r, (o << 1 | 1));       push_up(o);     }     long long query(int ql, int qr, int l = 1, int r = n, int o = 1) {       if (ql > r || qr < l) return -kInf;       if (ql <= l && r <= qr) return seg[o].a * seg[o].b;       push_down(o);       int m = (l + r) >> 1;       return max(query(ql, qr, l, m, (o << 1)),                  query(ql, qr, m + 1, r, (o << 1 | 1)));     }   };   SegmentTree t1, t2;   int main() {     scanf( %d%d , &n, &q);     for (int i = 2, x; i <= n; i++) {       scanf( %d , &x);       g[x].push_back(i);     }     for (int i = 1; i <= n; i++) scanf( %lld , a + i);     for (int i = 1; i <= n; i++) scanf( %lld , b + i);     dfs(1);     for (int i = 1; i <= n; i++) {       p1[in[i]] = {a[i], b[i]}, p2[in[i]] = {a[i], -b[i]};     }     t1.init(p1), t2.init(p2);     t1.build(), t2.build();     while (q--) {       int op, v;       scanf( %d%d , &op, &v);       if (op == 1) {         int x;         scanf( %d , &x);         t1.update(in[v], out[v], x);         t2.update(in[v], out[v], x);       } else {         printf( %lld n , max(t1.query(in[v], out[v]), t2.query(in[v], out[v])));       }     }     return 0;   }