#include <bits/stdc++.h> n using namespace std; n const long double PI = 3.14159265358979323846264338; n const long long INF = 1000000000000000000; n const long long INF1 = 1000000000; n const long long MOD = 1000000007; n long long mpow(long long a, long long b) { n   a %= MOD; n   if (!b) return 1; n   long long temp = mpow(a, b / 2); n   temp = (temp * temp) % MOD; n   if (b % 2) return (a * temp) % MOD; n   return temp; n } n long long _pow(long long a, long long b) { n   if (!b) return 1; n   long long temp = _pow(a, b / 2); n   temp = (temp * temp); n   if (b % 2) return (a * temp); n   return temp; n } n long long mod_in(long long n) { return mpow(n, MOD - 2); } n long long cl(long long a, long long b) { n   if (a % b) return 1 + a / b; n   return a / b; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T; n   T = 1; n   while (T--) { n     long long n, k; n     cin >> n >> k; n     set<long long> s1, s2; n     cout <<  ?  ; n     for (int i = 1; i <= k; i++) cout << i <<    ; n     cout <<   n ; n     cout.flush(); n     long long pos, apos; n     cin >> pos >> apos; n     s1.insert(k + 1); n     for (int i = 1; i <= k; i++) { n       if (pos == i) continue; n       cout <<  ?  ; n       for (int j = 1; j <= k + 1; j++) { n         if (i == j) continue; n         cout << j <<    ; n       } n       cout <<   n ; n       cout.flush(); n       long long x, y; n       cin >> x >> y; n       if (y == apos) n         s1.insert(i); n       else n         s2.insert(i); n     } n     cout <<  ?  ; n     for (long long x : s1) cout << x <<    ; n     for (long long y : s2) cout << y <<    ; n     cout <<   n ; n     cout.flush(); n     long long u, v; n     cin >> u >> v; n     long long p1 = -1, p2 = -1, c[2] = {}; n     if (s1.count(u)) { n       if (v > apos) { n         p1 = 1; n         p2 = 0; n       } else { n         p1 = 0; n         p2 = 1; n       } n     } else { n       if (v > apos) { n         p1 = 0; n         p2 = 1; n       } else { n         p1 = 1; n         p2 = 0; n       } n     } n     for (int i = 1; i <= k; i++) { n       if (i == pos) continue; n       if (s1.count(i)) n         c[p1]++; n       else n         c[p2]++; n     } n     cout <<  !   << c[0] + 1 <<   n ; n     cout.flush(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5050; n const int inf = 0x3f3f3f3f; n int to[N * 2], nxt[N * 2], pre[N * 2], cnt, n, tot[N], dp[N][N][2]; n void makeedge(int x, int y) { n   to[cnt] = x; n   nxt[cnt] = pre[y]; n   pre[y] = cnt++; n   to[cnt] = y; n   nxt[cnt] = pre[x]; n   pre[x] = cnt++; n } n int dfs(int x, int fa) { n   int son = 0; n   if (tot[x] == 1) { n     son = 1; n     dp[x][0][1] = 0; n     dp[x][1][0] = 0; n   } else { n     dp[x][0][1] = 0; n     dp[x][0][0] = 0; n   } n   for (int p = pre[x]; p != -1; p = nxt[p]) { n     int y = to[p]; n     if (y == fa) continue; n     int tmp = dfs(y, x); n     for (int j = son; j >= 0; j--) { n       for (int k = tmp; k >= 1; k--) { n         dp[x][j + k][0] = n             min(dp[x][j + k][0], n                 min(dp[x][j][0] + dp[y][k][1] + 1, dp[x][j][0] + dp[y][k][0])); n         dp[x][j + k][1] = n             min(dp[x][j + k][1], n                 min(dp[x][j][1] + dp[y][k][0] + 1, dp[x][j][1] + dp[y][k][1])); n       } n       dp[x][j][0] = min(inf, dp[x][j][0] + min(dp[y][0][1] + 1, dp[y][0][0])); n       dp[x][j][1] = min(inf, dp[x][j][1] + min(dp[y][0][0] + 1, dp[y][0][1])); n     } n     son += tmp; n   } n   return son; n } n int main() { n   scanf( %d , &n); n   memset(pre, -1, sizeof(pre)); n   memset(dp, inf, sizeof(dp)); n   for (int i = 1; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     tot[x]++; n     tot[y]++; n     makeedge(x, y); n   } n   if (n == 2) { n     printf( 1 n ); n     return 0; n   } n   int son = 0, S; n   for (int i = 1; i <= n; i++) { n     if (tot[i] != 1) n       S = i; n     else n       son++; n   } n   dfs(S, 0); n   printf( %d n , min(dp[S][son / 2][0], dp[S][son / 2][1])); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, s, q, nr = 0; n   scanf( %d%d%d , &t, &s, &q); n   while (s < t) { n     nr++; n     s += s * (q - 1); n   } n   printf( %d , nr); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int arr[105]; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int n, x, y, t, l, m, k = 0, r = 0, a, b, c, d, p = 0, q = 0, s, i, n                                   j, ans = 0, ans1 = 0; n   string s1; n   char ch; n   cin >> t; n   while (t--) { n     cin >> n >> a >> b >> c >> d; n     q = a + b; n     p = a - b; n     s = c + d; n     r = c - d; n     x = p * n; n     y = q * n; n     if (x > s || y < r) n       cout <<  No  n            <<   n ; n     else n       cout <<  Yes  n            <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int sci[200005], bn[200005]; n int main() { n   int n; n   while (scanf( %d , &n) != EOF) { n     int m, ple = 0, sat = 0, fi = 1, cn; n     map<int, int> mp; n     for (int i = 0; i < n; i++) { n       scanf( %d , &sci[i]); n       mp[sci[i]]++; n     } n     scanf( %d , &m); n     for (int i = 1; i <= m; i++) { n       scanf( %d , &bn[i]); n     } n     for (int i = 1; i <= m; i++) { n       scanf( %d , &cn); n       if (mp[bn[i]] > ple) { n         ple = mp[bn[i]]; n         sat = mp[cn]; n         fi = i; n       } else if (mp[bn[i]] == ple && mp[cn] > sat) { n         sat = mp[cn]; n         fi = i; n       } n     } n     printf( %d n , fi); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int M = (int)1e5 + 5; n int n; n int a[M]; n int q, ans[M]; n vector<pair<int, int> > ps[M]; n int pas[M], las[M], llas[M], mfr[M]; n struct SEGMENT1 { n   int val[M * 4]; n   void update(int L, int R, int p, int l, int r, int v) { n     if (L == l && R == r) { n       val[p] += v; n       return; n     } n     int mid = (L + R) >> 1; n     if (mid >= r) n       update(L, mid, p << 1, l, r, v); n     else if (mid < l) n       update(mid + 1, R, p << 1 | 1, l, r, v); n     else n       update(L, mid, p << 1, l, mid, v), n           update(mid + 1, R, p << 1 | 1, mid + 1, r, v); n   } n   int query(int L, int R, int p, int x) { n     if (L == R) return val[p]; n     int mid = (L + R) >> 1; n     if (mid >= x) n       return val[p] + query(L, mid, p << 1, x); n     else n       return val[p] + query(mid + 1, R, p << 1 | 1, x); n   } n } T1; n struct SEGMENT2 { n   int val[M * 4]; n   void update(int L, int R, int p, int x, int v) { n     val[p] += v; n     if (L == R) return; n     int mid = (L + R) >> 1; n     if (mid >= x) n       update(L, mid, p << 1, x, v); n     else n       update(mid + 1, R, p << 1 | 1, x, v); n   } n   int query(int L, int R, int p, int l, int r) { n     if (L == l && R == r) return val[p]; n     int mid = (L + R) >> 1; n     if (mid >= r) n       return query(L, mid, p << 1, l, r); n     else if (mid < l) n       return query(mid + 1, R, p << 1 | 1, l, r); n     else n       return query(L, mid, p << 1, l, mid) + n              query(mid + 1, R, p << 1 | 1, mid + 1, r); n   } n } T2; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   scanf( %d , &q); n   for (int i = 1, l, r; i <= q; i++) n     scanf( %d %d , &l, &r), ps[r].push_back(pair<int, int>(l, i)); n   for (int i = 1, x, len; i <= n; i++) { n     x = a[i]; n     len = i - las[x]; n     if (len != pas[x] && pas[x] != las[x]) { n       if (mfr[x] < llas[x]) T1.update(1, n, 1, mfr[x] + 1, llas[x], 1); n       mfr[x] = llas[x]; n     } n     pas[x] = len; n     if (las[x] != 0) T2.update(1, n, 1, las[x], -1); n     T2.update(1, n, 1, i, 1); n     llas[x] = las[x]; n     las[x] = i; n     for (int j = 0, up = ps[i].size(), c1, c2, l, id; j < up; j++) { n       l = ps[i][j].first; n       id = ps[i][j].second; n       c1 = T2.query(1, n, 1, l, i); n       c2 = T1.query(1, n, 1, l); n       if (c1 != c2) n         ans[id] = c1; n       else n         ans[id] = c1 + 1; n     } n   } n   for (int i = 1; i <= q; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e4 + 10, K = 256, B = 16; n int a[N], parent[N], depth[N], up[N], ans[N][K]; n vector<int> g[N]; n bitset<(1 << (B + 1)) + 10> trie; n void make_tries(int i, int par); n void trie_insert(int val); n void trie_clear(int val); n int trie_max(int val); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, q; n   cin >> n >> q; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   for (int i = 1; i < n; ++i) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   make_tries(1, 0); n   while (q--) { n     int u, v; n     cin >> u >> v; n     int dist = 0, res = 0; n     while (depth[v] - depth[u] >= K - 1) { n       res = max(res, ans[v][dist]); n       v = up[v]; n       ++dist; n     } n     dist <<= 8; n     while (depth[v] >= depth[u]) { n       res = max(res, dist xor a[v]); n       v = parent[v]; n       ++dist; n     } n     cout << res << endl; n   } n } n void make_tries(int i, int par) { n   parent[i] = par; n   depth[i] = 1 + depth[par]; n   if (depth[i] >= K) { n     int u = i; n     int dist = 0; n     while (depth[i] - depth[u] <= K - 1) { n       trie_insert(dist xor a[u]); n       u = parent[u]; n       ++dist; n     } n     for (int d = 0; d < K; ++d) { n       ans[i][d] = trie_max(d << 8); n     } n     up[i] = u; n     dist = 0; n     u = i; n     while (depth[i] - depth[u] <= K - 1) { n       trie_clear(dist xor a[u]); n       u = parent[u]; n       ++dist; n     } n   } n   for (auto &j : g[i]) { n     if (j != par) { n       make_tries(j, i); n     } n   } n } n void trie_insert(int val) { n   int pos = 1; n   for (int b = B - 1; b >= 0; --b) { n     pos = (pos << 1) | ((val & (1 << b)) >> b); n     trie[pos] = 1; n   } n } n void trie_clear(int val) { n   int pos = 1; n   for (int b = B - 1; b >= 0; --b) { n     pos = (pos << 1) | ((val & (1 << b)) >> b); n     trie[pos] = 0; n   } n } n int trie_max(int val) { n   int pos = 1, res = 0; n   for (int b = B - 1; b >= 0; --b) { n     pos = (pos << 1) | (((val & (1 << b)) >> b) ^ 1); n     if (trie[pos]) { n       res |= (1 << b); n     } else { n       pos ^= 1; n     } n   } n   return res; n } n


