#include <bits/stdc++.h>   using namespace std;   struct edge_t {     int u, dist;   };   struct firstk_sum_t {     multiset<long long> left, right;     long long left_sum = 0LL;     void insert(long long value) {       if (!left.empty() && value <= *left.rbegin()) {         left.insert(value);         left_sum += value;       } else {         right.insert(value);       }     }     void erase(long long value) {       if (!left.empty() && value <= *left.rbegin()) {         auto it = left.find(value);         if (it != left.end()) {           left_sum -= *it;           left.erase(it);         }       } else {         auto it = right.find(value);         if (it != right.end()) right.erase(it);       }     }     long long get_sum(int k) {       while (!left.empty() && (int)left.size() > k) {         auto it = left.end();         --it;         right.insert(*it);         left_sum -= *it;         left.erase(it);       }       while (!right.empty() && (int)left.size() < k) {         auto it = right.begin();         left.insert(*it);         left_sum += *it;         right.erase(it);       }       return left_sum;     }   };   struct res_t {     int parent_cost;     vector<long long> with;     vector<long long> without_override;     res_t *dup_prefix(int size) {       res_t *result = new res_t;       result->parent_cost = parent_cost;       result->with = vector<long long>(           with.begin(), with.begin() + min(size, (int)with.size()));       result->without_override = vector<long long>(           without_override.begin(),           without_override.begin() + min(size, (int)without_override.size()));       return result;     }     void set_without(int i, long long value) {       int new_size = i + 1;       without_override.reserve(new_size);       while ((int)without_override.size() < new_size) {         without_override.push_back(with[without_override.size()]);       }       without_override[i] = value;     }     long long get_with(int i) {       if (i < (int)with.size())         return with[i];       else         return 0LL;     }     long long get_without(int i) {       if (i < (int)without_override.size())         return without_override[i];       else if (i < (int)with.size())         return with[i];       else         return 0LL;     }   };   res_t *solve(int v, int p, int pc, const vector<vector<edge_t>> &adj) {     vector<res_t *> children;     for (const edge_t &ed : adj[v])       if (ed.u != p) children.push_back(solve(ed.u, v, ed.dist, adj));     if (children.empty()) {       res_t *result = new res_t;       result->parent_cost = pc;       result->with = {LLONG_MAX};       result->without_override = {0LL};       return result;     } else {       sort(children.begin(), children.end(),            [](const res_t *lhs, const res_t *rhs) {              return lhs->with.size() > rhs->with.size();            });       int compute_until = (int)children.size() + 2;       if ((int)children.size() >= 2)         compute_until = max(compute_until, (int)children[1]->with.size());       res_t *children0start = children[0]->dup_prefix(compute_until);       res_t *result = children[0];       children[0] = children0start;       firstk_sum_t first_k;       vector<long long> children_value(children.size());       int good_to_remove = 0;       for (int i = 0; i < (int)children.size(); ++i) {         children_value[i] = children[i]->parent_cost;         good_to_remove += (children_value[i] < 0);         first_k.insert(children_value[i]);       }       result->with.resize(max((int)result->with.size(), compute_until));       result->with[0] = LLONG_MAX;       for (int j = compute_until - 1; j >= 1; --j) {         result->with[j] = 0LL;         for (int i = 0; i < (int)children.size(); ++i) {           if ((int)children[i]->with.size() <= j) break;           result->with[j] += children[i]->get_with(j);         }         int max_children = j - 1;         int nremove = (int)children.size() - max_children;         for (int i = 0; i < (int)children.size(); ++i) {           if ((int)children[i]->with.size() <= j) break;           first_k.erase(children_value[i]);           good_to_remove -= (children_value[i] < 0);           children_value[i] = children[i]->get_without(j) -                               children[i]->get_with(j) + children[i]->parent_cost;           good_to_remove += (children_value[i] < 0);           first_k.insert(children_value[i]);         }         nremove = max(nremove, good_to_remove);         long long total_cost = first_k.get_sum(nremove);         result->with[j] += total_cost;       }       result->without_override.clear();       first_k.left.clear();       first_k.right.clear();       first_k.left_sum = 0LL;       good_to_remove = 0;       for (int i = 0; i < (int)children.size(); ++i) {         children_value[i] = children[i]->parent_cost;         good_to_remove += (children_value[i] < 0);         first_k.insert(children_value[i]);       }       long long without0 = 0LL;       for (int i = 0; i < (int)children.size(); ++i) {         without0 += children[i]->get_without(0);         without0 += children[i]->parent_cost;       }       result->set_without(0, without0);       for (int j = compute_until - 1; j >= 1; --j) {         long long children_cost = 0LL;         for (int i = 0; i < (int)children.size(); ++i) {           if ((int)children[i]->with.size() <= j) break;           children_cost += children[i]->get_with(j);         }         int max_children = j;         int nremove = (int)children.size() - max_children;         for (int i = 0; i < (int)children.size(); ++i) {           if ((int)children[i]->with.size() <= j) break;           first_k.erase(children_value[i]);           good_to_remove -= (children_value[i] < 0);           children_value[i] = children[i]->get_without(j) -                               children[i]->get_with(j) + children[i]->parent_cost;           good_to_remove += (children_value[i] < 0);           first_k.insert(children_value[i]);         }         nremove = max(nremove, good_to_remove);         long long total_cost = first_k.get_sum(nremove);         result->set_without(j, children_cost + total_cost);       }       if (pc != INT_MAX) {         for (int j = 1; j < compute_until; ++j) {           result->with[j] =               min(result->with[j], result->without_override[j] + pc);         }       }       result->parent_cost = pc;       return result;     }   }   int main() {     int n;     scanf( %d , &n);     vector<vector<edge_t>> adj(n);     for (int i = 0; i < n - 1; ++i) {       int a, b, c;       scanf( %d %d %d , &a, &b, &c), --a, --b;       adj[a].push_back({b, c});       adj[b].push_back({a, c});     }     res_t *result = solve(0, -1, INT_MAX, adj);     for (int i = 0; i < n; ++i) {       if (i > 0) printf(   );       printf( %lld , result->get_without(i));     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 233;   int a[N][N];   int main() {     ios::sync_with_stdio(false);     int m, n, q, p, x, y, b;     cin >> n >> m;     for (int i = 0; i < n; ++i) cin >> a[i][0];     p = a[0][0];     for (int i = 0; i < m; ++i) cin >> a[0][i];     q = a[0][0];     x = y = b = 0;     for (int i = 1; i < n; ++i) x ^= a[i][0];     for (int i = 1; i < m; ++i) y ^= a[0][i];     if ((y ^ p) != (x ^ q)) {       printf( NO n );       return 0;     } else {       b = y ^ p;       a[0][0] = b;       printf( YES n );       for (int i = 0; i < n; ++i) {         for (int j = 0; j < m; ++j) printf( %d  , a[i][j]);         printf(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   void Letter() {     char str[100000 + 1];     int Left[100000 + 1], Right[100000 + 1];     int i, j;     int Len;     scanf( %s , str);     Len = strlen(str);     Left[0] = Right[Len - 1] = 0;     for (i = 1; i < Len; i++)       if (str[i - 1] <=  z  && str[i - 1] >=  a )         Left[i] = Left[i - 1] + 1;       else         Left[i] = Left[i - 1];     for (i = Len - 2; i >= 0; i--)       if (str[i + 1] <=  Z  && str[i + 1] >=  A )         Right[i] = Right[i + 1] + 1;       else         Right[i] = Right[i + 1];     int min = INT_MAX;     for (i = 0; i < Len; i++)       if (min > Left[i] + Right[i]) min = Left[i] + Right[i];     printf( %d n , min);   }   int main() {     Letter();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, cnt;   int po[999994], cr[14];   map<int, int> mert;   void calc(int L) {     int x = 0;     for (int i = 1; i <= L; i++) x = x * 10 + cr[i];     if (x == 0)       return;     else if (!mert.count(x)) {       mert[x] = 1;       po[++cnt] = x;     }   }   void dfs(int cur, int limit) {     if (cur > limit)       calc(limit);     else       for (int i = 0; i <= 1; i++) {         cr[cur] = i;         dfs(cur + 1, limit);       }   }   int main() {     cin >> n;     for (int i = 1; i <= 10; i++) dfs(1, i);     sort(po + 1, po + cnt + 1);     int i = 0;     for (i = 1; po[i] <= n; i++)       ;     cout << i - 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, count(0);     cin >> n >> m;     for (int a = 0; a * a <= n; a++) {       for (int b = 0; b * b <= m; b++) {         if (a * a + b == n && a + b * b == m) count++;       }     }     cout << count << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( inline )   using namespace std;   const int N = 405;   const int MOD = 1000000007;   int T, lim, n, k, l, p, cnt, id[N];   long long C[N][N], a[N], dp[N][N], fac[N] = {1};   vector<int> G[N];   bool Judge(long long x) {     long long k = sqrt(x);     return k * k == x;   }   void init() {     C[0][0] = 1LL;     for (int i = 1; i <= 350; i++) {       C[i][0] = C[i][i] = 1;       for (int j = 1; j < i; j++) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;       fac[i] = fac[i - 1] * i % MOD;     }     id[1] = cnt = 1, G[1].push_back(1);     for (int i = 2; i <= n; i++) {       bool flag = true;       for (int j = i - 1; j >= 1; j--)         if (Judge(a[j] * a[i])) {           G[id[j]].push_back(i);           id[i] = id[j];           flag = false;           break;         }       if (flag) {         id[i] = ++cnt;         G[cnt].push_back(i);       }     }   }   signed main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %lld , a + i);     init();     dp[1][(int)G[1].size() - 1] = 1;     lim = (int)G[1].size();     for (int i = 2; i <= cnt; i++) {       int sz = G[i].size();       for (int k = 0; k < sz; k++)         for (int j = 0; j < lim; j++)           for (int p = 0; p <= min(j, k + 1); p++) {             dp[i][j + sz - k - 1 - p] += dp[i - 1][j] * C[sz - 1][k] % MOD *                                          C[j][p] % MOD *                                          C[lim + 1 - j][k + 1 - p] % MOD;             dp[i][j + sz - k - 1 - p] %= MOD;           }       lim += sz;     }     long long res = dp[cnt][0];     for (int i = 1; i <= cnt; i++) res = res * fac[(int)G[i].size()] % MOD;     printf( %lld n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(false);     ;     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       vector<set<long long>> lol(n - 1);       long long cnt = 0;       long long x;       for (long long i = 0; i < n - 1; i++) {         cin >> cnt;         for (long long j = 0; j < cnt; j++) {           cin >> x;           lol[i].insert(x);         }       }       for (long long i = 1; i <= n; i++) {         bool ok = 1;         vector<set<long long>> st(lol.begin(), lol.end());         long long need = i;         for (auto &z : st) {           z.erase(need);         }         vector<long long> ans;         ans.emplace_back(i);         for (long long j = 1; j < n; j++) {           long long cur = -1;           long long how = 0;           for (auto &z : st) {             if (z.size() == 1) {               cur = (*z.begin());               how++;             }           }           if (how != 1) {             ok = 0;             break;           }           ans.emplace_back(cur);           for (auto &z : st) z.erase(cur);         }         if (!ok) continue;         set<set<long long>> kavo(lol.begin(), lol.end());         for (long long k = 2; k < n; k++) {           set<long long> now;           now.insert(ans[k]);           bool is = 0;           for (long long j = k - 1; j >= 0; j--) {             now.insert(ans[j]);             if (kavo.count(now)) {               kavo.erase(now);               is = 1;               break;             }           }           if (!is) {             ok = 0;             break;           }         }         if (ok) {           for (auto z : ans) {             cout << z <<    ;           }           cout <<   n ;           break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long const lim = 1e5 + 5;   long long const mod = 1000000007;   vector<long long> adj[lim + 5];   long long vis[lim + 5], n;   vector<long long> count_edge;   long long child[lim + 5];   void all_clear() {     count_edge.clear();     for (long long i = 1; i <= lim; i++) {       adj[i].clear();       vis[i] = -1;       child[i] = 0;     }   }   void dfs(long long s) {     vis[s] = 1;     for (auto v : adj[s]) {       if (vis[v] == -1) {         dfs(v);         child[s] += child[v];         count_edge.push_back(child[v] * (n - child[v]));       }     }     child[s] += 1;   }   void solve() {     cin >> n;     all_clear();     for (int i = 1; i < n; i++) {       long long a, b;       cin >> a >> b;       adj[a].push_back(b), adj[b].push_back(a);     }     long long m;     cin >> m;     vector<long long> fact;     for (int i = 0; i < m; i++) {       long long p;       cin >> p;       fact.push_back(p);     }     dfs(1);     sort(count_edge.rbegin(), count_edge.rend());     sort(fact.rbegin(), fact.rend());     for (int i = 0; i < count_edge.size(); i++) count_edge[i] %= mod;     long long ans = 0;     if (fact.size() > count_edge.size()) {       while (fact.size() > count_edge.size()) {         long long val = fact[0];         fact.erase(fact.begin());         fact[0] = (fact[0] * val) % mod;       }       for (int i = 0; i < fact.size(); i++) {         ans = (ans + (fact[i] * count_edge[i]) % mod) % mod;       }     } else {       for (int i = 0; i < count_edge.size(); i++) {         if (i >= fact.size()) {           ans = (ans + count_edge[i]) % mod;         } else {           ans = (ans + (fact[i] * count_edge[i]) % mod) % mod;         }       }     }     cout << ans << endl;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     int t = 1, no = 0;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, c, d, cnt1, cnt2;     cin >> n;     pair<int, int> date[n];     for (i = 0; i < n; i++) {       cin >> date[i].first >> date[i].second;     }     sort(date, date + n);     c = date[0].first;     d = date[0].second;     if (c <= d)       cnt1 = c;     else       cnt1 = d;     for (i = 0; i < n; i++) {       c = date[i].first;       d = date[i].second;       if (c <= d)         cnt2 = c;       else         cnt2 = d;       if (cnt1 <= cnt2)         cnt1 = cnt2;       else         cnt1 = c;     }     cout << cnt1;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   long long v[100100];   vector<int> pos, neg;   vector<int> X, Y;   vector<long long> V;   int main() {     scanf( %d %d , &n, &m);     int x, y;     long long z;     memset(v, 0, sizeof(v));     for (int i = 0; i < m; i++) {       scanf( %d %d %I64d , &x, &y, &z);       v[x] += z;       v[y] -= z;     }     for (int i = 1; i <= n; i++) {       if (v[i] > 0) pos.push_back(i);       if (v[i] < 0) neg.push_back(i);     }     int i = 0, j = 0;     while (i < pos.size() && j < neg.size()) {       long long tem = v[pos[i]] > -v[neg[j]] ? -v[neg[j]] : v[pos[i]];       v[pos[i]] -= tem;       v[neg[j]] += tem;       X.push_back(pos[i]);       Y.push_back(neg[j]);       V.push_back(tem);       if (v[pos[i]] <= 0) i++;       if (v[neg[j]] >= 0) j++;     }     printf( %d n , X.size());     for (int i = 0; i < X.size(); i++) {       cout << X[i] <<     << Y[i] <<     << V[i] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, p;   long long int a[100001];   int is_good(long long int q) {     int b[n];     for (int i = n - 1; i >= 0; i--) {       long long int l = max(a[i] - q, 0ll);       b[i] = n - l - (n - 1 - i);       if (b[i] % p == 0) return 0;     }     return 1;   }   int main() {     cin >> n >> p;     long long int maxx = 0;     for (int i = 0; i < n; ++i) {       cin >> a[i];       maxx = max(maxx, a[i]);     }     sort(a, a + n);     long long int t1 = 0;     for (int i = 0; i < n; i++) {       t1 = max(t1, a[i] - i);     }     long long int l = t1, r = 1000000001;     while (r > l) {       long long int mid = (r + l + 1) / 2;       if (is_good(mid)) {         l = mid;       } else {         r = mid - 1;       }     }     if (l == t1 && !is_good(l)) {       cout <<  0             <<   n ;       cout <<   n ;       exit(0);     }     cout << (l - t1 + 1) <<   n ;     for (long long int i = t1; i <= l; i++) {       cout << i <<    ;     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   char ss[1002][1002];   int ak = -1, p = 0;   int main() {     while (gets(ss[p])) {       if ((int)strlen(ss[p]) > ak) ak = (int)strlen(ss[p]);       p++;     }     for (int i = 0; i <= ak + 1; i++) printf( * );     printf(  n );     bool k = false;     for (int i = 0; i < p; i++) {       int len = strlen(ss[i]);       if ((ak - len) % 2 != 0) k = !k;       printf( * );       for (int j = 0; j < (ak - len + (!k)) / 2; j++) printf(   );       printf( %s , ss[i]);       for (int j = 0; j < (ak - len + (k)) / 2; j++) printf(   );       printf( * n );     }     for (int i = 0; i <= ak + 1; i++) printf( * );     printf(  n );     return 0;   }


