/*     */   #include <bits/stdc++.h>   using namespace std;      #define rng(x) x.begin(), x.end()   #define pb push_back   #define ff first   #define ss second      typedef double db;   typedef long long ll;   typedef pair<int, int> pi;   typedef pair<ll, ll> pl;   typedef vector<int> vi;   typedef vector<ll> vl;   typedef vector<vi> vvi;   typedef vector<vl> vvl;   typedef vector<bool> vb;   typedef unsigned long long int lld;      /*-----------------------------Code begins----------------------------------*/   ll mod = 1e9 + 7;   const ll INF = 1e9;      ll inv(ll v)   {    if (v<=1)     return v;    return inv(mod%v)*(mod-mod/v)%mod;   }      void solve(){       int n; cin>>n;       vvl dist(n,vl(n,INF));       for(int i=1; i<n; i++){           int u,v; cin>>u>>v;           u--; v--;           dist[u][v]=1;           dist[v][u]=1;       }       for(int i=0; i<n; i++){           dist[i][i]=0;       }       for(int k=0; k<n; k++){           for(int i=0; i<n; i++){               for(int j=0; j<n; j++){                   dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);               }           }       }       vvl prob(207,vl(207));       for(int i=0; i<=n; i++){           for(int j=0; j<=n; j++){               if(i==0){                   prob[i][j]=0;               }               else if(j==0){                   prob[i][j]=1;               }               else{                   prob[i][j] = ((prob[i-1][j] + prob[i][j-1])%mod * inv(2) )%mod;               }           }       }          ll ans=0;       for(int a=0; a<n; a++){           for(int b=a+1; b<n; b++){               for(int root=0; root<n; root++){                   int root_a = dist[a][root];                   int root_b = dist[b][root];                   int root_lca = ( root_a + root_b - dist[a][b] )/2 ;                   int lca_a = root_a - root_lca ;                   int lca_b = root_b - root_lca ;                   ans = (ans + prob[lca_a][lca_b])%mod;               }           }       }       ans = (ans * inv(n) )%mod;       cout<<ans<<  n ;   }   int main(){     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int T = 1;     //cin >> T;     while (T--){       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double EPS = (long double)1e-7;   const long double PI = acos(0) * 2;   bool isZero(const long double& x) { return abs(x) <= EPS; }   int sign(const long double& x) { return isZero(x) ? 0 : (0 < x ? 1 : -1); }   long long gcd(long long a, long long b) {     for (; b; a %= b, swap(a, b)) {     }     return abs(a);   }   pair<long long, long long> operator+(const pair<long long, long long>& a,                                        const pair<long long, long long>& b) {     return pair<long long, long long>(a.first + b.first, a.second + b.second);   }   pair<long long, long long> operator-(const pair<long long, long long>& a,                                        const pair<long long, long long>& b) {     return pair<long long, long long>(a.first - b.first, a.second - b.second);   }   pair<long long, long long> operator*(const pair<long long, long long>& a,                                        const long long& b) {     return pair<long long, long long>(a.first * b, a.second * b);   }   long long operator*(const pair<long long, long long>& a,                       const pair<long long, long long>& b) {     return a.first * b.second - b.first * a.second;   }   long long ccw(const pair<long long, long long>& a,                 const pair<long long, long long>& b,                 const pair<long long, long long>& c) {     return a * b + b * c + c * a;   }   void fg(vector<int> G[], int a, int b) {     G[a].emplace_back(b);     G[b].emplace_back(a);   }   void fg(vector<pair<int, int> > G[], int a, int b, int c) {     G[a].emplace_back(b, c);     G[b].emplace_back(a, c);   }   const int MAXN = 1000055;   int A[MAXN], B[MAXN];   bitset<MAXN> C;   int T, N;   void run() {     cin >> N;     for (int i = 1; i <= N; i++) {       cin >> A[i];       B[i] = i - A[i];     }     vector<int> V;     for (int i = 1;;) {       V.emplace_back(i);       if (C[i]) break;       C[i] = true;       i = B[i];     }     for (int v : V) C[v] = false;     V.erase(V.begin(), find(((V).begin()), ((V).end()), V.back()) + 1);     cout << ((int)(V).size()) <<   n ;     for (int v : V) cout << v <<    ;     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     for (cin >> T; T--;) run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf(1010101010);   int le_res, aa;   char naam[1000];   int le_naam;   char naam_and_res[1000];   char occ[1000];   int zvals[1000];   void z_functie(char* s, int n, int* z) {     int a(0), b(0);     for (int i(0); i < (n); i++) z[i] = n;     for (int i = 1; i < n; i++) {       int k = max(0, min(b - i, z[i - a]));       while (i + k < n && s[i + k] == s[k]) k++;       z[i] = k;       if (i + k > b) a = i, b = i + k;     }   }   bool chk(char* s) {     for (int i(0); i < (le_res); i++) naam_and_res[le_naam + 1 + i] = s[i];     naam_and_res[le_naam + 1 + le_res] = 0;     z_functie(naam_and_res, le_naam + 1 + le_res, zvals);     for (int i(0); i < (le_res - le_naam + 1); i++) {       int zz(zvals[le_naam + 1 + i]);       if (zz == le_naam && occ[i] ==  0 ) return false;       if (zz != le_naam && occ[i] ==  1 ) return false;     }     return true;   }   char tpl[1000];   char ans[1000];   int main() {     cin >> le_res >> aa;     scanf( %s , naam), le_naam = strlen(naam);     for (int i(0); i < (le_naam); i++) naam_and_res[i] = naam[i];     naam_and_res[le_naam] =  $ ;     scanf( %s , occ);     for (int i(0); i < (le_res); i++) tpl[i] =  . ;     for (int i(0); i < (le_res - le_naam + 1); i++) {       if (occ[i] ==  1 ) {         for (int j(i); j < (i + le_naam); j++) {           tpl[j] = naam[j - i];         }       }     }     for (int i(0); i < (le_naam); i++) {       char c( a  + (((naam[i] -  a ) + 1) % aa));       for (int i(0); i < (le_res); i++) {         if (tpl[i] ==  . )           ans[i] = c;         else           ans[i] = tpl[i];       }       if (chk(ans)) {         ans[le_res] = 0;         printf( %s n , ans);         goto done;       }     }     puts( No solution );   done:;   }


#include <bits/stdc++.h>   #pragma GCC optimize  03    using namespace std;   const long long int N = 2e5 + 5;   const long long int mod = 1e9 + 7;   const long long int inf = 1e9 + 9;   long long int a[N];   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int n;     cin >> n;     stack<long long int> s;     long long int ans = 0;     for (long long int i = 1; i <= n; i++) {       cin >> a[i];       while (!s.empty() && a[s.top()] < a[i]) s.pop();       if (!s.empty()) ans = max(ans, a[s.top()] ^ a[i]);       s.push(i);     }     for (long long int i = n; i >= 1; i--) {       while (!s.empty() && a[s.top()] < a[i]) s.pop();       if (!s.empty()) ans = max(ans, a[s.top()] ^ a[i]);       s.push(i);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( -O2 )   using namespace std;   void err(istream_iterator<string> it) {}   template <typename T, typename... Args>   void err(istream_iterator<string> it, T a, Args... args) {     cerr << *it <<   =   << a << endl;     err(++it, args...);   }   const int LIM = 1e5 + 5, MOD = 1e9 + 7;   int t, n, m, k, x, y;   vector<int> par, rk;   int getpar(int i) { return (par[i] == i) ? i : (par[i] = getpar(par[i])); }   bool join(int i, int j) {     i = getpar(i), j = getpar(j);     if (i == j) return 0;     if (rk[i] > rk[j])       par[j] = i;     else {       par[i] = j;       if (rk[i] == rk[j]) rk[j]++;     }     return 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     par.resize(2 * n + 2 * m - 2);     for (int i = 0; i < par.size(); i++) par[i] = i;     x = t + m + n + m - 2;     rk.assign(2 * n + 2 * m - 2, 0);     for (int i = 0; i < n; i++) {       join(i + x, i);       join(i + x + 1 - m, i + m - 1);     }     for (int i = 0; i < m; i++) {       join(x - i, i);       join(x - i + n - 1, i + n - 1);     }     set<int> st;     for (int i = 0; i < par.size(); i++) {       st.insert(getpar(i));     }     cout << st.size() <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   ll x[1 << 18];   int main() {     ios::sync_with_stdio(false), cin.tie(0);     ll n, r;     cin >> n >> r;     ll m = 1 << n;     ll sum = 0;     for (int i = 0; i < m; i++) cin >> x[i], sum += x[i];     cout << fixed << setprecision(7);     cout << (ld)sum / (ld)m << endl;     for (int i = 0; i < r; i++) {       ll a, b;       cin >> a >> b;       sum -= x[a];       x[a] = b;       sum += x[a];       cout << (ld)sum / (ld)m << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     long long n, k;     cin >> n >> k;     if (k >= n / 2)       cout << n * (n - 1) / 2;     else       cout << n * (n - 1) / 2 - (n - 2 * k) * (n - 2 * k - 1) / 2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long T;     cin >> T;     while (T--) {       int N;       string S;       cin >> N >> S;       int Who = -1;       string Ans1 =   , Ans2 =   ;       for (int i = 0; i < N; i++) {         if (S[i] ==  2  && !(~Who)) {           Ans1.push_back( 1 );           Ans2.push_back( 1 );         } else if (S[i] ==  2  && ~Who) {           Ans1.push_back( 0 );           Ans2.push_back( 2 );         } else if (S[i] ==  0 ) {           Ans1.push_back( 0 );           Ans2.push_back( 0 );         } else {           if (~Who) {             Ans1.push_back( 0 );             Ans2.push_back( 1 );           } else {             Ans1.push_back( 1 );             Ans2.push_back( 0 );             Who = 1;           }         }       }       cout << Ans1 <<   n  << Ans2 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int const N = 1000000;   int n, m;   vector<vector<pair<int, int> > > adj;   bool valid;   int ans[N];   int cnt;   int st[N * 3];   void readin() {     int u, v;     char b[10];     scanf( %d%d , &n, &m);     valid = true;     adj.clear();     adj.resize(n + 1);     for (int i = 0; i < m; ++i) {       scanf( %d%d%s , &u, &v, b);       if (v <= u) {         valid = false;       }       adj[u].push_back(make_pair(v, b[0] ==  L  ? 0 : 1));     }   }   inline int lc(int root) { return root << 1; }   inline int rc(int root) { return (root << 1) | 1; }   void init_st(int root, int l, int r) {     if (l == r) {       st[root] = l;       return;     }     int mid = (l + r) >> 1;     init_st(lc(root), l, mid);     init_st(rc(root), mid + 1, r);     st[root] = max(st[lc(root)], st[rc(root)]);   }   void update_st(int root, int l, int r, int x, int v) {     if (l == r) {       st[root] = v;       return;     }     int mid = (l + r) >> 1;     if (x <= mid) {       update_st(lc(root), l, mid, x, v);     } else {       update_st(rc(root), mid + 1, r, x, v);     }   }   int query_st(int root, int l, int r, int x, int y) {     if (l == r) {       return st[root];     }     int mid = (l + r) >> 1;     if (y <= mid) {       return query_st(lc(root), l, mid, x, y);     } else if (x > mid) {       return query_st(rc(root), mid + 1, r, x, y);     } else {       return max(query_st(lc(root), l, mid, x, mid),                  query_st(rc(root), mid + 1, r, mid + 1, y));     }   }   bool build(int root, int limit) {     if (root > limit) {       return true;     }     int l_max = 0, r_min = INT_MAX;     for (vector<pair<int, int> >::const_iterator it = adj[root].begin();          it != adj[root].end(); ++it) {       if (it->second == 0) {         l_max = max(l_max, query_st(1, 1, n, root + 1, it->first));       } else {         r_min = min(r_min, it->first);       }     }     if (l_max == 0) {       ans[cnt++] = root;       return build(root + 1, limit);     } else if (r_min == INT_MAX) {       bool ret = build(root + 1, limit);       ans[cnt++] = root;       return ret;     } else {       if (l_max >= r_min) {         return false;       }       bool ret = true;       ret = ret && build(root + 1, l_max);       ans[cnt++] = root;       ret = ret && build(l_max + 1, limit);       return ret;     }   }   void solve() {     if (!valid) {       printf( IMPOSSIBLE n );       return;     }     init_st(1, 1, n);     for (int i = n; i >= 1; --i) {       int u = i;       for (vector<pair<int, int> >::const_iterator it = adj[i].begin();            it != adj[i].end(); ++it) {         u = max(u, it->first);       }       update_st(1, 1, n, i, query_st(1, 1, n, i, u));     }     cnt = 0;     if (build(1, n)) {       for (int i = 0; i < n; ++i) {         printf( %d%c , ans[i], i < n - 1 ?     :   n );       }     } else {       printf( IMPOSSIBLE n );     }   }   int main() {     readin();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, f = 0;     cin >> n;     string a[] = { Sheldon ,  Leonard ,  Penny ,  Rajesh ,  Howard };     while (n > 5) {       n = n / 2 - 2;     }     cout << a[n - 1] << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     int n, k;     cin >> n >> k;     string s, st =   ;     cin >> s;     vector<int> m(28);     vector<int> l(28);     for (int i = 0; i < n; i++) {       m[((int)s[i]) - 97]++;     }     if (n == k) {       cout <<   n ;       return 0;     }     while (k > 0) {       for (int i = 0; i < 27; i++) {         if (k >= m[i]) {           k -= m[i];           l[i] = m[i];         } else {           m[i] = k;           l[i] = k;           k = 0;         }         if (k == 0) {           break;         }       }     }     for (int i = 0; i < n; i++) {       if (l[((int)s[i]) - 97] > 0) {         l[((int)s[i]) - 97]--;         continue;       } else         cout << s[i];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 80, Inf = 0x3f3f3f3f;   int n, f[N][N][N][2];   int pos[3][N], num[3][N];   char s[N];   inline int getid(char ch) {     if (ch ==  V ) return 0;     if (ch ==  K ) return 1;     return 2;   }   inline int move(int i, int j, int k, int id) {     return max(0, num[0][id] - i) + max(0, num[1][id] - j) +            max(0, num[2][id] - k) - 1;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> s + 1;     for (int i = 1; i <= n; ++i) {       int now = getid(s[i]);       for (int j = 0; j < 3; ++j) num[j][i] = num[j][i - 1];       pos[now][++num[now][i]] = i;     }     memset(f, 0x3f, sizeof f);     f[0][0][0][0] = 0;     for (int i = 0; i <= num[0][n]; ++i)       for (int j = 0; j <= num[1][n]; ++j)         for (int k = 0; k <= num[2][n]; ++k)           for (int fl = 0; fl < 2; ++fl) {             if (i < num[0][n])               f[i + 1][j][k][1] =                   min(f[i + 1][j][k][1],                       f[i][j][k][fl] + move(i, j, k, pos[0][i + 1]));             if (j < num[1][n] && !fl)               f[i][j + 1][k][0] =                   min(f[i][j + 1][k][0],                       f[i][j][k][fl] + move(i, j, k, pos[1][j + 1]));             if (k < num[2][n])               f[i][j][k + 1][0] =                   min(f[i][j][k + 1][0],                       f[i][j][k][fl] + move(i, j, k, pos[2][k + 1]));           }     cout << min(f[num[0][n]][num[1][n]][num[2][n]][0],                 f[num[0][n]][num[1][n]][num[2][n]][1])          << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e18;   long long fn(string a, string b) {     long long n = a.size(), m = b.size(), j = 0;     for (long long i = 0; i < m; i++) {       if (b[i] == a[j]) {         j++;         if (j >= n) {           return m - n;         }       }     }     if (j >= n) {       return m - n;     }     return m - j + n - j;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long ttt;     cin >> ttt;     vector<string> v;     long long x = 1;     while (x <= inf) {       string s =   ;       long long z = x;       while (z) {         s += (char)( 0  + z % 10);         z /= 10;       }       reverse(s.begin(), s.end());       v.push_back(s);       x *= 2;     }     while (ttt--) {       string s;       cin >> s;       long long n = s.size();       long long ans = inf;       for (auto x : v) {         ans = min(ans, fn(x, s));       }       cout << ans << endl;     }   }


