#include <bits/stdc++.h> n using namespace std; n char st[100000]; n int n, t, a[26], minn, maxx; n int main() { n   scanf( %d%d , &n, &t); n   scanf( %s , st); n   minn = 25; n   maxx = 0; n   for (int i = 0; i < n; i++) { n     if (st[i] - 97 < minn) minn = st[i] - 97; n     if (st[i] - 97 > maxx) maxx = st[i] - 97; n     a[st[i] - 97] = 1; n   } n   if (t > n) { n     printf( %s , st); n     for (int i = n + 1; i <= t; i++) printf( %c , minn + 97); n   } else { n     int wei = t - 1; n     while (st[wei] - 97 == maxx) { n       st[wei] = minn + 97; n       wei--; n     } n     st[wei]++; n     while (a[st[wei] - 97] == 0) st[wei]++; n     for (int i = 0; i < t; i++) printf( %c , st[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 100; n void breakGame(int a, int b) { n   int n, j, k, dk; n   if (a > b) swap(a, b); n   dk = 2 * a; n   k = a + 1; n   n = b - a; n   if (n & 1) k *= 2; n   cout << k <<   n ; n   if (n % 2 == 0) { n     for (j = 0; j <= dk; j += 2) cout << n / 2 + j <<    ; n   } else n     for (j = 0; j <= dk; j += 2) n       cout << n / 2 + j <<     << n / 2 + j + 1 <<    ; n   cout <<   n ; n } n int main() { n   int t, a, b; n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> t; n   while (t--) { n     cin >> a >> b; n     breakGame(a, b); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 100000 + 5; n map<long long int, int> mp; n queue<int> Q; n int vis[M]; n long long int dk[M]; n vector<long long int> step; n long long int a[M]; n int rch[M]; n struct TREE { n   int lc; n   int rc; n   long long int l; n   long long int r; n   long long int idx; n   long long int val; n } tree[20 * M]; n int tot = 1; n void update(long long int l, long long int r, int node, long long int val, n             long long int idx) { n   if (l == r) { n     tree[node].val += val; n     tree[node].l = l; n     tree[node].r = r; n     tree[node].idx = idx; n     tree[node].lc = node; n     tree[node].rc = node; n     return; n   } n   long long int m = (l + r) / 2; n   if (tree[node].lc == 0) { n     tot++; n     tree[node].lc = tot; n   } n   if (tree[node].rc == 0) { n     tot++; n     tree[node].rc = tot; n   } n   if (idx <= m) { n     update(l, m, tree[node].lc, val, idx); n   } else { n     update(m + 1, r, tree[node].rc, val, idx); n   } n   int lc = tree[node].lc; n   int rc = tree[node].rc; n   if (tree[lc].val < tree[rc].val) { n     tree[node].idx = tree[rc].idx; n     tree[node].val = tree[rc].val; n   } else { n     tree[node].idx = tree[lc].idx; n     tree[node].val = tree[lc].val; n   } n   return; n } n int main() { n   time_t t_start, t_end; n   t_start = clock(); n   long long int h; n   cin >> h; n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) { n     long long int ai; n     int ci; n     cin >> ai >> ci; n     a[i + 1] = ai; n     mp[ai] = ci; n     if (ai % k == 1) { n       rch[i + 1] = 1; n       update(1, M, 1, ci, i + 1); n     } n   } n   step.push_back(k); n   for (int q = 0; q < m; q++) { n     int ty; n     cin >> ty; n     if (ty == 1) { n       long long int x; n       cin >> x; n       step.push_back(x); n       for (int i = 0; i < M; i++) { n         vis[i] = 0; n         dk[i] = -1; n       } n       Q.push(1); n       dk[1] = 0; n       while (!Q.empty()) { n         int u = Q.front(); n         vis[u] = 0; n         Q.pop(); n         for (int i = 0; i < step.size(); i++) { n           long long int dx = step[i]; n           int nxtu = (u + dx) % k; n           long long int nxty = (u + dk[u] * k + dx) / k; n           if (dk[nxtu] == -1 || dk[nxtu] > nxty) { n             dk[nxtu] = nxty; n             if (vis[nxtu] == 0) { n               Q.push(nxtu); n               vis[nxtu] = 1; n             } n           } n         } n       } n       for (int i = 1; i <= n; i++) { n         long long int ai = a[i]; n         int u = ai % k; n         if (dk[u] != -1) { n           long long int yy = dk[u] * k + u; n           if (ai >= yy && rch[i] == 0) { n             rch[i] = 1; n             update(1, M, 1, mp[ai], i); n           } n         } n       } n     } n     if (ty == 2) { n       int x; n       long long int y; n       cin >> x >> y; n       long long int ai = a[x]; n       mp[ai] = mp[ai] - y; n       if (rch[x] == 1) { n         update(1, M, 1, -y, x); n       } n     } n     if (ty == 3) { n       long long int res = tree[1].val; n       cout << res << endl; n       update(1, M, 1, -res, tree[1].idx); n     } n   } n   t_end = clock(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e9 + 113; n const long long maxn = 5000 + 113; n bool used[maxn]; n int main() { n   srand(time(NULL)); n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<pair<pair<int, int>, int>> we; n   for (int i = 0; i < n; i++) { n     cout <<  ? 0   << i << endl; n     cout.flush(); n     int s; n     cin >> s; n     we.push_back({{0, i}, s}); n   } n   for (int i = 1; i < n; i++) { n     cout <<  ?   << i <<   0  << endl; n     cout.flush(); n     int s; n     cin >> s; n     we.push_back({{i, 0}, s}); n   } n   int k = 1; n   vector<int> p, b; n   vector<int> ansp; n   ansp.resize(n); n   long long ans = 0; n   p.resize(n), b.resize(n); n   for (int x = 0; x < n; x++) { n     for (int i = 0; i < n; i++) p[i] = 0, b[i] = 0; n     p[0] = x; n     for (int j = 0; j < we.size(); j++) { n       if (we[j].first.first == 0) n         b[we[j].first.second] = p[0] ^ we[j].second; n       else n         p[we[j].first.first] = b[0] ^ we[j].second; n     } n     bool tr = true; n     for (int i = 0; i < n; i++) { n       if (used[p[i]] == k || p[i] >= n) { n         tr = false; n         break; n       } n       used[p[i]] = k; n     } n     k++; n     if (!tr) continue; n     for (int i = 0; i < n; i++) { n       if (used[b[i]] == k || b[i] >= n) { n         tr = false; n         break; n       } n       used[b[i]] = k; n     } n     k++; n     if (!tr) continue; n     for (int i = 0; i < n; i++) { n       if (p[b[i]] != i) { n         tr = false; n         break; n       } n     } n     if (!tr) continue; n     if (!ans) n       for (int i = 0; i < n; i++) ansp[i] = p[i]; n     ans++; n   } n   cout <<  !  << endl; n   cout << ans << endl; n   for (int i = 0; i < n; i++) cout << ansp[i] <<    ; n   cout << endl; n   cout.flush(); n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e9 + 7; n void add(int &a, int b) { n   a += b; n   if (a >= M) a -= M; n } n int mypow(int x, int t) { n   int ans = 1, res = x; n   while (t) { n     if (t & 1) ans = (long long)ans * res % M; n     res = (long long)res * res % M, t >>= 1; n   } n   return ans; n } n int n; n int fac[103], inv[103], p2[53], d[53], s1[53], s2[53], coef[53]; n int f[53][53], g[53][103]; n void init() { n   fac[0] = 1; n   for (int i = 1; i <= 100; i++) fac[i] = (long long)fac[i - 1] * i % M; n   inv[100] = mypow(fac[100], M - 2); n   for (int i = 99; ~i; i--) inv[i] = (long long)inv[i + 1] * (i + 1) % M; n   p2[0] = 1; n   for (int i = 1; i <= n; i++) p2[i] = (long long)p2[i - 1] * inv[2] % M; n } n int main() { n   scanf( %d , &n); n   init(); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &d[i]); n     if (i > 1) { n       d[i]--; n       s1[i] = s1[i - 1], s2[i] = s2[i - 1]; n       if (d[i] == 1) n         s1[i]++; n       else n         s2[i]++; n     } n   } n   for (int s = 0; s <= n; s++) n     for (int p = 0; p * 2 <= s; p++) { n       int q = s - 2 * p, t = (long long)inv[p] * inv[q] % M; n       if ((p + q) & 1) n         add(coef[s], M - t); n       else n         add(coef[s], t); n     } n   f[1][d[1]] = 1; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= 2 * n; j++) n       if (g[i][j]) n         for (int d = 0; d * 2 <= j; d++) n           add(f[i][j - d * 2], (long long)g[i][j] * inv[d] % M * p2[d] % M); n     for (int j = 1; i + j <= n; j++) n       if (f[i][j]) { n         int x = s1[i + j] - s1[i], y = s2[i + j] - s2[i]; n         int res = (long long)f[i][j] * fac[y] % M; n         for (int s = 0; s <= y; s++) n           add(g[i + j][x + 2 * y - 2 * s], (long long)res * coef[s] % M * n                                                fac[x + 2 * y - 2 * s] % M * n                                                inv[y - s] % M); n       } n   } n   int ans = f[n][0]; n   for (int i = 2; i <= n; i++) ans = (long long)ans * inv[d[i]] % M; n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 251, N2 = N * N, M = 1e9 + 7; n long long kc[N2], k1c[N2], ans, n, k, n2, C[N][N]; n int main() { n   cin >> n >> k; n   kc[0] = k1c[0] = 1; n   for (int i = 1; i < N2; i++) { n     kc[i] = kc[i - 1] * k % M; n     k1c[i] = k1c[i - 1] * (k - 1) % M; n   } n   C[0][0] = 1; n   for (int i = 1; i < N; i++) { n     C[i][0] = C[i][i] = 1; n     for (int j = 1; j < i; j++) { n       C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % M; n     } n   } n   n2 = n * n; n   for (int i = 0; i <= n; i++) { n     for (int j = 0; j <= n; j++) { n       long long t = C[n][j] * C[n][i] % M * kc[n2 - n * (i + j) + i * j] % M * n                     k1c[n * (i + j) - i * j] % M; n       if ((i + j) & 1) n         ans = (ans + M - t) % M; n       else n         ans = (ans + t) % M; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, rt, ra, ca, b; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int t; n     scanf( %d , &t); n     if (t != rt) { n       rt = t; n       ra = ca; n       ca = 0; n     } n     ca++; n     b = max(b, min(ra, ca) * 2); n   } n   printf( %d , b); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mabs(int a) { n   if (a < 0) n     return -a; n   else n     return a; n } n bool vis[4010]; n int main() { n   int x, k; n   int t, n1, n2; n   cin >> x >> k; n   for (int i = 0; i < k; ++i) { n     cin >> t; n     if (t == 2) { n       cin >> n2; n       vis[n2] = true; n     } else { n       cin >> n2 >> n1; n       vis[n2] = vis[n1] = true; n     } n   } n   vector<int> v; n   for (int i = 1; i < x; ++i) n     if (!vis[i]) v.push_back(i); n   int ret = 0; n   for (int i = 1; i < v.size(); ++i) { n     if (v[i] - v[i - 1] == 1) ++ret, ++i; n   } n   cout << ret + v.size() - ret * 2 <<     << v.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200], b[200], res[200]; n int main() { n   int n, count, i, j, m, k, ans; n   cin >> n >> m >> k; n   for (i = 0; i < m; i++) cin >> a[i]; n   for (i = 0; i < k; i++) cin >> b[i]; n   for (i = 0; i < m; i++) { n     for (j = 0; j < k; j++) { n       if (b[j] % a[i] == 0) res[i]++; n     } n   } n   ans = res[0]; n   for (i = 0; i < m; i++) ans = min(res[i], ans); n   count = 0; n   for (i = 0; i < m; i++) n     if (res[i] == ans) count++; n   cout << count <<   n ; n   for (i = 0; i < m; i++) n     if (ans == res[i]) cout << i + 1 <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long inf; n const double eps = 1e-8; n const double pi = acos(-1.0); n template <class T> n long long chkmin(T &a, T b) { n   return a > b ? a = b, 1 : 0; n } n template <class T> n long long chkmax(T &a, T b) { n   return a < b ? a = b, 1 : 0; n } n template <class T> n T sqr(T a) { n   return a * a; n } n template <class T> n T mmin(T a, T b) { n   return a < b ? a : b; n } n template <class T> n T mmax(T a, T b) { n   return a > b ? a : b; n } n template <class T> n T aabs(T a) { n   return a < 0 ? -a : a; n } n template <class T> n long long dcmp(T a, T b) { n   return a > b; n } n template <long long *a> n long long cmp_a(long long first, long long second) { n   return a[first] < a[second]; n } n struct __INIT__ { n   __INIT__() { memset(&inf, 0x3f, sizeof(inf)); } n } __INIT___; n namespace io { n const long long SIZE = (1 << 21) + 1; n char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, n                                        qu[55]; n long long f, qr; n inline void flush() { n   fwrite(obuf, 1, oS - obuf, stdout); n   oS = obuf; n } n inline void putc(char first) { n   *oS++ = first; n   if (oS == oT) flush(); n } n template <typename A> n inline bool read(A &first) { n   for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                               (iS == iT ? EOF : *iS++)) n                             : *iS++); n        c <  0  || c >  9 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     if (c ==  - ) n       f = -1; n     else if (c == EOF) n       return 0; n   for (first = 0; c <=  9  && c >=  0 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     first = first * 10 + (c & 15); n   first *= f; n   return 1; n } n inline bool read(char &first) { n   while ((first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                                (iS == iT ? EOF : *iS++)) n                             : *iS++)) ==     || n          first ==   n  || first ==   r ) n     ; n   return first != EOF; n } n inline bool read(char *first) { n   while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                                 (iS == iT ? EOF : *iS++)) n                              : *iS++)) ==   n  || n          *first ==     || *first ==   r ) n     ; n   if (*first == EOF) return 0; n   while (!(*first ==   n  || *first ==     || *first ==   r )) n     *(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                               (iS == iT ? EOF : *iS++)) n                            : *iS++); n   *first = 0; n   return 1; n } n template <typename A, typename... B> n inline bool read(A &first, B &...second) { n   return read(first) && read(second...); n } n template <typename A> n inline bool write(A first) { n   if (!first) putc( 0 ); n   if (first < 0) putc( - ), first = -first; n   while (first) qu[++qr] = first % 10 +  0 , first /= 10; n   while (qr) putc(qu[qr--]); n   return 0; n } n inline bool write(char first) { n   putc(first); n   return 0; n } n inline bool write(const char *first) { n   while (*first) { n     putc(*first); n     ++first; n   } n   return 0; n } n inline bool write(char *first) { n   while (*first) { n     putc(*first); n     ++first; n   } n   return 0; n } n template <typename A, typename... B> n inline bool write(A first, B... second) { n   return write(first) || write(second...); n } n struct Flusher_ { n   ~Flusher_() { flush(); } n } io_flusher_; n }  // namespace io n using io ::putc; n using io ::read; n using io ::write; n long long a[3005], b[3005]; n vector<long long> to[3005]; n pair<long long, long long> f[3005][3005], g[3005]; n long long siz[3005]; n pair<long long, long long> operator+(pair<long long, long long> a, n                                      pair<long long, long long> b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n void dfs(long long first, long long fa) { n   f[first][0] = make_pair(0, b[first]); n   siz[first] = 0; n   for (auto i : to[first]) n     if (i != fa) { n       dfs(i, first); n       for (long long j = 0; j <= siz[first] + siz[i] + 1; ++j) n         g[j] = make_pair(-1, -1); n       for (long long j = 0; j <= siz[first]; ++j) n         for (long long k = 0; k <= siz[i]; ++k) { n           chkmax(g[j + k], f[first][j] + f[i][k]); n           chkmax(g[j + k + 1], make_pair(f[first][j].first + f[i][k].first + n                                              (f[i][k].second > 0), n                                          f[first][j].second)); n         } n       for (long long j = 0; j <= siz[first] + siz[i] + 1; ++j) n         f[first][j] = g[j]; n       siz[first] += siz[i] + 1; n     } n } n signed main() { n   long long t, n, m, u, v; n   read(t); n   for (; t; --t) { n     read(n, m); n     for (long long i = 1; i <= n; ++i) read(a[i]); n     for (long long i = 1; i <= n; ++i) { n       read(b[i]); n       b[i] -= a[i]; n       to[i].clear(); n     } n     for (long long i = 1; i < n; ++i) { n       read(u, v); n       to[u].push_back(v); n       to[v].push_back(u); n     } n     dfs(1, 0); n     write(f[1][m - 1].first + (f[1][m - 1].second > 0),   n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n template <class T> n inline void read(T &res) { n   char ch; n   while (ch = getchar(), !isdigit(ch)) n     ; n   res = ch ^ 48; n   while (ch = getchar(), isdigit(ch)) res = res * 10 + ch - 48; n } n template <class T> n inline void put(T x) { n   if (x > 9) put(x / 10); n   putchar(x % 10 + 48); n } n const int N = 1e6 + 5; n int a[N], vis[N], nxt[N], pre[N], suf[N]; n int n, m, now = 1; n inline void Fail() { n   puts( no ); n   exit(0); n } n inline int Get() { n   while (vis[now]) ++now; n   if (now > n) Fail(); n   vis[now] = 1; n   return now; n } n inline void Delete(int l, int r) { n   int tl = pre[l], tr = suf[r]; n   suf[tl] = tr, pre[tr] = tl; n } n inline void Merge(int l, int r, int &x) { n   while (x > l && suf[suf[x]] && suf[suf[x]] <= r && !a[x] && a[suf[x]] && n          a[suf[suf[x]]]) { n     a[x] = a[suf[suf[x]]]; n     Delete(suf[x], suf[suf[x]]); n     x = pre[x] > l ? pre[pre[x]] : pre[x]; n   } n   while (x > l && suf[suf[x]] && suf[suf[x]] <= r && a[x] && a[suf[x]] && n          !a[suf[suf[x]]]) { n     a[suf[suf[x]]] = a[x]; n     Delete(suf[x], suf[suf[x]]); n     x = pre[x] > l ? pre[pre[x]] : pre[x]; n   } n } n inline void solve(int l, int r) { n   if (r - l & 1) Fail(); n   for (int i = l; i <= r; i = suf[i]) n     while (nxt[i]) { n       if (nxt[i] > r) Fail(); n       solve(suf[i], pre[nxt[i]]); n       Delete(suf[i], nxt[i]); n       nxt[i] = nxt[nxt[i]]; n     } n   int num1 = 0, num2 = 0, rt = a[pre[l]]; n   for (int i = l; i <= r; i = suf[i]) num1 += a[i] > 0, ++num2; n   num2 = (num2 >> 1) + 1; n   if (num1 > num2) Fail(); n   num2 -= num1; n   for (int i = l; num2 && i <= r; i = suf[i]) n     if (!a[i]) a[i] = Get(), --num2; n   if (!a[l]) { n     for (int i = suf[l]; i <= r; i = suf[i]) Merge(l, r, i); n     Merge(l - 1, r, l); n   } else { n     for (int i = l; i <= r; i = suf[i]) Merge(l - 1, r, i); n   } n   for (int i = l; i <= r; i = suf[i]) n     if (!a[i]) a[i] = rt; n } n int main() { n   read(n); n   if (n == 1) return puts( yes n1 ), 0; n   m = (n << 1) - 1; n   for (int i = 1; i <= m; ++i) read(a[i]); n   for (int i = 2; i <= m; ++i) n     if (a[i] && a[i - 1] && a[i] == a[i - 1]) Fail(); n   if (a[1] && a[m] && a[1] != a[m]) Fail(); n   a[1] = a[m] = a[1] | a[m]; n   for (int i = 0; i <= m + 1; ++i) pre[i] = i - 1, suf[i] = i + 1; n   pre[0] = 0; n   for (int i = m; i >= 1; --i) n     if (a[i]) { n       nxt[i] = vis[a[i]]; n       vis[a[i]] = i; n     } n   solve(1, m); n   puts( yes ); n   for (int i = 1; i <= m; ++i) put(a[i]), putchar(   ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char a[1 << 9][1 << 9]; n void solve(int n) { n   if (n == 0) { n     a[0][0] =  + ; n     return; n   } n   solve(n - 1); n   int mid = 1 << (n - 1); n   for (int i = (mid), _b = ((1 << n) - 1); i <= _b; ++i) n     for (int j = 0, _a = (mid); j < _a; ++j) a[i][j] = a[i - mid][j]; n   for (int i = 0, _a = (mid); i < _a; ++i) n     for (int j = (mid), _b = ((1 << n) - 1); j <= _b; ++j) n       a[i][j] = a[i][j - mid]; n   for (int i = (mid), _b = ((1 << n) - 1); i <= _b; ++i) n     for (int j = (mid), _b = ((1 << n) - 1); j <= _b; ++j) n       a[i][j] =  +  +  *  - a[i - mid][j - mid]; n } n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   while (cin >> n) { n     solve(n); n     for (int i = 0, _a = (1 << n); i < _a; ++i) { n       for (int j = 0, _a = (1 << n); j < _a; ++j) putchar(a[i][j]); n       puts(  ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long fact[200010], cnt, cnt1[200010], cnt2[200010]; n void precalc() { n   fact[0] = 1; n   for (int i = 1; i < 200010; i++) { n     fact[i] = (i * fact[i - 1]) % 1000000007; n   } n } n long long power(long long a, long long b, long long p) { n   if (b == 0) return 1; n   long long ret = power(a, b / 2, p); n   ret = (ret * ret) % p; n   if ((b % 2) == 1) ret = (ret * a) % p; n   return ret % p; n } n long long ncr(long long n, long long r) { n   if (n < r) { n     return 0; n   } n   long long ans = fact[n]; n   long long den = (fact[n - r] * fact[r]) % 1000000007; n   ans = (ans * power(den, 1000000007 - 2, 1000000007)) % 1000000007; n   return ans; n } n int main() { n   precalc(); n   string str; n   cin >> str; n   long long n = str.length(); n   cnt = 0; n   long long ans = 0; n   for (int i = 0; i < n; i++) { n     if (str[i] ==  ( ) { n       cnt++; n     } n     cnt1[i] = cnt; n   } n   cnt = 0; n   for (int i = n - 1; i >= 0; i--) { n     if (str[i] ==  ) ) { n       cnt++; n     } n     cnt2[i] = cnt; n   } n   for (int i = 0; i < n; i++) { n     if (str[i] ==  ( ) { n       long long x = cnt1[i]; n       long long y = cnt2[i]; n       ans = (ans + ncr(x + y - 1, x)) % 1000000007; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[101]; n int d[101]; n int main() { n   int N; n   cin >> N; n   for (int i = 0; i < N; ++i) cin >> arr[i]; n   for (int i = 1; i < N; ++i) d[i] = arr[i] - arr[i - 1]; n   int ans = 1001; n   for (int i = 1; i < N - 1; ++i) { n     int temp1 = 0; n     for (int j = 1; j < N; ++j) { n       int temp2; n       if (i == j) { n         temp2 = d[j] + d[j + 1]; n         j++; n       } else n         temp2 = d[j]; n       temp1 = max(temp1, temp2); n     } n     ans = min(ans, temp1); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   scanf( %I64d %I64d , &n, &m); n   long long a = min(n, m); n   long long b = max(n, m); n   long long ans; n   if (a == 1) { n     ans = b / 6 * 6; n     if (b % 6 == 4) n       ans += 2; n     else if (b % 6 == 5) n       ans += 4; n   } else if (a == 2) { n     if (b == 2) n       ans = 0; n     else if (b == 3) n       ans = 4; n     else if (b == 7) n       ans = 12; n     else n       ans = a * b; n   } else { n     ans = a * b / 2 * 2; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   vector<int> prime(n + 2, 1); n   prime[0] = 0; n   prime[1] = 0; n   long long ans = 1; n   for (long long i = 2; i <= n + 1; i++) { n     if (prime[i] == 1 && i * i <= n + 1) { n       for (long long j = i * i; j <= n + 1; j += i) { n         prime[j] = 2; n       } n       ans = 2; n     } n   } n   cout << ans << endl; n   for (long long i = 2; i <= n + 1; i++) cout << prime[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<int> arr(n); n   for (auto &x : arr) cin >> x; n   int height = 1; n   for (int i = 0; i < n; i++) { n     if (i > 0 and !arr[i - 1] and !arr[i]) { n       cout << -1 <<   n ; n       return; n     } n     height += arr[i] + (i > 0 and arr[i] and arr[i - 1] ? 4 : 0); n   } n   cout << height << endl; n } n int main(int argc, char const *argv[]) { n   int tc; n   cin >> tc; n   while (tc--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e9 + 55; n const int inf = 1e9 + 77; n const int MOD = 1e9 + 7; n const double PI = acos(-1.0); n const double eps = 1e-7; n vector<int> primes; n bitset<int(1e7) + 2> isprime; n void seive() { n   isprime[0] = isprime[1] = 1; n   for (int i = 2; i < int(1e6); ++i) { n     if (!isprime.test(i)) { n       primes.push_back(i); n       for (int j = i * 2; j < int(1e6) + 2; j += i) { n         isprime.set(j); n       } n     } n   } n } n const int n = 1e6 + 55; n int p[2][n]; n bool check(int n) { n   if (n < int(1e6)) return !isprime.test(n); n   for (auto i : primes) n     if (n % i == 0) return false; n   return true; n } n void factor(int a, int f) { n   long long id = 0, pf = primes[0]; n   while (pf * pf <= a) { n     while (a % pf == 0) { n       a /= pf; n       p[f][pf]++; n     } n     pf = primes[++id]; n   } n   if (a != 1) p[f][a]++; n } n int main() { n   seive(); n   int a, b; n   scanf( %d , &a); n   scanf( %d , &b); n   bool t1 = true; n   if (!check(a)) { n     factor(a, 0); n     t1 = false; n   } n   bool t2 = true; n   if (!check(b)) { n     factor(b, 1); n     t2 = false; n   } n   if (t1 && t2) { n     printf( -1 ); n     return 0; n   } n   if (t1 || t2) { n     if (t1) { n       if (t1 > int(1e6)) { n         printf( -1 ); n         return 0; n       } n     } else { n       if (t2 > int(1e6)) { n         printf( -1 ); n         return 0; n       } n     } n   } n   if (t1) factor(a, 0); n   if (t2) factor(b, 1); n   int res1 = 0; n   for (int i = 7; i < n; ++i) { n     if (abs(p[1][i] - p[0][i]) > 0) { n       res1 = -1; n       break; n     } n   } n   if (res1 == -1) { n     printf( -1 ); n     return 0; n   } n   for (int i = 2; i <= 5; ++i) { n     res1 += abs(p[0][i] - p[1][i]); n   } n   printf( %d , res1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long L, R; n   int n; n   cin >> n; n   while (n--) { n     cin >> L >> R; n     for (long long k = 0; k <= 60; ++k) { n       if ((1LL << k) & (L)) { n         continue; n       } else { n         if ((1LL << k) + L <= R) { n           L += (1LL << k); n         } else { n           break; n         } n       } n     } n     cout << L <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 3.14159265358979; n const double PI2 = 6.28318530717958; n const double PId2 = 1.570796326794895; n inline long long pw(long long n, int p) { n   long long ans = 1; n   for (int i = 0; i < p; i++) ans *= n; n   return ans; n } n inline vector<int> ReadVI(int count) { n   vector<int> arrayname(count); n   for (int i = 0; i < int(count); i++) cin >> arrayname[i]; n   return arrayname; n }; n inline vector<long long> ReadVlong(int count) { n   vector<long long> arrayname(count); n   for (int i = 0; i < int(count); i++) cin >> arrayname[i]; n   return arrayname; n }; n inline vector<pair<int, int> > ReadVII(int count) { n   vector<pair<int, int> > arrayname(count); n   for (int i = 0; i < int(count); i++) { n     cin >> arrayname[i].first; n     arrayname[i].second = i; n   } n   return arrayname; n }; n const int MOD = 1000000007; n const int MAXVALUE = 101; n int main() { n   int f[5]; n   cin >> f[0] >> f[1] >> f[2] >> f[3] >> f[4]; n   int n; n   cin >> n; n   string a[5] = { S ,  M ,  L ,  XL ,  XXL }; n   int d[9] = {0, 1, -1, 2, -2, 3, -3, 4, -4}; n   string ts; n   int t; n   for (int i = 0; i < n; i++) { n     cin >> ts; n     t = 0; n     for (; t < 5; t++) n       if (a[t] == ts) break; n     for (int j = 0; j < 9; j++) { n       int k = t; n       k += d[j]; n       if (k < 0 || k > 4) continue; n       if (f[k]) { n         f[k]--; n         cout << a[k] << endl; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void debug_out() { cerr <<   n ; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << H; n   debug_out(T...); n } n const long long MAXN = 1e2 + 10, INF = 1e9; n long long n, m, k, a, b, u, v, floyd[MAXN][MAXN], second[MAXN], t[MAXN], n     dp[2][MAXN]; n bool bo[MAXN][MAXN]; n vector<long long> h[MAXN]; n void update(long long id) { n   if (floyd[second[id]][t[id]] == INF) return; n   for (long long i = 0; i <= floyd[second[id]][t[id]]; i++) h[i].clear(); n   for (long long i = 1; i <= n; i++) dp[1][i] = INF; n   for (long long i = 1; i <= n; i++) n     if (floyd[second[id]][i] + floyd[i][t[id]] == floyd[second[id]][t[id]]) n       h[floyd[second[id]][i]].push_back(i); n   dp[1][t[id]] = dp[0][t[id]]; n   for (long long i = floyd[second[id]][t[id]] - 1; i >= 0; i--) { n     for (auto u : h[i]) { n       dp[1][u] = -1; n       for (auto v : h[i + 1]) n         if (bo[u][v]) dp[1][u] = max(dp[1][u], dp[1][v]); n       if (dp[1][u] == -1) n         dp[1][u] = dp[0][u]; n       else n         dp[1][u] = min(dp[1][u], dp[0][u]); n     } n     if ((long long)h[i].size() == 1) n       dp[0][h[i][0]] = min(dp[0][h[i][0]], dp[1][h[i][0]] + 1); n   } n   return; n } n void pre_Floyd() { n   for (long long i = 1; i <= n; i++) n     for (long long j = 1; j <= n; j++) floyd[i][j] = INF, floyd[j][j] = 0; n   return; n } n void Floyd_update() { n   for (long long k = 1; k <= n; k++) n     for (long long i = 1; i <= n; i++) n       for (long long j = 1; j <= n; j++) n         floyd[i][j] = min(floyd[i][j], floyd[i][k] + floyd[k][j]); n   return; n } n int32_t main() { n   cin >> n >> m >> a >> b, pre_Floyd(); n   for (long long i = 1; i <= n; i++) dp[0][i] = INF; n   for (long long i = 1; i <= m; i++) n     cin >> u >> v, floyd[u][v] = 1, bo[u][v] = true; n   cin >> k; n   for (long long i = 1; i <= k; i++) cin >> second[i] >> t[i]; n   Floyd_update(), dp[0][b] = 0; n   for (long long i = 1; i <= n; i++) n     for (long long j = 1; j <= k; j++) update(j); n   if (dp[0][a] == INF) return cout << -1 <<   n , 0; n   return cout << dp[0][a] <<   n , 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<string> st; n int main() { n   string s; n   cin >> s; n   string a = s + s; n   for (int i = 0; i < a.size() - s.size(); i++) { n     string k =   ; n     for (int j = i; j < i + s.size(); j++) k += a[j]; n     st.insert(k); n   } n   cout << st.size(); n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long k, n, w; n   cin >> k >> n >> w; n   long long req = ((w * (w + 1)) / 2) * k; n   req = req - n; n   if (req > 0) n     cout << req; n   else n     cout <<  0 ; n } n