#include <bits/stdc++.h>   const int N = 30;   int a[N];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);     }     int max = 0, sit;     for (int i = 1; i <= 100000; ++i) {       bool div = true;       for (int j = 0; j < n; ++j) {         if (a[j] % i) {           div = false;           break;         }       }       if (!div) continue;       int oddcnt = 0;       for (int j = 0; j < n; ++j) {         oddcnt += a[j] / i & 1;       }       if (oddcnt > 1) continue;       int value = i * (oddcnt ? 1 : 2);       if (max < value) {         max = value;         sit = i;       }     }     printf( %d n , max);     if (!max) {       for (int i = 0; i < n; ++i) {         for (int j = 0; j < a[i]; ++j) {           putchar( a  + i);         }       }       putchar(  n );       return 0;     }     for (int i = 0; i < n; ++i) {       a[i] /= sit;     }     for (int times = 0; times < sit; ++times) {       for (int i = 0; i < n; ++i) {         for (int j = 0; j < a[i] >> 1; ++j) {           putchar( a  + i);         }       }       for (int i = 0; i < n; ++i) {         if (a[i] & 1) {           putchar( a  + i);         }       }       for (int i = n - 1; i >= 0; --i) {         for (int j = 0; j < a[i] >> 1; ++j) {           putchar( a  + i);         }       }     }     putchar(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 9;   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, s;     cin >> n >> s;     if (s < 2 * n) return cout <<  NO n , 0;     cout <<  YES n ;     for (int i = 1; i < n; i++) cout << 2 <<    ;     cout << s - 2 * (n - 1) <<   n ;     cout << 1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double eps = 1e-9;   pair<int, int> get_closer(int a, vector<int> &v) {     auto it = lower_bound(v.begin(), v.end(), a);     int b = 2000000000, c = 2000000000;     if (it != v.end()) b = *it;     if (it != v.begin()) c = *(it - 1);     return {c, b};   }   unsigned long long int count(int a, int b, int c) {     return (a - b) * 1LL * (a - b) + (a - c) * 1LL * (a - c) +            (b - c) * 1LL * (b - c);   }   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n1, n2, n3;       scanf( %d %d %d , &n1, &n2, &n3);       vector<int> r(n1), g(n2), b(n3);       for (int i = 0; i < n1; i++) scanf( %d , &r[i]);       for (int i = 0; i < n2; i++) scanf( %d , &g[i]);       for (int i = 0; i < n3; i++) scanf( %d , &b[i]);       sort(r.begin(), r.end());       sort(g.begin(), g.end());       sort(b.begin(), b.end());       unsigned long long int mn = count(r[0], b[0], g[0]);       for (int i = 0; i < n1; i++) {         int a = r[i];         pair<int, int> p1 = get_closer(a, g);         pair<int, int> p2 = get_closer(a, b);         pair<int, int> p3 = get_closer(p1.first, b);         pair<int, int> p4 = get_closer(p1.second, b);         mn = min(mn, count(a, p1.first, p2.first));         mn = min(mn, count(a, p1.second, p2.first));         mn = min(mn, count(a, p1.first, p2.second));         mn = min(mn, count(a, p1.second, p2.second));         mn = min(mn, count(a, p1.first, p3.first));         mn = min(mn, count(a, p1.first, p3.second));         mn = min(mn, count(a, p1.second, p4.first));         mn = min(mn, count(a, p1.second, p4.second));       }       for (int i = 0; i < n2; i++) {         int a = g[i];         pair<int, int> p1 = get_closer(a, r);         pair<int, int> p2 = get_closer(a, b);         pair<int, int> p3 = get_closer(p1.first, b);         pair<int, int> p4 = get_closer(p1.second, b);         mn = min(mn, count(a, p1.first, p2.first));         mn = min(mn, count(a, p1.second, p2.first));         mn = min(mn, count(a, p1.first, p2.second));         mn = min(mn, count(a, p1.second, p2.second));         mn = min(mn, count(a, p1.first, p3.first));         mn = min(mn, count(a, p1.first, p3.second));         mn = min(mn, count(a, p1.second, p4.first));         mn = min(mn, count(a, p1.second, p4.second));       }       for (int i = 0; i < n3; i++) {         int a = b[i];         pair<int, int> p1 = get_closer(a, g);         pair<int, int> p2 = get_closer(a, r);         pair<int, int> p3 = get_closer(p1.first, r);         pair<int, int> p4 = get_closer(p1.second, r);         mn = min(mn, count(a, p1.first, p2.first));         mn = min(mn, count(a, p1.second, p2.first));         mn = min(mn, count(a, p1.first, p2.second));         mn = min(mn, count(a, p1.second, p2.second));         mn = min(mn, count(a, p1.first, p3.first));         mn = min(mn, count(a, p1.first, p3.second));         mn = min(mn, count(a, p1.second, p4.first));         mn = min(mn, count(a, p1.second, p4.second));       }       cout << mn <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int n;     int a[10000];     while (scanf( %d , &n) != EOF) {       for (int i = 1; i <= n; i++) scanf( %d , &a[i]);       for (int i = 1; i <= n; i++) {         if (a[i] % 2 == 0) a[i] -= 1;       }       for (int i = 1; i <= n - 1; i++) printf( %d  , a[i]);       printf( %d n , a[n]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     map<char, int> mp;     mp.insert(pair<char, int>( A , 0));     mp.insert(pair<char, int>( B , 0));     mp.insert(pair<char, int>( C , 0));     int i = 3;     while (i--) {       string s;       cin >> s;       if (s[1] ==  > )         mp[s[0]]++;       else         mp[s[2]]++;     }     if (mp[ A ] == mp[ B ] && mp[ C ] == mp[ B ])       cout <<  Impossible ;     else {       if (mp[ A ] == 0)         cout <<  A ;       else if (mp[ B ] == 0)         cout <<  B ;       else if (mp[ C ] == 0)         cout <<  C ;       if (mp[ A ] == 1)         cout <<  A ;       else if (mp[ B ] == 1)         cout <<  B ;       else if (mp[ C ] == 1)         cout <<  C ;       if (mp[ A ] == 2)         cout <<  A ;       else if (mp[ B ] == 2)         cout <<  B ;       else if (mp[ C ] == 2)         cout <<  C ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long GCD(long long u, long long v) {     long long r;     while (v) {       r = u % v;       u = v;       v = r;     }     return u;   }   long long modpow(long long a, long long p, long long mod) {     long long ret = 1;     while (p) {       if (p & 1) ret = (ret * a) % mod;       a = (a * a) % mod;       p /= 2;     }     return ret;   }   long long power(long long a, long long p) {     long long ret = 1;     while (p) {       if (p & 1) ret = (ret * a);       a = (a * a);       p /= 2;     }     return ret;   }   int a[51];   int main() {     int i, j, tmp;     ios_base::sync_with_stdio(0);     cin.tie(NULL);     int n, k;     cin >> n >> k;     for (i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     if (k > n)       cout << -1 << endl;     else       cout << a[n - k] <<     << a[n - k] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     if (n < 4) {       cout <<  NO n ;       return 0;     }     cout <<  YES n ;     if (n >= 4 && !(n & 1)) {       int ones = 0;       for (int i = 5; i < n; i += 2) {         printf( %d - %d = 1 n , i + 1, i);         ones++;       }       cout <<  1 * 4 = 4 n ;       cout <<  4 * 2 = 8 n ;       cout <<  8 * 3 = 24 n ;       while (ones > 0) {         cout <<  24 * 1 = 24 n ;         ones--;       }       return 0;     }     if (n > 4 && n & 1) {       cout <<  5 - 1 = 4 n ;       cout <<  4 - 2 = 2 n ;       cout <<  4 * 2 = 8 n ;       cout <<  8 * 3 = 24 n ;       int ones = 0;       for (int i = 6; i < n; i += 2) {         printf( %d - %d = 1 n , i + 1, i);         ones++;       }       while (ones > 0) {         cout <<  24 * 1 = 24 n ;         ones--;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, ai, m, xi, yi;     cin >> n;     vector<int> wire(100);     for (int i = 1; i <= n; i++) {       cin >> wire[i];     }     cin >> m;     for (int i = 1; i <= m; i++) {       cin >> xi >> yi;       wire[xi - 1] += (yi - 1);       wire[xi + 1] += (wire[xi] - yi);       wire[xi] = 0;     }     for (int i = 1; i <= n; i++) {       cout << wire[i] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e6 + 3;   const int INFI = 1e9 * 2;   const long long LINFI = 1e17;   const double pi = acos(-1.0);   const double s2 = sqrt(2.0);   const int N = 222;   const int M = 11;   const int move[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1};   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   int main() {     int a, b, c, d, p, q, m, x, y;     while (scanf( %d%d%d%d , &a, &b, &c, &d) != EOF) {       m = c * d / gcd(c, d);       a *= m;       b *= m;       x = a / c;       y = b / d;       if (x >= y) {         q = a;         p = a - y * c;       } else {         q = b;         p = b - x * d;       }       m = gcd(p, q);       q /= m;       p /= m;       printf( %d/%d n , p, q);     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int t, n, *arr, i, a, odd, even;     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       arr = (int*)malloc(n * sizeof(int));       for (i = 0; i < n; i++) scanf( %d , &arr[i]);       even = 0;       odd = 0;       for (i = 0; i < n; i++) {         if (arr[i] % 2 == 0)           even++;         else           odd++;       }       a = 0;       if (n % 2 == 0) {         if (even == odd) {           for (i = 0; i < n; i++) {             if ((i % 2) != (arr[i] % 2)) a++;           }           printf( %d n , a / 2);         } else           printf( %d n , -1);       } else {         if (odd == (even - 1)) {           for (i = 0; i < n; i++) {             if ((i % 2) != (arr[i] % 2)) a++;           }           printf( %d n , a / 2);         } else           printf( %d n , -1);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, c = 0, temp;     int h[101], g[101];     cin >> n;     for (int i = 0; i < n; i++) {       cin >> h[i] >> g[i];     }     for (int j = 0; j < n; j++) {       temp = n - 1;       while (temp > j) {         if (h[j] == g[temp]) c++;         if (h[temp] == g[j]) c++;         temp--;       }     }     cout << c;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t,n;   int a[100010];   int main(){    cin>>t;    while(t--){     cin>>n;     for(int i=0;i<n;++i)cin>>a[i];     sort(a,a+n);     long long ans=a[n-1],sum=0;     for(int i=0;i<n;++i){      sum+=a[i];      ans+=sum-1ll*a[i]*(i+1);     }     cout<<ans<<  n ;    }   }


