#include <bits/stdc++.h> n using namespace std; n const int N = 503; n char thien[N][N]; n int n, m, visit[N][N]; n vector<pair<int, int> > adj[N][N]; n int hang[4] = {-1, 0, 1, 0}; n int cot[4] = {0, 1, 0, -1}; n struct ds { n   char type; n   int u, v; n }; n vector<ds> Ans; n void DFS(int u, int v) { n   visit[u][v] = true; n   Ans.push_back({ B , u, v}); n   for (int dir = 0; dir < 4; ++dir) { n     int nu = u + hang[dir], nv = v + cot[dir]; n     if (nu < 1 || nu > n || nv < 1 || nv > m || thien[nu][nv] ==  #  || n         visit[nu][nv]) n       continue; n     adj[u][v].push_back({nu, nv}), DFS(nu, nv); n   } n } n void phudo(int u, int v) { n   for (auto it : adj[u][v]) { n     int x = it.first, y = it.second; n     phudo(x, y); n     Ans.push_back({ D , x, y}), Ans.push_back({ R , x, y}); n   } n } n int main() { n   if (fopen( trash.inp ,  r )) n     freopen( trash.inp ,  r , stdin), freopen( trash.out ,  w , stdout); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) cin >> thien[i][j]; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       if (!visit[i][j] && thien[i][j] !=  # ) { n         DFS(i, j); n         phudo(i, j); n       } n   cout << Ans.size() <<   n ; n   for (auto it : Ans) cout << it.type <<     << it.u <<     << it.v <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000010], b[1000010]; n int f[1000010], g[1000010]; n int tmp[1000010]; n int ansf[1000010], ansg[1000010]; n int main() { n   int sv, n, m; n   scanf( %d %d , &sv, &n); n   int p = 0, q = 0; n   for (int i = 1; i <= n; i++) { n     int x; n     scanf( %d , &x); n     x -= sv; n     if (x == 0) { n       puts( 1 ); n       return 0; n     } n     int y = abs(x); n     if (x < 0) n       a[++p] = y; n     else n       b[++q] = y; n   } n   if (!p || !q) { n     puts( -1 ); n     return 0; n   } n   sort(a + 1, a + p + 1); n   p = unique(a + 1, a + p + 1) - a - 1; n   sort(b + 1, b + q + 1); n   q = unique(b + 1, b + q + 1) - b - 1; n   n = 0; n   for (int i = 1; i <= p; i++) { n     f[++n] = a[i]; n     ansf[a[i]] = 1; n   } n   for (int i = 1; i < 1005; i++) { n     int N = 0; n     for (int j = 1; j <= n; j++) { n       for (int k = 1; k <= p; k++) { n         int x = f[j] + a[k]; n         if (!ansf[x]) { n           ansf[x] = i + 1; n           tmp[++N] = x; n         } n       } n     } n     n = N; n     for (int j = 1; j <= n; j++) f[j] = tmp[j]; n   } n   m = 0; n   for (int i = 1; i <= q; i++) { n     g[++m] = b[i]; n     ansg[b[i]] = 1; n   } n   for (int i = 1; i < 1005; i++) { n     int N = 0; n     for (int j = 1; j <= m; j++) { n       for (int k = 1; k <= q; k++) { n         int x = g[j] + b[k]; n         if (!ansg[x]) { n           ansg[x] = i + 1; n           tmp[++N] = x; n         } n       } n     } n     m = N; n     for (int j = 1; j <= m; j++) g[j] = tmp[j]; n   } n   int res = 0x3f3f3f3f; n   for (int i = 1; i < 1000010; i++) { n     if (ansf[i] && ansg[i]) res = min(res, ansf[i] + ansg[i]); n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long a[n + 10]; n   for (int i = 1; i <= n; ++i) scanf( %lld , &a[i]); n   long long x, y; n   cin >> x >> y; n   long long h = x + y; n   long long sum = 0; n   for (int i = 1; i <= n; ++i) { n     sum = sum + y * (a[i] / (h)); n     a[i] = a[i] % h; n     if (a[i] > x) sum += y; n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s[100]; n long long C[51][3000][2]; n int Count[2000100]; n double DP[2000100]; n int l; n double get(int mask) { n   int left = Count[mask]; n   if (left == 0) return 0; n   if (DP[mask] >= 0) return DP[mask]; n   double res = 0; n   int cnt = 0; n   for (int i = 0; i < l; i++) n     if ((mask & (1 << i)) == 0) { n       res += left + Count[mask | (1 << i)] * get(mask | (1 << i)); n       cnt++; n     } n   return DP[mask] = res / cnt / left; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> s[i]; n   l = s[0].size(); n   int h1 = l / 2; n   int h2 = l - l / 2; n   for (int j = 0; j < n; j++) n     for (int i = 0; i < (1 << h1); i++) { n       long long c = 0; n       for (int k = 0; k < n; k++) { n         if (k != j) { n           int common = 0; n           for (int m = 0; m < h1; m++) n             if (s[j][m] == s[k][m]) common |= 1 << m; n           if ((i & common) == i) c |= 1ll << k; n         } n       } n       C[j][i][0] = c; n     } n   for (int j = 0; j < n; j++) n     for (int i = 0; i < (1 << h2); i++) { n       long long c = 0; n       for (int k = 0; k < n; k++) { n         if (j != k) { n           int common = 0; n           for (int m = 0; m < h2; m++) n             if (s[j][m + h1] == s[k][m + h1]) common |= 1 << m; n           if ((i & common) == i) c |= 1ll << k; n         } n       } n       C[j][i][1] = c; n     } n   int mask = (1 << h1) - 1; n   for (int i = 0; i < (1 << l); i++) { n     int cnt = 0; n     for (int j = 0; j < n; j++) { n       long long common = C[j][mask & i][0] & C[j][i >> h1][1]; n       if (common != 0) cnt++; n     } n     Count[i] = cnt; n   } n   for (int i = 0; i < (1 << l); i++) DP[i] = -1.0; n   printf( %.15llf n , get(0)); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   long long m = 200, ans = 0; n   while (n--) { n     long long a, p; n     cin >> a >> p; n     m = min(m, p); n     ans += m * a; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n typedef struct mk { n   int v; n   struct mk *next; n } roy; n roy *add(roy *head, int v) { n   roy *t = (roy *)malloc(sizeof(roy)); n   t->v = v; n   t->next = NULL; n   if (head == NULL) return t; n   t->next = head; n   return t; n } n long long n, a[300010], s[300030], u, v, count; n roy *gp[300030]; n void dfs(int x, int p) { n   s[x] += a[x]; n   roy *t = gp[x]; n   while (t != NULL) { n     int y = t->v; n     if (y != p) { n       dfs(y, x); n       if (s[y] > 0) s[x] += s[y]; n     } n     t = t->next; n   } n } n void go(int x, int p, long long total) { n   s[x] += a[x]; n   roy *t = gp[x]; n   while (t != NULL) { n     int y = t->v; n     if (y != p) { n       go(y, x, total); n       if (s[y] > 0) s[x] += s[y]; n     } n     t = t->next; n   } n   if (s[x] == total) s[x] = 0, count++; n } n int main() { n   for (int i = 0; i < 300030; i++) gp[i] = NULL; n   scanf( %lld , &n); n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n   for (int i = 1; i < n; i++) { n     scanf( %lld%lld , &u, &v); n     gp[u] = add(gp[u], v); n     gp[v] = add(gp[v], u); n   } n   dfs(1, -1); n   long long mx = -1e18; n   for (int i = 1; i <= n; i++) { n     mx = (mx < s[i]) ? s[i] : mx; n     s[i] = 0; n   } n   go(1, -1, mx); n   printf( %lld %lld n , mx * count, count); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int s = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     s = (s << 3) + (s << 1) + ch -  0 , ch = getchar(); n   return s * f; n } n const int N = 1e5 + 5, M = 320; n int n, m, fa[N], top[N], t[N], sz[N], son[N]; n struct Edge { n   int to, next; n } e[N]; n int head[N], ecnt, id[N], pos[N], L[M], R[M]; n inline void adde(int u, int v) { n   e[++ecnt].next = head[u]; n   e[ecnt].to = v; n   head[u] = ecnt; n } n void dfs1(int u) { n   sz[u] = 1; n   for (int i = head[u], v; i; i = e[i].next) { n     v = e[i].to; n     dfs1(v); n     sz[u] += sz[v]; n     if (sz[son[u]] < sz[v]) son[u] = v; n   } n } n int inde, p[N], cnt[M][N], ans, tag[M], blk, blksz; n void dfs2(int u, int fr) { n   id[u] = ++inde; n   p[inde] = u; n   top[u] = fr; n   ++cnt[pos[inde]][t[u]]; n   if (son[u]) dfs2(son[u], fr); n   for (int i = head[u], v; i; i = e[i].next) { n     v = e[i].to; n     if (v != son[u]) dfs2(v, v); n   } n } n bool black[N]; n inline void modify_blk(int x, int l, int r, int val) { n   if (l <= L[x] && R[x] <= r) { n     if (val > 0) { n       ++tag[x]; n       ans += cnt[x][tag[x]]; n     } else { n       ans -= cnt[x][tag[x]]; n       --tag[x]; n     } n   } else { n     for (int i = max(l, L[x]), u; i <= min(r, R[x]); ++i) { n       u = p[i]; n       if (black[u]) { n         if (val > 0) n           --t[u]; n         else n           ++t[u]; n         continue; n       } n       if (val > 0) { n         --cnt[x][t[u]]; n         ++cnt[x][--t[u]]; n         if (t[u] == tag[x]) ++ans; n       } else { n         --cnt[x][t[u]]; n         if (t[u] == tag[x]) --ans; n         ++cnt[x][++t[u]]; n       } n     } n   } n } n inline void modify_node(int u, int val) { n   if (black[u] && !val) { n     ++cnt[pos[id[u]]][t[u]]; n     if (t[u] <= tag[pos[id[u]]]) ++ans; n   } else if (!black[u] && val) { n     --cnt[pos[id[u]]][t[u]]; n     if (t[u] <= tag[pos[id[u]]]) --ans; n   } n   black[u] = val; n } n inline void modify(int x, int val) { n   if (black[x] == val) return; n   modify_node(x, val); n   int u = fa[x], l, r; n   while (u) { n     l = id[top[u]]; n     r = id[u]; n     if (pos[l] == pos[r]) { n       modify_blk(pos[l], l, r, black[x]); n     } else { n       modify_blk(pos[l], l, r, black[x]); n       modify_blk(pos[r], l, r, black[x]); n       for (int i = pos[l] + 1; i < pos[r]; ++i) modify_blk(i, l, r, black[x]); n     } n     u = fa[top[u]]; n   } n } n int main() { n   n = read(); n   m = read(); n   for (int i = 2; i <= n; ++i) fa[i] = read(), adde(fa[i], i); n   for (int i = 1; i <= n; ++i) t[i] = read() + 1; n   blksz = min(N / (M - 2), int(sqrt(n))); n   blk = (n - 1) / blksz + 1; n   for (int i = 1; i <= n; ++i) pos[i] = (i - 1) / blksz + 1; n   for (int i = 1; i <= blk; ++i) L[i] = (i - 1) * blksz + 1, R[i] = i * blksz; n   R[blk] = n; n   dfs1(1); n   dfs2(1, 0); n   while (m--) { n     int node = read(), val = 1; n     if (node < 0) node = -node, val = 0; n     modify(node, val); n     printf( %d  , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int inf = (int)1e9; n const int mod = 1e9 + 7; n using namespace std; n long long a[1011][1011]; n int n, m; n int id[1000011]; n long long k; n int d[1000011]; n int c[1011][1011]; n int now = 0; n vector<pair<long long, pair<int, int> > > b; n bool cmp(pair<long long, pair<int, int> > a, n          pair<long long, pair<int, int> > b) { n   return a.first > b.first; n } n int get(int x) { n   if (d[x] == x) { n     return x; n   } n   return get(d[x]); n } n void unite(int x, int y) { n   x = get(x); n   y = get(y); n   if (x == y) { n     return; n   } n   if (id[x] <= id[y]) { n     d[x] = y; n     id[y] += id[x]; n   } else { n     d[y] = x; n     id[x] += id[y]; n   } n } n void rec(int x, int y, int cnt, int mn) { n   if (now >= cnt) { n     return; n   } n   now++; n   c[x][y] = mn; n   if (a[x - 1][y] >= mn && c[x - 1][y] == 0) { n     rec(x - 1, y, cnt, mn); n   } n   if (a[x + 1][y] >= mn && c[x + 1][y] == 0) { n     rec(x + 1, y, cnt, mn); n   } n   if (a[x][y + 1] >= mn && c[x][y + 1] == 0) { n     rec(x, y + 1, cnt, mn); n   } n   if (a[x][y - 1] >= mn && c[x][y - 1] == 0) { n     rec(x, y - 1, cnt, mn); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n       b.push_back(make_pair(a[i][j], make_pair(i, j))); n       d[(i - 1) * m + j] = (i - 1) * m + j; n       id[(i - 1) * m + j] = 1; n     } n   } n   sort(b.begin(), b.end(), &cmp); n   for (int x = 0; x < b.size(); x++) { n     int i = b[x].second.first, j = b[x].second.second; n     if (a[i - 1][j] >= a[i][j]) { n       unite((i - 1) * m + j, (i - 2) * m + j); n     } n     if (a[i + 1][j] >= a[i][j]) { n       unite((i - 1) * m + j, (i)*m + j); n     } n     if (a[i][j + 1] >= a[i][j]) { n       unite((i - 1) * m + j, (i - 1) * m + j + 1); n     } n     if (a[i][j - 1] >= a[i][j]) { n       unite((i - 1) * m + j, (i - 1) * m + j - 1); n     } n     if (k % a[i][j] == 0) { n       if (id[get((i - 1) * m + j)] >= k / a[i][j]) { n         cout <<  YES  << endl; n         rec(i, j, k / a[i][j], a[i][j]); n         for (int k = 1; k <= n; k++) { n           for (int l = 1; l <= m; l++) { n             cout << c[k][l] <<    ; n           } n           cout << endl; n         } n         return 0; n       } n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n struct val { n   long long x; n   int y, d; n   val *p, *l, *r; n } g1[100001], g2[100001], *rt1[100001], *rt2[100001], *nil = g1; n struct node { n   long long x; n   int y; n   bool operator<(const node &b) const { return x < b.x; } n } a[100001]; n struct vale { n   long long x; n   int y; n   bool operator<(const vale &b) const { return x > b.x; } n } c[100001]; n using namespace std; n int n, g[100001], h[100001], fa[100001], d[100001], wt, ss[19]; n char fl[1 << 23], *A = fl; n long long f[100001], e[100001], b[100001]; n void read(int &x) { n   char c; n   for (x = 0;  0  > (c = *A++) || c >  9 ;) n     ; n   while ( 0  <= c && c <=  9 ) x = (x << 3) + (x << 1) + c - 48, (c = *A++); n } n void read(long long &x) { n   char c; n   for (x = 0;  0  > (c = *A++) || c >  9 ;) n     ; n   while ( 0  <= c && c <=  9 ) x = (x << 3) + (x << 1) + c - 48, (c = *A++); n } n void print(long long x) { n   if (!x) n     putchar(48); n   else { n     for (wt = 0; x; ss[++wt] = x % 10, x /= 10) n       ; n     for (; wt; putchar(ss[wt--] + 48)) n       ; n   } n } n val *mg1(val *x, val *y) { n   return x == nil n              ? y n              : (y == nil n                     ? x n                     : (x->x < y->x n                            ? ((x->r = mg1(x->r, y))->p = x, n                               x->l->d < x->r->d ? (swap(x->l, x->r), 0) : 0, n                               x->d = x->l->d + 1, x) n                            : ((y->r = mg1(y->r, x))->p = y, n                               y->l->d < y->r->d ? (swap(y->l, y->r), 0) : 0, n                               y->d = y->l->d + 1, y))); n } n val *mg2(val *x, val *y) { n   return x == nil n              ? y n              : (y == nil n                     ? x n                     : (x->x > y->x n                            ? ((x->r = mg2(x->r, y))->p = x, n                               x->l->d < x->r->d ? (swap(x->l, x->r), 0) : 0, n                               x->d = x->l->d + 1, x) n                            : ((y->r = mg2(y->r, x))->p = y, n                               y->l->d < y->r->d ? (swap(y->l, y->r), 0) : 0, n                               y->d = y->l->d + 1, y))); n } n void fix_min(long long t1, int z) { n   int tmp, x, LIA; n   if (a[x = g[z]].x != t1) n     if (a[x].x = t1, x > 1 && a[x] < a[x >> 1]) n       for (; x > 1 && a[x] < a[x >> 1]; n            swap(a[x], a[x >> 1]), swap(g[a[x].y], g[a[x >> 1].y]), x >>= 1) n         ; n     else n       for (; (x << 1) <= n;) { n         if (a[tmp = x << 1] < a[LIA = x]) LIA = tmp; n         if ((tmp |= 1) <= n && a[tmp] < a[LIA]) LIA = tmp; n         if (LIA != x) n           swap(a[LIA], a[x]), swap(g[a[LIA].y], g[a[x].y]), x = LIA; n         else n           break; n       } n } n void fix_max(long long t1, int z) { n   int tmp, x, LIA; n   if (c[x = h[z]].x != t1) n     if (c[x].x = t1, x > 1 && c[x] < c[x >> 1]) n       for (; x > 1 && c[x] < c[x >> 1]; n            swap(c[x], c[x >> 1]), swap(h[c[x].y], h[c[x >> 1].y]), x >>= 1) n         ; n     else n       for (; (x << 1) <= n;) { n         if (c[tmp = x << 1] < c[LIA = x]) LIA = tmp; n         if ((tmp |= 1) <= n && c[tmp] < c[LIA]) LIA = tmp; n         if (LIA != x) n           swap(c[LIA], c[x]), swap(h[c[LIA].y], h[c[x].y]), x = LIA; n         else n           break; n       } n } n void fix(int x) { n   if (g1[x].p == nil) n     (rt1[fa[x]] = mg1(g1[x].l, g1[x].r))->p = nil; n   else if (g1[x].p->l == &g1[x]) n     (g1[x].p->l = mg1(g1[x].l, g1[x].r))->p = g1[x].p; n   else n     (g1[x].p->r = mg1(g1[x].l, g1[x].r))->p = g1[x].p; n   g1[x].x = f[x], g1[x].l = g1[x].r = nil, n   (rt1[fa[x]] = mg1(rt1[fa[x]], &g1[x]))->p = nil; n   if (g2[x].p == nil) n     (rt2[fa[x]] = mg2(g2[x].l, g2[x].r))->p = nil; n   else if (g2[x].p->l == &g2[x]) n     (g2[x].p->l = mg2(g2[x].l, g2[x].r))->p = g2[x].p; n   else n     (g2[x].p->r = mg2(g2[x].l, g2[x].r))->p = g2[x].p; n   g2[x].x = f[x], g2[x].l = g2[x].r = nil, n   (rt2[fa[x]] = mg2(rt2[fa[x]], &g2[x]))->p = nil; n } n void alter(int z, int x, int y, int LAx, int LAy) { n   long long t1 = e[x], t3 = e[y]; n   int t2 = b[x] % d[x], t4 = b[y] % d[y]; n   f[y] += b[y] % (++d[y]) + e[z] - t4, e[y] = b[y] / d[y], f[LAy] += e[y] - t3, n                                        f[y] += e[y] - t3; n   f[x] += b[x] % (--d[x]) - e[z] - t2, e[x] = b[x] / d[x], f[LAx] += e[x] - t1, n                                        f[x] += e[x] - t1; n   if (LAx == LAy) { n     if (e[y] + e[x] != t1 + t3) fix(LAx); n   } else { n     if (e[y] != t3) fix(LAy); n     if (e[x] != t1) fix(LAx); n   } n   fix(x), fix(y); n   if (g1[z].p == nil) n     (rt1[x] = mg1(g1[z].l, g1[z].r))->p = nil; n   else if (g1[z].p->l == &g1[z]) n     (g1[z].p->l = mg1(g1[z].l, g1[z].r))->p = g1[z].p; n   else n     (g1[z].p->r = mg1(g1[z].l, g1[z].r))->p = g1[z].p; n   g1[z].x = f[z], g1[z].l = g1[z].r = nil, n   (rt1[fa[z] = y] = mg1(rt1[y], &g1[z]))->p = nil; n   if (g2[z].p == nil) n     (rt2[x] = mg2(g2[z].l, g2[z].r))->p = nil; n   else if (g2[z].p->l == &g2[z]) n     (g2[z].p->l = mg2(g2[z].l, g2[z].r))->p = g2[z].p; n   else n     (g2[z].p->r = mg2(g2[z].l, g2[z].r))->p = g2[z].p; n   g2[z].x = f[z], g2[z].l = g2[z].r = nil, n   (rt2[y] = mg2(rt2[y], &g2[z]))->p = nil; n   fix_max(e[fa[LAx]] + rt2[fa[LAx]]->x, rt2[fa[LAx]]->y); n   fix_max(e[fa[LAy]] + rt2[fa[LAy]]->x, rt2[fa[LAy]]->y); n   fix_max(rt2[LAx]->x + e[LAx], rt2[LAx]->y); n   fix_max(rt2[LAy]->x + e[LAy], rt2[LAy]->y); n   if (rt2[x] != nil) fix_max(rt2[x]->x + e[x], rt2[x]->y); n   fix_max(rt2[y]->x + e[y], rt2[y]->y); n   fix_min(e[fa[LAx]] + rt1[fa[LAx]]->x, rt1[fa[LAx]]->y); n   fix_min(e[fa[LAy]] + rt1[fa[LAy]]->x, rt1[fa[LAy]]->y); n   fix_min(rt1[LAx]->x + e[LAx], rt1[LAx]->y); n   fix_min(rt1[LAy]->x + e[LAy], rt1[LAy]->y); n   if (rt1[x] != nil) fix_min(rt1[x]->x + e[x], rt1[x]->y); n   fix_min(rt1[y]->x + e[y], rt1[y]->y); n } n void dfs(int x) { n   long long t1 = e[fa[a[x].y]] + f[a[x].y]; n   int tmp = a[x].y; n   if (a[x].x != t1) { n     if ((x << 1) <= n) dfs(x << 1); n     if ((x << 1 | 1) <= n) dfs(x << 1 | 1); n     fix_min(t1, tmp); n   } n } n void vfs(int x) { n   long long t1 = e[fa[c[x].y]] + f[c[x].y]; n   int tmp = c[x].y; n   if (c[x].x != t1) { n     if ((x << 1) <= n) vfs(x << 1); n     if ((x << 1 | 1) <= n) vfs(x << 1 | 1); n     fix_max(t1, tmp); n   } n } n int main() { n   int test, i, x, y, tmp, LIA; n   for (*(fl + fread(fl, 1, 1 << 23, stdin)) = EOF, read(n), read(test), i = 1; n        i <= n; d[i] = 2, rt1[i] = rt2[i] = nil, read(b[i++])) n     ; n   for (i = 1; i <= n; read(fa[i]), d[fa[i]]++, i++) n     ; n   for (i = 1; i <= n; n        f[i] += b[i] % d[i] + (e[i] = b[i] / d[i]), f[fa[i]] += e[i], i++) n     ; n   for (i = 1; i <= n; g1[i] = g2[i] = (val){f[i], i, 1, nil, nil, nil}, n       (rt1[fa[i]] = mg1(rt1[fa[i]], &g1[i]))->p = nil, n       (rt2[fa[i]] = mg2(rt2[fa[i]], &g2[i]))->p = nil, n       a[i] = (node){f[i] + e[fa[i]], i}, c[i] = (vale){f[i] + e[fa[i]], i}, i++) n     ; n   for (sort(a + 1, a + n + 1), sort(c + 1, c + n + 1), i = 1; i <= n; n        g[a[i].y] = i, h[c[i].y] = i, i++) n     ; n   for (; test--;) switch (read(x), x) { n       case 1: n         if (read(x), read(y), fa[x] != y) alter(x, fa[x], y, fa[fa[x]], fa[y]); n         break; n       case 2: n         read(x), print(e[fa[x]] + f[x]), putchar(  n ); n         break; n       case 3: n         dfs(1), vfs(1), print(a[1].x), putchar(   ), print(c[1].x), n             putchar(  n ); n         break; n     } n } n


