#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, m, k; n   cin >> n >> m >> k; n   map<long long, long long> cnt1, cnt2; n   vector<long long> a(n), b(m); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (long long i = 0; i < m; i++) { n     cin >> b[i]; n   } n   long long run = 0; n   for (long long i = 0; i < n; i++) { n     if (a[i] == 0) { n       if (run) { n         cnt1[run]++; n         run = 0; n       } n     } else { n       run++; n     } n   } n   if (run) { n     cnt1[run]++; n   } n   run = 0; n   for (long long i = 0; i < m; i++) { n     if (b[i] == 0) { n       if (run) { n         cnt2[run]++; n         run = 0; n       } n     } else { n       run++; n     } n   } n   if (run) { n     cnt2[run]++; n   } n   long long ans = 0; n   for (long long i = 1; i * i <= k; i++) { n     if (k % i == 0) { n       long long j = k / i; n       auto it1 = cnt1.lower_bound(i); n       auto it2 = cnt2.lower_bound(j); n       while (it1 != cnt1.end()) { n         auto it3 = it2; n         long long x = (it1->first - i + 1) * it1->second; n         while (it3 != cnt2.end()) { n           long long y = (it3->first - j + 1) * it3->second; n           ans += x * y; n           it3++; n         } n         it1++; n       } n       if (i != j) { n         it1 = cnt1.lower_bound(j); n         it2 = cnt2.lower_bound(i); n         while (it1 != cnt1.end()) { n           auto it3 = it2; n           long long x = (it1->first - j + 1) * it1->second; n           while (it3 != cnt2.end()) { n             long long y = (it3->first - i + 1) * it3->second; n             ans += x * y; n             it3++; n           } n           it1++; n         } n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return (b != 0 ? gcd(b, a % b) : a); } n int lcm(int a, int b) { return (a / gcd(a, b) * b); } n int R[] = {1, -1, 0, 0, 1, -1, -1, 1}; n int C[] = {0, 0, 1, -1, 1, -1, 1, -1}; n int KR[] = {-2, -2, -1, 1, 2, 2, -1, 1}; n int KC[] = {1, -1, 2, 2, 1, -1, -2, -2}; n struct P { n   double x, y; n   P(double x = 0.0, double y = 0.0) { n     this->x = x; n     this->y = y; n   } n }; n P mkv(P ae, P be) { return P(be.x - ae.x, be.y - ae.y); } n double dtp(P ae, P be) { return (ae.x * be.x + ae.y * be.y); } n double crp(P ae, P be) { return (ae.x * be.y - ae.y * be.x); } n double val(P ae) { return sqrt(dtp(ae, ae)); } n P vresize(P ae, double llen) { n   double v = val(ae); n   return P(ae.x * llen / v, ae.y * llen / v); n } n double ART(P ae, P be) { return crp(ae, be) / 2.0; } n P rot(P ae, double ang) { n   return P(ae.x * cos(ang) - ae.y * sin(ang), n            ae.y * cos(ang) + ae.x * sin(ang)); n } n int main() { n   int a, b, c, d, h, m, n, p, x, y, i, j, k, l, q, r, t, cnt, sm, tmp; n   scanf( %d %d %d %d , &a, &b, &c, &d); n   sm = max((3.0 * a) / 10.0, a - (a * c / 250.0)); n   cnt = max((3.0 * b) / 10.0, b - (b * d / 250.0)); n   if (sm > cnt) n     printf( Misha n ); n   else if (cnt > sm) n     printf( Vasya n ); n   else n     printf( Tie n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[11111111]; n long long b[11111111]; n int len = 0; n long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); } n int counter(int x) { n   int res = 0; n   while (x) { n     res++; n     x /= 10; n   } n   return res; n } n void printK(int x, int k) { n   int n = counter(x); n   n = n - k + 1; n   for (int i = 0; i < n - 1; i++) x /= 10; n   cout << x % 10 <<   n ; n } n long long g(long long x) { n   long long res = 0; n   int len = 1; n   long long tmp = 9; n   while (x > tmp) { n     res += tmp * len; n     x -= tmp; n     tmp *= 10; n     len++; n   } n   res += x * len; n   return res; n } n long long f(long long x) { n   long long res = 0; n   long long tmp = 0; n   while (x >= 0) { n     res += (1 + x) * x / 2; n     if (tmp == 0) n       tmp = 9; n     else n       tmp = tmp * 10; n     x -= tmp; n   } n   return res; n } n int main() { n   long long q, k; n   cin >> q; n   for (int i = 0; i < q; i++) { n     cin >> k; n     long long m, l = 0, r = 2000000001; n     while (l < r - 1) { n       m = (l + r) >> 1; n       if (f(m) < k) { n         l = m; n       } else { n         r = m; n       } n     } n     k -= f(l); n     r = l + 2, l = 0; n     while (l < r - 1) { n       m = (l + r) >> 1; n       if (g(m) < k) { n         l = m; n       } else { n         r = m; n       } n     } n     k -= g(l); n     long long n = 0; n     long long L = l + 1; n     l++; n     while (L) { n       L /= 10; n       n++; n     } n     n = n - k + 1; n     for (int j = 0; j < n - 1; j++) l /= 10; n     cout << l % 10 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int q, c, i1, i2; n long long v, u, w; n bool flag; n map<long long, long long> cost; n vector<long long> a, b; n void Rule() { n   for (int i = i1 - 1; i >= 0; --i) { n     cost[a[i]] += w; n   } n   for (int i = i2 - 1; i >= 0; --i) { n     cost[b[i]] += w; n   } n } n long long Cuddle() { n   long long ret = 0; n   for (int i = i1 - 1; i >= 0; --i) { n     ret += cost[a[i]]; n   } n   for (int i = i2 - 1; i >= 0; --i) { n     ret += cost[b[i]]; n   } n   return ret; n } n int main() { n   scanf( %d , &q); n   while (q--) { n     scanf( %d , &c); n     a.clear(), b.clear(); n     cin >> v >> u; n     while (v) { n       a.push_back(v); n       if (v % 2) n         v = (v - 1) / 2; n       else n         v /= 2; n     } n     while (u) { n       b.push_back(u); n       if (u % 2) n         u = (u - 1) / 2; n       else n         u /= 2; n     } n     flag = 0; n     for (int i = 0; i < ((int)(a).size()); ++i) { n       for (int j = 0; j < ((int)(b).size()); ++j) { n         if (a[i] == b[j]) { n           i1 = i, i2 = j, flag = 1; n           break; n         } n       } n       if (flag) break; n     } n     if (c == 1) n       cin >> w, Rule(); n     else n       printf( %I64d n , Cuddle()); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[1000100]; n long long calc(int idx) { n   long long ans = 0; n   int x = -1, y = -1; n   for (int i = 0; i < idx; i += m) { n     ans += (long long)abs(a[i] - a[idx]) * 2; n     x = i; n   } n   for (int i = n - 1; i > idx; i -= m) { n     ans += (long long)abs(a[i] - a[idx]) * 2; n     y = i; n   } n   return ans; n } n long long solve(long long l, long long r) { n   while (l < r) { n     long long ml = (2 * l + r) / 3; n     long long mr = (l + 2 * r + 2) / 3; n     long long vl = calc(ml); n     long long vr = calc(mr); n     if (vl < vr) n       r = mr - 1; n     else if (vl > vr) n       l = ml + 1; n     else { n       l = ml; n       r = mr - 1; n     } n   } n   return l; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   int ansid = solve(0, n - 1); n   cout << calc(ansid) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char _buf[100000], *_p1 = _buf, *_p2 = _buf; n inline int gi() { n   int 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 << 3) + (x << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return (f == 1) ? x : -x; n } n const int maxn = 2e5 + 5, inf = 1e9, mod = 1e9 + 7; n int n, k, a[maxn]; n vector<pair<int, int>> e[maxn]; n int siz[maxn], maxv[maxn], rt, dis[maxn]; n long long val[maxn], ans; n vector<int> now; n struct segmentree { n   static const int maxn = 8e5 + 5; n   long long sum[maxn]; n   inline void pushup(int u) { sum[u] = sum[u << 1] + sum[u << 1 | 1]; } n   inline void update(int u, int l, int r, int x, long long v) { n     if (l == r) return sum[u] += v, void(); n     int mid = (l + r) >> 1; n     if (x <= mid) n       update(u << 1, l, mid, x, v); n     else n       update(u << 1 | 1, mid + 1, r, x, v); n     pushup(u); n   } n   inline long long query(int u, int l, int r, int x, int y) { n     if (x <= l && y >= r) return sum[u]; n     int mid = (l + r) >> 1; n     long long ret = 0; n     if (x <= mid) ret = query(u << 1, l, mid, x, y); n     if (y > mid) ret += query(u << 1 | 1, mid + 1, r, x, y); n     return ret; n   } n } tree[2], T[2]; n inline void init() {} n inline void input() { n   n = gi(), k = gi(); n   for (int i = 1; i <= n; ++i) a[i] = gi(); n   for (int i = 1; i <= n - 1; ++i) { n     int u = gi(), v = gi(), w = gi(); n     e[u].push_back({v, w}); n     e[v].push_back({u, w}); n   } n } n bool vis[maxn]; n int sum; n inline void getrt(int u, int fa) { n   now.push_back(u); n   siz[u] = 1; n   maxv[u] = 0; n   for (auto x : e[u]) { n     int v = x.first; n     if (v == fa || vis[v]) continue; n     getrt(v, u); n     siz[u] += siz[v]; n     maxv[u] = max(maxv[u], siz[v]); n   } n   maxv[u] = max(maxv[u], sum - siz[u]); n   if (maxv[u] < maxv[rt]) rt = u; n } n inline void getdis(int u, int fa, int nowc) { n   now.push_back(u); n   for (auto x : e[u]) { n     int v = x.first, w = x.second; n     if (v == fa || vis[v]) continue; n     val[v] = val[u] + a[v], dis[v] = dis[u] + (nowc != w); n     getdis(v, u, w); n   } n } n inline void count(int u) { n   vector<pair<int, int>> temp; n   dis[u] = 0, val[u] = 0; n   ans = (ans + a[u]) % mod; n   for (auto x : e[u]) { n     now.clear(); n     int v = x.first, w = x.second; n     if (vis[v]) continue; n     dis[v] = 0, val[v] = a[v]; n     getdis(v, u, w); n     for (int x : now) temp.push_back({w, x}); n     for (int x : now) n       if (dis[x] <= k) ans = (ans + val[x] + a[u]) % mod; n     for (int x : now) { n       if (dis[x] > k) continue; n       long long sum = tree[w].query(1, 0, n, 0, k - dis[x]) + n                       1ll * T[w].query(1, 0, n, 0, k - dis[x]) * val[x]; n       if (dis[x] < k) n         sum += tree[w ^ 1].query(1, 0, n, 0, k - dis[x] - 1) + n                1ll * T[w ^ 1].query(1, 0, n, 0, k - dis[x] - 1) * val[x]; n       ans = (ans + sum) % mod; n     } n     for (int x : now) n       if (dis[x] <= k) n         tree[w].update(1, 0, n, dis[x], val[x] + a[u]), n             T[w].update(1, 0, n, dis[x], 1); n   } n   for (auto x : temp) { n     int w = x.first, v = x.second; n     if (dis[v] > k) continue; n     tree[w].update(1, 0, n, dis[v], -val[v] - a[u]); n     T[w].update(1, 0, n, dis[v], -1); n   } n } n inline void Divide(int u) { n   vis[u] = 1; n   count(u); n   for (auto x : e[u]) { n     int v = x.first; n     if (vis[v]) continue; n     maxv[rt = 0] = inf; n     sum = siz[v]; n     getrt(v, u); n     Divide(rt); n   } n } n inline void solve() { n   maxv[rt = 0] = inf; n   sum = n; n   getrt(1, 0); n   Divide(rt); n   printf( %lld n , ans); n } n int main() { n   int T = 1; n   while (T--) { n     init(); n     input(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solution1(int a, int b, map<pair<int, int>, bool>& mp1, n                map<pair<int, int>, int>& mp2) { n   if (a == b) { n     mp2[{a, b}] = a; n     return; n   } n   int j = a; n   while (j <= b) { n     if (j == a) { n       if (mp1[{j + 1, b}]) { n         mp2[{a, b}] = j; n         solution1(j + 1, b, mp1, mp2); n         break; n       } n     } else if (j == b) { n       if (mp1[{a, j - 1}]) { n         mp2[{a, b}] = j; n         solution1(a, j - 1, mp1, mp2); n         break; n       } n     } else { n       if (mp1[{a, j - 1}] && mp1[{j + 1, b}]) { n         mp2[{a, b}] = j; n         solution1(a, j - 1, mp1, mp2); n         solution1(j + 1, b, mp1, mp2); n         break; n       } n     } n     j++; n   } n } n void solve() { n   int a; n   cin >> a; n   map<pair<int, int>, bool> mp1; n   map<pair<int, int>, int> mp2; n   int x, y; n   for (int i = 0; i < a; i++) { n     cin >> x >> y; n     mp1[{x, y}] = 1; n   } n   solution1(1, a, mp1, mp2); n   vector<vector<int>> v1; n   int i = 0; n   for (auto x : mp1) { n     if (mp2[x.first]) { n       cout << x.first.first <<     << x.first.second <<     << mp2[x.first] n            << endl; n     } n   } n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   int a; n   cin >> a; n   while (a--) { n     solve(); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k; n   cin >> n >> k; n   vector<pair<long long int, long long int>> v; n   for (long long int i = 0; i < n; i++) { n     long long int x; n     cin >> x; n     v.push_back({x, i + 1}); n   } n   sort(v.begin(), v.end()); n   vector<long long int> ans; n   for (auto i : v) { n     if (k >= i.first) { n       ans.push_back(i.second); n       k = k - i.first; n     } else n       break; n   } n   cout << ans.size() <<   n ; n   for (auto i : ans) cout << i <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[200005]; n map<int, int> mp; n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> v; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     v.push_back(a); n   } n   sort(v.begin(), v.end()); n   int ans = 1e9; n   for (int i = 0; i < n; i++) { n     int zz = v[i]; n     int cnt = 0; n     mp[zz]++; n     if (mp[zz] == k) ans = min(ans, arr[zz]); n     while (zz > 0) { n       zz /= 2; n       mp[zz]++; n       cnt++; n       arr[zz] += cnt; n       if (mp[zz] == k) { n         ans = min(ans, arr[zz]); n       } n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> vec; n map<int, int> mp; n multiset<long long int> st; n long long int n, m; n string s, s1, s2; n int arr[] = {4, 8, 15, 16, 23, 42}; n int main() { n   long long int i, j, k, c = INT_MAX, ans = 0, t, z, x, y; n   for (i = 0; i < 6; i++) mp[arr[i]] = 0; n   cout <<  ?   << 1 <<     << 2 << endl; n   fflush(stdout); n   cin >> z; n   for (i = 0; i < 6; i++) { n     if (z % arr[i] == 0) { n       y = z / arr[i]; n       for (j = i + 1; j < 6; j++) { n         if (y == arr[j]) { n           vec.push_back(z); n           mp[arr[i]]++; n           mp[arr[j]]++; n           ans = 1; n           break; n         } n       } n       if (ans == 1) break; n     } n   } n   ans = 0; n   cout <<  ?   << 1 <<     << 3 << endl; n   fflush(stdout); n   cin >> z; n   for (i = 0; i < 6; i++) { n     if (z % arr[i] == 0) { n       y = z / arr[i]; n       for (j = i + 1; j < 6; j++) { n         if (y == arr[j]) { n           vec.push_back(z); n           mp[arr[i]]++; n           mp[arr[j]]++; n           ans = 1; n           break; n         } n       } n       if (ans == 1) break; n     } n   } n   ans = 0; n   cout <<  ?   << 1 <<     << 4 << endl; n   fflush(stdout); n   cin >> z; n   for (i = 0; i < 6; i++) { n     if (z % arr[i] == 0) { n       y = z / arr[i]; n       for (j = i + 1; j < 6; j++) { n         if (y == arr[j]) { n           vec.push_back(z); n           mp[arr[i]]++; n           mp[arr[j]]++; n           ans = 1; n           break; n         } n       } n       if (ans == 1) break; n     } n   } n   ans = 0; n   cout <<  ?   << 1 <<     << 5 << endl; n   fflush(stdout); n   cin >> z; n   for (i = 0; i < 6; i++) { n     if (z % arr[i] == 0) { n       y = z / arr[i]; n       for (j = i + 1; j < 6; j++) { n         if (y == arr[j]) { n           vec.push_back(z); n           mp[arr[i]]++; n           mp[arr[j]]++; n           ans = 1; n           break; n         } n       } n       if (ans == 1) break; n     } n   } n   cout <<  !  ; n   if (mp[4] == 4) { n     cout << 4 <<    ; n     cout << vec[0] / 4 <<    ; n     cout << vec[1] / 4 <<    ; n     cout << vec[2] / 4 <<    ; n     cout << vec[3] / 4 <<    ; n     for (i = 0; i < 6; i++) { n       if (mp[arr[i]] == 0) { n         cout << arr[i]; n         return 0; n       } n     } n   } else if (mp[8] == 4) { n     cout << 8 <<    ; n     cout << vec[0] / 8 <<    ; n     cout << vec[1] / 8 <<    ; n     cout << vec[2] / 8 <<    ; n     cout << vec[3] / 8 <<    ; n     for (i = 0; i < 6; i++) { n       if (mp[arr[i]] == 0) { n         cout << arr[i]; n         return 0; n       } n     } n   } else if (mp[15] == 4) { n     cout << 15 <<    ; n     cout << vec[0] / 15 <<    ; n     cout << vec[1] / 15 <<    ; n     cout << vec[2] / 15 <<    ; n     cout << vec[3] / 15 <<    ; n     for (i = 0; i < 6; i++) { n       if (mp[arr[i]] == 0) { n         cout << arr[i]; n         return 0; n       } n     } n   } else if (mp[16] == 4) { n     cout << 16 <<    ; n     cout << vec[0] / 16 <<    ; n     cout << vec[1] / 16 <<    ; n     cout << vec[2] / 16 <<    ; n     cout << vec[3] / 16 <<    ; n     for (i = 0; i < 6; i++) { n       if (mp[arr[i]] == 0) { n         cout << arr[i]; n         return 0; n       } n     } n   } else if (mp[23] == 4) { n     cout << 23 <<    ; n     cout << vec[0] / 23 <<    ; n     cout << vec[1] / 23 <<    ; n     cout << vec[2] / 23 <<    ; n     cout << vec[3] / 23 <<    ; n     for (i = 0; i < 6; i++) { n       if (mp[arr[i]] == 0) { n         cout << arr[i]; n         return 0; n       } n     } n   } else if (mp[42] == 4) { n     cout << 42 <<    ; n     cout << vec[0] / 42 <<    ; n     cout << vec[1] / 42 <<    ; n     cout << vec[2] / 42 <<    ; n     cout << vec[3] / 42 <<    ; n     for (i = 0; i < 6; i++) { n       if (mp[arr[i]] == 0) { n         cout << arr[i]; n         return 0; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline void readInt(int &x) { n   int neg = 1; n   char ch; n   while (!isdigit(ch = getchar())) { n     if (ch ==  - ) { n       neg = -1; n     } n   } n   x = ch -  0 ; n   while (isdigit(ch = getchar())) { n     x = x * 10 + ch -  0 ; n   } n   x *= neg; n } n int N; n vector<bool> visited; n vector<bool> isprime; n vector<pair<int, int> > groups; n vector<int> rejected; n void solve(int X); n int main() { n   int i, j; n   readInt(N); n   isprime.resize(N + 1, true); n   visited.resize(N + 1); n   for (i = 2; i <= (N >> 1); ++i) { n     if (isprime[i]) { n       for (j = i << 1; j <= (N >> 1); j += i) { n         isprime[j] = false; n       } n     } n   } n   for (i = N >> 1; i > 1; --i) { n     if (isprime[i]) { n       solve(i); n     } n   } n   for (i = rejected.size() - 1; i > 0; i -= 2) { n     groups.push_back(make_pair(rejected[i], rejected[i - 1])); n   } n   printf( %d n , (int)groups.size()); n   for (auto x : groups) { n     printf( %d %d n , x.first, x.second); n   } n   return 0; n } n void solve(int X) { n   int i, aux = 0; n   for (i = X * 3; i <= N; i += X) { n     if (!visited[i]) { n       if (aux == 0) { n         aux = i; n       } else { n         visited[aux] = visited[i] = true; n         groups.push_back(make_pair(aux, i)); n         aux = 0; n       } n     } n   } n   visited[X] = visited[aux] = visited[X << 1] = true; n   if (aux != 0) { n     groups.push_back(make_pair(X, aux)); n     rejected.push_back(X << 1); n   } else { n     groups.push_back(make_pair(X, X << 1)); n   } n } n


