#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1.01E6;   vector<int> ans[MAXN];   char C[MAXN];   int A[MAXN];   int B[MAXN];   int n, top1, top2;   int main(void) {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> (C + 1), n = strlen(C + 1);     for (int i = 1; i <= n; ++i) {       if (C[i] ==  0 ) {         if (top2 == 0)           A[++top1] = i, ans[i].push_back(i);         else {           int nowPos = B[top2--];           ans[nowPos].push_back(i), A[++top1] = nowPos;         }       } else {         if (top1 == 0) return puts( -1 ) * 0;         int nowPos = A[top1--];         ans[nowPos].push_back(i), B[++top2] = nowPos;       }     }     if (top2) return puts( -1 ) * 0;     int o = 0;     cout << top1 <<   n ;     for (int i = 1; i <= top1; ++i) {       int id = A[i];       cout << ans[id].size() <<    ;       for (int a : ans[id]) cout << a <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   const long long mod = 1000000007;   const int mxN = 100010;   vector<int> g[mxN];   int main(int argc, char** argv) {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());     int N, M, u, v;     cin >> N >> M;     for (int i = 0; i < M; i++) {       cin >> u >> v;       u--, v--;       g[u].push_back(v);       g[v].push_back(u);     }     vector<int> a;     for (int i = 0; i < N; i++) a.push_back(i);     for (int iti = 0; iti < 1000; iti++) {       shuffle(a.begin(), a.end(), rng);       int prev = -10;       vector<pair<int, int> > res;       for (int i = 0; i + 1 < N && (int)res.size() < M; i++) {         u = a[i];         v = a[i + 1];         bool f = find(g[u].begin(), g[u].end(), v) == g[u].end();         if (!f && prev == -10) break;         if (f) {           res.push_back({u, v});           if (prev == -10) prev = i;         } else {           if (prev != i - 1) {             bool f2 = find(g[u].begin(), g[u].end(), a[prev]) == g[u].end();             if (f2) {               res.push_back({u, a[prev]});             }           }           prev = -10;         }       }       if (prev != -10 && prev != N - 2 && (int)res.size() + 1 == M) {         u = a[N - 1];         bool f2 = find(g[u].begin(), g[u].end(), a[prev]) == g[u].end();         if (f2) {           res.push_back({u, a[prev]});         }         prev = -10;       }       if ((int)res.size() == M) {         for (auto p : res) cout << p.first + 1 <<     << p.second + 1 <<   n ;         return 0;       }     }     cout <<  -1 n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, sz[100005], msz[100005], xsz[100005], lsz[100005], son[100005],       ans[100005];   vector<int> G[100005];   void dfs(int u) {     sz[u] = 1;     lsz[u] = n;     son[u] = -1;     for (int v : G[u]) {       dfs(v), sz[u] += sz[v];       if (son[u] == -1 || sz[v] > sz[son[u]]) son[u] = v;       if (sz[v] > msz[u])         xsz[u] = msz[u], msz[u] = sz[v];       else         xsz[u] = max(xsz[u], sz[v]);       lsz[u] = min(lsz[u], sz[v]);     }     if (n - sz[u] > msz[u])       xsz[u] = msz[u], msz[u] = n - sz[u];     else       xsz[u] = max(xsz[u], n - sz[u]);     if (n - sz[u]) lsz[u] = min(lsz[u], n - sz[u]);   }   multiset<int> st, st2;   void ser1(int u) {     st.insert(sz[u]);     for (int v : G[u]) ser1(v);   }   void dfs1(int u) {     for (int v : G[u])       if (v != son[u]) {         dfs1(v);         st.clear();       }     if (son[u] != -1) dfs1(son[u]);     if (n - sz[u] != msz[u]) {       int L = xsz[u], R = msz[u], mid;       for (; L < R;) {         mid = L + R >> 1;         multiset<int>::iterator it = st.lower_bound(msz[u] - mid);         if (it != st.end() && (*it) <= mid - lsz[u])           R = mid;         else           L = mid + 1;       }       ans[u] = L;     }     for (int v : G[u])       if (v != son[u]) ser1(v);     st.insert(sz[u]);   }   void ser2(int u) {     st.erase(st.find(sz[u]));     for (int v : G[u]) ser2(v);   }   void dfs2(int u) {     st.erase(st.find(sz[u]));     for (int v : G[u])       if (v != son[u]) {         st2.insert(sz[u]);         dfs2(v);         ser1(v);         st2.erase(st2.find(sz[u]));       }     if (son[u] != -1) {       int v = son[u];       st2.insert(sz[u]);       dfs2(v);       st2.erase(st2.find(sz[u]));     }     for (int v : G[u])       if (v != son[u]) ser2(v);     if (n - sz[u] == msz[u]) {       int L = xsz[u], R = msz[u], mid;       for (; L < R;) {         mid = L + R >> 1;         multiset<int>::iterator it = st.lower_bound(msz[u] - mid),                                 it2 = st2.lower_bound(msz[u] - mid + sz[u]);         if (it != st.end() && (*it) <= mid - lsz[u])           R = mid;         else if (it2 != st2.end() && (*it2) <= mid - lsz[u] + sz[u])           R = mid;         else           L = mid + 1;       }       ans[u] = L;     }   }   int main() {     scanf( %d , &n);     for (int i = (1), LIM = (n); i <= LIM; i++) {       int u, v;       scanf( %d%d , &u, &v);       G[u].push_back(v);     }     dfs(0);     dfs1(G[0][0]);     dfs2(G[0][0]);     for (int i = (1), LIM = (n); i <= LIM; i++) printf( %d n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long first, long long second) {     return !second ? first : gcd(second, first % second);   }   long long x, y, z;   deque<long long> q, p;   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     ;     cin >> x;     for (int i = 1; i <= x; i++) {       if (i % 2 == 0) {         z += i;         q.push_back(i);       } else {         y += i;         p.push_back(i);       }     }     if (gcd(y, z) > 1) {       cout <<  Yes  << endl;       cout << q.size() <<    ;       while (!q.empty()) {         cout << q.front() <<    ;         q.pop_front();       }       cout << endl;       cout << p.size() <<    ;       while (!p.empty()) {         cout << p.front() <<    ;         p.pop_front();       }       cout << endl;     } else {       cout <<  No  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long ans, cr[40002], ar[40005], br[40005], nd[40005];   void build(int id, int l, int r) {     if (l == r) {       nd[id] = cr[l];       return;     }     int md = (l + r) >> 1;     build(id * 2, l, md);     build(id * 2 + 1, md + 1, r);     nd[id] = min(nd[2 * id], nd[2 * id + 1]);     ans -= nd[id];   }   int main() {     long long i, j, k, n, m, x, mx;     cin >> n;     queue<long long> Q;     m = (1 << (n + 1)) - 2;     Q.push(2);     Q.push(3);     mx = 0;     k = 0;     for (i = 1; i <= m; i++) {       cin >> x;       long long tp = Q.front();       Q.pop();       if (tp >= (1 << (n))) cr[k++] = x;       ar[tp] = x;       br[tp] = x + br[tp / 2];       mx = max(mx, br[tp]);       Q.push(2 * tp);       Q.push(2 * tp + 1);     }     ans = 0;     int p = (1 << n);     k = 1;     while (1) {       cr[k] = mx - br[p];       ans += cr[k];       k++;       p++;       if (p > (m + 1)) break;     }     build(1, 1, (m + 2));     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1002020, mod = 1e9 + 7;   int m, tot, ans = 1, mu[N], iv[N], pr[N];   bool vis[N];   void prework(int mx) {     iv[1] = mu[1] = 1;     for (int i = 2; i <= mx; i++) {       iv[i] = 1ll * iv[mod % i] * (mod - mod / i) % mod;       if (!vis[i]) pr[++tot] = i, mu[i] = -1;       for (int j = 1; j <= tot && i * pr[j] <= mx; j++) {         vis[i * pr[j]] = 1;         if (!(i % pr[j])) break;         mu[i * pr[j]] = -mu[i];       }     }   }   int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   int main() {     m = read();     prework(m);     for (int i = 2; i <= m; i++)       ans = (ans - 1ll * mu[i] * (m / i) * iv[m - m / i] % mod) % mod;     cout << (ans + mod) % mod << endl;     return 0;   }


