#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   int n;   vector<pair<int, int> > v[maxn];   int size[maxn];   long long mi, mx;   void dfs(int first, int fa) {     size[first] = 1;     for (pair<int, int> i : v[first]) {       if (i.first == fa) continue;       dfs(i.first, first);       if (size[i.first] & 1) mi += 1LL * i.second;       size[first] += size[i.first];       mx += 1LL * min(n - size[i.first], size[i.first]) * i.second;     }   }   void solve() {     scanf( %d , &n);     n = n * 2;     for (int i = 1; i <= n; i++) v[i].clear();     for (int i = 1, first, second, z; i < n; i++) {       scanf( %d%d%d , &first, &second, &z);       v[first].push_back({second, z});       v[second].push_back({first, z});     }     mi = 0, mx = 0;     dfs(1, 0);     printf( %lld %lld n , mi, mx);   }   int T;   int main() {     scanf( %d , &T);     while (T--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 7;   const int MOD = 1e9 + 7;   const int LEN = 8, MAGIC = 1e8;   int a[N], b[N];   int n;   char bignum[N];   long long m[N];   int tot;   int f[N], g[N];   int add(int a, int b) {     a += b;     if (a < 0) a += MOD;     if (a >= MOD) a -= MOD;     return a;   }   int divide(int d) {     long long remain = 0;     for (int i = tot - 1; i >= 0; i--) {       remain = remain * MAGIC + m[i];       m[i] = remain / d;       remain = remain % d;     }     while (tot > 1 && m[tot - 1] == 0) tot--;     return remain;   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       scanf( %d , &a[i]);     }     a[n] = 1e9 + 7;     for (int i = 1; i <= n; i++) {       scanf( %d , &b[i]);     }     {       scanf( %s , bignum);       tot = 0;       int cur = 0, l = strlen(bignum);       for (int i = l - 1; i >= 0; i -= LEN) {         cur = 0;         for (int j = max(0, i - LEN + 1); j <= i; j++) {           cur = cur * 10 + bignum[j] -  0 ;         }         m[tot++] = cur;       }     }     int carry = 0;     f[0] = 1;     for (int i = 1; i <= n; i++) {       int new_carry = carry + b[i], s = 0;       fill(g, g + new_carry + 1, 0);       for (int j = 0; j <= new_carry; j++) {         s = add(s, f[j]);         if (j > b[i]) {           s = add(s, -f[j - b[i] - 1]);         }         g[j] = s;       }       fill(f, f + carry + 1, 0);       if (a[i] > 1) {         int remainder = divide(a[i]);         for (int ii = remainder, jj = 0; ii <= new_carry; ii += a[i], jj++) {           f[jj] = g[ii];         }         carry = (new_carry - remainder) / a[i];       } else if (a[i] == 1) {         for (int i = 0; i <= new_carry; i++) f[i] = g[i];         carry = new_carry;       } else {         assert(false);       }     }     if (tot == 1 && m[0] == 0) {       printf( %d n , f[0]);     } else {       puts( 0 );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 100005;   bool black[MAX];   vector<int> vec[MAX];   bool old_black[MAX];   const int MAX_BLOCK_SIZE = 600;   int t[MAX], v[MAX];   bool used[MAX];   vector<pair<pair<int, int>, int>> v2[MAX];   int push[MAX];   bool clear[MAX];   void dfs_1(int pos, int prev = -1, int white = 0, int dist = 0) {     if (used[pos]) {       if (prev != -1) v2[prev].push_back(make_pair(make_pair(pos, white), dist));       for (int a : vec[pos]) dfs_1(a, pos, 0, 0);     } else {       if (!black[pos]) white++;       for (int a : vec[pos]) dfs_1(a, prev, white, dist + 1);     }   }   void make_1(int pos) {     if (!black[pos]) {       black[pos] = true;       return;     }     push[pos]++;     for (auto a : v2[pos])       if (a.first.second + 1 <= push[pos]) make_1(a.first.first);   }   void make_2(int pos) {     black[pos] = false;     push[pos] = 0;     clear[pos] = true;     for (auto &a : v2[pos]) {       a.first.second = a.second;       make_2(a.first.first);     }   }   void dfs_2(int pos, int p = 0, bool cl = false) {     if (used[pos]) {       p = push[pos];       cl |= clear[pos];     } else {       black[pos] = old_black[pos];       if (cl) black[pos] = false;       if (!black[pos] && p) {         black[pos] = true;         p--;       }     }     for (int a : vec[pos]) dfs_2(a, p, cl);   }   int main() {     ios_base::sync_with_stdio();     cin.tie(0);     cout.tie(0);     int n, q;     cin >> n >> q;     for (int i = 2; i <= n; i++) {       int a;       cin >> a;       vec[a].push_back(i);     }     for (int i = 1; i <= q; i++) cin >> t[i] >> v[i];     int root = 1;     for (int i = 1; i <= q; i += MAX_BLOCK_SIZE) {       for (int j = 1; j <= n; j++) {         used[j] = false;         v2[j].clear();         old_black[j] = black[j];         push[j] = 0;         clear[j] = false;       }       for (int j = 0; j < MAX_BLOCK_SIZE && i + j <= q; j++)         used[v[i + j]] = true;       dfs_1(root);       for (int j = 0; j < MAX_BLOCK_SIZE && i + j <= q; j++) {         int t = ::t[i + j];         int v = ::v[i + j];         if (t == 1)           make_1(v);         else if (t == 2)           make_2(v);         else           cout << (black[v] ?  black  :  white ) << endl;       }       dfs_2(root);     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int mod = 1e9 + 7;   const double eps = 1e-6;   int x[8], y[8];   bool check(int a, int b) {     return a >= min({x[0], x[1], x[2], x[3]}) &&            a <= max({x[0], x[1], x[2], x[3]}) &&            b >= min({y[0], y[1], y[2], y[3]}) &&            b <= max({y[0], y[1], y[2], y[3]});   }   int main() {     for (int i = 0; i < 8; i++) scanf( %d%d , x + i, y + i);     int tx = (x[4] + x[6]) / 2;     int ty = (y[4] + y[6]) / 2;     int d = abs(x[4] - tx) + abs(y[4] - ty);     for (int xx = -100; xx <= 100; xx++)       for (int yy = -100; yy <= 100; yy++)         if (abs(xx - tx) + abs(yy - ty) <= d)           if (check(xx, yy)) return cout <<  YES , 0;     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0), eps = 1e-4;   const int inf = ~0U >> 2;   int mod = 1e9 + 7;   int Pow(int x, long long t) {     int r = 1;     for (; t; t >>= 1, x = (long long)x * x % mod)       if (t & 1) r = (long long)r * x % mod;     return r;   }   const long long linf = (long long)inf * inf;   const int N = 5, M = 101010;   int xcost[N][M], ycost[N][M];   int n, m;   long long f[2][1 << N];   void Update(long long &x, long long d) {     if (x > d) x = d;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m - 1; ++i)       for (int j = 0; j < n; ++j) scanf( %d , &xcost[j][i]);     for (int i = 0; i < m; ++i)       for (int j = 0; j < n; ++j) scanf( %d , &ycost[j][i]);     int c = 0;     for (int i = 0; i < 1 << n; ++i) f[c][i] = linf;     f[c][(1 << n) - 1] = 0;     for (int i = 1; i < m; ++i)       for (int j = 0; j < n; ++j) {         c ^= 1;         for (int i = 0; i < 1 << n; ++i) f[c][i] = linf;         for (int mask = 0; mask < 1 << n; ++mask) {           int newmask = mask & (~(1 << j));           long long Cost = 0;           if (j >= 1) Cost += mask >> (j - 1) & 1 ? 0 : ycost[j - 1][i];           if (j == n - 1) Cost += mask & 1 ? 0 : ycost[j][i];           Update(f[c][newmask | (1 << j)], f[c ^ 1][mask] + Cost);           Cost = mask >> j & 1 ? xcost[j][i - 1] : 0;           if (j >= 1) Cost += mask >> (j - 1) & 1 ? ycost[j - 1][i] : 0;           if (j == n - 1) Cost += mask & 1 ? ycost[j][i] : 0;           Update(f[c][newmask], f[c ^ 1][mask] + Cost);         }       }     cout << f[c][0] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   int a[1005];   int main() {     int x, y, n;     cin >> x >> y;     int a[10] = {-x, -y, x - y, x, y, y - x};     cin >> n;     int ans = (n + 2) % 6;     a[ans] %= mod;     if (a[ans] < 0) a[ans] += mod;     cout << a[ans] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int M = 1e9 + 7;   long long int cdiv(long long int a, long long int b) {     return (a % b == 0) ? a / b : a / b + 1;   }   long long int myMod(long long int a, long long int b) {     long long int r = a % b;     return r < 0 ? r + b : r;   }   long long int ModPow(long long int a, long long int b, long long int M) {     if (M == 1) return 0;     a %= M;     long long int ans = 1, t = 1;     while (t > 0 && t <= b) {       if (t & b) {         ans *= a;         ans %= M;       }       t <<= 1;       a *= a;       a %= M;     }     return ans;   }   struct seg {     long long int l, r, u, d;   };   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int n;     cin >> n;     seg a[n];     for (long long int i = 0; i < n; i++)       cin >> a[i].l >> a[i].d >> a[i].r >> a[i].u;     long long int mxll[n], mxlr[n], mnrl[n], mnrr[n];     mxll[0] = a[0].l, mxlr[n - 1] = a[n - 1].l, mnrl[0] = a[0].r,     mnrr[n - 1] = a[n - 1].r;     for (long long int i = 1; i < n; i++) {       mxll[i] = max(mxll[i - 1], a[i].l);       mnrl[i] = min(mnrl[i - 1], a[i].r);     }     for (long long int i = n - 1 - 1; i >= 0; i--) {       mxlr[i] = max(mxlr[i + 1], a[i].l);       mnrr[i] = min(mnrr[i + 1], a[i].r);     }     long long int mxdl[n], mxdr[n], mnul[n], mnur[n];     mxdl[0] = a[0].d, mxdr[n - 1] = a[n - 1].d, mnul[0] = a[0].u,     mnur[n - 1] = a[n - 1].u;     for (long long int i = 1; i < n; i++) {       mxdl[i] = max(mxdl[i - 1], a[i].d);       mnul[i] = min(mnul[i - 1], a[i].u);     }     for (long long int i = n - 1 - 1; i >= 0; i--) {       mxdr[i] = max(mxdr[i + 1], a[i].d);       mnur[i] = min(mnur[i + 1], a[i].u);     }     if (mnrr[1] - mxlr[1] >= 0 && mnur[1] - mxdr[1] >= 0) {       cout << mxlr[1] <<     << mxdr[1] <<   n ;       return 0;     }     for (long long int i = 1; i < n - 1; i++) {       long long int cix =           min(mnrl[i - 1], mnrr[i + 1]) - max(mxll[i - 1], mxlr[i + 1]);       long long int ciy =           min(mnul[i - 1], mnur[i + 1]) - max(mxdl[i - 1], mxdr[i + 1]);       if (cix >= 0 && ciy >= 0) {         cout << max(mxll[i - 1], mxlr[i + 1]) <<                  << max(mxdl[i - 1], mxdr[i + 1]);         return 0;       }     }     if (mnrl[n - 2] - mxll[n - 2] >= 0 && mnul[n - 2] - mxdl[n - 2] >= 0) {       cout << mxll[n - 2] <<     << mxdl[n - 2] <<   n ;       return 0;     }     return 0;   }