#include <bits/stdc++.h> n int kx[8] = {+2, +2, -2, -2, +1, +1, -1, -1}; n int ky[8] = {+1, -1, +1, -1, +2, -2, +2, -2}; n int d9x[9] = {+1, +1, +1, +0, +0, +0, -1, -1, -1}; n int d9y[9] = {+1, +0, -1, +1, +0, -1, +1, +0, -1}; n int dx4[4] = {+0, +0, +1, -1}; n int dy4[4] = {+1, -1, +0, +0}; n long long int gcd(unsigned long long int a, unsigned long long int b) { n   return (a == 0) ? b : gcd(b % a, a); n } n long long int lcm(unsigned long long int a, unsigned long long int b) { n   return a * (b / gcd(a, b)); n } n const long long INF = 1e18; n using namespace std; n int main() { n   int T; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     int a[n]; n     int earliest[n + 1]; n     memset(earliest, -1, sizeof(earliest)); n     for (int i = 0; i < n; ++i) { n       cin >> a[i]; n     } n     for (int i = 0; i < n; ++i) { n       if (earliest[a[i]] == -1) { n         earliest[a[i]] = i; n       } else if (earliest[a[i]] < i - 1) { n         cout <<  YES n ; n         goto end_of_loop; n       } n     } n     cout <<  NO n ; n   end_of_loop:; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> adj[200001]; n long long a[200001]; n vector<long long> start; n stack<long long> last; n long long ans; n bool f; n void dfs(long long); n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   set<long long> roots; n   for (long long i = 1; i <= n; i++) roots.insert(i); n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   long long x; n   for (long long i = 1; i <= n; i++) { n     cin >> x; n     if (x != -1) { n       adj[x].push_back(i); n       roots.erase(i); n     } n   } n   start.clear(); n   while (!last.empty()) last.pop(); n   ans = 0; n   for (auto x : roots) { n     dfs(x); n   } n   cout << ans <<   n ; n   for (auto x : start) { n     cout << x <<    ; n   } n   while (!last.empty()) { n     cout << last.top() <<    ; n     last.pop(); n   } n } n void dfs(long long v) { n   for (auto x : adj[v]) { n     dfs(x); n     if (a[x] > 0) a[v] += a[x]; n   } n   ans += a[v]; n   if (a[v] > 0) { n     start.push_back(v); n   } else { n     last.push(v); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int output = 0; n int N, consCats; n vector<int> adj[999999]; n int vCats[999999]; n bool visited[999999]; n bool leafs[999999]; n bool aux; n void deep(int s, bool visited[], vector<int> adj[], int consCats, int var, n           int vCats[]); n int main() { n   cin >> N >> consCats; n   for (int i = 1; i <= N; i++) { n     visited[i] = false; n     leafs[i] = false; n   } n   int cat; n   for (int i = 1; i <= N; i++) { n     cin >> cat; n     vCats[i] = cat; n   } n   for (int i = 1; i < N; i++) { n     int x, y; n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   deep(1, visited, adj, consCats, 0, vCats); n   cout << output; n   return 0; n } n void deep(int s, bool visited[], vector<int> adj[], int consCats, int var, n           int vCats[]) { n   if (visited[s]) return; n   visited[s] = true; n   aux = true; n   for (int i = 0; i < adj[s].size(); i++) { n     if (visited[adj[s][i]] == false) { n       aux = false; n       break; n     } n   } n   (aux) ? leafs[s] = true : leafs[s] = false; n   vCats[s] ? var += vCats[s] : var = 0; n   if (leafs[s] && var <= consCats) { n     output++; n     return; n   } n   for (auto u : adj[s]) { n     if (var > consCats) n       continue; n     else n       deep(u, visited, adj, consCats, var, vCats); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n long long i, n, x, m, k, ans; n int main() { n   cin >> n; n   x = n; n   while (x) { n     x /= 10; n     m++; n   } n   ans = n * m + m - 1; n   k = 1; n   for (i = 0; i < m - 1; i++) { n     k *= 10; n     ans -= k; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ost(int k, int res) { n   int ans = 0; n   while (k < (double)res / 2) { n     ans++; n     k *= 2; n   } n   return ans; n } n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a.begin(), a.end()); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (k < ((double)a[i]) / 2) ans += ost(k, a[i]); n     k = max(k, a[i]); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string scan(); n const int mod = 1000000007; n const int MAXN = 10010; n int sta[MAXN], stb[MAXN]; n long long s, x; n long long memo[MAXN][2]; n long long bt(int i, int rem) { n   if (i > 52 && rem == 0) return 1; n   if (i == 60) return 0; n   long long &ret = memo[i][rem]; n   if (ret != -1) return ret; n   ret = 0; n   if (sta[i] == 1 && ((rem + 1) % 2) == stb[i]) n     return ret = 2 * bt(i + 1, (rem + 1) / 2); n   if (sta[i] == 1) return rem = 0; n   if ((rem + 2) % 2 == stb[i]) ret += bt(i + 1, (rem + 2) / 2); n   if (rem % 2 == stb[i]) ret += bt(i + 1, 0); n   return ret; n } n int main() { n   memset(memo, -1, sizeof memo); n   long long a, b; n   cin >> a >> b; n   s = a, x = b; n   for (int i = 0; a; a /= 2, i++) stb[i] = a % 2; n   for (int i = 0; b; b /= 2, i++) sta[i] = b % 2; n   cout << bt(0, 0) - (s == x ? 2 : 0) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int mi = *min_element(a, a + n); n   int p = -1; n   int f = 0; n   int ans = INT_MAX; n   for (int i = 0; i < n; i++) { n     if (a[i] == mi and !f) { n       f = 1; n       p = i; n     } else if (a[i] == mi and f) { n       ans = min(ans, i - p); n       p = i; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int a[20007]; n int main() { n   ios_base::sync_with_stdio(0); n   int t = 1; n   while (t--) { n     int n; n     cin >> n; n     map<int, int> mp; n     for (int i = 0; i < n; i++) { n       int x, y; n       cin >> x >> y; n       mp[x] = y; n       if (mp[x] == -1 * mp[x + y]) { n         cout <<  YES ; n         return 0; n       } n     } n     cout <<  NO  n          <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v[100010]; n long long dp[100010]; n void dfs(long long i, long long first) { n   dp[first]++; n   for (auto x : v[i]) { n     dfs(x, first + 1); n   } n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   for (long long i = 2; i <= n; i++) { n     long long x; n     cin >> x; n     v[x].push_back(i); n   } n   long long ans = 0; n   dfs(1, 1); n   for (long long i = 1; i <= n; i++) { n     ans += (dp[i] % 2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int num; n   string ss; n }; n unordered_map<string, node> mm; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   while (cin >> n) { n     mm.clear(); n     for (int i = 0; i < n; i++) { n       string s; n       cin >> s; n       for (int j = 0; j < s.length(); j++) { n         for (int k = 1; k <= (s.length() - j); k++) { n           string sb = s.substr(j, k); n           if (mm.find(sb) != mm.end() && mm[sb].ss != s) { n             mm[sb].num++; n             mm[sb].ss = s; n           } else if (mm.find(sb) == mm.end()) { n             node temp; n             temp.num = 1; n             temp.ss = s; n             mm[sb] = temp; n           } n         } n       } n     } n     int q; n     cin >> q; n     for (int i = 0; i < q; i++) { n       string s; n       cin >> s; n       if (mm.find(s) != mm.end()) { n         cout << mm[s].num <<     << mm[s].ss << endl; n       } else { n         cout << 0 <<   -  << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 100005; n long long p = 1e9 + 7; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   long long sum = 0; n   for (long long i = 1; i <= n; i++) { n     long long a; n     cin >> a; n     sum += a; n   } n   for (long long i = 1; i <= n; i++) { n     long long a; n     cin >> a; n     sum -= a; n   } n   if (sum < 0) n     cout <<  No ; n   else n     cout <<  Yes ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n const int maxn = 2e5 + 10; n const int inf = 0x7fffffff; n const long long INF = 9E18; n const int mod = 1e9 + 7; n const long long mod2 = 1e9 + 9; n const int eps = 1e-7; n const double pi = acos(-1.0); n template <typename T> n inline void read(T &x) { n   T f = 1; n   char ch = getchar(); n   for (; ch <  0  || ch >  9 ; ch = getchar()) n     if (ch ==  - ) f = -1; n   for (x = 0; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   x *= f; n } n struct matrix { n   long long a[2][2]; n   matrix() { memset(a, 0, sizeof a); } n   matrix(long long x, long long y, long long p, long long q) { n     a[0][0] = x; n     a[0][1] = y; n     a[1][0] = p; n     a[1][1] = q; n   } n   matrix operator*(const matrix b) { n     matrix c; n     for (int i = 0; i <= 1; i++) n       for (int j = 0; j <= 1; j++) n         for (int k = 0; k <= 1; k++) n           c.a[i][j] = (c.a[i][j] + ((a[i][k] * b.a[k][j]) % mod)) % mod; n     return c; n   } n   matrix operator+(const matrix b) { n     matrix c; n     for (int i = 0; i <= 1; i++) n       for (int j = 0; j <= 1; j++) c.a[i][j] = (a[i][j] + b.a[i][j]) % mod; n     return c; n   } n   bool operator==(const matrix b) { n     for (int i = 0; i <= 1; i++) { n       for (int j = 0; j <= 1; j++) { n         if (a[i][j] != b.a[i][j]) return 0; n       } n     } n     return 1; n   } n   matrix operator^(long long x) { n     matrix c(1, 0, 0, 1), b = *this; n     while (x) { n       if (x & 1) c = c * b; n       b = b * b; n       x >>= 1; n     } n     return c; n   } n } change(1, 1, 1, 0), ori(1, 0, 0, 0), tree[maxn << 2], lazy[maxn << 2], n     I(1, 0, 0, 1); n long long a[maxn]; n int n, m; n void push_up(int rt) { tree[rt] = tree[rt << 1] + tree[rt << 1 | 1]; } n void push_down(int rt) { n   if (lazy[rt] == I) return; n   lazy[rt << 1] = lazy[rt << 1] * lazy[rt]; n   lazy[rt << 1 | 1] = lazy[rt << 1 | 1] * lazy[rt]; n   tree[rt << 1] = tree[rt << 1] * lazy[rt]; n   tree[rt << 1 | 1] = tree[rt << 1 | 1] * lazy[rt]; n   lazy[rt] = I; n } n void build(int l, int r, int rt) { n   lazy[rt] = I; n   if (l == r) { n     tree[rt] = ori * (change ^ (a[l] - 1)); n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, rt << 1); n   build(mid + 1, r, rt << 1 | 1); n   push_up(rt); n } n void update(int l, int r, int rt, int x, int y, matrix val) { n   if (x <= l && r <= y) { n     tree[rt] = (tree[rt] * val); n     lazy[rt] = lazy[rt] * val; n     return; n   } n   push_down(rt); n   int mid = (l + r) >> 1; n   if (x <= mid) update(l, mid, rt << 1, x, y, val); n   if (y > mid) update(mid + 1, r, rt << 1 | 1, x, y, val); n   push_up(rt); n } n long long query(int l, int r, int rt, int x, int y) { n   push_down(rt); n   if (x <= l && r <= y) return ((tree[rt].a[0][0] + mod) % mod); n   int mid = (l + r) >> 1; n   long long ans = 0; n   if (x <= mid) ans += query(l, mid, rt << 1, x, y); n   if (y > mid) ans += query(mid + 1, r, rt << 1 | 1, x, y); n   return ((ans + mod) % mod); n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) read(a[i]); n   build(1, n, 1); n   while (m--) { n     int op, l, r; n     scanf( %d%d%d , &op, &l, &r); n     if (op == 1) { n       long long x; n       read(x); n       update(1, n, 1, l, r, change ^ x); n     } else { n       cout << query(1, n, 1, l, r) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long double pi = acos(-1); n const int MOD = 1e9 + 7; n int ocor[26]; n int prefix[100010][26]; n int blaublau[100010]; n string s, t; n bool confere(int idx) { n   int cu = blaublau[idx]; n   int aux[26]; n   for (int i = 0; i < 26; i++) aux[i] = prefix[idx][i]; n   idx -= t.size(); n   if (idx >= 0) { n     cu -= blaublau[idx]; n     for (int i = 0; i < 26; i++) aux[i] -= prefix[idx][i]; n   } n   for (int i = 0; i < 26; i++) { n     if (aux[i] < ocor[i]) { n       if (cu >= ocor[i] - aux[i]) { n         cu -= (ocor[i] - aux[i]); n       } else n         return false; n     } n   } n   return true; n } n int main() { n   ios::sync_with_stdio(false); n   ; n   cin >> s >> t; n   memset(prefix, 0, sizeof(prefix)); n   memset(blaublau, 0, sizeof(blaublau)); n   memset(ocor, 0, sizeof(ocor)); n   for (int i = 0; i < s.size(); i++) { n     int aux = 0; n     if (s[i] ==  ? ) n       blaublau[i]++; n     else n       prefix[i][s[i] -  a ] = 1; n     if (i == 0) continue; n     blaublau[i] += blaublau[i - 1]; n     for (int j = 0; j < 26; j++) { n       prefix[i][j] += prefix[i - 1][j]; n     } n   } n   for (int i = 0; i < t.size(); i++) ocor[t[i] -  a ]++; n   int r = 0; n   for (int i = t.size() - 1; i < s.size(); i++) { n     if (confere(i)) r++; n   } n   cout << r <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n vector<int> digits; n vector<vector<bool>> dp; n vector<int> bin_digits; n bool can; n void In() { n   cin >> n >> k; n   digits.resize(n); n   string next; n   for (int i = 0; i < n; i++) { n     cin >> next; n     digits[i] = stoi(next, 0, 2); n   } n } n int GetDistance(int from, int result) { n   int count = 0; n   for (int bit = 0; bit <= 6; bit++) { n     int num = 1 << bit; n     int first = from & num; n     int second = result & num; n     if (first != second) { n       if (first == 0) n         count++; n       else n         return -1; n     } n   } n   return count; n } n bool Solve() { n   bin_digits.push_back(stoi( 1110111 , 0, 2)); n   bin_digits.push_back(stoi( 0010010 , 0, 2)); n   bin_digits.push_back(stoi( 1011101 , 0, 2)); n   bin_digits.push_back(stoi( 1011011 , 0, 2)); n   bin_digits.push_back(stoi( 0111010 , 0, 2)); n   bin_digits.push_back(stoi( 1101011 , 0, 2)); n   bin_digits.push_back(stoi( 1101111 , 0, 2)); n   bin_digits.push_back(stoi( 1010010 , 0, 2)); n   bin_digits.push_back(stoi( 1111111 , 0, 2)); n   bin_digits.push_back(stoi( 1111011 , 0, 2)); n   dp.resize(n + 1, vector<bool>(k + 1, false)); n   dp[n][0] = true; n   for (int n_index = n - 1; n_index >= 0; n_index--) n     for (int i = 0; i <= k; i++) n       for (int j = 0; j <= 9; j++) { n         int next_distance = GetDistance(digits[n_index], bin_digits[j]); n         if (next_distance == -1) continue; n         if (i - next_distance < 0) continue; n         if (dp[n_index + 1][i - next_distance]) dp[n_index][i] = true; n       } n   if (!dp[0][k]) return false; n   return true; n } n void Out() { n   if (!can) { n     cout << -1 << endl; n     return; n   } n   for (int n_index = 0; n_index < n; n_index++) { n     int distance; n     int next_digit; n     for (int j = 0; j <= 9; j++) { n       int next_distance = GetDistance(digits[n_index], bin_digits[j]); n       if (next_distance == -1) continue; n       if (k - next_distance < 0) continue; n       if (dp[n_index + 1][k - next_distance]) { n         next_digit = j; n         distance = next_distance; n       } n     } n     k -= distance; n     cout << next_digit; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   In(); n   can = Solve(); n   Out(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n void solve() { n   int n; n   cin >> n; n   vector<int> v(n); n   map<int, int> mpp; n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n     mpp[v[i]]++; n   } n   int ans1 = 0, ans2 = 0; n   bool flag = false; n   for (int i = 0; i <= 100; i++) { n     if (mpp[i] >= 2) { n       if (!flag) { n         ans1++; n       } n       ans2++; n     } else if (mpp[i] == 1) { n       flag = true; n       ans2++; n     } else { n       break; n     } n   } n   cout << ans1 + ans2 <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   cin >> t; n   while (t--) solve(); n   cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC n        <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int a[1010]; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   sort(a, a + n); n   if (a[0] == 1) { n     printf( -1 n ); n   } else { n     printf( 1 n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const uint64_t seed = n     std::chrono::system_clock::now().time_since_epoch().count(); n mt19937_64 rnd(seed); n const int MOD = 998244353; n struct Dinic { n   struct Edge { n     int from, to; n     long long cap, flow; n     Edge(int u, int v, long long c) : from(u), to(v), cap(c), flow(0) {} n   }; n   int n, m, s, t; n   vector<vector<int>> g; n   vector<Edge> edges; n   vector<int> dis; n   vector<int> cur; n   int curFlow; n   vector<bool> cut; n   Dinic(int n, int s, int t) n       : n(n), s(s), t(t), g(n), dis(n), cur(n), cut(n, false) { n     curFlow = 0; n     cut[0] = true; n   } n   void addEdge(int from, int to, long long cap) { n     edges.push_back(Edge(from, to, cap)); n     edges.push_back(Edge(to, from, 0)); n     m = edges.size(); n     g[from].push_back(m - 2); n     g[to].push_back(m - 1); n   } n   bool bfs() { n     fill(dis.begin(), dis.end(), n); n     dis[s] = 0; n     queue<int> qq; n     qq.push(s); n     int cnt = 0; n     while (!qq.empty()) { n       cnt++; n       int len = qq.size(); n       while (len--) { n         int p = qq.front(); n         qq.pop(); n         for (int a : g[p]) { n           Edge &e = edges[a]; n           if (e.flow >= e.cap) continue; n           if (dis[e.to] == n) { n             dis[e.to] = cnt; n             qq.push(e.to); n           } n         } n       } n     } n     return dis[t] != n; n   } n   long long dfs(int x, long long a) { n     if (x == t || a == 0) return a; n     long long flow = 0, f; n     for (; cur[x] < g[x].size(); cur[x]++) { n       Edge &e = edges[g[x][cur[x]]]; n       if (e.cap > e.flow && dis[x] + 1 == dis[e.to] && n           (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) { n         flow += f; n         e.flow += f; n         edges[g[x][cur[x]] ^ 1].flow -= f; n         a -= f; n         if (a == 0) break; n       } n     } n     return flow; n   } n   long long maxFlow() { n     long long flow = 0; n     while (bfs()) { n       fill(cur.begin(), cur.end(), 0); n       flow += dfs(s, INT_MAX); n     } n     return flow; n   } n   long long dfs2(int x, long long limit, vector<bool> &vis) { n     vis[x] = true; n     if (x == t) return limit; n     for (int ne : g[x]) { n       Edge &e = edges[ne]; n       if (e.cap > e.flow && !vis[e.to] && !cut[e.to]) { n         int r = dfs2(e.to, min(limit, e.cap - e.flow), vis); n         if (r > 0) { n           e.flow += r; n           edges[ne ^ 1].flow -= r; n           return r; n         } n       } n     } n     return 0; n   } n   long long work(int node) { n     int e1, e2; n     for (int i = 0; i < g[t].size(); i++) { n       if (edges[g[t][i]].to == node) { n         e1 = g[t][i]; n         break; n       } n     } n     for (int i = 0; i < g[node].size(); i++) { n       if (edges[g[node][i]].to == t) { n         e2 = g[node][i]; n         break; n       } n     } n     g[t].erase(find(g[t].begin(), g[t].end(), e1)); n     g[s].push_back(e1); n     edges[e1].from = s; n     edges[e1].cap = 1e9; n     edges[e2].to = s; n     curFlow -= edges[e2].flow; n     do { n       vector<bool> vis(n, false); n       vis[s] = 0; n       long long f = dfs2(node, edges[e2].cap - edges[e2].flow, vis); n       edges[e2].flow += f; n       edges[e2 ^ 1].flow -= f; n       curFlow += f; n       if (f == 0) { n         swap(cut, vis); n         break; n       } n     } while (true); n     return curFlow; n   } n }; n void solve(int caseNum) { n   int n; n   cin >> n; n   vector<vector<int>> a(n, vector<int>(n)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> a[i][j]; n     } n   } n   int dd[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; n   Dinic dinic(n * n + 2, n * n, n * n + 1); n   vector<pair<int, int>> nodes; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (a[i][j] == -1) continue; n       int n1 = i * n + j; n       if (a[i][j] > 0) { n         dinic.addEdge(n1, n * n + 1, 1e9); n         nodes.push_back({a[i][j], n1}); n       } n       for (int k = 0; k < 4; k++) { n         int ii = i + dd[k][0]; n         int jj = j + dd[k][1]; n         if (ii < 0 || ii >= n || jj < 0 || jj >= n) continue; n         int n2 = ii * n + jj; n         dinic.addEdge(n1, n2, 1); n       } n     } n   } n   sort(nodes.begin(), nodes.end()); n   int p = 0; n   long long ans = 0; n   int last = 0; n   long long lastF = 0; n   while (p < nodes.size()) { n     int q = p; n     ans += (nodes[p].first - last) * lastF; n     while (q < nodes.size() && nodes[q].first == nodes[p].first) { n       int node = nodes[q].second; n       dinic.work(node); n       q++; n     } n     lastF = dinic.curFlow; n     last = nodes[p].first; n     p = q; n   } n   cout << ans <<   n ; n } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.precision(10); n   int T = 1; n   for (int i = 1; i <= T; i++) { n     solve(i); n   } n   cout.flush(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (1 << 30) + 7; n const int N = 1e5 + 7; n int main() { n   ios::sync_with_stdio(false); n   int q, l, r, d, ans; n   cin >> q; n   while (q--) { n     cin >> l >> r >> d; n     ans = (r / d + 1) * d; n     if (d < l) n       ans = d; n     else n       ans = (r / d + 1) * d; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T& x) { n   bool fu = 0; n   char c; n   for (c = getchar(); c <= 32; c = getchar()) n     ; n   if (c ==  - ) fu = 1, c = getchar(); n   for (x = 0; c > 32; c = getchar()) x = x * 10 + c -  0 ; n   if (fu) x = -x; n }; n template <class T> n inline void read(T& x, T& y) { n   read(x); n   read(y); n } n template <class T> n inline void read(T& x, T& y, T& z) { n   read(x); n   read(y); n   read(z); n } n inline char getc() { n   char c; n   for (c = getchar(); c <= 32; c = getchar()) n     ; n   return c; n } n int n, p, i, j, k, l, g, x; n int a[50010]; n int pos[40], c[40]; n long long len[1111]; n char f[33][32][50010], f2[33][32][50010]; n int v[11111]; n long long pow(long long a, long long b, long long c) { n   long long ans = 1; n   for (a %= c; b; b >>= 1, a = a * a % c) n     if (b & 1) ans = ans * a % c; n   return ans; n } n void xx(char& x, char y) { x = min(10, x + y); } n int main() { n   read(n, p); n   for (i = 1; i <= n; i++) { n     read(x); n     if (x < 32) { n       g++; n       pos[g] = i; n       c[g] = x; n       for (len[g] = 1; len[g] <= c[g]; len[g] *= 10) n         ; n     } n   } n   f[0][0][0] = 1; n   for (i = 0; i <= g - 1; i++) n     for (j = 0; j <= 31; j++) n       for (k = 0; k <= p - 1; k++) n         if (f[i][j][k] > 0) { n           xx(f[i + 1][j ^ c[i + 1]][(1LL * k * len[i + 1] + c[i + 1]) % p], n              f[i][j][k]); n           f2[i + 1][j ^ c[i + 1]][(1LL * k * len[i + 1] + c[i + 1]) % p] = 1; n           xx(f[i + 1][j][k], f[i][j][k]); n         } n   if (f[g][0][0] < 2) { n     printf( No n ); n     return 0; n   } n   printf( Yes n ); n   i = g; n   j = 0; n   k = 0; n   while (i) { n     if (f2[i][j][k]) v[++v[0]] = pos[i]; n     if (f2[i][j][k]) { n       bool h = 0; n       for (int j2 = (0); j2 <= (31); j2++) { n         for (int k2 = (0); k2 <= (p - 1); k2++) n           if (f[i - 1][j2][k2]) n             if ((j2 ^ c[i]) == j) n               if ((1LL * k2 * len[i] + c[i]) % p == k) { n                 j = j2; n                 k = k2; n                 h = 1; n                 break; n               } n         if (h) break; n       } n     } n     i--; n   } n   printf( %d n , v[0]); n   for (i = v[0]; i >= 1; i--) printf( %d  , v[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void read() {} n int main() { n   read(); n   string s; n   while (cin >> s) { n     int l = s.size(); n     string ans; n     bool f = 0; n     for (int i = 0; i < l; i++) { n       if (i != 0) { n         if (s[i] ==  a  && i + 1 < l - 1 && s[i + 1] ==  t  && !f) { n           ans.push_back( @ ); n           i += 1; n           f = 1; n           continue; n         } else if (s[i] ==  d  && i + 2 < l - 1 && s[i + 1] ==  o  && n                    s[i + 2] ==  t ) { n           ans.push_back( . ); n           i += 2; n           continue; n         } n       } n       ans.push_back(s[i]); n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   sort(arr, arr + n); n   int c[1005] = {0}; n   int u[1005] = {0}, t = 0; n   for (int i = 0; i < n; i++) { n     if (c[arr[i]] == 0) { n       u[t++] = arr[i]; n     } n     c[arr[i]]++; n   } n   int fans = 0; n   while (1) { n     int ans = -1; n     for (int i = 0; i < t; i++) { n       if (c[u[i]] > 0) { n         ans++; n         c[u[i]]--; n       } n     } n     if (ans <= 0) break; n     fans += ans; n   } n   cout << fans; n } n


