#include <bits/stdc++.h>   using namespace std;   int dcmp(double a, double b) {     return ((fabs(a - b) < (1e-9)) ? 0 : (a < b) ? -1 : +1);   }   double crossP(const complex<double> &v1, const complex<double> &v2) {     return (conj(v1) * v2).imag();   }   double dotP(const complex<double> &v1, const complex<double> &v2) {     return (conj(v1) * v2).real();   }   double lengthSqr(const complex<double> &p) { return dotP(p, p); }   double length(const complex<double> &p) { return hypot(p.real(), p.imag()); }   double fixAngle(double a) { return ((a > 1) ? 1 : (a < -1) ? -1 : a); }   bool same(const complex<double> &p1, const complex<double> &p2) {     return ((dcmp(p1.real(), p2.real()) == 0 && dcmp(p1.imag(), p2.imag()) == 0)                 ? 1                 : 0);   }   double getAngle_A_abc(double a, double b, double c) {     return acos(fixAngle((b * b + c * c - a * a) / (2 * b * c)));   }   const int N = 3000 + 9;   map<int, vector<int> > v;   multiset<int> q;   long long calc(int mx, int n, int d) {     q.clear();     long long cost = 0;     for (const auto &a : v) {       const vector<int> &x = a.second;       int s = ((int)((x).size()));       int i = 0;       while (s > mx) d++, cost += x[i], s--, i++;       while (i < ((int)((x).size())) && d <= mx) q.emplace(x[i++]);     }     auto it = q.begin();     while (d <= mx) d++, cost += *it++;     return cost;   }   int main() {     int t, n;     scanf( %d %d , &t, &n);     long long d = 0;     for (__typeof(t) i = 0; i < t; ++i) {       int p, c;       scanf( %d %d , &p, &c);       --p;       if (p)         v[p].emplace_back(c);       else         d++;     }     int max_v = 0;     for (auto &a : v)       max_v = max(max_v, ((int)((a.second).size()))),       sort(a.second.begin(), a.second.end());     long long ans = (~(1LL << 63));     for (__typeof(max_v + 1) i = 0; i < max_v + 1; ++i)       ans = min(ans, calc(i, n, d));     printf( %I64d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, int> xMap;   map<int, int> yMap;   vector<pair<int, int> > row[200005], col[200005];   int x[200005], y[200005], tmr[200005], parent[200005];   int findrep(int u) {     if (parent[u] != u) parent[u] = findrep(parent[u]);     return parent[u];   }   void Union(int x, int y) {     int px = findrep(x);     int py = findrep(y);     if (px != py) {       parent[px] = py;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int t, n, m, i, j, k;     cin >> t;     while (t--) {       xMap.clear();       yMap.clear();       cin >> n >> k;       for (i = 0; i <= n; i++) {         row[i].clear();         col[i].clear();       }       int id1 = 0, id2 = 0;       for (i = 0; i < n; i++) {         cin >> x[i] >> y[i] >> tmr[i];         if (!xMap[x[i]]) {           id1++;           xMap[x[i]] = id1;         }         if (!yMap[y[i]]) {           id2++;           yMap[y[i]] = id2;         }         row[yMap[y[i]]].push_back({x[i], i});         col[xMap[x[i]]].push_back({y[i], i});       }       for (i = 1; i <= id1; i++) sort(col[i].begin(), col[i].end());       for (i = 1; i <= id2; i++) sort(row[i].begin(), row[i].end());       for (i = 0; i < n; i++) parent[i] = i;       for (i = 1; i <= id1; i++) {         int sz = col[i].size();         for (j = 1; j < sz; j++) {           if (col[i][j].first - col[i][j - 1].first <= k) {             Union(col[i][j].second, col[i][j - 1].second);           }         }       }       for (i = 1; i <= id2; i++) {         int sz = row[i].size();         for (j = 1; j < sz; j++) {           if (row[i][j].first - row[i][j - 1].first <= k) {             Union(row[i][j].second, row[i][j - 1].second);           }         }       }       for (i = 0; i < n; i++) {         j = findrep(i);         tmr[j] = min(tmr[j], tmr[i]);       }       vector<int> tt;       for (i = 0; i < n; i++) {         if (findrep(i) == i) tt.push_back(tmr[i]);       }       sort(tt.begin(), tt.end());       int ans = -1;       int sz = tt.size();       for (i = sz - 1; i >= 0; i--) {         if (tt[i] <= ans) break;         ans++;       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j;     int n;     cin >> n;     long long int a[n + 1];     map<long long, long long> m;     for (i = 1; i <= n; i++) {       cin >> a[i];       m[a[i]]++;     }     long long ans = 0;     multiset<long long> s;     for (auto it = m.begin(); it != m.end(); it++) {       s.insert(it->second);       ans = max(ans, it->second);     }     vector<long long int> v1;     long long ans2 = 0, ans1 = 0;     for (i = 1; i <= ans; i++) {       v1.clear();       long long p = i;       ans1 = 0;       while (1) {         auto it = s.lower_bound(p);         if (it == s.end()) break;         v1.push_back(*it);         s.erase(s.find(*it));         ans1 += p;         p = p * 2;       }       ans2 = max(ans2, ans1);       for (j = v1.size() - 1; j >= 0; j--) {         s.insert(v1[j]);       }     }     cout << ans2 << endl;   }


