#include <bits/stdc++.h>   using namespace std;   int n, t;   int ask(int r) {     cout <<  ?   << 1 <<     << r << endl;     int ans;     cin >> ans;     return ans;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     srand(time(0));     cin >> n >> t;     vector<int> a(1, t);     int inv = 0, rep = 0;     for (int i = n - 1; i >= 1; i--) {       int t2 = ask(i);       inv ^= 1;       int cur = i - (t - t2 + i) / 2;       if (inv) cur = i - cur;       if (t2 == n - t && rep < 25) {         i++;         rep++;       } else {         a.push_back(cur);         rep = 0;       }       t = t2;     }     reverse(a.begin(), a.end());     cout <<  !   << a[0];     for (int i = 1; i < (int)a.size(); i++) cout << a[i] - a[i - 1];     cout << endl;   }


//a.9   #include<bits/stdc++.h>   #define ll long long   #define all(x) x.begin(),x.end()   #define CASE(t) cout<< Case # <<(t)<< :  ;   #define endll endl   #define endl   n    #define INF 1e18   #define deb(x) cout <<  [  << (#x) <<  =  << x <<  ]  <<   n    #define deb2(x,y) cout <<  [  << (#x) <<  =  << x <<  ] [  << (#y) <<  =  << y <<  ]  <<   n    #define deb3(x,y,z) cout <<  [  << (#x) <<  =  << x <<  ] [  << (#y) <<  =  << y <<  ] [  << (#z) <<  =  << z <<  ]  <<   n    #define popcnt __builtin_popcountll   #define precise(x) fixed << setprecision(x)   #define ios ios_base::sync_with_stdio(false); cin.tie(NULL)   using namespace std;   const ll mod=1000000007;   const ll maxN=50005;      ll ceill(ll a,ll b){return a/b+bool(a%b);}   ll gcd(ll a,ll b){if(b==0)return a;return gcd(b, a % b);}   ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}   ll power(ll x,ll y){ll res=1;while(y>0){if(y&1)res=(res*x)%mod;x=(x*x)%mod;y>>=1;}return res;}   void bin(unsigned n,string &str){str=  ;if(n>1)bin(n>>1,str);str+=to_string(n&1);}   bool isPrime(ll n)   {       if(n<=1)return false;else if(n<=3)return true;else if(n%2==0 || n%3==0)return false;       for(ll i=5;i*i<=n;i=i+6){if(n%i==0 || n%(i+2)==0)return false;}return true;   }      vector<ll>primes;   void SieveOfEratosthenes()   {       bool prime[maxN];primes.clear();       memset(prime, true, sizeof(prime));       for(ll i=2;i*i<maxN;i++)if(prime[i]==true)for(ll j=i*i;j<maxN;j+=i)prime[j]=false;       for(ll i=2;i<maxN;i++)if(prime[i])primes.push_back(i);   }   //vector<ll>fact(maxN);   //void factpre(){fact[0]=1;for(ll i=1;i<maxN;i++)fact[i]=(fact[i-1]*1LL*i)%mod;}   //ll mul(ll a,ll b){return (a*1LL*b)%mod;}   //ll nCr(ll n,ll k){if(k>n)return 0;return mul(fact[n],power(mul(fact[k],fact[n-k]),mod-2));}      /*ll spf[maxN];   void sieve()   {       spf[1]=1;for(ll i=2;i<maxN;i++)spf[i]=i;for(ll i=4;i<maxN;i+=2)spf[i]=2;       for(ll i=3;i*i<maxN;i++){if(spf[i]==i)for(ll j=i*i;j<maxN;j+=i)if(spf[j]==j)spf[j]=i;}   }   vector<ll>getfacts(ll x){vector<ll>vec;while(x!=1)vec.push_back(spf[x]),x=x/spf[x];return vec;}*/      /*ll fen[maxN];   void update(ll i,ll val,ll n){i++;for(;i<=n;i+=i&-i)fen[i]+=val;}   ll get(ll i){i++;ll res=0;for(;i>0;i-=i&-i)res+=fen[i];return res;}*/      void solve()   {       ll a,b;cin>>a>>b;       if(b==1){cout<< NO <<endl;return;}       cout<< YES <<endl;       cout<<a<<   <<a*b<<   <<a*(b+1)<<endl;   }   signed main()   {       ios;       ll tc=1; cin>>tc;       for(ll t=1;t<=tc;t++)       {           //CASE(t);           solve();       }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     int b[n];     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     int c = 0, r = 0;     for (int i = 0; i < n; i++) {       if (a[i] == 0 and b[i] == 1) c++;       if (a[i] == 1 and b[i] == 0) r++;     }     if (r > c) {       cout <<  1  << endl;     } else if (r == 0)       cout <<  -1  << endl;     else {       int val = ceil(((float)(c + 1) / (float)r));       cout << val << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[90];   void error() {     cout << -1 << endl;     exit(0);   }   int main() {     int n, m, l = 1, r;     scanf( %d%d n , &n, &m);     r = n;     while (m--) {       int v;       scanf( To the %s of %d n , s, &v);       if (!strcmp(s,  left )) {         if (l >= v) error();         if (r >= v) r = v - 1;       } else {         if (r <= v) error();         if (l <= v) l = v + 1;       }     }     cout << r - l + 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 10000005;   int sum[MAXN];   bool ok[MAXN];   int n;   char a[200005];   bool grp[200005];   void submit(int s) {     vector<string> ret;     for (int i = 0; i < n; i++) {       if (grp[i]) {         string str;         str.push_back(a[i]);         str.push_back(a[i + 1]);         ret.push_back(str);         i++;         continue;       } else {         string str = {a[i]};         ret.push_back(str);       }     }     for (int i = 0; i < ret.size(); i++) {       printf( %s , ret[i].c_str());       if (i + 1 < ret.size()) printf( + );     }     puts(  );     if (s >= 10) {       vector<int> a;       int bs = sum[s];       while (s) {         a.push_back(s % 10);         s /= 10;       }       reverse(a.begin(), a.end());       for (int i = 0; i < a.size(); i++) {         printf( %d , a[i]);         if (i + 1 < a.size()) printf( + );       }       puts(  );       if (bs >= 10)         printf( %d+%d , bs / 10, bs % 10);       else         printf( %d , bs);     } else {       printf( %d n%d n , s, s);     }     exit(0);   }   int main() {     for (int i = 1; i < MAXN; i++) {       sum[i] = sum[i / 10] + (i % 10);     }     for (int i = 0; i < MAXN; i++) {       if (sum[sum[i]] <= 9) ok[i] = 1;     }     scanf( %d %s , &n, a);     int sum = 0;     for (int i = 0; i < n; i++) sum += a[i] -  0 ;     if (ok[sum]) submit(sum);     for (int i = 0; i + 1 < n; i += 2) {       sum -= a[i] -  0 ;       sum -= a[i + 1] -  0 ;       sum += (a[i] -  0 ) * 10 + (a[i + 1] -  0 );       grp[i] = 1;       if (ok[sum]) submit(sum);     }     for (int i = 0; i + 1 < n; i += 2) {       sum += a[i] -  0 ;       sum += a[i + 1] -  0 ;       sum -= (a[i] -  0 ) * 10 + (a[i + 1] -  0 );       grp[i] = 0;       if (ok[sum]) submit(sum);     }     for (int i = 1; i + 1 < n; i += 2) {       sum -= a[i] -  0 ;       sum -= a[i + 1] -  0 ;       sum += (a[i] -  0 ) * 10 + (a[i + 1] -  0 );       grp[i] = 1;       if (ok[sum]) submit(sum);     }     for (int i = 1; i + 1 < n; i += 2) {       sum += a[i] -  0 ;       sum += a[i + 1] -  0 ;       sum -= (a[i] -  0 ) * 10 + (a[i + 1] -  0 );       grp[i] = 0;       if (ok[sum]) submit(sum);     }     assert(0);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 200000;   int H[N];   vector<int> g[N], b[N];   int ans[N];   int n, q;   map<int, set<pair<int, int> > > m;   void dfs(int v, int h = 0, int p = -1) {     ans[v] = -1;     H[v] = h;     for (auto i : b[v]) {       if (!m[i].empty()) {         auto t = *m[i].begin();         if (ans[v] == -1 || H[ans[v]] < -t.first) ans[v] = t.second;       }       m[i].insert({-h, v});     }     for (auto u : g[v]) {       if (p != u) {         dfs(u, h + 1, v);       }     }     for (auto i : b[v]) {       m[i].erase({-h, v});     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> q;     for (int j = 1; j <= n; j++) {       int w;       cin >> w;       for (int i = 2; i * i <= w; i++) {         if (w % i == 0) {           b[j].push_back(i);           while (w % i == 0) w /= i;         }       }       if (w > 1) b[j].push_back(w);     }     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     dfs(1);     while (q--) {       int t;       cin >> t;       if (t == 2) {         int v, w;         cin >> v >> w;         b[v].clear();         for (int i = 2; i * i <= w; i++) {           if (w % i == 0) {             b[v].push_back(i);             while (w % i == 0) w /= i;           }         }         if (w > 1) b[v].push_back(w);         dfs(1);       } else {         int v;         cin >> v;         cout << ans[v] << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long t, a[200010];   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> t;     while (t--) {       long long n;       set<long long> s;       cin >> n;       for (long long i = 0; i < n; i++) cin >> a[i], a[i] = a[i] % n;       for (long long i = 0; i < n * 3; i++) s.insert(i + (a[i % n]));       if (s.size() != 3 * n)         cout <<  NO  <<   n ;       else         cout <<  YES  <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char **argv) {     ios_base::sync_with_stdio(false);     string s;     cin >> s;     int n = s.size();     long long maxDiff = 0;     long long ans = 0;     int x;     if (n == 1) {       cout << s << endl;       return 0;     }     vector<pair<char, vector<long long> > > a;     char t =  o ;     for (int i = 0; i < n; i += 2) {       x = s[i] -  0 ;       if (((i > 0) && (s[i - 1] ==  * )) || ((i < n - 1) && (s[i + 1] ==  * ))) {         if (t !=  * ) {           t =  * ;           a.push_back(make_pair(t, vector<long long>(1, x)));         } else {           a.back().second.push_back(a.back().second.back() * x);         }         if ((i < n - 1) && (s[i + 1] ==  + )) {           t =  o ;         }       } else {         if (t !=  + ) {           t =  + ;           a.push_back(make_pair(t, vector<long long>(1, x)));         } else {           a.back().second.push_back(a.back().second.back() + x);         }       }     }     n = a.size();     vector<long long> b(n);     for (int i = 0; i < n; i++) {       b[i] = a[i].second.back();       if (i) {         b[i] += b[i - 1];       }     }     ans = b[n - 1];     maxDiff = 0;     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         for (int si = 0; si < a[i].second.size(); si++) {           for (int sj = 0; sj < a[j].second.size(); sj++) {             long long old = b[j];             if (i > 0) {               old -= b[i - 1];             }             long long vLeft, vRight, vMiddle = 0;             vLeft = a[i].second.back();             if (si > 0) {               if (a[i].first ==  + ) {                 vLeft -= a[i].second[si - 1];               } else {                 vLeft /= a[i].second[si - 1];               }             }             vRight = a[j].second[sj];             if (j - 1 > i) {               vMiddle = b[j - 1] - b[i];             }             long long v = vLeft + vRight + vMiddle;             if (si > 0) {               if (a[i].first ==  + ) {                 v += a[i].second[si - 1];               } else {                 v *= a[i].second[si - 1];               }             }             if (sj < a[j].second.size() - 1) {               if (a[j].first ==  + ) {                 v += a[j].second.back() - a[j].second[sj];               } else {                 v *= a[j].second.back() / a[j].second[sj];               }             }             maxDiff = max(maxDiff, v - old);           }         }       }     }     cout << ans + maxDiff << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct TPoint {     double x, y;     TPoint(){};     TPoint(double _x, double _y) {       x = _x;       y = _y;     }   };   struct TVector {     double x, y;     TVector() {}     TVector(TPoint p) {       x = p.x;       y = p.y;     }     TVector(TPoint p1, TPoint p2) {       x = p2.x - p1.x;       y = p2.y - p1.y;     }   };   double cosv(TVector v1, TVector v2) {     return (double)(v2.x * v1.y - v1.x * v2.y);   }   int main() {     int n;     cin >> n;     vector<TPoint> in;     for (int i = 0; i < n; ++i) {       double x, y;       cin >> x >> y;       in.push_back(TPoint(x, y));     }     double maxpl = 0;     for (int i = 0; i < n; ++i) {       for (int j = i + 1; j < n; ++j) {         double lefter = -1, righter = -1;         for (int k = 0; k < n; ++k) {           if (k != i && k != j) {             double cosven = cosv(TVector(in[i], in[j]), TVector(in[i], in[k]));             if (cosven > 0) {               lefter = max(lefter, cosven / 2);             } else {               righter = max(righter, -cosven / 2);             }           }         }         if (lefter > 0 && righter > 0) maxpl = max(maxpl, lefter + righter);       }     }     cout.precision(10);     cout << maxpl << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 400010;   inline long long read() {     long long s = 0, w = 1;     register char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) w = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 )       s = (s << 3) + (s << 1) + (ch -  0 ), ch = getchar();     return s * w;   }   void print(long long x) {     if (x < 0) x = -x, putchar( - );     if (x > 9) print(x / 10);     putchar(x % 10 +  0 );   }   long long n, deg[N], root, cnt;   struct Node {     long long a, b, c;   } h[N];   long long head[N], maxE;   struct Edge {     long long nxt, to, rdis;   } e[N << 1];   inline void Add(long long u, long long v, long long w) {     e[++maxE].nxt = head[u];     head[u] = maxE;     e[maxE].to = v;     e[maxE].rdis = w;   }   long long DFS(long long x, long long fa, long long w) {     long long res;     long long p, q;     res = p = q = 0;     for (long long i = head[x]; i; i = e[i].nxt) {       if (e[i].to == fa) continue;       res += e[i].rdis;       long long gt = DFS(e[i].to, x, e[i].rdis);       (p ? q : p) = gt;     }     if (x == root) return p;     if (!p)       p = x, h[++cnt] = (Node){p, root, w};     else {       h[++cnt] = (Node){p, q, (res - w) / 2};       h[++cnt] = (Node){p, root, (w - res) / 2};       h[++cnt] = (Node){q, root, (w - res) / 2};     }     return p;   }   signed main() {     n = read();     for (register long long i = 1; i < n; i++) {       long long u, v, w;       u = read(), v = read(), w = read();       Add(u, v, w), Add(v, u, w);       deg[u]++, deg[v]++;     }     for (register long long i = 1; i <= n; i++) {       if (deg[i] == 2) {         puts( NO );         return 0;       }       if (deg[i] == 1) root = i;     }     puts( YES );     DFS(root, 0, 0);     printf( %lld n , cnt);     for (register long long i = 1; i <= cnt; i++)       printf( %lld %lld %lld n , h[i].a, h[i].b, h[i].c);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool chkmin(T& a, T b) {     return a > b ? a = b, true : false;   }   template <class T>   bool chkmax(T& a, T b) {     return a < b ? a = b, true : false;   }   template <class T>   void read(T& a) {     char c = getchar_unlocked();     T f = 1;     a = 0;     for (; !isdigit(c); c = getchar_unlocked())       if (c ==  - ) f = -1;     for (; isdigit(c); c = getchar_unlocked()) a = a * 10 + c -  0 ;     a *= f;   }   const int maxN = 2e5 + 5;   int N;   pair<int, int> a[maxN];   map<int, int> f;   map<pair<int, int>, int> g;   int main() {     read(N);     for (int i = (1), _ = (N); i <= _; ++i) read(a[i].first), read(a[i].second);     for (int i = (1), _ = (N); i <= _; ++i) f[a[i].first]++;     long long ans = 0;     for (map<int, int>::iterator it = f.begin(); it != f.end(); ++it)       ans += (long long)it->second * (it->second - 1) / 2;     f.clear();     for (int i = (1), _ = (N); i <= _; ++i) f[a[i].second]++;     for (map<int, int>::iterator it = f.begin(); it != f.end(); ++it)       ans += (long long)it->second * (it->second - 1) / 2;     for (int i = (1), _ = (N); i <= _; ++i) g[a[i]]++;     for (map<pair<int, int>, int>::iterator it = g.begin(); it != g.end(); ++it)       ans -= (long long)(it->second) * (it->second - 1) / 2;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long read() {     long long x = 0, f = 0;     char ch = getchar();     while (!isdigit(ch)) f = ch ==  - , ch = getchar();     while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();     return f ? -x : x;   }   const int N = 1005;   int n, k, b;   long long a[N][N], c[N][N];   int x[N], y[N];   int main() {     k = read(), b = 1 << k;     for (int i = (0); i <= (b - 1); i++)       for (int j = (0); j <= (b - 1); j++) a[i][j] = read();     n = read();     for (int i = (1); i <= (n); i++) x[i] = read(), y[i] = read();     for (int d = (0); d <= (k - 1); d++) {       memset(c, 0, sizeof(c));       for (int i = (0); i <= (b - 1); i++)         for (int j = (0); j <= (b - 1); j++)           for (int t = (1); t <= (n); t++)             c[(i + (x[t] << d)) & (b - 1)][(j + (y[t] << d)) & (b - 1)] ^=                 a[i][j];       swap(a, c);     }     int c = 0;     for (int i = (0); i <= (b - 1); i++)       for (int j = (0); j <= (b - 1); j++)         if (a[i][j]) c++;     cout << c << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, is, it;   vector<int> res;   string s, t;   int getis() {     for (int i = it; i < t.size(); ++i) {       if (s[i] == t[it]) return i;     }     return -1;   }   int main() {     cin >> n >> s >> t;     while (s != t) {       is = getis();       if (is == -1) {         cout << -1;         return 0;       }       while (is >= 0 && is > it) {         res.push_back(is - 1);         swap(s[is - 1], s[is]);         is--;       }       it++;     }     if (res.size() > 10000) {       cout << -1;       return 0;     }     cout << res.size() << endl;     for (int i : res) cout << i + 1 <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     bool flag = 1;     int c = 0, a[] = {5, 7, 5}, k = 0;     map<char, bool> m{{ a , 1}, { e , 1}, { o , 1}, { u , 1}, { i , 1}};     while (getline(cin, s)) {       c = 0;       for (int i = 0; i < s.size(); i++) c += m[s[i]];       if (c != a[k]) flag = 0;       k++;     }     if (flag == 1)       cout <<  YES ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int a, b;     cin >> a >> b;     stringstream ss;     ss << b;     string str;     str = ss.str();     reverse((str).begin(), (str).end());     stringstream ss1;     ss1 << str;     ss1 >> b;     cout << a + b << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   T big(T a, T b) {     return a > b ? a : b;   }   long long gcd(long long a, long long b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   bool mod(long double a, long double b) { return a / b - floor(a / b); }   long long occurs(vector<long long> v, long long n) {     auto it = lower_bound(((v)).begin(), ((v)).end(), (n));     auto it1 = upper_bound(((v)).begin(), ((v)).end(), (n));     long long x = it1 - it;     return x;   }   long long logb(long long base, long long x) { return (log(x) / log(base)); }   long long pow(long long n, long long m) {     long long ans = 1;     for (long long i{0}; i < m; i++) {       ans *= n;     }     return ans;   }   long long factors(long long ans) {     long long count = 0;     for (long long i = 1; i <= sqrt(ans); i++) {       if (ans % i == 0) {         if (ans / i == i)           count++;         else           count += 2;       }     }     return count;   }   bool isSubSequence(string str1, string str2, long long m, long long n) {     if (m == 0) return true;     if (n == 0) return false;     if (str1[m - 1] == str2[n - 1])       return isSubSequence(str1, str2, m - 1, n - 1);     return isSubSequence(str1, str2, m, n - 1);   }   long long log2(long long n) {     long long count = 0;     while (n != 0) {       n /= 2;       count++;     }     return count - 1;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n, m;     cin >> n >> m;     string s;     cin >> s;     if (n == 1 && m != 0) {       cout << 0;       return 0;     }     if (m == 0) {       for (long long i{0}; i < n; i++) {         cout << s[i];       }       return 0;     }     if (s[0] ==  1 ) {       m++;     }     cout << 1;     for (long long i{1}; i < m; i++) {       if (s[i] ==  0 ) {         m++;       }       cout << 0;       if (i == n - 1) break;     }     if (m <= n) {       for (long long i{m}; i < n; i++) {         cout << s[i];       }     }     return 0;   }


