#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using db = double; n using str = string; n using pi = pair<int, int>; n using pl = pair<ll, ll>; n using pd = pair<db, db>; n using vi = vector<int>; n using vb = vector<bool>; n using vl = vector<ll>; n using vd = vector<db>; n using vs = vector<str>; n using vpi = vector<pi>; n using vpl = vector<pl>; n using vpd = vector<pd>; n template <class T> n using V = vector<T>; n template <class T, size_t SZ> n using AR = array<T, SZ>; n const int MOD = 1e9 + 7; n const int MX = 1e6 + 4; n const ll INF = 1e18; n const ld PI = acos((ld)-1); n const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1}; n mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); n constexpr int pct(int x) { return __builtin_popcount(x); } n constexpr int bits(int x) { return 31 - __builtin_clz(x); } n ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } n ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } n ll half(ll x) { return fdiv(x, 2); } n template <class T> n bool ckmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool ckmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n template <class T, class U> n T fstTrue(T lo, T hi, U first) { n   hi++; n   assert(lo <= hi); n   while (lo < hi) { n     T mid = half(lo + hi); n     first(mid) ? hi = mid : lo = mid + 1; n   } n   return lo; n } n template <class T, class U> n T lstTrue(T lo, T hi, U first) { n   lo--; n   assert(lo <= hi); n   while (lo < hi) { n     T mid = half(lo + hi + 1); n     first(mid) ? lo = mid : hi = mid - 1; n   } n   return lo; n } n template <class T> n void remDup(vector<T>& v) { n   sort(begin(v), end(v)); n   v.erase(unique(begin(v), end(v)), end(v)); n } n template <class T, class U> n void erase(T& t, const U& u) { n   auto it = t.find(u); n   assert(it != end(t)); n   t.erase(u); n } n template <class T> n void re(complex<T>& c); n template <class T, class U> n void re(pair<T, U>& p); n template <class T> n void re(vector<T>& v); n template <class T, size_t SZ> n void re(AR<T, SZ>& a); n template <class T> n void re(T& x) { n   cin >> x; n } n void re(db& d) { n   str t; n   re(t); n   d = stod(t); n } n void re(ld& d) { n   str t; n   re(t); n   d = stold(t); n } n template <class T, class... U> n void re(T& t, U&... u) { n   re(t); n   re(u...); n } n template <class T> n void re(complex<T>& c) { n   T a, b; n   re(a, b); n   c = {a, b}; n } n template <class T, class U> n void re(pair<T, U>& p) { n   re(p.first, p.second); n } n template <class T> n void re(vector<T>& x) { n   for (auto& a : x) re(a); n } n template <class T, size_t SZ> n void re(array<T, SZ>& x) { n   for (auto& a : x) re(a); n } n str to_string(char c) { return str(1, c); } n str to_string(const char* second) { return (str)second; } n str to_string(str second) { return second; } n str to_string(bool b) { return to_string((int)b); } n template <class T> n str to_string(complex<T> c) { n   stringstream ss; n   ss << c; n   return ss.str(); n } n str to_string(vector<bool> v) { n   str res =  { ; n   for (int i = (0); i < ((int)(v).size()); ++i) res += char( 0  + v[i]); n   res +=  } ; n   return res; n } n template <size_t SZ> n str to_string(bitset<SZ> b) { n   str res =   ; n   for (int i = (0); i < (SZ); ++i) res += char( 0  + b[i]); n   return res; n } n template <class T, class U> n str to_string(pair<T, U> p); n template <class T> n str to_string(T v) { n   bool fst = 1; n   str res =   ; n   for (const auto& x : v) { n     if (!fst) res +=    ; n     fst = 0; n     res += to_string(x); n   } n   return res; n } n template <class T, class U> n str to_string(pair<T, U> p) { n   return to_string(p.first) +     + to_string(p.second); n } n template <class T> n void pr(T x) { n   cout << to_string(x); n } n template <class T, class... U> n void pr(const T& t, const U&... u) { n   pr(t); n   pr(u...); n } n void ps() { pr(  n ); } n template <class T, class... U> n void ps(const T& t, const U&... u) { n   pr(t); n   if (sizeof...(u)) pr(   ); n   ps(u...); n } n void DBG() { cerr <<  ]  << endl; } n template <class T, class... U> n void DBG(const T& t, const U&... u) { n   cerr << to_string(t); n   if (sizeof...(u)) cerr <<  ,  ; n   DBG(u...); n } n void setIn(str second) { freopen(second.c_str(),  r , stdin); } n void setOut(str second) { freopen(second.c_str(),  w , stdout); } n void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } n void setIO(str second =   ) { n   unsyncIO(); n   if ((int)(second).size()) { n     setIn(second +  .in ), setOut(second +  .out ); n   } n } n int n; n ll D; n AR<ll, 3> r; n ll yes[MX], no[MX]; n ll dp[MX][4]; n vl a; n int main() { n   setIO(); n   re(n, r, D); n   a.resize(n); n   re(a); n   for (int i = (0); i < (n); ++i) { n     no[i] = a[i] * r[0] + r[2]; n     yes[i] = min((a[i] + 1) * r[0], r[1]) + r[0]; n     ckmin(yes[i], no[i]); n   } n   for (int i = (0); i < (n); ++i) 0; n   ll ans = LLONG_MAX; n   for (int k = (1); k < (4); ++k) { n     int in = k / 2; n     if (in >= 1) n       dp[0][k] = yes[0] + k * D; n     else n       dp[0][k] = no[0] + k * D; n   } n   for (int i = (1); i < (n); ++i) { n     for (int k = (1); k < (4); ++k) dp[i][k] = INF; n     for (int j = (1); j < (4); ++j) { n       if (i == n - 1) { n         int in = 0; n         in += (j + 1) / 2; n         if (in >= 2) n           ckmin(ans, dp[i - 1][j] + yes[i]); n         else n           ckmin(ans, dp[i - 1][j] + no[i]); n       } else { n         for (int k = (1); k < (4); ++k) { n           if (j % 2 == 0 && k % 2 == 1) continue; n           int in = 0; n           in += (j + 1) / 2; n           in += k / 2; n           if (in >= 2) n             ckmin(dp[i][k], dp[i - 1][j] + yes[i] + k * D); n           else n             ckmin(dp[i][k], dp[i - 1][j] + no[i] + k * D); n         } n       } n     } n   } n   ps(ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 4; n struct node { n   int ptr[26], link, len, cl; n }; n struct SAM { n   node t[N]; n   int las, siz; n   inline void init() { n     siz = 1; n     las = 0; n     t[0].len = 0; n     t[0].link = -1; n   } n   inline void extend(int c) { n     int cur = siz++, p; n     t[cur].len = t[las].len + 1; n     for (p = las; p != -1 && !t[p].ptr[c]; p = t[p].link) t[p].ptr[c] = cur; n     if (p == -1) { n       t[cur].link = 0; n       las = cur; n       return; n     } n     int q = t[p].ptr[c]; n     if (t[p].len + 1 == t[q].len) { n       t[cur].link = q; n       las = cur; n       return; n     } n     int clo = siz++; n     t[clo] = t[q]; n     t[clo].len = t[p].len + 1; n     t[clo].cl = 1; n     for (; p != -1 && t[p].ptr[c] == q; p = t[p].link) t[p].ptr[c] = clo; n     t[q].link = t[cur].link = clo; n     las = cur; n   } n } sam1, sam2; n int n, tim, siz[N], fa[N], son[N], dep[N], dfn[N], top[N]; n int ed[N], ton[N], c[N]; n long long ans, d[N]; n vector<int> e[N]; n char S[N]; n struct int_cmp { n   bool operator()(int x, int y) const { return dfn[x] < dfn[y]; } n }; n set<int, int_cmp> s[N]; n void dfs_1(int x) { n   siz[x] = 1; n   dep[x] = dep[fa[x]] + 1; n   for (auto v : e[x]) { n     fa[v] = x; n     dfs_1(v); n     siz[x] += siz[v]; n     if (siz[v] > siz[son[x]]) son[x] = v; n   } n } n void dfs_2(int x, int tp) { n   top[x] = tp; n   dfn[x] = ++tim; n   if (son[x]) dfs_2(son[x], tp); n   for (auto v : e[x]) n     if (v != fa[x] && v != son[x]) dfs_2(v, v); n } n inline int lca(int u, int v) { n   while (top[u] != top[v]) { n     if (dep[top[u]] < dep[top[v]]) u ^= v ^= u ^= v; n     u = fa[top[u]]; n   } n   return dep[u] < dep[v] ? u : v; n } n inline int distan(int u, int v) { n   return sam2.t[u].len - sam2.t[lca(u, v)].len; n } n inline void insert(int x, int v) { n   auto it = s[x].upper_bound(v); n   auto it2 = it; n   if (it != s[x].begin()) it2--; n   if (it != s[x].end()) { n     if (it != s[x].begin()) { n       d[x] -= distan(*it, *it2); n     } else { n       d[x] -= sam2.t[*it].len + 1; n     } n   } n   if (it != s[x].begin()) { n     d[x] += distan(v, *it2); n   } else { n     d[x] += sam2.t[v].len + 1; n   } n   if (it != s[x].end()) { n     d[x] += distan(*it, v); n   } n   s[x].insert(v); n } n int main() { n   scanf( %s , S + 1); n   n = strlen(S + 1); n   sam1.init(); n   sam2.init(); n   for (int i = n; i; i--) { n     sam2.extend(S[i] -  a ); n     ed[i] = sam2.las; n   } n   for (int i = 1; i < sam2.siz; i++) e[sam2.t[i].link].push_back(i); n   dfs_1(0); n   dfs_2(0, 0); n   for (int i = 1; i <= n; i++) { n     sam1.extend(S[i] -  a ); n     if (i < n) insert(sam1.las, ed[i + 2]); n   } n   for (int i = 1; i < sam1.siz; i++) ton[sam1.t[i].len]++; n   for (int i = 1; i <= n; i++) ton[i] += ton[i - 1]; n   for (int i = 1; i < sam1.siz; i++) c[ton[sam1.t[i].len]--] = i; n   insert(0, ed[2]); n   for (int i = sam1.siz - 1, u; i >= 0; i--) { n     u = c[i]; n     if (!i) { n       ans += d[u]; n       break; n     } n     ans += (d[u] + 1) * (sam1.t[u].len - sam1.t[sam1.t[u].link].len); n     if (s[u].size() > s[sam1.t[u].link].size()) { n       swap(s[u], s[sam1.t[u].link]); n       swap(d[u], d[sam1.t[u].link]); n     } n     for (auto v : s[u]) insert(sam1.t[u].link, v); n   } n   cout << ans + 1 <<   n ; n   return (0 - 0); n } n


