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


#include <bits/stdc++.h> n using namespace std; n long long offset[1000]; n vector<pair<long long, int> > x[1000]; n long long v[500005]; n int bucketsize = 700; n inline int bucket(int x) { return x / bucketsize; } n int buckets, n; n inline int bstart(int x) { n   if (x == buckets + 1) { n     return n + 1; n   } n   return x * bucketsize; n } n void resort(int buc) { n   x[buc].clear(); n   int l = bstart(buc), r = bstart(buc + 1); n   for (int j = l; j < r; j++) { n     x[buc].push_back({v[j], j}); n   } n   sort(x[buc].begin(), x[buc].end()); n } n int main() { n   int q; n   scanf( %d %d , &n, &q); n   buckets = bucket(n); n   for (int i = 1; i <= n; i++) { n     int k; n     scanf( %d , &k); n     v[i] = k; n     x[bucket(i)].push_back({v[i], i}); n   } n   for (int i = 0; i <= buckets; i++) { n     resort(i); n   } n   for (int qq = 0; qq < q; qq++) { n     int t; n     scanf( %d , &t); n     if (t == 1) { n       int l, r, dd; n       scanf( %d %d %d , &l, &r, &dd); n       long long d = dd; n       int lbucket0 = bucket(l); n       int lbucket1 = lbucket0 + 1; n       int rbucket = bucket(r); n       int lstart1 = bstart(lbucket1); n       for (int j = lbucket1; j < rbucket; j++) { n         offset[j] += d; n       } n       if (lbucket0 == rbucket) { n         for (int j = l; j <= r; j++) v[j] += d; n         resort(lbucket0); n       } else { n         for (int j = l; j < lstart1; j++) v[j] += d; n         for (int j = bstart(rbucket); j <= r; j++) v[j] += d; n         resort(lbucket0); n         resort(rbucket); n       } n     } else { n       int yy; n       scanf( %d , &yy); n       long long y = yy; n       vector<pair<long long, int> >::iterator it; n       int l = 0, r = -1; n       for (int i = 0; i <= buckets; i++) { n         if (y - offset[i] <= 0) continue; n         pair<long long, int> search = {y - offset[i], 0}; n         it = lower_bound(x[i].begin(), x[i].end(), search); n         if (it == x[i].end()) continue; n         if (it->first == y - offset[i]) { n           l = it->second; n           break; n         } n       } n       for (int i = buckets; i >= 0; i--) { n         if (y - offset[i] <= 0) continue; n         pair<long long, int> search = {y - offset[i] + 1, 0}; n         it = lower_bound(x[i].begin(), x[i].end(), search); n         if (it == x[i].begin()) continue; n         it--; n         if (it->first == y - offset[i]) { n           r = it->second; n           break; n         } n       } n       printf( %d n , r - l); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int T; n   cin >> T; n   while (T--) { n     int a, b, n; n     cin >> a >> b >> n; n     if (n % 3 == 0) n       cout << a << endl; n     else if (n % 3 == 1) n       cout << b << endl; n     else n       cout << (a ^ b) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a; n int tab[105]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     cin >> a; n     tab[a]--; n   } n   sort(tab + 1, tab + 1 + 100); n   {} n   for (int wynik = 1; wynik <= 101; wynik++) { n     int ile = 0; n     for (int i = 1; i <= 100; i++) { n       ile += (-tab[i]) / wynik; n     } n     if (ile < n) { n       printf( %d n , wynik - 1); n       return 0; n     } n   } n   puts( 0 ); n   return 0; n } n


#include <bits/stdc++.h> n namespace myland { n using namespace std; n namespace _abbr { n const double EPS(1e-8); n const double PI(acos(-1.0)); n const int INF(0x3f3f3f3f); n const long long INFL(0x3f3f3f3f3f3f3f3fll); n const int MOD(1e9 + 7); n }  // namespace _abbr n using namespace _abbr; n namespace _solve {} n using namespace _solve; n namespace _calculate { n bool odd(long long x) { return x & 1; } n bool even(long long x) { return (x & 1) ^ 1; } n bool posi(long long x) { return x > 0; } n bool nega(long long x) { return x < 0; } n bool zero(long long x) { return x == 0; } n bool prime(long long x) { n   if (x < 2) return 0; n   for (int i = 2; i * i <= x; i++) n     if (x % i == 0) return 0; n   return 1; n } n long long droot(long long x) { return 1 + (x - 1) % 9; } n long long upd(long long a, long long b) { return a % b ? a / b + 1 : a / b; }; n long long random(long long a, long long b) { n   return a + rand() * rand() % (b - a + 1); n }; n long long bitn(long long x) { n   long long c = 0; n   while (x) c++, x >>= 1; n   return c; n } n template <class T> n T sqr(T x) { n   return x * x; n } n long long qpow(long long a, long long n, long long mod = MOD) { n   long long res(1); n   while (n) { n     if (n & 1) (res *= a) %= mod; n     (a *= a) %= mod; n     n >>= 1; n   } n   return res % mod; n } n template <class T> n void tomin(T& a, T b) { n   if (b < a) a = b; n } n template <class T> n void tomax(T& a, T b) { n   if (b > a) a = b; n } n }  // namespace _calculate n using namespace _calculate; n namespace _simple_algo { n long long stol(const string& s) { n   long long x = 0; n   for (char c : s) x = x * 10 + c - 48; n   return x; n } n string ltos(long long x) { n   string s =   ; n   if (x == 0) return  0 ; n   while (x) s = char(x % 10 + 48) + s, x /= 10; n   return s; n } n bool pal(const string& s) { n   int l = s.size(); n   for (int i = 0, j = l - 1; i < j; i++, j--) n     if (s[i] != s[j]) return 0; n   return 1; n } n }  // namespace _simple_algo n using namespace _simple_algo; n namespace _io { n template <class T> n void rd(T& x) { n   cin >> x; n } n long long rd() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n void rd(int& x) { x = rd(); } n void rd(long long& x) { x = rd(); } n template <class A, class B> n void rd(A& a, B& b) { n   rd(a), rd(b); n } n template <class A, class B> n void rd(pair<A, B>& p) { n   cin >> p.first >> p.second; n } n template <class T> n void wt(const T& x) { n   cout << x << endl; n } n template <class T> n void wt(const T& x, char c) { n   cout << x << c; n } n template <class T> n void wt(const T& x, const string& s) { n   cout << x << s; n } n template <class T> n void wt(const T& x, int rnd) { n   cout << fixed << setprecision(rnd) << x << endl; n } n template <class A, class B> n void wt(const pair<A, B>& make_pair) { n   cout << make_pair.first <<     << make_pair.second << endl; n } n template <class T> n void wt(const vector<T>& vec, int l, int r) { n   for (int i = l; i <= r; i++) cout << vec[i] <<    ; n   cout << endl; n } n }  // namespace _io n using namespace _io; n }  // namespace myland n using namespace myland; n const int N(2e5 + 5); n long long n, k; n long long a[N], d[N]; n int main() { n   rd(n, k); n   for (int i = 1; i <= n; i++) rd(a[i]), d[a[i] % k]++; n   long long ans(0); n   for (int i = 0; i <= k - 1; i++) { n     if (i == 0 || (k % 2 == 0 && i == k / 2)) { n       ans += d[i] / 2 * 2; n       continue; n     } n     long long t(min(d[i], d[k - i])); n     d[i] -= t; n     d[k - i] -= t; n     ans += 2 * t; n   } n   wt(ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 0, a[100001]; n   long long sum = 0, x = 0, ans = 0; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i], sum += a[i]; n   sum = sum / n; n   for (int i = 0; i < n; i++) x += a[i] - sum, ans += abs(x); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char a[440000], b[440000]; n int cnta = 0, cntb = 0; n int df[440000] = {0}; n int czl[440000], czr[440000]; n int zz1 = 0, zz2 = 0; n int ab[440000] = {0}, ba[440000] = {0}; n int ans = 0; n int main() { n   cin >> n; n   cin >> a >> b; n   for (int i = 0; i < n; i++) { n     if (a[i] ==  a ) { n       cnta++; n       if (a[i] != b[i]) ab[++zz1] = i + 1; n     } n     if (b[i] ==  a ) { n       cntb++; n       if (a[i] != b[i]) ba[++zz2] = i + 1; n     } n   } n   if ((zz1 + zz2) % 2 == 1) { n     cout << -1 << endl; n     return 0; n   } n   while (zz1 || zz2) { n     if (zz2 % 2 == 1) { n       zz2++; n       ans++; n       czl[ans] = ab[zz1], czr[ans] = ab[zz1]; n       ba[zz2] = ab[zz1]; n       zz1--; n     } n     if (zz1) { n       ans++; n       czl[ans] = ab[zz1], czr[ans] = ab[zz1 - 1]; n       zz1 -= 2; n     } else { n       ans++; n       czl[ans] = ba[zz2], czr[ans] = ba[zz2 - 1]; n       zz2 -= 2; n     } n   } n   cout << ans << endl; n   for (int i = 1; i <= ans; i++) cout << czl[i] <<     << czr[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n int vis[N]; n vector<int> a[N], tmp; n int main() { n   int n; n   scanf( %d , &n); n   int x = 1; n   for (int i = 2; i <= n; i++) { n     if (n - (n + i - 1) / i - i + 1 > n - (n + x - 1) / x - x + 1) x = i; n   } n   int ans = n - (n + x - 1) / x - x + 1; n   int cur = 0; n   for (int i = 1; i <= n; i++) { n     if (i % x == 1) cur++; n     a[cur].push_back(i); n   } n   int cnt = 0; n   while (cnt < ans) { n     for (int i = 1; i <= cur; i++) { n       int sz = a[i].size(); n       for (int j = 0; j < sz - 1; j++) { n         if (!vis[a[i][j]]) { n           vis[a[i][j]] = 1; n           tmp.push_back(a[i][j]); n         } n         if (tmp.size() == x) break; n       } n       if (tmp.size() == x) break; n     } n     int k = tmp.size(); n     cnt += k; n     printf( %d  , k); n     for (int j : tmp) printf( %d  , j); n     printf(  n ); n     fflush(stdout); n     tmp.clear(); n     int pos; n     scanf( %d , &pos); n     for (int i = 1; i <= k; i++) { n       if (vis[pos]) cnt--; n       vis[pos] = 0; n       pos = pos % n + 1; n     } n   } n   printf( 0 n ); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   bool a[m + 1]; n   memset(a, 0, sizeof(a)); n   for (int i = 1; i <= n; i++) { n     int l, r; n     cin >> l >> r; n     for (int j = l; j <= r; j++) a[j] = 1; n   } n   vector<int> v; n   int res = 0; n   for (int i = 1; i <= m; i++) { n     if (a[i] == 0) { n       res++; n       v.push_back(i); n     } n   } n   cout << res << endl; n   for (int i = 0; i < v.size(); i++) cout << v[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n struct a1 { n   int value, freq; n } arr[101]; n bool compare(a1 lhs, a1 rhs) { return lhs.freq > rhs.freq; } n int main() { n   int temp = 0, j, n, num = 0, m, i, a[101], ans = 0; n   scanf( %d %d , &n, &m); n   int b[n], c[n]; n   memset(a, 0, sizeof(a)); n   for (i = 0; i < n; i++) { n     scanf( %d , &temp); n     a[temp]++; n   } n   temp = 0; n   for (i = 1; i <= 100; i++) { n     if (a[i] != 0) { n       arr[num].freq = a[i]; n       arr[num++].value = i; n     } n   } n   sort(arr, arr + num, compare); n   for (i = 0; i < num; i++) { n     j = 0; n     while (j < arr[i].freq) { n       b[temp++] = arr[i].value; n       j++; n     } n   } n   for (i = arr[0].freq; i < n; i++) { n     c[i - arr[0].freq] = b[i]; n   } n   for (i = 0; i < arr[0].freq; i++) { n     c[n - arr[0].freq + i] = arr[0].value; n   } n   for (i = 0; i < n; i++) { n     if (b[i] != c[i]) ans++; n   } n   printf( %d n , ans); n   for (i = 0; i < n; i++) { n     printf( %d %d n , b[i], c[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class A> n void pr(A a) { n   cout << a; n   cout <<   n ; n } n template <class A, class B> n void pr(A a, B b) { n   cout << a <<    ; n   pr(b); n } n template <class A, class B, class C> n void pr(A a, B b, C c) { n   cout << a <<    ; n   pr(b, c); n } n template <class A, class B, class C, class D> n void pr(A a, B b, C c, D d) { n   cout << a <<    ; n   pr(b, c, d); n } n template <class A> n void PR(A a, long long n) { n   for (long long i = (long long)(0); i < (long long)(n); i++) { n     if (i) cout <<    ; n     cout << a[i]; n   } n   cout <<   n ; n } n long long check(long long n, long long m, long long x, long long y) { n   return x >= 0 && x < n && y >= 0 && y < m; n } n const long long MAX = 1e9 + 7, MAXL = 1LL << 61, dx[4] = {-1, 0, 1, 0}, n                 dy[4] = {0, 1, 0, -1}; n void Main() { n   int h1, m1, h2, m2; n   scanf( %d:%d , &h1, &m1); n   scanf( %d:%d , &h2, &m2); n   int d = h1 * 60 + m1 + h2 * 60 + m2; n   d /= 2; n   if (h1 * 60 + m1 > h2 * 60 + m2) d += 12 * 60; n   d %= 24 * 60; n   printf( %02d:%02d n , d / 60, d % 60); n } n int main() { n   Main(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n struct Node { n   int a, px, ori, c; n } node[N]; n int n, l, r; n bool cmp_px(Node &a, Node &b) { return a.px < b.px; } n bool cmp_ori(Node &a, Node &b) { return a.ori < b.ori; } n int main() { n   cin >> n >> l >> r; n   int t; n   for (int i = 0; i < n; i++) { n     cin >> t; n     node[i].ori = i; n     node[i].a = t * (-1); n     node[i].c = l; n   } n   for (int i = 0; i < n; i++) { n     cin >> t; n     node[i].px = t; n   } n   sort(node, node + n, cmp_px); n   for (int i = 1; i < n; i++) { n     if (node[i].a > node[i - 1].a) continue; n     int cha = node[i - 1].a - node[i].a; n     node[i].a = node[i].a + cha + 1; n     node[i].c += cha + 1; n     if (node[i].c > r) { n       cout << -1 << endl; n       return 0; n     } n   } n   { n     sort(node, node + n, cmp_ori); n     for (int i = 0; i < n; i++) { n       cout << node[i].c <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int sb, eb; n   vector<int> box; n   cin >> sb; n   for (int a = 0; a < sb; a++) { n     cin >> eb; n     box.push_back(eb); n   } n   sort(box.begin(), box.end()); n   for (int a = 0; a < sb; a++) { n     cout << box[a] <<    ; n   } n } n


