#include <bits/stdc++.h> n using namespace std; n pair<int, int> _e[600005], ex[600005]; n vector<int> ee[600005]; n int nt, rr = 0, dn = 1, colnum, dfnn, _enum, nn, mm, n, m, ca, idnum, num, top, n         hd[600005], dfn[600005], low[600005], instk[600005], a[600005], n         b[600005], col[600005], dffn[600005], dep[600005], fa[600005][20], n         lg[600005], vis[600005], stk[600005], id[600005]; n struct enode { n   int to, nxt; n } e[600005]; n int rt(int element) { n   element = (element + rr) % nt; n   if (!element) element = nt; n   return element; n } n void add(int u, int v) { n   e[++dn] = (enode){v, hd[u]}; n   hd[u] = dn; n } n void tarjan(int x, int lst) { n   int i, v; n   instk[x] = 1, dfn[x] = low[x] = ++dfnn; n   for (i = hd[x]; i; i = e[i].nxt) { n     v = e[i].to; n     if (i == lst || i == (lst ^ 1)) continue; n     if (!dfn[v]) { n       tarjan(v, i); n       if (low[x] > low[v]) low[x] = low[v]; n     } else if (instk[v]) { n       if (low[x] > dfn[v]) low[x] = dfn[v]; n     } n   } n   for (i = hd[x]; i; i = e[i].nxt) { n     v = e[i].to; n     if (i == lst || i == (lst ^ 1)) continue; n     if (low[v] <= dfn[x]) { n       ee[x].push_back(v); n       ee[v].push_back(x); n     } else n       _e[++_enum] = make_pair(x, v); n   } n   instk[x] = 0; n } n void dfs(int x) { n   col[x] = colnum; n   for (auto v : ee[x]) n     if (!col[v]) dfs(v); n } n void dfs1(int x, int father) { n   int i; n   dffn[x] = ++dfnn; n   dep[x] = dep[father] + 1; n   fa[x][0] = father; n   for (i = 1; i <= lg[dep[x]]; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1]; n   for (i = hd[x]; i; i = e[i].nxt) n     if (!dffn[e[i].to]) dfs1(e[i].to, x); n } n int getlca(int x, int y) { n   int i; n   if (dep[x] < dep[y]) swap(x, y); n   for (i = lg[dep[x]]; i >= 0; i--) n     if (dep[fa[x][i]] >= dep[y]) x = fa[x][i]; n   if (x == y) return x; n   for (i = lg[dep[x]]; i >= 0; i--) n     if (fa[x][i] != fa[y][i]) { n       x = fa[x][i]; n       y = fa[y][i]; n     } n   return fa[x][0]; n } n void add2(int u, int v) { n   add(u, v); n   add(v, u); n } n void dfss(int x) { n   vis[x] = 1; n   for (auto v : ee[x]) n     if (!vis[v]) dfss(v); n } n void solve(int t) { n   int i, flag = 1; n   for (i = 1; i <= mm; i++) add2(ex[i].first, ex[i].second); n   _enum = dfnn = 0; n   tarjan(a[1], 0); n   dfss(a[1]); n   for (i = 1; i <= nn; i++) n     if (!vis[a[i]]) flag = 0; n   puts(flag ?  YES  :  NO ); n   if (flag) rr = (rr + t) % nt; n   dn = 1; n   for (i = 1; i <= idnum; i++) { n     hd[id[i]] = vis[id[i]] = low[id[i]] = dfn[id[i]] = 0; n     ee[id[i]].clear(); n   } n } n void build() { n   int i, lca; n   top = 0, idnum = 0; n   for (i = 1; i <= num; i++) { n     if (!top) { n       stk[top = 1] = b[i]; n       id[++idnum] = b[i]; n       continue; n     } n     lca = getlca(stk[top], b[i]); n     if (!lca) { n       while (top > 1) { n         add2(stk[top - 1], stk[top]); n         top--; n       } n       stk[top = 1] = b[i]; n       id[++idnum] = b[i]; n       continue; n     } n     while (top > 1 && dep[stk[top - 1]] > dep[lca]) { n       add2(stk[top - 1], stk[top]); n       top--; n     } n     if (dep[stk[top]] > dep[lca]) add2(lca, stk[top--]); n     if (stk[top] != lca) { n       stk[++top] = lca; n       id[++idnum] = lca; n     } n     stk[++top] = b[i]; n     id[++idnum] = b[i]; n   } n   while (top > 1) { n     add2(stk[top - 1], stk[top]); n     top--; n   } n } n int main() { n   int i, u, v, t, xx, yy; n   cin >> n >> m >> ca; n   nt = n; n   for (i = 1; i <= m; i++) { n     cin >> u >> v; n     if (u != v) add2(u, v); n   } n   for (i = 1; i <= n; i++) n     if (!dfn[i]) tarjan(i, 0); n   for (i = 1; i <= n; i++) n     if (!col[i]) { n       colnum++; n       dfs(i); n     } n   dn = 1; n   for (i = 1; i <= n; i++) hd[i] = 0; n   for (i = 1; i <= _enum; i++) add2(col[_e[i].first], col[_e[i].second]); n   n = colnum; n   dep[0] = -1; n   lg[1] = 0; n   dfnn = 0; n   for (i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1; n   for (i = 1; i <= n; i++) n     if (!dffn[i]) dfs1(i, 0); n   dn = 1; n   for (i = 1; i <= n; i++) { n     hd[i] = dfn[i] = low[i] = 0; n     ee[i].clear(); n   } n   for (t = 1; t <= ca; t++) { n     cin >> nn >> mm; n     num = nn; n     for (i = 1; i <= nn; i++) { n       cin >> xx; n       a[i] = rt(xx); n     } n     for (i = 1; i <= mm; i++) { n       cin >> xx >> yy; n       ex[i].first = rt(xx); n       ex[i].second = rt(yy); n     } n     for (i = 1; i <= nn; i++) a[i] = b[i] = col[a[i]]; n     for (i = 1; i <= mm; i++) { n       ex[i] = make_pair(col[ex[i].first], col[ex[i].second]); n       b[++num] = ex[i].first; n       b[++num] = ex[i].second; n     } n     sort(a + 1, a + nn + 1, [&](int x, int y) { return dffn[x] < dffn[y]; }); n     nn = unique(a + 1, a + nn + 1) - a - 1; n     sort(b + 1, b + num + 1, [&](int x, int y) { return dffn[x] < dffn[y]; }); n     num = unique(b + 1, b + num + 1) - b - 1; n     build(); n     solve(t); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 998244353; n const long long int mxn = 1e5 + 7; n long long int a[mxn]; n long long int power(long long int a, long long int b); n long long int flcm(long long int n, long long int m); n long long int fgcd(long long int n, long long int m); n long long int modinv(long long int x); n long long int max_val(long long int start, long long int end) { n   long long int pres_max, global_max; n   for (long long int i = start; i <= end; i++) { n     if (i == start) { n       pres_max = a[i]; n       global_max = a[i]; n     } else { n       pres_max = max(pres_max + a[i], a[i]); n       global_max = max(global_max, pres_max); n     } n   } n   return global_max; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, m, k, u, v, t = 1, temp, ans = 0, flag = 1, c = 0, min_el, n                                max_el, sum_el; n   string s, s1, s2; n   cin >> t; n   while (t--) { n     ans = 0; n     c = 0; n     flag = 1; n     sum_el = 0; n     cin >> n; n     memset(a, 0, sizeof(a)); n     for (long long int i = 0; i < n; i++) cin >> a[i]; n     max_el = max(max_val(0, n - 2), max_val(1, n - 1)); n     if (max_el < max_val(0, n - 1)) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n long long int power(long long int a, long long int b) { n   long long int res = 1; n   while (b) { n     if (b % 2) { n       res = (res * a) % mod; n     } n     a = (a * a) % mod; n     b /= 2; n   } n   return res; n } n long long int flcm(long long int n, long long int m) { n   long long int temp = fgcd(n, m); n   return (n * m) / temp; n } n long long int fgcd(long long int n, long long int m) { n   long long int temp; n   while (m != 0) { n     temp = n; n     n = m; n     m = temp % m; n   } n   return n; n } n long long int modinv(long long int x) { return power(x, mod - 2); } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int N = 3123; n int n, m; n vector<tuple<int, int, int>> g[N]; n vector<int> lpos, rpos; n bool vis[N]; n bool dfs(int u, int l, int r) { n   vis[u] = 1; n   if (u == n) return 1; n   bool ret = 0; n   for (auto t : g[u]) { n     int v, tl, tr; n     tie(v, tl, tr) = t; n     if (tl > l or tr < r or vis[v]) continue; n     ret |= dfs(v, l, r); n   } n   return ret; n } n bool has_path(int l, int r) { n   memset(vis, 0, sizeof vis); n   return dfs(1, l, r); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int a, b, l, r; n     cin >> a >> b >> l >> r; n     g[a].emplace_back(b, l, r); n     g[b].emplace_back(a, l, r); n     lpos.emplace_back(l); n     rpos.emplace_back(r); n   } n   sort(begin(lpos), end(lpos)); n   sort(begin(rpos), end(rpos)); n   lpos.resize(unique(begin(lpos), end(lpos)) - lpos.begin()); n   rpos.resize(unique(begin(rpos), end(rpos)) - rpos.begin()); n   int ans = 0; n   for (int l : lpos) { n     int lo = lower_bound(begin(rpos), end(rpos), l) - rpos.begin(), n         hi = rpos.size(); n     while (lo < hi) { n       int mi = (lo + hi) / 2; n       if (has_path(l, rpos[mi])) n         lo = mi + 1; n       else n         hi = mi; n     } n     if (lo - 1 >= 0) ans = max(ans, rpos[lo - 1] - l + 1); n   } n   if (ans == 0) { n     cout <<  Nice work, Dima!  << endl; n     return 0; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, sum = 0; n   string s; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> s; n     if (s ==  Tetrahedron ) { n       sum += 4; n     } else if (s ==  Cube ) { n       sum += 6; n     } else if (s ==  Octahedron ) { n       sum += 8; n     } else if (s ==  Dodecahedron ) { n       sum += 12; n     } else if (s ==  Icosahedron ) { n       sum += 20; n     } n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> start_years; n vector<long long> start_numbers; n vector<long long> mods; n void init() { n   long long start = 1989; n   start_years.resize(10); n   start_numbers.resize(10); n   mods.resize(10); n   start_years[0] = start; n   start_numbers[0] = 9; n   mods[0] = 10; n   long long p = 10; n   for (int i = 1; i < 10; ++i) { n     start_years[i] = start_years[i - 1] + p; n     p *= 10; n     start_numbers[i] = start_years[i] % p; n     mods[i] = p; n   } n } n long long solve(int i, long long num) { n   long long pos = num - start_numbers[i] + mods[i]; n   pos %= mods[i]; n   return start_years[i] + pos; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   init(); n   int n = 0; n   cin >> n; n   for (int i = 0; i < (n); ++i) { n     string str; n     cin >> str; n     int len = str.size() - 4; n     stringstream ss(str.substr(4, len)); n     long long num; n     ss >> num; n     cout << solve(len - 1, num) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using int64 = long long; n constexpr int64 P = 1000000007; n struct FiniteField { n  private: n   int64 x; n  n  public: n   FiniteField(int64 x) : x(x) { n     if (x > P || x < 0) { n       cerr <<  Invalied FiniteField!  << endl; n       exit(1); n     } n   } n   FiniteField() : x(0) {} n   int64 Value() const { return x; } n   inline FiniteField operator+(FiniteField o) const { n     FiniteField r(*this); n     r += o; n     return r; n   } n   inline FiniteField operator-(FiniteField o) const { n     FiniteField r(*this); n     r -= o; n     return r; n   } n   inline FiniteField operator*(FiniteField o) const { n     FiniteField r(*this); n     r *= o; n     return r; n   } n   inline FiniteField operator/(FiniteField o) const { n     FiniteField r(*this); n     r /= o; n     return r; n   } n   inline void operator+=(FiniteField o) { x = (x + o.x) % P; } n   inline void operator-=(FiniteField o) { x = (x + P - o.x) % P; } n   inline void operator*=(FiniteField o) { x = (x * o.x) % P; } n   void operator/=(FiniteField o) { n     int64 p = P - 2; n     while (p) { n       if (p % 2) { n         *this *= o; n       } n       o *= o; n       p /= 2; n     } n   } n }; n template <typename T> n vector<vector<T>> operator*(const vector<vector<T>>& a1, n                             const vector<vector<T>>& a2) { n   int n = a1.size(); n   int m = a1[0].size(); n   if (a2.size() != m) { n     cerr <<  Matrix dimensions don t match for multiplication!  << endl; n     exit(1); n   } n   int l = a2[0].size(); n   vector<vector<T>> result(n, vector<T>(l)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < l; j++) { n       for (int k = 0; k < m; k++) { n         result[i][j] += a1[i][k] * a2[k][j]; n       } n     } n   } n   return result; n } n template <typename T> n vector<T> operator*(const vector<vector<T>>& matrix, const vector<T>& v) { n   int n = matrix.size(); n   int m = matrix[0].size(); n   if (v.size() != m) { n     cerr <<  Matrix and vector dimensions don t match for multiplication!  n          << endl; n     exit(1); n   } n   vector<T> result(n); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       result[i] += matrix[i][j] * v[j]; n     } n   } n   return result; n } n template <typename T> n vector<vector<T>> Make2DVector(int d1, int d2, T default_value) { n   return vector<vector<T>>(d1, vector<T>(d2, default_value)); n } n template <typename T> n vector<vector<vector<T>>> Make3DVector(int d1, int d2, int d3, n                                        T default_value) { n   return vector<vector<vector<T>>>( n       d1, vector<vector<T>>(d2, vector<T>(d3, default_value))); n } n constexpr int DEBUG = 0; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int64 n; n   int d; n   cin >> n >> d; n   auto shifts = Make3DVector<FiniteField>(d, d + 1, d + 1, 0); n   for (int k = 0; k < d; k++) { n     for (int i = 0; i < d; i++) { n       shifts[k][i][(i + k) % d] = 1; n     } n     shifts[k][d][d] = 1; n   } n   auto units = Make3DVector<FiniteField>(d, d + 1, d + 1, 0); n   for (int k = 0; k < d; k++) { n     for (int i = 0; i < d + 1; i++) { n       units[k][i][i] = 1; n     } n     for (int i = 0; i < d + 1; i++) { n       units[k][k][i] = 1; n     } n   } n   auto identity = Make2DVector<FiniteField>(d + 1, d + 1, 0); n   for (int i = 0; i < d + 1; i++) { n     identity[i][i] = 1; n   } n   map<int64, vector<vector<FiniteField>>> cache; n   function<vector<vector<FiniteField>>(int64)> find_matrix = n       [&](int64 p) -> vector<vector<FiniteField>> { n     if (cache.find(p) != cache.end()) { n       return cache.find(p)->second; n     } n     if (p <= d) { n       auto matrix = identity; n       for (int k = 0; k < p; k++) { n         matrix = units[k] * matrix; n       } n       return matrix; n     } n     int64 child_p = d; n     while ((p + child_p - 1) / child_p > d) child_p *= d; n     auto child = find_matrix(child_p); n     auto matrix = child; n     int64 remaining_p = p; n     remaining_p -= child_p; n     for (int k = 1; k < d; k++) { n       if (remaining_p >= child_p) { n         matrix = shifts[d - k] * child * shifts[k] * matrix; n         remaining_p -= child_p; n       } else if (remaining_p > 0) { n         matrix = shifts[d - k] * find_matrix(remaining_p) * shifts[k] * matrix; n         remaining_p = 0; n       } else { n         break; n       } n     } n     cache[p] = matrix; n     return matrix; n   }; n   if (DEBUG) cerr <<  PHASE 1  << endl; n   auto matrix = find_matrix(n); n   if (DEBUG) cerr <<  PHASE 2  << endl; n   vector<FiniteField> base_vector(d + 1); n   base_vector[d] = 1; n   auto final_vector = matrix * base_vector; n   FiniteField ans = 0; n   for (const auto& x : final_vector) ans += x; n   cout << ans.Value() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string com; n   map<string, bool> mp; n   int ans = 0; n   while (getline(cin, com)) { n     if (com[0] ==  + ) { n       com.erase(com.begin(), com.begin() + 1); n       mp[com] = 1; n     } else if (com[0] ==  - ) { n       com.erase(com.begin(), com.begin() + 1); n       mp[com] = 0; n     } else { n       int len = com.size() - com.find( : ) - 1, cnt = 0; n       for (map<string, bool>::iterator it = mp.begin(); it != mp.end(); it++) { n         if (it->second) cnt++; n       } n       ans += cnt * len; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n namespace { n using namespace std; n template <typename T> n istream &operator>>(istream &is, vector<T> &v) { n   for (T &t : v) is >> t; n   return is; n } n template <typename T> n bool space(vector<T>) { n   return false; n } n template <typename T> n bool space(T) { n   return true; n } n bool space(char) { return false; } n template <typename T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (const T &t : v) { n     os << t << (space(t) ?     :   ); n   } n   os <<   n ; n   return os; n } n long long gcd(long long a, long long b) { n   while (b) { n     long long mod = a % b; n     a = b; n     b = mod; n   } n   return a; n } n template <typename T> n void incr_sort(vector<T> &v) { n   stable_sort(v.begin(), v.end()); n } n template <typename T> n void decr_sort(vector<T> &v) { n   stable_sort(v.rbegin(), v.rend()); n } n long long sum(const vector<int> &v) { n   long long s = 0; n   for (long long value : v) s += value; n   return s; n } n vector<long long> prefix_sum(const vector<int> &v) { n   vector<long long> res(v.size()); n   res[0] = v[0]; n   for (int i = 1; i < v.size(); ++i) res[i] = res[i - 1] + v[i]; n   return res; n } n }  // namespace n void solve(); n int main() { n   cin.tie(nullptr); n   cout.tie(nullptr); n   ios::sync_with_stdio(false); n   int t = 1; n   cin >> t; n   for (int i = 0; i < t; ++i) solve(); n   return 0; n } n void solve() { n   int h, n; n   cin >> h >> n; n   vector<int> p(n); n   cin >> p; n   p.push_back(0); n   long long ans = 0; n   int i = 0; n   while (p[i] > 2) { n     if (p[i + 1] < p[i] - 1) p[i] = p[i + 1] + 1; n     if (p[i] <= 2) break; n     if (p[i + 2] == p[i + 1] - 1) n       i += 2; n     else { n       ++ans; n       p[i + 1] = p[i + 2] + 1; n       i++; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, v, k, cnt; n signed main() { n   scanf( %lld%lld%lld , &n, &m, &v), k = (v == 1) ? 2 : 1; n   if (m < n - 1 || m > n - 1 + (n - 2) * (n - 3) / 2) puts( -1 ), exit(0); n   for (long long i = 1; i <= n; i++) n     if (i != v) printf( %lld %lld n , i, v); n   cnt = n - 1; n   for (long long i = 1; i <= n; i++) { n     if (i == v || i == k) continue; n     for (long long j = i + 1; j <= n; j++) { n       if (j == v || j == k) continue; n       if (cnt + 1 <= m) cnt++, printf( %lld %lld n , i, j); n       if (cnt == m) break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long fac(long long n) { n   long long m = 1; n   for (long long i = 1; i <= n; i++) { n     m = m * i; n   } n   return m; n } n long long gcd(long long x, long long y) { n   if (x > y) swap(x, y); n   long long md; n   while (1) { n     md = y % x; n     if (md == 0) break; n     y = x; n     x = md; n   } n   return x; n } n long long nCr(long long n, long long r) { n   long long rs = (fac(n)) / (fac(r) * fac(n - r)); n   return rs; n } n long long nPr(long long n, long long r) { n   long long rs = fac(n) / fac(n - r); n   return rs; n } n bool flag, fl; n long long i, j; n long long pw(long long n, long long p) { n   long long mn = 1; n   if (p == 0) return 1; n   for (long long z = 0; z < p; z++) { n     mn *= n; n   } n   return mn; n } n int main() { n   long long n; n   scanf( %lld , &n); n   long long num = 0; n   vector<int> bn; n   long long rsl; n   for (i = 1; i <= 1024; i++) { n     bn.clear(); n     j = i; n     num = 0; n     while (j > 0) { n       bn.push_back(j % 2); n       j = j / 2; n     } n     long long ln = bn.size(); n     for (j = 0; j < ln; j++) { n       num += (bn[j] * pw(10, j)); n     } n     if (num <= n) { n       rsl = i; n     } else n       break; n   } n   printf( %lld n , rsl); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = int(1e6) + 10; n int n; n char buffer[MAX_N]; n int main() { n   gets(buffer); n   n = strlen(buffer); n   reverse(buffer, buffer + n); n   vector<pair<char, int> > v; n   for (int i = 0, j, l = -1; i < n; i = j) { n     for (j = i; j < n && buffer[j] ==  1 ; j++) n       ; n     if (j > i + 1) { n       if (l == i) { n         v.pop_back(); n       } n       v.push_back(make_pair( - , i)); n       v.push_back(make_pair( + , j)); n       buffer[l = j--] =  1 ; n     } else if (j == i + 1 && l != i) { n       v.push_back(make_pair( + , i)); n     } n     j++; n   } n   cout << (int)v.size() << endl; n   for (__typeof((v).begin()) e = (v).begin(); e != (v).end(); ++e) { n     printf( %c2^%d n , e->first, e->second); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int N; n   cin >> N; n   string s1, s2; n   cin >> s1 >> s2; n   int num_different = 0; n   vector<pair<bool, int>> differences; n   int num_s1_has_a = 0; n   int num_s1_has_b = 0; n   for (int i = 0; i < N; ++i) { n     if (s1[i] != s2[i]) { n       bool s1_has_a = (s1[i] ==  a ); n       if (s1_has_a) { n         ++num_s1_has_a; n       } else { n         ++num_s1_has_b; n       } n       differences.push_back({s1_has_a, i}); n     } n   } n   if ((num_s1_has_a + num_s1_has_b) % 2 != 0) { n     cout << -1 << endl; n     return; n   } else { n     sort(differences.begin(), differences.end()); n     int answer = (num_s1_has_a + num_s1_has_b) / 2; n     if (num_s1_has_a % 2 != 0) { n       ++answer; n     } n     cout << answer << endl; n     for (int i = 0; i < differences.size(); i += 2) { n       if (differences[i].first != differences[i + 1].first) { n         cout << differences[i].second + 1 <<     << differences[i].second + 1 n              << endl; n       } n       cout << differences[i].second + 1 <<     << differences[i + 1].second + 1 n            << endl; n     } n   } n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e9 + 7; n int main() { n   ios::sync_with_stdio(0); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   vector<pair<int, int> > ans; n   if (n == 1) { n     if (m == 1) { n       printf( 1 1 ); n       return 0; n     } n     for (int j = 0; j < m / 2; j++) { n       printf( 1 %d n , j + 1); n       printf( 1 %d n , m - (j + 1) + 1); n     } n     if (m % 2 != 0) { n       printf( 1 %d , m / 2 + 1); n     } n     return 0; n   } n   for (int i = 0; i < n / 2; i++) { n     for (int j = 0; j < m; j++) { n       printf( %d %d n , i + 1, j + 1); n       printf( %d %d n , n - i, m - j); n     } n   } n   if (n % 2 != 0) { n     if (m == 1) { n       printf( %d %d n , n / 2 + 1, 1); n       return 0; n     } n     for (int j = 0; j < m / 2; j++) { n       printf( %d %d n , n / 2 + 1, j + 1); n       printf( %d %d n , n / 2 + 1, m - j); n     } n     if (m % 2 != 0) { n       printf( %d %d n , n / 2 + 1, m / 2 + 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct __s { n   __s() { n     srand(time(NULL)); n     if (1) { n       ios_base::Init i; n       cin.sync_with_stdio(0); n       cin.tie(0); n     } n   } n   ~__s() { n     if (!1) n       fprintf(stderr,  Execution time: %.3lf s. n , n               (double)clock() / CLOCKS_PER_SEC); n     int n; n     cin >> n; n   } n } __S; n struct vertex { n   int go[26]; n   int link; n   int ans; n   int p; n   int pch; n   bool root; n   bool isAns; n   vertex(int p, int pch, bool root) : p(p), pch(pch), root(root) { n     memset(go, 255, sizeof(go)); n     link = -1; n     ans = 0; n     isAns = false; n   }; n }; n vector<vertex> t; n void add(string &s, int v) { n   for (int i = 0; i < (int)(s.size()); i++) { n     int c = s[i] -  a ; n     if (t[v].go[c] == -1) { n       t[v].go[c] = t.size(); n       t.push_back(vertex(v, c, false)); n     } n     v = t[v].go[c]; n   } n   t[v].ans++; n } n int getGo(int v, int c); n int getLink(int v) { n   if (t[v].link == -1) { n     if (t[t[v].p].root) n       return t[v].link = t[v].p; n     else n       return t[v].link = getGo(getLink(t[v].p), t[v].pch); n   } n   return t[v].link; n } n int getGo(int v, int c) { n   if (t[v].go[c] == -1) { n     if (t[v].root) n       return t[v].go[c] = v; n     else n       return t[v].go[c] = getGo(getLink(v), c); n   } n   return t[v].go[c]; n } n int getAns(int v) { n   if (t[v].isAns) return t[v].ans; n   t[v].isAns = true; n   return t[v].ans += getAns(getLink(v)); n } n int getAns(string &s, int v) { n   int res = 0; n   for (int i = 0; i < (int)(s.size()); i++) { n     int c = s[i] -  a ; n     v = getGo(v, c); n     res += getAns(v); n   } n   return res; n } n int n[2]; n int root[2][20]; n queue<string> p[2][20]; n queue<string> q[2]; n void build() { n   for (int i = 0; i < (int)(2); i++) { n     int N = n[i] + q[i].size(); n     for (int j = 0; j < (int)(20); j++) { n       bool a = ((n[i] >> j) & 1); n       bool b = ((N >> j) & 1); n       if (a == b) continue; n       if (a) { n         root[i][j] = 0; n         while (p[i][j].size()) { n           q[i].push(p[i][j].front()); n           p[i][j].pop(); n         } n         continue; n       } n       root[i][j] = t.size(); n       t.push_back(vertex(root[i][j], 0, true)); n       for (int k = 0; k < (int)(1LL << j); k++) { n         add(q[i].front(), root[i][j]); n         p[i][j].push(q[i].front()); n         q[i].pop(); n       } n     } n     n[i] = N; n   } n } n int get(string &s, int i) { n   int res = 0; n   for (int j = 0; j < (int)(20); j++) { n     if (root[i][j] == 0) continue; n     res += getAns(s, root[i][j]); n   } n   return res; n } n int get(string &s) { return get(s, 0) - get(s, 1); } n int main(void) { n   t.push_back(vertex(0, 0, false)); n   int n; n   cin >> n; n   for (int _ = 0; _ < (int)(n); _++) { n     int t; n     string s; n     cin >> t >> s; n     if (t == 1 || t == 2) { n       q[t - 1].push(s); n     } else { n       build(); n       cout << get(s) <<   n ; n       cout.flush(); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int _ = 2e5 + 7; n const int __ = 8e5 + 7; n const long long inf = 1e17; n int n, m, q; n long long wgt[2][_], cst[_], minx[__], tag[__]; n struct edge { n   int x, y; n   long long w; n } e[_]; n void build(int k, int l, int r, bool id) { n   tag[k] = 0; n   if (l == r) { n     if (!id) { n       minx[k] = wgt[0][l] + cst[l]; n     } else n       minx[k] = wgt[1][l]; n     return; n   } n   int mid = (l + r) >> 1; n   build(k << 1, l, mid, id); n   build(k << 1 | 1, mid + 1, r, id); n   minx[k] = min(minx[k << 1], minx[k << 1 | 1]); n } n bool rule_x(edge a, edge b) { return a.x < b.x; } n void init() { n   cin >> n >> m >> q; n   n--; n   for (int i = 1; i <= n; i++) scanf( %lld%lld , &wgt[0][i], &wgt[1][i]); n   for (int i = 1; i <= m; i++) scanf( %d%d%lld , &e[i].x, &e[i].y, &e[i].w); n   sort(e + 1, e + 1 + m, rule_x); n   build(1, 0, n, 1); n } n void upd(int k, long long w) { n   minx[k] += w; n   tag[k] += w; n } n void psd(int k) { n   if (!tag[k]) return; n   upd(k << 1, tag[k]); n   upd(k << 1 | 1, tag[k]); n   tag[k] = 0; n } n void modify(int k, int l, int r, int x, int y, long long w) { n   if (l >= x && r <= y) { n     upd(k, w); n     return; n   } n   psd(k); n   int mid = (l + r) >> 1; n   if (x <= mid) modify(k << 1, l, mid, x, y, w); n   if (y > mid) modify(k << 1 | 1, mid + 1, r, x, y, w); n   minx[k] = min(minx[k << 1], minx[k << 1 | 1]); n } n long long query(int k, int l, int r, int x, int y) { n   if (l >= x && r <= y) return minx[k]; n   psd(k); n   int mid = (l + r) >> 1; n   long long t1 = inf, t2 = inf; n   if (x <= mid) t1 = query(k << 1, l, mid, x, y); n   if (y > mid) t2 = query(k << 1 | 1, mid + 1, r, x, y); n   return min(t1, t2); n } n void pre() { n   int p = 1; n   for (int i = 1; i <= n + 1; i++) { n     while (p <= m && e[p].x <= i) { n       modify(1, 0, n, 0, e[p].y - 1, e[p].w); n       p++; n     } n     if (i == n + 1) n       cst[0] = query(1, 0, n, 0, n); n     else n       cst[i] = query(1, 0, n, 0, n); n   } n   build(1, 0, n, 0); n } n void run() { n   printf( %lld n , query(1, 0, n, 0, n)); n   int x; n   long long w; n   for (int i = 1; i <= q; i++) { n     scanf( %d%lld , &x, &w); n     modify(1, 0, n, x, x, -wgt[0][x] + w); n     wgt[0][x] = w; n     printf( %lld n , query(1, 0, n, 0, n)); n   } n } n int main() { n   init(); n   pre(); n   run(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void file_handling() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n } n void solve() { n   string dish; n   cin >> dish; n   long long b, c, s; n   b = s = c = 0; n   for (long long i = 0; i < dish.size(); ++i) n     if (dish[i] ==  B ) n       b++; n     else if (dish[i] ==  S ) n       s++; n     else n       c++; n   long long nb, nc, ns; n   cin >> nb >> ns >> nc; n   long long pb, pc, ps; n   cin >> pb >> ps >> pc; n   long long rubles; n   cin >> rubles; n   long long left = 0; n   long long right = 1e15; n   long long ans = 0; n   while (left <= right) { n     long long mid = (right + left) / 2; n     long long cost = 0; n     if (mid * b >= nb) cost += (mid * b - nb) * pb; n     if (mid * c >= nc) cost += (mid * c - nc) * pc; n     if (mid * s >= ns) cost += (mid * s - ns) * ps; n     if (cost <= rubles) { n       ans = mid; n       left = mid + 1; n     } else n       right = mid - 1; n   } n   cout << ans <<   n ; n } n signed main() { n   file_handling(); n   long long testCases = 1; n   while (testCases--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mxn = 0; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   while (t--) { n     long long int i, j, k, n, m, x; n     cin >> n >> x >> k; n     vector<long long int> a(n); n     for (i = 0; i < n; i++) cin >> a[i]; n     sort(a.begin(), a.end()); n     long long int ans = 0; n     for (i = 0; i < n; i++) { n       long long int l = a[i] / x; n       if (a[i] % x) { n         ans += lower_bound(a.begin(), a.end(), x * (l + k + 1)) - n                lower_bound(a.begin(), a.end(), max((l + k) * x, a[i])); n       } else { n         ans += lower_bound(a.begin(), a.end(), (l + k) * x) - n                lower_bound(a.begin(), a.end(), max((l + k - 1) * x, a[i])); n       } n     } n     cout << ans; n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n const int maxn=1e5+10; n int a[maxn],b[maxn],c[maxn]; n vector<int> v1[maxn],v2[maxn],app[maxn]; n int ans[maxn]; n int main() n { n  int t; n  cin>>t; n  while(t--) n  { n   memset(ans,-1,sizeof(ans)); n   int n,m; n   cin>>n>>m; n   for(int i=1;i<=n;i++) n   { n    v1[i].clear(); n    v2[i].clear(); n    app[i].clear(); n    cin>>a[i]; n   } n   for(int i=1;i<=n;i++) n   { n    cin>>b[i]; n    if(a[i]!=b[i]) n    { n     v1[b[i]].push_back(i); n    } n    else n    { n     app[a[i]].push_back(i); n    } n   } n   for(int i=1;i<=m;i++) n   { n    cin>>c[i]; n    v2[c[i]].push_back(i); n   } n   bool flag=true; n   for(int i=1;i<=n;i++) n   { n    if((int)v1[i].size()>(int)v2[i].size()) n    { n     puts( NO ); n     flag=false; n     break; n    } n    int pp=-1; n    if(!v1[i].empty()) n    { n     pp=v1[i].back(); n    } n    for(int j=0;j<v2[i].size();j++) n    { n     if(v1[i].empty()) n     { n      if(pp!=-1) n      { n       ans[v2[i][j]]=pp; n      } n      else if(!app[i].empty()) n      { n       ans[v2[i][j]]=app[i].back(); n      } n     } n     else n     { n      ans[v2[i][j]]=v1[i].back(); n      v1[i].pop_back(); n     } n    } n   } n   if(!flag) n   { n    continue; n   } n   if(ans[m]==-1) n   { n    puts( NO ); n    continue; n   } n   puts( YES ); n   for(int i=1;i<=m;i++) n   { n    if(ans[i]==-1) n    { n     ans[i]=ans[m]; n    } n    cout<<ans[i]<<   ; n   } n   cout<<endl; n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, step, smallest, count = 0; n   bool possible = true; n   cin >> n >> step; n   vector<long long> p(n); n   cin >> p[0]; n   smallest = p[0]; n   for (int i = 1; i < n; i++) { n     cin >> p[i]; n     if (p[i] < smallest) { n       smallest = p[i]; n     } n   } n   for (int i = 0; i < n; i++) { n     if ((p[i] - smallest) % step != 0) { n       cout << -1 << endl; n       possible = false; n       break; n     } else { n       count += (p[i] - smallest) / step; n     } n   } n   if (possible == true) { n     cout << count << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int dx[] = {0, 1, 0, -1}; n const int dy[] = {1, 0, -1, 0}; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline long long read1() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n const int N = 200010; n const int P = 1e9 + 7; n int dp[110][110][110]; n int pw[110]; n int Add(int a, int b) { n   a += b; n   return a >= P ? a - P : a; n } n char ch[110]; n int main() { n   int n, x; n   n = read(); n   x = read(); n   scanf( %s , ch + 1); n   for (int i = 1; i <= n; i++) dp[ch[i] -  0 ][i][i] = 1; n   pw[0] = pw[1] = 2; n   for (int i = 2; i <= x; i++) { n     pw[i] = (long long)(pw[i - 1]) * pw[i - 2] % P; n     for (int l = 1; l <= n; l++) n       for (int r = l; r <= n; r++) { n         dp[i][l][r] = n             Add((long long)(dp[i - 1][l][r]) * (r == n ? pw[i - 2] : 1) % P, n                 (long long)(dp[i - 2][l][r]) * (l == 1 ? pw[i - 1] : 1) % P); n         for (int m = l; m <= r - 1; m++) n           dp[i][l][r] = Add(dp[i][l][r], (long long)(dp[i - 1][l][m]) * n                                              dp[i - 2][m + 1][r] % P); n       } n   } n   cout << dp[x][1][n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n int n, sum, ans[6050]; n bool isprime(int x) { n   if (x < 2 || (x & 1) == 0) { n     return 0; n   } n   for (int i = 2; i * i <= x; ++i) { n     if (x % i == 0) { n       return 0; n     } n   } n   return 1; n } n int main() { n   scanf( %d , &n); n   sum = n * (n + 1) / 2; n   if (isprime(sum)) { n     for (int i = 1; i <= n; ++i) { n       ans[i] = 1; n     } n   } else if ((sum & 1) == 0) { n     for (int i = 3; i <= n; ++i) { n       if (isprime(i) && isprime(sum - i)) { n         ans[i] = 2; n         break; n       } n     } n     for (int i = 1; i <= n; ++i) { n       if (!ans[i]) { n         ans[i] = 1; n       } n     } n   } else { n     if (isprime(sum - 2)) { n       for (int i = 1; i <= n; ++i) { n         ans[i] = 1; n       } n       ans[2] = 2; n     } else { n       sum -= 3; n       ans[3] = 3; n       for (int i = 5; i <= n; ++i) { n         if (isprime(i) && isprime(sum - i)) { n           ans[i] = 2; n           break; n         } n       } n       for (int i = 1; i <= n; ++i) { n         if (!ans[i]) { n           ans[i] = 1; n         } n       } n     } n   } n   for (int i = 1; i <= n; ++i) { n     printf( %d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s[2005]; n char str[2005][2005]; n int a[2005][2005]; n int v[2005][2005]; n long long dp[8005][2005]; n int pool[8005], top = 0; n int child[8005]; n int ID = 0, n; n int getID() { return pool[--top]; } n void pre() { n   for (int i = 8000; i >= 0; i--) pool[top++] = i; n } n void delID(int x) { pool[top++] = x; } n int K; n int combine(int me, int you, int D) { n   int ret = getID(); n   child[ret] = min(K, child[me] + child[you]); n   for (int i = 0; i <= child[ret]; i++) dp[ret][i] = 0; n   for (int i = 0; i <= child[me]; i++) n     for (int j = 0; j <= child[you] && i + j <= K; j++) n       dp[ret][i + j] = max(dp[ret][i + j], dp[me][i] + dp[you][j] + i * j * D); n   return ret; n } n int go(int L, int R, int D) { n   int me = getID(); n   int c = 0; n   while (L <= R && str[L][D] ==   0 ) ++c, ++L; n   child[me] = c; n   for (int i = 0; i <= c; i++) dp[me][i] = i * (i - 1) / 2 * D; n   for (int i = L, j = L; i <= R; i = j) { n     for (j = i; j <= R && str[i][D] == str[j][D]; j++) n       ; n     int you = go(i, j - 1, D + 1); n     int nxt = combine(me, you, D); n     delID(me); n     delID(you); n     me = nxt; n   } n   return me; n } n int main(void) { n   pre(); n   scanf( %d%d , &n, &K); n   for (int i = 1; i <= n; i++) cin >> s[i]; n   sort(s + 1, s + 1 + n); n   for (int i = 1; i <= n; i++) strcpy(str[i], s[i].c_str()); n   int me = go(1, n, 0); n   printf( %I64d n , dp[me][K]); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n const int maxN = 110; n int n, k, a[maxN]; n void solve() { n   vector<vector<double> > d[2]; n   int u = 0, v = 1; n   d[u].assign(maxN, vector<double>(maxN, 0.0)); n   for (int i = 1; i <= n; ++i) { n     for (int j = i + 1; j <= n; ++j) { n       if (a[i] < a[j]) { n         d[u][i][j] = 1.0; n       } else { n         d[u][j][i] = 1.0; n       } n     } n   } n   double p = 2.0 / (double)(n * (n + 1)); n   for (int steps = 0; steps < k; ++steps, swap(u, v)) { n     d[v].assign(maxN, vector<double>(maxN, 0.0)); n     for (int i = 1; i <= n; ++i) { n       for (int j = 1; j <= n; ++j) { n         if (i == j) { n           continue; n         } n         for (int a = 1; a <= n; ++a) { n           for (int b = a; b <= n; ++b) { n             int ni = i; n             if (a <= i && i <= b) { n               ni = a + b - i; n             } n             int nj = j; n             if (a <= j && j <= b) { n               nj = a + b - j; n             } n             d[v][ni][nj] += p * d[u][i][j]; n           } n         } n       } n     } n   } n   double res = 0.0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j < i; ++j) { n       res += d[u][i][j]; n     } n   } n   printf( %.10lf n , res); n } n int main() { n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int q[100010], t, n; n bool cmp(int a, int b) { return a < b; } n int main() { n   scanf( %d , &t); n   for (int i = 1; i <= t; i++) { n     scanf( %d , &n); n     for (int j = 1; j <= n; j++) { n       scanf( %d , &q[j]); n     } n     int flag = 1; n     sort(q + 1, q + n + 1, cmp); n     for (int k = n - 2; k >= 1; k--) { n       if (q[n - 1] >= k + 1) { n         printf( %d n , k); n         flag = 0; n         break; n       } n     } n     if (flag) printf( 0 n ); n   } n } n