#include <bits/stdc++.h> n using namespace std; n long long c, t, n, I; n int main(void) { n   cin >> n; n   for (int i = int(1), ni = int(n); i <= ni; i++) cin >> t, c += (t == 1); n   I = 1; n   for (long long I1 = 1, I2 = 1, i = 2; i <= c; i++) n     I = (I1 + I2 * (i - 1) % 1000000007) % 1000000007, I2 = I1, I1 = I; n   for (int i = int(c + 1), ni = int(n); i <= ni; i++) I = (I * i) % 1000000007; n   cout << I; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[55][55][55][55]; n long long ara[55]; n double f[55]; n long long cnt(long long i, long long k, long long sum, long long zawad) { n   if (i == 0) return (k == 0 && sum == 0); n   if (k > i || sum < 0) return 0; n   if (dp[i][k][sum][zawad] != -1) return dp[i][k][sum][zawad]; n   dp[i][k][sum][zawad] = cnt(i - 1, k, sum, zawad); n   if (i != zawad) n     dp[i][k][sum][zawad] += cnt(i - 1, k - 1, sum - ara[i], zawad); n   return dp[i][k][sum][zawad]; n } n int main() { n   long long n, i, j, k, p, q, x, y; n   cin >> n; n   long long s = 0; n   for (i = 1; i <= n; i++) { n     cin >> ara[i]; n     s += ara[i]; n   } n   cin >> p; n   if (s <= p) { n     printf( %lld n , n); n     return 0; n   } n   f[1] = 1.0 / n; n   for (i = 2; i <= n; i++) f[i] = f[i - 1] * (i - 1) / (n - i + 1.0); n   memset(dp, -1, sizeof(dp)); n   double ans = 0; n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= n; j++) { n       for (k = p - ara[i] + 1; k <= p; k++) { n         q = cnt(n, j - 1, k, i); n         ans += q * (j - 1) * f[j]; n       } n     } n   } n   printf( %.10lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int MOD = 1e9 + 7; n const long double E = 1e-9; n unsigned char ccc; n bool _minus = false; n inline void read(int &n) { n   n = 0; n   _minus = false; n   while (true) { n     ccc = getchar(); n     if (ccc ==     || ccc ==   n ) break; n     if (ccc ==  - ) { n       _minus = true; n       continue; n     } n     n = n * 10 + ccc -  0 ; n   } n   if (_minus) n *= -1; n } n char wwww[12]; n int kkkk; n inline void write(int x) { n   kkkk = 0; n   if (!x) n     ++kkkk, wwww[kkkk] =  0 ; n   else n     while (x) { n       ++kkkk; n       wwww[kkkk] = char(x % 10 +  0 ); n       x /= 10; n     } n   for (int i = kkkk; i >= 1; --i) putchar(wwww[i]); n   putchar(  n ); n } n template <typename T> n inline T sqr(T t) { n   return t * t; n } n struct segment_tree { n  private: n   int *t, n; n   void build(int v, int l, int r) { n     t[v] = INT_MAX; n     if (l == r) { n       return; n     } n     int x = (l + r) >> 1; n     build(v << 1, l, x); n     build(v << 1 | 1, x + 1, r); n   } n   int get_min(int v, int tl, int tr, int l, int r) { n     if (l <= tl && tr <= r) return t[v]; n     if (tr < l || r < tl) return INT_MAX; n     int x = (tl + tr) >> 1; n     return min(get_min(v << 1, tl, x, l, r), n                get_min(v << 1 | 1, x + 1, tr, l, r)); n   } n   void upd(int v, int l, int r, int pos, int val) { n     if (l == r) { n       t[v] = val; n       return; n     } n     int x = (l + r) >> 1; n     if (pos <= x) n       upd(v << 1, l, x, pos, val); n     else n       upd(v << 1 | 1, x + 1, r, pos, val); n     t[v] = min(t[v << 1], t[v << 1 | 1]); n   } n  n  public: n   segment_tree(int n) { n     t = new int[n << 2]; n     this->n = n; n     build(1, 0, n - 1); n   } n   void upd(int pos, int val) { upd(1, 0, n - 1, pos, val); } n   int get_min(int l, int r) { n     if (r < l) return INT_MAX; n     return get_min(1, 0, n - 1, l, r); n   } n }; n const int MAX = 1e5 + 14; n vector<int> vec[MAX]; n bool is_root[MAX]; n int n, m; n int present[MAX]; n int parent[MAX]; n vector<int> ans; n int timer = 0; n int tin[MAX], tout[MAX], h[MAX]; n bool added[MAX], need_add[MAX]; n segment_tree *tree; n bool now[MAX]; n void add(int pos) { n   if (!added[pos]) { n     added[pos] = true; n     ans.push_back(pos); n   } n } n vector<int> q; n bool are_needed[MAX]; n int where[MAX]; n void dfs1(int pos, int hh) { n   for (int i = 0; i < (int)vec[pos].size(); i++) dfs1(vec[pos][i], hh + 1); n   if (are_needed[pos]) { n     where[pos] = q.size(); n     q.push_back(pos); n     ans.push_back(pos); n   } n } n void sorry() { n   cout << -1 <<   n ; n   exit(0); n } n set<pair<int, int> > w; n void dfs2(int pos) { n   if (are_needed[pos]) { n     w.insert(make_pair(where[pos], pos)); n   } n   for (int i = 0; i < (int)vec[pos].size(); i++) dfs2(vec[pos][i]); n   if (w.empty()) sorry(); n   if (w.begin()->second != present[pos]) sorry(); n   if (are_needed[pos]) { n     ::w.erase(make_pair(where[pos], pos)); n   } n } n int main() { n   cout.precision(3); n   cout << fixed; n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   srand(time(NULL)); n   cin >> n >> m; n   for (int i = 1; i <= n; i++) is_root[i] = true; n   for (int i = 0; i < m; i++) { n     int a, b; n     cin >> a >> b; n     vec[a].push_back(b); n     parent[b] = a; n     is_root[b] = false; n   } n   for (int i = 1; i <= n; i++) { n     cin >> present[i]; n     are_needed[present[i]] = true; n   } n   for (int i = 1; i <= n; i++) { n     if (is_root[i]) { n       timer = 0; n       dfs1(i, 1); n       dfs2(i); n     } n   } n   cout << ans.size() <<   n ; n   for (int i = 0; i < (int)ans.size(); i++) cout << ans[i] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n long long data[4 * 150005], lazy[4 * 150005], ret; n int ll, rr, n, m; n void change(int t, long long d) { data[t] = lazy[t] = d; } n void down(int t) { n   if (lazy[t]) { n     change(t * 2, lazy[t]); n     change(t * 2 + 1, lazy[t]); n     lazy[t] = 0; n   } n } n void modify(int t, int l, int r) { n   if (ll <= l && r <= rr) { n     change(t, ret); n     return; n   } n   int mid = (l + r) >> 1; n   down(t); n   if (ll <= mid) modify(t * 2, l, mid); n   if (rr > mid) modify(t * 2 + 1, mid + 1, r); n   data[t] = max(data[t * 2], data[t * 2 + 1]); n } n void query(int t, int l, int r) { n   if (ll <= l && r <= rr) { n     ret = max(ret, data[t]); n     return; n   } n   int mid = (l + r) >> 1; n   down(t); n   if (ll <= mid) query(t * 2, l, mid); n   if (rr > mid) query(t * 2 + 1, mid + 1, r); n } n void build(int t, int l, int r) { n   if (l == r) { n     scanf( %I64d , &data[t]); n     return; n   } n   int mid = (l + r) >> 1; n   build(t * 2, l, mid); n   build(t * 2 + 1, mid + 1, r); n   data[t] = max(data[t * 2], data[t * 2 + 1]); n } n int main() { n   scanf( %d , &n); n   build(1, 1, n); n   scanf( %d , &m); n   while (m--) { n     int h; n     scanf( %d%d , &rr, &h); n     ll = 1; n     ret = 0; n     query(1, 1, n); n     printf( %I64d n , ret); n     ret += h; n     modify(1, 1, n); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (2e5 + 5); n const int mod = 1e9 + 7; n long long int n, a[N], x; n vector<vector<int>> g; n long long int pow_mod_m(long long int a, long long int n) { n   if (!n) return 1; n   long long int pt = pow_mod_m(a, n / 2); n   pt *= pt; n   pt %= mod; n   if (n & 1) pt *= a, pt %= mod; n   return pt; n } n struct LCA { n   vector<int> height, euler, first, segtree; n   vector<bool> visited; n   int n; n   LCA(vector<vector<int>> &adj, int root = 0) { n     n = adj.size(); n     height.resize(n); n     first.resize(n); n     euler.reserve(2 * n); n     visited.assign(n, false); n     dfs(adj, root); n     int m = euler.size(); n     segtree.resize(4 * m); n     build(1, 0, m - 1); n   } n   void dfs(vector<vector<int>> &adj, int node, int h = 0) { n     visited[node] = 1; n     height[node] = h; n     first[node] = euler.size(); n     euler.push_back(node); n     for (auto to : adj[node]) { n       if (!visited[to]) { n         dfs(adj, to, h + 1); n         euler.push_back(node); n       } n     } n   } n   void build(int node, int b, int e) { n     if (b == e) n       segtree[node] = euler[b]; n     else { n       int m = (b + e) >> 1; n       build(node << 1, b, m); n       build(node << 1 | 1, m + 1, e); n       int l = segtree[node << 1], r = segtree[node << 1 | 1]; n       segtree[node] = (height[l] < height[r]) ? l : r; n     } n   } n   int query(int node, int b, int e, int L, int R) { n     if (b > R || e < L) return -1; n     if (b >= L and e <= R) return segtree[node]; n     int mid = (b + e) >> 1; n     int left = query(node << 1, b, mid, L, R); n     int right = query(node << 1 | 1, mid + 1, e, L, R); n     if (left == -1) return right; n     if (right == -1) return left; n     return (height[left] < height[right]) ? left : right; n   } n   int lca(int u, int v) { n     int left = first[u], right = first[v]; n     if (left > right) swap(left, right); n     return query(1, 0, euler.size() - 1, left, right); n   } n   int dist(int u, int v) { n     int lca_u_v = lca(u, v); n     return height[u] + height[v] - 2 * height[lca_u_v]; n   } n }; n long long int ceil(long long int m) { return x % m == 0 ? x / m : x / m + 1; } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int ts = 1; n   cin >> ts; n   while (ts--) { n     cin >> n >> x; n     bool flag = 0; n     for (long long int i = 0; i < n; ++i) { n       cin >> a[i]; n       if (x == a[i]) flag = 1; n     } n     if (flag) n       cout << 1 <<   n ; n     else n       cout << max(2ll, ceil(*max_element(a, a + n))) <<   n ; n   } n   return 0; n } n


