#include <bits/stdc++.h> n using namespace std; n template <typename T> n T getint() { n   T x = 0, p = 1; n   char ch; n   do { n     ch = getchar(); n   } while (ch <=    ); n   if (ch ==  - ) p = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x * p; n } n mt19937 gen(chrono::system_clock::now().time_since_epoch().count()); n template <typename T1, typename T2> n bool umin(T1 &x, const T2 &y) { n   if (x > y) return x = y, true; n   return false; n } n template <typename T1, typename T2> n bool umax(T1 &x, const T2 &y) { n   if (x < y) return x = y, true; n   return false; n } n const int maxn = (int)3e5 + 10; n const int inf = (int)1e9 + 5; n const int mod = (int)1e9 + 7; n const long long llinf = (long long)1e18 + 5; n const long double pi = acos(-1.0); n int nxt[maxn * 30][2]; n int f[maxn * 30]; n int sz = 1; n int v[maxn], t[maxn]; n void add(int x, int d) { n   int v = 1; n   for (int i = 29; i >= 0; --i) { n     int b = (x >> i & 1); n     if (nxt[v][b] == 0) nxt[v][b] = ++sz; n     v = nxt[v][b]; n     f[v] += d; n   } n } n int get(int x) { n   int v = 1; n   int res = 0; n   for (int i = 29; i >= 0; --i) { n     int b = (x >> i & 1); n     if (f[nxt[v][b]] > 0) { n       v = nxt[v][b]; n     } else { n       res |= 1 << i; n       v = nxt[v][b ^ 1]; n     } n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> v[i]; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> t[i]; n     add(t[i], 1); n   } n   for (int i = 1; i <= n; ++i) { n     int z = get(v[i]); n     cout << z <<    ; n     add(z ^ v[i], -1); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n bool inBound(int x) { return x >= 0 && x < 30001; } n int main() { n   std::ios::sync_with_stdio(0); n   std::cin.tie(0); n   std::cout.tie(0); n   int n, d; n   cin >> n >> d; n   int mx = 30000; n   vector<int> gems(mx + 1, 0); n   for (int i = 0; i < n; i++) { n     int temp; n     cin >> temp; n     gems[temp]++; n   } n   vector<vector<int> > dp; n   dp.resize(mx + 1, vector<int>(500, 0)); n   for (int i = mx; i >= d; i--) { n     for (int prev = 0; prev < 500; prev++) { n       dp[i][prev] = gems[i]; n       int prevJump = d + prev - 250; n       int nextPos = i + prevJump; n       int choice1 = n           (inBound(nextPos - 1) && prev - 1 >= 0 && (prevJump - 1) > 0) n               ? dp[nextPos - 1][prev - 1] n               : 0; n       int choice2 = n           (inBound(nextPos) && (prevJump) > 0) ? dp[nextPos][prev] : 0; n       int choice3 = n           (inBound(nextPos + 1) && prev + 1 < 500 && (prevJump + 1) > 0) n               ? dp[nextPos + 1][prev + 1] n               : 0; n       dp[i][prev] += max(choice1, max(choice2, choice3)); n     } n   } n   cout << dp[d][250] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n char s[555555]; n int t[555555]; n int n, a, b, T; n int getAns() { n   for (int i = 0; i + 1 < n - 1 - i; i++) { n     swap(t[i + 1], t[n - 1 - i]); n   } n   if (t[0] > T) { n     return 0; n   } n   int ct = t[0]; n   int ans = 1; n   for (int i = 1; i < n; i++) { n     ct += a + t[i]; n     if (ct <= T) { n       ans = i + 1; n     } n   } n   if (ans == n) { n     return ans; n   } n   long long wt = t[0]; n   int bi = n - 1; n   while (wt + a + t[bi] <= T) { n     wt += a + t[bi]; n     bi--; n   } n   ans = max(ans, n - bi); n   for (int i = 1; i + 1 < n; i++) { n     wt += 2 * a + t[i]; n     while (wt > T && bi + 1 < n) { n       bi++; n       wt -= a + t[bi]; n     } n     if (wt > T) { n       break; n     } n     ans = max(ans, i + n - bi); n   } n   return ans; n } n int main() { n   scanf( %d , &n); n   scanf( %d%d , &a, &b); n   scanf( %d , &T); n   scanf(  n%s , s); n   for (int i = 0; i < n; i++) { n     t[i] = 1; n     if (s[i] ==  w ) { n       t[i] += b; n     } n   } n   cout << max(getAns(), getAns()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; n const int n_ = 3010; n long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); } 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 int n, dp[n_][3], a[n_][3]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int j = 0; j < 3; j++) { n     for (int i = 0; i < n; i++) cin >> a[i][j]; n   } n   dp[0][1] = -INF; n   for (int i = 0; i < n; i++) { n     dp[i + 1][1] = max(dp[i][0] + a[i][0], dp[i][1] + a[i][1]); n     dp[i + 1][0] = max(dp[i][1] + a[i][2], dp[i][0] + a[i][1]); n   } n   cout << dp[n][1] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n long long dp[1005][2005]; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 1; i <= max(m, n); i++) { n     for (int j = 0; j <= i; j++) { n       if (j == 1) { n         dp[i][j] = i; n         continue; n       } n       dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; n       dp[i][j] %= mod; n     } n   } n   cout << 1LL * dp[n - 1][2 * k] * dp[m - 1][2 * k] % mod; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 8e6 + 7, mod = 998244353, root = 3; n long long x[N], n, m, i, j, ma, mb, inv, res, b[N], ans[N], L, R[N]; n long long pow_mod(long long x, long long n) { n   long long res = 1; n   while (n) { n     if (n & 1) res = 1LL * x * res % mod; n     x = 1LL * x * x % mod; n     n >>= 1; n   } n   return res; n } n void ntt(long long* x, long long type) { n   for (long long i = 0; i < n; ++i) n     if (i < R[i]) swap(x[i], x[R[i]]); n   for (long long i = 1; i < n; i <<= 1) { n     long long wn = pow_mod(root, type == 1 ? (mod - 1) / (i << 1) n                                            : (mod - 1 - (mod - 1) / (i << 1))); n     for (long long p = i << 1, j = 0; j < n; j += p) { n       long long w = 1; n       for (long long k = 0; k < i; ++k, w = 1LL * wn * w % mod) { n         long long l = x[j + k], r = 1LL * x[j + k + i] * w % mod; n         x[j + k] = (l + r) % mod; n         x[j + k + i] = (l - r + mod) % mod; n       } n     } n   } n } n void fpow(long long sz) { n   ntt(ans, 1); n   ntt(x, 1); n   while (sz) { n     if (sz & 1) { n       for (long long i = 0; i < n; i++) ans[i] = 1ll * ans[i] * x[i] % mod; n     } n     for (long long i = 0; i < n; i++) x[i] = 1ll * x[i] * x[i] % mod; n     sz >>= 1; n   } n   ntt(ans, -1); n } n int32_t main() { n   ans[0] = 1; n   long long sz, k, tx; n   cin >> sz >> k; n   for (long long i = 1; i <= k; i++) { n     scanf( %d , &tx); n     x[tx] = 1; n   } n   for (m = 200000 * 9 + 7, n = 1; n <= m; n <<= 1) L++; n   for (i = 0; i < n; ++i) R[i] = (R[i >> 1] >> 1) | ((i & 1) << L - 1); n   fpow(sz / 2); n   long long cnt = 0; n   inv = pow_mod(n, mod - 2); n   for (long long i = 0; i < n; i++) n     cnt = n         (cnt + (ans[i] % mod * inv % mod) * (ans[i] % mod * inv % mod) % mod) % n         mod; n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n bool readInteger(T& x) { n   char c, r = 0, n = 0; n   x = 0; n   for (;;) { n     c = getchar(); n     if ((c < 0) && (!r)) n       return (0); n     else if ((c ==  - ) && (!r)) n       n = 1; n     else if ((c >=  0 ) && (c <=  9 )) n       x = x * 10 + c -  0 , r = 1; n     else if (r) n       break; n   } n   if (n) x = -x; n   return (1); n } n template <typename T, T MOD> n struct ModInt { n   T value; n   ModInt() : value(0) {} n   ModInt(T x) { n     x %= MOD; n     if (x < 0) x += MOD; n     value = x; n   } n  n  private: n   T __________________(T ___, T ____) { n     if (!____) return ___; n     return __________________(____, ___ % ____); n   } n   T _____________(T _, T __, T ____, T ___) { n     T _____, ______, _______ = ____ - _; n     assert(!(_______ % __________________(__, ___))); n     for (_____ = ______ = 0; _____ - ______ != _______;) { n       _____ = (_______ + ______ + __ - 1) / __ * __; n       ______ = (_____ - _______ + ___ - 1) / ___ * ___; n     } n     return _____ + _; n   } n  n  public: n   ModInt& operator+=(ModInt x) { n     value += x.value; n     if (value >= MOD) value -= MOD; n     return *this; n   } n   ModInt& operator-=(ModInt x) { n     value -= x.value; n     if (value < 0) value += MOD; n     return *this; n   } n   ModInt& operator*=(ModInt x) { n     value *= x.value; n     value %= MOD; n     return *this; n   } n   ModInt& operator/=(ModInt x) { n     x.invert(); n     return *this *= x; n   } n   ModInt operator+(ModInt x) const { n     ModInt o = *this; n     o += x; n     return o; n   } n   ModInt operator-(ModInt x) const { n     ModInt o = *this; n     o -= x; n     return o; n   } n   ModInt operator*(ModInt x) const { n     ModInt o = *this; n     o *= x; n     return o; n   } n   ModInt operator/(ModInt x) const { n     ModInt o = *this; n     o /= x; n     return o; n   } n   bool operator==(ModInt x) const { return value == x.value; } n   bool operator!=(ModInt x) const { return !(*this == x); } n   ModInt operator-() const { return ModInt(0) - *this; } n   ModInt operator^(long long x) const { n     ModInt ret = 1; n     ModInt mul = *this; n     while (x) { n       if (x & 1) ret *= mul; n       mul *= mul; n       x >>= 1; n     } n     return ret; n   } n   ModInt& operator^=(long long x) { return *this = *this ^ x; } n  n  private: n   void invert() { *this ^= MOD - 2; } n  n  public: n   void answer() { std::cout << value << std::endl; } n }; n const int MX = 2.1e5; n int N, K; n int ai[MX]; n vector<int> E[MX]; n vector<int> child[MX]; n bool light[MX]; n long long dp[MX][5]; n const int NINF = -1e9; n void solve(int x) { n   for (auto& o : (child[x])) solve(o); n   long long useNow = NINF; n   { n     long long& ans = dp[x][4]; n     ans = NINF; n     if (light[x]) { n       { n         long long sum = 1; n         long long bestDelta = 0; n         for (auto& o : (child[x])) { n           sum += dp[o][3]; n           bestDelta = max(bestDelta, dp[o][4] - dp[o][3]); n         } n         sum += bestDelta; n         ans = max(ans, sum); n       } n       { n         long long sum = 1; n         long long bestA = 0; n         long long bestB = 0; n         for (auto& o : (child[x])) { n           long long v1 = max(0LL, dp[o][3]); n           long long v2 = dp[o][2]; n           sum += v1; n           long long delta = v2 - v1; n           if (delta > bestA) { n             bestB = bestA; n             bestA = delta; n           } else n             bestB = max(bestB, delta); n         } n         useNow = sum + bestA + bestB; n         ans = max(ans, useNow); n       } n     } n   } n   { n     long long& ans = dp[x][3]; n     ans = NINF; n     if (light[x]) { n       long long sum = 1; n       for (auto& o : (child[x])) sum += dp[o][3]; n       ans = max(ans, sum); n     } n   } n   { n     long long& ans = dp[x][2]; n     ans = NINF; n     if (light[x]) { n       long long sum = 1; n       long long bestDelta = 0; n       for (auto& o : (child[x])) { n         long long v1 = max(0LL, dp[o][3]); n         long long v2 = dp[o][2]; n         sum += v1; n         bestDelta = max(bestDelta, v2 - v1); n       } n       sum += bestDelta; n       ans = max(ans, sum); n     } n   } n   { n     long long& ans = dp[x][1]; n     ans = useNow; n     for (auto& o : (child[x])) ans = max(ans, dp[o][1]); n   } n } n int ROOT = 0; n bool ok(int threshold) { n   for (int i = (0); i < int(N); i++) light[i] = ai[i] >= threshold; n   if (light[ROOT]) return true; n   solve(ROOT); n   return dp[ROOT][1] >= K; n } n void init(int x, int par) { n   for (auto& o : (E[x])) n     if (o != par) { n       child[x].push_back(o); n       init(o, x); n     } n } n int main() { n   readInteger(N); n   readInteger(K); n   for (int i = (0); i < int(N); i++) readInteger(ai[i]); n   for (int k = (0); k < int(N - 1); k++) { n     int u, v; n     readInteger(u); n     readInteger(v); n     --u, --v; n     E[u].push_back(v); n     E[v].push_back(u); n   } n   for (int i = (0); i < int(N); i++) n     if (ai[i] < ai[ROOT]) ROOT = i; n   init(ROOT, -1); n   vector<int> vals(N); n   for (int i = (0); i < int(N); i++) vals[i] = ai[i]; n   sort(vals.begin(), vals.end()); n   reverse(vals.begin(), vals.end()); n   int lo = K - 1; n   int hi = N - 1; n   while (lo != hi) { n     int mid = (lo + hi) / 2; n     if (ok(vals[mid])) n       hi = mid; n     else n       lo = mid + 1; n   } n   printf( %d n , vals[lo]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int k; n   cin >> n; n   cin >> k; n   int array[n]; n   for (int i = 0; i < n; i++) { n     cin >> array[i]; n   } n   int e; n   int s; n   int a = 0; n   for (int i = 0; i < k; i++) { n     e = 0; n     s = 0; n     for (int j = 0; j < n; j++) { n       if (array[j] == 1 && j % k != i) { n         e++; n       } n       if (array[j] == -1 && j % k != i) { n         s++; n       } n     } n     if (abs(e - s) > a) { n       a = abs(e - s); n     } n   } n   cout << a; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200 + 5; n int a[maxn], b[maxn]; n int n, m, k, x; n int getloc(int x) { n   for (int i = 1; i <= k; i++) { n     if (a[i] == x) return i; n   } n } n void update(int x) { n   int pos = 0; n   for (int i = 1; i <= k; i++) { n     if (a[i] == x) pos = i; n   } n   b[1] = a[pos]; n   int t = 2; n   for (int i = 1; i <= k; i++) { n     if (i != pos) b[t++] = a[i]; n   } n   for (int i = 1; i <= k; i++) { n     a[i] = b[i]; n   } n } n int main() { n   while (scanf( %d%d%d , &n, &m, &k) != EOF) { n     for (int i = 1; i <= k; i++) scanf( %d , &a[i]); n     int ans = 0; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         scanf( %d , &x); n         ans += getloc(x); n         update(x); n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e6 + 3; n long long quickpow(long long a, long long x) { n   long long ans = 1; n   while (x) { n     if (x & 1) ans = ans * a % mod; n     x >>= 1; n     a = a * a % mod; n   } n   return ans; n } n int main() { n   long long n, k, x = 0, m = 0; n   cin >> n >> k; n   while ((1ll << x) < k) x++; n   if (x > n) { n     cout <<  1 1  << endl; n     return 0; n   } n   for (long long i = 2; i < k; i <<= 1) m += (k - 1) / i; n   long long p = quickpow(2, mod - 1 - m % (mod - 1)); n   long long ans = (quickpow(quickpow(2, n), k - 1) * p) % mod; n   if (mod <= k - 1) n     cout << ans <<     << ans << endl; n   else { n     long long ans1 = 1, ori = quickpow(2, n); n     for (long long i = 1; i < k; i++) ans1 = (ans1 * (ori - i)) % mod; n     ans1 = ans1 * p % mod; n     cout << ((ans - ans1) % mod + mod) % mod <<     << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100 * 1000 + 10; n pair<int, long double> song[maxn]; n int n; n long double ans, sum; n inline bool cmp(const pair<int, long double> &a, n                 const pair<int, long double> &b) { n   return ((long double)1 - a.second) * b.second * b.first < n          ((long double)1 - b.second) * a.second * a.first; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> song[i].first >> song[i].second; n     song[i].second *= (long double)0.01; n   } n   sort(song, song + n, cmp); n   for (int i = 0; i < n; i++) { n     ans += song[i].first; n     ans += ((long double)1 - song[i].second) * sum; n     sum += song[i].second * (long double)song[i].first; n   } n   cout << setprecision(12) << fixed << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortbysec(const pair<long long, pair<long long, long long> > &a, n                const pair<long long, pair<long long, long long> > &b) { n   return (a.second.first < b.second.first); n } n bool sortbyth(const pair<long long, pair<long long, long long> > &a, n               const pair<long long, pair<long long, long long> > &b) { n   return (a.second.second < b.second.second); n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n long long min(long long a, long long b) { return a < b ? a : b; } n long long max(long long a, long long b) { return a > b ? a : b; } n long long max3(long long a, long long b, long long c) { n   return max(max(a, b), c); n } n long long max4(long long a, long long b, long long c, long long d) { n   return max(max(a, b), max(c, d)); n } n long long min3(long long a, long long b, long long c) { n   return min(min(a, b), c); n } n long long min4(long long a, long long b, long long c, long long d) { n   return min(min(a, b), min(c, d)); n } n long long po(long long x, long long n, long long mod = 998244353) { n   if (n == 0) { n     return 1; n   } n   long long m = po(x, n / 2, mod); n   m *= m; n   m %= mod; n   if (n % 2) { n     m *= x; n     m %= mod; n   } n   return m; n } n long long n, m, x, y; n long long col[2][1005]; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   clock_t startTime = clock(); n   cin >> n >> m >> x >> y; n   char a[1005][1005]; n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < m; j++) { n       cin >> a[i][j]; n     } n   } n   for (long long i = 0; i < m; i++) { n     long long c = 0; n     for (long long j = 0; j < n; j++) { n       if (a[j][i] ==  # ) { n         c++; n       } n     } n     col[0][i] = c; n     col[1][i] = n - c; n   } n   long long dp[2][2][1005] = {}; n   for (long long i = 0; i < 2; i++) { n     for (long long j = 0; j < 1005; j++) { n       if (j >= x || j == 0) { n         dp[1][i][j] = 0; n       } else { n         dp[1][i][j] = INT_MAX; n       } n     } n   } n   for (long long index = m - 1; index >= 0; index--) { n     for (long long last = y - 1; last >= 0; last--) { n       for (long long color = 0; color < 2; color++) { n         if (last == y - 1) { n           dp[0][color][last] = dp[1][1 ^ color][0] + col[color][index]; n         } else if (last < x - 1) { n           dp[0][color][last] = dp[1][color][last + 1] + col[color][index]; n         } else { n           dp[0][color][last] = n               min(dp[1][color][last + 1], dp[1][1 ^ color][0]) + n               col[color][index]; n         } n       } n     } n     for (long long last = y - 1; last >= 0; last--) { n       for (long long color = 0; color < 2; color++) { n         dp[1][color][last] = dp[0][color][last]; n       } n     } n   } n   cout << min(dp[0][0][0], dp[0][1][0]) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int sgn(long long v) { return v > 0 ? 1 : 0; } n int main(void) { n   long long p, q, r, s; n   cin >> p >> q >> r >> s; n   int n; n   int res = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     long long temp1 = (a * p + b * q + c); n     long long temp2 = a * r + b * s + c; n     res = (sgn(temp1) == sgn(temp2)) ? res : res + 1; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n void solve() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   long long ans = 0; n   long long ca = 0, cab = 0, cabc = 0, slen = 1; n   for (char c : s) { n     if (c ==  a ) n       ca = (slen + ca) % mod; n     else if (c ==  b ) n       cab = (ca + cab) % mod; n     else if (c ==  c ) n       cabc = (cabc + cab) % mod; n     else { n       cabc = (3 * cabc + cab) % mod; n       cab = (ca + 3 * cab) % mod; n       ca = (slen + 3 * ca) % mod; n       slen = (3 * slen) % mod; n     } n   } n   cout << cabc <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const int M = (1 << 7); n const int mod = 1e9 + 7; n long long dp[N][M]; n struct node { n   int v, s[8]; n } a[N]; n bool cmp(node b, node d) { return b.v > d.v; } n int n, p, k; n int main() { n   scanf( %d%d%d , &n, &p, &k); n   int st = 1 << p; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i].v); n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < p; j++) scanf( %d , &a[i].s[j]); n   sort(1 + a, 1 + a + n, cmp); n   for (int i = 0; i <= n; i++) n     for (int j = 1; j < st; j++) dp[i][j] = -1e18; n   dp[0][0] = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < st; j++) { n       for (int k = 0; k < p; k++) n         if (j & (1 << k)) n           dp[i][j] = max(dp[i - 1][j - (1 << k)] + a[i].s[k], dp[i][j]); n       int p = __builtin_popcount(j); n       if ((i - 1) - p >= k) n         dp[i][j] = max(dp[i][j], dp[i - 1][j]); n       else n         dp[i][j] = max(dp[i][j], dp[i - 1][j] + a[i].v); n     } n   } n   printf( %lld n , dp[n][st - 1]); n } n


