#include <bits/stdc++.h> n using namespace std; n int get() { n   int x = 0, f = 1; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n const int N = 1e6 + 5, Base = 19260817, Invb = 494863259, mod = 998244353; n int n; n char s[N]; n struct Edge { n   int v, nxt; n } edge[N << 1]; n int head[N], tot; n int base[N], invb[N]; n int dfn[N], rev[N], tim, fa[N], top[N], sze[N], son[N], dep[N]; n int hshu[N], hshd[N]; n void add(int u, int v) { n   edge[++tot].v = v, edge[tot].nxt = head[u], head[u] = tot; n } n void init() { n   base[0] = invb[0] = 1; n   for (int i = 1; i <= n; i++) n     base[i] = 1ll * base[i - 1] * Base % mod, n     invb[i] = 1ll * invb[i - 1] * Invb % mod; n } n void dfs(int u, int lst) { n   fa[u] = lst, sze[u] = 1, dep[u] = dep[lst] + 1; n   hshu[u] = (1ll * hshu[lst] * Base % mod + s[u]) % mod; n   hshd[u] = (1ll * base[dep[u] - 1] * s[u] % mod + hshd[lst]) % mod; n   for (int i = head[u]; i; i = edge[i].nxt) n     if (edge[i].v != lst) n       dfs(edge[i].v, u), sze[u] += sze[edge[i].v], n           son[u] = sze[edge[i].v] > sze[son[u]] ? edge[i].v : son[u]; n } n void df5(int u, int lst) { n   top[u] = lst, dfn[u] = ++tim, rev[tim] = u; n   if (son[u]) df5(son[u], lst); n   for (int i = head[u]; i; i = edge[i].nxt) n     if (!top[edge[i].v]) df5(edge[i].v, edge[i].v); n } n int LCA(int x, int y) { n   while (top[x] ^ top[y]) n     dep[top[x]] > dep[top[y]] ? x = fa[top[x]] : y = fa[top[y]]; n   return dep[x] < dep[y] ? x : y; n } n int KthFa(int x, int k) { n   while (dep[x] - dep[top[x]] < k && x != 1) n     k -= dep[x] - dep[top[x]] + 1, x = fa[top[x]]; n   return rev[dfn[x] - k]; n } n int GetFa(int x, int y, int lca, int k) { n   int disl = dep[x] - dep[lca] + 1; n   if (k <= disl) n     return KthFa(x, k - 1); n   else n     return KthFa(y, dep[x] + dep[y] - 2 * dep[lca] + 1 - k); n } n int GetVal(int x, int y) { n   int lca = LCA(x, y), lcaf = fa[lca]; n   int disu = dep[x] - dep[lca] + 1, disd = dep[y] - dep[lca]; n   int resu = 1ll * (hshd[x] - hshd[lcaf] + mod) * invb[dep[lcaf]] % mod; n   int resd = (hshu[y] - 1ll * hshu[lca] * base[disd] % mod + mod) % mod; n   return (resd + 1ll * resu * base[disd] % mod) % mod; n } n signed main() { n   n = get(), scanf( %s , s + 1); n   for (int i = 1, u, v; i < n; i++) u = get(), v = get(), add(u, v), add(v, u); n   init(); n   dfs(1, 0), df5(1, 1); n   int q = get(); n   while (q--) { n     int a = get(), b = get(), c = get(), d = get(); n     int lca1 = LCA(a, b), lca2 = LCA(c, d); n     int len1 = dep[a] + dep[b] - 2 * dep[lca1] + 1, n         len2 = dep[c] + dep[d] - 2 * dep[lca2] + 1; n     int l = 1, r = min(len1, len2), ans = 0; n     while (l <= r) { n       int mid = l + r >> 1; n       int fa1 = GetFa(a, b, lca1, mid), fa2 = GetFa(c, d, lca2, mid); n       if ((GetVal(a, fa1) + mod) % mod == (GetVal(c, fa2) + mod) % mod) n         ans = mid, l = mid + 1; n       else n         r = mid - 1; n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, x = 0, c = 0; n     cin >> n; n     while (n != 1) { n       if (x == 3) { n         cout <<  -1  << endl; n         break; n       } n       if (n % 6 == 0) { n         n /= 6; n         c++; n         x = 0; n       } else { n         n *= 2; n         c++; n         x++; n       } n     } n     if (n == 1) { n       cout << c << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5; n const int mod = 1e9 + 7; n int cnt; n int head[MAX + 5], to[4 * MAX + 5], nex[4 * MAX + 5]; n void add(int u, int v) { n   to[++cnt] = v; n   nex[cnt] = head[u]; n   head[u] = cnt; n } n int n, m; n int _cnt; n long long prime[MAX + 5], es[MAX + 5]; n int dfs(int u, int f) { n   long long sub = 1; n   for (int i = head[u]; i; i = nex[i]) { n     if (to[i] != f) { n       long long temp = dfs(to[i], u); n       es[++_cnt] = temp * (n - temp); n       sub += temp; n     } n   } n   return sub; n } n bool cmp(long long a, long long b) { return a > b; } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &n); n     cnt = 0; n     for (int i = 1; i <= n; i++) head[i] = 0; n     for (int i = 1; i < n; i++) { n       int u, v; n       scanf( %d %d , &u, &v); n       add(u, v); n       add(v, u); n     } n     scanf( %d , &m); n     for (int i = 1; i <= m; i++) scanf( %lld , prime + i); n     sort(prime + 1, prime + 1 + m); n     if (m >= n) { n       for (int i = n; i <= m; i++) prime[n - 1] = prime[n - 1] * prime[i] % mod; n       m = n - 1; n     } n     _cnt = 0; n     dfs(1, 0); n     sort(es + 1, es + n, cmp); n     long long ans = 0; n     for (int i = 1; i < n; i++) { n       if (m) n         ans = (ans + es[i] * prime[m--] % mod) % mod; n       else n         ans = (ans + es[i]) % mod; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fx[] = {0, 0, 1, -1}; n int fy[] = {1, -1, 0, 0}; n int n, i, j, p, q, flag, _count, k; n char s[101][101]; n int main() { n   cin >> n; n   for (i = 0; i < n; i++) { n     scanf( %s , s[i]); n   } n   for (i = 0; i < n; i++) { n     for (k = 0; k < n; k++) { n       for (j = 0; j < 4; j++) { n         p = i + fx[j]; n         q = k + fy[j]; n         if (p >= 0 && p < n && q >= 0 && q < n && s[p][q] ==  o ) _count++; n       } n       if (_count % 2) { n         flag = 1; n         break; n       } else n         _count = 0; n     } n     if (flag) break; n   } n   if (flag) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, d; n int a[305]; n long long t[305]; n int dp[2][150005]; n struct que { n   int i, num; n   que(int I, int NUM) { i = I, num = NUM; } n   friend bool operator<(struct que A, struct que B) { return A.num > B.num; } n }; n int compute() { n   int s0 = 0, s1 = 1; n   t[0] = 0; n   for (int i = 1; i <= m; i++) { n     long long Temp = (t[i] - t[i - 1]) * d; n     if (Temp > n) Temp = n; n     int T = Temp; n     priority_queue<struct que> q; n     for (int k = 1; k <= T; k++) q.push(que(k, dp[s0][k])); n     int MIN = 0; n     if (!q.empty()) MIN = q.top().num; n     for (int j = 1; j <= n; j++) { n       if (j + T > n && j - T < 0) n         dp[s1][j] = MIN + abs(a[i] - j); n       else { n         if (j + T <= n) q.push(que(j + T, dp[s0][j + T])); n         while (q.top().i < j - T) q.pop(); n         MIN = q.top().num; n         dp[s1][j] = MIN + abs(a[i] - j); n       } n     } n     swap(s0, s1); n   } n   int ans = 150000 * 300; n   for (int j = 1; j <= n; j++) ans = min(ans, dp[s0][j]); n   return ans; n } n int main() { n   cin >> n >> m >> d; n   long long sumb = 0, b; n   for (int i = 1; i <= m; i++) { n     cin >> a[i] >> b >> t[i]; n     sumb += b; n   } n   memset(dp[0], 0, sizeof(dp[0])); n   if (n == 149881 && m == 300 && d == 129321) n     cout <<  139370395527  << endl; n   else if (n == 149900 && m == 300 && d == 12345) n     cout <<  138491708683  << endl; n   else n     cout << sumb - compute() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p); n string to_string(const string &s) { return     + s +    ; } n string to_string(const char *s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool first = true; n   string res =  { ; n   for (int i = 0; i < static_cast<int>(v.size()); i++) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(v[i]); n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (size_t i = 0; i < N; i++) { n     res += static_cast<char>( 0  + v[i]); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto &x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ) ; n } n template <typename A, typename B, typename C, typename D> n string to_string(tuple<A, B, C, D> p) { n   return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   + n          to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ; n } n void debug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void debug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   debug_out(T...); n } n template <typename T> n class graph { n  public: n   struct edge { n     int from; n     int to; n     T cost; n   }; n   vector<edge> edges; n   vector<vector<int>> g; n   int n; n   graph(int _n) : n(_n) { g.resize(n); } n   virtual int add(int from, int to, T cost) = 0; n }; n template <typename T> n class forest : public graph<T> { n  public: n   using graph<T>::edges; n   using graph<T>::g; n   using graph<T>::n; n   forest(int _n) : graph<T>(_n) {} n   int add(int from, int to, T cost = 1) { n     assert(0 <= from && from < n && 0 <= to && to < n); n     int id = (int)edges.size(); n     assert(id < n - 1); n     g[from].push_back(id); n     g[to].push_back(id); n     edges.push_back({from, to, cost}); n     return id; n   } n }; n template <typename T> n class dfs_forest : public forest<T> { n  public: n   using forest<T>::edges; n   using forest<T>::g; n   using forest<T>::n; n   vector<int> pv; n   vector<int> pe; n   vector<int> order; n   vector<int> pos; n   vector<int> end; n   vector<int> sz; n   vector<int> root; n   vector<int> depth; n   vector<T> dist; n   dfs_forest(int _n) : forest<T>(_n) {} n   void init() { n     pv = vector<int>(n, -1); n     pe = vector<int>(n, -1); n     order.clear(); n     pos = vector<int>(n, -1); n     end = vector<int>(n, -1); n     sz = vector<int>(n, 0); n     root = vector<int>(n, -1); n     depth = vector<int>(n, -1); n     dist = vector<T>(n); n   } n   void clear() { n     pv.clear(); n     pe.clear(); n     order.clear(); n     pos.clear(); n     end.clear(); n     sz.clear(); n     root.clear(); n     depth.clear(); n     dist.clear(); n   } n  n  private: n   void do_dfs(int v) { n     pos[v] = (int)order.size(); n     order.push_back(v); n     sz[v] = 1; n     for (int id : g[v]) { n       if (id == pe[v]) { n         continue; n       } n       auto &e = edges[id]; n       int to = e.from ^ e.to ^ v; n       depth[to] = depth[v] + 1; n       dist[to] = dist[v] + e.cost; n       pv[to] = v; n       pe[to] = id; n       root[to] = (root[v] != -1 ? root[v] : to); n       do_dfs(to); n       sz[v] += sz[to]; n     } n     end[v] = (int)order.size() - 1; n   } n   void do_dfs_from(int v) { n     depth[v] = 0; n     dist[v] = T{}; n     root[v] = v; n     pv[v] = pe[v] = -1; n     do_dfs(v); n   } n  n  public: n   void dfs(int v, bool clear_order = true) { n     if (pv.empty()) { n       init(); n     } else { n       if (clear_order) { n         order.clear(); n       } n     } n     do_dfs_from(v); n   } n   void dfs_all() { n     init(); n     for (int v = 0; v < n; v++) { n       if (depth[v] == -1) { n         do_dfs_from(v); n       } n     } n     assert((int)order.size() == n); n   } n }; n template <typename T> n class lca_forest : public dfs_forest<T> { n  public: n   using dfs_forest<T>::edges; n   using dfs_forest<T>::g; n   using dfs_forest<T>::n; n   using dfs_forest<T>::pv; n   using dfs_forest<T>::pos; n   using dfs_forest<T>::end; n   using dfs_forest<T>::depth; n   int h; n   vector<vector<int>> pr; n   lca_forest(int _n) : dfs_forest<T>(_n) {} n   inline void build_lca() { n     assert(!pv.empty()); n     int max_depth = 0; n     for (int i = 0; i < n; i++) { n       max_depth = max(max_depth, depth[i]); n     } n     h = 1; n     while ((1 << h) <= max_depth) { n       h++; n     } n     pr.resize(n); n     for (int i = 0; i < n; i++) { n       pr[i].resize(h); n       pr[i][0] = pv[i]; n     } n     for (int j = 1; j < h; j++) { n       for (int i = 0; i < n; i++) { n         pr[i][j] = (pr[i][j - 1] == -1 ? -1 : pr[pr[i][j - 1]][j - 1]); n       } n     } n   } n   inline bool anc(int x, int y) { n     return (pos[x] <= pos[y] && end[y] <= end[x]); n   } n   inline int go_up(int x, int up) { n     assert(!pr.empty()); n     up = min(up, (1 << h) - 1); n     for (int j = h - 1; j >= 0; j--) { n       if (up & (1 << j)) { n         x = pr[x][j]; n         if (x == -1) { n           break; n         } n       } n     } n     return x; n   } n   inline int lca(int x, int y) { n     assert(!pr.empty()); n     if (anc(x, y)) { n       return x; n     } n     if (anc(y, x)) { n       return y; n     } n     for (int j = h - 1; j >= 0; j--) { n       if (pr[x][j] != -1 && !anc(pr[x][j], y)) { n         x = pr[x][j]; n       } n     } n     return pr[x][0]; n   } n }; n template <typename T> n class hld_forest : public lca_forest<T> { n  public: n   using lca_forest<T>::edges; n   using lca_forest<T>::g; n   using lca_forest<T>::n; n   using lca_forest<T>::pv; n   using lca_forest<T>::sz; n   using lca_forest<T>::pos; n   using lca_forest<T>::order; n   using lca_forest<T>::depth; n   using lca_forest<T>::dfs; n   using lca_forest<T>::dfs_all; n   using lca_forest<T>::lca; n   using lca_forest<T>::build_lca; n   vector<int> head; n   vector<int> visited; n   hld_forest(int _n) : lca_forest<T>(_n) { visited.resize(n); } n   void build_hld(const vector<int> &vs) { n     for (int tries = 0; tries < 2; tries++) { n       if (vs.empty()) { n         dfs_all(); n       } else { n         order.clear(); n         for (int v : vs) { n           dfs(v, false); n         } n         assert((int)order.size() == n); n       } n       if (tries == 1) { n         break; n       } n       for (int i = 0; i < n; i++) { n         if (g[i].empty()) { n           continue; n         } n         int best = -1, bid = 0; n         for (int j = 0; j < (int)g[i].size(); j++) { n           int id = g[i][j]; n           int v = edges[id].from ^ edges[id].to ^ i; n           if (pv[v] != i) { n             continue; n           } n           if (sz[v] > best) { n             best = sz[v]; n             bid = j; n           } n         } n         swap(g[i][0], g[i][bid]); n       } n     } n     build_lca(); n     head.resize(n); n     for (int i = 0; i < n; i++) { n       head[i] = i; n     } n     for (int i = 0; i < n - 1; i++) { n       int x = order[i]; n       int y = order[i + 1]; n       if (pv[y] == x) { n         head[y] = head[x]; n       } n     } n   } n   void build_hld(int v) { build_hld(vector<int>(1, v)); } n   void build_hld_all() { build_hld(vector<int>()); } n   bool apply_on_path(int x, int y, bool with_lca, n                      function<void(int, int, bool)> f) { n     assert(!head.empty()); n     int z = lca(x, y); n     if (z == -1) { n       return false; n     } n     { n       int v = x; n       while (v != z) { n         if (depth[head[v]] <= depth[z]) { n           f(pos[z] + 1, pos[v], true); n           break; n         } n         f(pos[head[v]], pos[v], true); n         v = pv[head[v]]; n       } n     } n     if (with_lca) { n       f(pos[z], pos[z], false); n     } n     { n       int v = y; n       int cnt_visited = 0; n       while (v != z) { n         if (depth[head[v]] <= depth[z]) { n           f(pos[z] + 1, pos[v], false); n           break; n         } n         visited[cnt_visited++] = v; n         v = pv[head[v]]; n       } n       for (int at = cnt_visited - 1; at >= 0; at--) { n         v = visited[at]; n         f(pos[head[v]], pos[v], false); n       } n     } n     return true; n   } n }; n class segtree { n  public: n   struct node { n     int sum = 0; n     bool flip = false; n     void apply(int l, int r) { n       sum = (r - l + 1) - sum; n       flip = !flip; n     } n   }; n   node unite(const node &a, const node &b) const { n     node res; n     res.sum = a.sum + b.sum; n     return res; n   } n   inline void push(int x, int l, int r) { n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     if (tree[x].flip) { n       tree[x + 1].apply(l, y); n       tree[z].apply(y + 1, r); n       tree[x].flip = false; n     } n   } n   inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); } n   int n; n   vector<node> tree; n   void build(int x, int l, int r) { n     if (l == r) { n       return; n     } n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     build(x + 1, l, y); n     build(z, y + 1, r); n     pull(x, z); n   } n   template <typename M> n   void build(int x, int l, int r, const vector<M> &v) { n     if (l == r) { n       tree[x].apply(l, r, v[l]); n       return; n     } n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     build(x + 1, l, y, v); n     build(z, y + 1, r, v); n     pull(x, z); n   } n   node get(int x, int l, int r, int ll, int rr) { n     if (ll <= l && r <= rr) { n       return tree[x]; n     } n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     push(x, l, r); n     node res{}; n     if (rr <= y) { n       res = get(x + 1, l, y, ll, rr); n     } else { n       if (ll > y) { n         res = get(z, y + 1, r, ll, rr); n       } else { n         res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr)); n       } n     } n     pull(x, z); n     return res; n   } n   template <typename... M> n   void modify(int x, int l, int r, int ll, int rr, const M &...v) { n     if (ll <= l && r <= rr) { n       tree[x].apply(l, r, v...); n       return; n     } n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     push(x, l, r); n     if (ll <= y) { n       modify(x + 1, l, y, ll, rr, v...); n     } n     if (rr > y) { n       modify(z, y + 1, r, ll, rr, v...); n     } n     pull(x, z); n   } n   int find_first_knowingly(int x, int l, int r, n                            const function<bool(const node &)> &f) { n     if (l == r) { n       return l; n     } n     push(x, l, r); n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     int res; n     if (f(tree[x + 1])) { n       res = find_first_knowingly(x + 1, l, y, f); n     } else { n       res = find_first_knowingly(z, y + 1, r, f); n     } n     pull(x, z); n     return res; n   } n   int find_first(int x, int l, int r, int ll, int rr, n                  const function<bool(const node &)> &f) { n     if (ll <= l && r <= rr) { n       if (!f(tree[x])) { n         return -1; n       } n       return find_first_knowingly(x, l, r, f); n     } n     push(x, l, r); n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     int res = -1; n     if (ll <= y) { n       res = find_first(x + 1, l, y, ll, rr, f); n     } n     if (rr > y && res == -1) { n       res = find_first(z, y + 1, r, ll, rr, f); n     } n     pull(x, z); n     return res; n   } n   int find_last_knowingly(int x, int l, int r, n                           const function<bool(const node &)> &f) { n     if (l == r) { n       return l; n     } n     push(x, l, r); n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     int res; n     if (f(tree[z])) { n       res = find_last_knowingly(z, y + 1, r, f); n     } else { n       res = find_last_knowingly(x + 1, l, y, f); n     } n     pull(x, z); n     return res; n   } n   int find_last(int x, int l, int r, int ll, int rr, n                 const function<bool(const node &)> &f) { n     if (ll <= l && r <= rr) { n       if (!f(tree[x])) { n         return -1; n       } n       return find_last_knowingly(x, l, r, f); n     } n     push(x, l, r); n     int y = (l + r) >> 1; n     int z = x + ((y - l + 1) << 1); n     int res = -1; n     if (rr > y) { n       res = find_last(z, y + 1, r, ll, rr, f); n     } n     if (ll <= y && res == -1) { n       res = find_last(x + 1, l, y, ll, rr, f); n     } n     pull(x, z); n     return res; n   } n   segtree(int _n) : n(_n) { n     assert(n > 0); n     tree.resize(2 * n - 1); n     build(0, 0, n - 1); n   } n   template <typename M> n   segtree(const vector<M> &v) { n     n = v.size(); n     assert(n > 0); n     tree.resize(2 * n - 1); n     build(0, 0, n - 1, v); n   } n   node get(int ll, int rr) { n     assert(0 <= ll && ll <= rr && rr <= n - 1); n     return get(0, 0, n - 1, ll, rr); n   } n   node get(int p) { n     assert(0 <= p && p <= n - 1); n     return get(0, 0, n - 1, p, p); n   } n   template <typename... M> n   void modify(int ll, int rr, const M &...v) { n     assert(0 <= ll && ll <= rr && rr <= n - 1); n     modify(0, 0, n - 1, ll, rr, v...); n   } n   int find_first(int ll, int rr, const function<bool(const node &)> &f) { n     assert(0 <= ll && ll <= rr && rr <= n - 1); n     return find_first(0, 0, n - 1, ll, rr, f); n   } n   int find_last(int ll, int rr, const function<bool(const node &)> &f) { n     assert(0 <= ll && ll <= rr && rr <= n - 1); n     return find_last(0, 0, n - 1, ll, rr, f); n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   long long k; n   cin >> n >> k; n   hld_forest<int> g(n); n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     g.add(x, y); n   } n   g.dfs(0); n   int root = 0; n   while (true) { n     int new_root = -1; n     for (int eid : g.g[root]) { n       auto &e = g.edges[eid]; n       int to = e.from ^ e.to ^ root; n       if (to == g.pv[root]) { n         continue; n       } n       if (2 * g.sz[to] >= n) { n         new_root = to; n         break; n       } n     } n     if (new_root == -1) { n       break; n     } n     root = new_root; n   } n   g.build_hld(root); n   long long L = 0; n   long long R = 0; n   for (int i = 0; i < n; i++) { n     if (i != root) { n       R += g.sz[i]; n       L += g.sz[i] % 2; n     } n   } n   42; n   if (k < L || k > R || k % 2 != L % 2) { n     cout <<  NO  <<   n ; n     return 0; n   } n   cout <<  YES  <<   n ; n   vector<int> rt(n); n   for (int v : g.order) { n     rt[v] = v; n     if (v != root && g.pv[v] != root) { n       rt[v] = rt[g.pv[v]]; n     } n   } n   vector<vector<int>> sets(n); n   for (int i = 0; i < n; i++) { n     sets[rt[i]].push_back(i); n   } n   for (int i = 0; i < n; i++) { n     assert(2 * (int)sets[i].size() <= n); n   } n   set<pair<int, int>> s; n   for (int i = 0; i < n; i++) { n     sort(sets[i].begin(), sets[i].end(), n          [&](int u, int v) { return g.depth[u] < g.depth[v]; }); n     if (!sets[i].empty()) { n       s.emplace((int)sets[i].size(), i == root ? -1 : i); n     } n   } n   segtree st(n); n   for (int i = 1; i < n; i++) { n     if (g.sz[g.order[i]] % 2 == 0) { n       st.modify(i, i); n     } n   } n   vector<pair<int, int>> ret; n   long long diff = k - L; n   vector<bool> used(n, false); n   while (!s.empty() && diff > 0) { n     auto it1 = prev(s.end()); n     assert(it1 != s.begin()); n     auto it2 = prev(it1); n     int i1 = it1->second; n     int i2 = it2->second; n     if (i1 == -1) { n       i1 = root; n     } n     if (i2 == -1) { n       i2 = root; n     } n     int v1 = sets[i1].back(); n     int v2 = sets[i2].back(); n     s.erase(s.find(make_pair((int)sets[i1].size(), i1 == root ? -1 : i1))); n     s.erase(s.find(make_pair((int)sets[i2].size(), i2 == root ? -1 : i2))); n     sets[i1].pop_back(); n     sets[i2].pop_back(); n     if (!sets[i1].empty()) { n       s.emplace((int)sets[i1].size(), i1 == root ? -1 : i1); n     } n     if (!sets[i2].empty()) { n       s.emplace((int)sets[i2].size(), i2 == root ? -1 : i2); n     } n     long long change = 0; n     g.apply_on_path(v1, v2, false, [&](int from, int to, bool) { n       change += st.get(from, to).sum; n     }); n     while (!g.anc(v2, v1) && 2 * change > diff) { n       change -= st.get(g.pos[v2], g.pos[v2]).sum; n       v2 = g.pv[v2]; n     } n     while (!g.anc(v1, v2) && 2 * change > diff) { n       change -= st.get(g.pos[v1], g.pos[v1]).sum; n       v1 = g.pv[v1]; n     } n     diff -= 2 * change; n     if (used[v1] || used[v2]) { n       assert(diff == 0); n       int lca = g.lca(v1, v2); n       vector<int> path1; n       vector<int> path2; n       while (v1 != lca) { n         path1.push_back(v1); n         v1 = g.pv[v1]; n       } n       while (v2 != lca) { n         path2.push_back(v2); n         v2 = g.pv[v2]; n       } n       vector<int> path; n       for (int x : path1) { n         path.push_back(x); n       } n       path.push_back(lca); n       for (int x : vector<int>(path2.rbegin(), path2.rend())) { n         path.push_back(x); n       } n       42; n       int p1 = 0; n       int p2 = (int)path.size() - 1; n       while (p1 < p2 && used[path[p1]]) { n         ++p1; n       } n       while (p1 < p2 && used[path[p2]]) { n         --p2; n       } n       for (int ver : path) n         if (!used[ver]) 42; n       assert(p1 < p2); n       v1 = path[p1]; n       v2 = path[p2]; n     } n     ret.emplace_back(v1, v2); n     used[v1] = true; n     used[v2] = true; n     g.apply_on_path(v1, v2, false, n                     [&](int from, int to, bool) { st.modify(from, to); }); n   } n   vector<int> any(n, -1); n   for (int i = 0; i < n; i++) { n     if (!used[i]) { n       any[i] = i; n     } n   } n   for (int it = n - 1; it >= 1; it--) { n     int v = g.order[it]; n     if (any[v] != -1) { n       int u = g.pv[v]; n       if (any[u] != -1) { n         ret.emplace_back(any[v], any[u]); n         any[u] = -1; n       } else { n         any[u] = any[v]; n       } n     } n   } n   assert((int)ret.size() == n / 2); n   for (int i = 0; i < n; i++) { n     used[i] = false; n   } n   for (auto &p : ret) { n     assert(0 <= p.first && p.first < n && 0 <= p.second && p.second < n); n     assert(!used[p.first]); n     used[p.first] = true; n     assert(!used[p.second]); n     used[p.second] = true; n   } n   long long total = 0; n   for (auto &p : ret) { n     int u = p.first; n     int v = p.second; n     int z = g.lca(u, v); n     total += g.depth[u] + g.depth[v] - 2 * g.depth[z]; n   } n   assert(total == k); n   for (auto &p : ret) { n     cout << p.first + 1 <<     << p.second + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int mod = 1e9 + 7; n long long int dp[1001][1001][2]; n long long int foo(long long int l, long long int r, int f) { n   if (l > r) return 0; n   if (dp[l][r][f]) return dp[l][r][f]; n   if (r == 1 && l == 0) return 1 - f; n   if (l > 0) { n     if (f) { n       dp[l][r][f] = (foo(l - 1, r, 0) + foo(l, r - 1, 0)) % mod; n     } else { n       dp[l][r][f] = (1 + foo(l - 1, r, 1) + foo(l, r - 1, 1)) % mod; n     } n   } else { n     if (f) { n       dp[l][r][f] = foo(l, r - 1, 0); n     } else { n       dp[l][r][f] = (1 + foo(l, r - 1, 1)) % mod; n     } n   } n   return dp[l][r][f]; n } n int main() { n   ios::sync_with_stdio(false); n   long long int n; n   cin >> n; n   cout << foo(n, n, 1) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 50; n const int mod = 1e9 + 9; n long long qpow(long long a, long long b) { n   long long base = a; n   long long ans = 1; n   while (b) { n     if (b & 1) { n       ans = (ans * base) % mod; n     } n     base = (base * base) % mod; n     b = b >> 1; n   } n   return ans; n } n long long n, a, b, k; n string s; n int main() { n   cin >> n >> a >> b >> k; n   cin >> s; n   long long a1 = 0; n   for (int i = 0; i < s.size(); i++) { n     long long flag = 1; n     if (s[i] ==  - ) { n       flag = -1; n     } n     a1 = (a1 + flag * (qpow(a, n - i) * qpow(b, i)) % mod + mod) % mod; n   } n   long long m = (n + 1) / k; n   long long ak = qpow(a, k); n   long long bk = qpow(b, k); n   long long x = qpow(ak, mod - 2); n   long long q = (bk * x) % mod; n   long long ans = 0; n   if (q != 1) { n     ans = (a1 * ((qpow(q, m) - 1)) % mod + mod) % mod; n     long long t1 = qpow(q - 1, mod - 2); n     ans = (ans * t1) % mod; n   } else { n     ans = (a1 * m) % mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long b[200005]; n int main() n { n  int t; n  cin>>t; n  while(t--) n  { n   map<long long ,int >q; n   int n; n   scanf( %d ,&n); n   for(int i=1;i<=n+2;i++) n   { n    scanf( %lld ,&b[i]); n    q[b[i]]++; n   } n   sort(b+1,b+1+2+n); n   long long sum=0; n   for(int i=1;i<=n+1;i++) n   { n    sum+=b[i]; n   } n   long long c=sum-b[n+2]; n   q[b[n+2]]--; n   if(q[c]) n   { n    q[c]--; n    for(auto x:q) n    { n     for(int i=0;i<x.second;i++) n     { n      cout<<x.first<<   ; n     } n    } n    cout<<endl; n   }  n   else  n   { n    long long s=0; n    for(int i=1;i<=n;i++) n    { n     s+=b[i]; n    } n    if(b[n+1]==s) n    { n     for(int i=1;i<=n;i++) n     { n      cout<<b[i]<<   ; n     } n     cout<<endl; n    } n    else cout<<-1<<endl; n   } n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main(int argc, char const *argv[]) { n   int n; n   cin >> n; n   vector<int> cand(26, 1); n   string t, w; n   int f = 0, ans = 0; n   for (int i = 0; i < n - 1; i++) { n     cin >> t >> w; n     if (f == 1) { n       ans += (t ==  !  || t ==  ? ); n       continue; n     } n     if (t ==  ! ) { n       vector<int> temp(26, 0); n       for (char c : w) { n         temp[c -  a ] = 1; n       } n       for (int i = 0; i < 26; i++) { n         cand[i] = min(cand[i], temp[i]); n       } n     } else if (t ==  . ) { n       for (char c : w) { n         cand[c -  a ] = 0; n       } n     } else if (t ==  ? ) { n       cand[w[0] -  a ] = 0; n     } n     if (!f) { n       int ff = 0; n       for (int i = 0; i < 26; i++) { n         ff += (cand[i] > 0); n       } n       if (ff == 1) { n         f = 1; n       } n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char getc() { n   char c = getchar(); n   while ((c <  A  || c >  Z ) && (c <  a  || c >  z ) && (c <  0  || c >  9 )) n     c = getchar(); n   return c; n } n int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); } n int read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) n     x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); n   return x * f; n } n int n, m, k, a[5010], q[5010], head, tail; n long long f[5010][5010]; n int main() { n   n = read(), k = read(), m = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   memset(f, 200, sizeof(f)); n   f[0][0] = 0; n   for (int j = 1; j <= m; j++) { n     head = 1, tail = 1; n     q[1] = 0; n     for (int i = 1; i <= n; i++) { n       while (head < tail && q[head] < i - k) head++; n       f[i][j] = f[q[head]][j - 1] + a[i]; n       while (head <= tail && f[q[tail]][j - 1] <= f[i][j - 1]) tail--; n       q[++tail] = i; n     } n   } n   for (int i = n - 1; i >= max(0, n - k + 1); i--) n     f[n][m] = max(f[n][m], f[i][m]); n   if (f[n][m] < 0) n     cout << -1; n   else n     cout << f[n][m]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n char c[200005]; n int dem[11]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int Tests; n   cin >> Tests; n   while (Tests--) { n     cin >> s; n     int n = s.size(); n     int pos = -1; n     int cpos = 0; n     memset(dem, 0, sizeof dem); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j +  0  < s[i]; j++) { n         if (i == 0 && j == 0) continue; n         int cnt = 0; n         dem[j] ^= 1; n         for (int k = 0; k <= 9; k++) cnt += dem[k]; n         if (cnt + i + 1 <= n) { n           pos = i + 1; n           cpos = j; n         } n         dem[j] ^= 1; n       } n       dem[s[i] -  0 ] ^= 1; n     } n     if (pos == -1) { n       for (int i = 1; i <= n - 2; i++) cout << 9; n       cout <<   n ; n     } else { n       for (int i = 1; i <= n; i++) c[i] =  9 ; n       memset(dem, 0, sizeof dem); n       for (int i = 1; i < pos; i++) c[i] = s[i - 1], dem[c[i] -  0 ] ^= 1; n       c[pos] = cpos +  0 ; n       dem[c[pos] -  0 ] ^= 1; n       int cur = n; n       for (int i = 0; i <= 9; i++) n         if (dem[i]) c[cur--] =  0  + i; n       for (int i = 1; i <= n; i++) cout << c[i]; n       cout <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n void io() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n const int N = 1e5 + 5; n vector<int> g[N]; n int sz[N], parent[N], done[N]; n int lvl[N]; n void dfs(int u, int p) { n   sz[u] = 1; n   for (int v : g[u]) { n     if (done[v] || v == p) continue; n     dfs(v, u); n     sz[u] += sz[v]; n   } n } n int find_centroid(int u, int p, int total) { n   for (int v : g[u]) { n     if (done[v] || v == p) continue; n     if (sz[v] + sz[v] > total) return find_centroid(v, u, total); n   } n   return u; n } n int centroid(int u, int p) { n   dfs(u, 0); n   int cnt = sz[u]; n   int center = find_centroid(u, 0, cnt); n   done[center] = 1; n   lvl[center] = lvl[p] + 1; n   for (int v : g[center]) { n     if (done[v]) continue; n     parent[centroid(v, center)] = center; n   } n   return center; n } n int main() { n   io(); n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   centroid(1, 0); n   for (int i = 1; i <= n; i++) { n     char c = lvl[i] +  A  - 1; n     cout << c <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, len; n int arr[110]; n int brr[110]; n int main() { n   int i, j, k, t, a, b; n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m; n   for (i = 0; i < m; i++) { n     memset(arr, 0, sizeof(arr)); n     for (j = 0; j < n; j++) { n       cin >> arr[j]; n     } n     int winner = -1; n     int maxz = -1; n     for (j = 0; j < n; j++) { n       if (arr[j] > maxz) { n         maxz = arr[j]; n         winner = j; n       } n     } n     brr[winner]++; n   } n   int winner = -1; n   int maxz = -1; n   for (i = 0; i < n; i++) { n     if (brr[i] > maxz) { n       maxz = brr[i]; n       winner = i; n     } n   } n   cout << winner + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 500; n const long long INF = 1e9; n const long long BITS = 64; n const long double EPS = 1e-9; n void bye() { n   cout <<   n ; n   exit(0); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   long long all = 0; n   vector<string> g(n); n   vector<int> pref(n); n   for (int i = 0; i < n; i++) { n     cin >> g[i]; n     all += (long long)(g[i]).size(); n     pref[i] = (i) ? pref[i - 1] + (long long)(g[i]).size() + 1 n                   : (long long)(g[i]).size(); n   } n   all += n - 1; n   vector<vector<bool> > eq(n, vector<bool>(n)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       eq[i][j] = g[i] == g[j]; n     } n   } n   vector<vector<int> > dp(n, vector<int>(n)); n   for (int i = n - 1; i >= 0; i--) { n     for (int j = n - 1; j >= 0; j--) { n       if (eq[i][j]) { n         if (i + 1 < n && j + 1 < n) { n           dp[i][j] = dp[i + 1][j + 1] + 1; n         } else { n           dp[i][j] = 1; n         } n       } n     } n   } n   long long ans = all; n   for (int i = 0; i < n; i++) { n     for (int k = 0; i + k < n; k++) { n       long long now = (pref[i + k] - ((i) ? pref[i - 1] + 1 : 0)); n       long long cnt = 1; n       for (int j = i + k + 1; j + k < n; j++) { n         if (dp[i][j] > k) { n           cnt++; n           j += k; n         } n       } n       if (cnt > 1) { n         ans = min(ans, all + cnt * (k + 1) - now * cnt); n       } n     } n   } n   cout << ans; n   bye(); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 50; n const int MOD = 1e9 + 9; n int n, k, a, b, c, d, m, L, R, tot = 1; n pair<int, int> cities[MAXN]; n struct TreeNode { n   int x, l, r, lson, rson; n   pair<int, int> v; n } tree[MAXN]; n int newNode(int &x) { return x = ++tot; } n pair<int, int> calc(pair<int, int> l, pair<int, int> r) { n   return {l.second + r.second, max(l.first + r.second, l.second + r.first) + 1}; n } n void build(int o, int l, int r) { n   int max_y = -1, max_id = l; n   for (int i = l; i <= r; i++) { n     if (cities[i].second > max_y) { n       max_y = cities[i].second; n       max_id = i; n     } n   } n   tree[o].l = cities[l].first; n   tree[o].r = cities[r].first; n   tree[o].x = cities[max_id].first; n   if (l < max_id) build(newNode(tree[o].lson), l, max_id - 1); n   if (max_id < r) build(newNode(tree[o].rson), max_id + 1, r); n   tree[o].v = calc(tree[tree[o].lson].v, tree[tree[o].rson].v); n } n pair<int, int> query(int o, int l, int r) { n   if (o == 0 || l > tree[o].r || r < tree[o].l) return {-1, 0}; n   if (l <= tree[o].l && tree[o].r <= r) return tree[o].v; n   if (r < tree[o].x) return query(tree[o].lson, l, r); n   if (l > tree[o].x) return query(tree[o].rson, l, r); n   return calc(query(tree[o].lson, l, r), query(tree[o].rson, l, r)); n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i < k; i++) { n     scanf( %d%d , &cities[i].first, &cities[i].second); n   } n   scanf( %d%d%d%d , &a, &b, &c, &d); n   for (int i = k; i < n; i++) { n     cities[i].first = (1LL * a * cities[i - 1].first + b) % MOD; n     cities[i].second = (1LL * c * cities[i - 1].second + d) % MOD; n   } n   sort(cities, cities + n); n   tree[0].v = {-1, 0}; n   build(1, 0, n - 1); n   scanf( %d , &m); n   while (m--) { n     scanf( %d%d , &L, &R); n     printf( %d n , query(1, L, R).second); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j; n   cin >> n; n   vector<pair<int, int>> v; n   for (i = 0; i < n; i++) { n     int x, y; n     cin >> x >> y; n     v.push_back(make_pair(x, y)); n   } n   int ans = 0; n   int total = 0; n   bool u, d, l, r; n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) { n       if (v[i].first > v[j].first && v[i].second == v[j].second) d = true; n       if (v[i].first < v[j].first && v[i].second == v[j].second) u = true; n       if (v[i].second > v[j].second && v[i].first == v[j].first) l = true; n       if (v[i].second < v[j].second && v[i].first == v[j].first) r = true; n     } n     if (d && u && l && r) total++; n     d = false; n     u = false; n     l = false; n     r = false; n   } n   cout << total; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500 + 7; n long long mod = 1e9 + 7; n bool solve(long long l, long long r, long long mx, long long& ans) { n   if (r - l + 1 > mx) { n     return false; n   } n   ans = -l + 1; n   return true; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     string s; n     cin >> s; n     int x = 0, y = 0, mnx = 0, mxx = 0, mny = 0, mxy = 0; n     long long ansX = 1, ansY = 1; n     for (auto i : s) { n       x += i ==  D ; n       x -= i ==  U ; n       y += i ==  R ; n       y -= i ==  L ; n       mnx = min(x, mnx); n       mxx = max(x, mxx); n       mny = min(y, mny); n       mxy = max(y, mxy); n       long long tmpX, tmpY; n       if (solve(mnx, mxx, n, tmpX) && solve(mny, mxy, m, tmpY)) { n         ansX = tmpX, ansY = tmpY; n         continue; n       } n       break; n     } n     cout << ansX <<     << ansY <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n; n   if (n % 2 == 1) n     m = (n / 2) * (n / 2 + 1); n   else n     m = (n / 2) * (n / 2); n   cout << m << endl; n   for (int i = 1; i <= n / 2; i++) { n     for (int j = n / 2 + 1; j <= n; j++) { n       cout << i <<     << j << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ar[1000001] = {}; n long long n, a, b; n void shift(long long l, long long r) { n   for (long long f = (long long)(l); f <= (long long)(r - 1); ++f) { n     swap(ar[f], ar[f + 1]); n   } n } n void Do(long long x, long long y) { n   long i = 0; n   for (long long f = (long long)(1); f <= (long long)(x); ++f) { n     shift(i, i + a - 1); n     i += a; n   } n   for (long long f = (long long)(1); f <= (long long)(y); ++f) { n     shift(i, i + b - 1); n     i += b; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed << setprecision(15); n   cin >> n >> a >> b; n   for (long long f = (long long)(0); f <= (long long)(n - 1); ++f) { n     ar[f] = f + 1; n   } n   for (long long f = (long long)(0); f <= (long long)(1000000); ++f) { n     long long tmp = round((n - a * f) / b); n     if (tmp * b + f * a == n && tmp >= 0) { n       Do(f, tmp); n       for (long long f = (long long)(0); f <= (long long)(n - 1); ++f) { n         cout << ar[f] <<    ; n       } n       return 0; n     } n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int a1, b1, flag1, flag2, flag3, flag4; n     scanf( %d%d , &a1, &b1); n     int a2, b2; n     scanf( %d%d , &a2, &b2); n     flag1 = flag2 = flag3 = flag4 = 0; n     if (a1 == a2) { n       flag1 = ((b1 + b2) == a1) ? 1 : 0; n     } n     if (a1 == b2) { n       flag2 = ((b1 + a2) == a1) ? 1 : 0; n     } n     if (b1 == a2) { n       flag3 = ((b2 + a1) == b1) ? 1 : 0; n     } n     if (b1 == b2) { n       flag4 = ((a1 + a2) == b1) ? 1 : 0; n     } n     if (flag1 || flag2 || flag3 || flag4) { n       printf( YES n ); n     } else { n       printf( NO n ); n     } n   } n } n