#include <bits/stdc++.h>   long int temp, i, j, k, T;   long int ABS(int test) {     if (test < 0)       return test * -1;     else       return test;   }   int x8[] = {1, 1, 0, -1, -1, -1, 0, 1};   int y8[] = {0, 1, 1, 1, 0, -1, -1, -1};   int x4[] = {1, 0, -1, 0};   int y4[] = {0, 1, 0, -1};   long int arr[1000000];   long int buff[1000000];   long int memo[1000000];   using namespace std;   int main() {     char a[100001];     char b[100001];     int jawaban = 0, panjang, tujuh1 = 0, tujuh2 = 0, empat1 = 0, empat2 = 0;     memset(memo, 0, sizeof memo);     cin >> a;     cin >> b;     panjang = strlen(a);     for (i = 0; i < panjang; i++) {       if (a[i] ==  7 ) tujuh1++;       if (a[i] ==  4 ) empat1++;       if (b[i] ==  7 ) tujuh2++;       if (b[i] ==  4 ) empat2++;     }     for (i = 0; i < panjang; i++) {       if (a[i] == b[i]) {         if (a[i] ==  7 ) {           tujuh1--;           tujuh2--;         }         if (a[i] ==  4 ) {           empat1--;           empat2--;         }       }     }     jawaban = max(tujuh1, tujuh2);     cout << jawaban << endl;     {       fflush(stdin);       getchar();     };     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, t, i;     cin >> t;     for (i = 0; i < t; i++) {       cin >> a;       cout << a / 2 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     queue<unsigned long long> q;     vector<unsigned long long> v;     unsigned long long temp;     q.push(0);     while (!q.empty()) {       temp = q.front();       v.push_back(temp * 10 + 4);       v.push_back(temp * 10 + 7);       if (temp * 10 + 7 >= 4444444444) break;       q.pop();       q.push(temp * 10 + 4);       q.push(temp * 10 + 7);     }     unsigned long long left, right, sum = 0;     cin >> left >> right;     int i = 0;     for (i = 0; i < v.size(); i++)       if (v[i] >= left) break;     while (left <= right) {       sum = sum + v[i] * (min(v[i], right) - left + 1);       left = v[i] + 1;       i++;     }     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, b;   struct chel {     int cost;     int mas;     int coun;   };   chel f[103];   chel tem[103];   void mergesort(chel *a, chel *b, int n) {     int i, i0, i1, iRight, iEnd, width, j;     for (width = 1; width < n; width *= 2)       for (i = 0; i < n; i += width * 2) {         i0 = i;         i1 = iRight = min(i + width, n);         iEnd = min(i + width * 2, n);         for (j = i; j < iEnd; j++)           if (i0 < iRight && (i1 == iEnd || a[i0].coun < a[i1].coun))             b[j] = a[i0++];           else             b[j] = a[i1++];         for (j = i; j < iEnd; j++) a[j] = b[j];       }   }   long long dp[(1 << 20)];   int main() {     int i, j;     scanf( %d %d %d , &n, &m, &b);     dp[0] = 0;     for (i = 1; i < (1 << m); i++) dp[i] = (long long)1000000000 * 1000000000 * 2;     long long ans = (long long)1000000000 * 1000000000 * 2;     int x, t;     for (i = 0; i < n; i++) {       scanf( %d %d %d , &f[i].cost, &f[i].coun, &t);       for (j = 0; j < t; j++) {         scanf( %d , &x);         f[i].mas |= (1 << (x - 1));       }     }     mergesort(f, tem, n);     for (i = 0; i < n; i++) {       for (j = 0; j < (1 << m); j++)         dp[(j | f[i].mas)] = min(dp[(j | f[i].mas)], dp[j] + f[i].cost);       ans = min(ans, dp[(1 << m) - 1] + (long long)b * f[i].coun);     }     if (ans == (long long)1000000000 * 1000000000 * 2)       printf( -1 n );     else       printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int md = 1e9 + 7;   long long int __gcd(long long int a, long long int b) {     if (b == 0) return a;     return __gcd(b, a % b);   }   long long int poww(long long int a, long long int b) {     long long int res = 1;     while (b) {       if (b & 1) {         res *= a;         res %= md;       }       a *= a;       a %= md;       b >>= 1;     }     return res;   }   void ainp(long long int arr[], long long int n) {     for (long long int i = 1; i <= n; i++) cin >> arr[i];   }   const long long int N = 2e5 + 5;   void solve() {     long long int n;     cin >> n;     long long int t = n;     vector<long long int> bits;     while (n) {       bits.push_back(n % 3);       n /= 3;     }     bits.push_back(0);     long long int cnt2 = -1;     for (long long int i = 0; i < bits.size() - 1; i++) {       if (bits[i] == 2) {         cnt2 = i;       }     }     if (cnt2 == -1)       cout << t <<   n ;     else {       for (long long int i = 0; i < cnt2; i++) {         bits[i] = 0;       }       for (long long int i = cnt2; i < bits.size() - 1; i++) {         if (bits[i] == 2) {           bits[i] = 0;           bits[i + 1] += 1;         }       }       long long int ans = 0;       for (long long int i = 0; i < bits.size(); i++) {         ans += bits[i] * (long long int)pow(3, i);       }       cout << ans <<   n ;     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int t;     cin >> t;     while (t-- > 0) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, g = 0;     cin >> t;     for (int i = 0; i < t; i++) {       vector<int> p;       int n, x;       g = 0;       cin >> n;       if (n == 1) {         cin >> x;         cout <<  YES  << endl;         continue;       }       for (int l = 0; l < n; l++) {         int k;         cin >> k;         p.push_back(k);       }       sort(p.begin(), p.end());       for (int j = 0; j < p.size() - 1; j++) {         if (abs(p[j] - p[j + 1]) > 1) {           cout <<  NO  << endl;           g = 1;           break;         }       }       if (g == 0) cout <<  YES  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     int n;     bool check, lucky;     string s;     cin >> n;     for (int i = 4; i < n + 1; i++) {       check = false;       s = to_string(i);       for (int j = 0; j < s.size(); j++)         if ((s[j] ==  4 ) || (s[j] ==  7 ))           check = true;         else {           check = false;           break;         }       if (check)         if (n % i == 0) {           lucky = true;           break;         } else {           lucky = false;         }     }     if (lucky)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve(int d) {     cout << fixed << setprecision(9);     if (d == 0) {       cout <<  Y   << 0.0 <<     << 0.0;     } else if (d >= 1 && d < 4)       cout <<  N ;     else {       double D = sqrt(d * (d - 4));       double a = (d + D) / 2.0;       double b = (d - D) / 2.0;       cout <<  Y   << a <<     << b;     }   }   int main() {     int t;     cin >> t;     while (t--) {       int d;       cin >> d;       solve(d);       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n;       scanf( %d , &n);       vector<int> a(n, 0);       for (int i = 0; i < n; i++) scanf( %d , &a[i]);       int m;       scanf( %d , &m);       vector<pair<int, int> > h(m, {0, 0});       for (int i = 0; i < m; i++) scanf( %d%d , &h[i].first, &h[i].second);       sort(h.begin(), h.end());       vector<int> suf(m, 0);       suf[m - 1] = h[m - 1].second;       for (int i = m - 2; i >= 0; i--) suf[i] = max(suf[i + 1], h[i].second);       int ans = 0;       int bad = 0;       for (int i = 0; i < n;) {         int j = i;         int mx = 0;         while (j < n) {           mx = max(mx, a[j]);           int p = lower_bound(h.begin(), h.end(), make_pair(mx, -1)) - h.begin();           if (p < m && suf[p] >= j - i + 1)             j++;           else             break;         }         if (j <= i) {           bad = 1;           break;         }         ans++;         i = j;       }       if (bad)         printf( -1 n );       else         printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int arr[n + 1];       int i, s = 0;       for (i = 0; i < n; i++) {         cin >> arr[i];       }       int j;       for (i = 1; i < n - 1; i++) {         if (arr[i - 1] < arr[i] && arr[i + 1] < arr[i]) {           cout <<  YES  << endl << i <<     << i + 1 <<     << i + 2 << endl;           s = 1;           break;         }       }       if (s != 1) cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 17;   int N, root[maxn], sz[maxn];   long long minv, maxv, val[maxn];   vector<int> tree[maxn];   vector<pair<long long, int> > nodes;   int find(int x) {     if (root[x] == x) return x;     return root[x] = find(root[x]);   }   long long merge(int first, int second) {     int x = find(first), y = find(second);     if (x == y) return 0LL;     long long res = 1LL * sz[x] * sz[y] * val[first];     sz[x] += sz[y];     root[y] = x;     return res;   }   int main() {     scanf( %d , &N);     for (int i = 1; i <= N; i++)       scanf( %I64d , val + i), nodes.push_back(make_pair(val[i], i));     for (int i = 1; i < N; i++) {       int A, B;       scanf( %d %d , &A, &B);       tree[A].push_back(B);       tree[B].push_back(A);     }     sort(nodes.begin(), nodes.end());     for (int i = 0; i <= N; i++) root[i] = i, sz[i] = 1;     for (int i = 0; i < N; i++)       for (int v : tree[nodes[i].second])         if (val[nodes[i].second] >= val[v]) maxv += merge(nodes[i].second, v);     for (int i = 0; i <= N; i++) root[i] = i, sz[i] = 1;     for (int i = N - 1; i >= 0; i--)       for (int v : tree[nodes[i].second])         if (val[nodes[i].second] <= val[v]) minv += merge(nodes[i].second, v);     printf( %I64d n , maxv - minv);     return 0;   }