#include <bits/stdc++.h>      using namespace std;      #define lln long long int      #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);      void solve(){    int n, k;    cin>>n>>k;       vector<int> a(n+1);       for(int i = 1;i<=k;i++)     a[i] = i;       int x = k - 1;       for(int i = k+1;i<=n;i++){     a[i] = x;     x--;    }       vector<int> p(n+1);    vector<int> ans;       for(int i = n;i>=1;i--){     if(p[a[i]]==1)      continue;     else {      ans.push_back(a[i]);      p[a[i]] = 1;     }    }    reverse(ans.begin(),ans.end());       for(auto i:ans){     cout<<i<<   ;    }    cout<<endl;   }      int main(){       fast          int q;       cin>>q;          while(q--){     solve();       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, sz1, sz2, val;   int d1[200005], d2[200005], d[200005];   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &val);       if (val != 0) d1[sz1++] = val;     }     for (int i = 0; i < n; i++) {       scanf( %d , &val);       if (val != 0) d2[sz2++] = val;     }     for (int i = 0; i < sz1; i++) {       int val = d1[i];       int id = (i + 1) % sz1;       d[val] = d1[id];     }     for (int i = 0; i < sz2; i++) {       int val = d2[i];       int id = (i + 1) % sz2;       if (d[val] == d2[id]) continue;       printf( NO n );       return 0;     }     printf( YES n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void low(int n) {     switch (n) {       default:         cout <<  zero ;         break;       case 1:         cout <<  one ;         break;       case 2:         cout <<  two ;         break;       case 3:         cout <<  three ;         break;       case 4:         cout <<  four ;         break;       case 5:         cout <<  five ;         break;       case 6:         cout <<  six ;         break;       case 7:         cout <<  seven ;         break;       case 8:         cout <<  eight ;         break;       case 9:         cout <<  nine ;         break;       case 10:         cout <<  ten ;         break;       case 11:         cout <<  eleven ;         break;       case 12:         cout <<  twelve ;         break;       case 13:         cout <<  thirteen ;         break;       case 14:         cout <<  fourteen ;         break;       case 15:         cout <<  fifteen ;         break;       case 16:         cout <<  sixteen ;         break;       case 17:         cout <<  seventeen ;         break;       case 18:         cout <<  eighteen ;         break;       case 19:         cout <<  nineteen ;         break;     }   }   void high(int n) {     switch (n) {       case 2:         cout <<  twenty ;         break;       case 3:         cout <<  thirty ;         break;       case 4:         cout <<  forty ;         break;       case 5:         cout <<  fifty ;         break;       case 6:         cout <<  sixty ;         break;       case 7:         cout <<  seventy ;         break;       case 8:         cout <<  eighty ;         break;       default:         cout <<  ninety ;         break;     }   }   int main() {     int n;     cin >> n;     if (n < 20)       low(n);     else {       high(n / 10);       if (n % 10 != 0) {         cout <<  - ;         low(n % 10);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void ga(int N, int *A) {     for (int i(0); i < N; i++) scanf( %d , A + i);   }   char s[222222], r[222222];   int N, A, B;   int main(void) {     scanf( %s%s , s, r), N = strlen(s);     for (int i(0); i < N; i++)       if (s[i] ^ r[i]) A += s[i] == 52, B += s[i] != 52;     printf( %d n , max(A, B));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5, mod = 1e9 + 7;   int add(int x, int y) {     x += y;     return x >= mod ? x - mod : x;   }   int c[maxn << 1], dp[maxn];   struct node {     int x, y, op;   } e[maxn];   bool cmp(node a, node b) {     if (a.x == b.x) return a.y < b.y;     return a.x < b.x;   }   void ins(int x, int k) {     while (x) {       c[x] = add(c[x], k);       x -= x & (-x);     }   }   int query(int x) {     int res = 0;     for (; x <= 400000; x += (x & (-x))) {       res = add(res, c[x]);     }     return res;   }   int main() {     int n, q, x;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &e[i].y, &e[i].x);     }     scanf( %d , &q);     while (q--) {       scanf( %d , &x);       e[x].op = 1;     }     sort(e + 1, e + 1 + n, cmp);     ins(2 * n, 1);     for (int i = 1; i <= n; i++) {       dp[i] = query(e[i].y);       ins(e[i].y, dp[i]);     }     int pv = 1, ans = 0;     for (int i = n; i > 0; i--) {       ins(e[i].y, (mod - dp[i]) % mod);       if (e[i].op == 1 && e[i].y >= pv) {         ans = add(ans, query(pv));         pv = e[i].y;       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline bool ig(char c) { return c >=  0  && c <=  9 ; }   inline void read(int &oi) {     char c;     int f = 1, res = 0;     while (c = getchar(), (!ig(c)) && c !=  - )       ;     c ==  -  ? f = -1 : res = (c ^ 48);     while (c = getchar(), ig(c)) res = res * 10 + (c ^ 48);     oi = f * res;   }   inline void print(int oi) {     if (oi < 0) putchar( - ), oi = ~oi + 1;     if (oi > 9) print(oi / 10);     putchar(oi % 10 + 48);   }   int n, m, ans;   struct Edge {     int x, y, z;     bool operator<(const Edge &b) const { return z > b.z; }   } e[200005];   int fa[200005];   bool s[200005];   inline int gfa(int u) { return u ^ fa[u] ? fa[u] = gfa(fa[u]) : u; }   int main() {     read(n);     read(m);     for (int i = 1; i <= m; ++i) read(e[i].x), read(e[i].y), read(e[i].z);     sort(e + 1, e + 1 + m);     for (int i = 1; i <= n; ++i) fa[i] = i, s[i] = true;     for (int i = 1; i <= m; ++i) {       int fx = gfa(e[i].x), fy = gfa(e[i].y);       if ((fx ^ fy) && (s[fx] || s[fy]))         ans += e[i].z, fa[fx] = fy, s[fy] = s[fy] & s[fx];       else if (!(fx ^ fy) && s[fx])         s[fx] = false, ans += e[i].z;     }     print(ans);     putchar(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[200005];   unordered_map<long long, long long> f, done;   vector<long long> v;   int main() {     std::ios_base::sync_with_stdio(0);     cin.tie(0);     long long n;     long long ans = 0;     cin >> n;     for (long long i = 1; i <= n; i++) {       cin >> a[i];       f[a[i]]++;     }     for (int i = 1; i <= n; i++) {       if (f[i] == 0) {         ans++;         v.push_back(i);       }     }     cout << ans << endl;     long long ptr = 0;     for (long long i = 1; i <= n; i++) {       if (f[a[i]] == 1) {         if (done[a[i]] == 0) {           cout << a[i] <<    ;           done[a[i]]++;         } else {           cout << v[ptr] <<    ;           ptr++;         }       } else {         if (v[ptr] > a[i] && done[a[i]] == 0) {           cout << a[i] <<    ;           f[a[i]]--;           done[a[i]]++;         } else {           f[a[i]]--;           cout << v[ptr] <<    ;           done[v[ptr]]++;           ptr++;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int N, M, T, tb[100005];   long long a, b, c, co, x;   map<int, int> mp;   map<int, int>::iterator it;   int chk[100005];   int main() {     int i, j, k;     scanf( %d%I64d , &N, &co);     for (i = 1; i <= N; i++) {       scanf( %d , &tb[i]);       mp[tb[i]]++;       x += tb[i];     }     if (x < co) {       printf( -1 n );       scanf(   );       return 0;     } else if (x == co) {       printf(  n );       scanf(   );       return 0;     }     a = N;     b = 0;     for (it = mp.begin(); it != mp.end(); it++) {       k = (*it).first;       j = (*it).second;       if (c + a * (k - b) > co) break;       c += a * (k - b);       b = k;       a -= j;     }     a = k - b;     long long num = co - c;     int idx = 0;     for (i = 1; i <= N; i++) {       if (tb[i] >= k) {         chk[idx] = i;         idx++;       }     }     b = num / idx;     num %= idx;     i = num;     bool err = true;     for (int kk = 1; kk <= idx; kk++) {       if (b != a - 1 || ((b == a - 1 && tb[chk[i]] != k) || err))         printf( %d  , chk[i]);       i++;       if (i >= idx) {         i %= idx;         err = false;       }     }     scanf(   );   }


