#include <bits/stdc++.h> n const int N = 1e5 + 5, M = 2e5 + 5, logN = 17 + 1; n int n, m, root, idx, a[N], f[N][logN], dfn[N], seq[N], sz[N], dep[N]; n int tot, lnk[N], ter[M], nxt[M]; n long long seg[N << 2], tag[N << 2]; n void pushup(int rt) { seg[rt] = seg[rt << 1] + seg[rt << 1 | 1]; } n void build(int rt, int l, int r) { n   if (l == r) { n     seg[rt] = a[seq[l]]; n     return; n   } n   int mid = (l + r) >> 1; n   build(rt << 1, l, mid); n   build(rt << 1 | 1, mid + 1, r); n   pushup(rt); n } n void update(int rt, int l, int r, long long k) { n   seg[rt] += 1LL * (r - l + 1) * k; n   tag[rt] += k; n } n void pushdown(int rt, int l, int r) { n   if (!tag[rt]) return; n   int mid = (l + r) >> 1; n   update(rt << 1, l, mid, tag[rt]); n   update(rt << 1 | 1, mid + 1, r, tag[rt]); n   tag[rt] = 0; n } n void modify(int x, int y, int rt, int l, int r, int k) { n   if (x <= l && r <= y) { n     update(rt, l, r, k); n     return; n   } n   pushdown(rt, l, r); n   int mid = (l + r) >> 1; n   if (x <= mid) modify(x, y, rt << 1, l, mid, k); n   if (mid < y) modify(x, y, rt << 1 | 1, mid + 1, r, k); n   pushup(rt); n } n long long query(int x, int y, int rt, int l, int r) { n   if (x <= l && r <= y) return seg[rt]; n   pushdown(rt, l, r); n   int mid = (l + r) >> 1; n   long long ret = 0; n   if (x <= mid) ret += query(x, y, rt << 1, l, mid); n   if (mid < y) ret += query(x, y, rt << 1 | 1, mid + 1, r); n   return ret; n } n void add(int u, int v) { ter[++tot] = v, nxt[tot] = lnk[u], lnk[u] = tot; } n void dfs(int u, int fa) { n   dep[u] = dep[fa] + 1, f[u][0] = fa, dfn[u] = ++idx, seq[idx] = u, sz[u] = 1; n   for (int i = 1; (1 << i) <= dep[u]; ++i) f[u][i] = f[f[u][i - 1]][i - 1]; n   for (int i = lnk[u]; i; i = nxt[i]) { n     int v = ter[i]; n     if (v == fa) continue; n     dfs(v, u), sz[u] += sz[v]; n   } n } n int lca(int u, int v) { n   if (dep[u] > dep[v]) u ^= v ^= u ^= v; n   for (int i = 17; ~i; --i) n     if (dep[f[v][i]] >= dep[u]) v = f[v][i]; n   if (u == v) return u; n   for (int i = 17; ~i; --i) n     if (f[u][i] ^ f[v][i]) u = f[u][i], v = f[v][i]; n   return f[u][0]; n } n int getlca(int u, int v, int p) { n   int x = lca(u, v), y = lca(u, p), z = lca(v, p); n   if (dep[y] > dep[x]) x = y; n   if (dep[z] > dep[x]) x = z; n   return x; n } n int jump(int u, int d) { n   for (int i = 17; ~i; --i) n     if (d & (1 << i)) u = f[u][i]; n   return u; n } n void treeModify(int u, int k) { n   int l = dfn[u], r = dfn[u] + sz[u] - 1; n   if (u == root) n     modify(1, n, 1, 1, n, k); n   else if (dfn[root] < l || dfn[root] > r) n     modify(l, r, 1, 1, n, k); n   else { n     int son = jump(root, dep[root] - dep[u] - 1); n     modify(1, n, 1, 1, n, k), n         modify(dfn[son], dfn[son] + sz[son] - 1, 1, 1, n, -k); n   } n } n long long treeQuery(int u) { n   int l = dfn[u], r = dfn[u] + sz[u] - 1; n   if (u == root) n     return query(1, n, 1, 1, n); n   else if (dfn[root] < l || dfn[root] > r) n     return query(l, r, 1, 1, n); n   else { n     int son = jump(root, dep[root] - dep[u] - 1); n     return query(1, n, 1, 1, n) - n            query(dfn[son], dfn[son] + sz[son] - 1, 1, 1, n); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1; i < n; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     add(u, v), add(v, u); n   } n   dfs(1, 0); n   build(1, 1, n); n   root = 1; n   while (m--) { n     int opt; n     scanf( %d , &opt); n     if (opt == 1) { n       scanf( %d , &root); n     } else if (opt == 2) { n       int u, v, x; n       scanf( %d%d%d , &u, &v, &x); n       treeModify(getlca(u, v, root), x); n     } else { n       int x; n       scanf( %d , &x); n       printf( %lld n , treeQuery(x)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ans, dp[8][8]; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int l, r; n     cin >> l >> r; n     dp[l][r] = dp[r][l] = 1; n   } n   if (n <= 6) n     cout << m; n   else { n     for (int i = 1; i <= n; i++) n       for (int j = i + 1; j <= n; j++) { n         int res = 0; n         for (int k = 1; k <= n; k++) n           if (dp[i][k] && dp[j][k]) res++; n         ans = max(ans, m - res); n       } n     cout << ans; n   } n } n


#include <bits/stdc++.h> n  n using std::cin; n using std::cout; n using std::vector; n using std::swap; n  n int main() { n  int t; n  cin >> t; n   n  while (t--) { n   int n, x; n   cin >> n >> x; n   vector<int> a(n); n   for (auto& it : a) cin >> it; n   int cnt = 0; n   for (int i = 0; i < n && !is_sorted(a.begin(), a.end()); ++i) { n    if (a[i] > x) { n     swap(a[i], x); n     cnt++; n    } n   } n   cout << (!is_sorted(a.begin(), a.end()) ? -1 : cnt) <<   n ; n  } n   n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int maxn = 303; n int n, m, d; n int a[maxn], b[maxn], t[maxn]; n int dt[maxn]; n long long que[150001]; n int id[150001]; n long long dp[150001][2]; n int main() { n   scanf( %d%d%d , &n, &m, &d); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d%d , a + i, b + i, t + i); n     dt[i] = t[i] - t[i - 1]; n   } n   dt[1] = n; n   for (int ii = 1; ii <= m; ++ii) { n     int i = ii % 2; n     int l = 1, r = 1, nl = 1, nr = 1; n     int lt, rt; n     for (int j = 1; j <= n; ++j) { n       lt = max(1ll, 1ll * j - 1ll * dt[ii] * d); n       rt = min(1ll * n, 1ll * j + 1ll * dt[ii] * d); n       while (nr <= rt) { n         while (r > l && dp[nr][1 - i] >= que[r - 1]) r--; n         que[r] = dp[nr][1 - i]; n         id[r] = nr; n         r++; n         nr++; n       } n       while (r > l && id[l] < lt) l++; n       dp[j][i] = que[l] + 1ll * b[ii] - 1ll * abs(a[ii] - j); n     } n   } n   long long ans = dp[1][m % 2]; n   for (int i = 2; i <= n; ++i) { n     ans = max(ans, dp[i][m % 2]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n long long p; n int k; n namespace solver1 { n void main() { n   std::vector<int> ans; n   int i; n   for (i = 0; p; i++) { n     if (i & 1) n       ans.push_back(((p - 1) / k + 1) * k - p), p = (p - 1) / k + 1; n     else n       ans.push_back(p % k), p /= k; n   } n   printf( %d n , (int)ans.size()); n   for (auto i : ans) { n     printf( %d  , i); n   } n   printf(  n ); n } n }  // namespace solver1 n int main() { n   std::cin >> p >> k; n   solver1::main(); n } n


