#include <bits/stdc++.h>   using namespace std;   int main() {     long long int x, y, l, r;     cin >> x >> y >> l >> r;     vector<long long int> vec(0);     long long int xa = 1;     long long int cap = 0;     long long int r1 = r;     while (r1 > 0) {       r1 /= x;       ++cap;     }     for (long long int i = 0; i < cap; ++i) {       long long int ya = 1;       long long int diff = r - xa;       long long int cap2 = 0;       while (diff > 0) {         diff /= y;         ++cap2;       }       for (long long int j = 0; j < cap2; ++j) {         if (xa + ya >= l && xa + ya <= r) {           vec.push_back(xa + ya);         }         ya *= y;       }       xa *= x;     }     if (vec.size() == 0) {       cout << r - l + 1;       return 0;     }     sort(vec.begin(), vec.end());     long long int answer = vec[0] - l;     for (int i = 1; i < vec.size(); ++i) {       if (vec[i] - vec[i - 1] - 1 > answer) answer = vec[i] - vec[i - 1] - 1;     }     if (r - vec[vec.size() - 1] > answer) answer = r - vec[vec.size() - 1];     cout << answer;   }


#include <bits/stdc++.h>   const long long LLINF = 0x3f3f3f3f3f3f3f3f;   using namespace std;   class otr {    public:     long long len = 0;     long long* D = nullptr;     otr(long long alen) {       len = alen;       D = new long long[alen];     }     ~otr() { delete[] D; }   };   void Flip(long long* left, long long* right, long long flix, bool isleft) {     while (flix != -1) {       long long t = left[flix];       left[flix] = right[flix];       right[flix] = t;       flix = isleft ? right[flix] : left[flix];     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     std::cout.tie(0);     std::istream* istr = &cin;     const long long mdl = 1000000007;     long long q;     (*istr) >> q;     for (long long qi = 0; qi < q; qi++) {       long long n, m, a, b, c;       (*istr) >> n;       otr** data = new otr*[n - 1];       for (long long i = 0; i < n - 1; i++) {         (*istr) >> m;         data[i] = new otr(m);         for (long long j = 0; j < m; j++) (*istr) >> data[i]->D[j];       }       for (long long i = 0; i < n - 2; i++)         for (long long j = i + 1; j < n - 1; j++)           if (data[i]->len > data[j]->len) {             otr* t = data[i];             data[i] = data[j];             data[j] = t;           }       long long skippedcnt = 0;       long long* left = new long long[n];       long long* right = new long long[n];       long long start = -1;       long long end = -1;       long long qend = -1;       map<long long, bool> mp;       long long ix = 0;       bool* skip = new bool[n - 1];       while (ix < n - 1 && data[ix]->len == 2) {         for (long long i = 0; i < n - 1; i++) skip[i] = false;         skippedcnt = 0;         for (long long i = 0; i < n; i++) {           left[i] = -1;           right[i] = -1;         }         mp.clear();         right[data[ix]->D[0] - 1] = data[ix]->D[1] - 1;         left[data[ix]->D[1] - 1] = data[ix]->D[0] - 1;         long long lastskippedcnt = skippedcnt;         skip[ix] = true;         skippedcnt++;         mp[data[ix]->D[0] - 1] = true;         mp[data[ix]->D[1] - 1] = true;         start = data[ix]->D[0] - 1;         end = data[ix]->D[1] - 1;         qend = end;         bool isq = true;         bool isok = true;         while (skippedcnt < n - 1) {           long long iy = 0;           lastskippedcnt = skippedcnt;           while (iy < n - 1) {             if (!skip[iy]) {               bool startisthere = false;               bool endisthere = false;               long long newone = -1;               long long newonecount = 0;               for (long long i = 0; i < data[iy]->len; i++) {                 if (data[iy]->D[i] - 1 == start) startisthere = true;                 if (data[iy]->D[i] - 1 == qend) endisthere = true;                 if (mp.find(data[iy]->D[i] - 1) == mp.end()) {                   newone = data[iy]->D[i] - 1;                   newonecount++;                 }               }               if (newonecount > 1) {                 iy++;                 continue;               }               skip[iy] = true;               skippedcnt++;               if (startisthere && !endisthere) {                 Flip(left, right, qend, true);                 long long t = start;                 start = qend;                 qend = t;                 if (isq) end = t;                 if (left[start] >= 0) {                   left[left[start]] = qend;                   right[qend] = left[start];                   left[start] = -1;                 }               }               left[newone] = end;               right[end] = newone;               end = newone;               mp[newone] = true;               if (isq && data[iy]->len == 2)                 qend = end;               else                 isq = false;               break;             }             iy++;           }           if (lastskippedcnt == skippedcnt) {             isok = false;             break;           }         }         if (isok) break;         ix++;       }       long long v = start;       while (v >= 0) {         cout << v + 1 <<    ;         v = right[v];       }       cout << endl;       for (long long i = 0; i < n - 1; i++) delete data[i];       delete[] data;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x;     vector<pair<int, int>> v;     cin >> n;     for (int i = 1; i <= n && cin >> x; i++) v.push_back({x, i});     sort(v.begin(), v.end());     cout << v[n - 1].second <<     << v[n - 2].first;   }


#include <bits/stdc++.h>   using namespace std;   int z[1005];   string a[1005];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     for (int i = 0; i < n; ++i) cin >> a[i];     for (int j = 0; j < m; ++j) {       for (int i = 1; i < n; ++i)         for (int i1 = 0; i1 < i; ++i1) {           bool ok = false;           for (int k = 0; k < j; ++k)             if (z[k] == 0 && a[i][k] > a[i1][k]) {               ok = true;               break;             }           if (!ok && a[i][j] < a[i1][j]) z[j] = 1;         }     }     int ans = 0;     for (int i = 0; i < m; ++i)       if (z[i]) ++ans;     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int mark[123456];   int main() {     int n, t;     scanf( %d , &n);     for (int i = 0; i < n - 1; i++) {       scanf( %d , &t);       mark[t]++;     }     int ans = 0;     for (int i = 1; i <= n; i++) {       if (!mark[i]) {         ans = i;         break;       }     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int a[500009], posin[500009];   vector<int> pairs[500009];   int main() {     int n, m, x, y, c = 0;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       posin[a[i]] = i;     }     for (int i = 0; i < m; i++) {       scanf( %d %d , &x, &y);       pairs[x].push_back(posin[y]);       pairs[y].push_back(posin[x]);     }     x = -1, y = 0;     long long int sol = 0;     while (y < n && x < y) {       if (c) {         x++;         for (int i = 0; i < pairs[a[x]].size(); i++)           if (pairs[a[x]][i] > x && pairs[a[x]][i] <= y) {             c--;           }       } else {         sol += (y - x);         y++;         for (int i = 0; i < pairs[a[y]].size(); i++)           if (pairs[a[y]][i] > x && pairs[a[y]][i] <= y) {             c++;           }       }     }     printf( %I64d n , sol);   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     pair<int, int> a[101];     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i].first >> a[i].second;     }     int t;     cin >> t;     for (int i = n - 1; i >= 0; i--) {       if (a[i].first <= t) {         cout << n - i << endl;         break;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int q, n;   bool ok(long long edg) {     long long b = (edg + 1) >> 1, o = (n - b) * (n - b - 1) >> 1;     return (b + min(edg >> 1, o)) == edg && b < n;   }   long long bs() {     long long beg = 0, end = 2LL * n, mid;     while (beg < end) {       mid = (beg + end + 1) >> 1;       if (ok(mid))         beg = mid;       else         end = mid - 1;     }     return beg;   }   int main() {     for (scanf( %d , &q); q--;) {       scanf( %d , &n);       long long ans = bs();       printf( %lld n , ans);     }   }


#include <bits/stdc++.h>   using std::cin;   using std::cout;   const int N = 500054;   int n, Z, Hz;   int a[N], z[N];   std::vector<int> r[N];   std::pair<int, int> w[N];   std::priority_queue<int, std::vector<int>, std::greater<int>> pq;   inline void up(int &x, const int y) { x < y ? x = y : 0; }   inline void down(int &x, const int y) { x > y ? x = y : 0; }   inline int min(const int x, const int y) { return x < y ? x : y; }   void work() {     int i, ans = 0, x = 0, M = 0;     cin >> n;     for (Z = i = 0; i < n; ++i)       if (cin >> a[i], !a[i]) z[Z++] = i;     if (Z < 2) {       cout <<  0  <<   n ;       return;     }     Hz = Z / 2, std::fill(w + 1, w + (n + 1), std::pair<int, int>(0, Hz + 1));     for (i = 0; i < n; ++i)       if (a[i])         i < z[Hz] ? up(w[a[i]].first, x) : down(w[a[i]].second, x);       else if (i == z[Hz])         x = 2 - (Z & 1);       else         ++x;     for (i = 1; i <= n; ++i)       if (w[i].first + 1 >= w[i].second)         ++ans;       else if (0 < w[i].first || w[i].second <= Hz)         r[w[i].first].emplace_back(w[i].second);     memset(z, 0, (Hz + 1) << 2);     for (x = 0; x <= Hz; r[x].clear(), ++x)       for (int y : r[x])         if (pq.emplace(y), M < x)           ++M;         else           ++z[pq.top()], pq.pop();     ans += M;     for (i = 1, x = Hz; x >= 0; --x, ++i) M = min(z[x], i), ans += M, i -= M;     for (; !pq.empty(); pq.pop())       ;     cout << min(ans + M, Hz) <<   n ;   }   int main() {     int T;     std::ios::sync_with_stdio(false), cin.tie(NULL);     for (cin >> T; T; --T) work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, a[20000], p[20000], i, l, j, mx, ans[20000], mxi;   int main() {     cin >> n;     for (i = 0; i < n; i++) {       cin >> a[i];     }     for (i = 0; i < n; i++) {       mx = 0;       for (j = i; j < n; j++) {         p[a[j]]++;         if (p[a[j]] > mx or (p[a[j]] == mx and a[j] < mxi)) {           mx = p[a[j]];           mxi = a[j];         }         ans[mxi]++;       }       for (j = i + 1; j <= n; j++) p[a[j]] = 0;     }     for (i = 1; i <= n; i++) {       cout << ans[i] <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, A, B, X, Ans = 0, C = 0;   int main() {     cin >> N >> A >> B;     for (int i = 1; i <= N; i++) {       cin >> X;       if (X == 1) {         if (A > 0)           A--;         else if (B > 0)           C++, B--;         else if (C > 0)           C--;         else           Ans++;       } else {         if (B > 0)           B--;         else           Ans += 2;       }     }     cout << Ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 10;   long long n, m, k, ki[maxn], d[maxn], par[maxn];   long long ans;   vector<pair<int, int>> veci[maxn];   bool mark[maxn], fu;   set<pair<long long, pair<int, int>>> st;   vector<pair<long long, pair<int, int>>> yal;   int get(int u) { return par[u] = par[u] == u ? u : get(par[u]); }   int main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 0; i < m; i++) {       int u, v, w;       cin >> u >> v >> w;       veci[v].push_back({u, w});       veci[u].push_back({v, w});       yal.push_back({w, {u, v}});     }     cin >> k;     for (int i = 0; i < k; i++) {       int a;       cin >> a;       mark[a] = 1;       ki[a] = a;       for (auto x : veci[a])         if (mark[x.first] == 0) st.insert({x.second, {a, x.first}});       par[a] = a;       if (a == 1) fu = 1;     }     while (st.size()) {       auto y = *st.begin();       int u = y.second.second;       st.erase(y);       if (mark[u] == 1) continue;       mark[u] = 1;       ki[u] = y.second.first;       d[u] = y.first;       for (auto x : veci[u])         if (mark[x.first] == 0) st.insert({d[u] + x.second, {ki[u], x.first}});     }     st.clear();     for (auto x : yal)       st.insert({d[x.second.first] + d[x.second.second] + x.first,                  {ki[x.second.first], ki[x.second.second]}});     while (st.size()) {       auto x = *st.begin();       long long u = x.second.first, v = x.second.second, w = x.first;       st.erase(x);       u = get(u);       v = get(v);       if (u != v) {         par[u] = v;         ans += w;       }     }     if (fu == 1)       cout << ans;     else       cout << (long long)ans + d[1];   }