#include <bits/stdc++.h>   using std::cin;   using std::cout;   struct Item {     int parent;     int count;   };   int Find(int x, std::vector<Item>* items) {     if (items->at(x).parent == x) {       return x;     }     return items->at(x).parent = Find(items->at(x).parent, items);   }   void Unite(int x, int y, std::vector<Item>* items) {     assert(x == Find(x, items));     assert(y == Find(y, items));     if (items->at(x).count < items->at(y).count) {       std::swap(x, y);     }     items->at(y).parent = x;     items->at(x).count += items->at(y).count;   }   int main() {     std::ios_base::sync_with_stdio(false);     int N, M;     cin >> N >> M;     std::vector<std::pair<int, int>> A(N);     std::vector<Item> items(N);     for (int i = 0; i < N; ++i) {       int a;       cin >> a;       A[i].first = a;       A[i].second = i;       items[i].parent = i;       items[i].count = 1;     }     std::vector<std::vector<int>> gr(N);     for (int i = 0; i < M; ++i) {       int a, b;       cin >> a >> b;       --a;       --b;       gr[a].push_back(b);       gr[b].push_back(a);     }     std::sort(A.begin(), A.end());     int64_t answer = 0;     std::vector<int> visited(N, false);     for (int index = N - 1; index >= 0; --index) {       int value = A[index].first;       int vertex = A[index].second;       int64_t sum = 0, sum_sq = 0;       for (int neib : gr[vertex]) {         int x = Find(vertex, &items);         int y = Find(neib, &items);         if (x == y) {           continue;         }         if (!visited[y]) {           continue;         }         sum += items[y].count;         sum_sq += (int64_t)items[y].count * items[y].count;         Unite(x, y, &items);       }       answer += (int64_t)value * (sum * sum - sum_sq + 2 * sum);       visited[Find(vertex, &items)] = true;     }     long double result = (long double)answer / ((int64_t)N * (N - 1));     cout.precision(6);     cout << std::fixed << result <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, i, c, x, y, z, k, h;   int main() {     cin >> x >> y;     for (a = 0; a <= 1000; a++) {       for (b = 0; b <= 1000; b++)         if (a * a + b == x && a + b * b == y) h++;     }     cout << h;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 999999;   const double Eps = 1e-12;   const double PI = acos(-1.0);   int gcd(int x, int y) { return x % y == 0 ? y : gcd(y, x % y); }   long long pe[2222], key[2222];   char a[100], b[100];   int p[11111];   using namespace std;   int num[200004], ans[200005];   int main() {     int n;     while (cin >> n) {       for (int i = 1; i <= n; i++) scanf( %d , num + i);       memset(ans, -1, sizeof(ans));       ans[1] = 0;       queue<int> q;       q.push(1);       while (!q.empty()) {         int s = q.front();         q.pop();         for (int i = -1; i <= 1; ++i) {           if (i == 0) continue;           int d = s + i;           if (d >= 1 && d <= n && ans[d] == -1) {             ans[d] = ans[s] + 1;             q.push(d);           }         }         int d = num[s];         if (ans[d] == -1) {           ans[d] = ans[s] + 1;           q.push(d);         }       }       cout << ans[1];       for (int i = 2; i <= n; i++) printf(  %d , ans[i]);       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string in =  111 ;     cin >> in;     vector<vector<int>> result;     set<int> last1, last0;     for (int i = 0; i < in.size(); i++) {       if (in[i] ==  0 ) {         if (last1.empty()) {           result.push_back({i});           last0.insert(result.size() - 1);         } else {           int cur = *last1.begin();           result[cur].push_back(i);           last1.erase(cur);           last0.insert(cur);         }       } else {         if (last0.empty()) {           cout << -1;           return 0;         } else {           int cur = *last0.begin();           result[cur].push_back(i);           last0.erase(cur);           last1.insert(cur);         }       }     }     if (last1.size()) {       cout << -1;       return 0;     }     cout << result.size() << endl;     for (auto &it : result) {       cout << it.size() <<    ;       for (auto it2 : it) {         cout << (it2 + 1) <<    ;       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   struct abcd {     int to, next;   } table[300300 << 1];   int head[300300], tot;   int n, m;   char ans[300300];   void Add(int x, int y) {     table[++tot].to = y;     table[tot].next = head[x];     head[x] = tot;   }   void DFS(int x) {     int i, cnt = 0;     for (i = head[x]; i; i = table[i].next)       if (ans[table[i].to] == ans[x]) ++cnt;     if (cnt >= 2) {       ans[x] ^= 1;       for (i = head[x]; i; i = table[i].next) DFS(table[i].to);     }   }   int main() {     vector<int>::iterator it;     int i, x, y;     cin >> n >> m;     for (i = 1; i <= m; i++) {       scanf( %d%d , &x, &y);       Add(x, y);       Add(y, x);     }     for (i = 1; i <= n; i++) ans[i] =  0 ;     for (i = 1; i <= n; i++) DFS(i);     printf( %s n , ans + 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int INF = 2e9;   vector<string> v;   int n, m;   int diff(string a, string b) {     int i, cc = 0;     for (i = 0; i < m; ++i) cc += (a[i] != b[i]);     return cc;   }   string solve() {     string s = v[0], t;     int i, j, cc;     char c;     bool f;     for (i = 0; i < m; ++i) {       t = s;       for (c =  a ; c <=  z ; ++c) {         t[i] = c;         cc = 0;         f = 1;         for (j = 0; j < n; ++j) {           cc = diff(t, v[j]);           if (cc > 1) f = 0;         }         if (f) return t;       }     }     return  -1 ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int tc, ti = 1;     cin >> tc;     while (tc--) {       int i, j;       cin >> n >> m;       v.resize(n);       for (i = 0; i < n; ++i) cin >> v[i];       cout << solve() <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000010;   int a[MAXN], parent[MAXN], rnk[MAXN];   vector<int> arr[MAXN];   vector<int> ans;   void make_set(int v) {     parent[v] = v;     rnk[v] = 0;   }   int find_set(int v) {     if (v == parent[v]) return v;     return parent[v] = find_set(parent[v]);   }   void union_sets(int a, int b) {     a = find_set(a);     b = find_set(b);     if (a != b) {       if (rnk[a] < rnk[b]) swap(a, b);       parent[b] = a;       if (rnk[a] == rnk[b]) rnk[a]++;     }   }   int main() {     int n, m;     cin >> n >> m;     ans.clear();     ans.resize(n);     for (int i = 0; i < n; i++) {       cin >> a[i];       make_set(i);     }     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       u--, v--;       union_sets(u, v);     }     for (int i = 0; i < n; i++) {       int st = find_set(i);       arr[st].push_back(a[i]);     }     for (int i = 0; i < n; i++) {       sort(arr[i].begin(), arr[i].end());     }     for (int i = 0; i < n; i++) {       int st = find_set(i);       int x = arr[st].back();       arr[st].pop_back();       ans[i] = x;     }     for (int i = 0; i < n; i++) {       cout << ans[i] <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 9;   long long c[4005][4005];   void getc() {     memset(c, 0, sizeof(c));     c[0][0] = 1;     for (int i = 1; i < 4001; i++) {       c[i][0] = 1;       for (int j = 1; j <= i; j++) {         c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;       }     }   }   long long a[4005];   void geta() {     a[0] = 1;     a[1] = 1;     for (long long i = 2; i <= 4000; i++) a[i] = a[i - 1] * i % mod;   }   int main() {     getc();     geta();     int n, w, b;     while (scanf( %d%d%d , &n, &w, &b) == 3) {       int x = max(1, n - w), y = min(b, n - 2);       long long ans = 0;       for (int i = x; i <= y; i++) {         ans = (ans +                c[b - 1][i - 1] * c[w - 1][n - i - 1] % mod * (n - i - 1) % mod) %               mod;       }       ans = ans * a[w] % mod * a[b] % mod;       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = (long long)1e9 + 7;   long long dp[12][65][1024], dp0[12][65][1024];   void upd(long long &a, long long b) { a = (a + b); }   int get_next(int a, int b) {     if ((a & (1 << b)) == 0) {       a += (1 << b);     } else {       a -= (1 << b);     }     return a;   }   void precalc() {     for (int i = 2; i <= 10; ++i) {       dp[i][0][0] = 1;       dp0[i][0][0] = 1;     }     for (int osn = 2; osn <= 10; ++osn) {       for (int len = 0; len <= 62; ++len) {         for (int mask = 0; mask < (1 << osn); ++mask) {           for (int next = 0; next < osn; ++next) {             upd(dp[osn][len + 1][get_next(mask, next)], dp[osn][len][mask]);             if (next == 0 && len == 0) {               continue;             }             upd(dp0[osn][len + 1][get_next(mask, next)], dp0[osn][len][mask]);           }         }       }     }   }   int digits[1000];   int cur[10];   int build(int x) {     int ans = 0;     for (int i = x - 1; i >= 0; --i) {       ans = ans * 2 + cur[i] % 2;     }     return ans;   }   long long f(long long n, long long x) {     int cnt = 0;     if (n == 0) {       return 0;     }     while (n > 0) {       ++cnt;       digits[cnt] = n % x;       n /= x;     }     reverse(digits + 1, digits + cnt + 1);     memset(cur, 0, sizeof cur);     long long answer = 0;     for (int i = 1; i <= cnt; ++i) {       for (int new_digit = 0; new_digit < x; ++new_digit) {         if (new_digit == 0 && i == 1) continue;         if (new_digit < digits[i]) {           ++cur[new_digit];           int cur_mask = build(x);           answer = answer + dp[x][cnt - i][cur_mask];           --cur[new_digit];         } else {           ++cur[new_digit];           break;         }       }     }     int cur_mask = build(x);     if (cur_mask == 0) ++answer;     for (int i = 1; i < cnt; ++i) {       answer += dp0[x][i][0];     }     return answer;   }   void solve(long long b, long long l, long long r) {     long long ans = f(r, b) - f(l - 1, b);     cout << ans << endl;   }   int main() {     precalc();     ios_base::sync_with_stdio(0);     int test;     cin >> test;     for (int tst = 1; tst <= test; ++tst) {       long long b, l, r;       cin >> b >> l >> r;       solve(b, l, r);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3610, mod = 998244353;   template <typename T>   void read(T &x) {     int ch = getchar();     x = 0;     for (; ch <  0  || ch >  9 ; ch = getchar())       ;     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ;   }   void qmo(int &x) { x += x >> 31 & mod; }   int ksm(int a, int b) {     int res = 1;     for (; b; b >>= 1, a = (long long)a * a % mod)       if (b & 1) res = (long long)res * a % mod;     return res;   }   int n, m, k, cnt1, cnt2, f[N][N >> 1], g[N][N >> 1], fac[N], inv[N], ans;   bool vis1[N], vis2[N];   void init(int n) {     fac[0] = 1;     for (int i = 1; i <= n; ++i) fac[i] = (long long)fac[i - 1] * i % mod;     inv[n] = ksm(fac[n], mod - 2);     for (int i = n; i; --i) inv[i - 1] = (long long)inv[i] * i % mod;   }   int main() {     read(n);     read(m);     read(k);     for (int i = 1, a1, b1, a2, b2; i <= k; ++i) {       read(a1);       read(b1);       read(a2);       read(b2);       vis1[a1] = vis1[a2] = vis2[b1] = vis2[b2] = true;     }     init(max(n, m));     for (int i = 0; i <= n; ++i) f[i][0] = 1;     for (int i = 1; i <= n; ++i)       if (!vis1[i]) ++cnt1;     for (int i = 0; i <= m; ++i) g[i][0] = 1;     for (int i = 1; i <= m; ++i)       if (!vis2[i]) ++cnt2;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= (i >> 1); ++j) {         f[i][j] = f[i - 1][j];         if (j && !vis1[i] && !vis1[i - 1]) qmo(f[i][j] += f[i - 2][j - 1] - mod);       }     for (int i = 1; i <= m; ++i)       for (int j = 1; j <= (i >> 1); ++j) {         g[i][j] = g[i - 1][j];         if (j && !vis2[i] && !vis2[i - 1]) qmo(g[i][j] += g[i - 2][j - 1] - mod);       }     for (int i = 0; (i << 1) <= cnt1; ++i)       for (int j = 0; (i << 1) + j <= cnt1 && (j << 1) + i <= cnt2; ++j)         ans = (ans + (long long)f[n][i] * g[m][j] % mod * fac[cnt2 - (j << 1)] %                          mod * inv[cnt2 - (j << 1) - i] % mod *                          fac[cnt1 - (i << 1)] % mod * inv[cnt1 - (i << 1) - j]) %               mod;     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void chmin(T &t, const T &f) {     if (t > f) t = f;   }   template <class T>   void chmax(T &t, const T &f) {     if (t < f) t = f;   }   int main() {     long long int n, a, c1, c2, m, len = 0, prev = 0, ans = 0, rlow, rlen, rind,                                    ret[100005];     vector<pair<long long int, long long int> > v;     vector<long long int> sum;     scanf( %I64d %I64d %I64d %I64d %I64d , &n, &a, &c1, &c2, &m);     for (long long int i = 0; i < n; ++i) {       long long int x;       scanf( %I64d , &x);       v.push_back(make_pair(x, i));     }     if (n == 1) {       long long int maxi = min(a, v[0].first + m);       printf( %I64d n , c1 * (maxi == a ? 1 : 0) + maxi * c2);       printf( %I64d  , maxi);       return 0;     }     sort(v.begin(), v.end());     sum.push_back(0);     for (int i = 1; i < n; ++i)       sum.push_back((v[i].first - v[i - 1].first) * i + sum[i - 1]);     while (len <= n) {       if (len - 1 >= 0) prev += a - v[n - len].first;       if (prev > m) break;       long long int newm = m - prev;       long long int ind = upper_bound(sum.begin(), sum.end(), newm) - sum.begin();       ind = max(0ll, min((long long int)ind - 1, (long long int)n - len - 1));       newm -= sum[ind];       long long int low = min(v[ind].first + (newm / (ind + 1)), a);       if (ans < len * c1 + low * c2) {         ans = len * c1 + low * c2;         rlow = low;         rind = ind;         rlen = len;       }       len++;     }     for (int i = 0; i < n; ++i) ret[v[i].second] = v[i].first;     for (int i = 0; i <= rind; ++i) ret[v[i].second] = max(v[i].first, rlow);     for (int i = n - 1; i >= n - rlen; --i) ret[v[i].second] = a;     printf( %I64d n , ans);     for (int i = 0; i < n; ++i) printf( %I64d  , ret[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int max_of_num;   int n, test_Q, point_num[100005], f_delp[100005][2];   vector<int> vc[100005];   bool flag = false;   void search1(int u, int fa) {     for (int i = 0; i < vc[u].size(); i++) {       int v = vc[u][i];       if (v != fa) {         if (point_num[v] == 1) {           if (f_delp[u][0]) {             flag = true;           }           f_delp[u][1] = true;         } else {           search1(v, u);           if (f_delp[v][1] && f_delp[u][1]) {             flag = true;           }           if (f_delp[v][0] && f_delp[u][0]) {             flag = true;           }           f_delp[u][0] = f_delp[u][0] | f_delp[v][1];           f_delp[u][1] = f_delp[u][1] | f_delp[v][0];         }       }     }   }   void connect_add(int u = 0, int v = 0) {     scanf( %d%d , &u, &v);     point_num[u]++;     point_num[v]++;     vc[u].push_back(v);     vc[v].push_back(u);   }   void search2(int u, int fa) {     int qaq = 0;     for (int i = 0; i < vc[u].size(); i++) {       int v = vc[u][i];       if (v != fa) {         if (point_num[v] == 1)           qaq++;         else           search2(v, u);       }     }     if (qaq) max_of_num = max_of_num - (qaq - 1);   }   int main() {     scanf( %d , &n);     for (int i = 2; i <= n; i++) {       connect_add();     }     test_Q = 0;     for (int i = 1; i <= n; ++i) {       if (point_num[i] > 1) {         test_Q = i;       }     }     search1(test_Q, 0);     if (flag) {       printf( 3  );     } else {       printf( 1  );     }     max_of_num = n - 1;     search2(test_Q, 0);     printf( %d n , max_of_num);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int const MAX_CH = 110;   char st1[MAX_CH], st2[MAX_CH], st3[MAX_CH];   int dp[MAX_CH][MAX_CH][MAX_CH];   int new_pref_len[MAX_CH][30];   struct {     int i, j, k;     char ad_ch;   } from[MAX_CH][MAX_CH][MAX_CH];   int main() {     gets(st1);     int len_1 = (int)strlen(st1);     gets(st2);     int len_2 = (int)strlen(st2);     gets(st3);     int len_3 = (int)strlen(st3);     for (int taked = 0; taked < len_3; taked++)       for (int ch = 0; ch < 26; ch++) {         string cur;         for (int j = 0; j < taked; j++) cur += st3[j];         cur += (char)( A  + ch);         int mx_pref = 0;         for (int bg = 0; bg < (int)cur.length(); bg++) {           int Ok = 1;           for (int r = 0; r < len_3 && bg + r < (int)cur.length(); r++)             if (st3[r] != cur[bg + r]) {               Ok = 0;               break;             }           if (Ok) mx_pref = max(mx_pref, ((int)cur.length()) - bg);         }         new_pref_len[taked][ch] = mx_pref;       }     for (int i = 0; i < MAX_CH; i++)       for (int j = 0; j < MAX_CH; j++)         for (int k = 0; k < MAX_CH; k++) dp[i][j][k] = -1;     for (int i = 0; i < MAX_CH; i++) {       dp[i][0][0] = 0;       dp[0][i][0] = 0;     }     for (int i = 1; i <= len_1; i++)       for (int j = 1; j <= len_2; j++)         for (int k = 0; k < len_3; k++) {           if (st1[i - 1] == st2[j - 1]) {             int new_k;             if (st3[k] == st1[i - 1])               new_k = k + 1;             else               new_k = new_pref_len[k][st1[i - 1] -  A ];             if (new_k >= len_3)               ;             else if (dp[i - 1][j - 1][k] + 1 > dp[i][j][new_k]) {               dp[i][j][new_k] = dp[i - 1][j - 1][k] + 1;               from[i][j][new_k].i = i - 1;               from[i][j][new_k].j = j - 1;               from[i][j][new_k].k = k;               from[i][j][new_k].ad_ch = st1[i - 1];             }           }           if (dp[i - 1][j][k] > dp[i][j][k]) {             dp[i][j][k] = dp[i - 1][j][k];             from[i][j][k].i = i - 1;             from[i][j][k].j = j;             from[i][j][k].k = k;             from[i][j][k].ad_ch = 0;           }           if (dp[i][j - 1][k] > dp[i][j][k]) {             dp[i][j][k] = dp[i][j - 1][k];             from[i][j][k].i = i;             from[i][j][k].j = j - 1;             from[i][j][k].k = k;             from[i][j][k].ad_ch = 0;           }         }     int cur_i = len_1, cur_j = len_2, cur_k = -1;     int mx = -1;     for (int k = 0; k < len_3; k++)       if (dp[len_1][len_2][k] > mx) {         mx = dp[len_1][len_2][k];         cur_k = k;       }     if (mx <= 0) {       printf( 0 );       return 0;     }     string ans;     while (cur_i >= 1 && cur_j >= 1) {       if (from[cur_i][cur_j][cur_k].ad_ch > 0)         ans += from[cur_i][cur_j][cur_k].ad_ch;       int new_i = from[cur_i][cur_j][cur_k].i;       int new_j = from[cur_i][cur_j][cur_k].j;       int new_k = from[cur_i][cur_j][cur_k].k;       cur_i = new_i;       cur_j = new_j;       cur_k = new_k;     }     for (int i = ((int)ans.length()) - 1; i >= 0; i--) printf( %c , ans[i]);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   long long int invmod(long long int x, long long int n, long long int mod) {     if (n == 0) return 1 % mod;     long long int half = invmod(x, n / 2, mod);     half = (half * half) % mod;     if (n % 2 == 1) half = (half * (x % mod)) % mod;     return half;   }   long long int bin(vector<long long int> &a, long long int l, long long int r,                     long long int x) {     if (l <= r) {       long long int m = l + (r - l) / 2;       if (a[m] == x)         return m;       else if (a[m] > x)         return bin(a, l, m - 1, x);       else         return bin(a, m + 1, r, x);     } else       return -1;   }   long long int power(long long int b, long long int e, long long int m) {     if (e == 0) return 1;     if (e % 2)       return b * power(b * b % m, (e - 1) / 2, m) % m;     else       return power(b * b % m, e / 2, m);   }   long long int power(long long int b, long long int e) {     if (e == 0) return 1;     if (e % 2)       return b * power(b * b, (e - 1) / 2);     else       return power(b * b, e / 2);   }   long long int ncr(long long int n, long long int p) {     long long int r = min(p, n - p), rf = 1, ln = 1;     for (long long int i = 1; i <= r; i++) rf = rf * i;     for (long long int i = 0; i < r; i++) ln = ln * (n - i);     return ln / rf;   }   bool sbs(pair<long long int, long long int> &a,            pair<long long int, long long int> &b) {     return (a.second < b.second);   }   bool sbds(pair<long long int, long long int> &a,             pair<long long int, long long int> &b) {     return (a.second > b.second);   }   long long int SUM(vector<long long int> &a, long long int n) {     long long int sum = 0;     for (long long int i = 0; i < n; i++) sum += a[i];     return sum;   }   long long int chkprm(long long int n) {     long long int x = 5, s = sqrt(n);     if (n < 2) return 0;     if (n < 4) return 1;     if ((n & 1) == 0) return 0;     if (n % 3 == 0) return 0;     while (x <= s) {       if (n % x == 0) return 0;       x += 2;       if (n % x == 0) return 0;       x += 4;     }     return 1;   }   long long int merge(long long int arr[], long long int temp[],                       long long int left, long long int mid,                       long long int right) {     long long int inv_count = 0;     long long int i = left;     long long int j = mid;     long long int k = left;     while ((i <= mid - 1) && (j <= right)) {       if (arr[i] <= arr[j])         temp[k++] = arr[i++];       else {         temp[k++] = arr[j++];         inv_count = inv_count + (mid - i);       }     }     while (i <= mid - 1) temp[k++] = arr[i++];     while (j <= right) temp[k++] = arr[j++];     for (i = left; i <= right; i++) arr[i] = temp[i];     return inv_count;   }   long long int _mergeSort(long long int arr[], long long int temp[],                            long long int left, long long int right) {     long long int mid, inv_count = 0;     if (right > left) {       mid = (right + left) / 2;       inv_count = _mergeSort(arr, temp, left, mid);       inv_count += _mergeSort(arr, temp, mid + 1, right);       inv_count += merge(arr, temp, left, mid + 1, right);     }     return inv_count;   }   long long int countSwaps(long long int arr[], long long int n) {     long long int temp[n];     return _mergeSort(arr, temp, 0, n - 1);   }   void myth() {     long long int n;     cin >> n;     string s;     cin >> s;     set<long long int> s1[26];     for (long long int i = 0; i < n; i++) s1[s[i] -  a ].insert(i + 1);     vector<long long int> cnt(n + 1);     reverse(s.begin(), s.end());     for (long long int i = 0; i < n; i++) {       cnt[*s1[s[i] -  a ].begin()] = i + 1;       s1[s[i] -  a ].erase(s1[s[i] -  a ].begin());     }     long long int a[n];     for (long long int i = 1; i <= n; i++) a[i - 1] = cnt[i];     cout << countSwaps(a, n);   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int test_case = 1;     while (test_case--) {       myth();       cout <<   n ;     }     cerr <<   n           <<  Time Elasped :   << 1.0 * clock() / CLOCKS_PER_SEC <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   void cmin(long long& x, long long y) {     if (x > y) {       x = y;     }   }   int n, a[N];   long long f[N][2];   bool best[N][2];   vector<int> adj[N], answer;   void dfs1(int u, int p) {     long long min_diff = 0;     bool go = false;     for (auto v : adj[u]) {       if (v != p) {         go = true;         dfs1(v, u);         f[u][0] += f[v][0];         f[u][1] += f[v][0];         cmin(min_diff, f[v][1] - f[v][0]);       }     }     f[u][1] += min_diff;     if (!go) {       f[u][0] = a[u];     } else {       cmin(f[u][0], f[u][1] + a[u]);     }   }   void dfs2(int u, int p) {     if (f[u][0] == f[u][1] + a[u]) {       best[u][1] |= best[u][0];       if (best[u][0]) {         answer.push_back(u);       }     }     long long sumf = 0;     for (auto v : adj[u]) {       if (v != p) {         sumf += f[v][0];       }     }     vector<int> diff_nodes;     for (auto v : adj[u]) {       if (v != p) {         if (sumf == f[u][0]) {           best[v][0] |= best[u][0];         }         if (sumf + f[v][1] - f[v][0] == f[u][1] && best[u][1]) {           best[v][1] = true;           diff_nodes.push_back(v);         } else if (sumf + f[v][1] - f[v][0] + a[u] == f[u][0] && best[u][0]) {           diff_nodes.push_back(v);         }       }     }     for (auto v : adj[u]) {       if (v != p) {         if (diff_nodes.size() > 1) {           best[v][0] |= true;         } else if (diff_nodes.size() == 1 && v != diff_nodes.back()) {           best[v][0] |= true;         }         dfs2(v, u);       }     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);     }     for (int i = 1; i < n; ++i) {       int u, v;       scanf( %d%d , &u, &v);       adj[u].push_back(v);       adj[v].push_back(u);     }     dfs1(1, 0);     best[1][0] = true;     dfs2(1, 0);     printf( %lld %d n , f[1][0], answer.size());     sort(answer.begin(), answer.end());     for (auto v : answer) {       printf( %d  , v);     }     return puts(  ), 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   const int INF = (int)(1e9 + 1e6);   const long long LINF = (long long)(4e18);   const double EPS = 1e-9;   const long long mod = 1e9 + 7;   mt19937 ggen;   void solve();   int main() {     iostream::sync_with_stdio(false);     cin.tie(0);     ggen = mt19937(1337);     solve();   }   void solve() {     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     int x, f;     cin >> x >> f;     long long ans = 0;     long long ft = x + f;     for (int i = 0; i < n; ++i) {       long long gh = static_cast<long long>(a[i]);       long long mt = gh / ft;       ans += mt * f;       if (gh - mt * ft > x) {         ans += f;       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int x, y;     cin >> x >> y;     int tx = x - y;     int ty = x + y;     x = tx;     y = ty;     if (x < 0)       cout <<  -1 n ;     else {       int l = 1, r = 1e9 + 5;       while (r - l > 1) {         int mid = (l + r) / 2;         if ((1LL * y * (mid - 1) + mid - 1) / mid <= x)           l = mid;         else           r = mid;       }       long double ans = y;       ans /= l;       ans /= 2.0;       cout << setprecision(19) << fixed << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int nxt() {     int res;     scanf( %d , &res);     return res;   }   bool is_perm(int *p, int n) {     char used[n];     memset(used, 0, sizeof(used));     for (int i = 0; i < n; ++i) {       used[p[i]] = 1;     }     for (int i = 0; i < n; ++i) {       if (!used[i]) {         return false;       }     }     return true;   }   void solve(int n) {     int a[n];     for (int i = 0; i < n; ++i) {       a[i] = i + 1;     }     do {       int p[n];       p[0] = a[0] % n;       for (int i = 1; i < n; ++i) {         p[i] = p[i - 1] * a[i] % n;       }       if (is_perm(p, n)) {         for (int i = 0; i < n; ++i) {           cout << a[i] <<    ;         }         cout << endl;       }     } while (next_permutation(a, a + n - 1));   }   void solveTest(int n) {     cout <<  YES n ;     if (n == 1) {       cout <<  1 n ;       return;     }     if (n == 2) {       cout <<  1 n2 n ;       return;     }     if (n == 3) {       cout <<  1 n2 n3 n ;       return;     }     if (n == 4) {       cout <<  1 n3 n2 n4 n ;       return;     }   }   bool isprime(int n) {     for (int i = 2; i * i <= n; ++i) {       if (n % i == 0) {         return false;       }     }     return true;   }   long long powMod(long long a, int n, long long mod) {     long long ret = 1;     while (n) {       if (n & 1) ret = ret * a % mod;       n >>= 1;       a = a * a % mod;     }     return ret;   }   void solve() {     int n = nxt();     if (n <= 4) {       solveTest(n);       return;     }     if (!isprime(n)) {       cout <<  NO n ;       return;     }     int ans[n];     ans[0] = 1;     ans[n - 1] = n;     long long pw = 1;     for (int i = 1; i + 1 < n; ++i) {       ans[i] = (i + 1) * powMod(pw, n - 2, n) % n;       pw = pw * ans[i] % n;     }     cout <<  YES n ;     for (int i = 0; i < n; ++i) {       cout << ans[i] <<   n ;     }   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a, b, i, c = 0;     a = 8 * n;     b = 9 * n;     c = b - a;     cout << b <<     << a << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     std::string data;     cin >> data;     long long int counter = 0;     char ch;     for (int i = 0; i < data.length(); i++) {       if (data[i] ==  0 )         counter++;       else         counter = 0;     }     std::string to_join;     while (counter != 0) {       counter--;       data =  0  + data;     }     int flag = 0;     for (int i = 0; i < data.length() / 2; i++) {       if (data[i] != data[data.length() - i - 1]) {         flag = 1;         break;       }     }     if (flag)       cout <<  NO ;     else       cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int f[20];   bool ischange[300000];   int main() {     memset(ischange, 0, sizeof ischange);     int n;     scanf( %d , &n);     string a;     cin >> a;     for (int i = 1; i <= 9; i++) {       scanf( %d , &f[i]);     }     int ff = 0;     for (int i = 0; i < a.length(); i++) {       if (ff == 1) break;       int now = a[i] -  0 ;       int change = f[now];       if (change > now) {         ff = 1;         while (f[a[i] -  0 ] >= a[i] -  0  && i < a.length()) {           ischange[i] = 1;           i++;         }       }       if (ff == 1) break;     }     for (int i = 0; i < a.length(); i++) {       if (ischange[i])         cout << f[a[i] -  0 ];       else         cout << a[i];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void show(T *a, int n) {     for (int i = 0; i < n; i++) cout << a[i] <<     ;     puts(  );   }   template <class T>   void queue_clear(T *a, int n) {     for (int i = 0; i < n; i++) {       while (!a->empty()) a->pop();     }   }   template <class T>   void Bit_add(T *a, int x, int p, int MAX) {     while (p < MAX) {       a[p] += x;       p += p & (-p);     }   }   template <class T>   T Bit_sum(T *a, int p) {     T ret = 0;     while (p) {       ret += a[p];       p -= p & (-p);     }     return ret;   }   const int MAXN = 1007;   int d[MAXN], s[MAXN];   int main() {     int m, k;     while (~scanf( %d%d , &m, &k)) {       int i;       for (i = 1; i <= m; i++) scanf( %d , d + i);       for (i = 1; i <= m; i++) scanf( %d , s + i);       int tank = s[1], mx = s[1];       int p = 1, ans = 0;       while (p <= m) {         if (tank < d[p]) {           int t = ((d[p] - tank) + mx - 1) / mx;           ans += t * k;           tank += t * mx;         }         tank -= d[p];         ans += d[p];         p++;         mx = max(mx, s[p]);         tank += s[p];       }       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     string s;     int i = 0;     cin >> s;     int z = 0, o = 0;     while (s[i] ==  0 ) {       i++;     }     for (; i < s.length(); i++) {       if (s[i] ==  0 ) {         z++;       } else {         if (!o) {           o = s.length() - i;         }       }     }     if (z >= 6 && o >= 7) {       cout <<  Yes             <<   n ;     } else {       cout <<  No             <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 5e3 + 1;   char a[N], b[N];   int f[N][N];   int res;   int n, m;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= m; i++) cin >> b[i];     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (a[i] == b[j]) {           f[i][j] = f[i - 1][j - 1] + 2;         }         f[i][j] = max(f[i][j], max(f[i - 1][j], f[i][j - 1]) - 1);         res = max(res, f[i][j]);       }     }     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   int dp[1501][1501], sum[1501], tsum[1501], pro, ex[2][100001], c[100001],       P[1501];   int sp[1501], gp[1501], inv[1501];   int power(int a, int n) {     if (n == 0) return 1;     int res = power(a, n / 2);     res = (int)((long long)res * (long long)res % mod);     if (n % 2) res = (int)((long long)res * (long long)a % mod);     return res;   }   int main() {     int n, m, a, b, k, i, j, s, p, q, tmp;     scanf( %d%d%d%d%d , &n, &m, &a, &b, &k);     pro = (int)((long long)a * (long long)power(b, mod - 2) % mod);     for (i = 0; i <= 100000; i++) {       if (i == 0) {         ex[0][i] = ex[1][i] = 1;         c[i] = 1;       } else {         ex[1][i] = (int)((long long)ex[1][i - 1] * (long long)(1 - pro) % mod);         if (ex[1][i] < 0) ex[1][i] += mod;         ex[0][i] = (int)((long long)ex[0][i - 1] * (long long)pro % mod);         c[i] = (int)((long long)c[i - 1] * (long long)(k - i + 1) % mod);         c[i] = (int)((long long)c[i] * (long long)power(i, mod - 2) % mod);       }     }     memset(P, 0, sizeof(P));     for (i = 0; i <= m; i++) {       if (i <= k) {         P[i] = (int)((long long)c[i] * (long long)ex[0][i] % mod);         P[i] = (int)((long long)P[i] * (long long)ex[1][k - i] % mod);       }       sp[i] = P[i];       if (i) sp[i] = (sp[i - 1] + sp[i]) % mod;       inv[i] = power(P[i], mod - 2);     }     memset(dp, 0, sizeof(dp));     dp[0][m] = 1;     for (i = 0; i < n; i++) {       for (j = 0; j <= m; j++) {         sum[j] = (int)((long long)P[j] * (long long)dp[i][j] % mod);         if (j) sum[j] = (sum[j - 1] + sum[j]) % mod;       }       for (j = 0; j < m; j++) {         tsum[j] = (int)((long long)sp[m - 1 - j] * (long long)dp[i][m] % mod);         tmp = (int)((long long)sp[m - 1 - j] * (long long)dp[i][j] % mod);         tsum[j] = (tsum[j] - tmp + mod) % mod;         tsum[j] = (tsum[j] - sum[m - 1 - j] + mod) % mod;         tsum[j] = (int)((long long)tsum[j] * (long long)P[j] % mod);         if (j) tsum[j] = (tsum[j - 1] + tsum[j]) % mod;       }       dp[i + 1][0] = 0;       dp[i + 1][m] = tsum[m - 1];       for (j = 1; j < m; j++) {         if (j == 0)           dp[i + 1][m - j] = tsum[j - 1];         else           dp[i + 1][m - j] = (dp[i + 1][m] - tsum[j - 1] + mod) % mod;       }     }     printf( %d n , dp[n][m]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long INF;   int n;   long long v[100005];   int c[100005];   long long mx[100005];   pair<long long, int> st[2];   int q;   long long a, b;   void solve(long long a, long long b) {     long long ans = 0;     st[0].first = 0;     st[0].second = -2;     st[1].first = 0;     st[1].second = -1;     fill(mx, mx + n, -INF);     for (int i = 0; i < n; i++) {       int clr = c[i];       long long val = v[i];       long long w1, w2, w;       int td = 1;       if (st[td].second == clr) td--;       w1 = st[td].first + v[i] * b;       w2 = mx[clr] + v[i] * a;       w = max(w1, w2);       if (w > mx[clr]) {         if (st[1].second == clr) {           st[1].first = w;         } else if (st[0].second == clr) {           st[0].first = w;         } else {           if (w >= st[1].first) {             st[0] = st[1];             st[1] = make_pair(w, clr);           } else if (w > st[0].first) {             st[0] = make_pair(w, clr);           }         }         mx[clr] = w;         if (st[0] > st[1]) swap(st[0], st[1]);       }     }     for (int i = 0; i < n; i++) {       ans = max(ans, mx[i]);     }     cout << ans << endl;   }   int main() {     INF = 1.0e18;     cin >> n >> q;     for (int i = 0; i < n; i++) cin >> v[i];     for (int i = 0; i < n; i++) cin >> c[i], c[i]--;     for (int i = 0; i < q; i++) cin >> a >> b, solve(a, b);     return 0;   }