#include  bits/stdc++.h  n #define Abra_Ka_Dabra ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); n typedef long long ll; n typedef unsigned long long ull; n typedef long double ld; n using namespace std; n const long long mod = 1e9 + 7; n const double PI = acos(-1); n const double EPS = 1e-12; n  n bool isEqual(ld a, ld b) { n  return abs(a - b) <= EPS; n } n bool isGreater(ld a, ld b) { n  return a > b + EPS; n } n bool isSmaller(ld a, ld b) { n  return a + EPS < b; n } n  n  n int main() { n #ifdef Half_Blood_Prince n  freopen( in.txt ,  r , stdin); n  freopen( out.txt ,  w , stdout); n #endif n  n  Abra_Ka_Dabra n  n  int t; n  cin >> t; n  while (t--) { n   ll n, m, x; n   cin >> n >> m >> x; n   ll j = (x + (n - 1)) / n; n   ll i = x % n; n   i = (i == 0 ? n : i); n   cout << (i - 1)*m + j <<   n ; n  } n  n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const long long Mod = 1000000007LL; n const int N = 2e5 + 10; n const long long Inf = 2242545357980376863LL; n const long long Log = 30; n struct node { n   int lz, mn, cnt; n   long long sum; n   node() { lz = 0, mn = 0, cnt = 0, sum = 0; } n }; n node base; n node seg[N << 2]; n node Merge(node& a, node& b) { n   node res; n   res.mn = min(a.mn, b.mn); n   if (res.mn == a.mn) { n     res.cnt += a.cnt; n     res.sum += a.sum; n   } n   if (res.mn == b.mn) { n     res.cnt += b.cnt; n     res.sum += b.sum; n   } n   return res; n } n void Apply(int id, int x) { n   seg[id].lz += x; n   seg[id].mn += x; n } n void Shift(int id) { n   Apply(id << 1, seg[id].lz); n   Apply(id << 1 | 1, seg[id].lz); n   seg[id].lz = 0; n } n void Build(int id, int L, int R) { n   if (L + 1 == R) { n     seg[id].mn = 0; n     seg[id].lz = 0; n     seg[id].sum = L; n     seg[id].cnt = 1; n     return; n   } n   int mid = (L + R) >> 1; n   Build(id << 1, L, mid); n   Build(id << 1 | 1, mid, R); n   seg[id] = Merge(seg[id << 1], seg[id << 1 | 1]); n } n void Add(int id, int l, int r, int x, int L, int R) { n   if (r <= L || R <= l) return; n   if (l <= L && R <= r) { n     Apply(id, x); n     return; n   } n   Shift(id); n   int mid = (L + R) >> 1; n   Add(id << 1, l, r, x, L, mid); n   Add(id << 1 | 1, l, r, x, mid, R); n   seg[id] = Merge(seg[id << 1], seg[id << 1 | 1]); n } n int n, a[N], b[N]; n vector<pair<pair<long long, long long>, n             pair<pair<long long, long long>, pair<long long, long long>>>> n     V; n vector<pair<pair<long long, long long>, int>> E[N]; n int ps[N]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int m; n   cin >> m >> n; n   for (int i = 0; i < m; i++) { n     cin >> a[i] >> b[i]; n     a[i]--; n     ps[a[i]]++; n     ps[b[i]]--; n     V.push_back({{0, 0}, {{a[i], b[i] + 1}, {a[i], b[i] + 1}}}); n     V.push_back({{1, 1}, {{a[i], b[i] + 1}, {a[i], b[i] + 1}}}); n     V.push_back({{0, a[i] & 1}, {{0, a[i]}, {a[i] + 1, b[i] + 1}}}); n     V.push_back({{1, a[i] & 1}, {{0, a[i]}, {a[i] + 1, b[i] + 1}}}); n     V.push_back({{b[i] & 1, 0}, {{a[i], b[i]}, {b[i] + 1, n + 1}}}); n     V.push_back({{b[i] & 1, 1}, {{a[i], b[i]}, {b[i] + 1, n + 1}}}); n     if ((a[i] - b[i]) % 2 == 0) { n       V.push_back({{0, 0}, {{0, a[i]}, {b[i] + 1, n + 1}}}); n       V.push_back({{0, 1}, {{0, a[i]}, {b[i] + 1, n + 1}}}); n       V.push_back({{1, 0}, {{0, a[i]}, {b[i] + 1, n + 1}}}); n       V.push_back({{1, 1}, {{0, a[i]}, {b[i] + 1, n + 1}}}); n     } n   } n   for (int i = 1; i < n; i++) ps[i] += ps[i - 1]; n   long long ans = 0, cnt = 0; n   for (int i = 0; i < n; i++) { n     if (ps[i] == 0) n       cnt++; n     else n       cnt = 0; n     ans -= (cnt * (cnt + 1)) / 2ll; n   } n   for (int b1 = 0; b1 < 2; b1++) n     for (int b2 = 0; b2 < 2; b2++) { n       Build(1, 0, n + 1); n       for (int i = 0; i < N; i++) E[i].clear(); n       for (auto& rec : V) { n         if (rec.first != pair<long long, long long>(b1, b2)) continue; n         E[rec.second.first.first].push_back({rec.second.second, 1}); n         E[rec.second.first.second].push_back({rec.second.second, -1}); n       } n       for (int i = 0; i <= n; i++) { n         if ((i & 1) != b2) Add(1, i, i + 1, 1, 0, n + 1); n       } n       for (int i = 0; i < n; i++) { n         Add(1, i, i + 1, 1, 0, n + 1); n         for (auto& x : E[i]) { n           Add(1, x.first.first, x.first.second, x.second, 0, n + 1); n         } n         assert(seg[1].mn >= 0); n         if (seg[1].mn == 0 && ((i & 1) == b1)) { n           ans += seg[1].sum - (1ll * seg[1].cnt * i); n         } n       } n     } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool compa(int i, int j) { return i > j; } n long long int power(long long int a, long long int n) { n   if (n == 0) n     return 1; n   else if (n % 2) { n     long long int d1 = power(a, n / 2); n     return d1 * d1 * a; n   } else { n     long long int d1 = power(a, n / 2); n     return d1 * d1; n   } n } n int find_pivot(int a[], int l, int r) { n   while (l < r) { n     int mid = (l + r) / 2; n     if (a[l] > a[l + 1]) { n       return l; n     } else if (a[r] > a[r + 1]) n       return r; n     else if (a[mid] > a[mid + 1]) { n       return mid; n     } else if (a[mid] > a[r]) { n       l = mid + 1; n       cout <<  l   << l; n     } else if (a[mid] < a[r]) { n       r = mid - 1; n       cout <<  r   << r; n     } n   } n   return -1; n } n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   bool ans[4]; n   for (int i = 0; i < 4; i++) ans[i] = true; n   for (int i = 0; s[i] !=   0 ; i++) { n     if (s[i] ==  1 ) { n       ans[0] = false; n       ans[2] = false; n     } n     if (s[i] ==  2 ) { n       ans[0] = false; n     } n     if (s[i] ==  3 ) { n       ans[0] = false; n       ans[3] = false; n     } n     if (s[i] ==  4 ) { n       ans[2] = false; n     } n     if (s[i] ==  6 ) { n       ans[3] = false; n     } n     if (s[i] ==  7 ) { n       ans[1] = false; n       ans[2] = false; n     } n     if (s[i] ==  9 ) { n       ans[1] = false; n       ans[3] = false; n     } n     if (s[i] ==  0 ) { n       ans[1] = false; n       ans[2] = false; n       ans[3] = false; n     } n   } n   if (ans[0] || ans[1] || ans[2] || ans[3]) n     cout <<  NO ; n   else n     cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN = 1e5, mxV = 7001; n int n, q, qt, xi, yi, zi; n bitset<mxV> in[mxV], cq[mxV], ms[mxN], c; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   for (int i = 1; i < mxV; ++i) { n     for (int j = 0; (j += i) < mxV;) in[j][i] = 1; n     for (int j = i; j < mxV; ++j) { n       if (j == i ? !c[j] : c[j]) { n         for (int k = 0; (k += j) < mxV;) c.flip(k); n         cq[i][j] = 1; n       } n     } n     c.reset(); n   } n   cin >> n >> q; n   while (q--) { n     cin >> qt; n     if (qt == 1) { n       cin >> xi >> yi, --xi; n       ms[xi] = in[yi]; n     } else if (qt == 2) { n       cin >> xi >> yi >> zi, --xi, --yi, --zi; n       ms[xi] = ms[yi] ^ ms[zi]; n     } else if (qt == 3) { n       cin >> xi >> yi >> zi, --xi, --yi, --zi; n       ms[xi] = ms[yi] & ms[zi]; n     } else { n       cin >> xi >> yi, --xi; n       cout << ((ms[xi] & cq[yi]).count() & 1); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mtx[2000005]; n int main() { n   int tc, cs = 0; n   int i, j, k; n   int len; n   cin >> len; n   for (i = 0; i < len; i++) cin >> mtx[i]; n   sort(mtx, mtx + len); n   long long sum = 0; n   i = 0; n   while (1) { n     int n = pow(4, i); n     if (n > len) break; n     for (j = len - 1; j >= len - n; j--) { n       sum += mtx[j]; n     } n     i++; n   } n   cout << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b; n bool bad(int x, int y) { n   return abs(x + y) % (2 * a) == 0 || abs(x - y) % (2 * b) == 0; n } n void reorder(int& x, int& y) { n   int xx = (x - y) / 2; n   int yy = (x + y) / 2; n   x = xx; n   y = yy; n } n void norm(int& x, int& y) { n   if ((x + y) % 2 == 0) return; n   if (!bad(x + 1, y)) n     x++; n   else if (!bad(x - 1, y)) n     x--; n   else if (!bad(x, y + 1)) n     y++; n   else if (!bad(x, y - 1)) n     y--; n } n int torect(int x, int a) { n   if (x <= 0) n     return x / a; n   else n     return x / a + 1; n } n int main() { n   int x1, y1, x2, y2; n   cin >> a >> b >> x1 >> y1 >> x2 >> y2; n   norm(x1, y1); n   norm(x2, y2); n   reorder(x1, y1); n   reorder(x2, y2); n   x1 = torect(x1, b); n   x2 = torect(x2, b); n   y1 = torect(y1, a); n   y2 = torect(y2, a); n   int dx = abs(x1 - x2); n   int dy = abs(y1 - y2); n   int ans = max(dx, dy); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> a; n vector<long long int> b; n long long int fa(int l, int r) { n   long long int ret = 0; n   for (int i = l; i <= r; ++i) ret |= a[i]; n   return ret; n } n long long int fb(int l, int r) { n   long long int ret = 0; n   for (int i = l; i <= r; ++i) ret |= b[i]; n   return ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   long long int aux, ans = 0; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> aux; n     a.push_back(aux); n   } n   for (int i = 0; i < n; ++i) { n     cin >> aux; n     b.push_back(aux); n   } n   for (int i = 0; i < n; ++i) n     for (int j = i; j < n; ++j) { n       ans = max(ans, fa(i, j) + fb(i, j)); n     } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n long long ln[10000], fac[15]; n int cnt, ans, suf; n int tem[15]; n void input() { scanf( %d%d , &n, &k); } n void dfs(long long x) { n   if (x > 10000000000LL) return; n   ln[cnt++] = x; n   dfs(x * 10 + 4); n   dfs(x * 10 + 7); n } n void make_lucky_number() { n   cnt = 0; n   dfs(4); n   dfs(7); n   sort(ln, ln + cnt); n } n void get_my_suf(int s) { n   bool used[15]; n   memset(used, false, sizeof(used)); n   for (int i = 1; i <= s; ++i) { n     int u = (k - 1) / fac[s - i]; n     k -= (u * fac[s - i]); n     u++; n     for (int j = 1; j <= s; ++j) { n       if (!used[j]) u--; n       if (u == 0) { n         used[tem[i] = j] = true; n         break; n       } n     } n   } n } n bool check_lucky(int x) { n   while (x) { n     if (x % 10 != 4 && x % 10 != 7) return false; n     x /= 10; n   } n   return true; n } n void solve() { n   fac[0] = fac[1] = 1; n   for (int i = 2; i <= 13; ++i) fac[i] = fac[i - 1] * i; n   if (n <= 13 && k > fac[n]) { n     puts( -1 ); n     return; n   } n   for (int i = 0; i < cnt; ++i) n     if (n - 13 < ln[i]) { n       ans = i; n       break; n     } n   suf = min(n, 13); n   get_my_suf(suf); n   for (int i = 1; i <= suf; ++i) n     if (check_lucky(n - suf + tem[i]) && check_lucky(n - suf + i)) ans++; n   printf( %d n , ans); n } n int main() { n   input(); n   make_lucky_number(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; n int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; n void mad(long long &a, long long b) { n   a = (a + b) % 1000000007; n   if (a < 0) a += 1000000007; n } n long long gcd(long long a, long long b) { n   if (a > b) swap(a, b); n   if (!a) return b; n   return gcd(b % a, a); n } n int dp[1 << 22]; n void solve() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < 1 << 22; i++) dp[i] = -1; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     dp[a[i]] = i; n   } n   for (int i = 0; i < 1 << 22; i++) { n     if (dp[i] == -1) continue; n     for (int j = 0; j < 22; j++) { n       if (!(i & (1 << j))) dp[i | (1 << j)] = dp[i]; n     } n   } n   for (int i = 0; i < n; i++) { n     int id = dp[(1 << 22) - 1 - a[i]]; n     if (id == -1) n       cout << -1 <<    ; n     else n       cout << a[id] <<    ; n   } 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--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (int)1e9; n const double pi = acos(-1.0); n const double eps = 1e-9; n long long x, second, res, xx, first; n int main() { n   cin >> second >> x; n   xx = x; n   if ((second & 1) != (x & 1)) { n     cout << 0; n     return 0; n   } n   res = 1; n   while (xx) { n     if (xx & 1) res *= 2; n     xx /= 2; n   } n   if (second == x) { n     cout << res - 2; n     return 0; n   } n   first = second - x; n   if (first < 0) { n     cout << 0; n     return 0; n   } n   for (int i = 0; i <= 50; i++) { n     long long z = (1ll << (i)); n     long long zz = (1ll << (i + 1)); n     if ((x & z) > 0 && (first & zz) > 0) { n       cout << 0; n       return 0; n     } n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 10050; n int a[maxn], b[maxn]; n bool cmp(int a, int b) { return a > b; } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, k; n   while (cin >> n >> k) { n     memset(a, 0, sizeof(a)); n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     for (int i = 0; i < k; i++) { n       cin >> b[i]; n     } n     sort(b, b + n, cmp); n     int tmp = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] == 0) { n         a[i] = b[tmp++]; n       } n     } n     bool flag = false; n     for (int i = 1; i < n; i++) { n       if (a[i] - a[i - 1] < 0) { n         flag = true; n         break; n       } n     } n     if (flag) { n       cout <<  Yes  << endl; n     } else { n       cout <<  No  << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool flag = false; n int n, m, K; n int up[2005][2005], down[2005][2005]; n int Find(int p[], int x) { return p[x] != x ? p[x] = Find(p, p[x]) : x; } n void check(int x, int y, int i, int &g, int &h, int &L) { n   if (i < 1 || i > n) return; n   int j; n   j = Find(up[i], y); n   if (j != 0) { n     int d = abs(x - i) + abs(y - j); n     if (d < L || d == L && (!flag && (i < g || i == g && j < h) || n                             flag && (j < h || j == h && i < g))) n       L = d, g = i, h = j; n   } n   j = Find(down[i], y); n   if (j != m + 1) { n     int d = abs(x - i) + abs(y - j); n     if (d < L || d == L && (!flag && (i < g || i == g && j < h) || n                             flag && (j < h || j == h && i < g))) n       L = d, g = i, h = j; n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &K); n   if (n > m) { n     swap(n, m); n     flag = true; n   } n   for (int i = 1; i <= n; i++) n     for (int j = 0; j <= m + 1; j++) up[i][j] = down[i][j] = j; n   while (K--) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (flag) swap(x, y); n     int g = -1, h = -1, L = 0x7fffffff; n     for (int i = 0; i <= L; i++) { n       check(x, y, x - i, g, h, L); n       check(x, y, x + i, g, h, L); n     } n     up[g][h] = h - 1; n     down[g][h] = h + 1; n     if (flag) swap(g, h); n     printf( %d %d n , g, h); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int32_t MOD = 1000000007; n struct WEdge { n   int32_t u, v; n   int w; n   bool operator<(const WEdge& other) { return w < other.w; } n   WEdge(int32_t u, int32_t v, int w) : u(u), v(v), w(w) {} n   WEdge() {} n   void in() { cin >> u >> v >> w; } n   void trace() { n     if (0) cout << u <<     << v <<     << w <<   n ; n   } n }; n struct Edge { n   int32_t u, v; n   Edge(int32_t u, int32_t v) : u(u), v(v) {} n   void in() { cin >> u >> v; } n }; n struct UnionSet { n   map<int32_t, pair<int32_t, int32_t> > sets; n   void makeSet(int32_t x) { sets.emplace(x, make_pair(x, 0)); } n   void init(int32_t a, int32_t b) { n     for (int x = (a); x < (b); ++x) makeSet(x); n   } n   int find(int32_t x) { n     if (sets[x].first == x) n       return x; n     else n       return sets[x].first = find(sets[x].first); n   } n   void union_both(int32_t x, int32_t y) { n     int32_t parentX = find(x), parentY = find(y); n     int32_t rankX = sets[parentX].second, rankY = sets[parentY].second; n     if (parentX == parentY) n       return; n     else if (rankX < rankY) n       sets[parentX].first = parentY; n     else n       sets[parentY].first = parentX; n     if (rankX == rankY) sets[parentX].second++; n   } n }; n int32_t add(int32_t a, int32_t b) { n   int32_t r = a + b; n   if (r >= MOD) r -= MOD; n   return r; n } n int32_t sub(int32_t a, int32_t b) { n   int32_t r = a - b; n   if (r < 0) r += MOD; n   return r; n } n int32_t mul(int32_t a, int32_t b) { return (a * 1ll * b) % MOD; } n int32_t pow_mod(int32_t a, int64_t n) { n   if (n == 0) n     return 1; n   else { n     int32_t tmp = pow_mod(a, n / 2); n     tmp = mul(tmp, tmp); n     if (n % 2 == 0) n       return tmp; n     else n       return mul(tmp, a); n   } n } n int32_t fact_mod(int32_t n) { n   int32_t r = 1; n   for (int32_t k = 1; k <= n; k++) r = mul(r, k); n   return r; n } n vector<int> children[200001]; n int a[200001]; n int64_t S[200001]; n int64_t T[200001]; n void dfs_init(int u, int pred) { n   S[u] = a[u]; n   T[u] = 0; n   for (auto v : children[u]) { n     if (v != pred) { n       dfs_init(v, u); n       S[u] += S[v]; n       T[u] += T[v] + S[v]; n     } n   } n } n int64_t M = -1; n void dfs_maxt(int u, int pred, int64_t sa, int64_t ta) { n   M = max(M, ta + T[u]); n   sa += a[u]; n   ta += sa; n   int64_t ts = 0, tt = 0; n   for (auto v : children[u]) { n     if (v != pred) { n       ts += S[v]; n       tt += 2ll * S[v] + T[v]; n     } n   } n   for (auto v : children[u]) { n     if (v != pred) { n       dfs_maxt(v, u, sa + ts - S[v], ta + tt - T[v] - 2ll * S[v]); n     } n   } n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   for (int i = (0); i < (n); ++i) cin >> (a + 1)[i]; n   int u, v; n   for (int i = (0); i < (n - 1); ++i) { n     cin >> u >> v; n     children[u].push_back(v); n     children[v].push_back(u); n   } n   dfs_init(1, -1); n   dfs_maxt(1, -1, 0, 0); n   cout << M <<   n ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n template <class T> n T gcd(T a, T b) { n   return ((b == 0) ? a : gcd(b, a % b)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n, x; n   cin >> n >> x; n   n %= 6; n   for (int i = 0; i < (int)(3); i++) { n     vector<int> a(3, 0); n     a[i] = 1; n     for (int j = 0; j < (int)(n); j++) { n       if (j % 2 == 0) n         swap(a[0], a[1]); n       else n         swap(a[1], a[2]); n     } n     if (a[x]) { n       cout << i << endl; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int t, l, r, d, m, n, i; n   scanf( %d , &t); n   for (i = 0; i < t; i++) { n     scanf( %d %d %d , &l, &r, &d); n     if (l > d || r < d) { n       printf( %d n , d); n     } else { n       m = (r / d) + 1; n       n = d * m; n       printf( %d n , n); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ,  unroll-loops ,  no-stack-protector ) n using namespace std; n template <class T> n using minheap = priority_queue<T, vector<T>, greater<T>>; n template <typename T> n void setmax(T& a, T b) { n   a = max(a, b); n }; n template <typename T> n void setmin(T& a, T b) { n   a = min(a, b); n }; n const int N = 3010; n int n; n vector<int> g[N]; n long long sz[N][N]; n int par[N][N]; n long long dp[N][N]; n void dfs(int root, int x, int p = 0) { n   sz[root][x]++; n   par[root][x] = p; n   for (int y : g[x]) { n     if (y == p) continue; n     dfs(root, y, x); n     sz[root][x] += sz[root][y]; n   } n } n long long solve(int u, int v) { n   if (dp[u][v] != -1) return dp[u][v]; n   if (u > v) return dp[u][v] = solve(v, u); n   if (u == v) return dp[u][v] = 0; n   return dp[u][v] = sz[u][v] * sz[v][u] + n                     max(solve(par[v][u], v), solve(u, par[u][v])); 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 - 1; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) { n     dfs(i, i); n   } n   memset(dp, -1, sizeof dp); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       setmax(ans, solve(i, j)); n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int s, x, y, a, b; n   cin >> s >> x >> y >> a >> b; n   if ((x * s) + (2 * a) < (y * s) + (2 * b)) { n     cout <<  First ; n     return 0; n   } n   if ((x * s) + (2 * a) > (y * s) + (2 * b)) { n     cout <<  Second ; n     return 0; n   } n   if ((x * s) + (2 * a) == (y * s) + (2 * b)) { n     cout <<  Friendship ; n     return 0; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[1111]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, ans = 0; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   sort(a + 1, a + 1 + n); n   for (long long i = 1; i < n; i++) { n     ans = ans + (a[i + 1] - a[i] - 1); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long t, n; n string s; n int main() { n   cin >> t; n   for (int x = 1; x <= t; x++) { n     cin >> n; n     cin >> s; n     string ans; n     long long v = 0; n     for (int i = 0; i < s.length(); i++) { n       ans += s[i + (v++)]; n       if (i == n - 1) break; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std::chrono; n bool prime[2000005]; n vector<int> myarr; n void myprime(int n) { n   for (long int i = 0; i < n; i++) prime[i] = true; n   for (int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n   for (int p = 2; p <= n; p++) n     if (prime[p]) myarr.push_back(p); n } n int gcd(int a, int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n void swap(int &a, int &b) { n   int temp = a; n   a = b; n   b = temp; n } n int max(int a, int b) { return (a > b ? a : b); } n vector<long long> a(15); n void moj(long long n) {} n bool cmp(pair<string, int> p1, pair<string, int> p2) { n   if (p2.second < p1.second) return true; n   return false; n } n void solve() { n   int n; n   cin >> n; n   map<string, int> ms; n   vector<string> vs(n); n   for (long int i = 0; i < n; i++) cin >> vs[i]; n   int rr = 1; n   for (long int i = 0; i < n; i++) { n     string s; n     s = vs[i]; n     ms[s] = rr; n     rr++; n   } n   vector<pair<string, int> > sss; n   for (auto &i : ms) { n     sss.push_back(i); n   } n   sort(sss.begin(), sss.end(), cmp); n   for (auto i : sss) { n     cout << i.first <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000005; n struct ky { n   int u, val; n   bool operator<(const ky &p) const { return val < p.val; } n } a[N], b[N]; n int n, ca, cb, ci, vali; n bool tg[N]; n inline void fr(int &num) { n   num = 0; n   char c = getchar(); n   int p = 1; n   while (c <  0  || c >  9 ) c ==  -  ? p = -1, c = getchar() : c = getchar(); n   while (c >=  0  && c <=  9 ) num = num * 10 + c -  0 , c = getchar(); n   num *= p; n } n int main() { n   fr(n); n   for (register int i = 1; i <= n; ++i) n     fr(ci), fr(vali), ci ? a[++ca] = (ky){i, vali} : b[++cb] = (ky){i, vali}; n   sort(a + 1, a + 1 + ca), sort(b + 1, b + 1 + cb); n   register int i = 1, j = 1, minn; n   while (i <= ca && j <= cb) n     minn = (a[i].val < b[j].val ? a[i].val : b[j].val), n     printf( %d %d %d n , a[i].u, b[j].u, minn), a[i].val -= minn, n     b[j].val -= minn, tg[a[i].u] = tg[b[j].u] = true, !a[i].val ? ++i : ++j; n   while (i <= ca) { n     if (!tg[a[i].u]) printf( %d %d 0 n , a[i].u, b[1].u), tg[a[i].u] = 1; n     ++i; n   } n   while (j <= cb) { n     if (!tg[b[j].u]) printf( %d %d 0 n , a[1].u, b[j].u), tg[b[j].u] = 1; n     ++j; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   struct node* left; n   struct node* right; n   int vl; n   node() { n     vl = 0; n     left = NULL; n     right = NULL; n   } n }; n int a[300005]; n int ans = 0; n void query(node* n, int v, int i, int j) { n   if (n == NULL || j == 0) return; n   int no = (1 << (j - 1)); n   node* l2 = n->left; n   node* r2 = n->right; n   int l1 = 0, r1 = 0; n   if (l2 != NULL) l1 = l2->vl; n   if (r2 != NULL) r1 = r2->vl; n   j--; n   if ((v & (1 << j)) == 0) { n     if (l1 == no) { n       ans += (1 << (j)); n       query(n->right, v, i / 2, j); n       return; n     } n     query(n->left, v, i / 2, j); n     return; n   } n   if (r1 == no) { n     ans += (1 << (j)); n     query(n->left, v, i / 2, j); n     return; n   } n   query(n->right, v, i / 2, j); n   return; n } n void insert(node* n, int val, int i, int j) { n   if (j == 0) return; n   j--; n   if ((val & (1 << j)) == 0) { n     if (n->left == NULL) { n       node* cur = new node(); n       n->left = cur; n     } n     insert(n->left, val, i / 2, j); n     return; n   } n   if (n->right == NULL) { n     node* cur = new node(); n     n->right = cur; n   } n   insert(n->right, val, i / 2, j); n   return; n } n int dfs(node* n) { n   if (n->left == NULL && n->right == NULL) { n     n->vl = 1; n     return 1; n   } n   int w = 0; n   int u = 0; n   if (n->left != NULL) { n     w += dfs(n->left); n     u++; n   } n   if (n->right != NULL) { n     w += dfs(n->right); n     u++; n   } n   n->vl = w; n   return w; n } n int main() { n   int n, m; n   cin >> n >> m; n   node* h = new node(); n   node* w1 = h; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     h = w1; n     insert(h, a[i], (1 << 20) - 1, 19); n   } n   int x = 0; n   h = w1; n   dfs(h); n   for (int i = 1; i <= m; i++) { n     int x1; n     cin >> x1; n     x = x ^ x1; n     ans = 0; n     h = w1; n     query(h, x, (1 << 20) - 1, 19); n     cout << ans <<   n ; n   } n } n