/**     *  Author: dhruv_gheewala     *  Problem: A. Spy Detected!     *  Date:   12.04.2021     *  Time:   00:19:53   **/      #include  bits/stdc++.h    using namespace std;      #ifndef DEBUG      #define fundri 108   #define debug(...) 1729   #define endl   n       #endif      #define int int64_t      typedef pair<int, int> pii;   typedef vector<int> vi;      mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   inline int rnd(int l = 0, int r = INT_MAX) {return uniform_int_distribution<int>(l, r)(rng);}      bool in_range(int x, int l, int r) {return l <= x && x <= r;}   template<typename H, typename ...T>void inp(H &head) {cin >> head;}   template<typename H, typename ...T>void inp(H &head, T &...tail) {cin >> head;inp(tail...);}   template<typename T>inline istream &operator >>(istream &in, vector<T> &a) {for(T &x : a)in >> x; return in;}   template<typename T, typename U>inline istream &operator >>(istream &in, pair<T, U> &a) {in >> a.first >> a.second; return in;}      // Multi-Dimension Vector   // Usage: vec<n, data-type> dp(dimention-1, dimention-2, ..., dimention-n, default = data-type())   template<int D, typename T> struct vec : public vector<vec<D - 1, T>> {       static_assert(D >= 1,  Vector dimensions must be greater than zero !! );       template<typename... Args>       vec(int n = 0, Args... args) : vector<vec<D - 1, T>>(n, vec<D - 1, T>(args...)){}   };      template<typename T> struct vec<1, T> : public vector<T> {vec(int n = 0, T val = T()) : vector<T>(n, val){}};      const int inf = 1e15;   const bool testcases = true;      const bool ready = []() -> bool {       ios_base::sync_with_stdio(false);       cin.tie(nullptr);      #ifdef DHRUV_GHEEWALA       freopen( in.txt ,  r , stdin);       freopen( out.txt ,  w , stdout);       freopen( debug.txt ,  w , stderr);   #endif          return true;   }();      void solve(int tc);   int32_t main(int32_t argc, char **argv) {       int TC = 1;       if(testcases) cin >> TC;          // Pre-Processing goes here...       for(int tc = 1; tc <= TC; ++tc) {           solve(tc);           fundri;       }       exit(0);   }      void solve(int tc) {       int n;       cin >> n;          vector<int> a(n);       cin >> a;          map<int, int> mp;       for(int x: a)           mp[x]++;          for(int i = 0; i < n; i++) {           if(mp[a[i]] == 1) {               cout << i + 1 << endl;               return;           }       }   }


#include <bits/stdc++.h>   using namespace std;   int n, arr[110], ans[110];   int primes[17] = {2,  3,  5,  7,  11, 13, 17, 19, 23,                     29, 31, 37, 41, 43, 47, 53, 59};   int fact[65];   int memo[110][1 << 17];   int find_min_cost(int ind, int mask) {     if (ind >= n) return 0;     if (memo[ind][mask] != -1) {       return memo[ind][mask];     }     memo[ind][mask] = 1 << 30;     for (int i = 1; i < 2 * arr[ind]; i++) {       if ((mask & fact[i]) == 0) {         memo[ind][mask] =             min(memo[ind][mask],                 abs(i - arr[ind]) + find_min_cost(ind + 1, (mask | fact[i])));       }     }     return memo[ind][mask];   }   void compute_ans(int ind, int mask, int min_cost) {     if (ind >= n) return;     for (int i = 1; i < 2 * arr[ind]; i++) {       if (((mask & fact[i]) == 0) and           (min_cost == abs(i - arr[ind]) + memo[ind + 1][(mask | fact[i])])) {         ans[ind] = i;         compute_ans(ind + 1, (mask | fact[i]), memo[ind + 1][(mask | fact[i])]);         return;       }     }   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> arr[i];     memset(fact, 0, sizeof(fact));     for (int i = 2; i <= 62; i++) {       for (int j = 0; j < 17; j++) {         if ((i % primes[j]) == 0) fact[i] = (fact[i] | (1 << j));       }     }     for (int i = 0; i < n; i++)       for (int j = 0; j < (1 << 17); j++) memo[i][j] = -1;     for (int i = 0; i < (1 << 17); i++) memo[n][i] = 0;     int min_cost = find_min_cost(0, 0);     compute_ans(0, 0, min_cost);     for (int i = 0; i < n; i++) cout << ans[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int a;       cin >> a;       int k = a;       while (a--) {         cout << 1 <<    ;       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;      int dx[4] = {0, 0, 1, -1};   int dy[4] = {-1, 1, 0, 0};      int lredge[600][599]; //n, m-1   int tdedge[599][600]; //n-1, m   int n, m, k;      int distance(int i1, int j1, int i2, int j2){    //next cell is above    if(i1 > i2){     return tdedge[i2][j1];    }    //next cell is below    else if(i2 > i1){     return tdedge[i1][j1];    }    //next cell is to the left    else if(j1 > j2){     return lredge[i1][j2];    }    //next cell is to the right    else if(j2 > j1){     return lredge[i1][j1];    }       assert(1 == 0);   }      int dp[510][510][25];      int32_t main(){    cin >> n >> m >> k;       for(int i=0; i<510; i++)     for(int j=0; j<510; j++)      for(int k=1; k<25; k++)       dp[i][j][k] = 1000000000;       for(int i=0; i<n; i++){     for(int j=0; j<m-1; j++){      cin >> lredge[i][j];     }    }       for(int i=0; i<n-1; i++){     for(int j=0; j<m; j++){      cin >> tdedge[i][j];     }    }       if(k%2 == 1){     for(int i=0; i<n; i++){      for(int j=0; j<m; j++)       cout << -1 <<    ;      cout <<   n ;     }     return 0;    }       // for(int i=0; i<n; i++){    //  for(int j=0; j<m; j++){    //   for(int l=0; l<4; l++){    //    int newx = i + dx[l];    //    int newy = j + dy[l];    //    if(0 <= newx && newx <= n-1 && 0 <= newy && newy <= m-1){    //     dp[i][j][1] = min(dp[i][j][1], distance(i, j, newx, newy));    //    }    //   }    //  }    // }       // for(int i=0; i<n; i++){    //  for(int j=0; j<m; j++){    //   cout << dp[i][j][1] <<    ;    //  }    //  cout <<   n ;    // }       // for(int i=0; i<n; i++){    //  for(int j=0; j<m; j++){    //   cout << dp[i][j][0] <<    ;    //  }    //  cout <<   n ;    // }       for(int l=2; l<=k; l+=2){     for(int i=0; i<n; i++){      for(int j=0; j<m; j++){       for(int r=0; r<4; r++){        int newx = i + dx[r];        int newy = j + dy[r];        if(0 <= newx && newx <= n-1 && 0 <= newy && newy <= m-1){         dp[i][j][l] = min(dp[i][j][l], dp[newx][newy][l-2]+2*distance(i, j, newx, newy));        }       }      }     }        // debug( AA );     // for(int i=0; i<n; i++){     // for(int j=0; j<m; j++){     //  cout << dp[i][j][l] <<    ;     // }     // cout <<   n ;    // }    // cout <<   n ;    }       for(int i=0; i<n; i++){     for(int j=0; j<m; j++){      cout << dp[i][j][k] <<    ;     }     cout <<   n ;    }   }