#include <bits/stdc++.h> n using namespace std; n struct number { n   deque<int> q; n   map<int, int> db; n   int curr = 0; n }; n struct pii { n   int x, y, id; n }; n const int S = 300; n bool sortFunc(pii a, pii b) { n   if (a.x / S == b.x / S) return ((a.x / S) & 1 ? a.y > b.y : a.y < b.y); n   return a.x < b.x; n } n vector<number> t(1e5 + 1); n void inc(map<int, int>& db, int num, int& curr) { n   if (db.count(num) == 0) db[num] == 0; n   if (++db[num] == 1) curr++; n } n void dec(map<int, int>& db, int num, int& curr) { n   if (--db[num] == 0) curr--; n } n int curr = 0, nums = 0; n void add(int poz, int num) { n   if (t[num].curr <= 1 && t[num].q.size() > 0) curr--; n   if (t[num].q.empty()) n     t[num].q.push_front(poz); n   else if (poz < t[num].q.front()) { n     inc(t[num].db, t[num].q.front() - poz, t[num].curr); n     t[num].q.push_front(poz); n   } else { n     inc(t[num].db, poz - t[num].q.back(), t[num].curr); n     t[num].q.push_back(poz); n   } n   if (t[num].curr <= 1) curr++; n   if (t[num].q.size() == 1) nums++; n } n void remove(int poz, int num) { n   if (t[num].curr <= 1) curr--; n   if (t[num].q.size() == 1) n     t[num].q.pop_front(); n   else if (poz == t[num].q.front()) { n     t[num].q.pop_front(); n     dec(t[num].db, t[num].q.front() - poz, t[num].curr); n   } else { n     t[num].q.pop_back(); n     dec(t[num].db, poz - t[num].q.back(), t[num].curr); n   } n   if (t[num].curr <= 1 && t[num].q.size() > 0) curr++; n   if (t[num].q.size() == 0) nums--; n } n int main() { n   int n; n   cin >> n; n   vector<int> sor(n); n   for (int& a : sor) cin >> a; n   int q; n   cin >> q; n   vector<pii> v(q); n   for (int i = 0; i < q; i++) n     cin >> v[i].x >> v[i].y, v[i].id = i, v[i].x--, v[i].y--; n   sort(v.begin(), v.end(), sortFunc); n   int l = 0, r = -1; n   vector<int> ans(q); n   for (pii& a : v) { n     while (a.x < l) add(l - 1, sor[l - 1]), l--; n     while (r < a.y) add(r + 1, sor[r + 1]), r++; n     while (l < a.x) remove(l, sor[l]), l++; n     while (a.y < r) remove(r, sor[r]), r--; n     ans[a.id] = (curr > 0 ? nums : nums + 1); n   } n   for (int& a : ans) cout << a <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int MAX = 200005; n long long modexp(long long x, long long n) { n   if (n == 0) return 1LL; n   if (n % 2 == 0) { n     long long y = modexp(x, n / 2) % mod; n     return (y * y) % mod; n   } n   return (x * modexp(x, n - 1) % mod) % mod; n } n long long powr(long long x, long long n) { n   long long ans = 1; n   for (int i = 1; i <= n; i++) ans = ans * x; n   return ans; n } n int main() { n   int n; n   scanf( %d , &n); n   int a[n]; n   int dp[n]; n   pair<int, int> aux[n]; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     dp[i] = mod; n     aux[i] = make_pair(a[i], i); n   } n   sort(aux, aux + n); n   sort(a, a + n); n   dp[2] = a[2] - a[0]; n   dp[3] = a[3] - a[0]; n   dp[4] = a[4] - a[0]; n   int idx[n]; n   for (int i = 0; i < 5; i++) idx[i] = 0; n   for (int i = 5; i < n; i++) { n     vector<pair<int, int> > p; n     if (i - 2 >= 3) p.push_back(make_pair(dp[i - 3] + a[i] - a[i - 2], i - 2)); n     if (i - 3 >= 3) p.push_back(make_pair(dp[i - 4] + a[i] - a[i - 3], i - 3)); n     if (i - 4 >= 3) p.push_back(make_pair(dp[i - 5] + a[i] - a[i - 4], i - 4)); n     sort(p.begin(), p.end()); n     dp[i] = p[0].first; n     idx[i] = p[0].second; n   } n   int color[n]; n   int team = 0; n   for (int i = n - 1; i > 0; i--) { n     team++; n     int x = idx[i]; n     for (int j = x; j <= i; j++) { n       color[j] = team; n     } n     i = x; n   } n   printf( %d %d n , dp[n - 1], team); n   int ans[n]; n   for (int i = 0; i < n; i++) { n     ans[aux[i].second] = color[i]; n   } n   for (int i = 0; i < n; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, 1, -1}; n int dy[] = {1, -1, 0, 0}; n char dir[] = { R ,  L ,  D ,  U }; n const int MOD = 1e9 + 7; n const long long INF = 1e9; n void setIO(string filename) { n   if (fopen((filename +  .in ).c_str(),  r )) { n     freopen((filename +  .in ).c_str(),  r , stdin); n     freopen((filename +  .out ).c_str(),  w , stdout); n   } n } n void solve() { n   int n; n   cin >> n; n   vector<pair<long long, long long> > a(n); n   for (auto &i : a) cin >> i.first >> i.second; n   sort((a).begin(), (a).end()); n   for (int i = 0; i < n - 1; i++) { n     pair<long long, long long> curr = a[i]; n     if (a[i + 1].first - a[i].first >= 30) n       continue; n     else { n       while (curr.first < a[i + 1].first) { n         curr.second = (curr.second + 4 - 1) / 4; n         curr.first++; n       } n       a[i + 1].second += max(0ll, curr.second - a[i + 1].second); n     } n   } n   long long ans = a.back().first; n   while (a.back().second > 1) { n     ; n     a.back().second = (a.back().second + 4 - 1) / 4; n     ans++; n   } n   cout << max(1ll, ans + (a.back().first == ans)) <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int t = 1; n   for (int i = 1; i <= t; i++) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int flag = 0; n   for (int i = 0; i < n - 1; i++) { n     if (s[i] > s[i + 1]) { n       flag = 1; n       cout <<  YES  << endl; n       cout << i + 1 <<     << i + 2; n       break; n       return 0; n     } n   } n   if (flag == 0) { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   string s; n   cin >> s; n   int n = s.length(); n   s[n - 1] = s[0]; n   cout << s <<   n ; n   return; n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n using pil = pair<int, ll>; n using pli = pair<ll, int>; n using pll = pair<ll, ll>; n const int inf = (1 << 30) - 1; n const ll INF = (1LL << 60) - 1; n const double pi = acos(-1.0); n const double EPS = 1e-10; n template <typename T> n bool chmax(T &x, const T &y) { n   if (x < y) { n     x = y; n     return true; n   } n   return false; n } n template <typename T> n bool chmin(T &x, const T &y) { n   if (x > y) { n     x = y; n     return true; n   } n   return false; n } n template <typename T> n void in(T &x) { n   cin >> x; n } n template <typename T> n void out(T x) { n   cout << x <<   n ; n } n template <typename T> n void out2(T x) { n   cout << x <<    ; n } n template <typename T, typename U> n void in(pair<T, U> &p) { n   cin >> p.first >> p.second; n } n template <typename T, typename U> n void out(pair<T, U> p) { n   cout << p.first <<     << p.second <<   n ; n } n using vi = vector<int>; n using vll = vector<ll>; n template <typename T> n void in(vector<T> &a) { n   for (int i = 0; i < (int)a.size(); i++) cin >> a[i]; n } n template <typename T> n void out(const vector<T> &a) { n   for (auto &e : a) cout << e <<   n ; n } n template <typename T> n void out2(const vector<T> &a) { n   for (auto &e : a) cout << e <<    ; n   cout <<   n ; n } n template <typename T> n void unique(vector<T> &a) { n   sort(a.begin(), a.end()), a.erase(unique(a.begin(), a.end()), a.end()); n } n vector<int> iota(int n) { n   vector<int> ret(n); n   iota(ret.begin(), ret.end(), 0); n   return ret; n } n template <typename T> n vector<int> iota(const vector<T> &a, bool greater = false) { n   vector<int> ret = iota((int)a.size()); n   sort(ret.begin(), ret.end(), n        [&](int i, int j) { return (a[i] < a[j]) ^ greater; }); n   return ret; n } n struct io_setup { n   io_setup() { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cout << fixed << setprecision(15); n   } n } io_setup; n const int MOD = 1000000007; n int main() { n   int T; n   in(T); n   int ord[] = {0, 2, 3, 1}; n   while (T--) { n     ll N; n     in(N); n     ll x = 1; n     while (x * 4 <= N) x *= 4; n     ll y = (N - x) / 3; n     ll p, q, r; n     p = x + y, q = x * 2; n     while (y) { n       x /= 4; n       q += x * ord[y / x]; n       y %= x; n     } n     r = p ^ q; n     if (N % 3 == 1) out(p); n     if (N % 3 == 2) out(q); n     if (N % 3 == 0) out(r); n   } n } n