#include <bits/stdc++.h> n  n using namespace std; n typedef long long ll; n  n void solve() { n     int a, b, k; cin >> a >> b >> k; n     vector<int> A(k); n     vector<int> B(k); n     vector<int> cA(a + 1); n     vector<int> cB(b + 1); n     ll ans = 0; n     for (int i = 0; i < k; i++) { n         cin >> A[i]; n         cA[A[i]]++; n     } n     for (int i = 0; i < k; i++) { n         cin >> B[i]; n         cB[B[i]]++; n     } n     for (int i = 0; i < k; i++) { n         ans += k - cA[A[i]] - cB[B[i]] + 1; n     } n     cout << ans / 2 <<   n ; n } n  n int main() { n     ios::sync_with_stdio(false); n     cin.tie(nullptr); n     int t; cin >> t; n     while(t--) solve(); n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t-- > 0) { n     int n; n     cin >> n; n     int a[n]; n     cin >> a[0]; n     for (int i = 1; i < n; i++) { n       cin >> a[i]; n     } n     vector<int> v; n     for (int i = 1; i < n - 1; i++) { n       if ((a[i] > a[i - 1] && a[i] > a[i + 1]) || n           (a[i] < a[i - 1] && a[i] < a[i + 1])) n         v.push_back(a[i]); n     } n     cout << v.size() + 2 << endl; n     cout << a[0] <<    ; n     for (auto i = v.begin(); i != v.end(); ++i) { n       cout << *i <<    ; n     } n     cout << a[n - 1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   for (int e = 0; e < t; e++) { n     long long n; n     cin >> n; n     vector<bool> used(n + 2, false); n     long long q; n     long long last = -1; n     vector<long long> ans; n     long long index = 1; n     for (int i = 0; i < n; i++) { n       cin >> q; n       if (q != last) { n         ans.push_back(q); n         last = q; n         used[q] = true; n         continue; n       } n       while (used[index]) { n         index++; n         if (index >= q) { n           index = -1; n           break; n         } n       } n       if (index == -1) { n         cout << -1 << endl; n         while (i < n - 1) { n           cin >> q; n           i++; n         } n         break; n       } n       ans.push_back(index); n       used[index] = true; n       continue; n     } n     if (index != -1) n       for (int i = 0; i < ans.size(); i++) { n         cout << ans[i] <<    ; n       } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read() { n   T sum = 0, fl = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) fl = -1; n   for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch -  0 ; n   return sum * fl; n } n const int maxn = 5000 + 5; n const int maxm = 1e5 + 5; n int n, m, k, c; n long long mx[maxn]; n bool del[maxm << 1], no[maxm]; n int fa[maxn], id[maxm << 1]; n vector<int> vec, E; n int he[maxn], ne[maxm << 1], to[maxm << 1], w[maxm << 1]; n struct EDGE { n   int u, v, w, id; n } e[maxm]; n bool operator<(const EDGE& a, const EDGE& b) { return a.w < b.w; } n void add_edge(int u, int v, int val) { n   ne[++c] = he[u]; n   he[u] = c; n   to[c] = v; n   w[c] = val; n   ne[++c] = he[v]; n   he[v] = c; n   to[c] = u; n   w[c] = val; n } n int find(int first) { n   return fa[first] == first ? first : fa[first] = find(fa[first]); n } n void forest() { n   for (int i = 1; i <= n; ++i) fa[i] = i; n   sort(e + 1, e + m + 1); n   for (int i = 1; i <= m; ++i) { n     int u = find(e[i].u), v = find(e[i].v); n     if (u == v || u == 1 || v == 1) continue; n     fa[u] = v; n     E.push_back(e[i].id); n     add_edge(e[i].u, e[i].v, e[i].w); n     id[c] = id[c - 1] = e[i].id; n   } n } n void get_max(int p, int f) { n   for (int i = he[p]; i; i = ne[i]) { n     if (to[i] == f || del[i] || to[i] == 1) continue; n     if (w[i] >= w[mx[p]]) n       mx[to[i]] = i; n     else n       mx[to[i]] = mx[p]; n     get_max(to[i], p); n   } n } n void Solve() { n   n = read<int>(), m = read<int>(), k = read<int>(); n   c = 1; n   for (int i = 1; i <= m; ++i) n     e[i] = (EDGE){read<int>(), read<int>(), read<int>(), i}; n   forest(); n   for (int i = 1; i <= m; ++i) { n     if (e[i].u != 1 && e[i].v != 1) continue; n     int u = find(e[i].u), v = find(e[i].v); n     if (u == v) n       vec.push_back(i); n     else n       fa[u] = v, add_edge(e[i].u, e[i].v, e[i].w), E.push_back(e[i].id), --k; n   } n   for (int i = 2; i <= n; ++i) n     if (find(i) != find(i - 1)) return (void)printf( -1 ); n   if (k < 0) return (void)printf( -1 ); n   for (int i = 1; i <= k; ++i) { n     memset(mx, 0, sizeof(mx)); n     for (int j = he[1]; j; j = ne[j]) get_max(to[j], 1); n     int now = -1, ans = 2147483647; n     for (int j = vec.size() - 1; j >= 0; --j) { n       int p = vec[j], var = max(e[p].u, e[p].v); n       if (e[p].w - w[mx[var]] < ans) ans = e[p].w - w[mx[var]], now = j; n     } n     int p = vec[now], var = e[p].u == 1 ? e[p].v : e[p].u; n     add_edge(e[p].v, e[p].u, e[p].w); n     E.push_back(e[p].id); n     no[id[mx[var]]] = 1; n     del[mx[var]] = del[mx[var] ^ 1] = 1; n     vec.erase(vec.begin() + now); n   } n   printf( %d n , n - 1); n   for (int i = E.size() - 1; i >= 0; --i) n     if (!no[E[i]]) printf( %d  , E[i]); n } n int main() { n   Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, k, a[10000]; n   cin >> n; n   cin >> m; n   string s[1000], f[1000]; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   for (int i = 0; i < m; i++) { n     cin >> f[i]; n   } n   cin >> k; n   for (int i = 0; i < k; i++) { n     cin >> a[i]; n   } n   for (int i = 0; i < k; i++) { n     a[i]--; n     cout << s[a[i] % n] << f[a[i] % m] << endl; n   } n } n


