#include <bits/stdc++.h>   using namespace std;   long long dp[5050][505];   pair<long long, long long> pre[5050][505];   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long d, s;     cin >> d >> s;     queue<pair<long long, long long>> q;     for (long long i = 1; i < 10; i++) {       dp[i][i % d] = 1;       q.push({i, i % d});     }     while (q.size()) {       pair<long long, long long> curr = q.front();       q.pop();       long long sum = curr.first;       long long div = curr.second;       for (long long i = 0; i <= 9; i++) {         long long curr_sum = sum + i;         long long curr_div = (div * 10 + i) % d;         if (curr_sum <= s and !dp[curr_sum][curr_div]) {           dp[curr_sum][curr_div] = dp[sum][div] + 1;           q.push({curr_sum, curr_div});           pre[curr_sum][curr_div] = {sum, div};         }       }     }     if (!dp[s][0]) {       cout << -1;     } else {       vector<long long> ans;       long long sum = s, div = 0;       while (true) {         if (dp[sum][div] == 1) {           ans.push_back(sum);           break;         } else {           pair<long long, long long> curr = pre[sum][div];           ans.push_back(sum - curr.first);           sum = curr.first;           div = curr.second;         }       }       reverse(ans.begin(), ans.end());       for (auto x : ans) {         cout << x;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1010;   struct Modify {     bool add;     int a, b;   };   int n, m, t;   vector<int> x[MAXN];   map<int, int> h;   vector<int> s;   vector<Modify> ans;   int losestate[1 << 20];   int winstate[1 << 20];   void BuildBase() {     int out[MAXN];     vector<int> y[MAXN];     for (int i = 1; i <= n; ++i) {       out[i] = x[i].size();       y[i].clear();     }     for (int i = 1; i <= n; ++i) {       for (int b : x[i]) y[b].push_back(i);     }     queue<int> q;     for (int i = 1; i <= n; ++i) {       if (out[i] == 0) {         q.push(i);       }     }     while (!q.empty() && s.size() < 20) {       int a = q.front();       q.pop();       s.push_back(a);       h[a] = s.size() - 1;       for (int b : y[a]) {         --out[b];         if (out[b] == 0) q.push(b);       }     }     int z[20][20];     memset(z, 0, sizeof(z));     for (int i = 0; i < s.size(); ++i) {       for (int b : x[s[i]]) z[i][h[b]] = true;     }     for (int i = 0; i < s.size(); ++i) {       for (int j = 0; j < s.size(); ++j) {         if (z[i][j] && i <= j) {           ans.push_back({false, s[i], s[j]});         }         if (!z[i][j] && i > j) {           ans.push_back({true, s[i], s[j]});         }       }     }     for (int i = 0; i < s.size(); ++i) {       losestate[1 << i] = s[i];     }   }   void ModifyGraph() {     bool sz[1 << 20];     memset(sz, 0, sizeof(sz));     sz[0] = true;     for (int i = 1; i <= n; ++i) {       if (h.find(i) != h.end()) continue;       ans.push_back({true, i, i});       int state = 0;       for (int b : x[i]) {         if (h.find(b) == h.end()) continue;         state |= (1 << h[b]);       }       if (!sz[state]) {         sz[state] = true;         winstate[state] = i;         continue;       }       bool finished = false;       for (int p = 0; p < 20; ++p) {         int newst = state ^ (1 << p);         if (sz[newst]) continue;         finished = true;         sz[newst] = true;         winstate[newst] = i;         if (state & (1 << p)) {           ans.push_back({false, i, s[p]});         } else {           ans.push_back({true, i, s[p]});         }         if (finished) break;       }       if (finished) continue;       for (int p = 0; p < 20; ++p) {         for (int q = p + 1; q < 20; ++q) {           int newst = state ^ (1 << p) ^ (1 << q);           if (sz[newst]) continue;           finished = true;           sz[newst] = true;           winstate[newst] = i;           for (int o : {p, q}) {             if (state & (1 << o)) {               ans.push_back({false, i, s[o]});             } else {               ans.push_back({true, i, s[o]});             }           }           if (finished) break;         }         if (finished) break;       }       if (finished) continue;       for (int p = 0; p < 20; ++p) {         for (int q = p + 1; q < 20; ++q) {           for (int r = q + 1; r < 20; ++r) {             int newst = state ^ (1 << p) ^ (1 << q) ^ (1 << r);             if (sz[newst]) continue;             finished = true;             sz[newst] = true;             winstate[newst] = i;             for (int o : {p, q, r}) {               if (state & (1 << o)) {                 ans.push_back({false, i, s[o]});               } else {                 ans.push_back({true, i, s[o]});               }             }             if (finished) break;           }           if (finished) break;         }         if (finished) break;       }     }   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m >> t;     for (int i = 0; i < m; ++i) {       int a, b;       cin >> a >> b;       x[a].push_back(b);     }     BuildBase();     ModifyGraph();     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); ++i) {       if (ans[i].add)         cout <<  +  ;       else         cout <<  -  ;       cout << ans[i].a <<     << ans[i].b << endl;     }     cout << flush;     while (t--) {       int win = 0, lose = 0, draw = 0;       for (int i = 0; i < s.size(); ++i) {         cout <<  ? 1   << s[i] << endl << flush;         string st;         cin >> st;         if (st ==  Win ) {           win |= (1 << i);         } else if (st ==  Lose ) {           lose |= (1 << i);         } else {           draw |= (1 << i);         }       }       int ans = 0;       if (lose > 0) {         ans = losestate[lose];       } else {         ans = winstate[win];       }       cout <<  !   << ans << endl << flush;       string st;       cin >> st;       if (st !=  Correct ) return 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <int MOD, int RT>   struct Mint {     static const int mod = MOD;     static constexpr Mint rt() { return RT; }     int v;     explicit operator int() const { return v; }     Mint() { v = 0; }     Mint(long long _v) {       v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD);       if (v < 0) v += MOD;     }     friend bool operator==(const Mint& a, const Mint& b) { return a.v == b.v; }     friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); }     friend bool operator<(const Mint& a, const Mint& b) { return a.v < b.v; }     friend bool operator>(const Mint& a, const Mint& b) { return a.v > b.v; }     friend bool operator<=(const Mint& a, const Mint& b) { return a.v <= b.v; }     friend bool operator>=(const Mint& a, const Mint& b) { return a.v >= b.v; }     friend istream& operator>>(istream& in, Mint& a) {       long long x;       cin >> x;       a = Mint(x);       return in;     }     friend ostream& operator<<(ostream& os, const Mint& a) { return os << a.v; }     Mint& operator+=(const Mint& m) {       if ((v += m.v) >= MOD) v -= MOD;       return *this;     }     Mint& operator-=(const Mint& m) {       if ((v -= m.v) < 0) v += MOD;       return *this;     }     Mint& operator*=(const Mint& m) {       v = (long long)v * m.v % MOD;       return *this;     }     Mint& operator/=(const Mint& m) { return (*this) *= inv(m); }     friend Mint pow(Mint a, long long p) {       Mint ans = 1;       assert(p >= 0);       for (; p; p /= 2, a *= a)         if (p & 1) ans *= a;       return ans;     }     friend Mint inv(const Mint& a) {       assert(a.v != 0);       return pow(a, MOD - 2);     }     Mint operator-() const { return Mint(-v); }     Mint& operator++() { return *this += 1; }     Mint& operator--() { return *this -= 1; }     friend Mint operator+(Mint a, const Mint& b) { return a += b; }     friend Mint operator-(Mint a, const Mint& b) { return a -= b; }     friend Mint operator*(Mint a, const Mint& b) { return a *= b; }     friend Mint operator/(Mint a, const Mint& b) { return a /= b; }   };   namespace FFT {   template <class T>   void fft(vector<T>& A, bool inv = 0) {     int n = (int)A.size();     assert((T::mod - 1) % n == 0);     vector<T> B(n);     for (int b = n / 2; b; b /= 2, A.swap(B)) {       T w = pow(T::rt(), (T::mod - 1) / n * b);       T m = 1;       for (int i = 0; i < n; i += b * 2, m *= w)         for (int j = 0; j < b; j++) {           T u = A[i + j];           T v = A[i + j + b] * m;           B[i / 2 + j] = u + v;           B[i / 2 + j + n / 2] = u - v;         }     }     if (inv) {       reverse(1 + A.begin(), A.end());       T z = T(1) / T(n);       for (auto& t : A) t *= z;     }   }   template <class T>   vector<T> multiply(vector<T> A, vector<T> B) {     int sa = (int)A.size();     int sb = (int)B.size();     if (!min(sa, sb)) return {};     int s = sa + sb - 1;     int n = 1;     for (; n < s; n *= 2)       ;     bool eq = A == B;     A.resize(n);     fft(A);     if (eq)       B = A;     else       B.resize(n), fft(B);     for (int i = 0; i < n; i++) A[i] *= B[i];     fft(A, 1);     A.resize(s);     return A;   }   template <class T>   vector<T> operator*(const vector<T>& a, const vector<T>& b) {     auto c = multiply(a, b);     return c;   }   template <class T>   vector<T> operator+(const vector<T>& a, const vector<T>& b) {     vector<T> c = a;     c.resize(max(a.size(), b.size()));     for (int i = 0; i < b.size(); ++i) {       c[i] += b[i];     }     return c;   }   template <class M, class T>   vector<M> multiply_mod(vector<T> x, vector<T> y) {     auto convert = [](const vector<T>& v) {       vector<M> w((int)v.size());       for (int i = 0; i < (int)v.size(); i++) w[i] = (int)v[i];       return w;     };     return multiply(convert(x), convert(y));   }   template <class T>   vector<T> general_multiply(const vector<T>& A, const vector<T>& B) {     using m0 = Mint<(119 << 23) + 1, 62>;     using m1 = Mint<(5 << 25) + 1, 62>;     using m2 = Mint<(7 << 26) + 1, 62>;     auto c0 = multiply_mod<m0>(A, B);     auto c1 = multiply_mod<m1>(A, B);     auto c2 = multiply_mod<m2>(A, B);     int n = (int)c0.size();     vector<T> res(n);     m1 r01 = 1 / m1(m0::mod);     m2 r02 = 1 / m2(m0::mod);     m2 r12 = 1 / m2(m1::mod);     for (int i = 0; i < n; i++) {       int a = c0[i].v;       int b = ((c1[i] - a) * r01).v;       int c = (((c2[i] - a) * r02 - b) * r12).v;       res[i] = (T(c) * m1::mod + b) * m0::mod + a;     }     return res;   }   }  // namespace FFT   using namespace FFT;   using mi = Mint<998244353, 5>;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     auto fail = []() {       cout << 0 <<   n ;       exit(0);     };     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     vector<int> blocks;     while (!a.empty()) {       int x = a.back();       if (a.size() < x) {         fail();       }       blocks.push_back(x == 1);       for (int i = 0; i < x; ++i) {         if (a.back() != x) {           fail();         }         a.pop_back();       }     }     reverse(blocks.begin(), blocks.end());     int sz = blocks.size();     const mi HALF = 1 / mi(2);     function<vector<vector<mi>>(int, int)> solve = [&](int l, int r) {       vector<vector<mi>> res(4);       if (r - l + 1 == 1) {         res[0] = vector<mi>{1};         res[1] = vector<mi>{0, 2};         res[2] = vector<mi>{1};         if (blocks[l]) {           res[3] = vector<mi>{0, 1};         } else {           res[3] = vector<mi>{0, 2};         }         return res;       }       int m = (l + r) >> 1;       auto x = solve(l, m);       auto y = solve(m + 1, r);       res[0] = x[0] * y[0] + x[1] * y[2];       res[1] = x[0] * y[1] + x[1] * y[3];       res[2] = x[2] * y[0] + x[3] * y[2];       res[3] = x[3] * y[3] + x[2] * y[1];       return res;     };     auto res = solve(0, sz - 1)[3];     vector<mi> fact(n + 1);     fact[0] = 1;     for (int i = 1; i <= n; ++i) {       fact[i] = fact[i - 1] * i;     }     mi ans = 0;     for (int i = 1; i < res.size(); ++i) {       ans += res[i] * fact[i] * (i % 2 == res.size() % 2 ? -1 : 1);     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a, am, duong, zero;   unsigned long long t;   int main() {     scanf( %d n , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a);       if (a < 0)         am++;       else if (a > 0)         duong++;       else         zero++;       t += abs(abs(a) - 1);     }     if (am % 2 == 1) {       if (zero == 0) t += 2;     }     printf( %llu , t);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int sum = 0, a;     for (int i = 0; i < n; i++) {       cin >> a;       sum += a;     }     int m;     cin >> m;     vector<char> good(1e5 + 1, false);     int l, r;     for (int i = 0; i < m; i++) {       cin >> l >> r;       for (int j = l; j <= r; j++) {         good[j] = true;       }     }     for (int i = sum; i <= 1e5; i++) {       if (good[i]) {         cout << i << endl;         return 0;       }     }     cout << -1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a;     cin >> a;     int i, flag = 0;     for (i = 1; i < a.size(); i++) {       if (a[i] ==  1 ) {         flag = 1;         break;       }     }     i = (a.size()) / 2;     if (flag == 1 && a.size() % 2 == 1) i++;     cout << i;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T sqr(T x) {     return x * x;   }   template <typename T>   T abs(T x) {     return x < 0 ? -x : x;   }   template <typename T>   T gcd(T a, T b) {     return b ? gcd(b, a % b) : a;   }   const int MAXN = 8009;   const int MAXX = (int)1e+6 + 7;   struct triple {     int x, l, r;     triple(int x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {}   };   int s[MAXN];   vector<triple> v[MAXN];   int lg[MAXX];   int upd[MAXX], cnt;   int n, m;   int cpwr[MAXX];   int count_power(int l, int r) { return lg[r] - lg[l - 1]; }   int main(int argc, char **argv) {     ios_base::sync_with_stdio(false);     lg[0] = -1;     lg[1] = 0;     for (int i = 2; i < MAXX; ++i) {       lg[i] = lg[i >> 1] + 1;     }     cin >> n >> m;     s[1] = 1;     for (int i = 2; i <= n; ++i) {       s[i] = s[i - 1] + lg[s[i - 1]] + 1;     }     while (m--) {       int t;       cin >> t;       if (t == 1) {         int l, r, x;         cin >> t >> l >> r >> x;         v[t].push_back(triple(x, l, r));       } else {         int l, r, ans = 0;         cin >> t >> l;         r = l;         ++cnt;         for (; t <= n; ++t) {           for (size_t i = 0; i < v[t].size(); ++i) {             if (l <= v[t][i].r && v[t][i].l <= r && upd[v[t][i].x] != cnt) {               upd[v[t][i].x] = cnt;               ++ans;             }           }           int len = r - l + 1;           int k = count_power(l, r);           l += count_power(1, l - 1);           r = l + len - 1 + k;         }         cout << ans <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   int n;   int sum = 0;   int a[16];   int mat[4][4];   int rows[4], cols[4];   int gotx[4], goty[4], gotd[2];   int x[16], y[16];   int diag[2];   bool dfs(int state, int mask) {     for (int i = 0; i < 2; i++) {       if (gotd[i] == n && diag[i] != sum) {         return false;       }     }     if (state == n * n) return true;     int row = x[state];     int col = y[state];     bool d1 = row == col;     bool d2 = row + col == n - 1;     for (int i = 0; i < n * n; i++) {       if ((mask & (1 << i)) != 0 && rows[row] + a[i] <= sum &&           cols[col] + a[i] <= sum && (!d1 || diag[0] + a[i] <= sum) &&           (!d2 || diag[1] + a[i] <= sum)) {         if (gotx[row] + 1 == n && rows[row] + a[i] < sum) {           continue;         }         if (goty[col] + 1 == n && cols[col] + a[i] < sum) {           continue;         }         gotx[row]++;         goty[col]++;         rows[row] += a[i];         cols[col] += a[i];         if (d1) {           gotd[0]++;           diag[0] += a[i];         }         if (d2) {           gotd[1]++;           diag[1] += a[i];         }         if (dfs(state + 1, mask ^ (1 << i))) {           mat[row][col] = a[i];           return true;         }         gotx[row]--;         goty[col]--;         if (d1) {           gotd[0]--;           diag[0] -= a[i];         }         if (d2) {           gotd[1]--;           diag[1] -= a[i];         }         rows[row] -= a[i];         cols[col] -= a[i];       }     }     return false;   }   int main() {     std::ios_base::sync_with_stdio(false);     std::cin.tie(NULL);     std::cin >> n;     int mn = 1e9;     long long ans = 0;     for (int i = 0; i < n * n; i++) {       std::cin >> a[i];       mn = std::min(mn, a[i]);       ans += a[i];     }     std::cout << ans / n << std::endl;     if (n == 1) {       std::cout << a[0] << std::endl;       return 0;     } else if (n == 2) {       x[0] = 0;       x[1] = 1;       x[2] = 0;       x[3] = 1;       y[0] = 0;       y[1] = 1;       y[2] = 1;       y[3] = 0;     } else if (n == 3) {       x[0] = 0;       x[1] = 1;       x[2] = 2;       x[3] = 0;       x[4] = 2;       x[5] = 0;       x[6] = 1;       x[7] = 1;       x[8] = 2;       y[0] = 0;       y[1] = 1;       y[2] = 2;       y[3] = 2;       y[4] = 0;       y[5] = 1;       y[6] = 0;       y[7] = 2;       y[8] = 1;     } else if (n == 4) {       int wtfx[] = {0, 1, 2, 3, 0, 1, 2, 3, 0, 0, 1, 1, 2, 2, 3, 3};       int wtfy[] = {0, 1, 2, 3, 3, 2, 1, 0, 1, 2, 0, 3, 0, 3, 1, 2};       for (int i = 0; i < 16; i++) {         x[i] = wtfx[i];         y[i] = wtfy[i];       }     }     long long cur = 0;     for (int i = 0; i < n * n; i++) {       a[i] -= mn;       cur += a[i];     }     srand(time(0));     std::random_shuffle(a, a + n * n);     sum = cur / n;     dfs(0, (1 << (n * n)) - 1);     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         std::cout << mat[i][j] + mn << (j + 1 == n ?   n  :    );       }     }   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     vector<long long> a(n), b(n);     for (long long i = 0; i < n; ++i) cin >> a[i];     for (long long i = 0; i < n; ++i) cin >> b[i];     map<long long, vector<long long> > mp;     long long m;     cin >> m;     for (long long i = 0; i < n; ++i) {       mp[a[i]].push_back(i + 1);       mp[b[i]].push_back(i + 1);     }     long long ans = 1;     for (auto it = mp.begin(); it != mp.end(); ++it) {       long long den = 0;       long long num = it->second.size();       sort(it->second.begin(), it->second.end());       for (long long i = 1; i < (it->second.size()); ++i) {         if ((it->second[i]) == (it->second[i - 1])) den++;       }       long long nn = 1;       for (long long i = num; i > 0; --i) {         if (i & 1 || den == 0)           nn *= i;         else {           nn *= (i / 2);           den--;         }         if (nn > m) nn %= m;       }       ans *= nn;       if (ans > m) ans %= m;     }     cout << ans % m;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t, n, doc[100010], doc1[100010], p[100010];   vector<vector<int> > g;   vector<pair<int, pair<int, int> > > q;   vector<int> tin, tout;   int par(int u) {     if (p[u] == u) return u;     return p[u] = par(p[u]);   }   void dfs(int u) {     tin[u] = t;     for (int i = 0; i < g[u].size(); i++)       if (tin[g[u][i]] == -1) {         t++;         dfs(g[u][i]);       }     tout[u] = ++t;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int m, d = 0;     cin >> n >> m;     g.resize(n);     tin.resize(n, -1);     tout.resize(n);     for (int i = 0; i < n; i++) p[i] = i;     for (int i = 0; i < m; i++) {       int type, x, y;       cin >> type;       if (type == 1) {         cin >> x >> y;         x--;         y--;         p[x] = y;         g[y].push_back(x);       } else if (type == 2) {         cin >> x;         x--;         doc1[d] = par(x);         doc[d++] = x;       } else {         cin >> x >> y;         x--;         y--;         int a = 0;         if (par(x) == par(doc[y])) a = 1;         q.push_back(make_pair(a, make_pair(x, y)));       }     }     t = 0;     for (int i = 0; i < n; i++)       if (p[i] == i) dfs(i);     for (int i = 0; i < q.size(); i++) {       if (q[i].first == 0) {         cout <<  NO n ;         continue;       }       int u = q[i].second.first, v = doc[q[i].second.second],           x = doc1[q[i].second.second];       if (tin[u] <= tin[v] && tout[u] >= tout[v] && tin[x] <= tin[u] &&           tout[x] >= tout[u])         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     unsigned long long int counter = 0, a, b, t;     cin >> a >> b;     while (true) {       if (b > a) {         t = a;         a = b;         b = t;       }       counter = counter + (a / b);       a = (a % b);       if (a == 0) break;     }     cout << counter;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000;   int ans[N], l[N], r[N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> l[i];     }     for (int i = 0; i < n; ++i) {       cin >> r[i];     }     for (int k = n; k > 0; --k) {       for (int i = 0; i < n; ++i) {         if (l[i] == 0 && r[i] == 0 && ans[i] == 0) {           ans[i] = k;         }       }       int prefix = 0;       for (int i = 0; i < n; ++i) {         if (ans[i] == k) {           ++prefix;         } else if (ans[i] == 0) {           l[i] -= prefix;         }       }       int suffix = 0;       for (int i = n - 1; i >= 0; --i) {         if (ans[i] == k) {           ++suffix;         } else if (ans[i] == 0) {           r[i] -= suffix;         }       }     }     if (count(ans, ans + n, 0) == 0) {       cout <<  YES n ;       for (int i = 0; i < n; ++i) {         cout << ans[i] <<    ;       }       cout <<   n ;     } else {       cout <<  NO n ;     }   }