#include <bits/stdc++.h>   long long sum = 0, dd = 0;   int N, R, V[(1 << 18) + 233];   int main() {     scanf( %d%d , &N, &R);     dd = 1 << N;     for (int i = 0; i < dd; ++i) {       scanf( %d , V + i);       sum += V[i];     }     printf( %.10lf n , sum / (double)dd);     for (int i = 0; i < R; ++i) {       int z, g;       scanf( %d%d , &z, &g);       sum -= V[z];       V[z] = g;       sum += g;       printf( %.10lf n , sum / (double)dd);     }   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( avx2 )   #pragma GCC optimization( O3 )   #pragma GCC optimization( unroll-loops )   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, q;     cin >> n >> q;     std::vector<vector<int>> v;     v.resize(2, vector<int>(n, 0));     int ans = 0;     while (q--) {       int change = 0;       int x, y;       cin >> x >> y;       x--;       y--;       if (v[x][y] == 0)         change++;       else         change--;       v[x][y] = 1 - v[x][y];       for (int i = -1; i <= 1; i++) {         if (y + i >= n && y + i <= 0) continue;         if (v[1 - x][y + i] == 1) ans += change;       }       if (ans == 0)         cout <<  YES n ;       else         cout <<  NO n ;     }   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   long long size(const T& x) {     return x.size();   }   template <class T>   T smod(T a, T b) {     return (a % b + b) % b;   }   long long n, k;   vector<long long> X;   vector<pair<long long, pair<long long, long long> > > A;   vector<vector<long long> > mem;   long long dp(int at, int chosen) {     if (at >= n) return 0;     if (mem[at][chosen] != -1) return mem[at][chosen];     long long mx = 0;     if (chosen < 2) {       mx = max(mx, A[at].first + dp(A[at].second.second + 1, chosen + 1));     }     mx = max(mx, dp(at + 1, chosen));     return mem[at][chosen] = mx;   }   void solve() {     cin >> n >> k;     mem = vector<vector<long long> >(n + 10, vector<long long>(3, -1));     X = vector<long long>(n);     A = vector<pair<long long, pair<long long, long long> > >(n);     for (long long i = (0); i < (n); ++i) {       cin >> X[i];     }     sort(X.begin(), X.end());     for (long long i = (0); i < (n); ++i) {       long long a;       cin >> a;     }     for (long long i = (0); i < (n); ++i) {       auto r = upper_bound(X.begin(), X.end(), X[i] + k);       int index = r - X.begin() - 1;       A[i] = pair<long long, pair<long long, long long> >(           index - i + 1, pair<long long, long long>(i, index));     }     cout << dp(0, 0) << endl;   }   int main() {     cin.sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     long long t;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int i = 1;     while ((n * i) % 10 != k && (n * i) % 10 != 0) {       i++;     }     cout << i << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000007;   int n, m;   int fa[200010];   struct Line {     int u, v, c;   } l[200010];   int p[200010];   bool cmp(int x, int y) { return l[x].c < l[y].c; }   bool intree[200010];   struct Edge {     int v, id, nxt;   } e[400010];   int tot;   int first[200010];   void build(int u, int v, int id) {     e[++tot] = (Edge){v, id, first[u]};     first[u] = tot;     return;   }   int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }   bool merge(int u, int v) {     u = find(u);     v = find(v);     if (u == v) return false;     fa[u] = v;     return true;   }   void init() {     for (int i = 1; i <= n; i++) fa[i] = i;     for (int i = 1; i <= m; i++) p[i] = i;     sort(p + 1, p + m + 1, cmp);     for (int i = 1; i <= m; i++) {       int x = p[i];       if (merge(l[x].u, l[x].v)) {         intree[x] = true;         build(l[x].u, l[x].v, x);         build(l[x].v, l[x].u, x);       }     }     return;   }   const int M = 20;   int f[200010][20];   int g[200010][20];   int h[200010][20];   int dep[200010];   int Id[200010];   void dfs(int u, int lst, int lste) {     Id[u] = lste;     dep[u] = dep[lst] + 1;     f[u][0] = lst;     g[u][0] = l[lste].c;     h[u][0] = INF;     for (int i = 1; i < M; i++) {       f[u][i] = f[f[u][i - 1]][i - 1];       g[u][i] = max(g[u][i - 1], g[f[u][i - 1]][i - 1]);       h[u][i] = INF;     }     for (int i = first[u]; i; i = e[i].nxt)       if (e[i].v != lst) dfs(e[i].v, u, e[i].id);     return;   }   int ans[200010];   void solve(int x) {     int u = l[x].u;     int v = l[x].v;     int c = l[x].c;     ans[x] = 0;     if (dep[u] < dep[v]) swap(u, v);     int need = dep[u] - dep[v], cur = 0;     while (need) {       if (need & 1) {         ans[x] = max(ans[x], g[u][cur]);         h[u][cur] = min(h[u][cur], c);         u = f[u][cur];       }       need >>= 1;       cur++;     }     if (u == v) return;     for (int i = M - 1; i >= 0; i--)       if (f[u][i] != f[v][i]) {         ans[x] = max(ans[x], g[u][i]);         ans[x] = max(ans[x], g[v][i]);         h[u][i] = min(h[u][i], c);         h[v][i] = min(h[v][i], c);         u = f[u][i];         v = f[v][i];       }     ans[x] = max(ans[x], g[u][0]);     ans[x] = max(ans[x], g[v][0]);     h[u][0] = min(h[u][0], c);     h[v][0] = min(h[v][0], c);     return;   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= m; i++) scanf( %d %d %d , &l[i].u, &l[i].v, &l[i].c);     init();     dfs(1, 0, 0);     for (int i = 1; i <= m; i++)       if (!intree[i]) solve(i);     for (int i = M - 1; i > 0; i--)       for (int u = 1; u <= n; u++) {         h[u][i - 1] = min(h[u][i - 1], h[u][i]);         h[f[u][i - 1]][i - 1] = min(h[f[u][i - 1]][i - 1], h[u][i]);       }     for (int u = 2; u <= n; u++) ans[Id[u]] = h[u][0];     for (int i = 1; i <= m; i++)       if (ans[i] != INF)         printf( %d  , ans[i] - 1);       else         printf( -1  );     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> e[400010], b[400010];   int ll[400010], rr[400010], son[400010], No, an[400010], now;   void dfs0(int x, int last) {     son[x] = 1;     ll[x] = No++;     for (int i = 0; i < e[x].size(); i++) {       if (e[x][i] == last) continue;       dfs0(e[x][i], x);       son[x] += son[e[x][i]];     }     rr[x] = No++;   }   int ac[400010 << 1], ha[400010 << 1];   void insert(int d[], int id, int L, int R, int L2, int R2, int v) {     if (L2 <= L && R <= R2) {       d[id] += v;       return;     }     int M = (L + R) >> 1;     if (R2 <= M)       insert(d, id << 1, L, M, L2, R2, v);     else if (M < L2)       insert(d, (id << 1) | 1, M + 1, R, L2, R2, v);     else {       insert(d, id << 1, L, M, L2, R2, v);       insert(d, (id << 1) | 1, M + 1, R, L2, R2, v);     }   }   int qq(int d[], int id, int L, int R, int x) {     if (L == x && x == R) {       return d[id];     }     int M = (L + R) >> 1;     if (x <= M)       return d[id] + qq(d, id << 1, L, M, x);     else       return d[id] + qq(d, (id << 1) | 1, M + 1, R, x);   }   int qq2(int d[], int id, int L, int R, int L2, int R2) {     if (L2 <= L && R <= R2) {       return d[id];     }     int M = (L + R) >> 1;     if (R2 <= M)       return qq2(d, id << 1, L, M, L2, R2);     else if (M < L2)       return qq2(d, (id << 1) | 1, M + 1, R, L2, R2);     else       return qq2(d, id << 1, L, M, L2, R2) +              qq2(d, (id << 1) | 1, M + 1, R, L2, R2);   }   void insert2(int d[], int id, int L, int R, int x, int v) {     d[id] += v;     if (L == x && x == R) return;     int M = (L + R) >> 1;     if (x <= M)       insert2(d, id << 1, L, M, x, v);     else       insert2(d, (id << 1) | 1, M + 1, R, x, v);   }   void dfs(int x, int last) {     vector<int> tmp, tmp2;     int i;     for (i = 0; i < b[x].size(); i++) {       if (qq(ac, 1, 1, No, ll[b[x][i]]))         continue;       else {         tmp.push_back(b[x][i]);         insert(ac, 1, 1, No, ll[b[x][i]], rr[b[x][i]], 1);         int get_v = qq2(ha, 1, 1, No, ll[b[x][i]], rr[b[x][i]]);         insert2(ha, 1, 1, No, ll[b[x][i]], son[b[x][i]] - get_v);         tmp2.push_back(son[b[x][i]] - get_v);         now += son[b[x][i]] - get_v;       }     }     an[x] = now;     for (int i = 0; i < e[x].size(); i++) {       if (e[x][i] == last) continue;       dfs(e[x][i], x);     }     for (i = 0; i < tmp.size(); i++) {       insert(ac, 1, 1, No, ll[tmp[i]], rr[tmp[i]], -1);       insert2(ha, 1, 1, No, ll[tmp[i]], -tmp2[i]);       now -= tmp2[i];     }   }   int main() {     No = 1;     int i, j, k, n, m;     scanf( %d%d , &n, &m);     for (i = 1; i < n; i++) {       int x, y;       scanf( %d%d , &x, &y);       e[x].push_back(y);       e[y].push_back(x);     }     for (i = 0; i < m; i++) {       int x, y;       scanf( %d%d , &x, &y);       b[x].push_back(x);       b[x].push_back(y);       b[y].push_back(x);       b[y].push_back(y);     }     dfs0(1, 0);     dfs(1, 0);     for (i = 1; i <= n; i++) {       if (i != 1) printf(   );       printf( %d , an[i] > 0 ? (an[i] - 1) : 0);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n, v;     cin >> n >> v;     if (n >= v) {       long long t = (n - v) * (n - v + 1);       t /= 2;       t += v - 1;       cout << t;     } else {       cout << n - 1;     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t;     t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string n;     cin >> n;     for (int i = 0; i < n.length(); i++) cout << n[i];     for (int i = n.length() - 1; i >= 0; i--) cout << n[i];   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100010, C = 1000000000;   int n, num;   int kd[N], t[N], x[N], fr[N], nxt[N], ans[N];   map<int, int> S;   int lc[N * 40], rc[N * 40], s[N * 40];   void change(int &i, int l, int r, int k, int d) {     if (i == 0) i = ++num;     s[i] += d;     if (l == r) return;     int mid = (l + r) >> 1;     if (k <= mid) change(lc[i], l, mid, k, d);     if (k > mid) change(rc[i], mid + 1, r, k, d);   }   int query(int i, int l, int r, int rr) {     if (i == 0) return 0;     if (r <= rr) return s[i];     int mid = (l + r) >> 1, res = 0;     res += query(lc[i], l, mid, rr);     if (mid < rr) res += query(rc[i], mid + 1, r, rr);     return res;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d%d%d , &kd[i], &t[i], &x[i]);       fr[i] = 1;       if (S.count(x[i])) {         nxt[S[x[i]]] = i;         fr[i] = 0;       }       S[x[i]] = i;     }     for (int i = 1; i <= n; i++)       if (fr[i]) {         for (int j = 1; j <= num; j++) lc[j] = rc[j] = s[j] = 0;         num = 1;         int rt = 1;         for (int j = i; j; j = nxt[j]) {           if (kd[j] == 1) change(rt, 1, C, t[j], 1);           if (kd[j] == 2) change(rt, 1, C, t[j], -1);           if (kd[j] == 3) ans[j] = query(1, 1, C, t[j]);         }       }     for (int i = 1; i <= n; i++)       if (kd[i] == 3) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long in() {     long long a;     scanf( %lld , &a);     return a;   }   bool isprime(long long n) {     if (n == 1) return 0;     if (n == 2) return 1;     if (n % 2) return 0;     for (int i = 2; i <= sqrt(n) + 2; i++)       if (n % i == 0) return 0;     return 1;   }   long long dao(long long n) {     for (int i = 2; i <= (sqrt(n)); i++)       if (n % i == 0) return i;   }   int main() {     long long a = in(), b = in();     if (a == b) cout << -1 << endl, exit(0);     if (a == 2) {       if (b & 1)         cout << b << endl;       else         cout << --b << endl;       exit(0);     }     for (long long i = b; i >= a; i--) {       if (dao(i) > a || isprime(i)) cout << i << endl, exit(0);     }     cout << -1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long mod, n, m, a[200005], b[200005], kq, vt1, vt2, l, r, mid, mn[200005];   map<int, bool> mp;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     mod = 998244353;     for (int i = 1; i <= n; ++i) {       cin >> a[i];       mp[a[i]] = true;     }     for (int i = 1; i <= m; ++i) {       cin >> b[i];       if (!mp[b[i]]) {         cout << 0;         return 0;       }     }     for (int i = 1; i <= n; ++i) {       if (a[i] < b[1]) {         cout << 0;         return 0;       }       if (a[i] == b[m]) vt1 = i;     }     for (int i = n; i > vt1; --i)       if (a[i] < b[m]) {         cout << 0;         return 0;       }     mn[n] = a[n];     for (int i = n - 1; i > 0; --i) mn[i] = min(a[i], mn[i + 1]);     kq = 1;     for (int i = 1; i < m; ++i) {       l = 1;       r = n - 1;       while (l <= r) {         mid = (l + r) / 2;         if (mn[mid + 1] >= b[i + 1]) {           vt1 = mid;           r = mid - 1;         } else           l = mid + 1;       }       l = 1;       r = n - 1;       while (l <= r) {         mid = (l + r) / 2;         if (mn[mid + 1] > b[i + 1])           r = mid - 1;         else {           vt2 = mid;           l = mid + 1;         }       }       kq = kq * max(0LL, vt2 - vt1 + 1) % mod;       ;     }     cout << kq;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, r, x, s = 0;     cin >> n >> r >> x;     int a[r + 1];     for (int i = 1; i <= r; i++) {       a[i] = 10000000;     }     while (n--) {       int l, m;       cin >> l >> m;       a[l] = min(a[l], m);     }     for (int i = 1; i <= r; i++) {       s += a[i];     }     cout << min(s, x);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   __int128 read() {     __int128 s = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       s = (s << 3) + (s << 1) + ch -  0 , ch = getchar();     return f * s;   }   struct node {     __int128 x, y;   } a[200];   int n;   __int128 abs(__int128 a) { return a > 0 ? a : -a; }   int main() {     a[0].x = read(), a[0].y = read();     __int128 ax = read(), ay = read(), bx = read(), by = read();     for (; a[n].x + a[n].y <= 1e18; n++)       a[n + 1].x = a[n].x * ax + bx, a[n + 1].y = a[n].y * ay + by;     __int128 xs = read(), ys = read(), t = read();     int ans = 0;     for (int s = 0, i; s <= n; s++) {       __int128 d = 0, x = xs, y = ys;       for (i = s; ~i && d <= t; i--) {         d += abs(x - a[i].x) + abs(y - a[i].y), x = a[i].x, y = a[i].y;       }       ans = max(ans, d <= t ? s - i : s - i - 1);       d = 0, x = xs, y = ys;       for (i = s; i <= n && d <= t; i++) {         d += abs(x - a[i].x) + abs(y - a[i].y), x = a[i].x, y = a[i].y;       }       ans = max(ans, d <= t ? i - s : i - s - 1);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ull = unsigned long long;   const int kMod = 1e9 + 7;   mt19937_64 rng(time(0));   struct SegTree {     struct Node {       ull dp = 0;       int l = 0, r = 0;       ull hash = 0;     };     int n;     vector<Node> T;     vector<ull> rnd;     SegTree(int n) : n(n), T(1), rnd(n) {       for (int i = 0; i < n; ++i) rnd[i] = rng();     }     pair<int, int> add(int node, int b, int e, int pos) {       if (e <= pos) return {node, 0};       if (b == pos && T[node].dp + 1 == 0) return {0, 1};       T.emplace_back();       int ret = T.size() - 1;       if (e - b == 64) {         T[ret].dp = T[node].dp;         T[ret].dp += (1ULL << (pos - b));         int state = 1 + (T[ret].dp > T[node].dp);         for (int i = b; i < e; ++i)           if (T[ret].dp & (1ULL << (i - b))) T[ret].hash += rnd[i];         return {ret, state};       }       int state;       int l = T[node].l, r = T[node].r;       int m = (b + e) / 2;       tie(l, state) = add(l, b, m, pos);       if (state == 0)         tie(r, state) = add(r, m, e, pos);       else if (state == 1)         tie(r, state) = add(r, m, e, m);       T[ret].l = l;       T[ret].r = r;       T[ret].hash = T[l].hash + T[r].hash;       T[ret].dp = min(T[l].dp, T[r].dp);       return {ret, state};     }     int Add(int node, int val) {       auto [ret, state] = add(node, 0, n, val);       assert(state == 2);       return ret;     }     int compare(int x, int y, int b, int e) {       if (T[x].hash == T[y].hash) return 0;       if (e - b == 64) return T[x].dp < T[y].dp ? -1 : 1;       int m = (b + e) / 2;       int ret = compare(T[x].r, T[y].r, m, e);       if (ret) return ret;       return compare(T[x].l, T[y].l, b, m);     }     int Compare(int a, int b) { return compare(a, b, 0, n); }     void get(int node, int b, int e, int& ans) {       if (e - b == 64) {         for (int i = b; i < e; ++i) ans = (ans + ans) % kMod;         ans = (ans + T[node].dp % kMod) % kMod;       } else {         int m = (b + e) / 2;         get(T[node].r, m, e, ans);         get(T[node].l, b, m, ans);       }     }     int Get(int node) {       int ans = 0;       get(node, 0, n, ans);       return ans;     }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<vector<pair<int, int>>> graph(n);     for (int i = 0; i < m; ++i) {       int a, b, c;       cin >> a >> b >> c;       --a;       --b;       graph[a].emplace_back(c, b);       graph[b].emplace_back(c, a);     }     SegTree st(1 << 17);     int s, t;     cin >> s >> t;     --s;     --t;     auto cmp = [&](pair<int, int> a, pair<int, int> b) {       return st.Compare(a.first, b.first) == 1;     };     priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> pq(cmp);     vector<int> dist(n, -1), parent(n, -1), vis(n, false);     dist[s] = 0;     pq.emplace(0, s);     while (pq.size()) {       auto [d, node] = pq.top();       pq.pop();       if (d != dist[node]) continue;       if (node == t) break;       vis[node] = true;       for (auto [cost, vec] : graph[node]) {         if (vis[vec]) continue;         int nd = st.Add(d, cost);         if (dist[vec] == -1 || st.Compare(nd, dist[vec]) == -1) {           dist[vec] = nd;           parent[vec] = node;           pq.emplace(nd, vec);         }       }     }     if (dist[t] == -1) {       cout << -1 <<   n ;     } else {       vector<int> trace;       for (int node = t; node != -1; node = parent[node]) trace.push_back(node);       reverse(trace.begin(), trace.end());       cout << st.Get(dist[t]) << endl;       cout << trace.size() << endl;       for (auto x : trace) cout << x + 1 <<    ;       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t, n, a[11];   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       int day = 0;       for (int i = 0; i < 7; i++) scanf( %d , a + i), day += a[i];       int week = (n - 1) / day, best = 7, left = n - week * day;       for (int i = 0; i < 7; i++) {         int tot = 0, cur = 7;         for (int j = 0; j < 7; j++)           if (a[j] && ++tot == left) {             cur = j;             break;           }         best = min(best, cur + 1);         rotate(a, a + 1, a + 7);       }       printf( %d n , week * 7 + best);     }   }


