#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n const int D = 30; n struct node { n   int a, b; n }; n vector<int> road[N], h[N]; n vector<node> q[N]; n int n, m, t, fa[N][D + 1], w[N][D + 1], head[N], sum[N], ans[N], g[N]; n int cnt, tot, dep[N], id[N], size[N], c[N]; n int lowbit(int x) { return x & -x; } n void update(int x) { n   for (int i = x; i <= n; i += lowbit(i)) { n     c[i] += 1; n   } n } n int check(int x) { n   int tmp = 0; n   for (int i = x; i >= 1; i -= lowbit(i)) { n     tmp += c[i]; n   } n   return tmp; n } n struct edge { n   int to, nxt; n } e[N * 2]; n void add(int u, int v) { n   cnt++; n   e[cnt].nxt = head[u]; n   e[cnt].to = v; n   head[u] = cnt; n } n void dfs1(int u, int deep) { n   dep[u] = deep; n   id[u] = ++tot; n   size[u] = 1; n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (v == fa[u][0]) continue; n     dfs1(v, deep + 1); n     size[u] += size[v]; n   } n } n void dfs2(int u) { n   g[u] = u; n   for (int i = 0; i < h[u].size(); i++) { n     if (dep[h[u][i]] < dep[g[u]]) g[u] = h[u][i]; n   } n   for (int i = head[u]; i; i = e[i].nxt) { n     int v = e[i].to; n     if (v == fa[u][0]) continue; n     dfs2(v); n     if (dep[g[v]] < dep[g[u]]) g[u] = g[v]; n   } n   w[u][0] = g[u]; n } n int lca(int x, int y) { n   if (dep[x] < dep[y]) swap(x, y); n   for (int i = D; i >= 0; i--) { n     if (dep[fa[x][i]] >= dep[y]) x = fa[x][i]; n   } n   if (x == y) return x; n   for (int i = D; i >= 0; i--) { n     if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; n   } n   return fa[x][0]; n } n node work(int x, int LCA) { n   if (x == LCA) return (node){x, 0}; n   int tmp = 0; n   for (int i = D; i >= 0; i--) n     if (dep[w[x][i]] > dep[LCA]) x = w[x][i], tmp += (1 << i); n   if (w[x][0] == x) return (node){x, -1}; n   return (node){x, tmp}; n } n int main() { n   scanf( %d , &n); n   for (int i = 2; i <= n; i++) { n     scanf( %d , &fa[i][0]); n     add(fa[i][0], i); n     add(i, fa[i][0]); n   } n   dfs1(1, 1); n   for (int i = 1; i <= D; i++) n     for (int j = 1; j <= n; j++) { n       fa[j][i] = fa[fa[j][i - 1]][i - 1]; n     } n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     int c = lca(x, y); n     if (id[x] > id[y]) swap(x, y); n     road[id[x]].push_back(id[y]); n     if (dep[c] < dep[x]) h[x].push_back(c); n     if (dep[c] < dep[y]) h[y].push_back(c); n   } n   dfs2(1); n   for (int i = 1; i <= n; i++) { n   } n   for (int i = 1; i <= D; i++) n     for (int j = 1; j <= n; j++) { n       w[j][i] = w[w[j][i - 1]][i - 1]; n     } n   scanf( %d , &t); n   for (int i = 1; i <= t; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     int c = lca(x, y); n     node A = work(x, c); n     node B = work(y, c); n     if (A.b == -1 || B.b == -1) { n       ans[i] = -1; n       continue; n     } n     if (x == c || y == c) { n       ans[i] = A.b + B.b + 1; n       continue; n     } n     ans[i] = A.b + B.b + 2; n     x = A.a; n     y = B.a; n     if (id[x] > id[y]) swap(x, y); n     q[id[x] - 1].push_back((node){y, -i}); n     q[id[x] + size[x] - 1].push_back((node){y, i}); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < road[i].size(); j++) update(road[i][j]); n     for (int j = 0; j < q[i].size(); j++) { n       int num = (q[i][j].b > 0 ? 1 : -1); n       int x = q[i][j].a; n       int y = q[i][j].b * num; n       sum[y] += num * (check(id[x] + size[x] - 1) - check(id[x] - 1)); n     } n   } n   for (int i = 1; i <= t; i++) { n     printf( %d n , ans[i] - (sum[i] > 0)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct prog { n   int i; n   int cmp; n } a[100005]; n int ans[100005]; n bool cmp(prog a, prog b) { n   if (a.cmp == b.cmp) n     return a.i < b.i; n   else n     return a.cmp < b.cmp; n } n int main() { n   int n, m; n   while (cin >> n >> m) { n     priority_queue<prog> q; n     int i, j; n     for (i = 0; i < m; i++) { n       a[i].i = i + 1; n       a[i].cmp = (int)fabs((m + 1) / 2.0 - i - 1); n     } n     sort(a, a + m, cmp); n     for (i = 0; i < n; i++) { n       int k = i % m; n       cout << a[k].i << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 3e5 + 11, maxlg = 2e1; n typedef int i_N[maxN]; n typedef long long l_N[maxN]; n int N, M, Q, par[maxN][maxlg], umax[maxN][maxlg], umin[maxN][maxlg]; n vector<int> adj[maxN], pos[maxN]; n vector<pair<int, int> > query[maxN]; n pair<int, int> edge[maxN]; n i_N P, depth, mark, F; n l_N cnt, sum, mus, ans; n int lg(int x) { n   for (int i = maxlg; i; i--) n     if ((1 << i) <= x) return i; n   return (1 - 1); n } n int get_root(int u) { n   if (P[u] < (1 - 1)) return u; n   return P[u] = get_root(P[u]); n } n void join(int u, int v) { n   if (P[u] > P[v]) swap(P[u], P[v]); n   P[u] += P[v]; n   P[v] = u; n } n void dfs(int u) { n   for (int i = 1, x = lg(depth[u]); i <= x; i++) { n     par[u][i] = par[par[u][i - 1]][i - 1]; n     umax[u][i] = max(umax[u][i - 1], umax[par[u][i - 1]][i - 1]); n     umin[u][i] = min(umin[u][i - 1], umin[par[u][i - 1]][i - 1]); n   } n   for (int i = (1 - 1); i < (int)adj[u].size(); i++) { n     int v = adj[u][i]; n     if (par[v][(1 - 1)]) continue; n     par[v][(1 - 1)] = u; n     umax[v][(1 - 1)] = max(u, v); n     umin[v][(1 - 1)] = min(u, v); n     depth[v] = depth[u] + 1; n     dfs(v); n   } n } n void get(int u, int v, int& x, int& y) { n   if (depth[u] < depth[v]) swap(u, v); n   for (int i = lg(depth[u]); i >= (1 - 1); i--) n     if (depth[u] - (1 << i) >= depth[v]) { n       x = min(x, umin[u][i]); n       y = max(y, umax[u][i]); n       u = par[u][i]; n     } n   for (int i = lg(depth[u]); i >= (1 - 1); i--) n     if (par[u][i] != par[v][i]) { n       x = min(x, min(umin[u][i], umin[v][i])); n       y = max(y, max(umax[u][i], umax[v][i])); n       u = par[u][i]; n       v = par[v][i]; n     } n   if (u != v) { n     x = min(x, min(umin[u][(1 - 1)], umin[v][(1 - 1)])); n     y = max(y, max(umax[u][(1 - 1)], umax[v][(1 - 1)])); n   } n } n void update(long long* bit, int x, int v) { n   for (int i = x; i <= N; i |= i + 1) bit[i] += v; n } n long long get_bit(long long* bit, int x) { n   long long res = (1 - 1); n   for (int i = x; i >= 1; i = (i & (i + 1)) - 1) res += bit[i]; n   return res; n } n long long get_bit(long long* bit, int l, int r) { n   return get_bit(bit, r) - get_bit(bit, l - 1); n } n int main() { n   scanf( %d %d n , &N, &M); n   for (int i = 1; i <= M; i++) { n     int u, v; n     scanf( %d %d n , &u, &v); n     edge[i] = pair<int, int>(u, v); n   } n   memset(P, -1, sizeof(P)); n   for (int i = 1; i <= M; i++) { n     int u = edge[i].first, v = edge[i].second; n     int r1 = get_root(u), r2 = get_root(v); n     if (r1 != r2) { n       mark[i] = 1; n       join(r1, r2); n       adj[u].push_back(v); n       adj[v].push_back(u); n     } n   } n   for (int u = 1; u <= N; u++) n     if (!par[u][(1 - 1)]) { n       par[u][(1 - 1)] = -1; n       dfs(u); n     } n   for (int i = 1; i <= M; i++) n     if (!mark[i]) { n       int u = edge[i].first, v = edge[i].second; n       int x = 1e9, y = (1 - 1); n       get(u, v, x, y); n       F[x] = y; n     } n   F[N + 1] = N + 1; n   for (int i = N; i >= 1; i--) { n     if (F[i] == (1 - 1)) n       F[i] = F[i + 1]; n     else n       F[i] = min(F[i], F[i + 1]); n     pos[F[i]].push_back(i); n   } n   scanf( %d n , &Q); n   for (int i = 1; i <= Q; i++) { n     int l, r; n     scanf( %d %d n , &l, &r); n     query[r].push_back(pair<int, int>(l, i)); n   } n   for (int i = 1; i <= N; i++) { n     update(cnt, i, 1); n     update(sum, i, i - 1); n     for (int j = (1 - 1); j < (int)pos[i].size(); j++) { n       int p = pos[i][j]; n       update(cnt, p, -1); n       update(sum, p, 1 - p); n       update(mus, p, i - p); n     } n     for (int j = (1 - 1); j < (int)query[i].size(); j++) { n       int r = i, l = query[i][j].first, id = query[i][j].second; n       long long C = get_bit(cnt, l, r); n       long long S = get_bit(sum, l, r); n       long long T = get_bit(mus, l, r); n       ans[id] += C * i - S + T; n     } n   } n   for (int i = 1; i <= Q; i++) printf( %I64d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class L> n bool smax(T& x, L y) { n   return x < y ? (x = y, 1) : 0; n } n template <class T, class L> n bool smin(T& x, L y) { n   return x > y ? (x = y, 1) : 0; n } n const int maxn = 16; n int n, a[maxn], b[maxn][maxn], ex, x[maxn]; n bool mark[maxn]; n vector<vector<int> > all; n vector<int> todo; n int fond(int x) { n   for (int i = 0; i < n * n; i++) n     if (!mark[i] && a[i] == x) return i; n   return -1; n } n void back(int s = 0) { n   if (s == n) { n     for (int i = 0, sum = 0; i < n; i++, sum = 0) { n       for (int j = 0; j < n; j++) sum += b[j][i]; n       if (sum != ex) return; n     } n     { n       int sum = 0; n       for (int i = 0; i < n; i++) sum += b[i][i]; n       if (sum != ex) return; n     } n     { n       int sum = 0; n       for (int i = 0; i < n; i++) sum += b[i][n - i - 1]; n       if (sum != ex) return; n     } n     for (int i = 0; i < n; i++, cout <<   n ) n       for (int j = 0; j < n; j++) cout << b[i][j] <<    ; n     exit(0); n   } n   if (s == n - 1) { n     fill(b[n - 1], b[n - 1] + n, ex); n     for (int i = 0; i < n - 1; i++) n       for (int j = 0; j < n; j++) b[n - 1][j] -= b[i][j]; n     int i = 0; n     for (int tmp; i < n; i++) n       if (~(tmp = fond(b[n - 1][i]))) n         mark[tmp] = 1, todo.push_back(tmp); n       else n         break; n     if (i == n) back(n); n     for (auto x : todo) mark[x] = 0; n     todo.clear(); n   } n   for (auto v : all) { n     bool reval = 1; n     for (auto x : v) n       if (mark[x]) { n         reval = 0; n         break; n       } n     if (!reval) continue; n     do { n       for (int i = 0; i < n; i++) b[s][i] = a[v[i]], mark[v[i]] = 1; n       back(s + 1); n       for (int i = 0; i < n; i++) mark[v[i]] = 0; n     } while (next_permutation(v.begin(), v.end())); n   } n } n void tof(int idx) { n   if (idx == 4 && x[0] + x[1] + x[2] + x[3] != ex) return; n   if (idx == 8 && x[4] + x[5] + x[6] + x[7] != ex) return; n   if (idx == 12 && x[8] + x[9] + x[10] + x[11] != ex) return; n   if (idx == 16 && x[12] + x[13] + x[14] + x[15] != ex) return; n   if (idx == 13 && x[0] + x[4] + x[8] + x[12] != ex) return; n   if (idx == 14 && x[1] + x[5] + x[9] + x[13] != ex) return; n   if (idx == 15 && x[2] + x[6] + x[10] + x[14] != ex) return; n   if (idx == 16 && x[3] + x[7] + x[11] + x[15] != ex) return; n   if (idx == 13 && x[3] + x[6] + x[9] + x[12] != ex) return; n   if (idx == 16 && x[0] + x[5] + x[10] + x[15] != ex) return; n   if (idx == 16) { n     for (int i = 0; i < n; i++, cout <<   n ) n       for (int j = 0; j < n; j++) cout << x[i * 4 + j] <<    ; n     exit(0); n   } n   for (int i = 0; !(i >> 4); ++i) n     if (!mark[i]) { n       x[idx] = a[i], mark[i] = 1; n       tof(idx + 1); n       mark[i] = 0; n     } n } n int main() { n   srand(time(0)); n   ios::sync_with_stdio(0), cin.tie(); n   cin >> n; n   for (int i = 0; i < n * n; i++) cin >> a[i], ex += a[i]; n   random_shuffle(a, a + n * n); n   ex /= n; n   cout << ex <<   n ; n   if (n < 4) { n     for (int i = 0; i < 1 << n * n; i++) n       if (__builtin_popcount(i) == n) { n         int sum = 0; n         for (int j = 0; j < n * n; j++) sum += (i >> j & 1) * a[j]; n         if (sum == ex) { n           all.push_back({}); n           for (int j = 0; j < n * n; j++) n             if (i >> j & 1) all.back().push_back(j); n           sort(all.back().begin(), all.back().end()); n         } n       } n     return back(), 0; n   } n   tof(0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<pair<pair<int, int>, pair<int, int> > > s; n vector<int> ed[2][300000]; n int n, m, k; n int a[300000], b[300000]; n long long ans[300000]; n bool add(int x, int y, int dx, int dy) { n   pair<pair<int, int>, pair<int, int> > g = n       make_pair(make_pair(x, y), make_pair(dx, dy)); n   if (s.find(g) != s.end()) return false; n   s.insert(g); n   return true; n } n void go(int x, int y, int dx, int dy, long long t) { n   int tt = 1; n   int d = x - y + m; n   if (dx - dy != 0) { n     tt = 0; n     d = x + y; n   } n   bool f = add(x, y, dx, dy); n   if (!f) return; n   for (int j = 0; j < ed[tt][d].size(); j++) { n     int kx = a[ed[tt][d][j]]; n     int ky = b[ed[tt][d][j]]; n     kx = kx - x; n     ky = ky - y; n     kx = kx / dx; n     ky = ky / dy; n     int g = ed[tt][d][j]; n     if (ans[g] != -1) continue; n     ans[g] = t + min(kx, ky); n   } n   if (dx == 1 && dy == 1) { n     int v = min(n - x, m - y); n     x += v; n     y += v; n     if (x == n && y == m) return; n     if (y == m) n       go(x, y, 1, -1, t + v); n     else n       go(x, y, -1, 1, t + v); n   } n   if (dx == -1 && dy == -1) { n     int v = min(x, y); n     x -= v; n     y -= v; n     if (x == 0 && y == 0) return; n     if (y == 0) n       go(x, y, -1, 1, t + v); n     else n       go(x, y, 1, -1, t + v); n   } n   if (dx == 1 && dy == -1) { n     int v = min(n - x, y); n     x += v; n     y -= v; n     if (x == n && y == 0) return; n     if (y == 0) n       go(x, y, 1, 1, t + v); n     else n       go(x, y, -1, -1, t + v); n   } n   if (dx == -1 && dy == 1) { n     int v = min(x, m - y); n     x -= v; n     y += v; n     if (x == 0 && y == m) return; n     if (x == 0) n       go(x, y, 1, 1, t + v); n     else n       go(x, y, -1, -1, t + v); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= k; i++) ans[i] = -1; n   for (int i = 1; i <= k; i++) { n     cin >> a[i] >> b[i]; n     ed[0][a[i] + b[i]].push_back(i); n     ed[1][a[i] - b[i] + m].push_back(i); n   } n   go(0, 0, 1, 1, 0); n   for (int i = 1; i <= k; i++) cout << ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   char ch = getchar(); n   long long 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 int main(int argc, char *argv[]) { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long base = (1LL << 7) - 1; n   long long res = 0, cur; n   printf( ? ); n   for (long long i = (1); i <= (100); i += (1)) printf(  %lld , i); n   putchar(  n ); n   fflush(stdout); n   cur = read(); n   res += (cur >> 7) << 7; n   printf( ? ); n   for (long long i = (1); i <= (100); i += (1)) printf(  %lld , i << 7); n   putchar(  n ); n   fflush(stdout); n   cur = read(); n   res += cur & base; n   printf( ! %lld n , res); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 205, MAXL = 1 << 20; n char qq[MAXL]; n char s[MAX][MAX]; n pair<int, int> kamo[MAX][MAX]; n vector<pair<int, int> > R[MAX][MAX]; n int indeg[MAX][MAX]; n int cik[MAX][MAX]; n int imacik[MAX * MAX][10]; n int brred, brstup; n int pr[10], ps[10]; n int bio[MAX][MAX]; n int dokle[MAX][MAX]; n int imaq[MAXL][10]; n int brc = 1; n int Vani(int red, int stup) { n   return red < 0 || red >= brred || stup < 0 || stup >= brstup; n } n void Osisaj() { n   queue<pair<int, int> > Q; n   for (int i = 0; i < brred; i++) n     for (int j = 0; j < brstup; j++) n       if (!indeg[i][j]) Q.push(pair<int, int>(i, j)); n   for (; !Q.empty();) { n     pair<int, int> tmp = Q.front(); n     bio[tmp.first][tmp.second] = 1; n     Q.pop(); n     pair<int, int> nn = kamo[tmp.first][tmp.second]; n     indeg[nn.first][nn.second]--; n     if (!indeg[nn.first][nn.second]) Q.push(nn); n   } n   for (int i = 0; i < brred; i++) { n     for (int j = 0; j < brstup; j++) { n       if (!bio[i][j]) { n         int sz = 0; n         for (pair<int, int> tmp = pair<int, int>(i, j); n              tmp != pair<int, int>(i, j) || !sz; n              sz++, tmp = kamo[tmp.first][tmp.second]) { n           bio[tmp.first][tmp.second] = 1; n           cik[tmp.first][tmp.second] = brc; n           imacik[brc][s[tmp.first][tmp.second] -  0 ] = 1; n         } n         brc++; n       } n     } n   } n } n int Rijesi() { n   queue<pair<int, int> > Q; n   memset(dokle, 0, sizeof dokle); n   int len = (int)strlen(qq); n   for (int i = 0; i <= len; i++) n     for (int j = 0; j < 10; j++) imaq[i][j] = 0; n   for (int i = len - 1; i >= 0; i--) { n     imaq[i][qq[i] -  0 ] = 1; n     for (int j = 0; j < 10; j++) imaq[i][j] |= imaq[i + 1][j]; n   } n   for (int i = 0; i < brred; i++) n     for (int j = 0; j < brstup; j++) { n       indeg[i][j] = (int)R[i][j].size(); n       if (!indeg[i][j]) Q.push(pair<int, int>(i, j)); n     } n   for (; !Q.empty();) { n     pair<int, int> tmp = Q.front(); n     Q.pop(); n     int nd = dokle[tmp.first][tmp.second]; n     if (s[tmp.first][tmp.second] == qq[nd]) nd++; n     if (nd == len) return 1; n     pair<int, int> nn = kamo[tmp.first][tmp.second]; n     dokle[nn.first][nn.second] = max(dokle[nn.first][nn.second], nd); n     indeg[nn.first][nn.second]--; n     if (!indeg[nn.first][nn.second]) Q.push(nn); n   } n   for (int i = 0; i < brred; i++) { n     for (int j = 0; j < brstup; j++) { n       if (cik[i][j]) { n         int da = 1; n         int indc = cik[i][j]; n         for (int k = 0; k < 10; k++) n           if (!imacik[indc][k] && imaq[dokle[i][j]][k]) da = 0; n         if (da) return 1; n       } n     } n   } n   return 0; n } n int main() { n   int brq; n   scanf( %d%d%d , &brred, &brstup, &brq); n   for (int i = 0; i < brred; i++) scanf( %s , s[i]); n   for (int i = 0; i < 10; i++) scanf( %d%d , &pr[i], &ps[i]); n   for (int i = 0; i < brred; i++) { n     for (int j = 0; j < brstup; j++) { n       int nr = i + pr[s[i][j] -  0 ]; n       int ns = j + ps[s[i][j] -  0 ]; n       if (Vani(nr, ns)) n         kamo[i][j] = pair<int, int>(i, j); n       else n         kamo[i][j] = pair<int, int>(nr, ns); n       indeg[kamo[i][j].first][kamo[i][j].second]++; n       R[kamo[i][j].first][kamo[i][j].second].push_back(pair<int, int>(i, j)); n     } n   } n   Osisaj(); n   for (int i = 0; i < brq; i++) { n     scanf( %s , qq); n     printf( %s n , Rijesi() ?  YES  :  NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n struct Segtree { n   int N; n   vector<T> seg, lazy; n   Segtree(int _n) : N(_n) { n     seg.resize(4 * N, 0); n     lazy.resize(4 * N, 0); n   } n   T join(T x, T y) { return max(x, y); } n   void push(int x, int s, int e) { n     if (!lazy[x]) return; n     seg[x] = max(seg[x], lazy[x]); n     if (s != e) { n       lazy[2 * x] = max(lazy[2 * x], lazy[x]); n       lazy[2 * x + 1] = max(lazy[2 * x + 1], lazy[x]); n     } n     lazy[x] = 0; n   } n   void build(int x, int s, int e) { n     if (s == e) { n       seg[x] = 0; n       return; n     } n     int mid = (s + e) >> 1; n     build(2 * x, s, mid); n     build(2 * x + 1, mid + 1, e); n     seg[x] = join(seg[2 * x], seg[2 * x + 1]); n   } n   void update(int x, int s, int e, int l, int r, T val) { n     push(x, s, e); n     if (s > r || e < l) return; n     if (s >= l && e <= r) { n       lazy[x] = val; n       push(x, s, e); n       return; n     } n     int mid = (s + e) >> 1; n     update(2 * x, s, mid, l, r, val); n     update(2 * x + 1, mid + 1, e, l, r, val); n     seg[x] = join(seg[2 * x], seg[2 * x + 1]); n   } n   T query(int x, int s, int e, int l, int r) { n     push(x, s, e); n     if (e < l || s > r) return 0; n     if (s >= l && e <= r) { n       return seg[x]; n     } n     int mid = (s + e) >> 1; n     return join(query(2 * x, s, mid, l, r), query(2 * x + 1, mid + 1, e, l, r)); n   } n   void update(int l, int r, T val) { update(1, 0, N - 1, l, r, val); } n   T query(int l, int r) { return query(1, 0, N - 1, l, r); } n }; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, q; n   cin >> n >> q; n   vector<int> x(q), y(q), d(q), cc(1, 0); n   for (int i = 0; i < q; ++i) { n     char c; n     cin >> x[i] >> y[i] >> c; n     d[i] = (c ==  U ); n     cc.push_back(x[i]); n     cc.push_back(y[i]); n     cc.push_back(n + 1 - x[i]); n     cc.push_back(n + 1 - y[i]); n   } n   sort(cc.begin(), cc.end()); n   cc.erase(unique(cc.begin(), cc.end()), cc.end()); n   Segtree<int> left(cc.size() + 1), up(cc.size() + 1); n   for (int i = 0; i < q; ++i) { n     x[i] = lower_bound(cc.begin(), cc.end(), x[i]) - cc.begin(); n     y[i] = lower_bound(cc.begin(), cc.end(), y[i]) - cc.begin(); n     if (d[i]) { n       int id = up.query(y[i], y[i]); n       up.update(y[i], y[i], y[i]); n       cout << cc[y[i]] - cc[id] <<   n ; n       id = lower_bound(cc.begin(), cc.end(), n + 1 - cc[id]) - cc.begin(); n       y[i] = lower_bound(cc.begin(), cc.end(), n + 1 - cc[y[i]]) - cc.begin(); n       left.update(y[i], id, x[i]); n     } else { n       int id = left.query(x[i], x[i]); n       left.update(x[i], x[i], x[i]); n       cout << cc[x[i]] - cc[id] <<   n ; n       id = lower_bound(cc.begin(), cc.end(), n + 1 - cc[id]) - cc.begin(); n       x[i] = lower_bound(cc.begin(), cc.end(), n + 1 - cc[x[i]]) - cc.begin(); n       up.update(x[i], id, y[i]); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n void addmod(int &a, long long b) { n   a = (a + b); n   if (a >= 1000000007) a -= 1000000007; n } n void mulmod(int &a, long long b) { a = (a * b) % 1000000007; } n template <class T> n bool domin(T &a, const T &b) { n   return a > b ? a = b, 1 : 0; n } n template <class T> n bool domax(T &a, const T &b) { n   return a < b ? a = b, 1 : 0; n } n int gi() { n   int a; n   scanf( %d , &a); n   return a; n } n long long gll() { n   long long a; n   scanf( %lld , &a); n   return a; n } n int n; n namespace treap { n struct treap { n   int val, size, lazy; n   long long rnd; n   treap *l, *r; n   treap() n       : val(0), n         size(0), n         lazy(0), n         rnd((((long long)rand()) << 32) | rand()), n         l(NULL), n         r(NULL) {} n   treap(int val) n       : val(val), n         size(1), n         lazy(0), n         rnd((((long long)rand()) << 32) | rand()), n         l(NULL), n         r(NULL) {} n   void push() { n     if (lazy == 0) return; n     val += lazy; n     if (l != NULL) l->lazy += lazy; n     if (r != NULL) r->lazy += lazy; n     lazy = 0; n   } n   void updateSz() { n     size = 1; n     if (l != NULL) size += l->size; n     if (r != NULL) size += r->size; n   } n }; n typedef treap *ptreap; n ptreap root = NULL; n void split(ptreap tp, int key, ptreap &l, ptreap &r) { n   if (tp == NULL) { n     l = NULL, r = NULL; n     return; n   } n   tp->push(); n   if (tp->val < key) n     l = tp, split(tp->r, key, tp->r, r); n   else n     r = tp, split(tp->l, key, l, tp->l); n   if (l != NULL) l->updateSz(); n   if (r != NULL) r->updateSz(); n } n pair<ptreap, ptreap> split(int v) { n   ptreap l, r; n   split(root, v, l, r); n   return {l, r}; n } n pair<ptreap, ptreap> split(ptreap tp, int v) { n   ptreap l, r; n   split(tp, v, l, r); n   return {l, r}; n } n void insert(ptreap &tp, ptreap v) { n   if (tp == NULL) { n     tp = v; n     return; n   } n   tp->push(); n   if (v->rnd < tp->rnd) n     insert(v->val > tp->val ? tp->r : tp->l, v); n   else n     split(tp, v->val, v->l, v->r), tp = v; n   tp->updateSz(); n } n void insert(int v) { n   ptreap tp = new treap(v); n   insert(root, tp); n } n void merge(ptreap &t, ptreap l, ptreap r) { n   if (l != NULL) l->push(); n   if (r != NULL) r->push(); n   if (l == NULL || r == NULL) n     t = (l == NULL ? r : l); n   else if (l->rnd > r->rnd) n     t = l, merge(t->r, l->r, r); n   else n     t = r, merge(t->l, l, r->l); n   if (t != NULL) t->updateSz(); n } n void erase(ptreap &tp, int v) { n   if (tp == NULL) return; n   tp->push(); n   if (v == tp->val) { n     merge(tp, tp->l, tp->r); n   } else { n     erase(v < tp->val ? tp->l : tp->r, v); n     tp->updateSz(); n   } n } n int getmin(ptreap tp) { n   if (tp == NULL) return 0; n   tp->push(); n   return tp->l == NULL ? tp->val : getmin(tp->l); n } n int getmax(ptreap tp) { n   if (tp == NULL) return 0; n   tp->push(); n   return tp->r == NULL ? tp->val : getmax(tp->r); n } n void recalc(ptreap tp) { n   if (tp == NULL) return; n   tp->push(); n   recalc(tp->l); n   recalc(tp->r); n   tp->updateSz(); n } n void debug(ptreap &tp) { n   if (tp == NULL) return; n   tp->push(); n   if (tp->l != NULL) debug(tp->l); n   cout <<        << tp->val <<  :  << tp->rnd <<  :  << tp->size << endl; n   if (tp->r != NULL) debug(tp->r); n } n }  // namespace treap n int main() { n   n = gi(); n   for (int i = 0; i < n; i++) { n     int l = gi(), r = gi(); n     auto v = treap::split(l); n     auto v2 = treap::split(v.second, r); n     if (v2.first != NULL) v2.first->lazy++; n     if (v2.second != NULL) { n       auto v = treap::getmin(v2.second); n       treap::erase(v2.second, v); n     } n     treap::merge(treap::root, v.first, v2.first); n     treap::merge(treap::root, treap::root, v2.second); n     treap::insert(l); n   } n   cout << treap::root->size << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n int l = 1, r = 0; n int a[MAXN], b[MAXN], now[MAXN], num[MAXN << 1], cnt[MAXN], ans[MAXN]; n map<int, int> ms; n struct Change { n   int p, x, y; n } c[MAXN]; n struct Query { n   int l, r, t, id; n   bool operator<(const Query &A) const { n     if (b[l] == b[A.l]) return b[r] == b[A.r] ? t < A.t : b[r] < b[A.r]; n     return b[l] < b[A.l]; n   } n } q[MAXN]; n void update(int x, int d) { n   int &y = num[x]; n   if (y > 0) cnt[y]--; n   y += d; n   if (y > 0) cnt[y]++; n } n void modify(int p, int d) { n   if (l <= p && p <= r) { n     update(a[p], -1); n     update(d, 1); n   } n   a[p] = d; n } n int get() { n   for (int i = 1;; i++) { n     if (!cnt[i]) return i; n   } n } n int main() { n   int n, m, id = 0, Time = 0, sz = 0; n   scanf( %d%d , &n, &m); n   int B = 2000; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     int &t = ms[a[i]]; n     if (!t) t = ++sz; n     now[i] = a[i] = t; n     b[i] = i / B + 1; n   } n   while (m--) { n     int op, x, y; n     scanf( %d%d%d , &op, &x, &y); n     if (op == 1) n       q[++id] = (Query){x, y, Time, id}; n     else { n       int &t = ms[y]; n       if (!t) t = ++sz; n       c[++Time] = (Change){x, y = t, now[x]}; n       now[x] = y; n     } n   } n   sort(q + 1, q + id + 1); n   int v = 0; n   for (int i = 1; i <= id; i++) { n     while (v < q[i].t) modify(c[v + 1].p, c[v + 1].x), v++; n     while (v > q[i].t) modify(c[v].p, c[v].y), v--; n     while (l < q[i].l) update(a[l], -1), l++; n     while (l > q[i].l) update(a[l - 1], 1), l--; n     while (r < q[i].r) update(a[r + 1], 1), r++; n     while (r > q[i].r) update(a[r], -1), r--; n     ans[q[i].id] = get(); n   } n   for (int i = 1; i <= id; i++) { n     printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = 4.0 * atan(1.0); n void EnchomSolve() { n   int l; n   long double L; n   long double ans = 0.0; n   long double area; n   long double height; n   long double help, help2; n   scanf( %d , &l); n   L = (long double)l; n   help = L / sqrt(3.0); n   height = sqrt(L * L - help * help); n   help = (sqrt(3.0) / 2.0) * L; n   area = (help * L) / 2.0; n   ans += area * height / 3.0; n   scanf( %d , &l); n   L = (long double)l; n   help = (sqrt(2.0) / 2.0) * L; n   height = sqrt(L * L - help * help); n   area = L * L; n   ans += area * height / 3.0; n   scanf( %d , &l); n   L = (long double)l; n   help = L / (sqrt(2.0 - 2.0 * cos(72.0 * PI / 180.0))); n   height = sqrt(L * L - help * help); n   help2 = sqrt(help * help - (L / 2.0) * (L / 2.0)); n   area = L * help2 / 2.0; n   area *= 5.0; n   ans += area * height / 3.0; n   printf( %.10f n , (double)ans); n   return; n } n int main() { n   EnchomSolve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c; n   cin >> a >> b >> c; n   int x = min(a, b / 2); n   int y = min(x, c / 4); n   cout << y * 7; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct EDGE { n   int to, next, w, co; n } e[100000]; n int head[100], top, vis[100000]; n void add(int u, int v, int w, int co) { n   e[top].to = v; n   e[top].w = w; n   e[top].co = co; n   e[top].next = head[u]; n   head[u] = top++; n } n struct _ { n   int max_flow, co; n }; n int dis[100], vvis[100], prep[100], pree[100]; n queue<int> q; n deque<int> node; n void spfa(int s) { n   memset(dis, 63, sizeof(dis)); n   memset(vvis, 0, sizeof(vvis)); n   dis[s] = 0; n   q.push(s); n   int i; n   while (!q.empty()) { n     s = q.front(); n     q.pop(); n     vvis[s] = 0; n     int i; n     for (i = head[s]; ~i; i = e[i].next) { n       if (!e[i].w) continue; n       if (dis[s] + e[i].co < dis[e[i].to]) { n         dis[e[i].to] = dis[s] + e[i].co; n         pree[e[i].to] = i; n         prep[e[i].to] = s; n         if (vvis[e[i].to]) continue; n         vvis[e[i].to] = 1; n         q.push(e[i].to); n       } n     } n   } n } n _ dfs(int s, int t, int rest) { n   int flow = 998244353, co = 0; n   int temp = t; n   _ rv; n   while (t != s) { n     flow = min(flow, e[pree[t]].w); n     co += e[pree[t]].co; n     t = prep[t]; n   } n   if (co > 0) flow = min(flow, rest / co); n   if (flow == 0) return rv = (_){0, 0}; n   t = temp; n   while (t != s) { n     temp = pree[t]; n     e[temp].w -= flow; n     e[temp ^ 1].w += flow; n     t = prep[t]; n   } n   rv = (_){flow, co}; n   return rv; n } n long long EK(int s, int t, int rest) { n   long long sum = 0; n   _ _; n   while (1) { n     spfa(s); n     if (dis[t] > 998244353) break; n     _ = dfs(s, t, rest); n     if (_.max_flow == 0) break; n     sum += _.max_flow; n     rest -= _.max_flow * _.co; n   } n   return sum; n } n int main() { n   memset(head, 255, sizeof(head)); n   int n, k; n   scanf( %d%d , &n, &k); n   int i, j, t; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) { n       scanf( %d , &t); n       if (t) { n         add(i, j, t, 0); n         add(j, i, 0, 0); n         add(i, j, k, 1); n         add(j, i, 0, -1); n       } n     } n   } n   long long ans = EK(1, n, k); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct station { n   int pos, price; n } a[200003]; n int T[800008], d, n, m; n long long cnt; n bool comp(station A, station B) { return (A.pos < B.pos); } n void Build_MinIT(int, int, int); n int Find_Min(int, int, int, int, int); n int main() { n   scanf( %d %d %d , &d, &n, &m); n   for (int i = 1; i <= m; i++) scanf( %d %d , &a[i].pos, &a[i].price); n   sort(a + 1, a + 1 + m, comp); n   a[++m].pos = d; n   for (int i = 1; i <= m; i++) { n     if (a[i].pos - a[i - 1].pos > n) { n       cout << -1; n       return 0; n     } n   } n   if (n >= d) { n     cout << 0; n     return 0; n   } n   a[200002].price = 1E+9; n   Build_MinIT(1, m, 1); n   int i = 0, j = 1, t = n; n   while (j <= m) { n     while (a[i].price < a[j].price && a[j].pos - a[i].pos <= n && j <= m) j++; n     if (a[j].pos - a[i].pos <= n) { n       cnt += (long long)(a[j].pos - (a[i].pos + t)) * a[i].price; n       t = 0; n       i = j++; n     } else { n       int k = Find_Min(i + 1, j - 1, 1, m, 1); n       cnt += (long long)(n - t) * a[i].price; n       t = a[i].pos + n - a[k].pos; n       i = k; n     } n   } n   cout << cnt; n   return 0; n } n void Build_MinIT(int L, int R, int v) { n   if (L == R) { n     T[v] = L; n     return; n   } n   int m = (L + R) / 2; n   Build_MinIT(L, m, v * 2); n   Build_MinIT(m + 1, R, v * 2 + 1); n   T[v] = (a[T[v * 2]].price < a[T[v * 2 + 1]].price ? T[v * 2] : T[v * 2 + 1]); n } n int Find_Min(int l, int r, int L, int R, int v) { n   if (l > r) return 200002; n   if (l == L && r == R) return T[v]; n   int m = (L + R) / 2; n   int x = Find_Min(l, min(r, m), L, m, v * 2), n       y = Find_Min(max(l, m + 1), r, m + 1, R, v * 2 + 1); n   return (a[x].price < a[y].price ? x : y); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   double lo = 0.0, hi = 10000000.0; n   for (int i = 1; i <= n; i++) { n     int sta; n     cin >> sta; n     lo = max(lo, (double)10.0 * sta / i); n     hi = min(hi, (double)10.0 * (sta + 1) / i); n   } n   int nextlo = floor(lo * (n + 1) / 10.0); n   int nexthi = ceil(hi * (n + 1) / 10.0); n   if (nextlo + 1 >= nexthi) { n     cout <<  unique  << endl << nextlo << endl; n   } else { n     cout <<  not unique  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:102400000,102400000 ) n using namespace std; n int lowbit(int x) { return (x & -x); } n template <class T_> n T_ f_abs(T_ x) { n   return x < 0 ? -x : x; n } n template <class T_> n T_ f_max(T_ a, T_ b) { n   return a > b ? a : b; n } n template <class T_> n T_ f_min(T_ a, T_ b) { n   return a < b ? a : b; n } n template <class T_> n void chkmax(T_ &a, T_ b) { n   if (a == -1 || a < b) a = b; n } n template <class T_> n void chkmin(T_ &a, T_ b) { n   if (a == -1 || a > b) a = b; n } n template <class T_> n T_ gcd(T_ a, T_ b) { n   while (T_ t = a % b) a = b, b = t; n   return b; n } n template <class T_> n void f_swap(T_ &a, T_ &b) { n   T_ t = a; n   a = b; n   b = t; n } n const double eps = 1e-9; n const int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1}, n           dy[] = {0, 0, -1, 1, -1, 1, -1, 1}; n const int inf = 0x7fffffff; n const int mod = 1000000007; n const int N = 100003; n const int MAXV = 100003; n int n, m, cnt, who[N], dsu[N]; n bool vis[N], tree[N << 2]; n vector<pair<int, int> > vt[N], qu[N]; n struct Oper { n   int tp, x, y; n   bool tag; n   Oper() {} n   Oper(int tp_, int x_, int y_) { n     tp = tp_; n     x = x_; n     y = y_; n   } n } op[N]; n struct Edge { n   int to, next; n } edg[MAXV << 1]; n int total, idx, root, first[MAXV]; n int siz[MAXV]; n int dep[MAXV]; n int top[MAXV]; n int fa[MAXV]; n int son[MAXV]; n int id[MAXV]; n void Dfs_1(int u) { n   int e, v, i; n   siz[u] = 1; n   son[u] = 0; n   for (e = 0; e < (int)vt[u].size(); ++e) { n     v = vt[u][e].first; n     i = vt[u][e].second; n     op[i].tag = !vis[v]; n   } n   vis[u] = true; n   for (e = first[u]; ~e; e = edg[e].next) { n     v = edg[e].to; n     if (v != fa[u]) { n       fa[v] = u; n       dep[v] = dep[u] + 1; n       Dfs_1(v); n       if (son[u] == 0 || siz[v] > siz[son[u]]) son[u] = v; n       siz[u] += siz[v]; n     } n   } n   for (e = 0; e < (int)vt[u].size(); ++e) { n     v = vt[u][e].first; n     i = vt[u][e].second; n     if (op[i].tag && !vis[v]) op[i].tag = false; n   } n } n void Dfs_2(int u, int tp) { n   id[u] = ++idx; n   top[u] = tp; n   if (son[u] != 0) Dfs_2(son[u], top[u]); n   for (int e = first[u]; ~e; e = edg[e].next) { n     int v = edg[e].to; n     if (v != son[u] && v != fa[u]) Dfs_2(v, v); n   } n } n void BuildChain() { n   Dfs_1(root); n   Dfs_2(root, root); n } n void Build(int l, int r, int root) { n   tree[root] = false; n   if (l == r) return; n   int mid = ((l + r) >> 1); n   Build(l, mid, (root << 1)); n   Build(mid + 1, r, (root << 1 | 1)); n } n void Update(int p, int l, int r, int root) { n   if (l == r) { n     tree[root] = true; n     return; n   } n   int mid = ((l + r) >> 1); n   if (p <= mid) n     Update(p, l, mid, (root << 1)); n   else n     Update(p, mid + 1, r, (root << 1 | 1)); n   tree[root] = (tree[(root << 1)] && tree[(root << 1 | 1)]); n } n bool Query(int L, int R, int l, int r, int root) { n   if (L <= l && r <= R) return tree[root]; n   int mid = ((l + r) >> 1); n   bool res = true; n   if (L <= mid) res = (res && Query(L, R, l, mid, (root << 1))); n   if (R > mid) res = (res && Query(L, R, mid + 1, r, (root << 1 | 1))); n   return res; n } n bool FindChain(int u, int v) { n   int u_top = top[u], v_top = top[v], L, R; n   while (u_top != v_top) { n     if (dep[u_top] < dep[v_top]) { n       f_swap(u_top, v_top); n       f_swap(u, v); n     } n     L = id[u_top]; n     R = id[u]; n     if (!Query(L, R, 1, idx, 1)) return false; n     u = fa[u_top]; n     u_top = top[u]; n   } n   if (u != v) { n     if (dep[u] > dep[v]) f_swap(u, v); n     L = id[son[u]]; n     R = id[v]; n     if (!Query(L, R, 1, idx, 1)) return false; n   } n   return true; n } n void addedge(int from, int to) { n   edg[total].to = to; n   edg[total].next = first[from]; n   first[from] = total++; n } n void Init() { n   cnt = 0; n   root = n + 1; n   total = idx = fa[root] = dep[root] = 0; n   for (int i = 1; i <= n + 1; ++i) { n     first[i] = -1; n     dsu[i] = i; n     vt[i].clear(); n     vis[i] = false; n   } n } n void GetData() { n   int tp, x, y; n   for (int i = 0; i < m; ++i) { n     scanf( %d%d , &tp, &x); n     if (tp != 2) scanf( %d , &y); n     if (tp == 1) { n       dsu[x] = y; n       addedge(y, x); n     } else if (tp == 2) { n       y = -1; n       who[++cnt] = x; n       qu[cnt].clear(); n     } else { n       vt[x].push_back(make_pair(who[y], i)); n       qu[y].push_back(make_pair(x, i)); n     } n     op[i] = Oper(tp, x, y); n   } n } n void Solve() { n   int i, j, tp, p; n   bool flag; n   for (i = 1; i <= n; ++i) { n     if (dsu[i] == i) addedge(n + 1, i); n   } n   BuildChain(); n   Build(1, idx, 1); n   for (i = cnt = 0; i < m; ++i) { n     tp = op[i].tp; n     if (tp == 1) n       Update(id[op[i].x], 1, idx, 1); n     else if (tp == 2) { n       ++cnt; n       for (j = 0; j < (int)qu[cnt].size(); ++j) { n         p = qu[cnt][j].second; n         if (op[p].tag) n           flag = FindChain(who[cnt], qu[cnt][j].first); n         else n           flag = false; n         vis[p] = flag; n       } n     } n   } n   for (i = 0; i < m; ++i) { n     if (op[i].tp == 3) puts(vis[i] ?  YES  :  NO ); n   } n } n int main() { n   while (~scanf( %d%d , &n, &m)) { n     Init(); n     GetData(); n     Solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[111]; n int main() { n   int n; n   scanf( %d , &n); n   arr[0] = 0; n   for (int i = (1); i < (n + 1); ++i) scanf( %d , &arr[i]); n   int ans = 0; n   if (n % 2 == 0 || n == 1) { n     printf( -1 n ); n     return 0; n   } n   for (int i = n; i >= 1; i -= 2) { n     int tmp = max(arr[i], arr[i - 1]); n     ans += tmp; n     arr[i] = arr[i - 1] = 0; n     arr[(i - 1) / 2] = max(0, arr[(i - 1) / 2] - tmp); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace {}; n int sgn(int x) { return x >= 0 ? 1 : -1; } n int arr[200001]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     arr[i] = sgn(arr[i]); n     if (arr[i] == -1) k--; n   } n   if (k < 0) { n     cout <<  -1 n ; n     return 0; n   } n   vector<pair<int, int>> segs; n   int flips = 0; n   int curr = 1, c = 0; n   for (int i = 0; i < n; i++) { n     if (arr[i] == curr) n       c++; n     else { n       flips++; n       if (curr == 1 && i - c > 0) segs.emplace_back(c, -2); n       c = 1; n       curr = arr[i]; n     } n   } n   for (auto seg : segs) { n     ; n   } n   sort(segs.begin(), segs.end()); n   ; n   for (auto seg : segs) { n     if (seg.first > k) break; n     k -= seg.first; n     flips += seg.second; n   } n   if (curr == 1 && c > 0) { n     if (k >= c) flips--; n   } n   if (flips < 0) flips = 0; n   cout << flips <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct info { n   long long fs, sm, id; n } bd[100001]; n int cmp(info a, info b) { return a.fs < b.fs; } n int main() { n   ios_base::sync_with_stdio(0); n   long long int i, j, x, n, cnt = 0, m, y, k, ans = 0, cf, cm, a, rr1 = 0, l, r, n                             md, r1, r2, xxx, flg; n   cin >> n >> a >> cf >> cm >> m; n   long long in[n + 1], bs[n + 1]; n   for (i = 1; i <= n; i++) { n     cin >> in[i]; n     bd[i].fs = a - in[i]; n     bd[i].id = i; n     bs[i] = a - in[i]; n   } n   sort(bd + 1, bd + 1 + n, cmp); n   sort(bs + 1, bs + 1 + n); n   for (i = 2; i <= n; i++) { n     bd[i].fs += bd[i - 1].fs; n   } n   bd[n].sm = 0; n   for (i = n - 1; i >= 1; i--) { n     x = abs(in[bd[i + 1].id] - in[bd[i].id]); n     bd[i].sm = bd[i + 1].sm + x * (n - i); n   } n   bd[0].fs = bd[0].id = 0; n   bd[0].sm = (a - in[bd[1].id]) * n + bd[1].sm; n   for (i = 1; i <= n; i++) { n     if (bs[i] > 0) break; n     cnt++; n   } n   ans = cnt * cf + cm * in[bd[n].id]; n   for (i = n; i >= 0; i--) { n     x = m; n     y = cnt * cf; n     rr1 = cnt; n     flg = 0; n     if (x >= bd[i].fs) { n       flg = 1; n       y = i * cf; n       x -= bd[i].fs; n       rr1 = i; n     } n     l = i; n     r = n; n     while (r - l > 1) { n       md = (l + r) / 2; n       if (bd[md].sm > x) n         l = md; n       else n         r = md; n     } n     if (bd[l].sm <= x && i != 0) r = l; n     x -= bd[r].sm; n     if (r == i && flg) { n       if (i == n) { n         y = a * cm + n * cf; n         if (ans <= y) { n           ans = y; n           r1 = n; n           r2 = n + 1; n         } n       } else { n         y += cm * (min(a, in[bd[r].id] + x / (n - r))); n         if (ans <= y) { n           ans = y; n           r1 = i; n           xxx = min(a, in[bd[r].id] + x / (n - r)); n           r2 = i + 1; n         } n       } n       continue; n     } n     if (r - 1 > 0) n       y += cm * (min(a, min(in[bd[r - 1].id], in[bd[r].id] + x / (n - r + 1)))); n     else n       y += cm * (min(a, in[bd[r].id] + x / (n - r + 1))); n     if (y >= ans) { n       ans = y; n       r1 = rr1; n       r2 = r; n       if (r - 1 > 0) n         xxx = min(a, min(in[bd[r - 1].id], in[bd[r].id] + x / (n - r + 1))); n       else n         xxx = min(a, in[bd[r].id] + x / (n - r + 1)); n     } n   } n   for (i = 1; i <= r1; i++) { n     in[bd[i].id] = a; n   } n   for (i = r2; i <= n; i++) { n     in[bd[i].id] = xxx; n   } n   cout << ans << endl; n   for (i = 1; i <= n; i++) cout << in[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<long long int> a(n); n     vector<long long int> b(n); n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     for (int i = 0; i < n; i++) { n       cin >> b[i]; n     } n     long long int cnt = 0; n     long long int min_a = *min_element(a.begin(), a.end()); n     long long int min_b = *min_element(b.begin(), b.end()); n     for (int i = 0; i < n; i++) { n       if (a[i] > min_a && b[i] > min_b) { n         long long int num; n         num = min(a[i] - min_a, b[i] - min_b); n         cnt += num; n         a[i] -= num; n         b[i] -= num; n       } n     } n     for (int i = 0; i < n; i++) { n       cnt += (a[i] - min_a); n       cnt += (b[i] - min_b); n     } n     cout << cnt << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto& x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n class dsu { n  public: n   vector<int> p; n   int n; n   dsu(int _n) : n(_n) { n     p.resize(n); n     iota(p.begin(), p.end(), 0); n   } n   inline int get(int x) { return (x == p[x] ? x : (p[x] = get(p[x]))); } n   inline bool unite(int x, int y) { n     x = get(x); n     y = get(y); n     if (x != y) { n       p[x] = y; n       return true; n     } n     return false; n   } n }; n const int MAX = 100010; n int bound[MAX]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tt; n   cin >> tt; n   assert(1 <= tt && tt <= 1000); n   int x = 0; n   while (tt--) { n     int n; n     cin >> n; n     if (n == 0) { n       continue; n     } n     assert(n >= 1); n     vector<int> v(n); n     for (int i = 0; i < n; i++) { n       cin >> v[i]; n     } n     sort(v.begin(), v.end()); n     v.resize(unique(v.begin(), v.end()) - v.begin()); n     n = (int)v.size(); n     for (int i = 0; i < n - 1; i++) { n       assert(v[i] < v[i + 1]); n     } n     assert(0 <= v[0]); n     assert(v[n - 1] <= 100000); n     { n       int ptr = -1; n       for (int val = 0; val < MAX; val++) { n         if (ptr + 1 < n && v[ptr + 1] == val) { n           ++ptr; n         } n         bound[val] = ptr; n       } n     } n     vector<vector<int>> who(1, vector<int>(n, -1)); n     vector<dsu> d(1, dsu(n + 1)); n     vector<bool> seen; n     for (int i = n - 1; i >= 0; i--) { n       int start = 0; n       int j = 0; n       while (start < i) { n         if (j == (int)who.size()) { n           who.emplace_back(n, -1); n           d.emplace_back(n + 1); n         } n         int finish = i - 1; n         int tmp = who[j][i]; n         if (tmp != -1) { n           int val = v[i] - (v[tmp] - v[i]); n           finish = (val < 0 ? -1 : bound[val]); n         } n         if (start <= finish) { n           int pos = d[j].get(start); n           while (pos <= finish) { n             who[j][pos] = i; n             d[j].unite(pos, pos + 1); n             pos = d[j].get(pos); n           } n           start = finish + 1; n         } n         ++j; n       } n       int g = 0; n       while (g < (int)who.size() && who[g][i] != -1) { n         ++g; n       } n       if (g >= (int)seen.size()) { n         seen.resize(g + 1); n       } n       seen[g] = true; n     } n     int g = 0; n     while (g < (int)seen.size() && seen[g]) { n       ++g; n     } n     x ^= g; n     42; n   } n   cout << (x > 0 ?  YES  :  NO ) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n long long n, num; n long long a[maxn]; n long long vis[maxn]; n priority_queue<long long> pq; n void fnd(long long x) { n   if (vis[a[x]] == 0) { n     vis[a[x]] = 1; n     num++; n     fnd(a[x]); n   } else n     return; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   memset(vis, 0, sizeof(vis)); n   for (int i = 1; i <= n; i++) { n     num = 1; n     if (vis[i] == 0) { n       vis[i] = 1; n       fnd(i); n       pq.push(num); n     } n   } n   long long t = pq.top(); n   pq.pop(); n   if (!pq.size()) { n     cout << t * t << endl; n   } else { n     t += pq.top(); n     pq.pop(); n     long long ans = t * t; n     while (pq.size()) { n       t = pq.top(); n       pq.pop(); n       ans += t * t; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1e5 + 5; n vector<int> adj[maxN]; n int s[maxN], par[maxN]; n void dfs(int n, int p) { n   par[n] = p; n   for (int i = int(0); i < int((int)adj[n].size()); i++) { n     if (adj[n][i] != p) dfs(adj[n][i], n); n   } n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cin.tie(0), cout.tie(0), n       cout.precision(15); n   int n, tmp; n   cin >> n; n   for (int i = int(0); i < int(n - 1); i++) { n     cin >> tmp; n     tmp -= 1; n     adj[tmp].push_back(i + 1); n   } n   dfs(0, -1); n   for (int i = int(0); i < int(n); i++) { n     cin >> s[i]; n   } n   long long ans = s[0]; n   for (int i = int(0); i < int(n); i++) { n     if (s[i] == -1) { n       int n = i, tt = s[par[i]]; n       int mini = 2000000000; n       for (int j = int(0); j < int((int)adj[n].size()); j++) { n         if (s[adj[n][j]] < tt) { n           cout << -1; n           return 0; n         } n         mini = min(mini, s[adj[n][j]] - tt); n       } n       if (mini != 2000000000) ans += mini; n       for (int j = int(0); j < int((int)adj[n].size()); j++) { n         ans += s[adj[n][j]] - tt - mini; n       } n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1000000001; n const long double EPS = 1e-9; n const int MOD = 1000000007; n const long long LLINF = 1000000000000000001; n const int maxn = 1042; n int n; n int a[maxn]; n char ans[maxn][maxn]; n bool good[maxn]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     for (int j = 1; j <= n; j++) ans[i][j] =  . ; n   } n   bool none = true; n   for (int i = 1; i <= n; i++) n     if (a[i] != i) none = false; n   if (none) n     cout << n <<   n ; n   else { n     cout << n - 1 <<   n ; n     int mostleft = 1; n     while (a[mostleft] == mostleft) { n       good[mostleft] = true; n       mostleft++; n     } n     int row = 1; n     int z = mostleft; n     good[z] = true; n     z = a[z]; n     while (!good[z]) { n       good[z] = true; n       char mirror = (z < a[z]) ?  /  :     ; n       ans[row][z] = mirror; n       ans[row][a[z]] = mirror; n       row++; n       z = a[z]; n     } n     good[z] = true; n     for (int i = mostleft + 1; i <= n; i++) { n       if (a[i] != i && !good[i]) { n         int firstrow = row; n         z = i; n         while (a[z] > z) z = a[z]; n         z = a[z]; n         good[z] = true; n         row++; n         z = a[z]; n         while (!good[z]) { n           good[z] = true; n           char mirror = (z < a[z]) ?  /  :     ; n           ans[row][z] = mirror; n           ans[row][a[z]] = mirror; n           row++; n           z = a[z]; n         } n         ans[row - 1][mostleft] =     ; n         ans[firstrow][mostleft] =  / ; n         ans[firstrow][a[z]] =  / ; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) cout << ans[i][j]; n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int h[n]; n   for (int i = 0; i < n; i++) cin >> h[i]; n   int max; n   int a[n]; n   a[n - 1] = 0; n   max = h[n - 1]; n   for (int i = n - 2; i >= 0; i--) { n     if (h[i] > max) { n       a[i] = 0; n       max = h[i]; n     } else { n       a[i] = max - h[i] + 1; n     } n   } n   for (int i = 0; i < n; i++) { n     printf( %d  , a[i]); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int x = 0, f = 1; n   char c; n   do c = getchar(), f = c ==  -  ? -1 : f; n   while (!isdigit(c)); n   do x = (x << 3) + (x << 1) + c -  0 , c = getchar(); n   while (isdigit(c)); n   return x * f; n } n struct Point { n   int x, y, px, py, id; n   Point() {} n   Point(int x, int y, int px, int py) : x(x), y(y), px(px), py(py) {} n } a[55], ans[100020]; n bool tab[55]; n int b[55][55], d[55][55]; n int n, m, cnt, tmp = 1, num; n inline void Move(Point &a, int x, int y) { n   assert(!b[a.x + x][a.y + y]); n   b[a.x + x][a.y + y] = b[a.x][a.y]; n   b[a.x][a.y] = 0; n   ans[++cnt] = Point(a.x, a.y, a.x + x, a.y + y); n   a.x += x; n   a.y += y; n } n inline void Move_to(Point &a, int pos) { n   Move(a, 1, 0); n   while (a.y < pos) Move(a, 0, 1); n   while (a.y > pos) Move(a, 0, -1); n   Move(a, -1, 0); n } n inline void Swap(Point &a, Point &b) { n   if (a.id == b.id) return; n   if (n == 2) { n     if (a.y < b.y) n       Move(a, 1, 0), Move(b, 0, -1), Move(a, 0, 1), Move(a, -1, 0); n     else n       Move(a, 1, 0), Move(b, 0, 1), Move(a, 0, -1), Move(a, -1, 0); n     return; n   } n   Move(a, 1, 0); n   Move(b, 1, 0); n   Move(b, 1, 0); n   int t = a.y; n   while (a.y > b.y) Move(a, 0, -1); n   while (a.y < b.y) Move(a, 0, 1); n   while (b.y > t) Move(b, 0, -1); n   while (b.y < t) Move(b, 0, 1); n   Move(a, -1, 0); n   Move(b, -1, 0); n   Move(b, -1, 0); n } n inline void Move1(Point &a, int x) { n   while (a.x <= tmp) Move(a, 1, 0); n   while (a.y > x) Move(a, 0, -1); n   while (a.y < x) Move(a, 0, 1); n   while (!b[a.x - 1][a.y]) Move(a, -1, 0); n   tmp = max(tmp, a.x); n } n inline void Move2(Point &a, int y) { n   while (a.x < y) Move(a, 1, 0); n } n int main() { n   n = read(); n   m = read(); n   if (n == 1) return printf( 0 ), 0; n   assert(m <= n); n   for (int i = 1; i <= m; i++) { n     a[i].x = read(); n     a[i].y = read(); n     a[i].id = b[a[i].x][a[i].y] = i; n   } n   for (int i = 1; i <= m; i++) { n     a[i].px = read(); n     a[i].py = read(); n     d[a[i].px][a[i].py] = i; n   } n   for (int i = 0; i <= n + 1; i++) n     b[i][0] = b[0][i] = b[n + 1][i] = b[i][n + 1] = 2147483647; n   for (int i = 1; i <= n; i++) n     for (int j = n; j >= 1; j--) { n       if (!b[i][j]) continue; n       int t = b[i][j]; n       while (!b[i][a[t].y + 1]) Move(a[t], 0, 1); n     } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       if (!b[i][j]) continue; n       num++; n       int t = b[i][j]; n       assert(a[t].y >= num); n       while (a[t].y > num) Move(a[t], 0, -1); n       while (a[t].x > 1) Move(a[t], -1, 0); n     } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       if (d[i][j] && !tab[j]) { n         tab[j] = true; n         if (b[1][j]) n           Swap(a[b[1][j]], a[d[i][j]]); n         else n           Move_to(a[d[i][j]], j); n       } n   for (int j = 1; j <= n; j++) n     for (int i = 1; i <= n; i++) n       if (d[i][j]) Move1(a[d[i][j]], j); n   for (int i = n; i >= 1; i--) n     for (int j = n; j >= 1; j--) n       if (d[i][j]) Move2(a[d[i][j]], i); n   printf( %d n , cnt); n   for (int i = 1; i <= cnt; i++) n     printf( %d %d %d %d n , ans[i].x, ans[i].y, ans[i].px, ans[i].py); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 50 + 5, maxL = (1 << 20) + 10; n int N, L; n char s[maxn][25]; n bool vis[maxL]; n long double F[maxL], R[25]; n long long dask[maxL]; n bool check(int tt, int k) { n   if (dask[tt] & (1LL << k)) n     return false; n   else n     return true; n } n long double dfs(int tt, int g) { n   if (g == 0) return 0.0; n   if (vis[tt]) return F[tt]; n   long double ret = 0.0; n   long double step = L - g + 1; n   for (int i = 0; i < L; i++) { n     if ((1 << i) & tt) continue; n     int nt = (1 << i) | tt; n     int wq = __builtin_popcountll(dask[nt] ^ dask[tt]); n     ret += step * R[g] * wq; n     ret += dfs(nt, g - 1); n   } n   vis[tt] = true; n   F[tt] = ret; n   return ret; n } n void Prepare() { n   memset(dask, 0, sizeof(dask)); n   for (int i = (1), _i = (N); i <= _i; i++) n     for (int j = (i + 1), _j = (N); j <= _j; j++) { n       int same = 0; n       for (int k = 0; k < L; k++) n         if (s[i][k] == s[j][k]) same |= (1 << k); n       dask[same] |= (1LL << j) | (1LL << i); n     } n   for (int i = (1 << L) - 1; i; i--) n     for (int k = 0; k < L; k++) n       if (i & (1 << k)) dask[i ^ (1 << k)] |= dask[i]; n } n int main() { n   for (; scanf( %d , &N) != EOF;) { n     for (int i = (1), _i = (N); i <= _i; i++) scanf( %s , s[i]); n     L = strlen(s[1]); n     int gg = L; n     R[L] = 1.0 / (long double)L; n     for (int i = (L - 1), _i = (1); i >= _i; i--) { n       gg--; n       R[i] = R[i + 1] / (long double)gg; n     } n     memset(vis, 0, sizeof(vis)); n     long double ans = 0.0; n     Prepare(); n     ans = dfs(0, L); n     ans /= (long double)N; n     printf( %.15lf n , (double)ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int tot, n; n int vis[110]; n int p[110]; n int dp[2][262144], cs[110][262144], pre[110][262144]; n int have[100]; n int a[110], b[110]; n string print(int sta) { n   string x =   ; n   for (int i = tot - 1; i >= 0; i--) { n     if (sta & (1 << i)) n       x +=  1 ; n     else n       x +=  0 ; n   } n   return x; n } n int main() { n   for (int i = 2; i <= 59; i++) { n     if (!vis[i]) p[tot++] = i; n     for (int j = i; j <= 59; j += i) vis[j] = 1; n   } n   for (int i = 1; i <= 59; i++) { n     for (int j = 0; j < tot; j++) { n       if (i % p[j] == 0) have[i] |= (1 << j); n     } n   } n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   memset(dp, 0x3f, sizeof(dp)); n   dp[0][0] = 0; n   register int tmp; n   register int nowt = 1, pret = 0; n   for (int i = 1; i <= n; i++) { n     for (int sta = 0; sta < (1 << tot); sta++) { n       dp[nowt][sta] = 0x3f3f3f3f; n       for (int j = 1; j <= 59; j++) { n         if ((have[j] & sta) != have[j]) continue; n         tmp = dp[pret][have[j] ^ sta] + abs(j - a[i]); n         if (tmp < dp[nowt][sta]) { n           dp[nowt][sta] = tmp; n           pre[i][sta] = have[j] ^ sta; n           cs[i][sta] = j; n         } n       } n     } n     nowt ^= 1; n     pret ^= 1; n   } n   int mi = 0x3f3f3f3f; n   for (int i = 0; i < (1 << tot); i++) { n     mi = min(mi, dp[pret][i]); n   } n   int pp; n   for (int i = 0; i < (1 << tot); i++) { n     if (mi == dp[pret][i]) { n       pp = i; n       for (int now = n; now >= 1; now--) { n         b[now] = cs[now][pp]; n         pp = pre[now][pp]; n       } n       for (int now = 1; now <= n; now++) { n         printf( %d  , b[now]); n       } n       puts(  ); n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   int c[100]; n   for (int i = 0; i < n; i++) cin >> c[i]; n   int ans = 0; n   int min = a > b ? b : a; n   for (int i = 0; i < n / 2; i++) { n     if (c[i] == 0 && c[n - 1 - i] == 2 || c[i] == 2 && c[n - 1 - i] == 0) n       ans += a; n     else if (c[i] == 1 && c[n - 1 - i] == 2 || c[i] == 2 && c[n - 1 - i] == 1) n       ans += b; n     else if (c[i] == 2 && c[n - 1 - i] == 2) n       ans += min * 2; n     else if (c[i] == 0 && c[n - 1 - i] == 1 || c[i] == 1 && c[n - 1 - i] == 0) { n       cout << -1 << endl; n       return 0; n     } n   } n   if (n % 2 == 1 && c[n / 2] == 2) ans += min; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void read(long long a = 0) { n   freopen( in ,  r , stdin); n   if (a) freopen( out ,  w , stdout); n } n long long a[100009 * 10], b[100009 * 10]; n long long n, m; n bool bs(long long num) { n   bool vis[100009 * 10] = {}; n   long long space = 0; n   for (int i = num; i >= 0; i--) { n     if (a[i] && !vis[a[i]]) { n       vis[a[i]] = 1; n       space += b[a[i] - 1]; n     } else { n       space--; n       space = max(space, 0ll); n     } n   } n   for (int i = 1; i <= m; i++) n     if (!vis[i]) return 0; n   if (space == 0) n     return 1; n   else n     return 0; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) cin >> b[i]; n   long long lo = 0, hi = n + 1; n   while (lo < hi) { n     long long mid = (lo + hi - 1) >> 1; n     if (bs(mid)) n       hi = mid; n     else n       lo = mid + 1; n   } n   if (hi == n + 1) n     cout <<  -1 ; n   else n     cout << hi + 1; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 9; n const int N = 4040; n int n, w, b; n long long fac[N], f[N][N], inv[N]; n long long qpow(long long x, long long p) { n   long long res = 1ll; n   while (p) { n     if (p & 1) { n       res = (res * x) % mod; n     } n     x = (x * x) % mod; n     p >>= 1; n   } n   return res; n } n long long INV(long long k) { return qpow(k, mod - 2); } n long long calc(int x, int y) { n   return fac[x] * inv[y] % mod * inv[x - y] % mod; n } n void init() { n   fac[0] = 1ll; n   inv[0] = 1ll; n   for (int i = 1; i < N; i++) { n     fac[i] = (fac[i - 1] * 1ll * i) % mod; n     inv[i] = INV(fac[i]); n   } n } n int main() { n   scanf( %d%d%d , &n, &w, &b); n   init(); n   for (int i = 1; i <= n; i++) { n     for (int j = i; j < N; j++) { n       f[i][j] = (fac[j] * calc(j - 1, i - 1)) % mod; n     } n   } n   long long ans = 0; n   for (int i = 1; i < n - 1; i++) { n     ans = n         (ans + f[n - i][w] * f[i][b] % mod * (1ll * n - 1ll * i - 1ll) % mod) % n         mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long f(string s, long long m) { n   map<char, long long> a; n   map<char, long long>::iterator ii; n   long long i; n   for (i = 0; i < s.size(); i++) { n     a[s[i]]++; n   } n   long long best = 0; n   for (ii = a.begin(); ii != a.end(); ii++) best = max(best, ii->second); n   long long p = s.size(); n   if (best == p && m == 1) return p - 1; n   return min(p, best + m); n } n int main() { n   string s1, s2, s3; n   long long n; n   cin >> n; n   cin >> s1 >> s2 >> s3; n   if (f(s1, n) > f(s2, n) && f(s1, n) > f(s3, n)) { n     cout <<  Kuro ; n     return 0; n   } n   if (f(s2, n) > f(s1, n) && f(s2, n) > f(s3, n)) { n     cout <<  Shiro ; n     return 0; n   } n   if (f(s3, n) > f(s1, n) && f(s3, n) > f(s2, n)) { n     cout <<  Katie ; n     return 0; n   } n   cout <<  Draw ; n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n void rd(T &x) { n   x = 0; n   int f = 1; n   char c; n   while (!isdigit(c = getchar())) n     if (c ==  - ) f = -1; n   do { n     x = (x << 3) + (x << 1) + (c ^ 48); n   } while (isdigit(c = getchar())); n   x *= f; n } n using namespace std; n const int N = 1e5 + 5; n int n, dp[N], s[N], p[N], cnt, tp; n map<int, int> Map[N]; n int main() { n   rd(n); n   for (int i = 1; i <= n; i++) { n     int v, c, l, r; n     rd(v), rd(c), rd(l), rd(r); n     if (r + c <= 1e5 && (!l || Map[l].find(r + c) != Map[l].end())) { n       int t = Map[l][r + c]; n       dp[i] = dp[t] + v; n       p[i] = t; n       if (!r && dp[i] > dp[tp]) tp = i; n       if (dp[i] > dp[Map[l + c][r]]) Map[l + c][r] = i; n     } n   } n   while (tp) s[++cnt] = tp, tp = p[tp]; n   printf( %d n , cnt); n   for (; cnt > 0; cnt--) printf( %d  , s[cnt]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, v1, v2, L; n bool can(double m) { n   double bus = 0, nbus = 0; n   for (int i = 0; i < (n + k - 1) / k; i++) { n     if (i == 0) n       bus += m * v2; n     else n       bus += m * v1; n     nbus += m * v1; n     if (bus > L) return false; n     double sb = (bus - nbus) / (v1 + v2); n     bus += sb * v1; n     nbus += sb * v1; n   } n   return true; n } n int main() { n   scanf( %d%d%d%d%d , &n, &L, &v1, &v2, &k); n   double l = 0, r = (double)L / v2, m; n   for (int i = 0; i < 100; i++) { n     m = (l + r) / 2.0; n     if (can(m)) n       l = m; n     else n       r = m; n   } n   printf( %.12f , m + (L - m * v2) / v1); n } n


