#include <bits/stdc++.h> n using namespace std; n using Int = long long; n template <class T1, class T2> n ostream &operator<<(ostream &os, const pair<T1, T2> &a) { n   return os <<  (  << a.first <<  ,   << a.second <<  ) ; n }; n template <class T> n void pv(T a, T b) { n   for (T i = a; i != b; ++i) cerr << *i <<    ; n   cerr << endl; n } n template <class T> n bool chmin(T &t, const T &f) { n   if (t > f) { n     t = f; n     return true; n   } n   return false; n } n template <class T> n bool chmax(T &t, const T &f) { n   if (t < f) { n     t = f; n     return true; n   } n   return false; n } n template <class T, class S, class OpTT, class OpST, class OpSS> n struct SegmentTree { n   const OpTT opTT; n   const OpST opST; n   const OpSS opSS; n   const T idT; n   const S idS; n   int n; n   vector<T> ts; n   vector<S> ss; n   SegmentTree(int n_, const OpTT opTT, const OpST opST, const OpSS opSS, n               const T &idT, const S &idS) n       : opTT(opTT), opST(opST), opSS(opSS), idT(idT), idS(idS) { n     for (n = 1; n < n_; n <<= 1) { n     } n     ts.assign(n << 1, idT); n     ss.assign(n << 1, idS); n   } n   T &at(int a) { return ts[n + a]; } n   void build() { n     for (int a = n; --a;) ts[a] = opTT(ts[a << 1], ts[a << 1 | 1]); n   } n   T query(int a, int b, const S &s) { return query(1, 0, n, a, b, s); } n  n  private: n   T query(int u, int l, int r, int a, int b, const S &s) { n     if (a < l) a = l; n     if (b > r) b = r; n     if (a >= b) return idT; n     if (a == l && b == r) { n       ts[u] = opST(s, ts[u], r - l); n       ss[u] = opSS(s, ss[u]); n       return ts[u]; n     } n     const int uL = u << 1, uR = u << 1 | 1; n     const int mid = (l + r) >> 1; n     { n       ts[uL] = opST(ss[u], ts[uL], mid - l); n       ts[uR] = opST(ss[u], ts[uR], r - mid); n       ss[uL] = opSS(ss[u], ss[uL]); n       ss[uR] = opSS(ss[u], ss[uR]); n       ss[u] = idS; n     } n     const T resL = query(uL, l, mid, a, b, s); n     const T resR = query(uR, mid, r, a, b, s); n     ts[u] = opTT(ts[uL], ts[uR]); n     return opTT(resL, resR); n   } n }; n constexpr int MAX = 50005; n int N, M, Q, C; n int F[MAX], P[MAX]; n int D[MAX]; n int T[MAX], X[MAX], K[MAX]; n double anss[MAX]; n int f[MAX]; n int queriesLen; n pair<pair<int, int>, pair<int, int>> queries[4 * MAX]; n Int vals[MAX]; n void brute() { n   fill(vals, vals + N, 0); n   Int s1 = 0, s2 = 0; n   for (int h = 0; h < queriesLen; ++h) { n     switch (queries[h].second.second) { n       case +1: { n         const int u = queries[h].second.first; n         for (int v = u; v != -1; v = P[v]) { n           ++s1; n           s2 += vals[v]; n         } n         for (int v = u; v != -1; v = P[v]) { n           ++vals[v]; n         } n       } break; n       case -1: { n         const int u = queries[h].second.first; n         for (int v = u; v != -1; v = P[v]) { n           --vals[v]; n         } n         for (int v = u; v != -1; v = P[v]) { n           --s1; n           s2 -= vals[v]; n         } n       } break; n       case 0: { n         const int k = queries[h].first.first; n         double ans = 0.0; n         ans += 1.0 * C * C * N; n         ans -= 2.0 * C * D[k] * s1; n         ans += 1.0 * D[k] * D[k] * (s1 + 2.0 * s2); n         ans /= N; n         anss[queries[h].second.first] = ans; n       } break; n       default: n         assert(false); n     } n   } n   for (int q = 0; q < Q; ++q) { n     if (T[q] == 2) { n       printf( %.10f n , anss[q]); n     } n   } n } n vector<int> G[MAX]; n int par[MAX], sz[MAX], dep[MAX]; n int O, ord[MAX], head[MAX]; n void dfs0(int u, int p) { n   par[u] = p; n   sz[u] = 1; n   dep[u] = (p == -1) ? 0 : (dep[p] + 1); n   for (const int v : G[u]) { n     if (v != p) { n       dfs0(v, u); n       sz[u] += sz[v]; n     } n   } n } n void dfs1(int u, int p, int h) { n   ord[u] = O++; n   head[u] = h; n   int vm = -1; n   for (const int v : G[u]) { n     if (v != p) { n       if (vm == -1 || sz[vm] < sz[v]) { n         vm = v; n       } n     } n   } n   if (vm != -1) { n     dfs1(vm, u, h); n     for (const int v : G[u]) { n       if (v != p && v != vm) { n         dfs1(v, u, v); n       } n     } n   } n } n void solve() { n   for (int u = 0; u < N; ++u) { n     G[u].clear(); n   } n   for (int u = 1; u < N; ++u) { n     G[P[u]].push_back(u); n   } n   const int rt = 0; n   dfs0(rt, -1); n   O = 0; n   dfs1(rt, -1, 0); n   auto opTT = [](Int t0, Int t1) { return t0 + t1; }; n   auto opST = [](Int s, Int t, Int sz) { return t + s * sz; }; n   auto opSS = [](Int s0, Int s1) { return s0 + s1; }; n   SegmentTree<Int, Int, decltype(opTT), decltype(opST), decltype(opSS)> seg( n       N, opTT, opST, opSS, 0, 0); n   auto query = [&](int u, int val) { n     Int ret = 0; n     for (; u != -1;) { n       const Int h = head[u]; n       ret += seg.query(ord[h], ord[u] + 1, val); n       u = par[h]; n     } n     return ret; n   }; n   Int s1 = 0, s2 = 0; n   for (int h = 0; h < queriesLen; ++h) { n     switch (queries[h].second.second) { n       case +1: { n         const int u = queries[h].second.first; n         s1 += (dep[u] + 1); n         s2 += query(u, 0); n         query(u, +1); n       } break; n       case -1: { n         const int u = queries[h].second.first; n         s1 -= (dep[u] + 1); n         s2 -= query(u, -1); n       } break; n       case 0: { n         const int k = queries[h].first.first; n         double ans = 0.0; n         ans += 1.0 * C * C * N; n         ans -= 2.0 * C * D[k] * s1; n         ans += 1.0 * D[k] * D[k] * (s1 + 2.0 * s2); n         ans /= N; n         anss[queries[h].second.first] = ans; n       } break; n       default: n         assert(false); n     } n   } n   for (int q = 0; q < Q; ++q) { n     if (T[q] == 2) { n       printf( %.10f n , anss[q]); n     } n   } n } n int main() { n   for (; ~scanf( %d%d%d%d , &N, &M, &Q, &C);) { n     for (int u = 0; u < N; ++u) { n       scanf( %d , &F[u]); n       --F[u]; n     } n     P[0] = -1; n     for (int u = 1; u < N; ++u) { n       scanf( %d , &P[u]); n       --P[u]; n     } n     for (int k = 0; k < M; ++k) { n       scanf( %d , &D[k]); n     } n     for (int q = 0; q < Q; ++q) { n       scanf( %d , &T[q]); n       if (T[q] == 1) { n         scanf( %d , &X[q]); n         --X[q]; n       } n       scanf( %d , &K[q]); n       --K[q]; n     } n     copy(F, F + N, f); n     queriesLen = 0; n     for (int u = 0; u < N; ++u) { n       queries[queriesLen++] = {{f[u], -1}, {u, +1}}; n     } n     for (int q = 0; q < Q; ++q) { n       if (T[q] == 1) { n         const int u = X[q]; n         queries[queriesLen++] = {{f[u], q}, {u, -1}}; n         f[u] = K[q]; n         queries[queriesLen++] = {{f[u], q}, {u, +1}}; n       } else { n         queries[queriesLen++] = {{K[q], q}, {q, 0}}; n       } n     } n     for (int u = 0; u < N; ++u) { n       queries[queriesLen++] = {{f[u], Q}, {u, -1}}; n     } n     sort(queries, queries + queriesLen); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1005, maxd = 15; n int f[maxn], g[maxn][maxd][maxn], rfac[maxn], n, d, mo; n int pow(int a, int b) { n   int tmp = 1; n   for (; b; b >>= 1, a = a * 1ll * a % mo) n     if (b & 1) tmp = tmp * 1ll * a % mo; n   return tmp; n } n int main() { n   cin >> n >> d >> mo; n   if (n == 1) { n     cout << 1 << endl; n     return 0; n   } n   rfac[0] = 1; n   for (int i = 1; i <= 1000; i++) n     rfac[i] = rfac[i - 1] * 1ll * pow(i, mo - 2) % mo; n   for (int i = 0; i <= n; i++) g[0][0][i] = 1; n   for (int i = 1; i <= n; i++) { n     if (i != 1) n       f[i] = g[i - 1][d - 1][n]; n     else n       f[i] = 1; n     for (int dd = 1; dd <= d; dd++) { n       for (int x = 1; x <= i; x++) { n         int v = f[x]; n         for (int k = 1, cv = 1; k <= dd && x * k <= i; k++) { n           cv = cv * 1ll * (v + k - 1) % mo; n           g[i][dd][x] = (g[i][dd][x] + g[i - x * k][dd - k][x - 1] * 1ll * cv % n                                            mo * rfac[k] % mo) % n                         mo; n         } n       } n       for (int x = 1; x <= n; x++) n         g[i][dd][x] = (g[i][dd][x] + g[i][dd][x - 1]) % mo; n     } n   } n   int ans = g[n - 1][d][(n - 1) / 2]; n   if (!(n & 1)) { n     int v = f[n / 2]; n     ans = (ans + v * 1ll * (v + 1) % mo * rfac[2] % mo) % mo; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1000 * 100 + 1; n const long long INF = 1e12; n const int MOD = 1000 * 1000 * 1000 + 7; n const int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; n const int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; n const int MAXN = 2 * 1000 * 100 + 1; n struct edge { n   int to, w; n }; n int n, m; n vector<edge> g[MAXN]; n long long dist[MAXN]; n int par[MAXN]; n set<pair<long long, int> > s; n vector<int> ans; n edge e; n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int a, b, w; n     scanf( %d %d %d , &a, &b, &w); n     e.to = b; n     e.w = w; n     g[a].push_back(e); n     e.to = a; n     g[b].push_back(e); n   } n   s.insert(make_pair(0, 1)); n   for (int i = 2; i <= n; i++) { n     dist[i] = INF; n     s.insert(make_pair(INF, i)); n   } n   while (!s.empty()) { n     int cur = s.begin()->second; n     long long cur_dist = s.begin()->first; n     s.erase(s.begin()); n     for (int i = 0; i < (int)g[cur].size(); i++) { n       int to = g[cur][i].to, w = g[cur][i].w; n       if (cur_dist + w < dist[to]) { n         s.erase(make_pair(dist[to], to)); n         dist[to] = cur_dist + w; n         par[to] = cur; n         s.insert(make_pair(dist[to], to)); n       } n     } n   } n   if (dist[n] == INF) { n     printf( -1 ); n     return 0; n   } n   int cur = n; n   while (par[cur] != 0) { n     ans.push_back(cur); n     cur = par[cur]; n   } n   ans.push_back(1); n   reverse(ans.begin(), ans.end()); n   for (int i = 0; i < (int)ans.size(); i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int T, n, m, u, v, lab[300001]; n vector<int> g[300001]; n long long cnt[3]; n long long const mod = 998244353; n bool impossible; n void DFS(const int& u) { n   for (const int& v : g[u]) n     if (!lab[v]) { n       lab[v] = 3 - lab[u]; n       cnt[lab[v]]++; n       DFS(v); n       if (impossible) return; n     } else if (lab[v] == lab[u]) { n       impossible = true; n       return; n     } n } n long long Pow(const long long& k) { n   if (k == 0) return 1; n   long long tmp = Pow(k / 2); n   if (k % 2 == 0) n     return tmp * tmp % mod; n   else n     return 2 * tmp * tmp % mod; n } n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &n, &m); n     for (int i = 1; i <= n; i++) { n       g[i].clear(); n       lab[i] = 0; n     } n     while (m--) { n       scanf( %d%d , &u, &v); n       g[u].push_back(v); n       g[v].push_back(u); n     } n     long long res = 1; n     impossible = false; n     for (int i = 1; i <= n && !impossible; i++) n       if (!lab[i]) { n         lab[i] = 1; n         cnt[1] = 1; n         cnt[2] = 0; n         DFS(i); n         res = res * ((Pow(cnt[1]) + Pow(cnt[2])) % mod) % mod; n       } n     if (impossible) n       cout << 0 <<   n ; n     else n       cout << res <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n template <typename T, typename U> n inline void amin(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n inline void amax(T &x, U y) { n   if (x < y) x = y; n } n template <typename T> n inline void write(T x) { n   int i = 20; n   char buf[21]; n   buf[20] =   n ; n   do { n     buf[--i] = x % 10 +  0 ; n     x /= 10; n   } while (x); n   do putchar(buf[i]); n   while (buf[i++] !=   n ); n } n template <typename T> n inline T readInt() { n   T n = 0, s = 1; n   char p = getchar(); n   if (p ==  - ) s = -1; n   while ((p <  0  || p >  9 ) && p != EOF && p !=  - ) p = getchar(); n   if (p ==  - ) s = -1, p = getchar(); n   while (p >=  0  && p <=  9 ) { n     n = (n << 3) + (n << 1) + (p -  0 ); n     p = getchar(); n   } n   return n * s; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int n, k, *h, smin, c; n   n = readInt<long long int>(); n   h = (long long int *)malloc(n * sizeof(long long int)); n   k = readInt<long long int>(); n   for (int i = 0; i < n; i += 1) h[i] = readInt<long long int>(); n   sort(h, h + n); n   reverse(h, h + n); n   if (h[0] == h[n - 1]) { n     write(0); n     return 0; n   } n   smin = 1; n   c = 0; n   for (int i = 0; i < n - 1; i += 1) { n     for (int j = h[i]; j >= h[i + 1] + 1; j -= 1) { n       if (c + i + 1 > k) { n         smin++; n         c = i + 1; n       } else n         c += i + 1; n     } n   } n   write(smin); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, a, b; n string s, g; n map<string, pair<int, int> > m; n int main() { n   m[ goat ] = make_pair(1, 1); n   m[ asiago ] = make_pair(1, 2); n   m[ camembert ] = make_pair(1, 3); n   m[ gouda ] = make_pair(2, 1); n   m[ feta ] = make_pair(2, 2); n   m[ swiss ] = make_pair(2, 3); n   m[ muenster ] = make_pair(3, 1); n   m[ cheddar ] = make_pair(3, 2); n   m[ brie ] = make_pair(3, 3); n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> s >> g; n     if (g ==  soft ) { n       a++; n     } else { n       b++; n     } n   } n   int res = 1; n   while ((res * res) / 2 + (res * res) % 2 < max(a, b) || res * res < a + b) { n     res++; n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 5; n int n, m, p; n const double PI = acos(-1); n vector<int> a, b; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   cin >> n >> m >> p; n   for (int i = (int)1; i <= (int)n; i++) { n     int x; n     cin >> x; n     a.push_back(x); n   } n   for (int i = (int)1; i <= (int)m; i++) { n     int x; n     cin >> x; n     b.push_back(x); n   } n   int i = 0, j = 0; n   while (i < n) { n     if (a[i] % p != 0) break; n     i++; n   } n   while (j < m) { n     if (b[j] % p != 0) break; n     j++; n   } n   cout << i + j << endl; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:400000000 ) n int n, a[200010], b[200010], f[200010]; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     f[x - 1] = i; n   } n   for (int i = 0; i < n; i++) b[i] = f[a[i] - 1]; n   for (int i = 1; i < n; i++) { n     if (b[i] < b[i - 1]) { n       cout << n - i; n       return 0; n     } n   } n   cout << 0; n } n