#include <bits/stdc++.h>   using namespace std;   bool boy[1000], girl[1000];   int n, m, b, g, x, y, q[1000][2], h, t;   int main() {     memset(boy, false, sizeof(boy));     scanf( %d%d%d , &n, &m, &b);     for (int i = 1; i <= b; i++) {       scanf( %d , &x);       boy[x] = true;     }     memset(girl, false, sizeof(girl));     scanf( %d , &g);     for (int i = 1; i <= g; i++) {       scanf( %d , &y);       girl[y] = true;     }     h = t = 0;     for (int i = 0; i < n; i++)       if (boy[i]) {         t++;         q[t][1] = 0;         q[t][2] = i;       }     for (int i = 0; i < m; i++)       if (girl[i]) {         t++;         q[t][1] = 1;         q[t][2] = i;       }     while (h < t) {       h++;       if (q[h][1]) {         int u = q[h][2] % n;         for (int i = 1; i <= n; i++) {           if (!boy[u]) {             boy[u] = true;             t++;             q[t][1] = 0;             q[t][2] = u;           }           u = (u + m) % n;         }       } else {         int u = q[h][2] % m;         for (int i = 1; i <= m; i++) {           if (!girl[u]) {             girl[u] = true;             t++;             q[t][1] = 1;             q[t][2] = u;           }           u = (u + n) % m;         }       }     }     if (t == n + m)       puts( Yes );     else       puts( No );     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int n;   string a;   constexpr char t[4] = { A ,  C ,  T ,  G };   int best = INT_MAX;   int main() {     cin >> n;     cin >> a;     for (int i = 0; i < n - 3; ++i) {       int cur = 0;       for (int j = 0; j < 4; ++j) {         int temp = min(abs(t[j] - a[i + j]), abs(t[j] + 26 - a[i + j]));         temp = min(temp, abs(t[j] - a[i + j] - 26));         cur += temp;       }       best = min(best, cur);     }     cout << best;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void prn(T first) {     cout << first <<   n ;   }   int main() {     int a[4];     cin >> a[0] >> a[1] >> a[2] >> a[3];     sort(a, a + 4);     if (a[0] + a[3] == a[1] + a[2] || a[3] == a[0] + a[1] + a[2]) {       cout <<  YES ;     } else {       cout <<  NO ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, h, v[1100];     multiset<long long> s;     cin >> n >> h;     for (int i = 0; i < n; i++) cin >> v[i];     long long k, ht;     for (int i = 0; i < n; i++) {       s.insert(v[i]);       k = 0;       ht = 0;       for (multiset<long long>::reverse_iterator rit = s.rbegin();            rit != s.rend(); ++rit) {         if (k % 2 == 0) ht += *rit;         k++;       }       if (ht > h) {         cout << i <<   n ;         return 0;       }     }     cout << n <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   pair<int, int> st[3000002];   void build(int v, int l, int r) {     if (l == r) {       st[v] = make_pair(s[l] ==  ( , s[l] ==  ) );       return;     }     int lv, rv, mid;     lv = 2 * v;     rv = 2 * v + 1;     mid = (l + r) / 2;     build(lv, l, mid);     build(rv, mid + 1, r);     st[v].first = st[rv].first + max(0, st[lv].first - st[rv].second);     st[v].second = st[lv].second + max(0, st[rv].second - st[lv].first);   }   pair<int, int> query(int v, int l, int r, int L, int R) {     if (r < L || R < l) return make_pair(0, 0);     if (L <= l && r <= R) return st[v];     pair<int, int> L1 = query(2 * v, l, (l + r) / 2, L, R);     pair<int, int> R1 = query(2 * v + 1, (l + r) / 2 + 1, r, L, R);     return make_pair(R1.first + max(0, L1.first - R1.second),                      L1.second + max(0, R1.second - L1.first));   }   int main() {     cin >> s;     int n = s.length();     build(1, 0, n - 1);     int m;     cin >> m;     for (int i = 0; i < m; ++i) {       int l, r;       cin >> l >> r;       l--;       r--;       pair<int, int> q = query(1, 0, n - 1, l, r);       cout << r - l + 1 - (q.first + q.second) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long double k, d, t, g, c, p, ilec, ilep, cp, zos;   long long dzielup(long long x, long long y) {     if (x % y == 0) return x / y;     return x / y + 1;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     cin >> k >> d >> t;     cout.precision(10);     g = dzielup(k, d);     c = g * d;     p = k + (c - k) / 2;     ilep = floor(t / p);     cp = ilep * p;     zos = t - cp;     if (zos <= k) {       cout << fixed << ilep * c + zos <<   n ;     } else {       zos -= k;       cout << fixed << ilep * c + k + 2 * zos <<   n ;     }     return 0;   }


