#include <bits/stdc++.h> n using namespace std; n inline long long gcd(long long x, long long y) { return x ? gcd(y % x, x) : y; } n const long long mod = 1e9 + 7; n inline signed rd() { n   signed ans = 0; n   char last =    , ch = getchar(); n   while (!(ch >=  0  && ch <=  9 )) last = ch, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) ans = ans * 10 + ch -  0 , ch = getchar(); n   if (last ==  - ) ans = -ans; n   return ans; n } n inline long long QPow(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return ans; n } n inline long long QPow(long long a, long long b, const long long &mod) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return ans; n } n long long exgcd(long long a, long long b, long long &x, long long &y) { n   long long d = a; n   if (b == 0) n     x = 1, y = 0; n   else { n     d = exgcd(b, a % b, y, x), y -= a / b * x; n   } n   return d; n } n const long double PI = 3.141592653589793115997963468544185161590576171875; n const long long maxn = 100005; n struct Req { n   long long s, a, b; n   bool friend operator<(const Req &a, const Req &b) { n     return (a.b - a.a) > (b.b - b.a); n   } n } reqs[maxn]; n long long arr[maxn]; n long long rarr[maxn]; n long long cnt[maxn]; n signed main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long t, m, n, R, s; n   long long k; n   cin >> n >> s; n   for (long long i = 1; i <= n; i++) cin >> reqs[i].s >> reqs[i].a >> reqs[i].b; n   long long ans = 0; n   long long sum = 0; n   for (long long i = 1; i <= n; i++) n     ans += reqs[i].a * reqs[i].s, sum += reqs[i].s; n   sort(reqs + 1, reqs + 1 + n); n   for (long long i = 1; i <= n; i++) n     arr[i] = reqs[i].b - reqs[i].a, cnt[i] = reqs[i].s; n   for (long long i = 1; i <= n; i++) rarr[i] = arr[n - i + 1]; n   signed id = 1; n   while (arr[id] > 0) id++; n   id--; n   long long tot = 0; n   long long tmp = ans; n   for (long long i = 1; i <= id; i++) { n     tot += cnt[i]; n     tmp += arr[i] * cnt[i]; n   } n   long long num = (tot + s - 1) / s; n   num *= s; n   long long tmp3 = tmp; n   long long re = sum % s; n   num -= tot; n   num -= (s - re) % s; n   for (long long i = id + 1; i <= n; i++) { n     if (num <= 0) break; n     long long tmp1 = min(num, cnt[i]); n     num -= tmp1; n     tmp3 += tmp1 * arr[i]; n     if (num <= 0) break; n   } n   num = (tot + s - 1) / s; n   num *= s; n   num -= s; n   long long tmp4 = ans; n   for (long long i = 1; i <= n; i++) { n     if (num <= 0) break; n     long long tmp2 = min(cnt[i], num); n     num -= tmp2; n     tmp4 += tmp2 * arr[i]; n     if (num == 0) break; n   } n   ans = max(ans, max(tmp4, tmp3)); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, a, b, ii; n int ans; n char ch[1500][1500]; n int dx[4] = {0, 0, 1, -1}; n int dy[4] = {1, -1, 0, 0}; n int dp[1500][1500]; n int p[100007]; n void dfs(int x, int y) { n   if (x > n || x < 1 || y > m || y < 1) return; n   if (ch[x][y] ==  * ) { n     ans++; n     return; n   } n   if (dp[x][y] != 0) return; n   dp[x][y] = ii; n   for (int i = 0; i < 4; i++) { n     int temp_x = x + dx[i]; n     int temp_y = y + dy[i]; n     dfs(temp_x, temp_y); n   } n   return; n } n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> ch[i][j]; n     } n   } n   for (ii = 1; ii <= k; ii++) { n     cin >> a >> b; n     ans = 0; n     if (!dp[a][b]) n       dfs(a, b); n     else n       ans = p[dp[a][b]]; n     p[ii] = ans; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, a, b; n   cin >> n >> m; n   double mn = 101.00, x = 0.00; n   for (int i = 1; i <= n; i++) { n     cin >> a >> b; n     x = a / (b * 1.00); n     if (mn > x) mn = x; n   } n   cout << fixed << setprecision(8); n   cout << mn * m << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long int myRand(long long int B) { return (unsigned long long)rng() % B; } n inline double time() { n   return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>( n                                  chrono::steady_clock::now().time_since_epoch()) n                                  .count()) * n          1e-9; n } n int main() { n   cin.tie(nullptr); n   ios::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   vector<string> s(n); n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   vector<int> sum(m + 1); n   for (int i = 0; i + 1 < m; i++) { n     int u = 0; n     for (int j = 1; j < n; j++) { n       if (s[j][i] ==  X  and s[j - 1][i + 1] ==  X ) u = 1; n     } n     sum[i + 1] = sum[i] + u; n   } n   int q; n   cin >> q; n   while (q--) { n     int x, y; n     cin >> x >> y; n     if (x == y) { n       printf( YES n ); n       continue; n     } n     if (sum[y - 1] - sum[x - 1] > 0) { n       printf( NO n ); n       continue; n     } else { n       printf( YES n ); n       continue; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n, S, s, up, down, mid, mx, mn, a, b, k, l, r, ans, raod; n pair<long long, long long> p[300005]; n int main() { n   cin >> t; n   while (t--) { n     cin >> n >> S; n     mn = 1e10; n     mx = 0; n     for (k = 1; k <= n; k++) { n       cin >> a >> b; n       p[k].first = a; n       mn = min(mn, a); n       p[k].second = b; n       mx = max(mx, b); n     } n     ans = 0; n     sort(p + 1, p + n + 1); n     l = mn; n     r = mx; n     while (l <= r) { n       long long mid = (l + r + 1) / 2; n       up = 0; n       down = 0; n       s = 0; n       for (k = 1; k <= n; k++) n         if (p[k].first > mid) n           up++; n         else if (p[k].second < mid) n           down++; n       if (up >= n / 2 + 1) { n         l = mid + 1; n       } else if (down >= n / 2 + 1) n         r = mid - 1; n       else { n         raod = 0; n         for (k = n; k >= 1; k--) { n           if (raod <= n / 2 & p[k].second >= mid) { n             raod++; n             s += max(mid, p[k].first); n           } else n             s += p[k].first; n         } n         if (s <= S) { n           ans = mid; n           l = mid + 1; n         } else n           r = mid - 1; n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int const maxn = 100 * 1000 + 5; n int a[maxn]; n int b[maxn]; n int main() { n   int t; n   cin >> t; n   int n, f1, fm1; n   for (int i = 0; i < t; i++) { n     f1 = maxn; n     fm1 = maxn; n     cin >> n; n     for (int j = 0; j < n; j++) { n       cin >> a[j]; n       if (a[j] == 1 && f1 == maxn) f1 = j; n       if (a[j] == -1 && fm1 == maxn) fm1 = j; n     } n     bool check = true; n     for (int j = 0; j < n; j++) { n       cin >> b[j]; n     } n     for (int j = n - 1; j >= 0; j--) { n       if (j > 0) { n         if (a[j] < b[j] && f1 >= j) check = false; n         if (a[j] > b[j] && fm1 >= j) check = false; n       } else { n         if (a[j] != b[j]) check = false; n       } n       if (!check) break; n     } n     if (check) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long k, i, j, b = 1; n   string s =  codeforces ; n   vector<int> a(10, 1); n   cin >> k; n   for (; b < k;) n     for (i = 0; i < 10; i++) { n       b /= a[i]; n       ++a[i]; n       b = b * a[i]; n       if (b > k - 1) break; n     } n   for (i = 0; i < 10; i++) n     for (j = 0; j < a[i]; j++) cout << s[i]; n   cout <<   n ; n   return; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n int dp[502][502][2], n, m; n char str[502][502]; n int dis; n int check(int x, int y) { n   if (x < 0 or x >= n and y < 0 or y >= m) n     return 0; n   else n     return 1; n } n int call(int x, int y, int a, int idx) { n   int ham = x + y; n   int paisi = n - 1 - a; n   int baki = ham - paisi; n   int b = m - 1 - baki; n   if (check(x, y) == 0 or check(a, b) == 0) return dp[x][y][a & 1] = 0; n   if (str[x][y] != str[a][b]) return dp[x][y][a & 1] = 0; n   if (idx == 0) return dp[x][y][a & 1]; n   int dx = ((x - a) < 0 ? -(x - a) : (x - a)); n   int dy = ((y - b) < 0 ? -(y - b) : (y - b)); n   if (dis % 2 == 0) { n     if (ham + 1 > dis / 2) return dp[x][y][a & 1] = 0; n     if (ham + 1 == dis / 2) return dp[x][y][a & 1] = (dx + dy <= 1); n   } else { n     if (ham > dis / 2) return dp[x][y][a & 1] = 0; n     if (ham == dis / 2) return dp[x][y][a & 1] = (x == a and y == b); n   } n   long long ret = 0; n   ret += call(x + 1, y, a, 1 - idx); n   ret += call(x + 1, y, a - 1, 1 - idx); n   ret += call(x, y + 1, a, 1 - idx); n   ret += call(x, y + 1, a - 1, 1 - idx); n   return dp[x][y][a & 1] = ret % MOD; n } n int main() { n   scanf( %d %d , &n, &m); n   dis = n + m - 1; n   for (int i = 0; i < n; i++) scanf( %s , str[i]); n   for (int a = 0; a < n; a++) { n     for (int y = m - 1; y >= 0; y--) { n       int tempx = n - 1; n       int tempy = y; n       while (tempx >= 0 and tempy < m) { n         call(tempx, tempy, a, 1); n         tempy++; n         tempx--; n       } n     } n     for (int x = n - 2; x >= 0; x--) { n       int tempx = x; n       int tempy = 0; n       while (tempx >= 0 and tempy < m) { n         call(tempx, tempy, a, 1); n         tempy++; n         tempx--; n       } n     } n   } n   printf( %d n , dp[0][0][(n - 1) & 1]); n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = (long long)1e18; n const long long mod = (long long)1e9 + 7; n const double eps = (double)1e-9; n const double pi = acos(-1.0); n const int dx[] = {0, 0, 1, 0, -1}; n const int dy[] = {0, 1, 0, -1, 0}; n const int N = 500500; n int n, k; n vector<pair<int, int> > g[N]; n long long dp[N], W[N]; n void dfs(int v, int pr = -1) { n   dp[v] = 0; n   vector<long long> u; n   for (auto to : g[v]) { n     if (to.first == pr) continue; n     dfs(to.first, v); n     dp[v] += dp[to.first] + W[to.first]; n     if (-W[to.first] + to.second > 0) u.push_back(-W[to.first] + to.second); n   } n   sort(u.begin(), u.end()); n   reverse(u.begin(), u.end()); n   for (int i = 0; i < min(k - 1, (int)u.size()); ++i) dp[v] += u[i]; n   if (k <= (int)u.size()) n     W[v] = u[k - 1]; n   else n     W[v] = 0; n } n void solve() { n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) g[i].clear(); n   for (int i = 1; i < n; ++i) { n     int x, y, w; n     cin >> x >> y >> w; n     g[x].push_back(make_pair(y, w)); n     g[y].push_back(make_pair(x, w)); n   } n   dfs(1); n   cout << dp[1] + W[1] << endl; n } n int main() { n   cin.tie(NULL); n   cout.tie(NULL); n   ios_base::sync_with_stdio(false); n   int T; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   int count = 0; n   for (int a = 1; a <= n; a++) { n     for (int b = a; b <= n; b++) { n       int k = a * a + b * b; n       int c = sqrt(k); n       if (c * c == k && c <= n) count++; n     } n   } n   cout << count; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int N = 500031; n int tests, n, k; n vector<pair<int, int> > g[N]; n int used[N]; n long long dp[N][2]; n void dfs(int v) { n   used[v] = 1; n   long long guaranteed = 0; n   vector<long long> benefits; n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i].first; n     int cost = g[v][i].second; n     if (used[to] == 1) continue; n     dfs(to); n     long long without_edge = max(dp[to][0], dp[to][1]); n     long long with_edge = dp[to][0] + cost; n     guaranteed += without_edge; n     long long benefit_here = with_edge - without_edge; n     if (benefit_here < 0) benefit_here = 0; n     benefits.push_back(benefit_here); n   } n   sort(benefits.begin(), benefits.end()); n   reverse(benefits.begin(), benefits.end()); n   long long S = 0; n   for (int i = 0; i < k - 1 && i < benefits.size(); i++) { n     S += benefits[i]; n   } n   dp[v][0] = guaranteed + S; n   S = 0; n   for (int i = 0; i < k && i < benefits.size(); i++) { n     S += benefits[i]; n   } n   dp[v][1] = guaranteed + S; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> tests; n   for (; tests; --tests) { n     cin >> n >> k; n     for (int i = 1; i <= n; i++) { n       g[i].clear(); n       used[i] = 0; n     } n     for (int i = 1; i < n; i++) { n       int a, b, c; n       cin >> a >> b >> c; n       g[a].push_back(make_pair(b, c)); n       g[b].push_back(make_pair(a, c)); n     } n     dfs(1); n     cout << max(dp[1][0], dp[1][1]) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n int map[525][525]; n int note[10] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512}; n int main() { n   int time = 0, size = 1, i, j, head, tail; n   map[1][1] = 1; n   while (time != 9) { n     time++; n     head = size + 1; n     tail = 2 * size; n     for (i = 1; i <= size; i++) n       for (j = head; j <= tail; j++) map[i][j] = map[i][j - size]; n     for (i = head; i <= tail; i++) n       for (j = 1; j <= size; j++) map[i][j] = map[i - size][j]; n     for (i = head; i <= tail; i++) n       for (j = head; j <= tail; j++) map[i][j] = -map[i - size][j - size]; n     size *= 2; n   } n   int k; n   scanf( %d , &k); n   for (i = 1; i <= note[k]; i++) { n     for (j = 1; j <= note[k]; j++) n       if (map[i][j] == 1) n         printf( + ); n       else n         printf( * ); n     puts(  ); 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   int tichnas; n   cin >> tichnas; n   while (tichnas--) { n     long long n, c, m, maxs = 1e9, temp, ans; n     map<long long, long long> type; n     bool possible; n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> c; n       type[c]++; n     } n     for (auto it = type.begin(); it != type.end(); it++) n       maxs = min(maxs, it->second + 1); n     ans = n; n     for (int i = 2; i <= maxs; i++) { n       possible = true; n       m = 0; n       for (auto it = type.begin(); it != type.end() && possible; it++) { n         temp = it->second; n         if (temp % i == 0) n           m += temp / i; n         else if (i - 1 - temp % i <= temp / i) n           m += temp / i + 1; n         else n           possible = false; n       } n       if (possible) ans = m; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


