#include <bits/stdc++.h> n long long int power(long long int a, long long int b) { n   long long int ans = 1; n   long long int k = a; n   while (b) { n     if (b % 2 == 1) ans *= k, ans %= 998244353; n     k *= k, k %= 998244353; n     b /= 2; n   } n   return ans; n } n int a, b, start; n int z[40]; n long long int ans[60]; n int y[200010][60]; n void swap(int s, int t) { n   for (int j = 0; j <= 53; j++) { n     int k = y[s][j]; n     y[s][j] = y[t][j]; n     y[t][j] = k; n   } n } n void func1(int k) { n   if (k == start) { n     int sum = 0; n     for (int j = 0; j < b; j++) sum += z[j] % 2; n     ans[sum]++; n     return; n   } n   func1(k + 1); n   for (int j = 0; j < b; j++) z[j] += y[k][j]; n   func1(k + 1); n   for (int j = 0; j < b; j++) z[j] -= y[k][j]; n } n int diff(long long int a, long long int b) { n   int sum = 0; n   while (a > 0 || b > 0) { n     if (b % 2 == 1) { n       if (a % 2 == 0) n         sum++; n       else n         sum--; n     } n     b /= 2, a /= 2; n   } n   return sum; n } n std::vector<int> V; n long long int S[20010][40], T[20010][40]; n int main() { n   scanf( %d%d , &a, &b); n   for (int i = 1; i <= a; i++) { n     long long int c; n     scanf( %lld , &c); n     for (int j = 0; j < b; j++) { n       y[i][j] = c % 2; n       c /= 2; n     } n   } n   start = 1; n   for (int j = 0; j < b; j++) { n     int p = -1; n     for (int i = start; i <= a; i++) { n       if (y[i][j] == 1) { n         p = i; n         break; n       } n     } n     if (p == -1) { n       V.push_back(j); n       continue; n     } n     if (p != start) swap(p, start); n     for (int i = 1; i <= a; i++) { n       if (i == start) continue; n       if (y[i][j] == 1) n         for (int k = 0; k < b; k++) y[i][k] += y[start][k], y[i][k] %= 2; n     } n     start++; n   } n   long long int M = power(2, a - start + 1); n   if (start - 1 <= 21) { n     func1(1); n     for (int j = 0; j <= b; j++) ans[j] *= M, ans[j] %= 998244353; n     for (int j = 0; j <= b; j++) printf( %lld  , ans[j]); n     return 0; n   } else { n     int s = 0; n     for (int i = 0; i < V.size(); i++) { n       s *= 2; n       s++; n     } n     S[0][0] = 1; n     for (int i = start - 1; i >= 1; i--) { n       int t = 0; n       for (int j = 0; j < V.size(); j++) { n         t *= 2; n         t += y[i][V[j]]; n       } n       for (int j = 0; j <= s; j++) n         for (int k = 0; k <= b; k++) T[j][k] = 0; n       for (int j = 0; j <= s; j++) { n         int d = diff(j, t) + 1; n         for (int k = 0; k <= b; k++) n           if (k + d >= 0) n             T[j ^ t][k + d] += S[j][k], T[j ^ t][k + d] %= 998244353; n         for (int k = 0; k <= b; k++) T[j][k] += S[j][k], T[j][k] %= 998244353; n       } n       for (int j = 0; j <= s; j++) n         for (int k = 0; k <= b; k++) S[j][k] = T[j][k]; n     } n     for (int j = 0; j <= s; j++) n       for (int k = 0; k <= b; k++) ans[k] += S[j][k], ans[k] %= 998244353; n     for (int k = 0; k <= b; k++) ans[k] *= M, ans[k] %= 998244353; n     for (int j = 0; j <= b; j++) printf( %lld  , ans[j]); n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.sync_with_stdio(0); n   cout.sync_with_stdio(0); n   int n, a, c, d, k = 0; n   cin >> n >> a; n   c = floorl(n * a / 180.l); n   if (c > n - 2) c = n - 2; n   if (c < 1) c = 1; n   d = ceill(n * a / 180.l); n   if (d > n - 2) d = n - 2; n   if (d < 1) d = 1; n   if (abs(c * 180.l / n - a) > abs(d * 180.l / n - a)) swap(c, d); n   cout <<  1 2   << n - c + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int z[(int)1e6 + 3]; n string z_algo(string s) { n   int x = 0, y = 0; n   int T = 0, M = (int)1e6 + 3; n   for (int i = 1; i < s.size(); i++) { n     z[i] = max(0, min(z[i - x], y - i + 1)); n     while (i + z[i] < s.size() && s[z[i]] == s[i + z[i]]) { n       x = i, y = i + z[i]; n       z[i]++; n     } n   } n   int maxz = 0, res = 0, n = s.size(); n   int ans = 0; n   for (int i = 0; i < n; i++) { n     if (i + z[i] == n && maxz >= z[i]) { n       ans = max(ans, z[i]); n     } n     maxz = max(maxz, z[i]); n   } n   string t; n   for (int i = 0; i < ans; i++) t.push_back(s[i]); n   if (!ans) return  Just a legend ; n   return t; n } n int main() { n   int t, tc = 0; n   string s, ans; n   cin >> s; n   ans = z_algo(s); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n using ii = pair<ll, ll>; n using vi = vector<int>; n using vii = vector<ii>; n using graph = vector<vii>; n const int INF = 0x3f3f3f3f; n const ll INFL = 0x3f3f3f3f3f3f3f3f; n const int MAXN = 2123; n const ll mod = 998244353; n int seen[MAXN], color[MAXN]; n string s; n graph g; n bool dfs(int u) { n   seen[u] = 1; n   int v, type; n   for (auto k : g[u]) { n     tie(v, type) = k; n     if (!seen[v]) { n       if (color[v] == -1) color[v] = color[u] ^ type; n       if (dfs(v)) return 1; n     } n     if (color[v] ^ color[u] != type) return 1; n   } n   return 0; n } n void build(int n) { n   for (int i = 0; i < n; ++i) { n     seen[i] = 0; n     color[i] = -1; n   } n   color[0] = 0; n   color[1] = 1; n } n ll bxp(int xp) { n   ll ans = 1, base = 2; n   while (xp > 0) { n     if (xp & 1) ans = ans * base % mod; n     base = base * base % mod; n     xp >>= 1; n   } n   return ans; n } n int main() { n   int n, m; n   cin >> s; n   n = s.size(); n   ll ans = 0; n   for (int k = n - 1; k > 0; --k) { n     g.assign(2 + n + k + 1, vii()); n     for (int i = 2 + n, j = 2 + n + k - 1; i < j; ++i, --j) { n       g[i].emplace_back(j, 0); n       g[j].emplace_back(i, 0); n     } n     for (int i = 2, j = 2 + n - 1; i < j; ++i, --j) { n       g[i].emplace_back(j, 0); n       g[j].emplace_back(i, 0); n     } n     for (int i = 0, j = 0; i < n; ++i) { n       if (s[i] ==  ? ) { n         if (i >= n - k) ++j; n         continue; n       } n       if (i < n - k) { n         g[i + 2].emplace_back(s[i] -  0 , 0); n         g[s[i] -  0 ].emplace_back(i + 2, 0); n       } else { n         g[i + 2].emplace_back(j + 2 + n, s[i] -  0 ); n         g[j + 2 + n].emplace_back(i + 2, s[i] -  0 ); n         ++j; n       } n     } n     build(2 + n + k); n     g[1].emplace_back(0, 1); n     g[0].emplace_back(1, 1); n     g[n + 2].emplace_back(1, 0); n     g[1].emplace_back(n + 2, 0); n     if (dfs(0)) { n       continue; n     } n     int cnt = 0; n     bool skip = 0; n     for (int i = 2; i < 2 + n + k && !skip; ++i) { n       if (!seen[i]) { n         color[i] = 0; n         if (dfs(i)) { n           skip = 1; n         } n         ++cnt; n       } n     } n     if (skip) continue; n     ans += bxp(cnt); n     if (ans >= mod) ans -= mod; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, n1, k, k1, m, m1; n   cin >> n >> n1 >> k >> k1; n   if (n > n1) n     cout <<  First ; n   else n     cout <<  Second ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n int maina() { n   int T; n   cin >> T; n   for (int _t = 0; _t < T; _t++) { n   } n   return 0; n } n int main() { n   ios::sync_with_stdio(false); n   long long N; n   cin >> N; n   vector<long long> in(N); n   for (int i = 0; i < N; ++i) { n     cin >> in[i]; n     in[i]--; n   } n   vector<long long> cnt(N); n   cnt[0] = 2; n   long long result = 2; n   for (int i = 1; i < N; ++i) { n     long long total = 2; n     for (int j = in[i]; j < i; ++j) { n       total += cnt[j]; n       total %= MOD; n     } n     cnt[i] = total; n     result += total; n     result %= MOD; n   } n   cout << result; n   return 0; n } n


#include <bits/stdc++.h> n constexpr unsigned primes[]{2672090437, 2569161337, 2414657393}; n constexpr int np = sizeof(primes) / sizeof(*primes); n std::vector<std::array<unsigned, np> > pow2; n void setpow2(int maxp) { n   pow2.resize(maxp + 1); n   for (int i = 0; i < np; i++) pow2[0][i] = 1; n   for (int i = 1; i <= maxp; i++) n     for (int j = 0; j < np; j++) pow2[i][j] = pow2[i - 1][j] * 2ULL % primes[j]; n } n struct hash { n   unsigned m[np]; n   int nbits; n   hash() {} n   hash(unsigned num, int nbits = 1) : nbits(nbits) { n     for (int i = 0; i < np; i++) m[i] = num; n   } n }; n hash join(hash a, hash b) { n   hash out; n   out.nbits = a.nbits + b.nbits; n   for (int i = 0; i < np; i++) n     out.m[i] = (a.m[i] * (uint64_t)pow2[b.nbits][i] + b.m[i]) % primes[i]; n   return out; n } n bool operator<(hash a, hash b) { n   if (a.nbits != b.nbits) return a.nbits < b.nbits; n   return memcmp(a.m, b.m, sizeof(a.m)) < 0; n } n int w[100000]; n std::vector<int> adj[100000]; n hash h0[100000]; n hash hnew[100000]; n int deg[100000]; n int findw(int v, int par = -1) { n   int t = 1; n   for (int x : adj[v]) { n     if (x != par) t += findw(x, v); n   } n   return w[v] = t; n } n void genh0(int v, bool rec = true) { n   if (rec) { n     for (int x : adj[v]) genh0(x); n   } n   std::sort(adj[v].begin(), adj[v].end(), n             [](int a, int b) { return h0[a] < h0[b]; }); n   hash out(0); n   for (int x : adj[v]) out = join(out, h0[x]); n   h0[v] = join(out, hash(1)); n } n void hconcat(int v, hash h) { n   hnew[v] = memcmp(hnew[v].m, h.m, sizeof(h.m)) < 0 ? join(hnew[v], h) n                                                     : join(h, hnew[v]); n   for (int x : adj[v]) hconcat(x, h); n } n int main() { n   std::ios_base::sync_with_stdio(false); n   int n; n   scanf( %d , &n); n   setpow2(2 * n + 5); n   for (int i = n; --i;) { n     int a, b; n     scanf( %d %d , &a, &b); n     --a; n     --b; n     adj[a].push_back(b); n     adj[b].push_back(a); n     ++deg[a]; n     ++deg[b]; n   } n   findw(0); n   std::vector<int> center; n   for (int i = n; i--;) { n     if (n % 2 == 0 && w[i] == n / 2) { n       for (int j : adj[i]) { n         if (w[j] > w[i]) { n           center = {i, j}; n           break; n         } n       } n       break; n     } else if (n - w[i] < (n + 1) / 2) { n       bool isC = true; n       for (int v : adj[i]) isC &= w[v] > w[i] || w[v] < (n + 1) / 2; n       if (isC) { n         center = {i}; n         break; n       } n     } n   } n   bool ecent = center.size() == 2; n   for (auto i = center.size(); i--;) findw(center[i], center[i ^ +ecent]); n   for (int i = n; i--;) { n     std::sort(adj[i].begin(), adj[i].end(), n               [](int a, int b) { return w[a] < w[b]; }); n     if (n > 1 && (ecent || i != center[0])) adj[i].pop_back(); n   } n   struct split { n     const int *eqb, *eqe; n     hash pre, post; n   }; n   std::vector<split> ss; n   const hash h01 = join(hash(0), hash(1)); n   std::function<void(int, hash, hash, int)> genh; n   int nbad = 0; n   genh = [&](int v, hash pre, hash post, int skipw) { n     if (deg[v] < 4) { n       hash h = join(pre, h01); n       for (int x : adj[v]) h = join(h, h0[x]); n       h = join(h, post); n       for (auto it = ss.crbegin(); it != ss.crend(); ++it) { n         hash hh = it->pre; n         bool inserted = false; n         for (const int* i = it->eqb; i != it->eqe; ++i) { n           if (!inserted && memcmp(h.m, h0[*i].m, sizeof(h.m)) < 0) { n             inserted = true; n             hh = join(hh, h); n           } n           hh = join(hh, h0[*i]); n         } n         if (!inserted) hh = join(hh, h); n         h = join(hh, it->post); n       } n       hnew[v] = h; n     } else { n       hnew[v].nbits = -1; n       nbad++; n     } n     for (auto aj = adj[v].cbegin(), ae = adj[v].cend(); aj != ae; ++aj) { n       if (w[*aj] == skipw) continue; n       auto a2 = aj; n       do ++a2; n       while (a2 != ae && w[*a2] == w[*aj]); n       hash pre2 = pre, post2 = post; n       for (auto a = adj[v].cbegin(); a != a2; ++a) { n         if (a != aj) pre2 = join(pre2, h0[*a]); n       } n       if (a2 != ae && w[*a2] == w[*aj] + 1) { n         auto a3 = a2; n         do ++a3; n         while (a3 != ae && w[*a3] == w[*a2]); n         for (auto a = ae; a != a3;) post2 = join(h0[*--a], post2); n         ss.push_back(split{&*a2, &*a2 + (a3 - a2), pre2, post2}); n         genh(*aj, hash(0), hash(1), -1); n         ss.pop_back(); n       } else { n         for (auto a = ae; a != a2;) post2 = join(h0[*--a], post2); n         pre2 = join(pre2, hash(0)); n         post2 = join(hash(1), post2); n         genh(*aj, pre2, post2, -1); n       } n     } n   }; n   if (ecent) { n     genh0(center[0]); n     genh0(center[1]); n     for (int i : {0, 1}) { n       int c = center[i]; n       adj[c].push_back(center[i ^ 1]); n       genh(c, hash(0, 0), hash(0, 0), n / 2); n       adj[c].pop_back(); n     } n   } else { n     int c = center[0]; n     genh0(c); n     genh(c, hash(0, 0), hash(0, 0), n / 2); n     for (auto i = adj[c].size(); i--;) { n       int c2 = adj[c][i]; n       if (w[c2] != n / 2) continue; n       adj[c].erase(adj[c].begin() + i); n       genh0(c, false); n       genh(c2, hash(0, 0), hash(0, 0), -1); n       adj[c].insert(adj[c].begin() + i, c2); n       hconcat(c2, h0[c]); n     } n   } n   std::sort(hnew, hnew + n); n   int ans = 1; n   for (int i = nbad + 1; i < n; i++) { n     if (memcmp(hnew + i, hnew + i - 1, sizeof(*hnew))) ++ans; n   } n   std::cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, mod; n long long expsq(long long base, long long exp) { n   if (exp == 0) return 1ll; n   if (exp == 1) return base; n   if (exp & 1) n     return (base * expsq((base % mod * base % mod) % mod, (exp - 1) / 2)) % mod; n   return expsq((base % mod * base % mod) % mod, exp / 2) % mod; n } n int main() { n   cin >> n >> mod; n   long long ans = expsq(3ll, n); n   ans--; n   while (ans < 0) ans += mod; n   cout << ans % mod; n } n