#include <bits/stdc++.h> n using namespace std; n const int logsize = 18; n int n, m, q, rt = 1, t, sc, vis[100005], dfn[100005], low[100005], sid[100005], n              sz[100005], dep[100005], pr[100005][logsize], w[100005], n              mod = 1e9 + 7; n stack<int> s; n vector<int> g[100005], ng[100005]; n int inline read() { n   int res = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     ; n   for (; isdigit(c); res *= 10, res += c ^ 48, c = getchar()) n     ; n   return res; n } n void tarjan(int u, int p) { n   dfn[u] = low[u] = ++t; n   vis[u] = 1; n   s.push(u); n   for (int v : g[u]) { n     if (v == p || vis[v] == 2) continue; n     if (!vis[v]) { n       tarjan(v, u); n     } n     low[u] = min(low[u], low[v]); n   } n   if (dfn[u] == low[u]) { n     ++sc; n     for (;;) { n       int v = s.top(); n       s.pop(); n       vis[v] = 2; n       sid[v] = sc; n       ++sz[sc]; n       if (u == v) break; n     } n   } n } n void dfs(int u, int p) { n   dep[u] = u == rt ? 0 : dep[p] + 1; n   w[u] = u == rt ? sz[u] > 1 : w[p] + (sz[u] > 1); n   pr[u][0] = p; n   for (int v : ng[u]) { n     if (v == p) continue; n     dfs(v, u); n   } n } n void inline init() { n   dfs(rt, -1); n   for (int i = 1; i <= logsize - 1; ++i) { n     for (int u = 1; u <= sc; ++u) { n       int p = pr[u][i - 1]; n       if (p == -1) { n         pr[u][i] = -1; n       } else { n         pr[u][i] = pr[p][i - 1]; n       } n     } n   } n } n int inline lca(int u, int v) { n   if (dep[u] > dep[v]) { n     swap(u, v); n   } n   for (int i = 0; i < logsize; ++i) { n     if ((dep[v] - dep[u]) >> i & 1) { n       v = pr[v][i]; n     } n   } n   if (u == v) return u; n   for (int i = logsize - 1; i >= 0; --i) { n     if (pr[u][i] != pr[v][i]) { n       u = pr[u][i]; n       v = pr[v][i]; n     } n   } n   return pr[u][0]; n } n int qpow(long long a, int p) { n   long long res = 1; n   while (p) { n     if (p & 1) { n       res *= a; n       res %= mod; n     } n     a *= a; n     a %= mod; n     p >>= 1; n   } n   return res % mod; n } n int main() { n   ios_base::sync_with_stdio(0); n   n = read(); n   m = read(); n   for (int i = 1; i <= m; ++i) { n     int u = read(), v = read(); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int u = 1; u <= n; ++u) { n     if (!vis[u]) { n       tarjan(u, -1); n     } n   } n   for (int u = 1; u <= n; ++u) { n     for (int v : g[u]) { n       int su = sid[u], sv = sid[v]; n       if (su == sv) continue; n       ng[su].push_back(sv); n     } n   } n   init(); n   q = read(); n   for (int i = 1; i <= q; ++i) { n     int u = read(), v = read(), su = sid[u], sv = sid[v], sp = lca(su, sv); n     int d = w[su] + w[sv] - w[sp] * 2 + (sz[sp] > 1); n     cout << qpow(2, d) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 10; n const long long int Mod = 998244353; n vector<int> adj[maxn]; n long long int dp[maxn], pd[maxn], alone[maxn]; n int n; n void dfs(int u, int p = -1) { n   dp[u] = pd[u] = 0; n   alone[u] = 1; n   for (auto v : adj[u]) n     if (v ^ p) { n       dfs(v, u); n       dp[u] = ((dp[u] * (2 * dp[v] + alone[v]) % Mod) + n                ((alone[u] + pd[u]) * (alone[v] + pd[v]) % Mod)) % n               Mod; n       pd[u] = (pd[u] * (dp[v] * 2 + alone[v]) + alone[u] * dp[v]) % Mod; n       alone[u] = (alone[u] * (alone[v] + dp[v])) % Mod; n     } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i < n; i++) { n     int fi, se; n     cin >> fi >> se; n     fi--, se--; n     adj[fi].push_back(se); n     adj[se].push_back(fi); n   } n   dfs(0); n   cout << (dp[0] + alone[0]) % Mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 11; n const int MAXN = 1e5 + 5; n const int LG = 20; n const int INF = 1e9; n using ll = long long int; n using ull = unsigned long long int; n const ll MOD2 = 998244353; n const ll MOD1 = 1e9 + 7LL; n vector<int> g[MAXN], cg[MAXN]; n vector<int> cpar(MAXN); n vector<int> ans(MAXN); n int n, m; n struct CDMP { n   vector<bool> dead; n   vector<int> par; n   int oneCentroid(int root) { n     static vector<int> sub(MAXN); n     function<void(int, int)> dfs = [&](int u, int p) { n       sub[u] = 1; n       for (int v : g[u]) { n         if (v == p || dead[v]) continue; n         dfs(v, u); n         sub[u] += sub[v]; n       } n     }; n     dfs(root, -1); n     int N = sub[root]; n     function<int(int, int)> findcenter = [&](int u, int p) { n       for (int v : g[u]) { n         if (v == p || dead[v]) continue; n         if (2 * sub[v] > N) return findcenter(v, u); n       } n       return u; n     }; n     return findcenter(root, -1); n   } n   void traverse(int u, int p, int d = 0) { n     par[u] = p; n     for (int v : cg[u]) { n       if (v != p) { n         traverse(v, u, d + 1); n       } n     } n   } n   vector<int> &decomposeTree() { n     par = vector<int>(MAXN, -1); n     dead = vector<bool>(MAXN, false); n     function<int(int)> rec = [&](int u) { n       int c = oneCentroid(u); n       dead[c] = true; n       for (int v : g[c]) n         if (!dead[v]) { n           int cv = rec(v); n           cg[c].push_back(cv); n           cg[cv].push_back(c); n         } n       return c; n     }; n     int r = rec(1); n     traverse(r, -1); n     return par; n   } n } cdmp; n struct computeLCA { n   int level[MAXN]; n   int par[MAXN][LG]; n   int findlca(int u, int v) { n     if (level[u] < level[v]) swap(u, v); n     int lg = 0; n     for (; (1 << lg) <= level[u]; ++lg) n       ; n     --lg; n     for (int i = lg; i >= 0; --i) n       if (level[u] - (1 << i) >= level[v]) u = par[u][i]; n     if (u == v) return u; n     for (int i = lg; i >= 0; --i) n       if (par[u][i] != -1 && par[u][i] != par[v][i]) n         u = par[u][i], v = par[v][i]; n     return par[u][0]; n   } n   int dist(int u, int v) { n     return level[u] + level[v] - 2 * level[findlca(u, v)]; n   } n   void dfs(int u, int p) { n     level[u] = 1 + level[p]; n     par[u][0] = p; n     for (int v : g[u]) n       if (v != p) { n         dfs(v, u); n       } n   } n   void build() { n     level[1] = 0; n     for (int i = 0; i <= n; ++i) n       for (int j = 0; j < LG; ++j) par[i][j] = -1; n     dfs(1, 1); n     for (int l = 1; l < LG; ++l) n       for (int i = 1; i <= n; ++i) n         if (par[i][l - 1] != -1) { n           par[i][l] = par[par[i][l - 1]][l - 1]; n         } n   } n } lca; n void update(int x) { n   int node = x; n   while (node > 0) { n     ans[node] = min(ans[node], lca.dist(node, x)); n     node = cpar[node]; n   } n } n int query(int x) { n   int node = x; n   int ret = INF; n   while (node > 0) { n     ret = min(ret, lca.dist(node, x) + ans[node]); n     node = cpar[node]; n   } n   return ret; n } n int main() { n   scanf( %d , &n); n   scanf( %d , &m); n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     scanf( %d , &u); n     scanf( %d , &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 0; i <= n; ++i) ans[i] = INF; n   cpar = cdmp.decomposeTree(); n   lca.build(); n   update(1); n   while (m--) { n     int t, x; n     scanf( %d , &t); n     scanf( %d , &x); n     if (t == 1) { n       update(x); n     } else { n       printf( %d , query(x)); n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n, j, temp, cnt = 0; n     cin >> n; n     string s; n     cin >> s; n     for (int i = 0; i < n; i++) { n       temp = 0; n       for (int j = 0; j < n - 1; j++) { n         if (s[j] ==  A  && s[j + 1] ==  P ) { n           s[j + 1] =  A ; n           j++; n           temp = 1; n         } n       } n       if (temp == 1) { n         cnt++; n       } else n         break; n     } n     cout << cnt << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, m, k; n     cin >> n >> m >> k; n     vector<vector<int> > g(n); n     vector<int> deg(n); n     for (int i = 0; i < m; i++) { n       int x, y; n       cin >> x >> y; n       --x; n       --y; n       g[x].push_back(y); n       g[y].push_back(x); n       deg[x]++; n       deg[y]++; n     } n     for (int i = 0; i < n; i++) sort(g[i].begin(), g[i].end()); n     vector<bool> used(n), vis(n); n     vector<int> q(n); n     int top = 0; n     vector<int> vert(k); n     bool f = 0; n     for (int i = 0; i < n; i++) { n       used[i] = vis[i] = 0; n       if (deg[i] < k) { n         vis[i] = 1; n         q[top++] = i; n       } n     } n     for (int i = 0; i < top; i++) { n       int u = q[i]; n       vis[u] = 0; n       used[u] = 1; n       if (deg[u] < k - 1) { n         for (auto j : g[u]) { n           if (!used[j]) --m; n           deg[j]--; n           if (!vis[j] and !used[j] and deg[j] < k) { n             vis[j] = 1; n             q[top++] = j; n           } n         } n         continue; n       } else if (deg[u] == k - 1) { n         if ((long long)k * (k - 1) <= 2 * (long long)m) { n           int r = 0; n           for (auto p : g[u]) { n             if (!used[p]) vert[r++] = p; n           } n           bool ff = 1; n           for (int x = 0; x < r; x++) { n             if (!ff) break; n             for (int y = x + 1; y < r; y++) { n               int pp = n                   lower_bound(g[vert[x]].begin(), g[vert[x]].end(), vert[y]) - n                   g[vert[x]].begin(); n               if (pp == g[vert[x]].size() or g[vert[x]][pp] != vert[y]) { n                 ff = 0; n                 break; n               } n             } n           } n           if (ff) { n             f = 1; n             cout <<  2 n  << u + 1 <<    ; n             for (int c = 0; c < r; c++) cout << vert[c] + 1 <<    ; n             cout <<   n ; n             break; n           } n         } n       } n       for (auto j : g[u]) { n         deg[j]--; n         if (!used[j]) --m; n         if (!vis[j] and !used[j] and deg[j] < k) { n           vis[j] = 1; n           q[top++] = j; n         } n       } n     } n     if (f) continue; n     vector<int> ff; n     for (int i = 0; i < n; i++) n       if (!used[i]) ff.push_back(i); n     if (!ff.empty()) { n       cout <<  1   << ff.size() <<   n ; n       for (auto c : ff) { n         c++; n         cout << c <<    ; n       } n       cout <<   n ; n     } else n       cout <<  -1 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int i, n, *a, t, j, flag; n   long m; n   scanf( %d , &n); n   scanf( %ld , &m); n   a = (int*)calloc(n, sizeof(int)); n   for (i = 0; i < n; i++) scanf( %d , &a[i]); n   for (i = 0; i < n; i++) n     for (j = 0; j < n - i - 1; j++) n       if (a[j + 1] > a[j]) { n         t = a[j + 1]; n         a[j + 1] = a[j]; n         a[j] = t; n       } n   flag = 1; n   for (i = 0; i < n && flag; i++) { n     m = m - a[i]; n     if (m <= 0) flag = 0; n   } n   printf( %d , i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int i32INF = 1 << 30; n const long long i64INF = 1LL << 60; n const double mINF = 1e100; n const double EPS = 1e-9; n int nextint() { n   int a; n   scanf( %d , &a); n   return a; n } n int n; n vector<int> v; n int difs[23][23]; n bool isGood[1 << 23]; n bool solveIsGood(int mask) { n   int id = n; n   while ((mask & (1 << (id - 1))) == 0) id--; n   for (int i = 0; i < n; i++) n     if (mask & (1 << i) && mask & difs[id][i]) return true; n   return false; n } n int dp[1 << 23]; n int main() { n   n = nextint(); n   v.resize(n); n   for (int i = 0; i < n; i++) v[i] = nextint(); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (i != j) n         for (int k = 0; k < n; k++) n           if (v[i] - v[j] == v[k]) difs[i][j] |= (1 << k); n   isGood[0] = 1; n   for (int mask = 1; mask < (1 << (n - 1)); mask++) n     isGood[mask] = solveIsGood(mask); n   fill(dp, dp + (1 << n), 1024); n   dp[1] = 1; n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < (1 << (i - 1)); j++) { n       int jj = j | (1 << (i - 1)); n       if (isGood[jj]) { n         int cr = dp[jj]; n         jj |= 1 << i; n         dp[jj] = min(dp[jj], cr + 1); n         for (int k = 0; k < i; k++) n           if (jj & (1 << k)) n             dp[jj & (~(1 << k))] = min(dp[jj & (~(1 << k))], cr); n       } n     } n   } n   int res = 1024; n   for (int mask = 0; mask < (1 << (n - 1)); mask++) n     res = min(res, dp[mask | (1 << (n - 1))]); n   if (res == 1024) res = -1; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, h, t, x, y; n vector<int> s[100100]; n int a[100100], b[100100]; n bool bj[100100][2]; n void out(int u, int v) { n   int su = s[u].size(), sv = s[v].size(); n   int same = 0, i = 0, j = 0; n   while (i < su && j < sv) { n     if (s[u][i] == s[v][j]) n       same++, bj[i][0] = 1, bj[j][1] = 1, i++, j++; n     else if (s[u][i] > s[v][j]) n       j++; n     else n       i++; n   } n   printf( YES n%d %d n , u, v); n   for (int i = 0; i < su; i++) n     if (s[u][i] != v) { n       if (!h) break; n       if (!bj[i][0]) { n         printf( %d  , s[u][i]); n         h--; n       } n     } n   for (int i = 0; i < su; i++) n     if (s[u][i] != v) { n       if (!h) break; n       if (bj[i][0]) { n         printf( %d  , s[u][i]); n         h--; n       } n     } n   puts(  ); n   for (int i = 0; i < sv; i++) n     if (s[v][i] != u) { n       if (!t) break; n       if (!bj[i][1]) { n         printf( %d  , s[v][i]); n         t--; n       } n     } n   for (int i = sv - 1; i >= 0; i--) n     if (s[v][i] != u) { n       if (!t) break; n       if (bj[i][1]) { n         printf( %d  , s[v][i]); n         t--; n       } n     } n   puts(  ); n } n bool found(int u, int v) { n   int su = s[u].size(), sv = s[v].size(); n   if (su - 1 < h || sv - 1 < t) return 0; n   int same = 0, i = 0, j = 0; n   while (i < su && j < sv) { n     if (s[u][i] == s[v][j]) n       same++, i++, j++; n     else if (s[u][i] > s[v][j]) n       j++; n     else n       i++; n   } n   if (su + sv - same - 2 >= h + t) { n     out(u, v); n     return 1; n   } n   return 0; n } n int main() { n   scanf( %d%d%d%d , &n, &m, &h, &t); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &x, &y); n     s[x].push_back(y); n     s[y].push_back(x); n     a[i] = x, b[i] = y; n   } n   for (int i = 1; i <= n; i++) sort(s[i].begin(), s[i].end()); n   for (int i = 1; i <= m; i++) { n     if (found(a[i], b[i])) return 0; n     if (found(b[i], a[i])) return 0; n   } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> pr; n vector<bool> used(1000001, false); n vector<int> d(1000001, 0); n void sieve() { n   for (long long i = 2; i <= 1000000; i++) { n     for (long long k = i * i; k <= 1000000; k += i) used[k] = true; n   } n   for (int i = 2; i <= 1000000; i++) n     if (!used[i]) pr.push_back(i); n } n int main() { n   sieve(); n   d[1] = 1; n   for (int i = 2; i <= 1000000; i++) { n     d[i] = d[i - 1]; n     int k = lower_bound(pr.begin(), pr.end(), i) - pr.begin(); n     int k1 = upper_bound(pr.begin(), pr.end(), i) - pr.begin(); n     if (k1 - k > 0) { n       d[i]++; n       continue; n     } n     int x = sqrt(i); n     if (x * x == i) { n       k = lower_bound(pr.begin(), pr.end(), x) - pr.begin(); n       k1 = upper_bound(pr.begin(), pr.end(), x) - pr.begin(); n       if (k1 - k > 0) { n         d[i]--; n       } n     } n   } n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     printf( %d n , d[n]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 4e5 + 4; n const int oo = 1e8 + 7; n int n, a[maxn], cc, p; n long long s[maxn]; n int main() { n   cin >> n; n   a[1] = 1; n   a[2] = 2 * n; n   a[n + 1] = 2; n   a[n + 2] = 2 * n - 1; n   int cc = 3; n   for (int i = 3; i <= (n); ++i) { n     if (i % 2) { n       a[i] = cc; n       a[i + n] = cc + 1; n       cc += 2; n     } else { n       a[i + n] = cc; n       a[i] = cc + 1; n       cc += 2; n     } n   } n   long long k = 0; n   for (int i = 1; i <= (2 * n); ++i) k += i; n   for (int i = 2 * n + 1; i <= (4 * n); ++i) a[i] = a[i - 2 * n]; n   for (int i = 1; i <= (4 * n); ++i) s[i] = s[i - 1] + a[i]; n   k = k / 2; n   k++; n   for (int i = 1; i <= (3 * n); ++i) { n     long long x = s[i + n - 1] - s[i - 1]; n     if (abs(x - k) > 1) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES n ; n   for (int i = 1; i <= (2 * n); ++i) cout << a[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int n, c[N], k = 1e5; n char a[N], b[N]; n void solve(int p, int coef) { n   if (a[p - 1] + coef <  0  || a[p - 1] + coef >  9 ) { n     solve(p - 1, -coef); n   } n   a[p - 1] += coef; n   a[p] += coef; n   printf( %d %d n , p - 1, coef); n   if (!--k) { n     exit(0); n   } n } n int main() { n   scanf( %d%s%s , &n, a + 1, b + 1); n   copy(a + 1, a + 1 + n, c + 1); n   long long answer = 0; n   for (int i = 1; i < n; ++i) { n     if (c[i] != b[i]) { n       answer += abs(c[i] - (int)b[i]); n       c[i + 1] += (int)b[i] - c[i]; n     } n   } n   if (c[n] != b[n]) { n     return puts( -1 ); n   } else { n     printf( %lld n , answer); n     if (answer < k) { n       k = answer; n     } n     for (int i = n; i; --i) { n       for (; a[i] != b[i]; solve(i, a[i] < b[i] ? 1 : -1)) n         ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void print_ans(int x1, int y1, int x2, int y2) { n   long long int ans; n   ans = (x1 - x2) * (y1 - y2); n   if (ans < 0) ans = -ans; n   cout << ans; n   return; n } n int main() { n   int n, x[4], y[4], i; n   cin >> n; n   if (n < 2) n     cout <<  -1 ; n   else { n     for (i = 0; i < n; i++) cin >> x[i] >> y[i]; n     if (n == 2) { n       if (x[0] == x[1] || y[0] == y[1]) n         cout <<  -1 ; n       else n         print_ans(x[0], y[0], x[1], y[1]); n     } else { n       if (x[0] != x[1] && y[0] != y[1]) n         print_ans(x[0], y[0], x[1], y[1]); n       else if (x[1] != x[2] && y[1] != y[2]) n         print_ans(x[1], y[1], x[2], y[2]); n       else n         print_ans(x[0], y[0], x[2], y[2]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool vv[2000][2000]; n int main() { n   int n; n   string s; n   cin >> s; n   long long left[2000], right[2000]; n   for (int i = 0; i < s.length(); i++) { n     vv[i][i] = true; n   } n   int sm = 1, cp = s.length() - 1; n   for (; sm < s.length(); sm++) { n     for (int i = sm, j = 0; i < s.length(); i++, j++) { n       if (s[j] == s[j + sm] && (sm == 1 || vv[j + 1][j + sm - 1])) { n         vv[j][j + sm] = true; n       } else { n         vv[j][j + sm] = false; n       } n     } n   } n   for (int i = 0; i < s.length(); i++) { n     left[i] = 1; n     right[i] = 1; n     for (int j = 0; j < i; j++) { n       if (vv[j][i]) left[i]++; n       if (vv[s.length() - (i + 1)][s.length() - (j + 1)]) right[i]++; n     } n   } n   long long res = 0, right_sum = 0; n   for (int i = 0; i < s.length() - 1; i++) right_sum += right[i]; n   for (int i = 0; i < s.length() - 1; i++) { n     res += left[i] * right_sum; n     right_sum -= right[s.length() - (i + 2)]; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<pair<long long, pair<long long, long long>>> pq; n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int T = 1; n   while (T--) { n     bool flag = true, cond = true; n     long long n, i, j, x, y, ind, k, m, mn = INT_MAX, p = 0, mx = 0, diff, y1, n                                         count = 0; n     cin >> n; n     vector<long long> a(n), b(n); n     for (i = 0; i < n; i++) { n       cin >> a[i]; n     } n     map<long long, long long> mp; n     for (j = 0; j < n; j++) { n       cin >> b[j]; n       mp[b[j]]++; n     } n     vector<long long> c; n     map<long long, long long>::iterator it; n     for (i = 0; i < n; i++) { n       it = mp.lower_bound(n - a[i]); n       if (it == mp.end()) { n         it = mp.begin(); n         c.push_back((a[i] + it->first) % n); n         if (it->second == 1) n           mp.erase(it->first); n         else n           mp[it->first]--; n       } else { n         c.push_back((a[i] + it->first) % n); n         if (it->second == 1) n           mp.erase(it->first); n         else n           mp[it->first]--; n       } n     } n     for (int iz = 0; iz < c.size(); iz++) { n       cout << c[iz] <<    ; n     } n     cout <<   n ; n     ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s[50005]; n bool cmp(const string &a, const string &b) { return a + b < b + a; } n int main() { n   int n, i; n   cin >> n; n   for (i = 0; i < n; i++) cin >> s[i]; n   sort(s, s + n, cmp); n   string ans = accumulate(s, s + n, string()); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxs = 300 * 1000 + 5, Maxn = 300 * 1000 + 5; n int n, dp[Maxs]; n string s, t; n long long ans; n vector<pair<int, string> > adj[Maxn]; n void kmp() { n   dp[0] = -1; n   for (int i = 0; i < t.size(); i++) { n     int pt = i; n     while (dp[pt] != -1 && t[dp[pt]] != t[i]) pt = dp[pt]; n     dp[i + 1] = dp[pt] + 1; n   } n } n double now; n void dfs(int v, int indt) { n   for (auto u : adj[v]) { n     int indt2 = indt; n     for (int j = 0; j < u.second.size(); ++j) { n       if (clock() - now == CLOCKS_PER_SEC * 0.990) { n         cout << ans << endl; n         exit(0); n       } n       if (indt2 == t.size()) ans++, indt2 = dp[indt2]; n       if (indt2 >= 0 && indt2 < t.size() && u.second[j] == t[indt2]) n         indt2++; n       else if (indt2 != 0 && indt2 >= 0) n         indt2 = dp[indt2], j--; n     } n     if (indt2 == t.size()) ans++, indt2 = dp[indt2]; n     dfs(u.first, indt2); n   } n } n int main() { n   now = clock(); n   cin >> n; n   int par; n   for (int i = 1; i < n; i++) n     cin >> par >> s, par--, adj[par].push_back(make_pair(i, s)); n   cin >> s; n   t = s; n   kmp(); n   dfs(0, 0); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int calc(long long n, long long k, long long a1, long long a2, long long a3) { n   long long sum; n   if (a2 < a3) { n     swap(a2, a3); n   } n   if (a1 < a2) { n     swap(a1, a2); n   } n   if (a2 < a3) { n     swap(a2, a3); n   } n   if (a1 < 0 || a2 < 0 || a3 < 0) { n     return 0; n   } n   sum = a1 - a2 + a1 - a3; n   if (sum > n - k) { n     return 0; n   } n   if ((n - k - sum) % 3 == 0) { n     return 1; n   } n   return 0; n } n int task() { n   long long i, n, k, d1, d2, a1, a2, a3; n   cin >> n; n   ; n   cin >> k; n   ; n   cin >> d1; n   ; n   cin >> d2; n   ; n   a1 = k + 2 * d1 + d2; n   if (a1 % 3 == 0) { n     a1 = a1 / 3; n     a2 = a1 - d1; n     a3 = a2 - d2; n     if (calc(n, k, a1, a2, a3)) { n       return 1; n     } n   } n   a1 = k + 2 * d1 - d2; n   if (a1 % 3 == 0) { n     a1 = a1 / 3; n     a2 = a1 - d1; n     a3 = a2 + d2; n     if (calc(n, k, a1, a2, a3)) { n       return 1; n     } n   } n   a1 = k - 2 * d1 + d2; n   if (a1 % 3 == 0) { n     a1 = a1 / 3; n     a2 = a1 + d1; n     a3 = a2 - d2; n     if (calc(n, k, a1, a2, a3)) { n       return 1; n     } n   } n   a1 = k - 2 * d1 - d2; n   if (a1 % 3 == 0) { n     a1 = a1 / 3; n     a2 = a1 + d1; n     a3 = a2 + d2; n     if (calc(n, k, a1, a2, a3)) { n       return 1; n     } n   } n   return 0; n } n int main(int argc, char const *argv[]) { n   int i, t; n   string yes( yes ); n   string no( no ); n   cin >> t; n   ; n   for (i = 0; i < t; i++) { n     if (task()) { n       cout << yes << endl; n       ; n     } else { n       cout << no << endl; n       ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, f[1000010], mn, rt, ans; n int cnt, head[1000010], to[2000010], nxt[2000010]; n void add(int x, int y) { n   cnt++; n   to[cnt] = y; n   nxt[cnt] = head[x]; n   head[x] = cnt; n } n void dfs(int x, int fa, int now) { n   f[x] = now; n   for (int i = head[x]; i; i = nxt[i]) { n     int y = to[i]; n     if (y == fa) { n       continue; n     } n     dfs(y, x, min(now, y)); n   } n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     add(x, y); n     add(y, x); n   } n   q--; n   scanf( %d , &rt); n   scanf( %d , &rt); n   rt = rt % n + 1; n   mn = rt; n   dfs(rt, 0, rt); n   while (q--) { n     int op, x; n     scanf( %d%d , &op, &x); n     x = (x + ans) % n + 1; n     if (op == 1) { n       mn = min(mn, f[x]); n     } else { n       ans = min(mn, f[x]); n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long n, a, b; n   cin >> n >> a >> b; n   for (long long i = 0; i <= n; ++i) { n     if (a * i > n) { n       break; n     } n     long long cur = a * i; n     if (n - cur >= 0 && (n - cur) % b == 0) { n       cout <<  YES n ; n       cout << i <<     << (n - cur) / b; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t; n long long r, g, b, k = 0; n long long a[4]; n int main() { n   cin >> t; n   while (t--) { n     cin >> a[1] >> a[2] >> a[3]; n     sort(a + 1, a + 1 + 3); n     k = a[1] + a[2]; n     if (a[3] >= k) n       cout << k << endl; n     else { n       k -= a[3]; n       r = min(a[1], k / 2) + a[3]; n       cout << r << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1000 * 1000 + 50; n vector<int> v[maxN]; n int cnt; n bool mark[maxN]; n void dfs(int s) { n   mark[s] = 1; n   cnt++; n   for (int i = 0; i < v[s].size(); i++) n     if (mark[v[s][i]] == 0) dfs(v[s][i]); n } n int main() { n   int n, m, k, sum = 0, com = 0; n   cin >> n >> m >> k; n   for (int i = 1; i <= m; i++) { n     int a, b; n     cin >> a >> b; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   for (int i = 1; i <= n; i++) n     if (mark[i] == 0) { n       com++; n       cnt = 0; n       dfs(i); n       sum += min(k, cnt); n     } n   if (k == 1) n     cout << max(0, com - 2) << endl; n   else n     cout << max(0, com - 1 - (sum / 2)) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string x; n   int i; n   cin >> x; n   for (i = 0; x[i]; i++) { n     if (i == 0 && x[i] == 57) n       x[i] = x[i]; n     else if (x[i] >= 53) n       x[i] = (57 - x[i]) + 48; n     cout << x[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 105; n struct Point { n   long long x, y; n   Point() { x = y = 0; } n   Point(long long _x, long long _y) { n     x = _x; n     y = _y; n   } n   void input() { cin >> x >> y; } n   Point operator+(const Point &a) const { return Point(x + a.x, y + a.y); } n   Point operator-(const Point &a) const { return Point(x - a.x, y - a.y); } n   Point Rotate(const Point &a) const { return Point(-a.y, a.x); } n   long long modul(void) const { return 1LL * x + 1LL * y; } n   bool operator<(const Point &a) const { n     return (x == a.x ? y < a.y : x < a.x); n   } n   bool operator==(const Point &a) const { return (x == a.x && y == a.y); } n }; n Point rotation(const Point &o, const Point &a, long long t) { n   Point res = a; n   while (t--) { n     res = o + o.Rotate(res - o); n   } n   return res; n } n long long dis(Point u, Point v) { n   long long dx = u.x - v.x; n   long long dy = u.y - v.y; n   return dx * dx + dy * dy; n } n bool isSquare(vector<Point> &u) { n   vector<Point> mi = u; n   sort(mi.begin(), mi.end()); n   map<long long, long long> xx, yy; n   for (long long i = 0; i < mi.size(); i++) { n     xx[mi[i].x]++; n     yy[mi[i].y]++; n     if (xx[mi[i].x] == 3 || yy[mi[i].y] == 3) return 0; n   } n   vector<long long> d; n   for (long long i = 0; i < 4; i++) { n     for (long long j = i + 1; j < 4; j++) d.push_back(dis(mi[i], mi[j])); n   } n   sort(d.begin(), d.end()); n   if (d[0] == d[3] && d[4] == d[5] && d[0] && d[3] + d[0] == d[4]) return 1; n   return 0; n } n bool isRectangle(vector<Point> &u) { n   vector<Point> mi = u; n   sort(mi.begin(), mi.end()); n   map<long long, long long> xx, yy; n   for (long long i = 0; i < mi.size(); i++) { n     xx[mi[i].x]++; n     yy[mi[i].y]++; n     if (xx[mi[i].x] == 3 || yy[mi[i].y] == 3) return 0; n   } n   vector<long long> d; n   for (long long i = 0; i < 4; i++) { n     for (long long j = i + 1; j < 4; j++) d.push_back(dis(mi[i], mi[j])); n   } n   sort(d.begin(), d.end()); n   if (d[0] == d[1] && d[2] == d[3] && d[4] == d[5] && d[0] && n       d[0] + d[2] == d[5]) n     return 1; n   return 0; n } n void run() { n   Point pos[9]; n   int col[9]; n   for (long long i = 1; i <= 8; i++) { n     pos[i].input(); n   } n   bool ok = 0; n   for (int i = 1; i <= 8; i++) { n     for (int j = i + 1; j <= 8; j++) { n       for (int k = j + 1; k <= 8; k++) { n         for (int t = k + 1; t <= 8; t++) { n           for (long long run = 1; run <= 8; run++) n             col[run] = (run == j || run == k || run == t || run == i); n           vector<Point> rec; n           vector<Point> square; n           for (long long run = 1; run <= 8; run++) n             if (col[run]) n               rec.push_back(pos[run]); n             else n               square.push_back(pos[run]); n           if (!isSquare(square)) continue; n           if (!isRectangle(rec)) continue; n           cout <<  YES  << endl; n           for (long long run = 1; run <= 8; run++) n             if (!col[run]) cout << run <<    ; n           cout << endl; n           for (long long run = 1; run <= 8; run++) n             if (col[run]) cout << run <<    ; n           return; n         } n       } n     } n   } n   cout <<  NO ; n } n int main() { n   ios_base::sync_with_stdio(false); n   run(); n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, n, k, tot, ans, sum; n int a[100005]; n string st; n char ch; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   if (n == 2) { n     cout << 3 << endl; n     return 0; n   } n   if (n == 3) { n     cout << 5 << endl; n     return 0; n   } n   for (i = 1; i <= 100; i++) { n     if (i % 2 == 0) continue; n     j = (i * i + 1) / 2; n     if (j >= n) break; n   } n   cout << i << endl; n } n