n #include<bits/stdc++.h> n using namespace std; n #define ll long long n #define imin INT_MIN n #define imax INT_MAX n #define br   n  n #define sp     n #define pie 3.141592653589793238462643383279 n #define lower(sl) transform(sl.begin(), sl.end(), sl.begin(), ::tolower); n #define upper(sl) transform(sl.begin(), sl.end(), sl.begin(), ::toupper); n #define fo(i, n) for(int (i)=0;(i)<n;(i)++) n #define rfo(i, n) for(int (i)=n-1;(i)>=0;(i)--) n #define forl(i,a,n) for(int (i)=a;(i)<n;(i)++) n #define forr(i,a,n) for(int (i)=n-1;(i)>=a;(i)--) n #define tr(it, x) for(auto it = x.begin(); it != x.end(); it++) n #define trr(it, x) for(auto it = x.rbegin(); it != x.rend(); it++) n #define popcount(x) __builtin_popcount(x)  // count the number of one’s(set bits) in an integer. n #define popcountll(x) __builtin_popcountll(x) n #define clz(x) __builtin_clz(x)   // count the leading zeros(binary representation) of the integer n #define clzll(x) __builtin_clzll(x) n #define ctz(x) __builtin_ctz(x)   // count the trailing zeros(binary representation) of the integer n #define ctzll(x) __builtin_ctzll(x) n #define pb push_back n #define mp make_pair n #define F first n #define S second n #define gcd __gcd n #define lcm(a,b) ((a)*(b))/gcd(a,b) n #define deci(n) fixed << setprecision(n) n #define all(x) x.begin(),x.end() n #define max3(a,b,c) max(max(a,b),c) n #define min3(a,b,c) min(min(a,b),c) n #define max4(a,b,c,d) max(a,max3(b,c,d)) n #define min4(a,b,c,d) min(a,min3(b,c,d)) n #define max5(a,b,c,d,e) max(max4(a,b,c,d),e) n #define min5(a,b,c,d,e) min(min4(a,b,c,d),e) n typedef pair<int, int> pii; n typedef pair<ll, ll> pl; n typedef pair<double, double> pd; n typedef vector<int> vi; n typedef vector<ll> vl; n typedef vector<pii> vpii; n typedef vector<pl> vpl; n typedef vector<vi> vvi; n typedef vector<vl> vvl; n typedef priority_queue<int, vector<int>, greater<int>> minpq; n typedef priority_queue<int> pq; n typedef unordered_map<int,int> umii; n typedef map<int,int> mii; n typedef set<int> si; n typedef deque<int> di; n typedef queue<int> qi; n const int max_n = 1e6 + 1; n const int mod = 1e9 + 7; n const int inf = 1e9; n const ll linf = 1e18; n const int wmod = 998244353; n  n int midofthree(int a,int b,int c){ n     if((a<=b and a>=c) or (a<=c and a>=b)) n         return a; n     else if((b>=a and b<=c) or (b<=a and b>=c)) n         return b; n     return c; n } n  n bool isPowerTwo(int x){  n  return (x && !(x & (x-1)));  n } n  n bool ispalindrome(string s){ n     ll len=s.length();  n     fo(i, len){  n         if(s[i]!=s[len-i-1]) n         return false;  n     }  n     return true;  n }  n  n bool isSubstring(string s1, string s2){ n     if(s1.find(s2) != string::npos) return true; n         return false; n } n  n int sum_of_digits(int x){ n     int sum = 0; n     while (x > 0) n     { n         sum += x % 10; n         x /= 10; n     } n     return sum; n } n  n int num_of_digits(int x){ n     return floor(log10(x) + 1); n } n  n     int binaryToDecimal(int n){ n     int num = n; n     int dec_value = 0; n     int base = 1; n     int temp = num; n     while (temp){ n         int last_digit = temp % 10; n         temp = temp / 10; n         dec_value += last_digit * base; n         base = base * 2; n     } n     return dec_value; n } n  n int isvowel(char x){ n     if (x ==  a  || x ==  e  || x ==  i  || x ==  o  || x ==  u ) n         return 1; n     return 0; n } n  n int isconsonant(char x){ n     if (x !=  a  && x !=  e  && x !=  i  && x !=  o  && x !=  u ) n         return 1; n     return 0; n } n  n // Check if n is a prime in O(sqrt(n/6)) n bool isPrime(int n){ n  if(n<=1)return 0; n  if(n<=3)return 1; n  if(n%2==0 or n%3==0)return 0; n  for(int i=5;i*i<=n;i+=6){ n   if(n%i==0 or n%(i+2)==0)return 0; n  } n  return 1; n } n  n // Generate the all primes <= n in O(nlog(logn)) n vi sieve(int n){ n     vi primes, isprime(n + 1, 1); n     for (int i = 2; i * i <= n; i++) n         if (isprime[i]) n             for (int j = i + i; j <= n; j += i) n                 isprime[j] = 0; n     for (int i = 2; i <= n; i++) n         if (isprime[i]) n             primes.push_back(i); n     return primes; n } n  n vi factorize(int n){ n     vi res; n     for (int i = 2; i * i <= n; ++i){ n         while (n % i == 0){ n             res.push_back(i); n             n /= i; n         } n     } n     if (n != 1){ n         res.push_back(n); n     } n     return res; n } n  n // Iteratively finding (x^n)(mod M) (slightly faster) n int modularExponentiation(int x,int n,int M){ n     int result=1; n     while(n>0){ n         if(n % 2 ==1){ n             result=(result * x)%M;} n         x=(x*x)%M; n         n = n>>1; n     } n     return result; n } n  n //  calculationg exponential in logn  n int binaryExponentiation(int x,int n){ n     int result=1; n     while(n>0){ n         if(n % 2 ==1){ n             result=result * x;} n         x=x*x; n         n=n/2; n     } n     return result; n } n  n int modInverse(int a, int m){ n     int m0 = m,y = 0, x = 1;  n     if (m == 1)  n         return 0;  n     while (a > 1) {  n         int q = a / m;  n         int t = m;  n         m = a % m, a = t;  n         t = y;  n         y = x - q * y;  n         x = t;} n     if (x < 0)  n         x += m0;  n     return x;  n }  n  n ll factorial(int n){ n     if(n==1 || n==0){ n         return 1; n     } n     else{ n         return n*factorial(n-1); n     } n } n  n #define MAX 1000 n // Function to find the factorial of large number  n // and return them in string format  n string longfactorial(int n){ n     long long counter;  n     long double sum = 0;  n     if (n == 0)  n         return  1 ;  n     for (counter = 1; counter <= n;  n         counter++) {  n         sum = sum + log(counter);  n     }  n     string result = to_string(round(exp(sum)));  n     return result;  n } n  n ll NcR(int n,int m){ n     ll z=1; n     for(int i=0;i<m;i++) n         z=z*(n-i)/(i+1); n     return z; n } n  n void fillPrefixSum(int arr[], int n, int prefixSum[]){  n     prefixSum[0] = arr[0]; n     for (int i = 1; i < n; i++)  n         prefixSum[i] = prefixSum[i - 1] + arr[i];  n } n  n /* ========== YOUR CODE HERE ========= */ n void solution(){   n     // 0xAAAAAAAA=even bits set to 1 ; 0x55555555 = odd bits set to 1 n     // int prefixSuma[n]; n     // fillPrefixSum(a, n, prefixSuma); n  n     // int n;   n     // cin>>n; n     // int a[n]; n     // fo(i,n){ n     //     cin>>a[i]; n     // } n     int w,h,n; n     cin>>w>>h>>n; n     if(n==1){ n         cout<< YES <<br; n     } n     else{ n         int c=1,a=0; n         int t1=1, t2=1; n         while(w%2==0){ n             c = 2*c; n             w/=2; n         } n         while(h%2==0){ n             c*=2; n             h/=2; n         } n         if(c>=n){ n             cout<< YES <<br; n         } n         else{ n             cout<< NO <<br; n         } n     } n  n } n /* ========== YOUR CODE HERE ========= */ n  n int main(){ n     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n     srand(chrono::high_resolution_clock::now().time_since_epoch().count()); n  n     int testcase = 1; n     cin>>testcase; n     while(testcase--) { n         solution(); n     } n     return 0; n } n // By Ashwani Kumar, Indian Institute Of Technology(IIT), Guwahati.


