#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T sqr(const T& x) { n   return x * x; n } n template <class T> n inline string tostr(const T& a) { n   ostringstream os(  ); n   os << a; n   return os.str(); n } n const int inf = 1999999999; n const double pi = acos(-1.0); n const double eps = 1e-9; n int main(int argc, char* argv[]) { n   clock_t _ = clock(); n   cout << fixed << setprecision(15); n   string s, t; n   cin >> s >> t; n   bool automaton = false, array = true; n   int cnt1[26], cnt2[26]; n   memset((cnt1), (0), sizeof(cnt1)); n   memset((cnt2), (0), sizeof(cnt2)); n   for (__typeof((s).begin()) it = (s).begin(), _it = (s).end(); it != _it; n        ++it) { n     cnt1[*it -  a ]++; n   } n   for (__typeof((t).begin()) it = (t).begin(), _it = (t).end(); it != _it; n        ++it) { n     cnt2[*it -  a ]++; n   } n   if (s.size() > t.size()) automaton = true; n   for (int i = 0, _i = (26); i < _i; ++i) { n     if (cnt1[i] < cnt2[i]) { n       automaton = array = false; n       break; n     } n   } n   int i2 = 0; n   for (int i1 = 0, _i1 = (s.size()); i1 < _i1; ++i1) { n     if (s[i1] == t[i2]) i2++; n   } n   if (i2 == t.size()) { n     automaton = true; n     array = false; n   } n   if (automaton && array) n     cout <<  both  << (  n ); n   else if (automaton) n     cout <<  automaton  << (  n ); n   else if (array) n     cout <<  array  << (  n ); n   else n     cout <<  need tree  << (  n ); n   return 0; n } n


#include <bits/stdc++.h> n long long n, ans; n int main() { n   scanf( %lld , &n); n   for (long long i = 2; i / 2 <= n; i *= 2) { n     ans += (n + i / 2 - 1) / i * (i / 2); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long k; n   cin >> k; n   getchar(); n   char s[1000001]; n   cin >> s; n   int a[strlen(s)]; n   long long sum = 0; n   for (int i = 0; i < strlen(s); i++) { n     sum += s[i] - 48; n     a[i] = s[i] - 48; n   } n   k = k - sum; n   sort(a, a + strlen(s)); n   int c = 0; n   for (int i = 0; i < strlen(s); i++) { n     if (k > 0) { n       k -= (9 - a[i]); n       c++; n     } else n       break; n   } n   cout << c << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   long long l, a; n   cin >> n >> l >> a; n   long long arr[n][2]; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i][0] >> arr[i][1]; n     if (i == 0) { n       ans += arr[i][0] / a; n     } n     if (i != 0) { n       ans = ans + (arr[i][0] - (arr[i - 1][0] + arr[i - 1][1])) / a; n     } n   } n   if (n != 0) n     ans = ans + (l - arr[n - 1][0] - arr[n - 1][1]) / a; n   else n     ans = l / a; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bitset<50000000> arr; n int primes[100100], pr = 0; n int n; n unsigned int A, B, C, D; n int main() { n   cin >> n >> A >> B >> C >> D; n   for (int i = 2; i <= 18000; i++) { n     arr[i] = 1; n   } n   for (int i = 2; i <= 18000; i++) { n     if (arr[i] == 0) continue; n     primes[pr++] = i; n     for (int j = i * i; j <= 18000; j += i) { n       arr[j] = 0; n     } n   } n   int st = 2; n   unsigned int sol = 0; n   while (st <= n) { n     int en = min(st + 40000000, n); n     int ln = en - st + 1; n     for (int i = 0; i < ln; i++) { n       arr[i] = 1; n     } n     for (int i = 0; i < pr; i++) { n       int beg = max(primes[i] * primes[i], n                     ((st + primes[i] - 1) / primes[i]) * primes[i]); n       for (int j = beg - st; j < ln; j += primes[i]) { n         arr[j] = 0; n       } n     } n     for (int i = 0; i < ln; i++) { n       if (arr[i] == 1) { n         unsigned int rep = 0; n         int cur_pr = st + i; n         while (cur_pr <= n) { n           rep += (unsigned int)(n / cur_pr); n           if (cur_pr > n / (st + i)) { n             break; n           } else { n             cur_pr *= (st + i); n           } n         } n         sol += rep * (A * (st + i) * (st + i) * (st + i) + n                       B * (st + i) * (st + i) + C * (st + i) + D); n       } n     } n     st = en + 1; n   } n   cout << sol << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int l; n   int r; n } a[200005], b[2000005]; n int cmpl(struct node n, struct node m) { return n.l < m.l; } n int cmpr(struct node n, struct node m) { return n.r < m.r; } n int main() { n   int n, m; n   while (~scanf( %d , &n)) { n     int sum = 0; n     for (int i = 0; i < n; i++) { n       scanf( %d%d , &a[i].l, &a[i].r); n     } n     scanf( %d , &m); n     for (int i = 0; i < m; i++) { n       scanf( %d%d , &b[i].l, &b[i].r); n     } n     sort(a, a + n, cmpl); n     sort(b, b + m, cmpr); n     if (a[n - 1].l > b[0].r) { n       sum = max(sum, a[n - 1].l - b[0].r); n     } n     sort(a, a + n, cmpr); n     sort(b, b + m, cmpl); n     if (b[m - 1].l > a[0].r) { n       sum = max(sum, b[m - 1].l - a[0].r); n     } n     if (!sum) n       printf( 0 n ); n     else n       printf( %d n , sum); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const string FILENAME =   ; n const bool FILE_IO = 1; n const int INF = 1000000000; n void init() { n   if (!FILENAME.empty()) { n     freopen((FILENAME +  .in ).c_str(),  r , stdin); n     freopen((FILENAME +  .out ).c_str(),  w , stdout); n   } n } n int main() { n   init(); n   int n, m; n   cin >> n; n   vector<int> arr(n); n   vector<long long> s(n); n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   cin >> m; n   sort(arr.begin(), arr.end()); n   reverse(arr.begin(), arr.end()); n   s[0] = arr[0]; n   for (int i = 1; i < n; i++) { n     s[i] += arr[i] + s[i - 1]; n   } n   for (int i = 0; i < m; i++) { n     int q; n     long long ans = 0; n     cin >> q; n     ans = s[q - 2] + s[n - 1] - s[q - 1]; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005, M = 100005; n inline int read() { n   int x = 0; n   char ch = getchar(); n   bool positive = 1; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) positive = 0; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return positive ? x : -x; n } n inline void write(long long a) { n   if (a >= 10) write(a / 10); n   putchar( 0  + a % 10); n } n inline void writeln(long long a) { n   if (a < 0) { n     a = -a; n     putchar( - ); n   } n   write(a); n   puts(  ); n } n int sum, b[N], k, n, len, m, i, ll, rr, qian[N], hou[N]; n long long tong[N], a[N], c[N], kind, an[N]; n map<long long, int> Ma; n struct data { n   int l, r, id, num; n } q[M]; n inline void del1(int p) { n   tong[a[p]]--; n   if (hou[a[p]]) kind -= tong[hou[a[p]]]; n } n inline void del2(int p) { n   tong[a[p]]--; n   if (qian[a[p]]) kind -= tong[qian[a[p]]]; n } n inline void ins1(int p) { n   if (hou[a[p]]) kind += tong[hou[a[p]]]; n   tong[a[p]]++; n } n inline void ins2(int p) { n   if (qian[a[p]]) kind += tong[qian[a[p]]]; n   tong[a[p]]++; n } n inline bool cmp(data a, data b) { n   return a.id < b.id || (a.id == b.id && a.r < b.r); n } n int main() { n   n = read(); n   k = read(); n   len = sqrt(n); n   for (i = 1; i <= n; i++) b[i] = read(); n   for (i = 1; i <= n; i++) n     c[i] = a[i] = read() * (b[i] == 1 ? 1 : -1) + a[i - 1]; n   sort(&c[1], &c[n + 1]); n   c[0] = c[1] - 1; n   for (i = 1; i <= n; i++) { n     if (c[i] != c[i - 1]) { n       Ma[c[i]] = ++sum; n     } n   } n   for (i = 1; i <= n; i++) { n     qian[Ma[c[i]]] = Ma[c[i] - k]; n     hou[Ma[c[i]]] = Ma[c[i] + k]; n     a[i] = Ma[a[i]]; n   } n   hou[0] = Ma[k]; n   m = read(); n   for (i = 1; i <= m; i++) { n     q[i].l = read(); n     q[i].r = read(); n     q[i].id = q[i].l / len; n     q[i].num = i; n   } n   sort(&q[1], &q[m + 1], cmp); n   ll = q[1].l; n   rr = q[1].l - 1; n   for (i = 1; i <= m; i++) { n     while (ll > q[i].l) ins1(--ll); n     while (rr < q[i].r) ins2(++rr); n     while (ll < q[i].l) del1(ll++); n     while (rr > q[i].r) del2(rr--); n     an[q[i].num] = kind + tong[hou[a[ll - 1]]]; n   } n   for (i = 1; i <= m; i++) writeln(an[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long int MOD; n long long int mod(long long int a) { n   a %= MOD; n   if (a < 0) a += MOD; n   return a; n } n struct matrix { n   int n, m; n   long long int a[6][6]; n   matrix() {} n   matrix(int n_, int m_) { n     n = n_, m = m_; n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) a[i][j] = 0; n   } n   matrix operator*(const matrix &t) { n     int p = t.m; n     matrix res(n, p); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < p; j++) { n         for (int k = 0; k < m; k++) { n           long long int v = mod(1ll * a[i][k] * t.a[k][j]); n           res.a[i][j] = mod(res.a[i][j] + v); n         } n       } n     } n     return res; n   } n   void print() { n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) printf( %d  , a[i][j]); n       printf(  n ); n     } n     printf(  n ); n   } n }; n matrix fpow(matrix a, long long int n) { n   matrix ans(6, 6); n   for (int i = 0; i < 6; i++) ans.a[i][i] = 1; n   while (n > 0) { n     if (n & 1) ans = ans * a; n     a = a * a; n     n >>= 1; n   } n   return ans; n } n int ar[6][6] = {{2, 1, 1, 0, 1, 0}, {1, 2, 0, 1, 1, 0}, {1, 1, 1, 0, 1, 0}, n                 {1, 1, 0, 1, 1, 0}, {0, 0, 0, 0, 1, 1}, {0, 0, 0, 0, 0, 1}}; n int main() { n   int x, y, dx, dy; n   long long int t; n   scanf( %d%d%d%d%d%lld , &MOD, &x, &y, &dx, &dy, &t); n   matrix m(6, 6), v(6, 1); n   for (int i = 0; i < 6; i++) { n     for (int j = 0; j < 6; j++) m.a[i][j] = ar[i][j]; n   } n   v.a[0][0] = x, v.a[1][0] = y, v.a[2][0] = dx, v.a[3][0] = dy, v.a[5][0] = 1; n   matrix sol = fpow(m, t - 1) * v; n   long long int sum = mod(sol.a[0][0] + sol.a[1][0]); n   sum = mod(sum + t - 1); n   dx = mod(sol.a[2][0] + sum); n   dy = mod(sol.a[3][0] + sum); n   x = mod(sol.a[0][0] + dx - 1) + 1; n   y = mod(sol.a[1][0] + dy - 1) + 1; n   printf( %d %d , x, y); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char pal[111]; n int nn, L; n int dp[101][55][2]; n bool calc[101][55][2]; n pair<int, int> pai[101][55][2]; n int go(int ini, int n, bool pos) { n   int r1; n   if (ini == L) { n     pai[ini][n][pos] = make_pair(n, pos); n     return (n % 2 != 0) * (-0x3f3f3f3f); n   } n   if (!calc[ini][n][pos]) { n     calc[ini][n][pos] = true; n     int resp = 0; n     if (pal[ini] ==  F ) { n       resp = go(ini + 1, n, pos) + pos - (!pos); n       if (n) n         r1 = go(ini + 1, n - 1, !pos); n       else n         r1 = -0x3f3f3f3f; n       if (resp > r1) { n         pai[ini][n][pos] = make_pair(n, pos); n       } else { n         resp = r1; n         pai[ini][n][pos] = make_pair(n - 1, !pos); n       } n     } else { n       resp = go(ini + 1, n, !pos); n       if (n) n         r1 = go(ini + 1, n - 1, pos) + pos - (!pos); n       else n         r1 = -0x3f3f3f3f; n       if (resp > r1) { n         pai[ini][n][pos] = make_pair(n, !pos); n       } else { n         resp = r1; n         pai[ini][n][pos] = make_pair(n - 1, pos); n       } n     } n     dp[ini][n][pos] = resp; n   } n   return dp[ini][n][pos]; n } n void psol(int ind, int n, bool opa) { n   if (ind == L) return; n   if (n == pai[ind][n][opa].first) n     cout << pal[ind]; n   else if (pal[ind] ==  T ) n     cout <<  F ; n   else n     cout <<  T ; n   psol(ind + 1, pai[ind][n][opa].first, pai[ind][n][opa].second); n } n int main() { n   while (scanf( %s %d , pal, &nn) == 2) { n     L = strlen(pal); n     memset(calc, false, sizeof(calc)); n     int r1 = go(0, nn, true); n     int r2 = go(0, nn, false); n     printf( %d n , max(r2, r1)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 1e9 + 9; n const long long inf = 1e18 + 18; n const int max6 = 1e6 + 6; n const int modx = 1e9 + 7; n const int mody = 997; n const int base = 137; n int n, m; n int cnt[max6], a[max6], tmp[max6]; n int f[3][1005][1005]; n int b[3][1005][1005]; n int Pow(int a, int b) { n   if (b == 0) return 1; n   int half = Pow(a, b / 2); n   int res = 1ll * half * half % m; n   if (b % 2) res = 1ll * res * a % m; n   return res; n } n void update(int id, int j, int u) { n   if (id == 1) { n     for (; u > 0; u -= u & (-u)) b[id][j][u]++; n   } else { n     for (; u <= n; u += u & (-u)) b[id][j][u]++; n   } n } n int get(int id, int j, int u) { n   int res = 0; n   if (id == 1) { n     for (; u <= n; u += u & (-u)) res = res + b[id][j][u]; n   } else { n     for (; u > 0; u -= u & (-u)) res = res + b[id][j][u]; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= n; ++i) { n     int u = a[i] % m; n     if (cnt[u]) { n       cout << 0; n       return 0; n     } n     cnt[u]++; n   } n   int res = 1; n   for (int i = 1; i <= n; ++i) n     for (int j = i + 1; j <= n; ++j) res = 1ll * res * abs(a[i] - a[j]) % m; n   cout << res; n } n


