#include <cstdio>   #include <iostream>   #include <cstring>   #include <algorithm>   #include <cmath>   #include <bitset>   #include <vector>   #include <unordered_map>   using namespace std;      const int N = 100007, M = 500007, INF = 0x3f3f3f3f;      typedef long long ll;   typedef int itn;      int n, m, t, x;   int fa[N];   int deep[N];   int maxx;   int num[N];//每一层结点个数   int cnt;   int lson[N];//叶子节点   int head[N], ver[M], edge[M], nex[M], tot;   bitset<N> f[2007];//能否凑齐 x，使用 bitset 优化   int val[N];   unordered_map<int, int> vis;   //int vis[N];   bool ok[N];   vector<int> v[N];      void add(int x, itn y)   {    ver[tot] = y;    nex[tot] = head[x];    head[x] = tot ++ ;   }      void dfs1(int x)   {    deep[x] = deep[fa[x]] + 1;//这里根结点 deep[1] = 1 所以不用再 +1 了    num[deep[x]] ++ ;    maxx = max(maxx, deep[x]);//树的直径，实际上也就是最大深度    if (head[x] == -1) //没有子节点，说明是叶子结点     lson[deep[x]] ++ ;//每层的叶子结点个数    for (int i = head[x]; ~i; i = nex[i]) {     int y = ver[i];     dfs1(y);    }   }      void dfs2(itn x, int t)   {    if(x == 0)     return;    for (int i = 0; i < (int)v[x].size(); ++ i) {     if(val[x] > t || f[x - 1][t])      break;     t -= val[x], ok[v[x][i]] = true;    }    dfs2(x - 1, t);   }      void init()   {       memset(head, -1, sizeof head);       tot = 0;   }      void solve()   {       init();    scanf( %d%d , &n, &x);    for (int i = 2; i <= n; ++ i) {     scanf( %d , &fa[i]);     //add(i, fa[i]);     add(fa[i], i);    }    dfs1(1);//预处理每层结点个数    //把抽象的树转化为一个个物品，存到 v 里，离散化一下，种类（结点个数）和个数（层数）    //cout <<  ok  << endl;    //一共cnt种，    for (int i = 1; i <= maxx; ++ i) {     if (vis[num[i]]) {              v[vis[num[i]]].push_back(i);     }     else {               vis[num[i]] = ++ cnt;               val[cnt] = num[i];               v[cnt].push_back(i);//来找最后选了第几层     }    }       f[0][0] = 1;          for (int i = 1; i <= cnt; ++ i) {           f[i] = f[i - 1];     itn Size = v[i].size();     for (int j = 1; j <= Size; j <<= 1) {      Size -= j;      f[i] |= (f[i] << (j * val[i]));     }     if(Size > 0)      f[i] |= f[i] << (Size * val[i]);    }       if(f[cnt][x]) {     printf( %d n , maxx);     dfs2(cnt, x);//还原dp的方案     for (int i = 1; i <= n; ++ i) {      putchar(ok[deep[i]] ?  a  :  b );     }    }    //凑不齐， a  有剩余    else {     int res = INF;     for (int i = x; i >= 0; -- i) {      if(f[cnt][i]) {       res = i;//找到能凑到的最大的数       break;      }     }     dfs2(cnt, res);     int pos = -1;     for (int i = 1; i <= maxx; ++ i)     {      if(!ok[i] && lson[i] >= x - res) {       pos = i;       break;      }     }        printf( %d n , maxx + 1);        for (int i = 1; i <= n; ++ i) {      if(deep[i] == pos && head[i] == -1) {       if(res == x) {        putchar( b );       }       else        putchar( a ), ++ res;      }      else {       putchar(ok[deep[i]] ?  a  :  b );      }     }    }    return ;   }      itn main()   {       solve();    return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5, inf = 1e9;   int a[maxn];   struct segtree {     int lb, rb;     int mn = 0;     segtree *l, *r;     segtree(int _lb, int _rb) {       lb = _lb, rb = _rb;       if (lb == rb)         mn = a[lb];       else {         int t = (lb + rb) / 2;         l = new segtree(lb, t);         r = new segtree(t + 1, rb);         mn = min(l->mn, r->mn);       }     }     int rmq(int lq, int rq) {       if (lb >= lq && rb <= rq)         return mn;       else if (lb > rq || rb < lq)         return inf;       return min(l->rmq(lq, rq), r->rmq(lq, rq));     }   };   segtree *p;   vector<pair<int, int> > ans;   void solve(int l, int r, int d) {     if (l > r) return;     int mn = p->rmq(l, r);     while (mn > d) ans.push_back({l + 1, r + 1}), d++;     int lb = l, rb = r;     while (lb != rb) {       int t = (lb + rb) / 2;       if (p->rmq(l, t) == d)         rb = t;       else         lb = t + 1;     }     solve(l, lb - 1, d);     solve(lb + 1, r, d);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     p = new segtree(0, n - 1);     solve(0, n - 1, 0);     cout << ans.size() <<   n ;     for (auto x : ans) cout << x.first <<     << x.second <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<string> v;     map<char, int> m;     int k;     cin >> k;     string q;     cin >> q;     int l = q.size();     int flag = 1;     int cnt = 0;     string s;     if (l < k) {       cout <<  NO ;       return 0;     } else {       cnt = 1;       s += q[0];       m[q[0]] = 1;       for (int i = 1; i < l; i++) {         if (m[q[i]] == 0 and cnt < k) {           m[q[i]] += 1;           cnt++;           v.push_back(s);           s =   ;           s += q[i];         } else {           s += q[i];         }       }       v.push_back(s);     }     if (v.size() == k) {       cout <<  YES  << endl;       for (int i = 0; i < v.size(); i++) cout << v[i] << endl;     } else {       cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     double h, l;     scanf( %lf %lf , &h, &l);     printf( %lf n , (l * l - h * h) / (2 * h * 1.0));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX_N = 5000;   const long long MAX_Q = 10000;   struct Node {     vector<long long> val;     vector<int> nex;   };   long long cnt = 1, cur = 0,             pows[5] = {                 1LL, (MAX_Q + 1LL), (MAX_Q + 1LL) * (MAX_Q + 1LL),                 (MAX_Q + 1LL) * (MAX_Q + 1LL) * (MAX_Q + 1LL),                 (MAX_Q + 1LL) * (MAX_Q + 1LL) * (MAX_Q + 1LL) * (MAX_Q + 1LL)};   Node G[MAX_N];   void dfs(int pos, int dep, long long uplim) {     if (uplim > MAX_Q) {       cur += pows[dep] - 1LL;       return;     }     if (dep == 1) {       for (int i = 1; i <= uplim; i++) {         cur++;         G[pos].val.push_back(cur);         G[pos].nex.push_back(-1);       }       return;     }     long long cur_uplim = uplim;     for (int i = 1; i <= uplim + 1; i++) {       cnt++;       G[pos].nex.push_back(cnt);       dfs(cnt, dep - 1, cur_uplim);       if (i != uplim + 1) {         cur++;         G[pos].val.push_back(cur);         cur_uplim = cur + 1;       }     }   }   int main() {     dfs(1, 5, 1);     long long cx = 1, range_l = 1, range_r = cur;     for (int t = 4; t >= 0; t--) {       vector<long long> D;       if (cx != -1 && cx <= cnt && G[cx].val.size() >= 1) {         D = G[cx].val;       } else {         for (int i = 1; i <= MAX_Q; i++)           D.push_back((range_l - 1LL) + 1LL * i * pows[t]);       }       cout << D.size();       for (int i = 0; i < D.size(); i++) cout <<     << D[i];       cout << endl;       int S;       cin >> S;       if (S == -1) break;       if (cx != -1 && cx <= cnt && G[cx].val.size() >= 1)         cx = G[cx].nex[S];       else         cx = -1;       if (S >= 1) range_l = D[S - 1] + 1LL;       if (S < (int)D.size() - 1) range_r = D[S] - 1LL;     }     return 0;   }


#include <bits/stdc++.h>   void initialize(int *arr, int length) {     for (int i = 0; i < length; i++) arr[i] = 0;   }   int teams(int *arr, int length) {     for (int i = 1; i < length; i++) {       if (arr[i - 1] == 1 && arr[i] == 1) return 2;     }     return 1;   }   int main() {     int queries, len, curr, sieve[100];     scanf( %d n , &queries);     for (int i = 0; i < queries; i++) {       initialize(sieve, 100);       scanf( %d n , &len);       for (int j = 0; j < len; j++) {         scanf( %d , &curr);         sieve[curr - 1] = 1;       }       scanf(  n );       printf( %d n , teams(sieve, 100));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename A, typename B>   ostream &operator<<(ostream &os, const pair<A, B> &p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename T_container, typename T = typename enable_if<                                       !is_same<T_container, string>::value,                                       typename T_container::value_type>::type>   ostream &operator<<(ostream &os, const T_container &v) {     string sep;     for (const T &x : v) os << sep << x, sep =    ;     return os;   }   const int mxN = 12, oo = 1e9;   bool g[12][12], used[13];   long long ans = 0;   int n = 12, k;   int a[12];   bool pused[6];   bool goodr(int i = 0, int d = 0) {     if (d == n - 2) return g[a[i]][a[i + 1]];     for (int to = 0; to < n; to += 2)       if (!pused[to]) {         if ((g[a[i]][a[to]] and g[a[i + 1]][a[to + 1]]) or             (g[a[i]][a[to + 1]] and g[a[i + 1]][a[to]])) {           pused[to] = true;           if (goodr(to, d + 2)) {             pused[to] = false;             return true;           }           pused[to] = false;         }       }     return false;   }   bool good() {     for (int i = 0; i < n; i += 2) {       pused[i] = true;       if (goodr(i)) {         pused[i] = false;         return true;       }       pused[i] = false;     }     return false;   }   set<array<char, 12>> s;   int eq = 0;   array<char, 12> equiv, mp, reale;   void normalize() {     fill(begin(mp), end(mp), -1);     int t = 0;     for (int i = 0; i < n; ++i) {       if (mp[equiv[i]] == -1) mp[equiv[i]] = t++;       reale[i] = mp[equiv[i]];     }   }   void rec2(int i = 0) {     if (i == n) {       normalize();       if (eq > k) return;       if (!s.count(reale)) {         int cur = 1;         for (int j = k - eq + 1; j <= k; ++j) {           cur *= j;         }         ans += cur;         s.insert(reale);       }       return;     }     equiv[a[i]] = eq;     equiv[a[i + 1]] = eq;     eq++;     rec2(i + 2);     eq--;     for (int j = 0; j < eq; ++j) {       equiv[a[i]] = j;       equiv[a[i + 1]] = j;       rec2(i + 2);     }   }   void rec(int i, int d = 0) {     while (used[i]) ++i;     if (i == n) {       if (good()) {         rec2();       }       return;     }     used[i] = true;     for (int j = i + 1; j < n; ++j)       if (!used[j]) {         used[j] = true;         a[d] = i;         a[d + 1] = j;         rec(i + 1, d + 2);         used[j] = false;       }     used[i] = false;   }   int main() {     int m;     cin >> n >> m >> k;     for (int i = 0; i < m; ++i) {       int u, v;       cin >> u >> v, --u, --v;       g[u][v] = true;       g[v][u] = true;     }     rec(0);     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0), cout.tie(0);     int T;     cin >> T;     while (T--) {       int n;       cin >> n;       vector<int> v(n);       for (int i = 0; i < n; i++) {         cin >> v[i];       }       int i;       bool poss = 1;       for (i = 0; i < n; i++)         if (v[i] < i) break;       i--;       for (int j = i; j < n - 1; j++) {         if (v[j + 1] >= v[j]) v[j + 1] = v[j] - 1;         if (v[j + 1] < 0) {           poss = 0;           break;         }       }       if (poss)         cout <<  Yes n ;       else         cout <<  No n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 998244353;   int main() {     ios_base::sync_with_stdio(false);     long long r, s, p;     cin >> r >> s >> p;     double dp[r + 1][s + 1][p + 1];     int visited[r + 1][s + 1][p + 1];     for (int i = 0; i < r + 1; i++) {       for (int j = 0; j < s + 1; j++) {         for (int k = 0; k < p + 1; k++) {           dp[i][j][k] = 0;           visited[i][j][k] = 0;         }       }     }     dp[r][s][p] = 1;     visited[r][s][p] = 1;     queue<pair<pair<double, double>, double> > q;     q.push(make_pair(make_pair(r, s), p));     while (!q.empty()) {       int sz = q.size();       for (int i = 0; i < sz; i++) {         pair<pair<double, double>, double> curp = q.front();         q.pop();         double rr = curp.first.first;         double ss = curp.first.second;         double rp = curp.second;         if ((int)rr > 0 && (int)ss > 0) {           dp[(int)rr][(int)ss - 1][(int)rp] +=               dp[(int)rr][(int)ss][(int)rp] *               (((rr * ss)) / ((rr * ss + rp * rr + ss * rp)));           if (visited[(int)rr][(int)ss - 1][(int)rp] == 0) {             q.push(make_pair(make_pair(rr, ss - 1), rp));             visited[(int)rr][(int)ss - 1][(int)rp] = 1;           }         }         if ((int)rr > 0 && (int)rp > 0) {           dp[(int)rr - 1][(int)ss][(int)rp] +=               dp[(int)rr][(int)ss][(int)rp] *               (((rr * rp)) / ((rr * ss + rp * rr + ss * rp)));           if (visited[(int)rr - 1][(int)ss][(int)rp] == 0) {             q.push(make_pair(make_pair(rr - 1, ss), rp));             visited[(int)rr - 1][(int)ss][(int)rp] = 1;           }         }         if ((int)rp > 0 && (int)ss > 0) {           dp[(int)rr][(int)ss][(int)rp - 1] +=               dp[(int)rr][(int)ss][(int)rp] *               (((rp * ss)) / ((rr * ss + rp * rr + ss * rp)));           if (visited[(int)rr][(int)ss][(int)rp - 1] == 0) {             q.push(make_pair(make_pair(rr, ss), rp - 1));             visited[(int)rr][(int)ss][(int)rp - 1] = 1;           }         }       }     }     double ans1, ans2, ans3;     ans1 = ans2 = ans3 = 0;     for (int i = 1; i <= r; i++) {       ans1 += dp[i][0][0];     }     for (int i = 1; i <= s; i++) {       ans2 += dp[0][i][0];     }     for (int i = 1; i <= p; i++) {       ans3 += dp[0][0][i];     }     std::cout << std::setprecision(9) << ans1 <<     << ans2 <<     << ans3;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       string s;       cin >> s;       int a[4]{0};       int i, j, k, ans, min1 = 0, min2 = 0;       for (i = 0; i < s.size(); i++) {         if (s[i] ==  L ) a[0]++;         if (s[i] ==  R ) a[1]++;         if (s[i] ==  U ) a[2]++;         if (s[i] ==  D ) a[3]++;       }       min1 = min(a[0], a[1]);       min2 = min(a[2], a[3]);       if (min1 >= 1 && min2 == 0) {         cout <<  2  << endl;         cout <<  LR ;       } else if (min1 == 0 && min2 >= 1) {         cout <<  2  << endl;         cout <<  UD ;       } else {         string b(2 * (min1 + min2),  1 );         for (i = 0; i < 2 * (min1 + min2); i++) {           if (i < min1)             b[i] =  R ;           else if (i >= min1 + min2 && i < (2 * min1 + min2))             b[i] =  L ;           else if (i >= (2 * min1 + min2))             b[i] =  U ;           else             b[i] =  D ;         }         cout << b.size() << endl;         cout << b;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int e, s, n, m;   vector<int> comp;   vector<pair<int, int> > has;   vector<int> all, butR, onlyS;   int query[200013];   int id(int x) {     return lower_bound(comp.begin(), comp.end(), x) - comp.begin();   }   vector<int> cover(int use) {     vector<int> res(comp.size());     for (auto i : has)       if (i.second >= use) {         res[id(i.first)] += 1;         res[id(i.first + s)] -= 1;       }     int prev = 0;     for (int& i : res) i += prev, prev = i;     for (int i = 0; i < res.size() - 1; i++) {       if (res[i]) res[i] = comp[i + 1] - comp[i];     }     prev = 0;     for (int& i : res) i += prev, prev = i;     return res;   }   bool test(vector<int>& vec, int ind) {     return ind != 0 && vec[ind] > vec[ind - 1];   }   pair<int, int> solve(int f) {     if (f + s >= e) return make_pair(0, 0);     f += s;     int eAt = id(e) - 1, fAt = id(f) - 1;     if (all[eAt] - all[fAt] != e - f) return make_pair(-1, -1);     int needR = (e - f) - (butR[eAt] - butR[fAt]);     int needP = (e - f) - (onlyS[eAt] - onlyS[fAt]) - needR;     return make_pair(needR, needP);   }   int main() {     scanf( %d%d%d%d , &e, &s, &n, &m);     comp.push_back(e);     comp.push_back(-1.01e9);     for (int i = 0; i < n; i++) {       int t, x;       scanf( %d%d , &t, &x);       if (x >= e) continue;       has.push_back(make_pair(x, t));       comp.push_back(x);       comp.push_back(x + s);     }     for (int i = 0; i < m; i++) {       scanf( %d , &query[i]);       comp.push_back(query[i]);       comp.push_back(query[i] + s);     }     sort(comp.begin(), comp.end());     comp.erase(unique(comp.begin(), comp.end()), comp.end());     all = cover(1);     butR = cover(2);     onlyS = cover(3);     for (int i = 0; i < m; i++) {       pair<int, int> ans = solve(query[i]);       printf( %d %d n , ans.first, ans.second);     }     return 0;   }