#include <bits/stdc++.h>   #pragma GCC optimization( unroll-loops )   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4 )   using namespace std;   const int BASE = 1000000;   char what[1 << 20];   pair<int, int> nxt[10][1 << 20];   inline long long Calc(long long x) {     long long suff = x / BASE, pref = x % BASE;     long long ans = suff;     while (suff > 0) {       ans += nxt[what[suff]][pref].first;       pref = nxt[what[suff]][pref].second;       pref += BASE - max(what[suff], what[pref]);       --suff;     }     return ans + nxt[0][pref].first;   }   int main() {     for (int i = 1; i <= BASE; ++i) {       int val = 0;       for (int x = i; x > 0; x /= 10) {         val = max(val, x % 10);       }       what[i] = val;       for (int d = 0; d < 10; ++d) {         if (i >= max(d, val)) {           nxt[d][i] = make_pair(nxt[d][i - max(d, val)].first + 1,                                 nxt[d][i - max(d, val)].second);         } else {           nxt[d][i] = make_pair(0, i);         }       }     }     long long n;     cin >> n;     cout << Calc(n) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200, mo = 1000000000 + 7;   int n, b, k, x, s[10];   long long d[maxn];   struct node {     long long s[maxn][maxn];     void init() { memset(s, 0, sizeof(s)); }     void give(node zjz) {       for (int i = 0; i < x; i++)         for (int j = 0; j < x; j++) s[i][j] = zjz.s[i][j];     }     void work(node zjz) {       node zkj;       zkj.init();       for (int i = 0; i < x; i++)         for (int j = 0; j < x; j++)           for (int t = 0; t < x; t++) {             zkj.s[i][j] += s[i][t] * zjz.s[t][j];             zkj.s[i][j] %= mo;           }       this->give(zkj);     }   };   node a, aa;   void ksm(int m) {     if (m == 0) {       for (int i = 0; i < x; i++)         for (int j = 0; j < x; j++)           if (i == j)             aa.s[i][j] = 1;           else             aa.s[i][j] = 0;       return;     }     if (m == 1) {       aa.give(a);       return;     }     ksm(m / 2);     aa.work(aa);     if (m % 2) aa.work(a);   }   int main() {     cin >> n >> b >> k >> x;     for (int i = 1; i <= n; i++) {       int t;       scanf( %d , &t);       s[t]++;     }     a.init();     for (int j = 0; j < x; j++)       for (int jj = 0; jj < 10; jj++) {         int j0 = (j * 10 + jj) % x;         a.s[j0][j] += s[jj];         a.s[j0][j] %= mo;       }     ksm(b - 1);     for (int i = 0; i < 10; i++) d[i % x] += s[i];     long long ans = 0;     for (int i = 0; i < x; i++) {       ans += aa.s[k][i] * d[i];       ans %= mo;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n, t, z = 0;     cin >> n >> t;     if (n == 1 && t == 10)       cout << -1 << endl;     else if (t == 10) {       for (int i = 0; i < n - 1; i++) {         cout << 1;       }       cout << 0 << endl;     } else {       for (int i = 0; i < n; i++) {         cout << t;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e6 + 10;   const int mod = 1e9 + 7;   const int AL = 9;   const int N = 5000;   const int INF = 1e9;   struct Trie {     int net[10000][12], fail[10000];     bool end[10000];     int root, L;     int newnode() {       for (int i = 0; i < AL; ++i) net[L][i] = -1;       end[L++] = false;       return L - 1;     }     void init() {       L = 0;       root = newnode();     }     void insert(char buf[]) {       int len = strlen(buf);       int now = root;       for (int i = 0; i < len; ++i) {         if (net[now][buf[i] -  1 ] == -1) net[now][buf[i] -  1 ] = newnode();         now = net[now][buf[i] -  1 ];       }       end[now] = true;     }     void build() {       queue<int> Q;       fail[root] = root;       for (int i = 0; i < AL; ++i)         if (net[root][i] == -1)           net[root][i] = root;         else {           fail[net[root][i]] = root;           Q.push(net[root][i]);         }       while (!Q.empty()) {         int now = Q.front();         Q.pop();         if (end[fail[now]]) end[now] = true;         for (int i = 0; i < AL; ++i) {           if (net[now][i] == -1)             net[now][i] = net[fail[now]][i];           else {             fail[net[now][i]] = net[fail[now]][i];             Q.push(net[now][i]);           }         }       }     }   } ac;   char ch[1010];   char cp[30];   int x;   int f[30];   void dfs(int num, int le) {     if (x < num) return;     if (x == num) {       f[0] = 0;       for (int i = (1); i < (le + 1); ++i) f[i] = f[i - 1] + cp[i - 1] -  0 ;       for (int i = (1); i < (le + 1); ++i)         for (int j = (i); j < (le + 1 - (i == 1)); ++j) {           if (f[le] % (f[j] - f[i - 1]) == 0) {             return;           }         }       ac.insert(cp);       return;     }     for (int i = (1); i < (10); ++i) {       cp[le] =  0  + i;       dfs(num + i, le + 1);       cp[le] =   0 ;     }   }   int main() {     scanf( %s , ch);     scanf( %d , &x);     ac.init();     dfs(0, 0);     ac.build();     int len = strlen(ch);     vector<vector<int> > dp(len + 1, vector<int>(ac.L, INF));     dp[0][0] = 0;     for (int i = (0); i < (len); ++i)       for (int j = (0); j < (ac.L); ++j)         if (dp[i][j] != INF) {           dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);           int nxt = ac.net[j][ch[i] -  1 ];           if (!ac.end[nxt]) dp[i + 1][nxt] = min(dp[i + 1][nxt], dp[i][j]);         }     printf( %d n , *min_element((dp[len]).begin(), (dp[len]).end()));     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(3)   using namespace std;   inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); }   inline int qpow(int a, int n) {     int ans = 1;     while (n) {       if (n & 1) {         ans *= a;       }       a *= a;       n >>= 1;     }     return ans;   }   inline long long _qpow(long long a, long long b, long long m) {     a %= m;     long long res = 1;     while (b > 0) {       if (b & 1) res = res * a % m;       a = a * a % m;       b >>= 1;     }     return res;   }   inline long long ksm(long long a, long long b, long long m) {     a %= m;     long long res = 1;     while (b > 0) {       if (b & 1) res = res * a % m;       a = a * a % m;       b >>= 1;     }     return res;   }   const int maxnum = 1e6 + 5;   const int MAXN = (1 << 13) - 1;   set<int> st[maxnum];   int n;   int x;   int f[MAXN + 5];   void slove() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> x;       st[x].insert(i);     }     for (int i = 0; i <= MAXN; i++) f[i] = 0x3f3f3f3f;     f[0] = 0;     for (int i = 0; i <= MAXN; i++) {       for (int j = MAXN; j >= 0; j--) {         auto it = st[i].upper_bound(f[j]);         if (it != st[i].end()) {           f[i ^ j] = min(f[i ^ j], *it);         }       }     }     vector<int> ans;     for (int i = 0; i <= MAXN; i++) {       if (f[i] != 0x3f3f3f3f) ans.push_back(i);     }     cout << ans.size() <<   n ;     for (int x : ans) cout << x <<    ;     cout <<   n ;   }   signed main() {     std::ios::sync_with_stdio(false);     std::cin.tie(0);     ;     slove();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T abs(T a) {     return a < 0 ? -a : a;   }   template <typename T>   T sqr(T a) {     return a * a;   }   const int INF = (int)1e9;   const long double EPS = 1e-9;   const long double PI = 3.1415926535897932384626433832795;   long double a, b, h;   inline pair<long double, long double> rotate(       const pair<long double, long double>& a, const long double& ang) {     return pair<long double, long double>(         a.first * cos(ang) + a.second * sin(ang),         -a.first * sin(ang) + a.second * cos(ang));   }   inline long double dist(const long double& w, const long double& h,                           const long double& ang) {     pair<long double, long double> pa(w * cos(ang), 0);     pair<long double, long double> push_back(0, w * sin(ang));     pair<long double, long double> v =         rotate(pair<long double, long double>(h, 0), ang - PI / 2);     pa.first += v.first;     pa.second += v.second;     push_back.first += v.first;     push_back.second += v.second;     long double A, B, C;     A = push_back.second - pa.second;     B = pa.first - push_back.first;     C = -A * pa.first - B * pa.second;     return (A * b + B * a + C) / sqrtl(A * A + B * B);   }   inline long double findMaxAng(const long double& w, const long double& h) {     long double lf = 0;     long double rg = PI / 2;     for (int i = 0; i < int(100); ++i) {       long double mid = lf + (rg - lf) / 2;       if (w * cos(mid) + h * sin(mid) > b)         lf = mid;       else         rg = mid;     }     return lf;   }   inline bool check(const long double& w, const long double& h) {     if (a + EPS < h) return false;     if (w <= b + EPS) return true;     long double lf = 0;     long double rg = findMaxAng(w, h);     for (int i = 0; i < int(200); ++i) {       long double d = (rg - lf) / 3;       long double ml = lf + d;       long double mr = rg - d;       if (dist(w, h, ml) > dist(w, h, mr))         lf = ml;       else         rg = mr;     }     return dist(w, h, lf) > -EPS;   }   int main() {     cin >> a >> b >> h;     long double lf = 0;     long double rg = h;     for (int i = 0; i < int(100); ++i) {       long double mid = lf + ((rg - lf) / 2);       if (check(h, mid) || check(mid, h))         lf = mid;       else         rg = mid;     }     if (lf < EPS) {       puts( My poor head =( );     } else {       cout.precision(10);       cout << fixed << lf << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   unordered_set<string> was;   int n, i, j, pos, k = 13, t;   string codes[1001];   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (; i < n; ++i) cin >> codes[i];     for (i = 0; i < n; ++i)       for (j = i + 1; j < n; ++j) {         t = 0;         for (pos = 0; pos < 6; ++pos) t += codes[i][pos] != codes[j][pos];         k = min(k, t);       }     cout << (k - 1) / 2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int maxi = -1000000050;     int f, t;     int loli = -1000000050;     for (int i = 0; i < n; i++) {       cin >> f >> t;       if (t > k) {         loli = f - (t - k);       } else         loli = f;       if (loli > maxi) maxi = loli;     }     cout << maxi << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, q, s, cntlog2[2050] = {0};   int main() {     scanf( %d , &q);     while (q--) {       memset(cntlog2, 0, sizeof(cntlog2));       scanf( %d , &n);       for (int i = 0; i < n; i++) {         scanf( %d , &s);         if (s <= 2048) cntlog2[s]++;       }       for (int i = 1; i < 2048; i++) {         while (cntlog2[i] >= 2) {           cntlog2[i] -= 2;           cntlog2[2 * i]++;         }       }       if (cntlog2[2048]) {         printf( YES n );       } else {         printf( NO n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct FenwickTree {     vector<long long int> bit;     int n;     FenwickTree(int n) {       this->n = n;       bit.assign(n, 0);     }     FenwickTree(vector<long long int> a) : FenwickTree(a.size()) {       for (size_t i = 0; i < a.size(); i++) add(i, a[i]);     }     long long int sum(int r) {       long long int ret = 0;       for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];       return ret;     }     long long int sum(int l, int r) { return sum(r) - sum(l - 1); }     void add(int idx, long long int delta) {       for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta;     }   };   const int MAX_VAL = 3e5;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     vector<long long int> a(n);     vector<long long int> ra(MAX_VAL + 5, -1);     for (int i = 0; i < n; ++i) {       cin >> a[i];       ra[a[i]] = i;     }     vector<long long int> tr(n);     vector<long long int> ctr(MAX_VAL + 5);     int cidx = 0;     for (int i = 1; i <= MAX_VAL; ++i) {       if (ra[i] != -1) {         tr[ra[i]] = cidx;         cidx++;       }       ctr[i] = cidx - 1;     }     long long int csum = 0;     long long int tsum = 0;     FenwickTree fw(n);     FenwickTree fw2(n);     for (int i = 0; i < n; ++i) {       csum += tsum + a[i] * fw2.sum(tr[i]);       for (long long int k = 1; k * k <= a[i]; ++k) {         csum -= fw.sum(ctr[a[i] / k]);         if (a[i] / k > k) {           csum -= (a[i] / k - a[i] / (k + 1)) * fw.sum(ctr[k]);         }       }       for (long long int k = 2; k * a[i] <= MAX_VAL; ++k) {         csum -= a[i] * (i - fw2.sum(ctr[k * a[i] - 1]));       }       cout << csum <<    ;       tsum += a[i];       fw.add(tr[i], a[i]);       fw2.add(tr[i], 1);     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     long long int n;     cin >> n;     long long int ar[n + 1];     for (long long int i = 0; i < n; i++) cin >> ar[i];     sort(ar, ar + n, greater<long long int>());     vector<long long int> v, ans;     for (long long int i = 0; i < n; i++) {       v.push_back(ar[i]);     }     ans.push_back(v[0]);     for (long long int i = 1; i < v.size(); i++) {       long long int p = v[i];       while (p >= 0) {         if (find(ans.begin(), ans.end(), p) != ans.end()) {           p--;         } else {           ans.push_back(p);           break;         }       }     }     long long int cnt = 0;     for (long long int i = 0; i < ans.size(); i++) cnt += ans[i];     cout << cnt << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, ans[100], len, a, cur;   int main() {     cur = 1;     cin >> n;     for (int i = 0; i < (n); ++i) {       cin >> a;       if (a == cur) {         ans[len++] = 2001 + i;         cur++;       }     }     cout << len << endl;     for (int i = 0; i < (len); ++i) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> v(n);     for (int i = 0; i < n; i++) {       cin >> v[i];     }     cout << (*max_element(v.begin(), v.end()) ^ v.back()) << endl;     return 0;   }