#include <bits/stdc++.h>   int main() {     int HPY, ATKY, DEFY, HPM, ATKM, DEFM, h, a, d;     while (scanf( %d %d %d , &HPY, &ATKY, &DEFY) == 3) {       scanf( %d %d %d , &HPM, &ATKM, &DEFM);       scanf( %d %d %d , &h, &a, &d);       int mini = 1000000000;       for (int i = ATKY; i <= 301; i++)         for (int j = DEFY; j <= 301; j++) {           if (i - DEFM > 0) {             int cost = (i - ATKY) * a + (j - DEFY) * d;             int p1 = i - DEFM;             int k1 = HPM / p1;             if (HPM % p1 != 0) k1++;             int p2 = ATKM - j;             if (p2 > 0) {               int p3 = p2 * k1 + 1;               if (HPY < p3) cost += (p3 - HPY) * h;             }             if (cost < mini) mini = cost;           }         }       printf( %d n , mini);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double EPS = 1e-9;   int INF = 2000000000;   long long INFF = 8000000000000000000LL;   double PI = acos(-1);   int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};   int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};   inline string IntToString(int a) {     char x[100];     sprintf(x,  %d , a);     string s = x;     return s;   }   inline int StringToInt(string a) {     char x[100];     int res;     strcpy(x, a.c_str());     sscanf(x,  %d , &res);     return res;   }   inline string GetString(void) {     char x[1000005];     scanf( %s , x);     string s = x;     return s;   }   inline string uppercase(string s) {     int n = (int)s.size();     for (int(i) = (0); (i) < (n); ++(i))       if (s[i] >=  a  && s[i] <=  z ) s[i] = s[i] -  a  +  A ;     return s;   }   inline string lowercase(string s) {     int n = (int)s.size();     for (int(i) = (0); (i) < (n); ++(i))       if (s[i] >=  A  && s[i] <=  Z ) s[i] = s[i] -  A  +  a ;     return s;   }   inline void OPEN(string s) {     freopen((s +  .in ).c_str(),  r , stdin);     freopen((s +  .out ).c_str(),  w , stdout);   }   vector<int> angka;   int n, data[2005];   long long dewa[2005];   long long MOD = 1000000007;   bool sudah[2005];   int to[2005];   int conn;   int pisah;   long long risan;   long long C[2005][2005];   int main() {     scanf( %d , &n);     for (int(i) = (1); (i) <= (n); ++(i)) {       scanf( %d , &data[i]);       to[i] = data[i];     }     for (int(i) = (0); (i) <= (n); ++(i)) {       C[i][0] = C[i][i] = 1;       for (int(j) = (1); (j) <= (i - 1); ++(j))         C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;     }     dewa[1] = 0;     dewa[2] = 1;     dewa[0] = 1;     for (int(i) = (3); (i) <= (n); ++(i)) {       dewa[i] = (i - 1) * (dewa[i - 1] + dewa[i - 2]);       dewa[i] %= MOD;     }     memset(sudah, 0, sizeof(sudah));     for (int(i) = (1); (i) <= (n); ++(i)) {       if (to[i] == -1) continue;       int x = i;       while (x != -1) {         if (sudah[x]) goto hell;         sudah[x] = 1;         x = to[x];         if (x == i) goto hell;       }       ++conn;     hell:;     }     for (int(i) = (1); (i) <= (n); ++(i))       if (!sudah[i]) ++pisah;     for (int(i) = (0); (i) <= (conn); ++(i)) {       long long pos = (C[conn][i] * dewa[i + pisah]) % MOD;       risan = (risan + pos) % MOD;     }     printf( %d n , (int)risan);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int n, tot, u, v;   int c[N], head[N], last[N * 2], ver[N * 2];   void add(int x, int y) {     ver[++tot] = y;     last[tot] = head[x];     head[x] = tot;   }   void dfs(int x, int fa) {     cout << x <<    ;     for (int i = head[x]; i; i = last[i]) {       int y = ver[i];       if (y == fa) continue;       c[y] *= -1;       dfs(y, x);     }     if (c[x] == -1 && x != 1) {       cout << fa <<     << x <<    ;       c[fa] *= -1;       c[x] *= -1;     }     if (x != 1) {       if (fa == 1) {         for (int i = 1; i <= n; i++) {           if (c[i] == -1) {             cout << fa <<    ;             c[fa] *= -1;             return;           }         }       } else {         cout << fa <<    ;         c[fa] *= -1;       }     }   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> c[i];     for (int i = 1; i < n; i++) {       cin >> u >> v;       add(u, v);       add(v, u);     }     dfs(1, -1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long power_mod(long long num, long long g) {     if (g == 0) return 1;     if (g % 2 == 1)       return (num * power_mod((num * num) % 1000000007, g / 2)) % 1000000007;     return power_mod((num * num) % 1000000007, g / 2);   }   long long power(long long num, long long g) {     if (g == 0) return 1;     if (g % 2 == 1) return (num * power((num * num), g / 2));     return power((num * num), g / 2);   }   long long a[201], b[201], c[201];   long long d[201][201][201];   long long solve(long long x, long long y, long long z) {     long long e = 0;     if (d[x][y][z] > -1) return d[x][y][z];     long long ans = 0;     if (x > 0 && y > 0) {       ans = max(ans, solve(x - 1, y - 1, z) + a[x - 1] * b[y - 1]);     }     if (x > 0 && z > 0) {       ans = max(ans, solve(x - 1, y, z - 1) + a[x - 1] * c[z - 1]);     }     if (z > 0 && y > 0) {       ans = max(ans, solve(x, y - 1, z - 1) + b[y - 1] * c[z - 1]);     }     return d[x][y][z] = ans;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n;     cin >> n;     string s;     cin >> s;     vector<pair<long long, long long> > vec, vec1;     for (long long i = 0; i < n; i++) {       long long x = 1;       while (i + 1 < n && s[i] == s[i + 1]) x++, i++;       if (s[i] ==  ? )         vec.push_back(make_pair(x, 2));       else         vec.push_back(make_pair(x, s[i] -  0 ));     }     long long l = vec.size();     long long x = 0;     for (long long i = 0; i < l; i++) {       if (i == 0)         vec1.push_back(vec[0]);       else if (i == 1 && vec1[0].second == 2) {         vec1[0].first += vec[i].first;         vec1[0].second = vec[i].second;       } else if (vec[i].second < 2)         vec1.push_back(vec[i]), x++;       else {         if (i + 1 < l && vec[i + 1].second == vec1[x].second) {           vec1[x].first += vec[i].first + vec[i + 1].first;           i++;         } else           vec1.push_back(vec[i]), x++;       }     }     l = vec1.size();     if (l > 1 && vec1[l - 1].second == 2) {       vec1[l - 2].first += vec1[l - 1].first;       vec1.pop_back();     }     vec = vec1;     l = vec1.size();     if (l == 1) {       for (long long i = 1; i <= n; i++) {         cout << n / i <<    ;       }       cout << endl;       return 0;     }     cout << n;     for (long long i = 2; i <= n; i++) {       vec1.clear();       long long ans = 0;       l = vec.size();       long long prev = 0;       for (long long j = 0; j < l; j++) {         long long er = vec[j].first;         if (j > 0 && vec[j - 1].second == 2) er += vec[j - 1].first;         if (j + 1 < l && vec[j + 1].second == 2) er += vec[j + 1].first;         if (er >= i) {           vec1.push_back(vec[j]);           if (j + 1 < l && vec[j + 1].second == 2) vec1.push_back(vec[j + 1]);         } else {           long long rt = vec1.size();           if (rt > 0 && vec1[rt - 1].second == 2) {             vec1[rt - 2].first += vec1[rt - 1].first;             vec1.pop_back();           }           if (j + 1 < l && vec[j + 1].second == 2) {             vec[j + 2].first += vec[j + 1].first;             vec[j + 1].first = 0;           }         }         if (j + 1 < l && vec[j + 1].second == 2) {           prev += vec[j + 1].first + vec[j].first;           ans += prev / i;           prev = min(prev % i, vec[j + 1].first);           j++;         } else {           prev += vec[j].first;           ans += prev / i;           prev = 0;         }       }       cout <<     << ans;       vec = vec1;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char board[2048][2048];   int rows[2][2048];   int cols[2][2048];   int main() {     int N;     scanf( %d , &N);     for (int i = 0; i < N; i++)       for (int j = 0; j < N; j++) scanf(  %c , &board[i][j]);     int ans = 0;     for (int x = 0; x < 2; x++) {       for (int i = 0; i < N - 1; i++)         for (int j = N - 1; j > i; j--) {           int d = rows[x][i] ^ cols[x][j];           if (!d != (board[i][j] ==  0 )) {             ans++;             rows[x][i] ^= 1;             cols[x][j] ^= 1;           }           board[i][j] = board[j][i];         }     }     for (int i = 0; i < N; i++)       if (!(rows[0][i] ^ rows[1][i] ^ cols[0][i] ^ cols[1][i]) !=           (board[i][i] ==  0 ))         ans++;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     char a[n][5];     for (int i = 0; i < n; i++) {       for (int j = 0; j < 5; j++) cin >> a[i][j];     }     int flag = 0;     for (int i = 0; i < n; i++) {       if ((a[i][0] ==  O  && a[i][1] ==  O )) {         a[i][0] =  + ;         a[i][1] =  + ;         flag = 1;         break;       } else if ((a[i][3] ==  O  && a[i][4] ==  O )) {         a[i][3] =  + ;         a[i][4] =  + ;         flag = 1;         break;       }     }     if (flag) {       cout <<  YES  << endl;       for (int i = 0; i < n; i++) {         for (int j = 0; j < 5; j++) {           cout << a[i][j];         }         cout << endl;       }     } else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   double l;   int x[1005], b[1005];   const double eps = 1e-8;   double dp[1005];   double check(double mid) {     for (int i = 1; i <= n; i++) dp[i] = -1e10;     dp[0] = 0;     for (int i = 1; i <= n; i++) {       for (int j = 0; j < i; j++) {         dp[i] = max(dp[i],                     dp[j] - mid * (sqrt(abs(l - (x[i] - x[j])))) + (double)b[i]);       }     }     return dp[n];   }   void dfs(int step, double mid) {     if (step == 0) return;     for (int i = 0; i < step; i++) {       if (abs(dp[step] - (dp[i] - mid * sqrt(abs(x[step] - x[i] - l)) +                           (double)b[step])) < eps)         dfs(i, mid);     }     printf( %d  , step);   }   int main() {     scanf( %d%lf , &n, &l);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &x[i], &b[i]);     }     double l = 0, r = 100000000000;     while (l + eps < r) {       double mid = (l + r) / 2;       double qwq = check(mid);       if (qwq > -eps)         l = mid;       else         r = mid;     }     check(l);     dfs(n, l);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   const int N = 200031;   int n, m, a[N], b[N];   vector<int> g[N];   int used[N];   int dep[N];   vector<int> this_comp;   vector<pair<int, int> > order;   vector<pair<int, pair<int, int> > > solution;   int par[N];   int in_st[N];   int used_edge[N];   int par_edge[N];   set<int> sons[N];   vector<int> with_dep[N];   vector<int> not_st_list[N];   set<int>::iterator it;   void trace(int v) {     used[v] = 1;     this_comp.push_back(v);     for (int i = 0; i < g[v].size(); i++) {       int id = g[v][i];       int to = a[id] + b[id] - v;       if (used[to]) continue;       par[to] = v;       sons[v].insert(to);       par_edge[to] = id;       dep[to] = dep[v] + 1;       in_st[id] = 1;       trace(to);     }   }   void process_vertex(int v) {     vector<int> free_edges;     for (int i = 0; i < g[v].size(); i++) {       int id = g[v][i];       if (used_edge[id]) continue;       if (in_st[id]) continue;       free_edges.push_back(id);     }     while (free_edges.size() > 1) {       int a = free_edges.back();       free_edges.pop_back();       int b = free_edges.back();       free_edges.pop_back();       used_edge[a] = 1;       used_edge[b] = 1;       solution.push_back(make_pair(v, make_pair(a, b)));     }     if (free_edges.size() == 1) {       if (par[v] == 0) {         int id = free_edges[0];         used_edge[id] = 1;         return;       }       solution.push_back(make_pair(v, make_pair(par_edge[v], free_edges[0])));       used_edge[par_edge[v]] = 1;       used_edge[free_edges[0]] = 1;       sons[par[v]].erase(v);       return;     }   }   int get_out_of_st(int v) {     while (not_st_list[v].size() > 0) {       int id = not_st_list[v].back();       if (used_edge[id] == 0) return id;       not_st_list[v].pop_back();     }     return -1;   }   void resolve(int v) {     if (par[v] == 0) return;     if (used_edge[par_edge[v]]) return;     if (sons[par[v]].size() > 1) {       sons[par[v]].erase(v);       it = sons[par[v]].begin();       int othr = (*it);       sons[par[v]].erase(othr);       used_edge[par_edge[othr]] = 1;       used_edge[par_edge[v]] = 1;       solution.push_back(           make_pair(par[v], make_pair(par_edge[othr], par_edge[v])));       return;     }     int edge_in_father = get_out_of_st(par[v]);     if (edge_in_father != -1) {       sons[par[v]].erase(v);       solution.push_back(           make_pair(par[v], make_pair(par_edge[v], edge_in_father)));       used_edge[par_edge[v]] = 1;       used_edge[edge_in_father] = 1;       return;     }     if (dep[v] == 1) return;     int v2 = par[par[v]];     sons[par[v]].erase(v);     sons[v2].erase(par[v]);     solution.push_back(         make_pair(par[v], make_pair(par_edge[v], par_edge[par[v]])));     used_edge[par_edge[v]] = 1;     used_edge[par_edge[par[v]]] = 1;   }   void solve(int v) {     this_comp.clear();     trace(v);     int max_dep = 0;     for (int i = 0; i < this_comp.size(); i++) {       max_dep = max(max_dep, dep[this_comp[i]]);     }     order.clear();     for (int i = 0; i < this_comp.size(); i++) {       int cur_v = this_comp[i];       for (int j = 0; j < g[cur_v].size(); j++) {         int e_id = g[cur_v][j];         if (in_st[e_id]) continue;         not_st_list[cur_v].push_back(e_id);       }       order.push_back(make_pair(dep[cur_v], cur_v));       with_dep[dep[cur_v]].push_back(cur_v);     }     for (int i = max_dep; i >= 0; --i) {       for (int j = 0; j < with_dep[i].size(); j++) {         int v_id = with_dep[i][j];         process_vertex(v_id);       }       for (int j = 0; j < with_dep[i].size(); j++) {         int v_id = with_dep[i][j];         resolve(v_id);       }       with_dep[i].clear();     }   }   int get_other(int id, int v) { return a[id] + b[id] - v; }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     cin >> m;     for (int i = 1; i <= m; i++) {       cin >> a[i] >> b[i];       g[a[i]].push_back(i);       g[b[i]].push_back(i);     }     for (int i = 1; i <= n; i++) {       if (used[i]) continue;       solve(i);     }     cout << solution.size() << endl;     for (int i = 0; i < solution.size(); i++) {       int id1 = solution[i].second.first;       int id2 = solution[i].second.second;       cout << get_other(id1, solution[i].first) <<     << solution[i].first <<                << get_other(id2, solution[i].first) <<   n ;     }     cin.get();     cin.get();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t, n, m, k, a, b, ans;   int maxl = 1e9;   int num[300005];   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d%d%d , &n, &m, &k);       if (m <= k) k = m - 1;       a = n - m + 1;       b = n - k;       for (int i = 1; i <= n; i++) {         scanf( %d , &num[i]);       }       ans = 0;       for (int i = 1; i <= n; i++) {         if (i + b - 1 > n) break;         int ansl = maxl;         for (int j = i; j <= i + b - 1; j++) {           if (j + a - 1 > i + b - 1) break;           ansl = min(ansl, max(num[j], num[j + a - 1]));         }         ans = max(ans, ansl);       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, c0, c1, h;       cin >> n >> c0 >> c1 >> h;       string s;       cin >> s;       int ans = 0;       int cnt = 0;       int mini = min(c0, c1);       for (int i = 0; i < n; i++) {         if (s[i] ==  1 ) {           ans += c1;           if (c1 != mini) cnt++;         } else {           ans += c0;           if (c0 != mini) cnt++;         }       }       ans = min(ans, ((n * mini) + (h * cnt)));       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 10;   long long n, Mx, B[N], S[N];   struct Node {     int32_t l, r, c;     Node() { l = r = c = 0; }   };   struct BST {     long long L = 32;     vector<Node> A;     BST(long long _L = 32) {       L = _L, A.clear();       A.push_back(Node());     }     void Add(long long val, long long w = 1) {       long long id = 0;       for (long long i = L - 1; ~i; i--) {         A[id].c += w;         if (val & (1ll << i)) {           if (!A[id].r) A[id].r = A.size(), A.push_back(Node());           id = A[id].r;           continue;         }         if (!A[id].l) A[id].l = A.size(), A.push_back(Node());         id = A[id].l;       }       A[id].c += w;     }     long long MaxXor(long long val) {       long long id = 0, ret = 0;       if (!A[id].c) return (-1);       for (long long i = L - 1; ~i; i--) {         if (A[id].r && (!A[id].l || !(val & (1ll << i))))           ret |= (1ll << i), id = A[id].r;         else           id = A[id].l;       }       return (ret);     }   };   int32_t main() {     scanf( %lld , &n);     BST T = BST(43);     T.Add(0);     for (long long i = 1; i <= n; i++) scanf( %lld , &B[i]);     for (long long i = n; i; i--) S[i] = S[i + 1] ^ B[i], T.Add(S[i]);     for (long long i = 0, r = 0; i <= n; i++, r ^= B[i])       Mx = max(Mx, T.MaxXor(r) ^ r), T.Add(S[i + 1], -1);     return !printf( %lld , Mx);   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( O3 )   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   int get_random() {     static uniform_int_distribution<int> dist(0, 1e9 + 6);     return dist(rng);   }   template <class T>   void make_unique(T& v) {     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());   }   int geti() {     int x;     cin >> x;     return x;   }   long long getll() {     long long x;     cin >> x;     return x;   }   double getd() {     double x;     cin >> x;     return x;   }   const int MAXN = 5e5 + 100;   void solve(int tt) {}   template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S),             F (*composition)(F, F), F (*id)()>   struct segment_tree {     int _l, _r;     vector<S> a;     vector<S> tree;     vector<F> lazy;     void push(int l, int r, int node) {       if (lazy[node] != id()) {         int mid = (l + r) / 2;         int rnode = node + (mid - l + 1) * 2;         tree[node + 1] = mapping(lazy[node], tree[node + 1]);         lazy[node + 1] = composition(lazy[node], lazy[node + 1]);         tree[rnode] = mapping(lazy[node], tree[rnode]);         lazy[rnode] = composition(lazy[node], lazy[rnode]);         lazy[node] = id();       }     }     void build(int l, int r, int node) {       if (l == r)         tree[node] = a[l];       else {         int mid = l + (r - l) / 2;         int rnode = node + (mid - l + 1) * 2;         build(l, mid, node + 1);         build(mid + 1, r, rnode);         tree[node] = op(tree[node + 1], tree[rnode]);       }     }     void update(F v, int left, int right, int l, int r, int node) {       if (left > right) return;       if (right < l || r < left) return;       if (left <= l && r <= right) {         tree[node] = mapping(v, tree[node]);         lazy[node] = composition(v, lazy[node]);       } else {         push(l, r, node);         int mid = l + (r - l) / 2;         int rnode = node + (mid - l + 1) * 2;         update(v, left, right, l, mid, node + 1);         update(v, left, right, mid + 1, r, rnode);         tree[node] = op(tree[node + 1], tree[rnode]);       }     }     S query(int left, int right, int l, int r, int node) {       if (left > right) return e();       if (right < l || r < left) return e();       if (left <= l && r <= right) return tree[node];       push(l, r, node);       int mid = l + (r - l) / 2;       int rnode = node + (mid - l + 1) * 2;       S r1 = query(left, right, l, mid, node + 1);       S r2 = query(left, right, mid + 1, r, rnode);       return op(r1, r2);     }     vector<S> get_tree() {       vector<S> res;       for (int i = _l; i <= _r; i++) {         res.push_back(query(i, i));       }       return res;     }     void init(int l, int r) {       _l = l;       _r = r;       a.assign(r + 1, e());       tree.assign(2 * r + 1, e());       lazy.assign(2 * r + 1, id());     }     S& operator[](int idx) { return a[idx]; }     void build() { build(_l, _r, 0); }     void update(F v, int left, int right) { update(v, left, right, _l, _r, 0); }     S query(int left, int right) { return query(left, right, _l, _r, 0); }   };   long long zero() { return 0; }   long long int_max() { return INT_MAX; }   long long int_min() { return INT_MIN; }   long long get_min(long long x, long long y) { return min(x, y); }   long long get_max(long long x, long long y) { return max(x, y); }   long long add(long long x, long long y) { return x + y; }   long long mul(long long x, long long y) { return x * y; }   pair<int, int> f1(pair<int, int> x, pair<int, int> y) {     return pair<int, int>({min(x.first, y.first), max(x.second, y.second)});   };   pair<int, int> f2() { return pair<int, int>({1e9, -1e9}); };   pair<int, int> f3(int x, pair<int, int> y) {     return pair<int, int>(         {min(y.second * x, y.first * x), max(y.second * x, y.first * x)});   };   int f4(int x, int y) { return x * y; };   int f5() { return 1; };   segment_tree<pair<int, int>, f1, f2, int, f3, f4, f5> segt;   vector<int> graph1[MAXN];   vector<int> graph2[MAXN];   int in[MAXN], out[MAXN], level[MAXN], t = 1;   int cnt[MAXN];   int n;   void dfs(int i, int p, int c, int l) {     ;     ;     in[i] = t++;     cnt[in[i]] = c;     segt[in[i]] = (c % 2 ? pair<int, int>({-l, -l}) : pair<int, int>({l, l}));     level[i] = l;     for (auto j : graph1[i]) {       if (j == p) continue;       dfs(j, i, c, l + 1);     }     for (auto j : graph2[i]) {       if (j == p) continue;       dfs(j, i, c + 1, l + 1);     }     out[i] = t;   }   pair<int, int> find_root() {     queue<int> q({1});     vector<int> vis(n + 1);     vis[1] = 1;     int res1 = 1;     while (q.size()) {       int len = q.size();       while (len--) {         auto f = q.front();         q.pop();         for (auto p : graph1[f]) {           if (!vis[p]) {             q.push(p);             res1 = p;             vis[p] = 1;           }         }         for (auto p : graph2[f]) {           if (!vis[p]) {             q.push(p);             res1 = p;             vis[p] = 1;           }         }       }     }     int res2 = res1;     q.push(res1);     vis = vector<int>(n + 1);     vis[res1] = 1;     while (q.size()) {       int len = q.size();       while (len--) {         auto f = q.front();         q.pop();         for (auto p : graph1[f]) {           if (!vis[p]) {             q.push(p);             res2 = p;             vis[p] = 1;           }         }         for (auto p : graph2[f]) {           if (!vis[p]) {             q.push(p);             res2 = p;             vis[p] = 1;           }         }       }     }     return {res1, res2};   }   int main(int argc, char* argv[]) {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cin >> n;     int u, v, tp;     vector<int> us, vs;     for (auto i = (0); i < (n - 1); i++) {       cin >> u >> v >> tp;       if (tp == 0) {         graph1[u].push_back(v);         graph1[v].push_back(u);       } else {         graph2[u].push_back(v);         graph2[v].push_back(u);       }       us.push_back(u);       vs.push_back(v);     }     pair<int, int> roots = find_root();     segt.init(1, n);     t = 1;     dfs(roots.first, -1, 0, 0);     segt.build();     int m;     cin >> m;     vector<int> idxs;     for (auto i = (0); i < (m); i++) {       int idx;       cin >> idx;       idx--;       idxs.push_back(idx);     }     vector<int> res1(m), res2(m);     for (auto i = (0); i < (m); i++) {       int idx = idxs[i];       u = us[idx], v = vs[idx];       if (level[u] > level[v]) swap(u, v);       ;       ;       segt.update(-1, in[v], out[v] - 1);       res1[i] = segt.query(1, n).second;     }     segt.init(1, n);     t = 1;     dfs(roots.second, -1, 0, 0);     segt.build();     ;     ;     ;     ;     for (auto i = (0); i < (m); i++) {       int idx = idxs[i];       u = us[idx], v = vs[idx];       if (level[u] > level[v]) swap(u, v);       ;       ;       segt.update(-1, in[v], out[v] - 1);       res2[i] = segt.query(1, n).second;     }     for (auto i = (0); i < (m); i++) {       cout << max(res1[i], res2[i]) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long k, n, d = 0, ans;   string s;   unsigned long long int sol1(int k);   int main() {     cin >> s;     n = s.size();     cin >> k;     ans = max(0ULL, min(2 * k, n + k - ((n + k) % 2)));     ans = max(ans, sol1(k));     cout << ans << endl;     const clock_t begin_time = clock();     return 0;   }   unsigned long long int sol1(int t) {     int bop = 0;     int anss = 0;     for (int i = 0; i < n; i++) {       for (int j = 1; j <= n; j++) {         bop = 0;         if (i + 2 * j - 1 >= n + t) {           bop = 1;         } else           for (int k = 0; k < j; k++) {             if (i + j + k < n) {               if (s[i + k] != s[i + j + k]) {                 bop = 1;                 break;               }             }           }         if (bop == 0) {           anss = max(anss, 2 * j);         }       }     }     return anss;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n;     while (cin >> n) {       int flag = 0;       long long int i;       for (int sx = 1; sx < 100; sx++) {         long long int x = (sqrt(sx * sx + 4 * n) - sx) / 2;         if (x * x + sx * x == n) {           int tem = x;           int t = 0;           while (tem > 0) {             t = t + tem % 10;             tem = tem / 10;           }           if (sx == t) {             flag = 1;             printf( %d n , x);             break;           }         }       }       if (!flag) printf( -1 n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Block {     long long l, r, c;     Block(long long l, long long r, long long c) : l(l), r(r), c(c) {}   };   bool operator<(Block a, Block b) {     if (a.l != b.l) return a.l < b.l;     if (a.r != b.r) return a.r < b.r;     return a.c < b.c;   }   const int maxn = 1000000 + 10;   long long tr_a[maxn], tr_b[maxn];   void add(long long tr[maxn], long long at, long long what) {     for (++at; at < maxn; at += -at & at) tr[at] += what;   }   void modify(long long l, long long r, long long c) {     add(tr_a, l, c);     add(tr_b, l, (l - 1) * c);     add(tr_a, r + 1, -c);     add(tr_b, r + 1, -r * c);   }   long long sum(long long tr[maxn], long long at) {     long long s = 0;     for (++at; at > 0; at -= -at & at) s += tr[at];     return s;   }   long long query(long long l, long long r) {     long long sr = r * sum(tr_a, r) - sum(tr_b, r);     long long sl = (l - 1) * sum(tr_a, l - 1) - sum(tr_b, l - 1);     return sr - sl;   }   int main() {     ios_base::sync_with_stdio(false);     memset((tr_a), (0), sizeof(tr_a)), memset((tr_b), (0), sizeof(tr_b));     int n, q;     cin >> n >> q;     set<Block> blocks;     for (int _n(n + 1), i(1); i < _n; i++) blocks.insert(Block(i, i, i));     for (int _n(q), _(0); _ < _n; _++) {       long long op, l, r, x;       cin >> op >> l >> r;       if (op == 1) {         cin >> x;         auto it = blocks.upper_bound(Block(l, 1011111111, 1011111111));         --it;         while (it != blocks.end()) {           if (it->l > r) break;           Block tmp = *it;           modify(max(l, tmp.l), min(r, tmp.r), labs(tmp.c - x));           if (tmp.l < l) {             blocks.insert(Block(tmp.l, l - 1, tmp.c));           }           if (tmp.r > r) {             blocks.insert(Block(r + 1, tmp.r, tmp.c));           }           ++it;           blocks.erase(blocks.find(tmp));         }         blocks.insert(Block(l, r, x));       } else {         long long ans = query(l, r);         cout << ans << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long fib[100001];   void fibonacci() {     fib[0] = 1;     fib[1] = 1;     fib[2] = 2;     for (long long i = 3; i <= 100000; i++) {       fib[i] = fib[i - 1] + fib[i - 2];       fib[i] = fib[i] % 1000000007;     }   }   double dist(double x, double y, double a, double b) {     return sqrt((x - a) * (x - a) + (y - b) * (y - b));   }   char alphabet[26] = { a ,  b ,  c ,  d ,  e ,  f ,  g ,  h ,  i ,                         j ,  k ,  l ,  m ,  n ,  o ,  p ,  q ,  r ,                         s ,  t ,  u ,  v ,  w ,  x ,  y ,  z };   long long power(long long x, unsigned long long y) {     long long res = 1;     while (y > 0) {       if (y & 1) res = res * x;       y = y >> 1;       x = x * x;     }     return res;   }   long long mpower(long long x, unsigned long long y, long long p) {     long long res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   void swap(long long* a, long long* b) {     long long temp = *a;     *a = *b;     *b = temp;   }   char nxtchr(long long c) {     if (c == 1) return  1 ;     if (c == 2) return  2 ;     if (c == 3) return  3 ;     if (c == 4) return  4 ;     if (c == 5) return  5 ;     if (c == 6) return  6 ;     if (c == 7) return  7 ;     if (c == 8) return  8 ;     if (c == 9) return  9 ;     if (c == 0) return  0 ;     return -1;   }   long long dislet(string s) {     long long i = 0, count = 0;     while (i < s.size()) {       char c = s[i];       long long j = i;       count++;       while (s[j] == c) j++;       i = j;     }     return count;   }   long long findIndex(long long key, vector<pair<long long, long long>> v,                       long long n) {     long long l = 0, r = n - 1;     while (l != r) {       if (l < r) {         long long mid = (l + r) / 2;         if (v[mid].first > key)           r = mid;         else           l = mid + 1;       }     }     return l;   }   vector<long long> ranks;   vector<long long> parent;   vector<long long> maxim;   void make_set(long long v) {     parent[v] = v;     ranks[v] = 0;   }   long long find_Set(long long v) {     if (parent[v] == v) return v;     return parent[v] = find_Set(parent[v]);   }   void union_sets(long long a, long long b) {     a = find_Set(a);     b = find_Set(b);     if (a != b) {       if (ranks[a] < ranks[b]) swap(&a, &b);       parent[b] = a;       if (ranks[a] == ranks[b]) ranks[a]++;     }   }   void dfsutil(long long i, vector<long long> adj[], bool visited[]) {     visited[i] = true;     for (long long j = 0; j < adj[i].size(); j++) {       if (!visited[adj[i][j]]) dfsutil(adj[i][j], adj, visited);     }   }   long long dfs(vector<long long> adj[], long long present[]) {     bool visited[26];     long long count = 0;     for (long long i = 0; i < 26; i++) visited[i] = false;     for (long long i = 0; i < 26; i++) {       if (present[i] && !visited[i]) {         dfsutil(i, adj, visited);         count++;       }     }     return count;   }   int32_t main() {     long long n;     cin >> n;     string s;     cin >> s;     long long i = 0;     long long count = 0;     while (i < s.size()) {       if (i % 2 == 0) {         char c = s[i];         long long j = i + 1;         while (s[j] == c && j <= s.size()) {           s.erase(s.begin() + j);           count++;         }         i = j;       } else         i++;     }     if (s.size() % 2 != 0 && count < n) {       s.erase(s.end() - 1);       count++;     }     cout << count <<   n ;     cout << s <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e3 + 5;   int n, a[maxn], belong[maxn], bn = 1;   int ans[maxn][maxn];   struct node {     int l, r;   } block[maxn];   bool cmp(node x, node y) { return (x.r - x.l) > (y.r - y.l); }   int main() {     int l, r, flag;     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i], belong[i] = 1;     belong[0] = 1;     block[1].l = 0;     block[1].r = n;     for (int i = 1; i <= n; i++) {       sort(block + 1, block + 1 + bn, cmp);       flag = 0;       int bto = bn;       for (int b = 1; b <= bto; b++) {         l = block[b].l;         r = block[b].r;         if (a[i] > bto - b + 1) {           if (flag == 0)             while (a[i] > bto - b && l < r) ans[i][l++] = 1, a[i]--;           else             while (a[i] > bto - b && l <= r) ans[i][l++] = 1, a[i]--;           if (--l < r) {             flag = 1;             block[++bn].l = block[b].l;             block[bn].r = l;             block[b].l = l + 1;             for (int j = block[bn].l; j <= l; j++) belong[j] = bn;           }         } else if (a[i] > 0) {           ans[i][l] = 1;           a[i]--;           if (l < r) {             flag = 1;             belong[l] = ++bn;             block[bn].l = block[bn].r = l;             block[b].l = l + 1;           }         }       }     }     cout << bn << endl;     for (int j = 0; j <= n; j++) {       for (int i = 1; i <= n; i++) {         cout << ans[i][j];       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 1505;   const int dx[] = {-1, 0, 1, 0};   const int dy[] = {0, 1, 0, -1};   int visx[MAX_N][MAX_N];   int visy[MAX_N][MAX_N];   char A[MAX_N][MAX_N];   int sx, sy;   int n, m;   queue<pair<int, int> > q;   bool in(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> A[i];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (A[i][j] ==  S ) {           A[i][j] =  . ;           sx = i, sy = j;         }       }     }     sx += n * m;     sy += n * m;     q.push(make_pair(sx, sy));     memset(visx, -1, sizeof(visx));     memset(visy, -1, sizeof(visy));     while (!q.empty()) {       pair<int, int> t = q.front();       q.pop();       if (A[(t.first + n) % n][(t.second + m) % m] ==  # ) continue;       if (visx[(t.first + n) % n][(t.second + m) % m] == t.first &&           visy[(t.first + n) % n][(t.second + m) % m] == t.second)         continue;       if (visx[(t.first + n) % n][(t.second + m) % m] == -1)         visx[(t.first + n) % n][(t.second + m) % m] = t.first;       if (visy[(t.first + n) % n][(t.second + m) % m] == -1)         visy[(t.first + n) % n][(t.second + m) % m] = t.second;       if (visx[(t.first + n) % n][(t.second + m) % m] != t.first ||           visy[(t.first + n) % n][(t.second + m) % m] != t.second) {         cout <<  Yes  << endl;         return 0;       }       for (int i = 0; i < 4; i++) {         int nx = dx[i] + t.first;         int ny = dy[i] + t.second;         q.push(make_pair(nx, ny));       }     }     cout <<  No  << 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 fac(long long n) {     if (n == 1) return 1;     return n * fac(n - 1);   }   long long power(long long x, long long y) {     long long res = 1;     for (long long i = 1; i <= y; i++) res = (res * x) % 1000000007;     return res % 1000000007;   }   int32_t main() {     long long T = 1;     while (T--) {       long long n;       cin >> n;       long long a[n];       long long e = 0, o = 0;       for (long long i = 0; i < n; i++) {         cin >> a[i];         if (i % 2 == 0)           e += a[i];         else           o += a[i];       }       long long ans = 0;       for (long long i = 0; i < n; i++) {         if (i % 2 == 0) {           if (e - a[i] == o) ans++;           e -= a[i];           o += a[i];         } else {           if (o - a[i] == e) ans++;           e += a[i];           o -= a[i];         }       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, a, b, ans;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> k >> a >> b;     if (k == 1) {       cout << (n - 1) * a;       return 0;     }     while (n > 1) {       if (n % k != 0 && n < k) {         ans += (n - 1) * a;         n = 1;       } else if (n % k != 0) {         ans += (n % k) * a;         n -= (n % k);       } else {         long long temp, temp2, val;         val = n / k;         temp = (n - val) * a;         ans += min(temp, b);         n = val;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double PI = 3.14159265358979323846L;   const long double E = 2.71828182845904523536L;   const int INF = (1 << 30) - 1;   const long long LINF = 1e18;   const long double eps = 1e-7;   const long long mod = 1e9 + 7;   unordered_map<long long, long long> u[65][10];   vector<pair<int, int> > v;   long long ans[65];   int k, n;   unordered_map<long long, int> match;   unordered_set<long long> skip;   long long solve(long long idx, int lvl, int c) {     if (u[lvl][c].count(idx)) {       return u[lvl][c][idx];     }     long long& r = u[lvl][c][idx];     if (match.count(idx)) {       if (match[idx] != c) {         r = 0;         return r;       }     }     if (lvl == k) {       r = 1;       return 1;     }     if (!skip.count(idx)) {       r = ans[lvl];       return r;     }     long long m1 = 0, m2 = 0;     for (int j = 0; j < 6; j++) {       if (j == c || j == (c ^ 1)) {         continue;       }       m1 += solve(idx * 2, lvl + 1, j);       m2 += solve(idx * 2 + 1, lvl + 1, j);     }     m1 %= mod;     m2 %= mod;     r = m1 * m2 % mod;     return r;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     std::cout << std::fixed << std::setprecision(20);     cin >> k >> n;     unordered_map<string, int> g;     g[ white ] = 0;     g[ yellow ] = 1;     g[ green ] = 2;     g[ blue ] = 3;     g[ red ] = 4;     g[ orange ] = 5;     for (int i = 0; i < n; i++) {       long long x;       string s;       cin >> x >> s;       match[x] = g[s];       while (x) {         skip.insert(x);         x /= 2;       }     }     ans[k] = 1;     for (int i = k - 1; i >= 1; i--) {       ans[i] = (4 * ans[i + 1]) % mod;       ans[i] = (ans[i] * ans[i]) % mod;     }     long long r = 0;     for (int i = 0; i < 6; i++) {       r += solve(1, 1, i);     }     cout << r % mod <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   bool isPrime(long long n) {     if (n <= 1) return false;     for (int i = 2; i <= sqrt(n); i++)       if (n % i == 0) return false;     return true;   }   long long mod = 1000000007;   int main() {     long long i, j, k = 0, n, m = 0, a[300001], b[300001], t = 0, ans = 0, i1 = 0,                     i2, h, p1, p2 = 0, q, l, f, c[2], mi;     char c1, c2, d[1001][1001];     string s;     double j1;     string e, dz;     std::vector<pair<long long, long long>> vect;     vector<long long> v;     map<string, long long> m1;     map<string, long long>::iterator itr;     cin >> n;     for (i = 0; i < n; i++) {       cin >> a[i];     }     sort(a, a + n);     j = 1;     for (i = 0; i < n; i++) {       if (a[i] >= j) {         t++;         j++;       }     }     cout << t;   }


