#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void chkmax(T& x, U y) { n   if (x < y) x = y; n } n template <typename T, typename U> n inline void chkmin(T& x, U y) { n   if (y < x) x = y; n } n const int MAXN = 110000; n int n, m, N; n int xn; n int ans[MAXN], c[MAXN], a[MAXN], b[MAXN], id[MAXN], x[MAXN]; n int qid[MAXN], L[MAXN], R[MAXN], B[MAXN], ret[MAXN]; n int dp[1 << 18]; n void add(int u) { n   for (; u <= xn; u += u & -u) c[u]++; n } n int get(int u) { n   int ret = 0; n   for (; u; u -= u & -u) ret += c[u]; n   return ret; n } n bool cmp(int u, int v) { return b[u] > b[v]; } n bool cmp1(int u, int v) { return B[u] > B[v]; } n int mx[MAXN << 2]; n void pushup(int u) { mx[u] = max(mx[(u << 1)], mx[(u << 1 | 1)]); } n void update(int u, int l, int r, int pos, int x) { n   if (l == r) { n     mx[u] = max(mx[u], x); n     return; n   } n   int mid = l + r >> 1; n   if (pos <= mid) n     update((u << 1), l, mid, pos, x); n   else n     update((u << 1 | 1), mid + 1, r, pos, x); n   pushup(u); n } n int query(int u, int l, int r, int ql, int qr) { n   if (ql > qr) return 0; n   if (ql <= l && r <= qr) { n     return mx[u]; n   } n   int mid = l + r >> 1; n   if (qr <= mid) n     return query((u << 1), l, mid, ql, qr); n   else if (ql > mid) n     return query((u << 1 | 1), mid + 1, r, ql, qr); n   else n     return max(query((u << 1), l, mid, ql, qr), n                query((u << 1 | 1), mid + 1, r, ql, qr)); n } n int tag[MAXN]; n void work() { n   xn = 0; n   N = 0; n   n = 0; n   m = 0; n   memset(ans, 0, sizeof(ans)); n   memset(c, 0, sizeof(c)); n   memset(a, 0, sizeof(a)); n   memset(b, 0, sizeof(b)); n   memset(id, 0, sizeof(id)); n   memset(x, 0, sizeof(x)); n   memset(qid, 0, sizeof(qid)); n   memset(L, 0, sizeof(L)); n   memset(R, 0, sizeof(R)); n   memset(B, 0, sizeof(B)); n   memset(ret, 0, sizeof(ret)); n   memset(dp, 0, sizeof(dp)); n   memset(mx, 0, sizeof(mx)); n   scanf( %d%d , &n, &m); n   for (register int i = 1; i <= n; i++) scanf( %d , &b[i]); n   for (register int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (register int i = 1; i <= n; i++) id[i] = i; n   for (register int i = 1; i <= n; i++) x[xn++] = a[i]; n   sort(x, x + xn); n   xn = unique(x, x + xn) - x; n   for (register int i = 1; i <= n; i++) a[i] = lower_bound(x, x + xn, a[i]) - x; n   sort(id + 1, id + n + 1, cmp); n   for (register int i = n, j; i > 0; i = j) { n     for (j = i; j > 0 && b[id[i]] == b[id[j]]; j--) add(a[id[j]] + 1); n     for (register int k = i; k > j; k--) { n       int u = id[k]; n       int r = upper_bound(x, x + xn, x[a[u]] + m) - x; n       int l = lower_bound(x, x + xn, x[a[u]] - m) - x; n       ans[u] = get(r) - get(l); n     } n   } n   int q; n   scanf( %d , &q); n   for (register int i = 1; i <= q; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     L[i] = lower_bound(x, x + xn, max(x[a[u]], x[a[v]]) - m) - x + 1; n     R[i] = upper_bound(x, x + xn, min(x[a[u]], x[a[v]]) + m) - x; n     B[i] = max(b[u], b[v]); n     qid[i] = i; n   } n   sort(qid + 1, qid + q + 1, cmp1); n   for (register int i = 1, j = 1; i <= q; i++) { n     int u = qid[i]; n     for (; j <= n && b[id[j]] >= B[u]; j++) n       update(1, 1, xn, a[id[j]] + 1, ans[id[j]]); n     ret[u] = query(1, 1, xn, L[u], R[u]); n   } n   for (register int i = 1; i <= q; i++) { n     if (!ret[i]) ret[i] = -1; n     printf( %d n , ret[i]); n   } n   return; n } n int main() { n   int T = 1; n   while (T--) work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500010; n const int LOG = 20; n const long long INF = (1ll << 60); n int n; n int a[N]; n vector<int> adj[N]; n int par[LOG][N]; n void dfs(int u, int p) { n   par[0][u] = p; n   for (int i = 1; i < LOG; i++) { n     par[i][u] = par[i - 1][par[i - 1][u]]; n   } n   for (int v : adj[u]) { n     if (v != p) dfs(v, u); n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   int root = -1; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (root == -1 || a[root] > a[i]) root = i; n   } n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(root, 0); n   long long res = 0; n   for (int i = 1; i <= n; i++) { n     if (i == root) continue; n     long long foo = INF; n     for (int j = 0; j < LOG; j++) { n       int u = par[j][i]; n       if (u == 0) u = root; n       foo = min(foo, 1ll * a[u] * (j + 1)); n       if (u == root) break; n     } n     res += foo; n     res += a[i]; n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using uint = unsigned int; n using ull = unsigned long long; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n double startTime; n double gct() { return ((double)clock() - startTime) / CLOCKS_PER_SEC; } n void suraj() { n   int n, m; n   cin >> n >> m; n   vector<int> ans; n   if (m % 2 == 1) { n     int ref = m / 2; n     int ptr = 1; n     ans.push_back(ref); n     while (ptr <= ref) { n       ans.push_back(ref - ptr); n       ans.push_back(ref + ptr); n       ptr++; n     } n   } else { n     int ref = m / 2 - 1; n     int ptr = 1; n     ans.push_back(ref); n     while (ptr <= ref) { n       ans.push_back(ref + ptr); n       ans.push_back(ref - ptr); n       ptr++; n     } n     ans.push_back(ref + ptr); n   } n   for (int i = 0; i < n; i++) { n     cout << ans[i % m] + 1 <<   n ; n   } n } n void suraj2() {} n int main() { n   startTime = (double)clock(); n   suraj(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400, D = 8; n int vis[N][N]; n int T[30]; n bool used[N][N][D][6][31]; n struct obj { n   int x, y, d, t, i; n   void move() { n     if (d == 0 || d == 1 || d == 7) x++; n     if (d == 5 || d == 4 || d == 3) x--; n     if (d == 1 || d == 2 || d == 3) y++; n     if (d == 5 || d == 6 || d == 7) y--; n     t--; n   } n   obj splitL() { return {x, y, (d + 1) % D, T[i + 1], i + 1}; } n   obj splitR() { return {x, y, (d + D - 1) % D, T[i + 1], i + 1}; } n   bool check(bool set = false) { n     if (set) used[x][y][d][t][i] = true; n     return used[x][y][d][t][i]; n   } n }; n queue<obj> q; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> T[i]; n   q.push({N / 2, N / 2, 0, T[0] - 1, 0}); n   while (not q.empty()) { n     auto curr = q.front(); n     q.pop(); n     if (curr.check()) continue; n     curr.check(true); n     vis[curr.x][curr.y] = 1; n     if (curr.t == 0) { n       if (curr.i == n) continue; n       q.push(curr.splitL()); n       q.push(curr.splitR()); n       continue; n     } n     curr.move(); n     q.push(curr); n   } n   int ans = 0; n   for (int x = 0; x < N; x++) { n     for (int y = 0; y < N; y++) { n       ans += vis[x][y]; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n using vi = vector<int>; n const ll INF = 1e9 + 7; n void solution() { n   int n; n   cin >> n; n   if (n == 1) { n     cout <<  -1 n ; n     return; n   } else { n     cout <<  2 ; n     n -= 1; n     while (n--) { n       cout <<  3 ; n     } n   } n   cout << endl; n } n int main() { n   int test; n   cin >> test; n   while (test--) { n     solution(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005, logN = 18; n int n, q, l, r, tim; n struct vec { n   int x, y, num; n   bool operator<(const vec &A) const { return x < A.x || x == A.x && y > A.y; } n } a[N]; n inline long long calc(vec *a) { return a->x + 1LL * tim * a->y; } n inline long long cross(vec &a, vec &b, vec &c) { n   return (long long)(b.x - a.x) * (c.y - a.y) - n          (long long)(b.y - a.y) * (c.x - a.x); n } n vec mem[N * logN * 2], *newmem = mem; n struct data { n   vec *st, *en; n } t[N * 4]; n void build(int i, int beg, int end) { n   t[i].st = newmem; n   if (end - beg == 1) { n     scanf( %d%d , &t[i].st->x, &t[i].st->y); n     t[i].st->num = end; n     newmem++; n     t[i].en = newmem; n     return; n   } n   int mid = beg + end >> 1; n   build(i << 1, beg, mid); n   build(i << 1 | 1, mid, end); n   t[i].st = t[i].en = newmem; n   for (vec *l = t[i << 1].st, *r = t[i << 1 | 1].st; n        l < t[i << 1].en || r < t[i << 1 | 1].en;) n     if (l < t[i << 1].en && (r == t[i << 1 | 1].en || *l < *r)) { n       while (t[i].en - t[i].st > 1 && cross(t[i].en[-2], t[i].en[-1], *l) >= 0) n         t[i].en--; n       *t[i].en++ = *l++; n     } else { n       while (t[i].en - t[i].st > 1 && cross(t[i].en[-2], t[i].en[-1], *r) >= 0) n         t[i].en--; n       *t[i].en++ = *r++; n     } n   newmem = t[i].en; n } n vec *query(int i, int beg, int end) { n   if (r <= beg || l >= end) return a; n   if (l <= beg && r >= end) { n     vec *a = t[i].st, *b = t[i].en - 1; n     long long fa = calc(a), fb = calc(b); n     while (b - a + 1 >= 3) { n       vec *mid1 = a + (b - a + 1) / 3, *mid2 = b - (b - a + 1) / 3; n       long long f1 = calc(mid1), f2 = calc(mid2); n       if (fa <= f1 && f1 <= f2) n         a = mid1, fa = f1; n       else if (fb <= f2 && f2 <= f1) n         b = mid2, fb = f2; n       else n         puts( Error ); n     } n     vec *res = b; n     for (; a < b; a++) n       if (calc(res) < calc(a)) res = a; n     return res; n   } n   int mid = beg + end >> 1; n   vec *a = query(i << 1, beg, mid), *b = query(i << 1 | 1, mid, end); n   return calc(a) > calc(b) ? a : b; n } n int main() { n   scanf( %d%d , &n, &q); n   build(1, 0, n); n   while (q--) { n     scanf( %d%d%d , &l, &r, &tim); n     l--; n     printf( %d n , query(1, 0, n)->num); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int ans = 1000000005, rans = 0, bans = 0; n int numr[20], numb[20], color[20]; n int dp[70005][160]; n bool life[70005][160]; n int nowr, nowb, tempr, tempb; n char s[6]; n int nex[20]; n void ok(int x) { n   int now; n   nowr = nowb = now = 0; n   while (x) { n     if (x % 2) { n       if (color[now]) n         nowb++; n       else n         nowr++; n     } n     x /= 2; n     now++; n   } n } n int main() { n   int n; n   cin >> n; n   int maxx = 1 << n, maxxx = n * (1 + n) / 2; n   nex[0] = 1; n   life[0][0] = 1; n   for (int i = 1; i < n; i++) nex[i] = nex[i - 1] * 2; n   for (int i = 0; i < n; i++) { n     scanf( %s , s); n     if (s[0] ==  R ) n       color[i] = 0; n     else n       color[i] = 1; n     scanf( %d%d , numr + i, numb + i); n     rans += numr[i]; n     bans += numb[i]; n   } n   for (int i = 0; i < maxx; i++) { n     ok(i); n     for (int j = 0; j < n; j++) { n       if ((nex[j] & i) == 0) { n         tempr = min(nowr, numr[j]); n         tempb = min(nowb, numb[j]); n         for (int k = 0; k <= maxxx; k++) n           if (life[i][k]) { n             dp[i + nex[j]][k + tempr] = n                 max(dp[i + nex[j]][k + tempr], dp[i][k] + tempb); n             life[i + nex[j]][k + tempr] = 1; n           } n       } n     } n   } n   for (int i = 0; i < maxxx; i++) n     if (life[maxx - 1][i]) { n       ans = min(ans, max(rans - i, bans - dp[maxx - 1][i])); n     } n   cout << ans + n << endl; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n  n typedef long double ld; n typedef long long int ll; n typedef pair<int, int> pi; n typedef pair<long long, long long> pll; n  n #define endl   n  n #define ff first n #define ss second n #define pb push_back n #define int long long n #define sz(v) (int)v.size() n #define inf 2147483647 n #define llinf 9223372036854775807 n #define all(v) v.begin(),v.end() n #define bp(n) __builtin_popcountll(n) n #define f(i,l,r) for(long long i=l;i<=r;i++) n #define rf(i,r,l) for(long long i=r;i>=l;i--) n #define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL) n  n template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os <<  { ; string sep; for (const auto &x : v) os << sep << x, sep =  ,  ; return os <<  } ; } n template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os <<  { ; string sep; for (const auto &x : arr) os << sep << x, sep =  ,  ; return os <<  } ; } n template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os <<  (  << p.first <<  ,   << p.second <<  ) ; } n  n void dbg_out() { cout << endl; } n template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout <<     << H; dbg_out(T...); } n  n  n const int N = 3e5 + 5, mod = 1e9 + 7, bit = 61; n  n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n  n int getRand(int l, int r) n { n  uniform_int_distribution<int> uid(l, r); n  return uid(rng); n } n  n  n  n signed main() n { n  fast; n  n  n #ifndef ONLINE_JUDGE n  freopen( input.txt ,  r , stdin); n  freopen( output.txt ,  w , stdout); n #endif n  n  int t = 1; n  cin >> t; n  f(tc, 1, t) n  { n   int n, m, x; n   cin >> n >> m >> x; n   int r = (x % n); n   if (r == 0) n   { n    r = n; n   } n   int c = ((x - r) / (n)) + 1; n   cout << (r - 1) * m + c << endl; n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const int N = 55; n int n, cnt[N]; n vector<pair<int, int> > adj[N]; n void dfs(int u, int p, int depth, vector<pair<int, int> > &leaves) { n   bool leaf = 1; n   for (auto e : adj[u]) { n     int v, c; n     tie(v, c) = e; n     if (v == p) continue; n     dfs(v, u, depth + c, leaves); n     leaf = 0; n   } n   if (leaf) leaves.emplace_back(u, depth); n } n int dp[N][N], best[N][N]; n int go(int root, int pre, int total, int put) { n   vector<pair<int, int> > leaves; n   dfs(root, pre, 0, leaves); n   int sz = ((int)(leaves).size()); n   for (int from = (int)(sz + 1) - 1; from >= ((int)0); from--) { n     for (int left = (int)(0); left < ((int)put + 1); left++) { n       if (from == sz) { n         best[from][left] = (left ? -INF : INF); n       } else { n         best[from][left] = best[from + 1][left]; n         int l = leaves[from].first, cost = leaves[from].second; n         for (int pick = 1; pick <= left; pick++) { n           best[from][left] = n               max(best[from][left], n                   min(cost + dp[l][total - pick], best[from + 1][left - pick])); n         } n       } n     } n   } n   return best[0][put]; n } n void dfsSize(int u, int p) { n   for (auto e : adj[u]) { n     int v, c; n     tie(v, c) = e; n     if (v == p) continue; n     dfsSize(v, u); n     cnt[u] += cnt[v]; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int _ = 0; _ < (int)(n - 1); _++) { n     int u, v, c; n     cin >> u >> v >> c; n     u--; n     v--; n     adj[u].emplace_back(v, c); n     adj[v].emplace_back(u, c); n   } n   int s; n   cin >> s; n   s--; n   int m; n   cin >> m; n   for (int _ = 0; _ < (int)(m); _++) { n     int u; n     cin >> u; n     cnt[u - 1]++; n   } n   for (int total = 1; total <= m; total++) { n     for (int u = 0; u < (int)(n); u++) { n       dp[u][total] = go(u, u, total, total); n     } n   } n   dfsSize(s, -1); n   int ans = INF; n   for (auto e : adj[s]) { n     int v, c; n     tie(v, c) = e; n     ans = min(ans, c + go(v, s, m, cnt[v])); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using i32 = int32_t; n using i64 = int64_t; n using pii = std::pair<i32, i32>; n using pll = std::pair<i64, i64>; n int dp[5000]; n int iv[5000]; n bool used[5000]; n int div_up(int a, int b) { return (a + b - 1) / b; } n void log_pour(int n, int s, int d) { n   if (n > 0) { n     printf( %d %d %d n , n, s + 1, d + 1); n   } n } n int main() { n   int n, k, v; n   scanf( %d%d%d , &n, &k, &v); n   for (int i = 0; i < k; i++) { n     dp[i] = -1; n   } n   int vmax = 0; n   deque<int> q; n   for (int i = 0; i < n; i++) { n     scanf( %d , &iv[i]); n     if (iv[i] == 0) continue; n     auto v_mod = iv[i] % k; n     if (dp[v_mod] < 0 || i < dp[v_mod]) { n       dp[v_mod] = i; n       q.push_back(v_mod); n     } n     vmax += iv[i]; n   } n   if (vmax < v) { n     printf( NO n ); n     return 0; n   } n   if (v == 0) { n     printf( YES n ); n     log_pour(div_up(iv[0], k), 0, 1); n     return 0; n   } n   if (v % k == 0) { n     printf( YES n ); n     for (int i = 1; i < n; i++) { n       log_pour(div_up(iv[i], k), i, 0); n     } n     log_pour(v / k, 0, 1); n     return 0; n   } n   while (!q.empty()) { n     auto v_mod = q.front(); n     q.pop_front(); n     assert(dp[v_mod] >= 0); n     for (int i = dp[v_mod] + 1; i < n; i++) { n       if (iv[i] == 0) continue; n       auto v2 = (v_mod + iv[i]) % k; n       if (dp[v2] < 0 || i < dp[v2]) { n         dp[v2] = i; n         q.push_back(v2); n       } n     } n   } n   if (dp[v % k] < 0) { n     printf( NO n ); n     return 0; n   } n   printf( YES n ); n   int v0 = 0; n   int r = v % k; n   int j0 = dp[r]; n   do { n     assert(dp[r] >= 0); n     used[dp[r]] = true; n     v0 += iv[dp[r]]; n     if (dp[r] != j0) { n       log_pour(div_up(iv[dp[r]], k), dp[r], j0); n     } n     r = (k + r - iv[dp[r]] % k) % k; n   } while (v0 % k != v % k); n   for (int j = dp[j0]; v0 % k != v % k; j = dp[j]) { n     used[j] = true; n     v0 += iv[j]; n     log_pour(div_up(iv[j], k), dp[j], j0); n   } n   assert(v0 % k == v % k); n   int j1 = -1; n   int v1 = 0; n   for (int i = 0; i < n; i++) { n     if (used[i]) continue; n     v1 += iv[i]; n     if (j1 < 0) { n       j1 = i; n     } else { n       log_pour(div_up(iv[i], k), i, j1); n     } n   } n   if (v0 > v) { n     log_pour((v0 - v) / k, j0, (j0 + 1) % n); n   } else if (v0 < v) { n     assert(j1 >= 0); n     log_pour((v - v0) / k, j1, j0); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int id[110001]; n int ans[1000001]; n int main() { n   int n, m, i, number, t; n   while (scanf( %d %d , &n, &m) != EOF) { n     number = 0; n     memset(id, 0, sizeof(id)); n     memset(ans, 0, sizeof(ans)); n     for (i = 0; i < n; i++) { n       scanf( %d , &t); n       if (!i) n         id[number++] = t; n       else if (id[number - 1] != t) n         id[number++] = t; n     } n     for (i = 0; i < number; i++) { n       if (i == 0 || i == number - 1) n         ans[id[i]]++; n       else if (id[i - 1] == id[i + 1]) n         ans[id[i]] += 2; n       else n         ans[id[i]] += 1; n     } n     int sum = 0, h = 0; n     for (i = 1; i <= m; i++) { n       if (ans[i] > sum) { n         sum = ans[i]; n         h = i; n       } n     } n     printf( %d n , h); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Task { n  public: n   char invert(char c) { n     if (c ==  W ) return  B ; n     return  W ; n   } n   void shift_left(string& s, int k) { n     int n = s.length(); n     string t = s; n     for (int i = 0; i < n; i++) t[i] = s[(i + k) % n]; n     s = t; n   } n   void shift_left(vector<bool>& v, int k) { n     int n = v.size(); n     auto w = v; n     for (int i = 0; i < n; i++) w[i] = v[(i + k) % n]; n     v = w; n   } n   void solve() { n     int n, k; n     cin >> n >> k; n     string s; n     cin >> s; n     vector<bool> unstable(n); n     for (int i = 0; i < n; i++) { n       if (s[(i + 1) % n] != s[i] && s[(i - 1 + n) % n] != s[i]) { n         unstable[i] = true; n       } n     } n     bool all_stable = true; n     for (int i = 0; i < n; i++) { n       if (unstable[i]) { n         all_stable = false; n         break; n       } n     } n     if (all_stable) { n       cout << s <<   n ; n       return; n     } n     int stable = -1; n     for (int i = 0; i < n; i++) { n       if (!unstable[i]) { n         stable = i; n         break; n       } n     } n     if (stable == -1) { n       k %= 2; n       for (int i = 0; i < n; i++) { n         if (k % 2 == 1) { n           cout << invert(s[i]); n         } else { n           cout << s[i]; n         } n       } n       cout <<   n ; n       return; n     } n     if (stable == 0) { n       stable = n - 1; n       while (stable > 0 && !unstable[stable]) stable--; n       stable = (stable + 1) % n; n       shift_left(s, stable); n       shift_left(unstable, stable); n     } else { n       shift_left(s, stable); n       shift_left(unstable, stable); n     } n     for (int i = 2; i < n;) { n       if (unstable[i]) { n         int j; n         for (j = i; j < n && unstable[j]; j++) n           ; n         for (int t = i; t < min(i + k, i + (j - i + 1) / 2); t++) n           s[t] = s[(i - 1 + n) % n]; n         for (int t = j - 1; t >= max(j - k, j - (j - i + 1) / 2); t--) n           s[t] = s[j % n]; n         if (k % 2 == 1) { n           for (int t = min(i + k, i + (j - i + 1) / 2); n                t < max(j - k, j - (j - i + 1) / 2); t++) n             s[t] = invert(s[t]); n         } n         i = j; n       } else { n         i++; n       } n     } n     shift_left(s, n - stable); n     cout << s <<   n ; n   } n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   int T = 1; n   for (int t = 1; t <= T; t++) { n     (new Task())->solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 101; n long long n; n pair<long long, long long> a[N]; n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (long long i = 0; i < n; ++i) cin >> a[i].first >> a[i].second; n   long long p; n   cin >> p; n   for (long long i = n - 1; i >= 0; --i) { n     if (a[i].first <= p) { n       cout << n - i <<   n ; n       exit(0); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   long long n, k; n   cin >> n >> k; n   long long len = (1LL << n) - 1; n   while (true) { n     long long mid = len / 2 + 1; n     if (k == mid) break; n     if (k > mid) k -= mid; n     n--; n     len = (1LL << n) - 1; n   } n   cout << n << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long Mod = 1000000007LL; n const int N = 3e2 + 10; n const long long Inf = 2242545357980376863LL; n const long long Log = 30; n long long t1, t2, t3; n long long Get(long long x, long long y) { n   if (x < y) return t2; n   if (x == y) return t1; n   return t3; n } n long long a[N][N]; n long long ps1[N][N], ps2[N][N], ps3[N][N], ps4[N][N]; n set<pair<long long, long long> > st; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m, t; n   cin >> n >> m >> t; n   cin >> t1 >> t2 >> t3; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> a[i][j]; n   for (int i = 1; i <= n; i++) n     for (int j = 2; j <= m; j++) n       ps1[i][j] = ps1[i][j - 1] + Get(a[i][j - 1], a[i][j]); n   for (int i = 1; i <= n; i++) n     for (int j = m - 1; j >= 1; j--) n       ps2[i][j] = ps2[i][j + 1] + Get(a[i][j + 1], a[i][j]); n   for (int i = n - 1; i >= 1; i--) n     for (int j = 1; j <= m; j++) n       ps3[i][j] = ps3[i + 1][j] + Get(a[i + 1][j], a[i][j]); n   for (int i = 2; i <= n; i++) n     for (int j = 1; j <= m; j++) n       ps4[i][j] = ps4[i - 1][j] + Get(a[i - 1][j], a[i][j]); n   long long mn = Inf; n   long long x1 = 1, y1 = 1, x2 = 3, y2 = 3; n   for (int i1 = 1; i1 <= n; i1++) n     for (int i2 = i1 + 2; i2 <= n; i2++) { n       st.clear(); n       for (int j = 2; j <= m; j++) { n         long long V = ps1[i1][j] - ps4[i1][j] + ps4[i2][j] - ps2[i2][j]; n         long long R = t - V; n         auto it = st.lower_bound({R, -1}); n         pair<long long, long long> Q; n         if (st.size()) { n           if (it != st.end()) { n             Q = *it; n             if (abs(t - (V + Q.first)) < mn) { n               mn = abs(t - (V + Q.first)); n               x2 = i2; n               x1 = i1; n               y2 = j; n               y1 = it->second; n             } n           } n           if (it != st.begin()) { n             it--; n             Q = *it; n             if (abs(t - (V + Q.first)) < mn) { n               mn = abs(t - (V + Q.first)); n               x2 = i2; n               x1 = i1; n               y2 = j; n               y1 = it->second; n             } n           } n         } n         st.insert( n             {-ps1[i1][j - 1] + ps3[i1][j - 1] - ps3[i2][j - 1] + ps2[i2][j - 1], n              j - 1}); n       } n     } n   cout << x1 <<     << y1 <<     << x2 <<     << y2 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using std::string; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int f = 0; n   for (int i = 0; i < s.size() - 1; i++) { n     if (s[i + 1] < s[i]) { n       for (int j = i + 1; j < s.size(); j++) s[j - 1] = s[j]; n       f = 1; n       break; n     } n   } n   s.pop_back(); n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, q, x; n map<int, int> M; n map<int, int>::iterator it; n int main() { n   scanf( %d %d , &n, &k); n   for (int i = (1); i <= (n); i++) { n     int a; n     scanf( %d , &a); n     for (int j = (0); j <= (k); j++) { n       if (!M.count(a * j)) n         M[a * j] = j; n       else n         M[a * j] = min(M[a * j], j); n     } n   } n   scanf( %d , &q); n   while (q--) { n     scanf( %d , &x); n     int res = 99; n     for (it = M.begin(); it != M.end(); it++) { n       int v = (*it).first, c = (*it).second; n       if (M.count(x - v)) { n         int cc = M[x - v]; n         res = min(res, c + cc); n       } n     } n     if (res > k) res = -1; n     printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, ans = 0; n vector<int> a[100005]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int k, x; n     cin >> k; n     while (k--) { n       cin >> x; n       a[i].push_back(x); n     } n     sort(a[i].begin(), a[i].end()); n   } n   a[0] = a[n]; n   a[n + 1] = a[1]; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j < a[i].size(); j++) { n       if (upper_bound(a[i - 1].begin(), a[i - 1].end(), a[i][j]) - n               lower_bound(a[i - 1].begin(), a[i - 1].end(), a[i][j - 1]) != n           upper_bound(a[i + 1].begin(), a[i + 1].end(), a[i][j]) - n               lower_bound(a[i + 1].begin(), a[i + 1].end(), a[i][j - 1])) n         ans++; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( -O2 ) n using namespace std; n const long double PI = acos(-1); n const int MOD = 998244353, LIM = 2e5 + 5; n const long double EPS = 1e-9; n const int mod = 1000000007; n int main() { n   double l, p, q; n   std::cin >> l >> p >> q; n   double ans = l * p; n   ans /= p + q; n   std::cout << std::fixed << std::setprecision(9) << ans <<   n ; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (int)2e5 + 7; n const int INF = (int)1e9 + 7; n const long long LINF = (long long)1e12 + 7; n const int MOD = (int)1e9 + 7; n void file() {} n inline int readChar(); n template <class T = int> n inline T readInt(); n template <class Tl = long long> n inline Tl readLong(); n template <class T> n inline void writeInt(T x, char end = 0); n template <class Tl> n inline void writeLong(Tl x, char end = 0); n inline void writeChar(int x); n inline void writeWord(const char *s); n static const int buf_size = 4096; n inline int getChar() { n   static char buf[buf_size]; n   static int len = 0, pos = 0; n   if (pos == len) pos = 0, len = fread(buf, 1, buf_size, stdin); n   if (pos == len) return -1; n   return buf[pos++]; n } n inline int readChar() { n   int c = getChar(); n   while (c <= 32) c = getChar(); n   return c; n } n template <class T> n inline T readInt() { n   int s = 1, c = readChar(); n   T x = 0; n   if (c ==  - ) s = -1, c = getChar(); n   while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar(); n   return s == 1 ? x : -x; n } n template <class Tl> n inline Tl readLong() { n   int s = 1, c = readChar(); n   Tl x = 0; n   if (c ==  - ) s = -1, c = getChar(); n   while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getChar(); n   return s == 1 ? x : -x; n } n static int write_pos = 0; n static char write_buf[buf_size]; n inline void writeChar(int x) { n   if (write_pos == buf_size) n     fwrite(write_buf, 1, buf_size, stdout), write_pos = 0; n   write_buf[write_pos++] = x; n } n template <class T> n inline void writeInt(T x, char end) { n   if (x < 0) writeChar( - ), x = -x; n   char s[24]; n   int n = 0; n   while (x || !n) s[n++] =  0  + x % 10, x /= 10; n   while (n--) writeChar(s[n]); n   if (end) writeChar(end); n } n template <class Tl> n inline void writeLong(Tl x, char end) { n   if (x < 0) writeChar( - ), x = -x; n   char s[24]; n   int n = 0; n   while (x || !n) s[n++] =  0  + x % 10, x /= 10; n   while (n--) writeChar(s[n]); n   if (end) writeChar(end); n } n inline void writeWord(const char *s) { n   while (*s) writeChar(*s++); n } n struct Flusher { n   ~Flusher() { n     if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0; n   } n } flusher; n vector<vector<int> > G; n vector<pair<int, int> > p; n vector<int> coun; n void insert(int x) { n   int v = 0; n   for (int i = 30; i >= 0; --i) { n     ++coun[v]; n     if (G[v][(x >> i) & 1] == -1) { n       G.push_back(vector<int>(2, -1)); n       coun.push_back(0); n       p.push_back({v, (x >> i) & 1}); n       G[v][(x >> i) & 1] = G.size() - 1; n     } n     v = G[v][(x >> i) & 1]; n   } n   ++coun[v]; n } n void erase(int x) { n   int v = 0; n   for (int i = 30; i >= 0; --i) { n     --coun[v]; n     v = G[v][(x >> i) & 1]; n   } n   --coun[v]; n   while (p[v].first != -1 && !coun[v]) { n     G[p[v].first][p[v].second] = -1; n     v = p[v].first; n   } n } n int get(int x) { n   int ans = 0, v = 0; n   for (int i = 30; i >= 0; --i) { n     if (G[v][(x >> i) & 1] != -1) { n       if ((x >> i) & 1) { n         ans += (1 << i); n       } n       v = G[v][(x >> i) & 1]; n     } else { n       if (!((x >> i) & 1)) { n         ans += (1 << i); n       } n       v = G[v][!((x >> i) & 1)]; n     } n   } n   return ans; n } n int main() { n   file(); n   int q = readInt(); n   G.push_back(vector<int>(2, -1)); n   coun.push_back(0); n   p.push_back({-1, 0}); n   insert(0); n   while (q--) { n     char c = readChar(); n     int x = readInt(); n     if (c ==  + ) { n       insert(x); n     } n     if (c ==  - ) { n       erase(x); n     } n     if (c ==  ? ) { n       int cur = x ^ ((1ll << 31) - 1); n       int ans = get(cur) ^ x; n       writeInt(ans,   n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 5; n const int mod = 1e9 + 7; n int n, a, b; n double ans; n vector<vector<int>> g(N); n void dfs(int u, int par, int depth) { n   ans += 1.0 / depth; n   for (auto v : g[u]) n     if (v != par) dfs(v, u, depth + 1); n } n int main() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     cin >> a >> b; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   dfs(1, 1, 1); n   cout << fixed << setprecision(9) << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1005000; n int A[maxn], sum[maxn][10]; n int dfs(int u) { n   if (u < 10) return u; n   if (A[u]) return A[u]; n   int now = 1, tmp = u; n   while (u) { n     if (u % 10 != 0) now *= u % 10; n     u /= 10; n   } n   return A[tmp] = dfs(now); n } n int main() { n   for (int i = 1; i <= 1000000; i++) A[i] = dfs(i); n   for (int i = 1; i <= 1000000; i++) { n     for (int j = 1; j <= 9; j++) { n       if (A[i] == j) n         sum[i][j] = sum[i - 1][j] + 1; n       else n         sum[i][j] = sum[i - 1][j]; n     } n   } n   int Q; n   cin >> Q; n   while (Q--) { n     int l, r, k; n     scanf( %d%d%d , &l, &r, &k); n     cout << sum[r][k] - sum[l - 1][k] << endl; n   } n } n


