#include <bits/stdc++.h>   int main(int argc, const char* argv[]) {     int n;     std::cin >> n;     int step = 0;     int prev = 0;     int first = 0, second = 0;     bool prog = true;     int x = 0;     for (int i = 0; i < n; i++) {       std::cin >> x;       if (i == 0)         first = x;       else if (i == 1) {         second = x;         step = second - first;         prev = x;       } else {         if ((x - prev) != step) prog = false;         prev = x;       }     }     if (prog)       std::cout << x + step;     else       std::cout << x;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long N, W;     cin >> N >> W;     long long lowest = 0, highest = W;     long long current = 0, sum = 0;     while (N--) {       cin >> current;       sum += current;       lowest = max(lowest, -sum);       highest = min(highest, W - sum);     }     cout << max(0ll, highest - lowest + 1) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class team {    public:     int x, y, z;   };   int search(team *, int, int);   int main() {     int m, n, t;     cin >> n >> m;     int *arr1, *arr2;     arr1 = new int[m];     arr2 = new int[m];     for (int i = 0; i < m; i++) cin >> arr1[i] >> arr2[i];     team *res = new team[n / 3];     for (int i = 0; i < n / 3; i++) {       res[i].x = 0;       res[i].y = 0;       res[i].z = 0;     }     bool *tak = new bool[n + 1];     bool resf = false;     for (int i = 0; i < n + 1; i++) tak[i] = false;     tak[0] = true;     for (int i = 0; i < m; i++) {       if ((!tak[arr1[i]]) && (!tak[arr2[i]])) {         bool f = true;         for (int j = 0; j < n / 3; j++) {           if (res[j].x == 0) {             res[j].x = arr1[i];             res[j].y = arr2[i];             tak[arr1[i]] = true;             tak[arr2[i]] = true;             f = false;             break;           }         }         if (f) {           resf = true;           goto End;         }       } else if (tak[arr1[i]] && tak[arr2[i]]) {         if (search(res, arr1[i], n / 3) == search(res, arr2[i], n / 3))           continue;         else {           resf = true;           goto End;         }       } else if (tak[arr1[i]]) {         t = search(res, arr1[i], n / 3);         if (res[t].z == 0) {           res[t].z = arr2[i];           tak[arr2[i]] = true;         } else {           resf = true;           goto End;         }       } else if (tak[arr2[i]]) {         t = search(res, arr2[i], n / 3);         if (res[t].z == 0) {           res[t].z = arr1[i];           tak[arr1[i]] = true;         } else {           resf = true;           goto End;         }       }     }     for (int i = 0; i < n / 3; i++) {       if (res[i].x == 0) {         for (int j = 1; j < n + 1; j++)           if (!tak[j]) {             res[i].x = j;             tak[j] = true;             break;           }       }       if (res[i].y == 0) {         for (int j = 1; j < n + 1; j++)           if (!tak[j]) {             res[i].y = j;             tak[j] = true;             break;           }       }       if (res[i].z == 0) {         for (int j = 1; j < n + 1; j++)           if (!tak[j]) {             res[i].z = j;             tak[j] = true;             break;           }       }     }   End:     if (resf)       cout << -1;     else {       for (int i = 0; i < n / 3; i++)         cout << res[i].x <<     << res[i].y <<     << res[i].z << endl;     }     return 0;   }   int search(team *input, int val, int size) {     for (int i = 0; i < size; i++) {       if (input[i].x == val) return i;       if (input[i].y == val) return i;       if (input[i].z == val) return i;     }   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     long long n, k;     cin >> n >> k;     vector<long long> a(n);     vector<long long> b(n);     for (long long i = 0; i < n; i++) {       cin >> a[i];       a[i]--;     }     for (long long i = 0; i < n; i++) {       cin >> b[i];     }     long long cntZero = 0;     vector<long long> cnt(k);     for (long long i = 0; i < n; i++) {       cnt[a[i]]++;     }     for (long long i = 0; i < k; i++) {       if (cnt[i] == 0) cntZero++;     }     set<pair<long long, long long>> s;     for (long long i = 0; i < n; i++) {       s.insert({b[i], i});     }     long long ans = 0;     while (cntZero) {       auto pp = *s.begin();       s.erase(s.begin());       if (cnt[a[pp.second]] > 1) {         cntZero--;         cnt[a[pp.second]]--;         ans += pp.first;       }     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long l1, r1, l2, r2, k, ans;   int main() {     cin >> l1 >> r1 >> l2 >> r2 >> k;     if (r1 < l2 || r2 < l1) {       ans = 0;     } else {       long long r = min(r1, r2), l = max(l1, l2);       ans = r - l + 1;       if (k >= l && k <= r) ans--;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, sg, m, k;     cin >> n >> sg >> m >> k;     vector<int> a(n), starts(n, n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     for (int i = 0; i < sg; i++) {       int l, r;       cin >> l >> r;       l--;       r--;       starts[r] = min(starts[r], l);     }     auto bldPrefCnt = [&](int x) {       vector<int> res(n);       int cur = 0;       for (int i = 0; i < n; i++) {         if (a[i] <= x) {           cur++;         }         res[i] = cur;       }       return res;     };     auto check = [&](int x) {       auto prefs = bldPrefCnt(x);       vector<int> sufMem(n, -1);       map<int, int> sufL;       for (int i = 0; i < n; i++) {         int l = starts[i];         if (l == n) {           continue;         }         auto itL = sufL.lower_bound(l);         if (itL != sufL.end()) {           sufMem[i] = itL->first;         }         while (sufL.size() > 0 && l <= sufL.rbegin()->second) {           sufL.erase(prev(sufL.end()));         }         sufL[i] = l;       }       vector<vector<int>> dp(n, vector<int>(m + 1, 0));       int best = 0;       vector<int> prefMx(n);       for (int j = 1; j <= m; j++) {         vector<int> nxtPrefMx(n);         for (int i = 0; i < n; i++) {           auto &cur = dp[i][j];           if (starts[i] < n) {             int l = starts[i];             if (l > 0) {               cur = (prefs[i] - prefs[l - 1]) + prefMx[l - 1];             } else {               cur = prefs[i];             }             int u = sufMem[i];             if (u != -1) {               int take = prefs[i] - prefs[u];               cur = max(cur, take + dp[u][j - 1]);             }           }           nxtPrefMx[i] =               (i > 0 && nxtPrefMx[i - 1] > cur) ? nxtPrefMx[i - 1] : cur;           best = max(best, cur);         }         prefMx = nxtPrefMx;       }       return best >= k;     };     int l = 0, r = 1e9 + 10;     int ans = -1;     if (check(r)) {       while (l < r) {         int mid = (l + r) / 2;         if (check(mid)) {           r = mid;         } else {           l = mid + 1;         }       }       ans = r;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int w[40];   int main() {     int m, n;     cin >> m >> n;     for (int i = 1; i <= n; i++) {       cout << 1 << endl;       cin >> w[i];       if (w[i] == 0) {         return 0;       }     }     int l = 1, r = m;     int index = 1;     while (l <= r) {       int mid = (l + r) / 2;       cout << mid << endl;       int q;       cin >> q;       if (q == 0) return 0;       if (q == w[(index - 1) % n + 1]) {         l = mid + 1;       } else {         r = mid;       }       index++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<string, int> mp;   const int INF = 0x3F3F3F3F;   const int MOD = 1000003;   const int dx[] = {-1, 0, 1, 0};   const int dy[] = {0, 1, 0, -1};   vector<vector<char> > mat;   int n, m;   bool isvalid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }   int main() {     cin >> n >> m;     mat.resize(n);     for (int i = 0; i < n; i++) {       mat[i].resize(m);       for (int j = 0; j < m; j++) {         cin >> mat[i][j];       }     }     bool flag = 0;     int num = 0;     for (int i = 0; i < n; i++) {       if (flag) break;       bool l[2] = {0};       for (int j = 0; j < m; j++) {         if (mat[i][j] ==  . ) continue;         if (mat[i][j] ==  1  || mat[i][j] ==  2 ) {           l[j & 1] = 1;         } else {           l[(j + 1) & 1] = 1;         }       }       if (l[0] && l[1]) flag = 1;       if (!l[0] && !l[1]) num++;     }     for (int j = 0; j < m; j++) {       if (flag) break;       bool u[2] = {0};       for (int i = 0; i < n; i++) {         if (mat[i][j] ==  . ) continue;         if (mat[i][j] ==  1  || mat[i][j] ==  4 ) {           u[i & 1] = 1;         } else {           u[(i + 1) & 1] = 1;         }       }       if (u[0] && u[1]) flag = 1;       if (!u[0] && !u[1]) num++;     }     if (flag) {       cout << 0 << endl;       return 0;     }     int ans = 1;     for (int i = 0; i < num; i++) {       ans = (ans * 2) % MOD;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, i, cnt = 0;     cin >> n;     long long ar[n];     for (i = 0; i < n; i++) cin >> ar[i];     for (i = 0; i < n; i++) {       if (ar[i] % 2 != 0) cnt++;     }     cout << min(cnt, n - cnt);   }


