#include <bits/stdc++.h> n using namespace std; n class base { n  public: n   long double a, b; n   base(void) { n     a = 0; n     b = 0; n   } n   base(long double _a, long double _b) { n     a = _a; n     b = _b; n   } n   base(long double _a) { n     a = _a; n     b = 0; n   } n   base operator*(const base& b2) { n     base ret(a * b2.a - b * b2.b, b * b2.a + a * b2.b); n     return ret; n   } n   base operator+(const base& b2) { n     base ret(a + b2.a, b + b2.b); n     return ret; n   } n   base operator-(const base& b2) { n     base ret(a - b2.a, b - b2.b); n     return ret; n   } n   base operator/(const base& b2) { n     long double c = b2.a, d = b2.b; n     base ret((a * c + b * d) / (c * c + d * d), n              (b * c - a * d) / (c * c + d * d)); n     return ret; n   } n   void operator*=(const base& b2) { n     long double tmp = a * b2.a - b * b2.b; n     b = b * b2.a + a * b2.b; n     a = tmp; n   } n   void operator/=(const base& b2) { n     long double c = b2.a, d = b2.b; n     long double tmp = (a * c + b * d) / (c * c + d * d); n     b = (b * c - a * d) / (c * c + d * d); n     a = tmp; n   } n   inline long double real(void) { return a; } n }; n void fft(vector<base>& a, bool invert) { n   int n = (int)a.size(); n   for (int i = 1, j = 0; i < n; ++i) { n     int bit = n >> 1; n     for (; j >= bit; bit >>= 1) j -= bit; n     j += bit; n     if (i < j) swap(a[i], a[j]); n   } n   base ww[n]; n   long double ang = 2 * 3.14159265358979323846l / n * (invert ? -1 : 1); n   base wi(cos(ang), sin(ang)); n   ww[0] = 1; n   for (int i = 1; i < n; i++) { n     ww[i] = ww[i / 2] * ww[i / 2]; n     if (i % 2 == 1) ww[i] *= wi; n   } n   for (int len = 2; len <= n; len <<= 1) { n     int k = n / len; n     for (int i = 0; i < n; i += len) { n       int wn = 0; n       for (int j = 0; j < len / 2; ++j) { n         base u = a[i + j], v = a[i + j + len / 2] * ww[wn]; n         a[i + j] = u + v; n         a[i + j + len / 2] = u - v; n         wn += k; n       } n     } n   } n   if (invert) n     for (int i = 0; i < n; ++i) a[i] /= n; n } n vector<int> multiply(const vector<int>& a, const vector<int>& b) { n   vector<base> fa(a.begin(), a.end()); n   size_t n = 1; n   while (n < (a.size() + b.size())) n <<= 1; n   fa.resize(n); n   vector<int> res; n   fft(fa, false); n   for (size_t i = 0; i < n; ++i) fa[i] *= fa[i]; n   fft(fa, true); n   res.resize(n); n   long long int carry = 0; n   for (size_t i = 0; i < n; ++i) { n     carry += (long long int)(fa[i].real() + 0.5); n     res[i] = carry % 1000; n     carry /= 1000; n   } n   int i; n   for (i = n - 1; i > 0; i--) { n     if (res[i] != 0) break; n   } n   res.resize(i + 1); n   return res; n } n void small_mult(vector<int>& a, int b) { n   int carry = 0; n   for (int i = 0; i < (int)a.size() || carry; i++) { n     if (i == (int)a.size()) a.push_back(0); n     int cur = carry + a[i] * b; n     a[i] = cur % 1000; n     carry = cur / 1000; n   } n   while (a.size() > 1 && !a.back()) a.pop_back(); n } n void fast_3_pow(long long int k, vector<int>& ans) { n   ans = {1}; n   if (k <= 0) return; n   long long int r = 0, kk = 0; n   for (long long int t = k; t > 0; t /= 2) { n     r = (2 * r + t % 2); n     kk++; n   } n   for (long long int i = 0; i < kk; i++) { n     ans = multiply(ans, ans); n     if (r % 2) small_mult(ans, 3); n     r /= 2; n   } n } n bool first_is_BE(vector<int>& v1, vector<int>& v2) { n   if (v1.size() > v2.size()) return 1; n   if (v1.size() < v2.size()) return 0; n   for (long long int i = ((long long int)v1.size()) - 1; i >= 0; i--) { n     if (v1[i] > v2[i]) return 1; n     if (v1[i] < v2[i]) return 0; n   } n   return 1; n } n int main() { n   ios::sync_with_stdio(0); n   string s; n   cin >> s; n   if (s ==  1 ) { n     cout <<  1 ; n     return 0; n   } n   if (s ==  2 ) { n     cout <<  2 ; n     return 0; n   } n   if (s ==  3 ) { n     cout <<  3 ; n     return 0; n   } n   if (s ==  4 ) { n     cout <<  4 ; n     return 0; n   } n   if (s ==  5 ) { n     cout <<  5 ; n     return 0; n   } n   if (s ==  6 ) { n     cout <<  5 ; n     return 0; n   } n   if (s ==  7 ) { n     cout <<  6 ; n     return 0; n   } n   if (s ==  8 ) { n     cout <<  6 ; n     return 0; n   } n   if (s ==  9 ) { n     cout <<  6 ; n     return 0; n   } n   vector<int> n; n   int tmp = 0; n   while (s.size() % 3 != 0) s =  0  + s; n   for (int i = 0; i < (int)s.size(); i++) { n     tmp *= 10; n     tmp += s[i] -  0 ; n     if ((i + 1) % 3 == 0) { n       n.push_back(tmp); n       tmp = 0; n     } n   } n   reverse(n.begin(), n.end()); n   vector<int> v1, vt; n   int k; n   k = max((int)(s.size() * logl(10) / logl(3) - 8), (int)0); n   fast_3_pow(k, v1); n   vt = v1; n   small_mult(v1, 4); n   while (1) { n     if (first_is_BE(v1, n)) { n       break; n     } n     k++; n     small_mult(v1, 3); n   } n   int ans3 = 3 * k + 4; n   k = max((int)(s.size() * logl(10) / logl(3) - 8), (int)0); n   v1 = vt; n   while (1) { n     if (first_is_BE(v1, n)) { n       break; n     } n     k++; n     small_mult(v1, 3); n   } n   int ans1 = 3 * k; n   k = max((int)(s.size() * logl(10) / logl(3) - 8), (int)0); n   v1 = vt; n   small_mult(v1, 2); n   while (1) { n     if (first_is_BE(v1, n)) { n       break; n     } n     k++; n     small_mult(v1, 3); n   } n   int ans2 = 3 * k + 2; n   cout << min(min(ans1, ans2), ans3); n   return 0; n } n


#include <bits/stdc++.h> n #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); n #define ll long long int n #define pb push_back n #define pob pop_back n #define ub upper_bound n #define lb lower_bound n #define mp make_pair n #define f0(i,n) for(i=0;i<n;i++) n #define rf0(i,n) for(i=n-1;i>=0;i--) n #define f2(i,n) for(i=1;i<n;i++) n #define f1(i,n) for(i=1;i<=n;i++) n #define fab(i,a,b) for(i=a;i<=b;i++) n #define shr  ll t;cin>>t; while(t--) n #define fi first n #define sc second n #define pll pair<ll,ll> n #define vll vector<ll> n #define vpll vector<pll> n #define all(v) v.begin(),v.end() n #define mod 1000000007 n #define dev(a) for(auto it:a)cout<<it<<    n #define prec(nm,prc) cout<<fixed << setprecision(nm)<<prc n #define print(a) cout<<a<<endl; n #define nl   n  n using namespace std; n bool comp(pll &a,pll &b) n { n     return (a.sc<b.sc); n } n ll power(ll n,ll p) n { n  if(p==0) n  return 1; n  else n  { n   ll ans=power(n,p/2)%mod; n   ans=(ans*ans)%mod; n   if(p%2==0) n   return ans; n   else n   return ans=(ans*n)%mod; n  } n } n bool isPrime(ll n)  n {  n     if (n <= 1)  n         return false;  n     if (n <= 3)  n         return true;  n    n     if (n % 2 == 0 || n % 3 == 0)  n         return false;  n    n     for (ll i = 5; i * i <= n; i = i + 6)  n         if (n % i == 0 || n % (i + 2) == 0)  n             return false;  n    n     return true;  n }  n  n  n     /********************HAR HAR MAHADEV***********JAI BAJRANG BALI************************/ n   int main() n  { n    FAST; n    /* ifstream cin; n     cin.open( input.txt ); n     ofstream cout; n     cout.open( output.txt ); n                       */ n    shr n      {   n       ll i,j,n,a,b,c,c1,d,m,sum=0,temp,k,k2,k1,ans=0,cnt=0,r=0,e=0,f; n       string str,stri,om,s; n         map<ll,ll>mp,mp1; n         set<ll>st; vector<vll> v; n         cin>>n>>k; ll x[n][2]; n         f0(i,n) n         { n             cin>>x[i][0]>>x[i][1]; n         } n        f0(i,n) n        { n           vll vk; n            f0(j,n) n           { n                n               if(i!=j) n               { n                   vk.pb(abs(x[i][0]-x[j][0])+abs(x[i][1]-x[j][1])); n               } n               n           } n            v.pb(vk); n           r++; n       } n       f0(i,v.size()) n       { n       ll p=*max_element(all(v[i])); n       if(p<=k){e=1;break;} n       } n       if(e==1)cout<<1; n       else cout<<-1; n       cout<<endl; n      } n       n     return 0; n   }                      n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n const double pi = acos(-1); n const int MOD = 1e9 + 7; n const int INF = 1e9 + 7; n const int MAXN = 10000; n const double eps = 1e-9; n using namespace std; n int n, m, cur; n set<int> s; n void dfs(int x, int y) { n   if (y == 0) { n     int tmp = abs(abs(x) - n); n     if (tmp < MAXN) { n       if (s.size() < m) s.insert(cur * MAXN + tmp); n       if (s.size() < m) s.insert(cur + MAXN * tmp); n     } n     return; n   } n   dfs(x + (y % 10), y / 10); n   dfs(x - (y % 10), y / 10); n   dfs(x * (y % 10), y / 10); n } n int main() { n   scanf( %d , &(n)), scanf( %d , &(m)); n   for (int i = 0; i < MAXN; i++) { n     cur = i; n     dfs(i % 10, i / 10); n   } n   auto it = s.begin(); n   for (; it != s.end(); it++) printf( %08d n , *it); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000000; n const int MAX = 400005; n const int MAX1 = 2007; n const int MAX2 = 4007; n const int LEN = 21; n const int BASE = 1000000000; n const double EPS = 1e-7; n const int MOD = 1234567891; n vector<pair<char, int> > vec; n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ifstream in(  ); n   ofstream out(  ); n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   for (int i = (0); i < ((int)s.size()); ++i) { n     vec.push_back(make_pair(s[i], i)); n   } n   sort(vec.begin(), vec.end()); n   char ans[MAX]; n   memset(ans,   0 , sizeof(ans)); n   for (int i = (k); i < ((int)vec.size()); ++i) { n     ans[vec[i].second] = vec[i].first; n   } n   for (int i = (0); i < (n); ++i) { n     if (ans[i] !=   0 ) cout << ans[i]; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int num; n int likes[7][7]; n map<string, int> heroes; n int getNum(string s) { n   if (!heroes.count(s)) heroes[s] = num++; n   return heroes[s]; n } n int getMaxSymp(int n1, int n2) { n   vector<int> p, groups(7, 0); n   for (int i = 0; i < 7; i++) { n     p.push_back(i); n     if (i >= n1) groups[i] = i < n1 + n2 ? 1 : 2; n   } n   int res = 0; n   do { n     int r = 0; n     for (int i = 0; i < 7; i++) n       for (int j = 0; j < 7; j++) n         if (groups[i] == groups[j]) r += likes[p[i]][p[j]]; n     res = max(r, res); n   } while (next_permutation(p.begin(), p.end())); n   return res; n } n int main() { n   int i, j, k, n, a, b, c, res = 2000000000, symp = 0; n   cin >> n; n   for (i = 0; i < n; i++) { n     string s1, s2, s3; n     cin >> s1 >> s3 >> s2; n     likes[getNum(s1)][getNum(s2)] = 1; n   } n   cin >> a >> b >> c; n   for (i = 1; i < 7; i++) n     for (j = 1; j + i < 7; j++) { n       k = 7 - i - j; n       int d = max(a / i, max(b / j, c / k)) - min(a / i, min(b / j, c / k)); n       if (d < res) n         res = d, symp = getMaxSymp(i, j); n       else if (res == d) n         symp = max(symp, getMaxSymp(i, j)); n     } n   cout << res <<     << symp << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n long long inv[5050 + 10], fac[5050 + 10], ifac[5050 + 10]; n void setComb() { n   inv[0] = 1; n   inv[1] = 1; n   fac[1] = 1; n   ifac[1] = 1; n   fac[0] = 1; n   ifac[0] = 1; n   for (int i = 2; i < 5050; i++) { n     inv[i] = (-MOD / i) * inv[MOD % i] % MOD; n     fac[i] = fac[i - 1] * i % MOD; n     ifac[i] = ifac[i - 1] * inv[i] % MOD; n     inv[i] = (inv[i] + MOD) % MOD; n     fac[i] = (fac[i] + MOD) % MOD; n     ifac[i] = (ifac[i] + MOD) % MOD; n   } n   return; n } n long long comb(long long n, long long k) { n   if (n < k || n < 0 || k < 0) n     return 0; n   else n     return ((fac[n] * ifac[k] % MOD * ifac[n - k] % MOD + MOD) % MOD); n } n long long hcomb(long long n, long long r) { n   if (n == 0 && r == 0) n     return 1; n   else if (n < 0 || r < 0) n     return 0; n   else n     return comb(n + r - 1, r - 1); n } n int N; n vector<int> A; n long long dp[5050][5050]; n long long cnt[5050]; n void add(long long &a, long long b) { a = (a + b) % MOD; } n void mul(long long &a, long long b) { a = a * b % MOD; } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   cin >> N; n   A.resize(N); n   for (int i = 0; i < (N); ++i) cin >> A[i]; n   sort(A.begin(), A.end()); n   for (int i = 0; i < (N); ++i) cnt[A[i]]++; n   setComb(); n   dp[0][0] = 1; n   for (int i = 0; i < N; i++) { n     long long sum = 0; n     for (int j = 0; j <= 5000; j++) { n       long long res = sum; n       mul(res, cnt[j]); n       mul(res, inv[N - i]); n       add(dp[i + 1][j], res); n       sum += dp[i][j]; n     } n   } n   long long ans = 0; n   for (int i = 1; i < N; i++) { n     for (int j = 0; j <= 5000; j++) { n       long long res = dp[i][j]; n       mul(res, cnt[j] - 1); n       mul(res, inv[N - i]); n       add(ans, res); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> v; n   vector<int>::iterator it; n   int n, q, x, count1 = 0, x1; n   cin >> q; n   while (q--) { n     cin >> n; n     while (n--) { n       cin >> x; n       v.push_back(x); n     } n     it = v.end() - 1; n     x1 = *it; n     for (it = v.end() - 2; it >= v.begin(); it--) { n       if (*it > x1) n         count1++; n       else n         x1 = *it; n     } n     cout << count1 << endl; n     count1 = 0; n     v.clear(); n   } n } n


