#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n const int mod = 1e9 + 7; n struct edge { n   int u, v, t, c, id; n } e[N]; n int n, m; n vector<edge> s[N]; n bool cmp(edge a, edge b) { return a.c < b.c; } n struct TwoSat { n   static const int N = ::N << 1; n   int dfn[N], low[N], id[N], st[N], _st, _, cc; n   vector<int> g[N]; n   int mark[N], n; n   void init(int _n) { n     for (int i = ((n = _n << 1)) - 1; i >= (0); --i) g[i].clear(); n   } n   int new_node() { n     for (int i = (0); i < (2); i++) g[n++].clear(); n     return n / 2 - 1; n   } n   void addedge(int a, int va, int b, int vb) { n     a = a << 1 | va; n     b = b << 1 | vb; n     g[a].push_back(b); n     g[b ^ 1].push_back(a ^ 1); n   } n   void add_set(int a, int va) { n     a = a << 1 | va; n     g[a ^ 1].push_back(a); n   } n   void add_then(int a, int va, int b, int vb) { addedge(a, va, b, vb ^ 1); } n   void add_or(int a, int va, int b, int vb) { addedge(a, va ^ 1, b, vb); } n   void add_xor(int a, int va, int b, int vb) { n     addedge(a, va, b, vb); n     addedge(b, vb, a, va); n   } n   void dfs(int c, vector<int> g[]) { n     dfn[c] = low[c] = ++cc; n     st[_st++] = c; n     for (auto t : g[c]) n       if (!dfn[t]) n         dfs(t, g), low[c] = min(low[c], low[t]); n       else if (!id[t]) n         low[c] = min(low[c], dfn[t]); n     if (low[c] == dfn[c]) { n       ++_; n       do { n         id[st[--_st]] = _; n       } while (st[_st] != c); n     } n   } n   void find() { n     fill_n(dfn, n, cc = 0); n     fill_n(low, n, _st = 0); n     fill_n(id, n, _ = 0); n     for (int i = (0); i < (n); i++) n       if (!dfn[i]) dfs(i, g); n     for (int i = (0); i < (n); i++) --id[i]; n     return; n   } n   bool solve() { n     find(); n     for (int i = 0; i < n; i += 2) { n       if (id[i] == id[i + 1]) return 0; n       mark[i >> 1] = (id[i] > id[i + 1]); n     } n     return 1; n   } n } ts; n int p = 0; n void solve(int u) { n   sort(s[u].begin(), s[u].end(), cmp); n   for (int i = 0, j; i < (int)s[u].size();) { n     j = i; n     while (j < (int)s[u].size() && s[u][j].c == s[u][i].c) j++; n     if (j - i >= 3) { n       cout <<  No n ; n       exit(0); n     } n     if (j - i == 2) { n       int x = s[u][i].id, y = s[u][i + 1].id; n       ts.add_or(x, 0, y, 0); n     } n     i = j; n   } n   for (int i = (0); i < ((int)s[u].size() - 1); i++) { n     ts.addedge(p + i + 1, 0, p + i, 0); n   } n   for (int i = (0); i < ((int)s[u].size()); i++) { n     int x = s[u][i].id; n     ts.addedge(x, 0, p + i, 0); n     if (i != (int)s[u].size() - 1) ts.addedge(x, 0, p + i + 1, 1); n   } n } n void link() { n   p = m; n   for (int i = (1); i < (n + 1); i++) { n     solve(i); n     p += (int)s[i].size(); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   int L = 0, R = 0; n   for (int i = (1); i < (m + 1); i++) { n     cin >> e[i].u >> e[i].v >> e[i].c >> e[i].t; n     e[i].id = i - 1; n     R = max(R, e[i].t); n     s[e[i].u].push_back(e[i]); n     s[e[i].v].push_back(e[i]); n   } n   int ans = -1; n   while (L <= R) { n     int mid = (L + R) / 2; n     ts.init(3 * m); n     link(); n     for (int i = (1); i < (m + 1); i++) n       if (e[i].t > mid) { n         ts.add_set(e[i].id, 1); n       } n     if (ts.solve()) { n       ans = mid; n       R = mid - 1; n     } else n       L = mid + 1; n   } n   if (ans == -1) n     cout <<  No ; n   else { n     ts.init(3 * m); n     link(); n     for (int i = (1); i < (m + 1); i++) n       if (e[i].t > ans) ts.add_set(e[i].id, 1); n     ts.solve(); n     cout <<  Yes n ; n     vector<int> task; n     for (int i = 0; i < m; ++i) n       if (!ts.mark[i]) task.push_back(i + 1); n     cout << ans <<     << (int)task.size() <<   n ; n     for (auto v : task) cout << v <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000005; n const long long MOD = 1000000007; n long long n, k; n long long pre[MAXN], suf[MAXN], fac[MAXN], ifac[MAXN]; n inline long long quick_pow(long long x, long long k) { n   long long res = 1; n   while (k) { n     if (k & 1) (res *= x) %= MOD; n     (x *= x) %= MOD; n     k >>= 1; n   } n   return res; n } n inline void init() { n   pre[0] = suf[k + 3] = fac[0] = 1; n   for (int i = 1; i <= k + 2; i++) pre[i] = pre[i - 1] * (n - i) % MOD; n   for (int i = k + 2; i >= 1; i--) suf[i] = suf[i + 1] * (n - i) % MOD; n   for (int i = 1; i <= k + 2; i++) fac[i] = fac[i - 1] * i % MOD; n   ifac[k + 2] = quick_pow(fac[k + 2], MOD - 2); n   for (int i = k + 2 - 1; i >= 0; i--) ifac[i] = ifac[i + 1] * (i + 1) % MOD; n } n inline long long calc(int i, long long tmp) { n   return ((k - i) & 1 ? -1 : 1) * pre[i - 1] * suf[i + 1] % MOD * ifac[i - 1] % n          MOD * ifac[k + 2 - i] % MOD * tmp % MOD; n } n int main() { n   cin >> n >> k; n   init(); n   long long ans = 0, tmp = 0; n   for (int i = 1; i <= k + 2; i++) { n     (tmp += quick_pow(i, k)) %= MOD; n     (ans += calc(i, tmp) % MOD) %= MOD; n   } n   cout << (ans + MOD) % MOD; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string st; n double ans = 0.0, curr = 0.0; n int flag = 0; n int main() { n   cin >> st; n   for (int i = 0; i < st.size(); i++) { n     if ( a  <= st[i] && st[i] <=  z ) { n       ans += curr; n       curr = 0.0; n       continue; n     } n     if (st[i] ==  . ) { n       if (i + 3 < st.size() &&  0  <= st[i + 3] && st[i + 3] <=  9 ) { n         curr = curr * 1000 + st[i + 1] * 100 + st[i + 2] * 10 + st[i + 3] - n                48 * 111; n         i += 3; n         continue; n       } n       curr += (st[i + 1] * 10 + st[i + 2] - 48 * 11) / 100.0; n       i += 2; n       continue; n     } n     curr = curr * 10 + st[i] - 48; n   } n   ans += curr; n   string s = to_string(ans); n   s.erase(s.end() - 4, s.end()); n   int n = s.size(); n   stack<char> S; n   if (s[n - 1] !=  0  || s[n - 2] !=  0 ) n     S.push(s[n - 1]), S.push(s[n - 2]), S.push( . ); n   for (int i = n - 4, j = 1; i >= 0; i--, j++) { n     S.push(s[i]); n     if (j % 3 == 0 && i) S.push( . ); n   } n   while (!S.empty()) cout << S.top(), S.pop(); n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10, MAXM = 1e6 + 10; n const int INF = INT_MAX, SINF = 0x3f3f3f3f; n const long long llINF = LLONG_MAX; n const int MOD = 1e9 + 7, mod = 998244353; n const int inv2 = 5e8 + 4; n long double x, y, z; n long double a[MAXN]; n string s[MAXN]; n int main() { n   cin >> x >> y >> z; n   a[1] = powl(y, z) * log(x); n   a[2] = powl(z, y) * log(x); n   a[3] = z * y * log(x); n   a[4] = z * y * log(x); n   a[5] = powl(x, z) * log(y); n   a[6] = powl(z, x) * log(y); n   a[7] = z * x * log(y); n   a[8] = z * x * log(y); n   a[9] = powl(x, y) * log(z); n   a[10] = powl(y, x) * log(z); n   a[11] = x * y * log(z); n   a[12] = x * y * log(z); n   int cnt = max_element(a + 1, a + 13) - a; n   s[1] =  x^y^z ; n   s[2] =  x^z^y ; n   s[3] =  (x^y)^z ; n   s[4] =  (x^z)^y ; n   s[5] =  y^x^z ; n   s[6] =  y^z^x ; n   s[7] =  (y^x)^z ; n   s[8] =  (y^z)^x ; n   s[9] =  z^x^y ; n   s[10] =  z^y^x ; n   s[11] =  (z^x)^y ; n   s[12] =  (z^y)^x ; n   cout << s[cnt]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n int sum(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; } n int sub(int a, int b) { return a - b < 0 ? a - b + MOD : a - b; } n int mul(int a, int b) { return (1LL * a * b) % MOD; } n const int MAXN = 1e6 + 7; n int shuru[MAXN], shesh[MAXN]; n int zero[MAXN]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   string s; n   cin >> s; n   int n = s.size(); n   int zerosum = 1; n   int sheshsum = 0; n   int z = 0; n   for (int i = n - 1; i >= 0; i--) { n     if (s[i] ==  0 ) { n       z++; n       if (i + z == n) zerosum = sum(zerosum, 1); n       zerosum = sub(zerosum, zero[z]); n       zero[z] = shuru[i + z]; n       zerosum = sum(zerosum, zero[z]); n     } else { n       z = 0; n       shesh[i] = zerosum; n       sheshsum = sum(sheshsum, shesh[i]); n       shuru[i] = sheshsum; n     } n   } n   if (z == n) { n     cout << n <<   n ; n   } else { n     cout << mul(z + 1, shuru[z]) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5E4 + 5; n const int M = N * 17 * 17; n int n, m, q, x, y, son[N], dep[N], fa[N], g[N], l[N], tot, f[N], mark[M], to[N], n     ch[M][2], sum, c[N], a[N], k, b[N]; n long long ans[N], tr[M], C; n vector<int> G[N]; n int dfs(int x) { n   int y; n   son[x] = 1; n   dep[x] = dep[fa[x]] + 1; n   for (int i = 0; i < G[x].size(); i++) { n     son[x] += dfs(y = G[x][i]); n     if (son[y] > son[g[x]]) g[x] = y; n   } n   return son[x]; n } n void dfs2(int x, int pre) { n   l[x] = ++tot; n   b[tot] = x; n   f[x] = pre + 1; n   if (g[x]) dfs2(g[x], pre); n   for (int i = 0; i < G[x].size(); i++) n     if (G[x][i] != g[x]) dfs2(G[x][i], tot); n } n void push_down(int p, int l, int mid, int r, int &lc, int &rc) { n   if (!mark[p]) return; n   if (!lc) lc = ++sum; n   if (!rc) rc = ++sum; n   tr[lc] += (long long)mark[p] * (mid - l + 1), mark[lc] += mark[p]; n   tr[rc] += (long long)mark[p] * (r - mid), mark[rc] += mark[p]; n   mark[p] = 0; n } n long long get(int &p, int l, int r, int x, int y, int k) { n   if (x > r || y < l) return 0; n   if (!p) p = ++sum; n   if (x <= l && y >= r) { n     int len = r - l + 1; n     tr[p] += k * len; n     mark[p] += k; n     return tr[p] - k * len; n   } n   int mid = (l + r) / 2; n   push_down(p, l, mid, r, ch[p][0], ch[p][1]); n   long long ans = n       get(ch[p][0], l, mid, x, y, k) + get(ch[p][1], mid + 1, r, x, y, k); n   tr[p] = tr[ch[p][0]] + tr[ch[p][1]]; n   return ans; n } n void work(int x, int v, int k) { n   ans[v] += (long long)c[v] * c[v] * dep[x]; n   for (; x;) { n     ans[v] += 2ll * k * n               (get(to[v], 1, n, f[x], l[x], k) * c[v] - n                C * (dep[x] - dep[fa[b[f[x]]]])) * n               c[v]; n     x = fa[b[f[x]]]; n   } n } n int main() { n   cin >> n >> m >> q >> C; n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= m; i++) ans[i] = C * C * n; n   for (int i = 2; i <= n; i++) { n     scanf( %d , &fa[i]); n     G[fa[i]].push_back(i); n   } n   for (int i = 1; i <= m; i++) scanf( %d , &c[i]); n   dfs(1); n   dfs2(1, 0); n   for (int i = 1; i <= n; i++) work(i, a[i], 1); n   for (int i = 1; i <= q; i++) { n     scanf( %d%d , &k, &x); n     if (k == 1) { n       scanf( %d , &y); n       work(x, a[x], -1); n       work(x, y, 1); n       a[x] = y; n     } else n       printf( %.6f n , (double)ans[x] / n); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int NN = 50010; n const int INF = 0x3f3f3f3f; n const int MOD = 100000, STA = 8000010; n const long long LNF = 1LL << 60; n const double EPS = 1e-8; n const double OO = 1e15; n const int dx[4] = {-1, 0, 1, 0}; n const int dy[4] = {0, 1, 0, -1}; n const int day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n inline int sign(double x) { return (x > EPS) - (x < -EPS); } n template <class T> n T gcd(T a, T b) { n   return b ? gcd(b, a % b) : a; n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n template <class T> n inline T lcm(T a, T b, T d) { n   return a / d * b; n } n template <class T> n inline T Min(T a, T b) { n   return a < b ? a : b; n } n template <class T> n inline T Max(T a, T b) { n   return a > b ? a : b; n } n template <class T> n inline T Min(T a, T b, T c) { n   return min(min(a, b), c); n } n template <class T> n inline T Max(T a, T b, T c) { n   return max(max(a, b), c); n } n template <class T> n inline T Min(T a, T b, T c, T d) { n   return min(min(a, b), min(c, d)); n } n template <class T> n inline T Max(T a, T b, T c, T d) { n   return max(max(a, b), max(c, d)); n } n int N, M, T; n int n, m, x, t, w, h; n map<int, deque<int> > Q; n struct TT { n   int g, p, t, id; n } E[101111]; n int tp[101111], a[101111], b[101111]; n bool cp(TT a, TT b) { return a.p < b.p; } n void init(int x, int now) { n   for (int i = 1; i <= n; i++) { n     if (E[i].g == x) Q[E[i].t - E[i].p].push_back(i); n   } n   for (int i = 1; i <= n; i++) n     if (E[i].g != x) { n       auto p = Q.find(E[i].t - E[i].p); n       if (p == Q.end() || p->second.empty()) { n         tp[i] = i; n       } else { n         tp[i] = p->second.back(); n         p->second.pop_back(); n         p->second.push_front(i); n       } n     } n   Q.clear(); n } n int main() { n   scanf( %d%d%d , &n, &w, &h); n   int g, p, t; n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d , &E[i].g, &E[i].p, &E[i].t); n     E[i].id = i; n   } n   sort(E + 1, E + n + 1, cp); n   init(1, 2); n   init(2, 1); n   for (int i = 1; i <= n; i++) { n     if (E[i].g == 1) { n       a[E[tp[i]].id] = E[i].p; n       b[E[tp[i]].id] = h; n     } else { n       a[E[tp[i]].id] = w; n       b[E[tp[i]].id] = E[i].p; n     } n   } n   for (int i = 1; i <= n; i++) { n     printf( %d %d n , a[i], b[i]); 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   ; n   int n, k; n   cin >> n >> k; n   while (n > 0 && k > 0) { n     k--; n     if (n % 10 == 0) n       n = n / 10; n     else n       n--; n   } n   cout << n <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long A[200005], tree[200005]; n void update(int pos, long long v) { n   for (; pos < 200005; pos += pos & -pos) tree[pos] += v; n } n long long read(int pos) { n   long long ret = 0; n   for (; pos; pos -= pos & -pos) ret += tree[pos]; n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cout.setf(ios::fixed); n   cout.precision(10); n   int i, size = 1, t, n, lo, hi; n   long long x, sum = 0, a; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> t; n     if (t == 1) { n       cin >> a >> x; n       lo = 1, hi = min((long long)size, a); n       update(lo, x); n       update(hi + 1, -x); n       sum += hi * x; n     } else { n       if (t == 2) { n         cin >> x; n         update(size + 1, x); n         update(size + 2, -x); n         size++; n         sum += x; n       } else { n         if (size >= 2) { n           long long val = read(size); n           update(size, -val); n           update(size + 1, val); n           size--; n           sum -= val; n         } n       } n     } n     cout << (sum * 1.0 / size) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, int> sg; n map<int, bool> us; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x, d; n     cin >> x >> d; n     sg[x] = x + d; n     us[x] = true; n     if (us[x + d] && sg[x + d] == x && sg[x] == x + d) { n       cout <<  YES n ; n       return 0; n     } n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int v = 0; n   map<int, int> s; n   for (int i = 19; s.size() < 10010; i += 9) { n     int kk = i; n     int c = 0; n     while (kk > 0) { n       c += kk % 10; n       kk /= 10; n     } n     if (c == 10) s[v++] = i; n   } n   cout << s[n - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct data { n   int le, ri, cnt; n   data(int le_ = 0, int ri_ = 0, int cnt_ = 0) { n     le = le_; n     ri = ri_; n     cnt = cnt_; n   } n }; n const int N = 1e6 + 10; n int seg[4 * N + 10]; n int brk[5][4 * N + 10]; n int n; n void add(int idx, bool b, int L = 0, int R = n, int id = 1) { n   if (L == R - 1) { n     brk[b][id]++; n     return; n   } n   int mid = (L + R) / 2; n   if (idx < mid) n     add(idx, b, L, mid, 2 * id + 0); n   else n     add(idx, b, mid, R, 2 * id + 1); n   seg[id] = seg[2 * id + 0] + seg[2 * id + 1] + n             min(brk[0][2 * id + 0], brk[1][2 * id + 1]); n   brk[0][id] = n       brk[0][2 * id + 1] + max(0, brk[0][2 * id + 0] - brk[1][2 * id + 1]); n   brk[1][id] = n       brk[1][2 * id + 0] + max(0, brk[1][2 * id + 1] - brk[0][2 * id + 0]); n   return; n } n data get(int l, int r, int L = 0, int R = n, int id = 1) { n   if (L == l && R == r) return data(brk[0][id], brk[1][id], seg[id]); n   int mid = (L + R) / 2; n   data le, ri; n   if (l < mid) le = get(l, min(r, mid), L, mid, 2 * id + 0); n   if (r > mid) ri = get(max(l, mid), r, mid, R, 2 * id + 1); n   data ret; n   ret.cnt = le.cnt + ri.cnt + min(le.le, ri.ri); n   ret.le = ri.le + max(0, le.le - ri.ri); n   ret.ri = le.ri + max(0, ri.ri - le.le); n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   string s; n   cin >> s, n = s.size(); n   for (int i = 0; i < s.size(); i++) n     if (s[i] ==  ( ) n       add(i, 0); n     else n       add(i, 1); n   int q; n   cin >> q; n   while (q--) { n     int l, r; n     cin >> l >> r, l--; n     cout << 2 * get(l, r).cnt << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long f[1 << 10], M = 1000000; n long long l, r, k; n class Matrix { n  public: n   long long a[3][3]; n   Matrix(int fib = 0) { n     memset(a, 0, sizeof(a)); n     if (fib) a[1][2] = a[2][1] = a[2][2] = 1; n   } n   Matrix operator*(const Matrix &v) { n     Matrix ret; n     for (int i = 1; i <= 2; ++i) n       for (int j = 1; j <= 2; ++j) n         for (int k = 1; k <= 2; ++k) n           ret.a[i][k] = (ret.a[i][k] + a[i][j] * v.a[j][k]) % M; n     return ret; n   } n   void print() { cout << a[1][2] % M << endl; } n }; n Matrix one(1); n long long solve(long long l, long long r, long long v) { n   l = (l - 1) / v + 1; n   r = r / v; n   return r - l + 1; n } n Matrix fib(long long u) { n   if (u == 1) return one; n   Matrix cache = fib(u / 2); n   cache = cache * cache; n   if (u % 2) cache = cache * one; n   return cache; n } n int main() { n   cin >> M >> l >> r >> k; n   long long ans = 1; n   if (k >= 1000000) { n     ans = (r - l + 1) / (k - 1) + 2; n     while (solve(l, r, ans) < k) --ans; n   } else { n     for (int i = 1; i + k - 1 <= 1000000; ++i) { n       long long tl = (l - 1) / i + 1, tr = r / (i + k - 1); n       if (tl <= tr) ans = max(ans, tr); n     } n   } n   fib(ans).print(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long max(long long a, long long b) { return (a > b ? a : b); } n long long min(long long a, long long b) { return (a < b ? a : b); } n void algo() { n   string s1, s2; n   char c; n   cin >> s1 >> s2; n   long long j = 0, len = s1.size(); n   for (long long i = 0; i < len; i++) { n     c = s1[i]; n     long long k = j; n     if (s1[i + 1] == c && i != len - 1) { n       if (c == s2[j]) n         j++; n       else { n         cout <<  NO ; n         return; n       } n     } else { n       while (c == s2[j]) { n         j++; n       } n     } n     if (k == j) { n       cout <<  NO ; n       return; n     } n   } n   if (j != s2.size()) n     cout <<  NO ; n   else n     cout <<  YES ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     algo(); n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double const EPS = 1.0E-9; n int const MOD = (int)1e9 + 7; n inline void read(char* s) { scanf( %s , s); } n inline void read(int& x) { scanf( %d , &(x)); } n inline void read(long long& x) { scanf( %lld , &x); } n inline void read(double& x) { scanf( %lf , &x); } n inline void writeln(int x) { printf( %d n , x); } n inline void writeln(long long x) { printf( %lld n , x); } n inline void writeln(double x) { printf( %.12f n , x); } n template <typename... T> n void read(T&... args) { n   using swallow = int[]; n   swallow w = {0, (read(args), 0)...}; n } n inline long long gcd(long long x, long long y) { n   return y == 0 ? x : gcd(y, x % y); n } n inline long long lcm(long long x, long long y) { n   return x == 0 && y == 0 ? 0 : x / gcd(x, y) * y; n } n inline long long powmod(long long x, long long n, long long m = MOD) { n   long long r = 1; n   while (n) { n     if (n & 1) r = (r * x) % m; n     x = (x * x) % m; n     n /= 2; n   } n   return r; n } n static int leap(int y) { return y % 4 == 0 && y % 100 != 0 || y % 400 == 0; } n static int const month[2][12] = { n     {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, n     {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}; n static const int N = 1 << 18; n static const int M = 1 << 20; n int freq[N]; n int n, k; n int f[N]; n int h[12]; n int hw[5012][512]; n static int solve() { n   read(n, k); n   for (int i = 1; i <= n * k; ++i) { n     int a; n     read(a); n     freq[a]++; n   } n   for (int i = 1; i <= n; ++i) { n     int x; n     read(x); n     f[x]++; n   } n   for (int i = 1; i <= k; ++i) { n     read(h[i]); n   } n   for (int i = 0; i < 5012; ++i) { n     for (int j = 0; j < 512; ++j) { n       if (i == 0 || j == 0) { n         hw[i][j] = 0; n       } else { n         hw[i][j] = 0; n         for (int l = 0; l <= k && l <= i; ++l) { n           hw[i][j] = std::max(hw[i][j], hw[i - l][j - 1] + h[l]); n         } n       } n     } n   } n   long long ans = 0; n   for (int i = (0), vzq_b = (N); i < vzq_b; ++i) { n     int fa = f[i]; n     ans += hw[freq[i]][fa]; n   } n   writeln(ans); n   return 0; n } n int main(int argc, char* argv[]) { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ull = unsigned long long; n const int MAX_LENGTH = 100000 + 8; n const ull B = 100000007ULL; n char s[3][MAX_LENGTH]; n ull H[3][MAX_LENGTH], B_to[MAX_LENGTH] = {1ULL}; n int id[3] = {0, 1, 2}, len[3]; n void init_hash() { n   for (int i = 0; i < 3; ++i) { n     for (int j = 1; j < len[i] + 1; ++j) { n       H[i][j] = H[i][j - 1] * B + s[i][j]; n     } n   } n   int n = max(max(len[0], len[1]), len[2]); n   for (int i = 1; i < n + 1; ++i) { n     B_to[i] = B_to[i - 1] * B; n   } n } n inline ull hash_of(ull *h, int b, int l) { n   return h[b + l - 1] - h[b - 1] * B_to[l]; n } n bool contain(int a, int b) { n   if (len[a] < len[b]) return false; n   ull bh = H[b][len[b]]; n   for (int i = 1; i + len[b] <= len[a]; ++i) n     if (hash_of(H[a], i, len[b]) == bh) return true; n   return false; n } n int overlap(int a, int b) { n   int ans = 0; n   for (int i = 1; i <= min(len[a], len[b]); ++i) { n     if (hash_of(H[a], len[a] - i + 1, i) == hash_of(H[b], 1, i)) ans = i; n   } n   return ans; n } n int solve() { n   int ans = MAX_LENGTH * 3; n   for (int i = 0; i < 2; ++i) { n     for (int j = i + 1; j < 3; ++j) { n       if (contain(i, j) || contain(j, i)) { n         int x = len[i] > len[j] ? i : j; n         int y = 3 - i - j; n         if (contain(x, y) || contain(y, x)) return max(len[x], len[y]); n         return len[x] + len[y] - max(overlap(x, y), overlap(y, x)); n       } n     } n   } n   do n     ans = min(ans, len[0] + len[1] + len[2] - overlap(id[0], id[1]) - n                        overlap(id[1], id[2])); n   while (next_permutation(id, id + 3)); n   return ans; n } n int main() { n   for (int i = 0; i < 3; ++i) { n     gets(s[i] + 1); n     len[i] = strlen(s[i] + 1); n   } n   init_hash(); n   printf( %d n , solve()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int gcd(long long int a, long long int b) { n   if (a < b) swap(a, b); n   if (b == 0) return a; n   while (b) { n     long long int re = a % b; n     a = b; n     b = re; n   } n   return a; n } n vector<int> a[1000 + 10], b[1000 + 10]; n vector<int> c1(1000 + 10, 0), c2(1000 + 10, 0); n void dfs(int i, int k, int p) { n   if (p) n     c1[i] = k; n   else n     c2[i] = k; n   if (p) { n     for (int j : a[i]) { n       if (c1[j] == 0) dfs(j, k, p); n     } n   } else { n     for (int j : b[i]) { n       if (c2[j] == 0) dfs(j, k, p); n     } n   } n } n int main() { n   int n, m1, m2; n   cin >> n >> m1 >> m2; n   for (int i = 0; i < m1; i++) { n     int u, v; n     cin >> u >> v; n     a[u].push_back(v); n     a[v].push_back(u); n   } n   for (int i = 0; i < m2; i++) { n     int u, v; n     cin >> u >> v; n     b[u].push_back(v); n     b[v].push_back(u); n   } n   int k = 1; n   for (int i = 1; i < n + 1; i++) { n     if (c1[i] == 0) { n       dfs(i, k, 1); n       k++; n     } n   } n   k = 1; n   vector<pair<long long int, long long int> > ans; n   for (int i = 1; i < n + 1; i++) { n     if (c2[i] == 0) { n       dfs(i, k, 0); n       k++; n     } n   } n   map<int, int> mp1, mp2; n   for (int i = 0; i < n + 1; i++) { n     mp1[i] = i; n     mp2[i] = i; n   } n   for (int i = 1; i < n + 1; i++) { n     for (int j = 1; j < n + 1; j++) { n       int cai = mp1[c1[i]], caj = mp1[c1[j]], cbi = mp2[c2[i]], n           cbj = mp2[c2[j]]; n       if (cai != caj && cbi != cbj) { n         ans.push_back({i, j}); n         mp1[c1[j]] = mp1[c1[i]] = -1; n         mp2[c2[j]] = mp2[c2[i]] = -1; n       } n     } n   } n   cout << ans.size() << endl; n   for (auto it : ans) cout << it.first <<     << it.second << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n int val[maxn]; n int sito[maxn]; n int main() { n   ios::sync_with_stdio(false); n   for (int i = 0; i < maxn; i++) { n     sito[i] = true; n   } n   vector<int> v; n   sito[0] = sito[1] = false; n   for (int i = 2; i < maxn; i++) { n     if (sito[i]) { n       v.push_back(i); n       if (i < 1000) { n         for (int j = i * i; j < maxn; j += i) { n           sito[j] = false; n         } n       } n     } n   } n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> val[i]; n     val[i]--; n   } n   vector<pair<int, int> > ss; n   for (int i = 0; i < n;) { n     int pos = i; n     if (pos == val[pos]) { n       i++; n       continue; n     } n     while (pos != val[pos]) { n       int l = 0, r = v.size() - 1; n       int best = 0; n       while (l <= r) { n         int mid = (l + r) / 2; n         if (pos + v[mid] - 1 <= val[pos]) { n           l = mid + 1; n           best = max(best, v[mid] - 1); n         } else { n           r = mid - 1; n         } n       } n       ss.push_back(make_pair(pos + 1, pos + best + 1)); n       swap(val[pos], val[pos + best]); n       pos += best; n     } n   } n   cout << ss.size() << endl; n   for (int i = 0; i < ss.size(); i++) { n     cout << ss[i].first <<     << ss[i].second <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long sqr(long long x) { return x * x; } n bool done = false; n int p[8][3]; n bool same(double x, double y) { return abs(x - y) < 1e-9; } n int cnt = 0; n void backtr(int idx) { n   if (done) return; n   if (idx == 7) { n     cnt++; n     map<long long, int> ck; n     vector<long long> t(28), v(3, 1); n     int idx = 0, idx2 = 0; n     long long l1 = -1, l2 = -1, l3 = -1; n     for (int i = 0; i < 8; ++i) n       for (int j = i + 1; j < 8; ++j) { n         t[idx++] = sqr(p[i][0] - p[j][0]) + sqr(p[i][1] - p[j][1]) + n                    sqr(p[i][2] - p[j][2]); n         if (!ck.count(t[idx - 1])) { n           if (idx2 == 3) return; n           v[idx2++] = t[idx - 1], ck[t[idx - 1]] = 1; n         } else n           ck[t[idx - 1]]++; n       } n     sort(v.begin(), v.end()); n     if (ck[v[0]] == 12 && ck[v[1]] == 12 && ck[v[2]] == 4 && 2 * v[0] == v[1] && n         v[0] + v[1] == v[2]) { n       done = true; n       printf( YES n ); n       for (int i = 0; i < 8; ++i) n         printf( %d %d %d n , p[i][0], p[i][1], p[i][2]); n     } n   } else { n     do { n       backtr(idx + 1); n       if (done) return; n     } while (next_permutation(p[idx], p[idx] + 3)); n     sort(p[idx], p[idx] + 3); n   } n } n int main() { n   clock_t start_time, end_time; n   start_time = clock(); n   for (int i = 0; i < 8; ++i) n     scanf( %d%d%d , &p[i][0], &p[i][1], &p[i][2]), sort(p[i], p[i] + 3); n   backtr(0); n   if (!done) printf( NO ); n   end_time = clock(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 30; n bool vis[N]; n int id1 = -1, id2 = -1; n char ch; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   string s; n   cin >> s; n   int n = 27; n   for (int i = 0; i < 27; i++) { n     if (vis[s[i] -  A ]) { n       id2 = i; n       ch = s[i]; n     } else { n       vis[s[i] -  A ] = true; n     } n   } n   for (int i = 0; i < 27; i++) { n     if (s[i] == ch) { n       id1 = i; n       break; n     } n   } n   if (id2 == id1 + 1) { n     cout <<  Impossible n ; n     return 0; n   } n   if ((id2 - id1 - 1) % 2 == 1) { n     int cnt = id2 - id1 - 1; n     int rast = cnt / 2, chap = n - id2 - 1; n     if (rast + chap + 1 <= 13) { n       int rem = 13 - rast - chap - 1; n       for (int i = rem; i < id1; i++) cout << s[i]; n       for (int i = id1 + 1; i <= id1 + cnt / 2 + 1; i++) cout << s[i]; n       cout <<   n ; n       for (int i = rem - 1; i >= 0; i--) cout << s[i]; n       for (int i = n - 1; i > id2; i--) cout << s[i]; n       cout << ch; n       for (int i = id2 - 1; i >= id2 - cnt / 2; i--) cout << s[i]; n       cout <<   n ; n     } else { n       int rem = rast + chap + 1 - 13; n       for (int i = 0; i < rem; i++) cout << s[n - rem + i]; n       for (int i = 0; i < id1; i++) cout << s[i]; n       for (int i = id1 + 1; i <= id1 + cnt / 2 + 1; i++) cout << s[i]; n       cout <<   n ; n       for (int i = n - rem - 1; i > id2; i--) cout << s[i]; n       cout << ch; n       for (int i = id2 - 1; i >= id2 - cnt / 2; i--) cout << s[i]; n     } n   } else { n     int cnt = id2 - id1 - 1; n     int rast = cnt / 2, chap = n - id2 - 1; n     if (rast + chap + 1 <= 13) { n       int rem = 13 - rast - chap - 1; n       for (int i = rem; i < id1; i++) cout << s[i]; n       for (int i = id1 + 1; i <= id1 + cnt / 2; i++) cout << s[i]; n       cout <<   n ; n       for (int i = rem - 1; i >= 0; i--) cout << s[i]; n       for (int i = n - 1; i > id2; i--) cout << s[i]; n       cout << ch; n       for (int i = id2 - 1; i >= id2 - cnt / 2; i--) cout << s[i]; n       cout <<   n ; n     } else { n       int rem = rast + chap + 1 - 13; n       for (int i = 0; i < rem; i++) cout << s[n - rem + i]; n       for (int i = 0; i < id1; i++) cout << s[i]; n       for (int i = id1 + 1; i <= id1 + cnt / 2; i++) cout << s[i]; n       cout <<   n ; n       for (int i = n - rem - 1; i > id2; i--) cout << s[i]; n       cout << ch; n       for (int i = id2 - 1; i >= id2 - cnt / 2; i--) cout << s[i]; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 4; n void solve() { n   int n; n   cin >> n; n   int arr[n]; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   float min = 4.5 * n; n   int mn = (int)min; n   if (min - mn > 0) mn++; n   int sum = 0; n   for (int i = 0; i < n; i++) sum += arr[i]; n   int req = mn - sum; n   if (req <= 0) { n     cout << 0 << endl; n     return; n   } n   sort(arr, arr + n); n   int ans = 0, i = 0; n   while (req > 0) { n     req -= 5 - arr[i]; n     i++; n     ans++; n   } n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, i; n   scanf( %d%d , &n, &k); n   char s[n]; n   scanf( %s , s); n   for (i = 0; i < n; ++i) { n     if (s[i] ==  G ) { n       for (int j = i + k; j < n; j += k) { n         if (s[j] ==  # ) { n           printf( NO n ); n           return 0; n         } else if (s[j] ==  T ) { n           printf( YES n ); n           return 0; n         } n       } n       printf( NO n ); n       return 0; n     } else if (s[i] ==  T ) { n       for (int j = i + k; j < n; j += k) { n         if (s[j] ==  # ) { n           printf( NO n ); n           return 0; n         } else if (s[j] ==  G ) { n           printf( YES n ); n           return 0; n         } n       } n       printf( NO n ); n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n const int S = 550; n int n, q, a[N]; n struct Block { n   int lid, rid; n   deque<int> que; n   int mp[N]; n   Block() {} n   void init() { n     lid = rid = 0; n     que.clear(); n     memset(mp, 0, sizeof mp); n   } n   void addf(int x) { n     que.push_front(x); n     mp[x]++; n   } n   void addb(int x) { n     que.push_back(x); n     mp[x]++; n   } n   void delf() { n     if (que.empty()) return; n     int x = que.front(); n     que.pop_front(); n     mp[x]--; n   } n   void delb() { n     if (que.empty()) return; n     int x = que.back(); n     que.pop_back(); n     mp[x]--; n   } n } all[S]; n int tot; n void init() { n   for (int i = 0; i < S; ++i) all[i].init(); n   int id = 1; n   tot = 0; n   for (int i = 0; id <= n; ++i) { n     tot++; n     all[i].lid = all[i].rid = id; n     for (int j = 1; j <= S && id <= n; ++j) { n       all[i].addb(a[id]); n       all[i].rid++; n       id++; n     } n   } n   for (int i = 0; i < tot; ++i) all[i].rid--; n } n int st[N], top; n void mov(int l, int r) { n   int sx = 0, tx = 0; n   for (int i = 0; i < tot; ++i) { n     if (all[i].lid <= l && l <= all[i].rid) { n       sx = i; n     } n     if (all[i].lid <= r && r <= all[i].rid) { n       tx = i; n     } n   } n   for (int i = sx + 1; i <= tx; ++i) { n     all[i].addf(all[i - 1].que.back()); n     all[i - 1].delb(); n   } n   int val = 0; n   top = 0; n   for (int i = all[tx].rid; i > r; --i) { n     st[top++] = all[tx].que.back(); n     all[tx].delb(); n   } n   val = all[tx].que.back(); n   all[tx].delb(); n   while (top > 0) all[tx].addb(st[--top]); n   for (int i = all[sx].lid; i < l; ++i) { n     st[top++] = all[sx].que.front(); n     all[sx].delf(); n   } n   all[sx].addf(val); n   while (top > 0) all[sx].addf(st[--top]); n } n int cal(int ip, int l, int r, int k) { n   int res = 0; n   top = 0; n   for (int i = 1; i <= l - 1; ++i) { n     st[top++] = all[ip].que.front(); n     all[ip].delf(); n   } n   int alx = all[ip].rid - all[ip].lid + 1; n   for (int i = 1; i <= alx - r; ++i) { n     st[top++] = all[ip].que.back(); n     all[ip].delb(); n   } n   res = all[ip].mp[k]; n   for (int i = 1; i <= alx - r; ++i) { n     all[ip].addb(st[--top]); n   } n   for (int i = 1; i <= l - 1; ++i) { n     all[ip].addf(st[--top]); n   } n   return res; n } n int qry(int l, int r, int k) { n   int res = 0; n   for (int i = 0; i < tot; ++i) { n     if (all[i].lid <= l && r <= all[i].rid) n       return cal(i, l - all[i].lid + 1, r - all[i].lid + 1, k); n     if (l <= all[i].lid && all[i].rid <= r) res += all[i].mp[k]; n     if (all[i].lid < l && l <= all[i].rid) { n       res += cal(i, l - all[i].lid + 1, all[i].rid - all[i].lid + 1, k); n     } n     if (all[i].lid <= r && all[i].rid > r) { n       res += cal(i, 1, r - all[i].lid + 1, k); n     } n   } n   return res; n } n int scan() { n   int res = 0; n   char ch; n   while ((ch = getchar()) && (ch !=     && ch !=   n )) n     res = res * 10 + ch -  0 ; n   return res; n } n int Run() { n   n = scan(); n   for (int i = 1; i <= n; ++i) a[i] = scan(); n   init(); n   int lans = 0; n   q = scan(); n   while (q--) { n     int op, l, r; n     op = scan(), l = scan(), r = scan(); n     l = (l + lans - 1) % n + 1; n     r = (r + lans - 1) % n + 1; n     if (l > r) swap(l, r); n     if (op == 1) { n       mov(l, r); n     } else { n       int k; n       k = scan(); n       k = (k + lans - 1) % n + 1; n       lans = qry(l, r, k); n       printf( %d n , lans); n     } n   } n   return 0; n } n int main() { return Run(); } n