#include <bits/stdc++.h>   using ll = long long;   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     vector<int> arr(n);     for (int i = 0; i < n; i++) cin >> arr[i];     sort(arr.begin(), arr.end());     vector<int> fst;     for (int i = 0; i < (n / 2); i++) fst.push_back(arr[i]);     vector<int> snd;     for (int i = (n / 2); i < n; i++) snd.push_back(arr[i]);     int hidden = 0;     int j = 0;     for (int i = 0; i < fst.size(); i++) {       while (j < snd.size() && snd[j] < 2 * fst[i]) j++;       if (j == snd.size()) break;       hidden++;       j++;     }     cout << n - hidden <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> nxt[1 << 13], G[15];   vector<pair<int, int> > lca[13];   bool a[14][14], ok[14][1 << 13];   long long int dp[14][1 << 13][2];   int n;   long long int solve(int root, int mask, int check) {     if (check == 0) {       int i;       for (i = 0; i < lca[root].size(); i++) {         int x = lca[root][i].first, y = lca[root][i].second;         if ((mask & (1 << x)) == 0 && x != root) return 0;         if ((mask & (1 << y)) == 0 && y != root) return 0;       }     }     long long int &ret = dp[root][mask][check];     if (ret != -1) return ret;     if (mask == 0) return ret = 1;     ret = 0;     int i, j, k;     for (i = 0; i < nxt[mask].size(); i++) {       int newmask = nxt[mask][i];       if (!ok[root][newmask]) continue;       int msb = -1, ch = -1;       for (k = 0; k < n; k++) {         if (mask & (1 << k)) msb = k;         if ((newmask & (1 << k)) && a[root][k]) {           if (ch == -1)             ch = k;           else             ch = -2;         }       }       if (ch == -2 || (newmask & (1 << msb)) == 0) continue;       if (ch == -1) {         for (k = 0; k < n; k++) {           if (newmask & (1 << k))             ret +=                 solve(k, newmask ^ (1 << k), 0) * solve(root, mask ^ newmask, 1);         }       } else         ret += solve(ch, newmask ^ (1 << ch), 0) * solve(root, mask ^ newmask, 1);     }     return ret;   }   int main() {     int i, j, m, k, k1, q;     scanf( %d%d , &n, &m);     ;     scanf( %d , &q);     ;     while (m--) {       int x, y;       scanf( %d%d , &x, &y);       ;       x--;       y--;       G[x].push_back(y);       G[y].push_back(x);       a[x][y] = 1;       a[y][x] = 1;     }     while (q--) {       int x, y, z;       scanf( %d%d , &x, &y);       ;       scanf( %d , &z);       ;       x--;       y--;       z--;       lca[z].push_back(make_pair(x, y));     }     for (i = 0; i < 1 << n; i++)       for (j = 0; j < i + 1; j++)         if ((i & j) == j) nxt[i].push_back(j);     for (i = 0; i < n; i++) {       for (j = 0; j < 1 << n; j++) {         bool f = 1;         if (j & (1 << i)) continue;         for (k = 0; k < n; k++) {           if ((j & (1 << k)) == 0) continue;           for (k1 = 0; k1 < G[k].size(); k1++)             if ((j & (1 << G[k][k1])) == 0 && G[k][k1] != i) f = 0;         }         for (k = 0; k < lca[i].size(); k++) {           int x = lca[i][k].first, y = lca[i][k].second;           if ((j & (1 << x)) && (j & (1 << y))) f = 0;         }         ok[i][j] = f;       }     }     memset(dp, -1, sizeof(dp));     cout << solve(0, ((1 << (n - 1)) - 1) << 1, 0) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using vi = vector<int>;   using db = double;   const int mxn = 300006;   const ll mod = 1E9L + 7;   int n;   ll a[mxn];   ll p[mxn];   int main() {     cin >> n;     for (int i = 0; i < n; ++i) scanf( %lld , &a[i]);     sort(a, a + n);     p[0] = 1;     for (int i = 1; i <= n; ++i) p[i] = p[i - 1] * 2 % mod;     ll ans = 0;     for (int i = 0; i < n; ++i) {       ans -= a[i] * p[n - 1 - i];       ans += a[i] * p[i];       ans %= mod;     }     cout << (ans + mod) % mod << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int inf = 0x3f3f3f3f3f3f3f3fll;   const int mx = 15000005;   vector<string> adj[2000005];   vector<bool> vis;   vector<pair<string, int>> pa;   int ind = 0, mxx = 0;   void dfs(int d, int ti) {     mxx = max(mxx, d);     adj[d].push_back(pa[ind].first);     while (ti--) {       ind++;       if (ind < pa.size()) dfs(d + 1, pa[ind].second);     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     string s;     cin >> s;     std::istringstream is(s);     std::string token;     int now = 1;     while (std::getline(is, token,  , )) {       if (now) {         pa.push_back({token, 0});       } else {         int val = 0;         for (char c : token) {           val = val * 10 + (c -  0 );         }         pa.back().second = val;       }       now = now ^ 1;     }     vis.assign(pa.size() + 1, false);     while (ind < pa.size()) {       dfs(0, pa[ind].second);       ind++;     }     int i = 0;     cout << mxx + 1 << endl;     while (100) {       if (adj[i].size() == 0) break;       for (string u : adj[i]) cout << u <<    ;       cout << endl;       i++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, int> times;   const int INF = 0x3fffffff;   struct Node {     int l, r;     int v;     Node() { l = -1, r = -1; }     Node(int _l, int _r) { l = _l, r = _r; }   } node[120000];   int fa[120000];   int n, v, ls, rs, root;   int ans;   int Findroot(int t) {     if (fa[t] != t) return Findroot(fa[t]);     return t;   }   void dfs(int mi, int ma, int now) {     if (node[now].v <= mi || node[now].v >= ma) {       if (times[node[now].v] != -1) {         ans++;         times[node[now].v]++;       }     } else {       ans -= times[node[now].v];       times[node[now].v] = -1;     }     if (node[now].l ^ (~0)) dfs(mi, min(ma, node[now].v), node[now].l);     if (node[now].r ^ (~0)) dfs(max(mi, node[now].v), ma, node[now].r);     return;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) fa[i] = i;     for (int i = 1; i <= n; i++) {       scanf( %d%d%d , &v, &ls, &rs);       if (ls ^ (~0)) fa[ls] = i;       if (rs ^ (~0)) fa[rs] = i;       node[i].l = ls;       node[i].r = rs;       node[i].v = v;     }     root = Findroot(1);     dfs(-INF, INF, root);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 400000 + 10;   const double eps = 1e-9;   const double pi = acos(-1);   int n;   long long k;   struct Vec {     int x, y;     double d() { return x * x + y * y; }   } p[N];   vector<double> v;   int id(double x) { return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; }   int bit[N];   void add(int x, int k) {     while (x < N) {       bit[x] += k;       x += x & (-x);     }   }   int sum(int x) {     int ans = 0;     while (x > 0) {       ans += bit[x];       x -= x & (-x);     }     return ans;   }   long long cal(vector<pair<double, double> > vec) {     memset(bit, 0, sizeof(bit));     sort(vec.begin(), vec.end());     v.clear();     for (auto p : vec) {       v.push_back(p.first);       v.push_back(p.second);     }     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());     long long res = 0;     for (auto p : vec) {       res += sum(id(p.second)) - sum(id(p.first));       add(id(p.second), 1);     }     return res;   }   double check(double x) {     vector<pair<double, double> > vec;     long long tot = 1LL * n * (n - 1) / 2, cnt = 0;     for (int i = 1; i <= n; i++) {       if (p[i].d() > x * x - eps) {         double dis = sqrt(p[i].d());         double theta = acos(x / dis);         double mid = atan2(p[i].y, p[i].x);         int pre = vec.size();         if (mid < 0) mid += 2 * pi;         if (mid + theta > 2 * pi) {           vec.push_back(make_pair(mid + theta - 2 * pi, mid - theta));         } else if (mid - theta < 0) {           vec.push_back(make_pair(mid + theta, mid - theta + 2 * pi));         } else {           vec.push_back(make_pair(mid - theta, mid + theta));         }         int cur = vec.size();       }     }     tot -= cal(vec);     return tot >= k;   }   int main() {     scanf( %d%lld , &n, &k);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &p[i].x, &p[i].y);     }     double lef = 0, rig = 2e4;     for (int i = 1; i <= 100; i++) {       double mid = (lef + rig) / 2;       if (check(mid))         rig = mid;       else         lef = mid;     }     printf( %.8f n , rig);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10);     int m, x, y;     cin >> m;     vector<int> M(m);     for (int i = 0; i < (m); ++i) cin >> M[i];     int sum = accumulate(M.begin(), M.end(), 0);     cerr << sum << endl;     cin >> x >> y;     int val = 0;     for (int i = 0; i < (m); ++i) {       val += M[i];       if (val > y) {         cout << 0 << endl;         exit(0);       }       if (val >= x && sum - val >= x && sum - val <= y) {         cout << i + 2 << endl;         exit(0);       }     }     cout << 0 << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long dp[5000005];   void solve() {     for (long long i = 1; i <= 5000000; i++) dp[i] = i * (i - 1) / 2;     for (long long i = 2; i <= 5000000 / 2; i++) {       for (long long j = 2; j <= i; j++) {         if (i * j > 5000000) break;         dp[i * j] = min(dp[i * j], dp[i] + j * (j - 1) / 2 * i);         dp[i * j] = min(dp[i * j], dp[j] + i * (i - 1) / 2 * j);       }     }     long long t, l, r;     cin >> t >> l >> r;     long long tt = 1, ans = 0;     while (l <= r) {       dp[l] %= (long long)(1e9 + 7);       ans = (ans + tt * dp[l]) % (long long)(1e9 + 7);       tt = (tt * t) % (long long)(1e9 + 7);       l++;     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct sak {     int x, y;     sak(int x1, int y1) {       x = x1;       y = y1;     }   };   vector<sak> A, B;   vector<int> AA, BB;   int findA(int k) {     int t1 = -1;     for (int i = 0; i < A.size(); i++)       if (A[i].x <= k && AA[i] == 0 && (t1 == -1 || A[t1].y < A[i].y)) {         t1 = i;       }     return t1;   }   int findB(int k) {     int t1 = -1;     for (int i = 0; i < B.size(); i++)       if (B[i].x <= k && BB[i] == 0 && (t1 == -1 || B[t1].y < B[i].y)) {         t1 = i;       }     return t1;   }   int main() {     int n, k;     while (scanf( %d %d , &n, &k) == 2) {       int x, y, z;       A.clear();       B.clear();       AA.clear();       BB.clear();       for (int i = 0; i < n; i++) {         scanf( %d %d %d , &x, &y, &z);         if (x == 0) {           A.push_back(sak(y, z));           AA.push_back(0);         } else {           B.push_back(sak(y, z));           BB.push_back(0);         }       }       int maxi = 0;       int sak1 = findA(k);       if (A.size() > 0 && sak1 != -1) {         int cnt = 1;         x = 1;         y = 0;         int kk = k + A[sak1].y;         AA[sak1] = 1;         while (1) {           sak1 = findB(kk);           if (sak1 == -1) break;           cnt++;           kk += B[sak1].y;           BB[sak1] = 1;           y++;           sak1 = findA(kk);           if (sak1 == -1) break;           cnt++;           kk += A[sak1].y;           AA[sak1] = 1;           x++;         }         if (cnt > maxi) maxi = cnt;       }       for (int i = 0; i < AA.size(); i++) AA[i] = 0;       for (int i = 0; i < BB.size(); i++) BB[i] = 0;       sak1 = findB(k);       if (B.size() > 0 && sak1 != -1) {         int cnt = 1;         x = 0;         y = 1;         int kk = k + B[sak1].y;         BB[sak1] = 1;         while (1) {           sak1 = findA(kk);           if (sak1 == -1) break;           cnt++;           kk += A[sak1].y;           AA[sak1] = 1;           x++;           sak1 = findB(kk);           if (sak1 == -1) break;           cnt++;           kk += B[sak1].y;           BB[sak1] = 1;           y++;         }         if (cnt > maxi) maxi = cnt;       }       printf( %d n , maxi);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcdExtended(long long a, long long b, long long *x, long long *y) {     if (a == 0) {       *x = 0;       *y = 1;       return b;     }     long long x1, y1;     long long gcd = gcdExtended(b % a, a, &x1, &y1);     *x = y1 - (b / a) * x1;     *y = x1;     return gcd;   }   int main() {     long long x, y, a, b, c;     cin >> a >> b >> c;     c = -c;     long long g = gcdExtended(a, b, &x, &y);     if (c % g == 0)       cout << c * x / g <<     << c * y / g << endl;     else       cout <<  -1  << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   inline int readChar();   template <class T = int>   inline T readInt();   template <class T>   inline void writeInt(T x, char end = 0);   inline void writeChar(int x);   inline void writeWord(const char *s);   static const int buf_size = 4096;   inline int getChar() {     static char buf[buf_size];     static int len = 0, pos = 0;     if (pos == len) pos = 0, len = fread(buf, 1, buf_size, stdin);     if (pos == len) return -1;     return buf[pos++];   }   inline int readChar() {     int c = getChar();     while (c <= 32) c = getChar();     return c;   }   template <class T>   inline T readInt() {     int s = 1, c = readChar();     T x = 0;     if (c ==  - ) s = -1, c = getChar();     while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar();     return s == 1 ? x : -x;   }   static int write_pos = 0;   static char write_buf[buf_size];   inline void writeChar(int x) {     if (write_pos == buf_size)       fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;     write_buf[write_pos++] = x;   }   template <class T>   inline void writeInt(T x, char end) {     if (x < 0) writeChar( - ), x = -x;     char s[24];     int n = 0;     while (x || !n) s[n++] =  0  + x % 10, x /= 10;     while (n--) writeChar(s[n]);     if (end) writeChar(end);   }   inline void writeWord(const char *s) {     while (*s) writeChar(*s++);   }   struct Flusher {     ~Flusher() {       if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;     }   } flusher;   const int M = 1e5 + 5;   int n;   pair<pair<int, int>, int> b[M];   vector<int> cop;   long long t[8 * M];   long long dp[M];   void update(int v, int vl, int vr, int l, int r, long long val) {     if (l > vr || r < vl) return;     if (l <= vl && vr <= r)       t[v] = max(t[v], val);     else {       int vm = (vl + vr) / 2;       update(v * 2 + 1, vl, vm, l, r, val);       update(v * 2 + 2, vm + 1, vr, l, r, val);       t[v] = max(t[v * 2 + 1], t[v * 2 + 2]);     }   }   long long get(int v, int vl, int vr, int l, int r) {     if (l > vr || r < vl) return 0;     if (l <= vl && vr <= r) return t[v];     int vm = (vl + vr) / 2;     return max(get(v * 2 + 1, vl, vm, l, r), get(v * 2 + 2, vm + 1, vr, l, r));   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d%d%d , &b[i].first.second, &b[i].first.first, &b[i].second);       cop.push_back(b[i].first.first), cop.push_back(b[i].first.second);     }     sort(cop.begin(), cop.end());     for (int i = 0; i < n; ++i) {       b[i].first.first =           lower_bound(cop.begin(), cop.end(), b[i].first.first) - cop.begin() + 1;       b[i].first.second = lower_bound(cop.begin(), cop.end(), b[i].first.second) -                           cop.begin() + 1;     }     sort(b, b + n);     dp[n - 1] = b[n - 1].second;     update(0, 0, 2 * M, b[n - 1].first.second, b[n - 1].first.second, dp[n - 1]);     long long mx = dp[n - 1];     for (int i = n - 2; i >= 0; --i) {       long long x = get(0, 0, 2 * M, 0, b[i].first.first - 1);       dp[i] = x + b[i].second;       mx = max(mx, dp[i]);       update(0, 0, 2 * M, b[i].first.second, b[i].first.second, dp[i]);     }     cout << mx;   }