#include <bits/stdc++.h> n using namespace std; n int *a; n int *b; n int n; n long long int what(long long x) { n   long long int s = 0; n   for (int i = 0; i < n; i++) { n     s += (x * a[i]) / b[i]; n   } n   return s; n } n int main() { n   int i, j; n   int c; n   cin >> n >> c; n   a = new int[n]; n   b = new int[n]; n   for (i = 0; i < n; i++) { n     cin >> a[i] >> b[i]; n   } n   c = c - n; n   long long int l = 0; n   long long int r = ((c + 1) * b[0]) / a[0] + 3; n   long long int x; n   x = r; n   while (r - l > 1) { n     if (what(x) < c) n       l = x; n     else n       r = x; n     x = (l + r) / 2; n   } n   long long int rl = r; n   l = 0; n   r = ((c + 1) * b[0]) / a[0] + 3; n   x = r; n   while (r - l > 1) { n     if (what(x) <= c) n       l = x; n     else n       r = x; n     x = (l + r) / 2; n   } n   cout << (r - rl); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long M = 1000000007; n long long power(long long a, long long b) { n   long long res = 1; n   for (long long i = 0; i < b; i++) res = (res * a) % M; n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t, n, m, k, i, x, ara[100], ans; n   cin >> t; n   while (t--) { n     cin >> n >> k; n     x = 0; n     m = k; n     ans = 0; n     while (m != 0) { n       ara[x] = m % 2; n       x++; n       m /= 2; n     } n     for (i = 0; i < x; i++) { n       if (ara[i] == 1) ans = ((ans % M) + power(n, i)) % M; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long vet[9], used[9], available[9]; n int dp[100][32]; n int dp2[100][32]; n vector<int> itens, itens2; n void comp() { n   for (int i = 1; i <= 8; i++) { n     long long j = 1; n     used[i] = min(used[i], (long long)32 / i); n     while (used[i]) { n       itens.push_back(min(used[i], j) * i); n       used[i] -= min(used[i], j); n       j <<= 1; n     } n     j = 1; n     available[i] = min(available[i], (long long)32 / i); n     while (available[i]) { n       itens2.push_back(min(available[i], j) * i); n       available[i] -= min(available[i], j); n       j <<= 1; n     } n   } n   dp[0][0] = dp2[0][0] = 1; n   for (int i = 0; i < itens.size(); i++) { n     for (int j = 0; j < 32; j++) { n       dp[i + 1][j] |= dp[i][j]; n       if (j >= itens[i]) dp[i + 1][j] |= dp[i][j - itens[i]]; n     } n   } n   for (int i = 0; i < itens2.size(); i++) { n     for (int j = 0; j < 32; j++) { n       dp2[i + 1][j] |= dp2[i][j]; n       if (j >= itens2[i]) dp2[i + 1][j] |= dp2[i][j - itens2[i]]; n     } n   } n } n int consegue(int x) { n   for (int i = 0; i < 32 - x; i++) n     if (dp2[itens2.size()][i] && dp[itens.size()][x + i]) return 1; n   return 0; n } n int main() { n   long long W, total = 0; n   scanf( %lld , &W); n   for (int i = 1; i <= 8; i++) { n     scanf( %lld , &vet[i]); n     total += i * vet[i]; n   } n   if (total <= W) n     printf( %lld n , total); n   else { n     for (int i = 1; i <= 8; i++) used[i] = vet[i]; n     for (int i = 8; i >= 1; i--) { n       if (total - i * used[i] >= W) { n         total -= i * used[i]; n         available[i] = used[i]; n         used[i] = 0; n       } else { n         long long d = (total - W) / i; n         total -= d * i; n         used[i] -= d; n         available[i] = d; n       } n     } n     comp(); n     for (long long z = W; z >= max(0LL, W - 8); z--) { n       int x = total - z; n       if (consegue(x)) { n         return !printf( %lld n , z); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k, A, B; n vector<long long> a(100000 + 5); n long long rec(int l, int r) { n   long long m = (l + r) / 2, i, j, col = 0, lng = r - l + 1; n   i = lower_bound(a.begin(), a.end(), l) - a.begin(); n   j = upper_bound(a.begin(), a.end(), r) - a.begin(); n   j--; n   col = j - i + 1; n   if (l == r && !col) return A; n   if (l == r && col) return B * col; n   if (!col) return A; n   return min(B * lng * col, rec(l, m) + rec(m + 1, r)); n } n int main() { n   long long x; n   cin >> n >> k >> A >> B; n   for (int i = 0; i < k; i++) { n     cin >> x; n     a.push_back(x); n   } n   sort(a.begin(), a.end()); n   cout << rec(1, pow(2, n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[500100]; n int n; n vector<int> g[200100]; n int d[200100]; n int cnt = 1; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) { n     int ind = lower_bound(d + 1, d + n + 1, a[i]) - d; n     d[ind - 1] = a[i]; n     g[ind - 1].push_back(a[i]); n   } n   for (int i = n; i >= 1; i--) { n     for (int j = 0; j < g[i].size(); j++) cout << g[i][j] <<    ; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n double x[(int)2e3 + 5], y[(int)2e3 + 5]; n int fio() { n   int val = 0; n   char ch = getchar(); n   while (ch ==     || ch ==   n ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) { n     val = val * 10 + ch -  0 ; n     ch = getchar(); n   } n   return val; n } n int main() { n   int n, ans = 0; n   n = fio(); n   for (int i = 0; i < n; i++) { n     x[i] = fio(), y[i] = fio(); n   } n   map<pair<double, double>, int> mid_points; n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j < n; j++) { n       double mid_x = (x[i] + x[j]) / 2; n       double mid_y = (y[i] + y[j]) / 2; n       mid_points[make_pair(mid_x, mid_y)]++; n     } n   } n   map<pair<double, double>, int>::iterator it = mid_points.begin(); n   while (it != mid_points.end()) { n     ans += it->second * (it->second - 1) / 2; n     it++; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, b, p; n   cin >> n >> b >> p; n   int m = n; n   long long int nb = 0; n   while (n > 1) { n     for (int i = 8; i >= 0; i--) { n       if (pow(2, i) <= n) { n         nb += b * pow(2, i) + pow(2, i) / 2; n         n = n - (pow(2, i)) + pow(2, i) / 2; n         break; n       } n     } n   } n   cout << nb <<     << m * p; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int v, int vv) { return vv == 0 ? v : gcd(vv, v % vv); } n int main() { n   int powV1, powV2, v[101], v2[101], vv, vv2, x; n   cin >> powV1 >> powV2; n   for (int i = 0; i <= powV1; i++) { n     cin >> v[i]; n   } n   for (int i = 0; i <= powV2; i++) { n     cin >> v2[i]; n   } n   for (int i = 0; i < powV1; i++) { n     if (v[i] != 0) { n       vv = v[i]; n       break; n     } n   } n   for (int i = 0; i < powV2; i++) { n     if (v2[i] != 0) { n       vv2 = v2[i]; n       break; n     } n   } n   if (powV1 == 0) vv = v[0]; n   if (powV2 == 0) vv2 = v2[0]; n   if (powV1 > powV2) { n     if (vv * vv2 < 0) n       cout <<  -Infinity  << endl; n     else n       cout <<  Infinity  << endl; n   } else if (powV1 < powV2) { n     cout <<  0/1  << endl; n   } else { n     x = gcd(abs(vv), abs(vv2)); n     if (vv2 < 0) { n       vv2 *= -1; n       vv *= -1; n     } n     cout << vv / x <<  /  << vv2 / x << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 2002; n int a[N][N] = {0}, b[N][N] = {0}; n int c[N] = {0}, d[N] = {0}, e[N] = {0}, f[N] = {0}; n int main() { n   int n, m, ans = 0; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n       b[i][j] = a[i][j]; n       if (a[i][j] == 0) { n         if (c[i] == 1) ans++; n         if (d[j] == 1) ans++; n       } else { n         c[i] = 1, d[j] = 1; n       } n     } n   } n   for (int i = n; i >= 1; i--) { n     for (int j = m; j >= 1; j--) { n       if (b[i][j] == 0) { n         if (e[i] == 1) ans++; n         if (f[j] == 1) ans++; n       } else { n         e[i] = 1, f[j] = 1; n       } n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n vector<int> c[N]; n vector<int> s[20][N]; n int dp[20][N]; n int h[N]; n vector<int> v[N]; n int sh(int x, int y) { n   for (int i = 17; i + 1; --i) n     if (y & (1 << i)) x = dp[i][x]; n   return x; n } n int ac(int x, int y) { n   if (h[x] > h[y]) n     x = sh(x, h[x] - h[y]); n   else n     y = sh(y, h[y] - h[x]); n   for (int i = 17; i + 1 && x != y; --i) n     if (dp[i][x] != dp[i][y]) x = dp[i][x], y = dp[i][y]; n   if (x == y) n     return x; n   else n     return dp[0][x]; n } n void dfs(int prec, int node) { n   h[node] = h[prec] + 1; n   dp[0][node] = prec; n   s[0][node] = c[node]; n   for (auto it : v[node]) n     if (it != prec) dfs(node, it); n } n vector<int> get(vector<int> a, vector<int> b) { n   if (a.empty()) return b; n   if (b.empty()) return a; n   int *p = new int[a.size() + b.size()]; n   merge(a.begin(), a.end(), b.begin(), b.end(), p); n   vector<int> as; n   for (int *r = p, cn = 1; cn <= a.size() + b.size() && cn <= 10; ++r, ++cn) n     as.push_back(*r); n   while (as.size() && !(*as.begin())) as.erase(as.begin()); n   return as; n } n int main(void) { n   int n, m, q; n   scanf( %d%d%d n , &n, &m, &q); n   int a, b; n   for (int i = 1; i < n; ++i) { n     scanf( %d%d , &a, &b); n     v[a].push_back(b); n     v[b].push_back(a); n   } n   for (int i = 1; i <= m; ++i) { n     scanf( %d , &a); n     c[a].push_back(i); n   } n   for (int i = 1; i <= n; ++i) { n     sort(c[i].begin(), c[i].end()); n     while (c[i].size() > 10) c[i].erase(c[i].end() - 1); n   } n   dfs(0, 1); n   for (int i = 1; i < 18; ++i) n     for (int j = 1; j <= n; ++j) { n       dp[i][j] = dp[i - 1][dp[i - 1][j]]; n       s[i][j] = get(s[i - 1][j], s[i - 1][dp[i - 1][j]]); n     } n   while (q--) { n     int a, b, mx; n     scanf( %d%d%d , &a, &b, &mx); n     int anc = ac(a, b); n     int disa = h[a] - h[anc]; n     int disb = h[b] - h[anc]; n     vector<int> ans; n     for (int i = 17; i + 1; --i) n       if ((1 << i) & disa) { n         ans = get(ans, s[i][a]); n         a = dp[i][a]; n         while (ans.size() > mx) ans.erase(ans.end() - 1); n       } n     for (int i = 17; i + 1; --i) n       if ((1 << i) & disb) { n         ans = get(ans, s[i][b]); n         b = dp[i][b]; n         while (ans.size() > mx) ans.erase(ans.end() - 1); n       } n     ans = get(ans, c[anc]); n     while (ans.size() > mx) ans.erase(ans.end() - 1); n     printf( %d  , ans.size()); n     for (auto it : ans) printf( %d  , it); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxt = 300001; n typedef int mang[maxt + 10]; n struct mystr { n   int val[4]; n }; n mang bit1, bit2; n mystr a[100010]; n int n, dem, vt = 1; n long long d, m; n vector<pair<long long, pair<int, int>>> b; n int kq[100010]; n vector<int> s; n int getbit(int x, mang bit) { n   int res = -maxt; n   for (; x > 0; x -= x & (-x)) res = max(res, bit[x]); n   return res; n } n void updatebit(int x, int val, mang& bit) { n   for (; x <= maxt; x += x & (-x)) bit[x] = max(bit[x], val); n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> d; n   for (int i = 1; i <= n; i++) { n     cin >> m; n     b.push_back({m, {i, 1}}); n     b.push_back({m - d, {i, 2}}); n     b.push_back({m + d, {i, 3}}); n   } n   b.push_back({(int)-1e18, {0, 0}}); n   sort(b.begin(), b.end()); n   for (int i = 1; i < b.size(); i++) { n     if (b[i].first != b[i - 1].first) dem++; n     a[b[i].second.first].val[b[i].second.second] = dem; n   } n   for (int i = 1; i <= n; i++) { n     int longt = n         max(getbit(a[i].val[2], bit1), getbit(maxt - a[i].val[3], bit2)) + 1; n     updatebit(a[i].val[1], longt, bit1); n     updatebit(maxt - a[i].val[1], longt, bit2); n     kq[i] = longt; n     if (kq[i] >= kq[vt]) vt = i; n   } n   cout << kq[vt] <<   n ; n   for (int i = vt; i >= 1; i--) n     if (kq[vt] == kq[i] + 1 && n         (a[i].val[1] <= a[vt].val[2] || a[i].val[1] >= a[vt].val[3])) { n       s.push_back(vt); n       vt = i; n     } n   cout << vt <<    ; n   for (int i = s.size() - 1; i >= 0; i--) cout << s[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool used[2011]; n int b[2011]; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   int cand; n   for (int i = 0; i < n; i++) { n     int cnt = 0; n     for (int j = n; j; j--) { n       if (used[j + k]) cnt++; n       if (!used[j]) n         if (b[j] == cnt) cand = j; n     } n     cout << cand <<    ; n     used[cand] = true; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_VALUE = 32768; n struct Tokenizer { n   string input; n   int at; n   void tokenize(const string& s) { n     input = s +  @ ; n     at = 0; n     token =   ; n   } n   char nextChar() { return input[at]; } n   char nextNoneSpace() { n     while (nextChar() ==    ) skipChar(); n     return nextChar(); n   } n   void skipChar() { ++at; } n   string token; n   string nextToken() { n     if (!token.empty()) return token; n     char ch = nextNoneSpace(); n     if (isdigit(ch)) { n       while (isdigit(nextChar())) { n         token += nextChar(); n         skipChar(); n       } n     } else if (isalpha(ch)) { n       while (isalpha(nextChar())) { n         token += nextChar(); n         skipChar(); n       } n     } else { n       token = nextChar(); n       skipChar(); n     } n     return token; n   } n   void skipToken() { token =   ; } n   bool hasMoreTokens() { return at < input.size(); } n }; n Tokenizer tokenizer; n vector<string> tokens; n string input; n int what; n int n; n void readInput() { n   cin >> what; n   string line; n   input =   ; n   while (getline(cin, line)) input += line; n } n void tokenizeInput() { n   tokenizer.tokenize(input); n   while (tokenizer.hasMoreTokens()) { n     tokens.push_back(tokenizer.nextToken()); n     tokenizer.skipToken(); n   } n } n int result[MAX_VALUE]; n int at; n const string& nextToken() { return tokens[at]; } n void skipToken() { ++at; } n int parseFunction(int); n int parseOperatorSequence(); n int parseOperator(); n int parseLogicalExpr(); n int parseArithmExpr(); n int parseSum(); n int parseProduct(); n int parseMultiplier(); n int parseNumber(); n void eat(const string& s) { skipToken(); } n int parseFunction() { n   eat( int ); n   eat( f ); n   eat( ( ); n   eat( int ); n   eat( n ); n   eat( ) ); n   eat( { ); n   int e = parseOperatorSequence(); n   return e; n } n int parseOperatorSequence() { n   while (nextToken() !=  } ) { n     int e = parseOperator(); n     if (e != -1) return e; n   } n   return -1; n } n int parseOperator() { n   if (nextToken() ==  return ) { n     skipToken(); n     return parseArithmExpr(); n   } else { n     eat( if ); n     eat( ( ); n     int ok = parseLogicalExpr(); n     eat( ) ); n     eat( return ); n     int res = parseArithmExpr(); n     eat( ; ); n     if (ok) n       return res; n     else n       return -1; n   } n } n int parseLogicalExpr() { n   int a = parseArithmExpr(); n   string token = nextToken(); n   if (token ==  > ) { n     eat( > ); n     int b = parseArithmExpr(); n     return a > b; n   } else if (token ==  < ) { n     eat( < ); n     int b = parseArithmExpr(); n     return a < b; n   } else { n     eat( = ); n     eat( = ); n     int b = parseArithmExpr(); n     return a == b; n   } n } n inline int moduleAdd(int a, int b) { return (a + b) % MAX_VALUE; } n inline int moduleMinus(int a, int b) { n   a -= b; n   if (a < 0) a += MAX_VALUE; n   return a; n } n inline int moduleMult(int a, int b) { return (a * b) % MAX_VALUE; } n inline int moduleDiv(int a, int b) { n   if (!b) return -1; n   return a / b; n } n int parseArithmExpr() { return parseSum(); } n int parseSum() { n   int e = parseProduct(); n   while (nextToken() ==  +  || nextToken() ==  - ) { n     string token = nextToken(); n     skipToken(); n     if (token ==  + ) n       e = moduleAdd(e, parseProduct()); n     else n       e = moduleMinus(e, parseProduct()); n   } n   return e; n } n int parseProduct() { n   int e = parseMultiplier(); n   while (nextToken() ==  *  || nextToken() ==  / ) { n     string token = nextToken(); n     skipToken(); n     if (token ==  * ) n       e = moduleMult(e, parseMultiplier()); n     else n       e = moduleDiv(e, parseMultiplier()); n   } n   return e; n } n int parseMultiplier() { n   if (nextToken() ==  n ) { n     skipToken(); n     return n; n   } else if (nextToken() ==  f ) { n     eat( f ); n     eat( ( ); n     int arg = parseArithmExpr(); n     eat( ) ); n     return result[arg]; n   } else { n     istringstream iss(nextToken()); n     skipToken(); n     int num; n     iss >> num; n     return num; n   } n } n int main() { n   readInput(); n   tokenizeInput(); n   int ans = -1; n   for (int i = 0; i < MAX_VALUE; i++) { n     at = 0; n     n = i; n     result[i] = parseFunction(); n     if (result[i] == what) ans = i; n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = acos(-1); n const long long INF = 1e14; n long long gcd(long long a, long long b) { n   while (b) { n     a = a % b; n     swap(a, b); n   } n   return a; n } n int main() { n   long long n, m, k; n   cin >> n >> m >> k; n   if ((n * m * 2) % k != 0 || k == 1) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES  n        <<   n ; n   if (k % 2 == 0) { n     k = k / 2; n     long long g = gcd(n, k); n     n = n / g; n     k = k / g; n     g = gcd(m, k); n     m = m / g; n     k = k / g; n     cout << 0 <<     << 0 <<   n ; n     cout << n <<     << 0 <<   n ; n     cout << 0 <<     << m <<   n ; n     return 0; n   } n   long long g = gcd(n, k); n   if (g > 1) { n     n = (n / g) * 2; n     k = k / g; n     g = gcd(m, k); n     m = m / g; n     k = k / g; n   } else { n     n = (n / g); n     k = k / g; n     g = gcd(m, k); n     m = (m / g) * 2; n     k = k / g; n   } n   cout << 0 <<     << 0 <<   n ; n   cout << n <<     << 0 <<   n ; n   cout << 0 <<     << m <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_NUMBER = 105; n int flower[MAX_NUMBER], odd[MAX_NUMBER], even[MAX_NUMBER]; n int number; n int main() { n   scanf( %d , &number); n   int sums = 0; n   int odd_number = 0, even_number = 0; n   for (int i = 1; i <= number; i++) { n     scanf( %d , &flower[i]); n     sums = sums + flower[i]; n     if (flower[i] % 2 == 1) { n       odd[odd_number++] = flower[i]; n     } else { n       even[even_number++] = flower[i]; n     } n   } n   if (odd_number == 0) { n     printf( 0 n ); n   } else { n     if (odd_number % 2 == 1) { n       printf( %d n , sums); n     } else { n       sort(odd, odd + odd_number); n       printf( %d n , sums - odd[0]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[2005], b[2005]; n vector<pair<long long, pair<int, int> > > bb; n long long diff = 0L; n bool good(long long x, int place) { n   if (place >= bb.size()) n     return false; n   else n     return diff + 2 * (bb[place].first - x) <= 0; n } n int find_(long long x) { n   int place = -1, jump = bb.size(); n   while (jump > 0) { n     if (good(x, place + jump)) place += jump; n     jump /= 2; n   } n   while (good(x, place + 1)) place++; n   return place; n } n bool sorter(pair<long long, pair<int, int> > a, n             pair<long long, pair<int, int> > b) { n   return a.first < b.first; n } n int main() { n   int n; n   scanf( %i , &n); n   for (int e = 0; e < n; e++) scanf( %I64d , &a[e]); n   int m; n   scanf( %i , &m); n   for (int e = 0; e < m; e++) scanf( %I64d , &b[e]); n   for (int e = 0; e < n; e++) diff += a[e]; n   for (int e = 0; e < m; e++) diff -= b[e]; n   long long acc = diff; n   vector<pair<int, int> > rr; n   for (int e = 0; e < n; e++) { n     for (int ee = 0; ee < m; ee++) { n       if (abs(diff + 2 * (b[ee] - a[e])) < abs(acc)) { n         acc = diff + 2 * (b[ee] - a[e]); n         rr.clear(); n         rr.push_back(make_pair(e, ee)); n       } n     } n   } n   for (int e = 0; e < m; e++) { n     for (int ee = e + 1; ee < m; ee++) { n       bb.push_back(make_pair(b[e] + b[ee], make_pair(e, ee))); n     } n   } n   sort(bb.begin(), bb.end(), sorter); n   for (int e = 0; e < n; e++) { n     for (int ee = e + 1; ee < n; ee++) { n       long long itm = a[e] + a[ee]; n       int pl = find_(itm); n       if (pl < (int)bb.size() - 1) { n         if (abs(diff + 2 * (bb[pl + 1].first - itm)) < abs(acc)) { n           acc = diff + 2 * (bb[pl + 1].first - itm); n           rr.clear(); n           rr.push_back(make_pair(e, bb[pl + 1].second.first)); n           rr.push_back(make_pair(ee, bb[pl + 1].second.second)); n         } n       } n       if (pl > -1) { n         if (abs(diff + 2 * (bb[pl].first - itm)) < abs(acc)) { n           acc = diff + 2 * (bb[pl].first - itm); n           rr.clear(); n           rr.push_back(make_pair(e, bb[pl].second.first)); n           rr.push_back(make_pair(ee, bb[pl].second.second)); n         } n       } n     } n   } n   printf( %I64d n%i n , abs(acc), rr.size()); n   for (auto e : rr) printf( %i %i n , e.first + 1, e.second + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int l, r; n   cin >> l >> r; n   for (int i = l; i <= r; ++i) { n     int d[10] = {}; n     int c = i; n     bool ok = true; n     while (c) { n       if (d[c % 10]) { n         ok = false; n         break; n       } n       d[c % 10] += 1; n       c /= 10; n     } n     if (ok) { n       cout << i <<   n ; n       return 0; n     } n   } n   cout <<  -1 n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long dx[] = {-1, 0, 1, 0}; n long long dy[] = {0, 1, 0, -1}; n long long spf[1]; n void sieve() { n   long long i, j; n   for (i = 0; i < 1; i++) spf[i] = i; n   for (i = 4; i < 1; i += 2) spf[i] = 2; n   for (i = 3; i * i < 1; i++) { n     if (spf[i] == i) { n       for (j = i * i; j < 1; j += i) { n         if (spf[j] == j) spf[j] = i; n       } n     } n   } n } n bool isvowel(char c) { n   switch (c) { n     case  a : n     case  e : n     case  i : n     case  o : n     case  u : n     case  y : n       return true; n     default: n       return false; n   } n } n bool isPrime(long long n) { n   for (long long i = 2; i <= sqrt(n); i++) { n     if (n % i == 0) { n       return false; n     } n   } n   return true; n } n long long sum(long long n) { n   long long ans = 0; n   while (n > 0) { n     long long d = n % 10; n     ans += d; n     n /= 10; n   } n   return ans; n } n vector<long long> adj[200005]; n long long col[200005]; n pair<long long, long long> e[200005]; n long long bfs(long long node) { n   col[node] = 1; n   queue<long long> q; n   q.push(node); n   while (!q.empty()) { n     long long curr = q.front(); n     q.pop(); n     for (auto child : adj[curr]) { n       if (col[child] == -1) { n         col[child] = 1 - col[curr]; n         q.push(child); n       } else if (col[child] == col[curr]) { n         return false; n       } n     } n   } n   return true; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     for (long long i = 1; i <= m; i++) { n       long long u, v; n       cin >> u >> v; n       adj[u].push_back(v); n       adj[v].push_back(u); n       e[i] = {u, v}; n     } n     memset(col, -1, sizeof(col)); n     for (long long i = 1; i <= n; i++) { n       if (col[i] == -1) { n         if (bfs(i) == false) { n           cout <<  NO  n                <<   n ; n           return 0; n         } n       } n     } n     cout <<  YES  n          <<   n ; n     vector<long long> v; n     for (long long i = 1; i <= n; i++) { n       if (col[i] == 1) { n         v.push_back(i); n       } n     } n     for (long long i = 1; i <= m; i++) { n       if (col[e[i].first] == 0) { n         cout <<  0 ; n       } else { n         cout <<  1 ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const int inf = 1e9; n const long double eps = 1e-7; n const long double pi = acos(-1); n const int dx[8] = {0, 2, 1, 0, -2, -1, 1, 2}; n const int dy[8] = {2, 0, 1, -2, 0, -1, 2, 1}; n const int N = 23; n const int MAXN = 1e5; n const int mod = 1e9 + 7; n int g[N]; n int dp[(1 << N)], p[(1 << N)]; n int main() { n   int n, m; n   cin >> n >> m; n   if (m == n * (n - 1) / 2) { n     cout << 0; n     return 0; n   } n   for (int i = 1; i <= m; i++) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     g[u] |= (1 << v); n     g[v] |= (1 << u); n   } n   for (int i = 0; i < n; i++) { n     g[i] |= (1 << i); n   } n   for (int i = 0; i < (1 << n); i++) { n     dp[i] = inf; n   } n   dp[0] = 0; n   for (int i = 0; i < n; i++) { n     dp[g[i]] = 1; n     p[g[i]] = 0; n   } n   for (int mask = 1; mask < (1 << n); mask++) { n     if (dp[mask] == inf) continue; n     for (int i = 0; i < n; i++) { n       if ((mask >> i) & 1) { n         int nmask = (mask | g[i]); n         if (dp[nmask] > dp[mask] + 1) { n           dp[nmask] = dp[mask] + 1; n           p[nmask] = mask; n         } n       } n     } n   } n   int mask = (1 << n) - 1; n   vector<int> ans; n   cout << dp[mask] <<   n ; n   while (mask) { n     int pmask = p[mask]; n     if (p[mask] == 0) { n       for (int i = 0; i < n; i++) { n         if (mask == g[i]) { n           ans.push_back(i); n           break; n         } n       } n       break; n     } n     for (int i = 0; i < n; i++) { n       if ((pmask >> i) & 1) { n         int nmask = (pmask | g[i]); n         if (nmask == mask) { n           ans.push_back(i); n           break; n         } n       } n     } n     mask = pmask; n   } n   reverse(ans.begin(), ans.end()); n   for (int i = 0; i < (int)ans.size(); i++) { n     cout << ans[i] + 1 <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string slv(string s) { n   int sz = s.size(); n   if (sz % 2 == 1) n     return s; n   else { n     string a = slv(s.substr(0, sz / 2)); n     string b = slv(s.substr(sz / 2, sz)); n     return a < b ? a + b : b + a; n   } n } n int main() { n   string a, b; n   cin >> a >> b; n   cout << ((slv(a) == slv(b)) ?  YES  :  NO ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, n, ans; n   while (cin >> n >> a) { n     ans = (a / 2) + 1; n     if (a % 2 == 0) { n       ans = ans - 1; n       ans = (n / 2) - ans; n       ans = ans + 1; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, b; n   cin >> n >> m; n   vector<int> ct(0); n   if (m == 1) { n     for (int j = 0; j < n; j++) cout << 1 << endl; n     return 0; n   } n   if (m == 2) { n     ct.push_back(1); n     ct.push_back(2); n     for (int i = 0; i < n; i++) cout << ct[i % 2] << endl; n     return 0; n   } n   if (m % 2 != 0) { n     ct.push_back((m + 1) / 2); n     int l, r; n     l = (m + 1) / 2; n     r = (m + 1) / 2; n     while (l != 1) { n       l--; n       ct.push_back(l); n       r++; n       ct.push_back(r); n     } n     for (int i = 0; i < n; i++) cout << ct[i % m] << endl; n   } else if (m % 2 == 0) { n     ct.push_back((m + 1) / 2); n     int l, r; n     l = (m + 1) / 2; n     r = (m + 1) / 2; n     while (l != 1) { n       r++; n       ct.push_back(r); n       l--; n       ct.push_back(l); n     } n     ct.push_back(m); n     for (int i = 0; i < n; i++) cout << ct[i % m] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[10003], v1[10003]; n int cnt[10003]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m, i, j; n   cin >> m >> n; n   for (i = 0; i < m; i++) { n     int x, y; n     cin >> x; n     memset(cnt, 0, sizeof(cnt)); n     for (j = 0; j < x; j++) { n       cin >> y; n       v[i].push_back(y); n       cnt[y]++; n     } n     for (j = 1; j <= n; j++) { n       if (!cnt[j]) v1[i].push_back(j); n     } n     sort(v[i].begin(), v[i].end()); n     sort(v1[i].begin(), v1[i].end()); n   } n   for (i = 0; i < m; i++) n     for (j = 0; j < m; j++) { n       int fl = 1; n       for (int k = 0; k < v[i].size(); k++) { n         if (!binary_search(v1[j].begin(), v1[j].end(), v[i][k])) fl = 0; n       } n       if (fl) { n         cout <<  impossible ; n         return 0; n       } n     } n   cout <<  possible ; n } n


