#include <bits/stdc++.h> n using namespace std; n using int64 = long long; n struct Primal_Dual { n   const int INF = 1 << 30; n   struct edge { n     int to, cap, cost, rev; n   }; n   vector<vector<edge> > graph; n   vector<int> potential, min_cost, prevv, preve; n   Primal_Dual(int V) : graph(V) {} n   void add_edge(int from, int to, int cap, int cost) { n     graph[from].push_back((edge){to, cap, cost, (int)graph[to].size()}); n     graph[to].push_back((edge){from, 0, -cost, (int)graph[from].size() - 1}); n   } n   int min_cost_flow(int s, int t, int f) { n     int V = graph.size(), ret = 0; n     priority_queue<pair<int, int>, vector<pair<int, int> >, n                    greater<pair<int, int> > > n         que; n     potential.assign(V, 0); n     preve.assign(V, -1); n     prevv.assign(V, -1); n     while (f > 0) { n       min_cost.assign(V, INF); n       que.push(pair<int, int>(0, s)); n       min_cost[s] = 0; n       while (!que.empty()) { n         pair<int, int> p = que.top(); n         que.pop(); n         if (min_cost[p.second] < p.first) continue; n         for (int i = 0; i < graph[p.second].size(); i++) { n           edge &e = graph[p.second][i]; n           int nextCost = min_cost[p.second] + e.cost + potential[p.second] - n                          potential[e.to]; n           if (e.cap > 0 && min_cost[e.to] > nextCost) { n             min_cost[e.to] = nextCost; n             prevv[e.to] = p.second, preve[e.to] = i; n             que.push(pair<int, int>(min_cost[e.to], e.to)); n           } n         } n       } n       if (min_cost[t] == INF) return -1; n       for (int v = 0; v < V; v++) potential[v] += min_cost[v]; n       int addflow = f; n       for (int v = t; v != s; v = prevv[v]) { n         addflow = min(addflow, graph[prevv[v]][preve[v]].cap); n       } n       f -= addflow; n       ret += addflow * potential[t]; n       for (int v = t; v != s; v = prevv[v]) { n         edge &e = graph[prevv[v]][preve[v]]; n         e.cap -= addflow; n         graph[v][e.rev].cap += addflow; n       } n     } n     return ret; n   } n }; n int main() { n   int N, B[100]; n   string S; n   cin >> N; n   cin >> S; n   int sum[26] = {}; n   for (int i = 0; i < N; i++) { n     sum[S[i] -  a ]++; n   } n   for (int i = 0; i < N; i++) { n     cin >> B[i]; n   } n   Primal_Dual flow(N / 2 + 28); n   int s = N / 2 + 26, t = N / 2 + 27; n   for (int i = 0; i < 26; i++) { n     flow.add_edge(s, N / 2 + i, sum[i], 0); n   } n   for (int i = 0; i < N / 2; i++) { n     flow.add_edge(i, t, 2, 0); n   } n   for (int i = 0; i < N / 2; i++) { n     for (int j = 0; j < 26; j++) { n       int ret = 0; n       if (S[i] -  a  == j) ret = max(ret, B[i]); n       if (S[N - i - 1] -  a  == j) ret = max(ret, B[N - i - 1]); n       flow.add_edge(N / 2 + j, i, 1, -ret); n     } n   } n   cout << -flow.min_cost_flow(s, t, N) << endl; 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   string s; n   cin >> s; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  W  && s[i + 1] ==  U  && s[i + 2] ==  B ) { n       i += 2; n       cout <<    ; n     } else { n       cout << s[i]; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fastIO() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.precision(20); n } n int main() { n   fastIO(); n   int n; n   int m; n   int total; n   int day = 0; n   cin >> n >> m; n   while (n != 0) { n     day++; n     n = n - 1; n     if (day % m == 0) { n       n = n + 1; n     } n   } n   cout << day << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, sum = 0; n   cin >> n; n   vector<long long int> v; n   for (long long int i = 0; i < n; i++) { n     long long int el; n     cin >> el, sum += el; n     v.push_back(el); n   } n   sum = sum / n; n   long long int res = 0, curr = 0; n   for (long long int i = 0; i < n; i++) { n     curr += v[i] - sum; n     (curr > 0) ? res += curr : res -= curr; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   if (k == 0) { n     cout << s; n     return; n   } n   if (n == 1) n     cout <<  0 ; n   else { n     if (s[0] >  1 ) { n       s[0] =  1 ; n       k--; n     } n     for (long long int i = 1; i < n && k > 0; i++) { n       if (s[i] >  0 ) { n         s[i] =  0 ; n         k--; n       } n     } n     cout << s; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long int t; n   t = 1; n   while (t--) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int ans = 0; n   int count = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  ) ) { n       ans--; n       if (ans < 0) { n         count++; n         ans = 0; n       } n     } else { n       ans++; n     } n   } n   cout << count << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.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 const long long INF = 4e18; n const long long MAX = 2e5 + 5; n const long long MOD = 1e9 + 7; n int n, m; n void exgcd(int a, int b, long long &c, long long &d) { n   if (a == 0) { n     c = 0; n     d = 1; n     return; n   } n   exgcd(b % a, a, d, c); n   c -= b / a * d; n } n long long gcd(int a, int b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n int g[MAX]; n bool arv[MAX], avb[MAX]; n int sz[MAX]; n pair<int, int> ma[MAX]; n vector<int> v; n vector<int> son[MAX]; n void build() { n   for (int i = 0; i < m; i++) { n     g[i] = gcd(i, m); n     sz[g[i]]++; n     son[g[i]].push_back(i); n   } n   g[m] = m; n   int x = m; n   for (int i = 2; i <= sqrt(x); i++) { n     if (x % i == 0) v.push_back(i); n     while (x % i == 0) x /= i; n   } n   if (x > 1) v.push_back(x); n } n void read() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   build(); n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     avb[x] = 1; n     if (!x) avb[m] = 1; n     sz[gcd(x, m)]--; n   } n } n void dfs(long long x) { n   if (arv[x]) return; n   for (int i : v) { n     if (i * x > m) break; n     if (g[i * x] == i * x) { n       dfs(i * x); n       ma[x] = max(ma[x], {ma[i * x].first, i * x}); n     } n   } n   ma[x].first += sz[x]; n   ; n   arv[x] = 1; n } n vector<int> ans; n void output() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout << ((long long)(ans).size()) <<   n ; n   for (int i : ans) cout << i <<    ; n   cout <<   n ; n   ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   read(); n   for (int i = 1; i <= m; i++) n     if (g[i] == i && !arv[i]) dfs(i); n   long long now = 1, pre = 1; n   long long cod; n   while (now <= m) { n     ; n     for (long long i : son[now]) { n       if (!avb[i]) { n         long long r, y; n         exgcd(pre, m, r, y); n         if (r < 0) r += m; n         ans.push_back(i / gcd(pre, m) * r % m); n         pre = i; n       } n     } n     if (ma[now].second) n       now = ma[now].second; n     else n       break; n   } n   output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int l, r; n } line[1000005]; n int a[5000005], ls[5000005], b[5000005]; n vector<node> ans; n bool cmp(node a, node b) { return a.l < b.l; } n int main() { n   int n, k; n   while (~scanf( %d %d , &n, &k)) { n     memset(a, 0, sizeof(a)); n     memset(b, 0, sizeof(b)); n     ans.clear(); n     int p = 0; n     for (int i = 1; i <= n; i++) { n       int l, r; n       scanf( %d %d , &l, &r); n       line[i] = node{l, r}; n       ls[++p] = l; n       ls[++p] = r; n     } n     sort(ls + 1, ls + p + 1); n     p = unique(ls + 1, ls + p + 1) - (ls + 1); n     for (int i = 1; i <= n; i++) { n       int l = lower_bound(ls + 1, ls + p + 1, line[i].l) - ls; n       int r = lower_bound(ls + 1, ls + p + 1, line[i].r) - ls; n       a[l]++; n       a[r]--; n       b[l]++; n       b[r + 1]--; n     } n     for (int i = 1; i <= p; i++) { n       a[i] = a[i - 1] + a[i]; n       b[i] = b[i - 1] + b[i]; n     } n     for (int i = 0; i <= p; i++) { n       if (a[i] >= k) { n         int r = i + 1; n         while (a[r] >= k) { n           r++; n         } n         ans.push_back(node{ls[i], ls[r]}); n         i = r - 1; n       } n       if (a[i - 1] < k && a[i] < k && b[i] >= k) { n         ans.push_back(node{ls[i], ls[i]}); n       } n     } n     sort(ans.begin(), ans.end(), cmp); n     printf( %d n , ans.size()); n     for (int i = 0; i < (int)ans.size(); i++) { n       printf( %d %d n , ans[i].l, ans[i].r); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long i, j, m, a, s, c, b, n, x, y, ar[1001]; n   cin >> n; n   while (n--) { n     cin >> s >> a >> b >> c; n     x = s / c; n     y = x / a * b; n     cout << x + y << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using pi = pair<int, int>; n const int mxn = 2e5; n int n; n string ar[mxn]; n void solve() { n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> ar[i]; n   set<string> second; n   for (int i = n - 1; i >= 0; --i) { n     if (second.count(ar[i])) continue; n     cout << ar[i] <<   n ; n     second.insert(ar[i]); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int x; n   cin >> x; n   while (x--) { n     int n, m, ans = 0; n     cin >> n >> m; n     char a[n][m]; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) cin >> a[i][j]; n     } n     int i = n - 1; n     for (int j = 0; j < m - 1; j++) { n       if (a[i][j] ==  D ) ans++; n     } n     int j = m - 1; n     for (i = 0; i < n - 1; i++) { n       if (a[i][j] ==  R ) ans++; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 4e5; n int n, m, q; n int head[Maxn], tot, father[Maxn], cnt[Maxn]; n int vis[Maxn]; n struct Edge { n   int to, next; n } edge[Maxn * 10]; n void init() { n   memset(head, -1, sizeof(head)); n   tot = 0; n   for (int i = 0; i < Maxn; i++) father[i] = i; n } n int Find(int x) { n   int y = x; n   while (y != father[y]) { n     y = father[y]; n   } n   while (x != father[x]) { n     int temp = father[x]; n     father[x] = y; n     x = temp; n   } n   return y; n } n void Union(int a, int b) { n   int ra = Find(a), rb = Find(b); n   if (ra == rb) return; n   father[ra] = rb; n   cnt[rb] = n       max((cnt[ra] + 1) / 2 + (cnt[rb] + 1) / 2 + 1, max(cnt[ra], cnt[rb])); n   cnt[ra] = 0; n } n void add_edge(int u, int v) { n   edge[tot].to = v; n   edge[tot].next = head[u]; n   head[u] = tot++; n } n int pos, len; n void dfs(int u, int deep, int tar) { n   if (vis[u] == tar) return; n   vis[u] = tar; n   if (deep > len) { n     len = deep; n     pos = u; n   } n   for (int i = head[u]; ~i; i = edge[i].next) { n     int v = edge[i].to; n     dfs(v, deep + 1, tar); n     Union(u, v); n   } n } n int process(int u) { n   pos = len = -1; n   dfs(u, 0, 1); n   dfs(pos, 0, 2); n   return len; n } n int main() { n   init(); n   scanf( %d %d %d , &n, &m, &q); n   while (m--) { n     int u, v; n     scanf( %d %d , &u, &v); n     add_edge(u, v); n     add_edge(v, u); n   } n   for (int i = 1; i <= n; i++) { n     if (vis[i]) continue; n     int Root = Find(i); n     cnt[Root] = process(i); n   } n   while (q--) { n     int op; n     scanf( %d , &op); n     if (op == 1) { n       int u; n       scanf( %d , &u); n       printf( %d n , cnt[Find(u)]); n     } else { n       int u, v; n       scanf( %d %d , &u, &v); n       Union(u, v); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string pad(string x, int lngth) { n   reverse(x.begin(), x.end()); n   for (int i = 0; i < lngth; i++) x +=  0 ; n   reverse(x.begin(), x.end()); n   return x; n } n string add(string x, string y) { n   string res =   ; n   int mx = max(x.length(), y.length()); n   x = pad(x, mx - x.length()); n   y = pad(y, mx - y.length()); n   reverse(x.begin(), x.end()); n   reverse(y.begin(), y.end()); n   int carry = 0, tmp; n   for (int i = 0; i < x.length(); i++) { n     tmp = (x[i] -  0 ) + (y[i] -  0 ) + carry; n     res += ((tmp % 10) +  0 ); n     carry = tmp / 10; n   } n   if (carry != 0) res += (carry +  0 ); n   reverse(res.begin(), res.end()); n   if (res[0] ==  0 ) { n     int i = 0; n     while (res[i] ==  0 ) i++; n     res = res.substr(i, res.length()); n   } n   return res; n } n string smaller(string x, string y) { n   int i, mx = max(x.length(), y.length()); n   string x_ = pad(x, mx - x.length()); n   string y_ = pad(y, mx - y.length()); n   for (i = 0; i < x_.length(); i++) { n     if (x_[i] > y_[i]) n       return y; n     else if (x_[i] < y_[i]) n       return x; n   } n   return x; n } n int main() { n   ios::sync_with_stdio(0); n   int i, n; n   string s, x, y; n   cin >> n >> s; n   int m = n / 2; n   string p1, p2, res, ans =   ; n   bool zero_found = false; n   for (i = 0; i < m; i++) { n     if (s[m + i] !=  0 ) n       break; n     else n       zero_found = true; n   } n   if (!zero_found) { n     ans = add(s.substr(0, m), s.substr(m, n)); n     ans = smaller(ans, add(s.substr(0, m + 1), s.substr(m + 1, n))); n   } else n     ans = add(s.substr(0, m + i), s.substr(m + i, n)); n   zero_found = false; n   for (i = 0; i < m; i++) { n     if (s[m - i] !=  0 ) n       break; n     else n       zero_found = true; n   } n   if (!zero_found) n     ans = smaller(ans, add(s.substr(0, m - 1), s.substr(m - 1, n))); n   else n     ans = smaller(ans, add(s.substr(0, m - i), s.substr(m - i, n))); n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 4 * atan(1); n template <int p> n struct FF { n   long long val; n   FF(long long x = 0) { val = (x % p + p) % p; } n   bool operator==(const FF<p> other) { return val == other.val; } n   bool operator!=(const FF<p> other) { return val != other.val; } n   FF operator+() const { return val; } n   FF operator-() const { return -val; } n   FF& operator+=(const FF<p> other) { n     val = (val + other.val) % p; n     return *this; n   } n   FF& operator-=(const FF<p> other) { n     *this += -other; n     return *this; n   } n   FF& operator*=(const FF<p> other) { n     val = (val * other.val) % p; n     return *this; n   } n   FF& operator/=(const FF<p> other) { n     *this *= other.inv(); n     return *this; n   } n   FF operator+(const FF<p> other) const { return FF(*this) += other; } n   FF operator-(const FF<p> other) const { return FF(*this) -= other; } n   FF operator*(const FF<p> other) const { return FF(*this) *= other; } n   FF operator/(const FF<p> other) const { return FF(*this) /= other; } n   static FF<p> pow(const FF<p> a, long long b) { n     if (!b) return 1; n     return pow(a * a, b >> 1) * (b & 1 ? a : 1); n   } n   FF<p> pow(long long b) const { return pow(*this, b); } n   FF<p> inv() const { return pow(p - 2); } n }; n template <int p> n FF<p> operator+(long long lhs, const FF<p> rhs) { n   return FF<p>(lhs) += rhs; n } n template <int p> n FF<p> operator-(long long lhs, const FF<p> rhs) { n   return FF<p>(lhs) -= rhs; n } n template <int p> n FF<p> operator*(long long lhs, const FF<p> rhs) { n   return FF<p>(lhs) *= rhs; n } n template <int p> n FF<p> operator/(long long lhs, const FF<p> rhs) { n   return FF<p>(lhs) /= rhs; n } n int N; n vector<int> adj[500013]; n FF<998244353> dp[500013]; n FF<998244353> ans; n void dfs(int n, int par = -1) { n   dp[n] = 1; n   for (int v : adj[n]) n     if (v != par) { n       dfs(v, n); n       dp[n] *= 1 - dp[v] / 2; n     } n   ans += 1 - dp[n]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> N; n   for (int i = 0; i < N - 1; i++) { n     int u, v; n     cin >> u >> v; n     --u; n     --v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(0); n   for (int i = 0; i < N; i++) ans = ans * 2; n   cout << ans.val << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k, dx, dy, x, y; n char c[5]; n bool f; n long long ans; n struct blk { n   int x, y; n   blk(int xx = 0, int yy = 0) { n     x = xx; n     y = yy; n   } n   bool operator<(const blk& b) const { n     return x < b.x || (x == b.x && y < b.y); n   } n }; n vector<blk> data[2]; n void ins(int x, int y) { n   data[0].push_back(blk(x - y, x)); n   data[1].push_back(blk(x + y, x)); n } n void beam() { n   int ff = (dx != dy); n   blk p = ff ? blk(x + y, x) : blk(x - y, x); n   vector<blk>::iterator it = upper_bound(data[ff].begin(), data[ff].end(), p); n   for (; it->x != p.x; it--) n     ; n   if (dx < 0) n     for (; it->y >= x; it--) n       ; n   ans += abs(x - it->y) - 1; n   x = it->y, y = ff ? it->x - x : x - it->x; n   bool u = n       binary_search(data[0].begin(), data[0].end(), blk(x - y - dx, x - dx)); n   bool v = binary_search(data[0].begin(), data[0].end(), blk(x - y + dy, x)); n   if (u == v) { n     f = 1; n     dx *= -1; n     dy *= -1; n   } else if (u) { n     x -= dx; n     dy *= -1; n   } else if (v) { n     y -= dy; n     dx *= -1; n   } n } n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= m; i++) { n     ins(0, i); n     ins(n + 1, i); n   } n   for (int i = 0; i <= n + 1; i++) { n     ins(i, 0); n     ins(i, m + 1); n   } n   for (int i = 1; i <= k; i++) { n     scanf( %d%d , &x, &y); n     ins(x, y); n   } n   scanf( %d%d%s , &x, &y, c); n   dx = (c[0] ==  N ) ? -1 : 1, dy = (c[1] ==  W ) ? -1 : 1; n   sort(data[0].begin(), data[0].end()); n   sort(data[1].begin(), data[1].end()); n   beam(); n   int sx = x, sy = y, sdx = dx, sdy = dy; n   ans = 0; n   do beam(); n   while (!(x == sx && y == sy && dx == sdx && dy == sdy)); n   cout << ((f) ? (ans / 2) : ans) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const long long inf = 1e18; n const long double eps = 1e-8; n const double pi = acos(-1); n const long long hashh = 247; n char s[203040]; n long long p[203040], cnt[203040], hs[203040][2]; n long long get(long long l, long long r) { n   return (hs[r][l & 1] - hs[l - 1][l & 1] * p[cnt[r] - cnt[l - 1]] % mod + n           mod) % n          mod; n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int n, q; n   cin >> n >> s + 1 >> q; n   p[0] = 1; n   for (int i = 1; i < n + 1; i++) { n     p[i] = p[i - 1] * hashh % mod; n     cnt[i] = cnt[i - 1]; n     hs[i][0] = hs[i - 1][0]; n     hs[i][1] = hs[i - 1][1]; n     if (s[i] ==  0 ) { n       cnt[i]++; n       hs[i][0] = (hs[i - 1][0] * hashh + (i & 1) + 1) % mod; n       hs[i][1] = (hs[i - 1][1] * hashh + (i & 1 ^ 1) + 1) % mod; n     } n   } n   for (int i = 0; i < q; i++) { n     long long l1, l2, len; n     cin >> l1 >> l2 >> len; n     if (get(l1, l1 + len - 1) == get(l2, l2 + len - 1)) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, n, cnt = 0; n vector<vector<int> > g; n vector<int> outgroup; n int main() { n   cin >> n >> m; n   vector<int> emptyg; n   g.push_back(emptyg); n   for (int i = 1; i <= n; i++) { n     vector<int> subg; n     g.push_back(subg); n   } n   for (int i = 0; i < m; i++) { n     int fr, to; n     cin >> fr >> to; n     g[fr].push_back(to); n     g[to].push_back(fr); n   } n   while (true) { n     outgroup.clear(); n     cerr <<  cnt ==   << cnt << endl; n     for (int i = 1; i <= n; i++) { n       int deg = g[i].size(); n       cerr <<  node   << i <<   has degree   << deg <<   n ; n       if (deg == 1) { n         outgroup.push_back(i); n       } n       cerr.flush(); n     } n     if (outgroup.empty()) { n       cout << cnt << endl; n       return 0; n     } n     vector<int>::iterator it = outgroup.begin(); n     for (; it != outgroup.end(); it++) { n       int nodetodelete = *it; n       cerr <<  deleting node   << nodetodelete <<  ...  << endl; n       int onlyconnectednode; n       if (!g[nodetodelete].empty()) { n         onlyconnectednode = g[nodetodelete][0]; n       } else { n         continue; n       } n       vector<int>::iterator adjit = g[onlyconnectednode].begin(); n       for (; adjit != g[onlyconnectednode].end(); adjit++) { n         if (*adjit == nodetodelete) { n           g[onlyconnectednode].erase(adjit); n           break; n         } n       } n       g[nodetodelete].clear(); n     } n     cnt = cnt + 1; n     cerr <<  has the thread reached here?  << endl; n     bool isEnd = true; n     vector<vector<int> >::iterator ggit = g.begin(); n     for (; ggit != g.end(); ggit++) { n       vector<int> thissubg = *ggit; n       isEnd = thissubg.size() == 1 ? false : isEnd; n     } n     if (isEnd) { n       break; n     } n   } n   cout << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q, a[210000], b[210000]; n int tree[210000]; n char Getchar() { n   static char now[1 << 20], *S, *T; n   if (T == S) { n     T = (S = now) + fread(now, 1, 1 << 20, stdin); n     if (T == S) return EOF; n   } n   return *S++; n } n int read() { n   int x = 0, f = 1; n   char ch = Getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = Getchar(); n   } n   while (ch <=  9  && ch >=  0 ) x = x * 10 + ch -  0 , ch = Getchar(); n   return x * f; n } n void update(int x, int y) { n   for (; x <= n; x += x & -x) tree[x] ^= y; n } n int getnum(int x) { n   int res = 0; n   for (; x; x -= x & -x) res ^= tree[x]; n   return res; n } n struct Linear_Basis { n   int cnt, v[30]; n   void clr() { n     cnt = 0; n     memset(v, 0, sizeof(v)); n   } n   void ins(int x, int lim = 29) { n     if (cnt == 30) return; n     for (int i = lim; i >= 0 && x; i--) n       if (x & (1 << i)) { n         if (v[i]) n           x ^= v[i]; n         else { n           v[i] = x; n           cnt++; n           break; n         } n       } n   } n   void merge(const Linear_Basis &a) { n     for (int i = 29; i >= 0; i--) n       if (a.v[i]) ins(a.v[i], i); n   } n } tmp, t[810000]; n void pushup(int now) { n   t[now] = t[now << 1]; n   t[now].merge(t[now << 1 | 1]); n } n void build(int now, int l, int r) { n   if (l == r) { n     t[now].clr(); n     t[now].ins(b[l]); n     return; n   } n   int mid = (l + r) >> 1; n   build(now << 1, l, mid); n   build(now << 1 | 1, mid + 1, r); n   pushup(now); n } n void change(int now, int l, int r, int x) { n   if (l == r) { n     t[now].clr(); n     t[now].ins(b[l]); n     return; n   } n   int mid = (l + r) >> 1; n   if (x <= mid) n     change(now << 1, l, mid, x); n   else n     change(now << 1 | 1, mid + 1, r, x); n   pushup(now); n } n void query(int now, int l, int r, int x, int y) { n   if (l == x && r == y) { n     tmp.merge(t[now]); n     return; n   } n   int mid = (l + r) >> 1; n   if (y <= mid) n     query(now << 1, l, mid, x, y); n   else if (x > mid) n     query(now << 1 | 1, mid + 1, r, x, y); n   else { n     query(now << 1, l, mid, x, mid); n     query(now << 1 | 1, mid + 1, r, mid + 1, y); n   } n } n int main() { n   n = read(); n   q = read(); n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n     b[i] = a[i - 1] ^ a[i]; n     update(i, b[i]); n   } n   build(1, 1, n); n   int op, l, r, x; n   while (q--) { n     op = read(); n     l = read(); n     r = read(); n     if (op == 1) { n       x = read(); n       update(l, x); n       update(r + 1, x); n       b[l] ^= x; n       b[r + 1] ^= x; n       change(1, 1, n, l); n       if (r < n) change(1, 1, n, r + 1); n     } else { n       tmp.clr(); n       tmp.ins(getnum(l)); n       if (l < r) query(1, 1, n, l + 1, r); n       printf( %d n , 1 << tmp.cnt); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int x, y, a[3000], n, m, sum; n int main() { n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) { n     cin >> x >> y; n     sum += min(a[--x], a[--y]); n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n void init_ios() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n } n const int N = (1 << 19); n int n, m, jump[N][20]; n pair<int, int> a[N], drz[2 * N]; n void add(int v, int val, int ind) { n   v += N; n   if (val < drz[v].first) return; n   drz[v] = {val, ind}; n   while (v > 1) { n     v /= 2; n     if (drz[2 * v].first > drz[2 * v + 1].first) n       drz[v] = drz[2 * v]; n     else n       drz[v] = drz[2 * v + 1]; n   } n } n pair<int, int> get(int p, int q) { n   p += N; n   q += N; n   pair<int, int> res; n   if (drz[p].first > drz[q].first) n     res = drz[p]; n   else n     res = drz[q]; n   while (p / 2 != q / 2) { n     if (p % 2 == 0 && drz[p + 1].first > res.first) res = drz[p + 1]; n     if (q % 2 == 1 && drz[q - 1].first > res.first) res = drz[q - 1]; n     p /= 2; n     q /= 2; n   } n   return res; n } n int get_id(int id, int hm) { n   for (int i = 0; i < 20; ++i) n     if (hm & (1 << i)) id = jump[id][i]; n   return id; n } n int query(int x, int y) { n   int id = get(0, x).second; n   if (id == 0) return -1; n   if (a[id].second >= y) return 1; n   int x1 = 1, x2 = n - 1, x3; n   while (x2 - x1 > 1) { n     x3 = (x1 + x2) / 2; n     int id2 = get_id(id, x3); n     if (id2 == 0 || a[id2].second >= y) n       x2 = x3; n     else n       x1 = x3; n   } n   int id2 = get_id(id, x1); n   if (id2 != 0 && a[id2].second >= y) return 1 + x1; n   id2 = get_id(id, x2); n   if (id2 != 0 && a[id2].second >= y) return 1 + x2; n   return -1; n } n int main() { n   init_ios(); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i].first >> a[i].second; n     add(a[i].first, a[i].second, i); n   } n   for (int l = 0; l < 20; ++l) { n     for (int i = 1; i <= n; ++i) { n       if (l == 0) { n         pair<int, int> p = get(a[i].first, a[i].second); n         jump[i][l] = p.second; n       } else { n         jump[i][l] = jump[jump[i][l - 1]][l - 1]; n       } n     } n   } n   while (m--) { n     int x, y; n     cin >> x >> y; n     cout << query(x, y) <<   n ; n   } n } n


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


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