#include <bits/stdc++.h> n const int N = 1000005; n int fa[N], h[N], nxt[N], adj[N], a[N], u, v, t, id[N], Id, ans[N], f[N], n, tot, n     m; n inline int fd(register int x) { return fa[x] == x ? x : fa[x] = fd(fa[x]); } n inline void add() { n   nxt[++t] = h[u], h[u] = t, adj[t] = v, id[t] = Id, nxt[++t] = h[v], h[v] = t, n   adj[t] = u, id[t] = Id; n } n inline void mer() { n   register int t1 = fd(u), t2 = fd(v); n   if (t1 != t2) fa[t1] = t2, add(); n } n inline void dfs(register int x) { n   for (register int i = h[x], j; i; i = nxt[i]) n     if ((j = adj[i]) != f[x]) n       f[j] = x, dfs(j), a[x] += a[j], ans[id[i]] = (i & 1 ? a[j] : -a[j]); n } n inline void fr(int &num) { n   num = 0; n   char c = getchar(); n   int p = 1; n   while (c <  0  || c >  9 ) c ==  -  ? p = -1, c = getchar() : c = getchar(); n   while (c >=  0  && c <=  9 ) num = num * 10 + c -  0 , c = getchar(); n   num *= p; n } n int main() { n   fr(n); n   for (register int i = 1; i <= n; ++i) fr(a[i]), tot += a[i], fa[i] = i; n   if (tot) return puts( Impossible ), 0; n   puts( Possible ); n   fr(m); n   for (Id = 1; Id <= m; ++Id) fr(u), fr(v), mer(); n   dfs(1); n   for (register int i = 1; i <= m; ++i) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a; n string s; n int main() { n   cin >> a >> s; n   sort(s.rbegin(), s.rend()); n   for (int i = 0, j = 0; i < a.size(); i++) { n     int ins = s[j] -  0 ; n     int ina = a[i] -  0 ; n     if (ina < ins && j < s.size()) { n       a[i] = s[j++]; n     } n   } n   cout << a << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void ga(int N, int *A) { n   for (int i(0); i < N; i++) scanf( %d , A + i); n } n struct lt { n   char c; n   long long T; n   bool operator<(const lt &r) const { return c != r.c ? T < r.T : c < r.c; } n   bool operator==(const lt &r) const { return c == r.c && T == r.T; } n   bool operator!=(const lt &r) const { return c != r.c || T != r.T; } n } T[(1 << 20)], P[(1 << 20)], I[(1 << 20)]; n long long S; n void preKMP(lt *p, int *f) { n   *f = -1; n   for (int i(1), k; p[i + 1].c; ++i) { n     k = f[i - 1]; n     while (~k && p[k] != p[i - 1]) k = f[k]; n     f[i] = k + 1; n   } n } n int N, M, n, m, a; n int sm(int b) { n   if (!b) return 0; n   if (T[b - 1].c != P[0].c || T[b - 1].T < P[0].T) return 0; n   if (T[b + m - 2].c != P[m - 1].c || T[b + m - 2].T < P[m - 1].T) return 0; n   return 1; n } n void KMP(lt *p, lt *t, int m) { n   static int f[(1 << 20)]; n   preKMP(p, f); n   for (int i(0), k(0); t[i].c;) n     if (!~k) n       ++i, k = 0; n     else if (t[i] == p[k]) { n       ++i; n       if (++k == m) S += sm(i - k), k = f[k - 1], --i; n     } else n       k = f[k]; n } n char c; n int main(void) { n   scanf( %d%d , &N, &M); n   for (int i(0); i < N; i++) { n     scanf( %d-%c , &a, &c); n     if (i && T[n - 1].c == c) n       T[n - 1].T += a; n     else n       T[n++] = {c, a}; n   } n   T[n].c = 0; n   for (int i(0); i < M; i++) { n     scanf( %d-%c , &a, &c); n     if (i && P[m - 1].c == c) n       P[m - 1].T += a; n     else n       P[m++] = {c, a}; n   } n   P[m].c = 0; n   if (m == 1) { n     for (int i(0); i < n; i++) n       if (T[i].c == P[0].c) S += max(0ll, T[i].T - P[0].T + 1); n   } else if (m == 2) n     for (int i(0); i < n - 1; i++) n       S += T[i].c == P[0].c && T[i].T >= P[0].T && T[i + 1].c == P[1].c && n            T[i + 1].T >= P[1].T; n   else if (m == 3) n     for (int i(0); i < n - 1; i++) n       S += T[i + 1] == P[1] && T[i].c == P[0].c && T[i].T >= P[0].T && n            T[i + 2].c == P[2].c && T[i + 2].T >= P[2].T; n   else n     KMP(P + 1, T, m - 2); n   printf( %lld n , S); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x; n   cin >> x; n   if (!x) n     cout << 0 << endl; n   else { n     if (x < 0) x = -1 * x; n     long long ans = 1, temp = 0; n     while (temp < x) { n       temp = temp + ans; n       ans++; n     } n     ans--; n     long long res = ans * (ans + 1) / 2 - x; n     if (!res) n       cout << ans << endl; n     else if (res % 2) { n       if (ans % 2) ans++; n       cout << ans + 1 << endl; n     } else n       cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma warning(disable : 4996) n using namespace std; n const long long INF = 2147483647; n const double PI = acos(-1); n const int mod = 1e9 + 7; n char arr[121212]; n int main() { n   long long t; n   cin >> t; n   for (int j = 1; j <= t; j++) { n     long long n; n     cin >> n; n     cin >> (arr + 1); n     long long ans = 0; n     bool sign = false; n     while (sign == false) { n       sign = true; n       for (int i = 1; i <= n - 1; i++) { n         if (arr[i] ==  A  && arr[i + 1] ==  P ) { n           arr[i + 1] =  A ; n           sign = false; n           i = i + 1; n         } n       } n       if (!sign) ans++; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7, seed = 131, MAXN = 0; n struct Item { n   string s; n   int x; n }; n int main() { n   cin.tie(0); n   cin.sync_with_stdio(0); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     vector<string> arr(n); n     for (int i = 0; i < n; ++i) cin >> arr[i]; n     queue<Item> q; n     q.push({  , 0}); n     string str; n     int curr, uwu; n     for (int idx = 0; idx < m; ++idx) { n       int qsize = q.size(); n       for (int _ = 0; _ < qsize; ++_) { n         str = q.front().s; n         curr = q.front().x; n         q.pop(); n         for (char c =  a ; c <=  z ; ++c) { n           uwu = 0; n           for (int i = 0; i < n; ++i) n             if (arr[i][idx] != c) uwu |= (1 << i); n           if ((uwu & curr) == 0) q.push({str + c, uwu | curr}); n         } n       } n     } n     if (q.empty()) n       cout << -1 <<   n ; n     else n       cout << q.front().s <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000005; n int n, a[N], b[N], lst[N], pre[N], suf[N], pr[N], sf[N], cn[N], sta[N], top, n     ans; n long long s[N][4], tag[N][4]; n inline long long S2(const int x) { return (x + 1ll) * x >> 1; } n inline long long S(const int l, const int r) { return S2(r) - S2(l - 1); } n inline void pd(const int rt, const int l, const int r, const int x, n                const int i) { n   s[rt][2] = s[rt][i ^ 1] * x, s[rt][i] = (r - l + 1ll) * x, tag[rt][i] = x, n   !i ? s[rt][3] = S(l, r) * x : 0; n } n inline void pd(const int rt, const int l, const int r) { n   const int mid = l + r >> 1, ls = rt << 1, rs = ls | 1; n   tag[rt][0] ? pd(ls, l, mid, tag[rt][0], 0), pd(rs, mid + 1, r, tag[rt][0], 0), n       tag[rt][0] = 0 : 0; n   tag[rt][1] ? pd(ls, l, mid, tag[rt][1], 1), pd(rs, mid + 1, r, tag[rt][1], 1), n       tag[rt][1] = 0 : 0; n } n inline void upd(const int rt, const int l, const int r, const int u, n                 const int v, const int x, const int i) { n   if (l >= u && r <= v) return pd(rt, l, r, x, i); n   const int mid = l + r >> 1, ls = rt << 1, rs = ls | 1; n   pd(rt, l, r); n   u <= mid ? upd(ls, l, mid, u, v, x, i), 0 : 0, n       v > mid ? upd(rs, mid + 1, r, u, v, x, i), 0 : 0; n   s[rt][0] = s[ls][0] + s[rs][0], s[rt][1] = s[ls][1] + s[rs][1], n   s[rt][2] = s[ls][2] + s[rs][2], s[rt][3] = s[ls][3] + s[rs][3]; n } n inline long long que(const int rt, const int l, const int r, const int u, n                      const int v, const int i) { n   if (u > v) return 0; n   if (l >= u && r <= v) return s[rt][i]; n   const int mid = l + r >> 1, ls = rt << 1, rs = ls | 1; n   pd(rt, l, r); n   if (v <= mid) return que(ls, l, mid, u, v, i); n   if (u > mid) return que(rs, mid + 1, r, u, v, i); n   return que(ls, l, mid, u, mid, i) + que(rs, mid + 1, r, mid + 1, v, i); n } n int main() { n   scanf( %d , &n); n   register int i, j; n   for (i = 1; i <= n; ++i) scanf( %d , &a[i]), b[i] = a[i]; n   std::sort(b + 1, b + 1 + n), *b = std::unique(b + 1, b + 1 + n) - b - 1; n   for (i = 1; i <= n; ++i) n     a[i] = std::lower_bound(b + 1, b + 1 + *b, a[i]) - b, n     pre[i] = lst[a[i]] + 1, lst[a[i]] = i; n   for (i = 1; i <= *b; ++i) lst[i] = n + 1; n   for (i = n; i; --i) suf[i] = lst[a[i]] - 1, lst[a[i]] = i; n   for (i = n, top = 0; i; --i) { n     while (top && pre[i] > pre[sta[top]]) --top; n     pr[i] = top ? sta[top] - 1 : n, sta[++top] = i; n   } n   for (i = n, top = 0; i; --i) { n     while (top && suf[i] < suf[sta[top]]) --top; n     sf[i] = top ? sta[top] - 1 : n, sta[++top] = i; n   } n   for (i = n, j = n; i; --i) { n     upd(1, 1, n, i, pr[i], pre[i], 0), upd(1, 1, n, i, sf[i], suf[i], 1), n         ++cn[a[i]]; n     while (cn[a[i]] > 1) --cn[a[j]], --j; n     ans = (ans + que(1, 1, n, i, j, 3) + que(1, 1, n, i, j, 1) * i - n            S(i, j) * i - que(1, 1, n, i, j, 2)) % n           1000000007; n   } n   printf( %d , ans < 0 ? ans + 1000000007 : ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 2; n void testCase() { n   int n, k, cnt = 0, ans = 1, s = 0; n   scanf( %d%d , &n, &k); n   vector<int> a(n), fr(101), z(101); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n     if (fr[a[i]]++ == 0) ++cnt, ++s; n     if (cnt == k + 1) cnt = 2, ans++; n   } n   if (s >= 2 && k == 1) { n     puts( -1 ); n     return; n   } n   cout << ans << endl; n } n int main() { n   int T = 1; n   scanf( %d , &T); n   while (T--) { n     testCase(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int maxn = 1e5 + 10; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   int ans = 0; n   vector<int> b(n); n   for (int k = 0; k < 26; ++k) { n     for (int i = 0, mod = (1 << (k + 1)); i < n; ++i) b[i] = a[i] % mod; n     sort(b.begin(), b.end()); n     long long cnt = 0; n     int l1 = (1 << k), r1 = (1 << (k + 1)) - 1; n     int l2 = (1 << (k + 1)) + (1 << k), r2 = (1 << (k + 2)) - 2; n     for (int i = 0; i < n; ++i) { n       int l = l1 - b[i], r = r1 - b[i]; n       cnt += upper_bound(b.begin(), b.begin() + i, r) - n              lower_bound(b.begin(), b.begin() + i, l); n     } n     for (int i = 0; i < n; ++i) { n       int l = l2 - b[i], r = r2 - b[i]; n       cnt += upper_bound(b.begin(), b.begin() + i, r) - n              lower_bound(b.begin(), b.begin() + i, l); n     } n     if (cnt & 1) ans += (1 << k); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, i, k, j, c; n   while (scanf( %d%d , &n, &m) != EOF) { n     c = 0; n     char a[990][101], x; n     for (i = 0; i < n; i++) scanf( %s , a[i]); n     for (i = 0; i < n; i++) { n       for (k = 0; k < m; k++) { n         x = a[i][k]; n         for (j = 0; j < n; j++) { n           if (x >= a[j][k]) n             continue; n           else n             break; n         } n         if (j == n) { n           c = c + 1; n           break; n         } n       } n     } n     printf( %d n , c); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int s[100010]; n int main() { n   long long n, x; n   cin >> n >> x; n   long long sum = 0, ans = 0; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   sort(s, s + n); n   int j = 0; n   while (j < n) { n     if (x < 1) x = 1; n     ans += s[j] * x; n     x--; n     j++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 3e5; n int n; n pair<long long, long long> a[MAXN + 5]; n priority_queue<long long, vector<long long>, greater<long long> > q; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     long long t, w; n     cin >> t >> w; n     a[i] = make_pair(t, w); n   } n   sort(a + 2, a + n + 1); n   int ans = n, pos = n; n   for (;;) { n     for (; pos > 1 && a[pos].first > a[1].first; --pos) n       q.push(a[pos].second - a[pos].first + 1); n     ans = min(ans, (int)q.size() + 1); n     if (!q.empty() && a[1].first >= q.top()) { n       a[1].first -= q.top(); n       q.pop(); n     } else n       break; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline bool chmax(T& a, T b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <typename T> n inline bool chmin(T& a, T b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n const double EPS = 1e-10; n const double PI = acos(-1); n class EDividePoints { n  public: n   int N; n   void rec(std::ostream& out, vector<pair<int, int>>& vec) { n     vector<vector<int>> par(2); n     for (int i = (0); i < (N); ++i) n       par[((vec[i].first + vec[i].second) % 2 + 2) % 2].push_back(i + 1); n     if (not par[0].empty() and (int)(par[0]).size() < N) { n       out << (int)(par[0]).size() <<   n ; n       for (auto u : par[0]) out << u <<    ; n       out <<   n ; n       return; n     } n     vector<vector<vector<int>>> ids(2, vector<vector<int>>(2)); n     for (int i = (0); i < (N); ++i) { n       ids[(vec[i].first % 2 + 2) % 2][(vec[i].second % 2 + 2) % 2].push_back(i + n                                                                              1); n     } n     for (int i = (0); i < (2); ++i) n       for (int j = (0); j < (2); ++j) n         if (not ids[i][j].empty() and (int)(ids[i][j]).size() < N) { n           out << (int)(ids[i][j]).size() <<   n ; n           for (auto u : ids[i][j]) out << u <<    ; n           out <<   n ; n           return; n         } n     for (int i = (0); i < (N); ++i) { n       if (vec[i].first % 2) vec[i].first--; n       if (vec[i].second % 2) vec[i].second--; n       vec[i].first /= 2, vec[i].second /= 2; n     } n     rec(out, vec); n   } n   void solve(std::istream& in, std::ostream& out) { n     ios_base::sync_with_stdio(false); n     in.tie(nullptr), out.tie(nullptr); n     in >> N; n     vector<pair<int, int>> A(N); n     for (int i = (0); i < (N); ++i) in >> A[i].first >> A[i].second; n     rec(out, A); n   } n }; n int main() { n   EDividePoints solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read() { n   register T sum = 0; n   register char cc = getchar(); n   int sym = 1; n   while (cc !=  -  && (cc >  9  || cc <  0 )) cc = getchar(); n   if (cc ==  - ) sym = -1, cc = getchar(); n   sum = sum * 10 + cc -  0 ; n   cc = getchar(); n   while (cc >=  0  && cc <=  9 ) sum = sum * 10 + cc -  0 , cc = getchar(); n   return sym * sum; n } n template <typename T> n inline T read(T &a) { n   a = read<T>(); n   return a; n } n template <typename T, typename... Others> n inline void read(T &a, Others &...b) { n   a = read(a); n   read(b...); n } n struct Data { n   int x, y, p; n } a[200010]; n int n, l[200010], r[200010], t[200010]; n long long ans1, ans2; n int lowbit(int x) { return x & -x; } n void change(int x, int y) { n   for (int i = x; i <= n << 1; i += lowbit(i)) t[i] += y; n } n int query(int x) { n   int res = 0; n   for (int i = x; i; i -= lowbit(i)) res += t[i]; n   return res; n } n bool cmp1(const Data &a, const Data &b) { return a.x < b.x; } n bool cmp2(const Data &a, const Data &b) { return a.y > b.y; } n int main() { n   read(n); n   for (int i = 1; i <= n; i++) { n     read(a[i].x, a[i].y); n     if (a[i].x > a[i].y) swap(a[i].x, a[i].y); n     a[i].p = i; n   } n   sort(a + 1, a + 1 + n, cmp1); n   for (int i = 1; i <= n; i++) { n     l[a[i].p] += query(a[i].x) + query(n << 1) - query(a[i].y); n     change(a[i].y, 1); n   } n   memset(t + 1, 0, sizeof(int) * (n << 1)); n   for (int i = n; i >= 1; i--) { n     r[a[i].p] = query(a[i].y); n     change(a[i].y, 1); n   } n   memset(t + 1, 0, sizeof(int) * (n << 1)); n   sort(a + 1, a + 1 + n, cmp2); n   for (int i = 1; i <= n; i++) { n     l[a[i].p] += query(n << 1) - query(a[i].y); n     change(a[i].x, 1); n   } n   ans1 = 1ll * n * (n - 1) * (n - 2) / 6; n   for (int i = 1; i <= n; i++) { n     ans1 -= 1ll * l[i] * r[i]; n     ans2 += 1ll * (l[i] + r[i]) * (n - l[i] - r[i] - 1); n   } n   printf( %lld n , ans1 - ans2 / 2); n   return 0; n } n