#include <bits/stdc++.h> n long long ans; n int F[50000001], n, m, N; n char str[10001]; n void dfs(int x, int bit, int key) { n   if (x > 3) { n     if (bit) F[bit] += key; n     return; n   } n   dfs(x + 1, bit, key); n   int v = str[x] -  a ; n   dfs(x + 1, bit | (1 << v), -key); n } n int main() { n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     scanf( %s , str + 1); n     dfs(1, 0, -1); n   } n   n = 24; n   N = 1 << n; n   int res = 0; n   for (int i = 0; i < N; i++) res += F[i]; n   for (int i = 1; i < N; i <<= 1) n     for (int j = 0; j < N; j++) n       if (i & j) { n         F[j] += F[j - i]; n       } n   for (int i = 0; i < N; i++) ans ^= (long long)F[i] * F[i]; n   printf( %I64d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 10, MAXM = 100; n int A[MAXN][MAXM], B[MAXN][MAXM], C[MAXN][MAXM]; n int order[MAXM]; n int ib, is; n bool cmp(int a, int b) { return B[is][a] - A[ib][a] > B[is][b] - A[ib][b]; } n int main() { n   int n, m, k, ans = 0, tmp; n   int cur_hold, nx, p; n   int i, j, id; n   scanf( %d%d%d , &n, &m, &k); n   for (i = 0; i < n; ++i) { n     scanf( %*s ); n     for (j = 0; j < m; ++j) scanf( %d%d%d , A[i] + j, B[i] + j, C[i] + j); n   } n   for (ib = 0; ib < n; ++ib) n     for (is = 0; is < n; ++is) n       if (ib != is) { n         for (j = 0; j < m; ++j) order[j] = j; n         sort(order, order + m, cmp); n         for (tmp = cur_hold = j = 0; j < m; ++j) { n           id = order[j]; n           if ((p = B[is][id] - A[ib][id]) > 0) { n             nx = min(C[ib][id], k - cur_hold); n             tmp += p * nx; n             cur_hold += nx; n           } else n             break; n         } n         if (ans < tmp) ans = tmp; n       } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void init() {} n void _print(long long int t) { cerr << t; } n void _print(int t) { cerr << t; } n void _print(string t) { cerr << t; } n void _print(char t) { cerr << t; } n void _print(long double t) { cerr << t; } n void _print(double t) { cerr << t; } n void _print(unsigned long long int t) { cerr << t; } n template <class T, class V> n void _print(pair<T, V> p); n template <class T> n void _print(vector<T> v); n template <class T> n void _print(set<T> v); n template <class T, class V> n void _print(map<T, V> v); n template <class T> n void _print(multiset<T> v); n template <class T, class V> n void _print(pair<T, V> p) { n   cerr <<  { ; n   _print(p.first); n   cerr <<  , ; n   _print(p.second); n   cerr <<  } ; n } n template <class T> n void _print(vector<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(set<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(multiset<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T, class V> n void _print(map<T, V> v) { n   cerr <<  [  ; n   for (auto i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n long long int round_up(long long int a, long long int b) { n   return (a + b - 1) / b; n } n long long int gcd(long long int a, long long int b) { n   if (b > a) { n     return gcd(b, a); n   } n   if (b == 0) { n     return a; n   } n   return gcd(b, a % b); n } n long long int expo(long long int a, long long int b, long long int mod) { n   long long int res = 1; n   while (b > 0) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b = b >> 1; n   } n   return res; n } n void extendgcd(long long int a, long long int b, long long int *v) { n   if (b == 0) { n     v[0] = 1; n     v[1] = 0; n     v[2] = a; n     return; n   } n   extendgcd(b, a % b, v); n   long long int x = v[1]; n   v[1] = v[0] - v[1] * (a / b); n   v[0] = x; n   return; n } n long long int mminv(long long int a, long long int b) { n   long long int arr[3]; n   extendgcd(a, b, arr); n   return arr[0]; n } n long long int mminvprime(long long int a, long long int b) { n   return expo(a, b - 2, b); n } n bool revsort(long long int a, long long int b) { return a > b; } n void swap(int &x, int &y) { n   int temp = x; n   x = y; n   y = temp; n } n long long int combination(long long int n, long long int r, long long int m, n                           long long int *fact, long long int *ifact) { n   long long int val1 = fact[n]; n   long long int val2 = ifact[n - r]; n   long long int val3 = ifact[r]; n   return (((val1 * val2) % m) * val3) % m; n } n void google(int t) { cout <<  Case #  << t <<  :  ; } n vector<long long int> sieve(int n) { n   int *arr = new int[n + 1](); n   vector<long long int> vect; n   for (int i = 2; i <= n; i++) n     if (arr[i] == 0) { n       vect.push_back(i); n       for (int j = 2 * i; j <= n; j += i) arr[j] = 1; n     } n   return vect; n } n long long int mod_add(long long int a, long long int b, long long int m) { n   a = a % m; n   b = b % m; n   return (((a + b) % m) + m) % m; n } n long long int mod_mul(long long int a, long long int b, long long int m) { n   a = a % m; n   b = b % m; n   return (((a * b) % m) + m) % m; n } n long long int mod_sub(long long int a, long long int b, long long int m) { n   a = a % m; n   b = b % m; n   return (((a - b) % m) + m) % m; n } n long long int mod_div(long long int a, long long int b, long long int m) { n   a = a % m; n   b = b % m; n   return (mod_mul(a, mminvprime(b, m), m) + m) % m; n } n void solve() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     long long int sum = 0; n     long long int ans = 0; n     int arr[n]; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n       sum += arr[i]; n       if (sum < 0) { n         ans -= sum; n         sum = 0; n       } n     } n     cout << sum <<   n ; n   } n } n int main() { n   init(); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long int b[100005], p[100005]; n int n, m, a; n bool check(int x) { n   int temp_a = a; n   for (__typeof((x)) i = (0); i < (x); i += 1) { n     int t = x - 1; n     if (p[i] > b[t - i]) { n       if (temp_a < p[i] - b[t - i]) { n         return false; n       } else { n         temp_a -= p[i] - b[t - i]; n       } n     } n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> a; n   for (__typeof((n)) i = (0); i < (n); i += 1) { n     cin >> b[i]; n   } n   for (__typeof((m)) i = (0); i < (m); i += 1) { n     cin >> p[i]; n   } n   sort(b, b + n, greater<int>()); n   sort(p, p + m); n   int l = 0, h = ((n) < (m) ? (n) : (m)), ans = 0; n   while (l < h) { n     int mid = (l + h + 1) / 2; n     if (check(mid) == true) { n       l = mid; n     } else { n       h = mid - 1; n     } n     ans = l; n   } n   int ans2 = 0; n   for (__typeof((ans)) i = (0); i < (ans); i += 1) { n     ans2 += p[i]; n   } n   ans2 -= a; n   if (ans2 < 0) { n     ans2 = 0; n   } n   cout << ans <<     << ans2; n } n


#include <bits/stdc++.h> n using namespace std; n long long a00, a01, a10, a11; n char *s; n bool isSquare(long long x, long long &res) { n   long long t = sqrt(x); n   for (long long i = max(t - 10, 1LL); i <= t + 10; i++) { n     if (i * i == x) { n       res = i; n       return true; n     } n   } n   return false; n } n bool getS(long long n0, long long n1, long long n) { n   s = new char[n + 1]; n   long long A01 = a01, A10 = a10; n   for (long long i = 0; i < n; i++) { n     if (A01 - n1 >= 0) { n       s[i] =  0 ; n       n0 -= 1; n       A01 -= n1; n     } else if (A10 - n0 >= 0) { n       s[i] =  1 ; n       n1 -= 1; n       A10 -= n0; n     } else n       return false; n   } n   s[n] =   0 ; n   if (n0 != 0 || n1 != 0 || A01 != 0 || A10 != 0) return false; n   return true; n } n bool solve() { n   long long t; n   if (!isSquare(1 + 8 * a00, t)) return false; n   vector<long long> n0; n   n0.push_back((1 + t) / 2); n   n0.push_back((1 - t) / 2); n   if (!isSquare(1 + 8 * a11, t)) return false; n   vector<long long> n1; n   n1.push_back((1 + t) / 2); n   n1.push_back((1 - t) / 2); n   for (auto i : n0) n     for (auto j : n1) { n       long long n = i + j; n       if (i < 0 || j < 0 || n <= 0) continue; n       if (!getS(i, j, n)) continue; n       return true; n     } n   return false; n } n int main() { n   while (cin >> a00 >> a01 >> a10 >> a11) { n     bool can = solve(); n     printf( %s n , can ? s :  Impossible ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<long long>; n using pii = pair<long long, long long>; n const signed INF_ = 1001001001; n const long long INF = 1001001001001001001LL; n const long long DX[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0}, n                 DY[9] = {-1, 0, 1, 0, -1, 1, 1, -1, 0}; n template <class T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (auto i = (begin(v)); i != (end(v)); ++i) n     os << *i << (i == end(v) - 1 ?    :    ); n   return os; n } n template <class T> n istream &operator>>(istream &is, vector<T> &v) { n   for (auto i = (begin(v)); i != (end(v)); ++i) is >> *i; n   return is; n } n template <class T, class U> n istream &operator>>(istream &is, pair<T, U> &p) { n   is >> p.first >> p.second; n   return is; n } n template <class T, class U> n bool chmax(T &a, const U &b) { n   return a < b ? a = b, 1 : 0; n } n template <class T, class U> n bool chmin(T &a, const U &b) { n   return a > b ? a = b, 1 : 0; n } n template <class T> n void PSUM(T &c) { n   partial_sum(begin(c), end(c), begin(c)); n } n template <class T> n using heap = priority_queue<T, vector<T>, greater<T>>; n struct before_main_function { n   before_main_function() { n     cin.tie(0); n     ios::sync_with_stdio(0); n     cout << setprecision(15) << fixed; n   } n } before_main_function; n signed main() { n   long long N, M; n   cin >> N >> M; n   vector<vector<long long>> g(N); n   vector<long long> deg(N); n   for (long long i = 0, ilen = (long long)(M); i < ilen; ++i) { n     long long a, b; n     cin >> a >> b; n     a--, b--; n     g[a].push_back(b); n     g[b].push_back(a); n     deg[a]++; n     deg[b]++; n   } n   vector<pii> ans; n   vector<bool> vis(N); n   { n     queue<long long> q; n     long long st = max_element(begin(deg), end(deg)) - begin(deg); n     q.push(st); n     vis[st] = true; n     while (q.size()) { n       long long v = q.front(); n       q.pop(); n       for (auto &&w : g[v]) { n         if (vis[w]) continue; n         vis[w] = true; n         ans.push_back({v + 1, w + 1}); n         q.push(w); n       } n     } n   } n   for (auto &&p : ans) { n     cout << p.first <<     << p.second << endl; n   } n   return (0 ^ 0 ^ 0); n } n