#include <bits/stdc++.h>   using namespace std;   int lim = 1e6;   int inf = 1e9 + 7;   bool ispal(string s) {     int n = s.size();     bool ans = true;     for (int i = 0; i < n / 2; i++) {       if (s[i] != s[n - i - 1]) {         ans = false;         break;       }     }     return ans;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       string s;       cin >> s;       if (!ispal(s)) {         cout << s <<   n ;         ;       } else {         char x = s[0];         bool check = false;         string ans = s;         for (int i = 1; i < s.size(); i++) {           if (x != s[i]) {             check = true;             ans[0] = ans[i];             ans[i] = x;             break;           }         }         if (check) {           cout << ans <<   n ;           ;         } else {           cout << -1 <<   n ;           ;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b;     cin >> a >> b;     int ans = 1;     for (int i = 1; i <= min(a, b); i++) ans *= i;     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   long long int n;   int main() {     scanf( %lld , &n);     if (n == 1 || n == 3 || n == 4 || n == 6 || n == 9 || n == 12) {       cout << 0 << endl;       return 0;     }     int mod = n % 3, mx;     if (mod == 2) {       for (long long i = 1e7; i >= 1; i -= 3) {         if (i * i * 3 + i <= n * 2) {           mx = i;           break;         }       }     } else if (mod == 1) {       for (long long i = 1e7 + 1; i >= 1; i -= 3) {         if (i * i * 3 + i <= n * 2) {           mx = i;           break;         }       }     } else if (mod == 0) {       for (long long i = 1e7 + 5; i >= 1; i -= 3) {         if (i * i * 3 + i <= n * 2) {           mx = i;           break;         }       }     }     cout << (int)ceil((double)mx / 3) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> P[2];   int n;   int mark[100011];   int cnt[100011];   int cor(int p, int n) { return 2 * n + p; }   int main() {     int k, x;     cin >> n;     for (int i = 0; i < 2; i++) {       cin >> k;       for (int j = 0; j < k; j++) {         cin >> x;         P[i].push_back(x);       }     }     mark[cor(0, 0)] = mark[cor(1, 0)] = 2;     queue<int> Q;     Q.push(cor(0, 0));     Q.push(cor(1, 0));     while (Q.size()) {       int v = Q.front();       Q.pop();       if (mark[v] == 2) {         int p = v % 2;         v /= 2;         int val;         for (auto x : P[1 - p]) {           val = (v - x + 2 * n) % n;           x = cor(1 - p, val);           if (!mark[x]) {             mark[x] = 1;             Q.push(x);           }         }         continue;       }       int p = v % 2;       v /= 2;       int val;       for (auto x : P[1 - p]) {         val = (v - x + 2 * n) % n;         x = cor(1 - p, val);         cnt[x]++;         if (mark[x] == 0 and cnt[x] == P[x % 2].size()) {           Q.push(x);           mark[x] = 2;         }       }     }     for (int i = 1; i < n; i++) {       x = mark[cor(0, i)];       if (x == 1) {         cout <<  Win  ;       } else if (x == 2) {         cout <<  Lose  ;       } else {         cout <<  Loop  ;       }     }     cout <<   n ;     for (int i = 1; i < n; i++) {       x = mark[cor(1, i)];       if (x == 1) {         cout <<  Win  ;       } else if (x == 2) {         cout <<  Lose  ;       } else {         cout <<  Loop  ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18;   struct custom_hash {     static uint64_t splitmix64(uint64_t x) {       x += 0x9e3779b97f4a7c15;       x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;       x = (x ^ (x >> 27)) * 0x94d049bb133111eb;       return x ^ (x >> 31);     }     size_t operator()(uint64_t x) const {       static const uint64_t FIXED_RANDOM =           chrono::steady_clock::now().time_since_epoch().count();       return splitmix64(x + FIXED_RANDOM);     }   };   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;   }   long long mystoi(string str) {     stringstream ss(str);     long long ans = 0;     ss >> ans;     return ans;   }   long long exp(long long x, long long ex) {     long long ans = 1ll;     while (ex > 0) {       if (ex & 1ll) ans = (ans * x);       ex >>= 1ll;       x = (x * x);     }     return ans;   }   bool sort_cond(const pair<long long, long long>& a,                  const pair<long long, long long>& b) {     if (a.first == b.first) return a.second < b.second;     return a.first < b.first;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     cin >> t;     long long lo;     while (t--) {       long long n;       cin >> n;       vector<long long> a(2 * n);       cin >> a;       vector<long long> o, e;       for (long long i = (0); i < (long long)(2 * n); i++) {         if (a[i] % 2)           o.push_back(i + 1);         else           e.push_back(i + 1);       }       long long cnt = 0;       if (o.size() % 2) o.pop_back();       if (e.size() % 2) e.pop_back();       if (o.size() > e.size()) {         for (long long i = (0); i < (long long)(o.size()); i++) {           if (cnt == n - 1) break;           cout << o[i] <<     << o[i + 1] << endl;           cnt++;           i++;         }         for (long long i = (0); i < (long long)(e.size()); i++) {           if (cnt == n - 1) break;           cout << e[i] <<     << e[i + 1] << endl;           cnt++;           i++;         }       } else {         for (long long i = (0); i < (long long)(e.size()); i++) {           if (cnt == n - 1) break;           cout << e[i] <<     << e[i + 1] << endl;           cnt++;           i++;         }         for (long long i = (0); i < (long long)(o.size()); i++) {           if (cnt == n - 1) break;           cout << o[i] <<     << o[i + 1] << endl;           cnt++;           i++;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     char last, f;     cin >> last;     int ans = 0;     f = last;     for (int i = 1; i < n; i++) {       char a;       cin >> a;       if (a != last) ans++;       last = a;     }     if (f ==  F )       cout <<  NO n ;     else       cout << ((ans & 1) ?  YES n  :  NO n );   }