#include <bits/stdc++.h>   using namespace std;   long long t, a, b, d;   bool fun(long long mid) {     long long total = mid * (mid + 1) / 2;     if (total >= d) {       return true;     }     return false;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> t;     while (t--) {       cin >> a >> b;       d = abs(a - b);       long long s = 0, e = 1000000007, mid;       while (s < e) {         mid = (s + e) / 2;         if (fun(mid)) {           e = mid;         } else {           s = mid + 1;         }       }       long long total = s * (s + 1) / 2;       while (total % 2 != d % 2) {         s++;         total = s * (s + 1) / 2;       }       cout << s << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int n;   bool sogl(char x) {     return x !=  a  && x !=  e  && x !=  i  && x !=  o  && x !=  u ;   }   int dp[3000], opt[3000];   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> s;     int n = s.size();     for (int i = 0; i < n; i++) {       dp[i] = i + 1;       opt[i] = i;       for (int j = i; j >= 0; j--) {         if (i - j + 1 >= 3 && sogl(s[j]) && sogl(s[j + 1]) && sogl(s[j + 2]) &&             (s[j] != s[j + 1] || s[j] != s[j + 2] || s[j + 1] != s[j + 2]))           break;         if ((j != 0 ? dp[j - 1] : 0) + 1 < dp[i]) {           dp[i] = (j != 0 ? dp[j - 1] : 0) + 1;           opt[i] = j;         }       }     }     vector<char> ans;     int v = n - 1;     while (v != -1) {       int u = opt[v];       for (int i = v; i >= u; i--) ans.push_back(s[i]);       ans.push_back(   );       v = u - 1;     }     ans.pop_back();     reverse(ans.begin(), ans.end());     for (char x : ans) cout << x;   }


#include <bits/stdc++.h>   using namespace std;   int a[5][5];   int b[] = {0, 1, 2, 3, 4};   long long sol() {     int p0 = b[0];     int p1 = b[1];     int p2 = b[2];     int p3 = b[3];     int p4 = b[4];     long long p = a[p0][p1] + a[p1][p0] + a[p2][p3] + a[p3][p2];     p += a[p1][p2] + a[p2][p1] + a[p3][p4] + a[p4][p3];     p += a[p2][p3] + a[p3][p2];     p += a[p3][p4] + a[p4][p3];     return p;   }   int main() {     for (long long i = 0; i < 5; i++)       for (long long j = 0; j < 5; j++) cin >> a[i][j];     long long m = 0;     do {       m = max(sol(), m);     } while (next_permutation(b, b + 5));     cout << m;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e6 + 5;   const int mod = 1e9 + 7;   int n, k, m, dp[N], lst[300];   char s[N];   inline int add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }   inline int mul(int x, int y) { return (long long int)x * y % mod; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> k >> (s + 1);     m = strlen(s + 1);     dp[0] = 1;     for (int i = 1; i <= m; i++) {       dp[i] = mul(dp[i - 1], 2);       if (lst[s[i]] != 0) dp[i] = add(dp[i], mod - dp[lst[s[i]] - 1]);       lst[s[i]] = i;     }     for (int i = m + 1; i <= n + m; i++) {       dp[i] = mul(dp[i - 1], 2);       int mn = 0;       for (int j = 0; j < k; j++) {         if (lst[mn +  a ] > lst[j +  a ]) mn = j;       }       if (lst[mn +  a ] != 0) dp[i] = add(dp[i], mod - dp[lst[mn +  a ] - 1]);       lst[mn +  a ] = i;     }     cout << dp[n + m] << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int r, c;     cin >> r >> c;     if (r == c and r == 1) return cout << 0, 0;     if (r == 1) {       for (int i = 0; i < c; i++) cout << 2 + i <<    ;       return 0;     }     if (c == 1) {       for (int i = 0; i < r; i++) cout << 2 + i <<    ;       return 0;     }     int ans[r][c];     for (int i = 0; i < r; i++)       for (int j = 0; j < c; j++) ans[i][j] = 1;     int val = r + c;     for (int i = 0; i < r; i++) {       for (int j = 0; j < c; j++) ans[i][j] = val;       val--;     }     for (int j = 0; j < c; j++) {       for (int i = 0; i < r; i++) {         ans[i][j] = ans[i][j] * val;       }       val--;     }     for (int i = 0; i < r; i++) {       for (int j = 0; j < c; j++) cout << ans[i][j] <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int a[s.length()];     int ans = -1;     int n = s.length();     for (int i = 0; i < s.length(); i++) {       a[i] = int(s[i]) - 48;     }     for (int i = 0; i < n; i++) {       if (a[i] % 8 == 0) {         ans = a[i];         break;       } else         for (int j = i + 1; j < n; j++) {           if ((a[i] * 10 + a[j]) % 8 == 0) {             ans = a[i] * 10 + a[j];             break;           } else             for (int k = j + 1; k < n; k++) {               if ((100 * a[i] + 10 * a[j] + a[k]) % 8 == 0) {                 ans = 100 * a[i] + 10 * a[j] + a[k];                 break;               }             }         }     }     if (ans != -1) {       cout <<  YES  << endl;       cout << ans;     } else       cout <<  NO ;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, a[3000000];   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> k;     int mini = 2000000;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       mini = min(mini, x);       a[max(1, x - k)]++;       a[x + 1]--;     }     for (int i = 1; i <= 1500000; i++) a[i] += a[i - 1];     for (int i = 1000000; i >= k + 1; i--) {       int sum = 0;       for (int j = 0; j <= 1000000; j += i) sum += a[j];       assert(sum <= n);       if (sum == n) {         cout << i << endl;         return 0;       }     }     cout << mini << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 3000000;   const int MAXM = 200000;   const int INF = 1000000010;   const long long int MOD = 1000000007;   const long long int P = 31;   const long double EPS = 1e-6;   int N, M, K;   char s[MAXN];   long long int dp[26] = {0};   int last_used[26];   int main() {     ios_base::sync_with_stdio(false);     scanf( %d %d , &M, &K);     scanf( %s , s);     N = strlen(s);     memset(last_used, -1, sizeof(last_used));     long long int tmp;     for (int i = 0; i < N; i++) {       tmp = 1;       for (int j = 0; j < K; j++) {         tmp = (tmp + dp[j]) % MOD;       }       dp[s[i] -  a ] = tmp;       last_used[s[i] -  a ] = i;     }     int ptr;     for (int i = 0; i < M; i++) {       ptr = 0;       tmp = 1;       for (int j = 0; j < K; j++) {         tmp = (tmp + dp[j]) % MOD;         if (last_used[j] < last_used[ptr]) ptr = j;       }       dp[ptr] = tmp;       last_used[ptr] = i + N;     }     tmp = 1;     for (int i = 0; i < K; i++) tmp = (tmp + dp[i]) % MOD;     printf( %I64d , tmp);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   #pragma GCC optimize( unroll-loops )   using namespace std;   const long double eps = 1e-7;   const int inf = 1000000010;   const long long INF = 1000000000000001000LL;   const int mod = 1000000007;   const int MAXN = 300010, LOG = 19;   struct Data {     long long w[2][2];   } SP[MAXN][LOG];   Data operator+(Data x, Data y) {     Data out;     for (int i = 0; i < 2; i++)       for (int j = 0; j < 2; j++)         out.w[i][j] = min(x.w[i][0] + y.w[0][j], x.w[i][1] + y.w[1][j]);     return out;   }   long long n, m, k, u, v, x, y, t, a, b;   long long W[MAXN][2];   int h[MAXN];   int par[MAXN][LOG];   Data D[MAXN];   long long dist[MAXN];   bool mark[MAXN];   vector<pair<int, int>> G[MAXN];   priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,                  greater<pair<long long, long long>>>       pq;   void dfs(int node) {     for (int i = 1; i < LOG; i++) {       par[node][i] = par[par[node][i - 1]][i - 1];       SP[node][i] = SP[node][i - 1] + SP[par[node][i - 1]][i - 1];     }     h[node] = h[par[node][0]] + 1;     for (pair<int, int> p : G[node])       if (p.first != par[node][0]) {         int v = p.first, i = p.second;         par[v][0] = node;         SP[v][0].w[0][0] = min(W[i][0], W[i][1] + dist[v] + dist[node]);         SP[v][0].w[1][1] = min(W[i][1], W[i][0] + dist[v] + dist[node]);         SP[v][0].w[0][1] = min(W[i][0] + dist[node], dist[v] + W[i][1]);         SP[v][0].w[1][0] = min(W[i][1] + dist[node], dist[v] + W[i][0]);         dfs(v);       }   }   int LCA(int x, int y) {     if (h[x] > h[y]) swap(x, y);     for (int i = 0; i < LOG; i++)       if ((h[y] - h[x]) & (1 << i)) y = par[y][i];     if (x == y) return x;     for (int i = LOG - 1; i >= 0; i--)       if (par[x][i] != par[y][i]) x = par[x][i], y = par[y][i];     return par[x][0];   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) cin >> x, pq.push({dist[i] = x, i});     for (int i = 1; i < n; i++) {       cin >> u >> v >> W[i][1] >> W[i][0];       G[u].push_back({v, i});       G[v].push_back({u, i});     }     while (!pq.empty()) {       int v = pq.top().second;       pq.pop();       if (mark[v]) continue;       mark[v] = 1;       for (pair<int, int> p : G[v])         if (!mark[p.first]) {           long long w = W[p.second][0] + W[p.second][1];           if (dist[v] + w < dist[p.first])             pq.push({dist[p.first] = dist[v] + w, p.first});         }     }     for (int i = 1; i <= n; i++) D[i].w[0][1] = D[i].w[1][0] = dist[i];     dfs(1);     cin >> m;     while (m--) {       cin >> x >> y;       u = (x + 1) / 2;       v = (y + 1) / 2;       int lca = LCA(u, v);       Data ans1 = D[u], ans2 = D[v];       for (int i = 0; i < LOG; i++)         if ((h[lca] - h[u]) & (1 << i)) ans1 = ans1 + SP[u][i], u = par[u][i];       for (int i = 0; i < LOG; i++)         if ((h[lca] - h[v]) & (1 << i)) ans2 = ans2 + SP[v][i], v = par[v][i];       swap(ans2.w[0][1], ans2.w[1][0]);       Data ans = ans1 + ans2;       cout << ans.w[x & 1][y & 1] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string solve() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     if (n % 2 == 0) return  YES ;     for (int i = 0; i < n - 1; i++)       if (a[i] >= a[i + 1]) return  YES ;     return  NO ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int t = 1;     cin >> t;     while (t--) {       cout << solve() <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = int(1e9) + 7;   long long pom[2][2] = {{0, 1}, {1, 1}};   void mulMat(long long *A, long long *B, int n) {     long long res[2][2] = {{0}};     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++)         for (int k = 0; k < n; k++) {           res[i][j] += (*(A + i * n + k)) * (*(B + k * n + j));           if (res[i][j] >= MOD) res[i][j] %= MOD;         }     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) *(A + i * n + j) = res[i][j];   }   void fastExp(long long pot, long long *T) {     while (pot) {       if (pot & 1) {         mulMat(T, pom[0], 2);       }       mulMat(pom[0], pom[0], 2);       pot >>= 1;     }     pom[0][0] = 0;     pom[0][1] = 1;     pom[1][0] = 1;     pom[1][1] = 1;   }   struct Node {     long long mat[2][2];     Node() {       mat[0][0] = 1;       mat[0][1] = 0;       mat[1][0] = 0;       mat[1][1] = 1;     }     Node(int pot) {       mat[0][0] = 1;       mat[0][1] = 0;       mat[1][0] = 0;       mat[1][1] = 1;       fastExp(pot, mat[0]);     }   };   void zbrojiMat(long long *resMat, long long *op1, long long *op2) {     for (int i = 0; i < 2; i++)       for (int j = 0; j < 2; j++) {         *(resMat + i * 2 + j) = (*(op1 + i * 2 + j) + *(op2 + i * 2 + j));         if (*(resMat + i * 2 + j) >= MOD) *(resMat + i * 2 + j) -= MOD;       }   }   int n, m;   struct Node zeroe;   struct segTree {     struct Node arr[1 << 18];     struct Node lazy[1 << 18];     int offset;     struct Node neutralElem;     void normalInit(int n) {       for (offset = 1; offset < n; offset *= 2)         ;       for (int i = 0; i < 2 * offset; i++) {         arr[i] = zeroe;         lazy[i] = neutralElem;       }     }     struct Node mergeValues(struct Node l, struct Node r) {       struct Node n;       zbrojiMat(n.mat[0], l.mat[0], r.mat[0]);       return n;     }     segTree(int n) { normalInit(n); }     segTree(int n, int *data) {       normalInit(n);       for (int i = offset; i < offset + n; i++)         arr[i] = Node(*(data + i - offset));       for (int i = offset - 1; i >= 1; i--)         arr[i] = mergeValues(arr[i * 2], arr[i * 2 + 1]);     }     void updateRange(int lo, int hi, long long *x) {       update(1, 0, offset, lo, hi + 1, x);     }     struct Node query(int lo, int hi) {       return q(1, 0, offset, lo, hi + 1);     }       private:     void prop(int node) {       mulMat(arr[node * 2].mat[0], lazy[node].mat[0], 2);       mulMat(arr[node * 2 + 1].mat[0], lazy[node].mat[0], 2);       mulMat(lazy[node * 2].mat[0], lazy[node].mat[0], 2);       mulMat(lazy[node * 2 + 1].mat[0], lazy[node].mat[0], 2);       lazy[node] = neutralElem;     }     void update(int node, int lo, int hi, int qlo, int qhi, long long *T) {       if (lo >= qhi or hi <= qlo) return;       if (lo >= qlo && hi <= qhi) {         mulMat(arr[node].mat[0], T, 2);         mulMat(lazy[node].mat[0], T, 2);         return;       }       prop(node);       int mid = (lo + hi) / 2;       if (node < offset) {         update(node * 2, lo, mid, qlo, qhi, T);         update(node * 2 + 1, mid, hi, qlo, qhi, T);         arr[node] = mergeValues(arr[node * 2], arr[node * 2 + 1]);       }     }     struct Node q(int node, int lo, int hi, int qlo, int qhi) {       if (lo >= qhi || hi <= qlo) return zeroe;       if (lo >= qlo && hi <= qhi) return arr[node];       prop(node);       int mid = (lo + hi) / 2;       return mergeValues(q(node * 2, lo, mid, qlo, qhi),                          q(node * 2 + 1, mid, hi, qlo, qhi));     }   };   const int MAX = 100005;   int data[MAX];   int main() {     double start = clock();     zeroe.mat[0][0] = 0;     zeroe.mat[1][1] = 0;     cin >> n >> m;     for (int i = 0; i < n; i++) scanf( %d , &data[i]);     segTree *T = new segTree(n, data);     if (n == 99395)       ;     for (int i = 0; i < m; i++) {       int id;       scanf( %d , &id);       int l, r;       scanf( %d%d , &l, &r);       l--;       r--;       if (id == 1) {         int x;         scanf( %d , &x);         long long Ti[2][2] = {{1, 0}, {0, 1}};         fastExp(x, Ti[0]);         T->updateRange(l, r, Ti[0]);       } else {         printf( %d n , T->query(l, r).mat[0][1]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int mod = 1000000007;   void __print(long long int x) { cerr << x; }   void __print(unsigned x) { cerr << x; }   void __print(unsigned long x) { cerr << x; }   void __print(unsigned long long x) { cerr << x; }   void __print(float x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(long double x) { cerr << x; }   void __print(char x) { cerr <<      << x <<     ; }   void __print(const char *x) { cerr <<     << x <<    ; }   void __print(const string &x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }   template <typename Th, typename V>   void __print(const pair<Th, V> &x) {     cerr <<  { ;     __print(x.first);     cerr <<  , ;     __print(x.second);     cerr <<  } ;   }   template <typename Th>   void __print(const Th &x) {     long long int f = 0;     cerr <<  { ;     for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i);     cerr <<  } ;   }   void _print() { cerr <<  ] n ; }   template <typename Th, typename... V>   void _print(Th t, V... v) {     __print(t);     if (sizeof...(v)) cerr <<  ,  ;     _print(v...);   }   void binary_rock() {     string a;     cin >> a;     long long int m;     cin >> m;     long long int i, j, k, l;     vector<long long int> b(m);     for (i = 0; i < m; i++) cin >> b[i];     vector<long long int> fre(26);     for (i = 0; i < 26; i++) fre[i] = 0;     long long int n = a.size();     for (i = 0; i < n; i++) {       fre[a[i] -  a ]++;     };     long long int pos = 25;     string ans =   ;     long long int nikal = 0;     for (i = 0; i < m; i++) ans.push_back(( - ));     ;     ;     while (1) {       long long int zero = 0;       vector<long long int> id;       for (i = 0; i < m; i++) {         if (b[i] == 0) id.push_back(i);       }       zero = id.size();       while (1) {         if (fre[pos] >= zero)           break;         else {           pos--;         }       }       for (i = 0; i < zero; i++) {         ans[id[i]] =  a  + pos;       };       for (i = 0; i < zero; i++) {         long long int x = id[i];         for (j = 0; j < m; j++) {           if (b[j] == 0) continue;           b[j] -= abs(x - j);         }       };       for (i = 0; i < zero; i++) {         b[id[i]] = -1;       }       nikal += zero;       if (nikal >= m) break;       pos--;     }     cout << ans << endl;   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     clock_t t1 = clock();     long long int t = 1;     cin >> t;     while (t--) {       binary_rock();     }     clock_t t2 = clock();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n;   long long p[16];   long long Count(long long n, int i) {     long long ret = (i + 1LL) * (n / p[i]);     n %= p[i];     if (n == 0) return ret;     return ret + min(Count(n, i - 1), i + 1 + Count(p[i] - n, i - 1));   }   int main() {     p[0] = 1;     for (int i = 0; i < 16; i++) p[i] = p[i - 1] * 10 + 1;     cin >> n;     cout << Count(n, 15);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[500010];   int main() {     int n;     cin >> n;     a[0] = 2;     vector<pair<int, int> > cumcnt;     for (int i = 1; i <= n; ++i) {       cin >> a[i];       if (a[i] == a[i - 1])         cumcnt.back().second++;       else         cumcnt.emplace_back(a[i], 1);     }     cumcnt.back().second++;     int N = (int(cumcnt.size()));     int st = 0;     int ans = 0;     for (int i = 0; i < N; i++) {       if (i == N - 1) break;       st += cumcnt[i].second;       int j = i + 1, e = 0;       while (j < N && cumcnt[j].second == 1) j++, e++;       if (j < N) {         int steps = (e + 1) / 2;         ans = max(ans, steps);         for (int make_pair = 0; make_pair < e; ++make_pair) {           if (cumcnt[i].first == cumcnt[j].first)             a[make_pair + st + 1] = cumcnt[i].first;           else {             if (make_pair < e / 2)               a[make_pair + st + 1] = cumcnt[i].first;             else               a[make_pair + st + 1] = cumcnt[j].first;           }         }       }       st += e;       i = j - 1;     }     cout << ans <<   n ;     for (int i = 1; i <= n; ++i) {       cout << a[i] <<    n [i == n];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000 + 7;   vector<vector<int> > g, gr;   vector<char> used;   vector<int> order, component;   void dfs1(int v) {     used[v] = true;     for (size_t i = 0; i < g[v].size(); ++i)       if (!used[g[v][i]]) dfs1(g[v][i]);     order.push_back(v);   }   void dfs2(int v) {     used[v] = true;     component.push_back(v);     for (size_t i = 0; i < gr[v].size(); ++i)       if (!used[gr[v][i]]) dfs2(gr[v][i]);   }   int main() {     int n, cost[MAXN];     cin >> n;     for (int i = 0; i < n; ++i) cin >> cost[i];     int m;     cin >> m;     g.resize(n);     gr.resize(n);     for (int i = 0; i < m; ++i) {       int a, b;       cin >> a >> b;       --a;       --b;       g[a].push_back(b);       gr[b].push_back(a);     }     used.assign(n, false);     for (int i = 0; i < n; ++i)       if (!used[i]) dfs1(i);     used.assign(n, false);     long long ficost = 0;     long long fix = 1;     for (int i = 0; i < n; ++i) {       int v = order[n - 1 - i];       if (!used[v]) {         dfs2(v);         int curmin = INT_MAX;         for (size_t j = 0; j < component.size(); ++j)           curmin = min(curmin, cost[component[j]]);         ficost += curmin;         int curc = 0;         for (size_t j = 0; j < component.size(); ++j)           if (cost[component[j]] == curmin) ++curc;         fix *= curc;         fix %= 1000000000 + 7;         component.clear();       }     }     cout << ficost <<     << fix << endl;   }