#include <bits/stdc++.h> n int tcase, m; n long long n; n int gao(long long x) { n   int ret = 0; n   for (; x; x /= 10) ret += x % 10; n   return ret; n } n int main() { n   scanf( %d , &tcase); n   while (tcase--) { n     scanf( %lld%d , &n, &m); n     if (gao(n) <= m) { n       printf( 0 n ); n       continue; n     } n     long long x = 10; n     while (gao(n + (x - n % x)) > m) x *= 10; n     printf( %lld n , x - n % x); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1010; n int n, k, m; n int p10[maxn]; n inline void init() { n   p10[0] = 1; n   for (register int i = 1; i <= 1000; i++) { n     p10[i] = (p10[i - 1] * 10) % k; n   } n } n long long f[1010][110][2]; n long long dfs(long long pos, long long statu, bool fg) { n   if (pos == n + 1) { n     if (fg) return 1; n     return 0; n   } n   if (~f[pos][statu][fg]) { n     return f[pos][statu][fg]; n   } n   long long ans = 0; n   if (pos != n) ans += dfs(pos + 1, statu, fg); n   for (register int i = 1; i <= 9; i++) { n     ans += dfs(pos + 1, (statu + i * p10[pos - 1]) % k, n                fg || (!((statu + i * p10[pos - 1]) % k))); n     ans %= m; n   } n   f[pos][statu][fg] = ans; n   return ans; n } n int main() { n   memset(f, -1, sizeof(f)); n   scanf( %d%d%d , &n, &k, &m); n   init(); n   printf( %lld n , dfs(1, 0, 0)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = (int)1e9; n const double PI = 2 * acos(0.0); n const double eps = 1e-7; n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, 1, -1}; n int main() { n   int n, m; n   cin >> n >> m; n   string s; n   int mx = 0; n   int c = 0; n   for (int i = 0; i < m; i++) { n     cin >> s; n     int va = 0; n     for (int l = 0; l < ((int)((s).size())); l++) n       if (s[l] ==  0 ) { n         c++; n         va = 1; n         break; n       } n     if (va == 0) { n       mx = max(mx, c); n       c = 0; n     } n   } n   cout << max(mx, c) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n const long long inf = 1LL << 62; n const long long mx = 100005; n const double eps = 1e-10; n long long dx[10] = {1, 0, -1, 0}; n long long dy[10] = {0, -1, 0, 1}; n long long power(long long a, long long b) { n   if (b == 0) return 1; n   long long x = power(a, b / 2); n   x = x * x; n   if (b % 2) x = x * a; n   return x; n } n long long Set(long long N, long long pos) { return N = N | (1LL << pos); } n long long reset(long long N, long long pos) { return N = N & ~(1LL << pos); } n bool check(long long N, long long pos) { return (bool)(N & (1LL << pos)); } n string str, ss; n set<string> s; n int main() { n   long long tst, a, b, c, k, n, m, res = 0, ans = 0, t = 0; n   cin >> str; n   n = (long long)(str).size(); n   for (char ch =  a ; ch <=  z ; ch++) { n     ss.clear(); n     ss.push_back(ch); n     ss += str; n     s.insert(ss); n   } n   long long i, j; n   for (i = 0; i < n; i++) { n     ss.clear(); n     for (j = 0; j <= i; j++) { n       ss.push_back(str[j]); n     } n     string first = ss; n     for (char ch =  a ; ch <=  z ; ch++) { n       ss = first; n       ss.push_back(ch); n       for (j = i + 1; j < n; j++) { n         ss.push_back(str[j]); n       } n       s.insert(ss); n     } n   } n   cout << (long long)(s).size() <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int M, K, i, st, ed, ans[2], arr[100005]; n int main() { n   cin >> M >> K; n   for (i = st = ed = 0; i < M; i++) cin >> arr[i]; n   sort(arr, arr + M, greater<int>()); n   while (1) { n     while ((ed + 1) < M && (arr[st] - arr[ed + 1]) <= K) n       K -= (arr[st] - arr[++ed]); n     if ((ed - st + 1) >= ans[0]) { n       ans[0] = (ed - st + 1); n       ans[1] = arr[st]; n     } n     if (ed == M - 1 || st == M - 1) break; n     K += ((arr[st] - arr[st + 1]) * (ed - st)); n     st++; n   } n   cout << ans[0] <<     << ans[1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, q; n const long long MXN = 200005; n long long arr[MXN]; n long long seg[MXN * 4]; n void build(long long ind, long long l, long long r) { n   if (l == r) { n     seg[ind] = arr[l]; n     return; n   } n   long long m = (l + r) / 2; n   build(ind << 1, l, m); n   build((ind << 1) | 1, m + 1, r); n   seg[ind] = seg[ind * 2] + seg[ind * 2 + 1]; n } n void upd(long long ind, long long l, long long r, long long pos, long long to) { n   if (l == r) { n     seg[ind] = to; n     return; n   } n   long long m = (l + r) / 2; n   if (pos <= m) n     upd(ind << 1, l, m, pos, to); n   else n     upd((ind << 1) | 1, m + 1, r, pos, to); n   seg[ind] = seg[ind * 2] + seg[ind * 2 + 1]; n } n long long qry(long long ind, long long l, long long r, long long ql, n               long long qr) { n   if (ql <= l && r <= qr) { n     return seg[ind]; n   } n   if (ql > r || qr < l) { n     return 0; n   } n   long long m = (l + r) / 2; n   return qry(ind << 1, l, m, ql, qr) + qry((ind << 1) | 1, m + 1, r, ql, qr); n } n long long fact[MXN]; n set<long long> rm; n long long T(long long dv) { n   auto x = rm.begin(); n   for (long long j = 0; j < dv; j++) x++; n   return *x; n } n vector<long long> wh; n void sep(long long kth) { n   for (long long i = 1; i <= n; i++) { n     rm.insert(i); n   } n   for (long long pl = n - 1; pl >= 0; pl--) { n     long long dv = kth / fact[pl]; n     kth %= fact[pl]; n     long long K = T(dv); n     wh.push_back(K); n     rm.erase(K); n   } n   for (long long i = 1; i <= n; i++) { n     arr[i] = wh[i - 1]; n   } n   wh.clear(); n } n long long SP = 0; n void rew(long long kth) { n   if (SP == kth) return; n   if (n <= 15) { n     sep(kth); n   } else { n     for (long long i = 1; i <= 15; i++) { n       rm.insert(i); n     } n     for (long long pl = 15 - 1; pl >= 0; pl--) { n       long long dv = kth / fact[pl]; n       kth %= fact[pl]; n       long long K = T(dv); n       wh.push_back(K); n       rm.erase(K); n     } n     for (long long i = 1; i <= 15; i++) { n       arr[n - 15 + i] = n - 15 + wh[i - 1]; n     } n     wh.clear(); n   } n   SP = kth; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   fact[0] = 1; n   cin >> n >> q; n   for (long long i = 1; i <= n; i++) arr[i] = i; n   for (long long i = 1; i <= 20; i++) { n     fact[i] = fact[i - 1] * i; n   } n   long long k = 0; n   build(1, 1, n); n   for (long long i = 0; i < q; i++) { n     long long t; n     cin >> t; n     if (t == 1) { n       rew(k); n       for (long long i = max(1ll, n - 20); i <= n; i++) { n         upd(1, 1, n, i, arr[i]); n       } n       long long l, r; n       cin >> l >> r; n       cout << qry(1, 1, n, l, r) <<   n ; n     } else { n       long long w; n       cin >> w; n       k += w; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 10; n int main() { n   int n; n   scanf( %d , &n); n   vector<long long> a(n); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &a[i]); n   } n   vector<long long> t(n); n   for (int i = 0; i < n; i++) { n     scanf( %lld , &t[i]); n   } n   multiset<long long> S; n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     S.insert(a[i] + sum); n     long long ans = 0; n     sum += t[i]; n     while (!S.empty()) { n       long long top = *S.begin(); n       if (top < sum) { n         ans += top - (sum - t[i]); n         S.erase(S.begin()); n       } else { n         break; n       } n     } n     ans += (long long)(int)((S).size()) * t[i]; n     printf( %lld  , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long ori[100005]; n int main() { n   scanf( %d , &n); n   int i = 1; n   for (i; i <= n; i++) { n     scanf( %I64d , &ori[i]); n   } n   sort(ori + 1, ori + n + 1); n   i = 1; n   int ss = n - 2; n   bool flag = false; n   for (i; i <= ss; i++) { n     int j = i + 1; n     int k = j + 1; n     if (ori[i] + ori[j] > ori[k]) { n       flag = true; n       break; n     } n   } n   if (flag) { n     printf( YES ); n   } else { n     printf( NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<pair<int, int>> a(n); n   vector<int> b(n); n   vector<int> c(n); n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     a[i] = {x, i}; n   } n   while (k--) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     if (a[x].first > a[y].first) b[x]++; n     if (a[x].first < a[y].first) b[y]++; n   } n   sort(a.begin(), a.end()); n   int l = 0; n   for (int i = 0; i < n; i++) { n     k = i; n     k -= l; n     k -= b[a[i].second]; n     c[a[i].second] = k; n     if (i < n - 1) { n       if (a[i].first < a[i + 1].first) n         l = 0; n       else n         l++; n     } n   } n   for (auto it : c) cout << it <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 300500; n struct node { n   long long x; n   int id; n   node() {} n   bool operator<(const node ot) const { n     if (x != ot.x) return x < ot.x; n     return id < ot.id; n   } n } a[maxn]; n long long x[maxn]; n long long su[maxn]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < (int)(n); ++i) { n     scanf( %lld , &x[i]); n     a[i].x = x[i] + 1e9; n     a[i].id = i + 1; n   } n   int k; n   scanf( %d , &k); n   sort(a, a + n); n   long long dif = 1e18, ans = 0; n   long long cur = 0; n   for (int i = 0; i < (int)(n); ++i) su[i + 1] = su[i] + a[i].x; n   for (int i = 0; i < (int)(k); ++i) cur += a[i].x * i - (su[i]); n   dif = cur; n   for (int i = k; i < n; i++) { n     int j = i - k; n     cur -= (su[i] - su[j + 1]) - a[j].x * (k - 1); n     cur += a[i].x * (k - 1) - (su[i] - su[j + 1]); n     if (cur < dif) { n       ans = i - k + 1; n       dif = cur; n     } n   } n   for (int i = 0; i < (int)(k); ++i) printf( %d  , a[ans + i].id); n } n