#include <bits/stdc++.h>   using namespace std;   int n, M[2002][2002];   bool V[2002];   vector<int> L, L_;   void dfs(int inicial, vector<int> &Li) {     Li.push_back(inicial);     V[inicial] = 1;     for (int i = 0; i < n; ++i)       if (!V[i] && M[inicial][i]) dfs(i, Li);   }   bool Conexo() {     dfs(0, L);     for (int i = 0; i < n; ++i)       for (int j = 0; j <= i; ++j) swap(M[i][j], M[j][i]);     memset(V, 0, sizeof V);     dfs(0, L_);     return L.size() == n && L_.size() == n;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i)       for (int j = 0; j < n; ++j) scanf( %d , &M[i][j]);     if (Conexo())       printf( YES n );     else       printf( NO n );     return 0;   }


#include <bits/stdc++.h>   const int N = 8;   int n, x;   double p[1 << N];   void fwt(int l, int r) {     if (l + 1 == r) return;     int mid = (l + r) >> 1;     fwt(l, mid);     fwt(mid, r);     int t = mid - l;     for (int i = l; i < mid; ++i) {       double x = p[i], y = p[i + t];       p[i] = x + y;       p[i + t] = x - y;     }   }   void ifwt(int l, int r) {     if (l + 1 == r) return;     int mid = (l + r) >> 1;     ifwt(l, mid);     ifwt(mid, r);     int t = mid - l;     for (int i = l; i < mid; ++i) {       double x = p[i], y = p[i + t];       p[i] = (x + y) / 2.;       p[i + t] = (x - y) / 2.;     }   }   int main() {     scanf( %d%d , &n, &x);     for (int i = 0; i <= x; ++i) scanf( %lf , &p[i]);     fwt(0, 1 << N);     for (int i = 0; i < (1 << N); ++i) p[i] = pow(p[i], n);     ifwt(0, 1 << N);     double ans = 0.;     for (int i = 1; i < (1 << N); ++i) ans += p[i];     printf( %.12f n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     vector<int> res(n);     for (auto &v : res) cin >> v;     int sum = 0, mini = INT_MAX, index = -1, i;     for (i = 0; i < n; i++) {       if (i < k) {         sum += res[i];         continue;       } else {         if (mini > sum) {           mini = sum;           index = i;         }         sum -= res[i - k];         sum += res[i];       }     }     if (mini > sum) index = i;     mini == INT_MAX ? cout << 1 : cout << (index - k + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100005];   int prime[100005 + 5000];   int a2[10005];   map<int, int> M;   int f[100005];   int Gcd[50005];   int sum[50005];   int gcd(int a, int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int main() {     int i, j, k = 0;     for (i = 2; i <= 100000; i++) {       if (a[i] == 0) {         prime[k++] = i;         for (j = i + i; j <= 100000; j += i) a[j] = 1;       }     }     int n, m;     while (cin >> n >> m) {       M.clear();       for (i = 1; i <= n; i++) {         cin >> a2[i];         j = a2[i];         for (int t = 0; t < k; t++) {           if (j == 1) break;           if (j % prime[t] == 0) {             while (j % prime[t] == 0) j /= prime[t];           }         }         if (j > 1) prime[k++] = j;       }       for (i = 0; i < m; i++) {         cin >> j;         M[j] = 1;       }       int ans = 0;       for (i = 1; i <= n; i++) Gcd[i] = 1;       for (i = 0; i < k; i++) {         int f = 0;         if (M[prime[i]]) f = 1;         int X = 1;         for (j = 1; j <= n; j++) {           int tmp = 0;           int Y = 1;           if (a2[j] % prime[i] == 0) {             while (a2[j] % prime[i] == 0) {               a2[j] /= prime[i];               tmp++;               Y *= prime[i];             }           }           if (f)             ans -= tmp;           else             ans += tmp;           if (j == 1)             X = Y;           else if (X > Y)             X = Y;           Gcd[j] *= X;         }       }       int de = 0;       for (i = n; i >= 1; i--) {         int tmp = Gcd[i];         if (Gcd[i] == Gcd[i + 1]) continue;         int s = 0;         for (j = 0; j < k; j++) {           int tmp2 = 0;           if (tmp == 1) break;           if (tmp % prime[j] == 0) {             while (tmp % prime[j] == 0) {               tmp /= prime[j];               tmp2++;             }           }           if (M[prime[j]])             s -= tmp2;           else             s += tmp2;         }         if (s - de < 0) {           ans -= i * (s - de);           de += s - de;         }       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   inline int toInt(string s) {     int v;     istringstream sin(s);     sin >> v;     return v;   }   template <class T>   inline string toString(T x) {     ostringstream sout;     sout << x;     return sout.str();   }   const long long MOD = 1000000007;   long long inv[105];   vector<vector<long long>> matmul(vector<vector<long long>> a,                                    vector<vector<long long>> b) {     int n = a.size();     vector<vector<long long>> ret =         vector<vector<long long>>(n, vector<long long>(n, 0));     for (int i = 0; i < (int)n; ++i)       for (int j = 0; j < (int)n; ++j)         for (int k = 0; k < (int)n; ++k)           ret[i][j] = (ret[i][j] + a[i][k] * b[k][j]) % MOD;     return ret;   }   vector<long long> matvecmul(vector<vector<long long>> a, vector<long long> b) {     int n = a.size();     auto ret = vector<long long>(n, 0);     for (int i = 0; i < (int)n; ++i)       for (int k = 0; k < (int)n; ++k) ret[i] = (ret[i] + a[i][k] * b[k]) % MOD;     return ret;   }   vector<vector<long long>> matpow(vector<vector<long long>> a, long long p) {     vector<vector<long long>> tmp = a;     int n = a.size();     vector<vector<long long>> ret =         vector<vector<long long>>(n, vector<long long>(n, 0LL));     for (int i = 0; i < (int)n; ++i) ret[i][i] = 1LL;     while (p) {       if (p & 1) ret = matmul(ret, tmp);       tmp = matmul(tmp, tmp);       p >>= 1;     }     return ret;   }   vector<pair<long long, int>> f(long long n) {     vector<pair<long long, int>> ret;     for (long long i = 2; i * i <= n; i++) {       int cnt = 0;       while (n % i == 0) {         cnt++;         n /= i;       }       if (cnt != 0) ret.push_back(make_pair(i, cnt));     }     if (n != 1) ret.push_back(make_pair(n, 1));     return ret;   }   void init() {     inv[1] = 1LL;     for (int i = 2; i < 100; i++) {       inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;     }   }   int main() {     long long n, k;     init();     cin >> n >> k;     vector<vector<long long>> mat(51, vector<long long>(51, 0));     for (int i = 0; i < (int)51; ++i) {       for (int j = i; j < (int)51; ++j) {         mat[i][j] = inv[j + 1];       }     }     mat = matpow(mat, k);     auto ff = f(n);     long long ret = 1;     for (auto fc : ff) {       long long ex = 0;       long long p = 1;       vector<long long> vec(51, 0);       vec[fc.second] = 1LL;       vec = matvecmul(mat, vec);       for (int i = 0; i < (int)fc.second + 1; ++i) {         ex = (ex + (p * vec[i]) % MOD) % MOD;         p = (p * fc.first) % MOD;       }       ret = (ret * ex) % MOD;     }     cout << ret << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int nax = 2e5 + 10;   int first[nax], last[nax], a[nax];   int reach[nax];   int main() {     fill_n(first, nax, 1e9);     fill_n(last, nax, -1e9);     ios::sync_with_stdio(0);     cin.tie(0);     int n, q;     cin >> n >> q;     for (int i = 0; i < n; i++) {       cin >> a[i];       first[a[i]] = min(first[a[i]], i);       last[a[i]] = max(last[a[i]], i);       reach[i] = i;     }     for (int v = 0; v < nax; v++) {       if (last[v] == -1e9) continue;       reach[first[v]] = max(reach[first[v]], last[v]);     }     int ans = 0, r = 0;     map<int, int> count;     for (int i = 0; i <= n; i++) {       if (i >= r) {         int ma = 0, sum = 0;         for (auto p : count) {           ma = max(ma, p.second);           sum += p.second;         }         ans += sum - ma;         count.clear();       }       if (i == n) break;       count[a[i]]++;       r = max(r, reach[i] + 1);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<long long> a(n);     long long s = 0;     long long l = 0;     long long r = 1000000000000000;     for (int i = 0; i < n; i++) {       cin >> a[i];       s += a[i];       l = max(a[i], l);     }     long long is = r;     while (l <= r) {       long long mid = (l + r) / 2;       if (n * mid - s > s) {         is = min(is, mid);         r = mid - 1;       } else         l = mid + 1;     }     cout << is <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   long long n, a[N], sum;   vector<long long> v;   void fact() {     if (sum % 2 == 0) {       v.push_back(2);       while (sum % 2 == 0) sum /= 2;     }     for (long long i = 3; i * i <= sum; i += 2) {       if (sum % i == 0) {         v.push_back(i);         while (sum % i == 0) sum /= i;       }     }     if (sum > 2) v.push_back(sum);   }   long long solve(long long p) {     long long tmp, tmp2, res = 0;     for (int i = 1; i < n; i++) {       tmp = a[i] % p, tmp2 = p - tmp;       if (tmp < tmp2)         res += tmp;       else         res += tmp2;     }     return res;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       sum += a[i];       a[i] = a[i - 1] + a[i];     }     fact();     if (!v.size()) return cout <<  -1 n , 0;     long long ans = 1e18;     for (int i = 0; i < v.size(); i++) {       long long p = v[i];       ans = min(ans, solve(p));     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline bool cmin(int &x, int y) { return x > y ? x = y, 1 : 0; }   inline bool cmax(int &x, int y) { return x < y ? x = y, 1 : 0; }   const int _ = 1e5 + 55;   int N, x;   long long ans(0);   priority_queue<int> A, B;   int main() {     scanf( %d , &N);     for (int i(1), I(N); i <= I; ++i) scanf( %d , &x), A.push(x);     for (int i(1), I(N); i <= I; ++i) scanf( %d , &x), B.push(x);     for (int i(1), I(N); i <= I; ++i) {       if (!A.size())         B.pop();       else if (!B.size())         ans += A.top(), A.pop();       else if (A.top() > B.top())         ans += A.top(), A.pop();       else         B.pop();       if (!A.size())         ans -= B.top(), B.pop();       else if (!B.size())         A.pop();       else if (A.top() > B.top())         A.pop();       else         ans -= B.top(), B.pop();     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int out = 0, fh = 1;     char jp = getchar();     while ((jp >  9  || jp <  0 ) && jp !=  - ) jp = getchar();     if (jp ==  - ) fh = -1, jp = getchar();     while (jp >=  0  && jp <=  9 ) out = out * 10 + jp -  0 , jp = getchar();     return out * fh;   }   const int P = 998244353;   int add(int a, int b) { return (a + b >= P) ? (a + b - P) : (a + b); }   void inc(int &a, int b) { a = add(a, b); }   int mul(int a, int b) { return 1LL * a * b % P; }   int fpow(int a, int b) {     int res = 1;     while (b) {       if (b & 1) res = mul(res, a);       a = mul(a, a);       b >>= 1;     }     return res;   }   const int N = 2019;   char buf[N];   int n, f[N][N], s[N];   int sum(int L, int R) {     if (L > R) return 0;     return add(s[R], P - s[L - 1]);   }   int main() {     scanf( %s , buf + 1);     n = strlen(buf + 1);     for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + (buf[i] ==  ? );     for (int len = 2; len <= n; ++len)       for (int i = 1; i + len - 1 <= n; ++i) {         int j = i + len - 1;         if (buf[i] !=  ( ) inc(f[i][j], f[i + 1][j]);         if (buf[j] !=  ) ) inc(f[i][j], f[i][j - 1]);         if (buf[i] !=  (  && buf[j] !=  ) ) inc(f[i][j], P - f[i + 1][j - 1]);         if (buf[i] !=  )  && buf[j] !=  ( )           inc(f[i][j], add(f[i + 1][j - 1], fpow(2, sum(i + 1, j - 1))));       }     cout << f[1][n] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long q;     cin >> q;     while (q--) {       long long n;       cin >> n;       vector<long long> vec(n, 0);       vector<bool> vis(n, false);       vector<pair<long long, long long>> p(n);       for (long long i = 0; i < n; i++) cin >> vec[i];       for (long long i = 0; i < n; i++) {         if (!vis[i]) {           vis[i] = true;           long long j = vec[i] - 1;           long long days = 1;           while (j != i) {             p[j] = make_pair(1, i);             vis[j] = true;             j = vec[j] - 1;             days++;           }           p[i] = make_pair(0, days);         }       }       for (long long i = 0; i < n; i++) {         if (p[i].first == 0)           cout << p[i].second <<    ;         else           cout << p[p[i].second].second <<    ;       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(0);     int n, a, b;     cin >> n >> a >> b;     int ans = 0;     int c[n];     for (int i = 0; i < n; i++) cin >> c[i];     int st = 0;     int en = n - 1;     while (st <= en) {       if (st == en) {         if (c[st] == 2) {           if (a < b) {             c[st] = 0;             ans += a;           } else             ans += b;         }       } else if (c[st] == c[en] && c[st] != 2) {         st++, en--;         continue;       } else {         if (c[st] == 2) {           if (c[en] == 1)             ans += b, c[st] = 1;           else if (c[en] == 0)             ans += a, c[st] = 0;           else {             if (a < b) {               c[en] = 0;               c[st] = 0;               ans += 2 * a;             } else {               c[en] = 1;               c[st] = 1;               ans += 2 * b;             }           }         } else if (c[en] == 2) {           if (c[st] == 1)             ans += b, c[en] = 1;           else if (c[st] == 0)             ans += a, c[en] = 0;           else {             if (a < b) {               c[en] = 0;               c[st] = 0;               ans += 2 * a;             } else {               c[en] = 1;               c[st] = 1;               ans += 2 * b;             }           }         }       }       st++;       en--;     }     st = 0;     en = n - 1;     while (st <= en) {       if (c[st] != c[en]) {         cout << -1;         return 0;       }       st++;       en--;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   inline void write(int x) {     if (x > 9) write(x / 10);     putchar(x % 10 +  0 );   }   inline void writeln(int x) {     write(x);     putchar(  n );   }   const int N = 1000005;   struct node {     int l, r;   } b[N];   struct que {     int l, r, id;   } c[N];   int now;   int Ans[N];   int n, k, q;   int Mx[N * 4], pos[N * 4];   int Mxdis[N];   int stkc[N], topc;   int Pos, ans;   inline bool cmp2(node a, node b) {     return a.r < b.r || (a.r == b.r && a.l > b.l);   }   inline bool cmp3(que a, que b) {     return a.r < b.r || (a.r == b.r && a.l > b.l);   }   inline void change(int x, int y) {     int l = 1, r = n, num = 1;     while (1) {       int mid = (l + r) >> 1;       if (Mx[num] < y) pos[num] = x, Mx[num] = y;       if (l == r) break;       if (x <= mid)         r = mid, num = (num << 1);       else         l = mid + 1, num = (num << 1 | 1);     }   }   inline void query(int num, int l, int r, int x, int y) {     if (x <= l && r <= y) {       if (Mx[num] > ans) {         Pos = pos[num];         ans = Mx[num];       }       return;     }     int mid = (l + r) >> 1;     if (x <= mid) query(num << 1, l, mid, x, y);     if (y > mid) query(num << 1 | 1, mid + 1, r, x, y);   }   int main() {     n = read();     k = read();     for (int i = 1; i <= k; ++i) b[i].l = read(), b[i].r = read();     q = read();     for (int i = 1; i <= q; ++i) c[i].l = read(), c[i].r = read(), c[i].id = i;     sort(b + 1, b + k + 1, cmp2);     sort(c + 1, c + q + 1, cmp3);     now = 1;     for (int i = 1; i <= n; ++i) Mxdis[i] = i;     for (int i = 1; i <= q; ++i) {       while (now <= k && b[now].r <= c[i].r) {         if (Mxdis[b[now].l] < b[now].r) {           change(b[now].l, b[now].r);           Mxdis[b[now].l] = b[now].r;         }         ++now;       }       topc = 0;       stkc[++topc] = c[i].l;       int last = c[i].l;       while (1) {         int x = stkc[topc];         Pos = 0, ans = 0;         query(1, 1, n, last, Mxdis[x]);         if (ans > Mxdis[x])           stkc[++topc] = Pos, last = Mxdis[x];         else           break;       }       int mm = Mxdis[stkc[topc--]];       while (topc > 0) {         change(stkc[topc], mm);         Mxdis[stkc[topc--]] = mm;       }       Ans[c[i].id] = mm;     }     for (int i = 1; i <= q; ++i) writeln(Ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline bool chmax(T &a, T b) {     if (a < b) {       a = b;       return true;     }     return false;   }   template <class T>   inline bool chmin(T &a, T b) {     if (a > b) {       a = b;       return true;     }     return false;   }   template <class Z>   Z rng(Z a, Z b) {     static mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());     return uniform_int_distribution<Z>(a, b - 1)(mt);   }   struct RollingHash {     static constexpr uint64_t P0 = 1e9 + 7;     static constexpr uint64_t P1 = 1e9 + 9;     static uint64_t B0;     static uint64_t B1;     static vector<uint64_t> powB0;     static vector<uint64_t> powB1;     const int n;     vector<uint64_t> h0;     vector<uint64_t> h1;     template <class Itr>     RollingHash(Itr first, Itr last)         : n(distance(first, last)), h0(n + 1), h1(n + 1) {       for (int i = 0; i < n; ++i, ++first) {         h0[i + 1] = (h0[i] * B0 + *first) % P0;         h1[i + 1] = (h1[i] * B1 + *first) % P1;       }       while (powB0.size() <= n) {         powB0.push_back(powB0.back() * B0 % P0);         powB1.push_back(powB1.back() * B1 % P1);       }     }     uint64_t get0(int l, int r) {       return (h0[r] + (P0 - h0[l]) * powB0[r - l]) % P0;     }     uint64_t get1(int l, int r) {       return (h1[r] + (P1 - h1[l]) * powB1[r - l]) % P1;     }     bool eq(int l0, int r0, int l1, int r1) {       return get0(l0, r0) == get0(l1, r1) and get1(l0, r0) == get1(l1, r1);     }     template <class Itr>     static uint64_t get0(Itr first, Itr last) {       uint64_t res = 0;       while (first != last) {         res = (res * B0 + *first++) % P0;       }       return res;     }     template <class Itr>     static uint64_t get1(Itr first, Itr last) {       uint64_t res = 0;       while (first != last) {         res = (res * B1 + *first++) % P1;       }       return res;     }   };   uint64_t RollingHash::B0 = rng<unsigned>(1, RollingHash::P0);   uint64_t RollingHash::B1 = rng<unsigned>(1, RollingHash::P1);   vector<uint64_t> RollingHash::powB0{1};   vector<uint64_t> RollingHash::powB1{1};   long long beki(long long a, long long b, long long mod) {     if (b == 0) return 1;     long long ret = beki(a, b / 2, mod);     ret = (ret * ret) % mod;     if (b & 1) ret = (ret * a) % mod;     return ret;   }   long long B0;   long long B1;   long long P0;   long long P1;   long long B0inv;   long long B1inv;   vector<long long> v;   pair<long long, long long> now;   map<pair<long long, long long>, long long> mp;   set<pair<long long, long long> > st;   void solve() {     long long N;     cin >> N;     v.clear();     mp.clear();     st.clear();     mp[{0, 0}] = 1;     now.first = 0;     now.second = 0;     for (int i = 1; i <= N; i++) {       long long a;       cin >> a;       if (!v.empty() && v.back() == a) {         v.pop_back();         now.first -= a;         now.second -= a;         now.first = (now.first + P0) % P0;         now.second = (now.second + P1) % P1;         now.first = (now.first * B0inv) % P0;         now.second = (now.second * B1inv) % P1;         st.insert(now);         mp[now]++;       } else {         v.push_back(a);         now.first = (now.first * B0 + a) % P0;         now.second = (now.second * B1 + a) % P1;         st.insert(now);         mp[now]++;       }     }     long long ans = 0;     for (auto itr = st.begin(); itr != st.end(); itr++) {       ans += mp[*itr] * (mp[*itr] - 1) / 2;     }     cout << ans << endl;   }   int main() {     cin.tie(0);     ios::sync_with_stdio(false);     long long Q;     cin >> Q;     B0 = RollingHash::B0;     B1 = RollingHash::B1;     P0 = RollingHash::P0;     P1 = RollingHash::P1;     B0inv = beki(B0, P0 - 2, P0);     B1inv = beki(B1, P1 - 2, P1);     while (Q--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10, inf = 0x3f3f3f3f, mod = 1000000007;   const long long INF = 0x3f3f3f3f3f3f3f3f;   const double eps = 1e-9;   void read() {}   template <typename T, typename... T2>   inline void read(T &x, T2 &...oth) {     x = 0;     int ch = getchar(), f = 0;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = 1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = (x << 1) + (x << 3) + (ch ^ 48);       ch = getchar();     }     if (f) x = -x;     read(oth...);   }   int pre[maxn][2], ok = 0, tar = 0, vv[maxn];   bool vis[maxn][2];   list<int> ans;   vector<int> G[maxn];   void dfs(int x, bool sta) {     if (vis[x][sta] || ok == 2) return;     ans.push_back(x);     vv[x] = 1;     vis[x][sta] = 1;     for (int &v : G[x]) {       if (vv[v] == 1) ok = max(ok, 1);       if (vis[v][!sta]) continue;       if (G[v].size() == 0 && !sta && ok != 2) {         ok = 2;         ans.push_back(v);       }       dfs(v, !sta);     }     if (ok != 2) ans.pop_back();     vv[x] = 2;   }   signed main(signed argc, char const *argv[]) {     std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     int n, m, s;     cin >> n >> m;     for (int i = 1, c, v; i <= n; i++) {       cin >> c;       for (int j = 1; j <= c; j++) {         cin >> v;         G[i].push_back(v);       }     }     cin >> s;     dfs(s, 0);     if (ok == 2) {       cout <<  Win  << endl;       for (int &i : ans) cout << i <<    ;     } else if (ok == 1)       cout <<  Draw  << endl;     else if (ok == 0)       cout <<  Lose  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class TH>   void _dbg(const char *sdbg, TH h) {     cout << sdbg <<   =   << h << endl;   }   template <class TH, class... TA>   void _dbg(const char *sdbg, TH h, TA... a) {     while (*sdbg !=  , ) cout << *sdbg++;     cout <<   =   << h <<  ,  ;     _dbg(sdbg + 1, a...);   }   const long long mod = 1e9 + 7;   const long long M = 1e6 + 5;   const double pi = acos(-1.);   const long long dx[] = {1, 0, -1, 0, -1, -1, 1, 1};   const long long dy[] = {0, 1, 0, -1, 1, -1, 1, -1};   long long x, y, z, maxi, mini, ans, mid, low, high, m, n, a, b;   string s1, s2;   bool check(long long p) {     p = abs(p);     while (p) {       int k = p % 10;       if (k == 8) return true;       p /= 10;     }     return false;   }   void solve() {     cin >> n;     x = n;     x++;     while (!check(x)) x++;     cout << abs(x - n);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void umax(T &a, T b) {     if (a < b) a = b;   }   template <class T>   inline void umin(T &a, T b) {     if (a > b) a = b;   }   const int N = 2e5;   long long X, Y, x[N], y[N];   long long nx, bx;   long long ny, by;   long long basx, basy;   int main() {     cin >> nx >> bx;     for (int i = 1; i <= nx; i++) cin >> x[i];     cin >> ny >> by;     for (int i = 1; i <= ny; i++) cin >> y[i];     basx = basy = 1;     for (int i = nx; i >= 1; i--) {       X += basx * x[i];       basx *= bx;     }     for (int i = ny; i >= 1; i--) {       Y += basy * y[i];       basy *= by;     }     if (X < Y)       cout <<  < ;     else if (X > Y)       cout <<  > ;     else       cout <<  = ;     return !1;   }