#include <bits/stdc++.h>   const int mod = 1000000007;   const int inf = 1000000009;   const long long INF = 1000000000000000009;   const long long big = 1000000000000000;   const long double eps = 0.000000000000000000001;   using namespace std;   vector<pair<int, int> > W;   int T[200005];   int s, l, last = -1;   bool czy = 0;   vector<int> R;   void make_move(int r) {     R.push_back(s);     if (r == 0) {       last = s;       auto x = upper_bound(W.begin(), W.end(), make_pair(W[s].first + l, inf)) -                W.begin() - 1;       l -= W[x].first - W[s].first;       s = x;     } else {       auto x = lower_bound(W.begin(), W.end(), make_pair(W[s].first - l, -inf)) -                W.begin();       long long d = W[s].first - W[x].first;       l -= d;       if (s == x) {         czy = 1;         return;       }       s = x;       if (s == last) l = l % (d * 2);     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie();     cout.tie();     int n, q;     cin >> n >> q;     for (int i = 1; i <= n; i++) {       int a;       cin >> a;       W.push_back(make_pair(a, i));     }     sort(W.begin(), W.end());     for (int i = 0; i < W.size(); i++) T[W[i].second] = i;     for (int i = 1; i <= q; i++) {       cin >> s >> l;       s = T[s];       while (!czy) {         make_move(0);         make_move(1);       }       czy = 0;       last = -1;       cout << W[s].second <<   n ;       R.clear();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<pair<string, int>, vector<pair<string, int> > > edg;   queue<string> que;   map<string, int> num, por;   set<string> sett;   int main() {     int n, i, k, l, r, a, b, j;     cin >> n;     string s, t, S;     for (i = 0; i < n; i++) {       cin >> s >> a;       if (!i) {         que.push(s);         num[s] = a;         S = s;         por[s] = 1;       }       cin >> k;       for (j = 0; j < k; j++) {         cin >> t >> b;         edg[{s, a}].push_back({t, b});       }     }     pair<string, int> st;     while (!que.empty()) {       sett.insert(que.front());       st.first = que.front();       st.second = num[st.first];       que.pop();       for (i = 0; i < edg[st].size(); i++) {         if (!num.count(edg[st][i].first)) {           num[edg[st][i].first] = edg[st][i].second;           por[edg[st][i].first] = por[st.first] + 1;           que.push(edg[st][i].first);         } else {           if (por[st.first] + 1 == por[edg[st][i].first] &&               edg[st][i].second > num[edg[st][i].first]) {             num[edg[st][i].first] = edg[st][i].second;           }         }       }     }     sett.erase(S);     cout << sett.size() <<   n ;     for (auto j : sett) {       cout << j <<     << num[j] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int q = 1; q <= t; q++) {       int n;       int k;       cin >> n >> k;       long long a[n * k + 10];       long long ans = 0;       for (int i = 1; i <= n * k; i++) cin >> a[i];       int ind = n * k - n / 2;       int cnt = n / 2 + 1;       int col = 0;       while (ind > col) {         ans += a[ind];         ind -= cnt;         if (ind > 0) col += n - cnt;       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long MOD = 1e9 + 7;   long long powmod(long long a, long long l, long long md) {     long long res = 1;     while (l) {       if (l & 1) res = res * a % md;       l /= 2;       a = a * a % md;     }     return res;   }   long long binpow(long long a, long long l) {     long long res = 1;     while (l) {       if (l & 1) res = res * a;       l /= 2;       a = a * a;     }     return res;   }   long long __set(long long b, long long i) { return b | (1 << i); }   long long __unset(long long b, long long i) { return b & (~(1UL << i)); }   long long __check(long long b, long long i) { return b & (1 << i); }   long long mulmod(long long a, long long b, long long md) {     return ((a % md) * (b % md)) % md;   }   long long addmod(long long a, long long b, long long md) {     return (a + b) % md;   }   long long submod(long long a, long long b, long long md) {     return (((a - b) % md) + md) % md;   }   long long divmod(long long a, long long b, long long md) {     return mulmod(a, powmod(b, md - 2, md), md);   }   const long long inf = 0xFFFFFFFFFFFFFFFL;   priority_queue<long long, vector<long long>, greater<long long> > pq;   clock_t time_p = clock();   void time() {     time_p = clock() - time_p;     cerr <<  Time Taken :   << (float)(time_p) / CLOCKS_PER_SEC <<   n ;   }   class UnionFind {    private:     vector<long long> p, rank, setCount;     long long NumSets;       public:     UnionFind(long long N) {       NumSets = N;       rank.assign(N, 0);       setCount.assign(N, 1);       p.assign(N, 0);       for (long long i = 0; i < N; i++) p[i] = i;     }     long long findSet(long long i) {       return (p[i] == i) ? i : (p[i] = findSet(p[i]));     }     bool isSameSet(long long i, long long j) { return findSet(i) == findSet(j); }     void unionSet(long long i, long long j) {       if (!isSameSet(i, j)) {         long long x = findSet(i), y = findSet(j);         NumSets--;         if (rank[x] > rank[y]) {           p[y] = x;           setCount[x] += setCount[y];         } else {           p[x] = y;           setCount[y] += setCount[x];           if (rank[x] == rank[y]) rank[y]++;         }       }     }     long long SizeOfSet(long long i) { return setCount[findSet(i)]; }     long long NumDisjointSets() { return NumSets; }   };   vector<vector<long long> > g(300010);   vector<long long> vis(300010, 0);   void dfs(long long u, long long dp, vector<pair<long long, long long> >& dis) {     dis.push_back({dp, u});     vis[u] = 1;     for (long long i = 0; i < (long long)(g[u].size()); i++) {       long long v = g[u][i];       if (!vis[v]) {         dfs(v, dp + 1, dis);       }     }   }   void dfs1(long long u, long long dp, vector<pair<long long, long long> >& dis) {     dis.push_back({dp, u});     vis[u] = 1;     for (long long i = 0; i < (long long)(g[u].size()); i++) {       long long v = g[u][i];       if (!vis[v]) {         dfs(v, dp + 1, dis);       }     }   }   signed main(void) {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n, m;     cin >> n >> m;     long long q;     cin >> q;     vector<long long> d(n, 0);     UnionFind uf(n);     for (long long i = 0; i < m; i++) {       long long u, v;       cin >> u >> v;       u--;       v--;       g[u].push_back(v);       g[v].push_back(u);       uf.unionSet(u, v);     }     for (long long i = 0; i < n; i++) {       if (!vis[i]) {         vector<pair<long long, long long> > dis;         dfs(i, 0, dis);         sort((dis).begin(), (dis).end());         long long u = dis[(long long)(dis.size()) - 1].second;         for (long long j = 0; j < (long long)(dis.size()); j++)           vis[dis[j].second] = 0;         dis.clear();         dfs1(u, 0, dis);         sort((dis).begin(), (dis).end());         d[uf.findSet(i)] = dis[(long long)(dis.size()) - 1].first;       }     }     for (long long i = 0; i < q; i++) {       long long ty;       cin >> ty;       if (ty == 1) {         long long x;         cin >> x;         x--;         cout << d[uf.findSet(x)] << endl;         continue;       }       long long u, v;       cin >> u >> v;       u--;       v--;       if (uf.isSameSet(u, v)) continue;       long long dd1 = d[uf.findSet(u)], dd2 = d[uf.findSet(v)];       long long dd = max({dd1, dd2, dd1 / 2 + dd2 / 2 + dd1 % 2 + dd2 % 2 + 1});       uf.unionSet(u, v);       d[uf.findSet(u)] = dd;     }     time();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int main() {     scanf( %d%d , &n, &m);     if (n > m) swap(n, m);     if (n == 1)       printf( %d n , m / 6 * 6 + 2 * max(m % 6 - 3, 0));     else if (n == 2) {       if (m == 2)         printf( 0 n );       else if (m == 3)         printf( 4 n );       else if (m == 7)         printf( 12 n );       else         printf( %lld n , 1LL * n * m);     } else       printf( %lld n , 1LL * n * m / 2 * 2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 7;   long long sumb, sumc;   int n, a[N], b[N], c[N], cnt[30];   bool check() {     for (int i = 0; i < n; i++) {       for (int j = 0; j < 30; j++) {         if (a[i] & 1 << j) cnt[j]++;       }     }     for (int i = 0; i < n; i++) {       long long sum = 0;       for (int j = 0; j < 30; j++) {         if (a[i] & 1 << j) sum += cnt[j] * (1 << j);       }       if (sum != b[i]) return 0;     }     return 1;   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) scanf( %d , b + i), sumb += b[i];     for (int i = 0; i < n; i++) scanf( %d , c + i), sumc += c[i];     for (int i = 0; i < n; i++) {       long long buf = b[i] + c[i] - (sumb + sumc) / 2 / n;       if ((sumb + sumc) % (2 * n) || buf % n) {         puts( -1 );         return 0;       }       a[i] = buf / n;     }     if (!check())       puts( -1 );     else {       for (int i = 0; i < n; i++) {         printf( %d%c , a[i],    n [i == n - 1]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int fx[] = {1, -1, 0, 0};   int fy[] = {0, 0, 1, -1};   const unsigned long long hs = 3797;   unsigned long long F[100005], H[100005], H2[15];   int H2_size[15];   unsigned long long seg(int x, int y) { return H[y] - F[y - x + 1] * H[x - 1]; }   int main() {     F[0] = 1;     for (int i = 1; i <= 100005; i++) F[i] = F[i - 1] * hs;     string str, p;     str =  . ;     cin >> p;     str = str + p;     H[0] = 0;     for (int i = 1; i <= str.size() - 1; i++) {       H[i] = H[i - 1] * hs + str[i];     }     int N;     cin >> N;     for (int k = 1; k <= N; k++) {       string st, pp;       st =  . ;       cin >> pp;       st = st + pp;       H2_size[k] = st.size() - 1;       H2[k] = 0;       for (int i = 1; i <= st.size() - 1; i++) {         H2[k] = H2[k] * hs + st[i];       }     }     int start = 0, cnt = 0, ans = 0, ans_pos = 0;     for (int i = 1; i <= str.size() - 1; i++) {       bool flag = false;       int new_pos = 0;       for (int k = 1; k <= N; k++) {         if (i - H2_size[k] >= start) {           if (seg(i - H2_size[k] + 1, i) == H2[k]) {             flag = true;             new_pos = max(new_pos, i - H2_size[k] + 1);           }         }       }       if (flag) {         if (cnt > ans) {           ans = cnt;           ans_pos = start;         }         cnt = cnt - (new_pos - start) + 1;         start = new_pos;         continue;       }       cnt++;     }     if (cnt > ans) {       ans = cnt;       ans_pos = start;     }     cout << ans <<     << ans_pos;   }


#include <bits/stdc++.h>   const int MAXN = 200 + 5;   char str[MAXN][MAXN];   int n, m;   namespace Flow {   const int MAXM = 2e5 + 5;   struct Edge {     int to, w, nxt;   } e[MAXM << 3];   int head[MAXM], cur[MAXM], cnt = 1;   int dep[MAXM];   int S, T, N;   inline void add(int u, int v, int w) {     e[++cnt] = (Edge){v, w, head[u]};     head[u] = cnt;     e[++cnt] = (Edge){u, 0, head[v]};     head[v] = cnt;   }   inline bool bfs() {     for (int i = 0; i <= N; ++i) cur[i] = head[i], dep[i] = 0;     std::queue<int> q;     q.push(S);     dep[S] = 1;     while (!q.empty()) {       int v = q.front();       q.pop();       for (int i = head[v]; i; i = e[i].nxt) {         if (e[i].w > 0 && !dep[e[i].to]) {           dep[e[i].to] = dep[v] + 1;           if (e[i].to == T) return true;           q.push(e[i].to);         }       }     }     return dep[T];   }   inline int dfs(int v, int flow = 1e9) {     if (v == T) return flow;     if (!flow) return 0;     int ans = 0;     for (int &i = cur[v]; i; i = e[i].nxt) {       if (e[i].w > 0 && dep[e[i].to] == dep[v] + 1) {         int t = dfs(e[i].to, std::min(flow, e[i].w));         if (t > 0) {           ans += t;           flow -= t;           e[i].w -= t;           e[i ^ 1].w += t;           if (!flow) break;         }       }     }     return ans;   }   inline int Dinic() {     int ans = 0, t = 0;     while (bfs())       while ((t = dfs(S))) ans += t;     return ans;   }   };  // namespace Flow   const int dx[] = {0, 0, 1, -1};   const int dy[] = {1, -1, 0, 0};   inline int id(int i, int j) { return (i - 1) * m + j; }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; ++i) scanf( %s , str[i] + 1);     Flow::T = Flow::N = n * m + 2;     Flow::S = n * m + 1;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         if (str[i][j] ==  . ) continue;         int c1 = 0, c2 = 0;         for (int k = 0; k <= 3; ++k) {           int ii = i + dx[k], jj = j + dy[k];           if (ii >= 1 && ii <= n && jj >= 1 && jj <= m && str[ii][jj] ==  # ) {             Flow::add(id(i, j), id(ii, jj), 1);           } else {             if (k <= 1)               c1++;             else               c2++;           }         }         Flow::add(Flow::S, id(i, j), c1);         Flow::add(id(i, j), Flow::T, c2);       }     }     printf( %d n , Flow::Dinic() / 2);     return 0;   }


