#include <bits/stdc++.h> n using namespace std; n const long long N = 2000; n long long calcula(long long l, long long r, long long k) { n   long long d1 = -r; n   long long d2 = l - r; n   long long aum = d2 - d1; n   long long lim1 = d1; n   long long lim2 = d1 + (r * 1000000 - 1) * aum; n   if (lim1 <= k && k <= lim2 && (k - d1) % aum == 0) { n     long long num = (k - d1) / aum + 1; n     return num; n   } n   return -1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.precision(10); n   cout << fixed; n   long long k; n   cin >> k; n   for (long long i = 1; i <= N; i++) { n     for (long long j = 1; j <= N; j++) { n       if (i + j > N) break; n       long long aux = calcula(i, j, k); n       if (aux != -1) { n         cout << i + j << endl; n         for (long long a = 0; a < i - 1; a++) { n           cout << 0 <<    ; n         } n         cout << -1 <<    ; n         for (long long a = 0; a < j; a++) { n           cout << min(aux, 1000000ll) <<    ; n           aux -= min(aux, 1000000ll); n         } n         cout << endl; n         return 0; n       } n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int cakesneeded, timek, k, timeforoven; n   cin >> cakesneeded >> timek >> k >> timeforoven; n   int cyclesneeded = cakesneeded / k; n   if (cakesneeded % k != 0) { n     cyclesneeded++; n   } n   int totaltime = cyclesneeded * timek; n   int partialtime = totaltime - timek; n   if (partialtime > timeforoven) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c, d; n   cin >> a >> b >> c >> d; n   long long w, x, y, z; n   bool f = 1; n   if (a == c && f) { n     w = a + abs(d - b); n     x = b; n     y = a + abs(d - b); n     z = d; n     f = 0; n   } n   if (b == d && f) { n     w = a; n     x = b + abs(a - c); n     y = c; n     z = b + abs(a - c); n     f = 0; n   } else if (f) { n     if (abs(a - c) == abs(b - d)) { n       w = a; n       x = d; n       y = c; n       z = b; n       f = 0; n     } else { n       cout << -1 << endl; n       ; n     } n   } n   if (!f) cout << w <<     << x <<     << y <<     << z << endl; n   ; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   while (b) { n     swap(a %= b, b); n   } n   return a; n } n long long n, m, x, y, a, b, g; n double dist1(long long curstep) { n   long long l1 = a / g * curstep; n   long long l2 = b / g * curstep; n   long long x1 = max(1ll, (x - (l1 - l1 / 2))); n   if (x1 + l1 > n) { n     x1 = n - l1; n   } n   long long x2 = x1 + l1; n   long long y1 = max(1ll, (y - (l2 - l2 / 2))); n   if (y1 + l2 > m) { n     y1 = m - l2; n   } n   long long y2 = y1 + l2; n   double c1 = (x1 + x2 + .0) / 2; n   double c2 = (y1 + y2 + .0) / 2; n   double tempdst = sqrt((c1 - x) * (c1 - x) + (c2 - y) * (c2 - y)); n   return tempdst; n } n int main() { n   cin >> n >> m >> x >> y >> a >> b; n   g = gcd(a, b); n   a /= g; n   b /= g; n   long long step = min(n / a, m / b); n   a *= step; n   b *= step; n   long long l1 = a; n   long long l2 = b; n   long long x1 = max(0ll, (x - (l1 - l1 / 2))); n   if (x1 + l1 > n) { n     x1 = n - l1; n   } n   long long x2 = x1 + l1; n   long long y1 = max(0ll, (y - (l2 - l2 / 2))); n   if (y1 + l2 > m) { n     y1 = m - l2; n   } n   long long y2 = y1 + l2; n   cout << x1 <<    ; n   cout << y1 <<    ; n   cout << x2 <<    ; n   cout << y2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 100; n long long n_r, n_g, n_b, r[maxn], g[maxn], b[maxn], ans; n void cal(long long n_r, long long n_g, long long n_b, long long r[], n          long long g[], long long b[]) { n   long long x, y, z, i, j = 1, k = 1; n   for (i = 1; i <= n_g; ++i) { n     y = g[i]; n     while (r[j] <= y && j <= n_r) ++j; n     if (j == 1) continue; n     --j; n     x = r[j]; n     while (b[k] < y) ++k; n     if (k > n_b) break; n     z = b[k]; n     ans = min(ans, (x - y) * (x - y) + (y - z) * (y - z) + (z - x) * (z - x)); n   } n } n void solve() { n   ans = 0x7fffffffffffffff; n   cal(n_r, n_g, n_b, r, g, b); n   cal(n_r, n_b, n_g, r, b, g); n   cal(n_g, n_r, n_b, g, r, b); n   cal(n_g, n_b, n_r, g, b, r); n   cal(n_b, n_g, n_r, b, g, r); n   cal(n_b, n_r, n_g, b, r, g); n   cout << ans <<   n ; n } n int main() { n   int t, i, j; n   ios::sync_with_stdio(false); n   cin >> t; n   while (t--) { n     cin >> n_r >> n_g >> n_b; n     for (i = 1; i <= n_r; ++i) cin >> r[i]; n     for (i = 1; i <= n_g; ++i) cin >> g[i]; n     for (i = 1; i <= n_b; ++i) cin >> b[i]; n     sort(r + 1, r + 1 + n_r); n     sort(g + 1, g + 1 + n_g); n     sort(b + 1, b + 1 + n_b); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> e[111]; n int used[111], cnt, cnt2; n void dfs(int x) { n   used[x] = 1; n   cnt += e[x].size(); n   cnt2++; n   for (int i = 0; i < e[x].size(); i++) n     if (used[e[x][i]] == 0) dfs(e[x][i]); n } n int main() { n   int i, j, k, n, m, stop = 0; n   scanf( %d%d , &n, &m); n   for (i = 0; i < m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     e[x].push_back(y); n     e[y].push_back(x); n   } n   for (i = 1; i <= n; i++) { n     if (!used[i]) { n       cnt = 0; n       cnt2 = 0; n       dfs(i); n       if (cnt2 % 2 == 1 && cnt2 * 2 == cnt) { n         stop++; n       } n     } n   } n   printf( %d n , stop + (n - stop) % 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n queue<int> qx, qy; n int d[2][110000]; n char s[2][110000]; n int n, k; n int main() { n   gets(s[0]); n   sscanf(s[0],  %d %d , &n, &k); n   gets(s[0]); n   gets(s[1]); n   memset(d, 63, sizeof(d)); n   d[0][0] = 0; n   qx.push(0); n   qy.push(0); n   for (; qx.size();) { n     int x = qx.front(); n     int y = qy.front(); n     qx.pop(); n     qy.pop(); n     if (d[y][x] > x) continue; n     if (x + k >= n) { n       printf( YES n ); n       return 0; n     } n     if (s[y][x + 1] ==  - ) { n       if (d[y][x + 1] > d[y][x] + 1) { n         d[y][x + 1] = d[y][x] + 1; n         qx.push(x + 1); n         qy.push(y); n       } n     } n     if (x > 0 && s[y][x - 1] ==  - ) { n       if (d[y][x - 1] > d[y][x] + 1) { n         d[y][x - 1] = d[y][x] + 1; n         qx.push(x - 1); n         qy.push(y); n       } n     } n     if (s[y ^ 1][x + k] ==  - ) { n       if (d[y ^ 1][x + k] > d[y][x] + 1) { n         d[y ^ 1][x + k] = d[y][x] + 1; n         qx.push(x + k); n         qy.push(y ^ 1); n       } n     } n   } n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n; n int a[200001]; n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n     bool flag = false; n     for (int i = 2; i <= n; i++) n       if (abs(a[i] - a[i - 1]) > 1) { n         flag = true; n         printf( YES n ); n         printf( %d %d n , i - 1, i); n         break; n       } n     if (!flag) printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d n , &n); n   set<int> in, x; n   long totalCost(0), cost(0); n   while (n--) { n     int a, b, c; n     scanf( %d %d %d n , &a, &b, &c); n     totalCost += c; n     if (in.count(a) || x.count(b)) { n       cost += c; n       x.insert(a); n       in.insert(b); n     } else { n       in.insert(a); n       x.insert(b); n     } n   } n   if (totalCost - cost < cost) { n     cost = totalCost - cost; n   } n   printf( %ld n , cost); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> a; n void printall(int x, int y, bool paren = false) { n   if (paren) printf( ( ); n   for (int i = x; i < y; i++) printf( %d-> , a[i]); n   printf( %d , a[y]); n   if (paren) printf( ) ); n } n int main() { n   scanf( %d , &n); n   a.resize(n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   if (a.back() != 0) { n     puts( NO ); n   } else { n     if (n == 1 or a[n - 2] == 1) { n       puts( YES ); n       printall(0, n - 1); n     } else { n       int i; n       for (i = n - 3; i >= 0; i--) n         if (a[i] == 0) break; n       if (i < 0) { n         puts( NO ); n       } else { n         puts( YES ); n         if (i - 1 >= 0) { n           printall(0, i - 1); n           printf( -> ); n         } n         printf( (0-> ); n         printall(i + 1, n - 2, true); n         printf( )->0 ); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n long double PI = acosl(-1); n const long long infl = 1e18; n const int nmax = 1e6 + 5; n const int MAXLG = log2(nmax) + 1; n const int N = 2e4 + 5; n struct Hopcroft_Karp { n   int NIL = 0, INF = (1 << 28), match[N], dist[N], n, m; n   vector<int> G[N]; n   void init(int lft, int rgt) { n     n = lft, rgt = m; n     for (int i = 0; i <= n + m + 1; i++) G[i].clear(); n   } n   void addEdge(int u, int v) { G[u].push_back(v + n); } n   bool bfs() { n     queue<int> Q; n     for (int i = 1; i <= n; i++) { n       if (match[i] == NIL) n         dist[i] = 0, Q.push(i); n       else n         dist[i] = INF; n     } n     dist[NIL] = INF; n     while (!Q.empty()) { n       int u = Q.front(); n       Q.pop(); n       if (u != NIL) { n         for (int i = 0; i < G[u].size(); i++) { n           int v = G[u][i]; n           if (dist[match[v]] == INF) { n             dist[match[v]] = dist[u] + 1; n             Q.push(match[v]); n           } n         } n       } n     } n     return (dist[NIL] != INF); n   } n   bool dfs(int u) { n     if (u != NIL) { n       for (int i = 0; i < G[u].size(); i++) { n         int v = G[u][i]; n         if (dist[match[v]] == dist[u] + 1) { n           if (dfs(match[v])) { n             match[v] = u; n             match[u] = v; n             return true; n           } n         } n       } n       dist[u] = INF; n       return false; n     } n     return true; n   } n   int hopcroft_karp() { n     memset(dist, 0, sizeof dist); n     memset(match, 0, sizeof match); n     int matching = 0; n     while (bfs()) { n       for (int i = 1; i <= n; i++) { n         if (match[i] == NIL && dfs(i)) { n           matching++; n         } n       } n     } n     return matching; n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   Hopcroft_Karp carp; n   int n, m; n   cin >> n >> m; n   vector<pair<int, pair<int, int> > > ejes; n   for (int i = 0; i < m; i++) { n     int u, v, d; n     cin >> u >> v >> d; n     ejes.push_back({d, {u, v}}); n   } n   carp.init(n, n); n   for (pair<int, pair<int, int> > pp : ejes) { n     carp.addEdge(pp.second.first, pp.second.second); n   } n   int z = carp.hopcroft_karp(); n   if (z != n) cout <<  -1 n , exit(0); n   sort(ejes.begin(), ejes.end()); n   int lo = 1, hi = 1000000000; n   while (hi > lo) { n     int mid = (hi + lo) / 2; n     carp.init(n, n); n     for (auto pp : ejes) { n       if (pp.first > mid) break; n       carp.addEdge(pp.second.first, pp.second.second); n     } n     if (carp.hopcroft_karp() == n) n       hi = mid; n     else n       lo = mid + 1; n   } n   cout << lo << endl; n } n


#include <bits/stdc++.h> n using namespace std; n double max(double a, double b) { return (a > b ? a : b); } n int main() { n   std::ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   double A[n][n]; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       cin >> A[i][j]; n     } n   } n   vector<vector<double> > DP(n, vector<double>(1 << n, 0)); n   DP[0][0] = 1; n   for (int i = 1; i < n; ++i) { n     DP[i][1 << (n - 1)] = A[0][i]; n   } n   double mom = 0; n   for (int k = 1; k < n; ++k) { n     for (int i = 0; i < (1 << n); ++i) { n       if (__builtin_popcount(i) != k) { n         continue; n       } n       for (int player = 0; player < n; ++player) { n         if (i & (1 << player)) { n           continue; n         } n         double mx = 0; n         DP[player][i] = 0; n         for (int enemy = 0; enemy < n; ++enemy) { n           mx = 0; n           if (i & (1 << enemy)) { n             mx += A[player][enemy] * DP[player][i - (1 << enemy)]; n             mx += A[enemy][player] * DP[enemy][i - (1 << enemy)]; n           } n           DP[player][i] = max(mx, DP[player][i]); n         } n         if (k == n - 1) { n           mom = max(DP[player][i], mom); n         } n       } n     } n   } n   cout << (n == 1 ? 1 : mom) <<   n ; n } n