#include <bits/stdc++.h> n using namespace std; n int a[505]; n int sorted_a[505]; n void rot(int i) { n   i--; n   int temp = a[i + 2]; n   a[i + 2] = a[i + 1]; n   a[i + 1] = a[i]; n   a[i] = temp; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int cases; n   cin >> cases; n   while (cases--) { n     int n; n     cin >> n; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       sorted_a[i] = a[i]; n     } n     sort(sorted_a, sorted_a + n); n     vector<int> ans; n     for (int i = n - 1; i > 1; i--) { n       int j = i; n       while (a[j] != sorted_a[i]) j--; n       if (j == 0) { n         ans.push_back(1); n         rot(1); n         j++; n       } n       for (; j < i; j++) { n         ans.push_back(j); n         rot(j); n       } n     } n     if (a[0] != sorted_a[0]) { n       if (a[0] == a[2]) { n         ans.push_back(1); n         ans.push_back(1); n         rot(1); n         rot(1); n       } else { n         for (int i = 2; i < n - 1; i++) { n           if (a[i] == a[i + 1]) { n             for (int j = i; j >= 1; j--) { n               ans.push_back(j); n               rot(j); n             } n             break; n           } n         } n       } n     } n     if (a[0] == sorted_a[0]) { n       cout << ans.size() <<   n ; n       for (int i : ans) cout << i <<    ; n       cout <<   n ; n     } else n       cout <<  -1 n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int maxim = 0, sum = 0, a[n]; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       maxim = max(maxim, a[i]); n       sum += a[i]; n     } n     if (maxim > sum / 2) { n       cout <<  T n ; n       continue; n     } n     if (sum % 2 == 0) n       cout <<  HL n ; n     else n       cout <<  T n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool isPrime(int n) { n   if (n <= 1) return false; n   for (int i = 2; i < n; i++) n     if (n % i == 0) return false; n   return true; n } n int odd(int arr[], int n) { n   for (int i = 0; i < n; i++) { n     if (arr[i] % 2 != 0) { n       return i; n     } n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int arr[n]; n     for (int i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     int sum = 0; n     for (int i = 0; i < n; i++) { n       sum += arr[i]; n     } n     int fodd = odd(arr, n); n     if (isPrime(sum)) { n       cout << n - 1 << endl; n       for (int i = 0; i < n; i++) { n         if (i == fodd) { n           continue; n         } n         cout << (i + 1) <<    ; n       } n       cout << endl; n     } n     if (!(isPrime(sum))) { n       cout << n << endl; n       for (int i = 0; i < n; i++) { n         cout << (i + 1) <<    ; n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   string s1 =   ; n   if (n % 2 == 0) { n     for (int i = 0; i < n; i++) { n       cout << s[i] << s[i + 1]; n       i++; n       if (i < n - 2) { n         cout <<  - ; n       } n     } n   } else { n     if (n == 3) n       cout << s << endl; n     else { n       for (int i = 0; i < n; i++) { n         if (i == 0) { n           cout << s[i] << s[i + 1] << s[i + 2] <<  - ; n           i += 2; n           continue; n         } else { n           cout << s[i] << s[i + 1]; n         } n         if (i < n - 2) { n           cout <<  - ; n         } n         i++; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n long long int fastPow(long long int x, long long int y) { n   long long int p = mod; n   long long int res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long int arr[200005]; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cin.tie(0), cout.tie(0), n       cout.precision(15); n   long long int n, m, A, res = 1; n   cin >> n >> m >> A; n   for (long long int i = 1; i <= m; i++) { n     cin >> arr[i]; n   } n   res = fastPow(A, n - 2 * arr[m]); n   for (long long int i = m - 1; i >= 0; i--) { n     long long int tmp = fastPow(A, arr[i + 1] - arr[i]) % mod; n     long long int tmp2 = (tmp * ((tmp - 1 + mod) % mod) / 2) % mod; n     long long int tmp3 = (tmp + tmp2) % mod; n     res = (res * tmp3) % mod; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long p[10005]; n long long s[10005]; n long long dp[2][10005]; n int n; n long long m; n int main() { n   scanf( %d%I64d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %I64d , &p[i]); n   for (int i = 1; i <= n; i++) scanf( %I64d , &s[i]); n   int now = 0, next = 1; n   for (int i = 1; i <= n; i++) { n     memset(dp[next], 0, sizeof(dp[next])); n     for (int j = 0; j <= i; j++) { n       if (j == 0) { n         dp[next][j] = dp[now][j] + p[i]; n       } else if (j == i) { n         dp[next][j] = dp[now][j - 1] + s[i]; n       } else { n         dp[next][j] = min(dp[now][j] + p[i] + j * m, dp[now][j - 1] + s[i]); n       } n     } n     swap(now, next); n   } n   long long ans = dp[now][0]; n   for (int i = 1; i <= n; i++) ans = min(ans, dp[now][i]); n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a1, a2, b1, b2, c1, c2; n int main() { n   scanf( %d%d%d , &a1, &b1, &c1); n   scanf( %d%d%d , &a2, &b2, &c2); n   if (a1 == 0 && b1 == 0) { n     if (c1 != 0) { n       puts( 0 ); n       return 0; n     } n     if (a2 == 0 && b2 == 0 && c2 != 0) n       puts( 0 ); n     else n       puts( -1 ); n     return 0; n   } n   if (a2 == 0 && b2 == 0) { n     if (c2 != 0) { n       puts( 0 ); n       return 0; n     } n     if (a1 == 0 && b1 == 0 && c1 != 0) n       puts( 0 ); n     else n       puts( -1 ); n     return 0; n   } n   if (a1 * b2 == a2 * b1) { n     if (c1 * b2 == c2 * b1 && c1 * a2 == c2 * a1) n       puts( -1 ); n     else n       puts( 0 ); n   } else n     puts( 1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b, p; n     cin >> a >> b >> p; n     string s; n     cin >> s; n     int len = s.size(); n     int pos = 0; n     char ss =  C ; n     for (int i = len - 2; i >= 0; i--) { n       if (ss != s[i]) { n         p -= (s[i] ==  A  ? a : b); n         ss = s[i]; n         if (p < 0) { n           pos = i + 1; n           break; n         } n       } n     } n     cout << pos + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int col[n + 2], k[m + 2]; n   for (int i = 1; i <= n; i++) cin >> col[i]; n   for (int i = 1; i <= m; i++) cin >> k[i]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       vector<int> v[105]; n       int cnt = 0; n       for (int kk = i; kk <= j; kk++) v[col[kk]].push_back(1); n       for (int kk = 1; kk <= m; kk++) { n         if (v[kk].size() != k[kk]) goto damn; n       } n       cout <<  YES n ; n       return 0; n     damn:; n     } n   } n   cout <<  NO n ; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); n   return x * f; n } n int n, m; n vector<int> G[20], G2[20]; n int low[20], dfn[20]; n int cur; n bool tarjan(int u, int fa) { n   bool flg = 0; n   low[u] = dfn[u] = ++cur; n   bool flg2 = 1; n   for (auto v : G2[u]) { n     if (!dfn[v]) { n       if (!tarjan(v, u)) flg2 = 0; n       low[u] = min(low[u], low[v]); n       if (dfn[v] == low[v]) flg2 = 0; n     } else if (v != fa || flg) n       low[u] = min(low[u], dfn[v]); n     else n       flg = 1; n   } n   return flg2; n } n vector<pair<int, int> > del; n int curcnt; n int ans = 0x3f3f3f3f; n vector<int> Gans[20]; n void delete_edge(int x) { n   if (!x) return; n   if (G2[x].empty()) return; n   int rrrnd = (rand() % G2[x].size() + G2[x].size()) % G2[x].size(); n   int y = G2[x][rrrnd]; n   del.emplace_back(x, y); n   G2[x].erase(find(G2[x].begin(), G2[x].end(), y)); n   G2[y].erase(find(G2[y].begin(), G2[y].end(), x)); n   memset(low, 0, sizeof low); n   memset(dfn, 0, sizeof dfn); n   cur = 0; n   if (!tarjan(1, -1)) { n   Ret: n     G2[x].push_back(y); n     G2[y].push_back(x); n     del.pop_back(); n     return; n   } n   for (int i = 1; i <= n; i++) n     if (!dfn[i]) goto Ret; n   curcnt--; n   if (curcnt < ans) { n     ans = curcnt; n     for (int i = 1; i <= n; i++) Gans[i] = G2[i]; n   } n } n void add_edge(int x) { n   if (del.empty()) return; n   G2[del[x].first].push_back(del[x].second); n   G2[del[x].second].push_back(del[x].first); n   del.erase(del.begin() + x); n   curcnt++; n } n int main() { n   srand(19260817); n   int stt = clock(); n   n = read(), m = read(); n   for (int i = 1, x, y; i <= m; i++) { n     x = read(), y = read(); n     G[x].push_back(y); n     G[y].push_back(x); n   } n   curcnt = ans = m; n   for (int i = 1; i <= n; i++) Gans[i] = G[i]; n   const int max_time = 1950; n   for (int _ = 1; _ <= 1000; _++) { n     for (int i = 1; i <= n; i++) G2[i] = G[i]; n     curcnt = m; n     for (int __ = 1; __ <= 1000; __++) { n       if (rand() % 4) n         delete_edge((rand() % n + n) % n + 1); n       else if (!del.empty()) { n         add_edge(rand() % del.size()); n       } n     } n     if (clock() - stt > max_time) break; n   } n   printf( %d n , ans); n   for (int i = 1; i <= n; i++) n     for (int j = 0; j < Gans[i].size(); j++) n       if (Gans[i][j] > i) printf( %d %d n , i, Gans[i][j]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void chkmin(T &x, T y) { n   x = min(x, y); n } n template <typename T> n inline void chkmax(T &x, T y) { n   x = max(x, y); n } n template <typename T> n inline void read(T &x) { n   T f = 1; n   x = 0; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -f; n   for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c -  0 ; n   x *= f; n } n int main() { n   int q; n   read(q); n   while (q--) { n     long long l, r; n     read(l); n     read(r); n     long long ans = (l + r) * (r - l + 1) / 2; n     if (!(r & 1)) --r; n     if (!(l & 1)) ++l; n     if (l > r) n       cout << ans <<   n ; n     else n       cout << ans - (l + r) * ((r - l) / 2 + 1) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n template <class T> n inline void gmax(T &a, T b) { n   if (b > a) a = b; n } n template <class T> n inline void gmin(T &a, T b) { n   if (b < a) a = b; n } n using namespace std; n const int N = 1e5 + 10, M = 2e6 + 10, Z = 1e9 + 7, maxint = 2147483647, n           ms1 = 16843009, ms31 = 522133279, ms63 = 1061109567, n           ms127 = 2139062143; n const double PI = acos(-1.0), eps = 1e-8; n void fre() { n   freopen( /Users/luras/Desktop/in.txt ,  r , stdin); n   freopen( /Users/luras/Desktop/out.txt ,  w , stdout); n } n const int INF = 1e9; n int casenum, casei; n const int top = 1e5, top2 = top + top; n int g, n, m, b[N], c[N]; n struct A { n   int typ, p, t, o, v; n   bool operator<(const A &b) const { return p < b.p; } n } a[N]; n pair<int, int> ans[N]; n vector<int> sot1[N + top], sot2[N + top]; n int main() { n   while (~scanf( %d%d%d , &g, &n, &m)) { n     for (int i = 1; i <= g; i++) { n       scanf( %d%d%d , &a[i].typ, &a[i].p, &a[i].t); n       a[i].o = i; n     } n     sort(a + 1, a + g + 1); n     for (int i = 1; i <= g; i++) { n       a[i].v = a[i].t - a[i].p; n       if (a[i].typ == 1) n         ans[a[i].o] = {a[i].p, m}; n       else n         ans[a[i].o] = {n, a[i].p}; n       if (a[i].typ == 2) n         sot2[a[i].v + top].push_back(a[i].o); n       else n         sot1[a[i].v + top].push_back(a[i].o); n     } n     for (int i = 1; i <= top2; i++) { n       int t1 = sot1[i].size(), t2 = sot2[i].size(), t3 = min(t1, t2); n       for (int j = t1 - 1, k = 0; j >= t1 - t3; j--, k++) { n         b[sot1[i][j]] = sot2[i][k]; n       } n       for (int j = t2 - 1, k = 0; j >= t2 - t3; j--, k++) { n         b[sot2[i][j]] = sot1[i][k]; n       } n       if (t1 > t2) { n         for (int j = 0, k = t3; j < t1 - t3; j++, k++) { n           b[sot1[i][j]] = sot1[i][k]; n         } n       } else if (t2 > t1) { n         for (int j = 0, k = t3; j < t2 - t3; j++, k++) { n           b[sot2[i][j]] = sot2[i][k]; n         } n       } n     } n     for (int i = 1; i <= g; i++) { n       printf( %d %d n , ans[b[i]].first, ans[b[i]].second); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int c = 3002; n int w, n, m, dif[c], rf[c]; n long long sok = 1e15; n pair<long long, long long> op[c][c], cl[c][c]; n vector<int> sz[c]; n bool v[c]; n pair<long long, long long> add(pair<long long, long long> a, n                                pair<long long, long long> b) { n   return {a.first + b.first, a.second + b.second}; n } n void dfs(int a) { n   v[a] = true; n   rf[a] = 1; n   for (int i = 0; i <= n; i++) { n     op[a][i] = {-sok, -sok}; n   } n   op[a][1] = {0, dif[a]}; n   for (int x : sz[a]) { n     if (!v[x]) { n       dfs(x); n       rf[a] += rf[x]; n       for (int db = rf[a]; db >= 1; db--) { n         op[a][db] = max(add(op[a][db], op[x][1]), add(op[a][db - 1], cl[x][1])); n         for (int i = max(2, db - rf[a] + rf[x]); i <= min(rf[x], db); i++) { n           op[a][db] = max({op[a][db], add(op[a][db - i], cl[x][i]), n                            add(op[a][db - i + 1], op[x][i])}); n         } n       } n     } n   } n   for (int i = 1; i <= rf[a]; i++) { n     cl[a][i] = {op[a][i].first + (op[a][i].second > 0), 0}; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> w; n   while (w--) { n     cin >> n >> m; n     for (int i = 1; i <= n; i++) { n       int x; n       cin >> x; n       dif[i] = -x; n     } n     for (int i = 1; i <= n; i++) { n       int x; n       cin >> x; n       dif[i] += x; n     } n     for (int i = 1; i < n; i++) { n       int a, b; n       cin >> a >> b; n       sz[a].push_back(b), sz[b].push_back(a); n     } n     dfs(1); n     cout << cl[1][m].first <<   n ; n     for (int i = 1; i <= n; i++) { n       v[i] = 0; n       sz[i].clear(); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, c; n vector<vector<long long> > a; n long long dp[200000 + 5][2][2]; n long long fun(long long idx, bool state, bool prev) { n   if (idx == 0) return 0; n   if (dp[idx][state][prev]) return dp[idx][state][prev]; n   if (prev || !state) n     return dp[idx][state][prev] = a[idx][state] + min(fun(idx - 1, 0, state), n                                                       fun(idx - 1, 1, state)); n   return dp[idx][state][prev] = n              c + a[idx][state] + n              min(fun(idx - 1, 0, state), fun(idx - 1, 1, state)); n } n void solve() { n   cin >> n >> c; n   a.resize(n + 1, vector<long long>(2)); n   for (long long i = 1; i <= n - 1; i++) cin >> a[i][0]; n   for (long long i = 1; i <= n - 1; i++) cin >> a[i][1]; n   dp[n][0][0] = dp[n][1][0] = min(fun(n - 1, 0, 0), fun(n - 1, 1, 0)); n   for (long long i = 0; i <= n - 1; i++) n     cout << min(dp[i][0][0], dp[i][1][0]) <<    ; n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 6; n const int MSK = 1 << M; n const int N = 45; n int D[45][MSK][MSK]; n int col(int x) { n   int ans = 0; n   while (x) ans += x % 2, x /= 2; n   return ans; n } n int n, m; n bool BIT(int x, int i) { n   if (i < 0) return 0; n   return (x >> i) & 1; n } n bool bad(int a, int b, int c) { n   for (int i = 0; i < m; i++) n     if (!(BIT(a, i) || BIT(c, i) || BIT(b, i - 1) || BIT(b, i) || n           BIT(b, i + 1))) n       return true; n   return false; n } n int main() { n   cin >> n >> m; n   if (n < m) swap(n, m); n   for (int i = 0; i < N; i++) n     for (int j = 0; j < MSK; j++) n       for (int k = 0; k < MSK; k++) D[i][j][k] = 1e9; n   for (int i = 0; i < MSK; i++) { n     D[0][0][i] = col(i); n   } n   for (int i = 1; i <= n; i++) { n     for (int pmsk = 0; pmsk < MSK; pmsk++) n       for (int msk = 0; msk < MSK; msk++) { n         for (int tmsk = 0; tmsk < MSK; tmsk++) { n           if (bad(tmsk, pmsk, msk)) continue; n           D[i][pmsk][msk] = n               min(D[i][pmsk][msk], D[i - 1][tmsk][pmsk] + col(msk)); n         } n       } n   } n   int ans = 1e9; n   for (int msk = 0; msk < MSK; msk++) { n     ans = min(ans, (D[n][msk][0])); n   } n   cout << n * m - ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   set<int> keyboards; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int a; n     scanf( %d , &a); n     keyboards.insert(a); n   } n   int prev = 0; n   int result = 0; n   for (auto keyboard : keyboards) { n     if (prev != 0) result += (keyboard - prev - 1); n     prev = keyboard; n   } n   printf( %d n , result); n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[300010], f[300010], p; n vector<int> x[300010], w; n int main() { n   srand(time(0)); n   int i, j, k, l; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; i++) { n     a[i] = i + 1; n     for (j = i + i; j <= n; j += i) x[j].push_back(i); n   } n   p = n - 1; n   for (i = 1; i <= p; i++) { n     for (j = 2; j * j <= a[i]; j++) n       if (a[i] % j == 0) break; n     if (j * j > a[i] && a[i] * 2 > n) { n       swap(a[i], a[p]); n       p--; n       i--; n     } n   } n   for (int t = 1; t <= 12; t++) { n     for (i = 1; i <= n; i++) f[i] = 0; n     for (i = 1; i <= p; i++) swap(a[i], a[((rand() << 15) + rand()) % i + 1]); n     for (i = 1, k = 0; i <= p && k < m; i++) { n       for (j = a[i], l = 0; j <= n; j += a[i]) n         if (f[j]) l++; n       for (auto j : x[a[i]]) n         if (f[j]) l++; n       if (k + l <= m) { n         k += l; n         f[a[i]] = 1; n         w.push_back(a[i]); n       } n       if (k + w.size() >= m - (n - 1 - p)) { n         k += w.size(); n         w.push_back(1); n         for (j = p + 1; k < m; j++, k++) w.push_back(a[j]); n         break; n       } n     } n     if (k < m && k + w.size() >= m - (n - 1 - p)) { n       k += w.size(); n       w.push_back(1); n       for (j = p + 1; k < m; j++, k++) w.push_back(a[j]); n     } n     if (k == m) { n       printf( Yes n%d n , w.size()); n       for (auto i : w) printf( %d  , i); n       return 0; n     } n     w.clear(); n   } n   printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int L = 1000000 + 10; n int main() { n   int n; n   scanf( %d , &n); n   char s[n + 10]; n   scanf( %s , s); n   int next[n + 10]; n   next[0] = -1; n   int j = 0, k = -1; n   int maxn = 1; n   while (j < n) { n     if (k == -1 || s[j] == s[k]) { n       next[++j] = ++k; n       int xun = j - next[j]; n       maxn = max(maxn, j / xun / 2 * xun); n     } else n       k = next[k]; n   } n   printf( %d n , n - maxn + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string char_to_str(char c) { n   string tem(1, c); n   return tem; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n template <class T> n T rand() { n   return uniform_int_distribution<T>()(rng); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long n, k; n   cin >> n >> k; n   vector<long long> v; n   vector<long long> v2; n   for (long long i = 1; i * i <= n; ++i) { n     if (n % i == 0) { n       v.push_back(i); n       v2.push_back(n / i); n     } n   } n   reverse(v2.begin(), v2.end()); n   for (auto r : v2) v.push_back(r); n   long long f = 0; n   for (long long i = v.size() - 1; i >= 0; --i) { n     long long t = n / v[i]; n     if (k > 100000000 || (k * (k + 1)) / 2 > t) { n       continue; n     } n     for (long long j = 1; j < k; ++j) { n       t -= j; n       cout << j * v[i] <<    ; n     } n     cout << t * v[i]; n     return 0; n   } n   cout <<  -1 ; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5; n int n, fa[N], p[N], cnt, mk, la[N], size[N]; n int getfa(int x) { n   if (fa[x] == x) return x; n   return fa[x] = getfa(fa[x]); n } n void merge(int x, int y) { n   int fx = getfa(x), fy = getfa(y); n   if (fx == fy) return; n   fa[fx] = fy; n   size[fy] += size[fx]; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   for (int i = 1; i <= n; i++) { n     fa[i] = i; n     size[i] = 1; n     if (p[i] == i) { n       if (!mk) n         mk = i; n       else n         p[i] = mk, cnt++; n     } n   } n   if (!mk) { n     for (int i = 1; i <= n; i++) merge(i, p[i]); n     int now = 0; n     for (int i = 1; i <= n; i++) { n       if (size[i] > now) { n         now = size[i]; n         mk = i; n       } n     } n     for (int i = 1; i <= n; i++) fa[i] = i; n     p[mk] = mk; n     cnt++; n   } n   for (int i = 1; i <= n; i++) { n     if (p[i] == i) continue; n     if (getfa(i) == getfa(p[i])) p[i] = mk, cnt++; n     merge(i, p[i]); n   } n   cout << cnt << endl; n   for (int i = 1; i <= n; i++) cout << p[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int n, m, mod, a[N], b[N], ans[N]; n int main() { n   cin >> n >> m >> mod; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= m; i++) { n     cin >> b[i]; n   } n   for (int i = 1; i <= m; i++) { n     ans[i] += b[i]; n     ans[i + n - m + 1] -= b[i]; n   } n   for (int i = 1; i <= n; i++) { n     ans[i] += ans[i - 1]; n   } n   for (int i = 1; i <= n; i++) { n     ans[i] += a[i]; n     cout << ans[i] % mod <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double epsilon = 1e-9; n int main() { n   long long n, m, v; n   cin >> n >> m >> v; n   long long p = ((n - 1) * (n - 2)) / 2LL + 1; n   if (m > p || m < n - 1) { n     cout << -1 << endl; n     return 0; n   } n   int other = 1; n   if (v == 1) other = 2; n   for (int i = 1; i <= n; i++) { n     if (i == v) n       continue; n     else n       cout << v <<     << i << endl; n   } n   m -= n - 1; n   for (int i = 1; i <= n && m > 0; i++) { n     if (i == v || i == other) continue; n     for (int j = i + 1; j <= n && m > 0; j++) { n       if (j == v || j == other || j == i) continue; n       printf( %d %d n , i, j); n       m--; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-7; n const double PI = acos(-1.0); n const double ZERO = 0; n const double DOUBLE_MAX = 1 / ZERO; n int main() { n   long double t, s, q; n   cin >> t >> s >> q; n   long double speed = (q - 1) / q; n   long double x = 0, d; n   int res = 0; n   while (t - s > 1e-9) { n     x += s * speed; n     while (t - s > 1e-9 && x > 1e-10) { n       s += (x) + 1e-9; n       x *= speed; n     } n     res++; n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n T abs(T x) { n   if (x < 0) return -x; n   return x; n } n template <class T> n T sqr(T a) { n   return a * a; n } n const double pi = acos(-1.0); n const double eps = 1e-8; n char str1[2000010]; n char str2[2000010]; n int main() { n   int cross, cnt1, cnt2, sum1, sum2, n, f, i; n   while (1 == scanf( %d , &n)) { n     cnt1 = cnt2 = sum1 = sum2 = cross = 0; n     scanf( %s%s , str1, str2); n     for (i = 0; i < 2 * n; i++) { n       if (str1[i] ==  1  && str2[i] ==  1 ) n         cross++; n       else if (str1[i] ==  1 ) n         cnt1++; n       else if (str2[i] ==  1 ) n         cnt2++; n     } n     f = 1; n     while (cross) { n       if (f) n         sum1++, f = 0; n       else n         sum2++, f = 1; n       cross--; n     } n     if ((cnt1 == 0 && cnt2 == 0 && cross == 0) || (cross == 2 * n) || n         (cross % 2 == 0 && cnt1 == 0 && cnt2 == 0)) n       printf( Draw n ); n     else if (cnt2 == 0 && sum1 + cnt1 - 1 == sum2 && cnt1 != 1) n       printf( Draw n ); n     else if (sum2 + cnt2 - 1 == sum1 + cnt1 || sum2 + cnt2 == sum1 + cnt1) n       printf( Draw n ); n     else if (sum1 + cnt1 > sum2 + cnt2) n       printf( First n ); n     else n       printf( Second n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ii = int; n int main() { n   ii n, i; n   cin >> n; n   ii* ar = (ii*)malloc(sizeof(ii) * n); n   for (i = 0; i < n; i++) { n     cin >> ar[i]; n   } n   ii max = *max_element(ar, ar + n); n   if (n == 1) n     cout << max - max << endl; n   else { n     ii count = 0; n     for (i = 0; i < n; i++) { n       count = count + max - ar[i]; n     } n     cout << count << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> balance(n, 0); n   for (int i = 0; i < m; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     balance[a - 1] -= c; n     balance[b - 1] += c; n   } n   int total = 0; n   for (int i = 0; i < n; i++) { n     total += balance[i] > 0 ? balance[i] : 0; n   } n   cout << total << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char ch = getchar(); n   int x = 0, f = 1; n   while (ch <  0  || ch >  9 ) f = ch ==  -  ? -1 : 1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x * f; n } n inline int sqr(int x) { return x * x; } n inline int dis(int x1, int y1, int x2, int y2) { n   return sqr(x2 - x1) + sqr(y2 - y1); n } n const int N = 24, MAXN = N + 10; n int xo, yo; n int x[MAXN], y[MAXN]; n int dp[1 << N], pre[1 << N], w[MAXN][MAXN]; n int main(int argc, char *argv[]) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   xo = read(), yo = read(); n   int n = read(), msk = (1 << n) - 1; n   for (int i = (0); i <= (n - 1); i += (1)) x[i] = read(), y[i] = read(); n   x[n] = xo, y[n] = yo; n   for (int i = (0); i <= (n); i += (1)) n     for (int j = (i + 1); j <= (n); j += (1)) n       w[i][j] = w[j][i] = dis(x[i], y[i], x[j], y[j]); n   memset(dp, 0x3f, sizeof(dp)); n   dp[0] = 0; n   for (int s = (0); s <= (msk); s += (1)) n     if (dp[s] != 0x3f3f3f3f) n       for (int i = (0); i <= (n - 1); i += (1)) n         if (((s >> i) & 1) == 0) { n           int k = s | (1 << i); n           if (dp[s] + w[n][i] + w[i][n] < dp[k]) n             dp[k] = dp[s] + w[n][i] + w[i][n], pre[k] = s; n           for (int j = (i + 1); j <= (n - 1); j += (1)) n             if (((k >> j) & 1) == 0) { n               int l = k | (1 << j); n               if (dp[s] + w[n][i] + w[i][j] + w[j][n] < dp[l]) n                 dp[l] = dp[s] + w[n][i] + w[i][j] + w[j][n], pre[l] = s; n             } n           break; n         } n   printf( %d n , dp[msk]); n   printf( 0  ); n   for (int x = msk; x; x = pre[x]) { n     for (int i = (0); i <= (n - 1); i += (1)) n       if (((x ^ pre[x]) >> i) & 1) printf( %d  , i + 1); n     printf( 0  ); n   } n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e9 + 7; n const double eps = 1e-12; n const int maxn = 1e5 + 5; n const int INF = 1e9; n long long n, m; n long long ma = 0, mi = 0; n void pre() { n   for (int i = 0; i < n; i++) ma += i; n   if (n & 1) { n     mi = n / 2 * (n / 2 + 1); n   } else { n     mi = n / 2 * n / 2; n   } n } n int main() { n   long long ans = 0, x, d; n   scanf( %lld%lld , &n, &m); n   pre(); n   for (int i = 0; i < m; i++) { n     scanf( %lld %lld , &x, &d); n     if (d < 0) { n       ans += x * n + d * mi; n     } else { n       ans += x * n + d * ma; n     } n   } n   printf( %lf n , (double)1.0 * ans / n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int K = 30; n const int MOD = 1e9 + 7; n long long n, power[K + 1]; n int k; n __inline void add(int &a, int b) { n   if ((a += b) >= MOD) { n     a -= MOD; n   } n } n struct Matrix { n   int trans[K][K]; n   int go[K]; n   Matrix() { n     memset(trans, 0, sizeof(trans)); n     memset(go, 0, sizeof(go)); n   } n   int all() { n     int result = 0; n     for (int i = 0; i < k; i++) { n       add(result, go[i]); n       for (int j = 0; j < k; j++) { n         add(result, trans[i][j]); n       } n     } n     return result; n   } n }; n map<long long, Matrix> mp; n Matrix search(long long n) { n   if (mp.count(n)) { n     return mp[n]; n   } n   Matrix ret; n   if (k >= n) { n     for (int i = 0; i < n; i++) { n       ret.go[i] = 0; n       ret.trans[i][i] = 1; n       for (int j = i + 1; j < n; j++) { n         ret.trans[i][j] = power[j - i - 1]; n       } n     } n     return ret; n   } n   long long unit = 1; n   int length = 1; n   while (unit <= n / k && unit * k < n) { n     unit *= k; n     length++; n   } n   Matrix base = search(unit); n   ret = base; n   long long remaind = n % unit; n   Matrix extra = search(remaind); n   static int reach[K][K], _reach[K][K]; n   for (int i = 0; i < k; i++) { n     ret.go[i] = base.go[i]; n     for (int j = 0; j < k; j++) { n       reach[i][j] = base.trans[i][j]; n     } n   } n   for (int i = 1; i <= n / unit; i++) { n     if (i == n / unit && remaind == 0) { n       break; n     } n     Matrix &nxt = (i < n / unit) ? base : extra; n     int lead = i; n     for (int j = 0; j < k; j++) { n       for (int l = 0; l < k; l++) { n         _reach[j][l] = reach[j][l]; n         add(ret.go[j], reach[j][l]); n         reach[j][l] = 0; n       } n     } n     int t = ((k - 1) * (length - 2) + i - 1) % k; n     for (int start = 0; start < k; start++) { n       int sum = 0; n       for (int prev = k - 1; prev >= 0; prev--) { n         int pv = (prev + t) % k; n         add(sum, _reach[start][pv]); n         int succ = prev; n         int sv = (succ + t) % k, temp = ((sv - i) % k + k) % k; n         add(ret.go[start], 1LL * sum * nxt.go[temp] % MOD); n         for (int end = 0; end < k; end++) { n           add(reach[start][end], n               1LL * sum * nxt.trans[temp][((end - i) % k + k) % k] % MOD); n         } n       } n     } n     for (int i = 0; i < k; i++) { n       for (int j = 0; j < k; j++) { n         ret.trans[i][j] = reach[i][j]; n       } n     } n   } n   return mp[n] = ret; n } n int main() { n   power[0] = 1; n   for (int i = 1; i <= K; i++) { n     power[i] = power[i - 1] * 2; n   } n   cin >> n >> k; n   Matrix answer = search(n); n   cout << (answer.all() + 1) % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 10; n int n; n int a[N], sum[N], b[65][N], cnt[65]; n long long ans; n int t[2]; n long long find(int l, int r, int lim) { n   int i, j; n   long long ret = 0; n   if (l > r) return 0; n   j = l - 1; n   t[0] = t[1] = 0; n   for (i = l; i <= r; ++i) { n     while (sum[i] - sum[j] >= lim) { n       ++t[sum[j] & 1]; n       ++j; n     } n     ret += (long long)t[sum[i] & 1]; n   } n   return ret; n } n void dfs(int l, int r, int mx) { n   if (r - l - 1 < 1) return; n   if (!mx) return; n   int i, tmx = 0; n   cnt[mx] = 1; n   b[mx][cnt[mx]] = l; n   for (i = l + 1; i <= r - 1; ++i) n     if (a[i] == mx) b[mx][++cnt[mx]] = i; n   b[mx][++cnt[mx]] = r; n   if (cnt[mx] > 2) { n     ans += find(l + 1, r - 1, mx + mx); n     for (i = 1; i <= cnt[mx] - 1; ++i) { n       ans -= find(b[mx][i] + 1, b[mx][i + 1] - 1, mx + mx); n     } n   } n   for (i = 1; i <= cnt[mx] - 1; ++i) dfs(b[mx][i], b[mx][i + 1], mx - 1); n } n int main() { n   int i, mx = 0; n   long long x; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) { n     scanf( %lld , &x); n     for (; x; x >>= 1) n       if (x & 1ll) ++a[i]; n     mx = max(mx, a[i]); n     sum[i] = sum[i - 1] + a[i]; n   } n   dfs(0, n + 1, mx); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int T; n   int n, x, i, j; n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &n, &x); n     printf( %d n , 2 * x); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int test; n   cin >> test; n   while (test--) { n     int bun, beef, chicks, hp, cp, count1 = 0, count2 = 0; n     cin >> bun >> beef >> chicks >> hp >> cp; n     bun /= 2; n     if (hp >= cp) { n       count1 = min(bun, beef); n       bun -= count1; n       if (bun != 0) { n         count2 = min(bun, chicks); n       } n     } else { n       count2 = min(bun, chicks); n       bun -= count2; n       if (bun != 0) { n         count1 = min(bun, beef); n       } n     } n     cout << count1 * hp + count2 * cp << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n const long long mod = 998244353; n long long p[N]; n int main() { n   int n; n   long long ans; n   scanf( %d , &n); n   p[0] = 1; n   for (int i = 1; i <= n; i++) p[i] = p[i - 1] * (n - i + 1) % mod; n   ans = p[1]; n   for (int i = 2; i <= n; i++) { n     ans += (p[i] - p[i - 1] + mod) % mod * i % mod; n     ans %= mod; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, in, ans = 0, asum = 0, bsum = 0; n   vector<int> a, b; n   vector<int>::iterator minit, it2; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &in); n     a.push_back(in); n   } n   b = a; n   sort(b.begin(), b.end()); n   for (int i = 0; i < n; i++) { n     asum += a[i]; n     bsum += b[i]; n     if (asum == bsum) { n       ans++; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct state { n   state(int a, int b, int c, int d) : num(a), ind1(b), ind2(c), oper(d){}; n   int num; n   int ind1, ind2; n   int oper; n }; n vector<state> ansState; n int n, ans = 100000; n map<set<int>, int> m; n vector<state> v; n void dfs() { n   if (v[v.size() - 1].num == n) { n     if (v.size() < ans) { n       ans = v.size(); n       ansState = v; n     } n     return; n   } n   if (v.size() >= ans || v.size() > 5) return; n   set<int> s; n   for (int i = 0; i < v.size(); ++i) s.insert(v[i].num); n   if (m[s]) return; n   m.insert(make_pair(s, 1)); n   for (int i = 0; i < v.size(); ++i) { n     for (int j = 1; j <= 3; ++j) { n       int x = v[i].num * (1 << j); n       if (x > n) break; n       int flag = 1; n       for (int k = 0; k < v.size(); ++k) n         if (v[k].num == x) { n           flag = 0; n           break; n         } n       if (!flag) continue; n       v.push_back(state(x, i, -1, j)); n       dfs(); n       v.pop_back(); n     } n     for (int j = 0; j < v.size(); ++j) n       for (int k = 0; k <= 3; ++k) { n         int x = v[i].num + (1 << k) * v[j].num; n         if (x > n) break; n         int flag = 1; n         for (int t = 0; t < v.size(); ++t) n           if (v[t].num == x) { n             flag = 0; n             break; n           } n         if (flag) { n           v.push_back(state(x, i, j, 4 + k)); n           dfs(); n           v.pop_back(); n         } n       } n   } n } n int Solution() { n   cin >> n; n   v.push_back(state(1, -1, -1, -1)); n   dfs(); n   cout << ans - 1 << endl; n   for (int i = 1; i < ansState.size(); ++i) { n     cout <<  lea e  << (char)( a  + i) <<  x, [ ; n     if (ansState[i].oper < 4) { n       cout << (1 << ansState[i].oper) <<  *e  << (char)( a  + ansState[i].ind1) n            <<  x]  << endl; n     } n     if (ansState[i].oper == 4) { n       cout <<  e  << (char)( a  + ansState[i].ind1) <<  x +   n            <<  e  << (char)( a  + ansState[i].ind2) <<  x]  << endl; n     } n     if (ansState[i].oper > 4) { n       cout <<  e  << (char)( a  + ansState[i].ind1) <<  x +   n            << (1 << (ansState[i].oper - 4)) <<  *  n            <<  e  << (char)( a  + ansState[i].ind2) <<  x]  << endl; n     } n   } n   return 0; n } n int main() { n   Solution(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 2 * 1e9; n const int mod = 1e9 + 7; n const int maxn = 200005; n int n, m; n int arr[maxn], ob[maxn], fact[maxn], obr[maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> arr[1]; n   ob[1] = fact[1] = obr[1] = 1; n   for (int i = 2; i <= n; ++i) { n     cin >> arr[i]; n     ob[i] = (mod - 1LL * (mod / i) * ob[mod % i] % mod) % mod; n     fact[i] = (1LL * i * fact[i - 1]) % mod; n     obr[i] = (1LL * obr[i - 1] * ob[i]) % mod; n   } n   int ans = 0; n   if (n % 4 == 0) { n     for (int i = 1; i <= n; i += 2) { n       int p1 = (n - 2) / 2, p2 = (i - 1) / 2; n       int c = n           (((1LL * fact[p1] * obr[p2]) % mod * obr[p1 - p2]) % mod + mod) % mod; n       if (c == 0) c = 1; n       ans = (1LL * ans + (1LL * c * (arr[i] - arr[i + 1])) % mod) % mod; n     } n   } else if (n % 4 == 1) { n     for (int i = 1; i <= n; i += 2) { n       int p1 = (n - 1) / 2, p2 = (i - 1) / 2; n       int c = ((1LL * fact[p1] * obr[p2]) % mod * obr[p1 - p2]) % mod; n       if (c == 0) c = 1; n       ans = (1LL * ans + (1LL * c * arr[i]) % mod) % mod; n     } n   } else if (n % 4 == 2) { n     for (int i = 1; i <= n; ++i) { n       int p1 = (n - 2) / 2, p2 = (i - 1) / 2; n       int c = ((1LL * fact[p1] * obr[p2]) % mod * obr[p1 - p2]) % mod; n       if (c == 0) c = 1; n       ans = (1LL * ans + (1LL * c * arr[i]) % mod) % mod; n     } n   } else { n     ans = arr[1]; n     for (int i = 2; i <= n; i += 2) { n       int p1 = (n - 3) / 2, p2 = (i - 2) / 2; n       int c1 = ((1LL * fact[p1] * obr[p2]) % mod * obr[p1 - p2]) % mod; n       p2 += 1; n       int c2 = ((1LL * fact[p1] * obr[p2]) % mod * obr[p1 - p2]) % mod; n       if (c1 == 0) n         c1 = 1; n       else if (c2 == 0) n         c2 = 1; n       ans = (1LL * ans + (1LL * arr[i] * 2 * c1) % mod + n              (1LL * arr[i + 1] * (c2 - c1)) % mod) % n             mod; n     } n   } n   ans = (ans + mod) % mod; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T &x) { n   x = 0; n   T f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -f; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   x *= f; n } n int max(int x, int y) { return x > y ? x : y; } n int min(int x, int y) { return x < y ? x : y; } n char s[500010], t[500010]; n int m, n, cnt0, cnt1; n int nxt[500010], ans[500010]; n void Get_Next() { n   nxt[0] = -1; n   int j = 0, k = -1; n   while (j <= n) { n     if (k == -1 || t[j] == t[k]) n       nxt[++j] = ++k; n     else n       k = nxt[k]; n   } n } n bool fl; n int main() { n   scanf( %s , s), scanf( %s , t), m = strlen(s), n = strlen(t); n   t[n] =  $ ; n   for (int i = 1; i <= m; ++i) n     if (s[i - 1] ==  0 ) n       cnt0++; n     else n       cnt1++; n   Get_Next(); n   for (int i = 0; i < n; ++i) { n     if (t[i] ==  0  && cnt0 == 0) { n       fl = 1; n       break; n     } n     if (t[i] ==  1  && cnt1 == 0) { n       fl = 1; n       break; n     } n     ans[i] = t[i] -  0 ; n     if (t[i] ==  0 ) n       cnt0--; n     else n       cnt1--; n   } n   if (fl) { n     cout << s <<   n ; n     return 0; n   } n   int now = n, kk = n; n   while (1) { n     if (now == n) now = nxt[n]; n     if (t[now] ==  0  && cnt0 == 0) { n       for (int i = 1; i <= cnt1; ++i) ans[kk + i - 1] = 1; n       break; n     } n     if (t[now] ==  1  && cnt1 == 0) { n       for (int i = 1; i <= cnt0; ++i) ans[kk + i - 1] = 0; n       break; n     } n     ans[kk++] = t[now] -  0 ; n     if (t[now] ==  0 ) n       cnt0--; n     else n       cnt1--; n     now++; n     if (kk > m) break; n   } n   for (int i = 0; i < m; ++i) cout << ans[i]; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, ch; n   while (!isdigit(ch = getchar())) n     ; n   while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); n   return x; n } n template <typename T> n inline T gcd(T a, T b) { n   return !b ? a : gcd(b, a % b); n } n struct Node { n   int len, pos, id; n }; n struct prime { n   int p, c; n }; n const int MAXN = 100000; n vector<Node> a[MAXN + 1]; n vector<prime> d[41]; n int pri[45][10]; n long long pw[45][10]; n int main() { n   int n = read(), m = read(); n   for (int i = 1; i <= n; i++) { n     int len = read(); n     for (int j = 0; j < len; j++) { n       int x = read(); n       a[x].push_back((Node){len, j, i}); n     } n   } n   for (int i = 2; i <= 40; i++) { n     int tmp = i; n     for (int j = 2; j <= i; j++) { n       if (tmp % j) continue; n       int c = 0; n       while (tmp % j == 0) tmp /= j, ++c; n       d[i].push_back((prime){j, c}); n     } n     long long mul = 1; n     for (int j = 0; j < 10; j++) pw[i][j] = mul, mul *= i; n   } n   for (int i = 1; i <= m; i++) { n     int si = a[i].size(), res = 1, l = 0; n     if (!si) { n       puts( 0 ); n       continue; n     } n     long long lcm = a[i][0].len, pos = a[i][0].pos, las = a[i][0].id; n     int s = d[lcm].size(); n     for (int j = 0; j < s; j++) { n       prime z = d[lcm][j]; n       pri[z.p][z.c]++; n     } n     for (int r = 1; r < si; r++) { n       Node x = a[i][r], y = a[i][l]; n       if (x.id != las + 1) { n         l = r, lcm = x.len, pos = x.pos; n         memset(pri, 0, sizeof(pri)); n       } else { n         long long g = gcd(lcm, (long long)x.len); n         while (l < r && abs(pos - x.pos) % g) { n           s = d[y.len].size(); n           for (int j = 0; j < s; j++) { n             prime z = d[y.len][j]; n             pri[z.p][z.c]--; n             if (!pri[z.p][z.c]) { n               int flag = 0; n               for (int k = z.c + 1; k < 10; k++) flag |= pri[z.p][k]; n               if (!flag) { n                 lcm /= pw[z.p][z.c]; n                 for (int k = z.c; k >= 1; k--) n                   if (pri[z.p][k]) { n                     lcm *= pw[z.p][k]; n                     break; n                   } n               } n             } n           } n           g = gcd(lcm, (long long)x.len); n           y = a[i][++l]; n           pos %= lcm; n         } n         long long tpos = x.pos; n         while (pos != tpos) { n           if (pos < tpos) n             pos += (tpos - pos + lcm - 1) / lcm * lcm; n           else n             tpos += (pos - tpos + x.len - 1) / x.len * x.len; n         } n         lcm = lcm / g * x.len; n       } n       las = x.id; n       s = d[x.len].size(); n       for (int j = 0; j < s; j++) { n         prime z = d[x.len][j]; n         pri[z.p][z.c]++; n       } n       res = max(res, r - l + 1); n     } n     printf( %d n , res); n     memset(pri, 0, sizeof(pri)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int N = 2e5 + 5; n void solve() { n   long long int n, x; n   cin >> n >> x; n   vector<long long int> v(n); n   for (auto &it : v) cin >> it; n   ; n   sort(v.begin(), v.end()); n   long long int ans = 0; n   vector<long long int> a; n   for (long long int i = 0; i < n; i++) { n     if (v[i] >= x) { n       ans++; n     } else { n       a.push_back(v[i]); n     } n   } n   reverse(a.begin(), a.end()); n   for (long long int i = 0; i < a.size(); i++) { n     long long int mini = a[i]; n     long long int count = 1; n     while (mini * count < x) { n       i++; n       if (i >= a.size()) { n         break; n       } n       mini = min(mini, a[i]); n       count++; n     } n     if (mini * count >= x) { n       ans++; n     } n   } n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n vector<int> c[2]; n int main() { n   int n, k, p, x, m1 = 0, m2 = 0; n   scanf( %d%d%d , &n, &k, &p); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &x); n     if (x & 1) { n       c[0].push_back(x); n       m1++; n     } else { n       c[1].push_back(x); n       m2++; n     } n   } n   if (m1 >= k - p && (m1 - (k - p)) % 2 == 0 && n       (m2 + (m1 - (k - p)) / 2) >= p) { n     puts( YES ); n     int i, j; n     if (p > 0) { n       for (i = 0; i < k - p; i++) printf( 1 %d n , c[0][i]); n       for (j = 0; j < p - 1;) { n         if (j < m2) { n           printf( 1 %d n , c[1][j]); n           j++; n         } else { n           printf( 2 %d %d n , c[0][i], c[0][i + 1]); n           i += 2; n           p--; n         } n       } n       if (m1 - i + m2 - j > 0) { n         printf( %d , m1 - i + m2 - j); n         for (; j < m2; j++) printf(  %d , c[1][j]); n         for (; i < m1; i++) printf(  %d , c[0][i]); n         putchar(  n ); n       } n     } else { n       for (i = 0; i < k - 1; i++) printf( 1 %d n , c[0][i]); n       if (m1 - i + m2 > 0) { n         printf( %d , m1 - i + m2); n         for (; i < m1; i++) printf(  %d , c[0][i]); n         for (j = 0; j < m2; j++) printf(  %d , c[1][j]); n         putchar(  n ); n       } n     } n   } else { n     puts( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x; n char s[100005]; n bool flag[500]; n int cnt[500]; n vector<char> f; n int main() { n   scanf( %d , &x); n   scanf( %s , s); n   int len = strlen(s); n   for (int i = 0; i < len; i++) cnt[s[i]]++; n   for (int i =  a ; i <=  z ; i++) { n     if (cnt[i] == 0) continue; n     flag[i] = 1; n     int pre = -1; n     bool tag = 0; n     f.push_back(char(i)); n     for (int j = 0; j < len; j++) { n       if (flag[s[j]]) { n         pre = j; n         continue; n       } n       int y = j - pre; n       if (y >= x) { n         tag = 1; n         break; n       } n     } n     if (tag) n       continue; n     else { n       cnt[i] = 0; n       flag[i] = 0; n       int pos = -1; n       int pre = -1; n       for (int j = 0; j < len; j++) { n         if (s[j] == i) { n           pos = max(pos, j); n         } n         if (flag[s[j]]) { n           pre = j; n           continue; n         } n         int y = j - pre; n         if (y >= x) { n           pre = pos; n           cnt[i]++; n         } n       } n       break; n     } n   } n   string ans =   ; n   int p = f.size(); n   for (int i = 0; i < p; i++) { n     for (int j = 1; j <= cnt[f[i]]; j++) ans += f[i]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[200005], b[200005]; n int x[200005]; n int main() { n   int n; n   long long t; n   scanf( %d%I64d , &n, &t); n   for (int i = 1; i <= n; i++) scanf( %I64d , a + i); n   for (int i = 1; i <= n; i++) { n     scanf( %d , x + i); n     if (x[i] < x[i - 1] || x[i] < i) { n       puts( No ); n       return 0; n     } n   } n   for (int i = 1; i <= n; i++) b[x[i]] = -1; n   for (int i = 1; i <= n; i++) b[i] += b[i - 1]; n   for (int i = 1; i <= n; i++) n     if (b[x[i] - 1] - b[i - 1]) { n       puts( No ); n       return 0; n     } n   for (int i = n; i; i--) b[i] -= b[i - 1]; n   for (int i = 1; i < n; i++) b[i] += a[i + 1] + t; n   b[n] = a[n] + t + 1; n   for (int i = 1; i <= n; i++) n     if (b[i] < a[i] + t || b[i] <= b[i - 1]) { n       puts( No ); n       return 0; n     } n   puts( Yes ); n   for (int i = 1; i <= n; i++) printf( %I64d  , b[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tests; n   scanf( %d , &tests); n   while (tests--) { n     int item; n     cin >> item; n     vector<int> v; n     set<int> s; n     int temp; n     for (int i = 0; i < item; i++) { n       scanf( %d , &temp); n       v.push_back(temp); n       s.insert(temp); n     } n     int distinct = 0; n     cout << s.size() <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 16; n int from[MAXN], to[MAXN], p[MAXN], q[MAXN]; n bool w[MAXN][MAXN]; n long long dp[1 << MAXN]; n int main() { n   int n, m, a, b; n   long long y; n   cin >> n >> y >> m; n   fill(from, from + n, 0); n   fill(to, to + n, 0); n   fill(w[0], w[m], false); n   for (int i = 0; i < m; ++i) { n     cin >> a >> b; n     --a; n     --b; n     w[a][b] = true; n   } n   for (int k = 0; k < n; ++k) { n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < n; ++j) { n         w[i][j] |= w[i][k] && w[k][j]; n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (w[i][j]) { n         from[j] |= 1 << i; n         to[i] |= 1 << j; n       } n     } n   } n   fill(dp, dp + (1 << n), 0); n   dp[0] = 1; n   for (int i = 1; i < (1 << n); ++i) { n     for (int j = 0; j < n; ++j) { n       if ((i & (1 << j)) != 0 && (i & from[j]) == 0) { n         dp[i] += dp[i ^ (1 << j)]; n       } n     } n   } n   y -= 2001; n   m = (1 << n) - 1; n   if (y >= dp[m]) { n     cout <<  The times have changed  << endl; n     return 0; n   } n   fill(p, p + n, -1); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < n; ++j) { n       if (p[j] != -1) { n         continue; n       } n       p[j] = i; n       { n         fill(dp, dp + (1 << n), 0); n         dp[0] = 1; n         for (int i = 1; i < (1 << n); ++i) { n           int k = n - __builtin_popcount(i); n           if (p[k] != -1) { n             int j = p[k]; n             if ((i & (1 << j)) != 0 && (i & from[j]) == 0) { n               dp[i] += dp[i ^ (1 << j)]; n             } n           } else { n             for (int j = 0; j < n; ++j) { n               if ((i & (1 << j)) != 0 && (i & from[j]) == 0) { n                 dp[i] += dp[i ^ (1 << j)]; n               } n             } n           } n         } n       } n       if (y < dp[m]) { n         q[i] = j; n         break; n       } else { n         p[j] = -1; n         y -= dp[m]; n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     printf( %d%c , q[i] + 1, i == n - 1 ?   n  :    ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 50005; n int n, m; n int p[N], a[N]; n struct LCT { n   int fa[N], ch[N][2], siz[N], sv[N]; n   long long sv2[N], sav[N], san[N], sa[N]; n   int nroot(int u) { return ch[fa[u]][0] == u || ch[fa[u]][1] == u; } n   void pushup(int u) { n     siz[u] = siz[ch[u][0]] + siz[ch[u][1]] + sv[u]; n     san[u] = san[ch[u][0]] + san[ch[u][1]] + 1ll * a[u] * sv[u]; n     sa[u] = n         sa[ch[u][0]] + sa[ch[u][1]] + sav[u] + n         (1ll * sv[u] * sv[u] - sv2[u] + 2ll * siz[ch[u][1]] * sv[u]) * a[u] + n         2 * san[ch[u][0]] * (siz[ch[u][1]] + sv[u]); n   } n   void rot(int u) { n     int v = fa[u], t = fa[v], d = (u == ch[v][1]); n     if (nroot(v)) ch[t][v == ch[t][1]] = u; n     fa[u] = t; n     fa[ch[u][d ^ 1]] = v; n     ch[v][d] = ch[u][d ^ 1]; n     fa[v] = u; n     ch[u][d ^ 1] = v; n     pushup(v); n     pushup(u); n   } n   void splay(int u) { n     while (nroot(u)) { n       int v = fa[u]; n       if (nroot(v)) rot(u == ch[v][1] ^ v == ch[fa[v]][1] ? u : v); n       rot(u); n     } n   } n   int access(int u) { n     int v = 0; n     for (; u; v = u, u = fa[u]) { n       splay(u); n       sv[u] += siz[ch[u][1]] - siz[v]; n       sv2[u] += 1ll * siz[ch[u][1]] * siz[ch[u][1]] - 1ll * siz[v] * siz[v]; n       sav[u] += sa[ch[u][1]] - sa[v]; n       ch[u][1] = v; n       pushup(u); n     } n     return v; n   } n   int lca(int u, int v) { n     access(u); n     return access(v); n   } n   void makeroot(int u) { n     access(u); n     splay(u); n   } n   void Link(int u, int v) { n     makeroot(u); n     makeroot(v); n     fa[u] = v; n     sv[v] += siz[u]; n     sv2[v] += 1ll * siz[u] * siz[u]; n     sav[v] += sa[u]; n     pushup(v); n   } n   void Cut(int u, int v) { n     access(u); n     splay(v); n     ch[v][1] = fa[u] = 0; n     pushup(v); n   } n } lt; n void query() { n   lt.splay(1); n   printf( %.12lf n , 1.0 * lt.sa[1] / n / n); n } n int main() { n   int x, y; n   char ty; n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) scanf( %d , p + i); n   for (int i = 1; i <= n; i++) n     scanf( %d , a + i), lt.siz[i] = lt.sv[i] = 1, lt.san[i] = lt.sa[i] = a[i]; n   for (int i = 2; i <= n; i++) lt.Link(i, p[i]); n   scanf( %d , &m); n   query(); n   while (m--) { n     do ty = getchar(); n     while (ty !=  P  && ty !=  V ); n     scanf( %d%d , &x, &y); n     if (ty ==  P ) { n       if (lt.lca(x, y) == x) swap(x, y); n       lt.Cut(x, p[x]); n       lt.Link(x, p[x] = y); n     } else n       lt.makeroot(x), a[x] = y, lt.pushup(x); n     query(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[6]; n int sum = 0; n int x, y, z; n int x1, adjf, z1; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   scanf( %d%d%d , &x, &y, &z); n   scanf( %d%d%d , &x1, &adjf, &z1); n   for (auto i = 0; i != 6; i++) scanf( %d , &a[i]); n   if (y < 0) { n     sum += a[0]; n   } n   if (y > adjf) { n     sum += a[1]; n   } n   if (z < 0) { n     sum += a[2]; n   } n   if (z > z1) { n     sum += a[3]; n   } n   if (x < 0) { n     sum += a[4]; n   } n   if (x > x1) { n     sum += a[5]; n   } n   printf( %d , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, k; n long long mx, p1, p2, val, sum, res, res1; n long long m[200007][27]; n int main() { n   ios_base::sync_with_stdio(); n   cin.tie(); n   cout.tie(); n   ; n   cin >> n >> k; n   res = 1000000007.0; n   for (int i = 1; i <= n; i++) { n     cin >> val; n     mx = max(val, mx); n     m[val][0]++; n     if (m[val][0] >= k) res = 0; n   } n   mx /= 2; n   for (long long i = mx; i >= 0; i--) { n     if (i == 0) { n       sum = m[0][0]; n       for (int j = 0; j <= 19; j++) { n         m[0][j + 1] += m[1][j]; n         sum += m[i][j + 1]; n       } n     } else { n       p1 = 2 * i; n       p2 = 2 * i + 1; n       sum = m[i][0]; n       for (int j = 0; j <= 19; j++) { n         m[i][j + 1] += m[p1][j] + m[p2][j]; n         sum += m[i][j + 1]; n       } n     } n     if (sum >= k) { n       sum = 0; n       res1 = 0; n       for (long long j = 0; j <= 19; j++) { n         res1 += m[i][j] * j; n         sum += m[i][j]; n         if (sum >= k) { n           res = min(res, (res1 - (sum - k) * j)); n           break; n         } n       } n     } n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     long long a, b, c; n     scanf( %lld %lld %lld , &a, &b, &c); n     printf( %lld n , a + b + c - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100003; n int n, L, R, phi; n long long mod; n long long fac[N], inv[N]; n int d[N][33]; n int tot, p[N]; n long long fpow(long long x, int y) { n   long long res = 1; n   while (y) { n     if (y & 1) res = res * x % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return res; n } n void init() { n   fac[0] = inv[0] = 1; n   for (int i = 1; i <= n; i++) { n     int tmp = i; n     for (int j = 1; j <= tot; j++) { n       d[i][j] = d[i - 1][j]; n       while (tmp % p[j] == 0) tmp /= p[j], d[i][j]++; n     } n     fac[i] = fac[i - 1] * tmp % mod; n     inv[i] = fpow(fac[i], phi - 1); n   } n } n long long C(int n, int m) { n   if (n < m) return 0; n   long long res = fac[n] * inv[m] % mod * inv[n - m] % mod; n   for (int i = 1; i <= tot; i++) n     res = res * fpow(p[i], d[n][i] - d[m][i] - d[n - m][i]) % mod; n   return res; n } n int main() { n   scanf( %d%I64d%d%d , &n, &mod, &L, &R); n   int tmp = mod; n   phi = mod; n   for (int i = 2; i * i <= tmp; i++) n     if (tmp % i == 0) { n       p[++tot] = i; n       phi = phi / i * (i - 1); n       while (tmp % i == 0) tmp /= i; n     } n   if (tmp != 1) p[++tot] = tmp, phi = phi / tmp * (tmp - 1); n   init(); n   long long ans = 0; n   for (int i = 0; i <= n; i++) { n     long long res = (C(i, (i + L + 1) / 2) - C(i, (i + R) / 2 + 1) + mod) % mod; n     ans = (ans + res * C(n, i) % mod) % mod; n   } n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mapa[35]; n int main() { n   int n, q; n   scanf( %d %d , &n, &q); n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     for (int j = 30; j >= 0; j--) n       if ((1 << j) & x) { n         mapa[j]++; n         break; n       } n   } n   while (q--) { n     int x; n     scanf( %d , &x); n     long long pos = 0, res = 0; n     for (int i = 30; i >= 0; i--) { n       long long b = (1 << i); n       if (b > x) continue; n       long long aux = min(mapa[i], (long long)x / b); n       res += aux; n       x -= b * aux; n     } n     if (x > 0) n       printf( -1 n ); n     else n       printf( %lld n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mn = 2010; n long long a[mn], b[mn]; n int main() { n   int n, k; n   long long p; n   scanf( %d %d %lld , &n, &k, &p); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (int i = 1; i <= k; i++) scanf( %lld , &b[i]); n   sort(a + 1, a + n + 1); n   sort(b + 1, b + k + 1); n   long long ans = 4e18; n   for (int st = 0; st + n <= k; st++) { n     long long tans = 0; n     for (int i = 1; i <= n; i++) { n       long long temp = 0; n       long long now = b[st + i]; n       if (a[i] <= p && now <= p) { n         if (a[i] <= now) n           temp += p - a[i]; n         else n           temp += a[i] - now + p - now; n       } else if (a[i] <= p && p < now) n         temp += now - a[i] + now - p; n       else if (now <= p && p < a[i]) n         temp += a[i] - now + p - now; n       else if (p < a[i] && p < now) { n         if (now <= a[i]) n           temp += a[i] - p; n         else n           temp += now - a[i] + now - p; n       } n       tans = max(tans, temp); n     } n     ans = min(ans, tans); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, m, b, r; n   cin >> n >> m; n   b = 3; n   r = 1; n   while (n > 0) { n     if (n % 2 != 0) r = (r * b) % m; n     b = (b * b) % m; n     n = n / 2; n   } n   cout << (r - 1 + m) % m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 30, Mod = 1e9 + 7; n const long long SQ = 330; n unordered_map<int, int> Par[N]; n map<pair<int, int>, int> ans; n vector<int> G[N]; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n inline int find(int x, int c) { n   if (Par[c].find(x) == Par[c].end()) return x; n   return Par[c][x] = find(Par[c][x], c); n } n inline void merge(int x, int y, int c) { n   x = find(x, c); n   y = find(y, c); n   if (x == y) return; n   Par[c][x] = y; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     G[a].push_back(c); n     G[b].push_back(c); n     merge(a, b, c); n   } n   for (int i = 1; i <= n; i++) { n     sort(G[i].begin(), G[i].end()); n     G[i].resize(unique(G[i].begin(), G[i].end()) - G[i].begin()); n   } n   int q; n   cin >> q; n   while (q--) { n     int a, b; n     cin >> a >> b; n     if (G[a].size() > G[b].size()) swap(a, b); n     if (G[a].size() == G[b].size() and a > b) swap(a, b); n     if (ans.count({a, b})) { n       cout << ans[{a, b}] <<   n ; n       continue; n     } n     int res = 0; n     for (auto u : G[a]) n       if (find(b, u) == find(a, u)) res++; n     ans[{a, b}] = res; n     cout << res <<   n ; n   } n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n long long maxi = LLONG_MAX; n long long mini = LLONG_MIN; n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n long long a[] = {1, 1, 3}; n void fn(long long n, long long x) { n   if (n <= 1) { n     for (long long i = 0; i < n; i++) { n       cout << x <<    ; n     } n     return; n   } n   long long t = (n) / 2; n   if (n == 3) { n     for (long long i = 0; i < 3; i++) { n       cout << x * a[i] <<    ; n     } n     return; n   } else { n     if (n % 2 == 1) cout << x <<    ; n     for (long long i = 0; i < t; i++) { n       cout << x <<    ; n     } n   } n   x *= 2; n   fn(t, x); n } n int main() { n   fast(); n   long long n; n   cin >> n; n   fn(n, 1); n   return 0; n } n