#include <bits/stdc++.h> n #pragma clang diagnostic push n #pragma clang diagnostic ignored  -Wunused-const-variable  n using namespace std; n template <typename T> n class Modular { n  public: n   using Type = typename decay<decltype(T::value)>::type; n   constexpr Modular() : value() {} n   template <typename U> n   Modular(const U& x) { n     value = normalize(x); n   } n   static Type inverse(Type a, Type mod) { n     Type b = mod, x = 0, y = 1; n     while (a != 0) { n       Type t = b / a; n       b -= a * t; n       x -= t * y; n       swap(a, b); n       swap(x, y); n     } n     if (x < 0) x += mod; n     return x; n   } n   template <typename U> n   static Type normalize(const U& x) { n     Type v; n     if (-mod() <= x && x < mod()) n       v = static_cast<Type>(x); n     else n       v = static_cast<Type>(x % mod()); n     if (v < 0) v += mod(); n     return v; n   } n   const Type& operator()() const { return value; } n   template <typename U> n   explicit operator U() const { n     return static_cast<U>(value); n   } n   constexpr static Type mod() { return T::value; } n   Modular& operator+=(const Modular& other) { n     if ((value += other.value) >= mod()) value -= mod(); n     return *this; n   } n   Modular& operator-=(const Modular& other) { n     if ((value -= other.value) < 0) value += mod(); n     return *this; n   } n   template <typename U> n   Modular& operator+=(const U& other) { n     return *this += Modular(other); n   } n   template <typename U> n   Modular& operator-=(const U& other) { n     return *this -= Modular(other); n   } n   Modular& operator++() { return *this += 1; } n   Modular& operator--() { return *this -= 1; } n   Modular operator++(int) { n     Modular result(*this); n     *this += 1; n     return result; n   } n   Modular operator--(int) { n     Modular result(*this); n     *this -= 1; n     return result; n   } n   Modular operator-() const { return Modular(-value); } n   template <typename U = T> n   typename enable_if<is_same<typename Modular<U>::Type, int>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     value = normalize(static_cast<int64_t>(value) * n                       static_cast<int64_t>(rhs.value)); n     return *this; n   } n   template <typename U = T> n   typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     int64_t q = static_cast<int64_t>(static_cast<long double>(value) * n                                      rhs.value / mod()); n     value = normalize(value * rhs.value - q * mod()); n     return *this; n   } n   template <typename U = T> n   typename enable_if<!is_integral<typename Modular<U>::Type>::value, n                      Modular>::type& n   operator*=(const Modular& rhs) { n     value = normalize(value * rhs.value); n     return *this; n   } n   Modular& operator/=(const Modular& other) { n     return *this *= Modular(inverse(other.value, mod())); n   } n   template <typename U> n   friend const Modular<U>& abs(const Modular<U>& v) { n     return v; n   } n   template <typename U> n   friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs); n   template <typename U> n   friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs); n   template <typename U> n   friend std::istream& operator>>(std::istream& stream, Modular<U>& number); n  n  private: n   Type value; n }; n template <typename T> n bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { n   return lhs.value == rhs.value; n } n template <typename T, typename U> n bool operator==(const Modular<T>& lhs, U rhs) { n   return lhs == Modular<T>(rhs); n } n template <typename T, typename U> n bool operator==(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) == rhs; n } n template <typename T> n bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { n   return !(lhs == rhs); n } n template <typename T, typename U> n bool operator!=(const Modular<T>& lhs, U rhs) { n   return !(lhs == rhs); n } n template <typename T, typename U> n bool operator!=(U lhs, const Modular<T>& rhs) { n   return !(lhs == rhs); n } n template <typename T> n bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { n   return lhs.value < rhs.value; n } n template <typename T> n Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T, typename U> n Modular<T> operator+(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T, typename U> n Modular<T> operator+(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) += rhs; n } n template <typename T> n Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T, typename U> n Modular<T> operator-(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T, typename U> n Modular<T> operator-(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) -= rhs; n } n template <typename T> n Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T, typename U> n Modular<T> operator*(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T, typename U> n Modular<T> operator*(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) *= rhs; n } n template <typename T> n Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> operator/(const Modular<T>& lhs, U rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> operator/(U lhs, const Modular<T>& rhs) { n   return Modular<T>(lhs) /= rhs; n } n template <typename T, typename U> n Modular<T> power(const Modular<T>& a, const U& b) { n   assert(b >= 0); n   Modular<T> x = a, res = 1; n   U p = b; n   while (p > 0) { n     if (p & 1) res *= x; n     x *= x; n     p >>= 1; n   } n   return res; n } n template <typename T> n bool IsZero(const Modular<T>& number) { n   return number() == 0; n } n template <typename T> n string to_string(const Modular<T>& number) { n   return to_string(number()); n } n template <typename T> n std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) { n   return stream << number(); n } n template <typename T> n std::istream& operator>>(std::istream& stream, Modular<T>& number) { n   typename common_type<typename Modular<T>::Type, int64_t>::type x; n   stream >> x; n   number.value = Modular<T>::normalize(x); n   return stream; n } n const int md = 1e9 + 7; n using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>; n long long sqr(long long x) { return x * x; } n int mysqrt(long long x) { n   int l = 0, r = 1e9 + 1; n   while (r - l > 1) { n     int m = (l + r) / 2; n     if (m * (long long)m <= x) n       l = m; n     else n       r = m; n   } n   return l; n } n mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); n mt19937_64 rndll( n     chrono::high_resolution_clock::now().time_since_epoch().count()); n template <typename T> n T gcd(T a, T b) { n   return a ? gcd(b % a, a) : b; n } n int gcdex(int a, int b, int& x, int& y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   int x1, y1; n   int ret = gcdex(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return ret; n } n void setmin(int& x, int y) { x = min(x, y); } n void setmax(int& x, int y) { x = max(x, y); } n void setmin(long long& x, long long y) { x = min(x, y); } n void setmax(long long& x, long long y) { x = max(x, y); } n const long long llinf = 4e18 + 100; n const long double eps = 1e-9, PI = atan2(0, -1); n const int maxn = 2e5 + 100, maxw = 1e4 + 100, inf = 1e9 + 100, sq = 450, n           LG = 18, mod = 1e9 + 933; n int tp[maxn], h[maxn], n; n vector<int> e[maxn]; n long long q[maxn][2]; n void dfs(int v, int par) { n   int X = 0, Y = 0; n   vector<long long> g; n   long long cur = 0; n   for (int i : e[v]) n     if (i != par) { n       dfs(i, v); n       if (h[i] == h[v]) { n         g.push_back(q[i][1] - q[i][0]); n         cur += q[i][0]; n         X++; n       } else if (h[i] < h[v]) { n         X++; n         cur += q[i][0]; n       } else { n         Y++; n         cur += q[i][1]; n       } n     } n   sort(g.begin(), g.end()); n   q[v][0] = q[v][1] = llinf; n   auto upd = [&](int t, int x, int y) { n     setmin(q[v][t], max(x, y) * (long long)tp[v] + cur); n   }; n   for (int i = 0; i <= g.size(); i++) { n     if (par == -1) n       upd(0, X, Y); n     else { n       upd(0, X, Y + 1); n       upd(1, X + 1, Y); n     } n     if (i < g.size()) { n       cur += g[i]; n       X--; n       Y++; n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> tp[i]; n   for (int i = 0; i < n; i++) cin >> h[i]; n   for (int i = 1; i < n; i++) { n     int v, u; n     cin >> v >> u; n     v--; n     u--; n     e[v].push_back(u); n     e[u].push_back(v); n   } n   dfs(0, -1); n   cout << q[0][0] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int sum[501000 << 2], sum2[501000 << 2]; n void pushUp(int rt) { sum[rt] = max(sum[rt * 2], sum[rt * 2 + 1]); } n void updata(int l, int r, int rt, int q, int val) { n   if (l == q && r == q) { n     sum[rt] = val; n     return; n   } n   int m = (l + r) / 2; n   if (q <= m) updata(l, m, rt * 2, q, val); n   if (q > m) updata(m + 1, r, rt * 2 + 1, q, val); n   pushUp(rt); n } n int query(int l, int r, int rt, int ql, int qr) { n   if (l > qr || r < ql) return 0; n   if (l >= ql && r <= qr) return sum[rt]; n   int m = (l + r) / 2; n   return max(query(l, m, rt * 2, ql, qr), query(m + 1, r, rt * 2 + 1, ql, qr)); n } n void build(int l, int r, int rt) { n   if (l == r) { n     sum[rt] = 0; n     return; n   } n   int m = (l + r) / 2; n   build(l, m, rt * 2); n   build(m + 1, r, rt * 2 + 1); n   pushUp(rt); n } n void pushUp2(int rt) { sum2[rt] = min(sum2[rt * 2], sum2[rt * 2 + 1]); } n void updata2(int l, int r, int rt, int q, int val) { n   if (l == q && r == q) { n     sum2[rt] = val; n     return; n   } n   int m = (l + r) / 2; n   if (q <= m) updata2(l, m, rt * 2, q, val); n   if (q > m) updata2(m + 1, r, rt * 2 + 1, q, val); n   pushUp2(rt); n } n int query2(int l, int r, int rt, int ql, int qr) { n   if (l > qr || r < ql) return 0; n   if (l >= ql && r <= qr) return sum2[rt]; n   int m = (l + r) / 2; n   return min(query2(l, m, rt * 2, ql, qr), n              query2(m + 1, r, rt * 2 + 1, ql, qr)); n } n void build2(int l, int r, int rt) { n   if (l == r) { n     sum2[rt] = 0; n     return; n   } n   int m = (l + r) / 2; n   build2(l, m, rt * 2); n   build2(m + 1, r, rt * 2 + 1); n   pushUp2(rt); n } n struct edge { n   int to; n   int next; n } e[501000 << 1]; n int box[501000], cnt, tot; n int siz[501000], top[501000], son[501000], dep[501000], fa[501000], w[501000]; n int we[501000]; n int ww = 0; n void init() { n   tot = 0; n   son[0] = dep[0] = 0; n   memset(box, -1, sizeof(box)); n   cnt = 0; n } n void add(int from, int to) { n   e[cnt].to = to; n   e[cnt].next = box[from]; n   box[from] = cnt++; n } n void dfs(int now, int pre) { n   siz[now] = 1; n   fa[now] = pre; n   son[now] = 0; n   dep[now] = dep[pre] + 1; n   int t, v; n   for (t = box[now]; t + 1; t = e[t].next) { n     v = e[t].to; n     if (v != pre) { n       dfs(v, now); n       siz[now] += siz[v]; n       if (siz[son[now]] < siz[v]) { n         son[now] = v; n       } n     } n   } n } n void dfs2(int now, int tp) { n   w[now] = ++ww; n   top[now] = tp; n   if (son[now]) dfs2(son[now], top[now]); n   int t, v; n   for (t = box[now]; t + 1; t = e[t].next) { n     v = e[t].to; n     if (v != fa[now] && v != son[now]) dfs2(v, v); n   } n   we[now] = ww; n } n int n, m; n int solve(int a, int b) { n   int ma = 0; n   while (top[a] != top[b]) { n     if (dep[top[a]] < dep[top[b]]) swap(a, b); n     ma = max(ma, query(1, n, 1, w[top[a]], w[a])); n     a = fa[top[a]]; n   } n   if (dep[a] > dep[b]) swap(a, b); n   ma = max(ma, query(1, n, 1, w[a], w[b])); n   return ma; n } n int main() { n   scanf( %d , &n); n   init(); n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     add(x, y); n     add(y, x); n   } n   build(1, n, 1); n   build2(1, n, 1); n   dfs(1, 0); n   dfs2(1, 1); n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     int q, x; n     scanf( %d%d , &q, &x); n     if (q == 1) { n       updata(1, n, 1, w[x], i); n     } else if (q == 2) { n       updata2(1, n, 1, w[x], -1 * i); n     } else { n       int zs = query2(1, n, 1, w[x], we[x]); n       int zx = solve(1, x); n       if (zx + zs > 0) { n         printf( 1 n ); n       } else n         printf( 0 n ); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, n, k, s = 0, m; n   vector<pair<int, int> > v(110); n   scanf( %d , &n); n   scanf( %d , &k); n   for (i = 0; i < n; ++i) { n     scanf( %d , &v[i].first); n     v[i].second = i + 1; n   } n   sort(v.begin(), v.begin() + n); n   for (i = 0; s <= k && i < n; ++i) s = s + v[i].first; n   m = i; n   if (s > k) --m; n   printf( %d n , m); n   for (i = 0; i < m; ++i) printf( %d  , v[i].second); n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int v = 0, f = 1; n   char c = getchar(); n   while (c < 48 || 57 < c) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (48 <= c && c <= 57) v = (v << 3) + v + v + c - 48, c = getchar(); n   return v * f; n } n const int N = 5e5 + 100, INF = 2e9; n int val[N], a[N], b[N], n, m; n set<pair<int, int> > S; n struct node { n   node *ch[2], *fa; n   int sum, a, maxID, id; n   bool rev; n   node() { n     ch[0] = ch[1] = fa = this; n     sum = a = maxID = id = rev = 0; n   } n   node(int _a, int _id); n }; n node *null = new node; n node *pool[N]; n node ::node(int _a, int _id) { n   ch[0] = ch[1] = fa = null; n   sum = _a; n   a = _a; n   maxID = _id; n   id = _id; n   rev = 0; n } n bool isRoot(node *u) { return (u->fa->ch[0] != u) && (u->fa->ch[1] != u); } n bool cmp(int x, int y) { return val[x] < val[y]; } n void up(node *u) { n   u->sum = u->ch[0]->sum + u->ch[1]->sum + u->a; n   int t[3] = {u->ch[0]->maxID, u->ch[1]->maxID, u->id}; n   sort(t, t + 3, cmp); n   u->maxID = t[2]; n } n void putRev(node *u) { n   if (u == null) return; n   swap(u->ch[0], u->ch[1]); n   u->rev ^= 1; n } n void dn(node *u) { n   if (u->rev) { n     putRev(u->ch[0]); n     putRev(u->ch[1]); n     u->rev = 0; n   } n } n void rotate(node *u) { n   node *v = u->fa; n   bool w = u == v->ch[0]; n   if (!isRoot(v)) n     if (v->fa->ch[0] == v) n       v->fa->ch[0] = u; n     else n       v->fa->ch[1] = u; n   u->fa = v->fa; n   v->ch[!w] = u->ch[w]; n   u->ch[w]->fa = v; n   u->ch[w] = v; n   v->fa = u; n   up(v); n   up(u); n } n void splay(node *u) { n   static node *stk[N]; n   int top = 1; n   stk[1] = u; n   for (node *i = u; !isRoot(i); i = i->fa) stk[++top] = i->fa; n   for (int i = top; i >= 1; i--) dn(stk[i]); n   for (node *i; i = u->fa, !isRoot(u); rotate(u)) n     if (!isRoot(i)) rotate(u == i->ch[0] ^ i == i->fa->ch[0] ? u : i); n } n void access(node *u) { n   for (node *t = null; u != null; t = u, u = u->fa) { n     splay(u); n     u->a -= t->sum; n     u->a += u->ch[1]->sum; n     u->ch[1] = t; n     up(u); n   } n } n void makeRoot(node *u) { n   access(u); n   splay(u); n   putRev(u); n } n void link(node *u, node *v) { n   makeRoot(v); n   access(u); n   splay(u); n   u->a += v->sum; n   u->sum += v->sum; n   v->fa = u; n } n void cut(node *u, node *v) { n   makeRoot(u); n   access(v); n   splay(v); n   v->ch[0] = null; n   up(v); n   u->fa = null; n } n node *getRoot(node *u) { n   access(u); n   splay(u); n   while (u->ch[0] != null) u = u->ch[0]; n   return u; n } n int querySize(node *u) { n   access(u); n   splay(u); n   return u->sum; n } n bool queryOdd(int u) { n   cut(pool[n + u], pool[a[u]]); n   bool re = querySize(pool[a[u]]) & 1; n   link(pool[n + u], pool[a[u]]); n   return re; n } n int main() { n   n = read(), m = read(); n   if (n & 1) { n     while (m--) puts( -1 ); n     return 0; n   } n   for (int i = 1; i <= n; i++) pool[i] = new node(1, 0); n   int num = n; n   for (int i = 1; i <= m; i++) { n     a[i] = read(), b[i] = read(), val[i] = read(); n     node *u = pool[a[i]], *v = pool[b[i]]; n     if (getRoot(u) != getRoot(v)) { n       int sizU = querySize(u), sizV = querySize(v); n       pool[n + i] = new node(0, i); n       link(pool[n + i], u); n       link(pool[n + i], v); n       S.insert(make_pair(-val[i], i)); n       if ((sizU & 1) && (sizV & 1)) num -= 2; n     } else { n       makeRoot(u); n       access(v); n       splay(v); n       int t = v->maxID; n       if (val[i] < val[t]) { n         pool[n + i] = new node(0, i); n         S.erase(make_pair(-val[t], t)); n         cut(pool[n + t], pool[a[t]]); n         cut(pool[n + t], pool[b[t]]); n         S.insert(make_pair(-val[i], i)); n         link(pool[n + i], pool[a[i]]); n         link(pool[n + i], pool[b[i]]); n       } n     } n     if (num) n       puts( -1 ); n     else { n       while (1) { n         int t = (*S.begin()).second; n         if (!queryOdd(t)) { n           cut(pool[n + t], pool[a[t]]); n           cut(pool[n + t], pool[b[t]]); n           S.erase(S.begin()); n         } else { n           printf( %d n , val[t]); n           break; n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s[110]; n int n, d; n int m; n int main() { n   cin >> n >> d; n   cin >> m; n   for (int i = 0; i < m; ++i) { n     int x, y; n     cin >> x >> y; n     bool t = false; n     if (!((x - y) <= d && (x - y) >= -d)) t = true; n     if (!((x + y) <= n + n - d && (x + y) >= d)) t = true; n     if (t == true) n       s[i] =  NO ; n     else n       s[i] =  YES ; n   } n   for (int i = 0; i < m; i++) cout << s[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int a = 1 << 18; n using namespace std; n long long odp[a], n, k, res; n vector<int> v[a]; n void dfs(int x, int pop) { n   for (int i = 0; i < v[x].size(); i++) { n     if (v[x][i] != pop) n       dfs(v[x][i], x), odp[x] += odp[v[x][i]], n           res += min(odp[v[x][i]], k - odp[v[x][i]]); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cin >> n >> k; n   k *= 2; n   for (int i = 0; i < k; i++) { n     int b; n     cin >> b; n     odp[b] = 1; n   } n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   dfs(1, 1); n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n int b[8]; n string str; n int main() { n   int n, i; n   cin >> n >> str; n   for (i = 0; i <= (n - 1); i++) switch (str[i]) { n       case  2 : n         b[2]++; n         break; n       case  3 : n         b[3]++; n         break; n       case  4 : n         b[2] += 2, b[3]++; n         break; n       case  5 : n         b[5]++; n         break; n       case  6 : n         b[5]++, b[3]++; n         break; n       case  7 : n         b[7]++; n         break; n       case  8 : n         b[7]++, b[2] += 3; n         break; n       case  9 : { n         b[7]++; n         b[2]++; n         b[3] += 2; n       } n     } n   while (b[7]--) putchar( 7 ); n   while (b[5]--) putchar( 5 ); n   while (b[3]--) putchar( 3 ); n   while (b[2]--) putchar( 2 ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n #pragma GCC optimize( O2 ) n using namespace std; n const int maxn = 1e4 + 123; n int n, k; n int a[maxn]; n vector<int> ve[4]; n long long sum; n int main(int argc, char const *argv[]) { n   std::cin >> n; n   for (int i = 0; i < n; i++) { n     char s[3]; n     int val; n     std::cin >> s >> val; n     int id = 2 * (s[0] -  0 ) + (s[1] -  0 ); n     ve[id].push_back(val); n   } n   if (ve[1].size() < ve[2].size()) { n     swap(ve[1], ve[2]); n   } n   for (int i = 0; i <= 3; i++) { n     sort(ve[i].begin(), ve[i].end()); n   } n   long long cur = (long long)ve[3].size(); n   while (ve[3].size() > 0) { n     sum += ve[3][(long long)ve[3].size() - 1]; n     ve[3].pop_back(); n   } n   while (ve[2].size() > 0) { n     sum += ve[2][ve[2].size() - 1] + ve[1][(long long)ve[1].size() - 1]; n     ve[1].pop_back(); n     ve[2].pop_back(); n   } n   for (int i = 0; i < (int)ve[1].size(); i++) { n     ve[0].push_back(ve[1][i]); n   } n   sort(ve[0].begin(), ve[0].end()); n   while (cur > 0 && ve[0].size() > 0) { n     sum += ve[0][ve[0].size() - 1]; n     ve[0].pop_back(); n     cur--; n   } n   std::cout << sum <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; n const long long INF = 0x3f3f3f3f3f3f3f3f; n __inline__ __attribute__((always_inline)) unsigned int rnd() { n   static unsigned int seed = 416; n   return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13; n } n template <typename _Tp> n _Tp gcd(const _Tp &a, const _Tp &b) { n   return (!b) ? a : gcd(b, a % b); n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) { n   return a >= 0 ? a : -a; n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b) { n   return a < b ? b : a; n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b) { n   return a < b ? a : b; n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) { n   (a < b) && (a = b); n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) { n   (b < a) && (a = b); n } n template <typename _Tp> n __inline__ __attribute__((always_inline)) void read(_Tp &x) { n   char ch(getchar()); n   bool f(false); n   while (ch < 48 || ch > 57) f |= ch == 45, ch = getchar(); n   x = ch & 15, ch = getchar(); n   while (ch >= 48 && ch <= 57) n     x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); n   if (f) x = -x; n } n template <typename _Tp, typename... Args> n __inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) { n   read(t); n   read(args...); n } n template <typename _Tp, typename... Args> n __inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b, n                                                   const Args &...args) { n   return a < b ? min(a, args...) : min(b, args...); n } n template <typename _Tp, typename... Args> n __inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b, n                                                   const Args &...args) { n   return a < b ? max(b, args...) : max(a, args...); n } n __inline__ __attribute__((always_inline)) int read_str(char *s) { n   char ch(getchar()); n   while (ch ==     || ch ==   r  || ch ==   n ) ch = getchar(); n   char *tar = s; n   *tar = ch, ch = getchar(); n   while (ch !=     && ch !=   r  && ch !=   n  && ch != EOF) n     *(++tar) = ch, ch = getchar(); n   return tar - s + 1; n } n const int N = 4000005; n const int mod = 998244353; n template <typename _Tp1, typename _Tp2> n __inline__ __attribute__((always_inline)) void add(_Tp1 &a, _Tp2 b) { n   (a += b) >= mod && (a -= mod); n } n template <typename _Tp1, typename _Tp2> n __inline__ __attribute__((always_inline)) void sub(_Tp1 &a, _Tp2 b) { n   (a -= b) < 0 && (a += mod); n } n long long ksm(long long a, long long b = mod - 2) { n   long long res = 1; n   while (b) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod, b >>= 1; n   } n   return res; n } n long long fac[N], inv[N], ifac[N]; n void init() { n   fac[0] = fac[1] = inv[0] = inv[1] = ifac[0] = ifac[1] = 1; n   for (int i = 2; i < N; ++i) { n     fac[i] = fac[i - 1] * i % mod; n     inv[i] = inv[mod % i] * (mod - mod / i) % mod; n     ifac[i] = ifac[i - 1] * inv[i] % mod; n   } n } n __inline__ __attribute__((always_inline)) long long C(int n, int m) { n   if (n < m) return 0; n   return fac[n] * ifac[m] % mod * ifac[n - m] % mod; n } n long long f[N]; n long long s1[N], s2[N], s3[N]; n int main() { n   init(); n   long long n, m; n   read(n, m); n   for (int i = 0; i <= n; ++i) { n     if (i) s1[i] = s1[i - 1]; n     add(s1[i], fac[m + i - 1] * ifac[i] % mod); n     if (i) s2[i] = s2[i - 1], add(s2[i], fac[m + i - 1] * ifac[i - 1] % mod); n     long long _ = m * fac[i] % mod * fac[n - i] % mod * ifac[n + m] % mod; n     long long tmp = _ * C(n + m + 1, m + i + 1) % mod * n                     (s1[i] * (i + 1) % mod - s2[i] + mod) % mod; n     long long coef = n         (mod + 1 - n          _ * C(n + m, m + i) % mod * ifac[i] % mod * fac[m + i - 1] % mod); n     f[i] = (tmp + _ * C(n + m, m + i) % mod * s3[i] % mod) * ksm(coef) % mod; n     s3[i + 1] = s3[i], n            add(s3[i + 1], fac[m + i - 1] * ifac[i] % mod * f[i] % mod); n   } n   printf( %lld n , f[n]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n vector<pair<pair<int, int>, pair<int, int> > > a[300005]; n vector<int> g1[300005]; n vector<int> g2[300005]; n string Max =   , Min =   ; n int ansMax = 0; n int ansMin = 0; n bool used[300005]; n void getMax(int cur) { n   ansMax++; n   used[cur]++; n   for (auto it : g1[cur]) { n     if (!used[it]) { n       getMax(it); n     } n   } n } n void getMin(int cur) { n   ansMin++; n   used[cur]++; n   for (auto it : g2[cur]) { n     if (!used[it]) { n       getMin(it); n     } n   } n } n void updateMax(int s) { n   queue<int> q; n   q.push(s); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     if (used[u]) { n       continue; n     } n     used[u]++; n     for (auto it : a[u]) { n       if (it.first.first == 1) { n         if (!used[it.second.second]) { n           q.push(it.second.second); n           g1[u].push_back(it.second.second); n         } n       } else { n         if (u == it.second.first) { n           if (!used[it.second.second]) { n             q.push(it.second.second); n             g1[u].push_back(it.second.second); n           } n         } else { n           if (!used[it.second.first]) { n             q.push(it.second.first); n             g1[u].push_back(it.second.first); n             Max[it.first.second] =  - ; n           } n         } n       } n     } n   } n } n void updateMin(int s) { n   queue<int> q; n   q.push(s); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     if (used[u]) { n       continue; n     } n     used[u]++; n     for (auto it : a[u]) { n       if (it.first.first == 2) { n         if (u == it.second.first) { n           if (!used[it.second.second]) { n             Min[it.first.second] =  - ; n           } n         } n       } n     } n     for (auto it : a[u]) { n       if (it.first.first == 1 && !used[it.second.second]) { n         g2[u].push_back(it.second.second); n         q.push(it.second.second); n       } n     } n   } n } n int main() { n   int n, m, s; n   scanf( %d%d%d , &n, &m, &s); n   int i, t, u, v; n   int d = 0; n   for (i = 0; i < m; i++) { n     scanf( %d%d%d , &t, &u, &v); n     if (t == 1) { n       a[u].push_back({{t, i}, {u, v}}); n     } else { n       a[u].push_back({{t, d}, {u, v}}); n       a[v].push_back({{t, d}, {u, v}}); n       d++; n       Max +=  + ; n       Min +=  + ; n     } n   } n   updateMax(s); n   memset(used, 0, sizeof used); n   getMax(s); n   printf( %d n , ansMax); n   cout << Max << endl; n   memset(used, 0, sizeof used); n   updateMin(s); n   memset(used, 0, sizeof used); n   getMin(s); n   printf( %d n , ansMin); n   cout << Min; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 110000; n int t[4 * maxN], m; n int get(int k) { n   int res = 0; n   for (int i = k; i >= 0; i = (i & (i + 1)) - 1) { n     res = max(res, t[i]); n   } n   return res; n } n void update(int k, int delta) { n   for (int i = k; i <= m; i |= (i + 1)) { n     t[i] = max(t[i], delta); n   } n } n int calculate(vector<pair<int, int> > borders) { n   pair<int, int> start = borders[0]; n   sort(borders.rbegin(), borders.rend()); n   for (int i = 0; i < borders.size(); ++i) { n     int current = get(borders[i].second - 1); n     update(borders[i].second, current + 1); n     if (borders[i] == start) { n       return current; n     } n   } n } n int a, b, c, d, n; n struct rational { n   long long x, y; n   rational(long long x, long long y) : x(x), y(y) {} n   rational() : x(0), y(1) {} n   bool operator<(const rational &other) const { n     return x * other.y < other.x * y; n   } n }; n vector<pair<int, int> > compress(vector<rational> a, vector<rational> b) { n   set<rational> S; n   for (int i = 0; i < a.size(); ++i) { n     S.insert(a[i]); n     S.insert(b[i]); n   } n   map<rational, int> M; n   m = 0; n   for (set<rational>::iterator it = S.begin(); it != S.end(); ++it) { n     M[*it] = ++m; n   } n   vector<pair<int, int> > borders; n   for (int i = 0; i < a.size(); ++i) { n     borders.push_back(make_pair(M[a[i]], M[b[i]])); n   } n   return borders; n } n int main() { n   scanf( %d , &n); n   scanf( %d/%d%d/%d , &a, &b, &c, &d); n   vector<rational> u, v; n   for (int i = 0; i <= n; ++i) { n     long long x, y; n     if (i == 0) { n       x = y = 0; n     } else { n       cin >> x >> y; n     } n     u.push_back(rational(1000000LL * a + y * b - x * a, a + b)); n     v.push_back(rational(1000000LL * c + y * d - x * c, c + d)); n   } n   printf( %d n , calculate(compress(u, v))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int x, y; n   y = scanf( %d , &x); n   return x; n } n const int N = 300003; n int n, m, k; n vector<int> g[N], gr[N]; n int to[N], from[N], mark[N], res[N], din[N], rem; n int x[N], r[N], y[N]; n void out() { n   cout <<  -1 n ; n   exit(0); n } n void dfs(int v) { n   mark[v] = 1; n   for (int u : g[v]) n     if (mark[u] == 1) n       out(); n     else { n       if (!mark[u]) dfs(u); n       from[v] = max(from[v], from[u]); n     } n   from[v]++; n   if (r[v] > 0) { n     if (from[v] > r[v]) out(); n     from[v] = r[v]; n   } n   if (from[v] > k) out(); n   mark[v] = 2; n } n bool seen[N]; n set<pair<int, int>> ss, qs; n void updateRem() { n   while (rem > 0 && seen[rem]) { n     rem--; n     while (qs.size() && qs.rbegin()->first >= rem) { n       int v = qs.rbegin()->second; n       qs.erase({to[v], v}); n       ss.insert({from[v], v}); n     } n   } n } n void addSource(int v) { n   int x = to[v]; n   if (x >= rem) n     ss.insert({from[v], v}); n   else n     qs.insert({x, v}); n } n int main() { n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) r[i] = in(); n   for (int i = 0; i < m; i++) { n     x[i] = in() - 1; n     y[i] = in() - 1; n     g[x[i]].push_back(y[i]); n     gr[y[i]].push_back(x[i]); n     din[y[i]]++; n   } n   for (int i = 0; i < n; i++) n     if (!mark[i]) dfs(i); n   rem = k; n   fill(to, to + n, k); n   for (int i = 0; i < n; i++) { n     if (r[i] > 0) to[i] = min(to[i], r[i]); n     if (!din[i]) addSource(i); n   } n   for (int i = 0; i < n; i++) { n     if (ss.empty()) out(); n     int v = ss.rbegin()->second; n     ss.erase({from[v], v}); n     assert(to[v] >= rem); n     if (r[v] > 0) n       res[v] = r[v]; n     else n       res[v] = max(rem, from[v]); n     seen[res[v]] = true; n     updateRem(); n     for (int u : g[v]) { n       din[u]--; n       to[u] = min(to[u], res[v] - 1); n       if (to[u] < from[u]) out(); n       if (din[u] == 0) addSource(u); n     } n   } n   if (rem != 0) out(); n   for (int i = 0; i < m; i++) n     if (res[x[i]] <= res[y[i]]) out(); n   for (int i = 0; i < n; i++) cout << res[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 4e5 + 5; n pair<long long, long long> arr[N]; n long long l[N]; n long long s[N]; n long long dp[N]; n long long bit[N]; n long long n; n void update(long long idx, long long val) { n   while (idx <= n) { n     bit[idx] = max(bit[idx], val); n     idx += idx & -idx; n   } n } n long long query(long long idx) { n   long long res = 0; n   while (idx) { n     res = max(res, bit[idx]); n     idx -= idx & -idx; n   } n   return res; n } n int main() { n   scanf( %lld , &n); n   for (long long i = 1; i <= n; i++) { n     long long r, h; n     scanf( %lld%lld , &r, &h); n     arr[i] = pair<long long, long long>(r * r * h, -i); n   } n   sort(arr + 1, arr + n + 1); n   for (long long i = 1; i <= n; i++) { n     dp[i] = max(arr[i].first, query(-arr[i].second) + arr[i].first); n     update(-arr[i].second, dp[i]); n   } n   printf( %.7lf n , 3.14159265358979323846 * query(n)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt[100001]; n int main() { n   int n, k; n   cin >> n; n   int ans = 1; n   int m = n; n   while (n--) { n     cin >> k; n     cnt[k] = cnt[k - 1] + 1; n     ans = max(ans, cnt[k]); n   } n   cout << m - ans; n } n


