#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   return (b == 0LL ? a : gcd(b, a % b)); n } n long double dist(long double x, long double arayikhalatyan, long double x2, n                  long double y2) { n   return sqrt((x - x2) * (x - x2) + n               (arayikhalatyan - y2) * (arayikhalatyan - y2)); n } n long long int S(long long int a) { return (a * (a + 1LL)) / 2LL; } n mt19937 rnd(363542); n char vow[] = { a ,  e ,  i ,  o ,  u }; n int dx[] = {0, -1, 0, 1, -1, -1, 1, 1, 0}; n int dy[] = {-1, 0, 1, 0, -1, 1, -1, 1, 0}; n const int N = 5e5 + 30; n const long long int mod = 998244353; n const long double pi = acos(-1); n const long double e = 1e-13; n long long int bp(long long int a, long long int b = mod - 2LL) { n   long long int ret = 1; n   while (b) { n     if (b & 1) ret *= a, ret %= mod; n     a *= a; n     a %= mod; n     b >>= 1; n   } n   return ret; n } n ostream& operator<<(ostream& c, pair<int, int> a) { n   c << a.first <<     << a.second; n   return c; n } n struct pt { n   long long int x, y; n   int ind; n   int bl; n   bool operator==(const pt& a) { return ind == a.ind; } n   bool operator<(const pt& b) { n     if (x == b.x) return y < b.y; n     return x < b.x; n   } n }; n int n, m, t, k; n vector<int> g[N]; n int sz[N]; n unordered_map<int, int> mp[N]; n bool stg(int v) { n   vector<int> fp; n   for (int p : g[v]) n     if (sz[p] >= k - 1) fp.push_back(p); n   for (int i = 0; i < fp.size(); i++) n     for (int j = i + 1; j < fp.size(); j++) n       if (!mp[fp[i]][fp[j]]) return 0; n   return 1; n } n void cl() { n   for (int i = 1; i <= n; i++) { n     mp[i].clear(); n     g[i].clear(); n     sz[i] = 0; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> t; n   while (t--) { n     cin >> n >> m >> k; n     for (int i = 0; i < m; i++) { n       int a, b; n       cin >> a >> b; n       mp[a][b] = 1; n       mp[b][a] = 1; n       g[a].push_back(b); n       g[b].push_back(a); n     } n     if (k > 500) { n       cout << -1 << endl; n       cl(); n       continue; n     } n     queue<int> q, q1; n     for (int i = 1; i <= n; i++) { n       sz[i] = g[i].size(); n       if (sz[i] < k) q.push(i); n     } n     int qn = n; n     while (q.size()) { n       int v = q.front(); n       qn--; n       sz[v] = 0; n       q.pop(); n       for (auto p : g[v]) { n         sz[p]--; n         if (sz[p] == k - 1) q.push(p); n       } n     } n     if (qn) { n       cout << 1 <<     << qn << endl; n       for (int i = 1; i <= n; i++) n         if (sz[i] >= k) cout << i <<    ; n       cl(); n       cout <<   n ; n       continue; n     } n     for (int i = 1; i <= n; i++) { n       sz[i] = g[i].size(); n       if (sz[i] < k - 1) n         q.push(i); n       else if (sz[i] == k - 1) n         q1.push(i); n     } n     bool bl = 0; n     while ((int)q.size() + (int)q1.size()) { n       if (q.size()) { n         int v = q.front(); n         sz[v] = 0; n         q.pop(); n         for (auto p : g[v]) { n           sz[p]--; n           if (sz[p] == k - 1) n             q1.push(p); n           else if (sz[p] == k - 2) n             q.push(p); n         } n       } else { n         int v = q1.front(); n         q1.pop(); n         if (sz[v] < k - 1) { n           continue; n         } n         if (stg(v)) { n           cout << 2 << endl; n           cout << v <<    ; n           for (int p : g[v]) n             if (sz[p] >= k - 1) cout << p <<    ; n           cout <<   n ; n           bl = 1; n           break; n         } n         q.push(v); n       } n     } n     if (!bl) cout << -1 << endl; n     cl(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n const int size = 31; n const int mod = 1e8 + 7; n long long hash1[maxn], jec[maxn], hash2[maxn]; n int cnt[maxn]; n char s[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   scanf( %s , s + 1); n   jec[0] = 1; n   for (int i = 1; i <= n; ++i) jec[i] = jec[i - 1] * size % mod; n   for (int i = 1; i <= n; ++i) cnt[i] = cnt[i - 1] + !(s[i] -  0 ); n   for (int i = 1; i <= n; ++i) { n     if (s[i] ==  0 ) { n       hash1[i] = hash1[i - 1] * size + ((i & 1) +  0 ); n       hash2[i] = hash2[i - 1] * size + !(i & 1) +  0 ; n       hash1[i] %= mod; n       hash2[i] %= mod; n     } else { n       hash1[i] = hash1[i - 1]; n       hash2[i] = hash2[i - 1]; n     } n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int l, r, len; n     scanf( %d%d%d , &l, &r, &len); n     long long p, pp; n     if (l & 1) n       p = (hash1[l + len - 1] - n            hash1[l - 1] * jec[cnt[l + len - 1] - cnt[l - 1]] % mod + mod) % n           mod; n     else n       p = (hash2[l + len - 1] - n            hash2[l - 1] * jec[cnt[l + len - 1] - cnt[l - 1]] % mod + mod) % n           mod; n     if (r & 1) n       pp = (hash1[r + len - 1] - n             hash1[r - 1] * jec[cnt[r + len - 1] - cnt[r - 1]] % mod + mod) % n            mod; n     else n       pp = (hash2[r + len - 1] - n             hash2[r - 1] * jec[cnt[r + len - 1] - cnt[r - 1]] % mod + mod) % n            mod; n     if (p == pp) n       printf( Yes n ); n     else n       printf( No n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n const int FFTMOD = 119 << 23 | 1; n const int INF = (int)1e9 + 23111992; n const long long LINF = (long long)1e18 + 23111992; n const long double PI = acos((long double)-1); n const long double EPS = 1e-9; n inline long long gcd(long long a, long long b) { n   long long r; n   while (b) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n inline long long fpow(long long n, long long k, int p = MOD) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n template <class T> n inline int chkmin(T& a, const T& val) { n   return val < a ? a = val, 1 : 0; n } n template <class T> n inline int chkmax(T& a, const T& val) { n   return a < val ? a = val, 1 : 0; n } n inline unsigned long long isqrt(unsigned long long k) { n   unsigned long long r = sqrt(k) + 1; n   while (r * r > k) r--; n   return r; n } n inline long long icbrt(long long k) { n   long long r = cbrt(k) + 1; n   while (r * r * r > k) r--; n   return r; n } n inline void addmod(int& a, int val, int p = MOD) { n   if ((a = (a + val)) >= p) a -= p; n } n inline void submod(int& a, int val, int p = MOD) { n   if ((a = (a - val)) < 0) a += p; n } n inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } n inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } n inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; } n inline int sign(long double x, long double y) { return sign(x - y); } n mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count()); n inline int mrand() { return abs((int)mt()); } n void chemthan() { n   int n; n   cin >> n; n   vector<long long> a(n); n   for (int i = (0); i < (n); ++i) cin >> a[i]; n   vector<int> vals; n   for (int i = (0); i < (n); ++i) vals.push_back(i); n   vector<int> pv_order(n), dp(n + 1); n   for (int k = (1); k < (63); ++k) { n     sort((vals).begin(), (vals).end(), [&](int u, int v) { n       return make_pair(a[u] & (1LL << k) - 1, pv_order[u]) < n              make_pair(a[v] & (1LL << k) - 1, pv_order[v]); n     }); n     int x = 0, y = 0; n     for (int i = (0); i < (n); ++i) { n       if (!(((a[i]) >> (k - 1)) & 1)) { n         x++; n       } n     } n     vector<int> st(n + 1 << 1); n     auto upd = [&](int p, int val) { n       for (st[p += n + 1] = val; 1 < p;) n         p >>= 1, st[p] = min(st[p << 1], st[p << 1 | 1]); n     }; n     auto query = [&](int l, int r) { n       int res = INF; n       for (l += n + 1, r += n + 2; l < r; l >>= 1, r >>= 1) { n         if (l & 1) chkmin(res, st[l++]); n         if (r & 1) chkmin(res, st[--r]); n       } n       return res; n     }; n     for (int i = (0); i < (n + 1); ++i) upd(i, dp[i]); n     vector<int> ndp(n + 1); n     int lo = 0; n     int hi = n; n     if (1 < k && !(((a[vals[0]]) >> (k - 1)) & 1)) { n       hi = pv_order[vals[0]] - 1; n     } n     ndp[0] = x + (1 < k ? query(lo, hi) : 0); n     for (int i = (1); i < (n + 1); ++i) { n       int lo = pv_order[vals[i - 1]]; n       int hi = n; n       if (i < n && (((a[vals[i - 1]]) >> (k - 1)) & 1) == n                        (((a[vals[i]]) >> (k - 1)) & 1)) { n         hi = pv_order[vals[i]] - 1; n       } n       if (!(((a[vals[i - 1]]) >> (k - 1)) & 1)) { n         x--; n         ndp[i] = x + (1 < k ? query(lo, hi) : 0); n         if (i == n || (((a[vals[i]]) >> (k - 1)) & 1)) { n           chkmin(ndp[i], n - y + (1 < k ? query(0, hi) : 0)); n         } n       } else { n         y++; n         ndp[i] = n - y + (1 < k ? query(lo, hi) : 0); n       } n     } n     for (int i = (0); i < (n); ++i) { n       pv_order[vals[i]] = i + 1; n     } n     dp = ndp; n   } n   cout << dp.back() <<   n ; n } n int main(int argc, char* argv[]) { n   ios_base::sync_with_stdio(0), cin.tie(0); n   if (argc > 1) { n     assert(freopen(argv[1],  r , stdin)); n   } n   if (argc > 2) { n     assert(freopen(argv[2],  wb , stdout)); n   } n   chemthan(); n   cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const pair<int, int> queen[8] = {pair<int, int>(-1, 0), pair<int, int>(-1, 1), n                                  pair<int, int>(0, 1),  pair<int, int>(1, 1), n                                  pair<int, int>(1, 0),  pair<int, int>(1, -1), n                                  pair<int, int>(0, -1), pair<int, int>(-1, -1)}; n const int maxN = 1e5 + 11; n int N, A[maxN], BIT[maxN]; n vector<int> pos[maxN]; n pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) { n   return pair<int, int>(a.first + b.first, a.second + b.second); n } n void update(int x, int v) { n   for (int i = x; i <= N; i += i & (i ^ (i - 1))) BIT[i] += v; n } n int get_BIT(int x) { n   int res = 0; n   for (int i = x; i >= 1; i -= i & (i ^ (i - 1))) res += BIT[i]; n   return res; n } n int get_BIT(int l, int r) { return get_BIT(r) - get_BIT(l - 1); } n int main() { n   cin >> N; n   for (int i = 1; i <= N; i++) cin >> A[i], pos[A[i]].push_back(i); n   long long ans = 0; n   for (int i = 1; i <= N; i++) update(i, 1); n   for (int i = 1, p = 1; i <= 1e5; i++) { n     if (pos[i].empty()) continue; n     int q = lower_bound(pos[i].begin(), pos[i].end(), p) - pos[i].begin(); n     for (int j = 0; j < (int)pos[i].size(); j++) { n       q %= (int)pos[i].size(); n       if (p <= pos[i][q]) n         ans += get_BIT(p, pos[i][q]); n       else n         ans += get_BIT(1, pos[i][q]) + get_BIT(p, N); n       update(pos[i][q], -1); n       p = pos[i][q]; n       q++; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n long long N = 10000000000000000; n int points = 0; n int main() { n   long long xo, yo, ax, ay, bx, by; n   cin >> xo >> yo >> ax >> ay >> bx >> by; n   long long x, y, t; n   cin >> x >> y >> t; n   long long X[100], Y[100]; n   X[0] = xo, Y[0] = yo; n   for (int i = 1; i < 55; i++) { n     X[i] = ax * X[i - 1] + bx; n     Y[i] = ay * Y[i - 1] + by; n     points++; n     if (X[i] >= N || Y[i] >= N) break; n   } n   long long maxi = 0; n   for (int i = 0; i <= points; i++) { n     for (int j = 0; j <= points; j++) { n       long long tmpx = X[i], tmpy = Y[i], tt = t, ans = 0; n       tt -= abs(X[i] - x) + abs(y - Y[i]); n       if (tt >= 0) ans = 1; n       int k = i + 1; n       if (i > j) k = i - 1; n       for (;;) { n         if ((k > i && k > j) || (k < i && k < j)) break; n         long long curx = X[k], cury = Y[k]; n         tt -= abs(tmpx - curx) + abs(tmpy - cury); n         tmpx = curx, tmpy = cury; n         if (tt < 0) break; n         ans++; n         if (i <= j) n           k++; n         else n           k--; n       } n       maxi = max(maxi, ans); n     } n   } n   cout << maxi << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const int maxn = 1500 + 5; n int count(vector<int>& rec, vector<int>& temp, int l, int h) { n   if (h - l < 1) return 0; n   int m = (l + h) / 2; n   int ans = count(rec, temp, l, m) + count(rec, temp, m + 1, h); n   int a = l, b = m + 1; n   int i = l; n   for (i = l; i < h; ++i) { n     if (a > m || b > h) break; n     if (temp[a] > temp[b]) { n       ans += m - a + 1; n       rec[i] = temp[b]; n       ++b; n     } else { n       rec[i] = temp[a]; n       ++a; n     } n   } n   while (a <= m) { n     rec[i++] = temp[a++]; n   } n   while (b <= h) { n     rec[i++] = temp[b++]; n   } n   for (int i = l; i <= h; ++i) { n     temp[i] = rec[i]; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<int> pr(n), tem(n); n   for (int i = 0; i < n; ++i) { n     cin >> pr[i]; n     tem[i] = pr[i]; n   } n   int toi = count(pr, tem, 0, n - 1); n   toi %= 2; n   int m; n   cin >> m; n   while (m--) { n     int l, r; n     cin >> l >> r; n     int le = r - l + 1; n     int res = le * (le - 1) / 2; n     toi += res; n     toi %= 2; n     if (toi == 0) n       cout <<  even n ; n     else n       cout <<  odd n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e8 + 5; n long long MAX = 1e9; n long long MOD = 1e9 + 7; n long long Mod = 1e9 + 7; n double EPS = 0.0001; n long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } n long long lcm(long long a, long long b) { return ((a * b) / gcd(a, b)); } n const int dx[] = {1, -1, 0, 0, 0, 0}; n const int dy[] = {0, 0, 1, -1, 0, 0}; n int main() { n   int wait; n   long long n, k; n   int prin = 0; n   scanf( %I64d , &n); n   scanf( %I64d , &k); n   long long check = k * (k - 1); n   if (check < n) { n     cout <<  NO n ; n     return 0; n   } n   cout <<  YES n ; n   for (long long i = 1; i < k + 1; i++) { n     for (long long j = i + 1; j < k + 1; j++) { n       cout << i <<     << j << endl; n       prin++; n       if (prin == n) return 0; n       cout << j <<     << i << endl; n       prin++; n       if (prin == n) return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1000000LL * 1000000LL * 1000000LL; n int n, k; n int tyl, l, sum; n int T[1000][1000]; n int main() { n   scanf( %d %d , &n, &k); n   tyl = n * n; n   for (int i = 1; i <= n; i++) { n     for (int j = n; j >= k; j--) { n       T[i][j] = tyl; n       tyl--; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (j == k) { n         sum += T[i][j]; n       } n       if (T[i][j] == 0) { n         l++; n         T[i][j] = l; n       } n     } n   } n   printf( %d n , sum); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       printf( %d  , T[i][j]); n     } n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string s; n   cin >> s; n   vector<long long> cnt(26, 0); n   for (auto x : s) { n     cnt[x -  a ]++; n   } n   deque<long long> d; n   for (long long i = 0; i < 26; i++) { n     if (cnt[i]) { n       d.push_back(i); n     } n   } n   deque<long long> ans; n   if ((long long)d.size() == 2) { n     if (d[0] + 1 == d[1]) { n       cout <<  No answer  <<   n ; n       return; n     } n     ans = d; n   } else if ((long long)d.size() == 3) { n     if (d[0] + 1 == d[1] && d[1] + 1 == d[2]) { n       cout <<  No answer  <<   n ; n       return; n     } else if (d[0] + 1 == d[1]) { n       ans.push_back(d[0]); n       ans.push_back(d[2]); n       ans.push_back(d[1]); n     } else { n       ans.push_back(d[1]); n       ans.push_back(d[0]); n       ans.push_back(d[2]); n     } n   } else { n     long long fl = 1; n     while ((long long)d.size() > 1) { n       if (fl) { n         ans.push_back(d.front()); n         d.pop_front(); n       } else { n         ans.push_back(d.back()); n         d.pop_back(); n       } n       fl ^= 1; n     } n     ans.push_front(d[0]); n   } n   for (auto x : ans) { n     for (long long i = 0; i < cnt[x]; i++) { n       cout << (char)(x +  a ); n     } n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int tc; n   tc = 1; n   cin >> tc; n   for (int i = 1; i <= tc; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, -1, 0, 1}; n int dy[] = {1, 0, -1, 0}; n int c[2010][2010], edge[2010]; n long double s[2010], C[2010][2010]; n int main(int argc, char *argv[]) { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = (1); i <= (n); ++i) { n     C[i][0] = 1; n     for (int j = (1); j <= (i); ++j) C[i][j] = C[i][j - 1] * (i - j + 1) / j; n   } n   for (int i = (1); i <= (n - 1); ++i) n     for (int j = (i + 1); j <= (n); ++j) { n       scanf( %d , &c[i][j]); n       c[j][i] = c[i][j]; n     } n   long double m = 0; n   for (int i = (1); i <= (n); ++i) { n     edge[i] = 0; n     for (int j = (1); j <= (n); ++j) n       if (c[i][j] >= 0 && i != j) edge[i]++; n     s[i] = 0; n     if (edge[i] < k) continue; n     s[i] = C[edge[i]][k]; n     m += s[i]; n   } n   long double ans = 0; n   for (int i = (1); i <= (n); ++i) { n     if (edge[i] <= 0) continue; n     long double p = (s[i] * k) / (m * edge[i]); n     long double sum = 0; n     for (int j = (1); j <= (n); ++j) n       if (c[i][j] >= 0 && i != j) sum += (long double)c[i][j]; n     ans += sum * p; n   } n   ans = ans + 1e-5; n   printf( %I64d n , (long long)ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, h, a; n   vector<int> home, away; n   cin >> n; n   vector<int> cnt(100000, 0); n   vector<int> ans_home(n, 0); n   vector<int> ans_away(n, 0); n   for (int i = 0; i < n; i++) { n     cin >> h >> a; n     home.push_back(h); n     away.push_back(a); n   } n   for (int i = 0; i < n; i++) cnt[home[i] - 1]++; n   for (int i = 0; i < n; i++) { n     ans_home[i] = n - 1 + cnt[away[i] - 1]; n     ans_away[i] = 2 * (n - 1) - ans_home[i]; n   } n   for (int i = 0; i < n; i++) cout << ans_home[i] <<     << ans_away[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1e9 + 7; n const int inf = 1e9; n const int N = 1e5; n vector<long long int> tot(N); n vector<bool> vis(N); n vector<int> adj[N]; n void dfs(int node) { n   vis[node] = true; n   for (auto &i : adj[node]) { n     if (!vis[i]) { n       dfs(i); n       tot[node] += tot[i]; n     } n   } n   if ((int)adj[node].size() == 1 && node != 0) tot[node]++; n } n void solve() { n   vector<tuple<int, int, long long int, int>> vc; n   int n, u, v, x; n   long long int s, w; n   cin >> n >> s; n   for (int i = 0; i < n - 1; i++) { n     cin >> u >> v >> w >> x; n     u--, v--; n     adj[u].push_back(v); n     adj[v].push_back(u); n     vc.push_back({u, v, w, x}); n   } n   dfs(0); n   int cnt = 0; n   long long int sum = 0; n   priority_queue<tuple<long long int, long long int, long long int>> q1, q2; n   for (auto &i : vc) { n     u = get<0>(i); n     v = get<1>(i); n     w = get<2>(i); n     x = get<3>(i); n     long long int tp = min(tot[u], tot[v]); n     sum += tp * w; n     if (x == 1) n       q1.push({tp * w - tp * (w / 2), tp, w / 2}); n     else n       q2.push({tp * w - tp * (w / 2), tp, w / 2}); n   } n   long long int r1, r2, r3; n   while (sum > s) { n     tuple<long long int, long long int, long long int> x; n     tuple<long long int, long long int, long long int> y; n     r1 = r2 = r3 = 0; n     if (!q1.empty()) x = q1.top(); n     if (!q2.empty()) y = q2.top(); n     if (!q1.empty()) { n       long long int a = get<0>(x); n       long long int b = get<1>(x); n       long long int c = get<2>(x); n       r1 = a; n       q1.pop(); n       if (q1.empty()) n         r3 = b * c - b * (c / 2); n       else { n         r3 = max(get<0>(q1.top()), b * c - b * (c / 2)); n       } n       q1.push(x); n     } n     if (!q2.empty()) r2 = get<0>(y); n     if (r1 >= r2 || sum - r1 <= s) { n       cnt++; n       long long int a = get<0>(x); n       long long int b = get<1>(x); n       long long int c = get<2>(x); n       sum -= a; n       q1.pop(); n       q1.push({b * c - b * (c / 2), b, c / 2}); n     } else { n       if (r1 + r3 >= r2) { n         long long int a = get<0>(x); n         long long int b = get<1>(x); n         long long int c = get<2>(x); n         sum -= a; n         q1.pop(); n         q1.push({b * c - b * (c / 2), b, c / 2}); n         cnt++; n       } else { n         long long int a = get<0>(y); n         long long int b = get<1>(y); n         long long int c = get<2>(y); n         sum -= a; n         q2.pop(); n         q2.push({b * c - b * (c / 2), b, c / 2}); n         cnt += 2; n       } n     } n   } n   cout << cnt <<   n ; n   for (int i = 0; i < n; i++) { n     tot[i] = 0; n     vis[i] = false; n     adj[i].clear(); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n void Get(int &T) { n   char C; n   bool F = 0; n   for (; C = getchar(), C <  0  || C >  9 ;) n     if (C ==  - ) F = 1; n   for (T = C -  0 ; C = getchar(), C >=  0  && C <=  9 ; T = T * 10 + C -  0 ) n     ; n   F && (T = -T); n } n int L[2005][2005]; n int R[2005][2005]; n bool Rot; n int N, M, K; n int GetL(int X, int Y) { n   if (L[X][Y] == Y) return Y; n   return L[X][Y] = GetL(X, L[X][Y]); n } n int GetR(int X, int Y) { n   if (R[X][Y] == Y) return Y; n   return R[X][Y] = GetR(X, R[X][Y]); n } n void Init() { n   Get(N); n   Get(M); n   Get(K); n   if (N > M) { n     std::swap(N, M); n     Rot = 1; n   } n   for (int i = 1; i <= N; i++) n     for (int j = 1; j <= M + 1; j++) L[i][j] = R[i][j] = j; n } n int Ans; n int AnsX, AnsY; n int X0, Y0; n void Fresh(int X, int Y) { n   if (X < 1 || X > N) return; n   if (Y < 1 || Y > M) return; n   int Dis = std::abs(X - X0) + std::abs(Y - Y0); n   if (Rot) { n     std::swap(X, Y); n   } n   if (Ans > Dis) { n     Ans = Dis; n     AnsX = X; n     AnsY = Y; n   } else if (Ans == Dis) { n     if (AnsX > X) { n       AnsX = X; n       AnsY = Y; n     } else if (AnsX == X && AnsY > Y) { n       AnsX = X; n       AnsY = Y; n     } n   } n } n void Work() { n   for (int k = 1; k <= K; k++) { n     Ans = 0x2f2f2f2f; n     Get(X0); n     Get(Y0); n     if (Rot) std::swap(X0, Y0); n     for (int d = 0; d <= Ans; d++) { n       int X; n       int Y; n       if (X0 - d > 0) { n         X = X0 - d; n         Y = L[X][Y0]; n         Fresh(X, Y); n         Y = R[X][Y0]; n         Fresh(X, Y); n       } n       if (X0 + d <= N) { n         X = X0 + d; n         Y = L[X][Y0]; n         Fresh(X, Y); n         Y = R[X][Y0]; n         Fresh(X, Y); n       } n     } n     printf( %d %d n , AnsX, AnsY); n     if (Rot) { n       std::swap(AnsX, AnsY); n     } n     L[AnsX][AnsY] = L[AnsX][AnsY - 1]; n     R[AnsX][AnsY] = R[AnsX][AnsY + 1]; n     for (int j = AnsY + 1; L[AnsX][j] != j; j++) L[AnsX][j] = L[AnsX][AnsY]; n     for (int j = AnsY - 1; L[AnsX][j] != j; j--) R[AnsX][j] = R[AnsX][AnsY]; n   } n } n int main() { n   Init(); n   Work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 0x3f3f3f3f; n const double eps = 1e-6; n const long long mod = 1000000007; n inline string getstr(string &s, long long l, long long r) { n   string ret =   ; n   for (long long i = l; i <= r; i++) ret.push_back(s[i]); n   return ret; n } n long long modpow(long long x, long long y, long long md = mod) { n   long long ret = 1; n   do { n     if (y & 1) ret = (long long)ret * x % md; n     x = (long long)x * x % md; n   } while (y >>= 1); n   return ret; n } n long long n, mn; n vector<pair<long long, long long> > g[3003]; n long long dfs(long long x, long long p, long long cur, long long mv) { n   long long ret = 0; n   mn = min(mn, cur - mv); n   for (auto to : g[x]) n     if (to.first != p) { n       ret += (to.second > 0); n       ret += dfs(to.first, x, cur - to.second, max(mv, cur - to.second)); n     } n   return ret; n } n signed main() { n   cin >> n; n   for (long long i = 1; i < n; i++) { n     long long x, y; n     cin >> x >> y; n     g[x].push_back(make_pair(y, -1)); n     g[y].push_back(make_pair(x, 1)); n   } n   long long ans = 1e9; n   for (long long i = 1; i <= n; i++) { n     mn = inf; n     ans = min(ans, dfs(i, -1, 0, 0) + mn); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<pair<int, int>> vp; n int chk() { n   bool ok = true; n   int d = vp[2].first - vp[1].first; n   for (int i = 2; i < n; i++) n     if (vp[i].first - vp[i - 1].first != d) ok = false; n   if (ok) { n     printf( %d n , vp[0].second + 1); n     exit(0); n   } n   d = vp[2].first - vp[0].first; n   ok = true; n   for (int i = 3; i < n; ++i) n     if (vp[i].first - vp[i - 1].first != d) ok = false; n   if (ok) { n     printf( %d n , vp[1].second + 1); n     exit(0); n   } n   d = vp[1].first - vp[0].first; n   int cnt = 0, index = 0; n   ok = true; n   for (int i = 1; i < n; i++) { n     if (vp[i].first - vp[i - 1].first == d) continue; n     cnt++; n     if (i == n - 1 or vp[i + 1].first - vp[i - 1].first != d) ok = false; n     index = vp[i].second; n     i++; n   } n   if (ok and cnt == 1) { n     cout << index + 1 << endl; n     exit(0); n   } n } n int main() { n   scanf( %d , &n); n   vp.resize(n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &vp[i].first); n     vp[i].second = i; n   } n   sort(begin(vp), end(vp)); n   chk(); n   if (n < 4) return !printf( 1 n ); n   reverse(begin(vp), end(vp)); n   chk(); n   cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int buff1, buff2; n void solution(vector<int> v) { n   map<int, int> counter; n   for (int n : v) counter[n]++; n   vector<int> counts; n   for (auto it : counter) { n     counts.push_back(it.second); n   } n   sort(counts.begin(), counts.end(), greater<int>()); n   int cur = INT_MAX, ret = 0; n   for (int n : counts) { n     if (cur <= 1) { n       break; n     } n     cur = min(cur - 1, n); n     ret += cur; n   } n   cout << ret << endl; n } n int main() { n   int nTest; n   cin >> nTest; n   for (int i = 0; i < nTest; i++) { n     int N; n     cin >> N; n     vector<int> v; n     for (int i = 0; i < N; i++) { n       cin >> buff1; n       v.push_back(buff1); n     } n     solution(v); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n int ss[250000], ff[250000], cc[250000], rr[250000], ii[250000]; n int compare(const void *a, const void *b) { n   int i = *(int *)a; n   int j = *(int *)b; n   return rr[i] - rr[j]; n } n int main() { n   static int aa[400]; n   typedef int ian[400]; n   static ian dp_[400], dq_[400], *dp = dp_, *dq = dq_, *tmp; n   int n, m, h, h_, i, r, s, f; n   long long v; n   scanf( %d%d , &n, &m); n   for (i = 0; i < n; i++) scanf( %d , &aa[i]); n   for (h = 0; h < m; h++) { n     scanf( %d%d%d%d , &ss[h], &ff[h], &cc[h], &rr[h]); n     ss[h]--; n     ff[h]--; n     ii[h] = h; n   } n   qsort(ii, m, sizeof *ii, compare); n   for (s = 0; s < n; s++) n     for (f = s + 1; f < n; f++) dp[s][f] = aa[f] - aa[s]; n   v = 0; n   for (r = 0, h_ = 0; r <= n; r++) { n     while (h_ < m && rr[h = ii[h_]] == r) { n       long long x; n       if (v < (x = (long long)dp[ss[h]][ff[h]] * cc[h])) v = x; n       h_++; n     } n     for (s = 0; s < n; s++) { n       i = s; n       for (f = s + 1; f < n; f++) { n         while (i < f && dp[s][i] < aa[f] - aa[i]) i++; n         dq[s][f] = aa[f] - aa[i - 1]; n         if (dq[s][f] > dp[s][i]) dq[s][f] = dp[s][i]; n       } n     } n     tmp = dp; n     dp = dq; n     dq = tmp; n   } n   printf( %lld n , v); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int ans; n inline int f(int n) { n   int rt = 0; n   for (int i = 0; n / pow(k, i) != 0; i++) rt += n / pow(k, i); n   return rt; n } n void bin_search() { n   int s = 1, e = n; n   int m; n   while (s <= e) { n     m = (s + e) / 2; n     if (f(m) >= n) { n       e = m - 1; n       ans = m; n     } else n       s = m + 1; n   } n } n int main() { n   cin >> n >> k; n   bin_search(); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long f[200][200], a[200], x[200], y[200], d; n int n; n long long dist(int i, int j) { return abs(x[i] - x[j]) + abs(y[i] - y[j]); } n int main() { n   cin >> n >> d; n   for (int i = 2; i <= n - 1; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> x[i] >> y[i]; n   memset(f, 61, sizeof(f)); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (i != j) f[i][j] = dist(i, j) * d - a[i]; n   for (int i = 1; i <= n; i++) f[i][i] = 0; n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) f[i][j] = min(f[i][j], f[i][k] + f[k][j]); n   if (f[1][n] <= 0) n     cout << 0; n   else n     cout << f[1][n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 4e5 + 10; n int sum[mx]; n int x[mx]; n int n, m; n void add(int x) { n   while (x <= 2 * n + 1) { n     sum[x]++; n     x += x & (-x); n   } n } n int query(int x) { n   int ans = 0; n   while (x) { n     ans += sum[x]; n     x -= x & (-x); n   } n   return ans; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &x[i]); n   int cnt = n + 1; n   long long int a = 0, b = 0; n   add(cnt); n   for (int i = 1; i <= n; i++) { n     if (x[i] < m) n       cnt--; n     else n       cnt++; n     a += query(cnt - 1); n     add(cnt); n   } n   cnt = n + 1; n   memset(sum, 0, sizeof(sum)); n   add(cnt); n   for (int i = 1; i <= n; i++) { n     if (x[i] <= m) n       cnt--; n     else n       cnt++; n     b += query(cnt - 1); n     add(cnt); n   } n   printf( %I64d n , a - b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void topsort(long long int s, vector<vector<long long int>> &graph, n              vector<long long int> &vis, vector<long long int> &ans) { n   vis[s] = 1; n   for (long long int i = 0; i < graph[s].size(); i++) { n     if (vis[graph[s][i]] == 0) topsort(graph[s][i], graph, vis, ans); n   } n   ans.push_back(s); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n; n   cin >> n; n   vector<long long int> v; n   for (long long int i = 0; i < n; i++) { n     long long int num; n     cin >> num; n     v.push_back(num); n   } n   vector<vector<long long int>> graph(n); n   for (long long int i = 0; i < n; i++) { n     for (long long int j = 0; j < n; j++) { n       if ((v[i] % 3 == 0 && v[j] == v[i] / 3) || v[j] == v[i] * 2) n         graph[i].push_back(j); n     } n   } n   vector<long long int> ans; n   vector<long long int> vis(n, 0); n   for (long long int i = 0; i < n; i++) { n     if (vis[i] == 0) topsort(i, graph, vis, ans); n   } n   reverse(ans.begin(), ans.end()); n   for (long long int i = 0; i < ans.size(); i++) { n     cout << v[ans[i]] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 4e5 + 11; n set<int> ans; n int n, m, len; n int a[N], b[N], c[N], p[N]; n inline void manacher() { n   c[0] = -2; n   for (register int i = 0; i <= n + n - 2; ++i) c[++len] = b[i], c[++len] = -1; n   c[len + 1] = -3; n   int id = 0, mx = 0; n   for (register int i = 1; i <= len; ++i) { n     if (mx > i) n       p[i] = min(mx - i, p[id + id - i]); n     else n       p[i] = 1; n     while (c[i + p[i]] == c[i - p[i]]) ++p[i]; n     if (p[i] + i > mx) id = i, mx = i + p[i]; n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (register int i = 0; i <= n - 1; ++i) scanf( %d , a + i); n   sort(a, a + n); n   for (register int i = 1; i <= n; ++i) n     b[i - 1] = (a[i % n] - a[i - 1] + m) % m; n   for (register int i = 0; i <= n - 1; ++i) b[i + n] = b[i]; n   manacher(); n   for (int i = 2; i <= len; i += 2) n     if (p[i] / 2 >= n / 2 && n         (a[(i / 2 - 1 + n) % n] + a[(i / 2 + 1) % n]) % m == n             a[i / 2 % n] * 2 % m) n       ans.insert(a[i / 2 % n] * 2 % m); n   if (n % 2 == 0) { n     for (int i = 1; i <= len; i += 2) n       if (p[i] >= n) ans.insert((a[i / 2 % n] + a[(i / 2 + 1) % n]) % m); n   } n   printf( %d n , ans.size()); n   for (auto v : ans) printf( %d  , v); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000000 + 1; n int a[MAXN], parent[MAXN], rnk[MAXN], p[10 * MAXN]; n int n; n void make_set(int v) { n   parent[v] = v; n   rnk[v] = 0; n } n int find_set(int v) { n   if (v == parent[v]) return v; n   return parent[v] = find_set(parent[v]); n } n void union_sets(int u, int v) { n   u = find_set(u); n   v = find_set(v); n   if (u != v) { n     if (rnk[u] < rnk[v]) swap(u, v); n     parent[v] = u; n     if (rnk[u] == rnk[v]) ++rnk[u]; n   } n } n int gcd(int x, int y) { return x ? gcd(y % x, x) : y; } n bool check(int m[3]) { n   return gcd(m[0], m[1]) == 1 && gcd(m[1], m[2]) == 1 && gcd(m[0], m[2]) == 1; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n     make_set(i); n     p[a[i]] = i + 1; n   } n   sort(a, a + n); n   for (int i = 2; i * i <= 2 * a[n - 1]; ++i) n     for (int j = 1; j < i; ++j) { n       int t[3] = {i * i - j * j, 2 * i * j, i * i + j * j}; n       if (!check(t)) continue; n       int pos[3]; n       for (int k = 0; k < 3; ++k) n         if (t[k] <= a[n - 1]) pos[k] = p[t[k]]; n       if (t[0] <= a[n - 1] && t[1] <= a[n - 1] && pos[0] && pos[1]) n         union_sets(pos[0] - 1, pos[1] - 1); n       if (t[1] <= a[n - 1] && t[2] <= a[n - 1] && pos[1] && pos[2]) n         union_sets(pos[1] - 1, pos[2] - 1); n       if (t[0] <= a[n - 1] && t[2] <= a[n - 1] && pos[0] && pos[2]) n         union_sets(pos[0] - 1, pos[2] - 1); n     } n   set<int> f; n   for (int i = 0; i < n; ++i) f.insert(find_set(i)); n   printf( %d , f.size()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int>> edges; n vector<int> g[300001]; n int police[300001]; n int nearest[300001]; n int stat[300001]; n void dfs1(int beta, int baap) { n   int mini = (police[beta] ? 0 : (int)1e9), minpos = (police[beta] ? beta : -1); n   for (int i = 0; i < g[beta].size(); i++) { n     int pota = g[beta][i]; n     if (pota == baap) continue; n     dfs1(pota, beta); n     if (mini > 1 + nearest[pota]) { n       mini = 1 + nearest[pota]; n       minpos = stat[pota]; n     } n   } n   nearest[beta] = mini; n   stat[beta] = minpos; n } n void dfs2(int beta, int baap) { n   int up = (baap == -1 ? (int)1e9 : 1 + nearest[baap]); n   if (up < nearest[beta]) { n     nearest[beta] = up; n     stat[beta] = stat[baap]; n   } n   for (int i = 0; i < g[beta].size(); i++) { n     int pota = g[beta][i]; n     if (pota == baap) continue; n     dfs2(pota, beta); n   } n } n int main() { n   int n, k, d; n   scanf( %d %d %d , &n, &k, &d); n   for (int i = 0; i < k; i++) { n     int x; n     scanf( %d , &x); n     police[x] = 1; n   } n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     scanf( %d %d , &x, &y); n     g[x].push_back(y); n     g[y].push_back(x); n     edges.push_back(make_pair(x, y)); n   } n   dfs1(1, -1); n   dfs2(1, -1); n   vector<int> ans; n   for (int i = 0; i < n - 1; i++) { n     int u = edges[i].first, v = edges[i].second; n     if (stat[u] != stat[v]) ans.push_back(i + 1); n   } n   printf( %d n , (int)ans.size()); n   for (int i = 0; i < ans.size(); i++) { n     printf( %d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void _split(vector<string>& vc, const string& str, char ch) { n   string temp; n   for (int i = 0; i <= str.size(); i++) n     if (i == str.size() || str[i] == ch) { n       vc.emplace_back(temp); n       temp =   ; n     } else { n       temp += str[i]; n     } n } n void _output(vector<string>::iterator _) { n   cerr <<   n ; n   cerr.flush(); n } n template <typename T, typename... Args> n void _output(vector<string>::iterator it, T val, Args... args) { n   cerr << *it <<   =   << val <<      ; n   _output(++it, args...); n } n const size_t MAX_N = 1000 + 5; n int n, a[MAX_N], b[MAX_N], cnt[MAX_N]; n bool used[MAX_N]; n inline bool check() { n   int cnt = 0; n   memset(used, 0, sizeof used); n   for (int i = 0; i < n; i++) n     if (a[i] != b[i]) cnt++; n   if (cnt != 1) return false; n   for (int i = 0; i < n; i++) used[a[i]] = true; n   for (int i = 1; i <= n; i++) n     if (!used[i]) return false; n   return true; n } n int main() { n   int diff_cnt = 0, i, j, k; n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %d , a + i); n   for (i = 0; i < n; i++) scanf( %d , b + i); n   for (i = 0; i < n; i++) diff_cnt += a[i] != b[i]; n   if (!diff_cnt) { n     for (i = 0; i < n; i++) cnt[a[i]]++; n     for (i = 0; i < n; i++) cnt[b[i]]++; n     for (j = 1; j <= n; j++) n       if (cnt[j] == 4) break; n     for (k = 1; k <= n; k++) n       if (cnt[k] == 0) break; n     for (i = 0; i < n; i++) n       if (a[i] == j) { n         printf( %d  , k); n         break; n       } else { n         printf( %d  , a[i]); n       } n     for (; i < n; i++) printf( %d  , a[i]); n   } else if (diff_cnt == 1) { n     for (i = 0; i < n; i++) n       if (a[i] == b[i]) cnt[a[i]]++; n     for (j = 1; j <= n; j++) n       if (!cnt[j]) break; n     for (i = 0; i < n; i++) n       if (a[i] == b[i]) n         printf( %d  , a[i]); n       else n         printf( %d  , j); n   } else { n     vector<int> lft, pos; n     for (i = 0; i < n; i++) n       if (a[i] == b[i]) n         cnt[a[i]]++; n       else n         pos.emplace_back(i); n     for (i = 1; i <= n; i++) n       if (!cnt[i]) lft.emplace_back(i); n     for (int x : pos) n       for (int l : lft) n         if (l != a[x]) { n           int t = a[x]; n           a[x] = l; n           if (check()) { n             for (i = 0; i < n; i++) printf( %d  , a[i]); n             return 0; n           } n           a[x] = t; n         } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long INF = 1e18; n const int MAXN = 3e5 + 10; n const int MAXM = 1e4 + 500; n const int N = 5600 + 15; n const double EPS = 1e-9; n double len(complex<long long> a) { n   return sqrt(a.real() * a.real() + a.imag() * a.imag()); n } n long long sq(complex<long long> a) { n   return a.real() * a.real() + a.imag() * a.imag(); n } n complex<long long> perp(complex<long long> p) { return {-p.imag(), p.real()}; } n long long dotProduct(complex<long long> a, complex<long long> b) { n   return (conj(a) * b).real(); n } n long long crossProduct(complex<long long> a, complex<long long> b) { n   return (conj(a) * b).imag(); n } n long long orient(complex<long long> a, complex<long long> b, n                  complex<long long> c) { n   return crossProduct(b - a, c - a); n } n bool inDisk(complex<long long> a, complex<long long> b, complex<long long> p) { n   return dotProduct(a - p, b - p) <= 0; n } n bool onSegment(complex<long long> a, complex<long long> b, n                complex<long long> p) { n   return orient(a, b, p) == 0 && inDisk(a, b, p); n }; n struct cmpX { n   bool operator()(complex<long long> a, complex<long long> b) { n     return make_pair(a.real(), a.imag()) < make_pair(b.real(), b.imag()); n   } n }; n struct line { n   complex<long long> v; n   long long c; n   line(complex<long long> v, long long c) : v(v), c(c) {} n   line(long long a, long long b, long long c) : v({b, -a}), c(c) {} n   line(complex<long long> p, complex<long long> q) n       : v(q - p), c(crossProduct(v, p)) {} n   long long side(complex<long long> p) { return crossProduct(v, p) - c; } n   double dist(complex<long long> p) { return abs(side(p)) / len(v); } n   line perpThrough(complex<long long> p) { return {p, p + perp(v)}; } n   bool cmpProj(complex<long long> p, complex<long long> q) { n     return dotProduct(v, p) < dotProduct(v, q); n   } n   line translate(complex<long long> t) { return {v, c + crossProduct(v, t)}; } n   complex<long long> proj(complex<long long> p) { n     return p - perp(v) * side(p) / sq(v); n   } n }; n double segPoint(complex<long long> a, complex<long long> b, n                 complex<long long> p) { n   if (a != b) { n     line l(a, b); n     if (l.cmpProj(a, p) && l.cmpProj(p, b)) return l.dist(p); n   } n   return min(abs(p - a), abs(p - b)); n } n bool inAngle(complex<long long> a, complex<long long> b, complex<long long> c, n              complex<long long> p) { n   if (orient(a, b, c) < 0) swap(b, c); n   return orient(a, b, p) >= 0 && orient(a, c, p) <= 0; n } n bool isInsidePol(vector<complex<long long> > &a, complex<long long> b) { n   int n = a.size(); n   int pos = 0, neg = 0; n   for (int i = 0; i < n - 1; i++) { n     if (onSegment(a[i], a[(i + 1) % n], b)) return false; n     line l(a[i], a[i + 1]); n     if (l.side(b) > 0) n       pos++; n     else n       neg++; n   } n   line l(a[n - 1], a[0]); n   if (l.side(b) > 0) n     pos++; n   else n     neg++; n   return ((!pos && neg == n) || (!neg && pos == n)); n } n long long i, j, k; n long long n, m, t; n long long binpow(long long val, long long deg) { n   if (!deg) return 1; n   if (deg & 1) return binpow(val, deg - 1) * val % MOD; n   long long res = binpow(val, deg >> 1); n   return (res * res) % MOD; n } n long long inv(long long a, long long m) { return binpow(a, m - 2); } n long long add(long long a, long long b) { return (a += b) < MOD ? a : a - MOD; } n long long mul(long long a, long long b) { return a * b % MOD; } n long long cleverMulMod(long long a, long long b) { n   if (b == 0) return 0; n   long long s = cleverMulMod(a, b / 2); n   if (b % 2 == 1) n     return (a % MOD + 2ll * (s % MOD)) % MOD; n   else n     return (2ll * (s % MOD)) % MOD; n } n class Matrix { n  public: n   vector<vector<long long> > A; n   int dim; n   Matrix(int n) : dim(n) { A.assign(n, vector<long long>(n)); } n   vector<long long> &operator[](int n) { return A[n]; } n }; n Matrix operator*(Matrix A, Matrix B) { n   int n = A.dim; n   Matrix C(n); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       for (int k = 0; k < n; k++) { n         C[i][j] = add(C[i][j], mul(A[i][k], B[k][j])); n       } n   return C; n } n Matrix operator^(Matrix A, long long k) { n   int n = A.dim; n   Matrix R(n); n   for (int i = 0; i < n; i++) R[i][i] = 1; n   while (k > 0) { n     if (k % 2) R = R * A; n     A = A * A; n     k /= 2; n   } n   return R; n } n long long first, second, need; n bool check(long long k) { n   long long total = 2 * k * (k + 1) + 1; n   k++; n   if (k > first) { n     total -= (k - first) * (k - first); n   } n   if (k > second) { n     total -= (k - second) * (k - second); n   } n   if (k > n - first) { n     total -= (k - (n - first)) * (k - (n - first)); n   } n   if (k > n - second) { n     total -= (k - (n - second)) * (k - (n - second)); n   } n   if (k > first + second) { n     total += (k - (first + second)) * (k - (first + second) + 1) / 2; n   } n   if (k + first > n + second) { n     total += (k + first - n - second) * (k + first - n - second + 1) / 2; n   } n   if (k + first + second - n - n > 0) { n     total += n         (k + first + second - n - n) * (k + first + second - n - n + 1) / 2; n   } n   if (k + second > n + first) { n     total += (k + second - n - first) * (k + second - n - first + 1) / 2; n   } n   return total >= need; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> first >> second >> need; n   n++; n   long long left = 0, right = 1e9 + 3; n   while (right - left > 1) { n     long long mid = (right + left) / 2; n     if (check(mid)) n       right = mid; n     else n       left = mid; n   } n   if (check(left)) right = left; n   cout << right << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, long long> solve(long long n) { n   if (!n) return {0, 0}; n   long long a = 1; n   while ((a + 1) * (a + 1) * (a + 1) <= n) a++; n   pair<int, long long> r1 = solve(n - a * a * a), n                        r2 = n                            solve((a * a * a) - 1 - (a - 1) * (a - 1) * (a - 1)); n   if (r1.first > r2.first) return {1 + r1.first, r1.second + a * a * a}; n   if (r1.first < r2.first) n     return {1 + r2.first, r2.second + (a - 1) * (a - 1) * (a - 1)}; n   return {1 + r1.first, n           max(a * a * a + r1.second, (a - 1) * (a - 1) * (a - 1) + r2.second)}; n } n int main() { n   long long n; n   scanf( %lld , &n); n   pair<int, long long> r = solve(n); n   printf( %d %lld n , r.first, r.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unordered_map<int, int> X, pre; n unordered_map<int, int>::iterator it; n int Find(int u) { n   it = pre.find(u); n   if (it == pre.end()) { n     X[u] = 0; n     return u; n   } n   int v = it->second; n   int tmp = Find(v); n   X[u] ^= X[v]; n   return pre[u] = tmp; n } n bool join(int a, int b, int x) { n   int c = Find(a), d = Find(b); n   if (c == d) return false; n   pre[d] = c; n   X[d] = X[a] ^ X[b] ^ x; n   return true; n } n int main() { n   int q, last = 0; n   scanf( %d , &q); n   while (q--) { n     int tp, a, b; n     scanf( %d%d%d , &tp, &a, &b); n     a ^= last; n     b ^= last; n     if (a > b) swap(a, b); n     b++; n     if (tp == 1) { n       int x; n       scanf( %d , &x); n       join(a, b, x ^ last); n     } else { n       if (Find(a) != Find(b)) n         last = -1; n       else n         last = X[a] ^ X[b]; n       printf( %d n , last); n       last = abs(last); n     } 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[1000]; n   int m = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     m = max(m, a[i]); n   } n   for (int i = 1; i <= m; i++) { n     for (int j = 0; j < n; j++) a[j]--; n     bool f[1000]; n     f[0] = (a[0] > 0); n     for (int i = 1; i < n; i++) n       f[i] = (a[i] > 0 && (f[i - 1] || (i > 1 && f[i - 2]))); n     if (!f[n - 1]) { n       cout << i; n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tc; n   cin >> tc; n   while (tc--) { n     int test = 0; n     int t[1005]; n     int n, x, nb1 = 0, nb2 = 0, i; n     cin >> n >> x; n     for (i = 0; i < n; i++) { n       cin >> t[i]; n       if (t[i] % 2 == 0) n         nb1++; n       else n         nb2++; n     } n     if (nb1 > 0 && x - nb1 > 0 && nb2 >= x - nb1 && (x - nb1) % 2 != 0) n       cout <<  Yes  << endl; n     else if (nb1 > 0 && x - nb1 + 1 > 0 && nb2 >= x - nb1 + 1 && n              (x - nb1 + 1) % 2 != 0) n       cout <<  Yes  << endl; n     else if (nb1 > 0 && x - nb1 <= 0 && nb2 >= 1) n       cout <<  Yes  << endl; n     else if (nb1 == 0 && x % 2 != 0 && nb2 >= x) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char a[55][55]; n int n, m, x; n int dist(int p, int q, int r, int s) { n   return (p - r) * (p - r) + (q - s) * (q - s); n } n int ok[128] = {}, appear[128] = {}; n char s[1000000]; n int main(void) { n   scanf( %d%d%d , &n, &m, &x); n   for (int i = 0; i < n; i++) scanf( %s , a[i]); n   int S = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (a[i][j] ==  S ) { n         S = 1; n         for (int u = 0; u < n; u++) n           for (int v = 0; v < m; v++) n             if (dist(i, j, u, v) <= x * x) { n               ok[(int)a[u][v]] = 1; n             } n       } else n         appear[(int)a[i][j]] = 1; n   scanf( %d , &n); n   scanf( %s , s); n   int ans = 0, f = 0; n   for (int i = 0; s[i]; i++) n     if (s[i] >=  A  && s[i] <=  Z ) { n       if (!S || !appear[s[i] + 32]) f = 1; n       ans += !ok[s[i] + 32]; n     } else if (!appear[(int)s[i]]) n       f = 1; n   if (f) ans = -1; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 0x3f3f3f3f; n const long long ooo = 9223372036854775807ll; n const int _cnt = 1000 * 1000 + 7; n const int _p = 1000 * 1000 * 1000 + 7; n const int N = 300005; n const double PI = acos(-1.0); n const double eps = 1e-9; n int o(int x) { return x % _p; } n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int lcm(int a, int b) { return a / gcd(a, b) * b; } n void file_put() { n   freopen( filename.in ,  r , stdin); n   freopen( filename.out ,  w , stdout); n } n int n, a[N], L, t; n long long ret = 0; n int main() { n   scanf( %d , &n); n   for (int i = (1); i <= (n); ++i) scanf( %d , &a[i]), ret += a[i]; n   for (int i = (1); i <= (n); ++i) { n     if (a[i] >= 2) n       t = ((a[i] >> 1) < (L) ? (a[i] >> 1) : (L)), a[i] -= t << 1, L -= t; n     a[i] %= 3, L += a[i]; n   } n   printf( %lld n , (ret - L) / 3); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> gr; n vector<long long> c; n vector<int> parent; n vector<int> size; n vector<char> used; n void make_set(int v) { n   parent[v] = v; n   size[v] = 1; n } n int find_set(int v) { n   if (v == parent[v]) return v; n   return parent[v] = find_set(parent[v]); n } n void union_sets(int a, int b) { n   a = find_set(a); n   b = find_set(b); n   if (a != b) { n     if (size[a] < size[b]) swap(a, b); n     parent[b] = a; n     size[a] += size[b]; n   } n } n long long binpow(long long a, int n) { n   long long res = 1; n   while (n) { n     if (n & 1) { n       res *= a; n       res %= 1000000007; n     } n     a *= a; n     a %= 1000000007; n     n >>= 1; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   long long k; n   cin >> n >> m >> k; n   c.resize(n); n   for (int i = 0; i < n; ++i) cin >> c[i]; n   vector<map<long long, int>> adjacent(n); n   vector<pair<int, int>> edges(m); n   parent.resize(m); n   size.resize(m); n   used.resize(m, 0); n   for (int i = 0; i < m; ++i) { n     make_set(i); n     int a, b; n     cin >> a >> b; n     --a, --b; n     edges[i].first = a; n     edges[i].second = b; n   } n   for (int i = 0; i < m; ++i) { n     int from = edges[i].first; n     int to = edges[i].second; n     long long xr = c[to] ^ c[from]; n     { n       auto found = adjacent[from].find(xr); n       if (found != adjacent[from].end()) { n         union_sets(i, found->second); n       } else { n         adjacent[from][xr] = i; n       } n     } n     { n       auto found = adjacent[to].find(xr); n       if (found != adjacent[to].end()) { n         union_sets(i, found->second); n       } else { n         adjacent[to][xr] = i; n       } n     } n   } n   vector<set<int>> subgraphs(m); n   for (int i = 0; i < m; ++i) { n     int comp = find_set(i); n     int from = edges[i].first; n     int to = edges[i].second; n     subgraphs[comp].insert(from); n     subgraphs[comp].insert(to); n   } n   long long ans = binpow(2, n + k); n   map<long long, long long> q; n   for (int i = 0; i < m; ++i) { n     int comp = find_set(i); n     if (!used[comp]) { n       used[comp] = 1; n       int from = edges[i].first; n       int to = edges[i].second; n       long long xr = c[to] ^ c[from]; n       q[xr] += subgraphs[comp].size() - 1; n     } n   } n   for (auto it = q.begin(); it != q.end(); ++it) { n     ans -= binpow(2, n); n     if (ans < 0) ans += 1000000007; n     ans += binpow(2, n - it->second); n     ans %= 1000000007; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[100005], b[100005]; n int l[100005], r[100005]; n using pii = pair<int, int>; n using ppi = pair<pii, int>; n int tr[100005 * 2]; n int get(int k) { n   int res = 0; n   for (; k; k -= k & -k) res += tr[k]; n   return res; n } n void update(int k) { n   for (; k <= m; k += k & -k) tr[k]++; n } n void calc(int *g, int *gg) { n   vector<ppi> V; n   for (int i = 1; i <= n; i++) n     V.push_back(ppi(pii(min(a[i], b[i]), max(a[i], b[i])), i)); n   sort(V.begin(), V.end(), [](const ppi &x, const ppi &y) { n     return x.first.second < y.first.second; n   }); n   int cnt = 0; n   memset(tr, 0, sizeof tr); n   for (auto x : V) { n     g[x.second] = cnt - get(x.first.first); n     update(x.first.first); n     cnt++; n   } n   reverse(V.begin(), V.end()); n   memset(tr, 0, sizeof tr); n   cnt = 0; n   for (auto x : V) { n     gg[x.second] = cnt - get(x.first.second) + get(x.first.first); n     update(x.first.first); n     cnt++; n   } n   memset(tr, 0, sizeof tr); n   for (auto x : V) update(x.first.second); n   for (auto x : V) gg[x.second] += get(x.first.first); n   return; n } n int main() { n   scanf( %d , &n); n   m = 2 * n; n   for (int i = 1; i <= n; i++) scanf( %d %d , &a[i], &b[i]); n   calc(l, r); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     int t = n - 1 - l[i] - r[i]; n     ans += 1ll * t * (l[i] + r[i]); n   } n   ans >>= 1; n   for (int i = 1; i <= n; i++) ans += 1ll * l[i] * r[i]; n   ans = 1ll * n * (n - 1) * (n - 2) / 6 - ans; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %i , &n); n   vector<pair<int, int>> tree(n); n   for (int x = 0; x < n; x++) { n     scanf( %i%i , &tree[x].first, &tree[x].second); n   } n   sort(tree.begin(), tree.end()); n   int current_day = 0; n   for (int i = 0; i < n; i++) { n     current_day = n         (tree[i].second < tree[i].first && tree[i].second >= current_day) n             ? tree[i].second n             : tree[i].first; n   } n   printf( %i n , current_day); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n vector<string> ss; n double check(const string& x) { n   double res = 0; n   if (x.size() < 3) { n     for (char c : x) { n       res = res * 10 + (int)c -  0 ; n     } n     return res; n   } n   if (x[x.size() - 3] ==  . ) { n     for (int i = 0; i < x.size() - 3; ++i) { n       if (x[i] ==  . ) continue; n       res = res * 10 + (int)x[i] -  0 ; n     } n     res += ((int)x[x.size() - 2] -  0 ) / 10.0; n     res += ((int)x[x.size() - 1] -  0 ) / 100.0; n     return res; n   } else { n     for (int i = 0; i < x.size(); ++i) { n       if (x[i] ==  . ) continue; n       res = res * 10 + (int)x[i] -  0 ; n     } n     return res; n   } n } n int main(int argc, const char* argv[]) { n   cin >> s; n   for (int i = 0; i < s.size(); ++i) { n     if (s[i] >=  0  && s[i] <=  9 ) { n       for (int j = i; j < s.size(); ++j) { n         if (j == s.size() - 1) { n           ss.emplace_back(s.substr(i, j - i + 1)); n           i = j; n           continue; n         } n         if (s[j] >=  a  && s[j] <=  z ) { n           ss.emplace_back(s.substr(i, j - i)); n           i = j - 1; n           break; n         } n       } n     } n   } n   double ans = 0; n   for (auto& x : ss) { n     ans += check(x); n   } n   char sans[5000]; n   sprintf(sans,  %.2f , ans); n   int len = (int)strlen(sans); n   int cnt = (len - 3) % 3; n   if (cnt == 0) cnt = 3; n   for (int i = 0; i < cnt; ++i) putchar(sans[i]); n   for (int i = cnt; i < len - 3; i += 3) { n     if (cnt) putchar( . ); n     putchar(sans[i]); n     putchar(sans[i + 1]); n     putchar(sans[i + 2]); n   } n   if (sans[len - 1] !=  0  || sans[len - 2] !=  0 ) { n     putchar( . ); n     putchar(sans[len - 2]); n     putchar(sans[len - 1]); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:100000000000000 ) n using namespace std; n int n; n double a[500001], b[500001], p, q; n string st; n double calc(double y1) { n   double y2 = 1.; n   for (int i = 0; i < n; i++) { n     y2 = min(y2, (1. - (a[i] * y1)) / b[i]); n   } n   double ret = p * y1 + q * y2; n   return ret; n } n int main() { n   cin >> n >> p >> q; n   double maxi = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i] >> b[i]; n     maxi = max(a[i], maxi); n   } n   double l = 0, r = 1. / maxi; n   for (int i = 0; i < 100; i++) { n     double m1 = (fabs(l - r)) / 3; n     double x1 = calc(l + m1), x2 = calc(r - m1); n     if (x1 >= x2) n       r = r - m1; n     else n       l = l + m1; n   } n   printf( %.10lf n , calc(l)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n vector<vector<int> > col; n vector<vector<int> > adj; n int color[N]; n void dfs(int v, int p) { n   set<int> st; n   int toapply = 1; n   for (int i = 0; i < col[v].size(); i++) { n     int c = col[v][i]; n     if (color[c] != -1) st.insert(color[c]); n   } n   for (int i = 0; i < col[v].size(); i++) { n     int c = col[v][i]; n     if (color[c] != -1) continue; n     while (st.find(toapply) != st.end()) toapply++; n     color[c] = toapply; n     toapply++; n   } n   for (int i = 0; i < adj[v].size(); i++) { n     int u = adj[v][i]; n     if (u == p) continue; n     dfs(u, v); n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   col.resize(n + 5); n   adj.resize(n + 5); n   memset(color, -1, sizeof(color)); n   int mx = 1; n   for (int i = 1; i <= n; i++) { n     int s; n     scanf( %d , &s); n     mx = max(mx, s); n     while (s--) { n       int t; n       scanf( %d , &t); n       col[i].push_back(t); n     } n   } n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(1, -1); n   printf( %d n , mx); n   for (int i = 1; i <= m; i++) n     if (color[i] == -1) color[i] = 1; n   for (int i = 1; i <= m; i++) printf( %d  , color[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 3e3 + 100; n const long long mod = 1e9 + 7; n const long long inf = 1e18; n long long n, dsu[maxn], sz[maxn], t[maxn], a[maxn], mx, s, ans; n pair<long long, pair<long long, long long> > e[maxn]; n void init() { n   mx = 0; n   for (long long i = 1; i <= n; i++) { n     dsu[i] = i; n     sz[i] = 1; n     t[i] = a[i] + sz[i]; n     mx = max(mx, t[i]); n   } n } n long long root(long long v) { n   if (dsu[v] == v) return v; n   return root(dsu[v]); n } n bool Union(long long v, long long u) { n   long long x = root(v), y = root(u); n   if (x == y) return 1; n   if (sz[x] < sz[y]) swap(x, y); n   sz[x] += sz[y]; n   t[x] += t[y]; n   mx = max(mx, t[x]); n   dsu[y] = x; n   return 0; n } n bool isval(long long x) { n   init(); n   for (long long i = 0; i < n - 1; i++) { n     if (e[i].first < x) { n       Union(e[i].second.first, e[i].second.second); n     } n   } n   return (mx <= s); n } n int main() { n   long long v, u, c; n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (long long i = 0; i < n - 1; i++) { n     cin >> e[i].second.first >> e[i].second.second >> e[i].first; n   } n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n     s += a[i]; n   } n   long long l = 0, r = 2e4, mid; n   while (r - l > 1) { n     mid = (l + r) / 2; n     if (isval(mid)) n       l = mid; n     else n       r = mid; n   } n   cout << l; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mn = 1e6 + 10; n const long long mod = 998244353; n int a[100], t[100]; n string s[100]; n int dp[21][1 << 20]; n int main() { n   int n, m; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   sort(a, a + n, greater<int>()); n   scanf( %d , &m); n   for (int i = 0; i < m; i++) cin >> s[i] >> t[i]; n   for (int i = m - 1; i >= 0; i--) { n     if (s[i] ==  p ) { n       for (int j = 1; j < 1 << m; j++) { n         int l = __builtin_ctz(j); n         if (t[i] == 1) n           dp[i][j] = a[l] + dp[i + 1][j - (1 << l)]; n         else n           dp[i][j] = -a[l] + dp[i + 1][j - (1 << l)]; n       } n     } else { n       if (t[i] == 1) { n         for (int j = 0; j < 1 << m; j++) { n           dp[i][j] = dp[i + 1][j]; n           for (int k = 0; k < m; k++) n             if ((j >> k) & 1) { n               dp[i][j] = max(dp[i][j], dp[i + 1][j - (1 << k)]); n             } n         } n       } else { n         for (int j = 0; j < 1 << m; j++) { n           dp[i][j] = dp[i + 1][j]; n           for (int k = 0; k < m; k++) n             if ((j >> k) & 1) { n               dp[i][j] = min(dp[i][j], dp[i + 1][j - (1 << k)]); n             } n         } n       } n     } n   } n   printf( %d , dp[0][(1 << m) - 1]); n } n