#include <bits/stdc++.h> n int MakeTB(unsigned int *A, unsigned int *B, unsigned int *T, unsigned int N, n            unsigned int StartBit) { n   for (unsigned int i = 0; i < N - 1; i++) n     if (A[i] == 0) n       if (B[i] == 0) { n         if (i > 0 && T[i] != 0) return 0; n         T[i] = 0; n         T[i + 1] = 0; n       } else n         return 0; n     else if (B[i] == 0) { n       if (i == 0) T[i] = StartBit; n       T[i + 1] = !T[i]; n     } else { n       if (i > 0 && T[i] != 1) return 0; n       T[i] = 1; n       T[i + 1] = 1; n     } n   return 1; n } n int MakeTWB(unsigned int *A, unsigned int *B, unsigned int *T, unsigned int N) { n   unsigned int *a, *b, *t1, *t2; n   a = new unsigned int[N - 1]; n   b = new unsigned int[N - 1]; n   t1 = new unsigned int[N]; n   t2 = new unsigned int[N]; n   for (unsigned int i = 0; i < N - 1; i++) { n     a[i] = A[i] & 1; n     b[i] = B[i] & 1; n   } n   if (!(MakeTB(a, b, t1, N, 0) || MakeTB(a, b, t1, N, 1))) return 0; n   for (unsigned int i = 0; i < N - 1; i++) { n     a[i] = A[i] >> 1; n     b[i] = B[i] >> 1; n   } n   if (!(MakeTB(a, b, t2, N, 0) || MakeTB(a, b, t2, N, 1))) return 0; n   for (unsigned int i = 0; i < N; i++) T[i] = t1[i] | (t2[i] << 1); n   return 1; n } n int main(void) { n   unsigned int N; n   unsigned int *a, *b, *t; n   scanf( %d , &N); n   a = new unsigned int[N - 1]; n   b = new unsigned int[N - 1]; n   t = new unsigned int[N]; n   for (unsigned int i = 0; i < N - 1; i++) scanf( %d , &a[i]); n   for (unsigned int i = 0; i < N - 1; i++) scanf( %d , &b[i]); n   if (MakeTWB(a, b, t, N)) { n     printf( YES n ); n     for (unsigned int i = 0; i < N; i++) printf( %i  , t[i]); n   } else n     printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> mp, x[1000005]; n int main() { n   int t1; n   cin >> t1; n   while (t1--) { n     string s, t; n     cin >> s >> t; n     mp.clear(); n     for (int i = 0; i < s.size(); i++) mp[s[i]]++; n     bool q = true; n     for (int i = 0; i < t.size(); i++) { n       if (mp[t[i]] == 0) { n         q = false; n         break; n       } n     } n     if (q == false) { n       cout << -1 << endl; n       continue; n     } n     for (int j = 100005; j >= 1; j--) x[j].clear(); n     for (int i = s.size() - 1; i >= 0; i--) { n       for (int j = 1; j <= 26; j++) x[i + 1][j] = x[i + 2][j]; n       x[i + 1][s[i] -  a  + 1] = i + 1; n     } n     int in = 0, ans = 0; n     while (in < t.size()) { n       ans++; n       int ind = 0; n       while (true) { n         if (x[ind + 1][t[in] -  a  + 1] == 0) break; n         ind = x[ind + 1][t[in] -  a  + 1]; n         in++; n         if (in >= t.size()) break; n       } n       if (in >= t.size()) break; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[10001]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m, k; n   cin >> n >> m >> k; n   long long int x1 = 1, x2 = 1; n   int p = k; n   if (k % 2 == 0) { n     k -= 1; n   } n   int x = 1, y = 1, res = 0; n   for (int j = 1; j <= m; j++) { n     for (int i = 1; i <= n; i++) { n       a[i] = x; n       if (a[i] == k) { n         cout << i <<     << j <<    ; n         p % 2 == 0 ? cout <<  R  : cout <<  L ; n         return 0; n       } n       x += m * 2; n     } n     x = y + 2 * j; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e6 + 10; n int n, a[MAXN], d[MAXN]; n bool pic[MAXN]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int temp; n     scanf( %d , &temp); n     pic[temp] = 1; n   } n   fill(d, d + MAXN, 1); n   int ans = 0; n   for (int i = 1; i < MAXN; i++) { n     if (pic[i]) ans = max(ans, d[i]); n     if (pic[i]) n       for (int j = i + i; j < MAXN; j += i) d[j] = max(d[j], d[i] + 1); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n struct BIT { n   int n; n   vector<int> bit; n   BIT(int _n = 0) { n     n = _n; n     bit.assign(n + 5, 0); n   } n   void update(int pos, int val) { n     for (; pos <= n; pos += pos & -pos) bit[pos] += val; n   } n   int getsum(int pos) { n     int res = 0; n     for (; pos; pos -= pos & -pos) res += bit[pos]; n     return res; n   } n }; n BIT bit[3]; n set<int> pos[3]; n int a[N]; n int n, q; n int fix(char c) { n   if (c ==  R ) return 0; n   if (c ==  S ) return 1; n   return 2; n } n int getans() { n   int res = 0; n   for (int x = 0; x < 3; x++) n     if (pos[x].size()) { n       int y = (x + 1) % 3; n       int z = (x + 2) % 3; n       if (!pos[y].size()) n         res += n * (!pos[z].size()); n       else { n         res += pos[x].size(); n         if (pos[z].size()) { n           int l = *pos[y].begin(); n           int r = *prev(pos[y].end()); n           int u = *pos[z].begin(); n           int v = *prev(pos[z].end()); n           if (r < v) res -= bit[x].getsum(v) - bit[x].getsum(r - 1); n           if (u < l) res -= bit[x].getsum(l) - bit[x].getsum(u - 1); n         } n       } n     } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> q; n   for (int i = 0; i < 3; i++) bit[i] = BIT(n); n   for (int i = 1; i <= n; i++) { n     char c; n     cin >> c; n     a[i] = fix(c); n     pos[a[i]].insert(i); n     bit[a[i]].update(i, 1); n   } n   cout << getans() <<   n ; n   while (q--) { n     int i; n     char c; n     cin >> i >> c; n     pos[a[i]].erase(i); n     bit[a[i]].update(i, -1); n     a[i] = fix(c); n     pos[a[i]].insert(i); n     bit[a[i]].update(i, 1); n     cout << getans() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ss { n   long long sur, x, y, z; n   ss() {} n   ss(long long a, long long b, long long c, long long d) { n     sur = a; n     x = b; n     y = c; n     z = d; n   } n } ans; n int n, tot, a[100], b[100]; n long long x, uu, p[100], u[200000]; n long long calc(long long a, long long b, long long c) { n   return a * b + a * c + b * c; n } n long long getsqr(long long a) { n   long long b = (long long)sqrt(a) + 1; n   while (b * b > a) --b; n   return b; n } n long long gettri(long long a) { n   long long b = (long long)pow(a, 1.0 / 3) + 1; n   while (b * b * b > a) --b; n   return b; n } n void dfs(int now, long long lim, long long xx) { n   if (!now) { n     u[++tot] = xx; n     return; n   } n   dfs(now - 1, lim, xx); n   for (int i = 1; i <= a[now]; ++i) { n     xx *= p[now]; n     if (xx > lim) break; n     dfs(now - 1, lim, xx); n   } n } n void dfs2(int now, long long lim, long long xx) { n   if (!now) { n     if (calc(uu, xx, x / xx / uu) < ans.sur) n       ans = ss(calc(uu, xx, x / xx / uu), uu, xx, x / xx / uu); n     return; n   } n   dfs2(now - 1, lim, xx); n   for (int i = 1; i <= a[now]; ++i) { n     xx *= p[now]; n     if (xx > lim) break; n     dfs2(now - 1, lim, xx); n   } n } n bool cmp(long long a, long long b) { return a > b; } n int main() { n   int T; n   cin >> T; n   for (int test = 1; test <= T; ++test) { n     cin >> n; n     x = 1; n     for (int i = 1; i <= n; ++i) { n       cin >> p[i] >> a[i]; n       for (int j = 1; j <= a[i]; ++j) x *= p[i]; n     } n     memcpy(b, a, sizeof(a)); n     tot = 0; n     dfs(n, gettri(x), 1LL); n     sort(u + 1, u + tot + 1, cmp); n     ans = ss(calc(x, 1, 1), x, 1, 1); n     for (int i = 1; i <= tot; ++i) { n       long long vw = x / u[i], v = getsqr(vw), w = u[i]; n       uu = u[i]; n       if (calc(u[i], v, v) > ans.sur) continue; n       for (int j = 1; j <= n; ++j) n         while (w % p[j] == 0) { n           w /= p[j]; n           --a[j]; n         } n       dfs2(n, getsqr(vw), 1LL); n       memcpy(a, b, sizeof(b)); n     } n     cout << ans.sur * 2 <<     << ans.x <<     << ans.y <<     << ans.z << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int depth[200005], dad[200005]; n int dep = 0; n void node_depth(vector<int> vec[], int i) { n   dep = depth[i] + 1; n   for (auto j : vec[i - 1]) { n     if (j != dad[i]) { n       depth[j] = dep; n     } n   } n   for (auto j : vec[i - 1]) { n     if (j != dad[i]) { n       node_depth(vec, j); n     } n   } n } n int entry[200005], exiit[200005]; n int timer = 0; n void ancestor(vector<int> vec[], int i) { n   entry[i] = timer; n   timer++; n   for (auto j : vec[i - 1]) { n     if (j != dad[i]) { n       dad[j] = i; n       ancestor(vec, j); n     } n   } n   exiit[i] = timer; n   timer++; n } n bool isParent(int a, int b) { n   return (entry[a] <= entry[b] && exiit[a] >= exiit[b]); n } n signed idiot() { n   depth[1] = 0; n   int n, m; n   cin >> n >> m; n   vector<int> edge[n]; n   dad[1] = 1; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     edge[u - 1].push_back(v); n     edge[v - 1].push_back(u); n   } n   ancestor(edge, 1); n   node_depth(edge, 1); n   while (m--) { n     int k; n     cin >> k; n     int temp = 1; n     vector<int> ve; n     while (k--) { n       int vi; n       cin >> vi; n       if (depth[vi] > depth[temp]) { n         temp = vi; n       } n       ve.push_back(vi); n     } n     bool path = true; n     for (auto i : ve) { n       if (!isParent(dad[i], temp)) { n         path = false; n         break; n       } n     } n     cout << (path ?  YES  :  NO ) <<   n ; n   } n   return 0; n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int test_case = 1; n   while (test_case--) idiot(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 7, maxm = 35 * maxn; n int nxt[maxm][2], tot, a[maxn]; n long long ans = 0; n void ins(int x) { n   int now = 0; n   for (int i = 29; i >= 0; i--) { n     int go = (x >> i) & 1; n     if (nxt[now][go] == 0) nxt[now][go] = ++tot; n     now = nxt[now][go]; n   } n } n long long query(int x) { n   long long ret = 0; n   int now = 0; n   for (int i = 29; i >= 0; i--) { n     int go = (x >> i) & 1; n     if (nxt[now][go]) n       now = nxt[now][go]; n     else n       now = nxt[now][go ^ 1], ret |= (1 << i); n   } n   return ret; n } n void dfs(int l, int r, int dep) { n   if (dep == -1 || l >= r) return; n   int mid = l - 1; n   while (mid < r && (a[mid + 1] >> dep & 1) == 0) mid++; n   dfs(l, mid, dep - 1); n   dfs(mid + 1, r, dep - 1); n   if (mid == l - 1 || mid == r) return; n   for (int i = l; i <= mid; i++) ins(a[i]); n   long long ret = 1e18; n   for (int i = mid + 1; i <= r; i++) ret = min(ret, query(a[i])); n   ans += ret; n   for (int i = 0; i <= tot; i++) nxt[i][0] = nxt[i][1] = 0; n   tot = 0; n } n int n; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n   dfs(1, n, 29); n   return 0 * printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<long long int> v(n); n   for (long long int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   sort((v).begin(), (v).end()); n   set<long long> s; n   if (n == 1) { n     cout << -1; n   } else if (n == 2) { n     s.insert(v[1] + (v[1] - v[0])); n     s.insert(v[0] - (v[1] - v[0])); n     if ((v[1] - v[0]) % 2 == 0) { n       s.insert(v[0] + (v[1] - v[0]) / 2); n     } n     cout << s.size() <<   n ; n     for (auto i : s) { n       cout << i <<    ; n     } n   } else { n     long long d = INT_MAX; n     for (long long i = 0; i < n - 1; i++) { n       d = min(d, v[i + 1] - v[i]); n     } n     bool missed = false; n     bool noap = false; n     for (long long i = 0; i < n - 1; i++) { n       if (v[i + 1] == v[i] + d) n         continue; n       else { n         if (v[i + 1] == v[i] + (d * 2)) { n           if (missed == false) { n             missed = true; n             s.insert(v[i] + d); n           } else { n             noap = true; n             break; n           } n         } else { n           noap = true; n           break; n         } n       } n     } n     if (noap) { n       cout << 0; n     } else if (missed) { n       cout << 1 <<   n ; n       cout << (*s.begin()); n     } else { n       s.insert(v[0] - d); n       s.insert(v[n - 1] + d); n       cout << s.size() <<   n ; n       for (auto i : s) { n         cout << i <<    ; n       } n     } n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n template <class T> n bool scanff(T &ret) { n   char c; n   int sgn; n   T bit = 0.1; n   if (c = getchar(), c == EOF) return 0; n   while (c !=  -  && c !=  .  && (c <  0  || c >  9 )) c = getchar(); n   sgn = (c ==  - ) ? -1 : 1; n   ret = (c ==  - ) ? 0 : (c -  0 ); n   while (c = getchar(), c >=  0  && c <=  9 ) ret = ret * 10 + (c -  0 ); n   if (c ==     || c ==   n ) { n     ret *= sgn; n     return 1; n   } n   while (c = getchar(), c >=  0  && c <=  9 ) ret += (c -  0 ) * bit, bit /= 10; n   ret *= sgn; n   return 1; n } n using namespace std; n long long dp[55][55][55]; n int a[55]; n int n, sum; n double ans; n int main() { n   ans = 0.0; n   scanff(n); n   int tot = 0; n   for (int i = int(1); i <= int(n); i++) scanff(a[i]), tot += a[i]; n   scanff(sum); n   if (tot <= sum) { n     printf( %.10f n , n * 1.0); n     return 0; n   } n   for (int c = int(1); c <= int(n); c++) { n     memset(dp, 0, sizeof(dp)); n     dp[0][0][0] = 1; n     for (int i = int(1); i <= int(n); i++) { n       for (int j = int(0); j <= int(i - 1); j++) n         for (int k = int(0); k <= int(sum); k++) { n           if (dp[i - 1][j][k]) { n             dp[i][j][k] += dp[i - 1][j][k]; n             if (i != c && k + a[i] <= sum) n               dp[i][j + 1][k + a[i]] += dp[i - 1][j][k]; n           } n         } n     } n     for (int j = int(1); j <= int(n); j++) { n       for (int k = int(max(0, sum - a[c] + 1)); k <= int(50); k++) { n         if (dp[n][j][k]) { n           double temp = dp[n][j][k] * j; n           int num = n - j; n           int n1 = n - j, n2 = n; n           for (int l = int(1); l <= int(num); l++) { n             temp *= double(n1); n             temp /= double(n2); n             n1--; n             n2--; n           } n           temp /= double(n - j); n           ans += temp; n         } n       } n     } n   } n   printf( %.10f n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 200010; n int par[M]; n int sz[M]; n struct edge { n   int u, v, w; n   bool operator<(const edge &e) const { return w < e.w; } n }; n edge E[M]; n pair<int, int> q[M]; n long long out[M]; n int f(int u) { return par[u] = par[u] == u ? u : f(par[u]); } n int add(int u, int v) { n   u = f(u); n   v = f(v); n   if (u == v) return 0; n   par[v] = u; n   sz[u] += sz[v]; n   return sz[u]; n } n long long get(int n) { n   if (n <= 0) return 0; n   return ((long long)n * (long long)(n - 1)) / 2; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n - 1; i++) { n     cin >> E[i].u >> E[i].v >> E[i].w; n   } n   for (int i = 0; i < m; i++) { n     cin >> q[i].first; n     q[i].second = i; n   } n   sort(E, E + n - 1); n   sort(q, q + m); n   for (int i = 1; i <= n; i++) par[i] = i, sz[i] = 1; n   long long ans = 0; n   int j = 0; n   for (int i = 0; i < m; i++) { n     while (j < n - 1) { n       if (E[j].w > q[i].first) break; n       int u = f(E[j].u), v = f(E[j].v); n       ans -= (get(sz[u]) + get(sz[v])); n       ans += get(add(u, v)); n       j++; n     } n     out[q[i].second] = ans; n   } n   for (int i = 0; i < m; i++) cout << out[i] <<    ; n   cout << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b; n   cin >> a >> b; n   if (a == b) n     cout << a << 0 <<     << b << 1 << endl; n   else if (a + 1 == b) { n     cout << a << 9 <<     << b << 0 << endl; n   } else if (a == 9 && b == 1) { n     cout <<  9 10  << endl; n   } else { n     cout << -1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100123; n const int lgn = 20; n long long a[maxn], f[maxn][lgn], ans; n vector<int> G[maxn]; n void dfs(int u, int p, int x) { n   int w = a[u] >> x & 1; n   f[u][w] = 1, f[u][w ^ 1] = 0; n   for (auto& v : G[u]) { n     if (v != p) { n       dfs(v, u, x); n       ans += (1 << x) * (f[u][1] * f[v][0] + f[u][0] * f[v][1]); n       f[u][w] += f[v][0]; n       f[u][w ^ 1] += f[v][1]; n     } n   } n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < (n); ++i) cin >> a[i], ans += a[i]; n   for (int i = 0; i < (n - 1); ++i) { n     int u, v; n     cin >> u >> v; n     --u, --v; n     G[u].push_back(v); n     G[v].push_back(u); n   } n   for (int i = 0; i < (lgn); ++i) dfs(0, -1, i); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int ans; n char s[100]; n int main() { n   cin >> s; n   for (int i = 0; i < strlen(s); i++) n     if (s[i] ==  a  || s[i] ==  e  || s[i] ==  i  || s[i] ==  o  || n         s[i] ==  u  || s[i] ==  1  || s[i] ==  3  || s[i] ==  5  || n         s[i] ==  7  || s[i] ==  9 ) n       ans++; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int test, i, k, j, sum1, sum, n, p; n   cin >> test; n   for (i = 0; i < test; i++) { n     cin >> n >> k; n     sum = 0; n     sum1 = 0; n     vector<int> v1, v2; n     for (j = 0; j < n; j++) { n       cin >> p; n       v1.push_back(p); n     } n     for (j = 0; j < n; j++) { n       cin >> p; n       v2.push_back(p); n     } n     sort(v1.begin(), v1.end()); n     sort(v2.begin(), v2.end()); n     for (j = 0; j < k; j++) { n       if (v2[n - j - 1] > v1[j]) n         sum1 = sum1 + v2[n - j - 1]; n       else n         sum1 = sum1 + v1[j]; n     } n     for (j = k; j < n; j++) sum = sum + v1[j]; n     cout << sum + sum1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int query(int l, int r) { n   if (l < 1 || r > n) return false; n   string re; n   cout << 1 <<     << l <<     << r << endl; n   cin >> re; n   return re ==  TAK ; n } n int get(int l, int r) { n   if (l > r) return false; n   while (l < r) { n     int mid = (l + r) >> 1; n     if (query(mid, mid + 1)) n       r = mid; n     else n       l = mid + 1; n   } n   return l; n } n int main() { n   cin >> n >> k; n   int x, y; n   x = get(1, n); n   y = get(1, x - 1); n   if (!query(y, x)) y = get(x + 1, n); n   cout << 2 <<     << x <<     << y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0), eps = 1e-4; n double fgcd(double a, double b) { n   return fabs(a) < eps ? b : (fabs(b) < eps ? a : fgcd(b, fmod(a, b))); n } n int main() { n   double a, b, c, d, e, f; n   cin >> a >> b >> c >> d >> e >> f; n   c -= a; n   e -= a; n   d -= b; n   f -= b; n   a = (c * c + d * d) / 2; n   b = (e * e + f * f) / 2; n   double ov = 1.0 / (d * e - c * f); n   double A = (d * b - f * a) * ov, B = (e * a - c * b) * ov; n   double R = sqrt(A * A + B * B), G1 = asin(sqrt(a * 2) / (R * 2)), n          G2 = asin(sqrt(b * 2) / (R * 2)), n          G = asin(hypot(c - e, d - f) / (R * 2)); n   G = fgcd(G1, fgcd(G2, pi)); n   G = pi / round(pi / G); n   A = round(pi / G) * R * R * sin(G * 2) / 2; n   printf( %.8lf n , A); n } n