#include <bits/stdc++.h>   using namespace std;   int mida;   void update(vector<int>& T, int p, int x) {     T[mida + p] = x;     for (int i = (mida + p) / 2; i > 0; i /= 2) T[i] = T[2 * i] + T[2 * i + 1];   }   int suma(vector<int>& T, int a, int b, int c = 0, int d = mida - 1, int p = 1) {     if (a <= c and d <= b) return T[p];     if (d < a or c > b) return 0;     return suma(T, a, b, c, (c + d) / 2, 2 * p) +            suma(T, a, b, (c + d) / 2 + 1, d, 2 * p + 1);   }   int main() {     int k;     string r;     cin >> k >> r;     string s;     for (int i = 0; i < k; ++i) s += r;     int n = s.size();     mida = 1;     while (mida < n) mida *= 2;     vector<vector<int> > T( z  -  a  + 1, vector<int>(2 * mida, 0));     for (int i = 0; i < n; ++i) update(T[s[i] -  a ], i, 1);     int q;     cin >> q;     while (q--) {       int p;       char c;       cin >> p >> c;       int ini = 0, fin = n - 1;       while (ini <= fin) {         int m = (ini + fin) / 2;         if (suma(T[c -  a ], 0, m) >= p)           fin = m - 1;         else           ini = m + 1;       }       s[fin + 1] = 0;       update(T[c -  a ], fin + 1, 0);     }     for (int i = 0; i < n; ++i)       if (s[i] > 0) cout << s[i];     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   int r, c, s, e;   vector<int> adj[1000 * 1000 + 10];   int d[1000 * 1000 + 10];   char grid[1000][1000];   char access(int n) {     int x = n % c;     int y = n / c;     return grid[y][x];   }   void bfs(int n) {     queue<int> frontier;     frontier.push(n);     d[n] = 0;     while (!frontier.empty()) {       int u = frontier.front();       frontier.pop();       for (int i = 0; i < adj[u].size(); i++) {         int v = adj[u][i];         if (d[v] == -1) {           d[v] = d[u] + 1;           frontier.push(v);         }       }     }   }   int main() {     cin >> r >> c;     for (int i = 0; i < r; i++) {       for (int j = 0; j < c; j++) {         cin >> grid[i][j];       }     }     for (int i = 0; i < r; i++) {       for (int j = 0; j < c; j++) {         int cell = i * c + j;         if (grid[i][j] ==  T ) continue;         if (grid[i][j] ==  S )           s = cell;         else if (grid[i][j] ==  E )           e = cell;         if (grid[i - 1][j] !=  T  && i > 0) {           adj[cell].push_back(cell - c);           adj[cell - c].push_back(cell);         }         if (grid[i + 1][j] !=  T  && i < r - 1) {           adj[cell].push_back(cell + c);           adj[cell + c].push_back(cell);         }         if (grid[i][j - 1] !=  T  && j > 0) {           adj[cell].push_back(cell - 1);           adj[cell - 1].push_back(cell);         }         if (grid[i][j + 1] !=  T  && j < c - 1) {           adj[cell].push_back(cell + 1);           adj[cell + 1].push_back(cell);         }       }     }     memset(d, -1, sizeof(d));     bfs(e);     int shortest_path = d[s];     int ans = 0;     for (int i = 0; i < r * c; i++) {       char in = access(i);       if (in ==  T  || in ==  E  || in ==  S ) continue;       if (d[i] <= shortest_path && d[i] != -1) {         ans += in -  0 ;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int in[100000 + 5];   bool doit() {     if (scanf( %d%d , &n, &m) != 2) return false;     for (int i = 0; i < n; ++i) in[i] = 0;     int x, y;     for (int i = 0; i < m; ++i) {       scanf( %d%d , &x, &y);       ++in[--x], ++in[--y];     }     sort(in, in + n);     if (in[0] == 1 && in[n - 2] == 1 && in[n - 1] == n - 1)       printf( star topology n );     else if (in[2] == 2 && in[n - 1] == 2) {       if (in[0] == 1 && in[1] == 1)         printf( bus topology n );       else if (in[0] == 2)         printf( ring topology n );       else         printf( unknown topology n );     } else       printf( unknown topology n );     return true;   }   int main() {     while (1)       if (!doit()) break;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = (1 << 19) + 5;   template <typename T>   inline void read(T &WOW) {     T x = 0, flag = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) flag = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     WOW = flag * x;   }   namespace ModCalculator {   const int MOD = 998244353;   inline void Inc(int &x, int y) {     x += y;     if (x >= MOD) x -= MOD;   }   inline void Dec(int &x, int y) {     x -= y;     if (x < 0) x += MOD;   }   inline int Add(int x, int y) {     Inc(x, y);     return x;   }   inline int Sub(int x, int y) {     Dec(x, y);     return x;   }   inline int Mul(int x, int y) { return 1LL * x * y % MOD; }   inline int Ksm(int x, int y) {     int ret = 1;     for (; y; y >>= 1) {       if (y & 1) ret = Mul(ret, x);       x = Mul(x, x);     }     return ret;   }   inline int Inv(int x) { return Ksm(x, MOD - 2); }   }  // namespace ModCalculator   using namespace ModCalculator;   int len, rev[MAXN], w1[MAXN], w2[MAXN], fac[MAXN];   void NTT_init(int n) {     for (len = 1; len < n; len <<= 1)       ;     fac[0] = 1;     for (int i = 1; i <= len; ++i) {       fac[i] = Mul(fac[i - 1], i);     }     int Gi = 3, Gv = Inv(Gi);     for (int i = 1; i < len; i <<= 1) {       int wk1 = Ksm(Gi, (MOD - 1) / (i << 1)), wnk1 = 1;       int wk2 = Ksm(Gv, (MOD - 1) / (i << 1)), wnk2 = 1;       for (int k = 0; k < i; ++k) {         w1[i + k] = wnk1;         wnk1 = Mul(wnk1, wk1);         w2[i + k] = wnk2;         wnk2 = Mul(wnk2, wk2);       }     }   }   void GetRev(int n) {     int bit = -1;     for (len = 1; len < n; len <<= 1) ++bit;     for (int i = 0; i < len; ++i) {       rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << bit);     }   }   void NTT(vector<int> &a, int type) {     a.resize(len);     int *w = (type == 1 ? w1 : w2);     for (int i = 0; i < len; ++i) {       if (i < rev[i]) {         swap(a[i], a[rev[i]]);       }     }     for (int i = 1; i < len; i <<= 1) {       for (int j = 0; j < len; j += (i << 1)) {         for (int k = 0; k < i; ++k) {           int x = a[j + k], y = Mul(a[j + k + i], w[i + k]);           a[j + k] = Add(x, y);           a[j + k + i] = Sub(x, y);         }       }     }     if (type == 1) return;     int lenv = Inv(len);     for (int i = 0; i < len; ++i) {       a[i] = Mul(a[i], lenv);     }   }   vector<int> Poly_Mul(vector<int> F, vector<int> G) {     int deg = F.size() + G.size() - 1;     if (deg <= 100) {       vector<int> H(deg, 0);       int n = F.size(), m = G.size();       for (int i = 0; i < n; ++i) {         for (int j = 0; j < m; ++j) {           Inc(H[i + j], Mul(F[i], G[j]));         }       }       return H;     }     GetRev(deg);     NTT(F, 1);     NTT(G, 1);     for (int i = 0; i < len; ++i) {       F[i] = Mul(F[i], G[i]);     }     NTT(F, -1);     F.resize(deg);     return F;   }   int n, deg[MAXN], node[MAXN], tot;   vector<int> Poly_Prod(int l, int r) {     if (l == r) {       vector<int> F;       F.push_back(1);       F.push_back(node[l]);       return F;     }     int mid = (l + r) >> 1;     return Poly_Mul(Poly_Prod(l, mid), Poly_Prod(mid + 1, r));   }   void solve() {     read(n);     NTT_init((n + 1) << 1);     for (int i = 1, u, v; i < n; ++i) {       read(u);       read(v);       ++deg[u];       ++deg[v];     }     for (int i = 1; i <= n; ++i) {       int x = deg[i] - (i > 1);       if (x) node[++tot] = x;     }     vector<int> F = Poly_Prod(1, tot);     int ans = 0;     for (int i = 0; i < n && i < F.size(); ++i) {       int tmp = Mul(F[i], fac[n - i]);       (i & 1) ? Dec(ans, tmp) : Inc(ans, tmp);     }     printf( %d n , ans);   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   constexpr long long MOD = (long long)1e9 + 7;   long long fexp(long long a, long long n) {     a %= MOD;     if (n == 0) return 1;     long long ret = fexp(a * a % MOD, n / 2);     if (n % 2 == 1) ret = ret * a % MOD;     return ret;   }   long long blub(long long pa, long long pb) {     return pa * fexp((pa + pb) % MOD, MOD - 2) % MOD;   }   int main() {     long long k, pa, pb;     cin >> k >> pa >> pb;     long long ppa = blub(pa, pb);     long long ppb = blub(pb, pa);     vector<vector<long long>> dp(k + 1, vector<long long>(k, 0));     long long res = 0;     dp[1][0] = 1;     for (int i = 1; i < k; ++i) {       for (int j = 0; j < k; ++j) {         dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * ppa) % MOD;         if (j + i < k)           dp[i][j + i] = (dp[i][j + i] + dp[i][j] * ppb) % MOD;         else {           res = (res + (j + i) * dp[i][j] % MOD * ppb) % MOD;         }       }     }     for (int i = 0; i < k; ++i) {       res = (res + ppb * dp[k][i] % MOD *                        (ppa * fexp((1 - ppa) * (1 - ppa), MOD - 2) % MOD +                         (i + k) * fexp(1 - ppa, MOD - 2) % MOD)) %             MOD;     }     cout << (res % MOD + MOD) % MOD << endl;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> kmp(string &str) {     int n = str.size();     vector<int> res(n + 1);     res[0] = -1;     int ind = -1;     for (int i = 0; i < n; i++) {       while (ind >= 0 && str[i] != str[ind]) ind = res[ind];       ind++;       res[i + 1] = ind;     }     return res;   }   int nex(string &str, char ch, int i, vector<int> &kmp1) {     if (str[i] == ch) return i + 1;     int res = i;     while (res >= 0 && str[res] != ch) res = kmp1[res];     return res + 1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     string s, s1, s2;     cin >> s >> s1 >> s2;     int n = s.size(), n1 = s1.size(), n2 = s2.size();     vector<int> kmp1 = kmp(s1), kmp2 = kmp(s2);     vector<vector<vector<int>>> dp(         n + 1, vector<vector<int>>(n1 + 1, vector<int>(n2 + 1, -(1 << 29))));     dp[0][0][0] = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  * ) {         for (char ch =  a ; ch <=  z ; ch++) {           for (int j = 0; j < n1; j++) {             for (int k = 0; k < n2; k++) {               int nj = nex(s1, ch, j, kmp1), nk = nex(s2, ch, k, kmp2);               dp[i + 1][nj][nk] = max(dp[i + 1][nj][nk], dp[i][j][k]);             }           }         }       } else {         char ch = s[i];         for (int j = 0; j < n1; j++) {           for (int k = 0; k < n2; k++) {             int nj = nex(s1, ch, j, kmp1), nk = nex(s2, ch, k, kmp2);             dp[i + 1][nj][nk] = max(dp[i + 1][nj][nk], dp[i][j][k]);           }         }       }       int nj = kmp1[n1], nk = kmp2[n2];       for (int j = 0; j < n1; j++) {         dp[i + 1][j][nk] = max(dp[i + 1][j][nk], dp[i + 1][j][n2] - 1);       }       for (int k = 0; k < n2; k++) {         dp[i + 1][nj][k] = max(dp[i + 1][nj][k], dp[i + 1][n1][k] + 1);       }       dp[i + 1][nj][nk] = max(dp[i + 1][nj][nk], dp[i + 1][n1][n2]);     }     int res = -(1 << 29);     for (int j = 0; j < n1; j++) {       for (int k = 0; k < n2; k++) {         res = max(res, dp[n][j][k]);       }     }     cout << res <<   n ;     return 0;   }


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