#include <bits/stdc++.h> n void sc(int &x) { scanf( %d , &x); } n void sc(int &x, int &y) { scanf( %d%d , &x, &y); } n void sc(int &x, int &y, int &z) { scanf( %d%d%d , &x, &y, &z); } n void sc(long long &x) { scanf( %lld , &x); } n void sc(long long &x, long long &y) { scanf( %lld%lld , &x, &y); } n void sc(long long &x, long long &y, long long &z) { n   scanf( %lld%lld%lld , &x, &y, &z); n } n void sc(char *x) { scanf( %s , x); } n void sc(char *x, char *y) { scanf( %s%s , x, y); } n void sc(char *x, char *y, char *z) { scanf( %s%s%s , x, y, z); } n void out(int x) { printf( %d n , x); } n void out(long long x) { printf( %lld n , x); } n void out(int x, int y) { printf( %d %d n , x, y); } n void out(long long x, long long y) { printf( %lld %lld n , x, y); } n void out(int x, int y, int z) { printf( %d %d %d n , x, y, z); } n void out(long long x, long long y, long long z) { n   printf( %lld %lld %lld n , x, y, z); n } n void ast(long long x, long long l, long long r) { assert(x >= l && x <= r); } n using namespace std; n const int N = 666, mod = 998244353; n int k, a, h, rk[N], srk[N], g[N]; n vector<pair<int, int>> l1, l2, r1, r2; n void sol(int l, int r, vector<pair<int, int>> &s1, vector<pair<int, int>> &s2) { n   int len = r - l + 1; n   int tot = 0; n   while (len) { n     len /= 2; n     tot += len; n   } n   int up = 1 << tot; n   for (int i = 0; i < up; i++) { n     int cur = k + 1; n     vector<int> vc; n     for (int j = l; j <= r; j++) vc.push_back(j); n     int s = 0; n     int mul = 0; n     while (vc.size() > 1) { n       vector<int> gc; n       for (int j = 0; j < vc.size(); j += 2) { n         if (i >> s & 1) n           mul = (mul + 1ll * vc[j] * g[cur]) % mod, gc.push_back(vc[j + 1]); n         else n           mul = (mul + 1ll * vc[j + 1] * g[cur]) % mod, gc.push_back(vc[j]); n         s++; n       } n       vc = gc; n       cur--; n     } n     assert(s == tot); n     assert(cur == 2); n     assert(vc.size() == 1); n     s1.push_back({(mul + 1ll * vc[0] * g[1]) % mod, i}); n     s2.push_back({(mul + 1ll * vc[0] * g[2]) % mod, i}); n   } n } n int ans[N]; n int cal(int i, int l, int r) { n   vector<int> vc; n   for (int j = l; j <= r; j++) vc.push_back(j); n   int s = 0, cur = k + 1; n   while (vc.size() > 1) { n     vector<int> gc; n     for (int j = 0; j < vc.size(); j += 2) { n       if (i >> s & 1) n         ans[vc[j]] = rk[cur], gc.push_back(vc[j + 1]); n       else n         ans[vc[j + 1]] = rk[cur], gc.push_back(vc[j]); n       s++; n     } n     vc = gc; n     cur--; n   } n   return vc[0]; n } n void get(vector<pair<int, int>> &l, vector<pair<int, int>> &r, int rkl, n          int rkr) { n   sort(r.begin(), r.end()); n   for (pair<int, int> x : l) { n     int pos = lower_bound(r.begin(), r.end(), n                           make_pair((h + mod - x.first) % mod, 0)) - n               r.begin(); n     if (pos >= 0 && pos < r.size() && n         r[pos].first == (h + mod - x.first) % mod) { n       int a = cal(x.second, 1, 1 << (k - 1)), n           b = cal(r[pos].second, (1 << (k - 1)) + 1, 1 << k); n       ans[a] = rkl; n       ans[b] = rkr; n       for (int i = 1; i <= (1 << k); i++) n         printf(i == (1 << k) ?  %d n  :  %d  , ans[i]); n       exit(0); n     } n   } n } n void sol(int cas) { n   sc(k, a, h); n   for (int i = 1; i <= k + 1; i++) rk[i] = i - 2 >= 0 ? (1 << (i - 2)) + 1 : 1; n   for (int i = 1; i <= k + 1; i++) { n     g[i] = 1; n     for (int j = 1; j <= rk[i]; j++) g[i] = 1ll * g[i] * a % mod; n   } n   sol(1, 1 << (k - 1), l1, l2); n   sol((1 << (k - 1)) + 1, 1 << k, r1, r2); n   get(l1, r2, 1, 2); n   get(l2, r1, 2, 1); n   out(-1); n } n int main() { n   srand(time(0)); n   int t = 1, cas = 0; n   while (t--) { n     sol(++cas); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, cnt, k, m, r, ans; n int fa[21][100010], dep[100010], st[100010], ed[100010], h[100010]; n int f[100010][310]; n int t[100010]; n bool vis[100010]; n struct node { n   int id, num; n   bool operator<(const node &T) const { return num < T.num; } n } s[100010]; n vector<int> e[100010], Q; n inline void insert(int u, int v) { n   e[u].push_back(v); n   e[v].push_back(u); n } n int find(int x) { n   int sum = 0; n   for (; x; x -= (x & -x)) sum += t[x]; n   return sum; n } n void add(int x, int k) { n   for (; x <= n; x += (x & -x)) t[x] += k; n } n void dfs(int x) { n   st[x] = ++cnt; n   for (int i = 0; i < e[x].size(); i++) n     if (fa[0][x] != e[x][i]) { n       fa[0][e[x][i]] = x; n       dep[e[x][i]] = dep[x] + 1; n       dfs(e[x][i]); n     } n   ed[x] = cnt; n } n int lca(int a, int b) { n   int i, kk; n   if (dep[a] != dep[b]) { n     if (dep[a] < dep[b]) swap(a, b); n     i = 0; n     kk = 1; n     while (kk <= dep[a] - dep[b]) { n       i++; n       kk <<= 1; n     } n     while (dep[a] > dep[b]) { n       if (kk <= dep[a] - dep[b]) a = fa[i][a]; n       i--; n       kk >>= 1; n     } n   } n   if (a == b) return a; n   i = 0; n   while (fa[i][a] != fa[i][b]) i++; n   while (fa[0][a] != fa[0][b]) { n     if (fa[i][a] != fa[i][b]) { n       a = fa[i][a]; n       b = fa[i][b]; n     } n     i--; n   } n   return fa[0][a]; n } n int main() { n   int a, b; n   scanf( %d%d , &n, &q); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &a, &b); n     insert(a, b); n   } n   dfs(1); n   f[0][0] = 1ll; n   for (int i = 1; i <= 20; i++) n     for (int j = 1; j <= n; j++) fa[i][j] = fa[i - 1][fa[i - 1][j]]; n   while (q--) { n     scanf( %d%d%d , &k, &m, &r); n     Q.clear(); n     cnt = ans = 0; n     for (int j = 1; j <= k; j++) { n       scanf( %d , &a); n       Q.push_back(a); n       vis[a] = 1; n       add(st[a], 1); n       add(ed[a] + 1, -1); n     } n     for (int j = 0; j < k; j++) { n       a = Q[j]; n       b = lca(r, a); n       h[a] = find(st[a]) + find(st[r]) - 2 * find(st[b]) + vis[b] - 1; n       s[++cnt].id = a; n       s[cnt].num = h[a]; n     } n     sort(s + 1, s + 1 + cnt); n     for (int i = 0; i <= m; i++) f[1][i] = 0ll; n     for (int i = 1; i <= k; i++) n       for (int j = 1; j <= m; j++) n         f[i][j] = ((long long)f[i - 1][j] * (j - s[i].num) % 1000000007 + n                    f[i - 1][j - 1]) % n                   1000000007; n     for (int j = 1; j <= m; j++) ans = (ans + f[k][j]) % 1000000007; n     printf( %d n , ans); n     for (int j = 0; j < k; j++) { n       a = Q[j]; n       add(st[a], -1); n       add(ed[a] + 1, 1); n       vis[a] = 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n using namespace std; n long long binpow(long long base, long long exp, int mod) { n   long long res = 1; n   while (exp > 0) { n     if (exp % 2 == 1) res = (res * base) % mod; n     exp = exp >> 1; n     base = (base * base) % mod; n   } n   return res; n } n long long mod(long long x) { n   return ((x % 1000000007LL + 1000000007LL) % 1000000007LL); n } n long long add(long long a, long long b) { return mod(mod(a) + mod(b)); } n long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, m; n     cin >> n >> m; n     long long int maxdiff = -1e18; n     long long int maxele = -1e18; n     for (int i = 0; i < n; i++) { n       long long int l, r; n       cin >> l >> r; n       maxdiff = max(maxdiff, l - r); n       maxele = max(maxele, l); n     } n     long long int ans = 1; n     m -= maxele; n     if (m > 0) { n       if (maxdiff <= 0) { n         ans = -1; n       } else { n         ans += (m + maxdiff - 1) / maxdiff; n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const double PI = 3.141592653589793238460; n using namespace std; n vector<int> d; n vector<int> l; n vector<int> r; n vector<int> u; n bool isPossibleL(string st) { n   for (char ch : st) { n     for (int n : l) n       if (ch -  0  == n) return false; n   } n   return true; n } n bool isPossibleR(string st) { n   for (char ch : st) { n     for (int n : r) n       if (ch -  0  == n) return false; n   } n   return true; n } n bool isPossibleU(string st) { n   for (char ch : st) { n     for (int n : u) n       if (ch -  0  == n) return false; n   } n   return true; n } n bool isPossibleD(string st) { n   for (char ch : st) { n     for (int n : d) n       if (ch -  0  == n) return false; n   } n   return true; n } n int main() { n   l.push_back(1), l.push_back(4), l.push_back(7), l.push_back(0); n   r.push_back(3), r.push_back(6), r.push_back(9), r.push_back(0); n   d.push_back(7), d.push_back(0), d.push_back(9); n   u.push_back(1), u.push_back(2), u.push_back(3); n   int n; n   string st; n   cin >> n >> st; n   if (isPossibleL(st) || isPossibleR(st) || isPossibleU(st) || isPossibleD(st)) n     cout <<  NO ; n   else n     cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> statues(8); n vector<vector<char>> MAT(8, vector<char>(8)); n vector<vector<vector<bool>>> VIS(8, n                                  vector<vector<bool>>(8, n                                                       vector<bool>(10, false))); n vector<pair<int, int>> moveDir = {{0, 1},  {0, -1}, {1, 0},   {-1, 0}, {1, 1}, n                                   {-1, 1}, {1, -1}, {-1, -1}, {0, 0}}; n bool checkStatues(int r, int c, int level) { n   for (int i = 0; i < statues[c].size(); ++i) { n     if (statues[c][i] + level < 8 && statues[c][i] + level == r) return false; n   } n   return true; n } n bool isSafe(int r, int c, int level) { n   if (level > 8) level = 8; n   if (r < 0 || r >= 8 || c < 0 || c >= 8 || VIS[r][c][level]) return false; n   return checkStatues(r, c, level); n } n bool dfs(int r, int c, int level) { n   bool canMove = false; n   if (level > 8) level = 8; n   if (r == 0 && c == 7) return true; n   for (int i = 0; i < 9; ++i) { n     int newR = r + moveDir[i].first; n     int newC = c + moveDir[i].second; n     if (isSafe(newR, newC, level + 1) && checkStatues(newR, newC, level)) { n       VIS[r][c][level] = true; n       canMove = canMove || dfs(newR, newC, level + 1); n     } n   } n   return canMove; n } n int main() { n   for (int i = 0; i < 8; ++i) { n     for (int j = 0; j < 8; ++j) { n       cin >> MAT[i][j]; n       if (MAT[i][j] ==  S ) statues[j].push_back(i); n     } n   } n   bool canWin = dfs(7, 0, 0); n   cout << ((canWin) ?  WIN  :  LOSE ) <<   n ; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n const double eps = 10e-10; n const int mod = 1000000007; n const int maxn = 300005; n const int maxm = 10005; n pair<int, int> ans[maxn]; n int main() { n   int n, m, a; n   while (~scanf( %d%d , &n, &m)) { n     priority_queue<pair<int, int> > q; n     for (int i = (0); i < (n); i++) n       scanf( %d , &a), q.push(make_pair(a, i + 1)); n     int cnt = 0; n     while (!q.empty()) { n       stack<pair<int, int> > st; n       pair<int, int> cur = q.top(); n       q.pop(); n       while (!q.empty() && cur.first) { n         pair<int, int> cc = q.top(); n         q.pop(); n         if (cc.first >= 1) n           cc.first--, cur.first--, n               ans[cnt++] = make_pair(cc.second, cur.second); n         if (cc.first) st.push(cc); n       } n       if (cur.first) break; n       while (!st.empty()) q.push(st.top()), st.pop(); n     } n     if (cnt < m / 2) { n       puts( No ); n       continue; n     } n     printf( Yes n%d n , m / 2); n     for (int i = (0); i < (cnt); i++) n       printf( %d %d n , ans[i].first, ans[i].second); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M, neg, nsus; n int ar[100002]; n int cnt[100002]; n int sus[100002]; n int main() { n   scanf( %d%d , &N, &M); n   for (int i = 1; i <= N; i++) { n     scanf( %d , &ar[i]); n     if (ar[i] > 0) n       cnt[ar[i]]++; n     else { n       cnt[-ar[i]]--; n       neg++; n     } n   } n   for (int i = 1; i <= N; i++) n     if (cnt[i] + neg == M) { n       sus[i] = 1; n       nsus++; n     } n   for (int i = 1; i <= N; i++) { n     if ((ar[i] < 0 && !sus[-ar[i]]) || (ar[i] > 0 && sus[ar[i]] && nsus == 1)) n       printf( Truth n ); n     else if ((ar[i] < 0 && sus[-ar[i]] && nsus == 1) || n              (ar[i] > 0 && !sus[ar[i]])) n       printf( Lie n ); n     else n       printf( Not defined n ); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int fib; n   cin >> fib; n   cout << fib <<     << 0 <<     << 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 1e2 + 10; n const long long mod = 1e9 + 31; n mt19937 mtrand(time(0)); n int n, m; n char s[2][1 << 12]; n int mp[2][1 << 14]; n int f[1 << 15], ans, pr[1 << 15]; n int unfd(int x) { n   int r = x; n   while (r != f[r]) r = f[r]; n   int t; n   while (f[x] != r) t = f[x], f[x] = r, x = t; n   return r; n } n void unjoin(int x, int y) { n   int fx = unfd(x), fy = unfd(y); n   if (fx != fy) { n     --ans; n     f[fx] = fy; n   } n } n int main() { n   int num; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     int id = i & 1, nid = id ^ 1; n     scanf( %s , s[id]); n     num = 0; n     for (int j = 0; j < m / 4; ++j) { n       if (s[id][j] >=  0  && s[id][j] <=  9 ) n         num = s[id][j] -  0 ; n       else n         num = s[id][j] -  A  + 10; n       for (int k = 0; k < 4; ++k) { n         mp[id][j * 4 + k] = (num >> (3 - k)) & 1; n         if (mp[id][j * 4 + k] == 1) ++ans; n       } n     } n     for (int j = m; j < m * 2; ++j) f[j] = j; n     if (i > 0) { n       for (int j = 0; j < m; ++j) { n         if (mp[nid][j] & mp[id][j]) unjoin(j, m + j); n       } n     } n     for (int j = 1; j < m; ++j) { n       if (mp[id][j - 1] & mp[id][j]) unjoin(m + j - 1, m + j); n     } n     for (int j = m; j < m * 2; ++j) { n       pr[unfd(j)] = j - m; n     } n     for (int j = 0; j < m; ++j) { n       f[j] = pr[unfd(j + m)]; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e5 + 10; n const long long INF = 1e18; n const long long mod = 1e9 + 7; n long long poww(long long a, long long b) { n   if (b == 0) return 1; n   long long x = poww(a, b / 2) % mod; n   if (b & 1) n     return (((x * x) % mod) * a) % mod; n   else n     return (x * x) % mod; n } n long long n, m, k, dis[maxn], mark[maxn], p[maxn], ans1 = 0, ans2 = 0; n vector<long long> g[maxn], g2[maxn]; n void bfs(long long v) { n   queue<long long> qu; n   for (int i = 1; i <= n; i++) { n     mark[i] = 0; n     dis[i] = INF; n   } n   mark[v] = 1; n   dis[v] = 0; n   qu.push(v); n   while (qu.size()) { n     long long u = qu.front(); n     qu.pop(); n     for (auto uu : g2[u]) { n       if (!mark[uu]) { n         mark[uu] = 1; n         dis[uu] = dis[u] + 1; n         qu.push(uu); n       } n     } n   } n } n int main() { n   std::ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     long long v, u; n     cin >> v >> u; n     g[v].push_back(u); n     g2[u].push_back(v); n   } n   cin >> k; n   for (int i = 1; i <= k; i++) cin >> p[i]; n   long long t = p[k]; n   bfs(t); n   for (int i = 1; i < k; i++) { n     long long minn = INF, te = 0; n     for (auto u : g[p[i]]) { n       if (dis[u] < minn) { n         minn = dis[u]; n         te = 1; n       } else if (dis[u] == minn) n         te++; n     } n     long long to = p[i + 1]; n     if (dis[to] > minn) { n       ans1++; n       ans2++; n     } else { n       if (te > 1) ans2++; n     } n   } n   cout << ans1 <<     << ans2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b, c, d; n     cin >> a >> b >> c >> d; n     if (a == 0 || b == 0 || c == 0) { n       int cnt = 0; n       if (a % 2 == 1) cnt++; n       if (b % 2 == 1) cnt++; n       if (c % 2 == 1) cnt++; n       if (d % 2 == 1) cnt++; n       if (cnt < 2) n         cout <<  Yes  << endl; n       else n         cout <<  No  << endl; n       continue; n     } n     int cnt = 0; n     if (a % 2 == 1) cnt++; n     if (b % 2 == 1) cnt++; n     if (c % 2 == 1) cnt++; n     if (d % 2 == 1) cnt++; n     if (cnt == 1 || cnt == 0) n       cout <<  Yes  << endl; n     else { n       a--; n       b--; n       c--; n       d += 3; n       int cnt = 0; n       if (a % 2 == 1) cnt++; n       if (b % 2 == 1) cnt++; n       if (c % 2 == 1) cnt++; n       if (d % 2 == 1) cnt++; n       if (cnt == 1 || cnt == 0) n         cout <<  Yes  << endl; n       else n         cout <<  No  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, sum = 0; n     long long ans = 0; n     cin >> n; n     char s[n]; n     cin >> (s + 1); n     map<int, int> f; n     f[0] = 1; n     for (int i = 1; i <= n; i++) { n       int tmp = s[i] -  0 ; n       sum += tmp; n       ans += f[sum - i]; n       f[sum - i]++; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, a[300005], b1[500005], b2[500005], n     cnt = 0, pl[300005], c[300005], ans = 0, plc[300005], m, d, can[300005], r, n     ne[300005]; n pair<int, int> p; n pair<int, int> p1; n pair<int, int> p2; n map<pair<int, int>, int> m1; n string s; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     pl[a[i]] = i; n     can[i] = 0; n     ne[i] = i + 1; n   } n   for (int i = 0; i < m; i++) { n     cin >> b1[i] >> b2[i]; n     p.first = b1[i]; n     p.second = b2[i]; n     m1[p] = 1; n     if (b2[i] == a[n]) { n       c[cnt] = b1[i]; n       plc[cnt] = pl[b1[i]]; n       cnt++; n     } n   } n   sort(plc, plc + cnt); n   for (int i = 0; i < cnt; i++) { n     c[i] = a[plc[i]]; n   } n   for (int i = cnt - 1; i >= 0; i--) { n     d = 1; n     r = ne[pl[c[i]]]; n     while (r < n) { n       p1.first = c[i]; n       p1.second = a[r]; n       if (m1[p1] == 0) { n         d = 0; n         break; n       } n       r = ne[r]; n     } n     if (d) { n       ans++; n       if (c[i] > 0) { n         ne[pl[c[i]] - 1] = ne[pl[c[i]]]; n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n struct BIT { n   int n; n   vector<T> dat; n   BIT(int n = 0) { initialize(n); } n   void initialize(int nin) { n     n = nin; n     dat.resize(n, 0); n   } n   T sum(int i) { n     T s = 0; n     while (i >= 0) { n       s += dat[i]; n       i = (i & (i + 1)) - 1; n     } n     return s; n   } n   T sum_between(int i, int j) { return sum(j) - sum(i - 1); } n   void plus(int i, T x) { n     while (i < n) { n       dat[i] += x; n       i |= i + 1; n     } n   } n   int lower_bound(T x) { n     if (x < 0) return -1; n     if (sum(n - 1) < x) return n; n     int ret = -1; n     int k = 1; n     while (2 * k <= n) k <<= 1; n     for (; k > 0; k >>= 1) { n       if (ret + k < n && dat[ret + k] < x) { n         x -= dat[ret + k]; n         ret += k; n       } n     } n     return ret + 1; n   } n }; n void solve() { n   string S; n   cin >> S; n   vector<pair<int, int>> D; n   for (int i = 0; i < int(S.size()) - 1; i++) n     if (S[i] == S[i + 1]) { n       D.emplace_back(S[i] -  a , i + 1); n     } n   int N = D.size(); n   vector<int> num(26); n   BIT<int> bit[26], bit_sum(N); n   for (int k = 0; k < 26; k++) bit[k].initialize(N); n   for (int i = 0; i < N; i++) { n     int al = D[i].first; n     num[al]++; n     bit[al].plus(i, 1); n     bit_sum.plus(i, D[i].second - (i ? D[i - 1].second : 0)); n   } n   int sum = N; n   int total = S.size(); n   vector<pair<int, int>> ans; n   while (true) { n     int mx_al = max_element(num.begin(), num.end()) - num.begin(); n     if (num[mx_al] == sum) break; n     int fst = bit[mx_al].lower_bound(1); n     int r = N, l = -1; n     for (int k = 0; k < 26; k++) n       if (k != mx_al) { n         int pos = bit[k].lower_bound(bit[k].sum(fst) + 1); n         r = min(r, pos); n       } n     if (r == N) { n       r = fst; n       for (int k = 0; k < 26; k++) n         if (k != mx_al) { n           int pos = bit[k].lower_bound(bit[k].sum(fst)); n           l = max(l, pos); n         } n     } else { n       l = bit[mx_al].lower_bound(bit[mx_al].sum(r)); n     } n     int LL = bit_sum.sum(l); n     int RR = bit_sum.sum(r); n     ans.emplace_back(LL, RR); n     bit_sum.plus(l + 1, LL - RR); n     total -= RR - LL; n     sum -= 2; n     num[D[l].first]--; n     num[D[r].first]--; n     bit[D[l].first].plus(l, -1); n     bit[D[r].first].plus(r, -1); n   } n   for (int i = 0; i < N; i++) { n     int s = bit[D[i].first].sum_between(i, i); n     if (s > 0) { n       int rem = bit_sum.sum(i); n       ans.emplace_back(0, rem); n       bit_sum.plus(0, -rem); n       total -= rem; n     } n   } n   ans.emplace_back(0, total); n   printf( %d n , int(ans.size())); n   for (auto& p : ans) printf( %d %d n , p.first + 1, p.second); n } n int main() { n   int T; n   cin >> T; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr[n]; n   int orig[n]; n   int product = 1; n   int counter = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     orig[i] = arr[i]; n     if (arr[i] >= 0) { n       arr[i] = -arr[i] - 1; n     } n     if (arr[i] < 0) { n       counter++; n     } n   } n   if (counter % 2 == 0) { n     for (int i = 0; i < n; i++) { n       cout << arr[i] <<    ; n     } n   } else { n     int maxAbsVal = 0; n     int maxIndex = 0; n     for (int i = 0; i < n; i++) { n       if (abs(arr[i]) >= maxAbsVal) { n         maxAbsVal = abs(arr[i]); n         maxIndex = i; n       } n     } n     if (arr[maxIndex] == orig[maxIndex]) { n       arr[maxIndex] = -arr[maxIndex] - 1; n     } else { n       arr[maxIndex] = orig[maxIndex]; n     } n     for (int i = 0; i < n; i++) { n       cout << arr[i] <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n struct node { n   int index, val, team; n }; n node ni[N]; n int divi[N], rest[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     ni[i].index = i; n     scanf( %d , &ni[i].val); n   } n   sort(ni, ni + n, [](const node &a, const node &b) { return a.val < b.val; }); n   fill(begin(divi), end(divi), -0x3f3f3f3f); n   for (int i = 3; i < n; ++i) { n     divi[i] = rest[i - 3] + ni[i].val - ni[i - 1].val; n     rest[i - 2] = max(divi[i - 2], rest[i - 3]); n   } n   int k = 1; n   for (int i = n - 1; i >= n - 3; --i) { n     ni[i].team = k; n   } n   for (int i = n - 3; i > 0;) { n     if (rest[i] == divi[i]) { n       ++k; n       for (int j = i; j > 0 && j > i - 3; --j) { n         ni[j - 1].team = k; n       } n       i -= 3; n     } else { n       i -= 1; n       ni[i].team = k; n     } n   } n   printf( %d %d n , ni[n - 1].val - ni[0].val - rest[n - 3], k); n   sort(ni, ni + n, n        [](const node &a, const node &b) { return a.index < b.index; }); n   for (int i = 0; i < n; ++i) { n     printf( %d  , ni[i].team); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m, i; n   float x, max = 0.0; n   scanf( %d%d , &n, &m); n   int s[n][2]; n   for (i = 0; i < n; i++) { n     scanf( %d%d , &s[i][0], &s[i][1]); n     x = (float)(s[i][0]) / s[i][1]; n     if (max == 0.0) { n       max = x; n     } n     if (max > x) { n       max = x; n     } n   } n   x = max * m; n   printf( %.8f n , x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int t, next; n } a[800010]; n struct hhhh { n   int l, r; n } h[200010]; n vector<pair<int, int> > v[400010]; n vector<int> g[400010]; n long long ans[400010]; n int fa[400010], son[400010], dep[400010], top[400010], size[400010]; n int fat[400010][20], head[400010], id[400010], c[400010], len[400010], n, m, tt, n     tot; n char s[400010]; n inline int rd() { n   int x = 0; n   char ch = getchar(); n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     ; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   return x; n } n inline void print(long long x) { n   static char s[233]; n   if (!x) { n     putchar( 0 ); n     putchar(  n ); n     return; n   } n   int tot = 0; n   for (; x; x /= 10) s[++tot] = x % 10 +  0 ; n   for (; tot; tot--) putchar(s[tot]); n   putchar(  n ); n } n inline void add(int x, int y) { n   a[++tot].t = y; n   a[tot].next = head[x]; n   head[x] = tot; n } n namespace sam { n int ch[400010][26], fa[400010], len[400010], tot, last; n inline void init() { tot = last = 1; } n inline int insert(int x) { n   int p = last, np = ++tot; n   len[np] = len[p] + 1; n   for (; p && !ch[p][x]; p = fa[p]) ch[p][x] = np; n   if (!p) n     fa[np] = 1; n   else { n     int q = ch[p][x]; n     if (len[q] == len[p] + 1) n       fa[np] = q; n     else { n       int nq = ++tot; n       memcpy(ch[nq], ch[q], sizeof(ch[q])); n       len[nq] = len[p] + 1; n       fa[nq] = fa[q]; n       fa[q] = fa[np] = nq; n       for (; p && ch[p][x] == q; p = fa[p]) ch[p][x] = nq; n     } n   } n   return last = np; n } n inline void build() { n   for (int i = 2; i <= tot; i++) add(fa[i], i); n } n }  // namespace sam n inline void dfs1(int x) { n   size[x] = 1; n   for (int i = head[x]; i; i = a[i].next) { n     int t = a[i].t; n     if (t == fa[x]) continue; n     dep[t] = dep[x] + 1; n     fa[t] = x; n     dfs1(t); n     size[x] += size[t]; n     if (size[t] > size[son[x]]) son[x] = t; n   } n } n inline void dfs2(int x, int tp) { n   top[x] = tp; n   if (son[x]) dfs2(son[x], tp); n   for (int i = head[x]; i; i = a[i].next) { n     int t = a[i].t; n     if (t == fa[x] || t == son[x]) continue; n     dfs2(t, t); n   } n } n struct bit { n   long long c[200010]; n   inline void add(int x, long long y) { n     for (; x <= n; x += ((x) & (-(x)))) c[x] += y; n   } n   inline void modify(int l, int r, long long hh) { n     add(l, hh); n     add(r + 1, -hh); n   } n   inline long long query(int x) { n     long long res = 0; n     for (; x > 0; x -= ((x) & (-(x)))) res += c[x]; n     return res; n   } n   inline long long query(int l, int r) { return query(r) - query(l - 1); } n } c1, c2, c3, c4, c5; n inline void work() { n   int hh = len[fa[c[1]]] + 1; n   for (int i = 1; i <= tt; i++) { n     int x = c[i]; n     for (int t : g[x]) { n       int k = t + hh - 1; n       c1.add(k, 1); n       c2.add(k, k); n     } n   } n   for (int i = 1; i <= tt; i++) { n     int x = c[i], now = len[x] - hh + 1; n     for (pair<int, int> hhh : v[x]) { n       int t = hhh.first, now = hhh.second; n       int l = h[t].l, r = h[t].r; n       ans[t] += c1.query(r - now) * now; n       ans[t] += c1.query(r - now + 1, r) * (r + 1) - c2.query(r - now + 1, r); n       ans[t] += c3.query(r) + c4.query(r) * r - c5.query(r); n     } n     for (int t : g[x]) { n       int k = t + hh - 1; n       c1.add(k, -1); n       c2.add(k, -k); n       c3.add(k + now, now); n       c4.modify(k, k + now - 1, 1); n       c5.modify(k, k + now - 1, k - 1); n     } n   } n   for (int i = 1; i <= tt; i++) { n     int x = c[i], now = len[x] - hh + 1; n     for (int t : g[x]) { n       int k = t + hh - 1; n       c3.add(k + now, -now); n       c4.modify(k, k + now - 1, -1); n       c5.modify(k, k + now - 1, -k + 1); n       c1.add(t, 1); n     } n   } n   for (int i = 1; i <= tt; i++) { n     int x = c[i], now = len[x] - hh + 1; n     for (pair<int, int> hhh : v[x]) { n       int t = hhh.first, now = hhh.second; n       int l = h[t].l, r = h[t].r; n       ans[t] -= c1.query(l - 1) * now; n       ans[t] -= c2.query(l - 1); n     } n     for (int t : g[x]) c1.add(t, -1), c2.add(t, now); n   } n   for (int i = 1; i <= tt; i++) n     for (int t : g[c[i]]) c2.add(t, -(len[c[i]] - hh + 1)); n } n inline int jump(int x, int y) { n   for (int i = 19; ~i; i--) n     if (len[fat[x][i]] >= y) x = fat[x][i]; n   return x; n } n inline void gao() { n   for (int i = 1; i <= sam::tot; i++) n     if (top[i] == i) { n       tt = 0; n       for (int j = i; j; j = son[j]) n         if (j > 1) c[++tt] = j; n       work(); n     } n } n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   m = rd(); n   sam::init(); n   for (int i = n; i; i--) id[i] = sam::insert(s[i] -  a ); n   sam::build(); n   for (int i = 1; i <= sam::tot; i++) len[i] = sam::len[i]; n   dfs1(1); n   dfs2(1, 1); n   for (int i = 1; i <= sam::tot; i++) fat[i][0] = fa[i]; n   for (int j = 1; j < 20; j++) n     for (int i = 1; i <= sam::tot; i++) fat[i][j] = fat[fat[i][j - 1]][j - 1]; n   for (int i = 1; i <= m; i++) h[i].l = rd(), h[i].r = rd(); n   for (int i = 1; i <= m; i++) { n     int hh = jump(id[h[i].l], h[i].r - h[i].l + 1); n     v[hh].push_back( n         pair<int, int>(i, (h[i].r - h[i].l + 1) - len[fa[top[hh]]])); n     for (int j = fa[top[hh]]; j; j = fa[top[j]]) n       v[j].push_back(pair<int, int>(i, len[j] - len[fa[top[j]]])); n   } n   for (int i = 1; i <= n; i++) n     for (int j = id[i]; j; j = fa[top[j]]) g[j].push_back(i); n   gao(); n   for (int i = 1; i <= m; i++) print(ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> h; n vector<vector<int> > g; n int h0, v0; n vector<int> val; n int it = 0; n vector<bool> used; n vector<int> treesum, par; n vector<int> treemin; n void update(int v, int l, int r, int ind, int d) { n   if (l == r) { n     treesum[v] += d; n     treemin[v] += d; n     return; n   } n   if (ind <= (r + l) / 2) { n     update(v * 2, l, (r + l) / 2, ind, d); n   } else { n     update(v * 2 + 1, (r + l) / 2 + 1, r, ind, d); n   } n   treesum[v] = treesum[v * 2] + treesum[v * 2 + 1]; n   treemin[v] = min(treemin[v * 2], treesum[v * 2] + treemin[v * 2 + 1]); n } n int query(int v, int l, int r, int x) { n   if (l == r) { n     return l; n   } n   if (treemin[v * 2] + x < 0) { n     return query(v * 2, l, (r + l) / 2, x); n   } n   return query(v * 2 + 1, (r + l) / 2 + 1, r, treesum[v * 2] + x); n } n void dfs1(int v, int p = -1) { n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     if (to != p) { n       val[to] = min(val[v], h[v]); n       par[to] = v; n       dfs1(to, v); n     } n   } n } n vector<int> uniq; n int m; n int mass[1000000]; n void dfs2(int v) { n   mass[++it] = v; n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     if (to != par[v]) { n       if (val[to] < h[to]) { n         dfs2(to); n       } n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n; n   used.resize(n); n   g.resize(n); n   h.resize(n); n   par.resize(n, -1); n   val.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> h[i]; n     uniq.push_back(h[i]); n   } n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   cin >> k; n   vector<int> s(k); n   for (int i = 0; i < k; i++) { n     cin >> s[i]; n     uniq.push_back(s[i]); n   } n   sort(uniq.begin(), uniq.end()); n   m = unique(uniq.begin(), uniq.end()) - uniq.begin(); n   treesum.resize(4 * m, 0); n   treemin.resize(4 * m, 0); n   for (int i = 0; i < n; i++) { n     h[i] = lower_bound(uniq.begin(), uniq.begin() + m, h[i]) - uniq.begin(); n   } n   for (int i = 0; i < k; i++) { n     s[i] = lower_bound(uniq.begin(), uniq.begin() + m, s[i]) - uniq.begin(); n   } n   val[0] = 1e9; n   dfs1(0); n   for (int i = 0; i < n; i++) { n     update(1, 0, m - 1, m - min(val[i], h[i]) - 1, 1); n   } n   for (int i = 0; i < k; i++) { n     update(1, 0, m - 1, m - s[i] - 1, -1); n   } n   if (treemin[1] >= 0) { n     cout << 0; n     return 0; n   } n   int ans = 1e9; n   vector<int> b(n); n   for (int i = 0; i < n; i++) { n     if (h[i] < val[i]) { n       it = 0; n       dfs2(i); n       update(1, 0, m - 1, m - h[i] - 1, -it); n       int t = m - query(1, 0, m - 1, 0) - 1; n       if (t <= val[i]) { n         b[i] = t; n         update(1, 0, m - 1, m - t - 1, 1); n         for (int j = 2; j <= it; j++) { n           b[mass[j]] = min(h[mass[j]], b[par[mass[j]]]); n           update(1, 0, m - 1, m - b[mass[j]] - 1, 1); n         } n         if (treemin[1] >= 0) { n           ans = min(ans, uniq[t] - uniq[h[i]]); n         } n         for (int j = 1; j <= it; j++) { n           update(1, 0, m - 1, m - b[mass[j]] - 1, -1); n         } n       } n       update(1, 0, m - 1, m - h[i] - 1, it); n     } n   } n   if (ans == 1e9) { n     cout << -1; n   } else { n     cout << ans; 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<int> l(n), r(n); n   for (int i = 0; i < n; i++) { n     cin >> l[i] >> r[i]; n   } n   int a = -1, b = -1; n   bool ok = true; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (!((l[i] <= l[j] && r[i] <= r[j]) || (l[j] <= l[i] && r[j] <= l[i]))) { n         if (a == -1) { n           a = i; n           b = j; n         } else { n           if (a == i && j == b) n             continue; n           else if (a == j && b == i) n             continue; n           else if (i == a && j != b) { n             b = -1; n           } else if (i == b && j != a) { n             a = i; n             b = -1; n           } else if (j == a && i != b) { n             b = -1; n           } else if (j == b && i != a) { n             a = j; n             b = -1; n           } else { n             ok = false; n           } n         } n       } n     } n   } n   if (!ok) n     cout <<  0 n ; n   else if (a == -1 && b == -1) { n     cout << n <<   n ; n     for (int i = 0; i < n; i++) { n       cout << i + 1 <<    ; n     } n     cout <<   n ; n   } else if (b == -1) { n     cout <<  1 n  << a + 1 <<   n ; n   } else { n     if (a > b) swap(a, b); n     cout <<  2 n  << a + 1 <<     << b + 1 <<   n ; n   } 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   cout.tie(0); n   int s = 0; n   for (int i = 0; i < 5; i++) { n     int x; n     cin >> x; n     s += x; n   } n   if (s % 5 || !s) n     cout <<  -1 ; n   else n     cout << s / 5; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000 + 3; n int N, Q, K; n long long a[MAXN], sum[MAXN], save[MAXN]; n int main() { n   scanf( %d , &N); n   memset((save), (-1), sizeof(save)); n   for (int i = 1; i <= N; ++i) scanf( %lld , &a[i]); n   sort(a + 1, a + 1 + N); n   for (int i = 1; i <= N; ++i) sum[i] = sum[i - 1] + a[i]; n   scanf( %d , &Q); n   while (Q--) { n     scanf( %d , &K); n     if (save[K] != -1) { n       printf( %lld n , save[K]); n       continue; n     } n     long long ans = 0, deep = 1, deep_size = K, now = N - 1; n     while (now > 0) { n       long long next = max(0ll, now - deep_size); n       ans += (sum[now] - sum[next]) * deep; n       ++deep; n       now -= deep_size; n       deep_size *= K; n     } n     save[K] = ans; n     printf( %lld , ans); n     putchar(Q ?     :   n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt; n string s; n stack<int> st; n int main() { n   cin >> s; n   for (int i = 0; i < s.size(); ++i) { n     if (s[i] ==  (  || s[i] ==  [  || s[i] ==  {  || s[i] ==  < ) { n       st.push(int(s[i])); n     } else { n       if (st.empty()) { n         cout <<  Impossible ; n         return 0; n       } n       if (st.top() == 40) { n         if (int(s[i]) != 41) { n           cnt++; n         } n         if (!st.empty()) st.pop(); n       } else { n         if (st.top() + 2 != int(s[i])) { n           cnt++; n         } n         if (!st.empty()) st.pop(); n       } n     } n   } n   if (!st.empty()) { n     cout <<  Impossible ; n     return 0; n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int la, lb, cnt[20], ans[25]; n char a[25], b[25]; n void find(int v, bool flag) { n   if (v > lb) { n     for (int i = 1; i <= lb; i++) printf( %d , ans[i]); n     exit(0); n   } n   if (flag && cnt[b[v] -  0 ]) { n     cnt[b[v] -  0 ]--; n     ans[v] = b[v] -  0 ; n     find(v + 1, 1); n     ans[v] = 0; n     cnt[b[v] -  0 ]++; n   } n   for (int i = (flag ? b[v] -  0  - 1 : 9); i >= 0; i--) n     if (cnt[i]) { n       ans[v] = i; n       cnt[i]--; n       find(v + 1, 0); n       ans[v] = 0; n       cnt[i]++; n     } n } n int main() { n   scanf( %s%s , a + 1, b + 1); n   la = strlen(a + 1), lb = strlen(b + 1); n   for (int i = 1; i <= la; i++) cnt[a[i] -  0 ]++; n   if (lb > la) { n     lb = la; n     for (int i = 1; i <= lb; i++) b[i] =  9 ; n   } n   find(1, 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, ans, fact[200005]; n long long pangkat(long long a, long long b) { n   if (b == 0) return 1; n   long long temp = pangkat(a, b / 2); n   if (b % 2 == 0) return (temp * temp) % 1000000007; n   return (((temp * temp) % 1000000007) * a) % 1000000007; n } n long long comb(long long a, long long b) { n   long long temp = (fact[b] * fact[a - b]) % 1000000007; n   return (fact[a] * pangkat(temp, 1000000007 - 2)) % 1000000007; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   fact[0] = 1; n   for (int i = 1; i <= 2 * n; i++) fact[i] = (fact[i - 1] * i) % 1000000007; n   ans = 2 * comb(2 * n - 1, n) - n; n   cout << ans % 1000000007 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 48; n long long s, x, xCopy, sCopy, pos[MAXN][2], result; n int n, bitS, bitX; n int main() { n   cin >> s >> x; n   pos[0][0] = 1; n   pos[0][1] = 0; n   n = 0; n   xCopy = x; n   sCopy = s; n   while (x || s) { n     n++; n     bitS = s % 2; n     bitX = x % 2; n     if (bitS == 0 && bitX == 0) { n       pos[n][0] = pos[n - 1][0]; n       pos[n][1] = pos[n - 1][0]; n     } else { n       if (bitS == 0 && bitX == 1) { n         pos[n][0] = 0; n         pos[n][1] = 2 * pos[n - 1][1]; n       } else { n         if (bitS == 1 && bitX == 0) { n           pos[n][0] = pos[n - 1][1]; n           pos[n][1] = pos[n - 1][1]; n         } else { n           pos[n][0] = 2 * pos[n - 1][0]; n           pos[n][1] = 0; n         } n       } n     } n     s >>= 1; n     x >>= 1; n   } n   result = pos[n][0]; n   if ((sCopy ^ 0) == xCopy) result -= 2; n   cout << result; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 500100; n const int root = 800; n long long lazy[root]; n unordered_set<long long> s[root]; n long long v[MAXN]; n signed main() { n   memset(lazy, 0, sizeof lazy); n   long long n, m; n   scanf( %lld %lld , &n, &m); n   for (long long i = 0; i < n; i++) n     scanf( %lld , &v[i]), s[i / root].insert(v[i]); n   for (long long i = 1; i <= m; i++) { n     long long t; n     scanf( %lld , &t); n     if (t == 1) { n       long long l, r, x; n       scanf( %lld %lld %lld , &l, &r, &x); n       l -= 1, r -= 1; n       s[l / root].clear(); n       for (long long j = (l / root) * root; j < min((l / root + 1) * root, n); n            j++) { n         if (j >= l && j <= r) v[j] += x; n         s[l / root].insert(v[j]); n       } n       if (l / root == r / root) continue; n       for (long long j = l / root + 1; j < r / root; j++) { n         lazy[j] += x; n       } n       s[r / root].clear(); n       for (long long j = (r / root) * root; j < min((r / root + 1) * root, n); n            j++) { n         if (j <= r && j >= l) v[j] += x; n         s[r / root].insert(v[j]); n       } n     } n     if (t == 2) { n       long long y, ini, fim; n       scanf( %lld , &y); n       bool flag = false; n       for (long long j = 0; j < root; j++) { n         if (s[j].find(y - lazy[j]) != s[j].end()) { n           flag = true; n           for (long long k = j * root; k < (j + 1) * root; k++) n             if (v[k] == y - lazy[j]) { n               ini = k; n               break; n             } n           break; n         } n       } n       if (flag == false) { n         printf( -1 n ); n         continue; n       } n       for (long long j = root - 1; j >= 0; j--) { n         if (s[j].find(y - lazy[j]) != s[j].end()) { n           flag = true; n           for (long long k = min((j + 1) * root - 1, n - 1); k >= j * root; n                k--) { n             if (v[k] == y - lazy[j]) { n               fim = k; n               break; n             } n           } n           break; n         } n       } n       printf( %lld n , fim - ini); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i = 0; n   char a; n   char A[] =  hello ; n   while (cin >> a && a !=   n ) { n     if (a == A[i]) i++; n   } n   if (i == 5) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; n int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; n const int mod = 1e9 + 7; n int dcmp(long double x, long double y) { n   return fabs(x - y) <= 1e-12 ? 0 : x < y ? -1 : 1; n } n void fast() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n } n int n; n map<string, int> score; n map<int, vector<pair<string, int> > > ft; n int main() { n   fast(); n   cin >> n; n   string s; n   int x, mx = 0; n   for (int i = 0; i < n; i++) { n     cin >> s >> x; n     score[s] += x; n     ft[score[s]].push_back({s, i}); n   } n   for (auto e : score) { n     mx = max(mx, e.second); n   } n   int mn = 2e9; n   string wn =   ; n   for (auto &e : ft) { n     if (e.first < mx) continue; n     for (int i = 0; i < e.second.size(); i++) { n       if (score[e.second[i].first] == mx) { n         if (mn > e.second[i].second) { n           mn = e.second[i].second; n           wn = e.second[i].first; n         } n       } n     } n   } n   cout << wn <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct fenwick { n   int N; n   vector<int> sum; n   void init(int n) { n     N = n + 1; n     sum.assign(N, 0); n   } n   void add(int i, int x) { n     for (i++; i < N; i += i & -i) { n       sum[i] += x; n     } n   } n   int get(int i) { n     int tot = 0; n     for (i++; i > 0; i -= i & -i) { n       tot += sum[i]; n     } n     return tot; n   } n   int get(int i, int j) { return get(j) - (i > 0 ? get(i - 1) : 0); } n }; n fenwick xs; n int lo, hi; n bool flip; n void init(int n, int x) { n   xs.init(n); n   for (int i = 0; i < n; i++) { n     xs.add(i, x); n   } n   lo = 0; n   hi = n - 1; n } n int len() { return hi - lo + 1; } n int get(int i) { n   i = !flip ? lo + i : hi - i; n   return xs.get(i, i); n } n int get(int i, int j) { n   if (!flip) { n     i += lo; n     j += lo; n   } else { n     i = hi - i; n     j = hi - j; n     swap(i, j); n   } n   return xs.get(i, j); n } n void add(int i, int x) { n   i = !flip ? lo + i : hi - i; n   xs.add(i, x); n } n void drop(int k) { n   if (!flip) { n     lo += k; n   } else { n     hi -= k; n   } n } n void dump() { n   int n = len(); n   for (int i = 0; i < n; i++) { n     cerr << get(i) <<    ; n   } n   cerr << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, q; n   cin >> n >> q; n   init(n, 1); n   while (q--) { n     int t, i, j; n     cin >> t >> i; n     if (t == 1) { n       if (i > len() / 2) { n         i = len() - i; n         flip = !flip; n       } n       for (int k = 0; i - 1 - k >= 0; k++) { n         add(i + k, get(i - 1 - k)); n       } n       drop(i); n     } else { n       cin >> j; n       cout << get(i, j - 1) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 510000; n int f[maxn]; n int n, d, up; n int c[66]; n int b[66]; n int main() { n   cin >> n >> d; n   f[0] = 1; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     for (int j = (up += x); j >= x; j--) n       if (f[j - x]) f[j] = 1; n   } n   int num = 0, ans = 0; n   while (true) { n     int to = ans + d; n     while (!f[to] && to > ans) to--; n     if (ans == to) break; n     ans = to; n     num++; n   } n   cout << ans <<     << num << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n const int MaxN = 100005, NA = -1, MaxC = 0x3F3F3F3F; n int a[MaxN]; n int n; n multiset<int, greater<int> > s; n deque<int> d; n int main(void) { n   int i, v; n   multiset<int>::iterator it; n   while (scanf(  %d , &n) != EOF) { n     for (i = 0; i < n; i++) scanf(  %d , &a[i]); n     s.clear(); n     for (i = 0; i < n; i++) s.insert(a[i]); n     d.clear(); n     it = s.begin(); n     d.push_back(*it); n     s.erase(it); n     for (i = 1; i < n; i++) n       if (d.front() > d.back()) { n         v = d.front(); n         it = s.find(v + 1); n         if (it != s.end()) { n           d.push_front(*it); n           s.erase(it); n         } else { n           it = s.find(v - 1); n           if (it != s.end()) { n             d.push_front(*it); n             s.erase(it); n           } else n             break; n         } n       } else { n         v = d.back(); n         it = s.find(v + 1); n         if (it != s.end()) { n           d.push_back(*it); n           s.erase(it); n         } else { n           it = s.find(v - 1); n           if (it != s.end()) { n             d.push_back(*it); n             s.erase(it); n           } else n             break; n         } n       } n     if ((int)d.size() == n && abs(d.front() - d.back()) == 1) 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 template <typename T, typename U> n std::pair<T, U> operator+(const std::pair<T, U>& l, const std::pair<T, U>& r) { n   return {l.first + r.first, l.second + r.second}; n } n typedef void (*callback_function)(void); n const long long ZERO = 0LL; n const long long INF64 = 1e18; n const int INF32 = 1e9; n const int MOD = 1e9 + 7; n const long double PI = acos(-1.0L); n const long double EPS = static_cast<long double>(1e-9); n long long inv(long long a, long long b) { n   if (a != 1) { n     return b - (inv(b % a, a) * b) / a; n   } n   return 1; n } n inline int Pow(int a, int k) { n   int s = 1; n   for (; k; k >>= 1) { n     k& 1 ? s = 1LL * s * a % MOD : 0; n     a = 1LL * a * a % MOD; n   } n   return s; n } n const int N = 1e6 + 7; n void input() {} n char a[N][20]; n void solve() { n   int n; n   scanf( %d , &n); n   for (int i = (0); i < (n); i++) { n     scanf( %s , a[i]); n   } n   set<string> st; n   for (int i = (n - 1); i >= (0); i--) { n     if (!st.count(a[i])) { n       printf( %s n , a[i]); n       st.insert(a[i]); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   { n     input(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class A, class B> n inline bool mina(A &first, B second) { n   return (first > second) ? (first = second, 1) : 0; n } n template <class A, class B> n inline bool maxa(A &first, B second) { n   return (first < second) ? (first = second, 1) : 0; n } n const int MAXN = 3e5 + 5; n int in[MAXN]; n int N, M; n int pos[MAXN]; n pair<int, int> foes[MAXN]; n int main() { n   scanf( %d %d , &N, &M); n   for (int i = 1; i <= N; i++) { n     scanf( %d , &in[i]); n     pos[in[i]] = i; n   } n   int a, b; n   for (int(i) = 0; (i) < (M); ++(i)) { n     scanf( %d %d , &a, &b); n     a = pos[a]; n     b = pos[b]; n     if (a < b) swap(a, b); n     foes[i] = pair<int, int>(a, b); n   } n   sort(foes, foes + M); n   long long ans = 0; n   int maxx = 0; n   int cur = 0; n   for (int i = 1; i <= N; i++) { n     while (cur < M && foes[cur].first <= i) { n       maxa(maxx, foes[cur].second); n       cur++; n     } n     ans += i - maxx; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n char s[1000000]; n char ans[1000000]; n int main() { n   int i, len, cnt; n   char max = 0; n   while (scanf( %s , s) != EOF) { n     max = 0; n     cnt = 1000000 - 1; n     len = strlen(s); n     for (i = len - 1; i >= 0; i--) { n       if (s[i] >= max) { n         max = s[i]; n         ans[cnt--] = s[i]; n       } n     } n     for (i = cnt + 1; i < 1000000; i++) printf( %c , ans[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 228; n vector<int> g[MAXN]; n vector<bool> vis(MAXN); n int n, m; n int p[MAXN]; n int col[MAXN], help[MAXN]; n int t[4 * MAXN], lazy[4 * MAXN]; n int tin[MAXN], tout[MAXN]; n int timer; n void dfs(int v) { n   vis[v] = 1; n   tin[v] = timer++; n   for (int i = 0; i < g[v].size(); i++) { n     int to = g[v][i]; n     if (vis[to] == 0) { n       dfs(to); n     } n   } n   tout[v] = timer - 1; n } n void build(int v, int tl, int tr) { n   if (tl == tr) { n     t[v] = help[tl]; n     lazy[v] = -1; n   } else { n     int tm = (tl + tr) >> 1; n     build(2 * v, tl, tm); n     build(2 * v + 1, tm + 1, tr); n     t[v] = t[2 * v] + t[2 * v + 1]; n     lazy[v] = -1; n   } n } n void push(int v, int tl, int tr) { n   if (lazy[v] != -1) { n     if (lazy[v] % 2 == 1) { n       t[v] = (tr - tl + 1) - t[v]; n     } n     if (tl != tr) { n       if (lazy[2 * v] == -1) { n         lazy[2 * v] = lazy[v]; n       } else { n         lazy[2 * v] += lazy[v]; n       } n       if (lazy[2 * v + 1] == -1) { n         lazy[2 * v + 1] = lazy[v]; n       } else { n         lazy[2 * v + 1] += lazy[v]; n       } n     } n     lazy[v] = -1; n   } n } n void update(int v, int tl, int tr, int l, int r, long long color) { n   push(v, tl, tr); n   if (l > r) { n     return; n   } n   if (tl == l && tr == r) { n     if (lazy[v] == -1) { n       lazy[v] = color; n     } else { n       lazy[v] += color; n     } n     push(v, tl, tr); n     return; n   } n   int tm = (tl + tr) >> 1; n   update(2 * v, tl, tm, l, min(tm, r), color); n   update(2 * v + 1, tm + 1, tr, max(tm + 1, l), r, color); n   t[v] = t[2 * v] + t[2 * v + 1]; n } n long long get(int v, int tl, int tr, int l, int r) { n   push(v, tl, tr); n   if (l > r) { n     return 0; n   } n   if (tl == l && tr == r) { n     return t[v]; n   } n   int tm = (tl + tr) >> 1; n   return get(2 * v, tl, tm, l, min(tm, r)) + n          get(2 * v + 1, tm + 1, tr, max(tm + 1, l), r); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 1; i < n; i++) { n     cin >> p[i]; n     p[i]--; n     g[i].push_back(p[i]); n     g[p[i]].push_back(i); n   } n   for (int i = 0; i < n; i++) { n     cin >> col[i]; n   } n   dfs(0); n   for (int i = 0; i < n; i++) { n     int start = tin[i]; n     help[start] = col[i]; n   } n   build(1, 0, n - 1); n   cin >> m; n   for (int i = 0; i < m; i++) { n     string a; n     int x; n     cin >> a >> x; n     x--; n     if (a ==  get ) { n       cout << get(1, 0, n - 1, tin[x], tout[x]) << endl; n     } else { n       update(1, 0, n - 1, tin[x], tout[x], 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int num; n   int count = 0, i; n   int digitos[10]; n   scanf( %d , &num); n   int aux = num; n   for (i = 0; i < 10; i++) digitos[i] = 0; n   while (aux != 0) { n     digitos[aux % 10] = 1; n     aux = aux / 10; n   } n   if (num == 1) { n     printf( 1 n ); n     return 0; n   } n   for (i = 1; i * i <= num; i++) { n     if (num % i == 0) { n       aux = i; n       while (aux != 0) { n         if (digitos[aux % 10] == 1) { n           count++; n           break; n         } n         aux = aux / 10; n       } n       aux = num / i; n       if (aux != i) n         while (aux != 0) { n           if (digitos[aux % 10] == 1) { n             count++; n             break; n           } n           aux = aux / 10; n         } n     } n   } n   printf( %d n , count); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, cnt = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     cnt += x; n   } n   if (cnt * 2 <= n) { n     printf( %d n , n / 2); n     for (int i = 1; i <= n / 2; i++) printf( 0  ); n     printf(  n ); n   } else { n     int x = n / 2; n     if (x & 1) x++; n     printf( %d n , x); n     for (int i = 1; i <= x; i++) printf( 1  ); n     printf(  n ); n   } n   return; n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct candy { n   candy(int t, int h, int m) { n     this->type = t; n     this->height = h; n     this->mass = m; n   } n   int type; n   int height; n   int mass; n   bool operator<(const candy &c) const { return this->mass < c.mass; } n   bool operator>(const candy &c) const { return this->height < c.height; } n }; n int amount(int first, vector<candy> &all_candies, int x) { n   priority_queue<candy> candies0, candies1; n   int all_candies_index = 0; n   int max_height = x; n   int last_taken = 1 - first; n   int amount_taken = 0; n   while (true) { n     while (all_candies_index < all_candies.size()) { n       if (all_candies[all_candies_index].height > max_height) { n         break; n       } n       if (all_candies[all_candies_index].type == 0) { n         candies0.push(all_candies[all_candies_index]); n       } else { n         candies1.push(all_candies[all_candies_index]); n       } n       all_candies_index++; n     } n     if (last_taken) { n       if (candies0.empty()) { n         break; n       } n       amount_taken++; n       max_height += candies0.top().mass; n       candies0.pop(); n       last_taken = 0; n     } else { n       if (candies1.empty()) { n         break; n       } n       amount_taken++; n       max_height += candies1.top().mass; n       candies1.pop(); n       last_taken = 1; n     } n   } n   return amount_taken; n } n int main() { n   int n, x; n   scanf( %d%d , &n, &x); n   vector<candy> all_candies; n   for (int i = 0; i < n; ++i) { n     int t, h, m; n     scanf( %d%d%d , &t, &h, &m); n     all_candies.push_back(candy(t, h, m)); n   } n   sort(all_candies.begin(), all_candies.end(), std::greater<candy>()); n   printf( %d n , max(amount(0, all_candies, x), amount(1, all_candies, x))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i; n   vector<char> v; n   string s; n   cin >> s; n   n = s.size(); n   char c[n], ch; n   i = n; n   ch = s[n - 1]; n   while (i--) { n     if (s[i] == ch) n       v.push_back(s[i]); n     else if (s[i] > ch) { n       v.push_back(s[i]); n       ch = s[i]; n     } n   } n   for (i = v.size(); i > 0; i--) cout << v[i - 1]; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n bool iseven(long long int n) { n   if ((n & 1) == 0) return true; n   return false; n } n void print(long long int t) { cout << t; } n void print(int t) { cout << t; } n void print(string t) { cout << t; } n void print(char t) { cout << t; } n void print(double t) { cout << t; } n void print(long double t) { cout << t; } n template <class T, class V> n void print(pair<T, V> p); n template <class T> n void print(vector<T> v); n template <class T> n void print(set<T> v); n template <class T, class V> n void print(map<T, V> v); n template <class T> n void print(multiset<T> v); n template <class T, class V> n void print(pair<T, V> p) { n   cout <<  { ; n   print(p.first); n   cout <<  , ; n   print(p.second); n   cout <<  } ; n } n template <class T> n void print(vector<T> v) { n   cout <<  [  ; n   for (T i : v) { n     print(i); n     cout <<    ; n   } n   cout <<  ] ; n } n template <class T> n void print(set<T> v) { n   cout <<  [  ; n   for (T i : v) { n     print(i); n     cout <<    ; n   } n   cout <<  ] ; n } n template <class T> n void print(multiset<T> v) { n   cout <<  [  ; n   for (T i : v) { n     print(i); n     cout <<    ; n   } n   cout <<  ] ; n } n template <class T, class V> n void print(map<T, V> v) { n   cout <<  [  ; n   for (auto i : v) { n     print(i); n     cout <<    ; n   } n   cout <<  ] ; n } n void solve() { n   long long int n; n   cin >> n; n   string s; n   cin >> s; n   string original = s; n   string ans =   ; n   for (long long int i = 0; i < n; ++i) { n     ans +=  9 ; n   } n   for (long long int i = 0; i < n; ++i) { n     s = original; n     long long int toincrease = 10 - (s[i] -  0 ); n     for (auto &ch : s) { n       long long int temp = (ch -  0  + toincrease) % 10; n       ch =  0  + temp; n     } n     string tempans =   ; n     tempans += s[i]; n     long long int ptr = (i + 1) % n; n     while (ptr != i) { n       tempans += s[ptr]; n       ptr = (ptr + 1) % n; n     } n     ans = min(ans, tempans); n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n int SequenceValue(char seq[], int *min_val) { n   int value = 0; n   *min_val = 0; n   char *it = seq; n   while (*it !=   0  && *it !=   n ) { n     value += (*it ==  ( ) ? 1 : -1; n     if (value < *min_val) { n       *min_val = value; n     } n     it += 1; n   } n   return value; n } n char seq[300000 + 5]; n int starts[300000]; n int ends[300000]; n int main() { n   int n; n   scanf( %d%*c , &n); n   long long int pairs = 0; n   int i; n   for (i = 0; i < n; ++i) { n     fgets(seq, 300000 + 1, stdin); n     int min_val; n     int value = SequenceValue(seq, &min_val); n     if (min_val < value && min_val < 0) { n       continue; n     } n     if (value == 0) { n       pairs += 1LL * 2 * starts[0] + 1; n       starts[0] += 1; n       continue; n     } n     if (value > 0) { n       pairs += 1LL * ends[value]; n       starts[value] += 1; n     } else { n       pairs += 1LL * starts[-value]; n       ends[-value] += 1; n     } n   } n   printf( %I64d n , pairs); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i; n   long long n, m, k, l; n   cin >> n >> m >> k >> l; n   long long no = 0; n   long long yes = 0; n   long long j = (l + k); n   long long ans = ((j - 1) / m) + 1; n   if (((ans * m)) > n) no = 1; n   if (no) n     cout <<  -1 ; n   else n     cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD(long long, long long); n void doublesetprecision(double, int); n bool isEven(long long); n int gcd(int, int); n int n, m, o, ans, k; n int a[1000000]; n void solve() { n   string s, t; n   cin >> s >> t; n   if (s == t) { n     cout <<  yes ; n     return; n   } n   if (s.size() != t.size()) { n     cout <<  no ; n     return; n   } n   int hs[26] = {0}, ht[26] = {0}; n   for (long long i = 0; i < s.size(); i++) hs[s[i] -  a ]++; n   for (long long i = 0; i < t.size(); i++) ht[t[i] -  a ]++; n   for (long long i = 0; i < 26; i++) { n     if (ht[i] > 0 && hs[i] > 0) { n       cout <<  yes ; n       return; n     } n   } n   cout <<  no ; n } n int main() { n   int t = 1; n   scanf( %d , &t); n   while (t--) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n bool isEven(long long n) { n   if (n % 2 == 0) return 1; n   return 0; n } n void doublesetprecision(double x, int p) { n   cout << fixed << setprecision(p) << x; n } n long long MOD(long long x, long long M) { n   x = x % M; n   if (x < 0) x += M; n   return x; n } n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n


