#include <bits/stdc++.h> n using namespace std; n struct frnd { n   int w; n   int h; n   frnd(int w, int h) : w(w), h(h) {} n }; n bool cmp(frnd a, frnd b) { return a.h < b.h; } n int main() { n   int n; n   cin >> n; n   vector<frnd> friends; n   int sum = 0; n   int height = 0; n   int prevH = 0; n   for (int i = 0; i < n; i++) { n     int w, h; n     cin >> w >> h; n     frnd a(w, h); n     friends.push_back(a); n     sum += w; n     if (h > height) { n       prevH = height; n       height = h; n     } else { n       if (h > prevH) { n         prevH = h; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     cout << (sum - friends[i].w) * ((height == friends[i].h) ? prevH : height) n          <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct query { n   int id, l, x, ans; n } qrs[100009]; n bool idcomp(query a, query b) { return a.id < b.id; } n bool lcomp(query a, query b) { return a.l < b.l; } n int N, Q, a[100009], killer[20]; n int modExp(int a, int b) { n   if (b == 0) return 1; n   int c = modExp(a, b >> 1); n   c = (c * 1ll * c) % 1000000007; n   if (b & 1) c = (c * 1ll * a) % 1000000007; n   return c; n } n int main() { n   scanf( %d %d , &N, &Q); n   memset(&killer, -1, sizeof killer); n   for (int i = 0; i < N; i++) { n     scanf( %d , &a[i]); n   } n   for (int q = 0; q < Q; q++) { n     scanf( %d %d , &qrs[q].l, &qrs[q].x); n     qrs[q].id = q; n     qrs[q].l--; n   } n   sort(qrs, qrs + Q, lcomp); n   int q = 0, nz = 0; n   for (int i = 0; i < N; i++) { n     for (int j = 0; j < 20; j++) { n       if (killer[j] != -1 && (a[i] & 1 << j)) { n         a[i] ^= a[killer[j]]; n       } n     } n     for (int j = 0; j < 20; j++) { n       if (killer[j] == -1 && (a[i] & 1 << j)) { n         killer[j] = i; n         for (int it = 0; it < i; it++) { n           if ((a[it] & 1 << j)) a[it] ^= a[i]; n         } n         break; n       } n     } n     if (a[i] == 0) nz++; n     while (q < Q && qrs[q].l < i) q++; n     while (q < Q && qrs[q].l == i) { n       int x = qrs[q].x; n       for (int j = 0; j < 20; j++) { n         if (killer[j] != -1 && (x & 1 << j)) { n           x ^= a[killer[j]]; n         } n       } n       if (x == 0) n         qrs[q].ans = modExp(2, nz); n       else n         qrs[q].ans = 0; n       q++; n     } n   } n   sort(qrs, qrs + Q, idcomp); n   for (int q = 0; q < Q; q++) { n     printf( %d n , qrs[q].ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n const int MOD = 1e9 + 7; n const int N = 1007; n int n; n vector<int> ans, l, r, p1, p2, p3, p4; n long long dis[N]; n long long qry(int t, int i, int j, int k) { n   printf( %d %d %d %d n , t, i, j, k); n   fflush(stdout); n   long long res; n   scanf( %lld , &res); n   return res; n } n void Init() { n   ans.clear(); n   l.clear(); n   r.clear(); n } n int Solve() { n   for (int i = (3); i < (n + 1); ++i) { n     long long res = qry(2, 1, 2, i); n     if (res == 1) n       l.push_back(i); n     else n       r.push_back(i); n   } n   ans.push_back(1); n   if ((int)(r).size()) { n     long long mx = -LINF, id; n     for (auto o : r) { n       long long res = qry(1, 1, 2, o); n       dis[o] = res; n       if (res > mx) { n         mx = res; n         id = o; n       } n     } n     p4.clear(); n     p1.clear(); n     for (auto o : r) n       if (o != id) { n         long long res = qry(2, 1, id, o); n         if (res == 1) n           p1.push_back(o); n         else n           p4.push_back(o); n       } n     sort((p4).begin(), (p4).end(), n          [](int a, int b) { return dis[a] < dis[b]; }); n     sort((p1).begin(), (p1).end(), n          [](int a, int b) { return dis[a] > dis[b]; }); n     for (auto o : p4) ans.push_back(o); n     ans.push_back(id); n     for (auto o : p1) ans.push_back(o); n   } n   ans.push_back(2); n   if ((int)(l).size()) { n     long long mx = -LINF, id; n     for (auto o : l) { n       long long res = qry(1, 1, 2, o); n       dis[o] = res; n       if (res > mx) { n         mx = res; n         id = o; n       } n     } n     p2.clear(); n     p3.clear(); n     for (auto o : l) n       if (o != id) { n         long long res = qry(2, 1, id, o); n         if (res == 1) n           p3.push_back(o); n         else n           p2.push_back(o); n       } n     sort((p2).begin(), (p2).end(), n          [](int a, int b) { return dis[a] < dis[b]; }); n     sort((p3).begin(), (p3).end(), n          [](int a, int b) { return dis[a] > dis[b]; }); n     for (auto o : p2) ans.push_back(o); n     ans.push_back(id); n     for (auto o : p3) ans.push_back(o); n   } n   printf( 0 ); n   for (auto o : ans) printf(  %d , o); n   return puts(  ); n } n int main() { n   scanf( %d , &n); n   Init(); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 110, size = 1 << 20, mod = 998244353, inf = 2e9; n const long long INF = 1e15; n template <class o> n void qr(o& x) { n   char c = getchar(); n   x = 0; n   int f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) x = x * 10 + c -  0 , c = getchar(); n   x *= f; n } n template <class o> n void qw(o x) { n   if (x / 10) qw(x / 10); n   putchar(x % 10 +  0 ); n } n template <class o> n void pr1(o x) { n   if (x < 0) x = -x, putchar( - ); n   qw(x); n   putchar(   ); n } n template <class o> n void pr2(o x) { n   if (x < 0) x = -x, putchar( - ); n   qw(x); n   putchar(  n ); n } n long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n long long power(long long a, long long b = mod - 2, long long p = mod) { n   long long c = 1; n   while (b) { n     if (b & 1) c = c * a % p; n     b /= 2; n     a = a * a % p; n   } n   return c; n } n template <class o> n void cmax(o& x, o y) { n   if (x < y) x = y; n } n template <class o> n void cmin(o& x, o y) { n   if (x > y) x = y; n } n template <typename t1, typename t2> n void ad(t1& x, t2 y) { n   x += y; n   if (x >= mod) x -= mod; n } n template <typename t1, typename t2> n void dl(t1& x, t2 y) { n   x -= y; n   if (x < 0) x += mod; n } n template <typename T> n struct BIT { n   T* c; n   int n; n   BIT(int _n) : n(_n) { n     c = new T[n]; n     c--; n     for (int i = 1; i <= n; i++) c[i] = T(0); n   } n   void add(int x, T y) { n     for (; x <= n; x += x & -x) c[x] = c[x] + y; n   } n   T sum(int x) { n     T y = T(0); n     for (; x; x &= x - 1) y = y + c[x]; n     return y; n   } n }; n long long jc[N], inv[N]; n void jc_init(int n) { n   jc[0] = 1; n   for (int i = 1; i <= n; i++) jc[i] = jc[i - 1] * i % mod; n   inv[n] = power(jc[n]); n   for (int i = n; i; i--) inv[i - 1] = inv[i] * i % mod; n } n long long C(int x, int y) { n   if (x < y || y < 0) return 0; n   return jc[x] * inv[y] % mod * inv[x - y] % mod; n } n int n, m; n char s[N][N]; n void solve() { n   qr(n); n   qr(m); n   vector<vector<pair<int, int> > > v; n   for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); n   for (int i = 1; i <= n - 2; i++) n     for (int j = 1; j <= m; j++) n       if (s[i][j] ==  1 ) { n         s[i][j] ^= 1; n         vector<pair<int, int> > o; n         o.push_back(make_pair(i, j)); n         o.push_back(make_pair(i + 1, j)); n         s[i + 1][j] ^= 1; n         if (j < m) n           o.push_back(make_pair(i + 1, j + 1)), s[i + 1][j + 1] ^= 1; n         else n           o.push_back(make_pair(i + 1, j - 1)), s[i + 1][j - 1] ^= 1; n         v.push_back(o); n         assert(((int)o.size()) == 3); n       } n   for (int j = 1; j <= m - 2; j++) { n     vector<pair<int, int> > o; n     if (s[n][j] ==  1 ) o.push_back(make_pair(n, j)), s[n][j] =  0 ; n     if (s[n - 1][j] ==  1 ) o.push_back(make_pair(n - 1, j)), s[n - 1][j] =  0 ; n     if (((int)o.size())) { n       o.push_back(make_pair(n, j + 1)); n       s[n][j + 1] ^= 1; n       if (((int)o.size()) < 3) n         o.push_back(make_pair(n - 1, j + 1)), s[n - 1][j + 1] ^= 1; n       v.push_back(o); n     } n   } n   vector<pair<int, int> > o; n   char &x = s[n - 1][m - 1], &y = s[n - 1][m], &u = s[n][m - 1], &vv = s[n][m]; n   if (x ==  1 ) o.push_back(make_pair(n - 1, m - 1)), x =  0 ; n   if (y ==  1 ) o.push_back(make_pair(n - 1, m)), y =  0 ; n   if (((int)o.size())) { n     for (int j = m - 1; j <= m; j++) n       if (((int)o.size()) < 3) o.push_back(make_pair(n, j)), s[n][j] ^= 1; n     v.push_back(o); n   } n   o.clear(); n   if (u ==  1  && vv ==  1 ) { n     o.push_back(make_pair(n - 1, m - 1)); n     o.push_back(make_pair(n - 1, m)); n     o.push_back(make_pair(n, m)); n     v.push_back(o); n     o[2] = make_pair(n, m - 1); n     v.push_back(o); n   } else if (u ==  1 ) { n     o.push_back(make_pair(n, m - 1)); n     o.push_back(make_pair(n - 1, m)); n     o.push_back(make_pair(n, m)); n     v.push_back(o); n     o[0] = make_pair(n - 1, m - 1); n     o[1] = make_pair(n, m - 1); n     o[2] = make_pair(n - 1, m); n     v.push_back(o); n     o[2] = make_pair(n, m); n     v.push_back(o); n   } else if (vv ==  1 ) { n     o.push_back(make_pair(n, m)); n     o.push_back(make_pair(n - 1, m - 1)); n     o.push_back(make_pair(n, m - 1)); n     v.push_back(o); n     o.clear(); n     o.push_back(make_pair(n - 1, m - 1)); n     o.push_back(make_pair(n - 1, m)); n     o.push_back(make_pair(n, m)); n     v.push_back(o); n     o[0] = make_pair(n, m - 1); n     v.push_back(o); n   } n   pr2(((int)v.size())); n   for (auto i : v) { n     for (auto j : i) pr1(j.first), pr1(j.second); n     puts(  ); n   } n } n int main() { n   int T = 1; n   qr(T); n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct r { n   int p, t; n } a; n vector<r> v; n bool sss(const r& i, const r& j) { n   if (i.p > j.p) n     return true; n   else if (i.p == j.p) n     return i.t < j.t; n   else n     return false; n } n int main() { n   int n, k; n   cin >> n >> k; n   map<pair<int, int>, int> mp; n   for (int cs = 1; cs <= n; cs++) { n     cin >> a.p >> a.t; n     mp[make_pair(a.p, a.t)]++; n     v.push_back(a); n   } n   sort(v.begin(), v.end(), sss); n   cout << mp[{v[k - 1].p, v[k - 1].t}] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int N = 5e5 + 3, M = 2e5 + 3; n int MOD; n string s, t, ts; n int n, m, pw[N], hs[N], ht, Z[N + M]; n bool check_prime(int x) { n   for (int i = 2; i * i <= x; ++i) { n     if (x % i == 0) return false; n   } n   return true; n } n int add(int x, int y) { n   x += y; n   while (x >= MOD) x -= MOD; n   while (x < 0) x += MOD; n   return x; n } n int mul(int x, int y) { return (x * 1LL * y) % MOD; } n int get_hash(int l, int r) { n   return add(hs[r], -mul(hs[l - 1], pw[r - l + 1])); n } n void init() { n   while (true) { n     MOD = uniform_int_distribution<int>(1e8, 1e9)(rng); n     if (check_prime(MOD)) break; n   } n   cin >> s >> t; n   n = s.size(), m = t.size(); n   ts =     + t + s, s =     + s, t =     + t; n   pw[0] = 1; n   for (int i = 1; i <= n; ++i) pw[i] = mul(pw[i - 1], 10); n   for (int i = 1; i <= n; ++i) hs[i] = add(s[i] -  0 , mul(hs[i - 1], 10)); n   for (int i = 1; i <= m; ++i) ht = add(t[i] -  0 , mul(ht, 10)); n   Z[1] = n + m; n   int l = 1, r = 1; n   for (int i = 2; i <= n + m; ++i) { n     if (i > r) { n       l = r = i; n       while (r <= n + m && ts[r] == ts[r - l + 1]) ++r; n       Z[i] = r - l; n       r--; n     } else { n       int k = i - l + 1; n       if (Z[k] < r - i + 1) n         Z[i] = Z[k]; n       else { n         l = i; n         while (r <= n + m && ts[r] == ts[r - l + 1]) ++r; n         Z[i] = r - l; n         r--; n       } n     } n   } n } n void solve() { n   init(); n   for (int i = 1; i + 2 * (m - 1) - 1 <= n; ++i) { n     int x = get_hash(i, i + m - 2); n     int x2 = get_hash(i + m - 1, i + 2 * m - 3); n     x = add(x, x2); n     if (x == ht) { n       cout << i <<     << i + m - 2 <<   n  n            << i + m - 1 <<     << i + 2 * m - 3; n       return; n     } n   } n   for (int i = 1; i + m - 1 <= n; ++i) { n     int x = get_hash(i, i + m - 1); n     if (m - Z[i + m] > 0) { n       if (i - m + Z[i + m] > 0) { n         int x2 = get_hash(i - m + Z[i + m], i - 1); n         x = add(x, x2); n         if (x == ht) { n           cout << i - m + Z[i + m] <<     << i - 1 <<   n  n                << i <<     << i + m - 1; n           return; n         } n         x = add(x, -x2); n       } n       if (i + 2 * m - Z[i + m] - 1 <= n) { n         int x2 = get_hash(i + m, i + 2 * m - Z[i + m] - 1); n         x = add(x, x2); n         if (x == ht) { n           cout << i <<     << i + m - 1 <<   n  n                << i + m <<     << i + 2 * m - Z[i + m] - 1; n           return; n         } n         x = add(x, -x2); n       } n     } n     if (m - Z[i + m] - 1 > 0) { n       if (i - m + Z[i + m] + 1 > 0) { n         int x2 = get_hash(i - m + Z[i + m] + 1, i - 1); n         x = add(x, x2); n         if (x == ht) { n           cout << i - m + Z[i + m] + 1 <<     << i - 1 <<   n  n                << i <<     << i + m - 1; n           return; n         } n         x = add(x, -x2); n       } n       if (i + 2 * m - Z[i + m] - 2 <= n) { n         int x2 = get_hash(i + m, i + 2 * m - Z[i + m] - 2); n         x = add(x, x2); n         if (x == ht) { n           cout << i <<     << i + m - 1 <<   n  n                << i + m <<     << i + 2 * m - Z[i + m] - 2; n           return; n         } n         x = add(x, -x2); n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 500; n const int dr[] = {0, 1, 0, -1}; n const int dc[] = {1, 0, -1, 0}; n bool vis[MAXN + 5][MAXN + 5]; n char grid[MAXN + 5][MAXN + 5]; n int W, H; n vector<pair<char, pair<int, int> > > ans; n inline void put(char ch, int i, int j) { n   ans.push_back(make_pair(ch, make_pair(i, j))); n } n void buildRed(int i, int j) { n   if (i < 0 || j < 0 || i >= H || j >= W) return; n   if (grid[i][j] ==  # ) return; n   if (vis[i][j]) return; n   vis[i][j] = true; n   for (int dir = 0; dir < 4; dir++) buildRed(i + dr[dir], j + dc[dir]); n   put( D , i + 1, j + 1); n   put( R , i + 1, j + 1); n   return; n } n int main() { n   int c, c2; n   scanf( %d%d , &H, &W); n   for (c = 0; c < H; c++) scanf( %s , grid[c]); n   for (c = 0; c < H; c++) n     for (c2 = 0; c2 < W; c2++) n       if (grid[c][c2] ==  . ) put( B , c + 1, c2 + 1); n   for (c = 0; c < H; c++) n     for (c2 = 0; c2 < W; c2++) n       if (!vis[c][c2] && grid[c][c2] ==  . ) { n         vis[c][c2] = true; n         for (int dir = 0; dir < 4; dir++) buildRed(c + dr[dir], c2 + dc[dir]); n       } n   printf( %d n , ans.size()); n   for (c = 0; c < ans.size(); c++) n     printf( %c %d %d n , ans[c].first, ans[c].second.first, n            ans[c].second.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1 << 19; n int n, k, m, e, a[N], b[N], g[N], c[N], d[N], f[N], t[N], w[N], p[N]; n vector<int> v[N]; n void upd(int k) { n   if (t[k << 1] > t[k << 1 | 1]) n     t[k] = t[k << 1] + w[k], p[k] = p[k << 1]; n   else n     t[k] = t[k << 1 | 1] + w[k], p[k] = p[k << 1 | 1]; n } n void build(int k = 1, int l = 1, int r = n) { n   if (l == r) { n     t[k] = d[g[l]]; n     w[k] = 0; n     p[k] = l; n     return; n   } n   int h = l + r >> 1; n   build(k << 1, l, h); n   build(k << 1 | 1, h + 1, r); n   upd(k); n } n void modify(int L, int R, int X, int k = 1, int l = 1, int r = n) { n   if (L <= l && r <= R) { n     w[k] += X; n     t[k] += X; n     return; n   } n   int h = l + r >> 1; n   if (L <= h) modify(L, R, X, k << 1, l, h); n   if (h < R) modify(L, R, X, k << 1 | 1, h + 1, r); n   upd(k); n } n void dfs(int u, int fa) { n   int i, x, o = 1; n   a[u] = ++e; n   g[e] = u; n   d[u] = d[fa] + 1; n   f[u] = fa; n   for (i = 0; i < v[u].size(); i++) { n     x = v[u][i]; n     if (x != fa) dfs(x, u), o = 0; n   } n   m += o; n   b[u] = e; n } n void go(int x) { n   while (x && !c[x]) { n     modify(a[x], b[x], -1); n     c[x] = 1; n     e++; n     x = f[x]; n   } n } n int main() { n   int i, x, y; n   long long s = 0; n   scanf( %d%d , &n, &k); n   for (i = 1; i < n; i++) { n     scanf( %d%d , &x, &y); n     v[x].push_back(y); n     v[y].push_back(x); n   } n   e = 0; n   dfs(1, 0); n   if (m <= k) { n     for (i = m; i <= k; i++) s = max(s, (long long)i * (n - i)); n     cout << s; n     return 0; n   } n   build(); n   e = 0; n   for (i = 1; i <= k; i++) { n     go(g[p[1]]); n   } n   s = (long long)k * (n - k); n   for (i = 0; i <= n - e; i++) s = min(s, (long long)(k - i) * (n - k - i)); n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(nullptr); n   int n; n   std::cin >> n; n   std::vector<std::vector<std::pair<int, int>>> g(n); n   for (int i = 1; i < n; ++i) { n     int u, v, w; n     std::cin >> u >> v >> w; n     --u; n     --v; n     g[u].push_back(std::make_pair(v, w)); n     g[v].push_back(std::make_pair(u, w)); n   } n   std::vector<int> sz(n); n   std::vector<long long> w(n); n   long long ans = 0; n   std::function<void(int, int)> dfs = [&](int u, int fa) { n     sz[u] = 1; n     std::vector<int> adj; n     for (auto p : g[u]) { n       int v = p.first, l = p.second; n       if (v == fa) { n         continue; n       } n       w[v] += l * 2; n       dfs(v, u); n       w[u] += w[v]; n       sz[u] += sz[v]; n       ans += l * sz[v]; n       adj.push_back(v); n     } n     std::sort(adj.begin(), adj.end(), n               [&](int u, int v) { return w[u] * sz[v] < w[v] * sz[u]; }); n     long long sum = 0; n     for (auto v : adj) { n       ans += sum * sz[v]; n       sum += w[v]; n     } n   }; n   dfs(0, -1); n   std::cout << std::setprecision(20) << 1.0 * ans / (n - 1) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n void sherlock(int t) { n   int n; n   cin >> n; n   string s; n   cin >> s; n   string t1, t2, ans; n   int mink = 1; n   ans = s; n   for (int k = 1; k <= n; k++) { n     t1 = s.substr(k - 1, n - k + 1); n     t2 = s.substr(0, k - 1); n     if ((n - k + 1) % 2 == 1) reverse(t2.begin(), t2.end()); n     t1 += t2; n     if (t1 < ans) { n       mink = k; n       ans = t1; n     } n   } n   cout << ans << endl << mink << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int tt; n   cin >> tt; n   for (int t = 1; t <= tt; t++) sherlock(t); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct dato { n   int i, j, val; n }; n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   vector<dato> input(k); n   for (int t = 0; t < k; t++) { n     dato &d = input[t]; n     cin >> d.i >> d.j >> d.val; n     d.i--; n     d.j--; n   } n   long long int p; n   cin >> p; n   if (n > m) { n     for (int t = 0; t < k; t++) { n       dato &d = input[t]; n       swap(d.i, d.j); n     } n     swap(n, m); n   } n   vector<int> numcol(m, 0); n   vector<int> prodcol(m, 1); n   for (int t = 0; t < k; t++) { n     dato &d = input[t]; n     numcol[d.j]++; n     prodcol[d.j] *= d.val; n   } n   bool error = false; n   for (int j = 0; j < m and not error; j++) n     error = numcol[j] == n and prodcol[j] == 1; n   if (error or n % 2 != m % 2) n     cout << 0 << endl; n   else { n     int numllenas = 0; n     for (int j = 0; j < m; j++) n       if (numcol[j] == n) numllenas++; n     int e = (n - 1) * (m - 1) - k + numllenas; n     long long int r = 2; n     long long int a = 1; n     while (e > 0) { n       if (e % 2 == 0) { n         r = (r * r) % p; n         e /= 2; n       } else { n         a = (a * r) % p; n         e--; n       } n     } n     cout << a << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 10; n const int INF = 0x3f3f3f3f; n const long long int LINF = 0x3f3f3f3f3f3f3f3f; n const int NINF = -INF - 1; n const long long int mod = 1e9 + 7; n int pre[MAXN]; n int find(int x) { return pre[x] == x ? x : pre[x] = find(pre[x]); } n int T, n, k; n char s[MAXN]; n int cnt[MAXN][28]; n int main() { n   cin >> T; n   while (T--) { n     scanf( %d%d , &n, &k); n     scanf( %s , s); n     vector<int> vec; n     for (int i = 0; i <= n + 1; i++) pre[i] = i; n     for (int i = 0; i < n; i++) { n       if (i + k < n) { n         int a = find(i), b = find(i + k); n         if (a != b) pre[a] = b; n       } n     } n     for (int i = 0; i < n / 2; i++) { n       int a = find(i), b = find(n - i - 1); n       if (a != b) pre[a] = b; n     } n     for (int i = 0; i < n; i++) { n       cnt[find(i)][s[i] -  a ]++; n     } n     int ans = 0; n     for (int i = 0; i < n; i++) { n       if (pre[i] != i) continue; n       int mm = 0, tot = 0; n       for (int j = 0; j < 26; j++) { n         mm = max(cnt[i][j], mm); n         tot += cnt[i][j]; n       } n       for (int j = 0; j < 26; j++) cnt[i][j] = 0; n       ans += tot - mm; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 16; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   vector<int> ans(maxn, -1); n   ans[0] = 0; n   for (int i = 1; i < maxn; ++i) { n     for (auto j : vector<int>{4, 6, 9}) { n       if (i >= j && ans[i - j] != -1) { n         ans[i] = max(ans[i], ans[i - j] + 1); n       } n     } n   } n   int q; n   cin >> q; n   for (int i = 0; i < q; ++i) { n     int n; n     cin >> n; n     if (n < maxn) { n       cout << ans[n] <<   n ; n     } else { n       int t = (n - maxn) / 4 + 1; n       cout << t + ans[n - 4 * t] <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000000 + 7; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int n; n   cin >> n; n   n -= 10; n   if (n >= 1 && n <= 11 && n != 10) { n     cout << 4; n   } else if (n == 10) { n     cout << 15; n   } else { n     cout << 0; n   } n   return 0; n } 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 t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long a[n]; n     long long i, j, k, l; n     for (i = 0; i < n; i++) { n       cin >> a[i]; n     } n     long long diff; n     bool value = true; n     for (i = 0; i < n - 1; i++) { n       diff = abs(a[i] - a[i + 1]); n       if (diff == 1) { n         continue; n       } n       if (diff > 1) { n         if (a[i] > a[i + 1]) { n           continue; n         } n         if (a[i] < a[i + 1]) { n           cout <<  NO  n                <<   n ; n           value = false; n           break; n         } n       } n     } n     if (value) { n       cout <<  YES  n            <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int f[55], s[55]; n long double p[55]; n long double dp[55][5005]; n bool vis[55][5005]; n int main() { n   int n, r; n   scanf( %d%d , &n, &r); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%Lf , &f[i], &s[i], &p[i]); n     p[i] /= 100; n   } n   long double lo = 0, hi = 1e18, ans = 0; n   for (int i = 0; i < 200; i++) { n     long double mid = (lo + hi) / 2; n     memset(vis, 0, sizeof(vis)); n     memset(dp, 0, sizeof(dp)); n     vis[n + 1][0] = 1; n     for (int i = n; i >= 1; i--) { n       for (int j = 0; j <= r; j++) { n         long double res = 0; n         if (j >= s[i]) { n           res += (f[i] + dp[i + 1][j - f[i]]) * p[i]; n           res += (s[i] + dp[i + 1][j - s[i]]) * (1 - p[i]); n           res = min(res, mid); n           dp[i][j] = res; n           vis[i][j] = 1; n         } else if (j >= f[i]) { n           res += (f[i] + dp[i + 1][j - f[i]]) * p[i]; n           res += (s[i] + mid) * (1 - p[i]); n           res = min(res, mid); n           dp[i][j] = res; n           vis[i][j] = 1; n         } else { n           res += (f[i] + mid) * p[i]; n           res += (s[i] + mid) * (1 - p[i]); n           res = min(res, mid); n           dp[i][j] = mid; n           vis[i][j] = mid; n         } n       } n     } n     bool ok = 0; n     for (int j = 0; j <= r; j++) { n       if (!vis[1][j]) continue; n       if (dp[1][j] < mid) ok = 1; n     } n     if (ok) n       ans = mid, hi = mid; n     else n       lo = mid; n   } n   printf( %.20Lf n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, x, d; n int sad; n string s; n int main() { n   cin >> n >> x; n   while (n--) { n     string s; n     long long d; n     cin >> s >> d; n     if (s[0] ==  + ) { n       x += d; n     } else { n       if (d <= x) { n         x -= d; n       } else n         sad++; n     } n   } n   cout << x <<     << sad <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k, s; n   scanf( %I64d%I64d%I64d , &n, &k, &s); n   if (k * (n - 1) < s || s < k) n     printf( NO n ); n   else { n     long long sum = s, step = k; n     long long dis = n - 1; n     printf( YES n ); n     long long now = 1; n     for (int i = 1; i <= k; i++) { n       if ((step - 1) <= sum - dis) { n         if (now == 1) { n           if (i == 1) n             printf( %d , n); n           else n             printf(  %d , n); n           now = n; n         } else { n           if (i == 1) n             printf( %d , 1); n           else n             printf(  %d , 1); n           now = 1; n         } n         step -= 1; n         sum -= dis; n       } else { n         long long d = sum - step + 1; n         if (now - d <= 0) { n           if (i == 1) n             printf( %d , d + now); n           else n             printf(  %d , d + now); n           now = d + now; n         } else { n           if (i == 1) n             printf( %d , now - d); n           else n             printf(  %d , now - d); n           now = now - d; n         } n         step -= 1; n         sum -= d; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int n; n   cin >> n; n   if (n % 2 == 0) return cout <<  NO , 0; n   long long int arr[2 * n + 1]; n   long long int a = 1, b = n + 1; n   for (long long int i = 1; i < 2 * n + 1; i += 2) { n     if (a % 2) { n       arr[a++] = i; n       arr[b++] = i + 1; n     } else { n       arr[b++] = i; n       arr[a++] = i + 1; n     } n   } n   cout <<  YES n ; n   for (long long int i = 1; i < 2 * n + 1; i++) cout << arr[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005], b[100005]; n int main() { n   int i, j, n, p, k; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     b[i] = a[i]; n   } n   sort(b + 1, b + 1 + n); n   for (i = 1; i <= n; i++) { n     if (b[i] != a[i]) break; n   } n   p = i; n   if (i == n + 1) { n     printf( yes n ); n     printf( 1 1 ); n     return 0; n   } n   for (j = n; j >= 1; j--) { n     if (b[j] != a[j]) break; n   } n   k = j; n   int flag = 0; n   for (i = p, j = k; i <= k && j >= p; i++, j--) { n     if (b[i] != a[j]) { n       flag = 1; n       break; n     } n   } n   if (flag) { n     printf( no n ); n   } else { n     printf( yes n ); n     printf( %d %d n , p, k); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long double eps = 1e-9; n const int inf = numeric_limits<int>::max() / 2 - 1; n inline int run() { n   int n, a, b, k; n   cin >> n >> a >> b >> k; n   string s; n   cin >> s; n   int cur = 0; n   vector<int> ans; n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  0 ) cur++; n     if (s[i] ==  1 ) cur = 0; n     if (cur == b) ans.push_back(i), cur = 0; n   } n   for (int i = 0; i < a - 1 && ans.size() > 1; ++i) { n     ans.pop_back(); n   } n   cout << ans.size() << endl; n   for (auto i : ans) cout << i + 1 <<    ; n   return 0; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   run(); n   return 0; n } 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   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     bool ans = true; n     for (long long i = 0; i < k; i++) { n       char temp = s[i]; n       for (long long j = i; j < n; j += k) { n         if (temp ==  ?  && s[j] != temp) { n           temp = s[j]; n           s[i] = temp; n         } n         if (temp != s[j] && s[j] !=  ? ) { n           ans = false; n         } n       } n     } n     long long cnt1 = 0, cnt0 = 0, cnt = 0; n     for (long long i = 0; i < k; i++) { n       if (s[i] ==  1 ) { n         cnt1++; n       } else if (s[i] ==  0 ) { n         cnt0++; n       } else { n         cnt++; n       } n     } n     long long x = cnt - abs(cnt0 - cnt1); n     if (x < 0 || x % 2 == 1) { n       ans = false; n     } n     if (ans) { n       cout <<  YES  n            <<   n ; n     } else { n       cout <<  NO  n            <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n inline long long read() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n long long die = 0; n const double eps = 1e-8; n const long long mod = 1e9 + 7; n const long long N = 1e6 + 5; n long long n; n long long a[N]; n long long x, y; n bool mp[N]; n long long fac[N]; n long long inv[N]; n long long C(long long y, long long i) { n   return fac[y] * inv[i] % mod * inv[y - i] % mod; n } n void sol1() { n   x = y = 0; n   for (long long i = (1); i <= (n); i++) n     if (a[i] == -1) { n       if (mp[i]) n         x++; n       else n         y++; n     } n   n = x + y; n   inv[0] = inv[1] = fac[0] = 1; n   for (long long i = (1); i <= (n); i++) fac[i] = fac[i - 1] * i % mod; n   for (long long i = (2); i <= (n); i++) n     inv[i] = (mod - mod / i) * inv[mod % i] % mod; n   for (long long i = (2); i <= (n); i++) (inv[i] *= inv[i - 1]) %= mod; n   long long ans = fac[n]; n   for (long long i = (1); i <= (y); i++) n     (ans += ((i & 1) ? -1 : 1) * C(y, i) * fac[n - i] % mod) %= mod; n   (ans += mod) %= mod; n   printf( %I64d n , ans); n } n void rd() { n   n = read(); n   for (long long i = (1); i <= (n); i++) { n     a[i] = read(); n     if (a[i] != -1) mp[a[i]] = 1; n   } n   sol1(); n } n signed main() { rd(); } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> v; n   vector<int>::iterator j; n   int n, i; n   cin >> n; n   n = 2 * n; n   int cnt = 0; n   int x[n]; n   int cnt2 = 0; n   for (i = 0; i < n; i++) { n     cin >> x[i]; n     if (i % 2 == 0) { n       if (x[i] < 0) { n         cnt++; n       } else if (x[i] > 0) { n         cnt2++; n       } n     } n   } n   if (cnt <= 1 || cnt2 == 1) { n     cout <<  Yes ; n     return 0; n   } else if (cnt == n / 2 || cnt2 == n / 2) { n     cout <<  Yes ; n     return 0; n   } n   cout <<  No ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 5; n char s[MAXN]; n int lst = 1, cnt = 1, len[MAXN]; n int fa[MAXN], ch[MAXN][26]; n int siz[MAXN], c[MAXN], ord[MAXN]; n inline int read() { n   int x = 0; n   char ch = getchar(); n   while (!isdigit(ch)) ch = getchar(); n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x; n } n inline void insert(int c) { n   int p = lst, np = lst = ++cnt; n   len[np] = len[p] + 1, siz[np] = 1; n   while (p && !ch[p][c]) ch[p][c] = np, p = fa[p]; n   if (!p) return fa[np] = 1, void(); n   int q = ch[p][c]; n   if (len[q] == len[p] + 1) return fa[np] = q, void(); n   int nq = ++cnt; n   fa[nq] = fa[q], fa[q] = fa[np] = nq; n   len[nq] = len[p] + 1, memcpy(ch[nq], ch[q], sizeof(ch[q])); n   while (ch[p][c] == q) ch[p][c] = nq, p = fa[p]; n } n inline void clear() { n   lst = cnt = 1; n   memset(c, 0, sizeof(c)); n   memset(fa, 0, sizeof(fa)); n   memset(ch, 0, sizeof(ch)); n   memset(siz, 0, sizeof(siz)); n } n inline long long solve() { n   long long ans = 0; n   for (int i = 1; i <= cnt; i++) c[len[i]]++; n   for (int i = 1; i <= cnt; i++) c[i] += c[i - 1]; n   for (int i = cnt; i; i--) ord[c[len[i]]--] = i; n   for (int i = cnt; i; i--) siz[fa[ord[i]]] += siz[ord[i]]; n   for (int i = 1; i <= cnt; i++) n     ans += 1ll * (len[i] - len[fa[i]]) * siz[i] * siz[i]; n   return ans; n } n int main() { n   int t = read(); n   while (t--) { n     clear(); n     scanf( %s , s + 1); n     int n = strlen(s + 1); n     for (int i = 1; i <= n; i++) insert(s[i] -  a ); n     printf( %lld n , solve()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<pair<int, int>>> g; n vector<int> used; n vector<int> tout; n int timer = 0; n bool dfs(int v, int cost) { n   timer++; n   used[v] = 1; n   for (auto to : g[v]) { n     if (to.second > cost) { n       if (used[to.first] == 0) { n         if (!dfs(to.first, cost)) { n           return false; n         } n       } else { n         if (used[to.first] == 1) { n           return false; n         } n       } n     } n   } n   used[v] = 2; n   ++timer; n   tout[v] = timer; n   return true; n } n struct edge { n   int a, b, c, index; n }; n int main() { n   int n, m; n   int a, b, c; n   cin >> n >> m; n   g.resize(n); n   tout.resize(n); n   vector<pair<int, int>> index; n   vector<edge> E; n   for (int i = 0; i < m; i++) { n     cin >> a >> b >> c; n     --a; n     --b; n     g[a].push_back(make_pair(b, c)); n     index.push_back(make_pair(i + 1, c)); n     edge e; n     e.a = a; n     e.b = b; n     e.c = c; n     e.index = i + 1; n     E.push_back(e); n   } n   int L = -1; n   int R = 1e9; n   while (R - L > 1) { n     int M = (R + L) / 2; n     used.clear(); n     used.resize(n, 0); n     bool q = true; n     for (int i = 0; i < n; i++) { n       if (used[i] == 0) { n         if (!dfs(i, M)) { n           q = false; n           break; n         } n       } n     } n     if (q) { n       R = M; n     } else { n       L = M; n     } n   } n   int k = 0; n   timer = 0; n   used.clear(); n   used.resize(n, 0); n   bool q = true; n   for (int i = 0; i < n; i++) { n     if (used[i] == 0) { n       if (!dfs(i, R)) { n         q = false; n         break; n       } n     } n   } n   vector<int> ans; n   for (int i = 0; i < m; i++) { n     if (E[i].c <= R && tout[E[i].a] <= tout[E[i].b]) { n       k += 1; n       ans.push_back(E[i].index); n     } n   } n   cout << R <<     << k << endl; n   for (int i = 0; i < k; i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 210, M = 50 * 1000 + 10; n int G, S, n, m, par[N]; n int get_par(int v) { return par[v] == v ? v : par[v] = get_par(par[v]); } n struct edge { n   int v = 0, u = 0, g = 0, s = 0; n   bool operator<(edge b) const { n     return make_pair(make_pair(s, v), u) < make_pair(make_pair(b.s, b.v), b.u); n   } n } e[M]; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m >> G >> S; n   for (int i = 0; i < m; i++) cin >> e[i].v >> e[i].u >> e[i].g >> e[i].s; n   sort(e, e + m, [](edge x, edge y) { return x.g < y.g; }); n   long long ans = 1LL << 62; n   set<edge> E; n   for (int i = 0; i < m; i++) { n     E.insert(e[i]); n     iota(par, par + n + 1, 0); n     edge goodbye; n     for (auto e : E) { n       int v = e.v, u = e.u; n       v = get_par(v), u = get_par(u); n       if (v == u) n         goodbye = e; n       else n         par[u] = v; n     } n     E.erase(goodbye); n     if ((int)(E.size()) == n - 1) n       ans = min(1LL * G * e[i].g + 1LL * S * (*--E.end()).s, ans); n   } n   cout << (ans < 1LL << 62 ? ans : -1) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, a[100001], b[100001], flag = 1; n     cin >> n; n     vector<int> v(2, 0); n     for (int i = 0; i < n; i++) cin >> a[i]; n     for (int j = 0; j < n; j++) cin >> b[j]; n     for (int i = 0; i < n; i++) { n       if (a[i] < b[i] && v[1] != 1) { n         flag = 0; n         break; n       } else if (a[i] > b[i] && v[0] != -1) { n         flag = 0; n         break; n       } n       if (a[i] == 1) v[1] = 1; n       if (a[i] == -1) v[0] = -1; n     } n     if (flag == 0) n       cout <<  NO n ; n     else n       cout <<  YES n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int arr[10005]; n bool readl[10005]; n int read_pages(int ini) { n   int curr = ini; n   readl[ini] = 1; n   while (curr != arr[curr]) { n     curr = arr[curr]; n     readl[curr] = true; n   } n   return curr; n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> arr[i]; n   } n   int days = 0; n   for (int i = 1; i <= n; i++) { n     int curr = read_pages(i); n     for (int j = i; j <= curr; j++) { n       if (!readl[j]) { n         curr = max(read_pages(j), curr); n       } n     } n     i = curr; n     days++; n   } n   cout << days; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   set<char> ST; n   for (char x : s) ST.insert(x); n   if (k > n) { n     for (int i = 0; i < n; i++) cout << s[i]; n     for (int i = n; i < k; i++) cout << *ST.begin(); n     return; n   } n   for (int i = k - 1; i >= 0; i--) { n     if (s[i] < *ST.rbegin()) { n       string f =   ; n       for (int j = 0; j < i; j++) f += s[j]; n       char mnThatWorkz =  z ; n       for (char c : ST) { n         if (c > s[i] && mnThatWorkz > c) mnThatWorkz = c; n       } n       f += mnThatWorkz; n       while ((int)f.size() < k) f += *ST.begin(); n       cout << f <<   n ; n       return; n     } n   } n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void print(const T niz[], const int siz) { n   for (int i = 0; i < siz; i++) cout << niz[i] <<    ; n   cout << endl; n } n const long long p = 103; n long long add(long long x, long long y) { return (x + y) % 1000000007; } n long long mul(long long x, long long y) { return (x * y) % 1000000007; } n long long power(long long x, long long y) { n   if (y == 0) return 1; n   long long pola = power(x, y / 2); n   pola = mul(pola, pola); n   if (y % 2 == 1) pola = mul(pola, x); n   return pola; n } n long long inverz[1000005]; n int n; n string x; n long long hes[1000005][2]; n long long get(long long l, long long r, long long idx) { n   if (idx == 0) { n     long long sta = hes[r][0] - hes[l - 1][0]; n     sta %= 1000000007; n     sta += 1000000007; n     sta %= 1000000007; n     sta = mul(sta, inverz[l - 1]); n     return sta; n   } else { n     long long sta = hes[l][1] - hes[r + 1][1]; n     sta %= 1000000007; n     sta += 1000000007; n     sta %= 1000000007; n     sta = mul(sta, inverz[n - r]); n     return sta; n   } n } n bool checkpal(int l, int r) { n   l = max(l, 0); n   r = min(r, n); n   if (l == r) return 1; n   if (l > r) return 0; n   long long sta1 = get(l, r, 0); n   long long sta2 = get(l, r, 1); n   return (sta1 == sta2); n } n int maksn = 0; n void solve() { n   cin >> x; n   n = x.length(); n   maksn = max(maksn, n); n   for (int(i) = (0); (i) <= (maksn + 1); ++(i)) { n     hes[i][0] = hes[i][1] = 0; n   } n   long long st = 1; n   long long tr = 0; n   for (int(i) = (0); (i) <= (n - 1); ++(i)) { n     tr = add(tr, mul(st, x[i] -  a  + 1)); n     hes[i + 1][0] = tr; n     st = mul(st, p); n   } n   tr = 0; n   st = 1; n   for (int(i) = (n - 1); (i) >= (0); --(i)) { n     tr = add(tr, mul(st, x[i] -  a  + 1)); n     hes[i + 1][1] = tr; n     st = mul(st, p); n   } n   int l = 1; n   int r = n; n   if (checkpal(1, n)) { n     cout << x << endl; n     return; n   } n   int necul = 0; n   int necur = 0; n   while (l < r) { n     if (x[l - 1] != x[r - 1]) { n       for (int(i) = (l); (i) <= (r); ++(i)) { n         if (checkpal(l, i)) { n           necul = i + 1; n           necur = r; n         } n       } n       for (int(i) = (r); (i) >= (l); --(i)) { n         if (checkpal(i, r)) { n           if ((i - 1 - l) < (necur - necul)) { n             necur = i - 1; n             necul = l; n           } n         } n       } n       break; n     } else { n       l++; n       r--; n     } n   } n   for (int(i) = (1); (i) <= (n); ++(i)) { n     if (i >= necul && i <= necur) continue; n     cout << x[i - 1]; n   } n   cout << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   inverz[1000001] = power(power(p, 1000001), 1000000007 - 2); n   for (int(i) = (1000000); (i) >= (0); --(i)) inverz[i] = mul(inverz[i + 1], p); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, k, i; n   cin >> n >> k; n   long long int a[n + 1], b[n + 1]; n   pair<long long int, long long int> d[n + 1]; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < n; i++) cin >> b[i]; n   for (i = 0; i < n; i++) d[i] = make_pair(a[i] - b[i], i); n   sort(d, d + n); n   long long int s = 0; n   for (i = 0; i < k; i++) s += a[d[i].second]; n   for (i = k; i < n; i++) { n     s += min(a[d[i].second], b[d[i].second]); n   } n   cout << s << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long dev[5000005]; n long long S[5000005]; n long long pri[5000005]; n void sieve() { n   memset(dev, -1, sizeof dev); n   int p = 0; n   for (int i = 2; i <= 5000005; i++) { n     if (dev[i] != -1) continue; n     pri[p++] = i; n     for (long long j = 1; j < 5000005; j++) { n       if (i * j >= 5000005) break; n       dev[i * j] = i; n     } n   } n } n int main() { n   sieve(); n   for (int i = 1; i < 5000005; i++) { n     int x = i; n     while (x > 1) { n       int d = dev[x]; n       S[i]++; n       x /= d; n     } n   } n   for (int i = 2; i <= 5000005; i++) { n     S[i] += S[i - 1]; n   } n   int t; n   cin >> t; n   while (t--) { n     int a, b; n     scanf( %d%d , &a, &b); n     printf( %I64d n , S[a] - S[b]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int N = 1e5 + 5; n ll f[N]; n int n, ans, c; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %lld , &f[i]); n   for (int i = 2; i < n; i++) { n     if (f[i] == f[i - 1] + f[i - 2]) { n       c++; n       ans = max(ans, c + 2); n     } else { n       c = 0; n     } n   } n   printf( %d n , (ans == 0 || ans == 1) ? (n == 1 ? 1 : 2) : ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[100001], w[100001]; n vector<pair<int, int>> e[100001]; n long long ans[100001], sum; n int root(int x) { n   while (x != p[x]) x = p[x] = p[p[x]]; n   return x; n } n void pre(int x, int from) { n   sum += w[x]; n   for (auto c : e[x]) n     if (c.first != from) pre(c.first, x); n } n int gao(int x, int from, int t) { n   int cnt = w[x]; n   for (auto c : e[x]) { n     if (c.first == from) continue; n     cnt += gao(c.first, x, c.second); n   } n   e[x].clear(); n   ans[t] = cnt * (sum - cnt) * 2; n   return cnt; n } n int main() { n   int n; n   scanf( %d , &n); n   map<int, vector<pair<pair<int, int>, int>>> u; n   for (int i = 1; i <= n; i++) w[p[i] = i] = 1; n   for (int i = 1; i != n; i++) { n     int x, y, c; n     scanf( %d%d%d , &x, &y, &c); n     u[c].push_back({{x, y}, i}); n   } n   for (auto& r : u) { n     vector<int> v; n     for (auto c : r.second) { n       int x = root(c.first.first); n       int y = root(c.first.second); n       int t = c.second; n       e[x].push_back({y, t}); n       e[y].push_back({x, t}); n       v.push_back(x); n       v.push_back(y); n     } n     sort(v.begin(), v.end()); n     v.resize(unique(v.begin(), v.end()) - v.begin()); n     for (auto x : v) n       if (!e[x].empty()) { n         pre(x, sum = 0); n         gao(x, 0, 0); n       } n     for (auto c : r.second) { n       int x = root(c.first.first); n       int y = root(c.first.second); n       if (w[x] < w[y]) n         w[p[x] = y] += w[x]; n       else n         w[p[y] = x] += w[y]; n     } n   } n   long long tmp = *max_element(ans + 1, ans + n); n   printf( %I64d %d n , tmp, count(ans + 1, ans + n, tmp)); n   for (int i = 1; i != n; i++) n     if (ans[i] == tmp) printf( %d  , i); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n char c[maxn]; n long long mod = 1e9 + 7; n long long ex(long long a, long long b) { n   a = a % mod; n   long long ans = 1; n   while (b) { n     if (b % 2 == 1) { n       ans = (ans * a) % mod; n     } n     a = (a * a) % mod; n     b = b / 2; n   } n   return ans; n } n int main() { n   int k; n   scanf( %s%d , c, &k); n   long long sum = 0; n   int len = strlen(c); n   for (int i = 0; c[i]; i++) { n     if (c[i] ==  0  || c[i] ==  5 ) { n       long long a1 = ex(2, i); n       long long q = ex(2, len); n       long long ans = ((a1 % mod) * ((ex(q, k) - 1) % mod) % mod); n       long long ni = ex(q - 1, mod - 2) % mod; n       sum = (sum + (ans * ni) % mod) % mod; n     } n   } n   printf( %I64d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n istream &operator>>(istream &is, vector<T> &arr); n template <typename T> n ostream &operator<<(ostream &os, vector<T> &arr); n int INF = 1e9 + 5; n long long C(int n, int m) { n   long long ans = 1; n   for (int i = 1; i <= m; i++) { n     ans *= n; n     ans /= i; n     n--; n   } n   return ans; n } n int main() { n   int n; n   cin >> n; n   vector<pair<int, string>> scores = {{0,  Kuro }, {0,  Shiro }, {0,  Katie }}; n   vector<string> tr(3); n   cin >> tr; n   for (int i = 0; i < 3; i++) { n     map<char, int> charMap; n     for (int j = 0; j < tr[i].length(); j++) { n       charMap[tr[i][j]]++; n     } n     for (auto ca : charMap) { n       int sc = ca.second; n       int l = tr[i].length(); n       if (n == 1 && sc == l) n         scores[i].first = max(scores[i].first, sc - 1); n       else n         scores[i].first = max(scores[i].first, min(sc + n, l)); n     } n   } n   sort(scores.begin(), scores.end()); n   if (scores[2].first == scores[1].first) n     cout <<  Draw  << endl; n   else n     cout << scores[2].second << endl; n } n template <typename T> n istream &operator>>(istream &is, vector<T> &arr) { n   typename vector<T>::iterator a; n   for (a = arr.begin(); a != arr.end(); a++) { n     is >> *a; n   } n   return is; n } n template <typename T> n ostream &operator<<(ostream &os, vector<T> &arr) { n   typename vector<T>::iterator a; n   for (a = arr.begin(); a != arr.end(); a++) { n     os << *a <<    ; n   } n   os << endl; n   return os; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     cout << n / 2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, count = 0, max = 0; n   char line; n   cin >> n; n   n++; n   while (n--) { n     char temp; n     scanf( %c , &temp); n     if (temp ==    ) { n       if (count > max) max = count; n       count = 0; n     } else if (temp >=  A  && temp <=  Z ) n       count++; n   } n   if (count > max) max = count; n   cout << max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int x1, y1, x2, y2; n     cin >> x1 >> y1 >> x2 >> y2; n     if (y1 == y2) n       cout << abs(x2 - x1) <<   n ; n     else if (x1 == x2) n       cout << abs(y2 - y1) <<   n ; n     else { n       int x, y; n       x = abs(x2 - x1); n       y = abs(y2 - y1); n       cout << x + y + 2 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n vector<int> graph[maxn]; n vector<int> g[maxn]; n bool mark[maxn]; n int n, m; n int par[maxn]; n int cm[maxn]; n bool comp[maxn]; n int mk[maxn]; n int moalefe[maxn]; n void dfs(int x, int y) { n   mark[x] = 1; n   cm[x] = y; n   moalefe[y]++; n   for (int v : graph[x]) n     if (!mark[v]) dfs(v, y); n } n void ddfs(int x) { n   mk[x] = 1; n   for (int v : g[x]) { n     if (mk[v] == 0) n       ddfs(v); n     else if (mk[v] == 1) n       comp[cm[v]] = 1; n   } n   mk[x] = 2; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int t1, t2; n     cin >> t1 >> t2; n     t1--; n     t2--; n     graph[t1].push_back(t2); n     graph[t2].push_back(t1); n     g[t1].push_back(t2); n   } n   int mo = 1; n   for (int i = 0; i < n; i++) { n     if (!mark[i]) { n       dfs(i, mo); n       mo++; n     } n   } n   for (int i = 0; i < n; i++) { n     if (mk[i] == 0) ddfs(i); n   } n   int ans = 0; n   for (int i = 1; i < mo; i++) { n     if (comp[i]) n       ans += moalefe[i]; n     else n       ans += moalefe[i] - 1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n const unsigned int M = 1000000007; n long long power(long long x, long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = x * res; n     y = y >> 1; n     x = x * x; n   } n   return res; n } n long long power(long long x, long long y, long long mod) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = x * res % mod; n     y = y >> 1; n     x = x * x % mod; n   } n   return res; n } n using namespace std; n void nik() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n signed main() { n   nik(); n   long long test = 1; n   cin >> test; n   while (test--) { n     long long one[100005] = {0}; n     string s; n     cin >> s; n     long long n = s.size(); n     s =     + s; n     for (long long i = 1; i <= n; i++) { n       if (s[i] ==  1 ) n         one[i] = one[i - 1] + 1; n       else n         one[i] = one[i - 1]; n     } n     long long ans = min(one[n], n - one[n]); n     if (ans == 0) { n       cout << ans <<   n ; n       continue; n     } n     for (long long i = 1; i <= n; i++) { n       ans = min(ans, i - one[i] + one[n] - one[i]); n     } n     long long b = ans; n     for (long long i = n; i >= 1; i--) { n       b = min(b, (n - i + 1) - (one[n] - one[i - 1]) + one[i - 1]); n     } n     cout << min(ans, b) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> gru; n   for (int i = 0; i < n; i++) { n     gru.push_back(i + 1); n   } n   long long lead = 1; n   for (int i = 0; i < k; i++) { n     long long a; n     cin >> a; n     lead += a; n     lead = lead % gru.size(); n     if (lead == 0) { n       lead = gru.size(); n     } n     cout << gru[lead - 1] <<    ; n     gru.erase(gru.begin() + lead - 1); n   } n } n