#include <bits/stdc++.h> n using namespace std; n long long int arr[100001], done[100001], cnt0[100001], cnte[100001], n     cnto[100001]; n long long int dp[105][105][2]; n int main() { n   ios::sync_with_stdio(false); n   ; n   cin.tie(0); n   ; n   long long int i, j, n, k, l, x, y, evenLeft = 0, oddLeft = 0, z; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> arr[i]; n     if (arr[i] != 0) done[arr[i]] = 1; n   } n   oddLeft = 0; n   evenLeft = 0; n   for (i = 1; i <= n; i++) { n     if (done[i]) continue; n     if (i & 1) n       oddLeft++; n     else n       evenLeft++; n   } n   for (i = 1; i <= n; i++) cnt0[i] = cnt0[i - 1] + (arr[i] == 0); n   for (i = 1; i <= n; i++) { n     cnte[i] = cnte[i - 1]; n     if (arr[i] != 0 && arr[i] % 2 == 0) cnte[i]++; n   } n   for (i = 1; i <= n; i++) { n     cnto[i] = cnto[i - 1]; n     if (arr[i] != 0 && arr[i] & 1) cnto[i]++; n   } n   for (i = 0; i <= n; i++) { n     for (j = 0; j <= n; j++) { n       for (k = 0; k <= 1; k++) dp[i][j][k] = 1000000000000; n     } n   } n   if (arr[1] != 0) { n     if (arr[1] & 1) n       dp[1][evenLeft][1] = 0; n     else n       dp[1][evenLeft][0] = 0; n   } else { n     if (evenLeft > 0) dp[1][evenLeft - 1][0] = 0; n     if (oddLeft > 0) dp[1][evenLeft][1] = 0; n   } n   for (i = 1; i < n; i++) { n     for (j = 0; j <= evenLeft; j++) { n       for (k = 0; k <= 1; k++) { n         if (dp[i][j][k] == 1000000000000) continue; n         x = cnt0[i]; n         y = evenLeft - j; n         z = oddLeft - (x - y); n         if (arr[i + 1] != 0) { n           if (arr[i + 1] % 2 == k) n             dp[i + 1][j][arr[i + 1] % 2] = n                 min(dp[i + 1][j][arr[i + 1] % 2], dp[i][j][k]); n           else n             dp[i + 1][j][arr[i + 1] % 2] = n                 min(dp[i + 1][j][arr[i + 1] % 2], dp[i][j][k] + 1); n         } else { n           if (j > 0) { n             if (k == 0) n               dp[i + 1][j - 1][0] = min(dp[i + 1][j - 1][0], dp[i][j][k]); n             else n               dp[i + 1][j - 1][0] = min(dp[i + 1][j - 1][0], dp[i][j][k] + 1); n           } n           if (z > 0) { n             if (k == 0) n               dp[i + 1][j][1] = min(dp[i + 1][j][1], dp[i][j][k] + 1); n             else n               dp[i + 1][j][1] = min(dp[i + 1][j][1], dp[i][j][k]); n           } n         } n       } n     } n   } n   cout << min(dp[n][0][0], dp[n][0][1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int t, n; n   cin >> t; n   string s; n   for (int k = 0; k < t; k++) { n     cin >> n; n     cin >> s; n     if (s.size() == 2) { n       if (s[0] < s[1]) { n         cout <<  YES  n              <<   n ; n         cout << 2 <<   n ; n         cout << s[0] <<     << s[1] << endl; n       } else n         cout <<  NO  n              <<   n ; n     } else { n       cout <<  YES  n            <<   n ; n       cout << 2 <<   n ; n       cout << s[0] <<    ; n       for (int i = 1; i < s.size(); i++) cout << s[i]; n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int powmod(long long int a, int b, int n) { n   long long int rm = 1; n   while (b) { n     if (b % 2) { n       rm = (rm * a) % n; n     } n     a = (a * a) % n; n     b /= 2; n   } n   return rm; n } n int main() { n   int n; n   long long int a[110]; n   cin >> n; n   for (int i = (int)1; i <= (int)n; i++) cin >> a[i]; n   long long int cost = 0; n   for (long long int i = 1; i <= n; ++i) { n     cost = cost + (a[i] - 1) * i + 1; n   } n   cout << cost << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s, t; n int arr1[2222], arr2[2222]; n char ans[999999]; n long long int i, j, lo, hi, mid, cnt, sum, nd; n queue<int> q; n int main() { n   cin >> s >> t; n   for (i = 0; i < t.size(); i++) { n     arr2[(int)(t[i] -  a )]++; n   } n   cnt = 0; n   for (i = 0; i < s.size(); i++) { n     if (s[i] ==  ? ) { n       cnt++; n       q.push(i); n     } else n       arr1[(int)(s[i] -  a )]++; n   } n   lo = 0; n   hi = 10000000; n   while (lo <= hi) { n     mid = (lo + hi) / 2; n     sum = 0; n     for (i = 0; i < 26; i++) { n       nd = arr2[i] * mid; n       nd -= arr1[i]; n       if (nd < 0) nd = 0; n       sum = sum + nd; n     } n     if (sum >= cnt) n       hi = mid - 1; n     else n       lo = mid + 1; n   } n   mid++; n   sum = 0; n   for (i = 0; i < 26; i++) { n     nd = arr2[i] * mid; n     nd -= arr1[i]; n     if (nd < 0) nd = 0; n     sum = sum + nd; n   } n   if (sum > cnt) { n     mid--; n     sum = 0; n     for (i = 0; i < 26; i++) { n       nd = arr2[i] * mid; n       nd -= arr1[i]; n       if (nd < 0) nd = 0; n       sum = sum + nd; n     } n     if (sum > cnt) mid--; n   } n   for (i = 0; i < 26; i++) { n     arr2[i] = arr2[i] * mid; n     if (arr2[i]) { n       while (arr2[i]--) { n         if (arr1[i]) n           arr1[i]--; n         else { n           int u = q.front(); n           q.pop(); n           s[u] = (char)i +  a ; n           cnt--; n         } n       } n     } n   } n   while (cnt--) { n     int u = q.front(); n     q.pop(); n     s[u] =  z ; n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b); n long long mpower(long long x, long long y, long long c); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   map<long long, long long> M; n   vector<long long> aa; n   set<long long> st; n   set<long long>::iterator itr; n   long long n, i, j, k; n   cin >> n; n   string s; n   cin >> s; n   long long cnt = 0; n   for (i = n - 1; i >= 0; i--) { n     long long flag = -1; n     if (2 * (i + 1) <= n) { n       for (j = i + 1; j < (2 * (i + 1)); j++) { n         if (s[j] == s[j - 1 - i]) { n           flag = 1; n         } else { n           flag = -1; n           break; n         } n       } n     } n     if (flag == 1) { n       i = -1; n     } n     cnt++; n   } n   cout << cnt; n   return 0; n } n long long gcd(long long a, long long b) { n   if (b == 0) { n     return a; n   } else { n     return gcd(b, a % b); n   } n } n long long mpower(long long x, long long y, long long c) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = (res * x) % c; n     y = y >> 1; n     x = (x * x) % c; n   } n   return res; n } n


#include <bits/stdc++.h> n using namespace std; n double shortestDisSq(double x1, double y1, double x2, double y2, double x, n                      double y) { n   double magSq = pow(abs(x2 - x1), 2) + pow(abs(y2 - y1), 2); n   double proj = ((x - x1) * (x2 - x1) + (y - y1) * (y2 - y1)) / magSq; n   if (proj > 1) proj = 1; n   if (proj < 0) proj = 0; n   return pow(abs((x1 + proj * (x2 - x1)) - x), 2) + n          pow(abs((y1 + proj * (y2 - y1)) - y), 2); n } n int main() { n   int n; n   double x, y; n   cin >> n >> x >> y; n   double p, q; n   double op, oq, ip, iq; n   const double PI = 3.141592653589793238463; n   double dis; n   double R2 = 0; n   double r2 = 100000000000000; n   for (int i = 0; i < n; i++) { n     cin >> p >> q; n     if (i == 0) { n       ip = p; n       iq = q; n     } n     if (i > 0) r2 = min(r2, shortestDisSq(p, q, op, oq, x, y)); n     if (i == n - 1) r2 = min(r2, shortestDisSq(ip, iq, p, q, x, y)); n     op = p; n     oq = q; n     dis = pow(abs(x - p), 2) + pow(abs(y - q), 2); n     R2 = max(R2, dis); n   } n   double res = PI * (R2 - r2); n   printf( %.8lf n , res); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n const long double pi = acos(-1.0); n const long double eps = 1e-6; n const int INF = 1E9; n const int MAXN = 5111; n struct node { n   pair<int, int> key; n   int pr, cnt; n   node *l, *r; n   node(pair<int, int> _key) { n     key = _key; n     pr = rand(); n     cnt = 1; n     l = r = NULL; n   } n }; n int cnt(node *p) { return p ? p->cnt : 0; } n void upd_cnt(node *&p) { n   if (p) p->cnt = 1 + cnt(p->l) + cnt(p->r); n } n void split(node *p, pair<int, int> key, node *&l, node *&r) { n   if (p == NULL) { n     l = r = NULL; n     return; n   } n   if (key < p->key) { n     split(p->l, key, l, p->l); n     r = p; n   } else { n     split(p->r, key, p->r, r); n     l = p; n   } n   upd_cnt(p); n } n void merge(node *&p, node *l, node *r) { n   if (l == NULL) n     p = r; n   else if (r == NULL) n     p = l; n   else if (l->pr < r->pr) { n     p = l; n     merge(l->r, l->r, r); n   } else { n     p = r; n     merge(r->l, l, r->l); n   } n   upd_cnt(p); n } n void insert(pair<int, int> key, int pr, node *&p) { n   if (p == NULL) { n     p = new node(key); n     p->pr = pr; n     return; n   } n   if (pr > p->pr) { n     if (key > p->key) n       insert(key, pr, p->r); n     else n       insert(key, pr, p->l); n   } else { n     node *t = new node(key); n     t->pr = pr; n     split(p, key, t->l, t->r); n     p = t; n   } n   upd_cnt(p); n } n void erase(pair<int, int> key, node *&p) { n   if (p == NULL) return; n   if (p->key == key) n     merge(p, p->l, p->r); n   else { n     if (key < p->key) n       erase(key, p->l); n     else n       erase(key, p->r); n   } n   upd_cnt(p); n } n int n, m; n vector<int> color, used; n vector<vector<int> > g, queries; n vector<pair<int, int> > q; n vector<node *> root; n vector<map<int, int> > lib; n vector<int> ans, link; n void init() { n   color.resize(n); n   used.resize(n, 0); n   root.resize(n, NULL); n   g.resize(n); n   queries.resize(n); n   q.resize(m); n   ans.resize(m); n   link.resize(n); n   lib.resize(n); n } n void intersect(int vid, int wid, node *&w) { n   if (!w) return; n   if (w->l) intersect(vid, wid, w->l); n   if (w->r) intersect(vid, wid, w->r); n   int ckey = w->key.second; n   if (lib[link[vid]].find(ckey) == lib[link[vid]].end()) { n     lib[link[vid]][ckey] = w->key.first; n     insert(w->key, rand(), root[vid]); n   } else { n     erase(make_pair(lib[link[vid]][ckey], ckey), root[vid]); n     w->key.first += lib[link[vid]][ckey]; n     insert(w->key, rand(), root[vid]); n     lib[link[vid]][ckey] = w->key.first; n   } n   lib[link[wid]].erase(ckey); n   delete w; n } n void dfs(int v, int par) { n   used[v] = 1; n   for (int i = 0; i < (int)(g[v].size()); i++) { n     int w = g[v][i]; n     if (!used[w]) { n       dfs(w, v); n     } n   } n   if (v == 0) { n     v = v; n   } n   int maxid = -1; n   for (int i = 0; i < (int)(g[v].size()); i++) { n     int w = g[v][i]; n     if (w != par) { n       if (maxid == -1 || cnt(root[w]) > cnt(root[maxid])) maxid = w; n     } n   } n   if (maxid != -1) { n     link[v] = link[maxid]; n     root[v] = root[maxid]; n     for (int i = 0; i < (int)(g[v].size()); i++) { n       int w = g[v][i]; n       if (w != par && w != maxid) { n         intersect(v, w, root[w]); n       } n     } n     if (lib[link[v]].find(color[v]) == lib[link[v]].end()) { n       lib[link[v]][color[v]] = 1; n       insert(make_pair(1, color[v]), rand(), root[v]); n     } else { n       erase(make_pair(lib[link[v]][color[v]], color[v]), root[v]); n       lib[link[v]][color[v]]++; n       insert(make_pair(lib[link[v]][color[v]], color[v]), rand(), root[v]); n     } n   } else { n     root[v] = new node(make_pair(1, color[v])); n     link[v] = v; n     lib[link[v]][color[v]] = 1; n   } n   node *t1 = NULL, *t2 = NULL; n   for (int i = 0; i < queries[v].size(); i++) { n     int qid = queries[v][i]; n     split(root[v], make_pair(q[qid].second, 0), t1, t2); n     ans[qid] = cnt(t2); n     merge(root[v], t1, t2); n   } n } n int main() { n   cin >> n >> m; n   init(); n   for (int i = 0; i < (int)(n); i++) cin >> color[i]; n   for (int i = 0; i < (int)(n - 1); i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--, b--; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   for (int i = 0; i < (int)(m); i++) { n     cin >> q[i].first >> q[i].second; n     q[i].first--; n     queries[q[i].first].push_back(i); n   } n   dfs(0, 0); n   for (int i = 0; i < (int)(m); i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 1100000; n char s[MAXN]; n int n; n bool check(int o) { n   int p = 0; n   int mx = 0; n   bool uni = true; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  L ) n       p--; n     else { n       if (p < o - 1) p++; n     } n     if (p < mx) { n       mx = p; n       uni = true; n     } else { n       uni = false; n     } n   } n   return uni; n } n int main() { n   scanf( %s , s); n   n = strlen(s); n   if (s[n - 1] ==  R ) { n     for (int i = 0; i < n; i++) { n       s[i] = (s[i] ==  L ) ?  R  :  L ; n     } n   } n   if (check(n + 1)) { n     printf( 1 ); n     return 0; n   } n   int a = 1, b = n; n   while (a <= b) { n     int m = (a + b) / 2; n     if (check(m)) { n       a = m + 1; n     } else { n       b = m - 1; n     } n   } n   printf( %d , a - 1); n } n