#include <bits/stdc++.h>   const int MAX = 111;   using namespace std;   int A[MAX];   int main() {     int N, a, b;     scanf( %d%d%d , &N, &a, &b);     for (int i = 0; i < (a); ++i) {       int(x);       scanf( %d , &x);       A[x] = 1;     }     for (int i = 0; i < (b); ++i) {       int(x);       scanf( %d , &x);       if (!A[x]) A[x] = 2;     }     for (int i = (1); i <= (N); ++i) printf( %d  , A[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t, T;     cin >> t;     for (T = 0; T < t; T++) {       long long l, i;       string s, s1, s2;       cin >> s;       l = s.size();       for (i = 0; i < l / 2; i++) {         s1.push_back(s[i]);       }       for (i = l / 2; s[i] !=   0 ; i++) {         s2.push_back(s[i]);       }       if (s1 == s2)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[150], b[150];   void put(int x, int y[]) {     if (x == 1)       y[1] = 1;     else if (x == 2) {       y[1] = 3;       y[2] = 4;     } else {       for (int i = 1; i <= x - 1; i++) y[i] = 2;       y[x] = x - 2;     }   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     put(n, a);     put(m, b);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         printf( %d , a[i] * b[j]);         if (j == m)           printf(  n );         else           printf(   );       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   vector<T>& operator--(vector<T>& v) {     for (auto& i : v) --i;     return v;   }   template <typename T>   vector<T>& operator++(vector<T>& v) {     for (auto& i : v) ++i;     return v;   }   template <typename T>   istream& operator>>(istream& is, vector<T>& v) {     for (auto& i : v) is >> i;     return is;   }   template <typename T>   ostream& operator<<(ostream& os, vector<T>& v) {     for (auto& i : v) os << i <<    ;     return os;   }   template <typename T, typename U>   istream& operator>>(istream& is, pair<T, U>& p) {     is >> p.first >> p.second;     return is;   }   template <typename T, typename U>   ostream& operator<<(ostream& os, pair<T, U>& p) {     os << p.first <<     << p.second;     return os;   }   template <typename T, typename U>   pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {     return make_pair(a.first - b.first, a.second - b.second);   }   template <typename T, typename U>   pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {     return make_pair(a.first + b.first, a.second + b.second);   }   template <typename T, typename U>   void umin(T& a, U b) {     if (a > b) a = b;   }   template <typename T, typename U>   void umax(T& a, U b) {     if (a < b) a = b;   }   int main() {     int T;     cin >> T;     while (T--) {       long long a, b, p, q;       cin >> a >> b >> p >> q;       long long t = min(b - a + 1, (long long)sqrt(b - a) + 5);       vector<pair<long long, int>> v;       auto g = [&](long long x) { return p * x * 2 % (q * 2); };       auto f = [&](long long x) { return abs(g(x) - q); };       42;       ;       for (int i = 0; i < t; ++i) {         v.emplace_back(g(i), i);       }       long long ans = a;       sort(v.begin(), v.end());       42;       ;       for (long long i = a; i + t <= b + 1; i += t) {         long long need = (q - g(i) + q * 2) % (q * 2);         int ind = (lower_bound((v).begin(), (v).end(), (make_pair(need, -1))) -                    (v).begin());         for (int j = ind - 3; j <= ind + 3; ++j) {           int k = (j + v.size()) % v.size();           long long fn = f(i + v[k].second);           if (fn < f(ans) || (fn == f(ans) && i + v[k].second < ans))             ans = i + v[k].second;         }         ind = (upper_bound((v).begin(), (v).end(), (make_pair(need, -1))) -                (v).begin());         for (int j = ind - 3; j <= ind + 3; ++j) {           int k = (j + v.size()) % v.size();           long long fn = f(i + v[k].second);           if (fn < f(ans) || (fn == f(ans) && i + v[k].second < ans))             ans = i + v[k].second;         }       }       for (long long i = b; i >= max(b - t - 5, a); --i) {         long long fn = f(i);         if (fn < f(ans) || (fn == f(ans) && i < ans)) ans = i;       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   vector<vector<int>> adj;   vector<bool> vis;   vector<int> dep;   vector<int> sz;   vector<int> dif;   void dfsd(int v, int c) {     vis[v] = true;     c++;     for (auto it = adj[v].begin(); it != adj[v].end(); ++it) {       if (vis[*it] != true) {         dfsd(*it, c);       }     }     dep[v] = c;   }   void dfss(int v) {     vis[v] = false;     for (auto it = adj[v].begin(); it != adj[v].end(); ++it) {       if (vis[*it] != false) {         dfss(*it);       }     }     for (auto it = adj[v].begin(); it != adj[v].end(); ++it) {       sz[v] += sz[*it];     }     sz[v]++;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, i, k, p, q, j;     long long cnt = 0;     cin >> n >> k;     adj.resize(n);     vis.resize(n);     dep.resize(n);     sz.resize(n, 0);     dif.resize(n);     for (i = 0; i < n - 1; i++) {       cin >> p >> q;       adj[p - 1].push_back(q - 1);       adj[q - 1].push_back(p - 1);     }     dfsd(0, 0);     dfss(0);     for (i = 0; i < n; i++) {       dif[i] = sz[i] - dep[i];     }     sort(dif.begin(), dif.end(), greater<int>());     for (i = 0; i < n - k; i++) {       cnt += dif[i];     }     cout << cnt;   }


#include <bits/stdc++.h>   using namespace std;   long long n, sd, i, j, top, s, ans[100005], pos[100005], a[100005];   bitset<100005> f[62], g[62];   void read(long long &p) {     p = 0;     char x = getchar();     while (x <  0  || x >  9 ) x = getchar();     while (x >=  0  && x <=  9 ) {       p = p * 10 + x -  0 ;       x = getchar();     }   }   int main() {     read(n);     for (i = 1; i <= n; ++i) {       read(a[i]);       s ^= a[i];       for (j = 0; j <= 60; ++j)         if ((a[i] >> j) & 1) f[j][i] = 1;     }     for (sd = 0; sd <= 1; ++sd)       for (i = 60; i >= 0; --i)         if (((s >> i) & 1) == sd) {           top++;           g[top] = f[i];           g[top][n + 1] = (!sd);           for (j = 1; j <= top - 1; ++j)             if (g[top][pos[j]]) g[top] ^= g[j];           pos[top] = n + 2;           for (j = 1; j <= n; ++j)             if (g[top][j]) {               pos[top] = j;               break;             }           if (pos[top] > n) {             top--;             continue;           }           for (j = 1; j <= top - 1; ++j)             if (g[j][pos[top]]) g[j] ^= g[top];         }     for (i = 1; i <= top; ++i)       if (g[i][n + 1]) ans[pos[i]] = 1;     for (i = 1; i <= n; ++i) {       if (ans[i])         putchar( 1 );       else         putchar( 2 );       if (i == n)         putchar(  n );       else         putchar(   );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   void solve() {     long long p, q;     cin >> p >> q;     long long g = p;     long long t = q;     map<long long, long long> f, l;     for (long long i = 2; i * i <= t; i++) {       if (t % i == 0) {         while (g % i == 0) {           g /= i;           f[i]++;         }         while (t % i == 0) {           t /= i;           l[i]++;         }       }     }     if (t != 1) {       while (g % t == 0) {         g /= t;         f[t]++;       }       l[t]++;       t = 1;     }     long long ans = g;     for (auto x : l) {       long long u = p;       for (int i = 0; i < max(0ll, f[x.first] - x.second + 1); i++) u /= x.first;       ans = max(ans, u);     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int q = 1;     cin >> q;     for (; q > 0; q--) {       solve();       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename Kausi>   void debug(vector<Kausi> v) {     cerr <<  v = [  ;     long long n = v.size();     for (int i = 0; i < n; i++) {       cerr << v[i];       if (i < n - 1) cerr <<  ,  ;     }     cerr <<   ] length =   << v.size() <<   n ;   }   long long stringToLong(string s) {     stringstream raaz(s);     long long x = 0;     raaz >> x;     return x;   }   string longToString(long long k) {     stringstream ss;     ss << k;     string s;     ss >> s;     return s;   }   bool isPowerOfTwo(int n) {     if (n == 0) return false;     return (ceil(log2(n)) == floor(log2(n)));   }   void scanArr(int *arr, int n) {     for (int i = 0; i < n; i++) cin >> arr[i];   }   void printArr(int *arr, int n) {     for (int i = 0; i < n; i++) cout << arr[i] <<    ;     cout <<   n ;   }   template <typename Kausi>   void scanVector(vector<Kausi> &v, int n) {     for (int i = 0; i < n; i++) {       Kausi temp;       cin >> temp;       v.push_back(temp);     }   }   template <typename Kausi>   void printVector(vector<Kausi> v) {     for (auto x : v) cout << x <<    ;     cout <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<vector<char>> v(n, vector<char>(n,  = ));       string s;       cin >> s;       for (int i = 0; i < n; i++) v[i][i] =  X ;       bool ans = true;       for (int i = 0; i < n; i++) {         if (s[i] ==  2 ) {           bool flg = true;           for (int j = 0; j < n; j++) {             if (j != i) {               if (v[i][j] ==  =  and v[j][i] ==  =  and s[j] !=  1 ) {                 v[i][j] =  + ;                 v[j][i] =  - ;                 flg = false;                 break;               }             }           }           if (flg) {             cout <<  NO                   <<   n ;             ans = false;             break;           }         }       }       if (ans) {         cout <<  YES               <<   n ;         for (int i = 0; i < n; i++) {           for (int j = 0; j < n; j++) cout << v[i][j];           cout <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long d, k, a, b, t;   void solve() {     cin >> d >> k >> a >> b >> t;     if (d <= k) {       cout << d * a;       return;     }     long long ans = k * a + d * b - k * b;     long long mult = t + k * a - k * b;     if (mult >= 0) {       cerr << 1 <<   n ;       cout << ans;     } else {       mult = abs(mult);       long long low = 0;       long long high = d / k - 1;       long long n = ans / mult;       long long ans2 =           min(d * b, d * a + (long long)ceil(((double)d) / k - 1) * t);       if (n >= low && n <= high) {         cout << min(ans2, ans - n * mult);       } else if (n > high) {         cout << min(ans2, ans - high * mult);       } else         cout << min(ans2, ans);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void read(T &x) {     x = 0;     bool f = 0;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = 1;     for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);     if (f) x = -x;   }   template <typename F>   inline void write(F x, char ed =   n ) {     static short st[30];     short tp = 0;     if (x < 0) putchar( - ), x = -x;     do st[++tp] = x % 10, x /= 10;     while (x);     while (tp) putchar( 0  | st[tp--]);     putchar(ed);   }   template <typename T>   inline void Mx(T &x, T y) {     x < y && (x = y);   }   template <typename T>   inline void Mn(T &x, T y) {     x > y && (x = y);   }   const long long inf = 1ll << 60;   const int N = 200050;   long long ans[N][3], q;   set<long long> s;   long long a[N], bit[N], n;   inline bool oper(long long x, long long y, bool op) {     if (x + y >= inf) return 0;     long long t = op ? (x ^ y) : (x + y);     if (s.find(t) != s.end()) return 0;     s.insert(t);     a[++n] = t;     ans[++q][0] = x, ans[q][1] = y, ans[q][2] = op;     return 1;   }   bool chk(long long x) {     for (int i = 60; i >= 0; i--) {       if (x >> i & 1) {         if (bit[i])           x ^= bit[i];         else           return 1;       }     }     return 0;   }   void insert(long long x) {     for (int i = 60; i >= 0; i--) {       if (x >> i & 1) {         if (bit[i])           oper(bit[i], x, 1), x ^= bit[i];         else {           bit[i] = x;           break;         }       }     }   }   int main() {     srand((long long)new char);     read(a[1]), s.insert(a[1]), insert(a[1]), n = 1;     for (int i = 2; i <= 20; i++)       if (oper(a[n], a[n], 0) && chk(a[n])) insert(a[n]);     while (s.find(1) == s.end()) {       int x = rand() % n + 1, y = rand() % n + 1;       if (oper(a[x], a[y], 0) && chk(a[n])) insert(a[n]);     }     write(q);     for (int i = 1; i <= q; i++)       write(ans[i][0],    ), putchar(ans[i][2] ?  ^  :  + ), putchar(   ),           write(ans[i][1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   template <class t, class u>   void chmax(t& first, u second) {     if (first < second) first = second;   }   template <class t, class u>   void chmin(t& first, u second) {     if (second < first) first = second;   }   template <class t>   using vc = vector<t>;   template <class t>   using vvc = vc<vc<t>>;   using pi = pair<ll, ll>;   using vi = vc<ll>;   template <class t, class u>   ostream& operator<<(ostream& os, const pair<t, u>& p) {     return os <<  {  << p.first <<  ,  << p.second <<  } ;   }   template <class t>   ostream& operator<<(ostream& os, const vc<t>& v) {     os <<  { ;     for (auto e : v) os << e <<  , ;     return os <<  } ;   }   using uint = unsigned;   using ull = unsigned long long;   template <class t, size_t n>   ostream& operator<<(ostream& os, const array<t, n>& first) {     return os << vc<t>(first.begin(), first.end());   }   template <ll i, class T>   void print_tuple(ostream&, const T&) {}   template <ll i, class T, class H, class... Args>   void print_tuple(ostream& os, const T& t) {     if (i) os <<  , ;     os << get<i>(t);     print_tuple<i + 1, T, Args...>(os, t);   }   template <class... Args>   ostream& operator<<(ostream& os, const tuple<Args...>& t) {     os <<  { ;     print_tuple<0, tuple<Args...>, Args...>(os, t);     return os <<  } ;   }   template <class t>   void print(t x, ll suc = 1) {     cout << x;     if (suc == 1) cout <<   n ;     if (suc == 2) cout <<    ;   }   ll read() {     ll i;     cin >> i;     return i;   }   vi readvi(ll n, ll off = 0) {     vi v(n);     for (ll i = ll(0); i < ll(n); i++) v[i] = read() + off;     return v;   }   pi readpi(ll off = 0) {     ll first, second;     cin >> first >> second;     return pi(first + off, second + off);   }   template <class T>   void print(const vector<T>& v, ll suc = 1) {     for (ll i = ll(0); i < ll(v.size()); i++)       print(v[i], i == ll(v.size()) - 1 ? suc : 2);   }   string readString() {     string s;     cin >> s;     return s;   }   template <class T>   T sq(const T& t) {     return t * t;   }   void yes(bool ex = true) {     cout <<  Yes           <<   n ;     if (ex) exit(0);   }   void no(bool ex = true) {     cout <<  No           <<   n ;     if (ex) exit(0);   }   void possible(bool ex = true) {     cout <<  Possible           <<   n ;     if (ex) exit(0);   }   void impossible(bool ex = true) {     cout <<  Impossible           <<   n ;     if (ex) exit(0);   }   constexpr ll ten(ll n) { return n == 0 ? 1 : ten(n - 1) * 10; }   const ll infLL = LLONG_MAX / 3;   const ll inf = infLL;   ll topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }   ll topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }   ll botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); }   ll botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); }   ll popcount(signed t) { return __builtin_popcount(t); }   ll popcount(ll t) { return __builtin_popcountll(t); }   bool ispow2(ll i) { return i && (i & -i) == i; }   ll mask(ll i) { return (ll(1) << i) - 1; }   bool inc(ll first, ll second, ll c) { return first <= second && second <= c; }   template <class t>   void mkuni(vc<t>& v) {     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());   }   ll rand_int(ll l, ll r) {     static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());     return uniform_int_distribution<ll>(l, r)(gen);   }   template <class t>   void myshuffle(vc<t>& first) {     for (ll i = ll(0); i < ll(ll(first.size())); i++)       swap(first[i], first[rand_int(0, i)]);   }   template <class t>   ll lwb(const vc<t>& v, const t& first) {     return lower_bound(v.begin(), v.end(), first) - v.begin();   }   signed main() {     cin.tie(0);     ios::sync_with_stdio(0);     cout << fixed << setprecision(20);     ll n;     cin >> n;     ll k = n / 2;     vi x(k), y(n - k);     for (ll i = ll(0); i < ll(k); i++) x[i] = i + 1;     for (ll i = ll(0); i < ll(n - k); i++) y[i] = k + i + 1;     vvc<ll> ans;     if (n % 2) {       for (ll i = ll(0); i < ll(k); i++)         for (ll j = ll(i + 1); j < ll(k); j++)           for (ll _ = ll(0); _ < ll(2); _++) {             ans.push_back({x[i], x[j], y[i], y[j]});           }       for (ll i = ll(0); i < ll(k); i++)         for (ll _ = ll(0); _ < ll(2); _++) ans.push_back({x[i], y[k], y[i]});     } else {       for (ll i = ll(0); i < ll(k); i++) {         for (ll j = ll(i + 2); j < ll(i == 0 ? k - 1 : k); j++)           for (ll _ = ll(0); _ < ll(2); _++) {             ans.push_back({x[i], x[j], y[i], y[j]});           }         if (k >= 3) {           if (i + 1 < k) {             ll j = i + 1;             ans.push_back({x[i], x[j], y[i], y[j]});           }           if (i == 0) {             ll j = k - 1;             ans.push_back({x[i], x[j], y[i], y[j]});           }         }         {           ans.push_back({x[i], x[(i + 1) % k], y[(i + 1) % k]});           ans.push_back({x[(i + 1) % k], y[i], y[(i + 1) % k]});         }       }     }     print(ll(ans.size()));     for (auto vs : ans) {       print(ll(vs.size()), 2);       print(vs);     }     vvc<ll> cnt(n, vi(n));     for (auto vs : ans) {       for (ll i = ll(0); i < ll(ll(vs.size())); i++) {         ll p = vs[i] - 1, q = vs[(i + 1) % ll(vs.size())] - 1;         cnt[p][q]++;         cnt[q][p]++;       }     }     void(0);     for (ll i = ll(0); i < ll(n); i++)       for (ll j = ll(0); j < ll(n); j++)         if (i != j) {           assert(cnt[i][j] == 2);         }   }