#include <iostream> n #include <set> n #include <vector> n #include <algorithm> n using namespace std; n typedef long long ll; n  n vector<int> res; n vector<pair<int, int>> edges; n vector<vector<int>> v; n int n, k; n  n int buildTree(set<int> idx, int rootSalary) { n  if (idx.size() == 1) { return *idx.begin(); } n  int curRoot = k++; res.push_back(rootSalary); n  n  while (!idx.empty()) { n   int cur = *idx.begin(), nextRootSalary = 0; n   set<int> newIDX; newIDX.insert(cur); n  n   for (auto& x : idx) if(cur != x && v[cur][x] < rootSalary) { n    nextRootSalary = max(nextRootSalary, v[cur][x]); n    newIDX.insert(x); n   } n   for (auto& x : newIDX) { idx.erase(x); } n  n   int child = buildTree(newIDX, nextRootSalary); n   edges.push_back({ child, curRoot }); n  } n  n  return curRoot; n } n  n int main() { n  ios::sync_with_stdio(0); n  cin.tie(0), cout.tie(0); n  n  cin >> n; k = n; v.resize(n, vector<int>(n)); res.resize(n); int ma = 0; n  for (auto& x : v) for (auto& y : x) { cin >> y; ma = max(ma, y); } n  for (int i = 0; i < n; i++) res[i] = v[i][i]; n  n  set<int> idx; for (int i = 0; i < n; i++) idx.insert(i); n  int head = buildTree(idx, ma); n  n  cout << res.size() <<   n ; for (auto& x : res)  n   cout << x <<    ; cout <<   n ; n  cout << head + 1 <<   n ; for (auto& e : edges)  n   cout << ++e.first <<     << ++e.second <<   n ; n  n  cin.ignore(2); return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int N = 200200; n int n; n int a[N]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   int cnt_0 = 0, cnt_neg = 0, pos = -1; n   vector<int> used(n); n   for (int i = 0; i < n; i++) { n     if (a[i] == 0) { n       used[i] = 1; n       cnt_0++; n     } n     if (a[i] < 0) { n       cnt_neg++; n       if (pos == -1 || abs(a[pos]) > abs(a[i])) { n         pos = i; n       } n     } n   } n   if (cnt_neg & 1) { n     used[pos] = 1; n   } n   if (cnt_0 == n || (cnt_0 == n - 1 && cnt_neg == 1)) { n     for (int i = 0; i < n - 1; i++) { n       cout << 1 <<     << i + 1 <<     << i + 2 <<   n ; n     } n     return 0; n   } n   int lst = -1; n   for (int i = 0; i < n; i++) { n     if (!used[i]) continue; n     if (lst != -1) { n       cout << 1 <<     << lst + 1 <<     << i + 1 <<   n ; n     } n     lst = i; n   } n   if (lst != -1) cout << 2 <<     << lst + 1 <<   n ; n   lst = -1; n   for (int i = 0; i < n; i++) { n     if (used[i]) continue; n     if (lst != -1) { n       cout << 1 <<     << lst + 1 <<     << i + 1 <<   n ; n     } n     lst = i; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   string s; n   int n, x = 0, ans; n   cin >> s; n   n = s.length(); n   for (int i = 0; i < n; i++) { n     x = x * 10 + (s[i] -  0 ); n     x = x % 4; n   } n   ans = (1 + (int)pow(2, x) + (int)pow(3, x) + (int)pow(4, x)) % 5; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   int N, ans = 0; n   string s; n   cin >> N >> s; n   for (int i = 0, j; i < N;) { n     if (s[i] ==  ? ) { n       for (j = i; j < N && s[j] ==  ? ; j++) n         ; n       if (j - i > 1 || !i || j == N || s[i - 1] == s[j]) ans = 1; n       i = j; n     } else { n       if (i && s[i - 1] == s[i]) { n         cout <<  No n ; n         return 0; n       } n       i++; n     } n   } n   if (ans) n     cout <<  Yes n ; n   else n     cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lint = long long int; n constexpr int MOD = 1000000007; n int main() { n   int T; n   cin >> T; n   for (int t = (0), t_end_ = (T); t < t_end_; t++) { n     int r, c, k; n     cin >> r >> c >> k; n     vector<string> map(r); n     for (int i = (0), i_end_ = (r); i < i_end_; i++) { n       cin >> map[i]; n     } n     int rice = 0; n     for (int i = (0), i_end_ = (r); i < i_end_; i++) { n       for (int j = (0), j_end_ = (c); j < j_end_; j++) { n         if (map[i][j] ==  R ) { n           rice++; n         } n       } n     } n     int a = rice / k; n     int b = rice % k; n     vector<int> ricelist(k, a); n     for (int i = (0), i_end_ = (b); i < i_end_; i++) { n       ricelist[i]++; n     } n     vector<vector<char>> ans(r, vector<char>(c)); n     int nowk = 0; n     for (int i = (0), i_end_ = (r); i < i_end_; i++) { n       if (i % 2 == 0) { n         for (int j = (0), j_end_ = (c); j < j_end_; j++) { n           if (nowk >= 36) { n             ans[i][j] =  a  + (nowk - 36); n           } else if (nowk >= 10) { n             ans[i][j] =  A  + (nowk - 10); n           } else { n             ans[i][j] =  0  + nowk; n           } n           if (map[i][j] ==  R ) { n             ricelist[nowk]--; n           } n           if (ricelist[nowk] == 0 && nowk != k - 1) { n             nowk++; n           } n         } n       } else { n         for (int j = (c)-1, j_begin_ = (0); j >= j_begin_; j--) { n           if (nowk >= 36) { n             ans[i][j] =  a  + (nowk - 36); n           } else if (nowk >= 10) { n             ans[i][j] =  A  + (nowk - 10); n           } else { n             ans[i][j] =  0  + nowk; n           } n           if (map[i][j] ==  R ) { n             ricelist[nowk]--; n           } n           if (ricelist[nowk] == 0 && nowk != k - 1) { n             nowk++; n           } n         } n       } n     } n     for (int i = (0), i_end_ = (r); i < i_end_; i++) { n       for (int j = (0), j_end_ = (c); j < j_end_; j++) { n         cout << ans[i][j]; n       } n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline void boost() { n   ios_base::sync_with_stdio(); n   cin.tie(0); n   cout.tie(0); n } n const long long maxn = 1e5 + 123; n const long long inf = 1e9 + 123; n const long long mod = 1e9 + 7; n const double infm = 1e-9; n const double pi = acos(-1); n int a[maxn]; n bool used[maxn]; n int main() { n   boost(); n   int n, k; n   cin >> n >> k; n   int p = 0, m = 0; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   int mx = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (abs(j - i) % k == 0) { n         used[j] = 1; n       } n     } n     int sum = 0, sum1 = 0; n     for (int j = 1; j <= n; j++) { n       if (used[j] == 0) { n         if (a[j] == 1) { n           sum++; n         } else { n           sum1++; n         } n       } n       used[j] = 0; n     } n     mx = max(mx, abs(sum - sum1)); n   } n   cout << mx; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n double ans = 0; n map<double, double> F; n void insert(double dist, double s) { n   double need = dist / (s + 1); n   F[s] += need; n   if (s > 1) F[s] += dist / (s - 1); n   ans += need; n   while (need > 1e-9) { n     map<double, double>::iterator it = --F.end(); n     double y = it->first; n     double dlt = min(need, F[y]); n     F[y] -= dlt; n     need -= dlt; n     ans += dlt / y; n     if (F[y] < 1e-9) { n       F.erase(it); n     } n   } n } n int n, L, ls; n int main() { n   scanf( %d%d , &n, &L); n   int l, r; n   double s; n   while (n--) { n     scanf( %d%d%lf , &l, &r, &s); n     insert(l - ls, 1); n     insert(r - l, s + 1); n     ls = r; n   } n   insert(L - ls, 1); n   printf( %.10lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> arr[100100]; n int dist[100100]; n map<int, int> m1; n void dfs(int curr, int par) { n   dist[curr] = dist[par] + 1; n   if (curr == par) { n     dist[curr] = 0; n   } n   for (auto x : arr[curr]) { n     if (x != par) { n       dfs(x, curr); n     } n   } n } n int main() { n   int n; n   cin >> n; n   for (int i = 2; i <= n; i++) { n     int x; n     cin >> x; n     arr[i].push_back(x); n     arr[x].push_back(i); n   } n   dfs(1, 1); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     m1[dist[i]]++; n   } n   for (auto it = m1.begin(); it != m1.end(); it++) { n     ans += (it->second) % 2; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int i, k, b, n, m, r = 0, x = 0, y = 0, z = 0, h = 0, l; n   string s; n   cin >> s; n   n = s.size(); n   int a[n]; n   for (i = 0; i < n; i++) { n     if (s[i] ==  ( ) n       x++; n     else if (s[i] ==  )  && x > 0) n       x--; n     else if (s[i] ==  ) ) n       z = 1; n     else if (s[i] ==  #  && x > 0) { n       a[y] = 1; n       y++; n       x--; n       k = i; n       l = x; n     } else if (s[i] ==  # ) n       z = 1; n   } n   x = l; n   if (y > 0) { n     r = 0; n     for (i = k + 1; i < n; i++) { n       if (s[i] ==  ( ) { n         x++; n         r++; n       } else if (s[i] ==  )  && x > 0) n         x--; n       else n         h = 1; n       if (s[i] ==  )  && r > 0) r--; n     } n     a[y - 1] = x + 1; n   } n   if (z == 1 || h == 1 || r != 0) n     cout <<  -1 ; n   else { n     for (i = 0; i < y; i++) { n       cout << a[i] << endl; n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6; n vector<int> gr[N]; n long long int edges, z, o; n int col[N], vis[N]; n void color(int n) { n   for (int i = 0; i < gr[n].size(); i++) { n     if (col[gr[n][i]] == -1) { n       if (col[n] == 0) { n         o++; n         col[gr[n][i]] = 1; n       } else { n         z++; n         col[gr[n][i]] = 0; n       } n       edges++; n       color(gr[n][i]); n     } n   } n } n int main() { n   int n, a, b; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     col[i] = -1; n     cin >> a >> b; n     gr[a].push_back(b); n     gr[b].push_back(a); n   } n   col[n - 1] = -1; n   col[n] = -1; n   col[1] = 0; n   z++; n   color(1); n   if (n == 1) { n     cout << 0 << endl; n   } else { n     cout << z * o - edges << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x, y; n   cin >> n >> x >> y; n   int arr[n]; n   int countx = 0; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n     if (arr[i] <= x) { n       countx++; n     } n   } n   if (y < x) { n     cout << n << endl; n     return 0; n   } n   if (y >= x) { n     if (countx % 2 == 0) { n       countx = countx / 2; n     } else { n       countx = countx / 2 + 1; n     } n     cout << countx << endl; n     return 0; n   } n } n