#include <bits/stdc++.h> n using namespace std; n #define pb push_back n #define mp make_pair n #define x first n #define y second n #define LL long long n #define LD long double n #define Complex complex<double> n #define y1 ___y1 n #define next ___next n #define left ___left n #define right ___right n #define pii pair<int, int> n #define pll pair<LL, LL> n #define pdd pair<double, double> n #define pldld pair<LD, LD> n #define vi vector<int> n #define vl vector<LL> n #define vii vector<pii> n #define vll vector<pll> n #define PQ priority_queue n #define ffo fflush(stdout) n #define gch getchar() n #define e (int)(1e9 + 7) n #define e2 (double)2.71828182846 n #define pi (double)3.14159265358979 n #define ri(X) scanf( %d , &(X)) n #define rii(X, Y) scanf( %d%d , &(X), &(Y)) n #define riii(X, Y, Z) scanf( %d%d%d , &(X), &(Y), &(Z)) n #define rl(X) scanf( %lld , &(X)) n #define rll(X, Y) scanf( %lld%lld , &(X), &(Y)) n #define rlll(X, Y, Z) scanf( %lld%lld%lld , &(X), &(Y), &(Z)) n #define printi(X) printf( %d , (int)(X)) n #define printii(X, Y) printf( %d %d , (int)(X), (int)(Y)) n #define printiii(X, Y, Z) printf( %d %d %d , (int)(X), (int)(Y), (int)(Z)) n #define printl(X) printf( %lld , (LL)(X)) n #define printll(X, Y) printf( %lld %lld , (LL)(X), (LL)(Y)) n #define printlll(X, Y, Z) printf( %lld %lld %lld , (LL)(X), (LL)(Y), (LL)(Z)) n #define enter printf(  n ) n #define space printf(   ) n struct matrix { n     int sz; n     LL a[105][105]; n     matrix& operator = (const matrix &T) { n         sz = T.sz; n         for (int i = 1; i <= sz; i++) { n             for (int j = 1; j <= sz; j++) { n                 a[i][j] = T.a[i][j]; n             } n         } n         return *this; n     } n     matrix operator + (const matrix &T) const { n         matrix ans; n         ans.sz = sz; n         for (int i = 1; i <= sz; i++) { n             for (int j = 1; j <= sz; j++) { n                 ans.a[i][j] = a[i][j] + T.a[i][j]; n                 ans.a[i][j] %= e; n             } n         } n         return ans; n     } n     matrix operator * (const matrix &T) const { n         matrix ans; n         ans.sz = sz; n         for (int i = 1; i <= sz; i++) { n             for (int j = 1; j <= sz; j++) { n                 ans.a[i][j] = 0; n                 for (int k = 1; k <= sz; k++) { n                     ans.a[i][j] += a[i][k] * T.a[k][j]; n                     ans.a[i][j] %= e; n                 } n             } n         } n         return ans; n     } n     matrix I() { n         matrix ans; n         ans.sz = sz; n         for (int i = 1; i <= sz; i++) { n             for (int j = 1; j <= sz; j++) { n                 if (i == j) ans.a[i][j] = 1; n                 else ans.a[i][j] = 0; n             } n         } n         return ans; n     } n     matrix power(LL p) { n         if (p == 0) return I(); n         if (p == 1) return *this; n         matrix tmp = power(p / 2); n         if (p % 2) tmp = tmp * tmp * (*this); n         else tmp = tmp * tmp; n         return tmp; n     } n     void print() { n         for (int i = 1; i <= sz; i++) { n             for (int j = 1; j <= sz; j++) { n                 printf(j == sz?  %lld n  :  %lld  , a[i][j]); n             } n         } n         return; n     } n }; n struct polynomial { n     vector<Complex> v; n     int sz; n     polynomial operator * (const polynomial &T) const { n         polynomial ans; n         if (sz >= T.sz) { n             ans = *this; n             for (int i = 0; i < T.sz; i++) { n                 ans.v[i] *= T.v[i]; n             } n         } n         else { n             ans = T; n             for (int i = 0; i < sz; i++) { n                 ans.v[i] *= v[i]; n             } n         } n         return ans; n     } n }; n polynomial FFT(polynomial a) { n     if (a.sz <= 1) return a; n     polynomial even, odd; n     even.sz = odd.sz = 0; n     for (int i = 0; i < a.sz; i += 2) { n         even.v.pb(a.v[i]); n         even.sz++; n     } n     for (int i = 1; i < a.sz; i += 2) { n         odd.v.pb(a.v[i]); n         odd.sz++; n     } n     polynomial Even = FFT(even); n     polynomial Odd = FFT(odd); n     polynomial ans; n     ans.sz = a.sz; n     for (int i = 0; i < ans.sz; i++) { n         ans.v.pb(0); n     } n     for (int i = 0; i < a.sz / 2; i++) { n         Complex w = polar(1.0, -2 * pi * i / a.sz) * Odd.v[i]; n         ans.v[i] = Even.v[i] + w; n         ans.v[i + a.sz / 2] = Even.v[i] - w; n     } n     return ans; n } n polynomial IFFT(polynomial a) { n     for (int i = 0; i < a.sz; i++) { n         a.v[i] = conj(a.v[i]); n     } n     polynomial ans = FFT(a); n     for (int i = 0; i < a.sz; i++) { n         ans.v[i] = conj(ans.v[i]); n         ans.v[i] /= ans.sz; n     } n     return ans; n } n struct polygon { n     int n; n     pii a[1005]; n     double area() { n         double d1 = 0, d2 = 0; n         for (int i = 0; i < n; i++) { n             d1 += a[i].x * a[(i + 1) % n].y; n             d2 += a[(i + 1) % n].x * a[i].y; n         } n         return (d1 - d2) / 2; n     } n     bool inside(pldld p) { n         int ct = 0; n         LD d; n         pldld p1 = a[0]; n         for (int i = 1; i <= n; i++) { n             pldld p2 = a[i % n]; n             if (p.y > min(p1.y, p2.y) && p.y <= max(p1.y, p2.y) && p.x <= max(p1.x, p2.x) && p1.y != p2.y) { n                 d = (p.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x; n                 if (p1.x == p2.x || p.x <= d) ct++; n             } n             p1 = p2; n         } n         return ct % 2; n     } n }; n struct segtree { n     int l, r, mid; n     LL val = 0, lazy = 0; n     segtree *lc, *rc; n     segtree(int L, int R) { n         l = L; n         r = R; n         mid = (l + r) / 2; n         if (l != r) { n             lc = new segtree(l, mid); n             rc = new segtree(mid + 1, r); n         } n     } n     void apply(LL X) { n         val += X; n         // val += X * (r - l + 1); n         lazy += X; n     } n     void push() { n         if (l == r) return; n         lc->apply(lazy); n         rc->apply(lazy); n         lazy = 0; n     } n     void update(int L, int R, LL X) { n         push(); n         if (l == L && r == R) { n             apply(X); n             return; n         } n         else if (R <= mid) lc->update(L, R, X); n         else if (L >= mid + 1) rc->update(L, R, X); n         else { n             lc->update(L, mid, X); n             rc->update(mid + 1, R, X); n         } n         val = min(lc->val, rc->val); n         // val = lc->val + rc->val; n     } n     LL query(int L, int R) { n         push(); n         if (l == L && r == R) return val; n         else if (R <= mid) return lc->query(L, R); n         else if (L >= mid + 1) return rc->query(L, R); n         else { n             return min(lc->query(L, mid), rc->query(mid + 1, R)); n             // return lc->query(L, mid) + rc->query(mid + 1, R); n         } n     } n     //segroot = new segtree(0, n - 1) n } *segroot; n LL power(LL a, LL p) { n     if (p == 0) return 1; n     if (p == 1) return a; n     LL tmp = power(a, p / 2); n     if (p % 2) tmp = tmp * tmp % e * a; n     else tmp *= tmp; n     return tmp % e; n } n struct node { n     int num; n     int l, r; n }; n  n int T; n int n, a[200005]; n node p[200005]; n  n void solve(int CASE) { n     ri(n); n     for (int i = 1; i <= n; i++) { n         ri(a[i]); n     } n     int small = 0; n     int big = 0; n     int sz = 3; n     p[3].l = 1; n     p[3].r = 2; n     p[3].num = a[1]; n     int pl = 3; n     for (int i = 2; i <= n; i++) { n         if (a[i] == p[pl].num) { n         } n         else if (a[i] > p[pl].num) { n             if (p[pl].r == 2) { n                 int next = ++sz; n                 p[pl].r = next; n                 p[next].num = a[i]; n                 p[next].l = pl; n                 p[next].r = 2; n                 pl = next; n             } n             else if (p[p[pl].r].num == a[i]) { n                 pl = p[pl].r; n             } n             else if (p[p[pl].r].num > a[i]) { n                 int next = ++sz; n                 p[next].num = a[i]; n                 p[next].l = pl; n                 p[next].r = p[pl].r; n                 p[p[next].r].l = next; n                 p[pl].r = next; n                 pl = next; n             } n             else { n                 printf( NO n ); n                 return; n             } n         } n         else { n             if (p[pl].l == 1) { n                 int next = ++sz; n                 p[pl].l = next; n                 p[next].num = a[i]; n                 p[next].l = 1; n                 p[next].r = pl; n                 pl = next; n             } n             else if (p[p[pl].l].num == a[i]) { n                 pl = p[pl].l; n             } n             else if (p[p[pl].l].num < a[i]) { n                 int next = ++sz; n                 p[next].num = a[i]; n                 p[next].r = pl; n                 p[next].l = p[pl].l; n                 p[p[next].l].r = next; n                 p[pl].l = next; n                 pl = next; n             } n             else { n                 printf( NO n ); n                 return; n             } n         } n     } n     printf( YES n ); n     return; n } n  n int main() { n     // ios::sync_with_stdio(false); n     // cin.tie(NULL), cout.tie(NULL); n     T = 1; n     ri(T); // delete if one test case n     for (int _ = 1; _ <= T; _++) { n         solve(_); n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   ll N, M; n   cin >> N >> M; n   if (N > M) swap(N, M); n   ll rem = 0; n   if (N == 1) { n     rem = min(M % 6, 6 - M % 6); n   } else { n     if (N % 2 == 1 && M % 2 == 1) { n       rem = 1; n     } else { n       if (M == 2) { n         rem = 4; n       } else if (M == 3 || M == 7) { n         rem = 2; n       } else { n         rem = 0; n       } n     } n   } n   cout << N * M - rem << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n constexpr int maxn = 2200; n LL a[maxn * 2], b[maxn * 2]; n using PII = pair<int, int>; n constexpr int inf = 1000000000; n struct SSPA { n   struct Edge { n     int u, v, f, c; n   }; n   int n, s, t; n   vector<Edge> edges; n   vector<vector<int>> adj; n   vector<int> h, d, p, done; n   SSPA(int n, int s, int t) : n(n), s(s), t(t) { n     adj.resize(n); n     h.resize(n); n     d.resize(n); n     p.resize(n); n     done.resize(n); n   } n   void add(int u, int v, int f, int c) { n     adj[u].push_back(edges.size()); n     edges.push_back({u, v, f, c}); n     adj[v].push_back(edges.size()); n     edges.push_back({v, u, 0, -c}); n   } n   bool dijkstra() { n     fill(d.begin(), d.end(), inf); n     fill(p.begin(), p.end(), -1); n     fill(done.begin(), done.end(), 0); n     priority_queue<PII, vector<PII>, greater<PII>> q; n     q.push({d[s] = 0, s}); n     while (not q.empty()) { n       int u = q.top().second; n       q.pop(); n       if (done[u]) continue; n       done[u] = 1; n       for (int i : adj[u]) { n         if (auto& e = edges[i]; e.f and d[e.v] > d[e.u] + e.c + h[u] - h[e.v]) { n           p[e.v] = i; n           q.push({d[e.v] = d[e.u] + e.c + h[u] - h[e.v], e.v}); n         } n       } n     } n     return ~p[t]; n   } n   int min_cost_flow() { n     int flow = 0, cost = 0; n     while (dijkstra()) { n       for (int i = 0; i < n; i += 1) h[i] += d[i]; n       int f = inf; n       for (int u = t; u != s; u = edges[p[u]].u) f = min(f, edges[p[u]].f); n       flow += f; n       cost += f * h[t]; n       for (int u = t; u != s; u = edges[p[u]].u) { n         edges[p[u]].f -= f; n         edges[p[u] ^ 1].f += f; n       } n     } n     return cost; n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int n; n   cin >> n; n   for (int i = 0; i < 2 * n; i += 1) cin >> a[i] >> b[i]; n   LL L = 0, R = 1000000000 + 1; n   vector<int> u; n   for (int i = 0; i < 2 * n; i += 1) u.push_back(i); n   sort(u.begin(), u.end(), [&](const int& x, const int& y) { n     if (b[x] != b[y]) return b[x] < b[y]; n     return a[x] > a[y]; n   }); n   while (L < R) { n     LL M = (L + R) >> 1; n     vector<LL> v, w; n     LL m = 0; n     for (int i : u) { n       m = max(m, a[i] * M + b[i]); n       if (i < n) n         v.push_back(m); n       else n         w.push_back(a[i] * M + b[i]); n     } n     int ok = 1; n     sort(w.begin(), w.end()); n     for (int i = 0; i < n; i += 1) ok &= v[i] >= w[i]; n     if (ok) n       R = M; n     else n       L = M + 1; n   } n   if (L == 1000000000 + 1) { n     cout <<  -1 ; n     return 0; n   } n   cout << L <<    ; n   int s = 0, t = 8 * n + 1; n   SSPA SSPA(t + 1, s, t); n   for (int i = 0; i < n; i += 1) SSPA.add(s, i + 1, 1, 0); n   for (int i = n; i < 2 * n; i += 1) SSPA.add(4 * n + i + 1, t, 1, 0); n   for (int i = 0; i < 2 * n; i += 1) { n     SSPA.add(2 * n + i + 1, i + 1, inf, 0); n     SSPA.add(i + 1, 2 * n + i + 1, inf, 1); n     SSPA.add(6 * n + i + 1, 4 * n + i + 1, inf, 0); n     SSPA.add(4 * n + i + 1, 6 * n + i + 1, inf, 1); n     SSPA.add(i + 1, 4 * n + i + 1, inf, 0); n   } n   for (int i = 1; i < 2 * n; i += 1) { n     SSPA.add(2 * n + u[i] + 1, 2 * n + u[i - 1] + 1, inf, 0); n     if (b[u[i]] == b[u[i - 1]]) n       SSPA.add(2 * n + u[i - 1] + 1, 2 * n + u[i] + 1, inf, 0); n   } n   sort(u.begin(), u.end(), [&](const int& x, const int& y) { n     return a[x] * L + b[x] < a[y] * L + b[y]; n   }); n   for (int i = 1; i < 2 * n; i += 1) { n     SSPA.add(6 * n + u[i] + 1, 6 * n + u[i - 1] + 1, inf, 0); n     if (a[u[i]] * L + b[u[i]] == a[u[i - 1]] * L + b[u[i - 1]]) n       SSPA.add(6 * n + u[i - 1] + 1, 6 * n + u[i] + 1, inf, 0); n   } n   cout << SSPA.min_cost_flow(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 100005; n const int MOD = 1000000007; n unordered_map<int, bool> sp; n vector<int> g[MAX]; n struct node { n   int u, v; n   int cost; n   node(int _u, int _v, int _cost) { n     u = _u; n     v = _v; n     cost = _cost; n   } n }; n bool mst_sort(node &a, node &b) { return a.cost < b.cost; } n bool ans_sort(node &a, node &b) { return a.cost > b.cost; } n vector<node> e; n int par[MAX]; n int findpar(int r) { n   if (par[r] == r) return r; n   par[r] = findpar(par[r]); n   return par[r]; n } n vector<node> tree; n void mst(int n) { n   for (int i = 0; i < n; i++) par[i] = i; n   sort(e.begin(), e.end(), mst_sort); n   int c = 0; n   for (int i = 0; i < e.size(); i++) { n     int u = findpar(e[i].u); n     int v = findpar(e[i].v); n     if (u != v) { n       par[u] = v; n       tree.push_back(e[i]); n       g[e[i].u].push_back(e[i].v); n       g[e[i].v].push_back(e[i].u); n       c++; n       if (c == n - 1) break; n     } n   } n   return; n } n int can[MAX]; n map<pair<int, int>, bool> vis; n bool dfs(int n, int par) { n   if (sp[n]) return can[n] = true; n   if (vis[pair<int, int>(n, par)]) return can[n] = false; n   if (can[n] != -1) return can[n]; n   bool ret = false; n   for (int i : g[n]) { n     if (i != par) ret |= dfs(i, n); n     if (ret) return can[n] = ret; n   } n   return can[n] = ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < k; i++) { n     int temp; n     cin >> temp; n     sp[temp] = true; n   } n   while (m--) { n     int u, v, cost; n     cin >> u >> v >> cost; n     e.push_back(node(u, v, cost)); n   } n   mst(n); n   sort(tree.begin(), tree.end(), ans_sort); n   int ans = 0; n   memset(can, -1, sizeof can); n   for (int i = 0; i < tree.size(); i++) { n     node p = tree[i]; n     if (sp[p.u] && sp[p.v]) { n       ans = p.cost; n       break; n     } n     if (sp[p.u]) { n       can[p.v] = -1; n       if (dfs(p.v, p.u)) { n         ans = p.cost; n         break; n       } n     } else if (sp[p.v]) { n       can[p.u] = -1; n       if (dfs(p.u, p.v)) { n         ans = p.cost; n         break; n       } n     } else { n       can[p.u] = can[p.v] = -1; n       if (dfs(p.u, p.v) && dfs(p.v, p.u)) { n         ans = p.cost; n         break; n       } n     } n     vis[pair<int, int>(p.u, p.v)] = true; n   } n   for (int i = 0; i < k; i++) cout << ans <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n const long long M = 1000005; n const long long mod = 1e9 + 7; n using namespace std; n long long n, m, a, b, fac[M], Ans; n inline long long Fpw(long long Base, long long k) { n   long long temp = 1; n   while (k) { n     if (k & 1) temp = (1LL * temp * Base) % mod; n     Base = (1LL * Base * Base) % mod; n     k >>= 1; n   } n   return temp; n } n inline long long Inv(long long a) { return Fpw(a, mod - 2); } n inline long long C(long long n, long long m) { n   if (n < 0 || m < 0 || n < m) return 0; n   return fac[n] * Inv(fac[m]) % mod * Inv(fac[n - m]) % mod; n } n int main() { n   scanf( %lld%lld , &n, &m); n   fac[0] = 1; n   for (long long i = 1; i <= 1000000; i++) n     fac[i] = (1LL * fac[i - 1] * i) % mod; n   for (long long i = 2; i <= n; i++) { n     long long temp = C(n - 2, i - 2) * fac[i - 2] % mod; n     temp = temp * C(m - 1, i - 2) % mod; n     if (n - i - 1 >= 0) temp = temp * i % mod * Fpw(n, n - i - 1) % mod; n     temp = temp * Fpw(m, n - i) % mod; n     Ans = (Ans + temp) % mod; n   } n   printf( %lld n , Ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100 + 10; n int n, m, k; n string l[maxn]; n int t[maxn], T; n int w[5][3]; n vector<pair<int, int> > v; n pair<pair<int, int>, pair<int, int> > a[maxn]; n int vis[24 * 30 * 60 + 10]; n int cor[24 * 30 * 60 + 10]; n int dp[maxn][24 * 30 * 60 + 10]; n int par[maxn][24 * 30 * 60 + 10]; n inline bool cmp(pair<pair<int, int>, pair<int, int> > fi, n                 pair<pair<int, int>, pair<int, int> > se) { n   return fi.first.second < se.first.second; n } n inline int solve(int work, int time) { n   time++; n   int &ret = dp[work][time]; n   if (ret > -1) return ret; n   if (work == 0) return ret = 0; n   int need = t[a[work].first.first]; n   if (need <= time) { n     int Max = solve(work - 1, min(time - 1 - need, a[work - 1].first.second)) + n               a[work].second.first; n     int Max_ = solve(work - 1, min(time - 1, a[work - 1].first.second)); n     if (Max > Max_) par[work][time] = 1; n     return ret = max(Max, Max_); n   } else n     return ret = solve(work - 1, min(time - 1, a[work - 1].first.second)); n } n inline void Out_(int Time) { n   int t = cor[Time]; n   cout << t / 1440 + 1 <<    ; n   int p, q; n   t %= 1440; n   p = t / 60; n   q = t % 60; n   if (p < 10) n     cout <<  0  << p <<  : ; n   else n     cout << p <<  : ; n   if (q < 10) n     cout <<  0  << q; n   else n     cout << q; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; ++i) cin >> l[i]; n   for (int i = 1; i <= n; ++i) cin >> t[i]; n   for (int i = 1; i <= 4; ++i) { n     int p, q; n     int p_, q_; n     char g, g_; n     cin >> p >> g >> q >> g_ >> p_ >> g >> q_; n     w[i][1] = p * 60 + q; n     w[i][2] = p_ * 60 + q_; n   } n   for (int i = 1; i <= k; ++i) n     for (int j = 1; j <= 4; ++j) n       v.push_back(pair<int, int>(24 * 60 * (i - 1) + w[j][1], n                                  24 * 60 * (i - 1) + w[j][2])); n   for (int i = 0; i < v.size(); ++i) n     for (int j = v[i].first; j <= v[i].second; ++j) vis[j] = 1; n   int cp = m; n   for (int i = 1, cur = 1; i <= m; ++i, cur++) { n     string hlp; n     cin >> hlp; n     for (int j = 1; j <= n; ++j) n       if (hlp == l[j]) a[i].first.first = j; n     int day; n     cin >> day; n     int p, q; n     char g; n     cin >> p >> g >> q; n     a[i].first.second = (day - 1) * 24 * 60 + p * 60 + q; n     cin >> a[i].second.first; n     a[i].second.second = cur; n     if (a[i].first.first == 0) { n       a[m] = a[i]; n       i--; n       m--; n     } n   } n   for (int i = 1; i <= m; ++i) { n     int q = 0; n     bool flag = false; n     for (int j = 0; j < v.size(); ++j) n       if (a[i].first.second >= v[j].first) n         if (a[i].first.second > v[j].second) n           q += v[j].second - v[j].first + 1; n         else { n           q += a[i].first.second - v[j].first + 1; n           flag = true; n         } n     a[i].first.second -= q; n     if (flag == false) a[i].first.second--; n   } n   sort(a + 1, a + 1 + m, cmp); n   int cur = 0; n   for (int i = 0; i < k * 24 * 60; ++i) n     if (vis[i] == 0) { n       cor[cur++] = i; n       T++; n     } n   T--; n   memset(dp, -1, sizeof dp); n   cout << solve(m, min(a[m].first.second, T)) << endl; n   vector<int> ans; n   int ind, time; n   time = min(a[m].first.second, T); n   ind = m; n   for (; ind > 0;) { n     time++; n     int need = t[a[ind].first.first]; n     int nex = par[ind][time]; n     if (nex == 1) { n       ans.push_back(ind); n       time = min(time - 1 - need, a[ind - 1].first.second); n       ind--; n     } else { n       time = min(time - 1, a[ind - 1].first.second); n       ind--; n     } n   } n   cout << ans.size() << endl; n   int Min = 0; n   for (int i = ans.size() - 1; i >= 0; --i) { n     cout << a[ans[i]].second.second <<    ; n     Out_(Min); n     cout <<    ; n     Out_(Min + t[a[ans[i]].first.first] - 1); n     cout << endl; n     Min += t[a[ans[i]].first.first]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n enum { MAXPOS = 110500, MAXBYTE = 20, MAXN = 7070 }; n int q_level[MAXN], q_left[MAXN], q_right[MAXN], q_value[MAXN]; n int jump_up[MAXBYTE][MAXPOS], jump_low[MAXBYTE][MAXPOS]; n int high[MAXPOS]; n void init() { n   for (int i = 0; (1 << i) < MAXPOS; i++) high[1 << i] = 1; n   for (int i = 2; i < MAXPOS; i++) { n     high[i] += high[i - 1]; n   } n   for (int i = 1; i < MAXPOS; i++) { n     jump_up[0][i] = i + high[i]; n     jump_low[0][i] = i + high[i] - !(i & (i - 1)); n   } n   for (int i = 1; i < MAXBYTE; i++) { n     for (int j = 1; j < MAXPOS; j++) { n       if (jump_up[i - 1][j] < MAXPOS) { n         jump_up[i][j] = jump_up[i - 1][jump_up[i - 1][j]]; n       } n       if (jump_low[i - 1][j] < MAXPOS) { n         jump_low[i][j] = jump_low[i - 1][jump_low[i - 1][j]]; n       } n     } n   } n } n inline int get_left(int start, int jumps) { n   if (jumps == 0) return start; n   for (int i = MAXBYTE - 1; i >= 0; i--) { n     if (jumps >= (1 << i)) { n       jumps -= (1 << i); n       start = jump_low[i][start]; n     } n   } n   return start; n } n inline int get_right(int start, int jumps) { n   if (jumps == 0) return start; n   for (int i = MAXBYTE - 1; i >= 0; i--) { n     if (jumps >= (1 << i)) { n       jumps -= (1 << i); n       start = jump_up[i][start]; n     } n   } n   return start; n } n int main() { n   init(); n   int n, m; n   scanf( %d %d , &n, &m); n   int q_cnt = 0; n   for (int i = 0; i < m; i++) { n     int type; n     scanf( %d , &type); n     if (type == 1) { n       scanf( %d %d %d %d , &q_level[q_cnt], &q_left[q_cnt], &q_right[q_cnt], n             &q_value[q_cnt]); n       q_cnt++; n     } else { n       int level, position; n       scanf( %d %d , &level, &position); n       set<int> subtree; n       for (int j = 0; j < q_cnt; j++) { n         if (q_level[j] < level) continue; n         int left = get_left(position, q_level[j] - level); n         int right = get_right(position, q_level[j] - level); n         if (max(left, q_left[j]) <= min(right, q_right[j])) n           subtree.insert(q_value[j]); n       } n       printf( %d n , (int)subtree.size()); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[107]; n stack<int> Q[100]; n int cmp(int a, int b) { return a > b; } n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   sort(a + 1, a + 1 + n, cmp); n   int ans = 0; n   int tmp = -1; n   int flag = 1; n   for (int i = 1; i <= n; i++) { n     if (a[i] != k) { n       flag = 0; n       break; n     } n   } n   if (flag) { n     printf( 0 n ); n     return 0; n   } n   while (1) { n     tmp = -1; n     ans++; n     for (int i = 1; i <= n; i++) { n       if (a[i] == k) n         continue; n       else if (tmp == a[i]) n         continue; n       else { n         tmp = a[i]; n         a[i]++; n       } n     } n     int flag = 1; n     for (int i = 1; i <= n; i++) { n       if (a[i] != k) { n         flag = 0; n         break; n       } n     } n     if (flag) break; n   } n   printf( %d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool gt(long double a, long double b) { return a > b + 1e-12; } n int n, m; n vector<pair<bool, pair<int, int> > > pt, org; n pair<int, int> o; n inline bool left_turn(const pair<int, int> &a, const pair<int, int> &b, n                       const pair<int, int> &c) { n   return 1LL * (b.first - a.first) * (c.second - b.second) - n              1LL * (b.second - a.second) * (c.first - b.first) > n          0; n } n inline bool cmp(pair<bool, pair<int, int> > a, pair<bool, pair<int, int> > b) { n   return left_turn(o, a.second, b.second); n } n inline bool by_Y(pair<bool, pair<int, int> > a, pair<bool, pair<int, int> > b) { n   return a.second.second < b.second.second; n } n int main() { n   ios::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   org.resize(n + m); n   for (register int i = (0); i < (int)(n); ++i) n     cin >> org[i].second.first >> org[i].second.second, org[i].first = 0; n   for (register int i = (0); i < (int)(m); ++i) n     cin >> org[i + n].second.first >> org[i + n].second.second, n         org[i + n].first = 1; n   int tot = 0; n   sort((org).begin(), (org).end(), by_Y); n   for (register int rep = (0); rep < (int)(((int)(org).size())); ++rep) n     if (org[rep].first == 0) { n       pt.clear(); n       for (register int i = (0); i < (int)(((int)(org).size())); ++i) n         if (org[rep].second <= org[i].second && i != rep) pt.push_back(org[i]); n       o = org[rep].second; n       sort((pt).begin(), (pt).end(), cmp); n       for (register int oth = (0); oth < (int)(((int)(pt).size())); ++oth) n         if (pt[oth].first == 0) { n           pair<int, int> worst; n           bool wfound = false; n           for (register int i = (1); i < (int)(((int)(pt).size())); ++i) { n             pair<bool, pair<int, int> > next = n                 pt[(oth + i) % ((int)(pt).size())]; n             if (!left_turn(o, pt[oth].second, next.second)) break; n             if (next.first == 1 && n                 (wfound == false || n                  left_turn(pt[oth].second, worst, next.second))) n               worst = next.second, wfound = true; n             if (next.first == 0 && n                 (wfound == false || n                  left_turn(pt[oth].second, worst, next.second))) n               tot++; n           } n         } n     } n   cout << tot << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int res = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -f; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     res = res * 10 + ch -  0 ; n     ch = getchar(); n   } n   return res * f; n } n namespace qiqi { n const int N = 3005; n int n, m, ecnt, head[N], a[N], siz[N]; n pair<long long, long long> f[N][N], g[N]; n struct Edge { n   int v, nxt; n } e[N << 1]; n inline void add(int u, int v) { n   e[++ecnt] = (Edge){v, head[u]}; n   head[u] = ecnt; n } n void dfs(int u, int fa) { n   f[u][siz[u] = 1] = make_pair(0, a[u]); n   for (int k = head[u], v = e[k].v; k; v = e[k = e[k].nxt].v) n     if (v != fa) { n       dfs(v, u); n       for (int i = (1); i <= (siz[u] + siz[v]); ++i) g[i] = make_pair(-1, 0); n       for (int i = (siz[u]); i >= (1); --i) n         for (int j = (siz[v]); j >= (1); --j) { n           g[i + j] = max(g[i + j], make_pair(f[u][i].first + f[v][j].first + n                                                  (f[v][j].second > 0), n                                              f[u][i].second)); n           g[i + j - 1] = n               max(g[i + j - 1], make_pair(f[u][i].first + f[v][j].first, n                                           f[u][i].second + f[v][j].second)); n         } n       siz[u] += siz[v]; n       for (int i = (1); i <= (siz[u]); ++i) f[u][i] = g[i]; n     } n } n void main() { n   memset(head, ecnt = 0, sizeof(head)); n   n = read(); n   m = read(); n   for (int i = (1); i <= (n); ++i) a[i] = read(); n   for (int i = (1); i <= (n); ++i) a[i] = read() - a[i]; n   int u, v; n   for (int i = (2); i <= (n); ++i) { n     u = read(); n     v = read(); n     add(u, v); n     add(v, u); n   } n   dfs(1, 0); n   printf( %lld n , f[1][m].first + (f[1][m].second > 0)); n } n }  // namespace qiqi n int main() { n   int T = read(); n   while (T--) qiqi::main(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int sum(long long int n) { n   long long int s2um = 0; n   while (n > 0) { n     s2um += n % 10; n     n = n / 10; n   } n   return s2um; n } n long long int answer(long long int n) { n   long long int x = 0, y = 0; n   while (10 * y + 9 < n) { n     y = 10 * y + 9; n     x = x + 9; n   } n   long long int m = n - y; n   x += sum(m); n   return x; n } n int main() { n   ios_base::sync_with_stdio(false); n   std::cin.tie(NULL); n   std::cout.tie(NULL); n   long long int n; n   cin >> n; n   cout << answer(n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200100; n int n, l, r, m, a[N], b[N], c, pos, mi, ma; n pair<int, int> t[4 * N]; n void build(int v, int tl, int tr) { n   if (tl == tr) { n     t[v].first = a[tl]; n     t[v].second = b[tl]; n     return; n   } n   int tm = (tl + tr) / 2; n   build(2 * v, tl, tm); n   build(2 * v + 1, tm + 1, tr); n   t[v].first = max(t[2 * v].first, t[2 * v + 1].first); n   t[v].second = min(t[2 * v].second, t[2 * v + 1].second); n   return; n } n void get(int v, int tl, int tr, int l, int r) { n   if (l > r) return; n   if (tl == l && tr == r) { n     ma = max(ma, t[v].first); n     mi = min(mi, t[v].second); n     return; n   } n   int tm = (tl + tr) / 2; n   get(2 * v, tl, tm, l, min(r, tm)); n   get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r); n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   a[n + 1] = 1e9 - 2; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   b[n + 1] = -1e9 - 2; n   build(1, 1, n); n   long long ans = 0; n   for (int i = 1; i <= n; i++) n     if (a[i] - b[i] < 1) { n       l = i; n       r = n + 1; n       while (l < r) { n         c = (l + r) / 2; n         mi = 1e9 + 2; n         ma = -1e9 - 2; n         get(1, 1, n, i, c); n         if (mi > ma) n           l = c + 1; n         else n           r = c; n       } n       pos = l; n       l = i; n       r = n + 1; n       while (l < r) { n         c = (l + r) / 2; n         mi = 1e9 + 2; n         ma = -1e9 - 2; n         get(1, 1, n, i, c); n         if (mi >= ma) n           l = c + 1; n         else n           r = c; n       } n       ans += max(l - pos, 0); n     } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int x, long long int y, long long int p) { n   long long int res = 1; n   x = x % p; n   if (x == 0) return 0; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n int32_t main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   long long int n, x, y, sum = 0; n   cin >> n; n   long long int a[n + 1][n + 1]; n   for (long long int i = 1; i <= n; i++) n     for (long long int j = 1; j <= n; j++) { n       cin >> a[i][j]; n       if (a[i][j] == 0) { n         y = j; n         x = i; n       } n     } n   set<long long int> s; n   if (n == 1) { n     cout <<  1 ; n     exit(0); n   } n   if (x != y) { n     for (long long int i = 1; i <= n; i++) sum += a[i][i]; n     s.insert(sum); n   } n   if (x != n - y + 1) { n     sum = 0; n     for (long long int i = 1; i <= n; i++) sum += a[i][n - i + 1]; n     s.insert(sum); n   } n   for (long long int i = 1; i <= n; i++) { n     if (i == x) continue; n     sum = 0; n     for (long long int j = 1; j <= n; j++) sum += a[i][j]; n     s.insert(sum); n   } n   for (long long int j = 1; j <= n; j++) { n     if (j == y) continue; n     sum = 0; n     for (long long int i = 1; i <= n; i++) sum += a[i][j]; n     s.insert(sum); n   } n   long long int val = *(s.begin()); n   sum = 0; n   for (long long int i = 1; i <= n; i++) sum += a[i][y]; n   long long int t = val - sum; n   if ((long long int)s.size() > 1 || t <= 0) { n     cout <<  -1 ; n     exit(0); n   } n   a[x][y] = t; n   sum = 0; n   long long int sum1 = 0, sum2 = 0; n   for (long long int i = 1; i <= n; i++) sum += a[x][i]; n   for (long long int i = 1; i <= n; i++) sum1 += a[i][i]; n   for (long long int i = 1; i <= n; i++) sum2 += a[i][n - i + 1]; n   if (sum != val || sum1 != val || sum2 != val) { n     cout <<  -1 ; n     exit(0); n   } n   cout << t; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const long long Inf = 1e18; n const int N = 2e6 + 10; n const int mod = 1e9 + 7; n int gi() { n   int x = 0, o = 1; n   char ch = getchar(); n   while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) o = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x * o; n } n template <typename T> n bool chkmax(T &a, T b) { n   return a < b ? a = b, 1 : 0; n }; n template <typename T> n bool chkmin(T &a, T b) { n   return a > b ? a = b, 1 : 0; n }; n int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; } n int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; } n void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); } n void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); } n int n, p[N], a[N], pw[N], cnt[N]; n int pri[N], tot = 0, d[N]; n bool vis[N]; n void init() { n   const int n = 2e6; n   for (int i = 2; i <= n; i++) { n     if (!vis[i]) pri[++tot] = i, d[i] = i; n     for (int j = 1; j <= tot && i * pri[j] <= n; j++) { n       vis[i * pri[j]] = 1; n       d[i * pri[j]] = pri[j]; n       if (i % pri[j] == 0) break; n     } n   } n } n int qpow(int a, int b) { n   int ret = 1; n   while (b) { n     if (b & 1) ret = 1ll * ret * a % mod; n     a = 1ll * a * a % mod; n     b >>= 1; n   } n   return ret; n } n int main() { n   init(); n   cin >> n; n   for (int i = 1; i <= n; i++) p[i] = gi(); n   sort(p + 1, p + n + 1); n   for (int i = n; i; i--) { n     if (!pw[p[i]]) n       pw[p[i]] = cnt[p[i]] = 1, a[i] = p[i]; n     else { n       int x = p[i] - 1; n       a[i] = p[i] - 1; n       while (x > 1) { n         int y = d[x], sum = 0; n         while (x % y == 0) x /= y, sum++; n         if (pw[y] < sum) n           pw[y] = sum, cnt[y] = 1; n         else if (pw[y] == sum) n           cnt[y]++; n       } n     } n   } n   int tag = 0; n   for (int i = n; i; i--) { n     int x = a[i], fl = 1; n     while (x > 1) { n       int y = d[x], sum = 0; n       while (x % y == 0) x /= y, sum++; n       if (sum == pw[y] && cnt[y] == 1) fl = 0; n     } n     tag |= fl; n   } n   int ans = 1; n   for (int i = 2; i <= 2000000; i++) ans = 1ll * ans * qpow(i, pw[i]) % mod; n   printf( %d n , add(ans, tag)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXARR = 2e5 + 5, MAXVAL = 1e6 + 5, MOD = 1e9 + 7, INF = 2147483647; n const long long INFLL = (1LL << 62); n int n, d, m; n int last[MAXARR]; n pair<long long, long long> stop[MAXARR]; n stack<pair<long long, long long> > st; n long long ans = 0LL; n template <class T> n void maximize(T &x, T y) { n   if (x < y) x = y; n } n template <class T> n void minimize(T &x, T y) { n   if (x > y) x = y; n } n bool get_bit(int i, int k) { return ((k >> (i - 1)) & 1); } n int change_bit(int i, int k) { return (k ^ (1 << (i - 1))); } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> d >> n >> m; n   for (int i = 1; i <= m; i++) { n     cin >> stop[i].first >> stop[i].second; n   } n   stop[++m] = pair<long long, long long>(0, 0); n   stop[++m] = pair<long long, long long>(d, 0); n   sort(stop + 1, stop + m + 1); n   for (int i = 1; i <= m; i++) { n     last[i] = INF; n   } n   for (int i = 1; i <= m; i++) { n     while (!st.empty() && st.top().first > stop[i].second) { n       last[st.top().second] = i; n       st.pop(); n     } n     st.push(pair<long long, long long>(stop[i].second, i)); n   } n   long long now = n; n   for (int i = 2; i <= m; i++) { n     now -= stop[i].first - stop[i - 1].first; n     if (now < 0) { n       cout <<  -1 ; n       exit(0); n     } n     if (last[i] != INF && stop[last[i]].first - stop[i].first <= n) { n       ans += 1LL * max(0LL, (stop[last[i]].first - stop[i].first - now)) * n              stop[i].second; n       now = max(now, stop[last[i]].first - stop[i].first); n     } else if (n + stop[i].first < d) { n       ans += 1LL * (n - now) * stop[i].second; n       now = n; n     } else { n       ans += 1LL * max(0LL, d - stop[i].first - now) * stop[i].second; n       now = max(now, d - stop[i].first); n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   std::ios_base::sync_with_stdio(0), cin.tie(0); n   string s, t; n   cin >> s >> t; n   int a[26] = {}; n   int n = s.size(), m = t.size(); n   for (int i = 0; i < n; ++i) a[s[i] -  a ]++; n   bool ok = 0; n   if (n < m) { n     s =   ; n     for (int i = 0; i < n && !ok; ++i) { n       if (a[t[i] -  a ]) { n         a[t[i] -  a ]--; n         s += t[i]; n         continue; n       } else { n         for (int j = t[i] -  a  + 1; j < 26; ++j) { n           if (a[j]) { n             a[j]--; n             s += char( a  + j); n             ok = 1; n             break; n           } n         } n         if (ok) break; n         for (int j = i - 1; j >= 0 && !ok; --j) { n           a[s[j] -  a ]++; n           s.pop_back(); n           for (int k = t[j] -  a  + 1; k < 26; ++k) { n             if (a[k]) { n               a[k]--; n               s += char(k +  a ); n               ok = 1; n               break; n             } n           } n         } n         if (!ok) { n           cout << -1; n           return 0; n         } n       } n     } n     if (!ok) { n       cout << -1; n       return 0; n     } n     for (int i = 0; i < 26; ++i) { n       for (int j = 0; j < a[i]; ++j) { n         s += char( a  + i); n       } n     } n     cout << s; n     return 0; n   } n   s =   ; n   for (int i = 0; i < m && !ok; ++i) { n     if (a[t[i] -  a ]) { n       a[t[i] -  a ]--; n       s += t[i]; n       continue; n     } else { n       for (int j = t[i] -  a  + 1; j < 26; ++j) { n         if (a[j]) { n           a[j]--; n           s += char( a  + j); n           ok = 1; n           break; n         } n       } n       if (ok) break; n       for (int j = i - 1; j >= 0 && !ok; --j) { n         a[s[j] -  a ]++; n         s.pop_back(); n         for (int k = t[j] -  a  + 1; k < 26; ++k) { n           if (a[k]) { n             a[k]--; n             s += char(k +  a ); n             ok = 1; n             break; n           } n         } n       } n       if (!ok) { n         cout << -1; n         return 0; n       } n     } n   } n   if (!ok) { n     if (n == m) { n       if (next_permutation(s.begin(), s.end())) n         cout << s; n       else n         cout << -1; n       return 0; n     } n   } n   for (int i = 0; i < 26; ++i) { n     for (int j = 0; j < a[i]; ++j) { n       s += char( a  + i); n     } n   } n   cout << s; n   return 0; n } n


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