#include <bits/stdc++.h> n using namespace std; n bool primecheck(long long int a) { n   bool check = true; n   for (long long int i = 2; i <= sqrt(a); i++) { n     if (a % i == 0) { n       check = false; n       break; n     } n   } n   return (check); n } n vector<long long int> printDivisors(long long int n) { n   vector<long long int> sol; n   for (long long int i = 1; i <= sqrt(n); i++) { n     if (n % i == 0) { n       if (n / i == i) { n         sol.push_back(i); n       } else { n         sol.push_back(i); n         sol.push_back(n / i); n       } n     } n   } n   return (sol); n } n long long int digits(long long int a) { return (floor(log10(a)) + 1); } n long long binpow(long long a, long long b, long long m) { n   a %= m; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n int gcd(int a, int b) { n   if (a == 0) return b; n   if (b == 0) return a; n   if (a == b) return a; n   if (a > b) return gcd(a - b, b); n   return gcd(a, b - a); n } n long long int lcm(long long int a, long long int b) { n   return (a * b) / gcd(a, b); n } n bool check(string a, string b) { n   long long int freq1[26] = {0}; n   long long int i; n   for (i = 0; i < a.size(); i++) { n     freq1[a[i] -  a ]++; n   } n   for (i = 0; i < b.size(); i++) { n     freq1[b[i] -  a ]--; n   } n   bool sol = true; n   for (i = 0; i < 26; i++) n     if (freq1[i] != 0) sol = false; n   return (sol); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int a, b, c, i, j, k, l, m, n, t; n   cin >> t; n   while (t--) { n     string num1, num2; n     cin >> num1 >> num2; n     bool sol; n     long long int size1 = num1.size(); n     long long int size2 = num2.size(); n     if (size2 < size1) { n       cout <<  NO  n            <<   n ; n     } else { n       sol = false; n       for (i = 0; i <= size2 - size1; i++) { n         string fresh = num1; n         for (j = 0; j < size1; j++) { n           fresh[j] = num2[i + j]; n         } n         if (check(num1, fresh)) { n           cout <<  YES  n                <<   n ; n           sol = true; n           break; n         } n       } n       if (!sol) n         cout <<  NO  n              <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n const int INF = 0x3f3f3f3f; n const double pi = acos(-1.0); n const double eps = 1e-9; n template <class T> n inline void read(T &x) { n   int c; n   for (c = getchar(); c < 32 && ~c; c = getchar()) n     ; n   for (x = 0; c > 32; x = x * 10 + c -  0 , c = getchar()) n     ; n } n int n, m; n int chu[maxn]; n vector<int> g[maxn]; n priority_queue<int> q; n int ind; n int lab[maxn]; n int main() { n   read(n), read(m); n   int u, v; n   for (int i = 0; i < m; i++) { n     read(v), read(u); n     g[u].emplace_back(v); n     chu[v]++; n   } n   ind = n; n   for (int i = 1; i <= n; i++) { n     if (chu[i] == 0) { n       q.emplace(i); n     } n   } n   while (!q.empty()) { n     int x = q.top(); n     q.pop(); n     lab[x] = ind--; n     for (auto &i : g[x]) { n       chu[i]--; n       if (chu[i] == 0) q.emplace(i); n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << lab[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int MX = 2e5 + 5; n const long long INF = 1e18; n const long double PI = 4 * atan((long double)1); n template <class T> n bool ckmin(T& a, const T& b) { n   return a > b ? a = b, 1 : 0; n } n template <class T> n bool ckmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); n namespace input { n template <class T> n void re(complex<T>& x); n template <class T1, class T2> n void re(pair<T1, T2>& p); n template <class T> n void re(vector<T>& a); n template <class T, size_t SZ> n void re(array<T, SZ>& a); n template <class T> n void re(T& x) { n   cin >> x; n } n void re(double& x) { n   string t; n   re(t); n   x = stod(t); n } n void re(long double& x) { n   string t; n   re(t); n   x = stold(t); n } n template <class T, class... Ts> n void re(T& t, Ts&... ts) { n   re(t); n   re(ts...); n } n template <class T> n void re(complex<T>& x) { n   T a, b; n   re(a, b); n   x = cd(a, b); n } n template <class T1, class T2> n void re(pair<T1, T2>& p) { n   re(p.first, p.second); n } n template <class T> n void re(vector<T>& a) { n   for (int i = (0); i < ((int)a.size()); ++i) re(a[i]); n } n template <class T, size_t SZ> n void re(array<T, SZ>& a) { n   for (int i = (0); i < (SZ); ++i) re(a[i]); n } n }  // namespace input n using namespace input; n namespace output { n void pr(int x) { cout << x; } n void pr(long x) { cout << x; } n void pr(long long x) { cout << x; } n void pr(unsigned x) { cout << x; } n void pr(unsigned long x) { cout << x; } n void pr(unsigned long long x) { cout << x; } n void pr(float x) { cout << x; } n void pr(double x) { cout << x; } n void pr(long double x) { cout << x; } n void pr(char x) { cout << x; } n void pr(const char* x) { cout << x; } n void pr(const string& x) { cout << x; } n void pr(bool x) { pr(x ?  true  :  false ); } n template <class T> n void pr(const complex<T>& x) { n   cout << x; n } n template <class T1, class T2> n void pr(const pair<T1, T2>& x); n template <class T> n void pr(const T& x); n template <class T, class... Ts> n void pr(const T& t, const Ts&... ts) { n   pr(t); n   pr(ts...); n } n template <class T1, class T2> n void pr(const pair<T1, T2>& x) { n   pr( { , x.first,  ,  , x.second,  } ); n } n template <class T> n void pr(const T& x) { n   pr( { ); n   bool fst = 1; n   for (const auto& a : x) pr(!fst ?  ,   :   , a), fst = 0; n   pr( } ); n } n void ps() { pr(  n ); } n template <class T, class... Ts> n void ps(const T& t, const Ts&... ts) { n   pr(t); n   if (sizeof...(ts)) pr(   ); n   ps(ts...); n } n void pc() { pr( ] n ); } n template <class T, class... Ts> n void pc(const T& t, const Ts&... ts) { n   pr(t); n   if (sizeof...(ts)) pr( ,  ); n   pc(ts...); n } n }  // namespace output n using namespace output; n namespace io { n void setIn(string second) { freopen(second.c_str(),  r , stdin); } n void setOut(string second) { freopen(second.c_str(),  w , stdout); } n void setIO(string second =   ) { n   cin.sync_with_stdio(0); n   cin.tie(0); n   if ((int)second.size()) { n     setIn(second +  .in ), setOut(second +  .out ); n   } n } n }  // namespace io n using namespace io; n typedef decay<decltype(MOD)>::type T; n struct mi { n   T val; n   explicit operator T() const { return val; } n   mi() { val = 0; } n   mi(const long long& v) { n     val = (-MOD <= v && v <= MOD) ? v : v % MOD; n     if (val < 0) val += MOD; n   } n   friend void pr(const mi& a) { pr(a.val); } n   friend void re(mi& a) { n     long long x; n     re(x); n     a = mi(x); n   } n   friend bool operator==(const mi& a, const mi& b) { return a.val == b.val; } n   friend bool operator!=(const mi& a, const mi& b) { return !(a == b); } n   friend bool operator<(const mi& a, const mi& b) { return a.val < b.val; } n   mi operator-() const { return mi(-val); } n   mi& operator+=(const mi& m) { n     if ((val += m.val) >= MOD) val -= MOD; n     return *this; n   } n   mi& operator-=(const mi& m) { n     if ((val -= m.val) < 0) val += MOD; n     return *this; n   } n   mi& operator*=(const mi& m) { n     val = (long long)val * m.val % MOD; n     return *this; n   } n   friend mi pow(mi a, long long p) { n     mi ans = 1; n     assert(p >= 0); n     for (; p; p /= 2, a *= a) n       if (p & 1) ans *= a; n     return ans; n   } n   friend mi inv(const mi& a) { n     assert(a != 0); n     return pow(a, MOD - 2); n   } n   mi& operator/=(const mi& m) { return (*this) *= inv(m); } n   friend mi operator+(mi a, const mi& b) { return a += b; } n   friend mi operator-(mi a, const mi& b) { return a -= b; } n   friend mi operator*(mi a, const mi& b) { return a *= b; } n   friend mi operator/(mi a, const mi& b) { return a /= b; } n }; n int n, ways; n string g[2000]; n pair<int, int> ans = {MOD, MOD}; n bool check(vector<int> deg) { n   vector<int> v; n   for (int i = (0); i < (n); ++i) v.push_back(deg[i]); n   sort(begin(v), end(v)); n   int sum = 0; n   for (int i = (0); i < ((int)v.size() - 1); ++i) { n     sum += v[i]; n     if (sum == i * (i + 1) / 2) return 0; n   } n   return 1; n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   re(n); n   vector<int> deg(n); n   for (int i = (0); i < (n); ++i) { n     re(g[i]); n     for (int j = (0); j < (n); ++j) n       if (g[i][j] ==  1 ) deg[i]++; n   } n   if (check(deg)) { n     ps(0, 1); n     exit(0); n   } n   for (int i = (0); i < (n); ++i) { n     for (int j = (0); j < (n); ++j) n       if (i != j) { n         if (g[i][j] ==  0 ) { n           deg[j]--; n           deg[i]++; n         } else { n           deg[j]++; n           deg[i]--; n         } n       } n     ways += check(deg); n     for (int j = (0); j < (n); ++j) n       if (i != j) { n         if (g[i][j] ==  0 ) { n           deg[j]++; n           deg[i]--; n         } else { n           deg[j]--; n           deg[i]++; n         } n       } n   } n   if (ways) n     ps(1, ways); n   else { n     assert(n <= 6); n     pair<int, long long> ans = {MOD, MOD}; n     for (int i = (0); i < (1 << n); ++i) { n       vector<int> deg(n); n       for (int j = (0); j < (n); ++j) n         for (int k = (0); k < (n); ++k) n           if (j != k) { n             int ori = g[j][k] -  0 ; n             if (i & (1 << j)) ori ^= 1; n             if (i & (1 << k)) ori ^= 1; n             deg[j] += ori; n           } n       int p = __builtin_popcount(i); n       if (check(deg)) { n         if (p < ans.first) ans = {p, 0}; n         if (p == ans.first) ans.second++; n       } n     } n     if (ans.first == MOD) { n       ps(-1); n       exit(0); n     } n     for (int i = (1); i < (ans.first + 1); ++i) ans.second *= i; n     ps(ans.first, ans.second); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007LL; n long long dp[64]; n long long solve(long long k) { n   if (dp[k] != -1) return dp[k]; n   if (k == 0) return 1LL; n   return ((16LL * solve(k - 1) % mod) * (solve(k - 1) % mod) % mod + mod) % mod; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long k; n   cin >> k; n   for (int i = (0); i < (64); i++) dp[i] = -1; n   cout << ((6LL * solve(k - 1)) % mod + mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, A[100], B[100], t, r, x; n string s =   ; n int main() { n   cin >> n; n   for (int i = 0; i <= n; i++) { n     r = 0; n     for (int j = 0; j <= n; j++) { n       if ((t + r) == n) { n         x = 1; n         break; n       } n       r = r + 7; n     } n     if (x == 1) break; n     t = t + 4; n   } n   if (x == 1) { n     for (int i = 0; i < t / 4; i++) s +=  4 ; n     for (int i = 0; i < r / 7; i++) s +=  7 ; n   } else { n     s =  -1 ; n   } n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.14159265; n void fastInOut(); n const int N = 5003; n const long long OO = (long long)1e18; n int n, m, k; n long long cum[N], memo[N][N]; n long long go(int i, int c) { n   if (i == n + 1) return c == k ? 0 : -OO; n   long long &ret = memo[i][c]; n   if (ret != -1) return ret; n   ret = go(i + 1, c); n   if (i + m <= n + 1) n     ret = max(ret, cum[i + m - 1] - cum[i - 1] + go(i + m, c + 1)); n   return ret; n } n int main() { n   fastInOut(); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     cin >> cum[i]; n     cum[i] += cum[i - 1]; n   } n   memset(memo, -1, sizeof(memo)); n   cout << go(1, 0); n   return 0; n } n void fastInOut() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL), cout.tie(NULL); n } n


