#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ,  unroll-loops ,  no-stack-protector ) n using namespace std; n using ll = long long; n using ld = double; n using pii = pair<int, int>; n using uint = unsigned int; n using ull = unsigned long long; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n default_random_engine generator; n void solve(); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << setprecision(16) << fixed; n   cerr << setprecision(16) << fixed; n   int tests = 1; n   for (int test = 1; test <= tests; ++test) { n     solve(); n   } n } n const int INF = 1e9; n struct Point { n   int x, y; n }; n void solve() { n   int n; n   cin >> n; n   vector<Point> p(n); n   for (int i = 0; i < n; ++i) { n     string s; n     cin >> s; n     int a = 0, b = 0; n     for (char c : s) { n       a += c ==  B ; n       b += c ==  N ; n     } n     p[i] = {a, b}; n   } n   int minx = INF, maxx = -INF; n   int miny = INF, maxy = -INF; n   int l = INF, r = -INF; n   for (auto [x, y] : p) { n     minx = min(minx, x); n     maxx = max(maxx, x); n     miny = min(miny, y); n     maxy = max(maxy, y); n     l = min(l, x - y); n     r = max(r, x - y); n   } n   int res = 0; n   res = max(res, (maxx - minx + 1) / 2); n   res = max(res, (maxy - miny + 1) / 2); n   res = max(res, (minx - miny) - l); n   res = max(res, r - (minx - miny)); n   Point best = {minx + res, miny + res}; n   for (int op = 0; op < 2; ++op) { n     int xx = miny + l; n     int yy = miny; n     int cur = 0; n     cur = max(cur, (maxx - xx + 2) / 3); n     cur = max(cur, (maxy - yy + 1) / 2); n     cur = max(cur, (r - l + 1) / 2); n     if (cur < res && cur <= minx - xx) { n       res = cur; n       best = {xx + 2 * res, yy + res}; n       if (op) { n         swap(best.x, best.y); n       } n     } n     for (int i = 0; i < n; ++i) { n       swap(p[i].x, p[i].y); n     } n     swap(minx, miny); n     swap(maxx, maxy); n     r *= -1; n     l *= -1; n     swap(l, r); n   } n   cout << res << endl; n   string ans; n   for (int i = 0; i < best.x; ++i) { n     ans +=  B ; n   } n   for (int i = 0; i < best.y; ++i) { n     ans +=  N ; n   } n   cout << ans << endl; 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   string s; n   cin >> s; n   long long w = 0, o = 0; n   for (long long i = 0; i < s.length() - 1; i++) { n     if (s[i] ==  v  && s[i + 1] ==  v ) n       w++; n     else { n       if (s[i] ==  o ) o++; n     } n   } n   if (s[s.length() - 1] ==  o ) o++; n   long long x = 0, ans = 0; n   for (long long i = 0; i < s.length() - 1; i++) { n     if (s[i] ==  v  && s[i + 1] ==  v ) n       x++; n     else { n       if (s[i] ==  o ) { n         ans += x * (w - x); n       } n     } n   } n   cout << ans; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   int ans = 0; n   while (1) { n     int maxi = 0; n     for (int i = 0; i < n; i++) { n       if (arr[i] >= arr[maxi]) maxi = i; n     } n     if (maxi == 0) break; n     ans++; n     arr[0]++; n     arr[maxi]--; n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m, l = 0, p = 0, i, flag = 0, k, j, r = 0, q = 0, t, curr = 0, d, n                   x; n   cin >> n >> x >> k; n   long long a[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   for (long long i = 0; i < n; i++) { n     long long r1 = a[i] + (x * k); n     if (a[i] % x == 0) { n       r1--; n     } n     r1 -= r1 % x; n     long long r2 = r1 + x - 1; n     r1 = max(a[i], r1); n     auto it = upper_bound(a, a + n, r2); n     auto it1 = lower_bound(a, a + n, r1); n     p += it - it1; n   } n   cout << p; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename T1> n T amax(T &a, T1 b) { n   if (b > a) a = b; n   return a; n } n template <typename T, typename T1> n T amin(T &a, T1 b) { n   if (b < a) a = b; n   return a; n } n bool isPrime(long long x) { n   if (x == 1) return false; n   for (long long i = 2; i * i <= x; i++) { n     if (x % i == 0) { n       return false; n     } n   } n   return true; n } n bool isPowerOfTwo(long long n) { n   if (n == 0) return false; n   return (ceil(log2(n)) == floor(log2(n))); n } n long long gcdByBit(long long a, long long b) { n   if (!a || !b) return a | b; n   unsigned shift = __builtin_ctz(a | b); n   a >>= __builtin_ctzl(a); n   do { n     b >>= __builtin_ctzl(b); n     if (a > b) { n       swap(a, b); n     } n     b -= a; n   } while (b); n   return a << shift; n } n long long powerMod(long long n, long long p) { n   long long res = 1; n   while (p) { n     if (p % 2 == 0) { n       n = n * 1ll * n % 1000000007; n       p /= 2; n     } else { n       res = res * 1ll * n % 1000000007; n       p--; n     } n   } n   return res; n } n long long powerMod2(long long b, long long p, long long m) { n   if (p == 0) return 1; n   return (b * powerMod2(b, p - 1, m) % m) % m; n } n long long fact(long long n) { n   long long res = 1; n   for (long long i = 1; i <= n; i++) { n     res = res * 1ll * i % 1000000007; n   } n   return res; n } n long long nCr(long long n, long long r) { n   return fact(n) * 1ll * powerMod(fact(r), 1000000007 - 2) % 1000000007 * 1ll * n          powerMod(fact(n - r), 1000000007 - 2) % 1000000007; n } n long long arr[300005]; n long long ans[300005]; n long long parent(long long x) { n   if (arr[x] != x) arr[x] = parent(arr[x]); n   return arr[x]; n } n void Chaliye_Shuru_Karte_Hai() { n   for (long long i = 0; i < 300005; i++) arr[i] = i; n   long long n, m; n   cin >> n >> m; n   for (long long i = 0; i < m; i++) { n     long long a, b, c; n     cin >> a >> b >> c; n     for (long long j = parent(a); j <= b; j = parent(j + 1)) { n       if (j != c) ans[j] = c, arr[j] = j + 1; n     } n   } n   for (long long i = 1; i < n + 1; i++) cout << ans[i] <<    ; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long tc = 1; n   while (tc--) { n     Chaliye_Shuru_Karte_Hai(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005; n const int M = 101; n const double EPS = 1e-9; n const int INF = 1e9; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n double getDist(complex<double> a, complex<double> b) { n   return sqrt((a.real() - b.real()) * (a.real() - b.real()) + n               (a.imag() - b.imag()) * (a.imag() - b.imag())); n } n int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; } n long long mod = 1000000007, fib[N], dp[N][N]; n long long power(long long base, int p) { n   if (!p) return 1; n   long long ret = power(base, p / 2); n   ret *= ret; n   ret %= mod; n   if (p & 1) ret *= base, ret %= mod; n   return ret; n } n long long modInverse(int a) { return power(a, mod - 2); } n long long ncr(int n, int r) { n   return (((fib[n] * modInverse(fib[r])) % mod) * modInverse(fib[n - r])) % mod; n } n bool emty[N], taken[N]; n long long solve(int n, int k) { n   if (k == 0) return fib[n]; n   if (k == 1) return (fib[n] * n) % mod; n   if (dp[n][k] != -1) return dp[n][k]; n   long long ret = n       (solve(n, k - 1) * n) % mod + (solve(n + 1, k - 2) * (k - 1)) % mod; n   return dp[n][k] = ret % mod; n } n int main() { n   fib[0] = fib[1] = 1; n   for (int i = 2; i < N; i++) fib[i] = (fib[i - 1] * i) % mod; n   int n, free = 0, k = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int a; n     scanf( %d , &a); n     if (a == -1) n       emty[i] = 1; n     else n       taken[a] = 1; n   } n   for (int i = 1; i <= n; i++) { n     if (emty[i] == 1) { n       if (taken[i] == 1) n         free++; n       else n         k++; n     } n   } n   memset(dp, -1, sizeof(dp)); n   cout << solve(free, k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, tmp, sum = 1; n   int map[200000] = {0}; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> tmp; n     if (map[tmp] == 0) { n       map[tmp]++; n     } else n       sum++; n   } n   cout << sum; n } n