#include <bits/stdc++.h> n using namespace std; n vector<int> E[1 << 18]; n pair<int, int> R[1 << 18]; n int dfs(int a, int p, int d) { n   int cnt = 1; n   for (auto it = (E[a]).begin(); it != (E[a]).end(); ++it) { n     int b = *it; n     if (b == p) continue; n     cnt += dfs(b, a, d + 1); n   } n   R[a] = pair<int, int>(cnt - 1 - d, a); n   return cnt; n } n int main() { n   int n, k; n   cin >> n >> k; n   int i; n   for (i = (1); i < (n); ++i) { n     int a, b; n     cin >> a >> b; n     --a; n     --b; n     E[a].push_back(b); n     E[b].push_back(a); n   } n   dfs(0, -1, 0); n   sort(R, R + n); n   long long res = 0; n   for (i = (k); i < (n); ++i) res += R[i].first; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 1e5 + 2; n int n, last[NMAX], x, y; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int query = 1; query <= n; query++) { n     cin >> x >> y; n     int ans = 0, autre; n     for (int d = 1; d * d <= x; d++) { n       if (x % d == 0) { n         autre = x / d; n         if (last[d] < query - y || y == 0) { n           ans++; n         } n         if ((last[autre] < query - y || y == 0) && autre != d) { n           ans++; n         } n         last[d] = last[autre] = query; n       } n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7, LIM = 1e5; n unsigned long long int fact[2 * LIM + 1]; n unsigned long long int binpow(long long int x, int y) { n   unsigned long long int res = 1; n   while (y > 0) { n     if (y & 1) res = (res * x) % MOD; n     y >>= 1; n     x = (x * x) % MOD; n   } n   return res; n } n unsigned long long int inv_mod(unsigned long long int n) { n   return binpow(n, MOD - 2); n } n unsigned long long int com(int n, int r) { n   return (((fact[n] * inv_mod(fact[r])) % MOD) * inv_mod(fact[n - r])) % MOD; n } n bool comp(pair<int, int> p, pair<int, int> q) { n   if (p.first == q.first) return p.second < q.second; n   return p.first < q.first; n } n int main() { n   fact[0] = 1; n   for (int i = 1; i <= 2 * LIM; i++) { n     fact[i] = (i * fact[i - 1]) % MOD; n   } n   int h, w, n; n   cin >> h >> w >> n; n   vector<pair<int, int>> pts(n + 1); n   for (int i = 0; i < n; i++) { n     int r, c; n     cin >> r >> c; n     pts[i] = {r, c}; n   } n   pts[n] = {h, w}; n   sort(pts.begin(), pts.end(), comp); n   unsigned long long int dp[n + 1]; n   for (int i = 0; i <= n; i++) { n     dp[i] = com(pts[i].first + pts[i].second - 2, pts[i].first - 1); n     for (int j = 0; j < i; j++) { n       if (pts[j].first <= pts[i].first && pts[j].second <= pts[i].second) { n         dp[i] = (dp[i] + MOD - n                  (dp[j] * com(pts[i].first - pts[j].first + pts[i].second - n                                   pts[j].second, n                               pts[i].first - pts[j].first)) % n                      MOD) % n                 MOD; n       } n     } n   } n   cout << dp[n]; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int n; n int a[N]; n int fa(int x) { n   for (int i = 0;; i++) n     if ((1 << i) >= x) return (1 << i) - x; n } n int dis(int x, int y) { n   int sum = 0; n   while (x != y) x > y ? x = fa(x) : y = fa(y), sum++; n   return sum; n } n int get(int x) { n   int mx = 0, id = 0; n   for (int i = 1; i <= n; i++) { n     int t = dis(a[x], a[i]); n     if (t > mx) mx = t, id = i; n   } n   return id; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   int t1 = get(1), t2 = get(t1); n   printf( %d %d %d , t1, t2, dis(a[t1], a[t2])); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x; n   cin >> n >> x; n   int cnt = 0; n   for (int j = 1; j * j <= x; j++) { n     if (x % j == 0) { n       if (j <= n && x / j <= n) { n         if (j * j == x) n           cnt++; n         else n           cnt += 2; n       } n     } n   } n   cout << cnt << endl; n } n


