#include <bits/stdc++.h> n using namespace std; n string a; n int main() { n   cin >> a; n   cout << a; n   reverse(a.begin(), a.end()); n   cout << a << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100 + 7; n int n, k; n string second; n string s2; n string s1; n int main() { n   cin >> n >> k; n   cin >> second; n   sort(second.begin(), second.end()); n   for (int i = 0; i < n; i++) { n     if (second[i] != second[i + 1]) { n       s2 += second[i]; n     } n   } n   second =   ; n   second += s2; n   sort(second.begin(), second.end()); n   for (int i = 0; i < second.size(); i++) { n     s1 += second[i]; n     if (second[i] + 1 == second[i + 1] || second[i] == second[i + 1]) { n       i++; n     } n   } n   if (s1.size() >= k) { n     int cnt = 0; n     for (int i = 0; i < k; i++) { n       cnt += s1[i] -  a  + 1; n     } n     cout << cnt; n   } else { n     cout << -1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 207; n int n, f[MAXN]; n long long mmc_atual = 1; n vector<int> folhas, Gr[MAXN]; n bool in_cycle[MAXN], mrk[MAXN]; n int marca_ciclo(int u) { n   if (in_cycle[u]) return 0; n   in_cycle[u] = true; n   return 1 + marca_ciclo(f[u]); n } n void dfsr(int u) { n   mrk[u] = true; n   bool flag = true; n   for (int i = 0; i < (int)Gr[u].size(); i++) { n     int viz = Gr[u][i]; n     flag = false; n     if (mrk[viz]) continue; n     dfsr(viz); n   } n   if (flag) folhas.push_back(u); n } n int dfs(int u) { n   if (in_cycle[u]) return 0; n   return 1 + dfs(f[u]); n } n long long mdc(long long a, long long b) { return (a == 0) ? b : mdc(b % a, a); } n long long mmc(long long a, long long b) { return (a / mdc(a, b)) * b; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> f[i]; n     Gr[f[i]].push_back(i); n   } n   long long ans = 1; n   long long mx_chain = 0; n   for (int i = 1; i <= n; i++) { n     if (!mrk[i]) { n       int turtoise = i; n       int hare = i; n       do { n         turtoise = f[turtoise]; n         hare = f[f[hare]]; n       } while (turtoise != hare); n       int tam_ciclo = marca_ciclo(hare); n       mmc_atual = mmc(mmc_atual, (long long)tam_ciclo); n       dfsr(hare); n     } n   } n   for (int i = 0; i < (int)folhas.size(); i++) { n     int tam_chain = dfs(folhas[i]); n     mx_chain = max(mx_chain, (long long)tam_chain); n   } n   if (mx_chain % mmc_atual != 0) n     mx_chain = (mx_chain - mx_chain % mmc_atual) + mmc_atual; n   ans = max(mmc_atual, mx_chain); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n using ll = long long; n using db = long double; n using ii = pair<int, int>; n const int N = 1e5 + 5, LG = 17, MOD = 998244353; n const int SQ = 225; n const long double EPS = 1e-7; n int n, m, a[N], b[N]; n set<int> pos[N]; n bool vis[N]; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i < n + 1; i++) cin >> a[i]; n   cin >> m; n   for (int i = 1; i < m + 1; i++) cin >> b[i]; n   sort(b + 1, b + m + 1); n   vector<int> lis; n   for (int i = 1; i < n + 1; i++) { n     if (a[i] == -1) { n       int p = lower_bound(lis.begin(), lis.end(), b[m]) - lis.begin(); n       if (p == lis.size()) lis.push_back(INT_MAX); n       for (int j = m; j; --j) { n         while (p > 0 && b[j] <= lis[p - 1]) --p; n         lis[p] = min(lis[p], b[j]); n       } n     } else { n       int p = lower_bound(lis.begin(), lis.end(), a[i]) - lis.begin(); n       if (p == lis.size()) lis.push_back(0); n       lis[p] = a[i]; n       pos[p].insert(i); n     } n   } n   int idx = n, prv = INT_MAX, ptr = m; n   for (int i = lis.size() - 1; i >= 0; --i) { n     auto it = pos[i].upper_bound(idx); n     if (it != pos[i].begin()) { n       --it; n       if (a[*it] < prv) { n         idx = *it; n         prv = a[idx]; n         continue; n       } n     } n     while (~a[idx]) --idx; n     while (b[ptr] >= prv) --ptr; n     prv = a[idx] = b[ptr]; n     vis[ptr] = true; n   } n   int tmp = 1; n   for (int i = 1; i < n + 1; i++) { n     while (tmp <= m && vis[tmp]) tmp++; n     if (a[i] == -1) a[i] = b[tmp++]; n     cout << a[i] <<    n [i == n]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200010, M = 7000010; n int n, a[N], rt, tot; n map<int, int> mp; n int val[M]; n vector<int> g[M]; n int ans, ansx, ansy, S, T, cur, fa[M]; n bool vis[M]; n void dfs(int x, int fath, int dis, int *var) { n   vis[x] = true; n   if (x <= n && dis > cur) *var = x, cur = dis; n   for (int y : g[x]) n     if (y != fath) dfs(y, x, dis + 1, var); n } n int getfa(int v) { n   int t = 0, x = mp[v]; n   if (fa[x] != -1) return fa[x]; n   while ((1 << t) < v) t++; n   int w = (1 << t) - v; n   if (mp[w] == 0) tot++, mp[w] = tot, val[tot] = w; n   g[x].push_back(mp[w]), g[mp[w]].push_back(x); n   return fa[x] = w; n } n int main() { n   cin >> n, tot = n; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), mp[a[i]] = i, val[i] = a[i]; n   sort(a + 1, a + n + 1), rt = a[1]; n   for (int i = 1; i <= n * 32; i++) fa[i] = -1; n   for (int i = 2; i <= n; i++) { n     int x = a[i], y = rt; n     while (x != y) { n       if (x < y) swap(x, y); n       x = getfa(x); n     } n     rt = x; n   } n   ans = -1; n   for (int i = 1; i <= n; i++) n     if (!vis[i]) { n       cur = 0, dfs(i, 0, 0, &S); n       cur = 0, dfs(S, 0, 0, &T); n       if (cur > ans) ans = cur, ansx = S, ansy = T; n     } n   printf( %d %d %d n , ansx, ansy, cur); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   while (n) { n     if (n % 10 != 1 && n % 100 != 14 && n % 1000 != 144) { n       cout <<  NO ; n       return 0; n     } n     n /= 10; n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n char s[200005]; n int res[200005]; n int ptr; n int main() { n   int n, a, b, k; n   scanf( %d%d%d%d , &n, &a, &b, &k); n   scanf( %s , s); n   int cons = 0; n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  0 ) cons++; n     if (s[i] ==  1 ) { n       cnt += cons / b; n       cons = 0; n     } n   } n   cnt += cons / b; n   cons = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  0 ) n       cons++; n     else n       cons = 0; n     if (cons == b && cnt > a - 1) { n       res[ptr++] = i + 1; n       cons = 0; n       cnt--; n     } n   } n   printf( %d n , ptr); n   for (int i = 0; i < ptr; i++) printf( %d  , res[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int prefix[N]; n int main() { n   string s; n   int n, k; n   cin >> n >> k >> s; n   for (int i = int(1); i <= int(n); i++) n     prefix[i] = prefix[i - 1] +  1  - s[i - 1]; n   int ans = n + 1; n   for (int i = int(1); i <= int(n); i++) { n     if (s[i - 1] ==  0 ) { n       int l = 0, r = n - 1; n       while (l + 1 < r) { n         int mid = (l + r) >> 1; n         int L = i - mid, R = i + mid; n         L = max(1, L), R = min(n, R); n         if (prefix[R] - prefix[L - 1] > k) n           r = mid; n         else n           l = mid; n       } n       ans = min(ans, r); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, c = 0; n     cin >> n; n     int w = 1; n     int a[n]; n     for (int i = 0; i < n; i++) cin >> a[i]; n     if (n == 1) n       cout <<  First  << endl; n     else { n       for (int i = 0; i < n; i++) { n         if (a[i] == 1) c++; n         if (a[i] == 1 && w == 1) n           w = 2; n         else if (a[i] == 1 && w == 2) n           w = 1; n         if (a[i] != 1) break; n       } n       if (c == n && c % 2 == 1) n         cout <<  First  << endl; n       else if (c == n && c % 2 == 0) n         cout <<  Second  << endl; n       else if (w == 1 && c != n) n         cout <<  First  << endl; n       else if (w == 2 && c != n) n         cout <<  Second  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 1e3; n int adjm[sz + 1][sz + 1]; n vector<int> adjl[(int)(1e6 + 1)]; n vector<bool> vis((int)1e6 + 1, 0); n void init_adjm(int n) { n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= n; j++) adjm[i][j] = 0; n   } n } n void bfs(int s, int n, int m) { n   queue<int> q; n   q.push(s); n   q.push(-1); n   vis[s] = 1; n   while (!q.empty()) { n     int p = q.front(); n     q.pop(); n     if (p == -1) { n       if (!q.empty()) { n         q.push(-1); n       } n     } else { n       for (int i = 0; i < adjl[p].size(); i++) { n         int curr{adjl[p][i]}; n         if (!vis[curr]) { n           q.push(curr); n           vis[curr] = 1; n         } n       } n     } n   } n } n void dfs(int s) { n   vis[s] = 1; n   for (int i{0}; i < adjl[s].size(); i++) { n     if (!vis[adjl[s][i]]) { n       dfs(adjl[s][i]); n     } n   } n } n vector<bool> is_prime(1e6 + 1, true); n long long gcd(long long x, long long y) { n   if (x == 0) return y; n   return gcd(y % x, x); n } n void sievePrime(long long n) { n   for (long long p{2}; p * p <= n; p++) { n     if (is_prime[p]) { n       for (long long i{p * p}; i <= n; i += p) { n         is_prime[i] = false; n       } n     } n   } n } n vector<int> SPF((int)1e6 + 1); n vector<long long> prime; n void manipulated_seive(int N) { n   is_prime[0] = is_prime[1] = false; n   for (int i = 2; i < N; i++) { n     if (is_prime[i]) { n       prime.push_back(i); n       SPF[i] = i; n     } n     for (int j{0}; n          j < (int)prime.size() && i * prime[j] < N && prime[j] <= SPF[i]; j++) { n       is_prime[i * prime[j]] = false; n       SPF[i * prime[j]] = prime[j]; n     } n   } n } n class my_compare { n  public: n   bool operator()(pair<int, int> &p1, pair<int, int> &p2) { n     return p1.second < p2.second; n   } n }; n long long fact(int n) { n   long long res{1}; n   for (int i = 2; i <= n; i++) res *= i; n   return res; n } n long long nPr(int n, int r) { n   long long num{1}, denom{1}; n   for (int i = 0; i < r; i++) { n     denom *= (i + 1); n     num *= (n - i); n   } n   return num / denom; n } n long long nCr(int n, int r) { n   long long num{1}, denom{1}; n   for (int i = 0; i < r; i++) { n     denom = (denom * (i + 1)); n     num = (num * (n - i)); n   } n   return num / denom; n } n unsigned long long power(unsigned long long x, int y, int p) { n   unsigned long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n unsigned long long modInverse(unsigned long long n, int p) { n   return power(n, p - 2, p); n } n unsigned long long nCrModPFermat(unsigned long long n, int r, int p) { n   if (n < r) return 0; n   if (r == 0) return 1; n   unsigned long long fac[n + 1]; n   fac[0] = 1; n   for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % p; n   return (fac[n] * (modInverse(fac[r], p) % p) * n           (modInverse(fac[n - r], p) % p)) % n          p; n } n void solve() { n   int n, m, x; n   cin >> n >> m; n   multiset<int, greater<int>> reqd; n   for (int i = 0; i < n; i++) { n     cin >> x; n     reqd.insert(x); n   } n   for (int i = 0; i < m; i++) { n     cin >> x; n     auto it = reqd.lower_bound(x); n     if (it != reqd.end()) { n       reqd.erase(it); n     } n   } n   cout << reqd.size() <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   bool test_case{0}, fileop{0}; n   if (fileop) { n     freopen( input.txt ,  r , stdin); n     freopen( output.txt ,  w , stdout); n   } n   int t; n   if (test_case) { n     cin >> t; n     while (t--) { n       solve(); n     } n   } else n     solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, i, j, m, p, a[510]; n double b[710000]; n double calc(int x, int y, double a) { return (y - x) * a + x; } n int main() { n   scanf( %d%d , &n, &k); n   m = 700000 / k; n   for (i = 1; i <= n; i++) { n     double ans = 0; n     for (j = 0; j <= k; j++) scanf( %d , &a[j]); n     for (j = 0; j < k; j++) { n       for (p = 0; p < m; p++) { n         double s = calc(a[j], a[j + 1], 1.0 / m * (p + 0.5)); n         if (s > b[j * m + p]) ans += s - b[j * m + p], b[j * m + p] = s; n       } n     } n     printf( %.10lf n , ans / m); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int x[100], y[100]; n int l1, l2, l3, l4, s, f[10], n; n int len(int g1, int g2) { n   int res = n       (((x[g1] - x[g2]) * (x[g1] - x[g2]) + (y[g1] - y[g2]) * (y[g1] - y[g2]))); n   return res; n } n bool prr(int kk1, int kk2, int kk3, int kk4) { n   int ii, jj = 0, aa[4], kk, qq; n   for (ii = 0; ii < 8; ii++) n     if (ii != kk1 && ii != kk2 && ii != kk3 && ii != kk4) { n       aa[jj++] = ii; n     } n   for (ii = 0; ii < 4; ii++) n     for (jj = 0; jj < 4; jj++) n       for (kk = 0; kk < 4; kk++) n         for (qq = 0; qq < 4; qq++) n           if (ii != jj && ii != kk && ii != qq && jj != kk && jj != qq && n               kk != qq) n             if (len(aa[ii], aa[jj]) == len(aa[kk], aa[qq]) && n                 len(aa[ii], aa[jj]) == n                     len(aa[ii], aa[kk]) + len(aa[ii], aa[qq]) && n                 len(aa[qq], aa[kk]) == n                     len(aa[ii], aa[kk]) + len(aa[jj], aa[kk]) && n                 len(aa[ii], aa[qq]) == len(aa[kk], aa[jj]) && n                 len(aa[ii], aa[kk]) == len(aa[jj], aa[qq])) n               return true; n   return false; n } n bool kv(int q1, int q2, int q3, int q4) { n   int i, a[4], j, k, q; n   a[0] = q1; n   a[1] = q2; n   a[2] = q3; n   a[3] = q4; n   for (i = 0; i < 4; i++) n     for (j = 0; j < 4; j++) n       for (k = 0; k < 4; k++) n         for (q = 0; q < 4; q++) n           if (i != j && j != k && i != k && i != q && j != q && k != q) { n             if (len(a[i], a[j]) == len(a[k], a[q]) && n                 len(a[i], a[j]) == len(a[i], a[q]) + len(a[i], a[k]) && n                 len(a[i], a[k]) == len(a[i], a[q]) && n                 len(a[k], a[q]) == len(a[k], a[i]) + len(a[k], a[j]) && n                 len(a[q], a[i]) == len(a[q], a[j])) { n               if (prr(q1, q2, q3, q4)) return true; n             } n           } n   return false; n } n int main() { n   for (l1 = 0; l1 < 8; l1++) scanf( %d %d , &x[l1], &y[l1]); n   for (l1 = 0; l1 < 8; l1++) n     for (l2 = 0; l2 < 8; l2++) n       for (l3 = 0; l3 < 8; l3++) n         for (l4 = 0; l4 < 8; l4++) n           if (l1 != l2 && l1 != l3 && l1 != l4 && l2 != l3 && l2 != l4 && n               l3 != l4 && kv(l1, l2, l3, l4)) { n             printf( YES n ); n             printf( %d %d %d %d n , l1 + 1, l2 + 1, l3 + 1, l4 + 1); n             for (s = 0; s < 8; s++) n               if (s != l1 && s != l2 && s != l3 && s != l4) f[n++] = s; n             for (s = 0; s < 3; s++) printf( %d  , f[s] + 1); n             printf( %d n , f[3] + 1); n             return 0; n           } n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p; n struct Node { n   int i, d; n } d[100005]; n vector<int> v[100005 << 1]; n bool cmp(Node x, Node y) { return x.d > y.d; } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &d[i].d); n     d[i].i = 2 * i; n   } n   sort(d + 1, d + n + 1, cmp); n   for (int i = 1; i <= n; i++) { n     v[p++].push_back(d[i].i); n   } n   for (int i = 1; i <= n; i++) { n     if (i + d[i].d - 1 == p) { n       v[p++].push_back(d[i].i - 1); n     } else { n       v[i + d[i].d - 2].push_back(d[i].i - 1); n     } n   } n   for (int i = 0; i < p; i++) { n     for (int j = 1; j < v[i].size(); j++) { n       printf( %d %d n , v[i][0], v[i][j]); n     } n   } n   for (int i = 1; i < p; i++) { n     printf( %d %d n , v[i - 1][0], v[i][0]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000000 + 7; n const long long int N = 10000000 + 6; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int a, b; n   cin >> a >> b; n   vector<pair<int, int> > bb, aa; n   for (int i = 1; i < 1000; i++) { n     for (int j = 1; j < 1000; j++) { n       if (i * i + j * j == a * a) aa.push_back(make_pair(i, j)); n       if (i * i + j * j == b * b) bb.push_back(make_pair(i, j)); n     } n   } n   bool flag = false; n   for (int i = 0; i < aa.size(); i++) { n     for (int j = 0; j < bb.size(); j++) { n       int p = aa[i].first; n       int q = aa[i].second; n       int r = bb[j].first; n       int s = bb[j].second; n       if (p * s == q * r) { n         if (p != s) { n           cout <<  YES n  n                <<  0 0 n  n                << p <<     << q <<   n  n                << s <<     << -r; n           flag = true; n           break; n         } n       } n     } n     if (flag == true) break; n   } n   if (flag == false) cout <<  NO n  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Info { n   int h, l, r; n   vector<int> A; n   Info(vector<int> _A, int _h, int _l, int _r) : A(_A), h(_h), l(_l), r(_r) {} n }; n int FindMaxGoodSleep(int idx, int time, vector<vector<int>>& memo, n                      const Info& input) { n   if (idx >= input.A.size()) { n     return 0; n   } n   if (memo[idx][time] != -1) { n     return memo[idx][time]; n   } n   int new_time = (time + input.A[idx]) % input.h; n   int a = (new_time >= input.l && new_time <= input.r ? 1 : 0) + n           FindMaxGoodSleep(idx + 1, new_time, memo, input); n   new_time = (time + input.A[idx] - 1) % input.h; n   int b = (new_time >= input.l && new_time <= input.r ? 1 : 0) + n           FindMaxGoodSleep(idx + 1, new_time, memo, input); n   memo[idx][time] = max(a, b); n   return memo[idx][time]; n } n int main(void) { n   int n, h, l, r; n   cin >> n >> h >> l >> r; n   vector<int> A(n); n   for (int i = 0; i < n; i++) { n     cin >> A[i]; n   } n   Info info(A, h, l, r); n   vector<vector<int>> memo(n + 5, vector<int>(2005, -1)); n   cout << FindMaxGoodSleep(0, 0, memo, info) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> G[100009]; n map<string, int> pr; n struct comp { n   bool operator()(const pair<string, int>& l, const pair<string, int>& r) { n     return l.second > r.second; n   } n }; n int main() { n   pr[ captain ] = 1; n   pr[ man ] = 2; n   pr[ child ] = 3; n   pr[ woman ] = 3; n   pr[ rat ] = 4; n   int n; n   cin >> n; n   getchar(); n   vector<pair<string, int> > v; n   while (n--) { n     string a, b; n     cin >> a >> b; n     v.push_back({a, pr[b]}); n   } n   stable_sort(v.begin(), v.end(), comp()); n   for (int i = 0; i < v.size(); i++) cout << v[i].first << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> split(const string& s, char c) { n   vector<string> v; n   stringstream ss(s); n   string x; n   while (getline(ss, x, c)) v.emplace_back(x); n   return move(v); n } n void err(vector<string>::iterator it) {} n template <typename T, typename... Args> n void err(vector<string>::iterator it, T a, Args... args) { n   cerr << it->substr((*it)[0] ==    , it->length()) <<   =   << a <<   n ; n   err(++it, args...); n } n typedef tuple<int, int, int> State; n int dx[] = {0, 1, 0, -1}; n int dy[] = {1, 0, -1, 0}; n const int N = 1e6 + 5; n const double EPS = 1e-10; n const long long INF = 1e18; n const double PI = acos(-1); n int n, m, k; n int dcmp(double a, double b) { return fabs(a - b) <= EPS ? 0 : a < b ? -1 : 1; } n string s, s2; n int arr[26]; n int arr2[26]; n int cnt = 0; n bool ok() { n   for (int i = 0; i < 26; i++) { n     if (arr[i] > arr2[i]) return 0; n   } n   return 1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> s >> s2; n   if (s.size() < s2.size()) { n     cout <<  0 ; n     return 0; n   } n   for (int i = 0; i < s2.size(); i++) { n     arr2[s2[i] -  a ]++; n     if (s[i] !=  ? ) n       arr[s[i] -  a ]++; n     else n       cnt++; n   } n   int ans = ok(); n   n = s2.size(); n   for (int i = s2.size(); i < s.size(); i++) { n     if (s[i] !=  ? ) n       arr[s[i] -  a ]++; n     else n       cnt++; n     if (s[i - n] !=  ? ) n       arr[s[i - n] -  a ]--; n     else n       cnt--; n     ans += ok(); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e5 + 123; n const long long INF = (long long)1e18 + 123; n const int inf = (int)1e9 + 123; n const int MOD = (int)1e9 + 7; n int type(string a) { n   if (a ==  S ) return 1; n   if (a ==  M ) return 2; n   if (a ==  L ) return 3; n   if (a ==  XL ) return 4; n   if (a ==  XXL ) return 5; n   if (a ==  XXXL ) return 6; n   return 7; n } n int a[10], n; n string t, name[10] = {  ,  S ,  M ,  L ,  XL ,  XXL ,  XXXL }, res[N]; n set<int> st[10]; n vector<pair<int, pair<int, int> > > g; n int main() { n   for (int i = 1; i <= 6; i++) cin >> a[i]; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> t; n     string tmp1 =   , tmp2 =   ; n     int pos = (int)(t.size()); n     for (int j = 0; j < (int)(t.size()); j++) { n       if (t[j] ==  , ) { n         pos = j + 1; n         break; n       } n       tmp1 += t[j]; n     } n     for (int j = pos; j < (int)(t.size()); j++) tmp2 += t[j]; n     if (type(tmp2) < type(tmp1)) swap(tmp1, tmp2); n     if (type(tmp2) == 7) { n       res[i] = tmp1, a[type(tmp1)]--; n       continue; n     } n     g.push_back(make_pair(type(tmp1), make_pair(type(tmp2), i))); n   } n   sort(g.begin(), g.end()); n   for (auto i : g) { n     if (a[i.first] > 0) n       a[i.first]--, res[i.second.second] = name[i.first]; n     else n       a[i.second.first]--, res[i.second.second] = name[i.second.first]; n   } n   for (int i = 1; i <= 6; i++) n     if (a[i] < 0) { n       puts( NO ); n       return 0; n     } n   puts( YES ); n   for (int i = 1; i <= n; i++) cout << res[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int t, n, inp[3], cnt[100010]; n map<pair<int, int>, vector<pair<int, int>>> el; n int root; n vector<int> qans; n vector<int> neigh[100010]; n void solve() { n   cin >> n; n   el.clear(); n   for (int i = 1; i <= n; ++i) cnt[i] = 0, neigh[i].clear(); n   for (int i = 1; i + 2 <= n; ++i) { n     for (int j = 0; j < 3; ++j) { n       cin >> inp[j]; n       ++cnt[inp[j]]; n     } n     sort(inp, inp + 3); n     if (el.count({inp[0], inp[1]})) n       el[{inp[0], inp[1]}].push_back({inp[2], i}); n     else n       el[{inp[0], inp[1]}] = {{inp[2], i}}; n     if (el.count({inp[0], inp[2]})) n       el[{inp[0], inp[2]}].push_back({inp[1], i}); n     else n       el[{inp[0], inp[2]}] = {{inp[1], i}}; n     if (el.count({inp[1], inp[2]})) n       el[{inp[1], inp[2]}].push_back({inp[0], i}); n     else n       el[{inp[1], inp[2]}] = {{inp[0], i}}; n   } n   for (auto& x : el) { n     if (x.second.size() == 1) { n       neigh[x.first.first].push_back(x.first.second); n       neigh[x.first.second].push_back(x.first.first); n     } n   } n   cout << 1 <<     << neigh[1][0]; n   int curr = neigh[1][0], prev = 1; n   for (int i = 3; i <= n; ++i) { n     if (neigh[curr][0] == prev) { n       cout <<     << neigh[curr][1]; n       prev = curr; n       curr = neigh[curr][1]; n     } else { n       cout <<     << neigh[curr][0]; n       prev = curr; n       curr = neigh[curr][0]; n     } n   } n   cout << endl; n   for (int i = 1; i <= n; ++i) n     if (cnt[i] == 1) { n       root = i; n       break; n     } n   pair<int, int> ce = {neigh[root][0], neigh[root][1]}; n   if (ce.first > ce.second) swap(ce.first, ce.second); n   for (auto& x : el[ce]) { n     if (x.first == root) { n       qans.push_back(x.second); n     } n   } n   queue<pair<pair<int, int>, int>> qq; n   qq.push({ce, root}); n   while (!qq.empty()) { n     auto x = qq.front(); n     qq.pop(); n     if (el[x.first].size() == 1) { n       continue; n     } n     pair<int, int> y = n         (el[x.first][0].first == x.second ? el[x.first][1] : el[x.first][0]); n     qans.push_back(y.second); n     pair<int, int> nxt1 = {x.first.first, y.first}, n                    nxt2 = {x.first.second, y.first}; n     if (nxt1.first > nxt1.second) swap(nxt1.first, nxt1.second); n     if (nxt2.first > nxt2.second) swap(nxt2.first, nxt2.second); n     qq.push({nxt1, x.first.second}); n     qq.push({nxt2, x.first.first}); n   } n   while (!qans.empty()) { n     cout << qans.back() <<    ; n     qans.pop_back(); n   } n   cout << endl; n } n int main() { n   ios::sync_with_stdio(false); n   for (cin >> t; t--;) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline 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 <class T> n inline 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 <class T> n inline T sqr(T x) { n   return x * x; n } n template <class T> n T power(T N, T P) { n   return (P == 0) ? 1 : N * power(N, P - 1); n } n template <class T> n bool inside(T a, T b, T c) { n   return (b >= a && b <= c); n } n const long long INF64 = (long long)1E16; n int distsq2d(int x1, int y1, int x2, int y2) { n   return sqr(x1 - x2) + sqr(y1 - y2); n } n double dist2d(double x1, double y1, double x2, double y2) { n   return sqrt(sqr(x1 - x2) + sqr(y1 - y2)); n } n double dist3d(double x1, double y1, double z1, double x2, double y2, n               double z2) { n   return sqrt(sqr(x1 - x2) + sqr(y1 - y2) + sqr(z1 - z2)); n } n long long toInt64(string s) { n   long long r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n double LOG(long long N, long long B) { return (log10l(N)) / (log10l(B)); } n string itoa(long long a) { n   if (a == 0) return  0 ; n   string ret; n   for (long long i = a; i > 0; i = i / 10) ret.push_back((i % 10) + 48); n   reverse(ret.begin(), ret.end()); n   return ret; n } n vector<string> token(string a, string b) { n   const char *q = a.c_str(); n   while (count(b.begin(), b.end(), *q)) q++; n   vector<string> oot; n   while (*q) { n     const char *e = q; n     while (*e && !count(b.begin(), b.end(), *e)) e++; n     oot.push_back(string(q, e)); n     q = e; n     while (count(b.begin(), b.end(), *q)) q++; n   } n   return oot; n } n int isvowel(char s) { n   s = tolower(s); n   if (s ==  a  || s ==  e  || s ==  i  || s ==  o  || s ==  u ) return 1; n   return 0; n } n int isupper(char s) { n   if (s >=  A  and s <=  Z ) return 1; n   return 0; 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 int check(int N, int pos) { return (N & (1 << pos)); } n int toggle(int N, int pos) { n   if (check(N, pos)) return N = reset(N, pos); n   return N = Set(N, pos); n } n void pbit(int N) { n   printf( ( ); n   for (int i = 10; i >= 0; i--) { n     bool x = check(N, i); n     cout << x; n   } n   puts( ) ); n } n char ts[200]; n int n, m; n int a[14][104]; n int b[14][104]; n int c[14][104]; n int dp[14][14][104][104]; n int call(int p1, int p2, int idx, int left) { n   if (idx == m) return 0; n   if (dp[p1][p2][idx][left] != -1) { n     return dp[p1][p2][idx][left]; n   } n   int mxx = 0; n   for (int i = 1; i <= left and i <= c[p1][idx]; i++) { n     int bcost = a[p1][idx] * i; n     int scost = b[p2][idx] * i; n     int ret = (scost - bcost) + call(p1, p2, idx + 1, left - i); n     mxx = max(ret, mxx); n   } n   int ret = call(p1, p2, idx + 1, left); n   mxx = max(ret, mxx); n   return dp[p1][p2][idx][left] = mxx; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   ; n   int k; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     scanf( %s , ts); n     for (int j = 0; j < m; j++) { n       scanf( %d%d%d , &a[i][j], &b[i][j], &c[i][j]); n     } n   } n   int best = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (i == j) continue; n       int ret = call(i, j, 0, k); n       best = max(ret, best); n     } n   } n   cout << best << endl; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n const int N = 1000 * 1000 + 10; n long long int a[N], mm[N], visited[N], anss[N], ans; n int n; n vector <int> e[N]; n  n void dfs(int v){ n  visited[v] = 1; n  if (e[v].size() == 1 and v != 1){ n   anss[v] = 1; n   return; n  } n  vector <int> vec; n  for (auto u : e[v]){ n   if (visited[u] == 0){ n    dfs(u);/* n    if (v == 6){ n     cout << u <<   kk  ; n    }*/ n    vec.push_back(anss[u]); n   } n  } n  sort(vec.begin(), vec.end()); n   n  if (v != 1){ n   if (vec.size() > 1){ n    ans = max(ans, vec.back() + 1ll); n   } n    n  } n  else{ n    n   if (vec.size() > 1){ n    ans = max(ans, vec.back() + 0ll); n    ans = max(ans, vec[vec.size() - 2] + 1ll); n   } n  } n  anss[v] = vec[0] + 1; n  ans = max(ans, vec[0] + 0ll);/* n  if (ans == 4){ n   cout << v <<     << vec.back() + 1 <<   n ; n  }*/ n  vec.clear(); n   n } n  n int main(){ n  int tt; n  cin >> tt; n  while (tt--){ n   ans = 0; n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++){ n    int x, y; n    cin >> x >> y; n    e[x].push_back(y); n    e[y].push_back(x); n   }//vec.clear(); n   dfs(1); n   /* n   for (int i = 1; i <= n; i++){ n    cout << anss[i] <<    ; n   } n   cout <<   n ;*/ n   cout << ans <<   n ; n   for (int i = 1; i <= n; i++){ n    e[i].clear(); n    visited[i] = 0; n    anss[i] = 0; n   } n  n  } n }


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T gcd(T a, T b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n template <typename T> n T pow(T a, T b, long long m) { n   T ans = 1; n   while (b > 0) { n     if (b % 2 == 1) ans = (ans * a) % m; n     b /= 2; n     a = (a * a) % m; n   } n   return ans % m; n } n int bit[100005]; n int n; n int a[100005]; n int lis[100005]; n void update(int x, int val) { n   for (int i = x; i < 100005; i += (i & -i)) bit[i] = max(bit[i], val); n } n int read(int x) { n   int res = 0; n   for (int i = x; i; i -= (i & -i)) res = max(res, bit[i]); n   return res; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   int ans = 0; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) { n     lis[i] = read(a[i] - 1) + 1; n     update(a[i], lis[i]); n     ans = max(ans, lis[i]); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long H, L; n   scanf( %lld%lld , &H, &L); n   printf( %f , 0.5 * (L * L - H * H) / H); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   if (n < k || (n > 1 && k == 1)) { n     cout << -1 << endl; n     return 0; n   } n   if (n <= 2) { n     n == 1 ? cout <<  a  << endl : cout <<  ab  << endl; n     return 0; n   } n   char *str = new char[n + 1]; n   str[n] =   0 ; n   int pos = 0; n   int prev = 1; n   char used =  a  - 1; n   while (n - pos > k - 2) { n     if (prev == 1) { n       str[pos] =  a ; n       prev = 0; n     } else { n       str[pos] =  b ; n       prev = 1; n     } n     if (used < str[pos]) { n       used = str[pos]; n     } n     pos++; n   } n   while (pos != n) { n     used++; n     str[pos] = used; n     pos++; n   } n   cout << str << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, a, b, c, d, now, f[1001]; n bool jud[1001]; n int main() { n   cin.sync_with_stdio(false); n   cin >> n >> k; n   cin >> a >> b >> c >> d; n   if (k < n + 1 || n == 4) { n     cout << -1 << endl; n     return 0; n   } n   f[1] = a; n   f[2] = c; n   f[n - 1] = d; n   f[n] = b; n   jud[a] = jud[b] = jud[c] = jud[d] = true; n   int now = 1; n   for (int i = 3; i <= n - 2; i++) { n     while (jud[now]) now++; n     f[i] = now; n     now++; n   } n   for (int i = 1; i <= n - 1; i++) cout << f[i] <<    ; n   cout << f[n] << endl; n   cout << f[2] <<     << f[1] <<    ; n   for (int i = 3; i <= n - 2; i++) cout << f[i] <<    ; n   cout << f[n] <<     << f[n - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 50; n const long long mod = 1e9 + 7; n const long long INF = 1e18; n long long dx[] = {-1, 0, 1, 0}; n long long dy[] = {0, 1, 0, -1}; n bool compare(const pair<long long, long long>& i, n              const pair<long long, long long>& j) { n   return i.second > j.second; n } n long long fact[N]; n vector<bool> is_prime(100000, true); n vector<long long> prime; n void pre() { n   fact[0] = 1; n   for (long long i = 1; i < N; i++) { n     fact[i] = i * fact[i - 1]; n     fact[i] %= mod; n   } n } n long long modexp(long long x, long long n) { n   long long r = 1; n   while (n) { n     if (n & 1) r = (r * x) % mod; n     x = (x * x) % mod; n     n >>= 1; n   } n   return r % mod; n } n long long modinv(long long x) { return modexp(x, mod - 2); } n long long ncr(long long n, long long r) { n   if (n < r) return 0; n   long long ans = n       (fact[n] % mod * modinv(fact[r]) % mod * modinv(fact[n - r]) % mod) % mod; n   return ans; n } n long long exp(long long x, long long n) { n   long long r = 1; n   while (n) { n     if (n & 1) r = (r * x); n     x = (x * x); n     n >>= 1; n   } n   return r; n } n void solve() { n   long long n; n   cin >> n; n   long long x = 1; n   long long A[n + 2]; n   for (long long i = 1; i <= n; i += 2) { n     A[i] = x; n     x++; n   } n   for (long long i = n - n % 2; i >= 2; i -= 2) { n     A[i] = x; n     x++; n   } n   for (long long i = 1; i <= n; i++) cout << A[i] <<    ; n   cout << endl; n   return; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   for (long long i = 0; i < t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long b, a, r; n int k; n int d0[360360 + 5], d1[360360 + 5]; n void sim() { n   assert(a <= b); n   while (b > a) { n     long long w = b - 1; n     for (int i = 2, ThxDem = k + 1; i < ThxDem; ++i) n       if (b / i * i >= a) w = min(w, b / i * i); n     b = w; n     r++; n   } n } n int main() { n   scanf( %lld%lld%d , &b, &a, &k); n   memset(d0, -1, sizeof(d0)); n   d0[b % 360360] = d1[b % 360360] = 0; n   while (1) { n     long long w = b - 1; n     for (int i = 2, ThxDem = k + 1; i < ThxDem; ++i) w = min(w, b / i * i); n     if (w <= a) { n       sim(); n       break; n     } n     if (d0[w % 360360] >= 0) { n       int l0 = d0[b % 360360] - d0[w % 360360] + 1, n           l1 = d1[b % 360360] - d1[w % 360360] + b - w; n       long long nc = (w - a) / l1; n       r += nc * l0; n       b -= nc * l1; n       sim(); n       break; n     } n     d0[w % 360360] = d0[b % 360360] + 1; n     d1[w % 360360] = d1[b % 360360] + b - w; n     b = w; n     r++; n   } n   printf( %lld n , r); 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 songs = n + m - 1; n   cout << songs << endl; n   for (int i = 1; i <= n; i++) { n     cout << i <<     << 1 << endl; n   } n   for (int i = 2; i <= m; i++) { n     cout << 1 <<     << i << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int a, b, c; n     int d, e, f; n     scanf( %d%d%d , &a, &b, &c); n     scanf( %d%d%d , &d, &e, &f); n     int sum = 0; n     sum = min(c, e) * 2; n     e = e - sum / 2; n     c = c - sum / 2; n     if (c > 0) { n       f = f - min(c, f); n     } n     if (d + e < b) { n       sum = sum - min((b - d - e), f) * 2; n     } n     printf( %d n , sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   int m = 0, c = 0; n   while (t--) { n     int a, b; n     cin >> a >> b; n     if (a > b) n       m++; n     else if (a < b) n       c++; n     else { n       m++; n       c++; n     } n   } n   if (m > c) n     cout <<  Mishka ; n   else if (m < c) n     cout <<  Chris ; n   else { n     cout <<  Friendship is magic!^^ ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)6e4 + 2; n int n; n map<string, int> mp; n vector<pair<string, int>> v; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   string str; n   for (int i = 0; i < n; ++i) { n     cin >> str; n     mp[str] = i; n   } n   for (map<string, int>::iterator it = mp.begin(); it != mp.end(); ++it) { n     v.push_back(*it); n   } n   sort(v.begin(), v.end(), n        [](const pair<string, int>& a, pair<string, int>& b) { n          return a.second > b.second; n        }); n   for (int i = 0; i < v.size(); ++i) cout << v[i].first << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e6, mod = 1000000007; n long long n, m, x, k, d, p, z, sum; n vector<long long> v, ans; n set<long long> st; n int main() { n   cin >> n >> k; n   for (long long i = 1; i <= 2 * n; i *= 2) v.push_back(i - 1); n   if (k == 1) { n     cout << n; n     return 0; n   } else { n     for (int i = v.size() - 1; i >= 0; i--) { n       if (i == 0 || (v[i] >= n && v[i - 1] < n)) { n         cout << v[i]; n         return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n string toStr(const T &x) { n   stringstream s; n   s << x; n   return s.str(); n } n template <class T> n int toInt(const T &x) { n   stringstream s; n   s << x; n   int r; n   s >> r; n   return r; n } n vector<string> pals; n bool check(string s) { n   for (int i = 0, j = s.size() - 1; i < s.size() / 2; i++, j--) { n     if (s[i] != s[j]) return false; n   } n   return true; n } n void gen() { n   for (int i = 0; i < 24; ++i) { n     for (int j = 0; j < 60; ++j) { n       string t1 = i < 10 ?  0  :   ; n       string t2 = j < 10 ?  0  :   ; n       string t = t1 + toStr(i) +  :  + t2 + toStr(j); n       if (check(t)) pals.push_back(t); n     } n   } n } n int main() { n   gen(); n   string line; n   cin >> line; n   if (line >  23:32 ) n     cout <<  00:00  << endl; n   else { n     vector<string>::iterator it = upper_bound(pals.begin(), pals.end(), line); n     cout << pals[(it - pals.begin())] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5, MX = 2e5, MN = 0; n struct node; n extern node* empty; n struct node { n   int mx; n   node *LF, *RT; n   node() : mx(0), LF(this), RT(this) {} n   node(int val, node* lf = empty, node* rt = empty) : mx(val), LF(lf), RT(rt) {} n }; n node* empty = new node(); n node* insert(int val, int ind, node* cur, int ns = 0, int ne = MX) { n   if (ind > ne or ind < ns) return cur; n   if (ns == ne) return new node(val); n   int mid = ns + (ne - ns) / 2; n   node* lf = insert(val, ind, cur->LF, ns, mid); n   node* rt = insert(val, ind, cur->RT, mid + 1, ne); n   return new node(max(lf->mx, rt->mx), lf, rt); n } n int getMax(int qe, node* cur, int ns = 0, int ne = MX) { n   if (qe < 0) return 0; n   if (ns > qe) return 0; n   if (ne <= qe) return cur->mx; n   int mid = ns + (ne - ns) / 2; n   return max(getMax(qe, cur->LF, ns, mid), getMax(qe, cur->RT, mid + 1, ne)); n } n node* roots[N]; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (long long i = 1; i <= n; i++) { n     roots[i] = empty; n   } n   for (long long i = 0; i < m; i++) { n     int a, b, w; n     scanf( %d%d%d , &a, &b, &w); n     roots[b] = insert(getMax(w - 1, roots[a]) + 1, w, roots[b]); n   } n   int ans = 0; n   for (long long i = 1; i <= n; i++) { n     ans = max(ans, getMax(MX, roots[i])); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int n; n   cin >> n; n   int arr[n + 5][n + 5], a, b; n   map<int, int> mp, mp2; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       arr[i][j] = 0; n     } n   } n   for (int i = 1; i < n; i++) { n     cin >> a >> b; n     arr[a][b] = 1; n     mp[a]++; n     mp2[b]++; n   } n   vector<pair<int, pair<int, int>>> v; n   int tmp = n; n   while (tmp > 1) { n     if (mp2[tmp]) { n       for (int i = 1; i < tmp; i++) { n         if (!mp2[i]) { n           v.push_back({2, {i, tmp}}); n           mp2[i] = mp2[tmp]; n           for (int ii = 1; ii <= tmp; ii++) { n             swap(arr[ii][tmp], arr[ii][i]); n           } n           break; n         } n       } n     } n     if (!mp[tmp]) { n       for (int i = 1; i < tmp; i++) { n         if (mp[i]) { n           v.push_back({1, {i, tmp}}); n           mp[i] = mp[tmp]; n           for (int ii = 1; ii <= tmp; ii++) { n             mp2[ii] -= arr[i][ii]; n             swap(arr[tmp][ii], arr[i][ii]); n           } n           break; n         } n       } n     } else { n       for (int ii = 1; ii <= tmp; ii++) { n         mp2[ii] -= arr[tmp][ii]; n         arr[tmp][ii] = 0; n       } n     } n     tmp--; n   } n   cout << v.size() <<   n ; n   for (auto i : v) { n     cout << i.first <<     << i.second.first <<     << i.second.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long res, rec, d, h, v, e, p = 3.1415926535; n int main() { n   cin >> d >> h; n   if (d == 1 && h == 1) { n     cout << 0; n     return 0; n   } n   while (d != 0 && h != 0) { n     if (d <= h) { n       d++; n       h -= 2; n     } else if (d > h) { n       d -= 2; n       h++; n     } n     e++; n   } n   cout << e; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int kocka[25] = {}; n   for (int i = 1; i <= 24; i++) cin >> kocka[i]; n   if (kocka[1] == kocka[2] && kocka[2] == kocka[3] && kocka[3] == kocka[4] && n       kocka[17] == kocka[18] && kocka[18] == kocka[7] && kocka[7] == kocka[8] && n       kocka[21] == kocka[22] && kocka[22] == kocka[19] && n       kocka[19] == kocka[20]) { n     cout <<  YES ; n     exit(0); n   } n   if (kocka[1] == kocka[2] && kocka[2] == kocka[3] && kocka[3] == kocka[4] && n       kocka[13] == kocka[14] && kocka[14] == kocka[7] && kocka[7] == kocka[8] && n       kocka[5] == kocka[6] && kocka[6] == kocka[19] && kocka[19] == kocka[20]) { n     cout <<  YES ; n     exit(0); n   } n   if (kocka[1] == kocka[2] && kocka[2] == kocka[17] && kocka[17] == kocka[19] && n       kocka[5] == kocka[6] && kocka[6] == kocka[7] && kocka[7] == kocka[8] && n       kocka[10] == kocka[18] && kocka[18] == kocka[9] && n       kocka[9] == kocka[20]) { n     cout <<  YES ; n     exit(0); n   } n   if (kocka[1] == kocka[2] && kocka[2] == kocka[16] && kocka[16] == kocka[14] && n       kocka[5] == kocka[6] && kocka[6] == kocka[7] && kocka[7] == kocka[8] && n       kocka[3] == kocka[18] && kocka[18] == kocka[4] && kocka[4] == kocka[20]) { n     cout <<  YES ; n     exit(0); n   } n   if (kocka[5] == kocka[2] && kocka[2] == kocka[7] && kocka[7] == kocka[4] && n       kocka[9] == kocka[6] && kocka[6] == kocka[11] && kocka[11] == kocka[8] && n       kocka[17] == kocka[18] && kocka[18] == kocka[19] && n       kocka[19] == kocka[20]) { n     cout <<  YES ; n     exit(0); n   } n   if (kocka[24] == kocka[2] && kocka[2] == kocka[22] && kocka[22] == kocka[4] && n       kocka[1] == kocka[6] && kocka[6] == kocka[3] && kocka[3] == kocka[8] && n       kocka[17] == kocka[18] && kocka[18] == kocka[19] && n       kocka[19] == kocka[20]) { n     cout <<  YES ; n     exit(0); n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n const double eps = 1e-14; n char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc; n int ny; n inline char gc() { return getchar(); } n inline long long read() { n   long long x = 0; n   for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;) n     if (nc == 45) ny = -1; n   if (nc < 0) return nc; n   for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF; n        x = (x << 3) + (x << 1) + (nc ^ 48)) n     ; n   return x * ny; n } n int n, top, a[MAXN], b[MAXN]; n long long t; n double p[MAXN], Max; n struct Point { n   double x, y; n   int id; n   Point(double x = 0, double y = 0, int id = 0) : x(x), y(y), id(id) {} n   double K() { return y / x; } n } s[MAXN], stk[MAXN]; n inline Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); } n inline double operator^(Point a, Point b) { return a.x * b.y - a.y * b.x; } n inline int sig(double x) { return x < -eps ? -1 : x > eps ? 1 : 0; } n struct Mat { n   double a[3][3]; n   inline double* operator[](int x) { return a[x]; } n   inline friend Mat operator*(Mat a, Mat b) { n     Mat c; n     for (int i = (0); i <= (2); i++) n       for (int j = (0); j <= (2); j++) c[i][j] = 0; n     for (int i = (0); i <= (2); i++) n       for (int j = (0); j <= (2); j++) n         for (int k = (0); k <= (2); k++) c[i][j] += a[i][k] * b[k][j]; n     return c; n   } n } coef, st, ST[40]; n int main() { n   n = read(), t = read(); n   for (int i = (1); i <= (n); i++) n     scanf( %d%d%lf , a + i, b + i, p + i), s[i] = Point(p[i], p[i] * a[i], i), n                                            Max = max(Max, p[i] * b[i]); n   sort(s + 1, s + n + 1, [](Point x, Point y) { n     return !sig(x.x - y.x) ? sig(x.y - y.y) > 0 : sig(x.x - y.x) < 0; n   }); n   for (int i = (1); i <= (n); i++) { n     if (i != 1 && !sig(s[i].x - s[i - 1].x)) continue; n     for (; n          top > 1 && sig((s[i] - stk[top - 1]) ^ (stk[top] - stk[top - 1])) <= 0; n          top--) n       ; n     stk[++top] = s[i]; n   } n   int now = 1; n   st[2][0] = 1; n   for (long long l = 0, r; l < t; l = r) { n     double s = st[1][0] * Max - st[0][0]; n     for (; now < top && (stk[now + 1] - stk[now]).K() >= -s; now++) n       ; n     int j = stk[now].id; n     coef[0][0] = 1 - p[j], coef[0][1] = p[j] * Max, coef[0][2] = p[j] * a[j]; n     coef[1][0] = 0, coef[1][1] = coef[1][2] = 1, coef[2][0] = coef[2][1] = 0, n     coef[2][2] = 1; n     ST[0] = coef; n     for (int i = (1); i <= (34); i++) ST[i] = ST[i - 1] * ST[i - 1]; n     r = l; n     for (int i = (34); i >= (0); i--) n       if (r + (1ll << i) < t) { n         Mat t = ST[i] * st; n         double ss = t[1][0] * Max - t[0][0]; n         if (now == top || (stk[now + 1] - stk[now]).K() < -ss) n           r += 1ll << i, st = t; n       } n     st = ST[0] * st, r++; n   } n   printf( %.10lf , st[0][0]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n const long long INF = 1e18; n const long long MOD = 1000000007; n long double A[100005], B[100005]; n int main() { n   int n, p; n   cin >> n >> p; n   for (int i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     A[i] = y / p; n     A[i] -= (x - 1) / p; n     B[i] = (y - x + 1); n   } n   long double res; n   long double ans = 0; n   for (int i = 1; i < n; i++) { n     res = (A[i] * A[i - 1]) + (A[i] * (B[i - 1] - A[i - 1])) + n           (A[i - 1] * (B[i] - A[i])); n     long double d = B[i] * B[i - 1]; n     res = res / d; n     res = res * 2000; n     ans += res; n   } n   res = (A[0] * A[n - 1]) + (A[0] * (B[n - 1] - A[n - 1])) + n         (A[n - 1] * (B[0] - A[0])); n   long double d = B[0] * B[n - 1]; n   res = res / d; n   res = res * 2000; n   ans += res; n   cout << setprecision(10) << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mxn = 105; n const long long mxm = 1; n const long long INF = 0x3f3f3f3f; n const long long mod = 1e9 + 7; n const double eps = 1e-7; n long long a[mxn]; n signed main(void) { n   long long q; n   cin >> q; n   while (q--) { n     long long n; n     cin >> n; n     long long ct1 = 0, ct2 = 0, ct0 = 0, ans = 0; n     for (long long i = 1; i <= n; i++) { n       cin >> a[i]; n       a[i] = a[i] % 3; n       if (a[i] == 1) ct1++; n       if (a[i] == 2) ct2++; n       if (a[i] == 0) ct0++; n     } n     ans = ct0; n     if (ct1 >= ct2) { n       ans += ct2; n       ct1 -= ct2; n       ans += ct1 / 3; n     } else { n       ans += ct1; n       ct2 -= ct1; n       ans += ct2 / 3; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   vector<int> v; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   v.push_back(1); n   int flag = 0; n   for (int i = 1; i < n; i++) { n     flag = 0; n     for (int j = 0; j < v.size(); j++) { n       if (a[i] >= v[j]) { n         v[j]++; n         flag = 1; n         break; n       } n     } n     if (!flag) v.push_back(1); n   } n   cout << v.size(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5; n const int M = N + 7; n const int MOD = 1e9 + 7; n const long long INF = 1e16 + 17; n const int K = 5; n void ex() { n   printf( -1 n ); n   exit(0); n } n int a[M]; n int l[M], r[M], len[M]; n int an[M]; n bool dp[M][K + 3]; n pair<int, int> par[M][K + 3]; n bool cmp(int i, int j) { return l[i] < l[j]; } n void fillLR(int tl, int tr, int cnt, int st) { n   if (tl > tr) return; n   int d = tr - tl + 1; n   int cur = d / cnt; n   for (int i = st; i < st + cnt; ++i) { n     int x = cur + (d % cnt > i - st); n     while (x-- && tl <= tr) an[tl++] = i; n   } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     l[a[i]] = l[a[i]] ? l[a[i]] : i; n     r[a[i]] = i; n   } n   vector<int> ord; n   for (int i = 1; i <= N; ++i) { n     if (l[i]) { n       len[i] = r[i] - l[i] + 1; n       ord.push_back(i); n     } n     if (len[i] > 5) ex(); n   } n   l[0] = -4; n   r[0] = 0; n   len[0] = K; n   ord.push_back(0); n   sort(ord.begin(), ord.end(), cmp); n   for (int t = 1; t < (int)ord.size(); ++t) { n     if (l[ord[t]] <= r[ord[t - 1]]) ex(); n   } n   dp[0][0] = true; n   int m = (int)ord.size() - 1; n   for (int t = 1; t <= m; ++t) { n     int i = ord[t]; n     int j = ord[t - 1]; n     for (int rr = 0; rr + len[j] <= K; ++rr) { n       if (!dp[t - 1][rr]) continue; n       for (int tr = 0; tr + len[i] <= K; ++tr) { n         for (int tl = 0; tl + tr + len[i] <= K; ++tl) { n           if (tr + tl + len[i] < 2) continue; n           int dist = (l[i] - tl) - (r[j] + rr) - 1; n           int cnt = i - j - 1; n           if (cnt * 2 <= dist && dist <= cnt * 5) { n             dp[t][tr] = true; n             par[t][tr] = make_pair(tl, rr); n           } n         } n       } n     } n   } n   int ans = 0, k = 0; n   for (int tr = 0; tr + len[ord[m]] <= K && r[ord[m]] + tr <= n; ++tr) { n     if (dp[m][tr]) { n       int len = n - r[ord[m]] - tr; n       if (len != 1) { n         fillLR(r[ord[m]] + tr + 1, n, len / 2, ord[m] + 1); n         k = tr; n         ans = ord[m] + len / 2; n         break; n       } n     } n   } n   if (!ans) ex(); n   while (m) { n     int cnt = ord[m] - ord[m - 1] - 1; n     pair<int, int> cur = par[m][k]; n     fillLR(l[ord[m]] - cur.first, r[ord[m]] + k, 1, ord[m]); n     fillLR(r[ord[m - 1]] + cur.second + 1, l[ord[m]] - cur.first - 1, cnt, n            ord[m - 1] + 1); n     m--; n     k = cur.second; n   } n   printf( %d n , ans); n   for (int i = 1; i <= n; ++i) printf( %d  , an[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 302; n bool ar[mxN]; n int p, n; n void solve() { n   cin >> p >> n; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     int temp = a % p; n     if (ar[temp] == 0) { n       ar[temp] = 1; n     } else { n       cout << i + 1; n       return; n     } n   } n   cout << -1; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t = 1, i = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1.01E6; n vector<int> ans[MAXN]; n char C[MAXN]; n int A[MAXN]; n int B[MAXN]; n int n, top1, top2; n int main(void) { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> (C + 1), n = strlen(C + 1); n   for (int i = 1; i <= n; ++i) { n     if (C[i] ==  0 ) { n       if (top2 == 0) n         A[++top1] = i, ans[i].push_back(i); n       else { n         int nowPos = B[top2--]; n         ans[nowPos].push_back(i), A[++top1] = nowPos; n       } n     } else { n       if (top1 == 0) return puts( -1 ) * 0; n       int nowPos = A[top1--]; n       ans[nowPos].push_back(i), B[++top2] = nowPos; n     } n   } n   if (top2) return puts( -1 ) * 0; n   int o = 0; n   cout << top1 <<   n ; n   for (int i = 1; i <= top1; ++i) { n     int id = A[i]; n     cout << ans[id].size() <<    ; n     for (int a : ans[id]) cout << a <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const long long mod = 1000000007; n const int mxN = 100010; n vector<int> g[mxN]; n int main(int argc, char** argv) { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n   int N, M, u, v; n   cin >> N >> M; n   for (int i = 0; i < M; i++) { n     cin >> u >> v; n     u--, v--; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   vector<int> a; n   for (int i = 0; i < N; i++) a.push_back(i); n   for (int iti = 0; iti < 1000; iti++) { n     shuffle(a.begin(), a.end(), rng); n     int prev = -10; n     vector<pair<int, int> > res; n     for (int i = 0; i + 1 < N && (int)res.size() < M; i++) { n       u = a[i]; n       v = a[i + 1]; n       bool f = find(g[u].begin(), g[u].end(), v) == g[u].end(); n       if (!f && prev == -10) break; n       if (f) { n         res.push_back({u, v}); n         if (prev == -10) prev = i; n       } else { n         if (prev != i - 1) { n           bool f2 = find(g[u].begin(), g[u].end(), a[prev]) == g[u].end(); n           if (f2) { n             res.push_back({u, a[prev]}); n           } n         } n         prev = -10; n       } n     } n     if (prev != -10 && prev != N - 2 && (int)res.size() + 1 == M) { n       u = a[N - 1]; n       bool f2 = find(g[u].begin(), g[u].end(), a[prev]) == g[u].end(); n       if (f2) { n         res.push_back({u, a[prev]}); n       } n       prev = -10; n     } n     if ((int)res.size() == M) { n       for (auto p : res) cout << p.first + 1 <<     << p.second + 1 <<   n ; n       return 0; n     } n   } n   cout <<  -1 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, sz[100005], msz[100005], xsz[100005], lsz[100005], son[100005], n     ans[100005]; n vector<int> G[100005]; n void dfs(int u) { n   sz[u] = 1; n   lsz[u] = n; n   son[u] = -1; n   for (int v : G[u]) { n     dfs(v), sz[u] += sz[v]; n     if (son[u] == -1 || sz[v] > sz[son[u]]) son[u] = v; n     if (sz[v] > msz[u]) n       xsz[u] = msz[u], msz[u] = sz[v]; n     else n       xsz[u] = max(xsz[u], sz[v]); n     lsz[u] = min(lsz[u], sz[v]); n   } n   if (n - sz[u] > msz[u]) n     xsz[u] = msz[u], msz[u] = n - sz[u]; n   else n     xsz[u] = max(xsz[u], n - sz[u]); n   if (n - sz[u]) lsz[u] = min(lsz[u], n - sz[u]); n } n multiset<int> st, st2; n void ser1(int u) { n   st.insert(sz[u]); n   for (int v : G[u]) ser1(v); n } n void dfs1(int u) { n   for (int v : G[u]) n     if (v != son[u]) { n       dfs1(v); n       st.clear(); n     } n   if (son[u] != -1) dfs1(son[u]); n   if (n - sz[u] != msz[u]) { n     int L = xsz[u], R = msz[u], mid; n     for (; L < R;) { n       mid = L + R >> 1; n       multiset<int>::iterator it = st.lower_bound(msz[u] - mid); n       if (it != st.end() && (*it) <= mid - lsz[u]) n         R = mid; n       else n         L = mid + 1; n     } n     ans[u] = L; n   } n   for (int v : G[u]) n     if (v != son[u]) ser1(v); n   st.insert(sz[u]); n } n void ser2(int u) { n   st.erase(st.find(sz[u])); n   for (int v : G[u]) ser2(v); n } n void dfs2(int u) { n   st.erase(st.find(sz[u])); n   for (int v : G[u]) n     if (v != son[u]) { n       st2.insert(sz[u]); n       dfs2(v); n       ser1(v); n       st2.erase(st2.find(sz[u])); n     } n   if (son[u] != -1) { n     int v = son[u]; n     st2.insert(sz[u]); n     dfs2(v); n     st2.erase(st2.find(sz[u])); n   } n   for (int v : G[u]) n     if (v != son[u]) ser2(v); n   if (n - sz[u] == msz[u]) { n     int L = xsz[u], R = msz[u], mid; n     for (; L < R;) { n       mid = L + R >> 1; n       multiset<int>::iterator it = st.lower_bound(msz[u] - mid), n                               it2 = st2.lower_bound(msz[u] - mid + sz[u]); n       if (it != st.end() && (*it) <= mid - lsz[u]) n         R = mid; n       else if (it2 != st2.end() && (*it2) <= mid - lsz[u] + sz[u]) n         R = mid; n       else n         L = mid + 1; n     } n     ans[u] = L; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = (1), LIM = (n); i <= LIM; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n   } n   dfs(0); n   dfs1(G[0][0]); n   dfs2(G[0][0]); n   for (int i = (1), LIM = (n); i <= LIM; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long first, long long second) { n   return !second ? first : gcd(second, first % second); n } n long long x, y, z; n deque<long long> q, p; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   cin >> x; n   for (int i = 1; i <= x; i++) { n     if (i % 2 == 0) { n       z += i; n       q.push_back(i); n     } else { n       y += i; n       p.push_back(i); n     } n   } n   if (gcd(y, z) > 1) { n     cout <<  Yes  << endl; n     cout << q.size() <<    ; n     while (!q.empty()) { n       cout << q.front() <<    ; n       q.pop_front(); n     } n     cout << endl; n     cout << p.size() <<    ; n     while (!p.empty()) { n       cout << p.front() <<    ; n       p.pop_front(); n     } n     cout << endl; n   } else { n     cout <<  No  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans, cr[40002], ar[40005], br[40005], nd[40005]; n void build(int id, int l, int r) { n   if (l == r) { n     nd[id] = cr[l]; n     return; n   } n   int md = (l + r) >> 1; n   build(id * 2, l, md); n   build(id * 2 + 1, md + 1, r); n   nd[id] = min(nd[2 * id], nd[2 * id + 1]); n   ans -= nd[id]; n } n int main() { n   long long i, j, k, n, m, x, mx; n   cin >> n; n   queue<long long> Q; n   m = (1 << (n + 1)) - 2; n   Q.push(2); n   Q.push(3); n   mx = 0; n   k = 0; n   for (i = 1; i <= m; i++) { n     cin >> x; n     long long tp = Q.front(); n     Q.pop(); n     if (tp >= (1 << (n))) cr[k++] = x; n     ar[tp] = x; n     br[tp] = x + br[tp / 2]; n     mx = max(mx, br[tp]); n     Q.push(2 * tp); n     Q.push(2 * tp + 1); n   } n   ans = 0; n   int p = (1 << n); n   k = 1; n   while (1) { n     cr[k] = mx - br[p]; n     ans += cr[k]; n     k++; n     p++; n     if (p > (m + 1)) break; n   } n   build(1, 1, (m + 2)); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1002020, mod = 1e9 + 7; n int m, tot, ans = 1, mu[N], iv[N], pr[N]; n bool vis[N]; n void prework(int mx) { n   iv[1] = mu[1] = 1; n   for (int i = 2; i <= mx; i++) { n     iv[i] = 1ll * iv[mod % i] * (mod - mod / i) % mod; n     if (!vis[i]) pr[++tot] = i, mu[i] = -1; n     for (int j = 1; j <= tot && i * pr[j] <= mx; j++) { n       vis[i * pr[j]] = 1; n       if (!(i % pr[j])) break; n       mu[i * pr[j]] = -mu[i]; n     } n   } n } n int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int main() { n   m = read(); n   prework(m); n   for (int i = 2; i <= m; i++) n     ans = (ans - 1ll * mu[i] * (m / i) * iv[m - m / i] % mod) % mod; n   cout << (ans + mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int HPY, ATKY, DEFY, HPM, ATKM, DEFM, h, a, d; n   while (scanf( %d %d %d , &HPY, &ATKY, &DEFY) == 3) { n     scanf( %d %d %d , &HPM, &ATKM, &DEFM); n     scanf( %d %d %d , &h, &a, &d); n     int mini = 1000000000; n     for (int i = ATKY; i <= 301; i++) n       for (int j = DEFY; j <= 301; j++) { n         if (i - DEFM > 0) { n           int cost = (i - ATKY) * a + (j - DEFY) * d; n           int p1 = i - DEFM; n           int k1 = HPM / p1; n           if (HPM % p1 != 0) k1++; n           int p2 = ATKM - j; n           if (p2 > 0) { n             int p3 = p2 * k1 + 1; n             if (HPY < p3) cost += (p3 - HPY) * h; n           } n           if (cost < mini) mini = cost; n         } n       } n     printf( %d n , mini); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double EPS = 1e-9; n int INF = 2000000000; n long long INFF = 8000000000000000000LL; n double PI = acos(-1); n int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1}; n int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1}; n inline string IntToString(int a) { n   char x[100]; n   sprintf(x,  %d , a); n   string s = x; n   return s; n } n inline int StringToInt(string a) { n   char x[100]; n   int res; n   strcpy(x, a.c_str()); n   sscanf(x,  %d , &res); n   return res; n } n inline string GetString(void) { n   char x[1000005]; n   scanf( %s , x); n   string s = x; n   return s; n } n inline string uppercase(string s) { n   int n = (int)s.size(); n   for (int(i) = (0); (i) < (n); ++(i)) n     if (s[i] >=  a  && s[i] <=  z ) s[i] = s[i] -  a  +  A ; n   return s; n } n inline string lowercase(string s) { n   int n = (int)s.size(); n   for (int(i) = (0); (i) < (n); ++(i)) n     if (s[i] >=  A  && s[i] <=  Z ) s[i] = s[i] -  A  +  a ; n   return s; 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 vector<int> angka; n int n, data[2005]; n long long dewa[2005]; n long long MOD = 1000000007; n bool sudah[2005]; n int to[2005]; n int conn; n int pisah; n long long risan; n long long C[2005][2005]; n int main() { n   scanf( %d , &n); n   for (int(i) = (1); (i) <= (n); ++(i)) { n     scanf( %d , &data[i]); n     to[i] = data[i]; n   } n   for (int(i) = (0); (i) <= (n); ++(i)) { n     C[i][0] = C[i][i] = 1; n     for (int(j) = (1); (j) <= (i - 1); ++(j)) n       C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD; n   } n   dewa[1] = 0; n   dewa[2] = 1; n   dewa[0] = 1; n   for (int(i) = (3); (i) <= (n); ++(i)) { n     dewa[i] = (i - 1) * (dewa[i - 1] + dewa[i - 2]); n     dewa[i] %= MOD; n   } n   memset(sudah, 0, sizeof(sudah)); n   for (int(i) = (1); (i) <= (n); ++(i)) { n     if (to[i] == -1) continue; n     int x = i; n     while (x != -1) { n       if (sudah[x]) goto hell; n       sudah[x] = 1; n       x = to[x]; n       if (x == i) goto hell; n     } n     ++conn; n   hell:; n   } n   for (int(i) = (1); (i) <= (n); ++(i)) n     if (!sudah[i]) ++pisah; n   for (int(i) = (0); (i) <= (conn); ++(i)) { n     long long pos = (C[conn][i] * dewa[i + pisah]) % MOD; n     risan = (risan + pos) % MOD; n   } n   printf( %d n , (int)risan); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int n, tot, u, v; n int c[N], head[N], last[N * 2], ver[N * 2]; n void add(int x, int y) { n   ver[++tot] = y; n   last[tot] = head[x]; n   head[x] = tot; n } n void dfs(int x, int fa) { n   cout << x <<    ; n   for (int i = head[x]; i; i = last[i]) { n     int y = ver[i]; n     if (y == fa) continue; n     c[y] *= -1; n     dfs(y, x); n   } n   if (c[x] == -1 && x != 1) { n     cout << fa <<     << x <<    ; n     c[fa] *= -1; n     c[x] *= -1; n   } n   if (x != 1) { n     if (fa == 1) { n       for (int i = 1; i <= n; i++) { n         if (c[i] == -1) { n           cout << fa <<    ; n           c[fa] *= -1; n           return; n         } n       } n     } else { n       cout << fa <<    ; n       c[fa] *= -1; n     } n   } n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   for (int i = 1; i < n; i++) { n     cin >> u >> v; n     add(u, v); n     add(v, u); n   } n   dfs(1, -1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power_mod(long long num, long long g) { n   if (g == 0) return 1; n   if (g % 2 == 1) n     return (num * power_mod((num * num) % 1000000007, g / 2)) % 1000000007; n   return power_mod((num * num) % 1000000007, g / 2); n } n long long power(long long num, long long g) { n   if (g == 0) return 1; n   if (g % 2 == 1) return (num * power((num * num), g / 2)); n   return power((num * num), g / 2); n } n long long a[201], b[201], c[201]; n long long d[201][201][201]; n long long solve(long long x, long long y, long long z) { n   long long e = 0; n   if (d[x][y][z] > -1) return d[x][y][z]; n   long long ans = 0; n   if (x > 0 && y > 0) { n     ans = max(ans, solve(x - 1, y - 1, z) + a[x - 1] * b[y - 1]); n   } n   if (x > 0 && z > 0) { n     ans = max(ans, solve(x - 1, y, z - 1) + a[x - 1] * c[z - 1]); n   } n   if (z > 0 && y > 0) { n     ans = max(ans, solve(x, y - 1, z - 1) + b[y - 1] * c[z - 1]); n   } n   return d[x][y][z] = ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   string s; n   cin >> s; n   vector<pair<long long, long long> > vec, vec1; n   for (long long i = 0; i < n; i++) { n     long long x = 1; n     while (i + 1 < n && s[i] == s[i + 1]) x++, i++; n     if (s[i] ==  ? ) n       vec.push_back(make_pair(x, 2)); n     else n       vec.push_back(make_pair(x, s[i] -  0 )); n   } n   long long l = vec.size(); n   long long x = 0; n   for (long long i = 0; i < l; i++) { n     if (i == 0) n       vec1.push_back(vec[0]); n     else if (i == 1 && vec1[0].second == 2) { n       vec1[0].first += vec[i].first; n       vec1[0].second = vec[i].second; n     } else if (vec[i].second < 2) n       vec1.push_back(vec[i]), x++; n     else { n       if (i + 1 < l && vec[i + 1].second == vec1[x].second) { n         vec1[x].first += vec[i].first + vec[i + 1].first; n         i++; n       } else n         vec1.push_back(vec[i]), x++; n     } n   } n   l = vec1.size(); n   if (l > 1 && vec1[l - 1].second == 2) { n     vec1[l - 2].first += vec1[l - 1].first; n     vec1.pop_back(); n   } n   vec = vec1; n   l = vec1.size(); n   if (l == 1) { n     for (long long i = 1; i <= n; i++) { n       cout << n / i <<    ; n     } n     cout << endl; n     return 0; n   } n   cout << n; n   for (long long i = 2; i <= n; i++) { n     vec1.clear(); n     long long ans = 0; n     l = vec.size(); n     long long prev = 0; n     for (long long j = 0; j < l; j++) { n       long long er = vec[j].first; n       if (j > 0 && vec[j - 1].second == 2) er += vec[j - 1].first; n       if (j + 1 < l && vec[j + 1].second == 2) er += vec[j + 1].first; n       if (er >= i) { n         vec1.push_back(vec[j]); n         if (j + 1 < l && vec[j + 1].second == 2) vec1.push_back(vec[j + 1]); n       } else { n         long long rt = vec1.size(); n         if (rt > 0 && vec1[rt - 1].second == 2) { n           vec1[rt - 2].first += vec1[rt - 1].first; n           vec1.pop_back(); n         } n         if (j + 1 < l && vec[j + 1].second == 2) { n           vec[j + 2].first += vec[j + 1].first; n           vec[j + 1].first = 0; n         } n       } n       if (j + 1 < l && vec[j + 1].second == 2) { n         prev += vec[j + 1].first + vec[j].first; n         ans += prev / i; n         prev = min(prev % i, vec[j + 1].first); n         j++; n       } else { n         prev += vec[j].first; n         ans += prev / i; n         prev = 0; n       } n     } n     cout <<     << ans; n     vec = vec1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char board[2048][2048]; n int rows[2][2048]; n int cols[2][2048]; n int main() { n   int N; n   scanf( %d , &N); n   for (int i = 0; i < N; i++) n     for (int j = 0; j < N; j++) scanf(  %c , &board[i][j]); n   int ans = 0; n   for (int x = 0; x < 2; x++) { n     for (int i = 0; i < N - 1; i++) n       for (int j = N - 1; j > i; j--) { n         int d = rows[x][i] ^ cols[x][j]; n         if (!d != (board[i][j] ==  0 )) { n           ans++; n           rows[x][i] ^= 1; n           cols[x][j] ^= 1; n         } n         board[i][j] = board[j][i]; n       } n   } n   for (int i = 0; i < N; i++) n     if (!(rows[0][i] ^ rows[1][i] ^ cols[0][i] ^ cols[1][i]) != n         (board[i][i] ==  0 )) n       ans++; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   char a[n][5]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 5; j++) cin >> a[i][j]; n   } n   int flag = 0; n   for (int i = 0; i < n; i++) { n     if ((a[i][0] ==  O  && a[i][1] ==  O )) { n       a[i][0] =  + ; n       a[i][1] =  + ; n       flag = 1; n       break; n     } else if ((a[i][3] ==  O  && a[i][4] ==  O )) { n       a[i][3] =  + ; n       a[i][4] =  + ; n       flag = 1; n       break; n     } n   } n   if (flag) { n     cout <<  YES  << endl; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < 5; j++) { n         cout << a[i][j]; n       } n       cout << endl; n     } n   } else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n double l; n int x[1005], b[1005]; n const double eps = 1e-8; n double dp[1005]; n double check(double mid) { n   for (int i = 1; i <= n; i++) dp[i] = -1e10; n   dp[0] = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < i; j++) { n       dp[i] = max(dp[i], n                   dp[j] - mid * (sqrt(abs(l - (x[i] - x[j])))) + (double)b[i]); n     } n   } n   return dp[n]; n } n void dfs(int step, double mid) { n   if (step == 0) return; n   for (int i = 0; i < step; i++) { n     if (abs(dp[step] - (dp[i] - mid * sqrt(abs(x[step] - x[i] - l)) + n                         (double)b[step])) < eps) n       dfs(i, mid); n   } n   printf( %d  , step); n } n int main() { n   scanf( %d%lf , &n, &l); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d , &x[i], &b[i]); n   } n   double l = 0, r = 100000000000; n   while (l + eps < r) { n     double mid = (l + r) / 2; n     double qwq = check(mid); n     if (qwq > -eps) n       l = mid; n     else n       r = mid; n   } n   check(l); n   dfs(n, l); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int N = 200031; n int n, m, a[N], b[N]; n vector<int> g[N]; n int used[N]; n int dep[N]; n vector<int> this_comp; n vector<pair<int, int> > order; n vector<pair<int, pair<int, int> > > solution; n int par[N]; n int in_st[N]; n int used_edge[N]; n int par_edge[N]; n set<int> sons[N]; n vector<int> with_dep[N]; n vector<int> not_st_list[N]; n set<int>::iterator it; n void trace(int v) { n   used[v] = 1; n   this_comp.push_back(v); n   for (int i = 0; i < g[v].size(); i++) { n     int id = g[v][i]; n     int to = a[id] + b[id] - v; n     if (used[to]) continue; n     par[to] = v; n     sons[v].insert(to); n     par_edge[to] = id; n     dep[to] = dep[v] + 1; n     in_st[id] = 1; n     trace(to); n   } n } n void process_vertex(int v) { n   vector<int> free_edges; n   for (int i = 0; i < g[v].size(); i++) { n     int id = g[v][i]; n     if (used_edge[id]) continue; n     if (in_st[id]) continue; n     free_edges.push_back(id); n   } n   while (free_edges.size() > 1) { n     int a = free_edges.back(); n     free_edges.pop_back(); n     int b = free_edges.back(); n     free_edges.pop_back(); n     used_edge[a] = 1; n     used_edge[b] = 1; n     solution.push_back(make_pair(v, make_pair(a, b))); n   } n   if (free_edges.size() == 1) { n     if (par[v] == 0) { n       int id = free_edges[0]; n       used_edge[id] = 1; n       return; n     } n     solution.push_back(make_pair(v, make_pair(par_edge[v], free_edges[0]))); n     used_edge[par_edge[v]] = 1; n     used_edge[free_edges[0]] = 1; n     sons[par[v]].erase(v); n     return; n   } n } n int get_out_of_st(int v) { n   while (not_st_list[v].size() > 0) { n     int id = not_st_list[v].back(); n     if (used_edge[id] == 0) return id; n     not_st_list[v].pop_back(); n   } n   return -1; n } n void resolve(int v) { n   if (par[v] == 0) return; n   if (used_edge[par_edge[v]]) return; n   if (sons[par[v]].size() > 1) { n     sons[par[v]].erase(v); n     it = sons[par[v]].begin(); n     int othr = (*it); n     sons[par[v]].erase(othr); n     used_edge[par_edge[othr]] = 1; n     used_edge[par_edge[v]] = 1; n     solution.push_back( n         make_pair(par[v], make_pair(par_edge[othr], par_edge[v]))); n     return; n   } n   int edge_in_father = get_out_of_st(par[v]); n   if (edge_in_father != -1) { n     sons[par[v]].erase(v); n     solution.push_back( n         make_pair(par[v], make_pair(par_edge[v], edge_in_father))); n     used_edge[par_edge[v]] = 1; n     used_edge[edge_in_father] = 1; n     return; n   } n   if (dep[v] == 1) return; n   int v2 = par[par[v]]; n   sons[par[v]].erase(v); n   sons[v2].erase(par[v]); n   solution.push_back( n       make_pair(par[v], make_pair(par_edge[v], par_edge[par[v]]))); n   used_edge[par_edge[v]] = 1; n   used_edge[par_edge[par[v]]] = 1; n } n void solve(int v) { n   this_comp.clear(); n   trace(v); n   int max_dep = 0; n   for (int i = 0; i < this_comp.size(); i++) { n     max_dep = max(max_dep, dep[this_comp[i]]); n   } n   order.clear(); n   for (int i = 0; i < this_comp.size(); i++) { n     int cur_v = this_comp[i]; n     for (int j = 0; j < g[cur_v].size(); j++) { n       int e_id = g[cur_v][j]; n       if (in_st[e_id]) continue; n       not_st_list[cur_v].push_back(e_id); n     } n     order.push_back(make_pair(dep[cur_v], cur_v)); n     with_dep[dep[cur_v]].push_back(cur_v); n   } n   for (int i = max_dep; i >= 0; --i) { n     for (int j = 0; j < with_dep[i].size(); j++) { n       int v_id = with_dep[i][j]; n       process_vertex(v_id); n     } n     for (int j = 0; j < with_dep[i].size(); j++) { n       int v_id = with_dep[i][j]; n       resolve(v_id); n     } n     with_dep[i].clear(); n   } n } n int get_other(int id, int v) { return a[id] + b[id] - v; } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   cin >> m; n   for (int i = 1; i <= m; i++) { n     cin >> a[i] >> b[i]; n     g[a[i]].push_back(i); n     g[b[i]].push_back(i); n   } n   for (int i = 1; i <= n; i++) { n     if (used[i]) continue; n     solve(i); n   } n   cout << solution.size() << endl; n   for (int i = 0; i < solution.size(); i++) { n     int id1 = solution[i].second.first; n     int id2 = solution[i].second.second; n     cout << get_other(id1, solution[i].first) <<     << solution[i].first <<     n          << get_other(id2, solution[i].first) <<   n ; n   } n   cin.get(); n   cin.get(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, m, k, a, b, ans; n int maxl = 1e9; n int num[300005]; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d%d , &n, &m, &k); n     if (m <= k) k = m - 1; n     a = n - m + 1; n     b = n - k; n     for (int i = 1; i <= n; i++) { n       scanf( %d , &num[i]); n     } n     ans = 0; n     for (int i = 1; i <= n; i++) { n       if (i + b - 1 > n) break; n       int ansl = maxl; n       for (int j = i; j <= i + b - 1; j++) { n         if (j + a - 1 > i + b - 1) break; n         ansl = min(ansl, max(num[j], num[j + a - 1])); n       } n       ans = max(ans, ansl); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, c0, c1, h; n     cin >> n >> c0 >> c1 >> h; n     string s; n     cin >> s; n     int ans = 0; n     int cnt = 0; n     int mini = min(c0, c1); n     for (int i = 0; i < n; i++) { n       if (s[i] ==  1 ) { n         ans += c1; n         if (c1 != mini) cnt++; n       } else { n         ans += c0; n         if (c0 != mini) cnt++; n       } n     } n     ans = min(ans, ((n * mini) + (h * cnt))); n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 10; n long long n, Mx, B[N], S[N]; n struct Node { n   int32_t l, r, c; n   Node() { l = r = c = 0; } n }; n struct BST { n   long long L = 32; n   vector<Node> A; n   BST(long long _L = 32) { n     L = _L, A.clear(); n     A.push_back(Node()); n   } n   void Add(long long val, long long w = 1) { n     long long id = 0; n     for (long long i = L - 1; ~i; i--) { n       A[id].c += w; n       if (val & (1ll << i)) { n         if (!A[id].r) A[id].r = A.size(), A.push_back(Node()); n         id = A[id].r; n         continue; n       } n       if (!A[id].l) A[id].l = A.size(), A.push_back(Node()); n       id = A[id].l; n     } n     A[id].c += w; n   } n   long long MaxXor(long long val) { n     long long id = 0, ret = 0; n     if (!A[id].c) return (-1); n     for (long long i = L - 1; ~i; i--) { n       if (A[id].r && (!A[id].l || !(val & (1ll << i)))) n         ret |= (1ll << i), id = A[id].r; n       else n         id = A[id].l; n     } n     return (ret); n   } n }; n int32_t main() { n   scanf( %lld , &n); n   BST T = BST(43); n   T.Add(0); n   for (long long i = 1; i <= n; i++) scanf( %lld , &B[i]); n   for (long long i = n; i; i--) S[i] = S[i + 1] ^ B[i], T.Add(S[i]); n   for (long long i = 0, r = 0; i <= n; i++, r ^= B[i]) n     Mx = max(Mx, T.MaxXor(r) ^ r), T.Add(S[i + 1], -1); n   return !printf( %lld , Mx); n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( O3 ) n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int get_random() { n   static uniform_int_distribution<int> dist(0, 1e9 + 6); n   return dist(rng); n } n template <class T> n void make_unique(T& v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n } n int geti() { n   int x; n   cin >> x; n   return x; n } n long long getll() { n   long long x; n   cin >> x; n   return x; n } n double getd() { n   double x; n   cin >> x; n   return x; n } n const int MAXN = 5e5 + 100; n void solve(int tt) {} n template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), n           F (*composition)(F, F), F (*id)()> n struct segment_tree { n   int _l, _r; n   vector<S> a; n   vector<S> tree; n   vector<F> lazy; n   void push(int l, int r, int node) { n     if (lazy[node] != id()) { n       int mid = (l + r) / 2; n       int rnode = node + (mid - l + 1) * 2; n       tree[node + 1] = mapping(lazy[node], tree[node + 1]); n       lazy[node + 1] = composition(lazy[node], lazy[node + 1]); n       tree[rnode] = mapping(lazy[node], tree[rnode]); n       lazy[rnode] = composition(lazy[node], lazy[rnode]); n       lazy[node] = id(); n     } n   } n   void build(int l, int r, int node) { n     if (l == r) n       tree[node] = a[l]; n     else { n       int mid = l + (r - l) / 2; n       int rnode = node + (mid - l + 1) * 2; n       build(l, mid, node + 1); n       build(mid + 1, r, rnode); n       tree[node] = op(tree[node + 1], tree[rnode]); n     } n   } n   void update(F v, int left, int right, int l, int r, int node) { n     if (left > right) return; n     if (right < l || r < left) return; n     if (left <= l && r <= right) { n       tree[node] = mapping(v, tree[node]); n       lazy[node] = composition(v, lazy[node]); n     } else { n       push(l, r, node); n       int mid = l + (r - l) / 2; n       int rnode = node + (mid - l + 1) * 2; n       update(v, left, right, l, mid, node + 1); n       update(v, left, right, mid + 1, r, rnode); n       tree[node] = op(tree[node + 1], tree[rnode]); n     } n   } n   S query(int left, int right, int l, int r, int node) { n     if (left > right) return e(); n     if (right < l || r < left) return e(); n     if (left <= l && r <= right) return tree[node]; n     push(l, r, node); n     int mid = l + (r - l) / 2; n     int rnode = node + (mid - l + 1) * 2; n     S r1 = query(left, right, l, mid, node + 1); n     S r2 = query(left, right, mid + 1, r, rnode); n     return op(r1, r2); n   } n   vector<S> get_tree() { n     vector<S> res; n     for (int i = _l; i <= _r; i++) { n       res.push_back(query(i, i)); n     } n     return res; n   } n   void init(int l, int r) { n     _l = l; n     _r = r; n     a.assign(r + 1, e()); n     tree.assign(2 * r + 1, e()); n     lazy.assign(2 * r + 1, id()); n   } n   S& operator[](int idx) { return a[idx]; } n   void build() { build(_l, _r, 0); } n   void update(F v, int left, int right) { update(v, left, right, _l, _r, 0); } n   S query(int left, int right) { return query(left, right, _l, _r, 0); } n }; n long long zero() { return 0; } n long long int_max() { return INT_MAX; } n long long int_min() { return INT_MIN; } n long long get_min(long long x, long long y) { return min(x, y); } n long long get_max(long long x, long long y) { return max(x, y); } n long long add(long long x, long long y) { return x + y; } n long long mul(long long x, long long y) { return x * y; } n pair<int, int> f1(pair<int, int> x, pair<int, int> y) { n   return pair<int, int>({min(x.first, y.first), max(x.second, y.second)}); n }; n pair<int, int> f2() { return pair<int, int>({1e9, -1e9}); }; n pair<int, int> f3(int x, pair<int, int> y) { n   return pair<int, int>( n       {min(y.second * x, y.first * x), max(y.second * x, y.first * x)}); n }; n int f4(int x, int y) { return x * y; }; n int f5() { return 1; }; n segment_tree<pair<int, int>, f1, f2, int, f3, f4, f5> segt; n vector<int> graph1[MAXN]; n vector<int> graph2[MAXN]; n int in[MAXN], out[MAXN], level[MAXN], t = 1; n int cnt[MAXN]; n int n; n void dfs(int i, int p, int c, int l) { n   ; n   ; n   in[i] = t++; n   cnt[in[i]] = c; n   segt[in[i]] = (c % 2 ? pair<int, int>({-l, -l}) : pair<int, int>({l, l})); n   level[i] = l; n   for (auto j : graph1[i]) { n     if (j == p) continue; n     dfs(j, i, c, l + 1); n   } n   for (auto j : graph2[i]) { n     if (j == p) continue; n     dfs(j, i, c + 1, l + 1); n   } n   out[i] = t; n } n pair<int, int> find_root() { n   queue<int> q({1}); n   vector<int> vis(n + 1); n   vis[1] = 1; n   int res1 = 1; n   while (q.size()) { n     int len = q.size(); n     while (len--) { n       auto f = q.front(); n       q.pop(); n       for (auto p : graph1[f]) { n         if (!vis[p]) { n           q.push(p); n           res1 = p; n           vis[p] = 1; n         } n       } n       for (auto p : graph2[f]) { n         if (!vis[p]) { n           q.push(p); n           res1 = p; n           vis[p] = 1; n         } n       } n     } n   } n   int res2 = res1; n   q.push(res1); n   vis = vector<int>(n + 1); n   vis[res1] = 1; n   while (q.size()) { n     int len = q.size(); n     while (len--) { n       auto f = q.front(); n       q.pop(); n       for (auto p : graph1[f]) { n         if (!vis[p]) { n           q.push(p); n           res2 = p; n           vis[p] = 1; n         } n       } n       for (auto p : graph2[f]) { n         if (!vis[p]) { n           q.push(p); n           res2 = p; n           vis[p] = 1; n         } n       } n     } n   } n   return {res1, res2}; n } n int main(int argc, char* argv[]) { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cin >> n; n   int u, v, tp; n   vector<int> us, vs; n   for (auto i = (0); i < (n - 1); i++) { n     cin >> u >> v >> tp; n     if (tp == 0) { n       graph1[u].push_back(v); n       graph1[v].push_back(u); n     } else { n       graph2[u].push_back(v); n       graph2[v].push_back(u); n     } n     us.push_back(u); n     vs.push_back(v); n   } n   pair<int, int> roots = find_root(); n   segt.init(1, n); n   t = 1; n   dfs(roots.first, -1, 0, 0); n   segt.build(); n   int m; n   cin >> m; n   vector<int> idxs; n   for (auto i = (0); i < (m); i++) { n     int idx; n     cin >> idx; n     idx--; n     idxs.push_back(idx); n   } n   vector<int> res1(m), res2(m); n   for (auto i = (0); i < (m); i++) { n     int idx = idxs[i]; n     u = us[idx], v = vs[idx]; n     if (level[u] > level[v]) swap(u, v); n     ; n     ; n     segt.update(-1, in[v], out[v] - 1); n     res1[i] = segt.query(1, n).second; n   } n   segt.init(1, n); n   t = 1; n   dfs(roots.second, -1, 0, 0); n   segt.build(); n   ; n   ; n   ; n   ; n   for (auto i = (0); i < (m); i++) { n     int idx = idxs[i]; n     u = us[idx], v = vs[idx]; n     if (level[u] > level[v]) swap(u, v); n     ; n     ; n     segt.update(-1, in[v], out[v] - 1); n     res2[i] = segt.query(1, n).second; n   } n   for (auto i = (0); i < (m); i++) { n     cout << max(res1[i], res2[i]) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long k, n, d = 0, ans; n string s; n unsigned long long int sol1(int k); n int main() { n   cin >> s; n   n = s.size(); n   cin >> k; n   ans = max(0ULL, min(2 * k, n + k - ((n + k) % 2))); n   ans = max(ans, sol1(k)); n   cout << ans << endl; n   const clock_t begin_time = clock(); n   return 0; n } n unsigned long long int sol1(int t) { n   int bop = 0; n   int anss = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 1; j <= n; j++) { n       bop = 0; n       if (i + 2 * j - 1 >= n + t) { n         bop = 1; n       } else n         for (int k = 0; k < j; k++) { n           if (i + j + k < n) { n             if (s[i + k] != s[i + j + k]) { n               bop = 1; n               break; n             } n           } n         } n       if (bop == 0) { n         anss = max(anss, 2 * j); n       } n     } n   } n   return anss; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   while (cin >> n) { n     int flag = 0; n     long long int i; n     for (int sx = 1; sx < 100; sx++) { n       long long int x = (sqrt(sx * sx + 4 * n) - sx) / 2; n       if (x * x + sx * x == n) { n         int tem = x; n         int t = 0; n         while (tem > 0) { n           t = t + tem % 10; n           tem = tem / 10; n         } n         if (sx == t) { n           flag = 1; n           printf( %d n , x); n           break; n         } n       } n     } n     if (!flag) printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Block { n   long long l, r, c; n   Block(long long l, long long r, long long c) : l(l), r(r), c(c) {} n }; n bool operator<(Block a, Block b) { n   if (a.l != b.l) return a.l < b.l; n   if (a.r != b.r) return a.r < b.r; n   return a.c < b.c; n } n const int maxn = 1000000 + 10; n long long tr_a[maxn], tr_b[maxn]; n void add(long long tr[maxn], long long at, long long what) { n   for (++at; at < maxn; at += -at & at) tr[at] += what; n } n void modify(long long l, long long r, long long c) { n   add(tr_a, l, c); n   add(tr_b, l, (l - 1) * c); n   add(tr_a, r + 1, -c); n   add(tr_b, r + 1, -r * c); n } n long long sum(long long tr[maxn], long long at) { n   long long s = 0; n   for (++at; at > 0; at -= -at & at) s += tr[at]; n   return s; n } n long long query(long long l, long long r) { n   long long sr = r * sum(tr_a, r) - sum(tr_b, r); n   long long sl = (l - 1) * sum(tr_a, l - 1) - sum(tr_b, l - 1); n   return sr - sl; n } n int main() { n   ios_base::sync_with_stdio(false); n   memset((tr_a), (0), sizeof(tr_a)), memset((tr_b), (0), sizeof(tr_b)); n   int n, q; n   cin >> n >> q; n   set<Block> blocks; n   for (int _n(n + 1), i(1); i < _n; i++) blocks.insert(Block(i, i, i)); n   for (int _n(q), _(0); _ < _n; _++) { n     long long op, l, r, x; n     cin >> op >> l >> r; n     if (op == 1) { n       cin >> x; n       auto it = blocks.upper_bound(Block(l, 1011111111, 1011111111)); n       --it; n       while (it != blocks.end()) { n         if (it->l > r) break; n         Block tmp = *it; n         modify(max(l, tmp.l), min(r, tmp.r), labs(tmp.c - x)); n         if (tmp.l < l) { n           blocks.insert(Block(tmp.l, l - 1, tmp.c)); n         } n         if (tmp.r > r) { n           blocks.insert(Block(r + 1, tmp.r, tmp.c)); n         } n         ++it; n         blocks.erase(blocks.find(tmp)); n       } n       blocks.insert(Block(l, r, x)); n     } else { n       long long ans = query(l, r); n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fib[100001]; n void fibonacci() { n   fib[0] = 1; n   fib[1] = 1; n   fib[2] = 2; n   for (long long i = 3; i <= 100000; i++) { n     fib[i] = fib[i - 1] + fib[i - 2]; n     fib[i] = fib[i] % 1000000007; n   } n } n double dist(double x, double y, double a, double b) { n   return sqrt((x - a) * (x - a) + (y - b) * (y - b)); n } n char alphabet[26] = { a ,  b ,  c ,  d ,  e ,  f ,  g ,  h ,  i , n                       j ,  k ,  l ,  m ,  n ,  o ,  p ,  q ,  r , n                       s ,  t ,  u ,  v ,  w ,  x ,  y ,  z }; n long long power(long long x, unsigned long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = res * x; n     y = y >> 1; n     x = x * x; n   } n   return res; n } n long long mpower(long long x, unsigned long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n void swap(long long* a, long long* b) { n   long long temp = *a; n   *a = *b; n   *b = temp; n } n char nxtchr(long long c) { n   if (c == 1) return  1 ; n   if (c == 2) return  2 ; n   if (c == 3) return  3 ; n   if (c == 4) return  4 ; n   if (c == 5) return  5 ; n   if (c == 6) return  6 ; n   if (c == 7) return  7 ; n   if (c == 8) return  8 ; n   if (c == 9) return  9 ; n   if (c == 0) return  0 ; n   return -1; n } n long long dislet(string s) { n   long long i = 0, count = 0; n   while (i < s.size()) { n     char c = s[i]; n     long long j = i; n     count++; n     while (s[j] == c) j++; n     i = j; n   } n   return count; n } n long long findIndex(long long key, vector<pair<long long, long long>> v, n                     long long n) { n   long long l = 0, r = n - 1; n   while (l != r) { n     if (l < r) { n       long long mid = (l + r) / 2; n       if (v[mid].first > key) n         r = mid; n       else n         l = mid + 1; n     } n   } n   return l; n } n vector<long long> ranks; n vector<long long> parent; n vector<long long> maxim; n void make_set(long long v) { n   parent[v] = v; n   ranks[v] = 0; n } n long long find_Set(long long v) { n   if (parent[v] == v) return v; n   return parent[v] = find_Set(parent[v]); n } n void union_sets(long long a, long long b) { n   a = find_Set(a); n   b = find_Set(b); n   if (a != b) { n     if (ranks[a] < ranks[b]) swap(&a, &b); n     parent[b] = a; n     if (ranks[a] == ranks[b]) ranks[a]++; n   } n } n void dfsutil(long long i, vector<long long> adj[], bool visited[]) { n   visited[i] = true; n   for (long long j = 0; j < adj[i].size(); j++) { n     if (!visited[adj[i][j]]) dfsutil(adj[i][j], adj, visited); n   } n } n long long dfs(vector<long long> adj[], long long present[]) { n   bool visited[26]; n   long long count = 0; n   for (long long i = 0; i < 26; i++) visited[i] = false; n   for (long long i = 0; i < 26; i++) { n     if (present[i] && !visited[i]) { n       dfsutil(i, adj, visited); n       count++; n     } n   } n   return count; n } n int32_t main() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   long long i = 0; n   long long count = 0; n   while (i < s.size()) { n     if (i % 2 == 0) { n       char c = s[i]; n       long long j = i + 1; n       while (s[j] == c && j <= s.size()) { n         s.erase(s.begin() + j); n         count++; n       } n       i = j; n     } else n       i++; n   } n   if (s.size() % 2 != 0 && count < n) { n     s.erase(s.end() - 1); n     count++; n   } n   cout << count <<   n ; n   cout << s <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 5; n int n, a[maxn], belong[maxn], bn = 1; n int ans[maxn][maxn]; n struct node { n   int l, r; n } block[maxn]; n bool cmp(node x, node y) { return (x.r - x.l) > (y.r - y.l); } n int main() { n   int l, r, flag; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i], belong[i] = 1; n   belong[0] = 1; n   block[1].l = 0; n   block[1].r = n; n   for (int i = 1; i <= n; i++) { n     sort(block + 1, block + 1 + bn, cmp); n     flag = 0; n     int bto = bn; n     for (int b = 1; b <= bto; b++) { n       l = block[b].l; n       r = block[b].r; n       if (a[i] > bto - b + 1) { n         if (flag == 0) n           while (a[i] > bto - b && l < r) ans[i][l++] = 1, a[i]--; n         else n           while (a[i] > bto - b && l <= r) ans[i][l++] = 1, a[i]--; n         if (--l < r) { n           flag = 1; n           block[++bn].l = block[b].l; n           block[bn].r = l; n           block[b].l = l + 1; n           for (int j = block[bn].l; j <= l; j++) belong[j] = bn; n         } n       } else if (a[i] > 0) { n         ans[i][l] = 1; n         a[i]--; n         if (l < r) { n           flag = 1; n           belong[l] = ++bn; n           block[bn].l = block[bn].r = l; n           block[b].l = l + 1; n         } n       } n     } n   } n   cout << bn << endl; n   for (int j = 0; j <= n; j++) { n     for (int i = 1; i <= n; i++) { n       cout << ans[i][j]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1505; n const int dx[] = {-1, 0, 1, 0}; n const int dy[] = {0, 1, 0, -1}; n int visx[MAX_N][MAX_N]; n int visy[MAX_N][MAX_N]; n char A[MAX_N][MAX_N]; n int sx, sy; n int n, m; n queue<pair<int, int> > q; n bool in(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> A[i]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (A[i][j] ==  S ) { n         A[i][j] =  . ; n         sx = i, sy = j; n       } n     } n   } n   sx += n * m; n   sy += n * m; n   q.push(make_pair(sx, sy)); n   memset(visx, -1, sizeof(visx)); n   memset(visy, -1, sizeof(visy)); n   while (!q.empty()) { n     pair<int, int> t = q.front(); n     q.pop(); n     if (A[(t.first + n) % n][(t.second + m) % m] ==  # ) continue; n     if (visx[(t.first + n) % n][(t.second + m) % m] == t.first && n         visy[(t.first + n) % n][(t.second + m) % m] == t.second) n       continue; n     if (visx[(t.first + n) % n][(t.second + m) % m] == -1) n       visx[(t.first + n) % n][(t.second + m) % m] = t.first; n     if (visy[(t.first + n) % n][(t.second + m) % m] == -1) n       visy[(t.first + n) % n][(t.second + m) % m] = t.second; n     if (visx[(t.first + n) % n][(t.second + m) % m] != t.first || n         visy[(t.first + n) % n][(t.second + m) % m] != t.second) { n       cout <<  Yes  << endl; n       return 0; n     } n     for (int i = 0; i < 4; i++) { n       int nx = dx[i] + t.first; n       int ny = dy[i] + t.second; n       q.push(make_pair(nx, ny)); n     } n   } n   cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long fac(long long n) { n   if (n == 1) return 1; n   return n * fac(n - 1); n } n long long power(long long x, long long y) { n   long long res = 1; n   for (long long i = 1; i <= y; i++) res = (res * x) % 1000000007; n   return res % 1000000007; n } n int32_t main() { n   long long T = 1; n   while (T--) { n     long long n; n     cin >> n; n     long long a[n]; n     long long e = 0, o = 0; n     for (long long i = 0; i < n; i++) { n       cin >> a[i]; n       if (i % 2 == 0) n         e += a[i]; n       else n         o += a[i]; n     } n     long long ans = 0; n     for (long long i = 0; i < n; i++) { n       if (i % 2 == 0) { n         if (e - a[i] == o) ans++; n         e -= a[i]; n         o += a[i]; n       } else { n         if (o - a[i] == e) ans++; n         e += a[i]; n         o -= a[i]; n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, a, b, ans; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k >> a >> b; n   if (k == 1) { n     cout << (n - 1) * a; n     return 0; n   } n   while (n > 1) { n     if (n % k != 0 && n < k) { n       ans += (n - 1) * a; n       n = 1; n     } else if (n % k != 0) { n       ans += (n % k) * a; n       n -= (n % k); n     } else { n       long long temp, temp2, val; n       val = n / k; n       temp = (n - val) * a; n       ans += min(temp, b); n       n = val; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = 3.14159265358979323846L; n const long double E = 2.71828182845904523536L; n const int INF = (1 << 30) - 1; n const long long LINF = 1e18; n const long double eps = 1e-7; n const long long mod = 1e9 + 7; n unordered_map<long long, long long> u[65][10]; n vector<pair<int, int> > v; n long long ans[65]; n int k, n; n unordered_map<long long, int> match; n unordered_set<long long> skip; n long long solve(long long idx, int lvl, int c) { n   if (u[lvl][c].count(idx)) { n     return u[lvl][c][idx]; n   } n   long long& r = u[lvl][c][idx]; n   if (match.count(idx)) { n     if (match[idx] != c) { n       r = 0; n       return r; n     } n   } n   if (lvl == k) { n     r = 1; n     return 1; n   } n   if (!skip.count(idx)) { n     r = ans[lvl]; n     return r; n   } n   long long m1 = 0, m2 = 0; n   for (int j = 0; j < 6; j++) { n     if (j == c || j == (c ^ 1)) { n       continue; n     } n     m1 += solve(idx * 2, lvl + 1, j); n     m2 += solve(idx * 2 + 1, lvl + 1, j); n   } n   m1 %= mod; n   m2 %= mod; n   r = m1 * m2 % mod; n   return r; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   std::cout << std::fixed << std::setprecision(20); n   cin >> k >> n; n   unordered_map<string, int> g; n   g[ white ] = 0; n   g[ yellow ] = 1; n   g[ green ] = 2; n   g[ blue ] = 3; n   g[ red ] = 4; n   g[ orange ] = 5; n   for (int i = 0; i < n; i++) { n     long long x; n     string s; n     cin >> x >> s; n     match[x] = g[s]; n     while (x) { n       skip.insert(x); n       x /= 2; n     } n   } n   ans[k] = 1; n   for (int i = k - 1; i >= 1; i--) { n     ans[i] = (4 * ans[i + 1]) % mod; n     ans[i] = (ans[i] * ans[i]) % mod; n   } n   long long r = 0; n   for (int i = 0; i < 6; i++) { n     r += solve(1, 1, i); n   } n   cout << r % mod <<   n ; n } n