#include <bits/stdc++.h> n using namespace std; n long long int arr[100005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   long long int sum = 0; n   for (int i = 0; i < n; ++i) { n     cin >> arr[i]; n     sum = sum + arr[i]; n   } n   if (sum & 1) { n     cout <<  NO  << endl; n     return 0; n   } n   sum = sum / 2; n   multiset<long long int> m1, m2; n   for (int i = 0; i < n; ++i) { n     m2.insert(arr[i]); n   } n   for (int i = 0; i < n; ++i) { n     m1.insert(arr[i]); n     m2.erase(m2.find(arr[i])); n     sum = sum - arr[i]; n     if (sum == 0 || m1.find(-sum) != m1.end() || m2.find(sum) != m2.end()) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int powmod(int a, int p, int m) { n   int64_t ans = 1, cp = a; n   for (int i = 0; (1ll << i) <= p; ++i, cp = cp * cp % m) n     if ((1ll << i) & p) ans = ans * cp % m; n   return ans; n } n int divmod(int64_t a, int b, int m) { return a * powmod(b, m - 2, m) % m; } n const int N = 200000; n int a[N], t[N], b[N], n, m; n unordered_set<int> pres; n vector<int> s; n double et; n pair<int, int> solve() { n   { n     if (n > 2) n       if (rand() & 1) n         swap(a[0], a[rand() % (n - 2) + 2]), n             swap(a[1], a[rand() % (n - 2) + 2]); n       else n         swap(a[1], a[rand() % (n - 2) + 2]), n             swap(a[0], a[rand() % (n - 2) + 2]); n     s.clear(); n     for (int i = 0; i < n; ++i) s.push_back((a[i] - a[0] + m) % m); n     sort(begin(s), end(s)); n     int dst = 0; n     for (int i = 0; i < n; ++i) { n       auto t = lower_bound(begin(s), end(s), (a[i] - a[1] + m) % m); n       if (t == s.end() || *t != (a[i] - a[1] + m) % m) dst++; n     } n     int d = divmod((a[1] - a[0] + m) % m, dst, m), heads = 0; n     for (int i = 0; i < n; ++i) heads += !pres.count((a[i] + d) % m); n     if (!~-heads) n       for (int i = 0; i < n; ++i) n         if (!pres.count((a[i] + d) % m)) return {a[i], (m - d) % m}; n   } n   return {-1, -1}; n } n int main() { n   srand(101); n   cin.tie(0), ios::sync_with_stdio(0); n   int comp = 0; n   cin >> m >> n; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   if (n == 1 || n == m) return cout << a[0] <<     << 1, 0; n   if (n > m - n) { n     sort(a, a + n); n     for (int i = 0; i < m; ++i) t[i] = i; n     memcpy(a, b, (set_difference(t, t + m, a, a + n, b) - b) * sizeof *b); n     comp = 1; n     n = m - n; n   } n   for (int i = 0; i < n; ++i) pres.insert(a[i]); n   random_shuffle(a, a + n); n   auto ans = solve(); n   if (!~ans.first) n     cout <<  -1 ; n   else if (!comp) n     cout << ans.first <<     << ans.second <<   n ; n   else n     cout << (ans.first - ans.second + m) % m <<     << (m - ans.second) % m n          <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     vector<int> p(n); n     int id = -1; n     for (int i = 0; i < n; ++i) { n       cin >> p[i]; n       if (p[i] == 1) id = i; n     } n     bool f1 = 0, f2 = 0; n     int val = 0; n     for (int i = id, j = 0; j < n; ++i, ++j) { n       ++val; n       if (p[i % n] != val) f1 = 1; n     } n     val = 0; n     for (int i = id, j = 0; j < n; --i, ++j) { n       ++val; n       if (i < 0) i += n; n       if (p[i % n] != val) f2 = 1; n     } n     if (f1 == 0 || f2 == 0) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n int f[11][11][4][maxn]; n int n; n char s[maxn]; n char c[maxn]; n int lowbit(int x) { return x & (-x); } n int ch(char s) { n   if (s ==  A ) n     return 1; n   else if (s ==  T ) n     return 2; n   else if (s ==  G ) n     return 3; n   else if (s ==  C ) n     return 4; n   return -1; n } n void Add(int *tr, int x, int d) { n   for (; x <= n; x += lowbit(x)) { n     tr[x] += d; n   } n } n int Sum(int *tr, int x) { n   if (x == 0) return 0; n   int s = 0; n   while (x) { n     s += tr[x]; n     x -= lowbit(x); n   } n   return s; n } n int main() { n   scanf( %s , s + 1); n   n = strlen(s + 1); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= 10; j++) Add(f[j][i % j][ch(s[i])], i, 1); n   int q, x, d, l, r; n   scanf( %d , &q); n   while (q--) { n     scanf( %d , &x); n     if (x == 1) { n       scanf( %d%s , &d, c); n       for (int i = 1; i <= 10; i++) { n         Add(f[i][d % i][ch(s[d])], d, -1); n       } n       s[d] = c[0]; n       for (int i = 1; i <= 10; i++) Add(f[i][d % i][ch(s[d])], d, 1); n     } else { n       scanf( %d %d %s , &l, &r, c); n       int len = strlen(c); n       int ans = 0; n       for (int i = 0; i < len; i++) { n         ans += Sum(f[len][(l + i) % len][ch(c[i])], r) - n                Sum(f[len][(l + i) % len][ch(c[i])], l - 1); n       } n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[55][55]; n double comb[55]; n int a[55]; n int main(int argc, char **argv) { n   int n, p; n   int tot = 0; n   double res = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     tot += a[i]; n   } n   cin >> p; n   if (tot <= p) { n     cout << n << endl; n     return 0; n   } n   comb[0] = 1; n   for (int i = 1; i <= n; i++) comb[i] = comb[i - 1] * i; n   for (int block = 0; block < n; block++) { n     memset(dp, 0, sizeof(dp)); n     dp[0][0] = 1; n     for (int i = 0; i < n; i++) { n       if (i == block) continue; n       for (int j = p - a[i]; j >= 0; j--) { n         for (int k = 0; k < n - 1; k++) { n           dp[j + a[i]][k + 1] += dp[j][k]; n         } n       } n     } n     for (int i = 0; i <= p; i++) { n       if (a[block] + i > p) { n         for (int j = 1; j <= n - 1; j++) { n           res += j * dp[i][j] * comb[j] * comb[n - 1 - j] / comb[n]; n         } n       } n     } n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int x1, y1, x2, y2; n   int sum = 0; n   for (int i = 0; i < n; ++i) { n     cin >> x1 >> y1 >> x2 >> y2; n     sum += (abs(x2 - x1) + 1) * (abs(y2 - y1) + 1); n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   bool good[n]; n   for (int i = 0; i < n; i++) good[i] = 1; n   for (int i = 0; i < n; i++) { n     int isGood = 1; n     for (int j = 0; j < n; j++) { n       int dum; n       cin >> dum; n       if (dum == 1 || dum == 3) n         good[i] = 0; n       else if (dum == 2) n         good[j] = 0; n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) n     if (good[i]) ans++; n   cout << ans << endl; n   for (int i = 0; i < n; i++) { n     if (good[i]) cout << i + 1 <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, s, i, v = 0, a[1010]; n   cin >> n >> s; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n     v = v + a[i]; n   } n   for (i = 1; i <= n; i++) n     if ((v - a[i]) <= s) { n       cout <<  YES ; n       return 0; n     } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005], b[100005]; n int main() { n   int n, m, i; n   long long sum = 0; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= m; i++) { n     scanf( %d , &a[i]); n     sum += a[i]; n     if (a[i] + i > n + 1) { n       printf( -1 ); n       return 0; n     } n   } n   if (sum < n) { n     printf( -1 ); n     return 0; n   } n   b[m + 1] = 100005; n   for (i = m; i; i--) { n     b[i] = min(n - a[i] + 1, b[i + 1] - 1); n   } n   b[1] = 1; n   for (i = 2; i <= m; i++) { n     if (b[i - 1] + a[i - 1] >= b[i]) { n       break; n     } n     b[i] = b[i - 1] + a[i - 1]; n   } n   for (i = 1; i <= m; i++) { n     printf( %d  , b[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int n, m, in[N], out[N], id[N], dep[N]; n vector<int> v[N]; n int idx; n void dfs(int pos, int dd) { n   in[pos] = ++idx; n   id[idx] = pos; n   dep[idx] = dd; n   for (auto &i : v[pos]) dfs(i, dd + 1); n   out[pos] = ++idx; n   id[idx] = pos; n   dep[idx] = dd; n } n int fa[N], ch[N][2], mn[N], mx[N], val[N], sz[N]; n inline void pushup(int x) { n   sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1; n   mn[x] = mx[x] = dep[x]; n   mn[x] = min({mn[x], mn[ch[x][0]], mn[ch[x][1]]}); n   mx[x] = max({mx[x], mx[ch[x][0]], mx[ch[x][1]]}); n } n int build(int l, int r) { n   int mid = (l + r) >> 1, x = mid; n   if (l < mid) ch[x][0] = build(l, mid - 1), fa[ch[x][0]] = x; n   if (mid < r) ch[x][1] = build(mid + 1, r), fa[ch[x][1]] = x; n   pushup(x); n   return x; n } n inline void push(int x, int v) { n   val[x] += v; n   dep[x] += v; n   mn[x] += v; n   mx[x] += v; n } n inline void pushdown(int x) { n   if (val[x]) { n     if (ch[x][0]) push(ch[x][0], val[x]); n     if (ch[x][1]) push(ch[x][1], val[x]); n     val[x] = 0; n   } n } n inline bool sd(int x) { return ch[fa[x]][1] == x; } n void rotate(int x) { n   int y = fa[x], z = fa[y], k = sd(x); n   if (z) ch[z][sd(y)] = x; n   fa[x] = z; n   fa[y] = x; n   if (ch[x][!k]) fa[ch[x][!k]] = y; n   ch[y][k] = ch[x][!k]; n   ch[x][!k] = y; n   pushup(y); n   pushup(x); n } n void splay(int x, int target = 0) { n   static int stk[N], ptr; n   stk[ptr = 1] = x; n   int tmp = x; n   while (fa[tmp] != target) stk[++ptr] = tmp = fa[tmp]; n   while (ptr) pushdown(stk[ptr--]); n   while (fa[x] != target) { n     int y = fa[x]; n     if (fa[y] != target) rotate(sd(x) == sd(y) ? y : x); n     rotate(x); n   } n   pushup(x); n } n int kth(int x, int kk) { n   pushdown(x); n   if (mn[ch[x][1]] <= kk && mx[ch[x][1]] >= kk) return kth(ch[x][1], kk); n   if (dep[x] == kk) { n     splay(x); n     return id[x]; n   } n   return kth(ch[x][0], kk); n } n inline int split(int l, int r) { n   splay(l); n   splay(r, l); n   return ch[r][0]; n } n int pre(int x) { n   splay(x); n   x = ch[x][0]; n   while (ch[x][1]) x = ch[x][1]; n   splay(x); n   return x; n } n int suf(int x) { n   splay(x); n   x = ch[x][1]; n   while (ch[x][0]) x = ch[x][0]; n   splay(x); n   return x; n } n int erase(int l, int r) { n   int lp = pre(l), rp = suf(r), p = split(lp, rp), v = fa[p]; n   ch[v][0] = fa[p] = 0; n   return p; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     int kk; n     cin >> kk; n     v[i].resize(kk); n     for (int j = 0; j < kk; j++) cin >> v[i][j]; n   } n   mn[0] = INT_MAX >> 1; n   dfs(1, 0); n   build(1, idx); n   while (m--) { n     int op; n     cin >> op; n     if (op == 1) { n       int t1, t2; n       cin >> t1 >> t2; n       splay(in[t1]); n       int rk1 = sz[ch[in[t1]][0]] + 1, ans = dep[in[t1]]; n       splay(in[t2]); n       int rk2 = sz[ch[in[t2]][0]] + 1; n       ans += dep[in[t2]]; n       if (rk1 > rk2) swap(t1, t2); n       t1 = split(in[t1], out[t2]); n       ans -= (mn[t1] - 1) * 2; n       cout << ans << endl; n     } else if (op == 2) { n       int t1, t2; n       cin >> t1 >> t2; n       int val = t2 - 1; n       splay(in[t1]); n       t2 = kth(ch[in[t1]][0], dep[in[t1]] - t2); n       t1 = erase(in[t1], out[t1]); n       push(t1, -val); n       int xx = pre(out[t2]); n       splay(xx); n       splay(out[t2], xx); n       ch[out[t2]][0] = t1; n       fa[t1] = out[t2]; n       pushup(out[t2]); n       pushup(xx); n     } else { n       int k; n       cin >> k; n       splay(1); n       cout << kth(1, k) << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n vector<int> adj[(int)1e5 + 7]; n int vis[(int)1e5 + 7]; n int deg[(int)1e5 + 7]; n int cnt[(int)1e5 + 7]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   ; n   scanf( %d %d , &n, &k); n   for (int i = 1; i <= n - 1; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n     deg[v]++; n     deg[u]++; n   } n   int lvl = 0; n   queue<int> q; n   for (int i = 1; i <= n; i++) n     if (deg[i] == 1) q.push(i); n   while (q.size()) { n     set<int> kroots; n     int sz = q.size(); n     while (sz--) { n       int root = q.front(); n       q.pop(); n       vis[root] = 1; n       for (auto neighbour : adj[root]) { n         if (vis[neighbour]) { n           continue; n         } n         deg[neighbour]--; n         cnt[neighbour]++; n         kroots.insert(neighbour); n       } n     } n     for (auto val : kroots) { n       if (vis[val] || deg[val] > 1 || cnt[val] < 3) { n         puts( No ); n         return 0; n       } n       q.push(val); n     } n     if (kroots.size()) n       lvl++; n     else n       break; n   } n   if (lvl == k) n     puts( Yes ); n   else n     puts( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1000000007; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t, n, m, a, b, c, q, k, d, x; n   cin >> n >> m; n   vector<vector<int>> mat(n + 1, vector<int>(m + 1)), n       ans(n + 1, vector<int>(m + 1)); n   char ch; n   for (int i = 1; i < n + 1; i++) { n     for (int j = 1; j < m + 1; j++) { n       cin >> ch; n       mat[i][j] = ch -  0 ; n       ans[i][j] = ans[i - 1][j] + ans[i][j - 1] - ans[i - 1][j - 1] + mat[i][j]; n     } n   } n   a = 0; n   for (int i = 1; i < n + 1; i++) { n     for (int j = 1; j < m + 1; j++) { n       for (int i_ = i; i_ < n + 1; i_++) { n         for (int j_ = j; j_ < m + 1; j_++) { n           if (ans[i_][j_] - ans[i - 1][j_] - ans[i_][j - 1] + n                   ans[i - 1][j - 1] == n               0) { n             a = max(a, 2 * (i_ - i + 1 + j_ - j + 1)); n           } n         } n       } n     } n   } n   cout << a <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double EPS = 1e-9; n long long INF = 1000000000000000005ll; n double PI = acos(-1); n inline string IntToString(long long int a) { n   char x[100]; n   sprintf(x,  %lld , a); n   string s = x; n   return s; n } n inline long long int StringToInt(string a) { n   char x[100]; n   long long int res; n   strcpy(x, a.c_str()); n   sscanf(x,  %lld , &res); n   return res; n } n inline string uppercase(string s) { n   int n = (int)s.size(); n   for (long long int i = 0; i < n; i++) n     if (s[i] >=  a  && s[i] <=  z ) s[i] = s[i] -  a  +  A ; n   return s; n } n inline string lowercase(string s) { n   int n = (int)s.size(); n   for (long long int i = 0; i < n; i++) n     if (s[i] >=  A  && s[i] <=  Z ) s[i] = s[i] -  A  +  a ; n   return s; n } n bool sortcol(const vector<long long int>& v1, const vector<long long int>& v2) { n   return v1[0] > v2[0]; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int n; n   cin >> n; n   long long int c[n]; n   string r[n]; n   string s[n]; n   for (long long int i = 0; i < n; i++) cin >> c[i]; n   for (long long int i = 0; i < n; i++) { n     cin >> r[i]; n     s[i] = r[i]; n     reverse(r[i].begin(), r[i].end()); n   } n   long long int dp[n + 1][2]; n   memset(dp, 0, sizeof(dp)); n   dp[0][1] = c[0]; n   for (long long int i = 1; i < n; i++) { n     dp[i][0] = INF; n     dp[i][1] = INF; n     if (s[i] >= s[i - 1]) { n       dp[i][0] = min(dp[i - 1][0], dp[i][0]); n     } n     if (s[i] >= r[i - 1]) { n       dp[i][0] = min(dp[i - 1][1], dp[i][0]); n     } n     if (r[i] >= s[i - 1]) { n       dp[i][1] = min(dp[i - 1][0] + c[i], dp[i][1]); n     } n     if (r[i] >= r[i - 1]) { n       dp[i][1] = min(dp[i - 1][1] + c[i], dp[i][1]); n     } n   } n   long long int mi = min(dp[n - 1][0], dp[n - 1][1]); n   if (mi != INF) n     cout << min(dp[n - 1][0], dp[n - 1][1]) <<   n ; n   else n     cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T Min(T x, T y) { n   return x < y ? x : y; n } n template <class T> n inline T Max(T x, T y) { n   return x > y ? x : y; n } n bool MOP1; n long long T, n, res; n long long head[(105)], nxt[(105)], to[(105)], line; n void add(long long x, long long y) { n   nxt[++line] = head[x], to[line] = y, head[x] = line; n } n struct node { n   string name; n   long long lei, X, Y; n   long long tot, mx; n   long long sz, bo, sp; n   long long x() { n     if (lei == 2) return sz ? tot + bo * 2 + (sz - 1) * sp : 0; n     if (lei == 3) return sz ? mx + bo * 2 : 0; n   } n   long long y() { n     if (lei == 2) return sz ? mx + bo * 2 : 0; n     if (lei == 3) return sz ? tot + bo * 2 + (sz - 1) * sp : 0; n   } n   void add(node now) { n     sz++; n     if (lei == 2) { n       tot += now.X; n       mx = Max(mx, now.Y); n     } else if (lei == 3) { n       tot += now.Y; n       mx = Max(mx, now.X); n     } n   } n   bool operator<(const node &a) const { return name < a.name; } n } A[(105)]; n long long read(string k) { n   long long res = 0; n   for (long long i = 0; i < k.size(); i++) n     res = (res << 1) + (res << 3) + (k[i] ^ 48); n   return res; n } n map<string, long long> mp; n string str; n long long a, b; n bool mark[(105)]; n void dfs(long long x) { n   mark[x] = 1; n   for (long long i = head[x]; i; i = nxt[i]) { n     long long y = to[i]; n     if (!mark[y]) dfs(y), mark[y] = 1; n     A[x].add(A[y]); n   } n   if (A[x].lei != 1) { n     A[x].X = A[x].x(); n     A[x].Y = A[x].y(); n   } n   return; n } n bool MOP2; n signed main() { n   scanf( %d , &T); n   while (T--) { n     cin >> str; n     if (str[0] ==  W ) { n       getchar(); n       getline(cin, str,  ( ); n       mp[str] = ++n; n       A[n].lei = 1; n       A[n].name = str; n       scanf( %d,%d) , &A[n].X, &A[n].Y); n     } else if (str[0] ==  H ) { n       cin >> str; n       mp[str] = ++n; n       A[n].name = str; n       A[n].lei = 2; n     } else if (str[0] ==  V ) { n       cin >> str; n       mp[str] = ++n; n       A[n].name = str; n       A[n].lei = 3; n     } else { n       res = str.find( . ); n       a = mp[str.substr(0, res)]; n       if (str[res + 1] ==  s ) { n         long long now = str.find( ( ) + 1; n         long long tmp = read(str.substr(now, str.size() - now - 1)); n         if (str[res + 5] ==  b ) { n           A[a].bo = tmp; n         } else if (str[res + 5] ==  s ) { n           A[a].sp = tmp; n         } n       } else { n         b = mp[str.substr(res + 6, str.size() - res - 7)]; n         add(a, b); n       } n     } n   } n   for (long long i = 1; i <= n; i++) n     if (!mark[i]) dfs(i); n   sort(A + 1, A + 1 + n); n   for (long long i = 1; i <= n; i++) n     cout << A[i].name <<     << A[i].X <<     << A[i].Y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double EPS = 1e-9; n inline int cmp(double x, double y = 0, double tol = EPS) { n   return ((x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1); n } n struct point { n   long long x, y; n   point(long long x = 0, long long y = 0) : x(x), y(y) {} n   point operator+(point q) { return point(x + q.x, y + q.y); } n   point operator-(point q) { return point(x - q.x, y - q.y); } n   int cmp(point q) const { n     if (int t = ::cmp(x, q.x)) return t; n     return ::cmp(y, q.y); n   } n   bool operator==(point q) const { return cmp(q) == 0; }; n   bool operator!=(point q) const { return cmp(q) != 0; }; n   bool operator<(point q) const { return cmp(q) < 0; }; n }; n ostream &operator<<(ostream &os, const point &p) { n   os <<  (  << p.x <<  ,  << p.y <<  ) ; n } n long long dot(point a, point b) { return a.x * b.x + a.y * b.y; } n long long cross(point a, point b) { return a.x * b.y - a.y * b.x; } n long long abs2(point a) { return dot(a, a); } n bool collinear(point a, point b, point c) { n   return cmp(cross((b - a), (c - a))) == 0; n } n int ccw(point a, point b, point p) { return cmp(cross((b - a), (p - a))); } n point pivot; n bool radial_lt(point a, point b) { n   int R = ccw(pivot, a, b); n   if (R == 0) n     return dot(pivot - a, pivot - a) < dot(pivot - b, pivot - b); n   else n     return (R == 1); n } n vector<point> convexhull(vector<point> T) { n   sort(T.begin(), T.end()); n   T.resize(unique(T.begin(), T.end()) - T.begin()); n   int tam = 0, n = T.size(); n   vector<point> U; n   int idx = min_element(T.begin(), T.end()) - T.begin(); n   pivot = T[idx]; n   swap(T[0], T[idx]); n   sort(++T.begin(), T.end(), radial_lt); n   for (int i = 0; i < T.size(); i++) { n     while (tam > 1 && ccw(U[tam - 2], U[tam - 1], T[i]) <= 0) n       U.pop_back(), tam--; n     U.push_back(T[i]); n     tam++; n   } n   return U; n } n void show(pair<pair<long long, long long>, pair<long long, long long> > s) { n   cout << s.first.first <<     << s.first.second <<     << s.second.first <<     n        << s.second.second << endl; n } n int least_rot( n     vector<pair<pair<long long, long long>, pair<long long, long long> > > n         second) { n   int n = second.size(); n   for (int i = (0); i < (n); i++) second.push_back(second[i]); n   vector<int> f(second.size(), -1); n   int k = 0; n   for (int j = 1; j < second.size(); j++) { n     pair<pair<long long, long long>, pair<long long, long long> > sj = n         second[j]; n     int i = f[j - k - 1]; n     while (i != -1 && sj != second[k + i + 1]) { n       if (sj < second[k + i + 1]) k = j - i - 1; n       i = f[i]; n     } n     if (sj != second[k + i + 1]) { n       if (sj < second[k]) k = j; n       f[j - k] = -1; n     } else n       f[j - k] = i + 1; n   } n   return k; n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   vector<point> A(n), B(m); n   point p; n   for (int i = (0); i < (n); i++) { n     scanf( %lld %lld , &p.x, &p.y); n     A[i] = p; n   } n   for (int i = (0); i < (m); i++) { n     scanf( %lld %lld , &p.x, &p.y); n     B[i] = p; n   } n   A = convexhull(A); n   B = convexhull(B); n   n = A.size(); n   m = B.size(); n   if (n != m) { n     cout <<  NO n ; n     return 0; n   } n   vector<pair<pair<long long, long long>, pair<long long, long long> > > AA, BB; n   point esq, dir; n   int ee, dd; n   for (int i = (0); i < (n); i++) { n     ee = i - 1; n     if (ee < 0) ee += n; n     dd = i + 1; n     if (dd >= n) dd %= n; n     esq = A[ee] - A[i], dir = A[dd] - A[i]; n     AA.push_back(make_pair(make_pair(cross(esq, dir), dot(esq, dir)), n                            make_pair(abs2(esq), abs2(dir)))); n   } n   for (int i = (0); i < (n); i++) { n     ee = i - 1; n     if (ee < 0) ee += n; n     dd = i + 1; n     if (dd >= n) dd %= n; n     esq = B[ee] - B[i], dir = B[dd] - B[i]; n     BB.push_back(make_pair(make_pair(cross(esq, dir), dot(esq, dir)), n                            make_pair(abs2(esq), abs2(dir)))); n   } n   int u = least_rot(AA); n   int v = least_rot(BB); n   bool ok = true; n   for (int i = (0); i < (n); i++) { n     if (AA[u] != BB[v]) { n       ok = false; n       break; n     } n     u = (u + 1) % n; n     v = (v + 1) % n; n   } n   if (ok) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n const int mx = 110; n int a[mx]; n bool ok(char ch) { n   if (ch ==  a  || ch ==  e  || ch ==  i  || ch ==  o  || ch ==  u  || n       ch ==  y ) n     return true; n   return false; n } n int main() { n   int n; n   bool flag = 1; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   getchar(); n   for (int i = 0; i < n; i++) { n     int num = 0; n     char c; n     while ((c = getchar()) !=   n ) n       if (ok(c)) num++; n     if (num != a[i]) flag = 0; n   } n   printf(flag ?  YES n  :  NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100 + 2, maxm = 1e7 + 5; n struct node { n   int bg, ed, id; n } a[maxn]; n int n, ans[maxn]; n bool vis[maxm]; n bool cmp(node a, node b) { return a.ed < b.ed; } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &a[i].bg, &a[i].ed); n     a[i].id = i; n   } n   sort(a, a + n, cmp); n   for (int i = 0; i < n; i++) { n     int y = a[i].bg; n     while (vis[y]) y++; n     vis[y] = true; n     ans[a[i].id] = y; n   } n   for (int i = 0; i < n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const int N = 1e6 + 10; n const int mod = 1e9 + 7; n int root; n vector<vector<int> > g; n vector<int> ans; n int sub[N], t[N]; n void dfs0(int v, int p) { n   sub[v] = t[v]; n   for (auto u : g[v]) { n     if (u != p) { n       dfs0(u, v); n       sub[v] += sub[u]; n     } n   } n   return; n } n int dfs1(int v, int p) { n   int ret = -1, temp; n   if ((v != root) && 3 * sub[v] == sub[root]) { n     ret = v; n   } n   vector<int> ans; n   for (auto u : g[v]) { n     if (u != p) { n       temp = dfs1(u, v); n       if (temp != -1) { n         if (ret == -1) ret = temp; n         if (sub[v] == 2 * sub[temp] && (sub[root] - sub[v] == sub[temp]) && n             (v != root)) { n           cout << min(v, temp) <<     << max(v, temp) <<   n ; n           exit(0); n         } n         ans.push_back(temp); n         if (ans.size() >= 2) { n           cout << min(ans[0], ans[1]) <<     << max(ans[0], ans[1]) <<   n ; n           exit(0); n         } n       } n     } n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   g.resize(n + 2); n   int p; n   for (int i = 1; i <= n; i++) { n     cin >> p >> t[i]; n     if (p != 0) { n       g[i].push_back(p); n       g[p].push_back(i); n     } else { n       root = i; n     } n   } n   dfs0(root, -1); n   dfs1(root, -1); n   if ((int)ans.size() < 2) { n     cout << -1 <<   n ; n   } else { n     cout << min(ans[0], ans[1]) <<     << max(ans[0], ans[1]) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     cin >> n; n     if (n <= 2) { n       cout << 0 << endl; n       continue; n     } else { n       int x; n       x = n / 2; n       if (n % 2 == 0) x--; n       cout << x << endl; n     } n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int dp[60][20010]; n int S[60][20010]; n int n, m, k; n int sum(int i, int l, int r) { n   if (l > r) return 0; n   return S[i][r] - S[i][l - 1]; n } n int L[20010], R[20010]; n int pre[20010], suf[20010]; n int ST[2][200010]; n void init(int tree[], int A[], int node, int li, int ls) { n   if (li == ls) { n     tree[node] = A[li]; n   } else { n     int mid = (li + ls) / 2; n     init(tree, A, 2 * node, li, mid); n     init(tree, A, 2 * node + 1, mid + 1, ls); n     tree[node] = max(tree[2 * node], tree[2 * node + 1]); n   } n } n int query(int tree[], int node, int li, int ls, int i, int j) { n   if (i > ls || j < li) return -0x3f3f3f3f; n   if (li >= i && ls <= j) return tree[node]; n   int mid = (li + ls) / 2; n   int left = query(tree, 2 * node, li, mid, i, j); n   int right = query(tree, 2 * node + 1, mid + 1, ls, i, j); n   return max(left, right); n } n int main() { n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       scanf( %d , &S[i][j]); n       S[i][j] += S[i][j - 1]; n     } n   } n   int ans = 0; n   for (int i = n; i > 0; i--) { n     for (int j = 1; j <= m; j++) { n       dp[i][j] = 0; n       int l = max(1, j - k + 1), r = min(m, j + k - 1); n       if (l > 1) { n         dp[i][j] = pre[l - 1] + sum(i + 1, j, r); n       } n       if (r < m) { n         dp[i][j] = max(dp[i][j], suf[r + 1] + sum(i + 1, j, r)); n       } n       dp[i][j] = max(dp[i][j], query(ST[0], 1, 1, m, l, j) + S[i + 1][r]); n       dp[i][j] = n           max(dp[i][j], query(ST[1], 1, 1, m, j + 1, r) - S[i + 1][j - 1]); n       dp[i][j] += sum(i, j, r); n       ans = max(ans, dp[i][j]); n     } n     for (int j = 1; j <= m; j++) { n       pre[j] = dp[i][j]; n       suf[j] = dp[i][j]; n     } n     for (int j = 2; j <= m; j++) { n       pre[j] = max(pre[j], pre[j - 1]); n       suf[m - j + 1] = max(suf[m - j + 1], suf[m - j + 2]); n     } n     for (int j = 1; j <= m; j++) { n       L[j] = dp[i][j] - S[i][min(m, j + k - 1)]; n       R[j] = dp[i][j] + S[i][j - 1]; n     } n     init(ST[0], L, 1, 1, m); n     init(ST[1], R, 1, 1, m); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> ans; n void solve(long long n) { n   long long step = 0; n   while (n != 1) { n     if (n % 2 == 0) { n       if (n % 6 != 0) { n         ans.push_back(-1); n         return; n       } n       n /= 6; n       if (n == 0) { n         ans.push_back(-1); n         return; n       } n     } else { n       n *= 2; n       if (n % 6 != 0) { n         ans.push_back(-1); n         return; n       } n     } n     ++step; n   } n   ans.push_back(step); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     solve(n); n   } n   for (int i = (0); i < (ans.size()); ++i) cout << ans[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, nums[2201], ans = 0; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> nums[i]; n   } n   sort(nums, nums + n); n   for (int i = 0; i < k; i++) { n     ans += nums[i]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long linf = 1e18 + 9; n const int inf = 1e9 + 9; n const long long mod = 1e9 + 7; n const int N = 4e5 + 9; n int n, u, v, ans[N], sub[N], mx[N], centroid; n vector<int> gr[N], c[N]; n int dfs(int v, int f) { n   sub[v] = 1; n   for (int i = 0; i < gr[v].size(); i++) { n     if (gr[v][i] == f) continue; n     int t = dfs(gr[v][i], v); n     sub[v] += t; n     mx[v] = max(mx[v], t); n     c[v].push_back(t); n   } n   if ((n - sub[v]) <= n / 2 and mx[v] <= n / 2) { n     ans[v] = 1; n     centroid = v; n   } n   return sub[v]; n } n void dfs2(int v, int f, int cur, int root) { n   if (v != centroid and ans[v] == 0) { n     int t = c[centroid].size() - 1; n     if (sub[root] == c[centroid][t]) t--; n     if (t >= 0 and n - sub[v] - c[centroid][t] <= n / 2) n       ans[v] = 1; n     else { n       if (cur - sub[v] <= n / 2) n         ans[v] = 1; n       else n         ans[v] = 0; n     } n   } n   if (n - sub[v] <= n / 2) cur = sub[v]; n   for (int i = 0; i < gr[v].size(); i++) { n     if (gr[v][i] == f) continue; n     if (v == centroid) root = gr[v][i]; n     dfs2(gr[v][i], v, cur, root); n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     scanf( %d %d , &u, &v); n     gr[u].push_back(v); n     gr[v].push_back(u); n   } n   dfs(1, 0); n   memset(mx, 0, sizeof mx); n   for (int i = 1; i <= n; i++) c[i].clear(); n   dfs(centroid, 0); n   for (int i = 1; i <= n; i++) sort(c[i].begin(), c[i].end()); n   dfs2(centroid, 0, 0, 0); n   for (int i = 1; i <= n; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int x, int y) { return (y == 0 ? x : gcd(y, x % y)); } n const int N = (int)1e6 + 100; n int a[30]; n int n; n int m = 0; n char s[N]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 0; i < n; i++) m += a[i]; n   if (n == 1) { n     printf( %d n , m); n     for (int i = 0; i < m; i++) s[i] =  a ; n     s[m] = (char)0; n     printf( %s n , s); n     return 0; n   } n   int cntOdd = 0; n   for (int i = 0; i < n; i++) n     if (a[i] & 1) cntOdd++; n   if (cntOdd > 1) { n     printf( 0 n ); n     int idx = 0; n     for (int i = 0; i < n; i++) n       for (int j = 0; j < a[i]; j++) s[idx++] = (char)( a  + i); n     s[m] = (char)0; n     printf( %s n , s); n     return 0; n   } n   if (cntOdd == 1) { n     int g = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] & 1) n         g = gcd(g, a[i]); n       else n         g = gcd(g, a[i] / 2); n     } n     printf( %d n , g); n     int idx = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] & 1) continue; n       for (int j = 0; j < a[i] / 2 / g; j++) s[idx++] = (char)( a  + i); n     } n     for (int i = 0; i < n; i++) { n       if ((a[i] & 1) == 0) continue; n       for (int j = 0; j < a[i] / g; j++) s[idx++] = (char)( a  + i); n     } n     for (int i = n - 1; i >= 0; i--) { n       if (a[i] & 1) continue; n       for (int j = 0; j < a[i] / 2 / g; j++) s[idx++] = (char)( a  + i); n     } n     int p = idx; n     for (int i = p; i < m; i++) s[i] = s[i - p]; n     s[m] = (char)0; n     printf( %s n , s); n     return 0; n   } n   int g = 0; n   for (int i = 0; i < n; i++) g = gcd(g, a[i] / 2); n   printf( %d n , 2 * g); n   int idx = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < a[i] / 2 / g; j++) s[idx++] = (char)( a  + i); n   for (int i = n - 1; i >= 0; i--) n     for (int j = 0; j < a[i] / 2 / g; j++) s[idx++] = (char)( a  + i); n   int p = idx; n   for (int i = p; i < m; i++) s[i] = s[i - p]; n   s[m] = (char)0; n   printf( %s n , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5; n int d[maxn]; n int w[maxn]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n; n   cin >> n; n   string s; n   cin >> s; n   for (int i = 0; i < n; ++i) { n     cin >> d[i]; n   } n   int cur = 0; n   while (cur >= 0 && cur < n && !w[cur]) { n     w[cur] = 1; n     cur = cur + (s[cur] ==  <  ? -1 : 1) * d[cur]; n   } n   if (cur < 0 || cur >= n) { n     cout <<  FINITE n ; n   } else { n     cout <<  INFINITE n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int max_n = 22, max_q = 11111, mod = 1000000007; n struct matrix { n   vector<vector<int> > v; n   matrix() {} n   matrix(int n, int m) { v.resize(n, vector<int>(m, 0)); } n   matrix operator*(const matrix &m) const { n     matrix res(v.size(), m.v[0].size()); n     for (int i = 0; i < res.v.size(); ++i) { n       for (int j = 0; j < res.v[i].size(); ++j) { n         for (int k = 0; k < v[i].size(); ++k) { n           res.v[i][j] += (1LL * v[i][k] * m.v[k][j]) % mod; n           res.v[i][j] %= mod; n         } n       } n     } n     return res; n   } n }; n matrix power(matrix a, int b) { n   if (b == 0) { n     matrix res(a.v.size(), a.v.size()); n     for (int i = 0; i < res.v.size(); ++i) { n       res.v[i][i] = 1; n     } n     return res; n   } n   if (b % 2 == 0) { n     return power(a * a, b / 2); n   } n   return a * power(a, b - 1); n } n const int dx[] = {-1, 1, 0, 0, 0}; n const int dy[] = {0, 0, -1, 1, 0}; n int n, m, q, tp[max_q], x[max_q], y[max_q], t[max_q], f[max_q], a[max_n][max_n]; n matrix dp, p; n bool is_in(int x, int y) { return 0 <= x && 0 <= y && x < n && y < m; } n void get_p() { n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       for (int k = 0; k < 5; ++k) { n         int x = i + dx[k]; n         int y = j + dy[k]; n         if (is_in(x, y)) { n           p.v[i * m + j][x * m + y] = 0; n           if (a[x][y] == 0) { n             p.v[i * m + j][x * m + y] = 1; n           } n         } n       } n     } n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 0; i < q; ++i) { n     scanf( %d%d%d%d , &tp[i], &x[i], &y[i], &t[i]); n     --x[i]; n     --y[i]; n     if (tp[i] == 3) { n       ++t[i]; n     } n   } n   dp = matrix(1, n * m); n   p = matrix(n * m, n * m); n   get_p(); n   dp.v[0][0] = 1; n   int nt = 1; n   for (int i = 0; i < q; ++i) { n     if (t[i] != nt) { n       dp = dp * power(p, t[i] - nt - 1); n     } n     while (i + 1 < q && t[i] == t[i + 1]) { n       if (tp[i] == 2) { n         a[x[i]][y[i]] = 1; n       } n       if (tp[i] == 3) { n         a[x[i]][y[i]] = 0; n       } n       ++i; n     } n     if (tp[i] == 2) { n       a[x[i]][y[i]] = 1; n     } n     if (tp[i] == 3) { n       a[x[i]][y[i]] = 0; n     } n     get_p(); n     if (t[i] != nt) { n       dp = dp * p; n     } n     nt = t[i]; n     if (tp[i] == 1) { n       printf( %d n , dp.v[0][x[i] * m + y[i]]); n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int N, S, L, A[100009]; n int c[100009], dp[100009]; n deque<pair<int, int>> s, t, u; n int main() { n   scanf( %d%d%d , &N, &S, &L); n   for (int i = 1; i <= N; i++) dp[i] = 1e6; n   for (int i = 1, ptr = 1; i <= N; i++) { n     scanf( %d , &A[i]); n     while (s.size() && s.back().first >= A[i]) s.pop_back(); n     s.push_back({A[i], i}); n     while (t.size() && t.back().first <= A[i]) t.pop_back(); n     t.push_back({A[i], i}); n     while (ptr < i && t.front().first - s.front().first > S) { n       ptr++; n       while (s.front().second < ptr) s.pop_front(); n       while (t.front().second < ptr) t.pop_front(); n     } n     c[i] = ptr - 1; n   } n   for (int i = 1; i <= N; i++) c[i] = min(c[i], i - L + 1); n   for (int i = 1, ptr = 0; i <= N; i++) { n     if (i >= L) { n       while (u.size() && u.back().first >= dp[i - L]) u.pop_back(); n       u.push_back({dp[i - L], i - L}); n     } n     for (; ptr < c[i]; ptr++) { n       while (u.size() && u.front().second <= ptr) u.pop_front(); n     } n     if (u.size()) dp[i] = u.front().first + 1; n   } n   printf( %d n , dp[N] >= 1e6 ? -1 : dp[N]); n } n