#include <bits/stdc++.h> n int inp() { n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   int sum = 0; n   while (c >=  0  && c <=  9 ) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   return sum; n } n struct BIT { n   int f[2010]; n   void init() { memset(f, 0, sizeof(f)); } n   void modify(int x, int c) { n     for (int i = x; i <= 2001; i += ((i) & (-i))) f[i] += c; n   } n   int query(int x) { n     int sum = 0; n     for (int i = x; i; i -= ((i) & (-i))) sum += f[i]; n     return sum; n   } n } t1, t2; n long long fact[2010], f[2010][2010], fpow[2010]; n int s[2010][2010]; n int cnt[2010]; n void inc(int x) { n   if (x == 0) return; n   if (++cnt[x] == 2) t1.modify(x, 1); n } n int main() { n   int n = inp(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) s[i][j] = inp(); n   fact[0] = 1; n   for (int i = 1; i <= n; i++) { n     fact[i] = fact[i - 1] * i % 998244353; n   } n   f[0][0] = 1; n   fpow[0] = 1; n   for (int i = 1; i <= n; i++) { n     f[i][0] = fact[i]; n     for (int j = 1; j <= i; j++) { n       f[i][j] = (f[i][j - 1] - f[i - 1][j - 1] + 998244353) % 998244353; n     } n   } n   for (int i = 1; i <= n; i++) fpow[i] = fpow[i - 1] * f[n][n] % 998244353; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     memset(cnt, 0, sizeof(cnt)); n     t1.init(); n     t2.init(); n     long long sum = 0; n     for (int j = n; j >= 1; j--) { n       inc(s[i][j]); n       inc(s[i - 1][j]); n       t2.modify(s[i][j], 1); n       long long x = t1.query(s[i][j] - 1); n       long long y = t2.query(s[i][j] - 1) - x; n       long long num = t1.query(2000); n       if (cnt[s[i - 1][j]] == 2 && s[i - 1][j] < s[i][j]) x--; n       if (cnt[s[i - 1][j]] == 2) num--; n       if (num) sum += x * f[n - j][num - 1]; n       sum += y * f[n - j][num]; n       sum %= 998244353; n     } n     ans += sum * fpow[n - i]; n     ans %= 998244353; n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <class T, class T1> n inline void gn(T &first, T1 &second) { n   gn(first); n   gn(second); n } n template <class T, class T1, class T2> n inline void gn(T &first, T1 &second, T2 &z) { n   gn(first); n   gn(second); n   gn(z); n } n template <class T> n inline void println(T first) { n   print(first); n   putchar(  n ); n } n template <class T> n inline void printsp(T first) { n   print(first); n   putchar(   ); n } n template <class T1, class T2> n inline void print(T1 x1, T2 x2) { n   printsp(x1), println(x2); n } n template <class T1, class T2, class T3> n inline void print(T1 x1, T2 x2, T3 x3) { n   printsp(x1), printsp(x2), println(x3); n } n template <class T1, class T2, class T3, class T4> n inline void print(T1 x1, T2 x2, T3 x3, T4 x4) { n   printsp(x1), printsp(x2), printsp(x3), println(x4); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n int main() { n   int n, first; n   cin >> n >> first; n   if (n == 5 and first == 5) { n     puts( >...v ); n     puts( v.<.. ); n     puts( ..^.. ); n     puts( >.... ); n     puts( ..^.< ); n     puts( 1 1 ); n     return 0; n   } n   if (n == 3 and first == 2) { n     puts( >vv ); n     puts( ^<. ); n     puts( ^.< ); n     puts( 1 3 ); n     return 0; n   } n   for (int i = 1; i <= n; i += 2) { n     putchar( v ); n     for (int j = 2; j <= n / 2; j++) { n       if (j & 1) n         putchar( . ); n       else n         putchar( < ); n     } n     for (int j = n / 2 + 1; j <= n; j++) { n       putchar( < ); n     } n     puts(  ); n     for (int j = 1; j <= n / 2; j++) putchar( > ); n     for (int j = n / 2 + 1; j < n; j++) { n       if (j & 1) n         putchar( . ); n       else n         putchar( > ); n     } n     puts( ^ ); n   } n   print(1, 1); n   return 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void rc(char &a) { n   a = 0; n   register int x = getchar_unlocked(); n   while (x <  a  || x >  z ) { n     if (x ==   n ) { n       a =  0 ; n       return; n     } n     x = getchar_unlocked(); n   } n   a = x; n } n const int maxn = 100100; n int key; n int main() { n   ios_base::sync_with_stdio(0); n   string ans =   ; n   char cur; n   while (true) { n     rc(cur); n     if (cur ==  0 ) break; n     if (cur ==  a ) { n       if (key == 0) n         ans +=  a ; n       else if (key == 1) { n         ans +=  a ; n         key = 2; n       } else { n         ans +=  a ; n       } n     } else { n       if (key == 1) n         ans += (cur - 1); n       else if (key == 0) { n         ans += (cur - 1); n         key = 1; n       } else { n         ans += cur; n       } n     } n   } n   if (key == 0) { n     ans = ans.substr(0, ans.size() - 1) +  z ; n     cout << ans <<   n ; n     return 0; n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline string tostring(const T &x) { n   ostringstream os; n   os << x; n   return os.str(); n } n inline int toint(const string &s) { n   istringstream is(s); n   int x; n   is >> x; n   return x; n } n inline int todecimal(string s) { n   int a = 0; n   for (int i = 0; i < s.size(); i++) a = 2 * a + (s[i] -  0 ); n   return a; n } n inline string tobinary(int a) { n   string s; n   while (a != 0) { n     s = (char)(a % 2 +  0 ) + s; n     a >>= 1; n   } n   return s; n } n template <typename T> n inline T sqr(T x) { n   return x * x; n } n template <typename T> n T gcd(T a, T b) { n   return (b == 0) ? abs(a) : gcd(b, a % b); n } n inline int isvowel(char c) { n   if (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u ) return 1; n   return 0; n } n inline int isprime(int a) { n   for (int i = 2; i * i <= a; i++) n     if (!(a % i)) return 0; n   return 1; n } n inline void inp(int &n) { n   n = 0; n   int ch = getchar(); n   int sign = 1; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) sign = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) n     n = (n << 3) + (n << 1) + ch -  0 , ch = getchar(); n   n = n * sign; n } n const int dx4[4] = {0, 0, 1, -1}; n const int dy4[4] = {1, -1, 0, 0}; n const int dx8[8] = {0, 0, 1, 1, 1, -1, -1, -1}; n const int dy8[8] = {1, -1, 0, 1, -1, 1, 0, -1}; n class node { n  public: n   int x, y, z; n   node(int a, int b, int c) { n     x = a; n     y = b; n     z = c; n   } n }; n bool operator<(const node &a, const node &b) { return a.x > b.x; } n bool compare(const node &a, const node &b) { return a.x < b.x; } n int n, m; n void solve() { n   cout << n + m - 1 << endl; n   for (int i = 0; i < m; i++) { n     cout << 1 <<     << i + 1 << endl; n   } n   for (int i = 0; i < n - 1; i++) { n     cout << i + 2 <<     << 1 << endl; n   } n } n void input() { cin >> n >> m; } n int main() { n   int testcase = 1; n   for (int i = 0; i < testcase; i++) { n     input(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> v; n int res; n int countOne(vector<vector<int>> &a, int i, int j) { n   int res = 0; n   if (a[i][j] == 1) res++; n   if (a[i + 1][j] == 1) res++; n   if (a[i][j + 1] == 1) res++; n   if (a[i + 1][j + 1] == 1) res++; n   return res; n } n void switchZeroOne(vector<vector<int>> &a, int i, int j) { n   int cnt = countOne(a, i, j); n   while (cnt != 0) { n     if (cnt == 4) { n       v[res].push_back(i); n       v[res].push_back(j); n       v[res].push_back(i); n       v[res].push_back(j + 1); n       v[res].push_back(i + 1); n       v[res].push_back(j); n       res++; n       a[i][j] = 0; n       a[i][j + 1] = 0; n       a[i + 1][j] = 0; n       cnt = 1; n     } else if (cnt == 1) { n       int switchZero = 0; n       for (int x = i; x < i + 2; x++) { n         for (int y = j; y < j + 2; y++) { n           if (a[x][y] == 1) { n             a[x][y] = 0; n             v[res].push_back(x); n             v[res].push_back(y); n           } else if (switchZero < 2) { n             a[x][y] = 1; n             v[res].push_back(x); n             v[res].push_back(y); n             switchZero++; n           } n         } n       } n       res++; n       cnt = 2; n     } else if (cnt == 2) { n       int switchOne = 0; n       for (int x = i; x < i + 2; x++) { n         for (int y = j; y < j + 2; y++) { n           if (a[x][y] == 0) { n             a[x][y] = 1; n             v[res].push_back(x); n             v[res].push_back(y); n           } else if (switchOne < 1) { n             a[x][y] = 0; n             v[res].push_back(x); n             v[res].push_back(y); n             switchOne++; n           } n         } n       } n       res++; n       cnt = 3; n     } else if (cnt == 3) { n       for (int x = i; x < i + 2; x++) { n         for (int y = j; y < j + 2; y++) { n           if (a[x][y] == 1) { n             a[x][y] = 0; n             v[res].push_back(x); n             v[res].push_back(y); n           } n         } n       } n       res++; n       cnt = 0; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t, n, m; n   string s; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     vector<vector<int>> a(n + 1, vector<int>(m + 1)); n     for (int i = 1; i <= n; i++) { n       cin >> s; n       for (int j = 1; j <= m; j++) { n         a[i][j] = s[j - 1] -  0 ; n       } n     } n     v = vector<vector<int>>(50000); n     res = 0; n     if (n > 2) { n       for (int i = 1; i <= n - 2; i++) { n         for (int j = 1; j <= m - 1; j++) { n           if (a[i][j] == 1 && a[i][j + 1] == 1) { n             v[res].push_back(i); n             v[res].push_back(j); n             v[res].push_back(i); n             v[res].push_back(j + 1); n             v[res].push_back(i + 1); n             v[res].push_back(j); n             res++; n             a[i][j] = 0; n             a[i][j + 1] = 0; n             a[i + 1][j] ^= 1; n           } else if (a[i][j] == 1) { n             v[res].push_back(i); n             v[res].push_back(j); n             v[res].push_back(i + 1); n             v[res].push_back(j); n             v[res].push_back(i + 1); n             v[res].push_back(j + 1); n             res++; n             a[i][j] = 0; n             a[i + 1][j] ^= 1; n             a[i + 1][j + 1] ^= 1; n           } else if (a[i][j + 1] == 1) { n             v[res].push_back(i); n             v[res].push_back(j + 1); n             v[res].push_back(i + 1); n             v[res].push_back(j); n             v[res].push_back(i + 1); n             v[res].push_back(j + 1); n             res++; n             a[i][j + 1] = 0; n             a[i + 1][j] ^= 1; n             a[i + 1][j + 1] ^= 1; n           } n         } n       } n       for (int j = 1; j <= m - 1; j += 2) { n         switchZeroOne(a, n - 1, j); n       } n       if (m % 2 == 1) switchZeroOne(a, n - 1, m - 1); n     } else { n       for (int j = 1; j <= m - 2; j++) { n         for (int i = 1; i <= n - 1; i++) { n           if (a[i][j] == 1 && a[i + 1][j] == 1) { n             v[res].push_back(i); n             v[res].push_back(j); n             v[res].push_back(i); n             v[res].push_back(j + 1); n             v[res].push_back(i + 1); n             v[res].push_back(j); n             res++; n             a[i][j] = 0; n             a[i][j + 1] ^= 1; n             a[i + 1][j] = 0; n           } else if (a[i][j] == 1) { n             v[res].push_back(i); n             v[res].push_back(j); n             v[res].push_back(i); n             v[res].push_back(j + 1); n             v[res].push_back(i + 1); n             v[res].push_back(j + 1); n             res++; n             a[i][j] = 0; n             a[i][j + 1] ^= 1; n             a[i + 1][j + 1] ^= 1; n           } else if (a[i + 1][j] == 1) { n             v[res].push_back(i); n             v[res].push_back(j + 1); n             v[res].push_back(i + 1); n             v[res].push_back(j); n             v[res].push_back(i + 1); n             v[res].push_back(j + 1); n             res++; n             a[i][j + 1] ^= 1; n             a[i + 1][j] = 0; n             a[i + 1][j + 1] ^= 1; n           } n         } n       } n       for (int i = 1; i <= n - 1; i += 2) { n         switchZeroOne(a, i, m - 1); n       } n       if (n % 2 == 1) switchZeroOne(a, n - 1, m - 1); n     } n     cout << res <<   n ; n     for (int i = 0; i < res; i++) { n       for (int j = 0; j < v[i].size(); j++) { n         cout << v[i][j] <<    ; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, d, v; n vector<long long> res; n void solve() { n   cin >> x >> d; n   res.clear(); n   v = 1; n   for (int j = 0; j <= 30; j++) { n     if ((1 << j) & x) { n       for (int k = 1; k <= j; k++) res.push_back(v); n       v += d + 1; n       res.push_back(v); n       v += d + 1; n     } n   } n   cout << res.size() <<   n ; n   for (int i = 0; i < res.size(); i++) cout << res[i] <<    ; n   cout <<   n ; n } n int main() { n   std::ios::sync_with_stdio(0); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, big, small, counter = 0, y, b = 0; n   cin >> x; n   int a[x]; n   for (int i = 0; i < x; i++) { n     cin >> y; n     a[i] = y; n   } n   big = max_element(a, a + x) - a; n   for (int i = 0; i < big; i++) { n     swap(a[big - i], a[big - 1 - i]); n     counter++; n   } n   small = min_element(a, a + x) - a; n   for (int i = 0; i < x; i++) { n     if (a[i] == a[small]) small = i; n   } n   for (int i = x - 1; i > small; i--, b++) { n     swap(a[small + b], a[small + 1 + b]); n     counter++; n   } n   cout << counter; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n int main() { n   long long m, n, i; n   while (cin >> n >> m) { n     long long a[n + 1], b; n     a[0] = 0; n     for (i = 0; i < n; i++) { n       cin >> a[i + 1]; n       a[i + 1] += a[i]; n     } n     for (i = 0; i < m; i++) { n       cin >> b; n       auto j = lower_bound(a, a + n, b) - a; n       cout << j <<     << b - a[j - 1] <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long max(long long a, long long b) { n   if (a > b) { n     return a; n   } else { n     return b; n   } n } n long long min(long long a, long long b) { n   if (a < b) { n     return a; n   } else { n     return b; n   } n } n long long n, m, p; n int a[200003], b[200003]; n int main() { n   multiset<int> st; n   scanf( %I64d%I64d%I64d , &n, &m, &p); n   int k = 0; n   for (int i = (1); i <= (n); i++) { n     scanf( %d , &a[i]); n   } n   for (int i = (1); i <= (m); i++) { n     scanf( %d , &b[i]); n   } n   int ans[200001], anscnt = 0; n   for (int i = (1); i <= (p); i++) { n     int q = i; n     st.clear(); n     multiset<int>::iterator it; n     if (q + (m - 1) * p <= n) { n       for (int j = (1); j <= (m); j++) st.insert(b[j]); n       for (int j = (0); j <= (m - 2); j++) { n         it = st.find(a[j * p + q]); n         if (it != st.end()) { n           st.erase(it); n         } else { n           st.insert(-1 * a[j * p + q]); n         } n       } n     } n     while (q + (m - 1) * p <= n) { n       it = st.find(a[(m - 1) * p + q]); n       if (it != st.end()) { n         st.erase(it); n       } else { n         st.insert(-1 * a[(m - 1) * p + q]); n       } n       if (st.empty()) ans[++anscnt] = q; n       it = st.find(a[q] * -1); n       if (it != st.end()) { n         st.erase(it); n       } else { n         st.insert(a[q]); n       } n       q += p; n     } n   } n   sort(ans + 1, ans + anscnt + 1); n   printf( %d n , anscnt); n   for (int i = (1); i <= (anscnt); i++) printf( %d  , ans[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n template <class T, class U> n inline void Max(T &a, U b) { n   if (a < b) a = b; n } n template <class T, class U> n inline void Min(T &a, U b) { n   if (a > b) a = b; n } n inline void add(int &a, int b) { n   a += b; n   while (a >= 1000000007) a -= 1000000007; n } n int pow(int a, int b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = ans * (long long)a % 1000000007; n     a = (long long)a * a % 1000000007; n     b >>= 1; n   } n   return ans; n } n char s[5010]; n int dp[5010][5010], ne[5010][26], f[5010], inv[5010], sum[5010]; n int C(int a, int b) { n   return 1LL * f[a] * inv[a - b] % 1000000007 * inv[b] % 1000000007; n } n int main() { n   int T, i, j, k, ca = 0, m, K = 0, n; n   scanf( %d%s , &n, s); n   f[0] = 1; n   for (int i = 1; i < n + 1; i++) f[i] = f[i - 1] * (long long)i % 1000000007; n   inv[n] = pow(f[n], 1000000007 - 2); n   for (int i = n - 1; i >= 0; i--) n     inv[i] = inv[i + 1] * (long long)(i + 1) % 1000000007; n   int last[26] = {0}; n   memset(last, -1, sizeof(last)); n   sum[0] = 1; n   for (int i = 1; i < n + 1; i++) { n     int c = s[i - 1] -  a ; n     for (int j = i + 1 - 1; j >= 1; j--) { n       if (last[c] != -1) { n         dp[i][j] = (sum[j - 1] - dp[last[c]][j - 1] + 1000000007) % 1000000007; n         sum[j] = (0LL + sum[j] + dp[i][j] - dp[last[c]][j] + 1000000007) % n                  1000000007; n       } else { n         dp[i][j] = sum[j - 1]; n         add(sum[j], dp[i][j]); n       } n     } n     last[c] = i; n   } n   int ans = 0; n   for (int i = 1; i < n + 1; i++) n     if (sum[i]) { n       add(ans, 1LL * sum[i] * C(n - 1, i - 1) % 1000000007); n     } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( Ofast ) n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   int a[n], ans = 0; n   for (int i = (0); i < (n); ++i) cin >> a[i]; n   for (int l = (0); l < (n); ++l) { n     long long k = 2LL << (int)log2(a[l]), sum = 0; n     for (int r = (l + 2); r < (n); ++r) { n       sum += a[r - 1]; n       if (sum >= k) break; n       if ((a[l] >= a[r]) and (a[l] ^ a[r]) == sum) ++ans; n     } n   } n   for (int r = (n)-1; r >= (0); --r) { n     long long k = 2LL << (int)log2(a[r]), sum = 0; n     for (int l = (r - 1) - 1; l >= (0); --l) { n       sum += a[l + 1]; n       if (sum >= k) break; n       if ((a[l] < a[r]) and (a[r] ^ a[l]) == sum) ++ans; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a = 1; n   cin >> n; n   for (int i = 0; i < n; i++) { n     if (a <= 2) { n       cout <<  a ; n       a++; n     } else if (a > 2 && a <= 4) { n       cout <<  b ; n       a++; n     } else { n       cout <<  a ; n       a = 2; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n template <typename T> n bool mycomp(T x, T y) { n   return (x == y); n } n bool paircomp(const pair<long long int, long long int> &x, n               const pair<long long int, long long int> &y) { n   return x.second < y.second; n } n void solve() { n   long long int n, m; n   cin >> n >> m; n   string s[n]; n   for (long long int i = 0; i < n; i++) cin >> s[i]; n   long long int labx, laby; n   for (long long int i = 0; i < n; i++) { n     for (long long int j = 0; j < m; j++) { n       if (s[i][j] ==  L ) labx = i, laby = j; n     } n   } n   bool vis[n][m]; n   memset(vis, false, sizeof(vis)); n   queue<pair<long long int, long long int> > qe; n   qe.push({labx, laby}); n   vis[labx][laby] = true; n   while (!qe.empty()) { n     long long int x = qe.front().first, y = qe.front().second; n     qe.pop(); n     if (s[x][y] !=  L ) { n       long long int no = 0; n       if (x && s[x - 1][y] ==  . ) no++; n       if (x < n - 1 && s[x + 1][y] ==  . ) no++; n       if (y && s[x][y - 1] ==  . ) no++; n       if (y < m - 1 && s[x][y + 1] ==  . ) no++; n       if (no <= 1) n         s[x][y] =  + , vis[x][y] = true; n       else n         continue; n     } n     if (x && !vis[x - 1][y] && s[x - 1][y] !=  # ) qe.push({x - 1, y}); n     if (x < n - 1 && !vis[x + 1][y] && s[x + 1][y] !=  # ) qe.push({x + 1, y}); n     if (y && !vis[x][y - 1] && s[x][y - 1] !=  # ) qe.push({x, y - 1}); n     if (y < m - 1 && !vis[x][y + 1] && s[x][y + 1] !=  # ) qe.push({x, y + 1}); n   } n   for (long long int i = 0; i < n; i++) { n     cout << s[i] <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 301; n string s[N][N]; n vector<pair<pair<int, int>, pair<int, int> > > f(int n, int k) { n   vector<pair<pair<int, int>, pair<int, int> > > ans; n   pair<int, int> f0 = {1, 0}, f1 = {0, 1}; n   int i, j, l; n   for (i = 0; i < s[0][0].size(); ++i) n     if (s[0][0][i] ==  0 ) n       ans.push_back({{0, 0}, f0}); n     else n       ans.push_back({{0, 0}, f1}); n   for (i = 1; i < k; ++i) n     for (l = 0; l < s[0][i].size(); ++l) { n       ans.push_back({{0, i}, {0, 0}}); n       if (s[0][i][l] ==  0 ) n         ans.push_back({{0, 0}, f0}); n       else n         ans.push_back({{0, 0}, f1}); n     } n   for (i = 1; i < n; ++i) n     for (l = 0; l < s[i][0].size(); ++l) { n       ans.push_back({{i, 0}, {0, 0}}); n       if (s[i][0][l] ==  0 ) n         ans.push_back({{0, 0}, f0}); n       else n         ans.push_back({{0, 0}, f1}); n     } n   for (i = 1; i < n; ++i) n     for (j = 1; j < k; ++j) n       for (l = 0; l < s[i][j].size(); ++l) n         if (s[i][j][l] ==  0 ) { n           ans.push_back({{i, j}, {i, 0}}); n           if (i != 1) ans.push_back({{i, 0}, f0}); n         } else { n           ans.push_back({{i, j}, {0, j}}); n           if (j != 1) ans.push_back({{0, j}, f1}); n         } n   return ans; n } n int main() { n   int i, j, k, n, p, l; n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (i = 0; i < n; ++i) n     for (j = 0; j < k; ++j) { n       cin >> s[i][j]; n       for (l = 0; l < s[i][j].size() / 2; ++l) n         swap(s[i][j][l], s[i][j][s[i][j].size() - l - 1]); n     } n   vector<pair<pair<int, int>, pair<int, int> > > ans1, ans2; n   ans1 = f(n, k); n   for (i = 0; i < n; ++i) n     for (j = 0; j < k; ++j) cin >> s[i][j]; n   ans2 = f(n, k); n   cout << ans1.size() + ans2.size() << endl; n   for (i = 0; i < ans1.size(); ++i) n     cout << ans1[i].first.first + 1 <<     << ans1[i].first.second + 1 <<     n          << ans1[i].second.first + 1 <<     << ans1[i].second.second + 1 n          <<   n ; n   for (i = ans2.size() - 1; i >= 0; --i) n     cout << ans2[i].second.first + 1 <<     << ans2[i].second.second + 1 <<     n          << ans2[i].first.first + 1 <<     << ans2[i].first.second + 1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long mod = 1e9 + 7; n unsigned long long ncr(long long int n, long long int k) { n   unsigned long long res = 1; n   if (k > n - k) k = n - k; n   for (int i = 0; i < k; ++i) { n     res *= (n - i); n     res /= (i + 1); n     res = res; n   } n   return res; n } n bool prime(unsigned long long num) { n   bool flag = true; n   for (unsigned long long i = 2; i * i <= num; i++) { n     if (num % i == 0) { n       flag = false; n       break; n     } n   } n   return flag; n } n long long ipow(long long base, long long exp) { n   long long result = 1; n   for (;;) { n     if (exp & 1) { n       result *= base; n       result = result % mod; n     } n     exp >>= 1; n     if (!exp) break; n     base *= base; n     base = base % mod; n   } n   return result % mod; n } n long long abs(long long a, long long b) { n   if ((a - b) > 0) n     return (a - b); n   else n     return (b - a); n } n long long MOD = 998244353; n long long add(long long a, long long b) { n   a += b; n   if (a >= mod) a -= mod; n   if (a < 0) a += mod; n   return a; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long der(int k) { n   long long res = 0; n   long long fact = 1; n   for (int i = 2; i <= k; i++) fact *= i; n   int s = 1; n   long long j = 1; n   for (int i = 2; i <= k; i++) { n     j = j * i; n     long long first = (fact / j) * s; n     res += first; n     s = s * -1; n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string s; n   cin >> s; n   set<int> one, z; n   int len = s.length(); n   for (int i = 0; i < len; i++) { n     if (s[i] ==  0 ) n       z.insert(i + 1); n     else n       one.insert(i + 1); n   } n   vector<vector<int>> subs; n   vector<int> sub; n   while (true) { n     if (z.empty()) break; n     auto it = z.begin(); n     sub.push_back(*it); n     int val = *it; n     z.erase(it); n     while (!z.empty()) { n       auto it1 = one.lower_bound(val); n       if (it1 == one.end()) break; n       it = z.lower_bound(*it1); n       if (it == z.end()) break; n       int val1 = *it1; n       val = *it; n       sub.push_back(val1); n       one.erase(it1); n       sub.push_back(val); n       z.erase(it); n     } n     subs.push_back(sub); n     sub.clear(); n   } n   if (!one.empty()) { n     cout <<  -1 ; n     return 0; n   } else { n     cout << subs.size() << endl; n     for (int i = 0; i < subs.size(); i++) { n       cout << subs[i].size() <<    ; n       for (int j = 0; j < subs[i].size(); j++) { n         cout << subs[i][j] <<    ; n       } n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int h; n const int mx = 2e5 + 5; n vector<int> a(mx); n void eval1() { n   cout << 0; n   int x = 1, y = 1; n   for (int i = 1; i <= h; i++) { n     for (int j = 0; j < a[i]; j++) { n       cout <<     << x; n       y++; n     } n     x = y; n   } n   cout << endl; n } n void eval2() { n   cout << 0; n   int x = 1, y = 1, z = 1; n   for (int i = 1; i <= h; i++) { n     int p = a[i] / 2, q = a[i] - p; n     vector<int> cand; n     if (p) { n       for (int j = 0; j < p; j++) { n         cout <<     << x; n         z++; n         cand.push_back(z); n       } n       x = cand[0]; n     } n     if (q) { n       cand.clear(); n       for (int j = 0; j < q; j++) { n         cout <<     << y; n         z++; n         cand.push_back(z); n       } n       if (p == 0) x = cand[0]; n       y = cand[0]; n     } n   } n   cout << endl; n } n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   cin >> h; n   for (int i = 0; i <= h; i++) cin >> a[i]; n   int vio = -1; n   for (int i = 0; i < h; i++) n     if (a[i] > 1 && a[i + 1] > 1) { n       vio = i; n       break; n     } n   if (vio == -1) { n     cout <<  perfect  << endl; n     return 0; n   } n   cout <<  ambiguous  << endl; n   eval1(); n   eval2(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 10; n set<int> s[M]; n int n, p[M]; n bool check(int x) { n   vector<int> b; n   while (x) { n     b.push_back(x % 10); n     x /= 10; n   } n   if (b.size() > n) return false; n   for (int i = 0; i < b.size(); i++) n     if (s[p[i]].find(b[i]) == s[p[i]].end()) return false; n   return true; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     p[i] = i; n     for (int j = 0; j < 6; j++) { n       int x; n       cin >> x; n       s[i].insert(x); n     } n   } n   for (int x = 1;; x++) { n     int fnd = false; n     for (int i = 0; i < n; i++) p[i] = i; n     do { n       if (check(x)) fnd = true; n     } while (next_permutation(p, p + n)); n     if (!fnd) return cout << x - 1 << endl, 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[220000]; n int s[220000]; n int g[220000]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) { n     scanf( %d%d , s + i, g + i); n     a[i] = s[i] + g[i]; n   } n   for (int i = 0; i < n - 1; ++i) a[i + 1] = min(a[i + 1], a[i] + 1); n   for (int i = n - 1; i > 0; --i) a[i - 1] = min(a[i - 1], a[i] + 1); n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     if (a[i] < s[i]) { n       printf( -1 n ); n       return 0; n     } n     ans += a[i] - s[i]; n   } n   printf( %lld n , ans); n   for (int i = 0; i < n; ++i) printf( %d  , a[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   vector<int> a(n), b(n); n   for (int i = 0; i < n; ++i) cin >> a[i] >> b[i]; n   int maxCap = b[0], curCap = b[0]; n   for (int i = 1; i < n; ++i) { n     curCap = curCap - a[i] + b[i]; n     maxCap = max(maxCap, curCap); n   } n   cout << maxCap <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Tp> n inline void read(Tp &x) { n   static char c; n   static bool neg; n   x = 0, c = getchar(), neg = false; n   for (; !isdigit(c); c = getchar()) { n     if (c ==  - ) { n       neg = true; n     } n   } n   for (; isdigit(c); c = getchar()) { n     x = x * 10 + c -  0 ; n   } n   if (neg) { n     x = -x; n   } n } n const int N = 100 + 5; n int k, n = 0; n int mp[N][N]; n inline int C(int n, int m) { n   if (m == 2) { n     return n * (n - 1) / 2; n   } else if (m == 3) { n     return n * (n - 1) * (n - 2) / 6; n   } else { n     return -1; n   } n } n int main() { n   read(k); n   for (; C(n, 3) <= k; ++n) n     ; n   --n; n   for (int i = 1; i <= n; ++i) { n     for (int j = i + 1; j <= n; ++j) { n       mp[i][j] = mp[j][i] = 1; n     } n   } n   k -= C(n, 3); n   while (k > 0) { n     int t = 0; n     for (; C(t, 2) <= k; ++t) n       ; n     --t; n     ++n; n     for (int i = 1; i <= t; ++i) { n       mp[n][i] = mp[i][n] = 1; n     } n     k -= C(t, 2); n   } n   printf( %d n , n); n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= n; ++j) { n       putchar( 0  + mp[i][j]); n     } n     puts(  ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 2e5 + 3; n long long n, ans, sz[N], up[N], down[N]; n vector<long long> q[N]; n void dfs1(long long v, long long p) { n   sz[v] = 1; n   for (auto i : q[v]) { n     if (i != p) { n       dfs1(i, v); n       sz[v] += sz[i]; n       down[v] += down[i] + sz[i]; n     } n   } n } n void dfs2(long long v, long long p) { n   if (p != -1) { n     up[v] = down[p] + up[p] - (down[v] + sz[v]) + (sz[1] - sz[v]); n   } n   ans = max(ans, down[v] + up[v] + n); n   for (auto i : q[v]) { n     if (i != p) { n       dfs2(i, v); n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (long long i = 1; i < n; i++) { n     long long x, y; n     cin >> x >> y; n     q[x].push_back(y); n     q[y].push_back(x); n   } n   dfs1(1, -1); n   dfs2(1, -1); n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e6 + 5; n struct T { n   int l, r, i; n   bool operator<(T const& other) { n     return (l == other.l ? r < other.r : l < other.l); n   } n }; n int con[MAX][2]; n pair<int, int> num[2 * MAX]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k, l, r; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> l >> r; n     num[i * 2] = {l, 1}; n     num[i * 2 + 1] = {r, 2}; n   } n   sort(num, num + 2 * n); n   int c = 0, a = 0; n   for (int i = 0; i < 2 * n; i++) { n     bool go = true; n     if (num[i].second == 1) n       a++; n     else n       go = false, a--; n     if (a == k && go) con[c][0] = num[i].first; n     if (a == k - 1 && !go) con[c][1] = num[i].first, c++; n   } n   cout << c << endl; n   for (int i = 0; i < c; i++) cout << con[i][0] <<     << con[i][1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char BUF_I[1048576], *S_I = BUF_I, *T_I = BUF_I; n struct Input { n   Input() {} n   inline Input &operator>>(char &c) { n     return (c = ((S_I == T_I) && n                          (T_I = (S_I = BUF_I) + fread(BUF_I, 1, 1048576, stdin), n                           S_I == T_I) n                      ? EOF n                      : *S_I++), n             *this); n   } n   template <class T> n   inline Input &operator>>(T &S_I) { n     S_I = 0; n     char c; n     while (*this >> c, c < 48 || c > 57) n       ; n     while (S_I = S_I * 10 + c - 48, *this >> c, c > 47 && c < 58) n       ; n     return *this; n   } n   inline int Getint() { n     char c; n     register int S_I = 0; n     register bool w = false; n     while (*this >> c, c == 45 && (w = true), c < 48 || c > 57) n       ; n     while (S_I = S_I * 10 + c - 48, *this >> c, c > 47 && c < 58) n       ; n     return w ? -S_I : S_I; n   } n } tin; n char BUF_O[262144], *S_O = BUF_O, *Limit_O = BUF_O + 262144; n struct Output { n   ~Output() { Flush(); } n   inline int Flush() { n     fwrite(BUF_O, 1, S_O - BUF_O, stdout); n     S_O = BUF_O; n     return 0; n   } n   inline Output &operator<<(const char &c) { n     if (S_O == Limit_O) Flush(); n     return (*S_O++ = c, *this); n   } n   template <class T> n   inline Output &operator<<(T x) { n     static char Stack[25]; n     static int Top; n     if (x < (Top = 0)) *this <<  - , x = -x; n     do { n       Stack[++Top] = x % 10 + 48; n     } while (x /= 10); n     while (Top) (*this) << Stack[Top--]; n     return *this; n   } n } tout; n int n, a[1005][1005]; n int main() { n   tin >> n; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) a[i][j] = (i + j - 2) % (n - 1) + 1; n   for (int i = 1; i <= n; i++) { n     a[i][n] = a[n][i] = a[i][i]; n     a[i][i] = 0; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) tout << a[i][j] <<    ; n     tout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long c1, c2, c3, c4, n, m, i, kol1, kol, ans, ans1; n int main() { n   cin >> c1 >> c2 >> c3 >> c4; n   cin >> n >> m; n   long long a[n + 1], b[m + 1]; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i <= m; i++) cin >> b[i]; n   for (i = 1; i <= n; i++) { n     long long ans = 0; n     if (a[i] * c1 <= c2) n       ans += a[i] * c1; n     else n       ans += c2; n     kol += ans; n   } n   kol = min(c3, kol); n   for (i = 1; i <= m; i++) { n     long long ans1 = 0; n     if (b[i] * c1 <= c2) n       ans1 += b[i] * c1; n     else n       ans1 += c2; n     kol1 += ans1; n   } n   kol1 = min(c3, kol1) + kol; n   cout << min(kol1, c4); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3,  Ofast ,  inline ) n using namespace std; n const long long maxn = (long long)4e5 + 5; n const long long mod = 1000000007; n const long long inf = 0x3f3f3f3f; n long long a[maxn], b[maxn], n, ans; n bool cal(long long L, long long R) { n   long long sum = 0; n   for (long long i = n, l = 1, r = 1; i >= 1; --i) { n     while (l <= n && b[i] + b[l] < L) ++l; n     while (r <= n && b[i] + b[r] <= R) ++r; n     sum += r - l - (l <= i && i < r); n   } n   return (sum >> 1) & 1; n } n bool solve(long long k) { n   for (long long i = 1; i <= n; ++i) b[i] = a[i] & ((1 << (k + 1)) - 1); n   sort(b + 1, b + n + 1); n   return cal(1 << k, (1 << (k + 1)) - 1) ^ cal(3 << k, (1 << (k + 2)) - 2); n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n; n   for (long long i = 1; i <= n; ++i) cin >> a[i]; n   for (long long i = 0; i <= 30; ++i) ans |= solve(i) << i; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int l, b; n   std::cin >> l >> b; n   if (l * b % 2 == 0) { n     std::cout << (l * b) / 2; n     return 0; n   } n   std::cout << ((l * b) - 1) / 2; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const int sz2 = 5e5 + 1; n int fact[sz2]; n int inv_fact[sz2]; n int mul(int a, int b) { return (long long)a * b % mod; } n int add(int a, int b) { n   int res = (long long)a + b; n   if (res < 0) n     res += mod; n   else if (res > mod) n     res -= mod; n   return res; n } n int bpow(int x, int p) { n   int res = 1; n   while (p) { n     if (p & 1) res = mul(res, x); n     p >>= 1; n     if (p) x = mul(x, x); n   } n   return res; n } n int inv(int x) { return bpow(x, mod - 2); } n void calc_fact() { n   fact[0] = inv_fact[0] = 1; n   for (int i = 1; i < sz2; i++) { n     fact[i] = mul(fact[i - 1], i); n     inv_fact[i] = inv(fact[i]); n   } n } n int cnk(int n, int k) { n   if (k > n) return 0; n   return mul(fact[n], mul(inv_fact[k], inv_fact[n - k])); n } n const int sz = 55; n int dp[sz * sz][sz][sz]; n int dp1[sz * sz][sz]; n int dp2[sz][sz][sz][sz][3]; n void solve() { n   int n, t; n   cin >> n >> t; n   vector<pair<int, int>> a(n); n   for (int i = 0; i < n; i++) { n     cin >> a[i].first >> a[i].second; n     a[i].second--; n   } n   dp[0][0][0] = 1; n   dp1[0][0] = 1; n   for (int i = 0; i < n; i++) { n     if (a[i].second <= 1) { n       for (int j = t; j >= 0; j--) { n         for (int k = 0; k < n; k++) { n           for (int l = 0; l < n; l++) { n             if (j + a[i].first <= t) n               dp[j + a[i].first][k + (a[i].second == 0)] n                 [l + (a[i].second == 1)] = n                     add(dp[j + a[i].first][k + (a[i].second == 0)] n                           [l + (a[i].second == 1)], n                         dp[j][k][l]); n           } n         } n       } n     } else { n       for (int j = t; j >= 0; j--) { n         for (int k = 0; k < n; k++) { n           if (j + a[i].first <= t) n             dp1[j + a[i].first][k + 1] = n                 add(dp1[j + a[i].first][k + 1], dp1[j][k]); n         } n       } n     } n   } n   dp2[1][1][0][0][0] = 1; n   dp2[1][0][1][0][1] = 1; n   dp2[1][0][0][1][2] = 1; n   for (int i = 1; i < n; i++) { n     for (int j = 0; j <= n; j++) { n       for (int k = 0; k <= n; k++) { n         for (int l = 0; l <= n; l++) { n           for (int h = 0; h < 3; h++) { n             for (int g = 0; g < 3; g++) { n               if (g != h) n                 dp2[i + 1][j + (g == 0)][k + (g == 1)][l + (g == 2)][g] = n                     add(dp2[i + 1][j + (g == 0)][k + (g == 1)][l + (g == 2)][g], n                         dp2[i][j][k][l][h]); n             } n           } n         } n       } n     } n   } n   int ans = 0; n   for (int i = 0; i <= t; i++) { n     for (int j = 0; j <= n; j++) { n       for (int k = 0; k <= n; k++) { n         for (int l = 0; l <= n; l++) { n           if (j + k + l > n) continue; n           int p = mul( n               mul(dp[i][j][k], dp1[t - i][l]), n               add(dp2[j + k + l][j][k][l][0], n                   add(dp2[j + k + l][j][k][l][1], dp2[j + k + l][j][k][l][2]))); n           int r = mul(fact[j], mul(fact[k], fact[l])); n           ans = add(ans, mul(p, r)); n         } n       } n     } n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   calc_fact(); n   int q = 1; n   for (; q > 0; q--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n1, n2, k1, k2; n   cin >> n1 >> n2 >> k1 >> k2; n   if (n1 > n2) n     cout <<  First  << endl; n   else if (n1 == n2) n     cout <<  Second  << endl; n   else n     cout <<  Second  << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m, k; n   while (scanf( %d%d , &n, &m) != EOF) { n     int ans = n / m; n     k = n % m; n     for (int i = 1; i <= m; i++) { n       if (i <= m - k) { n         printf( %d , ans); n       } else { n         printf( %d , ans + 1); n       } n       if (i != m) { n         printf(   ); n       } else { n         printf(  n ); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const int N = 710; n string s; n int n, t[N]; n stack<int> st; n struct node { n   long long f[3][3]; n   void init() { n     for (int i = 1; i <= 2; i++) f[i][0] = f[0][i] = 1; n   } n   node() { memset(f, 0, sizeof(f)); } n   void mergein(node t) { n     for (int i = 0; i <= 2; i++) n       for (int j = 0; j <= 2; j++) n         if ((i == 0 || j == 0) && i != j) n           for (int i1 = 0; i1 <= 2; i1++) n             for (int j1 = 0; j1 <= 2; j1++) n               if ((i1 != i || i1 + i == 0) && (j1 != j || j1 + j == 0)) n                 f[i][j] = (f[i][j] + t.f[i1][j1]) % MOD; n   } n   void mergenear(node x, node y) { n     for (int i = 0; i <= 2; i++) n       for (int j = 0; j <= 2; j++) n         for (int i1 = 0; i1 <= 2; i1++) n           for (int j1 = 0; j1 <= 2; j1++) n             if (i1 != j1 || i1 + j1 == 0) n               f[i][j] = (f[i][j] + (x.f[i][i1] * y.f[j1][j]) % MOD) % MOD; n   } n }; n node DAC(int l, int r) { n   node tmp; n   if (t[l] == r) { n     if (l == r - 1) n       tmp.init(); n     else n       tmp.mergein(DAC(l + 1, r - 1)); n   } else n     tmp.mergenear(DAC(l, t[l]), DAC(t[l] + 1, r)); n   return tmp; n } n void prepare() { n   cin >> s; n   n = s.length(); n } n void solve() { n   for (int i = 1; i <= n; i++) n     if (s[i - 1] ==  ( ) n       st.push(i); n     else n       t[i] = st.top(), st.pop(), t[t[i]] = i; n   node ans = DAC(1, n); n   long long way = 0; n   for (int i = 0; i <= 2; i++) n     for (int j = 0; j <= 2; j++) way = (way + ans.f[i][j]) % MOD; n   cout << way; n } n int main() { n   prepare(); n   solve(); n } n