#include <bits/stdc++.h> n using namespace std; n struct Q { n   int l; n   int r; n   int num; n   Q(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {} n }; n int cnt[100100]; n int res[100100]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<int> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   vector<int> comp = a; n   sort(comp.begin(), comp.end()); n   comp.resize(unique(comp.begin(), comp.end()) - comp.begin()); n   for (int i = 0; i < n; i++) { n     a[i] = lower_bound(comp.begin(), comp.end(), a[i]) - comp.begin(); n   } n   int K = sqrt(n); n   int B = n / K + 1; n   vector<vector<Q>> blocks(B); n   for (int i = 0; i < m; i++) { n     int l, r; n     cin >> l >> r; n     l--; n     r--; n     blocks[l / K].emplace_back(l, r, i); n   } n   for (int b = 0; b < B; b++) { n     sort(blocks[b].begin(), blocks[b].end(), n          [&](Q x, Q y) { return x.r < y.r; }); n   } n   for (int b = 0; b < B; b++) { n     if (blocks[b].empty()) continue; n     memset(cnt, 0, sizeof(cnt)); n     int ans = 0; n     for (int i = blocks[b][0].l; i <= blocks[b][0].r; i++) { n       if (cnt[a[i]] == comp[a[i]]) { n         ans--; n       } n       cnt[a[i]]++; n       if (cnt[a[i]] == comp[a[i]]) { n         ans++; n       } n     } n     int pl = blocks[b][0].l; n     int pr = blocks[b][0].r; n     res[blocks[b][0].num] = ans; n     for (int q = 1; q < (int)blocks[b].size(); q++) { n       for (int i = pr + 1; i <= blocks[b][q].r; i++) { n         if (cnt[a[i]] == comp[a[i]]) { n           ans--; n         } n         cnt[a[i]]++; n         if (cnt[a[i]] == comp[a[i]]) { n           ans++; n         } n       } n       pr = blocks[b][q].r; n       for (int i = blocks[b][q].l; i < pl; i++) { n         if (cnt[a[i]] == comp[a[i]]) { n           ans--; n         } n         cnt[a[i]]++; n         if (cnt[a[i]] == comp[a[i]]) { n           ans++; n         } n       } n       for (int i = pl; i < blocks[b][q].l; i++) { n         if (cnt[a[i]] == comp[a[i]]) { n           ans--; n         } n         cnt[a[i]]--; n         if (cnt[a[i]] == comp[a[i]]) { n           ans++; n         } n       } n       pl = blocks[b][q].l; n       res[blocks[b][q].num] = ans; n     } n   } n   for (int i = 0; i < m; i++) { n     cout << res[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 105000; n const int INF = 2100000000; n struct target { n   int mi_n[2], ma_x[2], z, n; n }; n struct point { n   int min, n, dot, d[2], fa, l, r; n }; n int n, m, root, now, txl, txr, tyl, tyr, ans[MAXN] = {0}; n target rec[MAXN]; n point t[MAXN]; n void input_data() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d%d%d , &rec[i].mi_n[0], &rec[i].ma_x[0], &rec[i].mi_n[1], n           &rec[i].ma_x[1], &rec[i].z); n     rec[i].n = i; n   } n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &t[i].d[0], &t[i].d[1]); n     t[i].n = i; n   } n } n bool cmp_1(point a, point b) { return a.d[now] < b.d[now]; } n void gengxin(int father, int son) { n   if (t[father].min > t[son].min) { n     t[father].min = t[son].min; n     t[father].dot = t[son].dot; n   } n } n void up_data(int rt) { n   t[rt].min = t[rt].n; n   t[rt].dot = rt; n   if (t[rt].n == 0) { n     t[rt].min = INF; n     t[rt].dot = 0; n   } n   int l = t[rt].l, r = t[rt].r; n   if (l) gengxin(rt, l); n   if (r) gengxin(rt, r); n } n int build(int begin, int end, int fa, int fx) { n   int m = (begin + end) >> 1; n   now = fx; n   nth_element(t + begin, t + m, t + end + 1, cmp_1); n   t[m].fa = fa; n   if (begin < m) t[m].l = build(begin, m - 1, m, 1 - fx); n   if (m < end) t[m].r = build(m + 1, end, m, 1 - fx); n   up_data(m); n   return m; n } n bool inrange(int x, target b, int fx) { n   return ((b.mi_n[fx] <= x) && (x <= b.ma_x[fx])); n } n int query(int rt, int fx, int r, int xl, int xr, int yl, int yr) { n   if (!rt) return 0; n   if (!t[rt].dot) return 0; n   if (txl <= xl && xr <= txr && tyl <= yl && yr <= tyr) return t[rt].dot; n   int t1 = 0, t2 = 0; n   if (inrange(t[rt].d[fx], rec[r], fx)) { n     if (fx == 0) { n       t1 = query(t[rt].l, 1 - fx, r, xl, t[rt].d[0], yl, yr); n       t2 = query(t[rt].r, 1 - fx, r, t[rt].d[0], xr, yl, yr); n     } else { n       t1 = query(t[rt].l, 1 - fx, r, xl, xr, yl, t[rt].d[1]); n       t2 = query(t[rt].r, 1 - fx, r, xl, xr, t[rt].d[1], yr); n     } n   } else { n     if (t[rt].d[fx] < rec[r].mi_n[fx]) { n       if (fx == 0) n         return query(t[rt].r, 1 - fx, r, t[rt].d[0], xr, yl, yr); n       else n         return query(t[rt].r, 1 - fx, r, xl, xr, t[rt].d[1], yr); n     } n     if (rec[r].ma_x[fx] < t[rt].d[fx]) { n       if (fx == 0) n         return query(t[rt].l, 1 - fx, r, xl, t[rt].d[0], yl, yr); n       else n         return query(t[rt].l, 1 - fx, r, xl, xr, yl, t[rt].d[1]); n     } n   } n   int fi = 0; n   int temp = INF; n   if (t1 != 0) n     if (t[t1].n < temp) { n       temp = t[t1].n; n       fi = t1; n     } n   if (t2 != 0) n     if (t[t2].n < temp) { n       temp = t[t2].n; n       fi = t2; n     } n   if (t[rt].n != 0 && t[rt].n < temp) n     if (inrange(t[rt].d[0], rec[r], 0) && inrange(t[rt].d[1], rec[r], 1)) { n       temp = t[rt].n; n       fi = rt; n     } n   return fi; n } n void adjust(int rt) { n   up_data(rt); n   if (rt != root) adjust(t[rt].fa); n } n bool cmp_2(target a, target b) { return a.z < b.z; } n void get_ans() { n   root = build(1, m, 0, 0); n   sort(rec + 1, rec + 1 + n, cmp_2); n   for (int i = 1; i <= n; i++) { n     txl = rec[i].mi_n[0], txr = rec[i].ma_x[0], tyl = rec[i].mi_n[1], n     tyr = rec[i].ma_x[1]; n     int hit = query(root, 0, i, 0, INF, 0, INF); n     if (hit != 0) { n       ans[t[hit].n] = rec[i].n; n       t[hit].n = 0; n       adjust(hit); n     } n   } n } n void output_ans() { n   for (int i = 1; i <= m; i++) printf( %d n , ans[i]); n } n int main() { n   input_data(); n   get_ans(); n   output_ans(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q, ans; n map<int, int> boss[100010]; n map<pair<int, int>, int> mp; n vector<pair<int, int> > vt[100010]; n int findf(int x, int col) { n   if (!boss[col].count(x) || x == boss[col][x]) { n     boss[col][x] = x; n     return x; n   } n   return boss[col][x] = findf(boss[col][x], col); n } n void dsu(int x, int y, int col) { n   x = findf(x, col); n   y = findf(y, col); n   if (x != y) { n     boss[col][y] = x; n   } n   return; n } n int main() { n   int i, j, a, b, c; n   cin >> n >> m; n   for (i = 0; i < m; i++) { n     cin >> a >> b >> c; n     a--, b--, c--; n     dsu(a, b, c); n   } n   for (i = 0; i < m; i++) { n     for (map<int, int>::iterator it = boss[i].begin(); it != boss[i].end(); n          it++) { n       a = it->first; n       b = it->second; n       vt[a].push_back(make_pair(i, b)); n     } n   } n   for (i = 0; i < n; i++) { n     for (j = 0; j < vt[i].size(); j++) { n       vt[i][j].second = findf(i, vt[i][j].first); n     } n   } n   cin >> q; n   for (i = 0; i < q; i++) { n     cin >> a >> b; n     a--, b--; n     if (vt[a].size() >= vt[b].size()) { n       swap(a, b); n     } n     if (mp.count(make_pair(a, b))) { n       cout << mp[make_pair(a, b)] << endl; n       continue; n     } n     ans = 0; n     for (j = 0; j < vt[a].size(); j++) { n       if (binary_search(vt[b].begin(), vt[b].end(), vt[a][j])) { n         ans++; n       } n     } n     mp[make_pair(a, b)] = ans; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n template <typename T> n using PQ = priority_queue<T>; n template <typename T> n using GPQ = priority_queue<T, vector<T>, greater<T>>; n template <class L, class R> n ostream& operator<<(ostream& os, pair<L, R> p); n template <class T> n ostream& operator<<(ostream& os, vector<T> v) { n   os <<  [ ; n   for (auto vv : v) os << vv <<  , ; n   return os <<  ] ; n } n template <class T> n ostream& operator<<(ostream& os, set<T> v) { n   os <<  [ ; n   for (auto vv : v) os << vv <<  , ; n   return os <<  ] ; n } n template <class L, class R> n ostream& operator<<(ostream& os, pair<L, R> p) { n   return os <<  (  << p.first <<  ,  << p.second <<  ) ; n } n template <typename T> n inline T sq(T a) { n   return a * a; n } n template <typename T> n T gcd(T a, T b) { n   if (a > b) return gcd(b, a); n   return a == 0 ? b : gcd(b % a, a); n } n template <typename T, typename U> n T mypow(T b, U n) { n   if (n == 0) return 1; n   if (n == 1) return b; n   if (n % 2 == 0) { n     return mypow(b * b, n / 2); n   } else { n     return mypow(b, n - 1) * b; n   } n } n ll pcnt(ll b) { return __builtin_popcountll(b); } n template <typename T> n T iceil(T n, T d) { n   return (n + d - 1) / d; n } n map<char, int> hand = {{ R , 0}, { S , 1}, { P , 2}}; n class BIT { n  public: n   explicit BIT(int N, int v) : V_NUM(N) { n     data.resize(N); n     fill(data.begin(), data.end(), v); n   } n   ll query(int i) { n     ll ret = 0; n     while (i > 0) { n       ret += data[i]; n       i -= (i & -i); n     } n     return ret; n   } n   void update(int i, int v) { n     while (i < V_NUM) { n       data[i] += v; n       i += (i & -i); n     } n   } n   int_fast32_t V_NUM; n   vector<int> data; n }; n int main() { n   int N, Q; n   string S; n   cin >> N >> Q >> S; n   S =  $  + S; n   vector<BIT> bit(3, BIT(N + 1, 0)); n   vector<set<int>> pos(3); n   for (int i = 1; i <= N; ++i) { n     bit[hand[S[i]]].update(i, 1); n     pos[hand[S[i]]].insert(i); n   } n   for (int q = 0; q <= Q; ++q) { n     int lose = 0; n     int emp = 0; n     for (int h = 0; h < 3; ++h) { n       if (pos[h].empty()) ++emp; n     } n     if (emp == 1) { n       for (int h = 0; h < 3; ++h) { n         if (pos[h].empty() || pos[(h + 1) % 3].empty()) continue; n         lose = pos[(h + 1) % 3].size(); n       } n     } else if (emp == 0) { n       for (int h = 0; h < 3; ++h) { n         int w = (h + 1) % 3, l = (h + 2) % 3; n         int wpos = *(pos[w].begin()), lpos = *(pos[l].begin()); n         lose += max(0LL, bit[h].query(wpos) - bit[h].query(lpos)); n         wpos = *(--pos[w].end()), lpos = *(--pos[l].end()); n         lose += max(0LL, bit[h].query(lpos) - bit[h].query(wpos)); n       } n     } n     cout << N - lose <<   n ; n     if (q == Q) break; n     int p; n     char c; n     cin >> p >> c; n     bit[hand[S[p]]].update(p, -1); n     pos[hand[S[p]]].erase(p); n     bit[hand[c]].update(p, 1); n     pos[hand[c]].insert(p); n     S[p] = c; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int n = s.length(); n   vector<vector<int> > pos( z  -  a  + 1); n   for (int i = 0; i < n; i++) pos[s[i] -  a ].push_back(i); n   int good = 0; n   for (int c = 0; c <=  z  -  a ; c++) { n     if (pos[c].empty()) continue; n     int c_good = 0; n     for (int k = 0; k < n; k++) { n       int cnt[ z  -  a  + 1] = {}; n       for (int i : pos[c]) cnt[s[(i + k) % n] -  a ]++; n       int cur_c_good = 0; n       for (int d = 0; d <=  z  -  a ; d++) cur_c_good += (cnt[d] == 1); n       c_good = max(c_good, cur_c_good); n     } n     good += c_good; n   } n   cout.precision(10); n   cout << fixed; n   cout << (double)good / n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const double PI = acos(-1.0); n const int MAXN = 2e5 + 5; n long long int freq[MAXN]; n long long int pref_sum(int l, int r) { n   return (l ? freq[r] - freq[l - 1] : freq[r]); n } n void cp() { n   int n; n   long long int k; n   cin >> n >> k; n   vector<long long int> arr(n); n   memset(freq, 0, sizeof(freq)); n   for (int i = 0; i < n; i++) cin >> arr[i], freq[arr[i]]++; n   for (int i = 1; i < MAXN; i++) freq[i] += freq[i - 1]; n   sort(arr.begin(), arr.end()); n   int need = arr[0]; n   int ans = 0; n   long long int cost = 0; n   for (int i = 2e5; i >= need; i--) { n     long long int cnt = pref_sum(i + 1, MAXN - 1); n     long long int total = cost + cnt; n     if (total > k) { n       ans++; n       cost = cnt; n     } else n       cost += cnt; n   } n   if (cost) { n     ans++; n   } n   cout << ans <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t; n   t = 1; n   while (t--) { n     cp(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 35005; n struct segtree { n   segtree *left, *right; n   int start, end, v, m; n   int lazy; n   segtree(int start, int end) : start(start), end(end), v(0), lazy(0) { n     if (start == end) { n       left = right = NULL; n     } else { n       m = (start + end) / 2; n       left = new segtree(start, m); n       right = new segtree(m + 1, end); n     } n   } n   void propagate() { n     if (lazy) { n       v += lazy; n       if (left != NULL) left->lazy += lazy; n       if (right != NULL) right->lazy += lazy; n       lazy = 0; n     } n   } n   void update(int a, int b, int val) { n     if (b < a) return; n     propagate(); n     if (a == start && b == end) { n       lazy += val; n       propagate(); n     } else { n       if (b <= m) n         left->update(a, b, val); n       else if (a > m) n         right->update(a, b, val); n       else n         left->update(a, m, val), right->update(m + 1, b, val); n       left->propagate(); n       right->propagate(); n       v = max(left->v, right->v); n     } n   } n   int query(int a, int b) { n     if (b < a) return 1; n     propagate(); n     if (a == start && b == end) return v; n     if (b <= m) return left->query(a, b); n     if (a > m) return right->query(a, b); n     return max(left->query(a, m), right->query(m + 1, b)); n   } n }; n int dp[MAX][51]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   vector<int> v(n + 1); n   for (int i = 1; i <= n; i++) { n     cin >> v[i]; n   } n   set<int> diff; n   for (int i = 1; i <= n; i++) { n     diff.insert(v[i]); n     dp[i][1] = diff.size(); n   } n   for (int i = 2; i <= k; i++) { n     segtree cur(1, n); n     for (int j = 1; j <= n; j++) { n       cur.update(j, j, dp[j][i - 1]); n     } n     vector<int> last(n + 1, 1); n     for (int j = 1; j <= n; j++) { n       cur.update(last[v[j]], j - 1, 1); n       dp[j][i] = cur.query(1, j - 1); n       last[v[j]] = j; n     } n   } n   cout << dp[n][k] <<   n ; n } n