#include <iostream>   using namespace std;      const int MOD = 1e9+7, N = 1e5+5;      long long inv(long long a) {       long long b = 1;       int k = MOD-2;       while(k) {           if(k%2) b = b * a % MOD;           a = a * a % MOD;           k /= 2;       }       return b;   }      long long fact[N], ifact[N];      long long binom(long long x, long long y) {       if(x < 0 || y < 0 || x < y) return 0;       return fact[x] * ifact[x-y] % MOD * ifact[y] % MOD;   }      int t, n, k;      int main() {       ios_base::sync_with_stdio(false); cin.tie(0);       fact[0] = ifact[0] = 1;       for (int i = 1; i < N; i++) fact[i] = fact[i-1] * i % MOD;       ifact[N-1] = inv(fact[N-1]);       for (int i = N-2; 0 < i; i--) ifact[i] = ifact[i+1] * (i+1) % MOD;       cin >> t;       while(t--) {           cin >> n >> k;           long long sol = 0;           for (long long l = 1; l <= n; l++) sol = (sol + binom(n-(l-1)*(k-1), l) * fact[l] % MOD * fact[n-l]) % MOD;           sol = (sol + fact[n]) * ifact[n] % MOD;           cout << sol <<   n ;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   void debug_out() { cerr << endl; }   template <class T>   ostream& prnt(ostream& out, T v) {     out << v.size() <<   n ;     for (auto e : v) out << e <<    ;     return out;   }   template <class T>   ostream& operator<<(ostream& out, vector<T> v) {     return prnt(out, v);   }   template <class T>   ostream& operator<<(ostream& out, set<T> v) {     return prnt(out, v);   }   template <class T1, class T2>   ostream& operator<<(ostream& out, map<T1, T2> v) {     return prnt(out, v);   }   template <class T1, class T2>   ostream& operator<<(ostream& out, pair<T1, T2> p) {     return out <<  (  << p.first <<     << p.second <<  ) ;   }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << H;     debug_out(T...);   }   const int N = 1001;   const int MOD = 998244353;   int n, dp[2][N], a[N], k, f[100100], r[N];   int solve(int x) {     for (int i = 1; i <= n; i++) {       int j = upper_bound(a + 1, a + n + 1, a[i] - x) - a;       j--;       r[i] = j;     }     for (int j = 1; j <= k; j++) {       for (int i = 1; i <= n; i++)         if (j == 1)           dp[1][i] = 1;         else           dp[j % 2][i] = dp[1 - j % 2][r[i]];       for (int i = 2; i <= n; i++)         dp[j % 2][i] += dp[j % 2][i - 1], dp[j % 2][i] %= MOD;     }     return dp[k % 2][n];   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> a[i];     sort(a + 1, a + n + 1);     int mx = a[n] / (k - 1) + 1;     for (int x = 0; x <= mx; x++) f[x] = solve(x);     long long ans = 0;     for (int x = 0; x < mx; x++) {       f[x] -= f[x + 1];       if (f[x] < 0) f[x] += MOD;       ans += (1LL * x * f[x]) % MOD;       ans %= MOD;     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int Set(int N, int pos) { return N = N | (1 << pos); }   int reset(int N, int pos) { return N = N & ~(1 << pos); }   bool check(int N, int pos) { return (bool)(N & (1 << pos)); }   inline int addmod(int x, int y) {     return (x % 1000000007 + y % 1000000007) % 1000000007;   }   inline int submod(int x, int y) {     return (x % 1000000007 - y % 1000000007 + 1000000007) % 1000000007;   }   inline int mulmod(int x, int y) {     return (x % 1000000007 * 1LL * y % 1000000007) % 1000000007;   }   inline int nextSubMask(int i, int mask) { return (i - 1) & mask; }   template <typename T>   void we_r_done(T mssg) {     cout << mssg;     exit(0);   }   vector<char> chick;   string s[102];   int n, m, c;   int row, col;   bool vis[111][111];   void fixrc(int i, int j) {     row = i;     col = j;   }   char who;   void fun(int i, int j, int baki) {     if (s[i][j] ==  R  && baki == 0) {       fixrc(i, j);       return;     }     baki -= (s[i][j] ==  R );     s[i][j] = who;     vis[i][j] = true;     if (j && vis[i][j - 1] == false) {       fun(i, j - 1, baki);     } else if ((j + 1) < m && vis[i][j + 1] == false) {       fun(i, j + 1, baki);     } else if (i)       fun(i - 1, j, baki);   }   int main() {     for (char i =  0 ; i <=  9 ; i++) chick.push_back(i);     for (char i =  A ; i <=  Z ; i++) chick.push_back(i);     for (char i =  a ; i <=  z ; i++) chick.push_back(i);     int t;     cin >> t;     while (t--) {       cin >> n >> m >> c;       int tot = 0;       for (int i = 0; i < n; i++) {         cin >> s[i];         for (int j = 0; j < m; j++) {           tot += (s[i][j] ==  R );           vis[i][j] = false;         }       }       row = n - 1;       col = m - 1;       char last;       for (int i = 0; i < c; i++) {         who = chick[i];         int cc = c - i;         cc = tot / cc + (tot % cc != 0);         tot -= cc;         fun(row, col, cc);       }       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           cout << s[i][j];         }         cout << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int t, zero, cur;   long long ans;   string s;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> t;     while (t--) {       cin >> s;       ans = zero = 0;       for (int i = 0; i < s.size(); i++) {         if (s[i] ==  0 ) {           if (i == 0 || s[i - 1] ==  1 ) {             zero = 1;           } else {             zero++;           }         } else {           cur = 0;           if (i == 0 || s[i - 1] ==  1 ) {             zero = 0;           }           int b = min(i + 18, (int)s.size());           for (int j = i; j < b; j++) {             cur = cur * 2 + (s[j] -  0 );             if ((j - i + 1) + min(zero, cur - (j - i + 1)) == cur) {               ans++;             }           }         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     std::ios::sync_with_stdio(false);     int n;     cin >> n;     string sign[n];     long long x[n];     char ans[n];     long long low = -2e9, up = 2e9;     for (int i = 0; i < n; i++) {       cin >> sign[i] >> x[i] >> ans[i];       if (sign[i] ==  >= ) {         if (ans[i] ==  Y )           low = max(low, x[i]);         else           up = min(x[i] - 1, up);       } else if (sign[i] ==  > ) {         if (ans[i] ==  Y )           low = max(low, x[i] + 1);         else           up = min(x[i], up);       } else if (sign[i] ==  <= ) {         if (ans[i] ==  Y )           up = min(x[i], up);         else           low = max(low, x[i] + 1);       } else {         if (ans[i] ==  Y )           up = min(x[i] - 1, up);         else           low = max(low, x[i]);       }     }     if (low > up)       cout <<  Impossible ;     else       cout << low;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, l, r;   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d%d , &l, &r);     }     for (int i = 0; i < n; i++) {       if (i & 1)         printf( 0 );       else         printf( 1 );     }     puts(  );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a[1000];     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     sort(a, a + n);     int res = 0;     int sum = 10;     int ress = 0;     for (int i = 0; i < n; i++) {       sum += a[i];       if (sum <= 720) {         res++;         if (sum > 360) {           ress += sum - 360;         }       }     }     cout << res <<     << ress;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = (119 << 23) + 1;   int add(int a, int b) { return (a += b) < M ? a : a - M; }   int sub(int a, int b) { return (a -= b) < 0 ? a + M : a; }   int mul(int a, int b) { return 1LL * a * b % M; }   int mod_pow(int a, int b) {     int res = 1;     while (b) {       if (b % 2) {         res = mul(res, a);       }       a = mul(a, a);       b /= 2;     }     return res;   }   int mod_inv(int a) { return mod_pow(a, M - 2); }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     vector<int> a(n);     for (auto& i : a) {       cin >> i;     }     sort(a.rbegin(), a.rend());     for (int i = 1; i < n; ++i) {       a[i] = add(a[i], a[i - 1]);     }     for (int k = 1; k <= n; ++k) {       int ans = 0;       for (int i = 0; i < n; i += k) {         int sum = a[min(i + k - 1, n - 1)];         sum = sub(sum, i ? a[i - 1] : 0);         ans = add(ans, mul(i / k, sum));       }       ans = mul(ans, mod_inv(n));       cout << ans <<    ;     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read(void) {     long long x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return f * x;   }   int n;   int main() {     n = read();     int l = 0, r = 1000000000;     cout << 1 <<     << 0 << endl;     fflush(stdout);     char ch[10];     scanf( %s , ch + 1);     bool flag = false;     if (ch[1] ==  b ) {       flag = true;     }     for (register int i = 1; i < n; ++i) {       int mid = (l + r + 1) >> 1;       cout << 1 <<     << mid << endl;       fflush(stdout);       scanf( %s , ch + 1);       if (flag) {         ch[1] = (ch[1] ==  w ) ?  b  :  w ;       }       if (ch[1] ==  w ) {         l = mid;       } else {         r = mid;       }     }     printf( 0 %d 2 %d , l, l + 1);     return 0;   }