#include <bits/stdc++.h> n inline char getThirdColor(char a, char b) { n   char ret; n   if (a ==  B  && b ==  G  || a ==  G  && b ==  B ) n     ret =  R ; n   else if (a ==  B  && b ==  R  || a ==  R  && b ==  B ) n     ret =  G ; n   else if (a ==  R  && b ==  G  || a ==  G  && b ==  R ) n     ret =  B ; n   return ret; n } n std::unordered_map<std::string, bool> ump; n inline bool solve(std::string s, char c) { n   if (s.size() == 1) { n     return s[0] == c; n   } n   if (ump.find(s) != ump.end()) return ump[s]; n   bool ret = false; n   for (int i = 0; i < s.size(); ++i) { n     for (int j = i + 1; j < s.size(); ++j) { n       if (s[i] == s[j]) { n         std::string next_pass{s[i]}; n         for (int k = 0; k < s.size(); ++k) { n           if (k != i && k != j) next_pass += s[k]; n         } n         ret = solve(next_pass, c); n       } else { n         char thirdColor = getThirdColor(s[i], s[j]); n         std::string next_pass{thirdColor}; n         for (int k = 0; k < s.size(); ++k) { n           if (k != i && k != j) next_pass += s[k]; n         } n         ret = solve(next_pass, c); n       } n       if (ret) break; n     } n     if (ret) break; n   } n   return ump[s] = ret; n } n int main() { n   int n; n   std::cin >> n; n   std::string s; n   std::cin >> s; n   std::string modified; n   modified.reserve(n); n   std::unordered_map<char, int> cnt; n   for (int i = 0; i < s.size(); ++i) { n     if (modified.empty()) { n       modified += s[i]; n       ++cnt[s[i]]; n     } else if (modified.back() == s[i] && cnt[s[i]] == 1) { n       modified += s[i]; n       ++cnt[s[i]]; n     } else if (modified.back() != s[i]) { n       modified += s[i]; n       ++cnt[s[i]]; n     } n   } n   std::vector<char> possible{ B ,  G ,  R }; n   for (int i = 0; i < possible.size(); ++i) { n     ump.clear(); n     if (solve(modified, possible[i])) { n       std::cout << possible[i]; n     } n   } n   std::cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t, n, k; n   cin >> t; n   while (t--) { n     long long ats = 0; n     cin >> n >> k; n     while (n) { n       ats += n % k; n       n -= n % k; n       while (n % k == 0 && n) n /= k, ats++; n     } n     cout << ats <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000001; n int n, m, x; n pair<int, int> a[maxn]; n int st[maxn], q[maxn], cnt, ans[maxn], tail, top; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i].first); n   } n   int x; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &x); n     a[n + i].first = m - 1 - x; n     a[n + i].second = 1; n   } n   sort(a + 1, a + 1 + 2 * n); n   top = 0; n   tail = 0; n   for (int i = 1; i <= 2 * n; i++) { n     if (a[i].second == 0) n       st[++top] = a[i].first; n     else { n       if (top == 0) n         q[++tail] = a[i].first; n       else n         ans[++cnt] = (m - 1 - a[i].first + st[top--]) % m; n     } n   } n   for (int i = 1; i <= tail; i++) n     ans[++cnt] = (st[i] + m - 1 - q[tail - i + 1]) % m; n   sort(ans + 1, ans + 1 + n); n   reverse(ans + 1, ans + 1 + n); n   for (int i = 1; i <= n; i++) printf( %d  , ans[i]); n   cout << endl; n   fclose(stdin); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 15; n int n, a[MAXN], b[MAXN], inf = 1e9 + 19, pos, st[MAXN], en[MAXN], LIS, n                          cnt[MAXN]; n void fil() { n   b[0] = -inf; n   for (int i = 1; i < MAXN; i++) b[i] = inf; n } n int bs(int x) { n   int s = 0, e = 1e5 + 10; n   while (s + 1 < e) { n     int mid = (s + e) / 2; n     if (b[mid] < x) n       s = mid; n     else n       e = mid; n   } n   if (b[s] >= x) { n     b[s] = x; n     return s; n   } else { n     b[e] = x; n     return e; n   } n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   fil(); n   for (int i = 0; i < n; i++) { n     pos = bs(a[i]); n     st[i] = pos; n   } n   for (int i = 0; i <= n; i++) n     if (b[i] != inf) LIS = i; n   fil(); n   for (int i = n - 1; i >= 0; i--) { n     pos = bs(-a[i]); n     en[i] = pos; n   } n   for (int i = 0; i < n; i++) n     if (st[i] + en[i] - 1 == LIS) cnt[st[i]]++; n   for (int i = 0; i < n; i++) { n     if (st[i] + en[i] - 1 != LIS) n       cout << 1; n     else if (cnt[st[i]] == 1) n       cout << 3; n     else n       cout << 2; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<char> s(n); n   long long ans = 0; n   for (int i = 0; i < n; ++i) { n     cin >> s[i]; n     if (i > 0 and s[i] ==  .  and s[i - 1] ==  . ) ++ans; n   } n   for (int i = 0; i < m; ++i) { n     int pos; n     cin >> pos; n     --pos; n     char c; n     cin >> c; n     if (c ==  .  and s[pos] !=  . ) { n       if (pos > 0 and s[pos - 1] ==  . ) ++ans; n       if (pos < n and s[pos + 1] ==  . ) ++ans; n     } else if (c !=  .  and s[pos] ==  . ) { n       if (pos > 0 and s[pos - 1] ==  . ) --ans; n       if (pos < n and s[pos + 1] ==  . ) --ans; n     } n     s[pos] = c; n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char ans[1005][1005]; n int x[1005][1005], y[1005][1005]; n int n; n int dx[] = {1, 0, -1, 0}; n int dy[] = {0, 1, 0, -1}; n bool isvalid(int i, int j) { n   if (i < 0 || j < 0 || i >= n || j >= n) return 0; n   return 1; n } n void color(int i, int j) { n   if (i < 0 || j < 0 || i >= n || j >= n || ans[i][j] ==  Z  || x[i][j] != -1 || n       y[i][j] != -1) n     return; n   ans[i][j] =  Z ; n   for (int k = 0; k < 4; k++) color(i + dx[k], j + dy[k]); n } n void DFS(int i, int j, char ch, int ka, int kh) { n   if (i < 0 || j < 0 || i >= n || j >= n || x[i][j] == -1 || ans[i][j] !=  K ) n     return; n   ans[i][j] = ch; n   if (isvalid(i + 1, j) && x[i + 1][j] == ka && y[i + 1][j] == kh) { n     DFS(i + 1, j,  U , ka, kh); n   } n   if (isvalid(i - 1, j) && x[i - 1][j] == ka && y[i - 1][j] == kh) { n     DFS(i - 1, j,  D , ka, kh); n   } n   if (isvalid(i, j + 1) && x[i][j + 1] == ka && y[i][j + 1] == kh) { n     DFS(i, j + 1,  L , ka, kh); n   } n   if (isvalid(i, j - 1) && x[i][j - 1] == ka && y[i][j - 1] == kh) { n     DFS(i, j - 1,  R , ka, kh); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       cin >> x[i][j] >> y[i][j]; n       ans[i][j] =  K ; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (x[i][j] == -1 && y[i][j] == -1) n         color(i, j); n       else if (x[i][j] == i + 1 && y[i][j] == j + 1) n         DFS(i, j,  X , i + 1, j + 1); n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (ans[i][j] ==  Z ) { n         if (isvalid(i + 1, j) && x[i + 1][j] == -1) { n           ans[i][j] =  D ; n           ans[i + 1][j] =  U ; n         } else if (isvalid(i - 1, j) && x[i - 1][j] == -1) { n           ans[i][j] =  U ; n           ans[i - 1][j] =  D ; n         } else if (isvalid(i, j + 1) && x[i][j + 1] == -1) { n           ans[i][j] =  R ; n           ans[i][j + 1] =  L ; n         } else if (isvalid(i, j - 1) && x[i][j - 1] == -1) { n           ans[i][j] =  L ; n           ans[i][j - 1] =  R ; n         } n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (ans[i][j] ==  K  || ans[i][j] ==  Z ) { n         cout <<  INVALID ; n         return 0; n       } n     } n   } n   cout <<  VALID n ; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) cout << ans[i][j]; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n template <class T> n using pq = priority_queue<T>; n template <class T> n using pqg = priority_queue<T, vector<T>, greater<T>>; n template <class T> n bool ckmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool ckmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n void __print(int x) { cerr << x; } n void __print(long x) { cerr << x; } n void __print(long long x) { cerr << x; } n void __print(unsigned x) { cerr << x; } n void __print(unsigned long x) { cerr << x; } n void __print(unsigned long long x) { cerr << x; } n void __print(float x) { cerr << x; } n void __print(double x) { cerr << x; } n void __print(long double x) { cerr << x; } n void __print(char x) { cerr <<      << x <<     ; } n void __print(const char* x) { cerr <<     << x <<    ; } n void __print(const string& x) { cerr <<     << x <<    ; } n void __print(bool x) { cerr << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V>& x) { n   cerr <<  { ; n   __print(x.first); n   cerr <<  ,  ; n   __print(x.second); n   cerr <<  } ; n } n template <typename T> n void __print(const T& x) { n   int first = 0; n   cerr <<  { ; n   for (auto& i : x) cerr << (first++ ?  ,   :   ), __print(i); n   cerr <<  } ; n } n void _print() { cerr <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cerr <<  ,  ; n   _print(v...); n } n const int MOD = 1000000007; n const char nl =   n ; n const int MX = 100001; n void solve() { n   int N; n   cin >> N; n   int A[N]; n   for (int i = 0; i < (N); i++) cin >> A[i]; n   int sum = 0; n   for (int i = 0; i < (N); i++) sum += A[i]; n   if (sum % 2) { n     cout <<  NO  << nl; n     return; n   } n   int cnt = 0; n   vector<int> ops; n   for (int i = 0; i < (N); i++) { n     cnt += A[i]; n     if (A[i] == 0) { n       if (cnt % 2) { n         ops.push_back(i - 1); n         int X = 0; n         for (int j = 0; j < (3); j++) X ^= A[i - 1 + j]; n         for (int j = 0; j < (3); j++) A[i - 1 + j] = X; n       } n       if (A[i] == 0) { n         cnt = 0; n       } else n         cnt++; n     } n   } n   sum = 0; n   for (int i = 0; i < (N); i++) sum += A[i]; n   if (sum == N) { n     cout <<  NO  << nl; n     return; n   } n   int fz = 0; n   for (int i = (N)-1; i >= 0; i--) n     if (A[i] == 0) fz = i; n   while (fz > 0) { n     ops.push_back(fz - 2); n     A[fz - 2] = 0; n     A[fz - 1] = 0; n     A[fz] = 0; n     fz -= 2; n   } n   for (int i = 0; i < (N - 1); i++) { n     if (A[i] == 0 && A[i + 1] == 1) { n       ops.push_back(i); n       A[i] = 0; n       A[i + 1] = 0; n       A[i + 2] = 0; n     } n   } n   cout <<  YES  << nl; n   cout << (int)(ops).size() << nl; n   for (auto& a : ops) { n     cout << a + 1 <<    ; n   } n   cout << nl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int T = 1; n   cin >> T; n   while (T--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     vector<int> a(n); n     for (int i = 0; i < n; i++) cin >> a[i]; n     for (int i = 0; i < n - 1; i += 2) { n       cout << a[i + 1] <<     << -1 * a[i] <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_L = 20; n const long long MOD = 1e9 + 7; n const long long INF = 1e9 + 7; n int p = 100000007; n template <typename T> n void scan(T &x) { n   x = 0; n   bool neg = 0; n   register T c = getchar(); n   if (c ==  - ) neg = 1, c = getchar(); n   while ((c < 48) || (c > 57)) c = getchar(); n   for (; c < 48 || c > 57; c = getchar()) n     ; n   for (; c > 47 && c < 58; c = getchar()) x = (x << 3) + (x << 1) + (c & 15); n   if (neg) x *= -1; n } n template <typename T> n void print(T n) { n   bool neg = 0; n   if (n < 0) n *= -1, neg = 1; n   char snum[65]; n   long long int i = 0; n   do { n     snum[i++] = n % 10 +  0 ; n     n /= 10; n   } while (n); n   --i; n   if (neg) putchar( - ); n   while (i >= 0) putchar(snum[i--]); n   putchar(  n ); n } n bool semiprime(long long int num) { n   int cnt = 0; n   for (int i = 2; cnt < 2 && i * i <= num; ++i) n     while (num % i == 0) num /= i, ++cnt; n   if (num > 1) ++cnt; n   return cnt == 2; n } n bool prime(long long int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n vector<pair<long long int, long long int>> g[100001]; n long long int dfs(long long int nod, long long int y) { n   long long int ret = 0; n   for (auto x : g[nod]) { n     if (x.first != y) { n       ret = max(ret, x.second + dfs(x.first, nod)); n     } n   } n   return ret; n } n void overpower() { n   long long int n, x, y, z; n   long long int tot = 0; n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     cin >> x >> y >> z; n     g[x].push_back(make_pair(y, z)); n     g[y].push_back(make_pair(x, z)); n     tot += 2 * z; n   } n   cout << tot - dfs(1, 0); n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int t; n   t = 1; n   long long int i; n   for (long long int i = i = 0; i < t; i++) { n     overpower(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, Ord[100000 + 5]; n long long ans, Set[100000 + 5][5]; n inline int gcd(int x, int y) { return !y ? x : gcd(y, x % y); } n int main() { n   scanf( %d%d , &n, &k); n   printf( %d n , (n * 6 - 1) * k); n   for (int i = 0; i < n; i++) n     printf( %d %d %d %d n , (1 + i * 6) * k, (2 + i * 6) * k, (3 + i * 6) * k, n            (5 + i * 6) * k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int max(long long int a, long long int b) { n   if (a > b) n     return a; n   else n     return b; n } n long long int min(long long int a, long long int b) { n   if (a < b) n     return a; n   else n     return b; n } n const int dx[4] = {-1, 1, 0, 0}; n const int dy[4] = {0, 0, -1, 1}; n int XX[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n int YY[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n vector<pair<long long int, long long int> > edges; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   long long int n, m, i, j, k, l, r, ind, c, d, x; n   cin >> n >> k; n   j = k + 1; n   long long int z = n / k; n   long long int count = 0, ans = 2 * z; n   j = 2; n   vector<long long int> vec, vec2; n   for (i = 1; i <= k; i++) { n     count = 0; n     while (count < z && j <= n) { n       if (count == 0) n         edges.push_back(make_pair(1, j)); n       else n         edges.push_back(make_pair(j - 1, j)); n       j++; n       count++; n     } n     vec2.push_back(count); n     vec.push_back(j - 1); n   } n   if (j == n + 1) { n     sort(vec2.begin(), vec2.end()); n     long long int sum = vec2[vec2.size() - 1]; n     if (vec2.size() >= 2) sum += vec2[vec2.size() - 2]; n     cout << sum <<   n ; n     for (i = 0; i < edges.size(); i++) { n       cout << edges[i].first <<     << edges[i].second <<   n ; n     } n     return 0; n   } n   long long int flag = 0; n   for (i = 0; i < vec.size(); i++) { n     if (j == n + 1) break; n     edges.push_back(make_pair(vec[i], j)); n     j++; n     flag++; n   } n   if (flag == 1) n     ans++; n   else if (flag > 1) n     ans += 2; n   cout << ans <<   n ; n   for (i = 0; i < edges.size(); i++) { n     cout << edges[i].first <<     << edges[i].second <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n char arr[1000007]; n void sieve() { n   arr[0] = 1; n   arr[1] = 1; n   for (int i = 4; i <= 1000007; i += 2) arr[i] = 1; n   for (int i = 3; i * i <= 1000007; i += 2) { n     if (arr[i] == 0) { n       for (int j = i; i * j <= 1000007; j++) { n         arr[i * j] = 1; n       } n     } n   } n } n int main() { n   sieve(); n   int n; n   cin >> n; n   int p = n + 1; n   if (n < 3) { n     if (n == 1) { n       cout <<  1  << endl; n       cout <<  1  << endl; n       return 0; n     } else { n       cout <<  1  << endl; n       cout <<  1 1  << endl; n       return 0; n     } n   } n   cout <<  2  << endl; n   for (int i = 2; i <= p; i++) { n     if (arr[i] == 0) n       cout <<  1  ; n     else n       cout <<  2  ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, vector<int> > m; n int main() { n   int n, k, query; n   scanf( %d %d , &n, &k); n   for (int i = 0; i < (int)(k); i++) { n     scanf( %d , &query); n     m[query - 1].push_back(i); n   } n   int ans = 0; n   std::vector<int>::iterator low; n   for (int i = 0; i < (int)(n); i++) { n     if (m[i].size() == 0) { n       ans += 3; n       if (i == 0 || i == n - 1) ans--; n     } else { n       if (i - 1 >= 0) { n         low = lower_bound(m[i - 1].begin(), m[i - 1].end(), m[i][0]); n         if (low == m[i - 1].end() || m[i - 1].size() == 0) { n           ans++; n         } n       } n       if (i + 1 < n) { n         low = lower_bound(m[i + 1].begin(), m[i + 1].end(), m[i][0]); n         if (low == m[i + 1].end() || m[i + 1].size() == 0) ans++; n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 1e6 + 1; n int n, m; n string s[3]; n int dp[mx][8]; n bool isok(int i, int j) { n   int cnt = 0; n   for (int k = 0; k < 2; k++) cnt += ((i >> k) & 1) + ((j >> k) & 1); n   if (cnt % 2 == 0) return false; n   if (n == 2) return true; n   cnt = 0; n   for (int k = 1; k < 3; k++) cnt += ((i >> k) & 1) + ((j >> k) & 1); n   return cnt & 1; n } n int solve(int idx, int msk) { n   if (idx == m) return 0; n   int &ans = dp[idx][msk]; n   if (~ans) return ans; n   ans = 1e9; n   for (int i = 0; i < (1 << n); i++) n     if (idx == 0 || isok(msk, i)) { n       int cur = 0; n       for (int j = 0; j < n; j++) cur += ((i >> j) & 1) != (s[j][idx] -  0 ); n       ans = min(ans, cur + solve(idx + 1, i)); n     } n   return ans; n } n int main() { n   cin.tie(nullptr); n   ios_base::sync_with_stdio(0); n   cin >> n >> m; n   if (n == 1) return cout <<  0 , 0; n   if (n >= 4) return cout <<  -1 , 0; n   for (int i = 0; i < n; i++) cin >> s[i]; n   int ans = 1e9; n   memset(dp, -1, sizeof dp); n   for (int i = 0; i < (1 << n); i++) ans = min(ans, solve(0, i)); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long N, M; n long long Arb[800002], Value[100002], Lazy[800002], Colour[800002]; n void Build(long long K, long long L, long long R) { n   long long Mid = (L + R) / 2; n   if (L == R) { n     Colour[K] = L; n     return; n   } n   Build(2 * K, L, Mid); n   Build(2 * K + 1, Mid + 1, R); n } n void Read() { cin >> N >> M; } n void Relax(long long K, long long L, long long R) { n   if (Lazy[K] == 0) return; n   Colour[2 * K] = Colour[2 * K + 1] = Colour[K]; n   Arb[2 * K] += Lazy[K] * ((L + R) / 2 - L + 1); n   Arb[2 * K + 1] += Lazy[K] * (R - (L + R) / 2); n   if (L != R) { n     Lazy[2 * K] += Lazy[K]; n     Lazy[2 * K + 1] += Lazy[K]; n   } n   Lazy[K] = 0; n } n void Update(long long K, long long L, long long R, long long Poz1, n             long long Poz2, long long Val) { n   long long Mid = (L + R) / 2; n   Relax(K, L, R); n   if (L > Poz2 || R < Poz1 || K > 4 * N) return; n   if (Poz1 <= L && R <= Poz2) { n     if (Colour[K] > 0) { n       Arb[K] += abs(Colour[K] - Val) * (R - L + 1); n       Lazy[K] += abs(Colour[K] - Val); n       Colour[K] = Val; n     } else { n       Update(K * 2, L, Mid, Poz1, Poz2, Val); n       Update(K * 2 + 1, Mid + 1, R, Poz1, Poz2, Val); n       Arb[K] = Arb[2 * K] + Arb[2 * K + 1]; n       Colour[K] = Val; n     } n     return; n   } n   Update(2 * K + 1, Mid + 1, R, Poz1, Poz2, Val); n   Update(2 * K, L, Mid, Poz1, Poz2, Val); n   Arb[K] = Arb[2 * K] + Arb[2 * K + 1]; n   Colour[K] = Colour[K * 2] == Colour[K * 2 + 1] ? Colour[K * 2] : 0; n } n long long Qwery(long long K, long long L, long long R, long long Poz1, n                 long long Poz2) { n   Relax(K, L, R); n   long long Mid = (L + R) / 2; n   if (L > Poz2 || R < Poz1) return 0; n   if (Poz1 <= L && R <= Poz2) return Arb[K]; n   return Qwery(2 * K, L, Mid, Poz1, Poz2) + n          Qwery(2 * K + 1, Mid + 1, R, Poz1, Poz2); n } n void Browse() { n   long long i; n   for (i = 1; i <= M; i++) { n     long long type, a, b, c; n     cin >> type >> a >> b; n     if (type == 1) { n       cin >> c; n       Update(1, 1, N, a, b, c); n     } n     if (type == 2) cout << Qwery(1, 1, N, a, b) <<   n ; n   } n } n int main() { n   Read(); n   Build(1, 1, N); n   Browse(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 15; n const int inf = 1e9; n const int md = 1e9 + 7; n const int shift = 1 << 17; n int n, m; n vector<vector<int> > g; n set<pair<int, int> > br; n int timer; n int tin[maxn], fup[maxn]; n int used[maxn]; n void dfs(int v, int p = -1) { n   used[v] = 1; n   tin[v] = fup[v] = timer++; n   for (int i = 0; i < g[v].size(); ++i) { n     int to = g[v][i]; n     if (to == p) continue; n     if (used[to]) n       fup[v] = min(fup[v], tin[to]); n     else { n       dfs(to, v); n       fup[v] = min(fup[v], fup[to]); n       if (fup[to] > tin[v]) { n         br.insert(make_pair(to, v)); n         br.insert(make_pair(v, to)); n       } n     } n   } n } n int color[maxn]; n int first[maxn]; n int d[maxn]; n vector<int> eu; n int z[shift * 2]; n int c; n void dfs2(int v, int tekc, int len, int p = -1) { n   used[v] = 1; n   color[v] = tekc; n   d[tekc] = len; n   for (int i = 0; i < g[v].size(); ++i) { n     int to = g[v][i]; n     if (to == p) continue; n     if (used[to]) continue; n     if (br.find(make_pair(v, to)) != br.end()) { n       eu.push_back(c + 1); n       first[c + 1] = eu.size() - 1; n       ++c; n       dfs2(to, c, len + 1, v); n       eu.push_back(tekc); n     } else n       dfs2(to, tekc, len, v); n   } n } n void build() { n   for (int i = 0; i < eu.size(); ++i) z[i + shift] = eu[i]; n   for (int i = eu.size(); i < shift; ++i) z[i + shift] = n + 1; n   for (int i = shift - 1; i > 0; --i) n     if (d[z[i * 2]] < d[z[i * 2 + 1]]) n       z[i] = z[i * 2]; n     else n       z[i] = z[i * 2 + 1]; n } n int get(int v, int tl, int tr, int l, int r) { n   if (l > r) return n + 1; n   if (l == tl && r == tr) return z[v]; n   int tm = (tl + tr) / 2; n   int ans1 = get(v * 2, tl, tm, l, min(r, tm)); n   int ans2 = get(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r); n   return d[ans1] < d[ans2] ? ans1 : ans2; n } n int main() { n   cin >> n >> m; n   g.resize(n); n   for (int i = 0; i < m; ++i) { n     int v, u; n     cin >> v >> u; n     --v, --u; n     g[v].push_back(u); n     g[u].push_back(v); n   } n   dfs(0); n   memset(used, 0, sizeof(used)); n   memset(first, -1, sizeof(first)); n   eu.push_back(0); n   first[0] = 0; n   dfs2(0, 0, 0); n   d[n + 1] = inf; n   build(); n   int k; n   cin >> k; n   for (int i = 0; i < k; ++i) { n     int s, l; n     cin >> s >> l; n     --s, --l; n     int L = first[color[s]]; n     int R = first[color[l]]; n     if (L > R) swap(L, R); n     int lca = get(1, 0, shift - 1, L, R); n     cout << d[color[s]] + d[color[l]] - 2 * d[lca] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n int q, n, a[N], pos[N], dp[N], pre[N]; n long long ans = 0; n int main() { n   scanf( %d , &q); n   while (q--) { n     ans = 0; n     scanf( %d , &n); n     for (int i = 1; i <= n; i++) pre[i] = -1; n     for (int i = 0; i < n; i++) { n       scanf( %d , &a[i]); n       dp[i] = 0; n       pos[i] = N; n       if (pre[a[i]] == -1) { n         pre[a[i]] = i; n       } n     } n     dp[N - 1] = 0; n     for (int i = 0; i < n; i++) { n       if (a[i + 1] == a[i]) n         pos[i + 1] = i; n       else { n         if (a[i + 1] == a[pos[i] - 1]) { n           pos[i + 1] = pos[i] - 1; n           continue; n         } n         int tmp = i; n         while (a[i + 1] != a[pos[tmp] - 1]) { n           tmp = pos[tmp] - 1; n           if (a[i + 1] == a[pos[tmp] - 1]) { n             pos[i + 1] = pos[tmp] - 1; n             break; n           } n           if (tmp == N - 1 || tmp < pre[a[i + 1]]) { n             pos[i + 1] = N; n             break; n           } n         } n       } n     } n     for (int i = 0; i < n; i++) { n       if (pos[i] != N) dp[i] = 1; n     } n     for (int i = 0; i < n; i++) { n       dp[i] += dp[pos[i] - 1]; n       ans += dp[i]; n     } n     printf( %lld n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e17; n const int MAXM = 1e5 + 5; n const int MAXN = 1e6 + 4; n long long fact[MAXM]; n long long nChoosek(long long n, long long k) { n   if (k > n) return 0; n   if (k * 2 > n) k = n - k; n   if (k == 0) return 1; n   long long result = n; n   for (long long i = 2; i <= k; ++i) { n     result *= (n - i + 1); n     result /= i; n   } n   return result; n } n int main() { n   long long n; n   cin >> n; n   long long Ans = 0; n   for (long long i = 5; i <= 7; i++) { n     Ans += nChoosek(n, i); n   } n   cout << Ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n int val[501000], deg[501000]; n vector<int> adj[501000]; n int main() { n   int n, m, u, v; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &u, &v); n     adj[u].push_back(v); n     adj[v].push_back(u); n     deg[u]++, deg[v]++; n   } n   set<pair<int, int> > st; n   for (int i = 1; i <= n; i++) { n     if (adj[i].size() > 1) st.insert(pair<int, int>(adj[i].size(), i)); n   } n   while (!st.empty()) { n     pair<int, int> a = *st.begin(); n     if (a.first < 2) break; n     st.erase(a); n     val[a.second] ^= 1; n     u = a.second; n     deg[u] = 0; n     for (int i = 0; i < adj[u].size(); i++) { n       int v = adj[u][i]; n       if (val[v] == val[u]) { n         if (deg[v] > 1) st.erase(pair<int, int>(deg[v], v)); n         deg[v]++; n         if (deg[v] > 1) st.insert(pair<int, int>(deg[v], v)); n         deg[u]++; n       } else { n         if (deg[v] > 1) st.erase(pair<int, int>(deg[v], v)); n         deg[v]--; n         if (deg[v] > 1) st.insert(pair<int, int>(deg[v], v)); n       } n     } n   } n   for (int i = 1; i <= n; i++) printf( %d , val[i]); n   return 0; n } n