#include <bits/stdc++.h>   namespace io {   template <typename T>   inline void read(T& a) {     int c = getchar(), sign = 0;     a = 0;     while (c <  0  || c >  9 ) sign += (c ==  - ), c = getchar();     while (c >=  0  && c <=  9 ) a = (a << 1) + (a << 3) + c - 48, c = getchar();     if (sign & 1) a = -a;   }   template <typename T, typename... _T>   inline void read(T& a, _T&... argc) {     read(a);     read(argc...);   }   template <typename T>   inline void Write(T a) {     if (a > 9) Write(a / 10);     putchar(a % 10 +  0 );   }   template <typename T>   inline void write(T a, char ch =    ) {     if (a < 0) putchar( - ), a = -a;     Write(a);     putchar(ch);   }   }  // namespace io   using namespace io;   using namespace std;   const int maxn = 2e3;   const int mod = 1e9 + 7;   int n, d, m, a[maxn + 3], b[maxn + 3];   int dem[maxn + 3], id[maxn + 3];   int k, h;   int res = 2e9;   long long dp[maxn + 5], t[maxn + 3];   string second;   bool kt[maxn + 3];   map<int, int> maps;   vector<int> edge[maxn + 3];   map<int, int>::iterator it;   template <typename T>   void answer(T x) {     cout << x;     exit(0);   }   void putstring(string x, char ch =    ) {     for (char c : x) putchar(c);     putchar(ch);   }   struct T {     long long x, y;     T operator-(const T& other) const { return {x - other.x, y - other.y}; }     long long operator*(const T& other) const {       return x * other.y - y * other.x;     }     int direct_vector(const T& a, const T& b) const {       long long ans = (a - *this) * (b - *this);       return ans > 0;     }   } v[maxn + 3];   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL);     cin >> n;     for (int i = 1; i <= n; ++i) cin >> v[i].x >> v[i].y;     cin >> second;     int chot = min_element(v + 1, v + n + 1,                            [](const T& a, const T& b) {                              return a.x != b.x ? a.x < b.x : a.y < b.y;                            }) -                v;     kt[chot] = true;     write(chot);     for (int i = 0; i < second.size(); i++) {       int cc = -1, p = (second[i] ==  L );       for (int j = 1; j <= n; ++j) {         if (kt[j]) continue;         if (cc == -1) {           cc = j;           continue;         }         if (p == v[chot].direct_vector(v[j], v[cc])) {           cc = j;         }       }       kt[cc] = true;       chot = cc;       write(chot);     }     for (int i = 1; i <= n; ++i)       if (!kt[i]) write(i);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long i = 1, n;     string s[] = { Sheldon ,  Leonard ,  Penny ,  Rajesh ,  Howard };     cin >> n;     if (n <= 5) {       cout << s[n - 1];       return 0;     }     while (((1 << i) - 1) * 5 <= n) i++;     i--;     i = (1 << i);     n -= (i - 1) * 5;     n--;     cout << s[n / i];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k;   struct ff {     int value, order;   } a[100010];   bool cmp(ff a, ff b) { return a.value < b.value; }   long long sum1[100010], sum2[100010];   void add(long long s[], int i, int x) {     while (i <= n) {       s[i] += x;       i += i & -i;     }   }   long long getsum(long long s[], int i) {     long long res = 0;     while (i > 0) {       res += s[i];       i -= i & -i;     }     return res;   }   int b[100010];   int main() {     while (~scanf( %I64d%I64d , &n, &k)) {       for (int i = 1; i <= n; i++) {         scanf( %d , &a[i].value);         a[i].order = i;         b[i - 1] = a[i].value;       }       sort(b, b + n);       int m = unique(b, b + n) - b;       for (int i = 1; i <= n; i++) {         a[i].order = lower_bound(b, b + m, a[i].value) - b + 1;       }       memset(sum1, 0, sizeof sum1);       memset(sum2, 0, sizeof sum2);       long long res = 0;       long long ans = 0;       for (int i = 1; i <= n; i++) {         add(sum1, a[i].order, 1);         res += i - getsum(sum1, a[i].order);       }       if (res <= k) {         printf( %I64d n , n * (long long)(n - 1) / 2);         continue;       }       add(sum1, a[n].order, -1);       add(sum2, a[n].order, 1);       for (int l = n - 1, r = n - 1; r > 0; r--) {         while (l > 0 && res > k) {           res -= l - getsum(sum1, a[l].order);           res -= getsum(sum2, a[l].order - 1);           add(sum1, a[l].order, -1);           l--;         }         if (l == 0) break;         ans += l;         res += getsum(sum2, a[r].order - 1);         res += l - getsum(sum1, a[r].order);         add(sum2, a[r].order, 1);       }       cout << ans << endl;     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;   #define ll long long int   #define v vector<ll>   #define pb push_back   #define fr(i,a,n) for(ll i=a;i<n;i++)   #define Fast ios::sync_with_stdio(false);cin.tie(NULL);   #define in(a) ll a;cin>>a   #define ip(a) string a;cin>>a   #define R(a,n) for(int i=0;i<n;i++)cin>>a[i];   #define all(a) sort(a.begin(),a.end())   #define e endl   #define MOD (int)1e9 + 7   inline ll powe(ll a,ll b , ll p = MOD){ ll ans=1;for(;b;b>>=1){if(b&1) ans=ans*a%p;a=a*a%p;} return ans;}   void solve(){      in(n);      if(n==2)cout<< -1 <<e;      else{           ll pp=1;           ll size=n*n;           fr(i,0,n){             fr(j,0,n){               cout<<pp<<   ;               if(pp+2>size){                pp=2;         }         else{          pp+=2;         }       }       cout<<e;        }      }      }   int main()   {       Fast       ll t;cin>>t;       while(t--){           solve();       }   }


#include <bits/stdc++.h>   using namespace std;   int a[8];   int main() {     int n, m;     cin >> n >> m;     int xx = n;     int kk = m;     int ans = 0;     int len1 = 1, len2 = 1;     for (int b = 7; b < n; b *= 7) len1++;     for (int b = 7; b < m; b *= 7) len2++;     if (len2 + len1 > 7) {       cout << 0;       return 0;     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         for (int z = i, f = 0; f != len1; f++, z /= 7) {           int x = z % 7;           a[x]++;         }         for (int z = j, f = 0; f != len2; f++, z /= 7) {           int x = z % 7;           a[x]++;         }         bool ok = false;         for (int d = 0; d < 8; d++)           if (a[d] > 1) ok = true;         if (ok == false) ans++;         for (int d = 0; d < 8; d++) a[d] = 0;       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int x, y, time;   };   char str[120][120];   int vis[102][102][102], n, m, ck = 0;   void train(int p, int q, int cnt) {     int time = 0, d = 0;     for (int j = q; (j + cnt - 1) >= 0; j -= 2) {       d = 0;       int lim = cnt;       if (j < 0) lim = cnt + j;       for (int k = max(j, 0); d < lim; k++, d++) {         vis[p][k][time] = 1;       }       time++;     }   }   int level[102][102][102];   void bfs(int i, int j) {     int v, w, t, a, b, c;     level[i][j][0] = 1;     queue<node> q;     node gr;     gr.x = i;     gr.y = j;     gr.time = 0;     q.push(gr);     while (!q.empty()) {       gr = q.front();       q.pop();       v = gr.x;       w = gr.y;       if (w == m - 1) {         ck = 1;         return;       }       for (i = 0; i < 1; i++) {         a = v + 0;         b = w + 1;         t = gr.time + 1;         if (a >= 0 && a < n && b >= 0 && b < m && vis[a][b][t - 1] == 1) continue;         if (a >= 0 && a < n && b >= 0 && b < m && vis[a][b][t] == 0 &&             vis[a][b][t - 1] == 0 && level[a][b][t] == 0) {           node pr;           pr.x = a;           pr.y = b;           pr.time = t;           level[a][b][t] = 1;           q.push(pr);         }         c = a - 1;         if (c >= 0 && c < n && b >= 0 && b < m && vis[c][b][t] == 0 &&             vis[c][b][t - 1] == 0 && level[c][b][t] == 0) {           node pr;           pr.x = c;           pr.y = b;           pr.time = t;           level[c][b][t] = 1;           q.push(pr);         }         c = a + 1;         if (c >= 0 && c < n && b >= 0 && b < m && vis[c][b][t] == 0 &&             vis[c][b][t - 1] == 0 && level[c][b][t] == 0) {           node pr;           pr.x = c;           pr.y = b;           pr.time = t;           level[c][b][t] = 1;           q.push(pr);         }       }     }   }   int main() {     int i, j, k, l, test;     scanf( %d , &test);     while (test--) {       cin >> m >> k;       n = 3;       ck = 0;       memset(level, 0, sizeof level);       memset(vis, 0, sizeof vis);       for (i = 0; i < 3; i++) {         scanf( %s , str[i]);       }       for (i = 0; i < 3; i++) {         for (j = 0; j < m; j++) {           if (str[i][j] !=  s  && str[i][j] !=  . ) {             int cnt = 0;             for (k = j; str[i][k]; k++) {               if (str[i][j] != str[i][k]) break;               cnt++;             }             train(i, j, cnt);           }         }       }       for (i = 0; i < n; i++) {         for (j = 0; j < m; j++) {           if (str[i][j] ==  s ) {             bfs(i, j);           }         }       }       if (ck == 0)         printf( NO n );       else         printf( YES n );     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y, n, tx, ty, m;     scanf( %d %d , &n, &m);     double res = 0;     for (int i = 0; i < n; i++) {       scanf( %d %d , &x, &y);       if (i == 0) {         tx = x, ty = y;         continue;       }       res = res + sqrt(((x - tx) * (x - tx)) + ((y - ty) * (y - ty)));       tx = x, ty = y;     }     if (n == 1) {       res = res + abs(x - y);       printf( %.9f n , res);     } else {       printf( %.9f n , (res / 50.0) * m);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100][100];   int main() {     string x;     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x;       for (int j = 0; j < n; j++) {         if (x[j] ==  E ) a[i][j] = 1;       }     }     int k = 0;     int s;     for (int i = 0; i < n; i++) {       s = 1;       for (int j = 0; j < n; j++) s *= a[i][j];       k += (1 - s);     }     if (k == n) {       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++)           if (a[i][j] == 0) {             cout << i + 1 <<     << j + 1 << endl;             break;           }       return 0;     }     k = 0;     for (int i = 0; i < n; i++) {       s = 1;       for (int j = 0; j < n; j++) s *= a[j][i];       k += (1 - s);     }     if (k == n) {       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++)           if (a[j][i] == 0) {             cout << j + 1 <<     << i + 1 << endl;             break;           }       return 0;     }     cout << -1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int cons;   long long int check(long long int a) {     if (a >= cons) a %= cons;     return a;   }   long long int check2(long long int a) {     a %= cons;     if (a < 0) a += cons;     return a;   }   long long int GCD(long long int a, long long int b) {     if (b == 0) return a;     return GCD(b, a % b);   }   long long int exp(long long int a, long long int n) {     if (n == 0) return 1;     if (n == 1) return check(a);     long long int b = exp(a, n / 2);     if (n % 2 == 0) return check(b * b);     return check(b * check(b * a));   }   const int N = 200010;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cons = 1e9 + 7;     int n, a, b;     cin >> n >> a >> b;     int ans = 0;     while (true) {       ans++;       int c1 = a / ans;       int c2 = b / ans;       if (c1 > 0 && c2 > 0 && (c1 + c2 >= n))         continue;       else         break;     }     cout << ans - 1;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int n, a[N], place[N], BIT[N];   long long inverse, res[N];   set<int> firstHalf, secondHalf;   long long sumFirst = 0, sumSecond = 0;   long long fenwickGet(int x) {     long long res = 0;     for (; x <= n; x += x & -x) res += BIT[x];     return res;   }   void stable(set<int>& f, set<int>& s) {     while (f.size() > s.size()) {       int leftLarge = *f.rbegin();       f.erase(leftLarge);       s.insert(leftLarge);       sumFirst -= leftLarge;       sumSecond += leftLarge;     }     while (f.size() < s.size()) {       int rightSmall = *s.begin();       s.erase(rightSmall);       f.insert(rightSmall);       sumFirst += rightSmall;       sumSecond -= rightSmall;     }   }   void fenwickUpdate(int x) {     for (; x; x -= x & -x) BIT[x] += 1;   }   long long sol(int cur) {     inverse += fenwickGet(cur);     fenwickUpdate(cur);     if (firstHalf.size() > 0) {       if (cur > *firstHalf.rbegin()) {         secondHalf.insert(cur);         sumSecond += cur;       } else {         firstHalf.insert(cur);         sumFirst += cur;       }     } else {       firstHalf.insert(cur);       sumFirst += cur;     }     stable(firstHalf, secondHalf);     long long middle = *firstHalf.rbegin();     long long n = firstHalf.size(), m = secondHalf.size();     return inverse + middle * n - sumFirst - n * (n - 1) / 2 + sumSecond -            middle * m - m * (m + 1) / 2;   }   void solve() {     inverse = 0;     for (int i = 1; i <= n; ++i) {       char c = (i == n ?   n  :    );       cout << sol(place[i]) << c;     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       place[a[i]] = i;     }     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string to_string(bool b) { return (b ?  true  :  false ); }   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) {     cout <<     << to_string(H);     debug_out(T...);   }   mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());   template <typename T>   using V = vector<T>;   template <typename T>   bool ckmin(T& a, const T& b) {     return b < a ? a = b, 1 : 0;   }   template <typename T>   bool ckmax(T& a, const T& b) {     return a < b ? a = b, 1 : 0;   }   template <class T>   using pqg = priority_queue<T, vector<T>, greater<T>>;   const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};   using ll = long long;   using db = long double;   using vd = vector<db>;   using vs = vector<string>;   using pi = pair<int, int>;   using vi = vector<int>;   using vb = vector<bool>;   using vl = vector<ll>;   using vpi = vector<pi>;   const int mod = 1000000007;   const ll INF = 1e18;   const db PI = acos((db)-1);   constexpr int pct(int x) { return __builtin_popcount(x); }   constexpr int bits(int x) { return x == 0 ? 0 : 31 - __builtin_clz(x); }   constexpr int p2(int x) { return 1 << x; }   constexpr int msk2(int x) { return p2(x) - 1; }   const int mx = 1e5 + 10;   int n, m;   int arr[mx][10], nex[mx][10];   void solve() {     cin >> n >> m;     for (int j = (0); j < (m); ++j)       for (int i = (0); i < (n); ++i) cin >> arr[i][j];     for (int j = (0); j < (m); ++j) {       for (int i = (0); i < (n - 1); ++i) nex[arr[i][j]][j] = arr[i + 1][j];     }     for (int j = (1); j < (m); ++j) {       for (int i = (1); i < (n + 1); ++i) {         if (nex[i][0] && (nex[i][0] != nex[i][j])) nex[i][0] = 0;       }     }     ll ans = 0;     int prev = -1;     for (int i = (0); i < (n); ++i) {       if (!nex[arr[i][0]][0]) {         int len = i - prev;         ans += 1LL * len * (len + 1) / 2;         prev = i;       }     }     cout << ans <<   n ;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int tt = 1;     for (int ii = 1; ii <= tt; ii++) solve();   }


