#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long n;     cin >> n;     vector<long long> tpa[6], tpb[4];     long long amt[6];     fill(amt, amt + 6, 0);     for (long long i = 0; i < n; ++i) {       long long a, b, c;       cin >> a >> b >> c;       --a;       --c;       if (c < a) {         swap(a, c);       }       if (a == 0 && c == 0) {         tpb[0].push_back(b);       } else if (a == 0 && c == 1) {         tpa[0].push_back(b);         ++amt[0];       } else if (a == 0 && c == 2) {         tpa[1].push_back(b);         ++amt[1];       } else if (a == 0 && c == 3) {         tpa[2].push_back(b);         ++amt[2];       } else if (a == 1 && c == 1) {         tpb[1].push_back(b);       } else if (a == 1 && c == 2) {         tpa[3].push_back(b);         ++amt[3];       } else if (a == 1 && c == 3) {         tpa[4].push_back(b);         ++amt[4];       } else if (a == 2 && c == 2) {         tpb[2].push_back(b);       } else if (a == 2 && c == 3) {         tpa[5].push_back(b);         ++amt[5];       } else {         tpb[3].push_back(b);       }     }     for (long long i = 0; i < 6; ++i) {       if (i < 4) sort((tpb[i]).begin(), (tpb[i]).end(), greater<long long>());       sort((tpa[i]).begin(), (tpa[i]).end(), greater<long long>());     }     long long ans = 0;     for (long long i = 0; i < 4; ++i) {       long long cr = 0;       for (long long j : tpb[i]) {         cr += j;       }       ans = max(ans, cr);     }     long long val[amt[0] + 1][amt[1] + 1][amt[2] + 1][amt[3] + 1][amt[4] + 1]                  [amt[5] + 1];     memset(val, 0, sizeof val);     for (long long a = 0; a <= amt[0]; ++a) {       for (long long b = 0; b <= amt[1]; ++b) {         for (long long c = 0; c <= amt[2]; ++c) {           for (long long d = 0; d <= amt[3]; ++d) {             for (long long e = 0; e <= amt[4]; ++e) {               for (long long f = 0; f <= amt[5]; ++f) {                 if (!a && !b && !c && !d && !e && !f) continue;                 if (a) {                   val[a][b][c][d][e][f] =                       max(val[a][b][c][d][e][f],                           val[a - 1][b][c][d][e][f] + tpa[0][a - 1]);                 }                 if (b) {                   val[a][b][c][d][e][f] =                       max(val[a][b][c][d][e][f],                           val[a][b - 1][c][d][e][f] + tpa[1][b - 1]);                 }                 if (c) {                   val[a][b][c][d][e][f] =                       max(val[a][b][c][d][e][f],                           val[a][b][c - 1][d][e][f] + tpa[2][c - 1]);                 }                 if (d) {                   val[a][b][c][d][e][f] =                       max(val[a][b][c][d][e][f],                           val[a][b][c][d - 1][e][f] + tpa[3][d - 1]);                 }                 if (e) {                   val[a][b][c][d][e][f] =                       max(val[a][b][c][d][e][f],                           val[a][b][c][d][e - 1][f] + tpa[4][e - 1]);                 }                 if (f) {                   val[a][b][c][d][e][f] =                       max(val[a][b][c][d][e][f],                           val[a][b][c][d][e][f - 1] + tpa[5][f - 1]);                 }               }             }           }         }       }     }     for (long long i = 0; i < 16; ++i) {       for (long long j = 0; j < 6; ++j) {         amt[j] = tpa[j].size();       }       long long cur = 0;       bool adj[4];       fill(adj, adj + 4, false);       for (long long j = 0; j < 4; ++j) {         if (i & (1 << j)) {           for (long long k : tpb[j]) {             cur += k;           }         } else {           adj[j] = 1;           if (j == 0) {             amt[0] = amt[1] = amt[2] = 0;           } else if (j == 1) {             amt[0] = amt[3] = amt[4] = 0;           } else if (j == 2) {             amt[1] = amt[3] = amt[5] = 0;           } else if (j == 3) {             amt[2] = amt[4] = amt[5] = 0;           }         }       }       if (!adj[0] && !amt[0] && !amt[1] && !amt[2]) continue;       if (!adj[1] && !amt[0] && !amt[3] && !amt[4]) continue;       if (!adj[2] && !amt[1] && !amt[3] && !amt[5]) continue;       if (!adj[3] && !amt[2] && !amt[4] && !amt[5]) continue;       bitset<4> dp[amt[0] + 1][amt[1] + 1][amt[2] + 1][amt[3] + 1][amt[4] + 1]                   [amt[5] + 1];       for (long long j = 0; j < 4; ++j) {         dp[amt[0]][amt[1]][amt[2]][amt[3]][amt[4]][amt[5]][j] = 1;       }       for (long long a = amt[0]; a >= 0; --a) {         for (long long b = amt[1]; b >= 0; --b) {           for (long long c = amt[2]; c >= 0; --c) {             for (long long d = amt[3]; d >= 0; --d) {               for (long long e = amt[4]; e >= 0; --e) {                 for (long long f = amt[5]; f >= 0; --f) {                   if (a != amt[0]) {                     if (dp[a + 1][b][c][d][e][f][0]) {                       dp[a][b][c][d][e][f][1] = 1;                     }                     if (dp[a + 1][b][c][d][e][f][1]) {                       dp[a][b][c][d][e][f][0] = 1;                     }                   }                   if (b != amt[1]) {                     if (dp[a][b + 1][c][d][e][f][0]) {                       dp[a][b][c][d][e][f][2] = 1;                     }                     if (dp[a][b + 1][c][d][e][f][2]) {                       dp[a][b][c][d][e][f][0] = 1;                     }                   }                   if (c != amt[2]) {                     if (dp[a][b][c + 1][d][e][f][0]) {                       dp[a][b][c][d][e][f][3] = 1;                     }                     if (dp[a][b][c + 1][d][e][f][3]) {                       dp[a][b][c][d][e][f][0] = 1;                     }                   }                   if (d != amt[3]) {                     if (dp[a][b][c][d + 1][e][f][2]) {                       dp[a][b][c][d][e][f][1] = 1;                     }                     if (dp[a][b][c][d + 1][e][f][1]) {                       dp[a][b][c][d][e][f][2] = 1;                     }                   }                   if (e != amt[4]) {                     if (dp[a][b][c][d][e + 1][f][3]) {                       dp[a][b][c][d][e][f][1] = 1;                     }                     if (dp[a][b][c][d][e + 1][f][1]) {                       dp[a][b][c][d][e][f][3] = 1;                     }                   }                   if (f != amt[5]) {                     if (dp[a][b][c][d][e][f + 1][2]) {                       dp[a][b][c][d][e][f][3] = 1;                     }                     if (dp[a][b][c][d][e][f + 1][3]) {                       dp[a][b][c][d][e][f][2] = 1;                     }                   }                 }               }             }           }         }       }       long long cans = 0;       for (long long a = max(0LL, amt[0]); a >= 0; --a) {         for (long long b = max(0LL, amt[1]); b >= 0; --b) {           for (long long c = max(0LL, amt[2]); c >= 0; --c) {             for (long long d = max(0LL, amt[3]); d >= 0; --d) {               for (long long e = max(0LL, amt[4]); e >= 0; --e) {                 for (long long f = max(0LL, amt[5]); f >= 0; --f) {                   bool usd[4];                   fill(usd, usd + 4, 0);                   if (a < amt[0] || b < amt[1] || c < amt[2]) usd[0] = 1;                   if (a < amt[0] || d < amt[3] || e < amt[4]) usd[1] = 1;                   if (b < amt[1] || d < amt[3] || f < amt[5]) usd[2] = 1;                   if (c < amt[2] || e < amt[4] || f < amt[5]) usd[3] = 1;                   bool ok = 0;                   for (long long j = 0; j < 4; ++j) {                     if (dp[a][b][c][d][e][f][j]) ok = 1;                     if (i & (1 << j)) {                       if (!usd[j]) {                         ok = false;                         break;                       }                     }                   }                   if (!ok) continue;                   cans = max(cans, cur + val[amt[0] - a][amt[1] - b][amt[2] - c]                                             [amt[3] - d][amt[4] - e][amt[5] - f]);                 }               }             }           }         }       }       for (long long j = 0; j < 6; ++j) {         amt[j] = tpa[j].size();       }       ans = max(ans, cans);     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long double ans1[1000000];   long double ans2[1000000];   long double sa[1000000];   long double sb[1000000];   long double x1[1000000];   long double x[1000000];   long double sqr(long double x) { return x * x; }   long double sqrtt(long double x) {     if (x < 0) return 0;     return sqrt(x);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     for (int i = 1; i <= n; i++) cin >> x[i];     for (int i = 1; i <= n; i++) cin >> x1[i];     for (int i = 1; i <= 1; i++) {       long double D = sqrtt(-4 * x[i] + sqr(x1[i] + x[i]));       ans1[i] = (x1[i] + x[i] + D) / 2.;       ans2[i] = x[i] / ans1[i];       sa[i] = ans1[i];       sb[i] = ans2[i];     }     for (int i = 2; i <= n; i++) {       long double a = 1;       long double b = sa[i - 1] - x[i] - sb[i - 1] - x1[i];       long double c = x[i] - sa[i - 1] * x[i] - x1[i] * sa[i - 1];       long double D = sqrtt(b * b - a * c * 4);       ans1[i] = (-b + D) / 2.;       ans2[i] = (x[i] - ans1[i] * sb[i - 1]) / (ans1[i] + sa[i - 1]);       sa[i] = ans1[i] + sa[i - 1];       sb[i] = ans2[i] + sb[i - 1];     }     cout.precision(9);     for (int i = 1; i <= n; i++) cout << fixed << ans1[i] <<    ;     cout <<   n ;     for (int i = 1; i <= n; i++) cout << fixed << ans2[i] <<    ;     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 123, mod = 1e9 + 7;   int n, b, p, x;   int cnt[N];   vector<vector<int>> t, a, res;   void add(int& a, int b) {     a = (a + b);     if (a >= mod) {       a -= mod;     }   }   int mult(int a, int b) {     a = (a * 1ll * b) % mod;     return a;   }   vector<vector<int>> mat_mult(const vector<vector<int>>& a,                                const vector<vector<int>>& b) {     vector<vector<int>> c;     c.resize(x, vector<int>(x));     for (int i = 0; i < x; i++) {       for (int j = 0; j < x; j++) {         for (int k = 0; k < x; k++) {           add(c[i][k], mult(a[i][j], b[j][k]));         }       }     }     return c;   }   vector<vector<int>> binpow(int t) {     if (t == 0) return res;     vector<vector<int>> o = binpow(t / 2);     o = mat_mult(o, o);     if (t % 2 == 1) o = mat_mult(o, a);     return o;   }   int main() {     cin >> n >> b >> p >> x;     a.resize(x, vector<int>(x));     res.resize(x, vector<int>(x));     for (int i = 1, val; i <= n; i++) {       cin >> val;       cnt[val]++;     }     for (int i = 0; i < x; i++) {       res[i][i] = 1;     }     for (int i = 0; i < x; i++) {       for (int j = 0; j < 10; j++) {         a[i][(i * 10 + j) % x] += cnt[j];       }     }     res = binpow(b);     cout << res[0][p];   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   vector<int> v[N];   int n, kk;   int dis[N], ans[N];   void dfs1(int pos, int fa) {     for (auto &i : v[pos])       if (i != fa) dfs1(i, pos), dis[pos] = max(dis[pos], dis[i]);     ++dis[pos];   }   bool dfs2(int pos, int fa, const multiset<int, greater<int>> &mt) {     multiset<int, greater<int>> d;     int mx = 0;     for (auto &i : v[pos])       if (i != fa)         d.insert(dis[i]);       else         mx = 1;     bool fs = 1;     for (auto &i : mt) {       if (i == dis[pos] && fs) {         fs = 0;         continue;       }       mx = i + 1;       break;     }     if (mx) d.insert(mx);     while (d.size() > 3) d.erase(prev(d.end()));     if (d.size() > 2 && *next(d.begin()) + *next(next(d.begin())) >= kk - 1) {       cout <<  No  << endl;       return 0;     }     for (auto &i : v[pos])       if (i != fa && !dfs2(i, pos, d)) return 0;     return 1;   }   int dep[N], f[N];   void dfs(int pos, int fa) {     dep[pos] = dep[fa] + 1;     f[pos] = fa;     for (auto &i : v[pos])       if (i != fa) dfs(i, pos);   }   void dfs3(int pos, int fa, int cur, int dd) {     ans[pos] = cur;     for (auto &i : v[pos])       if (i != fa) dfs3(i, pos, (cur + dd + kk) % kk, dd);   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> kk;     int t1, t2;     for (int i = 1; i < n; i++) {       cin >> t1 >> t2;       v[t1].push_back(t2);       v[t2].push_back(t1);     }     dfs1(1, 0);     if (kk != 2 && !dfs2(1, 0, {})) return 0;     cout <<  Yes  << endl;     if (kk == 2) {       dfs3(1, 0, 0, 1);       for (int i = 1; i <= n; i++) cout << ans[i] + 1 <<    ;       return 0;     }     dfs(1, 0);     t1 = max_element(dep + 1, dep + n + 1) - dep;     dfs(t1, 0);     t2 = max_element(dep + 1, dep + n + 1) - dep;     vector<int> d{t2};     while (f[t2]) d.push_back(t2 = f[t2]);     int mid = (d.size() + 1) / 2 - 1;     dfs3(d[mid], d[mid + 1], mid % kk, -1);     dfs3(d[mid + 1], d[mid], (mid + 1) % kk, 1);     for (int i = 1; i <= n; i++) cout << ans[i] + 1 <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   string ans[N];   int len[N];   int dp[N];   string get_ans(const string &add) {     if (add.size() <= 10) {       auto res = add;       reverse(res.begin(), res.end());       return res;     }     string res;     int t = add.size();     for (int i = t - 1; i >= t - 5; i--) {       res += add[i];     }     res +=  ... ;     res += add[1];     res += add[0];     return res;   }   bool check(const string &a) {     int t = a.size();     if (a[t - 3] < a[t - 1])       return true;     else if (a[t - 3] > a[t - 1])       return false;     return dp[t - 1] < 0 || a[dp[t - 1]] < a[t - 1];   }   int main() {     string s;     cin >> s;     string a;     bool pre = 0;     memset(dp, -1, sizeof(dp));     for (int i = s.size() - 1; i >= 0; i--) {       a += s[i];       int t = a.size();       if (t > 1) {         if (a[t - 1] == a[t - 2]) {           dp[t - 1] = dp[t - 2];         } else {           dp[t - 1] = t - 2;         }       }       if (!pre) {         if (t >= 2) {           if (a[t - 1] == a[t - 2] && (i + 1 == s.size() || s[i] == s[i + 1]) &&               (t == 2 || check(a))) {             a.pop_back();             a.pop_back();             pre = 1;           }         }       } else {         pre = 0;       }       len[i] = a.size();       ans[i] = get_ans(a);     }     for (int i = 0; i < s.size(); i++) {       cout << len[i] <<     << ans[i] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int n, h[100001], root[100001], par[100001][25], size[100001],       dp_down_0[100001], dp_down_1[100001], dp_up_0[100001], dp_up_1[100001];   vector<pair<long long int, long long int> > v[100001];   long long int add(long long int x, long long int y) {     return (x + y) % 1000000007;   }   long long int mul(long long int x, long long int y) {     return (1LL * x * y) % 1000000007;   }   long long int sub(long long int x, long long int y) {     return (x - y + 1000000007) % 1000000007;   }   long long int sq(long long int x) { return (1LL * x * x) % 1000000007; }   void dfs_down(long long int cur, long long int p) {     if (p != -1) h[cur] = h[p] + 1;     par[cur][0] = p;     for (long long int i = 1; i < 20; i++) {       if (par[cur][i - 1] != -1) par[cur][i] = par[par[cur][i - 1]][i - 1];     }     size[cur] = 1;     for (long long int i = 0; i < v[cur].size(); i++) {       long long int x = v[cur][i].first;       if (x == p) continue;       root[x] = add(root[cur], v[cur][i].second);       dfs_down(x, cur);       size[cur] += size[x];       dp_down_0[cur] = add(dp_down_0[cur], dp_down_0[x]);       dp_down_0[cur] = add(dp_down_0[cur], mul(size[x], v[cur][i].second));       dp_down_1[cur] = add(dp_down_1[cur], mul(size[x], sq(v[cur][i].second)));       dp_down_1[cur] = add(dp_down_1[cur], dp_down_1[x]);       dp_down_1[cur] =           add(dp_down_1[cur], mul(2, mul(v[cur][i].second, dp_down_0[x])));     }   }   void dfs_up(long long int cur, long long int p, long long int wt) {     if (p != -1) {       dp_up_0[cur] = add(dp_up_0[cur], dp_up_0[p]);       dp_up_0[cur] = add(dp_up_0[cur], mul(n - size[p], wt));       long long int x = dp_down_0[p];       x = sub(x, dp_down_0[cur]);       x = sub(x, mul(size[cur], wt));       dp_up_0[cur] = add(dp_up_0[cur], x);       dp_up_0[cur] = add(dp_up_0[cur], mul(wt, size[p] - size[cur]));       dp_up_1[cur] = add(dp_up_1[cur], dp_up_1[p]);       dp_up_1[cur] = add(dp_up_1[cur], mul(n - size[p], sq(wt)));       dp_up_1[cur] = add(dp_up_1[cur], mul(2, mul(wt, dp_up_0[p])));       x = dp_down_1[p];       x = sub(x, dp_down_1[cur]);       x = sub(x, mul(2, mul(wt, dp_down_0[cur])));       x = sub(x, mul(size[cur], sq(wt)));       long long int y = dp_down_0[p];       y = sub(y, dp_down_0[cur]);       y = sub(y, mul(wt, size[cur]));       dp_up_1[cur] = add(dp_up_1[cur], x);       dp_up_1[cur] = add(dp_up_1[cur], mul(2, mul(wt, y)));       dp_up_1[cur] = add(dp_up_1[cur], mul(sq(wt), size[p] - size[cur]));     }     for (long long int i = 0; i < v[cur].size(); i++) {       long long int x = v[cur][i].first;       if (x == p) continue;       dfs_up(x, cur, v[cur][i].second);     }   }   int lca(int v, int u) {     if (h[v] < h[u]) swap(v, u);     for (int i = 20 - 1; i >= 0; i--) {       if (par[v][i] != -1 && h[par[v][i]] >= h[u]) {         v = par[v][i];       }     }     if (v == u) return u;     for (int i = 20 - 1; i >= 0; i--) {       if (par[v][i] - par[u][i]) v = par[v][i], u = par[u][i];     }     return par[v][0];   }   long long int distance(long long int x, long long int y) {     long long int ans = root[x];     ans = add(ans, root[y]);     ans = sub(ans, mul(2, root[lca(x, y)]));     return ans;   }   int main() {     ios::sync_with_stdio(false);     ;     cin.tie(0);     ;     long long int i, j, q, x, y, ans, temp, z, lc, dist;     memset(par, -1, sizeof par);     cin >> n;     for (i = 1; i <= n - 1; i++) {       cin >> x >> y >> z;       v[x].push_back(make_pair(y, z));       v[y].push_back(make_pair(x, z));     }     dfs_down(1, -1);     dfs_up(1, -1, 0);     cin >> q;     while (q--) {       cin >> x >> y;       dist = distance(x, y);       lc = lca(x, y);       if ((lc != x && lc != y) || lc == x) {         ans = dp_down_1[y];         ans = add(ans, mul(sq(dist), size[y]));         ans = add(ans, mul(2, mul(dist, dp_down_0[y])));         ans = mul(2, ans);         ans = sub(ans, dp_down_1[x]);         ans = sub(ans, dp_up_1[x]);         cout << ans <<   n ;       } else if (lc == y) {         ans = dp_down_1[x];         temp = dp_up_1[x];         temp = sub(temp, dp_up_1[y]);         temp = sub(temp, mul(2, mul(dist, dp_up_0[y])));         temp = sub(temp, mul(n - size[y], sq(dist)));         ans = add(ans, temp);         ans = mul(2, ans);         ans = sub(ans, dp_down_1[x]);         ans = sub(ans, dp_up_1[x]);         cout << ans <<   n ;       } else         assert(false);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int DIM = 55;   char mat[2][DIM][DIM];   vector<pair<int, int>> lst1, lst2;   void move(int k, int p, int q, vector<pair<int, int>> &lst) {     lst.push_back(make_pair(p, q));     if (mat[k][p][q] ==  U ) {       assert(mat[k][p + 0][q + 0] ==  U  and mat[k][p + 0][q + 1] ==  U  and              mat[k][p + 1][q + 0] ==  D  and mat[k][p + 1][q + 1] ==  D );       mat[k][p + 0][q + 0] =  L ;       mat[k][p + 0][q + 1] =  R ;       mat[k][p + 1][q + 0] =  L ;       mat[k][p + 1][q + 1] =  R ;     } else {       assert(mat[k][p + 0][q + 0] ==  L  and mat[k][p + 0][q + 1] ==  R  and              mat[k][p + 1][q + 0] ==  L  and mat[k][p + 1][q + 1] ==  R );       mat[k][p + 0][q + 0] =  U ;       mat[k][p + 0][q + 1] =  U ;       mat[k][p + 1][q + 0] =  D ;       mat[k][p + 1][q + 1] =  D ;     }   }   void solve(int k, int p, int q, bool whSide, vector<pair<int, int>> &lst) {     if (!whSide) {       if (mat[k][p][q] !=  L ) {         solve(k, p, q + 1, whSide ^ true, lst);         move(k, p, q, lst);       }     } else {       if (mat[k][p][q] !=  U ) {         solve(k, p + 1, q, whSide ^ true, lst);         move(k, p, q, lst);       }     }   }   int main(void) {     int n, m;     cin >> n >> m;     for (int k = 0; k <= 1; ++k)       for (int i = 1; i <= n; ++i) cin >> (mat[k][i] + 1);     for (int i = 1; i <= n; i += (m % 2 == 0 ? 1 : 2)) {       for (int j = 1; j <= m; j += (m % 2 == 0 ? 2 : 1)) {         solve(0, i, j, (m % 2 == 0 ? false : true), lst1);         solve(1, i, j, (m % 2 == 0 ? false : true), lst2);       }     }     cout << lst1.size() + lst2.size() <<   n ;     for (pair<int, int> pr : lst1) cout << pr.first <<     << pr.second <<   n ;     reverse(lst2.begin(), lst2.end());     for (pair<int, int> pr : lst2) cout << pr.first <<     << pr.second <<   n ;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) assert(mat[0][i][j] == mat[1][i][j]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void bug() { cout <<     << endl; }   template <class T>   void bug(T a) {     cout << a <<     ;   }   template <class T>   void bug(T* a, int n) {     for (int i = 0; i < (n); i++) bug(a[i]);   }   int n, m;   vector<pair<pair<int, int>, bool> > e;   int ans = 1000000000;   vector<int> vs;   int fa[111111], size[111111];   int find(int u) {     if (fa[u] == u)       return u;     else       return fa[u] = find(fa[u]);   }   void merge(int u, int v) {     u = find(u);     v = find(v);     if (u == v) return;     if (size[u] > size[v]) {       size[u] += size[v];       fa[v] = u;     } else {       size[v] += size[u];       fa[u] = v;     }   }   vector<int> g[111111];   int vis[111111];   void go(bool b) {     for (int i = 0; i < (n); i++) g[i].clear();     memset(vis, -1, sizeof(vis));     for (int i = 0; i < (n); i++) {       fa[i] = i;       size[i] = 1;     }     for (int i = 0; i < (m); i++) {       int u = e[i].first.first;       int v = e[i].first.second;       bool bb = e[i].second;       if (bb == b) {         merge(u, v);       }     }     for (int i = 0; i < (m); i++) {       int u = e[i].first.first;       int v = e[i].first.second;       bool bb = e[i].second;       if (bb != b) {         u = find(u);         v = find(v);         g[u].push_back(v);         g[v].push_back(u);       }     }     int ret = 0;     vector<int> vv;     for (int i = 0; i < (n); i++)       if (find(i) == i && vis[i] == -1) {         queue<int> q;         q.push(i);         vis[i] = 0;         int zero = size[i], one = 0;         vector<int> zs, os;         zs.push_back(i);         while (q.size() > 0) {           int u = q.front();           q.pop();           for (int v : g[u]) {             if (vis[v] != -1) {               if (vis[v] != (1 - vis[u])) return;             } else {               vis[v] = (1 - vis[u]);               if (vis[v] == 0) {                 zero += size[v];                 zs.push_back(v);               } else {                 one += size[v];                 os.push_back(v);               }               q.push(v);             }           }         }         if (zero < one) {           ret += zero;           for (int i = 0; i < (zs.size()); i++) vv.push_back(zs[i]);         } else {           ret += one;           for (int i = 0; i < (os.size()); i++) vv.push_back(os[i]);         }       }     if (ans > ret) {       ans = ret;       set<int> s;       for (int i = 0; i < (vv.size()); i++) s.insert(vv[i]);       vs.clear();       for (int i = 0; i < (n); i++)         if (s.find(find(i)) != s.end()) vs.push_back(i);     }   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m;     for (int i = 0; i < (m); i++) {       int u, v;       char c;       cin >> u >> v >> c;       u--;       v--;       bool b = (c ==  B );       e.push_back(make_pair(make_pair(u, v), b));     }     go(0);     go(1);     if (ans == 1000000000)       cout << -1 << endl;     else {       cout << ans << endl;       for (int i = 0; i < (vs.size()); i++) {         cout << vs[i] + 1 <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool Pfunc(string);   int main() {     string st;     cin >> st;     for (int i = st.size(); i > 0; i--) {       bool b = false;       for (int k = 0; k <= st.size() - i; k++) {         if (Pfunc(st.substr(k, i)) == false) {           cout << i;           b = true;           return 0;         } else {         }       }       if (b) {         break;       }     }     cout << 0;     return 0;   }   bool Pfunc(string x) {     for (int i = 0; i < x.size() / 2; i++) {       if (x[i] != x[x.size() - i - 1]) {         return false;       }     }     return true;   }