/* n { n ###################### n #       Author       # n #        Gary        # n #        2021        # n ###################### n */ n #include<bits/stdc++.h> n #define rb(a,b,c) for(int a=b;a<=c;++a) n #define rl(a,b,c) for(int a=b;a>=c;--a) n #define LL long long n #define IT iterator n #define PB push_back n #define II(a,b) make_pair(a,b) n #define FIR first n #define SEC second n #define FREO freopen( check.out , w ,stdout) n #define rep(a,b) for(int a=0;a<b;++a) n #define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) n #define random(a) rng()%a n #define ALL(a) a.begin(),a.end() n #define POB pop_back n #define ff fflush(stdout) n #define fastio ios::sync_with_stdio(false) n #define check_min(a,b) a=min(a,b) n #define check_max(a,b) a=max(a,b) n using namespace std; n //inline int read(){ n //    int x=0; n //    char ch=getchar(); n //    while(ch< 0 ||ch> 9 ){ n //        ch=getchar(); n //    } n //    while(ch>= 0 &&ch<= 9 ){ n //        x=(x<<1)+(x<<3)+(ch^48); n //        ch=getchar(); n //    } n //    return x; n //} n const int INF=0x3f3f3f3f; n typedef pair<int,int> mp; n /*} n */ n int root=1,cnt=1,ans[200000*41+233],lc[200000*41+233],rc[200000*41+233],depth[200000*41+233],r[200000*41+233],l[200000*41+233],tag[200000*41+233]; n bool have[200000*41+233]; n void pushdown(int idx){ n  if((tag[idx]>>depth[idx])&1) swap(lc[idx],rc[idx]); n  if(lc[idx]) tag[lc[idx]]^=tag[idx]; n  if(rc[idx]) tag[rc[idx]]^=tag[idx]; n  int oldl=l[idx],oldr=r[idx]; n  r[idx]^=r[idx]&tag[idx]; n  l[idx]^=l[idx]&tag[idx]; n  r[idx]|=oldl&tag[idx]; n  l[idx]|=oldr&tag[idx]; n  tag[idx]=0; n  assert(!(l[idx]&r[idx])); n } n void upd(int idx){ n  ans[idx]=have[idx]; n  if(depth[idx]==20) return ; n  l[idx]=r[idx]=0; n  assert(lc[idx]||rc[idx]); n  if(lc[idx]) pushdown(lc[idx]),ans[idx]+=ans[lc[idx]],assert(!tag[lc[idx]]); n  else r[idx]|=1<<depth[idx]; n  if(rc[idx]) pushdown(rc[idx]),ans[idx]+=ans[rc[idx]],assert(!tag[rc[idx]]); n  else l[idx]|=1<<depth[idx]; n  if(lc[idx]&&rc[idx]) l[idx]|=l[lc[idx]]&l[rc[idx]],r[idx]|=r[lc[idx]]&r[rc[idx]]; n  else l[idx]|=l[max(lc[idx],rc[idx])],r[idx]|=r[max(lc[idx],rc[idx])];  n  assert(!(l[idx]&r[idx])); n } n void merge(int& u,int& v){//merge(u,v) -> v n  if(!u||!v){ n   if(u){ n    v=u; n    u=0; n   } n   if(v) pushdown(v); n   return ; n  } n  pushdown(u),pushdown(v); n  have[v]|=have[u]; n  merge(rc[u],rc[v]); n  merge(lc[u],lc[v]); n // cout<<u<<   <<v<<   <<rc[v]<<   <<lc[v]<<   <<endl; n  u=0; n  upd(v); n } n int Rev(int x){ n  int ret=0; n  rep(i,20){ n   ret|=((x>>i)&1)<<(19-i); n  } n  return ret; n } n void split(int rt,int val,int & x,int & y){ n  if(!rt){ n   x=y=0; n   return ; n  } n  pushdown(rt); n  if(depth[rt]==20){ n   x=rt,y=0; n   return; n  } n  if((val>>depth[rt])&1){ n   x=rt; n   y=++cnt; n   depth[cnt]=depth[rt]; n   split(rc[rt],val,rc[x],rc[y]); n  } n  else{ n   x=++cnt; n   y=rt;                                                                                n   depth[cnt]=depth[rt]; n   split(lc[rt],val,lc[x],lc[y]); n  } n  if(!lc[x]&&!rc[x]){ n   x=0; n  } n  if(!lc[y]&&!rc[y]){ n   y=0; n  } n  if(x) upd(x); n  if(y) upd(y); n } n void getor(int idx,int x){ n  if(depth[idx]==20) return ; n  pushdown(idx); n   n  if((x&(l[idx]|r[idx]))==x){ n   tag[idx]^=x&l[idx]; n   pushdown(idx); n   return ; n  } n   n  if((x>>depth[idx])&1); n  else{ n   if(lc[idx]) getor(lc[idx],x); n   if(rc[idx]) getor(rc[idx],x); n   upd(idx); n   return ; n  } n  if(lc[idx]&&rc[idx]){ n   merge(lc[idx],rc[idx]); n   getor(rc[idx],x^(1<<depth[idx])); n   upd(idx); n   return ; n  } n  if(lc[idx]){ n   swap(lc[idx],rc[idx]); n   getor(rc[idx],x^(1<<depth[idx])); n   upd(idx); n   return ; n  } n  assert(rc[idx]); n  getor(rc[idx],x^(1<<depth[idx])); n  upd(idx); n } n void insert(int rt,int val){ n  if(depth[rt]==20){ n   have[rt]=1; n   upd(rt); n   return ;  n  } n  bool bt=(val>>depth[rt])&1; n  if(bt){ n   if(!rc[rt]) rc[rt]=++cnt,depth[cnt]=depth[rt]+1; n   insert(rc[rt],val); n  } n  else{ n   if(!lc[rt]) lc[rt]=++cnt,depth[cnt]=depth[rt]+1; n   insert(lc[rt],val); n  } n  upd(rt); n } n int main(){ n  int n,q; n  scanf( %d%d ,&n,&q); n  rb(i,1,n){ n   int ai; n   scanf( %d ,&ai); n   insert(root,Rev(ai)); n  } n  rb(i,1,q){ n   int ty,l,r; n   scanf( %d%d%d ,&ty,&l,&r); n   int x=0,y=0,z=0; n   split(root,Rev(r),y,z); n   if(l) n   split(y,Rev(l-1),x,y); n   else x=0; n   if(ty<=3){ n    int X; n    scanf( %d ,&X); n //   if(n==1000) cout<<ty<<   <<l<<   <<r<<   <<X<<endl; n    if(y){ n     if(ty==1){ n      tag[y]^=(1<<20)-1; n      getor(y,((1<<20)-1)^Rev(X)); n      tag[y]^=(1<<20)-1; n     } n     if(ty==2){ n      getor(y,Rev(X)); n     } n     if(ty==3){ n      tag[y]^=Rev(X); n     } n     pushdown(y); n    } n   } n   else{ n //   if(n==1000) cout<<ty<<   <<l<<   <.<r<<endl; n    printf( %d n ,ans[y]); n   } n   merge(x,y); n   merge(y,z); n   root=z; n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n long long a[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t, n, k; n   cin >> t; n   while (t--) { n     cin >> n; n     string ans, e, ant; n     bool ok = 0; n     for (int i = 0; i < (int)n - 2; i++) { n       cin >> e; n       if (!i) n         ans += e[0]; n       else if (e[0] == ant[1]) n         ans += e[0]; n       else n         ans += ant[1], ans += e[0], ok = 1; n       ant = e; n     } n     ans += ant[1]; n     if (!ok) ans +=  b ; n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int MAXN = 1000; n int n; n set<int> s[4]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int tmp; n     for (int j = 1; j <= 6; j++) cin >> tmp, s[i].insert(tmp); n   } n   int i; n   for (i = 1; i <= MAXN; i++) { n     if (i == MAXN) break; n     multiset<int> ms; n     int t = i; n     while (t > 0) { n       ms.insert(t % 10); n       t /= 10; n     } n     for (int j = 1; j <= n; j++) { n       for (auto x : ms) n         if (s[j].count(x) > 0) { n           ms.erase(ms.find(x)); n           break; n         } n     } n     if (ms.empty()) continue; n     ms.clear(); n     t = i; n     while (t > 0) { n       ms.insert(t % 10); n       t /= 10; n     } n     for (int j = n; j > 0; j--) { n       for (auto x : ms) n         if (s[j].count(x) > 0) { n           ms.erase(ms.find(x)); n           break; n         } n     } n     if (ms.empty()) continue; n     break; n   } n   cout << i - 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long read() { n   long long x = 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     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = getchar(); n   } n   return x * f; n } n long long n; n long long fa[1010000], lg[1010000]; n long long cnt[1010000], maxx = 0; n long long a[1010000]; n long long find(long long x) { n   if (x == fa[x]) return x; n   return fa[x] = find(fa[x]); n } n signed main() { n   n = read(); n   long long ans = 0; n   for (long long i = 1; i <= n; i++) n     a[i] = read(), maxx = max(maxx, a[i]), ans -= a[i], cnt[a[i]]++; n   for (long long i = 1; i <= maxx; i++) fa[i] = i, lg[i] = lg[i >> 1] + 1; n   cnt[0]++; n   long long m = 1 << (lg[maxx] + 1); n   for (register long long i = m - 1; ~i; i--) { n     for (long long j = i; j * 2 > i; j = (j - 1) & i) { n       long long x = j, y = i ^ j; n       if (!cnt[x] || !cnt[y]) continue; n       x = find(x), y = find(y); n       if (x != y) { n         ans += i * (cnt[x] + cnt[y] - 1); n         fa[x] = y; n         cnt[y] = 1; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long q, n; n int main() { n   cin >> q; n   for (int i = 0; i < q; i++) { n     cin >> n; n     vector<long long> nums; n     long long cSum = 0, power = 0; n     while (cSum < n) { n       cSum += pow(3, power); n       nums.push_back(pow(3, power)); n       power++; n     } n     for (long long j = nums.size() - 1; j >= 0; j--) { n       if (cSum - nums[j] >= n) cSum -= nums[j]; n     } n     cout << cSum <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n template <class T> n T gcd(T a, T b) { n   return ((b == 0) ? a : gcd(b, a % b)); n } n int m[1005]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> a(((n) * (n))); n   memset(m, 0, sizeof(m)); n   for (int i = 0; i < (int)(((n) * (n))); i++) cin >> a[i], m[a[i]]++; n   if (n & 1) { n     vector<int> pos, pos1; n     for (int i = 0; i < (int)(1005); i++) { n       if (m[i]) n         for (int j = 0; j < (int)((m[i] / 4)); j++) pos.push_back(i); n       m[i] %= 4; n       while (m[i]--) pos1.push_back(i); n     } n     int ans[n][n]; n     for (int i = 0; i < (int)(n); i++) n       for (int j = 0; j < (int)(n); j++) ans[i][j] = -1; n     for (int i = 0; i < (int)(n / 2); i++) n       for (int j = 0; j < (int)(n / 2); j++) { n         if (!pos.size()) { n           cout <<  NO  << endl; n           return 0; n         } n         int now = pos[(int)pos.size() - 1]; n         pos.pop_back(); n         ans[i][j] = now; n         ans[i][n - j - 1] = now; n         ans[n - i - 1][j] = now; n         ans[n - i - 1][n - j - 1] = now; n       } n     int m1[1005]; n     memset(m1, 0, sizeof(m1)); n     for (int i = 0; i < (int)(pos.size()); i++) { n       for (int j = 0; j < (int)(4); j++) pos1.push_back(pos[i]); n     } n     for (int i = 0; i < (int)(pos1.size()); i++) m1[pos1[i]]++; n     vector<int> pos2; n     int sp = 0; n     for (int i = 0; i < (int)(1005); i++) { n       if (m1[i]) { n         for (int j = 0; j < (int)((m1[i] / 2)); j++) pos2.push_back(i); n         if (m1[i] & 1) sp = i; n       } n     } n     ans[n / 2][n / 2] = sp; n     for (int i = 0; i < (int)(n / 2); i++) { n       if (!pos2.size()) { n         cout <<  NO  << endl; n         return 0; n       } n       int num = pos2[pos2.size() - 1]; n       pos2.pop_back(); n       ans[n / 2][i] = num; n       ans[n / 2][n - i - 1] = num; n     } n     for (int i = 0; i < (int)(n / 2); i++) { n       if (!pos2.size()) { n         cout <<  NO  << endl; n         return 0; n       } n       int num = pos2[pos2.size() - 1]; n       pos2.pop_back(); n       ans[i][n / 2] = num; n       ans[n - i - 1][n / 2] = num; n     } n     cout <<  YES  << endl; n     for (int i = 0; i < (int)(n); i++) { n       for (int j = 0; j < (int)(n); j++) { n         if (ans[i][j] == -1) { n           cout <<  NO  << endl; n           return 0; n         } n         cout << ans[i][j] <<    ; n       } n       cout << endl; n     } n   } else { n     vector<int> pos; n     for (int i = 0; i < (int)(1005); i++) { n       if (m[i] % 4 != 0) { n         cout <<  NO  << endl; n         return 0; n       } else if (m[i]) n         for (int j = 0; j < (int)((m[i] / 4)); j++) pos.push_back(i); n     } n     int ans[n][n]; n     for (int i = 0; i < (int)(n); i++) n       for (int j = 0; j < (int)(n); j++) ans[i][j] = -1; n     for (int i = 0; i < (int)(n / 2); i++) n       for (int j = 0; j < (int)(n / 2); j++) { n         if (!pos.size()) { n           cout <<  NO  << endl; n           return 0; n         } n         int now = pos[(int)pos.size() - 1]; n         pos.pop_back(); n         ans[i][j] = now; n         ans[i][n - j - 1] = now; n         ans[n - i - 1][j] = now; n         ans[n - i - 1][n - j - 1] = now; n       } n     cout <<  YES  << endl; n     for (int i = 0; i < (int)(n); i++) { n       for (int j = 0; j < (int)(n); j++) { n         if (ans[i][j] == -1) { n           cout <<  NO  << endl; n           return 0; n         } n         cout << ans[i][j] <<    ; n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4786) n using namespace std; n long long heads[100005], tracks[100005]; n int n, m; n long long Abs(long long x) { return x < 0 ? -x : x; } n int check(long long x) { n   int i, j = 0; n   for (i = 0; i < n; i++) { n     if (Abs(heads[i] - tracks[j]) > x) continue; n     int lo = j, hi = m - 1, k = -1; n     while (lo <= hi) { n       int mid = (lo + hi) / 2; n       long long t1 = Abs(heads[i] - tracks[mid]) + tracks[mid] - tracks[j]; n       long long t2 = Abs(heads[i] - tracks[j]) + tracks[mid] - tracks[j]; n       long long t = ((t1) < (t2) ? (t1) : (t2)); n       if (t <= x) n         k = mid, lo = mid + 1; n       else n         hi = mid - 1; n     } n     if (k < 0) continue; n     j = k + 1; n     if (j == m) return 1; n   } n   return 0; n } n int main() { n   int i, j, k, T; n   cin >> n >> m; n   for (i = 0; i < n; i++) cin >> heads[i]; n   for (i = 0; i < m; i++) cin >> tracks[i]; n   long long lo = 0, hi = 1; n   for (i = 0; i < 16; i++) hi *= (long long)10; n   long long ans = -1; n   while (lo <= hi) { n     long long mid = (lo + hi) / 2; n     if (check(mid)) n       ans = mid, hi = mid - 1; n     else n       lo = mid + 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n const int mod = (int)1e9 + 7; n const long long INF = 10000005; n const int inf = 1 << 30; n char buf[1000]; n const int N = 210; n vector<int> dep[N]; n vector<int> computer[3]; n bool completed[N]; n int n; n int check(int start) { n   int tot = 0, done = 0; n   memset(completed, false, sizeof(completed)); n   while (done < n) { n     bool found = true; n     while (found) { n       found = false; n       for (vector<int>::iterator i = computer[start].begin(); n            i != computer[start].end(); ++i) { n         if (!completed[*i]) { n           bool cando = true; n           for (vector<int>::iterator j = dep[*i].begin(); j != dep[*i].end(); n                ++j) { n             if (!completed[*j]) { n               cando = false; n               break; n             } n           } n           if (cando) { n             done++; n             completed[*i] = true; n             found = true; n           } n         } n       } n     } n     if (done < n) { n       tot++; n       start = (start + 1) % 3; n     } n   } n   return tot; n } n int main() { n   cin >> n; n   int tmp; n   for (int i = 0; i < n; ++i) { n     scanf( %d , &tmp); n     computer[tmp - 1].push_back(i); n   } n   for (int i = 0; i < n; ++i) { n     int d; n     scanf( %d , &d); n     for (int j = 0; j < d; ++j) { n       scanf( %d , &tmp); n       dep[i].push_back(tmp - 1); n     } n   } n   cout << n + min(check(0), min(check(1), check(2))) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500050; n const int inf = 0x3f3f3f3f; n const long long llinf = 0x3f3f3f3f3f3f3f3f; n const int mod = 998244353; n const int MOD = 1e9 + 7; n const double PI = acos(-1.0); n clock_t TIME__START, TIME__END; n void program_end() {} n const int Gmod = 3, maxn = (1 << 20) - 1; n inline int ksm(long long x, long long y) { n   int ret = 1; n   while (y) { n     if (y & 1) ret = 1ll * ret * x % mod; n     x = 1ll * x * x % mod; n     y >>= 1; n   } n   return ret; n } n int rev[maxn], _lstN, P[maxn], iP[maxn]; n inline void ntt(int *A, int polyN, int t) { n   for (int i = 0; i < polyN; ++i) n     if (rev[i] > i) std::swap(A[i], A[rev[i]]); n   for (int o = 1; o < polyN; o <<= 1) { n     int W = t ? P[o] : iP[o]; n     for (int *p = A; p != A + polyN; p += o << 1) n       for (int i = 0, w = 1; i < o; ++i, w = 1ll * w * W % mod) { n         int t = 1ll * w * p[i + o] % mod; n         p[i + o] = (p[i] - t + mod) % mod; n         p[i] = (p[i] + t) % mod; n       } n   } n   if (!t) { n     int inv = ksm(polyN, mod - 2); n     for (int i = 0; i < polyN; ++i) A[i] = 1ll * A[i] * inv % mod; n   } n } n int polyN, lg; n inline void setN(int n) { n   polyN = 1, lg = 0; n   while (polyN < n) polyN <<= 1, ++lg; n   if (polyN != _lstN) n     for (int i = 0; i < polyN; ++i) n       rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lg - 1)); n } n inline void ntt(std::vector<int> &a, int t) { n   static int A[maxn]; n   for (int i = 0; i < (int)a.size(); ++i) A[i] = a[i]; n   memset(A + a.size(), 0, 4 * (polyN - a.size())); n   ntt(A, polyN, t); n   a.resize(polyN); n   for (int i = 0; i < polyN; ++i) a[i] = A[i]; n   int s = a.size(); n   while (s && !a[s - 1]) --s; n   a.resize(s); n } n inline std::vector<int> mul(std::vector<int> a, std::vector<int> b, n                             int newn = -1) { n   if (newn == -1) newn = a.size() + b.size() - 1; n   setN(a.size() + b.size() - 1); n   ntt(a, 1), ntt(b, 1); n   for (int i = 0; i < polyN; ++i) a[i] = 1ll * a[i] * b[i] % mod; n   ntt(a, 0); n   a.resize(newn); n   return a; n } n inline std::vector<int> operator+(std::vector<int> a, n                                   const std::vector<int> &b) { n   if (a.size() < b.size()) a.resize(b.size()); n   for (int i = 0; i < (int)a.size(); ++i) n     if (i < (int)b.size()) a[i] = (a[i] + b[i]) % mod; n   return a; n } n inline std::vector<int> operator-(std::vector<int> a, n                                   const std::vector<int> &b) { n   if (a.size() < b.size()) a.resize(b.size()); n   for (int i = 0; i < (int)a.size(); ++i) n     if (i < (int)b.size()) a[i] = (a[i] - b[i] + mod) % mod; n   return a; n } n inline std::vector<int> operator*(std::vector<int> a, int b) { n   for (auto &i : a) i = 1ll * i * b % mod; n   return a; n } n inline std::vector<int> polydiff(std::vector<int> a) { n   for (int i = 0; i < (int)a.size() - 1; ++i) n     a[i] = 1ll * a[i + 1] * (i + 1) % mod; n   a.erase(a.end() - 1); n   return a; n } n inline std::vector<int> polyint(std::vector<int> a) { n   a.insert(a.begin(), 0); n   for (int i = 1; i < (int)a.size(); ++i) n     a[i] = 1ll * a[i] * ksm(i, mod - 2) % mod; n   return a; n } n inline std::vector<int> getinv(std::vector<int> a) { n   if (a.size() == 1) return std::vector<int>(1, ksm(a[0], mod - 2)); n   int n = a.size(), m = (a.size() + 1) >> 1; n   std::vector<int> _a(m); n   for (int i = 0; i < m; ++i) _a[i] = a[i]; n   std::vector<int> b = getinv(_a); n   setN(n + m * 2 - 2); n   ntt(a, 1); n   ntt(b, 1); n   for (int i = 0; i < polyN; ++i) a[i] = 1ll * a[i] * b[i] % mod * b[i] % mod; n   ntt(a, 0), ntt(b, 0); n   a.resize(n); n   return b * 2 - a; n } n inline std::vector<int> getln(std::vector<int> a, int n = -1) { n   if (n == -1) n = a.size(); n   a.resize(n); n   return polyint(mul(polydiff(a), getinv(a), n)); n } n inline std::vector<int> getexp(std::vector<int> a) { n   if (a.size() == 1) return a[0] = 1, a; n   int m = (a.size() + 1) >> 1; n   std::vector<int> _a(m); n   for (int i = 0; i < m; ++i) _a[i] = a[i]; n   std::vector<int> b = getexp(_a); n   return mul(b, std::vector<int>(1, 1) - getln(b, a.size()) + a, a.size()); n } n inline std::vector<int> operator^(std::vector<int> a, int b) { n   int n = a.size(); n   a = getexp(getln(a) * b); n   a.resize(n); n   return a; n } n inline std::vector<int> operator%(std::vector<int> a, std::vector<int> b) { n   int n = a.size(), m = b.size(); n   if (n < m) return a; n   std::reverse(a.begin(), a.end()); n   std::reverse(b.begin(), b.end()); n   b.resize(n); n   std::vector<int> c = mul(a, getinv(b), n - m + 1); n   std::reverse(a.begin(), a.end()); n   b.resize(m); n   std::reverse(b.begin(), b.end()); n   std::reverse(c.begin(), c.end()); n   a = a - mul(b, c); n   int s = a.size(); n   while (s && !a[s - 1]) --s; n   a.resize(s); n   return a; n } n inline std::vector<int> sqrt(std::vector<int> a) { n   if (a.size() == 1) return a; n   int n = a.size(), m = (a.size() + 1) >> 1; n   std::vector<int> _a(m); n   for (int i = 0; i < m; ++i) _a[i] = a[i]; n   std::vector<int> b = sqrt(_a); n   b.resize(n); n   return (b + mul(a, getinv(b), n)) * ((mod + 1) >> 1); n } n inline void poly_init() { n   int G = Gmod, iG = ksm(G, mod - 2); n   for (int i = 1; i < maxn; i <<= 1) n     P[i] = ksm(G, (mod - 1) / (i << 1)), iP[i] = ksm(iG, (mod - 1) / (i << 1)); n } n struct auto_poly_init { n   auto_poly_init() { poly_init(); } n } __auto_init; n std::vector<int> F, G; n int n, m, c[N]; n inline void solve() { n   scanf( %d%d , &n, &m); n   G.resize(100005, 0); n   for (int i = 1; i <= n; ++i) scanf( %d , &c[i]); n   for (int i = 1; i <= n; ++i) G[c[i]] = 1; n   G = G * (-4); n   G[0]++; n   G = sqrt(G); n   G[0]++; n   G = getinv(G); n   F = G * 2; n   for (int i = 1; i <= m; ++i) cout << F[i] <<   n ; n } n int main() { n   TIME__START = clock(); n   int Test = 1; n   while (Test--) { n     solve(); n   } n   TIME__END = clock(); n   program_end(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x7fffffff; n const double eps = 1e-10; n const double pi = acos(-1.0); n inline int read() { n   int x = 0, f = 1; n   char ch; n   ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = 0; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = (x << 1) + (x << 3) + (ch & 15); n     ch = getchar(); n   } n   if (f) n     return x; n   else n     return -x; n } n const int N = 5010; n int n, a, b; n int val[N], nex[N], ans; n char s[N]; n int main() { n   n = read(); n   a = read(); n   b = read(); n   memset(val, 0x3f, sizeof(val)); n   val[0] = 0; n   scanf( %s , s + 1); n   for (int i = 1; i <= n; i++) { n     val[i] = min(val[i], val[i - 1] + a); n     nex[i] = i - 1; n     int p = i - 1; n     for (int j = i + 1; j <= n; j++) { n       while (p >= i && s[j] != s[p + 1]) p = nex[p]; n       if (s[j] == s[p + 1]) p++; n       nex[j] = p; n     } n     p = i - 1; n     for (int j = 1; j <= i - 1; j++) { n       while (p >= i && s[j] != s[p + 1]) p = nex[p]; n       if (s[j] == s[p + 1]) p++; n       if (p >= i) val[p] = min(val[p], val[i - 1] + b); n     } n   } n   printf( %d n , val[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200004], n, m; n int c[400004]; n long long greaterc(int m) { n   long long ans = 0, add = 0; n   memset(c, 0, sizeof(c)); n   int div = n; n   c[n] = 1; n   for (int i = 0; i < n; i++) { n     if (a[i] < m) n       div--, add -= c[div]; n     else n       add += c[div], div++; n     ans += add; n     c[div]++; n   } n   return ans; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i]; n   cout << greaterc(m) - greaterc(m + 1) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n const int N = 100005; n const double PI = 4 * atan(1); n const double eps = 1e-7; n const long long oo = 1e18; n const int AlPHA = 26, B = 30; n long long n, m, L, sr, t; n long long adj[1005][1005]; n long long yes[1005][1005]; n long long dist[1005]; n vector<long long> ad[N]; n bool vis[1005]; n vector<pair<long long, long long> > v; n void dijkstra(long long src) { n   set<pair<long long, long long> > ss; n   pair<long long, long long> ras; n   for (int i = 0; i < n; i++) { n     dist[i] = 1e17; n     vis[i] = 0; n   } n   dist[src] = 0; n   ss.insert(make_pair(0, src)); n   while (!ss.empty()) { n     ras = *(ss.begin()); n     ss.erase(ss.begin()); n     src = ras.second; n     long long w = ras.first; n     if (vis[src]) continue; n     vis[src] = 1; n     for (auto u : ad[src]) { n       if (dist[u] > w + adj[src][u]) { n         dist[u] = w + adj[src][u]; n         ss.insert(make_pair(dist[u], u)); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> m >> L >> sr >> t; n   for (int i = 0; i < m; i++) { n     long long x, y, z; n     cin >> x >> y >> z; n     ad[x].push_back(y); n     ad[y].push_back(x); n     if (z > 0) { n       adj[x][y] = adj[y][x] = z; n     } else { n       adj[x][y] = adj[y][x] = 1; n       yes[x][y] = yes[y][x] = 1; n       v.push_back(make_pair(x, y)); n     } n   } n   dijkstra(sr); n   long long x = dist[t]; n   long long i = 0; n   while (i < v.size() && x < L) { n     long long y = L - x + 1; n     adj[v[i].first][v[i].second] = adj[v[i].second][v[i].first] = y; n     dijkstra(sr); n     x = dist[t]; n     i++; n   } n   if (x != L) { n     cout <<  NO ; n     return 0; n   } else { n     cout <<  YES n ; n     for (int i = 0; i < n; i++) { n       for (int j = i + 1; j < n; j++) { n         if (adj[i][j]) cout << i <<     << j <<     << adj[i][j] <<   n ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline T read(T& x) { n   x = 0; n   int f = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) f |= (ch ==  - ), ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x = f ? -x : x; n } n const int N = 3e5 + 10; n int n, i, j, len, ans = -1, a[N]; n int main() { n   read(n); n   for (i = 1; i <= n; ++i) read(a[i]); n   int mn = -1; n   for (i = 1, j = n, len = n - 1; i <= j; ++i, --j, --len) { n     if (mn == -1) n       mn = min(a[i], a[j]); n     else n       mn = min(mn, min(a[i], a[j])); n     int k = mn / len; n     if (k < 0) continue; n     if (ans == -1) n       ans = k; n     else n       ans = min(ans, k); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n int CASE = 1; n const int mxn = 2e5 + 1; n const long long INF = 1e18; n bool prime(int n) { n   if (n <= 1) return false; n   if (n == 2 or n == 3) return true; n   if (n % 2 == 0 or n % 3 == 0) return false; n   for (int i = 5; i * i <= n; i += 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n long long __gcd(long long a, long long b) { return !b ? a : __gcd(b, a % b); } n void solve() { n   string s; n   cin >> s; n   vector<string> v; n   while (s.find( > ) != -1) { n     string str = s.substr(0, s.find( > ) + 1); n     s.erase(0, str.length()); n     v.push_back(str); n   } n   int cnt = 0; n   map<char, vector<int> > mp; n   for (string str : v) { n     if (str.find( / ) != -1) { n       for (long long i = 0; i < mp[str[str.length() - 2]].back(); i++) n         cout <<    ; n       cout << str << endl; n       mp[str[str.length() - 2]].pop_back(); n       cnt -= 2; n     } else { n       mp[str[str.length() - 2]].push_back(cnt); n       for (long long i = 0; i < cnt; i++) cout <<    ; n       cout << str << endl; n       cnt += 2; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int T = 1; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n long long power(long long base, long long expo) { n   if (base <= 1) return 1; n   long long ans = 1; n   while (expo) { n     if (expo & 1) { n       ans = (ans * base) % n; n     } n     base = (base * base) % n; n     expo >>= 1; n   } n   return ans; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   if (n == 4) { n     cout <<  YES n1 n3 n2 n4 n ; n     return 0; n   } n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       cout <<  NO ; n       return 0; n     } n   } n   vector<long long> mark(n); n   cout <<  YES n ; n   for (long long i = 1; i <= n; i++) { n     long long prnt = i * power(i - 1, n - 2) % n; n     assert(!mark[prnt]); n     mark[prnt] = 1; n     if (!prnt) prnt += n; n     cout << prnt << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k = 0; n   string ans =   ; n   cin >> n; n   while (n > 1) { n     ans +=  8 ; n     k += 1; n     n -= 2; n   } n   if (n == 1) { n     ans +=  9 ; n     k++; n     n--; n   } n   if (k < 19) n     cout << ans; n   else n     cout << -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 a, b, c, m; n   cin >> a >> b >> c >> m; n   vector<long long int> usb; n   vector<long long int> p; n   for (long long int i = 0; i < m; i++) { n     string s; n     long long int x; n     cin >> x >> s; n     if (s ==  USB ) n       usb.push_back(x); n     else n       p.push_back(x); n   } n   sort(usb.begin(), usb.end()); n   sort(p.begin(), p.end()); n   long long int pos1 = 0, pos2 = 0, sum = 0, cnt = 0; n   for (int i = 0; i < usb.size() && i < a; i++) { n     sum += usb[i]; n     pos1 = i + 1; n     cnt++; n   } n   for (int i = 0; i < p.size() && i < b; i++) { n     sum += p[i]; n     pos2 = i + 1; n     cnt++; n   } n   if (pos1 < usb.size() && pos2 < p.size()) { n     long long int cnt2 = 0; n     while (pos1 < usb.size() && pos2 < p.size() && cnt2 < c) { n       if (usb[pos1] <= p[pos2]) { n         sum += usb[pos1]; n         cnt++; n         cnt2++; n         pos1++; n       } else { n         sum += p[pos2]; n         cnt++; n         cnt2++; n         pos2++; n       } n     } n     while (pos1 < usb.size() && cnt2 < c) { n       sum += usb[pos1]; n       cnt++; n       cnt2++; n       pos1++; n     } n     while (pos2 < p.size() && cnt2 < c) { n       sum += p[pos2]; n       cnt++; n       cnt2++; n       pos2++; n     } n   } else if (pos1 < usb.size()) { n     long long int cnt2 = 0; n     while (pos1 < usb.size() && cnt2 < c) { n       sum += usb[pos1]; n       cnt++; n       cnt2++; n       pos1++; n     } n   } else if (pos2 < p.size()) { n     long long int cnt2 = 0; n     while (pos2 < p.size() && cnt2 < c) { n       sum += p[pos2]; n       cnt++; n       cnt2++; n       pos2++; n     } n   } n   cout << cnt <<     << sum; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, vector<long long> > M; n set<long long> S; n const long long N = 1000001; n long long n, a[1000001], f[N]; n void upd(long long i, long long mx) { n   for (; i > 0; i -= (i & -i)) { n     f[i] = max(f[i], mx); n   } n } n long long get(long long i) { n   long long mx = -N; n   for (; i < N; i += (i & -i)) mx = max(mx, f[i]); n   return mx; n } n signed main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   vector<long long> v; n   for (long long i = 1; i <= n; ++i) { n     cin >> a[i]; n     v.push_back(a[i]); n   } n   sort(v.begin(), v.end()); n   v.resize(unique(v.begin(), v.end()) - v.begin()); n   for (long long i = 1; i <= n; ++i) { n     a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1; n   } n   for (long long i = 1; i <= n; ++i) { n     if ((abs(get(a[i]) - i) - 1) % 2 != 0) { n       cout <<  NO ; n       return 0; n     } n     upd(a[i], i); n   } n   for (long long i = 0; i < N; ++i) f[i] = -(n + 1); n   for (long long i = n; i >= 1; --i) { n     if ((abs(abs(get(a[i])) - i) - 1) % 2 != 0) { n       cout <<  NO ; n       return 0; n     } n     upd(a[i], -i); n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n constexpr int Maxn = 53; n std::vector<int> e[Maxn]; n int n, m; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1, u, v; i <= m; ++i) { n     scanf( %d%d , &u, &v); n     if (u <= 50 && v <= 50) { n       e[u].emplace_back(v); n       e[v].emplace_back(u); n     } n   } n   n = std::min(n, 50); n   for (int i = 1; i <= n; ++i) std::sort(e[i].begin(), e[i].end()); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j < i; ++j) n       for (int k = 1; k < j; ++k) n         for (int l = 1; l < k; ++l) n           for (int r = 1; r < l; ++r) { n             std::vector<int> vec = {i, j, k, l, r}; n             int cnt = 0; n             for (int x : {0, 1, 2, 3, 4}) n               for (int y = 0; y < x; ++y) { n                 auto iter = std::lower_bound(e[vec[x]].begin(), e[vec[x]].end(), n                                              vec[y]); n                 if (iter == e[vec[x]].end() || *iter != vec[y]) ++cnt; n               } n             if (cnt == 0 || cnt == 10) { n               printf( %d %d %d %d %d n , i, j, k, l, r); n               return 0; n             } n           } n   puts( -1 ); n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int f = 0, num; n   char ch; n   while (ch = getchar(), !isdigit(ch)) n     if (ch ==  - ) f = 1; n   num = ch -  0 ; n   while (ch = getchar(), isdigit(ch)) num = num * 10 + ch -  0 ; n   return f ? -num : num; n } n const int N = 103; n char s[N], ans[N], tmp[103]; n char ch; n void is() { n   ch = getchar(); n   while (ch !=   n ) ch = getchar(); n } n int cnt, tot, T, f; n int main() { n   T = read(); n   f = 0, cnt = 0; n   while (T--) { n     int f1 = 0; n     tot = 0; n     ch = getchar(); n     while (ch !=   n ) { n       if (ch ==    ) f1 = 1; n       if (f1) n         s[tot++] = ch; n       else if (ch !=    ) n         s[tot++] = ch; n       ch = getchar(); n     } n     s[tot++] =   0 ; n     if (f) { n       if (s[1] ==  r ) cnt++; n       if (s[0] ==  c ) { n         if (cnt) n           cnt--; n         else { n           tot = 0; n           int i = 6; n           while (s[i] !=  , ) { n             tmp[tot++] = s[i]; n             i++; n           } n           tmp[tot++] =   0 ; n           if (!strcmp(tmp, ans)) { n             int i = 7 + tot; n             while (s[i] !=    ) { n               cout << s[i]; n               i++; n             } n             cout << endl; n             f = 0; n           } n         } n       } n     } n     if (s[1] ==  h ) { n       f = 1; n       tot = 0; n       for (int i = 6; s[i] !=  ) ; i++) ans[tot++] = s[i]; n       ans[tot++] =   0 ; n     } n   } n   if (f) cout <<  Unhandled Exception  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long t; n   cin >> t; n   while (t--) { n     long long n, k, i; n     cin >> n >> k; n     string s(n,  a ); n     long long temp = k; n     i = 1; n     long long x = 0, y = 0; n     while (temp >= i) { n       temp -= i; n       i++; n     } n     if (temp != 0) { n       s[n - i - 1] =  b ; n       s[n - temp] =  b ; n     } else { n       s[n - i + 1] =  b ; n       s[n - i] =  b ; n     } n     cout << s << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[30000]; n int main() { n   int n; n   cin >> n; n   cnt[0] = 1; n   for (long long i = 0; i < n; ++i) { n     int a; n     cin >> a; n     for (long long q = 0; q < 30000 - a; ++q) { n       if (cnt[q] == 1) { n         cnt[q + a] = 1; n       } n     } n   } n   for (int i = 0; i < 30000; ++i) { n     if (cnt[i] == 0) { n       cout << i << endl; n       return 0; n     } n   } n   cout << -1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool rev[35]; n int main() { n   int n, m; n   cin >> m >> n; n   for (int i = 0; i < n; i++) { n     printf( 1 n ); n     fflush(stdout); n     int x; n     scanf( %d , &x); n     if (x == 0) return 0; n     if (x < 0) n       rev[i] = true; n     else n       rev[i] = false; n   } n   int l = 1, r = m; n   for (int i = n; i < 60; i++) { n     int mid = (l + r) / 2; n     printf( %d n , mid); n     fflush(stdout); n     int x; n     scanf( %d , &x); n     if (x == 0) return 0; n     if (x < 0) { n       if (rev[i % n]) { n         l = mid + 1; n       } else n         r = mid - 1; n     } else { n       if (rev[i % n]) { n         r = mid - 1; n       } else n         l = mid + 1; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   bool flag = false; n   while (!isdigit(c)) { n     if (c ==  - ) flag = true; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 1) + (x << 3) + (c ^ 48); n     c = getchar(); n   } n   if (flag) x = -x; n } n int n, q, root = 1; n int a[1000010], mn[4000010], val[4000010], tag[4000010]; n void pushup(int cur) { n   if (mn[(cur << 1)] < mn[(cur << 1 | 1)]) n     mn[cur] = mn[(cur << 1)], val[cur] = val[(cur << 1)]; n   else if (mn[(cur << 1)] > mn[(cur << 1 | 1)]) n     mn[cur] = mn[(cur << 1 | 1)], val[cur] = val[(cur << 1 | 1)]; n   else n     mn[cur] = mn[(cur << 1)], val[cur] = val[(cur << 1)] + val[(cur << 1 | 1)]; n } n void pushtag(int cur, int v) { mn[cur] += v, tag[cur] += v; } n void pushdown(int cur) { n   if (!tag[cur]) return; n   pushtag((cur << 1), tag[cur]), pushtag((cur << 1 | 1), tag[cur]), n       tag[cur] = 0; n } n void update(int l, int r, int pos, int v, int cur) { n   if (l == r) { n     val[cur] += v; n     return; n   } n   pushdown(cur); n   if (pos <= ((l + r) >> 1)) n     update(l, ((l + r) >> 1), pos, v, (cur << 1)); n   else n     update(((l + r) >> 1) + 1, r, pos, v, (cur << 1 | 1)); n   pushup(cur); n } n void modify(int L, int R, int l, int r, int v, int cur) { n   if (L > R) return; n   if (L <= l && R >= r) { n     pushtag(cur, v); n     return; n   } n   pushdown(cur); n   if (L <= ((l + r) >> 1)) modify(L, R, l, ((l + r) >> 1), v, (cur << 1)); n   if (R > ((l + r) >> 1)) n     modify(L, R, ((l + r) >> 1) + 1, r, v, (cur << 1 | 1)); n   pushup(cur); n } n int query(int L, int R, int l, int r, int cur) { n   if (L <= l && R >= r) return mn[cur] == 1 ? val[cur] : 0; n   int v = 0; n   pushdown(cur); n   if (L <= ((l + r) >> 1)) v += query(L, R, l, ((l + r) >> 1), (cur << 1)); n   if (R > ((l + r) >> 1)) n     v += query(L, R, ((l + r) >> 1) + 1, r, (cur << 1 | 1)); n   return v; n } n int main() { n   read(n), read(q), a[0] = 1000001; n   for (int i = 1; i <= n; ++i) read(a[i]), update(0, 1000001, a[i], 1, root); n   for (int i = 0; i <= n; ++i) n     modify(min(a[i], a[i + 1]), max(a[i], a[i + 1]) - 1, 0, 1000001, 1, root); n   while (q--) { n     int x; n     read(x), update(0, 1000001, a[x], -1, root); n     modify(min(a[x], a[x + 1]), max(a[x], a[x + 1]) - 1, 0, 1000001, -1, root); n     modify(min(a[x - 1], a[x]), max(a[x - 1], a[x]) - 1, 0, 1000001, -1, root); n     read(a[x]), update(0, 1000001, a[x], 1, root); n     modify(min(a[x], a[x + 1]), max(a[x], a[x + 1]) - 1, 0, 1000001, 1, root); n     modify(min(a[x - 1], a[x]), max(a[x - 1], a[x]) - 1, 0, 1000001, 1, root); n     printf( %d n , query(1, 1000001 - 1, 0, 1000001, root)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e18; n const long long mod = 1e9 + 7; n void __p(long long x) { cout << x; } n void __p(int32_t x) { cout << x; } n void __p(float x) { cout << x; } n void __p(double x) { cout << x; } n void __p(long double x) { cout << x; } n void __p(char x) { cout <<      << x <<     ; } n void __p(const char *x) { cout <<     << x <<    ; } n void __p(const string &x) { cout <<     << x <<    ; } n void __p(bool x) { cout << (x ?  true  :  false ); } n void _p() { cout <<  ] n ; } n template <typename T, typename V> n void __p(const pair<T, V> &x) { n   cout <<  { ; n   __p(x.first); n   cout <<  , ; n   __p(x.second); n   cout <<  } ; n } n template <typename T> n void __p(const T &x) { n   long long f = 0; n   cout <<  { ; n   for (auto &i : x) cout << (f++ ?  ,  :   ), __p(i); n   cout <<  } ; n } n template <typename T, typename... V> n void _p(T t, V... v) { n   __p(t); n   if (sizeof...(v)) cout <<  ,  ; n   _p(v...); n } n long long po(long long x, long long p) { n   long long ans = 1; n   x %= mod; n   while (p > 0) { n     if (p & 1) ans = (ans * x) % mod; n     p = p >> 1; n     x = (x * x) % mod; n   } n   return ans; n } n long long inv(long long x) { return po(x, mod - 2) % mod; } n const long long N = 1e6 + 6; n long long n, m, k, x, y, z, a, b, c, d, i, j, l, r, f, q; n void sol() { n   string s; n   cin >> n >> s; n   map<long long, long long> m; n   m[0] = 1; n   long long pre = 0; n   long long ans = 0; n   for (i = 0; i < n; i++) { n     pre += s[i] -  0 ; n     ans += m[pre - (i + 1)]; n     m[pre - (i + 1)]++; n   } n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) sol(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   char c = getchar(); n   int x = 0; n   bool f = 0; n   for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45); n   for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); n   if (f) x = -x; n   return x; n } n const int M = 4e6 + 10; n int n, maxn, pp[M], a[M], b[M], cnt, ppp[M]; n int max1, max2, sum1, sum2, vis[M]; n map<int, int> t; n vector<int> e[M]; n void dfs(int u) { n   if (vis[u]) return; n   vis[u] = 1, sum2++; n   max2 = max(max2, u); n   if (max1 < max2) swap(max1, max2); n   for (int i = 0; i < e[u].size(); i++) { n     int to = e[u][i]; n     sum1++; n     dfs(to); n   } n } n signed main() { n   n = read(); n   for (int i = 1; i <= n; i++) n     pp[++cnt] = a[i] = read(), pp[++cnt] = b[i] = read(); n   sort(pp + 1, pp + 1 + cnt); n   cnt = 0; n   for (int i = 1; i <= 2 * n; i++) n     if (pp[i] != pp[i - 1] || i == 1) t[pp[i]] = ++cnt, ppp[cnt] = pp[i]; n   for (int i = 1; i <= n; i++) n     a[i] = t[a[i]], b[i] = t[b[i]], e[a[i]].push_back(b[i]), n     e[b[i]].push_back(a[i]); n   for (int i = 1; i <= cnt; i++) n     if (!vis[i]) { n       max1 = max2 = sum1 = sum2 = 0; n       dfs(i); n       sum1 /= 2; n       if (sum2 == sum1) n         maxn = max(maxn, max1); n       else if (sum2 > sum1) n         maxn = max(maxn, max2); n       else n         return puts( -1 ), 0; n     } n   cout << ppp[maxn] << endl; n   return 0; n } n