#include <bits/stdc++.h> n using namespace std; n int pgcd(int a, int b) { n   if (a == 0) return b; n   return pgcd(b % a, a); n } n int nbNombres; n int vals[300 * 1000]; n int minRange[20][300 * 1000]; n int pgcdRange[20][300 * 1000]; n vector<int> possibles(int taille) { n   vector<int> sol; n   int etage = __builtin_log2(taille); n   int pseudo = (1 << etage); n   for (int iDebut = 0; iDebut + taille <= nbNombres; iDebut++) { n     int mini = n         min(minRange[etage][iDebut], minRange[etage][iDebut + taille - pseudo]); n     int pg = pgcd(pgcdRange[etage][iDebut], n                   pgcdRange[etage][iDebut + taille - pseudo]); n     if (mini == pg) sol.push_back(iDebut); n   } n   return sol; n } n int main() { n   cin >> nbNombres; n   for (int iNombre = 0; iNombre < nbNombres; iNombre++) { n     cin >> vals[iNombre]; n     minRange[0][iNombre] = vals[iNombre]; n     pgcdRange[0][iNombre] = vals[iNombre]; n   } n   for (int iPuis = 1; iPuis < 20; iPuis++) { n     int curPuis = (1 << iPuis), derPuis = (1 << (iPuis - 1)); n     for (int iPos = 0; iPos + curPuis <= nbNombres; iPos++) { n       minRange[iPuis][iPos] = n           min(minRange[iPuis - 1][iPos], minRange[iPuis - 1][iPos + derPuis]); n       pgcdRange[iPuis][iPos] = pgcd(pgcdRange[iPuis - 1][iPos], n                                     pgcdRange[iPuis - 1][iPos + derPuis]); n     } n   } n   int debut = 0, fin = nbNombres + 1; n   while (debut + 1 < fin) { n     int mil = (debut + fin) / 2; n     if (!possibles(mil).empty()) { n       debut = mil; n     } else { n       fin = mil; n     } n   } n   vector<int> sol = possibles(debut); n   cout << sol.size() <<     << debut - 1 << endl; n   for (int val : sol) { n     cout << val + 1 <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s, e; n int x[5013]; n int a[5013], b[5013], c[5013], d[5013]; n long long dp[5013][5013]; n int main() { n   scanf( %d%d%d , &n, &s, &e); n   for (int i = 1; i <= n; i++) scanf( %d , &x[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &b[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &c[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &d[i]); n   memset(dp, 0x7F, sizeof dp); n   dp[0][0] = 0; n   for (int i = 1; i <= n; i++) { n     int diff = 0; n     if (s < i && e >= i) diff = 1; n     if (e < i && s >= i) diff = -1; n     for (int j = 0; j <= n; j++) { n       int k = j - diff; n       if (k < 0) continue; n       if (i == s) { n         if (k) dp[i][j] = min(dp[i][j], dp[i - 1][j] + c[i] + x[i]); n         dp[i][j + 1] = min(dp[i][j + 1], dp[i - 1][j] + d[i] - x[i]); n       } else if (i == e) { n         if (j) dp[i][j - 1] = min(dp[i][j - 1], dp[i - 1][j] + a[i] + x[i]); n         dp[i][j] = min(dp[i][j], dp[i - 1][j] + b[i] - x[i]); n       } else { n         if (j && k) n           dp[i][j - 1] = n               min(dp[i][j - 1], dp[i - 1][j] + c[i] + a[i] + 2 * x[i]); n         if (k) dp[i][j] = min(dp[i][j], dp[i - 1][j] + c[i] + b[i]); n         if (j) dp[i][j] = min(dp[i][j], dp[i - 1][j] + d[i] + a[i]); n         dp[i][j + 1] = min(dp[i][j + 1], dp[i - 1][j] + d[i] + b[i] - 2 * x[i]); n       } n     } n     if (i == n) continue; n     diff = 0; n     if (s <= i && e > i) diff = 1; n     if (e <= i && s > i) diff = -1; n     for (int j = 0; j <= n; j++) { n       int k = j - diff; n       if (j <= 0 && k <= 0) dp[i][j] = 1e18; n     } n   } n   printf( %lld n , dp[n][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d, e, f, g = 0, h = 0, i = 0, j, k, l, p = 0; n   cin >> a >> b >> c >> d >> e >> f; n   g = a - d; n   h = b - e; n   i = c - f; n   if (g > 0) { n     j = g / 2; n   } else { n     j = 0; n   } n   if (h > 0) { n     k = h / 2; n   } else { n     k = 0; n   } n   if (i > 0) { n     l = i / 2; n   } else { n     l = 0; n   } n   if (g >= 0) { n     p = p + j; n   } else { n     p = p + g; n   }; n   if (h >= 0) { n     p = p + k; n   } else { n     p = p + h; n   } n   if (i >= 0) { n     p = p + l; n   } else { n     p = p + i; n   } n   if (p >= 0) { n     cout <<  Yes ; n   } else { n     cout <<  No ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, i, sum = 0; n   scanf( %d , &n); n   if (n == 1) sum = 1; n   if (n == 2) sum = 5; n   if (n == 3) sum = 13; n   if (n >= 4) { n     for (i = 4; i <= n; i++) { n       sum = sum + i * i - (i - 2) * (i - 2); n     } n     sum = sum + 13; n   } n   printf( %d , sum); n } n