#include <bits/stdc++.h>   using namespace std;   vector<char> ch1;   int main() {     string s, str =   ;     getline(cin, s);     for (int i = 0; i < s.length(); i++) {       if (s[i] ==  . ) {         ch1.push_back( - );         i = i + 2;         continue;       }       if (s[i] ==     && s[i + 1] >=  0  && s[i + 1] <=  9  &&           ch1.back() >=  0  && ch1.back() <=  9 ) {         ch1.push_back( / );       }       if (s[i] !=    ) ch1.push_back(s[i]);     }     for (int i = 0; i < ch1.size(); i++) {       if (ch1[i] ==  -  && ch1[i + 1] !=  - )         str = str +  ... ;       else if (ch1[i] ==  -  && ch1[i + 1] ==  - )         str = str +  ...  +    ;       else if (ch1[i] ==  , )         str = str + ch1[i] +    ;       else if (ch1[i] ==  / )         str = str +    ;       else if (ch1[i] >=  0  && ch1[i] <=  9  && ch1[i + 1] ==  - )         str = str + ch1[i] +    ;       else         str = str + ch1[i];     }     if (str.back() ==    ) str.erase(str.length() - 1, 1);     cout << str;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, t;     cin >> n >> t;     int f = 0;     int x;     int sum = 1;     for (int i = 1; i < n; i++) {       cin >> x;       if (i == sum) sum += x;       if (sum == t) f = 1;     }     if (f == 1)       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int xuli(long long l, long long r, long long d) {     if (l > d || r < d)       return d;     else       return (r / d + 1) * d;   }   int main() {     long long n;     cin >> n;     long long i, l, r, d;     for (i = 1; i <= n; i++) {       cin >> l >> r >> d;       cout << xuli(l, r, d) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long Set(long long N, long long pos) { return N = N | (1 << pos); }   long long reset(long long N, long long pos) { return N = N & ~(1 << pos); }   bool check(long long N, long long pos) { return (bool)(N & (1 << pos)); }   void CI(long long &_x) { scanf( %d , &_x); }   void CO(long long &_x) { cout << _x; }   template <typename T>   void getarray(T a[], long long n) {     for (long long i = 0; i < n; i++) cin >> a[i];   }   template <typename T>   void prLLIarray(T a[], long long n) {     for (long long i = 0; i < n - 1; i++) cout << a[i] <<    ;     cout << a[n - 1] << endl;   }   const double EPS = 1e-9;   const long long INF = 0x7f7f7f7f;   long long dr8[8] = {1, -1, 0, 0, 1, -1, -1, 1};   long long dc8[8] = {0, 0, -1, 1, 1, 1, -1, -1};   long long dr4[4] = {0, 0, 1, -1};   long long dc4[4] = {-1, 1, 0, 0};   long long kn8r[8] = {1, 2, 2, 1, -1, -2, -2, -1};   long long kn8c[8] = {2, 1, -1, -2, -2, -1, 1, 2};   long long pow(long long a, long long b) {     long long ans = 1;     for (long long i = 0; i < b; i++) ans *= a;     return ans;   }   int main() {     long long a, b, n;     cin >> a >> b >> n;     if (b == 0) {       cout <<  0 n ;       return 0;     }     if (a == 0) {       puts( No solution );       return 0;     }     long long y = b / a;     long long low = -(1001), hi = (1001);     for (int i = 1; i <= 1000; i++) {       long long kk = 0, temp = abs(y);       while (temp % i == 0 && kk < n) {         temp = temp / i;         kk++;       }       if (kk == n && temp == 1) {         if (y < 0) i *= -1;         cout << i <<   n ;         return 0;       }     }     puts( No solution );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       int cnt = 0;       while (n > 1) {         if (n % 5 == 0) {           n = (4 * n) / 5;           cnt++;           continue;         } else if (n % 3 == 0) {           n = (2 * n) / 3;           cnt++;           continue;         } else if (n % 2 == 0) {           n = (n) / 2;           cnt++;           continue;         } else {           cnt = -1;           break;         }       }       cout << cnt << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 103;   using pi = pair<int, int>;   inline int rd() {     int x = 0, f = 1, c;     while (!isdigit(c = getchar()))       if (c ==  - ) f = -1;     for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ;     return f * x;   }   int n, m, k;   char p[N][N];   inline void solve() {     n = rd(), m = rd(), k = rd();     int cntr = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) cin >> p[i][j], cntr += p[i][j] ==  R ;     vector<int> sz;     for (int i = 1; i <= cntr - cntr / k * k; i++) sz.push_back(cntr / k + 1);     while (sz.size() < k) sz.push_back(cntr / k);     vector<char> ch;     for (int i = 0; i < 26; i++) ch.push_back( A  + i), ch.push_back( a  + i);     for (char i =  0 ; i <=  9 ; i++) ch.push_back(i);     for (int r = 1, cr = 0; r <= n; r++) {       if (r & 1)         for (int c = 1; c <= m; c++) {           if (p[r][c] ==  R ) cr++;           p[r][c] = ch.back();           if (cr == sz.back()) {             sz.pop_back();             cr = 0;             if (sz.size()) ch.pop_back();           }         }       else         for (int c = m; c; c--) {           if (p[r][c] ==  R ) cr++;           p[r][c] = ch.back();           if (cr == sz.back()) {             sz.pop_back();             cr = 0;             if (sz.size()) ch.pop_back();           }         }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) cout << p[i][j];       cout << endl;     }   }   int main() {     for (int T = rd(); T--; solve())       ;   }


#include <bits/stdc++.h>   using namespace std;   struct dd {     int k1;     char k2;   } a[25000];   bool cmp(struct dd q, struct dd b) { return q.k1 > b.k1; }   int main() {     int i, j, m, n, sum;     char b[1000];     gets(b);     sum = 0;     char k =  a ;     scanf( %d , &m);     for (i = 0; i < 26; i++) {       scanf( %d , &a[i].k1);       a[i].k2 = k;       k++;     }     int len1 = strlen(b);     for (i = 0; i < len1; i++) {       sum += a[(int)b[i] - 97].k1 * (i + 1);     }     sort(a, a + 26, cmp);     for (i = len1; i < len1 + m; i++) {       sum += a[0].k1 * (i + 1);     }     printf( %d n , sum);   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   T abs(T a) {     return (a >= 0 ? a : -a);   }   int main() {     int k, no[12];     cin >> k;     for (int i = 0; i < 12; i++) cin >> no[i];     sort(no, no + 12);     int res = 0, i = 11;     while (k > 0 && i >= 0) {       res++;       k -= no[i];       i--;     }     if (k <= 0)       cout << res;     else       cout <<  -1 ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int N = 1e6 + 5;   const long long int INF = 1e18;   const long long int MOD = 1e9 + 7;   int a[N], has[N], hol[N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long int n, m, k, u, v;     cin >> n >> m >> k;     a[1] = 1;     long long int last;     for (long long int i = 1; i <= m; i++) {       cin >> hol[i];       has[hol[i]] = 1;     }     for (long long int i = 1; i <= k; i++) {       cin >> u >> v;       if (has[u] && a[u]) {         cout << u << endl;         exit(0);       }       if (has[v] && a[v]) {         cout << v << endl;         exit(0);       }       swap(a[u], a[v]);       if (has[u] && a[u]) {         cout << u << endl;         exit(0);       }       if (has[v] && a[v]) {         cout << v << endl;         exit(0);       }     }     for (long long int i = 1; i <= n; i++) {       if (a[i]) {         cout << i << endl;         exit(0);       }     }   }


#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:256000000 )   template <class T>   T sqr(T a) {     return a * a;   }   int n, m, b;   long long a[1 << 20];   int was[1 << 20];   struct dr {     int mon, pl, zad;     int mask;     const bool operator<(dr a) const {       if (mon < a.mon)         return true;       else         return false;     }   };   dr fr[100];   void init() {     scanf( %d%D%D , &n, &m, &b);     for (int i = 0; i < n; ++i) {       scanf( %d%d%D , &fr[i].pl, &fr[i].mon, &fr[i].zad);       for (int j = 0; j < fr[i].zad; ++j) {         int t;         scanf( %D , &t);         t--;         fr[i].mask |= 1 << t;       }     }     sort(fr, fr + n);   }   int q = 0;   long long ot = -1, ma = 1e18, co;   void solve() {     was[0] = 1;     for (int i = 0; i < (1 << m); ++i) a[i] = 1e18;     a[0] = 0;     for (int i = 0; i < n; ++i) {       co = fr[i].mon;       int t = fr[i].mask;       for (int j = (1 << m) - 1; j >= 0; --j) {         if (!was[j]) continue;         was[j | t] = 1;         a[j | t] = min(a[j | t], a[j] + fr[i].pl);       }       if (!was[(1 << m) - 1]) continue;       if (ot == -1)         ot = a[(1 << m) - 1] + co * (long long)b;       else         ot = min(ot, a[(1 << m) - 1] + co * (long long)b);     }     cout << ot;   }   int main() {     init();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace ::std;   int a[110], n, c, i, j, o, m;   int main() {     cin >> n >> c;     cin >> a[0];     for (i = 1; i < n; i++) {       cin >> a[i];       m = max(a[i - 1] - a[i] - c, m);     }     cout << m;   }


#include <bits/stdc++.h>   using namespace std;   map<string, int> mp;   int n, m, cc;   char s[1010];   int a[1010], cnt[1010];   void getcc(string s) {     if (mp.find(s) == mp.end()) mp[s] = cc++;     cnt[mp[s]]++;   }   int main() {     int i, j, k;     while (scanf( %d%d , &n, &m) > 0) {       for (i = 0; i < n; i++) scanf( %d , &a[i]);       sort(a, a + n);       mp.clear();       cc = 0;       memset(cnt, 0, sizeof(cnt));       for (i = 0; i < m; i++) {         scanf( %s , s);         getcc(s);       }       sort(cnt, cnt + cc);       int ma = 0, mi = 0;       for (i = 0; i < cc; i++) {         mi += a[i] * cnt[cc - 1 - i];         ma += a[n - 1 - i] * cnt[cc - 1 - i];       }       printf( %d %d n , mi, ma);     }     return 0;   }