#include <bits/stdc++.h> n char year[10]; n int main() { n   scanf( %s , year); n   int l = strlen(year) - 1; n   int num = 0, num2 = 0; n   for (int i = 0; i <= l; i++) { n     num = num * 10 + year[i] -  0 ; n   } n   for (int i = 1; i <= l; i++) num2 = num2 * 10 + year[i] -  0 ; n   int x = 1; n   for (int i = 1; i <= l; i++) x *= 10; n   printf( %d n , x - num2); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<tuple<int, int, int, int> > v(n); n   vector<int> used(n, 0); n   for (int i = 0; i < n; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     v[i] = make_tuple(a, b, c, i); n   } n   sort(v.begin(), v.end()); n   map<pair<int, int>, vector<int> > m; n   for (int i = 0; i < n; i++) { n     pair<int, int> t = {get<0>(v[i]), get<1>(v[i])}; n     if (m.find(t) == m.end()) m[t] = vector<int>(); n     m[t].push_back(i); n   } n   for (auto t : m) { n     for (int i = 0; i + 1 < t.second.size(); i += 2) { n       cout << get<3>(v[t.second[i]]) + 1 <<     n            << get<3>(v[t.second[i + 1]]) + 1 <<   n ; n       used[get<3>(v[t.second[i]])] = 1; n       used[get<3>(v[t.second[i + 1]])] = 1; n     } n   } n   vector<tuple<int, int, int, int> > v1; n   for (int i = 0; i < v.size(); i++) { n     if (used[get<3>(v[i])] == 0) v1.push_back(v[i]); n   } n   map<int, vector<int> > m1; n   for (int i = 0; i < v1.size(); i++) { n     if (m1.find(get<0>(v1[i])) == m1.end()) { n       m1[get<0>(v1[i])] = vector<int>(); n     } n     m1[get<0>(v1[i])].push_back(i); n   } n   for (auto t : m1) { n     for (int i = 0; i + 1 < t.second.size(); i += 2) { n       cout << get<3>(v1[t.second[i]]) + 1 <<     n            << get<3>(v1[t.second[i + 1]]) + 1 <<   n ; n       used[get<3>(v1[t.second[i]])] = 1; n       used[get<3>(v1[t.second[i + 1]])] = 1; n     } n   } n   vector<tuple<int, int, int, int> > v2; n   for (int i = 0; i < v1.size(); i++) { n     if (used[get<3>(v1[i])] == 0) { n       v2.push_back(v1[i]); n     } n   } n   for (int i = 0; i < v2.size(); i += 2) { n     cout << get<3>(v2[i]) + 1 <<     << get<3>(v2[i + 1]) + 1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int a = 26; n char c[10000], ans[10000]; n int p[10000], ap[10000], perm[10000]; n map<int, int> m; n int main() { n   string s, st; n   cin >> s; n   int n = s.size(), x, t, last, y; n   x = 17576; n   for (int i = 0; i < 3; i++) { n     x /= a; n     t = 0; n     last = -1; n     cout <<  ?  ; n     for (int j = 0; j < n; j++) { n       if (j % x == 0 || last != c[j]) { n         t = (t + 1) % a; n       } n       last = c[j]; n       c[j] = char(t +  a ); n       cout << c[j]; n     } n     cout << endl; n     cin >> st; n     for (int j = 0; j < n; j++) { n       y = st[j] -  a ; n       p[j] = p[j] * a + y; n       ap[j] = ap[j] * a + (c[j] -  a ); n     } n   } n   cout <<  !  ; n   for (int i = 0; i < n; i++) m[ap[i]] = i; n   for (int i = 0; i < n; i++) perm[i] = m[p[i]]; n   for (int i = 0; i < n; i++) ans[perm[i]] = s[i]; n   for (int i = 0; i < n; i++) cout << ans[i]; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int lim = 500000001; n class TMain { n  private: n   int n, m, f[505][505], dis[505][505], ans[505][505], now[505]; n  n  public: n   int run() { n     scanf( %d %d , &n, &m); n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         if (i == j) n           f[i][j] = dis[i][j] = 0; n         else n           f[i][j] = dis[i][j] = lim; n     for (int i = 1; i <= m; i++) { n       int a, b, c; n       scanf( %d %d %d , &a, &b, &c); n       dis[a][b] = dis[b][a] = f[a][b] = f[b][a] = min(f[a][b], c); n     } n     for (int k = 1; k <= n; k++) n       for (int i = 1; i <= n; i++) n         for (int j = 1; j <= n; j++) f[i][j] = min(f[i][j], f[i][k] + f[k][j]); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) now[j] = 0; n       for (int j = 1; j <= n; j++) n         for (int k = 1; k <= n; k++) n           if (j != k && f[i][j] + dis[j][k] == f[i][k]) now[k]++; n       for (int j = 1; j <= n; j++) n         for (int k = 1; k <= n; k++) n           if (f[i][j] + f[j][k] == f[i][k]) ans[i][k] += now[j]; n     } n     for (int i = 1; i < n; i++) n       for (int j = i + 1; j <= n; j++) n         if (f[i][j] == lim) n           printf( 0  ); n         else n           printf( %d  , ans[i][j]); n     printf(  n ); n     return 0; n   } n } Main; n int main() { return Main.run(); } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long k, n, s, p; n   cin >> k >> n >> s >> p; n   long long w = n / s; n   if (n % s != 0) w++; n   w *= k; n   long long ans = w / p; n   if (w % p != 0) ans++; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)5e5 + 50; n int n; n int ans[N]; n vector<int> G[N]; n int q[N]; n int dep[N]; n vector<pair<int, int> > hist[N]; n void upd(int u, int val) { q[u] = max(q[u], val); } n void calc(int v, int p) { n   dep[v] = 0; n   for (int nxt : G[v]) { n     if (nxt == p) continue; n     calc(nxt, v); n     dep[v] = max(dep[nxt] + 1, dep[v]); n   } n } n void merge(vector<pair<int, int> > h1, vector<pair<int, int> > h2) { n   int r = 0; n   for (int i = 0; i < h1.size(); i++) { n     while (r < h2.size() && (h2[r].first >= h1[i].first)) { n       r++; n     } n     if (r - 1 >= 0 && h2[r - 1].second > 1) n       upd(2 * h1[i].first, h2[r - 1].second + h1[i].second - 2); n   } n } n void dfs(int v, int p = -1, int up_dep = -1) { n   vector<int> deps; n   if (up_dep != -1) deps.push_back(up_dep + 1); n   for (int nxt : G[v]) { n     if (nxt != p) deps.push_back(dep[nxt] + 1); n   } n   sort(deps.begin(), deps.end(), n        [](const int a, const int b) { return a > b; }); n   for (int i = 0; i < deps.size(); i++) { n     upd(2 * deps[i], i + 1); n     upd(2 * deps[i] - 1, i + 1); n     if (i > 0 && deps[i - 1] > deps[i]) upd(2 * deps[i] + 1, i + 1); n   } n   for (int i = 0; i < deps.size(); i++) { n     if (hist[v].empty() || hist[v].back().first != deps[i]) n       hist[v].push_back({deps[i], 1}); n     else n       hist[v].back().second++; n   } n   for (int i = 1; i < hist[v].size(); i++) n     hist[v][i].second += hist[v][i - 1].second; n   for (int nxt : G[v]) { n     if (nxt == p) continue; n     dfs(nxt, v, n         (deps.size() == 1 ? 0 : (deps[0] == dep[nxt] + 1 ? deps[1] : deps[0]))); n     merge(hist[nxt], hist[v]); n     merge(hist[v], hist[nxt]); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--, b--; n     G[a].push_back(b); n     G[b].push_back(a); n   } n   for (int i = 0; i < n; i++) { n     ans[1] = max(ans[1], (int)G[i].size() + 1); n   } n   calc(0, -1); n   dfs(0); n   int rmax = 0; n   for (int i = (n - 1) / 2 * 2 + 1; i >= 2; i -= 2) { n     rmax = max(rmax, q[i]); n     ans[i] = rmax; n   } n   rmax = 0; n   for (int i = n / 2 * 2; i >= 2; i -= 2) { n     rmax = max(rmax, q[i]); n     ans[i] = rmax; n   } n   for (int i = 1; i <= n; i++) cout << max(ans[i], 1) <<    ; n } n