#include <bits/stdc++.h> n using namespace std; n void dfs(long long v, vector<long long> adj[], vector<bool>& visited) { n   cout << char(v +  a ); n   visited[v] = true; n   for (long long u : adj[v]) { n     if (!visited[u]) dfs(u, adj, visited); n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long n; n   cin >> n; n   vector<long long> adj[26]; n   set<char> st; n   vector<string> store; n   for (long long i = 0; i < n; i++) { n     string s; n     cin >> s; n     store.push_back(s); n     st.insert(s[0]); n     for (long long j = 0; j < s.length() - 1; j++) { n       long long a = s[j] -  a , b = s[j + 1] -  a ; n       adj[a].push_back(b); n     } n   } n   queue<long long> q; n   vector<bool> visited(26, false); n   for (auto it : st) { n     bool boo = true; n     for (long long i = 0; i < store.size(); i++) { n       string s = store[i]; n       for (long long j = 1; j < s.length(); j++) { n         if (s[j] == it) { n           boo = false; n           goto h; n         } n       } n     } n   h:; n     if (boo) { n       q.push(it -  a ); n       visited[it -  a ] = true; n     } n   } n   while (!q.empty()) { n     long long v = q.front(); n     q.pop(); n     dfs(v, adj, visited); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> set1; n vector<int> set2; n int col[100004], vis[100004]; n int flag = 1; n class Graph { n   int V; n   list<int> *adj; n  n  public: n   Graph(int V); n   void addEdge(int v, int w); n   void BFS(int s); n }; n Graph::Graph(int V) { n   this->V = V; n   adj = new list<int>[V]; n } n void Graph::addEdge(int v, int w) { n   adj[v].push_back(w); n   adj[w].push_back(v); n } n void Graph::BFS(int s) { n   list<int> queue; n   vis[s] = 1; n   queue.push_back(s); n   col[s] = 0; n   list<int>::iterator i; n   while (!queue.empty()) { n     s = queue.front(); n     queue.pop_front(); n     if (col[s] == 0) { n       set1.push_back(s); n     } else { n       set2.push_back(s); n     } n     for (i = adj[s].begin(); i != adj[s].end(); ++i) { n       if (vis[*i] != 1) { n         vis[*i] = 1; n         queue.push_back(*i); n         col[*i] = (col[s] + 1) % 2; n       } else { n         if ((col[s] + 1) % 2 != col[*i]) { n           flag = 0; n           return; n         } n       } n     } n   } n } n int main() { n   int n, m; n   cin >> n >> m; n   Graph g(n); n   memset(col, -1, sizeof(col)); n   memset(vis, -1, sizeof(vis)); n   int a, b; n   while (m--) { n     cin >> a >> b; n     vis[a - 1] = 0; n     vis[b - 1] = 0; n     g.addEdge(a - 1, b - 1); n   } n   for (int i = 0; i < n; i++) { n     if (vis[i] == 0) g.BFS(i); n     if (flag == 0) break; n   } n   if (flag == 0) { n     cout <<  -1 n ; n   } else { n     cout << set1.size() <<   n ; n     for (vector<int>::iterator i = set1.begin(); i != set1.end(); ++i) n       cout << (*i) + 1 <<    ; n     cout <<   n ; n     cout << set2.size() <<   n ; n     for (vector<int>::iterator i = set2.begin(); i != set2.end(); ++i) n       cout << (*i) + 1 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMax = 100005; n const int sqrNMax = 330; n int N; n int Nb[NMax][sqrNMax + 5]; n int A[NMax], B[sqrNMax + 5][2 * sqrNMax + 5], Start[sqrNMax + 5], Block[NMax], n     totalB, Size[sqrNMax + 5]; n void Read() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) scanf( %d , &A[i]); n } n void buildB() { n   int currentBlock = 1, cnt = 0; n   Start[1] = 1; n   for (int i = 1; i <= N; i++) { n     ++cnt; n     if (cnt == sqrNMax) { n       ++currentBlock; n       Start[currentBlock] = i; n       cnt = 1; n     } n     ++Size[currentBlock]; n     B[currentBlock][cnt] = A[i]; n     Nb[A[i]][currentBlock]++; n     Block[i] = currentBlock; n   } n   totalB = currentBlock; n } n int findBlock(int pos) { n   int sum = 0; n   for (int i = 1; i <= totalB; i++) { n     if (sum + Size[i] >= pos) { n       Start[i] = sum + 1; n       return i; n     } n     sum += Size[i]; n   } n } n int deleteElem(int block, int pos) { n   int p = pos - Start[block] + 1; n   int res = B[block][p]; n   Nb[res][block]--; n   for (int j = p; j < Size[block]; j++) swap(B[block][j], B[block][j + 1]); n   --Size[block]; n   return res; n } n void addElem(int block, int pos, int val) { n   int p = pos - Start[block] + 1; n   Nb[val][block]++; n   B[block][++Size[block]] = val; n   for (int j = Size[block] - 1; j >= p; j--) swap(B[block][j], B[block][j + 1]); n } n void makeCircularPerm(int left, int right) { n   int b = findBlock(right); n   int val = deleteElem(b, right); n   b = findBlock(left); n   addElem(b, left, val); n } n void buildA() { n   int cnt = 0; n   for (int i = 1; i <= totalB; i++) { n     for (int j = 1; j <= Size[i]; j++) A[++cnt] = B[i][j]; n   } n } n void initialize() { n   for (int i = 1; i <= totalB; i++) { n     for (int j = 1; j <= Size[i]; j++) Nb[B[i][j]][i] = 0; n     Size[i] = 0; n     Start[i] = 0; n   } n } n inline int decode(int nb, int lastans) { return (nb + lastans - 1) % N + 1; } n int Query(int left, int right, int val) { n   int ans = 0, b = findBlock(left), b2 = findBlock(right); n   for (int i = b + 1; i < b2; i++) ans += Nb[val][i]; n   int l = left - Start[b] + 1, r = right - Start[b2] + 1; n   if (b == b2) { n     for (int j = l; j <= r; j++) n       if (B[b][j] == val) ++ans; n     return ans; n   } n   for (int j = l; j <= Size[b]; j++) n     if (B[b][j] == val) ans++; n   for (int j = 1; j <= r; j++) n     if (B[b2][j] == val) ++ans; n   return ans; n } n void Solve() { n   int Q; n   scanf( %d , &Q); n   buildB(); n   int lastans = 0, cntQ = 0; n   for (int i = 1; i <= Q; i++) { n     int type, l, r, k; n     scanf( %d%d%d , &type, &l, &r); n     l = decode(l, lastans); n     r = decode(r, lastans); n     if (l > r) swap(l, r); n     if (type == 1) { n       ++cntQ; n       makeCircularPerm(l, r); n       if (cntQ == sqrNMax) { n         cntQ = 0; n         buildA(); n         initialize(); n         buildB(); n       } n     } else { n       scanf( %d , &k); n       k = decode(k, lastans); n       lastans = Query(l, r, k); n       printf( %d n , lastans); n     } n   } n } n int main() { n   Read(); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005], l[100005], r[100005]; n int main() { n   int n, m, i, j, x, y, pos; n   cin >> n >> m; n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   l[1] = r[1] = 0; n   for (i = 2; i <= n; i++) { n     l[i] = l[i - 1]; n     if (a[i - 1] > a[i]) l[i]++; n     r[i] = r[i - 1]; n     if (a[i - 1] < a[i]) r[i]++; n   } n   for (i = 1; i <= m; i++) { n     cin >> x >> y; n     if (r[x] == r[y] || l[x] == l[y]) n       cout <<  Yes  << endl; n     else { n       pos = int(upper_bound(l + 1, l + n + 1, l[x]) - l); n       pos--; n       if (l[pos] == l[x] && r[pos] == r[y]) n         cout <<  Yes  << endl; n       else n         cout <<  No  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ter { n   int cx, cy, dir, pt, rem; n   ter(){}; n   ter(int _cx, int _cy, int _dir, int _pt, int _rem) n       : cx(_cx), cy(_cy), dir(_dir), pt(_pt), rem(_rem){}; n }; n const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}; n const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}; n ter on; n queue<ter> Q; n int n, t[32], cx, cy, dir, pt, rem, dir1, dir2, sol, maxx = -3; n bool bio[312][312][8][31][6], mp[312][312]; n void radi() { n   while (!Q.empty()) { n     on = Q.front(); n     cx = on.cx; n     cy = on.cy; n     dir = on.dir; n     pt = on.pt; n     rem = on.rem; n     Q.pop(); n     if (bio[cx][cy][dir][pt][rem]) continue; n     bio[cx][cy][dir][pt][rem] = 1; n     if (mp[cx][cy] == 0) ++sol; n     mp[cx][cy] = 1; n     if (rem > 0) { n       Q.push(ter(cx + dx[dir], cy + dy[dir], dir, pt, rem - 1)); n     } else { n       if (pt == n) continue; n       dir1 = (dir + 1) % 8; n       dir2 = (dir + 7) % 8; n       Q.push(ter(cx + dx[dir1], cy + dy[dir1], dir1, pt + 1, t[pt + 1] - 1)); n       Q.push(ter(cx + dx[dir2], cy + dy[dir2], dir2, pt + 1, t[pt + 1] - 1)); n     } n   } n } n int main() { n   std::ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> t[i]; n   Q.push(ter(155, 155, 0, 1, t[1] - 1)); n   radi(); n   cout << sol << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   map<int, int> a; n   for (int i = 0; i < n; i++) { n     int temp; n     cin >> temp; n     a[temp]++; n   } n   map<int, int> b; n   for (int i = 0; i < n - 1; i++) { n     int temp; n     cin >> temp; n     b[temp]++; n   } n   map<int, int> c; n   for (int i = 0; i < n - 2; i++) { n     int temp; n     cin >> temp; n     c[temp]++; n   } n   for (map<int, int>::iterator it = a.begin(); it != a.end(); ++it) { n     if (it->second != b[it->first]) { n       cout << it->first << endl; n       break; n     } n   } n   for (map<int, int>::iterator it = b.begin(); it != b.end(); ++it) { n     if (it->second != c[it->first]) { n       cout << it->first << endl; n       break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   int people_in_queue[n]; n   for (int i = 0; i < n; i++) { n     scanf( %d  , &people_in_queue[i]); n   } n   int num_of_product[n][100], sum[n]; n   for (int i = 0; i < n; i++) { n     sum[i] = 0; n     for (int j = 0; j < people_in_queue[i]; j++) { n       scanf( %d , &num_of_product[i][j]); n       sum[i] = sum[i] + num_of_product[i][j]; n     } n   } n   int time[n]; n   for (int i = 0; i < n; i++) { n     time[i] = sum[i] * 5 + people_in_queue[i] * 15; n   } n   int min = time[0]; n   for (int i = 0; i < n; i++) { n     if (min > time[i]) { n       min = time[i]; n     } n   } n   printf( %d , min); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) {} n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << *it <<   =   << a << endl; n   err(++it, args...); n } n template <typename T> n void printV(const T &t) { n   std::copy(t.cbegin(), t.cend(), n             std::ostream_iterator<typename T::value_type>(std::cout,    )); n   cout <<   n ; n } n template <typename T> n void printVV(const T &t) { n   for (int i = 0; i < t.size(); i++) { n     printf( %d:  , i); n     printV(t[i]); n   } n } n template <typename T, typename S> n void printVP(vector<pair<T, S>> vp) { n   if (vp.empty()) { n     cout <<   n ; n     return; n   } n   for (int i = 0; i < vp.size() - 1; i++) { n     cout <<  (  << vp[i].first <<     << vp[i].second <<  )  ; n   } n   cout <<  (  << vp[vp.size() - 1].first <<     << vp[vp.size() - 1].second n        <<  ) n ; n } n template <typename T> n void printVVP(vector<vector<pair<T, T>>> vvp) { n   for (int i = 0; i < vvp.size(); i++) { n     cout << i <<  :  ; n     printVP(vvp[i]); n   } n } n vector<string> split(const string &s, char delim) { n   vector<string> result; n   stringstream ss(s); n   string item; n   while (getline(ss, item, delim)) { n     result.push_back(item); n   } n   return result; n } n bool is_number(const string &s) { n   string::const_iterator it = s.begin(); n   while (it != s.end() && isdigit(*it)) ++it; n   return !s.empty() && it == s.end(); n } n void fast() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n } n bool isValid(int r, int c, int R, int C) { n   return (r >= 0 && r < R && c >= 0 && c < C); n } n struct Can { n   long long x, r, id; n }; n bool compareByX(const Can &lhs, const Can &rhs) { return lhs.x < rhs.x; } n class Person { n  public: n   string name, speak; n   unordered_set<string> understands; n   friend ostream &operator<<(ostream &os, const Person &p) { n     os << p.name <<  (  << p.speak <<  ): ; n     for (auto s : p.understands) os <<     << s; n     return os; n   } n }; n vector<pair<int, int>> deltas = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; n const int INF = 1e9 + 10; n const long long inf = 1e18 + 10; n const long double PI = 3.14159265359; n int main() { n   int N; n   cin >> N; n   vector<int> A(N), B(N); n   for (int i = 0; i < (N); ++i) { n     cin >> A[i]; n     B[i] = A[i]; n   } n   sort(B.begin(), B.end()); n   unordered_map<int, int> m; n   for (int i = 0; i < (N); ++i) m[B[i]] = i; n   vector<int> C(N); n   for (int i = 0; i < (N); ++i) { n     C[i] = B[(m[A[i]] + 1) % N]; n   } n   printV(C); n } n