#include <bits/stdc++.h>   using namespace std;   deque<int> a[355];   int n, m, S, Q, z, L, R, k, l, r, x, c[355][100005];   int ans;   void deal(int &x) { x = (x + ans - 1) % n + 1; }   int main() {     scanf( %d , &n);     S = int(sqrt(n)), m = n / S + 1;     for (int i = 0; i < n; i++) {       scanf( %d , &x);       a[i / S].push_back(x);       c[i / S][x]++;     }     scanf( %d , &Q);     while (Q--) {       scanf( %d%d%d , &z, &L, &R);       deal(L), deal(R);       if (L > R) swap(L, R);       L--, l = L / S, r = R / S;       if (z == 2) {         scanf( %d , &k);         deal(k);         ans = 0;         if (l == r) {           for (int i = L % S, _ = R % S; i < _; i++) ans += a[l][i] == k;         } else {           for (int i = l + 1; i < r; i++) ans += c[i][k];           for (int i = L % S, _ = a[l].size(); i < _; i++) ans += a[l][i] == k;           for (int i = 0, _ = R % S; i < _; i++) ans += a[r][i] == k;         }         printf( %d n , ans);       } else {         if (l == r) {           R = R % S - 1, x = a[r][R], a[r].erase(a[r].begin() + R);           a[l].insert(a[l].begin() + L % S, x);         } else {           for (int i = l; i < r;) {             x = a[i].back(), a[i].pop_back(), c[i][x]--, i++;             a[i].push_front(x), c[i][x]++;           }           R %= S, x = a[r][R], a[r].erase(a[r].begin() + R), c[r][x]--;           a[l].insert(a[l].begin() + L % S, x), c[l][x]++;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int a[500001], pref[500001], valv[500001];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long int t, i, j, k, l, m = 0, n, o, p, q, r, s, x;     cin >> n >> x;     pref[0] = 0;     valv[0] = 0;     for (i = 0; i < n; i++) {       cin >> a[i];       valv[i + 1] = valv[i] + (a[i] * (a[i] + 1)) / 2;       a[i + n] = a[i];       pref[i + 1] = pref[i] + a[i];       m = max(m, a[i]);     }     for (i = n; i < 2 * n; i++) {       pref[i + 1] = pref[i] + a[i];       valv[i + 1] = valv[i] + (a[i] * (a[i] + 1)) / 2;     }     long long int ans = 0;     if (m >= x) {       while (x > 0) {         ans += m;         m--;         x--;       }       cout << ans <<   n ;       return 0;     }     for (i = 0; i < 2 * n; i++) {       long long int start = 0;       long long int end = i, index = 0;       while (start <= end) {         long long int mid = (start + end) / 2;         if (pref[i + 1] - pref[mid] >= x) {           index = mid;           start = mid + 1;         } else {           end = mid - 1;         }       }       long long int ss = pref[i + 1] - pref[index + 1], ans1, s1, s2;       ans1 = valv[i + 1] - valv[index + 1];       long long int sum = x - ss;       s1 = a[index];       ans1 += (s1 * (s1 + 1)) / 2;       long long int z = max(0LL, s1 - sum);       ans1 -= (z * (z + 1)) / 2;       ans = max(ans1, ans);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   const long long mod = 998244353;   long long qpow(long long a, long long b) {     long long ans = 1;     while (b) {       if (b & 1) ans = ans * a % mod;       a = a * a % mod;       b /= 2;     }     return ans;   }   long long b[maxn], inv[maxn];   long long C(long long n, long long m) {     if (m > n) return 0;     return b[n] * inv[m] % mod * inv[n - m] % mod;   }   long long Lucas(long long n, long long m) {     if (m == 0) return 1;     return C(n % mod, m % mod) * Lucas(n / mod, m / mod) % mod;   }   long long S(long long n, long long m) {     long long ans = 0;     for (int k = 0; k <= m; k++) {       if (k & 1)         ans = (ans - C(m, k) * qpow(m - k, n) % mod + mod) % mod;       else         ans = (ans + C(m, k) * qpow(m - k, n) % mod) % mod;     }     return ans;   }   int main() {     int n, m;     cin >> n >> m;     if (m >= n)       cout << 0 << endl;     else {       b[0] = 1;       inv[0] = 1;       for (int i = 1; i <= n; i++)         b[i] = b[i - 1] * i % mod, inv[i] = qpow(b[i], mod - 2) % mod;       long long ans = S(n, n - m) * C(n, n - m) % mod;       if (m == 0)         cout << ans << endl;       else         cout << ans * 2 % mod << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   int q, x_1, y_1, x_2, y_2, k, len, a[35], b[35], c[35];   pair<int, int> dp[35][2][2][2];   int Add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }   int Minus(int a, int b) { return a < b ? a - b + mod : a - b; }   pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {     return make_pair(Add(a.first, b.first), Add(a.second, b.second));   }   pair<int, int> dfs(int now, int f1, int f2, int f3) {     if (now == -1) return make_pair(1, 0);     if (~dp[now][f1][f2][f3].first) return dp[now][f1][f2][f3];     pair<int, int> ret = make_pair(0, 0);     for (int i = 0; i <= (f1 ? a[now] : 1); i++)       for (int j = 0; j <= (f2 ? b[now] : 1); j++)         if ((i ^ j) <= (f3 ? c[now] : 1)) {           pair<int, int> tmp = dfs(now - 1, f1 && i == a[now], f2 && j == b[now],                                    f3 && (i ^ j) == c[now]);           ret = ret + tmp;           if (i ^ j) ret.second = (ret.second + (1ll << now) * tmp.first) % mod;         }     dp[now][f1][f2][f3] = ret;     return ret;   }   int calc(int x, int y, int k) {     if (x < 0 || y < 0) return 0;     memset(dp, -1, sizeof(dp));     len = 0;     while (x || y || k) {       a[len] = (x & 1);       b[len] = (y & 1);       c[len++] = (k & 1);       x >>= 1;       y >>= 1;       k >>= 1;     }     pair<int, int> ret = dfs(len - 1, 1, 1, 1);     return Add(ret.first, ret.second);   }   int main() {     scanf( %d , &q);     while (q--) {       scanf( %d%d%d%d%d , &x_1, &y_1, &x_2, &y_2, &k);       printf(            %d n ,           Minus(               Add(calc(x_2 - 1, y_2 - 1, k - 1), calc(x_1 - 2, y_1 - 2, k - 1)),               Add(calc(x_1 - 2, y_2 - 1, k - 1), calc(x_2 - 1, y_1 - 2, k - 1))));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool visited[100005];   vector<long long> arr[100005];   int main() {     std::ios::sync_with_stdio(false);     long long n, x;     cin >> n;     vector<pair<long long, long long> > ans;     for (long long i = 0; i < n; i++) {       cin >> x;       arr[x].push_back(i);     }     for (long long i = 0; i < 100005; i++) {       if (arr[i].size() == 1) {         ans.push_back(make_pair(i, 0));       } else if (arr[i].size() >= 2) {         bool good = 1;         long long dif = arr[i][1] - arr[i][0];         for (long long j = 1; j < arr[i].size(); j++) {           if (arr[i][j] != arr[i][j - 1] + dif) good = 0;         }         if (good) {           ans.push_back(make_pair(i, dif));         }       }     }     cout << ans.size() << endl;     for (long long i = 0; i < ans.size(); i++)       cout << ans[i].first <<     << ans[i].second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long modI(long long a, long long m);   long long gcd(long long a, long long b);   long long powM(long long x, long long y, long long m);   long long swap(long long a, long long b);   void swap(long long& a, long long& b) {     long long tp = a;     a = b;     b = tp;   }   long long gcd(long long x, long long y) {     if (x == 0) return y;     return gcd(y % x, x);   }   long long powM(long long x, long long y, long long m) {     long long ans = 1, r = 1;     x %= m;     while (r > 0 && r <= y) {       if (r & y) {         ans *= x;         ans %= m;       }       r <<= 1;       x *= x;       x %= m;     }     return ans;   }   long long modI(long long a, long long m) {     long long m0 = m, y = 0, x = 1;     if (m == 1) return 0;     while (a > 1) {       long long q = a / m;       long long t = m;       m = a % m;       a = t;       t = y;       y = x - q * y;       x = t;     }     if (x < 0) x += m0;     return x;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(0);     long long tt = 1;     while (tt--) {       long long n;       cin >> n;       long long a[n];       for (long long i = 0; i < n; i++) cin >> a[i];       long long ans = 0;       for (long long mx = 1; mx < 31; mx++) {         long long dp = 0;         long long tmp = 0;         for (long long i = 0; i < n; i++) {           if (a[i] <= mx) {             dp = max((long long)0, dp + a[i]);           }           tmp = max(dp, tmp);         }         ans = max(ans, tmp - mx);       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<long long> a(n + 1);     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     int m;     cin >> m;     long long ans = 0;     long long wt = 0, ht = 0;     while (m--) {       long long w, h;       cin >> w >> h;       ans = max(ans + ht, a[w]);       wt = w;       ht = h;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(0);     ios_base::sync_with_stdio(0);     string x, y;     cin >> x >> y;     char ans[200];     for (int i = 0; i < x.size(); i++) {       if (y[i] == x[i]) {         ans[i] = x[i];       } else if (y[i] < x[i]) {         ans[i] = y[i];       } else {         cout << -1 <<   n ;         return 0;       }     }     for (int i = 0; i < x.size(); i++) {       cout << ans[i];     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a[4] = {};     cin >> n;     for (int i = 0; i < n; i++) {       char c;       cin >> c;       a[0] += c ==  U ;       a[1] += c ==  D ;       a[2] += c ==  L ;       a[3] += c ==  R ;     }     long res = min(a[0], a[1]) * 2 + min(a[2], a[3]) * 2;     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = LLONG_MAX;   bool check(char a, char b) { return a ==  _  || a == b; }   long long foo(string s, string tail) {     if (!check(s[s.size() - 2], tail[0]) || !check(s.back(), tail[1])) return 0;     s[s.size() - 2] = tail[0];     s[s.size() - 1] = tail[1];     if (s[0] ==  0 ) return 0;     long long res = 1;     for (long long i = 0; i + 2 < s.size(); i++) {       if (s[i] ==  _ ) {         res *= i > 0 ? 10 : 9;       }     }     return res;   }   string x(string s, long long val) {     for (auto &i : s) {       if (i ==  X ) {         i =  0  + val;       }     }     return s;   }   int main() {     ios_base::sync_with_stdio(0);     string s;     cin >> s;     long long res = 0;     if (s.size() == 1) {       res = (long long)(s[0] ==  _  || s[0] ==  X  || s[0] ==  0 );     } else {       if (count(s.begin(), s.end(),  X )) {         for (long long i = 0; i < 10; i++) {           string temp = x(s, i);           res += foo(temp,  25 ) + foo(temp,  50 ) + foo(temp,  75 ) +                  foo(temp,  00 );         }       } else {         res += foo(s,  25 ) + foo(s,  50 ) + foo(s,  00 ) + foo(s,  75 );       }     }     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200;   int n, a[N], m;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     scanf( %d , &m);     for (int i = 0; i < m; i++) {       int x, y;       scanf( %d%d , &x, &y);       x--;       if (x - 1 >= 0) a[x - 1] += y - 1;       if (x + 1 < n) a[x + 1] += a[x] - y;       a[x] = 0;     }     for (int i = 0; i < n; i++) printf( %d n , a[i]);     return 0;   }