#include <bits/stdc++.h> n using namespace std; n inline long long Calc(const int &x) { n   long long res = 0; n   for (long long i = 1; i <= x; i *= 10) res += x - i + 1; n   return res; n } n inline long long CalcSum(const int &x) { n   long long res = 0; n   for (long long i = 1; i <= x; i *= 10) n     res += 1ll * (x - i + 1) * (x - i + 2) / 2; n   return res; n } n inline int Pos(const int &x, const int &k) { n   stringstream ss; n   ss << x; n   string s; n   ss >> s; n   return s[k - 1] -  0 ; n } n inline int Solve(long long k) { n   int l = 1, r = 1000000000; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (CalcSum(mid) >= k) n       r = mid - 1; n     else n       l = mid + 1; n   } n   k = k - CalcSum(r); n   int block = l; n   l = 1; n   r = block; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (Calc(mid) >= k) n       r = mid - 1; n     else n       l = mid + 1; n   } n   return Pos(l, k - Calc(r)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int q; n   cin >> q; n   while (q--) { n     long long k; n     cin >> k; n     cout << Solve(k) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 1e9; n using namespace std; n void dfs(int v, int p, vector<vector<int> > &graph, vector<int> &sz) { n   sz[v] = 1; n   for (auto u : graph[v]) n     if (u != p) { n       dfs(u, v, graph, sz); n       sz[v] += sz[u]; n     } n } n void dfs(int v, int p, vector<vector<int> > &graph, vector<int> &c, int C) { n   c[v] = C; n   for (auto u : graph[v]) n     if (u != p) { n       dfs(u, v, graph, c, C); n     } n } n signed main() { n   int n; n   cin >> n; n   vector<vector<int> > graph(n); n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--, b--; n     graph[a].push_back(b); n     graph[b].push_back(a); n   } n   vector<int> sz(n); n   dfs(0, 0, graph, sz); n   vector<int> cen; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if ((n - sz[i]) * 2 <= n) { n       int t = 1; n       for (auto u : graph[i]) { n         if (sz[u] < sz[i]) { n           if (2 * sz[u] <= n) continue; n           t = 0; n         } n       } n       if (t) cen.push_back(i); n     } n   } n   for (int i = 0; i < n; i++) { n     ans += 2 * min(sz[i], n - sz[i]); n   } n   cout << ans <<   n ; n   if (cen.size() == 2) { n     int v = cen[0], u = cen[1]; n     vector<int> c(n); n     dfs(v, u, graph, c, 0); n     dfs(u, v, graph, c, 1); n     vector<int> p0, p1; n     for (int i = 0; i < n; i++) { n       if (c[i] == 0) p0.push_back(i); n       if (c[i] == 1) p1.push_back(i); n     } n     vector<int> ans(n); n     for (int i = 0; i < n / 2; i++) { n       ans[p0[i]] = p1[i]; n       ans[p1[i]] = p0[i]; n     } n     for (int i = 0; i < n; i++) { n       cout << ans[i] + 1 <<    ; n     } n     return 0; n   } else { n     dfs(cen[0], cen[0], graph, sz); n     int C = 0, idC = 0; n     vector<int> c(n); n     c[cen[0]] = -1; n     int maxsz = 0; n     for (auto u : graph[cen[0]]) { n       dfs(u, cen[0], graph, c, C); n       maxsz = max(maxsz, sz[u]); n       if (maxsz == sz[u]) idC = C; n       C++; n     } n     vector<pair<int, int> > q; n     for (int i = 0; i < n; i++) { n       if (i != cen[0] && c[i] != idC) q.push_back({c[i], i}); n     } n     for (int i = 0; i < n; i++) { n       if (i != cen[0] && c[i] == idC) q.push_back({INF, i}); n     } n     q.push_back({INF + 1, cen[0]}); n     sort((q).begin(), (q).end()); n     vector<int> ans(n); n     for (int i = 0; i < n; i++) { n       ans[q[i].second] = q[(i + maxsz) % q.size()].second; n     } n     for (int i = 0; i < n; i++) { n       assert(c[ans[i]] != c[i]); n       cout << ans[i] + 1 <<    ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5; n int n; n double a[MAXN], b[4], t; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n; n   b[3] = 1; n   for (int i = 1; i <= n; i++) { n     cin >> t; n     b[0] += t; n     b[1]++; n     if (b[1] * b[2] <= b[0] * b[3]) b[2] = b[0], b[3] = b[1]; n   } n   cout << fixed << setprecision(10) << b[2] / b[3] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x0f0f0f0f; n template <class S, class T> n inline pair<S, T> MP(S a, T b) { n   return make_pair(a, b); n } n template <class T> n inline auto LX(T &X) -> decltype((X.first)) { n   return X.first; n } n template <class T> n inline auto LX(const T &X) -> const decltype((X.first)) { n   return X.first; n } n template <class T> n inline auto RX(T &X) -> decltype((X.second)) { n   return X.second; n } n template <class T> n inline auto RX(const T &X) -> const decltype((X.second)) { n   return X.second; n } n template <class T> n inline void RST(T &a) { n   memset(a, 0, sizeof(a)); n } n template <class T> n inline void RST1(T &a) { n   memset(a, -1, sizeof(a)); n } n template <class T> n inline void SINF(T &a) { n   memset(a, 0x0f, sizeof(a)); n } n template <class S, class T> n inline void RST(S &a, T b) { n   fill((T *)a, (T *)a + (sizeof(a) / sizeof(b)), b); n } n template <class T> n inline void CLR(T &a) { n   a.clear(); n } n template <class S, class T> n inline void CPY(S &a, T &b) { n   memcpy(a, b, sizeof(a)); n } n template <class S, class T> n inline bool chkmin(S &a, T b) { n   return b < a ? a = b, 1 : 0; n } n template <class S, class T> n inline bool chkmax(S &a, T b) { n   return a < b ? a = b, 1 : 0; n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <class T> n inline int SZ(T &a) { n   return (int)(a.size()); n } n template <class T> n inline bool EPT(T &a) { n   return a.empty(); n } n template <class T> n inline auto FRT(T &a) -> decltype((a.front())) { n   return a.front(); n } n template <class T> n inline auto FRT(const T &a) -> const decltype((a.front())) { n   return a.front(); n } n template <class T> n inline auto BK(T &a) -> decltype(a.back()) & { n   return a.back(); n } n template <class T> n inline auto BK(const T &a) -> const decltype((a.back())) { n   return a.back(); n } n template <class T> n inline auto TOP(T &a) -> decltype((a.top())) { n   return a.top(); n } n template <class T> n inline auto TOP(const T &a) -> const decltype((a.top())) { n   return a.top(); n } n int n, m, f[1010], g[1010], cnt; n char buf[1010]; n double ans; n void solve(int *c, int n, int m) { n   int s = 0; n   for (int i = 1; i <= n; i++) n     if (c[i]) { n       s += c[i] - 1, ans += (double)s * (m - c[i]); n       if (!c[i + 1] || c[i + 1] < c[i]) s = 0; n     } n   s = 0; n   for (int i = 1; i <= n; i++) n     if (c[i]) { n       ans += (double)s * (c[i] - 1); n       s += m - c[i]; n       if (!c[i + 1] || c[i + 1] > c[i]) s = 0; n     } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %s , buf + 1); n     for (int j = 1; j <= m; j++) n       if (buf[j] ==  X ) f[i] = j, g[j] = i, cnt++; n   } n   solve(f, n, m); n   solve(g, m, n); n   ans *= 2; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= i; j++) n       ans += 1. * (m - (f[i] ? 1 : 0)) * (m - (f[j] ? 1 : 0)) * (i - j); n   for (int i = 1; i <= m; i++) n     for (int j = 1; j <= i; j++) n       ans += 1. * (n - (g[i] ? 1 : 0)) * (n - (g[j] ? 1 : 0)) * (i - j); n   ans *= 2; n   ans /= sqr((double)n * m - cnt); n   printf( %.10lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[101][1001]; n int n, m; n int g[101][101]; n vector<int> col[1001]; n set<int> row[101]; n bool ans[101][101]; n int main() { n   scanf( %d , &n), scanf( %d , &m); n   for (int i = 0; i < m; ++i) { n     int x, y; n     scanf( %d , &x), scanf( %d , &y); n     g[x][y] = g[y][x] = 1; n   } n   for (int i = 1; i <= n; ++i) g[i][i] = true; n   for (int i = 1; i <= n; ++i) { n     for (int j = i + 1; j <= n; ++j) { n       if (g[i][j] && !ans[i][j]) { n         for (int k = 0; k < 1001; ++k) n           if (col[k].size() == 0) { n             arr[i][k] = i; n             arr[j][k] = j; n             col[k].push_back(i), col[k].push_back(j); n             break; n           } else { n             bool ok = true; n             for (int l = 0; l < col[k].size(); ++l) { n               int cur = col[k][l]; n               if (!g[cur][i] || !g[cur][j]) { n                 ok = false; n                 break; n               } n             } n             if (ok) { n               arr[i][k] = i; n               arr[j][k] = j; n               for (int l = 0; l < col[k].size(); ++l) { n                 int cur = col[k][l]; n                 ans[cur][i] = ans[i][cur] = ans[j][cur] = ans[cur][j] = true; n               } n               col[k].push_back(i), col[k].push_back(j); n               break; n             } n           } n         ans[i][j] = true; n         ans[j][i] = true; n       } n     } n   } n   for (int i = 0; i < 1001; ++i) n     for (int j = 0; j < col[i].size(); ++j) row[col[i][j]].insert(i); n   for (int i = 1; i < 101; ++i) n     if (row[i].size() == 0) { n       for (int j = 0; j < 1001; ++j) n         if (col[j].size() == 0) { n           col[j].push_back(i); n           row[i].insert(j); n           break; n         } n     } n   for (int i = 1; i <= n; ++i) { n     printf( %d n , row[i].size()); n     for (set<int>::iterator it = row[i].begin(); it != row[i].end(); ++it) n       printf( %d %d n , i, (*it) + 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, p1, p2; n   cin >> n; n   cin >> p1; n   int a1[p1]; n   for (int i = 0; i < p1; i++) cin >> a1[i]; n   cin >> p2; n   int a2[p2]; n   for (int i = 0; i < p2; i++) cin >> a2[i]; n   sort(a1, a1 + p1); n   sort(a2, a2 + p2); n   vector<int>::iterator it; n   vector<int> res(100); n   it = set_union(a1, a1 + p1, a2, a2 + p2, res.begin()); n   res.resize(it - res.begin()); n   if (res.size() == n) n     cout <<  I become the guy. ; n   else n     cout <<  Oh, my keyboard! ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int v[5], p[5]; n int cnt[5], add[5]; n int getMaxScr(int acc, int tot) { n   int MaxScr; n   for (int i = 0; i < 6; i++) { n     int t = 1 << i; n     if (tot >= t * acc) MaxScr = (i + 1) * 500; n   } n   return MaxScr; n } n bool ok(int m) { n   int vScr = 0, pScr = 0; n   for (int i = 0; i < 5; i++) { n     int MaxScr = getMaxScr(cnt[i] + m * add[i], n + m); n     if (v[i] != -1) vScr += MaxScr / 250 * (250 - v[i]); n     if (p[i] != -1) pScr += MaxScr / 250 * (250 - p[i]); n   } n   return vScr > pScr; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 5; j++) { n       int t; n       cin >> t; n       if (i == 0) v[j] = t; n       if (i == 1) p[j] = t; n       if (t != -1) cnt[j]++; n     } n   } n   for (int i = 0; i < 5; i++) n     if (v[i] != -1 && p[i] != -1 && p[i] < v[i]) add[i] = 1; n   int ans = 0; n   while (!ok(ans) && ans < 100000) ans++; n   cout << (ans == 100000 ? -1 : ans) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long big = 1000000007; n unsigned long long gcd(unsigned long long a, unsigned long long b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n string getbin(unsigned long long x) { n   if (x == 0) return  0 ; n   string ret =   ; n   while (x != 0) { n     ret = (char)( 0  + x % 2) + ret; n     x /= 2; n   } n   return ret; n } n unsigned long long getdec(string b) { n   unsigned long long ret = 0; n   for (int i = 0; i < b.size(); i += 1) { n     ret *= 2; n     ret += b[i] -  0 ; n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long s = 0; n     vector<int> a(n); n     for (int i = 0; i < n; i += 1) cin >> a[i], s += a[i]; n     bool b = false; n     for (int i = 0; !b && i < n; i += 1) { n       if (a[i] > s / 2) b = true; n     } n     if (b) n       cout <<  T ; n     else if (s % 2 == 0) n       cout <<  HL ; n     else n       cout <<  T ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 0, f = 0; n   char ch = getchar(); n   for (; ch <  0  || ch >  9 ; ch = getchar()) f ^= (ch ==  - ); n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ; n   return f ? -x : x; n } n const long long maxn = 200050; n long long n, head[maxn], cnt = 0, siz[maxn], dep[maxn]; n long long ans = 0, qwq = 0; n struct node { n   long long to, nxt; n } e[maxn * 2]; n inline void add(long long u, long long v) { n   e[++cnt].to = v; n   e[cnt].nxt = head[u]; n   head[u] = cnt; n } n void dfs(long long u, long long f, long long d) { n   siz[u] = 1; n   dep[u] = d; n   for (long long i = head[u]; i; i = e[i].nxt) { n     long long v = e[i].to; n     if (v == f) continue; n     dfs(v, u, d + 1); n     siz[u] += siz[v]; n   } n } n signed main() { n   n = read(); n   for (long long i = 1, a, b; i < n; ++i) { n     a = read(); n     b = read(); n     add(a, b); n     add(b, a); n   } n   dfs(1, 0, 0); n   for (long long i = 1; i <= n; ++i) { n     ans += siz[i] * (n - siz[i]); n     if (dep[i] & 1) ++qwq; n   } n   ans += qwq * (n - qwq); n   printf( %lld n , ans / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n long double p[N], k[N]; n int par[N]; n vector<int> v[N]; n bool vstd[N]; n void dfs(int node) { n   if (vstd[node]) return; n   vstd[node] = true; n   for (auto u : v[node]) { n     if (u != par[node]) { n       k[node] += p[u]; n       par[u] = node; n       dfs(u); n     } n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(6); n   int n; n   cin >> n; n   long double tmp = 0.0; n   for (int i = 0; i < n; i++) { n     cin >> p[i]; n     p[i] = 1.0 - p[i]; n     tmp += p[i]; n   } n   for (int i = 1; i < n; i++) { n     int a, b; n     cin >> a >> b; n     v[a].push_back(b); n     v[b].push_back(a); n   } n   long double pmt = 0.0; n   dfs(0); n   for (int i = 0; i < n; i++) pmt += k[i] * p[i]; n   int q; n   cin >> q; n   while (q--) { n     int node; n     cin >> node; n     tmp -= p[node]; n     pmt -= k[node] * p[node]; n     if (node) { n       pmt -= k[par[node]] * p[par[node]]; n       k[par[node]] -= p[node]; n     } n     cin >> p[node]; n     p[node] = 1.0 - p[node]; n     tmp += p[node]; n     if (node) k[par[node]] += p[node]; n     pmt += k[node] * p[node]; n     if (node) pmt += k[par[node]] * p[par[node]]; n     cout << tmp - pmt <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char next; n   long long int neg = 0, pos = 0; n   cin >> next; n   bool estado = false; n   string orden =   ; n   while (next !=  = ) { n     if (next ==    ) { n       cin >> next; n     } n     if (next ==  - ) { n       estado = true; n       cin >> next; n     } n     if (next ==  + ) { n       estado = false; n       cin >> next; n     } n     if (next ==  ? ) { n       if (estado) { n         orden +=  - ; n         neg++; n       } else { n         orden +=  + ; n         pos++; n       } n     } n     cin >> next; n   } n   long long int num; n   cin >> num; n   long long int coefs[neg + pos]; n   if ((neg > ((pos - 1) * num)) || ((pos - neg * num) > num)) { n     cout <<  Impossible ; n     return 0; n   } n   long long int valpos = 0, valneg = 0; n   cout <<  Possible  << endl; n   if (neg == 0) { n     long long int cv = trunc(num / pos); n     long long int sum = 0; n     for (int i = 0; i < pos; i++) { n       coefs[i] = cv; n     } n     sum = cv * pos - num; n     if (sum != 0) { n       for (int i = 0; i < pos; i++) { n         coefs[i]++; n         sum++; n         if (sum == 0) break; n       } n     } n     cout << coefs[0]; n     for (int i = 1; i < pos; i++) { n       cout <<   +   << coefs[i]; n     } n     cout <<   =   << num; n     return 0; n   } n   if (pos - 1 > neg) { n     valpos = 1; n     valneg = trunc((pos - 1) / neg); n   } else { n     valneg = 1; n     valpos = trunc(neg / (pos - 1)); n   } n   int ref; n   for (int i = 0; i < orden.size(); i++) { n     if (orden[i] ==  - ) { n       coefs[i] = -valneg; n     } else n       coefs[i] = valpos; n   } n   coefs[0] = num; n   long long int suma = 0; n   for (int i = 0; i < neg + pos; i++) { n     suma += coefs[i]; n   } n   suma -= num; n   if (suma < 0) { n     for (int i = 1; i < neg + pos; i++) { n       if (coefs[i] > 0 && (abs(coefs[i]) < num)) { n         coefs[i]++; n         suma++; n       } n       if (suma == 0) break; n     } n   } n   if (suma > 0) { n     for (int i = 1; i < neg + pos; i++) { n       if (coefs[i] < 0 && (abs(coefs[i]) < num)) { n         coefs[i]--; n         suma--; n       } n       if (suma == 0) break; n     } n   } n   for (int i = 1; i < neg + pos; i++) { n     if (abs(coefs[i]) > num) { n       if (coefs[i] < 0) { n         suma += -num - coefs[i]; n         coefs[i] = -num; n       } else { n         suma += num - coefs[i]; n         coefs[i] = num; n       } n     } n   } n   if (suma > 0) { n     coefs[0] -= suma; n     suma -= suma; n   } n   if (suma < 0) { n     coefs[0] += suma; n     suma -= suma; n   } n   cout << coefs[0]; n   for (int i = 1; i < pos + neg; i++) { n     if (coefs[i] < 0) n       cout <<   -   << abs(coefs[i]); n     else n       cout <<   +   << coefs[i]; n   } n   cout <<   =   << suma + num; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   if (n == 1) { n     cout << -1; n   } else n     cout << n <<     << n + 1 <<     << n * (n + 1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y, large = 2000000000, small = -2000000000; n   string s1, s2; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> s1 >> x >> s2; n     if ((s1 ==  >  && s2 ==  Y ) || (s1 ==  <=  && s2 ==  N )) { n       if (x >= small) { n         small = x + 1; n       } n     } else if ((s1 ==  <  && s2 ==  Y ) || (s1 ==  >=  && s2 ==  N )) { n       if (x <= large) { n         large = x - 1; n       } n     } else if ((s1 ==  >=  && s2 ==  Y ) || (s1 ==  <  && s2 ==  N )) { n       if (x >= small) { n         small = x; n       } n     } else { n       if (x <= large) { n         large = x; n       } n     } n   } n   if (small > large) { n     cout <<  Impossible ; n   } else { n     cout << small; n   } n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 405; n char mp[30], s[N], a[5], b[5]; n int L, n, m, e[N][N], nxd[30], nxs[30], flg[30], lim[N], tmp[N], mx[2]; n vector<int> ans; n bool chk(int pos, int v, int x, bool ff) { n   if (lim[pos] != -1 && lim[pos] != flg[v]) return 0; n   for (int i = 1; i <= n; i++) n     if (e[x][i] && e[x][i + n]) return 0; n   for (int i = 1; i <= n; i++) tmp[i] = (e[x][i] ? 0 : (e[x][i + n] ? 1 : -1)); n   if (ff) { n     for (int i = 1; i <= n; i++) { n       if (pos + i > n) break; n       int t = max(lim[pos + i], tmp[pos + i]); n       if (t == -1) break; n       if (mx[t] < s[pos + i] -  a  + 1) return 0; n       if (mx[t] > s[pos + i] -  a  + 1) break; n     } n   } n   for (int i = 1; i <= n; i++) { n     lim[i] = max(lim[i], tmp[i]); n   } n   return 1; n } n int main() { n   scanf( %s , mp + 1); n   L = strlen(mp + 1); n   memset(lim, -1, sizeof(lim)); n   memset(mx, -1, sizeof(mx)); n   for (int i = 1; i <= L; i++) { n     flg[i] = mp[i] ==  V ; n     mx[flg[i]] = i; n   } n   for (int i = 1; i <= L; i++) n     for (int j = i + 1; j <= L; j++) n       if (flg[i] != flg[j]) { n         nxd[i] = j; n         break; n       } n   for (int i = 1; i <= L; i++) n     for (int j = i + 1; j <= L; j++) n       if (flg[i] == flg[j]) { n         nxs[i] = j; n         break; n       } n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%s%d%s , &x, a, &y, b); n     int a1 = a[0] ==  V , b1 = b[0] ==  V ; n     e[x + a1 * n][y + b1 * n] = 1; n     e[y + !b1 * n][x + !a1 * n] = 1; n   } n   scanf( %s , s + 1); n   for (int k = 1; k <= n * 2; k++) n     for (int i = 1; i <= n * 2; i++) n       for (int j = 1; j <= n * 2; j++) e[i][j] |= (e[i][k] & e[k][j]); n   bool ff = 1; n   for (int i = 1; i <= n; i++) { n     int mn = ff ? s[i] -  a  + 1 : 1; n     if (chk(i, mn, i + flg[mn] * n, ff)) { n       ans.push_back(mn); n     } else if (mn < L && flg[mn] == flg[mn + 1] && n                chk(i, mn + 1, i + flg[mn + 1] * n, 0)) { n       ans.push_back(mn + 1), ff = 0; n     } else if (nxd[mn] && chk(i, nxd[mn], i + flg[nxd[mn]] * n, 0)) { n       ans.push_back(nxd[mn]), ff = 0; n     } else if (nxs[mn] && nxs[mn] > nxd[mn] && n                chk(i, nxs[mn], i + flg[nxs[mn]] * n, 0)) { n       ans.push_back(nxs[mn]), ff = 0; n     } else { n       return puts( -1 ), 0; n     } n   } n   for (int i = 0; i <= n - 1; i++) printf( %c , ans[i] +  a  - 1); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps(1e-8); n char s[1000010]; n int n, m, a, b; n vector<int> vec[1000010]; n vector<int> p[1000010]; n int x[1000010], y[1000010], sum[1000010], ans[1000010]; n int Lowbit(int x) { return x & (-x); } n void Update(int pos, int val) { n   while (pos > 0) { n     sum[pos] += val; n     pos -= Lowbit(pos); n   } n } n int Query(int pos) { n   int ret = 0; n   while (pos <= n) { n     ret += sum[pos]; n     pos += Lowbit(pos); n   } n   return ret; n } n void Add(int l, int r, int val) { n   Update(r, val); n   Update(l - 1, -val); n } n int main() { n   scanf( %s , s + 1); n   for (n = 1; s[n]; n++) n     ; n   n--; n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &a, &b); n     vec[b].push_back(a); n     p[b].push_back(i); n   } n   int len = 0; n   for (int i = 1; i <= n; i++) { n     len++; n     if (s[i] ==  ( ) n       x[len] = 0; n     else n       x[len] = 1; n     y[len] = i; n     if (len >= 2 && x[len] == 1 && x[len - 1] == 0) { n       Add(1, y[len - 1], 2); n       len -= 2; n     } n     for (int j = 0; j < (int)vec[i].size(); j++) { n       int tmp = vec[i][j]; n       ans[p[i][j]] = Query(tmp); n     } n   } n   for (int i = 1; i <= m; i++) printf( %d n , ans[i]); 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, c, min_cost, min_a, min_b, min_c; n     cin >> a >> b >> c; n     if (b % a == 0 && c % b == 0) { n       cout << 0 << endl; n       cout << a <<     << b <<     << c << endl; n     } else { n       min_cost = 10000000; n       int max = 2 * c; n       for (int i = 1; i < max; i++) n         for (int j = i; j < max; j += i) n           for (int k = j; k < max; k += j) n             if (k % j == 0 && j % i == 0) { n               int cost = abs(a - i) + abs(b - j) + abs(c - k); n               if (cost < min_cost) { n                 min_cost = cost; n                 min_a = i; n                 min_b = j; n                 min_c = k; n               } n             } n       cout << min_cost << endl; n       cout << min_a <<     << min_b <<     << min_c << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100010; n vector<pair<long long, int>> adj[10 * MAXN]; n long long dist[10 * MAXN]; n int par[10 * MAXN]; n int go1(int idx, int l, int r, int ds) { n   if (idx > 1) { n     int par = idx / 2; n     adj[par + ds].push_back({0, idx + ds}); n   } n   if (l == r) { n     adj[idx + ds].push_back({0, l}); n     return idx; n   } n   int m = (l + r) / 2; n   int ret = n       max(idx, max(go1(2 * idx, l, m, ds), go1(2 * idx + 1, m + 1, r, ds))); n   return ret; n } n void go2(int idx, int l, int r, int ds) { n   for (int i = l; i < (r + 1); ++i) { n     adj[i].push_back({0, idx + ds}); n   } n   if (r > l) { n     int m = (l + r) / 2; n     go2(2 * idx, l, m, ds); n     go2(2 * idx + 1, m + 1, r, ds); n   } n } n void go3(int idx, int x, int y, int l, int r, function<void(int)> f) { n   if (y < l || x > r) return; n   if (x >= l && y <= r) { n     f(idx); n     return; n   } n   int m = (x + y) / 2; n   go3(2 * idx, x, m, l, r, f); n   go3(2 * idx + 1, m + 1, y, l, r, f); n } n void dijsktra(int src) { n   memset(dist, 0x3f3f3f3f, sizeof(dist)); n   dist[src] = 0; n   par[src] = src; n   priority_queue<pair<long long, int>, vector<pair<long long, int>>, n                  greater<pair<long long, int>>> n       pq; n   pq.push({dist[src], src}); n   while (!pq.empty()) { n     int u = pq.top().second; n     long long l = pq.top().first; n     pq.pop(); n     if (dist[u] == l) { n       for (auto p : adj[u]) { n         long long ndist = l + p.first; n         if (ndist < dist[p.second]) { n           dist[p.second] = ndist; n           par[p.second] = u; n           pq.push({dist[p.second], p.second}); n         } n       } n     } n   } n } n int main() { n   int n, m, src; n   scanf(  %d %d , &(n), &(m)); n   scanf(  %d , &(src)); n   --src; n   int ds1 = n - 1 + 5; n   int ds2 = go1(1, 0, n - 1, ds1) + ds1 + 5; n   go2(1, 0, n - 1, ds2); n   while (m--) { n     int tp; n     scanf(  %d , &(tp)); n     if (tp == 1) { n       int u, v, w; n       scanf(  %d %d , &(u), &(v)); n       scanf(  %d , &(w)); n       adj[u - 1].push_back({w, v - 1}); n     } else { n       int vtx, l, r, w; n       scanf(  %d %d , &(vtx), &(l)); n       scanf(  %d %d , &(r), &(w)); n       --l; n       --r; n       --vtx; n       go3(1, 0, n - 1, l, r, [ds1, ds2, tp, w, vtx](int idx) { n         if (tp == 2) { n           adj[vtx].push_back({w, idx + ds1}); n         } else { n           adj[idx + ds2].push_back({w, vtx}); n         } n       }); n     } n   } n   dijsktra(src); n   for (int i = 0; i < (n); ++i) n     printf( %lld%c , (dist[i] == 0x3f3f3f3f3f3f3f3fll ? -1 : dist[i]), n               n [i == n - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> a[300001]; n long long n, k, s, m; n bool b[300001]; n inline long long read() { n   long long sum = 0, x = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) x = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     sum = sum * 10 + ch -  0 ; n     ch = getchar(); n   } n   return sum * x; n } n inline void write(long long x) { n   if (x < 0) { n     putchar( - ); n     x = -x; n   } n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n   return; n } n inline bool cmp(pair<long long, long long> a, pair<long long, long long> b) { n   return a.first > b.first; n } n signed main() { n   n = read(); n   k = read(); n   for (register long long i = 1; i <= n; ++i) a[i].second = i; n   for (m = 1; m <= n; ++m) { n     for (register long long j = m * 2; j <= n; j += m) ++a[j].first; n     s += a[m].first; n     if (s >= k) break; n   } n   if (s < k) { n     puts( No ); n     return 0; n   } n   sort(a + 1, a + m, cmp); n   long long t = m; n   for (register long long i = 1; i <= m; ++i) { n     if (a[i].second > m / 2 && s - a[i].first >= k) { n       s -= a[i].first; n       b[a[i].second] = true; n       t--; n     } n     if (s == k) break; n   } n   puts( Yes ); n   write(t); n   putchar(  n ); n   for (register long long i = 1; i <= m; ++i) n     if (!b[i]) { n       write(i); n       putchar(   ); n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int te; n   cin >> te; n   while (te--) { n     int n, m, x, ji = 0, ou = 0; n     cin >> n >> m; n     for (int i = 0; i < n; ++i) { n       cin >> x; n       if (x & 1) n         ji++; n       else n         ou++; n     } n     if (ji == 0) { n       puts( No ); n       continue; n     } n     ji--; n     m--; n     int q = min(m / 2 * 2, ji / 2 * 2); n     m -= q; n     ou -= m; n     if (ou < 0) n       puts( No ); n     else n       puts( Yes ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, q[1000010], a[1000010], del[1000010], sz[1000010], p[1000010]; n stack<int> res; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) scanf( %d , &a[i]); n   if (a[m] <= n) a[m + 1] = n + 1; n   for (int i = 1; i <= m; i++) n     for (int j = a[i]; j < a[i + 1]; j++) p[j] = i; n   for (int i = 1; i <= n; i++) scanf( %d , &q[i]); n   for (int i = 0; i < a[1]; i++) del[i] = n; n   for (int i = 1; i <= m; i++) { n     del[a[i]] = 1; n     for (int j = a[i] + 1; j < a[i + 1]; j++) del[j] = del[j - i] + 1; n   } n   for (int i = 1; i <= n; i++) { n     if (q[i] == 1 || q[i] == 0) n       sz[i] = sz[i - 1] + 1; n     else n       sz[i] = sz[i - 1] - p[sz[i - 1]]; n   } n   if (!sz[n]) { n     puts( Poor stack! ); n     return 0; n   } n   int k = 0; n   for (int i = n; i >= 1; i--) { n     if (q[i] == -1) n       k++; n     else if (del[sz[i]] > k) n       res.push(q[i]); n   } n   while (!res.empty()) { n     printf( %d , res.top()); n     res.pop(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int s0, t0, t1, n, m, Next[500000 + 5]; n char s[500000 + 5], t[500000 + 5]; n void GetNext() { n   Next[0] = -1; n   int j = 0, k = -1; n   while (j < m) { n     if (!~k || t[j] == t[k]) n       Next[++j] = ++k; n     else n       k = Next[k]; n   } n } n int main() { n   scanf( %s%s , s, t); n   n = strlen(s), m = strlen(t); n   GetNext(); n   for (int i = 0; s[i]; i++) s0 += s[i] ==  0 ; n   for (int i = 0; t[i]; i++) t0 += t[i] ==  0 ; n   for (int i = Next[m]; t[i]; i++) t1 += t[i] ==  0 ; n   int A = t0, B = m - t0, l = m - Next[m], step = 0; n   while (A + t1 <= s0 && B + l - t1 <= n - s0) A += t1, B += l - t1, step++; n   if (t0 <= s0 && m - t0 <= n - s0) { n     printf( %s , t); n     for (int i = 0; i < step; i++) printf( %s , t + Next[m]); n     for (int i = s0 - A; i; i--) putchar( 0 ); n     for (int i = n - s0 - B; i; i--) putchar( 1 ); n     puts(  ); n   } else n     printf( %s n , s); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 * 100 + 10; n vector<int> g[maxn]; n int a[maxn], b[maxn]; n bool p[maxn]; n int n, m; n void input() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 0; i < n; i++) cin >> a[i]; n } n void bfs(int v) { n   queue<int> q; n   q.push(v); n   while (!q.empty()) { n     p[q.front()] = true; n     int u = q.front(); n     b[u]++; n     for (int i = 0; i < g[u].size(); i++) { n       b[g[u][i]]++; n       if (b[g[u][i]] == a[g[u][i]]) q.push(g[u][i]); n     } n     q.pop(); n   } n } n int main() { n   input(); n   for (int i = 0; i < n; i++) n     if (b[i] == a[i]) bfs(i); n   int ans = 0; n   for (int i = 0; i < n; i++) ans += p[i]; n   cout << ans << endl; n   ; n   for (int i = 0; i < n; i++) n     if (p[i]) cout << i + 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n int main() { n   string s; n   cin >> s; n   int k; n   cin >> k; n   unordered_set<string> st; n   for (int i = 0; i < k; i++) { n     string temp; n     cin >> temp; n     st.insert(temp); n     string temp2 =   ; n     temp2 = temp2 + temp[1]; n     temp2 = temp2 + temp[0]; n     st.insert(temp2); n   } n   vector<pair<char, int> > arr; n   for (int i = 0; i < s.size(); i++) { n     int count = 1; n     int j = i + 1; n     while (j < s.size() && s[j] == s[i]) { n       count++; n       j++; n     } n     arr.push_back(make_pair(s[i], count)); n     i = j - 1; n   } n   int ans = 0; n   for (int i = 0; i < arr.size() - 1; i++) { n     string temp =   ; n     temp = temp + arr[i].first; n     temp = temp + arr[i + 1].first; n     if (st.find(temp) != st.end()) { n       int c1 = arr[i].second, c2 = arr[i + 1].second; n       int j = i + 2; n       while (j < s.size()) { n         if (arr[j].first == arr[i].first) n           c1 += arr[j].second; n         else if (arr[j].first == arr[i + 1].first) n           c2 += arr[j].second; n         else n           break; n         j++; n       } n       ans += min(c1, c2); n       i = j - 1; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, x, y; n long long dp[10000005]; n int main(void) { n   scanf( %lld%lld%lld , &n, &x, &y); n   dp[1] = x; n   for (int i = 2; i <= n; ++i) { n     if (i & 1) { n       dp[i] = min(dp[i - 1] + x, dp[(i + 1) >> 1] + x + y); n     } else n       dp[i] = min(dp[i - 1] + x, dp[i >> 1] + y); n   } n   printf( %lld n , dp[n]); n } n