#include <bits/stdc++.h>   #pragma GCC optimize( -Ofast ,  -funroll-all-loops )   using namespace std;   const int N = 5e3 + 10;   int n, m, k, deg[N], dp[N][N], res, pre[N][N];   int head[N], nex[N], to[N], w[N], tot;   inline void add(int a, int b, int c) {     to[++tot] = b;     nex[tot] = head[a];     w[tot] = c;     head[a] = tot;   }   void Top() {     queue<int> q;     dp[1][1] = 0;     for (int i = 1; i <= n; i++)       if (!deg[i]) q.push(i);     while (q.size()) {       int u = q.front();       q.pop();       for (int i = head[u]; i; i = nex[i]) {         if (--deg[to[i]] == 0) q.push(to[i]);         for (int j = 1; j <= n; j++)           if (dp[u][j] + w[i] < dp[to[i]][j + 1]) {             dp[to[i]][j + 1] = dp[u][j] + w[i];             pre[to[i]][j + 1] = u;           }       }     }   }   void dfs(int x, int y) {     if (x && y) dfs(pre[x][y], y - 1);     if (x) printf( %d  , x);   }   signed main() {     cin >> n >> m >> k;     memset(dp, 0x3f, sizeof dp);     for (int i = 1, a, b, c; i <= m; i++)       cin >> a >> b >> c, add(a, b, c), deg[b]++;     Top();     for (int i = 1; i <= n; i++)       if (dp[n][i] <= k) res = i;     cout << res <<   n ;     dfs(n, res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s1[10], s2[10], name[10], score[10];   map<string, int> mp;   int a[10][10];   bool better(int x, int y) {     int i;     int px = 0, py = 0, mx = 0, sx = 0, sy = 0, my = 0;     for (i = 0; i < 4; i++) {       if (i != x) {         if (a[x][i] > a[i][x])           sx += 3;         else if (a[x][i] == a[i][x])           sx++;         px += a[x][i];         mx += a[i][x];       }     }     for (i = 0; i < 4; i++) {       if (i != y) {         if (a[y][i] > a[i][y])           sy += 3;         else if (a[y][i] == a[i][y])           sy++;         my += a[i][y];         py += a[y][i];       }     }     if (sx != sy) return (sx > sy);     if (px - mx != py - my) return (px - mx > py - my);     if (px != py) return (px > py);     return (x < y);   }   bool check(int x, int y) {     int i, c;     int b = mp[ BERLAND ];     for (i = 0; i < 4; i++)       if (i != b && a[b][i] == -1) c = i;     a[b][c] = x;     a[c][b] = y;     int rank = 1;     for (i = 0; i < 4; i++) {       if (i != b && better(i, b)) rank++;     }     a[b][c] = a[c][b] = -1;     return (rank <= 2);   }   int main() {     int d, i, j;     for (i = 0; i < 5; i++) cin >> s1[i] >> s2[i] >> score[i];     for (i = 0; i < 5; i++) mp[s1[i]] = mp[s2[i]] = 0;     i = 0;     for (__typeof((mp).begin()) itr = (mp).begin(); itr != (mp).end(); itr++) {       (itr->second) = i;       name[i] = (itr->first);       i++;     }     for (i = 0; i < 4; i++)       for (j = 0; j < 4; j++) a[i][j] = -1;     for (i = 0; i < 5; i++) {       int x = mp[s1[i]], y = mp[s2[i]];       for (j = 0; j < score[i].length(); j++)         if (score[i][j] ==  : ) score[i][j] =    ;       istringstream ss(score[i]);       ss >> a[x][y] >> a[y][x];     }     for (d = 1; d < 100; d++)       for (i = 0; i < 100; i++) {         j = i + d;         if (check(j, i)) {           cout << j <<  :  << i << endl;           return 0;         }       }     cout <<  IMPOSSIBLE  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b, w, x, c;     cin >> a >> b >> w >> x >> c;     long long ans = 0;     if (a < c) ans = ceil((double(a - c) * w + b) / (x - w));     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   uint64_t getSeed() {     auto ptr = new char;     delete ptr;     return (uint64_t)ptr +            chrono::high_resolution_clock::now().time_since_epoch().count();   }   mt19937 rnd(getSeed());   ll rndVal(uint64_t a, uint64_t b) {     return uniform_int_distribution<uint64_t>(a, b)(rnd);   }   constexpr uint64_t MOD = (1ull << 61) - 1;   inline uint64_t mod(uint64_t x) {     x++;     x = (x & MOD) + (x >> 61);     x = (x & MOD) + (x >> 61);     return x - 1;   }   uint64_t mulmod(const uint64_t a, const uint64_t b) {     uint64_t ha = a >> 32, la = uint32_t(a), hb = b >> 32, lb = uint32_t(b);     uint64_t h = ha * hb, m = ha * lb + la * hb, l = la * lb;     return mod((l & MOD) + (l >> 61) + (m >> 29) + (m << 35 >> 3) + (h << 3));   }   struct Hash {     vector<uint64_t> h;     vector<uint64_t> p;     void build(const string& s, const uint64_t base) {       int n = s.size();       h.resize(n + 1);       p.resize(n + 1);       h[0] = 1;       p[0] = 1;       for (int i = 0; i < n; i++) {         h[i + 1] = mod(mulmod(h[i], base) + s[i] + 130);         p[i + 1] = mulmod(p[i], base);       }     }     void build(const string& s) { build(s, rndVal(258, MOD - 1)); }     uint64_t query(int l, int r) {       return mod(h[r + 1] + MOD - mulmod(h[l], p[r - l + 1]));     }   };   string s, t;   int main() {     ios::sync_with_stdio(false);     cin >> s >> t;     int ns = s.size(), nt = t.size();     uint64_t base = rndVal(258, MOD - 1);     Hash hash;     hash.build(t, base);     int rep = 0;     for (int i = 0; i < nt - 1; i++) {       if (hash.query(0, i) == hash.query(nt - i - 1, nt - 1)) rep = i + 1;     }     int has[2] = {0, 0};     for (auto c : s) has[c -  0 ]++;     int needs[2] = {0, 0};     for (auto c : t) needs[c -  0 ]++;     if (needs[0] > has[0] or needs[1] > has[1]) {       cout << s << endl;       return 0;     }     cout << t;     has[0] -= needs[0];     has[1] -= needs[1];     for (int i = 0; i < rep; i++) needs[t[i] -  0 ]--;     while (has[0] >= needs[0] and has[1] >= needs[1]) {       for (int i = rep; i < nt; i++) {         cout << t[i];         has[t[i] -  0 ]--;       }     }     while (has[0]) {       cout <<  0 ;       has[0]--;     }     while (has[1]) {       cout <<  1 ;       has[1]--;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T gcd(T a, T b) {     if (a < 0) return gcd(-a, b);     if (b < 0) return gcd(a, -b);     return (b == 0) ? a : gcd(b, a % b);   }   const double EPS = 1e-10;   const double PI = acos(-1.0);   const int INF = 1 << 30;   const int mod = 1000000007;   const int maxm = 1 + 5;   const int maxn = 1000000 + 5;   char cat[4][100000];   char s[4][100000];   int n, k;   bool bj;   bool fit(char ch) {     if (ch ==  a  || ch ==  e  || ch ==  i  || ch ==  u  || ch ==  o )       return true;     return false;   }   void fun(int x) {     int len = strlen(s[x]);     int kk = 0, start = -1;     for (int i = len - 1; i >= 0; i--) {       if (fit(s[x][i])) kk++;       if (kk == k) {         start = i;         break;       }     }     if (kk != k)       bj = false;     else       bj = true;     for (int i = start; bj && i < len; i++) {       cat[x][i - start] = s[x][i];     }     if (bj) cat[x][len - start] =   0 ;   }   bool same(int i, int j) {     if (strcmp(cat[i], cat[j]) == 0) return true;     return false;   }   int cmp() {     if (bj == false) return -1;     if (same(0, 1) && same(1, 2) && same(2, 3)) return 0;     if (same(0, 1) && same(2, 3)) return 1;     if (same(0, 2) && same(1, 3)) return 2;     if (same(0, 3) && same(1, 2)) return 3;     return -1;   }   int main() {     int ans = -2;     scanf( %d %d , &n, &k);     for (int i = (0), _b(n); i < _b; ++i) {       for (int j = (0), _b(4); j < _b; ++j) {         scanf( %s , s[j]);         fun(j);       }       int p = cmp();       if (ans == -1)         continue;       else if (ans == -2)         ans = p;       else if (ans == 0)         ans = p;       else {         if (p == 0)           continue;         else if (ans != p)           ans = -1;       }     }     if (ans == -1)       cout <<  NO  << endl;     else if (ans == 0)       cout <<  aaaa  << endl;     else if (ans == 1)       cout <<  aabb  << endl;     else if (ans == 2)       cout <<  abab  << endl;     else if (ans == 3)       cout <<  abba  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int r, x1, y1, x2, y2;     cin >> r >> x1 >> y1 >> x2 >> y2;     long long int xd = x2 - x1, yd = y2 - y1;     double dist = sqrt(xd * xd + yd * yd);     long long int int_part = (int)dist;     double frac_part = dist - int_part;     long long int ans;     ans = int_part / (2 * r);     if (int_part % (2 * r) == 0 && frac_part == 0) ans--;     ans++;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int toInt(string s) {     int r = 0;     istringstream sin(s);     sin >> r;     return r;   }   long long toInt64(string s) {     long long r = 0;     istringstream sin(s);     sin >> r;     return r;   }   double toDouble(string s) {     double r = 0;     istringstream sin(s);     sin >> r;     return r;   }   string toString(long long n) {     string s, s1;     while (n / 10 > 0) {       s += (char)((n % 10) + 48);       n /= 10;     }     s += (char)((n % 10) + 48);     n /= 10;     s1 = s;     for (long long i = 0; i < s.length(); i++) s1[(s.length() - 1) - i] = s[i];     return s1;   }   bool isUpperCase(char c) { return c >=  A  && c <=  Z ; }   bool isLowerCase(char c) { return c >=  a  && c <=  z ; }   bool isLetter(char c) { return c >=  A  && c <=  Z  || c >=  a  && c <=  z ; }   bool isDigit(char c) { return c >=  0  && c <=  9 ; }   char toLowerCase(char c) { return (isUpperCase(c)) ? (c + 32) : c; }   char toUpperCase(char c) { return (isLowerCase(c)) ? (c - 32) : c; }   int main() {     ios::sync_with_stdio(0);     int n;     cin >> n;     long long count = 1;     int a = 12;     for (long long i = 0; i < n - 1; i++) {       count += a;       a += 12;     }     cout << count << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long int n, x, count = 0, sum = 0;       cin >> n >> x;       long long int a[n];       for (int i = 0; i < n; i++) {         cin >> a[i];         sum = sum + a[i];       }       sort(a, a + n);       int k = n, m;       for (int i = 0; i < n; i++) {         if (sum / k < x) {           m = 1;           sum = sum - a[i];           k--;         } else {           m = 0;           cout << k << endl;           break;         }       }       if (m) cout << 0 << endl;     }   }