#include <bits/stdc++.h>   using namespace std;   long long int power(long long int x, long long int y, long long int p) {     long long int r = 1;     x = x % p;     while (y) {       if (y & 1) r = r * x % p;       y = y >> 1;       x = x * x % p;     }     return r;   }   const long long int N = 5005;   long long int n, k;   struct edge {     long long int to, flow, cap, cost, rev;   };   struct MinCostMaxFlow {     long long int nodes;     vector<long long int> prio, curflow, prevedge, prevnode, q, pot;     vector<bool> inqueue;     vector<vector<edge> > graph;     MinCostMaxFlow() {}     MinCostMaxFlow(long long int n)         : nodes(n),           prio(n, 0),           curflow(n, 0),           prevedge(n, 0),           prevnode(n, 0),           q(n, 0),           pot(n, 0),           inqueue(n, 0),           graph(n) {}     void addEdge(long long int source, long long int to, long long int capacity,                  long long int cost) {       edge a = {to, 0, capacity, cost, (long long int)graph[to].size()};       edge b = {source, 0, 0, -cost, (long long int)graph[source].size()};       graph[source].push_back(a);       graph[to].push_back(b);     }     void bellman_ford(long long int source, vector<long long int> &dist) {       fill(dist.begin(), dist.end(), INT_MAX);       dist[source] = 0;       long long int qt = 0;       q[qt++] = source;       for (long long int qh = 0; (qh - qt) % nodes != 0; qh++) {         long long int u = q[qh % nodes];         inqueue[u] = false;         for (auto &e : graph[u]) {           if (e.flow >= e.cap) continue;           long long int v = e.to;           long long int newDist = dist[u] + e.cost;           if (dist[v] > newDist) {             dist[v] = newDist;             if (!inqueue[v]) {               inqueue[v] = true;               q[qt++ % nodes] = v;             }           }         }       }     }     pair<long long int, long long int> minCostFlow(long long int source,                                                    long long int dest,                                                    long long int maxflow) {       bellman_ford(source, pot);       long long int flow = 0;       long long int flow_cost = 0;       while (flow < maxflow) {         priority_queue<pair<long long int, long long int>,                        vector<pair<long long int, long long int> >,                        greater<pair<long long int, long long int> > >             q;         q.push({0, source});         fill(prio.begin(), prio.end(), INT_MAX);         prio[source] = 0;         curflow[source] = INT_MAX;         while (!q.empty()) {           long long int d = q.top().first;           long long int u = q.top().second;           q.pop();           if (d != prio[u]) continue;           for (long long int i = 0; i < graph[u].size(); i++) {             edge &e = graph[u][i];             long long int v = e.to;             if (e.flow >= e.cap) continue;             long long int newPrio = prio[u] + e.cost + pot[u] - pot[v];             if (prio[v] > newPrio) {               prio[v] = newPrio;               q.push({newPrio, v});               prevnode[v] = u;               prevedge[v] = i;               curflow[v] = min(curflow[u], e.cap - e.flow);             }           }         }         if (prio[dest] == INT_MAX) break;         for (long long int i = 0; i < nodes; i++) pot[i] += prio[i];         long long int df = min(curflow[dest], maxflow - flow);         flow += df;         for (long long int v = dest; v != source; v = prevnode[v]) {           edge &e = graph[prevnode[v]][prevedge[v]];           e.flow += df;           graph[v][e.rev].flow -= df;           flow_cost += df * e.cost;         }       }       return {flow, flow_cost};     }   };   long long int a[100][100];   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     for (long long int i = 0; i < n; i++)       for (long long int j = 0; j < n; j++) cin >> a[i + 1][j + 1];     long long int lo = 1, hi = 1e9, ans = 0, mi;     while (lo <= hi) {       MinCostMaxFlow mcmf(n + 1);       mi = (lo + hi) / 2;       for (long long int i = 1; i <= n; i++)         for (long long int j = 1; j <= n; j++)           if (a[i][j] > 0)             mcmf.addEdge(i, j, a[i][j], 0), mcmf.addEdge(i, j, 1e5, 1);       pair<long long int, long long int> xx = mcmf.minCostFlow(1, n, mi);       if (xx.second <= k)         ans = max(ans, xx.first), lo = mi + 1;       else         hi = mi - 1;     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b, p;     string s, x, y;     cin >> n;     cin.ignore();     while (n--) {       getline(cin, s);       a = 0;       b = 0;       p = s.length();       if (s[0] ==  m  && s[1] ==  i  && s[2] ==  a  && s[3] ==  o  &&           s[4] ==  . ) {         a = 1;       }       if (s[p - 1] ==  .  && s[p - 2] ==  a  && s[p - 3] ==  l  &&           s[p - 4] ==  a  && s[p - 5] ==  l ) {         b = 1;       }       if (a == 1 && b == 1) {         cout <<  OMG>.< I don t know!  << endl;         continue;       }       if (a == 1) {         cout <<  Rainbow s  << endl;         continue;       }       if (b == 1) {         cout <<  Freda s  << endl;         continue;       }       cout <<  OMG>.< I don t know!  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char **argv) {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n, ok = 1, i = 0, cnt;       cin >> n;       cnt = n;       vector<int> v(n), ans(n, -1), temp;       vector<pair<int, int> > mat;       for (int i = 0; i < n; i++) cin >> v[i], v[i]--;       temp.emplace_back(n);       while (i < n && ok) {         if (temp.back() == i) temp.pop_back();         if (v[i] == -2)           v[i] = temp.back();         else if (v[i] > temp.back())           ok = 0;         else if (v[i] < temp.back())           temp.emplace_back(v[i]);         mat.emplace_back(v[i], i), i++;       }       if (!ok) {         cout << -1 <<   n ;         continue;       }       sort(mat.begin(), mat.end(), [](pair<int, int> l, pair<int, int> r) {         if (l.first == r.first) return l.second < r.second;         return l.first > r.first;       });       for (auto x : mat) ans[x.second] = cnt--;       for (auto x : ans) cout << x <<    ;       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int INIT_SIZE_MAX = (1 << 29) + 10;   const int INIT_SIZE_MIN = -(1 << 29) - 10;   const int INIT_SIZE = 0;   const int MAX = 8;   const int DIR_SIZE = 12;   const double PI = 3.1415926535897932384;   template <class T, class U>   void convert(T &t, U &u) {     stringstream ss;     ss << t;     ss >> u;   }   int dfs(int pos, vector<vector<int> > &tb) {     if (pos % 2) return pos;     int half = pos / 2;     int res = 0;     for (int(i) = 0; (i) < (half); ++(i)) {       bool f = true;       for (int(j) = 0; (j) < (tb[0].size()); ++(j)) {         if (tb[0 + i][j] != tb[(pos - 1) - i][j]) {           f = false;           break;         }       }       if (f) {         res = dfs(pos / 2, tb);       } else {         return pos;       }     }     return res;   }   int main() {     int h, w;     cin >> h >> w;     vector<vector<int> > tb(h, vector<int>(w, 0));     for (int(i) = 0; (i) < (h); ++(i))       for (int(j) = 0; (j) < (w); ++(j)) cin >> tb[i][j];     if (h % 2) {       cout << h << endl;       return 0;     }     int ans = 1 << 29;     ans = min(ans, dfs(h, tb));     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int iinf = 1 << 29;   const long long inf = 1ll << 60;   const long long mod = 1e9 + 7;   void GG() {     cout <<  -1 n ;     exit(0);   }   long long mpow(long long a, long long n, long long mo = mod) {     long long re = 1;     while (n > 0) {       if (n & 1) re = re * a % mo;       a = a * a % mo;       n >>= 1;     }     return re;   }   long long inv(long long b) {     return b == 1 ? 1 : (mod - mod / b) * inv(mod % b) % mod;   }   const int maxn = 2e5 + 5;   int p[maxn][26];   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     ;     string second;     cin >> second;     int n = second.length();     for (int i = (0); i < (n); ++i) {       for (int j = (0); j < (26); ++j) p[i + 1][j] += p[i][j];       p[i + 1][second[i] -  a ]++;     }     int q;     cin >> q;     while (q--) {       int l, r;       cin >> l >> r;       --l;       --r;       if (l == r) {         cout <<  Yes  <<   n ;       } else {         if (second[l] != second[r]) {           cout <<  Yes  <<   n ;         } else {           int tp = 0;           for (int j = (0); j < (26); ++j) {             if (p[r + 1][j] - p[l][j] > 0) tp++;           };           if (tp > 2)             cout <<  Yes  <<   n ;           else             cout <<  No  <<   n ;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     bool check = 1;     vector<pair<int, int>> v;     int a, b, c;     cin >> a;     for (int i = 0; i < a; i++) {       cin >> b >> c;       v.push_back({b, c});     }     for (int i = 0; i < a; i++) {       if (v[i].second != v[i].first) {         cout <<  Happy Alex ;         return 0;       } else {         check = 0;       }     }     if (check == 0) cout <<  Poor Alex ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 110;   int a[N], c[N][N];   int main() {     int n, k;     scanf( %d %d , &n, &k);     for (int i = 0; i < n; ++i) {       scanf( %d , a + i);     }     int mn = a[0], mx = a[0];     for (int i = 0; i < n; ++i) {       if (a[i] < mn) mn = a[i];       if (a[i] > mx) mx = a[i];     }     int d = mx - mn;     if (d > k) {       puts( NO );       return 0;     }     puts( YES );     for (int i = 0; i < n; ++i) {       for (int j = 0; j <= mn; ++j) {         c[i][j] = 1;       }       int t = 2;       for (int j = mn + 1; j < a[i]; ++j) {         c[i][j] = t;         ++t;       }     }     for (int i = 0; i < n; ++i) {       for (int j = 0; j < a[i]; ++j) {         if (j > 0) putchar(   );         printf( %d , c[i][j]);       }       putchar(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char board[500][500];   int n, m, k;   int w = 0, e = 0;   int t;   bool used[500][500];   bool inBound(int a, int b) { return (a >= 0 && a < n && b >= 0 && b < m); }   bool dfs(int a, int b) {     if (t == 0) return false;     bool check = true;     used[a][b] = true;     for (int i = -1; i < 2; i++) {       for (int j = -1; j < 2; j++) {         if (abs(i) + abs(j) == 1 && inBound(a + i, b + j) &&             board[a + i][b + j] ==  . ) {           if (!used[a + i][b + j]) {             check = dfs(a + i, b + j);           }         }       }     }     if (t == 0) {       return false;     }     if (check) {       board[a][b] =  X ;       t--;     }     return check;   }   int main() {     for (int i = 0; i < 500; i++) {       for (int j = 0; j < 500; j++) {         used[i][j] = false;       }     }     cin >> n >> m >> k;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> board[i][j];         if (board[i][j] ==  . )           e++;         else           w++;       }     }     t = k;     bool bla = false;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (board[i][j] ==  . ) {           dfs(i, j);           bla = true;           break;         }       }       if (bla) break;     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cout << board[i][j];       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long sum[100010 << 2], tag[100010 << 2];   int num[100010 << 2], n, m;   bool is[100010 << 2], Tag[100010 << 2];   void build(int p, int l, int r) {     if (l == r) {       is[p] = true, num[p] = l;       return;     }     build(p << 1, l, (l + r) >> 1), build(p << 1 | 1, ((l + r) >> 1) + 1, r);     sum[p] = sum[p << 1] + sum[p << 1 | 1];     if (is[p << 1] && is[p << 1 | 1] && num[p << 1] == num[p << 1 | 1])       num[p] = num[p << 1], is[p] = true;     else       num[p] = 0, is[p] = false;   }   void pushdown(int p, int l, int r) {     if (l == r) return;     int mid = (l + r) >> 1;     if (tag[p]) {       tag[p << 1] += tag[p], sum[p << 1] += 1ll * (mid - l + 1) * tag[p];       tag[p << 1 | 1] += tag[p], sum[p << 1 | 1] += 1ll * (r - mid) * tag[p],           tag[p] = 0;     }     if (Tag[p])       num[p << 1] = num[p], num[p << 1 | 1] = num[p],                Tag[p << 1] = Tag[p << 1 | 1] = true, Tag[p] = false;   }   void change(int p, int l, int r, int x, int y, int z) {     if (x <= l && r <= y && is[p] && num[p]) {       sum[p] += 1ll * abs(z - num[p]) * (r - l + 1);       tag[p] += abs(z - num[p]), Tag[p] = true, num[p] = z;       return;     }     int mid = (l + r) >> 1;     pushdown(p, l, r);     if (x <= mid) change(p << 1, l, mid, x, y, z);     if (y > mid) change(p << 1 | 1, mid + 1, r, x, y, z);     sum[p] = sum[p << 1] + sum[p << 1 | 1];     if (is[p << 1] && is[p << 1 | 1] && num[p << 1] == num[p << 1 | 1])       num[p] = num[p << 1], is[p] = true;     else       num[p] = 0, is[p] = false;   }   long long find(int p, int l, int r, int x, int y) {     if (x <= l && r <= y) return sum[p];     int mid = (l + r) >> 1;     long long tmp = 0;     pushdown(p, l, r);     if (x <= mid) tmp += find(p << 1, l, mid, x, y);     if (y > mid) tmp += find(p << 1 | 1, mid + 1, r, x, y);     return tmp;   }   int main() {     scanf( %d%d , &n, &m), build(1, 1, n);     for (int i = 1, kind, l, r, col; i <= m; i++) {       scanf( %d%d%d , &kind, &l, &r);       if (kind == 1)         scanf( %d , &col), change(1, 1, n, l, r, col);       else         printf( %I64d n , find(1, 1, n, l, r));     }   }