#include <bits/stdc++.h>   using namespace std;   string s1, s2;   int main() {     cin >> s1 >> s2;     if (s1.length() != s2.length())       cout <<  NO ;     else {       int x = 0;       int y = 0;       for (int i = 0; i < s1.length(); i++) {         if (s1[i] -  0  == 1) x++;         if (s2[i] -  0  == 1) y++;       }       if (x == 0 && y == 0)         cout <<  YES ;       else if (x == 0 && y != 0)         cout <<  NO ;       else if (x != 0 && y == 0)         cout <<  NO ;       else if (x != 0 && y != 0)         cout <<  YES ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1E18;   const int MAXN = 2000;   int l[MAXN];   int r[MAXN];   int a[MAXN];   long long dp[MAXN];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     cin >> n >> k;     bool poss = true;     long long extra = 0;     for (int i = 0; i < n; i++) {       cin >> l[i] >> r[i] >> a[i];       if (i && l[i] > r[i - 1]) extra = 0;       long long bullets = extra + a[i];       if (bullets > (long long)(r[i] - l[i] + 1) * k) {         poss = false;       }       extra = max(0LL, bullets - (long long)(r[i] - l[i]) * k);     }     if (!poss) {       cout <<  -1 n ;       return 0;     }     dp[n - 1] = a[n - 1];     for (int i = n - 2; i >= 0; i--) {       dp[i] = INF;       extra = 0;       long long totalUsed = 0;       for (int j = i; j < n; j++) {         totalUsed += a[j];         long long bullets = extra + a[j];         if (bullets > (long long)(r[j] - l[j] + 1) * k) break;         extra = bullets % k;         if (j < n - 1) {           long long last = max(0LL, bullets - (long long)(r[j] - l[j]) * k);           if (r[j] < l[j + 1] || !last) {             if (extra)               dp[i] = min(dp[i], totalUsed + (k - extra) + dp[j + 1]);             else               dp[i] = min(dp[i], totalUsed + dp[j + 1]);           }         } else {           dp[i] = min(dp[i], totalUsed);         }       }     }     cout << dp[0] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string n;   int k;   int main() {     cin >> n >> k;     if (n[0] ==  0 ) {       cout << 0 << endl;       return 0;     }     int count = 0, c = 0;     for (int i = n.size() - 1; i >= 0; i--) {       count += (n[i] ==  0 );       c += (n[i] !=  0 );       if (count == k) {         cout << c << endl;         return 0;       }       if (i == 0) {         for (auto m : n)           if (m ==  0 ) {             cout << n.size() - 1 << endl;             return 0;           }       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int n, m, tren, pos[N], cnt[N];   vector<int> g[N];   int dist(int a, int tr) {     if (a > tr)       return a - tr;     else       return n + a - tr;   }   bool cmp(int a, int b) {     if (dist(a, tren) < dist(b, tren)) return true;     return false;   }   int next(int i) {     if (i < n) return i + 1;     return 1;   }   int check(int i) {     int num = 0, curr = i, time = 0;     while (num < m) {       if (cnt[curr]) {         num += cnt[curr];         cnt[curr] = 0;       }       if (pos[curr] >= 0) {         cnt[g[curr][pos[curr]]]++;         pos[curr]--;       }       curr = next(curr);       time++;     }     return time - 1;   }   int main() {     scanf( %d , &n);     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%d , &x, &y);       g[x].push_back(y);     }     for (int i = 1; i <= n; i++) {       tren = i;       sort(g[i].begin(), g[i].end(), cmp);     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         cnt[j] = 0;         pos[j] = g[j].size() - 1;       }       printf( %d  , check(i));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int size = 100004;   char s[size][4];   struct node {     int l, r, cnt;     long long sum[6];   } tree[size * 4];   int tmp[size], q[size];   void pushup(int rt) {     int i;     for (i = 0; i < 5; ++i)       tree[rt].sum[i] =           tree[rt << 1].sum[i] +           tree[rt << 1 | 1].sum[(i - tree[rt << 1].cnt % 5 + 5) % 5];     return;   }   void build(int rt, int l, int r) {     int i, mid;     tree[rt].l = l;     tree[rt].r = r;     tree[rt].cnt = 0;     if (l == r) return;     for (i = 0; i < 5; ++i) tree[rt].sum[i] = 0;     mid = (l + r) >> 1;     build(rt << 1, l, mid);     build(rt << 1 | 1, mid + 1, r);   }   void update(int pos, int cnt, int rt, int add) {     tree[rt].cnt += cnt;     if (tree[rt].l == tree[rt].r) {       tree[rt].sum[0] += (long long)add;       return;     } else {       if (pos <= tree[rt << 1].r)         update(pos, cnt, rt << 1, add);       else         update(pos, cnt, rt << 1 | 1, add);       pushup(rt);     }   }   int main() {     int n, cnt, i, j, pos;     while (scanf( %d , &n) != EOF) {       cnt = 0;       for (i = 0; i < n; ++i) {         scanf( %s , s[i]);         if (s[i][0] !=  s ) {           scanf( %d , &q[i]);           tmp[cnt++] = q[i];         }       }       if (cnt > 0) {         sort(tmp, tmp + cnt);         cnt = unique(tmp, tmp + cnt) - tmp;         build(1, 1, cnt);       }       for (i = 0; i < n; ++i) {         if (s[i][0] ==  s ) {           cout << tree[1].sum[2] << endl;         } else {           pos = lower_bound(tmp, tmp + cnt, q[i]) - tmp;           if (s[i][0] ==  a )             update(pos, 1, 1, q[i]);           else             update(pos, -1, 1, -q[i]);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int used[100];   vector<vector<int>> g;   int cmp = 0;   vector<int> parent, rank1;   void make_set(int v) {     parent[v] = v;     rank1[v] = 0;   }   int find_set(int v) {     if (v == parent[v]) return v;     return parent[v] = find_set(parent[v]);   }   void union_sets(int a, int b) {     a = find_set(a);     b = find_set(b);     if (a != b) {       if (rank1[a] < rank1[b]) swap(a, b);       parent[b] = a;       if (rank1[a] == rank1[b]) ++rank1[a];     }   }   bool ok = 1;   void dfs(int v, int p = -1) {     used[v] = 1;     for (auto to : g[v]) {       if (!used[to])         union_sets(v, to), dfs(to, v);       else {         if (to != p) ok = 0;       }     }   }   signed main() {     ios_base::sync_with_stdio(0), cin.tie(0);     int n, m;     cin >> n >> m;     parent.resize(n);     rank1.resize(n);     for (int i = 0; i < n; ++i) make_set(i);     g.resize(n);     bool ok1 = 1, ok2 = 1, ok3 = 1;     bool ok4 = 1;     if (m > n) ok1 = 0;     for (int i = 0; i < m; ++i) {       int a, b;       cin >> a >> b;       a--, b--;       g[a].push_back(b);       g[b].push_back(a);       if (g[a].size() > 2 || g[b].size() > 2) {         ok2 = 0;       }     }     dfs(0);     for (int i = 0; i < n; ++i) {       if (!used[i]) ok3 = 0;       if (g[i].size() != 2) ok4 = 0;     }     if (ok4 && ok3) {       cout <<  YES  << endl << 0;       return 0;     }     memset(used, 0, sizeof(used));     for (int i = 0; i < n; ++i) {       if (!used[i]) dfs(i);     }     if (!(ok1 && ok2 && ok)) {       cout <<  NO ;       return 0;     }     vector<pair<int, int>> ans;     vector<int> cur;     vector<int> deg(n);     for (int i = 0; i < n; ++i) {       deg[i] = g[i].size();       if (deg[i] <= 1) cur.push_back(i);     }     for (int i = 0; i < n; ++i) {       if (!used[i]) {         dfs(i);         cmp++;       }     }     vector<int> nd;     for (int i = 0; i < n; ++i) {       for (int j = i + 1; j < n; ++j) {         if (deg[i] >= 2 || deg[j] >= 2) continue;         if (find_set(i) != find_set(j)) {           ans.push_back({i, j});           deg[i]++;           deg[j]++;           union_sets(i, j);         }       }     }     for (int i = 0; i < n; ++i) {       if (deg[i] <= 1) nd.push_back(i);     }     ans.push_back({nd[0], nd[nd.size() - 1]});     cout <<  YES n  << ans.size() <<   n ;     for (auto u : ans) cout << u.first + 1 <<     << u.second + 1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     double m, n;     long long int r, a, b, c, d, dist, sim;     cin >> r >> a >> b >> c >> d;     dist = (c - a) * (c - a) + (d - b) * (d - b);     m = sqrt(dist);     m = m / 2;     c = m / r;     m = m / r;     if (m == c)       cout << c;     else       cout << c + 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, bool> in;   int main() {     int n, mx = 0, arr[200], ans = 0;     char a[200];     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i] >> arr[i];       if (a[i] ==  + ) {         in[arr[i]] = true;         mx++;       } else if (a[i] ==  - ) {         if (in[arr[i]]) {           mx--;           in[arr[i]] = false;         } else           ans++;       }       ans = max(mx, ans);     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     cout << n / 2 << endl;     if (n % 2) {       while (n > 3) {         cout << 2 <<    ;         n -= 2;       }       cout << 3;     } else {       while (n) {         cout << 2 <<    ;         n -= 2;       }     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   long sail(char ar[], long a, long m, long n) {     for (int k = 0; k < a; k++) {       if (m > 0) {         if (ar[k] ==  E ) m--;       }       if (m < 0) {         if (ar[k] ==  W ) m++;       }       if (n > 0) {         if (ar[k] ==  N ) n--;       }       if (n < 0) {         if (ar[k] ==  S ) n++;       }       if (n == 0 && m == 0) {         return k + 1;       }     }     return -1;   }   int main() {     long a, b, c, d, e;     cin >> a >> b >> c >> d >> e;     char ar[100000];     for (int i = 0; i < a; i++) cin >> ar[i];     long m = d - b;     long n = e - c;     cout << sail(ar, a, m, n) << endl;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long int n, cnt = 0;     cin >> n;     for (long long i = 2; i <= sqrt(n); i++) {       long long int temp[n];       if ((i * i * i) <= n) cnt++;     }     cout << cnt + 1 << endl;   }   void opti() {     long long int n;     cin >> n;     set<int> a;     for (int i = 1; i * i <= n; i++) {       a.insert(i * i);     }     for (int i = 1; i * i * i <= n; i++) {       a.insert(i * i * i);     }     cout << a.size() << endl;   }   int main() {     int t;     cin >> t;     while (t--) opti();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a, b;   int main() {     cin >> a >> b;     cout << a + b << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <class a, class b>   pair<a, b> operator+(pair<a, b> x, pair<a, b> y) {     return (pair<a, b>(x.first + y.first, x.second + y.second));   }   template <class t>   ostream &operator<<(ostream &tout, const vector<t> &s) {     tout <<  [ ;     for (long long i = 0; i < s.size(); i++)       if (i + 1 == s.size())         tout << s[i];       else         tout << s[i] <<  , ;     tout <<  ] ;     return (tout);   }   template <class a, class b>   ostream &operator<<(ostream &tout, const pair<a, b> &c) {     return (tout <<  (  << c.first <<  ,  << c.second <<  ) );   }   const long long MAX_N = 200000 + 20;   const complex<double> change(1.42423, 1.31231);   pair<long long, long long> num[MAX_N];   long long n, order[MAX_N], k;   bool df(long long a, long long b) {     double x = (complex<double>(num[a].first, num[a].second) * change).real();     double y = (complex<double>(num[b].first, num[b].second) * change).real();     return (x < y);   }   pair<long long, long long> first[MAX_N], second[MAX_N];   long long amount() {     long long p1 = 1, p2 = 1, ans = 0;     while (p1 <= n && p2 <= n) {       if (first[p1] < second[p2]) {         ans++;         p1++;         continue;       }       if (first[p1] > second[p2]) {         ans++;         p2++;         continue;       }       p1++;     }     ans += (n + 1) * 2 - p1 - p2;     return (ans);   }   int main() {     cout << fixed;     cout.precision(1);     ios_base::sync_with_stdio(false);     cin >> n >> k;     if (k >= n) {       cout << -1 << endl;       return (0);     }     for (long long i = 1; i <= n; i++) {       cin >> num[i].first >> num[i].second;       num[i].first *= 2;       num[i].second *= 2;     }     vector<pair<long long, long long> > candidates;     for (long long i = 1; i <= n; i++) order[i] = i;     sort(order + 1, order + n + 1, df);     for (long long i = 1; i <= n && i <= 11; i++)       for (long long j = n; j >= 1 && j >= n - 10; j--)         if (i - 1 + n - j <= k)           candidates.push_back(pair<long long, long long>(               (num[order[i]].first + num[order[j]].first) / 2,               (num[order[i]].second + num[order[j]].second) / 2));     vector<pair<long long, long long> > result;     sort(candidates.begin(), candidates.end());     candidates.resize(unique(candidates.begin(), candidates.end()) -                       candidates.begin());     for (long long i = 1; i <= n; i++) first[i] = num[i];     sort(first + 1, first + n + 1);     for (long long i = 1; i <= n; i++)       second[i] = pair<long long, long long>(-first[n - i + 1].first,                                              -first[n - i + 1].second);     for (long long i = 0; i < candidates.size(); i++) {       if (i == 0)         for (long long j = 1; j <= n; j++) {           second[j].first += 2 * candidates[i].first;           second[j].second += 2 * candidates[i].second;         }       else         for (long long j = 1; j <= n; j++) {           second[j].first += 2 * (candidates[i].first - candidates[i - 1].first);           second[j].second +=               2 * (candidates[i].second - candidates[i - 1].second);         }       if (amount() <= n + k) result.push_back(candidates[i]);     }     cout << result.size() << endl;     for (long long i = 0; i < result.size(); i++)       cout << ((double)result[i].first) / 2 <<                << ((double)result[i].second) / 2 << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     int l, r;     int p = 0, j = 0, i = 0, len = 0, cnt = 0;     while (j < n) {       if (a[j] == 0) {         cnt++;       }       while (cnt > k) {         if (a[i] == 0) cnt--;         i++;       }       if (j - i + 1 > len) {         l = i;         r = j;         len = j - i + 1;       }       j++;     }     cout << len << endl;     for (i = 0; i < n; i++) {       if (i >= l && i <= r)         cout << 1 <<    ;       else         cout << a[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005, MAX = 202;   struct Leg {     int len, cost;     friend bool operator<(const Leg x, const Leg y) { return x.len < y.len; }   };   Leg l[N];   int sum[N];   int c[MAX][N];   int f[N];   bool vis[N];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %d , &l[i].len);     for (int i = 0; i < n; ++i) scanf( %d , &l[i].cost);     sort(l, l + n);     for (int i = 0; i < n; ++i) {       ++f[l[i].len];       ++c[l[i].cost][l[i].len];     }     for (int i = N - 1; i > 0; --i) {       f[i] += f[i + 1];     }     for (int i = 1; i < MAX; ++i) {       for (int j = 1; j < N; ++j) {         c[i][j] += c[i][j - 1];       }     }     int ans = INT_MAX;     for (int i = 0; i < n; ++i) {       int curLen = l[i].len;       if (vis[curLen]) continue;       vis[curLen] = 1;       int curFreq = f[curLen] - f[curLen + 1];       int all = n - f[curLen];       int total = 0;       for (int j = 1; j < MAX; ++j) {         total += (c[j][N - 1] - c[j][curLen]) * j;       }       priority_queue<pair<int, int>> q;       for (int j = 1; j < MAX; ++j) {         q.push(make_pair(-j, c[j][curLen - 1]));       }       while (!q.empty() && curFreq <= all) {         int cost = -q.top().first, cnt = q.top().second;         if (curFreq + cnt > all) {           total += (all - curFreq + 1) * cost;           all = curFreq - 1;           break;         }         total += cnt * cost;         all -= cnt;         q.pop();       }       ans = min(ans, total);     }     printf( %d , ans);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1001;   int n, a, b, u;   int sol[N][N];   void W() {     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         printf( %d , sol[i][j]);       }       printf(  n );     }   }   int main() {     cin >> n >> a >> b;     if (a != 1 && b != 1) {       cout <<  NO ;       return 0;     }     if (a == 1 && b == 1) {       if (n == 1) {         printf( YES n0 );         return 0;       }       if (n <= 3) {         cout <<  NO ;         return 0;       }       printf( YES n );       for (int i = 1; i < n; i++) {         sol[i][i + 1] = 1;         sol[i + 1][i] = 1;       }       W();       return 0;     }     printf( YES n );     if (a != 1)       u = a;     else       u = b;     for (int i = 1; i <= n - u; i++) {       sol[i][i + 1] = 1;       sol[i + 1][i] = 1;     }     if (a != 1) {       W();       return 0;     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (i != j) {           sol[i][j] ^= 1;         }       }     }     W();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<pair<int, int> > V;   int p[102345];   int main() {     int n, m, x, y;     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d , &x, &y);       V.insert(make_pair(x, y));       V.insert(make_pair(y, x));     }     for (int i = 0; i < n; i++) p[i] = i + 1;     int tim = 16384;     bool flag = false;     while (tim && !flag) {       random_shuffle(p, p + n);       p[n] = p[0];       int cnt = 0;       for (int i = 0; i < n; i++) {         if (!V.count(pair<int, int>(p[i], p[i + 1]))) cnt++;         if (cnt == m) {           for (int i = 0; m > 0; i++) {             if (!V.count(pair<int, int>(p[i], p[i + 1]))) {               printf( %d %d n , p[i], p[i + 1]);               m--;             }           }           flag = true;           break;         }       }       tim--;     }     if (!flag) puts( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long pow(long long b, long long e, long long m) {     long long t = 1;     for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0;     return t;   }   template <class T>   inline bool chkmin(T &a, T b) {     return a > b ? a = b, true : false;   }   template <class T>   inline bool chkmax(T &a, T b) {     return a < b ? a = b, true : false;   }   template <class T>   inline T sqr(T x) {     return x * x;   }   template <typename T>   T gcd(T x, T y) {     for (T t; x; t = x, x = y % x, y = t)       ;     return y;   }   template <class edge>   struct Graph {     vector<vector<edge> > adj;     Graph(int n) {       adj.clear();       adj.resize(n + 5);     }     Graph() { adj.clear(); }     void resize(int n) { adj.resize(n + 5); }     void add(int s, edge e) { adj[s].push_back(e); }     void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); }     vector<edge> &operator[](int t) { return adj[t]; }   };   int main() {     ios_base::sync_with_stdio(false);     int n, ans = 0;     cin >> n;     stack<int> Q;     for (int i = 1, v; i <= n; ++i) {       cin >> v;       for (; !Q.empty(); Q.pop()) {         int x = Q.top();         chkmax(ans, v ^ x);         if (x > v) {           break;         }       }       Q.push(v);     }     cout << setprecision(15) << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> a[100010], dp[100010];   int len[100010];   int main() {     int n, m, i, j, k, l, r;     while (~scanf( %d%d , &n, &m)) {       for (i = 1; i <= n; i++) {         a[i].resize(m + 1);         for (j = 1; j <= m; j++) scanf( %d , &a[i][j]);       }       memset(len, 0, sizeof(len));       for (i = 1; i <= n; i++) {         dp[i].resize(m + 1);         if (i == 1)           for (j = 1; j <= m; j++) dp[i][j] = 1;         else           for (j = 1; j <= m; j++)             if (a[i][j] >= a[i - 1][j])               dp[i][j] = dp[i - 1][j] + 1;             else               dp[i][j] = 1;         for (j = 1; j <= m; j++) len[i] = max(len[i], dp[i][j]);       }       scanf( %d , &k);       while (k--) {         scanf( %d%d , &l, &r);         if (len[r] >= r - l + 1)           printf( Yes n );         else           printf( No n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<char> Set;   pair<int, int> p[181];   string s, sn;   int i, k, sum;   bool pole[181];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> s;     cin >> k;     for (i = 0; i < s.size(); i++) p[s[i] - 96].first++;     for (i = 1; i <= 26; i++) p[i].second = i;     sort(p + 1, p + 1 + 26);     for (i = 1; i <= 26; i++) {       sum += p[i].first;       if (k - sum >= 0)         pole[p[i].second] = true;       else         break;     }     for (i = 0; i < s.size(); i++)       if (!pole[s[i] - 96]) {         Set.insert(s[i]);         sn += s[i];       }     cout << Set.size() <<   n ;     cout << sn <<   n ;     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, m, r;     scanf( %d%d%d , &n, &m, &r);     if (2 * r <= n && 2 * r <= m)       puts( First );     else       puts( Second );   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int v, u, w;     edge() {}     edge(int v, int u, int w) : v(v), u(u), w(w) {}   };   const int N = 2e5 + 7;   vector<pair<int, bool>> g[N];   vector<pair<int, int>> gg[N];   vector<edge> glob_ans;   int color[N];   void init(int n) {     for (int i = 1; i <= n; i++) {       g[i].clear();       gg[i].clear();       color[i] = -1;     }     glob_ans.clear();   }   bool dfs0(int v, int cl) {     color[v] = cl;     for (auto [u, c] : g[v]) {       if (color[u] == -1) {         if (!dfs0(u, c ^ cl)) return 0;       } else {         if (color[u] != (c ^ cl)) return 0;       }     }     return 1;   }   void dfs(int v, int pr = -1) {     for (auto [u, w] : gg[v]) {       if (u == pr) continue;       if (w != -1) {         assert(__builtin_popcount(w) % 2 == (color[v] ^ color[u]));         glob_ans.push_back(edge(v, u, w));       } else {         glob_ans.push_back(edge(v, u, color[v] ^ color[u]));       }       dfs(u, v);     }   }   void solve() {     int n, m;     cin >> n >> m;     init(n);     for (int i = 0; i < n - 1; i++) {       int x, y, v;       cin >> x >> y >> v;       gg[x].push_back({y, v});       gg[y].push_back({x, v});       if (v != -1) {         int c = __builtin_popcount(v) % 2;         g[x].push_back({y, c});         g[y].push_back({x, c});       }     }     for (int i = 0; i < m; i++) {       int a, b, p;       cin >> a >> b >> p;       g[a].push_back({b, p});       g[b].push_back({a, p});     }     for (int i = 1; i <= n; i++) {       if (color[i] == -1) {         if (!dfs0(i, 0)) {           cout <<  NO n ;           return;         }       }     }     dfs(1);     cout <<  YES n ;     for (auto elem : glob_ans) {       cout << elem.v <<     << elem.u <<     << elem.w <<   n ;     }   }   signed main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     for (int i = 0; i < t; i++) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<long long> v(100005);   vector<int> cache(1e5 + 10, -1);   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     map<int, int> mp;     for (long long i = 0; i < n; i++) {       int x;       cin >> x;       set<int> st;       for (int k = 2; k * k <= x; k++) {         if (x % k == 0) {           st.insert(k);           while (x % k == 0) {             x = x / k;           }         }       }       int mx = 0;       if (x > 1) {         st.insert(x);       }       for (auto it : st) {         mp[it]++;         mx = max(mx, mp[it]);       }       for (auto it : st) {         mp[it] = mx;       }     }     int maxi = 1;     for (auto it : mp) maxi = max(maxi, it.second);     cout << maxi <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int a[10], b[10];   int main() {     int m, n;     cin >> m >> n;     for (int i = 0; i < m; i++) cin >> a[i];     for (int j = 0; j < n; j++) cin >> b[j];     sort(a, a + m);     sort(b, b + n);     int flag = 0, ans;     for (int i = 0, j = 0; i < m && j < n;) {       if (a[i] == b[j]) {         ans = a[i];         flag = 1;         break;       } else if (a[i] < b[j])         i++;       else if (a[i] > b[j])         j++;     }     if (flag == 1) {       cout << ans << endl;     } else {       if (a[0] == b[0])         cout << a[0] << endl;       else {         if (a[0] < b[0])           cout << a[0] * 10 + b[0] << endl;         else           cout << b[0] * 10 + a[0] << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1005;   int n, m;   char c[maxn][maxn];   static int dx[] = {-1, 0, 0, 1};   static int dy[] = {0, -1, 1, 0};   namespace dsu {   int fa[maxn * maxn];   int cnt[maxn * maxn];   int id(int x, int y) { return x * m + y; }   int findfa(int x) {     if (fa[x] == x) return x;     return fa[x] = findfa(fa[x]);   }   void unite(int x, int y) {     x = findfa(x);     y = findfa(y);     if (x == y) return;     if (cnt[x] < cnt[y]) swap(x, y);     fa[y] = x;     cnt[x] += cnt[y];   }   }  // namespace dsu   using namespace dsu;   bool judge(int x, int y) {     if (x < 0 || y < 0 || x >= n || y >= m) return 0;     if (c[x][y] ==  * ) return 0;     return 1;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> c[i][j];       }     for (int i = 0; i < n * m; i++) fa[i] = i, cnt[i] = 1;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         if (c[i][j] ==  * ) continue;         for (int k = 0; k < 4; k++) {           int _i = i + dx[k];           int _j = j + dy[k];           if (!judge(_i, _j)) continue;           int id1 = id(i, j);           int id2 = id(_i, _j);           unite(id1, id2);         }       }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (c[i][j] !=  * ) {           cout <<  . ;           continue;         }         set<int> st;         for (int k = 0; k < 4; k++) {           int _i = i + dx[k];           int _j = j + dy[k];           if (!judge(_i, _j)) continue;           st.insert(findfa(id(_i, _j)));         }         int ans = 0;         while (!st.empty()) {           int id = *st.begin();           st.erase(st.begin());           ans += cnt[id];         }         cout << (ans + 1) % 10;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int b, d, ct;   int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }   int main() {     cin >> b >> d;     while (gcd(b, d) > 1) {       d /= gcd(b, d);       ++ct;     }     if (d == 1)       cout <<  2-type n  << ct <<   n ;     else if (!ct && b % d == 1)       cout <<  3-type n ;     else if (!ct && b % d == d - 1)       cout <<  11-type n ;     else if ((b - 1) * (b + 1) / (b % 2 + 1) % d == 0)       cout <<  6-type n ;     else       cout <<  7-type n ;   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, mod, sumz, sumy, sum, last, first, kol, z;   long long run(long long x, long long step) {     long long res = 1;     while (step) {       if (step & 1) res = (res * x) % mod;       x = x * x % mod;       step >>= 1;     }     return res % mod;   }   int main() {     cin >> a >> b;     mod = 1000 * 1000 * 1000 + 7;     sumz = (a * b) % mod;     sumz = (sumz * (b - 1)) % mod;     sumz = (sumz * run(2, mod - 2)) % mod;     sumy = (a * (a + 1)) % mod;     sumy = (sumy * b) % mod;     sumy = (sumy * (b - 1)) % mod;     sumy = (sumy * run(4, mod - 2)) % mod;     sum = ((b * sumy) % mod + sumz) % mod;     cout << sum << endl;     return 0;   }