#include <bits/stdc++.h> n using namespace std; n const int ms = 100010; n vector<int> g[ms]; n int in[ms], out[ms]; n int main() { n   int n, m; n   int a, b; n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     scanf( %d%d , &a, &b); n     if (a > b) swap(a, b); n     in[a]++, out[b]++; n     g[a].push_back(b); n   } n   long long res = 0; n   for (int i = 0; i < n; ++i) { n     res += 1ll * in[i + 1] * out[i + 1]; n   } n   printf( %lld n , res); n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     scanf( %d , &a); n     res -= 1ll * out[a] * in[a]; n     for (int j : g[a]) { n       res -= 1ll * out[j] * in[j]; n       g[j].push_back(a); n       out[a]++; n       in[j]++; n       out[j]--; n       res += 1ll * out[j] * in[j]; n     } n     g[a].clear(); n     in[a] = 0; n     printf( %lld n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<int, vector<int>, greater<int> > p; n bool visited[100005]; n vector<int> G[100005]; n int n, m; n void prime() { n   p.push(1); n   while (!p.empty()) { n     int v = p.top(); n     p.pop(); n     if (visited[v]) continue; n     visited[v] = true; n     printf( %d  , v); n     for (int i = 0; i < G[v].size(); i++) { n       if (!visited[G[v][i]]) p.push(G[v][i]); n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   int x, y; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &x, &y); n     G[x].push_back(y); n     G[y].push_back(x); n   } n   memset(visited, false, sizeof(visited)); n   prime(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005, P = 998244353; n using ll = long long; n ll qpow(ll a, ll b) { n   ll ret = 1; n   while (b) { n     if (b & 1) ret = ret * a % P; n     a = a * a % P; n     b >>= 1; n   } n   return ret; n } n int n, a, b; n ll pw[N * N], pw1[N * N]; n ll dp[N][N], f[N], g[N], h[N]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> a >> b; n   ll p = a * qpow(b, P - 2) % P; n   if (n == 1) { n     cout << 0 << endl; n     return 0; n   } n   if (n == 2) { n     cout << 1 << endl; n     return 0; n   } n   pw[0] = pw1[0] = 1; n   for (int i = 1; i <= n * n; i++) { n     pw[i] = pw[i - 1] * p % P; n     pw1[i] = pw1[i - 1] * (P - p + 1) % P; n   } n   dp[0][0] = 1; n   for (int i = 1; i <= n; i++) { n     dp[i][0] = 1; n     for (int j = 1; j <= i; j++) n       dp[i][j] = (dp[i - 1][j] * pw1[j] + dp[i - 1][j - 1] * pw[i - j]) % P; n   } n   for (int i = 1; i <= n; i++) { n     f[i] = 1; n     for (int j = 1; j < i; j++) f[i] = (f[i] + P - f[j] * dp[i][j] % P) % P; n   } n   for (int i = 3; i <= n; i++) { n     ll tt = i * (i - 1) / 2; n     for (int j = 1; j < i; j++) { n       ll tmp = (g[j] + h[i - j]) * f[j] % P * dp[i][j] % P; n       tt = (tt + tmp) % P; n     } n     g[i] = tt * qpow(P + 1 - f[i], P - 2) % P; n     tt = f[i] * g[i] % P; n     for (int j = 1; j < i; j++) { n       ll tmp = (g[j] + h[i - j]) * f[j] % P * dp[i][j] % P; n       tt = (tt + tmp) % P; n     } n     h[i] = tt; n   } n   cout << g[n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 132674; n const int MOD = 1e9 + 7; n int main() { n   set<int> ar; n   set<int> to_buy; n   set<int> to_sell; n   long long multiplyier = 1; n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int temp; n     string l; n     cin >> l; n     scanf( %d , &temp); n     if (l.size() == 3) { n       if (to_buy.size() > 0 && *to_buy.rbegin() > temp) { n         to_buy.insert(temp); n       } else if (to_sell.size() > 0 && *to_sell.begin() < temp) { n         to_sell.insert(temp); n       } else n         ar.insert(temp); n     } else { n       if (ar.find(temp) != ar.end()) { n         for (auto u : ar) { n           if (u != temp) { n             if (u < temp) n               to_buy.insert(u); n             else n               to_sell.insert(u); n           } n         } n         ar.clear(); n         multiplyier = (multiplyier * 2) % MOD; n       } else if (to_buy.find(temp) != to_buy.end()) { n         if (*to_buy.rbegin() != temp) { n           cout << 0; n           return 0; n         } n         to_buy.erase(temp); n         for (auto u : ar) to_sell.insert(u); n         ar.clear(); n       } else if (to_sell.find(temp) != to_sell.end()) { n         if (*to_sell.begin() != temp) { n           cout << 0; n           return 0; n         } n         to_sell.erase(temp); n         for (auto u : ar) to_buy.insert(u); n         ar.clear(); n       } n     } n   } n   cout << (multiplyier * ((int)ar.size() + 1)) % MOD; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2010, M = N * N; n int n, m, dist[N][N], deg[N][N], fx[N][N], fy[N][N]; n char str[N][N]; n pair<int, int> p[M]; n int hh, tt; n bool st[N][N]; n void topsort() { n   hh = 0; n   tt = -1; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) { n       dist[i][j] = 1; n       fx[i][j] = i; n       fy[i][j] = j; n       if (!deg[i][j]) p[++tt] = {i, j}; n     } n   pair<int, int> u; n   int x, y, distance; n   while (hh <= tt) { n     u = p[hh++]; n     x = u.first, y = u.second, distance = dist[x][y]; n     if (str[x][y] ==  U ) n       x--; n     else if (str[x][y] ==  D ) n       x++; n     else if (str[x][y] ==  L ) n       y--; n     else if (str[x][y] ==  R ) n       y++; n     if (x <= 0 || x > n || y <= 0 || y > m) continue; n     deg[x][y]--; n     if (!deg[x][y]) p[++tt] = {x, y}; n     if (dist[x][y] < distance + 1) { n       dist[x][y] = distance + 1; n       fx[x][y] = fx[u.first][u.second]; n       fy[x][y] = fy[u.first][u.second]; n     } n   } n } n void dfs(int x, int y) { n   if (st[x][y]) return; n   p[tt++] = {x, y}; n   st[x][y] = 1; n   if (str[x][y] ==  U ) n     x--; n   else if (str[x][y] ==  D ) n     x++; n   else if (str[x][y] ==  L ) n     y--; n   else if (str[x][y] ==  R ) n     y++; n   dfs(x, y); n } n int main() { n   int T, res, x, y; n   cin >> T; n   while (T--) { n     cin >> n >> m; n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= m; ++j) deg[i][j] = st[i][j] = 0; n     for (int i = 1; i <= n; ++i) { n       scanf( %s , str[i] + 1); n       for (int j = 1; j <= m; ++j) { n         int x = i, y = j; n         if (str[i][j] ==  U ) x--; n         if (str[i][j] ==  D ) x++; n         if (str[i][j] ==  L ) y--; n         if (str[i][j] ==  R ) y++; n         if (x <= 0 || x > n || y <= 0 || y > m) continue; n         deg[x][y]++; n       } n     } n     topsort(); n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= m; ++j) n         if (deg[i][j]) { n           tt = 0; n           dfs(i, j); n           for (int k = 0; k < tt; ++k) { n             int x = p[k].first, y = p[k].second; n             deg[x][y] = 0; n             dist[x][y] += tt - 1; n           } n         } n     res = 0; n     for (int i = 1; i <= n; ++i) n       for (int j = 1; j <= m; ++j) n         if (res < dist[i][j]) { n           res = dist[i][j]; n           x = fx[i][j]; n           y = fy[i][j]; n         } n     cout << x <<     << y <<     << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.1415926536; n int n, m, spaces; n bool a[100005], vis[505][505]; n void walk(int x, int y, string s) { n   int counter = 1; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  U ) { n       if (x > 1) { n         x--; n         if (!vis[x][y]) spaces--, vis[x][y] = 1, a[counter] = 1; n       } n     } else if (s[i] ==  D ) { n       if (x < n) { n         x++; n         if (!vis[x][y]) spaces--, vis[x][y] = 1, a[counter] = 1; n       } n     } else if (s[i] ==  R ) { n       if (y < m) { n         y++; n         if (!vis[x][y]) spaces--, vis[x][y] = 1, a[counter] = 1; n       } n     } else { n       if (y > 1) { n         y--; n         if (!vis[x][y]) spaces--, vis[x][y] = 1, a[counter] = 1; n       } n     } n     counter++; n   } n   if (a[s.size()]) spaces++; n } n int main() { n   int x, y; n   string s; n   cin >> n >> m >> x >> y >> s; n   spaces = n * m - 1; n   vis[x][y] = 1; n   walk(x, y, s); n   cout <<  1  ; n   for (int i = 1; i < s.size(); i++) cout << a[i] <<    ; n   cout << spaces <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (long long)1e12; n const int maxn = 100050; n struct STU { n   int b, c, id; n   bool operator<(const STU &s) const { n     return c < s.c || (c == s.c && id < s.id); n   } n }; n map<STU, int> mp; n bool cmp(const STU &a, const STU &b) { return a.b < b.b; } n struct BUG { n   int a, id; n   bool operator<(const BUG &s) const { return a < s.a; } n }; n STU stu[maxn]; n BUG bug[maxn]; n int N, M, S; n int ans[maxn]; n bool judge(int times) { n   mp.clear(); n   long long pass = 0; n   int p = M - 1, q = N - 1; n   map<STU, int>::iterator it; n   for (; p >= 0; p--) { n     while (q >= 0 && stu[q].b >= bug[p].a) { n       mp[stu[q]] = times; n       q--; n     } n     if (p == M - 1 || it->second <= 0) { n       if (p != M - 1) mp.erase(it); n       it = mp.begin(); n       if (it == mp.end()) return false; n       pass += it->first.c; n     } n     ans[bug[p].id] = it->first.id; n     it->second--; n   } n   return pass <= S; n } n int main() { n   scanf( %d%d%d , &N, &M, &S); n   int max_a = 0; n   for (int i = 0; i < M; i++) n     scanf( %d , &bug[i].a), bug[i].id = i + 1, max_a = max(max_a, bug[i].a); n   for (int i = 0; i < N; i++) scanf( %d , &stu[i].b); n   for (int i = 0; i < N; i++) scanf( %d , &stu[i].c), stu[i].id = i + 1; n   bool ok = false; n   for (int i = 0; i < N; i++) n     if (stu[i].b >= max_a && stu[i].c <= S) ok = true; n   if (!ok) { n     printf( NO n ); n     return 0; n   } else n     printf( YES n ); n   sort(bug, bug + M); n   sort(stu, stu + N, cmp); n   int L = 1, H = M, mid; n   while (L < H) { n     mid = L + (H - L) / 2; n     if (judge(mid)) n       H = mid; n     else n       L = mid + 1; n   } n   judge(L); n   for (int i = 1; i < M; i++) printf( %d  , ans[i]); n   printf( %d n , ans[M]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, i; n     cin >> n; n     string s; n     int ans = 0; n     cin >> s; n     for (i = 1; i < n; i++) { n       if (s[i] != s[0]) break; n     } n     if (i != n) { n       int c = 1; n       for (int j = i + 1; j != i; j = (j + 1) % n) { n         if ((j != 0 && s[j] == s[j - 1]) || (j == 0 && s[0] == s[n - 1])) { n           c++; n         } else { n           while (c >= 3) { n             ans++; n             c -= 3; n           } n           c = 1; n         } n       } n       while (c >= 3) { n         ans++; n         c -= 3; n       } n     } else { n       int c = n - 1; n       ans = 1; n       while (c >= 3) { n         ans++; n         c -= 3; n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, Minus, tmp, pos, lar_pos, ans; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n long long f(long long a, long long b) { n   if (b == 0) n     return 0ll; n   else n     return 1ll + f(a, b - gcd(a, b)); n } n vector<long long> v, base; n int main() { n   scanf( %lld %lld , &a, &b); n   c = a; n   for (long long i = 2; i * i <= c; i++) { n     if (a % i == 0ll) v.push_back(i); n     while (a % i == 0ll) a /= i; n   } n   if (a != 1ll) v.push_back(a); n   for (int i = (0); i < (v.size()); i++) base.push_back(v[i]); n   a = c; n   while (b > 0) { n     Minus = gcd(a, b); n     for (int i = (0); i < (v.size()); i++) n       while (Minus % v[i] == 0) v[i] *= base[i]; n     lar_pos = 0ll; n     for (int i = (0); i < (v.size()); i++) { n       if (a % v[i] != 0ll) continue; n       tmp = lcm(Minus, v[i]); n       pos = b / tmp * tmp; n       lar_pos = max(pos, lar_pos); n     } n     ans += (b - lar_pos) / Minus; n     b = lar_pos; n   } n   printf( %lld n , ans); n   return 0; n } n