#include <bits/stdc++.h>   using namespace std;   constexpr int MAXN = 1 << 30;   struct tournament {     struct Node {       Node* parent;       set<int, greater<int>> blocked;       Node* left = nullptr;       Node* right = nullptr;       Node(Node* parent) : parent(parent) {}     }* root = new Node(nullptr);     set<int, greater<int>>::iterator it;     void update(int x, int start, int end, int lo = 0, int hi = MAXN,                 Node* node = nullptr) {       if (node == nullptr) node = root;       if (lo >= end || hi <= start) return;       if (lo >= start && hi <= end) {         node->blocked.insert(x);         return;       }       int mid = (lo + hi) / 2;       if (mid > start) {         if (node->left == nullptr) node->left = new Node(node);         update(x, start, end, lo, mid, node->left);       }       if (mid < end) {         if (node->right == nullptr) node->right = new Node(node);         update(x, start, end, mid, hi, node->right);       }     }     int query(int x, int pos) {       Node* node = root;       int lo = 0, hi = MAXN;       int sol = -1;       while (node != nullptr) {         if ((it = node->blocked.lower_bound(x)) != node->blocked.end() &&             *it > sol)           sol = *it;         int mid = (lo + hi) / 2;         if (pos < mid)           hi = mid, node = node->left;         else           lo = mid, node = node->right;       }       return sol;     }   } rows, cols;   set<int> bio;   int unzipA[200100], unzipB[200100];   pair<int, int> a[200100], b[200100];   char dirs[200100];   int main() {     ios_base::sync_with_stdio(false);     int n, q, i, j, x;     char dir;     cin >> n >> q;     rows.update(-1, 0, MAXN), cols.update(-1, 0, MAXN);     for (i = 0; i < q; ++i) {       cin >> b[i].first >> a[i].first >> dirs[i];       --a[i].first, --b[i].first;       a[i].second = b[i].second = i;     }     sort(a, a + q);     sort(b, b + q);     new (unzipA - 1) int;     new (unzipB - 1) int;     unzipA[-1] = unzipB[-1] = -1;     for (i = 0; i < q; ++i) {       unzipA[i] = a[i].first;       unzipB[i] = b[i].first;       a[i].first = b[i].first = i;     }     sort(a, a + q, [](const pair<int, int>& a, const pair<int, int>& b) {       return a.second < b.second;     });     sort(b, b + q, [](const pair<int, int>& a, const pair<int, int>& b) {       return a.second < b.second;     });     for (int k = 0; k < q; ++k) {       i = a[k].first;       j = b[k].first;       dir = dirs[k];       if (bio.count(MAXN + i - j)) {         cout <<  0 n ;         continue;       }       bio.insert(MAXN + i - j);       switch (dir) {         case  U :           x = rows.query(i, j);           cout << unzipA[i] - unzipA[x] << endl;           cols.update(j, x, i);           break;         case  L :           x = cols.query(j, i);           cout << unzipB[j] - unzipB[x] << endl;           rows.update(i, x + 1, j + 1);           break;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     int a, b, p, pos;     int i, cost, j, k, l;     string s;     while (t--) {       cin >> a >> b >> p;       cost = 0;       cin >> s;       pos = s.length() - 2;       i = s.length() - 2;       if (s[i] ==  A ) {         cost += a;         if (cost > p) {           cout << s.length() << endl;           continue;         }       }       if (s[i] ==  B ) {         cost += b;         if (cost > p) {           cout << s.length() << endl;           continue;         }       }       while (i > 0) {         if (s[i] == s[i - 1]) {           i--;           pos = i;         }         if (s[i] != s[i - 1] && i > 0) {           if (s[i - 1] ==  A ) {             cost += a;           }           if (s[i - 1] ==  B ) {             cost += b;           }           if (cost <= p) {             pos = i - 1;             i--;           }           if (cost > p) {             break;           }         }       }       cout << pos + 1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int const MOD = 1e9 + 7;   double const PI = acos(-1);   long long const oo = 8e18;   int const N = 3e5 + 100;   int const M = 1e6 + 100;   int n;   pair<int, int> a[N], p1[N], p2[N];   pair<int, int> intersection(pair<int, int> x, pair<int, int> y) {     if (x.second < y.first || y.second < x.first)       return {-1, -1};     else       return {max(x.first, y.first), min(x.second, y.second)};   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %d%d , &a[i].first, &a[i].second);     sort(a, a + n);     for (int i = 0; i < n; ++i) {       if (i == 0)         p1[i] = a[i];       else {         p1[i] = intersection(a[i], p1[i - 1]);       }     }     for (int i = n - 1; i >= 0; --i) {       if (i == n - 1)         p2[i] = a[i];       else {         p2[i] = intersection(a[i], p2[i + 1]);       }     }     int mx = 0;     for (int i = 0; i < n; ++i) {       pair<int, int> temp;       if (i == 0) {         temp = p2[i + 1];       } else if (i == n - 1) {         temp = p1[i - 1];       } else {         temp = intersection(p1[i - 1], p2[i + 1]);       }       mx = max(mx, temp.second - temp.first);     }     cout << mx << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, x;     cin >> n >> x;     int ar[n], i, cnt = 0;     for (i = 0; i < n; i++) {       cin >> ar[i];       cnt += ar[i];     }     cnt += n - 1;     if (cnt == x)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a1, a2, s1, s2;     cin >> a1 >> a2 >> s1 >> s2;     if (a1 <= a2)       cout <<  Second ;     else       cout <<  First ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5;   bool isprime[32000];   vector<int> prime;   int a[maxn + 50], num[maxn + 50], factor[maxn + 50], now[maxn + 50];   int k, n, len = 0;   long long ans = 0;   bool check() {     for (int i = 1; i <= len; ++i)       if (now[i] < num[i]) return false;     return true;   }   void make(int x, int type) {     for (int i = 1; i <= len; ++i) {       if (x % factor[i] != 0) continue;       if (factor[i] == 1) {         now[i] += type;         continue;       }       while (x % factor[i] == 0) {         x /= factor[i];         now[i] += type;       }       if (x == 1) break;     }   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     isprime[1] = 1;     for (int i = 2; i <= sqrt(k); ++i)       if (!isprime[i]) {         prime.push_back(i);         for (int j = i + i; j <= sqrt(k); j += i) isprime[j] = 1;       }     ++len;     factor[len] = 1;     num[len] = 1;     for (int i = 0; i < prime.size(); ++i) {       if (k % prime[i] != 0) continue;       factor[++len] = prime[i];       while (k % prime[i] == 0) {         k /= prime[i];         ++num[len];       }       if (k == 1) break;     }     if (k != 1) factor[++len] = k, num[len] = 1;     int r = 0;     for (int l = 1; l <= n; ++l) {       while (!check() && r < n) {         ++r;         make(a[r], 1);       }       if (check()) ans += n - r + 1;       make(a[l], -1);     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000500;   const int INF = 1000000000, mod = 1000000007;   const long long LLINF = 1000000000000000000ll;   char s[N];   int z[N];   int cnt[N];   int n;   void calcZ() {     int l = 0, r = 0;     for (int i = 1; i < n; ++i) {       if (i <= r) {         z[i] = min(z[i - l], r - i + 1);       }       while (i + z[i] < n && s[z[i]] == s[i + z[i]]) {         z[i]++;       }       if (i + z[i] >= r) {         l = i;         r = i + z[i] - 1;       }       cnt[z[i]]++;     }   }   int main() {     scanf( %s , &s);     n = strlen(s);     calcZ();     int cur = 0;     for (int i = n; i >= 1; --i) {       cur += cnt[i];       if (cur >= 2 && z[n - i] == i) {         for (int j = 0; j < i; ++j) {           putchar(s[j]);         }         exit(0);       }     }     puts( Just a legend );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool vis[10010][51];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int m, n;     cin >> m >> n;     for (int i = 0; i < m; i++) {       int k;       cin >> k;       for (int j = 0; j < k; j++) {         int temp;         cin >> temp;         vis[temp][i] = true;       }     }     for (int i = 0; i < m; i++) {       for (int j = i + 1; j < m; j++) {         int flag = 0;         for (int k = 1; k < n + 1; k++) {           if (vis[k][i] && vis[k][j]) {             flag = 1;             break;           }         }         if (flag == 0) {           cout <<  impossible  << endl;           return 0;         }       }     }     cout <<  possible  << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int dp[4][N];   string str2[N], str3[N];   set<string> ans;   int n;   int cal(int len, int idx) {     if (dp[len][idx] != -1) return dp[len][idx];     if (idx == n - 1) return dp[len][idx] = 1;     ;     int x = 0;     if (idx + 2 < n) {       if (len == 2 && str2[idx] == str2[idx + 2]) {       } else         x |= cal(2, idx + 2);     }     if (idx + 3 < n) {       if (len == 3 && str3[idx] == str3[idx + 3]) {       } else         x |= cal(3, idx + 3);     }     x = x ? 1 : 0;     return dp[len][idx] = x;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     string s;     cin >> s;     n = s.length();     for (int i = 0; i < n - 1; ++i) {       str2[i + 1] = s.substr(i, 2);       if (i + 2 < n) str3[i + 2] = s.substr(i, 3);     }     for (int i = 0; i < 4; ++i) {       for (int j = 0; j <= n; ++j) dp[i][j] = -1;     }     for (int i = 6; i < n; ++i) {       if (dp[2][i] == -1) cal(2, i);       if (i > 6 && dp[3][i] == -1) cal(3, i);     }     for (int i = 5; i < n; ++i) {       if (dp[2][i] > 0) ans.insert(str2[i]);       if (dp[3][i] > 0) ans.insert(str3[i]);     }     cout << ans.size() << endl;     for (auto I : ans) cout << I << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   const long long INF = 4e18 + 42;   long long l[N];   char type[N];   void get_stam(long long need, long long &stam, long long &have, long long &ans,                 long long cost);   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> l[i];     }     for (int i = 1; i <= n; ++i) {       cin >> type[i];     }     long long swim_res = 0, walk_res = INF;     long long stam = 0, s_fly = 0, w_fly = 0, ans = 0;     for (int i = 1; i <= n; ++i) {       if (type[i] ==  L ) {         get_stam(l[i] - stam, stam, s_fly, ans, 1);         get_stam(l[i] - stam, stam, w_fly, ans, 2);         get_stam(l[i] - stam, stam, swim_res, ans, 3);         get_stam(l[i] - stam, stam, walk_res, ans, 5);         stam -= l[i];         ans += l[i];       } else if (type[i] ==  W ) {         long long fly = min(2 * l[i], stam + l[i]), swim = 2 * l[i] - fly;         stam += (swim - fly) / 2;         ans += (3 * swim + fly) / 2;         s_fly += fly;         swim_res = INF;       } else {         get_stam(l[i] - stam, stam, s_fly, ans, 1);         long long fly = min(2 * l[i], stam + l[i]), walk = 2 * l[i] - fly;         stam += (walk - fly) / 2;         ans += (5 * walk + fly) / 2;         w_fly += fly;       }     }     cout << ans << endl;   }   void get_stam(long long need, long long &stam, long long &have, long long &ans,                 long long cost) {     if (need <= 0) {       return;     }     long long take = min(have, need);     ans += cost * take;     have -= take;     stam += take;   }