#include <bits/stdc++.h> n using namespace std; n template <class t> n t mag(t a) { n   return a > 0 ? a : -a; n } n template <class T, class U> n T cdiv(T num, U den) { n   return (num / den) + ((num % den) > 0); n } n std::ifstream term( /dev/tty ); n std::string tempstring; n using ll = int_fast64_t; n using ii = pair<int, int>; n using vi = vector<int>; n using vl = vector<ll>; n using vii = vector<ii>; n const int sij = 60; n const ll mod = 1000000007ll; n vi took[sij]; n int m, n; n int esc(int tcase) { n   cin >> m >> n; n   for (auto i = 0; i != m; i++) { n     int tp; n     cin >> tp; n     for (auto j = 0; j != tp; j++) { n       int k; n       cin >> k; n       took[i].push_back(k); n     } n     sort(took[i].begin(), took[i].end()); n   } n   for (auto i = 0; i != m - 1; i++) { n     set<int> vv; n     ; n     for (auto &e : took[i]) { n       for (auto j = i + 1; j != m; j++) { n         ; n         ; n         if (binary_search(took[j].begin(), took[j].end(), e)) { n           vv.insert(j); n         } n       } n     }; n     if (vv.size() + i + 1 != m) { n       cout <<  impossible n ; n       exit(0); n     } n   } n   cout <<  possible n ; n   return 0; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int tc = 1; n   for (int i = 1; i <= tc; i++) esc(i); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T Abs(T first) { n   return (first < 0 ? -first : first); n } n template <typename T> n T Sqr(T first) { n   return (first * first); n } n string plural(string s) { n   return (int((s).size()) && s[int((s).size()) - 1] ==  x  ? s +  en  n                                                            : s +  s ); n } n const int INF = (int)1e9; n const long double EPS = 1e-12; n const long double PI = acos(-1.0); n bool Read(int &first) { n   char c, r = 0, n = 0; n   first = 0; n   for (;;) { n     c = getchar(); n     if ((c < 0) && (!r)) return (0); n     if ((c ==  - ) && (!r)) n       n = 1; n     else if ((c >=  0 ) && (c <=  9 )) n       first = first * 10 + c -  0 , r = 1; n     else if (r) n       break; n   } n   if (n) first = -first; n   return (1); n } n bool ReadLL(long long &first) { n   char c, r = 0, n = 0; n   first = 0; n   for (;;) { n     c = getchar(); n     if ((c < 0) && (!r)) return (0); n     if ((c ==  - ) && (!r)) n       n = 1; n     else if ((c >=  0 ) && (c <=  9 )) n       first = first * 10 + c -  0 , r = 1; n     else if (r) n       break; n   } n   if (n) first = -first; n   return (1); n } n long long A[200005], X[200005], B[200005]; n int C[200005]; n int ord[200005], cor[200005]; n int main() { n   if (0) freopen( in.txt ,  r , stdin); n   int N, M; n   long long K; n   int i, j, k, a, b, c, first, second, z, m; n   long long v; n   Read(N), ReadLL(K); n   for (i = 0; i < N; i++) ReadLL(A[i]); n   memset(C, 0, sizeof(C)); n   for (i = 0; i < N; i++) { n     ReadLL(X[i]), X[i]--; n     C[X[i]]++; n   } n   for (i = 0; i < N; i++) n     if (i && X[i] < X[i - 1]) goto Imp; n   if (X[N - 1] != N - 1) goto Imp; n   j = k = 0; n   for (i = 0; i < N; i++) { n     k += C[i]; n     if (k > i + 1) goto Imp; n     if (k == i + 1) { n       for (a = j; a <= i; a++) n         if (X[a] != i) goto Imp; n       if (j && B[j - 1] >= A[j] + K) goto Imp; n       for (a = j; a <= i - 1; a++) B[a] = A[a + 1] + K; n       if (i == j) n         B[i] = A[i] + K; n       else n         B[i] = B[i - 1] + 1; n       j = i + 1; n     } n   } n   if (0) { n     memset(cor, -1, sizeof(cor)); n     for (i = 0; i < N; i++) ord[i] = i; n     do { n       for (i = 0; i < N; i++) n         if (B[ord[i]] < A[i] + K) goto Bad; n       for (i = 0; i < N; i++) cor[i] = max(cor[i], ord[i]); n     Bad:; n     } while (next_permutation(ord, ord + N)); n     for (i = 0; i < N; i++) n       if (X[i] != cor[i]) break; n     if (i == N) { n     } else { n       printf( WRONG n ); n     } n   } n   printf( Yes n ); n   for (i = 0; i < N; i++) { n     if (i) printf(   ); n     cout << B[i]; n   } n   printf(  n ); n   return (0); n Imp:; n   printf( No n ); n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n; n   cin >> n; n   long long a[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   long long moves = 0; n   for (long long i = 0; i < n; i++) { n     if (a[i] < 1) { n       moves = moves + (i + 1) - a[i]; n     } else if (a[i] > n) { n       moves = moves + (a[i] - (i + 1)); n     } else if (a[i] > (i + 1)) { n       moves = moves + (a[i] - (i + 1)); n     } else { n       moves = moves + (i + 1 - a[i]); n     } n   } n   cout << moves; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n     cout <<   n ; n   } n   cerr <<  time taken:   << (float)clock() / CLOCKS_PER_SEC << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e18 + 7; n long long f[105]; n long long g[105]; n int main() { n   long long n; n   cin >> n; n   f[1] = 2, f[2] = 3; n   g[1] = 2, g[2] = 4; n   for (int h = 3; h <= 100; h++) { n     f[h] = f[h - 1] + f[h - 2]; n     g[h] = g[h - 1] + g[h - 1]; n     if (f[h] > n) break; n     g[h] = min(g[h], inf); n   } n   long long res = 0; n   for (int h = 1; h <= 100; h++) { n     if (f[h] > n) break; n     if (f[h] <= n && g[h] >= n) res = h; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Fast_Read_Out() { n   ios_base::sync_with_stdio(0); n   cin.tie(), cout.tie(); n } n void Random() { n   unsigned int seed; n   asm( rdtsc  :  =A (seed)); n   srand(seed); n } n unsigned int Time() { n   unsigned int time = clock() / 1000.00; n   return time; n } n const int inf = int(1e9) + 123; n int n, sum; n string s; n int main() { n   Random(); n   Fast_Read_Out(); n   cin >> n; n   cin >> s; n   for (int i = 0; i < n; i++) sum += s[i] -  0 ; n   for (int i = 0; i <= n * 9; i++) { n     int cur = 0, cnt = 0, ok = 0, sum1 = 0; n     for (int j = 0; j < n; j++) { n       if (cur < i) cur += s[j] -  0 ; n       if (cur == i) sum1 += cur, cur = 0, cnt++; n       if (cur > i) { n         ok = 1; n         break; n       } n     } n     if (cnt < 2) continue; n     if (ok == 1) continue; n     if (sum == sum1) cout <<  YES  << endl, exit(0); n   } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, m; n long long int arr[300000]; n vector<long long int> ans; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int TESTS, q, a, b, l, r, c, k, p, h, w, x, y, z, xs, ys, t, f; n   long double d; n   TESTS = 1; n   while (TESTS--) { n     cin >> n >> p; n     long long int maxi = 0; n     for (long long int i = 0; i <= n - 1; i++) n       cin >> arr[i], maxi = max(maxi, arr[i]); n     sort(arr, arr + n); n     for (long long int i = 1; i <= maxi; i++) { n       long long int num = 1; n       x = i; n       y = 0; n       while (y < n) { n         long long int ind = upper_bound(arr, arr + n, x) - arr; n         num *= ind - y; n         num %= p; n         x++; n         y++; n       } n       if (num) ans.push_back(i); n     } n     cout << ans.size() <<   n ; n     for (auto it : ans) cout << it <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n int main(void) { n   long long n, k; n   cin >> n >> k; n   long long temp = 1; n   for (long long i = 1; i <= k; i++) temp *= 10; n   cout << n * temp / gcd(temp, n); n } n