#include <bits/stdc++.h> n using namespace std; n int n, f, r[4]; n char names[4][16] = { Gryffindor ,  Hufflepuff ,  Ravenclaw ,  Slytherin }; n int main() { n   scanf( %d , &n); n   vector<vector<int> > v; n   v.push_back(vector<int>(4)); n   for (int k = 0; k < (int)(n); k++) { n     char op; n     scanf(  %c , &op); n     ((void)0); n     if (op !=  ? ) { n       for (int i = 0; i < (int)(4); i++) n         if (op == names[i][0]) { n           for (int j = 0; j < (int)(v.size()); j++) v[j][i]++; n         } n     } else { n       vector<vector<int> > nv; n       for (int j = 0; j < (int)(v.size()); j++) { n         int mx = 1 << 30; n         for (int i = 0; i < (int)(4); i++) mx = min(mx, v[j][i]); n         for (int i = 0; i < (int)(4); i++) n           if (v[j][i] == mx) { n             nv.push_back(v[j]); n             nv.back()[i]++; n           } n       } n       sort(nv.begin(), nv.end()); n       nv.erase(unique(nv.begin(), nv.end()), nv.end()); n       v = nv; n     } n   } n   for (int i = 0; i < (int)(v.size()); i++) { n     int mx = 1 << 30; n     for (int j = 0; j < (int)(4); j++) mx = min(mx, v[i][j]); n     for (int j = 0; j < (int)(4); j++) n       if (v[i][j] == mx) r[j] = 1; n   } n   for (int i = 0; i < (int)(4); i++) n     if (r[i]) puts(names[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, L; n char s[100005]; n int main() { n   scanf( %s , &s); n   L = strlen(s); n   for (i = 0; i < L; i++) printf( %c , s[i]); n   for (i = L - 1; i >= 0; i--) printf( %c , s[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int N; n vector<int> adj[200005]; n int dfs(int cur, int prv, bool valid) { n   int cnt = valid; n   for (int nxt : adj[cur]) n     if (nxt != prv) cnt += dfs(nxt, cur, !valid); n   return cnt; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> N; n   for (int i = 0, a, b; i < N - 1; i++) { n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   int cnt = dfs(1, 0, true); n   cout << min(cnt, N - cnt) - 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, string> m; n int main() { n   long long int n, mm, i; n   string s, b, s1, b1; n   cin >> n >> mm; n   for (i = 0; i < n; i++) { n     cin >> s >> b; n     m[b] = s; n   } n   char c; n   for (i = 0; i < mm; i++) { n     cin >> s1 >> b1; n     int l = b1.length(); n     cout << s1 <<     << b1.substr(0, l - 1) <<  ; #  << m[b1.substr(0, l - 1)] n          <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000010; n long long n, m, s, l; n vector<pair<long long, long long> > Adj[N]; n long long Dis[N], Ans; n set<pair<long long, long long> > Res; n vector<pair<long long, pair<long long, long long> > > Edges; n void Dik() { n   for (int i = 0; i <= n; i++) Dis[i] = 1e18; n   Dis[s] = 0; n   set<pair<long long, long long> > S; n   S.insert({0, s}); n   while (!S.empty()) { n     auto Cur = *S.begin(); n     S.erase(S.begin()); n     for (int i = 0; i < Adj[Cur.second].size(); i++) { n       int to = Adj[Cur.second][i].first; n       long long w = Adj[Cur.second][i].second; n       if (Dis[to] > Cur.first + w) { n         Dis[to] = Cur.first + w; n         S.insert({Dis[to], to}); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> s; n   for (int i = 0; i < m; i++) { n     int a, b, w; n     cin >> a >> b >> w; n     Edges.push_back({w, {a, b}}); n     Adj[a].push_back({b, w}); n     Adj[b].push_back({a, w}); n   } n   cin >> l; n   if (l == 0) { n     cout << 1 << endl; n     return 0; n   } n   Dik(); n   for (int i = 1; i <= n; i++) n     if (Dis[i] == l) Ans++; n   for (int i = 0; i < Edges.size(); i++) { n     int a = Edges[i].second.first; n     int b = Edges[i].second.second; n     long long w = Edges[i].first; n     if (Dis[a] < l && Dis[a] + w > l && (w - (l - Dis[a])) + Dis[b] >= l) { n       Ans++; n     } n     if (Dis[b] < l && Dis[b] + w > l && (w - (l - Dis[b])) + Dis[a] >= l) { n       Ans++; n     } n     if (Dis[a] < l && Dis[a] + w > l && (w - (l - Dis[a])) + Dis[b] >= l) { n       if (Dis[b] < l && Dis[b] + w > l && (w - (l - Dis[b])) + Dis[a] >= l) { n         if (w - (l - Dis[a]) == (l - Dis[b])) Ans--; n       } n     } n   } n   cout << Ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, k, x, y; n     cin >> n >> k; n     if (n > k) { n       x = k * (k + 1); n       x = x / 2; n       cout << x << endl; n     } else { n       y = n - 1; n       x = y * (y + 1); n       x = x / 2; n       cout << x + 1 << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPSILON = 1e-9; n const double PI = acos(0.0) * 2; n const int MOD = 1000000007; n const int INF = 0x3c3c3c3c; n const long long INFL = 0x3c3c3c3c3c3c3c3c; n const int MAX_N = 102; n long long S, X; n long long solve(long long sum, int pos) { n   if (sum > S) return 0; n   if (pos < 0) return sum == S; n   long long ret = 0; n   if (X & (1ll << pos)) { n     if ((1ll << pos) + sum <= S) ret += 2 * solve(sum + (1ll << pos), pos - 1); n   } else { n     if ((1ll << (pos + 1)) + sum <= S) n       ret += solve(sum + 2 * (1ll << pos), pos - 1); n     if (S - sum <= 2 * ((1ll << pos) - 1)) ret += solve(sum, pos - 1); n   } n   return ret; n } n int main() { n   scanf( %lld%lld , &S, &X); n   cout << solve(0, 60) - (X == S ? 2 : 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n = 0; n   long long q, p = 1; n   cin >> q; n   for (int i = 0; i < 2; i++) { n     for (long long s = 2; s * s <= q; s++) { n       if (q % s == 0) { n         n++; n         p *= s; n         q /= s; n         break; n       } n     } n   } n   if (n == 2 && q > 1 || n == 0) { n     cout << 1 << endl; n     cout << (n == 0 ? 0 : p) << endl; n   } else { n     cout << 2 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 212345; n int seg[4 * N], v[N], tin[N], tout[N], ans[N]; n int a, b, n, q; n void init(int r, int i, int j) { n   if (i == j) { n     seg[r] = v[i]; n   } else { n     init(2 * r, i, (i + j) / 2); n     init(2 * r + 1, (i + j) / 2 + 1, j); n     seg[r] = min(seg[2 * r], seg[2 * r + 1]); n   } n } n int query(int r, int i, int j) { n   if (b < i || a > j) return 0x3f3f3f3f; n   if (i >= a && j <= b) { n     return seg[r]; n   } else { n     int L = query(2 * r, i, (i + j) / 2); n     int R = query(2 * r + 1, (i + j) / 2 + 1, j); n     return min(L, R); n   } n } n void update(int r, int i, int j, int pos, int val) { n   if (pos < i || pos > j) return; n   if (i == j) { n     seg[r] = val; n   } else { n     update(2 * r, i, (i + j) / 2, pos, val); n     update(2 * r + 1, (i + j) / 2 + 1, j, pos, val); n     seg[r] = min(seg[2 * r], seg[2 * r + 1]); n   } n } n set<int> zeros; n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> q; n   int mx = 0; n   for (int i = 1; i <= n; i++) { n     cin >> v[i]; n     if (v[i] == 0) { n       zeros.emplace(i); n       v[i] = 0x3f3f3f3f; n     } else { n       if (tin[v[i]] == 0) tin[v[i]] = i; n       tout[v[i]] = i; n       mx = max(mx, v[i]); n     } n   } n   if (zeros.size() == n) { n     cout <<  YES  << endl; n     for (int i = 1; i <= n; i++) cout << q <<    ; n     cout << endl; n     return 0; n   } n   if (mx != q and zeros.size() == 0) { n     cout <<  NO  << endl; n     return 0; n   } n   init(1, 1, n); n   if (mx != q and zeros.size()) { n     auto it = zeros.begin(); n     update(1, 1, n, *it, q); n     zeros.erase(it); n   } n   for (int i = q; i >= 1; i--) { n     if (tin[i] == 0) continue; n     a = tin[i]; n     b = tout[i]; n     if (query(1, 1, n) < i) { n       cout <<  NO  << endl; n       return 0; n     } n     auto it = zeros.lower_bound(tin[i]); n     while (it != zeros.end() and *it <= tout[i]) { n       update(1, 1, n, *it, i); n       it = zeros.erase(it); n     } n   } n   for (int i = 1; i <= n; i++) { n     a = i; n     b = i; n     ans[i] = query(1, 1, n); n   } n   for (int i = 2; i <= n; i++) { n     if (ans[i] == 0x3f3f3f3f and ans[i - 1] != 0x3f3f3f3f) ans[i] = ans[i - 1]; n   } n   for (int i = n - 1; i >= 1; i--) { n     if (ans[i] == 0x3f3f3f3f and ans[i + 1] != 0x3f3f3f3f) ans[i] = ans[i + 1]; n   } n   cout <<  YES  << endl; n   for (int i = 1; i <= n; i++) { n     assert(ans[i] != 0x3f3f3f3f); n     cout << ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int th = 0; n   for (int i = 1; i < n; i++) { n     th += i; n     th %= n; n     cout << (th + 1) <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e6 + 7; n const long long mod = 1e9 + 7; n const long long INF = 3e18 + 315; n vector<pair<long long, long long> > va, vb; n long long n, s, ans, na, nb, ta, tb; n int32_t main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> s; n   for (long long i = 0; i < n; i++) { n     long long num, a, b; n     cin >> num >> a >> b; n     if (a > b) { n       ans += num * a; n       na += num; n       va.push_back({a - b, num}); n     } else { n       ans += num * b; n       nb += num; n       vb.push_back({b - a, num}); n     } n   } n   na %= s; n   nb %= s; n   sort(va.begin(), va.end()); n   sort(vb.begin(), vb.end()); n   if (na + nb > s) return cout << ans << endl, 0; n   for (long long i = 0; i < ((long long)(va).size()); i++) { n     ta += min(na, va[i].second) * va[i].first; n     na -= min(na, va[i].second); n   } n   for (long long i = 0; i < ((long long)(vb).size()); i++) { n     tb += min(nb, vb[i].second) * vb[i].first; n     nb -= min(nb, vb[i].second); n   } n   ans -= min(ta, tb); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 10001; n void solve() {} n int main() { n   int a, b, c; n   while (cin >> a >> b >> c) { n     int t = a * c; n     for (int i = 0; i <= 1005 * 1005; i++) { n       if (b * i + c * b >= a * c) { n         cout << i << endl; n         break; n       } n     } n   } n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   int arr[200007]; n   map<int, bool> vis; n   queue<int> q; n   for (int i = 1; i <= n; i++) { n     cin >> arr[i]; n     if (vis[arr[i]]) continue; n     vis[arr[i]] = true; n     if (q.size() == k) { n       vis[q.front()] = false; n       q.pop(); n     } n     q.push(arr[i]); n   } n   vector<int> v; n   while (!q.empty()) { n     v.push_back(q.front()); n     q.pop(); n   } n   cout << (int)v.size() << endl; n   for (int i = (int)v.size() - 1; i >= 0; i--) { n     cout << v[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n int n, m, Q, a[300006][2], fa[300006], dep[300006], siz[300006], b[300006], D; n bool vis[300006]; n map<int, int> mp[300006]; n vector<int> v[300006], s[300006]; n const double pi = acos(-1.0); n void df5(int te, int la) { n   vis[te] = 1; n   siz[te] = 1; n   for (int i = 0; i < v[te].size(); i++) { n     int j = v[te][i]; n     if (j == la) continue; n     if (vis[j]) continue; n     dep[j] = dep[te] + 1; n     fa[j] = te; n     df5(j, te); n     siz[te] += siz[j]; n   } n } n inline double getde(int x, int y) { n   double ret = atan2(1.0 * a[y][1] - a[x][1], 1.0 * a[y][0] - a[x][0]); n   if (ret < 0) ret += 2.0 * pi; n   return ret; n } n inline bool cmp(int x, int y) { return getde(D, x) < getde(D, y); } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     v[x].push_back(y), v[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) scanf( %d%d , &a[i][0], &a[i][1]); n   df5(1, 1); n   for (int i = 1; i <= n; i++) { n     D = i; n     sort(v[i].begin(), v[i].end(), cmp); n     for (int j = 0; j < v[i].size(); j++) mp[i][v[i][j]] = j; n     if (!v[i].empty()) { n       if (v[i][0] == fa[i]) n         s[i].push_back(-siz[i]); n       else if (fa[v[i][0]] == i) n         s[i].push_back(siz[v[i][0]]); n       else n         s[i].push_back(0); n     } n     for (int j = 1; j < v[i].size(); j++) { n       int x = v[i][j], la = s[i][j - 1]; n       if (x == fa[i]) n         s[i].push_back(la - siz[i]); n       else if (fa[x] == i) n         s[i].push_back(la + siz[x]); n       else n         s[i].push_back(la); n     } n   } n   scanf( %d , &Q); n   while (Q--) { n     int k; n     scanf( %d , &k); n     bool hv1 = false; n     for (int i = 1; i <= k; i++) scanf( %d , &b[i]); n     for (int i = 1; i <= k; i++) n       if (b[i] == 1) { n         hv1 = true; n         break; n       } n     long long sum = 0; n     bool flag = false; n     for (int i = 1; i <= k; i++) { n       int ii = i % k + 1; n       sum += (1ll * (a[b[i]][0] - a[b[ii]][0]) * a[b[i]][1] - n               1ll * a[b[i]][0] * (a[b[i]][1] - a[b[ii]][1])); n     } n     if (sum > 0) flag = true; n     long long ans = 0; n     if (!flag) { n       for (int i = 2, j = k; i < j; i++, j--) swap(b[i], b[j]); n     } n     for (int i = 1; i <= k; i++) { n       int la = i - 1; n       if (!la) la = k; n       int nx = i % k + 1; n       int ts1 = mp[b[i]][b[la]], ts2 = mp[b[i]][b[nx]], n           tt = ((int)v[b[i]].size()) - 1; n       if (ts1 >= ts2) { n         if (ts1) ans += s[b[i]][ts1]; n         if (ts2) ans -= s[b[i]][ts2 - 1]; n       } else { n         ans += s[b[i]][ts1]; n         if (ts2) ans += s[b[i]][tt] - s[b[i]][ts2 - 1]; n       } n     } n     ans += k; n     if (ans <= 0) ans += n; n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n int main() { n   using namespace std; n   int c, m, i; n   cin >> c >> m; n   int a = 0; n   for (i = 1; i <= 9; i++) { n     if (i * c % 10 == 0) { n       cout << i; n       break; n     } n     if (c == m) { n       cout << i; n       break; n     } n     if (i * c % 10 == m) { n       cout << i; n       break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mx = 105; n const long long mod = 998244353; n vector<vector<int>> adj; n void solve() { n   long long n, i, j, k; n   cin >> n; n   long long a[n]; n   for (i = 0; i < n; i++) cin >> a[i]; n   long long res = 0, su = 0; n   for (i = 0; i < n; i++) { n     su += a[i]; n   } n   long long mm = su / n; n   for (i = 0; i < n; i++) { n     res += abs(a[i] - mm); n     a[i + 1] += a[i] - mm; n   } n   cout << res <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b; n   scanf( %I64d %I64d , &a, &b); n   long long t = 0; n   while (b) { n     t *= 10; n     t += b % 10; n     b /= 10; n   } n   printf( %I64d n , a + t); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = acos(-1); n const long long int mod = 1000000007; n const double epsilon = 1e-9; n template <class T> n ostream &operator<<(ostream &out, vector<T> &A) { n   for (auto x : A) out << x <<    ; n   return out; n } n template <class T> n ostream &operator<<(ostream &out, set<T> &A) { n   for (auto x : A) out << x <<    ; n   return out; n } n template <class T> n ostream &operator<<(ostream &out, unordered_set<T> &A) { n   for (auto x : A) out << x <<    ; n   return out; n } n template <class T1, class T2> n T1 powr(T1 a, T2 b) { n   T1 res = 1; n   for (long long int i = 1; i < b + 1; i++) res = res * a; n   return res; n } n long long int binpow(long long int a, long long int b) { n   a = a % mod; n   long long int res = 1; n   while (b > 0) { n     if (b & 1) res = (res * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return (res + mod) % mod; n } n const long long int N = 10000025; n long long int prime[10000000]; n void SieveOfEratosthenes() { n   for (long long int i = 0; i < N; i++) prime[i] = 1; n   prime[0] = 0; n   prime[1] = 0; n   for (long long int p = 2; p * p <= 10000000; p++) { n     if (prime[p] == 1) { n       for (long long int i = p * p; i <= 10000000; i += p) prime[i] = 0; n     } n   } n } n int32_t main() { n   std::ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int t; n   cin >> t; n   while (t--) { n     long long int n; n     cin >> n; n     long long int sum = 0, cnt = 0; n     for (long long int i = 1; i < 100000; i++) { n       long long int x = pow(2, i) - 1; n       if (n - (x * (x + 1)) / 2 >= 0) { n         n -= (x * (x + 1) / 2); n         cnt++; n       } else { n         break; n       } n     } n     cout << cnt <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int d, n, m, b[200005]; n int mn[200005 << 2]; n long long ans; n struct node { n   int x; n   long long p; n   bool operator<(const node &a) const { return x < a.x; } n } a[200005]; n int check(int x, int y) { return a[x].p < a[y].p ? x : y; } n void build(int l, int r, int rt) { n   if (l == r) { n     mn[rt] = l; n     return; n   } n   int mid = (l + r) >> 1; n   build(l, mid, rt << 1); n   build(mid + 1, r, rt << 1 | 1); n   mn[rt] = check(mn[rt << 1], mn[rt << 1 | 1]); n } n int get_mn(int l, int r, int rt, int L, int R) { n   if (L == l && R == r) return mn[rt]; n   int mid = (l + r) >> 1; n   if (R <= mid) n     return get_mn(l, mid, rt << 1, L, R); n   else if (L > mid) n     return get_mn(mid + 1, r, rt << 1 | 1, L, R); n   else n     return check(get_mn(l, mid, rt << 1, L, mid), n                  get_mn(mid + 1, r, rt << 1 | 1, mid + 1, R)); n } n int main() { n   cin >> d >> n >> m; n   for (int i = 1; i <= m; i++) scanf( %d%I64d , &a[i].x, &a[i].p); n   sort(a + 1, a + m + 1); n   a[++m].x = d; n   a[0].p = 0x3f3f3f3f; n   build(1, m, 1); n   for (int i = 1; i <= m; i++) b[i] = a[i].x; n   int now = n, i = 0, j = 1, t, k; n   while (i < m) { n     t = upper_bound(b + 1, b + m + 1, a[i].x + n) - b - 1; n     if (t == i) { n       puts( -1 ); n       return 0; n     } n     t = upper_bound(b + 1, b + m + 1, a[i].x + now) - b - 1; n     k = (t > i ? get_mn(1, m, 1, i + 1, t) : 0); n     if (a[k].p <= a[i].p) { n       now -= (a[k].x - a[i].x); n       i = k; n     } else { n       t++; n       while (t <= m && a[t].x - a[i].x <= n) { n         if (a[t].p <= a[i].p) { n           ans = (ans + (a[t].x - a[i].x - now) * a[i].p); n           now = 0; n           i = t; n           break; n         } n         t++; n       } n       if (a[t].x - a[i].x > n) { n         t = upper_bound(b + 1, b + m + 1, a[i].x + n) - b - 1; n         k = get_mn(1, m, 1, i + 1, t); n         ans = (ans + (n - now) * a[i].p); n         now = (n - (a[k].x - a[i].x)); n         i = k; n       } n     } n   } n   cout << ans << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long N = 1e5 + 1; n const long long INF = -1e17; n long long n; n long long ans, cnt, mx = 0; n string s; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n; n   getline(cin, s); n   getline(cin, s); n   for (long long i = 0; i < s.size(); i++) { n     if (s[i] > 64 and s[i] < 95) { n       ans++; n       mx = max(ans, mx); n     } else if (s[i] ==    ) { n       mx = max(ans, mx); n       ans = 0; n     } n   } n   cout << mx; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000010; n vector<int> freqs; n int t, n, k, a[N], f[N], vis[N]; n int main() { n   cin >> t; n   while (t--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; ++i) { n       f[i] = 0, vis[i] = 0; n     } n     for (int i = 1; i <= n; ++i) { n       scanf( %d , a + i); n       ++f[a[i]]; n     } n     int minFreq = n + n; n     for (int i = 1; i <= n; ++i) { n       if (f[i]) ++vis[f[i]], minFreq = min(minFreq, f[i]); n     } n     freqs.clear(); n     for (int i = 1; i <= n; ++i) n       if (vis[i]) { n         freqs.emplace_back(i); n       } n     int ans = n; n     for (int s = 1; s <= minFreq; ++s) { n       int bad = 0, m = 0; n       for (int it : freqs) { n         int x = (it + s) / (s + 1); n         if (x * s > it) { n           bad = 1; n           break; n         } n         m += vis[it] * x; n       } n       if (bad) continue; n       ans = min(ans, m); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const double e = 1e-6; n int main() { n   int T; n   double n; n   scanf( %d , &T); n   while (T--) { n     scanf( %lf , &n); n     double a = 0, b = n; n     bool flag = false; n     for (int i = 0; i < 10000; i++) { n       double m = (a + b) / 2, d = n - m; n       if (m * d - m - d > 1e-9) n         b = m; n       else if (m + d - d * m > 1e-9) n         a = m; n     } n     double m = (a + b) / 2, d = n - m; n     if (m + d - n <= e && m + d - n >= -e && (m + d) - m * d <= e && n         (m + d) - m * d >= -e && m * d - n <= e && m * d - n >= -e) n       printf( Y %.9lf %.9lf n , d, m); n     else n       puts( N ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, a[444], s, p, q, ans; n int main() { n   scanf( %d , &n); n   ans = -1; n   for (int i = 0; i < n; ++i) scanf( %d , a + i); n   for (int i = 1; i < n; ++i) n     for (int j = 0; j < n; ++j) { n       p = a[i - 1] - 1; n       for (int k = q = s = 0; k <= j;) n         q += p / a[k], s += p / a[k] * a[k], p %= a[k++]; n       ++q, p = s += a[j]; n       for (int k = 0; k < n; ++k) q -= p / a[k], p %= a[k]; n       if (q < 0) ans = (unsigned)ans < s ? ans : s; n     } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n int n, m, k; n set<int> a[maxn], b[maxn]; n set<int, greater<int> > c[maxn], d[maxn]; n int main() { n   scanf( %d%d%d , &n, &m, &k); n   int x, y, nx, ny, lx = 0, ly = 0, rx = n + 1, ry = m + 1, dir = 1; n   long long ans = 1; n   for (int i = 1; i <= k; i++) { n     scanf( %d%d , &x, &y); n     a[x].insert(y), c[x].insert(y); n     b[y].insert(x), d[y].insert(x); n   } n   x = 1, y = 1; n   for (int i = 1;; i++) { n     nx = x, ny = y; n     if (dir == 1) { n       auto t = a[x].lower_bound(y); n       ny = ((t == a[x].end()) ? ry : min(*t, ry)) - 1, lx = x; n     } n     if (dir == 2) { n       auto t = b[y].lower_bound(x); n       nx = ((t == b[y].end()) ? rx : min(*t, rx)) - 1, ry = y; n     } n     if (dir == 3) { n       auto t = c[x].lower_bound(y); n       ny = ((t == c[x].end()) ? ly : max(*t, ly)) + 1, rx = x; n     } n     if (dir == 4) { n       auto t = d[y].lower_bound(x); n       nx = ((t == d[y].end()) ? lx : max(*t, lx)) + 1, ly = y; n     } n     if (x == nx && y == ny && i > 1) break; n     ans += abs(nx - x) + abs(ny - y), x = nx, y = ny, n                                       dir = (dir < 4) ? dir + 1 : 1; n   } n   if (ans == (long long)n * m - k) n     printf( Yes n ); n   else n     printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n int cnt, n, res; n int tin[maxn], tout[maxn], a[maxn + maxn], dp[maxn], f[maxn][17], st[maxn], n     h[maxn], b[maxn]; n bool need[maxn]; n vector<int> adj[maxn]; n void Input() { n   int x, y; n   cin >> n; n   for (int i = (2); i <= (n); ++i) { n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n } n void DFS(int u) { n   tin[u] = ++cnt; n   for (int v : adj[u]) { n     if (h[v] != 0) continue; n     h[v] = h[u] + 1; n     f[v][0] = u; n     for (int i = (1); i <= (16); ++i) f[v][i] = f[f[v][i - 1]][i - 1]; n     DFS(v); n   } n   tout[u] = cnt; n } n int LCA(int u, int v) { n   if (h[u] < h[v]) swap(u, v); n   for (int i = (16); i >= (0); --i) n     if (h[u] - (1 << i) >= h[v]) u = f[u][i]; n   if (u == v) return u; n   for (int i = (16); i >= (0); --i) n     if (f[u][i] != f[v][i]) u = f[u][i], v = f[v][i]; n   return f[u][0]; n } n bool cmp(int u, int v) { return tin[u] < tin[v]; } n bool IsAnc(int u, int v) { return tin[u] < tin[v] && tout[v] <= tout[u]; } n bool canbuild(int k) { n   sort(a + 1, a + 1 + k, cmp); n   for (int i = (1); i <= (k - 1); ++i) a[k + i] = LCA(a[i], a[i + 1]); n   k = k + k - 1; n   sort(a + 1, a + 1 + k, cmp); n   k = unique(a + 1, a + 1 + k) - a - 1; n   for (int i = (1); i <= (k); ++i) adj[a[i]].clear(); n   int top = 1, u; n   st[1] = a[1]; n   for (int i = (2); i <= (k); ++i) { n     while (top >= 1 && !IsAnc(st[top], a[i])) --top; n     if (top == 0) { n       st[++top] = a[i]; n       continue; n     } n     u = st[top]; n     if (need[u] && need[a[i]] && f[a[i]][0] == u) return false; n     adj[u].push_back(a[i]); n     st[++top] = a[i]; n   } n   return true; n } n void Calc(int u) { n   int tmp = 0; n   for (int v : adj[u]) { n     Calc(v); n     tmp += dp[v]; n   } n   if (need[u]) { n     res += tmp; n     dp[u] = 1; n   } else { n     if (tmp >= 2) n       dp[u] = 0, ++res; n     else n       dp[u] = tmp; n   } n } n void Solve() { n   h[1] = 1; n   DFS(1); n   int q, k; n   cin >> q; n   for (int i = (1); i <= (q); ++i) { n     cin >> k; n     for (int j = (1); j <= (k); ++j) { n       cin >> a[j]; n       b[j] = a[j]; n       need[a[j]] = true; n     } n     if (!canbuild(k)) { n       cout << -1 <<   n ; n       for (int j = (1); j <= (k); ++j) need[b[j]] = false; n       continue; n     } n     res = 0; n     Calc(st[1]); n     cout << res <<   n ; n     for (int j = (1); j <= (k); ++j) need[b[j]] = false; n   } n } n int main() { n   iostream::sync_with_stdio(false); n   cin.tie(0); n   Input(); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n using maxpq = priority_queue<T, vector<T>, greater<T>>; n template <typename T> n using minpq = priority_queue<T>; n int n, TOT; n string a[305]; n int HASH[305], len[305], p[100005]; n inline int add(int a, int b) { n   long long c = a + b; n   if (c >= 1000000007) c -= 1000000007; n   return c; n } n inline int sub(int a, int b) { n   int c = a - b; n   if (c < 0) c += 1000000007; n   return c; n } n inline int mul(int a, int b) { return (a * 1ll * b) % 1000000007; } n int power(int a, int b, int m = 1000000007) { n   int res = 1; n   while (b > 0) { n     if (b & 1) res = mul(res, a); n     a = mul(a, a); n     b >>= 1; n   } n   return res; n } n inline int Fermat(int a, int p = 1000000007) { return power(a, p - 2, p); } n inline int Div(int a, int b) { return mul(a, Fermat(b)); } n int base = 128; n inline int h(char c) { return c; } n inline int hsh(string s) { n   int t = 0; n   for (int i = (0); i < (s.size()); i++) { n     t = mul(t, base); n     t = add(t, h(s[i])); n   } n   return t; n } n int ans = 0; n vector<pair<int, int>> adj[305]; n void pre() { n   p[0] = 1; n   for (int i = (1); i < (100005); i++) { n     p[i] = mul(p[i - 1], base); n   } n } n void solve() { n   for (int l = (1); l < (n + 1); l++) { n     for (int i = (0); i < (n - l + 1); i++) { n       int j = i + l - 1; n       int t = HASH[i], tot = len[i]; n       for (int k = (i + 1); k < (j + 1); k++) { n         t = mul(t, p[len[k] + 1]); n         t = add(t, mul(h(   ), p[len[k]])); n         t = add(t, HASH[k]); n         tot = tot + len[k] + 1; n       } n       adj[l].push_back({t, tot}); n     } n   } n   for (int l = (1); l < (n); l++) { n     for (int i = (0); i < (n - l + 1); i++) { n       bool b = 0; n       int tmp = TOT; n       for (int j = (i + l); j < (n - l + 1); j++) { n         if (adj[l][i].first != adj[l][j].first || n             adj[l][j].second != adj[l][j].second) n           continue; n         tmp += -adj[l][i].second + l; n         if (b == 0) b = 1, tmp += -adj[l][i].second + l; n         j += l - 1; n       } n       ans = min(ans, tmp); n     } n   } n } n int main() { n   clock_t clk = clock(); n   cerr <<  I will return... n ; n   cin >> n; n   for (int i = (0); i < (n); i++) n     cin >> a[i], len[i] = a[i].size(), TOT += len[i], HASH[i] = hsh(a[i]); n   TOT += n - 1; n   ans = TOT; n   pre(); n   solve(); n   cout << ans <<   n ; n   cerr <<  ...don t you ever hang your head. n ; n   cerr <<  Time (in ms):   << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC n        <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void __read(T& a) { n   cin >> a; n } n template <typename T, typename... Args> n void __read(T& a, Args&... args) { n   cin >> a; n   __read(args...); n } n constexpr long long M7 = 1000000007ll; n constexpr long long M9 = 1000000009ll; n constexpr long long MFFT = 998244353ll; n template <class T> n void outv(T& a) { n   for (auto& x : a) cout << x <<    ; n } n mt19937 rnd(static_cast<unsigned>( n     chrono::steady_clock::now().time_since_epoch().count())); n template <class T> n void random_shuffle(T s, T e) { n   shuffle(s, e, rnd); n }; n static auto __super_speed__ = (ios_base::sync_with_stdio(0), cin.tie(0)); n signed dp[30 + 1][30 + 1][51]; n signed calc(signed n, signed m, signed k) { n   signed& v = dp[n][m][k]; n   if (v != -1) return v; n   if (n * m == k || k == 0) return v = 0; n   v = M7; n   for (signed i = 1; i < n; ++i) n     for (signed nk = 0; nk < k; ++nk) n       v = min(v, calc(i, m, nk) + calc(n - i, m, k - nk) + m * m); n   for (signed i = 1; i < m; ++i) n     for (signed nk = 0; nk < k; ++nk) n       v = min(v, calc(n, i, nk) + calc(n, m - i, k - nk) + n * n); n   return v; n } n int32_t main() { n   memset(dp, -1, sizeof(dp)); n   signed t; n   __read(t); n   while (t--) { n     signed n, m, k; n     __read(n, m, k); n     cout << calc(n, m, k) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, test =  hello ; n   int c = 0; n   cin >> s; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] == test[c]) { n       c++; n     } n     if (c == 5) { n       break; n     } n   } n   if (c == 5) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class TAT> n void read(TAT &a) { n   static char cc; n   static bool fff; n   while (((cc = getchar()) <  0  || cc >  9 ) && cc !=  - ) n     ; n   if (cc ==  - ) n     fff = 1, a = 0; n   else n     fff = 0, a = cc - 48; n   while ((cc = getchar()) >=  0  && cc <=  9 ) a = a * 10 + cc - 48; n   if (fff) a = -a; n } n template <class TAT> n void write(TAT a) { n   static char cc[25]; n   static int ct; n   if (a == 0) { n     putchar( 0 ); n     return; n   } n   if (a < 0) { n     putchar( - ); n     a = -a; n   } n   ct = 0; n   while (a) { n     cc[++ct] = a % 10 + 48; n     a /= 10; n   } n   while (ct) { n     putchar(cc[ct--]); n   } n } n void begin() { n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n } n void end() { n   fclose(stdin); n   fclose(stdout); n } n template <class TAT> n void Ckmin(TAT &a, const TAT &b) { n   if (a > b) a = b; n } n template <class TAT> n void Ckmax(TAT &a, const TAT &b) { n   if (a < b) a = b; n } n const int mod = 1000000007; n const int maxn = 105; n const int maxm = 10005; n long long n, m, K; n long long C[maxn][maxn] = {0}; n long long F[maxn][maxm] = {0}; n long long P[maxn] = {0}; n void Init() { read(n), read(m), read(K); } n int Pow(long long x, long long y) { n   long long S = 1; n   while (y) { n     if (y & 1) S = (S * x) % mod; n     x = (x * x) % mod; n     y >>= 1; n   } n   return S; n } n void Prepare() { n   const int N = 101; n   register int i, j, k; n   C[0][0] = 1; n   for (i = 1; i <= N; ++i) { n     C[i][0] = 1; n     for (j = 1; j <= i; ++j) { n       C[i][j] = n           ((C[i][j - 1] * Pow(j, mod - 2)) % mod * (long long)(i - j + 1)) % n           mod; n     } n   } n } n void Solve() { n   register int i, j, k; n   memset(F, 0, sizeof(F)); n   F[0][0] = 1; n   for (i = 1; i <= n; ++i) { n     for (j = 0; j <= n; ++j) P[j] = Pow(C[n][j], (m + n - i) / n) % mod; n     for (j = 0; j <= K; ++j) { n       for (k = j; k <= K && k <= j + n; ++k) { n         F[i][k] = (F[i][k] + F[i - 1][j] * P[k - j]) % mod; n       } n     } n   } n   write(F[n][K]); n   putchar(  n ); n } n int main() { n   Init(); n   Prepare(); n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100009; n int main() { n   string s; n   cin >> s; n   int l = s.size(); n   for (int i = 0; i < l; i++) s += s[i]; n   int ans = 1; n   for (int i = 0; i < l;) { n     int j = i; n     for (; j < l + i - 1; j++) { n       if (s[j] == s[j + 1]) n         break; n       else n         ans = max(ans, j - i + 2); n     } n     i = j + 1; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string s; n   cin >> s; n   long long cnt = 0; n   for (long long i = 0; i < s.length(); i++) { n     if (s[i] >= 97) cnt++; n   } n   if (cnt >= s.length() - cnt) { n     transform(s.begin(), s.end(), s.begin(), ::tolower); n   } else n     transform(s.begin(), s.end(), s.begin(), ::toupper); n   cout << s << endl; n } n int main() { n   long long t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, s, l, r, res, cur, maxx = -9999999999; n int main() { n   cin >> n >> s; n   for (long long i = 1; i <= n; i++) { n     cin >> l >> r; n     maxx = max(maxx, l + r); n   } n   cout << max(maxx, s); n   return 0; n } n