#include <bits/stdc++.h> n using namespace std; n int rep[2000005]; n int sz[2000005]; n void dsu(int n) { n   for (int i = 0; i < n; i++) rep[i] = i, sz[i] = 1; n } n int findSet(int a) { return ((rep[a] == a) ? a : rep[a] = findSet(rep[a])); } n int mergeSet(int a, int b) { n   int y = findSet(b); n   int x = findSet(a); n   if (x != y) { n     rep[x] = y; n     sz[y] += sz[x]; n     sz[x] = 0; n   } n } n long long int grid[1005][1005]; n int vis[1005][1005] = {0}; n vector<pair<long long int, pair<int, int> > > v; n int n, m; n long long int k; n int dx[] = {-1, 0, 0, 1}; n int dy[] = {0, -1, 1, 0}; n int solx = -1; n int soly = -1; n long long int sol; n int cnt; n void floodfill(int x, int y) { n   if (cnt < 1) return; n   vis[x][y] = 1; n   grid[x][y] = sol; n   cnt--; n   for (int i = 0; i < 4; i++) { n     int newx = x + dx[i]; n     int newy = y + dy[i]; n     if (newx >= 0 && newx < n && newy >= 0 && newy < m && !vis[newx][newy] && n         grid[newx][newy] >= sol) { n       floodfill(newx, newy); n     } n   } n } n void solve() { n   cin >> n >> m; n   cin >> k; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> grid[i][j]; n       v.push_back(make_pair(grid[i][j], make_pair(i, j))); n     } n   } n   sort(v.begin(), v.end(), greater<pair<long long int, pair<int, int> > >()); n   dsu(n * m); n   int i = 0, j = 0; n   while (i < v.size()) { n     j = i; n     while (j < v.size()) { n       if (v[i].first == v[j].first) { n         int x = v[j].second.first; n         int y = v[j].second.second; n         for (int k = 0; k < 4; k++) { n           int newx = x + dx[k]; n           int newy = y + dy[k]; n           if (newx >= 0 && newx < n && newy >= 0 && newy < m && n               grid[newx][newy] >= v[j].first) { n             mergeSet(newx * m + newy, x * m + y); n           } n         } n       } else n         break; n       j++; n     } n     for (int l = i; l < j; l++) { n       if (v[l].first && (k % v[l].first == 0) && n           sz[findSet(v[l].second.first * m + v[l].second.second)] >= n               k / v[l].first) { n         solx = v[l].second.first; n         soly = v[l].second.second; n         sol = v[l].first; n         cnt = k / v[l].first; n         break; n       } n     } n     i = j; n   } n   if (solx == -1 || soly == -1) n     cout <<  NO n ; n   else { n     floodfill(solx, soly); n     cout <<  YES n ; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m - 1; j++) { n         if (vis[i][j]) n           cout << grid[i][j] <<    ; n         else n           cout <<  0  ; n       } n       if (vis[i][m - 1]) n         cout << grid[i][m - 1] <<   n ; n       else n         cout <<  0 n ; n     } n   } n } n int main() { n   solve(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5; n int edges[MAXN] = {0}; n int main() { n   int n, s; n   cin >> n >> s; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--, b--; n     edges[a]++, edges[b]++; n   } n   int amt = 0; n   for (int i = 0; i < n; i++) { n     if (edges[i] == 1) amt++; n   } n   double ans = ((double)s) / ((double)amt); n   cout << fixed << setprecision(10) << ans * 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int v[100]; n bool cmp(int a, int b) { return a > b; } n int highest(int n) { n   int i = 1, fact = 0; n   while (2 * i <= n) { n     i *= 2; n     fact++; n   } n   return fact; n } n void permute(int left, int right, int pos, int last) { n   if (!pos || (!last && pos == 1)) return; n   int t = highest(pos); n   pos -= (1 << t); n   if (!last) { n     left = right - t - 1; n     swap(v[left + 1], v[right]); n     sort(v + left + 2, v + right, cmp); n     permute(left, right, pos, t); n     return; n   } n   swap(v[left], v[right]); n   --right; n   if (last - t != 1) { n     ++left; n     sort(v + left, v + right + 1); n     permute(left, right, pos + (1 << t), 0); n     return; n   } n   permute(left, right, pos, t); n } n int main() { n   int n, m, num = 1, piv = 0, pos, t; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) v[i] = i + 1; n   permute(0, n - 1, m, 0); n   for (int i = 0; i < n; ++i) cout << v[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n class Solver { n  public: n   Solver() = default; n   void read() { n     int n; n     std::cin >> n; n     int left = 0; n     int right = 0; n     for (int i = 0; i < n; ++i) { n       int x, y; n       std::cin >> x >> y; n       if (x < 0) n         left++; n       else n         right++; n     } n     if (left <= 1 || right <= 1) n       std::cout <<  Yes n ; n     else n       std::cout <<  No n ; n   } n   void solve() {} n   void write() {} n }; n int main() { n   Solver solver; n   solver.read(); n   solver.solve(); n   solver.write(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int flow, n, m, S, T, tot, x, y; n int g[3000], dis[3000], pre[3000]; n int Next[50000], adj[50000], f[50000], c[50000]; n bool used[3000]; n queue<int> q; n int ans = 0; n int calc[110][30]; n string t, s; n void add(int x, int y, int flow, int cost) { n   adj[tot] = y; n   c[tot] = cost; n   f[tot] = flow; n   Next[tot] = g[x]; n   g[x] = tot++; n   adj[tot] = x; n   c[tot] = -cost; n   f[tot] = 0; n   Next[tot] = g[y]; n   g[y] = tot++; n } n bool spfa() { n   for (int i = 1; i <= T; i++) dis[i] = 1 << 23; n   dis[S] = 0; n   memset(used, 0, sizeof(used)); n   while (!q.empty()) q.pop(); n   q.push(S); n   used[S] = true; n   int p; n   while (!q.empty()) { n     x = q.front(); n     used[x] = false; n     p = g[x]; n     while (p != -1) { n       y = adj[p]; n       if (f[p] && dis[x] + c[p] < dis[y]) { n         dis[y] = dis[x] + c[p]; n         pre[y] = p; n         if (!used[y]) { n           used[y] = true; n           q.push(y); n         } n       } n       p = Next[p]; n     } n     q.pop(); n   } n   return dis[T] != 1 << 23; n } n int path() { n   int del = 1 << 23; n   int p = T; n   while (p != S) { n     if (f[pre[p]] < del) del = f[pre[p]]; n     p = adj[pre[p] ^ 1]; n   } n   p = T; n   while (p != S) { n     f[pre[p]] -= del; n     f[pre[p] ^ 1] += del; n     p = adj[pre[p] ^ 1]; n   } n   flow += del; n   return del * dis[T]; n } n void solve() { n   memset(pre, 0, sizeof(pre)); n   while (spfa()) ans += path(); n   if (flow != t.size()) n     cout << -1 << endl; n   else n     cout << ans << endl; n } n int main() { n   cin >> t; n   cin >> n; n   memset(g, -1, sizeof(g)); n   tot = 0; n   S = 26 * (n + 1) + n + 1; n   T = S + 1; n   for (int i = 1; i <= n; i++) { n     cin >> s >> x; n     for (int j = 0; j < s.size(); j++) calc[i][s[j] -  a ]++; n     add(S, 26 * (n + 1) + i, x, 0); n   } n   for (int i = 0; i < t.size(); i++) calc[n + 1][t[i] -  a ]++; n   for (int i = 0; i < 26; i++) add(26 * n + i + 1, T, calc[n + 1][i], 0); n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < 26; j++) { n       add(26 * (n + 1) + i, (i - 1) * 26 + j + 1, 1 << 23, 0); n       add((i - 1) * 26 + j + 1, 26 * n + j + 1, calc[i][j], i); n     } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   clock_t clk_begin = clock(); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   string s; n   cin >> s; n   int c = 0; n   for (int i = 0; i < n; ++i) n     if (s[i] ==  8 ) c++; n   n /= 11; n   cout << min(n, c) <<   n ; n   ; n   cerr <<  Time taken:   << (double)(clock() - clk_begin) / CLOCKS_PER_SEC n        <<   n ; n   return 0; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n int form[9], piece[9][2]; n map<int, int> res; n string result[] = { n      illegal ,  the first player won ,  the second player won ,  draw ,  first , n      second }; n void pre() { n   form[0] = 3; n   piece[0][0] = 1; n   piece[0][1] = 1 << 1; n   for (int i = 1; i < 9; i++) { n     form[i] = form[i - 1] << 2; n     piece[i][0] = piece[i - 1][0] << 2; n     piece[i][1] = piece[i - 1][1] << 2; n   } n } n int add(int now, int p, int who, bool end) { n   int px = p / 3 * 3, py = p % 3; n   if ((piece[px][who] & now) && (piece[px + 1][who] & now) && n       (piece[px + 2][who] & now)) n     return who + 1; n   if ((piece[py][who] & now) && (piece[py + 3][who] & now) && n       (piece[py + 6][who] & now)) n     return who + 1; n   if ((p == 0 || p == 4 || p == 8) && (piece[0][who] & now) && n       (piece[4][who] & now) && (piece[8][who] & now)) n     return who + 1; n   if ((p == 2 || p == 4 || p == 6) && (piece[2][who] & now) && n       (piece[4][who] & now) && (piece[6][who] & now)) n     return who + 1; n   return end ? 3 : 5 - who; n } n void enumeration(int now, int who, int round) { n   for (int p = 0; p < 9; p++) { n     if (now & form[p]) continue; n     now ^= piece[p][who]; n     if (!res[now]) { n       res[now] = add(now, p, who, round == 9); n       if (res[now] > 3) enumeration(now, who ^ 1, round + 1); n     } n     now ^= piece[p][who]; n   } n } n int main() { n   int now = 0; n   char ch; n   pre(); n   enumeration(now, 0, 1); n   for (int p = 0; p < 9; p++) { n     cin >> ch; n     if (ch ==  X ) n       now ^= piece[p][0]; n     else if (ch ==  0 ) n       now ^= piece[p][1]; n   } n   cout << result[res[now]] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int parent(int x, vector<int> &v) { n   return v[x] = (v[x] == x ? x : parent(v[x], v)); n } n void u(int x, int y, vector<int> &v, vector<int> &r) { n   x = parent(x, v); n   y = parent(y, v); n   if (r[x] == r[y]) { n     if (x == y) { n       return; n     } n     r[x]++; n   } n   if (r[x] > r[y]) { n     v[y] = x; n   } else { n     v[x] = y; n   } n   return; n } n int main() { n   int n; n   scanf( %d n , &n); n   vector<int> v(26, -1); n   vector<int> r(26); n   string s; n   for (int i = 0; i < n; i++) { n     cin >> s; n     if (v[s[0] -  a ] == -1) { n       v[s[0] -  a ] = s[0] -  a ; n     } n     for (int i = 1; i < (int)s.size(); i++) { n       if (v[s[i] -  a ] == -1) { n         v[s[i] -  a ] = s[i] -  a ; n       } n       u(s[i] -  a , s[0] -  a , v, r); n     } n   } n   unordered_map<int, int> h; n   for (int i = 0; i < 26; i++) { n     if (v[i] != -1) { n       h[parent(v[i], v)] = 1; n     } n   } n   cout << h.size(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105], b[105]; n int main() { n   int n, i, j, k, x, y; n   while (~scanf( %d , &n)) { n     for (i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n     } n     scanf( %d%d , &x, &y); n     for (i = 1, j = 0; i <= n; i++) { n       int count1 = 0, count2 = 0; n       for (k = 1; k <= n; k++) { n         if (k >= i) n           count1 += a[k]; n         else n           count2 += a[k]; n       } n       if ((count1 >= x && count1 <= y) && (count2 >= x && count2 <= y)) { n         b[j++] = i; n         break; n       } n     } n     if (j == 0) { n       printf( 0 n ); n       continue; n     } n     printf( %d n , b[0]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<vector<int>> a(n, vector<int>(m, 0)); n   vector<vector<int>> b(n, vector<int>(m, 0)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n     } n   } n   vector<pair<int, int>> ans; n   for (int i = 0; i < n - 1; i++) { n     for (int j = 0; j < m - 1; j++) { n       if (a[i][j] == 1 && a[i][j + 1] == 1 && a[i + 1][j + 1] == 1 && n           a[i + 1][j] == 1) { n         ans.push_back(make_pair(i, j)); n         b[i][j] = 1; n         b[i][j + 1] = 1; n         b[i + 1][j + 1] = 1; n         b[i + 1][j] = 1; n       } n     } n   } n   bool flag = true; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (a[i][j] != b[i][j]) { n         flag = false; n         i = n; n         break; n       } n     } n   } n   if (flag) { n     cout << ans.size() << endl; n     for (auto it = ans.begin(); it != ans.end(); it++) { n       cout << (*it).first + 1 <<     << (*it).second + 1 << endl; n     } n   } else n     cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, const U &b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, const U &b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T1, class T2> n inline void gn(T1 &x1, T2 &x2) { n   gn(x1), gn(x2); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n vector<int> adj[500500]; n int can0[500500], can1[500500], dp0[500500], dp1[500500]; n int dp[4]; n int n, sz[500500]; n void dfs(int u, int f = 0) { n   sz[u] = 1; n   for (int v : adj[u]) n     if (v != f) dfs(v, u), sz[u] += sz[v]; n   can0[u] = 1; n   dp[0] = 1, dp[1] = 0; n   for (int v : adj[u]) { n     if (v == f) continue; n     can0[u] &= can1[v]; n     int t[2] = {dp[0], dp[1]}; n     dp[0] = t[0] * can1[v]; n     dp[1] = t[0] * can0[v] + t[1] * can1[v]; n   } n   can1[u] = dp[1]; n   dp[0] = 1, dp[1] = 0; n   for (int v : adj[u]) { n     if (v == f) continue; n     int t[2] = {dp[0], dp[1]}; n     dp[0] = t[0] * can1[v]; n     dp[1] = t[1] * can1[v] + t[0] * dp1[v]; n   } n   dp0[u] = dp[1]; n   dp1[u] = can0[u]; n   dp[0] = 1, dp[1] = 0; n   for (int v : adj[u]) { n     if (v == f) continue; n     int t[2] = {dp[0], dp[1]}; n     dp[0] = t[0] * can1[v]; n     dp[1] = t[1] * can1[v] + t[0] * dp0[v]; n   } n   dp1[u] += dp[1]; n   dp[0] = 1, dp[1] = dp[2] = dp[3] = 0; n   for (int v : adj[u]) { n     if (v == f) continue; n     int t[4] = {dp[0], dp[1], dp[2], dp[3]}; n     dp[0] = t[0] * can1[v]; n     dp[1] = t[0] * can0[v] + t[1] * can1[v]; n     dp[2] = t[0] * dp1[v] + t[2] * can1[v]; n     dp[3] = t[1] * dp1[v] + t[2] * can0[v] + t[3] * can1[v]; n   } n   dp1[u] += dp[3]; n } n long long ans; n int c0[500500], c1[500500], d0[500500], d1[500500]; n int R[500500], Rdp[500500][4]; n void redfs(int u, int f = 0) { n   if (f) { n     if (can1[u] && c1[u]) n       ans += (long long)sz[u] * (n - sz[u]); n     else n       ans += (long long)dp1[u] * d1[u]; n   } n   R[adj[u].size()] = 1; n   Rdp[adj[u].size()][0] = 1; n   Rdp[adj[u].size()][1] = 0; n   for (int i = adj[u].size(); i--;) { n     int v = adj[u][i]; n     R[i] = R[i + 1] & (v == f ? c1[u] : can1[v]); n     Rdp[i][0] = Rdp[i + 1][0] * (v == f ? c1[u] : can1[v]); n     Rdp[i][1] = Rdp[i + 1][0] * (v == f ? c0[u] : can0[v]) + n                 Rdp[i + 1][1] * (v == f ? c1[u] : can1[v]); n   } n   int cur = 1; n   dp[0] = 1, dp[1] = 0; n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v != f) { n       c0[v] = cur & R[i + 1]; n       c1[v] = dp[0] & Rdp[i + 1][1] | dp[1] & Rdp[i + 1][0]; n     } n     int t[2] = {dp[0], dp[1]}; n     cur &= (v == f ? c1[u] : can1[v]); n     dp[0] = t[0] * (v == f ? c1[u] : can1[v]); n     dp[1] = n         t[0] * (v == f ? c0[u] : can0[v]) + t[1] * (v == f ? c1[u] : can1[v]); n   } n   Rdp[adj[u].size()][0] = 1; n   Rdp[adj[u].size()][1] = 0; n   for (int i = adj[u].size(); i--;) { n     int v = adj[u][i]; n     Rdp[i][0] = Rdp[i + 1][0] * (v == f ? c1[u] : can1[v]); n     Rdp[i][1] = Rdp[i + 1][0] * (v == f ? d1[u] : dp1[v]) + n                 Rdp[i + 1][1] * (v == f ? c1[u] : can1[v]); n   } n   dp[0] = 1, dp[1] = 0; n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v != f) d0[v] = dp[0] * Rdp[i + 1][1] + dp[1] * Rdp[i + 1][0]; n     int t[2] = {dp[0], dp[1]}; n     dp[0] = t[0] * (v == f ? c1[u] : can1[v]); n     dp[1] = n         t[1] * (v == f ? c1[u] : can1[v]) + t[0] * (v == f ? d1[u] : dp1[v]); n   } n   for (int v : adj[u]) n     if (v != f) d1[v] = c0[v]; n   Rdp[adj[u].size()][0] = 1; n   Rdp[adj[u].size()][1] = 0; n   for (int i = adj[u].size(); i--;) { n     int v = adj[u][i]; n     Rdp[i][0] = Rdp[i + 1][0] * (v == f ? c1[u] : can1[v]); n     Rdp[i][1] = Rdp[i + 1][0] * (v == f ? d0[u] : dp0[v]) + n                 Rdp[i + 1][1] * (v == f ? c1[u] : can1[v]); n   } n   dp[0] = 1, dp[1] = 0; n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v != f) d1[v] += dp[0] * Rdp[i + 1][1] + dp[1] * Rdp[i + 1][0]; n     int t[2] = {dp[0], dp[1]}; n     dp[0] = t[0] * (v == f ? c1[u] : can1[v]); n     dp[1] = n         t[1] * (v == f ? c1[u] : can1[v]) + t[0] * (v == f ? d0[u] : dp0[v]); n   } n   Rdp[adj[u].size()][0] = 1; n   Rdp[adj[u].size()][1] = 0; n   Rdp[adj[u].size()][2] = 0; n   Rdp[adj[u].size()][3] = 0; n   for (int i = adj[u].size(); i--;) { n     int v = adj[u][i]; n     Rdp[i][0] = Rdp[i + 1][0] * (v == f ? c1[u] : can1[v]); n     Rdp[i][1] = Rdp[i + 1][0] * (v == f ? c0[u] : can0[v]) + n                 Rdp[i + 1][1] * (v == f ? c1[u] : can1[v]); n     Rdp[i][2] = Rdp[i + 1][0] * (v == f ? d1[u] : dp1[v]) + n                 Rdp[i + 1][2] * (v == f ? c1[u] : can1[v]); n     Rdp[i][3] = Rdp[i + 1][1] * (v == f ? d1[u] : dp1[v]) + n                 Rdp[i + 1][2] * (v == f ? c0[u] : can0[v]) + n                 Rdp[i + 1][3] * (v == f ? c1[u] : can1[v]); n   } n   dp[0] = 1, dp[1] = dp[2] = dp[3] = 0; n   for (int i = 0; i < adj[u].size(); i++) { n     int v = adj[u][i]; n     if (v != f) n       d1[v] += dp[0] * Rdp[i + 1][3] + dp[1] * Rdp[i + 1][2] + n                dp[2] * Rdp[i + 1][1] + dp[3] * Rdp[i + 1][0]; n     int t[4] = {dp[0], dp[1], dp[2], dp[3]}; n     dp[0] = t[0] * (v == f ? c1[u] : can1[v]); n     dp[1] = n         t[0] * (v == f ? c0[u] : can0[v]) + t[1] * (v == f ? c1[u] : can1[v]); n     dp[2] = n         t[0] * (v == f ? d1[u] : dp1[v]) + t[2] * (v == f ? c1[u] : can1[v]); n     dp[3] = t[1] * (v == f ? d1[u] : dp1[v]) + n             t[2] * (v == f ? c0[u] : can0[v]) + n             t[3] * (v == f ? c1[u] : can1[v]); n   } n   for (int v : adj[u]) n     if (v != f) redfs(v, u); n } n int main() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     int u, v; n     gn(u, v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(1); n   redfs(1); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using pii = pair<int, int>; n  n void solve() { n  int m, k; n  cin >> m >> k; n  n  vector<pii> a; n  n  int ai; n  for (int i = 1; i <= k; i++) { n   cin >> ai; n   a.push_back({ai, i}); n  } n  n  sort(a.begin(), a.end(), greater<pii>()); n  n  int n = 1; n  for (; ; n++) { n   if (m > n * n - (n / 2) * (n / 2)) continue; n   if (a[0].first > n * ((n + 1) / 2)) continue; n   break; n  } n   n  vector<pii> pos[3]; n  for (int i = 0; i < n; i++) { n   for (int j = 0; j < n; j++) { n    int idx = ((i & 1) << 1) + (j & 1); n    if (idx != 3)  n     pos[idx].push_back({i, j}); n   } n  } n  n  vector<vector<int>> ans(n, vector<int>(n, 0)); n  n  for (auto [cnt, val] : a) { n   vector<pii> &curr = pos[1].empty() ? pos[2] : pos[1]; n  n   for (; !curr.empty() && cnt > 0; cnt--) { n    auto [x, y] = curr.back(); n    curr.pop_back(); n    ans[x][y] = val; n   } n   for (; cnt > 0; cnt--) { n    auto [x, y] = pos[0].back(); n    pos[0].pop_back(); n    ans[x][y] = val; n   } n  } n  n  cout << n <<   n ; n  for (int i = 0; i < n; i++) { n   for (int j = 0; j < n; j++) { n    cout << ans[i][j] <<    ; n   } n   cout <<   n ; n  } n } n  n int main() { n  ios_base::sync_with_stdio(0); n  cin.tie(0); n  n  int t; n  cin >> t; n  n  while (t--) { n   solve(); n  } n  n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 211, M = N * N / 2; n int u[M], v[M], w[M]; n int f[N][N]; n bool check(int n, int m, int lim) { n   for (int i = 1; i <= m; i++) { n     vector<pair<int, int>> v; n     bool t = 1; n     for (int j = 1; j <= n; j++) { n       int l = f[j][u[i]], r = f[j][::v[i]]; n       if (lim >= l && lim >= r) { n         if (2 * lim - l - r >= w[i]) { n           v.push_back({0, 1}); n           v.push_back({w[i] + 1, -1}); n         } else { n           v.push_back({0, 1}); n           v.push_back({min(w[i], lim - l) + 1, -1}); n           v.push_back({w[i] - min(w[i], lim - r), 1}); n           v.push_back({w[i] + 1, -1}); n         } n       } else if (lim >= l) { n         v.push_back({0, 1}); n         v.push_back({min(w[i], lim - l) + 1, -1}); n       } else if (lim >= r) { n         v.push_back({w[i] - min(w[i], lim - r), 1}); n         v.push_back({w[i] + 1, -1}); n       } else { n         t = 0; n         break; n       } n     } n     if (!t) continue; n     sort(v.begin(), v.end()); n     int cnt = 0; n     for (auto e : v) { n       cnt += e.second; n       if (cnt == n) return 1; n     } n   } n   return 0; n } n int main() { n   ios ::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   memset(f, 0x3f, sizeof f); n   for (int i = 1; i <= n; i++) f[i][i] = 0; n   for (int i = 1; i <= m; i++) { n     cin >> u[i] >> v[i] >> w[i]; n     w[i] *= 2; n     f[u[i]][v[i]] = f[v[i]][u[i]] = w[i]; n   } n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) f[i][j] = min(f[i][j], f[i][k] + f[k][j]); n   int l = 0, r = 1e8L; n   while (l + 1 < r) { n     int im = (l + r) / 2; n     if (check(n, m, im - 1)) n       r = im; n     else n       l = im; n   } n   cout << fixed << setprecision(1); n   cout << l / 2.0 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int ans1, ans2; n int t[4], v[4][10], T[10][10], A, B, C; n map<string, int> mymap; n const char *help[] = { Anka ,    Chapay ,  Cleo ,        Troll , n                        Dracul ,  Snowy ,   Hexadecimal }; n int countT() { n   int ret = 0; n   for (int k = 0; k < 3; k++) { n     for (int i = 0; i < t[k]; i++) n       for (int j = 0; j < t[k]; j++) n         if (T[v[k][i]][v[k][j]]) ret++; n   } n   return ret; n } n void go(int x) { n   if (x == 8) { n     if (!t[0] || !t[1] || !t[2]) return; n     int h[4]; n     h[0] = A / t[0]; n     h[1] = B / t[1]; n     h[2] = C / t[2]; n     sort(h, h + 3); n     int d = h[2] - h[0]; n     int p = countT(); n     if (d < ans1) { n       ans1 = d; n       ans2 = p; n     } else if (d == ans1) { n       ans2 = max(ans2, p); n     } n     return; n   } n   for (int i = 0; i < 3; i++) { n     v[i][t[i]++] = x; n     go(x + 1); n     t[i]--; n   } n   return; n } n int main() { n   char s1[30], s2[30], s3[30]; n   for (int i = 0; i < 7; i++) mymap[help[i]] = i + 1; n   memset(T, 0, sizeof(T)); n   int m; n   cin >> m; n   while (m--) { n     cin >> s1 >> s2 >> s3; n     int u, v; n     u = mymap[s1]; n     v = mymap[s3]; n     T[u][v] = 1; n   } n   cin >> A >> B >> C; n   ans1 = 2000000001; n   ans2 = 0; n   memset(t, 0, sizeof(t)); n   go(1); n   cout << ans1 <<     << ans2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n typedef long long ll ; n using namespace std; n const int maxn=2e5+5; n const int maxm=5e6+5; n #define P pair<int,int> n #define fi first n #define se second n int t,n; n int a[maxn]; n int vis[maxm*2]; n P mp[maxm]; n  n  n int main() { n     ios::sync_with_stdio(false); n     cin.tie(nullptr), cout.tie(nullptr); n     cin>>n; n     for(int i=1;i<=n;i++){ n         cin>>a[i]; n     } n     int sum1; n     for(int i=1;i<=n;i++){ n         for(int j=i+1;j<=n;j++){ n             sum1=a[i]+a[j]; n             if(mp[sum1].fi==0 &&mp[sum1].se==0){ n                 mp[sum1]={i,j}; n             }else if(i!=mp[sum1].fi&&i!=mp[sum1].se&&j!=mp[sum1].fi&&j!=mp[sum1].se){ n                 cout<< YES <<endl; n                 cout<<i<<   <<j<<   <<mp[sum1].fi<<   <<mp[sum1].se<<endl; n                 return 0; n             } n         } n     } n     cout<< NO <<endl; n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T BigMod(T b, T p, T m) { n   if (p == 0) return 1; n   if (p % 2 == 0) { n     T s = BigMod(b, p / 2, m); n     return ((s % m) * (s % m)) % m; n   } n   return ((b % m) * (BigMod(b, p - 1, m) % m)) % m; n } n template <typename T> n T ModInv(T b, T m) { n   return BigMod(b, m - 2, m); n } n template <typename T> n T in() { n   char ch; n   T n = 0; n   bool ng = false; n   while (1) { n     ch = getchar(); n     if (ch ==  - ) { n       ng = true; n       ch = getchar(); n       break; n     } n     if (ch >=  0  && ch <=  9 ) break; n   } n   while (1) { n     n = n * 10 + (ch -  0 ); n     ch = getchar(); n     if (ch <  0  || ch >  9 ) break; n   } n   return (ng ? -n : n); n } n template <typename T> n T POW(T B, T printf) { n   if (printf == 0) return 1; n   if (printf & 1) n     return B * POW(B, printf - 1); n   else n     return (POW(B, printf / 2) * POW(B, printf / 2)); n } n template <typename T> n T Bigmod(T b, T p, T m) { n   if (p == 0) n     return 1; n   else if (!(p & 1)) n     return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m; n   else n     return ((b % m) * Bigmod(b, p - 1, m)) % m; n } n template <typename T> n T Dis(T x1, T y1, T x2, T y2) { n   return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2)); n } n template <typename T> n T Angle(T x1, T y1, T x2, T y2) { n   return atan(double(y1 - y2) / double(x1 - x2)); n } n template <typename T> n T DIFF(T a, T b) { n   T d = a - b; n   if (d < 0) n     return -d; n   else n     return d; n } n template <typename T> n T ABS(T a) { n   if (a < 0) n     return -a; n   else n     return a; n } n template <typename T> n T gcd(T a, T b) { n   if (a < 0) return gcd(-a, b); n   if (b < 0) return gcd(a, -b); n   return (b == 0) ? a : gcd(b, a % b); n } n template <typename T> n T lcm(T a, T b) { n   if (a < 0) return lcm(-a, b); n   if (b < 0) return lcm(a, -b); n   return a * (b / gcd(a, b)); n } n template <typename T> n T euclide(T a, T b, T &x, T &y) { n   if (a < 0) { n     T d = euclide(-a, b, x, y); n     x = -x; n     return d; n   } n   if (b < 0) { n     T d = euclide(a, -b, x, y); n     y = -y; n     return d; n   } n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } else { n     T d = euclide(b, a % b, x, y); n     T t = x; n     x = y; n     y = t - (a / b) * y; n     return d; n   } n } n template <typename T> n void ia(T a[], int n) { n   for (int i = 0; i < n; i++) cin >> a[i]; n } n template <typename T> n void pa(T a[], int n) { n   for (int i = 0; i < n - 1; i++) cout << a[i] <<    ; n   cout << a[n - 1] << endl; n } n template <typename T> n long long int isLeft(T a, T b, T c) { n   return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y); n } n int Set(int N, int pos) { return N = N | (1 << pos); } n int Reset(int N, int pos) { return N = N & ~(1 << pos); } n bool Check(int N, int pos) { return (bool)(N & (1 << pos)); } n template <class T, class first> n inline T togglebit(T a, first i) { n   T t = 1; n   return (a ^ (t << i)); n } n int toInt(string s) { n   int sm; n   stringstream ss(s); n   ss >> sm; n   return sm; n } n int toLlint(string s) { n   long long int sm; n   stringstream ss(s); n   ss >> sm; n   return sm; n } n int cdigittoint(char ch) { return ch -  0 ; } n bool isVowel(char ch) { n   ch = toupper(ch); n   if (ch ==  A  || ch ==  U  || ch ==  I  || ch ==  O  || ch ==  E ) n     return true; n   return false; n } n bool isConst(char ch) { n   if (isalpha(ch) && !isVowel(ch)) return true; n   return false; n } n double DEG(double x) { return (180.0 * x) / (2.0 * acos(0.0)); } n double RAD(double x) { return (x * (double)2.0 * acos(0.0)) / (180.0); } n vector<int> adj[200007], dag[200007], par[200007]; n int DP[200007][18], siz[200007]; n long long int dis[200007]; n int L[200007]; n int inDeg[200007]; n bool vis[200007]; n void Init(int n) { n   for (int i = 0; i <= n; i++) { n     dis[i] = 1LL << 60; n     adj[i].clear(); n     dag[i].clear(); n     par[i].clear(); n     inDeg[i] = siz[i] = vis[i] = 0; n     for (int j = 0; j < 18; j++) DP[i][j] = -1; n   } n } n void Update(int u, int p) { n   DP[u][0] = p; n   L[u] = L[p] + 1; n   adj[p].push_back(u); n   for (int i = 1; i < 18; i++) n     if (DP[u][i - 1] != -1) DP[u][i] = DP[DP[u][i - 1]][i - 1]; n } n int LCA(int p, int q) { n   if (L[p] < L[q]) swap(p, q); n   for (int i = 18 - 1; i >= 0; i--) n     if (L[p] - (1 << i) >= L[q]) p = DP[p][i]; n   if (p == q) return p; n   for (int i = 18 - 1; i >= 0; i--) n     if (DP[p][i] != -1 && DP[p][i] != DP[q][i]) { n       p = DP[p][i]; n       q = DP[q][i]; n     } n   return DP[p][0]; n } n void Dijkstra(int n, int s) { n   set<pair<long long int, long long int> > pq; n   pq.insert(make_pair(0, s)); n   dis[s] = 0; n   L[s] = 1; n   while (!pq.empty()) { n     pair<long long int, long long int> tmp = *pq.begin(); n     pq.erase(pq.begin()); n     int u = tmp.second; n     for (int i = 0; i < adj[u].size(); i += 2) { n       int v = adj[u][i]; n       int w = adj[u][i + 1]; n       if (dis[v] > dis[u] + w) { n         dis[v] = dis[u] + w; n         pq.insert(make_pair(dis[v], v)); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < adj[i].size(); j += 2) { n       int v = adj[i][j]; n       int w = adj[i][j + 1]; n       if (dis[i] + w == dis[v]) { n         inDeg[v]++; n         dag[i].push_back(v); n         par[v].push_back(i); n       } n     } n     adj[i].clear(); n   } n } n void TopSort(int s) { n   queue<int> q; n   q.push(s); n   L[s] = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     if (!par[u].empty()) { n       int lca = par[u][0]; n       for (int i = 1; i < par[u].size(); i++) { n         lca = LCA(lca, par[u][i]); n       } n       Update(u, lca); n     } n     for (int i = 0; i < dag[u].size(); i++) { n       int v = dag[u][i]; n       inDeg[v]--; n       if (!inDeg[v]) q.push(v); n     } n   } n } n void Dfs(int u) { n   siz[u] = 1; n   vis[u] = 1; n   for (int i = 0; i < adj[u].size(); i++) { n     Dfs(adj[u][i]); n     siz[u] += siz[adj[u][i]]; n   } n } n int main() { n   int n, m, s; n   scanf( %d%d%d , &n, &m, &s); n   Init(n); n   for (int i = 1; i <= m; i++) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     adj[u].push_back(v); n     adj[u].push_back(w); n     adj[v].push_back(u); n     adj[v].push_back(w); n   } n   Dijkstra(n, s); n   TopSort(s); n   Dfs(s); n   int mx = 0; n   for (int i = 1; i <= n; i++) { n     if (i != s) mx = max(mx, siz[i]); n   } n   cout << mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 2048; n char s[M], b[M], e[M], fb[M], fe[M]; n int r[2][M], *p = r[0], *q = r[1], h[M]; n int main(void) { n   cin >> s >> b >> e; n   int ns = strlen(s), nb = strlen(b), ne = strlen(e), nn = nb > ne ? nb : ne, n       w = 0; n   for (int i = ns; i-- > 0;) { n     if (!strncmp(s + i, b, nb)) fb[i] = 1; n     if (!strncmp(s + i, e, ne)) fe[i] = 1; n   } n   for (int i = 0, *t; i < ns; ++i, t = p, p = q, q = t) n     for (int j = i; j-- > 0;) { n       q[j + 1] = (s[i] == s[j]) ? (p[j] + 1) : 0; n       if (h[i] < q[j + 1]) h[i] = q[j + 1]; n     } n   for (int i = ns; i-- > 0;) n     if (fb[i]) n       for (int j = i + nn; j <= ns; ++j) n         if (fe[j - ne] && h[j - 1] < j - i) ++w; n   cout << w << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int inv[25]; n long long a[25]; n int power(int a, int b) { n   int c = 1; n   for (; b; b >>= 1) { n     if (b & 1) c = (long long)c * a % mod; n     a = (long long)a * a % mod; n   } n   return c; n } n int C(long long y, int x) { n   if (y < 0 || x < 0 || y < x) return 0; n   y %= mod; n   if (y == 0 || x == 0) return 1; n   int ans = 1; n   for (int i = 0; i < x; i++) ans = (long long)ans * (y - i) % mod; n   for (int i = 1; i <= x; i++) ans = (long long)ans * inv[i] % mod; n   return ans; n } n int main() { n   int n; n   long long m; n   long long ans = 0; n   for (int i = 1; i <= 20; i++) inv[i] = power(i, mod - 2); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int x = 0; x < (1 << n); x++) { n     if (x == 0) n       ans = (ans + C(n + m - 1, n - 1)) % mod; n     else { n       long long t = n + m; n       int p = 0; n       for (int i = 0; i < n; i++) n         if ((x >> i) & 1) { n           p++; n           t -= a[i + 1]; n         } n       t -= (p + 1); n       if (p & 1) n         ans = (ans - C(t, n - 1)) % mod; n       else n         ans = (ans + C(t, n - 1)) % mod; n     } n   } n   cout << (ans + mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n string print_iterable(T1 begin_iter, T2 end_iter, int counter) { n   bool done_something = false; n   stringstream res; n   res <<  [ ; n   for (; begin_iter != end_iter and counter; ++begin_iter) { n     done_something = true; n     counter--; n     res << *begin_iter <<  ,  ; n   } n   string str = res.str(); n   if (done_something) { n     str.pop_back(); n     str.pop_back(); n   } n   str +=  ] ; n   return str; n } n vector<int> SortIndex(int size, std::function<bool(int, int)> compare) { n   vector<int> ord(size); n   for (int i = 0; i < size; i++) ord[i] = i; n   sort(ord.begin(), ord.end(), compare); n   return ord; n } n template <typename T> n bool MinPlace(T& a, const T& b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n template <typename T> n bool MaxPlace(T& a, const T& b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n const int MAXN = 100100; n int a[MAXN]; n int pr[MAXN]; n int last_apparizione[MAXN]; n int N; n int res[MAXN]; n int EasyCalc(int k) { n   if (res[k]) return res[k]; n   int ris = 1; n   int curr_start = 0; n   int curr_size = 1; n   for (int i = 1; i < N; i++) { n     curr_size += curr_start > pr[i]; n     if (curr_size > k) { n       curr_size = 1; n       curr_start = i; n       ris++; n     } n   } n   return ris; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> N; n   for (int i = 0; i < N; i++) cin >> a[i]; n   for (int i = 0; i < MAXN; i++) last_apparizione[i] = -1; n   for (int i = 0; i < N; i++) { n     pr[i] = last_apparizione[a[i]]; n     last_apparizione[a[i]] = i; n   } n   int rad = min((int)(2 * sqrt(N)), N); n   for (int k = 1; k <= rad; k++) res[k] = EasyCalc(k); n   int curr = rad; n   while (curr <= N) { n     int l = curr + 1; n     int r = N + 1; n     while (l < r) { n       int m = (l + r) / 2; n       res[m] = EasyCalc(m); n       if (res[m] == res[curr]) n         l = m + 1; n       else n         r = m; n     } n     int prox = l; n     for (int i = curr + 1; i < prox; i++) res[i] = res[curr]; n     curr = prox; n   } n   for (int k = 1; k <= N; k++) cout << res[k] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ans = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x, y, z; n     cin >> x >> y >> z; n     ans += (x + y + z >= 2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[1010]; n int pri[510][510]; n int pre[510][510]; n int f[510][510]; n bool v[510] = {0}; n int N; n void init() { n   gets(str); n   scanf( %d , &N); n } n void prework() { n   int len = strlen(str); n   for (int i = 0; i < len; ++i) n     for (int j = 0; j < len; ++j) { n       int p = 0; n       int tmp = 0; n       while (i + p < j - p) { n         if (str[i + p] != str[j - p]) tmp++; n         p++; n       } n       pri[i + 1][j + 1] = tmp; n     } n } n void dp() { n   int len = strlen(str); n   memset(f, 10, sizeof(f)); n   f[0][0] = 0; n   for (int i = 1; i <= len; ++i) { n     for (int j = 1; j <= N; ++j) n       for (int k = 0; k < i; ++k) { n         if (f[i][j] > f[k][j - 1] + pri[k + 1][i]) { n           f[i][j] = f[k][j - 1] + pri[k + 1][i]; n           pre[i][j] = k; n         } n       } n   } n   int Ans = 1000000000; n   int p; n   for (int i = 1; i <= N; ++i) n     if (f[len][i] < Ans) { n       Ans = f[len][i]; n       p = i; n     } n   printf( %d n , Ans); n   int q = len; n   while (q != 0) { n     v[pre[q][p]] = 1; n     int x = 0; n     while (pre[q][p] + x < q - 1 - x) { n       str[pre[q][p] + x] = str[q - 1 - x]; n       x++; n     } n     q = pre[q][p]; n     p--; n   } n   v[0] = 0; n   for (int i = 0; i < len; ++i) { n     if (v[i]) putchar( + ); n     putchar(str[i]); n   } n } n int main() { n   init(); n   prework(); n   dp(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target(   n      sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native ) n using namespace std; n template <typename T1, typename T2> n void ckmin(T1 &a, T2 b) { n   if (a > b) a = b; n } n template <typename T1, typename T2> n void ckmax(T1 &a, T2 b) { n   if (a < b) a = b; n } n int read() { n   int x = 0, f = 0; n   char ch = getchar(); n   while (!isdigit(ch)) f |= ch ==  - , ch = getchar(); n   while (isdigit(ch)) x = 10 * x + ch -  0 , ch = getchar(); n   return f ? -x : x; n } n template <typename T> n void print(T x) { n   if (x < 0) putchar( - ), x = -x; n   if (x >= 10) print(x / 10); n   putchar(x % 10 +  0 ); n } n template <typename T> n void print(T x, char let) { n   print(x), putchar(let); n } n const int mod = 998244353; n const int G = 3, Gi = 332748118; n int add(int x, int y) { n   if ((x += y) >= mod) x -= mod; n   return x; n } n int sub(int x, int y) { n   if ((x -= y) < 0) x += mod; n   return x; n } n int qpow(int a, int b = mod - 2) { n   int res = 1; n   while (b > 0) { n     if (b & 1) res = 1ll * res * a % mod; n     a = 1ll * a * a % mod; n     b >>= 1; n   } n   return res; n } n namespace Poly { n vector<int> Rev, W; n int lim, L; n void getR(int len) { n   lim = 1, L = 0; n   while (lim <= len) lim <<= 1, L++; n   Rev.resize(lim), W.resize(lim), W[0] = 1; n   for (int i = 0; i < lim; i++) n     Rev[i] = (Rev[i >> 1] >> 1) | ((i & 1) << L - 1); n } n void wf(vector<int> &a) { n   int n = a.size(); n   for (int i = 0; i < n; i++) a[i] = 1ll * (i + 1) * a[i + 1] % mod; n   a[n - 1] = 0; n } n void jf(vector<int> &a) { n   int n = a.size(); n   for (int i = n - 1; i >= 1; i--) a[i] = 1ll * a[i - 1] * qpow(i) % mod; n   a[0] = 0; n } n void NTT(vector<int> &a, int opt) { n   for (int i = 0; i < lim; i++) n     if (i < Rev[i]) swap(a[i], a[Rev[i]]); n   for (int mid = 1; mid < lim; mid <<= 1) { n     int Wn = qpow(opt == 1 ? G : Gi, (mod - 1) / (mid << 1)); n     for (int k = 1; k < mid; k++) W[k] = 1ll * W[k - 1] * Wn % mod; n     for (int j = 0; j < lim; j += mid << 1) { n       for (int k = 0; k < mid; k++) { n         int x = a[j + k], y = 1ll * W[k] * a[j + k + mid] % mod; n         a[j + k] = add(x, y); n         a[j + k + mid] = sub(x, y); n       } n     } n   } n   if (opt == -1) { n     int linv = qpow(lim); n     for (int i = 0; i < lim; i++) a[i] = 1ll * a[i] * linv % mod; n   } n } n vector<int> operator*(vector<int> a, vector<int> b) { n   int len = a.size() + b.size() - 1; n   getR(len); n   a.resize(lim), b.resize(lim); n   NTT(a, 1), NTT(b, 1); n   for (int i = 0; i < lim; i++) a[i] = 1ll * a[i] * b[i] % mod; n   NTT(a, -1); n   a.resize(len); n   return a; n } n vector<int> Inv(vector<int> a) { n   if ((int(a.size())) == 1) return vector<int>(1, qpow(a[0])); n   int len = a.size(); n   vector<int> ta = a; n   ta.resize((len + 1) / 2); n   vector<int> tb = Inv(ta); n   getR(2 * len), a.resize(lim), tb.resize(lim); n   NTT(a, 1), NTT(tb, 1); n   for (int i = 0; i < lim; i++) n     tb[i] = 1ll * tb[i] * (mod + 2 - 1ll * a[i] * tb[i] % mod) % mod; n   NTT(tb, -1); n   tb.resize(len); n   return tb; n } n vector<int> Ln(vector<int> a) { n   vector<int> ta = a; n   wf(ta); n   int len = a.size(); n   a = ta * Inv(a), jf(a); n   a.resize(len); n   return a; n } n vector<int> Exp(vector<int> a) { n   if ((int(a.size())) == 1) return vector<int>(1, 1); n   int len = a.size(); n   vector<int> ta = a; n   ta.resize((len + 1) / 2); n   vector<int> tb = Exp(ta); n   tb.resize(len); n   vector<int> Lnb = Ln(tb); n   assert(Lnb.size() == len); n   for (int i = 0; i < len; i++) Lnb[i] = (a[i] - Lnb[i] + mod) % mod; n   Lnb[0] = (Lnb[0] + 1) % mod; n   tb = tb * Lnb; n   tb.resize(len); n   return tb; n } n }  // namespace Poly n using namespace Poly; n const int N = 250005; n const int B = 15; n vector<int> adj[N]; n int n; n int son[N]; n void dfs1(int u, int fa) { n   for (auto v : adj[u]) { n     if (v == fa) continue; n     son[u]++; n     dfs1(v, u); n   } n } n int two[N], len; n vector<int> solve(int l, int r) { n   if (l > r) return {1}; n   if (l == r) return {1, mod - two[l]}; n   int mid = l + r >> 1; n   return solve(l, mid) * solve(mid + 1, r); n } n int fac[N], inv[N]; n int fuck[N]; n int main() { n   n = read(); n   inv[1] = 1; n   for (int i = 2; i <= n; i++) n     inv[i] = 1ull * (mod - mod / i) * inv[mod % i] % mod; n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = 1ull * fac[i - 1] * i % mod; n   for (int i = 1; i <= n - 1; i++) { n     int u = read(), v = read(); n     adj[u].push_back(v), adj[v].push_back(u); n   } n   dfs1(1, 0); n   vector<int> ONE(n + 1); n   for (int i = 1; i <= n; i++) { n     if (son[i] > B) { n       two[++len] = son[i]; n     } else { n       fuck[son[i]]++; n     } n   } n   for (int i = 1; i <= B; i++) { n     int coef = 1; n     for (int j = 1; j <= n; j++) { n       coef = 1ull * coef * i % mod; n       ONE[j] = sub(ONE[j], 1ull * fuck[i] * coef % mod * inv[j] % mod); n     } n   } n   ONE = Exp(ONE); n   vector<int> TWO = solve(1, len); n   vector<int> f = ONE * TWO; n   int ans = 0; n   for (int i = 0; i <= n - 1; i++) ans = (ans + 1ll * fac[n - i] * f[i]) % mod; n   print(ans,   n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, arr[200005]; n   string swaps; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   cin >> swaps; n   int m = 0, flag = 0; n   for (int i = 1; i < n; i++) { n     m = max(m, arr[i]); n     if (swaps[i - 1] ==  0  && m > i) { n       flag = 1; n       break; n     } n   } n   (flag) ? cout <<  NO n  : cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   char s; n   int d = 1, j = 1; n   for (int i = 1; i <= n; i++) { n     cin >> s; n     j -= 1; n     if (j == 0) { n       cout << s; n       d += 1; n       j = d; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n const int N = 1e5 + 5; n const double pi = acos(-1); n long long n, m, ans; n char arr[55][55]; n bool inrange(int i, int j) { n   if (i < n && i >= 0 && j < m && j >= 0) return true; n   return false; n } n void solve(int i, int j) { n   int cntr1 = 0, cntr2 = 0, cntr3 = 0; n   if (inrange(i + 1, j) && inrange(i + 1, j + 1) && inrange(i, j + 1)) { n     if (arr[i + 1][j] ==  a  || arr[i + 1][j + 1] ==  a  || n         arr[i][j + 1] ==  a ) n       cntr1++; n     if (arr[i + 1][j] ==  e  || arr[i + 1][j + 1] ==  e  || n         arr[i][j + 1] ==  e ) n       cntr2++; n     if (arr[i + 1][j] ==  c  || arr[i + 1][j + 1] ==  c  || n         arr[i][j + 1] ==  c ) n       cntr3++; n   } n   if (cntr1 == 1 && cntr3 == 1 && cntr2 == 1) ans++; n   cntr1 = 0, cntr2 = 0, cntr3 = 0; n   if (inrange(i + 1, j) && inrange(i + 1, j - 1) && inrange(i, j - 1)) { n     if (arr[i + 1][j] ==  a  || arr[i + 1][j - 1] ==  a  || n         arr[i][j - 1] ==  a ) n       cntr1++; n     if (arr[i + 1][j] ==  e  || arr[i + 1][j - 1] ==  e  || n         arr[i][j - 1] ==  e ) n       cntr2++; n     if (arr[i + 1][j] ==  c  || arr[i + 1][j - 1] ==  c  || n         arr[i][j - 1] ==  c ) n       cntr3++; n   } n   if (cntr1 == 1 && cntr3 == 1 && cntr2 == 1) ans++; n   cntr1 = 0, cntr2 = 0, cntr3 = 0; n   if (inrange(i - 1, j) && inrange(i - 1, j - 1) && inrange(i, j - 1)) { n     if (arr[i - 1][j] ==  a  || arr[i - 1][j - 1] ==  a  || n         arr[i][j - 1] ==  a ) n       cntr1++; n     if (arr[i - 1][j] ==  e  || arr[i - 1][j - 1] ==  e  || n         arr[i][j - 1] ==  e ) n       cntr2++; n     if (arr[i - 1][j] ==  c  || arr[i - 1][j - 1] ==  c  || n         arr[i][j - 1] ==  c ) n       cntr3++; n   } n   if (cntr1 == 1 && cntr3 == 1 && cntr2 == 1) ans++; n   cntr1 = 0, cntr2 = 0, cntr3 = 0; n   if (inrange(i - 1, j) && inrange(i - 1, j + 1) && inrange(i, j + 1)) { n     if (arr[i - 1][j] ==  a  || arr[i - 1][j + 1] ==  a  || n         arr[i][j + 1] ==  a ) n       cntr1++; n     if (arr[i - 1][j] ==  e  || arr[i - 1][j + 1] ==  e  || n         arr[i][j + 1] ==  e ) n       cntr2++; n     if (arr[i - 1][j] ==  c  || arr[i - 1][j + 1] ==  c  || n         arr[i][j + 1] ==  c ) n       cntr3++; n   } n   if (cntr1 == 1 && cntr3 == 1 && cntr2 == 1) ans++; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) cin >> arr[i][j]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (arr[i][j] ==  f ) solve(i, j); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   cin >> n; n   cout << n / 2 + n << endl; n   if (n & 1) { n     for (int i = 1; i <= n; i++) { n       if (!(i & 1)) cout << i <<    ; n     } n     for (int i = 1; i <= n; i++) { n       if (i & 1) cout << i <<    ; n     } n     for (int i = 1; i <= n; i++) { n       if (!(i & 1)) cout << i <<    ; n     } n   } else { n     for (int i = 1; i <= n; i++) { n       if (i & 1) cout << i <<    ; n     } n     for (int i = 1; i <= n; i++) { n       if (!(i & 1)) cout << i <<    ; n     } n     for (int i = 1; i <= n; i++) { n       if (i & 1) cout << i <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5, M = 20; n int n, m; n string s; n int pos[(1 << M) - 1], a[M + 5][M + 5], f[(1 << M) + 5], g[(1 << M) + 5][M + 5]; n int main() { n   memset(a, 0, sizeof(a)); n   memset(f, 0x3f, sizeof(f)); n   memset(g, 0, sizeof(g)); n   cin >> n >> m >> s; n   for (int i = 0; i < n - 1; i++) { n     a[s[i] -  a ][s[i + 1] -  a ]++; n     a[s[i + 1] -  a ][s[i] -  a ]++; n   } n   for (int i = 0; i < m; i++) pos[1 << i] = i; n   for (int i = 1; i < (1 << m); i++) n     for (int j = 0; j < m; j++) { n       int k = i & (-i); n       g[i][j] = g[i ^ k][j] + a[j][pos[k]]; n     } n   f[0] = 0; n   for (int i = 0; i < (1 << m) - 1; i++) { n     long long sum = 0; n     for (int j = 0; j < m; j++) n       if ((i & (1 << j)) == 0) sum += g[i][j]; n     for (int j = 0; j < m; j++) n       if ((i & (1 << j)) == 0) n         f[i | (1 << j)] = min(f[i] + sum, (long long)f[i | (1 << j)]); n   } n   cout << f[(1 << m) - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[31][31][51]; n void Cal() { n   for (int i = 0; i <= 30; i++) n     for (int j = 0; j <= 30; j++) n       for (int k = 0; k <= 50; k++) { n         dp[i][j][k] = (k == 0 || k == i * j) ? 0 : 10000000000000000; n         for (int kk = 0; kk <= k; kk++) { n           for (int ii = 1; ii < i; ii++) n             dp[i][j][k] = n                 min(dp[i][j][k], dp[ii][j][kk] + dp[i - ii][j][k - kk] + j * j); n           for (int jj = 1; jj < j; jj++) n             dp[i][j][k] = n                 min(dp[i][j][k], dp[i][jj][kk] + dp[i][j - jj][k - kk] + i * i); n         } n       } n } n int main() { n   int T; n   scanf( %d , &T); n   Cal(); n   while (T--) { n     int n, m, k; n     scanf( %d%d%d , &n, &m, &k); n     cout << dp[n][m][k] << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n vector<pair<long long, long long> > v[3]; n vector<pair<long long, int> > segment[3]; n struct matrix { n   int arr[3][3]; n   matrix(array<int, 3> a1, array<int, 3> a2) { n     for (int i = 0; i <= 2; i++) { n       for (int j = 0; j <= 2; j++) { n         if (!a1[i] and !a2[j] and abs(i - j) < 2) { n           arr[i][j] = 1; n         } else n           arr[i][j] = 0; n       } n     } n   } n   matrix() { n     for (int i = 0; i <= 2; i++) { n       for (int j = 0; j <= 2; j++) { n         if (i == j) n           arr[i][j] = 1; n         else n           arr[i][j] = 0; n       } n     } n   } n }; n matrix mul(matrix a, matrix b) { n   matrix c; n   for (int i = 0; i <= 2; i++) { n     for (int j = 0; j <= 2; j++) { n       c.arr[i][j] = 0; n       for (int k = 0; k <= 2; k++) { n         c.arr[i][j] = (0ll + c.arr[i][j] + n                        (1ll * a.arr[i][k] * b.arr[k][j]) % 1000000007) % n                       1000000007; n       } n     } n   } n   return c; n } n matrix matexpo(matrix a, long long int steps) { n   if (!steps) { n     return matrix(); n   } n   matrix temp = matexpo(a, steps / 2); n   temp = mul(temp, temp); n   if (steps % 2) { n     temp = mul(temp, a); n   } n   return temp; n } n vector<int> get(vector<int> old_ans, array<int, 3> oldcur, array<int, 3> newcur, n                 long long int steps) { n   matrix alpha(oldcur, newcur); n   matrix temp = matexpo(alpha, steps); n   vector<int> loop_ans(3); n   for (int i = 0; i <= 2; i++) { n     for (int j = 0; j <= 2; j++) { n       loop_ans[j] = (0ll + loop_ans[j] + n                      (1ll * temp.arr[i][j] * old_ans[i]) % 1000000007) % n                     1000000007; n     } n   } n   return loop_ans; n } n void solve() { n   long long int n, m; n   scanf( %lld , &n); n   scanf( %lld , &m); n   vector<int> ans = {0, 1, 0}; n   array<int, 3> cur = {0, 0, 0}; n   for (int i = 1; i <= n; i++) { n     long long int a, l, r; n     scanf( %lld , &a); n     scanf( %lld , &l); n     scanf( %lld , &r); n     v[a - 1].push_back({l, r}); n   } n   for (int i = 0; i <= 2; i++) { n     sort((v[i]).begin(), (v[i]).end()); n     stack<pair<long long, long long> > s1; n     for (int j = 0; j <= (int)v[i].size() - 1; j++) { n       if (s1.empty() or s1.top().second < (v[i][j].first - 1)) { n         s1.push(v[i][j]); n       } else { n         s1.top().second = max(v[i][j].second, s1.top().second); n       } n     } n     v[i].clear(); n     v[i].reserve(s1.size()); n     while (!s1.empty()) { n       v[i].push_back(s1.top()); n       s1.pop(); n     } n     reverse((v[i]).begin(), (v[i]).end()); n   } n   for (int i = 0; i <= 2; i++) { n     for (int j = 0; j <= (int)v[i].size() - 1; j++) { n       segment[i].push_back({v[i][j].first, 1}); n       segment[i].push_back({v[i][j].second + 1, 0}); n     } n     segment[i].push_back({m + 1, 0}); n   } n   int i = 0, j = 0, k = 0; n   long long int pos = 1; n   while (true) { n     long long int next = n         min({segment[0][i].first, segment[1][j].first, segment[2][k].first}); n     long long int steps = next - pos - 1; n     ans = get(ans, cur, cur, steps); n     if (next == (m + 1)) { n       printf( %d n , ans[1]); n       return; n     } n     pos = next - 1; n     array<int, 3> newcur = cur; n     if (next == segment[0][i].first) { n       newcur[0] = segment[0][i].second; n       i++; n     } n     if (next == segment[1][j].first) { n       newcur[1] = segment[1][j].second; n       j++; n     } n     if (next == segment[2][k].first) { n       newcur[2] = segment[2][k].second; n       k++; n     } n     ans = get(ans, cur, newcur, 1); n     cur = newcur; n     pos++; n   } n } n int main() { n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long co[100]; n int main() { n   long long a, l = 0; n   long long ans = 1; n   cin >> a; n   while (a > 1) { n     long long c = 0; n     for (int s = 2; s <= sqrt(a); s++) { n       if (a % s == 0) { n         a /= s, co[l] = s, l++, c = 1; n         break; n       } n     } n     if (c == 0) break; n   } n   if (l == 0) n     cout << 1 << endl << 0; n   else if (l == 1) n     cout << 2; n   else { n     cout << 1 << endl; n     for (int s = 0; s < 2; s++) ans *= co[s]; n     cout << ans; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, d, ans = 0; n   scanf( %d%d , &n, &d); n   int maxi, t, l; n   scanf( %d , &maxi); n   for (int i = 1; i < n; i++) { n     scanf( %d , &t); n     if (t > maxi) { n       maxi = t; n       continue; n     } else { n       l = (int)ceil((maxi + 1 - t) / (double)d); n       ans += l; n       maxi = t + d * l; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5, M = 1e5; n int dfn, cur[M + 1], in[N + 1], pre[N + 1], fin[N + 1], r[N + 1], sz[N + 1]; n vector<int> adj[N + 1]; n int dfs(int u, int a) { n   sz[u] = 1; n   r[u] = a; n   pre[u] = ++dfn; n   for (auto v : adj[u]) sz[u] += dfs(v, a); n   fin[u] = ++dfn; n   return sz[u]; n } n int main() { n   int n, m, k, q; n   scanf( %d%d%d%d , &n, &m, &k, &q); n   for (int i = (0), i_end = (k); i < i_end; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     if (cur[b]) { n       ++in[a]; n       adj[cur[b]].push_back(a); n     } n     cur[b] = a; n   } n   for (int i = (1), i_end = (n + 1); i < i_end; ++i) n     if (!in[i]) dfs(i, i); n   for (int i = (0), i_end = (q); i < i_end; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     y = cur[y]; n     if (r[x] == r[y] && pre[y] > pre[x] && fin[y] < fin[x]) n       printf( %d n , sz[x]); n     else n       puts( 0 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const int INF = 1000 * 1000 * 1000 + 7; n const long long LINF = INF * (long long)INF; n const int MAX = 2 * 1000 * 100 + 47; n struct Item { n   int a, t, ind; n   bool operator<(const Item& a) const { return t < a.t; } n }; n Item A[MAX]; n int n, t; n bool ok(int k) { n   long long time = 0; n   int cnt = 0; n   for (int i = (0); i < (n); i++) { n     int a = A[i].a; n     int tt = A[i].t; n     if (a >= k && time + tt <= t) { n       cnt++; n       time += tt; n     } n   } n   return cnt >= k; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> t; n   for (int i = (0); i < (n); i++) { n     cin >> A[i].a >> A[i].t; n     A[i].ind = i; n   } n   sort(A, A + n); n   int l = 0, r = n + 1; n   while (r - l > 1) { n     int c = (r + l) / 2; n     if (ok(c)) { n       l = c; n     } else { n       r = c; n     } n   } n   int cnt = l; n   cout << cnt <<   n  << cnt <<   n ; n   for (int i = (0); i < (n); i++) { n     if (cnt == 0) break; n     if (A[i].a >= l) { n       cnt--; n       cout << A[i].ind + 1 <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n double a, b, c, d; n int main() { n   scanf( %d%d%d%d , &a, &b, &c, &d); n   printf( %.12lf n , a / b / (1 - (1 - a / b) * (1 - c / d))); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 7; n int n, p[N]; n deque<pair<int, int> > deq; n bool mrk[N]; n vector<int> dor[2]; n int dfs(int v, int d) { n   mrk[v] = 1; n   if (!mrk[p[v]]) return dfs(p[v], d + 1); n   return d; n } n void add(int v, int l) { n   dor[l].clear(); n   int pnt = v; n   do { n     dor[l].push_back(pnt); n     pnt = p[pnt]; n   } while (pnt != v); n } n void remake(int l) { n   int go = dor[l].size() / 2 + 1; n   for (int i = 0; i < dor[l].size(); i++) { n     p[dor[l][i]] = dor[l][(i + go) % dor[l].size()]; n   } n } n void merge() { n   for (int i = 0; i < dor[0].size(); i++) { n     p[dor[0][i]] = dor[1][i]; n     p[dor[1][i]] = dor[0][(1 + i) % dor[0].size()]; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   for (int i = 1; i <= n; i++) { n     if (!mrk[i]) { n       deq.push_back({dfs(i, 1), i}); n     } n   } n   sort(deq.begin(), deq.end()); n   while (deq.size()) { n     if (deq[0].first % 2) { n       add(deq[0].second, 0); n       remake(0); n       deq.pop_front(); n       continue; n     } n     if (deq.size() == 1 || deq[1].first != deq[0].first) { n       cout << -1; n       return 0; n     } n     add(deq[0].second, 0); n     add(deq[1].second, 1); n     merge(); n     deq.pop_front(); n     deq.pop_front(); n   } n   for (int i = 1; i <= n; i++) cout << p[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200001; n int MAX[N]; n vector<int> A[N]; n int main() { n   ios::sync_with_stdio(false); n   set<pair<int, int> > S; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int u, v; n     cin >> u >> v; n     MAX[u] = v; n     A[u].push_back(v); n   } n   for (int i = 1; i <= n; i++) { n     if (MAX[i] > 0) S.insert({MAX[i], i}); n   } n   int q; n   cin >> q; n   for (int i = 0; i < q; i++) { n     int c; n     cin >> c; n     vector<int> temp; n     for (int j = 0; j < c; j++) { n       int u; n       cin >> u; n       temp.push_back(u); n       S.erase({MAX[u], u}); n     } n     if (S.size() > 1) { n       pair<int, int> p = *S.rbegin(), p2; n       S.erase(p); n       temp.push_back(p.second); n       p2 = *S.rbegin(); n       int l_lim = 0, u_lim = A[p.second].size(); n       while (l_lim < u_lim - 1) { n         int cur = (l_lim + u_lim) / 2; n         if (A[p.second][cur] > p2.first) { n           u_lim = cur; n         } else { n           l_lim = cur; n         } n       } n       if (A[p.second][l_lim] > p2.first) { n         cout << p.second <<     << A[p.second][l_lim] <<   n ; n       } else { n         cout << p.second <<     << A[p.second][l_lim + 1] <<   n ; n       } n     } else if (S.size() == 1) { n       pair<int, int> p = *S.rbegin(); n       cout << p.second <<     << A[p.second][0] <<   n ; n     } else { n       cout <<  0 0 n ; n     } n     while (!temp.empty()) { n       if (MAX[temp.back()] > 0) S.insert({MAX[temp.back()], temp.back()}); n       temp.pop_back(); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using i64 = long long; n using u64 = unsigned long long; n using u32 = unsigned; n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int n; n   std::cin >> n; n   std::vector<int> c0(n, 0); n   std::vector<int> c1(n, 0); n   for (int i = 0; i < n; i++) { n     std::cin >> c0[i] >> c1[i]; n   } n   std::vector<int> tmp{0}; n   for (int i = 0; i < n; i++) { n     tmp.emplace_back(c0[i]); n     tmp.emplace_back(c1[i]); n   } n   std::sort(tmp.begin(), tmp.end()); n   tmp.resize(std::unique(tmp.begin(), tmp.end()) - tmp.begin()); n   for (int i = 0; i < n; i++) { n     c0[i] = std::lower_bound(tmp.begin(), tmp.end(), c0[i]) - tmp.begin(); n     c1[i] = std::lower_bound(tmp.begin(), tmp.end(), c1[i]) - tmp.begin(); n   } n   std::vector<int> p(tmp.size(), 0); n   std::iota(p.begin(), p.end(), 0); n   auto find = [&](int x) { n     while (x != p[x]) { n       x = p[x] = p[p[x]]; n     } n     return x; n   }; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int x = c0[i]; n     int y = c1[i]; n     x = find(x); n     y = find(y); n     if (!x && !y) { n       std::cout << -1, exit(0); n     } else { n       if (x == y || !x || !y) { n         ans = std::max({ans, x, y}); n         p[x] = p[y] = 0; n       } else { n         ans = std::max(ans, std::min(x, y)); n         p[std::min(x, y)] = std::max(x, y); n       } n     } n   } n   std::cout << tmp[ans] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct compare { n   bool operator()(const std::string& first, const std::string& second) { n     return first.size() > second.size(); n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   int n, k, i; n   cin >> n >> k; n   string arr[110]; n   for (i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   string password; n   cin >> password; n   long long int tries = 0, equallen = 0; n   int plen = password.length(); n   for (i = 0; i < n; i++) { n     if (arr[i].length() < plen) { n       tries++; n     } else if (arr[i].length() == plen) { n       equallen++; n     } n   } n   long long int ans = 0; n   ans = ((tries) / k) * 5 + tries + 1; n   cout << ans <<    ; n   ans = ((tries + equallen - 1) / k) * 5 + (tries + equallen); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s[10] = { O-|-OOOO ,  O-|O-OOO ,  O-|OO-OO ,  O-|OOO-O ,  O-|OOOO- , n                    -O|-OOOO ,  -O|O-OOO ,  -O|OO-OO ,  -O|OOO-O ,  -O|OOOO- }; n   string num; n   while (cin >> num) { n     int i = num.size(); n     while (i--) cout << s[num[i] -  0 ] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n int main() { n   cin >> n >> k; n   cout << (n + k - n % k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n const int N = 2e5 + 10; n int a[N]; n int main() { n   string str; n   string s; n   while (cin >> s) str += s; n   for (auto& c : str) { n     if (c ==  < ) c =    ; n     if (c ==  > ) c =    ; n   } n   vector<string> vs; n   vector<int> ans; n   stringstream sin(str); n   while (sin >> str) { n     if (str[0] ==  / ) { n       if (str ==  /table ) { n         ans.push_back(a[vs.size() - 1]); n       } else { n         a[vs.size() - 2] += a[vs.size() - 1]; n       } n       vs.pop_back(); n       a[vs.size()] = 0; n     } else { n       if (str ==  td ) n         a[vs.size()] = 1; n       else n         a[vs.size()] = 0; n       vs.push_back(str); n     } n   } n   sort(ans.begin(), ans.end()); n   for (auto& x : ans) printf( %d  , x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n char field[110][110]; n bool check_solution(bool do_print, bool swapped) { n   for (int r = 0; r < N; ++r) { n     int c; n     for (c = 0; c < N; ++c) { n       if (field[r][c] ==  . ) break; n     } n     if (c == N) return false; n     if (do_print) { n       if (swapped) n         printf( %d %d n , c + 1, r + 1); n       else n         printf( %d %d n , r + 1, c + 1); n     } n   } n   return true; n } n int main(void) { n   scanf( %d , &N); n   for (int r = 0; r < N; ++r) scanf( %s , field[r]); n   if (check_solution(false, false)) { n     check_solution(true, false); n   } else { n     for (int r = 0; r < N; ++r) { n       for (int c = r + 1; c < N; ++c) { n         swap(field[r][c], field[c][r]); n       } n     } n     if (check_solution(false, true)) n       check_solution(true, true); n     else n       puts( -1 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct nade { n   int pos, tag, sum, l, r; n } tree[600005 * 2]; n int n; n void puup(int x) { n   if (tree[x * 2].sum > tree[x * 2 + 1].sum) { n     tree[x].pos = tree[x * 2].pos; n     tree[x].sum = tree[x * 2].sum; n   } else { n     tree[x].pos = tree[x * 2 + 1].pos; n     tree[x].sum = tree[x * 2 + 1].sum; n   } n } n void pudo(int x) { n   tree[x * 2].tag += tree[x].tag; n   tree[x * 2].sum += tree[x].tag; n   tree[x * 2 + 1].tag += tree[x].tag; n   tree[x * 2 + 1].sum += tree[x].tag; n   tree[x].tag = 0; n } n void build(int x, int l, int r) { n   tree[x].l = l; n   tree[x].r = r; n   if (l == r) { n     tree[x].pos = l; n     return; n   } n   int mid = (l + r) / 2; n   build(x * 2, l, mid); n   build(x * 2 + 1, mid + 1, r); n   tree[x].pos = tree[x * 2].pos; n } n void add(int l, int r, int val, int x) { n   if (tree[x].l >= l && tree[x].r <= r) { n     tree[x].sum += val; n     tree[x].tag += val; n     return; n   } n   pudo(x); n   if (tree[x * 2].r >= l) add(l, r, val, x * 2); n   if (tree[x * 2 + 1].l <= r) add(l, r, val, x * 2 + 1); n   puup(x); n } n int l[200005], r[200005], v[200005]; n struct node { n   int x, u, d; n   bool b; n } s[200005]; n bool cmp(node a, node b) { n   if (a.x != b.x) return a.x < b.x; n   return a.b < b.b; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d%d , &l[i], &v[i], &r[i]); n     s[i * 2].x = l[i]; n     s[i * 2].u = r[i]; n     s[i * 2].d = v[i]; n     s[i * 2].b = 0; n     s[i * 2 + 1].x = v[i]; n     s[i * 2 + 1].u = r[i]; n     s[i * 2 + 1].d = v[i]; n     s[i * 2 + 1].b = 1; n   } n   build(1, 1, 3e5 + 1); n   sort(s, s + 2 * n, cmp); n   int ans = 0, ansp = 0, ansx = 0; n   for (int i = 0; i < n * 2; i++) { n     add(s[i].d, s[i].u, s[i].b ? -1 : 1, 1); n     if (tree[1].sum > ans) { n       ans = tree[1].sum; n       ansp = tree[1].pos; n       ansx = s[i].x; n     } n   } n   printf( %d n , ans); n   for (int i = 0; i < n; i++) n     if (l[i] <= ansx && ansx <= v[i] && v[i] <= ansp && ansp <= r[i]) n       printf( %d  , i + 1); n } n