#include <bits/stdc++.h>   const int N = 22;   const int M = (1 << 22);   const int INF = 0x3f3f3f3f;   bool dp[M] = {0};   int neib[N] = {0}, ex[M] = {0};   int main() {     int m, n, u, v;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; ++i) {       neib[i] = (1 << i);       dp[1 << i] = true;     }     for (int i = 0; i < m; ++i) {       scanf( %d%d , &u, &v);       --u;       --v;       dp[(1 << u) | (1 << v)] = true;       neib[u] |= (1 << v);       neib[v] |= (1 << u);     }     if (n == 1 || m == n * (n - 1) / 2) {       printf( 0 n );       return 0;     }     for (int i = 0; i < (1 << n); ++i)       if (dp[i]) {         for (int j = 0; j < n; ++j)           if (i & (1 << j)) {             ex[i] |= neib[j];           }         for (int j = 0; j < n; ++j)           if (ex[i] & (1 << j)) {             dp[i | (1 << j)] = true;           }       }     int best = INF;     std::vector<int> ans;     for (int i = 0; i < (1 << n); ++i)       if (dp[i] && ex[i] == (1 << n) - 1) {         int cnt = 0;         for (int j = 0; j < n; ++j)           if (i & (1 << j)) ++cnt;         if (cnt >= best) continue;         best = cnt;         ans.clear();         for (int j = 0; j < n; ++j)           if (i & (1 << j)) ans.push_back(j);       }     printf( %d n , ans.size());     for (int i = 0; i < ans.size(); ++i) printf( %d  , ans[i] + 1);     putchar(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   set<int> s[100001];   int a[100001];   int main() {     cin >> n >> m;     for (int i = 1; i <= m; i++) {       int x, y;       cin >> x >> y;       s[x].insert(y);       s[y].insert(x);     }     srand(time(0));     for (int op = 1; op <= 200; op++) {       for (int i = 1; i <= n; i++) a[i] = i;       random_shuffle(a + 1, a + n + 1);       a[n + 1] = a[1];       int i;       for (i = 1; i <= m; i++) {         if (s[a[i]].count(a[i + 1])) break;       }       if (i == m + 1) {         for (i = 1; i <= m; i++) cout << a[i] <<     << a[i + 1] << endl;         return 0;       }     }     cout << -1;     return 0;   }