#include <cstdio> n #include <cstring> n #include <algorithm> n #define rep(a,b,c) for(int a=b;a<=c;a++) n #define per(a,b,c) for(int a=b;a>=c;a--) n #define int long long n const int inf=0x3f3f3f3f; n template<class T>inline void read(T &x) { n  T f=1;x=0;char s=getchar(); n  while(s< 0 ||s> 9 ){if(s== - )f=-1;s=getchar();} n  while(s>= 0 &&s<= 9 ){x=x*10+s- 0 ;s=getchar();} n  x*=f; n } n const int N=1e6+1; n struct Node { n  int i,j,val; n }a[N]; n int n,du[N],idx; n char s[10]; n inline bool cmp(Node a,Node b) { return a.val==b.val?a.j<b.j:a.val>b.val; } n inline void solve() { n  read(n); n  rep(i,1,n) read(du[i]); n  rep(i,1,n-1) rep(j,i+1,n) { n   if(du[i]>du[j]) a[++idx].i=i,a[idx].j=j,a[idx].val=du[i]-du[j]; n   else a[++idx].i=j,a[idx].j=i,a[idx].val=du[j]-du[i]; n  } n  std::sort(a+1,a+idx+1,cmp); n  rep(i,1,idx) { n   printf( ? %lld %lld n ,a[i].i,a[i].j); n   fflush(stdout); n   scanf( %s ,s+1); n   if(s[1]== Y ) { n    printf( ! %lld %lld n ,a[i].i,a[i].j); n    fflush(stdout); n    return; n   } n  } n  puts( ! 0 0 ); n  fflush(stdout); n } n signed main() { n  //int TEST; read(TEST); while(TEST--) n  solve(); n }


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t, a, b, diff, n; n   cin >> t; n   while (t--) { n     cin >> a >> b; n     diff = abs(a - b); n     n = ceil((-1 + sqrt(1 + 8 * diff)) / 2); n     if (((n + 1) / 2) % 2 != diff % 2) { n       if (n % 2) n         n += 2; n       else n         n++; n     } n     if (n == -0) n = 0; n     cout << n <<   n ; n   } n } n