#include <bits/stdc++.h>   int main() {     std::ios::sync_with_stdio(false);     std::string name;     std::cin >> name;     bool atFlag = false;     std::string result =   ;     result += name[0];     for (int i = 1; i < name.length(); i++) {       if (!atFlag && name.substr(i, 2) ==  at ) {         result +=  @ ;         ++i;         atFlag = true;       } else if (i + 3 < name.length() && name.substr(i, 3) ==  dot ) {         result +=  . ;         i += 2;       } else {         result += name[i];       }     }     std::cout << result;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, mm, s, it;   int a[200005], r[200005], so[200005], ne[200005];   bool u[200005];   string ss, na[200005];   vector<int> v[200005];   queue<int> q;   map<string, int> m;   inline string sb(string v) {     for (int i = 0; i < v.size(); i++)       if (v[i] <  a ) v[i] += 32;     return v;   }   inline void ope(string &x) {     x = sb(x);     if (m[x]) return;     m[x] = ++s;     for (int i = 0; i < x.size(); i++) r[s] += (x[i] ==  r );     na[s] = x;     so[s] = s;     return;   }   inline bool cmp(int x, int y) {     if (r[x] != r[y]) return r[x] < r[y];     return na[x].size() < na[y].size();   }   inline bool same(int x, int y) {     return r[x] == r[y] && na[x].size() == na[y].size();   }   inline void dfs(int x, int y) {     if (u[x]) return;     u[x] = 1;     ne[x] = y;     for (int i = 0; i < v[x].size(); i++) dfs(v[x][i], y);   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> ss;       ope(ss);       a[i] = m[ss];     }     m[  ] = 200002;     r[200002] = 2147483640;     cin >> mm;     for (int i = 1; i <= mm; i++) {       string p, q;       cin >> p >> q;       ope(p);       ope(q);       v[m[q]].push_back(m[p]);     }     sort(so + 1, so + s + 1, cmp);     for (int i = 1; i <= s; i++) dfs(so[i], so[i]);     long long rr = 0, ll = 0;     for (int i = 1; i <= n; i++) rr += r[ne[a[i]]], ll += na[ne[a[i]]].size();     cout << rr <<     << ll << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN(1e3 + 10);   int N, M, P[maxN], A[maxN][maxN], C;   void dfs(const int u) {     C++;     for (int v = 1; v <= N; v++)       if (A[u][v] && !P[v]) {         P[v] = u;         dfs(v);       }   }   int main() {     cin >> N >> M;     if (M != N - 1) {       cout <<  no ;       return 0;     }     for (int i = 0; i < M; i++) {       int u, v;       cin >> u >> v;       A[u][v] = A[v][u] = 1;     }     P[1] = -1;     dfs(1);     cout << (C == N ?  yes  :  no );   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long a, x, y;     cin >> a >> x >> y;     if (y % a == 0)       cout << -1;     else {       long long ratio = (long long)(y / a);       if (ratio > 0) {         if (((long long)(y / a)) % 2 == 1) {           if (2 * abs(x) < a)             cout << 2 * ratio - (long long)(ratio / 2);           else             cout << -1;         } else {           if (x != 0 && abs(x) < a && x < 0)             cout << 2 * ratio - (ratio / 2);           else if (x != 0 && abs(x) < a && x > 0)             cout << 2 * ratio - (ratio / 2) + 1;           else             cout << -1;         }       } else if (2 * abs(x) < a)         cout << 1;       else         cout << -1;     }   }


#include <bits/stdc++.h>   using namespace std;   int tmp;   string s;   char a[3][10] = {{ q ,  w ,  e ,  r ,  t ,  y ,  u ,  i ,  o ,  p },                    { a ,  s ,  d ,  f ,  g ,  h ,  j ,  k ,  l ,  ; },                    { z ,  x ,  c ,  v ,  b ,  n ,  m ,  , ,  . ,  / }};   char Tinh(char c) {     for (int i = 0; i < 3; i++)       for (int j = 0; j < 10; j++)         if (a[i][j] == c) return a[i][j + tmp];   }   int main() {     char ss;     cin >> ss;     if (ss ==  R )       tmp = -1;     else       tmp = 1;     cin >> s;     string res =   ;     for (int i = 0; i < s.length(); i++) {       res += Tinh(s[i]);     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e4 + 100;   int n, k;   struct node {     int sz;     int mn[15], mx[15];     void init(int a) {       sz = 0;       for (int i = 1; i <= k; i++) {         mx[i] = mn[i] = a;       }     }     void unit(node a) {       sz += a.sz;       for (int i = 1; i <= k; i++) {         mn[i] = min(mn[i], a.mn[i]);         mx[i] = max(mx[i], a.mx[i]);       }     }     bool operator<(const node& p) const {       for (int i = 1; i <= k; i++) {         if (mx[i] > p.mn[i]) return false;       }       return true;     }   };   set<node> s;   vector<set<node>::iterator> del;   int main() {     scanf( %d %d , &n, &k);     node now;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= k; j++) scanf( %d , &now.mn[j]), now.mx[j] = now.mn[j];       now.sz = 1;       set<node>::iterator l, r, iter;       r = s.upper_bound(now);       l = s.lower_bound(now);       for (iter = l; iter != r; iter++) {         now.unit(*iter);         del.push_back(iter);       }       for (int j = 0; j < del.size(); j++) {         s.erase(del[j]);       }       del.clear();       s.insert(now);       iter = s.end();       iter--;       printf( %d n , iter->sz);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int TESTS = 1;     while (TESTS--) {       int n;       cin >> n;       int a[n], f[1005] = {0};       for (int i = 0; i < n; i++) {         cin >> a[i];         f[a[i]]++;       }       int h = 0, t = 0;       for (int i = 0; i < 1005; i++) {         if (f[i]) t++;         h = max(h, f[i]);       }       cout << h <<     << t;     }     return 0;   }


#include <bits/stdc++.h>   const int S = 200003;   int n, m, h[S], nx[S], v[S], eg = 1, s[S], e[S], _ = 0, t[S], fa[S], dep[S],                                f[S], rk[S], root, a[S];   inline void egadd(int uu, int vv) {     nx[++eg] = h[uu];     h[uu] = eg;     v[eg] = vv;   }   void dfs_1(int x) {     s[x] = 1;     for (int i = h[x]; i; i = nx[i])       if (v[i] != fa[x]) {         fa[v[i]] = x;         dep[v[i]] = dep[x] + 1;         dfs_1(v[i]);         s[x] += s[v[i]];         if (s[e[x]] < s[v[i]]) e[x] = v[i];       }   }   void dfs_2(int x, int top) {     f[x] = ++_;     rk[_] = x;     t[x] = top;     if (!e[x]) return;     dfs_2(e[x], top);     for (int i = h[x]; i; i = nx[i])       if (v[i] != fa[x] && v[i] != e[x]) dfs_2(v[i], v[i]);   }   long long sg[S << 2], add[S << 2];   inline void down(int w, int l, int r) {     if (add[w]) {       int mid = (l + r) >> 1;       add[(w << 1)] += add[w];       add[(w << 1) | 1] += add[w];       sg[(w << 1)] += add[w] * (mid - l + 1);       sg[(w << 1) | 1] += add[w] * (r - mid);       add[w] = 0;     }   }   inline void sgadd(int l, int r, int ll, int rr, int k, int w) {     if (ll <= l && r <= rr) {       sg[w] += 1ll * (r - l + 1) * k;       add[w] += k;       return;     }     int mid = (l + r) >> 1;     down(w, l, r);     if (ll <= mid) sgadd(l, mid, ll, rr, k, (w << 1));     if (rr > mid) sgadd(mid + 1, r, ll, rr, k, (w << 1) | 1);     sg[w] = sg[(w << 1)] + sg[(w << 1) | 1];   }   inline void sgb(int l, int r, int w) {     if (l == r) {       sg[w] = a[rk[l]];       return;     }     int mid = (l + r) >> 1;     sgb(l, mid, (w << 1));     sgb(mid + 1, r, (w << 1) | 1);     sg[w] = sg[(w << 1)] + sg[(w << 1) | 1];   }   inline long long sgcal(int l, int r, int ll, int rr, int w) {     if (ll <= l && r <= rr) return sg[w];     int mid = (l + r) >> 1;     down(w, l, r);     long long ret = 0;     if (ll <= mid) ret += sgcal(l, mid, ll, rr, (w << 1));     if (rr > mid) ret += sgcal(mid + 1, r, ll, rr, (w << 1) | 1);     return ret;   }   inline int hang(int x, int y) {     while (true) {       if (fa[t[y]] == x) return t[y];       if (f[t[y]] <= f[x] && f[x] <= f[y]) return e[x];       y = fa[t[y]];     }   }   inline int lca(int x, int y) {     while (t[x] != t[y]) {       if (dep[t[x]] < dep[t[y]]) x ^= y ^= x ^= y;       x = fa[t[x]];     }     if (x == y) return x;     if (dep[x] > dep[y])       return y;     else       return x;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , a + i);     for (int i = 1; i < n; i++) {       int uu, vv;       scanf( %d%d , &uu, &vv);       egadd(uu, vv);       egadd(vv, uu);     }     root = 1;     dfs_1(1);     dfs_2(1, 1);     sgb(1, n, 1);     int o, x, y, z;     while (m--) {       scanf( %d%d , &o, &x);       if (o == 1)         root = x;       else if (o == 2) {         scanf( %d%d , &y, &z);         int o1, o2, o3;         o1 = lca(x, y);         if (o1 != root && (f[o1] <= f[root] && f[root] <= f[o1] + s[o1] - 1)) {           o2 = lca(x, root);           o3 = lca(y, root);           if (dep[o1] < dep[o2]) o1 = o2;           if (dep[o1] < dep[o3]) o1 = o3;           if (o1 == root)             sgadd(1, n, 1, n, z, 1);           else {             o2 = hang(o1, root);             sgadd(1, n, 1, n, z, 1);             sgadd(1, n, f[o2], f[o2] + s[o2] - 1, -z, 1);           }         } else if (o1 == root)           sgadd(1, n, 1, n, z, 1);         else           sgadd(1, n, f[o1], f[o1] + s[o1] - 1, z, 1);       } else {         if (x != root && (f[x] <= f[root] && f[root] <= f[x] + s[x] - 1)) {           int o = hang(x, root);           printf( %lld n ,                  sgcal(1, n, 1, n, 1) - sgcal(1, n, f[o], f[o] + s[o] - 1, 1));         } else if (x == root)           printf( %lld n , sgcal(1, n, 1, n, 1));         else           printf( %lld n , sgcal(1, n, f[x], f[x] + s[x] - 1, 1));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   string s;   pair<int, int> fff(const string &s, char x) {     pair<int, int> res;     int cc = 0;     for (int i = 0; i < s.length(); i++)       if (s[i] == x) cc++;     if (cc >= k) {       res.first = 0;       res.second = 0;       return res;     }     int cost = 0;     for (int i = 1; i <= 9; i++)       for (int j = 0; j < s.length(); j++)         if (abs(s[j] - x) == i) {           cc++;           cost += abs(s[j] - x);           if (cc == k) {             res.first = cost;             res.second = i;             return res;           }         }   }   string change(string s, char x, int d) {     int cc = 0;     for (int i = 0; i < s.length(); i++)       if (abs(s[i] - x) < d) {         cc++;         s[i] = x;       }     if (cc == k) return s;     for (int i = 0; i < s.length(); i++)       if (s[i] - x == d) {         s[i] = x;         cc++;         if (cc == k) return s;       }     for (int i = s.length(); i-- > 0;)       if (x - s[i] == d) {         s[i] = x;         cc++;         if (cc == k) return s;       }   }   const string &min(const string &s, const string &t) {     if (s > t) return t;     return s;   }   int main() {     scanf( %d%d , &n, &k);     cin >> s;     int cost = 1000000000;     string res =   ;     for (char x =  0 ; x <=  9 ; x++) {       pair<int, int> a = fff(s, x);       if (cost > a.first) {         cost = a.first;         res = change(s, x, a.second);       } else if (cost == a.first)         res = min(res, change(s, x, a.second));     }     cout << cost << endl << res;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   template <typename T>   using v = vector<T>;   const ll K = 1000;   const ll N = 1e5;   const ll B = N / K + 1;   ll n;   ll arr[N + 1];   ll cnt[N + 1];   ll bin[B + 1];   ll greedy(ll k) {     fill(begin(cnt), end(cnt), 0);     v<ll> dif;     ll bits = 0;     for (ll i = 1; i <= n; i++) {       ll val = arr[i];       if (cnt[val] == 0) {         if ((ll)dif.size() == k) {           for (const auto& color : dif) {             cnt[color] = 0;           }           dif.clear();           bits++;         }         dif.push_back(val);       }       cnt[val]++;     }     if (dif.size() > 0) {       bits++;     }     return bits;   }   int main(int argc, char const* argv[]) {     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);     cin >> n;     for (ll i = 0; i < n; i++) {       cin >> arr[i + 1];     }     v<ll> resarr(n + 1, -1);     for (ll k = 1; k <= min(K, n); k++) {       resarr[k] = greedy(k);     }     fill(begin(bin), end(bin), 1);     for (ll b = 1; b <= B; b++) {       ll lo = 1;       ll hi = (b > 1) ? bin[b - 1] : n;       while (lo != hi) {         ll mid = (lo + hi) / 2;         ll curbits = greedy(mid);         if (curbits <= b) {           hi = mid;         } else {           lo = mid + 1;         }       }       bin[b] = lo;     }     for (ll k = K + 1; k <= n; k++) {       ll lo = 1;       ll hi = resarr[k - 1];       while (lo != hi) {         ll mid = (lo + hi) / 2;         if (bin[mid] <= k) {           hi = mid;         } else {           lo = mid + 1;         }       }       resarr[k] = lo;     }     for (ll i = 1; i <= n; i++) {       cout << resarr[i] <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   const long long mod = 1e9 + 7;   const int inf = 1 << 30;   const int maxn = 600500;   const double EPS = 1e-7;   void solve() {     int q;     scanf( %d , &q);     while (q--) {       long long n;       scanf( %lld , &n);       int a, b;       scanf( %d%d , &a, &b);       if (b < 2LL * a) {         printf( %lld n , (n / 2LL) * 1LL * b + a * (n % 2));       } else         printf( %lld n , a * 1LL * n);     }   }   int main() {     int tc = 1;     for (int tt = 0; tt < (int)(tc); ++tt) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void print_array(T arr[], int size_arr) {     for (int i = (0); i < (size_arr); i++) {       cout << arr[i];       if (i == size_arr - 1)         cout << endl;       else         cout <<    ;     }   }   template <typename T>   void print_vector(vector<T> v) {     for (int i = (0); i < (v.size()); i++) {       cout << v[i];       if (i == v.size() - 1)         cout << endl;       else         cout <<    ;     }   }   template <typename T>   void memset_array(T arr[], T value, int size_arr) {     for (int i = (0); i < (size_arr); i++) {       arr[i] = value;     }   }   bool compare_lexical_string(string a, string b) {     for (int i = (0); i < (min(a.length(), b.length())); i++) {       if (a[i] != b[i]) return a[i] < b[i];     }     return a.length() < b.length();   }   struct lex_string {     bool operator()(string a, string b) { return compare_lexical_string(a, b); }   };   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long b, d, s;     cin >> b >> d >> s;     long long max_num, min_num;     max_num = max(b, max(d, s));     min_num = min(b, min(d, s));     if (max_num == min_num)       cout << 0 << endl;     else {       if (max_num == b) {         long long count = 0;         if (d < b) count += b - 1 - d;         if (s < b) count += b - 1 - s;         cout << count << endl;       } else if (max_num == d) {         if (d == s)           cout << d - 1 - b << endl;         else {           cout << 2 * d - 2 - b - s << endl;         }       } else {         cout << s - 1 - b + s - 1 - d << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const double EPS = 1e-8;   const double PI = 2 * acos(0);   const int MAXN = 2006;   struct Node {     int x, y;     bool operator<(Node b) const {       if (x != b.x) return x < b.x;       return y < b.y;     }   };   struct Math {     int n;     int MOD;     int SMALL_N;     long long *r;     long long *fac, *ifac;     long long **A, **C;     long long *twop, *iv_twop;     Math() {       n = -1;       MOD = 1;       SMALL_N = 3030;     }     void prepare(int n_, int MOD_) {       n = n_;       MOD = MOD_;       r = new long long[n + 1];       r[1] = 1;       for (int i = 2; i <= n; ++i)         r[i] = (MOD - (MOD / i) * r[MOD % i] % MOD) % MOD;       fac = new long long[n + 1];       fac[0] = 1;       for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % MOD;       ifac = new long long[n + 1];       ifac[0] = ifac[1] = 1;       for (int i = 2; i <= n; ++i) ifac[i] = ifac[i - 1] * r[i] % MOD;       twop = new long long[n + 1];       iv_twop = new long long[n + 1];       twop[0] = iv_twop[0] = 1;       twop[1] = 2;       iv_twop[1] = (MOD + 1) / 2;       for (int i = 2; i <= n; ++i) {         twop[i] = twop[i - 1] * 2 % MOD;         iv_twop[i] = iv_twop[i - 1] * iv_twop[1] % MOD;       }       if (n <= SMALL_N) {         A = new long long *[n + 1];         for (int i = 0; i <= n; ++i) A[i] = new long long[n + 1];         memset(A, 0, sizeof(A));         for (int i = 0; i <= n; ++i) A[i][0] = 1;         for (int i = 1; i <= n; ++i)           for (int j = 1; j <= i; ++j) A[i][j] = A[i][j - 1] * (i - j + 1) % MOD;       }       if (n <= SMALL_N) {         C = new long long *[n + 1];         for (int i = 0; i <= n; ++i) C[i] = new long long[n + 1];         memset(C, 0, sizeof(C));         for (int i = 0; i <= n; ++i) C[i][0] = C[i][i] = 1;         for (int i = 1; i <= n; ++i)           for (int j = 1; j < i; ++j)             C[i][j] = (C[i][j - 1] + C[i - 1][j - 1]) % MOD;       }     }     long long getA(int n_, int k_) {       if (k_ < 0 || n_ < 0) return 0;       if (n_ <= n && n <= SMALL_N) return A[n_][k_];       return fac[n_] * ifac[n_ - k_] % MOD;     }     long long getC(int n_, int k_) {       if (k_ < 0 || n_ < 0) return 0;       if (n_ <= n && n <= SMALL_N) return C[n_][k_];       return fac[n_] * ifac[n_ - k_] % MOD * ifac[k_] % MOD;     }   };   int n, m, k;   Node node[MAXN];   long long dp[MAXN];   Math toan;   bool input() {     int u, v, x, y;     cin >> n >> m >> k;     for (int i = 1; i <= k; ++i) cin >> node[i].x >> node[i].y;     return !cin.fail();   }   void clear_input() { k = 0; }   void preprocess() {     sort(node + 1, node + k + 1);     toan.prepare(200005, MOD);   }   void process(int itest) {     int u, v, x, y;     node[k + 1].x = n, node[k + 1].y = m;     for (int i = 1; i <= k + 1; ++i) {       x = node[i].x;       y = node[i].y;       dp[i] = toan.getC(x + y - 2, x - 1);       for (int j = 1; j < i; ++j)         if (node[j].x <= x && node[j].y <= y)           dp[i] = ((dp[i] - dp[j] * toan.getC(x - node[j].x + y - node[j].y,                                               x - node[j].x)) %                        MOD +                    MOD) %                   MOD;     }     cout << dp[k + 1] << endl;   }   void oneTest() {     while (input()) {       preprocess();       process(0);       clear_input();     }   }   void multiTest() {     int t;     cin >> t;     for (int it = 1; it <= t; ++it) {       input();       preprocess();       process(it);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     oneTest();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int x = 0, y = 0, z = 0;       int ar[4];       for (int i = 0; i < 4; i++) {         cin >> ar[i];       }       x = ar[1];       y = ar[2];       z = ar[2];       cout << x <<     << y <<     << z << endl;     }     return 0;   }