#include <bits/stdc++.h>   int main() {     int ans = 0;     int tub[3] = {0};     int n, a;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a);       tub[a % 3]++;     }     ans = (tub[0] / 2) + ((tub[1] > tub[2]) ? tub[2] : tub[1]);     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1 << 30;   const double EPS = 1e-7;   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     string s;     cin >> s;     int ans = 0;     for (int i = n; i < (int)s.length(); i += n) {       bool equal = true;       for (int j = 1; j <= 3; j++)         if (s[i - j] != s[i - 1]) equal = false;       if (equal) ans++;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int din[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   bool correct(int d, int m, int y) {     if (d > 31 || m > 12 || y > 99) return false;     int dc = din[m - 1];     if (y % 4 == 0 && m == 2) dc++;     if (d > dc) return false;     return true;   }   bool good(int db, int mb, int yb, int df, int mf, int yf) {     yb += 18;     if (yb > yf) return false;     if (yb < yf) return true;     if (mb > mf) return false;     if (mb < mf) return true;     if (db > df) return false;     return true;   }   int main() {     int db, mb, yb, df, mf, yf;     scanf( %d.%d.%d , &df, &mf, &yf);     scanf( %d.%d.%d , &db, &mb, &yb);     if (correct(db, mb, yb) && good(db, mb, yb, df, mf, yf)) {       cout <<  YES ;       return 0;     }     if (correct(db, yb, mb) && good(db, yb, mb, df, mf, yf)) {       cout <<  YES ;       return 0;     }     if (correct(mb, db, yb) && good(mb, db, yb, df, mf, yf)) {       cout <<  YES ;       return 0;     }     if (correct(mb, yb, db) && good(mb, yb, db, df, mf, yf)) {       cout <<  YES ;       return 0;     }     if (correct(yb, db, mb) && good(yb, db, mb, df, mf, yf)) {       cout <<  YES ;       return 0;     }     if (correct(yb, mb, db) && good(yb, mb, db, df, mf, yf)) {       cout <<  YES ;       return 0;     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   constexpr int N = 23;   int n, m, u, v, adj[N], hamsaye[1 << N], ans = N, ans2;   bool connected[1 << N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     if (2 * m == n * (n - 1)) {       cout << 0 <<   n ;       return 0;     }     for (int i = 0; i < m; i++) {       cin >> u >> v;       u--, v--;       adj[u] ^= 1 << v;       adj[v] ^= 1 << u;     }     for (int i = 0; i < n; i++) adj[i] ^= (1 << i);     connected[0] = true;     for (int mask = 1; mask < (1 << n); mask++) {       if (__builtin_popcount(mask) == 1) {         connected[mask] = true;         continue;       }       for (int i = 0; i < n; i++) {         if ((adj[i] & (mask ^ (1 << i))) && connected[mask ^ (1 << i)])           connected[mask] = true;       }     }     for (int mask = 1; mask < (1 << n); mask++) {       hamsaye[mask] =           adj[__builtin_ctz(mask)] | hamsaye[mask ^ (1 << __builtin_ctz(mask))];       if (connected[mask] && __builtin_popcount(hamsaye[mask]) == n) {         if (__builtin_popcount(mask) < ans) {           ans = __builtin_popcount(mask);           ans2 = mask;         }       }     }     cout << ans <<   n ;     for (int i = 0; i < n; i++)       if (ans2 & (1 << i)) cout << i + 1 <<    ;     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k;     cin >> k;     auto n = 19;     auto t = 1;     while (true) {       auto temp = n;       auto sum = 0;       while (temp > 0) {         auto a = temp % 10;         temp = temp / 10;         sum = sum + a;       }       if (sum == 10) {         if (t == k) {           break;         } else {           t++;         }       }       n++;     }     cout << n <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int k;     cin >> k;     string s, t;     cin >> s;     cin >> t;     int sarr[k];     int tarr[k];     for (int i = k - 1; i >= 0; i--) {       sarr[i] = s[i] -  a ;       tarr[i] = t[i] -  a ;     }     int sumarr[k + 1];     int carry = 0;     for (int i = k - 1; i >= 0; i--) {       sumarr[i + 1] = (sarr[i] + tarr[i] + carry) % 26;       if (sarr[i] + tarr[i] + carry >= 26) {         carry = 1;       } else {         carry = 0;       }     }     sumarr[0] = carry;     for (int i = 0; i <= k; i++) {       if (sumarr[i] % 2 == 1) {         sumarr[i + 1] += 26;       }       sumarr[i] /= 2;     }     for (int i = 1; i <= k; i++) {       cout << (char)(sumarr[i] +  a );     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, c = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &x);       if (!x && !c)         c = 1;       else if (c == 1 && !x)         c = 2;     }     if (n == 1) {       if (x)         printf( YES n );       else         printf( NO n );     } else {       if (c == 1)         printf( YES n );       else         printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 10;   const long long int INFLL = 1e18 + 10;   const long double EPS = 1e-8;   const long double EPSLD = 1e-14;   const long long int MOD = 1000000007;   template <class T>   T &chmin(T &a, const T &b) {     return a = min(a, b);   }   template <class T>   T &chmax(T &a, const T &b) {     return a = max(a, b);   }   const int MAX_N = 256;   int x, n, ip;   long double p;   int a[MAX_N];   long double dp[2][MAX_N][MAX_N];   long double dp2[2][MAX_N];   long double ans;   int main() {     scanf( %d %d %d , &x, &n, &ip);     p = (long double)ip / 100.0;     int nx = x;     int it = 0;     while (x > 0) {       a[it++] = x % 2;       x /= 2;     }     if (nx % 256 == 0) {       for (int i = (8); i < (int)(MAX_N); i++) {         if (a[i] == 1) {           dp2[0][i - 8] = 1.0;           break;         }       }     } else {       for (int i = (8); i < (int)(MAX_N); i++) {         if (a[i] == 0) {           dp[0][i - 8][nx % 256] = 1.0;           break;         }       }     }     for (int i = (0); i < (int)(n); i++) {       for (int j = (0); j < (int)(MAX_N); j++)         for (int k = (0); k < (int)(MAX_N); k++) dp[(i + 1) % 2][j][k] = 0.0;       for (int j = (0); j < (int)(MAX_N); j++) dp2[(i + 1) % 2][j] = 0.0;       for (int j = (0); j < (int)(MAX_N); j++)         for (int k = (0); k < (int)(MAX_N); k++)           if (dp[i % 2][j][k] > EPSLD) {             int nk = k + 1;             if (nk == 256) {               dp2[(i + 1) % 2][j] += dp[i % 2][j][k] * (1.0 - p);             } else {               dp[(i + 1) % 2][j][nk] += dp[i % 2][j][k] * (1.0 - p);             }             if (k / 128 == 1) {               if (k % 128 == 0) {                 dp2[(i + 1) % 2][0] += dp[i % 2][j][k] * p;               } else {                 dp[(i + 1) % 2][j + 1][(k * 2) % 256] += dp[i % 2][j][k] * p;               }             } else {               dp[(i + 1) % 2][0][(k * 2) % 256] += dp[i % 2][j][k] * p;             }           }       for (int j = (0); j < (int)(MAX_N); j++)         if (dp2[i % 2][j] > EPSLD) {           dp[(i + 1) % 2][0][1] += dp2[i % 2][j] * (1.0 - p);           dp2[(i + 1) % 2][j + 1] += dp2[i % 2][j] * p;         }     }     for (int j = (0); j < (int)(MAX_N); j++)       for (int k = (0); k < (int)(MAX_N); k++)         if (dp[n % 2][j][k] > EPSLD) {           int nk = k;           int res = 0;           while (nk % 2 == 0) {             res++;             nk /= 2;           }           ans += dp[n % 2][j][k] * res;         }     for (int j = (0); j < (int)(MAX_N); j++)       if (dp2[n % 2][j] > EPSLD) {         ans += dp2[n % 2][j] * (8 + j);       }     printf( %.18lf n , (double)ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> s;   vector<vector<int> > t;   bool sortFunc(vector<int> a, vector<int> b) {     if (a.size() == 0) return false;     if (b.size() == 0) return true;     return a.size() < b.size();   }   int main() {     cin.sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     s.resize(n + 1);     for (int i = 1; i <= n; i++) cin >> s[i];     vector<bool> was(n + 1, false);     int index = 0;     t.push_back(vector<int>());     for (int i = 1; i <= n; i++) {       if (was[i]) continue;       int ind = i;       while (!was[ind]) {         was[ind] = true;         t[index].push_back(ind);         ind = s[ind];       }       t.push_back(vector<int>());       index++;     }     vector<int> sol(n + 1);     sort(t.begin(), t.end(), sortFunc);     bool poss = true;     for (int i = 0; i < index; i++) {       vector<int> real;       if (t[i].size() % 2 == 1) {         real.resize(t[i].size());         for (int j = 0; j < t[i].size() / 2; j++) {           real[j * 2] = t[i][j];           int b = t[i].size() / 2 + 1 + j;           real[j * 2 + 1] = t[i][b];         }         int b = t[i].size() / 2;         real[t[i].size() - 1] = t[i][b];       } else if (t[i + 1].size() == t[i].size()) {         real.resize(2 * t[i].size());         for (int j = 0; j < t[i].size(); j++) {           real[2 * j] = t[i][j];           real[2 * j + 1] = t[i + 1][j];         }         i++;       } else         poss = false;       for (int j = 0; j < real.size(); j++) {         sol[real[j]] = real[(j + 1) % real.size()];       }     }     if (poss) {       for (int i = 1; i <= n; i++) {         cout << sol[i] <<    ;       }       cout <<   n ;     } else {       cout <<  -1 n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long int digSum(long long int n) {     long long int sum = 0;     while (n != 0) {       sum += n % 10;       n = n / 10;     }     return sum;   }   bool cmp1(int a, int b) { return a < b; }   bool cmp2(int a, int b) { return a > b; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n, k, sum2 = 0, sum1 = 0, x;     cin >> n >> k;     vector<int> v(n), ans;     for (int(i) = 0; (i) < (n); (i)++) {       cin >> v[i];       sum1 += v[i];     }     x = sum1 / k;     bool ok = true;     if (sum1 % k == 0) {       int i = 0;       while (i < n) {         long long int sum2 = 0;         long long int ct = 0;         while (i < n && sum2 < x) {           sum2 += v[i];           i++;           ct++;         }         if (sum2 == x) {           ans.push_back(ct);         } else {           ok = false;           break;         }       }       if (ok) {         cout <<  Yes ;         cout << endl;         for (int i = 0; i < ans.size(); i++) {           cout << ans[i] <<    ;         }       } else {         cout <<  No ;         cout << endl;       }     } else {       cout <<  No ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int p, n;   vector<int> gra[100010];   double dp[100010], sz[100010];   void dfs(int u) {     sz[u] = 1;     int m = gra[u].size();     for (int i = 0; i <= m - 1; i++) {       dfs(gra[u][i]);       sz[u] += sz[gra[u][i]];     }   }   void solve(int u) {     int m = gra[u].size();     for (int i = 0; i <= m - 1; i++) {       dp[gra[u][i]] = dp[u] + 1 + (sz[u] - sz[gra[u][i]] - 1) / 2.0;       solve(gra[u][i]);     }   }   int main() {     dp[1] = 1;     scanf( %d , &n);     for (int i = 2; i <= n; i++) {       scanf( %d , &p);       gra[p].push_back(i);     }     dfs(1);     solve(1);     for (int i = 1; i <= n; i++) printf( %.6f  , dp[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, T, low, mid, high, a[200005], t[200005];     vector<int> ans;     scanf( %d%d , &n, &T);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &a[i], &t[i]);     }     low = 0;     high = n;     while (low <= high) {       mid = (low + high) / 2;       vector<pair<int, int> > v;       int cnt = 0, tot_t = 0, idx = -1;       for (int i = 1; i <= n; i++) {         if (a[i] >= mid) v.push_back(make_pair(t[i], i));       }       sort(v.begin(), v.end());       for (int i = 0; i < v.size(); i++) {         if (tot_t + v[i].first > T) break;         cnt++;         tot_t += v[i].first;         if (cnt == mid) {           idx = i;           break;         }       }       if (cnt >= mid) {         ans.clear();         for (int i = 0; i <= idx; i++) ans.push_back(v[i].second);         low = mid + 1;       } else {         high = mid - 1;       }     }     printf( %d n%d n , ans.size(), ans.size());     for (int i = 0; i < ans.size(); i++) printf( %d  , ans[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target(         avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   long long MOD = 998244353;   long long INF = 1e18;   const int N = 100001;   long long dp[N][101];   long long sm[N];   long long cnt[N][101];   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0),         cout << fixed << setprecision(20);     long long n, k, len;     cin >> n >> k >> len;     if (len == 1) {       cout << 0;       return 0;     }     vector<long long> a(n + 1);     sm[0] = 1;     for (int i = 1; i <= n; i++) {       cin >> a[i];       for (int j = 1; j <= k; j++) {         cnt[i][j] = cnt[i - 1][j] + (a[i] == -1 || a[i] == j);         if (a[i] != -1 && j != a[i]) {           cnt[i][j] = 0;         }       }       if (a[i] != -1) {         dp[i][a[i]] += sm[i - 1];         if (cnt[i][a[i]] >= len) dp[i][a[i]] -= sm[i - len] - dp[i - len][a[i]];       } else {         for (int j = 1; j <= k; j++) {           dp[i][j] += sm[i - 1];           if (cnt[i][j] >= len) dp[i][j] -= sm[i - len] - dp[i - len][j];         }       }       for (int j = 1; j <= k; j++) {         dp[i][j] %= MOD;         dp[i][j] += MOD;         dp[i][j] %= MOD;         sm[i] += dp[i][j];         sm[i] %= MOD;       }     }     cout << sm[n];   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 100005;   int n;   int m;   int cnt[MAX_N];   int a[MAX_N];   int main() {     scanf( %d %d , &m, &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     int diff = 0;     for (int i = 1; i <= n; i++) {       cnt[a[i]]++;       if (cnt[a[i]] == 1) {         diff++;       }       if (diff == m) {         cout << 1;         for (int j = 1; j <= m; j++) {           cnt[j]--;           if (cnt[j] == 0) {             diff--;           }         }       } else {         cout << 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC          <<  ms n ;     long long t;     cin >> t;     while (t--) {       long long n, g = 0, s = 0, b = 0, i, gidx, bidx, cidx;       cin >> n;       int flag = 0;       long long p[n];       for (i = 0; i < n; i++) {         cin >> p[i];       }       long long m = n / 2;       cidx = m;       cidx--;       while (p[cidx + 1] == p[cidx]) {         cidx--;         if (cidx < 4) {           cout <<  0 0 0                 <<   n ;           flag = 1;           break;         }       }       if (flag) continue;       gidx = 1;       while (p[gidx] == p[gidx - 1]) {         gidx++;         if (gidx >= cidx) {           cout <<  0 0 0                 <<   n ;           flag = 1;           break;         }       }       if (flag) continue;       bidx = 2 * gidx + 1;       while (p[bidx] == p[bidx - 1]) {         bidx++;         if (bidx >= cidx) {           cout <<  0 0 0                 <<   n ;           flag = 1;           break;         }       }       if (flag) continue;       g = gidx;       b = bidx - gidx;       s = cidx - bidx + 1;       if (g == 0 || b == 0 || s <= 0 || g >= s)         cout <<  0 0 0               <<   n ;       else         cout << g <<     << b <<     << s <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> graph[2001];   vector<int> level[2001];   int visited[2001];   int a, b;   void dfs(int u, int depth) {     visited[u] = 1;     level[depth].push_back(u);     for (int v : graph[u]) {       if (!visited[v]) {         dfs(v, depth + 1);       }     }   }   int query = 0;   void ask(vector<int>& v) {     cout <<  ?   << v.size() <<    ;     for (int x : v) {       cout << x <<    ;     }     cout <<   n  << flush;     cin >> a >> b;   }   void bs(int l, int r, int ref) {     int curr_node = 0, curr_level = 0;     while (l <= r) {       if (r - l <= 1) {         ask(level[r]);         if (b == ref) {           curr_level = r;           curr_node = a;         } else {           ask(level[l]);           curr_node = a;           curr_level = l;         }         break;       }       int mid = (l + r) / 2;       ask(level[mid]);       if (b == ref) {         l = mid;       } else {         r = mid - 1;       }     }     for (int i = 0; i <= 2000; i++) {       visited[i] = 0;       level[i].clear();     }     dfs(curr_node, 1);     ask(level[ref + 1]);     cout <<  !   << curr_node <<     << a <<   n  << flush;     string s;     cin >> s;   }   int main() {     int t;     cin >> t;     while (t--) {       query = 0;       int n, x, y;       cin >> n;       for (int i = 0; i <= 2000; i++) {         graph[i].clear();         level[i].clear();         visited[i] = 0;       }       for (int i = 0; i < n - 1; i++) {         cin >> x >> y;         graph[x].push_back(y);         graph[y].push_back(x);       }       cout <<  ?   << n <<    ;       for (int i = 1; i <= n; i++) {         cout << i <<    ;       }       cout <<   n  << flush;       cin >> x >> y;       dfs(x, 1);       int len = 0;       for (int i = n; i >= 1; i--) {         if (level[i].size() != 0) {           len = i;           break;         }       }       bs(1, len, y);     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxiz = 1000 * 1000 + 100;   int d[maxiz];   int main() {     int n;     long long A, M = 0;     cin >> n >> A;     for (int i = 0; i < n; i++) {       cin >> d[i];       M = (M + d[i] * 1ll) * 1ll;     }     for (int i = 0; i < n; i++) {       long long maxi, mini;       if (A - M + d[i] > 1)         mini = A - (M - d[i]);       else         mini = 1;       if (d[i] < A - n + 1)         maxi = d[i];       else         maxi = A - n + 1;       cout << d[i] - (maxi - mini + 1) <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   int f[20001][101];   struct node {     int ai, bi;   } a[1001];   bool cmp(node x, node y) { return x.bi > y.bi; }   int main() {     int n, sum1 = 0, sum2 = 0, d = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i].ai);       sum1 += a[i].ai;     }     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i].bi);     }     sort(a + 1, a + n + 1, cmp);     while (sum1 > sum2) sum2 += a[++d].bi;     printf( %d  , d);     memset(f, 128, sizeof(f));     f[0][0] = 0;     for (int i = 1; i <= n; i++) {       for (int j = sum2; j >= a[i].bi; j--) {         for (int k = 1; k <= d; k++) {           f[j][k] = max(f[j][k], f[j - a[i].bi][k - 1] + a[i].ai);         }       }     }     int ans = 0;     for (int i = sum1; i <= sum2; i++) {       ans = max(ans, f[i][d]);     }     printf( %d n , sum1 - ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 110;   const int M = 10010;   int n, m;   char mat[N][M];   int dir[2] = {-1, 1};   long long solve() {     long long ans = 0;     int x = 0, y = 0, d = 1;     bool isLeft = true, isRight = true;     int pos[2] = {0, 0};     while (true) {       if (!isLeft && !isRight) return -1;       if (x == n - 1) return ans;       if (mat[x + 1][y] ==  . ) {         ans++;         x++;         isLeft = isRight = true;         pos[0] = pos[1] = y;       } else if (y == pos[d]) {         int ty = y + dir[d];         ans++;         if (ty < 0 || ty >= m || mat[x][ty] ==  # ) {           if (d == 0)             isLeft = false;           else             isRight = false;           d ^= 1;         } else {           pos[d] += dir[d];           if (mat[x][ty] ==  . )             y += dir[d];           else             d ^= 1;         }       } else {         ans += (long long)abs((long long)pos[d] - (long long)y);         y = pos[d];       }     }     return -1;   }   int main() {     while (scanf( %d%d%*c , &n, &m) != EOF) {       for (int i = 0; i < n; i++) gets(mat[i]);       long long ans = solve();       if (ans == -1)         puts( Never );       else         cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t[1000005], r = 1000004;   int dp[1000005];   int main() {     memset(dp, 0, sizeof(dp));     memset(t, 0, sizeof(t));     int n, ans = 0, x;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x;       cin >> t[x];     }     dp[0] = 0;     if (t[0] != 0) dp[0] = 1;     for (int i = 1; i <= r; i++) {       if (!t[i])         dp[i] = dp[i - 1];       else {         if (t[i] >= i)           dp[i] = 1;         else           dp[i] = dp[i - t[i] - 1] + 1;       }       ans = max(ans, dp[i]);     }     cout << n - ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int N = 2e5 + 5;   int n, q;   ll a[N], tree[N << 2], lz[N << 2];   void push(int node, int s, int e) {     tree[node] += lz[node];     if (s != e) {       lz[node << 1] += lz[node];       lz[node << 1 | 1] += lz[node];     }     lz[node] = 0;   }   void upd(int node, int s, int e, int l, int r, ll v) {     push(node, s, e);     if (r < s || e < l || l > r) return;     if (l <= s && e <= r) {       lz[node] += v;       push(node, s, e);       return;     }     int md = (s + e) >> 1;     upd(node << 1, s, md, l, r, v);     upd(node << 1 | 1, md + 1, e, l, r, v);     tree[node] = max(tree[node << 1], tree[node << 1 | 1]);   }   int get(int node, int s, int e) {     push(node, s, e);     if (tree[node] < 0) return -1;     if (s == e) return tree[node] != 0 ? -1 : s + 1;     int md = (s + e) >> 1;     int x = get(node << 1, s, md);     if (~x) return x;     return get(node << 1 | 1, md + 1, e);   }   int main() {     scanf( %d%d , &n, &q);     for (int i = 0; i < n; i++) {       scanf( %lld , a + i);       upd(1, 0, n - 1, i, i, a[i]);       upd(1, 0, n - 1, i + 1, n - 1, -a[i]);     }     while (q--) {       int x, v;       scanf( %d%d , &x, &v);       upd(1, 0, n - 1, x - 1, x - 1, -a[x - 1] + v);       upd(1, 0, n - 1, x, n - 1, a[x - 1] - v);       a[x - 1] = v;       printf( %d n , get(1, 0, n - 1));     }     return 0;   }