#include <bits/stdc++.h>   using namespace std;   int c, bi, ba;   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       if (i % 3 == 0)         c += x;       else if (i % 3 == 1)         bi += x;       else         ba += x;     }     if (c > bi && c > ba)       cout <<   chest ;     else if (bi > c && bi > ba)       cout <<  biceps ;     else       cout <<  back ;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, ans = 0, a, b;   vector<long long> v, sum, sumk;   vector<pair<long long, long long> > maxsumk, maxsum2k;   int main() {     cin >> n >> k;     v.resize(n);     for (int i = 0; i < n; i++) cin >> v[i];     sum.resize(n);     sumk.resize(n);     sum[0] = v[0];     for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + v[i];     sumk[k - 1] = sum[k - 1];     for (int i = k; i < n; i++) sumk[i] = sum[i] - sum[i - k];     maxsumk.resize(n, {0, 0});     maxsumk[k - 1].first = sumk[k - 1];     maxsumk[k - 1].second = k - 1;     for (int i = k; i < n; i++) {       maxsumk[i].first = max(maxsumk[i - 1].first, sumk[i]);       if (max(maxsumk[i - 1].first, sumk[i]) == maxsumk[i - 1].first)         maxsumk[i].second = maxsumk[i - 1].second;       else         maxsumk[i].second = i;     }     maxsum2k.resize(n, {0, 0});     maxsum2k[2 * k - 1].first = sumk[2 * k - 1] + maxsumk[2 * k - 1 - k].first;     maxsum2k[2 * k - 1].second = 2 * k - 1;     for (int i = 2 * k; i < n; i++) {       maxsum2k[i].first =           max(sumk[i] + maxsumk[i - k].first, maxsum2k[i - 1].first);       if (max(sumk[i] + maxsumk[i - k].first, maxsum2k[i - 1].first) ==           maxsum2k[i - 1].first)         maxsum2k[i].second = maxsum2k[i - 1].second;       else         maxsum2k[i].second = i;     }     int v = maxsum2k[n - 1].second;     b = v - k + 2;     a = maxsumk[v - k].second - k + 2;     cout << a <<     << b;   }