#include <bits/stdc++.h> n using namespace std; n int memo[77][107][107]; n inline int index(char ch) { return (ch ==  @ ) ? 0 : ch -  a ; } n bool eval(char ch, int i, int j, vector<vector<pair<int, char>>> &adj) { n   int val = memo[index(ch)][i][j]; n   if (val == 1) return true; n   if (val == 2) return false; n   for (auto nxt : adj[i]) { n     if (ch <= nxt.second) { n       if (!eval(nxt.second, j, nxt.first, adj)) { n         memo[index(ch)][i][j] = 1; n         return true; n       } n     } n   } n   memo[index(ch)][i][j] = 2; n   return false; n } n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   int n, m; n   cin >> n >> m; n   vector<vector<pair<int, char>>> adj(n + 1); n   for (int i = 0; i < m; i++) { n     int u, v; n     char c; n     cin >> u >> v >> c; n     adj[u].push_back({v, c}); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) cout << (eval( @ , i, j, adj) ?  A  :  B ); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 126, MOD = 12345; n int res[maxn][maxn], tmp[maxn][maxn], POW[maxn][maxn], flag[maxn], check[maxn]; n int A[maxn][maxn], B[maxn][maxn], a[maxn << 3], b[maxn << 3]; n int m, size = 1, temp, ans, vec[maxn << 3], judge; n char ch; n long long n; n void mul(int a[maxn][maxn], int b[maxn][maxn], int c[maxn][maxn]) { n   for (int i = 1; i <= size; i++) n     for (int j = 1; j <= size; j++) { n       tmp[i][j] = 0; n       for (int k = 1; k <= size; k++) n         tmp[i][j] = (tmp[i][j] + a[i][k] * b[k][j]) % MOD; n     } n   for (int i = 1; i <= size; i++) n     for (int j = 1; j <= size; j++) c[i][j] = tmp[i][j]; n } n void power(int a[maxn][maxn], long long p, int b[maxn][maxn]) { n   for (int i = 1; i <= size; i++) n     for (int j = 1; j <= size; j++) POW[i][j] = a[i][j], res[i][j] = (i == j); n   while (p) { n     if (p & 1) mul(res, POW, res); n     mul(POW, POW, POW); n     p >>= 1; n   } n   for (int i = 1; i <= size; i++) n     for (int j = 1; j <= size; j++) b[i][j] = res[i][j]; n } n int main() { n   scanf( %lld%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf(  n%c %d , &ch, &b[i]); n     a[i] = ch -  A ; n     flag[a[i]] = 1; n     size *= b[i]; n   } n   for (int i = 0; i <= size - 1; i++) { n     temp = i; n     for (int j = m; j >= 1; j--) vec[j] = temp % b[j], temp /= b[j]; n     for (int j = 0; j <= 25; j++) n       if (flag[j]) { n         temp = 0; n         for (int k = 1; k <= m; k++) n           temp = temp * b[k] + (vec[k] + (a[k] == j)) % b[k]; n         A[temp + 1][i + 1]++; n       } n   } n   power(A, n, A); n   B[1][1] = 1; n   mul(A, B, B); n   for (int i = 0; i <= size - 1; i++) { n     temp = i; n     for (int j = m; j >= 1; j--) check[a[j]] |= !(temp % b[j]), temp /= b[j]; n     judge = 1; n     for (int j = 0; j <= 25; j++) { n       judge &= !(flag[j] ^ check[j]); n       check[j] = 0; n     } n     if (judge) ans = (ans + B[i + 1][1]) % MOD; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100005]; n char b[100005]; n int main() { n   cin >> s; n   int cnt = 0; n   char x =  a ; n   for (int i = strlen(s); i >= 0; --i) { n     if (s[i] >= x) { n       x = s[i]; n       b[cnt++] = x; n     } n   } n   while (cnt--) { n     printf( %c , b[cnt]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   vector<string> nap(n); n   for (int i = 0; i < n; ++i) cin >> nap[i]; n   vector<vector<int> > dp(26); n   for (int i = 0; i < 26; ++i) n     for (int j = 0; j < 26; ++j) dp[i].push_back(0); n   for (int i = 0; i < n; ++i) { n     int dl = nap[i].size(); n     for (char c =  a ; c <=  z ; ++c) { n       if (dp[c -  a ][nap[i][0] -  a ] != 0) { n         dp[c -  a ][nap[i][dl - 1] -  a ] = n             max(dp[c -  a ][nap[i][dl - 1] -  a ], n                 dp[c -  a ][nap[i][0] -  a ] + dl); n       } n     } n     dp[nap[i][0] -  a ][nap[i][dl - 1] -  a ] = n         max(dp[nap[i][0] -  a ][nap[i][dl - 1] -  a ], dl); n   } n   int odp = 0; n   for (int i = 0; i < 26; ++i) odp = max(odp, dp[i][i]); n   cout << odp; n   return 0; n } n