#include <bits/stdc++.h> n struct MI { n  private: n   char bb[1 << 14]; n   FILE *f; n   char *bs, *be; n   char e; n   bool o, l; n  n  public: n   MI() : f(stdin), bs(0), be(0) {} n   inline char get() { n     if (o) { n       o = 0; n       return e; n     } n     if (bs == be) be = (bs = bb) + fread(bb, 1, sizeof(bb), f); n     if (bs == be) { n       l = 1; n       return -1; n     }; n     return *bs++; n   } n   inline void unget(char c) { n     o = 1; n     e = c; n   } n   template <class T> n   inline T read() { n     T r; n     *this > r; n     return r; n   } n   template <class T> n   inline MI &operator>(T &); n }; n template <class T> n struct Q { n   const static bool U = T(-1) >= T(0); n   inline void operator()(MI &t, T &r) const { n     r = 0; n     char c; n     bool y = 0; n     if (U) n       for (;;) { n         c = t.get(); n         if (c == -1) goto E; n         if (isdigit(c)) break; n       } n     else n       for (;;) { n         c = t.get(); n         if (c == -1) goto E; n         if (c ==  - ) { n           c = t.get(); n           if (isdigit(c)) { n             y = 1; n             break; n           }; n         } else if (isdigit(c)) n           break; n         ; n       }; n     for (;;) { n       if (c == -1) goto E; n       if (isdigit(c)) n         r = r * 10 + (c ^ 48); n       else n         break; n       c = t.get(); n     } n     t.unget(c); n   E:; n     if (y) r = -r; n   } n }; n template <> n struct Q<char> {}; n template <class T> n inline MI &MI::operator>(T &t) { n   Q<T>()(*this, t); n   return *this; n } n template <class T> n std::ostream &operator<(std::ostream &out, const T &t) { n   return out << t; n } n using std::cout; n MI cin; n const int $n = 500005; n const int $t = 1048600; n template <typename T> n inline bool gmax(T &a, const T &b) { n   return a < b && (a = b, true); n } n int n, fa[$n], mx[$n], se[$n], dep[$n], es[$n][2], t1, t2, dia; n std::vector<std::pair<int, bool>> outs[$n]; n bool val[$n]; n void dfs(int x) { n   mx[x] = x; n   dep[x] = dep[fa[x]] + 1; n   for (auto [v, w] : outs[x]) { n     if (v == fa[x]) continue; n     fa[v] = x; n     val[v] = val[x] ^ w; n     dfs(v); n     if (dep[mx[v]] >= dep[mx[x]]) { n       se[x] = mx[x]; n       mx[x] = mx[v]; n     } else if (dep[mx[v]] > dep[se[x]]) n       se[x] = mx[v]; n   } n   if (se[x] && gmax(dia, dep[mx[x]] + dep[se[x]] - dep[x] * 2)) { n     t1 = mx[x]; n     t2 = se[x]; n   } n } n struct { n   int rt, fa[$n], dfn[$n], dt, siz[$n], dep[$n], seq[$n]; n   int mx[$t][2], ll, rr; n   bool rv[$t]; n   inline void upd(int x) { n     std::swap(mx[x][0], mx[x][1]); n     rv[x] ^= 1; n   } n   inline void pd(int x) { n     if (!rv[x]) return; n     upd((x << 1)); n     upd(((x << 1) | 1)); n     rv[x] = 0; n   } n   inline void pu(int x) { n     mx[x][0] = std::max(mx[(x << 1)][0], mx[((x << 1) | 1)][0]); n     mx[x][1] = std::max(mx[(x << 1)][1], mx[((x << 1) | 1)][1]); n   } n   void build(int x = 1, int l = 1, int r = n) { n     if (l == r) return (void)(mx[x][val[seq[l]]] = dep[seq[l]] - 1); n     const int mid = (l + r) >> 1; n     build((x << 1), l, mid); n     build(((x << 1) | 1), mid + 1, r); n     pu(x); n   } n   void $update(int x, int l, int r) { n     if (ll <= l && r <= rr) return upd(x); n     const int mid = (l + r) >> 1; n     pd(x); n     if (ll <= mid) $update((x << 1), l, mid); n     if (mid < rr) $update(((x << 1) | 1), mid + 1, r); n     pu(x); n   } n   inline void update(int l, int r) { n     ll = l; n     rr = r; n     $update(1, 1, n); n   } n   void dfs(int x) { n     seq[dfn[x] = ++dt] = x; n     siz[x] = 1; n     dep[x] = dep[fa[x]] + 1; n     for (auto [v, w] : outs[x]) { n       if (v == fa[x]) continue; n       fa[v] = x; n       dfs(v); n       siz[x] += siz[v]; n     } n   } n   inline void init(int rt) { n     this->rt = rt; n     dfs(rt); n     build(); n   } n   inline int flip(int x, int y) { n     if (fa[y] == x) std::swap(x, y); n     assert(fa[x] == y); n     update(dfn[x], dfn[x] + siz[x] - 1); n     return mx[1][val[rt]]; n   } n } A, B; n int main() { n   cin > n; n   for (int i = 1; i < n; ++i) { n     const int x = es[i][0] = (cin.read<int>()), n               y = es[i][1] = (cin.read<int>()), w = (cin.read<int>()); n     outs[x].emplace_back(y, w); n     outs[y].emplace_back(x, w); n   } n   dfs(1); n   A.init(t1); n   B.init(t2); n   for (int T = (cin.read<int>()); T; --T) { n     const int id = (cin.read<int>()), x = es[id][0], y = es[id][1]; n     cout < std::max(A.flip(x, y), B.flip(x, y)) < (  n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n char s[3009], t[3009]; n long long dp[3009][3009]; n int n, m; n long long bigmod(long long a, long long p) { n   long long r = 1ll; n   while (p) { n     if ((p & 1ll)) { n       r = (a * r) % 998244353ll; n     } n     a = (a * a) % 998244353ll; n     p >>= 1; n   } n   return r; n } n long long func(int i, int j) { n   if (i < 0) { n     return 1ll; n   } n   if (j >= m) { n     return bigmod(2, i + 1); n   } n   long long &ret = dp[i][j]; n   if (~ret) return ret; n   ret = 0; n   if (s[i] == t[j]) ret = func(i - 1, j + 1); n   if (j + i >= m || (s[i] == t[i + j])) ret += func(i - 1, j); n   if (ret >= 998244353ll) ret -= 998244353ll; n   return ret; n } n int main() { n   scanf( %s , s); n   scanf( %s , t); n   n = strlen(s); n   m = strlen(t); n   memset(dp, -1, sizeof(dp)); n   long long ans = 0; n   for (int i = n; i >= m; i--) { n     ans += func(i - 1, 0); n     if (ans >= 998244353ll) ans -= 998244353ll; n   } n   if (ans >= 998244353ll) ans -= 998244353ll; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n int a, b, c, d, i, j, n, m, k, ans; n int cs[100001], mas[100001], dp[100001]; n inline int sum(int a, int b) { n   if (!a) n     return cs[b]; n   else n     return cs[b] - cs[a - 1]; n } n int main() { n   scanf( %d , &n); n   for (int _n((n)-1), i(0); i <= _n; i++) { n     scanf( %d , &mas[i]); n     if (!i) n       cs[i] = mas[i]; n     else n       cs[i] = cs[i - 1] + mas[i]; n   } n   ans = -1000000000 - 1; n   for (int _n((n)-1), i(0); i <= _n; i++) { n     if (!i) n       dp[i] = max(cs[i], -cs[i]); n     else n       dp[i] = max(dp[i - 1] + mas[i], max(cs[i], -cs[i])); n     ans = max(ans, dp[i]); n   } n   a = 0; n   for (int i = (n - 2), _b = (0); i >= _b; i--) { n     a += mas[i + 1]; n     ans = max(ans, max(a, -a) + dp[i]); n   } n   ans = max(ans, cs[n - 1]); n   ans = max(ans, -cs[n - 1]); n   ans = max(ans, dp[n - 1]); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200005]; n long long b[200005]; n void solve() { n   long long n; n   cin >> n; n   long long c = 0, t, c1 = 0; n   cin >> t; n   for (long long i = 0; i < t; i++) { n     cin >> a[i]; n   } n   for (long long i = 1; i < t; i++) { n     if (n - a[i - 1] > 0) c += n - a[i - 1]; n   } n   cout << c; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   for (int i = 1; i <= t; i++) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m, k; n   cin >> n >> k; n   m = (3 * n <= k) ? 0 : 3 * n - k; n   cout << m << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(); n   long long a, b, c, n, x; n   cin >> a >> b >> c >> n; n   x = a + b - c; n   x = n - x; n   if (x < 1 || c > a || c > b || c > (a + b)) n     cout << -1 << endl; n   else n     cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n #define jh(x, y) (x ^= y ^= x ^= y) n #define loc(x, y) ((x - 1) * m + y) n #define lowbit(x) (x & -x) n typedef long long ll; n const int N = 5e5 + 5, INF = 0x3f3f3f3f, mod = 1e9 + 7; n const double PI = acos(-1), EPS = 1e-7; n using namespace std; n namespace fast_IO { n  ll read() { n   ll x = 0; n   char c; n   bool flag = false; n   while ((c = getchar()) ==     || c ==   n  || c ==   r ); n   if (c ==  - )flag = true; else x = c ^ 48; n   while ((c = getchar()) !=     && c !=   n  && c !=   r  && ~c) n    x = (x << 1) + (x << 3) + (c ^ 48); n   if (flag)return -x; n   return x; n  } n  void write(ll x) { n   if (x < 0)putchar( - ), x = -x; n   if (x >= 10)write(x / 10); n   putchar(x % 10 ^ 48); n  } n } n char s[4]; n bool tag[N]; n int a[N], b[N], pre[N]; n ll dp[N]; n void print(int l, int r, int st, int m) { n  if (l >= st) { n   print(pre[l], l, st, m); n   putchar( + ); n  } n  printf( %d , b[l + 1]); n  for (int i = l + 2; i <= r; ++i) n   printf( *%d , b[i]); n } n void solve(int n) { n  //既有*又有+ n  int m = n, st = 1; n  while (m && b[m] == 1)--m; n  while (st <= n && b[st] == 1)++st; n  for (int i = 1; i < st; ++i) { n   printf( 1 ); n   if (i != n)putchar( + ); n  } n  if(st > n)return; n  int sum = 1; n  for (int i = st; i <= m; ++i)sum = min((m - st + 1) * 2, sum * b[i]); n  if (sum == (m - st + 1) * 2) { n   printf( %d , b[st]); n   for (int i = st + 1; i <= m; ++i) n    printf( *%d , b[i]); n  } n  else { n   for (int i = st; i <= m; ++i)dp[i] = -1e17; n   dp[st] = b[st]; pre[st] = st - 1; n   dp[st - 1] = 0; n   for (int i = st; i < m; ++i) { n    if (b[i + 1] == 1)dp[i + 1] = dp[i] + 1, pre[i + 1] = i; n    else { n     ll mul = 1; n     for (int j = i + 1; j >= st; --j) { n      mul *= b[j]; n      if (dp[i + 1] < dp[j - 1] + mul) { n       dp[i + 1] = dp[j - 1] + mul; n       pre[i + 1] = j - 1; n      } n     } n    } n   } n   print(pre[m], m, st, m); n  } n  for (int i = m + 1; i <= n; ++i) n   printf( +1 ); n } n  n int main() { n  int n = fast_IO::read(); n  for (int i = 1; i <= n; ++i)a[i] = fast_IO::read(); n  scanf( %s , s); n  for (int i = 0; s[i]; ++i)tag[s[i]] = true; n  if (!s[1]) { n   printf( %d , a[1]); n   for (int i = 2; i <= n; ++i) n    printf( %c%d , s[0], a[i]); n  } n  else if (!tag[ * ]) { n   if (tag[ + ]) { n    printf( %d , a[1]); n    for (int i = 2; i <= n; ++i) n     printf( +%d , a[i]); n   } n  } n  else { n   if (!tag[ + ]) { n    printf( %d , a[1]); n    for (int i = 2; i <= n; ++i) { n     if (a[i] == 0) { n      printf( -0 ); n      continue; n     } n     printf( *%d , a[i]); n    } n   } n   else { n    int now = 0; n    for (int i = 1; i <= n; ++i) { n     if (a[i] == 0) { n      if (now)solve(now); n      now = 0; n      if (i != 1 && a[i - 1])putchar( + ); n      printf( 0 ); n      if (i != n)putchar( + ); n     } n     else b[++now] = a[i]; n    } n    if (now)solve(now); n   } n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n const int K = 65; n const int inf = 1000 * 1000 * 1000; n const int mod = 1000 * 1000 * 1000 + 7; n int n, a[N], q; n int t[4 * N][K]; n void build(int v, int s, int e) { n   if (s == e) { n     for (int i = 0; i < 60; i++) { n       if (i % a[s] == 0) n         t[v][i] = 2; n       else n         t[v][i] = 1; n     } n     return; n   } n   int m = (s + e) / 2; n   build(2 * v, s, m); n   build(2 * v + 1, m + 1, e); n   for (int i = 0; i < 60; i++) { n     int x = (i + t[2 * v][i]) % 60; n     t[v][i] = t[2 * v][i] + t[2 * v + 1][x]; n   } n } n void update(int v, int s, int e, int pos) { n   if (s == e) { n     for (int i = 0; i < 60; i++) { n       if (i % a[s] == 0) n         t[v][i] = 2; n       else n         t[v][i] = 1; n     } n     return; n   } n   int m = (s + e) / 2; n   if (pos <= m) n     update(2 * v, s, m, pos); n   else n     update(2 * v + 1, m + 1, e, pos); n   for (int i = 0; i < 60; i++) { n     int x = (i + t[2 * v][i]) % 60; n     t[v][i] = t[2 * v][i] + t[2 * v + 1][x]; n   } n } n int get(int v, int s, int e, int l, int r, int st) { n   if (l > r) return -1; n   if (s == l && e == r) return t[v][st]; n   int m = (s + e) / 2; n   int answ = 0; n   int T = get(2 * v, s, m, l, min(r, m), st); n   if (T == -1) n     T = st; n   else { n     answ += T; n     T += st; n   } n   int P = get(2 * v + 1, m + 1, e, max(m + 1, l), r, T % 60); n   if (P != -1) answ += P; n   return answ; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   a[n + 1] = 1; n   build(1, 1, n + 1); n   scanf( %d , &q); n   while (q--) { n     char x; n     int aa, b; n     cin >> x; n     scanf( %d%d , &aa, &b); n     if (x ==  A ) { n       int T = get(1, 1, n + 1, aa, b - 1, 0); n       printf( %d n , T); n     } else { n       a[aa] = b; n       update(1, 1, n + 1, aa); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long a[100002]; n long b[100002]; n long s[100002]; n long l[100002]; n vector<int> br0, br1; n map<int, vector<int> > fre, an; n const int MAXN = 100001; n int comp = 0; n vector<int> g[MAXN], h[MAXN]; n bool used[MAXN]; n int timer, tin[MAXN], fup[MAXN], col[MAXN]; n int n, ans = 0; n void IS_BRIDGE(int i, int j) { n   br0.push_back(i); n   br1.push_back(j); n } n void dfs(int v, int p = -1) { n   used[v] = true; n   tin[v] = fup[v] = timer++; n   for (size_t i = 0; i < g[v].size(); ++i) { n     int to = g[v][i]; n     if (to == p) continue; n     if (used[to]) n       fup[v] = min(fup[v], tin[to]); n     else { n       dfs(to, v); n       fup[v] = min(fup[v], fup[to]); n       if (fup[to] > tin[v]) IS_BRIDGE(v, to); n     } n   } n } n void find_bridges() { n   timer = 0; n   for (int i = 0; i < n; ++i) used[i] = false; n   for (int i = 0; i < n; ++i) n     if (!used[i]) dfs(i); n } n void dfs_h(int i) { n   used[i] = 1; n   col[i] = comp; n   for (int j = 0; j < fre[i].size(); j++) n     if (!used[fre[i][j]]) dfs_h(fre[i][j]); n } n void dfs_k(int i, int dl, int z) { n   used[i] = 1; n   for (int j = 0; j < an[i].size(); j++) n     if (an[i][j] == z) { n       ans += dl + 1; n     } else if (!used[an[i][j]]) n       dfs_k(an[i][j], dl + 1, z); n } n int main() { n   int m, k, c; n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     cin >> a[i] >> b[i]; n     g[a[i]].push_back(b[i]); n     g[b[i]].push_back(a[i]); n   } n   cin >> k; n   for (int i = 1; i <= k; i++) cin >> s[i] >> l[i]; n   find_bridges(); n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < g[i].size(); j++) { n       bool OK = 0; n       for (int k = 0; k < br0.size(); k++) { n         if (((br0[k] == i) && (br1[k] == g[i][j])) || n             ((br1[k] == i) && (br0[k] == g[i][j]))) n           OK = 1; n       } n       if (!OK) { n         fre[i].push_back(g[i][j]); n         fre[g[i][j]].push_back(i); n       } n     } n   for (int i = 1; i <= n; i++) used[i] = 0; n   for (int i = 1; i <= n; i++) n     if (!used[i]) { n       comp++; n       dfs_h(i); n     } n   for (int k = 0; k < br0.size(); k++) { n     an[col[br0[k]]].push_back(col[br1[k]]); n     an[col[br1[k]]].push_back(col[br0[k]]); n   } n   for (int i = 1; i <= k; i++) { n     if (col[s[i]] == col[l[i]]) n       cout << 0 << endl; n     else { n       for (int j = 1; j <= n; j++) used[j] = 0; n       ans = 0; n       dfs_k(col[s[i]], 0, col[l[i]]); n       cout << ans << endl; n     } n   } n } n