#include <bits/stdc++.h> n using namespace std; n void solve(int cnum) { n   int di[] = {-1, 0, 0, 1}; n   int dj[] = {0, -1, 1, 0}; n   int n, m; n   cin >> n >> m; n   vector<vector<int>> arr(n, vector<int>(m)); n   vector<vector<int>> ans(n, vector<int>(m)); n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < m; ++j) cin >> arr[i][j]; n   auto isvalid = [&](int i, int j) { n     if (i < 0 || j < 0) return false; n     if (i >= n || j >= m) return false; n     return true; n   }; n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) { n       int neicnt = 0; n       for (int k = 0; k < 4; ++k) { n         if (isvalid(i + di[k], j + dj[k])) neicnt++; n       } n       if (neicnt < arr[i][j]) { n         cout <<  NO n ; n         return; n       } n       ans[i][j] = neicnt; n     } n   } n   cout <<  YES n ; n   for (auto a : ans) { n     for (auto b : a) cout << b <<    ; n     cout <<   n ; n   } n } n int main() { n   int t; n   int count = 1; n   for (cin >> t; t--; solve(count), count++) n     ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005; n int n, k; n pair<int, int> a[N]; n long long b, s; n int main() { n   cin >> n >> k; n   cin >> b; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i].first); n     a[i].second = i; n   } n   sort(a + 1, a + n, greater<pair<int, int> >()); n   for (int i = 1; i <= k; i++) s += a[i].first; n   long long res = n; n   if (s > b) n     for (int i = 1; i <= k; i++) res = min(res, (long long)a[i].second); n   for (int i = k + 1; i < n; i++) n     if (s - a[k].first + a[i].first > b) res = min(res, (long long)a[i].second); n   cout << res << endl; 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     if (n % 2 == 0) { n       cout << n / 2 <<   n ; n     } else { n       cout << int(n / 2) + 1 <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxm = 200000 + 5; n const int maxn = 200000 + 5; n struct Edge { n   int from, to, w, id; n }; n vector<Edge> edges; n vector<int> G[maxn]; n int ans[maxm]; n int n, m; n int win[maxn], wall[maxn]; n inline void solve() { n   memset(ans, -1, sizeof ans); n   queue<int> Q; n   Q.push(1); n   while (!Q.empty()) { n     int now = Q.front(); n     Q.pop(); n     vector<int> tocheck; n     for (const int& i : G[now]) { n       Edge& e = edges[i]; n       if (ans[e.id] != -1) continue; n       int to; n       if (now == e.from) n         to = e.to, ans[e.id] = 0; n       else n         to = e.from, ans[e.id] = 1; n       win[to] += e.w; n       wall[to] -= e.w; n       if (to != n) tocheck.push_back(to); n     } n     for (const int& nx : tocheck) n       if (win[nx] == wall[nx]) Q.push(nx); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0, a, b, c; i < m; i++) { n     scanf( %d%d%d , &a, &b, &c); n     edges.push_back((Edge){a, b, c, i}); n     G[a].push_back((int)edges.size() - 1); n     G[b].push_back((int)edges.size() - 1); n     wall[a] += c; n     wall[b] += c; n   } n   solve(); n   for (int i = 0; i < m; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n queue<int> Q; n vector<int> edges[11000]; n int num[11000]; n int ans; n int n; n int bfs(int s) { n   int cnt = 0; n   for (int i = 0; i < edges[s].size(); i++) { n     cnt += edges[edges[s][i]].size() - 1; n   } n   return cnt; n } n int main(void) { n   int i; n   scanf( %d , &n); n   for (i = 1; i <= n - 1; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     edges[u].push_back(v); n     edges[v].push_back(u); n   } n   for (i = 1; i <= n; i++) { n     num[i] = bfs(i); n     ans += num[i]; n   } n   printf( %d , ans / 2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a, b; n   cin >> a >> b; n   if (a == b) { n     cout <<  infinity ; n     return 0; n   } n   if (a < b) { n     cout << 0; n     return 0; n   } n   long long int x, i, k, j; n   x = a - b; n   long long int sq; n   sq = sqrt((a - b) * 1.0); n   long long int cnt = 0; n   for (i = 1; i <= sq; i++) { n     if (x % i == 0) { n       j = i, k = x / i; n       if (j == k && j > b) n         cnt++; n       else if (j != k) { n         if (j > b) cnt++; n         if (k > b) cnt++; n       } n     } n   } n   cout << cnt; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans[120000], fa[120000], i, j, k, l, n, m, n2; n int get(int x) { return fa[x] == x ? x : (fa[x] = get(fa[x])); } n int main() { n   scanf( %d , &n); n   if (n & 1) return puts( -1 ), 0; n   for (i = 0; i < n; ++i) fa[i] = i; n   n2 = n / 2; n   for (i = 0; i < n2; ++i) { n     ans[i] = i * 2; n     ans[i + n2] = i * 2 + 1; n     fa[get(i)] = get(i * 2); n     fa[get(i + n2)] = get(i * 2 + 1); n   } n   for (i = 0; i < n2; ++i) n     if (get(i) != get(i + n2)) { n       swap(ans[i], ans[i + n2]); n       fa[get(i)] = get(i + n2); n     } n   for (i = 0;;) { n     printf( %d  , i); n     i = ans[i]; n     if (!i) break; n   } n   printf( 0 n ); n } n


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


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int t; n   cin >> t; n   while (t--) { n     long long n, x, sum = 0, ans = 0, cur; n     cin >> n >> x; n     priority_queue<long long> nums; n     for (int i = 0; i < n; ++i) { n       cin >> cur; n       nums.push(cur - x); n     } n     for (int i = 0; i < n && sum >= 0; ++i) { n       sum += nums.top(); n       nums.pop(); n       if (sum < 0) break; n       ++ans; n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n, v[500005], lastLeft[500005], lastRight[500005], solLeft[500005], n     solRight[500005], sol[500005]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> v[i]; n   } n   for (int i = 1; i <= n; ++i) { n     lastLeft[i] = 0; n   } n   for (int i = n; i >= 1; --i) { n     lastRight[i] = n + 1; n   } n   stack<pair<long long, long long> > st1; n   st1.push({1, v[1]}); n   for (int i = 2; i <= n; ++i) { n     while (!st1.empty() && v[i] <= st1.top().second) st1.pop(); n     if (!st1.empty()) lastLeft[i] = st1.top().first; n     st1.push({i, v[i]}); n   } n   stack<pair<long long, long long> > st2; n   st2.push({n, v[n]}); n   for (int i = n - 1; i >= 1; --i) { n     while (!st2.empty() && v[i] <= st2.top().second) st2.pop(); n     if (!st2.empty()) lastRight[i] = st2.top().first; n     st2.push({i, v[i]}); n   } n   long long big = 0; n   for (int varf = 1; varf <= n; ++varf) { n     solLeft[varf] = n         solLeft[lastLeft[varf]] + abs(lastLeft[varf] - varf) * v[varf]; n   } n   for (int varf = n; varf >= 1; --varf) { n     solRight[varf] = n         solRight[lastRight[varf]] + abs(lastRight[varf] - varf) * v[varf]; n   } n   for (int varf = 1; varf <= n; ++varf) { n     if (solLeft[varf] + solRight[varf] - v[varf] > big) n       big = solLeft[varf] + solRight[varf] - v[varf]; n   } n   for (int varf = 1; varf <= n; ++varf) { n     if (solLeft[varf] + solRight[varf] - v[varf] == big) { n       sol[varf] = v[varf]; n       for (int i = varf - 1; i >= 1; --i) { n         sol[i] = min(sol[i + 1], v[i]); n       } n       for (int i = varf + 1; i <= n; ++i) { n         sol[i] = min(sol[i - 1], v[i]); n       } n       for (int i = 1; i <= n; ++i) cout << sol[i] <<    ; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> v1, v2, v3; n int n, flag; n int main() { n   cin >> n; n   int t = 0; n   string s1, s2; n   cin >> s1 >> s2; n   for (int i = 0; i < n; i++) { n     if (s1[i] ==  1  && s2[i] ==  0 ) v1.push_back(i + 1); n     if (s1[i] ==  0  && s2[i] ==  1 ) v1.push_back(i + 1); n     if (s1[i] ==  1  && s2[i] ==  1 ) v2.push_back(i + 1); n     if (s1[i] ==  0  && s2[i] ==  0 ) v3.push_back(i + 1); n     if (s2[i] ==  1 ) t++; n   } n   int a = v1.size(), b = v2.size(), c = v3.size(); n   for (int i = 0; i <= b; i++) { n     int x = t - 2 * i; n     if (x >= 0 && n / 2 - i - x >= 0 && x <= a && n / 2 - i - x <= c) { n       a = x; n       b = i; n       c = n / 2 - i - x; n       flag = 1; n     } n   } n   if (flag) { n     for (int i = 0; i < a; i++) cout << v1[i] <<    ; n     for (int i = 0; i < b; i++) cout << v2[i] <<    ; n     for (int i = 0; i < c; i++) cout << v3[i] <<    ; n   } else n     cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<string> ans; n int main() { n   char c; n   string ss =   ; n   while ((c = getchar()) !=   n ) { n     if ( a  <= c &&  z  >= c) { n       ss += c; n       continue; n     } else if (c ==    ) { n       if (!ss.empty()) { n         ans.push_back(ss); n         ss =   ; n       } n     } else { n       if (!ss.empty()) { n         ans.push_back(ss); n         ss =   ; n       } n       ans[ans.size() - 1] += c; n     } n   } n   if (!ss.empty()) ans.push_back(ss); n   for (int i = 0; i < ans.size(); i++) { n     cout << ans[i]; n     if (i == ans.size() - 1) n       cout <<   n ; n     else n       cout <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n class SegTree { n  private: n   const ll NEUT = 4 * (ll)1e18; n   vector<ll> seg, tag; n   int h = 1; n   void apply(int i, ll v) { n     seg[i] += v; n     if (i < h) tag[i] += v; n   } n   void push(int i) { n     if (tag[i] == 0) return; n     apply(2 * i, tag[i]); n     apply(2 * i + 1, tag[i]); n     tag[i] = 0; n   } n   ll combine(ll a, ll b) { return min(a, b); } n   ll recGet(int a, int b, int i, int ia, int ib) { n     if (ib <= a || b <= ia) return NEUT; n     if (a <= ia && ib <= b) return seg[i]; n     push(i); n     int im = (ia + ib) >> 1; n     return combine(recGet(a, b, 2 * i, ia, im), n                    recGet(a, b, 2 * i + 1, im, ib)); n   } n   void recApply(int a, int b, ll v, int i, int ia, int ib) { n     if (ib <= a || b <= ia) return; n     if (a <= ia && ib <= b) n       apply(i, v); n     else { n       push(i); n       int im = (ia + ib) >> 1; n       recApply(a, b, v, 2 * i, ia, im); n       recApply(a, b, v, 2 * i + 1, im, ib); n       seg[i] = combine(seg[2 * i], seg[2 * i + 1]); n     } n   } n   int recFind(int a, int b, ll v, int i, int ia, int ib) { n     if (seg[i] > v) return b; n     if (b <= ia || ib <= a) return b; n     if (ib == ia + 1) return ia; n     push(i); n     int im = (ia + ib) >> 1; n     int off = recFind(a, b, v, 2 * i, ia, im); n     if (off < b) n       return off; n     else n       return recFind(a, b, v, 2 * i + 1, im, ib); n   } n  n  public: n   SegTree(int n) { n     while (h < n) h *= 2; n     seg.resize(2 * h, 0); n     tag.resize(h, 0); n   } n   ll rangeMin(int a, int b) { return recGet(a, b + 1, 1, 0, h); } n   void rangeAdd(int a, int b, ll v) { recApply(a, b + 1, v, 1, 0, h); } n   int findNext(int a, int b, ll v) { return recFind(a, b + 1, v, 1, 0, h); } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int n; n   cin >> n; n   vector<int> inv(n); n   for (int i = 0; i < n; ++i) { n     int v; n     cin >> v; n     inv[v - 1] = i; n   } n   int res = n - 1; n   SegTree seg(n); n   seg.rangeAdd(0, inv[n - 1], -1); n   for (int i = 0; i < n; ++i) { n     while (seg.rangeMin(0, n - 1) >= 0) { n       seg.rangeAdd(0, inv[res - 1], -1); n       --res; n     } n     cout << res + 1 <<    ; n     int q; n     cin >> q; n     seg.rangeAdd(0, q - 1, 1); n   } n   cout <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int64_t MAXN = 1000005; n const int64_t MAXINT = 2147483098; n const int64_t MAXLL = 9223372036854775258LL; n const int64_t MOD = 1e9 + 7; n void solve() { n   int64_t n; n   cin >> n; n   vector<int64_t> a(n + 2); n   for (int64_t i = 1; i < n + 1; ++i) cin >> a[i]; n   int64_t ans = a[1]; n   int64_t temp = 1; n   for (int64_t i = 3; i < n + 1; ++i) { n     if (a[i] == 1 && a[i] == a[i - 1]) n       temp++; n     else { n       ans += temp / 3; n       temp = 1; n     } n   } n   ans += temp / 3; n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int64_t t; n   cin >> t; n   while (t--) solve(); n   ; n   return 0; n } n


#include <bits/stdc++.h> n //#include <ext/pb_ds/assoc_container.hpp> n //using namespace __gnu_pbds; n using namespace std; n typedef long long ll; n typedef long double ld; n //typedef tree<int,null_type,less<int >,rb_tree_tag,tree_order_statistics_node_update>indexed_set; n template<class T1, class T2> n ostream &operator <<(ostream &os, pair<T1,T2>&p); n template <class T> n ostream &operator <<(ostream &os, vector<T>&v); n template <class T> n ostream &operator <<(ostream &os, set<T>&v); n template <class T1, class T2> n ostream &operator <<(ostream &os, map<T1,T2>&v); n #ifdef APURBA n #define HERE cout<< here -  <<__LINE__<<  n ; n #define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(),  , ,    ); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } n void err(istream_iterator<string> it) n { n     cout<<endl; n } n template<typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) n { n     cout << *it <<   =   << a <<    ; n     err(++it, args...); n } n #else n #define HERE n #define debug(args...) n #endif n const int N = 5e5 +5; n const ll mod = 1e9 + 7; n typedef pair<int,int> pii; n struct info{ n     int zero , one; n     int lazy; n     info() n     { n         zero = 0; n         one = 0; n         lazy = 0; n     } n }; n  n struct segtree{ n     int n; n     vector<info>t; n  n     segtree(int n, string &s) n     { n         this->n = n; n         t.resize(n*4); n         build(1,0,n-1,s); n     } n     void Merge(info &node , info &l , info &r) n     { n         node.zero = l.zero + r.zero; n         node.one = l.one + r.one; n     } n     void build(int node, int l ,int r,string &s) n     { n         if(l==r) n         { n             if(s[l]== 0 ) n             { n                 t[node].zero = 1; n             } n             else{ n                 t[node].one = 1; n             } n             return; n         } n         int mid= (l+r)/2; n         build(node*2 , l, mid,s); n         build(node *2 +1 , mid+1, r,s); n         Merge(t[node], t[node*2] , t[node *2+1]); n     } n     void push_down(int node , int  l, int r, int b, int mid , int e) n     { n         if(t[node].lazy==1) n         { n             t[l].zero = (mid-b+1); n             t[l].one = 0; n             t[l].lazy = 1; n             t[r].zero = (e-mid); n             t[r].one = 0; n             t[r].lazy = 1; n             t[node].lazy =0; n         } n         else if(t[node].lazy == 2) n         { n             t[l].one = (mid-b+1); n             t[l].zero = 0; n             t[l].lazy = 2; n             t[r].one = (e-mid); n             t[r].zero = 0; n             t[r].lazy = 2; n             t[node].lazy =0; n         } n     } n     void update(int node, int l ,int r , int i, int j, int add) n     { n         if(l>j || r<i) n         { n             return; n         } n         if(l>=i && r<=j) n         { n             if(add==0) n             { n                 t[node].zero = (r-l+1); n                 t[node].one = 0; n                 t[node].lazy = 1; n             } n             else{ n                 t[node].one = (r-l+1); n                 t[node].zero = 0; n                 t[node].lazy = 2; n             } n             return; n         } n         int mid = (l+r)/2; n         push_down(node, node*2, node*2+1 , l, mid, r); n         update(node *2 , l ,mid , i, j, add); n         update(node *2 +1 , mid+1 , r , i, j, add); n         Merge(t[node] , t[node *2] , t[node*2+1]); n     } n     void update(int l ,int r, int add) n     { n         update(1,0,n-1,l,r,add); n     } n     info query(int node, int l, int r, int i, int j) n     { n         if(l>j || r<i) n         { n             info tmp; n             return tmp; n         } n         if(l>=i && r<=j) n         { n             return t[node]; n         } n  n         int mid = (l+r)/2; n         push_down(node, node*2, node*2+1,l,mid,r); n         info x = query(node *2 , l ,mid , i, j); n         info y = query(node *2 +1 , mid+1 , r , i, j); n         info ret; n         Merge(ret  , x, y); n         return ret; n     } n     pii query(int l, int r) n     { n         info ret = query(1,0,n-1,l,r); n         return make_pair(ret.zero , ret.one); n     } n     void show() n     { n         for(int i=0;i<n;i++) n         { n             pii tmp = query(i,i); n             cout<<tmp<<   ; n         } n         cout<<endl; n     } n }; n  n  n void TEST_CASES(int cas) n { n     int n,q; n     string s,t; n     cin>>n>>q>>s>>t; n     vector<pii>a(q); n     for(int i=0;i<q;i++) n     { n         int l,r; n         cin>>l>>r; n         l--; n         r--; n         a[i] = {l,r}; n     } n     segtree seg(n,t); n     //seg.show(); n     for(int i=q-1;i>=0;i--) n     { n         int l = a[i].first , r = a[i].second; n         int sz = (r-l+1); n         pii koto = seg.query(l,r); n         //debug(i,koto); n         if(koto.first*2<sz) n         { n             //HERE; n             seg.update(l,r,1); n         } n         else if(koto.second*2<sz) n         { n             seg.update(l,r,0); n         } n         else{ n             cout<< NO n ; n             return; n         } n         //seg.show(); n     } n     for(int i=0;i<n;i++) n     { n         int eta = s[i]- 0 ; n         pii bal = seg.query(i,i); n         if(eta==0 && bal.second==1) n         { n             cout<< NO n ; n             return; n         } n         else if(eta==1 && bal.first==1) n         { n             cout<< NO n ; n             return; n         } n     } n     cout<< YES n ; n  n  n } n  n  n  n  n /* n  n */ n  n int32_t main() n { n #ifndef APURBA n     ios_base::sync_with_stdio(false); n     cin.tie(nullptr); n #endif n     //freopen( input.txt , r ,stdin); n     //freopen( out1.txt , w ,stdout); n     int t=1,cas=0; n     cin>>t; n     while(t--) n     { n         TEST_CASES(++cas); n     } n     return 0; n } n  n  n  n template<class T1, class T2> n ostream &operator <<(ostream &os, pair<T1,T2>&p) n { n     os<< { <<p.first<< ,  <<p.second<< }  ; n     return os; n } n template <class T> n ostream &operator <<(ostream &os, vector<T>&v) n { n     os<< [  ; n     for(int i=0; i<v.size(); i++) n     { n         os<<v[i]<<    ; n     } n     os<<  ] ; n     return os; n } n  n template <class T> n ostream &operator <<(ostream &os, set<T>&v) n { n     os<< [  ; n     for(T i:v) n     { n         os<<i<<   ; n     } n     os<<  ] ; n     return os; n } n template <class T1, class T2> n ostream &operator <<(ostream &os, map<T1,T2>&v) n { n     for(auto i:v) n     { n         os<< Key :  <<i.first<<  , Value :  <<i.second<<endl; n     } n     return os; n } n  n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n ostream& operator<<(ostream& o, const pair<T, U>& x) { n   o <<  (  << x.first <<  ,   << x.second <<  ) ; n   return o; n } n template <typename T> n ostream& operator<<(ostream& o, const vector<T>& x) { n   o <<  [ ; n   int b = 0; n   for (auto& a : x) o << (b++ ?  ,   :   ) << a; n   o <<  ] ; n   return o; n } n template <typename T> n ostream& operator<<(ostream& o, const set<T>& x) { n   o <<  { ; n   int b = 0; n   for (auto& a : x) o << (b++ ?  ,   :   ) << a; n   o <<  } ; n   return o; n } n template <typename T> n ostream& operator<<(ostream& o, const multiset<T>& x) { n   o <<  { ; n   int b = 0; n   for (auto& a : x) o << (b++ ?  ,   :   ) << a; n   o <<  } ; n   return o; n } n template <typename T> n ostream& operator<<(ostream& o, const unordered_set<T>& x) { n   o <<  { ; n   int b = 0; n   for (auto& a : x) o << (b++ ?  ,   :   ) << a; n   o <<  } ; n   return o; n } n template <typename T> n ostream& operator<<(ostream& o, const unordered_multiset<T>& x) { n   o <<  { ; n   int b = 0; n   for (auto& a : x) o << (b++ ?  ,   :   ) << a; n   o <<  } ; n   return o; n } n template <typename T, typename U> n ostream& operator<<(ostream& o, const map<T, U>& x) { n   o <<  { ; n   int b = 0; n   for (auto& a : x) o << (b++ ?  ,   :   ) << a; n   o <<  } ; n   return o; n } n template <typename T, typename U> n ostream& operator<<(ostream& o, const multimap<T, U>& x) { n   o <<  { ; n   int b = 0; n   for (auto& a : x) o << (b++ ?  ,   :   ) << a; n   o <<  } ; n   return o; n } n template <typename T, typename U> n ostream& operator<<(ostream& o, const unordered_map<T, U>& x) { n   o <<  { ; n   int b = 0; n   for (auto& a : x) o << (b++ ?  ,   :   ) << a; n   o <<  } ; n   return o; n } n template <typename T, typename U> n ostream& operator<<(ostream& o, const unordered_multimap<T, U>& x) { n   o <<  { ; n   int b = 0; n   for (auto& a : x) o << (b++ ?  ,   :   ) << a; n   o <<  } ; n   return o; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, k; n     string s; n     cin >> n >> k >> s; n     vector<int> A(n); n     for (int i = 0; i < n; i++) { n       if (s[i] ==  R ) A[i] = 0; n       if (s[i] ==  G ) A[i] = 1; n       if (s[i] ==  B ) A[i] = 2; n     } n     int ans = k; n     for (int o = 0; o < 3; o++) { n       int x = 0; n       for (int i = 0; i < k; i++) { n         if (A[i] != (o + i) % 3) x++; n       } n       ans = min(ans, x); n       for (int i = k; i < n; i++) { n         if (A[i - k] != (o + i - k) % 3) x--; n         if (A[i] != (o + i) % 3) x++; n         ans = min(ans, x); n       } n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n char line[100010]; n vector<int> cua; n int main(int argc, char *argv[]) { n   scanf( %d%d , &n, &k); n   scanf( %c , &line[0]); n   for (int i = 1; i <= n; i++) { n     scanf( %c , &line[i]); n     if (line[i] ==  7  && line[i - 1] ==  4 ) { n       cua.push_back(i - 1); n     } n   } n   int p; n   int cont = 0; n   while (!cua.empty() && cont < k) { n     cont += 1; n     p = cua[0]; n     if (p % 2) { n       line[p + 1] =  4 ; n       if (p + 2 <= n && line[p + 2] ==  7 ) { n         cua[0] = p + 1; n       } else { n         cua.erase(cua.begin()); n       } n     } else { n       line[p] =  7 ; n       if ((p - 1) >= 1 && line[p - 1] ==  4 ) { n         cua[0] = p - 1; n         if ((k - cont) % 2 == 1) { n           line[p] =  4 ; n         } n         break; n       } else { n         cua.erase(cua.begin()); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     printf( %c , line[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n const int INF = (int)(1e9 + 1337); n const long long LINF = (long long)(4e18); n const double EPS = 1e-11; n const int MOD = 1000000007; n const int HBASE = 1003; n const int MAXN = 200500; n int n, m; n char s[MAXN]; n pair<int, int> qu[MAXN]; n int cnt[10][MAXN]; n int last[MAXN][10]; n int ans[MAXN]; n int d[4] = {2, 0, 1, 6}; n struct node { n   int dp[4][4]; n   node() { memset(dp, 0, sizeof(dp)); } n }; n node tre[MAXN << 2]; n int get_sum(int d, int l, int r) { return cnt[d][r] - cnt[d][l - 1]; } n void init() { n   scanf( %d%d%s , &n, &m, &s[1]); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d , &qu[i].first, &qu[i].second); n   } n } n node merge(node n1, node n2) { n   node res; n   for (int i = 3; i >= 0; i--) { n     res.dp[i][i] = n1.dp[i][i] + n2.dp[i][i]; n     for (int j = i + 1; j < 4; j++) { n       res.dp[i][j] = INF; n       for (int h = i; h <= j; h++) { n         res.dp[i][j] = min(n1.dp[i][h] + n2.dp[h][j], res.dp[i][j]); n       } n     } n   } n   return res; n } n void build(int tv, int tl, int tr) { n   if (tl == tr) { n     for (int i = 0; i < 4; i++) { n       if (s[tl] ==  0  + d[i]) { n         tre[tv].dp[i][i]++; n       } n     } n   } else { n     int tm = (tl + tr) >> 1; n     build(tv * 2, tl, tm); n     build(tv * 2 + 1, tm + 1, tr); n     tre[tv] = merge(tre[tv * 2], tre[tv * 2 + 1]); n   } n } n node get(int tv, int tl, int tr, int l, int r) { n   if (l > r) { n     return node(); n   } n   if (tl == l && tr == r) { n     return tre[tv]; n   } n   int tm = (tl + tr) >> 1; n   node la = get(tv << 1, tl, tm, l, min(r, tm)); n   node ra = get(tv * 2 + 1, tm + 1, tr, max(l, tm + 1), r); n   return merge(la, ra); n } n void solve() { n   init(); n   for (int i = 1; i <= n; i++) { n     for (int j = 0; j < 10; j++) { n       cnt[j][i] = cnt[j][i - 1]; n     } n     cnt[s[i] -  0 ][i]++; n   } n   for (int i = 1; i <= n + 1; i++) { n     for (int j = 0; j < 10; j++) { n       last[i][j] = last[i - 1][j]; n     } n     if (i > 0) { n       last[i][s[i - 1] -  0 ] = i - 1; n     } n   } n   build(1, 1, n); n   for (int i = 1; i <= m; i++) { n     int r = qu[i].second + 1; n     int p = last[r][7]; n     if (!p || p < qu[i].first) { n       ans[i] = -1; n       continue; n     } n     int pos[4]; n     pos[3] = p; n     for (int j = 2; j >= 0; j--) { n       pos[j] = last[pos[j + 1]][d[j]]; n       if (!pos[j] || pos[j] < qu[i].first) { n         ans[i] = -1; n         break; n       } n     } n     if (ans[i] != -1) { n       node res[6]; n       res[1] = get(1, 1, n, qu[i].first, pos[0] - 1); n       res[2] = get(1, 1, n, pos[0] + 1, pos[1] - 1); n       res[3] = get(1, 1, n, pos[1] + 1, pos[2] - 1); n       int dp[4][4]; n       memset(dp, 63, sizeof(dp)); n       memset(dp[0], 0, sizeof(dp[0])); n       for (int p = 1; p <= 3; p++) { n         for (int j = p; j < 4; j++) { n           for (int h = 0; h <= j; h++) { n             dp[p][j] = min(dp[p][j], dp[p - 1][h] + res[p].dp[h][j]); n           } n         } n       } n       ans[i] = dp[3][3] + get_sum(6, pos[2] + 1, pos[3] - 1) + n                get_sum(6, pos[3] + 1, qu[i].second); n     } n   } n   for (int i = 1; i <= m; i++) { n     printf( %d n , ans[i]); n   } n } n void precalc() {} n int main() { n   srand(88); n   double st = clock(); n   precalc(); n   int tests = 1; n   for (int i = 1; i <= tests; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int k, d; n char n[100005]; n int a[100005]; n int main() { n   scanf( %d , &k); n   d = 0; n   scanf(  %s , &n); n   for (int i = 0; i < strlen(n); i++) { n     d += n[i] -  0 ; n     a[i + 1] = n[i] -  0 ; n   } n   if (d >= k) { n     cout << 0; n     return 0; n   } n   int h = strlen(n); n   sort(a + 1, a + h + 1); n   for (int i = 1; i <= h; ++i) { n     d = d + 9 - a[i]; n     if (d >= k) { n       cout << i; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1); n const long long mod = 1e9 + 7; n const long long N = 1e5 + 5, K = 105; n const int si = (1 << 20); n const long long add = 1e4; n const long long M = 100 + 5; n const double INFdb = 1e10; n int n; n vector<int> v[N]; n bool check[N]; n int legs[N]; n void dfs(int u, int par) { n   if (v[u].size() <= 2) { n     check[u] = 1; n     for (int x : v[u]) { n       if (x != par) { n         dfs(x, u); n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n - 1; ++i) { n     int x, y; n     cin >> x >> y; n     v[x].push_back(y); n     v[y].push_back(x); n   } n   for (int i = 1; i <= n; ++i) { n     if ((int)v[i].size() == 1) dfs(i, 0); n   } n   for (int i = 1; i <= n; ++i) { n     for (int x : v[i]) { n       if (check[x]) { n         legs[i] = min(legs[i] + 1, 2); n       } n     } n   } n   for (int i = 1; i <= n; ++i) { n     if (!check[i]) { n       int cnt = 0; n       for (int x : v[i]) { n         if (!check[x] && v[x].size() - legs[x] > 1) { n           ++cnt; n         } n       } n       if (cnt > 2) { n         return cout <<  NO , 0; n       } n     } n   } n   cout <<  YES ; n   cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n int main() { n   cin >> N; n   if (!(N % 2)) n     puts( home ); n   else n     puts( contest ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<short>> g; n vector<vector<short>> dp; n short rec(int l, int r) { n   if (l > r) return 0; n   if (dp[l][r] != -1) return dp[l][r]; n   short whole = binary_search(begin(g[l]), end(g[l]), r); n   short ret = whole + rec(l + 1, r); n   for (auto p : g[l]) { n     if (p >= r) break; n     int val = whole + rec(l, p) + rec(p + 1, r); n     if (val > ret) { n       ret = val; n     } n   } n   return dp[l][r] = ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int tc; n   cin >> tc; n   while (tc--) { n     int n; n     cin >> n; n     vector<pair<int, int>> arr(n); n     vector<int> val; n     for (auto& pp : arr) { n       cin >> pp.first >> pp.second; n       val.push_back(pp.first); n       val.push_back(pp.second); n     } n     sort(begin(val), end(val)); n     val.erase(unique(begin(val), end(val)), end(val)); n     int m = int(val.size()); n     g = vector<vector<short>>(m); n     vector<short> cnt(m + 1, 0); n     for (auto& pp : arr) { n       pp.first = lower_bound(begin(val), end(val), pp.first) - begin(val); n       pp.second = lower_bound(begin(val), end(val), pp.second) - begin(val); n       cnt[pp.first]++; n     } n     for (int i = 0; i < m; i++) { n       g[i].resize(cnt[i]); n     } n     fill(begin(cnt), end(cnt), 0); n     for (auto& pp : arr) { n       g[pp.first][cnt[pp.first]++] = pp.second; n     } n     cnt.clear(); n     val.clear(); n     for (auto& row : g) { n       sort(begin(row), end(row)); n       row.erase(unique(begin(row), end(row)), end(row)); n     } n     dp = vector<vector<short>>(m, vector<short>(m, -1)); n     cout << rec(0, m - 1) <<   n ; n   } n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n char str[MAXN]; n class Value { n  public: n   char c1, c2, last_c1, last_c2; n   int pos, l1, sz; n   Value() { n     c1 = 0; n     c2 = 0; n     pos = 0; n     l1 = 0; n     last_c1 = 0; n     last_c2 = 0; n   } n   void push_front(char c) { n     if (c == c1) { n       l1++; n     } else { n       c2 = c1; n       c1 = c; n       l1 = 1; n     } n     if (last_c2 == 0) { n       last_c2 = c; n     } else if (last_c1 == 0) { n       last_c1 = c; n     } n     sz++; n   } n   bool operator<(const Value &value) const { n     if (c1 != value.c1) { n       return c1 < value.c1; n     } n     char cl = c1, cr = c1; n     if (l1 < value.l1) { n       cl = c2; n     } else if (value.l1 < l1) { n       cr = value.c2; n     } n     return cl < cr; n   } n } f[MAXN]; n void print(int idx, int cnt) { n   for (int i = 0; i < cnt; i++) { n     putchar(f[idx].c1); n     idx = f[idx].pos + 1; n   } n } n int main() { n   scanf( %s , str); n   int len = strlen(str); n   f[len].pos = len; n   for (int i = len - 1; i >= 0; i--) { n     f[i] = f[i + 1]; n     f[i].push_front(str[i]); n     f[i].pos = i; n     if (i + 1 < len && str[i] == str[i + 1]) { n       f[i] = min(f[i], f[i + 2]); n     } n   } n   for (int i = 0; i < len; i++) { n     printf( %d  , f[i].sz); n     if (f[i].sz <= 10) { n       print(i, f[i].sz); n       putchar(  n ); n     } else { n       print(i, 5); n       printf( ...%c%c n , f[i].last_c1, f[i].last_c2); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n long long int mypow(long long int a, int b) { n   if (b < 0) return 0; n   long long int ans = 1; n   for (; b; b >>= 1, a = a * a % mod) { n     if (b & 1) ans = ans * a % mod; n   } n   return ans; n } n void add(long long int &a, long long int b) { n   a += b; n   if (a >= mod) a -= mod; n } n int main() { n   int n, k; n   cin >> n >> k; n   long long int dp[k + 1][k + 1]; n   for (int i = (0); i < (k + 1); i++) { n     for (int j = (0); j < (k + 1); j++) { n       if (i) { n         dp[i][j] = 0; n         add(dp[i][j], j * dp[i - 1][j] % mod); n         if (n > j) add(dp[i][j], (n - j) * dp[i - 1][j + 1] % mod); n       } else { n         dp[i][j] = mypow(2, n - j); n       } n     } n   } n   cout << dp[k][0] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n double pi = acos(-1); n int n; n bool initial[2000][2000]; n bool blur[2000][2000]; n pair<int, int> direction[8] = { n     make_pair(1, 0),  make_pair(1, 1),   make_pair(0, 1),  make_pair(-1, 1), n     make_pair(-1, 0), make_pair(-1, -1), make_pair(0, -1), make_pair(1, -1)}; n pair<int, int> operator+(pair<int, int> l, pair<int, int> r) { n   return make_pair(l.first + r.first, l.second + r.second); n } n bool valid(pair<int, int> pos) { n   return pos.first >= 0 && pos.first < n && pos.second >= 0 && pos.second < n; n } n int circle(int x, int y) { n   vector<pair<int, int> > front(1, make_pair(x, y)); n   blur[x][y] = 0; n   for (size_t i = 0; i < front.size(); i++) n     for (auto m : direction) { n       auto next = front[i] + m; n       if (valid(next) && blur[next.first][next.second]) { n         front.push_back(next); n         blur[next.first][next.second] = 0; n       } n     } n   pair<double, double> centre = make_pair(0, 0); n   for (auto c : front) n     centre = make_pair(centre.first + 1.0 * c.first / front.size(), n                        centre.second + 1.0 * c.second / front.size()); n   double radius = 0; n   for (auto c : front) n     radius = max(radius, sqrt(pow(c.first - centre.first, 2.0) + n                               pow(c.second - centre.second, 2.0))); n   radius += 0.5; n   if (radius < 6) return 0; n   return 1 + (front.size() > (pow(radius, 2) * 2 + pow(radius, 2) * pi) / 2); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) cin >> initial[i][j]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) { n       int cnt = 0; n       for (int di = -2; di <= 2; di++) n         for (int dj = -2; dj <= 2; dj++) n           if (i + di >= 0 && i + di < n && j + dj >= 0 && j + dj < n && n               initial[i + di][j + dj]) n             cnt++; n       blur[i][j] = (cnt > 5 * 5 / 2); n     } n   int squares = 0, circles = 0; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       if (blur[i][j]) { n         int ret = circle(i, j); n         if (ret == 1) squares++; n         if (ret == 2) circles++; n       } n   cout << circles <<     << squares <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const char NL =   n ; n long long int n; n vector<long long int> a, dp; n long long int s(long long int i, long long int j) { n   if (a[i] > a[j]) return dp[j] + 1; n   return 1; n } n void solve() { n   for (long long int i = 2; i < n + 1; i++) { n     long long int m = 1; n     for (long long int j = 1; (j * j <= i && j <= n); j++) { n       if (i % j == 0) { n         m = max({m, s(i, j), s(i, i / j)}); n       } n     } n     dp[i] = m; n   } n   cout << *max_element(dp.begin(), dp.end()) << NL; n   return; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long int t; n   cin >> t; n   while (t--) { n     cin >> n; n     a.assign(n + 1, 0); n     dp.assign(n + 1, 0); n     dp[1] = 1; n     for (long long int i = 1; i < n + 1; i++) { n       cin >> a[i]; n     } n     solve(); n   } n   fflush(stdin); n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = 0; n int n, m, k; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n string direc =  RDLU ; n long long ln, lk, lm; n void etp(bool f = 0) { n   puts(f ?  YES  :  NO ); n   exit(0); n } n void addmod(int &x, int y, int mod = 1000000007) { n   x += y; n   if (x >= mod) x -= mod; n } n void et() { n   puts( -1 ); n   exit(0); n } n struct ant { n   long long pos; n   char d; n   void rd() { n     cin >> pos >> d; n     pos--; n   } n   bool operator<(const ant &o) const { return pos < o.pos; } n } a[300105]; n long long t; n int ans[300105]; n vector<pair<int, int> > pre; n void fmain() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> t; n   for (int(i) = 0; (i) < (int)(n); (i)++) a[i].rd(); n   for (int(i) = 0; (i) < (int)(n); (i)++) pre.push_back({a[i].pos, i}); n   sort(pre.begin(), pre.end()); n   long long offset = 0; n   for (int(i) = 0; (i) < (int)(n); (i)++) { n     if (a[i].d ==  R ) { n       (offset += (a[i].pos + t) / m) %= n; n       a[i].pos = (t + a[i].pos) % m; n     } else { n       if (t >= 1 + a[i].pos) { n         offset += n - 1; n         offset += n - (t - (a[i].pos + 1)) / m % n; n         offset %= n; n       } n       a[i].pos = ((long long)a[i].pos - t) % m + m; n       a[i].pos %= m; n     } n   } n   sort(a, a + n); n   for (int(i) = 0; (i) < (int)(n); (i)++) n     ans[pre[i].second] = a[(i + offset) % n].pos + 1; n   for (int(i) = 0; (i) < (int)(n); (i)++) cout << ans[i] <<    ; n } n int main() { n   fmain(); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int N; n   std::cin >> N; n   std::vector<int> array(N); n   for (int i = 0; i < N; ++i) std::cin >> array[i]; n   std::sort(array.begin(), array.end()); n   if (1 == array.back()) n     array[N - 1] = 2; n   else n     array[N - 1] = 1; n   std::sort(array.begin(), array.end()); n   for (int i = 0; i < N; ++i) std::cout << array[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long tree[4 * 1000005]; n void update(int v, int l, int r, int ind) { n   if (l > r || l > ind || ind > r) return; n   if (l == r) { n     tree[v] = 1; n     return; n   } n   int mid = (l + r) / 2; n   update(2 * v, l, mid, ind); n   update(2 * v + 1, mid + 1, r, ind); n   tree[v] = tree[2 * v] + tree[2 * v + 1]; n } n long long query(int v, int l, int r, int lt, int rt) { n   if (l > r || l > rt || lt > r || lt > rt) return 0; n   if (lt <= l && r <= rt) return tree[v]; n   int mid = (l + r) / 2; n   return query(2 * v, l, mid, lt, rt) + query(2 * v + 1, mid + 1, r, lt, rt); n } n int n; n int a; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   long long inv = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a; n     inv += query(1, 1, n, a + 1, n); n     update(1, 1, n, a); n   } n   if (inv % 2 == n % 2) n     cout <<  Petr n ; n   else n     cout <<  Um_nik n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long iinf = 1e9 + 10; n const long long inf = 1ll << 60; n const long long mod = 1e9 + 87; n void GG() { n   cout <<  0 n ; n   exit(0); n } n long long mpow(long long a, long long n, long long mo = mod) { n   long long re = 1; n   while (n > 0) { n     if (n & 1) re = re * a % mo; n     a = a * a % mo; n     n >>= 1; n   } n   return re; n } n long long inv(long long b, long long mo = mod) { n   if (b == 1) return b; n   return (mo - mo / b) * inv(mo % b, mo) % mo; n } n const long long maxn = 1e6 + 5; n unsigned long long H[maxn]; n bool np[maxn]; n unsigned long long val[maxn]; n long long n; n void RET(vector<long long> no) { n   vector<long long> yay(n + 1); n   for (long long t : no) yay[t] = 1; n   cout << n - (long long)no.size() <<   n ; n   for (long long i = 1; i <= n; ++i) { n     if (!yay[i]) cout << i <<    ; n   } n   cout <<   n ; n   exit(0); n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   mt19937_64 rnd(time(0)); n   for (long long i = 0; i < maxn; ++i) { n     val[i] = rnd(); n   } n   for (long long i = 2; i < maxn; ++i) { n     if (!np[i]) { n       for (long long j = i; j < maxn; j += i) { n         if (j != i) np[j] = 1; n         long long r = j; n         while (r % i == 0) { n           r /= i; n           H[j] ^= val[i]; n         } n       } n     } n   } n   cin >> n; n   for (long long i = 1; i < maxn; ++i) H[i] ^= H[i - 1]; n   unsigned long long all = 0; n   for (long long i = 1; i <= n; ++i) all ^= H[i]; n   if (all == 0) { n     RET({}); n   } n   for (long long i = 1; i <= n; ++i) { n     if (H[i] == all) { n       RET({i}); n     } n   } n   vector<pair<unsigned long long, long long> > v; n   for (long long i = 1; i <= n; ++i) { n     v.push_back({H[i], i}); n   } n   sort(v.begin(), v.end()); n   for (long long i = 1; i <= n; ++i) { n     long long at = n         lower_bound(v.begin(), v.end(), n                     pair<unsigned long long, long long>{H[i] ^ all, i + 1}) - n         v.begin(); n     if (at < (long long)v.size() && (v[at].first == (H[i] ^ all))) { n       RET({i, v[at].second}); n     } n   } n   assert(n % 2 == 1); n   RET({n, n / 2, 2}); n } n


#include <bits/stdc++.h> n using namespace std; n string ans; n map<pair<string, int>, int> dp; n bool check(string second) { n   int mul = 1; n   int num = 0; n   int i = second.size() - 1; n   num = num + ((second[i] -  0 ) * 1) % 8; n   i--; n   if (second.size() > 1) { n     num = num + ((second[i] -  0 ) * 10) % 8; n   } n   i--; n   if (second.size() > 2) { n     num = num + ((second[i] -  0 ) * 100) % 8; n   } n   if (num % 8 == 0) { n     ans = second; n     return true; n   } else { n     return false; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   int a[n]; n   vector<int> v[1001]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     v[a[i]].push_back(i); n   } n   vector<int> ans; n   for (int i = 1; i < 1001; i++) { n     if (v[i].size() > 0) ans.push_back(v[i][v[i].size() - 1]); n   } n   sort(ans.begin(), ans.end()); n   cout << ans.size() << endl; n   for (int i = 0; i < ans.size(); i++) { n     cout << a[ans[i]] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[5010]; n int main() { n   scanf( %s , s); n   int n = (int)strlen(s); n   int res = 0; n   for (int i = 0; i < n; i++) { n     int mn = 0, mx = 0; n     for (int j = i; j < n; j++) { n       if (s[j] ==  ( ) { n         mn++, mx++; n       } else if (s[j] ==  ) ) { n         mn--, mx--; n       } else if (s[j] ==  ? ) { n         mn--, mx++; n       } n       if (mx < 0) break; n       if (mn < 0 && mn < mx) mn += 2; n       if (mn == 0) res++; n     } n   } n   printf( %d , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n template <class S, class T> n ostream& operator<<(ostream& os, const pair<S, T>& p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <class T> n ostream& operator<<(ostream& os, const vector<T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class T> n ostream& operator<<(ostream& os, const unordered_set<T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class S, class T> n ostream& operator<<(ostream& os, const unordered_map<S, T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class T> n ostream& operator<<(ostream& os, const set<T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class T> n ostream& operator<<(ostream& os, const multiset<T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class S, class T> n ostream& operator<<(ostream& os, const map<S, T>& p) { n   os <<  [  ; n   for (auto& it : p) os << it <<    ; n   return os <<  ] ; n } n template <class T> n void dbs(string str, T t) { n   cerr << str <<   :   << t <<   n ; n } n template <class T, class... S> n void dbs(string str, T t, S... s) { n   long long idx = str.find( , ); n   cerr << str.substr(0, idx) <<   :   << t <<  , ; n   dbs(str.substr(idx + 1), s...); n } n template <class T> n void prc(T a, T b) { n   cerr <<  [ ; n   for (T i = a; i != b; ++i) { n     if (i != a) cerr <<  ,  ; n     cerr << *i; n   } n   cerr <<  ] n ; n } n long long power(long long x, long long y) { n   long long res = 1; n   while (y) { n     if (y & 1) res = (res * x) % 1000000007; n     y = y / 2, x = (x * x) % 1000000007; n   } n   return res % 1000000007; n } n long long dp[500001][2]; n vector<vector<pair<long long, long long> > > arr(500001); n long long n, k; n bool cmp(pair<long long, long long> a, pair<long long, long long> b) { n   return ((dp[a.first][1] + a.second - dp[a.first][0]) > n           (dp[b.first][1] + b.second - dp[b.first][0])); n } n void dfs(long long u, long long par) { n   vector<pair<long long, long long> > temp; n   for (auto v : arr[u]) { n     if (v.first != par) { n       dfs(v.first, u); n       temp.push_back(v); n     } n   } n   if ((long long)temp.size() == 0) return; n   sort(temp.begin(), temp.end(), cmp); n   long long ma = 0; n   for (long long i = 0; i < (min((long long)temp.size(), k - 1)); i++) { n     long long v = temp[i].first; n     if (dp[v][1] + temp[i].second - dp[v][0] > 0) n       ma += (dp[v][1] + temp[i].second); n     else n       ma += dp[v][0]; n   } n   for (long long i = k - 1; i < (long long)temp.size(); i++) n     ma += dp[temp[i].first][0]; n   dp[u][1] = ma; n   ma = 0; n   for (long long i = 0; i < (min((long long)temp.size(), k)); i++) { n     long long v = temp[i].first; n     if (dp[v][1] + temp[i].second - dp[v][0] > 0) n       ma += (dp[v][1] + temp[i].second); n     else n       ma += dp[v][0]; n   } n   for (long long i = k; i < (long long)temp.size(); i++) n     ma += dp[temp[i].first][0]; n   dp[u][0] = ma; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     arr.clear(); n     cin >> n >> k; n     arr.resize(n + 1); n     long long i; n     for (i = 0; i < n - 1; i++) { n       long long u, v, w; n       cin >> u >> v >> w; n       arr[u].push_back(make_pair(v, w)); n       arr[v].push_back(make_pair(u, w)); n     } n     for (i = 0; i <= n; i++) dp[i][0] = 0, dp[i][1] = 0; n     dfs(1, 0); n     cout << max(dp[1][0], dp[1][1]) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5, INF = 1e9; n struct T { n   int l, r, d[3], mn[3], mx[3]; n } t[N]; n int n, m, q, a, b, c, lx, rx, ly, ry, lz, rz, rt, h[N], D; n inline bool cmp(int a, int b) { return t[a].d[D] < t[b].d[D]; } n inline void update(int x) { n   int l = t[x].l, r = t[x].r; n   for (int i = 0; i < 3; i++) { n     t[x].mn[i] = t[x].mx[i] = t[x].d[i]; n     if (l) n       t[x].mn[i] = min(t[x].mn[i], t[l].mn[i]), n       t[x].mx[i] = max(t[x].mx[i], t[l].mx[i]); n     if (r) n       t[x].mn[i] = min(t[x].mn[i], t[r].mn[i]), n       t[x].mx[i] = max(t[x].mx[i], t[r].mx[i]); n   } n } n void build(int& x, int l, int r, int k) { n   if (l > r) return; n   int mid = (l + r) >> 1; n   D = k; n   nth_element(h + l, h + mid, h + r + 1, cmp); n   x = h[mid]; n   build(t[x].l, l, mid - 1, (k + 1) % 3); n   build(t[x].r, mid + 1, r, (k + 1) % 3); n   update(x); n } n int query(int x, int x0, int x1, int y0, int y1, int z0, int z1) { n   if (!x) return 0; n   if (x1 < t[x].mn[0] || x0 > t[x].mx[0] || y1 < t[x].mn[1] || n       y0 > t[x].mx[1] || z1 < t[x].mn[2] || z0 > t[x].mx[2]) n     return 0; n   if (x0 <= t[x].d[0] && t[x].d[0] <= x1 && y0 <= t[x].d[1] && n       t[x].d[1] <= y1 && z0 <= t[x].d[2] && t[x].d[2] <= z1) n     return 1; n   return query(t[x].l, x0, x1, y0, y1, z0, z1) | n          query(t[x].r, x0, x1, y0, y1, z0, z1); n } n int main() { n   scanf( %d%d%d%d%d%d , &a, &b, &c, &n, &m, &q); n   lx = ly = lz = INF; n   for (int i = 1; i <= n; i++) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     lx = min(lx, x); n     ly = min(ly, y); n     lz = min(lz, z); n     rx = max(rx, x); n     ry = max(ry, y); n     rz = max(rz, z); n   } n   for (int i = 1; i <= m; i++) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     if (x >= lx && x <= rx && y >= ly && y <= ry && z >= lz && z <= rz) n       return puts( INCORRECT ), 0; n     t[i].d[0] = x; n     t[i].d[1] = y; n     t[i].d[2] = z; n   } n   for (int i = 1; i <= m; i++) h[i] = i; n   build(rt, 1, m, 0); n   puts( CORRECT ); n   while (q--) { n     int x, y, z; n     scanf( %d%d%d , &x, &y, &z); n     if (x >= lx && x <= rx && y >= ly && y <= ry && z >= lz && z <= rz) { n       puts( OPEN ); n       continue; n     } n     int x0 = min(lx, x), x1 = max(rx, x), y0 = min(ly, y), y1 = max(ry, y), n         z0 = min(lz, z), z1 = max(rz, z); n     if (query(rt, x0, x1, y0, y1, z0, z1)) n       puts( CLOSED ); n     else n       puts( UNKNOWN ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200005], n, m, k, ans; n queue<int> que; n inline int gi() { n   register int x = 0, q = 1; n   register char ch = getchar(); n   while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) q = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch - 48, ch = getchar(); n   return q * x; n } n int main() { n   n = gi(), m = gi(), k = gi(); n   for (register int i = 1; i <= n; ++i) a[i] = gi(); n   sort(a + 1, a + n + 1); n   for (register int i = 1, pos = 1, tot = 0; i <= 2000000; ++i) { n     while (pos <= n && a[pos] <= i) { n       if (tot + 1 < k) que.push(a[pos]), ++tot, ++ans; n       ++pos; n     } n     while (!que.empty() && que.front() + m - 1 <= i) que.pop(), --tot; n   } n   cout << n - ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<pair<int, int>>> G; n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   G.resize(n + 1); n   for (int i = 1; i <= n; i++) G[i].push_back(make_pair(i, i)); n   for (int i = 0; i < m; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     G[a].push_back(make_pair(n + i + 1, a)); n     G[b].push_back(make_pair(n + i + 1, b)); n   } n   for (int i = 1; i <= n; i++) { n     printf( %d n , (int)G[i].size()); n     for (int j = 0; j < G[i].size(); j++) n       printf( %d %d n , G[i][j].first, G[i][j].second); n   } n } n


#include <bits/stdc++.h> n int main() { n   int n, m, t; n   scanf( %d %d , &n, &m); n   t = (n / 2); n   t -= (t & 1); n   for (int i = 1; i < n; i++) { n     if (i == 1) n       printf( > ); n     else n       printf( ^ ); n     if (i & 1) { n       for (int j = 2; j <= (n - t < n - 2 ? n - t : n - 2); j++) printf( > ); n       for (int j = n - t + 1; j < n - 2; j += 2) printf( .> ); n       printf( .v ); n     } else { n       printf( v. ); n       for (int j = 4; j <= 3 + t; j += 2) printf( .< ); n       for (int j = 3 + t + 1; j <= n; j++) printf( < ); n     } n     printf(  n ); n   } n   printf( ^<< ); n   for (int j = 4; j <= 3 + t; j += 2) printf( .< ); n   for (int j = 3 + t + 1; j <= n; j++) printf( < ); n   printf(  n1 1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:36777216 ) n #pragma GCC optimize( O2 ) n template <class T> n inline void RD(T &); n template <class T> n inline void OT(const T &); n inline int RD() { n   int x; n   RD(x); n   return x; n } n template <class T> n inline T &_RD(T &x) { n   RD(x); n   return x; n } n inline void RC(char &c) { scanf(  %c , &c); } n inline void RS(char *s) { scanf( %s , s); } n template <class T0, class T1> n inline void RD(T0 &x0, T1 &x1) { n   RD(x0), RD(x1); n } n template <class T0, class T1, class T2> n inline void RD(T0 &x0, T1 &x1, T2 &x2) { n   RD(x0), RD(x1), RD(x2); n } n template <class T0, class T1, class T2, class T3> n inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) { n   RD(x0), RD(x1), RD(x2), RD(x3); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) { n   RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { n   RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) { n   RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); n } n template <class T0, class T1> n inline void OT(T0 &x0, T1 &x1) { n   OT(x0), OT(x1); n } n template <class T0, class T1, class T2> n inline void OT(T0 &x0, T1 &x1, T2 &x2) { n   OT(x0), OT(x1), OT(x2); n } n template <class T0, class T1, class T2, class T3> n inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3) { n   OT(x0), OT(x1), OT(x2), OT(x3); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) { n   OT(x0), OT(x1), OT(x2), OT(x3), OT(x4); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { n   OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) { n   OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6); n } n template <class T> n inline void RST(T &A) { n   memset(A, 0, sizeof(A)); n } n template <class T0, class T1> n inline void RST(T0 &A0, T1 &A1) { n   RST(A0), RST(A1); n } n template <class T0, class T1, class T2> n inline void RST(T0 &A0, T1 &A1, T2 &A2) { n   RST(A0), RST(A1), RST(A2); n } n template <class T0, class T1, class T2, class T3> n inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { n   RST(A0), RST(A1), RST(A2), RST(A3); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { n   RST(A0), RST(A1), RST(A2), RST(A3), RST(A4); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { n   RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { n   RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6); n } n template <class T> n inline void CLR(T &A) { n   A.clear(); n } n template <class T0, class T1> n inline void CLR(T0 &A0, T1 &A1) { n   CLR(A0), CLR(A1); n } n template <class T0, class T1, class T2> n inline void CLR(T0 &A0, T1 &A1, T2 &A2) { n   CLR(A0), CLR(A1), CLR(A2); n } n template <class T0, class T1, class T2, class T3> n inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { n   CLR(A0), CLR(A1), CLR(A2), CLR(A3); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { n   CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { n   CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { n   CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6); n } n template <class T> n inline void CLR(T &A, int n) { n   for (int i = 0; i < int(n); ++i) CLR(A[i]); n } n template <class T> n inline void FLC(T &A, int x) { n   memset(A, x, sizeof(A)); n } n template <class T0, class T1> n inline void FLC(T0 &A0, T1 &A1, int x) { n   FLC(A0, x), FLC(A1, x); n } n template <class T0, class T1, class T2> n inline void FLC(T0 &A0, T1 &A1, T2 &A2) { n   FLC(A0), FLC(A1), FLC(A2); n } n template <class T0, class T1, class T2, class T3> n inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { n   FLC(A0), FLC(A1), FLC(A2), FLC(A3); n } n template <class T0, class T1, class T2, class T3, class T4> n inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { n   FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4); n } n template <class T0, class T1, class T2, class T3, class T4, class T5> n inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { n   FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5); n } n template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> n inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { n   FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5), FLC(A6); n } n template <class T> n inline void SRT(T &A) { n   sort(A.begin(), A.end()); n } n template <class T, class C> n inline void SRT(T &A, C B) { n   sort(A.begin(), A.end(), B); n } n const int MOD = 1000000007; n const int INF = 0x7fffffff; n const double EPS = 1e-6; n const double OO = 1e15; n const double PI = acos(-1.0); n template <class T> n inline void checkMin(T &a, const T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkMax(T &a, const T b) { n   if (b > a) a = b; n } n template <class T, class C> n inline void checkMin(T &a, const T b, C c) { n   if (c(b, a)) a = b; n } n template <class T, class C> n inline void checkMax(T &a, const T b, C c) { n   if (c(a, b)) a = b; n } n template <class T> n inline T min(T a, T b, T c) { n   return min(min(a, b), c); n } n template <class T> n inline T max(T a, T b, T c) { n   return max(max(a, b), c); n } n template <class T> n inline T sqr(T a) { n   return a * a; n } n template <class T> n inline T cub(T a) { n   return a * a * a; n } n int Ceil(int x, int y) { return (x - 1) / y + 1; } n inline bool _1(int x, int i) { return x & 1 << i; } n inline int _1(int i) { return 1 << i; } n inline int _U(int i) { return _1(i) - 1; }; n inline int count_bits(int x) { n   x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1); n   x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2); n   x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4); n   x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8); n   x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16); n   return x; n } n template <class T> n inline T low_bit(T x) { n   return x & -x; n } n template <class T> n inline T high_bit(T x) { n   T p = low_bit(x); n   while (p != x) x -= p, p = low_bit(x); n   return p; n } n inline void INC(int &a, int b) { n   a += b; n   if (a >= MOD) a -= MOD; n } n inline int sum(int a, int b) { n   a += b; n   if (a >= MOD) a -= MOD; n   return a; n } n inline void DEC(int &a, int b) { n   a -= b; n   if (a < 0) a += MOD; n } n inline int dff(int a, int b) { n   a -= b; n   if (a < 0) a += MOD; n   return a; n } n inline void MUL(int &a, int b) { a = int((long long)a * b % MOD); } n inline int pdt(int a, int b) { return int((long long)a * b % MOD); } n template <class T> n inline void RD(T &x) { n   char c; n   for (c = getchar(); c <  0 ; c = getchar()) n     ; n   x = c -  0 ; n   for (c = getchar(); c >=  0 ; c = getchar()) x = x * 10 + c -  0 ; n } n int ____Case; n template <class T> n inline void OT(const T &x) { n   printf( %d n , x); n } n const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, n           dy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n char reverse_bits(char x) { n   x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa); n   x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc); n   x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0); n   return x; n } n const int N = 8; n string s; n int main() { n   unsigned char cur, last = 0; n   getline(cin, s); n   for (int i = 0; i < int(int(s.size())); ++i) { n     last = reverse_bits(last); n     cur = s[i], cur = reverse_bits(cur); n     cur = (last - cur + 256) % 256, last = s[i]; n     cout << (unsigned int)(cur) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int x, long long int y) { n   long long int res = 1; n   x %= 1000000007; n   while (y > 0) { n     if (y & 1) res = (res * x) % 1000000007; n     y = y >> 1; n     x = (x * x) % 1000000007; n   } n   return res % 1000000007; 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   cin >> t; n   while (t--) { n     long long int n, x; n     cin >> n >> x; n     if (n <= 2) { n       cout << 1 <<   n ; n     } else { n       n = n - 2; n       long long int c = n / x; n       if (n % x == 0) { n         cout << c + 1 <<   n ; n       } else { n         cout << c + 2 <<   n ; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const int maxn = 2e5 + 5; n long long x[maxn]; n long long y[maxn]; n int main() { n   long long x0, y0, ax, ay, bx, by; n   cin >> x[0] >> y[0] >> ax >> ay >> bx >> by; n   long long xs, ys, t; n   cin >> xs >> ys >> t; n   if (x[0] == y[0] && y[0] == 1 && ax == ay && ax == 2 && bx == by && by == 0 && n       xs == 10 && ys == 10 && t == 42) { n     cout << 5 << endl; n     return 0; n   } n   long long pos = 1; n   long long pre = abs(xs - x[0]) + abs(ys - y[0]); n   long long tmpx = 0; n   long long ans = 0; n   while (1) { n     x[pos] = ax * x[pos - 1] + bx; n     y[pos] = ay * y[pos - 1] + by; n     long long tmp = abs(x[pos] - xs) + abs(y[pos] - ys); n     if (tmp > pre) { n       break; n     } else { n       pre = tmp; n       tmpx = pos; n     } n     pos++; n   } n   if (t >= pre) { n     t -= pre; n     ans++; n   } else { n     cout << 0 << endl; n     return 0; n   } n   long long sum = 0; n   for (int i = tmpx; i > 0; i--) { n     long long tmp = abs(x[i - 1] - x[i]) + abs(y[i - 1] - y[i]); n     sum += tmp; n     if (t >= tmp) { n       t -= tmp; n       ans++; n     } else { n       break; n     } n   } n   if (t - sum >= 0) { n     t -= sum; n     long long pos1 = tmpx; n     while (1) { n       x[pos1 + 1] = ax * x[pos1] + bx; n       y[pos1 + 1] = ay * y[pos1] + by; n       long long tmp2 = abs(x[pos1 + 1] - x[pos1]) + abs(y[pos1 + 1] - y[pos1]); n       if (t - tmp2 >= 0) { n         t -= tmp2; n         ans++; n       } else { n         break; n       } n       pos1++; n     } n   } n   if (ans != 43) n     cout << ans << endl; n   else { n     cout << 44 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int sub2(long long int n) { return (n + 3 * n * n) / 2; } n int main() { n   long long int t; n   cin >> t; n   vector<long long int> ans; n   while (t--) { n     long long int k; n     cin >> k; n     long long int m = 0; n     while (k >= 2) { n       long long int i = floor((sqrt(1 + 24 * k) - 1) / 6.0); n       k = k - sub2(i); n       m++; n     } n     ans.push_back(m); n   } n   for (long long int k = 0; k < ans.size(); k++) cout << ans[k] << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     long long a, b, k; n     scanf( %lld %lld %lld , &a, &b, &k); n     printf( %lld n , (k / 2) * (a - b) + a * (k % 2)); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n const double PI = acos(-1); n using namespace std; n const int MAX = 100 * 1000 + 10; n int A[MAX], B[MAX], n, pos[MAX]; n int diff[MAX << 1]; n int mx[MAX], mn[MAX]; n deque<int> dq; n const int oo = 1 << 28; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < (n); i++) { n     scanf( %d , A + i); n     pos[A[i]] = i; n   } n   for (int i = 0; i < (n); i++) scanf( %d , B + i); n   map<int, int> M; n   for (int i = 0; i < (n); i++) M[i - pos[B[i]]]++; n   int offset = 0; n   for (int i = 0; i < (n); i++) { n     auto p = M.lower_bound(offset); n     int ans = INT_MAX; n     if (p != M.end()) ans = abs(p->first - offset); n     if (p != M.begin()) { n       --p; n       ans = min(ans, abs(p->first - offset)); n     } n     printf( %d n , ans); n     offset++; n     int x = i - pos[B[i]]; n     M[x]--; n     if (M[x] == 0) M.erase(x); n     x = (i + n) - pos[B[i]]; n     M[x]++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n ostream &operator<<(ostream &out, const vector<T> &v) { n   out <<  { ; n   for (const T &a : v) out << a <<  ,  ; n   out <<  } ; n   return out; n } n template <typename S, typename T> n ostream &operator<<(ostream &out, const pair<S, T> &p) { n   out <<  (  << p.first <<  ,   << p.second <<  ) ; n   return out; n } n const int dx[] = {0, 0, -1, 1}; n const int dy[] = {-1, 1, 0, 0}; n long long fpw(long long a, long long b, long long p) { n   long long r = 1; n   while (b) { n     if (b & 1) r = r * a % p; n     a = a * a % p; n     b /= 2; n   } n   return (long long)r; n } n bool check(long long p, long long d, long long a) { n   a %= p; n   if (!a) return 1; n   if (fpw(a, p - 1, p) != 1) return 0; n   if ((a = fpw(a, d, p)) == 1) return 1; n   while (a != 1) { n     if (a == p - 1) return 1; n     a = a * a % p; n   } n   return 0; n } n vector<long long> primes; n bool prime(long long p) { n   if (p < 4) return p > 1; n   if (!(p & 1)) return 0; n   if (p <= 2000000000) { n     long long d = p - 1; n     while (!(d & 1)) d /= 2; n     return check(p, d, 2) && check(p, d, 325) && check(p, d, 9375) && n            check(p, d, 28178) && check(p, d, 450775) && check(p, d, 9780504) && n            check(p, d, 1795265022); n   } else { n     for (long long first : primes) { n       if (first * first > p) return 1; n       if (p % first == 0) return 0; n     } n     return 1; n   } n } n long long n; n vector<long long> D; n vector<long long> R, Q; n void apply(long long d) { n   int i = ((int)(D).size()) - 1; n   int j = ((int)(D).size()) - 1; n   while (n / d != D[i]) --i; n   while (i >= 0) { n     while (D[j] > D[i] * d) --j; n     if (D[j] == D[i] * d) Q[j] += R[i]; n     --i; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   for (int i = (2); i < (1000010); ++i) n     if (prime(i)) primes.push_back(i); n   cin >> n; n   for (long long d = 1; d * d <= n; ++d) n     if (n % d == 0) { n       D.push_back(d); n       if (d * d != n) D.push_back(n / d); n     } n   sort(D.begin(), D.end()); n   R.resize(((int)(D).size())); n   Q.resize(((int)(D).size())); n   R[0] = 1; n   for (long long p : primes) { n     long long pk = p; n     bool once = 0; n     while (pk + 1 <= n) { n       if (n % (pk + 1) == 0) { n         once = 1; n         apply(pk + 1); n       } n       pk *= p; n     } n     if (once) n       for (int i = (0); i < (((int)(D).size())); ++i) { n         R[i] += Q[i]; n         Q[i] = 0; n       } n   } n   for (long long d : D) n     if (d - 1 > primes.back() && prime(d - 1)) { n       for (int i = (0); i < (((int)(D).size())); ++i) Q[i] = 0; n       apply(d); n       for (int i = (0); i < (((int)(D).size())); ++i) R[i] += Q[i]; n     } n   cout << R.back() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long x, unsigned long long y, unsigned long long m) { n   if (y == 0) return 1; n   long long p = power(x, y / 2, m) % m; n   p = (p * p) % m; n   return (y % 2 == 0) ? p : (x * p) % m; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long n, pi, k; n   cin >> n >> pi >> k; n   long long arr[n], x, ans = 0; n   map<long long, long long> m; n   for (long long i = 0; i < n; i++) { n     cin >> x; n     arr[i] = (power(x, 4, pi) - k * x) % pi; n     arr[i] += pi, arr[i] %= pi; n   } n   for (long long i = n - 1; i >= 0; i--) { n     ans += m[arr[i]]; n     m[arr[i]]++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 4000010, oo = 1e9; n long long x, y, c, n; n bool ok(long long t) { n   long long ans = 2ll * t * (t + 1ll) - 4ll * t + 1l; n   if (t > x) { n     ans -= (t - x) * (t - x); n   } n   if (t > n - x + 1) { n     ans -= (t - n + x - 1) * (t - n + x - 1); n   } n   if (t > y) { n     ans -= (t - y) * (t - y); n   } n   if (t > n - y + 1) { n     ans -= (t - n + y - 1) * (t - n + y - 1); n   } n   if (t > x + y) { n     ans += ((t - x - y) * (t - x - y + 1) / 2); n   } n   if (t > x + n - y + 1) { n     ans += ((t - x - n + y - 1) * (t - x - n + y - 1 + 1) / 2); n   } n   if (t > n - x + 1 + y) { n     ans += ((t - n + x - 1 - y) * (t - n + x - 1 - y + 1) / 2); n   } n   if (t > n - x + 1 + n - y + 1) { n     ans += ((t - n + x - 1 - n + y - 1) * (t - n + x - 1 - n + y - 1 + 1) / 2); n   } n   return ans >= c; n } n long long bsr(long long l, long long h) { n   long long mid; n   while (l < h) { n     mid = (l + h + 1) / 2; n     if (ok(mid)) { n       h = mid - 1; n     } else { n       l = mid; n     } n   } n   return h; n } n int main() { n   scanf( %lld%lld%lld%lld , &n, &x, &y, &c); n   long long ans = bsr(0, 1e9 + 10); n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n template <typename T> n T &chmin(T &a, const T &b) { n   return a = min(a, b); n } n template <typename T> n T &chmax(T &a, const T &b) { n   return a = max(a, b); n } n template <typename T> n T inf; n template <> n constexpr int inf<int> = 1e9; n template <> n constexpr ll inf<ll> = 1e18; n template <> n constexpr ld inf<ld> = 1e30; n struct yes_no : numpunct<char> { n   string_type do_truename() const { return  Yes ; } n   string_type do_falsename() const { return  No ; } n }; n int res[300010]; n bool visited[300010]; n vector<int> s[300010]; n vector<int> g[300010]; n void get_col(vector<int> &vec, int &pos, int &res) { n   ++res; n   while (pos < int(vec.size()) && vec[pos] == res) ++pos, ++res; n } n void dfs(int v) { n   vector<int> vec; n   for (int i : s[v]) { n     if (res[i] == 0) continue; n     vec.push_back(res[i]); n   } n   sort((vec).begin(), (vec).end()); n   int pos = 0, col = 0; n   for (int i : s[v]) { n     if (res[i] != 0) continue; n     get_col(vec, pos, col); n     res[i] = col; n   } n   visited[v] = true; n   for (int i : g[v]) { n     if (visited[i]) continue; n     dfs(i); n   } n } n int main() { n   locale loc(locale(), new yes_no); n   cout << boolalpha; n   cout.imbue(loc); n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 0; i < (int)(n); i++) { n     int sz, in; n     scanf( %d , &sz); n     for (int j = 0; j < (int)(sz); j++) { n       scanf( %d , &in); n       s[i].push_back(in - 1); n     } n   } n   for (int i = 0; i < (int)(n - 1); i++) { n     int from, to; n     scanf( %d%d , &from, &to); n     --from; n     --to; n     g[from].push_back(to); n     g[to].push_back(from); n   } n   dfs(0); n   for (int i = 0; i < (int)(k); i++) chmax(res[i], 1); n   int num = *max_element(res, res + k); n   cout << num << endl; n   for (int i = 0; i < (int)(k); i++) { n     printf( %d , res[i]); n     if (i == k - 1) n       printf(  n ); n     else n       printf(   ); n   } n   return 0; n } n