#include <bits/stdc++.h> n char s[100] =  bear , str[10000]; n int check(int index) { n   int i; n   for (i = index; i <= index + 3; i++) { n     if (str[i] != s[i - index]) return 0; n   } n   return 1; n } n int main() { n   int len, i, j; n   while (scanf( %s , str) != EOF) { n     int ans = 0, index = 0; n     int len = strlen(str); n     for (i = 0; i <= len - 4; i++) { n       if (check(i)) ans += (i + 1 - index) * (len - (i + 3)), index = i + 1; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct p { n   int L, R, U, D; n } point[5005], pos[5005]; n string g[5005]; n void DEL(int x) { n   pos[pos[x].D].U = pos[x].U; n   pos[pos[x].U].D = pos[x].D; n   pos[pos[x].R].L = pos[x].L; n   pos[pos[x].L].R = pos[x].R; n } n int main() { n   int m, n, i, j, l, sx, sy; n   scanf( %d%d , &m, &n); n   for (i = 0; i < m; i++) cin >> g[i]; n   n = g[0].size(); n   for (i = 0; i < m; i++) { n     for (j = 0; j < n; j++) { n       if (g[i][j] !=  . ) { n         for (l = i - 1; l >= 0; l--) n           if (g[l][j] !=  . ) break; n         if (l < 0) n           point[i * n + j].U = -1; n         else n           point[i * n + j].U = l * n + j; n         for (l = i + 1; l < m; l++) n           if (g[l][j] !=  . ) break; n         if (l >= m) n           point[i * n + j].D = -1; n         else n           point[i * n + j].D = l * n + j; n         for (l = j - 1; l >= 0; l--) n           if (g[i][l] !=  . ) break; n         if (l < 0) n           point[i * n + j].L = -1; n         else n           point[i * n + j].L = i * n + l; n         for (l = j + 1; l < n; l++) n           if (g[i][l] !=  . ) break; n         if (l >= n) n           point[i * n + j].R = -1; n         else n           point[i * n + j].R = i * n + l; n       } n     } n   } n   int r1 = 0, r2 = 0, cnt, t, x, y; n   for (sx = 0; sx < m; sx++) { n     for (sy = 0; sy < n; sy++) n       if (g[sx][sy] !=  . ) { n         for (i = 0; i < m; i++) { n           for (j = 0; j < n; j++) { n             pos[i * n + j] = point[i * n + j]; n           } n         } n         if (g[sx][sy] ==  D ) n           t = pos[sx * n + sy].D; n         else if (g[sx][sy] ==  U ) n           t = pos[sx * n + sy].U; n         else if (g[sx][sy] ==  R ) n           t = pos[sx * n + sy].R; n         else if (g[sx][sy] ==  L ) n           t = pos[sx * n + sy].L; n         DEL(sx * n + sy); n         cnt = 1; n         while (t != -1) { n           cnt++; n           x = t / n; n           y = t % n; n           DEL(t); n           if (g[x][y] ==  D ) n             t = pos[t].D; n           else if (g[x][y] ==  U ) n             t = pos[t].U; n           else if (g[x][y] ==  R ) n             t = pos[t].R; n           else if (g[x][y] ==  L ) n             t = pos[t].L; n         } n         if (cnt > r1) { n           r1 = cnt; n           r2 = 1; n         } else if (cnt == r1) { n           r2++; n         } n       } n   } n   printf( %d %d n , r1, r2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e3 + 40; n int f[maxn][maxn]; n int dp[maxn]; n char s[maxn]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     scanf( %d , &n); n     scanf( %s , s + 1); n     f[n][n + 1] = f[n + 1][n] = f[n + 1][n + 1] = 0; n     for (int i = n; i >= 1; i--) n       for (int j = n; j >= 1; j--) { n         if (s[i] == s[j]) { n           f[i][j] = f[i + 1][j + 1] + 1; n         } else { n           f[i][j] = 0; n         } n       } n     int ans = 0; n     for (int i = 1; i <= n; i++) { n       dp[i] = n - i + 1; n       for (int j = 1; j < i; j++) { n         int len = f[i][j]; n         if (i + len - 1 >= n || s[i + len] <= s[j + len]) continue; n         dp[i] = max(dp[i], dp[j] + n - i + 1 - len); n       } n       ans = max(ans, dp[i]); n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> primes; n bool sums[1003]; n int n, k; n bool pr[1003]; n void sieve() { n   pr[1] = pr[0] = 1; n   for (int i = 2; i <= n; i++) n     if (!pr[i]) { n       for (int j = i + i; j <= n; j += i) pr[j] = 1; n       primes.push_back(i); n     } n   return; n } n int main() { n   cin >> n >> k; n   sieve(); n   for (int i = 1; i < primes.size(); i++) n     if (primes[i] + primes[i - 1] <= n) sums[primes[i] + primes[i - 1]] = 1; n   for (int i = 2; i <= n; ++i) n     if (sums[i - 1] && !pr[i]) k--; n   cout << (k <= 0 ?  YES  :  NO ); n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v(100003); n long long solve() { n   long long n; n   vector<long long> u; n   string s; n   cin >> n; n   while (n > 0) { n     auto itr = lower_bound((v).begin(), (v).end(), n); n     long long a = itr - v.begin(); n     if (v[a] > n) a--; n     u.push_back(a + 2); n     n = n - v[a]; n   } n   s.push_back( 1 ); n   long long j = 1; n   for (long long i = 0; i < u[0]; i++) { n     while (j < u.size() && i + u[j] == u[0]) { n       s.push_back( 1 ); n       j++; n     } n     s.push_back( 3 ); n   } n   s.push_back( 7 ); n   cout << s; n   return 0; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t = 1; n   cin >> t; n   v[0] = 1; n   for (long long i = 1; i < 100003; i++) v[i] = v[i - 1] + i + 1; n   while (t--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 101010; n pair<int, int> arr[MAXN]; n pair<int, int> a[MAXN], b[MAXN]; n int N; n bool cmp(const pair<int, int> &a, const pair<int, int> &b) { n   return a.second < b.second; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> N; n   for (int i = 0; i < N; i++) { n     cin >> arr[i].first; n     arr[i].second = i; n   } n   sort(arr, arr + N); n   int c3 = (N + 1) / 3; n   int c6 = (2 * N + 1) / 3; n   for (int i = 0; i < c3; i++) { n     a[i].first = arr[i].first; n     b[i].first = 0; n   } n   for (int i = c3; i < c6; i++) { n     a[i].first = 0; n     b[i].first = arr[i].first; n   } n   for (int i = c6; i < N; i++) { n     b[i].first = N - i - 1; n     a[i].first = arr[i].first - b[i].first; n   } n   for (int i = 0; i < N; i++) { n     a[i].second = b[i].second = arr[i].second; n   } n   sort(a, a + N, cmp); n   sort(b, b + N, cmp); n   cout <<  YES  << endl; n   for (int i = 0; i < N; i++) cout << a[i].first << (i == N - 1 ?   n  :    ); n   for (int i = 0; i < N; i++) cout << b[i].first << (i == N - 1 ?   n  :    ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const double eps = 1e-9; n const int INF = inf; n const double EPS = eps; n struct __timestamper {}; n class fenv_tree { n   static int f(int x) { return x & -x; } n   vector<int> tr; n  n  public: n   fenv_tree(int n) : tr(n + 1, -inf) {} n   void relax(int x, int v) { n     for (x++; x < ((int)(tr).size()); x += f(x)) tr[x] = max(tr[x], v); n   } n   int get(int x) { n     int res = -inf; n     for (x++; x > 0; x -= f(x)) res = max(res, tr[x]); n     return res; n   } n }; n vector<vector<pair<int, int>>> rests; n int n; n vector<int> rig, spl; n vector<int> ans; n void restore(int v) { n   if (v + 1 < spl[v]) { n     rig[v + 1] = spl[v] - 1; n     restore(v + 1); n   } n   ans.push_back(v); n   if (spl[v] <= rig[v]) { n     rig[spl[v]] = rig[v]; n     restore(spl[v]); n   } n } n bool check() { n   rig = vector<int>(n); n   for (int i = 0; i < (n); i++) rig[i] = i; n   spl = vector<int>(n); n   fenv_tree tr(n); n   for (int i = 0; i < (n); i++) tr.relax(i, rig[i]); n   for (int i = n - 1; i >= 0; i--) { n     for (pair<int, int> r : rests[i]) { n       if (r.first <= i) return false; n       rig[i] = max(rig[i], rig[r.first]); n     } n     rig[i] = tr.get(rig[i]); n     tr.relax(i, rig[i]); n     vector<pair<int, int>> seq; n     for (int i2 = i + 1; i2 <= rig[i];) { n       seq.push_back(make_pair(i2, rig[i2])); n       i2 = rig[i2] + 1; n     } n     int b1 = 0, b2 = ((int)(seq).size()); n     for (pair<int, int> r : rests[i]) { n       int id = upper_bound(seq.begin(), seq.end(), make_pair(r.first, inf)) - n                seq.begin() - 1; n       if (r.second == 0) { n         b1 = max(b1, id + 1); n       } else { n         b2 = min(b2, id); n       } n     } n     if (b1 > b2) return false; n     if (seq.empty()) { n       spl[i] = i + 1; n     } else { n       spl[i] = b1 == ((int)(seq).size()) ? rig[i] + 1 : seq[b1].first; n     } n   } n   ans.clear(); n   rig[0] = n - 1; n   restore(0); n   for (int i = 0; i < (n); i++) printf( %d%c , ans[i] + 1,   n  [i + 1 < n]); n   return true; n } n int main() { n   int m; n   while (scanf( %d%d , &n, &m) == 2) { n     rests = vector<vector<pair<int, int>>>(n); n     for (int i = 0; i < (m); i++) { n       int a, b; n       char ty[10]; n       scanf( %d%d%s , &a, &b, ty), a--, b--; n       rests[a].push_back(make_pair(b, !strcmp(ty,  LEFT ) ? 0 : 1)); n     } n     if (!check()) { n       printf( IMPOSSIBLE n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 151, maxs = 150 * 150 / 4 + 5, inf = 1e9; n vector<vector<int> > d[maxs]; n int a[maxn]; n int n, k, s; n int main() { n   scanf( %d%d%d , &n, &k, &s); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &a[i]); n   } n   if (s >= maxs - 2) { n     sort(a, a + n); n     int sum = 0; n     for (int i = 0; i < k; ++i) { n       sum += a[i]; n     } n     printf( %d , sum); n     return 0; n   } else { n     for (int g = 0; g <= s; ++g) { n       d[g].resize(n - k + 2); n       for (int j = 0; j < n - k + 2; ++j) { n         d[g][j].resize(k + 1, inf); n       } n     } n     d[s][0][k] = 0; n     for (int i = k; i >= 0; --i) { n       for (int j = k; j <= n; ++j) { n         for (int g = s; g >= 0; --g) { n           int j1 = j - k; n           if (i > 0) n             d[g][j1][i - 1] = min(d[g][j1][i - 1], d[g][j1][i] + a[i - 1]); n           if (j < n) d[g][j1 + 1][i] = min(d[g][j1 + 1][i], d[g][j1][i]); n           int st = j - i + 1; n           if (i > 0 && j < n && g - st >= 0) { n             d[g - st][j1 + 1][i - 1] = n                 min(d[g - st][j1 + 1][i - 1], d[g][j1][i] + a[j]); n           } n           if (i > 0 && j < n) { n             d[g][j1 + 1][i - 1] = n                 min(d[g][j1 + 1][i - 1], d[g][j1][i] + a[i - 1]); n           } n         } n       } n     } n     int ans = inf; n     for (int g = 0; g <= s; ++g) { n       ans = min(d[g][n - k][0], ans); n     } n     printf( %d , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     for (int pw = 2; pw < 30; ++pw) { n       int val = (1 << pw) - 1; n       if (n % val == 0) { n         cout << n / val << endl; n         break; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = int(1.5 * 1e7); n const int inf = 1e9 + 7; n const long long ll_inf = 1e18 + 420; n const double eps = 1e-4; n const int N = 1e6; n const int MAX = 2e5 + 9; n const int MOD = 1e9 + 7; n const long double pi = 3.14159265359; n string s; n bool was[10][10]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> s; n   for (int i0 = 0; i0 < s.size(); ++i0) { n     int cnt = 0; n     for (int i = 1; i <= 4; ++i, cnt = 0) { n       for (int j = 1; j <= 4; ++j) { n         cnt += was[i][j]; n       } n       if (cnt == 4) { n         for (int j = 1; j <= 4; ++j) { n           was[i][j] = 0; n         } n       } n     } n     for (int i = 1; i <= 4; ++i, cnt = 0) { n       for (int j = 1; j <= 4; ++j) { n         cnt += was[j][i]; n       } n       if (cnt == 4) { n         for (int j = 1; j <= 4; ++j) { n           was[j][i] = 0; n         } n       } n     } n     if (s[i0] ==  1 ) { n       bool q = 0; n       for (int i = 1; i <= 4; ++i) { n         for (int j = 1; j <= 3; ++j) { n           if (!was[i][j] && !was[i][j + 1]) { n             was[i][j] = 1; n             was[i][j + 1] = 1; n             cout << i <<     << j <<   n ; n             q = 1; n             break; n           } n         } n         if (q) break; n       } n     } else { n       bool q = 0; n       for (int i = 1; i <= 4; ++i) { n         for (int j = 1; j <= 3; ++j) { n           if (!was[j][i] && !was[j + 1][i]) { n             was[j][i] = 1; n             was[j + 1][i] = 1; n             cout << j <<     << i <<   n ; n             q = 1; n             break; n           } n         } n         if (q) break; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int T; n   cin >> T; n   while (T--) { n     long long n, m; n     cin >> n >> m; n     if (m + 1 <= n) { n       cout << 0 << endl; n       continue; n     } n     m++; n     bool flag = false; n     for (int i = 32; i >= 0; --i) { n       int x = n >> i & 1, y = m >> i & 1; n       if (flag) { n         m -= y << i; n         m += x << i; n       } else if (x > y) { n         flag = 1; n         m += (1 << i); n       } n     } n     cout << (n ^ m) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n int n; n int a[maxn]; n int c[maxn]; n int main() { n   int ans = 0; n   int i, j, x; n   scanf( %d , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   int id = 0; n   for (i = 2; i <= n; i++) { n     if (a[i] < a[i - 1]) id = i - 1; n   } n   printf( %d n , id); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 10; n const int MAXN = 1e4 + 10; n const int MOD = 1e9 + 7; n const int inf = 1e9; n const double pi = acos(-1.0); n const double eps = 1e-6; n int dx[] = {0, -1, 0, 1}; n int dy[] = {1, 0, -1, 0}; n long long dp[510][510]; n int n; n int a[510]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) dp[i][i] = 1; n   for (int j = 2; j <= n; j++) n     for (int i = j - 1; i >= 1; i--) { n       dp[i][j] = 0; n       for (int k = i + 1; k <= j; k++) n         if (a[i + 1] < a[k + 1] || k == j) n           (dp[i][j] += (dp[i + 1][k] * dp[k][j]) % MOD) %= MOD; n     } n   cout << dp[1][n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (long long)1e18 + 7; n int n, m; n map<char, int> ch_num; n int main() { n   int n; n   cin >> n; n   vector<string> ee(n); n   for (int i = 0; i < n; i++) { n     cin >> ee[i]; n   } n   vector<int> can_zero(27, -1); n   vector<pair<int, int> > let_sum(27); n   for (int i = 0; i < 26; i++) { n     let_sum[i] = make_pair(0, i); n   } n   for (auto cp : ee) { n     for (int i = 0; i < (int)cp.size(); i++) { n       if (i == 0) { n         can_zero[cp[i] -  a ] = 0; n       } else if (can_zero[cp[i] -  a ] != 0) { n         can_zero[cp[i] -  a ] = 1; n       } n       let_sum[cp[i] -  a ].first += pow(10, (int)cp.size() - i - 1); n     } n   } n   sort(let_sum.begin(), let_sum.end()); n   reverse(let_sum.begin(), let_sum.end()); n   bool z_used = false; n   int cur_n = 1; n   for (auto cp : let_sum) { n     if (can_zero[cp.second] == -1) { n       break; n     } n     if (can_zero[cp.second] && !z_used) { n       ch_num[(char)( a  + cp.second)] = 0; n       z_used = true; n     } else { n       ch_num[(char)( a  + cp.second)] = cur_n; n       cur_n++; n     } n   } n   int ans = 0; n   for (auto hui : ee) { n     for (int i = 0; i < (int)hui.size(); i++) { n       ans += ch_num[hui[i]] * pow(10, (int)hui.size() - i - 1); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   getline(cin, s); n   int k = s.length(); n   int flag = 0; n   for (int d = 0; d < k - 1; d++) { n     if (s[d] ==    ) { n       continue; n     } n     if (s[d] ==  a  || s[d] ==  e  || s[d] ==  i  || s[d] ==  o  || n         s[d] ==  u  || s[d] ==  y  || s[d] ==  A  || s[d] ==  E  || n         s[d] ==  I  || s[d] ==  O  || s[d] ==  U  || s[d] ==  Y ) { n       flag = 1; n     } else { n       flag = 0; n     } n   } n   if (flag == 1) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long int l, r; n   scanf( %ld %ld , &l, &r); n   if (l == r) n     printf( %ld n , r); n   else n     printf( 2 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1000000000000000; n int main() { n   int n, m; n   cin >> n >> m; n   vector<int> x(n); n   for (int i = 0; i < n; i++) { n     cin >> x[i]; n   } n   vector<int> p(m), c(m); n   for (int i = 0; i < m; i++) { n     cin >> p[i] >> c[i]; n   } n   sort(x.begin(), x.end()); n   vector<pair<int, int>> P(m); n   for (int i = 0; i < m; i++) { n     P[i] = make_pair(p[i], c[i]); n   } n   sort(P.begin(), P.end()); n   for (int i = 0; i < m; i++) { n     p[i] = P[i].first; n     c[i] = P[i].second; n   } n   int sum = 0; n   for (int i = 0; i < m; i++) { n     sum += c[i]; n   } n   if (sum < n) { n     cout << -1 << endl; n   } else { n     vector<long long> dp(n + 1, INF); n     dp[0] = 0; n     for (int i = 0; i < m; i++) { n       vector<long long> d(n); n       for (int j = 0; j < n; j++) { n         d[j] = abs(x[j] - p[i]); n       } n       vector<long long> S(n + 1); n       S[0] = 0; n       for (int j = 0; j < n; j++) { n         S[j + 1] = S[j] + d[j]; n       } n       vector<long long> a(n + 1); n       for (int j = 0; j <= n; j++) { n         a[j] = dp[j] - S[j]; n       } n       deque<int> dq; n       for (int j = 0; j <= n; j++) { n         while (!dq.empty()) { n           if (a[dq.back()] >= a[j]) { n             dq.pop_back(); n           } else { n             break; n           } n         } n         dq.push_back(j); n         dp[j] = a[dq.front()] + S[j]; n         if (dq.front() == j - c[i]) { n           dq.pop_front(); n         } n       } n     } n     cout << dp[n] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n vector<int> g[N]; n int a[N], b[N], last[N], indx[N], lvl[N], ans[N]; n int n, m, q; n void dfs(int node, int sz) { n   lvl[sz++] = node; n   for (int i = 0; i < g[node].size(); i++) { n     dfs(g[node][i], sz); n   } n   if (sz >= n) n     ans[node] = lvl[sz - n]; n   else n     ans[node] = N; n   sz--; n } n int tree[N << 2]; n void build(int i, int l, int r) { n   if (l == r) return void(tree[i] = ans[l]); n   int mid = (l + r) >> 1; n   build(i << 1, l, mid); n   build(i << 1 | 1, mid + 1, r); n   tree[i] = min(tree[i << 1], tree[i << 1 | 1]); n } n int get(int i, int l, int r, int a, int b) { n   if (l > b || r < a) return N; n   if (l >= a && r <= b) return tree[i]; n   int mid = (l + r) >> 1; n   return min(get(i << 1, l, mid, a, b), get(i << 1 | 1, mid + 1, r, a, b)); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> q; n   for (int i = 0; i < n; i++) cin >> a[i], indx[a[i]] = i; n   for (int i = 0; i < m; i++) cin >> b[i]; n   memset(last, -1, sizeof last); n   vector<int> roots; n   for (int i = m - 1; i >= 0; i--) { n     int x = a[(indx[b[i]] + 1) % n]; n     if (last[x] == -1) n       roots.push_back(i); n     else n       g[last[x]].push_back(i); n     last[b[i]] = i; n   } n   for (auto r : roots) dfs(r, 0); n   build(1, 0, m - 1); n   for (int i = 0, l, r; i < q; i++) { n     cin >> l >> r; n     l--, r--; n     cout << (get(1, 0, m - 1, l, r) <= r); n   } n   return 0; n } n