#include <bits/stdc++.h>   const int SZ = 2e5 + 5;   using namespace std;   int n, p[SZ];   string s, t;   bool valid(int idx) {     string tmp = s;     for (int i = 0; i < idx; i++) {       tmp[p[i]] =  . ;     }     string tmp2 =   ;     for (int i = 0; i < n; i++) {       if (tmp[i] !=  . ) tmp2 += tmp[i];     }     int i, j;     int m = tmp2.size();     int k = t.size();     for (i = 0, j = 0; i < m && j < k; i++) {       if (t[j] == tmp2[i]) j++;     }     return (j == k);   }   int binarySearch() {     int f = 0, e = n;     while (f <= e) {       int m = f + e >> 1;       if (valid(m))         f = m + 1;       else         e = m - 1;     }     return f - 1;   }   int main() {     cin >> s >> t;     n = s.size();     for (int i = 0; i < n; i++) {       cin >> p[i];       p[i]--;     }     cout << binarySearch();   }


#include <bits/stdc++.h>   using namespace std;   int v[100005];   stack<pair<int, int> > S;   vector<pair<int, int> > Q;   int main() {     int N;     scanf( %d , &N);     for (int i = 0; i < N; i++) scanf( %d , &v[i]);     ++N;     v[N] = 0;     for (int i = 0; i < N; i++) {       int a = v[i];       if (S.empty() && !a) continue;       if (S.empty() || a > S.top().first) {         int l = (S.empty() ? 0 : S.top().first);         for (int j = l + 1; j <= a; j++) S.push(pair<int, int>(j, i));       }       if (a < S.top().first) {         int l = S.top().first;         for (int j = l; j > a; j--) {           Q.push_back(pair<int, int>(S.top().second, i));           S.pop();         }       }     }     cout << Q.size() << endl;     for (int i = 0; i < (int)Q.size(); i++)       printf( %d %d n , Q[i].first + 1, Q[i].second);   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 200100;   int N, M;   vector<int> edge[MAXN];   int dep[MAXN];   int croute[MAXN];   int par[MAXN];   pair<int, int> cover[MAXN];   bool seen[MAXN];   void run(int cloc, int last, int cdep) {     seen[cloc] = true;     croute[cdep] = cloc;     dep[cloc] = cdep;     par[cloc] = last;     for (int i = 0; i < edge[cloc].size(); i++) {       int neigh = edge[cloc][i];       if (neigh == last) continue;       if (!seen[neigh])         run(neigh, cloc, cdep + 1);       else {         int s = dep[neigh];         for (int j = s + 1; j <= cdep; j++) {           int k = croute[j];           if (cover[k].first != -1) {             int a = cover[k].first, b = cover[k].second;             int c = neigh, d = cloc;             if (dep[a] > dep[c]) {               swap(a, c);               swap(b, d);             }             vector<int> x, y, z;             int goalx = b, goaly = d;             while (dep[goalx] > dep[goaly]) goalx = par[goalx];             while (dep[goaly] > dep[goalx]) goaly = par[goaly];             while (goalx != goaly) {               goalx = par[goalx];               goaly = par[goaly];             }             int goal = goalx;             int n = c;             x.push_back(n);             while (n != a) {               n = par[n];               x.push_back(n);             }             x.push_back(b);             n = b;             while (n != goal) {               n = par[n];               x.push_back(n);             }             y.push_back(c);             y.push_back(d);             n = d;             while (n != goal) {               n = par[n];               y.push_back(n);             }             n = goal;             z.push_back(n);             while (n != c) {               n = par[n];               z.push_back(n);             }             cout <<  YES n ;             cout << x.size();             for (int m = 0; m < x.size(); m++) cout <<     << x[m] + 1;             cout <<   n ;             cout << y.size();             for (int m = 0; m < y.size(); m++) cout <<     << y[m] + 1;             cout <<   n ;             cout << z.size();             for (int m = z.size() - 1; m >= 0; m--) cout <<     << z[m] + 1;             cout <<   n ;             exit(0);           } else             cover[k] = make_pair(neigh, cloc);         }       }     }   }   int main() {     for (int i = 0; i < MAXN; i++) {       cover[i] = make_pair(-1, -1);       seen[i] = false;     }     cin >> N >> M;     for (int i = 0; i < M; i++) {       int a, b;       cin >> a >> b;       a--, b--;       edge[a].push_back(b);       edge[b].push_back(a);     }     for (int i = 0; i < N; i++) {       if (!seen[i]) {         run(i, -1, 0);       }     }     cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000000 + 10;   const int INF = 10000000;   int n;   struct Number {     int L, R, num;   } a[N];   const bool comp(const Number &A, const Number &B) { return A.num > B.num; }   int main() {     scanf( %d , &n);     int pos;     for (int i = 1; i <= N; ++i)       a[i - 1].L = INF, a[i - 1].R = -INF, a[i - 1].num = 0;     for (int i = 1; i <= n; ++i) {       scanf( %d , &pos);       ++a[pos].num;       a[pos].L = min(i, a[pos].L);       a[pos].R = max(i, a[pos].R);     }     sort(a + 1, a + N, comp);     int Max = a[1].num, tmp = 1;     int ans1, ans2, ans3 = INF;     while (a[tmp].num == Max) {       if (a[tmp].R - a[tmp].L + 1 < ans3) {         ans3 = a[tmp].R - a[tmp].L + 1;         ans1 = a[tmp].L, ans2 = a[tmp].R;       }       ++tmp;     }     printf( %d %d n , ans1, ans2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename A>   string to_string(A* ptr) {     stringstream ss;     ss <<  0x  << std::setw(16) << std::setfill( 0 ) << std::hex        << (uint64_t)(uintptr_t)ptr;     return ss.str();   }   string to_string(char c) { return ((string)     + c) +    ; }   template <typename A, typename B>   string to_string(pair<A, B> p);   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> p);   template <typename A, typename B, typename C, typename D>   string to_string(tuple<A, B, C, D> p);   string to_string(const string& s) { return     + s +    ; }   string to_string(const char* s) { return to_string((string)s); }   string to_string(bool b) { return (b ?  true  :  false ); }   string to_string(vector<bool> v) {     bool first = true;     string res =  { ;     for (int i = 0; i < static_cast<int>(v.size()); i++) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(v[i]);     }     res +=  } ;     return res;   }   template <size_t N>   string to_string(bitset<N> v) {     string res =   ;     for (size_t i = 0; i < N; i++) {       res += static_cast<char>( 0  + v[i]);     }     return res;   }   template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto& x : v) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> p) {     return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   +            to_string(get<2>(p)) +  ) ;   }   template <typename A, typename B, typename C, typename D>   string to_string(tuple<A, B, C, D> p) {     return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   +            to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ;   }   void debug_out() { cerr << endl; }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     debug_out(T...);   }   const int DIM = 6;   pair<bool, vector<string>> solve2(const vector<string>& S) {     pair<bool, vector<string>> NO_ANS = {false, {}};     const string& A = S[0];     const string& B = S[1];     const string& C = S[2];     const string& D = S[3];     const string& E = S[4];     const string& F = S[5];     if (A.size() + C.size() != B.size() + 1) return NO_ANS;     if (D.size() + F.size() != E.size() + 1) return NO_ANS;     if (A[0] != D[0]) return NO_ANS;     if (A.back() != E[0]) return NO_ANS;     if (B[0] != D.back()) return NO_ANS;     if (B[A.size() - 1] != E[D.size() - 1]) return NO_ANS;     if (B.back() != F[0]) return NO_ANS;     if (C[0] != E.back()) return NO_ANS;     if (C.back() != F.back()) return NO_ANS;     string line;     for (int i = 0; i < (int)B.size(); i++) line +=  . ;     vector<string> R(E.size(), line);     for (int i = 0; i < (int)A.size(); i++) R[0][i] = A[i];     for (int i = 0; i < (int)B.size(); i++) R[(int)D.size() - 1][i] = B[i];     for (int i = 0; i < (int)C.size(); i++)       R[(int)E.size() - 1][i + (int)A.size() - 1] = C[i];     for (int j = 0; j < (int)D.size(); j++) R[j][0] = D[j];     for (int j = 0; j < (int)E.size(); j++) R[j][(int)A.size() - 1] = E[j];     for (int j = 0; j < (int)F.size(); j++)       R[j + (int)D.size() - 1][(int)B.size() - 1] = F[j];     return {true, R};   }   string solve(vector<string>& S) {     sort(S.begin(), S.end());     pair<bool, vector<string>> result = {true, {}};     do {       auto [ok, res] = solve2(S);       result = min(result, {!ok, res});     } while (next_permutation(S.begin(), S.end()));     if (result.first)       return  Impossible ;     else {       string ans =   ;       for (string s : result.second) ans += s +   n ;       return ans;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int TT;     TT = 1;     for (int ttt = 0; ttt < TT; ttt++) {       vector<string> S(DIM);       for (string& s : S) cin >> s;       cout << solve(S);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e5 + 5;   int n;   pair<long long, int> t[N];   long long tree[4 * N], dp[N], v[N];   int rnk[N];   inline void update(int idx, long long val, int st, int en, int root) {     if (idx < st || idx > en) return;     if (st == en) {       tree[root] = max(tree[root], val);       return;     }     int mid = (st + en) >> 1;     update(idx, val, st, mid, root * 2);     update(idx, val, mid + 1, en, root * 2 + 1);     tree[root] = max(tree[root * 2], tree[root * 2 + 1]);   }   inline long long query(int L, int R, int st, int en, int root) {     if (L > en || R < st) return 0;     if (st >= L && en <= R) return tree[root];     int mid = (st + en) >> 1;     return max(query(L, R, st, mid, root * 2),                query(L, R, mid + 1, en, root * 2 + 1));   }   map<long long, int> m;   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       int h, r;       scanf( %d %d , &r, &h);       v[i] = 1LL * r * r * h;       m[v[i]];     }     int id = 0;     for (auto &x : m) x.second = id++;     for (int i = 0; i < n; i++) {       int rnk = m[v[i]];       dp[i] = v[i];       long long best = query(0, rnk - 1, 0, n - 1, 1);       dp[i] += best;       update(rnk, dp[i], 0, n - 1, 1);     }     long long ret = 0;     for (int i = 0; i < n; i++) ret = max(ret, dp[i]);     cout << fixed << setprecision(12) << acos(-1) * ret <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<long long, long long>> bpow = {{1, 1}};   struct HashArr {     const long long base = 137137, mod[2] = {(int)1e9 + 7, (int)1e9 + 9};     vector<pair<long long, long long>> h;     void init(vector<int>& a) {       int n = a.size();       h.resize(n + 1);       bpow[0] = {1, 1};       for (int i = 0; i < n; i++) {         h[i + 1].first = (h[i].first * base + a[i]) % mod[0];         h[i + 1].second = (h[i].second * base + a[i]) % mod[1];       }       while ((int)bpow.size() < n + 1) {         bpow.push_back({bpow.back().first * base % mod[0],                         bpow.back().second * base % mod[1]});       }     }     long long getHash(int l, int r) {       assert(r >= l);       long long a =           (bpow[r - l].first * h[l].first - h[r].first + mod[0]) % mod[0];       long long b =           (bpow[r - l].second * h[l].second - h[r].second + mod[1]) % mod[1];       return a << 32 | b;     }   };   const int nax = 1e5;   const int B = 300;   struct Block {     bool bad;     vector<int> ends, starts;     HashArr endh, starth;     void init(vector<int>& a, int l, int r) {       ends.clear(), starts.clear();       bad = false;       for (int i = l; i < r; i++) {         if (a[i] < 0) {           if (starts.empty()) {             ends.push_back(-a[i]);           } else {             if (starts.back() != -a[i]) {               bad = true;               return;             } else {               starts.pop_back();             }           }         } else {           assert(a[i] > 0);           starts.push_back(a[i]);         }       }       reverse(ends.begin(), ends.end());       endh.init(ends);       starth.init(starts);     }   };   Block block[nax / B + 2];   struct Range {     Block& block;     int l, r;     Range(Block& b) : block(b) {       l = 0;       r = b.starts.size();     }   };   struct Joiner {     bool bad;     Joiner() { bad = false; }     vector<Range> starts;     void operator()(Block& b) {       bad |= b.bad;       if (bad) return;       bad = true;       int bl = 0, br = b.ends.size();       while (br > bl) {         if (starts.empty()) return;         Range& r = starts.back();         int aw = r.r - r.l;         int w = min(aw, br - bl);         if (r.block.starth.getHash(r.r - w, r.r) != b.endh.getHash(br - w, br))           return;         br -= w;         r.r -= w;         if (r.r == r.l) starts.pop_back();       }       if (b.starts.size()) starts.emplace_back(b);       bad = false;     }   };   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, k;     cin >> n >> k;     vector<int> a(n);     for (int& v : a) cin >> v;     for (int i = 0; i < n; i += B) {       block[i / B].init(a, i, min(i + B, n));     }     int q;     cin >> q;     while (q--) {       int type;       cin >> type;       if (type == 1) {         int i, t;         cin >> i >> t;         i--;         a[i] = t;         int l = i / B * B;         block[i / B].init(a, l, min(l + B, n));       } else if (type == 2) {         int l, r;         cin >> l >> r;         l--;         vector<Block> tmps;         tmps.reserve(2);         Joiner joiner;         int i = l;         while (i < r) {           int ni = min(i / B * B + B, r);           if (i % B == 0 && ni % B == 0) {             joiner(block[i / B]);           } else {             tmps.push_back(Block());             tmps.back().init(a, i, ni);             joiner(tmps.back());           }           i = ni;         }         cout << (joiner.bad || joiner.starts.size() ?  No  :  Yes ) <<   n ;       } else         assert(0);     }   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<pair<int, int>> moves_knight(int x, int y) {     vector<pair<int, int>> v;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (abs(x - i) + abs(y - j) == 3 && abs(x - i) * abs(y - j) == 2)           v.push_back({i, j});       }     }     return v;   }   vector<pair<int, int>> moves_bishop(int x, int y) {     vector<pair<int, int>> v;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (abs(x - i) == abs(y - j)) v.push_back({i, j});       }     }     return v;   }   vector<pair<int, int>> moves_rook(int x, int y) {     vector<pair<int, int>> v;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (x == i || y == j) v.push_back({i, j});       }     }     return v;   }   vector<pair<int, int>> moves(int x, int y, int c) {     if (c == 0) return moves_rook(x, y);     if (c == 1) return moves_bishop(x, y);     if (c == 2) return moves_knight(x, y);     return {};   }   int x[100], y[100];   int state(int x, int y, int c) { return c * n * n + x * n + y; }   struct resenje {     long long len, prom;     resenje(long long len = 123123123, long long prom = 123123123)         : len(len), prom(prom) {}     bool operator<(const resenje& o) const {       if (len != o.len) return len < o.len;       return prom < o.prom;     }     resenje operator+(const resenje& o) const {       return {len + o.len, prom + o.prom};     }   };   resenje d[300][300];   resenje g[3][100];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cerr.tie(nullptr);     cin >> n;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         int z;         cin >> z;         z--;         x[z] = i;         y[z] = j;       }     }     for (int c = 0; c < 3; c++) {       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           int u = state(i, j, c);           auto p = moves(i, j, c);           for (auto e : p) {             int v = state(e.first, e.second, c);             d[u][v] = min(d[u][v], resenje(1, 0));           }           d[u][u] = min(d[u][u], resenje(0, 0));           for (int z = 0; z < 3; z++)             if (z != c) {               int v = state(i, j, z);               d[u][v] = min(d[u][v], resenje(1, 1));             }         }       }     }     int m = 3 * n * n;     for (int j = 0; j < m; j++)       for (int i = 0; i < m; i++)         for (int k = 0; k < m; k++) d[i][k] = min(d[i][k], d[i][j] + d[j][k]);     g[0][0] = g[1][0] = g[2][0] = {0, 0};     for (int i = 1; i < n * n; i++) {       for (int c1 = 0; c1 < 3; c1++) {         for (int c2 = 0; c2 < 3; c2++) {           int u = state(x[i - 1], y[i - 1], c1);           int v = state(x[i], y[i], c2);           g[c2][i] = min(g[c2][i], g[c1][i - 1] + d[u][v]);         }       }     }     auto sol = min({g[0][n * n - 1], g[1][n * n - 1], g[2][n * n - 1]});     cout << sol.len <<     << sol.prom <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   char s[100010];   int l[100010], r[100010];   struct node {     int val, id;   };   struct cmp {     bool operator()(node a, node b) { return a.val < b.val; }   };   priority_queue<node, vector<node>, cmp> q;   int main() {     scanf( %s , s);     int len = strlen(s);     for (int i = (0); i <= (len - 1); ++i)       if (s[i] ==  ? ) scanf( %d %d , &l[i], &r[i]);     int num;     num = 0;     long long ans = 0;     node tmp;     for (int i = (0); i <= (len - 1); ++i) {       if (s[i] ==  ( )         num++;       else if (s[i] ==  ) )         num--;       else {         s[i] =  ) ;         ans += r[i];         tmp.val = r[i] - l[i];         tmp.id = i;         q.push(tmp);         num--;       }       if (num < 0) {         if (q.empty()) {           printf( -1 n );           return 0;         }         tmp = q.top();         q.pop();         num += 2;         s[tmp.id] =  ( ;         ans -= tmp.val;       }     }     if (num != 0) {       printf( -1 n );       return 0;     }     printf( %lld n , ans);     puts(s);   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 2e8;   struct st {     int l, r;   };   bool cmp(st a, st b) { return a.l < b.l; }   int main() {     bool a1 = false, b1 = false;     int i;     st v, q, w;     vector<st> v1, v2;     string s, a =  AB , b =  BA ;     cin >> s;     for (i = 0; i < s.size() - 1; i++) {       if (s.substr(i, 2) == a) {         v.l = i;         v.r = i + 1;         v1.push_back(v);       }       if (s.substr(i, 2) == b) {         v.l = i;         v.r = i + 1;         v2.push_back(v);       }     }     sort(v1.begin(), v1.end(), cmp);     sort(v2.begin(), v2.end(), cmp);     if (v1.size() == 0 || v2.size() == 0) {       cout <<  NO ;       return 0;     }     q = v1[0];     w = v2[v2.size() - 1];     if (q.l != w.l && q.l != w.r && q.r != w.l && q.r != w.r) {       cout <<  YES ;       return 0;     }     q = v2[0];     w = v1[v1.size() - 1];     if (q.l != w.l && q.l != w.r && q.r != w.l && q.r != w.r) {       cout <<  YES ;       return 0;     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int en[100004];   long long int dp[100004][3];   string x[100004];   int n;   long long int sol(int i, int ck) {     if (i > n) return 0;     if (dp[i][ck] != -1) return dp[i][ck];     string org = x[i];     long long int ans = 0;     long long int ans1 = 0;     if (!ck) {       string p = x[i - 1];       if (org >= p) {         ans = sol(i + 1, 0);       } else {         ans = 10000000000000000;       }       reverse(org.begin(), org.end());       if (org >= p) {         ans1 = min(ans, sol(i + 1, 1)) + en[i];       } else {         ans1 = 10000000000000000;       }       return dp[i][ck] = min(ans1, ans);     } else {       string p = x[i - 1];       reverse(p.begin(), p.end());       if (org >= p) {         ans = sol(i + 1, 0);       } else {         ans = 10000000000000000;       }       reverse(org.begin(), org.end());       if (org >= p) {         ans1 = min(ans, sol(i + 1, 1) + en[i]);       } else {         ans1 = 10000000000000000;       }       return dp[i][ck] = min(ans, ans1);     }   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) scanf( %d , &en[i]);     for (int i = 1; i <= n; i++) cin >> x[i];     memset(dp, -1, sizeof dp);     long long int ans = sol(2, 0);     ans = min(ans, en[1] + sol(2, 1));     if (ans == 10000000000000000) ans = -1;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long N, L, W;   vector<long long> neg, pos;   long long INF32 = 0x7fffffff;   inline long long div_floor(long long a, long long b) {     if (b == 0) return (a > 0 ? +INF32 : -INF32);     if (a % b < 0) a -= (b + a % b);     return a / b;   }   int main() {     cin >> N >> L >> W;     for (int i = 0; i < N; ++i) {       long long x, d;       cin >> x >> d;       (d == 1 ? pos : neg).push_back(d == 1 ? x : x + L);     }     sort(pos.begin(), pos.end());     sort(neg.begin(), neg.end());     long long ans = 0;     for (auto x : pos) {       long long lval = max(-x - 1, x);       if (W == 1) {         if (x < 0) {         } else {           lval = INF32;         }       } else {         lval = max(lval, div_floor(((W + 1) * x), (W - 1)));       }       auto l = upper_bound(neg.begin(), neg.end(), lval);       ans += int(neg.end() - l);       lval = max(x, div_floor((W - 1) * x, (W + 1)));       long long rval = -x;       l = upper_bound(neg.begin(), neg.end(), lval);       auto r = lower_bound(neg.begin(), neg.end(), rval);       ans += max(0, int(r - l));     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10;   const int MOD = 1e9 + 7;   int aa[N];   int tree[4 * N];   int query(int v, int l, int r, int L, int R) {     if (l > r || l > R || r < L) return 0;     if (L <= l && r <= R) return tree[v];     int v1 = 2 * v + 1;     int v2 = 2 * v + 2;     int mid = (l + r) / 2;     int q1 = query(v1, l, mid, L, R);     int q2 = query(v2, mid + 1, r, L, R);     return (q1 + q2) % MOD;   }   void update(int v, int l, int r, int pos, int newVal) {     if (l > r || l > pos || r < pos) return;     if (l == r && l == pos) {       tree[v] = newVal;       return;     }     int v1 = 2 * v + 1;     int v2 = 2 * v + 2;     int mid = (l + r) / 2;     update(v1, l, mid, pos, newVal);     update(v2, mid + 1, r, pos, newVal);     tree[v] = (tree[v1] + tree[v2]) % MOD;   }   int bpow(long long bb, int ee) {     if (ee < 0) return 0;     int res = 1;     while (ee > 0) {       if (ee % 2 == 1) res = res * bb % MOD;       bb = bb * bb % MOD;       ee >= 1;     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> aa[i];     const int ml = 1e6;     update(0, 0, ml, 0, 1);     long long ans = 0;     for (int i = 1; i <= n; i++) {       int s = query(0, 0, ml, 0, aa[i]);       int hm = (s * 1LL * aa[i]) % MOD;       update(0, 0, ml, aa[i], hm);     }     set<int> dict;     for (int i = 1; i <= n; i++) dict.insert(aa[i]);     for (int i : dict) ans += query(0, 0, ml, i, i);     cout << ans % MOD <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<long long> > room_to_switches;   vector<vector<pair<long long, long long> > > neighbors;   vector<long long> target;   void dfs(long long s, long long color, vector<long long>& colors) {     if (colors[s] != -1) {       if (color != colors[s]) {         cout <<  NO  << endl;         exit(0);       }       return;     }     colors[s] = color;     for (pair<long long, long long> P : neighbors[s]) {       long long v = P.first;       long long state = P.second;       dfs(v, color ^ state, colors);     }   }   int main() {     cin.tie(0);     ios_base::sync_with_stdio(0);     long long nRooms, nSwitches;     cin >> nRooms >> nSwitches;     for (int i = 0; i < nRooms; i++) {       long long x;       cin >> x;       target.push_back(1 - x);     }     room_to_switches.resize(nRooms);     neighbors.resize(nSwitches);     for (int i = 0; i < nSwitches; i++) {       long long k;       cin >> k;       for (int j = 0; j < k; j++) {         long long room;         cin >> room;         room--;         room_to_switches[room].push_back(i);         if (room_to_switches[room].size() == 2) {           long long u = room_to_switches[room][0];           long long v = room_to_switches[room][1];           neighbors[u].push_back({v, target[room]});           neighbors[v].push_back({u, target[room]});         }       }     }     vector<long long> colors(nSwitches, -1);     for (long long s = 0; s < nSwitches; s++) {       if (colors[s] == -1) dfs(s, 0, colors);     }     cout <<  YES  << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   char s[N];   int main() {     scanf( %s , s);     int n = strlen(s);     int x = 0, y = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  L )         x--;       else if (s[i] ==  R )         x++;       else if (s[i] ==  U )         y++;       else         y--;     }     if ((abs(x) + abs(y)) % 2 == 0)       cout << (abs(x) + abs(y)) / 2 << endl;     else       cout << -1 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000000 + 5;   vector<int> g[N], revg[N], cur;   bool used[N], isans[N];   set<int> all;   int n, m;   int main() {     scanf( %d %d , &n, &m);     for (int i = 0; i < m; i++) {       int u, v;       scanf( %d %d , &u, &v);       u--, v--;       g[u].push_back(v);       revg[v].push_back(u);     }     for (int i = 0; i < n; i++) {       all.insert(i);     }     while (!all.empty()) {       int k = *(all.begin());       for (auto x : g[k]) {         if (!used[x]) {           used[x] = true;           all.erase(x);         }       }       used[k] = true;       all.erase(k);       cur.push_back(k);     }     for (int i = cur.size() - 1; i >= 0; i--) {       int node = cur[i];       isans[node] = true;       for (auto x : revg[node]) {         if (isans[x]) {           isans[node] = false;         }       }     }     vector<int> ans;     for (int i = 0; i < n; i++) {       if (isans[i]) ans.push_back(i + 1);     }     printf( %d n , ans.size());     for (int i = 0; i < ans.size(); i++) {       printf( %d  , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 5e5 + 10, max_f = 800;   struct Rad {     int l, r;     vector<pair<long long, int> > ar;     long long s;     Rad() {       l = r = -1;       s = 0;     }   };   int n, q;   long long num[max_n];   Rad sq[max_n];   bool compare(const pair<long long, int> &a, const int &b) {     return a.first < b;   }   bool compare2(const int &b, const pair<long long, int> &a) {     return b < a.first;   }   void input() {     scanf( %d%d , &n, &q);     for (int i = 0; i < n; i++) scanf( %d , &num[i]);     for (int i = 0; 1; i++) {       sq[i].l = i * max_f;       sq[i].r = min((i + 1) * max_f, n);       for (int j = sq[i].l; j < sq[i].r; j++)         sq[i].ar.push_back(make_pair(num[j], j));       sort(sq[i].ar.begin(), sq[i].ar.end());       if (sq[i].r == n) break;     }   }   void modify(int l, int r, int x) {     for (int i = 0; sq[i].l != -1; i++) {       if (l <= sq[i].l && sq[i].r <= r) sq[i].s += x;       if (sq[i].l < l && l < sq[i].r) {         for (int j = l; j < min(sq[i].r, r); j++) num[j] += x;         for (int j = sq[i].l; j < sq[i].r; j++)           sq[i].ar[j - sq[i].l] = make_pair(num[j], j);         sort(sq[i].ar.begin(), sq[i].ar.end());       } else if (sq[i].l < r && r < sq[i].r) {         for (int j = sq[i].l; j < r; j++) num[j] += x;         for (int j = sq[i].l; j < sq[i].r; j++)           sq[i].ar[j - sq[i].l] = make_pair(num[j], j);         sort(sq[i].ar.begin(), sq[i].ar.end());       }     }   }   void output() {     int type, l, r, x, y;     int first, last, tmp, tmp2;     for (int i = 0; i < q; i++) {       scanf( %d , &type);       if (type == 1) {         scanf( %d%d%d , &l, &r, &x);         modify(l - 1, r, x);       } else {         scanf( %d , &y);         first = -1;         for (int j = 0; sq[j].l != -1; j++) {           tmp = lower_bound(sq[j].ar.begin(), sq[j].ar.end(), y - sq[j].s,                             compare) -                 sq[j].ar.begin();           tmp2 = upper_bound(sq[j].ar.begin(), sq[j].ar.end(), y - sq[j].s,                              compare2) -                  sq[j].ar.begin() - 1;           if (first == -1 && tmp != (int)sq[j].ar.size() &&               sq[j].ar[tmp].first == y - sq[j].s)             first = sq[j].ar[tmp].second;           if (tmp2 != -1 && sq[j].ar[tmp2].first == y - sq[j].s)             last = sq[j].ar[tmp2].second;         }         if (first == -1)           printf( -1 n );         else           printf( %d n , last - first);       }     }   }   int main() {     input();     output();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool sortbysec(const pair<long long, long long> &a,                  const pair<long long, long long> &b) {     return (a.second < b.second);   }   int main() {     long long n;     vector<long long> a;     vector<pair<long long, long long> > b;     cin >> n;     for (int i = int(0); i < int(n); i++) {       long long aux;       cin >> aux;       a.push_back(aux);     }     for (int i = int(0); i < int(n); i++) {       long long aux;       cin >> aux;       b.push_back(make_pair(aux, i));     }     sort(b.begin(), b.end());     reverse(b.begin(), b.end());     sort(a.begin(), a.end());     for (int i = int(0); i < int(n); i++) {       b[i].first = a[i];     }     sort(b.begin(), b.end(), sortbysec);     for (int i = int(0); i < int(n); i++) {       cout << b[i].first <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   const int inf = 0x3f3f3f3f;   int n, k, a[510][510];   int main() {     while (~scanf( %d%d , &n, &k)) {       int top = 1, ans = 0;       for (int i = 1; i <= n; i++) {         for (int j = 1; j < k; j++) {           a[i][j] = top++;         }       }       for (int i = 1; i <= n; i++) {         for (int j = k; j <= n; j++) {           if (j == k) ans += top;           a[i][j] = top++;         }       }       printf( %d n , ans);       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++) {           printf( %d , a[i][j]);           if (j == n)             putchar(  n );           else             putchar(   );         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m;     cin >> n >> m;     vector<long long> a(n);     vector<long long> b(n);     for (long long i = 0; i < n; i++) cin >> a[i];     for (long long i = 0; i < n; i++) cin >> b[i];     sort(b.begin(), b.end());     long long ans = m - 1;     for (long long i = 0; i < n; i++) {       long long z;       if (b[i] > a[0])         z = (b[i] - a[0]) % m;       else         z = (m + b[i] - a[0]) % m;       vector<long long> temp;       temp = a;       for (long long i = 0; i < n; i++) temp[i] = (temp[i] + z) % m;       sort(temp.begin(), temp.end());       if (temp == b) ans = min(ans, z);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 998244353;   const int M = 20;   pair<int, int> dp[M][(1 << 10)];   int a[M], K, ten[M];   int Count(int st) {     int rnt = 0;     while (st) st &= st - 1, rnt++;     return rnt;   }   pair<int, int> f(int pos, int st, int lead, int limit) {     if (pos == -1)       return Count(st) <= K ? pair<int, int>(0, 1) : pair<int, int>(0, 0);     if (!limit && dp[pos][st].first != -1) return dp[pos][st];     int up = limit ? a[pos] : 9;     pair<int, int> rnt = pair<int, int>(0, 0);     for (int k = 0; k <= up; k++) {       pair<int, int> p = f(pos - 1, k == 0 && lead ? 0 : st | (1 << k),                            k == 0 && lead, limit && k == up);       rnt.first =           (rnt.first + p.first + (long long)k * ten[pos] % MOD * p.second % MOD) %           MOD;       rnt.second = (rnt.second + p.second) % MOD;     }     if (!limit) dp[pos][st] = rnt;     return rnt;   }   pair<int, int> Solve(long long n) {     for (int i = 0; i < M; i++, n /= 10) a[i] = n % 10;     return f(M - 1, 0, 1, 1);   }   int main() {     for (int i = 0; i < M; i++) {       ten[i] = i == 0 ? 1 : ten[i - 1] * 10ll % MOD;       for (int j = 0; j < (1 << 10); j++) dp[i][j].first = -1;     }     long long l, r;     scanf( %I64d%I64d%d , &l, &r, &K);     pair<int, int> R = Solve(r), L = Solve(l - 1);     printf( %d n , (R.first - L.first + MOD) % MOD);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int c = 200002;   int sum, n, ans[c], pos;   vector<int> sz[c];   bool v[c];   bool dfs(int a) {     v[a] = true;     int el = a;     for (int i = 0; i < sz[a].size(); i++) {       int x = sz[a][i];       if (!v[x] && !dfs(x)) ans[el] = x, el = x;     }     if (el != a) {       ans[el] = a, sum -= 2;       return 1;     }     return 0;   }   int main() {     cin >> n, sum = 2 * n;     for (int i = 1; i < n; i++) {       int x, y;       cin >> x >> y;       sz[x].push_back(y), sz[y].push_back(x);     }     for (int i = 1; i <= n; i++)       if (sz[i].size() == 1) pos = sz[i][0];     dfs(pos);     cout << sum <<   n ;     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct query {     long long t, fi, si;   };   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     long long n;     cin >> n;     long long a[n];     map<long long, long long> f;     for (long long i = 0; i < n; i++) {       long long x;       cin >> x;       a[i] = x;       f[x]++;     }     long long maxn, maxf = -1;     for (auto i : f) {       if (i.second > maxf) {         maxf = i.second;         maxn = i.first;       }     }     vector<long long> index;     for (long long i = 0; i < n; i++) {       if (a[i] == maxn) index.push_back(i);     }     vector<query> ans;     for (auto i : index) {       long long j = i - 1;       while (a[j] != maxn && j >= 0) {         if (a[j] > a[j + 1]) {           ans.push_back({2, j + 1, j + 2});           a[j] = maxn;         } else if (a[j] < a[j + 1]) {           ans.push_back({1, j + 1, j + 2});           a[j] = maxn;         }         j--;       }     }     long long j = index.back() + 1;     while (j < n) {       if (a[j] > a[j - 1]) {         ans.push_back({2, j + 1, j});         a[j] = maxn;       } else if (a[j] < a[j - 1]) {         ans.push_back({1, j + 1, j});         a[j] = maxn;       }       j++;     }     cout << ans.size() <<   n ;     for (auto i : ans) cout << i.t <<     << i.fi <<     << i.si <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const double PI = acos(-1.0);   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     const int N = 1e2 + 10;     int n, m;     int v[N][2 * N];     cin >> n >> m;     for (int i = 0; i < n; i++) {       for (int j = 0; j < 2 * m; j++) {         cin >> v[i][j];       }     }     int ans = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < 2 * m; j += 2) {         if (v[i][j] == 1 or v[i][j + 1] == 1) ans++;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, s;     cin >> n >> s;     long long int min = INT_MAX;     unsigned long long int sum = 0;     long long int i;     long long int arr[n];     for (i = 0; i < n; i++) {       cin >> arr[i];       sum += arr[i];       if (arr[i] < min) min = arr[i];     }     if (sum < s) {       cout <<  -1 ;       return 0;     }     if (sum == s) {       cout <<  0 ;       return 0;     }     long long int count = 0;     for (i = 0; i < n; i++) {       if (arr[i] > min) count++;     }     for (i = 0; i < n; i++) {       if (count <= 0) break;       if (arr[i] > min) {         count -= 1;         if (s - (arr[i] - min) <= 0) {           cout << min;           return 0;         }         s = s - (arr[i] - min);         arr[i] = min;       }     }     while (s != 0 && min >= 0) {       if (s - (n) <= 0) {         cout << (min - 1);         return 0;       } else {         min--;         s = s - n;       }     }     cout <<  -1 ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 100030;   const long long MODD = 1000000009;   void do_case() {     int a, b, c, d, x, y;     cin >> x >> y >> a >> b >> c >> d;     if (x > y) swap(x, y);     pair<int, int> T[4] = {{a + c, max(b, d)},                            {a + d, max(b, c)},                            {b + c, max(a, d)},                            {b + d, max(a, c)}};     for (auto p : T) {       int X, Y;       tie(X, Y) = minmax(p.first, p.second);       if (X <= x && Y <= y) {         cout <<  YES  << endl;         return;       }     }     cout <<  NO  << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     do_case();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 500;   int a[maxn];   int main(void) {     int n, m;     cin >> n >> m;     int ans = 0;     for (int i = 1; i <= m; i++) {       int num;       scanf( %d , &num);       int x, temp;       int flag = 0;       scanf( %d , &x);       temp = x;       if (x == 1) flag = 1;       for (int j = 2; j <= num; j++) {         scanf( %d , &x);         if (flag == 1 && x == temp + 1) {           temp = x;           continue;         }         if (x == 1) {           temp = x;           flag = 1;         } else {           flag = 0;           ans++;         }       }     }     ans = ans + ans + m - 1;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     for (; ch <  0  || ch >  9 ; ch = getchar())       if (ch ==  - ) f = -1;     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ;     return x * f;   }   int n, m;   int dp[45][1 << 7][1 << 7];   int cnt[1 << 7];   void init() {     for (int i = 0; i < (1 << m); i++) {       for (int x = i; x; x >>= 1)         if ((x & 1)) cnt[i]++;       cnt[i] = m - cnt[i];     }   }   bool ok(int j, int k, int l) {     int state = k | (k << 1) | (k >> 1) | j | l;     return (state & ((1 << m) - 1)) == ((1 << m) - 1);   }   int main() {     n = read(), m = read();     if (n < m) swap(n, m);     init();     memset(dp, -0x3f3f3f3f, sizeof dp);     for (int i = 0; i < (1 << m); i++) dp[0][0][i] = 0;     for (int i = 0; i < n; i++)       for (int j = 0; j < (1 << m); j++)         for (int k = 0; k < (1 << m); k++)           if (dp[i][j][k] >= 0)             for (int l = 0; l < (1 << m); l++)               if (ok(j, k, l))                 dp[i + 1][k][l] = max(dp[i + 1][k][l], dp[i][j][k] + cnt[k]);     int ans = 0;     for (int i = 0; i < (1 << m); i++) ans = max(ans, dp[n][i][0]);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }   bool des(int i, int j) { return i > j; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     if (n == 5)       cout << 1;     else       cout << n % 3 + 1;   }