#include <bits/stdc++.h>   int main() {     int n, d, t, x, y, z, m;     scanf( %d %d %d , &n, &d, &m);     z = n - d;     while (m--) {       scanf( %d %d , &x, &y);       if (x > n || y > n) {         printf( NO n );       } else {         if (y >= abs(x - d) && y <= n - (abs(z - x))) {           printf( YES n );         } else {           printf( NO n );         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e3 + 10, mod = 1e9 + 7;   int fact[N], invfact[N];   inline int add(int x, int y) {     x += y;     if (x >= mod) x -= mod;     return x;   }   inline int sub(int x, int y) {     x -= y;     if (x < 0) x += mod;     return x;   }   inline int mul(int x, int y) { return (((long long)x) * y) % mod; }   inline int powr(int a, long long b) {     int x = 1 % mod;     while (b) {       if (b & 1) x = mul(x, a);       a = mul(a, a);       b >>= 1;     }     return x;   }   inline int inv(int a) { return powr(a, mod - 2); }   void pre() {     fact[0] = invfact[0] = 1;     for (int i = 1; i < N; i++) fact[i] = mul(i, fact[i - 1]);     invfact[N - 1] = inv(fact[N - 1]);     for (int i = N - 2; i >= 1; i--) invfact[i] = mul(invfact[i + 1], i + 1);     assert(invfact[1] == 1);   }   inline int C(int n, int k) {     if (n < k || k < 0) return 0;     return mul(fact[n], mul(invfact[k], invfact[n - k]));   }   int n, k;   int C2[N + 10], A[N], E[N];   inline int getC2(int i) {     if (i > k) return 0;     return C2[i];   }   int F[N][N];   int main() {     pre();     cin >> n >> k;     F[0][0] = 1;     for (int i = 1; i <= n; i++) {       F[i][0] = 1;       cin >> A[i];       for (int j = 1; j <= i; j++) {         F[i][j] = add(F[i - 1][j], mul(A[i], F[i - 1][j - 1]));       }     }     C2[0] = 1;     for (int i = 1; i <= n + 10; i++)       C2[i] = mul(C2[i - 1], mul(k - i + 1, inv(i)));     int invn = inv(n);     int _div = 1;     for (int i = 0; i <= n - 1; i++) {       E[i] = mul(getC2(i + 1), _div);       _div = mul(_div, invn);       E[i] = mul(E[i], fact[i]);     }     int ans = 0;     for (int i = 0; i <= n - 1; i++) {       ans = add(           ans,           mul(F[n][i], mul(n - i, mul(powr(mod - 1, n - 1 - i), E[n - 1 - i]))));     }     ans = mul(ans, invn);     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline bool chmax(T &a, T b) {     if (a < b) {       a = b;       return true;     }     return false;   }   template <class T>   inline bool chmin(T &a, T b) {     if (a > b) {       a = b;       return true;     }     return false;   }   const long long INF = 1e18;   long long N;   long long a[1050];   vector<string> ans, trueans;   vector<vector<int>> Prev, Next;   void f(int item) {     int idx1 = 0;     int idx2 = 1;     Next.clear();     for (auto v : Prev) {       if (v.size() <= 1) continue;       idx1 = v[0];       idx2 = v[1];     }     ans[idx1][item] =  1 ;     ans[idx2][item] =  0 ;     long long one = a[item] - 1;     for (int i = 0; i <= N; i++) {       if (i == idx2 or i == idx1) continue;       if (one) {         one--;         ans[i][item] =  1 ;       } else {         ans[i][item] =  0 ;       }     }     for (auto v : Prev) {       vector<int> u[2];       for (auto tmp : v) {         if (ans[tmp][item] ==  0 )           u[0].push_back(tmp);         else           u[1].push_back(tmp);       }       for (int i = 0; i < 2; i++) {         if (u[i].size()) Next.push_back(u[i]);       }     }     swap(Prev, Next);   }   int main() {     cin >> N;     for (int i = 0; i < N; i++) cin >> a[i];     ans.resize(N + 1, string(N,  0 ));     Prev.push_back({});     for (int i = 0; i <= N; i++) {       Prev[0].push_back(i);     }     for (int i = 0; i < N; i++) f(i);     for (auto v : Prev) {       assert(v.size() == 1);     }     for (auto s : ans) {       if (s != string(N,  0 )) trueans.push_back(s);     }     cout << trueans.size() <<   n ;     for (auto s : trueans) {       cout << s <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n;     cin >> n;     map<long long int, long long int> mp;     vector<long long int> v(n);     vector<long long int> cSum(n, 0);     for (long long int i = 0; i < n; i++) {       cin >> v[i];       mp[v[i]]++;     }     cSum[0] = v[0];     for (long long int i = 1; i < n; i++) {       cSum[i] = cSum[i - 1] + v[i];     }     if (cSum[n - 1] % 2 != 0) {       cout <<  NO ;     } else {       int flag2 = 0;       set<long long int> st;       for (long long int i = 0; i < n; i++) {         mp[v[i]]--;         long long int sum1 = cSum[i];         long long int sum2 = cSum[n - 1] - cSum[i];         long long int t = sum1 + sum2;         long long int each = t / 2;         if (each == sum1 && each == sum2) {           flag2 = 1;           break;         }         if (sum1 < sum2) {           long long int diff = each - sum1;           if (mp[diff] > 0) {             flag2 = 1;             break;           }         }         if (sum1 > sum2) {           long long int diff = each - sum2;           if (st.find(diff) != st.end()) {             flag2 = 1;             break;           }         }         st.insert(v[i]);       }       if (flag2 == 0) {         cout <<  NO ;       } else {         cout <<  YES ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n;   pair<string, long long> s[1000005];   long long m;   bool cmp(pair<string, long long> a, pair<string, long long> b) {     string s = a.first, t = b.first;     for (long long i = 0; i < (m); i++)       if (s[i] != t[i]) {         if (i & 1)           return s[i] > t[i];         else           return s[i] < t[i];       }     return a.second < b.second;   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (long long i = 0; i < (n); i++) cin >> s[i].first, s[i].second = i + 1;     sort(s, s + n, cmp);     for (long long i = 0; i < (n); i++) cout << s[i].second <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( Ofast )   const long long int maxn = 5e5 + 16, md = 1e9 + 7, inf = 2e18;   long long int tav(long long int n, long long int k) {     long long int res = 1;     while (k > 0) {       if (k & 1) {         res *= n;         res %= md;       }       n *= n;       n %= md;       k >>= 1;     }     return res;   }   struct segtree {     long long int sz = 1;     vector<long long int> val, laz;     void init(long long int n) {       while (sz < n) sz <<= 1;       val.assign((sz << 1), 0);       laz.assign((sz << 1), 0);       return;     }     void shift(long long int x, long long int lx, long long int rx) {       long long int h = laz[x];       laz[x] = 0;       if (h == 0) return;       val[x] += h;       if (rx - lx == 1) return;       long long int ln = (x << 1) + 1, rn = ln + 1;       laz[ln] += h;       laz[rn] += h;       return;     }     void add(long long int l, long long int r, long long int k, long long int x,              long long int lx, long long int rx) {       shift(x, lx, rx);       if (rx <= l || lx >= r) return;       if (rx <= r && lx >= l) {         laz[x] = k;         shift(x, lx, rx);         return;       }       long long int m = (rx + lx) >> 1, ln = (x << 1) + 1, rn = ln + 1;       add(l, r, k, ln, lx, m);       add(l, r, k, rn, m, rx);       val[x] = min(val[ln], val[rn]);       return;     }     void add(long long int l, long long int r, long long int k) {       add(l, r, k, 0, 0, sz);       return;     }     long long int cal(long long int l, long long int r, long long int x,                       long long int lx, long long int rx) {       shift(x, lx, rx);       if (rx <= l || lx >= r) return inf;       if (rx <= r && lx >= l) return val[x];       long long int m = (rx + lx) >> 1, ln = (x << 1) + 1, rn = ln + 1;       long long int a = cal(l, r, ln, lx, m), b = cal(l, r, rn, m, rx);       return min(a, b);     }     long long int cal(long long int l, long long int r) {       return cal(l, r, 0, 0, sz);     }   };   long long int n, q;   segtree st;   vector<pair<long long int, long long int> > adj[maxn];   vector<pair<pair<long long int, long long int>, long long int> > v[maxn];   long long int ft[maxn], tme = 0, ans[maxn], dis[maxn], dep = 0;   void dDFS(long long int r) {     dis[r] = dep;     tme++;     for (auto p : adj[r]) {       long long int i = p.first, w = p.second;       dep += w;       dDFS(i);       dep -= w;     }     if (adj[r].empty()) {       st.add(r, r + 1, dep);     } else {       st.add(r, r + 1, inf);     }     ft[r] = tme;     return;   }   void sDFS(long long int r) {     for (auto y : v[r]) {       long long int l = y.first.first, r = y.first.second, i = y.second;       ans[i] = st.cal(l, r);     }     for (auto p : adj[r]) {       long long int i = p.first, w = p.second;       st.add(0, n, w);       st.add(i, ft[i], -2ll * w);       sDFS(i);       st.add(0, n, -w);       st.add(i, ft[i], 2ll * w);     }     return;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> q;     for (long long int u = 1; u < n; u++) {       long long int v, w;       cin >> v >> w;       v--;       adj[v].push_back({u, w});     }     st.init(n);     for (long long int i = 0; i < q; i++) {       long long int u, l, r;       cin >> u >> l >> r;       u--;       l--;       v[u].push_back({{l, r}, i});     }     dDFS(0);     sDFS(0);     for (long long int i = 0; i < q; i++) {       cout << ans[i] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool umin(T& a, T b) {     if (a > b) {       a = b;       return 1;     }     return 0;   }   template <class T>   bool umax(T& a, T b) {     if (a < b) {       a = b;       return 1;     }     return 0;   }   char s[1000009], t[1000009];   int cnt[2];   int pw[1000009], par[1000009];   int wp[1000009], rap[1000009];   int mod(long long x) {     x %= 1000000007;     if (x < 0) x += 1000000007;     return x;   }   pair<int, int> get(int l, int r) {     return make_pair(mod(par[r] - (par[l - 1] * 1LL * pw[r - l + 1])),                      mod(rap[r] - (rap[l - 1] * 1LL * wp[r - l + 1])));   }   int main() {     scanf( %s , s);     int n = strlen(s);     scanf( %s , t + 1);     int m = strlen(t + 1);     pw[0] = wp[0] = 1;     for (int i = 1; i < 1000009; i++) {       pw[i] = mod(pw[i - 1] * 27LL);       wp[i] = mod(wp[i - 1] * 31LL);     }     for (int i = 1; i <= m; i++) {       par[i] = mod(par[i - 1] * 27LL + (t[i] -  a  + 1));       rap[i] = mod(rap[i - 1] * 31LL + (t[i] -  a  + 1));     }     for (int i = 0; i < n; i++) cnt[s[i] -  0 ]++;     int ans = 0;     for (int i = 1; i * cnt[0] < m and i < m; i++)       if ((m - i * cnt[0]) % cnt[1] == 0) {         int d = (m - i * cnt[0]) / cnt[1];         pair<int, int> a, b;         a.first = b.first = -1;         int cur = 1, f = 1;         for (int j = 0; j < n; j++) {           if (s[j] ==  0 ) {             if (a.first == -1)               a = get(cur, cur + i - 1);             else               f &= (a == get(cur, cur + i - 1));             cur += i;           } else {             if (b.first == -1)               b = get(cur, cur + d - 1);             else               f &= (b == get(cur, cur + d - 1));             cur += d;           }         }         if (f and d == i and a == b) f = 0;         ans += f;       }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   long long t, a, b, h, f[110];   using namespace std;   long long mi(long long n, long long m) {     long long res = 1;     for (int i = 1; i <= m; i++) res *= n;     return res;   }   void solve(long long a, long long b) {     for (int i = h; i; i--) {       long long t = mi(a, i);       f[i] = b / t;       b %= t;     }     f[0] = b;   }   int main() {     scanf( %lld%lld%lld , &t, &a, &b);     if (a == b && b == t) {       if (a == 1)         puts( inf );       else         puts( 2 );     } else if (a == b)       puts( 1 );     else {       if (t == 1) {         h = log(b) / log(a);         for (int i = 1; i <= h; i++)           if (mi(a, i) == b) {             puts( 1 );             return 0;           }       }       h = log(b) / log(a);       solve(a, b);       long long sum = 0;       for (int i = 0; i <= h; i++) sum += f[i] * mi(t, i);       if (sum != a)         puts( 0 );       else         puts( 1 );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n;     cin >> n;     vector<pair<long long, long long>> arr(n);     for (long long i = 0; i < n; i++) {       long long a;       cin >> a;       if (a >= 0) a = (-1) * a - 1;       arr[i] = {a, i};     }     sort(arr.begin(), arr.end());     if (n % 2 == 1) {       long long f = 0;       for (long long i = 0; i < n; i++) {         if (arr[i].first != -1) {           arr[i].first = (-1) * arr[i].first - 1;           f++;           break;         }       }       if (f == 0) {         arr[0].first = 0;       }     }     vector<long long> ans(n);     for (long long i = 0; i < n; i++) {       ans[arr[i].second] = arr[i].first;     }     for (auto i : ans) cout << i <<    ;     cout << endl;   }


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


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