#include <bits/stdc++.h> n     using namespace std; n       n     #define endl   n  n     #define ll long long n     #define INF (ll)1e18 n       n     ll i, i1, j, k, k1, t, n, m, res, check1, a[200010], a1, b, d, v, dist[200010], dp[200010], s, s1, s2; n     vector<ll> adj[200010]; n     vector<array<ll, 3>> dadj[200010]; n     bool visited[200010]; n       n     void dfs(ll s) { n         for (auto u : adj[s]) { n             if (!visited[u]) { n                 visited[u] = true; n                 dist[u] = dist[s] + 1; n                 dadj[dist[u]].push_back({-1, s, u}); n                 d = max(d, dist[u]); n                 dfs(u); n             } n         } n     } n       n     int main() { n         ios::sync_with_stdio(0); n         cin.tie(0); n       n         //ifstream cin( input.txt ); n         //ofstream cout( output.txt ); n       n         cin >> t; n         while (t--) { n             cin >> n; n             for (i = 0; i <= n; i++) { n                 adj[i].clear(); dadj[i].clear(); visited[i] = false; dist[i] = 0; dp[i] = 0; n             } n             for (i = 2; i <= n; i++) { n                 cin >> v; n                 adj[v - 1].push_back(i - 1); n                 adj[i - 1].push_back(v - 1); n             } n       n             for (i = 1; i < n; i++) { n                 cin >> a[i]; n             } n       n             d = 0; n             visited[0] = true; n             dfs(0); n             dadj[0].push_back({0, -1, 0}); n       n             for (i = 1; i <= d; i++) { n                 for (auto& u : dadj[i]) { n                     u[0] = a[u[2]]; n                     // cout << u[0] <<     << u[1] <<     << u[2] << endl; n                 } n                 // cout << endl; n                 sort(dadj[i].begin(), dadj[i].end()); n                 s = dadj[i].size(); n                 s1 = dadj[i][0][0]; n                 s2 = dadj[i][dadj[i].size() - 1][0]; n                 // cout << s1 <<     << s2 << endl; n                 for (auto& u : dadj[i]) { n                     a1 = a[u[2]]; n                     dp[u[2]] = max({dp[u[2]], dp[u[1]] + a1 - s1, dp[u[1]] + s2 - a1}); n                 } n                 k = 0; n                 for (j = 0; j < s; j++) { n                     if (dp[dadj[i][j][1]] - dadj[i][j][0] > dp[dadj[i][k][1]] - dadj[i][k][0]) { n                         k = j; n                     } n                     dp[dadj[i][j][2]] = max(dp[dadj[i][j][2]], dadj[i][j][0] - dadj[i][k][0] + dp[dadj[i][k][1]]); n                 } n                 k = s - 1; n                 for (j = s - 1; j >= 0; j--) { n                     if (dp[dadj[i][j][1]] + dadj[i][j][0] > dp[dadj[i][k][1]] + dadj[i][k][0]) { n                         k = j; n                     } n                     dp[dadj[i][j][2]] = max(dp[dadj[i][j][2]], dadj[i][k][0] - dadj[i][j][0] + dp[dadj[i][k][1]]); n                 } n             } n       n             res = 0; n             for (i = 1; i < n; i++) { n                 // cout << dp[i] <<    ; n                 res = max(res, dp[i]); n             } n             // cout << endl; n       n             cout << res << endl; n         } n       n         return 0; n     }