#include <bits/stdc++.h> n using namespace std; n long double f[1011][1011]; n int m, n; n void init() { n   memset(f, 0, sizeof f); n   f[1][1] = 1; n   for (int i = (1), _b = (n - 1); i <= _b; i++) { n     for (int x = (0), _b = (min(i, m)); x <= _b; x++) n       if (f[i][x] > 1e-50) { n         f[i + 1][x] += f[i][x] * ((m * n - m) - (i - x)) / (double)(m * n - i); n         f[i + 1][x + 1] += f[i][x] * (m - x) / (double)(m * n - i); n       } n   } n } n int main() { n   ios ::sync_with_stdio(false); n   cout << (fixed) << setprecision(8); n   while (cin >> n >> m) { n     init(); n     double res = 0.0; n     for (int k = (1), _b = (min(m, n)); k <= _b; k++) { n       res += f[n][k] * k / (double)n; n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e5 + 10; n long long int vis[N]; n vector<vector<int> > v; n int dfs(int t) { n   vis[t] = 1; n   for (int i = 0; i < v[t].size(); i++) { n     if (vis[v[t][t]] == 0) { n       dfs(v[t][i]); n     } n   } n   return 0; n } n int main() { n   long long int n, k; n   cin >> n >> k; n   long long int y = 1; n   long long int i = 0; n   long long int ans = 0, p = 0, d = 1; n   while (1) { n     if (d > n) { n       break; n     } n     p++; n     d = pow(2, p); n   } n   k = min(k, p + 1); n   long double o = 1 - (long double)(1 / (long double)(pow(2, k))); n   if (k == 1) n     cout << max(n, d / 2) << endl; n   else { n     ans = 2 * (d / 2) - 1; n     cout << ans << endl; n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n const int maxn=1000025; n const int mod=998244353; n int n,m,i,j,t,k,s,f[maxn],ans,dp[maxn],iv[10]; n  n inline int calc(int x) n { n  int ret=x*1ll*(x-1)%mod*(x-2)%mod*iv[6]%mod;//3 n  ret=(ret+x*1ll*(x-1))%mod;//2 n  ret=(ret+x)%mod;//1 n  //printf( calc %d %d n ,x,ret); n  return ret; n } n  n int main() n { n  iv[1]=1;iv[2]=(mod+1)/2;iv[3]=(mod+1)/3;iv[6]=iv[2]*1ll*iv[3]%mod; n  scanf( %d ,&n); n  f[0]=1;s=1; n  for (i=1;i<n;++i) n  { n   f[i]=(1ll*s*(s-1)/2ll%mod+s*1ll+1)%mod; n   dp[i]=(f[i]-f[i-1]+mod)%mod; n   //f[i]=(f[i]-s+mod)%mod; n   s=(s+f[i])%mod; n  } n  m=n-1; n  ans=(1ll*s*(s-1)/2ll%mod+s*1ll+1ll)%mod; n  ans=(ans+calc(s))%mod; n  ans=(ans-calc((s+mod-f[m])%mod)+mod)%mod; n  ans=2ll*(ans+mod)%mod; n  ans=(ans-1+mod)%mod; n  for (i=n-1;i>0;--i) n  { n   ans=(ans+dp[i]*1ll*(f[n-i-1]-1))%mod; n  } n  //ans=(ans-1ll*(f[(n-1)>>1]-1)+mod)%mod; n  ans=(ans+mod)%mod; n  printf( %d n ,ans); n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a[n][3]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 3; j++) { n       cin >> a[i][j]; n     } n   } n   int c = 1; n   for (int i = 0; i < 3; i++) { n     int sum = 0; n     for (int j = 0; j < n; j++) { n       sum += a[j][i]; n     } n     if (sum != 0) { n       c = 0; n       cout <<  NO ; n       break; n     } n   } n   if (c) { n     cout <<  YES ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> edge[80005]; n int cnt[80005]; n unsigned long long ret; n int n; n void dfs(int v, int u) { n   cnt[v] = 1; n   unsigned long long val = 1ull * n * (n - 1) / 2ull; n   for (int i = 0; i < edge[v].size(); i++) { n     int to = edge[v][i]; n     if (to == u) continue; n     dfs(to, v); n     val -= 1ull * cnt[to] * (cnt[to] - 1) / 2ull; n     cnt[v] += cnt[to]; n   } n   val -= 1ull * (n - cnt[v]) * (n - cnt[v] - 1) / 2ull; n   ret -= (val * val - n           (1ull * (n - cnt[v]) * cnt[v]) * (1ull * (n - cnt[v]) * cnt[v])); n } n int main() { n   cin >> n; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     edge[u].push_back(v); n     edge[v].push_back(u); n   } n   ret = 1ull * n * (n - 1) / 2ull; n   ret *= ret; n   dfs(rand() % n + 1, -1); n   cout << ret << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n set<string> s; n int main() { n   cin >> n; n   getchar(); n   while (n--) { n     string ss; n     getline(cin, ss); n     s.insert(ss); n   } n   cout << s.size(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2000 * 100 + 10; n int endss[maxn]; n int main() { n   int n, M, T; n   cin >> n >> M >> T; n   vector<pair<pair<int, int>, int> > ps(2 * n); n   for (int i = 0; i < n; i++) { n     int h, m, s; n     scanf( %d:%d:%d n , &h, &m, &s); n     int time = 60 * (60 * h + m) + s; n     ps[2 * i] = make_pair(make_pair(time, 0), i); n     ps[2 * i + 1] = make_pair(make_pair(time + T - 1, 1), i); n   } n   sort(ps.begin(), ps.end()); n   int cur = -1; n   int online = 0; n   vector<int> ans(n); n   bool ok = false; n   for (int i = 0; i < (int)(ps).size(); i++) { n     if (ps[i].first.second == 0) { n       online++; n       if (online > M) { n         online--; n       } else { n         if (online == M) ok = true; n         cur++; n       } n       ans[ps[i].second] = cur; n       endss[cur]++; n     } else { n       if (endss[ans[ps[i].second]]) endss[ans[ps[i].second]]--; n       if (!endss[ans[ps[i].second]]) online--; n     } n   } n   if (!ok) n     puts( No solution ); n   else { n     printf( %d n , cur + 1); n     for (int i = 0; i < n; i++) printf( %d n , ans[i] + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64000000 ) n using namespace std; n const int inf = (int)1e9; n const int mod = inf + 7; n const double eps = 1e-9; n const double pi = acos(-1.0); n int n, a[200200], b[200200]; n int mx[200200]; n set<pair<int, int> > second; n vector<int> g[200200]; n void restore(vector<int> &v) { n   for (int i = 0; i < v.size(); i++) { n     int x = v[i]; n     if (mx[x] != -1) second.insert(make_pair(-mx[x], x)); n   } n } n void solve() { n   int sz; n   int x; n   vector<int> v; n   scanf( %d , &sz); n   for (int i = 0; i < sz; i++) { n     scanf( %d , &x); n     x--; n     v.push_back(x); n     if (mx[x] != -1) second.erase(make_pair(-mx[x], x)); n   } n   if (second.size() == 0) { n     printf( 0 0 n ); n     restore(v); n     return; n   } n   if (second.size() == 1) { n     int x = (*second.begin()).second; n     printf( %d %d n , x + 1, b[g[x][0]]); n     restore(v); n     return; n   } n   __typeof(second.begin()) it = second.begin(); n   int y = (*it).second; n   it++; n   int X = (*it).second; n   int t = mx[X]; n   vector<int> &a = g[y]; n   int l = 0, r = (int)a.size() - 1; n   int best = r; n   while (l <= r) { n     int mid = (l + r) >> 1; n     if (a[mid] > t) { n       best = mid; n       r = mid - 1; n     } else n       l = mid + 1; n   } n   printf( %d %d n , y + 1, b[a[best]]); n   restore(v); n } n int main() { n   memset(mx, -1, sizeof mx); n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , a + i, b + i); n     a[i]--; n     mx[a[i]] = i; n     g[a[i]].push_back(i); n   } n   for (int i = 0; i < n; i++) { n     if (mx[i] == -1) continue; n     second.insert(make_pair(-mx[i], i)); n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = (int)1e9 + 7; n int a[17]; n int c[107][107]; n int dp[17][107]; n int n; n int main() { n   cin >> n; n   for (int i = 0; i < 10; i++) cin >> a[i]; n   for (int i = 0; i <= 100; i++) { n     c[i][0] = c[i][i] = 1; n     for (int j = 1; j < i; j++) { n       c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % M; n     } n   } n   int ans = 0; n   dp[10][0] = 1; n   for (int dig = 9; dig >= 0; dig--) { n     for (int cnt = a[dig]; cnt <= n; cnt++) { n       for (int rem = 0; rem + cnt <= n; rem++) { n         dp[dig][cnt + rem] = n             (dp[dig][cnt + rem] + n              (dp[dig + 1][rem] * 1ll * c[cnt + rem - (dig ? 0 : 1)][cnt] % M)) % n             M; n       } n     } n   } n   for (int len = 1; len <= n; len++) ans = (ans + dp[0][len]) % M; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[101101]; n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   sort(a, a + n); n   int l = 0, r = n - 1, ans = 0; n   for (int i = 0; i < n; i++) { n     if (a[l] < a[i]) { n       ans++; n       l++; n     } else n       r--; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T, class U> n void maximize(T &x, U y) { n   if (x < y) x = y; n } n template <class T, class U> n void minimize(T &x, U y) { n   if (x > y) x = y; n } n template <class T> n T Abs(T x) { n   return (x < (T)0 ? -x : x); n } n namespace task { n const int N = 505; n bool f[N][N]; n int a[N], b[N]; n int n, k; n long long tot = 0; n void solve() { n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i] >> b[i]; n     tot += a[i]; n     tot += b[i]; n   } n   f[0][0] = true; n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j < k; ++j) { n       f[i][j] = f[i - 1][(j - a[i] % k + k) % k]; n       for (int l = 0; l <= min(a[i], k - 1); ++l) { n         if ((a[i] - l) % k + b[i] >= k) { n           f[i][j] |= f[i - 1][(j - l + k) % k]; n         } n       } n     } n   } n   long long ans = 0; n   for (int i = 0; i < k; ++i) { n     if (f[n][i]) { n       maximize(ans, (tot - 1LL * i) / (1LL * k)); n     } n   } n   cout << ans; n } n }  // namespace task n int main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   task::solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int ted[1010]; n int main() { n   memset(ted, 0, sizeof ted); n   int n; n   cin >> n; n   while (n--) { n     int x; n     cin >> x; n     ted[x]++; n   } n   int ans = 0; n   int mx = 0; n   for (int i = 1; i <= 1000; i++) n     if (ted[i]) { n       ans++; n       mx = max(mx, ted[i]); n     } n   cout << mx <<     << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   long long N, M, K; n   cin >> N >> K >> M; n   vector<long long> A(N); n   long long s = 0; n   for (int i = 0; i < N; i++) { n     cin >> A[i]; n     s += A[i]; n   } n   sort(A.begin(), A.end()); n   long double res = (long double)(s + min(M, N * K)) / (long double)N; n   for (int i = 0; i < min(N - 1, M); i++) { n     s -= A[i]; n     long long tp = s + min((N - i - 1) * K, M - i - 1); n     long double avg = (long double)tp / (long double)(N - i - 1); n     res = max(res, avg); n   } n   cout << fixed << setprecision(20) << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   long long n; n   cin >> n; n   vector<long long> v(n); n   for (long long i = 0; i < n; i++) { n     cin >> v[i]; n   } n   sort(v.begin(), v.end()); n   long long count = 0; n   while (v.size() > 0) { n     count++; n     long long num = v[0]; n     for (long long i = 0; i < v.size(); i++) { n       if (v[i] % num == 0) { n         v.erase(v.begin() + i, v.begin() + i + 1); n         i--; n       } n     } n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using lld = long long; n using pi = pair<int, int>; n using pl = pair<lld, lld>; n using vi = vector<int>; n using vvi = vector<vector<int>>; n using vb = vector<bool>; n using vc = vector<char>; n using vl = vector<lld>; n using vp = vector<pi>; n using vs = vector<string>; n using si = stack<int>; n using qi = queue<int>; n using qp = queue<pi>; n using pqi = priority_queue<int>; n const lld mod = 998244353; n int n, mx; n vi t, c, adj; n vb chk; n void input() { n   t.clear(); n   c.clear(); n   adj.clear(); n   chk.clear(); n   cin >> n; n   t = vi(n); n   c = vi(n); n   adj = vi(n, -1); n   chk = vb(n); n   for (int i = 0; i < n; ++i) cin >> t[i]; n } n void go(int cur) { n   chk[cur] = true; n   int nxt = adj[cur]; n   if (nxt == -1) return; n   if (c[nxt]) { n     c[cur] = c[nxt] == 1 ? 2 : 1; n     return; n   } n   c[nxt] = c[cur] == 1 ? 2 : 1; n   int t = c[nxt]; n   go(nxt); n   if (t != c[nxt]) { n     c[cur] = c[nxt] == 1 ? 2 : 1; n   } n } n void solve() { n   for (int i = 0; i < n; ++i) n     if (t[i] != t[(i + 1) % n]) adj[i] = (i + 1) % n; n   int cnt = 0; n   for (int i = 0; i < n; ++i) { n     if (chk[i]) continue; n     c[i] = 1; n     go(i); n     cnt++; n   } n   if (cnt == 1) n     if (t[n - 1] != t[0] && c[n - 1] == c[0]) c[n - 1] = 3; n   int mx = 1; n   for (int i = 0; i < n; ++i) mx = max(mx, c[i]); n   cout << mx <<   n ; n   for (int i = 0; i < n; ++i) cout << c[i] <<    ; n   cout <<   n ; n } n int main() { n   cin.tie(nullptr); n   ios_base::sync_with_stdio(false); n   int tc; n   cin >> tc; n   while (tc--) { n     input(); n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int c = 100005; n void solve() { n   string s; n   cin >> s; n   long long o = 0, e = 0, m = 0; n   long long ans = INT_MAX; n   long long om = 5, em = 5; n   for (long long i = 0; i < s.size(); ++i) { n     if (i & 1) { n       if (s[i] ==  ?  or s[i] ==  1 ) o += 1; n       om -= 1; n     } else { n       if (s[i] ==  1 ) e += 1; n       em -= 1; n     } n     if ((o > e + em) or (e > o + om)) { n       ans = min(ans, i + 1); n       break; n     } n   } n   om = 5; n   em = 5; n   o = e = 0; n   for (long long i = 0; i < s.size(); ++i) { n     if (i & 1) { n       if (s[i] ==  1 ) o += 1; n       om -= 1; n     } else { n       if (s[i] ==  1  or s[i] ==  ? ) e += 1; n       em -= 1; n     } n     if ((o > e + em) or (e > o + om)) { n       ans = min(ans, i + 1); n       break; n     } n   } n   if (ans == INT_MAX) ans = 10; n   cout << ans <<   n ; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500 + 5; n char mp[maxn][maxn]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) scanf(  %c , &mp[i][j]); n   int x1, y1, x2, y2; n   for (int i = 0; i < n; i++) { n     bool bb = false; n     for (int j = 0; j < m; j++) n       if (mp[i][j] ==  X ) { n         x1 = i; n         y1 = j; n         bb = true; n         break; n       } n     if (bb) break; n   } n   for (int i = n - 1; i >= 0; i--) { n     bool bb = false; n     for (int j = m - 1; j >= 0; j--) { n       if (mp[i][j] ==  X ) { n         x2 = i; n         y2 = j; n         bb = true; n         break; n       } n     } n     if (bb) break; n   } n   if (x1 > x2 || y1 > y2) { n     puts( NO ); n     return 0; n   } n   for (int i = x1; i <= x2; i++) n     for (int j = y1; j <= y2; j++) { n       if (mp[i][j] ==  . ) { n         puts( NO ); n         return 0; n       } n       mp[i][j] =  . ; n     } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (mp[i][j] ==  X ) { n         puts( NO ); n         return 0; n       } n   puts( YES ); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int test_time, x = 0; n   cin >> test_time; n   for (int i = 0; i < test_time; ++i) { n     string temp; n     cin >> temp; n     for (int j = 0; j < temp.length(); ++j) { n       if (temp[j] ==  - ) { n         x--; n         break; n       } else if (temp[j] ==  + ) { n         x++; n         break; n       } n     } n   } n   cout << x; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n struct SegTree { n   int L, R, P, DELTA; n   struct node { n     int d; n     bool zero; n   } v[212345 * 4]; n   inline void set_zero(int x) { n     v[x].zero = 1; n     v[x].d = 0; n   } n   inline void fix(int x, int d) { n     v[x].zero = 0; n     v[x].d += d; n   } n   inline void rep(int x) { n     if (v[x].zero) { n       set_zero(x * 2); n       set_zero(x * 2 + 1); n       v[x].zero = 0; n     } else { n       fix(x * 2, v[x].d); n       fix(x * 2 + 1, v[x].d); n       v[x].d = 0; n     } n   } n   void update(int x, int l, int r) { n     if (l > R || r < L) return; n     if (L <= l && r <= R) { n       if (l < r) n         rep(x); n       else n         v[x].zero = 0; n       v[x].d += DELTA; n       return; n     } n     int m = (l + r) / 2; n     rep(x); n     update(x * 2, l, m); n     update(x * 2 + 1, m + 1, r); n   } n   void gao_zero(int x, int l, int r) { n     if (l > R || r < L) return; n     if (L <= l && r <= R) { n       set_zero(x); n       return; n     } n     int m = (l + r) / 2; n     rep(x); n     gao_zero(x * 2, l, m); n     gao_zero(x * 2 + 1, m + 1, r); n   } n   int add(int x, int y) { n     if (~y) return x + y; n     return -1; n   } n   int query(int x, int l, int r) { n     if (v[x].zero) return -1; n     if (l == r) return v[x].d; n     int m = (l + r) / 2; n     if (P <= m) n       return add(v[x].d, query(x * 2, l, m)); n     else n       return add(v[x].d, query(x * 2 + 1, m + 1, r)); n   } n   void up(int l, int r, int d) { n     L = l, R = r, DELTA = d; n     update(1, 1, n); n   } n   void zero(int l, int r) { n     L = l, R = r; n     gao_zero(1, 1, n); n   } n   int qr(int p) { n     P = p; n     return query(1, 1, n); n   } n } tree; n int x[212345], len[212345]; n vector<pair<int, int> > qu[212345]; n int ans[212345], nxt[212345]; n int L[212345], D[212345], id[212345], top = 0; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d%d , x + i, len + i); n   int q; n   scanf( %d , &q); n   for (int i = 0; i < int(q); i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     qu[u].push_back(make_pair(v, i)); n   } n   nxt[n] = -1; n   for (int i = n - 1; i >= 1; --i) { n     int j = i + 1; n     while (~j && x[j] + len[j] <= x[i] + len[i]) j = nxt[j]; n     nxt[i] = j; n   } n   for (int i = n; i >= 1; --i) { n     if (nxt[i] == -1) { n     } else { n       while (top > 0 && id[top] < nxt[i]) { n         tree.up(L[top], n, -D[top]); n         --top; n       } n       if (x[i] + len[i] >= x[nxt[i]]) { n       } else { n         tree.up(nxt[i], n, x[nxt[i]] - x[i] - len[i]); n         ++top; n         L[top] = nxt[i]; n         D[top] = x[nxt[i]] - x[i] - len[i]; n         id[top] = i; n       } n     } n     for (auto x : qu[i]) ans[x.second] = tree.qr(x.first); n   } n   for (int i = 0; i < int(q); i++) printf( %d n , ~ans[i] ? ans[i] : 0); n } n