#include <bits/stdc++.h> n int min(int a, int b) { return a > b ? b : a; } n int max(int a, int b) { return a < b ? b : a; } n int main() { n   int n, a, b, c, d, i, pos; n   int sum, next; n   while (~scanf( %d , &n)) { n     int ans = 0; n     for (i = 1; i <= 4; i++) { n       scanf( %d%d%d%d , &a, &b, &c, &d); n       a = min(a, b); n       b = max(a, b); n       c = min(c, d); n       d = max(c, d); n       if (a + c <= n) { n         ans = 1; n         pos = i; n         next = a; n       } n     } n     if (ans) n       printf( %d %d %d n , pos, next, n - next); n     else n       printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long l, r, x, y; n unsigned long long gcd(unsigned long long a, unsigned long long b) { n   while (b > 0) { n     unsigned long long t = b; n     b = a % b; n     a = t; n   } n   return a; n } n int main() { n   cin >> l >> r >> x >> y; n   if (x == y) { n     if (x >= l && x <= r) { n       cout << 1; n       return 0; n     } n   } n   unsigned long long number = 0; n   l = 1 + ((l - 1) / x); n   l *= x; n   for (unsigned long long i = l; i * i <= x * y; i += x) { n     unsigned long long p = x * y / i; n     if (p * i == x * y && p >= l && p <= r && i <= r) { n       if (gcd(p, i) == x) { n         number += 2; n         if (i == p) number--; n       } n     } n   } n   cout << number; n } n