#include <bits/stdc++.h> n using namespace std; n int n; n map<int, int> mpp; n map<int, int> tmp; n multiset<int> adj[200005]; n multiset<pair<int, int> > pairs; n int cnt[200005]; n int arra[100005]; n int arrb[100005]; n list<int> ans; n void addpair(int u, int v) { n   pairs.insert(make_pair(u, v)); n   pairs.insert(make_pair(v, u)); n } n void removepair(int u, int v) { n   pairs.erase(pairs.find(make_pair(u, v))); n   pairs.erase(pairs.find(make_pair(v, u))); n } n int ispair(int u, int v) { n   if (pairs.find(make_pair(u, v)) == pairs.end()) return 0; n   return 1; n } n void bit() { n   printf( -1 n ); n   exit(0); n } n int main() { n   clock_t start = clock(); n   scanf( %d , &n); n   for (int i = 0; i < 2 * n; i++) cnt[i] = 0; n   for (int i = 0; i < n - 1; i++) { n     scanf( %d , arra + i); n   } n   for (int i = 0; i < n - 1; i++) { n     scanf( %d , arrb + i); n   } n   for (int i = 0; i < n - 1; i++) { n     int a = arra[i], b = arrb[i]; n     int ai = i * 2, bi = i * 2 + 1; n     if (a > b) bit(); n     if (tmp.count(a)) n       ai = tmp[a]; n     else { n       tmp[a] = ai; n       mpp[ai] = a; n     } n     if (tmp.count(b)) n       bi = tmp[b]; n     else { n       tmp[b] = bi; n       mpp[bi] = b; n     } n     cnt[ai]++; n     cnt[bi]++; n     addpair(ai, bi); n     adj[ai].insert(bi); n     adj[bi].insert(ai); n   } n   int tek1 = -1; n   int tek2 = -1; n   for (int i = 0; i < 2 * n; i++) { n     if (cnt[i] % 2 == 1) { n       if (tek2 != -1) n         bit(); n       else if (tek1 != -1) n         tek2 = i; n       else n         tek1 = i; n     } n   } n   if (tek1 != -1 && tek2 == -1) assert(0); n   list<int> ans; n   if (tek1 == -1) { n     tek1 = 0; n   } n   stack<int> st; n   st.push(tek1); n   while (!st.empty()) { n     int node = st.top(); n     if (adj[node].size() == 0) { n       ans.push_back(mpp[node]); n       st.pop(); n     } else { n       st.push(*adj[node].begin()); n       adj[*adj[node].begin()].erase(adj[*adj[node].begin()].find(node)); n       adj[node].erase(adj[node].begin()); n     } n   } n   for (int i = 0; i < 2 * n; i++) { n     if (adj[i].size() != 0) bit(); n   } n   for (list<int>::iterator it = ans.begin(); it != ans.end(); ++it) { n     printf( %d  , *it); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans = 1, p, m; n string s; n set<int> buy, sell, extra; n int main() { n   cin >> n; n   while (n--) { n     cin >> s >> p; n     if (s ==  ACCEPT ) { n       if (extra.find(p) != extra.end()) { n         for (auto it : extra) { n           if (it < p) n             buy.insert(it); n           else if (it > p) n             sell.insert(it); n         } n         ans = (ans + ans) % 1000000007; n         extra.clear(); n       } else if (buy.find(p) != buy.end()) { n         auto it = buy.end(); n         it--; n         if (*it != p) ans = 0; n         it = buy.find(p); n         buy.erase(it); n         for (auto it : extra) { n           sell.insert(it); n         } n         extra.clear(); n       } else { n         auto it = sell.find(p); n         assert(it != sell.end()); n         if (it != sell.begin()) ans = 0; n         sell.erase(it); n         for (auto it : extra) { n           buy.insert(it); n         } n         extra.clear(); n       } n     } else { n       if (buy.empty()) { n         if (sell.empty()) { n           extra.insert(p); n         } else { n           auto it = sell.begin(); n           if (p > *it) n             sell.insert(p); n           else n             extra.insert(p); n         } n       } else { n         auto it = buy.end(); n         it--; n         if (sell.empty()) { n           if (p < *it) n             buy.insert(p); n           else n             extra.insert(p); n         } else { n           auto jt = sell.begin(); n           if (p > *it && p < *jt) n             extra.insert(p); n           else if (p < *it) n             buy.insert(p); n           else n             sell.insert(p); n         } n       } n     } n   } n   long long int res = ((ans * 1LL) * (extra.size() + 1)) % 1000000007; n   return cout << res <<   n , false; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n template <class T, class U> n inline void Max(T &a, U b) { n   if (a < b) a = b; n } n template <class T, class U> n inline void Min(T &a, U b) { n   if (a > b) a = b; n } n inline void add(int &a, int b) { n   a += b; n   while (a >= 1000000007) a -= 1000000007; n } n int pow(int a, int b) { n   int ans = 1; n   while (b) { n     if (b & 1) ans = ans * (long long)a % 1000000007; n     a = (long long)a * a % 1000000007; n     b >>= 1; n   } n   return ans; n } n pair<int, pair<int, int> > b[200010]; n int v[200010], f[200010], ans[200010]; n int query(int first) { n   int ans = 0; n   while (first < 200010) ans += f[first], first += first & -first; n   return ans; n } n void upd(int first) { n   while (first > 0) f[first]++, first -= first & -first; n } n int main() { n   int i, j, k, n, m = 0; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d%d , &j, &k); n     v[m++] = j; n     b[i].first = k; n     b[i].second = make_pair(j, i); n   } n   sort(v, v + m); n   m = unique(v, v + m) - v; n   sort(b, b + n); n   for (i = 0; i < n; i++) { n     k = lower_bound(v, v + m, b[i].second.first) - v + 1; n     ans[b[i].second.second] = query(k); n     upd(k); n   } n   for (i = 0; i < n; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int num[105]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     scanf( %1d , &num[i]); n   } n   int sum = 0; n   int flag = 0; n   for (int i = 1; i <= n; i++) { n     sum += num[i]; n     int t = 0; n     flag = 0; n     for (int j = i + 1; j <= n; j++) { n       if (num[j] == 0 && t == 0 && flag == 1) { n         if (j == n) { n           puts( YES ); n           return 0; n         } n         continue; n       } n       t += num[j]; n       if (t > sum) { n         break; n       } else if (t == sum) { n         t = 0; n         flag = 1; n         if (j == n) { n           puts( YES ); n           return 0; n         } n       } n     } n   } n   puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   int x, y, l, r; n }; n bool f(point a, point b) { return a.r > b.r; } n point a[5000]; n int par[5000]; n void init(int n) { n   for (int i = 1; i <= n; i++) par[i] = i; n } n int root(int n) { n   if (par[n] == n) return n; n   return par[n] = root(par[n]); n } n int main() { n   int n, m, maxx = 0; n   cin >> n >> m; n   for (int i = 1; i <= m; i++) cin >> a[i].x >> a[i].y >> a[i].l >> a[i].r; n   sort(a + 1, a + m + 1, f); n   for (int i = 1; i <= m; i++) { n     init(n); n     for (int j = 1; j <= m; j++) { n       if (a[j].l > a[i].l) continue; n       int q1 = root(a[j].x), q2 = root(a[j].y); n       if (q1 != q2) par[q1] = q2; n       if (root(1) == root(n)) maxx = max(maxx, a[j].r - a[i].l + 1); n     } n   } n   if (!maxx) n     cout <<  Nice work, Dima!  << endl; n   else n     cout << maxx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int na, ma, nb, mb; n   cin >> na >> ma; n   char ch1[na + 5][ma + 5]; n   for (int i = 1; i <= na; i++) { n     for (int j = 1; j <= ma; j++) { n       cin >> ch1[i][j]; n     } n   } n   cin >> nb >> mb; n   char ch2[nb + 5][mb + 5]; n   for (int i = 1; i <= nb; i++) { n     for (int j = 1; j <= mb; j++) { n       cin >> ch2[i][j]; n     } n   } n   int x = -1, y = -1, mx = -1; n   for (int xx = -50; xx <= 50; xx++) { n     for (int yy = -50; yy <= 50; yy++) { n       int sum = 0; n       for (int i = 1; i <= na; i++) { n         for (int j = 1; j <= ma; j++) { n           if (((i + xx) >= 1 && (i + xx) <= nb) && n               ((j + yy) >= 1 && (j + yy) <= mb)) { n             sum += (ch1[i][j] -  0 ) * (ch2[i + xx][j + yy] -  0 ); n           } n         } n       } n       if (sum > mx) { n         mx = sum; n         x = xx; n         y = yy; n       } n     } n   } n   cout << x <<     << y <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, sum, s; n   cin >> a >> b >> s; n   sum = abs(a) + abs(b); n   if (sum <= s && (s - sum) % 2 == 0) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n const int MOD = 1e9 + 7; n const int MAXN = (1 << 9) + 3; n int k; n int a[MAXN][MAXN]; n void dien(int x) { n   if (x == 1) return; n   dien(x / 2); n   for (int i = 1; i <= x / 2; i++) { n     for (int j = 1; j <= x / 2; j++) { n       a[i + x / 2][j] = a[i][j + x / 2] = a[i][j]; n       a[i + x / 2][j + x / 2] = 1 - a[i][j]; n     } n   } n } n int main() { n   cin >> k; n   dien(1 << k); n   for (int i = 1; i <= 1 << k; i++) { n     for (int j = 1; j <= 1 << k; j++) { n       if (a[i][j] == 0) n         cout <<  * ; n       else n         cout <<  + ; n     } n     cout << endl; n   } n   return 0; n } n