#include <bits/stdc++.h>   #pragma GCC optimize(3)   #pragma GCC optimize( inline )   using namespace std;   const int N = 100050, INF = 0x3f3f3f3f;   int n, col, sum, n1, n2;   struct node {     int color, val, id;     friend bool operator<(node x, node y) { return x.val < y.val; }   } white[N], black[N];   int min(int x, int y) { return (x > y) ? y : x; }   signed main() {     scanf( %d , &n);     for (register int i = 1; i <= n; i++) {       scanf( %d%d , &col, &sum);       if (col)         white[++n1] = node{col, sum, i};       else         black[++n2] = node{col, sum, i};     }     sort(white + 1, white + 1 + n1);     sort(black + 1, black + 1 + n2);     for (register int i = 1, j = 1; i <= n1 && j <= n2;) {       int now = min(white[i].val, black[j].val);       cout << white[i].id <<     << black[j].id <<     << now << endl;       white[i].val -= now;       black[j].val -= now;       if (white[i].val)         j++;       else if (black[j].val)         i++;       else if (i < n1)         i++;       else         j++;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 5e3 + 5;   long long read() {     long long s = 0;     char c = getchar(), lc =  + ;     while (c <  0  ||  9  < c) lc = c, c = getchar();     while ( 0  <= c && c <=  9 ) s = s * 10 + c -  0 , c = getchar();     return lc ==  -  ? -s : s;   }   void write(long long x) {     if (x < 0) {       putchar( - );       x = -x;     }     if (x < 10)       putchar(x +  0 );     else {       write(x / 10);       putchar(x % 10 +  0 );     }   }   void print(long long x, char c =   n ) {     write(x);     putchar(c);   }   long long dp[N][N], a[N], q[N];   signed main(signed Recall, char *_902_[]) {     (void)Recall, (void)_902_;     long long n = read(), k = read(), m = read(), ans = -1;     for (long long i = 1; i <= n; i++) a[i] = read();     for (long long i = 1; i <= n; i++) dp[i][0] = dp[0][i] = -1;     for (long long j = 1; j <= m; j++)       for (long long i = 1, h = 0, t = q[0] = 0; i <= n; i++) {         while (h <= t && i - q[h] > k) h++;         if (h <= t && dp[q[h]][j - 1] >= 0)           dp[i][j] = dp[q[h]][j - 1] + a[i];         else           dp[i][j] = -1;         while (h <= t && dp[q[t]][j - 1] <= dp[i][j - 1]) t--;         q[++t] = i;       }     for (long long i = max(n - k + 1, 1LL); i <= n; i++) ans = max(ans, dp[i][m]);     print(ans);     return 0;   }