#include <bits/stdc++.h> n void printResult() {} n int main(int argc, const char* argv[]) { n   std::ios::sync_with_stdio(false); n   int rows; n   bool possible = false; n   scanf( %d n , &rows); n   std::shared_ptr<std::vector<char*>> bus = n       std::make_shared<std::vector<char*>>(); n   if (rows > 0) { n     for (auto i = 0; i < rows; i++) { n       char* row = new char[4]; n       scanf( %c%c|%c%c n , &row[0], &row[1], &row[2], &row[3]); n       bus->push_back(row); n     } n     for (auto row : *bus) { n       if (row[0] == row[1] && row[0] ==  O ) { n         possible = true; n         row[0] =  + ; n         row[1] =  + ; n         break; n       } else if (row[2] == row[3] && row[2] ==  O ) { n         possible = true; n         row[2] =  + ; n         row[3] =  + ; n         break; n       } n     } n     if (possible == true) { n       printf( YES n ); n       for (auto row : *bus) { n         printf( %c%c|%c%c n , row[0], row[1], row[2], row[3]); n       } n     } else { n       printf( NO n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double d, h, v, e; n   cin >> d >> h >> v >> e; n   double pi = 2 * acos(0.0); n   double he = (4 * v) / (pi * d * d); n   if (he > e) { n     cout <<  YES n ; n     cout << setprecision(12) << (h) / (he - e); n   } else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[1000010]; n int c[20]; n int main() { n   int k; n   scanf( %d , &k); n   scanf( %s , s + 1); n   int n = strlen(s + 1); n   int sum = 0; n   int i; n   memset(c, 0, sizeof c); n   int ans = 0; n   for (i = 1; i <= n; i++) { n     c[s[i] -  0 ]++; n     sum += s[i] -  0 ; n   } n   sum = k - sum; n   for (i = 0; i <= 9; i++) n     while (sum > 0 && c[i]) { n       sum -= 9 - i; n       ans++; n       c[i]--; n     } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[2505][5005]; n int a[5000]; n int c1[30], c2[30]; n int dif[2505]; n int main() { n   int k, n; n   scanf( %d%d , &k, &n); n   scanf( %s , s[0]); n   bool flag = false; n   for (int i = 0; i < n; ++i) { n     c1[s[0][i] -  a ]++; n     if (c1[s[0][i] -  a ] > 1) flag = true; n   } n   for (int i = 1; i < k; ++i) { n     scanf( %s , s[i]); n     memset(c2, 0, sizeof(c2)); n     for (int j = 0; j < n; ++j) { n       c2[s[i][j] -  a ]++; n       if (s[i][j] != s[0][j]) dif[i]++; n     } n     if (dif[i] > 4) { n       cout <<  -1 n ; n       return 0; n     } n     for (int j = 0; j < 26; ++j) { n       if (c1[j] != c2[j]) { n         cout <<  -1 n ; n         return 0; n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       bool ok = true; n       for (int l = 1; l < k; ++l) { n         a[l] = dif[l]; n         a[l] -= (s[0][i] != s[l][i]); n         a[l] -= (s[0][j] != s[l][j]); n         a[l] += (s[0][i] != s[l][j]); n         a[l] += (s[0][j] != s[l][i]); n         if ((a[l] == 0 && flag) || a[l] == 2) continue; n         ok = false; n         break; n       } n       if (ok) { n         swap(s[0][i], s[0][j]); n         cout << s[0] << endl; n         return 0; n       } n     } n   } n   cout <<  -1 n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int maxn = 200011; n const int INF = 1 << 25; n const int logN = 18; n int n; n int a[maxn]; n bool p[maxn]; n int b[maxn]; n bool used[maxn]; n int main(int argc, char const *argv[]) { n   scanf( %d , &(n)); n   for (int(i) = 0; i < (int)(n); ++(i)) scanf( %d , &(a[i])); n   priority_queue<pair<int, int> > pq1, pq2; n   for (int(i) = 0; i < (int)(n); ++(i)) n     pq1.push(make_pair((a[i] - (n - i)), (i))); n   for (int i = n - 1; i >= 0; --i) { n     while (!pq1.empty() && pq1.top().second > i) pq1.pop(); n     while (!pq1.empty() && used[pq1.top().second]) pq1.pop(); n     while (!pq2.empty() && used[pq2.top().second]) pq2.pop(); n     int id1 = -1, id2 = -1; n     if (!pq1.empty()) id1 = pq1.top().second; n     if (!pq2.empty()) id2 = pq2.top().second; n     int pick; n     if (id1 != -1 && id2 != -1) { n       if (a[id1] - i + id1 > a[id2] - i + id2) { n         pick = id1; n         pq1.pop(); n       } else { n         pick = id2; n         pq2.pop(); n       } n     } else if (id1 != -1) { n       pick = id1; n       pq1.pop(); n     } else { n       pick = id2; n       pq2.pop(); n     } n     if (pick > i) n       b[i] = a[pick] - i + pick; n     else n       b[i] = a[pick] - i + pick; n     used[pick] = 1; n     if (!used[i]) pq2.push(make_pair((a[i] + i), (i))); n   } n   bool f = 1; n   for (int(i) = 0; i < (int)(n - 1); ++(i)) n     if (b[i] > b[i + 1]) f = 0; n   if (f) n     for (int(i) = 0; i < (int)(n); ++(i)) n       printf( %d%c , b[i], i == n - 1 ?   n  :    ); n   else n     puts( :( ); n } n