#include <bits/stdc++.h> n using ii = std::pair<int, int>; n bool visited[50001]; n int nexts[50001]; n int value[50001]; n int n, start, x; n void input() { n   for (int i = 1; i <= n; i++) { n     scanf( %d %d , &value[i], &nexts[i]); n   } n } n ii query(int pos) { n   visited[pos] = true; n   printf( ? %d n , pos); n   fflush(stdout); n   ii res; n   scanf( %d %d , &res.first, &res.second); n   return res; n } n int main() { n   srand(time(nullptr)); n   scanf( %d %d %d , &n, &start, &x); n   ii max = query(start); n   if (max.first >= x) { n     printf( ! %d n , max.first); n     fflush(stdout); n     return 0; n   } n   std::vector<int> pick; n   for (int i = 1; i <= n; i++) { n     if (i == start) continue; n     pick.push_back(i); n   } n   std::random_shuffle(pick.begin(), pick.end()); n   for (int i = 0; i < std::min(n - 1, 1000); i++) { n     auto q = query(pick[i]); n     if (q.first < x && q.first > max.first) max = q; n   } n   if (max.second == -1) { n     printf( ! -1 n ); n     fflush(stdout); n     return 0; n   } n   for (int i = 0; i < 1000; i++) { n     auto q = query(max.second); n     if (q.first >= x) { n       printf( ! %d n , q.first); n       fflush(stdout); n       return 0; n     } else if (q.second == -1) { n       printf( ! -1 n ); n       fflush(stdout); n       return 0; n     } else { n       max = q; n     } n   } n   while (true) n     ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, s1, s2; n   cin >> s; n   s1 = s; n   sort(s1.begin(), s1.end()); n   s2 += s1[0]; n   for (int i = 1; i < s1.length(); i++) { n     if (s1[i] != s1[i - 1]) s2 += s1[i]; n   } n   int d = 0; n   for (int j = s2.length() - 1; j >= 0; j--) { n     for (int i = d; i < s.length(); i++) { n       if (s2[j] == s[i]) cout << s[i], d = i + 1; n     } n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3,  Ofast ,  inline ) n using namespace std; n mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); n const long long maxn = (long long)1e6 + 5; n const long long mod = 998244353; n const long long inf = 0x3f3f3f3f; n long long T = 1; n vector<long long> v[maxn], vec[maxn]; n long long dep[maxn], pre[maxn]; n long long maxx; n void dfs(long long now) { n   maxx = max(maxx, dep[now]); n   vec[dep[now]].push_back(now); n   for (auto &to : v[now]) { n     if (dep[to]) continue; n     pre[to] = now; n     dep[to] = dep[now] + 1; n     dfs(to); n   } n } n void solve() { n   long long n, m; n   cin >> n >> m; n   maxx = 1; n   for (long long i = 1; i <= n; ++i) v[i].clear(), vec[i].clear(), dep[i] = 0; n   for (long long i = 1; i <= m; ++i) { n     long long x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dep[1] = 1; n   dfs(1); n   if (maxx > (n + 1) / 2) { n     cout <<  PATH n ; n     stack<long long> s; n     long long now = vec[maxx][0]; n     while (now) { n       s.push(now); n       now = pre[now]; n     } n     cout << s.size() <<   n ; n     while (!s.empty()) { n       cout << s.top() <<    ; n       s.pop(); n     } n     cout <<   n ; n     return; n   } n   cout <<  PAIRING n ; n   long long ans = 0; n   for (long long i = 1; i <= maxx; ++i) { n     ans += vec[i].size() / 2; n   } n   cout << ans <<   n ; n   for (long long i = 1; i <= maxx; ++i) { n     for (long long j = 0; j + 1 < vec[i].size(); j += 2) { n       cout << vec[i][j] <<     << vec[i][j + 1] <<   n ; n     } n   } n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int int_inf = 0x7fffffff; n const long long ll_inf = ~(1ll << 63); n const double eps = 1e-7; n char *p1, *p2, buf[1 << 20]; n inline int read() { n   int f = 1, x = 0; n   char ch; n   while (!isdigit( n       ch = (p1 == p2 && n                     (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) n                 ? EOF n                 : *p1++))) n     if (ch ==  - ) f = -1; n   x = ch -  0 ; n   while (isdigit( n       ch = (p1 == p2 && n                     (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) n                 ? EOF n                 : *p1++))) n     x = x * 10 + ch -  0 ; n   return x * f; n } n const int N = 1e6 + 10; n unordered_map<long long, int> m; n long long sa[N], sb[N]; n int a[N], b[N], s[N], n; n int main() { n   n = read(); n   for (int i = 1; i <= n; i++) a[i] = read(), sa[i] = sa[i - 1] + a[i]; n   for (int i = 1; i <= n; i++) b[i] = read(), sb[i] = sb[i - 1] + b[i]; n   m[0] = 0; n   for (int i = 1; i <= n; i++) { n     int p = s[i - 1]; n     while (sb[p] <= sa[i] && p <= n) p++; n     s[i] = p - 1; n     long long delt = sa[i] - sb[p - 1]; n     if (m.count(delt)) { n       int x = m[delt]; n       cout << (i - x) << endl; n       for (int j = x + 1; j <= i; j++) cout << j <<    ; n       cout << endl; n       cout << (s[i] - s[x]) << endl; n       for (int j = s[x] + 1; j <= s[i]; j++) cout << j <<    ; n       return 0; n     } n     m[delt] = i; n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0; n   char ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^  0 ), ch = getchar(); n   return x; n } n const int N = 1e7 + 5; n const long long mod = 998244353; n int ksm(int a, int b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = 1ll * ans * a % mod; n     a = 1ll * a * a % mod; n     b >>= 1; n   } n   return ans; n } n int prime[N], kth[N], cnt; n bool is_not_prime[N]; n void sieve(int n, int k) { n   is_not_prime[0] = is_not_prime[1] = 1; n   kth[1] = 1; n   for (int i = 2; i <= n; i++) { n     if (!is_not_prime[i]) prime[++cnt] = i, kth[i] = ksm(i, k); n     for (int j = 1; j <= cnt && 1ll * prime[j] * i <= n; j++) { n       if (i % prime[j] == 0) break; n       prime[i * prime[j]] = 1; n       kth[i * prime[j]] = 1ll * kth[i] * kth[prime[j]] % mod; n     } n   } n } n int inv[N]; n void preinv(int n) { n   inv[1] = 1; n   for (int i = 2; i <= n; i++) n     inv[i] = 1ll * inv[mod % i] * (mod - mod / i) % mod; n } n long long n, m, k, p, q, ivq, ivp; n int S[N]; n int main() { n   n = read(), m = read(), k = read(); n   sieve(k, k); n   p = ksm(m, mod - 2), q = (1 - p + mod) % mod, ivq = ksm(q, mod - 2), n   ivp = ksm(p, mod - 2); n   preinv(k); n   if (m == 1) n     printf( %d n , ksm(n, k)); n   else if (n <= k) { n     long long ans = 0; n     for (int i = 0, xs1 = 1, xs2 = ksm(q, n), xs3 = 1, xs4 = 1; i <= n; i++, n              xs1 = 1ll * xs1 * p % mod, xs2 = 1ll * xs2 * ivq % mod, n              xs3 = 1ll * xs3 * (n - i + 1) % mod, n              xs4 = 1ll * xs4 * inv[i] % mod) n       ans = n           (ans + 1ll * xs3 * xs4 % mod * kth[i] % mod * xs1 % mod * xs2 % mod) % n           mod; n     cout << ans; n   } else { n     S[k] = 1; n     for (int i = k - 1, xs1 = mod - p, n              xs2 = 1ll * (n - i - 1) * inv[k - i] % mod; n          i >= 0; i--, xs1 = 1ll * xs1 * (mod - p) % mod, n              xs2 = 1ll * xs2 * (n - i - 1) % mod * inv[k - i] % mod) n       S[i] = n           (1ll * S[i + 1] * (1 - p + mod) % mod + 1ll * xs2 * xs1 % mod) % mod; n     long long ans = 0; n     for (int i = 0, xs1 = 1, xs2 = 1; i <= k; i++, n              xs1 = 1ll * xs1 * (n - i + 1) % mod * inv[i] % mod, n              xs2 = 1ll * xs2 * p % mod) n       ans = (ans + 1ll * kth[i] * xs1 % mod * S[i] % mod * xs2 % mod) % mod; n     cout << ans; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   set<char> ss; n   map<string, bool> mp; n   long long l = 0; n   for (long long i = 0; i < s.size(); i++) { n     if (!mp[s]) l++; n     mp[s] = true; n     rotate(s.begin(), s.end() - 1, s.end()); n   } n   cout << l << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1100000; n int p[MAXN], f[MAXN], g[MAXN]; n int main() { n   int n; n   scanf( %d , &n); n   f[1] = 1, g[1] = 0; n   for (int i = 2; i <= n + 1; i++) { n     scanf( %d , &p[i]); n     f[i] = 1, g[i] = 0; n     for (int v = i; v != 1; v = p[v]) { n       int tmp = f[p[v]]; n       if (f[v] > g[p[v]]) n         f[p[v]] = g[p[v]] = f[v]; n       else n         f[p[v]] = max(f[p[v]], f[v] + 1); n       if (f[p[v]] == tmp) break; n     } n     printf( %d  , g[1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, x; n   cin >> n >> x; n   string s; n   cin >> s; n   for (int j = 0; j < x; j++) { n     for (int i = 0; i < n - 1; i++) { n       if (s[i] ==  B  && s[i + 1] ==  G ) { n         s[i] =  G ; n         s[i + 1] =  B ; n         i++; n       } n     } n   } n   cout << s <<   n ; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n typedef long long ll; n typedef unsigned long long ull; n #define pii pair<int,int> n #define fi first n #define se second n #define mp make_pair n #define poly vector<ll> n #define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++) n #define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--) n #define pb push_back n inline ll read(){ n  ll x=0; char ch=getchar(); bool d=1; n  for(;!isdigit(ch);ch=getchar()) if(ch== - ) d=0; n  for(;isdigit(ch);ch=getchar()) x=x*10+ch- 0 ; n  return d?x:-x; n } n inline ull rnd(){ n  return ((ull)rand()<<30^rand())<<4|rand()%4; n } n const int N=1e5+5; n map<pii,bool> used; n map<int,int> id; n set<int> S; n poly v[N<<1],rv; n bool ans[N]; n int q[N]; n int main(){ n  int n=read(),m=read(),cnt=1; n  used[mp(0,0)]=1; id[0]=1; rv.pb(0); v[1].pb(0); n  For(i,1,n){ n   int x=read(),y=read(); n   if(!id[x]) id[x]=++cnt,rv.pb(x); n   used[mp(x,y)]=1; v[id[x]].pb(y); n  } n  For(i,1,m){ n   int x=read(),y=read(); q[i]=y; n   if(used[mp(x,y)]) continue; n   if(!id[x]) id[x]=++cnt,rv.pb(x); n   v[id[x]].pb(-i); n  } n  sort(rv.begin(),rv.end()); n  int pre=-1,ssw=0; n  for(auto o:rv){ n   for(int i=pre+1;i<o;){ n    while(S.find(ssw)!=S.end()) ssw++; n    auto ksg=S.upper_bound(ssw); n    if(ksg==S.end()){ n     ssw+=o-i; break; n    } n    int t=min(*ksg-ssw,(int)o-i); n    ssw+=t; i+=t; n   } n   int pos=id[o],mn=0x3f3f3f3f; n   sort(v[pos].begin(),v[pos].end()); n   for(auto x:v[pos]) if(x>=0){ n    mn=x; break; n   } n   while(S.find(ssw)!=S.end()) ssw++; n   if(ssw<mn) S.insert(ssw); mn=min(mn,ssw); n   for(auto x:v[pos]){ n    if(x>=0) S.insert(x); n    else ans[-x]=(q[-x]!=mn); n   } n   pre=o; n  } n  For(i,1,m) puts(ans[i]? WIN : LOSE ); n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int x = 0, y = 0; n   for (int i = 0; i < s.size(); i++) { n     x = x % 4; n     y = y % 4; n     if (s[i] ==  0 ) { n       cout << 3 <<     << y + 1 << endl; n       y++; n     } else { n       cout << 1 <<     << x + 1 << endl; n       x += 2; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   srand(time(NULL)); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     unordered_map<string, int> codes; n     vector<string> v(n); n     for (int i = 0; i < n; i++) { n       string x; n       cin >> x; n       v[i] = x; n       codes[x]++; n     } n     int ans = 0; n     vector<string> anss; n     for (int i = 0; i < n; i++) { n       if (codes[v[i]] > 1) { n         ans++; n         string x = v[i]; n         while (codes[x] >= 1) { n           x = v[i]; n           int pos = rand() % 4; n           char digit = (char)( 0  + (rand() % 10)); n           x[pos] = digit; n         } n         codes[x] = 1; n         codes[v[i]]--; n         anss.push_back(x); n       } else n         anss.push_back(v[i]); n     } n     cout << ans <<   n ; n     for (auto a : anss) cout << a <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double x, y, R, r, eps = 0.000001; n int a, b; n int main() { n   scanf( %lf , &r); n   for (a = 1; a < 11; a++) n     for (b = 1; b < 11; b++) { n       x = (double)a / 2; n       y = (double)b; n       R = sqrt(x * x + y * y); n       if (abs((double)a / 2 * (double)b / R - r) < eps) { n         printf( %d %d n , a, b); n         return 0; n       } n     } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, l, r, a[100100], L[100100], R[100100]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1; i <= n; ++i) n     if (a[i] > a[i - 1]) n       L[i] = i; n     else n       L[i] = L[i - 1]; n   for (int i = n; i >= 1; --i) n     if (a[i] > a[i + 1]) n       R[i] = i; n     else n       R[i] = R[i + 1]; n   while (m--) { n     scanf( %d%d , &l, &r); n     if (R[l] >= L[r]) n       puts( Yes ); n     else n       puts( No ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int ret = 0; n   char c = getchar(); n   while (c >  9  || c <  0 ) c = getchar(); n   while (c >=  0  && c <=  9 ) n     ret = (ret << 3) + (ret << 1) + (c ^ 48), c = getchar(); n   return ret; n } n const int maxn = 55; n const int maxk = 6; n const int maxm = 2500; n const int inf = 1e9; n int n, k; n int dlim[maxk]; n struct edge { n   int from, to, val; n } e1[15], e2[maxm]; n int cnt1, cnt2; n int ans; n int d[maxk]; n bool ini[maxm], x[maxm], y[maxm]; n int dis[maxm], pre[maxm]; n struct dsu { n   int fa[maxn]; n   void prework() { n     for (int i = 1; i <= n; i++) fa[i] = i; n   } n   int get(int x) { return x == fa[x] ? x : fa[x] = get(fa[x]); } n   void merge(int x, int y) { fa[get(x)] = get(y); } n   bool check(int x, int y) { return get(x) == get(y); } n } S; n queue<int> q; n bool inq[maxm]; n int ret = 0; n bool deb = 0; n struct graph { n   int head[maxm], ver[maxm * maxm], nxt[maxm * maxm], val[maxm * maxm], tot; n   void add(int x, int y, int z) { n     ver[++tot] = y; n     val[tot] = z; n     nxt[tot] = head[x]; n     head[x] = tot; n   } n   void clear() { n     tot = 0; n     for (int i = 1; i <= cnt2 + 2; i++) head[i] = 0; n   } n   int dis[maxm], pre[maxm], len[maxm]; n   bool inq[maxm]; n   void spfa() { n     for (int i = 1; i <= cnt2 + 2; i++) n       dis[i] = 0x3f3f3f3f, inq[i] = 0, pre[i] = 0; n     dis[cnt2 + 1] = 0; n     q.push(cnt2 + 1); n     inq[cnt2 + 1] = 1; n     while (!q.empty()) { n       int now = q.front(); n       q.pop(); n       inq[now] = 0; n       for (int i = head[now]; i; i = nxt[i]) n         if (dis[ver[i]] > dis[now] + val[i] || n             (dis[ver[i]] == dis[now] + val[i] && len[ver[i]] > len[now] + 1)) { n           dis[ver[i]] = dis[now] + val[i]; n           len[ver[i]] = len[now] + 1; n           pre[ver[i]] = now; n           if (!inq[ver[i]]) q.push(ver[i]); n           inq[ver[i]] = 1; n         } n     } n   } n   void update() { n     int now = cnt2 + 2; n     while (now) { n       ini[now] ^= 1; n       if (ini[now]) n         ret -= e2[now].val; n       else n         ret += e2[now].val; n       now = pre[now]; n     } n   } n } o; n bool calc(int chose) { n   for (int i = 1; i <= k; i++) d[i] = 0; n   S.prework(); n   for (int i = 1; i <= cnt1; i++) n     if ((chose >> (i - 1)) & 1) { n       d[e1[i].from]++, d[e1[i].to]++; n       S.merge(e1[i].from, e1[i].to); n     } n   for (int i = 1; i <= cnt2; i++) n     if (ini[i]) { n       if (e2[i].from <= k) d[e2[i].from]++; n       S.merge(e2[i].from, e2[i].to); n     } n   int s = cnt2 + 1, t = cnt2 + 2; n   o.clear(); n   for (int i = 1; i <= cnt2; i++) { n     if (ini[i]) n       x[i] = y[i] = 0; n     else { n       x[i] = (!S.check(e2[i].from, e2[i].to)); n       y[i] = (e2[i].from > k || d[e2[i].from] + 1 <= dlim[e2[i].from]); n       if (x[i]) o.add(s, i, e2[i].val); n       if (y[i]) o.add(i, t, 0); n     } n   } n   for (int i = 1; i <= cnt2; i++) { n     if (!ini[i]) continue; n     S.prework(); n     for (int j = 1; j <= cnt2; j++) n       if (ini[j] && j != i) S.merge(e2[j].from, e2[j].to); n     for (int i = 1; i <= cnt1; i++) n       if ((chose >> (i - 1)) & 1) S.merge(e1[i].from, e1[i].to); n     for (int j = 1; j <= cnt2; j++) { n       if (ini[j]) continue; n       if (!S.check(e2[j].from, e2[j].to)) o.add(i, j, e2[j].val); n       if (e2[j].from > k || n           d[e2[j].from] - (e2[j].from == e2[i].from) < dlim[e2[j].from]) n         o.add(j, i, -e2[i].val); n     } n   } n   o.spfa(); n   if (o.dis[t] == 0x3f3f3f3f) return false; n   o.update(); n   return true; n } n void solve(int chose) { n   for (int i = 1; i <= k; i++) d[i] = 0; n   S.prework(); n   ret = 0; n   for (int i = 1; i <= cnt1; i++) n     if ((chose >> (i - 1)) & 1) { n       d[e1[i].from]++; n       d[e1[i].to]++; n       ret += e1[i].val; n       if (d[e1[i].from] > dlim[e1[i].from]) return; n       if (d[e1[i].to] > dlim[e1[i].to]) return; n       if (S.check(e1[i].from, e1[i].to)) return; n       S.merge(e1[i].from, e1[i].to); n     } n   for (int i = 1; i <= cnt2; i++) ini[i] = 0; n   int cnt = 0; n   if (ret > ans) return; n   while (calc(chose)) n     if (ret > ans) return; n   ret = 0; n   bool flag = 1; n   S.prework(); n   for (int i = 1; i <= cnt1; i++) n     if ((chose >> (i - 1)) & 1) S.merge(e1[i].from, e1[i].to), ret += e1[i].val; n   for (int i = 1; i <= cnt2; i++) n     if (ini[i]) S.merge(e2[i].from, e2[i].to), ret += e2[i].val; n   for (int i = 1; i <= n; i++) n     if (!S.check(1, i)) return; n   ans = min(ret, ans); n } n int main() { n   n = read(); n   k = read(); n   for (int i = 1; i <= k; i++) dlim[i] = read(); n   for (int i = 1; i <= n - 1; i++) n     for (int j = 1; j <= n - i; j++) n       if (i <= k && i + j <= k) { n         cnt1++; n         e1[cnt1].from = i; n         e1[cnt1].to = i + j; n         e1[cnt1].val = read(); n       } else { n         cnt2++; n         e2[cnt2].from = i; n         e2[cnt2].to = i + j; n         e2[cnt2].val = read(); n       } n   ans = inf; n   for (int i = 0; i < (1 << cnt1); i++) solve(i); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a, b; n int ans = 0; n vector<int> A, B; n string cp(string s, int n) { n   string ans = s; n   for (int i = 1; i < n; i++) ans += s; n   return ans; n } n int main() { n   cin >> a >> b; n   for (int i = 1; i <= a.size(); i++) n     if (a.size() % i == 0 && cp(a.substr(0, i), a.size() / i) == a) n       A.push_back(i); n   for (int i = 1; i <= b.size(); i++) n     if (b.size() % i == 0 && cp(b.substr(0, i), b.size() / i) == b) n       B.push_back(i); n   for (int i = 0; i < A.size(); i++) n     for (int j = 0; j < B.size(); j++) n       if (A[i] == B[j] && a.substr(0, A[i]) == b.substr(0, B[j])) ans++; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n static const int MAXN = 5004; n static const int ALPHA = 26; n static const int MODULUS = 1e9 + 7; n int n; n char s[MAXN]; n int binom[MAXN][MAXN]; n int f[MAXN][ALPHA] = {{0}}, f_rowsum[MAXN] = {0}; n void preprocess_binomials() { n   binom[0][0] = 1; n   for (int i = 1; i < MAXN; ++i) { n     binom[i][0] = 1; n     for (int j = 1; j <= i; ++j) n       binom[i][j] = (binom[i - 1][j - 1] + binom[i - 1][j]) % MODULUS; n   } n } n int main() { n   preprocess_binomials(); n   scanf( %d , &n); n   getchar(); n   for (int i = 0; i < n; ++i) s[i] = getchar() -  a ; n   f_rowsum[0] = 1; n   for (int i = 0; i < n; ++i) { n     for (int len = i + 1; len >= 1; --len) { n       int increment = n           ((f_rowsum[len - 1] - f[len - 1][s[i]] - f[len][s[i]]) % MODULUS + n            MODULUS) % n           MODULUS; n       (f[len][s[i]] += increment) %= MODULUS; n       (f_rowsum[len] += increment) %= MODULUS; n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; ++i) n     for (int j = 0; j < ALPHA; ++j) n       ans = n           ((long long)ans + (long long)f[i][j] * binom[n - 1][i - 1]) % MODULUS; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n std::vector<long long> prime; n unsigned long long int power(unsigned long long int x, unsigned long long int y, n                              unsigned long long int mod) { n   unsigned long long int res = 1; n   while (y) { n     if (y & 1) res = res * x % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return res; n } n bool miller(unsigned long long int x, unsigned long long int d) { n   unsigned long long int a = rand() % (x - 1) + 2; n   unsigned long long int ad = power(a, d, x); n   if (ad == 1 || ad == x - 1) { n     return true; n   } n   while (d != x - 1) { n     ad = ad * ad % x; n     if (ad == 1) { n       return false; n     } n     if (ad == x - 1) { n       return true; n     } n     d *= 2; n   } n   return false; n } n bool primer(unsigned long long int x) { n   if (x == 2 || x == 3 || x == 5 || x == 7) return true; n   if (x % 2 == 0 || x == 1) return false; n   unsigned long long int d = x - 1; n   while (d % 2 == 0) d = d / 2; n   for (int i = 0; i < 7; ++i) { n     if (!miller(x, d)) { n       return false; n     } n   } n   return true; n } n bool isp(long long n) { n   if (n == 1) { n     return false; n   } n   for (int i = 2; i <= sqrt(n); i++) { n     if (!n % 2) { n       return false; n     } n   } n   return true; n } n int nearestprime(unsigned long long int x) { n   unsigned long long int i; n   for (i = x; !primer(i); i--) n     ; n   return i; n } n set<pair<int, int> > st; n int main() { n   ios::sync_with_stdio(false); n   int n, k, c, p; n   int tsize[1000 + 45]; n   pair<int, pair<int, int> > price[1000 + 45]; n   bool occ[1000 + 45]; n   for (int i = 0; i <= 1000; i++) { n     occ[i] = false; n   } n   vector<pair<int, int> > ans; n   pair<int, int> table[1000 + 45]; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> c >> p; n     price[i].first = p; n     price[i].second.first = INT_MAX - c; n     price[i].second.second = i; n   } n   cin >> k; n   for (int i = 0; i < k; i++) { n     cin >> table[i].first; n     table[i].second = i; n   } n   pair<int, int> *it; n   sort(price, price + n); n   sort(table, table + k); n   table[k].first = INT_MAX; n   table[k].second = INT_MAX; n   long long sum = 0; n   int cnt = 0; n   for (int i = n - 1; i >= 0; i--) { n     it = lower_bound(table, table + k, n                      make_pair(INT_MAX - price[i].second.first, -1)); n     if (it->first == INT_MAX) { n       continue; n     } else { n       bool flag = true; n       while (occ[it->second] == true) { n         it++; n         if (it->first == INT_MAX) { n           flag = false; n           break; n         } n       } n       if (flag == true) { n         occ[it->second] = true; n         sum += price[i].first; n         cnt++; n         ans.push_back(make_pair(price[i].second.second, it->second)); n       } n     } n   } n   cout << cnt <<     << sum <<   n ; n   sort(ans.begin(), ans.end()); n   for (int i = 0; i < ans.size(); i++) { n     cout << ans[i].first + 1 <<     << ans[i].second + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T qmin(const T &x, const T &y) { n   return x < y ? x : y; n } n template <typename T> n inline T qmax(const T &x, const T &y) { n   return x > y ? x : y; n } n template <typename T> n inline void getmin(T &x, const T &y) { n   if (y < x) x = y; n } n template <typename T> n inline void getmax(T &x, const T &y) { n   if (y > x) x = y; n } n inline void fileio(string s) { n   freopen((s +  .in ).c_str(),  r , stdin); n   freopen((s +  .out ).c_str(),  w , stdout); n } n const int inf = (int)1e9 + 7; n const long long linf = (long long)1e17 + 7; n const int N = 1e5 + 7, Node = N << 2; n int to[Node][10]; n long long val[Node][10]; n int n, q, a[N]; n inline void merge(int u) { n   for (int i = 0; i < (10); ++i) val[u][i] = 0; n   for (int i = 0; i < (10); ++i) { n     val[u][to[((u) << 1)][i]] += val[((u) << 1)][i]; n     val[u][to[((u) << 1 ^ 1)][i]] += val[((u) << 1 ^ 1)][i]; n   } n } n inline void psd(int u) { n   for (int i = 0; i < (10); ++i) { n     to[((u) << 1)][i] = to[u][to[((u) << 1)][i]]; n     to[((u) << 1 ^ 1)][i] = to[u][to[((u) << 1 ^ 1)][i]]; n   } n   for (int i = 0; i < (10); ++i) to[u][i] = i; n } n inline void init(int u, int l, int r) { n   for (int i = 0; i < (10); ++i) to[u][i] = i; n   if (l == r) { n     for (int v = a[l], w = 1; v; v /= 10, w *= 10) val[u][v % 10] += w; n   } else { n     int mid = l + r >> 1; n     init(((u) << 1), l, mid); n     init(((u) << 1 ^ 1), mid + 1, r); n     merge(u); n   } n } n inline void change(int u, int l, int r, int l1, int r1, int x, int y) { n   if (l1 <= l && r <= r1) { n     for (int i = 0; i < (10); ++i) n       if (to[u][i] == x) to[u][i] = y; n   } else { n     psd(u); n     int mid = l + r >> 1; n     if (l1 <= mid) change(((u) << 1), l, mid, l1, r1, x, y); n     if (r1 > mid) change(((u) << 1 ^ 1), mid + 1, r, l1, r1, x, y); n     merge(u); n   } n } n inline long long getans(int u, int l, int r, int l1, int r1) { n   long long ans = 0; n   if (l1 <= l && r <= r1) { n     for (int i = 0; i < (10); ++i) ans += to[u][i] * val[u][i]; n   } else { n     psd(u); n     int mid = l + r >> 1; n     if (l1 <= mid) ans += getans(((u) << 1), l, mid, l1, r1); n     if (r1 > mid) ans += getans(((u) << 1 ^ 1), mid + 1, r, l1, r1); n     merge(u); n   } n   return ans; n } n int main() { n   scanf( %d%d , &n, &q); n   for (int i = (1); i <= (n); ++i) scanf( %d , a + i); n   init(1, 1, n); n   while (q--) { n     int type, l, r, x, y; n     scanf( %d%d%d , &type, &l, &r); n     if (type == 1) { n       scanf( %d%d , &x, &y); n       change(1, 1, n, l, r, x, y); n     } else { n       printf( %lld n , getans(1, 1, n, l, r)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<vector<int>> moves(2, vector<int>()); n   for (int i = 0; i < 2; ++i) { n     int k; n     cin >> k; n     for (int j = 0; j < k; ++j) { n       int m; n       cin >> m; n       moves[i].push_back(m); n     } n   } n   queue<std::pair<int, int>> q; n   q.push({0, 0}); n   q.push({0, 1}); n   vector<vector<int>> res(n, vector<int>(2, -1)); n   res[0][0] = 0; n   res[0][1] = 0; n   vector<vector<int>> counter(n, vector<int>(2, 0)); n   while (!q.empty()) { n     std::pair<int, int> v = q.front(); n     q.pop(); n     int pos = v.first; n     int player = v.second; n     if (res[pos][player] == 0) { n       for (const auto& m : moves[1 ^ player]) { n         int k = pos - m; n         if (k < 0) { n           k += n; n         } n         if (res[k][1 ^ player] == -1) { n           res[k][1 ^ player] = 1; n           q.push({k, 1 ^ player}); n         } n       } n     } else { n       for (const auto& m : moves[1 ^ player]) { n         int k = pos - m; n         if (k < 0) { n           k += n; n         } n         if (res[k][1 ^ player] == -1) { n           ++counter[k][1 ^ player]; n           if (counter[k][1 ^ player] == (int)moves[1 ^ player].size()) { n             res[k][1 ^ player] = 0; n             q.push({k, 1 ^ player}); n           } n         } n       } n     } n   } n   for (int i = 0; i < 2; ++i) { n     for (int j = 1; j < n; ++j) { n       if (res[j][i] == 0) { n         cout <<  Lose  ; n       } else if (res[j][i] == 1) { n         cout <<  Win  ; n       } else { n         cout <<  Loop  ; n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n int a[maxn], b[maxn]; n int p[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   memset(p, 0, sizeof(p)); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &b[i]); n     p[b[i]] = i; n   } n   if (p[1]) { n     for (int i = 1; p[i] == p[1] + i - 1; i++) { n       if (p[i] == n) { n         int k = i + 1; n         for (; k <= n && p[k] <= k - i - 1; k++) n           ; n         if (k > n) { n           printf( %d n , n - i); n           return 0; n         } n       } n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     ans = max(ans, p[i] - i + 1 + n); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 2e9; n struct point { n   int x, y, id; n   point operator-(const point &a) const { n     point res; n     res.x = x - a.x; n     res.y = y - a.y; n     return res; n   } n   int operator*(const point &a) const { return x * a.y - y * a.x; } n } p[100010], o; n int dis(point x, point y) { n   return (x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y); n } n int qua(point x) { n   if (x.x > 0 && x.y >= 0) { n     return 1; n   } n   if (x.x <= 0 && x.y > 0) { n     return 2; n   } n   if (x.x < 0 && x.y <= 0) { n     return 3; n   } n   if (x.x >= 0 && x.y < 0) { n     return 4; n   } n } n bool cmp(point x, point y) { n   if (fabs(atan2(x.y, x.x) - atan2(y.y, y.x)) > 0.1) { n     return atan2(x.y, x.x) < atan2(y.y, y.x); n   } n   int tmp = (x - o) * (y - o); n   if (tmp == 0) { n     return dis(x, o) < dis(y, o); n   } else { n     return tmp > 0; n   } n } n point Div(point x, point y) { n   point res; n   res.x = x.x * y.x + x.y * y.y; n   res.y = x.y * y.x - x.x * y.y; n   return res; n } n int n, ans, ansa = 1, ansb = -inf; n bool upd(int a, int b) { n   if (1LL * a * ansb < 1LL * ansa * b) { n     ansa = a; n     ansb = b; n     return true; n   } else { n     return false; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &p[i].x, &p[i].y); n     p[i].id = i + 1; n   } n   sort(p, p + n, cmp); n   p[n] = p[0]; n   for (int i = 0; i < n; i++) { n     point x = Div(p[i], p[i + 1]); n     if (x.y < 0) { n       x.y = -x.y; n     } n     if (upd(x.y, x.x)) { n       ans = i; n     } n   } n   printf( %d %d n , p[ans].id, p[ans + 1].id); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:512000000 ) n using namespace std; n void solve(bool); n void precalc(); n clock_t start; n int main() { n   start = clock(); n   int t = 1; n   cout.sync_with_stdio(0); n   cin.tie(0); n   precalc(); n   cout.precision(10); n   cout << fixed; n   int testNum = 1; n   while (t--) { n     solve(true); n   } n   cout.flush(); n   return 0; n } n template <typename T> n T binpow(T q, T w, T mod) { n   if (!w) return 1 % mod; n   if (w & 1) return q * 1LL * binpow(q, w - 1, mod) % mod; n   return binpow(q * 1LL * q % mod, w / 2, mod); n } n template <typename T> n T gcd(T q, T w) { n   while (w) { n     q %= w; n     swap(q, w); n   } n   return q; n } n template <typename T> n T lcm(T q, T w) { n   return q / gcd(q, w) * w; n } n template <typename T> n void make_unique(vector<T>& a) { n   sort(a.begin(), a.end()); n   a.erase(unique(a.begin(), a.end()), a.end()); n } n template <typename T> n void relax_min(T& cur, T val) { n   cur = min(cur, val); n } n template <typename T> n void relax_max(T& cur, T val) { n   cur = max(cur, val); n } n void precalc() {} n const long long C = 100500; n long long mod; n long long phi; n long long rem[C]; n long long rev_rem[C]; n vector<long long> primes, degs; n long long n; n long long get_deg(long long n, long long p) { n   long long res = 0; n   while (n) { n     res += n / p; n     n /= p; n   } n   return res; n } n long long getC(long long n, long long k) { n   if (n < k) { n     return 0; n   } n   long long res = rem[n] * rev_rem[k] % mod * rev_rem[n - k] % mod; n   for (long long i = 0; i < primes.size(); ++i) { n     long long deg = get_deg(n, primes[i]) - get_deg(k, primes[i]) - n                     get_deg(n - k, primes[i]); n     res = res * binpow(primes[i], deg, mod) % mod; n   } n   return res; n } n long long get_res(long long l) { n   long long res = 0; n   for (long long par = 0; par < 2; ++par) { n     for (long long cur = 0; 2 * cur + par <= n; ++cur) { n       long long m = 2 * cur + par; n       long long cur_l = l; n       if ((cur_l + par) % 2) { n         ++cur_l; n       } n       long long now_c = (m + cur_l) / 2; n       res += getC(n, m) * getC(m, now_c); n       res %= mod; n     } n   } n   return res; n } n void solve(bool read) { n   long long l, r; n   cin >> n >> mod >> l >> r; n   rem[0] = 1; n   long long M = mod; n   phi = mod; n   for (long long p = 2; p * p <= M; ++p) { n     if (M % p == 0) { n       phi = phi * (p - 1) / p; n       primes.push_back(p); n       degs.push_back(0); n       while (M % p == 0) { n         M /= p; n         ++degs.back(); n       } n     } n   } n   if (M > 1) { n     phi = phi * (M - 1) / M; n     primes.push_back(M); n     degs.push_back(1); n   } n   for (long long i = 1; i < C; ++i) { n     long long cur = i; n     for (long long p : primes) { n       while (cur % p == 0) { n         cur /= p; n       } n     } n     rem[i] = rem[i - 1] * cur % mod; n   } n   for (long long i = 0; i < C; ++i) { n     rev_rem[i] = binpow(rem[i], phi - 1, mod); n   } n   long long res = get_res(l) - get_res(r + 1); n   res %= mod; n   if (res < 0) { n     res += mod; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long int pi[1000], ai[1000]; n long long int bsearch(long long int arr[], long long int x, long long int n) { n   long long int a = 0; n   long long int k; n   long long int b = n - 1; n   while (a <= b) { n     k = (a + b) / 2; n     if (arr[k] == x) { n     } n     if (arr[k] > x) n       b = k - 1; n     else n       a = k + 1; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int i, j, k = 0, m = 0, n, f = 0, r1 = 0, r2 = 0, r3 = 0, r4 = 0, n                       f1 = 0, f2 = 0, c1 = 0, c2 = 0, c3 = 0, c4 = 0, k1 = 0, n                       k2 = 0, j1, j2; n   cin >> n; n   for (i = 1; i <= n; i++) { n     if (n % i == 0) { n       f1 = n / i; n       j1 = (long long int)(log2(i * 1.0)); n       j2 = (long long int)(log2(f1 * 1.0)); n       for (; j1 >= 0; j1--) { n         if (i & (1 << j1) && r2 == 0) { n           c1++; n           if (j1 == 0 && c2 == c1 - 1) { n             k1 = max(k1, i); n           } n         } else if (!(i & (1 << j1))) { n           c2++; n           r2 = 1; n           if (j1 == 0 && c2 == c1 - 1) { n             k1 = max(k1, i); n           } n         } else n           break; n       } n       for (; j2 >= 0; j2--) { n         if (f1 & (1 << j2) && r4 == 0) { n           c3++; n           if (j2 == 0 && c3 - 1 == c4) { n             k2 = max(k2, f1); n           } n         } else if (!(f1 & (1 << j2))) { n           r4 = 1; n           c4++; n           if (j2 == 0 && c3 - 1 == c4) { n             k2 = max(k2, f1); n           } n         } else n           break; n       } n       r3 = 0; n       r1 = 0; n       r2 = 0; n       r4 = 0; n       c1 = 0; n       c2 = 0; n       c3 = 0; n       c4 = 0; n     } n   } n   cout << max(k1, k2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1200 + 10; n char s[maxn]; n int a[maxn]; n int main() { n   while (~scanf( %s , s)) { n     memset(a, 0, sizeof(a)); n     int len = strlen(s); n     int cnt = 0; n     int m; n     scanf( %d , &m); n     if (len % m == 0) { n       int cur = len / m; n       int sign = 1; n       for (int i = 0; i < len; i += cur) { n         for (int j = i, k = i + cur - 1; j < k; j++, k--) { n           if (s[j] != s[k]) { n             sign = 0; n             break; n           } n         } n         if (sign == 0) break; n       } n       if (sign) cnt = 1; n     } n     if (cnt) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, m; n string a, s; n int main() { n   cin >> a >> s; n   m = s.size(); n   sort(s.begin(), s.end()); n   for (m--; i < a.size(); i++) { n     if (a[i] < s[m]) a[i] = s[m--]; n   } n   cout << a; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string one, o, two; n   vector<int> a(2, 0); n   cin >> n; n   cin >> one; n   a[0] += 1; n   while (n-- > 1) { n     cin >> o; n     if (o == one) n       a[0] += 1; n     else { n       a[1] += 1; n       two = o; n     } n   } n   if (a[0] > a[1]) n     cout << one; n   else n     cout << two; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx ) n const long double eps = 1e-12; n const long double Pi = acos(-1); n const long long md = 1e9 + 7; n using namespace std; n long long n, m, p[10100], f[10100], x, y; n pair<long long, long long> d[10100]; n bool used[10100]; n bool cmp(pair<long long, long long> a, pair<long long, long long> b) { n   if (f[b.second] && f[a.second]) { n     return a.first > b.first; n   } n   if (!f[b.second] && !f[a.second]) { n     return a.first > b.first; n   } n   return f[a.second]; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> m; n   for (long long i = 1; i <= 2 * n; i++) { n     cin >> p[i]; n     d[i].first = p[i]; n     d[i].second = i; n   } n   for (long long i = 0; i < m; i++) { n     cin >> x >> y; n     f[x] = y; n     f[y] = x; n     d[x].first = p[x] - p[y]; n     d[y].first = p[y] - p[x]; n   } n   sort(d + 1, d + 2 * n + 1, cmp); n   long long t; n   cin >> t; n   long long cnt = 2 * n; n   long long it = 1; n   while (cnt) { n     if (t == 2) { n       cnt--; n       cin >> x; n       used[x] = true; n       if (f[x]) { n         cout << f[x] <<   n ; n         used[f[x]] = true; n         cnt--; n         cout << flush; n         ; n       } else { n         t = 3 - t; n       } n     } else { n       for (; it <= 2 * n; it++) { n         if (used[d[it].second]) continue; n         cout << d[it].second <<   n ; n         used[d[it].second] = true; n         cout << flush; n         ; n         if (f[d[it].second]) { n           cin >> x; n           used[x] = true; n           cnt--; n           t = 3 - t; n         } n         break; n       } n       cnt--; n       t = 3 - t; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 50, M1 = 998244353, M2 = 1e9 + 7; n struct ModInt { n   int x1, x2; n   ModInt(int a = 0, int b = 0) : x1(a), x2(b) {} n }; n ModInt operator+(ModInt a, ModInt b) { n   return ModInt((a.x1 + b.x1) % M1, (a.x2 + b.x2) % M2); n } n ModInt operator-(ModInt a, ModInt b) { n   return ModInt((a.x1 - b.x1 + M1) % M1, (a.x2 - b.x2 + M2) % M2); n } n ModInt operator*(ModInt a, ModInt b) { n   return ModInt(1ll * a.x1 * b.x1 % M1, 1ll * a.x2 * b.x2 % M2); n } n bool operator==(ModInt a, ModInt b) { return a.x1 == b.x1 && a.x2 == b.x2; } n int n, m, sz[N], B[2]; n char s[N]; n ModInt h[2][N], pw[N]; n ModInt f(int l, int r) { n   return h[l & 1][sz[r]] - h[l & 1][sz[l - 1]] * pw[sz[r] - sz[l - 1]]; n } n int main(int argc, char *argv[]) { n   scanf( %d%s , &n, s + 1); n   pw[0] = ModInt(1, 1), B[0] = 213, B[1] = 233; n   for (int i = 1; i <= (n); ++i) { n     pw[i] = pw[i - 1] * ModInt(10007, 10007); n     if (s[i] ==  0 ) { n       ++m; n       h[0][m] = h[0][m - 1] * pw[1] + ModInt(B[i & 1], B[i & 1]); n       h[1][m] = h[1][m - 1] * pw[1] + ModInt(B[(i & 1) ^ 1], B[(i & 1) ^ 1]); n     } n     sz[i] = m; n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int l1, l2, len; n     scanf( %d%d%d , &l1, &l2, &len); n     puts(f(l1, l1 + len - 1) == f(l2, l2 + len - 1) ?  Yes  :  No ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   k--; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   int res = 0; n   if (a[k] != 0) { n     res++; n     a[k] = 0; n   } n   for (int i = 0; i < n; i++) { n     if (i == k) continue; n     if (a[i] == 0) a[i] = n; n   } n   sort(a.begin(), a.end()); n   int gap = 0; n   int ind = 0; n   int prev = 0; n   int te = n; n   for (int i = 0; i < n; i++) { n     while (ind < n && a[ind] <= i) { n       ind++; n       if (ind == n || a[ind] > i) break; n       gap += max(0, a[ind] - a[ind - 1] - 1); n     } n     ind--; n     int wrong = n - ind - 1; n     te = min(te, max(wrong, gap)); n   } n   if (n > 1) res += te; n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T checkmin(T &a, T b) { n   return (a < b) ? a : a = b; n } n template <class T> n inline T checkmax(T &a, T b) { n   return (a > b) ? a : a = b; n } n template <class T> n T GCD(T a, T b) { n   if (a < 0) return GCD(-a, b); n   if (b < 0) return GCD(a, -b); n   return (a == 0) ? b : GCD(b % a, a); n } n template <class T> n T LCM(T a, T b) { n   if (a < 0) return LCM(-a, b); n   if (b < 0) return LCM(a, -b); n   return (a == 0 || b == 0) ? 0 : a / GCD(a, b) * b; n } n template <class T> n inline T sqr(T X) { n   return X * X; n } n namespace Poor { n const int MaxiN = 2005; n const int MaxiM = 2005; n const int MaxiS = 2005; n int N, M; n int Id[MaxiM]; n pair<int, int> Opt[MaxiS][MaxiS]; n string Base[MaxiN]; n string St; n vector<int> List[MaxiN][26]; n void Run() { n   cin >> N; n   for (int i = 0; i < N; ++i) cin >> Base[i]; n   cin >> M; n   for (int i = 0; i < M; ++i) { n     cin >> Id[i]; n     --Id[i]; n   } n   cin >> St; n   for (int i = 0; i < N; ++i) n     for (int j = 0; j < (int)Base[i].size(); ++j) n       List[i][Base[i][j] -  a ].push_back(j); n   int Len = St.size(); n   for (int i = 0; i < Len + 1; ++i) n     for (int j = 0; j < Len + 1; ++j) Opt[i][j] = make_pair(M, 0); n   Opt[0][0] = make_pair(0, 0); n   for (int i = 1; i <= Len; ++i) { n     int t = St[i - 1] -  a ; n     Opt[i][0] = make_pair(0, 0); n     for (int j = 1; j <= i; ++j) { n       Opt[i][j] = Opt[i - 1][j]; n       pair<int, int> Tmp = Opt[i - 1][j - 1]; n       while (Tmp < Opt[i][j]) { n         vector<int>::iterator it = n             lower_bound(List[Id[Tmp.first]][t].begin(), n                         List[Id[Tmp.first]][t].end(), Tmp.second); n         if (it != List[Id[Tmp.first]][t].end()) { n           Tmp.second = *it + 1; n           break; n         } n         ++Tmp.first; n         Tmp.second = 0; n       } n       checkmin(Opt[i][j], Tmp); n     } n   } n   for (int j = Len; j >= 0; --j) n     if (Opt[Len][j] < make_pair(M, 0)) { n       cout << j << endl; n       break; n     } n } n }  // namespace Poor n int main() { n   ios::sync_with_stdio(false); n   Poor::Run(); n   return 0; n } n