#include <bits/stdc++.h>   using namespace std;   int main(){    ios::sync_with_stdio(0);    cin.tie(0); cout.tie(0);    int T; cin >> T;     while(T--){     int X[210] = {};      int N, M; cin >> N >> M;      for(int i = 0; i < N; i++){      for(int j = 0; j < M; j++){       int a; cin >> a;        X[i+j] ^= a;       }     }     bool ok = true;      for(int i = 0; i < N+M-1; i++){      if(X[i]) ok = false;      }     cout << (ok ?  Jeel n  :  Ashish n );     }   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void chkmax(T &x, T y) {     if (x < y) x = y;   }   template <class T>   inline void chkmin(T &x, T y) {     if (x > y) x = y;   }   int t, n, m, k, cnt[26];   bool vis[200020];   char s[200020];   int solve() {     int ans = 1000000007;     for (int i = 0; i < n; i++) vis[i] = 0;     vis[0] = 1;     for (int i = 0; i < 26; i++) {       for (int j = n - 1; j >= 0; j--) {         if (vis[j]) {           if (j + cnt[i] >= n) {             if (j + cnt[i] + m <= k) return 0;             chkmin(ans, (n - j) * (j + cnt[i] + m - k));           } else             vis[j + cnt[i]] = 1;         }       }     }     for (int i = 0; i < m; i++) vis[i] = 0;     vis[0] = 1;     for (int i = 0; i < 26; i++) {       for (int j = m - 1; j >= 0; j--) {         if (vis[j]) {           if (j + cnt[i] >= m) {             if (j + cnt[i] + n <= k) return 0;             chkmin(ans, (m - j) * (j + cnt[i] + n - k));           } else             vis[j + cnt[i]] = 1;         }       }     }     return ans;   }   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d %d %d , &n, &m, &k);       scanf( %s , s);       memset(cnt, 0, sizeof cnt);       for (int i = 0; i < k; i++) cnt[s[i] -  A ]++;       sort(cnt, cnt + 26, greater<int>());       printf( %d n , solve());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<pair<int, int>, int> a[100010];   vector<pair<long long, int> > pre[100010];   long long maxx = 0;   int maxi, maxj;   int c = 0;   long long s = 0;   long long val1, val2;   void eval(int i, int j, int k, bool f) {     val2 = pre[j][k].first;     if (val1 + val2 > maxx) {       maxx = val1 + val2;       maxi = pre[i][pre[i].size() - 1].second;       maxj = pre[j][k].second;       if (f) swap(maxi, maxj);     }   }   vector<int> out;   int main() {     int n, i;     scanf( %d , &n);     for (i = 0; i < n; ++i) {       scanf( %d , &a[i].first.first);       scanf( %d , &a[i].first.second);       a[i].first.second = -a[i].first.second;       a[i].second = i;     }     sort(a, a + n);     s = -a[0].first.second;     c++;     pre[c].push_back(make_pair(s, 0));     for (i = 1; i < n; ++i) {       if (a[i].first.first == a[i - 1].first.first) {         s -= a[i].first.second;         c++;       } else {         s = -a[i].first.second;         c = 1;       }       pre[c].push_back(make_pair(s, i));     }     for (i = 1; i < 100010; ++i) {       if (pre[i].size() == 0) break;       sort(pre[i].begin(), pre[i].end());     }     for (i = 1; i < 100010; ++i) {       if (pre[i].size() == 0) break;       val1 = pre[i][pre[i].size() - 1].first;       if (i > 1) {         if (pre[i - 1].size()) {           if (a[pre[i - 1][pre[i - 1].size() - 1].second].first.first !=               a[pre[i][pre[i].size() - 1].second].first.first)             eval(i, i - 1, pre[i - 1].size() - 1, 0);           else if (pre[i - 1].size() > 1)             eval(i, i - 1, pre[i - 1].size() - 2, 0);         }       }       if (pre[i].size() > 1) eval(i, i, pre[i].size() - 2, 0);       if (pre[i + 1].size()) {         if (a[pre[i + 1][pre[i + 1].size() - 1].second].first.first !=             a[pre[i][pre[i].size() - 1].second].first.first)           eval(i, i + 1, pre[i + 1].size() - 1, 1);         else if (pre[i + 1].size() > 1)           eval(i, i + 1, pre[i + 1].size() - 2, 1);       }     }     while (maxj) {       if (a[maxj - 1].first.first != a[maxj].first.first) break;       out.push_back(a[maxi].second + 1);       out.push_back(a[maxj].second + 1);       --maxi;       --maxj;     }     out.push_back(a[maxi].second + 1);     out.push_back(a[maxj].second + 1);     if (maxi) {       if (a[maxi - 1].first.first == a[maxi].first.first)         out.push_back(a[maxi - 1].second + 1);     }     printf( %lld n%d n , maxx, (int)out.size());     for (i = 0; i < out.size(); ++i) printf( %d  , out[i]);   }