#include <bits/stdc++.h>   const int MAXINT = 2147483640;   const long long MAXLL = 9223372036854775800LL;   const long long MAXN = 1000000;   using namespace std;   int l[MAXN], r[MAXN];   int n;   int ans[MAXN];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int T;     cin >> T;     while (T--) {       cin >> n;       for (int i = 1; i <= n; ++i) ans[i] = 0;       for (int i = 1; i <= n; ++i) cin >> l[i] >> r[i];       int t = l[1];       ans[1] = t;       for (int i = 2; i <= n; ++i) {         if (r[i] <= t)           ans[i] = 0;         else {           ans[i] = max(l[i], t + 1);           t = ans[i];         }       }       for (int i = 1; i <= n; ++i) cout << ans[i] <<    ;       cout <<   n ;     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   long long a[10];   long long ans = 0;   void DFS(long long n, long long u, long long r[10]) {     if (n == 10) {       long long sum = 1;       for (long long i = 2; i <= u; i++) sum *= i;       for (long long i = 1; i < 10; i++)         for (long long j = 2; j <= r[i]; j++) sum /= j;       long long b = 0;       for (long long i = 1; i <= a[0]; i++) {         long long d = 1, f = 1;         for (long long j = 0; j < i; j++) {           d *= u + j;           f *= j + 1;         }         b += d / f;       }       if (a[0] == 0) b = 1;       ans += b * sum;       return;     }     if (a[n] == 0) {       r[n] = 0;       DFS(n + 1, u, r);       return;     }     for (long long i = 1; i <= a[n]; i++) {       r[n] = i;       DFS(n + 1, u + i, r);     }   }   int main() {     long long d[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};     long long s;     cin >> s;     while (s) {       a[s % 10]++;       s /= 10;     }     DFS(1, 0, d);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18;   const long long MOD = 1e9 + 7;   const int inf = 1e9;   const int N = 3e5 + 5;   bool flag;   template <typename T>   istream& operator>>(istream& is, vector<T>& v) {     for (auto& i : v) is >> i;     return is;   }   template <typename T>   void mul(T& a, T b) {     a += b;     if (b < (T)0) a += MOD;     if (a >= (T)MOD) a -= MOD;   }   void solve() {     int n, a, b;     cin >> n >> a >> b;     if (abs(a - b) > 1 or a + b + 2 > n) {       cout << -1;       return;     }     if (a == 0 and b == 0) {       for (int i = 1; i <= n; i++) cout << i <<    ;       return;     }     vector<int> ans;     int l = 1, r = n;     if (a > b) {       ans.push_back(l++);       for (int i = 1; i <= a + b; i++) {         if (i % 2)           ans.push_back(r--);         else           ans.push_back(l++);       }       for (int i = a + b + 1; i < n; i++) ans.push_back(r--);     } else {       ans.push_back(r--);       for (int i = 1; i <= a + b; i++) {         if (i % 2 == 0)           ans.push_back(r--);         else           ans.push_back(l++);       }       for (int i = a + b + 1; i < n; i++) ans.push_back(a == b ? r-- : l++);     }     for (auto now : ans) cout << now <<    ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout << fixed << setprecision(12);     int tt = 1;     cin >> tt;     while (tt--) {       flag = 0;       solve();       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void debug(T a, T b) {     ;   }   template <class T>   void chmin(T& a, const T& b) {     if (a > b) a = b;   }   template <class T>   void chmax(T& a, const T& b) {     if (a < b) a = b;   }   namespace std {   template <class S, class T>   ostream& operator<<(ostream& out, const pair<S, T>& a) {     out <<  (  << a.first <<  ,  << a.second <<  ) ;     return out;   }   }  // namespace std   long long int readL() {     long long int res;     scanf( %I64d , &res);     return res;   }   void printL(long long int res) { printf( %I64d , res); }   int n, S;   struct pack {     int in, out, w, s, v;   };   pack par[505];   bool cmp(const pack& a, const pack& b) {     return make_pair(a.in, -a.out) < make_pair(b.in, -b.out);   }   int tmp[1005][1005];   int dp[505][1005];   void renew(int i, int base) {     int lim = min(S, par[base].w + par[base].s);     for (int j = 0; j < (lim); ++j) {       chmax(tmp[i][j + 1], tmp[i][j]);     }   }   void update(pack p, int id, int base) {     int l = p.in, r = p.out;     renew(l, base);     int lim = min(S, par[base].w + par[base].s);     for (int i = (par[base].w); i < (lim + 1); ++i)       if (tmp[l][i] >= 0 && dp[id][i - par[base].w] >= 0) {         chmax(tmp[r][i], tmp[l][i] + dp[id][i - par[base].w]);       }   }   void bring(int i, int base) {     renew(i, base);     int lim = min(S, par[base].w + par[base].s);     for (int j = 0; j < (lim + 1); ++j) {       chmax(tmp[i + 1][j], tmp[i][j]);     }   }   int main() {     cin >> n >> S;     for (int i = 0; i < (n); ++i) {       cin >> par[i].in >> par[i].out >> par[i].w >> par[i].s >> par[i].v;     }     par[n] = (pack){0, 2 * n, 0, S + 1, 0};     ++n;     sort(par, par + n, cmp);     memset(dp, -1, sizeof(dp));     int res = 0;     for (int i = n - 1; i >= 0; --i) {       pack p = par[i];       int l = par[i].in, r = par[i].out;       for (int j = (l); j < (r + 1); ++j)         for (int k = 0; k < (S + 1); ++k) tmp[j][k] = -1;       tmp[l][p.w] = p.v;       renew(l, i);       int cur = l;       for (int j = (i + 1); j < (n); ++j) {         if (l <= par[j].in && par[j].out <= r) {           while (cur < par[j].in) {             bring(cur, i);             ++cur;           }           update(par[j], j, i);         }       }       while (cur < r) {         bring(cur, i);         ++cur;       }       renew(r, i);       int lim = min(S, p.w + p.s);       for (int j = 0; j < (lim + 1); ++j) {         dp[i][j] = tmp[r][j];         chmax(res, tmp[r][j]);       }       for (int j = 0; j < (S); ++j) chmax(dp[i][j + 1], dp[i][j]);     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool PIIfs(const pair<int, int>& a, const pair<int, int>& b) {     return a.first < b.first || (a.first == b.first && a.second < b.second);   }   bool PIIsf(const pair<int, int>& a, const pair<int, int>& b) {     return a.second < b.second || (a.second == b.second && a.first < b.first);   }   bool PIIFS(const pair<int, int>& a, const pair<int, int>& b) {     return a.first > b.first || (a.first == b.first && a.second > b.second);   }   bool PIISF(const pair<int, int>& a, const pair<int, int>& b) {     return a.second > b.second || (a.second == b.second && a.first > b.first);   }   bool PIIfS(const pair<int, int>& a, const pair<int, int>& b) {     return a.first < b.first || (a.first == b.first && a.second > b.second);   }   bool PIIsF(const pair<int, int>& a, const pair<int, int>& b) {     return a.second < b.second || (a.second == b.second && a.first > b.first);   }   bool PIIFs(const pair<int, int>& a, const pair<int, int>& b) {     return a.first > b.first || (a.first == b.first && a.second < b.second);   }   bool PIISf(const pair<int, int>& a, const pair<int, int>& b) {     return a.second > b.second || (a.second == b.second && a.first < b.first);   }   template <typename T>   inline T max(T a, T b, T c) {     return max(max(a, b), c);   }   template <typename T>   inline T max(T a, T b, T c, T d) {     return max(max(a, b, c), d);   }   template <typename T>   inline T max(T a, T b, T c, T d, T e) {     return max(max(a, b, c, d), e);   }   template <typename T>   inline T min(T a, T b, T c) {     return min(min(a, b), c);   }   template <typename T>   inline T min(T a, T b, T c, T d) {     return min(min(a, b, c), d);   }   template <typename T>   inline T min(T a, T b, T c, T d, T e) {     return min(min(a, b, c, d), e);   }   template <typename T>   inline istream& RD(T& x) {     return cin >> x;   }   template <typename T, typename U>   inline istream& RD(T& a, U& b) {     return cin >> a >> b;   }   template <typename T, typename U, typename V>   inline istream& RD(T& a, U& b, V& c) {     return cin >> a >> b >> c;   }   template <typename T, typename U, typename V, typename W>   inline istream& RD(T& a, U& b, V& c, W& d) {     return cin >> a >> b >> c >> d;   }   template <typename T, typename U, typename V, typename W, typename X>   inline istream& RD(T& a, U& b, V& c, W& d, X& e) {     return cin >> a >> b >> c >> d >> e;   }   template <typename T>   void checkmin(T& a, const T& b) {     if (b < a) a = b;   }   template <typename T>   void checkmax(T& a, const T& b) {     if (b > a) a = b;   }   int n;   void dfs(const vector<string>& vs, bitset<300>& mark, int node,            vector<int>& ans) {     if (mark.test(node)) return;     mark.set(node);     ans.push_back(node);     for (int i = 0; i < n; i++)       if (vs[node][i] ==  1 ) dfs(vs, mark, i, ans);   }   void perm(vector<int>& vi, vector<int>& si) {     int x = si.size();     vector<int> tmp(x);     for (int i = 0; i < (int)(x); ++i) tmp[i] = vi[si[i]];     sort(tmp.begin(), tmp.end());     sort(si.begin(), si.end());     for (int i = 0; i < (int)(x); ++i) vi[si[i]] = tmp[i];   }   int main() {     std::ios::sync_with_stdio(false);     cin >> n;     vector<int> vi(n);     for (int i = 0; i < (int)(n); ++i) cin >> vi[i];     vector<string> vs;     string s;     for (int i = 0; i < (int)(n); ++i) {       cin >> s;       vs.push_back(s);     }     vector<set<int> > circles;     bitset<300> found;     for (int i = 0; i < n; i++) {       vector<int> ans;       dfs(vs, found, i, ans);       if (ans.size() > 1) {         perm(vi, ans);       }     }     for (__typeof__(vi.begin()) it = vi.begin(); it != vi.end(); ++it)       cout << *it <<    ;     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long double DP[202][202][202];   long double P[202];   int A[202];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, l, k;     cin >> n >> l >> k;     k = min(k, n);     for (int i = 1; i <= n; i++) cin >> P[i], P[i] /= 100.0;     for (int i = 1; i <= n; i++) cin >> A[i];     DP[1][0][k] = 1;     for (int i = 1; i <= n; i++)       for (int j = 0; j < i; j++)         for (int z = k; z <= n; z++) {           DP[i + 1][j][z] += DP[i][j][z] * (1.0 - P[i]);           if (A[i] == -1)             DP[i + 1][j + 1][z] += DP[i][j][z] * P[i];           else             DP[i + 1][j + 1][min(n, z + A[i] + 1)] += DP[i][j][z] * P[i];         }     long double ats = 0;     for (int j = l; j <= n; j++)       for (int z = max(k, j); z <= n; z++) ats += DP[n + 1][j][z];     cout << setprecision(13) << fixed << ats;   }