#include <bits/stdc++.h> n  n typedef long long int ll; n typedef long double ld; n typedef unsigned long long ull; n  n #define RESET(a, b) memset(a, b, sizeof(a)) n int INF = 1000000005; n long long INFF = 1000000000000000005LL; n  n using namespace std; n  n  n struct monster { n     ll health, attack_power; n }; n  n int main() { n  n  n  n     ll t; n     cin>>t; n     while(t--) n     { n         ll a,b; n         cin>>a>>b; n         ll ans=INFF; n         if(a==1) n         { n             if(b!=1) n             { n                 cout<<1<<endl; n             } n             else n             { n                 cout<< 2 n ; n             } n             continue; n         } n         if(b>a) n         { n             cout<< 1 n ; n             continue; n         } n  n  n  n         for(int i=0;i<=100000;i++) n         { n             ll x=b+i; n             ll y=a; n             ll cnt=i; n             bool ok=0; n             while(y and x!=1) n             { n                 ok=1; n                 y=y/x; n                 cnt++; n             } n  n             //cout<<x<<endl; n             if(ok)ans=min(ans,cnt); n         } n         cout<<ans<<endl; n  n  n     } n  n  n  n     //ctime; n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int upd[400000][10]; n long long val[400000][10]; n bool dirty[400000]; n void clean(int n, int l, int r) { n   long long buf[10]; n   memset(buf, 0, sizeof buf); n   for (int i = 0; i < 10; i++) { n     buf[upd[n][i]] += val[n][i]; n   } n   dirty[n] = false; n   for (int i = 0; i < 10; i++) { n     val[n][i] = buf[i]; n   } n   if (l < r) { n     dirty[n * 2] = dirty[n * 2 + 1] = true; n     for (int i = 0; i < 10; i++) { n       upd[n * 2][i] = upd[n][upd[n * 2][i]]; n       upd[n * 2 + 1][i] = upd[n][upd[n * 2 + 1][i]]; n     } n   } n   for (int i = 0; i < 10; i++) { n     upd[n][i] = i; n   } n } n long long get(int rl, int rr, int n = 1, int l = 0, int r = 100000) { n   if (l > rr || r < rl) return 0; n   if (dirty[n]) clean(n, l, r); n   if (l >= rl && r <= rr) { n     long long ans = 0; n     for (int i = 0; i < 10; i++) { n       ans += val[n][i] * i; n     } n     return ans; n   } n   long long ans = get(rl, rr, n * 2, l, (l + r) / 2); n   ans += get(rl, rr, n * 2 + 1, (l + r) / 2 + 1, r); n   return ans; n } n void update(int rl, int rr, int from, int to, int n = 1, int l = 0, n             int r = 100000) { n   if (l > rr || r < rl) { n     clean(n, l, r); n     return; n   } n   if (dirty[n]) clean(n, l, r); n   if (l >= rl && r <= rr) { n     for (int i = 0; i < 10; i++) { n       if (upd[n][i] == from) upd[n][i] = to; n     } n     dirty[n] = true; n     clean(n, l, r); n     return; n   } n   update(rl, rr, from, to, n * 2, l, (l + r) / 2); n   update(rl, rr, from, to, n * 2 + 1, (l + r) / 2 + 1, r); n   for (int i = 0; i < 10; i++) { n     val[n][i] = val[n * 2][i] + val[n * 2 + 1][i]; n   } n } n int n, q; n int arr[100000]; n void build(int n = 1, int l = 0, int r = 100000) { n   for (int i = 0; i < 10; i++) upd[n][i] = i; n   if (l == r) { n     if (l < n) { n       int mul = 1; n       int c = arr[l]; n       while (c) { n         val[n][c % 10] += mul; n         c /= 10; n         mul *= 10; n       } n     } n     return; n   } n   build(n * 2, l, (l + r) / 2); n   build(n * 2 + 1, (l + r) / 2 + 1, r); n   for (int i = 0; i < 10; i++) { n     val[n][i] = val[n * 2][i] + val[n * 2 + 1][i]; n   } n } n void printtree() { n   for (int i = 0; i < n; i++) { n     cout << get(i, i) <<    ; n   } n   cout << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> q; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   build(); n   for (int i = 0; i < q; i++) { n     int t, l, r, x, y; n     cin >> t; n     if (t == 1) { n       cin >> l >> r >> x >> y; n       update(l - 1, r - 1, x, y); n     } else { n       cin >> l >> r; n       cout << get(l - 1, r - 1) << endl; n     } n   } 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   int n; n   cin >> n; n   string ans; n   string t; n   for (int i = 0; i < n; i++) { n     cin >> t; n     if (i == 0) { n       ans = t; n       continue; n     } n     for (int j = 0; j < t.size(); j++) n       if (ans[j] ==  ? ) n         ans[j] = t[j]; n       else if (ans[j] == t[j] || t[j] ==  ? ) n         ans[j] = ans[j]; n       else n         ans[j] =  . ; n   } n   for (int i = 0; i < ans.size(); i++) n     if (ans[i] ==  . ) n       ans[i] =  ? ; n     else if (ans[i] ==  ? ) n       ans[i] =  a ; n   ios_base::sync_with_stdio(false); n   printf( %s , ans.c_str()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dr[] = {-1, 0, 1, 0, -1, -1, 1, 1}; n const int dc[] = {0, -1, 0, 1, -1, 1, -1, 1}; n const int N = 212345; n int n, m, q, p[N], a[N]; n int f[N], g[N], last[N]; n vector<int> loc[N]; n int maxm[N]; n int my_find(int id, int val) { n   int l = 0, r = loc[id].size(); n   while (l < r) { n     int mid = l + (r - l) / 2; n     int x = loc[id][mid]; n     if (f[x] > val) n       r = mid; n     else n       l = mid + 1; n   } n   if (l == loc[id].size()) n     return m + 1; n   else n     return loc[id][l]; n } n int tmin[N * 4]; n void build(int o, int l, int r) { n   if (l == r) n     tmin[o] = maxm[l]; n   else { n     int mid = l + (r - l) / 2; n     build(o * 2, l, mid); n     build(o * 2 + 1, mid + 1, r); n     tmin[o] = min(tmin[o * 2], tmin[o * 2 + 1]); n   } n } n const int INF = int(1e9); n int query(int o, int l, int r, int ql, int qr) { n   if (ql <= l && r <= qr) { n     return tmin[o]; n   } else { n     int mid = l + (r - l) / 2; n     int lmax = INF, rmax = INF; n     if (ql <= mid) lmax = query(o * 2, l, mid, ql, qr); n     if (qr > mid) rmax = query(o * 2 + 1, mid + 1, r, ql, qr); n     return min(lmax, rmax); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cin >> n >> m >> q; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     p[x] = i; n   } n   for (int i = 1; i <= m; i++) { n     cin >> a[i]; n     a[i] = p[a[i]]; n     loc[a[i]].push_back(i); n   } n   for (int i = 1; i <= n; i++) sort((loc[i]).begin(), (loc[i]).end()); n   for (int i = 1; i <= m; i++) { n     last[a[i]] = i; n     if (a[i] == 1) n       f[i] = i; n     else n       f[i] = f[last[a[i] - 1]]; n   } n   for (int i = 1; i <= n; i++) last[i] = m + 1; n   g[m + 1] = m + 1; n   for (int i = m; i >= 1; i--) { n     last[a[i]] = i; n     if (a[i] == n) n       g[i] = i; n     else n       g[i] = g[last[a[i] + 1]]; n   } n   for (int i = 1; i <= m; i++) { n     if (a[i] == 1) n       maxm[i] = g[i]; n     else n       maxm[i] = my_find(a[i] - 1, g[i]); n   } n   build(1, 1, m); n   string ans; n   while (q--) { n     int l, r; n     cin >> l >> r; n     if (query(1, 1, m, l, r) <= r) n       ans.push_back( 1 ); n     else n       ans.push_back( 0 ); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2050; n int p[N], id[N], rmd[N], P[N], n; n int qry(vector<int> vec) { n   cout <<  ?   << vec.size() <<    ; n   for (auto t : vec) cout << t <<    ; n   cout << endl, fflush(stdout); n   int ok; n   cin >> ok; n   return ok; n } n int query(int x) { n   vector<int> vec; n   for (int i = 1; i <= n; ++i) n     if (!p[i] && x != i) vec.push_back(i); n   return qry(vec); n } n void query(int x, int y, int xx, int yy) { n   for (int i = 1; i <= n; ++i) n     if (!id[x] && !p[i] && rmd[i] == y) { n       if (query(i)) id[x] = i; n     } else if (!id[xx] && !p[i] && rmd[i] == yy) n       if (query(i)) id[xx] = i; n   p[id[x]] = x, p[id[xx]] = xx; n } n int main() { n   cin >> n; n   query(1, 0, n, 0); n   for (int t = 2, l = 2, r = n - 1; l <= r; t <<= 1) { n     for (int i = 1; i <= n; ++i) n       if (!p[i]) { n         vector<int> vec; n         for (int j = 1; j <= t; ++j) n           if (j % t != rmd[i]) vec.push_back(id[j]); n         vec.push_back(i); n         if (qry(vec)) rmd[i] += t >> 1; n       } n     while (l <= 2 * t && l <= r) query(l, l % t, r, r % t), ++l, --r; n   } n   if (p[1] * 2 > n) n     for (int i = 1; i <= n; ++i) p[i] = n - p[i] + 1; n   cout <<  !  ; n   for (int i = 1; i <= n; ++i) cout << p[i] <<    ; n   cout << endl, fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n const bool TEST = 0; n const int inf = 0x7fffffff; n using namespace std; n template <typename T> n void pA(T *begin, int n) { n   for (int i = 0; i < n; i++) { n     cout << *(begin++); n   } n   printf(  n ); n } n const char cho[4] = { R ,  B ,  Y ,  G }; n vector<unsigned int> fAns; n bool flag = 0; n bool check(string s, vector<unsigned int> ans) { n   if (flag) return 0; n   if (TEST) cout << s << endl; n   ; n   for (int c = 0; c < 4; c++) { n     int pre = -4; n     for (unsigned int i = 0; i < s.size(); i++) { n       if (s[i] ==  ! ) { n         if (c == 3) n           return 1; n         else n           continue; n       } n       if (s[i] == cho[c]) { n         if (i - pre >= 4) { n           pre = i; n         } else n           return false; n       } n     } n   } n   fAns = ans; n   flag = 1; n   return 0; n } n int cor(char x) { n   switch (x) { n     case  R : n       return 0; n     case  B : n       return 1; n     case  Y : n       return 2; n     case  G : n       return 3; n   } n   return 30; n } n void count(string s, vector<unsigned int> ans) { n   if (TEST) cout <<  int count  << s << endl; n   int sum = 0; n   for (unsigned int i = 0; i < 4; i++) { n     sum += cor(s[i]); n   } n   if (sum != 6) return; n   for (unsigned int i = 4; i < s.size(); i++) { n     if (s[i] ==  ! ) { n       ans[cor(s[i - 4])]++; n     } n   } n   fAns = ans; n } n void dfs(unsigned int st, unsigned int curTol, const string s, n          vector<unsigned int> ans) { n   bool val = 0; n   val = check(s, ans); n   if (!val) return; n   if (st == 4) { n     count(s, ans); n   } n   if (TEST) cout << val << endl; n   for (unsigned int i = st; i < 4; i++) { n     if (s[i] ==  ! ) { n       for (int c = 0; c < 4; c++) { n         string s2 = s; n         s2[i] = cho[c]; n         ans[c]++; n         dfs(i + 1, curTol + 1, s2, ans); n         ans[c]--; n       } n       return; n     } n   } n   count(s, ans); n } n void sol() { n   string s; n   cin >> s; n   for (int i = 0; i < 4; i++) { n     fAns.push_back(0); n   } n   dfs(0, 0, s, fAns); n   for (int i = 0; i < 4; i++) { n     if (0 != i) cout <<    ; n     cout << fAns[i]; n   } n   cout << endl; n } n void sol2() { n   string s; n   cin >> s; n   for (int i = 0; i < 4; i++) { n     fAns.push_back(0); n   } n   for (int c = 0; c < 4; c++) { n     char x = cho[c]; n     unsigned int pos = s.find_first_of(x); n     pos %= 4; n     for (unsigned int i = pos; i < s.size(); i += 4) { n       if ( !  == s[i]) fAns[cor(x)]++; n     } n   } n   for (int i = 0; i < 4; i++) { n     if (0 != i) cout <<    ; n     cout << fAns[i]; n   } n   cout << endl; n } n int main() { n   sol2(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mx = 1e5 + 10; n long long n, m, a[mx], cnt[mx], r[mx], l[mx], p[mx], ans[mx]; n int main() { n   cin >> n >> m; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     if (a[i] < mx) cnt[a[i]]++; n   } n   for (long long i = 0; i < m; i++) cin >> l[i] >> r[i]; n   for (long long in = 1; in < mx; in++) { n     if (cnt[in] >= in) { n       p[0] = 0; n       for (long long i = 1; i <= n; i++) p[i] = p[i - 1] + (a[i] == in); n       for (long long i = 0; i < m; i++) n         ans[i] += ((p[r[i]] - p[l[i] - 1]) == in); n     } n   } n   for (long long i = 0; i < m; i++) cout << ans[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[1000010]; n char b[1000010]; n int main() { n   cin >> a; n   int ind = 0; n   int ans = 0; n   int n = strlen(a); n   for (int i = 0; i < n; i++) { n     if (a[i] ==  [  || a[i] ==  <  || a[i] ==  (  || a[i] ==  { ) { n       b[ind] = a[i]; n       ind++; n     } else { n       ind--; n       if (ind < 0) { n         cout <<  Impossible ; n         return 0; n       } n       if (b[ind] ==  [  && a[i] !=  ] ) { n         ans++; n       } else if (b[ind] ==  <  && a[i] !=  > ) n         ans++; n       else if (b[ind] ==  {  && a[i] !=  } ) n         ans++; n       else if (b[ind] ==  (  && a[i] !=  ) ) n         ans++; n     } n   } n   if (ind > 0) n     cout <<  Impossible ; n   else n     cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -f; n   for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); n   x *= f; n } n inline void print(int x) { n   if (x < 0) putchar( - ), x = -x; n   if (!x) return; n   print(x / 10), putchar((x % 10) ^ 48); n } n inline void write(int x) { n   if (!x) n     putchar( 0 ); n   else n     print(x); n   putchar(  n ); n } n int n, ans[5], head[1000050], tot, mx[1000050], mx1[1000050], p, dep[1000050], n     d, vis[1000050], f[1000050]; n struct edge { n   int to, nxt; n } e[1000050 << 1]; n void ins(int u, int v) { e[++tot] = (edge){v, head[u]}, head[u] = tot; } n void dfs(int x, int fa) { n   dep[x] = dep[fa] + 1; n   int cnt = 0; n   f[x] = fa; n   for (int i = head[x]; i; i = e[i].nxt) n     if (e[i].to != fa && !vis[e[i].to]) { n       dfs(e[i].to, x), cnt++; n       if (!mx[x] || dep[mx[x]] < dep[mx[e[i].to]]) n         mx1[x] = mx[x], mx[x] = mx[e[i].to]; n       else if (dep[mx[e[i].to]] > dep[mx1[x]]) n         mx1[x] = mx[e[i].to]; n     } n   if (!cnt) mx[x] = x; n   if (cnt >= 2) { n     if (dep[x] > d) n       d = dep[x], p = x; n     else if (dep[x] == d && dep[mx1[x]] + dep[mx[x]] > dep[mx1[p]] + dep[mx[p]]) n       p = x; n   } n } n int main() { n   read(n); n   for (int i = 1, x, y; i < n; i++) read(x), read(y), ins(x, y), ins(y, x); n   dfs(1, 0), ans[1] = mx[p], ans[3] = mx1[p]; n   for (int i = head[p]; i; i = e[i].nxt) n     if (e[i].to != f[p] && (mx[e[i].to] == mx[p] || mx[e[i].to] == mx1[p])) n       vis[e[i].to] = 1; n   memset(mx, 0, sizeof mx), memset(mx1, 0, sizeof mx1); n   d = 0; n   dfs(p, 0), ans[2] = mx[p], ans[4] = mx1[p]; n   printf( %d %d n%d %d n , ans[1], ans[2], ans[3], ans[4]); 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   string st[n]; n   int a[m]; n   for (int i = 0; i < n; i++) { n     cin >> st[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n   } n   int maxa = 0; n   int ca, cb, cc, cd, ce; n   for (int i = 0; i < m; i++) { n     ca = 0; n     cb = 0; n     cc = 0; n     cd = 0; n     ce = 0; n     for (int j = 0; j < n; j++) { n       if (st[j][i] ==  A ) ca++; n       if (st[j][i] ==  B ) cb++; n       if (st[j][i] ==  C ) cc++; n       if (st[j][i] ==  D ) cd++; n       if (st[j][i] ==  E ) ce++; n     } n     int x1 = max(ca, cb); n     int x2 = max(cc, cd); n     int x3 = max(max(x1, x2), ce); n     maxa += x3 * a[i]; n   } n   cout << maxa << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 2.0 * acos(0.0); n inline int getInt() { n   int x; n   scanf( %d , &x); n   return x; n } n inline long long getLL() { n   long long x; n   scanf( %lld , &x); n   return x; n }; n inline int NUM(char c) { return (int)c - 48; } n inline char CHR(int n) { return (char)(n + 48); } n template <class T> n inline T MAX(T a, T b) { n   if (a > b) return a; n   return b; n } n template <class T> n inline T MIN(T a, T b) { n   if (a < b) return a; n   return b; n } n inline void OPEN(string s) { n   freopen((s +  .in ).c_str(),  r , stdin); n   freopen((s +  .out ).c_str(),  w , stdout); n } n int N; n int A[100000 + 5]; n int sorted[100000 + 5]; n map<int, stack<int> > MAP; n int pos[100000 + 5]; n int idx = -1; n inline bool islucky(int x) { n   if (x == 0) return false; n   while (x) { n     if (x % 10 != 4 && x % 10 != 7) return false; n     x /= 10; n   } n   return true; n } n vector<pair<int, int> > sol; n stack<int> tumpuk; n bool found[100000 + 5]; n inline int jelajah(int i) { n   found[i] = true; n   int root = i; n   while (pos[i] != root) { n     sol.push_back(make_pair(i, pos[i])); n     i = pos[i]; n     found[i] = true; n   } n   return i; n } n int main() { n   scanf( %d , &N); n   for (int i = 0, _n = (N); i < _n; i++) { n     scanf( %d , &A[i]); n     if (islucky(A[i])) idx = i; n     sorted[i] = A[i]; n   } n   sort(sorted, sorted + N); n   bool so = true; n   for (int i = 0, _n = (N); i < _n; i++) { n     if (A[i] != sorted[i]) { n       so = false; n     } n   } n   if (idx == -1 && !so) { n     puts( -1 ); n     return 0; n   } else if (idx == -1) { n     puts( 0 ); n     return 0; n   } n   for (int i = (N)-1; i >= 0; i--) { n     MAP[sorted[i]].push(i); n   } n   for (int i = 0, _n = (N); i < _n; i++) { n     int x = A[i]; n     A[i] = MAP[x].top(); n     MAP[x].pop(); n     pos[A[i]] = i; n   } n   idx = jelajah(idx); n   for (int i = 0, _n = (N); i < _n; i++) { n     if (!found[i]) { n       tumpuk.push(idx); n       sol.push_back(make_pair(idx, i)); n       idx = jelajah(i); n     } n   } n   while ((tumpuk).size() > 0) { n     sol.push_back(make_pair(idx, tumpuk.top())); n     idx = tumpuk.top(); n     tumpuk.pop(); n   } n   printf( %d n , (sol).size()); n   for (int i = 0, _n = ((sol).size()); i < _n; i++) { n     printf( %d %d n , sol[i].first + 1, sol[i].second + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int TAILLE_MAX = 1000; n int main() { n   int n, l, r; n   cin >> n >> l; n   int p[TAILLE_MAX]; n   for (int i = 0; i < n; i++) cin >> p[i]; n   sort(p, p + n); n   r = max(p[0], l - p[n - 1]) * 2; n   for (int i = 0; i < n - 1; i++) r = max(r, p[i + 1] - p[i]); n   cout.precision(20); n   cout << fixed << r / 2.0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = (int)2E5 + 5; n const long long mdls = (long long)1E9 + 7; n vector<int> Adj[maxN]; n vector<long long> suff[maxN], pref[maxN]; n long long f[maxN], g[maxN], h[maxN]; n int n; n void DFS(int u) { n   f[u] = 1; n   for (int i = 0, sz = Adj[u].size(); i < sz; ++i) { n     int v = Adj[u][i]; n     DFS(v); n     f[u] = (f[u] * (f[v] + 1)) % mdls; n   } n   int sz = Adj[u].size(); n   suff[u].resize(sz); n   pref[u].resize(sz); n   long long temp = 1; n   for (int i = 0; i < sz; ++i) { n     temp = (temp * (f[Adj[u][i]] + 1)) % mdls; n     pref[u][i] = temp; n   } n   temp = 1; n   for (int i = sz - 1; i >= 0; --i) { n     temp = (temp * (f[Adj[u][i]] + 1)) % mdls; n     suff[u][i] = temp; n   } n } n void DFS2(int u) { n   for (int i = 0, sz = Adj[u].size(); i < sz; ++i) { n     int v = Adj[u][i]; n     h[v] = h[u]; n     if (i > 0) h[v] = (h[v] * pref[u][i - 1]) % mdls; n     if (i < sz - 1) h[v] = (h[v] * suff[u][i + 1]) % mdls; n     h[v] = (h[v] + 1) % mdls; n     g[v] = (f[v] * h[v]) % mdls; n     DFS2(v); n   } n } n int main() { n   scanf( %d , &n); n   for (int u = 1; u <= n; ++u) Adj[u].clear(); n   for (int v = 2; v <= n; ++v) { n     int u; n     scanf( %d , &u); n     Adj[u].push_back(v); n   } n   DFS(1); n   h[1] = 1; n   g[1] = f[1]; n   DFS2(1); n   for (int u = 1; u <= n; ++u) printf( %I64d  , g[u]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n template <class T> n int size(T &&x) { n   return int(x.size()); n } n template <class A, class B> n ostream &operator<<(ostream &out, const pair<A, B> &p) { n   return out <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class T> n auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { n   out <<  { ; n   for (auto it = x.begin(); it != x.end(); ++it) n     out << *it << (it == prev(x.end()) ?    :  ,  ); n   return out <<  } ; n } n void dump() {} n template <class T, class... Args> n void dump(T &&x, Args... args) { n   cerr << x <<  ;   ; n   dump(args...); n } n mt19937_64 rng(0); n int rd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int _degsize; n   cin >> _degsize; n   vector<int> d(_degsize); n   for (int &di : d) cin >> di; n   vector<pair<int, int>> edges; n   auto add_edge = [&](int v, int u) { n     assert(v != u); n     edges.emplace_back(v, u); n   }; n   while (size(d)) { n     0 && cerr; n     int n = d.back() + 1; n     if (size(d) == 1) { n       for (int v = (0); v <= ((n)-1); ++v) n         for (int u = (0); u <= ((v)-1); ++u) add_edge(v, u); n       d.clear(); n       break; n     } n     int max_cnt = d.front(); n     for (int &di : d) di -= max_cnt; n     for (int v = (n - max_cnt); v <= (n - 1); ++v) n       for (int u = (0); u <= ((v)-1); ++u) add_edge(v, u); n     d.erase(d.begin()); n     d.erase(prev(d.end())); n   } n   cout << size(edges) <<   n ; n   for (auto [v, u] : edges) cout << v + 1 <<     << u + 1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 5e5 + 10; n const double eps = 1e-9; n const int inf = 1e9; n char a[mx]; n multiset<char> ms; n int main() { n   scanf( %s , a); n   int n = strlen(a); n   int mn = a[0] -  a ; n   printf( Mike n ); n   for (int i = 1; i < n; i++) { n     if (a[i] -  a  > mn) { n       printf( Ann n ); n     } else n       printf( Mike n ); n     mn = min(mn, a[i] -  a ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int dem = 0; n   int n; n   int tong = 0; n   int s; n   cin >> n; n   for (int i = 9; i >= 1; i--) { n     if (n % i == 0) { n       s = i; n       break; n     } n   } n   int z; n   z = n / s; n   cout << z << endl; n   while (z) { n     cout << s <<    ; n     z--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string removeZero(string s) { n   int i = 0, m = 0, j = s.size() - 1; n   if (s[i] ==  0 ) { n     while (s[i] ==  0 ) { n       i++; n     } n   } n   s.erase(0, i); n   if (s[j] ==  0 ) { n     while (s[j] ==  0 ) { n       m++; n       j--; n     } n   } n   s.erase(j + 1, s.size() - j); n   return s; n } n int main() { n   string s; n   cin >> s; n   s = removeZero(s); n   int x = 0, o = s.size() - 1, flag = 0; n   while (x <= s.size() - 1) { n     if (s[x] != s[o]) { n       flag = 1; n       cout <<  NO ; n       break; n     } n     x++; n     o--; n   } n   if (flag == 0) cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n const int maxm = 1e5 + 5; n const long double eps = 1e-14; n const long double eps2 = 1e-9; n const long double INF = 1 << 30; n inline int dcmp(const long double& x) { n   return fabs(x) < eps ? 0 : (x < 0 ? -1 : 1); n } n template <typename T> n inline bool getmin(T& a, const T& b) { n   return dcmp(b - a) < 0 ? (a = b, true) : false; n } n template <typename T> n inline bool getmax(T& a, const T& b) { n   return dcmp(b - a) > 0 ? (a = b, true) : false; n } n pair<long double, long double> operator+( n     const pair<long double, long double>& a, n     const pair<long double, long double>& b) { n   return {a.first + b.first, a.second + b.second}; n } n pair<long double, long double> operator-( n     const pair<long double, long double>& a, n     const pair<long double, long double>& b) { n   return {a.first - b.first, a.second - b.second}; n } n pair<long double, long double> operator*( n     const pair<long double, long double>& a, const long double& b) { n   return {a.first * b, a.second * b}; n } n long double dot(const pair<long double, long double>& a, n                 const pair<long double, long double>& b) { n   return a.first * b.first + a.second * b.second; n } n long double cross(const pair<long double, long double>& a, n                   const pair<long double, long double>& b) { n   return a.first * b.second - a.second * b.first; n } n long double abs(const pair<long double, long double>& a) { n   return sqrt(dot(a, a)); n } n struct Iron { n   int show, speed, st, ed; n   inline void read() { n     scanf( %d%d%d%d , &show, &speed, &st, &ed); n     st--, ed--; n   } n } iron[maxm]; n struct Iron2 { n   long double from, fromt, to, tot; n }; n struct Path { n   int id, from, to; n }; n struct Event { n   long double t; n   int segid; n   bool operator<(const Event& rhs) const { return dcmp(t - rhs.t) < 0; } n }; n int n, m; n vector<int> G[maxn], chain[maxn]; n int fa[maxn], dep[maxn], chainid[maxn], size[maxn]; n int pre[maxn], dfs_clock; n void dfs(int now) { n   vector<int>& vec = chain[chainid[now]]; n   for (int i = ((int)(vec.size())) - 1; i >= 0; i--) pre[vec[i]] = dfs_clock++; n   for (int i = 0; i < ((int)(vec.size())); i++) { n     int now = vec[i]; n     for (const int& to : G[now]) n       if ((!i || to != vec[i - 1]) && to != fa[now]) dfs(to); n   } n } n inline void make_HLD() { n   int first = 0, last = 0; n   int* q = new int[n]; n   q[last++] = 0; n   fa[0] = -1; n   dep[0] = 0; n   while (last - first > 0) { n     int now = q[first++]; n     for (const int& to : G[now]) n       if (to != fa[now]) { n         fa[to] = now; n         dep[to] = dep[now] + 1; n         q[last++] = to; n       } n   } n   for (int i = last - 1; i >= 0; i--) { n     int now = q[i], hs = -1; n     size[now] = 1; n     for (const int& to : G[now]) n       if (to != fa[now]) { n         size[now] += size[to]; n         if (hs < 0 || size[to] > size[hs]) hs = to; n       } n     if (hs < 0) n       chainid[now] = now; n     else n       chainid[now] = chainid[hs]; n     chain[chainid[now]].push_back(now); n   } n   dfs_clock = 0; n   delete[] q; n   dfs(0); n } n inline bool inrange(const long double& x, const long double& L, n                     const long double& R) { n   return dcmp(x - L) >= 0 && dcmp(x - R) <= 0; n } n long double interPnt(pair<long double, long double> p1, n                      pair<long double, long double> p2, n                      pair<long double, long double> q1, n                      pair<long double, long double> q2) { n   pair<long double, long double> d = p2 - p1, e = q2 - q1; n   long double crs = cross(d, e); n   if (abs(crs) < eps) { n     long double c2 = cross(d, q1 - p1); n     if (abs(c2) > eps) return INF; n     long double da2 = dot(d, d); n     long double f1 = dot(d, q1 - p1) / da2; n     long double f2 = dot(d, q2 - p1) / da2; n     if (f1 < -eps and f2 < -eps) return INF; n     if (f1 > 1 + eps and f2 > 1 + eps) return INF; n     long double f = max(0.l, min(f1, f2)); n     return (p1 + d * f).first; n   } n   long double f1 = cross(d, q1 - p1); n   long double f2 = cross(d, p1 - q2); n   long double f = f1 + f2; n   pair<long double, long double> x = q1 * (f2 / f) + q2 * (f1 / f); n   long double fp = f1 / f; n   long double gp = dot(x - p1, d) / dot(d, d); n   if (fp < -eps or fp > 1 + eps) return INF; n   if (gp < -eps or gp > 1 + eps) return INF; n   return x.first; n } n inline long double getIntersectime(const Iron2& A, const Iron2& B) { n   return interPnt(make_pair(A.fromt, A.from), make_pair(A.tot, A.to), n                   make_pair(B.fromt, B.from), make_pair(B.tot, B.to)); n   const long double &x1 = A.from, &t1 = A.fromt, &x2 = A.to, &t2 = A.tot, n                     &x3 = B.from, &t3 = B.fromt, &x4 = B.to, &t4 = B.tot; n   long double deno = (x2 - x1) * (t4 - t3) - (t2 - t1) * (x4 - x3); n   long double nume = (x3 - x1) * (t2 - t1) * (t4 - t3) - n                      t3 * (t2 - t1) * (x4 - x3) + t1 * (t4 - t3) * (x2 - x1); n   long double ans; n   if (dcmp(deno) == 0) { n     if (dcmp((x4 - x3) / (t4 - t3) * (t1 - t3) + x3 - x1) == 0) n       ans = max(t1, t3); n     else n       ans = INF; n   } else n     ans = nume / deno; n   if (inrange(ans, max(t1, t3), min(t2, t4))) n     return ans; n   else n     return INF; n } n long double curtime; n vector<Iron2> vec; n struct cmp { n   inline long double getpos(const Iron2& I, const long double& t) const { n     return I.from + ((I.to - I.from) / (I.tot - I.fromt)) * (t - I.fromt); n   } n   bool operator()(const int& a, const int& b) const { n     long double d1 = dcmp(getpos(vec[a], curtime) - getpos(vec[b], curtime)), n                 d2 = dcmp(getpos(vec[a], curtime + 1) - n                           getpos(vec[b], curtime + 1)); n     return d1 < 0 || (d1 == 0 && d2 < 0) || (d1 == 0 && d2 == 0 && a < b); n   } n }; n vector<Iron2> Seg[maxn]; n inline long double solve_path(const int& nowSeg) { n   vec = Seg[nowSeg]; n   set<int, cmp> second; n   vector<Event> eve; n   for (int i = 0; i < ((int)(vec.size())); i++) n     eve.push_back((Event){vec[i].fromt, i << 1}), n         eve.push_back((Event){vec[i].tot, i << 1 | 1}); n   sort(eve.begin(), eve.end()); n   long double ans = INF; n   for (const Event& e : eve) { n     curtime = e.t; n     if (dcmp(curtime - ans) >= 0) break; n     int id = e.segid; n     if (!(id & 1)) { n       id /= 2; n       set<int>::iterator it = second.lower_bound(id); n       assert(second.find(id) == second.end()); n       if (it != second.end()) getmin(ans, getIntersectime(vec[id], vec[*it])); n       if (it != second.begin()) n         it--, getmin(ans, getIntersectime(vec[id], vec[*it])); n       second.insert(id); n     } else { n       id /= 2; n       set<int>::iterator it = second.lower_bound(id), itL = it, itR = it; n       assert((*it) == id); n       itR++; n       if (itL != second.begin() && itR != second.end()) { n         itL--; n         getmin(ans, getIntersectime(vec[*itL], vec[*itR])); n       } n       second.erase(it); n     } n   } n   return ans; n } n vector<Path> path, L, R; n inline void getPath(int a, int b) { n   L.clear(); n   R.clear(); n   while (chainid[a] != chainid[b]) { n     int atop = chain[chainid[a]].back(), btop = chain[chainid[b]].back(); n     if (dep[atop] > dep[btop]) n       L.push_back((Path){chainid[a], a, fa[atop]}), a = fa[atop]; n     else n       R.push_back((Path){chainid[b], fa[btop], b}), b = fa[btop]; n   } n   L.push_back((Path){chainid[a], a, b}); n   path.clear(); n   for (int i = 0; i < ((int)(L.size())); i++) path.push_back(L[i]); n   for (int i = ((int)(R.size())) - 1; i >= 0; i--) path.push_back(R[i]); n } n inline int PosOnPath(int id, int now) { n   if (chainid[now] != id) return 0; n   return pre[now] - pre[chain[id].back()] + 1; n } n inline long double solve_tree() { n   make_HLD(); n   for (int i = 0; i < m; i++) { n     getPath(iron[i].st, iron[i].ed); n     long double curt = iron[i].show, v = iron[i].speed; n     for (const Path& p : path) { n       int from = PosOnPath(p.id, p.from), to = PosOnPath(p.id, p.to); n       long double arrt = curt + abs(to - from) / v; n       long double cc = dcmp(arrt - curt) == 0 ? 0 : (to - from) / (arrt - curt); n       Seg[p.id].push_back( n           (Iron2){from - eps2 * cc, curt - eps2, to + eps2 * cc, arrt + eps2}); n       curt = arrt; n     } n   } n   long double res = INF; n   for (int i = 0; i < n; i++) getmin(res, solve_path(i)); n   return res; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0, a, b; i < n - 1; i++) { n     scanf( %d%d , &a, &b); n     a--, b--; n     G[a].push_back(b); n     G[b].push_back(a); n   } n   for (int i = 0; i < m; i++) iron[i].read(); n   long double ans = solve_tree(); n   if (dcmp(ans - INF) >= 0) n     printf( -1 n ); n   else n     printf( %.13f n , (double)ans); n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = 1e14; n long long ab(long long x) { return x > 0 ? x : -x; } n long long a[100005]; n long long kn[66]; n long long sum[100005]; n map<long long, int> mp; n int nxt[100005]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   kn[0] = 1; n   int cnt = 0; n   for (int i = 1;; i++) { n     kn[i] = kn[i - 1] * k; n     if (kn[i] == 1) break; n     cnt++; n     if (ab(kn[i]) >= oo) break; n   } n   for (int i = 1; i <= n; i++) { n     scanf( %lld , &a[i]); n     sum[i] = sum[i - 1] + a[i]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j <= cnt; j++) { n       mp[sum[n] - sum[i] - kn[j]]++; n     } n   } n   long long ans = 0, step = 0; n   for (int i = n; i >= 1; i--) { n     ans += mp[step]; n     for (int j = 0; j <= cnt; j++) { n       mp[sum[n] - sum[i - 1] - kn[j]]--; n     } n     step += a[i]; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e16; n const long long int maxn = 1 << 20; n double pie = 3.1415926535; n string str[101]; n bool done[101]; n int main() { n   long long int n, i, j, cnt, offset; n   string s, s1, ans; n   cin >> n >> s; n   ans =   ; n   for (i = 0; i < n; i++) ans +=  9 ; n   for (i = 0; i < n; i++) { n     offset = 10 - (s[i] -  0 ); n     s1.clear(); n     for (j = i, cnt = 0; cnt < n; cnt++, j++) { n       long long int x = (s[j] -  0  + offset) % 10; n       s1 += (x +  0 ); n       if (j == n - 1) j = -1; n     } n     ans = min(ans, s1); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using std::max; n using std::min; n const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; n const long long INF = 0x3f3f3f3f3f3f3f3f; n std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count()); n template <typename _Tp> n _Tp gcd(const _Tp &a, const _Tp &b) { n   return (!b) ? a : gcd(b, a % b); n } n template <typename _Tp> n inline _Tp abs(const _Tp &a) { n   return a >= 0 ? a : -a; n } n template <typename _Tp> n inline void chmax(_Tp &a, const _Tp &b) { n   (a < b) && (a = b); n } n template <typename _Tp> n inline void chmin(_Tp &a, const _Tp &b) { n   (b < a) && (a = b); n } n template <typename _Tp> n inline void read(_Tp &x) { n   char ch(getchar()); n   bool f(false); n   while (!isdigit(ch)) f |= ch == 45, ch = getchar(); n   x = ch & 15, ch = getchar(); n   while (isdigit(ch)) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); n   f && (x = -x); n } n template <typename _Tp, typename... Args> n inline void read(_Tp &t, Args &...args) { n   read(t); n   read(args...); n } n inline int read_str(char *s) { n   char ch(getchar()); n   while (ch ==     || ch ==   r  || ch ==   n ) ch = getchar(); n   char *tar = s; n   *tar = ch, ch = getchar(); n   while (ch !=     && ch !=   r  && ch !=   n  && ch != EOF) n     *(++tar) = ch, ch = getchar(); n   return tar - s + 1; n } n const int N = 3005; n const int S = 12; n const int mod = 998244353; n template <typename _Tp1, typename _Tp2> n inline void add(_Tp1 &a, _Tp2 b) { n   (a += b) >= mod && (a -= mod); n } n template <typename _Tp1, typename _Tp2> n inline void sub(_Tp1 &a, _Tp2 b) { n   (a -= b) < 0 && (a += mod); n } n long long ksm(long long a, long long b = mod - 2) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod, b >>= 1; n   } n   return res; n } n int n, c; n int a[N]; n int A[N], B[N]; n namespace sub1 { n int dp[N][N / S + 5]; n int f[N][N], cnt[N], ans[N]; n void MAIN() { n   int maxx = n / c; n   for (int j = 1; j <= n; ++j) { n     memset(cnt, 0, sizeof(cnt)); n     int qwq = c - 1, coef = 1; n     if (!qwq) f[j][j] = 1; n     for (int i = j - 1; i >= 1; --i) { n       if (a[i] != a[j]) { n         if (!cnt[a[i]]) n           --qwq; n         else n           coef = 1ll * coef * B[cnt[a[i]]] % mod * A[cnt[a[i]] + 1] % mod; n         ++cnt[a[i]]; n       } n       if (!qwq) f[i][j] = coef; n     } n   } n   dp[0][0] = 1; n   for (int i = 1; i <= n; ++i) n     for (int j = 1, LIM = i / c; j <= LIM; ++j) n       for (int k = 1; k <= i; ++k) n         add(dp[i][j], 1ll * f[k][i] * dp[k - 1][j - 1] % mod); n   memset(cnt, 0, sizeof(cnt)); n   int qwq = c, coef = 1, qaq = 1; n   for (int i = n; i >= 0; --i) { n     for (int j = 0; j <= maxx; ++j) n       add(ans[j], 1ll * (qaq - (qwq ? 0 : coef) + mod) * dp[i][j] % mod); n     if (!cnt[a[i]]) n       --qwq; n     else n       coef = 1ll * coef * B[cnt[a[i]]] % mod * A[cnt[a[i]] + 1] % mod; n     ++cnt[a[i]]; n     add(qaq, qaq); n   } n   sub(ans[0], 1); n   for (int j = 0; j <= n; ++j) printf( %d%c , ans[j],    n [j == n]); n } n }  // namespace sub1 n namespace sub2 { n int dp[N][1 << S]; n void MAIN() { n   int maxx = n / c; n   dp[0][0] = 1; n   for (int i = 0; i < n; ++i) { n     for (int j = i / c; j >= 0; --j) { n       for (int st = (1 << c) - 2; st >= 0; --st) { n         int to = st | 1 << (a[i + 1] - 1); n         if (to + 1 == (1 << c)) n           add(dp[j + 1][0], dp[j][st]); n         else n           add(dp[j][to], dp[j][st]); n       } n     } n   } n   for (int i = 0; i <= n; ++i) { n     int ans = 0; n     if (i <= maxx) n       for (int st = 0; st < 1 << c; ++st) add(ans, dp[i][st]); n     if (!i) sub(ans, 1); n     printf( %d%c , ans,    n [i == n]); n   } n } n }  // namespace sub2 n int main() { n   for (int i = 1; i < N; ++i) n     A[i] = (A[i - 1] + A[i - 1] + 1) % mod, B[i] = ksm(A[i]); n   read(n, c); n   for (int i = 1; i <= n; ++i) read(a[i]); n   if (c > S) n     sub1::MAIN(); n   else n     sub2::MAIN(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inff = 0x3f3f3f3f3f3f3f3f; n int main() { n   cin.tie(0); n   long long k, d, t, qw, e; n   scanf( %I64d , &k), scanf( %I64d , &d), scanf( %I64d , &t); n   qw = (k / d + 1) * d; n   if (k % d == 0) { n     cout << t << endl; n     return 0; n   } n   double s = 0.0, zz, po; n   po = (qw - k) * 0.5 + k; n   e = 2ll * t / (qw + k); n   e *= qw; n   s += e; n   e /= qw; n   zz = t - e * po; n   if (zz <= k) { n     s += zz; n   } else { n     s += k; n     zz -= k; n     s += zz * 2.0; n   } n   printf( %.10f n , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fff() { n   freopen( dravde.in ,  r , stdin); n   freopen( dravde.out ,  w , stdout); n } n const int MAXN = 100001; n int n; n map<int, int> m[MAXN]; n int f[MAXN], s[MAXN], p[MAXN], cnt = 0, z; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int v, c, l, r; n     scanf( %d%d%d%d , &v, &c, &l, &r); n     if (r + c < MAXN && (!l || m[l].find(r + c) != m[l].end())) { n       int u = m[l][r + c]; n       f[i] = f[u] + v, p[i] = u; n       if (!r && f[i] > f[z]) { n         z = i; n       } n       if (f[i] > f[m[l + c][r]]) { n         m[l + c][r] = i; n       } n     } n   } n   while (z) { n     s[cnt++] = z; n     z = p[z]; n   } n   printf( %d n , cnt); n   while (cnt--) { n     printf( %d  , s[cnt]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, v; n pair<int, int> p[10]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     scanf(  n ); n     for (int j = 1; j <= m; ++j) { n       char ch = getchar(); n       if (ch ==  * ) p[++v] = make_pair(i, j); n     } n   } n   if (p[1].first == p[2].first && p[1].second == p[3].second) n     printf( %d %d , p[3].first, p[2].second); n   else if (p[1].first == p[3].first && p[1].second == p[2].second) n     printf( %d %d , p[2].first, p[3].second); n   else if (p[2].first == p[1].first && p[2].second == p[3].second) n     printf( %d %d , p[3].first, p[1].second); n   else if (p[2].first == p[3].first && p[2].second == p[1].second) n     printf( %d %d , p[1].first, p[3].second); n   else if (p[3].first == p[1].first && p[3].second == p[2].second) n     printf( %d %d , p[2].first, p[1].second); n   else if (p[3].first == p[2].first && p[3].second == p[1].second) n     printf( %d %d , p[1].first, p[2].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long tests, t, qq, q, i, j, bb[200000], s1, ans[2100][2100]; n long long a, b; n long solve(long long a, long long b) { n   if (a < b) swap(a, b); n   if (a == 0 || b == 0) return 0; n   if (solve(a % b, b) == 0) return 1; n   t = a / b; n   if (b % 2 == 1) return 1 - t % 2; n   t %= (b + 1); n   if (t % 2 == 1) return 0; n   return 1; n } n int main() { n   long s = 0; n   cin >> tests; n   for (; tests; --tests) { n     cin >> a >> b; n     if (solve(a, b)) n       cout <<  First  << endl; n     else n       cout <<  Second  << endl; n   } n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100500; n using ll = long long; n using point = pair<ll, ll>; n ll& x(point& p) { return p.first; } n ll& y(point& p) { return p.second; } n int n; n point p[N], p2[N]; n set<point> s; n point centerN, center2, invP; n int no() { n   cout <<  NO  << endl; n   return 0; n } n int main() { n   cin >> n; n   x(centerN) = 0; n   y(centerN) = 0; n   for (int i = 0; i < n; ++i) { n     cin >> x(p[i]) >> y(p[i]); n     x(p2[i]) = 2 * x(p[i]); n     y(p2[i]) = 2 * y(p[i]); n     x(centerN) += x(p[i]); n     y(centerN) += y(p[i]); n   } n   if ((2 * x(centerN)) % n != 0 || (2 * y(centerN)) % n != 0) { n     return no(); n   } n   x(center2) = 2 * x(centerN) / n; n   y(center2) = 2 * y(centerN) / n; n   for (int i = 0; i < n; ++i) { n     x(p2[i]) -= x(center2); n     y(p2[i]) -= y(center2); n     s.insert(p2[i]); n   } n   for (int i = 0; i < n; ++i) { n     x(invP) = -x(p2[i]); n     y(invP) = -y(p2[i]); n     auto it = s.find(invP); n     if (it == s.end()) { n       return no(); n     } n   } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n int kek[20]; n long long a[696969]; n void upd(int x, int v, int s = 0, int e = (1 << n) - 1, int d = n - 1, n          int p = 1) { n   if (s == e) { n     a[p] = v; n     return; n   } n   int m = (s + e) / 2; n   if (x <= m) { n     upd(x, v, s, m, d - 1, 2 * p + kek[d]); n   } else { n     upd(x, v, m + 1, e, d - 1, 2 * p + 1 - kek[d]); n   } n   a[p] = a[2 * p] + a[2 * p + 1]; n } n long long query(int s, int e, int ps = 0, int pe = (1 << n) - 1, int d = n - 1, n                 int p = 1) { n   if (e < ps || pe < s) return 0; n   if (s <= ps && pe <= e) return a[p]; n   int pm = (ps + pe) / 2; n   return query(s, e, ps, pm, d - 1, 2 * p + kek[d]) + n          query(s, e, pm + 1, pe, d - 1, 2 * p + 1 - kek[d]); n } n int main() { n   scanf( %d %d , &n, &q); n   for (int i = 0; i < (1 << n); i++) { n     int x; n     scanf( %d , &x); n     upd(i, x); n   } n   while (q--) { n     int t; n     scanf( %d , &t); n     if (t == 1) { n       int x, v; n       scanf( %d %d , &x, &v); n       x--; n       upd(x, v); n     } n     if (t == 4) { n       int s, e; n       scanf( %d %d , &s, &e); n       printf( %lld n , query(s - 1, e - 1)); n     } n     if (t == 2) { n       int x; n       scanf( %d , &x); n       for (int i = 0; i < x; i++) kek[i] ^= 1; n     } n     if (t == 3) { n       int x; n       scanf( %d , &x); n       kek[x] ^= 1; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct event { n   long long int peakval; n   long long int pos; n   long long int time1; n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int n, m, d; n   cin >> n >> m >> d; n   struct event e1[m + 1]; n   e1[0].time1 = 0; n   for (long long int i = 1; i <= m; i++) { n     cin >> e1[i].pos >> e1[i].peakval >> e1[i].time1; n   } n   long long int dp[2][n + 1]; n   for (long long int i = 1; i <= n; i++) { n     dp[0][i] = 0; n   } n   deque<pair<long long int, long long int> > dq1; n   long long int delta, store; n   for (long long int i = 1; i <= m; i++) { n     delta = e1[i].time1 - (e1[i - 1].time1); n     delta *= d; n     for (long long int j = 1; j <= min(delta + 1, n); j++) { n       while (!dq1.empty() && dp[0][j] >= dq1.back().first) { n         dq1.pop_back(); n         if (dq1.empty()) { n           break; n         } n       } n       dq1.push_back({dp[0][j], j}); n     } n     for (long long int j = 1; j <= n; j++) { n       while (dq1.front().second < (j - delta)) { n         dq1.pop_front(); n       } n       store = e1[i].peakval - fabs(e1[i].pos - j); n       dp[1][j] = dq1.front().first + store; n       if ((j + delta + 1) <= n) { n         while (dp[0][j + delta + 1] >= dq1.back().first) { n           dq1.pop_back(); n           if (dq1.empty()) { n             break; n           } n         } n         dq1.push_back({dp[0][j + delta + 1], j + delta + 1}); n       } n     } n     for (long long int i = 1; i <= n; i++) { n       dp[0][i] = dp[1][i]; n     } n     dq1.clear(); n   } n   long long int ans = -1000000000000000007; n   for (long long int i = 1; i <= n; i++) { n     ans = max(ans, dp[1][i]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n class { n  public: n   int n, m; n   vector<pair<int, int> > V[5555]; n   bool vis[5555], in[5555]; n   int ans[5555]; n   void solve() { n     cin >> n >> m; n     for (int i = 0; i < m; ++i) { n       int u, v; n       cin >> u >> v; n       V[u].push_back({v, i}); n     } n     for (int i = 1; i <= n; ++i) { n       if (!vis[i]) dfs(i); n     } n     if (two) n       cout << 2 <<   n ; n     else n       cout << 1 <<   n ; n     for (int i = 0; i < m; ++i) { n       cout << ans[i] + 1 <<    ; n     } n   } n   bool two = 0; n   void dfs(int u) { n     in[u] = 1; n     vis[u] = 1; n     for (auto v : V[u]) { n       if (!vis[v.first]) { n         dfs(v.first); n       } else if (in[v.first]) { n         ans[v.second] = 1; n         two = 1; n       } n     } n     in[u] = 0; n   } n } NSPACE; n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(0); n   cin.tie(0); n   NSPACE.solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e9 + 7; n void solve() { n   long long n, curr, val, ans = 0; n   cin >> n >> curr; n   vector<long long> a(n), cost(n); n   for (long long i = 0; i < n; i++) cin >> a[i]; n   cin >> val; n   for (long long i = 0; i < n; i++) cin >> cost[i]; n   multiset<long long> s; n   for (long long i = 0; i < n; i++) { n     s.insert(cost[i]); n     if (curr < a[i]) { n       long long x = (a[i] - curr + val - 1) / val; n       if (x > s.size()) { n         ans = -1; n         break; n       } n       while (x--) { n         ans += *s.begin(); n         curr += val; n         s.erase(s.begin()); n       } n     } n   } n   cout << ans; n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); 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 int n; n string s; n vector<long long int> ss; n inline bool check(long long int lim) { n   long long int sum = 0, cnt = 0; n   for (long long int i = 0; i < ss.size(); i++) { n     if (sum + ss[i] <= lim) { n       sum += ss[i]; n     } else { n       cnt++; n       sum = ss[i]; n     } n   } n   cnt++; n   if (cnt <= n) return true; n   return false; n } n int main() { n   cin >> n; n   cin.ignore(); n   getline(cin, s); n   long long int pos = 0; n   for (long long int i = 0; i < s.length(); i++) { n     if (s[i] ==  -  || s[i] ==    ) { n       ss.push_back(i - pos + 1); n       pos = i + 1; n     } n   } n   ss.push_back(s.length() - pos); n   long long int low, high, mid; n   high = s.length(); n   low = -1; n   for (long long int i = 0; i < ss.size(); i++) low = max(low, ss[i]); n   long long int ans = high; n   while (low <= high) { n     mid = (low + high) / 2; n     if (check(mid)) { n       high = mid - 1; n       ans = min(ans, mid); n     } else n       low = mid + 1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int podch[1100]; n int newp[1100]; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, k, x; n   cin >> n >> k >> x; n   for (int i = 0; i < n; i++) { n     int tmp; n     cin >> tmp; n     podch[tmp]++; n   } n   for (int i = 0; i < k; i++) { n     int countwas = 0; n     for (int j = 0; j < 1100; j++) { n       newp[j] = 0; n     } n     for (int j = 0; j < 1100; j++) { n       int tmp = podch[j]; n       if (podch[j] % 2 != 0) { n         if (countwas % 2 == 0) { n           newp[(j ^ x)] += (podch[j] / 2 + 1); n           newp[j] -= (podch[j] / 2 + 1); n         } else { n           newp[(j ^ x)] += (podch[j] / 2); n           newp[j] -= (podch[j] / 2); n         } n       } else { n         newp[(j ^ x)] += (podch[j] / 2); n         newp[j] -= (podch[j] / 2); n       } n       countwas += tmp; n     } n     for (int j = 0; j < 1100; j++) { n       podch[j] += newp[j]; n     } n   } n   int kek = 0; n   for (int i = 0; i < 1100; i++) { n     if (podch[i]) { n       kek = i; n       break; n     } n   } n   int kek2 = 0; n   for (int i = 0; i < 1100; i++) { n     if (podch[i]) { n       kek2 = i; n     } n   } n   cout << kek2 <<     << kek << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n long long add(long long x, long long y, long long CMOD = MOD) { n   return (x + y + CMOD + CMOD) % CMOD; n } n long long mult(long long x, long long y, long long CMOD = MOD) { n   return add(x, 0) * add(y, 0) % CMOD; n } n long long fast_expo(long long x, long long y, long long CMOD = MOD) { n   if (x == 0) return 0; n   if (y == 0) return 1; n   long long ans = fast_expo(x, y / 2, CMOD); n   ans = mult(ans, ans, CMOD); n   if (y & 1) ans = mult(ans, x, CMOD); n   return ans; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n long long gen(long long x, long long y) { n   if (x == y) return x; n   return x + rng() % (y - x + 1); n } n const int TAM = 1e5 + 100; n const long long INF = LLONG_MAX / 4; n int n; n int main() { n   priority_queue<long long, vector<long long>, greater<long long> > pq; n   scanf( %d , &n); n   if (n % 2 == 0) pq.push(0); n   for (int i = 0; i < n; i++) { n     int x; n     scanf( %d , &x); n     pq.push(x); n   } n   long long ans = 0; n   while (pq.size() > 1) { n     long long x = 0; n     for (int i = 0; i < 3; i++) { n       x += pq.top(); n       pq.pop(); n     } n     ans += x; n     pq.push(x); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n]; n     int b[n]; n     vector<int> c; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     for (int i = 0; i < n; i++) { n       cin >> b[i]; n     } n     for (int i = 0; i < n; i++) { n       if (!b[i]) c.push_back(a[i]); n     } n     sort(c.rbegin(), c.rend()); n     int j = 0; n     for (int i = 0; i < n; i++) { n       if (b[i]) n         cout << a[i] <<    ; n       else n         cout << c[j++] <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char q[15][15], p[15][15], max_s[15], min_s[15], tmp[15], ans[15]; n int np[15]; n int main() { n   int i, j, k, n, carry, t, flag; n   while (scanf( %d %d , &n, &k) != EOF) { n     for (i = 0; i < n; ++i) scanf( %s , q + i); n     for (i = 0; i < k; ++i) { n       np[i] = i; n       ans[i] =  9 ; n     } n     ans[i] =   0 ; n     do { n       for (i = 0; i < k; ++i) { n         min_s[i] =  9 ; n         max_s[i] =  0 ; n       } n       min_s[i] =   0 ; n       max_s[i] =   0 ; n       for (i = 0; i < n; ++i) { n         for (j = 0; j < k; ++j) { n           p[i][j] = q[i][np[j]]; n         } n         p[i][j] =   0 ; n         if (strcmp(p[i], min_s) < 0) strcpy(min_s, p[i]); n         if (strcmp(p[i], max_s) > 0) strcpy(max_s, p[i]); n       } n       carry = 0; n       for (i = k - 1; i >= 0; --i) { n         t = max_s[i] - min_s[i] - carry; n         if (t < 0) { n           carry = 1; n           t += 10; n         } else n           carry = 0; n         tmp[i] = t +  0 ; n       } n       tmp[k] =   0 ; n       if (strcmp(tmp, ans) < 0) strcpy(ans, tmp); n     } while (next_permutation(np, np + k)); n     flag = 0; n     for (i = 0; i < k; ++i) { n       if (ans[i] ==  0 ) { n         if (flag != 0) printf( %c , ans[i]); n       } else { n         if (flag == 0) flag = 1; n         printf( %c , ans[i]); n       } n     } n     if (flag == 0) n       printf( 0 n ); n     else n       printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 100001; n int i, n, m, g[N], v[N], b[N], res[N], c[N], a; n int main() { n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; i++) scanf( %d , &g[i]); n   for (i = 1; i <= m; i++) scanf( %d , &v[i]); n   for (i = 1; i <= m; i++) scanf( %d , &b[i]); n   for (i = 1; i <= m; i++) { n     for (a = (v[i] + res[i - 1] - 1) % n + 1; b[i] > c[a]; a = g[a]) { n       if (c[a] == 0) res[i]++; n       c[a] = b[i]; n       b[i]--; n     } n     printf( %d n , res[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct var { n   char name[20], type[20]; n } v[1010]; n int vcnt; n struct func { n   char name[20]; n   char para[50][20]; n   int paracnt; n } f[1010]; n int fcnt; n int n; n char str[110], name[110], para[110][110], cnt; n int main() { n   scanf( %d , &fcnt); n   gets(str); n   for (int i = 1; i <= fcnt; ++i) { n     gets(str); n     int j = 0, l = 0, len = strlen(str); n     while (str[j] !=  d ) ++j; n     ++j; n     while (!isalpha(str[j]) && !isdigit(str[j])) ++j; n     while (isalpha(str[j]) || isdigit(str[j])) f[i].name[l++] = str[j++]; n     f[i].name[l] =   0 ; n     j++; n     while (!isalpha(str[j]) && !isdigit(str[j]) && (j < len)) ++j; n     while (j < len) { n       l = 0; n       f[i].paracnt++; n       while (isalpha(str[j]) || isdigit(str[j])) n         f[i].para[f[i].paracnt][l++] = str[j++]; n       f[i].para[f[i].paracnt][l] =   0 ; n       while (!isalpha(str[j]) && !isdigit(str[j]) && (j < len)) ++j; n     } n   } n   scanf( %d , &vcnt); n   gets(str); n   for (int i = 1; i <= vcnt; ++i) { n     gets(str); n     int j = 0, len = strlen(str), l = 0; n     while (!isalpha(str[j]) && !isdigit(str[j])) ++j; n     while (isdigit(str[j]) || isalpha(str[j])) v[i].type[l++] = str[j++]; n     v[i].type[l] =   0 ; n     while (!isalpha(str[j]) && !isdigit(str[j])) ++j; n     l = 0; n     while (isdigit(str[j]) || isalpha(str[j])) v[i].name[l++] = str[j++]; n     v[i].name[l] =   0 ; n   } n   scanf( %d , &n); n   gets(str); n   while (n--) { n     gets(str); n     int j = 0, l = 0, len = strlen(str); n     while (!isalpha(str[j]) && !isdigit(str[j])) ++j; n     while (isalpha(str[j]) || isdigit(str[j])) name[l++] = str[j++]; n     name[l] =   0 ; n     j++; n     cnt = 0; n     while (!isalpha(str[j]) && !isdigit(str[j]) && (j < len)) ++j; n     while (j < len) { n       l = 0; n       cnt++; n       while (isalpha(str[j]) || isdigit(str[j])) para[cnt][l++] = str[j++]; n       para[cnt][l] =   0 ; n       while (!isalpha(str[j]) && !isdigit(str[j]) && (j < len)) ++j; n     } n     bool flag = true; n     for (int i = 1; i <= cnt; ++i) { n       bool find = false; n       for (int j = 1; j <= vcnt; ++j) n         if (!strcmp(para[i], v[j].name)) { n           strcpy(para[i], v[j].type); n           find = true; n           break; n         } n       if (!find) { n         flag = false; n         break; n       } n     } n     if (!flag) { n       printf( 0 n ); n       continue; n     } n     int res = 0; n     for (int i = 1; i <= fcnt; ++i) n       if ((strcmp(f[i].name, name) == 0) && (cnt == f[i].paracnt)) { n         bool okay = true; n         for (int j = 1; j <= cnt; ++j) n           if (strcmp(f[i].para[j],  T ) && strcmp(f[i].para[j], para[j])) { n             okay = false; n             break; n           } n         res += okay; n       } n     printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int nxt[500005], to[500005], head[500005], fa[500005], f[500005][22], n     dep[500005], ru[500005], tot; n bool vis[500005], used[500005]; n queue<int> q; n vector<int> ei[500005]; n void add(int u, int v) { n   nxt[++tot] = head[u]; n   head[u] = tot; n   to[tot] = v; n   nxt[++tot] = head[v]; n   head[v] = tot; n   to[tot] = u; n } n void dfs(int u, int p) { n   dep[u] = dep[p] + 1; n   f[u][0] = p; n   for (int i = 1; i <= 20; i++) f[u][i] = f[f[u][i - 1]][i - 1]; n   for (int e = head[u]; e; e = nxt[e]) { n     int v = to[e]; n     if (v == p) continue; n     dfs(v, u); n   } n } n int lca(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   for (int i = 20; i >= 0; i--) { n     if (dep[f[x][i]] >= dep[y]) { n       x = f[x][i]; n     } n   } n   if (x == y) return x; n   for (int i = 20; i >= 0; i--) { n     if (f[x][i] != f[y][i]) { n       x = f[x][i]; n       y = f[y][i]; n     } n   } n   return f[x][0]; n } n int find(int x) { n   if (x == fa[x]) return x; n   return fa[x] = find(fa[x]); n } n struct sad { n   int x, y; n } a[500005]; n int main() { n   int n, x, y, u, v; n   cin >> n; n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= n - 1; i++) { n     scanf( %d%d , &x, &y); n     add(x, y); n   } n   for (int i = 1; i <= n - 1; i++) { n     scanf( %d%d , &a[i].x, &a[i].y); n     ru[a[i].x]++; n     ru[a[i].y]++; n     ei[a[i].x].push_back(i); n     ei[a[i].y].push_back(i); n   } n   dfs(1, 0); n   for (int i = 1; i <= n - 1; i++) { n     if (ru[a[i].x] == 1 || ru[a[i].y] == 1) { n       used[i] = 1; n       q.push(i); n     } n   } n   int ans = 0; n   cout << n - 1 << endl; n   while (!q.empty()) { n     int eg = q.front(); n     q.pop(); n     u = a[eg].x; n     v = a[eg].y; n     if (ru[u] != 1) swap(u, v); n     int z = lca(u, v); n     int p = find(u); n     if (dep[p] > dep[z]) { n       ans++; n       fa[p] = find(f[p][0]); n       printf( %d %d %d %d n , p, f[p][0], a[eg].x, a[eg].y); n     } else { n       int t = v; n       for (int i = 20; i >= 0; i--) n         if (dep[f[t][i]] > dep[z] && find(f[t][i]) != p) t = f[t][i]; n       fa[t] = find(f[t][0]); n       printf( %d %d %d %d n , t, f[t][0], a[eg].x, a[eg].y); n     } n     ru[u]--; n     ru[v]--; n     if (ru[v] == 1) { n       int mm = ei[v].size(); n       for (int i = 0; i < mm; i++) { n         int ey = ei[v][i]; n         if (used[ey] == 0) q.push(ey), used[ey] = 1; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 0x3f3f3f3f; n const long long MAXN = 0x7fffffff; n const long long INF = 0x3f3f3f3f3f3f3f3fLL; n void file() {} n void pr(bool num) { cout << (num == 1 ?  YES  :  NO ) <<   n ; } n signed main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   ; n   long long t, n; n   cin >> t; n   while (t--) { n     long long a, b, c; n     cin >> a >> b >> c; n     long long ans = min(a, b); n     ans = min(ans, c); n     a -= ans; n     b -= ans; n     c -= ans; n     long long num = min(a, b); n     a -= num; n     b -= num; n     long long sum = a + b + c; n     if (num <= sum) n       ans += num; n     else { n       ans += sum; n       num -= sum; n       ans += num / 3 * 2; n       if (num % 3 == 2) ans++; n     } n     cout << ans <<   n ; n   } n   return 0; n } n