#include <bits/stdc++.h> n using ld = long double; n using ul = std::size_t; n template <typename T, ul C> n using ar = std::array<T, C>; n const int MN = 55; n const ld INF = 1e10; n const ld EPS = 1e-9; n template <ul C> n ar<ld, C>& operator*=(ar<ld, C>& a, const ld& v) { n   for (ul i = 0; i < C; ++i) a[i] *= v; n   return a; n } n template <ul C> n ar<ld, C> operator*(ar<ld, C> a, const ld& v) { n   return a *= v; n } n template <ul C> n ar<ld, C>& operator+=(ar<ld, C>& a, const ar<ld, C>& b) { n   for (ul i = 0; i < C; ++i) a[i] += b[i]; n   return a; n } n template <ul C> n ar<ld, C> operator+(ar<ld, C> a, const ar<ld, C>& b) { n   return a += b; n } n inline bool equ(const ld& a, const ld& b) { return std::abs(a - b) < EPS; } n struct vec { n  public: n   ld x, y; n   vec(ld _x = 0, ld _y = 0) : x(_x), y(_y) {} n   vec& operator+=(const vec& o) { return x += o.x, y += o.y, *this; } n   vec& operator-=(const vec& o) { return x -= o.x, y -= o.y, *this; } n   vec& operator*=(const ld& o) { return x *= o, y *= o, *this; } n   friend vec operator+(vec a, const vec& b) { return a += b; } n   friend vec operator-(vec a, const vec& b) { return a -= b; } n   friend vec operator*(vec a, const ld& b) { return a *= b; } n   ld mag() const { return hypot(x, y); } n }; n struct range { n  public: n   ld l, r; n }; n using ranges = std::vector<range>; n ranges unite(const ranges& a, const ranges& b) { n   ranges f(0); n   bool rdy = 0; n   ld l = -INF - 1, r = -INF - 1; n   auto add = [&]() { n     if (rdy) n       f.push_back({l, r}); n     else n       rdy = 1; n   }; n   range t; n   for (int i = 0, j = 0; i < a.size() || j < b.size();) { n     if (j < b.size() && (i == a.size() || b[j].l < a[i].l)) n       t = b[j++]; n     else n       t = a[i++]; n     if (r + EPS < t.l) add(), l = t.l; n     r = std::max(r, t.r); n   } n   add(); n   return f; n } n ranges isect(const ranges& a, const ranges& b) { n   ranges f; n   for (int i = 0, j = 0; i < a.size() && j < b.size();) { n     ld l = std::max(a[i].l, b[j].l); n     ld r = std::min(a[i].r, b[j].r); n     if (l + EPS < r) f.push_back({l, r}); n     if (a[i].r < b[j].r) n       ++i; n     else n       ++j; n   } n   return f; n } n int N; n vec p[MN]; n ld pf[MN]; n ar<ld, 6> sqr(const ar<ld, 3>& a) { n   return {a[0] * a[0], 2 * a[0] * a[1], 2 * a[0] * a[2], n           a[1] * a[1], a[2] * a[2],     2 * a[1] * a[2]}; n } n ranges neg(const ar<ld, 3>& a) { n   if (equ(a[2], 0)) { n     if (equ(a[1], 0)) n       return a[0] < 0 ? ranges{{-INF, INF}} : ranges(); n     else n       return a[1] < 0 ? ranges{{-a[0] / a[1], INF}} n                       : ranges{{-INF, -a[0] / a[1]}}; n   } else if (a[2] < 0) { n     ld d = a[1] * a[1] - 4 * a[0] * a[2]; n     if (d > EPS) { n       d = sqrt(d) / (2 * -a[2]); n       ld c = -a[1] / (2 * a[2]); n       return ranges{{-INF, c - d}, {c + d, INF}}; n     } n     return ranges{{-INF, INF}}; n   } else { n     ld d = a[1] * a[1] - 4 * a[0] * a[2]; n     if (d > EPS) { n       d = sqrt(d) / (2 * a[2]); n       ld c = -a[1] / (2 * a[2]); n       return ranges{{c - d, c + d}}; n     } n     return ranges(); n   } n } n ranges eval(const ar<ld, 6>& a) { n   ranges f; n   f = neg({a[0], a[1], a[3]}); n   f = unite(f, neg({a[0] + a[2] + a[4], a[1] + a[5], a[3]})); n   if (a[4] > EPS) { n     if (equ(a[5], 0)) { n       ld yv = a[2] / (-2 * a[4]); n       if (0 < yv && yv < 1) n         f = unite(f, neg({a[0] + a[2] * yv + a[4] * yv * yv, a[1], a[3]})); n     } else { n       range xr = {(-2 * a[4] - a[2]) / a[5], -a[2] / a[5]}; n       if (a[5] < 0) xr = {xr.r, xr.l}; n       f = unite(f, isect(ranges{xr}, neg({a[0] - a[2] * a[2] / (4 * a[4]), n                                           a[1] - 2 * a[2] * a[5] / (4 * a[4]), n                                           a[3] - a[5] * a[5] / (4 * a[4])}))); n     } n   } n   return isect(f, ranges{{0, 1}}); n } n bool ok(const ld& v) { n   ranges bot, top; n   for (int i = 0; i < N; ++i) n     for (int j = i + 1; j < N; ++j) { n       ld mi = (p[i + 1] - p[i]).mag(); n       ld mj = (p[j + 1] - p[j]).mag(); n       ar<ld, 6> L = sqr({pf[j] - pf[i], -mi, mj}); n       ar<ld, 6> Dx = n           sqr({p[j].x - p[i].x, -p[i + 1].x + p[i].x, p[j + 1].x - p[j].x}); n       ar<ld, 6> Dy = n           sqr({p[j].y - p[i].y, -p[i + 1].y + p[i].y, p[j + 1].y - p[j].y}); n       L *= -1; n       Dx *= v * v; n       Dy *= v * v; n       ar<ld, 6> fin = L + Dx + Dy; n       ranges bot_ = eval(fin); n       std::swap(fin[1], fin[2]); n       std::swap(fin[3], fin[4]); n       ranges top_ = eval(fin); n       for (range& x : bot_) x.l = pf[i] + mi * x.l, x.r = pf[i] + mi * x.r; n       for (range& x : top_) x.l = pf[j] + mj * x.l, x.r = pf[j] + mj * x.r; n       bot = unite(bot, bot_); n       top = unite(top, top_); n     } n   return isect(bot, top).empty(); n } n int main() { n   scanf( %d , &N); n   for (int i = 0; i <= N; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     p[i] = {(ld)u, (ld)v}; n   } n   for (int i = 0; i < N; ++i) pf[i + 1] = pf[i] + (p[i + 1] - p[i]).mag(); n   ld l = 1, r = 5000; n   for (int i = 0; i < 100; ++i) { n     ld m = l + (r - l) / 2; n     if (ok(m)) n       r = m; n     else n       l = m; n   } n   printf( %.9lf n , (double)r); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, dd, hh, mm, t; n   int sep[16]; n   char buf[80]; n   vector<string> todo; n   map<string, int> cost; n   vector<pair<int, int> > time; n   vector<vector<pair<int, bool> > > dp; n   vector<pair<pair<int, int>, pair<int, int> > > job; n   scanf( %d%d%d , &n, &m, &dd); n   for (int i = 0; i < n; ++i) { n     scanf( %s , buf); n     todo.push_back(buf); n   } n   for (int i = 0; i < n; ++i) { n     scanf( %d , &cost[todo[i]]); n   } n   for (int i = 0; i < 8; ++i) { n     scanf( %*[^0-9]%d:%d , &hh, &mm); n     sep[i] = hh * 60 + mm; n   } n   for (int i = 1, k = 0; i <= dd; ++i) { n     for (int j = 0; j < 24 * 60; ++j) { n       if (k % 2 == 0 && j == sep[k]) { n         k = (k + 1) % 8; n       } n       if (k % 2 == 0) { n         time.push_back(make_pair(i, j)); n       } n       if (k % 2 != 0 && j == sep[k]) { n         k = (k + 1) % 8; n       } n     } n   } n   for (int i = 0; i < m; ++i) { n     scanf( %s%d%d:%d%d , buf, &dd, &hh, &mm, &t); n     if (cost.count(buf) == 0) { n       continue; n     } n     job.push_back(make_pair(make_pair(lower_bound(time.begin(), time.end(), n                                                   make_pair(dd, hh * 60 + mm)) - n                                           time.begin(), n                                       i), n                             make_pair(cost[buf], t))); n   } n   sort(job.begin(), job.end()); n   reverse(job.begin(), job.end()); n   n = time.size(); n   m = job.size(); n   dp = vector<vector<pair<int, bool> > >(1, vector<pair<int, bool> >(n + 1)); n   for (int i = 0; i < m; ++i) { n     t = job[i].second.first; n     dp.push_back(dp.back()); n     for (int j = 0; j <= n; ++j) { n       dp[i + 1][j].second = false; n     } n     for (int j = t; j <= job[i].first.first; ++j) { n       if (dp[i + 1][j - t].first < dp[i][j].first + job[i].second.second) { n         dp[i + 1][j - t].first = dp[i][j].first + job[i].second.second; n         dp[i + 1][j - t].second = true; n       } n     } n   } n   printf( %d n , dp[m][0].first); n   todo.clear(); n   for (int i = m - 1, j = 0; i >= 0; --i) { n     if (dp[i + 1][j].second) { n       t = j + job[i].second.first - 1; n       sprintf(buf,  %d %d %02d:%02d %d %02d:%02d , job[i].first.second + 1, n               time[j].first, time[j].second / 60, time[j].second % 60, n               time[t].first, time[t].second / 60, time[t].second % 60); n       todo.push_back(buf); n       j = t + 1; n     } n   } n   printf( %d n , todo.size()); n   for (int i = 0; i < (int)todo.size(); ++i) { n     puts(todo[i].c_str()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool r[100010], c[100010]; n int main() { n   ios_base::sync_with_stdio(false); n   long long n, m, a, b, li, co; n   cin >> n >> m; n   li = co = 0; n   long long ans; n   for (int i = 0; i < m; i++) { n     cin >> a >> b; n     if (r[a] == false) { n       r[a] = true; n       li++; n     } n     if (c[b] == false) { n       c[b] = true; n       co++; n     } n     ans = n * n - co * n; n     ans = ans - li * (n - co); n     if (i > 0) cout <<    ; n     cout << ans; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> Ts; n string size[101] = { S ,  M ,  L ,  XL ,  XXL }; n inline int to_size(string s) { n   for (int i = 0; i < 5; ++i) { n     if (s == size[i]) return i; n   } n } n int main() { n   int a[11], k; n   string pe[10010]; n   for (int i = 0; i < 5; ++i) cin >> a[i]; n   for (int i = 0; i < 5; ++i) { n     Ts[size[i]] = a[i]; n   } n   cin >> k; n   for (int i = 0; i < k; ++i) cin >> pe[i]; n   for (int i = 0; i < k; ++i) { n     int pe1 = to_size(pe[i]); n     for (int j = 0; j <= 4; ++j) { n       if (pe1 + j <= 4) { n         if (Ts[size[pe1 + j]] > 0) { n           Ts[size[pe1 + j]]--; n           cout << size[pe1 + j] << endl; n           break; n         } n       } n       if (pe1 - j >= 0) { n         if (Ts[size[pe1 - j]] > 0) { n           Ts[size[pe1 - j]]--; n           cout << size[pe1 - j] << endl; n           break; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int maxn = 5010; n vector<int> G[maxn]; n int d[maxn][maxn][2]; n void check(int &a, int b) { n   if (a > b) a = b; n } n int dfs(int u, int fa) { n   int num = G[u].size(), tot = 0, now = 0; n   d[u][0][0] = 0; n   d[u][0][1] = 0; n   if (num == 1) { n     d[u][0][1] = INF; n     d[u][1][1] = 0; n     return 1; n   } n   for (int i = 0; i < num; ++i) { n     int v = G[u][i]; n     if (v == fa) continue; n     now = dfs(v, u); n     tot += now; n     for (int j = tot; j >= 0; --j) { n       int mn0, mn1; n       mn0 = mn1 = INF; n       for (int k = 0; k <= now; ++k) { n         check(mn0, min(d[u][j - k][0] + d[v][k][0], n                        d[u][j - k][0] + d[v][k][1] + 1)); n         check(mn1, min(d[u][j - k][1] + d[v][k][0] + 1, n                        d[u][j - k][1] + d[v][k][1])); n       } n       d[u][j][0] = mn0; n       d[u][j][1] = mn1; n     } n   } n   return tot; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n - 1; ++i) { n     int u, v; n     scanf( %d %d , &u, &v); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   if (n == 2) { n     puts( 1 ); n     return 0; n   } n   int cnt = 0, p = 1; n   memset(d, INF, sizeof d); n   for (int i = 1; i <= n; ++i) { n     if (G[i].size() == 1) n       ++cnt; n     else n       p = i; n   } n   dfs(p, 0); n   int ans = min(d[p][cnt / 2][0], d[p][cnt / 2][1]); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int inf = (1ll << 30) - 1; n const int maxn = (int)1e5 + 10; n const int mod = (int)1e9 + 7; n using namespace std; n void solve() { n   string second; n   cin >> second; n   sort(second.begin(), second.end()); n   int cnt = 0; n   vector<string> v; n   string x =   ; n   x += second[0]; n   for (int i = 1; i < second.size(); i++) { n     if (second[i] != second[i - 1]) { n       v.push_back(x); n       x =   ; n     } n     x += second[i]; n   } n   v.push_back(x); n   if (v.size() == 1) { n     cout << second <<   n ; n     return; n   } n   string A =   ; n   if (v.size() > 3) { n     A = v[2] + v[0] + v[3] + v[1]; n     for (int i = 4; i < v.size(); i++) { n       if (i % 2 == 0) n         A += v[i]; n       else n         A = v[i] + A; n     } n     cout << A <<   n ; n     return; n   } n   if (v.size() == 2) { n     if (v[1][0] - v[0][0] == 1) { n       cout <<  No answer n ; n       return; n     } n     cout << second <<   n ; n     return; n   } n   if (v[1][0] - v[0][0] == 1 && v[2][0] - v[1][0] == 1) { n     cout <<  No answer n ; n     return; n   } n   if (v[1][0] - v[0][0] != 1 && v[2][0] - v[1][0] != 1) { n     cout << second <<   n ; n     return; n   } n   if (v[1][0] - v[0][0] == 1) n     cout << v[0] << v[2] << v[1] <<   n ; n   else n     cout << v[1] << v[0] << v[2] <<   n ; n } n int main() { n   int t = 1; n   scanf( %d , &t); n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct flow_graph { n   int V, E, s, t; n   int *flow, *low, *cap, *to, *next, *last, *delta; n   int *dist, *q, *now, head, tail; n   flow_graph() {} n   flow_graph(int V, int E) { n     (*this).V = V; n     (*this).E = 0; n     int TE = 2 * (E + V + 1); n     flow = new int[TE]; n     low = new int[TE]; n     cap = new int[TE]; n     to = new int[TE]; n     next = new int[TE]; n     last = new int[V + 2]; n     delta = new int[V]; n     dist = new int[V + 2]; n     q = new int[V + 2]; n     now = new int[V + 2]; n   } n   void clear(int V) { n     (*this).V = V; n     (*this).E = 0; n     fill(last, last + V, -1); n   } n   void add_edge(int a, int b, int l, int u) { n     to[E] = b; n     low[E] = l; n     cap[E] = u; n     flow[E] = 0; n     next[E] = last[a]; n     last[a] = E++; n     to[E] = a; n     low[E] = 0; n     cap[E] = 0; n     flow[E] = 0; n     next[E] = last[b]; n     last[b] = E++; n   } n   bool bfs() { n     fill(dist, dist + V + 2, -1); n     head = tail = 0; n     q[tail] = t; n     ++tail; n     dist[t] = 0; n     while (head < tail) { n       int v = q[head]; n       ++head; n       for (int e = last[v]; e != -1; e = next[e]) { n         if (cap[e ^ 1] > flow[e ^ 1] && dist[to[e]] == -1) { n           q[tail] = to[e]; n           ++tail; n           dist[to[e]] = dist[v] + 1; n         } n       } n     } n     return dist[s] != -1; n   } n   int dfs(int v, int f) { n     if (v == t) return f; n     for (int &e = now[v]; e != -1; e = next[e]) { n       if (cap[e] > flow[e] && dist[to[e]] == dist[v] - 1) { n         int ret = dfs(to[e], min(f, cap[e] - flow[e])); n         if (ret > 0) { n           flow[e] += ret; n           flow[e ^ 1] -= ret; n           return ret; n         } n       } n     } n     return 0; n   } n   int max_flow(int source, int sink) { n     fill(delta, delta + V, 0); n     for (int e = 0; e < E; e += 2) { n       delta[to[e ^ 1]] -= low[e]; n       delta[to[e]] += low[e]; n       cap[e] -= low[e]; n     } n     last[V] = last[V + 1] = -1; n     int sum = 0; n     for (int i = 0; i < V; ++i) { n       if (delta[i] > 0) { n         add_edge(V, i, 0, delta[i]); n         sum += delta[i]; n       } n       if (delta[i] < 0) add_edge(i, V + 1, 0, -delta[i]); n     } n     add_edge(sink, source, 0, INT_MAX); n     s = V; n     t = V + 1; n     int f = 0, df; n     fill(flow, flow + E, 0); n     while (bfs()) { n       for (int i = V + 1; i >= 0; --i) now[i] = last[i]; n       while (true) { n         df = dfs(s, INT_MAX); n         if (df == 0) break; n         f += df; n       } n     } n     if (f != sum) return -1; n     for (int e = 0; e < E; e += 2) { n       cap[e] += low[e]; n       flow[e] += low[e]; n       flow[e ^ 1] -= low[e]; n       cap[e ^ 1] -= low[e]; n     } n     s = source; n     t = sink; n     last[s] = next[last[s]]; n     last[t] = next[last[t]]; n     E -= 2; n     while (bfs()) { n       for (int i = V - 1; i >= 0; --i) now[i] = last[i]; n       while (true) { n         df = dfs(s, INT_MAX); n         if (df == 0) break; n         f += df; n       } n     } n     return f; n   } n }; n const int N = 100005; n pair<int, int> arr[N]; n int xs[N], ys[N]; n int dx[N], dy[N]; n int qntx[N], qnty[N]; n int pnt_id[N]; n char ans[N]; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   int r, b; n   scanf( %d %d , &r, &b); n   char cheap =  r , expensive =  b ; n   if (r > b) { n     swap(cheap, expensive); n     swap(r, b); n   } n   for (int i = 0; i < n; ++i) { n     int x, y; n     scanf( %d %d , &x, &y); n     arr[i] = pair<int, int>(x, y); n     xs[i] = x; n     ys[i] = y; n   } n   sort(xs, xs + n); n   int xssz = unique(xs, xs + n) - xs; n   sort(ys, ys + n); n   int yssz = unique(ys, ys + n) - ys; n   for (int i = 0; i < n; ++i) { n     arr[i].first = lower_bound(xs, xs + xssz, arr[i].first) - xs; n     arr[i].second = lower_bound(ys, ys + yssz, arr[i].second) - ys; n     ++qntx[arr[i].first]; n     ++qnty[arr[i].second]; n     ++dx[arr[i].first]; n     ++dy[arr[i].second]; n   } n   for (int i = 0; i < m; ++i) { n     int t, l, d; n     scanf( %d %d %d , &t, &l, &d); n     if (t == 1) { n       int k = lower_bound(xs, xs + xssz, l) - xs; n       if (xs[k] == l) { n         dx[k] = min(dx[k], d); n       } n     } else { n       int k = lower_bound(ys, ys + yssz, l) - ys; n       if (ys[k] == l) { n         dy[k] = min(dy[k], d); n       } n     } n   } n   flow_graph fg = flow_graph(xssz + yssz + 2, n + xssz + yssz); n   fg.clear(xssz + yssz + 2); n   int source = xssz + yssz, sink = source + 1; n   for (int i = 0; i < xssz; ++i) { n     int l = (qntx[i] - dx[i] + 1) / 2; n     int r = (qntx[i] + dx[i]) / 2; n     if (l > r) { n       puts( -1 ); n       return 0; n     } n     fg.add_edge(source, i, l, r); n   } n   for (int i = 0; i < n; ++i) { n     pnt_id[i] = fg.E; n     fg.add_edge(arr[i].first, arr[i].second + xssz, 0, 1); n   } n   for (int i = 0; i < yssz; ++i) { n     int l = (qnty[i] - dy[i] + 1) / 2; n     int r = (qnty[i] + dy[i]) / 2; n     if (l > r) { n       puts( -1 ); n       return 0; n     } n     fg.add_edge(i + xssz, sink, l, r); n   } n   if (fg.max_flow(source, sink) < 0) { n     puts( -1 ); n     return 0; n   } n   long long qnt = 0; n   for (int i = 0; i < n; ++i) { n     int id = pnt_id[i]; n     if (fg.flow[id] > 0) n       ans[i] = cheap, ++qnt; n     else n       ans[i] = expensive; n   } n   printf( %lld n , qnt * r + (n - qnt) * b); n   printf( %s n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, K; n vector<int> G[700005], W[700005], I[700005]; n long long int ptr[700005]; n long long int ans[700005]; n int dis[700005], rem[700005], ulta[700005], vis[700005], tim = 0, lev[700005]; n int mx = 0; n vector<int> jinish[700005]; n void dfs(int u, int l) { n   lev[u] = l; n   mx = max(mx, l); n   vis[u] = 1; n   dis[u] = ++tim; n   ulta[tim] = u; n   jinish[l].push_back(tim); n   for (int v : G[u]) { n     if (!vis[v]) { n       dfs(v, l + 1); n     } n   } n   rem[u] = tim; n } n void dfs2(int u) { n   vis[u] = 1; n   for (auto v : G[u]) { n     if (!vis[v]) { n       ans[v] += ans[u]; n       dfs2(v); n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   dfs(1, 1); n   scanf( %d , &m); n   while (m--) { n     int u, d, x; n     scanf( %d %d %d , &u, &d, &x); n     ans[u] += x; n     int go = min(lev[u] + d + 1, mx + 1); n     int in = lower_bound(jinish[go].begin(), jinish[go].end(), dis[u]) - n              jinish[go].begin(); n     if (in < jinish[go].size()) ptr[jinish[go][in]] += x; n     int inn = upper_bound(jinish[go].begin(), jinish[go].end(), rem[u]) - n               jinish[go].begin(); n     if (inn < jinish[go].size()) ptr[jinish[go][inn]] -= x; n   } n   for (int i = 1; i <= mx; i++) { n     for (int j = 1; j < jinish[i].size(); j++) { n       ptr[jinish[i][j]] += ptr[jinish[i][j - 1]]; n     } n   } n   for (int i = 1; i <= mx; i++) { n     for (int j = 0; j < jinish[i].size(); j++) { n       ans[ulta[jinish[i][j]]] -= ptr[jinish[i][j]]; n     } n   } n   memset(vis, 0, sizeof vis); n   dfs2(1); n   bool ff = false; n   for (int u = 1; u <= n; u++) { n     if (ff) printf(   ); n     ff = true; n     printf( %lld , ans[u]); n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n class Timer { n   clock_t start; n  n  public: n   Timer() : start(clock()) {} n   double now() { return (double)(clock() - start) / CLOCKS_PER_SEC; } n } timer; n struct node { n   int u, v, l; n   friend bool operator<(const node& a, const node& b) { return a.l < b.l; } n } in[300005]; n int dp[300005]; n queue<pair<int, int> > qe; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int u, v, l; n     scanf( %d%d%d , &in[i].u, &in[i].v, &in[i].l); n   } n   sort(in + 1, in + 1 + m); n   for (int i = 1; i <= m;) { n     int l = in[i].l; n     while (i <= m && in[i].l == l) { n       qe.push(make_pair(in[i].v, dp[in[i].u] + 1)); n       i++; n     } n     while (!qe.empty()) { n       dp[qe.front().first] = max(dp[qe.front().first], qe.front().second); n       qe.pop(); n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     ans = max(ans, dp[i]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, i; n   cin >> a >> b; n   int c = a; n   while (a >= b) { n     c++; n     a++; n     a = a - b; n   } n   i++; n   cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, b, c, co, i, j, k, l, m, n; n   cin >> n; n   co = 0; n   set<long long int> myset; n   for (i = 0; i < n; i++) { n     cin >> a; n     myset.insert(a); n   } n   if (myset.size() > 3) n     cout <<  NO n ; n   else if (myset.size() == 3) { n     i = 0; n     for (auto it = myset.begin(); it != myset.end(); it++) { n       if (i == 0) a = *it; n       if (i == 1) b = *it; n       if (i == 2) c = *it; n       i++; n     } n     if (abs(a - b) == abs(b - c)) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } else n     cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int mod = 1e9 + 7; n const int inf = (1 << 30) - 1; n const ll infll = (1LL << 61) - 1; n double a[1 << 17]; n int b[1 << 17], N; n ll pos_sum = 0, neg_sum = 0; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> N; n   for (int i = 0; i < N; i++) { n     cin >> a[i]; n     if (a[i] > 0) { n       b[i] = ceil(a[i]); n       pos_sum += b[i]; n     } else if (a[i] < 0) { n       b[i] = floor(a[i]); n       neg_sum += b[i]; n     } n   } n   if (abs(pos_sum) < abs(neg_sum)) { n     for (int i = 0; i < N && abs(pos_sum) < abs(neg_sum); i++) { n       if (a[i] < 0 && ceil(a[i]) != b[i]) { n         b[i] = ceil(a[i]); n         neg_sum++; n       } n     } n   } else if (abs(pos_sum) > abs(neg_sum)) { n     for (int i = 0; i < N && abs(pos_sum) > abs(neg_sum); i++) { n       if (a[i] > 0 && floor(a[i]) != b[i]) { n         b[i] = floor(a[i]); n         pos_sum--; n       } n     } n   } n   for (int i = 0; i < N; i++) { n     cout << b[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   vector<char> A(n * 2); n   for (int i = 0; i < n * 2; i++) cin >> A[i]; n   vector<vector<pair<int, int> > > table(n * 2, vector<pair<int, int> >(20)); n   for (int i = 0; i < n * 2; i++) { n     if (A[i] ==  X  || i == n - 1 || i == n * 2 - 1) n       table[i][0] = make_pair(-1, -1); n     else { n       if (A[i + 1] ==  . ) n         table[i][0] = make_pair(i + 1, 1); n       else { n         int num = (i < n ? i + n : i - n); n         if (A[num] ==  . ) n           table[i][0] = make_pair(num, 1); n         else n           table[i][0] = make_pair(-1, -1); n       } n     } n   } n   for (int k = 1; k < 20; k++) { n     for (int i = 0; i < n * 2; i++) { n       if (table[i][k - 1].first == -1) n         table[i][k] = make_pair(-1, -1); n       else { n         pair<int, int> p = table[i][k - 1]; n         table[i][k] = make_pair(table[p.first][k - 1].first, n                                 p.second + table[p.first][k - 1].second); n       } n     } n   } n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     a--, b--; n     if (a % n > b % n) swap(a, b); n     int k = 19; n     int res = 0; n     while (k >= 0) { n       if (table[a][k].first != -1 && table[a][k].first % n < b % n) { n         res += table[a][k].second; n         a = table[a][k].first; n       } n       k--; n     } n     if (a % n != b % n) { n       if (A[a + 1] ==  . ) n         a = a + 1, res++; n       else { n         a = a < n ? a + n : a - n, res++; n         if (A[a] ==  X ) { n           cout <<  -1 n ; n           continue; n         } n         if (A[a + 1] ==  . ) n           a = a + 1, res++; n         else { n           cout <<  -1 n ; n           continue; n         } n       } n     } n     if (a == b) n       cout << res <<   n ; n     else { n       if (A[a < n ? a + n : a - n] ==  . ) { n         a = (a < n ? a + n : a - n), res++; n         if (a == b) n           cout << res <<   n ; n         else n           cout <<  -1 n ; n       } else n         cout <<  -1 n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5, mod = 1e9 + 7; n int grundy[2000][2000], dr[50], dc[50]; n int x, y, n, d; n bool valid(int x, int y) { return x * x + y * y <= d * d; } n int mex(set<int> &st) { n   int ret = 0; n   while (st.count(ret)) ret++; n   return ret; n } n int solve(int x, int y) { n   if (grundy[x][y] != -1) return grundy[x][y]; n   set<int> st; n   for (int i = 0; i < n; i++) { n     int nx, ny; n     nx = x + dr[i]; n     ny = y + dc[i]; n     if (valid(nx - 200, ny - 200)) { n       st.insert(solve(nx, ny)); n     } n   } n   return grundy[x][y] = mex(st); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   memset(grundy, -1, sizeof grundy); n   cin >> x >> y >> n >> d; n   for (int i = 0; i < n; i++) { n     cin >> dr[i] >> dc[i]; n   } n   if (solve(x + 200, y + 200)) n     cout <<  Anton ; n   else n     cout <<  Dasha ; n   return 0; n } n