#include <bits/stdc++.h> n using namespace std; n  n typedef long long ll; n typedef pair <int, int> ii; n  n const int Maxn = 200005; n const ll Inf = 1000000000000000000ll; n  n int T; n int n; n vector <int> neigh[Maxn]; n int a[Maxn]; n int mylvl[Maxn]; n vector <ii> L[Maxn]; n int mxlvl; n ll best[Maxn]; n ll dp[Maxn]; n  n void Traverse(int v, int p, int lvl) n { n  mylvl[v] = lvl; n  L[lvl].push_back(ii(a[v], v)); n  mxlvl = max(mxlvl, lvl); n  for (int i = 0; i < neigh[v].size(); i++) { n   int u = neigh[v][i]; n   if (u == p) continue; n   Traverse(u, v, lvl + 1); n  } n } n  n int main() n { n  scanf( %d , &T); n  while (T--) { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) n    neigh[i].clear(); n   for (int i = 2; i <= n; i++) { n    int a; scanf( %d , &a); n    neigh[i].push_back(a); n    neigh[a].push_back(i); n   } n   for (int i = 2; i <= n; i++) n    scanf( %d , &a[i]); n   mxlvl = 0; n   Traverse(1, 0, 0); n   for (int i = mxlvl; i >= 0; i--) { n    sort(L[i].begin(), L[i].end()); n    ll all = -Inf, notall = -Inf; n    for (int j = 0; j < L[i].size(); j++) { n     int v = L[i][j].second; n     best[v] = 0; n     for (int z = 0; z < neigh[v].size(); z++) { n      int u = neigh[v][z]; n      if (mylvl[u] <= mylvl[v]) continue; n      best[v] = max(best[v], dp[u]); n     } n     all = max(all, best[v] - L[i][j].first); n     notall = max(notall, -ll(L[i][j].first)); n     dp[v] = max(all + L[i][j].first, notall + L[i][j].first + best[v]); n    } n    all = -Inf, notall = -Inf; n    for (int j = int(L[i].size()) - 1; j >= 0; j--) { n     int v = L[i][j].second; n     dp[v] = max(dp[v], max(all - L[i][j].first, notall - L[i][j].first + best[v])); n     all = max(all, best[v] + L[i][j].first); n     notall = max(notall, ll(L[i][j].first)); n    } n    L[i].clear(); n   } n   printf( %I64d n , dp[1]); n  } n     return 0; n } n