#include <bits/stdc++.h>   bool sev(int h, int m);   using namespace std;   int main() {     int snooze;     cin >> snooze;     int h;     int m;     int n = 0;     cin >> h >> m;     if (sev(h, m)) {       cout <<  0  << endl;     } else {       while (sev(h, m) == false) {         if (m >= snooze) {           m = m - snooze;         } else {           if (h == 0) h = 24;           int x = snooze;           x = x - m;           h = h - 1;           m = 60;           m = m - (x);         }         n++;       }       cout << n;     }     return 0;   }   bool sev(int h, int m) {     if (h % 10 == 7 || h / 10 % 10 == 7 || m % 10 == 7 || m / 10 % 10 == 7)       return true;     else       return false;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x, ans, sum = 0;     cin >> x;     x = abs(x);     for (int i = 0;; i++) {       sum += i;       if (sum >= x && (sum - x) % 2 == 0) {         ans = i;         break;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long int> g[200001];   vector<long long int> g1[200001];   vector<long long int> vec;   bool vis[200001];   void fun(long long int u) {     vis[u] = 1;     for (long long int i = 0; i < g[u].size(); i++) {       long long int v = g[u][i];       if (!vis[v]) {         fun(v);       }     }     vec.push_back(u);   }   bool st[200001];   bool foo = 1;   bool check(long long int u) {     if (!foo) return false;     st[u] = 1;     vis[u] = 1;     for (long long int i = 0; i < g[u].size(); i++) {       long long int v = g[u][i];       if (st[v]) {         foo = 0;         return false;       }       if (!vis[v]) check(v);     }     st[u] = 0;     return true;   }   int main() {     long long int t;     cin >> t;     while (t--) {       long long int n, m;       cin >> n >> m;       for (long long int i = 1; i <= n; i++) {         g[i].clear();         g1[i].clear();       }       vec.clear();       memset(vis, false, sizeof(vis));       memset(st, 0, sizeof(st));       for (long long int i = 1; i <= m; i++) {         long long int k, u, v;         cin >> k >> u >> v;         if (k == 1) {           g[u].push_back(v);         } else {           g1[u].push_back(v);           g1[v].push_back(u);         }       }       bool ans = 1;       foo = 1;       for (long long int i = 1; i <= n; i++) {         if (!vis[i]) {           check(i);           if (!foo) {             ans = 0;             break;           }         }       }       if (!ans) {         cout <<  NO n ;         continue;       }       memset(vis, 0, sizeof(vis));       for (long long int i = 1; i <= n; i++) {         if (!vis[i]) fun(i);       }       reverse(vec.begin(), vec.end());       map<pair<long long int, long long int>, bool> done;       for (long long int i = 0; i < vec.size(); i++) {         long long int u = vec[i];         for (long long int j = 0; j < g1[u].size(); j++) {           long long int v = g1[u][j];           long long int u1 = min(u, v), v1 = max(u, v);           if (!done[{u1, v1}]) {             done[{u1, v1}] = 1;             g[u].push_back(v);           }         }       }       cout <<  YES n ;       for (long long int i = 1; i <= n; i++) {         for (long long int j = 0; j < g[i].size(); j++) {           cout << i <<     << g[i][j] <<   n ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   int ans[maxn];   vector<int> xs[maxn], ys[maxn];   struct query {     int l, r, d, u, id;     query(int aa, int bb, int cc, int dd, int ee) {       l = aa;       r = bb;       d = cc;       u = dd;       id = ee;     }     query() {}   };   query qry[maxn];   vector<int> xq[maxn], yq[maxn];   struct SegmentTree {     SegmentTree *lc, *rc;     static SegmentTree mem[2000000];     static SegmentTree *ptr;     int pos;     SegmentTree() {}     ~SegmentTree() {}     SegmentTree(int L, int R) {       lc = rc = nullptr;       pos = 0;       if (L == R) return;       lc = new (ptr++) SegmentTree(L, ((L + R) >> 1));       rc = new SegmentTree(((L + R) >> 1) + 1, R);     }     void pull() { pos = min(lc->pos, rc->pos); }     void modify(int L, int R, int x, int v) {       if (L == R) {         pos = v;         return;       }       if (x <= ((L + R) >> 1))         lc->modify(L, ((L + R) >> 1), x, v);       else         rc->modify(((L + R) >> 1) + 1, R, x, v);       pull();     }     int ask(int L, int R, int l, int r) {       if (L > r || l > R) return maxn;       if (L >= l && R <= r) return pos;       return min(lc->ask(L, ((L + R) >> 1), l, r),                  rc->ask(((L + R) >> 1) + 1, R, l, r));     }   } * stx, *sty;   SegmentTree SegmentTree::mem[2000000];   SegmentTree *SegmentTree::ptr = mem;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m, k, q;     cin >> n >> m >> k >> q;     for (int i = 0; i < k; ++i) {       int x, y;       cin >> x >> y;       xs[x].push_back(y);       ys[y].push_back(x);     }     for (int i = 0; i < q; ++i) {       int x1, y1, x2, y2;       cin >> x1 >> y1 >> x2 >> y2;       qry[i] = query(x1, x2, y1, y2, i);       xq[x2].push_back(i);       yq[y2].push_back(i);     }     stx = new (SegmentTree::ptr++) SegmentTree(0, maxn - 1);     sty = new (SegmentTree::ptr++) SegmentTree(0, maxn - 1);     for (int i = 0; i < maxn; ++i)       if (xs[i].size() || xq[i].size()) {         for (int j : xs[i]) stx->modify(0, maxn - 1, j, i);         for (int j : xq[i]) {           if (stx->ask(0, maxn - 1, qry[j].d, qry[j].u) < qry[j].l) ++ans[j];         }       }     for (int i = 0; i < maxn; ++i)       if (ys[i].size() || yq[i].size()) {         for (int j : ys[i]) sty->modify(0, maxn - 1, j, i);         for (int j : yq[i]) {           if (sty->ask(0, maxn - 1, qry[j].l, qry[j].r) < qry[j].d) ++ans[j];         }       }     for (int i = 0; i < q; ++i) {       if (ans[i] == 2)         cout <<  NO  << endl;       else         cout <<  YES  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     scanf( %d %d , &n, &k);     if (n == 5 && k == 2) {       puts( YES );       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           if (i & 1)             printf( L );           else             printf( S );         }         puts(  );       }       return 0;     }     vector<string> ans(n,   );     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         int sum = (i + j) & 1;         if (sum == 0 && k > 0) {           ans[i] +=  L ;           k--;         } else {           ans[i] +=  S ;         }       }     }     if (k == 0) {       puts( YES );       for (int i = 0; i < n; i++) puts(ans[i].c_str());     } else {       puts( NO );     }   }