#include <bits/stdc++.h>   const int N = 100005;   long long a[N], s[N];   long long max;   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %I64d , &a[i]);     }     std::sort(a + 1, a + n + 1);     for (int i = 1; i <= n; i++) {       s[i] = s[i - 1] + a[i];       max += s[i];     }     int m, k;     scanf( %d , &m);     for (int i = 0; i < m; i++) {       scanf( %d , &k);       if (i) putchar(   );       if (k == 1)         printf( %I64d n , max - s[n]);       else {         long long ans = 0, t = n - 1, d = 1;         while (t >= 0) {           ans += s[t];           d *= k;           t -= d;         }         printf( %I64d , ans);       }     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int n, i;     cin >> n;     vector<long long int> a(n), mini(n);     for (i = 0; i < (n); i++) cin >> a[i];     mini[n - 1] = a[n - 1];     for (i = n - 2; i >= 0; i--) {       mini[i] = min(mini[i + 1], a[i]);     }     for (i = 0; i < n; i++) {       long long int l = i, r = n - 1;       long long int ans = -1;       while (l <= r) {         long long int m = l + (r - l) / 2;         if (mini[m] < a[i]) {           l = m + 1;           ans = m;         } else           r = m - 1;       }       if (ans == -1)         cout << -1 <<    ;       else         cout << ans - i - 1 <<    ;     }     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, d;   int a[105];   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> d;     for (int i = 0; i < n; i++) cin >> a[i];     int dp[n][n];     sort(a, a + n);     for (int i = 0; i < n; i++) {       for (int j = i; j < n; j++) dp[i][j] = a[j] - a[i];     }     int ans = 0;     for (int i = 0; i < n; i++) {       for (int j = i; j < n; j++) {         if (dp[i][j] <= d) ans = max(ans, j - i + 1);       }     }     cout << n - ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int maps[1010][1010];   int n;   int dir[8][2] = {{0, 1},  {1, 0}, {0, -1}, {-1, 0},                    {-1, 1}, {1, 1}, {1, -1}, {-1, -1}};   int pre[8][1010][1010];   double sums[8][1010][1010];   void partial() {     for (int i = 0; i < 8; i++) {       for (int j = 0; j < n; j++) {         for (int k = 0; k < n; k++) {           if (j - dir[i][0] < 0 || j - dir[i][0] >= n || k - dir[i][1] < 0 ||               k - dir[i][1] >= n)             for (int x = j, y = k; x >= 0 && x < n && y >= 0 && y < n;                  x += dir[i][0], y += dir[i][1]) {               if (maps[x][y] == 0) {                 pre[i][x][y] = 0;                 sums[i][x][y] = 0;               } else {                 if (x == j && y == k) {                   pre[i][x][y] = 1;                   sums[i][x][y] = log(maps[x][y]);                 } else {                   pre[i][x][y] = pre[i][x - dir[i][0]][y - dir[i][1]] + 1;                   sums[i][x][y] =                       sums[i][x - dir[i][0]][y - dir[i][1]] + log(maps[x][y]);                 }               }             }         }       }     }   }   double maxCross(int x, int y) {     int minN = 1e9, minR = 1e9;     double rot = log(maps[x][y]), norm = log(maps[x][y]);     for (int i = 0; i < 4; i++) {       minN = min(minN, pre[i][x][y]);     }     for (int i = 4; i < 8; i++) {       minR = min(minR, pre[i][x][y]);     }     minN--;     minR--;     for (int i = 0; i < 4; i++) {       int sx = x + (dir[i][0] * minN);       int sy = y + (dir[i][1] * minN);       norm += sums[i][sx][sy] - sums[i][x][y];     }     for (int i = 4; i < 8; i++) {       int sx = x + (dir[i][0] * minR);       int sy = y + (dir[i][1] * minR);       rot += sums[i][sx][sy] - sums[i][x][y];     }     return max(norm, rot);   }   bool isNorm(int x, int y) {     int minN = 1e9, minR = 1e9;     double rot = log(maps[x][y]), norm = log(maps[x][y]);     for (int i = 0; i < 4; i++) {       minN = min(minN, pre[i][x][y]);     }     for (int i = 4; i < 8; i++) {       minR = min(minR, pre[i][x][y]);     }     minN--;     minR--;     for (int i = 0; i < 4; i++) {       int sx = x + (dir[i][0] * minN);       int sy = y + (dir[i][1] * minN);       norm += sums[i][sx][sy] - sums[i][x][y];     }     for (int i = 4; i < 8; i++) {       int sx = x + (dir[i][0] * minR);       int sy = y + (dir[i][1] * minR);       rot += sums[i][sx][sy] - sums[i][x][y];     }     return norm > rot;   }   long long calcMax(int x, int y) {     int minN = 1e9, minR = 1e9;     long long rot = maps[x][y], norm = maps[x][y];     for (int i = 0; i < 4; i++) {       minN = min(minN, pre[i][x][y]);     }     for (int i = 4; i < 8; i++) {       minR = min(minR, pre[i][x][y]);     }     minN--;     minR--;     for (int i = 0; i < 4; i++) {       int sx = x;       int sy = y;       for (int j = 0; j < minN; j++) {         sx += dir[i][0];         sy += dir[i][1];         norm *= maps[sx][sy];         norm %= (long long)1e9 + 7;       }     }     for (int i = 4; i < 8; i++) {       int sx = x;       int sy = y;       for (int j = 0; j < minR; j++) {         sx += dir[i][0];         sy += dir[i][1];         rot *= maps[sx][sy];         rot %= (long long)1e9 + 7;       }     }     return (isNorm(x, y) ? norm : rot);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     ;     cin >> n;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         char ch;         cin >> ch;         maps[i][j] = ch -  0 ;       }     }     partial();     int maxX = 0, maxY = 0;     double maxVal = -1;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (maps[i][j] != 0) {           double temp = maxCross(i, j);           if (temp > maxVal) {             maxVal = temp;             maxX = i;             maxY = j;           }         }       }     }     cout << calcMax(maxX, maxY) << endl;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, ll>;   int n, m;   vector<pii> coupons;   bool comp(pii a, pii b) { return a.second > b.second; }   ll getlen(ll x) {     if (x % 2 == 1) return x * (x - 1) / 2 + 1;     return x * (x - 1) / 2 + x / 2;   }   int main() {     cin >> n >> m;     ll low = 0, high = n;     while (low < high) {       ll mid = (low + high + 1) / 2;       if (getlen(mid) <= n) {         low = mid;       } else {         high = mid - 1;       }     }     for (int i = 0; i < m; i++) {       ll q, w;       cin >> q >> w;       coupons.push_back({q, w});     }     sort(coupons.begin(), coupons.end(), comp);     ll ans = 0;     for (int i = 0; i < low && i < m; i++) {       ans += coupons[i].second;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   const int MOD = 1e9 + 7;   string ans =        What are you doing at the end of the world? Are you busy? Will you save          us? ;   string a =  What are you doing while sending   ;   string b =   ? Are you busy? Will you send   ;   string c =   ? ;   vector<long long int> dp(N, 1e18 + 1);   char get(long long int n, long long int k) {     if (k >= dp[n]) return  . ;     if (n == 0) return ans[k];     if (k < a.size()) return a[k];     if (k < a.size() + dp[n - 1]) return get(n - 1, k - a.size());     if (k < a.size() + dp[n - 1] + b.size()) return b[k - a.size() - dp[n - 1]];     if (k < a.size() + dp[n - 1] + b.size() + dp[n - 1])       return get(n - 1, k - a.size() - dp[n - 1] - b.size());     if (k < a.size() + dp[n - 1] + b.size() + dp[n - 1] + c.size())       return c[k - a.size() - dp[n - 1] - b.size() - dp[n - 1]];     return  . ;   }   void solve() {     dp[0] = ans.size();     for (int i = 1; i < 60; ++i) {       long long int k = (2 * dp[i - 1]) + a.size() + b.size() + c.size();       dp[i] = min(k, (long long int)1e18 + 1);     }     int q;     cin >> q;     while (q--) {       long long int n, k;       cin >> n >> k;       cout << get(n, k - 1);     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     int t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n, m;     cin >> n >> m;     if (m > n)       cout << -1;     else if (m == n)       cout << n;     else {       long long int x, y;       for (y = (n / 2); y >= 0; y--) {         if (n - y >= 0) {           x = n - 2 * y;           if ((x + y) % m == 0) {             cout << x + y;             break;           }         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 55;   int n, m;   int sz, inp;   long long ans[N];   long long sum[N];   long long suf[N];   long long pre[N];   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; ++i) {       scanf( %d , &sz);       ans[i] = pre[i] = -1e9;       while (sz--) {         scanf( %d , &inp);         sum[i] += inp;         suf[i] = max(0LL, suf[i]) + inp;         ans[i] = max(ans[i], suf[i]);         pre[i] = max(pre[i], sum[i]);       }     }     ans[0] = -1e15;     while (m--) {       scanf( %d , &inp);       ans[0] = max(ans[0], max(0LL, ans[n + 1]) + pre[inp]);       ans[n + 1] = max(suf[inp], sum[inp] + max(0LL, ans[n + 1]));       ans[0] = max(ans[0], max(ans[inp], ans[n + 1]));     }     printf( %lld n , ans[0]);   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const double eps = 1e-8;   const int mod = 1e9 + 7;   const int P = 1e9 + 7;   const int N = 2e7;   const int maxn = 1e6 + 5;   long long t, a, b, x, y, n, minn;   int main() {     ios::sync_with_stdio(false);     cin >> t;     while (t--) {       cin >> a >> b >> x >> y >> n;       long long temp1 = max(x, a - n);       long long temp2 = max(y, b - n);       if (temp1 >= temp2) {         swap(a, b);         swap(x, y);       }       minn = min(n, a - x);       n -= minn;       a -= minn;       minn = min(n, b - y);       b -= minn;       cout << a * b << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long f(unsigned long long x, int b) {     for (int i = 0; i < 64; i++)       if (__builtin_popcountll(x) < b) x |= 1ULL << i;     return x;   }   unsigned long long brute(unsigned long long l, unsigned long long r) {     unsigned long long ret = l;     for (unsigned long long i = l; i <= r; i++)       if (__builtin_popcountll(i) > __builtin_popcountll(ret)) ret = i;     return ret;   }   int main() {     int T;     scanf( %d , &T);     unsigned long long a, b;     while (T--) {       scanf( %llu%llu , &a, &b);       unsigned long long ans = a;       for (int i = __builtin_popcountll(a) + 1; i < 64; i++) {         unsigned long long v = f(a, i);         if (v <= b) ans = v;       }       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int up[200005];   void init() { memset(up, -1, sizeof(up)); }   int Find(int x) {     if (up[x] < 0) return x;     int root = Find(up[x]);     up[x] = root;     return root;   }   void Union(int x, int y) {     x = Find(x);     y = Find(y);     if (x == y) return;     if (up[x] < up[y]) {       up[x] += up[y];       up[y] = x;     } else {       up[y] += up[x];       up[x] = y;     }   }   int n;   int a[200005];   vector<int> can;   set<int> aree;   signed main() {     init();     int x;     scanf( %d , &n);     int keep = -1;     for (int i = 1; i <= n; i++) {       scanf( %d , &x);       if (Find(x) != Find(i)) {         Union(x, i);         a[i] = x;       } else {         if (x == i && keep == -1) {           keep = 0;           a[i] = x;         } else {           a[i] = -1;           can.push_back(i);         }       }     }     if (keep == -1) {       a[*(can.end() - 1)] = *(can.end() - 1);       can.pop_back();     }     for (int i = 1; i <= n; i++) {       aree.insert(Find(i));     }     for (auto i : can) {       for (set<int>::iterator k = aree.begin(); k != aree.end(); k++) {         int j = *k;         int u, v;         u = Find(j);         v = Find(i);         if (Find(j) != Find(i)) {           a[i] = j;           Union(i, j);           aree.erase(v);           aree.erase(u);           aree.insert(Find(j));           goto lol;         }       }     lol:;     }     cout << can.size() - keep <<   n ;     for (int i = 1; i <= n; i++) {       cout << a[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> a(n);     map<long long, vector<pair<int, int>>> d;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) {       long long sum = 0;       for (int j = i; j < n; j++) {         sum += a[j];         d[sum].push_back({i, j});       }     }     pair<int, vector<pair<int, int>>> opt = {-INT_MAX, {}};     for (auto it : d) {       sort(it.second.begin(), it.second.end(),            [](pair<int, int> p1, pair<int, int> p2) {              return p1.second < p2.second;            });       pair<int, int> prev = it.second[0];       pair<int, vector<pair<int, int>>> cur = {1, {prev}};       for (int i = 1; i < it.second.size(); i++) {         if (it.second[i].first > prev.second) {           cur.second.push_back(it.second[i]);           cur.first++;           prev = it.second[i];         }       }       opt = max(opt, cur);     }     cout << opt.first << endl;     for (int i = 0; i < opt.first; i++) {       cout << opt.second[i].first + 1 <<     << opt.second[i].second + 1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void pv(T a, T b) {     for (T i = a; i != b; ++i) cout << *i <<    ;     cout << endl;   }   template <class T>   void chmin(T &t, T f) {     if (t > f) t = f;   }   template <class T>   void chmax(T &t, T f) {     if (t < f) t = f;   }   int in() {     int x;     scanf( %d , &x);     return x;   }   const long long MO = 1000000007;   const int LIM = 1000005;   long long inv[1000010];   long long fac[1000010], fnv[1000010];   void prepare() {     int i;     inv[1] = 1;     for (i = 2; i < LIM; ++i) {       inv[i] = MO - MO / i * inv[MO % i] % MO;       assert(i * inv[i] % MO == 1);     }     fac[0] = 1;     fnv[0] = 1;     for (i = 1; i < LIM; ++i) {       fac[i] = fac[i - 1] * i % MO;       fnv[i] = fnv[i - 1] * inv[i] % MO;     }     assert(3628800 * fnv[10] % MO == 1);   }   int A, B, N;   bool isGood(int x) {     for (; x; x /= 10) {       if (!(x % 10 == A || x % 10 == B)) {         return 0;       }     }     return 1;   }   int main() {     int i;     prepare();     for (; ~scanf( %d%d%d , &A, &B, &N);) {       long long ans = 0;       for (i = 0; i <= N; ++i) {         if (isGood(A * i + B * (N - i))) {           long long tmp = 1;           (tmp *= fac[N]) %= MO;           (tmp *= fnv[i]) %= MO;           (tmp *= fnv[N - i]) %= MO;           (ans += tmp) %= MO;         }       }       ans = (ans % MO + MO) % MO;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mxN = 1e5 + 5;   const long long int MOD = 1e9 + 7;   long long int one = 0;   long long int ceil1(long long int a, long long int b) {     if (a % b == 0)       return (a / b);     else       return ((a / b) + 1);   }   void solve() {     long long int n;     cin >> n;     string s;     cin >> s;     long long int x = 0, y = 0;     for (long long int i = 0; i < n; i++) {       if (i & 1) {         if (s[i] !=  r ) x++;       } else {         if (s[i] !=  b ) y++;       }     }     long long int ans = INT_MAX;     ans = min(ans, max(x, y));     x = 0, y = 0;     for (long long int i = 0; i < n; i++) {       if (i & 1) {         if (s[i] !=  b ) x++;       } else {         if (s[i] !=  r ) y++;       }     }     ans = min(ans, max(x, y));     cout << ans <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t = 1;     while (t--) {       one++;       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   int n, o;   string st;   long long a[maxn], all;   int cc[100], where[100], zhu[100], g[maxn], wc[maxn];   int f[66][maxn];   int main() {     cin >> n;     all = 0;     for (int i = 1; i <= n; i++) {       cin >> a[i];       all ^= a[i];     }     st =   ;     for (int i = 1; i <= 63; i++) {       st = char(all % 2 + 48) + st;       all /= 2;     }     o = 0;     for (int i = 1; i <= 63; i++)       if (st[i - 1] ==  0 ) ++o, where[o] = i;     for (int i = 1; i <= 63; i++)       if (st[i - 1] ==  1 ) ++o, where[o] = i;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= 63; j++) {         cc[64 - j] = a[i] % 2;         a[i] /= 2;       }       a[i] = 0;       for (int j = 1; j <= 63; j++) a[i] = (long long)a[i] * 2 + cc[where[j]];     }     memset(f, 0, sizeof(f));     memset(g, 0, sizeof(g));     memset(zhu, 0, sizeof(zhu));     for (int i = 1; i <= 63; i++) {       for (int j = 1; j <= n; j++) f[i][j] = ((long long)a[j] >> (63 - i)) % 2;       g[i] = 1;       for (int j = 1; j < i; j++)         if ((zhu[j] != -1) && (f[i][zhu[j]] == 1)) {           for (int k = 1; k <= n; k++) f[i][k] ^= f[j][k];           g[i] ^= g[j];         }       zhu[i] = -1;       for (int j = 1; j <= n; j++)         if (f[i][j] == 1) {           zhu[i] = j;           break;         }       if (zhu[i] == -1) g[i] = 0;     }     memset(wc, 0, sizeof(wc));     for (int i = 63; i >= 1; i--) {       for (int j = 1; j <= n; j++)         if (f[i][j] == 1) g[i] ^= wc[j];       wc[zhu[i]] = g[i];     }     for (int i = 1; i <= n; i++) cout << wc[i] + 1 <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, Q;   vector<vector<int>> V;   map<pair<int, int>, int> M;   int ret[101010];   template <int um>   class UF {    public:     vector<int> par, rank;     vector<vector<int>> hist;     UF() {       par = rank = vector<int>(um, 0);       for (int i = 0; i < um; i++) par[i] = i;     }     void reinit() {       int i;       for (i = 0; i < (um); i++) rank[i] = 0, par[i] = i;     }     int operator[](int x) { return (par[x] == x) ? (x) : operator[](par[x]); }     void pop() {       if (hist.size()) {         auto a = hist.back();         hist.pop_back();         par[a[0]] = a[1];         rank[a[0]] = a[2];         par[a[3]] = a[4];         rank[a[3]] = a[5];       }     }     void operator()(int x, int y) {       x = operator[](x);       y = operator[](y);       hist.push_back({x, par[x], rank[x], y, par[y], rank[y]});       if (x == y) return;       if (rank[x] < rank[y]) {         par[x] = y;       } else if (rank[x] > rank[y]) {         par[y] = x;       } else {         rank[x]++;         par[y] = x;       }     }   };   void dfs(int L, int R, UF<201010>& uf, vector<vector<int>> V) {     vector<vector<int>> LV, RV;     int nums = 0;     int M = (L + R) / 2;     int con = 0;     if (V.empty()) return;     for (auto& v : V) {       if (v[2] <= L && R <= v[3]) {         uf(v[0] * 2, v[1] * 2 + 1);         uf(v[0] * 2 + 1, v[1] * 2);         if (uf[v[0] * 2] == uf[v[0] * 2 + 1]) con = 1;         if (uf[v[1] * 2] == uf[v[1] * 2 + 1]) con = 1;         nums += 2;       } else {         if (v[2] < M) LV.push_back({v[0], v[1], max(L, v[2]), min(M, v[3])});         if (v[3] > M) RV.push_back({v[0], v[1], max(M, v[2]), min(R, v[3])});       }     }     int i;     if (con) {       for (int i = L; i < R; i++) ret[i] = 1;     } else {       dfs(L, M, uf, LV);       dfs(M, R, uf, RV);     }     while (nums--) uf.pop();   }   void solve() {     int i, j, k, l, r, x, y;     string s;     cin >> N >> Q;     for (i = 0; i < (Q); i++) {       cin >> x >> y;       x--, y--;       if (x > y) swap(x, y);       if (M.count({x, y}) == 0) {         M[{x, y}] = i;       } else {         V.push_back({x, y, M[{x, y}], i});         M.erase({x, y});       }     }     for (auto& r : M) V.push_back({r.first.first, r.first.second, r.second, Q});     UF<201010> uf;     dfs(0, Q, uf, V);     for (i = 0; i < (Q); i++) {       if (ret[i])         cout <<  NO  << endl;       else         cout <<  YES  << endl;     }   }   int main(int argc, char** argv) {     string s;     int i;     if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);     for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s +=   n ;     for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s, ss;     getline(cin, s);     getline(cin, ss);     int ara[500];     memset(ara, 0, sizeof ara);     for (auto x : s)       if (isalpha(x)) ara[x]++;     bool ok = true;     for (auto x : ss) {       if (isalpha(x)) {         if (!ara[x]) {           ok = false;           break;         } else           ara[x]--;       }     }     if (ok)       printf( YES n );     else       printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const int N = 1e5 + 100;   const int maxn = 1e6;   const int MAXL = sqrt(1e7) + 1;   int n, a[220], f[220][410];   void solve() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     sort(a + 1, a + n + 1);     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= 400; j++) f[i][j] = INT_MAX;     }     for (int j = 1; j <= 400; j++) {       f[1][j] = abs(j - a[1]);       f[1][j] = min(f[1][j], f[1][j - 1]);     }     for (int i = 2; i <= n; i++) {       for (int j = i; j <= 400; j++) {         f[i][j] = min(f[i][j], f[i - 1][j - 1] + abs(j - a[i]));         f[i][j] = min(f[i][j], f[i][j - 1]);       }     }     int ans = INT_MAX;     for (int i = n; i <= 400; i++) ans = min(ans, f[n][i]);     printf( %d n , ans);   }   int main() {     int T;     scanf( %d , &T);     int tt = 0;     while (T--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int cal(vector<int> q, int n, int m) {     int sum = 0;     for (int i = (int)0; i < (int)n; ++i) {       for (int j = (int)i; j < (int)n; ++j) {         int mn = INT_MAX;         for (int k = (int)i; k < (int)j + 1; ++k)           mn = ((mn) < (q[k]) ? (mn) : (q[k]));         sum += mn;       }     }     return sum;   }   int main() {     int i, j, n, t;     int m;     scanf( %d , &n);     scanf( %d , &m);     vector<int> q;     for (int i = (int)1; i < (int)n + 1; ++i) q.push_back(i);     int mx = 0;     do {       mx = ((mx) > (cal(q, n, m)) ? (mx) : (cal(q, n, m)));     } while (next_permutation(q.begin(), q.end()));     sort(q.begin(), q.end());     int cnt = 0;     do {       if (cal(q, n, m) == mx) cnt++;       if (cnt == m) break;     } while (next_permutation(q.begin(), q.end()));     for (int i = (int)0; i < (int)n; ++i) printf( %d  , q[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100100;   struct node {     long long l, r;     long long color;     long long sum;     long long change;   } tree[maxn << 2];   long long n, m;   int a[maxn];   void pushup(long long cur) {     if (tree[cur * 2].color == tree[cur * 2 + 1].color)       tree[cur].color = tree[cur * 2].color;     tree[cur].sum = tree[cur * 2].sum + tree[cur * 2 + 1].sum;   }   void build(long long l, long long r, long long cur) {     tree[cur].l = l;     tree[cur].r = r;     tree[cur].sum = 0;     tree[cur].change = 0;     tree[cur].color = 0;     if (l == r) {       tree[cur].color = l;       return;     }     long long mid = (r + l) >> 1;     build(l, mid, cur << 1);     build(mid + 1, r, cur << 1 | 1);     pushup(cur);   }   void pushdown(long long cur) {     if (tree[cur].change) {       tree[cur * 2].sum +=           tree[cur].change * (tree[cur * 2].r - tree[cur * 2].l + 1);       tree[cur * 2 + 1].sum +=           tree[cur].change * (tree[cur * 2 + 1].r - tree[cur * 2 + 1].l + 1);       tree[cur * 2].change += tree[cur].change;       tree[cur * 2 + 1].change += tree[cur].change;       tree[cur].change = 0;     }     if (tree[cur].color)       tree[cur * 2].color = tree[cur * 2 + 1].color = tree[cur].color;     tree[cur].color = 0;   }   void update(long long pl, long long pr, long long l, long long r, long long cur,               long long x) {     if ((pl <= l && r <= pr && tree[cur].color) || l == r) {       tree[cur].sum += abs(x - tree[cur].color) * (r - l + 1);       tree[cur].change += abs(x - tree[cur].color);       tree[cur].color = x;       return;     }     pushdown(cur);     long long mid = (r + l) >> 1;     if (pl <= mid) update(pl, pr, l, mid, cur << 1, x);     if (pr > mid) update(pl, pr, mid + 1, r, cur << 1 | 1, x);     pushup(cur);   }   long long query(long long pl, long long pr, long long l, long long r,                   long long cur) {     if (pl <= l && r <= pr) {       return tree[cur].sum;     }     pushdown(cur);     long long mid = (r + l) >> 1;     long long res = 0;     if (pl <= mid) res += query(pl, pr, l, mid, cur << 1);     if (pr > mid) res += query(pl, pr, mid + 1, r, cur << 1 | 1);     return res;   }   int main() {     while (~scanf( %lld%lld , &n, &m)) {       build(1, n, 1);       long long op, l, r, x;       while (m--) {         scanf( %lld%lld%lld , &op, &l, &r);         if (op == 1) {           scanf( %lld , &x);           update(l, r, 1, n, 1, x);         } else {           printf( %lld n , query(l, r, 1, n, 1));         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n = 5000001;   void okms() {     vector<int> kk(n);     kk[0] = 0, kk[1] = 0;     for (int(i) = 2; i < n; ++i) kk[i] = 1;     vector<bool> ch(n);     for (int(i) = 0; i < n; ++i) ch[i] = true;     ch[0] = 0, ch[1] = 0;     vector<int> trr(n);     for (int i = 2; i * i <= n - 1; i++) {       if (ch[i]) {         trr[i] = i;         for (int j = i * i; j <= n - 1; j += i) {           ch[j] = false;           trr[j] = i;         }       }     }     for (int(i) = 2; i < n; ++i) {       if (!ch[i]) {         kk[i] = kk[trr[i]] + kk[i / trr[i]];       }     }     vector<int> ans(n);     for (int(i) = 1; i < n; ++i) ans[i] += ans[i - 1] + kk[i];     int t;     cin >> t;     while (t--) {       int a, b;       cin >> a >> b;       cout << ans[a] - ans[b] <<   n ;     }     return;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int test;     test = 1;     while (test--) {       okms();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, q;   long long siz[100005];   long long par[100005];   string s;   unordered_map<string, long long> idx;   vector<pair<long long, long long> > e[100005];   long long ans[100005];   long long dis[100005];   pair<long long, pair<long long, long long> > edges[100005];   long long find_set(long long a) {     if (a == par[a]) return a;     return par[a] = find_set(par[a]);   }   void union_set(long long a, long long b) {     a = find_set(a);     b = find_set(b);     if (a != b) {       if (siz[a] < siz[b]) swap(a, b);       par[b] = a;       siz[a] += siz[b];     }   }   void dfs(long long u, long long len, long long par) {     dis[u] = len;     for (auto i : e[u]) {       if (i.first != par) dfs(i.first, len + i.second, u);     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m >> q;     for (long long i = 0; i < n; i++) {       cin >> s;       idx[s] = i;       par[i] = i;       siz[i] = 1;     }     for (long long i = 0; i < m; i++) {       long long type;       string a, b;       cin >> type >> a >> b;       long long u = idx[a];       long long v = idx[b];       edges[i] = {type, {u, v}};       long long uRep = find_set(u);       long long vRep = find_set(v);       if (uRep == vRep) continue;       union_set(uRep, vRep);       if (type == 1)         e[u].push_back({v, 0}), e[v].push_back({u, 0});       else         e[u].push_back({v, 1}), e[v].push_back({u, 1});       ans[i] = 1;     }     memset(dis, -1, sizeof(dis));     for (long long i = 0; i < n; i++) {       if (dis[i] == -1) {         long long iRep = find_set(i);         dfs(iRep, 0, -1);       }     }     for (long long i = 0; i < m; i++) {       if (ans[i])         cout <<  YES n ;       else {         long long u = edges[i].second.first;         long long v = edges[i].second.second;         long long type = edges[i].first;         long long real_type;         if (dis[u] % 2 == dis[v] % 2)           real_type = 1;         else           real_type = 2;         if (real_type == type)           cout <<  YES n ;         else           cout <<  NO n ;       }     }     while (q--) {       string a, b;       cin >> a >> b;       long long u = idx[a];       long long v = idx[b];       long long uRep = find_set(u);       long long vRep = find_set(v);       if (uRep != vRep)         cout << 3 << endl;       else {         if (dis[u] % 2 == dis[v] % 2)           cout << 1 << endl;         else           cout << 2 << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int tc, n, i, j, p, k;     scanf( %d , &tc);     while (tc--) {       scanf( %d , &n);       vector<int> v[n + 5];       for (i = 0; i < n; i++) {         scanf( %d , &p);         v[p].push_back(i);       }       k = 0;       for (i = 1; i <= n; i++) {         int b = 0;         if (v[i].size() == 2) b = (v[i][1] - v[i][0]) > 1;         if ((v[i].size() >= 3) || b) {           k = 1;           printf( YES );           break;         }       }       if (!k) printf( NO );       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[200005];   int main() {     int n;     scanf( %d , &n);     int sum = 0;     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       sum += a[i];     }     vector<int> ans;     for (int i = 1; i <= n; ++i) {       if ((sum - a[i]) % (n - 1) == 0 && (sum - a[i]) / (n - 1) == a[i]) {         ans.push_back(i);       }     }     printf( %d n , (int)ans.size());     for (auto p : ans) {       printf( %d n , p);     }   }