#include <bits/stdc++.h> n int main(int argc, const char* argv[]) { n   int n, x; n   std::cin >> n >> x; n   std::vector<int> v(n); n   for (int i = 0; i < n; i++) std::cin >> v[i]; n   std::sort(v.begin(), v.end()); n   for (int i = 0; i < n; i++) { n     if (v[i] > x) { n       std::cout << x - i << std::endl; n       return 0; n     } else if (v[i] == x) { n       std::cout << x - i + 1 << std::endl; n       return 0; n     } n   } n   std::cout << x - v.size() << std::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1e9 + 7; n const long long inf = (long long)1e9; n double PI = 3.14159265; n double eps = 1e-9; n long long t = 0; n long long pow(long long a, long long b) { n   if (b == 0) return 1; n   if (b % 2 == 1) { n     return (pow(a, b - 1) * a); n   } n   if (b % 2 == 0) { n     long long c = pow(a, b / 2); n     return (c * c); n   } n } n vector<pair<long long, long long>> reb; n long long dp[1 << 17]; n long long krask[1 << 17]; n vector<vector<long long>> g; n vector<long long> dep; n vector<vector<long long>> p; n vector<vector<long long>> dep2; n vector<long long> u; n vector<long long> tin, tout; n void dfs(long long v, long long k) { n   tin[v] = t++; n   p[v][0] = k; n   for (long long i = 1; i < 20; i++) { n     p[v][i] = p[p[v][i - 1]][i - 1]; n   } n   for (long long i = 0; i < g[v].size(); i++) { n     long long to = g[v][i]; n     if (dep[to] == -1) { n       dep[to] = dep[v] + 1; n       dep2[dep[to]].push_back(t); n       dfs(to, v); n     } n   } n   tout[v] = t; n } n long long parent(long long v, long long h) { n   for (long long i = 0; i < 20; i++) { n     if ((h & (1 << i))) { n       v = p[v][i]; n     } n   } n   return v; n } n long long lca(long long a, long long b) { n   if (dep[a] < dep[b]) swap(a, b); n   a = parent(a, dep[a] - dep[b]); n   if (a == b) return a; n   for (long long i = 19; i >= 0; i--) { n     if (p[a][i] != p[b][i]) { n       a = p[a][i]; n       b = p[b][i]; n     } n   } n   return parent(a, 1); n } n struct stree { n   vector<long long> t; n   int n; n   stree(vector<long long> a) { n     n = 1; n     while (n < a.size()) { n       n *= 2; n     } n     t.resize(n + n); n     for (int i = n; i < n + a.size(); i++) { n       t[i] = a[i - n]; n     } n     for (int i = n - 1; i > 0; i--) { n       t[i] = t[i + i] + t[i + i + 1]; n     } n   } n   long long sum(long long l, long long r) { n     long long sum = 0; n     for (l += n, r += n; l <= r; l /= 2, r /= 2) { n       if (l % 2 == 1) { n         sum += t[l++]; n       } n       if (r % 2 == 0) { n         sum += t[r--]; n       } n     } n     return sum; n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, q; n   cin >> n >> q; n   vector<long long> a; n   a.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   stree d(a); n   vector<long long> k; n   k.resize(q); n   long long gl = 0; n   int pred = 0; n   for (int i = 0; i < q; i++) { n     cin >> k[i]; n     long long r = a.size(), l = gl - 1; n     while (r - l > 1) { n       long long mid = (l + r) / 2; n       long long sum = d.sum(gl, mid); n       if (sum > k[i] + pred) n         r = mid; n       else { n         l = mid; n       } n     } n     if (r == a.size()) { n       gl = 0; n       pred = 0; n     } else { n       if (k[i] + pred < d.sum(gl, r - 1)) { n         pred = k[i] + pred; n       } else { n         pred = k[i] + pred - d.sum(gl, r - 1); n         gl = r; n       } n     } n     cout << a.size() - gl << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a1, b1, a2, b2, a3, b3; n   cin >> a1 >> b1 >> a2 >> b2 >> a3 >> b3; n   int l1 = min(a1, b1); n   int l2 = min(a2, b2); n   int l3 = min(a3, b3); n   int L1 = max(a1, b1); n   int L2 = max(a2, b2); n   int L3 = max(a3, b3); n   if (l2 + l3 <= l1 && max(L2, L3) <= L1) n     cout <<  YES  n          <<   n ; n   else if (l2 + l3 <= L1 && max(L2, L3) <= l1) n     cout <<  YES  n          <<   n ; n   else if (L2 + L3 <= l1 && max(l2, l3) <= L1) n     cout <<  YES  n          <<   n ; n   else if (L2 + L3 <= L1 && max(l2, l3) <= l1) n     cout <<  YES  n          <<   n ; n   else if (L2 + l3 <= L1 && max(l2, L3) <= l1) n     cout <<  YES  n          <<   n ; n   else if (l2 + L3 <= L1 && max(L2, l3) <= l1) n     cout <<  YES  n          <<   n ; n   else n     cout <<  NO  n          <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 100; n const long long M = 21; n const long long mod = 1e9 + 7; n const long long MOD = 998244353; n const long long P = 1336; n const long double eps = 0.000000001; n const long long inf = 1e16 + 7; n mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count()); n vector<pair<long long, long long> > g[N], ng[N]; n long long sz[N], pr[N], tin[N], tout[N]; n long long tim = 0; n pair<long long, long long> w[N][M]; n long long r[N]; n long long get(long long v) { n   if (pr[v] == v) return v; n   return pr[v] = get(pr[v]); n } n void unite(long long a, long long b) { n   a = get(a); n   b = get(b); n   if (a == b) return; n   if (sz[a] < sz[b]) swap(a, b); n   sz[a] += sz[b]; n   pr[b] = a; n } n void DFS(long long v, long long pr, long long zn) { n   w[v][0].first = pr; n   w[v][0].second = zn; n   for (long long i = 1; i < M; i++) { n     w[v][i].first = w[w[v][i - 1].first][i - 1].first; n     w[v][i].second = n         max(w[v][i - 1].second, w[w[v][i - 1].first][i - 1].second); n   } n   tim++; n   tin[v] = tim; n   for (auto to : ng[v]) { n     if (to.first == pr) continue; n     DFS(to.first, v, to.second); n   } n   tim++; n   tout[v] = tim; n } n bool upper(long long a, long long b) { n   return ((tin[a] <= tin[b]) && (tout[a] >= tout[b])); n } n long long LCA(long long a, long long b) { n   if (upper(b, a)) swap(a, b); n   long long mx = 0; n   for (long long i = M - 1; i >= 0; i--) { n     if (!upper(w[b][i].first, a)) { n       mx = max(mx, w[b][i].second); n       b = w[b][i].first; n     } n   } n   mx = max(mx, w[b][0].second); n   b = w[b][0].first; n   for (long long i = M - 1; i >= 0; i--) { n     if (!upper(w[a][i].first, b)) { n       mx = max(mx, w[a][i].second); n       a = w[a][i].first; n     } n   } n   if (a != b) mx = max(mx, w[a][0].second); n   return mx; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   srand(time(0)); n   long long n, m, k, T; n   cin >> n >> m >> k >> T; n   for (long long i = 0; i < m; i++) { n     long long x, y, z; n     cin >> x >> y >> z; n     x--; n     y--; n     g[x].push_back({y, z}); n     g[y].push_back({x, z}); n   } n   for (long long i = 0; i < n; i++) { n     r[i] = inf; n   } n   priority_queue<pair<long long, long long> > q; n   for (long long i = 0; i < k; i++) { n     q.push({0, i}); n   } n   while (!q.empty()) { n     long long v = q.top().second; n     long long ra = q.top().first; n     ra = -ra; n     q.pop(); n     if (ra > r[v]) continue; n     r[v] = ra; n     for (auto to : g[v]) { n       if (r[to.first] > r[v] + to.second) { n         r[to.first] = r[v] + to.second; n         q.push({-r[to.first], to.first}); n       } n     } n   } n   vector<pair<long long, pair<long long, long long> > > e; n   for (long long i = 0; i < n; i++) { n     for (auto to : g[i]) { n       e.push_back({r[i] + r[to.first] + to.second, {i, to.first}}); n     } n   } n   for (long long i = 0; i < n; i++) { n     pr[i] = i; n     sz[i] = 1; n   } n   sort(e.begin(), e.end()); n   for (auto to : e) { n     if (get(to.second.first) != get(to.second.second)) { n       unite(to.second.first, to.second.second); n       ng[to.second.first].push_back({to.second.second, to.first}); n       ng[to.second.second].push_back({to.second.first, to.first}); n     } n   } n   DFS(0, 0, 0); n   while (T--) { n     long long a, b; n     cin >> a >> b; n     a--; n     b--; n     cout << LCA(a, b) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } n const int INF = (((1 << 30) - 1) << 1) + 1; n const int nINF = 1 << 31; n string s; n int main() { n   ios::sync_with_stdio(false); n   cin >> s; n   int n = s.size(); n   int open, close, q, ans = 0; n   for (int i = 0; i < (n); i++) { n     open = 0, close = 0, q = 0; n     for (int j = (i); j <= (n - 1); j++) { n       if (s[j] ==  ( ) n         ++open; n       else if (s[j] ==  ) ) n         ++close; n       else { n         ++close; n         ++q; n       } n       if (close > open) { n         if (q) { n           --q; n           ++open; n           --close; n         } else n           break; n       } n       if ((i - j + 1) % 2 == 0 && open == close) { n         ++ans; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   long long n, k; n   string x; n   cin >> n >> k >> x; n   string ans =   ; n   int o = -1; n   for (int i = 1; i < x.size(); i++) { n     if (x.substr(i, x.size() - i) == x.substr(0, x.size() - i)) { n       o = i; n       break; n     } n   } n   if (o == -1) { n     for (int i = 0; i < k; i++) { n       cout << x; n     } n   } else { n     for (int i = 0; i < k; i++) { n       cout << x.substr(0, o); n     } n     cout << x.substr(o, x.size() - o); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lli = long long; n using lld = long double; n using ulli = unsigned long long int; n using pll = pair<lli, lli>; n using ttt = pair<lli, pll>; n using vttt = vector<ttt>; n using vll = vector<pll>; n using vl = vector<lli>; n using vi = vector<int>; n using vvi = vector<vector<int>>; n using cd = complex<lld>; n const double PI = acos(-1); n vvi adj, dist; n pll get(vi vect) { n   cout <<  ?   << ((lli)(vect).size()); n   for (auto& x : vect) cout <<     << x; n   cout <<   n ; n   cout.flush(); n   pll ans; n   cin >> ans.first >> ans.second; n   return ans; n } n void dfs(int v, int p, int dis) { n   dist[dis].push_back(v); n   for (auto& x : adj[v]) n     if (x != p) { n       dfs(x, v, dis + 1); n     } n } n pll check(int val) { n   if (dist[val].empty()) return {1000000007, 1000000007}; n   return get(dist[val]); n } n int tejas_919(int kkkk) { n   lli n, m, k, q, u, v, temp = 0, ans = 0; n   cin >> n; n   adj = vvi(n + 2); n   dist = vvi(n + 2); n   for (int i = 1; i < n; i++) { n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   int root, pdist; n   { n     vi vect; n     for (int i = 1; i < n + 1; i++) vect.push_back(i); n     tie(root, pdist) = get(vect); n   } n   dfs(root, root, 0); n   int l = (pdist + 1) / 2, r = pdist, cdis; n   root = -1; n   while (l <= r) { n     int mid = (l + r) / 2; n     tie(temp, cdis) = check(mid); n     if (cdis == pdist) { n       l = mid + 1; n       root = temp; n     } else n       r = mid - 1; n   } n   if (root < 0) root = get(dist[l - 1]).first; n   dist = vvi(n + 2); n   dfs(root, root, 0); n   ans = get(dist[pdist]).first; n   cout <<  !   << root <<     << ans <<   n ; n   cout.flush(); n   string s; n   cin >> s; n   return 0; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cout << fixed << setprecision(10); n   int t = 1; n   cin >> t; n   for (int i = 0; i < t; i++) { n     tejas_919(i + 1); n   } n   {}; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using pll = pair<ll, ll>; n const int N = 325; n int n, s[N], sum[26][N], vc; n bitset<N> b[N]; n char ans[N]; n void addv(int s, int e) { n   int flag = 0; n   for (int i = s - 1, j = e - 1; i <= j; i++, j--) { n     if (!flag) { n       b[vc].reset(); n       flag = 1; n     } else n       b[vc] = b[vc - 1]; n     for (int k = i; k <= j; k++) b[vc].flip(k); n     vc++; n   } n } n int ref() { n   int c = 0; n   for (int i = 0; i < n; i++) { n     for (int j = c + 1; j < vc; j++) n       if (b[j][i]) { n         swap(b[j], b[c]); n         break; n       } n     if (!b[c][i]) continue; n     for (int j = c + 1; j < vc; j++) n       if (b[j][i]) b[j] ^= b[c]; n     c++; n   } n   if (c == n) { n     for (int i = n - 1; i >= 0; i--) { n       for (int j = 0; j < i; j++) n         if (b[j][i]) b[j] ^= b[i]; n     } n   } n   return c; n } n void ask(int s, int e) { n   printf( ? %d %d n , s, e); n   fflush(stdout); n   int cnt = (e - s + 1) * (e - s + 2) / 2; n   char buf[N]; n   while (cnt--) { n     scanf( %s , buf); n     int k = strlen(buf); n     if (k > (e - s + 2) / 2) continue; n     for (int i = 0; i < k; i++) { n       sum[buf[i] -  a ][vc + k - 1] ^= 1; n     } n   } n   vc += (e - s + 2) / 2; n } n int main() { n   cin >> n; n   int aa, bb, cc; n   int ra, rb, rc; n   srand(time(0)); n   while (true) { n     aa = rand() % n + 1; n     bb = rand() % n + 1; n     cc = rand() % n + 1; n     if (aa * (aa + 1) / 2 + bb * (bb + 1) / 2 + cc * (cc + 1) / 2 > n         ceil(0.777 * (n + 1) * (n + 1)) - 1) n       continue; n     ra = 1; n     rb = rand() % (n - bb + 1) + 1; n     rc = n - cc + 1; n     vc = 0; n     addv(ra, ra + aa - 1); n     addv(rb, rb + bb - 1); n     addv(rc, rc + cc - 1); n     int ttt = ref(); n     if (ttt == n) break; n   } n   vc = 0; n   ask(ra, ra + aa - 1); n   ask(rb, rb + bb - 1); n   ask(rc, rc + cc - 1); n   for (int i = 0; i < 26; i++) { n     vc = 0; n     addv(ra, ra + aa - 1); n     addv(rb, rb + bb - 1); n     addv(rc, rc + cc - 1); n     for (int j = 0; j < vc; j++) b[j][n] = sum[i][j]; n     ref(); n     for (int j = 0; j < n; j++) n       if (b[j][n]) ans[j] = i +  a ; n   } n   printf( ! %s n , ans); n   return 0; n } n