// Problem: A. Dungeon n // Contest: Codeforces - Educational Codeforces Round 100 (Rated for Div. 2) n // URL: https://codeforces.com/problemset/problem/1463/A n // Memory Limit: 256 MB n // Time Limit: 2000 ms n //  n // Powered by CP Editor (https://cpeditor.org) n  n #include<bits/stdc++.h> n using namespace std; n #pragma GCC optimize( O2 ) n #define M 1000000007 n #define ll long long int n #define pb push_back n  n #define pf push_front n #define all(x) x.begin(), x.end() n #define pob pop_back n #define pof pop_front n #define debug1(x) cout<<#x<<   <<x<<endl; n #define debug2(x,y) cout<<#x<<   <<x<<     <<#y<<   <<y<<endl; n #define debug3(x,y,z) cout<<#x<<   <<x<<     <<#y<<   <<y<<     <<#z<<   <<z<<endl; n #define present(c,x) ((c).find(x) != (c).end()) n #define null NULL n #define mp make_pair n #define sz(x) (ll)x.size() n #define fi first n #define se second n #define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); n #define inf 1e18 n //#define endl   n  n //unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); n //shuffle (foo.begin(), foo.end(), std::default_random_engine(seed)); n #define ordered_set tree<ll, null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> n typedef pair<int, int>  pii; n #define pi (3.14159265358979323846264338327950288) n template<typename T> n void printv(const T& t) { n     std::copy(t.cbegin(), t.cend(), std::ostream_iterator<typename T::value_type>(std::cout,  ,  )); n     cout<<endl; n } n ll modpower(ll a,ll b,ll c) n { n  ll res=1; n  while(b) n  { n   if(b&1LL) n    res=(res*a)%c; n   a=(a*a)%c; n   b>>=1; n  } n  return res; n } n //-------------------------------Template--Above----------------------------------------------- n void solve(){ n  vector<ll>v(3); n  for(ll i=0;i<3;i++) n  cin>>v[i]; n  sort(all(v)); n  ll sum=v[0]+v[1]+v[2]; n  if(sum%9!=0){ n   cout<< NO <<endl; n   return; n  } n  if(v[0]<sum/9){ n   cout<< NO <<endl; n   return; n  } n  cout<< YES <<endl; n     return; n } n int main() n { n     boost n     ll t; n    // t=1; n     cin>>t; n     while(t--){ n         solve(); n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char ar[] =  1689 , s[1000010]; n int val[10], ct[10]; n int main() { n   do { n     val[atoi(ar) % 7] = atoi(ar); n   } while (next_permutation(ar, ar + 4)); n   scanf( %s , s); n   int len = strlen(s); n   for (int i = 0; i < len; ++i) ct[s[i] -  0 ]++; n   for (int i = 0; i < 4; ++i) ct[ar[i] -  0 ]--; n   int rem = 0, div = 0; n   for (int i = 1; i < 10; ++i) { n     while (ct[i]--) { n       printf( %d , i); n       div = rem * 10 + i; n       rem = div % 7; n     } n   } n   rem *= 10000; n   for (int i = 0; i < 7; ++i) n     if (!((val[i] + rem) % 7)) { n       printf( %d , val[i]); n       while (ct[0]--) printf( 0 ); n       printf(  n ); n       return 0; n     } n   return 0; n } n