#include <bits/stdc++.h>   using namespace std;   int main(void) {     int t;     cin >> t;     while (t--) {       long long n, p, k;       cin >> n >> p >> k;       long long ans = 0, pre = 0;       vector<long long> a(n, -1);       for (long long i = 0; i < n; ++i) {         cin >> a[i];       }       sort(a.begin(), a.end());       for (long long i = 0; i <= k; ++i) {         long long sum = pre;         if (sum > p) break;         long long ct = i;         for (long long j = i + k - 1; j < n; j += k) {           if (sum + a[j] <= p) {             ct += k;             sum += a[j];           } else             break;         }         pre += a[i];         ans = max(ans, ct);       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void fast() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   }   int main() {     fast();     long long i, j, n, t, su = 0, k;     cin >> t;     vector<long long> v[t], s[t];     vector<pair<long long, pair<long long, long long>>> p;     for (i = 0; i < t; i++) {       cin >> n;       su = 0;       for (j = 0; j < n; j++) {         cin >> k;         v[i].push_back(k);         su += k;       }       for (j = 0; j < n; j++) {         p.push_back(make_pair(su - v[i][j], make_pair(i + 1, j + 1)));       }     }     sort(p.begin(), p.end());     for (i = 0; i < p.size() - 1; i++) {       if ((p[i].first == p[i + 1].first) &&           p[i].second.first != p[i + 1].second.first) {         cout <<  YES  << endl;         cout << p[i].second.first <<     << p[i].second.second << endl;         cout << p[i + 1].second.first <<     << p[i + 1].second.second << endl;         return 0;       }     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   unsigned int dp[(int)1e3 + 7], mn[(int)1e3 + 7], n;   int a[(int)1e3 + 7], b[26], ml = 0;   char aa[(int)1e3 + 7];   void dpi() {     dp[0] = 1;     mn[0] = 1;     ml = 1;     for (int i = 1; i < n; i++) {       int mm = a[i];       dp[i] = dp[i - 1];       mn[i] = mn[i - 1] + 1;       for (int j = i - 1; j > 0; j--) {         mm = min(a[j], mm);         if (mm >= i - j + 1) {           dp[i] += dp[j - 1];           dp[i] %= (int)1e9 + 7;           mn[i] = min(mn[j - 1] + 1, mn[i]);           ml = max(ml, i - j + 1);         }       }       mm = min(a[0], mm);       if (mm >= i + 1) {         dp[i]++;         mn[i] = 1;         ml = max(ml, i + 1);       }     }   }   int main() {     scanf( %d , &n);     scanf( %s , aa);     for (int i = 0; i < 26; i++) scanf( %d , &b[i]);     for (int i = 0; i < n; i++) a[i] = b[aa[i] -  a ];     dpi();     printf( %d n%d n%d , dp[n - 1], ml, mn[n - 1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double l[10], b[10];   vector<int> kk(10, -1);   vector<vector<int> > v;   int k, n;   void abc(int i, int sum) {     if (i == 0 && sum == 0) {       v.push_back(kk);       return;     }     if (i == 0) {       return;     }     for (int j = 0; j <= k; j++) {       if (sum - j < 0) break;       kk[i - 1] = j;       abc(i - 1, sum - j);     }   }   double A;   double ans = 0.0;   void solve(vector<int>& v) {     double sum = 0.0;     for (int i = 0; i < (1 << n); i++) {       double cur = 1.0;       int ed = 0;       double B = 0.0;       for (int j = 0; j < n; j++) {         double z = (l[j] + v[j] * 10) * 0.01;         z = min(z, 1.0);         if (i & (1 << j)) {           cur *= z;           ed++;         } else {           cur *= 1.0 - z;           B += b[j];         }       }       if (ed <= n / 2) cur *= A / (A + B);       sum += cur;     }     ans = max(ans, sum);   }   int main() {     cin >> n >> k >> A;     abc(n, k);     for (int i = 0; i < n; i++) {       cin >> b[i] >> l[i];     }     for (int i = 0; i < v.size(); i++) {       solve(v[i]);     }     printf( %.9f , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     long long n, k;     cin >> n >> k;     long long r = max(n - 2 * k, 0LL);     cout << n * (n - 1) / 2 - r * (r - 1) / 2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int r, b, y;     cin >> y >> b >> r;     int ans = min(y, b - 1);     ans = min(ans, r - 2);     ans = ans * 3 + 3;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10, mod = 998244353, M = 3e3 + 10;   int ksm(int x, int y) {     int ans = 1;     while (y) {       if (y & 1) ans = (long long)ans * x % mod;       x = (long long)x * x % mod;       y >>= 1;     }     return ans;   }   int a[N], w[N], f[M][M], g[M][M], inv[M << 1], sum[3];   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , a + i);     for (int i = 1; i <= n; i++) {       scanf( %d , w + i);       sum[a[i]] += w[i];       sum[2] += w[i];     }     for (int i = 0; i <= 2 * m; i++) inv[i] = ksm(sum[2] + i - m, mod - 2);     for (int i = m; i >= 0; i--) {       f[i][m - i] = g[i][m - i] = 1;       for (int j = m - i - 1; j >= 0; j--) {         f[i][j] = (1LL * (sum[1] + i + 1) * f[i + 1][j] +                    1LL * (sum[0] - j) * f[i][j + 1]) %                   mod * inv[i - j + m] % mod;         g[i][j] = (1LL * (sum[1] + i) * g[i + 1][j] +                    1LL * (sum[0] - j - 1) * g[i][j + 1]) %                   mod * inv[i - j + m] % mod;       }     }     for (int i = 1; i <= n; i++) {       if (a[i])         printf( %I64d n , 1LL * w[i] * f[0][0] % mod);       else         printf( %I64d n , 1LL * w[i] * g[0][0] % mod);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string n;     cin >> n;     if (n[0] !=  - ) {       cout << n;     } else {       char max =  0 ;       int mem;       for (int i = n.length() - 2; i < n.length(); ++i) {         if (n[i] >= max) {           max = n[i];           mem = i;         }       }       string merge = n.substr(0, mem) + n.substr(mem + 1, n.length());       int result = atoi(merge.c_str());       cout << result;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long inv[100010], fac[100010], p[100010], t[100010][20];   int n, l, r, P, tot, Phi;   int phi(int k) {     int i, s;     s = k;     for (i = 2; i * i <= k; i++) {       if (k % i == 0) s = s / i * (i - 1);       while (k % i == 0) k /= i;     }     if (k > 1) s = s / k * (k - 1);     return s;   }   long long qpow(long long a, long long b, long long mod) {     long long res = 1;     while (b) {       if (b & 1) res = res * a % mod;       b >>= 1;       a = a * a % mod;     }     return res;   }   void init() {     long long x = P;     Phi = phi(P);     for (int i = 2; (long long)i * i <= x; i++) {       if (x % i) continue;       while (x % i == 0) x /= i;       p[++tot] = i;     }     if (x > 1) p[++tot] = x;     inv[0] = inv[1] = 1;     fac[0] = fac[1] = 1;     for (int i = 2; i < 100010; i++) {       x = i;       for (int j = 1; j <= tot; j++) {         t[i][j] = t[i - 1][j];         if (x % p[j]) continue;         while (x % p[j] == 0) x /= p[j], t[i][j]++;       }       fac[i] = fac[i - 1] * x % P;       inv[i] = qpow(fac[i], Phi - 1, P);     }   }   long long C(int n, int m) {     long long res = 0;     if (n < m || n < 0) return 0;     res = fac[n] * inv[m] % P * inv[n - m] % P;     for (int i = 1; i <= tot; i++)       res = res * qpow(p[i], t[n][i] - t[m][i] - t[n - m][i], P) % P;     return res;   }   int main() {     scanf( %d%d%d%d , &n, &P, &l, &r);     init();     long long ans = 0;     for (int i = 0; i <= n; i++) {       int m = n - i;       ans = (ans + C(n, i) *                        (-C(m, (m + min(r, m) >> 1) + 1) + C(m, m + l + 1 >> 1)) %                        P) %             P;     }     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100100;   int n;   long long a[maxn];   map<long long, long long> mp;   long long mul(long long x, long long n) {     long long ret = 1;     while (n) {       if (n & 1) ret *= x;       x *= x;       n >>= 1;     }     return ret;   }   int main() {     while (~scanf( %d , &n)) {       mp.clear();       for (int i = (1); i < (n + 1); i++) {         cin >> a[i];         mp[a[i]]++;       }       long long ret = 0;       for (int i = (1); i < (n + 1); i++) {         for (int j = 0; j < (31); j++) {           long long cur = mul(2, j);           if (mp.find(cur - a[i]) != mp.end()) {             if (mp[cur - a[i]] == 1 && cur - a[i] == a[i])               continue;             else if (mp[cur - a[i]] > 1 && cur - a[i] == a[i])               ret += mp[cur - a[i]] - 1;             else               ret += mp[cur - a[i]];           }         }       }       cout << ret / 2 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     multiset<long long> heros;     for (int i = 0; i < n; i++) {       long long temp;       cin >> temp;       heros.insert(temp);     }     long long sum = accumulate(heros.begin(), heros.end(), 0LL);     int m;     cin >> m;     for (int i = 0; i < m; i++) {       long long def, atk;       cin >> def >> atk;       long long min_gold = numeric_limits<long long>::max();       auto it = heros.lower_bound(def);       if (it != heros.end()) {         min_gold = min(min_gold, max(0LL, atk - (sum - *it)));       }       if (it != heros.begin()) {         it--;         min_gold = min(min_gold, max(0LL, atk - (sum - *it)) + (def - *it));       }       cout << min_gold <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int base(int a, int b) {     int ret = 0;     vector<int> v;     while (a > 0) {       v.push_back(a % b);       a /= b;     }     for (int i = 0; i < v.size(); i++) {       ret += v[i];     }     return ret;   }   int main() {     long long int a;     cin >> a;     long long int ans = 0;     long long int k = 0;     for (long long int i = 2; i < a; i++) {       ans += base(a, i);     }     k = a - 2;     for (int i = 2; i < k; i++) {       while (ans % i == 0 && k % i == 0) {         ans /= i;         k /= i;       }     }     cout << ans <<  /  << k;   }