#include <bits/stdc++.h>   using namespace std;   long long x, off = 10;   vector<long long> vec[15];   inline long long mul(long long a, long long b) {     bool neg = false;     long long res = 0;     if (b < 0) {       neg = true;       b = -b;     }     while (b) {       if (b & 1) res = (res + a) % off;       a = (a + a) % off;       b >>= 1;     }     return neg ? (off - res) % off : res;   }   inline pair<long long, long long> fib(long long n) {     if (!n) return make_pair(0, 1);     if (n & 1) {       pair<long long, long long> res = fib(n - 1);       return make_pair(res.second, (res.first + res.second) % off);     }     pair<long long, long long> res = fib(n >> 1);     return make_pair(         mul(res.first, 2 * res.second - res.first),         (mul(res.first, res.first) + mul(res.second, res.second)) % off);   }   int main() {     scanf( %lld , &x);     for (int i = 0; i < 60; i++) {       if (fib(i).first == x % off) vec[0].push_back(i);     }     long long step = 60;     for (int i = 0; i < 12; i++) {       off *= 10;       for (long long y : vec[i]) {         for (int j = 0; j < 10; j++) {           if (fib(y + j * step).first == x % off)             vec[i + 1].push_back(y + j * step);         }       }       step *= 10;     }     if (!vec[12].size())       printf( -1 n );     else       printf( %lld n , *min_element(vec[12].begin(), vec[12].end()));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s, t;     while (cin >> t) s += t;     vector<string> str;     int pos = 0;     vector<int> a, ans;     while (pos != ((int)(s).size())) {       if (s.substr(pos, 7) ==  <table> ) {         a.push_back(0);         pos += 7;       } else if (s.substr(pos, 4) ==  <tr> )         pos += 4;       else if (s.substr(pos, 4) ==  <td> ) {         ++a.back();         pos += 4;       } else if (s.substr(pos, 5) ==  </td> )         pos += 5;       else if (s.substr(pos, 5) ==  </tr> )         pos += 5;       else if (s.substr(pos, 8) ==  </table> ) {         ans.push_back(a.back());         a.pop_back();         pos += 8;       }     }     sort((ans).begin(), (ans).end());     for (int(i) = 0; (i) < (((int)(ans).size())); ++(i)) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, t;     cin >> n >> t;     char pos[50];     cin >> pos;     while (t--) {       for (int i = 0; i < n; i++) {         if (pos[i] ==  B ) {           if ((i + 1) < n && pos[i + 1] ==  G ) {             char temp = pos[i];             pos[i] = pos[i + 1];             pos[i + 1] = temp;             i++;           }         }       }     }     for (int i = 0; i < n; i++) {       cout << pos[i];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int mod = 1e9 + 7;   long long int inf = 9 * 1e18;   const long long maxn = 2e5 + 100;   long long pow(long long a, long long b) {     if (b < 0) return 0LL;     long long ans = 1;     while (b) {       if (b & 1) ans = ans * a;       a = a * a;       b >>= 1;     }     return ans;   }   long long binpow(long long a, long long b, long long m) {     if (b < 0) return 0LL;     if (a <= 0) return 0LL;     a %= m;     long long ans = 1LL;     while (b) {       if (b & 1) ans = ans * a % m;       a = a * a % m;       b >>= 1;     }     return ans;   }   long long modinv(long long n) { return binpow(n, mod - 2, mod); }   int gcdExtended(int a, int b, int *x, int *y) {     if (a == 0) {       *x = 0, *y = 1;       return b;     }     int x1, y1;     int gcd = gcdExtended(b % a, a, &x1, &y1);     *x = y1 - (b / a) * x1;     *y = x1;     return gcd;   }   void modInverse(int a, int m) {     int x, y;     int g = gcdExtended(a, m, &x, &y);     if (g != 1)       cout <<  Inverse doesn t exist ;     else {       int res = (x % m + m) % m;       cout <<  Modular multiplicative inverse is   << res;     }   }   bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {     return (a.second < b.second);   }   const long long N = 1e7 + 5;   long long sp[N];   void sieve(long long N) {     for (long long i = 1; i < N; i++) sp[i] = i;     for (long long i = 2; i < N; i++) {       if (sp[i] == i) {         for (long long j = i * i; j < N; j += i) {           if (sp[j] == j) sp[j] = i;         }       }     }   }   void solve() {     string s1;     cin >> s1;     long long k;     cin >> k;     long long n = s1.size();     map<char, char> ma;     for (int i = 0; i < k; i++) {       char u, x;       cin >> u >> x;       ma[u] = x;       ma[x] = u;     }     long long c1 = 0, c2 = 0;     long long ans = 0;     long long j = 0;     for (int i = 0; i < n; i++) {       c1 = 1;       c2 = 0;       char a = s1[i];       char b = ma[a];       for (j = i + 1; j < n; j++) {         if (s1[j] == a)           c1++;         else if (s1[j] == b)           c2++;         else           break;       }       ans += min(c1, c2);       i = j - 1;     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int t;     t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   int start, n, m;   bool edge[25][25];   long long memory[25][(1 << 19) + 5];   long long dfs(int u, int mask) {     if (memory[u][mask] != -1) return memory[u][mask];     int visit = 0;     for (int i = 0; i < n; i++) {       if (mask & (1 << i)) visit++;     }     long long result = 0;     if (visit >= 3 && edge[u][start]) result = 1;     for (int v = 0; v <= start; v++) {       if (!edge[u][v]) {         continue;       }       if (mask & (1 << v)) {         continue;       }       result += dfs(v, mask | (1 << v));     }     memory[u][mask] = result;     return result;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       int u, v;       scanf( %d%d , &u, &v);       u--;       v--;       edge[u][v] = edge[v][u] = true;     }     long long answer = 0;     for (start = 0; start < n; start++) {       for (int i = 0; i < n; i++) {         for (int j = 0; j < 1 << n; j++) {           memory[i][j] = -1;         }       }       answer += dfs(start, 1 << start);     }     printf( %lld n , answer / 2);   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = (double)1e-9;   const double PI = (double)acos(-1.0);   int irand(int lo, int hi) {     return (((double)rand()) / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo;   }   string toString(long long x) {     stringstream ss;     ss << x;     return ss.str();   }   long long toNumber(string S) {     long long ret;     sscanf(S.c_str(),  %lld , &ret);     return ret;   }   const int INF = (int)2e9;   const long long MOD = (long long)1e9 + 7;   int main() {     long long n, res = 0, two = 1;     scanf( %lld , &n);     while (n > 1) {       res += two * (n / 2);       n = (n / 2) + (n % 2);       two <<= 1;     }     printf( %lld n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<char> v;   string s;   int main() {     cin >> s;     for (int i = 0; i < s.size(); i++) {       if (!v.empty() && v.back() == s[i])         v.pop_back();       else         v.push_back(s[i]);     }     cout << (v.empty() ?  Yes  :  No );   }


#include <bits/stdc++.h>   using namespace std;   long long DP[20][4];   void init() {     DP[0][3] = DP[0][2] = DP[0][1] = DP[0][0] = 1;     for (int i = 1; i <= 18; i++) {       DP[i][0] = 1;       for (int j = 1; j <= 3; j++) {         DP[i][j] = 9 * DP[i - 1][j - 1] + DP[i - 1][j];       }     }   }   long long sol(int i, int j, string str) {     if (i == 0) {       if (j >= 0)         return 1;       else         return 0;     }     int d = str[i - 1] -  0 ;     if (d == 0)       return sol(i - 1, j, str);     else if (j == 0)       return 1;     return DP[i - 1][j] + (d - 1) * DP[i - 1][j - 1] + sol(i - 1, j - 1, str);   }   string tostr(long long x) {     string rtn;     while (x) {       rtn += (x % 10 +  0 );       x /= 10;     }     return rtn;   }   string A, B;   long long a, b;   int cas;   int main() {     ios::sync_with_stdio(false);     init();     cin >> cas;     while (cas--) {       cin >> a >> b;       a--;       A = tostr(a);       B = tostr(b);       cout << sol(B.length(), 3, B) - sol(A.length(), 3, A) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, K, ma;   int A[1000005];   int dp[10000005];   bool ok(int val) {     int nr = 0;     for (int i = 1; i <= ma; i++) {       dp[i] = 0;       if (val <= i) dp[i] = 1;       if (i % 2)         dp[i] = max(dp[i], dp[i >> 1] + dp[(i + 1) >> 1]);       else         dp[i] = max(dp[i], 2 * dp[i >> 1]);     }     for (int i = 1; i <= N; i++) {       nr += dp[A[i]];       if (nr >= K) return 1;     }     return 0;   }   int main() {     bool notthesamecode = 1;     cin.sync_with_stdio(false);     cout.sync_with_stdio(false);     cin >> N >> K;     for (int i = 1; i <= N; i++) {       cin >> A[i];       ma = max(ma, A[i]);     }     int st = 0, dr = (1e7 + 10), mid;     while (st < dr) {       mid = (st + dr + 1) / 2;       if (ok(mid))         st = mid;       else         dr = mid - 1;     }     if (!st)       cout << -1;     else       cout << st;     return 0;   }