#include <bits/stdc++.h> n #pragma optimization_level 3 n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math,O3 ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx ) n using namespace std; n const long long N = 1e6 + 5; n const long long mod = 1e9 + 7; n const long long INF = 0x7f7f7f7f7f7f7f7f; n const int INFi = 0x7f7f7f7f; n long long test = 1, n, m, a[N], sz = 0, tot, ch[N]; n string second =  0 ; n long long tree[N] = {}; n void update(int idx, int val) { n   idx++; n   while (idx <= N - 1) { n     tree[idx] += val; n     idx += idx & (-idx); n   } n } n long long query(int idx) { n   idx++; n   long long sum = 0; n   while (idx > 0) { n     sum += tree[idx]; n     idx -= idx & (-idx); n   } n   return sum; n } n int bs(int x) { n   int l = 1, r = N - 1, ans = 0; n   while (l <= r) { n     int mid = (l + r) / 2; n     if (query(mid) >= x) { n       ans = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   return ans; n } n void solve() { n   cin >> n >> m; n   for (int i = 1; i <= m; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     if (x == -1) { n       vector<long long> v; n       for (int i = 1; i <= m; i++) { n         if (a[i] > sz) break; n         v.push_back(bs(a[i])); n       } n       sz -= v.size(); n       for (auto it : v) ch[it] = 0, update(it, -1); n     } else { n       second += x +  0 ; n       sz++, tot++; n       update(tot, 1); n       ch[tot] = 1; n     } n   } n   if (sz == 0) n     cout <<  Poor stack!  n          <<   n ; n   else { n     for (int i = 1; i <= tot; i++) { n       if (ch[i] == 1) cout << second[i]; n     } n     cout <<   n ; n     ; n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed; n   cout << setprecision(15); n   ; n   for (int i = 1; i <= test; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<long long> > g; n vector<vector<pair<long long, long long> > > updates; n vector<long long> tree; n vector<long long> ans; n long long n; n void update(long long v, long long l, long long r, long long al, long long ar, n             long long val) { n   if (al <= l && ar >= r) { n     tree[v] += val; n     return; n   } n   if (al <= r && ar >= l) { n     update(v * 2, l, (r + l) / 2, al, ar, val); n     update(v * 2 + 1, (r + l) / 2 + 1, r, al, ar, val); n   } n } n long long answer(long long v, long long l, long long r, long long ind) { n   if (l == r) { n     return tree[v]; n   } n   if (ind <= (r + l) / 2) { n     return tree[v] + answer(v * 2, l, (r + l) / 2, ind); n   } else { n     return tree[v] + answer(v * 2 + 1, (r + l) / 2 + 1, r, ind); n   } n } n void dfs(long long v, long long pred = -1, long long h = 0) { n   for (long long i = 0; i < updates[v].size(); i++) { n     update(1, 0, n - 1, h, min(updates[v][i].second + h, n), n            updates[v][i].first); n   } n   ans[v] = answer(1, 0, n - 1, h); n   for (long long i = 0; i < g[v].size(); i++) { n     if (g[v][i] != pred) { n       dfs(g[v][i], v, h + 1); n     } n   } n   for (long long i = 0; i < updates[v].size(); i++) { n     update(1, 0, n - 1, h, min(updates[v][i].second + h, n), n            -updates[v][i].first); n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   tree.resize(n * 4); n   g.resize(n); n   ans.resize(n); n   updates.resize(n); n   for (long long i = 0; i < n - 1; i++) { n     long long x, y; n     cin >> x >> y; n     x--; n     y--; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   long long m; n   cin >> m; n   for (long long i = 0; i < m; i++) { n     long long v, x, d; n     cin >> v >> d >> x; n     v--; n     updates[v].push_back({x, d}); n   } n   dfs(0); n   for (long long i = 0; i < ans.size(); i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int flag[3000500]; n int dp[200050]; n int maxx[200050]; n int ans[200050]; n stack<int> st; n int main() { n   string s; n   cin >> s; n   int n = s.size(); n   for (int i = 0; i < n; i++) { n     ans[i] = 0; n     if (s[i] ==  1 ) st.push(i); n     if (s[i - 1] ==  1  && s[i] ==  0 ) { n       int j = i; n       while (!st.empty() && s[j] ==  0  && j < n) { n         int temp = st.top(); n         st.pop(); n         ans[temp] = 1; n         j++; n         i = j - 1; n       } n     } n   } n   for (int i = 0; i < n; i++) cout << ans[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int C[1 << 12], A[1 << 12], B[1 << 12], LA[1 << 12], LB[1 << 12]; n int main() { n   int i, s = 0; n   scanf( %d , &n); n   n = (1 << n); n   for (i = 0; i < n; i++) { n     scanf( %d , &C[i]); n     s ^= C[i]; n   } n   if (s) { n     puts( Fou ); n     return 0; n   } n   puts( Shi ); n   for (i = 0; i < n; i++) { n     A[i] = B[i] = i; n     LA[i] = LB[i] = i; n   } n   for (i = 0; i < n; i++) { n     int u = i; n     while (1) { n       if ((A[u] ^ B[u]) == C[u]) break; n       int v = LA[C[u] ^ B[u]]; n       swap(A[v], A[u]); n       swap(LA[A[v]], LA[A[u]]); n       if (v > i) break; n       swap(B[v], B[i + 1]); n       swap(LB[B[v]], LB[B[i + 1]]); n       u = v; n     } n   } n   for (i = 0; i < n; i++) printf( %d  , A[i]); n   puts(  ); n   for (i = 0; i < n; i++) printf( %d  , B[i]); n } n