//To debug :  g++ -g file.cpp -o code n //to flush output : fflush(stdout) or cout.flush() n //cout<<setprecision(p)<<fixed<<var n //use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll n //take care of precedence rule of operators  n //do not forget to change the sizes of arrays and value of contants and other things after debugging   n   n #include<bits/stdc++.h> n using namespace std; n #define ll long long n #define rep(i,a,n) for(i=a;i<n;++i) n #define irep(i,n,a) for(i=n;i>a;--i) n #define mod 1000000007 n #define pb push_back n #define big 9223372036854775807 n #define big1 LONG_MAX n #define big2 ll_MAX n #define big3 1000000000 n #define sma1 LONG_MIN n #define sma2 ll_MIN n #define sma3 -1000000000 n #define mp make_pair n #define dub double n #define ivec vector<ll> n #define lvec vector<long long> n #define cvec vector<char> n #define svec vector<string> n #define mt make_tuple n #define MOD 998244353 n #define ld long double n #define pi acos(-1.0) n   n #define SZ(x)  (ll)(x.size()) n   n //comment the below if not required n   n /* n   n #define ss second.second n #define ff first.first n #define f first n #define s second n #define sf second.first n #define fs first.second n */ n   n #define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) n   n //cout<< Case # <<c<< :  <<ans<<  n  ; n  n const ll N = 150001; n  n ll a[5][N],n[5],x,y,temp,val,i,j,ans=LLONG_MAX; n  n set<pair<ll,ll>> m; n  n vector<ll> v[5][N]; n  n int main() n { n  IOS; n  n  rep(i,1,5) n   cin>>n[i]; n  n  rep(i,1,5) n   rep(j,1,n[i]+1) n    cin>>a[i][j]; n  n  rep(i,1,4) n  { n   cin>>val; n  n   rep(j,1,val+1) n   { n    cin>>x>>y; n    v[i+1][y].pb(x); n   } n  n  } n  n  rep(i,2,5) n  { n   m.clear(); n  n   rep(j,1,n[i-1]+1) n   { n    if(a[i-1][j]!=-1) n     m.insert({a[i-1][j],j}); n   } n  n   rep(j,1,n[i]+1) n   { n    for(auto u : v[i][j]) n    { n     if(a[i-1][u]!=-1) n     { n      auto it = m.find({a[i-1][u],u}); n      m.erase(it); n     } n    } n  n    if(SZ(m)) n    { n     auto it = m.begin(); n     a[i][j]+=(*it).first; n    } n    else n     a[i][j]=-1; n  n    for(auto u : v[i][j]) n    { n     if(a[i-1][u]!=-1) n      m.insert({a[i-1][u],u}); n    } n  n   } n  n  } n  n  n  rep(i,1,n[4]+1){ n  n   if(a[4][i]==-1) n    continue; n  n   ans = min(ans,a[4][i]); n  } n  n  if(ans==LLONG_MAX) n   ans = -1; n  n  cout<<ans<<  n ; n  n  n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.first > b.first); n } n struct greaterr { n   template <class T> n   bool operator()(T const &a, T const &b) const { n     return a > b; n   } n }; n int main() { n   int n; n   scanf( %d , &n); n   char ans[n]; n   int x, y; n   long long int suma = 0, sumb = 0; n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &y, &x); n     if (suma + y - sumb <= 500) { n       suma += y; n       printf( A ); n     } else { n       sumb += x; n       printf( G ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n long double pi = acos(-1); n long long z = 1000000007; n long long power(long long a, long long b) { n   if (b == 0) return 1; n   long long c = power(a, b / 2); n   if (b % 2 == 0) n     return c * c; n   else n     return c * c * a; n } n using namespace std; n const int N = 1e6 + 7; n void solve() { n   long long n; n   cin >> n; n   long long k; n   cin >> k; n   map<long long, long long> m; n   for (long long i = 0; i < n; i++) { n     long long x; n     cin >> x; n     m[x]++; n   } n   if (m.size() <= k) n     cout << 1 << endl; n   else if (k == 1 && m.size() > 1) n     cout << -1 << endl; n   else { n     long long ans = (m.size() - k) / (k - 1) + 1; n     if ((m.size() - k) % (k - 1) != 0) ans++; n     cout << ans << endl; n   } n } n int main() { n   long long t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int get(long long a) { n   if (a < 4) return 0; n   if (a < 16) return 1; n   if (a < 82) return 2; n   if (a < 6724) return 0; n   if (a < 50626) return 3; n   if (a < 2562991876) return 1; n   return 2; n } n int main() { n   int cur = 0; n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     long long t; n     cin >> t; n     cur ^= get(t); n   } n   if ((cur) != 0) n     cout <<  Furlo ; n   else n     cout <<  Rublo ; n } n