#include <bits/stdc++.h>   const int N = 500;   using namespace std;   int n, yh, yhuo = 0, huo = 0, yu, x = 0, y;   char pd;   int main() {     y = 1023;     x = 0;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       cin >> pd >> yh;       if (pd ==  & ) x &= yh, y &= yh;       if (pd ==  | ) x |= yh, y |= yh;       if (pd ==  ^ ) x ^= yh, y ^= yh;     }     huo = 0;     yhuo = 0;     for (int i = 0; i <= 9; i++) {       int a = x & (1 << i), b = y & (1 << i);       if (a && b == 0) yhuo |= (1 << i);       if (a == 0 && b == 0) huo |= (1 << i), yhuo |= (1 << i);       if (a && b) huo |= (1 << i);     }     printf( 2 n );     printf( | %d n^ %d n , huo, yhuo);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     string ss, tt;     cin >> ss >> tt;     int l = ss.size();     vector<int> s(30);     vector<int> t(30);     for (int i = 0; i < l; i++) {       s[ss[i] -  a ]++;       t[tt[i] -  a ]++;     }     bool ok = false;     for (int i = 0; i < s.size(); i++) {       if (s[i] && t[i]) {         ok = true;       }     }     cout << (ok ?  YES n  :  NO n );     return;   }   int main() {     int q;     cin >> q;     while (q--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2000;   int pre[maxn];   int countlen(int i) {     int ans = 0, p;     for (p = i; p != -2; p = pre[p]) ans++;     return ans;   }   int main() {     int i, n;     cin >> n;     for (i = 0; i < n; i++) cin >> pre[i], pre[i]--;     int ans = 0;     for (i = 0; i < n; i++) ans = max(ans, countlen(i));     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100004;   long long int a[MAXN];   long long int k, n;   vector<long long int> b(MAXN, 0);   long long int f(double num) {     long long int res = 0;     for (int i = 0; i < n; i++) {       if (num > a[i] + 0.25) {         b[i] = 0;         continue;       }       double tem = sqrt(12 * (a[i] - num) - 3);       b[i] = (long long int)(tem / 6 + 0.5);       if (b[i] > a[i]) b[i] = a[i];       res += b[i];     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> a[i];     double mid;     double lo = -3e18, hi = 1e9;     for (int i = 0; i < 200; i++) {       mid = (lo + hi) / 2;       if (f(mid) >= k)         lo = mid;       else         hi = mid;     }     long long int tot = 0;     priority_queue<pair<long long int, int>, vector<pair<long long int, int> >,                    greater<pair<long long int, int> > >         pq;     priority_queue<pair<long long int, int> > pq1;     for (int i = 0; i < n; i++) {       double tem = sqrt(12 * (a[i] - lo) - 3);       b[i] = min((long long int)(tem / 6 + 0.5), a[i]);       tot += b[i];       if (b[i]) pq.push({a[i] - 3 * b[i] * b[i] + 3 * b[i] - 1, i});       if (b[i] != a[i]) pq1.push({a[i] - 3 * b[i] * b[i] - 3 * b[i] - 1, i});     }     while (tot - k > 0) {       pair<long long int, int> tem = pq.top();       pq.pop();       int i = tem.second;       b[i]--;       tem.first = a[i] - 3 * b[i] * b[i] + 3 * b[i] - 1;       pq.push(tem);       tot--;     }     for (int i = 0; i < n; i++) {       int bu = b[i];       printf( %d  , bu);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int bl = 400, sz = 4e5 + 10;   int pr[sz], bor[sz][26], cnt[sz], an[sz], q = 1;   char prsy[sz];   vector<pair<int, int> > sp[sz];   vector<int> sv[sz];   void dfs(int v) {     int u = v, s = 0;     for (int a = 0; (a < bl and u != 0 and s != -1); a++) {       s = bor[s][prsy[u] -  a ];       if (s != -1) cnt[s]++;       u = pr[u];     }     for (int a = 0; a < sp[v].size(); a++) {       int s = sp[v][a].first, nu = sp[v][a].second;       an[nu] = cnt[s];     }     for (int a = 0; a < sv[v].size(); a++) {       dfs(sv[v][a]);     }     u = v, s = 0;     for (int a = 0; (a < bl and u != 0 and s != -1); a++) {       s = bor[s][prsy[u] -  a ];       if (s != -1) cnt[s]--;       u = pr[u];     }   }   int f(string &s, int le) {     int n = s.size(), re = 0;     int zf[n], l = 0, r = -1;     zf[0] = 0;     for (int a = 1; a < n; a++) {       zf[a] = 0;       if (r >= a) zf[a] = min(zf[a - l], r - a + 1);       while (a + zf[a] < n) {         if (s[zf[a]] == s[a + zf[a]])           zf[a]++;         else           break;       }       if (a + zf[a] - 1 > r) l = a, r = a + zf[a] - 1;       if (zf[a] == le) re++;     }     return re;   }   int main() {     int n;     cin >> n;     for (int a = 1; a <= n; a++) {       int ty;       scanf( %d , &ty);       if (ty == 2) scanf( %d , &pr[a]);       scanf( %s , prsy + a);       sv[pr[a]].push_back(a);     }     int m;     cin >> m;     for (int a = 0; a < 26; a++) bor[0][a] = -1;     for (int a = 0; a < m; a++) {       int v;       scanf( %d , &v);       string st;       cin >> st;       reverse(st.begin(), st.end());       if (st.size() < bl) {         int s = 0;         for (int b = 0; b < st.size(); b++) {           int ne = st[b] -  a ;           if (bor[s][ne] == -1) {             bor[s][ne] = q++;             for (int i = 0; i < 26; i++) bor[q - 1][i] = -1;           }           s = bor[s][ne];         }         sp[v].push_back({s, a});       } else {         int l = st.size();         st.push_back( # );         while (v) {           st.push_back(prsy[v]), v = pr[v];         }         an[a] = f(st, l);       }     }     dfs(0);     for (int a = 0; a < m; a++) printf( %d n , an[a]);   }