#include <bits/stdc++.h> n int main() { n   int n, s, i, sum = 0, max = 0; n   scanf( %d %d , &n, &s); n   int arr[n]; n   for (i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     sum = sum + arr[i]; n     if (arr[i] > max) max = arr[i]; n   } n   if (sum - max > s) n     printf( NO ); n   else n     printf( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 7; n int n, m, len, cnt1 = 0, cnt2 = 0, cnt3 = 0, fail[maxn]; n long long ans = 0; n struct Node { n   long long c; n   char ch; n } s[maxn], str[maxn], _str[maxn]; n bool ok(int i, int j) { n   if (_str[i].c != _str[j].c) return false; n   if (_str[i].ch != _str[j].ch) return false; n   return true; n } n bool _ok(int i, int j) { n   if (s[i].c != _str[j].c) return false; n   if (s[i].ch != _str[j].ch) return false; n   return true; n } n void getfail() { n   fail[0] = fail[1] = 0; n   for (int i = 1; i < cnt3; i++) { n     int j = fail[i]; n     while (j && !ok(i, j)) j = fail[j]; n     fail[i + 1] = (ok(i, j)) ? j + 1 : 0; n   } n } n void KMP_search() { n   for (int i = 0, j = 0; i < cnt1; i++) { n     while (j && !_ok(i, j)) j = fail[j]; n     if (_ok(i, j)) j++; n     if (j == cnt3) { n       if ((i - j + 1) && i != (cnt1 - 1) && s[i - j].ch == str[0].ch && n           s[i - j].c >= str[0].c && s[i + 1].ch == str[cnt2 - 1].ch && n           s[i + 1].c >= str[cnt2 - 1].c) n         ans++; n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %I64d-%c , &s[cnt1].c, &s[cnt1].ch); n     if (i && s[cnt1].ch == s[cnt1 - 1].ch) n       s[cnt1 - 1].c += s[cnt1].c; n     else n       cnt1++; n   } n   for (int i = 0; i < m; i++) { n     scanf( %I64d-%c , &str[cnt2].c, &str[cnt2].ch); n     if (i && str[cnt2].ch == str[cnt2 - 1].ch) n       str[cnt2 - 1].c += str[cnt2].c; n     else n       cnt2++; n   } n   if (cnt2 == 1) { n     for (int i = 0; i < cnt1; i++) n       if (s[i].ch == str[0].ch && s[i].c >= str[0].c) n         ans += s[i].c - str[0].c + 1; n   } else if (cnt2 == 2) { n     for (int i = 0; i < cnt1; i++) n       if (s[i].ch == str[0].ch && s[i].c >= str[0].c && (i != (cnt1 - 1)) && n           s[i + 1].ch == str[1].ch && s[i + 1].c >= str[i + 1].c) n         ans++; n   } else { n     for (int i = 1; i < cnt2 - 1; i++) _str[cnt3++] = str[i]; n     getfail(); n     KMP_search(); n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int inf = 0x3f3f3f3f; n const int mod = 998244353; n using ll = long long; n using vi = vector<int>; n using pii = pair<int, int>; n inline int read() { n   int x = 0, f = 1, c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c == 45) f ^= 1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; n   return f ? x : -x; n } n int qp(int x, int n) { n   return !n ? 1 : 1LL * qp(1LL * x * x % mod, n >> 1) * (n & 1 ? x : 1) % mod; n } n int inv(int x) { return qp(x, mod - 2); } n int n, m; n int a[55], w[55]; n int dp[55][55][55][55]; n int main() { n   n = read(), m = read(); n   for (int i = 0; i < (int)(n); ++i) a[i] = read(); n   int sum = 0, like = 0; n   for (int i = 0; i < (int)(n); ++i) n     w[i] = read(), sum += w[i], like += w[i] * a[i]; n   for (int i = 0; i < (int)(n); ++i) dp[0][0][i][0] = 1; n   for (int i = 0; i < (int)(m); ++i) n     for (int j = 0; j < (int)(i + 1); ++j) { n       int nwsum = sum + j - (i - j); n       int invnwsum = inv(nwsum); n       for (int k = 0; k < (int)(n); ++k) n         for (int l = 0; l < (int)(i + 1); ++l) { n           if (!dp[i][j][k][l]) continue; n           int nwlike = like + j; n           for (int sel = 0; sel < (int)(2); ++sel) { n             int nj = j + sel; n             int ful = sel ? nwlike : nwsum - nwlike; n             int nwk = l * (a[k] ? 1 : -1) + w[k]; n             if (nwk <= 0) continue; n             if (sel == a[k]) ful -= nwk; n             (dp[i + 1][nj][k][l] += n              1LL * dp[i][j][k][l] * ful % mod * invnwsum % mod) %= mod; n             if (sel == a[k]) n               (dp[i + 1][nj][k][l + 1] += n                1LL * dp[i][j][k][l] * nwk % mod * invnwsum % mod) %= mod; n           } n         } n     } n   for (int i = 0; i < (int)(n); ++i) { n     int ans = 0; n     for (int j = 0; j < (int)(m + 1); ++j) n       for (int l = 0; l < (int)(m + 1); ++l) { n         (ans += 1LL * (l * (a[i] ? 1 : -1) + w[i]) * dp[m][j][i][l] % mod) %= n             mod; n       } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d[(1 << 17) + 20][100], par[(1 << 17) + 20][100]; n int prime[20] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53}; n int sum[70], a[100]; n int ret, temp; n int ans = 10000000; n void pre() { n   for (int i = 1; i < 59; i++) { n     for (int j = 0; j < 17; j++) { n       if (prime[j] > 1 && i % prime[j] == 0) sum[i] += (1 << j); n     } n   } n } n void pp(int mask, int ind) { n   if (ind > 0) pp(mask - sum[par[mask][ind]], ind - 1); n   cout << par[mask][ind] <<    ; n   return; n } n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   pre(); n   for (int mask = 0; mask < (1 << 17); mask++) { n     ret = ans; n     for (int val = 1; val < 59; val++) { n       temp = ret; n       if ((sum[val] & mask) == sum[val]) ret = min(ret, abs(val - a[0])); n       if (temp > ret) par[mask][0] = val; n     } n     d[mask][0] = ret; n   } n   for (int mask = 0; mask < (1 << 17); mask++) { n     for (int ind = 0; ind < n; ind++) { n       ret = ans; n       for (int val = 1; val < 59; val++) { n         temp = ret; n         if ((sum[val] & mask) == sum[val]) n           ret = min(d[mask - sum[val]][ind - 1] + abs(val - a[ind]), ret); n         if (temp > ret) { n           par[mask][ind] = val; n         } n       } n       d[mask][ind] = ret; n     } n   } n   pp((1 << 17) - 1, n - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 1, i, j = 0; n   string s, t; n   getline(cin, s); n   getline(cin, t); n   for (i = 0; i < t.length(); i++) { n     if (s[j] == t[i]) { n       n++; n       j++; n     } n   } n   cout << n; n } n