#include <bits/stdc++.h> n const long long md = (1e9 + 7); n const long long fakmd = 998244353; n using namespace std; n long long n, m; n pair<long long, long long> a[100100]; n long long ans[100100]; n int32_t main() { n   cin >> n >> m; n   long long it = 1; n   long long sum = 0; n   for (long long i = 1; i <= m; i++) { n     cin >> a[i].first; n     sum += a[i].first; n     a[i].second = i; n   } n   if (sum < n) { n     return cout << -1, 0; n   } n   for (long long i = 1; i <= m; i++) { n     sum -= a[i].first; n     long long ln = max((n - it + 1) - sum, 1ll); n     if (ln > a[i].first || it + a[i].first - 1 > n) { n       return cout << -1, 0; n     } n     ans[a[i].second] = it; n     it += ln; n   } n   if (it < n) { n     cout << -1; n   } else { n     for (long long i = 1; i <= m; i++) { n       cout << ans[i] <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void clear(T &x) { n   T t; n   x.swap(t); n } n template <class T> n void miz(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n void maz(T &a, T b) { n   if (b > a) a = b; n } n template <class T> n T gcd(T a, T b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n struct Shoe { n   int p, i; n   Shoe() {} n   Shoe(int p, int i) : p(p), i(i) {} n }; n struct Customer { n   int m, f, i; n   bool operator<(const Customer &q) const { return f < q.f; } n }; n const int CMAX = 100000; n map<int, Shoe> bst; n int C; n Customer cus[CMAX]; n Shoe sa[CMAX], sb[CMAX]; n long long dp[CMAX][2][2]; n int pd[CMAX][2][2]; n void MAZ(int i, int j, int k, long long v1, int v2) { n   if (v1 > dp[i][j][k]) { n     dp[i][j][k] = v1; n     pd[i][j][k] = v2; n   } n } n void solve() { n   int i, j, k; n   long long x; n   for (i = C - 1; i >= 0; i--) { n     for (j = 0; j < 2; j++) { n       for (k = 0; k < 2; k++) { n         if (i != C - 1) { n           if (cus[i + 1].f == cus[i].f) { n             dp[i][j][k] = dp[i + 1][j][k]; n           } else if (cus[i + 1].f == cus[i].f + 1) { n             dp[i][j][k] = dp[i + 1][k][0]; n           } else { n             dp[i][j][k] = dp[i + 1][0][0]; n           } n         } n         if (!j && cus[i].m >= sa[i].p) { n           if (i == C - 1) { n             x = 0; n           } else if (cus[i + 1].f == cus[i].f) { n             x = dp[i + 1][1][k]; n           } else if (cus[i + 1].f == cus[i].f + 1) { n             x = dp[i + 1][k][0]; n           } else { n             x = dp[i + 1][0][0]; n           } n           MAZ(i, j, k, sa[i].p + x, 1); n         } n         if (!k && cus[i].m >= sb[i].p) { n           if (i == C - 1) { n             x = 0; n           } else if (cus[i + 1].f == cus[i].f) { n             x = dp[i + 1][j][1]; n           } else if (cus[i + 1].f == cus[i].f + 1) { n             x = dp[i + 1][1][0]; n           } else { n             x = dp[i + 1][0][0]; n           } n           MAZ(i, j, k, sb[i].p + x, 2); n         } n       } n     } n   } n   printf( %I64d n , dp[0][0][0]); n   vector<pair<int, int> > ans; n   i = 0; n   j = 0; n   k = 0; n   while (1) { n     if (pd[i][j][k] == 0) { n       if (i == C - 1) { n         break; n       } else if (cus[i + 1].f == cus[i].f) { n         i++; n       } else if (cus[i + 1].f == cus[i].f + 1) { n         i++; n         j = k; n         k = 0; n       } else { n         i++; n         j = 0; n         k = 0; n       } n     } else if (pd[i][j][k] == 1) { n       ans.push_back(make_pair(cus[i].i, sa[i].i)); n       if (i == C - 1) { n         break; n       } else if (cus[i + 1].f == cus[i].f) { n         i++; n         j = 1; n       } else if (cus[i + 1].f == cus[i].f + 1) { n         i++; n         j = k; n         k = 0; n       } else { n         i++; n         j = 0; n         k = 0; n       } n     } else if (pd[i][j][k] == 2) { n       ans.push_back(make_pair(cus[i].i, sb[i].i)); n       if (i == C - 1) { n         break; n       } else if (cus[i + 1].f == cus[i].f) { n         i++; n         k = 1; n       } else if (cus[i + 1].f == cus[i].f + 1) { n         i++; n         j = 1; n         k = 0; n       } else { n         i++; n         j = 0; n         k = 0; n       } n     } n   } n   printf( %d n , ans.size()); n   for (i = 0; i < ans.size(); i++) n     printf( %d %d n , ans[i].first, ans[i].second); n } n void GetShoe(int s, Shoe &shoe) { n   map<int, Shoe>::iterator it; n   it = bst.find(s); n   shoe = (it != bst.end() ? it->second : Shoe(INT_MAX, -1)); n } n void input() { n   int S, i; n   scanf( %d , &S); n   for (i = 1; i <= S; i++) { n     int p, s; n     scanf( %d%d , &p, &s); n     bst.insert(make_pair(s, Shoe(p, i))); n   } n   scanf( %d , &C); n   for (i = 0; i < C; i++) { n     scanf( %d%d , &cus[i].m, &cus[i].f); n     cus[i].i = i + 1; n   } n   sort(cus, cus + C); n   for (i = 0; i < C; i++) { n     GetShoe(cus[i].f, sa[i]); n     GetShoe(cus[i].f + 1, sb[i]); n   } n } n int main() { n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   unsigned long long k, d, t; n   cin >> k >> d >> t; n   if (k % d == 0) { n     printf( %lld n , t); n   } else { n     unsigned long long num = k / d; n     unsigned long long num1 = k % d; n     unsigned long long t1 = (num + 1) * d; n     double onezhi = num * d + (d - num1) * 0.5 + num1; n     unsigned long long ans1 = t / onezhi; n     double t2 = t - onezhi * ans1; n     double ans; n     if (t2 <= k) { n       ans = t2 + t1 * ans1; n     } else { n       ans = t1 * ans1 + k + (t2 - k) * 2; n     } n     printf( %.9lf n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5e3 + 51, MOD = 998244353; n int n, res; n int p[MAXN][MAXN], fact[MAXN], finv[MAXN]; n inline int read() { n   register int num = 0, neg = 1; n   register char ch = getchar(); n   while (!isdigit(ch) && ch !=  - ) { n     ch = getchar(); n   } n   if (ch ==  - ) { n     neg = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     num = (num << 3) + (num << 1) + (ch -  0 ); n     ch = getchar(); n   } n   return num * neg; n } n inline int qpow(int base, int exponent) { n   int res = 1; n   while (exponent) { n     if (exponent & 1) { n       res = (long long int)res * base % MOD; n     } n     base = (long long int)base * base % MOD, exponent >>= 1; n   } n   return res; n } n inline void setup(int cnt) { n   fact[0] = fact[1] = finv[0] = 1; n   for (register int i = 0; i <= cnt; i++) { n     p[i][0] = 1; n   } n   for (register int i = 1; i <= cnt; i++) { n     for (register int j = 1; j <= i; j++) { n       p[i][j] = ((long long int)(i - j) * p[i - 1][j - 1] % MOD + n                  (long long int)(j + 1) * p[i - 1][j] % MOD) % n                 MOD; n     } n   } n   for (register int i = 2; i <= cnt; i++) { n     fact[i] = (long long int)fact[i - 1] * i % MOD; n   } n   finv[cnt] = qpow(fact[cnt], MOD - 2); n   for (register int i = cnt - 1; i; i--) { n     finv[i] = (long long int)finv[i + 1] * (i + 1) % MOD; n   } n } n inline int binom(int m, int n) { n   return (long long int)fact[m] * finv[n] % MOD * finv[m - n] % MOD; n } n int main() { n   setup(n = read()); n   for (register int k = 0; k < n; k++) { n     res = 0; n     for (register int i = max(k, 1); i <= n; i++) { n       res = (res + n              (long long int)binom(n, i) * p[i][k] % MOD * fact[n - i] % MOD) % n             MOD; n     } n     printf( %d  , res); n   } n   puts(  ); n } n