#include <bits/stdc++.h>   using namespace std;   void setIO() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   const long double PI = 4 * atan((long double)1);   const int INF = 1e9 + 7;   const long long _INF = 1e18;   int main() {     setIO();     int t;     cin >> t;     while (t--) {       int x, k = 0;       cin >> x;       int y = x;       while (y) {         y /= 10;         k++;       }       cout << 10 * (x % 10 - 1) + k * (k + 1) / 2 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   using dd = long double;   struct Tree {     struct Node {       dd mul;       dd add;       dd sum;       Node() : mul(1), add(0), sum(0) {}     };     int S;     vector<Node> drz;     void update(int v) { drz[v].sum = drz[2 * v].sum + drz[2 * v + 1].sum; }     Tree(const vector<int>& tab) {       int n = tab.size();       S = 1;       while (S < n) {         S *= 2;       }       drz.resize(2 * S);       for (int i = 0; i < n; i++) {         drz[i + S].sum = tab[i];       }       for (int v = S - 1; v; v--) {         update(v);       }     }     void receive(int v, int len, dd mul, dd add) {       drz[v].sum = drz[v].sum * mul + add * len;       drz[v].mul *= mul;       drz[v].add = drz[v].add * mul + add;     }     void push(int v, int len) {       dd mul = drz[v].mul;       dd add = drz[v].add;       receive(2 * v, len / 2, mul, add);       receive(2 * v + 1, len / 2, mul, add);       drz[v].mul = 1;       drz[v].add = 0;     }     void mul_add_rec(int v, int vl, int vr, int l, int r, dd mul, dd add) {       if (vl > r || l > vr) {         return;       }       if (l <= vl && vr <= r) {         receive(v, vr - vl + 1, mul, add);         return;       }       push(v, vr - vl + 1);       int vc = (vl + vr) / 2;       mul_add_rec(2 * v, vl, vc, l, r, mul, add);       mul_add_rec(2 * v + 1, vc + 1, vr, l, r, mul, add);       update(v);     }     void mul_add(int l, int r, dd mul, dd add) {       mul_add_rec(1, 0, S - 1, l, r, mul, add);     }     dd sum_rec(int v, int vl, int vr, int l, int r) {       if (vl > r || l > vr) {         return 0;       }       if (l <= vl && vr <= r) {         return drz[v].sum;       }       push(v, vr - vl + 1);       int vc = (vl + vr) / 2;       return sum_rec(2 * v, vl, vc, l, r) + sum_rec(2 * v + 1, vc + 1, vr, l, r);     }     dd sum(int l, int r) { return sum_rec(1, 0, S - 1, l, r); }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n, q;     cin >> n >> q;     vector<int> tab(n + 1);     for (int i = 1; i <= n; i++) {       cin >> tab[i];     }     Tree tree(tab);     while (q--) {       int t;       cin >> t;       if (t == 1) {         int l1, r1, l2, r2;         cin >> l1 >> r1 >> l2 >> r2;         auto sum1 = tree.sum(l1, r1);         auto sum2 = tree.sum(l2, r2);         int len1 = r1 - l1 + 1;         int len2 = r2 - l2 + 1;         tree.mul_add(l1, r1, 1.0 * (len1 - 1) / len1, 1.0 * sum2 / len1 / len2);         tree.mul_add(l2, r2, 1.0 * (len2 - 1) / len2, 1.0 * sum1 / len1 / len2);       } else {         int l, r;         cin >> l >> r;         cout << setprecision(10) << fixed << tree.sum(l, r) <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int day[100005];   int cnt[100005];   int vis[100005];   int n, m;   int jud(int x) {     memset(vis, 0, sizeof(vis));     int i, j;     int sum = 0, cntt = 0;     ;     for (i = x; i >= 1; i--) {       if (day[i] && vis[day[i]] == 0) {         cntt++;         vis[day[i]] = 1;         sum += cnt[day[i]];       } else if (sum)         sum--;     }     if (cntt == m && sum == 0)       return 1;     else       return 0;   }   int main() {     int i, j;     scanf( %d %d , &n, &m);     for (i = 1; i <= n; i++) scanf( %d , &day[i]);     for (i = 1; i <= m; i++) scanf( %d , &cnt[i]);     int left, right, mid;     int ans = -1;     left = 1, right = n;     while (left <= right) {       mid = (left + right) >> 1;       if (jud(mid)) {         right = mid - 1;         ans = mid;       } else {         left = mid + 1;       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, j, t, k, x, y;     scanf( %d , &t);     int a[10] = {0}, b[10] = {0};     for (j = 0; j < t; j++) {       scanf( %d%d%d , &k, &x, &y);       if (k == 1) {         a[0] = a[0] + x;         a[1] = a[1] + y;       } else {         b[0] = b[0] + x;         b[1] = b[1] + y;       }     }     if (a[0] >= a[1])       printf( LIVE n );     else       printf( DEAD n );     if (b[0] >= b[1])       printf( LIVE n );     else       printf( DEAD n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     vector<string> a;     vector<string> ra;     cin >> n >> m;     for (int i = 0; i < n; ++i) {       string s;       cin >> s;       a.push_back(s);       reverse(s.begin(), s.end());       ra.push_back(s);     }     vector<int> selected;     vector<int> arr(n, -1);     vector<int> pallindrome;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < n; ++j) {         if (ra[i] == a[j]) {           arr[i] = j;           if (arr[j] != i) {             selected.push_back(i);           } else if (i == j) {             pallindrome.push_back(i);           }         }       }     }     cout << (selected.size() * 2 * m) + (pallindrome.size() > 0 ? 1 : 0) * m          << endl;     for (int i = 0; i < selected.size(); ++i) {       cout << a[selected[i]];     }     if (pallindrome.size() > 0) {       cout << a[pallindrome[0]];     }     for (int i = selected.size() - 1; i >= 0; --i) {       cout << ra[selected[i]];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct comp {     bool operator()(long long int a, long long int b) { return a > b; }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int n, k;     cin >> n >> k;     vector<pair<long long int, long long int> > pow(n);     vector<long long int> mon(n);     vector<long long int> res(n);     priority_queue<long long int, vector<long long int>, comp> least;     for (int(i) = (0); (i) < (n); (i)++) {       long long int power;       cin >> power;       pow[i] = make_pair(power, i);     }     sort(pow.begin(), pow.end());     for (int(i) = (0); (i) < (n); (i)++) {       cin >> mon[i];     }     if (k == 0) {       for (int(i) = (0); (i) < (n); (i)++) {         if (i)           cout <<     << mon[i];         else           cout << mon[i];       }       return 0;     }     least.push(mon[pow[0].second]);     res[pow[0].second] = mon[pow[0].second];     for (int(i) = (1); (i) < (min(n, k + 1)); (i)++) {       res[pow[i].second] = res[pow[i - 1].second] + mon[pow[i].second];       least.push(mon[pow[i].second]);     }     for (int(i) = (k + 1); (i) < (n); (i)++) {       long long int toremove = least.top();       least.pop();       res[pow[i].second] = res[pow[i - 1].second] + mon[pow[i].second] - toremove;       least.push(mon[pow[i].second]);     }     for (int(i) = (0); (i) < (n); (i)++) {       if (i)         cout <<     << res[i];       else         cout << res[i];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n;   char str[1000010];   long long cntu[1000010];   long long cntd[1000010];   long long sumu[1000010];   long long sumd[1000010];   long long CNTU;   long long CNTD;   int main() {     scanf( %I64d%s , &n, str + 1);     for (long long i = 1; i <= n; i++) {       cntu[i] = cntu[i - 1];       cntd[i] = cntd[i - 1];       sumu[i] = sumu[i - 1];       sumd[i] = sumd[i - 1];       if (str[i] ==  U ) {         CNTU++;         cntu[i]++;         sumu[i] += i;       } else {         CNTD++;         cntd[i]++;         sumd[i] += i;       }     }     for (long long i = 1; i <= n; i++)       if (str[i] ==  U ) {         if (cntu[i - 1] >= CNTD - cntd[i]) {           long long k =               upper_bound(cntu + 1, cntu + n + 1, cntu[i - 1] - CNTD + cntd[i]) -               cntu;           printf( %I64d  ,                  (-(sumu[i - 1] - sumu[k - 1]) + (sumd[n] - sumd[i])) * 2 + n -                      i + 1);         } else {           long long k =               lower_bound(cntd + 1, cntd + n + 1, cntd[i] + cntu[i - 1] + 1) -               cntd;           printf( %I64d  , (-sumu[i - 1] + (sumd[k] - sumd[i]) - i) * 2 + i);         }       } else {         if (cntu[i - 1] <= CNTD - cntd[i]) {           long long k =               lower_bound(cntd + i, cntd + n + 1, cntd[i] + cntu[i - 1]) - cntd;           printf( %I64d  , (-sumu[i - 1] + (sumd[k] - sumd[i])) * 2 + i);         } else {           long long k = upper_bound(cntu + 1, cntu + n + 1,                                     cntu[i - 1] - CNTD + cntd[i] - 1) -                         cntu;           printf( %I64d  ,                  (i - (sumu[i - 1] - sumu[k - 1]) + (sumd[n] - sumd[i])) * 2 + n -                      i + 1);         }       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 7002;   const int N = 100010;   int n, q;   bitset<M> num[M];   bitset<M> sq[M];   bitset<M> a[N];   vector<int> squareFree;   int main() {     cin >> n >> q;     for (int i = 1; i < M; ++i) {       int good = 1;       for (int j = 2; j * j <= i; ++j) {         if (i % (j * j) == 0) {           good = 0;           break;         }       }       if (good) squareFree.push_back(i);     }     for (int i = 1; i < M; ++i) {       for (int j = 1; j * j <= i; ++j) {         if (i % j == 0) {           num[i][j] = num[i][i / j] = 1;         }       }     }     for (int i = 1; i < M; ++i) {       for (int j : squareFree) {         if (i * j > 7000) break;         sq[i][i * j] = 1;       }     }     while (q--) {       int cmd, x, y, z, v;       scanf( %d %d , &cmd, &x);       if (cmd == 1) {         scanf( %d , &v);         a[x] = num[v];       } else if (cmd == 2) {         scanf( %d %d , &y, &z);         a[x] = a[y] ^ a[z];       } else if (cmd == 3) {         scanf( %d %d , &y, &z);         a[x] = a[y] & a[z];       } else {         scanf( %d , &v);         int ans = (sq[v] & a[x]).count() % 2;         printf( %d , ans);       }     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long double EPS = 1e-9;   long long cel(long long a, long long b) { return ((a - 1) / b + 1); }   long long gcd(long long a, long long b) {     if (a < b) swap(a, b);     return (b == 0) ? a : gcd(b, a % b);   }   long long MIN(long long a, int b) {     long long ans;     (a >= b) ? ans = b : ans = a;     return ans;   }   long long MAX(long long a, int b) {     long long ans;     (a >= b) ? ans = a : ans = b;     return ans;   }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   long long po(long long x, long long y) {     long long ans = 1;     while (y) {       if (y & 1) {         ans = (ans * x) % 1000000007;       }       y >>= 1;       x = (x * x) % 1000000007;     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, t, x, y, m, k;     cin >> n >> m;     vector<int> v(1000001, 0);     vector<int> A(n), B(n), C(m), D(m);     for (int i = 0; i < n; i++) {       cin >> A[i] >> B[i];     }     for (int i = 0; i < m; i++) {       cin >> C[i] >> D[i];     }     for (int i = 0; i < m; i++) {       for (int j = 0; j < n; j++) {         if (C[i] - A[j] >= 0) {           v[C[i] - A[j]] = max(v[C[i] - A[j]], D[i] - B[j] + 1);         }       }     }     for (int i = 1000000 - 1; i >= 0; i--) {       v[i] = max(v[i + 1], v[i]);     }     int ans = 1000000007;     for (int i = 0; i < 1000001; i++) {       y = ans;       ans = min(ans, i + v[i]);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int count_on(int mask) {     int ans = 0;     while (mask) {       if (mask & 1) {         ans++;       }       mask >>= 1;     }     return ans;   }   int dp[1 << 20];   string bin(int mask) {     string str =   ;     while (mask) {       if (mask & 1) {         str += ( 1 );       } else {         str += ( 0 );       }       mask >>= 1;     }     reverse(str.begin(), str.end());     if (str ==   ) {       str =  0 ;     }     return str;   }   int main() {     string str;     cin >> str;     vector<int> masks;     int N = str.size();     for (int i = 0; i < N; i++) {       int mask = 0;       for (int j = 0; i + j < N && j < 20; j++) {         int dig = str[i + j] -  a ;         if (mask & (1 << dig)) {           break;         }         mask |= (1 << dig);         masks.push_back(mask);         dp[mask] = count_on(mask);       }     }     for (int i = 0; i < (1 << 20); i++) {       for (int j = 0; j < 20; j++) {         if (i & (1 << j)) {           dp[i] = max(dp[i], dp[i ^ (1 << j)]);         }       }     }     int best = 0;     for (auto mask : masks) {       int ans = dp[mask] + dp[((1 << 20) - 1) ^ mask];       best = max(best, ans);     }     cout << best << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int M;   int next_losing_index(int A, int losing_index) {     return A == 0 || losing_index < M ? min(losing_index + 1, M) : 0;   }   struct segment_change {     bool flip;     segment_change(bool _flip = false) : flip(_flip) {}     void reset() { flip = false; }     segment_change combine(const segment_change &other) const {       return segment_change(flip ^ other.flip);     }   };   struct segment {     vector<int> through, flipped;     segment() {       through.resize(M + 1);       flipped.resize(M + 1);       for (int index = 0; index <= M; index++)         through[index] = flipped[index] = index;     }     void init(int start, int end, int A) {       for (int index = 0; index <= M; index++) {         through[index] = next_losing_index(A, index);         flipped[index] = next_losing_index(A ^ 1, index);       }     }     void apply(int start, int end, const segment_change &change) {       if (change.flip) swap(through, flipped);     }     void join(const segment &other) {       vector<int> next_through(M + 1), next_flipped(M + 1);       for (int index = 0; index <= M; index++) {         next_through[index] = through[other.through[index]];         next_flipped[index] = flipped[other.flipped[index]];       }       through = next_through;       flipped = next_flipped;     }     void join(const segment &a, const segment &b) {       *this = a;       join(b);     }   };   struct seg_tree {     int tree_n;     vector<segment> tree;     vector<segment_change> changes;     seg_tree(int n = 0) {       tree_n = 0;       if (n > 0) init(n);     }     void init(int n) {       tree_n = 1;       while (tree_n < n) tree_n *= 2;       tree.assign(2 * tree_n, segment());       changes.assign(2 * tree_n, segment_change());     }     template <typename T>     void build(const vector<T> &initial) {       int n = initial.size();       assert(n <= tree_n);       for (int i = 0; i < n; i++) tree[tree_n + i].init(i, i + 1, initial[i]);       for (int position = tree_n - 1; position > 0; position--)         tree[position].join(tree[2 * position], tree[2 * position + 1]);     }     void push_down(int position, int start, int mid, int end) {       tree[2 * position].apply(start, mid, changes[position]);       tree[2 * position + 1].apply(mid, end, changes[position]);       changes[2 * position] = changes[2 * position].combine(changes[position]);       changes[2 * position + 1] =           changes[2 * position + 1].combine(changes[position]);       changes[position].reset();     }     template <typename T_range_op, typename T_join_op>     void process_range(int position, int start, int end, int a, int b,                        T_range_op &&range_op, T_join_op &&join_op) {       if (a <= start && end <= b) {         range_op(position, start, end);         return;       }       if (position >= tree_n) return;       int mid = (start + end) / 2;       push_down(position, start, mid, end);       if (a < mid)         process_range(2 * position, start, mid, a, b, range_op, join_op);       if (b > mid)         process_range(2 * position + 1, mid, end, a, b, range_op, join_op);       join_op(position);     }     segment query(int a, int b) {       segment answer;       process_range(           1, 0, tree_n, a, b,           [&](int position, int, int) { answer.join(tree[position]); },           [&](int) {});       return answer;     }     void update(int a, int b, const segment_change &change) {       process_range(           1, 0, tree_n, a, b,           [&](int position, int start, int end) {             tree[position].apply(start, end, change);             changes[position] = changes[position].combine(change);           },           [&](int position) {             tree[position].join(tree[2 * position], tree[2 * position + 1]);           });     }     template <typename T_bool>     int find_last_prefix(int n, T_bool &&should_join) {       segment current;       if (!should_join(current, current)) return -1;       if (should_join(current, tree[1])) return n;       int position = 1;       int start = 0, end = tree_n;       while (position < tree_n) {         int mid = (start + end) / 2;         push_down(position, start, mid, end);         if (should_join(current, tree[2 * position])) {           current.join(tree[2 * position]);           position = 2 * position + 1;           start = mid;         } else {           position = 2 * position;           end = mid;         }       }       assert(position == tree_n + start);       assert(0 <= start && start < n);       assert(end - start == 1);       return start;     }   };   int N, Q;   vector<int> A;   int main() {     scanf( %d %d %d , &N, &M, &Q);     A.resize(N);     for (int i = 0; i < N; i++) {       long long x;       scanf( %lld , &x);       A[i] = x % 2;     }     seg_tree tree(N);     tree.build(A);     for (int q = 0; q < Q; q++) {       int type, L, R;       scanf( %d %d %d , &type, &L, &R);       L--;       if (type == 1) {         long long d;         scanf( %lld , &d);         if (d % 2 == 1) tree.update(L, R, segment_change(true));       } else if (type == 2) {         puts(tree.query(L, R).through[M] != 0 ?  1  :  2 );       }     }   }