#include <bits/stdc++.h>   using namespace std;   bool vis[101];   vector<int> a[101];   void dfs(int x) {     vis[x] = true;     for (int i = 0; i < a[x].size(); i++)       if (!vis[a[x][i]]) dfs(a[x][i]);   }   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       a[x].push_back(y);       a[y].push_back(x);     }     dfs(1);     if (*min_element(vis + 1, vis + n + 1) && m == n)       cout <<  FHTAGN! ;     else       cout <<  NO ;   }


#include <bits/stdc++.h>   const int MOD = 1000000007;   const int Inv2 = 500000004;   inline int mul(int x, int y) { return (long long)x * (long long)y % MOD; }   inline int add(int x, int y) {     int r = x + y;     if (r >= MOD) r -= MOD;     return r;   }   inline int qpow(int a, int p) {     int x = a, ans = 1;     while (p) {       if (p & 1) ans = mul(ans, x);       x = mul(x, x);       p >>= 1;     }     return ans;   }   using namespace std;   template <typename T>   inline void read(T &x) {     char ch;     while ((ch = getchar()), (ch <  0  || ch >  9 ))       ;     x = ch -  0 ;     while ((ch = getchar()), (ch >=  0  && ch <=  9 )) x = x * 10 + (ch -  0 );   }   inline int readbin(int *str) {     char ch;     int len = 1;     while ((ch = getchar()), (ch <  0  || ch >  1 ))       ;     *(str++) = ch -  0 ;     while ((ch = getchar()), (ch >=  0  && ch <=  1 )) *(str++) = ch -  0 , len++;     return len;   }   void fwt(int A[], int n) {     int i, h, k, A0, A1;     for (h = 2; h <= n; h <<= 1) {       for (i = 0; i < n; i += h) {         for (k = i; k < i + (h / 2); k++) {           A0 = A[k];           A1 = A[k + (h / 2)];           A[k] = (A0 + A1) % MOD;           A[k + (h / 2)] = (A0 - A1) % MOD;         }       }     }   }   void ifwt(int A[], int n) {     int i, h, k, A0, A1;     for (h = 2; h <= n; h <<= 1) {       for (i = 0; i < n; i += h) {         for (k = i; k < i + (h / 2); k++) {           A0 = A[k];           A1 = A[k + (h / 2)];           A[k] = mul(Inv2, A0 + A1);           A[k + (h / 2)] = mul(Inv2, A0 - A1);         }       }     }   }   const int MAXN = 5 + (1 << 20);   int A[MAXN], P[MAXN];   int N, NB;   int main() {     int i;     read(NB);     N = readbin(A);     fwt(A, N);     for (i = 0; i < N; i++) P[i] = mul(A[i], A[i]);     ifwt(P, N);     int Ans = 0;     for (i = 0; i < N; i++)       Ans = add(Ans, mul(P[i], 1 << (NB - __builtin_popcount(i))));     Ans = mul(Ans, 3);     printf( %d , Ans);   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int t, n, m, i, j, k, sum = 0;     long long int p, w, d;     cin >> n >> p >> w >> d;     long long int l = 0;     long long int r = n;     for (i = 0; i <= w; i++) {       long long int temp = p - i * d;       if (temp < 0) break;       if ((temp) % w != 0) continue;       long long int q = temp / w;       if (i + q <= n) {         cout << q <<     << i <<     << n - q - i <<   n ;         return 0;       }     }     cout << -1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   int main() {     int q;     int a[510][2];     int m = 0;     int l1, r1, l2, r2;     int flag;     scanf( %d , &q);     for (int i = 0; i < q; i++) {       scanf( %d%d%d%d , &l1, &r1, &l2, &r2);       flag = 0;       for (int j = l1; j <= r1; j++) {         a[m][0] = j;         for (int k = l2; k <= r2; k++) {           if (j != k) {             flag = 1;             a[m][1] = k;             break;           }         }         if (flag == 1) {           m++;           break;         }       }     }     for (int i = 0; i < q; i++) printf( %d %d n , a[i][0], a[i][1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long modpow(long long a, long long b,                    long long mod = (long long)(1e9 + 7)) {     if (!b) return 1;     a %= mod;     return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod;   }   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   const int mxn = 1e6 + 4, mxk = 20;   int n, q;   int d[mxn], parent[mxn][mxk];   void add(int i, int p) {     parent[i][0] = p;     d[i] = d[p] + 1;     for (int j = 0; j < mxk; j++)       if (j) parent[i][j] = parent[parent[i][j - 1]][j - 1];   }   int lca(int i, int j) {     if (d[i] > d[j]) swap(i, j);     int diff = d[j] - d[i];     for (int k = mxk - 1; k >= 0; k--)       if (diff & (1 << k)) j = parent[j][k];     if (i == j) return i;     for (int k = mxk - 1; k >= 0; k--)       if (parent[i][k] != parent[j][k]) i = parent[i][k], j = parent[j][k];     return parent[i][0];   }   int dist(int i, int j) { return d[i] + d[j] - 2 * d[lca(i, j)]; }   void solve() {     for (int j = 0; j < mxk; j++) parent[0][j] = 0;     d[0] = 0;     add(1, 0);     add(2, 0);     add(3, 0);     scanf( %d , &q);     n = 4;     stringstream ss;     int a = 1, b = 2;     int best = dist(a, b);     for (int _ = 0; _ < q; _++) {       int x;       scanf( %d , &x);       x--;       add(n++, x);       add(n++, x);       int d1 = dist(a, n - 1), d2 = dist(b, n - 1);       if (max(d1, d2) > best) {         best = max(d1, d2);         if (d1 > d2)           b = n - 1;         else           a = n - 1;       }       ss << best << endl;     }     cout << ss.str();   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int k;       string s;       cin >> k >> s;       s +=  A ;       vector<int> a;       for (int i = 0; i < s.size(); i++) {         if (s[i] ==  A ) a.push_back(i);       }       int ans = 0;       for (int i = 0; i + 1 < a.size(); i++) {         ans = max(ans, a[i + 1] - a[i] - 1);       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   long long x, k;   long long qow(long long a, long long b) {     long long ans = 1;     a = a % mod;     while (b) {       if (b % 2 == 1) {         ans = (ans * a) % mod;       }       a = (a * a) % mod;       b /= 2;     }     return ans;   }   int main() {     while (~scanf( %lld %lld , &x, &k)) {       if (x == 0) {         printf( 0 n );         continue;       }       x = x % mod;       cout << (((2 * x - 1) * qow(2, k) + mod) % mod + 1 + mod) % mod << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   struct node {     long long x, y;   } s, t, dist[100005];   int dx[4] = {0, 0, -1, 1};   int dy[4] = {1, -1, 0, 0};   char wind[100005];   string mov =  UDLR ;   long long pos, l, r, mid;   bool check(long long mid) {     long long zq = mid / n, sy = mid % n;     long long x = s.x + zq * dist[n].x + dist[sy].x;     long long y = s.y + zq * dist[n].y + dist[sy].y;     long long dis = abs(t.x - x) + abs(t.y - y);     return dis <= mid;   }   int main() {     cin >> s.x >> s.y >> t.x >> t.y;     cin >> n >> wind;     for (int i = 0; i < n; i++) {       pos = -1;       for (int j = 0; j < 4; j++) {         if (wind[i] == mov[j]) pos = j;       }       dist[i + 1].x = dist[i].x + dx[pos];       dist[i + 1].y = dist[i].y + dy[pos];     }     l = 0, r = 1e15;     while (l <= r) {       mid = (l + r) >> 1;       if (check(mid)) {         r = mid - 1;       } else {         l = mid + 1;       }     }     if (l > 1e15)       cout << -1 << endl;     else       cout << l << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void read(T &x) {     x = 0;     char c = getchar();     bool flag = false;     while (!isdigit(c)) {       if (c ==  - ) flag = true;       c = getchar();     }     while (isdigit(c)) {       x = (x << 1) + (x << 3) + (c ^ 48);       c = getchar();     }     if (flag) x = -x;   }   int n, lenth, las = 1, root = 1, tot = 1;   long long ans;   int ch[1000010][26], fa[1000010], len[1000010], siz[1000010][15], l[1000010],       r[1000010];   char s[1000010], str[1000010];   struct edge {     int to, nxt;   } e[1000010];   int head[1000010], edge_cnt;   void add(int from, int to) {     e[++edge_cnt] = (edge){to, head[from]};     head[from] = edge_cnt;   }   void insert(int c, int id) {     int p = las, np = las = ++tot;     len[np] = len[p] + 1, siz[np][id] = 1;     while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];     if (!p)       fa[np] = root;     else {       int q = ch[p][c];       if (len[q] == len[p] + 1)         fa[np] = q;       else {         int nq = ++tot;         memcpy(ch[nq], ch[q], sizeof(ch[q]));         len[nq] = len[p] + 1, fa[nq] = fa[q], fa[q] = fa[np] = nq;         while (ch[p][c] == q) ch[p][c] = nq, p = fa[p];       }     }   }   void dfs(int x) {     for (int i = head[x]; i; i = e[i].nxt) {       int y = e[i].to;       dfs(y);       for (int j = 0; j <= n; ++j) siz[x][j] += siz[y][j];     }   }   void calc(int x) {     if (!siz[x][0]) return;     for (int i = 1; i <= n; ++i)       if (siz[x][i] < l[i] || siz[x][i] > r[i]) return;     ans += len[x] - len[fa[x]];   }   int main() {     scanf( %s , str + 1), lenth = strlen(str + 1);     for (int i = 1; i <= lenth; ++i) insert(str[i] -  a , 0);     read(n);     for (int i = 1; i <= n; ++i) {       las = 1, scanf( %s , s + 1), lenth = strlen(s + 1), read(l[i]), read(r[i]);       for (int j = 1; j <= lenth; ++j) insert(s[j] -  a , i);     }     for (int i = 2; i <= tot; ++i) add(fa[i], i);     dfs(root);     for (int i = 2; i <= tot; ++i) calc(i);     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1e5 + 69;   const long long Inf = 1e13;   long long n, newa[maxn], ans[maxn], newb[maxn];   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (long long i = 2; i <= n; i += 2) cin >> ans[i];     for (long long i = 2; i <= n; i += 2) {       long long found = 0;       for (long long j = sqrt(ans[i]); j >= 1; j--)         if (ans[i] % j == 0) {           long long newj = ans[i] / j;           if (!((j + newj) & 1)) {             newa[i] = (newj + j) / 2;             newb[i] = (newj - j) / 2;             if (newb[i] <= newa[i - 2]) continue;             found = 1;             break;           }         }       if (!found) return cout <<  No , 0;     }     for (long long i = 1; i <= n; i++) {       if (i & 1) {         ans[i] = (newb[i + 1] - newa[i - 1]) * (newb[i + 1] + newa[i - 1]);         if (ans[i] > Inf) return cout <<  No , 0;       }     }     cout <<  Yes  <<   n ;     for (long long i = 1; i <= n; i++) cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int la, lb;   int a[2000000];   int b[2000000];   int bpos[1000001];   void deb(int* x, int xlow, int xhigh) {     cout <<  [ ;     for (int i = xlow; i < xhigh; i++) {       cout <<     << x[i];     }     cout <<   ] ;   }   int seen[1000001];   int solve() {     fill(bpos, bpos + 1000001, -1);     for (int i = 0; i < lb; i++) {       bpos[b[i]] = i;     }     int res = 0;     for (int i = la; i < la * 2; i++) {       a[i] = a[i - la];     }     int alow = -1;     int ahigh = alow + la;     int blow = -1;     int last = -1;     int len = 0;     fill(seen, seen + 1000001, 0);     int steps = 0;     for (int i = 0; i < la; i++) {       alow++;       ahigh++;       len = max(len - 1, 0);       if (len == 0) {         if (bpos[a[alow]] != -1) {           blow = bpos[a[alow]];         } else {           blow += lb - 1;           if (blow >= lb) {             blow -= lb;           }         }         last = -1;       } else {         blow = bpos[a[alow]];       }       while (len < la) {         int y = bpos[a[alow + len]];         bool good = false;         if ((y != -1) && (last != -1)) {           if (y < blow) {             if (last < blow) {               good = (y > last);             } else {               good = (y > last - lb);             }           } else {             if (last >= blow) {               good = (y > last);             } else {               good = (y - lb > last);             }           }         } else if (y != -1) {           good = true;         }         if (good) {           seen[a[alow + len]]++;           assert(seen[a[alow + len]] <= 2);           last = y;           steps++;           assert(steps < 4000000);           len++;         } else {           break;         }       }       res = std::max(res, len);     }     return res;   }   inline void init() {}   int main() {     init();     ios_base::sync_with_stdio(0);     bool prev = false;     while (cin >> la >> lb) {       if (prev) {         cout << endl;       }       prev = true;       for (int i = 0; i < la; i++) {         cin >> a[i];       }       for (int i = 0; i < lb; i++) {         cin >> b[i];       }       cout << solve() << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct data {     string name;     int score;     data() {}     data(string name, int score) : name(name), score(score) {}     const bool operator<(const data& other) const {       if (score != other.score) return score > other.score;       return name < other.name;     }     const bool operator==(const data& other) const { return name == other.name; }   };   data vasya;   int n, m, b[100005];   int best(vector<data> v) {     sort(b, b + m);     reverse(b, b + m);     v.erase(find(v.begin(), v.end(), vasya));     sort(v.rbegin(), v.rend());     v.insert(v.begin(), vasya);     for (int i = 0; i < m; i++) v[i].score += b[i];     sort(v.begin(), v.end());     return find(v.begin(), v.end(), vasya) - v.begin() + 1;   }   int worst(vector<data> v) {     multiset<int> S;     multiset<int>::iterator it;     int idx;     for (int i = 0; i < m; i++) S.insert(b[i]);     idx = find(v.begin(), v.end(), vasya) - v.begin();     if (m == n) v[idx].score += *S.begin(), S.erase(S.begin());     sort(v.begin(), v.end());     idx = find(v.begin(), v.end(), vasya) - v.begin();     for (int i = idx + 1; i < (int)v.size() && S.size(); i++) {       int add;       if (v[i].name < vasya.name)         add = v[idx].score - v[i].score;       else         add = v[idx].score - v[i].score + 1;       it = S.lower_bound(add);       if (it == S.end()) continue;       v[i].score += *it;       S.erase(it);     }     sort(v.begin(), v.end());     return find(v.begin(), v.end(), vasya) - v.begin() + 1;   }   int main() {     cin >> n;     vector<data> v(n);     for (int i = 0; i < n; i++) cin >> v[i].name >> v[i].score;     cin >> m;     for (int i = 0; i < m; i++) cin >> b[i];     cin >> vasya.name;     vasya.score = v[find(v.begin(), v.end(), vasya) - v.begin()].score;     int place1 = best(v);     int place2 = worst(v);     cout << place1 <<     << place2 << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   vector<vector<vector<int> > > dp;   vector<vector<int> > tmp;   vector<vector<int> > base;   int get_dig(int pos, int k) {     return (pos < k) ? pos : (get_dig(pos / k, k) + pos % k) % k;   }   inline int get_val(const vector<vector<int> > &a, int i, int j, int shift,                      int k) {     if (i == k && j < k) {       return a[k][(j + k - shift) % k];     }     return a[(i + k - shift) % k][(j + k - shift) % k];   }   vector<vector<int> > shift(const vector<vector<int> > &a, int shift) {     int k = int(a.size()) - 1;     vector<vector<int> > res = a;     for (int i = 0; i <= k; ++i) {       for (int j = 0; j < k; ++j) {         res[i][j] = get_val(a, i, j, shift, k);       }     }     return res;   }   vector<vector<int> > operator*(const vector<vector<int> > &a,                                  const vector<vector<int> > &b) {     int n = int(a.size()), k = int(a[0].size()), m = int(b[0].size());     vector<vector<int> > res(n, vector<int>(m, 0));     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         for (int l = 0; l < k; ++l) {           res[i][j] = (res[i][j] + 1ll * a[i][l] * b[l][j]) % MOD;         }       }     }     return res;   }   int main() {     long long n;     int k;     cin >> n >> k;     int max_depth = 0;     {       long long tmp = n;       while (tmp != 0) {         ++max_depth;         tmp /= k;       }     }     dp.resize(max_depth);     {       dp[0].assign(k + 1, vector<int>(k + 1, 0));       for (int i = 0; i <= k; ++i) {         for (int j = 0; j <= k; ++j) {           dp[0][i][j] = (i == j || j == 0);         }       }       vector<vector<int> > e(k + 1, vector<int>(k + 1, 0));       for (int i = 0; i <= k; ++i) {         e[i][i] = 1;       }       for (int depth = 1; depth < max_depth; ++depth) {         dp[depth] = e;         vector<vector<int> > bs = dp[depth - 1];         vector<vector<int> > &res = dp[depth];         int curx = k;         for (int i = 0; (1 << i) <= curx; ++i) {           if (curx & (1 << i)) {             res = bs * shift(res, (1 << i));             curx ^= (1 << i);           }           bs = bs * shift(bs, (1 << i));         }       }     }     base.assign(1, vector<int>(k + 1, 0));     base[0][k] = 1;     int sl = 0;     long long x = n;     vector<int> vec;     while (x != 0) {       vec.push_back(x % k);       x /= k;     }     int len = int(vec.size());     for (int cur_depth = len - 1; cur_depth >= 0; --cur_depth) {       int rest = vec[cur_depth];       for (int it = 0; it < rest; ++it, sl = (sl + 1) % k) {         base = base * shift(dp[cur_depth], sl);       }     }     {       int res = 0;       for (int i = 0; i <= k; ++i) {         res = (res + base[0][i]) % MOD;       }       cout << res <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 10001;   long long a[MAXN];   int f[MAXN];   long long c[MAXN], b[MAXN];   int n;   int main() {     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i];     for (int i = 0; i < n; ++i) {       long long X = a[i], cnt = 0;       while (!(X & 1)) {         ++cnt;         X >>= 1;       }       b[i] = X;       c[i] = cnt;     }     int ans = 0;     for (int i = 0; i < n; ++i) {       f[i] = 1;       for (int j = 0; j < i; ++j) {         if (a[j] % a[i] == 0 && a[i] % 2 == 1) f[i] = max(f[i], f[j] + 1);         if (a[i] % 2 == 0) {           long long t1 = b[j], t2 = b[i];           if (t1 % t2 == 0) {             if (c[i] - c[j] == i - j || c[i] <= i - j - 1)               f[i] = max(f[i], f[j] + 1);           }         }       }       if (f[i] > ans) ans = f[i];     }     cout << n - ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int inf = (1 << 30) - 1, n, X, x, y, z, f[510][510], c[510], R, g[510][510],       size[510], h[2][510], H[2][510][510], q[510], L[510];   int Lng[510];   double a[510][510];   vector<int> E[510];   void read(int &x) {     char ch = getchar();     int mark = 1;     for (; ch !=  -  && (ch <  0  || ch >  9 ); ch = getchar())       ;     if (ch ==  - ) mark = -1, ch = getchar();     for (x = 0; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch - 48;     x *= mark;   }   double Solve(int m, int n) {     bool flag = 1;     for (; flag;) {       flag = 0;       for (int i = 1; i <= n; i++)         if (a[0][i] > 0) {           double Min = inf;           int l;           for (int j = 1; j <= m; j++)             if (a[j][i] < 0) {               double tmp = -a[j][0] / a[j][i];               if (Min > tmp) Min = tmp, l = j;             }           if (Min == inf) continue;           flag = 1;           double tmp = -a[l][i];           a[l][i] = -1;           for (int j = 0; j <= n; j++) a[l][j] /= tmp;           for (int j = 0; j <= m; j++)             if (a[j][i] && j != l) {               for (int k = 0; k <= n; k++)                 if (k != i) a[j][k] += a[j][i] * a[l][k];               a[j][i] *= a[l][i];             }         }     }     return a[0][0];   }   void dfs(int u, int pre) {     int cnt = 0;     for (int i = 0; i < E[u].size(); i++)       if (E[u][i] != pre) {         dfs(E[u][i], u);         ++cnt;         Lng[u] = max(Lng[u], min(Lng[E[u][i]] + f[u][E[u][i]], inf));       }     size[u] = 1;     int A = 0;     for (int j = 0; j <= R; j++) h[A][j] = inf;     for (int Bg = 0; Bg <= cnt; Bg++)       for (int j = 0; j <= R; j++) H[A][Bg][j] = inf;     H[A][0][0] = inf - 1;     h[A][1] = 0;     cnt = 0;     L[0] = -inf;     for (int i = 0; i < E[u].size(); i++)       if (E[u][i] != pre) {         int v = E[u][i];         q[++cnt] = v;         L[cnt] = min(inf, f[u][v] + Lng[v]);         A ^= 1;         for (int j = 0; j <= R; j++) h[A][j] = inf;         for (int Bg = 0; Bg <= cnt; Bg++)           for (int j = 0; j <= R; j++) H[A][Bg][j] = inf;         for (int k = 0; k <= size[u] && k <= R; k++)           for (int j = 0; j <= size[v] && j + k <= R; j++) {             if (j == 0 && max(h[A ^ 1][k], Lng[v] + f[u][v]) <= X)               h[A][j + k] = 0;             if (j && g[v][j] != inf &&                 max(h[A ^ 1][k], min(g[v][j], f[u][v])) <= X)               h[A][j + k] = 0;           }         for (int Bg = 0; Bg < cnt; Bg++)           for (int k = 0; k <= size[u] && k <= R; k++)             if (H[A ^ 1][Bg][k] != inf) {               for (int j = 1; j + k <= R && j <= size[v]; j++)                 if (g[v][j] <= X)                   H[A][Bg][j + k] = min(H[A][Bg][j + k],                                         min(H[A ^ 1][Bg][k], g[v][j] + f[u][v]));               if (L[cnt] > L[Bg])                 H[A][cnt][k] = min(H[A][cnt][k], H[A ^ 1][Bg][k]);               else                 H[A][Bg][k] = min(H[A][Bg][k], H[A ^ 1][Bg][k]);             }         size[u] += size[v];       }     for (int j = 0; j <= R; j++) g[u][j] = h[A][j];     for (int Bg = 0; Bg <= cnt; Bg++)       for (int j = 0; j <= R; j++)         if (H[A][Bg][j] + L[Bg] <= X) g[u][j] = min(g[u][j], H[A][Bg][j]);   }   bool check() {     for (int i = 1; i <= n; i++)       for (int j = 0; j <= n; j++) g[i][j] = inf;     dfs(1, 0);     return (g[1][R] <= X);   }   int main() {     read(n);     read(X);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) f[i][j] = inf;       f[i][i] = 0;       read(c[i]);       R += c[i];     }     for (int i = 1; i < n; i++) {       read(x);       read(y);       read(z);       f[x][y] = f[y][x] = z;       E[x].push_back(y);       E[y].push_back(x);     }     for (int k = 1; k <= n; k++)       for (int i = 1; i <= n; i++)         if (i ^ k)           for (int j = 1; j <= n; j++)             if ((i ^ j) && (k ^ j)) f[i][j] = min(f[i][k] + f[k][j], f[i][j]);     if (!check()) {       printf( -1 n );       return 0;     }     a[0][n + 1] = -R;     for (int i = 1; i <= n; i++) {       a[0][i] = 1;       a[i][n + 1] = 1;       a[i][0] = c[i] ^ 1;       for (int j = 1; j <= n; j++) a[i][j] = -(f[i][j] <= X);     }     printf( %.0lf n , Solve(n, n + 1));     return 0;   }