#include <bits/stdc++.h>   const int N = (int)3e5 + 2;   int n;   bool a[N << 1];   int ans, ansl, ansr;   inline long long read() {     long long x = 0;     char ch = getchar();     for (; ch >  9  || ch <  0 ; ch = getchar())       ;     for (; ch >=  0  && ch <=  9 ; ch = getchar())       x = (x << 1) + (x << 3) + (ch ^ 48);     return x;   }   int main() {     n = read();     int cnt = 0, mn = 0, pos = -1;     for (int i = 0; i < n; ++i) {       char ch = getchar();       if (ch ==  ( )         ++cnt;       else         a[i] = a[i + n] = 1, --cnt;       if (mn > cnt)         mn = cnt, pos = i, ans = 1;       else if (mn == cnt)         ++ans;     }     if (cnt) {       printf( 0 n1 1 );       return 0;     }     int I = n + ++pos, num[2] = {0, 0}, l[2], tmp = ans;     for (int i = pos; i < I; ++i) {       if (!a[i]) {         ++cnt;         if (cnt == 1)           ++num[0], l[0] = i;         else if (cnt == 2)           ++num[1], l[1] = i;       } else {         --cnt;         if (!cnt) {           if (num[0] > ans) {             ans = num[0];             ansl = l[0];             ansr = i;           }           num[0] = 0;         } else if (cnt == 1) {           if (tmp + num[1] > ans) {             ans = tmp + num[1];             ansl = l[1];             ansr = i;           }           ++num[0];           num[1] = 0;         } else if (cnt == 2)           ++num[1];       }     }     printf( %d n%d %d , ans, ansl % n + 1, ansr % n + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename A>   ostream &operator<<(ostream &cout, vector<A> const &v);   template <typename A, typename B>   ostream &operator<<(ostream &cout, pair<A, B> const &p) {     return cout <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename A>   ostream &operator<<(ostream &cout, vector<A> const &v) {     cout <<  [ ;     for (int i = 0; i < v.size(); i++) {       if (i) cout <<  ,  ;       cout << v[i];     }     return cout <<  ] ;   }   template <typename A, typename B>   istream &operator>>(istream &cin, pair<A, B> &p) {     cin >> p.first;     return cin >> p.second;   }   mt19937_64 rng(61378913);   void usaco(string filename) {     freopen((filename +  .in ).c_str(),  r , stdin);     freopen((filename +  .out ).c_str(),  w , stdout);   }   const long double pi = 3.14159265358979323846;   long long n, m, k, q, l, r, x, y, z;   const long long template_array_size = 1e6 + 8432;   long long a[template_array_size];   long long b[template_array_size];   long long c[template_array_size];   string second, t;   long long ans = 0;   void solve(int tc = 0) {     bool dead[10] = {0};     long long v = 0;     cin >> n;     for (long long i = 0; i < n; i++) {       char c;       cin >> c >> x;       if (c ==  | ) {         for (long long j = 0; j < 10; j++) {           if (x & (1 << j)) dead[j] = 1;         }         v |= x;       } else if (c ==  & ) {         for (long long j = 0; j < 10; j++) {           if (!(x & (1 << j))) dead[j] = 1;         }         v &= x;       } else {         v ^= x;       }     }     long long ov = 0;     for (long long i = 0; i < 10; i++) {       if (!dead[i]) ov += (1 << i);     }     cout << 2 <<   n ;     cout <<  &   << ov <<   n ;     cout <<  ^   << v <<   n ;   }   int main() {     { ios_base::sync_with_stdio(false); }     { cin.tie(NULL); }     cout << setprecision(12) << fixed;     int tc = 1;     for (int t = 0; t < tc; t++) solve(t);   }


#include <bits/stdc++.h>   using namespace std;   int n, maxim, a;   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a;       if (a > maxim) maxim = a;     }     cout << (maxim ^ a);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int a, b, t;     cin >> t;     int arr[t];     for (int i = 0; i < t; i++) {       cin >> a >> b;       arr[i] = a + b;     }     for (int i = 0; i < t; i++) {       cout << arr[i] << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b;     cin >> a >> b;     string a1(b.size(),  0 );     string b1(a.size(),  0 );     a1 = a1 + a;     b1 = b1 + b;     if (a1 > b1) cout <<  >  << endl;     if (a1 == b1) cout <<  =  << endl;     if (a1 < b1) cout <<  <  << endl;     return 0;   }