#include <bits/stdc++.h>   using namespace std;   vector<int> tree[100005];   int cat[100005];   bool vis[100005];   int n, m, ans = 0;   void dfs(int root, int num) {     vis[root] = true;     if (cat[root])       num++;     else       num = 0;     if (num > m) return;     if (tree[root].size() == 1 && vis[tree[root][0]]) {       ans++;       return;     }     for (int i = 0; i < tree[root].size(); i++) {       int c = tree[root][i];       if (!vis[c]) dfs(c, num);     }   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %d , &cat[i]);     for (int i = 0; i < n - 1; i++) {       int f, s;       scanf( %d %d , &f, &s);       tree[f].push_back(s);       tree[s].push_back(f);     }     dfs(1, 0);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     string t;     cin >> s >> t;     long j = 0;     long ans = 0;     for (long i = 0; i < t.size(); i++) {       if (t[i] == s[j]) ++j;       if (j == s.size()) {         long rj = s.size() - 1;         for (long k = t.size() - 1; k >= 0; k--) {           if (t[k] == s[rj]) --rj;           if (rj == -1) {             cout << std::max(k - i, long(0));             return 0;           }         }         break;       }     }     cout << 0;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     char ch[n];     ch[n] =   0 ;     int a[n][2];     for (int i = 0; i < n; i++) {       cin >> a[i][0] >> a[i][1];     }     int suma = 0;     int sumg = 0;     for (int i = 0; i < n; i++) {       if (((a[i][0] + suma) - sumg) <= 500) {         suma += a[i][0];         ch[i] =  A ;       } else if (((a[i][1] + sumg) - suma) <= 500) {         sumg += a[i][1];         ch[i] =  G ;       }     }     if ((abs(suma - sumg)) > 500) {       cout <<  -1 ;     } else {       cout << ch;     }   }