#include <bits/stdc++.h> n long long int MAX(long long int a, long long int b) { n   if (a > b) n     return a; n   else n     return b; n } n using namespace std; n int main() { n   string S, SS; n   cin >> S >> SS; n   int PR = 0, DIF = 0, l = S.length(); n   for (int x = 0; x < l; x++) n     if (S[x] != SS[x]) DIF++; n   if (DIF & 1) n     printf( impossible n ); n   else { n     DIF /= 2; n     bool SEC = false; n     for (int i = 0; i < l; i++) { n       if (S[i] == SS[i]) n         printf( 1 ); n       else { n         if (PR == DIF) SEC = true; n         if (!SEC) { n           if (S[i] ==  1 ) n             printf( 0 ); n           else n             printf( 1 ); n         } else { n           if (SS[i] ==  1 ) n             printf( 0 ); n           else n             printf( 1 ); n         } n         PR++; n       } n     } n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = (long long)1e4; n long long n, k; n vector<pair<int, int> > l, r; n int length(int x) { return x < 10 ? 1 : length(x / 10) + 1; } n pair<int, int> checkWavy(int x) { n   if (x < 10) return make_pair(1, 0); n   int digits[8] = {x % 10, x / 10 % 10}, len = 2, sign = digits[1] > digits[0]; n   if (digits[1] == digits[0]) return make_pair(0, 0); n   x /= 100; n   while (x) { n     digits[len] = x % 10; n     x /= 10; n     if (digits[len] == digits[len - 1]) return make_pair(0, 0); n     int newSign = digits[len] > digits[len - 1]; n     if (newSign == sign) return make_pair(0, 0); n     sign = newSign; n     len++; n   } n   if (len < 6) return make_pair(1, 0); n   if (len == 7) return make_pair(1, 1); n   return make_pair(1, sign); n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> k; n   for (int i = 1; i < int(1e7); i++) { n     pair<int, int> u = checkWavy(i); n     if (!u.first) continue; n     if (i % n == 0) n       if (!--k) { n         cout << i << endl; n         return 0; n       } n     l.push_back(make_pair(i, i % n)); n     if (u.second) r.push_back(make_pair(i % n, i)); n   } n   sort(l.begin(), l.end()); n   sort(r.begin(), r.end()); n   for (int i = 0; i < int(l.size()); i++) { n     if ((n - 1LL * l[i].second * int(1e7) % n) % n > int(1e7)) continue; n     int mod = (n - 1LL * l[i].second * int(1e7) % n) % n; n     int x = l[i].first, lastDigit = x % 10; n     int from = lower_bound(r.begin(), r.end(), make_pair(mod, 0)) - r.begin(); n     int to = lower_bound(r.begin(), r.end(), make_pair(mod + 1, 0)) - r.begin(); n     if (from == to) continue; n     for (int digit = 0; digit < 10 && from < to; digit++) { n       int bound = lower_bound(r.begin() + from, r.begin() + to, n                               make_pair(mod, (digit + 1) * int(1e6))) - n                   r.begin(); n       if (digit < lastDigit) { n         if (x < 10 || x / 10 % 10 < lastDigit) { n           int low = (digit * 10 + digit + 1) * int(1e5); n           int idLow = lower_bound(r.begin() + from, r.begin() + bound, n                                   make_pair(mod, low)) - n                       r.begin(); n           if (bound - idLow >= k) { n             cout << x; n             for (int q = length(r[from + k - 1].second); q < 7; q++) cout << 0; n             cout << r[idLow + k - 1].second << endl; n             return 0; n           } else n             k -= bound - idLow; n         } n       } n       if (digit > lastDigit) { n         if (x < 10 || x / 10 % 10 > lastDigit) { n           int high = (digit * 10 + digit) * int(1e5); n           int idHigh = lower_bound(r.begin() + from, r.begin() + bound, n                                    make_pair(mod, high)) - n                        r.begin(); n           if (idHigh - from >= k) { n             cout << x; n             for (int q = length(r[from + k - 1].second); q < 7; q++) cout << 0; n             cout << r[from + k - 1].second << endl; n             return 0; n           } else n             k -= idHigh - from; n         } n       } n       from = bound; n     } n   } n   cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 505; n int a[N]; n int b[N]; n int dp[N]; n int pv[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   int m; n   cin >> m; n   for (int i = 1; i <= m; i++) { n     cin >> b[i]; n   } n   int k; n   for (int i = 1; i <= n; i++) { n     k = 0; n     for (int j = 1; j <= m; j++) { n       if (a[i] == b[j]) { n         if (dp[j] < dp[k] + 1) { n           dp[j] = dp[k] + 1; n           pv[j] = k; n         } n       } else if (b[j] < a[i]) { n         if (dp[j] > dp[k]) { n           k = j; n         } n       } n     } n   } n   int d = 0; n   for (int i = 1; i <= m; i++) { n     if (dp[i] > dp[d]) d = i; n   } n   vector<int> ans; n   while (d != 0) { n     ans.push_back(b[d]); n     d = pv[d]; n   } n   reverse(ans.begin(), ans.end()); n   cout << ans.size() <<   n ; n   for (auto x : ans) cout << x <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200010; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b; n     cin >> a >> b; n     int s = abs(a - b); n     int ans = s / 5; n     s %= 5; n     if (s > 2) n       ans += 2; n     else if (s != 0) n       ans++; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NN = (1 << 15); n int pre[NN], suf[NN]; n map<string, int> mp; n int main() { n   int n; n   cin >> n; n   for (int i = 1, x; i <= n; i++) { n     cin >> x; n     pre[i] = x >> 15; n     suf[i] = x & (NN - 1); n   } n   for (int i = 0; i < NN; i++) { n     string tmp; n     for (int j = 2; j <= n; j++) { n       tmp.push_back(__builtin_popcount(i ^ pre[j]) - n                     __builtin_popcount(i ^ pre[1])); n     } n     if (!mp.count(tmp)) mp[tmp] = i; n   } n   for (int i = 0; i < NN; i++) { n     string tmp; n     for (int j = 2; j <= n; j++) { n       tmp.push_back(__builtin_popcount(i ^ suf[1]) - n                     __builtin_popcount(i ^ suf[j])); n     } n     if (mp.count(tmp)) { n       cout << (mp[tmp] << 15) + i << endl; n       return 0; n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, n; n int mat[105][105]; n int main() { n   cin >> m >> n; n   for (int i = 0; i < m; i++) { n     for (int j = 0; j < n; j++) { n       cin >> mat[i][j]; n     } n   } n   if (m % 2 != 0) { n     cout << m << endl; n   } else { n     int aux = m; n     bool flag = true; n     int cont = 0; n     bool flag2 = true; n     while (flag) { n       aux = aux / 2; n       if (aux == 1) { n         for (int j = 0; j < n; j++) { n           if (mat[0][j] != mat[1][j]) { n             flag2 = false; n           } n         } n         if (flag2) { n           cont++; n         } n         flag = false; n         break; n       } n       for (int i = 0; i < aux; i++) { n         for (int j = 0; j < n; j++) { n           if (mat[aux - i - 1][j] != mat[aux + i][j]) { n             flag = false; n           } n         } n       } n       if (flag) cont++; n       if (aux % 2 != 0) break; n     } n     for (int i = 0; i < cont; i++) { n       m = m / 2; n     } n     cout << m << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m; n   cin >> n >> m; n   double num; n   num = (double)m / n; n   if (n == m) { n     cout <<  0 ; n   } else { n     long long int x = num, counter1 = 0, counter2 = 0; n     if (ceil(num) == num) { n       if (x % 2 == 0 || x % 3 == 0) { n         while (x % 2 == 0) { n           x = x / 2; n           counter1++; n         } n         while (x % 3 == 0) { n           x = x / 3; n           counter2++; n         } n         if (pow(2, counter1) * pow(3, counter2) == num) n           cout << counter1 + counter2; n         else n           cout <<  -1 ; n       } else n         cout <<  -1 ; n     } else n       cout <<  -1 ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long calc(int n, int d, long long m, int l) { n   for (long long i = 0; i < n; i++) { n     long long high = i * m + l; n     long long x = high % d; n     if (d < (x + m - l)) { n       return high + (d - x); n     } n   } n   long long high = (n - 1) * m + l; n   long long x = high % d; n   return high + (d - x); n } n int main() { n   int n, d, m, l; n   scanf( %d %d %d %d , &n, &d, &m, &l); n   cout << calc(n, d, m, l) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   if (a.second == b.second) return (a.first < b.first); n   return (a.second < b.second); n } n int main(void) { n   long long a, b; n   cin >> a >> b; n   string str = to_string(b); n   reverse(str.begin(), str.end()); n   long long d = atoi(str.c_str()); n   cout << d + a; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n void finish(int x) { n   printf( ! %d n , x); n   fflush(stdout); n   exit(0); n } n int query(int x) { n   int a, b, mult = 1; n   if (x > n / 2) { n     x -= n / 2; n     mult = -1; n   } n   printf( ? %d n , x); n   fflush(stdout); n   scanf( %d , &a); n   printf( ? %d n , x + n / 2); n   fflush(stdout); n   scanf( %d , &b); n   if (a == b) finish(x); n   return mult * (b - a); n } n int main() { n   scanf( %d , &n); n   if (n % 4 != 0) finish(-1); n   int offset = 0, l = 1, r = n / 2; n   int dif = query(1); n   if (dif < 0) offset += n / 2; n   while (l < r) { n     int m = (l + r) / 2; n     if (query(m + offset) > 0) n       l = m + 1; n     else n       r = m - 1; n   } n   if (query(l) != 0) finish(-1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500005; n int n, k, i, t, j, m; n char c[N]; n long long ans, s; n struct str { n   int l, r; n } p[N], a[N]; n bool cmp(str a, str b) { return a.r - a.l > b.r - b.l; } n bool cmp2(str a, str b) { return a.r < b.r; } n int main() { n   scanf( %d , &n); n   scanf( %s , c + 1); n   for (i = 1; i <= n; ++i) n     if (c[i] ==  1 ) { n       for (j = i; c[j] ==  1 ; ++j) n         ; n       p[++k] = (str){i, j - 1}; n       i = j; n     } n   sort(p + 1, p + 1 + k, cmp); n   for (i = 1; i <= n; ++i) { n     int las = 0; n     for (j = 1; j <= k; ++j) n       if (p[j].r - p[j].l + 1 >= i) n         a[j] = p[j]; n       else n         break; n     m = j - 1; n     sort(a + 1, a + 1 + m, cmp2); n     s = las = 0; n     for (j = 1; j <= m; ++j) { n       if (las != 0) n         s += 1ll * (i + a[j].l + i - 1 - las) * (a[j].l - las) / 2; n       else n         s += 1ll * (a[j].l + i - 2) * (a[j].l + i - 1) / 2; n       las = a[j].r - i + 2; n       s += 1ll * (a[j].r - a[j].l + 2 - i) * (i - 1); n     } n     if (m != 0) n       s += 1ll * (i + n - las + 1) * (n - a[m].r) / 2; n     else n       s = 1ll * n * (n + 1) / 2; n     ans += 1ll * n * (n + 1) / 2 - s; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 9, B = 511; n inline long long read() { n   long long res = 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     res = (res << 3) + (res << 1) + ch -  0 ; n     ch = getchar(); n   } n   return res * f; n } n struct query { n   long long p, id; n   query(long long _p = 0, long long _id = 0) { p = _p, id = _id; } n }; n long long m, n, tri[N][27], fail[N], cnt = 1, siz[N], ind, dfn[N]; n long long len[N], fa[N], ed[N], tr[N], ans[N], a[N]; n char s[N]; n vector<long long> G[N]; n vector<query> L[N], R[N], ll[N], rr[N]; n inline bool cmp(query a, query b) { return a.p > b.p; } n inline void insert(long long x) { n   long long p = 1; n   for (long long i = 1; i <= len[x]; i++) { n     long long v = s[i] -  a ; n     if (!tri[p][v]) tri[p][v] = ++cnt, fa[tri[p][v]] = p; n     p = tri[p][v]; n   } n   ed[x] = p; n } n inline void build_fail() { n   queue<long long> Q; n   for (long long i = 0; i < 26; i++) n     if (tri[1][i]) n       Q.push(tri[1][i]), fail[tri[1][i]] = 1; n     else n       tri[1][i] = 1; n   while (!Q.empty()) { n     long long u = Q.front(); n     Q.pop(); n     for (long long i = 0; i < 26; i++) n       if (tri[u][i]) n         Q.push(tri[u][i]), fail[tri[u][i]] = tri[fail[u]][i]; n       else n         tri[u][i] = tri[fail[u]][i]; n   } n } n void dfs(long long u) { n   for (auto v : G[u]) dfs(v), a[u] += a[v]; n } n void Dfs(long long u) { n   siz[u] = 1; n   dfn[u] = ++ind; n   for (auto v : G[u]) Dfs(v), siz[u] += siz[v]; n } n inline void add(long long x, long long c) { n   while (x <= ind) tr[x] += c, x += (x & -x); n } n inline long long ask(long long x) { n   long long res = 0; n   while (x) res += tr[x], x -= (x & -x); n   return res; n } n signed main() { n   n = read(), m = read(); n   for (long long i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     len[i] = strlen(s + 1); n     insert(i); n   } n   build_fail(); n   for (long long i = 2; i <= cnt; i++) G[fail[i]].push_back(i); n   for (long long i = 1; i <= m; i++) { n     long long l, r, k; n     l = read(), r = read(), k = read(); n     if (len[k] > B) n       L[k].push_back(query(l, i)), R[k].push_back(query(r, i)); n     else n       ll[l].push_back(query(k, i)), rr[r].push_back(query(k, i)); n   } n   for (long long i = 1; i <= n; i++) n     if (L[i].size() || R[i].size()) { n       sort(L[i].begin(), L[i].end(), cmp); n       sort(R[i].begin(), R[i].end(), cmp); n     } n   for (long long i = 1; i <= n; i++) n     if (len[i] > B) { n       long long p = ed[i]; n       while (p != 1) a[p] = 1, p = fa[p]; n       dfs(1); n       for (long long j = 1, val = 0; j <= n; j++) { n         while (L[i].size() && L[i].back().p == j) n           ans[L[i].back().id] -= val, L[i].pop_back(); n         val += a[ed[j]]; n         while (R[i].size() && R[i].back().p == j) n           ans[R[i].back().id] += val, R[i].pop_back(); n       } n       memset(a, 0, sizeof a); n     } n   Dfs(1); n   for (long long i = 1; i <= n; i++) { n     for (auto j : ll[i]) { n       long long p = ed[j.p]; n       while (p != 1) ans[j.id] -= ask(dfn[p]), p = fa[p]; n     } n     add(dfn[ed[i]], 1), add(dfn[ed[i]] + siz[ed[i]], -1); n     for (auto j : rr[i]) { n       long long p = ed[j.p]; n       while (p != 1) ans[j.id] += ask(dfn[p]), p = fa[p]; n     } n   } n   for (long long i = 1; i <= m; i++) printf( %lld n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n const int MAX_MEM = 1e8; n int mpos = 0; n char mem[MAX_MEM]; n inline void *operator new(size_t n) { n   assert((mpos += n) <= MAX_MEM); n   return (void *)(mem + mpos - n); n } n inline void operator delete(void *) noexcept {} n using namespace std; n struct Ex { n   int s, d, c, i; n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   vector<Ex> data(m + 1); n   unordered_map<int, int> table(m + 1); n   for (int i = 1; i <= m; i++) { n     int s, d, c; n     cin >> s >> d >> c; n     data[i] = {s, d, c, i}; n     table[d] = i; n   } n   vector<int> answer(n + 1); n   vector<int> cnt(m + 1); n   for (int i = 1; i <= n; i++) { n     if (table[i]) { n       if (cnt[table[i]] == data[table[i]].c) { n         answer[i] = m + 1; n       } else { n         cout << -1; n         return 0; n       } n     } else { n       int best_ex = -1; n       int day = 1000; n       for (int j = 1; j <= m; j++) { n         if (data[j].d < day and cnt[data[j].i] < data[j].c and data[j].s <= i) { n           best_ex = data[j].i; n           day = data[j].d; n         } n       } n       if (best_ex == -1) { n         answer[i] = 0; n       } else { n         answer[i] = best_ex; n         cnt[best_ex]++; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     cout << answer[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, s1[1000005], r, s2[1000005], dp[1000005], w, mi, mid, ma; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &s1[i]); n   } n   for (int i = 1; i <= n; i++) { n     scanf( %d , &r); n     s2[r] = i; n   } n   for (int i = 1; i <= n; i++) { n     s1[i] = s2[s1[i]]; n   } n   w = 0; n   dp[0] = 10000000; n   for (int i = 1; i <= n; i++) { n     if (dp[w] > s1[i]) { n       dp[++w] = s1[i]; n     } else { n       mi = 0; n       ma = w; n       while (mi < ma) { n         mid = (mi + ma) >> 1; n         if (dp[mid] < s1[i]) { n           ma = mid; n         } else { n           mi = mid + 1; n         } n       } n       dp[mi] = s1[i]; n     } n   } n   printf( %d n , w); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> sieve(long long n) { n   long long *arr = new long long[n + 1](); n   vector<long long> vect; n   for (long long i = 2; i <= n; i++) n     if (arr[i] == 0) { n       vect.push_back(i); n       for (long long j = 2 * i; j <= n; j += i) arr[j] = 1; n     } n   return vect; n } n long long binpow(long long a, long long b, long long m) { n   a = a % m; n   long long res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n class cmp { n  public: n   bool operator()(pair<long long, long long> &a, n                   pair<long long, long long> &b) { n     if (a.second == b.second) n       return a.first < b.first; n     else n       return a.second < b.second; n   } n }; n signed main() { n   long long t; n   cin >> t; n   while (t--) { n     long long n, k; n     cin >> n >> k; n     vector<long long> v(n); n     for (long long i = 0; i < n; i++) cin >> v[i]; n     long long x = k; n     sort(v.begin(), v.end()); n     long long ans = 0; n     k *= 2; n     long long j = n - 1; n     vector<long long> vec; n     while (k--) { n       vec.push_back(v[j]); n       j--; n     } n     sort(vec.begin(), vec.end()); n     for (long long i = 0; i < vec.size() / 2; i++) { n       ans += (vec[i] / vec[i + x]); n     } n     for (long long i = 0; i <= j; i++) ans += v[i]; n     cout << ans <<   n ; n     ; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:100000000000,100000000000 ) n const long long INF = 1000000007; n const double cp = 2 * asin(1.0); n const double eps = 1e-9; n const long long mod = 1000000007; n using namespace std; n int main() { n   cin.tie(NULL); n   ios_base::sync_with_stdio(false); n   string s, ans; n   bool ok = false; n   cin >> s; n   for (char q =  a ; q <=  z ; q++) { n     string h; n     for (int(i) = 0; (i) < (s.size() + 1); i++) { n       h =   ; n       for (int(j) = 0; (j) < (i); j++) h += s[j]; n       h += q; n       if (i != s.size()) n         for (int(j) = (i); (j) <= (s.size() - 1); j++) h += s[j]; n       string t = h; n       reverse((t).begin(), (t).end()); n       if (t == h) { n         return cout << t, 0; n         ans = t, ok = true; n       } n     } n   } n   if (!ok) n     cout <<  NA ; n   else n     cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000000 + 100, logN = 23; n long long read() { n   long long x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n int f[N][logN + 1]; n int query(int l, int r) { n   long long ans = 0; n   for (int i = logN - 1; i >= 0; --i) { n     if (f[l][i] < r) { n       l = f[l][i]; n       ans += (1 << i); n     } n   } n   l = f[l][0]; n   ans++; n   if (l < r) return -1; n   return ans; n } n int main() { n   int n = read(), m = read(), mx = 0; n   for (int i = 1; i <= n; ++i) { n     int l = read() + 1, r = read() + 1; n     f[l][0] = max(f[l][0], r); n     mx = max(mx, r); n   } n   for (int i = 1; i <= mx; ++i) f[i][0] = max(f[i][0], max(i, f[i - 1][0])); n   for (int j = 1; j < logN; ++j) n     for (int i = 1; i <= mx; ++i) f[i][j] = f[f[i][j - 1]][j - 1]; n   while (m--) { n     int l = read() + 1, r = read() + 1; n     printf( %d n , query(l, r)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100001], a[5] =  hard ; n int x[100001]; n long long d[5][100001]; n const long long inf = 0x3f3f3f3f3f3f3f3f; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf(  %c , &s[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &x[i]); n   memset(d, 0x3f, sizeof(d)); n   for (int i = 0; i < 4; i++) d[i][0] = 0; n   for (int i = 0; i < 4; i++) { n     for (int j = 1; j <= n; j++) { n       if (s[j] == a[i]) { n         if (i >= 1) n           d[i][j] = min(d[i - 1][j - 1], d[i][j - 1] + x[j]); n         else n           d[i][j] = d[i][j - 1] + x[j]; n       } else n         d[i][j] = d[i][j - 1]; n     } n   } n   printf( %lld n , min({d[0][n], d[1][n], d[2][n], d[3][n]})); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fastModPow(long long a, long long n, long long mod) { n   if (mod == 1) return 0; n   if (n == 1) return a % mod; n   long long res = fastModPow(a, n / 2, mod); n   res = res * res % mod; n   if (n & 1) res = res * a % mod; n   return res; n } n long long fastMaxPow(long long a, long long n, long long mx) { n   if (n == 1) return min(a, mx); n   long long res = fastMaxPow(a, n / 2, mx); n   res = min(res * res, mx); n   if (n & 1) res = min(res * a, mx); n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   long long mod = 1000000009; n   long long m1 = fastMaxPow(2, m, 100001); n   if (n >= m1) { n     cout << 0; n     return 0; n   } n   long long mult = fastModPow(2, m, mod); n   long long res = 1; n   for (int i = 1; i <= n; i++) { n     mult--; n     if (mult <= 0) mult += mod; n     res = res * mult % mod; n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 100 * 1000 + 5; n const int lim = 1000 * 1000 * 1000; n const int inf = 1000 * 1000 * 1000 + 5; n int f[3 * maxN]; n vector<pair<int, string> > a[maxN]; n string t; n int ans; n void dfs(int v, int F) { n   for (auto u : a[v]) { n     int tmp = F; n     string s = u.second; n     for (int i = 0; i < s.size(); i++) { n       while (tmp && (tmp == t.size() || t[tmp] != s[i])) tmp = f[tmp - 1]; n       if (t[tmp] == s[i]) tmp++; n       if (tmp == t.size()) ans++; n     } n     dfs(u.first, tmp); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i < n; i++) { n     int par; n     string s; n     cin >> par >> s; n     par--; n     a[par].push_back(make_pair(i, s)); n   } n   cin >> t; n   int tmp = 0; n   for (int i = 1; i < t.size(); i++) { n     while (tmp && t[tmp] != t[i]) tmp = f[tmp - 1]; n     if (t[tmp] == t[i]) tmp++; n     f[i] = tmp; n   } n   dfs(0, 0); n   cout << ans << endl; n   return 0; n } n