#include <bits/stdc++.h> n using namespace std; n long long int a, b, c, d, n, ans = 0; n int main() { n   cin >> a; n   cin >> b; n   cin >> c; n   cin >> d; n   if ((a + b > c && a + c > b && b + c > a) || n       (a + b > d && a + d > b && b + d > a) || n       (a + d > c && a + c > d && d + c > a) || n       (d + b > c && d + c > b && b + c > d)) { n     cout <<  TRIANGLE ; n   } else if ((a + b == c) || (a + b == d) || (a + c == b) || (a + c == d) || n              (a + d == c) || (a + d == b) || (b + c == a) || (b + c == d) || n              (b + d == a) || (b + d == c) || (c + d == a) || (c + d == b)) { n     cout <<  SEGMENT ; n   } else { n     cout <<  IMPOSSIBLE ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int _inf = 0xc0c0c0c0; n const long long INF = 0x3f3f3f3f3f3f3f3f; n const long long _INF = 0xc0c0c0c0c0c0c0c0; n const long long mod = (int)1e9 + 7; n const int N = 3e5 + 100; n vector<int> vc[N], e[N]; n int col[N]; n int vis[N]; n int mx = 0; n void dfs(int o, int u) { n   for (int v : vc[u]) { n     vis[col[v]]++; n   } n   int b = 1; n   for (int v : vc[u]) { n     if (col[v]) continue; n     while (vis[b]) ++b; n     vis[b] = 1; n     col[v] = b; n   } n   mx = max(mx, b); n   for (int v : vc[u]) { n     vis[col[v]] = 0; n   } n   for (int v : e[u]) { n     if (v == o) continue; n     dfs(u, v); n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) { n     int si, tv; n     scanf( %d , &si); n     for (int j = 1; j <= si; ++j) { n       scanf( %d , &tv); n       vc[i].push_back(tv); n     } n   } n   int u, v; n   for (int i = 1; i < n; ++i) { n     scanf( %d%d , &u, &v); n     e[u].push_back(v); n     e[v].push_back(u); n   } n   dfs(0, 1); n   printf( %d n , mx); n   for (int i = 1; i <= m; ++i) { n     if (!col[i]) col[i] = 1; n     printf( %d%c , col[i],    n [i == m]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   for (int l = 0; l < n; l++) { n     string b; n     cin >> b; n     string a; n     a = b[0]; n     string c; n     for (int i = 1; i <= b.length(); i += 2) { n       c = b[i]; n       a += c; n     } n     cout << a <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 1500000; n const long long MAX = 10000000000000000; n template <typename T> n T abs(T a) { n   return a < 0 ? -a : a; n } n int main() { n   int n, m; n   cin >> n >> m; n   vector<vector<int> > graph(n); n   for (long long i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     graph[u - 1].push_back(v - 1); n     graph[v - 1].push_back(u - 1); n   } n   vector<int> val(n, 0); n   set<int> q; n   vector<int> flag(n, 0); n   vector<int> a(n); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     if (!a[i]) { n       q.insert(i); n     } n   } n   vector<int> ans; n   while (!q.empty()) { n     int top = *q.begin(); n     q.erase(q.begin()); n     if (flag[top]) { n       return 1; n     } n     val[top]++; n     flag[top] = true; n     ans.push_back(top); n     for (vector<int>::iterator i = graph[top].begin(); i != graph[top].end(); n          i++) { n       val[*i]++; n       if (val[*i] == a[*i]) { n         q.insert(*i); n       } n     } n   } n   cout << ans.size() << endl; n   for (vector<int>::iterator i = ans.begin(); i != ans.end(); i++) { n     cout << (*i) + 1 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a, b; n   scanf( %d%d , &a, &b); n   if (a > b) n     printf( %d %d n , b, (a - b) / 2); n   else n     printf( %d %d n , a, (b - a) / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   char grid[n][n]; n   int hash_count = 0, cross = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> grid[i][j]; n       if (grid[i][j] ==  # ) hash_count++; n     } n   } n   for (int i = 0; i < n - 2; i++) { n     for (int j = 0; j < n - 2; j++) { n       if (grid[i][j + 1] ==  #  && grid[i + 1][j] ==  #  && n           grid[i + 1][j + 1] ==  #  && grid[i + 1][j + 2] ==  #  && n           grid[i + 2][j + 1] ==  # ) { n         cross += 5; n         grid[i][j + 1] =  x ; n         grid[i + 1][j] =  x ; n         grid[i + 1][j + 1] =  x ; n         grid[i + 1][j + 2] =  x ; n         grid[i + 2][j + 1] =  x ; n       } n     } n   } n   if (hash_count == cross) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n, k; n   cin >> n >> k; n   vector<int> v(n); n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n     sum += v[i]; n   } n   if (sum % k != 0) n     cout <<  No n ; n   else { n     int num = sum / k; n     for (int i = 1; i < n; i++) { n       v[i] += v[i - 1]; n     } n     int cnt = 0; n     vector<int> res; n     int q = 0; n     for (int i = 0; i < n; i++) { n       q++; n       if (v[i] % num == 0) { n         cnt++; n         res.push_back(q); n         q = 0; n       } n     } n     if (cnt == k) { n       cout <<  Yes n ; n       for (int i = 0; i < k; i++) cout << res[i] <<    ; n     } else n       cout <<  No n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n long long a[200200]; n int f(int x) { return ((x % n) + n) % n; } n int main() { n   scanf( %d%d , &n, &m); n   m--; n   for (int i = (0); i < (n); i++) cin >> a[i]; n   int p = m; n   for (int i = (1); i <= (n); i++) n     if (a[p] >= a[f(m + i)]) p = f(m + i); n   long long tmp = a[p] * n + f(m - p); n   for (int i = (0); i < (n); i++) n     if (i != p) a[i] -= a[p]; n   for (int i = (0); i < (n); i++) { n     a[f(i + p)]--; n     if (f(i + p) == m) break; n   } n   a[p] = tmp; n   cout << a[0]; n   for (int i = (1); i < (n); i++) cout <<     << a[i]; n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v[10001]; n long long n, ass1, ass2; n int main() { n   v[1].push_back(1); n   v[1].push_back(2); n   v[2].push_back(1); n   v[2].push_back(3); n   v[3].push_back(2); n   v[3].push_back(3); n   cin >> n; n   ass1 = ass2 = 3; n   while (ass1 + ass2 <= n) { n     v[1].push_back(ass2 + 1); n     v[ass1 + 1].push_back(ass2 + 1); n     for (int i = 2; i <= ass1; i++) { n       v[i].push_back(ass2 + i); n       v[ass1 + 1].push_back(ass2 + i); n     } n     ass2 += ass1; n     ass1++; n   } n   cout << ass1 << endl; n   for (int i = 1; i <= ass1; i++) { n     for (int j = 0; j < v[i].size(); j++) { n       if (j != 0) cout <<    ; n       cout << v[i][j]; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt = -1; n struct node { n   bool op; n   string val; n   int op1, op2; n   int type; n   node() { n     op = 0; n     val =   ; n     type = 0; n   } n } m[5020]; n map<string, int> ind; n inline int cti(char i) { return (int)(i -  0 ); } n vector<string> vct; n bool ansx[2020], ansm[2020]; n int tb[5020]; n int main() { n   int n, mm; n   string t, p; n   cin >> n >> mm; n   for (int i = 0; i < n; i++) { n     cin >> t; n     if (ind.find(t) == ind.end()) { n       ind[t] = ++cnt; n     } n     cin >> p; n     cin >> p; n     if (p[0] ==  1  or p[0] ==  0 ) { n       m[cnt].val = p; n       continue; n     } n     m[cnt].op = 1; n     if (p ==  ? ) n       m[cnt].op1 = -1; n     else n       m[cnt].op1 = ind[p]; n     cin >> p; n     if (p[0] ==  O ) m[cnt].type = 1; n     if (p[0] ==  A ) m[cnt].type = 2; n     if (p[0] ==  X ) m[cnt].type = 3; n     cin >> p; n     if (p ==  ? ) n       m[cnt].op2 = -1; n     else n       m[cnt].op2 = ind[p]; n   } n   int x0, x1, pic1, pic2; n   for (int i = 0; i < mm; i++) { n     x0 = x1 = 0; n     for (int k = 0; k < n; k++) { n       auto j = m[k]; n       if (j.op == 0) { n         x0 += cti(j.val[i]); n         tb[k] = cti(j.val[i]); n       } else { n         if (j.op1 == -1) n           pic1 = 0; n         else n           pic1 = tb[j.op1]; n         if (j.op2 == -1) n           pic2 = 0; n         else n           pic2 = tb[j.op2]; n         if (j.type == 1) { n           x0 += pic1 | pic2; n           tb[k] = pic1 | pic2; n         } else if (j.type == 2) { n           x0 += pic1 & pic2; n           tb[k] = pic1 & pic2; n         } else { n           x0 += pic1 ^ pic2; n           tb[k] = pic1 ^ pic2; n         } n       } n     } n     for (int k = 0; k < n; k++) { n       auto j = m[k]; n       if (j.op == 0) { n         x1 += cti(j.val[i]); n         tb[k] = cti(j.val[i]); n       } else { n         if (j.op1 == -1) n           pic1 = 1; n         else n           pic1 = tb[j.op1]; n         if (j.op2 == -1) n           pic2 = 1; n         else n           pic2 = tb[j.op2]; n         if (j.type == 1) { n           x1 += pic1 | pic2; n           tb[k] = pic1 | pic2; n         } else if (j.type == 2) { n           x1 += pic1 & pic2; n           tb[k] = pic1 & pic2; n         } else { n           x1 += pic1 ^ pic2; n           tb[k] = pic1 ^ pic2; n         } n       } n     } n     if (x0 > x1) { n       ansx[i] = 0; n       ansm[i] = 1; n     } n     if (x0 == x1) { n       ansx[i] = 0; n       ansm[i] = 0; n     } n     if (x0 < x1) { n       ansx[i] = 1; n       ansm[i] = 0; n     } n   } n   for (int i = 0; i < mm; i++) cout << ansm[i]; n   cout << endl; n   for (int i = 0; i < mm; i++) cout << ansx[i]; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct T { n   int ind; n   int st; n   int ds; n   bool operator<(const T &one) const { n     if (st != one.st) return st < one.st; n     return ds < one.ds; n   } n } gr; n gr grps[10000]; n int n; n vector<int> mv; n int valid(int nt) { n   int en = 0; n   for (int i = 0; i < n; i++) { n     if (i == nt) continue; n     if (grps[i].st < en) return 0; n     en = max(en, grps[i].ds); n   } n   return 1; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> grps[i].st; n     cin >> grps[i].ds; n     grps[i].ind = i + 1; n   } n   sort(grps, grps + n); n   for (int i = 0; i < n; i++) { n     if (valid(i) == 1) mv.push_back(grps[i].ind); n   } n   cout << mv.size() << endl; n   sort(mv.begin(), mv.end()); n   for (int i = 0; i < mv.size(); i++) cout << mv[i] <<    ; n   return 0; n } n