#include <bits/stdc++.h> n using namespace std; n struct car { n   int c, v; n }; n bool operator<(car a, car b) { return a.v < b.v; } n int main() { n   ios::sync_with_stdio(false); n   int n, k, s, t; n   cin >> n >> k >> s >> t; n   vector<car> arr(n); n   for (int i = 0; i < n; i++) cin >> arr[i].c >> arr[i].v; n   vector<int> g(k + 2); n   g[0] = 0; n   for (int i(1); i <= k; i++) cin >> g[i]; n   k += 2; n   g[k - 1] = s; n   sort(arr.begin(), arr.end()); n   sort(g.begin(), g.end()); n   int l = -1, r = n; n   int cnt = 0; n   while (r - l > 1) { n     cnt++; n     int m = (r + l) / 2; n     int time = 0; n     for (int i(1); i < k; i++) { n       int d = g[i] - g[i - 1]; n       int a = min(arr[m].v - d, d); n       int norm = d - a; n       if (arr[m].v < d) { n         time = 2000000007; n         break; n       } n       time += a + 2 * norm; n     } n     if (time > t) n       l = m; n     else n       r = m; n   } n   if (r == n) n     cout << -1 << endl; n   else { n     int ans = 1000000007; n     for (int i = r; i < n; ++i) ans = min(ans, arr[i].c); n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char t[200003], p[200003]; n int a, pos[200003], nt, np; n bool check(int mid) { n   int it = 0; n   for (int ip = 0; ip < np; ip++) { n     while (it < nt && (t[it] != p[ip] || pos[it + 1] <= mid)) it++; n     if (it == nt) return false; n     it++; n   } n   return true; n } n int main() { n   scanf( %s , t); n   scanf( %s , p); n   nt = strlen(t); n   np = strlen(p); n   for (int i = 1; i <= nt; i++) { n     scanf( %d , &a); n     pos[a] = i; n   } n   int lo = 1, mid, hi = nt; n   while (hi >= lo) { n     mid = (lo + hi) / 2; n     if (check(mid)) n       lo = mid + 1; n     else n       hi = mid - 1; n   } n   printf( %d n , hi); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000 + 10; n char ch[N]; n int a[N][N]; n int h[N], l[N]; n int n, m; n int main() { n   cin >> n >> m; n   int tot = 0; n   for (int i = 1; i <= n; i++) { n     scanf( %s , ch + 1); n     for (int j = 1; j <= m; j++) { n       a[i][j] = ch[j] ==  *  ? 1 : 0; n       h[i] += a[i][j]; n       l[j] += a[i][j]; n       tot += a[i][j]; n     } n   } n   int x = -1, y = -1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (h[i] + l[j] - a[i][j] == tot) { n         x = i; n         y = j; n         break; n       } n   if (x == -1) { n     puts( NO ); n   } else { n     puts( YES ); n     cout << x <<     << y << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n string to_string(pair<A, B> p); n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> p); n string to_string(const string& s) { return     + s +    ; } n string to_string(const char* s) { return to_string((string)s); } n string to_string(bool b) { return (b ?  true  :  false ); } n string to_string(vector<bool> v) { n   bool f = true; n   string res =  { ; n   for (long long i = (0); i <= (static_cast<long long>(v.size()) - 1); i++) { n     if (!f) res +=  ,  ; n     res += to_string(v[i]); n     f = false; n   } n   res +=  } ; n   return res; n } n template <size_t N> n string to_string(bitset<N> v) { n   string res =   ; n   for (auto& a : v) { n     res += (char)( 0  + a); n   } n   return res; n } n template <typename A> n string to_string(A v) { n   bool f = true; n   string res =  { ; n   for (auto& x : v) { n     if (!f) res +=  ,  ; n     res += to_string(x); n     f = false; n   } n   res +=  } ; n   return res; n } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A, typename B, typename C> n string to_string(tuple<A, B, C> t) { n   return  (  + to_string(get<0>(t)) +  ,   + to_string(get<1>(t)) +  ,   + n          to_string(get<2>(t)) +  ) ; n } n void DBG() { cerr << endl; } n template <typename H, typename... T> n void DBG(H h, T... t) { n   cerr <<     << to_string(h); n   DBG(t...); n } n template <typename T> n void re(T& x) { n   cin >> x; n }; n template <typename H, typename... T> n void re(H& h, T&... t) { n   re(h); n   re(t...); n } n template <typename T> n void pr(T x) { n   cout << x; n }; n template <typename H, typename... T> n void pr(H h, T... t) { n   pr(h); n   pr(t...); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long tt; n   re(tt); n   while (tt--) { n     string s; n     re(s); n     long long n = (long long)s.size(); n     long long a, b, c; n     a = b = c = -1; n     long long inf = (long long)9e18; n     long long ans = inf; n     for (long long i = (0); i <= (n - 1); i++) { n       if (s[i] ==  1 ) { n         a = i; n         if (b == -1 || c == -1) continue; n         ans = min(ans, i - min(b, c) + 1); n       } n       if (s[i] ==  2 ) { n         b = i; n         if (a == -1 || c == -1) continue; n         ans = min(ans, i - min(a, c) + 1); n       } n       if (s[i] ==  3 ) { n         c = i; n         if (b == -1 || a == -1) continue; n         ans = min(ans, i - min(b, a) + 1); n       } n     } n     pr((ans == inf ? 0 : ans),   n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200015], n, k, b[100015]; n int cmp(int x, int y) { return x < y; } n int main() { n   while (scanf( %d%d , &n, &k) != EOF) { n     memset(a, 0, sizeof(a)); n     int ans = 0, flag = 1, t = 0; n     int m = k; n     for (int i = 0; i < n; i++) { n       scanf( %d , &a[i]); n       if (a[i] < 0) t++; n       if (flag == 1 && a[i] < 0) { n         flag = 0; n         ans++; n       } else if (flag == 0 && a[i] >= 0) { n         flag = 1; n         ans++; n       } n     } n     if (t == 0) { n       printf( 0 n ); n     } else if (t > k) { n       printf( -1 n ); n     } else if (t == k) { n       printf( %d n , ans); n     } else { n       k = k - t; n       int sum = 0, c = -1; n       for (int i = n - 1; i >= 0; i--) { n         if (a[i] < 0) { n           if (c == -1) { n             t = sum; n             c++; n           } else if (sum) { n             b[c] = sum; n             c++; n           } n           sum = 0; n         } else n           sum++; n       } n       sort(b, b + c, cmp); n       int ans1 = ans, ans2 = ans, kk = k; n       if (kk > 0) { n         for (int i = 0; i < c; i++) { n           if (kk - b[i] >= 0) { n             kk -= b[i]; n             ans1 -= 2; n           } else n             break; n         } n       } n       kk = k - t; n       if (kk >= 0 && t) { n         ans2 = ans - 1; n         for (int i = 0; i < c; i++) { n           if (kk - b[i] >= 0) { n             kk -= b[i]; n             ans2 -= 2; n           } else n             break; n         } n       } n       printf( %d n , min(ans1, ans2)); n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int mark[(10000000 >> 5) + 1]; n void sieve() { n   register int i, j, k; n   (mark[1 >> 5] |= 1 << (1 & 31)); n   int n = 10000000; n   for (i = 2; i <= n; i++) { n     if (!(mark[i >> 5] >> (i & 31) & 1)) { n       for (k = n / i, j = i * k; k >= i; k--, j -= i) n         (mark[j >> 5] |= 1 << (j & 31)); n     } n   } n } n long long C(long long n, long long k) { n   if (k == 0) return 1; n   return (n * C(n - 1, k - 1)) / k; n } n long long modular_pow(long long budgetase, long long exponent, int modulus) { n   long long result = 1; n   while (exponent > 0) { n     if (exponent % 2 == 1) result = (result * budgetase) % modulus; n     exponent = exponent >> 1; n     budgetase = (budgetase * budgetase) % modulus; n   } n   return result; n } n long long binaryToDec(string number) { n   long long result = 0, pow = 1; n   for (int i = number.length() - 1; i >= 0; --i, pow <<= 1) n     result = (result + (number[i] -  0 ) * pow) % 1000003; n   return result; n } n long long GCD(long long a, long long b) { return b == 0 ? a : GCD(b, a % b); } n int main() { n   double d, h, v, e; n   cin >> d >> h >> v >> e; n   double r = d / 2; n   double area = 3.1415926535897932384626433832795 * r * r; n   if (e * area > v || fabs(e * area - v) < 0.0000000001) { n     cout <<  NO  << endl; n     return 0; n   } n   double vol = 3.1415926535897932384626433832795 * r * r * h; n   double p = e * 3.1415926535897932384626433832795 * r * r; n   double dif = fabs(p - v); n   cout <<  YES  << endl; n   cout << setprecision(12) << fixed << (double)vol / dif << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n int s, t, n; n string str; n bool flag = true, flag1 = true; n int main() { n   scanf( %d , &n); n   cin >> str; n   for (int i = 0; i < n; i++) { n     if (str[i] ==  R  && flag) { n       s = i; n       flag = false; n     } n     if (str[i] ==  L  && flag1) { n       t = i; n       flag1 = false; n     } n   } n   if (flag1) { n     for (int i = 0; i < n; i++) n       if (str[i] ==  R ) t = i; n     printf( %d %d , s + 1, t + 2); n     return 0; n   } n   if (flag) { n     for (int i = 0; i < n; i++) n       if (str[i] ==  L ) s = i; n     printf( %d %d , s + 1, t); n     return 0; n   } n   printf( %d %d , s + 1, t); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long INF = -10000000; n int main() { n   long long da, db; n   cin >> da >> db; n   if (da == db) { n     cout << da * 10 <<     << db * 10 + 1; n   } else if (da == 9 && db == 1) { n     cout << da * 10 + 9 <<     << db * 100; n   } else if (da == 0 && db == 1) { n     cout << da <<     << db; n   } else if (da + 1 == db) { n     cout << da * 10 + 9 <<     << db * 10; n   } else { n     cout << -1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n std::istream& operator>>(std::istream& i, pair<T, U>& p) { n   i >> p.first >> p.second; n   return i; n } n template <typename T> n std::istream& operator>>(std::istream& i, vector<T>& t) { n   for (auto& v : t) { n     i >> v; n   } n   return i; n } n template <typename T, typename U> n std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) { n   o << p.first <<     << p.second; n   return o; n } n template <typename T> n std::ostream& operator<<(std::ostream& o, const vector<T>& t) { n   if (t.empty()) o <<   n ; n   for (size_t i = 0; i < t.size(); ++i) { n     o << t[i] <<    n [i == t.size() - 1]; n   } n   return o; n } n template <typename T> n using minheap = priority_queue<T, vector<T>, greater<T>>; n template <typename T> n using maxheap = priority_queue<T, vector<T>, less<T>>; n template <typename T> n bool in(T a, T b, T c) { n   return a <= b && b < c; n } n unsigned int logceil(long long first) { n   return first ? 8 * sizeof(long long) - __builtin_clzll(first) : 0; n } n namespace std { n template <typename T, typename U> n struct hash<pair<T, U>> { n   hash<T> t; n   hash<U> u; n   size_t operator()(const pair<T, U>& p) const { n     return t(p.first) ^ (u(p.second) << 7); n   } n }; n }  // namespace std n template <typename T, typename F> n T bsh(T l, T h, const F& f) { n   T r = -1, m; n   while (l <= h) { n     m = (l + h) / 2; n     if (f(m)) { n       l = m + 1; n       r = m; n     } else { n       h = m - 1; n     } n   } n   return r; n } n template <typename F> n double bshd(double l, double h, const F& f, double p = 1e-9) { n   unsigned int r = 3 + (unsigned int)log2((h - l) / p); n   while (r--) { n     double m = (l + h) / 2; n     if (f(m)) { n       l = m; n     } else { n       h = m; n     } n   } n   return (l + h) / 2; n } n template <typename T, typename F> n T bsl(T l, T h, const F& f) { n   T r = -1, m; n   while (l <= h) { n     m = (l + h) / 2; n     if (f(m)) { n       h = m - 1; n       r = m; n     } else { n       l = m + 1; n     } n   } n   return r; n } n template <typename F> n double bsld(double l, double h, const F& f, double p = 1e-9) { n   unsigned int r = 3 + (unsigned int)log2((h - l) / p); n   while (r--) { n     double m = (l + h) / 2; n     if (f(m)) { n       h = m; n     } else { n       l = m; n     } n   } n   return (l + h) / 2; n } n template <typename T> n T gcd(T a, T b) { n   if (a < b) swap(a, b); n   return b ? gcd(b, a % b) : a; n } n template <typename T> n class vector2 : public vector<vector<T>> { n  public: n   vector2() {} n   vector2(size_t a, size_t b, T t = T()) n       : vector<vector<T>>(a, vector<T>(b, t)) {} n }; n template <typename T> n class vector3 : public vector<vector2<T>> { n  public: n   vector3() {} n   vector3(size_t a, size_t b, size_t c, T t = T()) n       : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} n }; n template <typename T> n class vector4 : public vector<vector3<T>> { n  public: n   vector4() {} n   vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) n       : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} n }; n template <typename T> n class vector5 : public vector<vector4<T>> { n  public: n   vector5() {} n   vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) n       : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} n }; n int Prev[int(1e5) + 3]; n class EEgorAndAnRPGGame { n  public: n   void solve(istream& cin, ostream& cout) { n     int T; n     cin >> T; n     for (int t = 0; t < T; ++t) { n       int N; n       cin >> N; n       vector<int> A(N); n       cin >> A; n       vector<vector<int>> Ans; n       while (!A.empty()) { n         int M = A.size(); n         vector<vector<int>> B{{0}}; n         for (int i = 0; i < M; ++i) { n           int s = bsh(0, int(B.size() - 1), n                       [&](int first) { return B[first].back() < A[i]; }); n           if (s == B.size() - 1) B.emplace_back(); n           B[s + 1].push_back(A[i]); n           Prev[A[i]] = B[s].back(); n         } n         vector<int> Cur, Left; n         int idx = B.back().back(); n         while (idx != 0) { n           Cur.push_back(idx); n           idx = Prev[idx]; n         } n         reverse(Cur.begin(), Cur.end()); n         int j = 0; n         for (int i = 0; i < M; ++i) { n           if (j != Cur.size() && Cur[j] == A[i]) n             ++j; n           else n             Left.push_back(A[i]); n         } n         int K = Cur.size(); n         if (K * (K + 1) / 2 >= A.size()) { n           swap(A, Left); n           Ans.push_back(Cur); n         } else { n           for (auto& b : B) { n             if (b.front() == 0) continue; n             Ans.emplace_back(); n             for (auto& bb : b) { n               Ans.back().push_back(bb); n             } n           } n           break; n         } n       } n       cout << Ans.size() << endl; n       for (auto& ans : Ans) { n         cout << ans.size() <<     << ans; n       } n     } n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   EEgorAndAnRPGGame solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int c1, c2, c3, c4; n   cin >> c1 >> c2 >> c3 >> c4; n   int n, m; n   cin >> n >> m; n   int a[n], b[m]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) cin >> b[i]; n   int sb = 0, st = 0; n   for (int i = 0; i < n; i++) sb += min(a[i] * c1, c2); n   sb = min(sb, c3); n   for (int i = 0; i < m; i++) st += min(b[i] * c1, c2); n   st = min(st, c3); n   cout << min(st + sb, c4); n } n