#include <bits/stdc++.h>   using namespace std;   long long lmin(long long a, long long b) {     if (a < b) return a;     return b;   }   long long lmax(long long a, long long b) {     if (a > b) return a;     return b;   }   int main() {     long long t1, t2, x1, x2, t0;     scanf(  %lld %lld %lld %lld %lld , &t1, &t2, &x1, &x2, &t0);     long long mint = 100000000000LL, mint2 = 10000000000000000LL;     double min = 10000000000000.000;     if (t2 == t0 && t1 != t0) {       printf( 0 %lld n , x2);       return 0;     } else if (t2 == t0 && t1 == t0) {       printf( %lld %lld n , x1, x2);       return 0;     }     for (long long i = 1; i <= x1; i++) {       double temp = (double)((t0 - t1) * i);       temp /= (double)(t2 - t0);       long long temp2 = (long long)temp;       double t = ((double)t1 * (double)i + (double)t2 * (double)temp2) /                  (double)(i + temp2);       if (temp > x2) continue;       long long temp3 = temp2 * (t2 - t0) - i * (t0 - t1);       if (temp3 >= 0) {         if (t <= min) {           min = t;           mint = temp2;           mint2 = i;         }       } else {         t = ((double)t1 * (double)i + (double)t2 * (double)(temp2 + 1)) /             (double)(i + temp2 + 1);         if (temp2 + 1 > x2) continue;         if (t <= min) {           min = t;           mint = temp2 + 1;           mint2 = i;         }       }     }     long long i = 1;     for (i = 1; mint2 * i <= x1 && mint * i <= x2; i++)       ;     if (mint2 * i > x1 || mint * i > x2) i--;     if (i == 0) {       mint2 = 0;       mint = x2;       i = 1;     }     printf( %lld %lld n , mint2 * i, mint * i);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y, z;     cin >> x >> y >> z;     if (abs(x - y) > z) {       if (x > y) {         cout <<  + n ;       } else {         cout <<  - n ;       }     } else {       if (abs(x - y) == 0 && z == 0) {         cout <<  0 n ;       } else {         cout <<  ? n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MN = 1e4 + 5;   int N, K, i, x, y, sz[MN], mx[MN], ct, c[MN], dp[MN];   vector<int> adj[MN], elem[2];   vector<pair<int, int> > ans;   int dfs(int n, int p) {     sz[n] = 1;     mx[n] = 0;     for (auto v : adj[n]) {       if (v == p || c[v]) continue;       sz[n] += dfs(v, n);       mx[n] = max(mx[n], sz[v]);     }     return sz[n];   }   void dfs2(int n, int p, int tot) {     if (2 * mx[n] <= tot && 2 * (tot - sz[n]) <= tot) ct = n;     for (auto v : adj[n]) {       if (v == p || c[v]) continue;       dfs2(v, n, tot);     }   }   void op(int n, int p, int r) {     if (r ^ p) ans.push_back({n, r});     for (auto v : adj[n]) {       if (v == p || c[v]) continue;       op(v, n, r);     }   }   void op2(int n, int p, int d) {     elem[d].push_back(n);     for (auto v : adj[n]) {       if (v == p || c[v]) continue;       op2(v, n, d ^ 1);     }   }   void solve(int n) {     dfs2(n, 0, dfs(n, 0));     int cur = ct;     c[cur] = 1;     dfs(cur, 0);     pair<int, int> big(-1, -1);     for (auto v : adj[cur]) {       if (c[v]) continue;       if (sz[v] > big.first) big = {sz[v], v};     }     for (int i = 0; i < 2; i++) elem[i].clear();     for (auto v : adj[cur]) {       if (c[v]) continue;       if (v == big.second)         op2(v, cur, 0);       else         op(v, cur, cur);     }     int idx = 0;     if (elem[1].size() < elem[0].size()) idx = 1;     for (auto v : elem[idx])       if (v != big.second) ans.push_back({cur, v});     for (auto v : adj[cur]) {       if (c[v]) continue;       solve(v);     }   }   int main() {     scanf( %d%d , &N, &K);     for (i = 1; i < N; i++) {       scanf( %d%d , &x, &y);       adj[x].push_back(y);       adj[y].push_back(x);     }     solve(1);     assert(10 * N >= ans.size());     printf( %d n , (int)ans.size());     for (auto v : ans) printf( %d %d n , v.first, v.second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long n;     cin >> n;     long long int ans = 0, ans1, k, a1, a2, x, y, m, s = 0;     vector<pair<long, pair<long, long>>> r;     for (int i = 0; i < n; ++i) {       cin >> k;       s += k;       cin >> a1 >> x >> y >> m;       ans1 = 0;       r.push_back(make_pair(0, make_pair(a1, i + 1)));       for (int j = 1; j < k; ++j) {         a2 = ((a1 * x) % m + y % m) % m;         if (a2 < a1) ans1++;         a1 = a2;         if (r.size() <= 200000)           r.push_back(make_pair(ans1, (make_pair(a2, i + 1))));       }       ans = max(ans, ans1);     }     cout << ans <<   n ;     sort(r.begin(), r.end());     if (s <= 200000)       for (int i = 0; i < s; ++i)         cout << r[i].second.first <<     << r[i].second.second <<   n ;     return 0;   }