#include <iostream>   #include <vector>   #include <numeric>   #include <algorithm>      using namespace std;       int main(){       ios_base::sync_with_stdio(false);       cin.tie(NULL);       cout.tie(NULL);       int t;       cin >> t;       while(t--){           int n;           cin >> n;           vector<long long> a(n);           for (int i = 0; i < n; ++i){               cin >> a[i];           }           sort(a.begin(), a.end());           long long ans = 0;           for (int k = 1; k < n; ++k){               ans += a[k] * (2 * k + 1 - n);           }           ans -= a[n-1];                      cout << -ans << endl;       }   }


#include <bits/stdc++.h>   using namespace std;   struct line {     int l, r;   } p[510], q[510];   struct Point {     long double x, y;   } Q[10], tmp[10];   int vis[10];   long double A, B, C;   int n, H, F, tot, cnt;   void read(int &x) {     char ch = getchar();     int mark = 1;     for (; ch !=  -  && (ch <  0  || ch >  9 ); ch = getchar())       ;     if (ch ==  - ) mark = -1, ch = getchar();     for (x = 0; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch - 48;     x *= mark;   }   inline bool cmp(line a, line b) {     if (a.l == b.l) return a.r > b.r;     return a.l < b.l;   }   inline long double Get(long double x, long double y) {     return A * x + B * y + C;   }   inline long double Abs(long double x) {     if (x < 0) return -x;     return x;   }   inline long double dis(Point X, long double A, long double B, long double C) {     return Abs(X.x * A + X.y * B + C) / sqrt(A * A + B * B);   }   inline long double Getsum(long double x1, long double y1, long double x2,                             long double y2) {     return x1 * y2 - x2 * y1;   }   Point Getp(Point X, Point Y, long double A, long double B, long double C) {     long double d1 = dis(X, A, B, C), d2 = dis(Y, A, B, C);     Point Ans;     Ans.x = (d2 * X.x + d1 * Y.x) / (d1 + d2);     Ans.y = (d2 * X.y + d1 * Y.y) / (d1 + d2);     return Ans;   }   void solve() {     int tot = 0;     int s1 = 0, s2 = 0;     for (int k = 1; k <= cnt; k++) {       double t = Get(Q[k].x, Q[k].y);       if (t == 0)         vis[k] = -1, s2++;       else if (t > 0)         vis[k] = 1, s1++;       else         vis[k] = -1, s2++;     }     if (s1 == 0) {       cnt = 0;       return;     } else if (s2 == 0)       return;     Q[cnt + 1] = Q[1];     vis[cnt + 1] = vis[1];     for (int i = 1; i <= cnt; i++) {       if (vis[i] == 1) tmp[++tot] = Q[i];       if (vis[i] * vis[i + 1] < 0) {         tmp[++tot] = Getp(Q[i], Q[i + 1], A, B, C);       }     }     cnt = tot;     for (int i = 1; i <= cnt; i++) Q[i] = tmp[i];   }   int main() {     read(n);     read(H);     read(F);     for (int i = 1; i <= n; i++) read(p[i].l), read(p[i].r);     sort(p + 1, p + n + 1, cmp);     q[++tot] = p[1];     for (int i = 2; i <= n; i++) {       if (p[i].l > q[tot].r)         q[++tot] = p[i];       else         q[tot].r = max(q[tot].r, p[i].r);     }     long double sum = 0;     for (int i = 1; i <= tot; i++) {       long double d = (long double)(F + H) / (F - H) * (q[i].r - q[i].l);       sum += (d + q[i].r - q[i].l) * H * 2;     }     for (int i = 1; i <= tot; i++) {       for (int j = 1; j <= tot; j++) {         cnt = 4;         Q[1].x = q[i].l;         Q[1].y = -H;         Q[2].x = q[i].r;         Q[2].y = -H;         Q[3].x = (long double)(F + H) / (F - H) * q[i].r;         Q[3].y = H;         Q[4].x = (long double)(F + H) / (F - H) * q[i].l;         Q[4].y = H;         A = H - F;         B = -q[j].l;         C = F * q[j].l;         A = -A;         B = -B;         C = -C;         solve();         if (cnt <= 2) continue;         A = H - F;         B = -q[j].r;         C = F * q[j].r;         solve();         if (cnt <= 2) continue;         long double over = 0;         for (int k = 2; k < cnt; k++)           over += Getsum(Q[k].x - Q[1].x, Q[k].y - Q[1].y, Q[k + 1].x - Q[1].x,                          Q[k + 1].y - Q[1].y) /                   2;         sum -= over;       }     }     printf( %.7lf n , (double)sum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m1, q;   int m[102][102];   struct query {     int t;     int a;     int b;     int c;   };   void rote(int type, int val) {     if (type == 1) {       int aux = m[val][m1];       for (int i = m1 - 1; i >= 1; i--) m[val][i + 1] = m[val][i];       m[val][1] = aux;     } else {       int aux = m[n][val];       for (int i = n - 1; i >= 1; i--) m[i + 1][val] = m[i][val];       m[1][val] = aux;     }   }   int main() {     cin >> n >> m1 >> q;     stack<query> st;     for (int i = 0; i < q; i++) {       int in;       cin >> in;       if (in == 3) {         int a, b, c;         cin >> a >> b >> c;         query qu;         qu.t = in;         qu.a = a;         qu.b = b;         qu.c = c;         st.push(qu);       } else {         int a;         cin >> a;         query qu;         qu.t = in;         qu.a = a;         st.push(qu);       }     }     memset(m, 0, sizeof(m));     while (!st.empty()) {       int type = st.top().t;       if (type == 3) {         int x, y, v;         x = st.top().b;         y = st.top().a;         v = st.top().c;         st.pop();         m[y][x] = v;       } else {         int a = st.top().a;         st.pop();         rote(type, a);       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m1; j++) cout << m[i][j] <<    ;       cout << endl;     }   }