#include <iostream>   #include <vector>      using namespace std;      const int MAXN = 2e5 + 5;      struct Info   {       int maxPref, maxSuff;          Info(){}       Info(int x) : maxPref(x), maxSuff(x) {}       Info(int maxPref, int maxSuff)       {           this->maxPref = maxPref;           this->maxSuff = maxSuff;       }          void modifyPref(int x)       {           maxPref += x;       }       void modifySuff(int x)       {           maxSuff += x;       }   };      Info operator +(const Info A, const Info B)   {       return Info(max(A.maxPref, B.maxPref), max(A.maxSuff, B.maxSuff));   }      struct SegmentTree   {       Info x;       int lazyPref, lazySuff;          int l, r;       SegmentTree *L, *R;          SegmentTree() : L(nullptr), R(nullptr), lazyPref(0), lazySuff(0) {}       SegmentTree(int l, int r) : SegmentTree()       {           this->l = l;           this->r = r;       }          void build(int val, int n)       {           if(l==r)           {               x = Info((l+1)*val, (n-r)*val);               return;           }              L = new SegmentTree(l, (l+r)/2);           R = new SegmentTree((l+r)/2+1, r);              L->build(val, n);           R->build(val, n);           x = L->x + R->x;       }          void updateLazy()       {           x.modifyPref(lazyPref);           x.modifySuff(lazySuff);           if(l!=r)           {               L->lazyPref += lazyPref;               L->lazySuff += lazySuff;               R->lazyPref += lazyPref;               R->lazySuff += lazySuff;           }              lazyPref = lazySuff = 0;       }          Info query(int ql, int qr)       {           updateLazy();              if(l>=ql && r<=qr) return x;           if(r<ql || l>qr) return Info(-MAXN, -MAXN);              return L->query(ql, qr) + R->query(ql, qr);       }          void updatePref(int ql, int qr, int change)       {           updateLazy();              if(l>=ql && r<=qr)           {               lazyPref += change;               updateLazy();                  return;           }           if(r<ql || l>qr) return;              L->updatePref(ql, qr, change);           R->updatePref(ql, qr, change);           x = L->x + R->x;       }       void updateSuff(int ql, int qr, int change)       {           updateLazy();              if(l>=ql && r<=qr)           {               lazySuff += change;               updateLazy();                  return;           }           if(r<ql || l>qr) return;              L->updateSuff(ql, qr, change);           R->updateSuff(ql, qr, change);           x = L->x + R->x;       }          void update(int q, int change)       {           updatePref(q, r, change);           updateSuff(l, q, change);       }   };      int n;   int a[MAXN];   vector <int> num2Pos[MAXN];      int answer[MAXN];      void updateWithBiggerThanMiddle()   {       SegmentTree *T = new SegmentTree(0, n-1);       T->build(-1, n);          for(int x = 1;x<=n;x++)       {           for(int i: num2Pos[x]) T->update(i, +2);           for(int i: num2Pos[x])           {               int sum = 0;               sum += T->query(i, n-1).maxPref - T->query(i, i).maxPref;               sum += T->query(0, i).maxSuff - T->query(i, i).maxSuff;                  answer[i] = max(answer[i], sum/2);           }       }   }      void updateWithSmallerThanMiddle()   {       SegmentTree *T = new SegmentTree(0, n-1);       T->build(-1, n);          for(int x = n;x>=1;x--)       {           for(int i: num2Pos[x]) T->update(i, +2);           for(int i: num2Pos[x])           {               int sum = 0;               sum += T->query(i, n-1).maxPref - T->query(i, i).maxPref;               sum += T->query(0, i).maxSuff - T->query(i, i).maxSuff;                  answer[i] = max(answer[i], (sum+1)/2);           }       }   }      int main()   {       ios::sync_with_stdio(false);       cin.tie(nullptr);          cin >> n;       for(int i = 0;i<n;i++)        {           cin >> a[i];           num2Pos[a[i]].push_back(i);       }          updateWithBiggerThanMiddle();          updateWithSmallerThanMiddle();          for(int i = 0;i<n;i++)           cout << answer[i] <<    ;       cout <<   n ;   }


#include <bits/stdc++.h>   namespace algo {   using namespace std;   const long double PI = 3.141592653589793238462643383279502884L;   const long double E = 2.718281828459045235360287471352662498L;   const long double EPS = 1e-12L;   template <typename T1, typename T2>   T1 bin_pow(T1 a, T2 n) {     T1 r = 1;     while (n > 0) {       if (n & 1) r *= a;       a *= a;       n /= 2;     }     return r;   }   template <typename T1, typename T2>   T1 bin_pow(T1 a, T2 n, const T1& m) {     if (m == 1) return 0;     T1 r = 1;     while (n > 0) {       if (n & 1) {         r *= a;         r %= m;       }       a *= a;       a %= m;       n /= 2;     }     return r;   }   template <typename T>   T gcd(const T& a, const T& b) {     if (b == 0) return abs(a);     return gcd(b, a % b);   }   template <typename T>   T gcd(const T& a, const T& b, T& x, T& y) {     if (b == 0) {       x = a < 0 ? -1 : 1;       y = 0;       return x * a;     }     T d = gcd(b, a % b, y, x);     y -= (a / b) * x;     return d;   }   template <typename T>   T lcm(const T& a, const T& b) {     return abs(a / gcd(a, b) * b);   }   template <typename T>   T mod_add(const T& a, const T& b, const T& m) {     if (a < m - b) return a + b;     return a - (m - b);   }   template <typename T>   T mod_sub(const T& a, const T& b, const T& m) {     if (a < b) return a + (m - b);     return a - b;   }   template <typename T>   T mod_mul(T a, T b, const T& m) {     if (b > a) swap(a, b);     T r = 0;     while (b > 0) {       if (b & 1) r = mod_add(r, a, m);       a = mod_add(a, a, m);       b /= 2;     }     return r;   }   template <typename T1, typename T2>   T1 mod_pow(T1 a, T2 n, const T1& m) {     if (m == 1) return 0;     T1 r = 1;     while (n > 0) {       if (n & 1) r = mod_mul(r, a, m);       a = mod_mul(a, a, m);       n /= 2;     }     return r;   }   template <typename T>   T mod_inv(const T& a, const T& m) {     T x, y;     T d = gcd(a, m, x, y);     if (d != 1) return 0;     if (x < 0) x += m;     return x;   }   struct Sieve {     int* lpf = nullptr;     int* phi = nullptr;     int size;     vector<int> primes;     Sieve(bool calcPhi = false, int n = 0xfffff) : size(n) {       lpf = new int[size + 1];       fill(lpf, lpf + size + 1, 0);       lpf[1] = 1;       if (calcPhi) {         phi = new int[size + 1];         phi[1] = 1;         for (int i = 2; i <= size; ++i) {           if (lpf[i] == 0) {             phi[i] = i - 1;             lpf[i] = i;             primes.push_back(i);           }           for (int j = 0, t; j < primes.size() && (t = i * primes[j]) <= size;                ++j) {             lpf[t] = primes[j];             if (primes[j] == lpf[i]) {               phi[t] = phi[i] * primes[j];               break;             }             phi[t] = phi[i] * (primes[j] - 1);           }         }       } else {         for (int i = 2; i <= size; ++i) {           if (lpf[i] == 0) {             lpf[i] = i;             primes.push_back(i);           }           for (int j = 0, t; j < primes.size() && (t = i * primes[j]) <= size;                ++j) {             lpf[t] = primes[j];             if (primes[j] == lpf[i]) break;           }         }       }     }     Sieve(int n) : Sieve(false, n) {}     ~Sieve() {       delete[] lpf;       lpf = nullptr;       delete[] phi;       phi = nullptr;     }   };   template <typename T>   T phi(T n) {     T r = n;     for (T i = 2; i * i <= n; ++i) {       if (n % i == 0) {         r -= r / i;         do {           n /= i;         } while (n % i == 0);       }     }     if (n > 1) r -= r / n;     return r;   }   }  // namespace algo   using namespace std;   using namespace algo;   namespace task {   struct Widget;   map<string, Widget*> widgets;   struct Widget {     char type;     int64_t width = -1;     int64_t height = -1;     int64_t border = 0;     int64_t spacing = 0;     vector<string> children;     Widget(char type, int64_t width = -1, int64_t height = -1)         : type(type), width(width), height(height) {}     void pack(const string& name) { children.push_back(name); }     pair<int64_t, int64_t> size() {       if (width == -1 || height == -1) {         if (type ==  W ) {           exit(3);         } else if (children.size() == 0) {           width = 0;           height = 0;         } else if (type ==  H ) {           int64_t t_width = (children.size() - 1) * spacing;           int64_t t_height = 0;           for (int i = 0; i < children.size(); ++i) {             pair<int64_t, int64_t> t_size = widgets[children[i]]->size();             t_width += t_size.first;             t_height = max(t_height, t_size.second);           }           width = t_width + 2 * border;           height = t_height + 2 * border;         } else if (type ==  V ) {           int64_t t_width = 0;           int64_t t_height = (children.size() - 1) * spacing;           for (int i = 0; i < children.size(); ++i) {             pair<int64_t, int64_t> t_size = widgets[children[i]]->size();             t_width = max(t_width, t_size.first);             t_height += t_size.second;           }           width = t_width + 2 * border;           height = t_height + 2 * border;         }       }       return {width, height};     }   };   int n, x, y;   string s, name1, name2;   int main() {     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> s;       if (s ==  Widget ) {         cin >> s;         char name[16];         int64_t width, height;         sscanf(s.c_str(),  %[a-z](%lli,%lli) , name, &width, &height);         name1 = string(name);         widgets[name1] = new Widget( W , width, height);       } else if (s ==  VBox ) {         cin >> name1;         widgets[name1] = new Widget( V );       } else if (s ==  HBox ) {         cin >> name1;         widgets[name1] = new Widget( H );       } else {         char name_1[16];         char name_2[16];         char method[16];         sscanf(s.c_str(),  %[a-z].%[a-z_](%[a-z0-9]) , name_1, method, name_2);         name1 = string(name_1);         if (string(method) ==  pack ) {           name2 = string(name_2);           widgets[name1]->pack(name2);         } else if (string(method) ==  set_border ) {           int64_t border;           sscanf(name_2,  %lli , &border);           widgets[name1]->border = border;         } else if (string(method) ==  set_spacing ) {           int64_t spacing;           sscanf(name_2,  %lli , &spacing);           widgets[name1]->spacing = spacing;         }       }     }     for (auto& it : widgets) {       pair<int64_t, int64_t> size = it.second->size();       cout << it.first <<     << size.first <<     << size.second <<   n ;     }     return 0;   }   }  // namespace task   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.precision(11);     cout.setf(ios::fixed);     return task::main();   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000009;   map<pair<int, int>, int> in, id;   set<int> S;   int X[100010], Y[100010];   bool check(pair<int, int> P) {     int x = P.first, y = P.second + 1;     for (int dx = x - 1; dx <= x + 1; dx++) {       if (in.count(make_pair(dx, y)) && in[make_pair(dx, y)] == 1) return 0;     }     return 1;   }   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       int x, y;       cin >> x >> y;       X[i] = x, Y[i] = y;       in[make_pair(x, y)] = 0, id[make_pair(x, y)] = i;     }     for (map<pair<int, int>, int>::iterator it = in.begin(); it != in.end();          it++) {       int x = it->first.first, y = it->first.second - 1;       for (int dx = x - 1; dx <= x + 1; dx++) {         if (in.count(make_pair(dx, y))) {           in[it->first]++;         }       }     }     for (map<pair<int, int>, int>::iterator it = in.begin(); it != in.end();          it++) {       if (check(it->first)) S.insert(id[it->first]);     }     long long ans = 0;     for (int it = 0; it < n; it++) {       if (it & 1) {         int v = *S.begin();         S.erase(S.begin());         int y = Y[v] + 1;         id.erase(make_pair(X[v], Y[v]));         in[make_pair(X[v], Y[v])] = 0;         for (int x = X[v] - 1; x <= X[v] + 1; x++) {           if (!id.count(make_pair(x, y))) continue;           in[make_pair(x, y)]--;         }         y--;         for (int x = X[v] - 2; x <= X[v] + 2; x++) {           if (!id.count(make_pair(x, y))) continue;           if (check(make_pair(x, y)))             S.insert(id[make_pair(x, y)]);           else             S.erase(id[make_pair(x, y)]);         }         y--;         for (int x = X[v] - 2; x <= X[v] + 2; x++) {           if (!id.count(make_pair(x, y))) continue;           if (check(make_pair(x, y)))             S.insert(id[make_pair(x, y)]);           else             S.erase(id[make_pair(x, y)]);         }         ans = (ans * n + v) % mod;       } else {         int v = *(--S.end());         S.erase(--S.end());         int y = Y[v] + 1;         id.erase(make_pair(X[v], Y[v]));         in[make_pair(X[v], Y[v])] = 0;         for (int x = X[v] - 1; x <= X[v] + 1; x++) {           if (!id.count(make_pair(x, y))) continue;           in[make_pair(x, y)]--;         }         y--;         for (int x = X[v] - 2; x <= X[v] + 2; x++) {           if (!id.count(make_pair(x, y))) continue;           if (check(make_pair(x, y)))             S.insert(id[make_pair(x, y)]);           else             S.erase(id[make_pair(x, y)]);         }         y--;         for (int x = X[v] - 2; x <= X[v] + 2; x++) {           if (!id.count(make_pair(x, y))) continue;           if (check(make_pair(x, y)))             S.insert(id[make_pair(x, y)]);           else             S.erase(id[make_pair(x, y)]);         }         ans = (ans * n + v) % mod;       }     }     cout << ans << endl;     return 0;   }