#include <bits/stdc++.h>   using namespace std;   int get(string str) {     int cnt = 0;     for (int i = 0; i < str.size() - 1; i++) {       if (str[i] ==  V  && str[i + 1] ==  K ) cnt++;     }     return cnt;   }   int main() {     string str;     cin >> str;     int res = get(str);     for (int i = 0; i < str.size(); i++) {       string cp = str;       cp[i] = (cp[i] ==  K  ?  V  :  K );       res = max<int>(res, get(cp));     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline bool fs(T &x) {     int c = getchar();     int sgn = 1;     while (~c && c <  0  || c >  9 ) {       if (c ==  - ) sgn = -1;       c = getchar();     }     for (x = 0; ~c &&  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + c -  0 ;     x *= sgn;     return ~c;   }   int ara[600][600];   void boshao(int n) {     int ln = n * 2;     for (int i = 1; i <= n; i++)       for (int j = n + 1; j <= ln; j++) ara[i][j] = ara[i][j - n];     for (int j = 1; j <= n; j++)       for (int i = n + 1; i <= ln; i++) ara[i][j] = ara[i - n][j];     for (int j = n + 1; j <= ln; j++)       for (int i = n + 1; i <= ln; i++) ara[i][j] = ara[i - n][j - n] ^ 1;   }   int main() {     ara[1][1] = 1;     ara[1][2] = 1;     ara[2][1] = 1;     ara[2][2] = 0;     int n;     fs(n);     int x = 2;     for (int i = 2; i <= 9; i++) {       boshao(x);       x *= 2;     }     for (int i = 1; i <= (1 << n); i++) {       for (int j = 1; j <= (1 << n); j++) cout << ((ara[i][j]) ?  *  :  + );       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int c1, c2, c3, c4;     int n, m, x;     int b = 0, t = 0, tot = 0;     scanf( %d %d %d %d , &c1, &c2, &c3, &c4);     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &x);       if (x * c1 > c2)         b += c2;       else         b += x * c1;     }     if (b > c3) b = c3;     for (int i = 1; i <= m; i++) {       scanf( %d , &x);       if (x * c1 > c2)         t += c2;       else         t += x * c1;     }     if (t > c3) t = c3;     tot = b + t;     if (tot > c4) tot = c4;     printf( %d n , tot);   }