#include <bits/stdc++.h>   const int maxn = 1005;   int n, m;   int a[maxn], b[maxn];   bool solve(double v) {     for (int i = 0; i < n; i++) {       if ((m + v) / a[i] <= v)         v -= (m + v) / a[i];       else         return false;       if ((m + v) / b[i] <= v)         v -= (m + v) / b[i];       else         return false;     }     if (v >= 0) return true;     return false;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);     }     scanf( %d , &b[n - 1]);     for (int i = 0; i < n - 1; i++) {       scanf( %d , &b[i]);     }     double left = -1, right = 1e9 + 1;     while (left + 0.000001 < right) {       double mid = (left + right) / 2;       if (solve(mid)) {         right = mid;       } else {         left = mid;       }     }     if (right > 1e9 + 0.00001)       puts( -1 );     else       printf( %f n , right);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct info {     long long int val, in;   };   long long int arr[100010];   info tree[100010 * 4], temp;   void init(long long int node, long long int b, long long int e) {     if (b == e) {       tree[node].val = arr[b];       tree[node].in = b;       return;     }     long long int Left = node * 2;     long long int Right = node * 2 + 1;     long long int mid = (b + e) / 2;     init(Left, b, mid);     init(Right, mid + 1, e);     if (tree[Left].val >= tree[Right].val) {       tree[node].val = tree[Right].val;       tree[node].in = tree[Right].in;     } else {       tree[node].val = tree[Left].val;       tree[node].in = tree[Left].in;     }   }   info query(long long int node, long long int b, long long int e,              long long int i, long long int j) {     if (i > e || j < b) {       temp.in = temp.val = 30;       return temp;     }     if (b >= i && e <= j) return tree[node];     long long int Left = node * 2;     long long int Right = node * 2 + 1;     long long int mid = (b + e) / 2;     info p1 = query(Left, b, mid, i, j);     info p2 = query(Right, mid + 1, e, i, j);     if (p1.val >= p2.val)       return p2;     else       return p1;   }   int main() {     long long int i, j, len, mn = 30, m, last = 1, in;     long long int used = -1;     string s;     vector<char> v, ans;     char mxx =  a  - 1;     cin >> m >> s;     len = s.length();     for (i = 0; i < s.length(); i++) {       arr[i + 1] = s[i] -  a  + 1;     }     init(1, 1, len);     for (i = 1; i <= len - m + 1; i++) {       info ret = query(1, 1, len, i, i + m - 1);       if (used == -1) {         used = ret.in;         i = ret.in;         last = ret.val;         v.push_back(last - 1 +  a );         mxx = max(mxx, (char)(last - 1 +  a ));       } else {         if (ret.val == last && used == ret.in) {         } else {           used = ret.in;           i = ret.in;           last = ret.val;           v.push_back(last - 1 +  a );           mxx = max(mxx, (char)(last - 1 +  a ));         }       }     }     if (v.size() == 0) v.push_back(s[0]);     sort(v.begin(), v.end());     long long int inx = v.size() - 1;     long long int cnt = 0;     for (i = 0; i < v.size(); i++) {       if (v[i] == v[inx]) cnt++;     }     for (i = 0; i < s.length(); i++) {       if (s[i] < v[inx]) ans.push_back(s[i]);     }     for (i = 1; i <= cnt; i++) {       ans.push_back(v[inx]);     }     sort(ans.begin(), ans.end());     for (i = 0; i < ans.size(); i++) {       cout << ans[i];     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5e3 + 3;   const int INF = 2000000000;   inline long long ab(long long x) { return x < 0 ? -x : x; }   long long a[MAXN];   int c_a[MAXN];   int l[MAXN], r[MAXN], x[MAXN], t[MAXN];   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) {       a[i] = INF;     }     for (int q = 0; q < m; q++) {       cin >> t[q] >> l[q] >> r[q] >> x[q];       --l[q], --r[q];     }     for (int q = m - 1; q >= 0; q--) {       if (t[q] == 1) {         for (int i = l[q]; i <= r[q]; i++) {           a[i] -= x[q];         }       } else {         bool found = false;         for (int i = l[q]; i <= r[q]; i++) {           if (a[i] >= x[q]) {             a[i] = x[q];             found = true;           }         }         if (!found) {           cout <<  NO ;           return 0;         }       }     }     for (int i = 0; i < n; i++) {       if (a[i] < 0) {         if (ab(a[i]) > INF / 2) a[i] = -INF / 2;       } else {         if (a[i] > INF / 2) a[i] = INF / 2;       }     }     for (int i = 0; i < n; i++) {       c_a[i] = a[i];     }     for (int q = 0; q < m; q++) {       if (t[q] == 1) {         for (int i = l[q]; i <= r[q]; i++) {           a[i] += x[q];         }       } else {         long long maxv = -INF;         for (int i = l[q]; i <= r[q]; i++) {           maxv = max(maxv, a[i]);         }         if (maxv != x[q]) {           cout <<  NO ;           return 0;         }       }     }     cout <<  YES n ;     for (int i = 0; i < n; i++) {       if (i > 0) cout <<    ;       cout << c_a[i];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 2000000000000000000LL;   const int inf = 0x3f3f3f3f;   const long double EPS = 1e-9;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     long long x, i, j, n = 0, t;     cin >> x;     while (x / 2 != 0) {       if (x % 2 == 1) n++;       x = x / 2;     }     cout << n + 1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 105;   long long g[N][N], f[N][N], a[N], d;   int n;   pair<int, int> p[N];   void FloydWarshall() {     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) f[i][j] = g[i][j];       f[i][i] = 0;     }     for (int k = 1; k <= n; k++) {       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++) {           f[i][j] = min(f[i][j], f[i][k] + f[k][j]);         }       }     }   }   int main() {     scanf( %d%lld , &n, &d);     for (int i = 2; i < n; i++) scanf( %lld , &a[i]);     for (int i = 1; i <= n; i++) scanf( %d%d , &p[i].first, &p[i].second);     for (int i = 2; i < n; i++) {       for (int j = 1; j <= n; j++) {         if (i == j) continue;         g[i][j] -= a[i];       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (i == j) continue;         g[i][j] +=             1ll * d *             (abs(p[i].first - p[j].first) + abs(p[i].second - p[j].second));       }     }     FloydWarshall();     printf( %lld , f[1][n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t, s, a, b, c;     cin >> t;     for (int i = 0; i < t; i++) {       cin >> s >> a >> b >> c;       cout << s / c + ((s / c) / a) * b << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int lpd(int n) {     int ans = -1;     for (int i = 2; i * i <= n; i++) {       if (n % i == 0) {         ans = i;         while (n % i == 0) n /= i;       }     }     if (n > 1) ans = n;     return ans;   }   int x, p[1000006], pd, t, ans;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     p[0] = p[1] = 1;     for (int i = 2; i * i <= 1e6; i++) {       if (p[i] == 0) {         for (int j = i * i; j <= 1e6; j += i) {           p[j] = 1;         }       }     }     cin >> x;     ans = 1e9;     pd = lpd(x);     t = x / pd;     t -= 1;     if (p[pd * t + 1] == 0 && (pd * t + 2 == x)) {       cout << (x - 1) <<   n ;       return 0;     }     for (int i = pd * t + 1; i < x; i++) {       if (p[i] == 0) continue;       pd = lpd(i);       t = i / pd;       t -= 1;       ans = min(ans, pd * t + 1);     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   long long int dp[4011][4011];   long long int b[2000000];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n;     cin >> n;     long long int a[n];     set<long long int> st;     for (int i = 0; i < n; i++) {       cin >> a[i];       st.insert(a[i]);     }     long long int k = 0;     for (auto u : st) {       b[u] = k;       k++;     }     for (int i = 0; i < n; i++) {       a[i] = b[a[i]];     }     for (int i = 0; i < n; i++) {       long long int f = 0;       for (int j = i - 1; j >= 0; j--) {         if (a[i] == a[j]) {           if (f == 1) continue;           f = 1;           dp[a[i]][a[j]]++;         } else {           dp[a[i]][a[j]] = max(dp[a[i]][a[j]], 1 + dp[a[j]][a[i]]);         }       }     }     long long int ans = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) ans = max(ans, dp[i][j]);     }     cout << ans + 1;   }