#include <bits/stdc++.h> n using namespace std; n using lld = long long int; n int n, m; n int a[200009]; n int b[200009]; n int c[420009]; n int main() { n   int t = 1, tv = 0; n   int i, j, k; n   scanf( %d , &t); n   while (t--) { n     scanf( %d %d , &n, &m); n     b[0] = 0; n     bool nom = true; n     for (i = 0; i < n; i++) { n       scanf( %d , &a[i]); n       if (a[i] >= m) { n         b[i + 1] = b[i] + 1; n       } else { n         b[i + 1] = b[i] - 1; n       } n       if (a[i] == m) { n         nom = false; n       } n     } n     bool valid = false; n     if (n == 1 && a[0] == m) { n       valid = true; n       goto HELL; n     } n     if (!nom && !valid) { n       for (i = 200009 - n - 9; i <= 200009 + n + 9; i++) { n         c[i] = 0; n       } n       for (i = 0; i < n; i++) { n         if (a[i] >= m) { n           if (c[b[i] + 200009] == 1 || c[b[i] + 200009 - 1] == 1) { n             valid = true; n             goto HELL; n           } n         } n         c[b[i] + 200009] = 1; n       } n       for (i = 200009 - n - 9; i <= 200009 + n + 9; i++) { n         c[i] = 0; n       } n       for (i = n - 1; i >= 0; i--) { n         if (a[i] >= m) { n           if (c[b[i + 1] + 200009] == 1 || c[b[i + 1] + 200009 + 1] == 1) { n             valid = true; n             goto HELL; n           } n         } n         c[b[i + 1] + 200009] = 1; n       } n     } n   HELL: n     printf( %s n , valid ?  yes  :  no ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100500; n const int infi = (1 << 30); n const long long infl = (1LL << 62); n const double eps = 1e-9; n const long long mod = 1000000007LL; n const double pi = acos(-1.0); n int n, k; n int a[maxn], c[maxn]; n int main() { n   cin >> n >> k; n   for (int i = 1; i < n; i++) a[i] = 1; n   a[0] = 0; n   for (int i = 0; i < k; i++) { n     for (int j = 0; j < n; j++) { n       int second = int(upper_bound(a, a + n, j - a[j] - 1) - a); n       if (second == n) second = n - 1; n       c[j] = second; n     } n     for (int j = 0; j < n; j++) { n       a[j] += a[c[j]]; n     } n     for (int j = n - 1; j >= 0; --j) cout << n - c[j] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> divs[1000009]; n bool p[1000009]; n vector<int> primes; n void pre() { n   for (int i = 2; i <= (1000000); ++i) { n     if (!p[i]) { n       primes.push_back(i); n       for (int j = i; j <= 1000000; j += i) divs[j].push_back(i), p[j] = true; n     } n   } n } n int L(int x, int p) { n   int s = (int)(divs[p]).size(); n   int tot = (1 << s) - 1; n   long long ans = 0; n   for (int i = 1; i <= (tot); ++i) { n     int cnt = 0; n     int var = 1; n     for (int j = 0; j < (s); ++j) n       if (i & (1 << j)) cnt++, var *= divs[p][j]; n     if (cnt % 2) n       ans += x / var; n     else n       ans -= x / var; n   } n   return x - ans; n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   cin.exceptions(cin.failbit); n   pre(); n   int q; n   cin >> q; n   for (int aa = 0; aa < (q); ++aa) { n     int x, p, k; n     cin >> x >> p >> k; n     if (p == 1) n       cout << k + x <<   n ; n     else { n       k += L(x, p); n       int l = 1, r = 1e8; n       while (l < r) { n         int m = (l + r) >> 1; n         if (L(m, p) >= k) n           r = m; n         else n           l = m + 1; n       } n       cout << l <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Solution { n  public: n   long long count(long long _m, long long _x) { n     if (_m < 3) { n       return max(0LL, _m); n     } n     m = _m; n     x = _x; n     vector<pair<long long, int>> cnts = factorize(m); n     long long res = 0; n     search(0, 1, cnts, res); n     return res; n   } n  n  private: n   map<long long, vector<pair<long long, int>>> fs; n   long long m, x; n   long long gcd(long long x, long long y) { n     while (x != 0) { n       y %= x; n       swap(x, y); n     } n     return y; n   } n   vector<pair<long long, int>>& factorize(long long n) { n     if (fs.count(n) > 0) { n       return fs[n]; n     } n     vector<pair<long long, int>>& res = fs[n]; n     for (long long i = 2; i * i <= n; ++i) { n       int cnt = 0; n       while (n % i == 0) { n         ++cnt; n         n /= i; n       } n       if (cnt > 0) { n         res.emplace_back(i, cnt); n       } n     } n     if (n > 1) { n       res.emplace_back(n, 1); n     } n     return res; n   } n   void search(int i, long long dv, vector<pair<long long, int>>& cnts, n               long long& ans) { n     if (i >= cnts.size()) { n       vector<pair<long long, int>> dvcnts; n       dvcnts.reserve(cnts.size()); n       for (auto& pc : cnts) { n         if (pc.second <= 1) { n           continue; n         } n         dvcnts.emplace_back(pc.first, pc.second - 1); n       } n       long long phi = dv; n       for (auto& pc : cnts) { n         long long p = pc.first; n         int c = pc.second; n         if (c == 0) { n           continue; n         } n         dvcnts = merge(dvcnts, factorize(p - 1)); n         phi = phi - phi / p; n       } n       ans += phi / calcPeriod(x % dv, dv, dvcnts); n       return; n     } n     long long p = fs[m][i].first; n     long long n = fs[m][i].second; n     for (int j = 0; j <= n; ++j) { n       cnts[i].second = j; n       search(i + 1, dv, cnts, ans); n       dv *= p; n     } n   } n   vector<pair<long long, int>> merge(vector<pair<long long, int>>& list1, n                                      vector<pair<long long, int>>& list2) { n     vector<pair<long long, int>> res; n     res.reserve(list1.size() + list2.size()); n     int n = list1.size(), m = list2.size(); n     int i = 0, j = 0; n     while (i < n && j < m) { n       if (list1[i].first < list2[j].first) { n         res.emplace_back(list1[i].first, list1[i].second); n         ++i; n       } else if (list1[i].first > list2[j].first) { n         res.emplace_back(list2[j].first, list2[j].second); n         ++j; n       } else { n         res.emplace_back(list1[i].first, list1[i].second + list2[j].second); n         ++i; n         ++j; n       } n     } n     while (i < n) { n       res.emplace_back(list1[i].first, list1[i].second); n       ++i; n     } n     while (j < m) { n       res.emplace_back(list2[j].first, list2[j].second); n       ++j; n     } n     return res; n   } n   long long calcPeriod(long long x, long long mod, n                        vector<pair<long long, int>>& factors) { n     long long phi = 1; n     for (auto& pc : factors) { n       long long p = pc.first; n       int c = pc.second; n       for (int i = 0; i < c; ++i) { n         phi *= p; n       } n     } n     long long res = 1; n     for (auto& pc : factors) { n       long long p = pc.first; n       int c = pc.second; n       long long base = phi; n       for (int i = 0; i < c; ++i) { n         base /= p; n       } n       long long val = powM(x, base, mod); n       int j = c; n       for (int i = 1; i <= c; ++i) { n         if (val == 1) { n           j = i - 1; n           break; n         } n         val = powM(val, p, mod); n       } n       res *= powI(p, j); n     } n     return res; n   } n   long long mulM(long long a, long long b, long long m) { n     long long res = 0; n     while (b > 0) { n       if (b & 0x1) { n         res = (res + a) % m; n       } n       a = (a + a) % m; n       b >>= 1; n     } n     return res; n   } n   long long powM(long long x, long long e, long long m) { n     long long res = 1; n     while (e > 0) { n       if (e & 0x1) { n         res = mulM(res, x, m); n       } n       x = mulM(x, x, m); n       e >>= 1; n     } n     return res; n   } n   long long powI(long long x, long long e) { n     long long res = 1; n     for (int i = 0; i < e; ++i) { n       res *= x; n     } n     return res; n   } n }; n int main(int argc, char** argv) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long m, x; n   cin >> m >> x; n   Solution sol; n   cout << sol.count(m, x) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n ll dx[] = {0, 0, 1, -1}; n ll dy[] = {1, -1, 0, 0}; n void solve() { n   ll n; n   cin >> n; n   vector<ll> v(n); n   for (ll i = 0; i < (n); ++i) cin >> v[i]; n   vector<pair<ll, ll>> aux(n); n   ll i = n - 1; n   stack<ll> s; n   while (i >= 0) { n     while (!s.empty() && s.top() < v[i]) s.pop(); n     if (s.empty()) n       aux[i].first = -1; n     else n       aux[i].first = s.top(); n     s.push(v[i]); n     i--; n   } n   i = 0; n   while (!s.empty()) s.pop(); n   while (i < n) { n     while (!s.empty() && s.top() < v[i]) s.pop(); n     if (s.empty()) n       aux[i].second = -1; n     else n       aux[i].second = s.top(); n     s.push(v[i]), i++; n   } n   ll res = 0; n   for (ll i = 0; i < (n); ++i) { n     if (aux[i].first != -1) res = max(res, aux[i].first ^ v[i]); n     if (aux[i].second != -1) res = max(res, aux[i].second ^ v[i]); n   } n   cout << res <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   ll t = 1; n   while (t--) solve(); n   return 0; n } n