#include <bits/stdc++.h>   using namespace std;   long long t, a, b;   int main() {     scanf( %I64d%I64d%I64d , &t, &a, &b);     if (t > a && a != b || b < a)       printf( 0 n );     else if (t == 1) {       if (a == b) {         if (a == 1)           printf( inf n );         else           printf( 1 n );       } else if (a == 1)         printf( 0 n );       else {         long long temp = b;         while (temp % a == 0) temp /= a;         if (temp == 1) {           printf( 1 n );           return 0;         }         int pt = 0;         int cur = 1;         while (b > 0) {           long long digit = b % a;           pt += cur * digit;           b /= a;           cur *= t;         }         if (pt == a)           printf( 1 n );         else           printf( 0 n );       }     } else {       if (a == b) {         if (t == a)           printf( 2 n );         else           printf( 1 n );       } else {         long long pt = 0;         long long cur = 1;         while (b > 0) {           long long digit = b % a;           pt += cur * digit;           b /= a;           cur *= t;         }         if (pt == a)           printf( 1 n );         else           printf( 0 n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007LL;   long long large = 2000000000000000000LL;   vector<int> dp;   vector<vector<int> > adj;   int f(int u) {     if (dp[u] != -1) return dp[u];     vector<int> ch;     for (int j = 0; j < (int)adj[u].size(); j++) {       int v = adj[u][j];       ch.push_back(f(v));     }     sort(ch.begin(), ch.end());     int re = 0;     for (int i = 0; i < (int)ch.size(); i++) {       if (ch[i] == re)         re++;       else if (ch[i] > re)         break;     }     dp[u] = re;     return dp[u];   }   int main() {     int n, m;     cin >> n >> m;     vector<int> h(n, 0);     for (int i = 0; i < n; i++) scanf( %d , &h[i]);     adj.assign(n, vector<int>());     for (int i = 0; i < m; i++) {       int x, y;       scanf( %d%d , &x, &y);       x--;       y--;       adj[x].push_back(y);     }     dp.assign(n, -1);     int ans = 0;     int sz = 0;     for (int i = 0; i < n; i++) sz = max(sz, f(i) + 1);     vector<int> tp(sz, 0);     for (int i = 0; i < n; i++) tp[f(i)] ^= h[i];     for (int i = 0; i < sz; i++) ans |= tp[i];     if (ans == 0) {       cout <<  LOSE  << endl;       return 0;     }     int p = -1;     for (int i = 0; i < sz; i++) {       if (tp[i]) p = i;     }     for (int i = 0; i < n; i++)       if (f(i) == p && (h[i] ^ tp[p]) < h[i]) {         h[i] ^= tp[p];         tp[p] = 0;         for (int j = 0; j < (int)adj[i].size(); j++) {           int v = adj[i][j];           h[v] ^= tp[f(v)];           tp[f(v)] = 0;         }         break;       }     cout <<  WIN  << endl;     for (int i = 0; i < n; i++) cout << h[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   using ll = long long;   using ld = long double;   using pi = pair<int, int>;   using pl = pair<ll, ll>;   using vi = vector<int>;   using vl = vector<ll>;   using vpi = vector<pi>;   using si = set<int>;   using sl = set<ll>;   using qi = queue<int>;   using ql = queue<ll>;   template <class T>   bool uin(T& a, T b) {     return a > b ? (a = b, true) : false;   }   template <class T>   bool uax(T& a, T b) {     return a < b ? (a = b, true) : false;   }   template <class T>   bool uin(T& u, T& v, T a, T b) {     return v - u > b - a ? (u = a, v = b, true) : false;   }   template <class T>   bool uax(T& u, T& v, T a, T b) {     return v - u < b - a ? (u = a, v = b, true) : false;   }   namespace input {   template <class T>   void re(complex<T>& x);   template <class T1, class T2>   void re(pair<T1, T2>& p);   template <class T>   void re(vector<T>& a);   template <class T, size_t SZ>   void re(array<T, SZ>& a);   template <class T>   void re(T& x) {     cin >> x;   }   void re(double& x) {     string t;     re(t);     x = stod(t);   }   void re(ld& x) {     string t;     re(t);     x = stold(t);   }   template <class T, class... Ts>   void re(T& t, Ts&... ts) {     re(t);     re(ts...);   }   template <class T>   void re(complex<T>& x) {     T a, b;     re(a, b);     x = cd(a, b);   }   template <class T1, class T2>   void re(pair<T1, T2>& p) {     re(p.f, p.s);   }   template <class T>   void re(vector<T>& a) {     for (int i = 0; i < (((int)(a).size())); ++i) re(a[i]);   }   template <class T, size_t SZ>   void re(array<T, SZ>& a) {     for (int i = 0; i < (SZ); ++i) re(a[i]);   }   }  // namespace input   using namespace input;   namespace output {   void pr(int x) { cout << x; }   void pr(long x) { cout << x; }   void pr(ll x) { cout << x; }   void pr(unsigned x) { cout << x; }   void pr(unsigned long x) { cout << x; }   void pr(unsigned long long x) { cout << x; }   void pr(float x) { cout << x; }   void pr(double x) { cout << x; }   void pr(ld x) { cout << x; }   void pr(char x) { cout << x; }   void pr(const char* x) { cout << x; }   void pr(const string& x) { cout << x; }   void pr(bool x) { pr(x ?  true  :  false ); }   template <class T>   void pr(const complex<T>& x) {     cout << x;   }   template <class T1, class T2>   void pr(const pair<T1, T2>& x);   template <class T>   void pr(const T& x);   template <class T, class... Ts>   void pr(const T& t, const Ts&... ts) {     pr(t);     pr(ts...);   }   template <class T1, class T2>   void pr(const pair<T1, T2>& x) {     pr( { , x.f,  ,  , x.s,  } );   }   template <class T>   void pr(const T& x) {     pr( { );     bool fst = 1;     for (const auto& a : x) pr(!fst ?  ,   :   , a), fst = 0;     pr( } );   }   void ps() { pr(  n ); }   template <class T, class... Ts>   void ps(const T& t, const Ts&... ts) {     pr(t);     if (sizeof...(ts)) pr(   );     ps(ts...);   }   void pc() { pr( ] n ); }   template <class T, class... Ts>   void pc(const T& t, const Ts&... ts) {     pr(t);     if (sizeof...(ts)) pr( ,  );     pc(ts...);   }   }  // namespace output   using namespace output;   inline int bs(bool (*valid)(int), int l, int r, bool order) {     l--, r++;     if (!order) swap(l, r);     while (abs(r - l) > 1) {       int mid = (l + r) >> 1;       if (valid(mid))         r = mid;       else         l = mid;     }     valid(r);     return r;   }   struct dsu {     vector<int> p;     dsu(int n) { p.resize(n + 1); }     inline int get(int x) { return p[x] ? p[x] = get(p[x]) : x; }     inline bool mrg(int x, int y) {       return get(x) == get(y) ? false : p[get(x)] = get(y);     }   };   const int M = 1e9 + 7;   const ll lnf = 1e18 + 3;   const int inf = 1e9 + 3;   const int nax = 2e5 + 5;   typedef decay<decltype(M)>::type T;   struct mi {     T val;     explicit operator T() const { return val; }     mi() { val = 0; }     mi(const ll& v) {       val = (-M <= v && v <= M) ? v : v % M;       if (val < 0) val += M;     }     friend void pr(const mi& a) { pr(a.val); }     friend void re(mi& a) {       ll x;       re(x);       a = mi(x);     }     friend bool operator==(const mi& a, const mi& b) { return a.val == b.val; }     friend bool operator!=(const mi& a, const mi& b) { return !(a == b); }     friend bool operator<(const mi& a, const mi& b) { return a.val < b.val; }     mi operator-() const { return mi(-val); }     mi& operator+=(const mi& m) {       if ((val += m.val) >= M) val -= M;       return *this;     }     mi& operator-=(const mi& m) {       if ((val -= m.val) < 0) val += M;       return *this;     }     mi& operator*=(const mi& m) {       val = (ll)val * m.val % M;       return *this;     }     friend mi pow(mi a, ll p) {       mi ans = 1;       assert(p >= 0);       for (; p; p /= 2, a *= a)         if (p & 1) ans *= a;       return ans;     }     friend mi inv(const mi& a) {       assert(a != 0);       return pow(a, M - 2);     }     mi& operator/=(const mi& m) { return (*this) *= inv(m); }     friend mi operator+(mi a, const mi& b) { return a += b; }     friend mi operator-(mi a, const mi& b) { return a -= b; }     friend mi operator*(mi a, const mi& b) { return a *= b; }     friend mi operator/(mi a, const mi& b) { return a /= b; }   };   vi invs, fac, ifac;   void genFac(int SZ) {     invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ);     invs[1] = fac[0] = ifac[0] = 1;     for (int i = (2); i <= (SZ - 1); ++i)       invs[i] = M - (ll)M / i * invs[M % i] % M;     for (int i = (1); i <= (SZ - 1); ++i) {       fac[i] = (ll)fac[i - 1] * i % M;       ifac[i] = (ll)ifac[i - 1] * invs[i] % M;     }   }   ll comb(int a, int b) {     if (a < b || b < 0) return 0;     return (ll)fac[a] * ifac[b] % M * ifac[a - b] % M;   }   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     int test_case;     cin >> test_case;     while (test_case--) {       int n;       re(n);       vi a(n, 0);       re(a);       int q = 0, p = n - 1;       while (a[q] >= q) q++;       while (a[p] >= n - 1 - p) p--;       ps(q > p + 1 ?  YES  :  NO );     }   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18;   const double PI = acos(-1);   long long mod = 1e9 + 7;   struct segment {     long long l, r;     bool operator<(const segment& s) const {       if (r != s.r) return r > s.r;       return l < s.l;     }   };   const long long MAXN = 2e5 + 7;   vector<segment> vs(MAXN);   vector<long long> sum(MAXN, 0), sub(MAXN, 0), ans(MAXN, 0);   vector<vector<long long>> vv(MAXN);   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, k, l, r;     cin >> n >> k;     long long m = 1;     for (long long i = 0; i < n; i++) {       cin >> vs[i].l >> vs[i].r;       m = max(m, vs[i].r);       sum[vs[i].l]++;       sum[vs[i].r + 1]--;       vv[vs[i].l].push_back(i + 1);       vv[vs[i].r + 1].push_back(-i - 1);     }     for (long long i = 1; i <= m; i++) sum[i] += sum[i - 1];     set<pair<segment, long long>> s;     long long cntSub = 0;     long long cnt = 0;     for (long long i = 1; i <= m; i++) {       for (long long& idx : vv[i]) {         if (idx > 0) {           s.insert({vs[idx - 1], idx});         } else {           idx *= -1;           s.erase({vs[idx - 1], idx});         }       }       cntSub += sub[i];       while (sum[i] - cntSub > k) {         auto it = *s.begin();         cntSub++;         s.erase(it);         sub[it.first.r + 1]--;         ans[it.second] = 1;         cnt++;       }     }     cout << cnt <<   n ;     for (long long i = 1; i <= n; i++)       if (ans[i]) cout << i <<    ;     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int cum[200000];   int main() {     int n, x, k;     cin >> n >> x >> k;     int arr[n + 10];     set<int> st;     map<int, int> pos;     int now = 0;     for (int i = 1; i <= n; i++) {       scanf( %d , &arr[i]);       st.insert(arr[i]);     }     for (set<int>::iterator it = st.begin(); it != st.end(); it++) {       pos[*it] = ++now;     }     for (int i = 1; i <= n; i++) {       cum[pos[arr[i]]]++;     }     for (int i = 1; i <= now; i++) {       cum[i] += cum[i - 1];     }     long long res = 0;     for (int i = 1; i <= n; i++) {       long long pres = arr[i];       long long first, lst;       if (k > 0) {         pres = arr[i];         if (arr[i] % x != 0) pres = pres + (x - (arr[i] % x));         pres += (k - 1) * 1LL * x;         first = pres;         lst = pres + x - 1;       } else {         pres = arr[i];         if (arr[i] % x != 0) pres = pres + (x - (arr[i] % x));         first = arr[i];         lst = pres - 1;       }       if (*st.rbegin() < lst)         lst = *st.rbegin();       else {         if (st.find(lst) == st.end()) {           set<int>::iterator it = (st.upper_bound(lst));           it--;           lst = *it;         }       }       if (*st.rbegin() < first)         first = lst + 10;       else {         if (st.find(first) == st.end()) {           set<int>::iterator it = (st.upper_bound(first));           first = *it;         }       }       if (lst >= first) {         res += (cum[pos[lst]] - cum[pos[first] - 1]);       }     }     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   int a, b, mn = 1000000009, mx;   char c[59][59];   int ist, ien;   bool w;   int main() {     cin >> a >> b;     for (int i = 1; i <= a; i++)       for (int j = 1; j <= b; j++) {         cin >> c[i][j];         if (c[i][j] ==  * )           mn = min(j, mn), mx = max(j, mx), (w == 0 ? ist = i : ist = ist),           ien = i, w = 1;       }     for (int i = ist; i <= ien; i++) {       for (int j = mn; j <= mx; j++) cout << c[i][j];       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       for (int i = 1; i <= n; i++) cout << i <<    ;       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int q;     cin >> q;     while (q--) {       long long int b, w;       cin >> b >> w;       if (b >= w) {         if (b > 3 * w + 1) {           std::cout <<  NO  << std::endl;           continue;         } else {           std::cout <<  YES  << std::endl;           for (long long int i = 0; i < 2 * w; i += 2) {             std::cout << 2 <<     << i + 2 << std::endl;           }           for (long long int i = 0; i < 2 * b && i < 2 * w; i += 2) {             std::cout << 2 <<     << i + 3 << std::endl;           }           b = b - w;           if (b > 0) {             std::cout << 2 <<     << 1 << std::endl;           }           b--;           long long int i = 0;           while (b > 0) {             std::cout << 1 <<     << i + 2 << std::endl;             b--;             if (b > 0) std::cout << 3 <<     << i + 2 << std::endl;             b--;             i += 2;           }         }       } else {         if (w > 3 * b + 1) {           std::cout <<  NO  << std::endl;           continue;         } else {           std::cout <<  YES  << std::endl;           for (long long int i = 0; i < 2 * b; i += 2) {             std::cout << 2 <<     << i + 3 << std::endl;           }           for (long long int i = 0; i < 2 * w && i < 2 * b; i += 2) {             std::cout << 2 <<     << i + 4 << std::endl;           }           w = w - b;           if (w > 0) std::cout << 2 <<     << 2 << std::endl;           w--;           long long int i = 0;           while (w > 0) {             std::cout << 1 <<     << i + 3 << std::endl;             w--;             if (w > 0) std::cout << 3 <<     << i + 3 << std::endl;             w--;             i += 2;           }         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[100010];   int s[100010];   int solve() {     int ret = 0, top = 0;     for (int i = 0; i < n; i++) {       while (top && a[s[top]] < a[i]) --top;       if (top) ret = max(ret, a[s[top]] ^ a[i]);       s[++top] = i;     }     return ret;   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     reverse(a, a + n);     int ans = solve();     reverse(a, a + n);     ans = max(ans, solve());     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long pw(long long a, long long b, long long p) {     if (b == 0) return 1;     long long t = pw(a, b / 2, p);     if (b % 2)       return ((a * t * t) % p);     else       return ((t * t) % p);   }   long long cost[100005], vis[100005], ini[100005], lb[100005];   long long abmc = 0, abtw = 1;   vector<long long> v[100005];   pair<long long, pair<long long, long long> > dfs(long long z, long long iniz,                                                    long long lev) {     if (vis[z]) {       if (ini[z] != iniz) return {-1, {-1, 1}};       return {lb[z], {-1, 1}};     }     vis[z] = 1;     lb[z] = lev;     ini[z] = iniz;     long long cmi = -1, mc = -1, tw = 1;     for (long long k : v[z]) {       pair<long long, pair<long long, long long> > t = dfs(k, iniz, lev + 1);       if (t.first == -1) continue;       if (cmi == -1 || t.first < cmi) cmi = t.first;       if (t.second.first == -1) continue;       if (mc == -1 || t.second.first < mc) {         mc = t.second.first;         tw = t.second.second;       } else if (mc == t.second.first)         tw += t.second.second;     }     lb[z] = cmi;     if (cmi == -1) {       abmc += cost[z];     } else if (cmi <= lev) {       if (cost[z] < mc || mc == -1) {         mc = cost[z];         tw = 1;       } else if (cost[z] == mc)         tw++;     }     if (cmi == lev) {       cmi = -1;       abmc += mc;       abtw = (abtw * tw) % 1000000007;       mc = -1;       tw = 1;     }     return {cmi, {mc, tw}};   }   void solve() {     long long n;     cin >> n;     for (long long i = 1; i <= n; i++) {       cin >> cost[i];     }     long long m;     cin >> m;     for (long long i = 0; i < m; i++) {       long long a, b;       cin >> a >> b;       v[a].push_back(b);     }     for (long long i = 1; i <= n; i++) {       if (!vis[i]) dfs(i, i, 0);     }     cout << abmc <<     << abtw;   }   signed main() {     ios_base::sync_with_stdio(false);     long long t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = int(1e9) + 7;   const int MX = 200500;   long long f[MX];   long long f1[MX];   int pow_mod(long long a, int st) {     a %= mod;     long long res = 1;     while (st) {       if (st & 1) {         res *= a;         res %= mod;       }       a *= a;       a %= mod;       st >>= 1;     }     return res;   }   void init() {     f[0] = 1;     for (int i = 1; i < MX; i++) {       f[i] = f[i - 1] * i;       f[i] %= mod;     }     f1[MX - 1] = pow_mod(f[MX - 1], mod - 2);     for (int i = MX - 2; i >= 0; i--) {       f1[i] = f1[i + 1] * (i + 1);       f1[i] %= mod;     }   }   int c(int n, int k) {     long long res = f[n] * f1[k];     res %= mod;     res *= f1[n - k];     return res % mod;   }   int go1(int, int);   int go0(int c0, int c1) {     if (c0 < 0 || c1 < 0) return 0;     if (c0 == 1 && c1 == 0) return 1;     if (c0 == 0 && c1 == 1) return 0;     int ans = 0;     if (c1) ans = c(c0 + c1 - 1, c0);     if (c0) ans += go1(c0 - 1, c1);     return ans % mod;   }   int go1(int c0, int c1) {     if (c0 == 0 && c1 == 1) return 1;     return go0(c0 - 1, c1);   }   int main() {     init();     ios_base::sync_with_stdio(false);     int n, m, g;     cin >> n >> m >> g;     if (g)       cout << go1(n, m) << endl;     else       cout << go0(n, m) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename TH>   void _dbg(const char* sdbg, TH h) {     cerr << sdbg <<  =  << h <<   n ;   }   template <typename TH, typename... TA>   void _dbg(const char* sdbg, TH h, TA... t) {     while (*sdbg !=  , ) cerr << *sdbg++;     cerr <<  =  << h <<  , ;     _dbg(sdbg + 1, t...);   }   template <class C>   void mini(C& a4, C b4) {     a4 = min(a4, b4);   }   template <class C>   void maxi(C& a4, C b4) {     a4 = max(a4, b4);   }   template <class T1, class T2>   ostream& operator<<(ostream& out, pair<T1, T2> pair) {     return out <<  (  << pair.first <<  ,   << pair.second <<  ) ;   }   template <class A, class B, class C>   struct Triple {     A first;     B second;     C third;     bool operator<(const Triple& t) const {       if (first != t.first) return first < t.first;       if (second != t.second) return second < t.second;       return third < t.third;     }   };   template <class T>   void ResizeVec(T&, vector<long long>) {}   template <class T>   void ResizeVec(vector<T>& vec, vector<long long> sz) {     vec.resize(sz[0]);     sz.erase(sz.begin());     if (sz.empty()) {       return;     }     for (T& v : vec) {       ResizeVec(v, sz);     }   }   template <class A, class B, class C>   ostream& operator<<(ostream& out, Triple<A, B, C> t) {     return out <<  (  << t.first <<  ,   << t.second <<  ,   << t.third <<  ) ;   }   template <class T>   ostream& operator<<(ostream& out, vector<T> vec) {     out <<  ( ;     for (auto& v : vec) out << v <<  ,  ;     return out <<  ) ;   }   const long long N = 1e5 + 5;   long long par[N];   long long rep[N];   long long sum2[N];   long long sz_cc[N];   long long sz_sub[N];   long long score[N];   long long Find(long long x) {     if (rep[x] == x) {       return x;     }     return rep[x] = Find(rep[x]);   }   long long n;   long long res;   long long pre[N], post[N];   long long d;   vector<long long> slo[N];   long long IsAnc(long long a, long long b) {     return pre[a] <= pre[b] && post[b] <= post[a];   }   void Union(long long a, long long b) {     a = Find(a);     b = Find(b);     assert(IsAnc(a, b));     if (a == b) {       return;     }     res -= score[a];     res -= score[b];     long long lower = sz_sub[b];     sum2[a] -= lower * lower;     long long upper = n - lower;     sum2[b] -= upper * upper;     sum2[a] += sum2[b];     sz_cc[a] += sz_cc[b];     long long s = sz_cc[a];     score[a] = s * (s - 1) * (s - 2) + 2 * s * (s - 1) * (n - s) +                s * ((n - s) * (n - s) - sum2[a]);     res += score[a];     rep[b] = a;   }   void Dfs(long long v) {     sz_sub[v] = 1;     d++;     pre[v] = d;     for (auto nei : slo[v]) {       if (sz_sub[nei]) {         continue;       }       par[nei] = v;       Dfs(nei);       sum2[v] += sz_sub[nei] * sz_sub[nei];       sz_sub[v] += sz_sub[nei];     }     long long upper = n - sz_sub[v];     sum2[v] += upper * upper;     long long s = 1;     score[v] = s * (s - 1) * (s - 2) + 2 * s * (s - 1) * (n - s) +                s * ((n - s) * (n - s) - sum2[v]);     res += score[v];     d++;     post[v] = d;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cout << fixed << setprecision(10);     if (0) cout << fixed << setprecision(10);     cin.tie(0);     cin >> n;     for (long long i = (1); i <= (n); ++i) {       rep[i] = i;       sz_cc[i] = 1;     }     for (long long i = (1); i <= (n - 1); ++i) {       long long a, b;       cin >> a >> b;       slo[a].push_back(b);       slo[b].push_back(a);     }     Dfs(1);     long long q;     cin >> q;     cout << res <<   n ;     for (long long i = (1); i <= (q); ++i) {       long long a, b;       cin >> a >> b;       a = Find(a);       b = Find(b);       while (!IsAnc(a, b)) {         Union(par[a], a);         a = Find(a);       }       while (!IsAnc(b, a)) {         Union(par[b], b);         b = Find(b);       }       cout << res <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   long long n, i, j, h, k, t, d, m, a, b, c;   using namespace std;   char s;   string ss;   int main() {     cin >> t;     for (h = 1; h <= t; h++) {       cin >> n;       ss =  . ;       while (n > ss.length()) ss = ss +  . ;       cin >> a >> b >> c;       d = 0;       for (i = 0; i < n; i++) {         cin >> s;         if (s ==  R  && b > 0) {           b--;           d++;           ss[i] =  P ;         }         if (s ==  P  && c > 0) {           c--;           d++;           ss[i] =  S ;         }         if (s ==  S  && a > 0) {           a--;           d++;           ss[i] =  R ;         }       }       if (d >= (n + 1) / 2) {         i = 0;         while (a > 0) {           if (ss[i] ==  . ) ss[i] =  R , a--;           i++;         }         while (b > 0) {           if (ss[i] ==  . ) ss[i] =  P , b--;           i++;         }         while (c > 0) {           if (ss[i] ==  . ) ss[i] =  S , c--;           i++;         }         cout <<  YES  << endl;         for (i = 0; i < n; i++) cout << ss[i];         cout << endl;       } else         cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string s;     cin >> s;     queue<int> v1;     deque<int> v2;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  ( )         v1.push(i);       else {         v2.push_front(i);       }     }     int k = 0;     vector<int> res;     while (!v1.empty() && !v2.empty()) {       if (v1.front() > v2.front()) {         v1.pop();         v2.pop_front();         continue;       }       k += 2;       res.push_back(v2.front());       res.push_back(v1.front());       v1.pop();       v2.pop_front();     }     sort(res.begin(), res.end());     if (k == 0) return cout << 0, 0;     cout << 1 <<   n ;     cout << k <<   n ;     for (auto i : res) cout << i + 1 <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   long long a[maxn];   int main() {     int n;     scanf( %d , &n);     priority_queue<long long, vector<long long>, greater<long long>> que;     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       que.push(a[i]);     }     if (n == 1) {       cout << 0 << endl;     } else {       if (n % 2 == 0) que.push(0);       long long ans = 0;       long long sum = 0;       while (que.size() > 2) {         sum = que.top();         que.pop();         sum += que.top();         que.pop();         sum += que.top();         que.pop();         ans += sum;         que.push(sum);       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a[3], b[3];     for (int i = 0; i < 3; i++) cin >> a[i] >> b[i];     bool flag = false;     for (int i = 0; i < 2; i++) {       for (int j = 0; j < 2; j++) {         for (int p = 0; p < 2; p++) {           if (max(a[1], a[2]) <= a[0] && b[1] + b[2] <= b[0]) flag = true;           swap(a[0], b[0]);         }         swap(a[2], b[2]);       }       swap(a[1], b[1]);     }     if (flag)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, K;   void rip() {     puts( -1 );     exit(0);   }   int main() {     scanf( %d %d , &N, &K);     if (K == 1) {       rip();     } else if (K == 2) {       if (N <= 4)         rip();       else {         printf( %d n , N - 1);         for (int i = (1); i < (N); i++) printf( %d %d n , i, i + 1);       }     } else if (K == 3) {       if (N <= 3)         rip();       else {         printf( %d n , N - 1);         printf( 1 2 n2 3 n );         for (int i = (4); i < (N + 1); i++) printf( %d %d n , 3, i);       }     } else       rip();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k, l, i = 0, mod = 0;     cin >> k;     cin >> l;     while (l > 1) {       mod += l % k;       l = l / k;       i++;     }     if (mod > 0)       cout <<  NO ;     else       cout <<  YES  << endl << i - 1;     return 0;   }