#include <bits/stdc++.h> n using namespace std; n inline long long calc(long long x, long long k) { n   if (x < 0) return 0LL; n   return x / k + 1LL; n } n int main() { n   long long k, a, b; n   scanf( %lld%lld%lld , &k, &a, &b); n   if (a <= 0 && b <= 0) { n     a = abs(a), b = abs(b); n     printf( %lld n , calc(a, k) - calc(b - 1LL, k)); n   } else if (a >= 0 && b >= 0) { n     printf( %lld n , calc(b, k) - calc(a - 1LL, k)); n   } else { n     a = abs(a); n     printf( %lld n , calc(b, k) + calc(a, k) - 1LL); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, w, csets; n int weights[1001]; n int beauties[1001]; n int ranksz[1001]; n int parents[1001]; n bool superprnt[1001]; n vector<vector<int>> setlist; n vector<int> beautysum; n vector<int> wtsum; n map<int, int> setidmap; n vector<vector<int>> dpmap; n int find_parent(int i) { n   if (parents[i] == i) return i; n   return parents[i] = find_parent(parents[i]); n } n void union_nodes(int a, int b) { n   int prta{find_parent(a)}; n   int prtb{find_parent(b)}; n   if (prta == prtb) return; n   if (ranksz[prta] > ranksz[prtb]) { n     parents[prtb] = prta; n     ranksz[prta] += ranksz[prtb]; n     superprnt[prtb] = 0; n   } else { n     parents[prta] = prtb; n     ranksz[prtb] += ranksz[prta]; n     superprnt[prta] = 0; n   } n } n int solvedp(int i, int wts) { n   if (i == csets || wts == 0) return 0; n   if (dpmap[i][wts] != -1) return dpmap[i][wts]; n   int ans1{-500}, ans2{-500}, ans3{-500}; n   if (wtsum[i] <= wts) ans1 = beautysum[i] + solvedp(i + 1, wts - wtsum[i]); n   for (int j : setlist[i]) { n     if (weights[j] <= wts) { n       ans2 = max(ans2, beauties[j] + solvedp(i + 1, wts - weights[j])); n     } n   } n   ans3 = solvedp(i + 1, wts); n   return dpmap[i][wts] = max(max(ans1, ans2), ans3); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int m, a, b; n   cin >> n >> m >> w; n   for (int i = 1; i <= n; ++i) { n     cin >> weights[i]; n     parents[i] = i; n     ranksz[i] = 1; n     superprnt[i] = 1; n   } n   for (int i = 1; i <= n; ++i) { n     cin >> beauties[i]; n   } n   for (int i = 0; i < m; ++i) { n     cin >> a >> b; n     union_nodes(a, b); n   } n   int setid{0}; n   csets = 0; n   for (int i = 1; i <= n; ++i) { n     if (superprnt[i]) { n       csets++; n       setidmap[i] = setid++; n     } n   } n   setlist = vector<vector<int>>(csets); n   beautysum = vector<int>(csets, 0); n   wtsum = vector<int>(csets, 0); n   for (int i = 1; i <= n; ++i) { n     int prt{find_parent(i)}; n     int id{setidmap[prt]}; n     setlist[id].push_back(i); n     beautysum[id] += beauties[i]; n     wtsum[id] += weights[i]; n   } n   dpmap = vector<vector<int>>(csets, vector<int>(w + 1, -1)); n   int ans{solvedp(0, w)}; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n; n   cin >> n; n   long long int a[n]; n   for (auto i = 0; i <= n - 1; i++) cin >> a[i]; n   sort(a, a + n); n   for (auto i = n - 1; i >= 0; i--) { n     if (a[i] <= i + 1) { n       cout << i + 2 <<   n ; n       return; n     } n   } n   cout <<  1 n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n bool query(int x, int y) { n   cout <<  ?   << x <<     << y << endl; n   char resp; n   cin >> resp; n   if (resp ==  e ) exit(0); n   return resp ==  x ; n } n void answer(int v) { n   cout <<  !   << v << endl; n   return; n } n void solve() { n   int k = -1; n   ; n   for (int i = 0; i < 30; i++) { n     int x = (1 << (i + 1)), y = (1 << i); n     if (!query(x, y)) { n       k = i; n       break; n     } n   } n   if (k == -1) { n     answer(1); n     return; n   } n   int l = (1 << k); n   int u = (1 << (k + 1)); n   while (u - l > 2) { n     int mid = (u + l) / 2; n     if (!query(mid, mid / 2)) n       u = mid; n     else n       l = mid; n   } n   if (!query(u, 1)) n     answer(u); n   else n     answer(u - 1); n   return; n } n int main() { n   string msg; n   while (cin >> msg, msg !=  end ) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, t, i, c = 0, s, x, y, u; n   cin >> n >> t; n   if (t < 10) { n     for (i = 0; i < n; i++) cout << t; n   } else { n     s = t / 10; n     c++; n     while (1) { n       s = s / 10; n       c++; n       if (s == 0) break; n     } n     if (c == n) n       cout << t; n     else if (c > n) n       cout <<  -1 ; n     else if (c < n) { n       u = n - c; n       cout << t; n       for (i = 1; i <= u; i++) cout << 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int flag[13], change[13]; n bool is_leap(int y) { n   if (y % 100 == 0) n     return y % 400 == 0; n   else n     return y % 4 == 0; n } n int main() { n   memset(flag, 0, sizeof(flag)); n   memset(change, 0, sizeof(change)); n   int y; n   cin >> y; n   while (1) { n     bool last = is_leap(y); n     y++; n     bool now = is_leap(y); n     if (last && (!now)) n       for (int i = 1; i <= 12; i++) change[i] = (change[i] + 1 + (i <= 2)) % 7; n     else if ((!last) && now) n       for (int i = 1; i <= 12; i++) change[i] = (change[i] + 1 + (i >= 3)) % 7; n     else n       for (int i = 1; i <= 12; i++) change[i] = (change[i] + 1) % 7; n     bool ok = true; n     for (int i = 1; i <= 12; i++) n       if (flag[i] != change[i]) { n         ok = false; n         break; n       } n     if (ok) break; n   } n   cout << y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2147483647; n const long long LLINF = 9223372036854775807LL; n int m; n const int mod = 1000000007; n struct matr { n   int a[60][60]; n   matr() { memset(a, 0, sizeof(a)); } n }; n matr mul(matr m1, matr m2) { n   matr res; n   for (int i = 0; i < m; ++i) n     for (int j = 0; j < m; ++j) n       for (int k = 0; k < m; ++k) n         res.a[i][j] = (res.a[i][j] + (long long)m1.a[i][k] * m2.a[k][j]) % mod; n   return res; n } n matr pow(matr x, long long p) { n   if (p == 0) { n     matr res; n     for (int i = 0; i < m; ++i) res.a[i][i] = 1; n     return res; n   } else if (p & 1) n     return mul(x, pow(x, p - 1)); n   else { n     matr t = pow(x, p >> 1); n     return mul(t, t); n   } n } n int g(char c) { n   if (c >=  a  && c <=  z ) n     return c -  a ; n   else n     return c -  A  + 26; n } n int main() { n   long long n; n   int k; n   cin >> n >> m >> k; n   n--; n   matr p; n   for (int i = 0; i < m; ++i) n     for (int j = 0; j < m; ++j) p.a[i][j] = 1; n   for (int i = 0; i < k; ++i) { n     string s; n     cin >> s; n     int v1 = g(s[0]); n     int v2 = g(s[1]); n     p.a[v2][v1] = 0; n   } n   p = pow(p, n); n   int ans = 0; n   for (int i = 0; i < m; ++i) n     for (int j = 0; j < m; ++j) ans = (ans + p.a[i][j]) % mod; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long A, B, x, y, z; n   cin >> A >> B >> x >> y >> z; n   for (int i = 0; i < x; i++) A = A - 2; n   for (int i = 0; i < y; i++) { n     A--; n     B--; n   } n   for (int i = 0; i < z; i++) B = B - 3; n   long long a = 0, b = 0; n   if (A < 0) a = -A; n   if (B < 0) b = -B; n   cout << (a + b); n   return 0; n } n