#include <bits/stdc++.h> n template <typename T, typename U> n static inline void amin(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n static inline void amax(T &x, U y) { n   if (x < y) x = y; n } n using namespace std; n const int N = 2e5 + 5; n int par[N], r[N]; n int find(int a) { n   if (par[a] < 0) n     return a; n   else n     return (par[a] = find(par[a])); n } n void merge(int a, int b) { n   a = find(a); n   b = find(b); n   if (a == b) return; n   if (r[a] > r[b]) { n     par[b] = a; n     r[a] += r[b]; n   } else { n     par[a] = b; n     r[b] += r[a]; n   } n } n void solve() { n   int n, m, i, j; n   cin >> n >> m; n   for (i = 1; i < n + 1; i++) par[i] = -1, r[i] = 1; n   for (i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     merge(a, b); n   } n   long long int ans = 1; n   for (i = 1; i < n + 1; i++) { n     if (r[i] > 1 && par[i] < 0) ans = ans * pow(2, r[i] - 1); n   } n   cout << ans; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n struct Solution { n   int n, k; n   std::vector<std::vector<int>> as; n   std::vector<int> pows; n   int64_t calcPair(int ind1, int ind2) { n     std::vector<int> left; n     left.reserve(as[ind1].size()); n     for (int v : as[ind1]) { n       left.push_back((int64_t(k - v) * pows[ind2 + 1]) % k); n     } n     std::sort(left.begin(), left.end()); n     int64_t result = 0; n     size_t i1 = 0; n     size_t j1 = 0; n     std::vector<int>& right = as[ind2]; n     while (i1 < left.size() && j1 < right.size()) { n       if (left[i1] < right[j1]) { n         i1++; n         continue; n       } n       if (left[i1] > right[j1]) { n         j1++; n         continue; n       } n       size_t i2 = i1; n       while (i2 < left.size() && left[i2] == left[i1]) i2++; n       size_t j2 = j1; n       while (j2 < right.size() && right[j2] == right[j1]) j2++; n       result += (i2 - i1) * int64_t(j2 - j1); n       i1 = i2; n       j1 = j2; n     } n     if (ind1 == ind2) { n       for (int v : right) { n         if ((v + int64_t(v) * pows[ind2 + 1]) % k == 0) { n           result--; n         } n       } n     } n     return result; n   } n   void run(std::istream& in, std::ostream& out) { n     in >> n >> k; n     as.assign(10, std::vector<int>()); n     pows.resize(11); n     pows[0] = 1; n     for (int i = 1; i <= 10; i++) { n       pows[i] = (pows[i - 1] * int64_t(10)) % k; n     } n     for (int i = 0; i < n; i++) { n       int a; n       in >> a; n       int aa = a % k; n       int len = 1; n       while (a >= 10) { n         a /= 10; n         len++; n       } n       as[len - 1].push_back(aa); n     } n     for (size_t i = 0; i < as.size(); i++) { n       std::sort(as[i].begin(), as[i].end()); n     } n     int64_t ans = 0; n     for (int i = 0; i < 10; i++) { n       for (int j = 0; j < 10; j++) { n         ans += calcPair(i, j); n       } n     } n     out << ans <<   n ; n   } n }; n int main() { n   std::cin.sync_with_stdio(false); n   std::cin.tie(nullptr); n   Solution().run(std::cin, std::cout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int pai[1000010]; n int find(int x) { n   if (pai[x] == x) return x; n   return pai[x] = find(pai[x]); n } n void join(int a, int b) { pai[find(a)] = find(b); } n int main() { n   int n, m, q; n   cin >> n >> m >> q; n   for (int i = 1; i <= n + m; i++) pai[i] = i; n   for (int i = 0; i < q; i++) { n     int a, b; n     cin >> a >> b; n     b += n; n     join(a, b); n   } n   int comps = 0; n   for (int i = 1; i <= n + m; i++) comps += pai[i] == i; n   cout << comps - 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   long long k; n   cin >> n; n   cin >> k; n   vector<long long> d(n); n   vector<long long> afterd(n); n   for (long long i = 0; i < n; i++) cin >> d[i]; n   for (long long i = 0; i < n; i++) cin >> afterd[i]; n   vector<pair<long long, long long> > utility(n); n   for (long long i = 0; i < n; i++) { n     utility[i].first = d[i] - afterd[i]; n     utility[i].second = i; n   } n   sort(utility.begin(), utility.end()); n   set<long long> u2; n   long long p = 0; n   while (k > 0 || utility[p].first < 0) { n     u2.insert(utility[p].second); n     p += 1; n     k -= 1; n   } n   long long cost = 0; n   for (long long i = 0; i < n; i++) { n     if (u2.find(i) != u2.end()) n       cost += d[i]; n     else n       cost += afterd[i]; n   } n   cout << cost; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10005; n const int P = 998244353; n struct edge { n   int u, v, len; n   inline edge() {} n   inline edge(int _u, int _v, int _len) { n     u = _u; n     v = _v; n     len = _len; n   } n } a[N], Q[N], res[N]; n struct node { n   int l, r; n   vector<int> E; n   node *lson, *rson; n } pool[3 * N], *root; n int n, m; n int OscarDaGumert; n int top = -1; n int Index, cnt; n bitset<N> cur; n bitset<N> ans; n void B_T(node *now, int l, int r) { n   now->l = l; n   now->r = r; n   if (l == r) return; n   int lr = (l + r) >> 1, rl = lr + 1; n   now->lson = &pool[++top]; n   B_T(now->lson, l, lr); n   now->rson = &pool[++top]; n   B_T(now->rson, rl, r); n } n void change(node *now, int l, int r, int id) { n   if (now->l == l && now->r == r) { n     now->E.push_back(a[id].len); n     return; n   } n   int lr = (now->l + now->r) >> 1, rl = lr + 1; n   if (r < rl) n     change(now->lson, l, r, id); n   else if (l > lr) n     change(now->rson, l, r, id); n   else { n     change(now->lson, l, lr, id); n     change(now->rson, rl, r, id); n   } n } n inline void query(node *now) { n   bitset<N> tmp = cur; n   int len = now->E.size(); n   for (int i = 0; i < len; i++) { n     cur |= (cur << now->E[i]); n   } n   ans |= cur; n   if (now->l != now->r) { n     query(now->lson); n     query(now->rson); n   } n   cur = tmp; n } n void init() { n   int x1, x2, x3; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &x1, &x2, &x3); n     a[i] = edge(x1, x2, x3); n   } n   root = &pool[++top]; n   B_T(root, 1, n); n   for (int i = 1; i <= m; i++) change(root, a[i].u, a[i].v, i); n   cur[0] = 1; n   query(root); n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (ans[i] == 1) { n       cnt++; n     } n   } n   printf( %d n , cnt); n   for (int i = 1; i <= n; i++) { n     if (ans[i] == 1) { n       printf( %d  , i); n     } n   } n } n int main(int argc, char **argv) { n   init(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 9; n const long long INF = 1e18; n const int MAXN = 1e6 + 10; n const int MAXM = 1e4 + 500; n const int N = 2000 + 15; n const double EPS = 1e-9; n int dx[] = {1, 0, 0, -1}; n int dy[] = {0, 1, -1, 0}; n double len(complex<long long> a) { n   return sqrt(a.real() * a.real() + a.imag() * a.imag()); n } n long long sq(complex<long long> a) { n   return a.real() * a.real() + a.imag() * a.imag(); n } n complex<long long> perp(complex<long long> p) { return {-p.imag(), p.real()}; } n long long dotProduct(complex<long long> a, complex<long long> b) { n   return (conj(a) * b).real(); n } n long long crossProduct(complex<long long> a, complex<long long> b) { n   return (conj(a) * b).imag(); n } n long long orient(complex<long long> a, complex<long long> b, n                  complex<long long> c) { n   return crossProduct(b - a, c - a); n } n bool inDisk(complex<long long> a, complex<long long> b, complex<long long> p) { n   return dotProduct(a - p, b - p) <= 0; n } n bool onSegment(complex<long long> a, complex<long long> b, n                complex<long long> p) { n   return orient(a, b, p) == 0 && inDisk(a, b, p); n }; n struct cmpX { n   bool operator()(complex<long long> a, complex<long long> b) { n     return make_pair(a.real(), a.imag()) < make_pair(b.real(), b.imag()); n   } n }; n struct line { n   complex<long long> v; n   long long c; n   line(complex<long long> v, long long c) : v(v), c(c) {} n   line(long long a, long long b, long long c) : v({b, -a}), c(c) {} n   line(complex<long long> p, complex<long long> q) n       : v(q - p), c(crossProduct(v, p)) {} n   long long side(complex<long long> p) { return crossProduct(v, p) - c; } n   double dist(complex<long long> p) { return abs(side(p)) / len(v); } n   line perpThrough(complex<long long> p) { return {p, p + perp(v)}; } n   bool cmpProj(complex<long long> p, complex<long long> q) { n     return dotProduct(v, p) < dotProduct(v, q); n   } n   line translate(complex<long long> t) { return {v, c + crossProduct(v, t)}; } n   complex<long long> proj(complex<long long> p) { n     return p - perp(v) * side(p) / sq(v); n   } n }; n double segPoint(complex<long long> a, complex<long long> b, n                 complex<long long> p) { n   if (a != b) { n     line l(a, b); n     if (l.cmpProj(a, p) && l.cmpProj(p, b)) return l.dist(p); n   } n   return min(abs(p - a), abs(p - b)); n } n bool inAngle(complex<long long> a, complex<long long> b, complex<long long> c, n              complex<long long> p) { n   if (orient(a, b, c) < 0) swap(b, c); n   return orient(a, b, p) >= 0 && orient(a, c, p) <= 0; n } n bool isInsidePol(vector<complex<long long> > &a, complex<long long> b) { n   int n = a.size(); n   int pos = 0, neg = 0; n   for (int i = 0; i < n - 1; i++) { n     if (onSegment(a[i], a[(i + 1) % n], b)) return false; n     line l(a[i], a[i + 1]); n     if (l.side(b) > 0) n       pos++; n     else n       neg++; n   } n   line l(a[n - 1], a[0]); n   if (l.side(b) > 0) n     pos++; n   else n     neg++; n   return ((!pos && neg == n) || (!neg && pos == n)); n } n long long i, j, k; n long long n, m, t; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   string w; n   cin >> w; n   stack<int> kek; n   kek.push(0); n   for (auto e : w) { n     e == kek.top() ? kek.pop() : kek.push(e); n   } n   cout << (kek.top() ?  No  :  Yes ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int p[55]; n string t; n void getp() { n   int j = 0; n   p[0] = p[1] = 0; n   for (int i = 1; i < t.length(); ++i) { n     while (j && t[j] != t[i]) j = p[j]; n     if (t[j] == t[i]) j++; n     p[i + 1] = j; n   } n } n void build() { n   getp(); n   string ans = t; n   int cnt = 1; n   while (cnt < k) { n     for (int i = p[t.length()]; i < t.length(); ++i) ans.append(1, t[i]); n     cnt++; n   } n   cout << ans; n } n int main() { n   cin >> n >> k >> t; n   build(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, u, r; n vector<long long> k; n vector<int> a, b, p, tmp; n vector<vector<int> > v; n vector<int> w; n void read(); n int main() { n   ios_base::sync_with_stdio(false); n   read(); n   v.push_back(a); n   w.push_back(1); n   long long s = 0; n   if (u & 1) n     s = -1e15; n   else { n     for (int i = 0; i < n; ++i) s += (a[i] * k[i]); n   } n   int uu = (u < 29 ? u : u - 2); n   for (int i = 0; i < uu; ++i) { n     int lim = v.size(); n     for (int j = 0; j < lim; ++j) { n       if (w[j]) { n         v.push_back(v[j]); n         w.push_back(0); n         for (int h = 0; h < n; ++h) v.back()[h] ^= b[h]; n         for (int h = 0; h < n; ++h) tmp[h] = v[j][p[h]] + r; n         swap(v[j], tmp); n       } else { n         w[j] = 1; n         for (int h = 0; h < n; ++h) tmp[h] = v[j][p[h]] + r; n         swap(v[j], tmp); n       } n     } n     if ((u - i) & 1) { n       for (int i = 0; i < v.size(); ++i) { n         long long ss = 0; n         for (int j = 0; j < n; ++j) ss += (v[i][j] * k[j]); n         s = max(s, ss); n       } n     } n   } n   if (uu < u) { n     for (int i = 0; i < v.size(); ++i) { n       long long ss = 0; n       for (int j = 0; j < n; ++j) tmp[j] = (v[i][p[j]] ^ b[p[j]]) + r; n       for (int j = 0; j < n; ++j) ss += (tmp[j] * k[j]); n       s = max(s, ss); n       ss = 0; n       for (int j = 0; j < n; ++j) tmp[j] = ((v[i][p[j]] + r) ^ b[j]); n       for (int j = 0; j < n; ++j) ss += (tmp[j] * k[j]); n       s = max(s, ss); n       ss = 0; n       for (int j = 0; j < n; ++j) tmp[j] = v[i][p[p[j]]] + r + r; n       for (int j = 0; j < n; ++j) ss += (tmp[j] * k[j]); n       s = max(s, ss); n     } n   } n   cout << s << endl; n   return 0; n } n void read() { n   cin >> n >> u >> r; n   a.resize(n); n   b.resize(n); n   k.resize(n); n   p.resize(n); n   tmp.resize(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int i = 0; i < n; ++i) cin >> b[i]; n   for (int i = 0; i < n; ++i) cin >> k[i]; n   for (int i = 0; i < n; ++i) cin >> p[i], --p[i]; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 2005; n int d[maxN][maxN], c[maxN][maxN], D[maxN], C[maxN], dx[maxN][maxN], n     cx[maxN][maxN]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   char cur; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) { n       cin >> cur; n       d[i][j] = d[i][j - 1] + (cur ==  W ); n       c[i][j] = c[i - 1][j] + (cur ==  W ); n     } n   for (int i = 1; i <= n; ++i) { n     D[i] = D[i - 1] + (d[i][n] == n); n     C[i] = C[i - 1] + (c[n][i] == n); n   } n   for (int i = 1; i <= n; ++i) n     for (int j = k; j <= n; ++j) n       dx[i][j] = dx[i - 1][j] + (d[i][n] - d[i][j] + d[i][j - k] == n - k); n   for (int i = k; i <= n; ++i) n     for (int j = 1; j <= n; ++j) n       cx[i][j] = cx[i][j - 1] + (c[n][j] - c[i][j] + c[i - k][j] == n - k); n   int ans = 0; n   for (int i = k; i <= n; ++i) n     for (int j = k; j <= n; ++j) n       ans = max(ans, D[n] - D[i] + D[i - k] + dx[i][j] - dx[i - k][j] + C[n] - n                          C[j] + C[j - k] + cx[i][j] - cx[i][j - k]); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[500010], x, sz; n long long ans; n int main() { n   scanf( %d , &n); n   for (int step = 1; step <= n; step++) { n     scanf( %d , &x); n     while (a[sz - 1] > a[sz] && a[sz] <= x && sz >= 2) { n       ans += min(a[sz - 1], x); n       sz--; n     } n     a[++sz] = x; n   } n   sort(a + 1, a + sz + 1); n   for (int i = 1; i <= sz - 2; i++) ans += a[i]; n   printf( %I64d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> v; n vector<int> ans_lis; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     v.emplace_back(x); n   } n   sort(v.begin(), v.end()); n   for (int i = (int)v.size() - 1; i >= 0; i--) { n     if (i % 2 == 0) { n       ans_lis.insert(ans_lis.begin(), v[i]); n     } else n       ans_lis.emplace_back(v[i]); n   } n   bool flag = true; n   if (ans_lis[0] >= ans_lis.back() + ans_lis[1]) flag = false; n   if (ans_lis.back() >= ans_lis[0] + ans_lis[(int)ans_lis.size() - 2]) n     flag = false; n   for (int i = 1; i + 1 < (int)ans_lis.size() && flag; i++) { n     if (ans_lis[i] >= ans_lis[i + 1] + ans_lis[i - 1]) flag = false; n   } n   if (!flag) n     cout <<  NO  << endl; n   else { n     cout <<  YES  << endl; n     for (int i = 0; i < (int)ans_lis.size(); i++) cout << ans_lis[i] <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n long long mx, mi; n int t, head[N], len, k, vis[N]; n struct edge { n   int v, w, next; n } e[N * 2]; n void add(int u, int v, int w) { n   e[len].v = v; n   e[len].w = w; n   e[len].next = head[u]; n   head[u] = len++; n } n void dfs(int u, int fa) { n   vis[u] = 1; n   for (int j = head[u]; j; j = e[j].next) { n     int v = e[j].v; n     int w = e[j].w; n     if (v == fa) continue; n     dfs(v, u); n     if (vis[v] & 1) { n       mi += w; n     } n     mx += (long long)min(k - vis[v], vis[v]) * w; n     vis[u] += vis[v]; n   } n } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &k); n     int u, v, w; n     k <<= 1; n     len = 1; n     memset(head, 0, sizeof(head)); n     memset(vis, 0, sizeof(vis)); n     for (int i = 1; i < k; i++) { n       scanf( %d%d%d , &u, &v, &w); n       add(u, v, w); n       add(v, u, w); n     } n     mi = 0, mx = 0; n     dfs(1, 0); n     printf( %lld %lld n , mi, mx); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, cnt = 0; n long long sum = 0; n bool flag = false; n struct Node { n   int num; n   int pos; n } a[200005]; n bool cmp(const Node &a, const Node &b) { return a.num < b.num; } n vector<int> v; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].num; n     a[i].pos = i; n     sum += a[i].num; n   } n   sort(a + 1, a + n + 1, cmp); n   for (int i = 1; i <= n - 1; i++) { n     if (sum - a[i].num == 2 * a[n].num) { n       cnt++; n       v.push_back(a[i].pos); n     } n   } n   if (sum - a[n].num == 2 * a[n - 1].num) { n     v.push_back(a[n].pos); n     cnt++; n   } n   if (cnt == 0) { n     cout << 0 << endl; n     return 0; n   } n   cout << cnt << endl; n   for (int i = 0; i < v.size(); i++) { n     cout << v[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string itosm(long long x) { n   if (x == 0) return  0 ; n   string ans =   ; n   while (x > 0) { n     ans += ((x % 10) +  0 ); n     x /= 10; n   } n   reverse(ans.begin(), ans.end()); n   return ans; n } n long long stoim(string str) { n   long long ans = 0; n   long long k = 1; n   for (int i = str.length() - 1; i >= 0; i--) { n     ans += (str[i] -  0 ) * k; n     k *= 10; n   } n   return ans; n } n const long long infll = 1e18 + 3; n const int inf = 1009000999; n const double eps = 1e-6; n const int maxn = 1e6 + 77; n const int baseint = 1000200013; n const long long basell = 1e18 + 3; n const long double PI = acos(-1.0); n int main() { n   srand(228228); n   ios_base::sync_with_stdio(0); n   ; n   string t, s, p =   ; n   cin >> t >> s; n   int n = s.length(); n   while (p.length() != n) p +=  0 ; n   int cnt = 0; n   for (int i = (0); i < (n); i++) { n     if (s[i] == t[i]) n       p[i] = s[i]; n     else n       cnt++; n   } n   if (cnt % 2) { n     cout <<  impossible ; n     return 0; n   } n   cnt = 0; n   for (int i = (0); i < (n); i++) { n     if (s[i] != t[i]) { n       if (cnt <= 0) { n         p[i] = s[i]; n         cnt++; n       } else { n         p[i] = t[i]; n         cnt--; n       } n     } n   } n   cout << p; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n string name, host, sour; n int main() { n   ios::sync_with_stdio(false); n   cin >> s; n   int i = 0; n   while (i < s.size() && s[i] !=  @ ) { n     name.push_back(s[i]); n     i++; n   } n   i++; n   while (i < s.size() && s[i] !=  / ) { n     host.push_back(s[i]); n     i++; n   } n   bool exist = 0; n   if (s[i] ==  / ) exist = 1; n   i++; n   if (i != s.size() + 1) n     while (i < s.size()) { n       sour.push_back(s[i]); n       i++; n     } n   if (sour.size() > 16 || name.size() == 0 || name.size() > 16 || n       host.size() > 32 || host.size() == 0) { n     cout <<  NO n ; n     return 0; n   } n   if (exist && sour.size() == 0) { n     cout <<  NO n ; n     return 0; n   } n   for (int j = 0; j < name.size(); j++) { n     char c = name[j]; n     if (!isdigit(c) && !isalpha(c) && (c !=  <  && c !=  >  && c !=  _ )) { n       cout <<  NO n ; n       return 0; n     } n   } n   int cnt = 0; n   for (int j = 0; j < host.size(); j++) { n     char c = host[j]; n     if (!isdigit(c) && !isalpha(c) && n         (c !=  <  && c !=  >  && c !=  .  && c !=  _ )) { n       cout <<  NO n ; n       return 0; n     } n     if (c ==  . ) { n       if (cnt == 0) { n         cout <<  NO n ; n         return 0; n       } n       cnt = -1; n     } n     cnt++; n     if (cnt > 16) { n       cout <<  NO n ; n       return 0; n     } n   } n   if (cnt == 0) { n     cout <<  NO n ; n     return 0; n   } n   for (int j = 0; j < sour.size(); j++) { n     char c = sour[j]; n     if (!isalpha(c) && !isdigit(c) && (c !=  <  && c !=  >  && c !=  _ )) { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int min(int a, int b) { n   if (a < b) n     return a; n   else n     return b; n } n int main() { n   int n, len, a[100010], f[100010], dai, max = 0, vitri = 0; n   char s[100010]; n   char si[13][13]; n   bool kt; n   scanf( %s , s); n   len = strlen(s); n   for (int i = 0; i < len; i++) f[i] = 0; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %s , si[i]); n   for (int i = 0; i < len; i++) { n     dai = 100; n     for (int j = 1; j <= n; j++) { n       kt = true; n       for (int k = 0; k < strlen(si[j]); k++) n         if (si[j][k] != s[i + k]) { n           kt = false; n           break; n         } n       if (kt && strlen(si[j]) < dai) dai = strlen(si[j]); n     } n     a[i] = dai; n   } n   f[len] = 0; n   for (int i = len - 1; i >= 0; i--) { n     if (a[i] != 100) n       f[i] = min(f[i + 1] + 1, a[i] - 1); n     else n       f[i] = f[i + 1] + 1; n     if (f[i] > max) { n       max = f[i]; n       vitri = i; n     } n   } n   printf( %d %d , max, vitri); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void chmax(T &x, T y) { n   x = x > y ? x : y; n } n template <class T> n void chmin(T &x, T y) { n   x = x < y ? x : y; n } n const int maxn = 205; n int n, k; n int d[maxn], ans[maxn], choice[maxn]; n int dp[maxn][maxn]; n int dist[maxn][maxn]; n vector<int> v[maxn]; n void predfs(int pos, int fa, int *dis) { n   for (int i = 0; i < (int)(v[pos]).size(); i++) { n     int to = v[pos][i]; n     if (to == fa) continue; n     dis[to] = dis[pos] + 1; n     predfs(to, pos, dis); n   } n } n void dfs(int pos, int fa) { n   for (int i = 1; i <= n; i++) dp[pos][i] = d[dist[pos][i]] + k; n   for (int i = 0; i < (int)(v[pos]).size(); i++) { n     int to = v[pos][i]; n     if (to == fa) continue; n     dfs(to, pos); n     for (int j = 1; j <= n; j++) n       dp[pos][j] += min(dp[to][choice[to]], dp[to][j] - k); n   } n   choice[pos] = min_element(dp[pos] + 1, dp[pos] + n + 1) - dp[pos]; n   return; n } n void print(int pos, int fa, int j) { n   for (int i = 0, _j; i < (int)(v[pos]).size(); i++) { n     int to = v[pos][i]; n     if (to == fa) continue; n     if (dp[to][choice[to]] < dp[to][j] - k) { n       _j = choice[to]; n     } else { n       _j = j; n     } n     ans[to] = _j; n     print(to, pos, _j); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n - 1; i++) cin >> d[i]; n   for (int i = 1; i <= n - 1; i++) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) predfs(i, 0, dist[i]); n   dfs(1, 0); n   cout << dp[1][choice[1]] << endl; n   print(1, 0, choice[1]); n   ans[1] = choice[1]; n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, y; n int a, b; n int diff; n void ask(int mask) { n   int sz = 0; n   for (int i = 0; i < n; ++i) { n     if ((i & mask) > 0) { n       ++sz; n     } n   } n   if (sz == 0) { n     return; n   } n   printf( ? %d , sz); n   for (int i = 0; i < n; ++i) { n     if ((i & mask) > 0) { n       printf(  %d , i + 1); n     } n   } n   printf(  n ); n   fflush(stdout); n   int ans = 0; n   scanf( %d , &ans); n   if (ans != 0 && ans != x) { n     diff += mask; n   } n } n vector<int> v; n bool f(int sr) { n   printf( ? %d , sr + 1); n   for (int i = 0; i <= sr; ++i) { n     printf(  %d , v[i] + 1); n   } n   printf(  n ); n   fflush(stdout); n   int ans; n   scanf( %d , &ans); n   if (ans != 0 && ans != x) { n     return true; n   } n   return false; n } n void input() { scanf( %d%d%d , &n, &x, &y); } n void solve() { n   for (int i = 0; i < 10; ++i) { n     ask((1 << i)); n   } n   for (int i = 0; i < n; ++i) { n     if ((i ^ diff) < i) { n       v.push_back(i); n     } n   } n   int l, r, mid; n   l = 0; n   r = v.size() - 1; n   while (l != r) { n     int mid = (l + r) / 2; n     if (f(mid) == true) { n       r = mid; n     } else { n       l = mid + 1; n     } n   } n   printf( ! %d %d n , (v[l] ^ diff) + 1, v[l] + 1); n   fflush(stdout); n } n int main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(NULL); n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = ~0U >> 1; n const long long INF = (long long)1e18; n const double pi = acos(-1.0); n template <class T> n inline T sqr(T a) { n   return a * a; n } n template <class T> n inline T min(T a, T b, T c) { n   return min(min(a, b), c); n } n template <class T> n inline T max(T a, T b, T c) { n   return max(max(a, b), c); n } n template <class T> n inline void read(T &n) { n   char c; n   for (c = getchar(); !(c >=  0  && c <=  9 ); c = getchar()) n     ; n   n = c -  0 ; n   for (c = getchar(); c >=  0  && c <=  9 ; c = getchar()) n = n * 10 + c -  0 ; n } n int pw(int base, int n, int mo) { n   if (n == 0) return 1; n   if (n == 1) return base; n   int tmp = pw(base, n >> 1, mo); n   tmp = (long long)tmp * tmp % mo; n   if (n & 1) tmp = (long long)tmp * base % mo; n   return tmp; n } n const int maxn = 200000; n int N, M, x, y, p[maxn], c[maxn], f[maxn], s[maxn]; n long long ans; n bool cmp(const int &a, const int &b) { return c[a] > c[b]; } n vector<int> E[maxn]; n int find(int x) { n   if (f[x] == x) return x; n   int t = find(f[x]); n   s[x] += s[f[x]]; n   return f[x] = t; n } n int main() { n   scanf( %d%d , &N, &M); n   for (int i = (1); i <= (N); ++i) scanf( %d , &c[i]); n   for (int i = (1); i <= (M); ++i) { n     scanf( %d%d , &x, &y); n     if (c[x] < c[y]) n       E[x].push_back(y); n     else n       E[y].push_back(x); n   } n   for (int i = (1); i <= (N); ++i) p[i] = i; n   sort(p + 1, p + N + 1, cmp); n   for (int i = (1); i <= (N); ++i) f[i] = i, s[i] = 1; n   for (int i = (1); i <= (N); ++i) { n     int S = p[i]; n     for (int j = 0; j < E[S].size(); ++j) { n       int x = find(S), y = find(E[S][j]); n       if (x != y) { n         ans += (long long)c[S] * s[x] * s[y]; n         f[x] = y; n         s[y] += s[x]; n       } n     } n   } n   double ret = ans * 2. / N / (N - 1); n   printf( %.10lf n , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x, y; n long long s; n int main() { n   cin >> x >> y >> s; n   if (abs(x) + abs(y) <= s && (s - (abs(x) + abs(y))) % 2 == 0) n     cout <<  Yes ; n   else n     cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n; n string str; n int main() { n   cin >> t; n   while (t--) { n     cin >> n >> str; n     int x = n, res = n; n     for (int i = 0; i < n; i++) n       if (str[i] ==  1 ) { n         res = max(res, max(n + 1, 2 * max(i + 1, n - i))); n       } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n string grid[105]; n int check_bomb(int i, int j) { n   if ((i > 0 && grid[i - 1][j] ==  * ) || n       (i < n - 1 && grid[i + 1][j] ==  * ) || n       (j > 0 && grid[i][j - 1] ==  * ) || n       (j < m - 1 && grid[i][j + 1] ==  * ) || n       (i > 0 && j > 0 && grid[i - 1][j - 1] ==  * ) || n       (i > 0 && j < m - 1 && grid[i - 1][j + 1] ==  * ) || n       (i < n - 1 && j > 0 && grid[i + 1][j - 1] ==  * ) || n       (i < n - 1 && j < m - 1 && grid[i + 1][j + 1] ==  * )) { n     printf( NO ); n     return 1; n   } n   return 0; n } n int count_bomb(int i, int j) { n   int k = grid[i][j] - 48; n   int cnt = 0; n   if (i > 0 && grid[i - 1][j] ==  * ) cnt++; n   if (i < n - 1 && grid[i + 1][j] ==  * ) cnt++; n   if (j > 0 && grid[i][j - 1] ==  * ) cnt++; n   if (j < m - 1 && grid[i][j + 1] ==  * ) cnt++; n   if (i > 0 && j > 0 && grid[i - 1][j - 1] ==  * ) cnt++; n   if (i > 0 && j < m - 1 && grid[i - 1][j + 1] ==  * ) cnt++; n   if (i < n - 1 && j > 0 && grid[i + 1][j - 1] ==  * ) cnt++; n   if (i < n - 1 && j < m - 1 && grid[i + 1][j + 1] ==  * ) cnt++; n   if (cnt != k) { n     printf( NO ); n     return 1; n   } n   return 0; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) { n     cin >> grid[i]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (grid[i][j] ==  . ) { n         int check = check_bomb(i, j); n         if (check == 1) return 0; n       } else if (grid[i][j] >=  1  && grid[i][j] <=  9 ) { n         int bomb = count_bomb(i, j); n         if (bomb == 1) return 0; n       } n     } n   } n   printf( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long a, b, T; n char s[500010]; n long long pref[500010]; n long long suff[500010]; n int main() { n   cin >> n >> a >> b >> T; n   for (int i = 1; i <= n; ++i) { n     cin >> s[i]; n   } n   pref[1] = 0; n   for (int i = 2; i <= n; ++i) { n     pref[i] = a + pref[i - 1] + 1; n     if (s[i] ==  w ) { n       pref[i] += b; n     } n   } n   suff[n + 1] = 0; n   for (int i = n; i >= 2; --i) { n     suff[i] = a + 1 + suff[i + 1]; n     if (s[i] ==  w ) { n       suff[i] += b; n     } n   } n   int ans = 0; n   long long t = 1; n   if (s[1] ==  w ) { n     t += b; n   } n   if (T < t) { n     cout << 0; n     return 0; n   } n   T -= t; n   ++ans; n   for (int i = 2; i <= n; ++i) { n     if (pref[i] > T) { n       break; n     } n     ans = max(ans, i); n     long long t = T - pref[i] - (i - 1) * a; n     if (t < 0) { n       continue; n     } n     int l = i; n     int r = n + 1; n     while (l + 1 < r) { n       int m = (l + r) / 2; n       if (suff[m] <= t) { n         r = m; n       } else { n         l = m; n       } n     } n     ans = max(ans, i + n + 1 - r); n   } n   for (int i = n; i >= 2; --i) { n     if (suff[i] > T) { n       break; n     } n     ans = max(n - i + 2, ans); n     long long t = T - suff[i] - (n + 1 - i) * a; n     if (t < 0) { n       continue; n     } n     int l = 1, r = i; n     while (l + 1 < r) { n       int m = (l + r) / 2; n       if (pref[m] <= t) { n         l = m; n       } else { n         r = m; n       } n     } n     ans = max(ans, n - i + 1 + l); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 60; n const int M = 40010; n const int OFF = 20005; n int n, m; n int lt[N], rt[N]; n set<int> cnt; n bitset<N> ll[M], rr[M]; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> lt[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> rt[i]; n     for (int j = 0; j < n; j++) { n       cnt.insert(lt[j] + rt[i] + OFF); n       ll[lt[j] + rt[i] + OFF].set(j); n       rr[lt[j] + rt[i] + OFF].set(i); n     } n   } n   int m1 = 0; n   for (auto cc1 : cnt) { n     for (auto cc2 : cnt) { n       bitset<N> unl = ll[cc1] | ll[cc2]; n       bitset<N> unr = rr[cc1] | rr[cc2]; n       m1 = max(m1, int(unl.count() + unr.count())); n     } n   } n   printf( %d n , m1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 5; n const long long linf = 1e18 + 5; n class point { n  public: n   int x, y; n   double m; n   point() {} n   point(int x, int y) { n     this->x = x; n     this->y = y; n     this->m = atan2(y, x); n   } n   bool operator<(point other) const { return m < other.m; } n   bool operator==(point other) const { n     return x == other.x and y == other.y and m == other.m; n   } n }; n int n, c, x, y, sumx, sumy; n pair<int, int> a[5]; n set<point> s; n long long ccw(point a, point c, point b) { n   pair<int, int> v1 = make_pair(c.x - a.x, c.y - a.y); n   pair<int, int> v2 = make_pair(b.x - a.x, b.y - a.y); n   return (long long)v1.first * v2.second - (long long)v1.second * v2.first; n } n set<point>::iterator something_stupid_(point x) { n   set<point>::iterator it = s.lower_bound(x); n   if (it == s.end()) it = s.begin(); n   return it; n } n set<point>::iterator something_stupid_(set<point>::iterator it) { n   it++; n   if (it == s.end()) it = s.begin(); n   return it; n } n set<point>::iterator _something_stupid(set<point>::iterator it) { n   if (it == s.begin()) { n     it = s.end(); n     it--; n   } else n     it--; n   return it; n } n int main() { n   ios ::sync_with_stdio(0); n   cin >> n; n   for (int i = 1; i <= 3; i++) { n     cin >> c >> a[i].first >> a[i].second; n     sumx += a[i].first; n     sumy += a[i].second; n   } n   pair<int, int> cent = make_pair(sumx, sumy); n   for (int i = 1; i <= 3; i++) { n     a[i].first *= 3; n     a[i].second *= 3; n     a[i].first -= cent.first; n     a[i].second -= cent.second; n     point t(a[i].first, a[i].second); n     s.insert(t); n   } n   for (int i = 4; i <= n; i++) { n     cin >> c >> x >> y; n     x *= 3; n     y *= 3; n     x -= cent.first; n     y -= cent.second; n     point t(x, y); n     if (c == 1) { n       set<point>::iterator left = something_stupid_(t); n       set<point>::iterator right = _something_stupid(left); n       if (ccw(*left, *right, t) <= 0) continue; n       while (s.size() >= 2 and ccw(t, *left, *something_stupid_(left)) <= 0) { n         set<point>::iterator it = left; n         left = something_stupid_(left); n         s.erase(it); n       } n       right = _something_stupid(left); n       while (s.size() >= 2 and ccw(t, *right, *_something_stupid(right)) >= 0) { n         set<point>::iterator it = right; n         right = _something_stupid(right); n         s.erase(it); n       } n       s.insert(t); n     } else { n       set<point>::iterator left = something_stupid_(t); n       set<point>::iterator right = _something_stupid(left); n       if (ccw(*left, *right, t) <= 0) n         cout <<  YES  <<   n ; n       else n         cout <<  NO  <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long n, m, ta, tb, k, res = 0; n   cin >> n >> m >> ta >> tb >> k; n   if (k >= n || k >= m) { n     cout << -1; n     return 0; n   } n   vector<long long> a(n), b(m); n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) cin >> b[i]; n   for (int i = 0; i < n && i <= k; i++) { n     long long search = a[i] + ta; n     long long l = -1; n     long long r = m; n     long long mid; n     while (l != r - 1) { n       mid = (l + r) / 2; n       if (b[mid] >= search) n         r = mid; n       else n         l = mid; n     } n     if (r + max(0LL, k - i) >= m) { n       cout << -1; n       return 0; n     } n     res = max(res, b[r + max(0LL, k - i)] + tb); n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n int arr[400009]; n map<int, int> mp; n vector<int> v; n int sum[400009]; n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     if (mp[arr[i]] == 0) { n       v.push_back(arr[i]); n     } n     mp[arr[i]]++; n   } n   sort(v.begin(), v.end()); n   k *= 8; n   int ans = 1000000009; n   int j = 0; n   int num = 0; n   int curr = 0; n   for (int i = 0; i < v.size(); i++) { n     curr += mp[v[i]]; n   } n   for (int i = 0; i < v.size(); i++) { n     while (j < v.size() && ceil(log2(j - i + 1)) * n <= k) { n       curr -= mp[v[j]]; n       j++; n     } n     ans = min(ans, curr); n     j = max(i, j); n     curr += mp[v[i]]; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tt = 120; n   int test[6] = {0, 1, 3, 5, 2, 4}; n   int n = 5; n   int p[2001], s[2001]; n   int p_l[2001], s_l[2001]; n   int ans = 0; n   vector<pair<int, int> > v_ans; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &p[i]); n     p_l[p[i]] = i; n   } n   for (int i = 1; i <= n; i++) s[i] = i; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &s[i]); n     s_l[s[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     if (p[i] != s[i]) { n       int r = p_l[s[i]]; n       for (int j = i; j <= r; j++) { n         int pos_j = s_l[p[j]]; n         int pos_r = s_l[p[r]]; n         if ((pos_j >= r && pos_r <= j && pos_j != pos_r)) { n           ans += abs(j - r); n           v_ans.push_back(make_pair(j, r)); n           swap(p[j], p[r]); n           swap(p_l[p[j]], p_l[p[r]]); n           r = j; n           j = i - 1; n         } n       } n     } n   } n   printf( %d n%d n , ans, v_ans.size()); n   for (int i = 0; i < v_ans.size(); i++) n     printf( %d %d n , v_ans[i].first, v_ans[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int H, M, K; n int NonZeroDigit(int x) { n   int res = 0; n   while (x) { n     if (x % 10) res++; n     x /= 10; n   } n   return res; n } n int Get(int x, int p) { n   for (int i = 1; i < p; ++i) x /= 10; n   return x; n } n long long Solve(int h, int m) { n   int d = NonZeroDigit(M - 1); n   long long res = Get(h, K - d); n   res += (long long)Get(m, K) + (long long)h * Get(M - 1, K); n   return res; n } n int main() { n   cin >> H >> M >> K; n   int h1, m1, h2, m2; n   cin >> h1 >> m1 >> h2 >> m2; n   if (pair<int, int>(h1, m1) <= pair<int, int>(h2, m2)) n     cout << Solve(h2, m2) - Solve(h1, m1) << endl; n   else n     cout << Solve(H - 1, M - 1) - Solve(h1, m1) + n                 (NonZeroDigit(H - 1) + NonZeroDigit(M - 1) >= K) + Solve(h2, m2) n          << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a1, a2, a3, a4; n int head; n int b, c; n int nott(int x) { n   if (x == 4) return 7; n   return 4; n } n int main() { n   cin >> a1 >> a2 >> a3 >> a4; n   if (abs(a3 - a4) > 1) { n     cout << -1 << endl; n     return 0; n   } n   if (a3 > a4) head = 4; n   if (a4 > a3) head = 7; n   c = a3; n   b = a4; n   if (!head) { n     if (b != a1) n       head = 4; n     else n       head = 7; n   } n   if (head) { n     if (head == 7) n       c++; n     else n       b++; n   } n   if (b > a1 || c > a2) { n     cout << -1 << endl; n     return 0; n   } n   if (head == 7) { n     cout << 7; n     a2--; n     a4--; n     c--; n   } n   for (int i = 0; i < a1 - (b - 1); ++i) cout << 4; n   for (int i = 0; i < b - 1 - (b == c + 1); ++i) cout << 74; n   for (int i = 0; i < a2 - (b - 1) + (b == c + 1); ++i) cout << 7; n   if (b == c + 1) cout << 4; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string st, t; n vector<string> a; n int p; n int main() { n   ios_base ::sync_with_stdio(false); n   cin >> st; n   for (auto i : st) { n     t += i; n     if (i ==  > ) { n       a.push_back(t); n       t =   ; n     } n   } n   for (auto i : a) { n     if (i[1] ==  / ) { n       --p; n     } n     for (int j = 0; j < p; ++j) { n       cout <<     ; n     } n     cout << i << endl; n     if (i[1] !=  / ) { n       ++p; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a[110][110], row[110], col[110], ansrow[110], anscol[110], n, m, tans, ans; n bool flag; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %d , &a[i][j]); n   ans = 10000000; n   for (int i = 0; i <= 500; i++) { n     row[1] = i; n     flag = 1; n     for (int j = 1; j <= m; j++) n       if (a[1][j] < i) { n         flag = 0; n         break; n       } else n         col[j] = a[1][j] - i; n     if (!flag) break; n     for (int j = 2; j <= n; j++) { n       if (a[j][1] - col[1] < 0) { n         flag = 0; n         break; n       } n       row[j] = a[j][1] - col[1]; n       for (int k = 2; k <= m; k++) n         if (a[j][k] - col[k] != row[j]) { n           flag = 0; n           break; n         } n       if (!flag) break; n     } n     if (!flag) continue; n     tans = 0; n     for (int j = 1; j <= n; j++) tans += row[j]; n     for (int j = 1; j <= m; j++) tans += col[j]; n     if (tans < ans) { n       ans = tans; n       for (int j = 1; j <= n; j++) ansrow[j] = row[j]; n       for (int j = 1; j <= m; j++) anscol[j] = col[j]; n     } n   } n   if (ans == 10000000) { n     printf( -1 ); n     return 0; n   } n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= ansrow[i]; j++) printf( row %d n , i); n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= anscol[i]; j++) printf( col %d n , i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000050]; n vector<int> vec[1000050]; n int pos[1000050]; n int main() { n   int n, x; n   scanf( %d%d , &n, &x); n   for (int i = 0; i < n; i++) { n     scanf( %d , a + i); n     vec[a[i]].push_back(i); n   } n   pos[x + 1] = n; n   int st = 1; n   for (int i = x; i; i--) { n     pos[i] = pos[i + 1]; n     if (vec[i].size()) { n       if (vec[i].back() < pos[i]) { n         pos[i] = vec[i][0]; n       } else { n         st = i + 1; n         break; n       } n     } n   } n   int cur = -1; n   long long res = 0; n   for (int i = 1; i <= x; i++) { n     int p = lower_bound(pos + st, pos + x + 1, cur) - pos; n     p = max(p, i + 1); n     res += (x - p + 2); n     if (p == i) res--; n     if (vec[i].size()) { n       if (cur < vec[i][0]) n         cur = vec[i].back(); n       else n         break; n     } n   } n   printf( %lld n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, k; n   cin >> n >> k; n   priority_queue<pair<int, int> > p; n   set<int> s; n   for (int i = 1; i <= n; i++) { n     int in; n     cin >> in; n     p.push({in, i}); n     s.insert(i + k); n   } n   long long ans = 0; n   vector<int> v(n); n   while (p.size()) { n     int c = p.top().first, d = p.top().second; n     p.pop(); n     auto it = s.lower_bound(d); n     ans += 1LL * (*it - d) * c; n     v[d - 1] = *it; n     s.erase(it); n   } n   cout << ans <<   n ; n   for (auto i : v) cout << i <<    ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string arf[1005], ars[1005]; n string ans[1005]; n void solve() { n   int n; n   cin >> n; n   map<string, vector<int> > f, s; n   int done[n]; n   memset(done, 0, sizeof(done)); n   ; n   for (int i = 0; i < (int)(n); i++) { n     string s1, s2; n     cin >> s1 >> s2; n     arf[i] = s1.substr(0, 3); n     ars[i] = s1.substr(0, 2) + s2.substr(0, 1); n     f[arf[i]].push_back(i); n     s[ars[i]].push_back(i); n   } n   map<string, int> final; n   for (auto i = f.begin(); i != f.end(); i++) { n     if (i->second.size() > 1) { n       for (int k = 0; k < i->second.size(); k++) { n         final[ars[i->second[k]]]++; n         ans[i->second[k]] = ars[i->second[k]]; n         done[i->second[k]] = 1; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     if (done[i] == 0) { n       if (final[ars[i]] == 0) { n         final[ars[i]] = 1; n         ans[i] = ars[i]; n       } else if (final[arf[i]] == 0) { n         final[arf[i]] = 1; n         ans[i] = arf[i]; n       } else { n         cout <<  NO ; n         return; n       } n     } n   } n   for (auto i = final.begin(); i != final.end(); i++) { n     if (i->second > 1) { n       cout <<  NO ; n       return; n     } n   } n   cout <<  YES n ; n   for (int i = 0; i < (int)(n); i++) { n     cout << ans[i] <<   n ; n   } n } n int main() { n   cin.tie(0); n   cout.tie(0); n   cin.sync_with_stdio(0); n   cout.sync_with_stdio(0); n   int TC = 1; n   for (int ZZ = 1; ZZ <= TC; ZZ++) { n     clock_t start = clock(); n     solve(); n     clock_t end = clock(); n     cerr <<  Time:   << (double)(end - start) / CLOCKS_PER_SEC <<   seconds  n          <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 20; n int h[maxn], n, q, cnt; n string s; n struct Node { n   int ans, mx, mn, l, r, lazy; n   Node(int a = 0) { ans = mn = mx = l = r = lazy = a; } n   void update(Node a, Node b) { n     ans = max({a.ans, b.ans, a.l + b.mx, b.r + a.mx}); n     mx = max({a.mx, b.mx}); n     mn = min({a.mn, b.mn}); n     l = max({a.l, b.l, a.mx - 2 * b.mn}); n     r = max({a.r, b.r, b.mx - 2 * a.mn}); n   } n } sg[4 * maxn]; n void add(int qval, int id) { n   sg[id].l -= qval; n   sg[id].r -= qval; n   sg[id].mn += qval; n   sg[id].mx += qval; n   sg[id].lazy += qval; n } n void shift(int id) { n   add(sg[id].lazy, id * 2); n   add(sg[id].lazy, id * 2 + 1); n   sg[id].lazy = 0; n } n void update(int l, int r, int ql, int qr, int qval, int id) { n   if (l >= ql && r <= qr) n     add(qval, id); n   else if (l > qr || r < ql) n     return; n   else { n     int mid = (l + r) / 2; n     shift(id); n     update(l, mid, ql, qr, qval, id * 2); n     update(mid + 1, r, ql, qr, qval, id * 2 + 1); n     sg[id].update(sg[id * 2], sg[id * 2 + 1]); n   } n } n void build(int l, int r, int id) { n   if (l == r) { n     sg[id].mn = sg[id].mx = h[r]; n     sg[id].l = sg[id].r = -h[r]; n   } else { n     int mid = (l + r) / 2; n     build(l, mid, id * 2); n     build(mid + 1, r, id * 2 + 1); n     sg[id].update(sg[id * 2], sg[id * 2 + 1]); n   } n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> q >> s; n   for (int i = 0; i < 2 * n - 2; i++) { n     if (s[i] ==  ( ) n       cnt++; n     else n       cnt--; n     h[i + 2] = cnt; n   } n   build(1, 2 * n - 1, 1); n   int a, b, tmp; n   cout << sg[1].ans << endl; n   while (q--) { n     cin >> a >> b; n     if (a > b) swap(a, b); n     tmp = 0; n     if (s[a - 1] ==  (  && s[b - 1] ==  ) ) tmp = -2; n     if (s[a - 1] ==  )  && s[b - 1] ==  ( ) tmp = 2; n     update(1, 2 * n - 1, a + 1, b, tmp, 1); n     cout << sg[1].ans << endl; n     swap(s[a - 1], s[b - 1]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int safeop(int n, int mod) { return (n + mod) % mod; } n int main() { n   int n; n   cin >> n; n   vector<int> v(n, 0); n   vector<pair<int, int>> ans(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   int vn = n; n   int v1 = -1; n   stack<int> bal1; n   stack<int> bal2; n   stack<int> bal3; n   for (int i = 0; i < n; i++) { n     if (v[i] == 3) { n       if (vn - v1 == 1) { n         cout << -1; n         return 0; n       } n       vn--; n       ans[i] = make_pair(vn, -1); n       if (!bal3.empty()) { n         int tmp = bal3.top(); n         bal3.pop(); n         ans[tmp].second = i; n         bal3.push(i); n       } else { n         bal3.push(i); n       } n     } else if (v[i] == 2) { n       if (vn - v1 == 1) { n         cout << -1; n         return 0; n       } n       v1++; n       ans[i] = make_pair(v1, -1); n       if (!bal3.empty()) { n         int tmp = bal3.top(); n         bal3.pop(); n         ans[tmp].second = i; n         bal2.push(i); n       } else { n         bal2.push(i); n       } n     } else if (v[i] == 1) { n       if (!bal2.empty()) { n         int tmp = bal2.top(); n         bal2.pop(); n         ans[tmp].second = i; n         ans[i] = make_pair(ans[tmp].first, 0); n       } else if (!bal3.empty()) { n         if (vn - v1 == 1) { n           cout << -1; n           return 0; n         } n         vn--; n         ans[i] = make_pair(vn, 0); n         int tmp = bal3.top(); n         bal3.pop(); n         ans[tmp].second = i; n       } else { n         if (vn - v1 == 1) { n           cout << -1; n           return 0; n         } n         vn--; n         ans[i] = make_pair(vn, 0); n       } n     } else { n       continue; n     } n   } n   for (int i = 0; i < n; i++) { n     if (ans[i].second == -1) { n       cout << -1 << endl; n       return 0; n     } n   } n   set<pair<int, int>> ansset; n   for (int i = 0; i < n; i++) { n     if (v[i] == 1) { n       ansset.insert(make_pair(i + 1, ans[i].first + 1)); n     } n     if (v[i] == 2) { n       ansset.insert(make_pair(i + 1, ans[i].first + 1)); n       ansset.insert(make_pair(ans[i].second + 1, ans[i].first + 1)); n     } n     if (v[i] == 3) { n       ansset.insert(make_pair(i + 1, ans[i].first + 1)); n       ansset.insert(make_pair(ans[i].second + 1, ans[i].first + 1)); n       ansset.insert(make_pair(ans[i].second + 1, ans[ans[i].second].first + 1)); n     } n   } n   cout << ansset.size() << endl; n   for (auto it = ansset.begin(); it != ansset.end(); ++it) { n     cout << n + 1 - it->second <<     << it->first << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     for (int i = 1; i <= n; ++i) cout << 1 <<    ; n     cout << endl; n   } n } n