#include <bits/stdc++.h>   namespace io {   int F() {     int F = 1, n = 0;     char ch;     while ((ch = getchar()) !=  -  && (ch <  0  || ch >  9 ))       ;     ch ==  -  ? F = 0 : n = ch -  0 ;     while ((ch = getchar()) >=  0  && ch <=  9 ) n = n * 10 + ch -  0 ;     return F ? n : -n;   }   long long G() {     long long F = 1, n = 0;     char ch;     while ((ch = getchar()) !=  -  && (ch <  0  || ch >  9 ))       ;     ch ==  -  ? F = 0 : n = ch -  0 ;     while ((ch = getchar()) >=  0  && ch <=  9 ) n = n * 10 + ch -  0 ;     return F ? n : -n;   }   }  // namespace io   int R(int l, int r) { return (rand() << 15 | rand()) % (r - l + 1) + l; }   int main() {     long long n = io::G(), l = io::G() - 1, r = io::G() - 1, k = io::G();     long long B = l <= r ? r - l + 1 : n - (l - r - 1), S = n - B;     long long ans = -1;     int fl = 1;   start:;     if ((k - B) / n <= 22000000) {       int u = (k - B) / n;       for (register int x = 0; x <= u; ++x) {         long long re = k - B - x * n;         if (x == 0) {           if (re <= B && re >= 0)             if (fl || re) (ans < (S + re) ? ans = (S + re), 1 : 0);         } else {           long long B1 = re % x, S1 = re / x - B1;           if (B1 > B || S1 < 0) continue;           if (S1 <= S) {             if (fl || B1) (ans < (B1 + S1) ? ans = (B1 + S1), 1 : 0);           } else {             long long T = S1 - S;             long long ex = (T + x) / (x + 1);             B1 += ex * x;             S1 -= ex * (x + 1);             if (S1 >= 0 && B1 <= B)               if (fl || B1) (ans < (B1 + S1) ? ans = (B1 + S1), 1 : 0);           }         }       }     } else {       for (register long long S1 = 0; S1 <= S; ++S1)         for (register long long B1 = 0; B1 <= B; ++B1) {           long long y = k - B1 - B;           long long a = S1 + B1 + n;           if (y == 0 && a == 0 || a && y % a == 0 && y / a >= 0)             if (fl || B1) (ans < (S1 + B1) ? ans = (S1 + B1), 1 : 0);         }     }     if (fl) {       k = k + 1;       fl = 0;       goto start;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, flag = 0;     cin >> n >> m;     char c;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> c;         if (c ==  B  || c ==  W  || c ==  G )           continue;         else           flag = 1;       }     }     if (flag == 0)       cout <<  #Black&White  << endl;     else       cout <<  #Color  << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int a, b;     double gia[n];     for (int i = 0; i < n; i++) {       cin >> a >> b;       gia[i] = (double)a / b;     }     double kq = gia[0];     for (int i = 1; i < n; i++) {       if (kq > gia[i]) kq = gia[i];     }     cout << setprecision(8) << kq * m;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 2147483647;   const int maxn = 21;   const int dx[4] = {0, 0, 1, -1};   const int dy[4] = {1, -1, 0, 0};   int Map[maxn][maxn], Value[maxn], X[maxn], Y[maxn], XX[maxn], YY[maxn];   int vis[maxn][maxn][260][129], VV[maxn][maxn][260][129],       inq[maxn][maxn][260][129];   int ans, num, N, M, D;   const int Maxn = 3020120;   char s[30];   struct Point {     int x, y, z, value, num;   } Q[Maxn];   void BFS(int sx, int sy) {     int x, y, z, V, u, v, w, NOW, value, NNN, head, tail;     Q[head = tail = 1] = (Point){sx, sy, 0, 0, 0};     memset(vis, 0, sizeof(vis));     vis[sx][sy][0][0] = 1;     while (head <= tail) {       x = Q[head].x;       y = Q[head].y;       z = Q[head].z;       V = Q[head].value;       NOW = Q[head].num;       inq[x][y][z][V] = 0;       ++head;       V--;       for (int i = 0; i < 4; i++) {         u = x + dx[i], v = y + dy[i], w = z, value = V, NNN = NOW;         if (!u || !v || u > N || v > M) continue;         if (!Map[u][v]) continue;         for (int k = 1; k <= D; k++)           if (u == X[k] && v > Y[k] && x == X[k] + 1 ||               u == X[k] + 1 && v > Y[k] && x == X[k]) {             w = w ^ (1 << k - 1);             if (w & (1 << k - 1))               value += Value[k];             else               value -= Value[k];           }         for (int k = 1; k <= num; k++) {           if (u == XX[k] && v > YY[k] && x == XX[k] + 1) NNN = NNN ^ (1 << k - 1);           if (u == XX[k] + 1 && v > YY[k] && x == XX[k]) NNN = NNN ^ (1 << k - 1);         }         if (NNN == 1) NNN = 1;         if (value > VV[u][v][w][NNN])           VV[u][v][w][NNN] = value;         else           continue;         if (!vis[u][v][w][NNN])           Q[++tail] = (Point){u, v, w, value, NNN};         else           continue;         if (u == sx && v == sy && value > ans && NNN == 0) ans = max(ans, value);         inq[u][v][w][NNN] = 1;       }     }   }   int main() {     cin >> N >> M;     memset(VV, 235, sizeof(VV));     for (int i = 1; i <= N; i++) {       scanf( %s , s + 1);       for (int j = 1; j <= M; j++) {         if (s[j] >  0  && s[j] !=  #  && s[j] !=  .  && s[j] !=  S  &&             s[j] !=  B )           X[s[j] - 48] = i, Y[s[j] - 48] = j, D = max(D, s[j] - 48);         if (s[j] ==  . ) Map[i][j] = 1;         if (s[j] ==  S ) Map[i][j] = 2;         if (s[j] ==  B ) {           XX[++num] = i;           YY[num] = j;         }       }     }     for (int i = 1; i <= D; i++) scanf( %d , &Value[i]);     for (int i = 1; i <= N; i++)       for (int j = 1; j <= M; j++)         if (Map[i][j] == 2) BFS(i, j);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   int n, m, dp[250][1 << 15][2][2];   char g[250][251];   int calc(int r, int c, int left, int up, int emp) {     if (r == n) {       for (int i = 0; i < 15; i++)         if (c + 1 < m && (left & (1 << i)) && g[i][c + 1] ==  x )           left &= ~(1 << i);       return calc(0, c + 1, left, 0, emp);     }     if (c == m) return 1;     int &ret = dp[r * m + c][left][up][emp];     if (ret != -1) return ret;     ret = 0;     if (g[r][c] ==  x ) {       ret = calc(r + 1, c, left, 0, emp);     } else {       if (up || (left & (1 << r)))         ret = (ret + calc(r + 1, c, left, up, emp)) % MOD;       else if (!emp)         ret = (ret + calc(r + 1, c, left, up, 1)) % MOD;       ret = (ret + calc(r + 1, c, left | (1 << r), 1, emp)) % MOD;     }     return ret;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) scanf( %s , g[i]);     if (n > m) {       char tmp[250][251];       for (int i = 0; i < n; i++)         for (int j = 0; j < m; j++) tmp[j][i] = g[i][j];       swap(n, m);       for (int i = 0; i < n; i++)         for (int j = 0; j < m; j++) g[i][j] = tmp[i][j];     }     memset(dp, -1, sizeof(dp));     printf( %d n , calc(0, 0, 0, 0, 0));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   const int inf = 1e9;   int n, m;   int mp[3][N];   char ch[N];   struct node {     int d1, d2, d3, d4;   };   struct tree {     int l, r;     node d;   } t[N * 5];   node merge(node a, node b) {     node tmp;     tmp.d1 = min(inf, min(a.d1 + b.d1, a.d2 + b.d3) + 1);     tmp.d2 = min(inf, min(a.d1 + b.d2, a.d2 + b.d4) + 1);     tmp.d3 = min(inf, min(a.d3 + b.d1, a.d4 + b.d3) + 1);     tmp.d4 = min(inf, min(a.d3 + b.d2, a.d4 + b.d4) + 1);     return tmp;   }   inline void build(int x, int l, int r) {     t[x].l = l, t[x].r = r;     if (l == r) {       t[x].d.d1 = t[x].d.d2 = t[x].d.d3 = t[x].d.d4 = inf;       if (mp[1][l]) t[x].d.d1 = 0;       if (mp[2][l]) t[x].d.d4 = 0;       if (mp[1][l] && mp[2][l]) t[x].d.d2 = t[x].d.d3 = 1;       return;     }     int mid = (l + r) >> 1;     build(x << 1, l, mid);     build(x << 1 | 1, mid + 1, r);     t[x].d = merge(t[x << 1].d, t[x << 1 | 1].d);   }   inline node query(int x, int l, int r) {     if (l == t[x].l && r == t[x].r) return t[x].d;     int mid = (t[x].l + t[x].r) >> 1;     if (r <= mid)       return query(x << 1, l, r);     else if (l > mid)       return query(x << 1 | 1, l, r);     else       return merge(query(x << 1, l, mid), query(x << 1 | 1, mid + 1, r));   }   inline int ask(int x, int y) {     int a = (x - 1) % n + 1, b = (y - 1) % n + 1;     if (a > b) {       swap(x, y);       swap(a, b);     }     node tmp = query(1, a, b);     if (x <= n && y <= n) return tmp.d1;     if (x <= n && y > n) return tmp.d2;     if (x > n && y <= n) return tmp.d3;     if (x > n && y > n) return tmp.d4;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= 2; i++) {       scanf( %s , ch);       for (int j = 1; j <= n; j++)         if (ch[j - 1] ==  . ) mp[i][j] = 1;     }     build(1, 1, n);     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       int ans = ask(x, y);       if (ans < inf)         printf( %d n , ans);       else         printf( -1 n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 0;     cin >> t;     for (int i = 0; i < t; i++) {       long long m = 0;       int n = 0;       cin >> n;       for (int j = n; j > 2; j -= 2) {         m += (2 * pow((j - 1), 2));       }       cout << m << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, q, a[100010], l[100010], r[100010];   int st[100010], top;   int main() {     scanf( %d%d , &n, &q);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     n--;     for (int i = 1; i <= n; i++) a[i] = abs(a[i + 1] - a[i]);     for (int i = 1; i <= n; i++) {       while (top && a[st[top]] < a[i]) top--;       l[i] = st[top];       st[++top] = i;     }     top = 0;     st[top] = n + 1;     for (int i = n; i >= 1; i--) {       while (top && a[st[top]] <= a[i]) top--;       r[i] = st[top];       st[++top] = i;     }     for (int i = 1; i <= q; i++) {       int L, R;       long long ans = 0;       scanf( %d%d , &L, &R);       if (L >= R) {         puts( 0 );         continue;       }       R--;       for (int j = L; j <= R; j++) {         ans += (long long)(j - max(l[j], L - 1)) * (min(r[j], R + 1) - j) * a[j];       }       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long pw(long long a, long long b) {     long long t = 1;     if (b == 0) return t;     if (b % 2) {       t = t * a;     }     t %= 1000000009;     long long x = pw(a, b / 2);     x = x * x;     x %= 1000000009;     t *= x;     t %= 1000000009;     return t;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long ti = 1;     for (long long io = 0; io < ti; io++) {       long long n, m, k;       cin >> n >> m >> k;       long long oc = 0, rm;       oc = n / k;       rm = n % k;       long long ans = 0;       if (m > (n - oc)) {         long long x = m - n + oc;         long long y;         y = k;         y = y * ((pw(2, x + 1) - 2) % 1000000009);         y %= 1000000009;         ans += y;         y = k - 1;         y *= (oc - x);         y %= 1000000009;         ans += y;         ans += rm;       } else {         ans = m;       }       ans %= 1000000009;       ans += 1000000009;       ans %= 1000000009;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Solution() {     int n;     cin >> n;     string s;     cin >> s;     int cnt = 0;     for (int i = 0; i < n; ++i) cnt += (s[i] ==  H );     s += s;     int ans = 0x7fffffff;     for (int i = 0; i < n; ++i) {       int dif = 0;       for (int j = 0; j < cnt; ++j)         if (s[i + j] !=  H ) ++dif;       if (dif < ans) ans = dif;     }     cout << ans << endl;     return 0;   }   int main() {     Solution();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int x, int y) {     if (y == 0) return x;     return gcd(y, x % y);   }   int lcm(int x, int y) { return x * y / gcd(x, y); }   string in;   int check(string s) {     int ret = 0;     while (true) {       bool beforem = s[0] ==  M ;       bool changed = false;       for (int i = 1; i < s.size(); ++i) {         if (s[i] ==  F ) {           if (beforem) {             s[i - 1] =  F ;             s[i] =  M ;             changed = true;           }           beforem = false;         } else {           beforem = true;         }       }       if (!changed) break;       ++ret;     }     return ret;   }   int main() {     cin >> in;     vector<int> times;     int fcnt = 0;     for (int i = 0; i < in.size(); ++i) {       if (in[i] ==  F ) {         if (times.size() == 0) {           if (i == fcnt) {             times.push_back(0);           } else {             times.push_back(i);           }         } else {           if (i == fcnt) {             times.push_back(0);           } else {             int move = i - fcnt;             if (times[times.size() - 1] + 1 <= move) {               times.push_back(move);             } else {               times.push_back(times[times.size() - 1] + 1);             }           }         }         ++fcnt;       }     }     int ret = 0;     for (int i = 0; i < times.size(); ++i) ret = max(ret, times[i]);     printf( %d , ret);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18;   template <typename T1, typename T2>   istream& operator>>(istream& in, pair<T1, T2>& a) {     in >> a.first >> a.second;     return in;   }   template <typename T1, typename T2>   ostream& operator<<(ostream& out, pair<T1, T2> a) {     out << a.first <<     << a.second;     return out;   }   template <typename T, typename T1>   T amax(T& a, T1 b) {     if (b > a) a = b;     return a;   }   template <typename T, typename T1>   T amin(T& a, T1 b) {     if (b < a) a = b;     return a;   }   template <typename Arg1>   void __f(const char* name, Arg1&& arg1) {     cerr << name <<   :   << arg1 <<   n ;   }   template <typename Arg1, typename... Args>   void __f(const char* names, Arg1&& arg1, Args&&... args) {     const char* comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<   :   << arg1 <<   |  ;     __f(comma + 1, args...);   }   void solve() {     long long n, k;     cin >> n >> k;     long long a[n], cnt = 0, l = 0, r = -1, l3, r3;     for (long long i = 0; i < n; ++i) {       cin >> a[i];       if (a[i] == 0) cnt++;       if (cnt <= k) r++;     }     long long ans = r - l + 1;     l3 = l;     r3 = r;     for (long long i = r + 1; i < n; ++i) {       if (r == n - 1) break;       if (a[i] == 0) {         if (ans < r - l + 1) {           ans = r - l + 1;           l3 = l;           r3 = r;         }         long long l1 = l;         for (long long j = l1; j < n; ++j) {           l++;           if (a[j] == 0) break;         }         r++;       } else         r++;     }     if (ans < r - l + 1) {       ans = r - l + 1;       l3 = l;       r3 = r;     }     cout << ans <<   n ;     for (long long i = 0; i < n; ++i) {       if (i >= l3 && i <= r3)         cout << 1 <<    ;       else         cout << a[i] <<    ;     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int xd[] = {0, 1, 0, -1};   int yd[] = {1, 0, -1, 0};   int xx[] = {0, 1, 0, -1, 1, -1, 1, -1};   int yy[] = {1, 0, -1, 0, 1, 1, -1, -1};   int kx[] = {-2, -1, -2, -1, 1, 2, 2, 1};   int ky[] = {1, 2, -1, -2, -2, -1, 1, 2};   bool islucky(int x) {     int four = 0, seven = 0;     while (x) {       if (x % 10 != 7 && x % 10 != 4) return false;       if (x % 10 == 7)         seven++;       else         four++;       x = x / 10;     }     return four == seven;   }   bool is_prime(long long x) {     if (x == 1) return false;     for (int i = 2; i * i <= x; i++)       if (x % i == 0) return false;     return true;   }   long long C(int k, int l) {     if (l == k) return 1;     if (l * 2 > k) l = k - l;     long long res = 1, temp = l;     for (int i = k - l + 1; i <= k; i++) {       res *= i / temp;       temp--;     }     return res;   }   long long sumOfDigit(int x) {     int res = 0;     while (x) {       res += x % 10;       x /= 10;     }     return res;   }   uint64_t bigPow(uint64_t base, uint64_t p) {     uint64_t temp = 1;     while (p--) {       temp *= base;     }     return temp;   }   vector<int> soso;   bool prime[10000001] = {0};   void SieveOfEratosthenes(int n) {     soso.push_back(2);     for (int p = 3; p * p <= n; p += 2) {       if (prime[p] == false) {         for (int i = p * 2; i <= n; i += p) prime[i] = true;       }     }     for (int i = 3; i <= n; i += 2)       if (!prime[i]) soso.push_back(i);   }   vector<vector<int> > AdjList;   bool visited[100005];   void dfs(int s) {     if (visited[s]) return;     visited[s] = 1;     for (int i = 0; i < AdjList[s].size(); i++) dfs(AdjList[s][i]);     return;   }   long long gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   unsigned long long fact(long long a) {     unsigned long long ans = 1;     for (int i = 1; i <= a; i++) ans *= i;     return ans;   }   bool lucky(int x) {     while (x) {       if (x % 10 == 7) return true;       x /= 10;     }     return false;   }   int main() {     int t;     cin >> t;     while (t--) {       vector<pair<pair<int, int>, int> > q;       int n;       cin >> n;       for (int i = 0; i < n; i++) {         int l, r;         cin >> l >> r;         q.push_back(make_pair(make_pair(l, i), r));       }       sort(q.begin(), q.end());       int w = 0, last = 0;       vector<pair<int, int> > turn;       for (int i = 0; i < q.size(); i++) {         if (q[i].second < max(q[i].first.first, last + 1)) {           turn.push_back(make_pair(q[i].first.second, 0));           w++;         } else {           turn.push_back(               make_pair(q[i].first.second, max(q[i].first.first, last + 1)));           last = max(q[i].first.first, last + 1);         }       }       sort(turn.begin(), turn.end());       for (int i = 0; i < turn.size(); i++) cout << turn[i].second <<    ;       cout << endl;     }   }