#include <bits/stdc++.h> n const long long mod = 2147483647LL; n long long read() { n   long long r = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   while (c >=  0  && c <=  9 ) { n     r = 10LL * r + (long long)c -  0 ; n     c = getchar(); n   } n   return r; n } n long long fast(long long a, long long b) { n   long long r = 1LL; n   while (b) { n     if (b & 1) r = (r * a) % mod; n     b >>= 1; n     a = (a * a) % mod; n   } n   return r; n } n std::string str; n int a, b, c, T; n signed main() { n   T = read(); n   while (T--) { n     std::cin >> str; n     a = 0; n     b = 0; n     c = 0; n     for (int i = 0; i < str.size(); ++i) { n       if (str[i] ==  A ) ++a; n       if (str[i] ==  B ) ++b; n       if (str[i] ==  C ) ++c; n     } n     if (b == a + c) n       puts( YES ); n     else n       puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:20000000 ) n using namespace std; n int ri() { n   int x; n   scanf( %d , &x); n   return x; n } n long long rll() { n   long long x; n   scanf( %lld , &x); n   return x; n } n int mas[200500]; n int tmas[200500]; n int d1[100500]; n int d2[100500]; n int cnt[200500]; n int p1[200500]; n int p2[200500]; n int c[20][200500]; n void suffArray(int n) { n   for (int i = (int)(0); i <= (int)(n - 1); i++) cnt[mas[i]]++; n   for (int i = (int)(1); i <= (int)(200499); i++) cnt[i] += cnt[i - 1]; n   for (int i = (int)(0); i <= (int)(n - 1); i++) p1[--cnt[mas[i]]] = i; n   c[0][p1[0]] = 0; n   int cl = 0; n   for (int i = (int)(1); i <= (int)(n - 1); i++) { n     if (mas[p1[i]] != mas[p1[i - 1]]) cl++; n     c[0][p1[i]] = cl; n   } n   for (int mid = 1, step = 0; mid < n; mid <<= 1, step++) { n     for (int i = (int)(0); i <= (int)(cl); i++) cnt[i] = 0; n     for (int i = (int)(0); i <= (int)(n - 1); i++) n       p2[i] = (p1[i] - mid + n + n) % n; n     for (int i = (int)(0); i <= (int)(n - 1); i++) cnt[c[step][p2[i]]]++; n     for (int i = (int)(1); i <= (int)(cl); i++) cnt[i] += cnt[i - 1]; n     for (int i = (int)(n - 1); i >= (int)(0); i--) n       p1[--cnt[c[step][p2[i]]]] = p2[i]; n     c[step + 1][p1[0]] = 0; n     cl = 0; n     for (int i = (int)(1); i <= (int)(n - 1); i++) { n       int a = (p1[i] + mid) % n; n       int b = (p1[i - 1] + mid) % n; n       if (c[step][p1[i]] != c[step][p1[i - 1]] || c[step][a] != c[step][b]) n         cl++; n       c[step + 1][p1[i]] = cl; n     } n   } n } n int getLCP(int a, int b, int mx) { n   int res = 0; n   int m = min(mx - a, mx - b); n   int L = 0; n   while ((1 << L) <= m) L++; n   L--; n   for (int i = (int)(L); i >= (int)(0); i--) n     if (c[i][a] == c[i][b]) { n       res += (1 << i); n       a += (1 << i); n       b += (1 << i); n       a %= mx; n       b %= mx; n     } n   return min(res, mx); n } n int posD1[100500]; n vector<int> tree[8 * 100500]; n int N; n void build(int v, int l, int r) { n   if (l == r) { n     if (p1[l] >= N) tree[v].push_back(p1[l] - N); n     return; n   } n   int mid = (l + r) >> 1; n   build(2 * v, l, mid); n   build(2 * v + 1, mid + 1, r); n   tree[v].resize(tree[2 * v].size() + tree[2 * v + 1].size()); n   merge(tree[2 * v].begin(), tree[2 * v].end(), tree[2 * v + 1].begin(), n         tree[2 * v + 1].end(), tree[v].begin()); n } n int get(int v, int l, int r, int L, int R, int h1, int h2) { n   if (l == L && r == R) { n     if (tree[v].empty()) return 0; n     int res = 0; n     int tl = 0, tr = tree[v].size(); n     if (tree[v][0] <= h2) { n       while (tl + 1 < tr) { n         int mid = (tl + tr) >> 1; n         if (tree[v][mid] <= h2) n           tl = mid; n         else n           tr = mid; n       } n       res += tl + 1; n     } n     tl = 0, tr = tree[v].size(); n     if (tree[v][0] < h1) { n       while (tl + 1 < tr) { n         int mid = (tl + tr) >> 1; n         if (tree[v][mid] < h1) n           tl = mid; n         else n           tr = mid; n       } n       res -= tl + 1; n     } n     return res; n   } n   int mid = (l + r) >> 1; n   if (R <= mid) return get(2 * v, l, mid, L, R, h1, h2); n   if (L > mid) return get(2 * v + 1, mid + 1, r, L, R, h1, h2); n   return get(2 * v, l, mid, L, mid, h1, h2) + n          get(2 * v + 1, mid + 1, r, mid + 1, R, h1, h2); n } n void solve() { n   int n = ri(); n   for (int i = (int)(1); i <= (int)(n); i++) mas[i] = ri(); n   for (int i = (int)(1); i <= (int)(n - 1); i++) n     d1[i] = mas[i] - mas[i + 1], d2[i] = -d1[i]; n   for (int i = (int)(0); i <= (int)(n - 2); i++) mas[i] = d1[i + 1]; n   mas[n - 1] = 2000000000; n   for (int i = (int)(0); i <= (int)(n - 2); i++) mas[i + n] = d2[i + 1]; n   mas[2 * n - 1] = 2000000001; n   memcpy(tmas, mas, sizeof(mas)); n   sort(tmas, tmas + 2 * n); n   map<int, int> index; n   int cn = 0; n   for (int i = (int)(0); i <= (int)(2 * n - 1); i++) n     if (i == 0) n       index[tmas[i]] = cn++; n     else if (tmas[i] != tmas[i - 1]) n       index[tmas[i]] = cn++; n   for (int i = (int)(0); i <= (int)(2 * n - 1); i++) mas[i] = index[mas[i]]; n   suffArray(n + n); n   for (int i = (int)(0); i <= (int)(2 * n - 1); i++) n     if (p1[i] <= n - 2) posD1[p1[i]] = i; n   N = n; n   build(1, 0, 2 * n - 1); n   int q = ri(); n   for (int i = (int)(0); i <= (int)(q - 1); i++) { n     int L = ri() - 1, R = ri() - 1; n     int pos = posD1[L]; n     int lb = pos, rb = pos; n     int l = -1, r = pos; n     while (l + 1 < r) { n       int mid = (l + r) >> 1; n       if (getLCP(p1[mid], p1[pos], 2 * n) >= R - L) n         r = mid; n       else n         l = mid; n     } n     lb = r; n     l = pos, r = 2 * n; n     while (l + 1 < r) { n       int mid = (l + r) >> 1; n       if (getLCP(p1[pos], p1[mid], 2 * n) >= R - L) n         l = mid; n       else n         r = mid; n     } n     rb = l; n     int res = 0; n     if (L == R) n       res = n - 1; n     else { n       int tl = L, tr = R; n       if (tl - tr + tl - 1 >= 0) n         res += get(1, 0, 2 * n - 1, lb, rb, 0, tl - tr + tl - 1); n       if (tr + 1 <= n - 1) res += get(1, 0, 2 * n - 1, lb, rb, tr + 1, n - 1); n     } n     printf( %d n , res); n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500010; n const int INF = 0x3f3f3f3f; n const int iinf = 1 << 30; n const long long linf = 2e18; n const int MOD = 1000000007; n const double eps = 1e-7; n void print(int x) { n   cout << x << endl; n   exit(0); n } n void PRINT(string x) { n   cout << x << endl; n   exit(0); n } n void douout(double x) { printf( %lf n , x + 0.0000000001); } n int num[N], bit[N]; n int n; n struct node { n   int x, y, z; n } a[N]; n bool cmp(node a, node b) { return a.y > b.y; } n void lsh() { n   sort(num + 1, num + n + 1); n   int t = unique(num + 1, num + n + 1) - (num + 1); n   for (int i = (1); i <= (n); i++) n     a[i].x = t - (lower_bound(num + 1, num + 1 + t, a[i].x) - (num + 1)); n } n void add(int x, int y) { n   for (; x < N; x += (x & -x)) bit[x] = max(bit[x], y); n } n int ask(int x) { n   int ans = 0; n   for (; x; x -= (x & -x)) ans = max(ans, bit[x]); n   return ans; n } n signed main() { n   scanf( %d , &n); n   for (int i = (1); i <= (n); i++) scanf( %d , &a[i].x), num[i] = a[i].x; n   for (int i = (1); i <= (n); i++) scanf( %d , &a[i].y); n   for (int i = (1); i <= (n); i++) scanf( %d , &a[i].z); n   lsh(); n   sort(a + 1, a + n + 1, cmp); n   int ans = 0; n   for (int i = 1, j; i <= n; i = j) { n     for (j = i; j <= n && a[j].y == a[i].y; j++) { n       int z = ask(a[j].x - 1); n       if (z > a[j].z) ans++; n     } n     for (; i < j; i++) add(a[i].x, a[i].z); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500; n int n, ret, cnt; n char a[maxn][maxn]; n int arr[maxn][maxn]; n pair<int, int> par[maxn][maxn]; n multiset<pair<int, int> > s; n pair<int, int> root(pair<int, int> c) { n   if (par[c.first][c.second].first < 0) return c; n   return par[c.first][c.second] = root(par[c.first][c.second]); n } n void merge(pair<int, int> A, pair<int, int> B) { n   if ((A = root(A)) == (B = root(B))) return; n   if (par[A.first][A.second].first < par[B.first][B.second].first) swap(A, B); n   par[B.first][B.second].first += par[A.first][A.second].first; n   par[A.first][A.second] = B; n } n void add(pair<int, int> A) { n   A = root(A); n   if (arr[A.first][A.second] == 0) ret += -par[A.first][A.second].first; n   arr[A.first][A.second]++; n } n void Add(pair<int, int> A) { n   int i = A.first, j = A.second; n   if (a[i][j] ==  X ) ret++; n   if (a[i][j] !=  X ) add({i, j}); n   if (i - 1 >= 0 && a[i - 1][j] !=  X ) add({i - 1, j}); n   if (i + 1 < n && a[i + 1][j] !=  X ) add({i + 1, j}); n   if (j - 1 >= 0 && a[i][j - 1] !=  X ) add({i, j - 1}); n   if (j + 1 < n && a[i][j + 1] !=  X ) add({i, j + 1}); n } n void rem(pair<int, int> A) { n   A = root(A); n   arr[A.first][A.second]--; n   if (arr[A.first][A.second] == 0) ret -= -par[A.first][A.second].first; n } n void Rem(pair<int, int> A) { n   int i = A.first, j = A.second; n   if (a[i][j] ==  X ) ret--; n   if (a[i][j] !=  X ) rem({i, j}); n   if (i - 1 >= 0 && a[i - 1][j] !=  X ) rem({i - 1, j}); n   if (i + 1 < n && a[i + 1][j] !=  X ) rem({i + 1, j}); n   if (j - 1 >= 0 && a[i][j - 1] !=  X ) rem({i, j - 1}); n   if (j + 1 < n && a[i][j + 1] !=  X ) rem({i, j + 1}); n } n int main() { n   ios_base::sync_with_stdio(false); n   int K; n   cin >> n >> K; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) cin >> a[i][j]; n   for (int i = 0; i < maxn; i++) n     for (int j = 0; j < maxn; j++) par[i][j] = {-1, -1}; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (a[i][j] ==  . ) { n         if (i + 1 < n && a[i + 1][j] ==  . ) merge({i, j}, {i + 1, j}); n         if (j + 1 < n && a[i][j + 1] ==  . ) merge({i, j}, {i, j + 1}); n       } n   int ans = 0; n   for (int i = K - 1; i < n; i++) { n     memset(arr, 0, sizeof arr); n     ret = 0; n     for (int j = i - K + 1; j <= i; j++) n       for (int k = 0; k < K; k++) { n         Add({j, k}); n       } n     ans = max(ans, ret); n     for (int j = K; j < n; j++) { n       for (int k = i - K + 1; k <= i; k++) Add({k, j}); n       for (int k = i - K + 1; k <= i; k++) Rem({k, j - K}); n       ans = max(ans, ret); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     int n, k; n     cin >> n >> k; n     vector<int> a(n); n     for (int i = 0; i < n; ++i) { n       cin >> a[i]; n     } n     if (n == 1) { n       if (a[0] == k) { n         cout <<  yes n ; n       } else { n         cout <<  no n ; n       } n       continue; n     } n     bool flag = false; n     bool k_flag = false; n     for (int i = 0; i < n; ++i) { n       if (a[i] == k) { n         k_flag = true; n         break; n       } n     } n     if (!k_flag) { n       cout <<  no n ; n       continue; n     } n     for (int i = 0; i < n; ++i) { n       if (a[i] != k) { n         continue; n       } n       if (i - 1 >= 0 && a[i - 1] >= k || i + 1 < n && a[i + 1] >= k) { n         flag = true; n         break; n       } n       if (i - 2 >= 0 && a[i - 2] >= k || i + 2 < n && a[i + 2] >= k) { n         flag = true; n         break; n       } n       if (flag) { n         break; n       } n     } n     for (int i = 0; i < n - 1; ++i) { n       if (a[i] >= k && a[i + 1] >= k) { n         flag = true; n       } n     } n     for (int i = 0; i < n - 2; ++i) { n       if (a[i] >= k && a[i + 2] >= k) { n         flag = true; n       } n     } n     if (flag) { n       cout <<  yes n ; n     } else { n       cout <<  no n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, m; n int a[128], id = 1; n int main() { n   cin >> t >> m; n   for (int i = 0; i < t; ++i) { n     string q; n     cin >> q; n     switch (q[0]) { n       case  a : { n         int k; n         bool ok = false; n         cin >> k; n         for (int i = 0; i + k <= m; ++i) { n           int j = 0; n           for (j = 0; j < k && a[i + j] == 0; ++j) n             ; n           if (j == k) { n             ok = true; n             for (int j = 0; j < k; ++j) a[i + j] = id; n             break; n           } n         } n         if (ok) n           cout << id++ << endl; n         else n           puts( NULL ); n         break; n       } n       case  e : { n         bool ok = false; n         int x; n         cin >> x; n         if (x > 0) { n           for (int i = 0; i < m; ++i) n             if (a[i] == x) ok = true, a[i] = 0; n         } n         if (!ok) puts( ILLEGAL_ERASE_ARGUMENT ); n         break; n       } n       case  d : n         for (int i = remove(a, a + m, 0) - a; i < m; ++i) a[i] = 0; n         break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void OUT_TST(string s, istream_iterator<string> it) { cerr << s; } n template <typename T, typename... Args> n void OUT_TST(string s, istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<  =  << a <<    ; n   OUT_TST(s, ++it, args...); n } n template <class T> n void rd(T &ret) { n   ret = 0; n   bool ok = 0, u = 0; n   for (;;) { n     int c = getchar(); n     if (c >=  0  && c <=  9 ) n       ret = (ret << 3) + (ret << 1) + c -  0 , ok = 1; n     else if (c ==  - ) n       u = 1; n     else if (ok) { n       if (u) ret *= -1; n       return; n     } n   } n } n long long powmod(long long p, long long n, long long mod) { n   long long ret = 1; n   for (; n; n >>= 1) { n     if (n & 1) ret = ret * p % mod; n     p = p * p % mod; n   } n   return ret; n } n template <class T> n bool chmin(T &a, const T &b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool chmax(T &a, const T &b) { n   return b > a ? a = b, 1 : 0; n } n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n bool ok(vector<pair<int, int> > &bn, vector<int> &pos, bool tmp) { n   vector<pair<int, int> > an(4); n   for (int i = (0); i < (4); i++) an[i] = bn[pos[i]]; n   if (tmp == 1) { n     if (an[0].first != an[2].first) return 0; n     if (an[0].second >= an[2].second) return 0; n     if (an[1].second != an[3].second) return 0; n     if (an[1].first <= an[3].first) return 0; n     return 1; n   } n   return 1; n   if (an[0].first != an[3].first) return 0; n   if (an[0].second >= an[3].second) return 0; n   if (an[1].second >= an[2].second) return 0; n   return 1; n } n vector<pair<int, int> > gao(vector<pair<int, int> > an, bool tmp) { n   vector<int> pos; n   for (int i = (0); i < (4); i++) pos.push_back(i); n   bool fg = 0; n   if (ok(an, pos, tmp)) fg = 1; n   while (!fg && next_permutation((pos).begin(), (pos).end())) { n     if (ok(an, pos, tmp)) { n       assert(fg == 0); n       fg = 1; n     } n   } n   vector<pair<int, int> > cn(4); n   for (int i = (0); i < (4); i++) cn[i] = an[pos[i]]; n   return cn; n } n vector<pair<int, int> > an, bn; n int xlow, ylow, xup, yup; n bool ins(int first, int second) { n   if (first >= xlow && first <= xup && second >= ylow && second <= yup) n     return 1; n   return 0; n } n int main() { n   pair<int, int> t; n   for (int i = (0); i < (4); i++) { n     scanf( %d%d , &t.first, &t.second); n     an.push_back(t); n   } n   for (int i = (0); i < (4); i++) { n     scanf( %d%d , &t.first, &t.second); n     bn.push_back(t); n   } n   an = gao(an, 0); n   bn = gao(bn, 1); n   xlow = INT_MAX, ylow = INT_MAX, xup = -INT_MAX, yup = -INT_MAX; n   for (int i = (0); i < (4); i++) { n     chmin(xlow, an[i].first); n     chmax(xup, an[i].first); n     chmin(ylow, an[i].second); n     chmax(yup, an[i].second); n   } n   for (int i = (0); i < (4); i++) { n     string _s =  an[i].x, an[i].y ; n     replace(_s.begin(), _s.end(),  , ,    ); n     stringstream _ss(_s); n     istream_iterator<string> _it(_ss); n     OUT_TST(  , _it, an[i].first, an[i].second); n   }; n   puts(  ); n   for (int i = (0); i < (4); i++) { n     string _s =  bn[i].x, bn[i].y ; n     replace(_s.begin(), _s.end(),  , ,    ); n     stringstream _ss(_s); n     istream_iterator<string> _it(_ss); n     OUT_TST(  , _it, bn[i].first, bn[i].second); n   }; n   puts(  ); n   int l = bn[1].second, r = bn[1].second; n   for (int i = (bn[3].first); i < (bn[1].first + 1); i++) { n     for (int j = (l); j < (r + 1); j++) n       if (ins(i, j)) { n         puts( YES ); n         return 0; n       } n     if (i < bn[0].first) n       --l, ++r; n     else n       ++l, --r; n   } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n #define N 200020 n int cnt, last[N], n, ans; n struct edge { n     int to, next; n } e[N << 1]; n  n inline void add(int u, int v) { n     e[++cnt] = {v, last[u]}, last[u] = cnt; n     e[++cnt] = {u, last[v]}, last[v] = cnt; n } n  n int dfs(int x, int fa) { n     vector<pair<int, int> > q; n     q.clear(); n     for (int i = last[x]; i; i = e[i].next) { n         int y = e[i].to; n         if (y == fa)continue; n         q.push_back({dfs(y, x), y}); n     } n     if (!q.size())return 0; n     sort(q.begin(), q.end()); n     for (int i = 1; i < q.size(); ++i) { n         ans = max(q[i - (x == 1)].first + 2, ans); n     } n     return q[(q.size() - 1) * (x == 1)].first + 1; n } n  n inline void solve() { n     scanf( %d , &n); n     for (int i = 1; i <= n; ++i)last[i] = 0; n     cnt = 0; n     for (int i = 1; i < n; ++i) { n         int u, v; n         scanf( %d%d , &u, &v); n         add(u, v); n     } n     ans = 0; n     ans = max(ans, dfs(1, 0)); n     printf( %d n , ans); n } n  n int main() { n     int T = 1; n     scanf( %d , &T); n     for (int i = 1; i <= T; ++i)solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int hag(int p, int t) { n   int d; n   d = max(3 * p / 10, p - ((p / 250) * t)); n   return d; n } n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   if (hag(a, c) > hag(b, d)) cout <<  Misha ; n   if (hag(a, c) == hag(b, d)) cout <<  Tie ; n   if (hag(a, c) < hag(b, d)) cout <<  Vasya ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, f, t, sum = -999999999, m, x, i; n   cin >> n >> k; n   for (i = 0; i < n; i++) { n     cin >> f >> t; n     if (k < t) { n       m = f - (t - k); n     } else { n       m = f; n     } n     x = max(sum, m); n     sum = x; n   } n   cout << x << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n unordered_map<string, int> ma; n int T, n, tot; n int a[1000010], b[1000010]; n long long dp[1000010][2], ans; n struct node { n   int per, to; n } e[1000010]; n int head[1000010]; n long long sum; n void addedge(int from, int to) { n   e[++tot].to = to; n   e[tot].per = head[from]; n   head[from] = tot; n } n void dfs(int u, int fa) { n   dp[u][b[u]]++; n   for (int i = head[u]; i > 0; i = e[i].per) { n     int v = e[i].to; n     if (v == fa) continue; n     dfs(v, u); n     sum += dp[v][0] * dp[u][1] + dp[v][1] * dp[u][0]; n     if (b[u] > 0) { n       dp[u][0] += dp[v][1]; n       dp[u][1] += dp[v][0]; n     } else { n       dp[u][0] += dp[v][0]; n       dp[u][1] += dp[v][1]; n     } n   } n   sum += b[u]; n } n int main() { n   T = 1; n   while (T--) { n     memset(head, -1, sizeof(head)); n     scanf( %d , &n); n     for (int i = 1; i <= n; ++i) { n       scanf( %d , &a[i]); n     } n     for (int i = 1; i < n; ++i) { n       int u, v; n       scanf( %d%d , &u, &v); n       addedge(u, v); n       addedge(v, u); n     } n     for (int i = 0; i <= 20; ++i) { n       sum = 0; n       for (int j = 1; j <= n; ++j) { n         b[j] = (a[j] & (1 << i)) > 0; n         dp[j][0] = dp[j][1] = 0; n       } n       dfs(1, -1); n       ans += sum << i; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000010; n bitset<N> b; n int p[N]; n int cnt[N]; n bool was[N]; n int main() { n   int n, k; n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %d , p + i); n     p[i]--; n     was[i] = false; n   } n   for (int i = 1; i <= n; i++) { n     cnt[i] = 0; n   } n   int c1 = 0, c2 = 0; n   for (int i = 0; i < n; i++) { n     if (was[i]) { n       continue; n     } n     int x = i; n     int len = 0; n     while (!was[x]) { n       was[x] = true; n       x = p[x]; n       len++; n     } n     c1 += len % 2; n     c2 += len / 2; n     cnt[len]++; n   } n   b.set(n); n   for (int i = 1; i <= n; i++) { n     if (cnt[i] == 0) { n       continue; n     } n     for (int j = 0; (1 << j) <= cnt[i]; j++) { n       int x = i * (1 << j); n       b |= (b >> x); n       cnt[i] -= (1 << j); n     } n     b |= (b >> (i * cnt[i])); n   } n   int ans_min = k; n   if (!b.test(n - k)) { n     ans_min++; n   } n   int ans_max = 0; n   ans_max += 2 * min(k, c2); n   k -= min(k, c2); n   ans_max += min(k, c1); n   printf( %d %d n , ans_min, ans_max); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string c; n   cin >> c; n   string ans = regex_replace(c, regex( (/)+ ),  / ); n   if (ans[ans.size() - 1] ==  /  and ans.size() > 1) { n     ans[ans.size() - 1] =    ; n   } n   cout << ans << endl; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long OO = 1e18; n vector<int> prs = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; n long long solve(long long num, int in, int lst) { n   if (in == prs.size() || lst == 0) return num == 1 ? 1 : -1; n   long long res = -1; n   long long cur = 1; n   long long cp = num; n   for (int i = 0; i <= lst; i++) { n     if (num < (i + 1) || cur > OO || (i && cur > OO / prs[in])) break; n     if (i) cur *= prs[in]; n     if (num % (i + 1) != 0) continue; n     long long can = solve(num / (i + 1), in + 1, i); n     if ((res == -1 && can != -1) || (can != -1 && can < (res + cur - 1) / cur)) n       res = cur * can; n   } n   return res; n } n int main() { n   long long n; n   cin >> n; n   cout << solve(n, 0, 100); n } n


#include <bits/stdc++.h> n uint16_t C[10][10 + 1]; n void flow(int i, int j, uint16_t c) { n   if (i >= 10) { n     return; n   } n   if (C[i][j] == (1 << 10)) { n     flow(i + 1, j, c >> 1); n     flow(i + 1, j + 1, c >> 1); n   } else { n     C[i][j] += c; n   } n } n int main() { n   int n, t; n   std::cin >> n >> t; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j <= i; j++) { n       C[i][j] = 0; n     } n   } n   while (t--) { n     flow(0, 0, (1 << 10)); n   } n   int r = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j <= n; j++) { n       if (C[i][j] == (1 << 10)) { n         r++; n       } n     } n   } n   std::cout << r <<   n ; n   return 0; n } n