#include <cstdio> n #include <iostream> n  n #define ll long long n  n using namespace std; n  n int n , m , cnt = 0; n  n const int N = 5e5 + 5; n const int mod = 1e9 + 7; n  n ll pow2[N]; n  n int num[N] , fa[N]; n  n int find(int x){ n  if(fa[x] == x) n   return x; n  return fa[x] = find(fa[x]); n } n  n int main(){ n  pow2[0] = 1; n  cin >> n >> m; n  for(int i = 1; i < N; i++) n   pow2[i] = 1ll * 2 * pow2[i - 1] % mod; n  for(int i = 1; i <= m + 1; i++) n   fa[i] = i; n  for(int i = 1; i <= n; i++){ n   int k; n   int x , y; n   scanf( %d  , &k); n   if(k == 1){ n    scanf( %d  , &x); n    y = m + 1; n   } n   else n    scanf( %d%d  , &x , &y); n   int fx , fy; n   fx = find(x); n   fy = find(y); n   if(fx == fy) n    continue; n   fa[fx] = fy; n   num[++cnt] = i; n  } n  ll ans = pow2[cnt] % mod; n  cout << ans <<     << cnt << endl; n  for(int i = 1; i <= cnt; i++) n   printf( %d   , num[i]); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n unordered_map<long long, int> mpSt; n unordered_map<long long, int> mpEnd; n set<long long> AllPoints; n long long K[200005]; n int main() { n   int N; n   scanf( %d , &N); n   for (int i = 0; i < N; i++) { n     long long st, end; n     scanf(  %lld %lld , &st, &end); n     AllPoints.insert(st); n     AllPoints.insert(end); n     if (mpSt.count(st) > 0) n       mpSt[st]++; n     else n       mpSt[st] = 1; n     if (mpEnd.count(end) > 0) n       mpEnd[end]++; n     else n       mpEnd[end] = 1; n   } n   if (N == 1) { n     if (AllPoints.size() == 1) { n       printf( 1 ); n     } else { n       long long seg = *next(AllPoints.begin()) - *AllPoints.begin() + 1; n       printf( %lld , seg); n     } n     return 0; n   } n   auto nextIt = next(AllPoints.begin()); n   long long cumulative = 0; n   for (auto it = AllPoints.begin(); nextIt != AllPoints.end(); it++) { n     long long cur = *it; n     long long nxt = *nextIt; n     cumulative += mpSt[cur]; n     K[cumulative] += 1; n     cumulative -= mpEnd[cur]; n     long long middle = nxt - cur - 1; n     K[cumulative] += middle; n     nextIt = next(nextIt); n   } n   cumulative += mpSt[*prev(AllPoints.end())]; n   K[cumulative]++; n   for (int i = 1; i <= N; i++) { n     printf( %lld  , K[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, temp, count, i, len, len1, j; n   char c, a[200], b[40]; n   strcpy(b,  qwertyuiopasdfghjkl;zxcvbnm,./ ); n   while (~scanf( %c , &c)) { n     getchar(); n     gets(a); n     len = strlen(a); n     len1 = strlen(b); n     if (c ==  R ) n       temp = -1; n     else n       temp = 1; n     for (i = 0; i < len; i++) { n       for (j = 0; j < len1; j++) { n         if (a[i] == b[j]) printf( %c , b[j + temp]); n       } n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n int n, i, j, l, h, k[5500], f[5500], t[5500]; n int main() { n   scanf( %d n , &n); n   for (i = 1; i <= n; i++) { n     scanf( %d , &h); n     k[i] = k[i - 1] + h; n     j = i - 1; n     while (t[j] > k[i] - k[j]) j--; n     f[i] = f[j] + i - 1 - j; n     t[i] = k[i] - k[j]; n   } n   printf( %d , f[n]); n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j, x = 0; n   char c[30][30] = {0}; n   scanf( %d n , &n); n   for (i = 1; i <= n; i++) { n     char temp[605] =   ; n     cin.getline(temp, 605); n     int length = strlen(temp); n     for (j = 0; j < length; j++) { n       c[0][temp[j] - 96] = 1; n       if (j != length - 1) c[temp[j] - 96][temp[j + 1] - 96] = 1; n     } n   } n   for (i = 0; i <= 26; i++) { n     for (j = 1; j <= 26; j++) { n       if (c[i][j] == 0 && i == 0) { n         printf( %c n , j + 96); n         x = 1; n       } else if (c[i][j] == 0) { n         printf( %c%c n , i + 96, j + 96); n         x = 1; n       } n       if (x == 1) return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t; n   t = 1; n   while (t--) { n     double n; n     cin >> n; n     double a[200001], sum = 0; n     set<double> s; n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n       sum += a[i]; n     } n     for (int i = 0; i < n; i++) { n       double z = sum - a[i]; n       double y = (z / (n - 1)); n       if (y == a[i]) s.insert(i + 1); n     } n     cout << s.size() << endl; n     for (auto i : s) cout << i <<    ; n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n namespace myland { n using namespace std; n namespace _abbr {} n using namespace _abbr; n namespace _constant { n const double EPS(1e-8); n const double PI(acos(-1.0)); n const int INF(0x3f3f3f3f); n const long long INFL(0x3f3f3f3f3f3f3f3fll); n const int MOD(1e9 + 7); n const int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1}, n           dy[] = {0, 1, 0, -1, 1, 1, -1, -1}; n }  // namespace _constant n using namespace _constant; n namespace _solve {} n using namespace _solve; n namespace _calculate { n bool odd(long long x) { return x & 1; } n bool even(long long x) { return (x & 1) ^ 1; } n bool posi(long long x) { return x > 0; } n bool nega(long long x) { return x < 0; } n bool zero(long long x) { return x == 0; } n bool prime(long long x) { n   if (x < 2) return 0; n   for (int i = 2; i * i <= x; i++) n     if (x % i == 0) return 0; n   return 1; n } n long long droot(long long x) { return 1 + (x - 1) % 9; } n long long upd(long long a, long long b) { return a % b ? a / b + 1 : a / b; }; n long long random(long long a, long long b) { n   return a + rand() * rand() % (b - a + 1); n }; n long long bitn(long long x) { n   long long c = 0; n   while (x) c++, x >>= 1; n   return c; n } n template <class T> n T sqr(T x) { n   return x * x; n } n long long qpow(long long a, long long n, long long mod = MOD) { n   long long res(1); n   while (n) { n     if (n & 1) (res *= a) %= mod; n     (a *= a) %= mod; n     n >>= 1; n   } n   return res % mod; n } n long long inv(long long a, long long mod = MOD) { return qpow(a, mod - 2); } n template <class T> n void tomin(T& a, T b) { n   if (b < a) a = b; n } n template <class T> n void tomax(T& a, T b) { n   if (b > a) a = b; n } n }  // namespace _calculate n using namespace _calculate; n namespace _simple_algo { n long long sol(const string& s) { n   long long x = 0; n   for (char c : s) x = x * 10 + c - 48; n   return x; n } n string los(long long x) { n   string s =   ; n   if (x == 0) return  0 ; n   while (x) s = char(x % 10 + 48) + s, x /= 10; n   return s; n } n bool pal(const string& s) { n   int l = s.size(); n   for (int i = 0, j = l - 1; i < j; i++, j--) n     if (s[i] != s[j]) return 0; n   return 1; n } n }  // namespace _simple_algo n using namespace _simple_algo; n namespace _io { n template <class T> n void rd(T& x) { n   cin >> x; n } n string srd() { n   string s; n   cin >> s; n   return s; n } n long long rd() { 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 void rd(int& x) { x = rd(); } n void rd(long long& x) { x = rd(); } n template <class A, class B> n void rd(pair<A, B>& p) { n   cin >> p.first >> p.second; n } n template <class A, class B> n void rd(A& a, B& b) { n   rd(a), rd(b); n } n template <class A, class B, class C> n void rd(A& a, B& b, C& c) { n   rd(a, b); n   rd(c); n } n template <class T> n void wt(const T& x) { n   cout << x << endl; n } n template <class T> n void wt(const T& x, char c) { n   cout << x << c; n } n template <class T> n void wt(const T& x, const string& s) { n   cout << x << s; n } n template <class T> n void wt(const T& x, int rnd) { n   cout << fixed << setprecision(rnd) << x << endl; n } n template <class T> n void wt(const vector<T>& v) { n   for (T x : v) wt(x,    ); n   wt(  ); n } n template <class A, class B> n void wt(const pair<A, B>& make_pair) { n   cout << make_pair.first <<     << make_pair.second << endl; n } n }  // namespace _io n using namespace _io; n }  // namespace myland n using namespace myland; n int n(rd()), m(rd()), r(rd()); n int s[50], b[50]; n int main() { n   for (int i = 1; i <= n; i++) rd(s[i]); n   for (int i = 1; i <= m; i++) rd(b[i]); n   sort((s + 1), (s + n + 1)); n   sort((b + 1), (b + m + 1)); n   int t = r / s[1]; n   if (s[1] < b[m]) { n     r -= t * s[1]; n     r += t * b[m]; n   } n   wt(r); n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-6; n const int N = 101; n const int INF = 0x3f3f3f3f; n const int mod = 1000000007; n long long powmod(long long a, long long b) { n   long long res = 1; n   a %= mod; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % mod; n     a = a * a % mod; n   } n   return res; n } n int a[N], maxf, ans; n int d[N]; n bool mul; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int k, f; n     cin >> k >> f; n     a[k] = f; n     maxf = max(maxf, f); n   } n   int now = 1; n   for (; now <= 100; now++) { n     int line = 1; n     bool ok = 1; n     for (int i = 1; i <= 100; i++) { n       if (a[i] && a[i] != line) { n         ok = 0; n         break; n       } n       if (i == n && ans && d[n] != line) { n         mul = 1; n       } n       if (i % now == 0) { n         line++; n       } n     } n     if (mul && ok) { n       ans = -1; n       break; n     } n     if (ok) { n       line = 1; n       for (int i = 1; i <= 100; i++) { n         d[i] = line; n         if (i % now == 0) { n           line++; n         } n       } n       ans = d[n]; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxl = 60, w = 10000; n struct Big { n   int s[maxl], l; n   Big() { n     memset(s, 0, sizeof(s)); n     l = 1; n   } n   inline void fix() { n     for (int i = 1; i < l; i++) { n       if (s[i] >= w) { n         s[i + 1] += s[i] / w; n         s[i] %= w; n       } n     } n     while (s[l] >= w) { n       s[l + 1] += s[l] / w; n       s[l] %= w; n       l++; n     } n   } n   void fuzhi(int x) { n     s[1] = x; n     l = 1; n     fix(); n   } n   inline void out() { n     if (s[l] >= 1000) cout << s[l] / 1000 % 10; n     if (s[l] >= 100) cout << s[l] / 100 % 10; n     if (s[l] >= 10) cout << s[l] / 10 % 10; n     cout << s[l] % 10; n     for (int i = l - 1; i >= 1; i--) { n       if (s[i] < 1000) cout << 0; n       if (s[i] < 100) cout << 0; n       if (s[i] < 10) cout << 0; n       cout << s[i]; n     } n     cout << endl; n   } n }; n inline Big operator*(const Big &x, const Big &y) { n   Big ans; n   ans.l = x.l + y.l - 1; n   for (int i = 1; i <= x.l; i++) { n     for (int j = 1; j <= y.l; j++) ans.s[i + j - 1] += x.s[i] * y.s[j]; n   } n   ans.fix(); n   return ans; n } n inline bool operator<(const Big &x, const Big &y) { n   if (x.l != y.l) return x.l < y.l; n   for (int i = x.l; i >= 1; i--) n     if (x.s[i] != y.s[i]) return x.s[i] < y.s[i]; n   return false; n } n inline Big tran(long long x) { n   Big ans; n   ans.fuzhi(x); n   return ans; n } n const int maxn = 770; n struct E { n   int from, to, next; n } e[maxn * 2]; n int te = 1, n, point[maxn]; n void add(int x, int y) { n   te++; n   e[te].from = x; n   e[te].to = y; n   e[te].next = point[x]; n   point[x] = te; n } n int size[maxn]; n Big f[maxn][maxn]; n void dfs(int p, int fa) { n   size[p] = 1; n   for (int i = 0; i <= n; i++) f[p][i].fuzhi(1); n   for (int i = point[p]; i; i = e[i].next) { n     int to = e[i].to; n     if (to == fa) continue; n     dfs(to, p); n     for (int j = size[p]; j >= 0; j--) n       for (int k = size[to]; k >= 0; k--) n         f[p][j + k] = max(f[p][j + k], f[p][j] * f[to][k]); n     size[p] += size[to]; n   } n   for (int i = 1; i <= size[p]; i++) f[p][0] = max(f[p][0], f[p][i] * tran(i)); n } n void ceshi() { n   Big a, b; n   int x, y; n   cin >> x >> y; n   a.fuzhi(x); n   b.fuzhi(y); n   a.out(); n   b.out(); n   a = a * b; n   a.out(); n   exit(0); n } n int main() { n   std::ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     add(x, y); n     add(y, x); n   } n   dfs(1, 0); n   f[1][0].out(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n template <typename T> n inline void read(T &AKNOI) { n   T x = 0, flag = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) flag = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   AKNOI = flag * x; n } n template <typename T> n inline void cmax(T &x, T y) { n   if (x < y) x = y; n } n int n, m, lb[N][N], rb[N][N], dp[N][N]; n void init() { n   read(n); n   read(m); n   for (int i = 1; i <= n; ++i) { n     int k; n     read(k); n     while (k--) { n       int l, r; n       read(l); n       read(r); n       for (int j = l; j <= r; ++j) { n         lb[i][j] = l; n         rb[i][j] = r; n       } n     } n   } n } n void solve() { n   for (int l = 1; l <= m; ++l) { n     for (int i = 1, j = i + l - 1; j <= m; ++i, ++j) { n       for (int k = i; k <= j; ++k) { n         int cnt = 0; n         for (int p = 1; p <= n; ++p) { n           cnt += (i <= lb[p][k] && rb[p][k] <= j); n         } n         cmax(dp[i][j], dp[i][k - 1] + cnt * cnt + dp[k + 1][j]); n       } n     } n   } n   printf( %d n , dp[1][m]); n } n int main() { n   init(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e5 + 123; n const long long INF = (long long)1e18 + 123; n const int inf = (int)1e9 + 123; n const int MOD = (int)1e9 + 7; n int n; n int main() { n   cin >> n; n   if (n % 2 == 0) { n     cout <<  white n ; n     cout <<  1 2 ; n   } else n     cout <<  black ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[300000 + 10], ans = 2e9; n vector<int> lj[300000 + 10]; n multiset<int> s; n int main() { n   int i; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) scanf( %d , &a[i]), s.insert(a[i]); n   for (i = 1; i < n; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     lj[u].push_back(v); n     lj[v].push_back(u); n   } n   for (i = 1; i <= n; ++i) { n     int da = a[i]; n     s.erase(s.find(a[i])); n     for (int j : lj[i]) da = max(da, a[j] + 1), s.erase(s.find(a[j])); n     if (!s.empty()) da = max(da, *s.rbegin() + 2); n     s.insert(a[i]); n     for (int j : lj[i]) s.insert(a[j]); n     ans = min(da, ans); n   } n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   cin >> n; n   cout << (n - 2) * (n - 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   cin >> n; n   for (int i = 1; n; i++) { n     if (i & 1) n       printf( %d 2 n , i), n--; n     else if (n == 1) n       printf( %d 1 n , i), n = 0; n     else n       printf( %d 1 n%d 3 n , i, i), n -= 2; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e4 + 42; n int n, m, k; n int d[55][maxn], dp[55][maxn], s[55][maxn]; n int val[maxn << 2], tag[maxn << 2]; n int g; n void build(int day, int l, int r, int rt) { n   if (l == r) { n     val[rt] = dp[day][l] + s[day + 1][l + k - 1] - s[day + 1][l - 1]; n     return; n   } n   int mid = l + r >> 1; n   build(day, l, mid, rt << 1); n   build(day, mid + 1, r, rt << 1 | 1); n   val[rt] = max(val[rt << 1], val[rt << 1 | 1]); n } n void pushd(int rt) { n   if (!tag[rt]) return; n   val[rt << 1] += tag[rt]; n   val[rt << 1 | 1] += tag[rt]; n   tag[rt << 1] += tag[rt]; n   tag[rt << 1 | 1] += tag[rt]; n   tag[rt] = 0; n } n void upd(int a, int b, int l, int r, int rt, int v) { n   if (a <= l && r <= b) { n     val[rt] += v, tag[rt] += v; n     return; n   } n   pushd(rt); n   int mid = l + r >> 1; n   if (a <= mid) upd(a, b, l, mid, rt << 1, v); n   if (b > mid) upd(a, b, mid + 1, r, rt << 1 | 1, v); n   val[rt] = max(val[rt << 1], val[rt << 1 | 1]); n } n void upd(int day, int pos) { n   if (pos) upd(max(1, pos - k + 1), min(g, pos), 1, g, 1, -d[day][pos]); n } n void del(int day, int pos) { n   if (pos) upd(max(1, pos - k + 1), min(g, pos), 1, g, 1, d[day][pos]); n } n signed main() { n   ios ::sync_with_stdio(false); n   cin.tie(nullptr), cout.tie(nullptr); n   cin >> n >> m >> k; n   g = m - k + 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       cin >> d[i][j]; n       s[i][j] = s[i][j - 1] + d[i][j]; n     } n   for (int i = 1; i <= n; i++) { n     if (i > 1) { n       for (int j = 1; j < k; j++) upd(i, j); n     } n     for (int j = 1; j + k - 1 <= m; j++) { n       int l = j, r = j + k - 1; n       if (i == 1) n         dp[1][j] = s[1][r] - s[1][l - 1]; n       else { n         del(i, l - 1); n         upd(i, r); n         dp[i][j] = s[i][r] - s[i][l - 1] + val[1]; n       } n     } n     memset(val, 0, sizeof(val)); n     memset(tag, 0, sizeof(tag)); n     if (i < n) build(i, 1, g, 1); n   } n   int ans = 0; n   for (int i = 1; i <= g; i++) ans = max(ans, dp[n][i]); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n inline void mine(T1 &x, T2 y) { n   if (x > y) x = y; n } n template <typename T1, typename T2> n inline void maxe(T1 &x, T2 y) { n   if (x < y) x = y; n } n ostream &operator<<(ostream &a, const vector<long long> &b) { n   for (auto k : b) cout << k <<    ; n   return a; n } n const long long MOD = 1000000007; n const long long INF = 1000000050; n const long long BIG = (long long)2e18 + 50; n const long long MX = 2001; n const long double EPS = 1e-6; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n bool lt(long double x, long double y) { return x + EPS < y; } n bool eq(long double x, long double y) { return abs(x - y) < EPS; } n struct pt { n   long double x; n   long double y; n   long double z; n   pt(long double a, long double b, long double c) { n     x = a; n     y = b; n     z = c; n   } n   bool operator<(const pt &p) const { n     return lt(x, p.x) || n            (eq(x, p.x) && (lt(y, p.y) || (eq(y, p.y) && lt(z, p.z)))); n   } n   bool operator==(const pt &p) const { n     return eq(x, p.x) && eq(y, p.y) && eq(z, p.z); n   } n }; n long double vx[MX], vy[MX], x[MX], y[MX]; n bool can[MX][MX]; n long double tt[MX][MX]; n bool cmp(pair<long double, long double> a, pair<long double, long double> b) { n   return a.second < b.second; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n; n   cin >> n; n   for (long long i = 0; i < n; i++) { n     long double t1, x1, y1, t2, x2, y2; n     cin >> t1 >> x1 >> y1 >> t2 >> x2 >> y2; n     vy[i] = (y2 - y1) / (t2 - t1); n     vx[i] = (x2 - x1) / (t2 - t1); n     x[i] = x1 - vx[i] * t1; n     y[i] = y1 - vy[i] * t1; n   } n   for (long long i = 0; i < n; i++) { n     for (long long j = i + 1; j < n; j++) { n       long double tim = 0; n       if (!eq(vx[i], vx[j])) tim = (x[i] - x[j]) / (vx[j] - vx[i]); n       if (!eq(vy[i], vy[j])) tim = (y[i] - y[j]) / (vy[j] - vy[i]); n       long double xx = x[i] + vx[i] * tim; n       long double yy = y[i] + vy[i] * tim; n       if (eq(xx, x[j] + vx[j] * tim) && eq(yy, y[j] + vy[j] * tim)) { n         can[i][j] = can[j][i] = true; n         tt[i][j] = tt[j][i] = tim; n       } n     } n   } n   long long ans = 1; n   for (long long i = 0; i < n; i++) { n     vector<pair<long double, long double>> kek; n     vector<long double> colis; n     for (long long j = 0; j < n; j++) { n       if (!can[i][j]) continue; n       colis.push_back(tt[i][j]); n       long double xx = vx[j] - vx[i]; n       long double yy = vy[j] - vy[i]; n       long double ang = atan2(yy, xx); n       if (ang < -EPS) ang += 4 * atan(1); n       kek.push_back({ang, sin(ang) * yy + cos(ang) * xx}); n     } n     sort(colis.begin(), colis.end()); n     long long it = 0; n     while (it < (long long)colis.size()) { n       long long it2 = it + 1; n       while (it2 < (long long)colis.size() && eq(colis[it2], colis[it])) ++it2; n       maxe(ans, it2 - it + 1); n       it = it2; n     } n     sort(kek.begin(), kek.end()); n     long long her = 0; n     it = 0; n     while (it < (long long)kek.size()) { n       long long it2 = it + 1; n       while (it2 < (long long)kek.size() && n              kek[it2].first - kek[it].first < EPS) n         ++it2; n       sort(kek.begin() + it, kek.begin() + it2, cmp); n       long long dif = 1; n       for (long long k = it + 1; k < it2; k++) n         dif += !eq(kek[k].second, kek[k - 1].second); n       maxe(her, dif + 1); n       it = it2; n     } n     maxe(ans, her); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   long long cnt = 0; n   for (long long i = 0; i < s.length(); i++) { n     if ((s[i] -  0 ) % 2 == 0) cnt++; n   } n   if (cnt == 0) n     cout << -1 << endl; n   else { n     vector<char> v; n     for (long long i = 0; i < s.length(); i++) v.push_back(s[i]); n     char c = s[s.length() - 1]; n     for (long long i = 0; i < s.length(); i++) { n       if ((s[i] -  0 ) % 2 == 0) { n         cnt--; n         if (s[i] < c || cnt == 0) { n           v[i] = c; n           v[s.length() - 1] = s[i]; n           break; n         } n       } n     } n     cout << string(v.begin(), v.end()) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std::chrono; n using namespace std; n const long long int mod = 998244353; n const int inf = 1e9 + 7; n long long int f(long long int x, long long int n) { n   long long int r = 1; n   while (n > 0) { n     if (n % 2 == 1) r = (r * x) % mod; n     x = (x * x) % mod; n     n /= 2; n   } n   return r; n } n long long int fac[400010], invf[400010]; n void buildfactorial() { n   fac[0] = 1; n   fac[1] = 1; n   for (int i = 2; i < 400010; i++) n     fac[i] = ((long long int)i * fac[i - 1]) % mod; n   for (int i = 0; i < 400010; ++i) { n     invf[i] = f(fac[i], mod - 2); n   } n } n long long int C(int n, int r) { n   if (n < r) return 0; n   return (((fac[n] * invf[r]) % mod) * invf[n - r]) % mod; n } n long double dist(long double x1, long double y1, long double x2, n                  long double y2) { n   return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); n } n struct node { n   int v, d, p; n }; n void solve() { n   int n; n   cin >> n; n   string s[n]; n   for (int i = 0; i < n; ++i) cin >> s[i]; n   for (int i = n - 2; i >= 0; --i) { n     string t =   ; n     for (int j = 0; j < min(s[i].length(), s[i + 1].length()); ++j) { n       if (s[i][j] > s[i + 1][j]) n         break; n       else if (s[i][j] < s[i + 1][j]) { n         t = s[i]; n         break; n       } n       t += s[i][j]; n     } n     s[i] = t; n   } n   for (int i = 0; i < n; ++i) cout << s[i] << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n int main() { n   int s1, s2, s3, s4; n   int a[4]; n   cin >> a[0] >> a[1] >> a[2] >> a[3]; n   int i, j, k = 0; n   sort(a, a + 4); n   j = a[0]; n   for (i = 1; i < 4; ++i) { n     if (j == a[i]) { n       k++; n     } else n       j = a[i]; n   } n   cout << k; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e8; n const int MN = 2e+7; n int n, m, nn, mm; n int i, j, k, x, y, z, p, u, v; n int d[2000], tt; n char ss[2000][2000]; n void change() { n   int n = nn, m = mm; n   if (x == 1) { n     int tmp = u; n     u = v; n     v = n - tmp + 1; n     swap(n, m); n   } n   if (x == 2) { n     u = n - u + 1; n     v = m - v + 1; n   } n   if (x == 3) { n     int tmp = v; n     v = u; n     u = m - tmp + 1; n     swap(n, m); n   } n   if (y == 1) { n     v = m - v + 1; n   } n   if (z == 3) { n     int tmp = u; n     u = v; n     v = n - tmp + 1; n   } n   if (z == 2) { n     u = n - u + 1; n     v = m - v + 1; n   } n   if (z == 1) { n     int tmp = v; n     v = u; n     u = m - tmp + 1; n   } n } n int main() { n   scanf( %d%d%d%d%d%d , &n, &m, &x, &y, &z, &p); n   nn = n; n   mm = m; n   x = x % 4; n   y = y % 2; n   z = z % 4; n   for (i = 1; i <= p; i++) { n     scanf( %d%d , &u, &v); n     change(); n     printf( %d %d n , u, v); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> z; n const long long N = 5e3 + 7, M1 = 1e9 + 7, M2 = 1e9 + 9, b1 = 127, b2 = 239; n long long maxi1[N], maxi2[N], maxx[N]; n pair<long long, long long> h1[N], h2[N]; n pair<long long, long long> poww[N]; n string best; n long long mod(long long x, long long m) { n   x %= m; n   if (x < 0) x += m; n   return x; n } n pair<long long, long long> get_h(long long l, long long r) { n   long long res1 = n       mod(h1[r].first - h1[l - 1].first * poww[r - l + 1].first, M1); n   long long res2 = n       mod(h1[r].second - h1[l - 1].second * poww[r - l + 1].second, M2); n   return {res1, res2}; n } n pair<long long, long long> get_h2(long long l, long long r) { n   long long res1 = n       mod(h2[r].first - h2[l - 1].first * poww[r - l + 1].first, M1); n   long long res2 = n       mod(h2[r].second - h2[l - 1].second * poww[r - l + 1].second, M2); n   return {res1, res2}; n } n void solve(string s) { n   long long n = s.length(); n   string t = s; n   long long ser = (n + 1) / 2; n   long long len = 0; n   while (len != n && s[len] == s[n - len - 1]) { n     len++; n   } n   if (len == n) { n     best = s; n     return; n   } n   if (2 * len > (long long)best.size()) { n     best =   ; n     for (long long i = 0; i < len; i++) best += s[i]; n     for (long long i = len - 1; i >= 0; i--) best += s[i]; n   } n   for (long long i = 0; i < ser; i++) { n     maxi1[i] = -1; n     long long l = i, r = i; n     while (l >= 0 && r < n && s[l] == s[r]) { n       l--; n       r++; n       maxi1[i] += 2; n       if (l < len) { n         long long curlen = maxi1[i] + 2 * (l + 1); n         if (curlen > (long long)best.size()) { n           best =   ; n           for (long long j = 0; j < r; j++) { n             best += s[j]; n           } n           for (long long j = l; j >= 0; j--) { n             best += s[j]; n           } n         } n       } n     } n   } n   if (n % 2 == 1) ser--; n   for (long long i = 0; i < ser; i++) { n     maxi2[i] = 0; n     long long l = i, r = i + 1; n     while (l >= 0 && r < n && s[l] == s[r]) { n       l--; n       r++; n       maxi2[i] += 2; n       if (l < len) { n         long long curlen = maxi2[i] + 2 * (l + 1); n         if (curlen > (long long)best.size()) { n           best =   ; n           for (long long j = 0; j < r; j++) { n             best += s[j]; n           } n           for (long long j = l; j >= 0; j--) { n             best += s[j]; n           } n         } n       } n     } n   } n } n signed main() { n   ios_base ::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   for (long long i = 0; i < t; i++) { n     string s; n     cin >> s; n     best = s[0]; n     solve(s); n     reverse(s.begin(), s.end()); n     solve(s); n     cout << best << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void chkmax(T& x, T y) { n   x = x > y ? x : y; n } n template <class T> n void chkmin(T& x, T y) { n   x = x < y ? x : y; n } n int n, m; n vector<string> s; n int calc(int mask, int j) { n   int res = 0; n   for (int i = 0; i < n; ++i) n     if ((mask >> i & 1) != (s[i][j] -  0 )) ++res; n   return res; n } n bool check(int a, int b) { n   for (int i = 0; i < n - 1; ++i) n     if (((a >> i & 1) + (a >> i + 1 & 1) + (b >> i & 1) + (b >> i + 1 & 1)) % n             2 == n         0) n       return 0; n   return 1; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cin >> n >> m; n   if (n >= 4) { n     cout << -1 <<   n ; n     return 0; n   } n   s.resize(n); n   for (int i = 0; i < n; ++i) cin >> s[i]; n   vector<vector<int>> dp(m, vector<int>(1 << n, 1e9)); n   for (int mask = 0; mask < 1 << n; ++mask) dp[0][mask] = calc(mask, 0); n   for (int i = 0; i < m - 1; ++i) n     for (int c_mask = 0; c_mask < 1 << n; ++c_mask) n       for (int n_mask = 0; n_mask < 1 << n; ++n_mask) n         if (check(c_mask, n_mask)) n           chkmin(dp[i + 1][n_mask], dp[i][c_mask] + calc(n_mask, i + 1)); n   int ans = 1e9; n   for (int mask = 0; mask < 1 << n; ++mask) chkmin(ans, dp[m - 1][mask]); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 987654321; n const long long int INF = 123456789987654321; n int N; n vector<int> arr, chk, chk2; n bool f(int l, int r) { n   for (int i = 0; i < min(l, N - r - 1); i++) { n     if (arr[i] != arr[N - i - 1]) return false; n   } n   chk = vector<int>(N, 0); n   for (int i = min(l, N - r - 1); i <= max(r, N - l - 1); i++) { n     chk[arr[i]]++; n   } n   chk2 = vector<int>(N, 0); n   int len = max(r, N - l - 1) - min(l, N - r - 1) + 1; n   if (len < 2 * (r - l + 1)) { n     for (int i = min(l, N - r - 1); i <= max(r, N - l - 1); i++) { n       if (i < l || r < i) { n         chk2[arr[i]]++; n         if (chk2[arr[i]] > chk[arr[i]] / 2) return false; n       } n     } n   } else { n     for (int i = min(l, N - r - 1); i < min(l, N - r - 1) + (r - l + 1); i++) { n       chk2[arr[i]]++; n     } n     for (int i = min(l, N - r - 1); i < min(l, N - r - 1) + (r - l + 1); i++) { n       chk2[arr[N - 1 - i]]--; n     } n     for (int i = 0; i < N; i++) { n       if (chk2[i]) return false; n     } n     for (int i = min(l, N - r - 1) + (r - l + 1); n          i <= max(r, N - l - 1) - (r - l + 1); i++) { n       if (arr[i] != arr[N - 1 - i]) return false; n     } n   } n   return true; n } n long long int ans = 0; n int main() { n   scanf( %d , &N); n   arr.resize(N); n   chk = vector<int>(N, 0); n   for (int i = 0; i < N; i++) { n     scanf( %d , &arr[i]); n     chk[--arr[i]]++; n   } n   if (N == 1) { n     printf( 1 ); n     return 0; n   } n   int oddcnt = 0; n   for (int i = 0; i < N; i++) { n     if (chk[i] % 2) oddcnt++; n   } n   if (oddcnt > 1) { n     printf( 0 ); n     return 0; n   } n   int s = 0, e = N - 1; n   int idx; n   while (s <= e) { n     int m = (s + e) >> 1; n     if (f(0, m)) { n       idx = m; n       e = m - 1; n     } else n       s = m + 1; n   } n   s = 0, e = N - 1; n   int idx2; n   while (s <= e) { n     int m = (s + e) >> 1; n     if (f(m, N - 1)) { n       idx2 = m; n       s = m + 1; n     } else n       e = m - 1; n   } n   int len = 0; n   for (int i = 0; i < N / 2; i++) { n     if (arr[i] != arr[N - 1 - i]) break; n     len++; n   } n   if (len == N / 2) { n     printf( %I64d , (long long int)N * (N + 1) / 2); n     return 0; n   } n   long long int ans = len * len; n   if (idx < N - len) ans += (len + 1) * (N - len - idx); n   if (len <= idx2) ans += (len + 1) * (idx2 - len + 1); n   if (idx < N - len && len <= idx2) ans--; n   printf( %I64d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n char s[200010]; n int g[200010][30]; n int main() { n   int i, j, k; n   int n, t, m; n   cin >> t; n   while (t--) { n     cin >> n >> m; n     cin >> s + 1; n     int ans[30] = {0}; n     for (i = 1; i <= n; i++) { n       for (j = 0; j < 26; j++) { n         g[i][j] = g[i - 1][j]; n       } n       g[i][s[i] -  a ]++; n     } n     for (i = 1; i <= m; i++) { n       int x; n       cin >> x; n       for (j = 0; j < 26; j++) { n         ans[j] += g[x][j]; n       } n     } n     for (i = 0; i < 26; i++) ans[i] += g[n][i]; n     for (i = 0; i < 26; i++) { n       if (i) printf(   ); n       printf( %d , ans[i]); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int arr; n   int a = 0, b = 0, i, j, k; n   for (i = 0; i < n; i++) { n     cin >> arr; n     if (arr >= 0) { n       a += arr; n     } else { n       b += arr; n     } n   } n   cout << a - b << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, d; n long long x; n int a[100005], b[100005]; n int a_index[100005], q[100005]; n long long getNextX() { n   x = (x * 37 + 10007) % 1000000007; n   return x; n } n void initAB() { n   for (int i = 0; i < n; i = i + 1) { n     a[i] = i + 1; n   } n   for (int i = 0; i < n; i = i + 1) { n     swap(a[i], a[getNextX() % (i + 1)]); n   } n   for (int i = 0; i < n; i = i + 1) { n     if (i < d) n       b[i] = 1; n     else n       b[i] = 0; n   } n   for (int i = 0; i < n; i = i + 1) { n     swap(b[i], b[getNextX() % (i + 1)]); n   } n } n int main() { n   cin >> n >> d >> x; n   initAB(); n   for (int i = 0; i < n; i++) { n     a_index[a[i]] = i; n   } n   int s = 30; n   for (int i = 0; i < n; i++) n     if (b[i]) q[++q[0]] = i; n   for (int i = 0; i < n; i++) { n     int j; n     for (j = n; j >= n - s; j--) n       if (j > 0 && i >= a_index[j] && b[i - a_index[j]]) { n         printf( %d n , j); n         break; n       } n     if (j < n - s) { n       int ma = 0; n       for (j = 1; j <= q[0] && q[j] <= i; j++) ma = max(ma, a[i - q[j]]); n       printf( %d n , ma); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, t, p[105], ans, x = 1, c; n pair<long long, long long> a[105]; n int cmp(pair<long long, long long> a, pair<long long, long long> b) { n   return a.first * a.second < b.first * b.second; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i].second >> a[i].first; n   cin >> t; n   for (int i = 1; i <= t; i++) cin >> p[i]; n   for (int i = t; i >= 1; i--) p[i] -= p[i - 1]; n   sort(a + 1, a + 1 + n); n   p[t + 1] = 1000000000000000009; n   for (int i = 1; i <= n; i++) n     while (a[i].second) { n       ans += min(a[i].second, p[x]) * x * a[i].first; n       long long r = min(a[i].second, p[x]); n       a[i].second -= r; n       p[x] -= r; n       if (!p[x]) x++; n     } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool solve() { n   char s[500]; n   if (scanf( %s , s) == EOF) return false; n   int in = 0, out = 0; n   for (int i = 0; s[i]; ++i) { n     if (s[i] ==  + ) { n       if (out) { n         out--; n         in++; n       } else { n         in++; n       } n     } else { n       if (in) { n         in--; n         out++; n       } else { n         out++; n       } n     } n   } n   printf( %d n , in + out); n   return true; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n  n typedef long long ll; n typedef long double ld; n #define mems(p) memset(p,-1,sizeof(p)) n #define pb push_back n #define eb emplace_back n #define nl   n  n #define sz(x) (int)(x).size() n #define all(x) x.begin(), x.end() n #define allr(x) x.rbegin(), x.rend() n  n using pi = pair<int,int>; n using pl = pair<ll,ll>; n using vi = vector<int>; n using vl = vector<ll>; n using vb = vector<bool>; n using vd = vector<double>; n using vc = vector<char>; n using vpi = vector<pi>; n using vpl = vector<pl>; n using vvi = vector<vector<int>>; n using vvl = vector<vector<ll>>; n using vvb = vector<vector<bool>>; n using vvd = vector<vector<double>>; n using vvc = vector<vector<char>>; n using vvpi = vector<vector<pi>>; n using vvpl = vector<vector<pl>>; n using vvvi = vector<vector<vector<int>>>; n using vvvl = vector<vector<vector<ll>>>; n using vvvb = vector<vector<vector<bool>>>; n using vvvd = vector<vector<vector<double>>>; n using vvvc = vector<vector<vector<char>>>; n using vvvpi = vector<vector<vector<pi>>>; n using vvvpl = vector<vector<vector<pl>>>; n  n const int mxN = 8e5 + 100; n const ll MOD = 1e9 + 7; n  n void fast() n {ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);} n  n ll gcd(ll a, ll b) { n     return b ? gcd(b, a%b) : a; n } n  n vl factorial; n  n ll expo(ll x, ll exp, ll mod = MOD) { n     if (exp <= 0) return 1; n     ll tmp = (x*x) % mod; n     if (exp%2) { n         return (expo(tmp, exp/2, mod) * x) % mod; n     } n     return (expo(tmp, exp/2, mod)) % mod; n } n  n ll choice(ll n, ll r, ll mod = MOD) { n     if (n < r) return 0; n     ll numer = factorial[n]; n     ll denom = (factorial[r] * factorial[n-r]) % mod; n     return numer * expo(denom, mod - 2, mod) % mod; n } n  n ll phi(ll n) { n     ll result = n; n     for (ll i = 2; i * i <= n; i++) { n         if (n % i == 0) { n             while (n % i == 0) n                 n /= i; n             result -= result / i; n         } n     } n     if (n > 1) n         result -= result / n; n     return result; n } n  n ll store[5001][5001]; n ll a[5001]; n int n; n  n ll dp(int x, int k) { n     if (k == 0) return 1; n     if (store[x][k] != -1) return store[x][k]; n     ll res = 0; n     if (x > 0) res += dp(x-1,k-1); n     if (x < n - 1) res += dp(x+1,k-1); n     if (res >= MOD) res -= MOD; n     return store[x][k] = res; n } n  n int main() { n     fast(); n     int k, q; n     cin >> n >> k >> q; n     for (int i = 0; i < n; ++i) n     { n         cin >> a[i]; n     } n     mems(store); n     ll ans = 0; n     vl sum(n, 0); n     for (int i = 0; i < n; ++i) n     { n         for (int j = 0; j <= k; ++j) n         { n             sum[i] += dp(i,j)*dp(i,k-j); n             sum[i] %= MOD; n         } n         ans += sum[i]*a[i]; n         ans %= MOD; n     } n     //cout <<  init   << ans << nl; n  n     int idx; n     ll x; n     for (int i = 0; i < q; ++i) n     { n         cin >> idx >> x; n         --idx; n         ll chng = x - a[idx]; n         if (chng < 0) chng += MOD; n         ans += chng*sum[idx]; n         ans %= MOD; n         cout << ans << nl; n         a[idx] = x; n     } n } n  n  n  n  n  n  n  n  n  n