//#include <bits/stdc++.h> n //using namespace std; n #pragma GCC optimize(2) n #pragma GCC optimize(3) n #pragma GCC target( avx ) n #pragma GCC optimize( Ofast ) n #include <cstdio> n  n #define rep(i,_l,_r) for(signed i=(_l),_end=(_r);i<=_end;++i) n #define fep(i,_l,_r) for(signed i=(_l),_end=(_r);i>=_end;--i) n #define print(x,y) write(x),putchar(y) n  n template <class T> inline T read(const T sample) { n     T x=0; int f=1; char s; n     while((s=getchar())> 9 ||s< 0 ) if(s== - ) f=-1; n     while(s>= 0 &&s<= 9 ) x=(x<<1)+(x<<3)+(s^48),s=getchar(); n     return x*f; n } n template <class T> inline void write(const T x) { n     if(x<0) return (void) (putchar( - ),write(-x)); n     if(x>9) write(x/10); n     putchar(x%10^48); n } n template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;} n template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;} n  n const int maxn=2e4+4; n  n int n,q,lg[maxn],a[maxn],st[32][18][maxn],dp[18][32][maxn],ans[maxn],f[maxn][32],tmp[32]; n struct node { n  int l,r,k; n } s[maxn]; n  n void init() { n  rep(i,2,n) lg[i]=lg[i>>1]+1; n } n  n int Query(int k,int l,int r) { n  int d=lg[r-l+1]; n  return Max(st[k][d][l],st[k][d][r-(1<<d)+1]); n } n  n int main() { n  n=read(9),q=read(9); init(); n  int lgn=lg[n]+1; n  rep(i,1,n) a[i]=read(9); n  rep(k,0,30) rep(i,1,n) dp[0][k][i]=Min(n,i+a[i]+k); n  rep(j,1,lgn) { n   rep(k,0,30) rep(i,1,n) st[k][0][i]=dp[j-1][k][i]; n   rep(k,0,30) rep(i,1,lgn) rep(p,1,n) n    if(p+(1<<i)-1<=n) n     st[k][i][p]=Max(st[k][i-1][p],st[k][i-1][p+(1<<i-1)]); n    else break; n   rep(k1,0,30) rep(k2,0,30) { n    if(k1+k2>30) break; n    rep(i,1,n) dp[j][k1+k2][i]=Max(dp[j][k1+k2][i],Query(k1,i,dp[j-1][k2][i])); n   } n  } n  rep(i,1,q) { n   s[i].l=read(9),s[i].r=read(9),s[i].k=read(9); n   if(s[i].l==s[i].r) ans[i]=-1; n   else rep(j,0,s[i].k) f[i][j]=s[i].l; n  } n  fep(j,lgn,0) { n   rep(k,0,30) rep(i,1,n) st[k][0][i]=dp[j][k][i]; n   rep(k,0,30) rep(i,1,lgn) rep(p,1,n) n    if(p+(1<<i)-1<=n) n     st[k][i][p]=Max(st[k][i-1][p],st[k][i-1][p+(1<<i-1)]); n    else break; n   rep(i,1,q) { n    if(ans[i]==-1) continue; n    rep(k,0,s[i].k) tmp[k]=0; n    rep(k1,0,s[i].k) rep(k2,0,s[i].k) { n     if(k1+k2>s[i].k) break; n     tmp[k1+k2]=Max(tmp[k1+k2],Query(k1,s[i].l,f[i][k2])); n    } n    if(tmp[s[i].k]<s[i].r) { n     ans[i]+=(1<<j); n     rep(k,0,s[i].k) f[i][k]=tmp[k]; n    } n   } n  } n  rep(i,1,q) print(ans[i]+1,  n ); n  return 0; n } n  n