#include<bits/stdc++.h>   #include<algorithm>   #include<iostream>   #include<cstdio>   #include<cmath>   #include<cstring>   #include<stack>   #include<queue>   using namespace std;   typedef long long ll;   const ll INF=0x7f7f7f7f;   const int maxn=2e5+7;   const ll mod=1e9+7;   ll read(){       ll f=0,x=0;char ch=getchar();       while(!isdigit(ch)){if(ch== - ) f=1;ch=getchar();}       while(isdigit(ch)){x=x*10+ch- 0 ;ch=getchar();}       return !f?x:-x;   }   int tt,n,s[maxn];   bool check(int x,int y){       if(x==3){           if(s[y]>=s[y+1]&&s[y+1]>=s[y+2]) return false;           if(s[y]<=s[y+1]&&s[y+1]<=s[y+2]) return false;           return true;       }       else{           for(int i=0;i<4;i++){               for(int j=i+1;j<4;j++){                   for(int k=j+1;k<4;k++){                       if(s[y+i]>=s[y+j]&&s[y+j]>=s[y+k]) return false;                       if(s[y+i]<=s[y+j]&&s[y+j]<=s[y+k]) return false;                   }               }           }           return true;       }   }   int main(){       scanf( %d ,&tt);       while(tt--){           ll ans=0;           scanf( %d ,&n);           for(int i=1;i<=n;i++){               scanf( %d ,&s[i]);           }           ans+=2*n-1;           for(int i=3;i<=4;i++){               for(int j=1;j<=n-i+1;j++){                   if(check(i,j)) ans++;               }           }           printf( %lld n ,ans);       }       return 0;   }


#include <bits/stdc++.h>   const int N = 1e5 + 5, M = 2e5 + 5, logN = 17 + 1;   int n, m, root, idx, a[N], f[N][logN], dfn[N], seq[N], sz[N], dep[N];   int tot, lnk[N], ter[M], nxt[M];   long long seg[N << 2], tag[N << 2];   void pushup(int rt) { seg[rt] = seg[rt << 1] + seg[rt << 1 | 1]; }   void build(int rt, int l, int r) {     if (l == r) {       seg[rt] = a[seq[l]];       return;     }     int mid = (l + r) >> 1;     build(rt << 1, l, mid);     build(rt << 1 | 1, mid + 1, r);     pushup(rt);   }   void update(int rt, int l, int r, long long k) {     seg[rt] += 1LL * (r - l + 1) * k;     tag[rt] += k;   }   void pushdown(int rt, int l, int r) {     if (!tag[rt]) return;     int mid = (l + r) >> 1;     update(rt << 1, l, mid, tag[rt]);     update(rt << 1 | 1, mid + 1, r, tag[rt]);     tag[rt] = 0;   }   void modify(int x, int y, int rt, int l, int r, int k) {     if (x <= l && r <= y) {       update(rt, l, r, k);       return;     }     pushdown(rt, l, r);     int mid = (l + r) >> 1;     if (x <= mid) modify(x, y, rt << 1, l, mid, k);     if (mid < y) modify(x, y, rt << 1 | 1, mid + 1, r, k);     pushup(rt);   }   long long query(int x, int y, int rt, int l, int r) {     if (x <= l && r <= y) return seg[rt];     pushdown(rt, l, r);     int mid = (l + r) >> 1;     long long ret = 0;     if (x <= mid) ret += query(x, y, rt << 1, l, mid);     if (mid < y) ret += query(x, y, rt << 1 | 1, mid + 1, r);     return ret;   }   void add(int u, int v) { ter[++tot] = v, nxt[tot] = lnk[u], lnk[u] = tot; }   void dfs(int u, int fa) {     dep[u] = dep[fa] + 1, f[u][0] = fa, dfn[u] = ++idx, seq[idx] = u, sz[u] = 1;     for (int i = 1; (1 << i) <= dep[u]; ++i) f[u][i] = f[f[u][i - 1]][i - 1];     for (int i = lnk[u]; i; i = nxt[i]) {       int v = ter[i];       if (v == fa) continue;       dfs(v, u), sz[u] += sz[v];     }   }   int lca(int u, int v) {     if (dep[u] > dep[v]) u ^= v ^= u ^= v;     for (int i = 17; ~i; --i)       if (dep[f[v][i]] >= dep[u]) v = f[v][i];     if (u == v) return u;     for (int i = 17; ~i; --i)       if (f[u][i] ^ f[v][i]) u = f[u][i], v = f[v][i];     return f[u][0];   }   int getlca(int u, int v, int p) {     int x = lca(u, v), y = lca(u, p), z = lca(v, p);     if (dep[y] > dep[x]) x = y;     if (dep[z] > dep[x]) x = z;     return x;   }   int jump(int u, int d) {     for (int i = 17; ~i; --i)       if (d & (1 << i)) u = f[u][i];     return u;   }   void treeModify(int u, int k) {     int l = dfn[u], r = dfn[u] + sz[u] - 1;     if (u == root)       modify(1, n, 1, 1, n, k);     else if (dfn[root] < l || dfn[root] > r)       modify(l, r, 1, 1, n, k);     else {       int son = jump(root, dep[root] - dep[u] - 1);       modify(1, n, 1, 1, n, k),           modify(dfn[son], dfn[son] + sz[son] - 1, 1, 1, n, -k);     }   }   long long treeQuery(int u) {     int l = dfn[u], r = dfn[u] + sz[u] - 1;     if (u == root)       return query(1, n, 1, 1, n);     else if (dfn[root] < l || dfn[root] > r)       return query(l, r, 1, 1, n);     else {       int son = jump(root, dep[root] - dep[u] - 1);       return query(1, n, 1, 1, n) -              query(dfn[son], dfn[son] + sz[son] - 1, 1, 1, n);     }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (int i = 1; i < n; ++i) {       int u, v;       scanf( %d%d , &u, &v);       add(u, v), add(v, u);     }     dfs(1, 0);     build(1, 1, n);     root = 1;     while (m--) {       int opt;       scanf( %d , &opt);       if (opt == 1) {         scanf( %d , &root);       } else if (opt == 2) {         int u, v, x;         scanf( %d%d%d , &u, &v, &x);         treeModify(getlca(u, v, root), x);       } else {         int x;         scanf( %d , &x);         printf( %lld n , treeQuery(x));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, ans, dp[8][8];   int main() {     cin >> n >> m;     for (int i = 1; i <= m; i++) {       int l, r;       cin >> l >> r;       dp[l][r] = dp[r][l] = 1;     }     if (n <= 6)       cout << m;     else {       for (int i = 1; i <= n; i++)         for (int j = i + 1; j <= n; j++) {           int res = 0;           for (int k = 1; k <= n; k++)             if (dp[i][k] && dp[j][k]) res++;           ans = max(ans, m - res);         }       cout << ans;     }   }


#include <bits/stdc++.h>      using std::cin;   using std::cout;   using std::vector;   using std::swap;      int main() {    int t;    cin >> t;        while (t--) {     int n, x;     cin >> n >> x;     vector<int> a(n);     for (auto& it : a) cin >> it;     int cnt = 0;     for (int i = 0; i < n && !is_sorted(a.begin(), a.end()); ++i) {      if (a[i] > x) {       swap(a[i], x);       cnt++;      }     }     cout << (!is_sorted(a.begin(), a.end()) ? -1 : cnt) <<   n ;    }        return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const int maxn = 303;   int n, m, d;   int a[maxn], b[maxn], t[maxn];   int dt[maxn];   long long que[150001];   int id[150001];   long long dp[150001][2];   int main() {     scanf( %d%d%d , &n, &m, &d);     for (int i = 1; i <= m; ++i) {       scanf( %d%d%d , a + i, b + i, t + i);       dt[i] = t[i] - t[i - 1];     }     dt[1] = n;     for (int ii = 1; ii <= m; ++ii) {       int i = ii % 2;       int l = 1, r = 1, nl = 1, nr = 1;       int lt, rt;       for (int j = 1; j <= n; ++j) {         lt = max(1ll, 1ll * j - 1ll * dt[ii] * d);         rt = min(1ll * n, 1ll * j + 1ll * dt[ii] * d);         while (nr <= rt) {           while (r > l && dp[nr][1 - i] >= que[r - 1]) r--;           que[r] = dp[nr][1 - i];           id[r] = nr;           r++;           nr++;         }         while (r > l && id[l] < lt) l++;         dp[j][i] = que[l] + 1ll * b[ii] - 1ll * abs(a[ii] - j);       }     }     long long ans = dp[1][m % 2];     for (int i = 2; i <= n; ++i) {       ans = max(ans, dp[i][m % 2]);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   long long p;   int k;   namespace solver1 {   void main() {     std::vector<int> ans;     int i;     for (i = 0; p; i++) {       if (i & 1)         ans.push_back(((p - 1) / k + 1) * k - p), p = (p - 1) / k + 1;       else         ans.push_back(p % k), p /= k;     }     printf( %d n , (int)ans.size());     for (auto i : ans) {       printf( %d  , i);     }     printf(  n );   }   }  // namespace solver1   int main() {     std::cin >> p >> k;     solver1::main();   }