#include <bits/stdc++.h> n int main() { n   long int n, m; n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %ld%ld , &n, &m); n     if (n == 1) n       printf( 0 n ); n     else if (n == 2) { n       printf( %ld n , m); n     } else { n       printf( %ld n , 2 * m); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n struct particle { n   int x, v; n } particles[500002]; n bool possible(long double time) { n   long double maxRightyX = -1e19; n   for (int i = 1; i <= n; i++) { n     if (particles[i].v > 0) { n       maxRightyX = max(particles[i].x + particles[i].v * time, maxRightyX); n     } else if (maxRightyX >= particles[i].x + particles[i].v * time) { n       return true; n     } n   } n   return false; n } n void bin() { n   long double l = 0, r = 1000000000; n   while (l + 0.0000000001 < r) { n     long double mid = (l + r) / 2; n     if (possible(mid)) n       r = mid; n     else n       l = mid + 0.0000000001; n   } n   cout << fixed << setprecision(10); n   if (possible(l)) { n     cout << l; n   } else if (possible(r)) { n     cout << r; n   } else { n     cout <<  -1 ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> particles[i].x >> particles[i].v; n   bin(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> adj; n vector<int> a; n vector<int> dep; n vector<int> ret; n unordered_map<int, vector<int>> pmp; n unordered_map<int, vector<int>> mp; n vector<int> primeFactors(int num) { n   vector<int> ret; n   for (int i = 2; i * i <= num; i++) { n     while (num % i == 0) { n       ret.push_back(i); n       num /= i; n     } n   } n   if (num != 1) ret.push_back(num); n   return ret; n } n void dfs(int idx, int prev, int level) { n   dep[idx] = level; n   for (int i : pmp[a[idx]]) { n     if (mp.count(i) && !mp[i].empty()) { n       if (ret[idx] == -1 || dep[ret[idx]] < dep[mp[i].back()]) n         ret[idx] = mp[i].back(); n     } n   } n   for (int i : pmp[a[idx]]) { n     mp[i].push_back(idx); n   } n   for (auto nidx : adj[idx]) { n     if (nidx == prev) continue; n     dfs(nidx, idx, level + 1); n   } n   for (int i : pmp[a[idx]]) { n     mp[i].pop_back(); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, q; n   cin >> n >> q; n   a = vector<int>(n + 1, 0); n   dep = vector<int>(n + 1, 0); n   ret = vector<int>(n + 1, -1); n   for (int i = 1; i <= n; i++) cin >> a[i]; n   adj = vector<vector<int>>(n + 1); n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   for (int i = 1; i <= n; i++) { n     if (pmp.count(a[i])) continue; n     pmp[a[i]] = primeFactors(a[i]); n   } n   dfs(1, 0, 1); n   while (q--) { n     int type, v, w; n     cin >> type; n     if (type == 1) { n       cin >> v; n       cout << ret[v] << endl; n     } else { n       cin >> v >> w; n       a[v] = w; n       if (pmp.count(a[v])) continue; n       pmp[a[v]] = primeFactors(a[v]); n       ret = vector<int>(n + 1, -1); n       dfs(1, 0, 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool prime(int x) { n   if (x % 2 == 0) return 0; n   for (size_t i = 3; i * i <= x; i += 2) { n     if (x % i == 0) return 0; n   } n   return 1; n } n void solve() { n   int n, x = 3; n   cin >> n; n   while (n) { n     if (prime(x)) { n       cout << x <<    ; n       n--; n     } n     x += 2; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 210000; n int n, root, opt, Max[N << 6], lson[N << 6], rson[N << 6], lazy[N << 6]; n void pushdown(int k, int l, int r) { n   if (!lazy[k]) return; n   if (l != r) { n     if (!lson[k]) lson[k] = ++opt; n     if (!rson[k]) rson[k] = ++opt; n     lazy[lson[k]] += lazy[k]; n     lazy[rson[k]] += lazy[k]; n     Max[lson[k]] += lazy[k]; n     Max[rson[k]] += lazy[k]; n   } n   lazy[k] = 0; n } n void insert(int &k, int l, int r, int L, int R) { n   if (l > R || r < L) return; n   if (!k) k = ++opt; n   if (l >= L && r <= R) return (void)(Max[k]++, lazy[k]++); n   pushdown(k, l, r); n   int mid = l + r >> 1; n   if (L <= mid) insert(lson[k], l, mid, L, R); n   if (R > mid) insert(rson[k], mid + 1, r, L, R); n   Max[k] = max(Max[lson[k]], Max[rson[k]]); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     insert(root, 0, 1000000000, x, y); n     if (Max[1] > 2) { n       puts( NO ); n       return 0; n     } n   } n   puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 6e5; n const int oo = 1e9 + 5; n int du[] = {-1, 0, 0, 1}; n int dv[] = {0, -1, 1, 0}; n const long long mod = 1e9 + 7; n long long sqr(long long x) { return x * x; } n int n, x, a[N + 5], ans; n bool vis[N + 5]; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> x; n   vis[x] = true; n   for (int i = 1; i < (1 << n); i++) { n     if (!vis[i]) a[ans++] = i; n     vis[i ^ x] = vis[i] = true; n   } n   cout << ans << endl; n   for (int i = 1; i <= ans; i++) cout << (a[i] ^ a[i - 1]) <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5 * 100 * 1000 + 10; n int arr[MAXN], nx[MAXN], bf[MAXN]; n stack<int> st; n pair<int, int> pr[MAXN]; n int main() { n   int n, m, p; n   cin >> n >> m >> p; n   string second, w; n   cin >> second >> w; n   second =     + second; n   int cntr = 1; n   for (int i = 1; i < second.size(); i++) { n     if (second[i] ==  ( ) { n       arr[i] = cntr; n       pr[cntr].first = i; n       st.push(cntr); n       cntr++; n     } else { n       arr[i] = st.top(); n       pr[arr[i]].second = i; n       st.pop(); n     } n     nx[i] = i + 1; n     bf[i] = i - 1; n   } n   nx[0] = 1; n   for (int i = 0; i < w.size(); i++) { n     if (w[i] ==  R ) n       p = nx[p]; n     else if (w[i] ==  L ) n       p = bf[p]; n     else { n       int sr = pr[arr[p]].first; n       int er = pr[arr[p]].second; n       nx[bf[sr]] = nx[er]; n       bf[nx[er]] = bf[sr]; n       if (nx[er] < second.size()) n         p = nx[er]; n       else n         p = bf[sr]; n     } n   } n   int i = nx[0]; n   while (i < second.size()) { n     cout << second[i]; n     i = nx[i]; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int l, r, a, b, dif, t, x, lit, poz; n bool uz[27]; n char s[1001]; n int main() { n   cin >> a >> b >> l >> r; n   dif = min(500, r - l); n   l = l % (a + b); n   if (l == 0) l = a + b; n   r = l + dif; n   if (l <= a) { n     for (int i = l; i <= min(a, r); i++) s[++poz] = i; n     l = a + 1; n     t = 1; n   } else { n     for (int i = l; i <= min(a + b, r); i++) s[++poz] = 1; n     l = a + b + 1; n     memset(uz, 0, sizeof(uz)); n     for (int i = poz; i >= max(1, poz - a + 1); i--) uz[s[i]] = 1; n     lit = 1; n     t = 2; n   } n   x = 0; n   while (l <= r) { n     if (t == 1) { n       x++; n       s[++poz] = s[poz - 1]; n       l++; n       if (x == b) { n         memset(uz, 0, sizeof(uz)); n         for (int i = poz; i >= max(1, poz - a + 1); i--) uz[s[i]] = 1; n         lit = 1; n         t = 2; n         x = 0; n       } n     } else { n       x++; n       while (uz[lit] == 1) lit++; n       s[++poz] = lit; n       l++; n       uz[lit] = 1; n       if (x == a) { n         t = 1; n         x = 0; n       } n     } n   } n   memset(uz, 0, sizeof(uz)); n   for (int i = 1; i <= poz; i++) { n     uz[s[i]] = 1; n   } n   int nr = 0; n   for (int i = 1; i <= 26; i++) nr += uz[i]; n   cout << nr; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long pwr(long long base, long long p, long long mod = (1000000007LL)) { n   long long ans = 1; n   while (p) { n     if (p & 1) ans = (ans * base) % mod; n     base = (base * base) % mod; n     p /= 2; n   } n   return ans; 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 int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   string a, b; n   cin >> a >> b; n   int cnt1 = 0; n   for (int i = 0; i < (int)a.length(); i++) cnt1 += a[i] -  0 ; n   int cnt2 = 0; n   for (int i = 0; i < (int)b.length(); i++) cnt2 += b[i] -  0 ; n   if (cnt1 + (cnt1 & 1) >= cnt2) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long iinf = 1e9 + 10; n const long long inf = 1ll << 60; n const long long MOD = 998244353; n void GG() { n   cout <<  0 n ; n   exit(0); n } n const long long maxn = 1e5 + 5; n vector<pair<long long, long long> > here[maxn]; n template <long long mod> n struct Modint { n   long long val; n   Modint(long long _val = 0) { val = _val; } n   operator long long() const { return (*this).val; } n   Modint operator+(const Modint& o) const { n     return val + o.val >= mod ? Modint(val + o.val - mod) : Modint(val + o.val); n   } n   Modint operator-() const { return val ? Modint(mod - val) : *this; } n   Modint operator-(const Modint& o) const { n     return val - o.val < 0 ? Modint(val - o.val + mod) : Modint(val - o.val); n   } n   Modint operator*(const Modint& o) const { return Modint(val * o.val % mod); } n   Modint operator+=(const Modint& o) { n     *this = (*this) + o; n     return *this; n   } n   Modint operator-=(const Modint& o) { n     *this = (*this) - o; n     return *this; n   } n   Modint operator*=(const Modint& o) { n     *this = (*this) * o; n     return *this; n   } n   Modint Pow(long long b) const { n     Modint tmp(val), ret(1); n     while (b) { n       if (b & 1) ret *= tmp; n       b >>= 1; n       tmp *= tmp; n     } n     return ret; n   } n   Modint Pow(const Modint& a, long long b) const { return a.Pow(b); } n   inline Modint inv() const { return (*this).Pow(mod - 2); } n   Modint operator/(const Modint& o) const { return *this * o.inv(); } n   bool operator==(const Modint& o) const { return val == o.val; } n }; n Modint<MOD> mpow(Modint<MOD> a, long long n, long long mo = MOD) { n   Modint<MOD> re = 1; n   while (n > 0) { n     if (n & 1) re = re * a % mo; n     a = a * a % mo; n     n >>= 1; n   } n   return re; n } n Modint<MOD> inv(long long b, long long mo = MOD) { n   if (b == 1) return b; n   return (mo - mo / b) * inv(mo % b, mo) % mo; n } n const Modint<MOD> i4 = inv(4); n const Modint<MOD> i2 = inv(2); n Modint<MOD> go(long long x) { n   ; n   Modint<MOD> oth2 = 0; n   Modint<MOD> oth1 = 0; n   Modint<MOD> oth0 = 1; n   Modint<MOD> siga = 0; n   long long sigf = 0; n   for (pair<long long, long long>& p : here[x]) { n     Modint<MOD> a = p.first, first = p.second; n     Modint<MOD> mf = mpow(2, first); n     oth2 = mf * oth2 + n            oth0 * a * a * mf * i4 * first * (first + (Modint<MOD>)1) + n            oth1 * (Modint<MOD>)2 * a * mf * i2 * first; n     siga += a * first; n     sigf += first; n     sigf %= (MOD - 1); n     Modint<MOD> m2 = mpow(2, sigf); n     oth1 = siga * m2 * i2; n     oth0 = m2; n   } n   Modint<MOD> mpowsigf = mpow(2, sigf); n   ; n   Modint<MOD> re = 0; n   for (pair<long long, long long>& p : here[x]) { n     Modint<MOD> a = p.first, first = p.second; n     Modint<MOD> have1 = siga * mpowsigf * i4 + a * mpowsigf * inv(4); n     Modint<MOD> two = a * a * mpowsigf * i2 + oth2 + oth1 * (Modint<MOD>)2 * a - n                       (Modint<MOD>)2 * a * have1; n     two *= i2; n     ; n     re += two * first; n   }; n   return re - oth2; n } n long long mob[maxn]; n bool np[maxn]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   long long n; n   cin >> n; n   for (long long i = 0; i < n; ++i) { n     long long a, first; n     cin >> a >> first; n     for (long long j = 1; j <= sqrt(a); ++j) { n       if (a % j == 0) { n         here[j].push_back({a, first}); n         if (j * j != a) { n           here[a / j].push_back({a, first}); n         } n       } n     } n   } n   fill(mob, mob + maxn - 1, 1); n   for (long long i = 2; i < maxn; ++i) { n     if (!np[i]) { n       mob[i] = -1; n       for (long long j = i + i; j < maxn; j += i) { n         mob[j] = -mob[j]; n         np[j] = 1; n       } n       if (i * i <= maxn) n         for (long long j = i * i; j < maxn; j += i * i) { n           mob[j] = 0; n         } n     } n   } n   long long re = 0; n   for (long long i = 0; i < maxn; ++i) { n     if (mob[i] && (long long)here[i].size()) { n       re += mob[i] * go(i); n       re %= MOD; n     } n   } n   cout << (re + MOD) % MOD <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void amin(T &first, const T &second) { n   if (second < first) first = second; n } n template <class T> n inline void amax(T &first, const T &second) { n   if (first < second) first = second; n } n void solve() { n   int n; n   cin >> n; n   vector<pair<int, int> > a(n); n   map<int, int> home; n   for (int i = 0; i < n; ++i) { n     cin >> a[i].first >> a[i].second; n     home[a[i].first]++; n   } n   vector<int> ret(n); n   for (int i = 0; i < n; ++i) { n     ret[i] = n - 1; n     ret[i] += home[a[i].second]; n   } n   for (int i = 0; i < n; ++i) { n     cout << ret[i] <<     << 2 * (n - 1) - ret[i] <<   n ; n   } n } n int main() { n   ios_base::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   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   long long q; n   cin >> q; n   for (long long t = 0; t < q; t++) { n     long long n, k; n     cin >> n >> k; n     vector<vector<long long>> adj((size_t)n); n     map<pair<long long, long long>, long long> weight; n     for (long long i = 0; i < n - 1; i++) { n       long long u, v, w; n       cin >> u >> v >> w; n       u--, v--; n       adj[u].push_back(v); n       adj[v].push_back(u); n       weight[{u, v}] = w; n       weight[{v, u}] = w; n     } n     vector<vector<long long>> dp((size_t)n, vector<long long>((size_t)2)); n     function<void(long long, long long)> visit = [&](long long u, long long v) { n       dp[v][0] = 0; n       dp[v][1] = 0; n       vector<long long> g; n       for (long long w : adj[v]) { n         if (w != u) { n           visit(v, w); n           dp[v][0] += dp[w][1]; n           dp[v][1] += dp[w][1]; n           g.push_back(max(dp[w][0] + weight[{v, w}], dp[w][1]) - dp[w][1]); n         } n       } n       sort(g.rbegin(), g.rend()); n       dp[v][0] += accumulate( n           g.begin(), g.begin() + min((long long)g.size(), k - 1), (long long)0); n       dp[v][1] += accumulate(g.begin(), g.begin() + min((long long)g.size(), k), n                              (long long)0); n     }; n     visit(-1, 0); n     cout << dp[0][1] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct pt { n   unsigned long long x, y, v; n }; n int n; n vector<unsigned long long> a, b; n pt c[200005]; n vector<vector<unsigned long long>> d; n void sq(vector<unsigned long long>& v) { n   sort(v.begin(), v.end()); n   v.erase(unique(v.begin(), v.end()), v.end()); n } n template <class T> n T gcd(T a, T b) { n   T t; n   while (a) { n     t = a; n     a = b % a; n     b = t; n   } n   return b; n } n const unsigned long long MOD1 = 1000000007; n const unsigned long long MOD2 = 1000000009; n struct frac { n   unsigned long long p, q; n   bool operator==(const frac& b) const { n     unsigned long long v11 = (p % MOD1 * (b.q % MOD1)) % MOD1; n     unsigned long long v12 = (q % MOD1 * (b.p % MOD1)) % MOD1; n     if (v11 != v12) return false; n     unsigned long long v21 = (p % MOD2 * (b.q % MOD2)) % MOD2; n     unsigned long long v22 = (q % MOD2 * (b.p % MOD2)) % MOD2; n     if (v21 != v22) return false; n     return p * b.q == q * b.p; n   } n }; n bool deli(unsigned long long a, unsigned long long b, unsigned long long c) { n   unsigned long long g = gcd(a, b); n   a /= g; n   g = gcd(a, c); n   return a == g; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   cin >> n; n   for (int i = 0; i < n; i++) { n     unsigned long long x, y, v; n     cin >> x >> y >> v; n     c[i] = {x, y, v}; n     a.push_back(x); n     b.push_back(y); n   } n   sq(a); n   sq(b); n   int rows = a.size(); n   int cols = b.size(); n   if (rows * 1ll * cols != n) { n     cout <<  0 n ; n     return 0; n   } n   d.assign(rows, vector<unsigned long long>(cols, 0)); n   for (int i = 0; i < n; i++) { n     int rr = lower_bound(a.begin(), a.end(), c[i].x) - a.begin(); n     int cc = lower_bound(b.begin(), b.end(), c[i].y) - b.begin(); n     d[rr][cc] = c[i].v; n   } n   bool ok = true; n   for (int i = 0; i < rows - 1; i++) { n     for (int j = 0; j < cols - 1; j++) { n       frac a = {d[i][j], d[i + 1][j]}; n       frac b = {d[i][j + 1], d[i + 1][j + 1]}; n       if (!(a == b)) { n         ok = false; n       } n     } n   } n   if (!ok) { n     cout <<  0 n ; n     return 0; n   } n   unsigned long long sol = 0; n   unsigned long long v0 = d[0][0]; n   unsigned long long grow = 0; n   for (int j = 0; j < cols; j++) grow = gcd(grow, d[0][j]); n   unsigned long long gcol = 0; n   for (int i = 0; i < rows; i++) gcol = gcd(gcol, d[i][0]); n   set<unsigned long long> divs; n   for (unsigned long long i = 1; i * i <= grow; i++) { n     if (grow % i == 0) { n       divs.insert(i); n       divs.insert(grow / i); n     } n   } n   for (unsigned long long g : divs) { n     if (deli(v0, gcol, g)) { n       sol++; n     } n   } n   cout << sol <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   map<char, int> mp; n   for (char c : s) mp[c]++; n   cout << 2 * min(mp[ L ], mp[ R ]) + 2 * min(mp[ U ], mp[ D ]) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a, b, c; n   int n; n   cin >> n >> a; n   sort(a.begin(), a.end()); n   for (int i = 0; i < a.size(); i += n) b += a[i]; n   for (int i = 0; i < n; i++) c += b; n   b = c; n   sort(b.begin(), b.end()); n   if (b != a) n     cout << -1; n   else n     cout << c; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v[100005]; n vector<pair<int, int> > asdf; n int n; n int m; n int cnt[100005]; n vector<int> nxt[100005]; n void build(int at, int num) { n   if (cnt[at] > 2) return; n   if (num == m) { n     while ((int)asdf.size()) { n       printf( %d %d n , 1 + asdf.back().first, 1 + asdf.back().second); n       asdf.pop_back(); n     } n     exit(0); n   } n   if (cnt[at] == 2) return; n   int a, b; n   if ((int)v[at].size() != 0) { n     a = v[at].front(); n     b = v[at].back(); n   } else { n     a = -1, b = -1; n   } n   int c, d; n   if ((int)nxt[at].size()) { n     c = nxt[at].back(); n     d = nxt[at].front(); n   } else { n     c = d = -1; n   } n   for (auto(i) = (at + 1); (i) != (n); (i)++) { n     if (i != d && i != c && i != a && i != b && cnt[i] < 2) { n       asdf.push_back(make_pair(i, at)); n       cnt[at]++; n       cnt[i]++; n       nxt[at].push_back(i); n       nxt[i].push_back(at); n       build(i, num + 1); n       nxt[at].pop_back(); n       nxt[i].pop_back(); n       cnt[at]--; n       cnt[i]--; n       asdf.pop_back(); n     } n   } n   if (cnt[at] > 0 && cnt[at] < 2) { n     c = nxt[at].front(); n     d = nxt[at].back(); n     for (auto(i) = (0); (i) != (at); (i)++) { n       if (cnt[i] < 2 && i != a && i != b && i != c && i != d) { n         asdf.push_back(make_pair(i, at)); n         cnt[at]++; n         cnt[i]++; n         nxt[at].push_back(i); n         nxt[i].push_back(at); n         build(i, num + 1); n         nxt[at].pop_back(); n         nxt[i].pop_back(); n         cnt[at]--; n         cnt[i]--; n         asdf.pop_back(); n       } n     } n   } else { n     c = d = -1; n     for (auto(i) = (0); (i) != (at); (i)++) { n       if (i != c && i != d && i != a && i != b && cnt[i] < 2) { n         asdf.push_back(make_pair(i, at)); n         cnt[at]++; n         cnt[i]++; n         nxt[at].push_back(i); n         nxt[i].push_back(at); n         build(i, num + 1); n         nxt[at].pop_back(); n         nxt[i].pop_back(); n         cnt[at]--; n         cnt[i]--; n         asdf.pop_back(); n       } n     } n   } n } n int main() { n   cin >> n >> m; n   for (auto(i) = (0); (i) != (m); (i)++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     v[a].push_back(b), v[b].push_back(a); n   } n   int kek = 0; n   for (auto(i) = (0); (i) != (n); (i)++) n     kek += (min(2, (n - 1 - (int)v[i].size()))); n   kek /= 2; n   if (kek < m) { n     puts( -1 ); n     return 0; n   } n   for (auto(i) = (0); (i) != (n); (i)++) build(i, 0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 4; n vector<int> adj[N]; n int deg[N]; n int lv[N]; n int decr[N]; n int vis[N]; n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     adj[x].push_back(y); n     adj[y].push_back(x); n     deg[x]++; n     deg[y]++; n   } n   queue<pair<int, int>> q; n   bool yes = n > 3; n   for (int i = 1; i <= n; i++) n     if (deg[i] == 1) q.push({i, 0}), lv[i] = 1, decr[i] = 3; n   while (!q.empty()) { n     auto tmp = q.front(); n     q.pop(); n     int u = tmp.first; n     int p = tmp.second; n     vis[u] = 1; n     if (decr[u] < 3) yes = false; n     for (auto v : adj[u]) { n       if (vis[v]) continue; n       if (lv[v] != 0 && lv[v] != lv[u] + 1) { n         yes = false; n       } n       lv[v] = lv[u] + 1; n       deg[v]--; n       decr[v]++; n       if (lv[v] == k + 1 && deg[v] == 0) n         q.push({v, u}); n       else if (deg[v] == 1) n         q.push({v, u}); n     } n   } n   int mx = 0; n   for (int i = 1; i <= n; i++) mx = max(mx, lv[i]); n   yes &= (mx == k + 1); n   puts(yes ?  Yes  :  No ); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 5000000; n void solve1(char *S) { n   int lenS = strlen(S); n   for (int i = 0, j = lenS - 1; i < j; i++, j--) n     if (S[i] < S[j]) n       break; n     else if (S[i] > S[j]) { n       for (i = 0, j = lenS - 1; i < j; i++, j--) swap(S[i], S[j]); n       break; n     } n   puts(S); n } n void lyndon(char *S, int *f, int lenS) { n   int cnt = 0; n   for (int i = 0; i < lenS; i++) f[i] = 0; n   for (int i = 0; i < lenS;) { n     int j = i, k = i + 1; n     while (k < lenS && S[j] <= S[k]) j = (S[j] == S[k] ? j + 1 : i), k++; n     int t = k - j; n     cnt++; n     while (i + t - 1 < k) f[i + t - 1] = cnt, i += t; n   } n } n void algorithmZ(char *S, int *f, int lenS) { n   f[0] = lenS; n   int mx = 0, ps = 0; n   for (int i = 1; i < lenS; i++) { n     f[i] = (i <= mx ? min(mx - i + 1, f[i - ps]) : 0); n     while (S[f[i]] == S[i + f[i]]) f[i]++; n     if (i + f[i] - 1 > mx) mx = i + f[i] - 1, ps = i; n   } n } n char ans[MAXN + 5], T[2 * MAXN + 5]; n int f[2 * MAXN + 5]; n int cmp(int l, int r) { n   int p = f[l]; n   if (p >= r - l + 1) n     return 0; n   else n     return (T[l + p] < T[p] ? -1 : 1); n } n bool cmp2(char *S, char *T, int n) { n   for (int i = 0; i < n; i++) { n     if (S[i] < T[i]) n       return true; n     else if (S[i] > T[i]) n       return false; n   } n   return true; n } n void update(char *T, int lenS) { n   if (cmp2(T, ans, lenS)) n     for (int i = 0; i < lenS; i++) ans[i] = T[i]; n } n int get(int x) { n   int p; n   for (p = x - 1; p >= 0 && f[p] == 0; p--) n     ; n   return p; n } n void update2(char *S, int x, int lenS) { n   for (int i = 0; i < x; i++) T[i] = S[x - i - 1]; n   for (int i = x; i < lenS; i++) T[i] = S[i]; n   update(T, lenS); n   for (int i = x, j = lenS - 1; i < j; i++, j--) swap(T[i], T[j]); n   update(T, lenS); n } n void solve2(char *S) { n   int lenS = strlen(S); n   for (int i = 0; i < lenS; i++) ans[i] = S[i]; n   for (int i = 0; i < lenS; i++) T[lenS - i - 1] = S[i], T[lenS + i + 1] = S[i]; n   algorithmZ(T, f, 2 * lenS + 1); n   int pos = 0; n   for (int i = 1; i < lenS - 1; i++) { n     int t = cmp(lenS + 1 + pos + 1, lenS + 1 + i); n     if (t == 0) { n       if (cmp(i - pos, lenS - pos - 2) == 1) pos = i; n     } else if (t == -1) n       pos = i; n   } n   for (int i = 0; i < lenS; i++) T[i] = S[i]; n   for (int i = pos + 1, j = lenS - 1; i < j; i++, j--) swap(T[i], T[j]); n   update(T, lenS); n   for (int i = 0; i < lenS; i++) T[lenS - i - 1] = S[i]; n   lyndon(T, f, lenS); n   int tmp = get(lenS - 1); n   while (tmp >= 0 && f[tmp] == f[lenS - 1]) tmp = get(tmp); n   update2(S, lenS - tmp - 1, lenS); n   if (tmp >= 0) { n     int p = get(tmp); n     while (p >= 0 && f[p] == f[tmp]) p = get(p); n     update2(S, lenS - p - 1, lenS); n   } n   puts(ans); n } n void print(int l, int r) { n   for (int i = l; i <= r; i++) putchar(T[i]); n } n char S[MAXN + 5]; n int main() { n   int k; n   scanf( %s%d , S, &k); n   if (k == 1) n     solve1(S); n   else { n     int lenS = strlen(S); n     for (int i = 0; i < lenS; i++) T[lenS - i - 1] = S[i]; n     lyndon(T, f, lenS); n     int lst = lenS - 1, len = 0, tmp = 0; n     while (k >= 2 && lst >= 0) { n       int p = get(lst); n       if (!(len == 1 && lst - p == 1) && !(f[lst] == tmp)) { n         if (k == 2) break; n         k--; n       } n       print(p + 1, lst); n       len = lst - p, tmp = f[lst], lst = p; n     } n     if (lst >= 0) solve2(S + (lenS - lst - 1)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int sz = 1e5 + 100; n const int INF = 1e9; n const double eps = 1e-7; n void solve() { n   int n; n   cin >> n; n   vector<long long> a(n + 1, 0); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a.begin(), a.begin() + n); n   for (int i = n - 1; i > -1; i--) { n     cout << a[i % 2 ? n - 1 - i / 2 : i / 2] <<    ; n   } n   cout << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<long long> hs, vs; n multiset<long long> hms, vms; n int main() { n   long long h, w, t, n, i, j, k, l; n   char c; n   cin >> w >> h >> n; n   hs.insert(0); n   hs.insert(h); n   vs.insert(0); n   vs.insert(w); n   hms.insert(h); n   vms.insert(w); n   for (i = 0; i < n; i++) { n     cin >> c >> j; n     if (c ==  H ) { n       hs.insert(j); n       auto q = hs.find(j); n       auto p = q; n       auto r = q; n       p--; n       r++; n       hms.erase(hms.find(*r - *p)); n       hms.insert(*q - *p); n       hms.insert(*r - *q); n     } else { n       vs.insert(j); n       auto q = vs.find(j); n       auto p = q; n       auto r = q; n       p--; n       r++; n       vms.erase(vms.find(*r - *p)); n       vms.insert(*q - *p); n       vms.insert(*r - *q); n     } n     cout << (*hms.rbegin()) * (*vms.rbegin()) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, n, a[101], d = 0, x = 0, y = 0, m = 0, p = 0; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     m = max(m, a[i]); n     p = min(p, a[i]); n     if (a[i] > 0) n       x++; n     else if (a[i] < 0) n       y++; n   } n   float z = ceil(n / 2.0); n   if (x >= z) n     d = m; n   else if (y >= z) n     d = p; n   cout << d << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void pr1(T a) { n   cout << a; n   cout <<   n ; n } n template <class T, class T2> n void pr2(T a, T2 b) { n   cout << a <<     << b; n   cout <<   n ; n } n template <class T, class T2, class T3> n void pr3(T a, T2 b, T3 c) { n   cout << a <<     << b <<     << c; n   cout <<   n ; n } n template <class T> n void PR(T a, int n) { n   for (int i = (int)(0); i < (int)(n); i++) { n     if (i) cout <<    ; n     cout << a[i]; n   } n   cout <<   n ; n } n bool check(int n, int m, int x, int y) { n   return x >= 0 && x < n && y >= 0 && y < m; n } n const long long MAX = 1000000007, MAXL = 1LL << 60, dx[4] = {-1, 0, 1, 0}, n                 dy[4] = {0, 1, 0, -1}; n void Main() { n   double d, h, v, e; n   cin >> d >> h >> v >> e; n   double r = d / 2; n   double t = v / (r * r * M_PI); n   double ans = h / (t - e); n   if (ans < 0) n     pr1( NO ); n   else { n     printf( YES n ); n     printf( %.10f n , ans); n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   Main(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int in(char &c) { return scanf( %c , &c); } n int in(char s[]) { return scanf( %s , s); } n int in(int &x) { return scanf( %d , &x); } n int in(long long &x) { return scanf( %lld , &x); } n int in(double &x) { return scanf( %lf , &x); } n int in(pair<int, int> &p) { return scanf( %d%d , &p.first, &p.second); } n int in(pair<long long, long long> &p) { n   return scanf( %lld%lld , &p.first, &p.second); n } n int in(pair<double, double> &p) { return scanf( %lf%lf , &p.first, &p.second); } n int in(long double &a) { return scanf( %Lf , &a); } n int in(int &x, long long &y) { return scanf( %d%lld , &x, &y); } n int in(long long &x, int &y) { return scanf( %lld%d , &x, &y); } n int in(int &x, int &y) { return scanf( %d%d , &x, &y); } n int in(long long &x, long long &y) { return scanf( %lld%lld , &x, &y); } n int in(char s[], int &n) { return scanf( %s%d , s, &n); } n int in(int &n, char s[]) { return scanf( %d%s , &n, s); } n int in(char a[], char b[]) { return scanf( %s%s , a, b); } n int in(double &x, double &y) { return scanf( %lf%lf , &x, &y); } n int in(int &n, char s[], char t[]) { return scanf( %d%s%s , &n, s, t); } n int in(int &x, int &y, int &z) { return scanf( %d%d%d , &x, &y, &z); } n int in(long long &x, long long &y, long long &z) { n   return scanf( %lld%lld%lld , &x, &y, &z); n } n int in(double &x, double &y, double &z) { n   return scanf( %lf%lf%lf , &x, &y, &z); n } n int in(int &x, int &y, int &z, int &k) { n   return scanf( %d%d%d%d , &x, &y, &z, &k); n } n int in(long long &x, long long &y, long long &z, long long &k) { n   return scanf( %lld%lld%lld%lld , &x, &y, &z, &k); n } n int in(double &x, double &y, double &z, double &k) { n   return scanf( %lf%lf%lf%lf , &x, &y, &z, &k); n } n const int N = 3e5 + 10, M = 1e3 + 10, mod = 1e9 + 7; n const double eps = 1e-6, Pi = acos(-1.0); n vector<int> g[N]; n int dep[N], vis[N], op[N], u[N], v[N], In[N], col[N], use[N], Out[N]; n queue<int> que; n int topsort(int n) { n   for (int i = 1; i <= n; i++) { n     if (In[i] == 0) { n       que.push(i); n     } n   } n   int cnt = 0; n   while (!que.empty()) { n     int u = que.front(); n     que.pop(); n     dep[u] = ++cnt; n     for (auto e : g[u]) { n       In[e]--; n       if (In[e] == 0) { n         que.push(e); n       } n     } n   } n   return (n != cnt); n } n int main() { n   int T, c = 0, t; n   in(T); n   t = T; n   while (T--) { n     c++; n     int n, m; n     in(n, m); n     for (int i = 1; i <= n; i++) In[i] = dep[i] = vis[i] = 0, g[i].clear(); n     for (int i = 1; i <= m; i++) { n       in(op[i], u[i], v[i]); n       if (op[i] == 1) { n         In[v[i]]++; n         g[u[i]].push_back(v[i]); n       } n     } n     if (topsort(n)) { n       printf( NO n ); n       continue; n     } n     printf( YES n ); n     for (int i = 1; i <= m; i++) { n       if (op[i] == 0) { n         if (dep[u[i]] > dep[v[i]]) swap(u[i], v[i]); n       } n       printf( %d %d n , u[i], v[i]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int par[4000]; n long long int sub[4000]; n long long int n; n vector<long long int> adj[4000]; n long long int dp[4000][4000]; n long long int conec[4000][4000]; n void dfsc(long long int node, long long int cur, long long int dir, n           long long int prev) { n   conec[node][cur] = dir; n   long long int ele = adj[cur].size(); n   for (long long int i = 0; i <= ele - 1; i++) { n     if (adj[cur][i] != prev) { n       dfsc(node, adj[cur][i], dir, cur); n     } n   } n } n void dfss(long long int node) { n   sub[node] = 1; n   long long int ele = adj[node].size(); n   for (long long int i = 0; i <= ele - 1; i++) { n     if (par[node] != adj[node][i]) { n       par[adj[node][i]] = node; n       dfss(adj[node][i]); n       sub[node] += sub[adj[node][i]]; n     } n   } n } n long long int depth(long long int now, long long int pr) { n   if (adj[now].size() == 1) return 0; n   long long int ret = 0; n   for (long long int i = 0; i <= adj[now].size() - 1; i++) { n     if (adj[now][i] != pr) ret = max(ret, depth(adj[now][i], now)); n   } n   return 1 + ret; n } n void dfs(long long int start, long long int fin) { n   if (dp[start][fin] != -1) return; n   if (par[start] == fin) { n     dp[start][fin] = sub[start] * (n - sub[start]); n     return; n   } n   if (par[fin] == start) { n     dp[start][fin] = sub[fin] * (n - sub[fin]); n     return; n   } n   dfs(conec[start][fin], fin); n   dfs(start, conec[fin][start]); n   long long int lef, rig; n   if (par[start] == conec[start][fin]) n     lef = sub[start]; n   else n     lef = n - sub[conec[start][fin]]; n   if (par[fin] == conec[fin][start]) n     rig = sub[fin]; n   else n     rig = n - sub[conec[fin][start]]; n   dp[start][fin] = n       lef * rig + max(dp[start][conec[fin][start]], dp[conec[start][fin]][fin]); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int cur, TESTS, q, a, m, s, b, r, c, k, p, w, d, x, i, j, y, z, xs, n       ys, t; n   TESTS = 1; n   memset(par, -1, sizeof(par)); n   memset(dp, -1, sizeof(dp)); n   while (TESTS--) { n     cin >> n; n     for (long long int i = 0; i <= n - 2; i++) { n       cin >> a >> b; n       a--; n       b--; n       adj[a].push_back(b); n       adj[b].push_back(a); n     } n     dfss(0); n     for (long long int i = 0; i <= n - 1; i++) { n       long long int ele = adj[i].size(); n       for (long long int j = 0; j <= ele - 1; j++) { n         dfsc(i, adj[i][j], adj[i][j], i); n       } n     } n     for (long long int i = 0; i <= n - 1; i++) { n       for (long long int j = 0; j <= n - 1; j++) { n         if (i == j) continue; n         dfs(i, j); n       } n     } n     long long int ans = 0; n     for (long long int i = 0; i <= n - 1; i++) n       for (long long int j = 0; j <= n - 1; j++) ans = max(ans, dp[i][j]); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 998244353; n struct intmod { n   long long val; n   intmod(long long _val = 1) { n     val = _val % MOD; n     if (val < 0) val += MOD; n   } n   bool operator!=(intmod rhs) const { return val != rhs.val; } n   bool operator==(intmod rhs) const { return val == rhs.val; } n   intmod operator+=(intmod rhs) { n     val += rhs.val; n     if (val >= MOD) val -= MOD; n     return *this; n   } n   intmod operator-=(intmod rhs) { n     val -= rhs.val; n     if (val < 0) val += MOD; n     return *this; n   } n   intmod operator*=(intmod rhs) { n     val = val * rhs.val % MOD; n     return *this; n   } n   friend intmod operator+(intmod lhs, intmod rhs) { return lhs += rhs; } n   friend intmod operator-(intmod lhs, intmod rhs) { return lhs -= rhs; } n   friend intmod operator*(intmod lhs, intmod rhs) { return lhs *= rhs; } n   friend intmod sqr(intmod a) { return a * a; } n   friend intmod bpow(intmod a, int p) { n     intmod ans(1); n     while (p) { n       if (p & 1) ans *= a; n       a = sqr(a); n       p >>= 1; n     } n     return ans; n   } n   friend intmod inv(intmod a) { return bpow(a, MOD - 2); } n   intmod operator/=(intmod rhs) { return (*this) *= inv(rhs); } n   friend intmod operator/(intmod lhs, intmod rhs) { return lhs /= rhs; } n   friend istream &operator>>(istream &is, intmod &im) { return is >> im.val; } n   friend ostream &operator<<(ostream &os, intmod im) { return os << im.val; } n }; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   cout << bpow(intmod(2), n + m) << endl; n   return 0; n } n