#include <bits/stdc++.h>   using int_t = long long int;   template <typename T, typename FirstOp = std::plus<T>,             typename SecondOp = std::multiplies<T>,             typename UpdateOp = std::plus<T>>   class SegmentTree {    public:     explicit SegmentTree(std::size_t size, FirstOp first_operation = FirstOp(),                          SecondOp second_operation = SecondOp(),                          UpdateOp update_operation = UpdateOp(), T identity = T())         : m_size(size),           m_tree(std::max(size * 4, std::size_t(4)), identity),           m_lazy_tag(std::max(size * 4, std::size_t(4)), identity),           m_first_operation(first_operation),           m_second_operation(second_operation),           m_update_operation(update_operation),           m_identity(identity) {}     void update(std::size_t pos, const T& value) {       update(pos + 1, value, 1, 1, m_size);     }     void update(std::size_t start, std::size_t end, const T& value) {       update(start + 1, end, value, 1, 1, m_size);     }     T query(std::size_t start, std::size_t end) {       return query(start + 1, end, 1, 1, m_size);     }       private:     std::size_t m_size;     std::vector<T> m_tree;     std::vector<T> m_lazy_tag;     FirstOp m_first_operation;     SecondOp m_second_operation;     UpdateOp m_update_operation;     const T m_identity;     void push_up(std::size_t root) {       m_tree[root] = m_first_operation(m_tree[left(root)], m_tree[right(root)]);     }     void push_down(std::size_t root, std::size_t length) {       m_tree[left(root)] = m_update_operation(           m_tree[left(root)],           m_second_operation(m_lazy_tag[root], length - length / 2));       m_lazy_tag[left(root)] =           m_update_operation(m_lazy_tag[left(root)], m_lazy_tag[root]);       m_tree[right(root)] = m_update_operation(           m_tree[right(root)], m_second_operation(m_lazy_tag[root], length / 2));       m_lazy_tag[right(root)] =           m_update_operation(m_lazy_tag[right(root)], m_lazy_tag[root]);       m_lazy_tag[root] = m_identity;     }     void update(std::size_t pos, const T& value, std::size_t root, std::size_t l,                 std::size_t r) {       if (l == r) {         m_tree[root] = m_update_operation(m_tree[root], value);         return;       }       std::size_t m = (l + r) / 2;       if (pos <= m) {         update(pos, value, left(root), l, m);       } else {         update(pos, value, right(root), m + 1, r);       }       push_up(root);     }     void update(std::size_t start, std::size_t end, const T& value,                 std::size_t root, std::size_t l, std::size_t r) {       if (start <= l && r <= end) {         m_tree[root] = m_update_operation(m_tree[root],                                           m_second_operation(value, r - l + 1));         m_lazy_tag[root] = m_update_operation(m_lazy_tag[root], value);         return;       }       if (m_lazy_tag[root] != m_identity) {         push_down(root, r - l + 1);       }       std::size_t m = (l + r) / 2;       if (start <= m) {         update(start, end, value, left(root), l, m);       }       if (m < end) {         update(start, end, value, right(root), m + 1, r);       }       push_up(root);     }     T query(std::size_t start, std::size_t end, std::size_t root, std::size_t l,             std::size_t r) {       if (start <= l && r <= end) {         return m_tree[root];       }       if (m_lazy_tag[root] != m_identity) {         push_down(root, r - l + 1);       }       std::size_t m = (l + r) / 2;       if (start <= m && m < end) {         return m_first_operation(query(start, end, left(root), l, m),                                  query(start, end, right(root), m + 1, r));       } else if (end <= m) {         return query(start, end, left(root), l, m);       } else {         return query(start, end, right(root), m + 1, r);       }     }     static std::size_t left(std::size_t root) { return root * 2; }     static std::size_t right(std::size_t root) { return root * 2 + 1; }   };   int main() {     std::ios::sync_with_stdio(false);     std::cin.tie(nullptr);     int_t m;     std::cin >> m;     std::vector<int_t> x(m);     struct FirstOp {       int_t operator()(int_t lhs, int_t rhs) const { return std::max(lhs, rhs); }     };     struct SecondOp {       int_t operator()(int_t x, std::size_t) const { return x; }     };     SegmentTree<int_t, FirstOp, SecondOp> segment_tree(m);     for (int_t i = 0; i < m; ++i) {       int_t p;       int_t t;       std::cin >> p >> t;       int_t index = m - p;       if (t == 0) {         segment_tree.update(index, m, -1);       } else {         std::cin >> x[index];         segment_tree.update(index, m, 1);       }       if (m == 1) {         if (segment_tree.query(0, 1) > 0) {           std::cout << x[0] <<   n ;         } else {           std::cout << -1 <<   n ;         }         continue;       }       int_t start = 0;       int_t end = m;       while (start < end - 1) {         int_t mid = (start + end - 1) / 2;         if (segment_tree.query(start, mid + 1) > 0) {           end = mid + 1;         } else if (segment_tree.query(mid, end) > 0) {           start = mid + 1;         } else {           start = end = -1;         }       }       if (start == -1) {         std::cout << -1 <<   n ;       } else {         std::cout << x[start] <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int D[100010];   int TN[100010];   vector<int> G[100010];   vector<int> T[100010];   vector<long long int> TS[100010];   map<int, double> S[100010];   struct Furthest {     int s, t, x, maxl, N;     void DFS1(int u, int f, int l) {       if (l > maxl) {         x = u;         maxl = l;       }       TN[u] = N;       for (auto it = G[u].begin(); it != G[u].end(); ++it) {         int v = *it;         if (v != f) DFS1(v, u, l + 1);       }     }     void DFS2(int u, int f, int l) {       if (l > maxl) {         x = u;         maxl = l;       }       if (l > D[u]) D[u] = l;       for (auto it = G[u].begin(); it != G[u].end(); ++it) {         int v = *it;         if (v != f) DFS2(v, u, l + 1);       }     }     void DFS3(int u, int f, int l) {       if (l > D[u]) D[u] = l;       T[N].push_back(D[u]);       for (auto it = G[u].begin(); it != G[u].end(); ++it) {         int v = *it;         if (v != f) DFS3(v, u, l + 1);       }     }     void Find(int u, int n) {       N = n;       maxl = -1;       DFS1(u, -1, 0);       s = x;       maxl = -1;       DFS2(s, -1, 0);       t = x;       DFS3(t, -1, 0);       sort(T[N].begin(), T[N].end());       TS[N].push_back(0);       for (int i = 1; i <= T[N].size(); ++i)         TS[N].push_back(T[N][i - 1] + TS[N][i - 1]);       if (D[t] != D[s]) cout <<  Wrong!  << endl;     }   };   Furthest fest;   int main() {     ios::sync_with_stdio(false);     int n, m, q, i, j, k, x, y, cnt, u, v;     cin >> n >> m >> q;     for (i = 0; i < m; ++i) {       cin >> u >> v;       G[u].push_back(v);       G[v].push_back(u);     }     cnt = 0;     memset(D, 0xFF, sizeof(D));     for (i = 1; i <= n; ++i)       if (D[i] < 0) fest.Find(i, cnt++);     for (i = 0; i < q; ++i) {       cin >> u >> v;       x = TN[u];       y = TN[v];       if (x == y)         cout << -1 << endl;       else {         if (T[x].size() > T[y].size()) swap(x, y);         if (S[x].find(y) != S[x].end())           cout << S[x][y] << endl;         else {           int minv, maxl;           double sum = 0;           for (j = 0; j < T[x].size(); ++j) {             u = T[x][j];             maxl = max(*T[x].rbegin(), *T[y].rbegin());             minv = maxl - u;             k = lower_bound(T[y].begin(), T[y].end(), minv) - T[y].begin();             sum += (long long int)maxl * k + *TS[y].rbegin() - TS[y][k] +                    (T[y].size() - k) * (u + 1);           }           sum /= T[x].size() * T[y].size();           S[x][y] = sum;           cout << fixed << setprecision(10) << sum << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, cnt;   char s[111];   void dfs(int id, int d) {     if (id >= n) return;     if (s[id] ==  * )       cnt++;     else       return;     dfs(id + d, d);   }   int main() {     scanf( %d n , &n);     gets(s);     bool fl = 0;     for (int i = 0; i < n; ++i) {       for (int d = 1; d <= n; ++d) {         cnt = 0;         dfs(i, d);         if (cnt >= 5) fl = 1;       }     }     if (fl) {       puts( yes );     } else       puts( no );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int NR = 5005;   void Min(int& x, int y) { x = min(x, y); }   void Max(int& x, int y) { x = max(x, y); }   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 << 3) + (x << 1) + (ch ^ 48);       ch = getchar();     }     return x * f;   }   int n, m;   char s[NR << 1];   int sa[NR << 1], id[NR << 1];   int rk[NR << 1], lark[NR << 1];   int cnt[NR];   int h[NR << 1];   int dp[NR << 1];   int mn[NR][NR];   void work() {     memset(s, 0, sizeof(s));     memset(sa, 0, sizeof(sa));     memset(rk, 0, sizeof(rk));     n = read(), scanf( %s , s + 1);     m = max(n, 300);     memset(cnt, 0, sizeof(cnt));     for (int i = 1; i <= n; i++) cnt[rk[i] = s[i]]++;     for (int i = 1; i <= m; i++) cnt[i] += cnt[i - 1];     for (int i = n; i >= 1; i--) sa[cnt[rk[i]]--] = i;     for (int w = 1; w < n; w <<= 1) {       memset(cnt, 0, sizeof(cnt));       int p = 0;       for (int i = n - w + 1; i <= n; i++) id[++p] = i;       for (int i = 1; i <= n; i++)         if (sa[i] > w) id[++p] = sa[i] - w;       for (int i = 1; i <= n; i++) cnt[rk[id[i]]]++;       for (int i = 1; i <= m; i++) cnt[i] += cnt[i - 1];       for (int i = n; i >= 1; i--) sa[cnt[rk[id[i]]]--] = id[i];       memcpy(lark, rk, sizeof(lark));       rk[sa[1]] = m = 1;       for (int i = 2; i <= n; i++) {         if (lark[sa[i]] == lark[sa[i - 1]] &&             lark[sa[i] + w] == lark[sa[i - 1] + w])           rk[sa[i]] = m;         else           rk[sa[i]] = ++m;       }     }     memset(h, 0, sizeof(h));     int lcp = 0;     for (int i = 1; i <= n; i++) {       if (lcp) lcp--;       while (s[i + lcp] == s[sa[rk[i] - 1] + lcp]) lcp++;       h[rk[i]] = lcp;     }     for (int i = 1; i <= n; i++) {       mn[i][i] = h[i];       for (int j = i + 1; j <= n; j++) mn[i][j] = min(mn[i][j - 1], h[j]);     }     int ans = 0;     memset(dp, 0, sizeof(dp));     for (int i = 1; i <= n; i++) {       int len = dp[i] = n - i + 1;       for (int j = 1; j < i; j++) {         if (rk[i] < rk[j]) continue;         dp[i] = max(dp[i], dp[j] + len - mn[rk[j] + 1][rk[i]]);       }       ans = max(ans, dp[i]);     }     printf( %d n , ans);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) mn[i][j] = 0;   }   int main() {     int T = read();     while (T--) work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1e9 + 7;   long long n, a[100010];   int main() {     long long n, mx = 0, sum = 0;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] > mx) mx = a[i];       sum += a[i];     }     long long s = (sum + n - 2) / (n - 1);     if (mx > s) s = mx;     cout << s <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, tr;     cin >> n >> m;     vector<long long> a, b;     for (int i = 1; i <= n; i++) {       cin >> tr;       a.push_back(tr);     }     for (int i = 1; i <= m; i++) {       cin >> tr;       b.push_back(tr);     }     long long max, ans = LONG_LONG_MAX;     for (int i = 0; i < n; i++) {       max = LONG_LONG_MIN;       for (int j = 0; j < n; j++) {         if (j == i) continue;         for (int k = 0; k < m; k++) {           if (a[j] * b[k] > max) max = a[j] * b[k];         }       }       if (max < ans) ans = max;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<string> m;   int main() {     int n, k;     cin >> n >> k;     int e = 0;     for (int i = 1; i < k; i++) {       string t =   ;       t += char( A  + e / 26);       t += char( a  + e % 26);       e++;       m.push_back(t);     }     for (int i = k; i <= n; i++) {       string s;       cin >> s;       if (s ==  YES ) {         string t =   ;         t += char( A  + e / 26);         t += char( a  + e % 26);         e++;         m.push_back(t);       } else {         m.push_back(m[i - k]);       }     }     for (string &s : m) std::cout << s <<    ;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> v;   vector<pair<long long, long long> > ans;   long long sz, n, m, pos;   long long p2(long long x) {     long long y = sqrt(x);     for (long long i = y - 1; i <= y + 1; i++)       if (i * i == x) return y;     return false;   }   bool fnd(long long x) {     long long p = pos, i;     for (i = pos; i <= sz; i++) {       long long j = p2(i * i + x);       if (j) {         pos = j;         ans.push_back({i, j});         return true;       }     }     return false;   }   int32_t main() {     cin >> n;     for (long long i = 1; i <= n / 2; i++) {       long long x;       scanf( %lld , &x);       v.push_back(x);     }     sz = 1e7;     pos = 1;     for (auto x : v)       if (!fnd(x)) return cout <<  No  << endl, 0;     long long bef = 0;     cout <<  Yes  << endl;     for (auto p : ans) {       printf( %lld %lld  , p.first * p.first - bef * bef,              p.second * p.second - p.first * p.first);       bef = p.second;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m, h;     cin >> n >> m >> h;     long long r[n], c[m], a[n][m];     for (long long i = 0; i < m; i++) {       cin >> c[i];     }     for (int i = 0; i < n; i++) {       cin >> r[i];     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> a[i][j];       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (r[i] == c[j] && a[i][j] == 1) {           a[i][j] = r[i];         }       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (a[i][j] == 1) {           a[i][j] = min(r[i], c[j]);         }       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cout << a[i][j] <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = -1;   struct dot {     long long x, y;     dot(long long x = 0, long long y = 0) : x(x), y(y) {}   };   bool operator<(dot a, dot b) {     return ((a.x + a.y) == (b.x + b.y) ? a.x < b.x : (a.x + a.y) < (b.x + b.y));   }   bool Amber(dot a, dot b) { return (a.x - a.y) < (b.x - b.y); }   multiset<dot> L, R;   long long getans(long long st, vector<dot> &V) {     L.clear();     R.clear();     for (int i = (0); i < (int)(V.size()); i++) R.insert(V[i]);     long long ans = 0;     int p = 0;     for (long long lim = -st; lim < st; lim += 2) {       while (p < V.size() && (V[p].x - V[p].y < lim)) {         L.insert(V[p]);         R.erase(V[p]);         p++;       }       if (L.empty() || R.empty()) {         continue;       }       dot A = max(*L.begin(), *R.begin());       long long l = A.x + A.y;       dot B = min(*L.rbegin(), *R.rbegin());       long long r = B.x + B.y;       if (r < l) continue;       ans += (r - l) / 2;     }     return ans;   }   int n;   vector<dot> B, W;   int main() {     while (scanf( %d , &n) == 1) {       B.clear();       W.clear();       for (int i = (0); i < (int)(n); i++) {         dot T;         scanf( %lld %lld , &T.x, &T.y);         if ((T.x + T.y) % 2)           B.push_back(T);         else           W.push_back(T);       }       sort(B.begin(), B.end(), Amber);       sort(W.begin(), W.end(), Amber);       long long ans = getans(200004, B) + getans(200005, W);       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 9;   int n, u, v, w, t, p, sub[N], par[N], mark[N];   vector<pair<int, int> > g[N];   vector<pair<int, pair<int, int> > > edge[N];   pair<pair<int, int>, pair<int, int> > ed[N];   long long mx, ans[N];   set<int> s;   int Get_par(int u) {     if (par[u] < 0) return u;     return par[u] = Get_par(par[u]);   }   void merge(int u, int v) {     u = Get_par(u), v = Get_par(v);     if (par[v] < par[u]) swap(u, v);     par[u] += par[v];     par[v] = u;   }   void dfs1(int x) {     sub[x] = par[x] * -1, mark[x] = 1;     for (int i = 0; i < g[x].size(); i++)       if (!mark[g[x][i].first]) dfs1(g[x][i].first), sub[x] += sub[g[x][i].first];   }   void dfs2(int x, int rt, int e) {     mark[x] = 2;     ans[e] = 1ll * sub[x] * (sub[rt] - sub[x]);     for (int i = 0; i < g[x].size(); i++)       if (mark[g[x][i].first] < 2) dfs2(g[x][i].first, rt, g[x][i].second);   }   int main() {     fill(par, par + N, -1);     cin >> n;     for (int i = 1; i < n; i++) {       scanf( %d%d , &ed[i].second.first, &ed[i].second.second);       ;       scanf( %d , &ed[i].first.first);       ;       ed[i].first.second = i;     }     sort(ed + 1, ed + n);     for (int i = 1; i < n; i++) {       if (i != 1 && ed[i].first.first != ed[i - 1].first.first) p++;       edge[p].push_back(           make_pair(ed[i].first.second,                     make_pair(ed[i].second.first, ed[i].second.second)));     }     for (int k = 0; k < N; k++) {       for (int i = 0; i < edge[k].size(); i++) {         edge[k][i].second.first = Get_par(edge[k][i].second.first),         edge[k][i].second.second = Get_par(edge[k][i].second.second);         g[edge[k][i].second.first].clear(), g[edge[k][i].second.second].clear();       }       for (int i = 0; i < edge[k].size(); i++)         g[edge[k][i].second.first].push_back(             make_pair(edge[k][i].second.second, edge[k][i].first)),             g[edge[k][i].second.second].push_back(                 make_pair(edge[k][i].second.first, edge[k][i].first)),             mark[edge[k][i].second.first] = mark[edge[k][i].second.second] = 0;       for (int i = 0; i < edge[k].size(); i++)         if (!mark[edge[k][i].second.first]) dfs1(edge[k][i].second.first);       for (int i = 0; i < edge[k].size(); i++) {         merge(edge[k][i].second.first, edge[k][i].second.second);         if (mark[edge[k][i].second.first] < 2)           dfs2(edge[k][i].second.first, edge[k][i].second.first, 0);       }     }     for (int i = 1; i < n; i++) mx = max(mx, ans[i]);     ;     for (int i = 1; i < n; i++) t += (ans[i] == mx);     cout << mx * 2 <<     << t << endl;     for (int i = 1; i < n; i++)       if (ans[i] == mx) cout << i <<    ;   }