#include <bits/stdc++.h> n using namespace std; n long long i, j, k, l, n, m, c, d, a[2005][2005], b[10005], f, mod = 1e9 + 7; n vector<long long> v; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n; n   a[0][0] = 1; n   for (i = 1; i <= n; i++) n     for (j = 0; j <= i; j++) { n       if (j - 1 < 0) n         k = 0; n       else n         k = a[j - 1][i - 1]; n       a[j][i] = (k + a[j + 1][i - 1]) % mod; n     } n   for (i = n + 1; i <= 2 * n; i++) n     for (j = 0; j <= n - (i - 1) % n - 1; j++) { n       if (j - 1 < 0) n         k = 0; n       else n         k = a[j - 1][i - 1]; n       a[j][i] = (k + a[j + 1][i - 1]) % mod; n     } n   for (i = 1; i <= 2 * n; i++) { n     for (j = 0; j <= n; j++) b[i] = (b[i] + a[j][i]) % mod; n   } n   k = 0; n   for (i = 1; i <= 2 * n; i = i + 2) k = (k + b[i]) % mod; n   cout << k; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string s; n   cin >> n; n   getline(cin, s); n   while (n--) { n     getline(cin, s); n     bool L = false, M = false; n     if (s.length() < 5) { n       cout <<  OMG>.< I don t know!  << endl; n       continue; n     } n     if (s.substr(0, 5) ==  miao. ) { n       M = true; n     } n     if (s.substr(s.length() - 5) ==  lala. ) { n       L = true; n     } n     if (M && L) { n       cout <<  OMG>.< I don t know!  << endl; n     } else if (M) { n       cout <<  Rainbow s  << endl; n     } else if (L) { n       cout <<  Freda s  << endl; n     } else { n       cout <<  OMG>.< I don t know!  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t; n   t = 1; n   while (t--) { n     int n; n     cin >> n; n     int a[n + 1]; n     a[1] = 0; n     for (int i = 2; i <= n; i++) cin >> a[i]; n     vector<int> v; n     v.push_back(n); n     n = a[n]; n     v.push_back(n); n     while (n != 1) { n       v.push_back(a[n]); n       n = a[n]; n     } n     reverse(v.begin(), v.end()); n     for (auto x : v) cout << x <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios::sync_with_stdio(false); n   long long n; n   cin >> n; n   set<long long> s = {-2, -1}; n   vector<long long> arr(n, 0); n   while (n--) { n     long long x; n     cin >> x; n     --x; n     if ((*prev(s.end())) > x && (*prev(prev(s.end()))) < x) n       ++arr[(*prev(s.end()))]; n     if ((*prev(s.end())) < x) --arr[x]; n     s.insert(x); n   } n   cout << (max_element(arr.begin(), arr.end()) - arr.begin() + 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int s[105], d[105], c[105]; n int ans[105] = {0}; n vector<pair<int, int>> v; n int main() { n   int tc; n   tc = 1; n   while (tc--) { n     int n, m; n     cin >> n >> m; n     int flag = 1; n     for (int i = 1; i <= m; i++) { n       cin >> s[i] >> d[i] >> c[i]; n       if (d[i] - s[i] < c[i]) flag = 0; n       ans[d[i]] = m + 1; n       v.push_back({i, d[i]}); n     } n     if (flag == 0) { n       cout << -1 <<   n ; n       continue; n     } n     sort(v.begin(), v.end(), [](pair<int, int> &a, pair<int, int> &b) { n       return a.second < b.second; n     }); n     flag = 1; n     for (int i = 0; i < m; i++) { n       int now = v[i].first; n       int cnt = 0; n       for (int j = s[now]; j < d[now] and cnt < c[now]; j++) { n         if (ans[j] == 0) { n           ans[j] = now; n           cnt++; n         } n       } n       if (cnt < c[now]) { n         flag = 0; n         break; n       } n     } n     if (flag == 0) n       cout << -1 <<   n ; n     else { n       for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int t, n, m, a[3005] = {0}; n string ans[3005]; n vector<pair<string, int> > v[3005]; n map<string, int> mp; n map<int, string> mp1; n vector<string> v1[3005]; n int F(int x, int vis[]) { n   int i, i1, i2, idx; n   for (i = 0; i < v1[x].size(); i++) { n     string s = v1[x][i]; n     i1 = mp[s]; n     if (!vis[i1]) { n       vis[i1] = 1; n       if (a[i1] < 0 || F(a[i1], vis)) { n         a[i1] = x; n         ans[x] = s; n         return 1; n       } n     } n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int i, j, k, x, y, vis[3005] = {0}; n   string s1, s2; n   cin >> n; n   int cc = 1; n   memset(a, -1, sizeof a); n   for (i = 0; i < n; i++) { n     cin >> s1 >> s2; n     string temp = s1.substr(0, 3), temp1 = s1.substr(0, 2) + s2[0]; n     if (mp[temp] == 0) mp[temp] = cc++; n     if (mp[temp1] == 0) mp[temp1] = cc++; n     v[mp[temp]].push_back({temp1, i}); n     mp1[mp[temp]] = temp; n   } n   for (i = 1; i < cc; i++) { n     s2 = mp1[i]; n     if (v[i].size() == 1) { n       k = v[i][0].second; n       s1 = v[i][0].first; n       v1[k].push_back(s1); n       v1[k].push_back(s2); n       continue; n     } n     for (j = 0; j < v[i].size(); j++) { n       s1 = v[i][j].first; n       k = v[i][j].second; n       v1[k].push_back(s1); n     } n   } n   m = mp.size(); n   for (i = 0; i < n; i++) { n     for (j = 0; j < m + 2; j++) vis[j] = 0; n     if (F(i, vis)) n       continue; n     else { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES n ; n   for (i = 0; i < n; i++) cout << ans[i] <<   n ; n   cerr <<   n  n        <<  Time elapsed :  << clock() * 1000.0 / CLOCKS_PER_SEC <<   ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, a[100100], sum = 0, Min = 1e6; n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     if (a[i] < 0 && k) a[i] *= -1, k--; n     Min = min(Min, abs(a[i])); n     sum += a[i]; n   } n   if (k & 1) sum -= Min * 2; n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; n long long power(long long a, long long n) { n   long long p = 1; n   while (n > 0) { n     if (n % 2) { n       p = p * a; n     } n     n >>= 1; n     a *= a; n   } n   return p; n } n long long power(long long a, long long n, long long mod) { n   long long p = 1; n   while (n > 0) { n     if (n % 2) { n       p = p * a; n       p %= mod; n     } n     n >>= 1; n     a *= a; n     a %= mod; n   } n   return p % mod; n } n void swap(char *a, char *b) { n   char temp = *a; n   *a = *b; n   *b = temp; n } n long long int nCr(int n, int r) { n   long long int C[r + 1]; n   memset(C, 0, sizeof(C)); n   C[0] = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]); n   } n   return C[r]; n } n long long int countDer(int n) { n   int der[n + 1]; n   der[0] = 1; n   der[1] = 0; n   der[2] = 1; n   for (int i = 3; i <= n; ++i) der[i] = (i - 1) * (der[i - 1] + der[i - 2]); n   return der[n]; n } n int digitsum(long long a) { n   int ans = 0; n   while (a > 0) { n     ans = ans + a % 10; n     a = a / 10; n   } n   return ans; n } n int count(long long a) { n   int temp = 0; n   while (a > 0) { n     a = a / 10; n     temp++; n   } n   return temp; n } n int main() { n   int n; n   cin >> n; n   char s[n]; n   for (int i = 0; i < n; i++) cin >> s[i]; n   sort(s, s + n); n   for (int i = 0; i < n; i++) cout << s[i]; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1005, maxD = 10, oo = 1e9; n int Len, dp[maxN][maxN]; n int t[maxN][maxN], s[maxN], tool[maxN]; n int n, perm[maxN], cost[maxD], cnt[maxN][maxD], lost[maxN]; n bool flag = 0; n inline void solve(int i, int j, int d) { n   int jp = 0, sum = 0; n   for (int d2 = 0; d2 < maxD; d2++) { n     int td = cnt[j][d2]; n     int fl = (d + 1 + d2) >= 10; n     jp += fl * td; n     sum += td * cost[(d + 1 + d2) % 10]; n     td = cnt[n][d2] - cnt[j][d2]; n     if (d2 == 0 && d == 0 && flag) td -= lost[n] - lost[j]; n     fl = (d + d2) >= 10; n     jp += fl * td; n     sum += td * cost[(d + d2) % 10]; n   } n   dp[i + 1][jp] = max(dp[i + 1][jp], dp[i][j] + sum); n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   string r; n   cin >> r; n   int sz = r.size(); n   for (int i = 0; i < sz; i++) { n     if (r[i] ==  ? ) n       s[sz - i - 1] = -1; n     else n       s[sz - i - 1] = r[i] -  0 ; n   } n   Len = max(Len, sz); n   cin >> n; n   for (int i = 0; i < n; i++) { n     string tmp; n     cin >> tmp; n     int cz = tmp.size(); n     for (int j = 0; j < cz; j++) t[i][cz - j - 1] = tmp[j] -  0 ; n     Len = max(Len, cz); n     tool[i] = cz; n   } n   for (int i = 0; i < maxD; i++) cin >> cost[i]; n   dp[0][0] = 0; n   for (int j = 1; j <= n; j++) dp[0][j] = -oo; n   for (int i = 1; i < maxN; i++) n     for (int j = 0; j < maxN; j++) dp[i][j] = -oo; n   for (int i = 0; i < n; i++) perm[i] = i; n   for (int i = 0; i < Len; i++) { n     for (int j = 1; j <= n; j++) { n       for (int d = 0; d < maxD; d++) cnt[j][d] = cnt[j - 1][d]; n       int d = t[perm[j - 1]][i]; n       cnt[j][d]++; n       lost[j] = lost[j - 1]; n       if (tool[perm[j - 1]] <= i) lost[j]++; n     } n     if (i >= sz) flag = true; n     for (int j = 0; j <= n; j++) { n       if (s[i] != -1) n         solve(i, j, s[i]); n       else { n         int st = 0; n         if (i == sz - 1) st = 1; n         for (int d = st; d < maxD; d++) solve(i, j, d); n       } n     } n     vector<int> vc; n     for (int d = maxD - 1; d >= 0; d--) n       for (int j = 0; j < n; j++) n         if (t[perm[j]][i] == d) vc.push_back(perm[j]); n     for (int j = 0; j < n; j++) perm[j] = vc[j]; n   } n   int ans = 0; n   for (int j = 0; j <= n; j++) ans = max(ans, dp[Len][j] + j * cost[1]); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e6 + 7; n const int INF = 0x7fffffff; n long long c[maxn], s[maxn]; n long long a[maxn], num[maxn]; n int main() { n   long long n, x, y; n   while (cin >> n >> x >> y) { n     long long ans = n * x; n     memset(num, 0, sizeof(num)); n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       num[a[i]]++; n     } n     c[0] = s[0] = 0; n     for (int i = 1; i <= 2e6; i++) { n       c[i] = c[i - 1] + num[i]; n       s[i] = s[i - 1] + i * num[i]; n     } n     long long p = (x + y - 1) / y; n     for (int i = 2; i <= 1e6; i++) { n       long long cur = 0; n       for (int j = i; j <= 1e6 + i && cur < ans; j += i) { n         int l = j - i, r = j; n         int k = r - p; n         k = max(k, l); n         long long temp = (c[r] - c[k]) * j - (s[r] - s[k]); n         cur += temp * y; n         cur += (c[k] - c[l]) * x; n       } n       ans = min(ans, cur); n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct st { n   int x, y; n   bool operator<(const st o) const { return y < o.y || (y == o.y && x < o.x); } n }; n long long a[1000000][2]; n long long n, m, x, y; n long long M[1000000][2]; n int main() { n   cin >> n >> m; n   cin >> M[0][0] >> M[0][1]; n   for (int i = 0; i < (int)(n); i++) { n     for (int j = 0; j < (int)(2); j++) { n       cin >> a[i][j]; n     } n   } n   for (int i = 1; i < 2 * n; i++) { n     for (int j = 0; j < (int)(2); j++) { n       M[i][j] = 2 * (a[(i - 1) % n][j]) - M[i - 1][j]; n     } n   } n   cout << M[m % (2 * n)][0] <<     << M[m % (2 * n)][1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int yee[6][3] = {{0, 1, 2}, {1, 0, 2}, {1, 2, 0}, n                  {2, 1, 0}, {2, 0, 1}, {0, 2, 1}}; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, x; n   cin >> n >> x; n   n %= 6; n   cout << yee[n][x] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, d, n; n int main() { n   while (~scanf( %I64d%I64d%I64d%I64d%I64d , &n, &a, &b, &c, &d)) { n     long long mi = min(b - c, min(a - d, a + b - c - d)), n               ma = max(b - c, max(a - d, a + b - c - d)); n     long long num1 = min(n - ma, n), num2 = max(1 - mi, (long long)1), ans = 0; n     ; n     if (num1 < num2) n       ans = 0; n     else n       ans = num1 - num2 + 1; n     printf( %I64d n , n * ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int binarySearch(int arr[], int l, int r, int x) { n   while (l <= r) { n     int m = l + (r - l) / 2; n     if (arr[m] == x) return m; n     if (arr[m] < x) n       l = m + 1; n     else n       r = m - 1; n   } n   return -1; n } n bool isprime(int n) { n   if (n <= 1) return false; n   for (int i = 2; i < n; i++) n     if (n % i == 0) return false; n   return true; n } n long long int factorial(int n) { n   if (n == 0) return 1; n   return n * factorial(n - 1); n } n int countDigit(long long n) { n   int count = 0; n   while (n != 0) { n     n = n / 10; n     ++count; n   } n   return count; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int v1, v2, t, d, sum = 0; n   cin >> v1 >> v2 >> t >> d; n   for (int i = 0; i < t; i++) sum += min(v2 + (t - 1 - i) * d, v1 + i * d); n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (int)(1e5 + 1e1); n const double PI = acos(-1.0); n long long a, m, t; n int main() { n   cin >> m >> a; n   if (a == 0 && m > 1) { n     cout <<  No solution ; n     return 0; n   } n   cout << a; n   for (int i = 1; i < m; i++) cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   bool ok[999]; n   memset(ok, false, sizeof(ok)); n   ok[ A ] = true; n   ok[ H ] = true; n   ok[ I ] = true; n   ok[ M ] = true; n   ok[ O ] = true; n   ok[ T ] = true; n   ok[ U ] = true; n   ok[ V ] = true; n   ok[ W ] = true; n   ok[ X ] = true; n   ok[ Y ] = true; n   static char s[199919]; n   scanf( %s , s + 1); n   int n = strlen(s + 1); n   bool flag = true; n   for (int i = 1, j = n; i <= j; ++i, --j) n     if (s[i] != s[j] || !ok[s[i]]) flag = false; n   printf( %s n , flag ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; n int dy[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; n using namespace std; n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n long long to_int(string s) { n   long long ans = 0; n   for (int i = 0; i < s.size(); i++) { n     ans = ans * 10 + (s[i] - 48); n   } n   return ans; n } n int l[100001]; n int r[100001]; n int a[100001]; n int main() { n   fast(); n   int d1, d2, d3, ans = 0; n   cin >> d1 >> d2 >> d3; n   if (d2 < d1) swap(d2, d1); n   ans += d1; n   ans += min(d3, d1 + d2); n   ans += min(d2, d3 + d1); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-7; n const long long MOD = 1000000007LL; n const int INF = 0x3f3f3f3f; n const int _NAN = -0x3f3f3f3f; n const double EULC = 0.5772156649015328; n const int NIL = -1; n template <typename T> n void read(T &x) { n   x = 0; n   char ch = getchar(); n   long long f = 1; n   while (!isdigit(ch)) { n     if (ch ==  - ) f *= -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch - 48; n     ch = getchar(); n   } n   x *= f; n } n const int maxn = 2e5 + 10; n int arr[maxn]; n int main(void) { n   ios::sync_with_stdio(false); n   int n; n   while (cin >> n) { n     if (~n & 1) { n       cout <<  NO n ; n       continue; n     } n     for (int i = 1; i <= n; ++i) { n       if (i & 1) { n         arr[i] = 2 * i - 1; n         arr[i + n] = 2 * i; n       } else { n         arr[i] = 2 * i; n         arr[i + n] = 2 * i - 1; n       } n     } n     cout <<  YES n ; n     for (int i = 1; i <= 2 * n; ++i) n       cout << arr[i] << (i == 2 * n ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   long long o = 0; n   long long z = 0; n   for (long long i = 0; i < n; i++) n     if (s[i] ==  n ) n       o++; n     else if (s[i] ==  z ) n       z++; n   while (o--) cout << 1 <<    ; n   while (z--) cout << 0 <<    ; n } n int main() { n   long long t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n int add(int a, int b) { n   a += b; n   if (a >= MOD) a -= MOD; n   if (a < 0) a += MOD; n   return a; n } n int mul(int a, int b) { return a * 1ll * b % MOD; } n int binpow(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) res = mul(res, a); n     a = mul(a, a); n     b >>= 1; n   } n   return res; n } n vector<int> pr; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> c(n); n   for (int i = 0; i < int(n); i++) cin >> c[i]; n   sort(c.begin(), c.end(), greater<int>()); n   pr.push_back(0); n   for (int i = 0; i < int(n); i++) { n     pr.push_back(add(pr.back(), c[i])); n   } n   int invn = binpow(n, MOD - 2); n   for (int k = 1; k <= n; ++k) { n     int ans = 0; n     for (int i = 0, j = 0; i < n; i += k, ++j) { n       ans = add(ans, mul(j, add(pr[min(n, i + k)], -pr[i]))); n     } n     cout << mul(ans, invn) <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, cnt = 0; n long long a[623456], b[623456], v[9999999]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     v[a[i]] = 1; n   } n   for (int i = 1; i <= n; i++) { n     cin >> b[i]; n     v[b[i]] = 1; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       long long x = a[i] ^ b[j]; n       if (v[x]) { n         cnt++; n       } n     } n   } n   if (cnt % 2 == 0) n     cout <<  Karen ; n   else n     cout <<  Koyomi ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200010; n struct Node { n   long long dis, mxdis; n   int id; n   bool operator<(const Node &rhs) const { n     if (dis == rhs.dis) return mxdis < rhs.mxdis; n     return dis < rhs.dis; n   } n } p[MAXN]; n struct NO { n   long long dis, mxdis; n   int id; n   NO(long long x, long long y, int z) { n     dis = x, mxdis = y; n     id = z; n   } n   bool operator<(const NO &rhs) const { return mxdis > rhs.mxdis; } n }; n priority_queue<NO> q; n long long l[MAXN], r[MAXN]; n struct Node1 { n   long long val; n   int id; n   bool operator<(const Node1 &rhs) const { return val < rhs.val; } n } a[MAXN]; n int ans[MAXN]; n int main() { n   int n, m, i; n   while (scanf( %d%d , &n, &m) == 2) { n     for (i = 0; i < n; i++) { n       scanf( %lld%lld , &l[i], &r[i]); n       if (i > 0) { n         p[i].dis = l[i] - r[i - 1]; n         p[i].mxdis = r[i] - l[i - 1]; n         p[i].id = i; n       } n     } n     sort(p + 1, p + n); n     for (i = 0; i < m; i++) { n       scanf( %lld , &a[i].val); n       a[i].id = i; n     } n     sort(a, a + m); n     int left = 1; n     while (!q.empty()) q.pop(); n     int ok = 1; n     if (n > m + 1) { n       printf( No n ); n       continue; n     } n     for (i = 0; i < m; i++) { n       while (a[i].val >= p[left].dis && left < n) { n         q.push(NO(p[left].dis, p[left].mxdis, p[left].id)); n         left++; n       } n       if (!q.empty()) { n         NO x = q.top(); n         q.pop(); n         if (x.mxdis >= a[i].val) n           ans[x.id] = a[i].id + 1; n         else { n           ok = 0; n           break; n         } n       } n     } n     if (ok && q.empty() && left == n) { n       printf( Yes n ); n       for (i = 1; i < n; i++) printf( %d%c , ans[i], (i == n - 1) ?   n  :    ); n     } else n       printf( No n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T modpow(T base, T exp) { n   T result = 1; n   while (exp > 0) { n     if (exp & 1) result = (result * base); n     base = (base * base); n     exp >>= 1; n   } n   return result; n } n int main() { n   queue<string> poss; n   long long int n, q; n   string x, y, poss1; n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> q; n   map<string, vector<string> > m; n   for (long long int(i) = (0); (i) < (q); (i)++) { n     cin >> x >> y; n     m[y].push_back(x); n   } n   map<string, vector<string> >::iterator it; n   it = m.begin(); n   if ((*it).first !=  a ) { n     cout << 0 << endl; n     return 0; n   } else { n     for (long long int(i) = (0); (i) < (m[ a ].size()); (i)++) n       poss.push((m[ a ])[i]); n     int cnt = 2; n     while (cnt != n) { n       while ((poss.front()).length() != cnt + 1) { n         string c = (poss.front()).substr(0, 1); n         if (m.find(c) != m.end()) { n           string str = poss.front(); n           poss.pop(); n           str = str.substr(1, str.length() - 1); n           for (long long int(j) = (0); (j) < (m[c].size()); (j)++) n             poss.push(m[c][j] + str); n         } else n           poss.pop(); n         if (poss.empty()) { n           cout <<  0  << endl; n           return 0; n         } n       } n       cnt++; n     } n   } n   cout << poss.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int arr[100005]; n map<long long int, int> m; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %I64d , &arr[i]); n   } n   for (int i = 1; i <= n; i++) { n     arr[i] += arr[i - 1]; n     m[arr[i]]++; n   } n   int maxVal = 0; n   for (int i = 1; i <= n; i++) { n     maxVal = max(maxVal, m[arr[i - 1]]); n   } n   printf( %d , n - maxVal); n } n