#include <bits/stdc++.h>   using namespace std;   int mod = 1e9 + 7;   vector<long long> num;   long long gcd(long long x, long long y) {     if (y > x) return gcd(y, x);     return x % y ? gcd(y, x % y) : y;   }   int a[3];   vector<vector<int> > grid;   vector<vector<int> > dist;   void bfs(int x, vector<bool>& vis) {     int n = vis.size();     queue<int> q;     q.push(a[x]);     vis[a[x]] = 1;     dist[a[x]][x] = 0;     int step = 1;     while (q.size()) {       int t = q.size();       while (t--) {         int tmp = q.front();         q.pop();         for (int i = 0; i < grid[tmp].size(); i++) {           int next = grid[tmp][i];           if (!vis[next]) {             dist[next][x] = step;             vis[next] = 1;             q.push(next);           }         }       }       step++;     }     return;   }   long long shu[63];   int find(long long x) {     for (int i = 0; i < 63; i++) {       if (shu[i] >= x) return i;     }     return 63;   }   long long zhuan(string s) {     int n = s.size();     long long ans = 0;     for (int i = 0; i < n; i++) {       ans = ans * 2 + (long long)(s[i] -  0 );     }     return ans;   }   int main() {     int n;     cin >> n;     while (n--) {       long long t;       cin >> t;       t--;       long long pos = t % 3;       t = t / 3;       long long sum = 1;       int cnt = 1;       while (sum <= t) {         t -= sum;         sum *= 4;         cnt += 2;       }       string ans =   ;       if (pos == 0)         ans +=  1 ;       else {         if (pos == 1)           ans +=  10 ;         else           ans +=  11 ;       }       string str[12] = { 00 ,  01 ,  10 ,  11 ,  00 ,  10 ,                          11 ,  01 ,  00 ,  11 ,  01 ,  10 };       while (ans.size() < cnt) {         ans.insert((pos == 0 ? 1 : 2), str[pos * 4 + t % 4]);         t /= 4;       }       cout << zhuan(ans) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool query = 0;   struct line {     long long m, b;     mutable function<const line*()> succ;     bool operator<(const line rhs) const {       if (!query) return (m < rhs.m);       const line* s = succ();       return s ? (b - s->b) < (rhs.m * (s->m - m)) : 0;     }   };   struct hull : multiset<line> {     bool bad(iterator y) {       auto z = next(y);       if (y == begin()) {         if (z == end()) return 0;         return (y->m == z->m) && (y->b <= z->b);       }       auto x = prev(y);       if (z == end()) return (y->m == x->m) && (y->b <= x->b);       return (1.0) * (x->b - y->b) * (z->m - y->m) >=              (1.0) * (y->b - z->b) * (y->m - x->m);     }     void add(long long m, long long b) {       auto it = insert({m, b});       it->succ = [=] { return (next(it) == end()) ? 0 : &*next(it); };       if (bad(it)) {         erase(it);         return;       }       while (next(it) != end() && bad(next(it))) erase(next(it));       while (it != begin() && bad(prev(it))) erase(prev(it));     }     long long eval(long long x) {       query = 1;       line l = *lower_bound({x, 0});       query = 0;       return (l.m * x + l.b);     }   };   hull h;   int N;   int A[200005];   long long SumA[200005];   long long SumAi[200005];   long long Solve() {     for (int i = 1; i <= N; i++) {       SumA[i] = SumA[i - 1] + A[i];       SumAi[i] = SumAi[i - 1] + (1LL * A[i] * i);     }     long long Ans = -9e18;     for (int i = 1; i <= N; i++) {       h.add(-i, -SumA[i - 1]);       Ans = max(Ans, h.eval(-A[i]) - 1LL * A[i] * i + SumA[i - 1] + SumAi[N]);     }     h.clear();     for (int i = N; i; i--) {       h.add(i, -SumA[i]);       Ans = max(Ans, h.eval(A[i]) - 1LL * A[i] * i + SumA[i] + SumAi[N]);     }     return Ans;   }   int main() {     scanf( %d , &N);     for (int i = 1; i <= N; i++) scanf( %d , &A[i]);     printf( %I64d n , Solve());   }


#include <bits/stdc++.h>   using namespace std;   int triangle(long long n) {     if (n == 1)       return 1;     else       return n + triangle(n - 1);   }   int heart(long long a) {     if (a == 1)       return 1;     else       return (6 * (a - 1)) + heart(a - 1);   }   int balls(long long a) {     if (a == 1)       return 1;     else       return heart(a) + (triangle(a - 1) * 6);   }   int main() {     long long a, b;     cin >> a;     b = balls(a);     cout << b;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   set<int> nx[1000100];   vector<pair<int, int> > nw;   int a[1000100];   vector<pair<int, int> > ans;   set<pair<int, int> > order;   set<pair<int, int> > lst;   int main() {     ios_base ::sync_with_stdio(0);     cin.tie();     cout.tie();     srand(23095);     cin >> n >> m;     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       x--;       y--;       nx[x].insert(y);       nx[y].insert(x);       a[x]++;       a[y]++;     }     for (int i = 0; i < n; i++) {       a[i] = (a[i] / 2) + (a[i] % 2);       order.insert(make_pair(a[i], i));     }     while (!order.empty()) {       int v = order.begin()->second;       order.erase(order.begin());       nw.clear();       for (auto u : nx[v]) {         nw.push_back(make_pair(a[u], u));       }       sort(nw.rbegin(), nw.rend());       for (int j = 0; a[v] > 0; j++) {         a[v]--;         if (a[nw[j].second] > 0) {           order.erase(make_pair(a[nw[j].second], nw[j].second));         }         a[nw[j].second]--;         if (a[nw[j].second] > 0) {           order.insert(make_pair(a[nw[j].second], nw[j].second));         }         ans.push_back(make_pair(v, nw[j].second));         nx[v].erase(nw[j].second);         nx[nw[j].second].erase(v);       }     }     cout << ans.size() <<   n ;     for (auto i : ans) {       cout << i.first + 1 <<     << i.second + 1 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[201010];   int main() {     long long n, m, k;     while (~scanf( %lld%lld%lld , &n, &m, &k)) {       memset(a, 0, sizeof(a));       long long i, j, ans = 0, sum = k;       for (i = 1; i <= n; i++) scanf( %lld , &a[i]);       for (i = n; i >= 1; i--) {         if (a[i] > k) continue;         if (a[i] <= sum) {           sum -= a[i];           ans++;         } else {           m--;           if (m <= 0) break;           sum = k;           i++;         }       }       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long power(long long a, long long b) {     if (b == 1) return a;     if (b == 0) return 1;     long long ans = power(a, b / 2);     ans = (ans * ans);     if (b & 1) ans = (ans * a);     return ans;   }   void solve() {     long long n, k;     cin >> n >> k;     if (n == 1) {       cout << 0 <<   n ;       return;     }     if (k == 1) {       cout << n - 1 <<   n ;       return;     }     long long x = 1;     long long cnt = 0;     while (x < k && n - x >= 0) {       cnt++;       x *= 2;     }     n -= x;     cnt += (n + k - 1) / k;     cout << cnt <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long tt = 1;     cin >> tt;     while (tt--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int a, long long int b) {     if (a == 0) {       return b;     }     if (b == 0) {       return a;     }     return gcd(b, a % b);   }   long long int power(long long int x, long long int n) {     long long ans = 1;     while (n > 0) {       if (n & 1) ans = (ans * x) % 1000000007;       x = (x * x) % 1000000007;       n /= 2;     }     if (x == 0) ans = 0;     return ans;   }   long long int getsum(int g, int a[], int n, int x) {     long long int i = n - 1, tmp = 0, k = g, count = 0;     while (i >= 0) {       while (k != 0 && i >= 0) {         tmp += max((long long)0, (a[i] - count));         i--;         k--;       }       count++;       k = g;     }     return tmp;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     while (t--) {       int n, m, i;       cin >> n >> m;       int a[n];       for (i = 0; i < n; i++) cin >> a[i];       sort(a, a + n);       int lo = 1, hi = n, mid;       while (lo < hi) {         mid = lo + (hi - lo) / 2;         long long int tmp = getsum(mid, a, n, m);         if (tmp < m) {           lo = mid + 1;         } else {           hi = mid;         }       }       if (getsum(lo, a, n, m) >= m) {         cout << lo;       } else {         cout << -1;       }     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename... Args>   void trace(Args... args) {}   template <typename... Args>   void recInit(Args... args) {}   template <typename... Args>   void recEnd(Args... args) {}   struct node {     long long sum, lp, rp, L, R, sz;   } tree[4 * 200020], np;   node merge(node l, node r) {     node id;     id.sz = l.sz + r.sz;     id.L = l.L;     id.R = r.R;     if (l.lp == l.sz) {       id.lp = l.sz + (l.R <= r.L) * r.lp;     } else {       id.lp = l.lp;     }     if (r.rp == r.sz) {       id.rp = r.sz + (l.R <= r.L) * l.rp;     } else {       id.rp = r.rp;     }     id.sum = l.sum + r.sum;     if (l.R <= r.L) {       id.sum += l.rp * r.lp;     }     return id;   }   void build(int id, int l, int r, long long* a) {     if (l == r) {       tree[id] = node({1, 1, 1, a[l], a[l], 1});       return;     }     int m = (l + r) / 2;     build(id * 2, l, m, a);     build(id * 2 + 1, m + 1, r, a);     tree[id] = merge(tree[id * 2], tree[id * 2 + 1]);   }   void update(int id, int l, int r, int x, long long y) {     if (r < x || x < l) return;     if (l == r) {       tree[id] = node({1, 1, 1, y, y, 1});       return;     }     int m = (l + r) / 2;     update(id * 2, l, m, x, y);     update(id * 2 + 1, m + 1, r, x, y);     tree[id] = merge(tree[id * 2], tree[id * 2 + 1]);   }   node query(int id, int l, int r, int lq, int rq) {     if (lq > rq || r < lq || rq < l) return np;     if (lq <= l && r <= rq) return tree[id];     int m = (l + r) / 2;     return merge(query(id * 2, l, m, lq, rq),                  query(id * 2 + 1, m + 1, r, lq, rq));   }   long long t = 1, n, q, ans, a[200020];   int main() {     np = node({0, 0, 0, -10000000000LL, 10000000000LL, 0});   st:     ans = 0;     scanf( %lld %lld , &n, &q);     for (int i = 1; i <= n; i++) {       scanf( %lld , &a[i]);     }     build(1, 1, n, a);     while (q--) {       long long t, x, y;       scanf( %lld %lld %lld , &t, &x, &y);       if (t == 1) {         a[x] = y;         update(1, 1, n, x, y);       } else if (t == 2) {         printf( %lld n , query(1, 1, n, x, y).sum);       } else         assert(false);     }     if (--t) goto st;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void read(T &x) {     int f = 0, c = getchar();     x = 0;     while (!isdigit(c)) f |= c == 45, c = getchar();     while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();     if (f) x = -x;   }   template <typename T, typename... Args>   inline void read(T &x, Args &...args) {     read(x);     read(args...);   }   const int maxn = 2e5 + 50;   int fa[maxn], bj[maxn], n, m, ans;   struct node {     int x, y, z;   } e[maxn];   bool cmp(node a, node b) { return a.z > b.z; }   inline int find(int x) {     if (fa[x] == x) return x;     return fa[x] = find(fa[x]);   }   signed main() {     read(n, m);     for (int i = 1; i <= n; i++) fa[i] = i;     for (int i = 1; i <= m; i++) read(e[i].x, e[i].y, e[i].z);     stable_sort(e + 1, e + 1 + m, cmp);     for (int i = 1; i <= m; i++) {       int f1 = find(e[i].x), f2 = find(e[i].y);       if (f1 == f2) {         if (bj[f1] == 0)           bj[f1] = 1, ans += e[i].z;         else           continue;       } else {         if (bj[f1] + bj[f2] == 0) fa[f1] = f2, ans += e[i].z;         if (bj[f1] == 1 && bj[f2] == 0) fa[f2] = f1, ans += e[i].z;         if (bj[f1] == 0 && bj[f2] == 1) fa[f1] = f2, ans += e[i].z;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char mat[55][55];   int n;   int A[] = {-1, 0, 1, 0};   int B[] = {0, 1, 0, -1};   vector<pair<int, int> > ilha1, ilha2;   void flood_fill(int i, int j, bool prima) {     if (i < 0 or j < 0 or i >= n or j >= n or mat[i][j] !=  0 ) return;     mat[i][j] =  1 ;     if (prima)       ilha1.push_back({i, j});     else       ilha2.push_back({i, j});     for (int k = 0; k < 4; k++) {       flood_fill(i + A[k], j + B[k], prima);     }   }   int main() {     pair<int, int> p1, p2;     scanf( %d , &n);     scanf( %d%d%d%d , &p1.first, &p1.second, &p2.first, &p2.second);     p1.first--;     p1.second--;     p2.first--;     p2.second--;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         scanf(  %c , &mat[i][j]);       }     }     flood_fill(p1.first, p1.second, true);     if (mat[p2.first][p2.second] ==  1 ) {       puts( 0 );       return 0;     }     flood_fill(p2.first, p2.second, false);     int ans = INT_MAX;     for (int i = 0; i < (int)ilha1.size(); i++) {       for (int j = 0; j < (int)ilha2.size(); j++) {         int a = abs(ilha1[i].first - ilha2[j].first);         int b = abs(ilha1[i].second - ilha2[j].second);         ans = min(ans, a * a + b * b);       }     }     cout << ans << endl;     return 0;   }