#include <bits/stdc++.h>   using namespace std;   struct Node {     int ancestor;     vector<int> children;   };   class TarjanOLCA {     enum Color {       WHITE = 0,       BLACK = 1,     };       public:     TarjanOLCA(const vector<Node>& tree, int root,                const vector<pair<int, int> >& query)         : m_tree(tree),           m_root(root),           m_size(tree.size()),           m_rank(m_size, -1),           m_parent(m_size, -1),           m_color(m_size, WHITE) {       for (vector<pair<int, int> >::const_iterator it = query.begin();            it != query.end(); ++it) {         int u = it->first;         int v = it->second;         if (m_query.count(u) == 0) {           m_query[u] = set<int>();         }         if (m_query.count(v) == 0) {           m_query[v] = set<int>();         }         m_query[u].insert(v);         m_query[v].insert(u);       }       solve();     }     const map<pair<int, int>, int> getLCA() const { return m_lca; }       private:     vector<Node> m_tree;     int m_root;     map<int, set<int> > m_query;     map<pair<int, int>, int> m_lca;     const int m_size;     vector<int> m_rank;     vector<int> m_parent;     vector<Color> m_color;     void makeSet(int k) {       assert(0 <= k && k < m_size);       m_color[k] = WHITE;       m_parent[k] = k;       m_rank[k] = 0;     }     int find(int k) {       if (m_parent[k] != k) {         m_parent[k] = find(m_parent[k]);       }       return m_parent[k];     }     void link(int u, int v) {       if (m_rank[u] > m_rank[v]) {         m_parent[v] = u;       } else {         m_parent[u] = v;         if (m_rank[u] == m_rank[v]) {           m_rank[v]++;         }       }     }     void join(int u, int v) { link(find(u), find(v)); }     void lca(int u) {       makeSet(u);       m_tree[u].ancestor = u;       for (int i = 0; i < (int)m_tree[u].children.size(); i++) {         int v = m_tree[u].children[i];         lca(v);         join(u, v);         m_tree[find(u)].ancestor = u;       }       m_color[u] = BLACK;       if (m_query.count(u) > 0) {         for (set<int>::const_iterator it = m_query[u].begin();              it != m_query[u].end(); ++it) {           int v = *it;           if (m_color[v] == BLACK) {             m_lca[make_pair(v, u)] = m_lca[make_pair(u, v)] =                 m_tree[find(v)].ancestor;           }         }       }     }     void solve() { lca(m_root); }   };   struct Fence {     int r;     int x;     int y;     Fence(int r_, int x_, int y_) : r(r_), x(x_), y(y_) {}     bool operator<(const Fence& other) const {       if (r < other.r)         return true;       else if (r > other.r)         return false;       else if (x < other.x)         return true;       else if (x > other.x)         return false;       else         return y < other.y;     }   };   struct CP {     int x;     int y;     CP(int x_, int y_) : x(x_), y(y_) {}   };   class Solution {    public:     Solution() {       int n, k;       cin >> n >> m >> k;       for (int i = 0; i < n; i++) {         int kx, ky;         cin >> kx >> ky;         cp.push_back(CP(kx, ky));       }       for (int i = 0; i < m; i++) {         int r, cx, cy;         cin >> r >> cx >> cy;         fence.push_back(Fence(r, cx, cy));       }       sort(fence.begin(), fence.end());       for (int i = 0; i < k; i++) {         int a, b;         cin >> a >> b;         query.push_back(make_pair(a - 1, b - 1));       }       makeTree();       height.resize(m + 1);       buildHeight(m, 0);       cpPos.resize(n);       buildCpPos();       transformQuery();       processQuery();     }       private:     int m;     vector<CP> cp;     vector<Fence> fence;     vector<Node> tree;     vector<pair<int, int> > query;     vector<int> height;     vector<int> cpPos;     bool pointInside(int i, int j) const {       int64_t xi = cp[i].x;       int64_t yi = cp[i].y;       int64_t xj = fence[j].x;       int64_t yj = fence[j].y;       int64_t rj = fence[j].r;       return rj * rj > (xi - xj) * (xi - xj) + (yi - yj) * (yi - yj);     }     bool fenceInside(int i, int j) const {       int64_t xi = fence[i].x;       int64_t yi = fence[i].y;       int64_t ri = fence[i].r;       int64_t xj = fence[j].x;       int64_t yj = fence[j].y;       int64_t rj = fence[j].r;       return (rj - ri) * (rj - ri) >              (xi - xj) * (xi - xj) + (yi - yj) * (yi - yj);     }     void makeTree() {       tree.resize(m + 1);       for (int i = 0; i <= m; i++) {         tree[i].ancestor = i;       }       for (int i = 0; i < m - 1; i++) {         for (int j = i + 1; j < m; j++) {           if (fenceInside(i, j)) {             tree[i].ancestor = j;             tree[j].children.push_back(i);             break;           }         }       }       for (int i = 0; i < m; i++) {         if (tree[i].ancestor == i) {           tree[i].ancestor = m;           tree[m].children.push_back(i);         }       }     }     void buildHeight(int u, int h) {       height[u] = h;       for (int i = 0; i < (int)tree[u].children.size(); i++) {         int v = tree[u].children[i];         buildHeight(v, h + 1);       }     }     void buildCpPos() {       for (int i = 0; i < (int)cp.size(); i++) {         int j = 0;         for (; j < m; j++) {           if (pointInside(i, j)) {             break;           }         }         cpPos[i] = j;       }     }     void transformQuery() {       for (int i = 0; i < (int)query.size(); i++) {         int a = query[i].first;         int b = query[i].second;         int u = cpPos[a];         int v = cpPos[b];         query[i] = make_pair(u, v);       }     }     void processQuery() {       TarjanOLCA olca(tree, m, query);       map<pair<int, int>, int> result = olca.getLCA();       for (int i = 0; i < (int)query.size(); i++) {         int u = query[i].first;         int v = query[i].second;         assert(result.count(make_pair(u, v)) > 0);         int w = result[make_pair(u, v)];         cout << height[u] + height[v] - 2 * height[w] << endl;       }     }   };   int main() {     Solution s;     return 0;   }