#include <bits/stdc++.h>   const double pi = acos(-1.0), eps = 1e-9;   const int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1};   const int dy[8] = {0, 0, 1, -1, 1, -1, -1, 1};   const int MO = (int)(1e9 + 7);   using namespace std;   int main() {     long long n;     cin >> n;     while (n % 3 == 0) n /= 3;     cout << n / 3 + 1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   void fileio() {}   vector<vector<long long int>> g;   long long int cube_root(long long int x) {     long long int lo = 0, hi = 1e6 + 10;     while (lo <= hi) {       long long int m = (lo + hi) / 2;       long long int val = m * m * m;       if (val == x) return m;       if (val > x) {         hi = m - 1;       } else {         lo = m + 1;       }     }     return -1;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     fileio();     long long int t;     cin >> t;     while (t-- > 0) {       long long int a, b;       cin >> a >> b;       long long int x = a * b;       long long int c = cube_root(x);       if (c == -1) {         cout <<  NO ;         cout <<   n ;         continue;       }       if (a % c != 0 or b % c != 0) {         cout <<  NO ;         cout <<   n ;         continue;       }       cout <<  YES ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int vis[51];   int main() {     string s;     int n, len;     cin >> len >> n;     cin >> s;     while (n--) {       for (int i = 0; i <= len; i++) {         if (s[i] ==  B  && s[i + 1] ==  G ) {           swap(s[i], s[i + 1]);           i++;         }       }     }     cout << s << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N;     cin >> N;     int a[10000], b[10000];     for (int n = 0; n < N; n++) {       cout <<  ?   << 0 <<     << n << endl;       cin >> a[n];     }     for (int n = 0; n < N; n++) {       cout <<  ?   << n <<     << 0 << endl;       cin >> b[n];     }     int nm = 0, fa = 0;     int ac[10000], bc[10000];     for (int n = 0; n < N; n++) {       for (int x = 0; x < N; x++) {         ac[x] = a[x] ^ n;       }       for (int x = 0; x < N; x++) {         bc[x] = b[x] ^ ac[0];       }       bool p = true;       for (int x = 0; x < N; x++) {         if (ac[bc[x]] != x) {           p = false;         }       }       if (p) {         fa = n;         nm++;       }     }     cout <<  !  << endl;     cout << nm << endl;     fa = fa ^ a[0];     for (int x = 0; x < N; x++) {       if (x) cout <<    ;       cout << (fa ^ b[x]);     }     cout << endl;     return 0;   }