#include <bits/stdc++.h> n using namespace std; n int n, nn, sum; n vector<double> q; n double x, ans, anss; n int main() { n   scanf( %d , &n); n   sum = 0; n   for (int i = 1; i <= 2 * n; i++) { n     scanf( %lf , &x); n     ans += (x - floor(x)); n     if (floor(x) == ceil(x)) sum++; n   } n   if (sum > n) n = n * 2 - sum; n   sum = min(sum, n); n   anss = 10000000; n   for (int i = n - sum; i <= n; i++) anss = min(anss, fabs(ans - i)); n   printf( %.3lf , anss); n } n


#include <bits/stdc++.h> n using namespace std; n struct TnT { n   int k, f; n } T[111]; n bool cmp(TnT a, TnT b) { n   if (a.f == b.f) return a.k < b.k; n   return a.f < b.f; n } n map<int, int> t; n int main() { n   ios::sync_with_stdio(false); n   int n, m, ans = -1; n   cin >> n >> m; n   for (int i = 0; i < m; i++) cin >> T[i].k >> T[i].f; n   sort(T, T + m, cmp); n   for (int k = 1; k <= 100; k++) { n     int cnt = 1; n     for (int i = 1; i <= 110; i += k) { n       for (int j = i; j <= i + k - 1; j++) t[j] = cnt; n       cnt++; n     } n     int flag = 1; n     for (int i = 0; i < m; i++) { n       if (t[T[i].k] != T[i].f) flag = 0; n     } n     if (flag) { n       if (ans == -1) n         ans = t[n]; n       else if (ans != t[n]) { n         cout << -1 << endl; n         return 0; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool distinct(int n) { n   set<int> a; n   int i, c = 0; n   while (n > 0) { n     int d = n % 10; n     a.insert(d); n     n /= 10; n   } n   int l = a.size(); n   for (int i = 0; i <= l - 1; i++) { n     c++; n   } n   if (c == 4) { n     return true; n   } else { n     return false; n   } n } n int main() { n   int y; n   cin >> y; n   for (int i = y; i <= 10000; i++) { n     if (i > y) { n       if (distinct(i)) { n         cout << i; n         break; n       } n     } n   } n   return 0; n } n


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