#include <bits/stdc++.h> n const int N = 100005; n long long num1[N], num2[N], num3[N]; n void init() { n   memset(num1, 0, sizeof(num1)); n   memset(num2, 0, sizeof(num2)); n   memset(num3, 0, sizeof(num3)); n   for (int i = 1; i < N; ++i) { n     for (int j = i; j < N; j += i) { n       num1[j] += 1; n     } n   } n   num2[0] = 0; n   for (int i = 1; i < N; ++i) { n     num2[i] = num2[i - 1] + i; n   } n   num3[0] = 0; n   for (int i = 1; i < N; ++i) { n     num3[i] = num3[i - 1] + num2[i]; n   } n } n int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); } n int main() { n   init(); n   int t; n   scanf( %d , &t); n   while (t--) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     int gcd_3 = gcd(x, gcd(y, z)); n     int gcd_xy = gcd(x, y); n     int gcd_yz = gcd(y, z); n     int gcd_xz = gcd(x, z); n     long long g_3 = num1[gcd_3]; n     long long g_xy = num1[gcd_xy] - num1[gcd_3]; n     long long g_xz = num1[gcd_xz] - num1[gcd_3]; n     long long g_yz = num1[gcd_yz] - num1[gcd_3]; n     long long g_x = num1[x] + num1[gcd_3] - num1[gcd_xy] - num1[gcd_xz]; n     long long g_y = num1[y] + num1[gcd_3] - num1[gcd_xy] - num1[gcd_yz]; n     long long g_z = num1[z] + num1[gcd_3] - num1[gcd_xz] - num1[gcd_yz]; n     long long cnt = 0; n     cnt += num3[g_3]; n     cnt += num2[g_3] * (g_xy + g_yz + g_xz + g_x + g_y + g_z); n     cnt += g_3 * (g_x * g_y + g_y * g_z + g_x * g_z + num2[g_xy] + num2[g_xz] + n                   num2[g_yz]); n     cnt += g_3 * ((g_xy + g_yz + g_xz) * (g_x + g_y + g_z)); n     cnt += g_3 * (g_xy * g_xz + g_xz * g_yz + g_xy * g_yz); n     cnt += num2[g_xy] * (g_z + g_xz + g_yz) + num2[g_yz] * (g_x + g_xy + g_xz) + n            num2[g_xz] * (g_y + g_xy + g_yz); n     cnt += (g_x + g_y + g_z) * (g_xy * g_yz + g_xz * g_yz + g_xy * g_xz); n     cnt += (g_x * g_y) * (g_xz + g_yz) + (g_y * g_z) * (g_xy + g_xz) + n            (g_x * g_z) * (g_xy + g_yz); n     cnt += g_x * g_y * g_z; n     cnt += g_xy * g_yz * g_xz; n     printf( %d n , (int)cnt); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<pair<int, int>, int>> ordered; n deque<pair<int, int>> solutions; n vector<bool> stay, check; n bool cmp(pair<pair<int, int>, int> &a, pair<pair<int, int>, int> &b) { n   int A = a.first.second - a.first.first; n   int B = b.first.second - b.first.first; n   return A < B; n } n vector<pair<pair<int, int>, int>> new_array( n     vector<pair<pair<int, int>, int>> &v, int value) { n   vector<pair<pair<int, int>, int>> temp; n   for (int i = 0; i < v.size(); i++) { n     if (stay[i]) n       temp.push_back(make_pair( n           make_pair(v[i].first.first + value, v[i].first.second), v[i].second)); n   } n   return temp; n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     int aux; n     cin >> aux; n     stay.push_back(false); n     check.push_back(false); n     if (i < k) n       ordered.push_back(make_pair(make_pair(1, aux), i)); n     else n       solutions.push_back(make_pair(aux, i)); n   } n   sort(ordered.begin(), ordered.end(), cmp); n   int d = 0, ans = 0, m = 0; n   while (ordered.size() != 0) { n     int mini = ordered[0].first.second - ordered[0].first.first + 1; n     int index = 0, out = 0, change = 0, intern_ans = 0; n     for (auto &x : ordered) { n       int L = x.first.first; n       int R = L + mini - 1; n       int id = x.second; n       if (L <= d && d <= R && check[id] == false) { n         ans++; n         intern_ans++; n         check[id] = true; n       } n       int range = x.first.second - x.first.first + 1; n       if (range == mini) { n         stay[index++] = false; n         m++; n         out++; n         change = true; n       } else n         stay[index++] = true; n     } n     ordered = new_array(ordered, mini); n     double temp = 100.00 * ((double)m / n) + 0.5; n     d = (int)temp; n     while (solutions.size() != 0 && out > 0) { n       ordered.push_back( n           make_pair(make_pair(1, solutions[0].first), solutions[0].second)); n       solutions.pop_front(); n       out--; n     } n     if (change) sort(ordered.begin(), ordered.end(), cmp); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long funk(long long x, long long y) { n   bool used[10000]; n   long long res = 1; n   for (long long i = 1; i <= 100; i++) used[i] = 0; n   for (long long i = x + 1; i <= y; i++) { n     res *= i; n     for (long long j = 2; j <= y - x; j++) { n       if (!used[j] && res % j == 0) { n         res /= j; n         used[j] = 1; n       } n     } n   } n   return res; n } n int main() { n   long long n, m, t, i, j; n   cin >> n >> m >> t; n   long long s = 0; n   for (i = 4; i < t && i <= n; i++) { n     if (t - i <= m) { n       s += (funk(i, n) * funk(t - i, m)); n     } n   } n   cout << s <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int par[200009]; n int flag, sum, ans, cnt, mini, maxi; n int main() { n   int n; n   scanf( %d , &n); n   int a[n]; n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   sort(a, a + n); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (i == j) continue; n       par[a[i] + a[j]]++; n       ans = max(par[a[i] + a[j]], ans); n     } n   } n   cout << ans / 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 12; n const int Maxm = 105; n int n, m, k; n string s; n int a[Maxn][Maxm], b[Maxn][Maxm], c[Maxn][Maxm]; n int res; n int main() { n   scanf( %d %d %d , &n, &m, &k); n   getline(cin, s); n   for (int i = 0; i < n; i++) { n     getline(cin, s); n     for (int j = 0; j < m; j++) scanf( %d %d %d , &a[i][j], &b[i][j], &c[i][j]); n     getline(cin, s); n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (i != j) { n         int cand = 0; n         priority_queue<int> Q; n         for (int l = 0; l < m; l++) n           for (int k = 0; k < c[i][l]; k++) Q.push(b[j][l] - a[i][l]); n         for (int l = 0; l < k && !Q.empty() && Q.top() > 0; l++) { n           cand += Q.top(); n           Q.pop(); n         } n         res = max(res, cand); n       } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) { n   return a.first < b.first; n } n int main() { n   int n, k; n   cin >> n >> k; n   vector<pair<int, pair<int, int> > > book; n   for (int i = 0; i < n; i++) { n     pair<int, pair<int, int> > temp; n     cin >> temp.first >> temp.second.first >> temp.second.second; n     book.push_back(temp); n   } n   stack<int> alice, bob, both; n   sort(book.begin(), book.end(), comp); n   for (int i = 0; i < n; i++) { n     int t = book[i].first; n     bool a = book[i].second.first; n     bool b = book[i].second.second; n     if (a && b) { n       int t1 = 0, t2 = 0; n       if (!alice.empty()) { n         t1 = alice.top(); n       } n       if (!bob.empty()) { n         t2 = bob.top(); n       } n       if (t1 + t2 >= t) { n         both.push(t); n         if (alice.size() + both.size() > k) { n           alice.pop(); n         } n         if (bob.size() + both.size() > k) { n           bob.pop(); n         } n       } else { n         if (alice.size() + bob.size() + 2 * both.size() < 2 * k) { n           both.push(t); n         } n         if (alice.size() + both.size() > k) { n           alice.pop(); n         } n         if (bob.size() + both.size() > k) { n           bob.pop(); n         } n       } n     } else if (a) { n       if (alice.size() + both.size() < k) { n         alice.push(t); n       } n     } else if (b) { n       if (bob.size() + both.size() < k) { n         bob.push(t); n       } n     } n   } n   if (alice.size() + bob.size() + 2 * both.size() < 2 * k) { n     cout << -1 << endl; n   } else { n     int ans = 0; n     while (!alice.empty()) { n       ans += alice.top(); n       alice.pop(); n     } n     while (!bob.empty()) { n       ans += bob.top(); n       bob.pop(); n     } n     while (!both.empty()) { n       ans += both.top(); n       both.pop(); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void _print(int t) { cerr << t; } n void _print(string t) { cerr << t; } n void _print(char t) { cerr << t; } n void _print(long double t) { cerr << t; } n void _print(double t) { cerr << t; } n void _print(unsigned long long t) { cerr << t; } n template <class T, class V> n void _print(pair<T, V> p); n template <class T> n void _print(vector<T> v); n template <class T> n void _print(set<T> v); n template <class T, class V> n void _print(map<T, V> v); n template <class T> n void _print(multiset<T> v); n template <class T, class V> n void _print(pair<T, V> p) { n   cerr <<  { ; n   _print(p.first); n   cerr <<  , ; n   _print(p.second); n   cerr <<  } ; n } n template <class T> n void _print(vector<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(set<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(multiset<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T, class V> n void _print(map<T, V> v) { n   cerr <<  [  ; n   for (auto i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n void init_code() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n void solve() { n   string a; n   string b; n   cin >> a >> b; n   string ans; n   int n = ((int)(a).size()); n   int n1 = ((int)(b).size()); n   int j = n1 - 1; n   int flag = 0; n   int i = n - 1; n   while (i >= 0) { n     if (a[i] == b[j]) { n       i--; n       j--; n     } else { n       i -= 2; n     } n     if (j == -1) { n       flag = 1; n       break; n     } n   } n   if (flag) n     cout <<  YES ; n   else n     cout <<  NO ; n   cout <<   n ; n } n signed main() { n   init_code(); n   long long t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, x_num; n   x_num = 0; n   std::string x; n   std::cin >> n; n   for (int i = 0; i < n; i++) { n     std::cin >> x; n     if (x.substr(0, 2) ==  --  || x.substr(1, 2) ==  -- ) n       x_num--; n     else n       x_num++; n   } n   std::cout << x_num; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long const mod = 998244353; n long long const inf = 1e15; n long long n, m, k, x; n long long w[200004], ans[200004]; n pair<long long, long long> t[800003]; n void build(long long nd, long long l, long long r) { n   if (l == r) { n     t[nd] = {w[l], l}; n     return; n   } n   long long mid = (l + r) / 2; n   build(2 * nd, l, mid); n   build(2 * nd + 1, mid + 1, r); n   if (t[2 * nd].first <= t[2 * nd + 1].first) n     t[nd] = t[2 * nd]; n   else n     t[nd] = t[2 * nd + 1]; n } n void up(long long nd, long long l, long long r, long long a, long long b) { n   if (a > r || a < l) return; n   if (l == r) { n     t[nd] = {b, a}; n     return; n   } n   long long mid = (l + r) / 2; n   up(2 * nd, l, mid, a, b); n   up(2 * nd + 1, mid + 1, r, a, b); n   if (t[2 * nd].first <= t[2 * nd + 1].first) n     t[nd] = t[2 * nd]; n   else n     t[nd] = t[2 * nd + 1]; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k >> x; n   long long d1 = 0, d2 = 0; n   long long mn = 0, mx = n + 1; n   w[0] = inf; n   w[n + 1] = -inf; n   long long v = -1; n   for (int i = 1; i <= n; i++) { n     cin >> w[i]; n     if (w[i] == 0) { n       if (v == -1) n         v = i; n       else { n         if (k) { n           w[i] -= x; n           k--; n         } n       } n     } n     if (w[i] > 0) { n       d1++; n       if (w[i] < w[mn]) mn = i; n     } else if (w[i] < 0) { n       d2++; n       if (w[i] > w[mx]) mx = i; n     } n   } n   if (v != -1 && k) { n     if (d2 % 2) n       w[v] = x; n     else n       w[v] = -x, d2++; n     k--; n   } n   if (d2 % 2 == 0) { n     long long dd1, dd2; n     dd1 = dd2 = 1e9; n     if (d1) dd1 = w[mn] / x + 1; n     if (d2) dd2 = (abs(w[mx])) / x + 1; n     if ((dd1 == dd2 && (x - w[mn] % x) >= (x - (abs(w[mx])) % x)) || n         (dd1 < dd2)) { n       w[mn] -= min(dd1, k) * x; n       k -= min(dd1, k); n     } else { n       w[mx] += min(dd2, k) * x; n       k -= min(dd2, k); n     } n   } n   for (int i = 1; i <= n; i++) ans[i] = w[i], w[i] = abs(w[i]); n   build(1, 1, n); n   for (int ff = 1; ff <= k; ff++) { n     pair<long long, long long> d = t[1]; n     if (ans[d.second] > 0) n       ans[d.second] += x; n     else n       ans[d.second] -= x; n     up(1, 1, n, d.second, abs(ans[d.second])); n   } n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int LimitLength = 1e5 + 10; n const int oo = 1e9 + 7; n const int Shift =  a ; n vector<vector<int> > F; n int Next[LimitLength][27]; n int nS, nT; n char S[2 * LimitLength], T[LimitLength]; n int KMP[2 * LimitLength]; n int main() { n   cin >> S + 1 >> T + 1; n   nS = strlen(S + 1); n   nT = strlen(T + 1); n   KMP[0] = -1; n   for (int iT = 1; iT <= nT; iT++) { n     for (int k = KMP[iT - 1]; k != -1; k = KMP[k]) { n       if (T[k + 1] == T[iT]) { n         KMP[iT] = k + 1; n         break; n       } n     } n   } n   memset(Next, 0xff, sizeof(Next)); n   for (int Char = 0; Char < 26; Char++) Next[0][Char] = 0; n   for (int iT = 1; iT <= nT; iT++) { n     Next[iT - 1][T[iT] - Shift] = iT; n   } n   for (int iT = 1; iT <= nT; iT++) { n     for (int Char = 0; Char < 26; Char++) { n       if (Next[iT][Char] == -1) Next[iT][Char] = Next[KMP[iT]][Char]; n     } n   } n   F.resize(nS + 2); n   for (int iS = 0; iS <= nS + 1; iS++) { n     F[iS].resize(nT + 2); n   } n   for (int iS = 0; iS <= nS; iS++) { n     for (int iT = 0; iT <= nT; iT++) { n       F[iS][iT] = -oo; n     } n   } n   F[0][0] = 0; n   int res = 0; n   for (int iS = 0; iS < nS; iS++) { n     for (int preT = 0; preT <= nT; preT++) { n       if (S[iS + 1] ==  ? ) { n         for (int Char = 0; Char < 26; Char++) { n           if (Next[preT][Char] != -1) { n             F[iS + 1][Next[preT][Char]] = n                 max(F[iS + 1][Next[preT][Char]], n                     F[iS][preT] + (Next[preT][Char] == nT)); n           } n         } n       } else { n         int Char = S[iS + 1] - Shift; n         if (Next[preT][Char] != -1) { n           F[iS + 1][Next[preT][Char]] = n               max(F[iS + 1][Next[preT][Char]], n                   F[iS][preT] + (Next[preT][Char] == nT)); n         } n       } n     } n   } n   for (int iS = 0; iS <= nS; iS++) { n     for (int preT = 0; preT <= nT; preT++) { n       res = max(res, F[iS][preT]); n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100010; n const unsigned Upper = (1LL << 32) - 1; n unsigned a[maxn], b[maxn]; n int main() { n   int n, K; n   int i, j; n   unsigned s, a0, a1, a2, a3; n   scanf( %d%d , &n, &K); n   for (i = 0; i < n; ++i) { n     scanf( %u.%u.%u.%u , &a0, &a1, &a2, &a3); n     a[i] = (a0 << 24) + (a1 << 16) + (a2 << 8) + a3; n   } n   sort(a, a + n); n   for (j = 31; j; --j) { n     s = Upper >> j << j; n     for (i = 0; i < n; ++i) b[i] = a[i] & s; n     if (unique(b, b + n) - b == K) { n       printf( %u.%u.%u.%u n , (s >> 24) & 255, (s >> 16) & 255, (s >> 8) & 255, n              s & 255); n       return 0; n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long int n, m; n   cin >> n >> m; n   vector<long long int> dp(n + 1); n   vector<pair<pair<long long int, long long int>, n               pair<long long int, long long int>>> n       input; n   for (long long int i = 1; i <= m; i++) { n     long long int s, d, c; n     cin >> s >> d >> c; n     input.push_back({{d, c}, {s, i}}); n     dp[d] = m + 1; n   } n   sort(input.begin(), input.end()); n   for (int i = 1; i <= n; i++) { n     int flag = 0; n     if (dp[i] == m + 1) continue; n     for (int j = 0; j < m; j++) { n       if (input[j].second.first <= i && input[j].first.second > 0 && n           input[j].first.first > i) { n         dp[i] = input[j].second.second; n         input[j].first.second--; n         flag = 1; n         break; n       } n     } n     if (!flag) dp[i] = 0; n   } n   int flag = 1; n   for (int i = 0; i < m; i++) { n     if (input[i].first.second != 0) flag = 0; n   } n   if (!flag) n     cout << -1; n   else n     for (int i = 1; i <= n; i++) cout << dp[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int x, y, mn, mx, ai; n int an[200], ax[200]; n int main() { n   cin >> x >> y; n   for (int i = 1; i <= x; i++) { n     cin >> ai; n     an[i] = ax[i] = ai; n   } n   for (int i = 1; i <= y; i++) { n     sort(an + 1, an + 1 + x); n     an[1]++; n   } n   sort(ax + 1, ax + 1 + x); n   ax[x] += y; n   sort(an + 1, an + 1 + x); n   mn = an[x]; n   mx = ax[x]; n   cout << mn <<     << mx; 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 n; n     cin >> n; n     vector<int> a(n), b; n     for (int i = 0; i < n; i++) cin >> a[i]; n     b = a; n     sort(b.begin(), b.end()); n     int k = 0, set1 = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] != b[i] && k == 0) { n         k++; n         set1++; n       } else if (a[i] == b[i] && k == 1) { n         k++; n       } else if (a[i] != b[i] && k == 2) { n         set1++; n         break; n       } n     } n     cout << set1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long mod = 1000000007; n const long long Inf = 2 * 1e9; n const long long p = 31; n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n int n, k; n vector<int> q; n vector<vector<int>> a; n vector<int> ans, used, used2; n void dfs(int x) { n   if (used2[x]) return; n   if (used[x]) { n     cout << -1; n     exit(0); n   } n   used[x] = true; n   if (a[x].size() == 0) { n     used2[x] = true; n     ans.push_back(x); n     return; n   } n   for (int(i) = 0; (i) < a[x].size(); (i)++) { n     int to = a[x][i]; n     dfs(to); n   } n   ans.push_back(x); n   used2[x] = true; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> k; n   q.resize(k + 1); n   used.resize(n + 1, 0); n   used2.resize(n + 1, 0); n   a.resize(n + 1); n   for (int(i) = 0; (i) < k; (i)++) cin >> q[i]; n   for (int(i) = 1; (i) < n + 1; (i)++) { n     int sz; n     cin >> sz; n     a[i].resize(sz); n     for (int(j) = 0; (j) < sz; (j)++) cin >> a[i][j]; n   } n   for (int(i) = 0; (i) < k; (i)++) { n     dfs(q[i]); n   } n   cout << ans.size() << endl; n   for (int(i) = 0; (i) < ans.size(); (i)++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long base, long long pow, long long mo) { n   long long res = 1; n   while (pow) { n     if (pow % 2 != 0) res = (res * base) % mo; n     base = (base * base) % mo; n     pow /= 2; n   } n   return res; n } n long long inv(long long i) { return power(i, 1000000007 - 2, 1000000007); } n std::string str =  All Good Till Here ; n void solve() { n   long long i, j, x, n; n   string str; n   cin >> str; n   long long up = 0, lr = 0, up1 = 0, lr1 = 0; n   n = str.length(); n   for (long long i = 0; i < n; i++) { n     if (str[i] >=  a  && str[i] <=  z ) n       lr++; n     else n       up++; n   } n   long long ans = INT_MAX; n   for (long long i = 0; i < n; i++) { n     ans = min(ans, lr1 + up - up1); n     if (str[i] >=  a  && str[i] <=  z ) n       lr1++; n     else n       up1++; n   } n   ans = min(ans, lr1 + up - up1); n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long tt; n   tt = 1; n   while (tt--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n class Scanner { n  private: n   static const int BUFFER_SIZE = 10000; n   char buff[BUFFER_SIZE]; n   int buffPos, buffLim; n  n  public: n   Scanner() { n     buffLim = fread(buff, 1, BUFFER_SIZE, stdin); n     buffPos = 0; n   } n  n  private: n   inline void flushBuff() { n     buffLim = fread(buff, 1, BUFFER_SIZE, stdin); n     if (buffLim == 0) { n       buff[buffLim++] =   n ; n     } n     buffPos = 0; n   } n   inline bool isWS(char t) { return t ==     || t ==   n ; } n   inline bool isDig(char t) { return t >=  0  && t <=  9 ; } n   void nextPos() { n     buffPos++; n     if (buffPos == buffLim) { n       flushBuff(); n     } n   } n  n  public: n   inline char getchar() { n     char ch = buff[buffPos]; n     nextPos(); n     return ch; n   } n   inline void next(char* s) { n     while (isWS(buff[buffPos])) { n       nextPos(); n     } n     while (!isWS(buff[buffPos])) { n       *s = buff[buffPos]; n       s++; n       nextPos(); n     } n     *s =   0 ; n   } n   inline void nextLine(char* s) { n     while (buff[buffPos] !=   n ) { n       nextPos(); n     } n     if (buff[buffPos] ==   n ) { n       nextPos(); n     } n     while (buff[buffPos] !=   n ) { n       *s = buff[buffPos]; n       s++; n       buffPos++; n     } n     *s =   0 ; n   } n   inline int nextInt() { n     while (!isDig(buff[buffPos]) && buff[buffPos] !=  - ) { n       nextPos(); n     } n     int sign = (buff[buffPos] ==  - ) ? nextPos(), -1 : 1; n     int res = 0; n     while (isDig(buff[buffPos])) { n       res = res * 10 + buff[buffPos] -  0 ; n       nextPos(); n     } n     return res * sign; n   } n   inline long long nextLong() { n     while (!isDig(buff[buffPos]) && buff[buffPos] !=  - ) { n       nextPos(); n     } n     long long sign = (buff[buffPos] ==  - ) ? nextPos(), -1 : 1; n     long long res = 0; n     while (isDig(buff[buffPos])) { n       res = res * 10 + buff[buffPos] -  0 ; n       nextPos(); n     } n     return res * sign; n   } n   inline int n() { n     while (buff[buffPos] <  0  || buff[buffPos] >  9 ) { n       buffPos++; n       if (buffPos == buffLim) { n         flushBuff(); n       } n     } n     int res = 0; n     while (buff[buffPos] >=  0  && buff[buffPos] <=  9 ) { n       res = res * 10 + (buff[buffPos] -  0 ); n       buffPos++; n       if (buffPos == buffLim) { n         flushBuff(); n       } n     } n     return res; n   } n   inline long long nl() { n     while (buff[buffPos] <  0  || buff[buffPos] >  9 ) { n       buffPos++; n       if (buffPos == buffLim) { n         flushBuff(); n       } n     } n     long long res = 0; n     while (buff[buffPos] >=  0  && buff[buffPos] <=  9 ) { n       res = res * 10 + (buff[buffPos] -  0 ); n       buffPos++; n       if (buffPos == buffLim) { n         flushBuff(); n       } n     } n     return res; n   } n   inline long long nlm(const int MOD) { n     while (buff[buffPos] <  0  || buff[buffPos] >  9 ) { n       buffPos++; n       if (buffPos == buffLim) { n         flushBuff(); n       } n     } n     long long res = 0; n     while (buff[buffPos] >=  0  && buff[buffPos] <=  9 ) { n       res = (res * 10 + (buff[buffPos] -  0 )) % MOD; n       buffPos++; n       if (buffPos == buffLim) { n         flushBuff(); n       } n     } n     return res; n   } n   inline double nextDouble() { n     while (isWS(buff[buffPos])) { n       nextPos(); n     } n     int sign = (buff[buffPos] ==  - ) ? nextPos(), -1 : 1; n     double res = 0; n     while (isDig(buff[buffPos])) { n       res = res * 10 + buff[buffPos] -  0 ; n       nextPos(); n     } n     if (buff[buffPos] ==  . ) { n       nextPos(); n       double ep = 1; n       while (isDig(buff[buffPos])) { n         ep *= 0.1; n         res += ep * (buff[buffPos] -  0 ); n         nextPos(); n       } n     } n     return sign * res; n   } n   inline char nextChar() { n     while (isWS(buff[buffPos])) nextPos(); n     char res = buff[buffPos]; n     nextPos(); n     return res; n   } n }; n Scanner sc; n template <class _Type> n void logArray(_Type a[], int n) { n   for (int i = 0; i < n; ++i) std::cout << a[i] <<    ; n   std::cout << std::endl; n } n template <class T> n inline bool checkMin(T& a, T b) { n   return (a > b ? a = b, 1 : 0); n } n template <class T> n inline bool checkMax(T& a, T b) { n   return (a < b ? a = b, 1 : 0); n } n void preInit(); n void init(); n void solve(); n int32_t main() { n   preInit(); n   init(); n   solve(); n   return 0; n } n const int N = 300005; n std::vector<int> e[N]; n int col[N], n; n int dp[N], sz[N]; n void dfs(int u) { n   dp[u] = 1; n   sz[u] = (e[u].size() == 0); n   for (int v : e[u]) { n     dfs(v); n     sz[u] += sz[v]; n   } n   if (col[u] == 0) { n     for (int v : e[u]) dp[u] += dp[v] - 1; n   } else { n     for (int v : e[u]) checkMax(dp[u], sz[u] - sz[v] + dp[v]); n   } n } n void preInit() {} n void init() { n   n = sc.n(); n   for (int i = 1; i <= n; i++) col[i] = sc.n(); n   for (int i = 2; i <= n; i++) { n     e[sc.n()].push_back(i); n   } n } n void solve() { n   dfs(1); n   printf( %d n , dp[1]); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> d; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, u; n   cin >> n >> u; n   d.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> d[i]; n   } n   double ans = -1.0; n   for (int k = 2; k < n; k++) { n     int i = lower_bound(d.begin(), d.end(), d[k] - u) - d.begin(); n     int j = i + 1; n     if (i == j || j == k || k == i) continue; n     double res = double(d[k] - d[j]) / double(d[k] - d[i]); n     ans = max(ans, res); n   } n   for (int i = 0; i < n - 2; i++) { n     int j = i + 1; n     int k = upper_bound(d.begin(), d.end(), u + d[i]) - d.begin(); n     k--; n     if (i == j || j == k || k == i) continue; n     double res = double(d[k] - d[j]) / double(d[k] - d[i]); n     ans = max(ans, res); n   } n   cout.precision(15); n   cout.fixed; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, x, p, q, ans = 0, cnt = 0; n   ; n   cin >> n >> k >> x; n   int a[n + 5]; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   if (n == 1) { n     cout << x << endl; n     return 0; n   } n   for (int i = n; i > 1; i--) { n     if (a[i] >= a[i - 1] && cnt < k) { n       cnt++; n       ans += x; n     } else n       ans += a[i]; n   } n   if (cnt < k && a[1] <= a[2]) n     ans += x; n   else n     ans += a[1]; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:667772160 ) n using namespace std; n struct __isoff { n   __isoff() { n     if (0) freopen( input.txt ,  r , stdin), freopen( output.txt ,  w , stdout); n     srand(time(0)); n   } n   ~__isoff() {} n } __osafwf; n const unsigned long long p1 = 31; n const unsigned long long p2 = 29; n const double eps = 1e-8; n const double pi = acos(-1.0); n const long long inf = 1e18 + 7; n const int infi = 1e9 + 7; n const long long dd = 1e6 + 70; n const long long mod = 1e9 + 7; n int n, k; n vector<int> P[dd]; n vector<int> T(1); n vector<pair<int, int> > Z; n bool Bs[dd]; n int get(int l, int r, int x) { n   return upper_bound(P[x].begin(), P[x].end(), r) - n          lower_bound(P[x].begin(), P[x].end(), l); n } n set<int> Q, W; n int main() { n   cin >> n >> k; n   long long s = 0; n   P[0].push_back(0); n   for (long long i = 0; i < (long long)n; i++) { n     int t; n     scanf( %d , &t); n     Z.push_back(make_pair(-t, i)); n     s = (s + t) % k; n     T.push_back(s); n     P[s].push_back(i + 1); n   } n   sort(Z.begin(), Z.end()); n   Bs[0] = Bs[n] = 1; n   long long res = 0; n   Q.insert(-1); n   Q.insert(n); n   W.insert(1); n   W.insert(-n); n   for (long long i = 0; i < (long long)n; i++) { n     int t = Z[i].second; n     int r = *Q.lower_bound(t); n     int l = -(*W.lower_bound(-t)); n     if (t - l < r - t) { n       for (int j = l + 1; j <= t; j++) { n         int z = max(j + 2, t + 1); n         int ts = (T[j] - Z[i].first) % k; n         int tt = get(z, r, ts); n         res += tt; n       } n     } else { n       for (int j = t + 1; j <= r; j++) { n         int z = min(j - 2, t); n         int ts = ((T[j] + Z[i].first) % k + k) % k; n         int tt = get(l + 1, z, ts); n         res += tt; n       } n     } n     Q.insert(t); n     W.insert(-t); n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n int main() { n   int h; n   cin >> h; n   int n = (1 << h) - 1; n   vector<pair<int, int>> c(n + 1); n   for (int i = 0; i <= n; i++) c[i] = {0, i}; n   for (int i = 0; i < 420; i++) { n     int u = rng() % n + 1, v = rng() % n + 1, w = rng() % n + 1; n     while (u == v) v = rng() % n + 1; n     while (w == u || w == v) w = rng() % n + 1; n     cout <<  ?   << u <<     << v <<     << w <<   n ; n     int x; n     cin >> x; n     c[x].first++; n   } n   sort(c.rbegin(), c.rend()); n   for (int i = 1; i <= n; i++) { n     if (i == c[0].second || i == c[1].second) continue; n     cout <<  ?   << c[0].second <<     << c[1].second <<     << i <<   n ; n     int x; n     cin >> x; n     if (x == i) { n       cout <<  !   << i <<   n ; n       return 0; n     } n   } n } n