#include <bits/stdc++.h> n #define debug(a) cout << #a <<  :   << a <<   n  n #define debugLine() cout <<  ============== n  n #define tick() cout <<  Tick n ; n #define testCases() int t; cin >> t; while(t--) n #define all(a) a.begin(), a.end() n #define fillWith(a, b) memset(a, b, sizeof(a)) n #define INF_32 1e9 n #define INF_64 1e18 n #define Mod 1000000007 n #define PI 3.14159265358979323846 n #define endl   n  n #define goFast() ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) n typedef long long ll; n using namespace std; n  n int main() n { n     goFast(); n     testCases() n     { n         ll n; n         cin >> n; n         ll arr[n + 3]; n         ll sum = 0; n         for(int i = 0; i < n; i++) n         { n             cin >> arr[i]; n             sum += arr[i]; n         } n         cout << (n - sum % n) * (sum % n) << endl; n     } n }


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 50; n const int INF = 0x3f3f3f3f; n struct node { n   int to, next; n } edge[maxn * 3]; n struct Node { n   int id; n   long long val; n   bool operator<(const Node& a) const { return val < a.val; } n }; n int head[maxn], cnt, d[maxn], depth[maxn], ans[maxn]; n int s[maxn]; n void add(int u, int v) { n   edge[cnt].to = v; n   edge[cnt].next = head[u]; n   head[u] = cnt++; n } n void dfs(int k, int fa) { n   for (int i = head[k]; i != -1; i = edge[i].next) { n     int to = edge[i].to; n     if (to == fa) continue; n     depth[to] = depth[k] + 1; n     dfs(to, k); n     s[k] += s[to] + 1; n   } n } n int main() { n   int n, k; n   cnt = 0; n   memset(head, -1, sizeof(head)); n   memset(depth, -1, sizeof(depth)); n   memset(s, 0, sizeof(s)); n   scanf( %d%d , &n, &k); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     add(u, v); n     add(v, u); n     d[u]++; n     d[v]++; n   } n   depth[1] = 0; n   dfs(1, 1); n   priority_queue<Node> q; n   for (int i = 1; i <= n; i++) n     if (d[i] == 1 && i != 1) q.push({i, depth[i]}); n   long long sum = 0; n   while (k && !q.empty()) { n     Node top = q.top(); n     sum += top.val; n     q.pop(); n     k--; n     for (int i = head[top.id]; i != -1; i = edge[i].next) { n       int to = edge[i].to; n       if (depth[to] < depth[top.id]) { n         d[to]--; n         if (d[to] == 1) q.push({to, depth[to] - s[to]}); n         break; n       } n     } n   } n   printf( %lld n , sum); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void in(T &x) { n   x = 0; n   bool f = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = 1; n     c = getchar(); n   } n   while ( 0  <= c && c <=  9 ) { n     x = (x << 3) + (x << 1) + (c ^ 48); n     c = getchar(); n   } n   if (f) x = -x; n } n long long n, m; n void print(long long x) { n   printf( %lld , x); n   exit(0); n } n signed main() { n   in(n), in(m); n   if (n > m) swap(n, m); n   if (n == 1) print(m - m % 6 + max(0ll, (m % 6 - 3) * 2)); n   if ((n & 1) == 0 && (m & 1) == 0) { n     if (n == 2 && m == 2) n       print(0); n     else n       print(n * m); n   } n   if ((n + m) & 1) { n     if (m & 1) swap(n, m); n     if (m == 2) { n       if (n == 3) print(4); n       if (n == 7) print(12); n     } n     print(n * m); n   } else n     print(n * m - 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n void solve() { n   int n, k, d; n   cin >> n >> k >> d; n   vector<int> dist(n, -1); n   vector<vector<pair<int, int>>> za(n); n   vector<int> keep(n - 1), par(n); n   queue<int> q; n   for (int i = (0); i < (k); ++i) { n     int p; n     cin >> p; n     --p; n     dist[p] = 0; n     q.push(p); n     par[p] = -1; n   } n   for (int i = (0); i < (n - 1); ++i) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     za[x].emplace_back(y, i); n     za[y].emplace_back(x, i); n   } n   int res = 0; n   while (!q.empty()) { n     int cur = q.front(); n     q.pop(); n     for (auto edge : za[cur]) { n       int u = edge.first; n       int edge_no = edge.second; n       if (dist[u] == -1) { n         ++res; n         dist[u] = dist[cur] + 1; n         keep[edge_no] = 1; n         q.push(u); n       } n     } n   } n   vector<int> shut; n   for (int i = (0); i < (n - 1); ++i) { n     if (keep[i] == 0) { n       shut.push_back(i + 1); n     } n   } n   cout << ((int)((shut).size())) << endl; n   for (auto s : shut) { n     cout << s <<    ; n   } n   cout << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t; n   t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int qq, n, m, k, l, r; n string s, s1, s2, s3; n int INF = (int)2e9 + 7; n struct st { n   int x, y; n }; n bool operator<(st& a, st& b) { n   if (a.x != b.x) return a.x < b.x; n   return a.y < b.y; n } n vector<vector<char>> v; n int x[4] = {0, 0, -1, 1}, y[4] = {-1, 1, 0, 0}; n void dfs(int i, int j) { n   v[i][j] =  . ; n   for (int ii = 0; ii < 4; ii++) { n     if (v[i + x[ii]][j + y[ii]] ==  B ) dfs(i + x[ii], j + y[ii]); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int k1, k2, c; n   char e; n   cin >> n >> m; n   int ans = 0; n   v.assign(n + 2, vector<char>(m + 2)); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> e; n       v[i][j] = e; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (v[i][j] ==  B ) { n         dfs(i, j); n         ans++; n       } n     } n   } n   cout << ans <<    ; n   cin >> k; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fre() { n   freopen( c://test//input.in ,  r , stdin); n   freopen( c://test//output.out ,  w , stdout); n } n template <class T1, class T2> n inline void gmax(T1 &a, T2 b) { n   if (b > a) a = b; n } n template <class T1, class T2> n inline void gmin(T1 &a, T2 b) { n   if (b < a) a = b; n } n const int N = 1e5 + 10, inf = 0x3f3f3f3f; n int n, Z; n long long mul[N]; n long long inv[N]; n vector<pair<int, int> > a[N]; n int sz[N]; n int mxp[N]; n bool done[N]; n int root; n int treesz; n map<int, int> mop; n int getsz(int x, int fa) { n   sz[x] = 1; n   for (int i = a[x].size() - 1; ~i; --i) { n     int y = a[x][i].first; n     if (y == fa || done[y]) continue; n     sz[x] += getsz(y, x); n   } n   return sz[x]; n } n void getroot(int x, int fa) { n   mxp[x] = treesz - sz[x]; n   for (auto &it : a[x]) { n     int y = it.first; n     if (y == fa || done[y]) continue; n     getroot(y, x); n     gmax(mxp[x], sz[y]); n   } n   if (mxp[x] < mxp[root]) root = x; n } n long long ans, sig; n void dfs1(int x, int fa, int val, int dep) { n   sig += (val == 0); n   ans += mop[(Z - val) * inv[dep] % Z]; n   for (auto &it : a[x]) { n     if (it.first == fa || done[it.first]) continue; n     dfs1(it.first, x, (val * 10ll + it.second) % Z, dep + 1); n   } n } n void dfs2(int x, int fa, int val, int dep) { n   sig += (val == 0); n   ++mop[val]; n   for (auto &it : a[x]) { n     if (it.first == fa || done[it.first]) continue; n     dfs2(it.first, x, (val + it.second * mul[dep]) % Z, dep + 1); n   } n } n void cal(int x) { n   mop.clear(); n   for (auto &it : a[x]) { n     int y = it.first; n     if (done[y]) continue; n     int z = it.second; n     dfs1(y, x, z, 1); n     dfs2(y, x, z, 1); n   } n   reverse(a[x].begin(), a[x].end()); n   mop.clear(); n   for (auto &it : a[x]) { n     int y = it.first; n     if (done[y]) continue; n     int z = it.second; n     dfs1(y, x, z, 1); n     dfs2(y, x, z, 1); n   } n } n void dfz(int x) { n   treesz = getsz(x, 0); n   getroot(x, root = 0); n   cal(root); n   done[root] = 1; n   for (auto &it : a[root]) n     if (!done[it.first]) dfz(it.first); n } n void exgcd(long long a, long long b, long long &d, long long &x, long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     d = a; n   } else { n     exgcd(b, a % b, d, y, x); n     y -= a / b * x; n   } n } n long long INV(long long a, long long Z) { n   long long x, y, d; n   exgcd(a, Z, d, x, y); n   if (d == 1) n     return (x % Z + Z) % Z; n   else n     return -1; n } n int main() { n   mxp[0] = inf; n   while (~scanf( %d%d , &n, &Z)) { n     mul[0] = 1; n     mul[1] = 10 % Z; n     inv[0] = 1; n     inv[1] = INV(10, Z); n     for (int i = 2; i <= n; ++i) { n       mul[i] = mul[i - 1] * 10 % Z; n       inv[i] = inv[i - 1] * inv[1] % Z; n     } n     for (int i = 1; i <= n; ++i) { n       a[i].clear(); n       done[i] = 0; n     } n     for (int i = 1; i < n; ++i) { n       int x, y, z; n       scanf( %d%d%d , &x, &y, &z); n       ++x; n       ++y; n       z %= Z; n       a[x].push_back({y, z}); n       a[y].push_back({x, z}); n     } n     ans = sig = 0; n     treesz = n; n     dfz(1); n     printf( %lld n , ans + sig / 2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> adj[100001]; n vector<int> cost[3]; n long long matrix[3][3] = {0}; n int tree[100000]; n int output[100000] = {0}; n void makeTree(int a, int pos) { n   tree[pos] = a; n   for (auto &i : adj[a]) { n     if (pos == 0 || tree[pos - 1] != i) makeTree(i, pos + 1); n   } n } n long long calc(int a, int b, int i) { n   if (i == n) return 0; n   int c = 0 ^ 1 ^ 2 ^ a ^ b; n   return cost[c][tree[i] - 1] + calc(b, c, i + 1); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> n; n   for (int i = 0; i < int(3); i++) n     for (int j = 0; j < int(n); j++) { n       int x; n       cin >> x; n       cost[i].push_back(x); n     } n   for (int i = 0; i < int(n - 1); i++) { n     int a, b; n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   int counter = 0; n   int lf; n   for (int i = 0; i < int(n); i++) { n     if (adj[i + 1].size() == 1) { n       lf = i + 1; n       counter++; n     } n     if (counter > 2) { n       cout <<  -1 ; n       return 0; n     } n   } n   makeTree(lf, 0); n   long long sml = pow(10, 15); n   int x, y; n   for (int i = 0; i < int(3); i++) { n     for (int j = 0; j < int(3); j++) { n       if (i == j) continue; n       matrix[i][j] = n           cost[i][tree[0] - 1] + cost[j][tree[1] - 1] + calc(i, j, 2); n       sml = min(sml, matrix[i][j]); n       if (sml == matrix[i][j]) { n         x = i; n         y = j; n       } n     } n   } n   cout << sml <<   n ; n   output[tree[0] - 1] = x; n   output[tree[1] - 1] = y; n   for (int i = 2; i < n; i++) { n     int c = 0 ^ 1 ^ 2 ^ x ^ y; n     output[tree[i] - 1] = c; n     x = y; n     y = c; n   } n   for (int i = 0; i < int(n); i++) cout << output[i] + 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, x[1010], y[1010], i, j, l, r[1010], w[1010], dad[1010], ss, flag, n     use[1010], ke, t; n int rr[1010], ww[1010], use1[1010]; n char s1[1010][20], s[20]; n int fi(int x) { return dad[x] == x ? x : dad[x] = fi(dad[x]); } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (i = 1; i <= n; i++) dad[i] = i; n   for (i = 1; i <= m; i++) scanf( %d%d , &x[i], &y[i]); n   for (i = 1; i <= k; i++) { n     scanf( %s%d%d , s1[i], &r[i], &ss); n     for (j = 1; j <= ss; j++) scanf( %d , &t), w[t] = i; n   } n   for (; !flag;) { n     flag = 1; n     for (i = 1; i <= m; i++) n       if (!use[i]) n         if ((fi(x[i]) == fi(r[w[i]])) || (fi(y[i]) == fi(r[w[i]]))) { n           flag = 0, use[i] = 1; n           if (fi(x[i]) != fi(y[i])) dad[fi(x[i])] = fi(y[i]); n         } n   } n   for (i = 1; i <= k; i++) { n     scanf( %s%d%d , s, &rr[i], &ss); n     for (j = 1; j <= k; j++) n       if (!strcmp(s1[j], s)) break; n     if (fi(rr[i]) != fi(r[j])) { n       printf( NO n ); n       return 0; n     } n     for (l = 1; l <= ss; l++) { n       scanf( %d , &ke); n       ww[ke] = i; n       if (fi(r[w[ke]]) != fi(rr[i])) { n         printf( NO n ); n         return 0; n       } n     } n   } n   for (i = 1; i <= n; i++) dad[i] = i; n   for (i = 1; i <= m; i++) use1[i] = use[i], use[i] = 0; n   for (flag = 0; !flag;) { n     flag = 1; n     for (i = 1; i <= m; i++) n       if (!use[i]) n         if ((fi(x[i]) == fi(rr[ww[i]])) || (fi(y[i]) == fi(rr[ww[i]]))) { n           flag = 0, use[i] = 1; n           if (fi(x[i]) != fi(y[i])) dad[fi(x[i])] = fi(y[i]); n         } n   } n   for (i = 1; i <= m; i++) n     if (use1[i] != use[i]) { n       printf( NO n ); n       return 0; n     } n   printf( YES n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j, sum = 1, sum2 = 2, t = 0, k = 0; n   cin >> n; n   int a[n], b[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (i = 0; i < n; i++) { n     if (a[i] == 1) t++; n     if (a[i + 1] == 1 || i == n - 1) { n       b[k] = a[i]; n       k++; n     } n   } n   cout << t <<   n ; n   for (i = 0; i < k; i++) { n     cout << b[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int N; n long long shu[200010]; n int main() { n   while (scanf( %d , &N) != EOF) { n     for (int i = 1; i <= N * 2; i++) { n       scanf( %I64d , &shu[i]); n     } n     sort(shu + 1, shu + 1 + 2 * N); n     long long ans = (shu[N] - shu[1]) * (shu[2 * N] - shu[N + 1]); n     for (int i = 2; i <= N; i++) { n       ans = min(ans, (shu[i + N - 1] - shu[i]) * (shu[2 * N] - shu[1])); n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int w, h; n   cin >> w >> h; n   char char1[100][100], char2[100][100]; n   string char3[200]; n   for (int i(0); i < h; ++i) cin >> char1[i]; n   for (int i(0); i < w; ++i) { n     for (int j(0); j < h; ++j) { n       char2[i][j] = char1[abs(j - h + 1)][i]; n     } n   } n   for (int i(0); i < w; ++i) { n     for (int j(0); j < h / 2; ++j) { n       char tmp = char2[i][j]; n       char2[i][j] = char2[i][abs(j - h + 1)]; n       char2[i][abs(j - h + 1)] = tmp; n     } n   } n   for (int i(0); i < 2 * w; i += 2) { n     for (int j(0); j < h; ++j) { n       char3[i].insert(char3[i].end(), char2[i / 2][j]); n       char3[i].insert(char3[i].end(), char2[i / 2][j]); n     } n     char3[i + 1] = char3[i]; n   } n   for (int i(0); i < 2 * w; ++i) cout << char3[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n; n   bool val = false; n   cin >> n; n   char array[n][5]; n   for (int i = 0; i < n; i++) { n     cin >> array[i]; n   } n   for (int i = 0; i < n; i++) { n     if (array[i][0] ==  O  & array[i][1] ==  O ) { n       val = true; n       array[i][0] =  + ; n       array[i][1] =  + ; n       break; n     } n     if (array[i][3] ==  O  & array[i][4] ==  O ) { n       val = true; n       array[i][3] =  + ; n       array[i][4] =  + ; n       break; n     } n   } n   if (val == true) { n     cout <<  YES  <<   n ; n   } else if (val == false) n     cout <<  NO ; n   if (val == true) { n     for (int i = 0; i < n; i++) { n       cout << array[i][0]; n       cout << array[i][1]; n       cout << array[i][2]; n       cout << array[i][3]; n       cout << array[i][4] <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[110]; n long long ans; n int main() { n   ans = 0LL; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1; i <= n; ++i) { n     ans += (long long)a[i]; n     ans += (long long)(i - 1) * (long long)(a[i] - 1); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long niz[200005]; n int main() { n   ios_base::sync_with_stdio(false); n   cout.precision(10); n   cout << fixed; n   int n; n   cin >> n; n   long long k, m; n   cin >> m >> k; n   for (int i = 1; i <= n; i++) cin >> niz[i]; n   sort(niz + 1, niz + 1 + n); n   long long b = niz[n]; n   long long a = niz[n - 1]; n   long long br = m / (k + 1); n   long long ost = m % (k + 1); n   long long res = 0; n   res += ost * b; n   res += br * a; n   res += br * k * b; n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n  n  n  n  n void solve(){ n  int n;cin>>n; n   n  if(n <= 45){ n   int point = 9; n   string ans =   ; n   int x = n; n  n   while(x > point){ n    ans += to_string(point); n    x -= point; n    point--; n   } n   if(x) ans += to_string(x); n   reverse(ans.begin(),ans.end()); n   cout<<ans<<endl; n   return; n  } n  n  cout<< -1 <<endl; n } n  n  n int main(){ n  ios::sync_with_stdio(false); cin.tie(0); n  #ifndef ONLINE_JUDGE  n  freopen( input.txt ,  r , stdin); n  freopen( result.txt ,  w , stdout); n  #endif n  int t;cin>>t; n  while(t--) n   solve(); n  n   n  n   n  n  return 0; n }//main n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1}; n int dy[] = {0, 1, 0, -1, 1, 1, -1, -1}; n int n, m; n bool dfs(pair<int, int> start, vector<string>& G, vector<vector<bool> >& odw, n          set<pair<int, int> >& jed) { n   stack<pair<int, int> > S; n   S.push(start); n   odw[start.first][start.second] = true; n   bool brzeg = false; n   while (!S.empty()) { n     pair<int, int> akt = S.top(); n     S.pop(); n     if (akt.first == 0 || akt.first == n - 1 || akt.second == 0 || n         akt.second == m - 1) n       brzeg = true; n     for (int i = 0; i < 8; ++i) { n       pair<int, int> nast = make_pair(akt.first + dx[i], akt.second + dy[i]); n       if (nast.first >= 0 && nast.first < n && nast.second >= 0 && n           nast.second < m) { n         if (G[nast.first][nast.second] ==  0  && n             !odw[nast.first][nast.second]) { n           S.push(nast); n           odw[nast.first][nast.second] = true; n         } n         if (G[nast.first][nast.second] ==  1 ) jed.insert(nast); n       } n     } n   } n   return brzeg; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   vector<string> a(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   int odp = 0; n   for (int i = 0; i + 1 < n; ++i) n     for (int j = 0; j + 1 < m; ++j) n       if (a[i][j] ==  1  && a[i][j + 1] ==  1  && a[i + 1][j] ==  1  && n           a[i + 1][j + 1] ==  1 ) n         odp = 4; n   vector<vector<bool> > odw(n, vector<bool>(m, false)); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       if (a[i][j] ==  1  || odw[i][j]) continue; n       set<pair<int, int> > S; n       if (dfs(make_pair(i, j), a, odw, S)) continue; n       int wlk = S.size(); n       pair<int, int> start = *S.begin(); n       pair<int, int> nast; n       int ilSa = 0; n       for (int k = 0; k < 4; ++k) { n         pair<int, int> t = make_pair(start.first + dx[k], start.second + dy[k]); n         if (S.find(t) != S.end()) { n           nast = t; n           ++ilSa; n         } n       } n       if (ilSa != 2) continue; n       S.erase(start); n       pair<int, int> akt = nast; n       bool ok = true; n       while (true) { n         ilSa = 0; n         for (int k = 0; k < 4; ++k) { n           pair<int, int> t = make_pair(akt.first + dx[k], akt.second + dy[k]); n           if (S.find(t) != S.end()) { n             nast = t; n             ++ilSa; n           } n         } n         if (ilSa > 1) { n           ok = false; n           break; n         } n         if (ilSa < 1) { n           if (S.size() > 1) ok = false; n           break; n         } n         S.erase(akt); n         akt = nast; n       } n       if (ok) odp = max(odp, wlk); n     } n   } n   cout << odp; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, r, avg, m, k, t, x, y, i, j, b, d, MOD = 1073741824, n                                                       count = 0, ans = 0; n   string s; n   cin >> s; n   long long int a[36] = {0}; n   for (i = 0; i < s.size(); i++) { n     if (s[i] -  a  >= 0) { n       a[s[i] -  a  + 10]++; n     } else { n       x = s[i] - 48; n       a[x]++; n     } n   } n   for (i = 0; i < 36; i++) { n     count += pow(a[i], 2); n   } n   cout << count; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define ll long long int n const int N=1000005; n //vector<int>prime; n bool prime[N]; n void sieve() n { n     int k=sqrt(N-2); n     for(int i=1;i<=N-2;++i) n         prime[i]=true; n     for(int i=2;i<=k;++i) n     { n         if(prime[i]) n         { n             for(int k=i*i;k<=N-2;k+=i) n             { n                 prime[k]=false; n                 //pf[k]=i; n             } n         } n     } n } n  n void solve() n { n         int d; n      cin>>d; n      int x; n      int y; n      for(int i=d+1;;i++) n      { n          if(prime[i]) n          { n               x=i; n              break; n          } n      } n      for(int i=x+d;;i++) n      { n          if(prime[i]) n          { n               y=i; n              break; n          } n      } n      cout<<x*y<<  n ; n } n  n int main() { n  // your code goes here n  ios_base::sync_with_stdio(false); n     cin.tie(NULL); n  sieve(); n  int t; n  cin>>t; n   n  while(t--) n  { n      solve(); n  } n       n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tt; n   cin >> tt; n   while (tt--) { n     int n, l; n     cin >> n >> l; n     vector<double> flags(n); n     for (auto& i : flags) cin >> i; n     double a_s = 0, b_s = l; n     double a_v = 1, b_v = 1; n     int a_i = 0, b_i = n - 1; n     double t = 0; n     for (;;) { n       double a_d = a_i < n ? flags[a_i] - a_s : 1e300, n              b_d = b_i >= 0 ? b_s - flags[b_i] : 1e300; n       double a_t = a_d / a_v, b_t = b_d / b_v; n       double sep = b_s - a_s; n       double sep_t = sep / (a_v + b_v); n       double mn = min({a_t, b_t, sep_t}); n       t += mn; n       a_s += a_v * mn; n       b_s -= b_v * mn; n       if (a_t == mn) { n         a_v++; n         a_i++; n       } else if (b_t == mn) { n         b_v++; n         b_i--; n       } else n         break; n     } n     cout << fixed << t << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5.2e3 + 5; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n char s[MAXN]; n bool a[MAXN][MAXN]; n int main(void) { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %s , s + 1); n     for (int j = 1; s[j]; ++j) { n       if (s[j] >=  0  && s[j] <=  9 ) n         s[j] -=  0 ; n       else n         s[j] = s[j] -  A  + 10; n       for (int k = 0; k < 4; ++k) a[i][(j << 2) - k] = (s[j] >> k) & 1; n     } n   } n   int x = 0; n   for (int i = 1; i <= n && x != 1; ++i) n     for (int l = 1, r = 1; l <= n && x != 1; l = r) { n       while (r <= n && a[i][r] == a[i][l]) ++r; n       x = gcd(x, r - l); n     } n   for (int j = 1; j <= n && x != 1; ++j) n     for (int l = 1, r = 1; l <= n && x != 1; l = r) { n       while (r <= n && a[r][j] == a[l][j]) ++r; n       x = gcd(x, r - l); n     } n   printf( %d , x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double sum[2][30][200010]; n char A[2][200010]; n int main() { n   int N; n   cin >> N; n   cin >> A[0] >> A[1]; n   for (int i = 0; i < (N); i++) n     for (int k = 0; k < (2); k++) A[k][i] -=  A ; n   double cont = 0; n   double val = 0; n   for (int i = (1); i <= (N); i++) val = N - i + 1, val *= val, cont += val; n   memset(sum, 0, sizeof(sum)); n   for (int i = 0; i < (N); i++) { n     for (int k = 0; k < (2); k++) { n       sum[k][A[k][i]][i] = ((i == 0) ? 0 : sum[k][A[k][i]][i - 1]) + (i + 1); n       if (i > 0) { n         for (char c = 0; c <= 25; c++) { n           sum[k][c][i] = max(sum[k][c][i], sum[k][c][i - 1]); n         } n       } n     } n   } n   double ans = 0; n   for (int i = 0; i < (N); i++) { n     for (int k = 0; k < (2); k++) { n       double* lst = sum[1 - k][A[k][i]]; n       double val = lst[i - 1]; n       ans += ((double)(N - i)) * val / cont; n     } n     if (A[0][i] == A[1][i]) { n       ans += ((double)(N - i)) * (i + 1) / cont; n     } n   } n   double vv = ans; n   cout << fixed << setprecision(12) << vv << endl; n } n


#include <bits/stdc++.h> n bool u[1000001]; n int pr[500001]; n int l; n int n; n long long a[8]; n int f[7000]; n int thr[10]; n int b[8]; n int inf = 10000; n int g[8]; n int cal(long long x) { n   int i; n   int ret = 0; n   for (i = 1; i <= l && pr[i] <= x / pr[i]; i++) { n     while (x % pr[i] == 0) { n       x = x / pr[i]; n       ret++; n     } n   } n   if (x > 1) ret++; n   return ret; n } n void dfs(int x, int y, long long z, int r, int cur) { n   int i; n   if (r == n) { n     if (f[x] > f[y] + cur + 1) f[x] = f[y] + cur + 1; n   } else { n     if (z % a[r] == 0 && b[r] == 2) n       dfs(x, y - thr[r], z / a[r], r + 1, cur - g[r]); n     dfs(x, y, z, r + 1, cur); n   } n } n int main() { n   int i, j; n   memset(u, true, sizeof(u)); n   l = 0; n   for (i = 2; i <= 1000000; i++) { n     if (u[i]) { n       l++; n       pr[l] = i; n     } n     for (j = 1; j <= l && i <= 1000000 / pr[j]; j++) { n       u[i * pr[j]] = false; n       if (i % pr[j] == 0) break; n     } n   } n   scanf( %d , &n); n   for (i = 0; i < n; i++) scanf( %I64d , &a[i]); n   for (i = 0; i < n; i++) g[i] = cal(a[i]); n   thr[0] = 1; n   for (i = 1; i <= n; i++) thr[i] = thr[i - 1] * 3; n   int mm = inf; n   f[0] = 0; n   for (i = 1; i < thr[n] - 1; i++) { n     int cur = i; n     bool flag = true; n     for (j = n - 1; j >= 0; j--) { n       b[j] = cur / thr[j]; n       cur = cur % thr[j]; n       if (b[j] == 0) flag = false; n     } n     f[i] = inf; n     for (j = 0; j < n; j++) n       if (b[j] == 1) { n         if (g[j] == 1) { n           if (f[i] > f[i - thr[j]] + 1) f[i] = f[i - thr[j]] + 1; n         } else { n           if (f[i] > g[j] + f[i - thr[j]] + 1) f[i] = g[j] + f[i - thr[j]] + 1; n         } n         dfs(i, i - thr[j], a[j], 0, g[j]); n       } n     if (flag) { n       for (j = 0; j < n; j++) n         if (b[j] == 1) break; n       if (i + thr[j] == thr[n] - 1) { n         if (f[i] < mm) mm = f[i]; n       } else { n         if (f[i] + 1 < mm) mm = f[i] + 1; n       } n     } n   } n   printf( %d n , mm); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   priority_queue<pair<int, int> > neg; n   priority_queue<pair<int, int>, vector<pair<int, int> >, n                  greater<pair<int, int> > > n       pos; n   cin >> n; n   int x, a; n   pair<int, int> temp; n   for (int i = 0; i < n; i++) { n     cin >> x >> a; n     temp = make_pair(x, a); n     if (x < 0) { n       neg.push(temp); n     } else { n       pos.push(temp); n     } n   } n   int ans = 0; n   if (pos.size() >= neg.size()) { n     while (1) { n       if (pos.empty()) n         break; n       else { n         ans += pos.top().second; n         pos.pop(); n       } n       if (neg.empty()) n         break; n       else { n         ans += neg.top().second; n         neg.pop(); n       } n     } n   } else { n     while (1) { n       if (neg.empty()) n         break; n       else { n         ans += neg.top().second; n         neg.pop(); n       } n       if (pos.empty()) n         break; n       else { n         ans += pos.top().second; n         pos.pop(); n       } n     } n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int v, nxt; n } e[200005 << 1]; n int top = 1; n int head[200005]; n void Add_Edge(int u, int v) { n   e[++top] = edge{v, head[u]}; n   head[u] = top; n } n int n; n int a[200005], par[200005][21], dep[200005], size[200005]; n void dfs(int u, int fa) { n   size[u] = 1; n   par[u][0] = fa; n   dep[u] = dep[fa] + 1; n   for (register int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].v; n     dfs(v, u); n     size[u] += size[v]; n   } n } n void Init() { n   for (register int j = 1; j <= 20; ++j) n     for (register int i = 1; i <= n; ++i) par[i][j] = par[par[i][j - 1]][j - 1]; n } n int LCA(int u, int v) { n   if (dep[u] < dep[v]) swap(u, v); n   int dis = dep[u] - dep[v]; n   for (register int i = 20; i >= 0; --i) n     if (dis & (1 << i)) u = par[u][i]; n   if (u == v) return u; n   for (register int i = 20; i >= 0; --i) n     if (par[u][i] ^ par[v][i]) u = par[u][i], v = par[v][i]; n   return par[u][0]; n } n int dist(int a, int b) { n   int lca = LCA(a, b); n   return dep[a] + dep[b] - dep[lca] * 2; n } n struct Node { n   int x, y; n }; n Node tree[200005 << 2]; n Node merge(Node a, int b) { n   if (a.x < 0 || b < 0) return Node{-1, -1}; n   int aa = a.x, bb = a.y; n   int d1 = dist(aa, b); n   int d2 = dist(bb, b); n   int d3 = dist(bb, aa); n   if (d1 + d2 == d3) return a; n   if (d1 + d3 == d2) return Node{b, bb}; n   if (d2 + d3 == d1) return Node{aa, b}; n   return Node{-1, -1}; n } n Node Merge(Node a, Node b) { n   if (a.x < 0 || b.x < 0) return Node{-1, -1}; n   Node up1 = merge(a, b.x); n   if (up1.x < 0) n     return Node{-1, -1}; n   else n     return merge(up1, b.y); n } n void Pushup(int i) { tree[i] = Merge(tree[i << 1], tree[i << 1 | 1]); } n int Index[200005]; n void build(int i, int l, int r) { n   if (l == r) return tree[i] = Node{Index[l], Index[l]}, void(); n   int mid = (l + r) >> 1; n   build(i << 1, l, mid); n   build(i << 1 | 1, mid + 1, r); n   Pushup(i); n } n void update(int i, int l, int r, int res) { n   if (l == r) return tree[i] = Node{Index[l], Index[l]}, void(); n   int mid = (l + r) >> 1; n   if (res <= mid) n     update(i << 1, l, mid, res); n   else n     update(i << 1 | 1, mid + 1, r, res); n   Pushup(i); n } n int query(Node &mq, int i, int l, int r) { n   if (tree[i].x >= 0) { n     if (mq.x < 0) return mq = tree[i], r + 1; n     Node up1 = Merge(mq, tree[i]); n     if (up1.x >= 0) return mq = up1, r + 1; n   } n   if (l == r) return l; n   int mid = (l + r) >> 1; n   int res = query(mq, i << 1, l, mid); n   if (res <= mid) n     return res; n   else n     return query(mq, i << 1 | 1, mid + 1, r); n } n int read() { n   int x = 0; n   char c = getchar(); n   int f = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -f; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n void write(int x) { n   if (x < 0) { n     x = -x; n     putchar( - ); n   } n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n signed main() { n   n = read(); n   for (register int i = 1; i <= n; ++i) { n     a[i] = read() + 1; n     Index[a[i]] = i; n   } n   for (register int i = 2; i <= n; ++i) { n     int fa = read(); n     Add_Edge(fa, i); n   } n   dfs(1, 1); n   Init(); n   build(1, 1, n); n   int q = read(); n   for (register int i = 1; i <= q; ++i) { n     int type = read(); n     if (type == 1) { n       int x = read(), y = read(); n       swap(Index[a[x]], Index[a[y]]); n       swap(a[x], a[y]); n       update(1, 1, n, a[x]); n       update(1, 1, n, a[y]); n     } else { n       Node mq = Node{-1, -1}; n       int Ans = query(mq, 1, 1, n); n       write(Ans - 1), putchar(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(0); n   int n; n   std::cin >> n; n   std::vector<bool> col(6 * n); n   for (int i = 0; i < 3 * n; ++i) { n     int x; n     std::cin >> x; n     --x; n     col[x] = true; n   } n   std::vector<int> sta; n   std::vector<std::vector<int>> tmp(6 * n); n   std::vector<std::tuple<int, int, int>> tp; n   std::vector<bool> bel; n   std::vector<std::vector<int>> E(2 * n); n   for (int i = 0; i < 6 * n; ++i) { n     sta.push_back(i); n     if ((int)sta.size() >= 3) { n       auto c = std::prev(sta.end()), b = std::prev(c), a = std::prev(b); n       if (col[*a] == col[*b] && col[*b] == col[*c]) { n         int u = tp.size(); n         tp.emplace_back(*a, *b, *c); n         bel.push_back(col[*a]); n         if (a != sta.begin()) { n           tmp[*std::prev(a)].push_back(u); n         } n         E[u].insert(E[u].end(), tmp[*a].begin(), tmp[*a].end()); n         E[u].insert(E[u].end(), tmp[*b].begin(), tmp[*b].end()); n         sta.pop_back(), sta.pop_back(), sta.pop_back(); n       } n     } n   } n   std::vector<int> fa(2 * n, -1), deg(2 * n, 0); n   for (int u = 0; u < 2 * n; ++u) { n     for (int v : E[u]) { n       fa[v] = u; n     } n     deg[u] = E[u].size(); n   } n   int cnt = 0; n   for (int u = 0; u < 2 * n; ++u) { n     if (fa[u] == -1 && !bel[u]) { n       ++cnt; n     } n   } n   int p = -1; n   std::array<std::queue<int>, 2> Q; n   for (int u = 0; u < 2 * n; ++u) { n     if (!deg[u]) { n       Q[bel[u]].push(u); n     } n   } n   std::vector<std::tuple<int, int, int>> ans; n   int c = 0; n   for (int i = 0; i < 2 * n - 1; ++i) { n     c ^= 1; n     while (1) { n       int u = Q[c].front(); n       Q[c].pop(); n       if (fa[u] == -1 && !bel[u]) { n         if (cnt == 1) { n           p = u; n           continue; n         } n         --cnt; n       } n       ans.emplace_back(tp[u]); n       if (fa[u] != -1) { n         --deg[fa[u]]; n         if (!deg[fa[u]]) { n           Q[bel[fa[u]]].push(fa[u]); n         } n       } n       break; n     } n   } n   if (p == -1) { n     c ^= 1; n     p = Q[c].front(); n   } n   ans.emplace_back(tp[p]); n   for (auto [x, y, z] : ans) { n     std::cout << x + 1 <<     << y + 1 <<     << z + 1 <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n const int maxn = 300 + 10, maxm = 4e4 + 10; n const int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0}; n const int INF = 0x3f3f3f3f; n struct Graph { n   int ndn, edn, last[maxn]; n   int u[maxm], v[maxm], flw[maxm], cap[maxm], nxt[maxm]; n   void init(int _n) { n     ndn = _n; n     edn = 0; n     memset(last, -1, sizeof(last)); n     memset(flw, 0, sizeof(flw)); n     memset(cap, 0, sizeof(cap)); n   } n   void adde(int _u, int _v, int _c) { n     u[edn] = _u; n     v[edn] = _v; n     cap[edn] = _c; n     nxt[edn] = last[_u]; n     last[_u] = edn++; n   } n }; n struct Dinic { n   Graph *G; n   int S, T, dist[maxn], cur[maxn]; n   int BFS(); n   int DFS(int, int); n   int solve(Graph *, int, int); n }; n int N, T; n char sci[15][15], cap[15][15]; n int death[15][15], idsci[15][15], idcap[15][15]; n int dist[15][15]; n Graph G; n Dinic dinic; n bool judge(int x, int y) { n   return x < 0 || x >= N || y < 0 || y >= N || !isdigit(sci[x][y]); n } n int main() { n   int zx, zy; n   G.init(1); n   scanf( %d%d , &N, &T); n   for (int i = 0; i < N; i++) { n     scanf(  %s , sci[i]); n     for (int j = 0; j < N; j++) { n       if (isdigit(sci[i][j]) && sci[i][j] >=  1 ) { n         idsci[i][j] = ++G.ndn; n         G.adde(0, G.ndn, sci[i][j] -  0 ); n         G.adde(G.ndn, 0, 0); n       } n       if (sci[i][j] ==  Z ) { n         zx = i; n         zy = j; n       } n     } n   } n   for (int i = 0; i < N; i++) { n     scanf(  %s , cap[i]); n     for (int j = 0; j < N; j++) { n       if (isdigit(cap[i][j]) && cap[i][j] >=  1 ) { n         idcap[i][j] = ++G.ndn; n         G.adde(G.ndn, 1, cap[i][j] -  0 ); n         G.adde(1, G.ndn, 0); n       } n     } n   } n   queue<pair<int, int> > que; n   memset(death, 0x3f, sizeof(death)); n   death[zx][zy] = 0; n   que.push({zx, zy}); n   while (que.size()) { n     pair<int, int> u = que.front(); n     que.pop(); n     int d = death[u.first][u.second] + 1; n     for (int k = 0; k < 4; k++) { n       int x = u.first + dir[k][0]; n       int y = u.second + dir[k][1]; n       if (judge(x, y) || death[x][y] < INF) continue; n       death[x][y] = d; n       que.push({x, y}); n     } n   } n   for (int i = 0; i < N; i++) n     for (int j = 0; j < N; j++) n       if (isdigit(sci[i][j]) && sci[i][j] >=  1 ) { n         while (que.size()) que.pop(); n         memset(dist, 0x3f, sizeof(dist)); n         dist[i][j] = 0; n         que.push({i, j}); n         while (que.size()) { n           pair<int, int> u = que.front(); n           que.pop(); n           if (idcap[u.first][u.second]) { n             G.adde(idsci[i][j], idcap[u.first][u.second], sci[i][j] -  0 ); n             G.adde(idcap[u.first][u.second], idsci[i][j], 0); n           } n           int d = dist[u.first][u.second] + 1; n           if (d > T || dist[u.first][u.second] >= death[u.first][u.second]) n             continue; n           for (int k = 0; k < 4; k++) { n             int x = u.first + dir[k][0]; n             int y = u.second + dir[k][1]; n             if (judge(x, y) || dist[x][y] < INF || death[x][y] < d) continue; n             dist[x][y] = d; n             que.push({x, y}); n           } n         } n       } n   printf( %d n , dinic.solve(&G, 0, 1)); n   return 0; n } n int Dinic::solve(Graph *g, int ts, int tt) { n   G = g; n   S = ts; n   T = tt; n   int res = 0; n   while (BFS()) { n     for (int i = 0; i <= G->ndn; i++) cur[i] = G->last[i]; n     res += DFS(S, 1e9); n   } n   return res; n } n int Dinic::BFS() { n   memset(dist, -1, sizeof(dist)); n   dist[S] = 0; n   queue<int> que; n   que.push(S); n   while (que.size()) { n     int u = que.front(); n     que.pop(); n     for (int e = G->last[u]; ~e; e = G->nxt[e]) { n       int v = G->v[e]; n       if (dist[v] == -1 && G->cap[e] > G->flw[e]) { n         dist[v] = dist[u] + 1; n         que.push(v); n       } n     } n   } n   return ~dist[T]; n } n int Dinic::DFS(int u, int tmin) { n   if (u == T || tmin == 0) return tmin; n   int nflw = 0, f; n   for (int &e = cur[u]; ~e; e = G->nxt[e]) { n     int v = G->v[e]; n     if (dist[u] + 1 == dist[v] && n         (f = DFS(v, min(tmin, G->cap[e] - G->flw[e]))) > 0) { n       G->flw[e] += f; n       G->flw[e ^ 1] -= f; n       nflw += f; n       tmin -= f; n       if (tmin == 0) break; n     } n   } n   return nflw; n } n


#include <bits/stdc++.h> n using namespace std; n int a[52][52] = {0}; n int b[52]; n bool c[52] = {0}; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       scanf( %d , &a[i][j]); n       if (c[a[i][j]] == 0) n         c[a[i][j]] = 1; n       else n         b[i] = a[i][j]; n     } n     memset(c, 0, sizeof(c)); n   } n   for (int i = 1; i <= n; i++) c[b[i]] = 1; n   for (int i = 1; i <= n; i++) { n     if (b[i]) n       cout << b[i]; n     else { n       for (int j = 1; j <= n; j++) n         if (c[j] == 0) { n           cout << j; n           c[j] = 1; n           break; n         } n     } n     cout <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> ok; n vector<long long> nok; n void doit() { n   long long n, d, m; n   cin >> n >> d >> m; n   vector<long long> v; n   long long x; n   while (n--) { n     cin >> x; n     v.push_back(x); n   } n   sort(v.begin(), v.end()); n   long long mleg = 0; n   long long ans = 0; n   long long cur = 0; n   while ((mleg < v.size()) && (v[mleg] <= m)) { n     cur += v[mleg]; n     mleg++; n   } n   long long sum[mleg]; n   sum[0] = v[0]; n   for (int i = 1; i < mleg; i++) { n     sum[i] = v[i] + sum[i - 1]; n   } n   ans = cur; n   x = 1; n   long long comp = 0; n   while (v.size() - x >= mleg) { n     long long ci = v.size() - x; n     comp += v[ci]; n     long long nesesito = max(((x - 1) * d) - (ci - mleg), (long long)(0)); n     if (nesesito <= mleg) { n       long long mres = 0; n       if (nesesito) n         mres = comp + (cur - sum[nesesito - 1]); n       else n         mres = comp + cur; n       ans = max(mres, ans); n     } n     x++; n   } n   cout << ans << endl; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t = 1; n   while (t--) { n     doit(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005, M = N * N; n int n, m; n string s[N]; n bool isvalid(int x, int y) { n   return 0 <= x && x < n && 0 <= y && y < m && s[x][y] ==  . ; n } n int id(int x, int y) { return x * N + y; } n int par[M]; n void init() { n   for (int i = 0; i < M; i++) par[i] = i; n } n int find(int u) { n   if (par[u] == u) return u; n   return par[u] = find(par[u]); n } n void merge(int u, int v) { par[find(u)] = find(v); } n vector<int> adj[M]; n bool vis[M]; n int dis[M]; n void bfs(int s) { n   dis[s] = 0; n   vis[s] = 1; n   queue<int> q; n   q.push(s); n   while (q.size()) { n     int u = q.front(); n     q.pop(); n     for (int v : adj[u]) n       if (!vis[v]) { n         vis[v] = 1; n         dis[v] = 1 + dis[u]; n         q.push(v); n       } n   } n } n int main() { n   init(); n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   int sx, sy, l, r; n   cin >> sx >> sy >> l >> r; n   sx--; n   sy--; n   for (int i = 0; i < n; i++) cin >> s[i]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       if (s[i][j] !=  . ) continue; n       if (isvalid(i + 1, j)) merge(id(i, j), id(i + 1, j)); n     } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       if (s[i][j] !=  . ) continue; n       int u = find(id(i, j)); n       int l = find(id(i, j - 1)); n       int r = find(id(i, j + 1)); n       if (isvalid(i, j - 1)) adj[u].push_back(l); n       if (isvalid(i, j + 1)) adj[u].push_back(r); n     } n   bfs(find(id(sx, sy))); n   int ans = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       int k = find(id(i, j)); n       if (s[i][j] !=  .  || !vis[k]) continue; n       int dif = j - sy; n       int sum = dis[k]; n       assert(sum % 2 == abs(dif) % 2); n       int rr = (sum + dif) / 2; n       int ll = (sum - dif) / 2; n       if (rr <= r && ll <= l) ans++; n     } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int solve(int n, int a[]) { n   int count = 0; n   for (int i = 0; i < n; ++i) { n     if (a[i] == i) { n       ++count; n     } n   } n   for (int i = 0; i < n; ++i) { n     if (a[i] != i && a[a[i]] == i) { n       return count + 2; n     } n   } n   return min(count + 1, n); n } n int main() { n   int n; n   int a[100000]; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   cout << solve(n, a) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 52; n const int MAX = 1e9 + 5; n int n, a[N]; n long double dp[N][N][N][N]; n int p; n long double all, P[N]; n long double f(int wh, int ret, int left, int whd) { n   if (left == 0 && ret) return 0; n   if (left == 0 && ret == 0) return 1; n   if (ret < 0) return 0; n   if (wh > n) return 0; n   long double &r = dp[wh][ret][left][whd]; n   if (r != -1) return r; n   if (wh == whd) return r = f(wh + 1, ret, left, whd); n   return r = f(wh + 1, ret - a[wh], left - 1, whd) + f(wh + 1, ret, left, whd); n } n int main() { n   scanf( %d , &n); n   P[0] = 1; n   for (int i = 1; i <= n; i++) P[i] = P[i - 1] * i; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   scanf( %d , &p); n   for (int A = 0; A <= N - 1; A++) n     for (int B = 0; B <= N - 1; B++) n       for (int C = 0; C <= N - 1; C++) n         for (int D = 0; D <= N - 1; D++) dp[A][B][C][D] = -1; n   for (int i = 1; i <= n; i++) { n     int begin = p - a[i] + 1; n     int end = p; n     for (int j = begin; j <= end; j++) { n       for (int k = 0; k <= n; k++) { n         long double temp = f(1, j, k, i); n         all += temp * P[k] / P[n] * P[n - k - 1] * k; n       } n     } n   } n   for (int i = 0; i <= p; i++) all += f(1, i, n, 0) * n; n   cout << setprecision(12) << fixed << all <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   unsigned long long int t; n   cin >> t; n   while (t--) { n     unsigned long long int n; n     cin >> n; n     unsigned long long int x; n     cin >> x; n     vector<unsigned long long int> ind; n     vector<pair<unsigned long long int, unsigned long long int>> store; n     unsigned long long int count = 0, ind1; n     for (unsigned long long int i = 1; i <= n - 1; i++) { n       unsigned long long int y; n       cin >> y; n       if (y != x) { n         ind1 = i + 1; n         count++; n         store.push_back({1, i + 1}); n       } else { n         ind.push_back(i); n       } n     } n     if (count == 0) n       cout <<  NO  n            <<   n ; n     else { n       cout <<  YES  n            <<   n ; n       for (auto pr : store) { n         cout << pr.first <<     << pr.second <<   n ; n       } n       if (ind.size() != 0) { n         for (auto x : ind) { n           cout << ind1 <<     << x + 1 <<   n ; n         } n       } n     } n   } n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename T2> n T2 fac(T n) { n   return n < 2 ? 1 : fac<T, T2>(n - 1) * n; n } n template <typename T, typename T2, typename T3> n T2 fac(T n, T3 mod) { n   return n < 2 ? 1 : (fac<T, T2, T3>(n - 1, mod) * n) % mod; n } n template <typename T> n T nb_combinaisons(T k, T n) { n   if (n < k) return nb_combinaisons<T>(n, k); n   return fac<T, long long int>(n) / n          (fac<T, long long int>(n - k) * fac<T, long long int>(k)); n } n template <typename T, typename T2> n T nb_permutations(vector<T2> v) { n   T total = fac<T, T>(v.size()); n   map<T2, T> m; n   bool distinct = true; n   for (long long unsigned c = 0; c < v.size(); c++) { n     m[v[c]]++; n     if (m[v[c]] - 1) distinct = false; n   } n   for (typename map<T2, T>::iterator it = m.begin(); it != m.end(); it++) n     total /= fac<T, T>(it->second); n   return total; n } n template <typename T, typename T2, typename T3> n T nb_permutations(vector<T2> v, T3 mod) { n   T total = fac<T, T, T3>(v.size(), mod); n   map<T2, T> m; n   bool distinct = true; n   for (long long unsigned c = 0; c < v.size(); c++) { n     m[v[c]]++; n     if (m[v[c]] - 1) distinct = false; n   } n   for (typename map<T2, T>::iterator it = m.begin(); it != m.end(); it++) n     total /= fac<T, T>(it->second); n   return total % mod; n } n string itoa_2(long long int n) { n   string s; n   stringstream ss; n   ss << n; n   ss >> s; n   while (s.size() != 9) s =  0  + s; n   return s; n } n int pgcd(int a, int b) { n   if (a == 0) return b; n   if (b == 0) return a; n   return pgcd(b, a % b); n } n string conversion(int n, int base) { n   string res =   ; n   while (n != 0) { n     res = ((char)( 0  + n % base)) + res; n     n /= base; n   } n   return res; n } n int main() { n   int n, m; n   cin >> n >> m; n   int i = 0; n   vector<int> v; n   if (m % 2 == 0) { n     int gauche = (m - 1) / 2 + 1; n     int droite = (m - 1) / 2 + 2; n     bool g = true; n     for (int c = 0; c < n; c++) { n       if (g) { n         cout << gauche << endl; n         if (gauche - 1 <= 0) { n           gauche = (m - 1) / 2 + 1; n         } else n           gauche--; n       } else { n         cout << droite << endl; n         if (droite + 1 > m) n           droite = (m - 1) / 2 + 2; n         else n           droite++; n       } n       g = !g; n     } n   } else { n     int gauche = (m) / 2; n     int droite = (m) / 2 + 2; n     bool g = true; n     for (int c = 0; c < n; c++) { n       if (c % m == 0) n         cout << (m / 2) + 1 << endl; n       else if (g) { n         cout << gauche << endl; n         if (gauche - 1 <= 0) { n           gauche = (m) / 2; n         } else n           gauche--; n       } else { n         cout << droite << endl; n         if (droite + 1 > m) n           droite = (m) / 2 + 2; n         else n           droite++; n       } n       if (c % m != 0) g = !g; n     } n   } n } n