#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n int l[100005], r[100005], fr[100005], to[100005]; n int f[100005]; n vector<int> v[600000]; n int fa[100005], it[100005]; n int getfa(int x) { return (x == fa[x]) ? x : (fa[x] = getfa(fa[x])); } n void merge(int x, int y) { n   if (x == y) return; n   x = getfa(x); n   y = getfa(y); n   if (x == y) return; n   l[x] = min(l[x], l[y]); n   r[x] = max(r[x], r[y]); n   fa[y] = x; n } n void update(int x, int l, int r, int tl, int tr, int p) { n   if (l > tr || tl > r) return; n   if (l >= tl && r <= tr) { n     v[x].emplace_back(p); n     return; n   } n   int mid = (l + r) >> 1; n   update(x << 1, l, mid, tl, tr, p); n   update((x << 1) | 1, mid + 1, r, tl, tr, p); n } n void get(int x, int l, int r, int p, int id) { n   bool f = v[x].empty(); n   for (auto u : v[x]) merge(u, id); n   v[x].clear(); n   if (!f) v[x].emplace_back(id); n   if (l == r) return; n   int mid = (l + r) >> 1; n   if (p <= mid) n     get(x << 1, l, mid, p, id); n   else n     get((x << 1) | 1, mid + 1, r, p, id); n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   vector<int> v; n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> f[i]; n     if (f[i] == 1) { n       cin >> l[i] >> r[i]; n       v.emplace_back(l[i]); n       v.emplace_back(r[i]); n     } else { n       cin >> fr[i] >> to[i]; n     } n   } n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n   for (int i = 1; i <= n; i++) { n     if (f[i] == 2) continue; n     l[i] = lower_bound(v.begin(), v.end(), l[i]) - v.begin() + 1; n     r[i] = lower_bound(v.begin(), v.end(), r[i]) - v.begin() + 1; n   } n   int len = v.size(), cnt = 0; n   for (int i = 1; i <= n; i++) { n     if (f[i] == 1) { n       fa[i] = i; n       it[++cnt] = i; n       if (l[i] < r[i] - 1) update(1, 1, len, l[i] + 1, r[i] - 1, i); n       get(1, 1, len, l[i], i); n       get(1, 1, len, r[i], i); n     } else { n       int t1 = getfa(it[fr[i]]), t2 = getfa(it[to[i]]); n       if (l[t1] > l[t2] && l[t1] < r[t2] || r[t1] > l[t2] && r[t1] < r[t2] || n           t1 == t2) { n         cout <<  YES n ; n       } else n         cout <<  NO n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 100010, mod = 998244353; n int n, fac[maxn], ifac[maxn]; n long long pw(long long a, int n = mod - 2) { n   long long b = 1; n   for (; n; n >>= 1) n& 1 ? b = b * a % mod : 1, a = a * a % mod; n   return b; n } n struct edge { n   int to; n   edge* next; n } E[maxn * 2], *ne = E, *fir[maxn]; n void link(int u, int v) { n   *ne = (edge){v, fir[u]}; n   fir[u] = ne++; n } n int fa[maxn], siz[maxn], son[maxn]; n void init(int i) { n   siz[i] = 1; n   for (edge* e = fir[i]; e; e = e->next) n     if (e->to != fa[i]) { n       fa[e->to] = i; n       init(e->to); n       if (siz[e->to] > siz[son[i]]) son[i] = e->to; n       siz[i] += siz[e->to]; n     } n } n int cs[maxn]; n std::vector<int> ps[maxn], f[maxn]; n bool cmp(int i, int j) { return siz[i] < siz[j]; } n std::vector<int> add(std::vector<int> a, std::vector<int> b) { n   std::vector<int> c; n   for (int i = 0; i < a.size() || i < b.size(); i++) n     c.push_back(((i < a.size() ? a[i] : 0) + (i < b.size() ? b[i] : 0)) % mod); n   return c; n } n int ws[1 << 17]; n void dft(int* a, int n, bool r = 0) { n   int* w = ws + n / 2; n   w[0] = 1; n   w[1] = pw(3, (mod - 1) / n); n   if (r) w[1] = pw(w[1]); n   for (int i = 2; i < n / 2; i++) w[i] = 1ll * w[1] * w[i - 1] % mod; n   for (int i = n / 2; --i;) ws[i] = ws[i * 2]; n   w = ws + 1; n   for (int i = 0, j = 0, t; i < n; i++) { n     if (i < j) t = a[i], a[i] = a[j], a[j] = t; n     for (t = n / 2; (j ^= t) < t; t /= 2) n       ; n   } n   for (int i = 1; i < n; w += i, i *= 2) { n     for (int j = 0; j < n; j += i * 2) { n       for (int k = 0, t; k < i; k++) { n         t = 1ll * a[i + j + k] * w[k] % mod; n         a[i + j + k] = (a[j + k] + mod - t) % mod; n         a[j + k] = (a[j + k] + t) % mod; n       } n     } n   } n   if (r) { n     long long I = pw(n); n     for (int i = 0; i < n; i++) a[i] = a[i] * I % mod; n   } n } n int A[1 << 17], B[1 << 17]; n std::vector<int> mul(std::vector<int> a, std::vector<int> b) { n   int n = a.size() - 1, m = b.size() - 1, N = 1; n   while (N <= n + m) N *= 2; n   for (int i = 0; i < N; i++) A[i] = i > n ? 0 : a[i], B[i] = i > m ? 0 : b[i]; n   dft(A, N); n   dft(B, N); n   for (int i = 0; i < N; i++) A[i] = 1ll * A[i] * B[i] % mod; n   dft(A, N, 1); n   std::vector<int> c; n   for (int i = 0; i <= n + m; i++) c.push_back(A[i]); n   return c; n } n std::pair<std::vector<int>, std::vector<int> > calc( n     std::vector<std::vector<int> >& a, int l, int r) { n   if (r - l == 1) return std::make_pair(a[l], a[l]); n   int pl = l, pr = r, sl = 0, sr = 0; n   while (pl < pr) sl < sr ? sl += a[pl++].size() - 1 : sr += a[--pr].size() - 1; n   std::pair<std::vector<int>, std::vector<int> > fl = calc(a, l, pl), n                                                  fr = calc(a, pl, r); n   return std::make_pair(mul(fl.first, fr.first), n                         add(fl.second, mul(fl.first, fr.second))); n } n void dc(int i) { n   for (int o = i; o; o = son[o]) n     for (edge* e = fir[o]; e; e = e->next) n       if (e->to != fa[o] && e->to != son[o]) dc(e->to); n   std::vector<std::vector<int> > a; n   for (int o = i; o; o = son[o]) { n     int *cl = cs, *cr = cs; n     std::vector<int>*pl = ps, *pr = ps; n     for (edge* e = fir[o]; e; e = e->next) n       if (e->to != fa[o] && e->to != son[o]) *cr++ = e->to; n     std::sort(cs, cr, cmp); n     for (int t = cr - cs; --t > 0;) { n       std::vector<int>* p[2]; n       for (int j = 0; j < 2; j++) { n         if (pl == pr || cl < cr && siz[*cl] < pl->size()) n           p[j] = f + *cl++; n         else n           p[j] = pl++; n       } n       *pr++ = mul(*p[0], *p[1]); n     } n     std::vector<int>&g = cl < cr ? f[*cl] : *pl, h; n     h.push_back(0); n     if (cl == cr && pl == pr) n       h.push_back(1); n     else n       for (int j = 0; j < g.size(); j++) h.push_back(g[j]); n     a.push_back(h); n   } n   f[i] = calc(a, 0, a.size()).second; n   f[i][0] = 1; n } n int main() { n   long long x; n   scanf( %d%lld , &n, &x); n   x %= mod; n   for (int i = 1, u, v; i < n; i++) n     scanf( %d%d , &u, &v), link(u, v), link(v, u); n   for (int i = *fac = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod; n   ifac[n] = pw(fac[n]); n   for (int i = n; i; i--) ifac[i - 1] = 1ll * ifac[i] * i % mod; n   init(1); n   dc(1); n   int s = 0; n   for (int i = 1, w = 1; i <= n; i++) n     s = (s + 1ll * w * ifac[i - 1] % mod * f[1][i]) % mod, n     w = w * (x + i) % mod; n   printf( %d n , s); n } n


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int n, x; n   while (cin >> n) { n     map<int, int> a; n     for (int i = 0; i < n; i++) { n       cin >> x; n       if (x != 0) a[x]++; n     } n     int ans = 0, ans1 = 0, ans2 = 0; n     for (map<int, int>::iterator it = a.begin(); it != a.end(); it++) { n       if (it->second == 2) n         ans++; n       else if (it->second < 2) n         ans1++; n       else n         ans2++; n     } n     if (ans2 == 0 && ans > 0) n       cout << ans << endl; n     else if (ans2 == 0 && ans == 0) n       cout << 0 << endl; n     else n       cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int z[10000], ans[10000], size[10000], v[10000]; n long long a[10004]; n int vis[10004]; n int root(int i) { n   while (z[i] != i) i = z[i]; n   return i; n } n void union1(int i, int j) { n   i = root(i); n   j = root(j); n   if (i != j) { n     if (size[i] > size[j]) { n       size[i] += size[j]; n       z[j] = i; n     } else { n       size[j] += size[i]; n       z[i] = j; n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     size[i] = 1; n     z[i] = i; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       if (a[i] == 0 || a[j] == 0) continue; n       long long w = a[i] * a[j]; n       if (w >= 0) { n         double w1 = sqrt(w); n         long long w2 = w1; n         w2 = w2 * w2; n         if (w2 == w) union1(i, j); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (a[i] != 0) v[i] = root(i); n   } n   for (int i = 1; i <= n; i++) { n     int cnt = 0; n     for (int j = 1; j <= n; j++) vis[j] = 0; n     if (a[i] == 0) { n       cnt = 0; n       ans[1]++; n     } else { n       vis[v[i]] = 1; n       cnt = 1; n       ans[cnt]++; n     } n     for (int j = i + 1; j <= n; j++) { n       if (a[j] != 0) { n         if (vis[v[j]] == 0) { n           vis[v[j]] = 1; n           cnt++; n         } n       } n       if (cnt != 0) n         ans[cnt]++; n       else n         ans[1]++; n     } n   } n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int mat[(int)1e3][(int)1e3], n; n int dp2[(int)1e3][(int)1e3]; n int dp5[(int)1e3][(int)1e3]; n int changeRow[] = {1, 0}; n int changeCol[] = {0, 1}; n int pw2[(int)1e3][(int)1e3], pw5[(int)1e3][(int)1e3]; n string dir[] = { D ,  R }; n int pwr(int n, int k) { n   int ret = 0; n   while (n >= k and n % k == 0) { n     ret++; n     n /= k; n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (int x = 0; x < n; x++) n     for (int y = 0; y < n; y++) cin >> mat[x][y]; n   bool hasZero = 0; n   pair<int, int> pos; n   for (int x = 0; x < n; x++) { n     for (int y = 0; y < n; y++) { n       if (mat[x][y] == 0) { n         pos = make_pair(x, y); n         hasZero = true; n         break; n       } n     } n     if (hasZero) break; n   } n   for (int x = 0; x < n; x++) { n     for (int y = 0; y < n; y++) { n       pw2[x][y] = pwr(mat[x][y], 2); n       pw5[x][y] = pwr(mat[x][y], 5); n     } n   } n   for (int x = 0; x < n; x++) { n     for (int y = 0; y < n; y++) { n       dp2[x][y] = 2e9; n       dp5[x][y] = 2e9; n     } n   } n   dp2[0][0] = pw2[0][0]; n   for (int x = 0; x < n; x++) { n     for (int y = 0; y < n; y++) { n       for (int z = 0; z < 2; z++) { n         int X = x + changeRow[z]; n         int Y = y + changeCol[z]; n         if (X == x and Y == y) continue; n         if (X >= 0 and Y >= 0 and X < n and Y < n) { n           dp2[X][Y] = min(dp2[X][Y], pw2[X][Y] + dp2[x][y]); n         } n       } n     } n   } n   dp5[0][0] = pw5[0][0]; n   for (int x = 0; x < n; x++) { n     for (int y = 0; y < n; y++) { n       for (int z = 0; z < 2; z++) { n         int X = x + changeRow[z]; n         int Y = y + changeCol[z]; n         if (X == x and Y == y) continue; n         if (X >= 0 and Y >= 0 and X < n and Y < n) { n           dp5[X][Y] = min(dp5[X][Y], pw5[X][Y] + dp5[x][y]); n         } n       } n     } n   } n   int res2 = dp2[n - 1][n - 1]; n   int res5 = dp5[n - 1][n - 1]; n   int res = min(res2, res5); n   string ans =   ; n   if (hasZero && res > 1) { n     cout <<  1 n ; n     for (int x = 0; x < pos.first; x++) cout <<  D ; n     for (int x = 0; x < pos.second; x++) cout <<  R ; n     for (int x = pos.first; x < n - 1; x++) cout <<  D ; n     for (int x = pos.second; x < n - 1; x++) cout <<  R ; n   } else if (res == res2) { n     cout << res <<   n ; n     int i = n - 1, j = n - 1; n     while (!(i == 0 and j == 0)) { n       for (int z = 0; z < 2; z++) { n         int X = i - changeRow[z]; n         int Y = j - changeCol[z]; n         if (X == i and Y == j) continue; n         if (X >= 0 and Y >= 0 and X < n and Y < n) { n           if (dp2[i][j] == dp2[X][Y] + pw2[i][j]) { n             ans += dir[z]; n             i = X; n             j = Y; n             break; n           } n         } n       } n     } n     assert(i == 0 and j == 0); n     reverse(ans.begin(), ans.end()); n     for (auto x : ans) cout << x; n     cout <<   n ; n   } else { n     cout << res <<   n ; n     assert(res == res5); n     int i = n - 1, j = n - 1; n     while (!(i == 0 and j == 0)) { n       for (int z = 0; z < 2; z++) { n         int X = i - changeRow[z]; n         int Y = j - changeCol[z]; n         if (X == i and Y == j) continue; n         if (X >= 0 and Y >= 0 and X < n and Y < n) { n           if (dp5[i][j] == dp5[X][Y] + pw5[i][j]) { n             ans += dir[z]; n             i = X; n             j = Y; n             break; n           } n         } n       } n     } n     assert(i == 0 and j == 0); n     reverse(ans.begin(), ans.end()); n     for (auto x : ans) cout << x; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int N = 5e5 + 10; n vector<int> v[31]; n long long par[31], X[31], Y[31], vis[31]; n int xxx = 31; n void bfs() { n   queue<tuple<int, int, int>> qu; n   qu.push(make_tuple(1, 0, xxx)); n   vis[1] = 1; n   X[1] = Y[1] = 1ll << xxx; n   while (!qu.empty()) { n     tuple<int, int, int> xx = qu.front(); n     qu.pop(); n     int s = get<0>(xx); n     int dd = get<1>(xx); n     int pp = get<2>(xx); n     int dir = 1; n     for (auto it : v[s]) { n       if (vis[it]) continue; n       vis[it] = 1; n       if (dir == dd) dir++; n       if (dir == 1) { n         X[it] = X[s] + (1ll << (pp - 1)); n         Y[it] = Y[s]; n         qu.push(make_tuple(it, 2, pp - 1)); n         dir++; n       } else if (dir == 2) { n         X[it] = X[s] - (1ll << (pp - 1)); n         Y[it] = Y[s]; n         qu.push(make_tuple(it, 1, pp - 1)); n         dir++; n       } else if (dir == 3) { n         X[it] = X[s]; n         Y[it] = Y[s] - (1ll << (pp - 1)); n         qu.push(make_tuple(it, 4, pp - 1)); n         dir++; n       } else { n         X[it] = X[s]; n         Y[it] = Y[s] + (1ll << (pp - 1)); n         qu.push(make_tuple(it, 3, pp - 1)); n         dir++; n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   int flag = 0; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     v[a].push_back(b); n     v[b].push_back(a); n     if (v[a].size() > 4 || v[b].size() > 4) flag = 1; n   } n   if (flag) n     puts( NO ); n   else { n     bfs(); n     cout <<  YES n ; n     for (int i = 1; i <= n; i++) cout << X[i] <<     << Y[i] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 233, mxzt = (1 << 20) + 23; n int f[21][mxzt], tmp[2][mxzt][20]; n int stv[maxn]; n int i, j, k, n, m; n char s[maxn]; n int ra, fh; n char rx; n inline int read() { n   rx = getchar(), ra = 0, fh = 1; n   while (rx < 48) rx ==  -  && (fh = -1), rx = getchar(); n   while (rx > 47) ra = ra * 10 + rx - 48, rx = getchar(); n   return ra * fh; n } n inline int getnum1(int x) { n   int n = 0; n   while (x) n += x & 1, x >>= 1; n   return n; n } n inline int min(int a, int b) { return a < b ? a : b; } n int main() { n   n = read(), m = read(); n   int mx = 1 << n; n   for (i = 1; i <= n; i++) { n     scanf( %s , s + 1); n     for (j = 1; j <= m; j++) stv[j] = stv[j] << 1 | (s[j] - 48); n   } n   for (i = 1; i <= m; i++) f[0][stv[i]]++; n   int zt, p, nf; n   bool now = 1, pre = 0; n   int *nt; n   for (i = 1; i <= n; i++, swap(now, pre)) { n     for (zt = 0; zt < mx; zt++) { n       for (nf = p = 0, nt = tmp[now][zt]; p < n; p++, nt++) n         nf += (*nt = f[i - 1][zt ^ (1 << p)] - tmp[pre][zt ^ (1 << p)][p]); n       f[i][zt] = nf / i; n     } n   } n   int ans = 1e9; n   for (zt = 0; zt < mx; zt++) { n     k = 0; n     for (j = 0; j <= n && k < ans; j++) k += min(j, n - j) * f[j][zt]; n     if (j > n) ans = k; n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n int p; n struct seg { n   std::vector<int64_t> cuts; n   int64_t sum; n   int64_t operator()(int64_t x) const { n     return x + sum - n            p * int64_t(std::upper_bound(begin(cuts), end(cuts), x) - n                        begin(cuts)); n   } n }; n seg operator*(seg const& a, seg const& b) { n   for (unsigned i = 1; i < a.cuts.size(); ++i) n     assert(a.cuts[i] - a.cuts[i - 1] >= p); n   for (unsigned i = 1; i < b.cuts.size(); ++i) n     assert(b.cuts[i] - b.cuts[i - 1] >= p); n   seg out; n   out.sum = a.sum + b.sum; n   out.cuts.reserve(a.cuts.size() + b.cuts.size()); n   int64_t x = INT64_MIN / 2; n   auto iter1 = begin(a.cuts); n   auto iter2 = begin(b.cuts); n   while (true) { n     auto x1 = x + a.sum - p * int64_t(iter1 - begin(a.cuts)); n     auto x2 = x1 + b.sum - p * int64_t(iter2 - begin(b.cuts)); n     assert(iter1 == std::upper_bound(begin(a.cuts), end(a.cuts), x)); n     assert(a(x) == x1); n     assert(iter2 == std::upper_bound(begin(b.cuts), end(b.cuts), x1)); n     assert(b(x1) == x2); n     auto d1 = iter1 == end(a.cuts) ? INT64_MAX : *iter1 - x; n     auto d2 = iter2 == end(b.cuts) ? INT64_MAX : *iter2 - x1; n     assert(d1 > 0); n     assert(d2 > 0); n     if (d1 != INT64_MAX) n       assert(iter1 != std::upper_bound(begin(a.cuts), end(a.cuts), x + d1)); n     if (d2 < d1) { n       assert(iter1 == std::upper_bound(begin(a.cuts), end(a.cuts), x + d2)); n       assert(a(x + d2) == x1 + d2); n       assert(iter2 != std::upper_bound(begin(b.cuts), end(b.cuts), x1 + d2)); n       x += d2; n       out.cuts.push_back(x); n       ++iter2; n     } else if (d1 == INT64_MAX) { n       break; n     } else { n       x += d1; n       ++iter1; n       auto new_x1 = x1 + d1 - p; n       assert(a(x) == new_x1); n       assert(std::upper_bound(begin(b.cuts), end(b.cuts), new_x1) <= iter2); n       if (iter2 != begin(b.cuts) and iter2[-1] > new_x1) { n         --iter2; n         assert(std::upper_bound(begin(b.cuts), end(b.cuts), new_x1) == iter2); n       } else { n         out.cuts.push_back(x); n       } n     } n   } n   assert(out.cuts.size() == a.cuts.size() + b.cuts.size()); n   return out; n } n std::vector<seg> st; n int main() { n   std::ios::sync_with_stdio(0); n   std::cin.tie(0); n   int n, nquery; n   std::cin >> n >> nquery >> p; n   st.resize(n * 2); n   for (int i = n; i < 2 * n; ++i) { n     int ai; n     std::cin >> ai; n     st[i].cuts.push_back(p - ai); n     st[i].sum = ai; n   } n   for (int i = n; --i;) st[i] = st[i << 1] * st[i << 1 | 1]; n   while (nquery--) { n     int l, r; n     std::cin >> l >> r; n     --l; n     l += n; n     r += n; n     int64_t out = 0; n     std::vector<seg*> pendr; n     while (l < r) { n       if (l & 1) out = st[l++](out); n       if (r & 1) pendr.push_back(&(st[--r])); n       l >>= 1; n       r >>= 1; n     } n     while (not pendr.empty()) { n       out = (*pendr.back())(out); n       pendr.pop_back(); n     } n     std::cout << out <<   n ; n   } n } n


#include <bits/stdc++.h> n int main() { n   char s[5200] = {0}, next[5200] = {0}, p[5200] = {0}, t[5200] = {0}; n   while (scanf( %s , s + 1) != EOF) { n     s[0] =  a ; n     double sum = 0; n     int f = 0; n     while (sscanf(s,  %*[^0-9]%[0-9.]%[^ 0] , p, next) > 0) { n       double num1 = 0, num2 = 0; n       int len = strlen(p), flag = 0; n       if (len > 3 && p[len - 3] ==  . ) { n         sscanf(p + len - 3,  %lf , &num1); n         flag = f = 1; n       } n       if (flag == 1) len = len - 3; n       for (int i = 0; i < len; i++) { n         if (p[i] ==  . ) continue; n         num2 = num2 * 10 + (p[i] -  0 ); n       } n       sum += num1 + num2; n       strcpy(s, next); n       memset(next, 0, sizeof(next)); n     } n     sprintf(t,  %.2f , sum); n     int len = strlen(t), k = 0; n     char ans[5200]; n     if (f == 0) len -= 3; n     if (f == 1) { n       ans[k++] = t[--len]; n       ans[k++] = t[--len]; n       ans[k++] = t[--len]; n       if (ans[0] ==  0  && ans[1] ==  0 ) k = 0; n     } n     for (int i = len - 1, counts = 1; i >= 0; i--, counts++) { n       if (counts % 3 == 1 && counts != 1) ans[k++] =  . ; n       ans[k++] = t[i]; n     } n     for (int i = k - 1; i >= 0; i--) printf( %c , ans[i]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, num[100100]; n inline int as(int u) { n   int a, b; n   if (num[u]) return num[u]; n   printf( ? %d n , u); n   fflush(stdout); n   scanf( %d , &a); n   return num[u] = a; n } n inline void pd(int u) { n   if (as(u) == as(u + m)) { n     printf( ! %d n , u); n     exit(0); n   } n } n int main() { n   int i, j, l, r, mid, p, q; n   cin >> n; n   m = n / 2; n   if (m % 2) { n     puts( ! -1 ); n     return 0; n   } n   as(1), as(m + 1), pd(1); n   l = 2, r = m; n   for (; l <= r;) { n     mid = ((l + r) >> 1); n     p = as(mid), q = as(mid + m); n     pd(mid); n     if ((p > q) ^ (num[1] > num[m + 1])) n       r = mid - 1; n     else n       l = mid + 1; n   } n } n


#include <bits/stdc++.h> n long num[1000000]; n int main() { n   long long ans; n   long i, j, k, n; n   long long max1, max2; n   scanf( %ld , &n); n   ans = 0; n   for (i = 1; i <= n; i++) { n     scanf( %ld , &num[i]); n     ans += num[i]; n   } n   max1 = 0; n   max2 = 0; n   for (i = 1; i <= n; i++) { n     scanf( %ld , &num[i]); n     if (num[i] > max1) { n       max2 = max1; n       max1 = num[i]; n     } else { n       if (num[i] > max2) { n         max2 = num[i]; n       } n     } n   } n   if (max1 + max2 < ans) { n     printf( NO n ); n   } else { n     printf( YES n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n long long n, m, Now, Ans, i; n long long sumi(long long a, long long b); n int main() { n   scanf( %I64d %I64d , &n, &m); n   Now = sumi(2, m); n   if (Now == 0) n     Now = 1000000009 - 1; n   else n     Now--; n   Ans = 1; n   for (i = 1; i <= n; i++) { n     Ans *= Now; n     Ans %= 1000000009; n     if (Now == 0) n       Now = 1000000009 - 1; n     else n       Now--; n   } n   printf( %I64d n , Ans); n   return 0; n } n long long sumi(long long a, long long b) { n   long long ret = 1, i, c = a; n   for (i = 1; i <= b; i += i) { n     if ((b & i) > 0) { n       ret *= c; n       ret %= 1000000009; n     } n     c *= c; n     c %= 1000000009; n   } n   return ret; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n, m, val, i, j = 0, sum = 0, k = 0; n   vector<long> toys; n   vector<long> selected; n   cin >> n >> m; n   for (i = 0; i < n; i++) { n     cin >> val; n     toys.push_back(val); n   } n   sort(toys.begin(), toys.end()); n   for (i = 0; i < 1000000000; i++) { n     if (j < (long)toys.size()) { n       if (toys[j] == (i + 1)) { n         j++; n       } else { n         if (sum + i + 1 <= m) { n           selected.push_back(i + 1); n           sum += i + 1; n           k++; n         } else { n           break; n         } n       } n     } else if (sum + i + 1 <= m) { n       selected.push_back(i + 1); n       sum += i + 1; n       k++; n     } else { n       break; n     } n   } n   cout << k << endl; n   for (auto t : selected) { n     cout << t <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5; n const long long inf = 0x3f3f3f3f3f3f3f3f; n int n, m, q, u[N], v[N], w[N]; n long long d[2][N]; n bool vis[N]; n vector<pair<long long, int> > g[N]; n pair<long long, int> pre[N]; n void dij(int s, long long d[]) { n   for (int i = (1); i < (n + 1); i++) d[i] = inf, vis[i] = 0; n   d[s] = 0; n   priority_queue<pair<long long, int> > q; n   q.push(pair<long long, int>(0, s)); n   while (!q.empty()) { n     int u = q.top().second; n     q.pop(); n     if (vis[u]) continue; n     vis[u] = 1; n     for (auto it : g[u]) { n       if (d[u] + w[it.second] < d[it.first]) { n         d[it.first] = d[u] + w[it.second]; n         pre[it.first] = pair<long long, int>(u, it.second); n         q.push(pair<long long, int>(-d[it.first], it.first)); n       } n     } n   } n } n int k[N], e[N]; n bool used[N], in[N]; n vector<long long> ad[N], del[N]; n long long mn[N]; n int main() { n   scanf( %d%d%d , &n, &m, &q); n   for (int i = (1); i < (m + 1); i++) { n     scanf( %d%d%d , &u[i], &v[i], &w[i]); n     g[u[i]].push_back(pair<long long, int>(v[i], i)); n     g[v[i]].push_back(pair<long long, int>(u[i], i)); n   } n   dij(n, d[1]); n   dij(1, d[0]); n   vector<int> vec; n   for (int i = (2); i < (n + 1); i++) used[pre[i].second] = 1; n   pre[1] = pair<long long, int>(0, 0); n   for (int i = n; i >= 1; i = pre[i].first) n     in[pre[i].second] = 1, vec.push_back(i); n   int cnt = 0; n   reverse(vec.begin(), vec.end()); n   for (auto v : vec) k[v] = ++cnt; n   for (int i = (1); i < (n + 1); i++) { n     if (d[0][i] == inf) continue; n     if (!k[i]) { n       int u = i; n       for (; !k[u]; u = pre[u].first) n         ; n       for (int v = i; !k[v]; v = pre[v].first) k[v] = k[u]; n     } n   } n   for (int i = (1); i < (m + 1); i++) { n     if (used[i]) continue; n     int l = k[u[i]], r = k[v[i]]; n     if (l == r) continue; n     if (l > r) swap(u[i], v[i]), swap(l, r); n     ad[l].push_back(d[0][u[i]] + w[i] + d[1][v[i]]); n     del[r].push_back(d[0][u[i]] + w[i] + d[1][v[i]]); n   } n   multiset<long long> S; n   S.insert(inf); n   for (int i = (1); i < (cnt); i++) { n     for (auto v : del[i]) S.erase(S.find(v)); n     for (auto v : ad[i]) S.insert(v); n     mn[i] = *(S.begin()); n   } n   int x, y; n   while (q--) { n     scanf( %d%d , &x, &y); n     long long ans = min(d[0][u[x]] + d[1][v[x]], d[0][v[x]] + d[1][u[x]]) + y; n     if (!in[x]) { n       ans = min(ans, d[0][n]); n     } else { n       ans = min(ans, mn[min(k[u[x]], k[v[x]])]); n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> graph[200002]; n int visited[200002], sub[200002]; n int subsize(int root) { n   visited[root] = 1; n   for (int child : graph[root]) { n     if (!visited[child]) sub[root] += subsize(child); n   } n   return sub[root]; n } n void destroy(int root, int par) { n   for (int child : graph[root]) { n     if (child != par && (sub[child] % 2 == 0)) destroy(child, root); n   } n   printf( %d n , root); n   for (int child : graph[root]) { n     if (child != par && (sub[child] & 1)) destroy(child, root); n   } n } n int main() { n   for (int i = 0; i < 200002; i++) sub[i] = 1; n   int n, x; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x); n     if (x) { n       graph[x].push_back(i); n       graph[i].push_back(x); n     } n   } n   if (n % 2 == 0) n     printf( NO ); n   else { n     subsize(1); n     printf( YES n ); n     destroy(1, 0); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, crush[100005], ans = -1; n bool exi[100005]; n long long ms(int o, int x, int p) { n   if (p > 2 * n) return -1; n   if (crush[x] == x) return p ? -1 : 0; n   if ((x == o) && p) return 0; n   int t = ms(o, crush[x], p + 1); n   return (t < 0 ? t : t + 1); n } n long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); } n long long lcm(long long x, long long y) { return x / gcd(x, y) * y; } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &crush[i]); n     exi[crush[i]] = 1; n   } n   for (int i = 1; i <= n; i++) { n     if (!exi[i]) { n       ans = -1; n       break; n     } n     long long tmp = ms(i, i, 0); n     if (tmp <= 0) tmp = 1; n     tmp /= (2 - (tmp & 1)); n     ans = ans < 0 ? tmp : lcm(ans, tmp); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Array[100005], Left[100005]; n int Right[100005]; n int main() { n   int N, i; n   scanf( %d , &N); n   for (i = 1; i <= N; ++i) { n     scanf( %d , Array + i); n     Left[i] = min(Left[i - 1] + 1, Array[i]); n   } n   for (i = N; i >= 1; --i) Right[i] = min(Right[i + 1] + 1, Array[i]); n   int Ans = 0; n   for (i = 1; i <= N; ++i) Ans = max(Ans, min(Left[i], Right[i])); n   printf( %d n , Ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<int, int> > di = {{2, 1},   {1, 2},   {-1, 2}, {-2, 1}, n                               {-2, -1}, {-1, -2}, {1, -2}, {2, -1}}; n int n, m; n int X1, Y1, X2, Y2; n queue<pair<int, int> > q1, q2; n bool isValid(pair<int, int> cell) { n   return (1 <= cell.first && cell.first <= n && 1 <= cell.second && n           cell.second <= m); n } n int dist1[1009][1009]; n int dist2[1009][1009]; n pair<int, int> dad1[1009][1009]; n pair<int, int> dad2[1009][1009]; n bool was1[1009][1009]; n bool was2[1009][1009]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   cin >> X1 >> Y1 >> X2 >> Y2; n   q1.push({n / 2, m / 2}); n   dist1[n / 2][m / 2] = 0; n   was1[n / 2][m / 2] = true; n   while (!q1.empty()) { n     pair<int, int> cur = q1.front(); n     q1.pop(); n     for (pair<int, int> d : di) { n       pair<int, int> neighbour; n       neighbour.first = cur.first + d.first; n       neighbour.second = cur.second + d.second; n       if (!isValid(neighbour)) continue; n       if (was1[neighbour.first][neighbour.second]) continue; n       dist1[neighbour.first][neighbour.second] = n           dist1[cur.first][cur.second] + 1; n       dad1[neighbour.first][neighbour.second] = cur; n       was1[neighbour.first][neighbour.second] = true; n       q1.push(neighbour); n     } n   } n   q2.push({n / 2 + 1, m / 2}); n   dist2[n / 2 + 1][m / 2] = 0; n   was2[n / 2 + 1][m / 2] = true; n   while (!q2.empty()) { n     pair<int, int> cur = q2.front(); n     q2.pop(); n     for (pair<int, int> d : di) { n       pair<int, int> neighbour; n       neighbour.first = cur.first + d.first; n       neighbour.second = cur.second + d.second; n       if (!isValid(neighbour)) continue; n       if (was2[neighbour.first][neighbour.second]) continue; n       dist2[neighbour.first][neighbour.second] = n           dist2[cur.first][cur.second] + 1; n       dad2[neighbour.first][neighbour.second] = cur; n       was2[neighbour.first][neighbour.second] = true; n       q2.push(neighbour); n     } n   } n   if (dist1[X1][Y1] < dist1[X2][Y2] && dist1[X1][Y1] <= dist2[X2][Y2]) { n     cout <<  WHITE  << endl; n     fflush(stdout); n     while (1) { n       pair<int, int> uj = dad1[X1][Y1]; n       X1 = uj.first; n       Y1 = uj.second; n       cout << X1 <<     << Y1 << endl; n       fflush(stdout); n       if (X1 == n / 2 && Y1 == m / 2) return 0; n       cin >> X2 >> Y2; n       if (X2 == -1 && Y2 == -1) return 0; n     } n   } n   if (dist2[X2][Y2] + 1 < dist2[X1][Y1] && dist2[X2][Y2] < dist1[X1][Y1]) { n     cout <<  BLACK  << endl; n     fflush(stdout); n     cin >> X1 >> Y1; n     if (X1 == -1 && Y1 == -1) return 0; n     while (1) { n       pair<int, int> uj = dad2[X2][Y2]; n       X2 = uj.first; n       Y2 = uj.second; n       cout << X2 <<     << Y2 << endl; n       fflush(stdout); n       if (X2 == n / 2 + 1 && Y2 == m / 2) return 0; n       cin >> X1 >> Y1; n       if (X1 == -1 && Y1 == -1) return 0; n     } n   } n   if ((X1 + Y1) % 2 != (X2 + Y2) % 2) { n     cout <<  WHITE  << endl; n     fflush(stdout); n     bool reached = false; n     if (X1 == n / 2 + 1 && Y1 == m / 2) { n       reached = true; n     } n     while (1) { n       if (reached) break; n       pair<int, int> uj = dad2[X1][Y1]; n       X1 = uj.first; n       Y1 = uj.second; n       cout << X1 <<     << Y1 << endl; n       fflush(stdout); n       if (X1 == X2 && Y1 == Y2) return 0; n       if (X1 == n / 2 + 1 && Y1 == m / 2) break; n       cin >> X2 >> Y2; n       if (X2 == -1 && Y2 == -1) return 0; n     } n     if (!reached) cin >> X2 >> Y2; n     int ddx = abs(X1 - X2); n     int ddy = abs(Y1 - Y2); n     if ((ddx == 1 && ddy == 2) || (ddx == 2 && ddy == 1)) { n       cout << X2 <<     << Y2 << endl; n       fflush(stdout); n       return 0; n     } n     cout << (n / 2) <<     << (m / 2) + 2 << endl; n     fflush(stdout); n     if (n / 2 == X2 && m / 2 + 2 == Y2) return 0; n     cin >> X2 >> Y2; n     cout << (n / 2) + 2 <<     << (m / 2) + 1 << endl; n     fflush(stdout); n     if (n / 2 + 2 == X2 && m / 2 + 1 == Y2) return 0; n     cin >> X2 >> Y2; n     cout << (n / 2) <<     << (m / 2) << endl; n     fflush(stdout); n     return 0; n   } else { n     cout <<  BLACK  << endl; n     fflush(stdout); n     cin >> X1 >> Y1; n     if (X1 == -1 && Y1 == -1) return 0; n     int dx = abs(X2 - (n / 2 + 1)); n     int dy = abs(Y2 - m / 2); n     if ((dx == 1 && dy == 2) || (dx == 2 && dy == 1)) { n       cout << n / 2 + 1 <<     << m / 2 << endl; n       fflush(stdout); n       return 0; n     } n     bool reached = false; n     if (X2 == n / 2 && Y2 == m / 2) { n       reached = true; n     } n     while (1) { n       if (reached) break; n       pair<int, int> uj = dad1[X2][Y2]; n       X2 = uj.first; n       Y2 = uj.second; n       cout << X2 <<     << Y2 << endl; n       fflush(stdout); n       if (X1 == X2 && Y1 == Y2) return 0; n       if (X2 == n / 2 && Y2 == m / 2) break; n       cin >> X1 >> Y1; n       if (X1 == -1 && Y1 == -1) return 0; n     } n     if (!reached) cin >> X1 >> Y1; n     int ddx = abs(X1 - X2); n     int ddy = abs(Y1 - Y2); n     if ((ddx == 1 && ddy == 2) || (ddx == 2 && ddy == 1)) { n       cout << X1 <<     << Y1 << endl; n       fflush(stdout); n       return 0; n     } n     cout << (n / 2) + 2 <<     << (m / 2) + 1 << endl; n     fflush(stdout); n     if (n / 2 + 2 == X1 && m / 2 + 1 == Y1) return 0; n     cin >> X1 >> Y1; n     cout << (n / 2) <<     << (m / 2) + 2 << endl; n     fflush(stdout); n     if (n / 2 == X1 && m / 2 + 2 == Y1) return 0; n     cin >> X1 >> Y1; n     cout << (n / 2) + 1 <<     << (m / 2) << endl; n     fflush(stdout); n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, t; n int dp[21][5][11][11][2]; n int solve(int pos, int h, int mn, int mx, bool w) { n   if (mn > t - 1 || mx > t) return 0; n   if (pos == n) return mx == t && mn == t - 1; n   int &ref = dp[pos][h][mn][mx][w]; n   if (ref != -1) return ref; n   ref = 0; n   for (int i = 1; i < 5; ++i) { n     if (i == h) continue; n     bool wp = i > h; n     if (!pos) n       ref += solve(pos + 1, i, mn, mx, w); n     else if (pos == 1) n       ref += solve(pos + 1, i, mn, mx, wp); n     else n       ref += solve(pos + 1, i, mn + (!w & wp), mx + (w & !wp), wp); n   } n   return ref; n } n int main() { n   scanf( %d %d , &n, &t); n   memset(dp, -1, sizeof(dp)); n   int sol = solve(0, 0, 0, 0, 0); n   printf( %d n , sol); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using uint = unsigned; n using ll = long long; n using ull = unsigned long long; n using vi = vector<int>; n using vvi = vector<vi>; n using vl = vector<ll>; n using vvl = vector<vl>; n using vd = vector<double>; n using vvd = vector<vd>; n using vs = vector<string>; n template <typename T1, typename T2> n ostream& operator<<(ostream& os, const pair<T1, T2>& p) { n   return os <<  (  << p.first <<  ,  << p.second <<  ) ; n } n template <typename Tuple> n void print_tuple(ostream&, const Tuple&) {} n template <typename Car, typename... Cdr, typename Tuple> n void print_tuple(ostream& os, const Tuple& t) { n   print_tuple<Cdr...>(os, t); n   os << (sizeof...(Cdr) ?  ,  :   ) << get<sizeof...(Cdr)>(t); n } n template <typename... Args> n ostream& operator<<(ostream& os, const tuple<Args...>& t) { n   print_tuple<Args...>(os <<  ( , t); n   return os <<  ) ; n } n template <typename Ch, typename Tr, typename C> n basic_ostream<Ch, Tr>& operator<<(basic_ostream<Ch, Tr>& os, const C& c) { n   os <<  [ ; n   for (auto i = begin(c); i != end(c); ++i) n     os << (i == begin(c) ?    :    ) << *i; n   return os <<  ] ; n } n constexpr int INF = 1e9; n constexpr int MOD = 1e9 + 7; n constexpr double EPS = 1e-9; n int main() { n   for (int n; cin >> n && n;) { n     vi a(n); n     for (int i = int(0); i < int(n); i++) cin >> a[i]; n     ll mx = *max_element(begin(a), end(a)), n        sum = accumulate(begin(a), end(a), 0ll); n     cout << max<ll>(2 * mx - sum, 0) + 1 << endl; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n using ii = pair<int, int>; n using ll = long long; n const int mod = 1e9 + 7; n const int N = 1e6 + 5; n const int M = 29; n int ans[N]; n int dp[3][N]; n struct suffixAutomaton { n   struct state { n     int len; n     int link; n     int next[M]; n     ll cnt; n     bool terminal; n     state() { n       len = 0, link = -1, cnt = 0; n       terminal = false; n       for (int i = 0; i < M; i++) next[i] = -1; n     } n   }; n   int get(char c) { n     if (c ==  @ ) return 0; n     if (c ==  # ) return 1; n     if (c ==  $ ) return 2; n     return 3 + c -  a ; n   } n   vector<state> st; n   int sz, last, l; n   char offset =  a ; n   suffixAutomaton() {} n   int pos[3]; n   int getLen(int x) { n     if (~x) return st[x].len; n     return 0; n   } n   suffixAutomaton(string s) { n     int l = s.length(); n     st.resize(2 * l); n     for (int i = 0; i < 2 * l; i++) st[i] = state(); n     sz = 1, last = 0; n     st[0].len = 0; n     st[0].link = -1; n     for (int i = 0; i < l; i++) { n       addChar(get(s[i])); n     } n     for (int i = last; i != -1; i = st[i].link) st[i].terminal = true; n     vector<vector<int>> adj(2 * l); n     memset(dp, -1, sizeof dp); n     for (int i = 0; i < sz; i++) { n       ans[getLen(st[i].link) + 1] += n           solve(0, i) * 1ll * solve(1, i) * 1ll * solve(2, i) % mod; n       ans[getLen(i) + 1] -= n           solve(0, i) * 1ll * solve(1, i) * 1ll * solve(2, i) % mod; n       if (ans[getLen(st[i].link) + 1] >= mod) n         ans[getLen(st[i].link) + 1] -= mod; n       if (ans[getLen(i) + 1] < 0) ans[getLen(i) + 1] += mod; n     } n   } n   int solve(int f, int idx) { n     if (idx == pos[f]) return 1; n     if (idx == pos[0] || idx == pos[1] || idx == pos[2]) return 0; n     int &ret = dp[f][idx]; n     if (~ret) return ret; n     ret = 0; n     for (int i = 0; i < M; i++) n       if (~st[idx].next[i]) { n         ret += solve(f, st[idx].next[i]); n         if (ret >= mod) ret -= mod; n       } n     return ret; n   } n   void addChar(int c) { n     int cur = sz++; n     assert(cur < N * 2); n     if (c < 3) pos[c] = cur; n     st[cur].len = st[last].len + 1; n     st[cur].cnt = 1; n     int p = last; n     while (p != -1 and st[p].next[c] == -1) { n       st[p].next[c] = cur; n       p = st[p].link; n     } n     last = cur; n     if (p == -1) { n       st[cur].link = 0; n       return; n     } n     int q = st[p].next[c]; n     if (st[q].len == st[p].len + 1) { n       st[cur].link = q; n       return; n     } n     int clone = sz++; n     for (int i = 0; i < M; i++) st[clone].next[i] = st[q].next[i]; n     st[clone].link = st[q].link; n     st[clone].len = st[p].len + 1; n     st[clone].cnt = 0; n     while (p != -1 and st[p].next[c] == q) { n       st[p].next[c] = clone; n       p = st[p].link; n     } n     st[q].link = st[cur].link = clone; n     return; n   } n   bool contains(string &t) { n     int cur = 0; n     for (int i = 0; i < t.length(); i++) { n       cur = st[cur].next[t[i] - offset]; n       if (cur == -1) return false; n     } n     return true; n   } n   ll numberOfSubstrings() { n     ll res = 0; n     for (int i = 1; i < sz; i++) res += st[i].len - st[st[i].link].len; n     return res; n   } n   void numberOfOccPreprocess() { n     vector<ii> v; n     for (int i = 1; i < sz; i++) v.push_back(make_pair(st[i].len, i)); n     sort(v.begin(), v.end(), greater<ii>()); n     for (int i = 0; i < sz - 1; i++) { n       int suf = st[v[i].second].link; n       st[suf].cnt += st[v[i].second].cnt; n     } n   } n   ll numberOfOcc(string &t) { n     int cur = 0; n     for (int i = 0; i < t.length(); i++) { n       cur = st[cur].next[t[i] - offset]; n       if (cur == -1) return 0; n     } n     return st[cur].cnt; n   } n }; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   string a, b, c; n   cin >> a >> b >> c; n   suffixAutomaton SA(a +  @  + b +  #  + c +  $ ); n   int mn = min(a.size(), min(b.size(), c.size())); n   int cur = 0; n   for (int i = 1; i <= mn; i++) { n     cur += ans[i]; n     if (cur >= mod) cur -= mod; n     if (cur < 0) cur += mod; n     cout << cur <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace IO { n char buf[1 << 22], *p1 = buf, *p2 = buf; n template <typename T> n inline void read(T &x) { n   x = 0; n   register char ch = n       (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n            ? EOF n            : *p1++); n   while (!(ch >= 48 && ch <= 57)) n     ch = n         (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n              ? EOF n              : *p1++); n   while ((ch >= 48 && ch <= 57)) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = n         (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) n              ? EOF n              : *p1++); n   } n } n template <typename T, typename... Args> n inline void read(T &t, Args &...args) { n   read(t); n   read(args...); n } n }  // namespace IO n using namespace IO; n const int N = 1e5 + 10, inf = 0x3f3f3f3f; n struct ALTXDY { n   int tp, t, x, y, id; n   bool operator<(ALTXDY const &a) const { n     return x == a.x ? tp < a.tp : x < a.x; n   } n }; n ALTXDY q[N << 1]; n int n, m, tot, bus[N], ans[N], c[N << 1], dat[N << 2], ls[N << 2], rs[N << 2]; n void update(int &i, int l, int r, int x, int k) { n   if (!i) i = ++tot; n   if (bus[dat[i]] < bus[k]) dat[i] = k; n   if (l == r) return; n   int mid = (l + r) >> 1; n   if (x <= mid) n     update(ls[i], l, mid, x, k); n   else n     update(rs[i], mid + 1, r, x, k); n } n int query(int i, int l, int r, int x, int y, int val) { n   if (l == r) return dat[i]; n   int mid = (l + r) >> 1, t = -1; n   if (x <= mid && ls[i] && bus[dat[ls[i]]] >= val) n     t = query(ls[i], l, mid, x, y, val); n   if (t == -1 && y >= mid + 1 && rs[i] && bus[dat[rs[i]]] >= val) n     t = query(rs[i], mid + 1, r, x, y, val); n   return t; n } n int main() { n   read(n, m); n   for (int i = 1; i <= n; i++) { n     int s, f, t; n     read(s, f, t); n     q[i] = (ALTXDY){0, t, s, f, i}; n     c[i] = t; n     bus[i] = f; n   } n   for (int i = 1; i <= m; i++) { n     int b, l, r; n     read(l, r, b); n     q[i + n] = (ALTXDY){1, b, l, r, i}; n     c[i + n] = b; n   } n   sort(c + 1, c + 1 + n + m); n   register int k = unique(c + 1, c + 1 + n + m) - c - 1; n   for (int i = 1; i <= n + m; i++) n     q[i].t = lower_bound(c + 1, c + 1 + k, q[i].t) - c; n   sort(q + 1, q + 1 + n + m); n   register int rt = 0; n   for (int i = 1; i <= n + m; i++) n     if (q[i].tp == 0) n       update(rt, 1, k, q[i].t, q[i].id); n     else n       ans[q[i].id] = query(rt, 1, k, q[i].t, k, q[i].y); n   for (int i = 1; i <= m; i++) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, ini_pos; n vector<int> v[2020]; n int a[2020], MAX; n map<int, int> mp; n int dist(int a, int b) { n   if (a < b) swap(a, b); n   return min(a - b, n - a + b); n } n void input() { n   scanf( %d %d , &n, &ini_pos); n   for (int i = 1; i <= n; i++) scanf( %d , a + i), mp[a[i]] = 0; n   for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) { n     mp[it->first] = ++MAX; n   } n   for (int i = 1; i <= n; i++) a[i] = mp[a[i]], v[a[i]].push_back(i); n } n int f[2020], en[2020]; n int same[2020], Same[2020], nxt[2020]; n void prepare() { n   v[0].push_back(ini_pos); n   for (int m = MAX; m >= 0; m--) { n     if (m < MAX) { n       for (int i, j = 0; j < v[m].size(); j++) { n         i = v[m][j]; n         int t = m ? i : 0; n         en[t] = -1; n         for (int ii, jj = 0; jj < v[m + 1].size(); jj++) { n           ii = v[m + 1][jj]; n           int d = dist(i, ii); n           if (en[t] == -1 || en[t] > d + f[ii]) { n             nxt[t] = ii; n             en[t] = d + f[ii]; n           } n         } n       } n     } n     if (!m) break; n     int s = v[m].size(); n     for (int ii, i, j = 0; j < s; j++) { n       i = v[m][j]; n       ii = v[m][(j + 1) % s]; n       same[i] = +1; n       Same[i] = ii; n       f[i] = en[ii] + (n - (ii >= i ? ii - i : ii - i + n)); n       if (ii == i) { n         f[i] = en[ii]; n         continue; n       } n       ii = v[m][(j - 1 + s) % s]; n       int ff = en[ii] + (n - (i >= ii ? i - ii : i - ii + n)); n       if (f[i] > ff) f[i] = ff, same[i] = -1, Same[i] = ii; n     } n   } n } n void move(int a, int b) { n   int d = dist(a, b); n   if (d == b - a || d == b + n - a) n     printf( %+d n , d); n   else n     printf( %+d n , -d); n } n void a_a(int s, int cur, int type) { n   if (type == -1) reverse(v[s].begin(), v[s].end()); n   for (int I = -1, i = 0; i < v[s].size(); i = (i + 1) % (int(v[s].size()))) { n     int t = v[s][i]; n     if (I == -1) { n       if (t == cur) I = i; n       continue; n     } n     if (I == i) return; n     move(cur, t); n     cur = t; n   } n } n void solve() { n   int cur = ini_pos; n   printf( %d n , en[0]); n   for (int s = 0; s < MAX;) { n     int t = nxt[s ? cur : 0]; n     move(cur, t); n     cur = t; n     ++s; n     a_a(s, cur, -same[cur]); n     cur = Same[cur]; n   } n } n int main() { n   input(); n   prepare(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[201000]; n long long vis[201000]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     scanf( %d , &n); n     for (int i = 1; i <= n; i++) scanf( %lld , &a[i]), vis[i] = 0; n     int m; n     scanf( %d , &m); n     for (int i = 1; i <= m; i++) { n       long long x, y; n       scanf( %lld%lld , &x, &y); n       vis[y] = max(vis[y], x); n     } n     for (int i = n - 1; i >= 1; i--) { n       if (vis[i] < vis[i + 1]) vis[i] = vis[i + 1]; n     } n     long long ans = 1; n     long long num = 0, co = 0; n     int vis1 = 0; n     for (int i = 1; i <= n; i++) { n       num++; n       if (vis[num] >= max(co, a[i])) { n         co = max(co, a[i]); n       } else { n         ans++; n         num--; n         if (vis[1] < a[i]) vis1 = 1; n         num = 1, co = a[i]; n       } n     } n     if (vis1) n       printf( -1 n ); n     else n       printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int k; n   cin >> k; n   if (k == 0) return cout <<  + n , 0; n   vector<string> ats = { ++ ,  +* }; n   while (ats.size() < (1 << k)) { n     auto cp = ats; n     for (auto& s : cp) n       for (auto& ch : s) ch = (ch ==  +  ?  *  :  + ); n     for (int i = 0; i < cp.size(); i++) { n       ats.push_back(ats[i] + cp[i]); n       ats[i] += ats[i]; n     } n   } n   for (auto& s : ats) cout << s <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int t[107][107]; n int mini; n int a; n int maxi; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     mini = 1000000009; n     for (int j = 1; j <= m; j++) { n       scanf( %d , &a); n       if (a < mini) { n         mini = a; n       } n     } n     if (mini > maxi) maxi = mini; n   } n   printf( %d , maxi); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n const int N = 100005; n struct Node { n   char c; n   int pos; n   Node() {} n   Node(char _c, int _pos) : c(_c), pos(_pos) {} n   bool operator<(const Node& rhs) const { n     if (c != rhs.c) return c < rhs.c; n     return pos < rhs.pos; n   } n }; n Node p[N]; n vector<int> q[26]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   string s; n   cin >> s; n   for (int i = 0; i < s.size(); ++i) { n     p[i].c = s[i]; n     p[i].pos = i + 1; n   } n   int len = s.size(); n   sort(p, p + len); n   for (int i = 0; i < len; ++i) { n     q[p[i].c -  a ].push_back(p[i].pos); n   } n   for (int i = 1; i <= n; ++i) { n     string t; n     cin >> t; n     LL res = 0; n     for (int i = 0; i < t.size(); ++i) { n       if (q[t[i] -  a ].size() == 0) { n         res += t.size(); n       } else { n         int d = 0; n         int pos = n             lower_bound(q[t[i] -  a ].begin(), q[t[i] -  a ].end(), i + 1) - n             q[t[i] -  a ].begin(); n         if (pos == q[t[i] -  a ].size()) n           d = abs(i + 1 - q[t[i] -  a ][pos - 1]); n         else if (pos == 0) { n           d = abs(i + 1 - q[t[i] -  a ][pos]); n         } else { n           d = min(abs(i + 1 - q[t[i] -  a ][pos - 1]), n                   abs(i + 1 - q[t[i] -  a ][pos])); n         } n         res += d; n       } n     } n     printf( %lld n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, maxi = 0, sum = 0; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(NULL); n   int x; n   cin >> x; n   int one = 0, zero = 0; n   string s; n   cin >> s; n   for (int i = 0; i < x; i++) { n     if (s[i] -  0  == 1) n       one++; n     else n       zero++; n   } n   cout << abs(one - zero); 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   long long a; n   cin >> a; n   long long m = 0; n   vector<long long> c; n   long long b[a]; n   for (int i = 0; i < a; i++) { n     cin >> b[i]; n   } n   for (int y = 0; y < a; y++) { n     m = 0; n     for (int i = 0; i < a; i++) { n       long long x = abs(i - y) + i + y; n       x = x * b[i] * 2; n       m = m + x; n     } n     c.push_back(m); n   } n   sort(c.begin(), c.end()); n   cout << c[0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n struct node { n   int mx[11], mn[11], num; n   friend bool operator<(const node &A, const node &B) { n     int ok = 1; n     for (int i = 1; i <= k; i++) ok &= A.mx[i] < B.mn[i]; n     return ok; n   } n   friend node operator+(const node &A, const node &B) { n     node C; n     C.num = A.num + B.num; n     for (int i = 1; i <= k; i++) n       C.mx[i] = max(A.mx[i], B.mx[i]), C.mn[i] = min(A.mn[i], B.mn[i]); n     return C; n   } n }; n inline int read(register int a = 0, register char ch = getchar()) { n   while (ch <  0  || ch >  9 ) ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     a = (a << 3) + (a << 1) + (ch ^ 48), ch = getchar(); n   return a; n } n set<node> QAQ; n int main() { n   n = read(), k = read(); n   for (int i = 1; i <= n; i++) { n     node tmp; n     tmp.num = 1; n     for (int j = 1; j <= k; j++) tmp.mx[j] = tmp.mn[j] = read(); n     set<node>::iterator it = QAQ.find(tmp); n     while (it != QAQ.end()) { n       tmp = tmp + (*it); n       QAQ.erase(it); n       it = QAQ.find(tmp); n     } n     QAQ.insert(tmp); n     printf( %d n , (*QAQ.rbegin()).num); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n; n   string s; n   int finish_x, finish_y; n   cin >> n >> s >> finish_x >> finish_y; n   if (n < abs(finish_x) + abs(finish_y) || n       n % 2 != (abs(finish_x) + abs(finish_y)) % 2) { n     cout << -1; n     return 0; n   } n   vector<int> dx(n + 1), dy(n + 1); n   dx[n] = dy[n] = 0; n   map<char, pair<int, int>> m; n   m[ U ] = {0, 1}; n   m[ D ] = {0, -1}; n   m[ L ] = {-1, 0}; n   m[ R ] = {1, 0}; n   for (int i = n - 1; i >= 0; --i) { n     auto &[cdx, cdy] = m[s[i]]; n     dx[i] = dx[i + 1] + cdx; n     dy[i] = dy[i + 1] + cdy; n   } n   vector<int> x(n + 1), y(n + 1); n   x[0] = y[0] = 0; n   for (int i = 0; i < n; ++i) { n     auto [cdx, cdy] = m[s[i]]; n     x[i + 1] = x[i] + cdx; n     y[i + 1] = y[i] + cdy; n   } n   int l = -1, r = n; n   while (r - l > 1) { n     int m = (l + r) / 2; n     bool good = false; n     for (int i = 0; i <= n - m; ++i) { n       int u = x[i] + dx[i + m]; n       int v = y[i] + dy[i + m]; n       if (abs(finish_x - u) + abs(finish_y - v) <= m) { n         good = true; n         break; n       } n     } n     if (good) n       r = m; n     else n       l = m; n   } n   cout << r; n } n


#include <bits/stdc++.h> n using namespace std; n long double eps = (long double)1 / 1e9; n long long inf = LLONG_MAX, mod1 = 1e9 + 7, mod2 = 998244353; n long long sqr(long long a) { return a * a; } n long long qb(long long a) { return a * a * a; } n long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); } n long long binpow(long long a, long long b, long long mod) { n   return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod n                     : sqr(binpow(a, b / 2, mod)) % mod) n            : 1; n } n long long binmult(long long a, long long b, long long mod) { n   return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod n                     : (2 * binmult(a, b / 2, mod)) % mod) n            : 0; n } n long long b[51], kl[27]; n bool used[51]; n char ans[51]; n int main() { n   cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0); n   long long q, n, i, j, ps, kt; n   string a; n   cin >> q; n   while (q--) { n     cin >> a >> n; n     ps = 25; n     for (i = 0; i < 26; i++) kl[i] = 0; n     for (i = 0; i < a.size(); i++) kl[a[i] -  a ]++; n     for (i = 1; i <= n; i++) cin >> b[i], used[i] = 0; n     while (1) { n       kt = 0; n       for (i = 1; i <= n; i++) n         if (!b[i] && !used[i]) kt++; n       if (!kt) break; n       while (kl[ps] < kt) ps--; n       vector<long long> vc; n       for (i = 1; i <= n; i++) n         if (!b[i] && !used[i]) used[i] = 1, vc.push_back(i); n       for (auto f : vc) { n         ans[f] =  a  + ps; n         for (j = 1; j <= n; j++) n           if (!used[j]) b[j] -= abs(j - f); n       } n       ps--; n     } n     for (i = 1; i <= n; i++) cout << ans[i]; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const long long infLL = 0x3f3f3f3f3f3f3f3fLL; n const int hash_mod = 1000037; n const double eps = 1e-10; n const double pi = acos(-1); n const int maxn = 5000 + 5; n int n, m; n char s[maxn]; n int f[maxn][maxn], g[maxn]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) { n     scanf( %s , s + 1); n     for (int j = 1; j <= m; ++j) n       if (s[j] ==  1 ) f[i][j] = f[i][j - 1] + 1; n   } n   int res = 0; n   for (int j = 1; j <= m; ++j) { n     memset(g, 0, sizeof(g)); n     for (int i = 0; i < n; ++i) g[f[i][j]]++; n     int t = 0; n     for (int i = j; i > 0; --i) { n       t += g[i]; n       res = max(res, i * t); n     } n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<long long> s; n void dfs(int a, int b, long long sum, int d, int& n) { n   if (d >= 0) { n     if (sum <= n && sum > 0) { n       s.insert(sum); n     } n   } n   if (d == 0) { n     return; n   } n   dfs(a, b, sum * 10 + a, d - 1, n); n   dfs(a, b, sum * 10 + b, d - 1, n); n } n int main() { n   int n; n   cin >> n; n   int c = 0, nn = n; n   while (nn) { n     nn /= 10; n     ++c; n   } n   for (int i = 0; i <= 9; ++i) { n     for (int j = i + 1; j <= 9; ++j) { n       dfs(i, j, 0, c, n); n     } n   } n   cout << s.size() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 53e4, mod = 998244353; n int n, nn, L, A, B, ans, fac[N], inv[N], a[N], b[N], c[N], R[N]; n int C(int a, int b) { return 1ll * fac[a] * inv[b] % mod * inv[a - b] % mod; } n int qpow(int a, int b) { n   int ret = 1; n   while (b) { n     if (b & 1) ret = 1ll * ret * a % mod; n     a = 1ll * a * a % mod, b >>= 1; n   } n   return ret; n } n void init(int n) { n   nn = 1, L = 0; n   while (nn <= 2 * n) nn <<= 1, ++L; n   for (int i = 1; i < nn; ++i) R[i] = (R[i >> 1] >> 1) | ((i & 1) << L - 1); n } n void NTT(int* a, int tp) { n   for (int i = 0; i < nn; ++i) n     if (i < R[i]) swap(a[i], a[R[i]]); n   for (int i = 1; i < nn; i <<= 1) { n     int wn = qpow(3, tp * (mod / (i * 2)) + mod - 1); n     for (int j = 0; j < nn; j += i * 2) n       for (int k = 0, w = 1; k < i; ++k, w = 1ll * w * wn % mod) { n         int x = a[j + k], y = 1ll * w * a[j + k + i] % mod; n         a[j + k] = (x + y) % mod; n         a[j + k + i] = (x - y + mod) % mod; n       } n   } n   if (tp == 1) return; n   int invn = qpow(nn, mod - 2); n   for (int i = 0; i < nn; ++i) a[i] = 1ll * a[i] * invn % mod; n } n void solve(int n) { n   if (n == 1) { n     a[1] = 1; n     return; n   } n   if (n & 1) { n     solve(n - 1); n     for (int i = n - 1; ~i; --i) n       a[i + 1] = (a[i + 1] + a[i]) % mod, a[i] = 1ll * (n - 1) * a[i] % mod; n     return; n   } n   int l = n / 2; n   solve(l), init(l); n   for (int i = 0; i < nn; ++i) b[i] = c[i] = 0; n   int tmp = 1; n   for (int i = 0; i <= l; ++i) n     b[i] = 1ll * a[i] * fac[i] % mod, c[i] = 1ll * tmp * inv[i] % mod, n     tmp = 1ll * tmp * l % mod; n   reverse(b, b + l + 1); n   NTT(b, 1), NTT(c, 1); n   for (int i = 0; i < nn; ++i) b[i] = 1ll * b[i] * c[i] % mod; n   NTT(b, -1); n   reverse(b, b + l + 1); n   for (int i = 0; i <= l; ++i) b[i] = 1ll * inv[i] * b[i] % mod; n   for (int i = l + 1; i < nn; ++i) b[i] = 0; n   init(n); n   NTT(a, 1), NTT(b, 1); n   for (int i = 0; i < nn; ++i) a[i] = 1ll * a[i] * b[i] % mod; n   NTT(a, -1); n } n int main() { n   scanf( %d%d%d , &n, &A, &B); n   if (n == 1) { n     if (A == 1 && B == 1) n       puts( 1 ); n     else n       puts( 0 ); n     return 0; n   } n   if (A < 1 || B < 1 || A + B > n + 1 || A + B == 2) { n     puts( 0 ); n     return 0; n   } n   fac[0] = inv[0] = 1; n   for (int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % mod; n   inv[n] = qpow(fac[n], mod - 2); n   for (int i = n; i; --i) inv[i - 1] = 1ll * inv[i] * i % mod; n   solve(n - 1); n   ans = 1ll * a[A + B - 2] * C(A + B - 2, A - 1) % mod; n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int n, c[25], coin[25]; n int ans = 0; n bool can; n int p_b; n bool good() { n   int cnt = 0; n   for (int i = 0; i < 25; i++) cnt += coin[i]; n   return cnt == 1; n } n void rec(int s, int r, int p) { n   if (r == 1) { n     if (c[p] > 1) coin[c[p]]++; n     p++; n     if (p < n) r = c[p]; n     s = 1; n   } n   if (p == n && good()) can = true; n   for (int i = s; i < c[p] - 1; i++) { n     for (int j = 0; j <= coin[i]; j++) { n       if (r - i * j >= 1) { n         coin[i] -= j; n         rec(i + 1, r - i * j, p); n         coin[i] += j; n       } n       if (can) return; n     } n   } n   if (r == 1) { n     p--; n     if (c[p] > 1) coin[c[p]]--; n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &c[i]); n     if (c[i] == 1) coin[1]++; n   } n   sort(c, c + n); n   int start_pos = 0; n   for (int i = 0; i < n; i++) n     if (c[i] != 1) { n       start_pos = i; n       break; n     } n   rec(1, c[start_pos], start_pos); n   if (can) { n     cout <<  YES ; n   } else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[1002], l, n; n   scanf( %d %d , &n, &l); n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   sort(a, a + n); n   double d = max(a[0] - 0, l - a[n - 1]); n   for (int i = n - 1; i > 0; i--) d = max(d, double(a[i] - a[i - 1]) / 2); n   printf( %0.10lf , d); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double m, n; n   double i; n   cin >> m >> n; n   double ans = 0; n   for (i = 1; i <= m; i++) { n     ans = ans + i * (pow((i / m), n) - pow(((i - 1) / m), n)); n   } n   printf( %.9lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int pre[5005][5005]; n int main() { n   int n, k; n   bool flag = true; n   scanf( %d%d , &n, &k); n   map<int, int> m; n   for (int i = 1; i <= n; i++) { n     int yy; n     scanf( %d , &yy); n     m[yy]++; n     pre[yy][m[yy]] = i; n     if (m[yy] > k) flag = false; n   } n   int xx = 0; n   bool flag1 = false; n   int p[5005]; n   for (int i = 1; i <= 5000; i++) { n     for (int j = 1; j <= m[i]; j++) { n       p[pre[i][j]] = ++xx; n       if (xx == k) { n         flag1 = true; n         xx = 0; n       } n     } n   } n   if (flag == false || flag1 == false) n     puts( NO ); n   else { n     puts( YES ); n     for (int i = 1; i <= n; i++) { n       cout << p[i] <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const long double PI = 4 * atan((long double)1); n const long long int INF = 1e18; n const long long int NINF = -1e18; n long long int get_hash(string s) { n   long long int N = 1000001; n   long long int base[N], A = 11, MD = 1110111110111; n   base[0] = 1; n   for (long long int i = (1); i < (N); ++i) base[i] = (base[i - 1] * A) % MD; n   long long int hs = 0; n   for (long long int i = (0); i < (s.size()); ++i) { n     hs += (s[i] * base[i]); n     hs %= MD; n   } n   return hs; n } n long long power(long long a, long long n) { n   long long res = 1; n   while (n) { n     if (n % 2) res *= a; n     a *= a; n     n /= 2; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n, m; n   cin >> n >> m; n   map<int, int> mpc; n   long long int a[n], b[m]; n   for (long long int i = (0); i < (n); ++i) cin >> a[i]; n   int arr[n]; n   map<int, int> mpa, mpb; n   int k = 0; n   for (long long int i = (0); i < (m); ++i) { n     cin >> b[i]; n     if (mpa[b[i]] != 1) { n       arr[k] = b[i]; n       k++; n       mpa[b[i]] = 1; n     } n   } n   if (k != n) { n     for (long long int i = (1); i < (n + 1); ++i) { n       if (mpa[i] != 1) { n         mpa[i] = 1; n         arr[k] = i; n         k++; n       } n     } n   } n   stack<int> st, st1; n   for (int i = n - 1; i >= 0; i--) st.push(arr[i]); n   long long int ans = 0; n   for (long long int i = (0); i < (m); ++i) { n     int x = b[i]; n     while (st.top() != x) { n       st1.push(st.top()); n       ans += a[st.top() - 1]; n       st.pop(); n     } n     st.pop(); n     while (!st1.empty()) { n       st.push(st1.top()); n       st1.pop(); n     } n     st.push(x); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n int main() { n   int n; n   cin >> n; n   int arr[n + 1]; n   for (int i = 1; i <= n; i++) cin >> arr[i]; n   int long long ans = 0; n   for (int mx = 0; mx < 31; mx++) { n     int long long cur = 0; n     int long long best = 0; n     for (int i = 1; i < n + 1; i++) { n       int val = (arr[i] > mx ? -INF : arr[i]); n       cur += val; n       best = min(best, cur); n       ans = max(ans, (cur - best) - mx); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int aux; n   string a; n   cin >> a; n   cout << (a.size() * 25) + 26 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, x, y, sum, cnt, ans; n int main() { n   printf( red n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class U> n T &ctmax(T &x, const U &y) { n   return x = max<T>(x, y); n } n template <class T, class U> n T &ctmin(T &x, const U &y) { n   return x = min<T>(x, y); n } n int main() { n   cin.tie(0)->sync_with_stdio(0); n   cin.exceptions(ios::badbit | ios::failbit); n   int n, c1, c2; n   cin >> n >> c1 >> c2; n   vector<int> a(n); n   for (auto i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   long long res = numeric_limits<long long>::max(); n   vector<int> primes; n   auto get_primes = [&](int x) { n     for (auto p = 2; p * p <= x; ++p) { n       if (x % p == 0) { n         primes.push_back(p); n         while (x % p == 0) { n           x /= p; n         } n       } n     } n     if (x >= 2) { n       primes.push_back(x); n     } n   }; n   get_primes(a.front() - 1), get_primes(a.front()), get_primes(a.front() + 1); n   get_primes(a.back() - 1), get_primes(a.back()), get_primes(a.back() + 1); n   sort(primes.begin(), primes.end()); n   primes.erase(unique(primes.begin(), primes.end()), primes.end()); n   for (auto p : primes) { n     array<long long, 3> dp{}; n     for (auto x : a) { n       array<long long, 3> dp_next{}; n       dp_next.fill(numeric_limits<long long>::max() / 2); n       if (x % p == 0) { n         ctmin(dp_next[0], dp[0]); n         ctmin(dp_next[2], min(dp[1], dp[2])); n       } n       ctmin(dp_next[1], min(dp[0], dp[1]) + c1); n       if ((x + 1) % p == 0 || (x - 1) % p == 0) { n         ctmin(dp_next[0], dp[0] + c2); n         ctmin(dp_next[2], min(dp[1], dp[2]) + c2); n       } n       swap(dp, dp_next); n     } n     ctmin(res, *min_element(dp.begin(), dp.end())); n   } n   cout << res <<   n ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const double pi = 3.141592653589793238; n int main() { n   int t, a, b; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n]; n     for (long long int i = 0; i < n; i++) cin >> a[i]; n     int ct = 0, ct1 = 0, ct2 = 0; n     sort(a, a + n); n     for (long long int i = 0; i < n; i++) { n       if (a[i] % 2 == 0) n         ct1 += 1; n       else n         ct2 += 1; n     } n     int i = 0; n     while (i < (n - 1)) { n       if (a[i + 1] - a[i] == 1) { n         ct += 1; n         i += 1; n       } n       i += 1; n     } n     if (ct1 % 2 == 0 && ct2 % 2 == 0) { n       cout <<  YES ; n     } else { n       int e = 0; n       if (ct1 % 2 != 0) { n         if (ct2 > 0 && ct > 0) { n           if ((ct2 - 1) % 2 == 0) { n             e = 1; n             ct2 -= 1; n             ct1 -= 1; n           } n         } n       } else if (ct2 % 2 != 0) { n         if (ct1 > 0 && ct > 0) { n           if ((ct1 - 1) % 2 == 0) e = 1; n         } n       } n       if (e == 1) n         cout <<  YES ; n       else n         cout <<  NO ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> a[1111]; n queue<int> p; n int ji[1111], f = 1, b[1111], c[1111]; n void dfs(int p) { n   ji[p]++; n   for (int i = 0; i < a[p].size(); i++) { n     if (ji[a[p][i]] >= 3) { n       f = 0; n       return; n     } n     dfs(a[p][i]); n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     a[x].push_back(y); n     b[y]++; n     c[x]++; n     c[y]++; n   } n   int h = 0; n   for (int i = 1; i <= n; i++) { n     if (!b[i] && c[i]) { n       p.push(i); n       ji[i]++; n       h++; n     } n   } n   while (!p.empty()) { n     int k = p.front(); n     p.pop(); n     ji[k]++; n     for (int i = 0; i < a[k].size(); i++) { n       int x = a[k][i]; n       b[x]--; n       if (!b[x] && !ji[x]) { n         p.push(x); n         h++; n         ji[x]++; n       } n     } n   } n   if (h == n) n     printf( yes n ); n   else n     printf( no n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > adj; n bool vis[100100]; n int mx = 0; n int far = -1; n void dfs(int cur, int len) { n   if (vis[cur]++) return; n   if (len > mx) { n     mx = len; n     far = cur; n   } n   for (int x : adj[cur]) dfs(x, len + 1); n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   adj = vector<vector<int> >(n); n   for (int i = 0; i < m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     adj[x - 1].push_back(y - 1); n     adj[y - 1].push_back(x - 1); n   } n   bool ok = (m == n - 1); n   dfs(0, 0); n   memset(vis, 0, sizeof(vis)); n   dfs(far, 0); n   printf( %d , mx); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n ll mod = 1e9 + 7; n ll bpow(ll a, ll b, ll m) { n   a %= m; n   ll res = 1; n   while (b > 0) { n     if (b & 1) res = res * a % m; n     a = a * a % m; n     b >>= 1; n   } n   return res; n } n bool cmp(vector<ll> v1, vector<ll> v2) { return v1[1] < v2[1]; } n ll power(ll x, ll y) { n   ll result = 1; n   while (y > 0) { n     if (y % 2 == 0) { n       x = x * x; n       y = y / 2; n     } else { n       result = result * x; n       y = y - 1; n     } n   } n   return result; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     ll n; n     cin >> n; n     ll a[n]; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     sort(a, a + n); n     ll count = 0; n     ll tar = n / 2; n     for (int i = 1; i < n; i++) { n       cout << a[i] <<     << a[0] << endl; n       count++; n       if (count == tar) break; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n; n   vector<string> v; n   cin >> n; n   for (int i = 0; i < int(n); i++) { n     string s; n     cin >> s; n     v.push_back(s); n   } n   for (int i = 0; i < int(10); i++) { n     cout <<  ??  << i <<  >>  << i <<  ??  << endl; n   } n   cout <<  ??>>?  << endl; n   for (int i = 0; i < int(9); i++) { n     cout << i <<  ?  n          <<  <>  << i + 1 << endl; n   } n   cout <<  9?>>?0  << endl; n   cout <<  ?<>1  << endl; n   cout <<  >>??  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> G[500005]; n void solve() { n   int n, m, i, a, b; n   cin >> n >> m; n   vector<pair<int, int>> topics(n + 1); n   vector<int> assigned(n + 1, 1); n   vector<bool> done(n + 1, 0); n   for (i = 0; i < m; ++i) { n     cin >> a >> b; n     G[a].push_back(b); n     G[b].push_back(a); n   } n   for (i = 1; i <= n; ++i) { n     cin >> topics[i].first; n     topics[i].second = i; n   } n   sort(topics.begin() + 1, topics.end()); n   for (i = 1; i <= n; ++i) { n     if (assigned[topics[i].second] != topics[i].first) { n       cout << -1 << endl; n       return; n     } n     done[topics[i].second] = 1; n     for (int child : G[topics[i].second]) { n       if (!done[child] && assigned[child] == assigned[topics[i].second]) n         assigned[child] = assigned[topics[i].second] + 1; n     } n   } n   for (i = 1; i <= n; ++i) cout << topics[i].second <<    ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, d, e, i, j, n; n int main() { n   map<string, long long> m, m1; n   cin >> a >> b; n   string s; n   for (i = 1; i <= a; i++) { n     cin >> s; n     m[s]++; n   } n   d = b; n   for (i = 1; i <= b; i++) { n     cin >> s; n     if (m[s] > 0) { n       n++; n       a--; n       d--; n       m[s]--; n     } n   } n   if (n % 2 == 1) { n     if (d > a) n       cout <<  NO ; n     else n       cout <<  YES ; n   } else { n     if (a > d) n       cout <<  YES ; n     else n       cout <<  NO ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n const double PI = 4 * atan(1); n const int INF = 0x3f3f3f3f; n const ll LINF = 0x3f3f3f3f3f3f3f3f; n const int MOD = 1e9 + 7; n const int MAX_N = 2e6 + 10; n int n; n int a[MAX_N], b[MAX_N], p[MAX_N]; n void solve() { n   for (int i = 1; i <= n; ++i) { n     p[b[i]] = i; n   } n   if (p[1]) { n     int i = 2; n     for (; p[i] == p[1] + i - 1; ++i) n       ; n     if (p[i - 1] == n) { n       int j; n       for (j = i; j <= n && p[j] <= j - i; ++j) n         ; n       if (j > n) { n         cout << n - i + 1 <<   n ; n         return; n       } n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; ++i) ans = max(ans, p[i] - i + 1 + n); n   cout << ans <<   n ; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> a[i]; n   for (int i = 1; i <= n; ++i) cin >> b[i]; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1 << 30; n const long long mod = 95542721; n const double eps = 1e-9; n const double pi = 3.1415926535897932384626; n const int DX[] = {1, 0, -1, 0}, DY[] = {0, 1, 0, -1}; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n long long powmod(long long a, long long b, long long mod) { n   long long res = 1; n   a %= mod; n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n namespace flow { n const int M = 300000, N = 30000; n int y[M], nxt[M], gap[N], fst[N], c[N], pre[N], q[N], dis[N]; n int f[M]; n int S, T, tot, Tn; n void init(int s, int t, int tn) { n   tot = 1; n   memset(fst, 0, sizeof(fst)); n   S = s; n   T = t; n   Tn = tn; n } n void add(int u, int v, int c1, int c2 = 0) { n   tot++; n   y[tot] = v; n   f[tot] = c1; n   nxt[tot] = fst[u]; n   fst[u] = tot; n   tot++; n   y[tot] = u; n   f[tot] = c2; n   nxt[tot] = fst[v]; n   fst[v] = tot; n } n int sap() { n   int u = S, t = 1; n   int flow = 0; n   for (int i = 0; i < (int)Tn; i++) c[i] = fst[i], dis[i] = Tn, gap[i] = 0; n   q[0] = T; n   dis[T] = 0; n   pre[S] = 0; n   for (int i = 0; i < (int)t; i++) { n     int u = q[i]; n     for (int j = fst[u]; j; j = nxt[j]) n       if (dis[y[j]] > dis[u] + 1 && f[j ^ 1]) n         q[t++] = y[j], dis[y[j]] = dis[u] + 1; n   } n   for (int i = 0; i < (int)Tn; i++) gap[dis[i]]++; n   while (dis[S] <= Tn) { n     while (c[u] && (!f[c[u]] || dis[y[c[u]]] + 1 != dis[u])) c[u] = nxt[c[u]]; n     if (c[u]) { n       pre[y[c[u]]] = c[u] ^ 1; n       u = y[c[u]]; n       if (u == T) { n         int minf = inf; n         for (int p = pre[T]; p; p = pre[y[p]]) minf = min(minf, f[p ^ 1]); n         for (int p = pre[T]; p; p = pre[y[p]]) f[p ^ 1] -= minf, f[p] += minf; n         flow += minf; n         u = S; n       } n     } else { n       if (!(--gap[dis[u]])) break; n       int mind = Tn; n       c[u] = fst[u]; n       for (int j = fst[u]; j; j = nxt[j]) n         if (f[j] && dis[y[j]] < mind) mind = dis[y[j]], c[u] = j; n       dis[u] = mind + 1; n       gap[dis[u]]++; n       if (u != S) u = y[pre[u]]; n     } n   } n   return flow; n } n };  // namespace flow n int n, m, g, w, k, Sx, fg, ans; n int sx[10100], c[20]; n int main() { n   scanf( %d%d%d , &n, &m, &g); n   flow::init(0, n + m + 1, n + m + 2); n   for (int i = 0; i < (int)n; i++) scanf( %d , sx + i); n   for (int i = 0; i < (int)n; i++) { n     scanf( %d , &w); n     if (sx[i] == 0) n       flow::add(0, i + 1, w); n     else n       flow::add(i + 1, n + m + 1, w); n   } n   for (int i = 0; i < (int)m; i++) { n     scanf( %d%d%d , &Sx, &w, &k); n     for (int j = 0; j < (int)k; j++) scanf( %d , c + j); n     scanf( %d , &fg); n     ans += w; n     if (fg) w += g; n     if (Sx == 0) { n       flow::add(0, i + n + 1, w); n       for (int j = 0; j < (int)k; j++) flow::add(i + n + 1, c[j], inf); n     } else { n       flow::add(i + n + 1, n + m + 1, w); n       for (int j = 0; j < (int)k; j++) flow::add(c[j], i + n + 1, inf); n     } n   } n   printf( %d n , ans - flow::sap()); n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 100 * 1000 * 1000; n int n1, n2, k1, k2; n long long dp[2][101][101]; n int solve(int a, int b, int c) { n   if (b == 0 || c == 0) { n     if (a == 0) { n       dp[a][b][c] = (b <= k1); n       return dp[a][b][c]; n     } n     dp[a][b][c] = (c <= k2); n     return dp[a][b][c]; n   } n   if (dp[a][b][c] != -1) return dp[a][b][c]; n   long long ret = 0LL; n   if (a == 0) { n     for (int i = 1; (i <= k1 && i <= b); ++i) n       ret = (ret + solve(1, b - i, c)) % MOD; n   } n   if (a == 1) { n     for (int i = 1; (i <= k2 && i <= c); ++i) n       ret = (ret + solve(0, b, c - i)) % MOD; n   } n   dp[a][b][c] = ret; n   return dp[a][b][c]; n } n int main() { n   memset(dp, -1, sizeof(dp)); n   cin >> n1 >> n2 >> k1 >> k2; n   int ans = (solve(0, n1, n2) + solve(1, n1, n2)) % MOD; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef int (*uniopi)(int); n typedef long long (*uniopl)(long long); n typedef int (*binopi)(int, int); n typedef long long (*binopl)(long long, long long); n typedef int (*tupopi)(int, int, int); n typedef long long (*tupopl)(long long, long long, long long); n const long double PI = acos(0) * 2; n const int INF = 0x3f2f1f0f; n const long long LINF = 1ll * INF * INF; n int X4[] = {-1, 0, 1, 0}, Y4[] = {0, -1, 0, 1}; n int X8[] = {-1, -1, -1, 0, 1, 1, 1, 0}, Y8[] = {-1, 0, 1, 1, 1, 0, -1, -1}; n template <typename T> n inline bool uax(T& a, T b) { n   return a < b ? (a = b, 1) : 0; n } n template <typename T> n inline bool uin(T& a, T b) { n   return a > b ? (a = b, 1) : 0; n } n template <typename T1, typename T2> n istream& operator>>(istream& is, pair<T1, T2>& p) { n   return is >> p.first >> p.second; n } n template <typename T1, typename T2> n ostream& operator<<(ostream& os, const pair<T1, T2>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T> n ostream& operator<<(ostream& os, const vector<T>& v) { n   for (int i = 0; i < (int)v.size(); i++) { n     os << v[i]; n     if (i + 1 < (int)v.size()) os <<    ; n   } n   return os; n } n const int MAX_N = 3e5 + 1; n int N, A[MAX_N], B[MAX_N]; n long long D[MAX_N][3]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(nullptr); n   cout << std::setprecision(10); n   cout << fixed; n   int q; n   cin >> q; n   while (q--) { n     cin >> N; n     for (int i = 1; i <= N; i++) cin >> A[i] >> B[i]; n     for (int i = N; i >= 0; i--) { n       for (int j = 0; j < 3; j++) { n         long long& d = D[i][j] = LINF; n         int a = A[i] + j, b = B[i], c = j * b, na = A[i + 1]; n         if (a != na) uin(d, D[i + 1][0] + c); n         if (a != na + 1) uin(d, D[i + 1][1] + c); n         if (a != na + 2) uin(d, D[i + 1][2] + c); n       } n     } n     cout << D[0][0] << endl; n     for (int i = 1; i <= (N); i++) n       for (int j = 0; j < (3); j++) D[i][j] = 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct BIT { n   vector<pair<int, int> > f; n   void init() { n     f.push_back(make_pair(-1e9, 0)); n     f.push_back(make_pair(0, 0)); n     f.push_back(make_pair(1e9, 0)); n     sort(f.begin(), f.end()); n   } n   void update(int i, int val, bool fake = 0) { n     if (fake) { n       f.push_back(make_pair(i, 0)); n       return; n     } n     i = lower_bound(f.begin(), f.end(), make_pair(i, 0)) - f.begin(); n     for (; i < f.size(); i += i & -i) f[i].second = max(f[i].second, val); n   } n   int get(int i, bool fake = 0) { n     if (fake) { n       f.push_back(make_pair(i, 0)); n       return 0; n     } n     i = lower_bound(f.begin(), f.end(), make_pair(i, 0)) - f.begin(); n     int ret = 0; n     for (; i; i -= i & -i) ret = max(ret, f[i].second); n     return ret; n   } n } f[100005]; n struct data { n   int u, v, c; n   data(int _u, int _v, int _c) : u(_u), v(_v), c(_c){}; n }; n int n, m; n vector<data> e; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 1; i <= m; i++) { n     int u, v, c; n     cin >> u >> v >> c; n     e.push_back(data(u, v, c)); n   } n   for (auto &it : e) { n     f[it.u].get(it.c - 1, 1); n     f[it.v].update(it.c, 0, 1); n   } n   for (int i = 1; i <= n; i++) f[i].init(); n   int ans = 0; n   for (auto &it : e) { n     int rec = f[it.u].get(it.c - 1); n     ans = max(ans, rec + 1); n     f[it.v].update(it.c, rec + 1); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int t, n, k, d, a[200010]; n int num[1000010]; n bool check(int mid) { n   int l = 1, r = 1, nl = 1, nr = 1, cnt = 1; n   num[a[1]]++; n   while (nr < n) { n     nr++; n     num[a[nr]]++; n     if (num[a[nr]] == 1) { n       cnt++; n     } n     while (cnt > mid) { n       num[a[nl]]--; n       if (num[a[nl]] == 0) { n         cnt--; n       } n       nl++; n     } n     if (r - l + 1 < nr - nl + 1) { n       l = nl, r = nr; n     } n   } n   for (register int i = nl; i <= nr; i++) { n     num[a[i]]--; n   } n   return r - l + 1 >= d; n } n int main() { n   scanf( %d , &t); n   while (t--) { n     scanf( %d%d%d , &n, &k, &d); n     for (register int i = 1; i <= n; i++) { n       scanf( %d , &a[i]); n     } n     int l = 1, r = n, mid, res; n     while (l <= r) { n       mid = (l + r) >> 1; n       if (check(mid)) { n         res = mid; n         r = mid - 1; n       } else { n         l = mid + 1; n       } n     } n     printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c, d, e, z[509], x[509], pi, g, s; n vector<int> v[509]; n bool m, rr; n pair<int, int> p[509]; n void dfs(int w) { n   if (m == 1) return; n   z[w] = 1; n   for (vector<int>::iterator it = v[w].begin(); it != v[w].end(); it++) { n     if (x[(*it)] == c) { n       p[pi].first = w; n       p[pi].second = (*it); n       pi++; n       m = 1; n       g = (*it); n       return; n     } n     if (z[(*it)] == 0) { n       x[(*it)] = c; n       p[pi].first = w; n       p[pi].second = (*it); n       pi++; n       dfs((*it)); n       if (m == 1) return; n     } n   } n   x[w] = 0; n } n void dfss(int w) { n   if (rr == 1) { n     x[w] = 0; n     return; n   } n   z[w] = d + 1; n   for (vector<int>::iterator it = v[w].begin(); it != v[w].end(); it++) { n     if ((((*it) == p[d].second && w == p[d].first))) continue; n     if (x[(*it)] == d + 1) { n       s = 1; n       rr = 1; n       x[w] = 0; n       return; n     } n     if (z[(*it)] != d + 1) { n       x[(*it)] = d + 1; n       dfss((*it)); n       if (rr == 1) { n         x[w] = 0; n         return; n       } n     } n   } n   x[w] = 0; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> a >> b; n   for (c = 1; c <= b; c++) { n     cin >> d >> e; n     v[d].push_back(e); n   } n   for (c = 1; c <= a; c++) { n     if (z[c] == 0) { n       x[c] = c; n       dfs(c); n       if (m == 1) break; n     } n   } n   if (g == 0) { n     cout <<  YES ; n     return 0; n   } n   for (d = 0; d < pi; d++) { n     if (p[d].first == g) break; n   } n   for (c = 0; c <= 502; c++) z[c] = 0, x[c] = 0; n   for (d = d; d < pi; d++) { n     for (c = 1; c <= a; c++) { n       if (z[c] != d + 1) { n         x[c] = d + 1; n         rr = 0; n         dfss(c); n         if (rr == 1) break; n       } n     } n     if (rr == 0) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[101]; n vector<int> vec; n int main(void) { n   int n; n   long long k; n   long long sum; n   scanf( %d %lld , &n, &k); n   sum = k; n   vec.push_back(1); n   for (int i = 0; i < n; i++) { n     scanf( %d , &arr[i]); n     sum += arr[i]; n     vec.push_back(arr[i]); n     for (int j = 2; j * j <= arr[i]; j++) { n       if (arr[i] % j == 0) { n         vec.push_back(j); n         vec.push_back(arr[i] / j); n       } else { n         vec.push_back(j); n         vec.push_back(arr[i] / j + 1); n       } n     } n   } n   sort(vec.begin(), vec.end()); n   vec.resize(unique(vec.begin(), vec.end()) - vec.begin()); n   int len = vec.size(); n   long long ans = 0; n   for (int i = len - 1; i >= 0; i--) { n     long long llsum = 0; n     for (int j = 0; j < n; j++) { n       llsum += (arr[j] / vec[i]); n       if (arr[j] % vec[i] != 0) llsum++; n     } n     long long d = sum / llsum; n     if (d >= vec[i]) { n       ans = max(ans, d); n     } n   } n   printf( %lld , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m[200]; n int n; n int S, E; n bool is_ok(int lim) { n   int s = 0, e = 0; n   for (int i = 0; i < lim; ++i) s += m[i]; n   for (int j = lim; j <= n; ++j) e += m[j]; n   return s <= E && e <= E && s >= S && e >= S; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> m[i]; n   cin >> S >> E; n   for (int i = 1; i <= n; ++i) n     if (is_ok(i)) { n       cout << i << endl; n       break; n     } else if (i == n) n       cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 105; n map<string, int> H1[maxn], H2[maxn]; n vector<char> ans; n char getA(string a, string b) { n   int c[30]; n   for (int i = 0; i < 26; i++) c[i] = 0; n   for (int i = 0; i < a.size(); i++) c[a[i] -  a ]++; n   for (int i = 0; i < b.size(); i++) c[b[i] -  a ]--; n   for (int i = 0; i < 26; i++) { n     if (c[i] > 0) return i +  a ; n   } n } n int main() { n   int n; n   cin >> n; n   if (n == 1) { n     cout <<  ? 1 1  << endl; n     string sss; n     cin >> sss; n     cout <<  !   << sss << endl; n   } else { n     cout <<  ? 1   << n << endl; n     for (int i = 0; i < n * (n + 1) / 2; i++) { n       string s; n       cin >> s; n       sort(s.begin(), s.end()); n       H1[s.size()][s]++; n     } n     cout <<  ? 2   << n << endl; n     for (int i = 0; i < n * (n - 1) / 2; i++) { n       string s; n       cin >> s; n       sort(s.begin(), s.end()); n       H2[s.size()][s]++; n     } n     for (int i = 1; i <= n; i++) { n       for (auto p : H2[i]) { n         H1[i][p.first] -= H2[i][p.first]; n       } n       for (auto p : H1[i]) { n         if (p.second > 0) { n           string new1 =   ; n           for (int j = 0; j < ans.size(); j++) new1 += ans[j]; n           ans.push_back(getA(p.first, new1)); n         } n       } n     } n     string new1 =   ; n     for (int j = 0; j < ans.size(); j++) new1 += ans[j]; n     cout <<  !   << new1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int C[50][50]; n int main() { n   C[0][0] = 1; n   for (int i = 1; i <= 30; i++) { n     C[i][0] = C[i][i] = 1; n     for (int j = 1; j <= 30; j++) { n       C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; n     } n   } n   string p =  aaaaaaaaab ; n   int pSize = (int)p.size() - 1; n   string result =   ; n   int n; n   cin >> n; n   for (int i = 30; i >= 9; i--) { n     while (n >= C[i][pSize]) { n       n -= C[i][pSize]; n       result +=  b ; n     } n     result +=  a ; n   } n   reverse(result.begin(), result.end()); n   result = p.substr(0, pSize - 1) + result; n   long long cnt = 0; n   int aCnt = 0; n   for (int i = 0; i < result.size(); i++) { n     aCnt += result[i] ==  a ; n     if (result[i] ==  b ) cnt += C[aCnt][pSize]; n   } n   cout << result <<     << p <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long b[1510100], n, x; n long long Mod, C; n long long power(long long a, long long n, long long mod) { n   if (n == 0) return 1; n   long long y; n   y = power(a, n / 2, mod); n   if (n & 1) { n     return (((y * y) % mod) * a) % mod; n   } else { n     return (y * y) % mod; n   } n } n bool func(int mid) { n   int i, j, k; n   long long EX, sum; n   EX = power(x, mid, Mod); n   sum = 0; n   for (i = 0; i < n; i++) { n     if (b[i] >= mid) n       break; n     else { n       sum += power(x, b[i], EX); n       sum %= EX; n     } n   } n   if (sum == 0) n     return true; n   else n     return false; n } n int main() { n   int i, j, k; n   scanf( %d , &n); n   scanf( %d , &x); n   long long mx, quan; n   mx = 0; n   quan = 0; n   int a[n]; n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     mx = max(mx, (long long)a[i]); n     quan += a[i]; n   } n   quan = quan - mx; n   for (i = 0; i < n; i++) { n     b[i] = mx - a[i]; n   } n   sort(b, b + n); n   int hi, lo, mid; n   hi = mx; n   lo = 0; n   Mod = 1000000007; n   while (hi != lo) { n     mid = (hi + lo + 1) / 2; n     if (func(mid)) { n       lo = mid; n     } else { n       hi = mid - 1; n     } n   } n   quan += hi; n   cout << power(x, quan, Mod); n } n


#include <bits/stdc++.h> n using namespace std; n const int Max = 3e5 + 10; n const int E = Max * 2; n int son[Max], maxson[Max], fa[Max], zx[Max]; n int head[Max], nex[E], pnt[E], edge; n int vis[Max]; n void Init() { n   edge = 0; n   memset(son, 0, sizeof(son)); n   memset(maxson, 0, sizeof(maxson)); n   memset(fa, -1, sizeof(fa)); n   memset(head, -1, sizeof(head)); n   memset(vis, 0, sizeof(vis)); n } n void Addedge(int u, int v) { n   pnt[edge] = v; n   nex[edge] = head[u]; n   head[u] = edge++; n } n void dfs1(int u) { n   son[u] = 1; n   vis[u] = 1; n   maxson[u] = 0; n   int v; n   for (int e = head[u]; e != -1; e = nex[e]) { n     v = pnt[e]; n     if (!vis[v]) { n       dfs1(v); n       son[u] += son[v]; n       maxson[u] = max(maxson[u], son[v]); n     } n   } n } n bool is_ok(int u, int v) { n   if (son[u] >= maxson[v] * 2 && son[u] >= (son[u] - son[v]) * 2) return 1; n   return 0; n } n void dfs2(int u) { n   vis[u] = 1; n   if (son[u] == 1) { n     zx[u] = u; n     return; n   } n   int v, z = u; n   for (int e = head[u]; e != -1; e = nex[e]) { n     v = pnt[e]; n     if (!vis[v]) { n       dfs2(v); n       if (maxson[u] == son[v]) { n         z = zx[v]; n       } n     } n   } n   while (!is_ok(u, z) && fa[z] != -1) { n     z = fa[z]; n   } n   zx[u] = z; n } n int main() { n   int n, m; n   while (~scanf( %d%d , &n, &m)) { n     Init(); n     int u, v; n     for (int i = 2; i <= n; i++) { n       scanf( %d , &u); n       fa[i] = u; n       Addedge(u, i); n       Addedge(i, u); n     } n     dfs1(1); n     memset(vis, 0, sizeof(vis)); n     dfs2(1); n     while (m--) { n       scanf( %d , &u); n       printf( %d n , zx[u]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m; n int main() { n   cin >> n >> m; n   if (n > m) swap(n, m); n   if (n == 1) { n     cout << 6 * (m / 6) + 2 * max(m % 6 - 3, (long long)0); n     return 0; n   } n   if (n == 2) { n     if (m == 2) { n       cout << 0; n       return 0; n     } n     if (m == 3) { n       cout << 4; n       return 0; n     } n     if (m == 7) { n       cout << 12; n       return 0; n     } n   } n   if (n % 2 == 0 || m % 2 == 0) { n     cout << n * m; n     return 0; n   } n   cout << n * m - 1; n   return 0; n } n


#include <bits/stdc++.h> n clock_t t = clock(); n namespace my_std { n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n template <typename T> n inline T rnd(T l, T r) { n   return uniform_int_distribution<T>(l, r)(rng); n } n template <typename T> n inline bool chkmax(T& x, T y) { n   return x < y ? x = y, 1 : 0; n } n template <typename T> n inline bool chkmin(T& x, T y) { n   return x > y ? x = y, 1 : 0; n } n template <typename T> n inline void read(T& t) { n   t = 0; n   char f = 0, ch = getchar(); n   double d = 0.1; n   while (ch >  9  || ch <  0 ) f |= (ch ==  - ), ch = getchar(); n   while (ch <=  9  && ch >=  0 ) t = t * 10 + ch - 48, ch = getchar(); n   if (ch ==  . ) { n     ch = getchar(); n     while (ch <=  9  && ch >=  0 ) t += d * (ch ^ 48), d *= 0.1, ch = getchar(); n   } n   t = (f ? -t : t); n } n template <typename T, typename... Args> n inline void read(T& t, Args&... args) { n   read(t); n   read(args...); n } n char __sr[1 << 21], __z[20]; n int __C = -1, __zz = 0; n inline void Ot() { fwrite(__sr, 1, __C + 1, stdout), __C = -1; } n inline void print(register int x) { n   if (__C > 1 << 20) Ot(); n   if (x < 0) __sr[++__C] =  - , x = -x; n   while (__z[++__zz] = x % 10 + 48, x /= 10) n     ; n   while (__sr[++__C] = __z[__zz], --__zz) n     ; n   __sr[++__C] =   n ; n } n void file() {} n inline void chktime() {} n long long ksm(long long x, int y) { n   long long ret = 1; n   for (; y; y >>= 1, x = x * x) n     if (y & 1) ret = ret * x; n   return ret; n } n }  // namespace my_std n using namespace my_std; n int n; n int ans[10][333][10] = { n     {{2}, {3, 1, 2, 3}, {3, 1, 2, 3}}, n     {{4}, {3, 1, 2, 3}, {3, 1, 2, 4}, {3, 1, 3, 4}, {3, 2, 3, 4}}, n     {{6}, n      {3, 5, 4, 2}, n      {3, 3, 1, 5}, n      {4, 4, 5, 2, 3}, n      {4, 4, 3, 2, 1}, n      {3, 4, 2, 1}, n      {3, 3, 1, 5}}, n     {{9}, n      {4, 1, 5, 2, 4}, n      {4, 1, 6, 3, 4}, n      {4, 2, 6, 3, 5}, n      {3, 1, 2, 6}, n      {3, 2, 3, 4}, n      {3, 1, 5, 3}, n      {3, 1, 2, 3}, n      {3, 4, 5, 6}, n      {3, 4, 5, 6}}}; n int L[333][2], R[333][2]; n int Ans[333 * 333][10], cnt; n void addcir(int s, int l) { n   for (int i = (1); i <= (ans[s][0][0]); i++) { n     assert(ans[s][i][0]); n     ++cnt; n     for (int j = (0); j <= (ans[s][i][0]); j++) n       Ans[cnt][j] = ans[s][i][j] + (!!j) * l; n   } n } n void solve(int l, int r) { n   int s = r - l + 1; n   if (s <= 6) return addcir(s - 3, l); n   int x, y, c; n   if (s & 1) { n     x = 3, y = (s - 3) >> 1, c = 1; n     ++l; n     L[1][0] = l, L[1][1] = l + 1; n     L[2][0] = l, L[2][1] = l + 2; n     L[3][0] = l + 1, L[3][1] = l + 2; n     for (int i = (1); i <= (y); i++) n       for (int k = (0); k <= (1); k++) R[i][k] = l + 3 + 2 * (i - 1) + k; n     --l; n     addcir(0, l); n   } else { n     x = 2, y = (s - 4) >> 1, c = 2; n     ++l; n     for (int i = (1); i <= (x); i++) n       for (int k = (0); k <= (1); k++) L[i][k] = l + 2 * (i - 1) + k; n     for (int i = (1); i <= (y); i++) n       for (int k = (0); k <= (1); k++) R[i][k] = l + 4 + 2 * (i - 1) + k; n     --l; n     addcir(1, l); n   } n   for (int i = (1); i <= (x); i++) n     for (int j = (1); j <= (y); j++) n       for (int k = (1); k <= (c); k++) n         ++cnt, Ans[cnt][0] = 4, Ans[cnt][1] = L[i][0], Ans[cnt][2] = R[j][0], n                Ans[cnt][3] = L[i][1], Ans[cnt][4] = R[j][1]; n   solve(l + ((s & 1) ? 3 : 4), r); n } n int main() { n   file(); n   read(n); n   solve(0, n - 1); n   printf( %d n , cnt); n   for (int i = (1); i <= (cnt); i++) { n     for (int j = (0); j <= (Ans[i][0]); j++) printf( %d  , Ans[i][j]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T1, class T2> n bool chmin(T1 &a, T2 b) { n   return b < a && (a = b, true); n } n template <class T1, class T2> n bool chmax(T1 &a, T2 b) { n   return a < b && (a = b, true); n } n int main() { n   int n; n   cin >> n; n   static long long dp[100][100][100][4]; n   dp[1][1][1][0] = 1; n   dp[1][1][1][1] = 1; n   dp[1][1][1][2] = 1; n   dp[1][1][1][3] = 1; n   for (int i = (1); i < (2 * n - 2); i++) n     for (int j = 0; j < (n + 1); j++) n       for (int k = 0; k < (100); k++) n         for (int l = 0; l < (4); l++) { n           for (int x = 0; x < (4); x++) { n             if (l == x) { n               dp[i + 1][j + 1][max(k, j + 1)][x] += dp[i][j][k][l]; n             } else { n               dp[i + 1][1][max(k, 1)][x] += dp[i][j][k][l]; n             } n           } n         } n   long long ans = 0; n   for (int j = 0; j < (n + 1); j++) n     for (int i = 0; i < (4); i++) ans += dp[2 * n - 2][j][n][i]; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   cout.tie(0); n   iostream::sync_with_stdio(false); n   long long n, m; n   cin >> n >> m; n   set<int> taken_lines, taken_cols; n   long long base = n * n; n   for (int i = 0; i < m; ++i) { n     int x, y; n     cin >> x >> y; n     bool lined = false, coled = false; n     if (taken_lines.count(x) == 0) lined = true; n     if (taken_cols.count(y) == 0) coled = true; n     if (lined) { n       base -= n - taken_cols.size(); n     } n     if (coled) { n       base -= n - taken_lines.size(); n     } n     if (lined && coled) { n       base++; n     } n     taken_lines.insert(x); n     taken_cols.insert(y); n     cout << base <<    ; n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int l = 0; n   int r = n - 1; n   while (l < n - 1 && a[l] < a[l + 1]) l++; n   while (r > 0 && a[r] < a[r - 1]) r--; n   for (int i = l + 1; i <= r; i++) { n     if (a[l] != a[i]) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 10; n int a[maxn], b[maxn]; n int dp[2][maxn][55][55]; n inline int ip(int k) { return k > 0 ? k : 0; } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, p, k; n   cin >> n >> p >> k; n   int r, s; n   cin >> r; n   for (int i = 0; i < r; i++) { n     int t; n     cin >> t; n     a[t] = 1; n   } n   cin >> s; n   for (int i = 0; i < s; i++) { n     int t; n     cin >> t; n     b[t] = 1; n   } n   p = min(p, (2 * n) / k + ((2 * n % k) > 0 ? 1 : 0)); n   int f = 0; n   memset(dp, -0x3f, sizeof(dp)); n   dp[0][0][0][0] = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j <= p; j++) { n       for (int x = 0; x < k; x++) { n         for (int y = 0; y < k; y++) { n           dp[!f][j][ip(x - 1)][ip(y - 1)] = n               max(dp[!f][j][ip(x - 1)][ip(y - 1)], n                   dp[f][j][x][y] + ((x > 0 && a[i]) || (y > 0 && b[i]))); n           dp[!f][j + 1][k - 1][ip(y - 1)] = n               max(dp[!f][j + 1][k - 1][ip(y - 1)], n                   dp[f][j][x][y] + (a[i] || (y > 0 && b[i]))); n           dp[!f][j + 1][ip(x - 1)][k - 1] = n               max(dp[!f][j + 1][ip(x - 1)][k - 1], n                   dp[f][j][x][y] + (b[i] || (x > 0 && a[i]))); n         } n       } n     } n     memset(dp[f], -0x3f, sizeof(dp[f])); n     f = !f; n   } n   int ans = 0; n   for (int l = 0; l <= p; l++) n     for (int i = 0; i < k; i++) n       for (int j = 0; j < k; j++) ans = max(ans, dp[f][l][i][j]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int Left[1000010]; n int mini[1000010]; n int main(void) { n   cin.tie(0); n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   string S; n   cin >> S; n   for (int i = 0; i < S.length(); i++) { n     if (S[i] ==  ( ) n       Left[i + 1] = Left[i] + 1; n     else n       Left[i + 1] = Left[i] - 1; n   } n   for (int i = 0; i < 1000010; i++) mini[i] = 2000000000; n   int ans = 0; n   if (Left[n] == 2) { n     for (int i = n; i >= 0; i--) mini[i] = min(mini[i + 1], Left[i]); n     for (int i = 0; i < n; i++) { n       if (i != 0 && Left[i - 1] < 0) break; n       if (S[i] ==  (  && mini[i + 1] >= 2 && Left[n] == 2) ans++; n     } n   } else if (Left[n] == -2) { n     for (int i = n; i >= 0; i--) mini[i] = min(mini[i + 1], Left[i]); n     for (int i = 0; i < n - 1; i++) { n       if (i != 0 && Left[i - 1] < 0) break; n       if (S[i] ==  )  && mini[i] >= -2) ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1111; n int a[maxn], b[maxn]; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   int maxim = 0, num = -1; n   for (int i = 1; i <= n; i++) { n     b[i] = a[i]; n     for (int j = 1; j <= n; j++) b[j] = a[i] + (j - i) * k; n     int kol = 0; n     for (int j = 1; j <= n; j++) n       if (a[j] == b[j]) kol++; n     if (b[1] > 0 && kol > maxim) { n       maxim = kol; n       num = i; n     } n   } n   cout << n - maxim << endl; n   for (int i = 1; i <= n; i++) { n     if (a[num] + (i - num) * k == a[i]) continue; n     if (a[num] + (i - num) * k > a[i]) n       cout <<  +   << i <<     << -(a[i] - a[num] - (i - num) * k) << endl; n     else n       cout <<  -   << i <<     << -(a[num] + (i - num) * k - a[i]) << endl; n   } n   cin >> n; n   return 0; n } n