#include <bits/stdc++.h> n using namespace std; n int pgcd(int a, int b) { n   if (b % a == 0) return a; n   pgcd(b % a, a); n } n int main() { n   int o = 0, gcd, az[1001], t[5000], j, k = 0, m, x, y, i, a, b, n; n   cin >> a >> b >> n; n   gcd = pgcd(a, b); n   for (i = 1; i * i <= gcd; i++) { n     if (gcd % i == 0) { n       t[k] = i; n       k++; n       t[k] = gcd / i; n       k++; n     } n   } n   for (i = 0; i < n; i++) { n     m = -1; n     cin >> x >> y; n     for (j = 0; j < k; j++) { n       if (t[j] <= y && t[j] >= x) m = max(m, t[j]); n     } n     cout << m << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int x, y, x2, y2; n int v; n vector<int> mass1, mass2; n int check1(int ind) { n   if (ind == -1 || ind == (int)mass1.size()) { n     return 1e9; n   } n   return abs(mass1[ind] - x) + abs(x2 - mass1[ind]) + (y2 - y + v - 1) / v; n } n int check2(int ind) { n   if (ind == -1 || ind == (int)mass2.size()) { n     return 1e9; n   } n   return abs(mass2[ind] - x) + abs(x2 - mass2[ind]) + (y2 - y); n } n signed main() { n   int n, m, c1, c2; n   cin >> n >> m >> c1 >> c2 >> v; n   mass1.resize(c2); n   mass2.resize(c1); n   for (int i = 0; i < c1; i++) { n     cin >> mass2[i]; n   } n   for (int i = 0; i < c2; i++) { n     cin >> mass1[i]; n   } n   int q; n   cin >> q; n   while (q--) { n     cin >> x >> y >> x2 >> y2; n     swap(x, y); n     swap(x2, y2); n     if (x > x2) { n       swap(x, x2); n     } n     if (y > y2) { n       swap(y, y2); n     } n     int ans = 1e9; n     if (y == y2) { n       ans = x2 - x; n     } n     int l1 = lower_bound(mass1.begin(), mass1.end(), x) - mass1.begin(); n     ans = min(ans, check1(l1)); n     ans = min(ans, check1(l1 - 1)); n     l1 = lower_bound(mass1.begin(), mass1.end(), x2) - mass1.begin(); n     ans = min(ans, check1(l1)); n     ans = min(ans, check1(l1 - 1)); n     l1 = lower_bound(mass2.begin(), mass2.end(), x) - mass2.begin(); n     ans = min(ans, check2(l1)); n     ans = min(ans, check2(l1 - 1)); n     l1 = lower_bound(mass2.begin(), mass2.end(), x2) - mass2.begin(); n     ans = min(ans, check2(l1)); n     ans = min(ans, check2(l1 - 1)); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n const int N = 2000 + 10; n int n; n int pa, pb, p, q; n int cp[N][N]; n int strong[N]; n int f[N]; n void add(int &t, int s) { n   t += s; n   if (t >= MOD) t -= MOD; n } n void mul(int &t, int s) { t = (long long)t * s % MOD; } n int pow(int s, int n) { n   long long ret = 1; n   while (n) n     if (n % 2) n       ret = ret * s % MOD, n--; n     else n       s = (long long)s * s % MOD, n >>= 1; n   return ret; n } n int inv(int s) { return pow(s, MOD - 2); } n int main() { n   cin >> n >> pa >> pb; n   p = (long long)pa * inv(pb) % MOD; n   q = (MOD - p + 1) % MOD; n   f[0] = 0; n   f[1] = 0; n   cp[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     cp[i][0] = 1; n     for (int j = 1; j <= i; j++) { n       cp[i][j] = ((long long)cp[i - 1][j - 1] * pow(q, i - j) + n                   (long long)cp[i - 1][j] * pow(p, j)) % n                  MOD; n     } n   } n   strong[1] = 1; n   for (int i = 2; i <= n; i++) { n     int res = 0; n     for (int j = 1; j < i; j++) { n       int tmp = strong[j]; n       mul(tmp, cp[i][j]); n       add(res, tmp); n     } n     strong[i] = (1 - res + MOD) % MOD; n   } n   for (int s = 2; s <= n; s++) { n     int res = 0; n     for (int i = 1; i < s; i++) { n       int cur = 1; n       mul(cur, strong[i]); n       mul(cur, cp[s][i]); n       int games = 0; n       add(games, i * (s - i)); n       add(games, i * (i - 1) / 2); n       add(games, f[i]); n       add(games, f[s - i]); n       mul(cur, games); n       add(res, cur); n     } n     int coeff = 1; n     mul(coeff, strong[s]); n     mul(coeff, cp[s][s]); n     int tmp = coeff; n     mul(tmp, s * (s - 1) / 2); n     add(res, tmp); n     coeff = (1 + MOD - coeff) % MOD; n     mul(res, inv(coeff)); n     f[s] = res; n   } n   cout << f[n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n vector<T>& operator--(vector<T>& v) { n   for (auto& i : v) --i; n   return v; n } n template <typename T> n vector<T>& operator++(vector<T>& v) { n   for (auto& i : v) ++i; n   return v; n } n template <typename T> n istream& operator>>(istream& is, vector<T>& v) { n   for (auto& i : v) is >> i; n   return is; n } n template <typename T> n ostream& operator<<(ostream& os, vector<T> v) { n   for (auto& i : v) os << i <<    ; n   return os; n } n template <typename T, typename U> n pair<T, U>& operator--(pair<T, U>& p) { n   --p.first; n   --p.second; n   return p; n } n template <typename T, typename U> n pair<T, U>& operator++(pair<T, U>& p) { n   ++p.first; n   ++p.second; n   return p; n } n template <typename T, typename U> n istream& operator>>(istream& is, pair<T, U>& p) { n   is >> p.first >> p.second; n   return is; n } n template <typename T, typename U> n ostream& operator<<(ostream& os, pair<T, U> p) { n   os << p.first <<     << p.second; n   return os; n } n template <typename T, typename U> n pair<T, U> operator-(pair<T, U> a, pair<T, U> b) { n   return mp(a.first - b.first, a.second - b.second); n } n template <typename T, typename U> n pair<T, U> operator+(pair<T, U> a, pair<T, U> b) { n   return mp(a.first + b.first, a.second + b.second); n } n template <typename T, typename U> n void umin(T& a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n void umax(T& a, U b) { n   if (a < b) a = b; n } n bool prime(long long n) { n   for (long long i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) { n       return false; n     } n   } n   return true; n } n long long fact(long long x) { n   long long ans = 1; n   for (long long i = 2; i <= x; i++) ans *= i; n   return ans; n } n void solve() { n   long long n; n   cin >> n; n   vector<long long> a(n); n   cin >> a; n   if (n == 1) { n     cout << 0 << endl; n     return; n   } n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     long long cnt = 0; n     if ((i == 0 && a[i + 1] == 2) || (i == n - 1 && a[i - 1] == 999) || n         (i > 0 && i + 1 < n && a[i + 1] - a[i - 1] == 2)) { n       while (i < n) { n         if (i == 0) { n           if (a[i + 1] == 2) { n             cnt++; n             i++; n           } else { n             break; n           } n         } else if (i == n - 1) { n           if (a[i - 1] == 999) { n             cnt++; n             i++; n           } else { n             break; n           } n         } else { n           if (a[i + 1] - a[i - 1] == 2) { n             cnt++; n             i++; n           } else { n             break; n           } n         } n       } n       i--; n       ans = max(ans, cnt); n     } n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long Tc, a, b, n, m; n int main() { n   long long ans = 0; n   cin >> n >> m; n   if (m % n != 0) { n     cout <<  -1 n ; n     return 0; n   } n   m /= n; n   while (m % 2 == 0) { n     m /= 2; n     ans++; n   } n   while (m % 3 == 0) { n     m /= 3; n     ans++; n   } n   (m > 1) ? cout <<  -1  : cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = (int)2e5 + 5; n const int maxM = (int)2e5 + 5; n inline int readInt() { n   char c; n   int tmp = 0, x = 1; n   c = getchar(); n   while (c >  9  || c <  0 ) { n     if (c ==  - ) x = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     tmp = tmp * 10 + c -  0 ; n     c = getchar(); n   } n   return tmp * x; n } n vector<int> g[maxN]; n void addEdge(int u, int v) { n   g[u].push_back(v); n   g[v].push_back(u); n } n int Father[maxN], Ranking[maxN]; n void init(int n) { n   for (int i = 1; i <= n; i++) Father[i] = i; n } n int getFather(int x) { n   return x == Father[x] ? x : Father[x] = getFather(Father[x]); n } n void mergeUnion(int u, int v) { n   int Fu = getFather(u), Fv = getFather(v); n   if (Fu == Fv) return; n   if (Ranking[Fu] > Ranking[Fv]) swap(Fu, Fv); n   Father[Fu] = Fv; n   Ranking[Fv] += Ranking[Fu]; n } n int n, m, s[maxN], ans[maxM]; n pair<int, int> rec[maxM]; n int dep[maxN], Sum[maxN]; n void dfs(int v, int fa) { n   dep[v] = (fa == -1 ? 1 : dep[fa] + 1); n   Sum[v] = s[v]; n   for (int i = 0; i < (int)g[v].size(); i++) { n     if (g[v][i] != fa) dfs(g[v][i], v), Sum[v] += Sum[g[v][i]]; n   } n } n int main() { n   n = readInt(); n   int tot = 0; n   for (int i = 1; i <= n; i++) s[i] = readInt(), tot += s[i]; n   if (tot) { n     printf( Impossible n ); n     exit(0); n   } else n     printf( Possible n ); n   m = readInt(); n   init(n); n   int u, v; n   for (int i = 1; i <= m; i++) { n     u = readInt(), v = readInt(); n     if (getFather(u) != getFather(v)) { n       addEdge(u, v); n       mergeUnion(u, v); n       rec[i] = make_pair(u, v); n     } n   } n   dfs(1, -1); n   for (int i = 1; i <= m; i++) { n     if (!rec[i].first && !rec[i].second) continue; n     if (dep[rec[i].first] < dep[rec[i].second]) n       ans[i] = Sum[rec[i].second]; n     else n       ans[i] = -Sum[rec[i].first]; n   } n   for (int i = 1; i <= m; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 200005, m1 = 998244353, m2 = 1e9 + 9; n char s[N], c[N]; n long long n, z[N], p1[N] = {1}, p2[N] = {1}, a[N]; n pair<long long, long long> tr[N << 2]; n struct Hash { n   long long h1[N], h2[N]; n   char s[N]; n   void init() { n     h1[0] = h2[0] = s[0] -  a ; n     for (long long i = 1; i < n; i++) { n       h1[i] = (h1[i - 1] * 31 + s[i] -  a ) % m1; n       h2[i] = (h2[i - 1] * 255 + s[i] -  a ) % m2; n       p1[i] = p1[i - 1] * 31 % m1; n       p2[i] = p2[i - 1] * 255 % m2; n     } n   } n   pair<long long, long long> query(long long l, long long r) { n     return make_pair( n         (h1[r] - (l == 0 ? 0 : h1[l - 1] * p1[r - l + 1] % m1) + m1) % m1, n         (h2[r] - (l == 0 ? 0 : h2[l - 1] * p2[r - l + 1] % m2) + m2) % m2); n   } n } h1, h2; n struct node { n   long long x, y, l, p, k; n   node() {} n   node(long long _x, long long _y, long long _l, long long _p, long long _k) n       : x(_x), y(_y), l(_l), p(_p), k(_k) {} n   bool operator<(const node zh) const { return l + l + k < zh.l + zh.l + zh.k; } n } ans; n long long check(long long i, long long m) { n   return h1.query(i - m, i) == h2.query(n - (i + m) - 1, n - i - 1); n } n void build(long long k, long long l, long long r) { n   if (l == r) { n     tr[k] = make_pair(a[l - 1], l - 1); n     return; n   } n   long long mid = l + r >> 1; n   build(k << 1, l, mid); n   build(k << 1 | 1, mid + 1, r); n   tr[k] = max(tr[k << 1], tr[k << 1 | 1]); n } n pair<long long, long long> query(long long k, long long l, long long r, n                                  long long a, long long b) { n   if (l == a && r == b) return tr[k]; n   long long mid = l + r >> 1; n   if (b <= mid) n     return query(k << 1, l, mid, a, b); n   else if (a > mid) n     return query(k << 1 | 1, mid + 1, r, a, b); n   else n     return max(query(k << 1, l, mid, a, mid), n                query(k << 1 | 1, mid + 1, r, mid + 1, b)); n } n pair<long long, long long> xzh(long long x, long long y) { n   long long l = 0, r = (y - x) / 2 + 1; n   while (r - l > 1) { n     long long m = l + r >> 1; n     pair<long long, long long> k = query(1, 1, n, (x + m) + 1, (y - m) + 1); n     if (k.first >= m) n       l = m; n     else n       r = m; n   } n   return query(1, 1, n, (x + l) + 1, (y - l) + 1); n } n signed main() { n   scanf( %s , s); n   n = strlen(s); n   for (long long i = 0; i < n; i++) c[i] = s[n - i - 1]; n   c[n] =  $ ; n   for (long long i = n + 1; i <= 2 * n; i++) c[i] = s[i - n - 1]; n   long long l = 0, r = 0; n   for (long long i = 1; i <= 2 * n; i++) { n     if (i > r) { n       l = i, r = i; n       while (r <= 2 * n && c[r - l] == c[r]) r++; n       z[i] = r - l; n       r--; n     } else { n       long long k = i - l; n       if (z[k] < r - i + 1) n         z[i] = z[k]; n       else { n         l = i; n         while (r <= 2 * n && c[r - l] == c[r]) r++; n         z[i] = r - l; n         r--; n       } n     } n   } n   if (z[n + 1] == n) { n     printf( 1 n1 %I64d n , n - !(n & 1)); n     return 0; n   } n   for (long long i = 0; i < n; i++) { n     h1.s[i] = s[i]; n     h2.s[i] = s[n - i - 1]; n   } n   h1.init(), h2.init(); n   for (long long i = 0; i < n; i++) { n     long long l = 0, r = min(i, n - i - 1) + 1; n     while (r - l > 1) { n       long long m = l + r >> 1; n       if (check(i, m)) n         l = m; n       else n         r = m; n     } n     a[i] = l; n   } n   build(1, 1, n); n   for (long long i = 0; i < n; i++) { n     long long x = i, l = z[i + n + 1], y = n - l; n     if (l == 0 || x + l > y - 1) continue; n     pair<long long, long long> zh = xzh(x + l, y - 1); n     long long k = zh.first; n     k = min(k, y - x - l); n     k = min(k, min(zh.second - (x + l), y - 1 - zh.second)); n     long long p = zh.second - k; n     k = 2 * k + 1; n     node q = node(x, y, l, p, k); n     if (ans < q) ans = q; n   } n   long long zh = 0; n   for (long long i = 0; i < n; i++) n     if (a[i] > a[zh]) zh = i; n   if (2 * a[zh] + 1 > ans.l + ans.l + ans.k) n     printf( 1 n%I64d %I64d n , zh - a[zh] + 1, 2 * a[zh] + 1); n   else n     printf( 3 n%I64d %I64d n%I64d %I64d n%I64d %I64d n , ans.x + 1, ans.l, n            ans.p + 1, ans.k, ans.y + 1, ans.l); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline int size(const T& c) { n   return c.size(); n } n using namespace std; n int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } n int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; } n const int maxn = 1000 + 10; n int a[maxn][maxn]; n int u, x, y; n int h[4][maxn][maxn]; n int res; n vector<int> st; n int s[maxn]; n int n, m, q; n int vt[maxn]; n int cal(int bd, int x) { n   st.push_back(0); n   int top = 0; n   memset(vt, 0, sizeof(vt)); n   memset(s, 0, sizeof(s)); n   s[top] = 0; n   int res = 0; n   for (int i = 1; i < st.size(); i++) { n     int temp = i; n     while (top > 0 && s[top] > st[i]) { n       if (bd + i - 2 >= x && x >= bd + vt[top] - 1) n         res = max(res, (i - vt[top]) * s[top]); n       temp = vt[top]; n       top--; n     } n     if (s[top] != st[i]) { n       top++; n       s[top] = st[i]; n       vt[top] = temp; n     } n   } n   return res; n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %d , &a[i][j]); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (a[i][j] == 0) n         h[0][i][j] = 0; n       else n         h[0][i][j] = h[0][i - 1][j] + 1; n     } n   for (int i = n; i >= 1; i--) n     for (int j = 1; j <= m; j++) { n       if (a[i][j] == 0) n         h[1][i][j] = 0; n       else n         h[1][i][j] = h[1][i + 1][j] + 1; n     } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (a[i][j] == 0) n         h[2][i][j] = 0; n       else n         h[2][i][j] = h[2][i][j - 1] + 1; n     } n   for (int i = 1; i <= n; i++) n     for (int j = m; j >= 1; j--) { n       if (a[i][j] == 0) n         h[3][i][j] = 0; n       else n         h[3][i][j] = h[3][i][j + 1] + 1; n     } n   for (int cq = 1; cq <= q; cq++) { n     scanf( %d%d%d , &u, &y, &x); n     if (u == 1) { n       a[y][x] = a[y][x] ^ 1; n       for (int i = 1; i <= n; i++) n         if (a[i][x] == 0) n           h[0][i][x] = 0; n         else n           h[0][i][x] = h[0][i - 1][x] + 1; n       for (int i = n; i >= 1; i--) n         if (a[i][x] == 0) n           h[1][i][x] = 0; n         else n           h[1][i][x] = h[1][i + 1][x] + 1; n       for (int j = 1; j <= m; j++) n         if (a[y][j] == 0) n           h[2][y][j] = 0; n         else n           h[2][y][j] = h[2][y][j - 1] + 1; n       for (int j = m; j >= 1; j--) n         if (a[y][j] == 0) n           h[3][y][j] = 0; n         else n           h[3][y][j] = h[3][y][j + 1] + 1; n     } n     if (u == 2) { n       res = 0; n       st.clear(); n       st.push_back(0); n       for (int i = y - h[0][y][x] + 1; i <= y + h[1][y][x] - 1; i++) n         st.push_back(h[2][i][x]); n       res = max(res, cal(y - h[0][y][x] + 1, y)); n       st.clear(); n       st.push_back(0); n       for (int i = y - h[0][y][x] + 1; i <= y + h[1][y][x] - 1; i++) n         st.push_back(h[3][i][x]); n       res = max(res, cal(y - h[0][y][x] + 1, y)); n       st.clear(); n       st.push_back(0); n       for (int j = x - h[2][y][x] + 1; j <= x + h[3][y][x] - 1; j++) n         st.push_back(h[0][y][j]); n       res = max(res, cal(x - h[2][y][x] + 1, x)); n       st.clear(); n       st.push_back(0); n       for (int j = x - h[2][y][x] + 1; j <= x + h[3][y][x] - 1; j++) n         st.push_back(h[1][y][j]); n       res = max(res, cal(x - h[2][y][x] + 1, x)); n       cout << res << endl; n     } n   } n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 100000; n int a[MaxN]; n int color[MaxN]; n bool f = true; n int n; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   sort(a + 1, a + n + 1); n   reverse(a + 1, a + n + 1); n   while (n % k) { n     a[n + 1] = a[n]; n     n++; n   } n   int ans = 0; n   for (int i = 0; i < n / k; ++i) { n     ans += (2 * (a[k * i + 1] - 1)); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int maxn = 200050; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int isprime(int x) { n   for (int i = 2; i <= sqrt(x); i++) { n     if (x % i == 0) return 0; n   } n   return 1; n } n int judge(char ch) { n   if (ch ==  a  || ch ==  u ) n     return 1; n   else n     return 0; n } n vector<int> e[maxn]; n int ans[maxn]; n vector<int> v[maxn]; n void dfs(int x) { n   for (int i = 0; i < e[x].size(); i++) { n     dfs(e[x][i]); n     for (int j = 0; j < v[e[x][i]].size(); j++) { n       v[x].push_back(v[e[x][i]][j]); n     } n   } n } n int main() { n   int n; n   string a, b; n   cin >> n >> a >> b; n   int maxsub[3] = {0}; n   int adds = 0; n   int flg = 0; n   for (int i = 0; i < n / 2; i++) { n     char s[4]; n     s[0] = a[i]; n     s[1] = a[n - i - 1]; n     s[2] = b[i]; n     s[3] = b[n - i - 1]; n     if (a[i] == b[i] && s[1] == s[3]) { n       continue; n     } else if (a[i] == b[i] || s[1] == s[3]) { n       adds++; n       continue; n     } else if ((s[0] == s[1] && s[2] == s[3])) { n       continue; n     } else if (s[0] == s[3] && s[1] == s[2]) { n       continue; n     } else if (s[2] == s[3]) { n       adds++; n     } else if (s[0] == s[3] || s[1] == s[2]) { n       adds++; n     } else { n       adds += 2; n     } n   } n   if (n % 2 == 1) { n     if (a[n / 2] != b[n / 2]) adds++; n   } n   int sub = 0; n   if (maxsub[2] >= 2) n     sub = 4; n   else if (maxsub[2] == 1) n     sub = 2 + min(maxsub[1], 1); n   else if (maxsub[2] == 0) n     sub = min(maxsub[1], 2); n   cout << adds - sub; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long a, b; n   cin >> a >> b; n   if (a == b) { n     cout <<  infinity  <<   n ; n     return 0; n   } n   if (a < b) { n     cout << 0 <<   n ; n     return 0; n   } n   long long ans = 0; n   long long n = a - b; n   for (long long i = 1; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (i == n / i) { n         if (i > b) ans++; n       } else { n         if (i > b) ans++; n         if (n / i > b) ans++; n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long i, j, t, a, b, m, n, c, d, y, x, k, s; n   t = 1; n   while (t--) { n     string s1, s2; n     cin >> s1 >> s2; n     if (s1 == s2) n       cout << s1; n     else n       cout << 1; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   scanf( %d%d%d , &a, &b, &c); n   int aweek = a / 3; n   int bweek = b / 2; n   int cweek = c / 2; n   int ans = min(aweek, min(bweek, cweek)); n   ans -= 2; n   ans = max(ans, 0); n   a -= ans * 3; n   b -= ans * 2; n   c -= ans * 2; n   ans *= 7; n   int tmp = 0; n   for (int i = 1; i <= 7; i++) { n     int tmpa = a, tmpb = b, tmpc = c; n     int total = 0; n     int cnt = i; n     while (true) { n       if (cnt == 1 || cnt == 4 || cnt == 7) { n         if (tmpa > 0) n           tmpa--; n         else n           break; n       } else if (cnt == 2 || cnt == 6) { n         if (tmpb > 0) n           tmpb--; n         else n           break; n       } else { n         if (tmpc > 0) n           tmpc--; n         else n           break; n       } n       total++; n       cnt++; n       if (cnt == 8) cnt = 1; n     } n     tmp = max(tmp, total); n   } n   printf( %d , ans + tmp); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 10; n int n, m, q, times[maxn]; n long long a[maxn]; n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= n; ++i) { n     int x; n     scanf( %d , &x); n     a[i] = (long long)(times[x]++) * m + x; n   } n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n; ++i) a[i] -= i; n   while (q--) { n     long long k; n     scanf( %lld , &k); n     k += lower_bound(a + 1, a + 1 + n, k - n) - a - 1 - n; n     printf( %lld n , (k - 1) % m + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename _T> n inline void _DBG(const char *s, _T x) { n   cerr << s <<   =   << x <<   n ; n } n template <typename _T, typename... args> n void _DBG(const char *s, _T x, args... a) { n   while (*s !=  , ) cerr << *s++; n   cerr <<   =   << x <<  , ; n   _DBG(s + 1, a...); n } n template <typename TP> n struct pSegmentTree { n   long long MAX; n   TP init; n   struct node { n     TP val; n     node *left = nullptr, *right = nullptr; n     long long l, r; n     node(long long L, long long R, TP v) : val(v), l(L), r(R) {} n   }; n   vector<node *> roots; n   pSegmentTree(long long n, TP zero) : init(zero) { n     MAX = 1LL << (64 - __builtin_clzll(n)); n     roots.push_back(new node(0, MAX - 1, init)); n   } n   void check(node *root) { n     long long m = (root->l + root->r) / 2; n     if (root->left == nullptr) root->left = new node(root->l, m, init); n     if (root->right == nullptr) root->right = new node(m + 1, root->r, init); n   } n   node *add(node *root, long long id, TP v) { n     node *res = new node(*root); n     res->val += v; n     if (res->l == res->r) return res; n     long long m = (res->l + res->r) / 2; n     check(res); n     if (id <= m) n       res->left = add(res->left, id, v); n     else n       res->right = add(res->right, id, v); n     return res; n   } n   TP ask(node *root, long long a, long long b) { n     if (root->r < a || b < root->l) return init; n     if (a <= root->l && root->r <= b) return root->val; n     check(root); n     return ask(root->left, a, b) + ask(root->right, a, b); n   } n   void update(long long id, TP v, int state = -1) { n     if (state == -1) state += (int)(roots).size(); n     assert(state < (int)(roots).size()); n     roots.push_back(add(roots[state], id, v)); n   } n   TP query(long long a, long long b, int state = -1) { n     if (state == -1) state += (int)(roots).size(); n     assert(state < (int)(roots).size()); n     return ask(roots[state], a, b); n   } n }; n const int N = 1e5 + 7; n int n, k, t[N], p[N]; n vector<int> pos[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> t[i]; n     pos[t[i]].push_back(i); n     if ((int)(pos[t[i]]).size() <= k) n       p[i] = 0; n     else n       p[i] = pos[t[i]][(int)(pos[t[i]]).size() - k - 1]; n   } n   pSegmentTree<int> tree(n + 7, 0); n   for (int i = 1; i <= n; i++) tree.update(p[i], 1); n   int q, last = 0; n   cin >> q; n   while (q--) { n     int l, r; n     cin >> l >> r; n     l = (l + last) % n + 1; n     r = (r + last) % n + 1; n     if (l > r) swap(l, r); n     last = tree.query(0, l - 1, r) - tree.query(0, l - 1, l - 1); n     cout << last <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100002][2]; n long long dp[100002][2][2]; n int n; n long long solve(int idx, int arga3, int dir) { n   if (idx == 1 && dir == 0) return 0; n   if (idx == n && dir == 1) return 0; n   long long &ret = dp[idx][arga3][dir]; n   if (ret != -1) return ret; n   ret = 0; n   int fd; n   if (dir == 0) n     fd = -1; n   else n     fd = 1; n   if (arga3) { n     int x = arr[idx][dir]; n     if (x % 2) x--; n     if (x) ret = max(ret, solve(idx + fd, arga3, dir) + x); n   } else { n     int x = arr[idx][dir]; n     if (x == 1) n       ret = max(ret, solve(idx + fd, false, dir) + 1); n     else if (x % 2) { n       ret = max(ret, solve(idx + fd, false, dir) + x); n       x--; n       ret = max(ret, solve(idx + fd, true, dir) + x); n     } else if (x) { n       ret = max(ret, solve(idx + fd, true, dir) + x); n       x--; n       ret = max(ret, solve(idx + fd, false, dir) + x); n     } n   } n   return ret; n } n int main() { n   cin >> n; n   for (long long i = 1; i <= n - 1; i++) { n     int x; n     cin >> x; n     arr[i][1] = x; n     arr[i + 1][0] = x; n   } n   memset(dp, -1, sizeof dp); n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     if (i == 1) { n       int x = arr[i][1]; n       if (x == 1) { n         ans = max(ans, solve(2, false, 1) + 1); n       } else { n         if (x % 2) { n           ans = max(ans, solve(2, false, 1) + x); n           ans = max(ans, solve(2, true, 1) + x - 1); n         } else { n           ans = max(ans, solve(2, true, 1) + x); n           ans = max(ans, solve(2, false, 1) + x - 1); n         } n       } n     } else if (i == n) { n       int x = arr[n][0]; n       if (x == 1) { n         ans = max(ans, solve(n - 1, false, 0) + 1); n       } else { n         if (x % 2) { n           ans = max(ans, solve(n - 1, false, 0) + x); n           ans = max(ans, solve(n - 1, true, 0) + x - 1); n         } else { n           ans = max(ans, solve(n - 1, true, 0) + x); n           ans = max(ans, solve(n - 1, false, 0) + x - 1); n         } n       } n     } else { n       if (arr[i][0] % 2 && arr[i][1] % 2) { n         long long y7 = 0, ym = 0, s7 = 0, sm = 0; n         if (arr[i][1] - 1) y7 = solve(i + 1, true, 1) + arr[i][1] - 1; n         ym = solve(i + 1, false, 1) + arr[i][1]; n         if (arr[i][0] - 1) s7 = solve(i - 1, true, 0) + arr[i][0] - 1; n         sm = solve(i - 1, false, 0) + arr[i][0]; n         ans = max(ans, y7 + s7); n         ans = max(ans, y7 + sm); n         ans = max(ans, ym + s7); n       } else if (arr[i][1] % 2) { n         long long y7 = 0, ym = 0, s7 = 0, sm = 0; n         if (arr[i][1] - 1) y7 = solve(i + 1, true, 1) + arr[i][1] - 1; n         ym = solve(i + 1, false, 1) + arr[i][1]; n         s7 = solve(i - 1, true, 0) + arr[i][0]; n         sm = solve(i - 1, false, 0) + arr[i][0] - 1; n         ans = max(ans, y7 + s7); n         ans = max(ans, y7 + sm); n         ans = max(ans, ym + s7); n       } else if (arr[i][0] % 2) { n         long long y7 = 0, ym = 0, s7 = 0, sm = 0; n         y7 = solve(i + 1, true, 1) + arr[i][1]; n         ym = solve(i + 1, false, 1) + arr[i][1] - 1; n         if (arr[i][0] - 1) s7 = solve(i - 1, true, 0) + arr[i][0] - 1; n         sm = solve(i - 1, false, 0) + arr[i][0]; n         ans = max(ans, y7 + s7); n         ans = max(ans, y7 + sm); n         ans = max(ans, ym + s7); n       } else { n         long long y7 = 0, ym = 0, s7 = 0, sm = 0; n         y7 = solve(i + 1, true, 1) + arr[i][1]; n         ym = solve(i + 1, false, 1) + arr[i][1] - 1; n         s7 = solve(i - 1, true, 0) + arr[i][0]; n         sm = solve(i - 1, false, 0) + arr[i][0] - 1; n         ans = max(ans, y7 + s7); n         ans = max(ans, y7 + sm); n         ans = max(ans, ym + s7); n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     string s; n     cin >> s; n     int zero = 0, one = 0; n     for (char i : s) n       if (i ==  0 ) n         zero++; n       else n         one++; n     if (min(zero, one) % 2 == 1) n       cout <<  DA n ; n     else n       cout <<  NET n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<pair<int, int>, pair<int, int> > > way[222222]; n queue<int> turn; n bool was[222222]; n int i, n, m, ans[222222], cost[222222]; n int main() { n   cin >> n >> m; n   for (i = 1; i <= m; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     cost[a] += c; n     cost[b] += c; n     way[a].push_back(make_pair(make_pair(i, c), make_pair(b, 0))); n     way[b].push_back(make_pair(make_pair(i, c), make_pair(a, 1))); n   } n   for (i = 1; i <= n; i++) cost[i] /= 2; n   turn.push(1); n   memset(was, 1, sizeof(was)); n   while (!turn.empty()) { n     int t = turn.front(); n     was[t] = 0; n     turn.pop(); n     for (i = 0; i < way[t].size(); i++) n       if (was[way[t][i].second.first]) { n         int u = way[t][i].second.first; n         ans[way[t][i].first.first] = way[t][i].second.second; n         cost[u] -= way[t][i].first.second; n         if (cost[u] == 0 && u != n) turn.push(u); n       } n   } n   for (i = 1; i <= m; i++) cout << ans[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a[4]; n vector<long long> sum[4]; n int hasha[200050], hashb[200050]; n int c[200050]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m, k, t; n   cin >> n >> m >> k; n   memset(hasha, 0, sizeof(hasha)); n   memset(hashb, 0, sizeof(hashb)); n   for (int i = 1; i <= n; i++) cin >> c[i]; n   cin >> t; n   for (int i = 0; i < t; i++) { n     int x; n     cin >> x; n     hasha[x] = 1; n   } n   cin >> t; n   for (int i = 0; i < t; i++) { n     int x; n     cin >> x; n     hashb[x] = 1; n   } n   for (int i = 1; i <= n; i++) { n     if (hasha[i] && hashb[i]) n       a[3].push_back(c[i]); n     else if (!hasha[i] && hashb[i]) n       a[2].push_back(c[i]); n     else if (hasha[i] && !hashb[i]) n       a[1].push_back(c[i]); n     else n       a[0].push_back(c[i]); n   } n   for (int i = 0; i < 4; i++) sort(a[i].begin(), a[i].end()); n   for (int i = 0; i < 4; i++) sum[i].push_back(0); n   for (int i = 0; i < 4; i++) { n     for (int j = 0; j < a[i].size(); j++) n       sum[i].push_back(sum[i].back() + a[i][j]); n   } n   long long ans = 1e18 + 1; n   for (int i = 0; i <= a[3].size(); i++) { n     long long cost = sum[3][i]; n     int need = max(0, k - i); n     if (need > a[1].size() || need > a[2].size() || i + need * 2 > m) continue; n     cost += sum[1][need], cost += sum[2][need]; n     int left = m - i - 2 * need; n     if (a[1].size() - need + a[2].size() - need + a[0].size() < left) continue; n     if (left > 0) { n       int l = 0, r = 1e9; n       while (l < r) { n         int mid = (l + r) / 2; n         int cnt = 0; n         int h = upper_bound(a[0].begin(), a[0].end(), mid) - a[0].begin(); n         cnt += max(0, h); n         h = upper_bound(a[1].begin(), a[1].end(), mid) - a[1].begin(); n         cnt += max(0, h - need); n         h = upper_bound(a[2].begin(), a[2].end(), mid) - a[2].begin(); n         cnt += max(0, h - need); n         if (cnt >= left) n           r = mid; n         else n           l = mid + 1; n       } n       int mid = l, cnt = 0; n       int h = upper_bound(a[0].begin(), a[0].end(), mid) - a[0].begin(); n       cnt += max(0, h); n       cost += sum[0][h]; n       h = upper_bound(a[1].begin(), a[1].end(), mid) - a[1].begin(); n       cnt += max(0, h - need); n       if (h > need) cost += sum[1][h] - sum[1][need]; n       h = upper_bound(a[2].begin(), a[2].end(), mid) - a[2].begin(); n       cnt += max(0, h - need); n       if (h > need) cost += sum[2][h] - sum[2][need]; n       if (cnt > left) cost -= mid * (cnt - left); n     } n     ans = min(ans, cost); n   } n   if (ans > 1e17) ans = -1; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std::chrono; n const long long int N = 10e5 + 10; n const long long int M = 32767; n long long int a[100050] = {0}; n long long int b[100050] = {0}; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     int n, k; n     int segment = 0; n     map<int, int> m; n     cin >> n >> k; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       b[i] = a[i]; n     } n     sort(b, b + n); n     for (int i = 0; i < n - 1; i++) { n       m[b[i]] = b[i + 1]; n     } n     for (int i = 0; i < n - 1; i++) { n       if ((a[i + 1] != m[a[i]]) || (a[i] == b[n - 1])) { n         segment++; n       } n     } n     if (segment < k) { n       cout <<  Yes  << endl; n     } else { n       cout <<  No  << endl; n     } n     memset(a, 0, n * sizeof(a[0])); n     memset(b, 0, n * sizeof(a[0])); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int c[100]; n int main() { n   int n, k, a; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> k; n     for (int j = 1; j <= k; j++) { n       cin >> a; n       c[a]++; n     } n   } n   for (int i = 1; i <= 100; i++) { n     if (c[i] == n) cout << i <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string a, b; n   cin >> a >> b; n   if (a.size() < b.size()) a = string(b.size() - a.size(),  0 ) + a; n   if (a.size() > b.size()) b = string(a.size() - b.size(),  0 ) + b; n   if (a < b) n     cout <<  <  << endl; n   else if (a > b) n     cout <<  >  << endl; n   else n     cout <<  =  << endl; n   return; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[1005]; n map<long long, int> x; n int ans; n void f(long long a, long long b) { n   long long cur = a + b; n   vector<long long> q; n   q.push_back(a); n   q.push_back(b); n   int len = 2; n   x[a]--; n   x[b]--; n   while (x[cur] > 0) { n     x[cur]--; n     q.push_back(cur); n     a = b; n     b = cur; n     cur = a + b; n     len++; n   } n   ans = max(len, ans); n   for (int i = 0; i < q.size(); i++) x[q[i]]++; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     x[a[i]]++; n   } n   ans = x[0]; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       if (a[i] == a[j] && a[i] == 0) continue; n       f(a[i], a[j]); n       f(a[j], a[i]); n     } n   } n   cout << ans; n } n