#include <bits/stdc++.h>      #define optimize ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);   #define S second   #define F first   #define ENDL   n    #define fori(i, n) for(int i=0; i<n; ++i)   #define foreach(n) for(int i=0; i<n; ++i)   #define read_v(v) for(int i=0; i<v.size(); ++i) cin >> v[i]   #define all(v) v.begin(), v.end()   #define asc_sort(v) sort(v.begin(), v.end())   #define desc_sort(v) sort(v.rbegin(), v.rend())   #define print_all(v) for(int i=0; i<(int)v.size(); ++i) cout << v[i] << (i<(int)v.size()-1 ?     :   n )   #define pb push_back   #define sz(v) v.size()   #define ceil_div(a, b) (a+b-1) / b   #define tcs(foo)  {                                        int tc;                                  cin >> tc;                               while(tc--) foo();                       }      using namespace std;      typedef string str;   typedef unsigned int ui;   typedef long long ll;   typedef unsigned long long ull;   typedef vector<int> vi;   typedef vector<bool> vbool;   typedef vector<long long> vll;   typedef vector<unsigned long long> vull;   typedef vector<string> vstr;   typedef vector<pair<int, int>> vpii;   typedef set<int> s_int;   typedef pair<int, int> pii;   typedef priority_queue<int, vector<int>, greater<int>> min_heap;   typedef priority_queue<int> max_heap;      string to_string(string s) { return     + s +    ; }      string to_string(const char* s) { return to_string((string) s); }      string to_string(bool b) { return (b ?  true  :  false ); }      template <typename A, typename B>   string to_string(pair<A, B> p) { return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; }      template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto &x : v) {       if (!first) res +=  ,  ;       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }      void debug_out() { cerr << endl; }      template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     debug_out(T...);   }      #ifdef LOCAL   #define debug(...) cerr <<  [  << #__VA_ARGS__ <<  ]: , debug_out(__VA_ARGS__)   #else   #define debug(...) 42   #endif      /********************       END TEMPLATE   *********************/   ll n, m, ans, sm, suma, sumb;      void solve(){     cin >> n >> m;     vi A, B, tmp(n);     int aux, i, j;     suma = sumb = sm  = 0;     ans = INT_MAX;        read_v(tmp);        for(auto &a : tmp){       cin >>  aux;       sm += a;       if(aux==1) {         A.pb(a);       }else {         sumb += a;         B.pb(a);       }     }        if(sm < m){       cout << -1 << endl;       return;     }        desc_sort(A);     desc_sort(B);        i = 0;     j = sz(B)-1;        for(i=0; i<=sz(A); ++i){       while(j >= 0 && suma+sumb-B[j] >= m)         sumb -= B[j--];          if(sumb+suma >= m) ans = min(ans, (ll)(j+1)*2+i);       if(i < sz(A)) suma += A[i];     }     cout << ans << endl;   }         int main(){     tcs(solve)     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Data {     bool bip;     int a, xora, ha;     int b, xorb, hb;   };   struct dsu {     int n;     bool bip = 1;     vector<int> x, t, h;     vector<Data> memo;     dsu(int nn) {       n = nn;       x.resize(n + 1);       t.resize(n + 1);       h.resize(n + 1);       for (int i = 1; i <= n; i++) {         x[i] = 0;         t[i] = i;         h[i] = 1;       }       bip = 1;     }     int root(int a) {       if (t[a] == a) {         return a;       } else {         return root(t[a]);       }     }     int color(int a) {       if (t[a] == a) {         return x[a];       } else {         return x[a] ^ color(t[a]);       }     }     void unite(int a, int b) {       int f = a;       int s = b;       a = root(a);       b = root(b);       memo.push_back({bip, a, x[a], h[a], b, x[b], h[b]});       if (a == b) {         if (color(f) == color(s)) {           bip = 0;         }         return;       }       if (h[a] > h[b]) {         swap(a, b);       }       h[b] += (h[a] == h[b]);       t[a] = b;       if (color(f) == color(s)) {         x[a] ^= 1;       }     }     void rev() {       Data it = memo.back();       memo.pop_back();       bip = it.bip;       int a, b;       a = it.a;       b = it.b;       t[a] = a;       t[b] = b;       x[a] = it.xora;       x[b] = it.xorb;       h[a] = it.ha;       h[b] = it.hb;     }   };   vector<bool> solve(int n, vector<pair<int, int>> operations) {     int q = (int)operations.size();     for (auto &it : operations) {       if (it.second > it.first) {         swap(it.first, it.second);       }     }     vector<vector<pair<int, int>>> events(4 * q + 7);     function<void(int, int, int, int, int, int, int)> add =         [&](int v, int tl, int tr, int l, int r, int x, int y) {           if (tr < l || r < tl) {             return;           }           if (l <= tl && tr <= r) {             events[v].push_back({x, y});             return;           }           int tm = (tl + tr) / 2;           add(2 * v, tl, tm, l, r, x, y);           add(2 * v + 1, tm + 1, tr, l, r, x, y);         };     map<pair<int, int>, int> last;     set<pair<int, int>> s;     for (int i = 0; i < q; i++) {       pair<int, int> it = operations[i];       if (s.count(it) == 0) {         last[it] = i;         s.insert(it);       } else {         add(1, 0, q - 1, last[it], i - 1, it.first, it.second);         s.erase(it);       }     }     for (auto &it : s) {       add(1, 0, q - 1, last[it], q - 1, it.first, it.second);     }     vector<bool> ret(q);     dsu d(n);     vector<pair<int, int>> now;     function<void(int, int, int)> dfs = [&](int v, int tl, int tr) {       for (auto &it : events[v]) {         now.push_back(it);         d.unite(it.first, it.second);       }       if (tl == tr) {         ret[tl] = d.bip;       } else {         int tm = (tl + tr) / 2;         dfs(2 * v, tl, tm);         dfs(2 * v + 1, tm + 1, tr);       }       for (auto &it : events[v]) {         now.pop_back();         d.rev();       }     };     dfs(1, 0, q - 1);     return ret;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, q;     cin >> n >> q;     vector<pair<int, int>> Operations(q);     for (int i = 0; i < q; i++) {       cin >> Operations[i].first >> Operations[i].second;     }     vector<bool> x = solve(n, Operations);     for (int i = 0; i < q; i++) {       cout << (x[i] ?  YES  :  NO ) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int t, a, b, c, ans, t1;   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d%d%d , &a, &b, &c);       ans = 0;       t1 = c / 2;       t1 = min(t1, b);       ans += t1 * 3;       b -= t1;       t1 = b / 2;       t1 = min(t1, a);       ans += t1 * 3;       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     string s;     cin >> s;     vector<int> st;     st.push_back(s[0]);     for (int i = 1; i < s.size(); i++) {       if (st.empty())         st.push_back(s[i]);       else {         if (st.back() == s[i])           st.pop_back();         else           st.push_back(s[i]);       }     }     int turns = (s.size() - st.size()) / 2;     cout << (turns % 2 ?  Yes  :  No );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int A[55], ctos, lleno;   long long D[52][52][52][52];   double res = 0, suma;   int main() {     scanf( %d , &ctos);     for (int i = 1; i <= ctos; i++) {       scanf( %d , &A[i]);       suma += A[i];     }     scanf( %d , &lleno);     for (int quien = 1; quien <= ctos; quien++) {       for (int dnde = ctos + 1; dnde > 0; dnde--) {         for (int lleva = 0; lleva <= 50; lleva++) {           for (int lugares = 0; lugares <= ctos; lugares++) {             if (dnde == quien) {               D[dnde][lleva][lugares][quien] = D[dnde + 1][lleva][lugares][quien];             } else if (dnde > ctos) {               if (lleva == 0 and lugares == 0) {                 D[dnde][lleva][lugares][quien] = 1;               } else {                 D[dnde][lleva][lugares][quien] = 0;               }             } else if (lleva == 0 or lugares == 0) {               if (lleva == 0 and lugares == 0) {                 D[dnde][lleva][lugares][quien] = 1;               } else {                 D[dnde][lleva][lugares][quien] = 0;               }             } else {               D[dnde][lleva][lugares][quien] = D[dnde + 1][lleva][lugares][quien];               if (lleva >= A[dnde]) {                 D[dnde][lleva][lugares][quien] +=                     D[dnde + 1][lleva - A[dnde]][lugares - 1][quien];               }             }           }         }       }     }     if (suma <= lleno) {       res = ctos;     } else {       for (int i = ctos - 1; i > 0; i--) {         for (int eleg = 1; eleg <= ctos; eleg++) {           suma = 0;           for (int es = lleno - A[eleg] + 1; es <= lleno; es++) {             suma += D[1][es][i][eleg];           }           suma *= i;           for (double e = ctos; e > i; e--) {             suma /= e;           }           for (int e = 2; e <= ctos - i - 1; e++) {             suma *= e;           }           res += suma;         }       }     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   int main() {     long a, b;     while (scanf( %ld%ld , &a, &b) != EOF) {       if (a == b) {         if (a % 2 == 0)           printf( 2 n );         else if (a % 3 == 0)           printf( 3 n );         else if (a % 5 == 0)           printf( 5 n );         else if (a % 7 == 0)           printf( 7 n );         else           printf( %ld , a);       } else         printf( 2 n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const double eps = 1e-8;   char ans[1024];   int K, X, N, M;   char st[64], ed[64];   long long num[64];   void printAns(char from, char to, int all, int len) {     int pos = 1;     if (from ==  A ) pos = 0;     for (int i = 0; i < len; i++) ans[i] =  B ;     for (int i = 0; i < all; i++)       ans[pos + 2 * i] =  A , ans[pos + 2 * i + 1] =  C ;     ans[0] = from;     ans[len - 1] = to;     ans[len] =   0 ;     printf( %s n , ans);   }   int main() {     scanf( %d%d%d%d , &K, &X, &N, &M);     for (char s1 =  A ; s1 <=  C ; s1++) {       for (char e1 =  A ; e1 <=  C ; e1++) {         if (N == 1 && s1 != e1) continue;         for (char s2 =  A ; s2 <=  C ; s2++) {           for (char e2 =  A ; e2 <=  C ; e2++) {             if (M == 1 && s2 != e2) continue;             long long tn = N - (s1 !=  A ) - (e1 !=  C );             long long tm = M - (s2 !=  A ) - (e2 !=  C );             long long inN = tn / 2;             long long inM = tm / 2;             st[1] = s1, ed[1] = e1;             st[2] = s2, ed[2] = e2;             for (long long i = 0; i <= inN; i++) {               for (long long k = 0; k <= inM; k++) {                 if (N == 2 && s1 ==  A  && e1 ==  C  && i == 0) continue;                 if (M == 2 && s2 ==  A  && e2 ==  C  && k == 0) continue;                 num[1] = i;                 num[2] = k;                 for (int g = 3; g <= K; g++) {                   st[g] = st[g - 2], ed[g] = ed[g - 1];                   num[g] = num[g - 1] + num[g - 2] +                            (ed[g - 2] ==  A  && st[g - 1] ==  C );                 }                 if (num[K] == X) {                   printAns(s1, e1, i, N);                   printAns(s2, e2, k, M);                   return 0;                 }               }             }           }         }       }     }     printf( Happy new year! n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string last_w =   ;   set<string> reserved;   void change_last_w() {     do {       int j(last_w.size() - 1);       while (j >= 0 && last_w[j] ==  z ) --j;       if (j == -1)         last_w.assign(last_w.size() + 1,  a );       else {         ++last_w[j];         for (int i(j + 1); i < last_w.size(); ++i) last_w[i] =  a ;       }     } while (reserved.count(last_w));   }   bool valid_char(char c) {     return isdigit(c) || isalpha(c) || c ==  _  || c ==  $ ;   }   int parse_word(const string& s) {     if (s.empty()) return 0;     if (isdigit(s[0])) return false;     int ans(0);     while (ans < s.size() && valid_char(s[ans])) ++ans;     return ans;   }   int parse_num(const string& s) {     if (s.empty()) return 0;     int ans(0);     while (ans < s.size() && isdigit(s[ans])) ++ans;     return ans;   }   int parse_reserved(const string& s) {     if (s.empty()) return 0;     int best(0);     string t;     for (int i(0); i < min<size_t>(20UL, s.size()) && s[i] !=  # ; ++i) {       t += s[i];       if (reserved.count(t)) best = i + 1;     }     return best;   }   int parse_all(const string& s) {     return max({parse_num(s), parse_reserved(s), parse_word(s)});   }   vector<string> parse(string s) {     vector<string> ans;     while (!s.empty() && s[0] !=  # ) {       int cnt(0);       while (cnt < s.size() && s[cnt] ==    ) ++cnt;       if (cnt) {         s = s.substr(cnt, s.size() - cnt);         continue;       }       int cur(parse_all(s));       ans.push_back(s.substr(0, cur));       s = s.substr(cur, s.size() - cur);     }     return ans;   }   vector<int> parse_len(string s) {     vector<int> ans;     while (!s.empty()) {       int cnt(0);       while (cnt < s.size() && s[cnt] ==    ) ++cnt;       if (cnt) {         s = s.substr(cnt, s.size() - cnt);         continue;       }       int cur(parse_all(s));       ans.push_back(cur);       s = s.substr(cur, s.size() - cur);     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     string s__;     for (int i(0); i < n; ++i) {       cin >> s__;       reserved.insert(s__);     }     cin >> n;     getline(cin, s__);     vector<string> tokens;     while (n--) {       getline(cin, s__);       auto add = parse(s__);       copy(begin(add), end(add), back_inserter(tokens));     }     map<string, string> was;     for (string& s : tokens)       if (!reserved.count(s) && !isdigit(s[0])) {         if (!was.count(s)) {           change_last_w();           was[s] = last_w;         }         s = was[s];       }     string ans;     int next_space(tokens.size());     for (int i(0); i < tokens.size(); ++i) {       string s(tokens[i]);       int j(i + 1);       while (true) {         if (j == tokens.size()) break;         s += tokens[j];         if (parse_all(s) != tokens[i].size()) {           next_space = min(next_space, j - 1);           break;         }         if (s.size() > 20) break;         ++j;       }       ans += tokens[i];       if (next_space == i) {         next_space = tokens.size();         ans +=    ;       }     }     cout << ans;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK: 1024000000,1024000000 )   using namespace std;   inline int getint() {     int _x = 0;     char _tc = getchar();     while (_tc <  0  || _tc >  9 ) _tc = getchar();     while (_tc >=  0  && _tc <=  9 ) _x *= 10, _x += (_tc -  0 ), _tc = getchar();     return _x;   }   const int N = 1010, M = 1e5 + 15, INF = 1e7;   bool vis[N];   int dis[N];   struct Edge {     int u, v, c, w;     int nxt;     Edge() {}     Edge(int t2, int t3, int t4, int t5, int t6)         : u(t2), v(t3), c(t4), w(t5), nxt(t6) {}   };   Edge e[M];   int h[N], ect;   int mom[N], id[N];   int n, m, A[N], D[N], X[N], ca, cd;   char s[10];   int S, T;   void init() {     memset(vis, false, sizeof(vis));     ect = 0;     memset(h, -1, sizeof(h));     ca = cd = 0;   }   void _add(int u, int v, int c, int w) {     e[ect] = Edge(u, v, c, w, h[u]);     h[u] = ect++;     e[ect] = Edge(v, u, 0, -w, h[v]);     h[v] = ect++;   }   int spfa() {     queue<int> Q;     for (int i = S + 1; i <= T; i++) dis[i] = 10 * INF;     Q.push(S);     vis[S] = true;     while (!Q.empty()) {       int u = Q.front();       Q.pop();       vis[u] = false;       for (int i = h[u]; i + 1; i = e[i].nxt) {         int v = e[i].v;         if (e[i].c && dis[v] > dis[u] + e[i].w) {           dis[v] = dis[u] + e[i].w;           if (!vis[v]) vis[v] = true, Q.push(v);           id[v] = i;         }       }     }     return dis[T];   }   void aug() {     int p = T;     while (p != S) {       e[id[p]].c -= 1;       e[id[p] ^ 1].c += 1;       p = e[id[p] ^ 1].v;     }   }   int MFMC() {     int ret = 0, d, tmp = 0;     for (int i = 1; i <= n; i++) {       d = spfa();       if (d == 10 * INF) return -ret;       tmp += d, ret = min(ret, tmp), aug();     }     for (int i = n + 1; i <= m; i++) {       d = spfa();       if (d == 10 * INF) return -ret;       tmp += d - 2 * INF, ret = min(ret, tmp), aug();     }     return -ret;   }   int main() {     init();     scanf( %d%d , &n, &m);     S = 0, T = n + m + 1;     dis[S] = 0;     for (int i = 1, x; i <= n; i++) {       scanf( %s %d , s, &x);       if (s[0] ==  A )         A[++ca] = x;       else         D[++cd] = x;     }     for (int i = 1; i <= n; i++) _add(i + m, T, 1, 0);     for (int i = 1; i <= m; i++) {       scanf( %d , &X[i]);       _add(S, i, 1, 0);       _add(i, T, 1, 2 * INF - X[i]);       for (int j = 1; j <= ca; j++)         if (X[i] >= A[j]) _add(i, j + m, 1, A[j] - X[i]);       for (int j = 1; j <= cd; j++)         if (X[i] > D[j]) _add(i, j + m + ca, 1, 0);     }     printf( %d n , MFMC());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     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 i = n - 1; i >= 0; i--) {       if (i == n - 1)         ans += a[i];       else if (a[i] < a[i + 1])         ans += a[i];       else if (a[i] >= a[i + 1]) {         if (a[i + 1] == 0)           a[i] = 0;         else {           ans += a[i + 1] - 1;           a[i] = a[i + 1] - 1;         }       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   long long mod = 1000000007;   long long mod2 = 998244353;   long long OO = 1e18;   long long david_villa_pow(long long x, long long n) {     if (n == 0) return 1;     long long u = david_villa_pow(x, n / 2);     u = ((u) * (u));     if (n % 2 == 1) u = ((u) * (x));     return u;   }   long long up(long long x, long long y) {     if (x % y == 0) return x / y;     return x / y + 1;   }   using namespace std;   long long a[200200];   int main() {     long long i, j;     ;     long long T;     cin >> T;     while (T--) {       long long n, k, mn = OO;       cin >> n >> k;       for (i = 0; i < n; i++) {         cin >> a[i];         mn = min(mn, a[i]);       }       long long indx = 0;       for (i = 0; i < n; i++) {         if (a[i] == mn) {           indx = i;           break;         }       }       long long res = 0;       for (i = 0; i < n; i++) {         if (i != indx) {           res += (k - a[i]) / mn;         }       }       cout << res << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = (1 << 30) - 1;   const long long linf = (1ll << 62) - 1;   int main() {     int n;     cin >> n;     vector<int> is_prime(n + 1, true), primes;     is_prime[0] = is_prime[1] = 0;     for (int i = 2; i * i <= n; i++) {       if (is_prime[i]) {         for (int j = i * i; j <= n; j += i) {           is_prime[j] = false;         }       }     }     vector<int> ans;     for (int i = 1; i <= n; i++) {       if (is_prime[i]) {         int q = i;         while (q <= n) {           ans.push_back(q);           q *= i;         }       }     }     cout << ((int)(ans).size()) << endl;     for (int i : ans) {       printf( %d  , i);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int i, n, k, r;     cin >> n >> k;     long long int a[n];     for (i = 0; i < n; i++) {       cin >> a[i];     }     long long int *m = min_element(a, a + n);     r = 0;     for (i = 0; i < n; i++) {       if ((a[i] - *m) % k == 0) {         r += (a[i] - *m) / k;       } else {         cout << -1;         return 0;       }     }     cout << r;   }