#include <bits/stdc++.h>   using namespace std;   int *a;   int *b;   int n;   long long int what(long long x) {     long long int s = 0;     for (int i = 0; i < n; i++) {       s += (x * a[i]) / b[i];     }     return s;   }   int main() {     int i, j;     int c;     cin >> n >> c;     a = new int[n];     b = new int[n];     for (i = 0; i < n; i++) {       cin >> a[i] >> b[i];     }     c = c - n;     long long int l = 0;     long long int r = ((c + 1) * b[0]) / a[0] + 3;     long long int x;     x = r;     while (r - l > 1) {       if (what(x) < c)         l = x;       else         r = x;       x = (l + r) / 2;     }     long long int rl = r;     l = 0;     r = ((c + 1) * b[0]) / a[0] + 3;     x = r;     while (r - l > 1) {       if (what(x) <= c)         l = x;       else         r = x;       x = (l + r) / 2;     }     cout << (r - rl);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long M = 1000000007;   long long power(long long a, long long b) {     long long res = 1;     for (long long i = 0; i < b; i++) res = (res * a) % M;     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t, n, m, k, i, x, ara[100], ans;     cin >> t;     while (t--) {       cin >> n >> k;       x = 0;       m = k;       ans = 0;       while (m != 0) {         ara[x] = m % 2;         x++;         m /= 2;       }       for (i = 0; i < x; i++) {         if (ara[i] == 1) ans = ((ans % M) + power(n, i)) % M;       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long vet[9], used[9], available[9];   int dp[100][32];   int dp2[100][32];   vector<int> itens, itens2;   void comp() {     for (int i = 1; i <= 8; i++) {       long long j = 1;       used[i] = min(used[i], (long long)32 / i);       while (used[i]) {         itens.push_back(min(used[i], j) * i);         used[i] -= min(used[i], j);         j <<= 1;       }       j = 1;       available[i] = min(available[i], (long long)32 / i);       while (available[i]) {         itens2.push_back(min(available[i], j) * i);         available[i] -= min(available[i], j);         j <<= 1;       }     }     dp[0][0] = dp2[0][0] = 1;     for (int i = 0; i < itens.size(); i++) {       for (int j = 0; j < 32; j++) {         dp[i + 1][j] |= dp[i][j];         if (j >= itens[i]) dp[i + 1][j] |= dp[i][j - itens[i]];       }     }     for (int i = 0; i < itens2.size(); i++) {       for (int j = 0; j < 32; j++) {         dp2[i + 1][j] |= dp2[i][j];         if (j >= itens2[i]) dp2[i + 1][j] |= dp2[i][j - itens2[i]];       }     }   }   int consegue(int x) {     for (int i = 0; i < 32 - x; i++)       if (dp2[itens2.size()][i] && dp[itens.size()][x + i]) return 1;     return 0;   }   int main() {     long long W, total = 0;     scanf( %lld , &W);     for (int i = 1; i <= 8; i++) {       scanf( %lld , &vet[i]);       total += i * vet[i];     }     if (total <= W)       printf( %lld n , total);     else {       for (int i = 1; i <= 8; i++) used[i] = vet[i];       for (int i = 8; i >= 1; i--) {         if (total - i * used[i] >= W) {           total -= i * used[i];           available[i] = used[i];           used[i] = 0;         } else {           long long d = (total - W) / i;           total -= d * i;           used[i] -= d;           available[i] = d;         }       }       comp();       for (long long z = W; z >= max(0LL, W - 8); z--) {         int x = total - z;         if (consegue(x)) {           return !printf( %lld n , z);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k, A, B;   vector<long long> a(100000 + 5);   long long rec(int l, int r) {     long long m = (l + r) / 2, i, j, col = 0, lng = r - l + 1;     i = lower_bound(a.begin(), a.end(), l) - a.begin();     j = upper_bound(a.begin(), a.end(), r) - a.begin();     j--;     col = j - i + 1;     if (l == r && !col) return A;     if (l == r && col) return B * col;     if (!col) return A;     return min(B * lng * col, rec(l, m) + rec(m + 1, r));   }   int main() {     long long x;     cin >> n >> k >> A >> B;     for (int i = 0; i < k; i++) {       cin >> x;       a.push_back(x);     }     sort(a.begin(), a.end());     cout << rec(1, pow(2, n));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[500100];   int n;   vector<int> g[200100];   int d[200100];   int cnt = 1;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n; i++) {       int ind = lower_bound(d + 1, d + n + 1, a[i]) - d;       d[ind - 1] = a[i];       g[ind - 1].push_back(a[i]);     }     for (int i = n; i >= 1; i--) {       for (int j = 0; j < g[i].size(); j++) cout << g[i][j] <<    ;       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   double x[(int)2e3 + 5], y[(int)2e3 + 5];   int fio() {     int val = 0;     char ch = getchar();     while (ch ==     || ch ==   n ) ch = getchar();     while (ch >=  0  && ch <=  9 ) {       val = val * 10 + ch -  0 ;       ch = getchar();     }     return val;   }   int main() {     int n, ans = 0;     n = fio();     for (int i = 0; i < n; i++) {       x[i] = fio(), y[i] = fio();     }     map<pair<double, double>, int> mid_points;     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         double mid_x = (x[i] + x[j]) / 2;         double mid_y = (y[i] + y[j]) / 2;         mid_points[make_pair(mid_x, mid_y)]++;       }     }     map<pair<double, double>, int>::iterator it = mid_points.begin();     while (it != mid_points.end()) {       ans += it->second * (it->second - 1) / 2;       it++;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, b, p;     cin >> n >> b >> p;     int m = n;     long long int nb = 0;     while (n > 1) {       for (int i = 8; i >= 0; i--) {         if (pow(2, i) <= n) {           nb += b * pow(2, i) + pow(2, i) / 2;           n = n - (pow(2, i)) + pow(2, i) / 2;           break;         }       }     }     cout << nb <<     << m * p;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int v, int vv) { return vv == 0 ? v : gcd(vv, v % vv); }   int main() {     int powV1, powV2, v[101], v2[101], vv, vv2, x;     cin >> powV1 >> powV2;     for (int i = 0; i <= powV1; i++) {       cin >> v[i];     }     for (int i = 0; i <= powV2; i++) {       cin >> v2[i];     }     for (int i = 0; i < powV1; i++) {       if (v[i] != 0) {         vv = v[i];         break;       }     }     for (int i = 0; i < powV2; i++) {       if (v2[i] != 0) {         vv2 = v2[i];         break;       }     }     if (powV1 == 0) vv = v[0];     if (powV2 == 0) vv2 = v2[0];     if (powV1 > powV2) {       if (vv * vv2 < 0)         cout <<  -Infinity  << endl;       else         cout <<  Infinity  << endl;     } else if (powV1 < powV2) {       cout <<  0/1  << endl;     } else {       x = gcd(abs(vv), abs(vv2));       if (vv2 < 0) {         vv2 *= -1;         vv *= -1;       }       cout << vv / x <<  /  << vv2 / x << endl;     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   vector<int> c[N];   vector<int> s[20][N];   int dp[20][N];   int h[N];   vector<int> v[N];   int sh(int x, int y) {     for (int i = 17; i + 1; --i)       if (y & (1 << i)) x = dp[i][x];     return x;   }   int ac(int x, int y) {     if (h[x] > h[y])       x = sh(x, h[x] - h[y]);     else       y = sh(y, h[y] - h[x]);     for (int i = 17; i + 1 && x != y; --i)       if (dp[i][x] != dp[i][y]) x = dp[i][x], y = dp[i][y];     if (x == y)       return x;     else       return dp[0][x];   }   void dfs(int prec, int node) {     h[node] = h[prec] + 1;     dp[0][node] = prec;     s[0][node] = c[node];     for (auto it : v[node])       if (it != prec) dfs(node, it);   }   vector<int> get(vector<int> a, vector<int> b) {     if (a.empty()) return b;     if (b.empty()) return a;     int *p = new int[a.size() + b.size()];     merge(a.begin(), a.end(), b.begin(), b.end(), p);     vector<int> as;     for (int *r = p, cn = 1; cn <= a.size() + b.size() && cn <= 10; ++r, ++cn)       as.push_back(*r);     while (as.size() && !(*as.begin())) as.erase(as.begin());     return as;   }   int main(void) {     int n, m, q;     scanf( %d%d%d n , &n, &m, &q);     int a, b;     for (int i = 1; i < n; ++i) {       scanf( %d%d , &a, &b);       v[a].push_back(b);       v[b].push_back(a);     }     for (int i = 1; i <= m; ++i) {       scanf( %d , &a);       c[a].push_back(i);     }     for (int i = 1; i <= n; ++i) {       sort(c[i].begin(), c[i].end());       while (c[i].size() > 10) c[i].erase(c[i].end() - 1);     }     dfs(0, 1);     for (int i = 1; i < 18; ++i)       for (int j = 1; j <= n; ++j) {         dp[i][j] = dp[i - 1][dp[i - 1][j]];         s[i][j] = get(s[i - 1][j], s[i - 1][dp[i - 1][j]]);       }     while (q--) {       int a, b, mx;       scanf( %d%d%d , &a, &b, &mx);       int anc = ac(a, b);       int disa = h[a] - h[anc];       int disb = h[b] - h[anc];       vector<int> ans;       for (int i = 17; i + 1; --i)         if ((1 << i) & disa) {           ans = get(ans, s[i][a]);           a = dp[i][a];           while (ans.size() > mx) ans.erase(ans.end() - 1);         }       for (int i = 17; i + 1; --i)         if ((1 << i) & disb) {           ans = get(ans, s[i][b]);           b = dp[i][b];           while (ans.size() > mx) ans.erase(ans.end() - 1);         }       ans = get(ans, c[anc]);       while (ans.size() > mx) ans.erase(ans.end() - 1);       printf( %d  , ans.size());       for (auto it : ans) printf( %d  , it);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxt = 300001;   typedef int mang[maxt + 10];   struct mystr {     int val[4];   };   mang bit1, bit2;   mystr a[100010];   int n, dem, vt = 1;   long long d, m;   vector<pair<long long, pair<int, int>>> b;   int kq[100010];   vector<int> s;   int getbit(int x, mang bit) {     int res = -maxt;     for (; x > 0; x -= x & (-x)) res = max(res, bit[x]);     return res;   }   void updatebit(int x, int val, mang& bit) {     for (; x <= maxt; x += x & (-x)) bit[x] = max(bit[x], val);   }   int main() {     ios::sync_with_stdio(0);     cin >> n >> d;     for (int i = 1; i <= n; i++) {       cin >> m;       b.push_back({m, {i, 1}});       b.push_back({m - d, {i, 2}});       b.push_back({m + d, {i, 3}});     }     b.push_back({(int)-1e18, {0, 0}});     sort(b.begin(), b.end());     for (int i = 1; i < b.size(); i++) {       if (b[i].first != b[i - 1].first) dem++;       a[b[i].second.first].val[b[i].second.second] = dem;     }     for (int i = 1; i <= n; i++) {       int longt =           max(getbit(a[i].val[2], bit1), getbit(maxt - a[i].val[3], bit2)) + 1;       updatebit(a[i].val[1], longt, bit1);       updatebit(maxt - a[i].val[1], longt, bit2);       kq[i] = longt;       if (kq[i] >= kq[vt]) vt = i;     }     cout << kq[vt] <<   n ;     for (int i = vt; i >= 1; i--)       if (kq[vt] == kq[i] + 1 &&           (a[i].val[1] <= a[vt].val[2] || a[i].val[1] >= a[vt].val[3])) {         s.push_back(vt);         vt = i;       }     cout << vt <<    ;     for (int i = s.size() - 1; i >= 0; i--) cout << s[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool used[2011];   int b[2011];   int main() {     int n, k;     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> b[i];     int cand;     for (int i = 0; i < n; i++) {       int cnt = 0;       for (int j = n; j; j--) {         if (used[j + k]) cnt++;         if (!used[j])           if (b[j] == cnt) cand = j;       }       cout << cand <<    ;       used[cand] = true;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_VALUE = 32768;   struct Tokenizer {     string input;     int at;     void tokenize(const string& s) {       input = s +  @ ;       at = 0;       token =   ;     }     char nextChar() { return input[at]; }     char nextNoneSpace() {       while (nextChar() ==    ) skipChar();       return nextChar();     }     void skipChar() { ++at; }     string token;     string nextToken() {       if (!token.empty()) return token;       char ch = nextNoneSpace();       if (isdigit(ch)) {         while (isdigit(nextChar())) {           token += nextChar();           skipChar();         }       } else if (isalpha(ch)) {         while (isalpha(nextChar())) {           token += nextChar();           skipChar();         }       } else {         token = nextChar();         skipChar();       }       return token;     }     void skipToken() { token =   ; }     bool hasMoreTokens() { return at < input.size(); }   };   Tokenizer tokenizer;   vector<string> tokens;   string input;   int what;   int n;   void readInput() {     cin >> what;     string line;     input =   ;     while (getline(cin, line)) input += line;   }   void tokenizeInput() {     tokenizer.tokenize(input);     while (tokenizer.hasMoreTokens()) {       tokens.push_back(tokenizer.nextToken());       tokenizer.skipToken();     }   }   int result[MAX_VALUE];   int at;   const string& nextToken() { return tokens[at]; }   void skipToken() { ++at; }   int parseFunction(int);   int parseOperatorSequence();   int parseOperator();   int parseLogicalExpr();   int parseArithmExpr();   int parseSum();   int parseProduct();   int parseMultiplier();   int parseNumber();   void eat(const string& s) { skipToken(); }   int parseFunction() {     eat( int );     eat( f );     eat( ( );     eat( int );     eat( n );     eat( ) );     eat( { );     int e = parseOperatorSequence();     return e;   }   int parseOperatorSequence() {     while (nextToken() !=  } ) {       int e = parseOperator();       if (e != -1) return e;     }     return -1;   }   int parseOperator() {     if (nextToken() ==  return ) {       skipToken();       return parseArithmExpr();     } else {       eat( if );       eat( ( );       int ok = parseLogicalExpr();       eat( ) );       eat( return );       int res = parseArithmExpr();       eat( ; );       if (ok)         return res;       else         return -1;     }   }   int parseLogicalExpr() {     int a = parseArithmExpr();     string token = nextToken();     if (token ==  > ) {       eat( > );       int b = parseArithmExpr();       return a > b;     } else if (token ==  < ) {       eat( < );       int b = parseArithmExpr();       return a < b;     } else {       eat( = );       eat( = );       int b = parseArithmExpr();       return a == b;     }   }   inline int moduleAdd(int a, int b) { return (a + b) % MAX_VALUE; }   inline int moduleMinus(int a, int b) {     a -= b;     if (a < 0) a += MAX_VALUE;     return a;   }   inline int moduleMult(int a, int b) { return (a * b) % MAX_VALUE; }   inline int moduleDiv(int a, int b) {     if (!b) return -1;     return a / b;   }   int parseArithmExpr() { return parseSum(); }   int parseSum() {     int e = parseProduct();     while (nextToken() ==  +  || nextToken() ==  - ) {       string token = nextToken();       skipToken();       if (token ==  + )         e = moduleAdd(e, parseProduct());       else         e = moduleMinus(e, parseProduct());     }     return e;   }   int parseProduct() {     int e = parseMultiplier();     while (nextToken() ==  *  || nextToken() ==  / ) {       string token = nextToken();       skipToken();       if (token ==  * )         e = moduleMult(e, parseMultiplier());       else         e = moduleDiv(e, parseMultiplier());     }     return e;   }   int parseMultiplier() {     if (nextToken() ==  n ) {       skipToken();       return n;     } else if (nextToken() ==  f ) {       eat( f );       eat( ( );       int arg = parseArithmExpr();       eat( ) );       return result[arg];     } else {       istringstream iss(nextToken());       skipToken();       int num;       iss >> num;       return num;     }   }   int main() {     readInput();     tokenizeInput();     int ans = -1;     for (int i = 0; i < MAX_VALUE; i++) {       at = 0;       n = i;       result[i] = parseFunction();       if (result[i] == what) ans = i;     }     printf( %d n , ans);   }