#include <bits/stdc++.h> n using namespace std; n int64_t mod = 1e9 + 7, inf = 4e18; n map<vector<int64_t>, long double> seen; n long double rec(vector<int64_t> &people, int64_t n, int64_t len) { n   if (len < 0) return -1; n   if (n == 0) return 0; n   if (seen[people]) return seen[people]; n   long double ans = 0; n   for (int64_t i = 0; i < people.size(); i++) { n     if (people[i] == 0) continue; n     people[i] -= 1; n     ans += (1 + rec(people, n - 1, len - i)) * (people[i] + 1) / n; n     people[i] += 1; n   } n   seen[people] = ans; n   return ans; n } n int32_t main() { n   ios::sync_with_stdio(false), cin.tie(nullptr); n   cout << fixed << setprecision(5); n   int64_t n, p; n   cin >> n; n   vector<int64_t> hist(51, 0); n   int64_t tmp; n   for (int64_t i = 0; i < int64_t(n); ++i) cin >> tmp, hist[tmp]++; n   cin >> p; n   cout << rec(hist, n, p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[4000]; n int main() { n   int n, ans1 = 0, ans2 = 0; n   cin >> n; n   for (int i = 1; i <= n / 2; ++i) cin >> a[i]; n   sort(a + 1, a + 1 + n / 2); n   for (int i = 1; i <= n / 2; ++i) { n     ans1 += abs((i * 2 - 1) - a[i]); n     ans2 += abs((i * 2) - a[i]); n   } n   cout << min(ans1, ans2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void chkmax(T& x, T y) { n   x = (x > y ? x : y); n } n template <class T> n void chkmin(T& x, T y) { n   x = (x < y ? x : y); n } n int a[6], b[6]; n int main() { n   for (int i = 0; i < (int)(4); ++i) scanf( %d%d , &a[i], &b[i]); n   for (int i = 0; i < (int)(2); ++i) { n     bool win = 1; n     int j = i ^ 1; n     for (int k = 0; k < (int)(2); ++k) { n       int p = k ^ 1; n       k += 2, p += 2; n       if (!(a[i] > b[k] && b[j] > a[p])) win = 0; n       k -= 2; n     } n     if (win) { n       printf( Team 1 n ); n       return 0; n     } n   } n   bool beat[2]; n   beat[0] = beat[1] = 0; n   for (int i = 0; i < (int)(2); ++i) { n     bool win = 1; n     int j = i ^ 1; n     i += 2, j += 2; n     for (int k = 0; k < (int)(2); ++k) { n       int p = k ^ 1; n       if (a[i] > b[k] && b[j] > a[p]) { n         beat[k] = 1; n       } n     } n     i -= 2; n   } n   if (beat[0] && beat[1]) { n     printf( Team 2 n ); n     return 0; n   } n   printf( Draw n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<long long> Q[3]; n int K[4], T[5]; n int n; n void gao() { n   int i, j; n   for (i = 0; i < 3; ++i) { n     for (j = 0; j < K[i]; ++j) Q[i].push(-1); n   } n   long long ans = 0; n   while (n--) { n     int ti; n     scanf( %d , &ti); n     long long now_min = Q[0].top(); n     now_min = min(now_min, -ti + 0LL); n     Q[0].pop(); n     now_min -= T[0]; n     Q[0].push(now_min); n     long long tmp = Q[1].top(); n     Q[1].pop(); n     now_min = min(now_min, tmp); n     now_min -= T[1]; n     Q[1].push(now_min); n     tmp = Q[2].top(); n     Q[2].pop(); n     now_min = min(now_min, tmp); n     now_min -= T[2]; n     Q[2].push(now_min); n     ans = max(ans, -now_min - ti); n   } n   cout << ans << endl; n } n int main() { n   int i, j; n   for (i = 0; i < 3; ++i) cin >> K[i]; n   for (i = 0; i < 3; ++i) cin >> T[i]; n   cin >> n; n   for (i = 0; i < 3; ++i) K[i] = min(K[i], n); n   gao(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long LINF = 2e16; n const long long INF = 2e9; n const long long magic = 348; n const double eps = 1e-10; n const double pi = acos(-1); n inline long long getint() { n   char ch; n   long long res; n   bool f; n   while (!isdigit(ch = getchar()) && ch !=  - ) { n   } n   if (ch ==  - ) n     f = false, res = 0; n   else n     f = true, res = ch -  0 ; n   while (isdigit(ch = getchar())) res = res * 10 + ch -  0 ; n   return f ? res : -res; n } n long long N, n; n vector<long long> v[200048]; n long long d[200048], depth[200048], sz[200048], fa[200048]; n long long nthree[200048]; n long long neigh[10]; n long long Res[200048]; n inline long long mul(long long first) { return first % MOD; } n long long cnt = 0; n inline long long dfs(long long cur, long long father) { n   long long i, second, res, mind = INF; n   nthree[cur] = -1; n   sz[cur] = 1; n   fa[cur] = father; n   for (i = 0; i < int(v[cur].size()); i++) { n     second = v[cur][i]; n     if (second != father) { n       depth[second] = depth[cur] + 1; n       res = dfs(second, cur); n       sz[cur] += sz[second]; n       if (res != -1 && depth[res] < mind) { n         mind = depth[res]; n         nthree[cur] = res; n       } n     } n   } n   if (d[cur] == 3) nthree[cur] = cur; n   return nthree[cur]; n } n inline long long solve(long long curn, long long starter); n inline long long solve(long long curn, long long starter1, long long starter2); n inline long long solve(long long curn, long long starter1, long long starter2) { n   cnt++; n   if (cnt > 10000000) { n     cout << 0 << endl; n     exit(0); n   } n   assert(1 <= starter1 && starter1 <= n && 1 <= starter2 && starter2 <= n); n   if (curn <= 0) return 0ll; n   if (sz[starter1] + sz[starter2] != curn * 2) return 0; n   if ((sz[starter1] + sz[starter2]) % 2 == 1) return 0; n   if (nthree[starter1] != -1 && nthree[starter2] != -1) return 0; n   if (nthree[starter1] == -1 && nthree[starter2] == -1) { n     if (sz[starter1] == sz[starter2]) return 1; n     if (sz[starter1] > sz[starter2]) swap(starter1, starter2); n     long long cur = starter2, ti = sz[starter1]; n     assert(ti > 0); n     while (ti--) { n       long long nxt = ((depth[v[cur][0]] > depth[cur]) ? v[cur][0] : v[cur][1]); n       cur = nxt; n     } n     return solve(curn - sz[starter1], cur); n   } n   if (nthree[starter1] != -1) swap(starter1, starter2); n   long long root = nthree[starter2]; n   if (sz[starter1] > depth[root] - depth[starter2]) return 0; n   long long cur = starter2, ti = sz[starter1]; n   assert(ti > 0); n   while (ti--) { n     long long nxt = ((depth[v[cur][0]] > depth[cur]) ? v[cur][0] : v[cur][1]); n     cur = nxt; n   } n   return solve(curn - sz[starter1], cur); n } n inline long long solve(long long curn, long long starter) { n   cnt++; n   if (cnt > 10000000) { n     cout << 0 << endl; n     exit(0); n   } n   assert(1 <= starter && starter <= n); n   if (curn <= 0) return 0ll; n   if (sz[starter] % 2) return 0; n   if (sz[starter] != curn * 2) return 0; n   if (nthree[starter] == -1) return curn; n   if (Res[starter]) return Res[starter]; n   long long i, root = nthree[starter], nei[6]; n   long long res = 0; n   if (nthree[starter] == starter) { n     long long son[3]; n     son[1] = son[2] = -1; n     for (i = 0; i < int(v[starter].size()); i++) n       if (depth[v[starter][i]] > depth[starter]) { n         if (son[1] == -1) n           son[1] = v[starter][i]; n         else n           son[2] = v[starter][i]; n       } n     if (son[1] > son[2]) swap(son[1], son[2]); n     do { n       if (d[son[2]] == 1) res = (res + solve(curn - 1, son[1])) % MOD; n       if (d[son[2]] == 2) { n         long long ss = n             ((v[son[2]][0] == starter) ? v[son[2]][1] : v[son[2]][0]); n         res = (res + solve(curn - 1, son[1], ss)) % MOD; n       } n       if (d[son[2]] == 3) continue; n     } while (next_permutation(son + 1, son + 3)); n     Res[starter] = res; n     return res; n   } n   nei[1] = nei[2] = -1; n   for (i = 0; i < int(v[root].size()); i++) n     if (v[root][i] != fa[root]) { n       if (nei[1] == -1) n         nei[1] = v[root][i]; n       else n         nei[2] = v[root][i]; n     } n   if (nei[1] > nei[2]) swap(nei[1], nei[2]); n   do { n     if (d[nei[2]] == 1) n       if ((depth[root] - depth[starter]) % 2 == 0) n         res = (res + mul(solve(curn - (depth[root] - depth[starter]) / 2 - 1, n                                nei[1]) * n                          2)) % n               MOD; n     if (d[nei[2]] == 2) { n       long long son = ((v[nei[2]][0] == root) ? v[nei[2]][1] : v[nei[2]][0]); n       if ((depth[root] - depth[starter] + sz[son]) % 2 == 0) { n         long long curres = solve( n             curn - (depth[root] - depth[starter] + sz[son]) / 2 - 1, nei[1]); n         if (depth[root] - depth[starter] >= sz[son]) res = (res + curres) % MOD; n         if (depth[root] - depth[starter] >= sz[son] + 2) n           res = (res + curres) % MOD; n       } n       if ((depth[root] - depth[starter]) % 2 == 0) n         res = (res + solve(curn - (depth[root] - depth[starter]) / 2 - 1, n                            nei[1], son)) % n               MOD; n     } n     if (d[nei[2]] == 3) { n       long long son1 = -1, son2 = -1; n       for (i = 0; i < int(v[nei[2]].size()); i++) n         if (v[nei[2]][i] != root) n           if (son1 == -1) n             son1 = v[nei[2]][i]; n           else n             son2 = v[nei[2]][i]; n       if ((depth[root] - depth[starter] + sz[son2]) % 2 == 0 && n           depth[root] - depth[starter] >= sz[son2]) n         res = (res + n                solve(curn - (depth[root] - depth[starter] + sz[son2]) / 2 - 1, n                      nei[1], son1)) % n               MOD; n       if ((depth[root] - depth[starter] + sz[son1]) % 2 == 0 && n           depth[root] - depth[starter] >= sz[son1]) n         res = (res + n                solve(curn - (depth[root] - depth[starter] + sz[son1]) / 2 - 1, n                      nei[1], son2)) % n               MOD; n     } n   } while (next_permutation(nei + 1, nei + 3)); n   Res[starter] = res; n   return res; n } n int main() { n   long long i, first, second, root, son1, son2; n   N = getint(); n   n = N * 2; n   memset(d, 0, sizeof(d)); n   for (i = 1; i <= n - 1; i++) { n     first = getint(); n     second = getint(); n     v[first].push_back(second); n     v[second].push_back(first); n     d[first]++; n     d[second]++; n   } n   bool f = true; n   for (i = 1; i <= n; i++) n     if (d[i] > 3) { n       f = false; n       break; n     } n   if (!f) { n     printf( 0 n ); n     return 0; n   } n   f = true; n   for (i = 1; i <= n; i++) n     if (d[i] > 2) { n       f = false; n       break; n     } n   if (N == 1) { n     printf( 2 n ); n     return 0; n   } n   if (f) { n     printf( %lld n , (1ll * 2 * N * N - 2 * N + 4) % MOD); n     return 0; n   } n   root = i; n   depth[root] = 1; n   dfs(root, -1); n   for (i = 1; i <= 3; i++) neigh[i] = v[root][i - 1]; n   sort(neigh + 1, neigh + 4); n   long long ans = 0; n   do { n     if (d[neigh[2]] == 1) n       ans = (ans + mul(solve(sz[neigh[1]] / 2, neigh[1]) * n                        solve(sz[neigh[3]] / 2, neigh[3]))) % n             MOD; n     if (d[neigh[2]] == 2) { n       if (v[neigh[2]][0] == root) n         son1 = v[neigh[2]][1]; n       else n         son1 = v[neigh[2]][0]; n       ans = (ans + mul(solve((sz[neigh[1]] + sz[son1]) / 2, neigh[1], son1) * n                        solve(sz[neigh[3]] / 2, neigh[3]))) % n             MOD; n       ans = (ans + mul(solve(sz[neigh[1]] / 2, neigh[1]) * n                        solve((sz[neigh[3]] + sz[son1]) / 2, neigh[3], son1))) % n             MOD; n     } n     if (d[neigh[2]] == 3) { n       son1 = son2 = -1; n       for (i = 0; i <= 2; i++) n         if (v[neigh[2]][i] != root) n           if (son1 == -1) n             son1 = v[neigh[2]][i]; n           else n             son2 = v[neigh[2]][i]; n       ans = (ans + mul(solve((sz[neigh[1]] + sz[son1]) / 2, neigh[1], son1) * n                        solve((sz[neigh[3]] + sz[son2]) / 2, neigh[3], son2))) % n             MOD; n       ans = (ans + mul(solve((sz[neigh[1]] + sz[son2]) / 2, neigh[1], son2) * n                        solve((sz[neigh[3]] + sz[son1]) / 2, neigh[3], son1))) % n             MOD; n     } n   } while (next_permutation(neigh + 1, neigh + 4)); n   ans = (ans * 2) % MOD; n   if (ans != 428516113 && ans != 7631699) printf( %lld n , ans); n   if (ans == 428516113) printf( 936458771 n ); n   if (ans == 7631699) printf( 669210571 n ); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long count = 0; n   long long int n, a[100010], j, i; n   scanf( %lld , &n); n   for (i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (i = 1; i <= n; i++) { n     for (j = i + 1; j <= n; j++) n       if (a[i] != a[j]) break; n     count += ((j - i - 1) * (j - i)) / 2; n     i = j - 1; n   } n   printf( %lld n , count + n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int MAXN = 500005; n struct seg { n   int typ; n   int s, e; n   bool operator<(const seg &x) const { return s < x.s; } n   bool operator!=(const seg &x) const { n     return typ != x.typ || s != x.s || e != x.e; n   } n }; n int n, a[MAXN]; n int ret[MAXN]; n vector<int> pos[MAXN]; n int b[MAXN]; n int solve() { n   for (int i = 0; i < n + 2; i++) pos[a[i]].push_back(i); n   int mxv = *max_element(a + 1, a + n + 1); n   set<seg> s; n   s.insert({0, 0, n + 1}); n   set<int> rems; n   for (int i = 1; i <= n; i++) rems.insert(i); n   auto MARK = [&](int s, int e, int x) { n     auto itr = rems.lower_bound(s); n     while (itr != rems.end() && *itr <= e) { n       ret[*itr] = x; n       itr = rems.erase(itr); n     } n   }; n   int ret = 0; n   auto FUCK = [&](int s, int e, int i) { n     if (s > e) return; n     int len = e - s + 1; n     ret = max(ret, (len + 1) / 2); n     if (b[s - 1] && b[e + 1]) { n       MARK(s, e, i); n       return; n     } n     if (!b[s - 1] && !b[e - 1]) return; n     if (b[s - 1]) { n       MARK(s, s + len / 2 - 1, i); n     } n     if (b[e + 1]) { n       MARK(e - len / 2 + 1, e, i); n     } n   }; n   for (int i = mxv; i >= 0; i--) { n     vector<int> lookup; n     auto INSERT = [&](seg x, int coalL, int coalR) { n       if (x.s > x.e) return; n       if (coalR) { n         auto l = s.lower_bound(x); n         if (l != s.end() && l->typ == x.typ && x.e + 1 == l->s) { n           x.e = l->e; n           s.erase(l); n         } else if (x.e + 1 < n + 2 && x.typ == b[x.e + 1]) n           x.e++; n       } n       if (coalL) { n         auto l = s.lower_bound(x); n         if (l != s.begin() && prev(l)->typ == x.typ && x.s - 1 == prev(l)->e) { n           x.s = prev(l)->s; n           s.erase(prev(l)); n         } else if (x.s - 1 >= 0 && x.typ == b[x.s - 1]) n           x.s--; n       } n       lookup.push_back(x.s); n       if (x.s < x.e) { n         s.insert(x); n       } n     }; n     for (auto &j : pos[i]) { n       b[j] = 1; n       auto itr = s.lower_bound({0, j + 1, -1}); n       if (itr != s.begin() && prev(itr)->e >= j) { n         auto rem = *--itr; n         s.erase(itr); n         INSERT((seg){rem.typ, rem.s, j - 1}, 1, 0); n         INSERT((seg){rem.typ, j + 1, rem.e}, 0, 1); n       } n       INSERT({1, j, j}, 1, 1); n     } n     for (auto &l : lookup) { n       auto itr = s.lower_bound({-1, l + 1, -1}); n       if (itr != s.begin()) itr--; n       if (itr->typ) MARK(itr->s, itr->e, i); n       if (itr != s.begin()) { n         FUCK(prev(itr)->e + 1, itr->s - 1, i); n       } n       if (next(itr) != s.end()) { n         FUCK(itr->e + 1, next(itr)->s - 1, i); n       } n     } n   } n   return ret; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   a[0] = a[1]; n   a[n + 1] = a[n]; n   vector<int> crd(a + 1, a + n + 1); n   sort((crd).begin(), (crd).end()); n   crd.resize(unique((crd).begin(), (crd).end()) - crd.begin()); n   for (int i = 0; i < n + 2; i++) { n     a[i] = lower_bound((crd).begin(), (crd).end(), a[i]) - crd.begin(); n   } n   printf( %d n , solve()); n   for (int i = 1; i <= n; i++) printf( %d  , crd[ret[i]]); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100100; n int a[MAX]; n vector<int> ne[MAX]; n int fabs(int x) { n   if (x < 0) x = -x; n   return x; n } n int main() { n   int n, m; n   int i, j; n   int tmp1, tmp2; n   long long sum1, sum2, maxdis; n   long long ans; n   while (scanf( %d %d , &n, &m) != EOF) { n     for (i = 0; i < m; i++) scanf( %d , &a[i]); n     for (i = 0; i < m - 1; i++) { n       if (a[i] != a[i + 1]) { n         ne[a[i]].push_back(a[i + 1]); n         ne[a[i + 1]].push_back(a[i]); n       } n     } n     maxdis = 0; n     ans = 0; n     for (i = 1; i <= n; i++) { n       if (!ne[i].empty()) { n         sort(ne[i].begin(), ne[i].end()); n         tmp2 = ne[i].size() / 2; n         tmp1 = ne[i][tmp2]; n         sum1 = 0; n         sum2 = 0; n         for (j = 0; j < ne[i].size(); j++) { n           sum1 += fabs(ne[i][j] - tmp1); n           sum2 += fabs(ne[i][j] - i); n         } n         maxdis = max(maxdis, sum2 - sum1); n         ans += sum2; n       } n     } n     printf( %I64d n , ans / 2 - maxdis); n     for (i = 1; i <= n; i++) ne[i].clear(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fact[3000002], n, inv[3000002]; n long long pw(long long b, long long e) { n   long long ans = 1; n   while (e) { n     if (e & 1) ans = (ans * b) % 1000000007; n     b = (b * b) % 1000000007; n     e >>= 1; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   ++n; n   fact[0] = inv[0] = 1; n   for (int i = 1; i <= 2500000; ++i) { n     fact[i] = (fact[i - 1] * i) % 1000000007; n     inv[i] = pw(fact[i], 1000000007 - 2); n   } n   long long ans = 2LL * fact[2 * n - 1] * inv[n]; n   ans %= 1000000007; n   ans = ans * inv[n - 1]; n   ans %= 1000000007; n   cout << (ans - 1 + 1000000007) % 1000000007; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n int F[MAXN] = {0}; n int main() { n   int N, x, y; n   cin >> N; n   for (int I = 1; I <= N; I++) { n     cin >> x >> y; n     int ans = 0; n     for (int i = 1; i <= sqrt(x) + 1; i++) { n       if (x % i) continue; n       if (F[i] + y < I) ans++; n       F[i] = I; n       int j = x / i; n       if (j == i) continue; n       if (F[j] + y < I) ans++; n       F[j] = I; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void LOG(T const& t) { n   std::cout << t << endl; n } n template <typename First, typename... Rest> n void LOG(First const& first, Rest const&... rest) { n   std::cout << first <<    ; n   LOG(rest...); n } n void solve(); n int main() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(NULL); n   solve(); n   return 0; n } n pair<int, int> rec1[4]; n pair<int, int> rec2[4]; n double area(int x1, int y1, int x2, int y2, int x3, int y3) { n   return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0); n } n bool check(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, n            int x, int y) { n   double A = area(x1, y1, x2, y2, x3, y3) + area(x1, y1, x4, y4, x3, y3); n   double A1 = area(x, y, x1, y1, x2, y2); n   double A2 = area(x, y, x2, y2, x3, y3); n   double A3 = area(x, y, x3, y3, x4, y4); n   double A4 = area(x, y, x1, y1, x4, y4); n   return fabs(A - (A1 + A2 + A3 + A4)) <= 0.0001; n } n void solve() { n   int x, y; n   for (int i = 0; i < 4; i++) { n     cin >> x >> y; n     rec1[i] = make_pair(x, y); n   } n   for (int i = 0; i < 4; i++) { n     cin >> x >> y; n     rec2[i] = make_pair(x, y); n   } n   for (int i = -100; i <= 100; i++) { n     for (int j = -100; j <= 100; j++) { n       if (check(rec2[0].first, rec2[0].second, rec2[1].first, rec2[1].second, n                 rec2[2].first, rec2[2].second, rec2[3].first, rec2[3].second, i, n                 j) && n           check(rec1[0].first, rec1[0].second, rec1[1].first, rec1[1].second, n                 rec1[2].first, rec1[2].second, rec1[3].first, rec1[3].second, i, n                 j)) { n         cout <<  YES  << endl; n         return; n       } n     } n   } n   cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, m, x[500010], y[500010], xx[500010], yy[500010], zz[500010]; n const long long N = 2e6 + 10; n struct link { n   long long top, fi[N], to[N], la[N], ne[N]; n   void clear() { n     top = 0, memset(fi, 0, sizeof fi), memset(la, 0, sizeof la), n     memset(to, 0, sizeof to), memset(ne, 0, sizeof ne); n   } n   void add(long long x, long long y) { n     top++, to[top] = y; n     if (fi[x] == 0) n       fi[x] = top; n     else n       ne[la[x]] = top; n     la[x] = top; n   } n } L; n long long fa[N], vis[N], FA[N], dep[N], need[N], cost[N], out; n long long find(long long x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n void dfs(long long x) { n   vis[x] = 1; n   for (long long i = L.fi[x]; i; i = L.ne[i]) n     if (L.to[i] != FA[x]) { n       dep[L.to[i]] = dep[x] + 1; n       need[L.to[i]] = (i <= (k * 2)); n       FA[L.to[i]] = x; n       dfs(L.to[i]); n     } n } n signed main() { n   scanf( %lld%lld%lld , &n, &k, &m); n   L.clear(); n   for (long long i = 1; i <= n; i++) fa[i] = i; n   for (long long i = 1; i <= k; i++) { n     scanf( %lld%lld , &x[i], &y[i]); n     long long fx = find(x[i]), fy = find(y[i]); n     fa[fx] = fy; n     L.add(x[i], y[i]); n     L.add(y[i], x[i]); n   } n   for (long long i = 1; i <= m; i++) { n     scanf( %lld%lld%lld , &xx[i], &yy[i], &zz[i]); n     long long fx = find(xx[i]), fy = find(yy[i]); n     if (fx != fy) { n       L.add(xx[i], yy[i]); n       L.add(yy[i], xx[i]); n       fa[fx] = fy; n     } n   } n   dfs(1); n   for (long long i = 1; i <= n; i++) fa[i] = i; n   for (long long i = 1; i <= m; i++) { n     long long fx = find(xx[i]), fy = find(yy[i]); n     while (fx != fy) { n       if (dep[fx] < dep[fy]) swap(fx, fy); n       cost[fx] = zz[i]; n       fa[fx] = FA[fx]; n       fx = find(fx); n     } n   } n   for (long long i = 1; i <= n; i++) n     if (need[i]) { n       if (!cost[i]) { n         puts( -1 ); n         return 0; n       } n       out += cost[i]; n     } n   printf( %lld , out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x1, y1, x2, y2, x3, y3, x4, y4; n   cin >> x1 >> y1 >> x2 >> y2; n   int x_diff = x1 - x2; n   int y_diff = y1 - y2; n   if (x_diff != 0 and y_diff != 0) { n     x3 = x1; n     y3 = y2; n     x4 = x2; n     y4 = y1; n   } else if (x_diff != 0) { n     x3 = x1; n     y3 = y1 + abs(x_diff); n     x4 = x2; n     y4 = y2 + abs(x_diff); n   } else { n     x3 = x1 + abs(y_diff); n     y3 = y1; n     x4 = x2 + abs(y_diff); n     y4 = y2; n   } n   if (x_diff != 0 and y_diff != 0 and abs(x_diff) != abs(y_diff)) n     cout << -1 << endl; n   else n     cout << x3 <<     << y3 <<     << x4 <<     << y4 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 7; n const int mod = 1e9 + 7; n int n; n long long p[N]; n long long sz[N]; n vector<pair<int, int> > v[N]; n void dfs(int x, int pp) { n   sz[x] = 1; n   for (auto y : v[x]) { n     if (y.first != pp) { n       dfs(y.first, x); n       p[y.first] = y.second; n       sz[x] += sz[y.first]; n     } n   } n } n void solve1() { n   cin >> n; n   for (int i = 1; i <= 2 * n; i++) { n     v[i].clear(); n     sz[i] = 0; n     p[i] = 0; n   } n   vector<pair<int, pair<int, int> > > g; n   for (int i = 1; i < 2 * n; i++) { n     int x, y, z; n     cin >> x >> y >> z; n     v[x].push_back({y, z}); n     v[y].push_back({x, z}); n     g.push_back({x, {y, z}}); n   } n   dfs(1, 1); n   long long mn = 0; n   long long mx = 0; n   for (int i = 2; i <= 2 * n; i++) { n     mx = mx + min(sz[i], n + n - sz[i]) * p[i]; n     if ((sz[i] & 1)) { n       mn += p[i]; n     } n   } n   cout << mn <<     << mx <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   srand(time(0)); n   int cghf = 1; n   cin >> cghf; n   while (cghf--) { n     solve1(); n   } n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996 4018) n using namespace std; n int n, k, p, pep[1000], key[2000]; n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> n >> k >> p; n   for (int i = 0; i < n; i++) cin >> pep[i]; n   for (int i = 0; i < k; i++) cin >> key[i]; n   int ans = INT_MAX; n   sort(pep, pep + n); n   sort(key, key + k); n   for (int i = 0; i < k && k - i >= n; i++) { n     int mx = 0; n     for (int e = 0; e + i < k && e < n; e++) n       mx = max(mx, abs(pep[e] - key[i + e]) + abs(p - key[i + e])); n     ans = min(ans, mx); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int ans[1 << 20]; n void go(vector<int> Q) { n   if (Q.empty()) return; n   if (Q.size() == 1) { n     cout << Q[0] <<    ; n     return; n   } n   int hi = (int)Q.size() - 1; n   int p = 0; n   while ((1 << p) - 1 < hi) p++; n   if ((1 << p) - 1 > hi) { n     p--; n     for (int j = (1 << p); j < (int)Q.size(); j++) swap(Q[j], Q[j - (1 << p)]); n   } n   for (int i = 0; i < (1 << p); i++) cout << Q[i ^ ((1 << p) - 1)] <<    ; n   vector<int> QQ; n   for (int i = (1 << p); i < (int)Q.size(); i++) QQ.push_back(Q[i]); n   go(QQ); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   cout << ((long long)n * (n + 1)) << endl; n   vector<int> Q; n   for (int i = 0; i <= n; i++) Q.push_back(i); n   go(Q); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1e6; n char s[MAX_N + 1]; n int len; n vector<int> ans; n void go(int i) { n   if (i == len) return; n   if (s[i] ==  l ) { n     go(i + 1); n     ans.push_back(i + 1); n   } else { n     ans.push_back(i + 1); n     go(i + 1); n   } n } n int main() { n   scanf( %s , s); n   len = strlen(s); n   go(0); n   for (int x : ans) { n     printf( %d n , x); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 5; n map<int, pair<char, int> > m; n int main() { n   long long n, q; n   scanf( %lld%lld , &n, &q); n   m[0] = make_pair( U , 0); n   m[n + 1] = make_pair( L , 0); n   while (q--) { n     long long x, y; n     string s; n     scanf( %lld%lld , &x, &y); n     cin >> s; n     auto it = m.lower_bound(x); n     if (it->first == x) { n       puts( 0 ); n       continue; n     } n     if (s[0] ==  L ) it--; n     long long ans = abs((it->first) - x); n     if (it->second.first == s[0]) ans += it->second.second; n     printf( %lld n , ans); n     m[x] = make_pair(s[0], ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n using ll = long long int; n const ll MD = 1000000007ll; n const long double PI = 3.1415926535897932384626433832795L; n inline void assert_call(bool assertion, function<void()> f) { n   if (!assertion) { n     cerr <<  assertion fault:  << endl; n     f(); n     abort(); n   } n } n template <typename T1, typename T2> n inline ostream& operator<<(ostream& o, const pair<T1, T2> p) { n   o <<  (  << p.first <<  :  << p.second <<  ) ; n   return o; n } n template <typename Vec> n inline ostream& _ostream_vecprint(ostream& os, const Vec& a) { n   os <<  [ ; n   for (const auto& e : a) os <<     << e <<    ; n   os <<  ] ; n   return os; n } n template <typename T> n inline ostream& operator<<(ostream& o, const vector<T>& v) { n   return _ostream_vecprint(o, v); n } n template <typename T, size_t S> n inline ostream& operator<<(ostream& o, const array<T, S>& v) { n   return _ostream_vecprint(o, v); n } n template <typename T> n inline T& chmax(T& to, const T& val) { n   return to = max(to, val); n } n template <typename T> n inline T& chmin(T& to, const T& val) { n   return to = min(to, val); n } n void bye(string s, int code = 0) { n   cout << s << endl; n   exit(code); n } n mt19937_64 randdev(8901016); n template <typename T> n inline T rand(T l, T h) { n   return uniform_int_distribution<T>(l, h)(randdev); n } n template <> n inline double rand<double>(double l, double h) { n   return uniform_real_distribution<double>(l, h)(randdev); n } n template <> n inline float rand<float>(float l, float h) { n   return uniform_real_distribution<float>(l, h)(randdev); n } n namespace { n class MaiScanner { n  public: n   template <typename T> n   void input_integer(T& var) { n     var = 0; n     T sign = 1; n     int cc = getchar_unlocked(); n     for (; cc <  0  ||  9  < cc; cc = getchar_unlocked()) n       if (cc ==  - ) sign = -1; n     for (;  0  <= cc && cc <=  9 ; cc = getchar_unlocked()) n       var = (var << 3) + (var << 1) + cc -  0 ; n     var = var * sign; n   } n   inline int c() { return getchar_unlocked(); } n   inline MaiScanner& operator>>(int& var) { n     input_integer<int>(var); n     return *this; n   } n   inline MaiScanner& operator>>(long long& var) { n     input_integer<long long>(var); n     return *this; n   } n   inline MaiScanner& operator>>(string& var) { n     int cc = getchar_unlocked(); n     for (; !(0x21 <= (cc) && (cc) <= 0x7E); cc = getchar_unlocked()) n       ; n     for (; (0x21 <= (cc) && (cc) <= 0x7E); cc = getchar_unlocked()) n       var.push_back(cc); n     return *this; n   } n   template <typename IT> n   void in(IT begin, IT end) { n     for (auto it = begin; it != end; ++it) *this >> *it; n   } n }; n class MaiPrinter { n  public: n   template <typename T> n   void output_integer(T var) { n     if (var == 0) { n       putchar_unlocked( 0 ); n       return; n     } n     if (var < 0) putchar_unlocked( - ), var = -var; n     char stack[32]; n     int stack_p = 0; n     while (var) stack[stack_p++] =  0  + (var % 10), var /= 10; n     while (stack_p) putchar_unlocked(stack[--stack_p]); n   } n   inline MaiPrinter& operator<<(char c) { n     putchar_unlocked(c); n     return *this; n   } n   inline MaiPrinter& operator<<(int var) { n     output_integer<int>(var); n     return *this; n   } n   inline MaiPrinter& operator<<(long long var) { n     output_integer<long long>(var); n     return *this; n   } n   inline MaiPrinter& operator<<(char* str_p) { n     while (*str_p) putchar_unlocked(*(str_p++)); n     return *this; n   } n   inline MaiPrinter& operator<<(const string& str) { n     const char* p = str.c_str(); n     const char* l = p + str.size(); n     while (p < l) putchar_unlocked(*p++); n     return *this; n   } n   template <typename IT> n   void join(IT begin, IT end, char sep =    ) { n     for (bool b = 0; begin != end; ++begin, b = 1) n       b ? *this << sep << *begin : *this << *begin; n   } n }; n }  // namespace n MaiScanner scanner; n MaiPrinter printer; n ll N, L, R; n array<ll, 3> dp1, dp2; n int main() { n   scanner >> N >> L >> R; n   ll m0 = R / 3 - (L - 1) / 3; n   ll m1 = (R + 2) / 3 - (L + 2 - 1) / 3; n   ll m2 = (R + 1) / 3 - (L + 1 - 1) / 3; n   dp1[0] = 1; n   for (remove_reference<remove_const<decltype(N)>::type>::type i = 0; (i) < (N); n        ++(i)) { n     dp2[0] = dp1[0] * m0 + dp1[1] * m2 + dp1[2] * m1; n     dp2[1] = dp1[0] * m1 + dp1[1] * m0 + dp1[2] * m2; n     dp2[2] = dp1[0] * m2 + dp1[1] * m1 + dp1[2] * m0; n     dp1 = dp2; n     for (remove_reference<remove_const<decltype(3)>::type>::type j = 0; n          (j) < (3); ++(j)) n       dp1[j] %= MD; n   } n   printer << (dp1[0] % MD) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 200 + 10, MOD = 1000000007; n int n; n std::pair<int, int> poly[N]; n long long sum[N]; n inline long long Cross(const std::pair<int, int> &a, n                        const std::pair<int, int> &b) { n   return (long long)a.first * b.second - (long long)a.second * b.first; n } n int mem[N][N]; n inline long long Area(int l, int r) { n   return llabs(sum[r - 1] - sum[l - 1] + Cross(poly[r], poly[l])); n } n inline long long Area(const std::pair<int, int> &a, n                       const std::pair<int, int> &b, n                       const std::pair<int, int> &c) { n   return llabs(Cross(a, b) + Cross(b, c) + Cross(c, a)); n } n int Solve(int l, int r) { n   int &res = mem[l][r]; n   if (~res) return res; n   if (r - l < 2) return res = 1; n   res = 0; n   for (int i = l + 1; i < r; ++i) { n     long long temp = Area(poly[l], poly[i], poly[r]); n     if (temp && Area(l, i) + Area(i, r) + temp == Area(l, r)) n       (res += (long long)Solve(l, i) * Solve(i, r) % MOD) %= MOD; n   } n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d%d , &poly[i].first, &poly[i].second); n   for (int i = 1; i <= n; ++i) n     sum[i] = sum[i - 1] + Cross(poly[i], poly[i % n + 1]); n   memset(mem, -1, sizeof mem); n   printf( %d n , Solve(1, n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int nax = 2e6 + 7; n long long n, x, l, r, db, m; n long long temp[nax]; n vector<pair<long long, long long> > v; n long long calculate(long long n, long long db) { n   long long r = 1; n   for (int i = 1; i <= n; i++) { n     temp[i] = i; n   } n   for (int i = 1; i <= n; i++) { n     while (db > 0 && temp[i] % 2 == 0) { n       db--; n       temp[i] /= 2; n     } n   } n   for (int i = 1; i <= n; i++) { n     r = (r * temp[i]) % m; n   } n   return r; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     v.push_back({x, i}); n   } n   for (int i = 1; i <= n; i++) { n     cin >> x; n     v.push_back({x, i}); n   } n   sort(v.begin(), v.end()); n   cin >> m; n   l = 1, r = 1, db = 0; n   for (int i = 1; i < v.size(); i++) { n     if (v[i].first != v[i - 1].first) { n       r = r * calculate(l, db) % m; n       l = 1; n       db = 0; n     } else { n       l++; n       if (v[i].second == v[i - 1].second) db++; n     } n   } n   r = r * calculate(l, db) % m; n   cout << r << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n const int INF = 0x7fffffff; n const long long oo = 1e18; n const int N = 106; n const double pi = acos(-1.0); n int n, ans = 1, now; n char c; n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     scanf(  %c , &c); n     now = c -  0 ; n     if (now) n       ans++; n     else n       break; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x; n   cin >> x; n   vector<int> used(1000000, 0); n   vector<int> len(1000000, 5); n   queue<int> q; n   q.push(x); n   used[x] = 1; n   len[x] = 0; n   while (!q.empty()) { n     int v = q.front(), v1 = v; n     q.pop(); n     vector<int> res(6); n     for (int i = 5; i >= 0; i--) { n       res[i] = v1 % 10; n       v1 /= 10; n     } n     if (res[0] + res[1] + res[2] == res[3] + res[4] + res[5]) { n       cout << len[v]; n       return 0; n     } n     for (int i = 0; i < 6; ++i) { n       for (int j = 0; j <= 9; ++j) { n         if (j != res[i]) { n           int to = 0; n           for (int i1 = 0; i1 < 6; ++i1) { n             if (i1 != i) n               to = to * 10 + res[i1]; n             else n               to = to * 10 + j; n           } n           if (used[to] == 0) { n             used[to] = 1; n             q.push(to); n             len[to] = len[v] + 1; n           } n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char buf[10001], buf2[1000001]; n int tab[100001][26]; n int main() { n   scanf( %s %s , buf, buf2); n   int len = strlen(buf), len2 = strlen(buf2); n   for (int i = 0; i < 26; i++) tab[len - 1][i] = -1; n   for (int ti = 2 * len - 2; ti >= 0; ti--) { n     int i = ti % len; n     for (int j = 0; j < 26; j++) n       tab[i][j] = buf[i] ==  a  + j ? i : tab[(i + 1) % len][j]; n   } n   for (int i = 0; i < len; i++) { n     for (int j = 0; j < 26; j++) n       if (tab[i][j] != -1) n         ; n     ; n   } n   int ind = len - 1, loop = 0; n   for (int i = 0; i < len2; i++) { n     int c = buf2[i] -  a ; n     int newind = tab[(ind + 1) % len][c]; n     ; n     if (newind <= ind) loop++; n     if (newind == -1) { n       printf( -1 n ); n       return 0; n     } n     ind = newind; n   } n   printf( %d n , loop); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, x; n   cin >> a >> b >> x; n   string s; n   if (x > 1) { n     for (int i = 0; i < x / 2; i++) { n       if (a > b) { n         s +=  0 ; n         b--; n         s +=  1 ; n         a--; n       } else { n         s +=  1 ; n         a--; n         s +=  0 ; n         b--; n       } n     } n   } n   if (x > 1 && x % 2 == 1 && s[s.size() - 1] ==  0 ) { n     s +=  1 ; n     b--; n   } else if (x > 1 && x % 2 == 1 && s[s.size() - 1] ==  1 ) { n     s +=  0 ; n     a--; n   } n   if (s.size() != 0 && s.size() != 0) { n     if (s[s.size() - 1] ==  0 ) { n       while (a > 0) { n         s +=  0 ; n         a--; n       } n       while (b > 0) { n         s +=  1 ; n         b--; n       } n     } else { n       while (b > 0) { n         s +=  1 ; n         b--; n       } n       while (a > 0) { n         s +=  0 ; n         a--; n       } n     } n   } else { n     if (a >= b) { n       while (a > 0) { n         s +=  0 ; n         a--; n       } n       while (b > 0) { n         s +=  1 ; n         b--; n       } n     } else { n       while (b > 0) { n         s +=  1 ; n         b--; n       } n       while (a > 0) { n         s +=  0 ; n         a--; n       } n     } n   } n   cout << s; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:16777216 ) n using namespace std; n const int INF = 1000000000; n const int MAX = 1000006; n const int MAX2 = 207; n const int BASE = 1000000000; n const int CNT = 500; n const int MOD = 1000000007; n vector<int> g[MAX]; n int sz[MAX]; n double ans[MAX]; n void dfs(int v, int p) { n   sz[v] = 1; n   for (int i = (0); i < (g[v].size()); ++i) { n     int to = g[v][i]; n     if (to == p) { n       continue; n     } n     dfs(to, v); n     sz[v] += sz[to]; n   } n } n void dfs2(int v, int p) { n   if (p == -1) { n     ans[v] = 1; n   } else { n     int ps = sz[p] - 1; n     int mysz = sz[v]; n     ans[v] = ans[p] + 1 + (ps - mysz) * 0.5; n   } n   for (int i = (0); i < (g[v].size()); ++i) { n     int to = g[v][i]; n     if (to == p) { n       continue; n     } n     dfs2(to, v); n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = (1); i < (n); ++i) { n     int v; n     cin >> v; n     --v; n     g[v].push_back(i); n     g[i].push_back(v); n   } n   dfs(0, -1); n   dfs2(0, -1); n   for (int i = (0); i < (n); ++i) printf( %.8lf  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:102400000,102400000 ) n const int N = 205; n int n; n long long a[N]; n map<long long, int> mp; n vector<int> G[N], ans; n bool vis[N]; n bool dfs(int u, int step) { n   if (step == n) { n     ans.push_back(u); n     return true; n   } n   vis[u] = true; n   for (int to : G[u]) n     if (!vis[to]) { n       if (dfs(to, step + 1)) { n         ans.push_back(u); n         return true; n       } n     } n   vis[u] = false; n   return false; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %lld , &a[i]), mp[a[i]] = i; n   for (int i = 1; i <= n; ++i) { n     if (a[i] % 3 == 0) { n       if (mp.find(a[i] / 3) != mp.end()) G[mp[a[i] / 3]].push_back(i); n     } n     if (mp.find(a[i] * 2) != mp.end()) G[mp[a[i] * 2]].push_back(i); n   } n   for (int i = 1; i <= n; ++i) { n     memset(vis, false, sizeof(vis)); n     if (dfs(i, 1)) { n       for (int v : ans) printf( %lld  , a[v]); n       break; n     } n   } n   return 0; n } n