/****************************************** By Lord Sanskar Bhargava******************************/           #include<bits/stdc++.h>   using namespace std;      typedef long long ll;   typedef long double ld;   typedef complex<ld> cd;       typedef pair<int, int> pi;   typedef pair<ll,ll> pl;   typedef pair<ld,ld> pd;       typedef vector<int> vi;   typedef vector<ld> vd;   typedef vector<ll> vl;   typedef vector<pi> vpi;   typedef vector<pl> vpl;   typedef vector<cd> vcd;      int main()   {       ios::sync_with_stdio(0);cin.tie(0);       int tt;       cin>>tt;       while(tt--)       {         ll n;cin>>n;       ll k=n%2020;         if(n<2020)         {           cout<< NO <<endl;         }         else if(k*2021<=n)         {           cout<< YES <<endl;         }         else         {           cout<< NO <<endl;         }          }   return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dp[1003][1003][12][3];   string s, t;   int solve(int i, int j, int k, int cont) {     if (i < 0 || j < 0) return 0;     if (dp[i][j][k][cont] != -1) return dp[i][j][k][cont];     int ret = -9;     if (s[i] == t[j]) {       if (cont) {         ret = max(ret, 1 + solve(i - 1, j - 1, k, 1));       }       if (k > 0) ret = max(ret, 1 + solve(i - 1, j - 1, k - 1, 1));     }     ret = max(ret, solve(i - 1, j, k, 0));     ret = max(ret, solve(i, j - 1, k, 0));     dp[i][j][k][cont] = ret;     return ret;   }   int main() {     int n, m, k;     cin >> n >> m >> k;     cin >> s >> t;     for (int i = 0; i < 1003; ++i) {       for (int j = 0; j < 1003; ++j) {         for (int k = 0; k < 12; ++k) {           for (int l = 0; l < 3; ++l) {             dp[i][j][k][l] = -1;           }         }       }     }     int ans = solve(n - 1, m - 1, k, 0);     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   const int N = 510;   int m;   long long n, ans;   int cnt, head[N];   struct edge {     int to, next;   } e[1010];   void ade(int u, int v) {     e[++cnt] = (edge){v, head[u]};     head[u] = cnt;   }   long long getsize(long long num) {     if (num > n) return 0;     long long tl = num, tr = num;     int m1 = 0, m2 = 0;     while (tl * 2 <= n) tl = tl * 2, m1++;     while (tr * 2 + 1 <= n) tr = tr * 2 + 1, m2++;     if (tl <= tr)       return (1 << (m2 + 1)) - 1;     else       return (1 << (m2 + 1)) - 1 + n - tl + 1;   }   map<int, int> vis;   int tot;   long long val[N];   void build(int x) {     val[vis[x]] = 1;     int to = x << 1;     if (to <= n && vis[to]) {       build(to);       ade(vis[x], vis[to]);       ade(vis[to], vis[x]);     } else       val[vis[x]] += getsize(to);     to = x << 1 | 1;     if (to <= n && vis[to]) {       build(to);       ade(vis[x], vis[to]);       ade(vis[to], vis[x]);     } else       val[vis[x]] += getsize(to);   }   int bk[N];   void dfs(int now, long long w) {     bk[now] = 1;     for (int i = head[now]; i; i = e[i].next) {       int to = e[i].to;       if (bk[to]) continue;       dfs(to, w);       ans = (ans + w * val[to] % mod) % mod;     }     bk[now] = 0;   }   int main() {     cin >> n >> m;     if (!m) {       ans = n * n % mod;       ans = (ans % mod + mod) % mod;       printf( %lld n , ans);     } else {       for (int i = 1; i <= m; i++) {         int a, b;         cin >> a >> b;         if (!vis[a]) vis[a] = ++tot;         if (!vis[b]) vis[b] = ++tot;         ade(vis[a], vis[b]);         ade(vis[b], vis[a]);         while (a) {           a >>= 1;           if (!vis[a]) vis[a] = ++tot;         }         while (b) {           b >>= 1;           if (!vis[b]) vis[b] = ++tot;         }       }       build(1);       for (int i = 1; i <= tot; i++) dfs(i, val[i]);       for (int i = 1; i <= tot; i++) ans = (ans + val[i] * val[i] % mod) % mod;       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 105, MAX_R = 60005;   int n, r, a, b;   vector<pair<int, int> > inc, d;   int dp[MAX_R][MAX_N];   int main() {     ios::sync_with_stdio(false);     cin >> n >> r;     for (int i = 0; i < n; i++) {       cin >> a >> b;       if (b >= 0) {         inc.push_back({a, b});       } else {         d.push_back({max(a, -b) + b, b});       }     }     sort(inc.begin(), inc.end());     sort(d.begin(), d.end());     int proj = 0;     for (auto p : inc) {       if (r >= p.first) {         proj++;         r += p.second;       }     }     int len = d.size();     for (int i = 0; i <= len; i++) {       if (i == 0) {         for (int j = 0; j <= r; j++) {           dp[j][i] = proj;         }         continue;       }       int a = d[i - 1].first - d[i - 1].second;       int b = d[i - 1].second;       for (int j = 0; j <= r; j++) {         if (j >= a) {           dp[j][i] = max(dp[j][i - 1], 1 + dp[j + b][i - 1]);         } else {           dp[j][i] = dp[j][i - 1];         }       }     }     cout << dp[r][len] << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long n, b, x, q, t, ans, l, r, m, p, now, sum;   bool ok;   string s;   map<int, int> a, pos;   map<char, int> c;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> q;     while (q--) {       cin >> n >> m;       cin >> s;       for (int i = 1; i <= m; ++i) {         cin >> x;         a[x + 1]++;       }       sum = m + 1;       for (int i = 0; i < s.size(); ++i) {         sum -= a[i + 1];         c[s[i]] += sum;       }       for (char j =  a ; j <=  z ; ++j) {         cout << c[j] <<    ;       }       cout <<   n ;       a.clear();       c.clear();     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T read(T& x) {     x = 0;     int f = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) f |= (ch ==  - ), ch = getchar();     while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar();     return x = f ? -x : x;   }   const int N = 500 + 10;   const long long INF = 2e18;   int i, j, x, n, match[N], pre[N];   long long a[N], b[N], k[N], cost[N][N], lx[N], ly[N], slack[N];   bool vy[N];   void augment(int root) {     fill(vy + 1, vy + n + 1, false);     fill(slack + 1, slack + n + 1, INF);     int py;     match[py = 0] = root;     do {       vy[py] = true;       int x = match[py], yy;       long long delta = INF;       for (int y = 1; y <= n; y++) {         if (!vy[y]) {           if (lx[x] + ly[y] - cost[x][y] < slack[y]) {             slack[y] = lx[x] + ly[y] - cost[x][y];             pre[y] = py;           }           if (slack[y] < delta) {             delta = slack[y];             yy = y;           }         }       }       for (int y = 0; y <= n; y++) {         if (vy[y]) {           lx[match[y]] -= delta;           ly[y] += delta;         } else {           slack[y] -= delta;         }       }       py = yy;     } while (~match[py]);     do {       int cnt = pre[py];       match[py] = match[cnt];       py = cnt;     } while (py);   }   long long KM() {     for (int i = 1; i <= n; i++) {       lx[i] = ly[i] = 0;       match[i] = -1;       for (int j = 1; j <= n; j++) lx[i] = max(lx[i], cost[i][j]);     }     long long ans = 0;     for (int i = 1; i <= n; i++) augment(i);     for (int i = 1; i <= n; i++) {       ans += lx[i];       ans += ly[i];     }     return ans;   }   int main() {     read(n);     for (i = 1; i <= n; i++) {       read(a[i]), read(b[i]), read(k[i]);       for (j = 1; j <= n; j++) {         cost[i][j] = max(a[i] - b[i] * min(k[i], j - 1LL), 0LL);       }     }     printf( %lld n , KM());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = (int)1e9 + 10;   const int N = (int)1e6 + 10;   const long double eps = 1e-7;   mt19937 rnd(228);   int main() {     int n, q;     cin >> n >> q;     deque<int> d;     int mn = INF;     int mx = -INF;     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       d.push_back(x);       mn = min(mn, x);       mx = max(mx, x);     }     vector<pair<int, int>> ans(N);     for (int iter = 1; iter < N; ++iter) {       int a = d.front();       d.pop_front();       int b = d.front();       ans[iter] = make_pair(a, b);       d.pop_front();       d.push_front(max(a, b));       d.push_back(min(a, b));     }     int st = -1;     for (int i = N / 2 - 1; i >= 1; --i) {       if (ans[i] == pair<int, int>({mx, mn})) {         st = i;         break;       }     }     for (int i = 1; i <= q; ++i) {       long long m;       cin >> m;       if (m < N) {         cout << ans[m].first <<     << ans[m].second <<   n ;         continue;       }       m -= st;       m %= (n - 1);       cout << ans[st + m].first <<     << ans[st + m].second <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int n, k;   pair<int, int> a[N];   vector<pair<int, int> > Center;   bool inf;   bool Check(pair<int, int> center) {     int cnt = 0, l = 1, r = n;     pair<int, int> t;     while (l <= r && cnt <= k) {       t = make_pair(center.first - a[l].first, center.second - a[l].second);       if (t == a[r]) {         l++;         r--;       } else {         cnt++;         if (t < a[r])           r--;         else           l++;       }     }     return (cnt <= k);   }   void Solve() {     if (k >= n) {       inf = true;       return;     }     sort(a + 1, a + n + 1);     pair<int, int> center;     int res = 0;     for (int i = 1; i <= k + 1; i++)       for (int j = n - k; j <= n; j++)         if (i <= j) {           center = make_pair(a[i].first + a[j].first, a[i].second + a[j].second);           if (Check(center)) Center.push_back(center);         }   }   int main() {     cin >> n >> k;     for (int i = 1; i <= n; i++) scanf( %d %d , &a[i].first, &a[i].second);     Solve();     if (inf) {       cout << -1 << endl;       return 0;     }     int res = 0;     sort(Center.begin(), Center.end());     for (int i = 0; i < Center.size(); i++)       if (i == 0 || Center[i] != Center[i - 1]) res++;     cout << res << endl;     int cnt = 0;     for (int i = 0; i < Center.size(); i++)       if (i == 0 || Center[i] != Center[i - 1]) {         printf( %.3lf %.3lf n , (Center[i].first + 0.0) / 2.0,                (Center[i].second + 0.0) / 2.0);       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( Ofast,no-stack-protector )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   #pragma GCC optimize( unroll-loops )   int on_bit(int N, int pos) { return N = N | (1 << pos); }   int off_bit(int N, int pos) { return N = N & ~(1 << pos); }   bool check_bit(long long int N, int pos) { return (bool)(N & (1 << pos)); }   template <class T>   inline bool read(T &x) {     int c = getchar();     int sgn = 1;     while (~c && c <  0  || c >  9 ) {       if (c ==  - ) sgn = -1;       c = getchar();     }     for (x = 0; ~c &&  0  <= c && c <=  9 ; c = getchar()) x = x * 10 + c -  0 ;     x *= sgn;     return ~c;   }   int main() {     unsigned long long ar[500005];     memset((ar), 0, sizeof((ar)));     for (unsigned long long i = 3, j = 1; i <= 500001; i += 2, j++) {       ar[i] = ((i * 4) - 4) * j;     }     for (int i = 5; i <= 500001; i += 2) {       ar[i] += ar[i - 2];     }     int ___T;     cin >> ___T;     for (int cs = 1; cs <= ___T; cs++) {       int n;       cin >> n;       cout << ar[n] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long nt[105];   string s;   vector<long long> V;   void ts() {     nt[0] = 0;     nt[1] = 0;     for (long long i = 2; i <= 100; i++) {       nt[i] = 1;     }     for (long long i = 2; i <= 100; i++) {       if (nt[i] == 1) {         for (long long j = i * i; j <= 100; j += i) {           nt[j] = 0;         }         V.push_back(i);       }     }   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     V.push_back(4);     V.push_back(9);     V.push_back(25);     V.push_back(49);     ts();     long long i = 0;     long long dem = 0;     while (i < 20) {       cout << V[i] << endl;       cin >> s;       if (s ==  yes ) dem++;       if (dem == 2) {         cout <<  composite ;         return 0;       }       i++;     }     cout <<  prime ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void maxE(T& a, const T& b) {     a = max(a, b);   }   template <typename T>   void minE(T& a, const T& b) {     a = min(a, b);   }   template <typename T>   ostream& operator<<(ostream& out, const vector<T>& t_) {     out <<  [ ;     for (auto i : t_) out << i <<  ,  ;     out <<  ] ;     return out;   }   template <typename S, typename T>   ostream& operator<<(ostream& out, const pair<S, T>& rhs) {     out <<  (  << rhs.first <<  ,  << rhs.second <<  ) ;     return out;   }   const long long INF = 1000LL * 1000 * 1000 * 1000 * 1000 * 1000LL;   struct Val {     long long maxPref, maxSuf, sum, maxInf;   };   Val neutral() {     Val r;     r.maxPref = r.maxSuf = r.maxInf = 0;     r.sum = 0;     return r;   }   Val single(long long a) {     Val r;     r.maxPref = r.maxSuf = r.maxInf = max(0LL, a);     r.sum = a;     return r;   }   Val join(const Val& a, const Val& b) {     Val r;     r.maxPref = max(a.maxPref, a.sum + b.maxPref);     r.maxSuf = max(a.maxSuf + b.sum, b.maxSuf);     r.sum = a.sum + b.sum;     r.maxInf = max(max(a.maxInf, b.maxInf), a.maxSuf + b.maxPref);     return r;   }   struct Tree {     int s;     vector<Val> t;     void init(int s_) {       s = 1;       while (s <= s_) s *= 2;       t.assign(2 * s, neutral());     }     Val tget(int a, int b) {       if (a > b)         return neutral();       else if (a == b)         return t[a];       Val r = neutral();       int na = a;       int nb = b;       if (a % 2) na++;       if (nb % 2 == 0) nb--;       if (a % 2) r = join(r, t[a]);       r = join(r, tget(na / 2, nb / 2));       if (b % 2 == 0) r = join(r, t[b]);       return r;     }     long long get(int a, int b) { return tget(s + a, s + b).maxInf; }   };   int n;   vector<long long> t;   Tree tree;   int main() {     int m;     assert(scanf( %d%d , &n, &m) == 2);     vector<int> d, h;     for (int i = 0; i < int(n); ++i) {       int a;       assert(scanf( %d , &a) == 1);       d.push_back(a);     }     for (int i = 0; i < int(n); ++i) {       int a;       assert(scanf( %d , &a) == 1);       h.push_back(a);     }     for (int j = 0; j < int(2); ++j)       for (int i = 0; i < int(n); ++i) {         t.push_back(h[i] * 2LL);         t.push_back(-4LL * h[i]);         t.push_back(h[i] * 2LL);         t.push_back(d[i]);       }     tree.init(((int)((t).size())));     for (int i = 0; i < int(((int)((t).size()))); ++i)       tree.t[tree.s + i] = single(t[i]);     for (int i = tree.s - 1; i >= 1; i--)       tree.t[i] = join(tree.t[i * 2], tree.t[i * 2 + 1]);     if (0) cout <<  tree ready   << endl;     if (0)       cout <<  SIZE(t)             <<     << ((int)((t).size())) <<     << endl;     for (int i = 0; i < int(m); ++i) {       int a, b;       assert(scanf( %d%d , &a, &b) == 2);       a--;       b--;       int x, y;       if (a <= b) {         x = b + 1;         y = a + n - 1;       } else {         x = b + 1;         y = a - 1;       }       assert(x < y);       if (0)         cout <<  a               <<     << a <<                  <<  b               <<     << b <<                  <<  x               <<     << x <<                  <<  y               <<     << y <<     << endl;       x = 4 * x;       y = 4 * y;       int s = ((int)((t).size()));       assert(x >= 0 && x < s && y >= 0 && y < s);       cout << tree.get(x, y) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string get(int n, int m) {     string str;     for (int i = 1; i <= n; i++) str.push_back( 4 );     for (int i = 1; i <= m; i++) str.push_back( 7 );     return str;   }   int32_t main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     string str;     cin >> str;     int n = str.size();     if (n & 1) {       cout << get((n + 1) / 2, (n + 1) / 2) << endl;       return 0;     }     int cnta = 0, cntb = 0, ans = -1, aa = 0, bb = 0;     for (int i = 0; i < n; i++) {       if (cnta + 1 <= n / 2 && str[i] <  7 ) {         ans = i;         aa = cnta;         bb = cntb;       }       if (cntb + 1 <= n / 2 && str[i] <  4 ) {         ans = i;         aa = cnta;         bb = cntb;       }       cnta += str[i] ==  7 ;       cntb += str[i] ==  4 ;       if ((str[i] !=  4  && str[i] !=  7 ) || cnta > n / 2 || cntb > n / 2) break;     }     if (cnta + cntb == n && cnta == cntb) {       cout << str << endl;       return 0;     }     if (ans == -1) {       cout << get((n + 2) / 2, (n + 2) / 2) << endl;       return 0;     }     string x;     for (int i = 0; i < ans; i++) {       x.push_back(str[i]);     }     if (bb + 1 <= n / 2 && str[ans] <  4 ) {       ++bb;       x.push_back( 4 );     } else {       ++aa;       x.push_back( 7 );     }     x += get(n / 2 - bb, n / 2 - aa);     cout << x << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> a;   vector<stack<int>> ids;   vector<int> b;   string answer;   int segtree_size;   template <typename T>   struct segtree {     int segtree_size;     vector<T> segtree;     T identity;     T (*UpdateFn)(T, T);     void InitSegtree(int main_size, T (*UpdateFunction)(T, T), T fun_identity) {       segtree_size = 1;       while (segtree_size < main_size) segtree_size *= 2;       segtree_size *= 2;       segtree.resize(segtree_size);       UpdateFn = UpdateFunction;       this->identity = fun_identity;     }     void UpdateAllParents() {       for (int i = segtree_size / 2 - 1; i > 0; i--)         segtree[i] = UpdateFn(segtree[i * 2], segtree[i * 2 + 1]);     }     void ChangeValue(int index, T value) {       segtree[index + segtree_size / 2] = value;     }     T GetValue(int index) { return segtree[index + segtree_size / 2]; }     void Update(int pos, T value) {       for (segtree[pos += segtree_size / 2] = value; pos > 1; pos /= 2)         segtree[pos / 2] = UpdateFn(segtree[pos], segtree[pos ^ 1]);     }     T Query(int start, int finish) {       T ans = this->identity;       for (start += segtree_size / 2, finish += segtree_size / 2; start < finish;            start /= 2, finish /= 2) {         if (start % 2 == 1) ans = UpdateFn(ans, segtree[start++]);         if (finish % 2 == 0) ans = UpdateFn(ans, segtree[finish--]);       }       return UpdateFn(ans, segtree[start]);     }     void Destroy() { segtree.clear(); }   };   struct segtree<int> tree;   void In() {     cin >> n;     a.resize(n);     ids.resize(n + 1);     b.resize(n);     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];   }   void Solve() {     tree.InitSegtree(         n + 1, [](int a, int b) -> int { return max(a, b); }, INT_MIN);     for (int i = 0; i < n; i++) tree.ChangeValue(i, a[i]);     tree.UpdateAllParents();     answer =  NO ;     for (int i = 0; i < n; i++) ids[a[i]].push(i);     for (int i = n - 1; i >= 0; i--) {       if (ids[b[i]].empty()) return;       int a_idx = ids[b[i]].top();       ids[b[i]].pop();       if (tree.Query(a_idx + 1, n) > b[i]) return;       tree.Update(a_idx, 0);     }     answer =  YES ;   }   void Out() {     cout << answer << endl;     tree.Destroy();     a.clear();     ids.clear();     b.clear();   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       In();       Solve();       Out();     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, k, i;     scanf( %d%d , &n, &k);     while (n > k) {       for (i = 1; i <= k; i++) {         if (n % 10 > 0)           n--;         else           n = n / 10;       }       printf( %d , n);       n = 0;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, l[200000 + 5], r[200000 + 5];   int p[200000 + 5], q[200000 + 5], sum;   long long add(long long a, long long b) { return (a + b) % 1000000007; }   long long mul(long long a, long long b) { return 1LL * a * b % 1000000007; }   long long dec(long long a, long long b) {     return (a - b + 1000000007) % 1000000007;   }   int fst_pow(int a, int b) {     int res = 1;     while (b) {       if (b & 1) res = mul(res, a);       a = mul(a, a);       b >>= 1;     }     return res;   }   int val(int i) {     int ans = 1;     ans = dec(ans, p[i]), ans = dec(ans, p[i + 1]);     int L = max(l[i - 1], max(l[i], l[i + 1])),         R = min(r[i - 1], min(r[i], r[i + 1]));     if (L < R)       ans = add(ans, mul(R - L, fst_pow(mul(mul(r[i] - l[i], r[i - 1] - l[i - 1]),                                             r[i + 1] - l[i + 1]),                                         1000000007 - 2)));     return ans;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &l[i]);     for (int i = 1; i <= n; i++) {       scanf( %d , &r[i]);       r[i]++;     }     int sum = 0;     for (int i = 1; i <= n; i++) {       int L = max(l[i], l[i - 1]), R = min(r[i], r[i - 1]);       if (L < R)         p[i] = mul(R - L, fst_pow(mul(r[i] - l[i], r[i - 1] - l[i - 1]),                                   1000000007 - 2));       q[i] = dec(1, p[i]);       sum = add(sum, q[i]);     }     int ans = 0;     for (int i = 1; i <= n; i++) {       ans = add(ans, q[i]);       int ps = dec(sum, q[i]);       if (i > 2) ans = add(ans, val(i - 1)), ps = dec(ps, q[i - 1]);       if (i < n) ans = add(ans, val(i)), ps = dec(ps, q[i + 1]);       ans = add(ans, mul(q[i], ps));     }     printf( %d , ans);   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:100000000 )   using namespace std;   int matrix[22][22];   int mas[22];   int matrix1[22][22];   int matrix2[22][22];   int best = 0;   int minv[22];   bool used[22];   int u[22];   int v[22];   int p[22];   int way[22];   int getresult(int n) {     int res = 0;     for (int i = 0; i <= n; i++) u[i] = 0;     for (int i = 0; i <= n; i++) v[i] = 0;     for (int i = 0; i <= n; i++) p[i] = 0;     for (int i = 0; i <= n; i++) way[i] = 0;     int m = n;     const int INF = 1e9;     for (int i = 1; i <= n; ++i) {       p[0] = i;       int j0 = 0;       for (int x = 0; x <= m; x++) minv[x] = INF;       for (int x = 0; x <= m; x++) used[x] = false;       do {         used[j0] = true;         int i0 = p[j0], delta = INF, j1;         for (int j = 1; j <= m; ++j)           if (!used[j]) {             int cur = matrix[i0][j] - u[i0] - v[j];             if (cur < minv[j]) minv[j] = cur, way[j] = j0;             if (minv[j] < delta) delta = minv[j], j1 = j;           }         for (int j = 0; j <= m; ++j)           if (used[j])             u[p[j]] += delta, v[j] -= delta;           else             minv[j] -= delta;         j0 = j1;       } while (p[j0] != 0);       do {         int j1 = way[j0];         p[j0] = p[j1];         j0 = j1;       } while (j0);     }     return -v[0];   }   int line1max[22];   int line2max[22];   int colmax[22];   int rr[22];   bool stop = false;   int itt = 0;   int go(int pos, int N, int N2, int cnt1 = 0, int cnt2 = 0) {     if (stop) return 0;     if (pos == N) {       int res = 0;       int maxres = 0;       int maxres2 = 0;       for (int i = 0; i < N; i++) colmax[i] = 0;       for (int i = 0; i < N; i++) {         if (mas[i] == 1) {           for (int j = 0; j < N; j++)             matrix[i + 1][j + 1] = -matrix1[i][j],                               colmax[j] = max(colmax[j], matrix1[i][j]);           maxres += line1max[i];         } else {           for (int j = 0; j < N; j++)             matrix[i + 1][j + 1] = -matrix2[i][j],                               colmax[j] = max(colmax[j], matrix2[i][j]);           maxres += line2max[i];         }       }       for (int i = 0; i < N; i++) maxres2 += colmax[i];       if (maxres > best && maxres2 > best) {         res = -getresult(N);         best = max(best, res);       }       return res;     }     int res = 0;     if (rr[pos]) {       if (cnt1 < N2) {         mas[pos] = 1;         res = max(res, go(pos + 1, N, N2, cnt1 + 1, cnt2));       }       if (cnt2 < N2) {         mas[pos] = 2;         res = max(res, go(pos + 1, N, N2, cnt1, cnt2 + 1));       }     } else {       if (cnt2 < N2) {         mas[pos] = 2;         res = max(res, go(pos + 1, N, N2, cnt1, cnt2 + 1));       }       if (cnt1 < N2) {         mas[pos] = 1;         res = max(res, go(pos + 1, N, N2, cnt1 + 1, cnt2));       }     }     itt++;     if (itt & 63) {       if (clock() * 1e-3 > 0.8) {         stop = true;         return res;       }     }     return res;   }   int rowperm[22];   int colperm[22];   bool visrow[22];   bool viscol[22];   int main() {     int N;     scanf( %d , &N);     for (int i = 0; i < N; i++) rowperm[i] = i;     for (int i = 0; i < N; i++) colperm[i] = i;     srand(time(0));     random_shuffle(rowperm, rowperm + N);     random_shuffle(colperm, colperm + N);     for (int i = 0; i < N; i++) rr[i] = rand() % 2;     for (int i = 0; i < N; i++)       for (int j = 0; j < N; j++) scanf( %d , &matrix1[rowperm[i]][colperm[j]]);     for (int i = 0; i < N; i++) {       for (int j = 0; j < N; j++) {         line1max[i] = max(line1max[i], matrix1[i][j]);       }     }     for (int i = 0; i < N; i++)       for (int j = 0; j < N; j++) scanf( %d , &matrix2[rowperm[i]][colperm[j]]);     for (int i = 0; i < N; i++) {       for (int j = 0; j < N; j++) {         line2max[i] = max(line2max[i], matrix2[i][j]);       }     }     int ost1 = N / 2;     int ost2 = N / 2;     memset(visrow, true, sizeof(visrow));     memset(viscol, true, sizeof(viscol));     for (int step = 0; step < N; step++) {       int maxi1 = 0;       int r1 = 0, c1 = 0;       int maxi2 = 0;       int r2 = 0, c2 = 0;       for (int i = 0; i < N; i++) {         for (int j = 0; j < N; j++) {           if (matrix1[i][j] > maxi1 && visrow[i] && viscol[j]) {             maxi1 = matrix1[i][j];             r1 = i;             c1 = j;           }           if (matrix2[i][j] > maxi2 && visrow[i] && viscol[j]) {             maxi2 = matrix2[i][j];             r2 = i;             c2 = j;           }         }       }       if (ost1 > 0 && maxi1 > maxi2) {         ost1--;         best += maxi1;         visrow[r1] = false;         viscol[c1] = false;       } else if (ost2 > 0 && maxi2 > maxi1) {         ost2--;         best += maxi2;         visrow[r2] = false;         viscol[c2] = false;       } else if (maxi1 == maxi2) {         if (ost1 > ost2) {           ost1--;           best += maxi1;           visrow[r1] = false;           viscol[c1] = false;         } else {           ost2--;           best += maxi2;           visrow[r2] = false;           viscol[c2] = false;         }       } else if (ost1 > 0) {         ost1--;         best += maxi1;         visrow[r1] = false;         viscol[c1] = false;       } else {         ost2--;         best += maxi2;         visrow[r2] = false;         viscol[c2] = false;       }     }     printf( %d n , max(best, go(0, N, N / 2)));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int solve(string s, int x, int y) {     int t = 0;     for (auto i : s) {       if (i -  0  == x) {         t++;         swap(x, y);       }     }     if (x != y && t % 2 == 1) --t;     return t;   }   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(0);     int t;     cin >> t;     while (t--) {       string s;       cin >> s;       int ans = 0;       for (int i = 0; i <= 9; i++) {         for (int j = 0; j <= 9; j++) {           ans = max(ans, solve(s, i, j));         }       }       cout << (int)(s.size()) - ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char a[2010], b[2010], c[2010];   int aa[256], bb[256];   int n;   void _reverse(char *a, int l) {     for (int i = 0; i < l; i++) c[i] = a[i];     for (int i = 0; i < l; i++) a[i] = c[l - 1 - i];   }   void _rotate(char *a, int p) {     for (int i = 0; i < p; i++) c[n - p + i] = a[i];     for (int i = 0; i < n - p; i++) c[i] = a[p + i];     for (int i = 0; i < n; i++) a[i] = c[i];   }   int main() {     scanf( %d%s%s , &n, a, b);     for (int i = 0; i < n; i++) {       aa[a[i]]++;       bb[b[i]]++;     }     for (int i = 0; i < 256; i++)       if (aa[i] != bb[i]) {         printf( -1 n );         return 0;       }     printf( %d n , 3 * n);     for (int i = 0; i < n; i++) {       int j;       _reverse(a, n);       for (j = 0; j < n; j++)         if (a[j] == b[n - 1 - i]) break;       printf( %d %d %d  , n, n - j - 1, 1);       _reverse(a + j + 1, n - j - 1);       _rotate(a, j + 1);       _rotate(a, n - 1);     }   }