#include <bits/stdc++.h> n using namespace std; n char ans[1000000]; n int main() { n   int n, c, k = 1, u = 0, w = 1, h = 0; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int m = 0; n   while (w) { n     u = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] == 0) u++; n     } n     if (u == n) break; n     c = 0; n     for (int i = h; i < n; i++) { n       if (a[i] != 0) { n         ans[m] =  P ; n         a[i]--; n         m++; n         u = 0; n         for (int i = 0; i < n; i++) { n           if (a[i] == 0) u++; n         } n         if (u == n) { n           break; n           w = 0; n         } n         if ((i + 1) != n) { n           ans[m] =  R ; n           m++; n         } n         u = 0; n         for (int i = 0; i < n; i++) { n           if (a[i] == 0) u++; n         } n         if (u == n) { n           break; n           w = 0; n         } n       } else if (a[i] == 0) { n         u = 0; n         for (int i = 0; i < n; i++) { n           if (a[i] == 0) u++; n         } n         if (u == n) { n           break; n           w = 0; n         } n         if ((i + 1) != n) { n           ans[m] =  R ; n           m++; n         } n       } n     } n     u = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] == 0) u++; n     } n     if (u == n) break; n     ans[m] =  L ; n     m++; n     for (int i = (n - 2); i >= 0; i--) { n       if (a[i] != 0) { n         ans[m] =  P ; n         a[i]--; n         m++; n         u = 0; n         for (int i = 0; i < n; i++) { n           if (a[i] == 0) u++; n         } n         if (u == n) { n           break; n           w = 0; n         } n         if ((i - 1) != -1) { n           ans[m] =  L ; n           m++; n         } n         u = 0; n         for (int i = 0; i < n; i++) { n           if (a[i] == 0) u++; n         } n         if (u == n) { n           break; n           w = 0; n         } n       } else if (a[i] == 0) { n         u = 0; n         for (int i = 0; i < n; i++) { n           if (a[i] == 0) u++; n         } n         if (u == n) { n           break; n           w = 0; n         } n         if ((i - 1) != -1) { n           ans[m] =  L ; n           m++; n         } n       } n       if (i == 0) { n         ans[m] =  R ; n         m++; n       } n     } n     u = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] == 0) u++; n     } n     if (u == n) break; n     h = 1; n   } n   for (int i = 0; i < m; i++) cout << ans[i]; n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int fact(int a) { n   int res = 1; n   while (a > 0) { n     res *= a--; n   } n   return res; n } n int main() { n   int a, b; n   cin >> a >> b; n   cout << fact(min(a, b)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace mine { n long long qread() { n   long long ans = 0, f = 1; n   char c = getchar(); n   while (c <  0  or c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while ( 0  <= c and c <=  9 ) ans = ans * 10 + c -  0 , c = getchar(); n   return ans * f; n } n void write(long long num) { n   if (num < 0) putchar( - ), num = -num; n   if (num >= 10) write(num / 10); n   putchar( 0  + num % 10); n } n void write1(long long num) { n   write(num); n   putchar(   ); n } n void write2(long long num) { n   write(num); n   putchar(  n ); n } n template <typename T> n void chmax(T &x, const T y) { n   x = (x > y ? x : y); n } n template <typename T> n void chmin(T &x, const T y) { n   x = (x < y ? x : y); n } n long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; } n const int INF = 0x3f3f3f3f; n const int MOD = 998244353; n inline int mm(const int x) { return x >= MOD ? x - MOD : x; } n template <typename T> n inline void add(T &x, const T y) { n   x = mm(x + y); n } n inline long long qpower(long long x, long long e, int mod = MOD) { n   long long ans = 1; n   if (e < 0) { n     puts( error ); n     exit(666); n   } n   while (e) { n     if (e & 1) ans = ans * x % mod; n     x = x * x % mod; n     e >>= 1; n   } n   return ans; n } n long long invm(long long x) { return qpower(x, MOD - 2); } n const int N = 2e5 + 10; n int n; n namespace SGT { n int mxc[N * 4]; n void change_cnt(int x, int l, int r, int pos, int num) { n   if (l == r) { n     mxc[x] = num; n     return; n   } n   if (pos <= (l + r) / 2) n     change_cnt(x << 1, l, (l + r) / 2, pos, num); n   else n     change_cnt(x << 1 | 1, (l + r) / 2 + 1, r, pos, num); n   mxc[x] = max(mxc[x << 1], mxc[x << 1 | 1]); n } n int ask_cnt(int x, int l, int r, int fl, int fr) { n   if (fl > fr) return 0; n   if (l == fl and r == fr) return mxc[x]; n   if (fr <= (l + r) / 2) return ask_cnt(x << 1, l, (l + r) / 2, fl, fr); n   if (fl > (l + r) / 2) return ask_cnt(x << 1 | 1, (l + r) / 2 + 1, r, fl, fr); n   return max(ask_cnt(x << 1, l, (l + r) / 2, fl, (l + r) / 2), n              ask_cnt(x << 1 | 1, (l + r) / 2 + 1, r, (l + r) / 2 + 1, fr)); n } n int mif[N * 4], tag[N * 4], mifl[N * 4], mifr[N * 4], sum[N * 4]; n void init(int x, int l, int r) { n   mifl[x] = l; n   mifr[x] = r; n   if (l < r) init(x << 1, l, (l + r) / 2), init(x << 1 | 1, (l + r) / 2 + 1, r); n } n void pushdown(int x) { n   if (tag[x] != 0) n     mif[x << 1] += tag[x], mif[x << 1 | 1] += tag[x], tag[x << 1] += tag[x], n         tag[x << 1 | 1] += tag[x], tag[x] = 0; n } n void pushup(int x, int l, int r) { n   if (l == r) { n     sum[x] = ask_cnt(x, l, r, l, l); n     return; n   } n   pushdown(x); n   if (mif[x << 1] == mif[x << 1 | 1]) { n     mif[x] = mif[x << 1]; n     mifl[x] = mifl[x << 1]; n     mifr[x] = mifr[x << 1 | 1]; n     sum[x] = sum[x << 1] + sum[x << 1 | 1] - n              ask_cnt(x, l, r, mifr[x << 1] + 1, (l + r) / 2) - n              ask_cnt(x, l, r, (l + r) / 2 + 1, mifl[x << 1 | 1]) + n              ask_cnt(x, l, r, mifr[x << 1] + 1, mifl[x << 1 | 1]); n   } else if (mif[x << 1] < mif[x << 1 | 1]) { n     mif[x] = mif[x << 1]; n     mifl[x] = mifl[x << 1]; n     mifr[x] = mifr[x << 1]; n     sum[x] = sum[x << 1] - ask_cnt(x, l, r, mifr[x << 1] + 1, (l + r) / 2) + n              ask_cnt(x, l, r, mifr[x << 1] + 1, r); n   } else { n     mif[x] = mif[x << 1 | 1]; n     mifl[x] = mifl[x << 1 | 1]; n     mifr[x] = mifr[x << 1 | 1]; n     sum[x] = sum[x << 1 | 1] - n              ask_cnt(x, l, r, (l + r) / 2 + 1, mifl[x << 1 | 1]) + n              ask_cnt(x, l, r, l, mifl[x << 1 | 1]); n   } n } n void add_f(int x, int l, int r, int fl, int fr, int val) { n   if (l == fl and r == fr) { n     mif[x] += val; n     tag[x] += val; n     pushup(x, l, r); n     return; n   } n   pushdown(x); n   if (fr <= (l + r) / 2) n     add_f(x << 1, l, (l + r) / 2, fl, fr, val); n   else if (fl > (l + r) / 2) n     add_f(x << 1 | 1, (l + r) / 2 + 1, r, fl, fr, val); n   else n     add_f(x << 1, l, (l + r) / 2, fl, (l + r) / 2, val), n         add_f(x << 1 | 1, (l + r) / 2 + 1, r, (l + r) / 2 + 1, fr, val); n   pushup(x, l, r); n } n void add_f(int l, int r, int val) { n   if (l <= r) add_f(1, 1, n, l, r, val); n } n void change_cnt(int pos, int num) { n   change_cnt(1, 1, n, pos, num); n   add_f(pos, pos, 0); n } n };  // namespace SGT n int a[N]; n set<int> pos[N]; n void main() { n   n = qread(); n   int q = qread(); n   SGT::init(1, 1, n); n   for (int i = (1), I = (n); i <= I; i++) a[i] = qread(), pos[a[i]].insert(i); n   for (int num = (1), I = (N - 1); num <= I; num++) n     if (((int)(pos[num]).size())) n       SGT::change_cnt(*pos[num].begin(), ((int)(pos[num]).size())), n           SGT::add_f(*pos[num].begin(), *(--pos[num].end()) - 1, 1); n   write2(n - SGT::sum[1]); n   while (q--) { n     int x = qread(), val = qread(); n     set<int>::iterator now = pos[a[x]].find(x); n     SGT::change_cnt(*pos[a[x]].begin(), 0); n     if (((int)(pos[a[x]]).size()) > 1) { n       if (now == pos[a[x]].begin()) n         SGT::add_f(x, *(++now) - 1, -1); n       else if (now == --pos[a[x]].end()) n         SGT::add_f(*(--now), x - 1, -1); n     } n     pos[a[x]].erase(x); n     if (((int)(pos[a[x]]).size())) n       SGT::change_cnt(*pos[a[x]].begin(), ((int)(pos[a[x]]).size())); n     a[x] = val; n     pos[a[x]].insert(x); n     SGT::change_cnt(*pos[a[x]].begin(), ((int)(pos[a[x]]).size())); n     now = pos[a[x]].find(x); n     if (((int)(pos[a[x]]).size()) > 1) { n       if (now == pos[a[x]].begin()) { n         now++; n         SGT::add_f(x, *now - 1, 1); n         SGT::change_cnt(*now, 0); n       } else if (now == --pos[a[x]].end()) n         SGT::add_f(*(--now), x - 1, 1); n     } n     write2(n - SGT::sum[1]); n   } n } n };  // namespace mine n signed main() { n   srand(time(0)); n   mine::main(); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 105; n vector<int> adj[MAX]; n string s[MAX]; n int mark[MAX]; n bool cycle; n string ans =   ; n void dfs(int v) { n   mark[v] = 1; n   for (int i = 0; i < adj[v].size(); i++) { n     int u = adj[v][i]; n     if (mark[u] == 1) cycle = true; n     if (!mark[u]) dfs(u); n   } n   mark[v] = 2; n } n void print(int v) { n   mark[v] = 1; n   for (int i = 0; i < adj[v].size(); i++) { n     int u = adj[v][i]; n     if (!mark[u]) print(u); n   } n   ans +=  a  + v; n } n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> s[i]; n   for (int i = 0; i < n - 1; i++) { n     int j = 0; n     while (j < s[i].size() && j < s[i + 1].size() && s[i][j] == s[i + 1][j]) n       j++; n     if (j == s[i + 1].size() && s[i].size() > s[i + 1].size()) { n       cout <<  Impossible  << endl; n       return 0; n     } n     if (j < s[i].size() && j < s[i + 1].size()) n       adj[s[i][j] -  a ].push_back(s[i + 1][j] -  a ); n   } n   for (int i = 0; i < 26; i++) n     if (!mark[i]) dfs(i); n   if (cycle) { n     cout <<  Impossible  << endl; n     return 0; n   } n   memset(mark, 0, sizeof(mark)); n   for (int i = 0; i < 26; i++) n     if (!mark[i]) print(i); n   reverse(ans.begin(), ans.end()); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n ostream& operator<<(ostream& out, const tuple<T>& a) { n   out <<  [  << get<0>(a) <<  ] ; n   return out; n } n template <class T, class U> n ostream& operator<<(ostream& out, const tuple<T, U>& a) { n   out <<  [  << get<0>(a) <<     << get<1>(a) <<  ] ; n   return out; n } n template <class T, class U, class V> n ostream& operator<<(ostream& out, const tuple<T, U, V>& a) { n   out <<  [  << get<0>(a) <<     << get<1>(a) <<     << get<2>(a) <<  ] ; n   return out; n } n template <class T, class U, class V, class W> n ostream& operator<<(ostream& out, const tuple<T, U, V, W>& a) { n   out <<  [  << get<0>(a) <<     << get<1>(a) <<     << get<2>(a) <<     n       << get<3>(a) <<  ] ; n   return out; n } n template <class T, class U> n ostream& operator<<(ostream& out, const pair<T, U>& a) { n   out <<  [  << a.first <<     << a.second <<  ] ; n   return out; n } n template <class T> n ostream& operator<<(ostream& out, const vector<T>& a) { n   out <<  [  ; n   for (auto& it : a) out << it <<    ; n   out <<  ] ; n   return out; n } n template <class T> n ostream& operator<<(ostream& out, const set<T>& a) { n   out <<  [  ; n   for (auto& it : a) out << it <<    ; n   out <<  ] ; n   return out; n } n template <class T> n ostream& operator<<(ostream& out, const multiset<T>& a) { n   out <<  [  ; n   for (auto& it : a) out << it <<    ; n   out <<  ] ; n   return out; n } n template <class T, class U> n ostream& operator<<(ostream& out, const map<T, U>& a) { n   for (auto& it : a) out << it.first <<   ->   << it.second <<   |  ; n   return out; n } n template <class T, class U> n ostream& operator<<(ostream& out, const multimap<T, U>& a) { n   for (auto& it : a) out << it.first <<   ->   << it.second <<   |  ; n   return out; n } n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n void r(){}; n template <typename T, typename... Args> n void r(T& a, Args&... args) { n   cin >> a; n   r(args...); n } n void p() { cout <<   n ; }; n template <typename T, typename... Args> n void p(T& a, Args&... args) { n   cout << a <<    ; n   p(args...); n } n template <class T> n T gcd(T a, T b) { n   return a ? gcd(b % a, a) : b; n } n template <class T> n T lcm(T a, T b) { n   return ((a * b) / gcd(b % a, a)); n } n template <class T1, class T2> n T1 binpow(T1 number, T2 power) { n   T1 result = number; n   while (power) { n     if (power & 1) result = result * number; n     number = number * number; n     power /= 2; n   } n   return result; n } n long long n; n class Segtree { n  public: n   long long *tree, *lazy, *array, *color, *is_single; n   Segtree(long long* arr, long long sz) { n     array = new long long[sz]; n     lazy = new long long[4 * sz]; n     tree = new long long[4 * sz]; n     color = new long long[4 * sz]; n     is_single = new long long[4 * sz]; n     n = sz; n     for (auto i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); n          i += 1 - 2 * ((0) > (n))) n       array[i] = arr[i]; n     for (auto i = (0) - ((0) > (4 * sz)); i != (4 * sz) - ((0) > (4 * sz)); n          i += 1 - 2 * ((0) > (4 * sz))) n       tree[i] = 0; n     for (auto i = (0) - ((0) > (4 * sz)); i != (4 * sz) - ((0) > (4 * sz)); n          i += 1 - 2 * ((0) > (4 * sz))) n       lazy[i] = 0; n     for (auto i = (0) - ((0) > (4 * sz)); i != (4 * sz) - ((0) > (4 * sz)); n          i += 1 - 2 * ((0) > (4 * sz))) n       color[i] = 0; n     for (auto i = (0) - ((0) > (4 * sz)); i != (4 * sz) - ((0) > (4 * sz)); n          i += 1 - 2 * ((0) > (4 * sz))) n       is_single[i] = 0; n     build(); n   } n   void build(long long id = 1, long long l = 0, long long r = n) { n     if (r - l < 2) { n       tree[id] = 0; n       color[id] = array[l]; n       is_single[id] = 1; n       return; n     } n     auto mid = (l + r) / 2; n     build(2 * id, l, mid); n     build(2 * id + 1, mid, r); n     tree[id] = tree[2 * id] + tree[2 * id + 1]; n     if (color[2 * id] == color[2 * id + 1] and color[2 * id] != 0) n       is_single[id] = 1, color[id] = color[2 * id]; n     else n       is_single[id] = 0, color[id] = 0; n   } n   void upd(long long id, long long l, long long r, long long x, n            long long coloring) { n     lazy[id] += x; n     tree[id] += (r - l) * x; n     if (coloring != 0) color[id] = coloring; n   } n   void shift(long long id, long long l, long long r) { n     long long mid = (l + r) / 2; n     upd(id * 2, l, mid, lazy[id], color[id]); n     upd(id * 2 + 1, mid, r, lazy[id], color[id]); n     lazy[id] = 0; n   } n   long long sum(long long x, long long y, long long id = 1, long long l = 0, n                 long long r = n) { n     if (l >= y or r <= x) return 0; n     if (l >= x and r <= y) return tree[id]; n     shift(id, l, r); n     auto mid = (l + r) / 2; n     return sum(x, y, 2 * id, l, mid) + sum(x, y, 2 * id + 1, mid, r); n   } n   void range_update(long long x, long long y, long long coloring, n                     long long id = 1, long long l = 0, long long r = n) { n     auto mid = (l + r) / 2; n     if (l >= y or r <= x) return; n     if (x <= l and r <= y) { n       if (is_single[id]) { n         upd(id, l, r, abs(color[id] - coloring), coloring); n       } else { n         shift(id, l, r); n         range_update(x, y, coloring, 2 * id, l, mid); n         range_update(x, y, coloring, 2 * id + 1, mid, r); n         tree[id] = tree[2 * id] + tree[2 * id + 1]; n       } n       is_single[id] = 1; n       color[id] = coloring; n       return; n     } n     shift(id, l, r); n     range_update(x, y, coloring, 2 * id, l, mid); n     range_update(x, y, coloring, 2 * id + 1, mid, r); n     tree[id] = tree[2 * id] + tree[2 * id + 1]; n     if (color[2 * id] == color[2 * id + 1] and color[2 * id] != 0) n       is_single[id] = 1, color[id] = color[2 * id]; n     else n       is_single[id] = 0, color[id] = 0; n   } n }; n long long m, arr[112345], types, lef, ri, x; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   r(n, m); n   for (auto i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); n        i += 1 - 2 * ((0) > (n))) n     arr[i] = i + 1; n   Segtree tree(arr, n); n   for (auto i = (0) - ((0) > (m)); i != (m) - ((0) > (m)); n        i += 1 - 2 * ((0) > (m))) { n     r(types); n     if (types == 1) { n       r(lef, ri, x); n       tree.range_update(lef - 1, ri, x); n     } else { n       r(lef, ri); n       auto sum = tree.sum(lef - 1, ri); n       p(sum); n     } n   } 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   if (fopen( main.in ,  r )) n     freopen( main.in ,  r , stdin); n   else if (fopen( something  n                   .inp , n                   r )) n     freopen( n          something  n          .inp , n          r , stdin), n         freopen( n              something  n              .out , n              w , stdout); n   int n, m; n   cin >> n >> m; n   int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0; n   for (int i = 1; i <= n; i++) { n     int a; n     cin >> a; n     cnt1 += (a % 2 == 1); n     cnt2 += (a % 2 == 0); n   } n   for (int i = 1; i <= m; i++) { n     int a; n     cin >> a; n     cnt3 += (a % 2 == 1); n     cnt4 += (a % 2 == 0); n   } n   cout << min(cnt1, cnt4) + min(cnt2, cnt3) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d[1234678], a[1234678], ans; n int main() { n   int n, cnt = 0; n   char c; n   scanf( %d n , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %c , &c); n     d[i] = (c ==  R ); n   } n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   ans = 1047483647; n   for (int i = 2; i <= n; i++) n     if (d[i - 1] == 1 && d[i] == 0) ans = min(ans, (a[i] - a[i - 1]) / 2); n   if (ans == 1047483647) n     puts( -1 ); n   else n     printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char a; n   int b; n   scanf( %c%d , &a, &b); n   if ((a ==  a  || a ==  h ) && (b == 1 || b == 8)) n     printf( 3 n ); n   else if (a ==  a  || a ==  h  || b == 1 || b == 8) n     printf( 5 n ); n   else n     printf( 8 n ); n } n