#include<bits/stdc++.h>      using namespace std;   typedef long long ll;   const int N=2e5+10,mod=1e9+7;   ll a[N];   #define PII pair<int,int>   #define x first   #define y second   #define PB push_back   void solve()   {    int n;       cin>>n;       int sum=0;       map<ll,int> mp;       for(int i=1;i<=n;i++){           cin>>a[i];       }       bool flag=1;       if(n==2){           if(a[1]==a[2]){               cout<< YES <<endl;           }           else cout<< NO <<endl;           return;       }       ll ans=0;       for(int i=1;i<=n;i++){           ans=(ans^a[i]);       }       if(ans==0){           cout<< YES <<endl;return;       }       int cnt=0;       ll ans1=0;          for(int i=1;i<=n;i++){           ans1=(ans1^a[i]);           if(ans1==ans){cnt++;ans1=0;}           if(cnt==2){               cout<< YES <<endl;return;           }           }       cout<< NO <<endl;   }   int main()   {    ios::sync_with_stdio(false);    cin.tie(0);    cout.tie(0);    int t;    cin >> t;    while (t--)     solve();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, a;     scanf( %d%d , &n, &m);     if (n == 1)       a = 1;     else if (m == 1)       a = 2;     else if (m == n)       a = n - 1;     else if (m - 1 < n - m)       a = m + 1;     else       a = m - 1;     printf( %d , a);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   set<pair<int, int> > topo[2];   vector<int> G[maxn], R[maxn];   int n, m, code[maxn], in[maxn], out[maxn];   bool seen[maxn];   void dfs(int node) {     seen[node] = 1;     for (int son : G[node])       if (!seen[son]) dfs(son);     topo[code[node]].insert({out[node], node});   }   int process() {     int answer = 0;     while (!topo[1].empty()) {       answer++;       for (int i = 0; i < 2; i++) {         while (!topo[i].empty() && topo[i].begin()->first == 0) {           auto aux = *topo[i].begin();           topo[i].erase(topo[i].begin());           for (int son : R[aux.second]) {             topo[code[son]].erase({out[son], son});             out[son]--;             topo[code[son]].insert({out[son], son});           }         }       }     }     return answer;   }   int main() {     ios::sync_with_stdio(false);     int x, y;     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> code[i];     for (int i = 1; i <= m; i++) {       cin >> x >> y;       x++;       y++;       G[x].push_back(y);       R[y].push_back(x);       in[y]++;       out[x]++;     }     for (int i = 1; i <= n; i++)       if (in[i] == 0) dfs(i);     cout << process();     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   map<string, int> ma;   int main() {     string s;     int n;     cin >> n;     ma.clear();     while (n--) {       cin >> s;       if (ma[s] == 0)         printf( NO n );       else         printf( YES n );       ma[s]++;     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, w, cnt1, cnt2;     scanf( %d , &n);     cnt1 = cnt2 = 0;     while (n-- > 0) {       scanf( %d , &w);       if (w == 100)         cnt1++;       else         cnt2++;     }     printf( %s n , cnt1 % 2 == 0 && (cnt2 % 2 == 0 || cnt1 >= 2) ?  YES  :  NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 20005;   const int INF = -1;   int n, m, p;   int a[N];   int b[N];   int F[2][N];   int Ra[N];   int Rb[N];   string Path;   inline void checkmax(int &t, const int x) {     if (x > t) t = x;   }   inline int w(int i, int j) {     int ret = a[i] + b[j];     if (ret >= p) ret -= p;     return ret;   }   long long Gao(int sx, int sy, int ex, int ey) {     if (sx == ex && sy == ey) return w(sx, sy);     int step = ex - sx + ey - sy;     if (step == 1) {       if (sx < ex)         Path +=  C ;       else         Path +=  S ;       return w(sx, sy) + w(ex, ey);     }     int s1 = step / 2;     int s2 = step - s1;     for (int j = 0; j + sy <= ey; j++) F[0][j] = F[1][j] = INF;     for (int j = sy; j <= ey; j++) Ra[j] = Rb[j] = INF;     F[0][0] = w(sx, sy);     for (int i = 1; i <= s1; i++) {       int p = i & 1;       for (int j = max(0, i + sx - ex); j <= i && j <= ey - sy; j++) {         F[p][j] = INF;         int nx = i - j + sx;         int ny = sy + j;         if (F[p ^ 1][j] != INF) checkmax(F[p][j], F[p ^ 1][j] + w(nx, ny));         if (j && F[p ^ 1][j - 1] != INF)           checkmax(F[p][j], F[p ^ 1][j - 1] + w(nx, ny));         if (i == s1) Ra[ny] = F[p][j];       }     }     for (int j = 0; j + sy <= ey; j++) F[0][j] = F[1][j] = INF;     F[0][0] = w(ex, ey);     for (int i = 1; i <= s2; i++) {       int p = i & 1;       for (int j = max(0, sx + i - ex); j <= i && j <= ey - sy; j++) {         F[p][j] = INF;         int nx = ex - (i - j);         int ny = ey - j;         if (F[p ^ 1][j] != INF) checkmax(F[p][j], F[p ^ 1][j] + w(nx, ny));         if (j && F[p ^ 1][j - 1] != INF)           checkmax(F[p][j], F[p ^ 1][j - 1] + w(nx, ny));         if (i == s2) Rb[ny] = F[p][j] - w(nx, ny);       }     }     int bx = -1;     int by = -1;     int ret = -1;     for (int j = sy; j <= ey; j++) {       if (Ra[j] != INF && Rb[j] != INF && Ra[j] + Rb[j] > ret) {         bx = sx + s1 - (j - sy);         by = j;         ret = Ra[j] + Rb[j];       }     }     Gao(sx, sy, bx, by);     Gao(bx, by, ex, ey);     return ret;   }   int main() {     scanf( %d%d%d , &n, &m, &p);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       a[i] %= p;     }     for (int i = 0; i < m; i++) {       scanf( %d , &b[i]);       b[i] %= p;     }     cout << Gao(0, 0, n - 1, m - 1) << endl;     cout << Path << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int INFint = 2e9;   const long long INF = 1e18;   const long long MOD = 1000000007ll;   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     vector<long long> a(n), b;     for (auto &x : a) cin >> x;     b = a;     sort(b.begin(), b.end());     b.resize(unique(b.begin(), b.end()) - b.begin());     vector<long long> dp[2];     dp[0].resize(b.size(), INF);     dp[1].resize(b.size(), INF);     dp[0][0] = abs(a[0] - b[0]);     for (int i = 1; i < b.size(); i++) {       dp[0][i] = min(dp[0][i - 1], abs(b[i] - a[0]));     }     int e = 1;     for (int i = 1; i < n; i++) {       dp[e][0] = dp[e ^ 1][0] + abs(a[i] - b[0]);       for (int j = 1; j < b.size(); j++) {         dp[e][j] = min(dp[e][j - 1], dp[e ^ 1][j] + abs(a[i] - b[j]));       }       e ^= 1;     }     cout << dp[e ^ 1][b.size() - 1];     fprintf(stderr,   nTIME = %lf n , 1.0 * clock() / CLOCKS_PER_SEC);     ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4e5 + 10, INF = 1e9;   int l[N], r[N], ps[N];   map<int, int> mp;   int cnt, seg[4 * N], lazy[4 * N];   inline void shift(int id) {     seg[2 * id] += lazy[id];     lazy[2 * id] += lazy[id];     seg[2 * id + 1] += lazy[id];     lazy[2 * id + 1] += lazy[id];     lazy[id] = 0;     return;   }   void add(int id, int L, int R, int s = 0, int e = cnt) {     if (L <= s && e <= R) {       seg[id]++;       lazy[id]++;       return;     }     if (L >= e || s >= R) return;     int mid = (s + e) / 2;     shift(id);     add(2 * id, L, R, s, mid);     add(2 * id + 1, L, R, mid, e);     seg[id] = min(seg[2 * id], seg[2 * id + 1]);     return;   }   int get(int id, int L, int R, int s = 0, int e = cnt) {     if (L <= s && e <= R) return seg[id];     if (L >= e || s >= R) return INF;     int mid = (s + e) / 2;     shift(id);     return min(get(2 * id, L, R, s, mid), get(2 * id + 1, L, R, mid, e));   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> l[i] >> r[i];       r[i]++;       mp[l[i]] = mp[r[i]] = 0;     }     for (auto X : mp) mp[X.first] = cnt++;     for (int i = 0; i < n; i++) {       l[i] = mp[l[i]];       r[i] = mp[r[i]];     }     for (int i = 0; i < n; i++) add(1, l[i], r[i]);     for (int i = 0; i < n; i++) {       int mn = get(1, l[i], r[i]);       if (mn > 1) return cout << i + 1 << endl, 0;     }     return cout << -1 << endl, 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 1500 + 10;   const int INF = 1e9;   int ans[MAX_N][26];   int main() {     ios::sync_with_stdio(0);     int n;     cin >> n;     string s;     cin >> s;     for (int it = 0; it < 26; ++it) {       char c = char(it +  a );       for (int i = 0; i < n; ++i) {         int cnt = 0;         for (int j = i; j < n; ++j) {           if (s[j] != c) cnt++;           ans[cnt][it] = max(ans[cnt][it], j - i + 1);         }       }       for (int i = 1; i <= n; ++i) {         if (ans[i][it] == 0) {           ans[i][it] = ans[i - 1][it];         }       }     }     int q;     cin >> q;     while (q--) {       int k;       char c;       cin >> k >> c;       cout << ans[k][(int)(c -  a )] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long w, m;     cin >> w >> m;     vector<int> a;     long long z = m;     while (z) {       a.push_back(z % w);       z /= w;     }     a.push_back(0);     int flag = 1;     for (int i = 0; i < a.size(); i++) {       if (a[i] == w || a[i] == w - 1) {         a[i] -= w;         a[i + 1] += 1;       }     }     for (int i = 0; i < a.size(); i++)       if (a[i] != 0 && a[i] != 1 && a[i] != -1) {         flag = 0;         break;       }     if (flag)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 7;   int n, a[N], f[N][21], pos[N], nxt[N], tmp[N], ans;   int query(int l, int r) {     int k = log2(r - l + 1);     return max(f[l][k], f[r - (1 << k) + 1][k]);   }   int main() {     scanf( %d , &n);     memset(pos, 0x3f, sizeof(pos));     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]), f[i][0] = a[i];       pos[tmp[a[i]]] = i;       tmp[a[i]] = i;     }     nxt[n] = n;     for (int i = n - 1; i >= 1; --i) nxt[i] = min(nxt[i + 1], pos[i] - 1);     for (int j = 1; j <= 20; ++j)       for (int i = 1; i + (1 << j) - 1 <= n; ++i)         f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);     for (int i = 1; i <= n; ++i) {       for (int j = i; j <= nxt[i];) {         int A = query(i, j);         if (A <= j - i + 1)           ans++, j++;         else           j = i + A - 1;       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000007;   const double PI = 3.141592653589793238463;   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int q;     cin >> q;     while (q--) {       long long sum = 0, r = 0;       long long x, y;       cin >> x;       for (int i = 1; i <= x; i++) {         cin >> y;         sum += y;         r ^= y;       }       cout <<  2 n  << r <<     << sum + r <<   n ;     }     return 0;   }