#include <bits/stdc++.h> n using namespace std; n int dp[1010][1010][11][2]; n int main() { n   ios_base::sync_with_stdio(0); n   int n, m, k; n   cin >> n >> m >> k; n   int kk = k; n   string a, b; n   cin >> a >> b; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= m; ++j) { n       if (a[i - 1] == b[j - 1]) { n         for (int k = 1; k <= kk; ++k) { n           dp[i][j][k][0] = n               max(dp[i - 1][j - 1][k][0], dp[i - 1][j - 1][k - 1][1]) + 1; n         } n       } n       for (int k = 1; k <= kk; ++k) { n         dp[i][j][k][1] = max(max(dp[i - 1][j - 1][k][1], dp[i - 1][j][k][1]), n                              max(dp[i][j - 1][k][1], dp[i][j][k][0])); n       } n     } n   } n   int ans = 0; n   for (int i = 1; i <= k; ++i) { n     ans = max(ans, dp[n][m][i][1]); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[300], ans[500][2], tot, v[300]; n void sl(int p, int x) { n   if (p == x || x == 0) return; n   if (a[x] == 0 || a[x] == x) { n     ans[tot][0] = p, ans[tot][1] = x; n     a[x] = x; n     a[p] = 0; n     tot++; n     return; n   } n   if (v[x]) { n     ans[tot][0] = p, ans[tot][1] = n; n     a[p] = 0; n     a[n] = x; n     tot++; n     return; n   } n   v[p] = 1; n   sl(x, a[x]); n   ans[tot][0] = p, ans[tot][1] = x; n   a[x] = x; n   a[p] = 0; n   tot++; n   v[p] = 0; n } n int main() { n   scanf( %d%d , &n, &m); n   int p = 0, t, sum = 0; n   for (int i = 0; i < m; i++) { n     scanf( %d , &t); n     sum += t; n     while (t--) { n       int x; n       scanf( %d , &x); n       a[x] = ++p; n     } n   } n   for (int i = n; i > 0; i--) { n     sl(i, a[i]); n     sl(n, a[n]); n   } n   printf( %d n , tot); n   for (int i = 0; i < tot; i++) { n     printf( %d %d n , ans[i][0], ans[i][1]); n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n const double eps = 1e-6; n const double pi = acos(-1.0); n template <class Z> n inline Z SQ(Z &a) { n   return a * a; n } n template <class Z> n inline bool checkmax(Z &a, Z b) { n   if (a == -1 || a < b) { n     a = b; n     return true; n   } n   return false; n } n template <class Z> n inline bool checkmin(Z &a, Z b) { n   if (a == -1 || a > b) { n     a = b; n     return true; n   } n   return false; n } n template <class Z> n inline bool mymax(Z &a, Z b) { n   if (a < b) { n     a = b; n     return true; n   } n   return false; n } n template <class Z> n inline bool mymin(Z &a, Z b) { n   if (a > b) { n     a = b; n     return true; n   } n   return false; n } n int a[100005]; n int n, m; n struct SegTree { n   long long tree[100005 << 2]; n   inline void PushUp(int rt) { n     tree[rt] = tree[(rt << 1)] + tree[(rt << 1 | 1)]; n   } n   void build(int s, int z, int l, int r, int rt) { n     if (l == r) { n       long long v = a[l]; n       int tt = (l + s) % (2 * z - 2); n       if (tt == 0) n         tree[rt] = v * 2; n       else if (tt <= z) n         tree[rt] = v * tt; n       else n         tree[rt] = v * (2 * z - tt); n       return; n     } n     int m = (l + r) >> 1; n     build(s, z, l, m, rt << 1); n     build(s, z, m + 1, r, rt << 1 | 1); n     PushUp(rt); n   } n   void update(int s, int z, long long val, int x, int l, int r, int rt) { n     if (l == r) { n       int tt = (x + s) % (2 * z - 2); n       if (tt == 0) n         tree[rt] = val * 2; n       else if (tt <= z) n         tree[rt] = val * tt; n       else n         tree[rt] = val * (2 * z - tt); n       return; n     } n     int m = (l + r) >> 1; n     if (x <= m) n       update(s, z, val, x, l, m, rt << 1); n     else n       update(s, z, val, x, m + 1, r, rt << 1 | 1); n     PushUp(rt); n   } n   long long query(int ll, int rr, int l, int r, int rt) { n     if (ll <= l && r <= rr) { n       return tree[rt]; n     } n     long long sum = 0; n     int m = (l + r) >> 1; n     if (ll <= m) sum += query(ll, rr, l, m, rt << 1); n     if (rr > m) sum += query(ll, rr, m + 1, r, rt << 1 | 1); n     return sum; n   } n } tree[4][8], t6[10]; n void input() { n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n } n void build() { n   for (int z = 2; z <= 5; z++) { n     for (int i = 0; i < 2 * z - 2; i++) { n       tree[z - 2][i].build(i, z, 1, n, 1); n     } n   } n   for (int i = 0; i < 10; i++) { n     t6[i].build(i, 6, 1, n, 1); n   } n } n void gao() { n   scanf( %d , &m); n   while (m--) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       int p, v; n       scanf( %d%d , &p, &v); n       for (int z = 2; z <= 5; z++) { n         for (int i = 0; i < 2 * z - 2; i++) { n           tree[z - 2][i].update(i, z, v, p, 1, n, 1); n         } n       } n       for (int i = 0; i < 10; i++) { n         t6[i].update(i, 6, v, p, 1, n, 1); n       } n     } else { n       int l, r, z; n       scanf( %d%d%d , &l, &r, &z); n       int tp = l % (2 * z - 2); n       if (tp == 0) tp = 2 * z - 2; n       int s = (2 * z - 2) - tp + 1; n       s %= (2 * z - 2); n       if (z == 6) n         printf( %I64d n , t6[s].query(l, r, 1, n, 1)); n       else n         printf( %I64d n , tree[z - 2][s].query(l, r, 1, n, 1)); n     } n   } n } n int main() { n   while (~scanf( %d , &n)) { n     input(); n     build(); n     gao(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 5; n const int MOD = 1e9 + 7; n void solve() { n   int n, m; n   cin >> n >> m; n   cout << (n % m ?  NO n  :  YES n ); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int t = 1; n   cin >> t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   int n, m, f = 0; n   cin >> n >> m; n   cin >> s; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  # ) n       f++; n     else n       f = 0; n     if (f >= m) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve(); n void YN(bool b) { n   if (b) { n     cout <<  YES  n          <<   n ; n     ; n   } else { n     cout <<  NO  n          <<   n ; n     ; n   } n }; n void yn(bool b) { n   if (b) { n     cout <<  Yes  n          <<   n ; n     ; n   } else { n     cout <<  No  n          <<   n ; n     ; n   } n }; n void queries() { n   int n; n   cin >> n; n   while (n--) solve(); n } n const int maxn = 200200; n void solve() { n   int n; n   cin >> n; n   vector<int> a(n); n   vector<int> b; n   map<int, int> m; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     m[a[i]]++; n   } n   for (auto i : m) { n     b.push_back(i.second); n   } n   map<int, bool> taken; n   long long sum = 0; n   sort(b.begin(), b.end()); n   for (int i = b.size() - 1; i >= 0; i--) { n     if (!taken[b[i]]) { n       sum += b[i]; n       taken[b[i]] = 1; n     } else { n       for (int j = b[i]; j > 0; j--) { n         if (!taken[j]) { n           sum += j; n           taken[j] = 1; n           break; n         } n       } n     } n   } n   cout << sum <<   n ; n   ; n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   queries(); n   ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, q; n   string a; n   cin >> n; n   map<string, int> m; n   map<string, string> ma; n   for (int i = 1; i <= n; i++) { n     cin >> a; n     int len = a.length(); n     for (int i = 0; i <= len; i++) { n       for (int j = 0; j + i <= len; j++) { n         string s1 = a.substr(i, j); n         if (ma[s1] != a) { n           m[s1]++; n           ma[s1] = a; n         } n       } n     } n   } n   cin >> q; n   for (int i = 1; i <= q; i++) { n     cin >> a; n     if (m[a] != 0) { n       cout << m[a] <<     << ma[a] << endl; n     } else n       printf( 0 - n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   n = (int)((double)n / 3 + 0.5); n   cout << n / 12 <<     << n % 12 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n struct LinearBase { n   const int n = 30; n   vector<int> pivot = vector<int>(n); n   void Insert(int curr) { n     for (int bit = n - 1; bit >= 0 && curr != 0; --bit) { n       if ((curr & (1 << bit)) != 0) { n         curr ^= pivot[bit]; n       } n     } n     if (curr == 0) return; n     int msb = 31 - __builtin_clz(curr); n     assert(pivot[msb] == 0); n     pivot[msb] = curr; n     for (int bit = n - 1; bit > msb; --bit) { n       if ((pivot[bit] & (1 << bit)) != 0) { n         pivot[bit] ^= curr; n       } n     } n   } n   int Query(int ans) { n     for (int bit = n - 1; bit >= 0; --bit) { n       if ((ans & (1 << bit)) != 0) { n         ans ^= pivot[bit]; n       } n     } n     return ans; n   } n }; n struct Graph { n   int n; n   LinearBase base; n   vector<int> dist; n   vector<vector<pair<int, int>>> adj; n   Graph(int n_) : n(n_), dist(n, -1), adj(n) {} n   void AddEdge(int a, int b, int w) { n     adj[a].emplace_back(b, w); n     adj[b].emplace_back(a, w); n   } n   void DFS(int node) { n     for (auto &e : adj[node]) { n       int x, w; n       tie(x, w) = e; n       if (dist[x] == -1) { n         dist[x] = dist[node] ^ w; n         DFS(x); n       } else { n         base.Insert(dist[node] ^ dist[x] ^ w); n       } n     } n   } n   int Solve() { n     dist[0] = 0; n     DFS(0); n     return base.Query(dist[n - 1]); n   } n }; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   Graph g(n); n   for (int i = 0; i < m; ++i) { n     int a, b, w; n     cin >> a >> b >> w; n     g.AddEdge(a - 1, b - 1, w); n   } n   cout << g.Solve() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, dp[30][30]; n string s; n long long ten[30]; n string ans[30][30]; n int main() { n   cin >> n >> s; n   reverse(s.begin(), s.end()); n   ten[0] = 1; n   for (int i = 1; i <= 20; i++) ten[i] = ten[i - 1] * 10; n   dp[0][0] = 0; n   dp[1][0] = s[0] -  0 ; n   ans[1][0] =  M ; n   dp[0][1] = s[0] -  0 ; n   ans[0][1] =  H ; n   for (int i = 2; i <= n; i++) { n     long long q = (int)s[i - 1] -  0 ; n     dp[i][0] = dp[i - 1][0] + q * ten[i - 1]; n     ans[i][0] = ans[i - 1][0]; n     ans[i][0] +=  M ; n     dp[0][i] = dp[0][i - 1] + q * ten[i - 1]; n     ans[0][i] = ans[0][i - 1]; n     ans[0][i] +=  H ; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       long long q = (int)s[i + j - 1] -  0 ; n       long long m = dp[i - 1][j] + q * ten[i - 1]; n       long long h = dp[i][j - 1] + q * ten[j - 1]; n       if (m <= h) { n         dp[i][j] = h; n         ans[i][j] = ans[i][j - 1]; n         ans[i][j] +=  H ; n       } else { n         dp[i][j] = m; n         ans[i][j] = ans[i - 1][j]; n         ans[i][j] +=  M ; n       } n     } n   s = ans[n][n]; n   reverse(s.begin(), s.end()); n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 7; n const int maxm = 1e9 + 7; n int n; n int a[maxn], b[maxn], c[maxn], ans[maxn]; n map<int, int> mp; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     b[i] = a[i]; n   } n   queue<int> q; n   q.push(n); n   mp[a[n]] = n; n   for (int i = n; i >= 1; i--) { n     if (a[i] < a[q.back()]) { n       q.push(i); n       mp[a[q.back()]] = i; n     } n   } n   sort(b + 1, b + 1 + n); n   int num = unique(b + 1, b + n + 1) - b; n   int p = 0; n   for (int i = 1; i <= num - 1; i++) { n     int j = b[i]; n     if (!mp[j]) n       mp[j] = p; n     else { n       swap(mp[j], p); n     } n   } n   for (int i = 1; i <= n; i++) { n     if (i >= mp[a[i]]) n       printf( -1  ); n     else n       printf( %d  , mp[a[i]] - i - 1); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.sync_with_stdio(false); n   cin.tie(0); n   long int n; n   cin >> n; n   const long int N = n; n   char s[N]; n   int cnt0 = 0, cnt1 = 0; n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n     if (s[i] ==  z ) { n       cnt0++; n     } n     if (s[i] ==  n ) { n       cnt1++; n     } n   } n   for (int i = 0; i < cnt1; i++) cout <<  1  ; n   for (int i = 0; i < cnt0; i++) cout <<  0  ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10005, M = 105; n int n; n char s[N]; n int p, m; n int lim; n int dp[2][N][M]; n int ch[N][2], tot = 1; n int fa[N]; n void dfs(int u) { n   if (!ch[u][0]) return; n   dfs(ch[u][0]); n   dfs(ch[u][1]); n   for (int i = 0; i <= lim; i++) n     for (int j = 0; i + j <= lim; j++) { n       dp[0][u][i + j + (p < m)] = max(dp[0][u][i + j + (p < m)], n                                       dp[0][ch[u][0]][i] + dp[0][ch[u][1]][j]); n       dp[0][u][i + j + (p >= m)] = max(dp[0][u][i + j + (p >= m)], n                                        dp[0][ch[u][0]][i] - dp[1][ch[u][1]][j]); n       dp[1][u][i + j + (p < m)] = min(dp[1][u][i + j + (p < m)], n                                       dp[1][ch[u][0]][i] + dp[1][ch[u][1]][j]); n       dp[1][u][i + j + (p >= m)] = min(dp[1][u][i + j + (p >= m)], n                                        dp[1][ch[u][0]][i] - dp[0][ch[u][1]][j]); n     } n   return; n } n int main() { n   scanf( %s , s + 1); n   scanf( %d%d , &p, &m); n   n = strlen(s + 1); n   lim = min(p, m); n   memset(dp[0], -63, sizeof(dp[0])); n   memset(dp[1], 63, sizeof(dp[1])); n   tot = 1; n   int pre = tot; n   for (int i = 1; i <= n; i++) { n     if (s[i] ==  (  || s[i] ==  ? ) { n       ch[pre][ch[pre][0] ? 1 : 0] = ++tot; n       fa[tot] = pre; n       pre = tot; n     } else if (s[i] ==  ) ) n       pre = fa[pre]; n     else n       dp[0][tot][0] = dp[1][tot][0] = s[i] -  0 , pre = fa[pre]; n   } n   dfs(1); n   printf( %d , dp[0][1][lim]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 100007; n int v[NMAX]; n int n; n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> v[i]; n   } n   sort(v + 1, v + n + 1); n   for (int i = 3; i <= n; ++i) { n     if (v[i - 2] + v[i - 1] > v[i]) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char s[100005]; n vector<int> p, star; n void input() { n   scanf( %s , s); n   p.clear(), star.clear(); n   int l = strlen(s); n   for (int i = 0; i < l; i++) { n     if (s[i] ==  P ) p.push_back(i); n     if (s[i] ==  * ) star.push_back(i); n   } n   n = p.size(), m = star.size(); n } n bool can(int step) { n   int now = 0; n   for (int i = 0; i < n; i++) { n     if (now == m) return 1; n     if (p[i] - star[now] > step) return 0; n     int j = now; n     while (j < m) { n       int MIN = min(abs(p[i] - star[now]), abs(p[i] - star[j])); n       MIN += star[j] - star[now]; n       if (MIN <= step) n         j++; n       else n         break; n     } n     now = j; n     if (now == m) return 1; n   } n   return 0; n } n int binary(int l, int r) { n   if (l == r) return l; n   int m = (l + r) >> 1; n   if (can(m)) return binary(l, m); n   return binary(m + 1, r); n } n int main() { n   int l; n   while (cin >> l) { n     input(); n     cout << binary(0, 2 * l) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0); n #define F first n #define S second n #define V vector n #define PB push_back n #define MP make_pair n #define EB emplace_back n #define ALL(v) (v).begin(), (v).end() n #define debug(x) cerr <<  Line(  << __LINE__ <<  ) ->   << #x <<   is   << x << endl n  n using namespace std; n  n typedef long long ll; n typedef pair<int, int> pi; n typedef V<int> vi; n  n const int INF = 1e9 + 7, N = 4e5 + 7; n  n int p[N], cnt[N], vis[N]; n vi G[N]; n  n int find(int u) { n  return p[u] == u ? u : p[u] = find(p[u]); n } n  n void unite(int x, int y) { n  int rx = find(x), ry = find(y); n  if(rx == ry) return; n  cnt[ry] += cnt[rx]; n  p[rx] = ry; n } n  n signed main() n { n  IO_OP; n  n  int n; n  cin >> n; n  V<array<int, 3>> a; n  for(int i = 1; i <= n; i++) { n   int x, y; n   cin >> x >> y; n   a.PB({x, y, i}); n   a.PB({y, x, -i}); n  } n  sort(ALL(a)); n  for(int i = 2; i <= 2 * n + 1; i++) p[i] = i; n  for(int i = 1; i <= n; i++) cnt[i * 2] = 1; n  set<array<int, 3>> stk; n  for(int i = 0; i < n * 2; i++) { n   int u = abs(a[i][2]) * 2, v = u + 1; n   if(a[i][2] < 0) swap(u, v); n   int y = a[i][1]; n   for(auto tt:stk) { n    if(tt[0] > y) break; n    unite(tt[1], v); n    unite(tt[2], u); n    if(find(u) == find(v)) { n     cout << -1 <<   n ; n     return 0; n    } n   } n   stk.insert({y, u, v}); n  } n  for(int i = 1; i <= n; i++) { n   if(find(i * 2) == find(i * 2 + 1)) { n    cout << -1 <<   n ; n    return 0; n   } n  } n  for(int i = 2; i <= n * 2 + 1; i++) { n   G[find(i)].PB(i); n  } n  int ans = 0; n  for(int i = 1; i <= n; i++) { n   if(!vis[i]) { n    int x = find(i * 2), y = find(i * 2 + 1); n    ans += min(cnt[x], cnt[y]); n    for(int u:G[x]) vis[u / 2] = 1; n   } n  } n  cout << ans <<   n ; n } n  n