#include <bits/stdc++.h> n using namespace std; n int n, t; n char G[500005]; n int solve(int k) { n   int last = n, i; n   int hs, ss; n   hs = ss = 0; n   for (i = 1; i <= n; i++) { n     if (G[i] ==  H ) hs++; n     if (G[i] ==  S ) ss++; n     if (G[i] ==  H ) last = i; n     if (hs - ss >= k && G[i] !=  . ) last = i; n   } n   if (hs - ss > k) { n     return 0; n   } n   if (t < last) return 0; n   int remT = t - 1; n   int pos, val = k, twice, st; n   for (pos = 1; pos <= last; pos++) { n     if (remT < 0) return 0; n     if (G[pos] ==  H ) { n       if (val > 0) n         val--; n       else { n         twice = (last - pos) << 1; n         if (twice <= remT) { n           return 1; n         } else { n           st = pos; n           ss = hs = 0; n           for (; pos <= last; pos++) { n             if (G[pos] ==  H ) n               hs++; n             else if (G[pos] ==  S ) n               ss++; n             if (hs <= ss) break; n           } n           if (pos > last) { n             return 0; n           } n           twice = (pos - st) * 3; n           remT -= twice; n           if (remT < 0) return 0; n         } n       } n     } else if (G[pos] ==  S ) n       val++; n     remT--; n   } n   return 1; n } n int main() { n   scanf( %d%d , &n, &t); n   scanf( %s , G + 1); n   int high = n + 1; n   int low = 0; n   int mid; n   while (low < high) { n     mid = (low + high) / 2; n     if (solve(mid)) { n       high = mid; n     } else { n       low = mid + 1; n     } n   } n   if (high <= n) n     printf( %d , high); n   else n     printf( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n - 1; i++) cout << 8; n   cout << 9 << endl; n   for (int i = 0; i < n; i++) cout << 1; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, size_t N> n ostream& operator<<(ostream& os, const array<T, N>& v); n template <class T1, class T2> n ostream& operator<<(ostream& os, const pair<T1, T2>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   bool f = 1; n   os <<  [ ; n   for (auto& i : v) { n     if (!f) os <<  ,  ; n     os << i; n     f = 0; n   } n   return os <<  ] ; n } n template <class T, size_t N> n ostream& operator<<(ostream& os, const array<T, N>& v) { n   bool f = 1; n   os <<  [ ; n   for (auto& i : v) { n     if (!f) os <<  ,  ; n     os << i; n     f = 0; n   } n   return os <<  ] ; n } n template <class T1, class T2> n ostream& operator<<(ostream& os, const set<T1, T2>& v) { n   bool f = 1; n   os <<  [ ; n   for (auto& i : v) { n     if (!f) os <<  ,  ; n     os << i; n     f = 0; n   } n   return os <<  ] ; n } n template <class T1, class T2> n ostream& operator<<(ostream& os, const multiset<T1, T2>& v) { n   bool f = 1; n   os <<  [ ; n   for (auto& i : v) { n     if (!f) os <<  ,  ; n     os << i; n     f = 0; n   } n   return os <<  ] ; n } n template <class T1, class T2, class T3> n ostream& operator<<(ostream& os, const map<T1, T2, T3>& v) { n   bool f = 1; n   os <<  [ ; n   for (auto& ii : v) { n     if (!f) os <<  ,  ; n     os <<  (  << ii.first <<   ->   << ii.second <<  )  ; n     f = 0; n   } n   return os <<  ] ; n } n template <class T1, class T2> n ostream& operator<<(ostream& os, const multimap<T1, T2>& v) { n   bool f = 1; n   os <<  [ ; n   for (auto& ii : v) { n     if (!f) os <<  ,  ; n     os <<  (  << ii.first <<   ->   << ii.second <<  )  ; n     f = 0; n   } n   return os <<  ] ; n } n template <class T1, class T2> n ostream& operator<<(ostream& os, priority_queue<T1, T2> v) { n   bool f = 1; n   os <<  [ ; n   while (!v.empty()) { n     auto x = v.top(); n     v.pop(); n     if (!f) os <<  ,  ; n     f = 0; n     os << x; n   } n   return os <<  ] ; n } n template <class T1, class T2> n void printarray(const T1& a, T2 sz, T2 beg = 0) { n   for (T2 i = beg; i < sz; i++) cout << a[i] <<    ; n   cout << endl; n } n void cio() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n } n const long long mod = 1e9 + 9; n void die() { n   printf( -1 n ); n   exit(0); n } n int N; n int p[500]; n void Do(int R) { n   for (int i = 1; i <= N; i++) n     if (p[i] < R) return; n   bool odd = false; n   int sum = 0; n   for (int i = 1; i <= N; i++) sum += p[i] - R; n   if (sum % 2) odd = true; n   int X; n   vector<pair<int, int> > ansv; n   set<pair<int, int> > S; n   for (int i = 1; i <= N; i++) S.insert({p[i] - R, i}); n   if (odd) { n     auto it = S.end(); n     it--; n     pair<int, int> pp = *it; n     X = pp.second; n     S.erase(it); n     S.insert({pp.first - 1, pp.second}); n     sum--; n   } n   if (S.rbegin()->first * 2 > sum) return; n   while (true) { n     auto it1 = S.end(); n     it1--; n     S.erase(it1); n     auto it2 = S.end(); n     it2--; n     S.erase(it2); n     pair<int, int> p1 = *it1, p2 = *it2; n     if (p1.first == 0 || p2.first == 0) break; n     ansv.push_back({p1.second, p2.second}); n     S.insert({p1.first - 1, p1.second}); n     S.insert({p2.first - 1, p2.second}); n   } n   if (odd && (int)ansv.size() == 0) return; n   if (odd) { n     bool flag = false; n     for (auto e : ansv) { n       if (e.first != X && e.second != X) flag = true; n     } n     if (!flag) return; n   } n   printf( %d n , R); n   printf( %d n , (int)ansv.size()); n   for (auto e : ansv) { n     for (int i = 1; i <= N; i++) { n       if (e.first == i || e.second == i) n         printf( 1 ); n       else if (odd && (e.first != X && e.second != X) && i == X) { n         odd = false; n         printf( 1 ); n       } else n         printf( 0 ); n     } n     printf(  n ); n   } n   exit(0); n } n int main() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) scanf( %d , &p[i]); n   for (int x = 100; x >= 1; x--) { n     Do(x); n   } n   printf( 0 n ); n   vector<pair<int, int> > ansv; n   while (p[1] > 0 && p[2] > 0) { n     p[1]--; n     p[2]--; n     ansv.push_back({1, 2}); n   } n   int X = 1; n   if (p[2] == 0) X = 2; n   for (int i = 1; i <= N; i++) n     if (i != X) { n       while (p[i] > 0) { n         p[i]--; n         ansv.push_back({X, i}); n       } n     } n   printf( %d n , (int)ansv.size()); n   for (auto e : ansv) { n     for (int i = 1; i <= N; i++) { n       if (e.first == i || e.second == i) n         printf( 1 ); n       else n         printf( 0 ); n     } n     printf(  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 0x3f3f3f3f; n const double eps = 1e-6; n const long long mod = 998244353; n inline string getstr(string &s, long long l, long long r) { n   string ret =   ; n   for (long long i = l; i <= r; i++) ret.push_back(s[i]); n   return ret; n } n long long modpow(long long x, long long y, long long md = mod) { n   if (y == 0) return 1; n   long long ret = modpow(x, y >> 1, md); n   ret = (long long)ret * ret % md; n   if (y & 1) ret = (long long)ret * x % md; n   return ret; n } n string s; n vector<pair<long long, long long> > g[2005]; n long long value[2005]; n long long LEN = 0; n bool error = 0; n void add_edge(long long x, long long y, long long cost) { n   g[x].push_back(make_pair(y, cost)); n   g[y].push_back(make_pair(x, cost)); n } n void dfs(long long x) { n   if (error) return; n   for (auto p : g[x]) { n     if (~value[p.first] && value[p.first] != value[x] ^ p.second) error = 1; n     if (error) return; n     if (~value[p.first]) continue; n     value[p.first] = value[x] ^ p.second; n     dfs(p.first); n   } n } n long long calc(long long len) { n   LEN = len; n   memset(value, -1, sizeof value); n   long long Maxa = (len) / 2; n   long long Maxb = ((long long)s.size() - 1) / 2; n   swap(Maxa, Maxb); n   for (long long i = 0; i <= 2002; i++) g[i].clear(); n   value[0] = 1; n   value[1001] = 1; n   for (long long i = 0; i < s.size(); i++) { n     if (s[i] ==  ? ) continue; n     long long posa = (i <= Maxa ? i : ((long long)s.size() - i - 1)); n     long long posb = (i <= Maxb ? i : (len - i)); n     if (i > len) { n       if (~value[posa] && value[posa] != s[i] -  0 ) return 0; n       value[posa] = s[i] -  0 ; n       continue; n     } n     add_edge(posa, posb + 1001, s[i] -  0 ); n   } n   long long ret = 1; n   error = 0; n   for (long long i = 1; i <= 2002; i++) { n     if (~value[i]) dfs(i); n   } n   for (long long i = 1; i <= 2002; i++) { n     if (((0 <= i && i <= Maxa) || (1001 <= i && i <= 1001 + Maxb)) && n         value[i] == -1) { n       value[i] = 0; n       (ret *= 2) %= mod; n       dfs(i); n     } n     if (error) return 0; n   } n   return ret; n } n signed main() { n   cin >> s; n   reverse(s.begin(), s.end()); n   long long ans = 0; n   for (long long len = 0; len < s.size(); len++) { n     (ans += calc(len)) %= mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   double n, m; n   scanf( %lf%lf , &n, &m); n   int a[1000], b[1000]; n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   for (int i = 0; i < n; i++) scanf( %d , b + i); n   double x; n   if (b[0] != 1 && a[0] != 1) { n     x = m + m / (b[0] - 1); n     for (int i = n - 1; i >= 1; i--) { n       if (a[i] == 1 || b[i] == 1) { n         printf( -1 ); n         return 0; n       } n       x += x / (a[i] - 1); n       x += x / (b[i] - 1); n     } n     x += x / (a[0] - 1); n     printf( %.10f , x - m); n   } else n     printf( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y; n   cin >> x >> y; n   int l = 0, r = 0, u = 0, d = 0; n   int tx = 0, ty = 0, dir = 0, px = 0, py = 0, k = 0; n   while (true) { n     px = tx; n     py = ty; n     if (dir == 0) { n       r = tx = r + 1; n       dir = 1; n     } else if (dir == 1) { n       u = ty = u + 1; n       dir = 2; n     } else if (dir == 2) { n       l = tx = l - 1; n       dir = 3; n     } else if (dir == 3) { n       d = ty = d - 1; n       dir = 0; n     } n     int rx1 = min(tx, px), ry1 = min(ty, py); n     int rx2 = max(tx, px), ry2 = max(ty, py); n     if ((x >= rx1 && x <= rx2) && (y >= ry1 && y <= ry2)) { n       cout << k; n       return 0; n     } n     k++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   int mini1 = 0; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mini1 = max(mini1, a[i]); n   } n   int mini2 = 0; n   cin >> n; n   int b[n]; n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n     mini2 = max(mini2, b[i]); n   } n   cout << mini1 <<     << mini2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void read(int &a) { n   a = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     a = (a << 1) + (a << 3) + (c ^ 48); n     c = getchar(); n   } n } n const int Maxn = 200000; n struct Segment { n   int l, r, t; n   friend bool operator<(Segment p, Segment q) { n     if (p.r == q.r) { n       return p.l < q.l; n     } n     return p.r < q.r; n   } n } a[Maxn + 5]; n int f[Maxn + 5]; n int n; n int d[Maxn << 1 | 5], d_len; n struct Segment_Node { n   int lson, rson; n   int val, lazy; n } seg[Maxn << 4 | 5]; n int Root[2]; n int id_tot; n void insert(int l, int r, int x, int &root, int left = 1, int right = d_len) { n   if (l > right || r < left) { n     return; n   } n   if (root == 0) { n     root = ++id_tot; n   } n   if (l <= left && r >= right) { n     seg[root].val += x; n     seg[root].lazy += x; n     return; n   } n   int mid = (left + right) >> 1; n   insert(l, r, x, seg[root].lson, left, mid); n   insert(l, r, x, seg[root].rson, mid + 1, right); n   seg[root].val = n       max(seg[seg[root].lson].val, seg[seg[root].rson].val) + seg[root].lazy; n } n int query(int l, int r, int root, int left = 1, int right = d_len) { n   if (l > right || r < left) { n     return 0; n   } n   if (root == 0) { n     return 0; n   } n   if (l <= left && r >= right) { n     return seg[root].val; n   } n   int mid = (left + right) >> 1; n   return max(query(l, r, seg[root].lson, left, mid), n              query(l, r, seg[root].rson, mid + 1, right)) + n          seg[root].lazy; n } n void update(int x, int a, int &root, int left = 1, int right = d_len) { n   if (root == 0) { n     root = ++id_tot; n   } n   a -= seg[root].lazy; n   if (left == right) { n     seg[root].val = max(seg[root].val, a); n     return; n   } n   int mid = (left + right) >> 1; n   if (x <= mid) { n     update(x, a, seg[root].lson, left, mid); n   } else { n     update(x, a, seg[root].rson, mid + 1, right); n   } n   seg[root].val = n       max(seg[seg[root].lson].val, seg[seg[root].rson].val) + seg[root].lazy; n } n int main() { n   read(n); n   for (int i = 1; i <= n; i++) { n     read(a[i].l), read(a[i].r), read(a[i].t); n     a[i].t--; n     d[++d_len] = a[i].l; n     d[++d_len] = a[i].r; n   } n   sort(d + 1, d + 1 + d_len); n   d_len = unique(d + 1, d + 1 + d_len) - d - 1; n   for (int i = 1; i <= n; i++) { n     a[i].l = lower_bound(d + 1, d + 1 + d_len, a[i].l) - d + 1; n     a[i].r = lower_bound(d + 1, d + 1 + d_len, a[i].r) - d + 1; n   } n   d_len++; n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n; i++) { n     f[i] = query(1, a[i].l - 1, Root[a[i].t]) + 1; n     insert(1, a[i].l - 1, 1, Root[a[i].t]); n     update(a[i].r, f[i], Root[a[i].t ^ 1]); n   } n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     ans = max(ans, f[i]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int problem, time; n   cin >> problem >> time; n   int sum = 240; n   int j = 0; n   for (int i = 1; i <= problem; ++i) { n     sum -= i * 5; n     if (sum < time) { n       break; n     } n     j++; n   } n   cout << j; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100 + 20; n const int LG = 15; n int a[N], cnt[N]; n map<vector<int>, int> mp; n int main() { n   ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     cnt[i] = __builtin_popcount(a[i]); n   } n   for (int mask = 0; mask < (1 << LG); mask++) { n     vector<int> vec(n + 20, 0); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < LG; j++) { n         if (!((mask >> j) & 1)) continue; n         vec[i] += (((a[i] >> j) & 1) ? -1 : 1); n       } n       vec[i] += cnt[i]; n     } n     mp[vec] = mask; n   } n   for (int mask = 0; mask < (1 << 30); mask += (1 << LG)) { n     vector<int> vec(n + 20, 0); n     for (int i = 0; i < n; i++) { n       for (int j = LG; j < 30; j++) { n         if (!((mask >> j) & 1)) continue; n         vec[i] += (((a[i] >> j) & 1) ? -1 : 1); n       } n       vec[i] *= -1; n     } n     for (int i = 0; i <= 30; i++) { n       auto it = mp.find(vec); n       if (it != mp.end()) return cout << (mask | it->second) << endl, 0; n       for (int j = 0; j < n; j++) vec[j]++; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9 + 7, maxn = 3e5 + 500; n vector<pair<int, int> > v[maxn]; n bool visited[maxn], deg[maxn]; n int d[maxn]; n set<int> s; n void dfs(int u, int par) { n   visited[u] = 1; n   int idx; n   for (int i = 0; i < v[u].size(); i++) { n     int w = v[u][i].first; n     if (!visited[w]) dfs(w, u); n     if (w == par) idx = v[u][i].second; n   } n   if (deg[u] != d[u]) { n     deg[par] ^= 1; n     s.insert(idx); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   int n, m, cntt = 0, cnt = 0; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     cin >> d[i]; n     if (d[i] == 1) cnt++; n     if (d[i] == -1) cntt++; n   } n   for (int i = 1; i <= m; i++) { n     int u, w; n     cin >> u >> w; n     v[u].push_back(make_pair(w, i)); n     v[w].push_back(make_pair(u, i)); n   } n   if (cnt % 2 == 1 and cntt == 0) return cout << -1 << endl, 0; n   if (cnt % 2 == 1) { n     for (int i = 1; i <= n; i++) { n       if (d[i] == -1) { n         d[i] = 1; n         break; n       } n     } n   } n   for (int i = 1; i <= n; i++) n     if (d[i] == -1) d[i] = 0; n   dfs(1, -1); n   cout << s.size() << endl; n   for (auto it : s) cout << it <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int x[100010]; n int main() { n   int n, a; n   cin >> n >> a; n   if (n == 1) { n     cout << 0 << endl; n     return 0; n   } n   for (int i = 0; i < n; ++i) { n     cin >> x[i]; n   } n   sort(x, x + n); n   int l = 1, r = n - 1; n   int ans = 0; n   ans = min(abs(a - x[r]) + x[r] - x[l], abs(a - x[l]) + x[r] - x[l]); n   l = 0, r = n - 2; n   ans = min(ans, min(abs(a - x[r]) + x[r] - x[l], abs(a - x[l]) + x[r] - x[l])); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, op, i, index = 0; n   cin >> n >> k; n   char ss[n - 1]; n   op = k; n   i = 97; n   while (op--) { n     ss[index] = char(i); n     i++; n     index++; n   } n   if (n - k != 0) { n     for (int d = 0; d < n - k; d++) { n       ss[index] = ss[d]; n       index++; n     } n   } n   for (int c = 0; c < n; c++) cout << ss[c]; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int x; n   scanf( %d , &x); n   return x; n } n inline void priv(vector<int> a) { n   for (int i = 0; i < ((int)(a).size()); ++i) n     printf( %d%c , a[i], i == (int)(a).size() - 1 ?   n  :    ); n } n template <typename T> n istream& operator>>(istream& i, vector<T>& v) { n   for (int j = 0; j < ((int)(v).size()); ++j) i >> v[j]; n   return i; n } n template <typename T> n string join(vector<T>& v) { n   stringstream s; n   for (int i = 0; i < ((int)(v).size()); ++i) s <<     << v[i]; n   return s.str().substr(1); n } n template <typename T> n ostream& operator<<(ostream& o, vector<T>& v) { n   if ((int)(v).size()) o << join(v); n   return o; n } n template <typename T1, typename T2> n istream& operator>>(istream& i, pair<T1, T2>& v) { n   return i >> v.first >> v.second; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& o, pair<T1, T2>& v) { n   return o << v.first <<  ,  << v.second; n } n const int MX = 100005, INF = 1001001001; n const long long int LINF = 1e18; n const double eps = 1e-10; n struct Maxflow { n   int n; n   vector<int> to, lim, next, head, dist, it; n   Maxflow() {} n   Maxflow(int n) : n(n), head(n, -1), it(n) {} n   void add(int a, int b, int c = 1) { n     next.push_back(head[a]); n     head[a] = to.size(); n     to.push_back(b); n     lim.push_back(c); n     next.push_back(head[b]); n     head[b] = to.size(); n     to.push_back(a); n     lim.push_back(0); n   } n   void bfs(int sv) { n     dist = vector<int>(n, INF); n     queue<int> q; n     dist[sv] = 0; n     q.push(sv); n     while (!q.empty()) { n       int v = q.front(); n       q.pop(); n       for (int i = head[v]; i != -1; i = next[i]) { n         if (lim[i] && dist[to[i]] == INF) { n           dist[to[i]] = dist[v] + 1; n           q.push(to[i]); n         } n       } n     } n   } n   int dfs(int v, int tv, int nf = INF) { n     if (v == tv) return nf; n     for (; it[v] != -1; it[v] = next[it[v]]) { n       int u = to[it[v]], f; n       if (!lim[it[v]] || dist[v] >= dist[u]) continue; n       if (f = dfs(u, tv, min(nf, lim[it[v]])), f) { n         lim[it[v]] -= f; n         lim[it[v] ^ 1] += f; n         return f; n       } n     } n     return 0; n   } n   int solve(int sv, int tv) { n     int flow = 0, f; n     while (1) { n       bfs(sv); n       if (dist[tv] == INF) return flow; n       for (int i = 0; i < (n); ++i) it[i] = head[i]; n       while (f = dfs(sv, tv), f) flow += f; n     } n   } n }; n int n, m; n vector<vector<int> > to, co; n vector<int> used, vs, es; n vector<vector<int> > g; n void dfs(int v, int p = -1) { n   used[v] = 1; n   for (int i = 0; i < ((int)(to[v]).size()); ++i) { n     int u = to[v][i]; n     if (u == p) continue; n     if (used[u]) { n       if (used[u] == 1) { n         g.push_back(vector<int>(1, co[v][i])); n         for (int i = (int)(vs).size() - 1;; --i) { n           g.back().push_back(es[i]); n           es[i] = -1; n           if (vs[i] == u) break; n         } n       } n     } else { n       vs.push_back(v); n       es.push_back(co[v][i]); n       dfs(u, v); n       if (es.back() != -1) g.push_back(vector<int>(1, es.back())); n       vs.pop_back(); n       es.pop_back(); n     } n   } n   used[v] = 2; n } n int main() { n   scanf( %d%d , &n, &m); n   to = co = vector<vector<int> >(n); n   for (int i = 0; i < (m); ++i) { n     int a, b, c; n     scanf( %d%d%d , &a, &b, &c); n     --a; n     --b; n     --c; n     to[a].push_back(b); n     co[a].push_back(c); n     to[b].push_back(a); n     co[b].push_back(c); n   } n   used = vector<int>(n); n   dfs(0); n   int l = (int)(g).size(), sv = m + l, tv = sv + 1; n   Maxflow mf(tv + 1); n   for (int i = 0; i < (m); ++i) mf.add(sv, i); n   for (int i = 0; i < (l); ++i) n     mf.add(m + i, tv, max(1, (int)(g[i]).size() - 1)); n   for (int i = 0; i < (l); ++i) { n     for (int j : g[i]) mf.add(j, m + i); n   } n   cout << mf.solve(sv, tv) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 3; n int n; n int main() { n   cin >> n; n   cout << (int)log2(n) + 1; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, m, n, rstart, rend, cstart, cend; n   scanf( %d %d %d %d , &n, &m, &a, &b); n   if ((m >= n) || (m == 1)) n     printf( 1 n ); n   else { n     rstart = (a / m) + 1; n     if ((a % m) == 0) --rstart; n     cstart = a - ((rstart - 1) * m); n     rend = (b / m) + 1; n     if ((b % m) == 0) --rend; n     cend = b - ((rend - 1) * m); n     if (((cstart == 1) && ((cend == m) || (b == n))) || (rstart == rend) || n         ((a == 1) && (b == n))) n       printf( 1 n ); n     else { n       if (((cstart != 1) && ((cend == m) || (b == n))) || n           ((cstart == 1) && (cend != m))) n         printf( 2 n ); n       else if ((rend - rstart) == 1) n         printf( 2 n ); n       else if ((cstart - cend) == 1) n         printf( 2 n ); n       else n         printf( 3 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ans, fa[100001]; n bool mark[100001]; n int find(int x) { n   if (x == fa[x]) return x; n   return fa[x] = find(fa[x]); n } n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     int fx = find(x), fy = find(y); n     if (fx != fy) { n       fa[fx] = fy; n       if (mark[fx] || mark[x] || mark[fy] || mark[y]) n         mark[fx] = mark[x] = mark[fy] = mark[y] = 1; n     } else n       mark[fx] = mark[x] = mark[fy] = mark[y] = 1; n   } n   for (int i = 1; i <= n; i++) n     if (find(i) == i && !mark[i]) ans++; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using uint = unsigned int; n template <class T> n using V = vector<T>; n template <class T> n using VV = vector<vector<T>>; n template <class T, class U> n void chmax(T& x, U y) { n   if (x < y) x = y; n } n template <class T, class U> n void chmin(T& x, U y) { n   if (y < x) x = y; n } n template <class T> n void mkuni(V<T>& v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n } n template <class S, class T> n ostream& operator<<(ostream& o, const pair<S, T>& p) { n   return o <<  (  << p.first <<  ,  << p.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& o, const vector<T>& vc) { n   o <<  { ; n   for (const T& v : vc) o << v <<  , ; n   o <<  } ; n   return o; n } n constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } n unsigned int mod = 1; n struct ModInt { n   using uint = unsigned int; n   using ll = long long; n   using ull = unsigned long long; n   uint v; n   ModInt() : v(0) {} n   ModInt(ll _v) : v(normS(_v % mod + mod)) {} n   explicit operator bool() const { return v != 0; } n   static uint normS(const uint& x) { return (x < mod) ? x : x - mod; } n   static ModInt make(const uint& x) { n     ModInt m; n     m.v = x; n     return m; n   } n   ModInt operator+(const ModInt& b) const { return make(normS(v + b.v)); } n   ModInt operator-(const ModInt& b) const { return make(normS(v + mod - b.v)); } n   ModInt operator-() const { return make(normS(mod - v)); } n   ModInt operator*(const ModInt& b) const { return make((ull)v * b.v % mod); } n   ModInt operator/(const ModInt& b) const { return *this * b.inv(); } n   ModInt& operator+=(const ModInt& b) { return *this = *this + b; } n   ModInt& operator-=(const ModInt& b) { return *this = *this - b; } n   ModInt& operator*=(const ModInt& b) { return *this = *this * b; } n   ModInt& operator/=(const ModInt& b) { return *this = *this / b; } n   ModInt& operator++(int) { return *this = *this + 1; } n   ModInt& operator--(int) { return *this = *this - 1; } n   ll extgcd(ll a, ll b, ll& x, ll& y) const { n     ll p[] = {a, 1, 0}, q[] = {b, 0, 1}; n     while (*q) { n       ll t = *p / *q; n       for (int i = 0; i < int(3); i++) swap(p[i] -= t * q[i], q[i]); n     } n     if (p[0] < 0) n       for (int i = 0; i < int(3); i++) p[i] = -p[i]; n     x = p[1], y = p[2]; n     return p[0]; n   } n   ModInt inv() const { n     ll x, y; n     extgcd(v, mod, x, y); n     return make(normS(x + mod)); n   } n   ModInt pow(ll p) const { n     if (p < 0) return inv().pow(-p); n     ModInt a = 1; n     ModInt x = *this; n     while (p) { n       if (p & 1) a *= x; n       x *= x; n       p >>= 1; n     } n     return a; n   } n   bool operator==(const ModInt& b) const { return v == b.v; } n   bool operator!=(const ModInt& b) const { return v != b.v; } n   friend istream& operator>>(istream& o, ModInt& x) { n     ll tmp; n     o >> tmp; n     x = ModInt(tmp); n     return o; n   } n   friend ostream& operator<<(ostream& o, const ModInt& x) { return o << x.v; } n }; n using mint = ModInt; n V<mint> fact, ifact, invs; n mint Choose(int a, int b) { n   if (b < 0 || a < b) return 0; n   return fact[a] * ifact[b] * ifact[a - b]; n } n void InitFact(int N) { n   N++; n   fact.resize(N); n   ifact.resize(N); n   invs.resize(N); n   fact[0] = 1; n   for (int i = 1; i <= int(N - 1); i++) fact[i] = fact[i - 1] * i; n   ifact[N - 1] = fact[N - 1].inv(); n   for (int i = N - 2; i >= 0; i--) ifact[i] = ifact[i + 1] * (i + 1); n   for (int i = 1; i <= int(N - 1); i++) invs[i] = fact[i - 1] * ifact[i]; n } n int main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   cout << fixed << setprecision(20); n   int N, M; n   cin >> N >> M >> mod; n   InitFact(100000); n   V<mint> way(N + 1), sum(N + 1); n   way[0] = 1; n   for (int n = 1; n <= int(N); n++) { n     for (int i = 0; i < int(n); i++) { n       way[n] += way[i] * way[n - 1 - i] * Choose(n - 1, i) * (n + 1); n       sum[n] += sum[i] * way[n - 1 - i] * Choose(n - 1, i) * (n + 1); n       sum[n] += way[i] * sum[n - 1 - i] * Choose(n - 1, i) * (n + 1); n       auto f = [&](int x) { return x * (x + 1) / 2; }; n       sum[n] += n           way[i] * way[n - 1 - i] * Choose(n - 1, i) * (f(i) + f(n - 1 - i)); n     } n   } n   VV<mint> f(N + 2, V<mint>(N + 1)); n   f[0][0] = 1; n   VV<mint> g(N + 2, V<mint>(N + 1)); n   for (int n = 0; n < int(N + 1); n++) n     for (int k = 0; k < int(n + 1); k++) { n       for (int dn = 1; dn <= int(N); dn++) { n         if (n + dn > N || k + dn > M) break; n         f[n + dn + 1][k + dn] += f[n][k] * way[dn] * Choose(k + dn, k); n         g[n + dn + 1][k + dn] += n             (f[n][k] * sum[dn] + g[n][k] * way[dn]) * Choose(k + dn, k); n       } n       f[n + 1][k] += f[n][k]; n       g[n + 1][k] += g[n][k]; n     } n   cout << g[N + 1][M] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5; n const int INF = 1e9; n long long MOD = 998244353; n long long sum(long long n) { n   long long sm = 0; n   while (n) { n     sm += n % 10; n     n /= 10; n   } n   return sm; n } n int main() { n   list<int> a; n   list<int> b; n   long long n; n   cin >> n; n   int ans = 0; n   vector<long long> res; n   for (long long i = max(n - 100, 0LL); i <= n; i++) { n     if (sum(i) + i == n) { n       ans++; n       res.push_back(i); n     } n   } n   cout << ans <<   n ; n   for (int i = 0; i < res.size(); i++) { n     cout << res[i] <<    ; n   } n   return 0; n } n