#include <bits/stdc++.h> n using namespace std; n int n, ma, a1; n long long ans; n char ch; n struct point { n   int a, b; n } s[1000005]; n struct node { n   int l, r; n   long long add, sum, ma; n   node *ch[2]; n } * nil, *root; n long long gets() { n   long long v = 0; n   bool f = 0; n   char ch; n   while (!isdigit(ch)) { n     f |= ch ==  - ; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     v = (v << 3) + (v << 1) + ch - 48; n     ch = getchar(); n   } n   return f ? -v : v; n } n void init() { n   nil = new node(); n   nil->ch[0] = nil->ch[1] = nil; n   nil->add = nil->ma = nil->sum = 0; n } n void pushup(node *x) { n   x->ma = max(x->ch[0]->ma + x->ch[1]->sum, x->ch[1]->ma); n   x->sum = x->ch[0]->sum + x->ch[1]->sum; n } n void build(node *&x, int l, int r) { n   x = new node(); n   x->l = l, x->r = r; n   x->ma = x->sum = 0; n   if (l == r) { n     x->ma = x->l; n     return; n   } n   int mid = l + r >> 1; n   build(x->ch[0], l, mid); n   build(x->ch[1], mid + 1, r); n   pushup(x); n } n void insert(node *x, int pos, long long v) { n   if (x->r == x->l && x->r == pos) { n     x->ma += v, x->sum += v; n     return; n   } n   int mid = x->l + x->r >> 1; n   if (pos <= mid) n     insert(x->ch[0], pos, v); n   else n     insert(x->ch[1], pos, v); n   pushup(x); n } n void ask(node *x, int l, int r) { n   if (l == x->l && r == x->r) { n     ans = max(ans + x->sum, x->ma); n     return; n   } n   int mid = x->l + x->r >> 1; n   if (r <= mid) n     ask(x->ch[0], l, r); n   else if (l > mid) n     ask(x->ch[1], l, r); n   else n     ask(x->ch[0], l, mid), ask(x->ch[1], mid + 1, r); n } n int main() { n   n = gets(); n   ma = 1000000; n   init(); n   build(root, 1, ma); n   for (int i = 1; i <= n; i++) { n     scanf( %c , &ch); n     if (ch ==  ? ) { n       a1 = gets(); n       ans = 0; n       ask(root, 1, a1); n       ans -= a1; n       printf( %I64d n , ans); n     } n     if (ch ==  + ) { n       s[i].a = gets(), s[i].b = gets(); n       insert(root, s[i].a, s[i].b); n     } n     if (ch ==  - ) { n       a1 = gets(); n       insert(root, s[a1].a, -s[a1].b); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   n--; n   k--; n   int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   int count = 0; n   int flag = 0; n   while (count <= k) { n     if (count == k) { n       flag = 1; n       break; n     } n     count += arr[count]; n   } n   if (flag == 1) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int team1, team2; n   std::string s; n   std::cin >> s; n   team1 = s.find( 1111111 ); n   team2 = s.find( 0000000 ); n   if (team1 >= 0 || team2 >= 0) { n     std::cout <<  YES ; n   } else n     std::cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 7; n const int M = 1 << 18; n int n, m; n string s; n set<int> S[256]; n int sub[2 * M]; n void insert(int a) { n   a += M - 1; n   while (a) { n     --sub[a]; n     a /= 2; n   } n } n int kth(int k, int v = 1, int l = 1, int r = M) { n   if (l == r) return l; n   if (sub[2 * v] >= k) n     return kth(k, 2 * v, l, (l + r) / 2); n   else n     return kth(k - sub[2 * v], 2 * v + 1, (l + r) / 2 + 1, r); n } n void build(int v = 1, int l = 1, int r = M) { n   sub[v] = r - l + 1; n   if (l == r) return; n   build(2 * v, l, (l + r) / 2); n   build(2 * v + 1, (l + r) / 2 + 1, r); n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> n >> m >> s; n   s =  #  + s; n   for (int i = 1; i <= n; ++i) { n     S[s[i]].insert(i); n   } n   build(); n   while (m--) { n     int l, r; n     char c; n     cin >> l >> r >> c; n     l = kth(l); n     r = kth(r); n     auto it = S[c].lower_bound(l); n     while (it != S[c].end() && *it <= r) { n       insert(*it); n       S[c].erase(it); n       it = S[c].lower_bound(l); n     } n   } n   for (int i = 1; i <= n; ++i) { n     if (S[s[i]].find(i) != S[s[i]].end()) cout << s[i]; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n int main() { n   char a[100009], c; n   int j = 0, i; n   while ((c = getchar()) !=   n ) { n     if (j == 0) { n       a[j] = c; n       j++; n     } else { n       if (a[j - 1] == c) { n         j--; n       } else { n         a[j] = c; n         j++; n       } n     } n   } n   if (j == 0) { n     printf( Yes n ); n   } else { n     printf( No n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int ans = 123422412; n int n, a, b, x, y; n void dfs(int r, int x, int y, int ret) { n   if (x < 0 || y < 0) return; n   if (r < 0) return; n   if (x == 0 && y == 0) { n     ans = min(ans, ret); n     return; n   } n   dfs(r - a, x - 1, y, ret); n   dfs(n - a, x - 1, y, ret + 1); n   dfs(r - b, x, y - 1, ret); n   dfs(n - b, x, y - 1, ret + 1); n } n int main() { n   cin >> n >> a >> b; n   dfs(n, 4, 2, 1); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 200005; n const int mod = 1e9 + 7; n struct Node { n   int x, y, z; n   Node() {} n   Node(int _x, int _y, int _z) { n     x = _x; n     y = _y; n     z = _z; n     if (x < y) swap(x, y); n     if (x < z) swap(x, z); n     if (y < z) swap(y, z); n   } n   bool operator<(const Node& a) const { n     if (x != a.x) return x < a.x; n     if (y != a.y) return y < a.y; n     return z < a.z; n   } n }; n Node a[10]; n int A, B, C; n set<pair<int, int> > s; n set<pair<int, int> >::iterator it; n int v[MAXN]; n int vis[MAXN]; n int n; n void solve() { n   s.clear(); n   memset(vis, 0, sizeof vis); n   scanf( %d%d%d , &A, &B, &C); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &v[i]); n   } n   sort(v + 1, v + n + 1); n   for (int i = 1; i <= n; ++i) { n     s.insert(make_pair(v[i], i)); n   } n   a[0] = Node(A, B, C); n   a[1] = Node(A + B, C, 0); n   a[2] = Node(A + C, B, 0); n   a[3] = Node(B + C, A, 0); n   a[4] = Node(A + B + C, 0, 0); n   sort(a, a + 5); n   int ans = 0; n   for (int i = n; i >= 1; --i) n     if (!vis[i]) { n       int ok = 0; n       ++ans; n       for (int j = 0; j < 5; ++j) { n         if (a[j].x >= v[i]) { n           ok = 1; n           vis[i] = 1; n           s.erase(make_pair(v[i], i)); n           int t = 0; n           if (s.begin() == s.end()) break; n           it = s.lower_bound(make_pair(a[j].y, 0x3f3f3f3f)); n           if (it != s.begin()) { n             --it; n             vis[it->second] = 1; n             s.erase(*it); n             ++t; n           } n           if (s.begin() == s.end()) break; n           it = s.lower_bound(make_pair(a[j].z, 0x3f3f3f3f)); n           if (it != s.begin()) { n             --it; n             vis[it->second] = 1; n             s.erase(*it); n             ++t; n           } n           if (t == 0) { n             it = s.lower_bound(make_pair(a[j].y + a[j].z, 0x3f3f3f3f)); n             if (it != s.begin()) { n               --it; n               vis[it->second] = 1; n               s.erase(*it); n               ++t; n             } n           } n           break; n         } n       } n       if (!ok) { n         printf( -1 n ); n         return; n       } n     } n   printf( %d n , ans); n } n int main() { n   while (~scanf( %d , &n)) solve(); 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   long long n, p, w, d; n   cin >> n >> p >> w >> d; n   bool been[int(1e5)] = {}, can = 0; n   long long cnt = p / w, dif = 0; n   while (cnt >= 0) { n     dif = p - cnt * w; n     if (been[dif % d]) break; n     if (dif % d == 0) { n       if (cnt + dif / d <= n) { n         can = 1; n         break; n       } n     } n     been[dif % d] = 1; n     cnt--; n   } n   if (can) n     cout << cnt <<     << dif / d <<     << n - cnt - dif / d <<   n ; n   else n     cout <<  -1 n ; n   return 0; n } n