#include <bits/stdc++.h> n using namespace std; n string solve() { n   int a, b, n; n   string number =   ; n   cin >> a >> b >> n; n   bool added; n   added = false; n   a *= 10; n   for (int j = 1; j < 11; j++) { n     if (a % b == 0) { n       added = true; n       break; n     } n     a++; n   } n   if (!added) return  -1 ; n   number = to_string(a); n   for (int i = 0; i < n - 1; i++) number +=  0 ; n   return number; n } n int main(void) { cout << solve() <<   n ; } n


#include <bits/stdc++.h> n using namespace std; n vector<int> adj[3003]; n int cost[3003], par[3003]; n bool vis[3003]; n map<pair<int, int>, bool> mark; n void path(int x, int y) { n   if (x == y) return; n   cost[x] = 0; n   path(par[x], y); n } n void dfs(int u) { n   vis[u] = true; n   for (int v : adj[u]) { n     if (vis[v]) { n       if (par[u] != v) { n         cost[v] = 0; n         if (!mark[{u, v}]) { n           path(u, v); n           mark[{u, v}] = mark[{v, u}] = true; n         } n       } n     } else n       par[v] = u, dfs(v); n   } n } n void bfs(int src) { n   queue<int> q; n   q.push(src); n   while (q.size()) { n     int u = q.front(); n     q.pop(); n     for (int v : adj[u]) { n       if (cost[v] == -1) { n         cost[v] = cost[u] + 1; n         q.push(v); n       } n     } n   } n } n int main() { n   int n, x, y; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   memset(cost, -1, sizeof cost); n   par[1] = 1; n   dfs(1); n   memset(vis, false, sizeof vis); n   for (int i = 1; i <= n; i++) { n     if (cost[i] == 0) bfs(i); n   } n   for (int i = 1; i <= n; i++) cout << cost[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200010]; n vector<int> v[70]; n int main() { n   int i, j, n; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n     for (j = 1; j < 63; j++) { n       if (a[i] % (1LL << j)) { n         v[j].push_back(i); n         break; n       } n     } n   } n   int ans = 0, tmp = 0; n   for (i = 1; i < 63; i++) { n     if (ans < v[i].size()) { n       ans = v[i].size(); n       tmp = i; n     } n   } n   printf( %d n , n - ans); n   for (i = 1; i < 63; i++) { n     if (i == tmp) continue; n     for (auto j : v[i]) printf( %lld  , a[j]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char a[100010]; n vector<int> qe[710000]; n map<pair<int, int>, int> dp; n long long hehe[20]; n long long find(int x, int y) { n   int k = y, n = 1; n   while (k) { n     n++; n     k /= 10; n   } n   return hehe[n] * x + y; n } n int get(char a[], int n) { n   int ans = 0; n   for (int i = 0; i < n; i++) { n     ans = ans * 27 + a[i] -  a  + 1; n   } n   return ans; n } n int getans(int x, int y, int n1, int n2) { n   int ans = 78974453; n   int ll, rr, l, r; n   for (int i = 0; i < qe[x].size(); i++) { n     l = qe[x][i]; n     r = l + n1; n     int p = int(lower_bound(qe[y].begin(), qe[y].end(), l) - qe[y].begin()); n     if (p < qe[y].size()) { n       rr = qe[y][p] + n2; n       if (r > rr) rr = r; n       ans = min(ans, rr - l); n     } n     p--; n     if (p >= 0) { n       rr = qe[y][p] + n2; n       if (r > rr) rr = r; n       ans = min(ans, rr - qe[y][p]); n     } n   } n   return ans; n } n int main() { n   int n, i, j, k, m; n   int x, y; n   char s1[5], s2[5]; n   hehe[1] = 1; n   for (i = 2; i <= 15; i++) hehe[i] = hehe[i - 1] * 10; n   scanf( %s , a); n   n = strlen(a); n   for (i = 0; i < n; i++) { n     for (j = 1; j < 5; j++) { n       if (j + i > n) break; n       k = get(a + i, j); n       qe[k].push_back(i); n     } n   } n   scanf( %d , &m); n   while (m--) { n     scanf( %s%s , s1, s2); n     int xx = strlen(s1); n     int yy = strlen(s2); n     x = get(s1, xx); n     y = get(s2, yy); n     if (qe[x].size() > qe[x].size() || n         (qe[x].size() == qe[y].size() && x > y)) { n       swap(x, y); n       swap(xx, yy); n     } n     int &ans = dp[pair<int, int>(x, y)]; n     if (!ans) ans = getans(x, y, xx, yy); n     printf( %d n , ans == 78974453 ? -1 : ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[100]; n queue<int> q; n int main() { n   ios::sync_with_stdio(false); n   int n, m, k, t; n   cin >> n >> m >> k; n   for (int i = 0; i < k; ++i) cin >> p[i]; n   for (int i = 0; i < m * n; ++i) { n     cin >> t; n     q.push(t); n   } n   int an = 0; n   for (int i = 0; i < m * n; ++i) { n     for (int j = 0; j < k; ++j) { n       if (p[j] == q.front()) n         for (int h = j; h > 0; --h) { n           swap(p[h], p[h - 1]); n           an++; n         } n     } n     an++; n     q.pop(); n   } n   cout << an; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const string alf =  abcdefghijklmnopqrstuvwxyz ; n const int hashP = 239017; n const int N = 1e5 + 10; n const int MOD = 1e9 + 7; n const int MOD2 = 998244353; n const int INF = 1e9; n const long long INF2 = 1e18; n template <typename T> n bool umn(T &a, T b) { n   return (a > b ? (a = b, 1) : 0); n } n template <typename T> n bool umx(T &a, T b) { n   return (a < b ? (a = b, 1) : 0); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   string s = to_string(n); n   long long dl = s.size(); n   long long ans = 0; n   for (long long mask = 1; mask <= (1 << dl); mask++) { n     long long ch = 0; n     vector<long long> a; n     for (int i = 0; i < dl; i++) { n       long long c = (mask >> i) & 1; n       a.push_back(c); n     } n     reverse(a.begin(), a.end()); n     for (int i = 0; i < dl; i++) { n       ch *= 10; n       long long c = a[i]; n       ch += c; n     } n     if (n >= ch) ans++; n   } n   cout << ans - 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2e9; n FILE* stream; n mt19937 rng(1337); n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < int(n); i++) { n     cin >> a[i]; n   } n   vector<int> b(n); n   for (int i = 0; i < int(n); i++) { n     if (i % 2) { n       b[i] = a[i - 1]; n     } else { n       b[i] = -a[i + 1]; n     } n   } n   for (int i = 0; i < int(n); i++) cout << b[i] <<    ; n   cout << endl; n   return; n } n signed main() { n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m, a[4005], b[4005]; n   bitset<4005> adj[4005]; n   cin >> n >> m; n   vector<int> tam(n + 1); n   for (int i = 0; i < m; i++) { n     cin >> a[i] >> b[i]; n     adj[a[i]][b[i]] = 1; n     adj[b[i]][a[i]] = 1; n     tam[a[i]]++; n     tam[b[i]]++; n   } n   int res = 1e9; n   for (int i = 0; i < m; i++) { n     for (int j = i + 1; j < m; j++) { n       vector<int> v = {a[i], b[i], a[j], b[j]}; n       sort(v.begin(), v.end()); n       v.erase(unique(v.begin(), v.end()), v.end()); n       if (v.size() != 3) continue; n       if (adj[v[0]][v[1]] && adj[v[1]][v[2]] && adj[v[2]][v[0]]) { n         res = min(res, tam[v[0]] + tam[v[1]] + tam[v[2]] - 6); n       } n     } n   } n   if (res == 1e9) n     cout << -1 <<   n ; n   else n     cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n #define FF first n #define SS second n #define pb push_back n #define sz(x) (int)x.size() n #define oo 1e9 n #define eps 1e-9 n #define PI acos(-1.0) n #define lb lower_bound n #define ub upper_bound n #define all(a) (a).begin(), (a).end() n #define log2i(x) (64 - __builtin_clzll(1ll*(x)) - 1) n #define INF 0x7f7f7f7f n typedef long long ll; n typedef unsigned long long ull; n typedef pair<int, int> ii; n typedef pair<ii, ii> Li; n const int N = 2e5+7, M=100+7; n const int MOD = 1e9+7; n const int base=30; n  n int n, m, k, t; n  n int fac[N]{1}, invf[N]{1}; n  n int add(int a, int b) { n     a+=b; if(a>MOD) a-=MOD; n     return a; n } n int mul(int a, int b) {return (1LL*a*b)%MOD;} n void gcd(int a, int b, int &x, int &y) { n     if(!b) {x=1, y=0; return;} n     int tx, ty; gcd(b, a%b, tx, ty); n     x=ty, y=tx-(a/b)*ty; n } n int inv(int a) { n     int x, y; gcd(a, MOD, x, y); n     return (x%MOD+MOD)%MOD; n } n int nCk(int n, int k) { n     if(n<k||k<0) return 0; n     return mul(fac[n], mul(invf[n-k], invf[k])); n } n  n signed main() { n     //freopen( test.inp ,  r , stdin); n     //freopen( test.out ,  w , stdout); n     //fastIO; n     scanf( %d , &t); n     for(int i=1;i<N;i++) { n         fac[i]=mul(fac[i-1], i); n         invf[i]=inv(fac[i]); n     } n     while(t--) { n         scanf( %d%d%d , &n, &m, &k); n         int r=min(1-m, k-n), ans=nCk(n, n/2); n         if(n&1) ans=mul(ans, 2); ans=mul(ans, r); n         for(int pos, neg, i=r+1; i<=r+n;i++) { n             pos=max(0, m+i-1), neg=max(0, n-k+i); n             int tmp=n-pos-neg; n             if(tmp<0) break; n             ans=add(ans, nCk(tmp, n/2-pos)); n             if(n&1) ans=add(ans, nCk(tmp, n/2-neg)); n         } n         printf( %d n , ans); n     } n     return 0; n } n /* stuff you should look for n     - int overflow, array   bounds n     - special cases (n=1?) n     - do smth instead of do nothing and stay organized n     - WRITE STUFF DOWN n     - DONT JUST STICK ON ONE APPROACH n */ n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int n, m; n   std::cin >> n >> m; n   if (n == 1 && m == 2) { n     std::cout <<  0  <<   n ; n     std::cout <<  1 1  <<   n ; n     std::cout <<  1 2  <<   n ; n     std::cout <<  1 1  <<   n ; n     return 0; n   } n   if (n == 2 && m == 1) { n     std::cout <<  0  <<   n ; n     std::cout <<  1 1  <<   n ; n     std::cout <<  2 1  <<   n ; n     std::cout <<  1 1  <<   n ; n     return 0; n   } n   if ((n * m % 2 == 0 && n != 1 && m != 1)) { n     std::cout << 0 <<   n ; n     std::cout <<  1 1  <<   n ; n     if (n % 2 == 0) { n       for (int i = 0; i < n; i++) { n         if (i % 2 == 0) { n           for (int j = 0; j < m - 1; j++) { n             std::cout << i + 1 <<     << j + 2 <<   n ; n           } n         } n         if (i % 2 == 1) { n           for (int j = 0; j < m - 1; j++) { n             std::cout << i + 1 <<     << m - j <<   n ; n           } n         } n       } n       for (int i = 0; i < n; i++) { n         std::cout << n - i <<     << 1 <<   n ; n       } n     } else { n       for (int i = 0; i < m; i++) { n         if (i % 2 == 0) { n           for (int j = 0; j < n - 1; j++) { n             std::cout << j + 2 <<     << i + 1 <<   n ; n           } n         } n         if (i % 2 == 1) { n           for (int j = 0; j < n - 1; j++) { n             std::cout << n - j <<     << i + 1 <<   n ; n           } n         } n       } n       for (int i = 0; i < m; i++) { n         std::cout << 1 <<     << m - i <<   n ; n       } n     } n   } else { n     std::cout <<  1  <<   n ; n     std::cout << n <<     << m <<    ; n     std::cout <<  1 1  <<   n ; n     for (int i = 0; i < n; i++) { n       if (i % 2 == 0) { n         for (int j = 0; j < m; j++) { n           std::cout << i + 1 <<     << j + 1 <<   n ; n         } n       } n       if (i % 2 == 1) { n         for (int j = 0; j < m; j++) { n           std::cout << i + 1 <<     << m - j <<   n ; n         } n       } n     } n     std::cout <<  1 1  <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int ar[50][50]; n int dp[51][51]; n bool leaf[50]; n vector<int> adj[50]; n vector<int> adjw[50]; n vector<int> leafs; n int bad; n vector<int> pos; n int go(int rem, int now) { n   if (rem == 0) { n     return 0; n   } n   if (dp[rem][now] != -1) { n     return dp[rem][now]; n   } n   int low = 0; n   int high = 12500000; n   while (low < high) { n     int mid = (low + high + 1) / 2; n     int temp = rem; n     for (int i = 0; i < n; i++) { n       if (!leaf[i] || i == now) { n         continue; n       } n       int cur = 0; n       while (temp > 0 && ar[now][i] + go(rem - cur - 1, i) >= mid) { n         cur++; n         temp--; n       } n     } n     if (temp == 0) { n       low = mid; n     } else { n       high = mid - 1; n     } n   } n   dp[rem][now] = low; n   return low; n } n void dfs(int now, int from) { n   if (leaf[now]) { n     leafs.push_back(now); n   } n   for (int i = 0; i < pos.size(); i++) { n     if (now == pos[i]) { n       bad++; n     } n   } n   for (int i = 0; i < adj[now].size(); i++) { n     int to = adj[now][i]; n     if (to != from) { n       dfs(to, now); n     } n   } n } n void dfs(int now, int from, int root, int dist) { n   ar[root][now] = dist; n   for (int i = 0; i < adj[now].size(); i++) { n     int to = adj[now][i]; n     if (to != from) { n       dfs(to, now, root, dist + adjw[now][i]); n     } n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   int maxw = 0; n   for (int i = 1; i < n; i++) { n     int a, b, w; n     cin >> a >> b >> w; n     a--; n     b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n     adjw[a].push_back(w); n     adjw[b].push_back(w); n   } n   for (int i = 0; i < n; i++) { n     dfs(i, -1, i, 0); n   } n   for (int i = 0; i <= 50; i++) { n     for (int j = 0; j <= 50; j++) { n       dp[i][j] = -1; n     } n   } n   for (int i = 0; i < n; i++) { n     leaf[i] = adj[i].size() == 1; n   } n   int start, m; n   cin >> start >> m; n   start--; n   for (int i = 0; i < m; i++) { n     int x; n     cin >> x; n     x--; n     pos.push_back(x); n   } n   int ans = 12500000; n   for (int i = 0; i < adj[start].size(); i++) { n     leafs.clear(); n     leafs.resize(0); n     bad = 0; n     dfs(adj[start][i], start); n     if (bad == 0) { n       continue; n     } n     int low = 0; n     int high = 1250000; n     while (low < high) { n       int mid = (low + high + 1) / 2; n       int temp = bad; n       for (int z = 0; z < leafs.size(); z++) { n         int cl = leafs[z]; n         if (cl == start) { n           continue; n         } n         int cur = 0; n         while (temp > 0 && ar[start][cl] + go(m - cur - 1, cl) >= mid) { n           cur++; n           temp--; n         } n       } n       if (temp == 0) { n         low = mid; n       } else { n         high = mid - 1; n       } n     } n     ans = min(ans, low); n   } n   cout << ans << endl; n   return 0; n } n