#include <bits/stdc++.h>   using namespace std;   long long dp[100001];   bool seen[100001];   int n, q;   vector<int> v, c;   long long cal() {     long long a, b;     cin >> a >> b;     for (int i = 0; i < 100001; i++) seen[i] = false;     int c1, c2;     c1 = c2 = -1;     for (int i = 0; i < n; i++) {       long long maxV = b * v[i];       if (seen[c[i]]) {         maxV =             (((dp[c[i]] + a * v[i]) > (maxV)) ? (dp[c[i]] + a * v[i]) : (maxV));       }       if (c1 >= 0 && c1 != c[i]) {         maxV = (((dp[c1] + b * v[i]) > (maxV)) ? (dp[c1] + b * v[i]) : (maxV));       }       if (c2 >= 0 && c2 != c[i]) {         maxV = (((dp[c2] + b * v[i]) > (maxV)) ? (dp[c2] + b * v[i]) : (maxV));       }       if (!seen[c[i]]) dp[c[i]] = maxV;       seen[c[i]] = true;       dp[c[i]] = (((maxV) > (dp[c[i]])) ? (maxV) : (dp[c[i]]));       if (c1 < 0 || maxV > dp[c1]) {         c2 = c1;         c1 = c[i];       } else if (c2 < 0 || maxV > dp[c2] && c[i] != c1) {         c2 = c[i];       }     }     if (c1 >= 0) return (((dp[c1]) > (0LL)) ? (dp[c1]) : (0LL));     return 0LL;   }   int main() {     cin >> n >> q;     for (int i = 0; i < n; i++) {       int tmp;       cin >> tmp;       v.push_back(tmp);     }     for (int i = 0; i < n; i++) {       int tmp;       cin >> tmp;       c.push_back(tmp);     }     for (int i = 0; i < q; i++) cout << cal() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long sum;   struct myS {     int index;     long long v;     myS(){};     myS(int index, long long v) : index(index), v(v){};     bool operator<(const myS& right) const { return v < right.v; }   } a[200001];   bool dd[1000001];   vector<int> res;   int main() {     cin.tie(0);     ios::sync_with_stdio(0);     cout.tie(0);     ;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i].v;       sum += a[i].v;       a[i].index = i;     }     sort(a + 1, a + n + 1);     for (int i = 1; i <= n; i++) {       long long le = sum - a[i].v;       if (le % 2 == 1) continue;       long long need = (le >> 1);       auto it = lower_bound(a + 1, a + n + 1, myS(0, need));       if ((*it).v == need && (*it).index != a[i].index)         res.push_back(a[i].index);       else if ((*it).v == need && (*it).index == a[i].index) {         auto it2 = upper_bound(a + 1, a + n + 1, myS(0, need));         while (it != it2) {           if ((*it).index != a[i].index) {             res.push_back(a[i].index);             break;           } else             it++;         }       }     }     cout << res.size() << endl;     for (auto i : res) cout << i <<    ;   }