#include<iostream> n #include<cstdlib> n #include<cstdio> n #include<cmath> n #include<iomanip> n #include<cstring> n #include<algorithm> n #include<ctime> n using namespace std; n const int MAXN=2010; n int read() n { n  int kkk=0,x=1; n  char c=getchar(); n  while((c< 0  || c> 9 ) && c!= - ) c=getchar(); n  if(c== - ) c=getchar(),x=-1; n  while(c>= 0  && c<= 9 ) kkk=kkk*10+(c- 0 ),c=getchar(); n  return kkk*x; n } n int n,X[MAXN],Y[MAXN],tag[MAXN],father[MAXN],id[MAXN],cur,ans[MAXN*MAXN][2],ANS,dl[MAXN],h,t,pd[MAXN]; n double Theta[MAXN]; n int find(int k){return father[k]==k?k:father[k]=find(father[k]);} n void solve(int S) n { n  h=t=0; n  dl[++t]=S; n  while(h!=t) n  { n   int u=dl[++h]; n   father[u]=S; n   if(tag[u]==S) break; n   dl[++t]=tag[u]; n  } n  if(t==1) pd[S]=1; n } n bool cmp(int x,int y){return Theta[x]<Theta[y];} n int main() n { n  n=read(); n  for(int i=1;i<=n;++i) X[i]=read(),Y[i]=read(),tag[i]=read(); n  for(int i=1;i<=n;++i) if(!father[i]) solve(i); n  for(int i=1;i<=n;++i) if(!pd[i]) n  { n   if(!cur) cur=i; n   else if(X[i]<X[cur] || (X[i]==X[cur] && Y[i]<Y[cur])) cur=i; n  } n  for(int i=1;i<=n;++i) Theta[i]=atan2(Y[i]-Y[cur],X[i]-X[cur]); n  for(int i=1;i<=n;++i) id[i]=i; n  sort(id+1,id+n+1,cmp); n  pd[cur]=1; n  for(int i=1,las=0;i<=n;++i) if(!pd[id[i]]) n  { n   if(las) n   { n    int alas=find(las),aid=find(id[i]); n    if(alas!=aid) n    { n     father[alas]=aid; n     ans[++ANS][0]=las; n     ans[ANS][1]=id[i]; n     swap(tag[las],tag[id[i]]); n    } n   } n   las=id[i]; n  } n  while(tag[cur]!=cur) n  { n   ans[++ANS][0]=cur; n   ans[ANS][1]=tag[cur]; n   swap(tag[cur],tag[tag[cur]]); n  } n  printf( %d n ,ANS); n  for(int i=1;i<=ANS;++i) printf( %d %d n ,ans[i][0],ans[i][1]); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int read() { n   char ch = getchar(); n   int x = 0, fl = 1; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) fl = -1; n   for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch -  0 ); n   return x * fl; n } n const int NN = 500000 + 17; n void open() { n   freopen( a.in ,  r , stdin); n   freopen( a.out ,  w , stdout); n } n void OK() { printf( OK n ); } n int n, q; n int c[NN]; n int ans[NN]; n vector<pair<int, int> > e[NN]; n pair<int, int> ep[NN]; n struct Tree { n   int fa[NN], dep[NN], d[NN], ll[NN], rr[NN], rev[NN]; n   int tim; n   struct node { n     int w[2]; n   } t[NN << 2]; n   int tag[NN << 2]; n   node merge(node a, node b) { n     node c; n     c.w[0] = max(a.w[0], b.w[0]); n     c.w[1] = max(a.w[1], b.w[1]); n     return c; n   } n   void set_tag(int rt) { n     tag[rt] ^= 1; n     swap(t[rt].w[0], t[rt].w[1]); n   } n   void psd(int rt) { n     if (tag[rt]) { n       set_tag(rt << 1); n       set_tag(rt << 1 | 1); n       tag[rt] = 0; n     } n   } n   void build(int rt, int l, int r) { n     if (l == r) { n       t[rt].w[d[rev[l]]] = dep[rev[l]]; n       return; n     } n     int mid = (l + r) >> 1; n     build(rt << 1, l, mid); n     build(rt << 1 | 1, mid + 1, r); n     t[rt] = merge(t[rt << 1], t[rt << 1 | 1]); n   } n   void flip(int rt, int l, int r, int ll, int rr) { n     if (ll <= l && r <= rr) { n       set_tag(rt); n       return; n     } n     psd(rt); n     int mid = (l + r) >> 1; n     if (ll <= mid) flip(rt << 1, l, mid, ll, rr); n     if (rr > mid) flip(rt << 1 | 1, mid + 1, r, ll, rr); n     t[rt] = merge(t[rt << 1], t[rt << 1 | 1]); n   } n   void dfs(int x, int ff) { n     fa[x] = ff; n     dep[x] = dep[ff] + 1; n     ll[x] = ++tim; n     rev[tim] = x; n     for (int i = 0, top = e[x].size(); i < top; i++) { n       int y = e[x][i].first, z = e[x][i].second; n       if (y != ff) { n         d[y] = (d[x] + z) & 1; n         dfs(y, x); n       } n     } n     rr[x] = tim; n   } n   void work(int root) { n     dfs(root, 0); n     build(1, 1, n); n     for (int i = 1; i <= q; i++) { n       int x = ep[c[i]].first, y = ep[c[i]].second; n       if (fa[x] == y) swap(x, y); n       flip(1, 1, n, ll[y], rr[y]); n       ans[i] = max(ans[i], t[1].w[0]); n     } n   } n } tx, ty; n int mx, pos, px, py; n void dfs(int x, int dep, int ff) { n   if (dep > mx) mx = dep, pos = x; n   for (int i = 0, top = e[x].size(); i < top; i++) { n     int y = e[x][i].first; n     if (y != ff) dfs(y, dep + 1, x); n   } n } n void get_path() { n   dfs(1, 0, 0); n   px = pos; n   mx = -1; n   dfs(px, 0, 0); n   py = pos; n   tx.work(px); n   ty.work(py); n } n int main() { n   n = read(); n   for (int i = 1; i < n; i++) { n     int x = read(), y = read(), z = read(); n     e[x].push_back(make_pair(y, z)); n     e[y].push_back(make_pair(x, z)); n     ep[i] = make_pair(x, y); n   } n   q = read(); n   for (int i = 1; i <= q; i++) c[i] = read(); n   get_path(); n   for (int i = 1; i <= q; i++) { n     ans[i]--; n     printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int getDiff(int n, string s1, string s2) { n   int cnt = 0; n   for (int i = 0; i < n; i++) { n     if (s1[i] != s2[i]) { n       cnt++; n     } n   } n   return cnt; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   string s; n   cin >> s; n   string temp = s; n   for (int i = 0; i < n - 1; i++) { n     if (i < n - 2) { n       if (s[i] == s[i + 1] && s[i + 1] == s[i + 2] && s[i + 2] == s[i]) { n         if (s[i] ==  R ) { n           s[i + 1] =  G ; n         } else if (s[i] ==  G ) { n           s[i + 1] =  R ; n         } else { n           s[i + 1] =  R ; n         } n         continue; n       } n     } n     if (s[i] == s[i + 1]) { n       if (i == 0) { n         if (s[i] ==  R ) { n           s[i] =  G ; n         } else if (s[i] ==  G ) { n           s[i] =  R ; n         } else { n           s[i] =  R ; n         } n       } else { n         if (s[i - 1] ==  R ) { n           if (s[i] ==  B ) { n             s[i] =  G ; n           } else { n             s[i] =  B ; n           } n         } else if (s[i - 1] ==  B ) { n           if (s[i] ==  R ) { n             s[i] =  G ; n           } else { n             s[i] =  R ; n           } n         } else { n           if (s[i] ==  B ) { n             s[i] =  R ; n           } else { n             s[i] =  B ; n           } n         } n       } n     } n   } n   int ans = getDiff(n, s, temp); n   cout << ans <<   n ; n   cout << s <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, l[1010], r[1010]; n char a[1010]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i < n; i++) cin >> a[i]; n   l[1] = 0; n   for (int i = 1; i < n; i++) { n     if (a[i] ==  R ) l[i + 1] = l[i] + 1; n     if (a[i] ==  = ) l[i + 1] = l[i]; n     if (a[i] ==  L ) l[i + 1] = 0; n   } n   r[n] = 0; n   for (int i = n - 1; i >= 1; i--) { n     if (a[i] ==  L ) r[i] = r[i + 1] + 1; n     if (a[i] ==  = ) r[i] = r[i + 1]; n     if (a[i] ==  R ) r[i] = 0; n   } n   for (int i = 1; i <= n; i++) cout << max(l[i], r[i]) + 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e3 + 99; n complex<int> read() { n   int x, y; n   cin >> x >> y; n   return complex<int>(x, y); n } n long double dist(complex<int> a, complex<int> b) { n   return hypot(abs(a.real() - b.real()), abs(a.imag() - b.imag())); n } n complex<long double> conv(complex<int> a) { n   return complex<long double>(a.real(), a.imag()); n } n bool _90_deg(complex<int> a, complex<int> b, complex<int> c) { n   return fabs(((conj(((conv(b)) - (conv(a)))) * (((conv(c)) - (conv(b))))) n                    .real())) < 1e-9; n } n bool isSquare(vector<complex<int>> vec) { n   sort(vec.begin(), vec.end(), [](complex<int> a, complex<int> b) { n     if (a.real() == b.real()) return a.imag() < b.imag(); n     return a.real() < b.real(); n   }); n   long double len = dist(vec[0], vec[1]); n   if (fabs(len) < 1e-9) return false; n   vector<pair<int, int>> _ = {{0, 2}, {1, 3}, {2, 3}}; n   for (auto cur : _) { n     auto __ = dist(vec[cur.first], vec[cur.second]); n     if (fabs(len - dist(vec[cur.first], vec[cur.second])) > 1e-9) return false; n   } n   long double diag1 = dist(vec[1], vec[2]); n   long double diag2 = dist(vec[0], vec[3]); n   if (fabs(diag1 - diag2) > 1e-9) return false; n   swap(vec[2], vec[3]); n   for (int i = 0; i < 4; ++i) vec.push_back(vec[i]); n   for (int i = 2; i < vec.size(); ++i) { n     if (!_90_deg(vec[i - 2], vec[i - 1], vec[i])) { n       return false; n     } n   } n   return true; n } n vector<complex<int>> home; n int go(int i, vector<complex<int>> vec) { n   if (i >= 4) { n     return isSquare(vec) ? 0 : 1e9; n   } n   int ret = 1e9; n   for (int j = 0; j < 4; ++j) { n     ret = min(ret, go(i + 1, vec) + j); n     vec[i] -= home[i]; n     complex<int> r(-vec[i].imag(), vec[i].real()); n     vec[i] = r + home[i]; n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   home.resize(4); n   int t; n   cin >> t; n   while (t--) { n     vector<complex<int>> v(4); n     for (int i = 0; i < 4; ++i) { n       v[i] = read(); n       home[i] = read(); n     } n     int ret = go(0, v); n     cout << (ret > 1e8 ? -1 : ret) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:64000000 ) n const int N = 5e3 + 10; n int n; n int t[N]; n int cnt[N]; n int res[N]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> t[i]; n   } n   for (int r = 1; r <= n; r++) { n     memset(cnt, 0, sizeof(cnt)); n     int mxCnt = 0, val = -1; n     for (int l = r; l >= 1; l--) { n       cnt[t[l]]++; n       if (cnt[t[l]] > mxCnt || (cnt[t[l]] == mxCnt && t[l] < val)) { n         mxCnt = cnt[t[l]]; n         val = t[l]; n       } n       res[val]++; n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << res[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long freq[100005]; n int32_t main() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   sort(s.begin(), s.end()); n   reverse(s.begin(), s.end()); n   cout << s[0]; n   for (long long i = 1; i < n; i++) { n     if (s[i] ==  0 ) cout << s[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const int NMAX = 1e6; n struct eq { n   int x, c; n   bool operator<(const eq &aux) const { return x < aux.x; } n }; n eq a[200005], b[200005]; n stack<pair<int, int> > st; n void prec(int &n, eq a[]) { n   for (int i = 1; i <= n; ++i) scanf( %d%d , &a[i].x, &a[i].c); n   sort(a + 1, a + n + 1); n   st.push({a[1].x, a[1].c}); n   for (int i = 2; i <= n; ++i) { n     while (!st.empty() && st.top().second >= a[i].c) st.pop(); n     st.push({a[i].x, a[i].c}); n   } n   int sz = st.size(); n   n = sz; n   while (!st.empty()) { n     a[sz].x = st.top().first; n     a[sz].c = st.top().second; n     st.pop(); n     --sz; n   } n } n struct monster { n   int x, y, c; n   bool operator<(const monster &aux) const { return x < aux.x; } n }; n monster A[200005]; n long long Arb[NMAX * 4 + 5], pref[NMAX * 4 + 5]; n void update(int x, long long val, int st = 0, int dr = NMAX, int nod = 1) { n   if (st == dr) { n     Arb[nod] += val; n     pref[nod] = Arb[nod]; n     return; n   } n   int mij = (st + dr) / 2; n   if (x <= mij) n     update(x, val, st, mij, nod * 2); n   else n     update(x, val, mij + 1, dr, nod * 2 + 1); n   Arb[nod] = Arb[nod * 2] + Arb[nod * 2 + 1]; n   pref[nod] = n       max(Arb[nod], max(pref[nod * 2], Arb[nod * 2] + pref[nod * 2 + 1])); n } n int n, m, p; n int main() { n   scanf( %d%d%d , &n, &m, &p); n   prec(n, a); n   prec(m, b); n   for (int i = 1; i <= m; ++i) update(b[i - 1].x, -b[i].c + b[i - 1].c); n   update(b[m].x, -INF); n   for (int i = 1; i <= p; ++i) scanf( %d%d%d , &A[i].x, &A[i].y, &A[i].c); n   sort(A + 1, A + p + 1); n   int j = 1; n   long long Sol = -a[1].c - b[1].c; n   for (int i = 1; i <= n; ++i) { n     while (j <= p && A[j].x < a[i].x) { n       update(A[j].y, A[j].c); n       ++j; n     } n     Sol = max(Sol, pref[1] - a[i].c); n   } n   printf( %lld , Sol); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, x; n     cin >> n >> x; n     ; n     int a[n]; n     int b[n]; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       cin >> b[i]; n     } n     reverse(b, b + n); n     bool ans = 1; n     for (int i = 0; i < n; i++) { n       if (a[i] + b[i] > x) { n         ans = 0; n         break; n       } n     } n     if (ans) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n, a, s = 0, c = 0; n   cin >> n; n   for (long long int i = 0; i < n; i++) { n     cin >> a; n     s += a; n   } n   for (long long int i = 1; i <= 5; i++) { n     s += i; n     if (s % (n + 1) != 1) c++; n     s -= i; n   } n   cout << c; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const long long inf = mod * mod; n const long long d2 = (mod + 1) / 2; n const long double EPS = 1e-9; n const long double PI = acos(-1.0); n int ABS(int a) { return max(a, -a); } n long long ABS(long long a) { return max(a, -a); } n long double ABS(long double a) { return max(a, -a); } n int p[110000]; n int main() { n   int a; n   scanf( %d , &a); n   for (int i = 0; i < a; i++) scanf( %d , p + i); n   std::sort(p, p + a); n   long long sum = 0; n   set<int> S; n   for (int i = 0; i < a; i++) S.insert(p[i]); n   for (int i = 0; i < a; i++) sum += p[i]; n   if (a == 1 || sum == 0) { n     if (sum % 2 == 0) n       printf( cslnb n ); n     else n       printf( sjfnb n ); n     return 0; n   } n   bool dame = false; n   int cnt = 0; n   for (int i = 0; i < a - 1; i++) { n     if (p[i] == p[i + 1]) { n       cnt++; n       if (p[i] == 0 || S.count(p[i] - 1)) dame = true; n     } n   } n   if (cnt > 1) dame = true; n   if (dame) { n     printf( cslnb n ); n     return 0; n   } n   long long val = 0; n   for (int i = 0; i < a; i++) { n     val += p[i] - i; n   } n   if (val % 2 == 0) n     printf( cslnb n ); n   else n     printf( sjfnb n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(), cout.tie(); n   long long n, k; n   cin >> n >> k; n   long long a[n]; n   map<long long, long long> mp; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mp[a[i]]++; n   } n   sort(a, a + n); n   int i = 0, j = n - 1; n   long long mini = a[0], maxi = a[n - 1]; n   while (k && i < j) { n     while (i + 1 < n && a[i] == a[i + 1]) i++; n     while (j - 1 >= 0 && a[j] == a[j - 1]) j--; n     if (i < j) { n       long long x = mp[a[i]], y = mp[a[j]]; n       if (x < y) { n         long long d = a[i + 1] - a[i]; n         long long mn = min(k / x, d); n         if ((a[i] + mn) == a[i + 1]) { n           mp[a[i + 1]] += mp[a[i]]; n           k -= (mn * mp[a[i]]); n           mini = a[i + 1]; n           i++; n         } else { n           mini = a[i] + mn; n           break; n         } n       } else { n         long long d = a[j] - a[j - 1]; n         long long mn = min(k / y, d); n         if ((a[j] - mn) == a[j - 1]) { n           mp[a[j - 1]] += mp[a[j]]; n           k -= (mn * mp[a[j]]); n           maxi = a[j - 1]; n           j--; n         } else { n           maxi = a[j] - mn; n           break; n         } n       } n     } else { n       mini = a[i], maxi = a[i]; n     } n   } n   cout << maxi - mini << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n string s; n int pows[4] = {0, 0, 0, 0}; n void Desc(int q) { n   if (q <= 1) return; n   switch (q) { n     case 2: n       pows[0]--; n       break; n     case 3: n       pows[1]--; n       break; n     case 4: n       pows[0] -= 2; n       break; n     case 5: n       pows[2]--; n       break; n     case 6: n       pows[0]--; n       pows[1]--; n       break; n     case 7: n       pows[3]--; n       break; n     case 8: n       pows[0] -= 3; n       break; n     case 9: n       pows[1] -= 2; n       break; n   } n   Desc(q - 1); n } n void Proc(int q) { n   if (q <= 1) return; n   switch (q) { n     case 2: n       pows[0]++; n       break; n     case 3: n       pows[1]++; n       break; n     case 4: n       pows[0] += 2; n       break; n     case 5: n       pows[2]++; n       break; n     case 6: n       pows[0]++; n       pows[1]++; n       break; n     case 7: n       pows[3]++; n       break; n     case 8: n       pows[0] += 3; n       break; n     case 9: n       pows[1] += 2; n       break; n   } n   Proc(q - 1); n } n int conv(int q) { n   switch (q) { n     case 0: n       return 2; n     case 1: n       return 3; n     case 2: n       return 5; n     case 3: n       return 7; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cout.precision(20); n   cin >> N; n   cin >> s; n   for (int i = 0; i < N; ++i) Proc(s[i] -  0 ); n   for (int i = 3; i >= 0; --i) { n     while (pows[i] != 0) { n       cout << conv(i); n       Desc(conv(i)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> status(26); n vector<vector<int> > adjList(200000); n vector<string> names(100); n vector<int> ts; n vector<int> processing(26); n void dfs(int u, bool &valid) { n   status[u] = 1; n   processing[u] = true; n   for (int j = 0; j < adjList[u].size(); j++) { n     int v = adjList[u][j]; n     if (processing[v] == true) { n       valid = false; n       return; n     } n     if (status[v] == 0) { n       dfs(v, valid); n     } n   } n   processing[u] = false; n   ts.push_back(u); n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> names[i]; n   } n   for (int i = 0; i < 26; i++) { n     status[i] = 0; n     processing[i] = false; n   } n   for (int i = 0; i < n - 1; i++) { n     int c1 = -1; n     int c2 = -1; n     for (int j = 0; j < names[i].size() && j < names[i + 1].size(); j++) { n       if (names[i][j] != names[i + 1][j]) { n         c1 = names[i][j]; n         c2 = names[i + 1][j]; n         adjList[(c1 -  a )].push_back(c2 -  a ); n         break; n       } n     } n     if (c1 == -1 && c2 == -1 && names[i].size() > names[i + 1].size()) { n       cout <<  Impossible  << endl; n       return 0; n     } n   } n   for (int i = 0; i < 26; i++) { n     if (status[i] == 0) { n       bool valid = true; n       dfs(i, valid); n       if (!valid) { n         cout <<  Impossible  << endl; n         return 0; n       } n     } n   } n   for (int i = ts.size() - 1; i >= 0; i--) { n     cout << (char)( a  + ts[i]); n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 123456; n char buffer[N]; n string solve(string s, int k) { n   const int n = s.size(); n   sort(s.begin(), s.end()); n   string res; n   int ptr = 0; n   while (ptr < n) { n     int p = ptr; n     char mini =  ~ ; n     char maxi =   0 ; n     assert(mini >  z  && maxi <  a ); n     for (int i = 0; i < k && ptr < n; i++) { n       mini = min(mini, s[ptr]); n       maxi = max(maxi, s[ptr]); n       ptr++; n     } n     if (!res.empty()) { n       if (mini == s.back()) { n         res += mini; n       } else { n         res += s.substr(p); n         break; n       } n     } else { n       if (mini == maxi) { n         res += mini; n       } else { n         res += maxi; n         break; n       } n     } n   } n   return res; n } n string brute_force(string s, int k) { n   assert(k == 2); n   int n = s.size(); n   string res =  ~~~ ; n   sort(s.begin(), s.end()); n   for (int i = 0; i < (1 << n); i++) { n     string left, right; n     for (int j = 0; j < n; j++) { n       if (i & (1 << j)) { n         left += s[j]; n       } else { n         right += s[j]; n       } n     } n     if (left.empty() || right.empty()) { n       continue; n     } n     res = min(res, max(left, right)); n   } n   return res; n } n void test() { n   assert(solve( ccbbb , 2) ==  bbcc ); n   assert(solve( bbabc , 2) ==  b ); n   assert(solve( bababc , 2) ==  abbbc ); n   assert(solve( babab , 2) ==  abb ); n   assert(solve( baba , 2) ==  ab ); n   assert(solve( baacb , 2) ==  abbc ); n   assert(solve( baacb , 3) ==  b ); n   assert(solve( aaaaa , 3) ==  aa ); n   assert(solve( aaxxzz , 4) ==  x ); n   assert(solve( phoenix , 1) ==  ehinopx ); n   assert(solve( phoenix , 2) ==  h ); n   for (int i = 0; i < 100; i++) { n     int n = 10; n     string s; n     for (int j = 0; j < n; j++) { n       s += rand() % 5 +  a ; n     } n     assert(brute_force(s, 2) == solve(s, 2)); n   } n } n int main() { n   int T; n   cin >> T; n   int n, k; n   while (T--) { n     scanf( %d%d , &n, &k); n     scanf( %s , buffer); n     puts(solve(buffer, k).c_str()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, fl; n   cin >> t; n   while (t--) { n     fl = 1; n     int a, b, c, d, x, y, x1, y1, x2, y2; n     cin >> a >> b >> c >> d >> x >> y >> x1 >> y1 >> x2 >> y2; n     int p = x - a + b, q = y - c + d; n     if ((p >= x1 && p <= x2) && (x1 < x2 || a + b == 0)) n       ; n     else n       fl = 0; n     if ((q >= y1 && q <= y2) && (y1 < y2 || c + d == 0)) n       ; n     else n       fl = 0; n     if (fl) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int N; n   cin >> N; n   string s; n   cin >> s; n   int current = N; n   int count = 0; n   while (true) { n     if (current >= s.size()) break; n     set<char> vs; n     vs.insert(s[current - 1]); n     vs.insert(s[current - 2]); n     vs.insert(s[current - 3]); n     if (vs.size() == 1) count++; n     current += N; n   } n   cout << count; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long int n, h, A, B, k, S, T, ta, tb, fa, fb; n   cin >> n >> h >> A >> B >> k; n   for (long int i = 0; i < k; i++) { n     S = 0; n     cin >> ta >> fa >> tb >> fb; n     if (ta == tb) { n       S = abs(fa - fb); n     } else { n       if (fa < A) { n         S += abs(A - fa); n         T = A; n       } else if (fa > B) { n         S += abs(fa - B); n         T = B; n       } else { n         S += 0; n         T = fa; n       } n       S += abs(ta - tb); n       S += abs(fb - T); n     } n     cout << S << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long a, long long b, long long p) { n   if (a == 0) return 0; n   long long res = 1; n   a %= p; n   while (b > 0) { n     if (b & 1) res = (res * a) % p; n     b >>= 1; n     a = (a * a) % p; n   } n   return res; n } n int32_t main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long ar[n]; n     for (long long i = 0; i < n; i++) cin >> ar[i]; n     sort(ar, ar + n); n     for (long long i = n - 1; i >= 0; i--) cout << ar[i] <<    ; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n const int INF = 1.01e9; n const long long INF18 = 1.01e18; n int main() { n   int n; n   long long C; n   scanf( %d%lld , &n, &C); n   vector<long long> a(n); n   vector<long long> b(n - 1); n   for (int i = 0; i < (n); i++) { n     scanf( %lld , &a[i]); n     if (i + 1 != n) { n       scanf( %lld , &b[i]); n     } n   } n   b.push_back(0); n   vector<long long> c(n); n   set<pair<long long, int>> q; n   long long shift = 0; n   vector<long long> hist(n); n   int cur = 0; n   vector<long long> prefB; n   prefB.push_back(0); n   for (int i = 0; i < n; i++) { n     prefB.push_back(prefB.back() + a[i] + b[i]); n   } n   vector<long long> prefC; n   prefC.push_back(0); n   for (int i = 0; i < n; i++) { n     if (i > 0) { n       long long diff = b[i - 1] - c[i - 1]; n       shift += diff; n       q.insert({-shift, i}); n       hist[i] = -shift; n     } n     if (i == 0) { n       q.insert({0, i}); n       hist[i] = 0; n     } n     for (; cur < i && (prefB[i] - prefB[cur]) + a[i] > C;) { n       assert(q.erase({hist[cur], cur})); n       cur++; n     } n     long long val = 0; n     if (!q.empty()) { n       val = max(q.rbegin()->first + shift, val); n     } n     val = max( n         min(prefB[i] + a[i], C) - ((prefC[i] - prefC[max(0, cur - 1)]) + a[i]), n         val); n     c[i] = val; n     prefC.push_back(prefC.back() + a[i] + c[i]); n   } n   long long answer = 0; n   for (auto x : c) { n     answer += x; n   } n   for (auto x : a) { n     answer += min(x, C); n   } n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x[1000 + 1], y[1000 + 1]; n void solve() { n   int n; n   cin >> n; n   for (int i = 0; i < n + 1; i++) cin >> x[i] >> y[i]; n   int res = 0; n   for (int i = 1; i < n; i++) { n     double X = x[i], Y = y[i]; n     int cnt = 0; n     if (x[i - 1] == x[i]) { n       if (y[i] > y[i - 1]) n         Y += 0.2; n       else n         Y -= 0.2; n       for (int j = 0; j < n; j++) n         if (x[j] == x[j + 1] && x[j] < x[i] && min(y[j], y[j + 1]) < Y && n             Y < max(y[j], y[j + 1])) n           cnt++; n       if (cnt % 2 == 1) res++; n     } else { n       if (x[i] > x[i - 1]) n         X += 0.2; n       else n         X -= 0.2; n       for (int j = 0; j < n; j++) n         if (y[j] == y[j + 1] && y[j] < y[i] && min(x[j], x[j + 1]) < X && n             X < max(x[j], x[j + 1])) n           cnt++; n       if (cnt % 2 == 1) res++; n     } n   } n   cout << res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   int fac[4]; n   fac[0] = 1; n   fac[1] = 1; n   fac[2] = 1; n   for (int i = 3; i < 10; i++) { n     int y1213 = 100; n     int n1113; n     vector<int> ans112; n     int tempor = 100; n   } n   while (t--) { n     int n; n     cin >> n; n     long long res = 1; n     for (int i = 0; i < 10; i++) { n       int j661; n       int k771; n       string k772; n     } n     int y1213 = 100; n     int n1113; n     long long modu = 1e9 + 7; n     for (int i = 2 * n; i >= 3; i--) { n       res = (res * i) % modu; n     } n     vector<int> ans1222; n     int tem33por = 100; n     for (int i = 0; i < 10; i++) { n       int j12; n       int k15; n       string k277; n     } n     res = res % modu; n     cout << res <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> v; n   for (int i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       v.push_back(i); n       if (i != n / i) { n         v.push_back(n / i); n       } n     } n   } n   sort(v.begin(), v.end()); n   int ans = 0; n   for (int i = v.size() - 1; i > -1; i--) { n     int p = v[i]; n     int q = 0; n     while (p % 2 == 0) { n       q++; n       p /= 2; n     } n     int r = pow(2, q + 1) + 0.5; n     r--; n     if (p == r) { n       ans = v[i]; n       break; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 20; n long long int c[15][maxn]; n int lowbit(int x) { return x & (-x); } n void upDate(int pos, long long int val, int which) { n   while (pos <= maxn - 20) { n     c[which][pos] += val; n     pos += lowbit(pos); n   } n } n long long int ask(int pos, int which) { n   long long int ans = 0; n   while (pos) { n     ans += c[which][pos]; n     pos -= lowbit(pos); n   } n   return ans; n } n long long int dp[maxn][15]; n void work() { n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) { n     int x; n     cin >> x; n     dp[i][1] = 1; n     for (int j = 2; j <= k + 1; ++j) { n       dp[i][j] += ask(x - 1, j - 1); n     } n     for (int j = 1; j <= k + 1; ++j) { n       upDate(x, dp[i][j], j); n     } n   } n   long long int ans = 0; n   for (int i = 1; i <= n; ++i) ans += dp[i][k + 1]; n   cout << ans << endl; n } n int main() { n   ios::sync_with_stdio(false); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, m, q; n int c[N]; n vector<pair<long long, int> > girl[N]; n int head[N], to[N * 2], pre[N * 2], tot; n void addedge(int u, int v) { n   to[++tot] = v; n   pre[tot] = head[u]; n   head[u] = tot; n } n int L[N], R[N], dfn, id[N], dep[N], top[N], son[N], fa[N]; n int dfs1(int u, int depth) { n   dep[u] = depth; n   int mx = 0, sum = 1, tmp; n   for (int v, i = head[u]; i; i = pre[i]) n     if ((v = to[i]) != fa[u]) { n       fa[v] = u; n       tmp = dfs1(v, depth + 1); n       if (tmp > mx) mx = tmp, son[u] = v; n       sum += tmp; n     } n   return sum; n } n void dfs2(int u, int anc) { n   top[u] = anc; n   L[u] = ++dfn; n   id[dfn] = u; n   if (son[u]) dfs2(son[u], anc); n   for (int i = head[u]; i; i = pre[i]) n     if (to[i] != fa[u] && to[i] != son[u]) dfs2(to[i], to[i]); n   R[u] = dfn; n } n pair<long long, int> min(pair<long long, int> a, pair<long long, int> b) { n   if (a.first < b.first) return a; n   if (a.first > b.first) return b; n   if (c[a.second] < c[b.second]) return a; n   return b; n } n pair<long long, int> mi[N * 4]; n long long add[N * 4]; n void up(int i) { n   mi[i] = min(mi[i << 1], mi[i << 1 | 1]); n   mi[i].first += add[i]; n } n void build(int i, int l, int r) { n   if (l == r) { n     if (girl[id[l]].begin() == girl[id[l]].end()) n       mi[i] = make_pair((1e17), (0)); n     else n       mi[i] = *(girl[id[l]].end() - 1); n     return; n   } n   int mid = (l + r) >> 1; n   build(i << 1, l, mid); n   build(i << 1 | 1, mid + 1, r); n   up(i); n } n pair<long long, int> ans; n pair<long long, int> query(int i, int l, int r, int opl, int opr) { n   if (l == opl && r == opr) return mi[i]; n   pair<long long, int> ans; n   int mid = (l + r) >> 1; n   if (opr <= mid) n     ans = query(i << 1, l, mid, opl, opr); n   else if (mid < opl) n     ans = query(i << 1 | 1, mid + 1, r, opl, opr); n   else n     ans = min(query(i << 1, l, mid, opl, mid), n               query(i << 1 | 1, mid + 1, r, mid + 1, opr)); n   ans.first += add[i]; n   return ans; n } n void work(int i, int l, int r, int pos) { n   if (l == r) { n     girl[id[l]].pop_back(); n     if (girl[id[l]].begin() == girl[id[l]].end()) n       mi[i] = make_pair((1e17), (0)); n     else n       mi[i] = *(girl[id[l]].end() - 1); n     mi[i].first += add[i]; n     return; n   } n   int mid = (l + r) >> 1; n   if (pos <= mid) n     work(i << 1, l, mid, pos); n   else n     work(i << 1 | 1, mid + 1, r, pos); n   up(i); n } n int work(int u, int v) { n   ans = make_pair((1e17), (0)); n   while (top[u] != top[v]) { n     if (dep[top[u]] < dep[top[v]]) swap(u, v); n     ans = min(query(1, 1, n, L[top[u]], L[u]), ans); n     u = fa[top[u]]; n   } n   if (dep[u] > dep[v]) swap(u, v); n   ans = min(ans, query(1, 1, n, L[u], L[v])); n   if (ans.first == 1e17) return -1; n   work(1, 1, n, L[c[ans.second]]); n   return ans.second; n } n void updata(int i, int l, int r, int opl, int opr, long long k) { n   if (l == opl && r == opr) { n     add[i] += k; n     mi[i].first += k; n     return; n   } n   int mid = (l + r) >> 1; n   if (opr <= mid) n     updata(i << 1, l, mid, opl, opr, k); n   else if (mid < opl) n     updata(i << 1 | 1, mid + 1, r, opl, opr, k); n   else n     updata(i << 1, l, mid, opl, mid, k), n         updata(i << 1 | 1, mid + 1, r, mid + 1, opr, k); n   up(i); n } n int num, tmp[N]; n int main() { n   int ty, u, v, k; n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &u, &v); n     addedge(u, v); n     addedge(v, u); n   } n   for (int i = 1; i <= m; i++) { n     scanf( %d , &c[i]); n     girl[c[i]].push_back(make_pair((i), (i))); n   } n   dfs1(1, 1); n   dfs2(1, 1); n   for (int i = 1; i <= n; i++) n     sort(girl[i].begin(), girl[i].end(), greater<pair<long long, int> >()); n   build(1, 1, n); n   for (int i = 1; i <= q; i++) { n     scanf( %d , &ty); n     if (ty == 1) { n       scanf( %d%d%d , &u, &v, &k); n       num = 0; n       while (k--) { n         tmp[++num] = work(u, v); n         if (tmp[num] == -1) { n           num--; n           break; n         } n       } n       printf( %d  , num); n       for (int i = 1; i <= num; i++) printf( %d  , tmp[i]); n       puts(  ); n     } else { n       scanf( %d%d , &u, &k); n       updata(1, 1, n, L[u], R[u], k); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, v, a, b, x; n   cin >> n >> v; n   int mp[3002] = {0}; n   for (int i = 0; i < n; i++) { n     cin >> a >> b; n     mp[a] += b; n   } n   int prev = 0, ans = 0; n   for (int i = 1; i <= 3001; i++) { n     int now = mp[i]; n     if (now + prev <= v) { n       ans += (now + prev); n       prev = 0; n     } else { n       ans += v; n       prev = now - max(v - prev, 0); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   node *c[3]; n   bool flag; n }; n node *root; n void insert(string a) { n   node *cur = root; n   for (int i = 0; i < a.size(); i++) { n     if (!(cur->c[a[i] -  a ])) cur->c[a[i] -  a ] = new node(); n     cur = cur->c[a[i] -  a ]; n   } n   cur->flag = 1; n } n string s; n bool solve(node *root, int pos, int diff) { n   if (!root) return 0; n   if (diff > 1) return 0; n   if (pos == s.size()) return diff == 1 && root->flag; n   int ret = 0; n   for (int i = 0; i < 3; i++) n     ret = ret || solve(root->c[i], pos + 1, (s[pos] -  a  != i) + diff); n   return ret; n } n int main() { n   int n, m; n   cin >> n >> m; n   root = new node(); n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     insert(s); n   } n   for (int i = 0; i < m; i++) { n     cin >> s; n     if (solve(root, 0, 0)) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const int INF = int(1e9); n const int MAX = 200; n int num[MAX], mixt[MAX]; n int n; n bool substring(string &a, string &b) { n   for (int i = 0; i < n; i++) { n     bool found = true; n     for (int j = 0; j < 3; j++) { n       if (a[i + j] != b[j]) { n         found = false; n       } n     } n     if (found) { n       return true; n     } n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   string s; n   cin >> s; n   n = s.size(); n   string t =  ABC ; n   do { n     if (substring(s, t)) { n       cout <<  Yes n ; n       return 0; n     } n   } while (next_permutation(t.begin(), t.end())); n   cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod(long long a, long long b) { n   if (a % b < 0) { n     return a % b + b; n   } n   return a % b; n } n long long mod_exp(long long a, long long b, long long c) { n   long long res = 1; n   a = a % c; n   while (b > 0) { n     if (b % 2 == 1) { n       res = (res * a) % c; n     } n     b /= 2; n     a = (a * a) % c; n   } n   return res; n } n long long mymod(long long a, long long b) { return ((a % b) + b) % b; } n bool prime[200001]; n void SieveOfEratosthenes() { n   memset(prime, true, sizeof(prime)); n   prime[1] = false; n   for (int p = 2; p * p <= 200000; p++) { n     if (prime[p] == true) { n       for (int i = p * p; i <= 200000; i += p) prime[i] = false; n     } n   } n   return; n } n long long powe[200005]; n void power() { n   powe[0] = 1; n   for (long long i = 1; i < 200005; i++) { n     powe[i] = mymod(2 * powe[i - 1], 1000000007); n   } n } n long long gcdExtended(long long, long long, long long *, long long *); n long long modInverse(long long a, long long m) { n   long long x, y; n   long long g = gcdExtended(a, m, &x, &y); n   long long res = (x % m + m) % m; n   return res; n } n long long gcdExtended(long long a, long long b, long long *x, long long *y) { n   if (a == 0) { n     *x = 0, *y = 1; n     return b; n   } n   long long x1, y1; n   long long gcd = gcdExtended(b % a, a, &x1, &y1); n   *x = y1 - (b / a) * x1; n   *y = x1; n   return gcd; n } n long long n; n struct node { n   vector<long long> adj; n   long long colour; n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   string second; n   cin >> second; n   long long count[36001] = {0}; n   for (long long i = 0; i < second.size(); i++) { n     long long sum = 0; n     for (long long j = i; j < second.size(); j++) { n       sum += (second[j] - 48); n       if (sum <= 36000) { n         count[sum]++; n       } else n         break; n     } n   } n   vector<long long> factors; n   for (long long i = 1; i < sqrt(n); i++) { n     if (n % i == 0) { n       factors.push_back(i); n       factors.push_back(n / i); n     } n   } n   if ((long long)(sqrt(n)) * (long long)(sqrt(n)) == n) n     factors.push_back((long long)(sqrt(n))); n   long long ans = 0; n   if (n == 0) { n     long long p = second.size(); n     long long ways0 = count[0]; n     long long waysn0 = (p * (p + 1)) / 2 - count[0]; n     ans = 2 * ways0 * waysn0 + ways0 * ways0; n     cout << ans << endl; n     exit(0); n   } n   for (auto x : factors) { n     if (x <= 36000 && (n / x) <= 36000) ans += (count[x] * count[n / x]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double h, luas, top; n int n; n double f(double x) { return ((x / h + top / h) * (x - top)) / 2; } n double binser(double l, double r) { n   double ret = 0; n   for (int i = 1; i <= 100; i++) { n     double mid = (l + r) / 2; n     if (f(mid) >= luas) { n       ret = mid; n       r = mid; n     } else n       l = mid; n   } n   return ret; n } n int main() { n   cin >> n >> h; n   luas = h / (2 * n); n   for (int i = 1; i <= n - 1; i++) { n     top = binser(0, h); n     cout << fixed << setprecision(6) << top; n     if (i < n - 1) cout <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long n, H; n unsigned long long bound[60]; n unsigned long long cnt(unsigned long long p, unsigned long long h, n                        unsigned long long n) { n   if (h == 1) { n     if (p == 0 && n == 1) return 1; n     if (p == 1 && n == 2) return 1; n     return 2; n   } n   unsigned long long res = 1; n   if (n > bound[h - 1] && p == 0 || n <= bound[h - 1] && p == 1) n     res += 2 * bound[h - 1] - 1; n   if (n > bound[h - 1]) n     res += cnt(0, h - 1, n - bound[h - 1]); n   else n     res += cnt(1, h - 1, n); n   return res; n } n int main() { n   cin >> H >> n; n   bound[0] = 1; n   for (unsigned long long i = 0; i < H; ++i) bound[i + 1] = 2 * bound[i]; n   cout << cnt(0, H, 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 INF64 = 1e18; n const long long INF32 = 1e9; n long long dist(pair<long long, long long> &a, pair<long long, long long> &b) { n   return abs(a.first - b.first) + abs(a.second - b.second); n } n long long dist(pair<long long, long long> &a) { n   return abs(a.first) + abs(a.second); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   { n     long long n; n     string s; n     cin >> n >> s; n     long long res = 1; n     bool first[2] = {true, true}; n     char hv[2]; n     string HV[2] = { LR ,  DU }; n     for (long long i = (0); i < (s.length()); i++) { n       for (long long j = (0); j < (2); j++) { n         if (HV[j].find(s[i]) != string::npos) { n           if (first[j]) { n             first[j] = false; n             hv[j] = s[i]; n           } else if (hv[j] != s[i]) { n             first[1 - j] = true; n             res++; n             hv[j] = s[i]; n           } n           break; n         } n       } n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   if (a < b) swap(a, b); n   if (b == 0) n     return a; n   else n     gcd(b, a % b); n } n int main() { n   long long int ncr[62][62]; n   for (long long int i = 0; i < 62; i++) { n     ncr[i][0] = 1; n     ncr[i][i] = 1; n   } n   for (long long int i = 1; i < 62; i++) { n     for (long long int j = 1; j <= i; j++) { n       ncr[i][j] = ncr[i - 1][j - 1] + ncr[i - 1][j]; n     } n   } n   long long int n, m, t, ans = 0; n   cin >> n >> m >> t; n   for (long long int i = 4; i <= t - 1; i++) { n     ans += (ncr[n][i] * ncr[m][t - i]); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n, q; n   cin >> n >> q; n   long i, j; n   int** a = new int*[2]; n   for (i = 0; i < 2; ++i) { n     a[i] = new int[n + 2]; n     for (j = 0; j < n + 2; j++) { n       a[i][j] = 0; n     } n   } n   long res = 0; n   long x, y; n   while (q--) { n     cin >> x >> y; n     if (a[x - 1][y] == 0) { n       a[x - 1][y] = 1; n       for (i = -1; i <= 1; i++) { n         if (a[2 - x][y + i] == 1) res++; n       } n     } else { n       a[x - 1][y] = 0; n       for (i = -1; i <= 1; i++) { n         if (a[2 - x][y + i] == 1) { n           res--; n         } n       } n     } n     if (res == 0) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n   return 0; n } n