#include<bits/stdc++.h> n #define maxn 20010 n using namespace std; n  n int n, m, a[maxn]; n int lg[maxn], mx[maxn][21], f[maxn][35][21], s[35], t[35]; n int mxx(int x,int y){return x+a[x]>y+a[y]?x:y;} n int qry(int x,int y){return mxx(mx[x][lg[y-x+1]],mx[y-(1<<lg[y-x+1])+1][lg[y-x+1]]);} n   n int main() n { n  lg[0]=-1; n  scanf( %d%d ,&n,&m); n  for(int i=1;i<=n;i++) scanf( %d ,&a[i]),lg[i]=lg[i>>1]+1,mx[i][0]=i; n  for(int j=1;j<=14;j++) for(int i=1;i<=n-(1<<j)+1;i++) mx[i][j]=mxx(mx[i][j-1],mx[i+(1<<(j-1))][j-1]); n  for(int i=1;i<=n;i++) for(int j=0;j<31;j++) f[i][j][0]=min(n,i+a[i]+j); n  for(int j=1;j<15;j++) for(int i=1;i<=n;i++) for(int x=0;x<31;x++) for(int y=0;x+y<31;y++) f[i][x+y][j]=max(f[i][x+y][j],f[qry(i,f[i][x][j-1])][y][j-1]); n  while(m--) n  { n   int l,r,k; n   scanf( %d%d%d ,&l,&r,&k); n   if(l==r){puts( 0 );continue;} n   for(int i=0;i<=k;i++) s[i]=t[i]=l; n   int ans=0; n   for(int j=14;j>=0;j--) n   { n    for(int i=0;i<=k;i++) t[i]=s[i]; n    for(int x=0;x<=k;x++) for(int y=0;x+y<=k;y++) t[x+y]=std::max(t[x+y],f[qry(l,s[x])][y][j]); n    bool flg=0; n    for(int i=0;i<=k;i++) if(t[i]>=r) flg=1; n    if(flg) continue; n    ans+=(1<<j); n    for(int i=0;i<=k;i++) s[i]=t[i]; n   } n   printf( %d n ,ans+1); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long a[100001]; n long long b[100001]; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   map<int, int> cnt; n   vector<int> q; n   int loc = 0; n   for (int i = n - 1; i >= 0; i--) { n     cnt[a[i]]++; n     if (cnt[a[i]] == 1) loc++; n     b[i] = loc; n   } n   cnt.clear(); n   long long ans = 0; n   for (int i = 0; i < n - 1; i++) { n     cnt[a[i]]++; n     if (cnt[a[i]] == 1) ans += b[i + 1]; n   } n   cout << ans << endl; n   return 0; n } n