#include <bits/stdc++.h>   using namespace std;   template <class T1>   void deb(T1 e1) {     cout << e1 << endl;   }   template <class T1, class T2>   void deb(T1 e1, T2 e2) {     cout << e1 <<     << e2 << endl;   }   template <class T1, class T2, class T3>   void deb(T1 e1, T2 e2, T3 e3) {     cout << e1 <<     << e2 <<     << e3 << endl;   }   template <class T1, class T2, class T3, class T4>   void deb(T1 e1, T2 e2, T3 e3, T4 e4) {     cout << e1 <<     << e2 <<     << e3 <<     << e4 << endl;   }   template <class T1, class T2, class T3, class T4, class T5>   void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {     cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 << endl;   }   template <class T1, class T2, class T3, class T4, class T5, class T6>   void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {     cout << e1 <<     << e2 <<     << e3 <<     << e4 <<     << e5 <<     << e6          << endl;   }   int main() {     long long n, m, x, y, sum = 0, total;     vector<long> v, v1, v2;     set<int> s, s1, s2;     map<long long, bool> Mymap;     scanf( %I64d %I64d , &n, &m);     for (long long int i = 1; i <= n; i++) {       cin >> x;       Mymap[x]++;     }     for (long long int j = 1; j <= m; j++) {       if (Mymap[j] != true) {         m -= j;         v.push_back(j);       }     }     cout << v.size() << endl;     for (auto it : v) {       cout << it <<    ;     }     cout << endl;     v.clear();     Mymap.clear();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long len[1000006];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n, q, t, x, s, m, p, d, l;     cin >> n >> m >> l;     for (int i = 0; i < n; i++) {       cin >> len[i];     }     long long co = 0;     long long pre;     if (n == 1) {       if (len[0] > l) co++;       for (int i = 0; i < m; i++) {         cin >> x;         if (x == 1) {           cin >> p >> d;           pre = len[p - 1];           len[p - 1] += d;           if (len[p - 1] > l && pre <= l) {             co++;           }         } else           cout << co <<   n ;       }       return 0;     }     for (int i = 0; i < n; i++) {       if (len[i] > l) {         co++;         int j = i;         while (j < n && len[j] > l) {           j++;         }         if (j > i) i = j;       }     }     long long prev;     for (int i = 0; i < m; i++) {       cin >> x;       if (x == 1) {         cin >> p >> d;         p--;         prev = len[p];         len[p] += d;         if (prev <= l) {           if (len[p] > l) {             if (p == 0) {               if (len[p + 1] > l)                 continue;               else {                 co++;                 continue;               }             } else if (p == n - 1) {               if (len[p - 1] > l)                 continue;               else                 co++;             } else {               if (len[p - 1] > l && len[p + 1] > l)                 co--;               else if (len[p - 1] <= l && len[p + 1] <= l)                 co++;             }           }         }       } else {         cout << max(1ll * 0, co) <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int www = 9;   const int Size = 10000;   char buffer[Size];   int dr[] = {-1, 0, 1, 0};   int dc[] = {0, 1, 0, -1};   const int inf = 0x0fffffff;   const int size = 52;   long long dp[size][10];   int ar[size];   struct Point {     int x, y, z;     Point(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {}   };   Point operator-(const Point &a, const Point &b) {     return Point(a.x - b.x, a.y - b.y, a.z - b.z);   }   double dist(const Point &a) {     return sqrt((double)a.x * a.x + (double)a.y * a.y + (double)a.z * a.z);   }   int solution(int nTest) {     int n;     scanf( %d , &n);     vector<Point> v;     for (int i = 0; i < n; i++) {       int x, y, z;       scanf( %d%d%d , &x, &y, &z);       v.push_back(Point(x, y, z));     }     double m = 10e8;     Point cent = v[0];     for (int i = 1; i < n; i++) {       for (int j = i + 1; j < n; j++) {         Point a = v[i], b = v[j];         double x = dist(a - cent);         double y = dist(b - cent);         double d = dist(a - b);         double time = (x + d - y) / 2 + y;         m = min(m, time);       }     }     printf( %.7lf n , m);     return 0;   }   int main() {     int i = 0, n = 9999;     while (i < n && solution(i)) {       i++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void _R(T &x) {     cin >> x;   }   void _R(int &x) { scanf( %d , &x); }   void _R(long long &x) { scanf( %lld , &x); }   void _R(double &x) { scanf( %lf , &x); }   void _R(char &x) { scanf(  %c , &x); }   void _R(char *x) { scanf( %s , x); }   void R() {}   template <class T, class... U>   void R(T &head, U &...tail) {     _R(head);     R(tail...);   }   template <class T>   void _W(const T &x) {     cout << x;   }   void _W(const int &x) { printf( %d , x); }   void _W(const long long &x) { printf( %lld , x); }   void _W(const double &x) { printf( %.16f , x); }   void _W(const char &x) { putchar(x); }   void _W(const char *x) { printf( %s , x); }   template <class T, class U>   void _W(const pair<T, U> &x) {     _W(x.first);     putchar(   );     _W(x.second);   }   template <class T>   void _W(const vector<T> &x) {     for (auto i = x.begin(); i != x.end(); _W(*i++))       if (i != x.cbegin()) putchar(   );   }   void W() {}   template <class T, class... U>   void W(const T &head, const U &...tail) {     _W(head);     putchar(sizeof...(tail) ?     :   n );     W(tail...);   }   int MOD = 1e9 + 7;   void ADD(long long &x, long long v) {     x = (x + v) % MOD;     if (x < 0) x += MOD;   }   const int SIZE = 1e6 + 10;   int lv[SIZE], lat[SIZE];   vector<int> e[SIZE];   int can[SIZE];   int AA;   void dfs0(int x, int lt, int v) {     lv[x] = v;     for (int y : e[x]) {       if (y == lt) continue;       lat[y] = x;       dfs0(y, x, v + 1);     }   }   int target;   int cnt(int x, int lt, int dd) {     if (!dd) {       target = x;       return 1;     }     int ret = 0;     for (int y : e[x]) {       if (y == lt) continue;       ret += cnt(y, x, dd - 1);     }     return ret;   }   int dp[SIZE];   int dfs2(int x, int lt, int dd) {     if (!dd) {       dp[x] = can[x];       return -1;     }     dp[x] = 0;     for (int y : e[x]) {       if (y == lt) continue;       int now = dfs2(y, x, dd - 1);       if (now != -1) return now;       dp[x] += dp[y];       if (dp[x] >= (AA + 1) / 2) return x;     }     return -1;   }   void clear(int x, int lt, int dd) {     if (!dd) {       if (can[x]) {         can[x] = 0;         AA--;       }       return;     }     for (int y : e[x]) {       if (y == lt) continue;       clear(y, x, dd - 1);     }   }   void clear2(int x, int lt, int stop, int dd) {     if (x == stop) return;     if (!dd) {       if (can[x]) {         can[x] = 0;         AA--;       }       return;     }     for (int y : e[x]) {       if (y == lt) continue;       clear2(y, x, stop, dd - 1);     }   }   int find_answer() {     for (int i = (1); i <= (SIZE); ++i) {       if (can[i]) return i;     }     return -1;   }   int qq(int x, int dd) {     if (AA == 1) {       return find_answer();     }     if (!dd) return x;     int qx = dfs2(x, lat[x], dd);     if (qx != x) {       W( d , qx);       fflush(stdout);       int v;       R(v);       if (lv[qx] + v != lv[x] + dd) {         clear(qx, lat[qx], lv[x] + dd - lv[qx]);         qx = x;       } else {         clear2(x, lat[x], qx, dd);       }     }     if (AA == 1) {       return find_answer();     }     W( s , qx);     fflush(stdout);     int v;     R(v);     clear2(x, lat[x], v, dd);     return qq(v, dd - (lv[v] - lv[x]));   }   int main() {     int n;     R(n);     for (int i = (1); i < (n); ++i) {       int x, y;       R(x, y);       e[x].push_back(y);       e[y].push_back(x);     }     dfs0(1, 1, 0);     int now = 1, dd;     W( d , now);     fflush(stdout);     R(dd);     for (int i = (1); i <= (n); ++i) {       if (lv[i] == dd) {         can[i] = 1;         AA++;       }     }     W( ! , qq(1, dd));     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int n, m;   struct AA {     int p, s;   } c[110];   int st[110];   bool cmp1(AA a, AA b) {     if (a.p == b.p) return a.s < b.s;     return a.p < b.p;   }   bool vis[110];   int work1() {     int ret = 0;     sort(c, c + n, cmp1);     sort(st, st + m);     for (int i = 0; i < n; i++) {       if (i == m) break;       if (c[i].p == 1) break;       if (c[i].s > st[m - 1 - i]) break;       ret += st[m - 1 - i] - c[i].s;     }     return ret;   }   int work2() {     int ret = 0;     sort(c, c + n, cmp1);     sort(st, st + m);     memset(vis, 0, sizeof(vis));     for (int i = 0; i < n; i++)       if (c[i].p == 1) {         bool fg = true;         for (int j = 0; j < m; j++)           if (!vis[j] && st[j] > c[i].s) {             vis[j] = true;             fg = false;             break;           }         if (fg) return 0;       }     for (int i = 0; i < n; i++)       if (c[i].p == 0) {         bool fg = true;         for (int j = 0; j < m; j++)           if (!vis[j] && st[j] >= c[i].s) {             vis[j] = true;             ret += st[j] - c[i].s;             fg = false;             break;           }         if (fg) return 0;       }     for (int j = 0; j < m; j++)       if (!vis[j]) ret += st[j];     return ret;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       char s[10];       scanf( %s , s);       if (s[0] ==  A )         c[i].p = 0;       else         c[i].p = 1;       scanf( %d , &c[i].s);     }     for (int i = 0; i < m; i++) scanf( %d , &st[i]);     cout << max(work1(), work2());   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t;     cin >> t;     while (t--) {       long long n, k;       cin >> n >> k;       long long n1 = n - (k - 1);       if (n1 > 0 && n1 % 2 == 1) {         cout <<  YES n ;         for (long long i = 0; i < k - 1; i++) cout <<  1  ;         cout << n1 <<   n ;         continue;       }       long long n2 = n - 2 * (k - 1);       if (n2 > 0 && n2 % 2 == 0) {         cout <<  YES n ;         for (long long i = 0; i < k - 1; i++) cout <<  2  ;         cout << n2 <<   n ;         continue;       }       cout <<  NO n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   int main() {     int n, K;     cin >> n >> K;     if (K == 1 || n <= 3) {       puts( -1 );     } else if (K >= n) {       puts( -1 );     } else if (n == 4) {       if (K == 3) {         puts( 3 n1 2 n2 3 n3 4 );       } else {         puts( -1 );       }     } else {       if (K == 3) {         vector<pair<int, int> > E;         E.push_back({1, 2});         E.push_back({2, 3});         E.push_back({3, 4});         for (int i = 2; i <= 3; ++i) {           for (int j = 5; j <= n; ++j) {             E.push_back({i, j});           }         }         printf( %d n , E.size());         for (auto it : E) {           printf( %d %d n , it.first, it.second);         }       } else if (K == 2) {         printf( %d n , n - 1);         for (int i = 1; i < n; ++i) {           printf( %d %d n , i, i + 1);         }       } else {         puts( -1 );       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   int N;   char s[105];   int dp[105][2605];   void pre() {     dp[0][0] = 1;     for (int i = 1; i <= 100; i++)       for (int j = 0; j <= 25 * i; j++)         for (int c = 0; c < 26; c++)           if (j - c >= 0) (dp[i][j] += dp[i - 1][j - c]) %= mod;   }   void solve_test() {     scanf( %s n , s + 1);     N = strlen(s + 1);     int cst = 0;     for (int i = 1; i <= N; i++) cst += s[i] -  a ;     int ans = (dp[N][cst] - 1 + mod) % mod;     printf( %d n , ans);   }   int main() {     pre();     int T;     scanf( %d n , &T);     for (int t = 1; t <= T; t++) solve_test();     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:200000000 )   using namespace std;   long long step(long long a, long long n) {     if (n == 0)       return 1;     else if (n % 2 == 0)       return step(a * a, n / 2);     else       return a * step(a, n - 1);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, k;     cin >> n >> k;     vector<int> A, d;     A.resize(n + 1);     d.resize(n + 1);     for (int i = 1; i <= n; i++) cin >> A[i];     vector<int> tmp(n + 1, 10e6);     for (int i = n; i >= 1; i--) {       d[i] = tmp[A[i]];       tmp[A[i]] = i;     }     set<pair<int, int>> cur;     set<int> used;     int ans = 0, last = 0;     for (int i = 0; i <= n; i++) tmp[i] = 0;     for (int i = 1; i <= n && used.size() < k; i++) {       if (used.find(A[i]) == used.end()) {         ans++;         used.insert(A[i]);         cur.insert(make_pair(-d[i], A[i]));         tmp[A[i]] = -d[i];       } else {         cur.erase(cur.find(make_pair(tmp[A[i]], A[i])));         cur.insert(make_pair(-d[i], A[i]));         tmp[A[i]] = -d[i];       }       last = i;     }     for (int i = last + 1; i <= n; i++) {       if (used.find(A[i]) != used.end()) {         cur.erase(cur.find(make_pair(tmp[A[i]], A[i])));         cur.insert(make_pair(-d[i], A[i]));         tmp[A[i]] = -d[i];       } else {         pair<int, int> t = *cur.begin();         cur.erase(cur.begin());         used.erase(t.second);         used.insert(A[i]);         cur.insert(make_pair(-d[i], A[i]));         tmp[A[i]] = -d[i];         ans++;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int a[200005];   int mv;   int main() {     scanf( %d , &n);     mv = n / 2;     if (n % 2 == 0) mv--;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     sort(a + 1, a + n + 1);     int ret = 2000000000;     for (int i = 0; i <= mv; i++) {       ret = min(ret, a[n - (mv - i)] - a[i + 1]);     }     printf( %d n , ret);     return 0;   }