#include <bits/stdc++.h> n using namespace std; n const long long NMAX = 1000001; n vector<int> v[NMAX]; n int tobehappy[NMAX]; n void DFS(int node) { n   if (v[node].size() == 0) tobehappy[node] = 1; n   for (auto x : v[node]) { n     DFS(x); n     tobehappy[node] += tobehappy[x]; n   } n } n int main() { n   int n, i; n   cin >> n; n   for (i = 2; i <= n; i++) { n     int x, y; n     cin >> y; n     v[y].push_back(i); n   } n   DFS(1); n   sort(tobehappy + 1, tobehappy + n + 1); n   for (i = 1; i <= n; i++) cout << tobehappy[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e6 + 7; n int n, u, v, cnt, head[maxn], to[maxn << 1], nex[maxn << 1], deep[maxn], n     len[maxn], son[maxn], *f[maxn], tmp[maxn], *id = tmp, ans[maxn]; n inline int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = x * 10 + ch - 48; n     ch = getchar(); n   } n   return x * f; n } n inline void add(int u, int v) { n   to[++cnt] = v; n   nex[cnt] = head[u]; n   head[u] = cnt; n } n void dfs1(int x, int pre, int deep) { n   int maxson = 0; n   ::deep[x] = deep; n   for (int i = head[x]; i; i = nex[i]) { n     if (to[i] == pre) continue; n     dfs1(to[i], x, deep + 1); n     if (len[to[i]] > maxson) son[x] = to[i], maxson = len[to[i]]; n   } n   len[x] = maxson + 1; n } n void dfs2(int x, int pre) { n   f[x][0] = 1; n   if (son[x]) { n     f[son[x]] = f[x] + 1; n     dfs2(son[x], x); n     ans[x] = ans[son[x]] + 1; n   } n   for (int i = head[x]; i; i = nex[i]) { n     int v = to[i]; n     if (v == pre || v == son[x]) continue; n     f[v] = id, id += len[v], dfs2(v, x); n     for (int j = 1; j <= len[v]; ++j) { n       f[x][j] += f[v][j - 1]; n       if (f[x][j] > f[x][ans[x]] || (f[x][j] == f[x][ans[x]] && j < ans[x])) n         ans[x] = j; n     } n   } n   if (f[x][ans[x]] == 1) ans[x] = 0; n } n int main() { n   n = read(); n   for (int i = 1; i < n; ++i) { n     u = read(), v = read(); n     add(u, v); n     add(v, u); n   } n   dfs1(1, 0, 1); n   f[1] = id, id += len[1], dfs2(1, 0); n   for (int i = 1; i <= n; ++i) printf( %d n , ans[i]); n } n


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