// n //  main.cpp n //  google-code-jam-2020 n // n //  Created by Ali Naderi Parizi on 2/10/1400 AP. n // n  n #include <iostream> n #include <math.h> n  n int main(int argc, const char * argv[]) n { n     int t; n     std::cin >> t; n     for (int i = 0 ; i < t ; i++) n     { n         double r,b,d; n         std::cin >> r >> b >> d; n         double x = std::min(r, b) * d; n  n         if(std::abs(r - b) <= x) n         { n             std::cout <<  YES  << std::endl; n         } n         else n         { n             std::cout <<  NO  << std::endl; n         } n     } n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int q = 1; n   while (q--) { n     long long int n, i, t, j, k = 0, maxm = 0, ans; n     cin >> n; n     vector<long long int> v(n), v1(30), v2; n     map<long long int, long long int> m; n     for (long long int i = 0; i < 30; i++) v1[i] = 0; n     for (long long int i = 0; i < n; i++) { n       cin >> v[i]; n       t = v[i]; n       j = 0; n       while (t > 0) { n         v1[j] = v1[j] + t % 2; n         t = t / 2; n         j++; n       } n     } n     sort(v.begin(), v.end()); n     for (long long int i = 0; i < 30; i++) { n       if (v1[i] == 1) v2.push_back(i); n     } n     if (v2.size() == 0) { n       for (i = n - 1; i >= 0; i--) cout << v[i] <<    ; n       return 0; n     } n     for (long long int i = 0; i < n; i++) { n       t = v[i]; n       j = 0; n       k = 0; n       while (t > 0) { n         if (j == v2[k]) { n           if (t % 2 != 0) m[v[i]] += pow(2, j); n           k++; n         } n         t = t / 2; n         j++; n       } n     } n     for (auto it = m.begin(); it != m.end(); it++) { n       if ((it->second) > maxm) { n         maxm = it->second; n         ans = it->first; n       } n     } n     cout << ans <<    ; n     for (long long int i = 0; i < n; i++) { n       if (v[i] == ans) n         continue; n       else n         cout << v[i] <<    ; n     } n   } n } n