#include <bits/stdc++.h> n using namespace std; n vector<int> a; n int main() { n   string s; n   cin >> s; n   int cur = 0, mul = 1; n   for (int i = s.length() - 1; i >= 0; --i) { n     if (s[i] ==  , ) { n       a.push_back(cur); n       mul = 1; n       cur = 0; n     } else { n       cur += mul * (s[i] - 48); n       mul *= 10; n     } n   } n   a.push_back(cur); n   sort(a.begin(), a.end()); n   int L; n   int last = a[0]; n   bool ok = 0; n   printf( %d , a[0]); n   L = a[0]; n   for (int i = 1; i < a.size(); ++i) { n     if (a[i] == last) continue; n     if (a[i] == last + 1) { n       ok = 1; n       last = a[i]; n     } else { n       if (ok && L != last) { n         printf( -%d , last); n         L = last; n       } n       last = a[i]; n       printf( ,%d , a[i]); n       L = a[i]; n     } n   } n   if (ok && L != last) printf( -%d , last); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   string str; n   cin >> str; n   set<string> visited; n   queue<string> s; n   s.push(str); n   visited.insert(str); n   int count = 0, x = 0; n   while (!s.empty() && count != k) { n     string temp = s.front(); n     x = x + temp.length(); n     count++; n     s.pop(); n     for (int i = 0; i < temp.length(); i++) { n       string temp2 =   ; n       for (int j = 0; j < temp.length(); j++) { n         if (j != i) temp2 += temp[j]; n       } n       if (visited.find(temp2) == visited.end()) { n         s.push(temp2); n         visited.insert(temp2); n       } n     } n   } n   if (s.empty() && count < k) n     cout <<  -1 ; n   else n     cout << n * k - x; n   return 0; n } n


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