#include <bits/stdc++.h> n long long int power(long long int x, long long int n) { n   long long int result = 1; n   while (n) { n     if (n % 2 == 1) result = result * x; n     n = n / 2; n     x = x * x; n   } n   return result; n } n long long int gcd(long long int a, long long int b) { n   if (!b) return a; n   return gcd(b, a % b); n } n long long int lcm(long long int a, long long int b) { n   return (a * b) / gcd(a, b); n } n long long int BS(long long int a[], long long int s, long long int n, n                  long long int val) { n   long long int mid, beg = s, end = n - 1; n   while (beg <= end) { n     mid = (beg + end) / 2; n     if (val == a[mid]) { n       break; n     } else if (val > a[mid]) { n       beg = mid + 1; n     } else { n       end = mid - 1; n     } n   } n   return mid; n } n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, k; n     cin >> n >> k; n     long long int d, ans = 0; n     d = (n / 2) + 1; n     long long int a[n * k]; n     for (long long int i = 0; i < n * k; i++) cin >> a[i]; n     long long int count = k; n     if (n != 2 && n != 1) { n       for (long long int j = n * k - d; j >= 0; j -= d) { n         ans += a[j]; n         count--; n         if (count == 0) break; n       } n       cout << ans <<   n ; n     } else if (n == 2) { n       long long int x = k; n       for (long long int i = 0; i < n * k; i++) { n         ans += a[i]; n         x--; n         i++; n         if (x == 0) break; n       } n       cout << ans <<   n ; n     } else if (n == 1) { n       for (long long int i = 0; i < n * k; i++) ans += a[i]; n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   char with[1005], make[1005]; n   int count1[26], count2[26], ans = 0; n   bool flag = false; n   scanf( %s , with); n   memset(count1, 0, sizeof(count1)); n   memset(count2, 0, sizeof(count2)); n   for (int i = 0; with[i]; i++) { n     count1[with[i] -  a ]++; n   } n   int m; n   scanf( %s , make); n   for (int j = 0; make[j]; j++) { n     count2[make[j] -  a ]++; n   } n   for (int i = 0; i <= 25; i++) { n     if (count1[i] >= count2[i]) n       ans += count2[i]; n     else if (count1[i] < count2[i] && count1[i] > 0) n       ans += count1[i]; n     else n       flag = true; n   } n   if (flag) { n     cout <<  -1  << endl; n   } else n     cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n std::vector<int> a[500005]; n int n, m, col[500005], ans[505][505], f[505][505]; n int read() { n   char ch = getchar(); n   if (ch ==  .  || ch ==  X ) return ch ==  X ; n   return read(); n } n int t(int x, int y) { return (x - 1) * m + y; } n std::pair<int, int> it(int id) { n   return std::make_pair((id - 1) / m + 1, (id - 1) % m + 1); n } n void add(int x, int y) { n   a[x].push_back(y); n   a[y].push_back(x); n } n void dfs(int v) { n   for (int i = 0; i < (int)a[v].size(); i++) { n     int u = a[v][i]; n     if (col[u] != -1) continue; n     col[u] = 1 - col[v]; n     dfs(u); n   } n } n int getans(int x, int y) { n   int s = 0; n   if (!f[x + 1][y]) s = s + ans[x + 1][y]; n   if (!f[x - 1][y]) s = s + ans[x - 1][y]; n   if (!f[x][y + 1]) s = s + ans[x][y + 1]; n   if (!f[x][y - 1]) s = s + ans[x][y - 1]; n   return s; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) f[i][j] = read(); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (!f[i][j]) { n         col[t(i, j)] = -1; n         continue; n       } n       int cnt = 0; n       if (!f[i + 1][j]) ++cnt; n       if (!f[i - 1][j]) ++cnt; n       if (!f[i][j + 1]) ++cnt; n       if (!f[i][j - 1]) ++cnt; n       if (cnt == 1 || cnt == 3) return puts( NO ), 0; n       if (!cnt) continue; n       if (cnt == 2) { n         int p1 = 0, p2 = 0; n         if (!f[i + 1][j]) !p1 ? p1 = t(i + 1, j) : p2 = t(i + 1, j); n         if (!f[i][j + 1]) !p1 ? p1 = t(i, j + 1) : p2 = t(i, j + 1); n         if (!f[i - 1][j]) !p1 ? p1 = t(i - 1, j) : p2 = t(i - 1, j); n         if (!f[i][j - 1]) !p1 ? p1 = t(i, j - 1) : p2 = t(i, j - 1); n         add(p1, p2); n       } else if (cnt == 4) { n         add(t(i + 1, j), t(i, j + 1)); n         add(t(i + 1, j), t(i, j - 1)); n         add(t(i - 1, j), t(i, j + 1)); n         add(t(i - 1, j), t(i, j - 1)); n       } n     } n   for (int i = 1; i <= n * m; i++) n     if (col[i] == -1) col[i] = 0, dfs(i); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (!f[i][j]) ans[i][j] = (col[t(i, j)] ? 1 : 4); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (f[i][j]) ans[i][j] = getans(i, j); n   puts( YES ); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) printf( %d  , ans[i][j]); n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y; n string s; n bool v[505][505]; n bool judge(int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= m); } n int dirx(char x) { n   if (x ==  L  || x ==  R ) return 0; n   if (x ==  U ) return -1; n   return 1; n } n int diry(char x) { n   if (x ==  U  || x ==  D ) return 0; n   if (x ==  L ) return -1; n   return 1; n } n int main() { n   cin >> n >> m >> x >> y; n   cin >> s; n   int len = (int)s.length(); n   int nx, ny; n   v[x][y] = true; n   printf( 1  ); n   for (int i = 0; i < len - 1; ++i) { n     nx = x + dirx(s[i]); n     ny = y + diry(s[i]); n     if (!judge(nx, ny)) { n       printf( 0  ); n       continue; n     } n     x = nx; n     y = ny; n     if (v[x][y]) n       printf( 0  ); n     else { n       v[x][y] = true; n       printf( 1  ); n     } n   } n   int ans = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (!v[i][j]) ans++; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int v[1 << 20], n, L = 1048575; n void R(int i) { n   if (~i) { n     int p; n     for (; v[p = L ^ i] || p > n; L /= 2) n       ; n     v[p] = 1; n     R(i - 1); n     printf( %d  , p); n   } n } n int main() { n   scanf( %d , &n); n   printf( %lld n , 1LL * n * (n + 1)); n   R(n); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int a, b; n     cin >> a >> b; n     if (a == b) n       cout << 0 << endl; n     else if ((a < b && (b - a) % 2 == 1) || ((a > b) && (a - b) % 2 == 0)) { n       cout << 1 << endl; n     } else if ((a < b && (b - a) % 2 == 0) || ((a > b) && (a - b) % 2 == 1)) { n       cout << 2 << endl; n     } 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   unordered_map<char, int> v; n   int cnt = 0; n   int j = 0; n   for (int i1 = 1; i1 <= n; ++i1) { n     char ch; n     string second; n     cin >> ch >> second; n     if (ch ==  ! ) { n       if (cnt == 0) { n         for (int i = 0; i < second.length(); ++i) { n           if (v[second[i]] == 0) { n             v[second[i]] = 1; n           } n         } n         cnt++; n       } else { n         unordered_map<char, int> v1; n         for (int i = 0; i < second.length(); ++i) { n           if (v1[second[i]] == 0) { n             v1[second[i]] = 1; n           } n         } n         for (char ch1 =  a ; ch1 <=  z ; ++ch1) { n           if (v[ch1] == 1 && v1[ch1] == 1) { n             v[ch1] = 1; n           } else { n             v[ch1] = -1; n           } n         } n       } n     } else if (ch ==  . ) { n       if (cnt == 0) { n         for (int i = 0; i < second.length(); ++i) { n           v[second[i]] = -1; n         } n       } else { n         for (int i = 0; i < second.length(); ++i) { n           v[second[i]] = -1; n         } n       } n     } else { n       if (i1 != n) { n         v[second[0]] = -1; n       } n     } n     int sum = 0, sum1 = 0; n     for (char ch1 =  a ; ch1 <=  z ; ++ch1) { n       if (v[ch1] == 1) { n         sum += 1; n       } else if (v[ch1] == -1) { n         sum1--; n       } n     } n     j++; n     if (sum == 1 || sum1 == -25) { n       break; n     } n   } n   int totSum = 0; n   for (int i = j + 1; i <= n; ++i) { n     char ch; n     string second; n     cin >> ch >> second; n     if (ch ==  ! ) { n       totSum++; n     } else if (ch ==  ?  && i != n) { n       totSum++; n     } n   } n   cout << totSum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000; n const int MAXM = 300000; n struct node { n   int key, id, rev; n   int siz1, siz2, s; n   node *fa, *ch[2], *mx; n } tree[MAXN + MAXM + 5], *ad[MAXN + MAXM + 5]; n struct edge { n   int u, v, w, id; n   edge(int _u = 0, int _v = 0, int _w = 0, int _i = 0) n       : u(_u), v(_v), w(_w), id(_i) {} n } e[MAXM + 5]; n bool operator<(edge a, edge b) { n   if (a.w == b.w) return a.id < b.id; n   return a.w < b.w; n } n set<edge> Set; n set<edge>::iterator it; n node *NIL, *ncnt; n void Init() { n   NIL = ncnt = &tree[0]; n   NIL->fa = NIL->ch[0] = NIL->ch[1] = NIL->mx = NIL; n   NIL->key = -1; n } n bool IsRoot(node *x) { n   return (x->fa == NIL) || (x->fa->ch[0] != x && x->fa->ch[1] != x); n } n void SetChild(node *x, node *y, int d) { n   x->ch[d] = y; n   if (y != NIL) y->fa = x; n } n node *NewNode(int k, int id, int s) { n   ncnt++; n   ncnt->key = k, ncnt->id = id, ncnt->s = ncnt->siz1 = s; n   ncnt->fa = ncnt->ch[0] = ncnt->ch[1] = NIL; n   ncnt->mx = ncnt; n   return ncnt; n } n void PushDown(node *x) { n   if (x->rev) { n     swap(x->ch[0], x->ch[1]); n     if (x->ch[0] != NIL) x->ch[0]->rev ^= 1; n     if (x->ch[1] != NIL) x->ch[1]->rev ^= 1; n     x->rev = 0; n   } n } n void PushUp(node *x) { n   if (x->key > x->ch[0]->mx->key && x->key > x->ch[1]->mx->key) n     x->mx = x; n   else if (x->ch[0]->mx->key > x->ch[1]->mx->key) n     x->mx = x->ch[0]->mx; n   else n     x->mx = x->ch[1]->mx; n   x->siz1 = x->s + x->ch[0]->siz1 + x->ch[1]->siz1 + x->siz2; n } n void Rotate(node *x) { n   node *y = x->fa; n   PushDown(y), PushDown(x); n   int d = (y->ch[1] == x); n   if (IsRoot(y)) n     x->fa = y->fa; n   else n     SetChild(y->fa, x, y->fa->ch[1] == y); n   SetChild(y, x->ch[!d], d); n   SetChild(x, y, !d); n   PushUp(y); n } n void Splay(node *x) { n   PushDown(x); n   while (!IsRoot(x)) { n     node *y = x->fa; n     if (IsRoot(y)) n       Rotate(x); n     else { n       if ((y->fa->ch[1] == y) == (y->ch[1] == x)) n         Rotate(y); n       else n         Rotate(x); n       Rotate(x); n     } n   } n   PushUp(x); n } n void Access(node *x) { n   node *y = NIL; n   while (x != NIL) { n     Splay(x); n     x->siz2 += x->ch[1]->siz1; n     x->ch[1] = y; n     x->siz2 -= x->ch[1]->siz1; n     PushUp(x); n     y = x, x = x->fa; n   } n } n void MakeRoot(node *x) { n   Access(x), Splay(x); n   x->rev ^= 1; n } n void Link(node *x, node *y) { n   MakeRoot(x); n   MakeRoot(y); n   x->fa = y; n   y->siz2 += x->siz1; n } n void Cut(node *x, node *y) { n   MakeRoot(x); n   Access(y), Splay(y); n   y->ch[0] = x->fa = NIL; n   PushUp(y); n } n node *FindRoot(node *x) { n   Access(x), Splay(x); n   node *ret = x; n   while (ret->ch[0] != NIL) ret = ret->ch[0]; n   PushUp(ret); n   return ret; n } n node *LCA(node *x, node *y) { n   Access(x), Splay(x); n   Splay(y); n   while (y->fa != NIL) { n     y = y->fa; n     Splay(y); n   } n   return y; n } n node *QueryMAX(node *x, node *y) { n   node *z = LCA(x, y); n   node *ret = z; n   Access(x), Splay(z); n   if (z->ch[1]->mx->key > ret->key) ret = z->ch[1]->mx; n   Access(y), Splay(z); n   if (z->ch[1]->mx->key > ret->key) ret = z->ch[1]->mx; n   return ret; n } n int n, m, stot; n void Debug() { n   for (it = Set.begin(); it != Set.end(); it++) n     printf( (%d, %d, %d) n , it->u, it->v, it->w); n } n int main() { n   Init(); n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) ad[i] = NewNode(-1, -1, 1); n   stot = n; n   for (int i = 1; i <= m; i++) { n     e[i].id = i; n     scanf( %d%d%d , &e[i].u, &e[i].v, &e[i].w); n     if (e[i].u == e[i].v) continue; n     ad[n + i] = NewNode(e[i].w, i, 0); n     Set.insert(e[i]); n     if (FindRoot(ad[e[i].u]) == FindRoot(ad[e[i].v])) { n       node *p = QueryMAX(ad[e[i].u], ad[e[i].v]); n       Splay(p); n       if (p->key > e[i].w) { n         Set.erase(e[p->id]); n         Cut(p, ad[e[p->id].u]), Cut(p, ad[e[p->id].v]); n         Link(ad[e[i].u], ad[n + i]), Link(ad[e[i].v], ad[n + i]); n       } else n         Set.erase(e[i]); n     } else { n       MakeRoot(ad[e[i].u]), MakeRoot(ad[e[i].v]); n       if (ad[e[i].u]->siz1 % 2 == 1 && ad[e[i].v]->siz1 % 2 == 1) stot -= 2; n       Link(ad[e[i].u], ad[n + i]), Link(ad[e[i].v], ad[n + i]); n     } n     if (stot == 0) { n       it = Set.end(); n       it--; n       while (true) { n         node *p = ad[n + it->id], *q = ad[e[it->id].u], *r = ad[e[it->id].v]; n         MakeRoot(p); n         Access(q); n         Access(r); n         if (q->siz1 & 1) break; n         Access(q); n         if (r->siz1 & 1) break; n         Set.erase(it); n         it = Set.end(); n         it--; n       } n       it = Set.end(); n       it--; n       printf( %d n , it->w); n     } else n       printf( -1 n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long ar[1000010]; n int main() { n   long long n; n   cin >> n; n   long long ans = 0; n   if (n % 2 == 1) n     puts( 0 ); n   else { n     n = n / 2; n     n = (n - 1) / 2; n     cout << n << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n int n, m, k, par[maxn], ans[maxn]; n bool arr[maxn]; n pair<int, pair<int, int>> e[maxn]; n int get_path(int v) { return par[v] < 0 ? v : (par[v] = get_path(par[v])); } n void merge(int v, int u) { n   if ((v = get_path(v)) == (u = get_path(u))) return; n   if (par[v] < par[u]) swap(v, u); n   par[u] += par[v]; n   par[v] = u; n   arr[u] = arr[u] || arr[v]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> m >> k; n   for (int i = 0; i < k; i++) { n     int x; n     cin >> x; n     arr[x] = 1; n   } n   for (int i = 0; i < m; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     e[i] = {w, {u, v}}; n   } n   sort(e, e + m); n   int ind; n   memset(par, -1, sizeof(par)); n   for (int i = 0; i < m; i++) { n     int u = e[i].second.first, v = e[i].second.second; n     if ((u = get_path(u)) != (v = get_path(v)) && arr[u] && arr[v]) { n       ind = i; n     } n     merge(u, v); n   } n   for (int i = 0; i < k; i++) { n     cout << e[ind].first <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a, b; n int t; n int main() { n   cin >> t; n   while (t--) { n     cin >> a >> b; n     cout << a + b <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void Yahia74() {} n const int N = 1e5 + 74, M = 1e5 + 74, OO = 0x3f3f3f3f; n int main() { n   Yahia74(); n   int n, cnt[N] = {}; n   cin >> n; n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     if (i < n / 2) { n       if (x - i >= 0) cnt[x - i]++; n     } else { n       if (x - (n - i - 1) >= 0) cnt[x - (n - i - 1)]++; n     } n   } n   int mx = 0; n   for (int i = 1; i < 100001; i++) mx = max(mx, cnt[i]); n   cout << n - mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n template <typename Sep, typename Head, typename... Tail> n void fold_print(Sep sep, Head&& h, Tail&&... args) { n   ((std::cerr << h) << ... << (std::cerr << sep, args)); n } n std::mt19937_64 myrand( n     std::chrono::high_resolution_clock::now().time_since_epoch().count()); n using namespace std; n using LL = long long int; n void init(); n void process(); n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int T = 1; n   init(); n   for (int t = 1; t <= T; t++) { n     process(); n   } n   cout.flush(); n   return 0; n } n constexpr LL MOD = 998244353; n vector<LL> fac(5000 * 2 + 1), inv(5000 * 2 + 1); n LL fastpw(LL x, LL p) { n   if (p == 0) return 1; n   LL tmp = fastpw(x * x % MOD, p / 2); n   if (p % 2 == 0) n     return tmp; n   else n     return tmp * x % MOD; n } n LL modinv(LL x) { return fastpw(x, MOD - 2); } n void init() { n   fac[0] = 1; n   for (LL i = 1; i < fac.size(); i++) { n     fac[i] = i * fac[i - 1] % MOD; n   } n   for (size_t i = 0; i < inv.size(); i++) { n     inv[i] = modinv(fac[i]); n   } n } n LL C(LL n, LL m) { return fac[n] * inv[m] % MOD * inv[n - m] % MOD; } n void process() { n   int n, k; n   cin >> n >> k; n   vector<int> arr(n); n   { n     for (auto& x : arr) { n       char ch; n       cin >> ch; n       x = ch -  0 ; n     } n   } n   auto permute = [&](LL l, LL r, LL one) { n     LL len = r - l + 1; n     LL cnt[] = {len - one, one}; n     if (len == 1 || cnt[0] == len || cnt[1] == len) return 0ll; n     cnt[1 - arr[l]] -= 1; n     return C(len - 1, cnt[0]); n   }; n   LL ans = 1, r = 0, cnt = arr[0]; n   for (LL i = 0; i < n; cnt -= arr[i], i += 1) { n     while (r + 1 < n && cnt + arr[r + 1] <= k) { n       cnt += arr[r + 1], r += 1; n     } n     if (cnt != k) break; n     if (r == n - 1) { n       for (int j = i; j < n; cnt -= arr[j], j += 1) { n         ans = (ans + permute(j, r, cnt)) % MOD; n       } n       break; n     } else { n       ans = (ans + permute(i, r, cnt)) % MOD; n     } n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long int fact[200005]; n long long int n; n struct point { n   long long int x, y; n }; n bool cmp(struct point i1, struct point i2) { n   if (i1.x != i2.x) n     return i1.x < i2.x; n   else { n     return (i1.y < i2.y); n   } n } n long long int fact_(long long int n) { n   long long int ans = 1; n   fact[0] = fact[1] = 1; n   for (int i = 2; i <= n; i++) { n     ans = (ans * i) % 1000000007; n     fact[i] = ans % 1000000007; n   } n   return ans; n } n long long int mod_inv(long long int a, long long int b) { n   long long int ans = 1; n   while (b) { n     if (b % 2 == 1) ans = (ans * a) % 1000000007; n     a = (a * a) % 1000000007; n     b = b >> 1; n   } n   return ans; n } n long long int ncr(long long int n, long long int r) { n   if (n < r) return 0; n   if (r == 0) return n; n   if (r == 1) return n; n   if (n == r) return 1; n   return ((((fact[n] % 1000000007) * n             mod_inv(fact[r] % 1000000007, 1000000007 - 2)) % n            1000000007) * n           mod_inv(fact[n - r] % 1000000007, 1000000007 - 2)) % n          1000000007; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   vector<struct point> ar1(4), ar2(4); n   for (int i = 0; i < 4; i++) { n     long long int a, b; n     cin >> a >> b; n     ar1[i].x = a; n     ar1[i].y = b; n   } n   for (int i = 0; i < 4; i++) { n     long long int a, b; n     cin >> a >> b; n     ar2[i].x = a; n     ar2[i].y = b; n   } n   sort(ar1.begin(), ar1.end(), cmp); n   sort(ar2.begin(), ar2.end(), cmp); n   bool f = 0; n   for (int i = 0; i < 4; i++) { n     if (ar2[i].x >= ar1[0].x && ar2[i].y >= ar1[0].y && ar2[i].x <= ar1[3].x && n         ar2[i].y <= ar1[3].y) n       f = 1; n   } n   for (int i = 0; i < 4; i++) { n     if ((ar1[i].x + ar1[i].y) >= (ar2[1].x + ar2[1].y) && n         (ar1[i].y - ar1[i].x) >= (ar2[1].y - ar2[1].x) && n         (ar1[i].x + ar1[i].y) <= (ar2[2].x + ar2[2].y) && n         (ar1[i].y - ar1[i].x) <= (ar2[2].y - ar2[2].x)) n       f = 1; n   } n   point c1, c2; n   c1.x = (ar1[0].x + ar1[1].x) / 2; n   c1.y = (ar1[0].y + ar1[3].y) / 2; n   c2.x = (ar2[0].x + ar2[1].x) / 2; n   c2.y = (ar2[0].y + ar2[3].y) / 2; n   if (c2.x >= ar1[0].x && c2.y >= ar1[0].y && c2.x <= ar1[3].x && n       c2.y <= ar1[3].y) n     f = 1; n   if ((c1.x + c1.y) >= (ar2[1].x + ar2[1].y) && n       (c1.y - c1.x) >= (ar2[1].y - ar2[1].x) && n       (c1.x + c1.y) <= (ar2[2].x + ar2[2].y) && n       (c1.y - c1.x) <= (ar2[2].y - ar2[2].x)) n     f = 1; n   if (!f) n     cout <<  NO  << endl; n   else { n     cout <<  YES  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   int N, M; n   cin >> N >> M; n   using Interval = pair<int, int>; n   vector<Interval> intervals(N); n   for (int i = 0; i < N; i++) { n     cin >> intervals[i].first >> intervals[i].second; n   } n   int i = 0; n   int current_time = 1; n   for (int m = 0; m < M; m++) { n     int moment; n     cin >> moment; n     while (i < N && moment > current_time + n                                  intervals[i].first * intervals[i].second - 1) { n       current_time += intervals[i].first * intervals[i].second; n       i++; n     } n     assert(i < N); n     cout << i + 1 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   long long p; n   int d; n } store[200005]; n bool cmp(node& a, node& b) { return a.d < b.d; } n bool check(int e, int full, int n) { n   if (store[0].d > full) return 1; n   for (int i = 0; i < (n - 1); ++i) n     if (store[i + 1].d - store[i].d > full) return 1; n   if (e - store[n - 1].d > full) return 1; n   return 0; n } n int main(void) { n   int e, full, n; n   scanf( %d %d %d , &e, &full, &n); n   for (int i = 0; i < (n); ++i) scanf( %d %lld , &store[i].d, &store[i].p); n   sort(store, store + n, cmp); n   if (check(e, full, n)) { n     puts( -1 ); n     return 0; n   } n   long long ret = 0; n   int cur = full - store[0].d; n   for (int i = 0; i < (n); ++i) { n     int d = store[i + 1].d - store[i].d; n     int s = i + 1, minn = 1e7, indexx; n     bool flag = 0; n     while (d <= full && s < n) { n       if (store[s].p <= store[i].p) { n         if (d <= cur) { n           cur -= d; n         } else { n           ret += (d - cur) * store[i].p; n           cur = 0; n         } n         i = s - 1; n         flag = 1; n         break; n       } n       if (store[s].p <= minn) { n         minn = store[i].p; n         indexx = s; n       } n       s++; n       d = store[s].d - store[i].d; n     } n     if (!flag) { n       if (store[i].d + full >= e) { n         ret += store[i].p * max(0, e - store[i].d - cur); n         break; n       } n       ret += (full - cur) * store[i].p; n       cur = full - (store[indexx].d - store[i].d); n       i = indexx - 1; n     } n   } n   printf( %lld , ret); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int getint() { n   static char c; n   while ((c = getchar()) <  0  || c >  9 ) n     ; n   int res = c -  0 ; n   while ((c = getchar()) >=  0  && c <=  9 ) res = res * 10 + c -  0 ; n   return res; n } n const int MaxN = 200000; n int n; n int col[MaxN + 1]; n struct state { n   int x, l; n   state() {} n   state(const int &_x, const int &_l) : x(_x), l(_l) {} n   friend inline bool operator<(const state &lhs, const state &rhs) { n     if (lhs.x != rhs.x) return lhs.x < rhs.x; n     return lhs.l < rhs.l; n   } n }; n state lower[MaxN + 1], upper[MaxN + 1]; n int used[MaxN + 1]; n int main() { n   n = getint(); n   for (int i = 1; i <= n; ++i) col[i] = getint(); n   if (col[1] > 1) { n     printf( -1 n ); n     return 0; n   } n   lower[1] = state(1, 1); n   upper[1] = state(1, 1); n   for (int i = 2; i <= n; ++i) { n     lower[i] = lower[i - 1]; n     if (++lower[i].l > 5) ++lower[i].x, lower[i].l = 1; n     upper[i] = upper[i - 1]; n     if (++upper[i].l > 2) ++upper[i].x, upper[i].l = 1; n     if (col[i] > 0) { n       state l(col[i], 1); n       state r(col[i], 5); n       lower[i] = max(lower[i], l); n       upper[i] = min(upper[i], r); n       if (lower[i].x > col[i] || upper[i].x < col[i]) { n         printf( -1 n ); n         return 0; n       } n     } n   } n   state res = upper[n]; n   if (res.l == 1) res.l = 5, --res.x; n   if (res < lower[n]) { n     printf( -1 n ); n     return 0; n   } n   used[col[n] = res.x] = 1; n   for (int i = n - 1; i >= 1; --i) { n     col[i] = min(col[i + 1], upper[i].x); n     col[i] -= used[col[i]] == 5; n     ++used[col[i]]; n   } n   printf( %d n , res.x); n   for (int i = 1; i <= n; ++i) printf( %d  , col[i]); n   return 0; n } n