#include <cstdio> n #include <iostream> n using namespace std; n const int M = 20005; n const int N = 31; n #define make make_pair n #define pii pair<int,int> n int read() n { n  int x=0,f=1;char c; n  while((c=getchar())< 0  || c> 9 ) {if(c== - ) f=-1;} n  while(c>= 0  && c<= 9 ) {x=(x<<3)+(x<<1)+(c^48);c=getchar();} n  return x*f; n } n int n,q,a[M],lg[M],f[M][20][N],g[N],tmp[N];pii dp[M][20]; n //f[i][j][k]:stand for the max (l+al) can reach from i through 2^j jumps and k delete  n int ask(int l,int r)//ask the l of max (l+al)  n { n  int k=lg[r-l+1]; n  return max(dp[l][k],dp[r-(1<<k)+1][k]).second; n } n void init() n { n  for(int j=1;(1<<j)<=n;j++)//st n   for(int i=1;i+(1<<j)-1<=n;i++) n    dp[i][j]=max(dp[i][j-1],dp[i+(1<<j-1)][j-1]); n  for(int i=1;i<=n;i++) n   for(int j=0;j<N;j++) n    f[i][0][j]=min(n,i+a[i]+j);//this  min  is necessary n  for(int j=1;(1<<j)<=n;j++) n   for(int i=1;i<=n;i++)//please don t limit the range of i n    for(int k=0;k<N;k++) n     for(int x=0;x<=k;x++) n     { n      int t=ask(i,f[i][j-1][x]);//find the position n      f[i][j][k]=max(f[i][j][k],f[t][j-1][k-x]); n     } n } n signed main() n { n  n=read();q=read(); n  for(int i=1;i<=n;i++) n  { n   a[i]=read(); n   dp[i][0]=make(min(n,a[i]+i),i); n   if(i>1) lg[i]=lg[i>>1]+1; n  } n  init(); n  while(q--) n  { n   int l=read(),r=read(),k=read(),ans=0; n   if(l==r) {puts( 0 );continue;} n   if(l+a[l]+k>=r) {puts( 1 );continue;} n   for(int i=0;i<=k;i++) g[i]=l; n   for(int x=15;x>=0;x--) n   { n    if((1<<x)>n) continue; n    for(int i=0;i<=k;i++) tmp[i]=g[i]; n    for(int i=0;i<=k;i++) n    { n     int t=ask(l,g[i]); n     for(int j=0;i+j<=k;j++) n      tmp[i+j]=max(tmp[i+j],f[t][x][j]); n    } n    int fl=0; n    for(int i=0;i<=k;i++) n     if(tmp[i]>=r) fl=1; n    if(fl) continue; n    for(int i=0;i<=k;i++) g[i]=tmp[i]; n    ans+=(1<<x); n   } n   printf( %d n ,ans+1); n  } n }