// BY : VIDIT BHARDWAJ   #include<bits/stdc++.h>   using namespace std;      #define int                 long long int   #define ff                  first   #define ss                  second   #define pb                  push_back   #define popb                pop_back   #define lb                  lower_bound   #define ub                  upper_bound   #define all(v)              (v).begin(),(v).end()   #define tc(t)               int t; cin>>t; while(t--)   #define pii                 pair<int,int>   #define max_pq              priority_queue<int>   #define min_pq              priority_queue<int,vector<int>, greater<int> >   #define fast_ios            ios_base::sync_with_stdio(false);cin.tie(NULL);   #define ps(x,y)             fixed<<setprecision(y)<<x    #define setbits(x)          __builtin_popcountll(x)   #define zerobits(x)         __builtin_ctzll(x)   #define YES                 cout<< YES <<  n    #define NO                  cout<< NO <<  n    #define Yes                 cout<< Yes <<  n    #define No                  cout<< No <<  n    #define endl                  n               const int N=200010;   const long long mod=1000000007;   // const long long mod=998244353;   const long long inf=1000000000000000000;   const long double pi=3.14159265358979323846264338;      /*_______________________________________________________________________*/      signed main(){   #ifndef ONLINE_JUDGE       freopen( input.txt ,  r , stdin);       freopen( output.txt ,  w , stdout);   #endif       fast_ios       tc(t){           string str,s; cin>>str>>s;           int n=str.size(),m=s.size();              if(n<m){            string temp=str;            str=s,s=temp;            n=str.size(),m=s.size();           }              if(s!=str.substr(0,s.size())){            cout<< -1 <<endl;            continue;           }           int it=500,yes=0;        string temp=s;        while(it>0){         if(s.size()%str.size()==0){          yes=1;          break;         }         s+=temp,it--;        }        if(yes){         temp=str;         int x=s.size()/str.size();         for(int i=1;i<x;i++){          str+=temp;         }         if(str==s) cout<<s<<endl;         else cout<< -1 <<endl;        }        else cout<< -1 <<endl;       }          return 0;   }   /*      */


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 5;   long long a[N];   struct t {     int lval, rval, mval;   } tr[N * 4];   int sign(long long x) {     if (x > 0) return 1;     if (x < 0) return -1;     return 0;   }   inline void cal(int cur, int l, int r) {     int m = (l + r) / 2;     int dcur = cur + cur, u = dcur, v = u + 1;     tr[cur].mval = max(tr[u].mval, tr[v].mval);     tr[cur].lval = tr[u].lval;     tr[cur].rval = tr[v].rval;     if (!(!a[m] || !a[m + 1] || sign(a[m]) < sign(a[m + 1]))) {       tr[cur].mval = max(tr[cur].mval, tr[u].rval + tr[v].lval);       if (tr[dcur].mval == m - l + 1) {         tr[cur].lval = tr[u].lval + tr[v].lval;       }       if (tr[dcur + 1].mval == r - m) {         tr[cur].rval = tr[v].rval + tr[u].rval;       }     }   }   void build(int pos, int l, int r) {     if (l == r) {       int x = !!a[l];       tr[pos].lval = tr[pos].rval = tr[pos].mval = x;       return;     }     int mid = (l + r) >> 1;     build(pos * 2, l, mid);     build(pos * 2 + 1, mid + 1, r);     cal(pos, l, r);   }   void update(int pos, int l, int r, int k, int d) {     int mid = (l + r) >> 1;     if (l == r) {       a[l] += d;       int x = !!a[l];       tr[pos].lval = tr[pos].rval = tr[pos].mval = x;       return;     } else {       int mid = (l + r) >> 1;       if (k <= mid)         update(pos * 2, l, mid, k, d);       else         update(pos * 2 + 1, mid + 1, r, k, d);       cal(pos, l, r);     }   }   int main() {     int u, v, n, i, m, l, r, d;     cin >> n;     cin >> u;     for (i = 1; i < n; ++i) scanf( %d , &v), a[i - 1] = v - u, u = v;     if (n > 1) build(1, 0, n - 2);     cin >> m;     while (m--) {       scanf( %d%d%d , &l, &r, &u);       if (n == 1) {         printf( 1 n );       } else {         if (l > 1) update(1, 0, n - 2, l - 2, u);         if (r < n) update(1, 0, n - 2, r - 1, -u);         printf( %d n , tr[1].mval + 1);       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long f[5005][5005];   long long mod = 1000000007, ans = 0;   char s1[5005], s2[5005];   int main() {     scanf( %s  n %s , s1 + 1, s2 + 1);     int len1 = strlen(s1 + 1);     int len2 = strlen(s2 + 1);     for (int i = 1; i <= len1; ++i)       for (int j = 1; j <= len2; ++j) {         if (s1[i] == s2[j]) f[i][j] = (f[i - 1][j - 1] + 1) % mod;         f[i][j] = (f[i][j] + f[i][j - 1]) % mod;       }     for (int i = 1; i <= len1; ++i) ans = (ans + f[i][len2]) % mod;     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     int good = 0;     int bad = 0;     cin >> s;     for (int i = 0; i < s.length(); i++) {       if (s.at(i) ==  a )         good++;       else         bad++;     }     if (good > bad)       cout << s.length();     else       cout << s.length() - (bad - good) - 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int x[3];     cin >> x[0] >> x[1] >> x[2];     sort(x, x + 3);     cout << (x[1] - x[0]) + (x[2] - x[1]) <<   n ;     ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long llINF = 9223372036854775807;   const int INF = 2147483647;   const int maxn = 2e5 + 7;   const int maxm = 5e4 + 7;   const long long mod = 1e9 + 7;   const double pi = acos(-1.0);   const double eps = 1e-12;   int n;   int fa[2][maxn];   int a[maxn], b[maxn];   int u[maxn], v[maxn], w[maxn];   long long ans;   void init() {     for (int j = 0; j < 2; j++)       for (int i = 0; i < maxn; i++) fa[j][i] = i;   }   int find(int x, int idx) {     return x == fa[idx][x] ? x : fa[idx][x] = find(fa[idx][x], idx);   }   void merge(int x, int y, int idx) { fa[idx][find(x, idx)] = find(y, idx); }   int main(int argc, char const *argv[]) {     scanf( %d , &n);     for (int i = 1; i <= n - 1; i++) scanf( %d%d%d , &u[i], &v[i], &w[i]);     init();     for (int i = 1; i <= n - 1; i++) {       if (w[i] == 0)         merge(u[i], v[i], 0);       else         merge(u[i], v[i], 1);     }     for (int i = 1; i <= n; i++) a[find(i, 0)]++;     for (int i = 1; i <= n; i++) b[find(i, 1)]++;     for (int i = 1; i <= n; i++) ans += 1ll * a[find(i, 0)] * b[find(i, 1)];     printf( %lld n , ans - n);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using point = complex<double>;   const int T = 1 << 16;   point root[T];   void fft(vector<point>& a) {     int n = a.size();     vector<int> reverse(n);     for (int i = 0; i < n; ++i) {       reverse[i] = (reverse[i / 2] + (i % 2) * n) / 2;       if (i < reverse[i]) {         swap(a[i], a[reverse[i]]);       }     }     for (int i = 1; i < n; i *= 2) {       for (int j = 0; j < n; j += 2 * i) {         for (int k = 0; k < i; ++k) {           point l = a[j + k];           point r = a[i + j + k] * root[i + k];           a[j + k] = l + r;           a[i + j + k] = l - r;         }       }     }   }   vector<point> mul(vector<point> a, vector<point> b) {     int n = 1, m = a.size() + b.size();     while (n < m) {       n *= 2;     }     a.resize(n), b.resize(n);     fft(a), fft(b);     for (int i = 0; i < n; ++i) {       a[i] *= b[i];     }     fft(a);     for (auto& i : a) {       i /= n;     }     reverse(a.begin() + 1, a.end());     a.resize(m - 1);     return a;   }   const int M = 100;   const int N = 50;   double prob[M][T], expect[M][T], ans[N][T];   array<int, 3> edges[M];   long long dist[N];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout << fixed << setprecision(6);     for (int i = 1; i < T; i *= 2) {       for (int j = 0; j < i; ++j) {         root[i + j] = polar(1.0, j * M_PI / i);       }     }     int n, m, t, x;     cin >> n >> m >> t >> x;     for (int i = 0; i < m; ++i) {       int u, v, w;       cin >> u >> v >> w;       edges[i] = {u - 1, v - 1, w};       for (int j = 1; j <= t; ++j) {         int p;         cin >> p;         prob[i][j] = p / 100000.0;       }     }     fill(dist, dist + n - 1, LLONG_MAX);     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         auto [u, v, w] = edges[j];         if (dist[v] < LLONG_MAX) {           dist[u] = min(dist[u], dist[v] + w);         }       }     }     for (int i = 0; i < n - 1; ++i) {       fill(ans[i], ans[i] + t + 1, HUGE_VAL);     }     for (int i = 0; i < m; ++i) {       auto [u, v, w] = edges[i];       double sum = 0.0;       for (int j = t; j >= 0; --j) {         expect[i][j] = w + sum * (dist[v] + x);         sum += prob[i][j];       }       ans[u][0] = min(ans[u][0], expect[i][0]);     }     for (int i = 1; i <= t; ++i) {       for (int j = 0; j < m; ++j) {         auto [u, v, w] = edges[j];         for (int k = 1; i % k == 0; k *= 2) {           vector<point> poly =               mul(vector<point>(ans[v] + i - k, ans[v] + i),                   vector<point>(prob[j] + k, prob[j] + min(2 * k, t + 1)));           for (int l = 0; l <= t - i && l <= (int)poly.size(); ++l) {             expect[j][l + i] += poly[l].real();           }         }         ans[u][i] = min(ans[u][i], expect[j][i]);       }     }     cout << ans[0][t] <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   bool prime[1007];   int n;   void sieve() {     for (int i = 4; i <= 1005; i += 2) prime[i] = false, prime[i - 1] = true;     prime[2] = true;     for (int i = 3; i <= 1000; i += 2) {       if (prime[i]) {         for (int j = i * i; j <= 1005; j += i * 2) prime[j] = false;       }     }   }   int main() {     ios::sync_with_stdio(false);     sieve();     scanf( %d , &n);     vector<int> ans;     for (int i = 1; i <= n; i++) {       if (prime[i]) {         int q = 1;         while (q <= n / i) {           q *= i;           ans.push_back(q);         }       }     }     cout << (ans.size()) << endl;     for (int i : ans) {       printf( %d  , i);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long q, x;     cin >> q >> x;     long long n = q;     long long a[n];     map<long long, long long> m;     long long min = 0;     map<long long, long long> f;     for (long long i = 0; i < n; i++) {       cin >> a[i];       long long mod = a[i] % x;       f[mod]++;       m[mod + ((f[mod] - 1) * x)]++;       while (m[min] == 1) min++;       cout << min << endl;     }     map<long long, long long>::iterator it;   }


#include <bits/stdc++.h>   int read() {     static int c, x;     while ((c = getchar()) < 48) {     }     x = c & 15;     while ((c = getchar()) >= 48) x = x * 10 + (c & 15);     return x;   }   struct edge {     int next, to;   } e[100010 << 1];   int ans = 0;   int head[100010], deg[100010], f0[100010], f1[100010];   inline void add(const int &x, const int &y) {     static int ei;     e[++ei].next = head[x];     e[ei].to = y;     head[x] = ei;     ++deg[x];   }   void dfs(const int &x, const int &fa) {     f1[x] = 1;     f0[x] = deg[x] - 1;     for (int i = head[x]; i; i = e[i].next) {       const int y = e[i].to;       if (y != fa) {         dfs(y, x);         ans = std::max(ans, f0[x] + std::max(f0[y], f1[y]));         ans = std::max(ans, f1[x] + f0[y]);         f0[x] = std::max(f0[x], std::max(f0[y], f1[y]) + deg[x] - 2);         f1[x] = std::max(f1[x], f0[y] + 1);       }     }   }   int main() {     const int n = read();     for (int i = n; --i;) {       const int x = read(), y = read();       add(x, y);       add(y, x);     }     dfs(1, 0);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int nroot = floor(sqrt(n));       if (nroot * nroot >= n) {         cout << 2 * nroot - 2 <<   n ;       } else if ((nroot + 1) * nroot >= n) {         cout << 2 * nroot - 1 <<   n ;       } else {         cout << 2 * nroot <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   const int maxn = 100000;   struct point {     int x, y;   };   point p[maxn + 10];   int n, used[maxn + 10];   inline int one_line(int a, int b, int c) {     if ((a == 0) || (b == 0) || (c == 0)) {       return 1;     }     return 1ll * (p[c].x - p[a].x) * (p[b].y - p[a].y) ==            1ll * (p[c].y - p[a].y) * (p[b].x - p[a].x);   }   int check(int a, int b) {     memset(used, 0, sizeof used);     used[a] = used[b] = 1;     for (register int i = 1; i <= n; ++i) {       if ((!used[i]) && (one_line(a, b, i))) {         used[i] = 1;       }     }     int u = 0, v = 0, w = 0, flag = 1;     for (register int i = 1; i <= n; ++i) {       if (!used[i]) {         u = v;         v = w;         w = i;         if (!one_line(u, v, w)) {           flag = 0;           break;         }       }     }     return flag;   }   int main() {     scanf( %d , &n);     for (register int i = 1; i <= n; ++i) {       scanf( %d%d , &p[i].x, &p[i].y);     }     if (check(1, 2) || check(2, 3) || check(1, 3)) {       puts( YES );     } else {       puts( NO );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int T;     scanf( %d , &T);     while (T--) {       int x, n, m;       scanf( %d%d%d , &x, &n, &m);       while (n-- && x >= 20) x = floor(1.0 * x / 2) + 10;       while (m--) x -= 10;       printf(x <= 0 ?  YES n  :  NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int MOD = 1e9 + 7;   int n, m;   vector<int> liga[200100];   vector<int> adj[200100];   int state[200100];   int id[200100], sz[200100];   int vis[200200];   vector<int> top;   int find(int x) {     if (id[x] == x) return x;     return id[x] = find(id[x]);   }   void join(int a, int b) {     a = find(a);     b = find(b);     if (a == b) return;     if (sz[a] < sz[b]) swap(a, b);     id[b] = a;     sz[a] += sz[b];   }   void dfs(int v) {     vis[v] = 1;     for (auto x : adj[v]) {       if (vis[x]) continue;       dfs(x);     }     top.push_back(v);   }   void dfs2(int v, int ori) {     vis[v] = 1;     for (auto x : adj[v]) {       if (vis[x]) continue;       join(ori, x);       dfs2(x, ori);     }   }   void add(int a, int b) { adj[a].push_back(b); }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> state[i];     for (int i = 1; i <= 2 * m; i++) {       id[i] = i;       sz[i] = 1;     }     for (int i = 1; i <= m; i++) {       int amt;       cin >> amt;       while (amt--) {         int x;         cin >> x;         liga[x].push_back(i);       }     }     for (int i = 1; i <= n; i++) {       int x = liga[i][0];       int y = liga[i][1];       if (state[i]) {         add(2 * x - 1, 2 * y - 1);         add(2 * y, 2 * x);         add(2 * y - 1, 2 * x - 1);         add(2 * x, 2 * y);       } else {         add(2 * x, 2 * y - 1);         add(2 * y, 2 * x - 1);         add(2 * x - 1, 2 * y);         add(2 * y - 1, 2 * x);       }     }     for (int i = 1; i <= 2 * m; i++)       if (!vis[i]) dfs(i);     reverse(top.begin(), top.end());     memset(vis, 0, sizeof(vis));     for (auto x : top)       if (!vis[x]) dfs2(x, x);     for (int i = 1; i <= m; i++) {       if (find(2 * i) == find(2 * i - 1)) {         cout <<  NO  << endl;         return 0;       }     }     cout <<  YES  << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     while (cin >> n >> m) {       int tmp = 0, mid = (m + 1) / 2, res = mid;       while (n--) {         if (tmp % 2 == m % 2)           res -= tmp;         else           res += tmp;         if (tmp == m) tmp = 0, res = mid;         cout << res << endl;         tmp++;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int v[100010];   int j[100010];   string ss;   bool haha;   int m;   void jump(int n) {     if (n < 0 || n >= m) {       haha = true;       return;     } else if (j[n] == 1) {       haha = false;       return;     } else {       int p;       if (ss[n] ==  < )         p = -1;       else if (ss[n] ==  > )         p = 1;       j[n] = 1;       jump(n + p * v[n]);       return;     }   }   int main(int argc, const char* argv[]) {     memset(j, 0, sizeof(j));     memset(v, 0, sizeof(v));     int n;     cin >> n;     m = n;     cin >> ss;     haha = true;     for (int i = 0; i < n; i++) {       cin >> v[i];     }     jump(0);     if (haha) {       printf( FINITE n );     } else {       printf( INFINITE n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int Search[1001][1001], Target1[1001], Target2[1001], Attack = 0;   int main() {     int Data1, Data2;     cin >> Data1 >> Data2;     for (int i = 0; i < Data1; i++) {       for (int i2 = 0; i2 < Data2; i2++) {         char Test;         cin >> Test;         if (Test ==  * ) {           Search[i][i2] = 1, Attack++;         }         Target1[i] += Search[i][i2];         Target2[i2] += Search[i][i2];       }     }     for (int i = 0; i < Data1; i++) {       for (int i2 = 0; i2 < Data2; i2++) {         if (Search[i][i2] == 0 && Target1[i] + Target2[i2] == Attack)           return cout <<  YES  << endl << i + 1 <<     << i2 + 1, 0;         if (Search[i][i2] == 1 && Target1[i] + Target2[i2] == Attack + 1)           return cout <<  YES  << endl << i + 1 <<     << i2 + 1, 0;       }     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   long long mulmod(long long a, long long b, long long MOD) {     if (b == 0) return 0;     long long res = mulmod(a, b >> 1, MOD);     res += res;     res %= MOD;     return (b & 1) ? (a + res) % MOD : res;   }   int gcd(int a, int b) {     while (a != 0 && b != 0) {       if (a > b) {         a %= b;       } else {         b %= a;       }     }     return a + b;   }   long long gcd(long long a, long long b) {     while (a != 0 && b != 0) {       if (a > b) {         a %= b;       } else {         b %= a;       }     }     return a + b;   }   long long next(long long i) {     long long a[13], j = 0;     memset(a, 0, sizeof(a));     while (i != 0) {       a[j] = i % 10;       i /= 10;       j++;     }     bool pp = 0;     for (int k = j - 1; k >= 0; k--) {       if (pp) {         a[k] = 4;         continue;       }       if (a[k] < 4) {         a[k] = 4;         pp = 1;         continue;       }       if (4 < a[k] && a[k] < 7) {         a[k] = 7;         pp = 1;         continue;       }       if (a[k] > 7) {         while (a[k + 1] == 7) {           k++;         }         if (a[k + 1] == 4) {           a[k + 1] = 7;         } else {           a[k + 1] = 4;         }         a[k] = 4;         pp = 1;         continue;       }     }     i = 0;     for (int j = 12; j >= 0; j--) {       i = i * 10 + a[j];     }     return i;   }   int main() {     ios::sync_with_stdio(0);     int n, k;     string s;     cin >> n >> k >> s;     for (int i = 0; i < n - 1; i++) {       if (i < 0) i = 0;       if (k <= 0) break;       if (s[i] ==  4  && s[i + 1] ==  7 ) {         if (i % 2 == 0) {           if (i + 2 < n && s[i + 2] ==  7 ) {             if (k % 2 == 1) {               s[i + 1] =  4 ;             }             k = 0;             break;           } else {             s[i + 1] =  4 ;             k--;           }         } else {           s[i] =  7 ;           i -= 2;           k--;         }       }     }     cout << s;     return 0;   }


#include <bits/stdc++.h>   namespace IO {   template <typename Tp>   inline Tp input() {     Tp x = 0, y = 1;     char c = getchar();     while ((c <  0  ||  9  < c) && c != EOF) {       if (c ==  - ) y = -1;       c = getchar();     }     if (c == EOF) return 0;     while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getchar();     return x *= y;   }   template <typename Tp>   inline void read(Tp &x) {     x = input<Tp>();   }   };  // namespace IO   using namespace IO;   namespace modular {   const int MOD = 1000000007;   inline int add(int x, int y) { return (x += y) >= MOD ? x -= MOD : x; }   inline void inc(int &x, int y) { (x += y) >= MOD ? x -= MOD : 0; }   inline int mul(int x, int y) { return 1LL * x * y % MOD; }   inline int qpow(int x, int y) {     int ans = 1;     for (; y; y >>= 1, x = mul(x, x))       if (y & 1) ans = mul(ans, x);     return ans;   }   };  // namespace modular   int N, top, tot;   int a[2007], b[2007], c[3];   std::pair<int, int> ans[2007 * 2007];   bool cmp(int x, int y) { return a[x] < a[y]; }   int main() {     srand(0x66ccf);     read(N);     for (int i = 1; i <= N; ++i) read(a[i]);     top = N;     for (int i = 1; i <= top; ++i) b[i] = i;     std::sort(b + 1, b + N + 1, cmp);     std::reverse(b + 1, b + N + 1);     while (top && !a[b[top]]) top--;     if (top < 2) return puts( -1 ), 0;     while (top > 2) {       for (int i = 0; i <= 2; ++i) {         c[i] = rand() % (top - i) + i + 1;         std::swap(b[i + 1], b[c[i]]);         c[i] = b[i + 1];       }       std::sort(c, c + 3, cmp);       int k = a[c[1]] / a[c[0]], mx = 0;       for (int i = 0; i <= 21; ++i)         if (k >> i & 1) mx = i;       for (int i = 0; i <= 2; ++i) b[i + 1] = c[i];       for (int i = 0; i <= mx; ++i) {         if (k >> i & 1) {           ans[++tot] = std::pair<int, int>(c[0], c[1]);           a[c[1]] -= a[c[0]];           a[c[0]] *= 2;         } else {           ans[++tot] = std::pair<int, int>(c[0], c[2]);           a[c[2]] -= a[c[0]];           a[c[0]] *= 2;         }       }       if (!a[c[1]]) {         std::swap(b[top], b[2]);         top--;       }     }     printf( %d n , tot);     for (int i = 1; i <= tot; ++i) printf( %d %d n , ans[i].first, ans[i].second);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1000000000 + 9;   long long pow(long long a, int n, long long mod) {     long long res = 1;     while (n) {       if (n % 2) {         res *= a;         res %= mod;         --n;       } else {         a *= a;         a %= mod;         n /= 2;       }     }     return res;   }   inline long long dec(long long a, long long b) {     return ((a - b) % mod + mod) % mod;   }   int main() {     int n, m;     cin >> n >> m;     if (m < 26 && n > pow(2, m, mod) - 1) {       cout << 0;       return 0;     }     long long ans = dec(pow(2, m, mod), 1);     int i;     for (i = 1; i < n; ++i) {       ans *= dec(pow(2, m, mod), i + 1);       ans %= mod;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 105;   const int oo = 1000000000;   long long n, k;   long long a[N], b[N], c[N];   long long module = 1000000007;   void print(vector<vector<long long> > mat) {     for (int j = 0; j < mat.size(); j++) {       for (int k = 0; k < mat[j].size(); k++) cout << mat[j][k] <<    ;       cout << endl;     }   }   vector<vector<long long> > mul(vector<vector<long long> > mat1,                                  vector<vector<long long> > mat2) {     vector<vector<long long> > res;     for (int i = 0; i < mat1.size(); i++) {       vector<long long> row;       for (int k = 0; k < mat2[0].size(); k++) {         long long temp = 0;         for (int j = 0; j < mat1[0].size(); j++) {           temp = (temp + mat1[i][j] * mat2[j][k] % module) % module;         }         row.push_back(temp);       }       res.push_back(row);     }     return res;   }   vector<vector<long long> > pow(vector<vector<long long> > mat, long long t) {     vector<vector<long long> > res;     if (t == 0) {       for (int i = 0; i < 16; i++) {         vector<long long> row;         for (int j = 0; j < 16; j++) {           if (i == j)             row.push_back(1);           else             row.push_back(0);         }         res.push_back(row);       }       return res;     }     res = pow(mat, t / 2);     res = mul(res, res);     if (t % 2 == 0)       return res;     else       return mul(mat, res);   }   int main() {     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> a[i] >> b[i] >> c[i];     vector<vector<long long> > base;     vector<long long> row;     row.push_back(1);     base.push_back(row);     for (int i = 1; i < 16; i++) {       row.resize(0);       row.push_back(0);       base.push_back(row);     }     base.push_back(row);     for (int i = 1; i <= n; i++) {       long long dist = 0;       if (i == n)         dist = k - a[i];       else         dist = b[i] - a[i];       vector<vector<long long> > mat;       for (int j = 0; j < 16; j++) {         vector<long long> row;         for (int k = 0; k < 16; k++) {           if (j <= c[i] && k <= c[i] && (k == j || k == j - 1 || k == j + 1))             row.push_back(1);           else             row.push_back(0);         }         mat.push_back(row);       }       base = mul(pow(mat, dist), base);     }     cout << base[0][0];     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:16777216 )   using namespace std;   int com(const void* a, const void* b) { return *(int*)a - *(int*)b; }   int main(void) {     int n, k, i;     cin >> n >> k;     if (n / 2 > k)       cout << -1;     else if (n == 1 && k != 0)       cout << -1;     else {       i = n / 2 - 1;       int l = k - i;       if (n > 1) {         cout << l <<     << l * 2 <<    ;         for (l = l * 2 + 1; i > 0; i--, l += 2) printf( %d %d  , l, l + 1);       }       if (n % 2) printf( %d , l);     }   }


#include <bits/stdc++.h>   using namespace std;   int a[200001];   int b[600001];   int n, L;   bool chk(int d) {     if (d * 2 >= L) return true;     int l = 0, r = 3 * n - 1;     while (b[l] < a[0] - d) l++;     while (b[r] > a[0] + d) r--;     for (int i = 1; i < n; i++) {       l++;       r++;       while (b[l] < a[i] - d) l++;       if (b[r] > a[i] + d) r--;     }     return l <= r;   }   void Init() {     scanf( %d%d , &n, &L);     for (int i = 0; i < n; i++) scanf( %d , a + i);     for (int i = 0; i < n; i++) scanf( %d , b + n + i);   }   void Solve() {     for (int i = 0; i < n; i++) {       b[i] = b[i + n] - L;       b[i + 2 * n] = b[i + n] + L;     }     sort(a, a + n);     sort(b, b + 3 * n);     int left = 0, right = L / 2 + 1;     while (left < right) {       int m = (left + right) / 2;       if (chk(m))         right = m;       else         left = m + 1;     }     printf( %d n , left);   }   int main() {     Init();     Solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int A[2006];   int B[2005];   map<int, int> mp;   int main() {     int a, b, n, x;     while (cin >> n) {       mp.clear();       int t = 1, x1, y1;       for (int i = 0; i < n; i++) {         scanf( %d , &A[i]);         if (mp.count(A[i]) == 0) mp[A[i]] = t++;       }       for (int i = 0; i < n; i++) {         scanf( %d , &B[i]);         if (mp.count(B[i]) == 0) mp[B[i]] = t++;       }       int sum = 0;       for (int i = 0; i < n; i++) {         for (int j = 0; j < n; j++) {           if (i == j) {             x = A[i] ^ B[j];             if (mp.count(x) == 1) sum++;           } else {             x = A[i] ^ B[j];             if (mp.count(x) == 1) sum++;           }         }       }       if (sum % 2 == 0)         cout <<  Karen  << endl;       else         cout <<  Koyomi  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100005;   int n, a[MAXN * 3], u[MAXN * 3], v[MAXN * 3], front, rear;   int main() {     int Q;     long long x;     scanf( %d%d , &n, &Q);     front = 1;     rear = n;     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= 2 * n - 2; i++) {       u[i] = a[front], v[i] = a[front + 1];       if (u[i] < v[i])         a[++front] = v[i], a[++rear] = u[i];       else         a[++front] = u[i], a[++rear] = v[i];     }     while (Q--) {       cin >> x;       if (x > n - 1) x = x % (n - 1) + n - 1;       printf( %d %d n , u[x], v[x]);     }     return 0;   }


#include <bits/stdc++.h>   const long long INF = (long long)1e18 + 123;   const int inf = (int)2e9 + 123;   const int mod = 1e9 + 7;   using namespace std;   int n, m, d[8000011], a[1000011], b[1000011];   void upd(int id, int x, int v = 1, int tl = 1, int tr = 1000000) {     if (tl > id || tr < id) return;     if (tl == tr) {       d[v] = x;       return;     }     int mid = (tl + tr) / 2;     upd(id, x, v * 2, tl, mid);     upd(id, x, v * 2 + 1, mid + 1, tr);     d[v] = d[v * 2] + d[v * 2 + 1];   }   int get(int x, int v = 1, int tl = 1, int tr = 1000000) {     if (tl == tr) return tl;     int mid = (tl + tr) / 2;     if (d[v * 2] >= x) {       return get(x, v * 2, tl, mid);     } else       return get(x - d[v * 2], v * 2 + 1, mid + 1, tr);   }   int get_id(int id, int v = 1, int tl = 1, int tr = 1000000) {     if (tl == tr) return d[v];     int mid = (tl + tr) / 2;     if (id <= mid)       return get_id(id, v * 2, tl, mid);     else       return get_id(id, v * 2 + 1, mid + 1, tr);   }   int size = 0;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> m >> n;     vector<int> v;     for (int i = 1; i <= n; i++) {       cin >> b[i];     }     for (int i = 1; i <= m; i++) {       int type;       cin >> type;       if (type == -1) {         int SZ = d[1];         for (int j = 1; j <= n; j++) {           if (b[j] > SZ) break;           upd(get(b[j] - j + 1), 0);         }       } else {         a[++size] = type;         upd(size, 1);       }     }     for (int i = 1; i <= size; i++) {       if (get_id(i) > 0) v.push_back(a[i]);     }     if (!v.size()) {       cout <<  Poor stack! ;       return 0;     }     for (int i = 0; i < v.size(); i++) {       cout << v[i];     }     return 0;   }


#include <bits/stdc++.h>   namespace IO {   template <typename T>   inline void read(T &x) {     x = 0;     int f = 1;     char ch = getchar();     while (ch >  9  || ch <  0 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       x = (x << 3) + (x << 1) + ch -  0 , ch = getchar();     x *= f;     return;   }   template <typename T>   inline void write(T x = 0) {     T wr[51];     wr[0] = 0;     if (x < 0) putchar( - ), x = -x;     if (!x) putchar(48);     while (x) wr[++wr[0]] = x % 10, x /= 10;     while (wr[0]) putchar(48 + wr[wr[0]--]);     return;   }   }  // namespace IO   using IO::read;   using IO::write;   using namespace std;   const int N = 4e6 + 11;   int n, m, top, now, k;   int op[N], a[N], b[N], fa[N], sz[N], sa[N], sb[N], cur[N], ti[N];   vector<pair<int, int> > v[N];   void modify(int x, int y, int a, int b, int k, int l, int r) {     if (x <= l && r <= y) {       v[k].push_back(pair<int, int>(a, b));       return;     }     int mid = (l + r) >> 1;     if (x <= mid) modify(x, y, a, b, k << 1, l, mid);     if (mid < y) modify(x, y, a, b, k << 1 | 1, mid + 1, r);   }   int find(int x) { return fa[x] == x ? x : find(fa[x]); }   void merge(int x, int y) {     x = find(x), y = find(y);     if (x == y) return;     if (sz[x] < sz[y]) swap(x, y);     sa[++top] = x, sb[top] = y;     sz[x] += sz[y];     fa[y] = x;   }   void del(int tim) {     int x, y;     while (top > tim) {       x = sa[top], y = sb[top];       fa[y] = y, sz[x] -= sz[y];       --top;     }   }   void dfs(int k, int l, int r) {     int t = top;     for (auto s : v[k]) merge(s.first, s.second);     if (l == r) {       if (op[l] == 2) printf( %d n , sz[find(a[l])]);       del(t);       return;     }     int mid = (l + r) >> 1;     dfs(k << 1, l, mid), dfs(k << 1 | 1, mid + 1, r);     del(t);   }   int main() {     read(n);     read(m);     read(k);     for (int i = 1; i <= n; ++i) fa[i] = i, sz[i] = 1;     for (int i = 1; i <= m; ++i) {       read(op[i]);       if (op[i] == 1)         read(a[i]), read(b[i]), cur[i] = now;       else if (op[i] == 2)         read(a[i]), cur[i] = now;       else         ti[++now] = i, cur[i] = now;     }     for (int i = 1; i <= m; ++i)       if (op[i] == 1) {         if (cur[i] + k > now)           modify(i, m, a[i], b[i], 1, 1, m);         else           modify(i, ti[cur[i] + k], a[i], b[i], 1, 1, m);       }     dfs(1, 1, m);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     long long n;     cin >> n;     long long a[n];     map<long long, long long> m;     for (long long i = 0; i < n; i++) {       cin >> a[i];       m[a[i]]++;     }     long long ctr = 0, mx = 1;     for (auto i : m) {       ctr++;       if (i.second > 1) mx = max(mx, i.second);     }     cout << mx <<     << ctr;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 65;   const int M = 5e4 + 10;   int n, m, a[N], b[N], q, trans[M], ans;   vector<int> p[N * N];   template <class T>   void chkMax(T &x, T y) {     if (x < y) x = y;   }   int Do(int x, int y) {     int i = 0, j = 0, ret = 0, szx = p[x].size(), szy = p[y].size();     while (i < szx && j < szy) {       ++ret;       if (p[x][i] == p[y][j])         ++i, ++j;       else if (p[x][i] < p[y][j])         ++i;       else         ++j;     }     return ret + szx - i + szy - j;   }   int main() {     cin >> n >> m;     for (int i = 0; i < n; ++i) cin >> a[i];     for (int i = 0; i < m; ++i) cin >> b[i];     memset(trans, -1, sizeof(trans));     q = 0;     for (int i = 0; i < n; ++i)       for (int j = 0; j < m; ++j) {         int tmp = a[i] + b[j] + 2e4 + 1;         if (trans[tmp] == -1) p[trans[tmp] = q++].clear();         p[trans[tmp]].push_back(i);         p[trans[tmp]].push_back(j + n);       }     for (int i = 0; i < q; ++i) {       sort(p[i].begin(), p[i].end());       p[i].resize(unique(p[i].begin(), p[i].end()) - p[i].begin());     }     ans = 0;     if (q == 1) ans = n + m;     for (int i = 0; i < q; ++i)       for (int j = i + 1; j < q; ++j) chkMax(ans, Do(i, j));     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int piles = 1, heavy = 0, num;   int a[110], n;   void solve(int x) {     if (num == 0) {       return;     }     if (heavy <= a[x]) {       a[x] = -1;       heavy++;       num--;       solve(x + 1);     } else if (x < n) {       solve(x + 1);     } else {       for (int i = 1; i <= n; i++) {         if (a[i] != -1) {           a[i] = -1;           num--;           heavy = 1;           piles++;           break;         }       }       solve(1);     }   }   int main() {     cin >> n;     num = n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     for (int i = 2; i <= n; i++) {       for (int j = 1; j <= i; j++) {         if (a[i] < a[j]) {           swap(a[i], a[j]);         }       }     }     solve(1);     cout << piles;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, nxt[100010], pre[100010];   pair<int, int> b[100010];   struct Frog {     int p, a, id;   } a[100010];   set<pair<int, int> > s;   inline bool operator<(Frog a, Frog b) { return a.p < b.p; }   inline int cal(int i, int j) {     if (i == j) return 0x7fffffff;     int p1 = a[i].p, p2 = a[j].p;     if (i > j) (p2 += a[j].a) %= m;     int d = (p2 - p1 + m) % m, step = (a[i].a - a[j].a);     if (d <= a[i].a) return 1;     if (a[j].a >= a[i].a) return 0x7fffffff;     return (d - a[j].a + step - 1) / step;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++)       scanf( %d%d , &a[i].p, &a[i].a), a[i].p--, b[i] = make_pair(a[i].p, i);     sort(b, b + n);     for (int i = 0; i < n; i++) {       nxt[b[i].second] = b[(i + 1) % n].second;       pre[b[(i + 1) % n].second] = b[i].second;     }     for (int i = 0; i < n; i++) s.insert(make_pair(cal(i, nxt[i]), i));     while (!s.empty()) {       set<pair<int, int> >::iterator it = s.begin();       int i = it->second;       if (it->first == 0x7fffffff) break;       s.erase(it);       s.erase(make_pair(cal(nxt[i], nxt[nxt[i]]), nxt[i]));       s.erase(make_pair(cal(pre[i], i), pre[i]));       a[i].p += cal(i, nxt[i]);       a[i].a--;       nxt[i] = nxt[nxt[i]];       pre[nxt[i]] = i;       s.insert(make_pair(cal(pre[i], i), pre[i]));       s.insert(make_pair(cal(i, nxt[i]), i));     }     printf( %d n , s.size());     for (set<pair<int, int> >::iterator it = s.begin(); it != s.end(); it++)       printf( %d  , it->second + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a, b, c;     map<char, char> mp;     mp.clear();     int s = 0;     cin >> a;     cin >> b;     cin >> c;     s = c.size();     for (int i = 0; i < 26; i++) {       mp[a[i]] = b[i];     }     for (int i = 0; i < s; i++) {       if (c[i] < 97 && c[i] >= 65) {         c[i] += 32;         cout << char(mp[c[i]] - 32);       } else if (c[i] >= 97)         cout << mp[c[i]];       else         cout << c[i];     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 3e5 + 5;   const long long mod = 1e9 + 7;   const long double PI = acos(-1);   long long n, k;   pair<long long, long long> a[N];   long long ans[N];   int main() {     cin >> n >> k;     for (int i = 0; i < n; i++) {       scanf( %lld , &a[i].first);       a[i].second = i;     }     set<int> st;     for (int i = k + 1; i <= k + n; i++) {       st.insert(i);     }     sort(a, a + n);     reverse(a, a + n);     long long out = 0;     for (int i = 0; i < n; i++) {       auto it = st.lower_bound(a[i].second + 1);       out += ((*it - (a[i].second + 1)) * 1ll * a[i].first);       ans[a[i].second] = *it;       st.erase(it);     }     printf( %lld n , out);     for (int i = 0; i < n; i++)       printf( %lld%c , ans[i], (i + 1 == n ?   n  :    ));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int occ[26];   int main() {     string s, p;     cin >> s >> p;     int err = 0;     if (s.size() < p.size()) {       cout << 0 << endl;       return 0;     }     for (int i = 0; i < p.size(); i++) {       occ[p[i] -  a ]++;     }     for (int i = 0; i < p.size(); i++) {       if (s[i] !=  ? ) {         occ[s[i] -  a ]--;         if (occ[s[i] -  a ] == -1) err++;       }     }     int sol = 0;     for (int i = 0; i <= s.size() - p.size(); i++) {       if (err == 0) sol++;       if (i + p.size() == s.size()) break;       if (s[i] !=  ? ) {         occ[s[i] -  a ]++;         if (occ[s[i] -  a ] == 0) err--;       }       if (s[i + p.size()] !=  ? ) {         occ[s[i + p.size()] -  a ]--;         if (occ[s[i + p.size()] -  a ] == -1) err++;       }     }     cout << sol << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1005;   int g[maxn][maxn];   int vis[maxn][maxn];   int cnt[maxn][maxn];   struct node {     int x, y;   } que[maxn * maxn];   int dx[10] = {0, 1, 0, -1, 0};   int dy[10] = {0, 0, 1, 0, -1};   map<char, int> p;   void init() {     p[ U ] = 2;     p[ D ] = 4;     p[ L ] = 3;     p[ R ] = 1;   }   void add(int x1, int y1, int x2, int y2) {     int pp = 0;     if (x1 == x2) {       if (y1 < y2)         pp = 2;       else         pp = 4;     } else {       if (x1 < x2)         pp = 1;       else         pp = 3;     }     g[x2][y2] = pp;     while (x1 != x2 || y1 != y2) {       g[x1][y1] = pp;       x1 += dx[pp];       y1 += dy[pp];     }   }   int main() {     init();     int n, m;     scanf( %d%d , &m, &n);     int x1, y1, x2, y2;     while (m--) {       scanf( %d%d%d%d , &x1, &y1, &x2, &y2);       add(x1, y1, x2, y2);     }     long long t;     char c;     int Q, x, y, vx, vy;     scanf( %d , &Q);     for (int q = 1; q <= Q; q++) {       scanf( %d %d %c %lld , &x, &y, &c, &t);       vx = dx[p[c]];       vy = dy[p[c]];       long long t0 = 0;       while (x >= 0 && x <= n && y >= 0 && y <= n && t > 0) {         if (g[x][y]) {           vx = dx[g[x][y]];           vy = dy[g[x][y]];         }         if (g[x][y] && vis[x][y] == q) {           long long t1 = cnt[x][y];           long long mod = t0 - t1;           t %= mod;           x = que[t1 + t].x;           y = que[t1 + t].y;           break;         }         vis[x][y] = q;         cnt[x][y] = t0;         que[t0].x = x;         que[t0++].y = y;         x += vx;         y += vy;         t--;       }       if (x < 0) x = 0;       if (x > n) x = n;       if (y < 0) y = 0;       if (y > n) y = n;       printf( %d %d n , x, y);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<long long, int> mark;   int32_t main() {     int n;     scanf( %d , &n);     set<long long> s;     for (int i = 0; i < n; i++) {       int x;       scanf( %d , &x);       mark[x]++;       s.insert(x);     }     int p = 0;     long long MX = 0;     while (s.size()) {       long long u = *s.begin();       ((MX) = max((MX), (u)));       mark[u + 1] += mark[u] / 2;       if (mark[u + 1]) s.insert(u + 1);       if (mark[u] % 2) p--;       s.erase(s.begin());     }     return cout << MX + p + 1 << endl, 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int c[5005];   int r[5005];   int p[5005];   int main() {     memset(p, -1, sizeof p);     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> c[i];       if (p[c[i]] == -1) {         p[c[i]] = i;       }     }     for (int i = 0; i < n; ++i) {       int o[5005];       memset(o, 0, sizeof o);       o[c[i]] = 1;       r[c[i]] += 1;       int mO = c[i];       for (int j = i + 1; j < n; ++j) {         o[c[j]] += 1;         if (o[c[j]] > o[mO] || (o[c[j]] == o[mO] && c[j] < mO)) {           mO = c[j];         }         r[mO] += 1;       }     }     for (int i = 1; i <= n; ++i) cout << r[i] <<    ;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int buffer_size = 1e5 + 5;   char buf[buffer_size], *S, *T;   bool flag_EOF;   inline char read_char() {     if (S == T) T = (S = buf) + fread(buf, 1, buffer_size, stdin);     return S != T ? *(S++) : EOF;   }   inline int read_int() {     bool flag = false;     char c = read_char();     while (c <  0  || c >  9 ) {       if (c == EOF) {         flag_EOF = true;         return 0;       }       flag = (c ==  -  ? true : flag);       c = read_char();     }     int x = 0;     while (c >=  0  && c <=  9 ) {       x = x * 10 + (c ^ 48);       c = read_char();     }     return flag ? -x : x;   }   const int max_cnt = 1e3 + 5;   int l[max_cnt], p[max_cnt];   bool mark[max_cnt];   vector<int> a[2], b[2];   const int max_size = 5e5 + 5;   bitset<max_size> f[2];   bool dp[max_size];   int val[max_size];   inline int cmp1(int x, int y) { return abs(x) < abs(y); }   inline int cmp2(int x, int y) { return abs(x) > abs(y); }   int main() {     int t;     scanf( %d , &t);     while (t--) {       int h, v;       scanf( %d , &h);       for (int i = 1; i <= h; ++i) scanf( %d , l + i);       scanf( %d , &v);       for (int i = 1; i <= v; ++i) scanf( %d , p + i);       if (h != v) {         printf( No n );         continue;       }       int sum1 = 0, sum2 = 0;       for (int i = 1; i <= h; ++i) sum1 += l[i];       for (int i = 1; i <= v; ++i) sum2 += p[i];       if ((sum1 & 1) || (sum2 & 1)) {         printf( No n );         continue;       }       random_shuffle(l + 1, l + h + 1);       for (int i = 0; i <= sum1; ++i) f[0][i] = 0;       f[0][0] = 1;       int pos1 = -1;       for (int i = 1; i <= h; ++i) {         int x = (i & 1) ^ 1, y = i & 1;         f[y] = f[x] | (f[x] << l[i]);         if (f[y][sum1 >> 1]) {           pos1 = i;           break;         }       }       if (pos1 == -1) {         printf( No n );         continue;       }       random_shuffle(p + 1, p + v + 1);       for (int i = 0; i <= sum2; ++i) f[0][i] = 0;       f[0][0] = 1;       int pos2 = -1;       for (int i = 1; i <= v; ++i) {         int x = (i & 1) ^ 1, y = i & 1;         f[y] = f[x] | (f[x] << p[i]);         if (f[y][sum2 >> 1]) {           pos2 = i;           break;         }       }       if (pos2 == -1) {         printf( No n );         continue;       }       a[0].clear(), a[1].clear();       int tot1 = sum1 >> 1;       for (int i = 1; i <= h; ++i) mark[i] = false;       for (int i = pos1 + 1; i <= h; ++i) {         a[0].push_back(l[i]);         mark[i] = true;         tot1 -= l[i];       }       for (int i = 0; i <= tot1; ++i) dp[i] = false;       dp[0] = true;       for (int i = 1; i <= pos1; ++i)         for (int j = tot1 - l[i]; j >= 0; --j)           if (dp[j] && !dp[j + l[i]]) {             dp[j + l[i]] = true;             val[j + l[i]] = i;           }       int now = tot1;       while (now) {         a[0].push_back(l[val[now]]);         mark[val[now]] = true;         now -= l[val[now]];       }       int cnt1 = 0;       for (int i = 1; i <= h; ++i)         if (!mark[i]) {           a[1].push_back(l[i]);           ++cnt1;         }       b[0].clear(), b[1].clear();       int tot2 = sum2 >> 1;       for (int i = 1; i <= v; ++i) mark[i] = false;       for (int i = pos2 + 1; i <= v; ++i) {         b[0].push_back(p[i]);         mark[i] = true;         tot2 -= p[i];       }       for (int i = 0; i <= tot2; ++i) dp[i] = false;       dp[0] = true;       for (int i = 1; i <= pos2; ++i)         for (int j = tot2 - p[i]; j >= 0; --j)           if (dp[j] && !dp[j + p[i]]) {             dp[j + p[i]] = true;             val[j + p[i]] = i;           }       now = tot2;       while (now) {         b[0].push_back(p[val[now]]);         mark[val[now]] = true;         now -= p[val[now]];       }       int cnt2 = 0;       for (int i = 1; i <= v; ++i)         if (!mark[i]) {           b[1].push_back(p[i]);           ++cnt2;         }       printf( Yes n );       int x = 0, y = 0;       if (a[0].size() < a[1].size()) swap(a[0], a[1]);       if (b[0].size() > b[1].size()) swap(b[0], b[1]);       sort(a[1].begin(), a[1].end());       sort(b[1].begin(), b[1].end(), greater<int>());       while (a[1].size()) {         x += a[1].back();         printf( %d %d n , x, y);         y += b[1].back();         printf( %d %d n , x, y);         a[1].pop_back();         b[1].pop_back();       }       while (b[1].size()) {         x -= a[0].back();         printf( %d %d n , x, y);         y += b[1].back();         printf( %d %d n , x, y);         a[0].pop_back();         b[1].pop_back();       }       sort(a[0].begin(), a[0].end());       sort(b[0].begin(), b[0].end(), greater<int>());       while (b[0].size()) {         x -= a[0].back();         printf( %d %d n , x, y);         y -= b[0].back();         printf( %d %d n , x, y);         a[0].pop_back();         b[0].pop_back();       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x60000000;   const int MINF = -1000000000;   const int mod = 99999997;   const long long cons = 100000;   const double pi = 3.141592653589793;   int lim;   int p[100005][20];   int depth[100005];   pair<int, int> dmax[100005][3];   int up[100005][20];   int down[100005][20];   vector<vector<int> > adj;   int n;   void dfs(int u, int prev) {     if (prev != -1) {       for (int i = 2; i >= 0; i--) {         if (dmax[prev][i].second == u) continue;         up[u][0] = dmax[prev][i].first + 1;         down[u][0] = dmax[prev][i].first;         break;       }     }     p[u][0] = prev;     for (int j = 0; j <= lim; j++) {       if (p[u][j] == -1) continue;       p[u][j + 1] = p[p[u][j]][j];       up[u][j + 1] = max(up[p[u][j]][j] + (1 << j), up[u][j]);       down[u][j + 1] = max(down[u][j] + (1 << j), down[p[u][j]][j]);     }     for (auto v : adj[u]) {       if (v == prev) continue;       depth[v] = depth[u] + 1;       dfs(v, u);     }   }   int getPar(int u, int dist) {     if (dist < 0) return -1;     for (int i = 0; i < lim; i++)       if (dist & (1 << i)) u = p[u][i];     return u;   }   int getLca(int u, int v) {     if (depth[u] > depth[v]) swap(u, v);     v = getPar(v, depth[v] - depth[u]);     if (u == v) return u;     for (int i = lim; i >= 0; i--) {       if (p[u][i] == p[v][i]) continue;       u = p[u][i];       v = p[v][i];     }     return p[u][0];   }   int getDist(int u, int v) {     int lca = getLca(u, v);     return depth[u] + depth[v] - 2 * depth[lca];   }   void buildDmax(int u, int prev) {     for (auto v : adj[u]) {       if (v == prev) continue;       buildDmax(v, u);       if (dmax[u][0].first < dmax[v][2].first + 1) {         dmax[u][0].first = dmax[v][2].first + 1;         dmax[u][0].second = v;       }       sort(dmax[u], dmax[u] + 3);     }   }   void build() {     for (lim = 0; (1 << lim) < n; lim++)       ;     buildDmax(1, -1);     dfs(1, -1);   }   int getUMax(int u, int d) {     if (d < 0) return -1e7;     int ret = 0;     int added = 0;     for (int i = 0; i < lim; i++) {       if (d & (1 << i)) {         ret = max(ret, up[u][i] + added);         added |= (1 << i);         u = p[u][i];       }     }     return ret;   }   int getDMax(int u, int d) {     if (d < 0) return -1e7;     int ret = 0;     int added = d;     for (int i = 0; i < lim; i++) {       if (d & (1 << i)) {         added ^= (1 << i);         ret = max(ret, down[u][i] + added + 1);         u = p[u][i];       }     }     return ret;   }   int query(int u, int v) {     if (depth[u] < depth[v]) swap(u, v);     int lca = getLca(u, v);     int d = getDist(u, v);     int center = getPar(u, (d - 1) / 2);     int vld = getDist(v, lca);     int ans = dmax[u][2].first;     if (lca != v) ans = max(ans, dmax[v][2].first);     ans = max(ans, getUMax(v, vld - 1));     ans = max(ans, getUMax(lca, depth[lca]) + vld);     ans = max(ans, getUMax(u, depth[u] - depth[center]));     ans = max(ans, getDMax(center, depth[center] - depth[lca] - 1) + vld);     for (int i = 2; i >= 0; i--) {       if (dmax[lca][i].second == getPar(v, depth[v] - depth[lca] - 1) ||           dmax[lca][i].second == getPar(u, depth[u] - depth[lca] - 1))         continue;       ans = max(ans, dmax[lca][i].first + vld);       break;     }     return ans;   }   int main() {     scanf( %d , &n);     adj.resize(n + 1);     for (int i = 0; i < n - 1; i++) {       int u, v;       scanf( %d%d , &u, &v);       adj[u].push_back(v);       adj[v].push_back(u);     }     build();     int q;     scanf( %d , &q);     while (q--) {       int u, v;       scanf( %d%d , &u, &v);       printf( %d n , query(u, v));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int sum = 0, pos = 0;     string s;     cin >> s;     for (int i = 0; i < s.length(); i++)       sum += min(abs(pos - (s[i] -  a )), 26 - abs(pos - (s[i] -  a ))),           pos = s[i] -  a ;     cout << sum;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[30], col[100100] = {};   int main() {     string s;     cin >> s;     for (int i = 0; i < 26; i++) a[i] = -1;     for (int i = 0; i < s.size(); i++) a[s[i] -  a ] = i;     int p = 25;     for (int i = 0;;) {       while (p >= 0 && a[p] < i) p--;       if (p < 0) break;       int col = 0;       while (i <= a[p]) {         if (s[i] -  a  == p) col++;         i++;       }       for (int j = 0; j < col; j++) cout << s[a[p]];       p--;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     std::ios::sync_with_stdio(false);     int n, c = 0, fl, i, a, b;     int deg[100005] = {0};     vector<int> v;     cin >> n;     for (i = 0; i < n - 1; i++) {       cin >> a >> b;       deg[a]++;       deg[b]++;     }     for (i = 1; i < n + 1; i++) {       if (deg[i] > 2) {         c++;         fl = i;       }       if (deg[i] == 1) {         v.push_back(i);       }     }     if (c > 1) {       cout <<  No  << endl;       return 0;     }     cout <<  Yes  << endl;     if (n == 2) {       cout << 1 << endl;       cout << 1 <<     << 2 << endl;     } else {       if (c == 0) {         cout << 1 << endl;         cout << v[0] <<     << v[1] << endl;       } else {         cout << v.size() << endl;         for (i = 0; i < v.size(); i++) {           cout << v[i] <<     << fl << endl;         }       }     }   }


#include <bits/stdc++.h>   const long long INF = 1e18;   const long long N = 2e5 + 50;   const long long MOD = 1e9 + 7;   using namespace std;   vector<long long> prime, graph[N];   long long n, k, a[N], m, z;   long long POWER(long long base, long long expo) {     long long ret = 1;     while (expo) {       if (expo & 1) ret = ret * base;       expo >>= 1;       base = base * base;     }     return ret;   }   void SOE(long long n) {     bool isPrime[n + 1];     memset(isPrime, true, sizeof(isPrime));     for (long long p = 2; p <= n; p++) {       if (isPrime[p]) {         for (long long i = p * p; i <= n; i += p) isPrime[i] = false;       }     }     for (long long i = 2; i <= n; i++) {       if (isPrime[i]) prime.push_back(i);     }   }   int main() {     long long test_case = 1;     cin >> test_case;     while (test_case--) {       cin >> n >> k >> z;       for (long long i = 0; i < n; i++) cin >> a[i];       vector<long long> v;       long long ans = 0, m = -1;       for (long long i = 0; i < n; i++) {         if (m < k) {           ans += a[i];           m++;         }       }       for (long long i = 1; i <= k; i++) {         for (long long c = 0; c <= z; c++) {           long long l = k, sum = 0, j = i, times = 0;           while (j < l && times < c) {             if (j == i) {               j--;               sum += a[j];               sum -= a[l];               l--;               times++;             }             if (j < l) {               j++;               sum += a[j];               sum -= a[l];               l--;             }           }           v.push_back(sum);         }       }       sort(v.begin(), v.end());       long long s = 0;       if (v.size() > 0) {         s = v[v.size() - 1];       }       cout << max(ans, ans + s) << endl;     }     return 0;   }


#include <bits/stdc++.h>   int main() {     long long int tcase, nc, np, o, connectable, done;     std::vector<long long int> O;     std::cin >> tcase;     for (int i = 0; i < tcase; i++) {       connectable = 1;       done = 1;       o = 0;       std::cin >> nc >> np;       while (nc > done) {         o++;         done += connectable;         if (done <= np)           connectable = done;         else if (done > np && done < nc) {           connectable = np;           if ((nc - done) % connectable != 0)             o += ((nc - done) / connectable) + 1;           else             o += ((nc - done) / connectable);           break;         }       }       O.push_back(o);     }     for (int i = 0; i < tcase; i++) {       std::cout << O[i] << std::endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 150005;   int n, m;   int a, b;   vector<int> g[N];   bool pos[N];   vector<int> dsu;   bool rez;   bool proveri() {     for (int u = 0; u < dsu.size(); u++)       if (g[dsu[u]].size() != dsu.size() - 1) return false;     return true;   }   void dfs(int cvor) {     pos[cvor] = true;     dsu.push_back(cvor);     for (int v = 0; v < g[cvor].size(); v++)       if (!pos[g[cvor][v]]) dfs(g[cvor][v]);   }   void DFS() {     for (int u = 1; u <= n && !rez; u++) {       if (!pos[u]) {         dsu.clear();         dfs(u);         if (!proveri()) rez = true;       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> m;     for (int i = 1; i <= m; i++) {       cin >> a >> b;       g[a].push_back(b);       g[b].push_back(a);     }     DFS();     if (rez == true)       cout <<  NO  << endl;     else       cout <<  YES  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int INP() {     int n, flag;     char c;     n = 0;     flag = 1;     c = getchar();     while (c < 33) {       c = getchar();     }     if (c ==  - ) {       flag = -1;       c = getchar();     }     while (c > 32) {       n = n * 10 + c -  0 ;       c = getchar();     }     n = n * flag;     return n;   }   int GCD(int a, int b) { return (a % b == 0 ? b : GCD(b, a % b)); }   int main() {     int n, i, idx, ans, stop;     string s, s1, mx;     vector<string> v;     cin >> s;     n = s.size();     mx =  0 ;     for (i = 0; i < n; i++) {       s1.push_back(s[i]);       if (s[i + 1] !=  0 ) {         v.push_back(s1);         if (s1.size() > mx.size()) {           mx = s1;           idx = v.size();         } else if (s1.size() == mx.size() && s1 > mx) {           mx = s1;           idx = v.size();         }         s1.clear();       }     }     s1.clear();     for (i = 0; i < idx; i++) {       if (s1.size() > v[i].size()) {         stop = min(stop, i - 1);       } else if (s1.size() == v[i].size() && s1 >= v[i]) {         stop = min(stop, i - 1);       }       if (s1.size() < v[i].size()) {         stop = INT_MAX;       } else if (s1.size() == v[i].size() && v[i] > s1) {         stop = INT_MAX;       }       s1 += v[i];       if (s1.size() > mx.size()) {         ans = v.size() - min(i, stop);         break;       } else if (s1.size() == mx.size() && s1 >= mx) {         ans = v.size() - min(i, stop);         break;       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T, class U>   struct LazySegTree {     int n;     vector<T> dat;     vector<U> lazy;     T e;     U upe;     static void biop_assign(T &u, const T &a, const T &b) {       int m = a[0].size();       for (int i = (int)(0); i < (int)(2); i++) {         for (int j = (int)(0); j < (int)(m); j++) {           u[i][j] = a[i][b[i][j]];         }       }     }     static void upop(T &a, U b) {       if (b == 1) {         swap(a[0], a[1]);       }     }     static U upbiop(U a, U b) { return a ^ b; }     LazySegTree(int n_, T e, U upe) : e(e), upe(upe) {       n = 1;       while (n < n_) n *= 2;       dat = vector<T>(2 * n - 1, e);       lazy = vector<U>(2 * n - 1, upe);     }     void lazy_evaluate_node(int k) {       upop(dat[k], lazy[k]);       if (k < n - 1) {         lazy[2 * k + 1] = upbiop(lazy[2 * k + 1], lazy[k]);         lazy[2 * k + 2] = upbiop(lazy[2 * k + 2], lazy[k]);       }       lazy[k] = upe;     }     void update_node(int k) {       assert(0 <= k && k < n - 1);       biop_assign(dat[k], dat[2 * k + 1], dat[2 * k + 2]);     }     void update_sub(int a, int b, U v, int k, int l, int r) {       lazy_evaluate_node(k);       if (r <= a || b <= l) return;       if (a <= l && r <= b) {         lazy[k] = upbiop(lazy[k], v);         lazy_evaluate_node(k);         return;       }       int mid = (l + r) / 2;       update_sub(a, b, v, 2 * k + 1, l, mid);       update_sub(a, b, v, 2 * k + 2, mid, r);       update_node(k);     }     void update(int a, int b, U v) { update_sub(a, b, v, 0, 0, n); }     void update_single(int a, T val, int k, int l, int r) {       lazy_evaluate_node(k);       if (r <= a || a + 1 <= l) return;       if (a <= l && r <= a + 1) {         dat[k] = val;         return;       }       int mid = (l + r) / 2;       update_single(a, val, 2 * k + 1, l, mid);       update_single(a, val, 2 * k + 2, mid, r);       update_node(k);     }     void update_single(int a, T val) { update_single(a, val, 0, 0, n); }     int query_sub(int a, int b, int k, int l, int r, int x) {       lazy_evaluate_node(k);       if (r <= a || b <= l) return x;       if (a <= l && r <= b) return dat[k][0][x];       int mid = (l + r) / 2;       int vr = query_sub(a, b, 2 * k + 2, mid, r, x);       int vl = query_sub(a, b, 2 * k + 1, l, mid, vr);       update_node(k);       return vl;     }     int query(int a, int b, int x) { return query_sub(a, b, 0, 0, n, x); }   };   int main(void) {     int n, m, q;     scanf( %d%d%d , &n, &m, &q);     vector<long long int> a(n);     for (int i = (int)(0); i < (int)(n); i++) {       scanf( %lld , &a[i]);       a[i] %= 2;     }     vector<vector<int> > id(2, vector<int>(m + 1, 0));     for (int i = (int)(0); i < (int)(m + 1); i++) {       id[0][i] = id[1][i] = i;     }     LazySegTree<vector<vector<int> >, int> lst(n, id, 0);     for (int i = (int)(0); i < (int)(n); i++) {       vector<vector<int> > val(2, vector<int>(m + 1, 0));       for (int j = (int)(0); j < (int)(m); j++) {         val[0][j] = val[1][j] = j + 1;       }       val[0][m] = m;       val[1][m] = 0;       if (a[i] == 1) {         swap(val[0], val[1]);       }       lst.dat[lst.n - 1 + i] = val;     }     for (int i = lst.n - 2; i >= 0; --i) {       lst.update_node(i);     }     for (int i = (int)(0); i < (int)(q); i++) {       int ty;       scanf( %d , &ty);       if (ty == 1) {         int l, r;         long long int d;         scanf( %d%d%lld , &l, &r, &d);         l--;         d %= 2;         lst.update(l, r, d);       } else {         int l, r;         scanf( %d%d , &l, &r);         l--;         int res = lst.query(l, r, m);         printf( %d n , res ? 1 : 2);       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     map<pair<long long, long long>, long long> m;     vector<long long> p1, p2;     long long n, s, i, j, k, a, u, v, w, l, k1, k2;     cin >> n;     while (n--) {       cin >> a;       if (a == 1) {         cin >> u >> v >> w;         j = u;         k1 = 0, k2 = 0;         p1.push_back(j);         while (j != 1) {           j = j / 2;           p1.push_back(j);         }         j = v;         p2.push_back(j);         while (j != 1) {           j = j / 2;           p2.push_back(j);         }         for (i = 0; i < p1.size(); i++) {           int t = 1;           for (j = 0; j < p2.size(); j++) {             if (p1[i] == p2[j]) {               k1 = i;               k2 = j;               t = 0;               break;             }           }           if (t == 0) break;         }         for (i = 0; i < k1; i++) {           m[make_pair(p1[i], p1[i + 1])] += w;         }         for (i = 0; i < k2; i++) {           m[make_pair(p2[i], p2[i + 1])] += w;         }         p1.clear();         p2.clear();       } else {         s = 0;         cin >> u >> v;         j = u;         k1 = k2 = 0;         p1.push_back(j);         while (j != 1) {           j = j / 2;           p1.push_back(j);         }         j = v;         p2.push_back(j);         while (j != 1) {           j = j / 2;           p2.push_back(j);         }         for (i = 0; i < p1.size(); i++) {           int t = 1;           for (j = 0; j < p2.size(); j++) {             if (p1[i] == p2[j]) {               k1 = i;               k2 = j;               t = 0;               break;             }           }           if (t == 0) break;         }         for (i = 0; i < k1; i++) {           s += m[make_pair(p1[i], p1[i + 1])];         }         for (i = 0; i < k2; i++) {           s += m[make_pair(p2[i], p2[i + 1])];         }         p1.clear();         p2.clear();         cout << s << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     vector<long long> a(n + 1), b(n + 1);     for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];     long long k;     cin >> k;     long long x;     for (int i = 1; i <= n; i++)       if (k >= a[i] && k <= b[i]) {         x = i;         break;       }     cout << n - x + 1;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(0);     solve();   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 100 * 30;   int main() {     string A, B;     cin >> A >> B;     vector<vector<int> > v(26, vector<int>(26, INF));     for (int i = 0; i < 26; ++i) v[i][i] = 0;     int N;     cin >> N;     while (N--) {       char a, b;       int cost;       cin >> a >> b >> cost;       int ia = a -  a , ib = b -  a ;       v[ia][ib] = min(v[ia][ib], cost);     }     for (int k = 0; k < 26; ++k)       for (int i = 0; i < 26; ++i)         for (int j = 0; j < 26; ++j) v[i][j] = min(v[i][j], v[i][k] + v[k][j]);     if (A.size() != B.size()) {       cout << -1 << endl;     } else {       unsigned ans = 0;       bool fail = false;       string res =   ;       for (int i = 0; i < A.size(); ++i) {         if (A[i] == B[i]) {           res += A[i];           continue;         }         int ia = A[i] -  a , ib = B[i] -  a ;         char to_ch = 0;         int cost = INF * 10;         for (int j = 0; j < 26; ++j) {           if (v[ia][j] < INF && v[ib][j] < INF) {             if (v[ia][j] + v[ib][j] < cost) {               cost = v[ia][j] + v[ib][j];               to_ch = j +  a ;             }           }         }         if (to_ch == 0) {           fail = true;           break;         }         ans += cost;         res += to_ch;       }       if (fail)         cout << -1 << endl;       else {         cout << ans << endl;         cout << res << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int sti(const string &s) {     int x;     stringstream ss(s);     ss >> x;     return x;   }   string its(const int &x) {     string s;     stringstream ss(s);     ss << x;     s = ss.str();   }   void sscan(string &str) {     str =   ;     char str_c;     scanf( %c , &str_c);     while (!(str_c ==   n  || str_c ==    )) {       str += str_c;       scanf( %c , &str_c);     }   }   void sprint(const string &str) {     int length = str.length();     for (int i = 0; i < length; ++i) printf( %c , str[i]);   }   uint64_t fact(int x) {     if (x < 2) return 1;     return fact(x - 1) * x;   }   uint64_t C(int x, int y) {     uint64_t c = 1;     for (int i = 0; i < y; ++i) {       c *= x - i;       c /= i + 1;     }     return c;   }   int main() {     int n, m, t;     scanf( %d %d %d , &n, &m, &t);     uint64_t res = 0;     for (int i = 4; i <= n && i < t; ++i) {       if (t - i > m) continue;       res += C(n, i) * C(m, t - i);     }     printf( %I64u , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 7;   set<int, greater<int> > s;   int main() {     int q;     scanf( %d , &q);     while (q--) {       s.clear();       int n;       scanf( %d , &n);       for (int i = 1; i <= n; i++) {         int x;         scanf( %d , &x);         s.insert(x);       }       int x = *s.begin();       int ans = 0;       if (s.find(x / 2) != s.end() && s.find(x / 3) != s.end() &&           s.find(x / 5) != s.end())         if (x % 2 == 0 && x % 3 == 0 && x % 5 == 0)           ans = max(ans, x / 2 + x / 3 + x / 5);       int cnt = 0;       int sum = 0;       while (!s.empty() && cnt < 3) {         int now = *s.begin();         sum += now;         cnt++;         s.erase(now);         for (int i = 2; i <= sqrt(now) + 1; i++) {           if (now % i == 0) {             s.erase(i);             if (s.find(i) != s.end()) s.erase(i);             if (s.find(now / i) != s.end()) s.erase(now / i);           }         }       }       printf( %d n , max(ans, sum));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n;   bool h[100100][40], u[100100][40];   bool big(long long a, long long b) {     long long ans = 1;     for (long long i = 0; i < b; ++i) {       ans *= a;       if (ans >= n) return 1;     }     return 0;   }   bool win(long long a, long long b) {     if (b == 1 && a > 100100 - 10) return (n - a) % 2 == 0;     if (u[a][b]) return h[a][b];     u[a][b] = 1;     if (big(a, b)) return h[a][b] = 1;     if (!win(a + 1, b) || !win(a, b + 1)) return h[a][b] = 1;     return h[a][b] = 0;   }   bool miss(long long a, long long b) {     for (long long i = b; i < 40; ++i)       if (!win(a + 1, i)) {         if ((i - b) % 2 == 0)           cout <<  Masha ;         else           cout <<  Stas ;         return 0;       }     cout <<  Missing ;     return 1;   }   int main() {     long long a, b;     cin >> a >> b >> n;     if ((a > 1))       if (win(a, b))         cout <<  Masha ;       else         cout <<  Stas ;     if (a == 1) miss(a, b);     return 0;   }


#include <iostream>   #include <cstring>      using namespace std;      const int MAXN = 100 + 10;      int cnt[MAXN];      int main() {     int t;     cin >> t;     while (t--) {       int n, input, ans = 0;       cin >> n;       memset(cnt, 0, sizeof(cnt));       for (int i = 0; i < n; ++i) {         cin >> input;         ++cnt[input];         ans = max(ans, cnt[input]);       }       cout << ans << endl;     }     // system( pause );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e4;   int n, aa[N + 2], fib[N + 2], con[N + 2], m;   void compress() {     vector<int> v;     for (int i = 1; i <= n; i++) v.push_back(aa[i]);     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());     map<int, int> mp;     for (int i = 0; i < v.size(); i++) {       mp[v[i]] = i + 1;       con[i + 1] = v[i] % m;     }     for (int i = 1; i <= n; i++) aa[i] = mp[aa[i]];   }   bool vs[N + 2];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> aa[i];     compress();     fib[1] = 1;     fib[2] = 1;     for (int i = 3; i <= 30000; i++) fib[i] = (fib[i - 1] + fib[i - 2]) % m;     int q;     cin >> q;     while (q--) {       int l, r;       cin >> l >> r;       for (int i = l; i <= r; i++) vs[aa[i]] = 1;       long long ans = 0;       int cnt = 1;       for (int i = 1; i <= 30000; i++) {         if (vs[i]) {           ans += con[i] * fib[cnt++];           vs[i] = 0;         }       }       cout << ans % m <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma G++ optimize( Ofast,no-stack-protector )   #pragma G++ optimize( -O3,-O2,-O1 )   #pragma G++ target(         sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native )   #pragma G++ optimize( unroll-loops )   using namespace std;   long long binpow(long long a, long long n, long long mod) {     long long ans = 1;     while (n) {       if (n & 1) ans = (ans * a) % mod;       a = (a * a) % mod;       n >>= 1;     }     return ans;   }   long long binpow(long long a, long long n) {     long long ans = 1;     while (n) {       if (n & 1) ans *= a;       a *= a;       n >>= 1;     }     return ans;   }   vector<long long> pr;   void PrDoX(long long x) {     if (pr.size() > 0) return;     vector<long long> p(x + 1);     for (long long i = 2; i <= x; i++) {       if (p[i] == 0) {         pr.push_back(i);         for (long long j = i + i; j <= x; j += i) p[j] = 1;       }     }   }   vector<long long> fact(long long x) {     PrDoX(sqrt(1e9));     vector<long long> ans;     long long i = 0, c = sqrt(x), n = pr.size();     while (x > 1 and pr[i] <= c and i < n) {       if (x % pr[i] == 0) {         x /= pr[i];         ans.push_back(pr[i]);         c = sqrt(x);       } else         i++;     }     if (x != 1) ans.push_back(x);     return ans;   }   int dp[501][26][26], cost[501][26][26];   pair<int, int> ans[501][26][26];   int main() {     cin.tie(0);     cout.tie(0);     ios::sync_with_stdio(0);     for (int i = 1; i < 501; i++)       for (int j = 0; j < 26; j++)         for (int l = 0; l < 26; l++) dp[i][j][l] = 1e9;     int n, m;     cin >> n >> m;     vector<vector<int> > v(n, vector<int>(m));     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         char c;         cin >> c;         v[i][j] = c -  a ;       }     }     for (int i = 0; i < n; i++) {       for (int A = 0; A < 26; A++) {         for (int B = 0; B < 26; B++) {           if (A == B) continue;           for (int j = 0; j < m; j++)             cost[i][A][B] +=                 ((A != v[i][j]) * (j % 2 == 0) + (B != v[i][j]) * (j % 2 == 1));         }       }     }     for (int i = 0; i < 26; i++) {       for (int j = 0; j < 26; j++) dp[1][i][j] = cost[0][i][j];     }     for (int i = 1; i <= n; i++) {       for (int A = 0; A < 26; A++) {         for (int B = 0; B < 26; B++) {           if (A == B) continue;           for (int A2 = 0; A2 < 26; A2++) {             if (A2 == A) continue;             for (int B2 = 0; B2 < 26; B2++) {               if (B2 == B or A2 == B2) continue;               if (dp[i - 1][A2][B2] + cost[i - 1][A][B] < dp[i][A][B]) {                 dp[i][A][B] = dp[i - 1][A2][B2] + cost[i - 1][A][B];                 ans[i][A][B] = {A2, B2};               }             }           }         }       }     }     int mn = 1e9;     pair<int, int> x = {0, 0};     for (int i = 0; i < 26; i++) {       for (int j = 0; j < 26; j++) {         if (i != j and dp[n][i][j] < mn) mn = dp[n][i][j], x = {i, j};       }     }     cout << mn <<   n ;     vector<pair<int, int> > Ans;     for (int i = n; i > 0; i--) {       Ans.push_back(x);       x = ans[i][x.first][x.second];     }     for (int i = n - 1; i >= 0; i--) {       for (int j = 0; j < m; j++) {         if (j % 2 == 0)           cout << char(Ans[i].first +  a );         else           cout << char(Ans[i].second +  a );       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 4, M = 1e5 + 10, Mo = 1e9 + 7;   long long n, m, nxt[M], p, lena, lenb, lens;   string aa, bb, s;   struct Matrix {     long long n, a[N][N];     Matrix(long long _n = 4) { n = _n, memset(a, 0, sizeof(a)); }     void operator~() {       for (long long i = 0; i < 4; i++) a[i][i] = 1;     }     Matrix operator+(const Matrix &b) const {       Matrix ret(4);       for (long long i = 0; i < 4; i++)         for (long long j = 0; j < 4; j++)           ret.a[i][j] = (a[i][j] + b.a[i][j]) % Mo;       return ret;     }     Matrix operator-(const Matrix &b) const {       Matrix ret(4);       for (long long i = 0; i < 4; i++)         for (long long j = 0; j < 4; j++)           ret.a[i][j] = (a[i][j] - b.a[i][j] + Mo) % Mo;       return ret;     }     Matrix operator*(const Matrix &b) const {       Matrix ret(4);       for (long long i = 0; i < 4; i++)         for (long long j = 0; j < 4; j++)           for (long long k = 0; k < 4; k++)             ret.a[i][k] =                 (ret.a[i][k] + 1LL * a[i][j] * b.a[j][k] % Mo + Mo) % Mo;       return ret;     }     Matrix operator^(const long long &b) const {       Matrix ret, x, m1, m2;       for (long long i = 0; i < 4; i++)         for (long long j = 0; j < 4; j++)           if (i == j)             ret.a[i][j] = 1;           else             ret.a[i][j] = 0;       for (long long i = 0; i < 4; i++)         for (long long j = 0; j < 4; j++) m1.a[i][j] = m2.a[i][j] = 0;       m1.a[0][0] = m1.a[0][1] = m1.a[1][0] = m1.a[2][2] = m1.a[2][3] =           m1.a[3][2] = m2.a[0][0] = m2.a[0][1] = m2.a[1][0] = m2.a[2][0] =               m2.a[3][2] = 1;       x = m2 * m1;       for (long long B = b >> 1; B; B >>= 1, x = x * x) {         if (B & 1) ret = ret * x;       }       if (b & 1) ret = m1 * ret;       return ret;     }   } a, b, c, m1, m2;   inline long long read() {     long long x = 0, k = 1;     char c = getchar();     for (; c < 48 || c > 57; c = getchar()) k ^= (c ==  - );     for (; c >= 48 && c <= 57; c = getchar()) x = x * 10 + (c ^ 48);     return k ? x : -x;   }   long long kmp1(long long l, long long r) {     long long ans = 0;     for (long long i = 0, p = 0; i < r; i++) {       for (; (~p) && s[p] != aa[i]; p = nxt[p])         ;       p = p + 1;       if (p == lens && i >= l && i <= r - 1)         ans = ans + 1, p = nxt[p];       else if (p == lens)         p = nxt[p];     }     return ans;   }   long long kmp2(long long l, long long r) {     long long ans = 0;     for (long long i = 0, p = 0; i < r; i++) {       for (; (~p) && s[p] != bb[i]; p = nxt[p])         ;       p = p + 1;       if (p == lens && i >= l && i <= r - 1)         ans = ans + 1, p = nxt[p];       else if (p == lens)         p = nxt[p];     }     return ans;   }   long long KMP(const string &b, long long l, long long r) {     long long ans = 0;     long long lenb = b.length();     for (long long i = 0, p = 0; i < lenb; i++) {       for (; (~p) && s[p] != b[i]; p = nxt[p])         ;       p = p + 1;       if (p == lens && i >= l && i <= r - 1)         ans = ans + 1, p = nxt[p];       else if (p == lens)         p = nxt[p];     }     return ans;   }   signed main() {     m1.a[0][0] = m1.a[0][1] = m1.a[1][0] = m1.a[2][2] = m1.a[2][3] = m1.a[3][2] =         m2.a[0][0] = m2.a[0][1] = m2.a[1][0] = m2.a[2][0] = m2.a[3][2] = 1;     std::ios::sync_with_stdio(false);     std::cin >> n;     long long T;     std::cin >> T;     for (long long x; T--;) {       std::cin >> s;       aa =  a , bb =  b ;       lena = aa.length(), lenb = bb.length(), lens = s.length(), nxt[0] = -1,       p = 0;       ;       for (long long i = 0; i < lens; i++) {         for (p = nxt[i]; (~p) && s[p] != s[i]; p = nxt[p])           ;         nxt[i + 1] = ++p;       }       if (n == 1) {         printf( %lld n , kmp1(0, lena));       } else if (n == 2) {         printf( %lld n , kmp2(0, lenb));       } else {         for (x = 1; lena < lens && x <= n; ++x) {           aa = bb + aa;           std::swap(aa, bb);           lena = aa.length();         }         lenb = bb.length();         if (x > n)           printf( 0 n );         else if (x == n)           printf( %lld n , kmp1(0, lena));         else {           lena = aa.length(), lenb = bb.length(), lens = s.length();           long long tt = n - x - 1;           c = c ^ tt;           printf( %lld n ,                  (c.a[0][0] % Mo * kmp2(0, lenb) % Mo +                   c.a[1][0] % Mo * KMP(bb + aa, lenb, lena + lenb) % Mo +                   c.a[2][0] % Mo * KMP(aa + bb, lena, lena + lens - 1) % Mo +                   c.a[3][0] % Mo * KMP(bb + bb, lenb, lenb + lens - 1) % Mo) %                      Mo);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   int main() {     string s;     cin >> s;     int cnt0, cnt1, len = s.size();     string t(len,  0 );     cnt0 = cnt1 = 0;     for (int i = len - 1; i >= 0; --i) {       if (s[i] ==  0 )         ++cnt0;       else {         if (cnt0 != cnt1)           t[i] =  1 ;         else           ++cnt0;         ++cnt1;       }     }     cout << t <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool is(long long x) { return x != 1; }   int main() {     int t;     cin >> t;     while (t--) {       long long x, y;       cin >> x >> y;       long long z = x - y;       if (is(z))         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1 << 30;   const long long maxn = 110;   struct node {     int v, cost;   } a[1500];   int N, K, dp1[100 * 150], dp2[100 * 150];   int solve() {     memset(dp1, -0x3f3f3f3f, sizeof(dp1));     ;     memset(dp2, -0x3f3f3f3f, sizeof(dp2));     ;     dp1[1] = dp2[1] = 0;     for (int i = 1; i <= N; i++) {       if (a[i].cost >= 0) {         for (int j = 100 * 105; j >= a[i].cost; j--)           dp1[j] = max(dp1[j], dp1[j - a[i].cost] + a[i].v);       } else {         a[i].cost = -a[i].cost;         for (int j = 100 * 105; j >= a[i].cost; j--)           dp2[j] = max(dp2[j], dp2[j - a[i].cost] + a[i].v);       }     }     int ans = -1;     for (int i = 0; i <= 100 * 105; i++) {       if (dp1[i] + dp2[i] != 0 && dp1[i] + dp2[i] > ans) ans = dp1[i] + dp2[i];     }     return ans;   }   int main() {     cin >> N >> K;     for (int i = 1; i <= N; i++) cin >> a[i].v;     int b;     for (int i = 1; i <= N; i++) cin >> b, a[i].cost = a[i].v - K * b;     cout << solve() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, d;     cin >> a >> b >> c >> d;     if (a != d) {       cout << 0;     } else if (c && !a) {       cout << 0;     } else {       cout << 1;     }     return 0;   }