#include <bits/stdc++.h>   void reduce(unsigned& nom, unsigned& denom) {     bool reduced = true;     while (reduced) {       reduced = false;       for (unsigned i = 2; !reduced && i < 1000; ++i) {         if (nom % i == 0 && denom % i == 0) {           nom /= i;           denom /= i;           reduced = true;         }       }     }   }   void solve() {     unsigned sum = 0;     unsigned A;     std::cin >> A;     for (unsigned i = 2; i < A; ++i) {       auto val = A;       while (val > 0) {         sum += val % i;         val /= i;       }     }     auto nom = sum;     auto denom = A - 2;     reduce(nom, denom);     std::cout << nom <<  /  << denom << std::endl;   }   int main(int argc, char* argv[]) {     { solve(); }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 1500;   const int max_m = 1500;   const int inv = numeric_limits<int>::max();   int n, m;   char G[max_n][max_m];   int si, sj;   int di[max_n][max_m][4];   int dj[max_n][max_m][4];   int asz[max_n][max_m] = {0};   int vsti[max_n][max_m];   int vstj[max_n][max_m];   stack<int> sti, stj;   int ct[max_n][max_m] = {0};   bool res;   int amod(int p, int d) {     if (p % d < 0)       return p % d + d;     else       return p % d;   }   void DFS() {     int vi, vj;     sti.push(si);     stj.push(sj);     while (!sti.empty()) {       vi = sti.top();       vj = stj.top();       int rvi = amod(vi, n), rvj = amod(vj, m);       vsti[rvi][rvj] = vi;       vstj[rvi][rvj] = vj;       int i;       int ni, nj, rni, rnj;       if (ct[rvi][rvj] == asz[rvi][rvj]) {         sti.pop();         stj.pop();       } else {         i = ct[rvi][rvj];         ++ct[rvi][rvj];         ni = vi + di[rvi][rvj][i];         nj = vj + dj[rvi][rvj][i];         rni = amod(ni, n);         rnj = amod(nj, m);         if (vsti[rni][rnj] == inv) {           sti.push(ni);           stj.push(nj);         } else if (vsti[rni][rnj] != ni or vstj[rni][rnj] != nj) {           res = true;           return;         }       }     }     return;   }   int main() {     int ldi[4] = {-1, 1, 0, 0};     int ldj[4] = {0, 0, -1, 1};     scanf( %d %d , &n, &m);     char trow[max_m + 10];     for (int i = 0; i < n; ++i) {       scanf( %s , trow);       for (int j = 0; j < m; ++j) {         G[i][j] = trow[j];         if (G[i][j] ==  S ) {           G[i][j] =  . ;           si = i;           sj = j;         }       }     }     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         if (G[i][j] ==  . ) {           for (int k = 0; k < 4; ++k) {             int xi = amod(i + ldi[k], n), xj = amod(j + ldj[k], m);             if (G[xi][xj] ==  . ) {               di[i][j][asz[i][j]] = ldi[k];               dj[i][j][asz[i][j]] = ldj[k];               ++asz[i][j];             }           }         }       }     }     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         vsti[i][j] = inv;         vstj[i][j] = inv;       }     }     res = false;     DFS();     if (res)       printf( Yes n );     else       printf( No n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void add_self(int &a, int b) {     a += b;     if (a >= (int)1e9 + 7) a -= (int)1e9 + 7;   }   int n, l;   void f(vector<int> &a) {     int x = a[0];     for (int i = 0; i < n - 1; i++) {       a[i] = a[i + 1] - a[i];     }     a[n - 1] = l - a[n - 1] + x;   }   void f1(vector<int> &a, int k) {     reverse(begin(a), end(a));     reverse(begin(a) + k, end(a));     reverse(begin(a), begin(a) + k);   }   void solve() {     cin >> n >> l;     vector<int> a(n), b(n);     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     f(a);     f(b);     for (int i = 0; i < n; i++) {       f1(a, 1);       bool ok = true;       for (int j = 0; j < n; j++) {         if (a[j] != b[j]) {           ok = false;           break;         }       }       if (ok) {         cout <<  YES ;         return;       }     }     cout <<  NO ;   }   int main() {     ios::sync_with_stdio(false);     int q = 1;     for (int i = 1; i <= q; i++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   long long int minimum(long long int a[], long long int m, long long int n) {     long long int ar[m];     for (long long int i = 0; i < m; i++) ar[i] = a[i];     long long int j = 0;     long long int ans = 0;     while (n > 0) {       if (ar[j] > 0) {         ans += ar[j];         ar[j] -= 1;         n--;       } else         j++;     }     return ans;   }   long long int maximum(long long int a[], long long int m, long long int n) {     long long int ar[m];     for (long long int i = 0; i < m; i++) ar[i] = a[i];     long long int ans = 0;     long long int j = m - 1;     while (n > 0) {       ans += ar[j];       ar[j] -= 1;       n--;       long long int maxi = ar[j];       long long int id = j;       for (long long int k = m - 1; k >= 0; k--) {         if (maxi < ar[k]) {           id = k;           maxi = ar[k];         }       }       swap(ar[id], ar[j]);     }     return ans;   }   void solve() {     long long int n, m;     cin >> n >> m;     long long int ar[m];     for (auto &it : ar) cin >> it;     ;     sort(ar, ar + m);     long long int mini = minimum(ar, m, n);     long long int maxi = maximum(ar, m, n);     cout << maxi <<     << mini <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();   }