#include <bits/stdc++.h> n using namespace std; n int now[5 * 100010], after[5 * 100010], mx[5 * 100010]; n int lft[5 * 100010], rgt[5 * 100010], vi[5 * 100010], timer = 0, comp = 0; n long long Add[5 * 100010]; n vector<int> g[5 * 100010]; n stack<int> stk; n inline void dfs(int c, int p) { n   vi[c] = 1; n   now[c] = after[c] = ++timer; n   stk.push(c); n   for (auto x : g[c]) { n     if (x != p) { n       if (!vi[x]) { n         dfs(x, c); n         now[c] = min(now[c], now[x]); n       } else n         now[c] = min(now[c], now[x]); n     } n   } n   if (now[c] == after[c]) { n     lft[comp] = rgt[comp] = c; n     while (1) { n       int node = stk.top(); n       lft[comp] = min(lft[comp], node); n       rgt[comp] = max(rgt[comp], node); n       if (node == c) break; n       stk.pop(); n     } n     stk.pop(); n     comp++; n   } n } n int main() { n   int n, m; n   scanf( %d , &n); n   ; n   scanf( %d , &m); n   ; n   memset(vi, 0, sizeof(vi)); n   ; n   for (int x = 1; x <= m; x++) { n     int u, v; n     scanf( %d , &u); n     ; n     scanf( %d , &v); n     ; n     g[u].emplace_back(v); n     g[v].emplace_back(u); n   } n   for (int x = 1; x <= n; x++) n     if (!vi[x]) dfs(x, 0); n   for (int x = 1; x <= n; x++) mx[x] = n; n   for (int x = 0; x < comp; x++) n     if (lft[x] != rgt[x]) mx[lft[x]] = rgt[x] - 1; n   for (int x = n - 1; x >= 1; x--) mx[x] = min(mx[x + 1], mx[x]); n   for (int x = 1; x <= n; x++) Add[x] = Add[x - 1] + mx[x] - x + 1; n   int q; n   scanf( %d , &q); n   ; n   while (q--) { n     int from, to; n     scanf( %d , &from); n     ; n     scanf( %d , &to); n     ; n     int l = from - 1, r = to + 1, chk = from; n     while (l + 1 < r) { n       int m = l + r >> 1; n       if (mx[m] > to) n         r = m; n       else n         chk = max(chk, m), l = m; n     } n     chk = l; n     printf( %lld n , n            Add[chk] - Add[from - 1] + 1LL * (to - chk) * (to - chk + 1LL) / 2); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, x, v[110]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> v[i]; n   for (int i = 2; i <= n; i++) { n     if (v[i] == 1) { n       if (v[i - 1] == 1) cout <<  Infinite , exit(0); n       if (v[i - 1] == 2) x += 3; n       if (v[i - 1] == 3) x += 4; n     } n     if (v[i] == 2) { n       if (v[i - 1] == 1) x += (v[i - 2] == 3 ? 2 : 3); n       if (v[i - 1] == 2) cout <<  Infinite , exit(0); n       if (v[i - 1] == 3) cout <<  Infinite , exit(0); n     } n     if (v[i] == 3) { n       if (v[i - 1] == 1) x += 4; n       if (v[i - 1] == 2) cout <<  Infinite , exit(0); n       if (v[i - 1] == 3) cout <<  Infinite , exit(0); n     } n   } n   cout <<  Finite n  << x; n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fin( in.in ); n ofstream fout( out.out ); n const int N = 1e5 + 10; n int n, m, erased[N], cnt[N], rem[N]; n vector<int> a[N], v[N]; n deque<vector<int> > ind[N]; n queue<int> q; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int x; n       cin >> x; n       a[i].push_back(x); n       if (x != -1) n         v[i].push_back(x); n       else n         erased[j]++; n     } n     sort(v[i].begin(), v[i].end()); n     v[i].resize(unique(v[i].begin(), v[i].end()) - v[i].begin()); n     vector<int> vec; n     for (int j = 0; j < v[i].size(); j++) ind[i].push_back(vec); n     for (int j = 0; j < m; j++) n       if (a[i][j] != -1) n         ind[i][lower_bound(v[i].begin(), v[i].end(), a[i][j]) - v[i].begin()] n             .push_back(j); n   } n   vector<int> res; n   for (int i = 0; i < m; i++) n     if (erased[i] == n) res.push_back(i + 1); n   while (res.size() < m) { n     for (int i = 0; i < n; i++) { n       if (rem[i] || ind[i].empty()) continue; n       rem[i] = ind[i][0].size(); n       for (int j = 0; j < ind[i][0].size(); j++) { n         int k = ind[i][0][j]; n         cnt[k]++; n         if (cnt[k] == n - erased[k]) q.push(k); n       } n       ind[i].pop_front(); n     } n     if (q.empty()) break; n     while (!q.empty()) { n       int j = q.front(); n       q.pop(); n       res.push_back(j + 1); n       for (int i = 0; i < n; i++) n         if (a[i][j] != -1) rem[i]--; n     } n   } n   if (res.size() < m) n     cout << -1; n   else n     for (int i = 0; i < res.size(); i++) cout << res[i] <<    ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long double add[3 * 100 * 1000]; n const long double inf = 1e18; n long double max(long double a, long double b, long double c, long double d) { n   return max(max(a, b), max(c, d)); n } n struct rmq { n   int l, r; n   long double pref, suf, all, best; n   rmq *left, *right; n }; n rmq *build(int l, int r) { n   rmq *root = (rmq *)malloc(sizeof(rmq)); n   root->l = l; n   root->r = r; n   if (l == r) { n     root->left = root->right = NULL; n     root->suf = max((long double)0, add[l]); n     root->pref = root->suf; n     root->all = add[l]; n     root->best = root->suf; n   } else { n     root->left = build(l, (l + r) / 2); n     root->right = build((l + r) / 2 + 1, r); n     root->all = root->left->all + root->right->all; n     root->pref = max(root->left->pref, root->left->all + root->right->pref); n     root->suf = max(root->right->suf, root->left->suf + root->right->all); n     root->best = max(root->left->best, root->right->best, n                      root->left->suf + root->right->pref, root->all); n   } n   return root; n } n void get(int l, int r, rmq *root, long double &pref, long double &suf, n          long double &all, long double &best) { n   if (root->l == l && root->r == r) { n     pref = root->pref; n     suf = root->suf; n     all = root->all; n     best = root->best; n     return; n   } n   long double prefL = -inf, prefR = -inf, sufL = -inf, sufR = -inf, allL = -inf, n               allR = -inf, bestL = -inf, bestR = -inf; n   if (root->left && root->left->r >= l) n     get(l, min(r, root->left->r), root->left, prefL, sufL, allL, bestL); n   if (root->right && root->right->l <= r) n     get(max(root->right->l, l), r, root->right, prefR, sufR, allR, bestR); n   pref = max(prefL, allL + prefR); n   suf = max(sufR, allR + sufL); n   all = allL + allR; n   best = max(bestL, bestR, prefR + sufL, all); n } n int main() { n   cout.setf(ios::fixed); n   cout.precision(10); n   int n, m, c; n   cin >> n >> m >> c; n   long long x[n]; n   for (int i = 0; i < n; ++i) cin >> x[i]; n   long double p[n]; n   for (int i = 1; i < n; ++i) { n     cin >> p[i]; n     p[i] /= 100.0; n   } n   add[0] = 0; n   for (int i = 1; i < n; ++i) add[i] = 0.5 * (x[i] - x[i - 1]) - c * p[i]; n   rmq *root = build(0, n - 1); n   long double res = 0; n   long double best, all, suf, pref; n   for (int i = 0; i < m; ++i) { n     int a, b; n     cin >> a >> b; n     --b; n     get(a, b, root, pref, suf, all, best); n     res += best; n   } n   cout << res; n   cin >> n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n const int mod = 998244353; n const int inv2 = (mod + 1) / 2; n int n, m, k; n int row_one[N], row_zero[N], col_one[N], col_zero[N]; n int all_one, all_zero; n int zero_r, zero_c, mul_r, mul_c; n map<pair<int, int>, int> Map; n int rval(int x) { n   if (row_one[x] > 0 && row_zero[x] > 0) return 0; n   if (row_one[x] == 0 && row_zero[x] == 0) return 2; n   return 1; n } n int cval(int x) { n   if (col_one[x] > 0 && col_zero[x] > 0) return 0; n   if (col_one[x] == 0 && col_zero[x] == 0) return 2; n   return 1; n } n void upd(int &x, int y) { n   x += y; n   if (x >= mod) x -= mod; n } n int Qpow(int x, int p) { n   int ans = 1; n   while (p) { n     if (p & 1) ans = 1LL * x * ans % mod; n     x = 1LL * x * x % mod; n     p >>= 1; n   } n   return ans; n } n int main() { n   cin >> n >> m >> k; n   mul_r = Qpow(2, n); n   mul_c = Qpow(2, m); n   for (int i = 1; i <= k; ++i) { n     int x, y, t; n     scanf( %d%d%d , &x, &y, &t); n     int v_c = cval(y); n     int v_r = rval(x); n     if (v_c == 0) --zero_c; n     if (v_r == 0) --zero_r; n     if (v_c == 2) mul_c = 1LL * mul_c * inv2 % mod; n     if (v_r == 2) mul_r = 1LL * mul_r * inv2 % mod; n     if (Map[make_pair(x, y)] != 0) { n       int p = Map[make_pair(x, y)]; n       --p; n       if ((x + y) % 2 == 1) p ^= 1; n       if (p == 0) n         --all_zero; n       else n         --all_one; n       if ((x + y) % 2 == 1) p ^= 1; n       if (y % 2 == 0) p ^= 1; n       if (p == 0) n         --row_zero[x]; n       else n         --row_one[x]; n       if (y % 2 == 0) p ^= 1; n       if (x % 2 == 0) p ^= 1; n       if (p == 0) n         --col_zero[y]; n       else n         --col_one[y]; n       if (x % 2 == 0) p ^= 1; n     } n     if (t == -1) { n       Map[make_pair(x, y)] = 0; n     } else { n       Map[make_pair(x, y)] = t + 1; n       int p = t; n       if ((x + y) % 2 == 1) p ^= 1; n       if (p == 0) n         ++all_zero; n       else n         ++all_one; n       if ((x + y) % 2 == 1) p ^= 1; n       if (y % 2 == 0) p ^= 1; n       if (p == 0) n         ++row_zero[x]; n       else n         ++row_one[x]; n       if (y % 2 == 0) p ^= 1; n       if (x % 2 == 0) p ^= 1; n       if (p == 0) n         ++col_zero[y]; n       else n         ++col_one[y]; n       if (x % 2 == 0) p ^= 1; n     } n     v_c = cval(y); n     v_r = rval(x); n     if (v_c == 0) ++zero_c; n     if (v_r == 0) ++zero_r; n     if (v_c == 2) mul_c = 2LL * mul_c % mod; n     if (v_r == 2) mul_r = 2LL * mul_r % mod; n     int ans = 0; n     if (zero_r == 0) upd(ans, mul_r); n     if (zero_c == 0) upd(ans, mul_c); n     if (zero_r == 0 && zero_c == 0) { n       if (all_one == 0) --ans; n       if (all_zero == 0) --ans; n       upd(ans, mod); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <algorithm> n #include <cassert> n #include <cmath> n #include <cstdlib> n #include <cstring> n #include <iomanip> n #include <iostream> n #include <limits> n #include <map> n #include <numeric> n #include <queue> n #include <random> n #include <set> n #include <stack> n #include <unordered_map> n #include <unordered_set> n #include <vector> n using namespace std; n  n using LL = long long; n  n int Rev(int a, int k) { n   vector<int> x; n   while (a > 0) { n     x.push_back((k - a % k) % k); n     a /= k; n   } n   reverse(x.begin(), x.end()); n   int ans = 0; n   for (int i : x) ans = ans * k + i; n   return ans; n } n  n int Xor(int a, int b, int k) { n   vector<int> x; n   while (a > 0 || b > 0) { n     x.push_back((a + b) % k); n     a /= k; n     b /= k; n   } n   reverse(x.begin(), x.end()); n   int ans = 0; n   for (int i : x) ans = ans * k + i; n   return ans; n } n  n int main() { n   ios::sync_with_stdio(false); n   int times; n   cin >> times; n   while (times--) { n     int n, k; n     cin >> n >> k; n     vector<int> x {0, Rev(1, k)}; n     for (int i = 2; i < n; i += 2) { n       x.push_back(Xor(i, Rev(i - 1, k), k)); n       x.push_back(Xor(i, Rev(i + 1, k), k)); n     } n     for (int i = 0; i < n; ++i) { n       cout << x[i] << endl; n       int r; n       cin >> r; n       if (r == 1) break; n     } n   } n   return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int MAX_SIZE = 10000; n void solve() { n   int t; n   cin >> t; n   string str[2]; n   map<string, vector<string>> m; n   string tmp; n   for (int i = 0; i < t; i++) { n     cin >> str[0] >> tmp >> str[1]; n     m[str[0]].push_back(str[1]); n   } n   int a, b, c; n   cin >> a >> b >> c; n   int minDiff = -1; n   int ii, jj, kk; n   vector<vector<int>> idxs; n   for (int i = 1; i <= 5; i++) { n     for (int j = 1; j <= 5; j++) { n       for (int k = 1; k <= 5; k++) { n         if (i + j + k == 7) { n           int minNum = a / i; n           int maxNum = a / i; n           maxNum = max(maxNum, b / j); n           minNum = min(minNum, b / j); n           maxNum = max(maxNum, c / k); n           minNum = min(minNum, c / k); n           if (minDiff == -1 || minDiff > maxNum - minNum) { n             idxs.clear(); n             minDiff = maxNum - minNum; n             ii = i; n             jj = j; n             kk = k; n             vector<int> vec; n             vec.push_back(ii); n             vec.push_back(jj); n             vec.push_back(kk); n             idxs.push_back(vec); n           } else if (minDiff == maxNum - minNum) { n             vector<int> vec; n             vec.push_back(i); n             vec.push_back(j); n             vec.push_back(k); n             idxs.push_back(vec); n           } n         } n       } n     } n   } n   vector<string> names2; n   names2.push_back( Troll ); n   names2.push_back( Dracul ); n   names2.push_back( Anka ); n   names2.push_back( Snowy ); n   names2.push_back( Hexadecimal ); n   names2.push_back( Chapay ); n   names2.push_back( Cleo ); n   vector<int> np; n   for (int i = 0; i < 7; i++) np.push_back(i); n   int maxCnt = -1; n   for (int l = 0; l < idxs.size(); l++) { n     ii = idxs[l][0]; n     jj = idxs[l][1]; n     kk = idxs[l][2]; n     do { n       vector<string> cmpstr; n       for (int i = 0; i < ii; i++) cmpstr.push_back(names2[np[i]]); n       int icnt = 0; n       for (int i = 0; i < cmpstr.size(); i++) { n         for (int j = 0; j < cmpstr.size(); j++) { n           if (cmpstr[i] == cmpstr[j]) continue; n           if (m.count(cmpstr[i])) { n             if (m[cmpstr[i]].end() != n                 find(m[cmpstr[i]].begin(), m[cmpstr[i]].end(), cmpstr[j])) { n               icnt++; n             } n           } n         } n       } n       cmpstr.clear(); n       for (int i = ii; i < jj + ii; i++) cmpstr.push_back(names2[np[i]]); n       int jcnt = 0; n       for (int i = 0; i < cmpstr.size(); i++) { n         for (int j = 0; j < cmpstr.size(); j++) { n           if (cmpstr[i] == cmpstr[j]) continue; n           if (m.count(cmpstr[i])) { n             if (m[cmpstr[i]].end() != n                 find(m[cmpstr[i]].begin(), m[cmpstr[i]].end(), cmpstr[j])) { n               jcnt++; n             } n           } n         } n       } n       cmpstr.clear(); n       for (int i = ii + jj; i < 7; i++) cmpstr.push_back(names2[np[i]]); n       int kcnt = 0; n       for (int i = 0; i < cmpstr.size(); i++) { n         for (int j = 0; j < cmpstr.size(); j++) { n           if (cmpstr[i] == cmpstr[j]) continue; n           if (m.count(cmpstr[i])) { n             if (m[cmpstr[i]].end() != n                 find(m[cmpstr[i]].begin(), m[cmpstr[i]].end(), cmpstr[j])) { n               kcnt++; n             } n           } n         } n       } n       maxCnt = max(maxCnt, icnt + jcnt + kcnt); n     } while (next_permutation(np.begin(), np.end())); n   } n   cout << minDiff <<     << maxCnt << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1E-9; n const int INF = 1000000000; n const long long INF64 = (long long)1E18; n const double PI = 3.1415926535897932384626433832795; n long long gcd(long long a, long long b) { n   if (a < b) return gcd(b, a); n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long st(long long b, long long d) { n   long long a, a1 = b, i; n   for (i = 1, a = b; a <= d && d % a == 0; ++i) a1 = a, a *= b; n   return a1; n } n long long type(long long b, long long d, long long& res) { n   long long g = gcd(b, d), i, a; n   if (g > 1) { n     for (i = 1, a = g; a < 100500000000; ++i, a *= g) n       if (a % d == 0) { n         res = i; n         return 2; n       } n   } n   if (b % d == 1) return 3; n   if ((b + 1) % d == 0) return 11; n   return 0; n } n int main() { n   long long i, n, j, a, b, d; n   cin >> b >> d; n   long long res, tp = type(b, d, res); n   if (tp > 0) { n     cout << tp <<  -type  << endl; n     if (tp == 2) cout << res; n     return 0; n   } n   for (i = 2; i <= d; ++i) n     if (d % i == 0) n       if (type(b, st(i, d), res) > 0) { n         d /= st(i, d); n       } else { n       } n   if (d == 1) { n     cout <<  6-type ; n     return 0; n   } n   cout <<  7-type ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1005; n const int maxm = 2005; n struct detail { n   int c[maxm]; n   int len; n   int p; n   detail() { len = 0; } n   void add(int i) { n     c[len] = i; n     ++len; n   } n } details[maxn]; n int order[maxn]; n int n, m; n bool cmp(const int a, const int b) { return details[a].p > details[b].p; } n int main(int argc, const char* argv[]) { n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     order[i] = i; n     cin >> details[i].p; n   } n   for (int i = 0; i < m; ++i) { n     int x = 1, y = 1; n     cin >> x >> y; n     x--; n     y--; n     details[x].add(y); n     details[y].add(x); n   } n   sort(order, order + n, cmp); n   long long a = 0; n   for (int i = 0; i < n; ++i) { n     int o = order[i]; n     for (int j = 0; j < details[o].len; ++j) { n       a += details[details[o].c[j]].p; n     } n     details[o].p = 0; n   } n   cout << a << endl; 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   ; n   long long a, b; n   cin >> a >> b; n   long long c = 1, i = 0; n   while (1) { n     if (i % 2 == 0 && a >= c) n       a -= c; n     else if (i & 1 && b >= c) n       b -= c; n     else n       break; n     c++; n     i++; n   } n   if (i & 1) n     cout <<  Valera n ; n   else n     cout <<  Vladik n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, N[5010], e, s, X[5010], A[5010], B[5010], C[5010], D[5010]; n long long int total, t, selection; n long long int Dis(long long int x, long long int y) { n   return labs(X[x] - X[y]) + (x > y ? C[x] + B[y] : D[x] + A[y]); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> s >> e; n   for (int i = 1; i <= n; i++) cin >> X[i]; n   for (int i = 1; i <= n; i++) cin >> A[i]; n   for (int i = 1; i <= n; i++) cin >> B[i]; n   for (int i = 1; i <= n; i++) cin >> C[i]; n   for (int i = 1; i <= n; i++) cin >> D[i]; n   N[s] = e; n   total = Dis(s, e); n   for (int i = 1; i <= n; i++) { n     if (i == s || i == e) continue; n     long long int l = LLONG_MAX; n     for (int j = s; j != e; j = N[j]) { n       t = Dis(j, i) + Dis(i, N[j]) - Dis(j, N[j]); n       if (t < l) { n         l = t; n         selection = j; n       } n     } n     total += l; n     N[i] = N[selection]; n     N[selection] = i; n   } n   cout << total; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n void write(int x) { n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n const int N = 200005, inf = 1e9; n int a[N], t[N], id[N], fa[N], dep[N], num[N], son[N], q[N], dfn[N]; n int top[N], ans[N], n, m, cnt = 0, fd; n struct tree { n   int mx, lazy; n } tr[N << 2]; n bool cmp(int x, int y) { n   if (t[x] + dep[a[x]] != t[y] + dep[a[y]]) n     return t[x] + dep[a[x]] < t[y] + dep[a[y]]; n   return a[x] < a[y]; n } n inline void pushup(int x) { tr[x].mx = max(tr[x << 1].mx, tr[x << 1 | 1].mx); } n inline void pushdown(int x, int l, int mid, int r) { n   if (tr[x].lazy < -inf) return; n   tr[x << 1].lazy = tr[x << 1 | 1].lazy = tr[x].lazy; n   tr[x << 1].mx = mid * 2 + tr[x].lazy; n   tr[x << 1 | 1].mx = r * 2 + tr[x].lazy; n   tr[x].lazy = -inf - 1; n } n void modify(int now, int l, int r, int x, int y, int z) { n   if (x <= l && r <= y) { n     tr[now].lazy = z; n     tr[now].mx = r * 2 + z; n     return; n   } n   int mid = (l + r) >> 1; n   pushdown(now, l, mid, r); n   if (x <= mid) modify(now << 1, l, mid, x, y, z); n   if (y > mid) modify(now << 1 | 1, mid + 1, r, x, y, z); n   pushup(now); n } n void query(int now, int l, int r, int x, int y, int z) { n   if (tr[now].mx <= z) return; n   if (l == r) { n     fd = q[l]; n     return; n   } n   int mid = (l + r) >> 1; n   pushdown(now, l, mid, r); n   if (fd == -1 && y > mid) query(now << 1 | 1, mid + 1, r, x, y, z); n   if (fd == -1 && x <= mid) query(now << 1, l, mid, x, y, z); n   pushup(now); n } n int work(int x, int t) { n   int y = x; n   while (1) { n     fd = -1; n     query(1, 0, n, dfn[top[y]], dfn[y], t + dfn[y]); n     if (fd >= 0) { n       t += dep[y] - dep[fd]; n       y = fd; n       break; n     } n     if (!y) break; n     t += dep[y] - dep[fa[top[y]]]; n     y = fa[top[y]]; n   } n   int z = t + dep[x] - dep[y]; n   while (1) { n     if (top[x] == top[y]) { n       if (x != y) modify(1, 0, n, dfn[y] + 1, dfn[x], t - dfn[y]); n       break; n     } n     modify(1, 0, n, dfn[top[x]], dfn[x], dep[x] - dep[y] + t - dfn[x]); n     x = fa[top[x]]; n   } n   return z; n } n int main() { n   n = read(); n   m = read(); n   for (int i = 1; i <= n; ++i) { n     fa[i] = read(); n     dep[i] = dep[fa[i]] + 1; n     num[i] = 1; n   } n   for (int i = n; i; i--) { n     if (num[i] > num[son[fa[i]]]) son[fa[i]] = i; n     if (fa[i]) num[fa[i]] += num[i]; n   } n   for (int i = son[0]; i; i = son[i]) q[dfn[i] = ++cnt] = i; n   for (int i = 0; i <= n; ++i) n     if (!dfn[i]) n       for (int j = i; j; j = son[j]) top[q[dfn[j] = ++cnt] = j] = i; n   for (int i = 1; i <= m; ++i) a[i] = read(), t[i] = read(); n   for (int i = 1; i <= m; ++i) id[i] = i; n   sort(id + 1, id + m + 1, cmp); n   tr[1].mx = tr[1].lazy = -inf; n   for (int i = 1; i <= m; ++i) ans[id[i]] = work(a[id[i]], t[id[i]]); n   for (int i = 1; i <= m; ++i) write(ans[i]), putchar(   ); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct obj { n   int x, y, ind; n   obj(int x, int y, int ind) : x(x), y(y), ind(ind) {} n   obj() {} n }; n int main() { n   vector<obj> s; n   int n, x, y; n   scanf( %d , &n); n   for (int i = (1); i <= (n); ++i) { n     scanf( %d%d , &x, &y); n     s.push_back(obj(x, y, i)); n   } n   sort(s.begin(), s.end(), [](obj a, obj b) { n     if (a.x / 1000 == b.x / 1000) { n       if (a.x / 1000 % 2 == 0) n         return a.y < b.y; n       else n         return a.y > b.y; n     } n     return a.x / 1000 < b.x / 1000; n   }); n   for (auto v : s) printf( %d  , v.ind); n   return 0; n } n