#include <bits/stdc++.h> n using namespace std; n long long N; n long long i, j; n vector<long long> v; n map<long long, long long> mp; n long long arr[200]; n int main() { n   scanf( %lld , &N); n   for (i = 0; i < N; i++) scanf( %lld , &arr[i]); n   sort(arr, arr + N); n   long long ans = 0; n   for (i = 0; i < N; i++) { n     long long get = arr[i]; n     if (mp[arr[i]] == 1) { n       continue; n     } else { n       ans++; n       for (j = 1; j < 200; j++) { n         mp[get * j] = 1; n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxd = 1e6 + 10; n int a[maxd]; n long long sum, ans = 1e18; n vector<long long> b; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) sum += a[i]; n   if (sum == 1) { n     printf( -1 n ); n     return 0; n   } n   for (long long i = 2; i * i <= sum; i++) { n     if (sum % i == 0) { n       b.push_back(i); n       sum /= i; n       i--; n     } n   } n   b.push_back(sum); n   for (auto x : b) { n     long long res = 0; n     sum = 0; n     for (int i = 1; i <= n; i++) { n       sum += a[i]; n       sum %= x; n       res += min(sum, x - sum); n     } n     ans = min(res, ans); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a; n vector<int> q; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     bool w = true; n     for (int j = 0; j < n; j++) { n       scanf( %d , &a); n       if (a == 1 || a == 3) w = false; n     } n     if (w) q.push_back(i + 1); n   } n   printf( %d n , q.size()); n   for (int i = 0; i < q.size(); i++) printf( %d  , q[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool can(bool print, int t, long long S, const vector<pair<int, int> >& bugs, n          const vector<pair<int, pair<int, int> > >& st) { n   priority_queue<pair<int, int>, vector<pair<int, int> >, n                  greater<pair<int, int> > > n       pq; n   int ind = bugs.size() - 1; n   int stind = st.size() - 1; n   vector<int> ans(bugs.size()); n   long long cost = 0; n   while (ind >= 0) { n     while (stind >= 0 and st[stind].first >= bugs[ind].first) { n       pq.push(pair<int, int>(st[stind].second.first, st[stind].second.second)); n       --stind; n     } n     if (pq.empty()) return false; n     long long c = pq.top().first; n     int indans = pq.top().second; n     pq.pop(); n     cost += c; n     if (cost > S) return false; n     for (int i = 0; ind >= 0 and i < t; ++i) { n       ans[bugs[ind].second] = indans; n       --ind; n     } n   } n   if (print) { n     for (int i = 0; i < int(bugs.size()); ++i) cout << (i ?     :   ) << ans[i]; n     cout << endl; n   } n   return true; n } n int main() { n   int n, m, s; n   cin >> n >> m >> s; n   vector<pair<int, int> > bugs(m); n   for (int i = 0; i < m; ++i) { n     cin >> bugs[i].first; n     bugs[i].second = i; n   } n   vector<pair<int, pair<int, int> > > st(n); n   for (int i = 0; i < n; ++i) cin >> st[i].first; n   for (int i = 0; i < n; ++i) { n     cin >> st[i].second.first; n     st[i].second.second = i + 1; n   } n   sort(bugs.begin(), bugs.end()); n   sort(st.begin(), st.end()); n   int low = 1, hi = m + 1; n   while (low < hi) { n     int mid = (low + hi) / 2; n     if (can(false, mid, s, bugs, st)) n       hi = mid; n     else n       low = mid + 1; n   } n   if (low == m + 1) n     cout <<  NO  << endl; n   else { n     cout <<  YES  << endl; n     can(true, low, s, bugs, st); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e6 + 10; n int n, m, q, pos[MAX], bit[MAX], res[MAX], XOR[MAX]; n pair<int, int> cache[MAX], a[MAX], b[MAX]; n struct Node { n   int lef, rig, tt; n   bool operator<(const Node& other) const { return this->lef < other.lef; } n }; n Node query[MAX]; n void up(int i) { n   int val = a[i].first; n   while (i <= n) { n     bit[i] ^= val; n     i += i & (-i); n   } n } n int get(int i) { n   int res = 0; n   while (i) { n     res ^= bit[i]; n     i -= i & (-i); n   } n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &cache[i].first); n     a[i].first = cache[i].first; n     cache[i].second = i; n   } n   sort(cache + 1, cache + 1 + n); n   for (int i = 1; i <= n; ++i) { n     if (i == 1 || cache[i].first > cache[i - 1].first) ++m; n     a[cache[i].second].second = m; n   } n   for (int i = 1; i <= n; ++i) { n     b[i] = pair<int, int>(pos[a[i].second], i); n     pos[a[i].second] = i; n     XOR[i] = XOR[i - 1] ^ a[i].first; n   } n   sort(b + 1, b + 1 + n); n   scanf( %d , &q); n   for (int i = 1; i <= q; ++i) { n     scanf( %d %d , &query[i], &query[i].rig); n     query[i].tt = i; n   } n   sort(query + 1, query + 1 + q); n   int j = 1; n   for (int i = 1; i <= q; ++i) { n     while (j <= n && b[j].first < query[i].lef) up(b[j++].second); n     res[query[i].tt] = XOR[query[i].rig] ^ XOR[query[i].lef - 1]; n     res[query[i].tt] ^= get(query[i].rig) ^ get(query[i].lef - 1); n   } n   for (int i = 1; i <= q; ++i) printf( %d n , res[i]); n   return 0; n } n


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


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


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> ans1, ans2; n     string a, b; n     cin >> a >> b; n     a = a +  0 ; n     b = b +  0 ; n     for (int i = 0; i < n; i++) { n       if (a[i] != a[i + 1]) ans1.push_back(i); n     } n     for (int i = 0; i < n; i++) { n       if (b[i] != b[i + 1]) ans2.push_back(i); n     } n     cout << ans1.size() + ans2.size() <<    ; n     for (int i = 0; i < ans1.size(); i++) cout << ans1[i] + 1 <<    ; n     for (int i = ans2.size() - 1; i >= 0; i--) cout << ans2[i] + 1 <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long p, x, a, b, ni, ans; n void e_gcd(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return; n   } n   e_gcd(b, a % b, x, y); n   int tmp = x; n   x = y; n   y = tmp - (a / b) * y; n   return; n } n int main() { n   cin >> a >> b >> p >> x; n   long long i, j, s; n   e_gcd(a, p, ni, s); n   a = 1; n   for (j = 1; j <= p - 1; j++) { n     a = a * ni % p; n     i = (j - b * a % p + p) % p; n     long long n = i * (p - 1) + j; n     if (n <= x) ans += (x - n) / (p * (p - 1)) + 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int *a = new int[n]; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   int m; n   scanf( %d , &m); n   int bred[10000]; n   for (int i = 0; i < 10000; i++) bred[i] = 0; n   for (int i = 0; i < m; i++) { n     int b; n     scanf( %d , &b); n     for (int j = 0; j < n; j++) { n       if (b % a[j] != 0) continue; n       bred[b / a[j]]++; n     } n   } n   for (int i = 9999; i >= 0; i--) { n     if (bred[i] != 0) { n       printf( %d , bred[i]); n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n inline int rd(int f = 1, int x = 0, char ch =    ) { n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) f = -1; n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return f * x; n } n const int N = 1e5 + 5; n int n, a[N], ans; n int main() { n   for (int T = rd(); T--;) { n     n = rd(), ans = 0; n     for (int i = 1; i <= n; ++i) a[i] = rd(); n     for (int i = 2; i <= n; ++i) ans |= a[i - 1] <= a[i]; n     puts(ans ?  YES  :  NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, q, a[200005], f[200005]; n int main() { n   cin >> n >> k >> q; n   for (int i = 1; i <= n; i++) { n     long long l, r; n     cin >> l >> r; n     a[l]++; n     a[r + 1]--; n   } n   for (int i = 1; i <= 200000; i++) a[i] += a[i - 1]; n   for (int i = 1; i <= 200000; i++) { n     f[i] += f[i - 1]; n     if (a[i] >= k) { n       f[i]++; n     } n   } n   for (int i = 1; i <= q; i++) { n     long long l, r; n     cin >> l >> r; n     cout << f[r] - f[l - 1] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n constexpr int INF = 2e9; n int main() { n   int t; n   t = 1; n   while (t--) { n     int n; n     string s; n     cin >> n; n     cin >> s; n     int res = 0; n     for (int d = 25; d >= 0; d--) { n       string tmp =   ; n       for (int i = 0; i < s.size(); i++) { n         int now = s[i] -  a ; n         if (now != d) { n           tmp += s[i]; n         } else { n           if (tmp.back() -  a  == d - 1) { n             res++; n           } else { n             tmp += s[i]; n           } n         } n       } n       swap(s, tmp); n       tmp =   ; n       for (int i = s.size() - 1; i >= 0; i--) { n         int now = s[i] -  a ; n         if (now != d) { n           tmp += s[i]; n         } else { n           if (tmp.back() -  a  == d - 1) { n             res++; n           } else { n             tmp += s[i]; n           } n         } n       } n       reverse(tmp.begin(), tmp.end()); n       swap(s, tmp); n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int MAXN = 2e5 + 5; n int n, k, d, ansL = 1, ansR = 1, top1, top2; n int a[MAXN], st1[MAXN], st2[MAXN]; n map<int, int> lst; n namespace SegmentTree { n struct Tree { n   int l, r; n   int w, tag; n } tree[MAXN << 2]; n void update(int k) { tree[k].w = min(tree[k << 1].w, tree[k << 1 | 1].w); } n void apply(int k, int val) { n   tree[k].w += val; n   tree[k].tag += val; n } n void down(int k) { n   if (tree[k].tag != 0) { n     apply(k << 1, tree[k].tag); n     apply(k << 1 | 1, tree[k].tag); n     tree[k].tag = 0; n   } n } n void Build(int k, int l, int r) { n   tree[k].l = l; n   tree[k].r = r; n   if (l == r) { n     tree[k].w = l; n     return; n   } n   int mid = (l + r) >> 1; n   Build(k << 1, l, mid); n   Build(k << 1 | 1, mid + 1, r); n   update(k); n } n void Modify(int k, int l, int r, int val) { n   if (tree[k].l >= l && tree[k].r <= r) { n     apply(k, val); n     return; n   } n   down(k); n   int mid = (tree[k].l + tree[k].r) >> 1; n   if (l <= mid) Modify(k << 1, l, r, val); n   if (r > mid) Modify(k << 1 | 1, l, r, val); n   update(k); n } n void Delete(int k, int x) { n   if (tree[k].l == tree[k].r) { n     tree[k].w = INF; n     return; n   } n   down(k); n   int mid = (tree[k].l + tree[k].r) >> 1; n   if (x <= mid) n     Delete(k << 1, x); n   else n     Delete(k << 1 | 1, x); n   update(k); n } n int Query(int k, int l, int r, int x) { n   if (tree[k].w > x) return INF; n   if (tree[k].l == tree[k].r) return tree[k].l; n   if (tree[k].l >= l && tree[k].r <= r) { n     down(k); n     if (tree[k << 1].w <= x) n       return Query(k << 1, l, r, x); n     else n       return Query(k << 1 | 1, l, r, x); n   } else { n     down(k); n     int mid = (tree[k].l + tree[k].r) >> 1; n     int res; n     if (l <= mid) { n       res = Query(k << 1, l, r, x); n       if (res != INF) return res; n     } n     if (r > mid) { n       res = Query(k << 1 | 1, l, r, x); n       if (res != INF) return res; n     } n     return -1; n   } n } n }  // namespace SegmentTree n int main() { n   scanf( %d%d%d , &n, &k, &d); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     a[i] += INF; n   } n   if (d == 0) { n     for (int i = 1; i <= n; i++) { n       int l = i; n       while (a[i] == a[i + 1] && i < n) i += 1; n       if (i - l + 1 > ansR - ansL + 1) { n         ansL = l; n         ansR = i; n       } n     } n     printf( %d %d n , ansL, ansR); n     return 0; n   } n   SegmentTree::Build(1, 1, n); n   int L = 1; n   for (int R = 1; R <= n; R++) { n     int tmp = L; n     if (a[R] % d == a[R - 1] % d) n       L = max(lst[a[R]] + 1, L); n     else n       L = R; n     lst[a[R]] = R; n     while (tmp < L) { n       SegmentTree::Delete(1, tmp); n       tmp += 1; n     } n     while (top1 && st1[top1] >= L && a[st1[top1]] >= a[R]) { n       SegmentTree::Modify(1, max(L, st1[top1 - 1] + 1), st1[top1], n                           a[st1[top1]] / d); n       top1 -= 1; n     } n     SegmentTree::Modify(1, max(L, st1[top1] + 1), R, -a[R] / d); n     st1[++top1] = R; n     while (top2 && st2[top2] >= L && a[st2[top2]] <= a[R]) { n       SegmentTree::Modify(1, max(L, st2[top2 - 1] + 1), st2[top2], n                           -a[st2[top2]] / d); n       top2 -= 1; n     } n     SegmentTree::Modify(1, max(L, st2[top2] + 1), R, a[R] / d); n     st2[++top2] = R; n     int pos = SegmentTree::Query(1, L, R, k + R); n     if (R - pos + 1 > ansR - ansL + 1) { n       ansL = pos; n       ansR = R; n     } n   } n   printf( %d %d n , ansL, ansR); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long add(long long a, long long b, long long mod) { n   return (a % mod + b % mod) % mod; n } n long long sub(long long a, long long b, long long mod) { n   return (a % mod - b % mod + mod) % mod; n } n long long mul(long long a, long long b, long long mod) { n   return (a % mod * b % mod) % mod; n } n long long sumodd(long long num, long long mod) { return mul(num, num, mod); } n long long sumeven(long long num, long long mod) { n   return mul(num, num + 1, mod); n } n long long sumrange(long long st, long long en, long long num) { n   return (num * (st + en) / 2); n } n long long gcd(long long a, long long b) { n   while (b != 0) { n     long long a2 = a; n     a = b; n     b = a2 % b; n   } n   return a; n } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n string makeitbinary(long long n) { n   string s; n   while (n) { n     s = s + (char)((n % 2) +  0 ); n     n /= 2; n   } n   reverse(s.begin(), s.end()); n   return s; n } n bool bit(int num, int i) { return ((num >> i) & 1); } n long long fastpowermod(long long b, long long p, long long mod) { n   long long ans = 1; n   while (p) { n     if (p % 2) { n       ans = mul(ans, b, mod); n     } n     b = mul(b, b, mod); n     p /= 2; n   } n   return ans; n } n long long fastpower(long long b, long long p) { n   long long ans = 1; n   while (p) { n     if (p % 2) { n       ans = ans * b; n     } n     b = b * b; n     p /= 2; n   } n   return ans; n } n double fastpower_double(double b, long long p) { n   double ans = 1; n   while (p) { n     if (p % 2) { n       ans = ans * b; n     } n     b = b * b; n     p /= 2; n   } n   return ans; n } n long long summation_formula(long long n) { return (n * (n + 1) / 2); } n bool lower_vowel(char c) { n   return (c ==  i  || c ==  o  || c ==  u  || c ==  a  || c ==  e ); n } n string bigint_mini(string s1, string s2) { n   if (s1.size() > s2.size()) { n     return s2; n   } else if (s2.size() > s1.size()) { n     return s1; n   } n   for (int i = 0; i < s1.size(); i++) { n     if ((s1[i] -  0 ) > (s2[i] -  0 )) { n       return s2; n     } else if ((s2[i] -  0 ) > (s1[i] -  0 )) { n       return s1; n     } n   } n   return s1; n } n double polygon_area(int n, vector<double> X, vector<double> Y) { n   double area = 0.0; n   int j = n - 1; n   for (int i = 0; i < n; i++) { n     area += (X[j] + X[i]) * (Y[j] - Y[i]); n     j = i; n   } n   return abs(area / 2.0); n } n long long sum_of_digits(string s) { n   long long sum = 0; n   for (int i = 0; i < s.size(); i++) { n     sum += s[i] -  0 ; n   } n   return sum; n } n string makeitbase(long long num, long long base) { n   string s; n   while (num) { n     long long mod = num % base; n     s += (mod +  0 ); n     num /= base; n   } n   reverse(s.begin(), s.end()); n   return s; n } n bool intersect(long long l1, long long r1, long long l2, long long r2) { n   return (max(l1, l2) <= min(r1, r2)); n } n pair<long long, long long> find_intersection(long long l1, long long r1, n                                              long long l2, long long r2) { n   return {max(l1, l2), min(r1, r2)}; n } n long long sum_ranges(long long l, long long r) { n   return summation_formula(r) - summation_formula(l); n } n double power_2(double num) { return num * num; } n struct point { n   int x, y; n }; n long long dist(point p1, point p2) { n   return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y); n } n bool square(point p1, point p2, point p3, point p4) { n   long long d2 = dist(p1, p2); n   long long d3 = dist(p1, p3); n   long long d4 = dist(p1, p4); n   if (d2 == d3 && 2 * d2 == d4 && 2 * dist(p2, p4) == dist(p2, p3)) { n     return true; n   } n   if (d3 == d4 && 2 * d3 == d2 && 2 * dist(p3, p2) == dist(p3, p4)) { n     return true; n   } n   if (d2 == d4 && 2 * d2 == d3 && 2 * dist(p2, p3) == dist(p2, p4)) { n     return true; n   } n   return false; n } n const int sz = 1e6 + 1; n long long M = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n, k, m, t; n   cin >> n >> k >> m >> t; n   while (t--) { n     int x, y; n     cin >> x >> y; n     if (x == 1) { n       if (y <= k) { n         k++; n       } n       n++; n     } else { n       if (k >= 1 && k <= y) { n         n -= (n - y); n       } else { n         n -= y; n         k -= y; n       } n     } n     cout << n <<     << k << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int f = n * 1.0 / 3 + 0.5; n   cout << f / 12 <<     << f % 12 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX1 = 3e5 + 10; n long long a, b, c, d, e, cnt, x, y, z, l, r, flag, num[MAX1], mod = 1e10 + 7; n string s; n char p[MAX1]; n long long sta(long long n) { n   for (int i = 1; i < 1000000; ++i) { n     if (i * (i - 1) / 2 == n) return i; n   } n   return 0; n } n int main() { n   cin >> a >> b >> c >> d; n   x = sta(a); n   y = sta(d); n   if (a == 0) { n     if (b == 0 && c == 0 && x && y) { n       for (int i = 0; i < y; ++i) cout << 1; n       return 0; n     } else n       x = 1; n   } n   if (d == 0) { n     if (b == 0 && c == 0 && x && y) { n       for (int i = 0; i < x; ++i) cout << 0; n       return 0; n     } else n       y = 1; n   } n   if (x == 0 || y == 0) { n     cout <<  Impossible ; n     return 0; n   } n   if (x * y != b + c) { n     cout <<  Impossible ; n     return 0; n   } n   cnt = b / y; n   b -= cnt * y; n   x -= cnt; n   for (long long i = 0; i < cnt; ++i) cout << 0; n   for (long long i = 0; i < y - b; ++i) cout << 1; n   y -= y - b; n   if (x && b) cout << 0, --x; n   for (long long i = 0; i < y; ++i) cout << 1; n   for (long long i = 0; i < x; ++i) cout << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long bigpow(long long x, long long n) { n   if (n == 0) return 1; n   long long temp = bigpow(x, n / 2); n   return n % 2 == 0 ? (temp * temp) % 1000000009 n                     : (x * ((temp * temp) % 1000000009)) % 1000000009; n } n int main() { n   long long n, m, k, x, t, p, q, gaps, groups; n   cin >> n >> m >> k; n   gaps = n - m; n   groups = m % (k - 1) == 0 ? m / (k - 1) : m / (k - 1) + 1; n   if (gaps >= groups - 1) n     cout << m << endl; n   else { n     x = n - gaps * k; n     t = 2 * (bigpow(2, x / k) - 1) * k + x % k; n     t += gaps * (k - 1); n     cout << t % 1000000009 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const int MOD = 1e9 + 7; n const int N = 0; n int a[5], m; n long long sum(int len, int x) { n   return 1LL * len * x - 1LL * (x + 1) * x / 2 + x; n } n long long cal(int x, int y, int bar, int len) { n   if (len < 0) return 0; n   int R = min(bar - y, len), L = min(len - (bar - x), R); n   long long ans = 0; n   if (L >= 0) n     ans += 1LL * (L + 1) * (bar - x + 1); n   else n     L = -1; n   ans += sum(len, R) - sum(len, L); n   return ans; n } n long long solve(int x, int y, int z, int len) { n   int tmp = max(max(y, z) + 1, x); n   len -= tmp - x; n   x = tmp; n   long long ans = 0; n   for (auto i = 0; i <= len; i++) { n     int cur = x + i, rem = min(2 * (cur - 1) - y - z, len - i); n     if (rem > cur - y - z) n       ans += cal(y, z, cur - 1, rem) - cal(y, z, cur - 1, (cur - y - z)); n   } n   return ans; n } n long long solve2(int x, int y, int z, int len) { n   int tmp = max(x, y); n   len -= 2 * tmp - x - y; n   x = tmp; n   if (x <= z) len -= 2 * (z + 1 - x), x = z + 1; n   long long ans = 0; n   if (len >= 0) n     for (auto i = 0; i <= len / 2; i++) { n       int rem = len - 2 * i; n       ans += min(x + i - 1 - z, rem) + 1; n     } n   return ans; n } n int main() { n   for (auto i = 0; i <= 2; i++) cin >> a[i]; n   cin >> m; n   long long ans = 0; n   for (auto i = 0; i <= 2; i++) n     ans += solve(a[i], a[(i + 1) % 3], a[(i + 2) % 3], m); n   for (auto i = 0; i <= 2; i++) n     ans += solve2(a[i], a[(i + 1) % 3], a[(i + 2) % 3], m); n   int tmp = *max_element(a, a + 3), v = 0; n   for (auto i = 0; i <= 2; i++) v += tmp - a[i]; n   if (v <= m) ans += (m - v) / 3 + 1; n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int a[k]; n   for (int i = 0; i < k; i++) cin >> a[i]; n   list<int> leader; n   for (int i = 1; i <= n; i++) leader.insert(leader.end(), i); n   int l = 1; n   int i = 0; n   int begin = 0; n   while (k--) { n     l = (leader.size() + a[i] + begin) % leader.size(); n     list<int>::iterator pnlist = leader.begin(); n     advance(pnlist, l); n     if (k == 0) n       cout << *pnlist << endl; n     else n       cout << *pnlist <<    ; n     leader.erase(pnlist); n     begin = l; n     i++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n int main() { n   int n; n   cin >> n; n   vector<int> q(n); n   for (int i = 0; i < n; ++i) cin >> q[i]; n   vector<int> cost(n, -1); n   int m; n   cin >> m; n   for (int i = 0; i < m; ++i) { n     int a, b, c; n     cin >> a >> b >> c; n     if (cost[b - 1] == -1 || cost[b - 1] > c) { n       cost[b - 1] = c; n     } n   } n   int ans = 0, head = 0; n   for (int i = 0; i < n; ++i) { n     if (cost[i] != -1) n       ans += cost[i]; n     else n       head++; n   } n   if (head > 1) n     cout << -1 << endl; n   else { n     if (!head) { n       int maxx = cost[0]; n       for (int i = 1; i < n; ++i) maxx = max(maxx, cost[i]); n       ans -= maxx; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<int> g[200002]; n vector<pair<int, pair<int, int> > > first[200002]; n int lvl[200002]; n int p[20][200002]; n int h[20][200002]; n int d[200002][2]; n inline int lca(int v1, int v2) { n   if (lvl[v1] < lvl[v2]) swap(v1, v2); n   for (int i = 20 - 1; i >= 0; --i) { n     if (lvl[p[i][v1]] >= lvl[v2]) { n       v1 = p[i][v1]; n     } n   } n   if (v1 == v2) return v1; n   for (int i = 20 - 1; i >= 0; --i) { n     if (p[i][v1] != p[i][v2]) { n       v1 = p[i][v1]; n       v2 = p[i][v2]; n     } n   } n   return p[0][v1]; n } n void dfs(int v = 0, int level = 0, int pr = 0) { n   lvl[v] = level; n   p[0][v] = pr; n   for (int i = 1; i < 20; ++i) p[i][v] = p[i - 1][p[i - 1][v]]; n   for (const int& v2 : g[v]) { n     dfs(v2, level + 1, v); n   } n } n inline int getit(int v, int k) { n   if (h[k][v] != -1) return h[k][v]; n   return h[k][v] = getit(p[k - 1][v], k - 1) + getit(v, k - 1); n } n inline int get(int v, int k) { n   if (k < 0) return 0; n   int res = 0; n   for (int i = 20 - 1; i >= 0; --i) { n     if ((k & (1 << i)) > 0) { n       res += getit(v, i); n       k ^= (1 << i); n       v = p[i][v]; n     } n   } n   return res; n } n void go(int v = 0) { n   for (const int& v2 : g[v]) { n     go(v2); n     d[v][0] += max(d[v2][0], d[v2][1]); n   } n   for (const int& v2 : g[v]) { n     h[0][v2] = d[v][0] - max(d[v2][0], d[v2][1]); n   } n   for (const auto& t : first[v]) { n     int cost = t.first; n     int v1 = t.second.first; n     int v2 = t.second.second; n     int w = -d[v][0] + get(v1, lvl[v1] - lvl[v]) + get(v2, lvl[v2] - lvl[v]) + n             d[v1][0] + d[v2][0] + cost; n     d[v][1] = max(d[v][1], w); n   } n } n int main() { n   memset(h, -1, sizeof(h)); n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n - 1; ++i) { n     int t; n     scanf( %d , &t); n     --t; n     g[t].push_back(i + 1); n   } n   dfs(); n   for (int i = 0; i < m; ++i) { n     int v1, v2, cost; n     scanf( %d%d%d , &v1, &v2, &cost); n     --v1, --v2; n     int lcav = lca(v1, v2); n     first[lcav].push_back(make_pair(cost, make_pair(v1, v2))); n   } n   go(); n   printf( %d n , max(d[0][0], d[0][1])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t[2000002]; n int longestPrefix(string &s) { n   t[0] = 0; n   for (int pos = 1; pos < s.length(); ++pos) { n     int prev = t[pos - 1]; n     while (prev > 0 && s[pos] != s[prev]) { n       prev = t[prev - 1]; n     } n     if (s[pos] == s[prev]) { n       t[pos] = prev + 1; n     } else { n       t[pos] = prev; n     } n   } n   return t[s.length() - 1]; n } n int main() { n   int t; n   cin >> t; n   while (t-- > 0) { n     string s; n     cin >> s; n     int lindex = 0, rindex = s.length(); n     while (lindex < rindex && s[lindex] == s[rindex - 1]) { n       ++lindex; n       --rindex; n     } n     if (lindex >= rindex) { n       cout << s << endl; n       continue; n     } n     string mid = s.substr(lindex, rindex - lindex); n     string rmid = mid; n     reverse(rmid.begin(), rmid.end()); n     string pre = mid +  *  + rmid; n     string suf = rmid +  *  + mid; n     int pref = longestPrefix(pre); n     int suff = longestPrefix(suf); n     int len = max(pref, suff); n     for (int i = 0; i < lindex; ++i) { n       cout << s[i]; n     } n     if (pref >= suff) { n       for (int i = lindex; i < lindex + pref; ++i) { n         cout << s[i]; n       } n     } else { n       for (int i = rindex - suff; i < rindex; ++i) { n         cout << s[i]; n       } n     } n     for (int i = rindex; i < s.length(); ++i) { n       cout << s[i]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int V[200010]; n char vowel[] =  aeiouAEIOU ; n int v(int a, int k) { n   if (k == 0) return 0; n   int x = V[a + k - 1]; n   if (a > 0) x -= V[a - 1]; n   return x; n } n int f(int a, int k) { return 2 * k - 3 * v(a, k); } n int g(int k) { return f(0, k); } n bool Good(int a, int k) { return f(a, k) >= 0; } n bool is_vowel(char c) { n   for (int i = 0; i < 10; i++) { n     if (vowel[i] == c) return true; n   } n   return false; n } n bool sf(const int& a, const int& b) { n   if (g(a) < g(b)) return true; n   if (g(a) > g(b)) return false; n   return a < b; n } n int Q[200010]; n vector<int> P; n int R[200010]; n int my_bin_search(int s, string S) { n   int a = 0; n   int b = P.size() - 1; n   while (a <= b) { n     int k = (a + b) / 2; n     if (P[k] == s) n       return k; n     else if (sf(P[k], s)) n       a = k + 1; n     else n       b = k - 1; n   } n   throw  element not found ; n } n void create_data(string S) { n   int N = S.length(); n   for (int a = 0; a <= N; a++) { n     P.push_back(a); n   } n   sort(P.begin(), P.end(), sf); n   for (int x = 0; x < P.size(); x++) { n     R[P[x]] = x; n   } n   Q[0] = P[0]; n   for (unsigned int x = 1; x < P.size(); x++) { n     Q[x] = min(Q[x - 1], P[x]); n   } n } n int main() { n   string S; n   cin >> S; n   int N = S.length(); n   V[0] = 0; n   for (unsigned int i = 0; i < S.length(); i++) { n     if (i > 0) V[i] = V[i - 1]; n     if (is_vowel(S[i])) V[i]++; n   } n   create_data(S); n   int bestk = 0; n   for (int a = 0; a <= N; a++) { n     int x = R[a]; n     int b = Q[x]; n     bestk = max(bestk, a - b); n   } n   if (bestk == 0) { n     cout <<  No solution  << endl; n     return 0; n   } n   int found = 0; n   for (int a = 0; (a + bestk) <= N; a++) { n     if (Good(a, bestk)) { n       found++; n     } n   } n   cout << bestk <<     << found << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n void read(int& x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) f = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   x *= f; n } n int n, f[200005], g[200005], els[200005]; n vector<int> E[200005]; n int power(int a, int b, int p) { n   int s = 1, w = a; n   while (b) { n     if (b & 1) s = (long long)s * w % p; n     w = (long long)w * w % p; n     b >>= 1; n   } n   return s; n } n vector<int> F[200005], B[200005]; n void dfs(int now) { n   f[now] = 1; n   for (int i = (0); i < (E[now].size()); i++) { n     dfs(E[now][i]), n         f[now] = (long long)f[now] * (f[E[now][i]] + 1) % 1000000007; n     F[now].push_back(f[E[now][i]] + 1), B[now].push_back(f[E[now][i]] + 1); n   } n   for (int i = (1); i < (E[now].size()); i++) n     F[now][i] = (long long)F[now][i - 1] * F[now][i] % 1000000007; n   for (int i = (E[now].size() - 2); i >= (0); i--) n     B[now][i] = (long long)B[now][i + 1] * B[now][i] % 1000000007; n   if (E[now].size() == 1) { n     els[E[now][0]] = 1; n     return; n   } n   for (int i = (0); i < (E[now].size()); i++) { n     if (i == 0) n       els[E[now][i]] = B[now][i + 1]; n     else if (i + 1 == E[now].size()) n       els[E[now][i]] = F[now][i - 1]; n     else n       els[E[now][i]] = (long long)B[now][i + 1] * F[now][i - 1] % 1000000007; n   } n } n void work(int now) { n   for (int i = (0); i < (E[now].size()); i++) { n     g[E[now][i]] = ((long long)g[now] * els[E[now][i]] + 1) % 1000000007; n     work(E[now][i]); n   } n } n int main() { n   read(n); n   for (int i = (2); i <= (n); i++) { n     int x; n     read(x); n     E[x].push_back(i); n   } n   dfs(1); n   g[1] = 1; n   work(1); n   for (int i = (1); i <= (n); i++) n     printf( %lld  , (long long)f[i] * g[i] % 1000000007); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string n; n int i, j, a[9][7], x, y; n int main() { n   cin >> n; n   for (i = n.size() - 1; i >= 0; i--) { n     x = n[i] - 48; n     y = max(y, x); n     for (j = 0; j < x; j++) a[j][7 + i - n.size()] = 1; n   } n   cout << y << endl; n   for (i = 0; i < y; cout <<    , i++) n     for (j = 0, x = 0; j < 7; j++) { n       if (a[i][j] != 0) x = 1; n       if (x != 0) cout << a[i][j]; n     } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long s = 0, w = 1; n   register char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); n   return s * w; n } n long long n; n string s; n signed main() { n   n = read(); n   cin >> s; n   for (register long long i = 2; i <= n; i++) { n     if (n % i) continue; n     for (register long long j = 0; j < i / 2; j++) swap(s[j], s[i - j - 1]); n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int D = 505; n const int N = 5005; n pair<int, int> pre[D][N]; n int ans[D][N]; n void solve() { n   memset(pre, -1, sizeof(pre)); n   memset(ans, -1, sizeof(ans)); n   int d, s; n   cin >> d >> s; n   queue<pair<int, int> > que; n   que.push(pair<int, int>(0, 0)); n   vector<int> vec; n   while (!que.empty()) { n     pair<int, int> u = que.front(); n     que.pop(); n     if (u.first == 0 && u.second == s) { n       pair<int, int> cur = u; n       while (!(cur.first == 0 && cur.second == 0)) { n         vec.push_back(ans[cur.first][cur.second]); n         cur = pre[cur.first][cur.second]; n       } n       break; n     } n     for (int i = 0; i <= 9; i++) { n       pair<int, int> v = pair<int, int>((u.first * 10 + i) % d, u.second + i); n       if (v.second <= s && ans[v.first][v.second] == -1) { n         ans[v.first][v.second] = i; n         pre[v.first][v.second] = u; n         que.push(v); n       } n     } n   } n   if (!vec.size()) n     printf( -1 n ); n   else { n     reverse(vec.begin(), vec.end()); n     for (auto i : vec) putchar(i +  0 ); n     printf(  n ); n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 5, MOD = 998244353; n int m, n, k; n long long res; n int free_Row, free_Col, line_vert, line_hori; n int Num_Row[N], Num_Row2[N][2], Num_Col[N], Num_Col2[N][2]; n int c[2][2]; n map<pair<int, int>, int> M; n long long power(long long x, int k) { n   long long ans = 1; n   while (k) { n     if (k & 1) ans = ans * x % MOD; n     x = x * x % MOD; n     k >>= 1; n   } n   return ans; n } n void Calculate() { n   res = 0; n   if (line_vert && line_hori) return; n   if (!line_vert) (res += power(2, free_Row)) %= MOD; n   if (!line_hori) (res += power(2, free_Col)) %= MOD; n   if (!line_vert && !line_vert) { n     if (c[0][0] == 0 && c[1][1] == 0) (res += MOD - 1) %= MOD; n     if (c[0][1] == 0 && c[1][0] == 0) (res += MOD - 1) %= MOD; n   } n } n void Delete(int x, int y) { n   if (!M.count({x, y})) return; n   int z = M[{x, y}]; n   M.erase({x, y}); n   --c[z][(x + y) & 1]; n   if (--Num_Row[x] == 0) ++free_Row; n   if (--Num_Row2[x][z ^ (y & 1)] == 0 && Num_Row[x]) --line_vert; n   if (--Num_Col[y] == 0) ++free_Col; n   if (--Num_Col2[y][z ^ (x & 1)] == 0 && Num_Col[y]) --line_hori; n } n void Add(int x, int y, int z) { n   M[{x, y}] = z; n   ++c[z][(x + y) & 1]; n   if (Num_Row[x]++ == 0) --free_Row; n   if (Num_Row2[x][z ^ (y & 1)]++ == 0 && Num_Row[x] > 1) ++line_vert; n   if (Num_Col[y]++ == 0) --free_Col; n   if (Num_Col2[y][z ^ (x & 1)]++ == 0 && Num_Col[y] > 1) ++line_hori; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> m >> n >> k; n   free_Row = m; n   free_Col = n; n   line_hori = 0; n   line_vert = 0; n   int x, y, z; n   while (k--) { n     cin >> x >> y >> z; n     Delete(x, y); n     if (z >= 0) Add(x, y, z); n     Calculate(); n     cout << res <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10000; n vector<int> vec(N); n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> vec[i]; n   } n   sort(vec.begin(), vec.begin() + n); n   int sum = 0; n   for (int i = 0; i < m; i++) { n     sum += vec[i]; n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n namespace algo { n using std::swap; n const long double PI = 3.141592653589793238462643383279502884L; n const long double E = 2.718281828459045235360287471352662498L; n const long double EPS = 1e-12L; n template <typename T1, typename T2> n T1 bin_pow(T1 a, T2 n) { n   T1 r{1}; n   while (n > 0) { n     if (n % 2 == 1) r *= a; n     a *= a; n     n /= 2; n   } n   return r; n } n template <typename T1, typename T2> n T1 bin_pow(T1 a, T2 n, const T1& m) { n   if (m == 1) return 0; n   T1 r{1}; n   while (n > 0) { n     if (n % 2 == 1) { n       r *= a; n       r %= m; n     } n     a *= a; n     a %= m; n     n /= 2; n   } n   return r; n } n template <typename T> n T gcd(const T& a, const T& b) { n   if (b == 0) return a < 0 ? -a : a; n   return gcd(b, a % b); n } n template <typename T> n T gcd(const T& a, const T& b, T& x, T& y) { n   if (b == 0) { n     x = a < 0 ? -1 : 1; n     y = 0; n     return x * a; n   } n   T d{gcd(b, a % b, y, x)}; n   y -= (a / b) * x; n   return d; n } n template <typename T> n T lcm(const T& a, const T& b) { n   T r{a / gcd(a, b) * b}; n   return r < 0 ? -r : r; n } n template <typename T> n T mod_add(const T& a, const T& b, const T& m) { n   if (a < m - b) return a + b; n   return a - (m - b); n } n template <typename T> n T mod_sub(const T& a, const T& b, const T& m) { n   if (a < b) return a + (m - b); n   return a - b; n } n template <typename T> n T mod_mul(T a, T b, const T& m) { n   if (b > a) swap(a, b); n   T r{0}; n   while (b > 0) { n     if (b % 2 == 1) r = mod_add(r, a, m); n     a = mod_add(a, a, m); n     b /= 2; n   } n   return r; n } n template <typename T1, typename T2> n T1 mod_pow(T1 a, T2 n, const T1& m) { n   if (m == 1) return 0; n   T1 r{1}; n   while (n > 0) { n     if (n % 2 == 1) r = mod_mul(r, a, m); n     a = mod_mul(a, a, m); n     n /= 2; n   } n   return r; n } n template <typename T> n T mod_inv(const T& a, const T& m) { n   T x, y; n   T d{gcd(a, m, x, y)}; n   if (d != 1) return 0; n   if (x < 0) x += m; n   return x; n } n template <typename T> n T phi(T n) { n   T t = std::sqrt(n) + EPS; n   T r = n; n   for (T i = 2; i <= t; ++i) { n     if (n % i == 0) { n       r -= r / i; n       do { n         n /= i; n       } while (n % i == 0); n       t = std::sqrt(n) + EPS; n     } n   } n   if (n > 1) r -= r / n; n   return r; n } n class LinearPhi { n  public: n   LinearPhi(uint32_t n = 1 << 20) : size_{n} { n     assert(size_ > 0 &&  Size should be nonzero ); n     assert(size_ <= INT32_MAX &&  Size should fit into an 32-bit signed int ); n     uint32_t sz = (size_ + 1) / 2; n     phi_ = new int32_t[sz]; n     mark_.assign(sz, false); n     phi_[0] = 1; n     if (size_ > 1) primes_.push_back(2); n     for (uint32_t i = 1; i < sz; ++i) { n       uint32_t val = i << 1 | 1; n       if (!mark_[i]) { n         phi_[i] = val - 1; n         primes_.push_back(val); n       } n       if (val < sz) { n         for (uint32_t j = 1; j < primes_.size(); ++j) { n           uint32_t t = val * primes_[j]; n           if (t > size_) break; n           mark_[t >> 1] = true; n           if (val % primes_[j] == 0) { n             phi_[t >> 1] = phi_[i] * primes_[j]; n             break; n           } n           phi_[t >> 1] = phi_[i] * (primes_[j] - 1); n         } n       } n     } n   } n   LinearPhi(const LinearPhi&) = delete; n   LinearPhi& operator=(const LinearPhi&) = delete; n   ~LinearPhi() { n     delete[] phi_; n     phi_ = nullptr; n   } n   const uint32_t& size() const { return size_; } n   int32_t phi(int32_t i) const { n     int32_t r = 1; n     int k = __builtin_ctz(i); n     if (k > 0) { n       i >>= k; n       r <<= k - 1; n     } n     return r * phi_[i >> 1]; n   } n   int32_t operator[](int32_t i) const { return phi(i); } n   const std::vector<int32_t>& primes() const { return primes_; } n  n  private: n   int32_t* phi_ = nullptr; n   std::vector<bool> mark_; n   uint32_t size_; n   std::vector<int32_t> primes_; n }; n class Sieve { n  public: n   Sieve(uint32_t n = 1 << 20) : size_{n} { n     assert(size_ > 0 &&  Size should be nonzero ); n     assert(size_ <= INT32_MAX &&  Size should fit into an 32-bit signed int ); n     uint32_t sz = (size_ + 1) / 2; n     prime_.assign(sz, true); n     prime_[0] = false; n     for (uint32_t i = 1; i < sz; ++i) { n       uint32_t val = i << 1 | 1; n       if (prime_[i] && val * 1ULL * val <= size_) n         for (uint32_t j = val * val; j <= size_; j += 2 * val) n           prime_[j >> 1] = false; n     } n   } n   Sieve(const Sieve&) = delete; n   Sieve& operator=(const Sieve&) = delete; n   const uint32_t& size() const { return size_; } n   bool isPrime(const int32_t& i) const { n     if (i % 2 == 0) return i == 2; n     return prime_[i >> 1]; n   } n   bool operator[](const int32_t& i) const { return isPrime(i); } n  n  private: n   uint32_t size_; n   std::vector<bool> prime_; n }; n class LinearSieve { n  public: n   LinearSieve(uint32_t n = 1 << 20) : size_{n} { n     assert(size_ > 0 &&  Size should be nonzero ); n     assert(size_ <= INT32_MAX &&  Size should fit into an 32-bit signed int ); n     uint32_t sz = (size_ + 1) / 2; n     lpf_ = new uint16_t[sz]; n     std::fill(lpf_, lpf_ + sz, 0); n     lpf_[0] = 1; n     if (size_ > 1) primes_.push_back(2); n     for (uint32_t i = 1; i < sz; ++i) { n       uint32_t val = i << 1 | 1; n       if (!lpf_[i]) primes_.push_back(val); n       if (val < sz) { n         for (uint32_t j = 1; j < primes_.size(); ++j) { n           uint32_t t = val * primes_[j]; n           if (t > size_) break; n           lpf_[t >> 1] = static_cast<uint16_t>(primes_[j]); n           if (lpf_[t >> 1] == (lpf_[i] ? lpf_[i] : val)) break; n         } n       } n     } n   } n   LinearSieve(const LinearSieve&) = delete; n   LinearSieve& operator=(const LinearSieve&) = delete; n   ~LinearSieve() { n     delete[] lpf_; n     lpf_ = nullptr; n   } n   const uint32_t& size() const { return size_; } n   bool isPrime(const int32_t& i) const { n     if (i % 2 == 0) return i == 2; n     return !lpf_[i >> 1]; n   } n   bool operator[](const int32_t& i) const { return isPrime(i); } n   int32_t lpf(const int32_t& i) const { n     if (i % 2 == 0) return 2; n     if (!lpf_[i >> 1]) return i; n     return lpf_[i >> 1]; n   } n   int32_t phi(int32_t i) const { n     int32_t r = 1; n     int k = __builtin_ctz(i); n     if (k > 0) { n       i >>= k; n       r <<= k - 1; n     } n     int32_t n, t = lpf_[i >> 1] ? lpf_[i >> 1] : i; n     while (t != 1) { n       r *= (t - 1); n       i /= t; n       n = lpf_[i >> 1] ? lpf_[i >> 1] : i; n       while (n == t) { n         r *= n; n         i /= t; n         n = lpf_[i >> 1] ? lpf_[i >> 1] : i; n       } n       t = n; n     } n     return r; n   } n   const std::vector<int32_t>& primes() const { return primes_; } n  n  private: n   uint16_t* lpf_ = nullptr; n   uint32_t size_; n   std::vector<int32_t> primes_; n }; n }  // namespace algo n using namespace std; n using namespace algo; n namespace task { n int x1, y1, x2, y2, x3, y3; n int main() { n   cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; n   cout << 3 <<   n ; n   cout << x3 - x1 + x2 <<     << y3 - y1 + y2 <<   n ; n   cout << x1 - x2 + x3 <<     << y1 - y2 + y3 <<   n ; n   cout << x2 - x3 + x1 <<     << y2 - y3 + y1 <<   n ; n   return 0; n } n }  // namespace task n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.precision(11); n   cout.setf(ios::fixed); n   return task::main(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, i; n   string s; n   cin >> t; n   while (t--) { n     cin >> s; n     int cap = 0, sm = 0, dig = 0; n     for (i = 0; i < s.size(); i++) { n       if (s[i] >= 65 && s[i] <= 90) { n         cap++; n       } else if (s[i] >= 97 && s[i] <= 122) { n         sm++; n       } else if (s[i] >= 48 && s[i] <= 57) { n         dig++; n       } n     } n     if (sm == 0) { n       if (cap > 1) { n         for (i = 0; i < s.size(); i++) { n           if (s[i] >= 65 && s[i] <= 90) { n             s[i] =  a ; n             break; n           } n         } n       } else if (dig > 1) { n         for (i = 0; i < s.size(); i++) { n           if (s[i] >= 48 && s[i] <= 57) { n             s[i] =  a ; n             break; n           } n         } n       } else { n         s = s +  a ; n       } n     } n     if (cap == 0) { n       if (sm > 1) { n         for (i = 0; i < s.size(); i++) { n           if (s[i] >= 97 && s[i] <= 122) { n             s[i] =  A ; n             break; n           } n         } n       } else if (dig > 1) { n         for (i = 0; i < s.size(); i++) { n           if (s[i] >= 48 && s[i] <= 57) { n             s[i] =  A ; n             break; n           } n         } n       } else { n         s = s +  A ; n       } n     } n     if (dig == 0) { n       if (cap > 1) { n         for (i = 0; i < s.size(); i++) { n           if (s[i] >= 65 && s[i] <= 90) { n             s[i] =  1 ; n             break; n           } n         } n       } else if (sm > 1) { n         for (i = 0; i < s.size(); i++) { n           if (s[i] >= 97 && s[i] <= 122) { n             s[i] =  1 ; n             break; n           } n         } n       } else { n         s = s +  1 ; n       } n     } n     cout << s << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool BeauYear(int y) { n   int a = y % 10; n   int b = (y / 10) % 10; n   int c = (y / 100) % 10; n   int d = (y / 1000) % 10; n   if (a != b && a != c && a != d && b != c && b != d && c != d) return true; n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int year; n   cin >> year; n   for (int i = year + 1; i <= 9012; i++) { n     if (BeauYear(i) == true) { n       cout << i; n       i = 9013; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, a, b, q, p[N], d[N]; n vector<pair<int, int> > g[N]; n int dp[N][17], v, ud[N], ans[N]; n bool vis[N]; n void dfs(int u) { n   vis[u] = 1; n   for (int i = 0; i < (int)g[u].size(); ++i) { n     v = g[u][i].first; n     if (!vis[v]) { n       p[v] = u; n       d[v] = d[u] + 1; n       dfs(v); n     } n   } n } n int lca(int a, int b) { n   if (d[a] < d[b]) swap(a, b); n   int log = 0; n   for (; (1 << log) <= d[a]; ++log) n     ; n   --log; n   for (int i = log; i >= 0; --i) { n     if (d[a] - (1 << i) >= d[b]) a = dp[a][i]; n   } n   if (a == b) return a; n   for (int i = log; i >= 0; --i) { n     if (dp[a][i] != dp[b][i]) { n       a = dp[a][i]; n       b = dp[b][i]; n     } n   } n   return p[a]; n } n int DFS(int u) { n   vis[u] = 1; n   for (int i = 0; i < (int)g[u].size(); ++i) { n     int v = g[u][i].first; n     if (!vis[v]) { n       ud[u] += DFS(v); n       ans[g[u][i].second] = ud[v]; n     } n   } n   return ud[u]; n } n int main(int argc, char **argv) { n   scanf( %d , &n); n   for (int i = 1; i < n; ++i) { n     scanf( %d%d , &a, &b); n     g[a].push_back(make_pair(b, i)); n     g[b].push_back(make_pair(a, i)); n   } n   memset(p, -1, sizeof p); n   memset(dp, -1, sizeof dp); n   dfs(1); n   for (int i = 0; i <= n; ++i) dp[i][0] = p[i]; n   for (int j = 1; (1 << j) <= n; ++j) { n     for (int i = 0; i <= n; ++i) { n       if (dp[i][j - 1] != -1) dp[i][j] = dp[dp[i][j - 1]][j - 1]; n     } n   } n   scanf( %d , &q); n   int LCA; n   while (q--) { n     scanf( %d%d , &a, &b); n     LCA = lca(a, b); n     ++ud[a], ++ud[b], ud[LCA] -= 2; n   } n   memset(vis, 0, sizeof vis); n   DFS(1); n   for (int i = 1; i < n; ++i) { n     if (i > 1) printf(   ); n     printf( %d , ans[i]); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string tobinary(long long x) { n   string ret; n   char ch; n   while (x) ch =  0  + (x % 2), ret = ch + ret, x /= 2; n   return ret; n } n long long todecimal(string s) { n   int sz = s.size(); n   long long ret = 0; n   for (int i = 0; i < sz; i++) ret = 2 * ret + (s[i] -  0 ); n   return ret; n } n bool _greater(string s, string t) { n   int n = s.size(), m = t.size(); n   if (n > m) return true; n   if (n < m) return false; n   for (int i = 0; i < n; i++) { n     if (s[i] > t[i]) n       return true; n     else if (s[i] < t[i]) n       return false; n   } n   return false; n } n long long eval(long long l, long long r) { n   string s = tobinary(l), t = tobinary(r), ret = s; n   while (1) { n     if (_greater(s, t)) break; n     ret = s; n     int sz = s.size(); n     bool ok = false; n     for (int i = sz - 1; i >= 0; i--) n       if (s[i] ==  0 ) { n         s[i] =  1 ; n         ok = true; n         break; n       } n     if (!ok) { n       s =  1  + s; n       int n = s.size(), m = t.size(); n       if (n > m) break; n     } n   } n   return todecimal(ret); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   while (n--) { n     long long l, r; n     cin >> l >> r; n     cout << eval(l, r) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > points; n pair<int, int> dif(pair<int, int> pa, pair<int, int> pb) { n   pair<int, int> res; n   res.first = pb.first - pa.first; n   res.second = pb.second - pa.second; n   return res; n } n int dot(pair<int, int> a, pair<int, int> b) { n   return a.first * b.first + a.second * b.second; n } n double dist(pair<int, int> a, pair<int, int> b) { n   return ((double)((a.first - b.first) * (a.first - b.first) + n                    (a.second - b.second) * (a.second - b.second))); n } n double len(pair<int, int> a) { return dist(a, make_pair(0, 0)); } n int main() { n   for (int i = 0; i < 8; i++) { n     int x, y; n     cin >> x >> y; n     points.push_back(make_pair(x, y)); n   } n   vector<int> perm; n   for (int i = 0; i < 8; i++) perm.push_back(i); n   int cnt = 0; n   bool found = false; n   do { n     cnt++; n     bool fs = false, ss = false; n     bool fis = true, sif = true; n     pair<int, int> a = points[perm[0]]; n     pair<int, int> b = points[perm[1]]; n     pair<int, int> c = points[perm[2]]; n     pair<int, int> d = points[perm[3]]; n     pair<int, int> e = points[perm[4]]; n     pair<int, int> f = points[perm[5]]; n     pair<int, int> g = points[perm[6]]; n     pair<int, int> h = points[perm[7]]; n     pair<int, int> ab = dif(a, b); n     pair<int, int> bc = dif(b, c); n     pair<int, int> cd = dif(c, d); n     pair<int, int> da = dif(d, a); n     fs = (dot(ab, bc) == 0 && dot(bc, cd) == 0 && dot(cd, da) == 0 && n           dot(da, ab) == 0 && fabs(len(ab) - len(bc)) < 0.01 && n           fabs(len(bc) - len(cd)) < 0.01 && fabs(len(cd) - len(da)) < 0.01); n     pair<int, int> ef = dif(e, f); n     pair<int, int> fg = dif(f, g); n     pair<int, int> gh = dif(g, h); n     pair<int, int> he = dif(h, e); n     ss = (dot(ef, fg) == 0 && dot(fg, gh) == 0 && dot(gh, he) == 0 && n           dot(he, ef) == 0 && fabs(len(ef) - len(gh)) < 0.01 && n           fabs(len(fg) - len(he)) < 0.01); n     if (ss && fs) { n       found = true; n       break; n     } n   } while (next_permutation(perm.begin(), perm.end())); n   if (found) { n     cout <<  YES  << endl; n     for (int i = 0; i < 4; i++) { n       if (i != 0) cout <<    ; n       cout << perm[i] + 1; n     } n     cout << endl; n     for (int i = 4; i < 8; i++) { n       if (i != 4) cout <<    ; n       cout << perm[i] + 1; n     } n     cout << endl; n   } else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int n; n char s[5001]; n int dp[5001][5001]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf(  n%c , &s[i]); n   } n   dp[1][0] = 1; n   for (int j = 1; j <= n; ++j) { n     dp[1][j] = 0; n   } n   for (int i = 2; i <= n; ++i) { n     if (s[i - 1] ==  f ) { n       for (int j = 1; j <= n; ++j) { n         dp[i][j] = dp[i - 1][j - 1]; n       } n     } else { n       int x = 0; n       for (int j = n; j >= 0; --j) { n         x += dp[i - 1][j]; n         if (x >= mod) { n           x -= mod; n         } n         dp[i][j] = x; n       } n     } n   } n   int res = 0; n   for (int i = 0; i <= n; ++i) { n     res += dp[n][i]; n     if (res >= mod) { n       res -= mod; n     } n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lli = long long; n using Double = long double; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string s[9][3]; n   for (int i = 0; i < 9; ++i) { n     for (int j = 0; j < 3; ++j) { n       cin >> s[i][j]; n     } n   } n   int x, y, cnt = 0; n   cin >> x >> y, --x, --y; n   for (auto c : s[x % 3 * 3][y % 3]) cnt += c ==  . ; n   for (auto c : s[x % 3 * 3 + 1][y % 3]) cnt += c ==  . ; n   for (auto c : s[x % 3 * 3 + 2][y % 3]) cnt += c ==  . ; n   if (cnt == 0) { n     for (int i = 0; i < 9; ++i) { n       for (int j = 0; j < 3; ++j) { n         for (auto c : s[i][j]) { n           cout << (c ==  .  ?  !  : c); n         } n         cout <<    n [j == 2]; n       } n       if (i % 3 == 2) cout <<   n ; n     } n   } else { n     for (int i = 0; i < 9; ++i) { n       for (int j = 0; j < 3; ++j) { n         for (auto c : s[i][j]) { n           cout << (c ==  .  and i / 3 == x % 3 and j == y % 3 ?  !  : c); n         } n         cout <<    n [j == 2]; n       } n       if (i % 3 == 2) cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char str[606]; n long long tmp[80]; n long long mat[2000][32]; n int see[2000]; n int main() { n   int M; n   scanf( %d , &M); n   for (int i = 0; i < M; i++) { n     scanf( %s , str); n     int len = 0; n     while (str[len]) len++; n     for (int j = 0; j < 80; j++) tmp[j] = 0; n     for (int j = 0; j < len; j++) { n       long long &t = tmp[(len - j - 1) / 8]; n       t = t * 10 + str[j] -  0 ; n     } n     for (int j = 0; j < 250; j++) { n       long long t = tmp[0] & 255; n       for (int k = 0; k < 8; k++) n         if (t & (1 << k)) mat[j * 8 + k][i / 64] |= 1ll << (i % 64); n       for (int k = 79; k; k--) { n         tmp[k - 1] += (tmp[k] & 255) * 100000000ll; n         tmp[k] >>= 8; n       } n       tmp[0] >>= 8; n     } n   } n   for (int i = 0, j = 0; i < 2000 && j < M; j++) { n     bool seen = false; n     for (int k = i; k < 2000; k++) { n       if (mat[k][j / 64] & (1ll << (j % 64))) { n         for (int l = j / 64; l < 32; l++) { n           swap(mat[i][l], mat[k][l]); n         } n         seen = true; n         break; n       } n     } n     if (!seen) { n       int cnt = 0; n       for (int k = 0; k < i; k++) n         if (mat[k][j / 64] & (1ll << (j % 64))) cnt++; n       printf( %d , cnt); n       for (int k = 0; k < i; k++) n         if (mat[k][j / 64] & (1ll << (j % 64))) printf(  %d , see[k]); n       puts(  ); n       continue; n     } n     see[i] = j; n     printf( 0 n ); n     for (int k = 0; k < 2000; k++) n       if (k != i && (mat[k][j / 64] & (1ll << (j % 64)))) { n         for (int l = j / 64; l < 32; l++) mat[k][l] ^= mat[i][l]; n       } n     i++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxm = 2e5 + 10; n set<int> g; n queue<set<int> > q; n int m, n, x, y; n int dfs(int top) { n   if (abs(top) <= 1) return 0; n   int now = 0; n   if ((-top) % 2 == 0) n     now = top / 2; n   else n     now = (top + 1) / 2; n   if (g.find(now) == g.end()) { n     g.erase(top); n     g.insert(now); n     return 1; n   } else n     return dfs(now); n } n int main() { n   cin >> m; n   for (int i = 1; i <= m; i++) { n     cin >> x; n     g.insert(-x); n   } n   for (;;) { n     int top = *g.begin(); n     if (!dfs(top)) break; n   } n   for (auto e : g) cout << -e <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v; n int qtd[100001]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int a; n     scanf( %d , &a); n     v.push_back(a); n   } n   sort(v.begin(), v.end()); n   int cont = 0; n   for (int i = 1, tmp = 0; i < n; i++) { n     if (v[i] > v[i - 1]) { n       cont += i - tmp; n       tmp = i; n     } n     qtd[i] = cont; n   } n   int ans = 0; n   int used = 0; n   for (int i = 0; i < n; i++) { n     if (qtd[i] > used) { n       used++; n       ans++; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct W { n   int t, l, r; n } w[100010]; n int n, m, s, f; n int main() { n   while (cin >> n >> m >> s >> f) { n     for (int i = (0); i < (m); i++) { n       cin >> w[i].t >> w[i].l >> w[i].r; n     } n     int t = 1, p = 0; n     int dir = f > s ? 1 : -1; n     while (s != f) { n       if (p < m && w[p].t == t && n           ((w[p].l <= s && s <= w[p].r) || n            (w[p].l <= s + dir && s + dir <= w[p].r))) { n         cout <<  X ; n       } else { n         s += dir; n         cout << (dir == 1 ?  R  :  L ); n       } n       if (++t > w[p].t) p++; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 100; n int t, cnt[N]; n int mx; n int check(int row, int col, int n, int m) { n   if (row < 0 || row >= n || col < 0 || col >= m) return 0; n   int i = 0, j = 0, ok = 1; n   for (; i < n; i++) { n     for (j = 0; j < m; j++) { n       if (!cnt[abs(row - i) + abs(col - j)]--) { n         ok = 0; n         break; n       } n     } n     if (!ok) break; n   } n   if (ok) { n     cout << n <<     << m << endl; n     cout << row + 1 <<     << col + 1 << endl; n     return 1; n   } n   for (int a = 0; a <= i; a++) n     for (int b = 0; b < m && (a != i || b <= j); b++) n       ++cnt[abs(row - a) + abs(col - b)]; n   return 0; n } n bool solve(int row, int n, int m, int mxi, int mxj) { n   if (check(row, mxj - (mx - abs(row - mxi)), n, m)) return 1; n   return check(row, mxj + (mx - abs(row - mxi)), n, m); n } n int main() { n   scanf( %d , &t); n   for (int i = 0; i < t; ++i) { n     int v; n     scanf( %d , &v); n     ++cnt[v]; n     mx = max(mx, v); n   } n   if (cnt[0] != 1) return puts( -1 ); n   int a; n   if (cnt[1] != 4) n     a = 0; n   else { n     a = 1; n     while (cnt[a + 1] == (a + 1) * 4) a++; n   } n   for (int n = 1; n * n <= t; n++) { n     if (t % n) continue; n     int m = t / n; n     if (solve(a, n, m, 0, 0)) return 0; n     if (solve(a, n, m, 0, m - 1)) return 0; n     if (solve(a, n, m, n - 1, 0)) return 0; n     if (solve(a, n, m, n - 1, m - 1)) return 0; n     if (solve(a, m, n, 0, 0)) return 0; n     if (solve(a, m, n, 0, n - 1)) return 0; n     if (solve(a, m, n, m - 1, 0)) return 0; n     if (solve(a, m, n, m - 1, n - 1)) return 0; n   } n   return puts( -1 ); n } n


#include <bits/stdc++.h> n int main() { n   int i, j; n   int str[6]; n   int num[6]; n   char ch; n   for (i = 0; i < 6; i++) { n     str[i] = 0; n     num[i] = 0; n   } n   for (i = 0; i < 6; i++) { n     scanf( %c , &ch); n     switch (ch) { n       case  R : n         str[0]++; n         break; n       case  O : n         str[1]++; n         break; n       case  Y : n         str[2]++; n         break; n       case  G : n         str[3]++; n         break; n       case  B : n         str[4]++; n         break; n       case  V : n         str[5]++; n         break; n     } n   } n   for (j = 0; j < 6; j++) { n     num[str[j]]++; n   } n   switch (num[0]) { n     case 0: n       printf( 30 n ); n       break; n     case 1: n       printf( 15 n ); n       break; n     case 2: n       switch (num[1]) { n         case 2: n           printf( 8 n ); n           break; n         case 3: n           printf( 5 n ); n           break; n       } n       break; n     case 3: n       switch (num[1]) { n         case 2: n           printf( 2 n ); n           break; n         case 1: n           printf( 3 n ); n           break; n         case 0: n           printf( 6 n ); n           break; n       } n       break; n     case 4: n       switch (num[1]) { n         case 1: n           printf( 1 n ); n           break; n         case 0: n           printf( 2 n ); n           break; n       } n       break; n     case 5: n       printf( 1 n ); n       break; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int Head[1000010], Next[1000010], Go[1000010], Val[1000010], iscut[1000010], n     Len[1000010], Dep[1000010], Fa[400010][21], Cnt = 1, n, m, q; n int Head2[1000010], Next2[1000010], Go2[1000010], Val2[1000010], Cnt2 = 1; n int DFN[1000010], MIN[1000010], ins[1000010], Bel[1000010], vis[1000010], n     tim = 0, tt = 0; n vector<int> S; n void addedge(int x, int y) { n   Go[++Cnt] = y; n   Next[Cnt] = Head[x]; n   Head[x] = Cnt; n } n void addedge2(int x, int y, int v) { n   Go2[++Cnt2] = y; n   Next2[Cnt2] = Head2[x]; n   Head2[x] = Cnt2; n   Val2[Cnt2] = v; n } n void Tarjan(int x, int f = 0) { n   ins[x] = 1; n   S.push_back(x); n   DFN[x] = ++tim; n   MIN[x] = DFN[x]; n   int las = x; n   for (int T = Head[x]; T; T = Next[T]) n     if (Go[T] != f) { n       if (!DFN[Go[T]]) { n         Tarjan(Go[T], x); n         MIN[x] = min(MIN[x], MIN[Go[T]]); n         if (DFN[x] <= MIN[Go[T]]) { n           tt++; n           for (;;) { n             if (S.back() == las) break; n             addedge2(tt + n + 1, S.back(), 1); n             addedge2(S.back(), tt + n + 1, 1); n             S.pop_back(); n           } n           addedge2(x, tt + n + 1, 1); n           addedge2(tt + n + 1, x, 1); n         } else n           las = S.back(); n       } else if (ins[Go[T]]) n         MIN[x] = min(MIN[x], DFN[Go[T]]); n     } n   ins[x] = 0; n } n void DFS2(int x, int dep = 1, int len = 0) { n   vis[x] = 1; n   Len[x] = len; n   Dep[x] = dep; n   for (int T = Head[x]; T; T = Next[T]) n     if (!vis[Go[T]]) { n       Fa[Go[T]][0] = x; n       DFS2(Go[T], dep + 1, len + Val[T]); n     } n } n int lca(int x, int y) { n   if (Dep[x] < Dep[y]) swap(x, y); n   int det = Dep[x] - Dep[y]; n   for (int i = 18; i >= 0; i--) n     if ((det >> i) & 1) x = Fa[x][i]; n   if (x == y) return x; n   for (int i = 18; i >= 0; i--) n     if (Fa[x][i] != Fa[y][i]) x = Fa[x][i], y = Fa[y][i]; n   return Fa[x][0]; n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     addedge(a, b); n     addedge(b, a); n   } n   for (int i = 1; i <= n; i++) n     if (!DFN[i]) Tarjan(i); n   memcpy(Head, Head2, sizeof Head); n   memcpy(Next, Next2, sizeof Next); n   memcpy(Go, Go2, sizeof Go); n   memcpy(Val, Val2, sizeof Val); n   for (int i = 1; i <= n + tt + 1; i++) n     if (!vis[i]) DFS2(i); n   for (int i = 1; i <= 20; i++) n     for (int j = 1; j <= n + tt + 1; j++) Fa[j][i] = Fa[Fa[j][i - 1]][i - 1]; n   for (int i = 1; i <= q; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     printf( %d n , (Len[a] + Len[b] - 2 * Len[lca(a, b)]) / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char ch = getchar(); n   int x = 0, f = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) -  0  + ch; n     ch = getchar(); n   } n   return x * f; n } n int main() { n   priority_queue<int> q; n   int n = read(), T = read(); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     int x = read(); n     x = max(x, i); n     while (!q.empty() && q.top() >= T - i) { n       q.pop(); n     } n     if (x < T) q.push(x - i); n     ans = max(ans, (int)q.size()); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int a[1005]; n int b[1005]; n int h; n bool cmp(int x, int y) { return x > y; } n bool check(int x) { n   for (int i = 1; i <= x; i++) { n     b[i] = a[i]; n   } n   sort(b + 1, b + x + 1, cmp); n   long long sum = 0; n   for (int i = 1; i <= x; i += 2) { n     sum += (long long)(b[i]); n   } n   if (sum <= h) { n     return 1; n   } else { n     return 0; n   } n } n int main() { n   int n; n   cin >> n >> h; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   int l = 1, r = n, mid; n   while (l < r) { n     mid = (l + r + 1) / 2; n     if (check(mid)) { n       l = mid; n     } else { n       r = mid - 1; n     } n   } n   cout << l; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int x; n   scanf( %d , &x); n   return x; n } n template <typename T> n inline istream& operator>>(istream& i, vector<T>& v) { n   for (int j = 0; j < ((int)(v).size()); ++j) i >> v[j]; n   return i; n } n template <typename T> n string join(const vector<T>& v) { n   stringstream s; n   for (int i = 0; i < ((int)(v).size()); ++i) s <<     << v[i]; n   return s.str().substr(1); n } n template <typename T> n inline ostream& operator<<(ostream& o, const vector<T>& v) { n   if ((int)(v).size()) o << join(v); n   return o; n } n template <typename T1, typename T2> n inline istream& operator>>(istream& i, pair<T1, T2>& v) { n   return i >> v.first >> v.second; n } n template <typename T1, typename T2> n inline ostream& operator<<(ostream& o, const pair<T1, T2>& v) { n   return o << v.first <<  ,  << v.second; n } n template <typename T> n inline long long int suma(const vector<T>& a) { n   long long int res(0); n   for (auto&& x : a) res += x; n   return res; n } n const double eps = 1e-10; n const long long int LINF = 1001002003004005006ll; n const int INF = 1001001001; n const int MX = 200005; n int n, m; n int dp[55][55][55]; n struct Q { n   int l, r, x, c; n   int cost(int i, int h) { n     if (l <= i && i < r && h > x) return c; n     return 0; n   } n }; n vector<Q> q; n int dfs(int l, int r, int h) { n   if (l == r) return 0; n   int& now = dp[l][r][h]; n   if (now != -1) return now; n   now = 0; n   if (h) now = dfs(l, r, h - 1); n   for (int i = l; i < r; ++i) { n     int c = h * h; n     for (int k = 0; k < (m); ++k) { n       if (l <= q[k].l && q[k].r <= r) c -= q[k].cost(i, h); n     } n     c += dfs(l, i, h) + dfs(i + 1, r, h); n     (now = max(now, c)); n   } n   return now; n } n int main() { n   int h; n   cin >> n >> h >> m; n   q = vector<Q>(m); n   for (int i = 0; i < (m); ++i) { n     int l, r, x, c; n     scanf( %d%d%d%d , &l, &r, &x, &c); n     --l; n     q[i] = (Q){l, r, x, c}; n   } n   for (int i = 0; i < (n); ++i) n     for (int j = 0; j < (n + 1); ++j) n       for (int k = 0; k < (h + 1); ++k) dp[i][j][k] = -1; n   int ans = dfs(0, n, h); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int x, y; n } p[8]; n int cmp(node a, node b) { n   if (a.x == b.x) return (a.y < b.y); n   return a.x < b.x; n } n int main() { n   while (scanf( %d %d , &p[0].x, &p[0].y) != EOF) { n     int cnt1 = 0; n     int cnt2 = 0; n     int cnt3 = 0; n     int cnt4 = 0; n     for (int i = 1; i < 8; i++) { n       scanf( %d %d , &p[i].x, &p[i].y); n     } n     sort(p, p + 8, cmp); n     node zuoshang = p[0]; n     node youxia = p[7]; n     for (int i = 1; i < 7; i++) { n       if (p[i].x == zuoshang.x) cnt1++; n       if (p[i].y == zuoshang.y) cnt2++; n       if (p[i].x == youxia.x) cnt3++; n       if (p[i].y == youxia.y) cnt4++; n     } n     if (cnt1 == 2 && cnt2 == 2 && cnt3 == 2 && cnt4 == 2 && p[1].y == p[6].y && n         p[4].x == p[3].x) n       puts( respectable ); n     else n       puts( ugly ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     double x, y, z, a, b, c, sq; n     cin >> x; n     a = 1.0, b = x, c = x; n     z = (b * b) - (4 * a * c); n     if (z < 0) { n       cout <<  N  << endl; n       continue; n     } n     y = ((-b + (sqrt((b * b) - (4 * a * c)))) / 2 * a); n     if (y != 0) y = -1 * y; n     cout << fixed << setprecision(9) <<  Y   << (x - y) <<     << y << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e3 + 789; n const int M = 2e5 + 5; n const int NN = 1e5 + 55; n const long long mod = 1e9 + 7; n const long long inf = 0x3f3f3f3f; n const long long INF = 1LL << 62; n bitset<2000> P[N], S[N], T; n char ms[N][N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   P[0].reset(); n   for (int i = 0; i < n; i++) { n     cin >> ms[i]; n     if (i) P[i] = P[i - 1]; n     for (int j = 0; j < m; j++) { n       if (ms[i][j] ==  1 ) P[i][j] = 1; n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     if (i != n - 1) S[i] = S[i + 1]; n     for (int j = 0; j < m; j++) { n       if (ms[i][j] ==  1 ) S[i][j] = 1; n     } n   } n   bool f = 0; n   for (int i = 0; i < n; i++) { n     T.reset(); n     if (i - 1 >= 0) T |= P[i - 1]; n     if (i + 1 < n) T |= S[i + 1]; n     if (T.count() == m) f = 1; n   } n   if (f) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline void Boost() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n const int NMax = 1e6 + 50; n string convert(const string &s) { n   string ret =  @ ; n   for (int i = 0; i < (int)s.size(); ++i) { n     ret +=  #  + s.substr(i, 1); n   } n   ret +=  #$ ; n   return ret; n } n int P[NMax * 2]; n void Solve() { n   string s; n   cin >> s; n   int n = (int)s.size(); n   int a = 0; n   int b = n - 1; n   while (a <= b) { n     if (a == b) { n       cout << s <<   n ; n       return; n     } n     if (s[a] == s[b]) { n       ++a; n       --b; n       if (a > b) { n         cout << s <<   n ; n         return; n       } n     } else { n       string nw = convert(s); n       int cen = 0, r = 0; n       for (int i = 0; i < (int)nw.size(); ++i) P[i] = 0; n       for (int i = 1; i < (int)nw.size() - 1; ++i) { n         int mir = cen - (i - cen); n         if (r > i) { n           P[i] = min(r - i, P[mir]); n         } n         while (nw[i + 1 + P[i]] == nw[i - 1 - P[i]]) { n           P[i]++; n         } n         if (i + P[i] > r) { n           cen = i; n           r = i + P[i]; n         } n       } n       int best = 0; n       int pos = 0; n       for (int i = 1; i < (int)nw.size() - 1; i++) { n         int start = (i - 1 - P[i]) / 2; n         int fin = start + P[i] - 1; n         if (start < a) { n           int mv = a - start; n           start += mv; n           fin -= mv; n         } n         if (fin > b) { n           int mv = fin - b; n           start += mv; n           fin -= mv; n         } n         if (start != a && fin != b) continue; n         if (start <= fin && (fin - start + 1) > best) { n           best = fin - start + 1; n           pos = start; n         } n       } n       for (int i = 0; i < a; ++i) cout << s[i]; n       for (int i = pos; i < pos + best; ++i) cout << s[i]; n       for (int i = a - 1; i >= 0; --i) cout << s[i]; n       cout <<   n ; n       return; n     } n   } n } n int main() { n   Boost(); n   int t; n   cin >> t; n   while (t--) { n     Solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool is_prefix(string s, string comp) { n   bool res = false; n   int sz = s.size(); n   int comp_sz = comp.size(); n   if (sz > comp_sz) n     return res; n   else { n     res = true; n     for (int i = 0; i < sz; i++) { n       if (s[i] != comp[i]) { n         res = false; n         break; n       } n     } n   } n   return res; n } n int main(int argc, char* argv[]) { n   int n; n   string s; n   vector<string> res; n   cin >> s; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string t; n     cin >> t; n     if (is_prefix(s, t)) res.push_back(t); n   } n   if (res.size() == 0) { n     cout << s; n   } else { n     sort(res.begin(), res.end()); n     cout << res[0]; n   } n   return 0; n } n


#include <bits/stdc++.h> n int du[100010], fa[100010], s1[100010], s2[100010]; n int find(int x) { n   if (x != fa[x]) fa[x] = find(fa[x]); n   return fa[x]; n } n int main() { n   int n, m, x, y, i, j, k, sum; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; i++) fa[i] = i; n   memset(du, 0, sizeof(du)); n   memset(s1, 0, sizeof(s1)); n   memset(s2, 0, sizeof(s2)); n   for (i = 1; i <= m; i++) { n     scanf( %d%d , &x, &y); n     j = find(x); n     k = find(y); n     du[x]++; n     du[y]++; n     if (j != k) { n       if (j < k) n         fa[k] = j; n       else n         fa[j] = k; n     } n   } n   for (i = 1; i <= n; i++) { n     j = find(i); n     s1[j] = s1[j] + du[i]; n     s2[j]++; n   } n   sum = 0; n   for (i = 1; i <= n; i++) n     if (s2[i] != 0) { n       j = s1[i] / 2; n       if (j < s2[i]) sum = sum + s2[i] - j; n     } n   printf( %d n , sum); n } n


#include <bits/stdc++.h> n const int maxn = 100001; n int n, val[maxn], cL[maxn], cR[maxn], ord[maxn], tot, in[maxn], out[maxn], n     bit[maxn], ans; n bool vis[maxn]; n void dfs(int u) { n   if (u == -1) return; n   in[u] = ++tot; n   dfs(cL[u]); n   dfs(cR[u]); n   out[u] = tot; n } n void bit_add(int x, int v) { n   for (; x <= n; x += x & -x) bit[x] += v; n } n int bit_sum(int x) { n   int ret = 0; n   for (; x > 0; x -= x & -x) ret += bit[x]; n   return ret; n } n bool cmp(int const &x, int const &y) { return val[x] < val[y]; } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d%d%d , val + i, cL + i, cR + i); n     if (cL[i] != -1) vis[cL[i]] = 1; n     if (cR[i] != -1) vis[cR[i]] = 1; n     ord[i] = i; n   } n   std::sort(ord + 1, ord + n + 1, cmp); n   for (int i = 1; i <= n; ++i) n     if (!vis[i]) dfs(i); n   for (int i = 1; i <= n; ++i) n     if (cR[i] != -1) { n       bit_add(in[cR[i]], 1); n       bit_add(out[cR[i]] + 1, -1); n     } n   for (int i = 1, j = 1; i <= n;) { n     for (; j <= n && val[ord[j]] <= val[ord[i]]; ++j) { n       int u = ord[j]; n       if (cL[u] != -1) { n         bit_add(in[cL[u]], 1); n         bit_add(out[cL[u]] + 1, -1); n       } n       if (cR[u] != -1) { n         bit_add(in[cR[u]], -1); n         bit_add(out[cR[u]] + 1, 1); n       } n     } n     int cnt = 0, flg = 0; n     for (int v = val[ord[i]]; i <= n && val[ord[i]] == v; ++i, ++cnt) n       flg |= !bit_sum(in[ord[i]]); n     if (!flg) ans += cnt; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 210; n int n, m, x; n long long dp[maxn][maxn], v[maxn], ans = -1; n void read_and_parse() { n   scanf( %d%d%d , &n, &m, &x); n   for (int i = 1; i <= n; i++) scanf( %d , &v[i]); n } n void solve() { n   memset(dp, 0xcf, sizeof(dp)); n   dp[0][0] = 0; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= x; j++) n       for (int k = max(0, i - m); k < i; k++) n         dp[i][j] = max(dp[i][j], dp[k][j - 1] + v[i]); n   for (int i = n - m + 1; i <= n; i++) ans = max(ans, dp[i][x]); n   printf( %lld n , ans); n } n int main() { n   read_and_parse(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void checkDefine(); n string x; n string y; n string z; n int main() { n   cin >> x >> y; n   long long int n = x.size(); n   for (int i = (0), _b = (n - 1); i <= _b; i++) { n     if (x[i] < y[i]) { n       cout << -1; n       return 0; n     } n   } n   cout << y; n   return 0; n } n void checkDefine() { n   long long int n, a[200005]; n   map<long long int, long long int> m; n   cin >> n; n   for (int i = (0), _b = (n - 1); i <= _b; i++) { n     cin >> a[i]; n     m[a[i]]++; n   } n   string s; n   cin >> s; n   { n     cout <<  s  n          <<   =  ; n     cout << (s) << endl; n   }; n   { n     cout <<  a  n          <<   =  ; n     for (int _ = 0, _a = (n); _ < _a; _++) cout << a[_] <<    ; n     cout << endl; n   }; n   { n     cout <<   ------------   n          <<   =  ; n     cout << ( ------------ ) << endl; n   }; n   for (__typeof(m.begin()) it = m.begin(); it != m.end(); ++it) { n     cout << it->first <<     << it->second << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class L, class R> n ostream &operator<<(ostream &os, map<L, R> P) { n   for (auto const &vv : P) os <<  (  << vv.first <<  ,  << vv.second <<  ) ; n   return os; n } n template <class T> n ostream &operator<<(ostream &os, set<T> V) { n   os <<  [ ; n   for (auto const &vv : V) os << vv <<  , ; n   os <<  ] ; n   return os; n } n template <class T> n ostream &operator<<(ostream &os, vector<T> V) { n   os <<  [ ; n   for (auto const &vv : V) os << vv <<  , ; n   os <<  ] ; n   return os; n } n template <class L, class R> n ostream &operator<<(ostream &os, pair<L, R> P) { n   os <<  (  << P.first <<  ,  << P.second <<  ) ; n   return os; n } n inline int fstoi(const string &str) { n   auto it = str.begin(); n   bool neg = 0; n   int num = 0; n   if (*it ==  - ) n     neg = 1; n   else n     num = *it -  0 ; n   ++it; n   while (it < str.end()) num = num * 10 + (*it++ -  0 ); n   if (neg) num *= -1; n   return num; n } n inline void getch(char &x) { n   while (x = getchar_unlocked(), x < 33) { n     ; n   } n } n inline void getstr(string &str) { n   str.clear(); n   char cur; n   while (cur = getchar_unlocked(), cur < 33) { n     ; n   } n   while (cur > 32) { n     str += cur; n     cur = getchar_unlocked(); n   } n } n template <typename T> n inline bool sc(T &num) { n   bool neg = 0; n   int c; n   num = 0; n   while (c = getchar_unlocked(), c < 33) { n     if (c == EOF) return false; n   } n   if (c ==  - ) { n     neg = 1; n     c = getchar_unlocked(); n   } n   for (; c > 47; c = getchar_unlocked()) num = num * 10 + c - 48; n   if (neg) num *= -1; n   return true; n } n template <typename T, typename... Args> n inline void sc(T &num, Args &...args) { n   bool neg = 0; n   int c; n   num = 0; n   while (c = getchar_unlocked(), c < 33) { n     ; n   } n   if (c ==  - ) { n     neg = 1; n     c = getchar_unlocked(); n   } n   for (; c > 47; c = getchar_unlocked()) num = num * 10 + c - 48; n   if (neg) num *= -1; n   sc(args...); n } n int getsum(int a, int b) { n   int oor, oand; n   cout <<  and   << a <<     << b << endl; n   cin >> oand; n   cout <<  or   << a <<     << b << endl; n   cin >> oor; n   return oor + oand; n } n int main() { n   int n, k; n   cin >> n >> k; n   int cr; n   int x, y, z; n   x = getsum(1, 2); n   y = getsum(1, 3); n   z = getsum(2, 3); n   int a[n + 1]; n   a[3] = (y + z - x) / 2; n   a[2] = (-y + z + x) / 2; n   a[1] = (y - z + x) / 2; n   for (auto i = (4); i <= (n); ++i) { n     int wh = getsum(1, i); n     a[i] = wh - a[1]; n   } n   sort(a + 1, a + n + 1); n   cout <<  finish   << a[k] << endl; n   cout << a[k] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3fll; n const long double pi = acos(-1); n const int MOD = 1e9 + 7; n const int N = 2e5 + 100; n int tr_min[4 * N][1 << 5]; n int tr_max[4 * N][1 << 5]; n vector<int> cara[N]; n int n, k; n void build(int node, int b, int e) { n   if (b == e) { n     for (int cord = 0; cord < (1 << k); cord++) { n       long long x = 0; n       for (int i = 0; i < k; i++) { n         if (cord & (1 << i)) n           x += cara[b][i]; n         else n           x -= cara[b][i]; n       } n       tr_max[node][cord] = tr_min[node][cord] = x; n     } n   } else { n     int mid = (b + e) / 2; n     build(2 * node, b, mid); n     build(2 * node + 1, mid + 1, e); n     for (int i = 0; i < (1 << k); i++) { n       tr_max[node][i] = max(tr_max[2 * node][i], tr_max[2 * node + 1][i]); n       tr_min[node][i] = min(tr_min[2 * node][i], tr_min[2 * node + 1][i]); n     } n   } n } n void update(int node, int b, int e, int i, vector<int> &cara) { n   if (e < i or b > i) return; n   if (b == e and b == i) { n     for (int cord = 0; cord < (1 << k); cord++) { n       long long x = 0; n       for (int i = 0; i < k; i++) { n         if (cord & (1 << i)) n           x += cara[i]; n         else n           x -= cara[i]; n       } n       tr_max[node][cord] = tr_min[node][cord] = x; n     } n     return; n   } n   int mid = (b + e) / 2; n   update(2 * node, b, mid, i, cara); n   update(2 * node + 1, mid + 1, e, i, cara); n   for (int i = 0; i < (1 << k); i++) { n     tr_max[node][i] = max(tr_max[2 * node][i], tr_max[2 * node + 1][i]); n     tr_min[node][i] = min(tr_min[2 * node][i], tr_min[2 * node + 1][i]); n   } n } n int query(int node, int b, int e, int i, int j, int cord) { n   if (e < i or b > j) return -INF; n   if (i <= b and j >= e) { n     return tr_max[node][cord]; n   } n   int mid = (b + e) / 2; n   return max(query(2 * node, b, mid, i, j, cord), n              query(2 * node + 1, mid + 1, e, i, j, cord)); n } n int query2(int node, int b, int e, int i, int j, int cord) { n   if (e < i or b > j) return INF; n   if (i <= b and j >= e) { n     return tr_min[node][cord]; n   } n   int mid = (b + e) / 2; n   return min(query2(2 * node, b, mid, i, j, cord), n              query2(2 * node + 1, mid + 1, e, i, j, cord)); n } n int proc(int cord, int l, int r) { n   int maxi = query(1, 1, n, l, r, cord); n   int mini = query2(1, 1, n, l, r, cord); n   return abs(mini - maxi); n } n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < k; j++) { n       int x; n       scanf( %d , &x); n       cara[i].push_back(x); n     } n   } n   int q; n   build(1, 1, n); n   scanf( %d , &q); n   while (q--) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       int idx; n       scanf( %d , &idx); n       vector<int> val; n       for (int j = 0; j < k; j++) { n         int x; n         scanf( %d , &x); n         val.push_back(x); n       } n       update(1, 1, n, idx, val); n     } else { n       int l, r; n       scanf( %d %d , &l, &r); n       int resp = 0; n       for (int i = 0; i < (1 << k); i++) resp = max(proc(i, l, r), resp); n       printf( %d n , resp); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n bool operator<(pair<int, int> lhs, pair<int, int> rhs) { n   return (lhs.first != rhs.first) ? (lhs.first < rhs.first) n                                   : (lhs.second < rhs.second); n } n set<pair<int, int> > inqUp, inqDown, pendUp, pendDown; n int N, M, prsis, rci; n long long T, Ans[MAXN]; n struct User { n   int id, ar, f, t; n }; n bool cmp_Useraraz(User lhs, User rhs) { return lhs.ar < rhs.ar; } n User u[MAXN], ru[MAXN]; n int Where; n void Unload() { n   while ((inqUp.size()) && (inqUp.begin()->first == Where)) { n     Ans[inqUp.begin()->second] = T; n     inqUp.erase(inqUp.begin()); n     rci++; n   } n   while (((inqDown.size()) && (--inqDown.end())->first == Where)) { n     Ans[(--inqDown.end())->second] = T; n     inqDown.erase(--inqDown.end()); n     rci++; n   } n } n void Request() { n   while ((prsis != N) && (u[prsis + 1].ar == T)) { n     prsis++; n     if (u[prsis].f >= Where) n       pendUp.insert(make_pair(u[prsis].f, u[prsis].id)); n     else n       pendDown.insert(make_pair(u[prsis].f, u[prsis].id)); n   } n } n void Load(int id) { n   if (ru[id].t >= Where) n     inqUp.insert(make_pair(ru[id].t, id)); n   else n     inqDown.insert(make_pair(ru[id].t, id)); n } n void Load() { n   while ((pendUp.size()) && (pendUp.begin()->first == Where)) { n     Load(pendUp.begin()->second); n     pendUp.erase(pendUp.begin()); n   } n   while ((pendDown.size()) && ((--pendDown.end())->first == Where)) { n     Load((--pendDown.end())->second); n     pendDown.erase(--pendDown.end()); n   } n } n int main() { n   scanf( %d%d , &N, &M); n   int i; n   for (i = 1; i <= N; i++) { n     scanf( %d%d%d , &u[i].ar, &u[i].f, &u[i].t); n     u[i].id = i; n     ru[i] = u[i]; n   } n   sort(u + 1, u + N + 1, cmp_Useraraz); n   inqUp.clear(); n   inqDown.clear(); n   pendUp.clear(); n   pendDown.clear(); n   Where = 1; n   T = 0; n   prsis = rci = 0; n   while (rci < N) { n     Unload(); n     Request(); n     Load(); n     int pUp = inqUp.size() + pendUp.size(); n     int pDown = inqDown.size() + pendDown.size(); n     int Action = (pUp + pDown) ? ((pUp >= pDown) ? 1 : -1) : 0; n     int timePass = (prsis == N) ? M : (u[prsis + 1].ar - T); n     if (Action == 1) { n       if (pendUp.size()) n         timePass = min(timePass, pendUp.begin()->first - Where); n       if (inqUp.size()) timePass = min(timePass, inqUp.begin()->first - Where); n     } n     if (Action == -1) { n       if (pendDown.size()) n         timePass = min(timePass, Where - (--pendDown.end())->first); n       if (inqDown.size()) n         timePass = min(timePass, Where - (--inqDown.end())->first); n     } n     T += timePass; n     Where += timePass * Action; n   } n   for (i = 1; i <= N; i++) printf( %I64d n , Ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n map<char, char> mp; n string check(string s) { n   for (int i = 0; i <= s.size() / 2; i++) n     if (s[i] != mp[s[s.size() - 1 - i]]) return  NIE ; n   return  TAK ; n } n int main() { n   mp[ A ] =  A ; n   mp[ b ] =  d ; n   mp[ d ] =  b ; n   mp[ H ] =  H ; n   mp[ I ] =  I ; n   mp[ M ] =  M ; n   mp[ O ] =  O ; n   mp[ o ] =  o ; n   mp[ p ] =  q ; n   mp[ q ] =  p ; n   mp[ T ] =  T ; n   mp[ U ] =  U ; n   mp[ V ] =  V ; n   mp[ v ] =  v ; n   mp[ W ] =  W ; n   mp[ w ] =  w ; n   mp[ X ] =  X ; n   mp[ x ] =  x ; n   mp[ Y ] =  Y ; n   string s; n   cin >> s; n   cout << check(s) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200000; n const long long mod = 1e9 + 7; n long long fac[maxn + 5], inv[maxn + 5]; n long long f[2005]; n struct node { n   long long x, y; n } data[2005]; n long long qmod(long long a, long long b) { n   long long ans = 1; n   a = a % mod; n   while (b) { n     if (b & 1) { n       ans = (ans * a) % mod; n     } n     b = b / 2; n     a = (a * a) % mod; n   } n   return ans; n } n void init() { n   fac[0] = 1; n   for (long long i = 1; i <= maxn; i++) { n     fac[i] = (fac[i - 1] * i) % mod; n   } n } n long long com(long long n, long long m) { n   if (n < m) n     return 0; n   else if (n == m) n     return 1; n   else n     return (fac[n] * qmod(fac[m] * fac[n - m], mod - 2)) % mod; n } n long long Lucas(long long n, long long m) { n   if (m == 0) n     return 1; n   else n     return (com(n % mod, m % mod) * Lucas(n / mod, m / mod)) % mod; n } n bool cmp(node a, node b) { n   if (a.x < b.x) n     return true; n   else if (a.x == b.x) { n     if (a.y < b.y) n       return true; n     else n       return false; n   } else n     return false; n } n int main(int argc, char const *argv[]) { n   long long h, w, n; n   init(); n   scanf( %lld %lld %lld , &h, &w, &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld %lld , &data[i].x, &data[i].y); n   } n   sort(data + 1, data + 1 + n, cmp); n   memset(f, 0, sizeof(f)); n   n++; n   data[n].x = h; n   data[n].y = w; n   for (int i = 1; i <= n; i++) { n     long long dx, dy; n     dx = data[i].x - 1; n     dy = data[i].y - 1; n     f[i] = Lucas(dx + dy, dx); n     for (int j = 1; j < i; j++) { n       if (data[i].x >= data[j].x && data[i].y >= data[j].y) { n         dx = data[i].x - data[j].x; n         dy = data[i].y - data[j].y; n         f[i] = f[i] - (f[j] * Lucas(dx + dy, dx)) % mod; n         f[i] = (f[i] + mod) % mod; n       } n     } n   } n   cout << (f[n] + mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x[100], s[100]; n struct par { n   int v, id; n   bool operator<(const par &r) const { return v < r.v; } n }; n vector<par> v; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &x[i]); n     v.push_back({x[i], i}); n   } n   sort(v.begin(), v.end()); n   vector<par> v1 = v; n   rotate(v1.begin(), v1.begin() + 1, v1.end()); n   for (int i = 0; i < (int)v.size(); i++) s[v[i].id] = v1[i].v; n   for (int i = 0; i < n; i++) { n     printf( %d , s[i]); n     i == n - 1 ? printf(  n ) : printf(   ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 3e5 + 7; n unordered_map<int, vector<int>> values[2]; n int t, n, q, a[oo], p[oo]; n vector<int> ans; n int psum(int l, int r) { return p[r] - (l ? p[l - 1] : 0); } n int first(vector<int>& list, int val) { n   int l = 0, r = list.size(); n   while (l < r) { n     int s = (l + r) / 2; n     if (list[s] >= val) n       r = s; n     else n       l = s + 1; n   } n   if (l == list.size()) return n * 2; n   return list[l]; n } n void problem(int l, int r) { n   int sum = psum(l, r); n   if (sum == 0) return; n   if (sum % 2 == 0) { n     ans.push_back(r); n     problem(l, r - 1); n     return; n   } n   int pl = p[l] - a[l]; n   int vals[2] = {pl + (sum + 1) / 2 - 1, pl + (sum - 1) / 2 + 1}; n   for (int i = 0; i < 2; i++) { n     int pans = first(values[i][vals[i]], l); n     if (pans <= r) { n       ans.push_back(pans); n       return; n     } n   } n   throw 69; n } n void solve() { n   cin >> n >> q; n   string ss; n   cin >> ss; n   for (int i = 0; i < n; i++) n     a[i] = (ss[i] ==  +  ? 1 : -1) * (i % 2 == 0 ? -1 : 1); n   p[0] = a[0]; n   for (int i = 1; i < n; i++) p[i] = p[i - 1] + a[i]; n   for (int i = 0; i < n; i++) values[(a[i] + 1) / 2][p[i]].push_back(i); n   while (q--) { n     int l, r; n     cin >> l >> r; n     l--, r--; n     problem(l, r); n     cout << ans.size() << endl; n     for (int e : ans) cout << e + 1 <<    ; n     cout << endl; n     ans.clear(); n   } n   values[0].clear(); n   values[1].clear(); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int a[1050], b[1050]; n int main() { n   int n, m, s = 0, d = 0; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   for (int i = 1; i <= n; i++) { n     sort(a, a + m); n     for (int j = 0; j < m; j++) { n       if (a[j] > 0) { n         s += a[j]; n         a[j]--; n         break; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     sort(b, b + m); n     for (int j = m - 1; j >= 0; j--) { n       if (b[j] > 0) { n         d += b[j]; n         b[j]--; n         break; n       } n     } n   } n   cout << d <<     << s; n   return 0; n } n