#include <bits/stdc++.h>   using namespace std;   const int N = (1 << 20) + 10;   vector<pair<int, int>> g[N];   int tamp = 0;   bool faz;   namespace hpath_space {   vector<int> path;   vector<int> idit, used_edge;   void dfs(int no) {     while (1) {       int &id = idit[no];       while (id < (int)(g[no].size()) and used_edge[g[no][id].second]) id++;       if (id == (int)(g[no].size())) break;       used_edge[g[no][id].second] = 1;       int it = g[no][id++].first;       dfs(it);     }     if (faz) path.push_back(no);     tamp++;   }   bool has_cycle(int n, int m) {     int nimp = 0;     for (int i = 0; i < n; i++)       if ((int)(g[i].size()) & 1) nimp++;     tamp = 0;     if (nimp > 0) return 0;     int inic = 0;     for (int i = 0; i < n; i++)       if ((int)(g[i].size())) inic = i;     path.clear();     idit = vector<int>(n);     used_edge = vector<int>(m);     dfs(inic);     if (tamp == m + 1) return 1;     return 0;   }   };  // namespace hpath_space   int n;   vector<pair<int, int>> pe;   vector<int> ans;   template <class key_type, class value_type>   struct mymap {     int n;     vector<key_type> keys;     vector<value_type> values;     mymap(vector<key_type> _keys) {       keys = _keys;       sort((keys).begin(), (keys).end());       keys.resize(unique((keys).begin(), (keys).end()) - keys.begin());       n = (int)(keys.size());       values = vector<value_type>(n);     }     value_type &operator[](key_type key) {       int id = lower_bound((keys).begin(), (keys).end(), key) - keys.begin();       return values[id];     }   };   bool check(int k, bool sob = 1) {     faz = !sob;     int mask = (1 << k) - 1;     for (int i = 0; i < mask + 1; i++) g[i].clear();     for (int i = 0; i < n; i++) {       int ao, bo;       tie(ao, bo) = pe[i];       int a = ao & mask;       int b = bo & mask;       g[a].emplace_back(b, i);       g[b].emplace_back(a, i);     }     if (hpath_space::has_cycle(mask + 1, n)) {       if (sob) return 1;       vector<pair<int, int>> vmp, vst;       for (int i = 0; i < n; i++) {         int ao, bo;         tie(ao, bo) = pe[i];         int a = ao & mask;         int b = bo & mask;         vmp.emplace_back(a, b);         vmp.emplace_back(b, a);         vst.emplace_back(2 * i, 2 * i + 1);         vst.emplace_back(2 * i + 1, 2 * i);       }       mymap<pair<int, int>, vector<pair<int, int>>> mp(vmp);       mymap<pair<int, int>, int> mpid(vmp), st(vst);       for (int i = 0; i < n; i++) {         int ao, bo;         tie(ao, bo) = pe[i];         int a = ao & mask;         int b = bo & mask;         mp[{a, b}].push_back({2 * i, 2 * i + 1});         mp[{b, a}].push_back({2 * i + 1, 2 * i});       }       ans.clear();       vector<int> &path = hpath_space::path;       for (int i = 0; i < (int)(path.size()) - 1; i++) {         int va = path[i], vb = path[i + 1];         int &imp = mpid[{va, vb}];         int ia, ja;         auto &v = mp[{va, vb}];         while (1) {           tie(ia, ja) = v[imp];           if (st[{ia, ja}]) {             imp++;             continue;           }           st[{ia, ja}] = 1;           st[{ja, ia}] = 1;           break;         }         imp++;         ans.push_back(ia);         ans.push_back(ja);         assert(abs(ia - ja) == 1);       }       return 1;     }     return 0;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 0; i < n; i++) {       int a, b;       cin >> a >> b;       pe.emplace_back(a, b);     }     int lo = 0, hi = 20;     while (lo < hi) {       int k = (lo + hi + 1) / 2;       if (hi - lo + 1 >= 8) k = lo + (hi - lo) / 3;       if (check(k))         lo = k;       else         hi = k - 1;     }     if (lo) {       int k = lo;       assert(check(k, 0));       assert((int)(ans.size()) == 2 * n);       cout << k <<   n ;       for (int i = 0; i < 2 * n; i++) {         if (i) cout <<    ;         cout << ans[i] + 1;       }       cout <<   n ;       return 0;     }     cout << 0 <<   n ;     for (int i = 0; i < 2 * n; i++) {       if (i) cout <<    ;       cout << i + 1;     }     cout <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long int w, v, qt = 0, n;   inline unsigned long long int f(unsigned long long int x) {     return w * (1 - x) * (1 - x) + v;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> v >> w;     char c;     while (cin >> c) qt += c ==  1 ;     unsigned long long int ans = (~0ULL);     for (int i = 1; i <= qt; i++) {       unsigned long long int tmp = 0;       tmp += (i - (n % i)) * f(n / i);       tmp += (n % i) * f(n / i + 1);       ans = min(ans, tmp);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int number_of_players;     cin >> number_of_players;     int old_bid, counter;     for (counter = 0; counter < number_of_players; counter++) {       int bid;       cin >> bid;       while (bid % 2 == 0) bid /= 2;       while (bid % 3 == 0) bid /= 3;       if (counter == 0)         old_bid = bid;       else if (old_bid != bid) {         cout <<  No  << endl;         break;       }     }     if (counter == number_of_players) cout <<  Yes  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = (long long)1e9 + 7;   long long n, m, k;   const long long moll = 998244353;   long long fact(long long n) {     if (n == 0) return 1;     return (n * fact(n - 1)) % moll;   }   long long binpow(long long a, long long b) {     if (b == 0) return 1;     if (b % 2 == 0) return (binpow((a * a) % moll, b / 2)) % moll;     return (a * (binpow((a * a) % moll, b / 2))) % moll;   }   long long C(long long n, long long k) {     long long x = (fact(n) * binpow(fact(k), moll - 2)) % moll;     x *= binpow(fact(n - k), moll - 2);     x %= moll;     if (x < 0) x += moll;     return x;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n >> m >> k;     long long x = m;     x *= C(n - 1, k);     x %= moll;     x *= binpow(m - 1, k);     x %= moll;     if (x < 0) x += moll;     cout << x << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, i, j, a[105], b[105];   int main() {     cin >> n >> m;     for (i = 0; i < n; i++) cin >> a[i];     for (i = 0; i < m; i++) cin >> b[i];     for (i = 0; i < n; i++) {       for (j = 0; j < m; j++) {         if (a[i] == b[j]) {           cout << a[i] <<    ;           b[j] = -1;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     vector<int> tree[n + 1];     for (int i = 2; i <= n; i++) {       int p;       cin >> p;       tree[p].push_back(i);     }     bool is_spruce = true;     for (int i = 1; i <= n; i++) {       if (tree[i].empty())         continue;       else {         int count = 0;         for (auto c : tree[i]) {           if (count == 3) break;           if (tree[c].size() == 0) count++;         }         if (count != 3) {           is_spruce = false;           break;         }       }     }     if (is_spruce)       cout <<  Yes  << endl;     else       cout <<  No  << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     long long int s, sum = 0;     cin >> n >> s;     long long int* v = new long long int[n];     for (int i = 0; i < n; i++) {       cin >> v[i];       sum += v[i];     }     if (s > sum)       cout << -1 << endl;     else {       sort(v, v + n);       if (s < (sum - v[0] * n))         cout << v[0] << endl;       else {         s -= sum - v[0] * n;         if (s % n == 0)           cout << v[0] - s / n << endl;         else           cout << v[0] - (s / n) - 1 << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int N, M;   signed long long L[100020], R[100020], T[100020];   signed long long Q[100020];   vector<pair<signed long long, signed long long> > QP;   signed long long PP[200200];   signed long long AS[200200], BS[200200], ret[200200];   vector<signed long long> S[200200], E[200200];   void solve() {     int i, j, k, l, r, x, y;     string s;     cin >> N >> M;     for (i = 0; i < M; i++) cin >> L[i] >> R[i] >> T[i];     for (i = 0; i < N; i++) cin >> Q[i], QP.push_back(make_pair(Q[i], i));     sort(QP.begin(), QP.end());     map<signed long long, signed long long> UM;     UM[0] = 0;     for (i = 0; i < M; i++) UM[L[i]] = UM[R[i]] = 1;     i = 0;     for (__typeof(UM.begin()) it = UM.begin(); it != UM.end(); it++)       it->second = i++, PP[it->second] = it->first;     for (i = 0; i < M; i++)       S[UM[L[i]]].push_back(T[i]), E[UM[R[i]]].push_back(T[i]);     map<signed long long, int> CS;     for (i = 0; i < UM.size(); i++) {       for (j = 0; j < E[i].size(); j++)         if (--CS[E[i][j]] == 0) CS.erase(E[i][j]);       for (j = 0; j < S[i].size(); j++) CS[S[i][j]]++;       if (CS.size()) {         signed long long ct = CS.begin()->first;         x = lower_bound(QP.begin(), QP.end(), make_pair(ct - PP[i + 1], 0LL)) -             QP.begin();         y = lower_bound(QP.begin(), QP.end(), make_pair(ct - PP[i], 0LL)) -             QP.begin();         AS[x]++;         AS[y]--;         BS[x] -= ct - PP[i + 1], BS[y] += ct - PP[i];       }     }     signed long long aa = 0, bb = 0;     for (i = 0; i < N; i++)       aa += AS[i], bb += BS[i], ret[QP[i].second] = aa * QP[i].first + bb;     for (i = 0; i < N; i++) cout << ret[i] << endl;   }   int main(int argc, char** argv) {     string s;     int i;     if (argc == 1) ios::sync_with_stdio(false);     for (i = 0; i < argc - 1; i++) s += argv[i + 1], s +=   n ;     for (i = 0; i < s.size(); i++) ungetc(s[s.size() - 1 - i], stdin);     solve();     return 0;   }


#include <bits/stdc++.h>   int main() {     int q;     std::cin >> q;     while (q--) {       int n;       std::cin >> n;       std::vector<int> a(n);       for (int i = 0; i < n; ++i) {         std::cin >> a[i];       }       for (int i = 0; i < n; i += 2) {         std::cout << -a[i + 1] <<     << a[i] <<    ;       }       std::cout <<   n ;     }   }