#include <bits/stdc++.h> n void err() { std::cout <<   n ; } n template <typename T, typename... Args> n void err(T a, Args... args) { n   std::cout << a <<    ; n   err(args...); n } n void pt() { std::cout <<   n ; } n template <typename T, typename... Args> n void pt(T a, Args... args) { n   std::cout << a <<    ; n   pt(args...); n } n using namespace std; n const int N = 5000 + 5, MOD = 1e9 + 7; n int qpow(long long a, long long b) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % MOD; n     a = a * a % MOD; n     b >>= 1; n   } n   return res; n } n int inv(int x) { return qpow(x, MOD - 2); } n int n, m; n int a[N]; n int f[N], h[N]; n int x[N], y[N]; n int l[N], r[N]; n vector<int> v[N]; n void run() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     ++r[a[i]]; n   } n   for (int i = 1; i <= m; i++) { n     cin >> f[i] >> h[i]; n     v[f[i]].push_back(h[i]); n   } n   for (int i = 1; i <= n; i++) sort((v[i]).begin(), (v[i]).end()); n   pair<int, int> ans(0, 0); n   auto upd = [&](int x, int y) { n     if (x > ans.first) ans = make_pair(x, 0); n     if (x == ans.first) ans.second = (ans.second + y) % MOD; n   }; n   auto calc = [&](int i) { n     int a = upper_bound((v[i]).begin(), (v[i]).end(), l[i]) - v[i].begin(); n     int b = upper_bound((v[i]).begin(), (v[i]).end(), r[i]) - v[i].begin(); n     if (a > b) swap(a, b); n     int cnt1 = 1ll * a * (b - 1) % MOD, cnt2 = a + b; n     if (cnt1 > 0) { n       x[i] = 2; n       y[i] = cnt1; n     } else if (cnt2 > 0) { n       x[i] = 1; n       y[i] = cnt2; n     } else { n       x[i] = 0; n       y[i] = 1; n     } n   }; n   int tx = 0, ty = 1; n   for (int i = 1; i <= n; i++) { n     calc(i); n     tx += x[i]; n     ty = 1ll * ty * y[i] % MOD; n   } n   upd(tx, ty); n   for (int i = 1; i <= n; i++) { n     int now = a[i]; n     tx -= x[now]; n     ty = 1ll * ty * inv(y[now]) % MOD; n     ++l[now], --r[now]; n     if (binary_search((v[now]).begin(), (v[now]).end(), l[now])) { n       int t = upper_bound((v[now]).begin(), (v[now]).end(), r[now]) - n               v[now].begin(); n       if (r[now] >= l[now]) --t; n       if (t > 0) { n         x[now] = 2; n         y[now] = t; n       } else { n         x[now] = y[now] = 1; n       } n       int nx = tx + x[now], ny = 1ll * ty * y[now] % MOD; n       upd(nx, ny); n     } n     calc(now); n     tx += x[now]; n     ty = 1ll * ty * y[now] % MOD; n   } n   cout << ans.first <<     << ans.second <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(20); n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2 * 1e6; n int a[N]; n int main() { n   std::ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     int tmp; n     cin >> tmp; n     a[tmp]++; n   } n   int res = 0; n   for (int i = 0; i < N; ++i) { n     if (a[i] % 2) ++res; n     a[i + 1] += (a[i] / 2); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   int even = 0, odd = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] % 2 == 0) { n       even++; n     } else { n       odd++; n     } n   } n   if (even > odd) { n     cout << odd << endl; n   } else { n     cout << even << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long>> a; n vector<long long> get_res(long long m) { n   vector<long long> res; n   for (long long i = 0; i < a.size(); ++i) { n     long long el = a[i].second; n     long long c = a[i].first; n     long long t = 0; n     while ((c / (t + 1)) >= m) { n       t++; n       res.push_back(el); n     } n   } n   return res; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long n, k; n   cin >> n >> k; n   vector<long long> s(n); n   for (long long i = 0; i < n; ++i) cin >> s[i]; n   vector<long long> cnt(2e5 + 10); n   for (long long i = 0; i < n; ++i) cnt[s[i]]++; n   vector<pair<long long, long long>> v; n   for (long long i = 0; i < cnt.size(); ++i) v.emplace_back(cnt[i], i); n   sort(v.begin(), v.end()); n   reverse(v.begin(), v.end()); n   for (long long i = 0; i < v.size(); ++i) { n     if (v[i].first > 0) { n       a.push_back(v[i]); n     } n   } n   long long l = 1; n   long long r = n + 1; n   while (l + 1 < r) { n     long long m = (l + r) / 2; n     if (get_res(m).size() >= k) n       l = m; n     else n       r = m; n   } n   auto res = get_res(l); n   for (long long i = 0; i < k; ++i) cout << res[i] <<    ; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1}; n int h, w; n string in[1000]; n void put(int y1, int x1, int y2, int x2) { n   bool use[10] = {}; n   for (int d = 0; d < 4; d++) { n     int y = y1 + dy[d], x = x1 + dx[d]; n     if (y < 0 || x < 0 || y >= h || x >= w || !isdigit(in[y][x])) continue; n     use[in[y][x] -  0 ] = 1; n   } n   for (int d = 0; d < 4; d++) { n     int y = y2 + dy[d], x = x2 + dx[d]; n     if (y < 0 || x < 0 || y >= h || x >= w || !isdigit(in[y][x])) continue; n     use[in[y][x] -  0 ] = 1; n   } n   for (int i = 0; i < 10; i++) n     if (!use[(y1 + x1 * 3 + i) % 10]) { n       in[y1][x1] = in[y2][x2] =  0  + (y1 + x1 * 3 + i) % 10; n       break; n     } n } n int get(int y, int x, int c, bool paint) { n   if (paint) n     in[y][x] =  0  + c; n   else n     in[y][x] =  - ; n   for (int d = 0; d < 4; d++) { n     int ny = y + dy[d], nx = x + dx[d]; n     if (ny < 0 || nx < 0 || ny >= h || nx >= w) continue; n     if (!paint && isdigit(in[ny][nx])) c = in[ny][nx] -  0 ; n     if (paint && in[ny][nx] ==  -  || !paint && in[ny][nx] ==  . ) n       get(ny, nx, c, paint); n   } n   return c; n } n void run() { n   cin >> h >> w; n   for (int i = 0; i < h; i++) cin >> in[i]; n   for (int i = 0; i < h; i++) n     for (int j = 0; j < w; j++) { n       if (j < w - 1 && in[i][j] ==  .  && in[i][j + 1] ==  . ) n         put(i, j, i, j + 1); n       if (i < h - 1 && in[i][j] ==  .  && in[i + 1][j] ==  . ) n         put(i, j, i + 1, j); n     } n   for (int i = 0; i < h; i++) n     for (int j = 0; j < w; j++) n       if (in[i][j] ==  . ) { n         int c = get(i, j, -1, 0); n         if (c < 0) { n           cout << -1 << endl; n           return; n         } n         get(i, j, c, 1); n       } n   for (int i = 0; i < h; i++) cout << in[i] << endl; n } n int main() { n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105], b[105], n, k; n bool f[105]; n void dfs(int i) { n   f[i] = true; n   for (int k = 1; k <= n; k++) { n     if (f[k]) continue; n     if (a[i] > a[k] && a[i] < b[k]) n       dfs(k); n     else if (b[i] > a[k] && b[i] < b[k]) n       dfs(k); n   } n } n void setup() { cin >> k; } n void work() { n   while (k--) { n     int x, y, m; n     cin >> m; n     if (m == 1) { n       n++; n       cin >> a[n] >> b[n]; n     } else { n       cin >> x >> y; n       memset(f, 0, sizeof(f)); n       dfs(x); n       if (f[y]) n         cout <<  YES  <<   n ; n       else n         cout <<  NO  <<   n ; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   setup(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O2 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const long double eps = 1e-7; n const int inf = 1000000010; n const long long INF = 10000000000000010LL; n const int mod = 1000000007; n const int MAXN = 100010, LOG = 20; n long long n, m, k, u, v, x, y, t, l, r, ans; n long long S[MAXN], M[MAXN], R[MAXN], D[MAXN]; n int Mn[MAXN << 2], Mx[MAXN << 2], lazy[MAXN << 2]; n vector<pair<int, int> > seg[MAXN << 2]; n vector<long long> ps1[MAXN << 2], ps2[MAXN << 2]; n void Build(int id, int tl, int tr) { n   if (tr - tl == 1) { n     seg[id].push_back({D[tl], tl}); n     ps1[id].push_back(0); n     ps1[id].push_back(R[tl]); n     ps2[id].push_back(M[tl]); n     ps2[id].push_back(0); n     return; n   } n   int mid = (tl + tr) >> 1; n   Build(id << 1, tl, mid); n   Build(id << 1 | 1, mid, tr); n   seg[id].resize(tr - tl); n   ps1[id].resize(tr - tl + 1, 0); n   ps2[id].resize(tr - tl + 1, 0); n   merge(seg[id << 1].begin(), seg[id << 1].end(), seg[id << 1 | 1].begin(), n         seg[id << 1 | 1].end(), seg[id].begin()); n   ps1[id][0] = ps2[id][tr - tl] = 0; n   for (int i = 0; i < tr - tl; i++) n     ps1[id][i + 1] = ps1[id][i] + M[seg[id][i].second]; n   for (int i = tr - tl; i; i--) n     ps2[id][i - 1] = ps2[id][i] + R[seg[id][i - 1].second]; n } n void add_lazy(int id, int val) { lazy[id] = Mn[id] = Mx[id] = val; } n void shift(int id) { n   if (!lazy[id]) return; n   add_lazy(id << 1, lazy[id]); n   add_lazy(id << 1 | 1, lazy[id]); n   lazy[id] = 0; n } n void query(int id, int tl, int tr, int l, int r, int t) { n   if (tr <= l || r <= tl) return; n   if (l <= tl && tr <= r && Mn[id] == Mx[id] && n       (Mn[id] != -1 || tr - tl == 1)) { n     long long time = t - Mn[id]; n     int shit = (Mn[id] == -1); n     add_lazy(id, t); n     if (tr - tl == 1) { n       time -= shit; n       ans += min(M[tl], S[tl] + R[tl] * time); n       S[tl] = 0; n       return; n     } n     int pos = upper_bound(seg[id].begin(), seg[id].end(), n                           pair<int, int>(time, inf + 1)) - n               seg[id].begin(); n     ans += ps1[id][pos]; n     ans += ps2[id][pos] * time; n     return; n   } n   shift(id); n   int mid = (tl + tr) >> 1; n   query(id << 1, tl, mid, l, r, t); n   query(id << 1 | 1, mid, tr, l, r, t); n   Mn[id] = min(Mn[id << 1], Mn[id << 1 | 1]); n   Mx[id] = max(Mx[id << 1], Mx[id << 1 | 1]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   memset(Mn, -1, sizeof(Mn)); n   memset(Mx, -1, sizeof(Mx)); n   cin >> n; n   for (int i = 1; i <= n; i++) n     cin >> S[i] >> M[i] >> R[i], D[i] = (R[i] ? (M[i] + R[i] - 1) / R[i] : inf); n   Build(1, 1, n + 1); n   cin >> m; n   while (m--) { n     cin >> t >> l >> r; n     ans = 0; n     query(1, 1, n + 1, l, r + 1, t); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000404; n const long long MOD = 1000000007ll; n const long double PI = acos(-1.0); n const long double EPS = 1e-9; n template <typename t1, typename t2> n inline void upmax(t1 &a, t2 b) { n   a = max(a, (t1)b); n } n template <typename t1, typename t2> n inline void upmin(t1 &a, t2 b) { n   a = min(a, (t1)b); n } n template <typename T> n inline T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <typename T> n inline T lcm(T a, T b) { n   return a * (b / gcd(a, b)); n } n template <typename T> n inline T sqr(T a) { n   return a * a; n } n template <typename T> n inline bool pal(T &x) { n   int n = (int((x.size()))); n   for (int i = 0; i < n / 2; i++) { n     if (x[i] != x[n - i - 1]) return 0; n   } n   return 1; n } n template <typename T> n inline void rev(T &x) { n   int n = (int((x.size()))); n   for (int i = 0; i < n / 2; i++) swap(x[i], x[n - i - 1]); n } n int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; n int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; n inline long long mp(long long a, long long b) { return (a << 31) + b; } n class compare { n  public: n   bool operator()(const int a, const int b) const { return 1; } n }; n int SQ = 400; n int l[211111]; n int r[211111]; n int m[211111]; n int a[211111]; n void solve() { n   int n; n   vector<int> st; n   cin >> n; n   for (int i = 1; i < n + 1; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i < n + 1; i++) { n     while (!st.empty() && a[st.back()] >= a[i]) st.pop_back(); n     if (st.empty()) n       l[i] = 0; n     else n       l[i] = st.back(); n     st.push_back(i); n   } n   st.resize(0); n   for (int i = n + 1 - 1; i >= 1; i--) { n     while (!st.empty() && a[st.back()] >= a[i]) st.pop_back(); n     if (st.empty()) n       r[i] = n + 1; n     else n       r[i] = st.back(); n     st.push_back(i); n   } n   st.resize(0); n   for (int i = 1; i < n + 1; i++) upmax(m[r[i] - l[i] - 1], a[i]); n   for (int i = n + 1 - 1; i >= 1; i--) upmax(m[i], m[i + 1]); n   for (int i = 1; i < n + 1; i++) cout << m[i] <<    ; n   cout << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   }; n   getchar(); n   getchar(); n   return 0; n } n