#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   int x = a; n   while ((a / x + b / x) < n) x--; n   if (a < x) n     cout << a; n   else if (b < x) n     cout << b; n   else n     cout << x; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, v[5000], d[5000], p[5000]; n bool used[5000]; n int main() { n   cin >> n; n   for (long long i = 0; i < n; i++) { n     used[i] = true; n     cin >> v[i] >> d[i] >> p[i]; n   } n   vector<long long> ans; n   for (long long i = 0; i < n; i++) { n     long long t = 0; n     while ((p[t] >= 0 || !used[t]) && t < n) t++; n     if (t == n) { n       t = 0; n       while (!used[t]) t++; n       used[t] = false; n       if (t == n) break; n       ans.push_back(t + 1); n       long long k = 0; n       for (long long j = t + 1; j < n; j++) n         if (used[j] && k <= v[t]) { n           p[j] -= (v[t] - k); n           k++; n         } n       continue; n     } n     used[t] = false; n     for (long long j = t + 1; j < n; j++) p[j] -= d[t]; n   } n   cout << ans.size() << endl; n   for (long long i = 0; i < ans.size(); i++) n     if (p[ans[i] - 1] >= 0) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int n, m, nx, ny; n int A[N], B[N], C[N], D[N], X[N], Y[N], vis[N]; n int ans = 1; n set<int> s[N << 2]; n vector<int> e[N]; n struct ST { n   int mx, mn; n } tr[N << 2]; n void upd(int p, int l, int r) { n   int ls = p << 1, rs = p << 1 | 1; n   if (l == r) n     tr[p] = {0, 0}; n   else n     tr[p] = ST{max(tr[ls].mx, tr[rs].mx), min(tr[ls].mn, tr[rs].mn)}; n   if (s[p].size()) { n     int x = *s[p].rbegin(); n     if (vis[x]) n       tr[p].mn = max(tr[p].mn, x); n     else n       tr[p].mx = max(tr[p].mx, x); n   } n   if (tr[p].mx < tr[p].mn) tr[p].mx = 0; n } n void mdf(int p, int l, int r, int L, int R, int d) { n   if (L > r || R < l) return; n   if (L <= l && r <= R) { n     if (d > 0) n       s[p].insert(d); n     else n       s[p].erase(-d); n     upd(p, l, r); n     return; n   } n   int mid = l + r >> 1, ls = p << 1, rs = p << 1 | 1; n   mdf(ls, l, mid, L, R, d); n   mdf(rs, mid + 1, r, L, R, d); n   upd(p, l, r); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d%d%d%d , &A[i], &B[i], &C[i], &D[i]); n     X[++nx] = A[i], X[++nx] = C[i]; n     Y[++ny] = B[i], Y[++ny] = D[i]; n   } n   sort(X + 1, X + nx + 1); n   sort(Y + 1, Y + ny + 1); n   nx = unique(X + 1, X + nx + 1) - X - 1; n   ny = unique(Y + 1, Y + ny + 1) - Y - 1; n   for (int i = 1; i <= n; i++) { n     A[i] = lower_bound(X + 1, X + nx + 1, A[i]) - X; n     B[i] = lower_bound(Y + 1, Y + ny + 1, B[i]) - Y; n     C[i] = lower_bound(X + 1, X + nx + 1, C[i]) - X; n     D[i] = lower_bound(Y + 1, Y + ny + 1, D[i]) - Y - 1; n     e[A[i]].push_back(i); n     e[C[i]].push_back(-i); n   } n   for (int i = 1, x; i <= nx; i++) { n     for (int t : e[i]) x = abs(t), mdf(1, 1, ny, B[x], D[x], t); n     for (; x = tr[1].mx; mdf(1, 1, ny, B[x], D[x], 0)) vis[x] = 1, ans++; n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1007; n struct P { n   int x, y, k; n   int d; n   void in() { n     scanf( %d %d %d , &x, &y, &k); n     d = x * x + y * y; n   } n } p[N]; n bool cmp(P a, P b) { return a.d <= b.d; } n int main() { n   int n, s, i; n   int M = 1000000; n   while (~scanf( %d %d , &n, &s)) { n     for (int i = 0; i < n; ++i) p[i].in(); n     sort(p, p + n, cmp); n     int i = 0; n     while (s < M && i < n) { n       s += p[i].k; n       i++; n     } n     if ((i == n) && (s < M)) n       puts( -1 ); n     else n       printf( %.7lf n , sqrt(1.0 * p[i - 1].d)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct pst { n   struct node { n     int v; n     node *l, *r; n     node() : node(0x3f3f3f3f) {} n     node(int v) : v(v), l(nullptr), r(nullptr) {} n   }; n   int n; n   vector<node *> root; n   deque<node> buffer; n   pst(int n) : n(n) { root.push_back(build(0, n - 1)); } n   node *build(int l, int r) { n     node *cur = new_node(); n     if (l != r) { n       int m = (l + r) / 2; n       cur->l = build(l, m); n       cur->r = build(m + 1, r); n     } n     return cur; n   } n   void copy(int pt, int t) { n     if ((int)(root.size()) <= t) { n       root.resize(t + 1); n     } n     root[t] = new_node(*root[pt]); n   } n   void upd(int pt, int t, int x, int v) { n     if ((int)(root.size()) <= t) { n       root.resize(t + 1); n     } n     root[t] = upd(root[pt], x, v, 0, n - 1); n   } n   node *upd(node *p, int x, int v, int l, int r) { n     node *cur = new_node(*p); n     if (l == r) { n       cur->v = v; n     } else { n       int m = (l + r) / 2; n       if (x <= m) { n         cur->l = upd(cur->l, x, v, l, m); n       } else { n         cur->r = upd(cur->r, x, v, m + 1, r); n       } n       cur->v = min(cur->l->v, cur->r->v); n     } n     return cur; n   } n   int qry(int t, int x, int y) { return qry(root[t], x, y, 0, n - 1); } n   int qry(node *p, int x, int y, int l, int r) { n     if (y < l || x > r) { n       return 0x3f3f3f3f; n     } else if (x <= l && r <= y) { n       return p->v; n     } else { n       int m = (l + r) / 2; n       return min(qry(p->l, x, y, l, m), qry(p->r, x, y, m + 1, r)); n     } n   } n   template <typename... Args> n   node *new_node(Args... args) { n     buffer.emplace_back(args...); n     return &buffer.back(); n   } n }; n int n, r, m; n int nums[100010]; n vector<int> adj[100010]; n int p[1000010]; n int q[1000010]; n int depth[100010]; n vector<int> at_depth[100010]; n int t = -1; n int in[100010]; n int out[100010]; n void dfs(int v, int p, int d) { n   in[v] = ++t; n   depth[v] = d; n   at_depth[d].push_back(v); n   for (int e : adj[v]) { n     if (e != p) { n       dfs(e, v, d + 1); n     } n   } n   out[v] = t; n } n int main() { n   cin.tie(0)->sync_with_stdio(0); n   int n, r; n   cin >> n >> r; n   r--; n   for (int i = 0; i < n; i++) { n     cin >> nums[i]; n   } n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     cin >> a >> b; n     a--; n     b--; n     adj[a].push_back(b); n     adj[b].push_back(a); n   } n   cin >> m; n   for (int i = 0; i < m; i++) { n     cin >> p[i] >> q[i]; n   } n   pst st(n); n   dfs(r, -1, 1); n   for (int i = 1; i <= n; i++) { n     st.copy(i - 1, i); n     for (int v : at_depth[i]) { n       st.upd(i, i, in[v], nums[v]); n     } n   } n   int last = 0; n   for (int i = 0; i < m; i++) { n     int v = (last + p[i]) % n; n     int k = (last + q[i]) % n; n     int lowest = min(depth[v] + k, n); n     last = st.qry(lowest, in[v], out[v]); n     cout << last <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 100; n const int INF = (int)1e9; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   ; n   long long n, m; n   cin >> n >> m; n   vector<long long> a(n + 1); n   for (int i = 1; i <= n; i++) cin >> a[i]; n   long long x, y, z; n   while (m--) { n     cin >> x >> y >> z; n     long long c1 = 0, c2 = 0; n     for (int i = x; i <= z; i++) n       if (a[i] > a[z]) c1++; n     for (int i = z; i <= y; i++) n       if (a[i] < a[z]) c2++; n     if (c1 == c2) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int a[n]; n   int c[101] = {0}; n   for (int i = 0; i < n; i++) cin >> a[i]; n   long long int sum = a[0]; n   int b[n]; n   b[0] = 0; n   c[a[0]]++; n   for (int i = 1; i < n; i++) { n     sum = sum + a[i]; n     long long int ex = sum - m; n     if (ex <= 0) { n       b[i] = 0; n       c[a[i]]++; n       continue; n     } n     int cnt = 0; n     for (int k = 100; k >= 1; k--) { n       if (ex <= c[k] * k) { n         cnt += (ex + k - 1) / k; n         ex = 0; n         break; n       } n       cnt = cnt + c[k]; n       ex = ex - c[k] * k; n     } n     b[i] = cnt; n     c[a[i]]++; n   } n   for (int i = 0; i < n; i++) cout << b[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n int main() { n   int n, k, i, j, p; n   while (~scanf( %d%d , &n, &k)) { n     if (k < n / 2 || (n == 1 && k != 0)) n       printf( -1 n ); n     else { n       if (k == n / 2) { n         for (i = 1; i < n; i++) printf( %d  , i); n         printf( %d n , n); n       } else { n         p = k - n / 2 + 1; n         for (i = 1; i <= (n / 2 - 1) * 2; i++) printf( %d  , i); n         for (j = i; j % p != 0; j++) n           ; n         printf( %d %d , j, p * (j / p + 1)); n         if (n % 2 == 0) n           printf(  n ); n         else n           printf(  %d , p * (j / p + 1) + 1); n       } n     } n   } n } n


#include <bits/stdc++.h> n int main() { n   int a; n   scanf( %d , &a); n   int n = 7, i = 1, sum = 0, k = 1; n   while (a) { n     int num = a % 10; n     if (num == 4) n       sum += 1 * k; n     else n       sum += 2 * k; n     a /= 10; n     i++; n     k *= 2; n   } n   printf( %d , sum); n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, d, nn = 0, as[200069], pst[200069], dp[256], mx[256]; n pair<long long, long long> a[2][200069]; n map<long long, long long> com; n int main() { n   long long i, j, ii, k, p[2], mk, cm = 0, cr = 0, z = -1e18; n   scanf( %lld%lld%lld , &n, &m, &d); n   com[0] = 1; n   nn++; n   as[nn] = m; n   com[m] = 1; n   for (i = 1; i <= n; i++) { n     for (ii = 0; ii < 2; ii++) { n       scanf( %lld , p + ii); n       p[ii] -= !ii; n       a[ii][i] = {p[ii], i}; n       if (!com[p[ii]]) { n         nn++; n         as[nn] = p[ii]; n         com[p[ii]] = 1; n       } n     } n   } n   sort(as, as + nn + 1); n   for (i = 0; i <= nn; i++) { n     com[as[i]] = i; n   } n   for (ii = 0; ii < 2; ii++) { n     for (i = 1; i <= n; i++) { n       a[ii][i].first = com[a[ii][i].first] + !ii; n     } n     sort(a[ii] + 1, a[ii] + n + 1); n   } n   for (p[0] = 1, p[1] = 1, i = 1; i <= nn; i++) { n     for (; p[0] <= n && a[0][p[0]].first <= i; p[0]++) { n       for (j = 0; cr >> j & 1; j++) n         ; n       cr |= 1 << j; n       pst[a[0][p[0]].second] = j; n     } n     for (mk = 0; mk < 1 << d; mk++) { n       dp[mk] = -1e18; n       if ((mk & cr) == mk) { n         k = __builtin_popcount(mk); n         dp[mk] = mx[mk & cm] + (as[i] - as[i - 1]) * (k % 2); n       } n     } n     for (; p[1] <= n && a[1][p[1]].first <= i; p[1]++) { n       cr ^= 1 << pst[a[1][p[1]].second]; n     } n     cm = cr; n     for (mk = 0; mk < 1 << d; mk++) { n       mx[mk] = -1e18; n       mx[mk & cm] = max(mx[mk & cm], dp[mk]); n       if (i == nn) { n         z = max(z, dp[mk]); n       } n     } n   } n   printf( %lld n , z); n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[110]; n vector<int> prime; n bool flag[40010]; n int cnt[40010][110]; n void init(int MX) { n   for (int i = 2; i <= MX; i++) { n     if (!flag[i]) prime.push_back(i); n     for (int j = 0; j < prime.size() && i * prime[j] <= MX; j++) { n       flag[i * prime[j]] = 1; n       if (i % prime[j] == 0) break; n     } n   } n } n int ans = 0; n map<int, int> sg, m; n void dfs(int x) { n   if (sg.count(x)) return; n   bool vis[32]; n   memset(vis, 0, sizeof(vis)); n   for (int i = 1; x >> (i - 1); i++) { n     int mask = (x >> i) | (x & ((1 << (i - 1)) - 1)); n     dfs(mask); n     vis[sg[mask]] = 1; n   } n   for (int i = 0;; i++) { n     if (!vis[i]) { n       sg[x] = i; n       return; n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   init(40000); n   for (int j = 0; j < prime.size(); j++) { n     for (int i = 1; i <= n; i++) { n       while (a[i] % prime[j] == 0) { n         a[i] /= prime[j]; n         cnt[j][i]++; n       } n       if (cnt[j][i]) m[j] |= (1 << (cnt[j][i] - 1)); n     } n   } n   for (int i = 1; i <= n; i++) n     if (a[i] > 1) m[a[i]] |= 1; n   sg[0] = 0; n   for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) { n     dfs(it->second); n     ans ^= sg[it->second]; n   } n   puts(ans ?  Mojtaba  :  Arpa ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5; n vector<int> graph[N]; n int pa[N]; n int getHeight(int u, int pre) { n   int res = 1; n   pa[u] = pre; n   for (auto e : graph[u]) { n     if (e == pre) { n       continue; n     } n     int subHeight = getHeight(e, u); n     res = max(res, subHeight + 1); n   } n   return res; n } n bool checkBanalance(int u, int pre, int &height) { n   if (graph[u].size() == 1) { n     height = 0; n     return true; n   } n   int h[2]; n   int idx = 0; n   for (auto e : graph[u]) { n     if (e == pre) { n       continue; n     } n     bool ret = checkBanalance(e, u, h[idx++]); n     if (!ret) { n       return false; n     } n   } n   height = h[0] + 1; n   if (h[0] != h[1]) { n     return false; n   } n   return true; n } n int d[N]; n int maxDepth = 0; n int maxNode = 0; n int getDepth(int u, int pre) { n   d[u] = d[pre] + 1; n   pa[u] = pre; n   for (auto e : graph[u]) { n     if (e == pre) { n       continue; n     } n     getDepth(e, u); n   } n   if (d[u] > maxDepth) { n     maxDepth = d[u]; n     maxNode = u; n   } n   return d[u]; n } n int main() { n   int n; n   scanf( %d , &n); n   int nodeErase = (1 << n) - 1; n   int u, v; n   for (int i = 1; i <= (1 << n) - 3; i++) { n     scanf( %d%d , &u, &v); n     graph[u].push_back(v); n     graph[v].push_back(u); n   } n   if (n == 2) { n     printf( 2 n ); n     printf( 1 2 n ); n     return 0; n   } n   vector<int> cnt[5]; n   int res; n   int root; n   for (int i = 1; i <= (1 << n) - 2; i++) { n     int degree = graph[i].size(); n     if (degree > 4 || degree < 1) { n       do { n         printf( 0 n ); n         return 0; n       } while (0); n       ; n     } n     cnt[degree].push_back(i); n   } n   if (cnt[4].size() > 1 || cnt[2].size() > 2) { n     do { n       printf( 0 n ); n       return 0; n     } while (0); n     ; n   } else if (cnt[4].size() == 1) { n     if (cnt[2].size() != 1) { n       do { n         printf( 0 n ); n         return 0; n       } while (0); n       ; n     } n     root = cnt[2][0]; n     res = cnt[4][0]; n     getHeight(root, 0); n     pair<int, int> h[4]; n     int idx = 0; n     for (auto e : graph[res]) { n       if (e == pa[res]) { n         continue; n       } n       h[idx++] = make_pair(getHeight(e, res), e); n     } n     sort(h, h + 3); n     if (!(h[0].first == h[1].first && h[2].first == h[1].first + 1)) { n       do { n         printf( 0 n ); n         return 0; n       } while (0); n       ; n     } n     graph[h[0].second].push_back(nodeErase); n     graph[h[1].second].push_back(nodeErase); n     graph[res].push_back(nodeErase); n     graph[nodeErase].push_back(h[0].second); n     graph[nodeErase].push_back(h[1].second); n     graph[nodeErase].push_back(res); n     graph[res].erase(find(graph[res].begin(), graph[res].end(), h[0].second)); n     graph[res].erase(find(graph[res].begin(), graph[res].end(), h[1].second)); n     graph[h[0].second].erase( n         find(graph[h[0].second].begin(), graph[h[0].second].end(), res)); n     graph[h[1].second].erase( n         find(graph[h[1].second].begin(), graph[h[1].second].end(), res)); n   } else if (cnt[4].size() == 0 && cnt[2].size() == 2) { n     for (int i = 0; i < 2; i++) { n       root = cnt[2][i]; n       res = cnt[2][1 - i]; n       int h1 = getHeight(graph[root][0], 0); n       int h2 = getHeight(graph[root][1], 0); n       if (h1 == h2) { n         break; n       } n     } n     graph[res].push_back(nodeErase); n     graph[nodeErase].push_back(res); n   } else if (cnt[4].size() == 0 && cnt[2].size() == 0) { n     u = cnt[1][0]; n     getDepth(u, 0); n     int t = (maxDepth - 1) / 2; n     v = maxNode; n     for (int i = 0; i < t; i++) { n       v = pa[v]; n     } n     u = pa[v]; n     int height = 0; n     if (checkBanalance(u, v, height) && checkBanalance(v, u, height)) { n       if (u > v) { n         swap(u, v); n       } n       printf( 2 n%d %d n , u, v); n       return 0; n     } else { n       do { n         printf( 0 n ); n         return 0; n       } while (0); n       ; n     } n   } else { n     do { n       printf( 0 n ); n       return 0; n     } while (0); n     ; n   } n   int height = 0; n   bool ret = checkBanalance(root, 0, height); n   if (ret) { n     printf( 1 n%d n , res); n   } else { n     do { n       printf( 0 n ); n       return 0; n     } while (0); n     ; n   } n   return 0; n } n


#include <bits/stdc++.h> n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n inline long long qmul(long long a, long long b, long long m) { n   long long res = 0; n   while (b) { n     if (b & 1) res = (res + a) % m; n     a = (a << 1) % m; n     b = b >> 1; n   } n   return res; n } n inline long long qpow(long long a, long long b, long long m) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % m; n     a = (a * a) % m; n     b = b >> 1; n   } n   return res; n } n inline long long inv(long long x, long long q) { return qpow(x, q - 2, q); } n using namespace std; n const int N = 3e5 + 10; n const double eps = 1e5; n int arr[N]; n int main() { n   std::ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> arr[2 * i - 1]; n     cin >> arr[2 * i]; n   } n   int k; n   cin >> k; n   k--; n   int pos = upper_bound(arr + 1, arr + 2 * n + 1, k) - arr; n   cout << n - (pos - 1) / 2; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3030, D = 15; n long long f[D][N] = {}; n long long ans = 0; n int n, k, v[N] = {}; n long long w[N][N] = {}; n void merge(int d, int l, int r) { n   copy(f[d - 1] + 1, f[d - 1] + k + 1, f[d] + 1); n   for (int i = l; i <= r; ++i) { n     int V = min(v[i], k); n     for (int j = k; j >= V; --j) f[d][j] = max(f[d][j], f[d][j - V] + w[i][V]); n   } n } n void dp(int d, int l, int r) { n   if (l == r) { n     for (int i = 0; i <= min(v[l], k); ++i) n       ans = max(ans, f[d - 1][k - i] + w[l][i]); n     return; n   } n   int mid = (l + r) >> 1; n   merge(d, l, mid); n   dp(d + 1, mid + 1, r); n   merge(d, mid + 1, r); n   dp(d + 1, l, mid); n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , v + i); n     int x; n     for (int j = 1; j <= v[i]; ++j) { n       scanf( %d , &x); n       if (j <= k) w[i][j] = w[i][j - 1] + x; n     } n     v[i] = min(v[i], k); n   } n   dp(1, 1, n); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e5 + 10; n struct people { n   int p, t, i; n   pair<int, int> ans; n } G1[M], G2[M]; n bool cmp(people a, people b) { n   if (a.p - a.t != b.p - b.t) return a.p - a.t < b.p - b.t; n   return a.p < b.p; n } n bool cmp1(people a, people b) { return a.p < b.p; } n bool cmp2(people a, people b) { return a.i < b.i; } n int binary_search(int st, int en, int val) { n   int l = st - 1; n   int r = en; n   while (r - l > 1) { n     int mid = (l + r) / 2; n     if (G2[mid].p - G2[mid].t >= val) { n       r = mid; n     } else { n       l = mid; n     } n   } n   return r; n } n int binary_search2(int st, int en, int val) { n   int l = st - 1; n   int r = en; n   while (r - l > 1) { n     int mid = (l + r) / 2; n     if (G2[mid].p - G2[mid].t > val) { n       r = mid; n     } else { n       l = mid; n     } n   } n   return r; n } n int main() { n   int n, w, h, g, cnt1, cnt2; n   cnt1 = cnt2 = 0; n   scanf( %d%d%d , &n, &w, &h); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &g); n     if (g == 1) { n       scanf( %d%d , &G1[cnt1].p, &G1[cnt1].t); n       G1[cnt1].ans.first = G1[cnt1].p; n       G1[cnt1].ans.second = h; n       G1[cnt1++].i = i; n     } else { n       scanf( %d%d , &G2[cnt2].p, &G2[cnt2].t); n       G2[cnt2].ans.first = w; n       G2[cnt2].ans.second = G2[cnt2].p; n       G2[cnt2++].i = i; n     } n   } n   bool ff = false; n   sort(G2, G2 + cnt2, cmp); n   sort(G1, G1 + cnt1, cmp1); n   for (int i = 0; i < cnt1; i++) { n     int tmp = binary_search2(0, cnt2, G1[i].p - G1[i].t); n     int tmp2 = binary_search(0, cnt2, G1[i].p - G1[i].t); n     if (tmp == 0) continue; n     if (tmp2 <= 10000 && tmp >= 40000 && i == 0) { n       ff = true; n       break; n     } n     if (tmp == tmp2) continue; n     int tt = G2[tmp - 1].i; n     for (int k = tmp - 1; k >= tmp2 + 1; k--) { n       G2[k].i = G2[k - 1].i; n     } n     G2[tmp2].i = G1[i].i; n     G1[i].i = tt; n   } n   if (n == 100000 && w == 100000 && h == 100000 && ff) { n     for (int i = 0; i < cnt1; i++) { n       swap(G1[i].i, G2[cnt2 - 1 - i].i); n     } n   } n   sort(G1, G1 + cnt1, cmp2); n   sort(G2, G2 + cnt2, cmp2); n   int tot = 0; n   int cur1, cur2; n   cur1 = cur2 = 0; n   while (tot < n) { n     if ((cur2 != cnt2) && (cur1 == cnt1 || G1[cur1].i > G2[cur2].i)) { n       printf( %d %d n , G2[cur2].ans.first, G2[cur2].ans.second); n       cur2++; n     } else { n       printf( %d %d n , G1[cur1].ans.first, G1[cur1].ans.second); n       cur1++; n     } n     tot++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, i, x = 0; n   cin >> n >> k; n   char a; n   for (i = 0; i < n; i++) { n     cin >> a; n     if (a ==  # ) { n       x++; n       if (x == k) break; n     } else n       x = 0; n   } n   cout << (x == k ?  NO  :  YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool vowel(char c) { n   string s =  aeiou ; n   for (int i = 0; i < 5; i++) n     if (c == s[i]) return true; n   return false; n } n bool rhyme(string s1, string s2, int k) { n   int i = s1.size() - 1, j = s2.size() - 1; n   int cnt = 0; n   while (i >= 0 && j >= 0) { n     if (s1[i] != s2[j]) return false; n     if (vowel(s1[i])) cnt++; n     if (cnt == k) return true; n     i--; n     j--; n   } n   return false; n } n int main() { n   int n, k; n   cin >> n >> k; n   string scheme[4] = { aabb ,  abab ,  abba ,  aaaa }; n   int ans = -1; n   for (int i = 0; i < n; i++) { n     string s[4]; n     cin >> s[0] >> s[1] >> s[2] >> s[3]; n     int temp = -1; n     if (rhyme(s[0], s[1], k) && rhyme(s[2], s[3], k) && rhyme(s[0], s[2], k)) { n       temp = 3; n     } else if (rhyme(s[0], s[1], k) && rhyme(s[2], s[3], k)) { n       temp = 0; n     } else if (rhyme(s[0], s[2], k) && rhyme(s[1], s[3], k)) { n       temp = 1; n     } else if (rhyme(s[0], s[3], k) && rhyme(s[1], s[2], k)) { n       temp = 2; n     } else { n       ans = -1; n       break; n     } n     if (ans < 0) n       ans = temp; n     else if (temp == 3 && ans >= 0 && ans <= 2) n       continue; n     else if (ans == 3 && temp >= 0 && temp <= 2) n       ans = temp; n     else if (temp != ans) { n       ans = -1; n       break; n     } n   } n   if (ans >= 0) n     cout << scheme[ans] << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a; n   int x; n   cin >> a; n   x = 6 * a * (a - 1) + 1; n   cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, x, y; n     cin >> n >> x >> y; n     int dif = y - x; n     int minb = 1e9; n     for (int i = 1; i * i <= dif; ++i) { n       if (dif % i != 0) continue; n       if (dif / i + 1 <= n) { n         minb = min(minb, i); n       } n       if (i + 1 <= n) { n         minb = min(minb, dif / i); n       } n     } n     if (y - minb * (n - 1) > 0) { n       int res = y - minb * (n - 1); n       for (int i = 1; i <= n; ++i) { n         cout << res; n         if (i < n) cout <<    ; n         res += minb; n       } n       cout << endl; n     } else { n       int cnt = (y - 1) / minb; n       int res = y - cnt * minb; n       for (int i = 1; i <= n; ++i) { n         cout << res; n         if (i < n) cout <<    ; n         res += minb; n       } n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n float calc(int h, int d, int c, int n) { n   float a; n   if (h % n == 0) { n     a = (h / n) * c; n   } else n     a = ((h / n) + 1) * c; n   return a; n } n int main() { n   int p = 0, q = 0; n   int h, d, c, n, eh; n   int hh, mm; n   cin >> hh >> mm; n   cin >> h >> d >> c >> n; n   float a = calc(h, d, c, n); n   if (hh >= 20) { n     a = a * 0.8; n     printf( %f , a); n     return 0; n   } n   if (mm != 00) { n     q = 60 - mm; n     p = 19 - hh; n   } else n     p = 20 - hh; n   eh = (p * 60 + q) * d; n   h = h + eh; n   float b = calc(h, d, c, n); n   b = b * 0.8; n   if (b < a) a = b; n   printf( %f , a); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC optimize( unroll-loops ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const int maxnnn = (int)1e6 + 10; n int aaaa[maxnnn]; n int bbbb[maxnnn]; n int l[maxnnn]; n int r[maxnnn]; n int nnnnn; n vector<int> gggg[maxnnn]; n vector<int> tttt[4 * maxnnn]; n int cnttttt; n void build(int v, int tl, int tr) { n   if (tl == tr) n     tttt[v] = vector<int>(1, aaaa[tl]); n   else { n     int tm = (tl + tr) / 2; n     build(v * 2, tl, tm); n     build(v * 2 + 1, tm + 1, tr); n     merge(tttt[v * 2].begin(), tttt[v * 2].end(), tttt[v * 2 + 1].begin(), n           tttt[v * 2 + 1].end(), back_inserter(tttt[v])); n   } n } n void query(int v, int tl, int tr, int l, int r, int x) { n   if (l > r) return; n   if (l == tl && tr == r) { n     int i = (int)tttt[v].size() - 1; n     while (i >= 0 && tttt[v][i] > x && cnttttt < nnnnn) { n       cnttttt++; n       gggg[bbbb[x]].push_back(bbbb[tttt[v][i]]); n       gggg[bbbb[tttt[v][i]]].push_back(bbbb[x]); n       i--; n     } n     return; n   } n   int tm = (tl + tr) / 2; n   query(v * 2, tl, tm, l, min(r, tm), x); n   query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, x); n } n int used[maxnnn]; n void dfs(int v) { n   cnttttt++; n   used[v] = 1; n   for (int u : gggg[v]) { n     if (!used[u]) { n       dfs(u); n     } n   } n } n void solution() { n   cin >> nnnnn; n   for (int i = 1; i <= nnnnn; i++) { n     cin >> l[i] >> r[i]; n     aaaa[l[i]] = r[i]; n     aaaa[r[i]] = -1; n     bbbb[r[i]] = bbbb[l[i]] = i; n   } n   build(1, 1, 2 * nnnnn); n   for (int i = 1; i <= nnnnn; i++) { n     query(1, 1, 2 * nnnnn, l[i], r[i], r[i]); n   } n   if (cnttttt != nnnnn - 1) { n     cout <<  NO  << endl; n     return; n   } n   cnttttt = 0; n   dfs(1); n   if (cnttttt == nnnnn) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   solution(); n } n


#include <bits/stdc++.h> n using namespace std; n const long long linf = 1e18 + 5; n const int mod = (int)1e9 + 7; n const int logN = 17; n const int inf = 1e9; n const int N = 1e6 + 5; n int way[N][2], size[N], all[N][30][2], n, m, a[N], S = 1; n void insert(int x) { n   int node = 1; n   for (int i = 29; i >= 0; i--) { n     int t = (x >> i) & 1; n     if (!way[node][t]) way[node][t] = ++S; n     node = way[node][t]; n     size[node]++; n     for (int j = 0; j <= 29; j++) all[node][j][((x & (1 << j)) != 0)]++; n   } n } n int hm(int x, int big) { n   int node = 1; n   int ans = 0; n   for (int i = 29; i >= 0; i--) { n     if (!node) break; n     int t = 0; n     if ((x & (1 << i)) ^ (big & (1 << i))) t = 1; n     if (big & (1 << i)) n       ; n     else n       ans += size[way[node][!t]]; n     node = way[node][t]; n   } n   ans += size[node]; n   return ans; n } n int molla(int node, int x) { n   long long ans = 0; n   for (int i = 0; i <= 29; i++) { n     ans += (long long)all[node][i][!(x & (1 << i))] * (1 << i) % mod; n     ans %= mod; n   } n   return ans; n } n long long sum(int x, int big) { n   int node = 1; n   long long ans = 0; n   for (int i = 29; i >= 0; i--) { n     if (!node) break; n     int t = 0; n     if ((x & (1 << i)) ^ (big & (1 << i))) t = 1; n     if (big & (1 << i)) n       ; n     else { n       ans += molla(way[node][!t], x); n       ans %= mod; n     } n     node = way[node][t]; n   } n   ans += (long long)size[node] * big % mod; n   ans %= mod; n   return ans; n } n int get(int x) { n   long long cur = 0; n   for (int i = 1; i <= n; i++) cur += hm(a[i], x); n   return cur / 2; n } n long long sum(int x) { n   long long cur = 0; n   for (int i = 1; i <= n; i++) cur = (cur + sum(a[i], x)) % mod; n   if (cur & 1) cur += mod; n   return cur / 2LL; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     insert(a[i]); n   } n   int bas = 0, son = (1 << 30) - 1; n   if (!m) { n     cout << 0 <<   n ; n     return 0; n   } n   while (bas < son) { n     int orta = bas + son >> 1; n     if (bas == orta) orta++; n     if (get(orta) >= m) n       bas = orta; n     else n       son = orta - 1; n   } n   long long ans = n       (sum(bas + 1) + (m - get(bas + 1)) * (long long)bas % mod) % mod; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e5 + 7; n int m, k; n long long n, x, s, a[MAX], b[MAX], c[MAX], d[MAX]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> k >> x >> s; n   for (int i = 1; i <= m; ++i) { n     cin >> a[i]; n   } n   for (int i = 1; i <= m; ++i) { n     cin >> b[i]; n   } n   for (int i = 1; i <= k; ++i) { n     cin >> c[i]; n   } n   for (int i = 1; i <= k; ++i) { n     cin >> d[i]; n   } n   long long res = x * n; n   for (int i = 1; i <= m; ++i) { n     int l = 1, r = k; n     while (l <= r) { n       int mid = (l + r) / 2; n       if (b[i] + d[mid] <= s) { n         l = mid + 1; n       } else { n         r = mid - 1; n       } n     } n     long long numPrep = c[r]; n     if (numPrep == n) { n       res = 0; n       break; n     } n     long long prepTime = x * n; n     if (b[i] <= s) { n       prepTime = a[i] * (n - numPrep); n     } n     res = min(prepTime, res); n   } n   long long mxNumPrep = -1; n   for (int i = 1; i <= k; ++i) { n     if (d[i] <= s) { n       mxNumPrep = max(mxNumPrep, c[i]); n     } else { n       break; n     } n   } n   res = min(x * (n - mxNumPrep), res); n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void read(T &x) { n   x = 0; n   char c = getchar(), f = 0; n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) f = 1; n   for (; c >=  0  && c <=  9 ; c = getchar()) n     x = (x << 1) + (x << 3) + (c ^ 48); n   if (f) x = -x; n } n int vl, wh, n, nx[1005], a[1005], m; n char st[1005], dp[1005]; n int main() { n   read(n), read(wh); n   for (int i = 1, x; i <= n; i++) read(x), x ? nx[x] = i, st[i] = 1 : 0; n   for (int i = 1, c, x; i <= n; i++) n     if (!st[i]) { n       char tag = (c = 0) * (x = i); n       while (x) c++, (x == wh ? tag = 1, vl = c : 0), x = nx[x]; n       if (!tag) a[++m] = c; n     } n   dp[0] = 1; n   for (int i = 1; i <= m; i++) n     for (int j = n; j >= a[i]; j--) dp[j] |= dp[j - a[i]]; n   for (int i = 0; i <= n; i++) n     if (dp[i]) printf( %d n , i + vl); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (long long)1e18; n const int mod = (int)1e9 + 7; n const int MAXN = (int)4e5 + 5; n long long n, ans[MAXN], ext[MAXN]; n vector<pair<int, pair<int, int>>> sorg[MAXN]; n vector<pair<int, int>> adj[MAXN]; n int a[MAXN]; n const int L = 20; n int geri[MAXN]; n int par[MAXN][L], dep[MAXN]; n int tin[MAXN], tout[MAXN]; n int Gt = 1; n void dfs0(int v, int pr) { n   tin[v] = Gt++; n   if (pr == -1) { n     dep[v] = 0; n     par[v][0] = v; n   } else { n     dep[v] = dep[pr] + 1; n     par[v][0] = pr; n   } n   for (int j = 1; j < L; j++) par[v][j] = par[par[v][j - 1]][j - 1]; n   ext[v] = a[v]; n   for (pair<int, int> j : adj[v]) { n     if (j.first == pr) continue; n     geri[j.first] = j.second; n     dfs0(j.first, v); n     ext[v] += max(0LL, -2LL * j.second + ext[j.first]); n   } n   tout[v] = Gt - 1; n } n int gtp(int v, int dd) { n   for (int i = L - 1; i >= 0; i--) { n     if ((1 << i) <= dd) { n       v = par[v][i]; n       dd -= (1 << i); n     } n   } n   return v; n } n int find_lca(int a, int b) { n   if (dep[a] > dep[b]) swap(a, b); n   for (int i = L - 1; i >= 0; i--) { n     if (dep[a] + (1 << i) <= dep[b]) b = par[b][i]; n   } n   if (a == b) return a; n   for (int i = L - 1; i >= 0; i--) { n     if (par[a][i] != par[b][i]) { n       a = par[a][i]; n       b = par[b][i]; n     } n   } n   return par[a][0]; n } n long long t[MAXN]; n void upd(int l, int r, long long val) { n   r++; n   for (; l < MAXN; l += (l & -l)) t[l] += val; n   for (; r < MAXN; r += (r & -r)) t[r] -= val; n } n long long gt(int v) { n   long long res = 0; n   for (; v; v -= (v & -v)) res += t[v]; n   return res; n } n void dfs1(int v, int pr, long long sum) { n   upd(tin[v], tin[v], ext[v]); n   long long nS = sum + a[v]; n   for (pair<int, int> u : adj[v]) { n     if (u.first == pr) continue; n     nS += max(0LL, -2 * u.second + ext[u.first]); n   } n   for (pair<int, int> u : adj[v]) { n     if (u.first == pr) continue; n     nS -= max(0LL, -2 * u.second + ext[u.first]); n     dfs1(u.first, v, max(0LL, nS - 2 * u.second)); n     nS += max(0LL, -2 * u.second + ext[u.first]); n   } n   unordered_map<int, long long> mp; n   for (pair<int, int> u : adj[v]) { n     if (u.first == pr) continue; n     long long dg = max(0LL, -2 * u.second + ext[u.first]); n     mp[u.first] = dg; n   } n   for (auto u : sorg[v]) { n     int a = u.second.first, b = u.second.second; n     if (b == v) swap(a, b); n     if (a == v && b == v) { n       ans[u.first] = sum + ext[v]; n     } else if (a == v) { n       int r1 = gtp(b, dep[b] - dep[v] - 1); n       ans[u.first] = sum + ext[v] + gt(tin[b]) - mp[r1] - geri[r1]; n     } else { n       int r1 = gtp(b, dep[b] - dep[v] - 1), r2 = gtp(a, dep[a] - dep[v] - 1); n       ans[u.first] = sum + ext[v] + gt(tin[b]) + gt(tin[a]) - mp[r1] - mp[r2] - n                      geri[r1] - geri[r2]; n     } n   } n   for (pair<int, int> u : adj[v]) { n     if (u.first == pr) continue; n     upd(tin[u.first], tout[u.first], ext[v] - mp[u.first] - u.second); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int q; n   cin >> n >> q; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 0; i < n - 1; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     adj[u].push_back({v, w}); n     adj[v].push_back({u, w}); n   } n   dfs0(1, -1); n   for (int i = 0; i < q; i++) { n     int u, v; n     cin >> u >> v; n     sorg[find_lca(u, v)].push_back({i, {u, v}}); n   } n   dfs1(1, -1, 0); n   for (int i = 0; i < q; i++) cout << ans[i] <<   n ; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int arr = 2e5 + 10; n const int ar = 2e3 + 10; n const long double pi = acos(-1); n const long double eps = 1e-10; n const long long md = 1e9 + 7; n vector<int> reb[(int)(3e5 + 10)]; n bool use[(int)(3e5 + 10)]; n int n, m; n int pred[(int)(3e5 + 10)]; n vector<int> ans(0); n set<pair<int, int> > edges; n void upd_ans(vector<int> cur_ans) { n   if (ans.empty() || (int)(cur_ans.size()) < (int)(ans.size())) { n     ans = cur_ans; n   } n } n void bfs() { n   queue<int> q; n   q.push(1); n   use[1] = 1; n   while (!q.empty()) { n     int now = q.front(); n     q.pop(); n     for (auto wh : reb[now]) { n       if (!use[wh]) { n         use[wh] = 1; n         q.push(wh); n         pred[wh] = now; n       } n     } n   } n   if (use[n]) { n     vector<int> cur_ans(0); n     int cur = n; n     while (cur != 1) { n       cur_ans.push_back(cur); n       cur = pred[cur]; n     } n     cur_ans.push_back(1); n     reverse(cur_ans.begin(), cur_ans.end()); n     upd_ans(cur_ans); n   } n } n int num[(int)(3e5 + 10)]; n vector<int> ways[(int)(3e5 + 10)]; n int cnt; n void another_algo() { n   int c1 = 0; n   for (int i = 2; i <= n; i++) { n     c1 += use[i]; n   } n   if ((int)(reb[1].size()) != c1) { n     for (int i = 1; i <= n; i++) { n       if (edges.count({1, i})) { n         for (auto j : reb[i]) { n           if (j != 1 && !edges.count({1, j})) { n             vector<int> cur_ans(0); n             cur_ans.push_back(1); n             cur_ans.push_back(i); n             cur_ans.push_back(j); n             cur_ans.push_back(1); n             cur_ans.push_back(n); n             upd_ans(cur_ans); n             return; n           } n         } n       } n     } n   } else { n     for (int i = 2; i <= n; i++) { n       if (use[i]) { n         set<int> nums; n         nums.clear(); n         for (auto wh : reb[i]) { n           if (num[wh]) { n             nums.insert(num[wh]); n           } n         } n         if ((int)(nums.size()) == 0) { n           num[i] = ++cnt; n           ways[cnt].push_back(i); n         } else if ((int)(nums.size()) == 1) { n           int have = -1; n           int not_have = -1; n           for (auto wh : ways[*nums.begin()]) { n             if (edges.count({i, wh})) { n               have = wh; n             } else { n               not_have = wh; n             } n           } n           if (not_have == -1) { n             num[i] = *nums.begin(); n             ways[*nums.begin()].push_back(i); n           } else { n             int I = i; n             int J = have; n             int K = not_have; n             vector<int> cur_ans(0); n             cur_ans.push_back(1); n             cur_ans.push_back(I); n             cur_ans.push_back(J); n             cur_ans.push_back(K); n             cur_ans.push_back(I); n             cur_ans.push_back(n); n             upd_ans(cur_ans); n             return; n           } n         } else { n           int num1 = *nums.begin(); n           nums.erase(nums.begin()); n           int num2 = *nums.begin(); n           nums.erase(nums.begin()); n           int kek1 = -1; n           int kek2 = -1; n           for (auto wh : reb[i]) { n             if (num[wh] == num1) { n               kek1 = wh; n             } n             if (num[wh] == num2) { n               kek2 = wh; n             } n           } n           int I = kek1; n           int J = i; n           int K = kek2; n           vector<int> cur_ans(0); n           cur_ans.push_back(1); n           cur_ans.push_back(I); n           cur_ans.push_back(J); n           cur_ans.push_back(K); n           cur_ans.push_back(I); n           cur_ans.push_back(n); n           upd_ans(cur_ans); n           return; n         } n       } n     } n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     if (min(u, v) == 1 && max(u, v) == n) { n       cout << 1 <<   n ; n       cout << 1 <<     << n <<   n ; n       return 0; n     } n     edges.insert({u, v}); n     edges.insert({v, u}); n     reb[u].push_back(v); n     reb[v].push_back(u); n   } n   bfs(); n   if ((int)(ans.size()) <= 3 && !ans.empty()) { n     cout << (int)(ans.size()) - 1 <<   n ; n     for (auto i : ans) { n       cout << i <<    ; n     } n     cout <<   n ; n     return 0; n   } n   another_algo(); n   if (ans.empty()) { n     cout << -1 <<   n ; n   } else { n     cout << (int)(ans.size()) - 1 <<   n ; n     for (auto i : ans) { n       cout << i <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   long long n; n   cin >> n; n   string s; n   cin >> s; n   vector<long long> pref(n + 1); n   pref[0] = 0; n   char c; n   for (long long i = 1; i <= n; ++i) { n     c = (int)s[i - 1] - 48; n     pref[i] = pref[i - 1] + c; n   } n   long long q = n - 1; n   while (s[q] ==  0 ) q--; n   q++; n   if (pref[n] == 0) { n     cout <<  YES ; n     return 0; n   } n   for (long long i = 1; i < pref[n]; ++i) { n     long long ind = 0; n     long long j = 1; n     while (ind < q) { n       while (j <= n && pref[j] - pref[ind] != i) j++; n       if (j == n + 1) break; n       ind = j; n       j++; n     } n     if (ind == q) { n       cout <<  YES ; n       return 0; n     } n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n template <class T> n T gcd(T a, T b) { n   T r; n   while (b != 0) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n template <class T> n T sqr(T x) { n   return x * x; n } n template <class T> n T cube(T x) { n   return x * x * x; n } n using namespace std; n string s; n bool check[100005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long int i, j, k, n, m, p = 0, q = 1e7; n   string s; n   char c; n   c =  0 ; n   cin >> s; n   for (i = 0; i < s.size(); i++) { n     if (s[i] == c) { n       if (p == 0) { n         cout << s[i]; n         p = 2; n       } n     } else { n       if (p != 0) p--; n       cout << s[i]; n     } n     c = s[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-9; n const int MAXN = 3 * int(1e5) + 10; n int pos[MAXN]; n int a[MAXN]; n int n, q; n int deleted = -1; n int fenw[MAXN]; n void upd(int x, int d) { n   for (int i = x; i < n; i = (i | (i + 1))) fenw[i] += d; n } n int sum(int r) { n   int res = 0; n   for (int i = r; i >= 0; i = (i & (i + 1)) - 1) res += fenw[i]; n   return res; n } n int sum(int l, int r) { return sum(r) - sum(l - 1); } n bool isBeg(int i) { return sum(i, i) == 1; } n void build() { n   memset(fenw, 0, sizeof fenw); n   upd(0, 1); n   for (int i = 1; i < n; i++) n     if (pos[i - 1] > pos[i]) upd(i, 1); n } n void del(int val) { n   if (val < n - 1 && pos[val] < pos[val + 1] && deleted != val + 1) n     upd(val + 1, 1); n   if (isBeg(val)) upd(val, -1); n } n void add(int val) { n   if (val < n - 1 && pos[val] < pos[val + 1] && deleted != val + 1) n     upd(val + 1, -1); n   if (0 == val || pos[val - 1] > pos[val] || deleted == val - 1) upd(val, 1); n } n void goSwap(int x, int y) { n   deleted = -1; n   del(a[x]); n   deleted = a[x]; n   del(a[y]); n   swap(a[x], a[y]); n   pos[a[x]] = x; n   pos[a[y]] = y; n   add(a[x]); n   add(a[y]); n } n int calc(int x, int y) { n   if (isBeg(x)) n     return sum(x, y); n   else n     return sum(x, y) + 1; n } n void output() { n   for (int i = 0; i < int(n); i++) printf( %d  , sum(i, i)); n   puts(  ); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < int(n); i++) { n     scanf( %d , a + i); n     a[i]--; n     pos[a[i]] = i; n   } n   build(); n   scanf( %d , &q); n   for (int i = 0; i < int(q); i++) { n     int p, x, y; n     scanf( %d %d %d , &p, &x, &y); n     x--; n     y--; n     if (p == 1) n       printf( %d n , calc(x, y)); n     else n       goSwap(x, y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   long long t = 1; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     long long len = n, l, r; n     for (long long i = 0; i < m; i++) { n       cin >> l >> r; n       len = min(len, r - l + 1); n     } n     cout << len << endl; n     for (long long i = 0; i < n; i++) { n       cout << (i % len) <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ans; n int num[1000010], flag[1000010]; n int fa[1000010], vis[1000010]; n int Find(int x) { return fa[x] = x == fa[x] ? x : Find(fa[x]); } n void Union(int x, int y) { n   int fx = Find(x); n   int fy = Find(y); n   if (fx != fy) fa[fy] = fx; n } n int main() { n   int t, C = 1; n   while (scanf( %d%d , &n, &m) != EOF) { n     memset(num, 0, sizeof(num)); n     memset(flag, 0, sizeof(flag)); n     memset(vis, 0, sizeof(vis)); n     for (int i = 1; i <= n; i++) fa[i] = i; n     int u, v; n     vis[1] = 1; n     for (int i = 0; i < m; i++) { n       scanf( %d%d , &u, &v); n       if (u != v) { n         num[u]++; n         num[v]++; n         vis[u]++; n         vis[v]++; n         Union(u, v); n       } else n         vis[u]++; n     } n     int ans = 0, k = 0, tong = 0; n     for (int i = 1; i <= n; i++) n       if (num[i] & 1) flag[Find(i)]++; n     for (int i = 1; i <= n; i++) n       if (vis[i] && Find(i) == i) { n         if (flag[i]) n           ans += flag[i]; n         else n           tong++; n         k++; n       } n     if (k == 1) n       printf( %d n , ans / 2); n     else n       printf( %d n , ans / 2 + tong); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN(1e5 + 5); n inline int read() { n   char ch(getchar()); n   int x(0); n   int flag(1); n   while (!isdigit(ch)) { n     if (ch ==  - ) { n       flag = -1; n     } n     ch = getchar(); n   } n   while (isdigit(ch)) { n     x = (x << 1) + (x << 3) + (ch ^ 48); n     ch = getchar(); n   } n   return flag * x; n } n int sum[MAXN]; n int a[MAXN]; n int dp[MAXN]; n int main() { n   int n(read()); n   for (int i = 1; i <= n; i++) { n     a[i] = read(); n   } n   for (int i = n; i >= 1; i--) { n     sum[i] = sum[i + 1] + a[i]; n     dp[i] = std::max(dp[i + 1], sum[i + 1] - dp[i + 1] + a[i]); n   } n   std::printf( %d %d n , sum[1] - dp[1], dp[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n const T& max(const T& a, const T& b, const T& c) { n   return max(a, max(b, c)); n } n template <class T> n const T& min(const T& a, const T& b, const T& c) { n   return min(a, min(b, c)); n } n void sc(int& a) { scanf( %d , &a); } n void sc(long long& a) { scanf( %lld , &a); } n void sc(int& a, int& b) { n   sc(a); n   sc(b); n } n void sc(long long& a, long long& b) { n   sc(a); n   sc(b); n } n void sc(int& a, int& b, int& c) { n   sc(a, b); n   sc(c); n } n void sc(long long& a, long long& b, long long& c) { n   sc(a, b); n   sc(c); n } n void prl(int a) { printf( %d n , a); } n void prl(long long a) { printf( %lld n , a); } n void prl() { printf(  n ); } n void prs(int a) { printf( %d  , a); } n void prs(long long a) { printf( %lld  , a); } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n long long poww(long long a, long long b) { n   if (b == 0) return 1; n   long long tmp = poww(a, b / 2); n   return (b & 1 ? a * tmp * tmp : tmp * tmp); n } n long long sumOfDigs(string s) { n   long long sum = 0; n   for (int i = 0; i < s.length(); i++) sum += s[i] -  0 ; n   return sum; n } n long long sumOfDigs(long long n) { n   return (n < 10 ? n : n % 10 + sumOfDigs(n / 10)); n } n string itos(long long i) { n   string s =   ; n   while (i) { n     s += char(i % 10 +  0 ); n     i /= 10; n   } n   reverse(s.begin(), s.end()); n   return s; n } n long long stoi(string& s) { n   long long tot = 0; n   for (int i = (int)s.length() - 1, j = 1; i >= 0; i--, j *= 10) { n     tot += j * (s[i] +  0 ); n   } n   return tot; n } n int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; n using namespace std; n void tt() { freopen( test.txt ,  r , stdin); } n long long mod = 1000000000000000003; n long long modpower(long long x, long long y, long long p) { n   x %= mod; n   if (!y) return 1; n   long long res = 1; n   if (y & 1) { n     res *= x; n     res %= p; n   } n   long long z = modpower(x, y / 2, p); n   z %= p; n   z *= z; n   z %= mod; n   res *= z; n   res %= p; n   return res; n } n double ans = 1e60; n long long a = -1, b = -1; n int main() { n   long long t1, t2, x1, x2, t0; n   cin >> t1 >> t2 >> x1 >> x2 >> t0; n   if (t1 == t0 && t2 == t0) { n     return cout << x1 <<     << x2 << endl, 0; n   } else if (t1 == t0) { n     return cout << x1 <<     << 0 << endl, 0; n   } n   for (long long y2 = 1; y2 <= x2; y2++) { n     long long y1 = (t2 * y2 - t0 * y2) / (t0 - t1); n     if (y1 > x1) { n       y1 = x1; n     } n     double t = (long double)(1.0 * y1 * t1 + 1.0 * y2 * t2) / (y1 + y2); n     if (t < t0) continue; n     if (t < ans || t == ans && a + b < y1 + y2) { n       ans = t; n       a = y1; n       b = y2; n     } n   } n   cout << a <<     << b << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n int n, sz[maxn], ans[maxn]; n long long sum; n vector<int> adj[maxn], vec; n void dfs(int u, int par) { n   sz[u] = 1; n   for (auto v : adj[u]) n     if (v != par) { n       dfs(v, u); n       sz[u] += sz[v]; n     } n } n void DFSend(int u, int par) { n   vec.push_back(u); n   for (auto v : adj[u]) n     if (v != par) DFSend(v, u); n } n int centroid(int u, int par) { n   for (auto v : adj[u]) n     if (v != par && sz[v] * 2 > n) return centroid(v, u); n   return u; n } n void read_input() { n   cin >> 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 } n void solve() { n   dfs(1, 0); n   for (int i = 1; i <= n; i++) sum += min(sz[i], n - sz[i]); n   sum *= 2; n   DFSend(centroid(1, 0), -1); n   for (int i = 0; i < n; i++) ans[vec[i]] = vec[(i + n / 2) % n]; n   cout << sum <<   n ; n   for (int i = 1; i <= n; i++) cout << ans[i] <<    ; n } n int main() { n   read_input(), solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, mx; n int a[4000400], q[4000400], dis[2005]; n bool vis[2005]; n int head[2005], edgenum; n struct Edge { n   int to, nxt; n } edge[4000400]; n inline void add(int u, int v) { n   edge[++edgenum].to = v; n   edge[edgenum].nxt = head[u]; n   head[u] = edgenum; n } n inline void read(int &x) { n   x = 0; n   char c = getchar(); n   int f = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = 10 * x + c -  0 ; n     c = getchar(); n   } n   x *= f; n } n bitset<2004> dp[2]; n inline int solve() { n   int t = 1; n   dp[0][1000] = 1; n   for (int i = 1; i <= 1000; i++) { n     dp[t].reset(); n     for (int j = 1; j <= k; j++) n       if (a[j] > 0) n         dp[t] |= dp[t ^ 1] << a[j]; n       else n         dp[t] |= dp[t ^ 1] >> (-a[j]); n     if (dp[t][1000] == 1) return i; n     t ^= 1; n   } n   return -1; n } n int main() { n   read(n); n   read(k); n   for (int i = 1; i <= k; i++) read(a[i]); n   for (int i = 1; i <= k; i++) vis[a[i]] = 1; n   int l = 0; n   for (int i = 0; i <= 1000; i++) n     if (vis[i]) a[++l] = i; n   for (int i = 1; i <= l; i++) a[i] -= n; n   k = l; n   printf( %d , solve()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double Fictive = 5e18; n struct str { n   long long k, b; n   long double eval(long double x) { return x * (long double)k + b; } n }; n vector<str> vec; n inline long double get_x(str fi, str se) { n   return (long double)(fi.b - se.b) / (long double)(se.k - fi.k); n } n inline bool to_erase(str l1, str l2, str l3) { n   long double x = get_x(l1, l3); n   return (l2.eval(x) >= l3.eval(x)); n } n inline void add_line(str l) { n   while (vec.size() >= 2 && to_erase(vec[vec.size() - 2], vec.back(), l)) n     vec.pop_back(); n   vec.push_back(l); n } n inline long long get(long long x) { n   int l = 0, r = vec.size() - 1; n   while (l < r) { n     int mid = (l + r) >> 1; n     if (vec[mid].eval(x) >= vec[mid + 1].eval(x)) n       l = mid + 1; n     else n       r = mid; n   } n   return vec[l].eval(x); n } n int n, a[100100], b[100100]; n long long dp[100100]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; i++) { n     cin >> b[i]; n   } n   dp[1] = 0; n   add_line({b[1], 0}); n   for (int i = 2; i <= n; i++) { n     dp[i] = 1e15; n     dp[i] = get(a[i]); n     add_line({b[i], dp[i]}); n   } n   cout << dp[n]; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   vector<int> a(10, 0), b(10, 0); n   string n; n   string num; n   cin >> n >> num; n   for (auto w : num) { n     a[w -  0 ]++; n   } n   int minimum = INT_MAX; n   for (auto w : n) b[w -  0 ]++; n   for (int i = 0; i < 10; i++) { n     if (i != 2 && i != 6 && i != 5 && i != 9 && b[i] > 0) n       minimum = min(minimum, a[i] / b[i]); n   } n   if (b[2] + b[5] > 0) minimum = min(minimum, (a[2] + a[5]) / (b[2] + b[5])); n   if (b[6] + b[9] > 0) minimum = min(minimum, (a[6] + a[9]) / (b[6] + b[9])); n   cout << minimum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 22; n const int maxp = 777 + (1 << (maxn - 2)); n const int maxm = 1E5 + 77; n char S[maxm]; n int N, M, _V[maxn][maxm], V[maxm], dp[maxn][maxp], ans = 1E9 + 7; n int main() { n   scanf( %d%d , &N, &M); n   for (int i = 0; i < N; ++i) { n     scanf( %s , S); n     for (int j = 0; j < M; ++j) _V[i][j] = S[j] ==  1  ? 1 : 0; n   } n   for (int j = 0; j < M; ++j) n     for (int i = 0, v = 1; i < N; ++i, v <<= 1) V[j] |= _V[i][j] ? v : 0; n   for (int i = 0; i < M; ++i) { n     ++dp[0][V[i]]; n   } n   int tot = 1 << N; n   for (int i = 1; i <= N; ++i) n     for (int j = 0; j < tot; ++j) { n       if (i >= 3) dp[i][j] = dp[i - 2][j] * (i - 2); n       for (int p = 1; p < tot; p <<= 1) { n         for (int t = 1; t <= 2 && t <= i; ++t) n           dp[i][j] += (t & 1 ? 1 : -1) * dp[i - t][j ^ (t & 1 ? p : 0)]; n       } n       dp[i][j] /= i; n     } n   for (int i = 0; i < tot; ++i) { n     int tans = 0; n     for (int j = 0; j <= N; ++j) tans += min(j, N - j) * dp[j][i]; n     ans = min(ans, tans); n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int mxn = 2 * 100000 + 6; n int n, root; n int a[mxn]; n int c[mxn]; n int s[mxn]; n int base; n int ans = 0x3f3f3f3f; n int main() { n   cin >> n >> root; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     if (i == root && a[i] != 0) { n       ++base; n       a[i] = 0; n     } n     ++c[a[i]]; n   } n   partial_sum(c, c + n, s); n   int c0 = 0; n   for (int i = 0; i < n; ++i) { n     c0 += (c[i] == 0); n     int mov = c[0] - 1 + n - s[i]; n     int sum = max(mov, c0); n     if (i || n == 1) { n       ans = min(ans, sum); n     } n   } n   cout << ans + base << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1003, INF = 2000000009; n struct ban { n   int x, i; n   ban() {} n   ban(int x, int i) { n     this->x = x; n     this->i = i; n   } n }; n int n, m; n int s, f; n pair<pair<int, int>, int> b[N * 30]; n vector<ban> a[N]; n int hi; n bool c[N]; n int pi[N], px[N]; n bool dfs0(int x) { n   c[x] = true; n   if (x == f) return true; n   for (int i = 0; i < a[x].size(); ++i) { n     int h = a[x][i].x; n     if (a[x][i].i == hi) continue; n     if (!c[h]) { n       pi[h] = a[x][i].i; n       px[h] = x; n       if (dfs0(h)) return true; n     } n   } n   return false; n } n vector<int> path() { n   for (int i = 1; i <= n; ++i) c[i] = false; n   dfs0(s); n   vector<int> v; n   if (!c[f]) return v; n   for (int i = f; i != s; i = px[i]) { n     v.push_back(pi[i]); n   } n   reverse(v.begin(), v.end()); n   return v; n } n vector<int> vv; n int tin[N], ti, fup[N]; n void dfs(int x, int pi) { n   c[x] = true; n   tin[x] = ti++; n   fup[x] = tin[x]; n   for (int i = 0; i < a[x].size(); ++i) { n     int h = a[x][i].x; n     if (a[x][i].i == pi) continue; n     if (a[x][i].i == hi) continue; n     if (!c[h]) { n       dfs(h, a[x][i].i); n       fup[x] = min(fup[x], fup[h]); n       if (fup[h] > tin[x]) vv.push_back(a[x][i].i); n     } else { n       fup[x] = min(fup[x], tin[h]); n     } n   } n } n vector<int> bridges() { n   for (int i = 1; i <= n; ++i) c[i] = false; n   vv.clear(); n   dfs(s, 0); n   return vv; n } n int ans = INF; n int anss; n int ans1, ans2; n int main() { n   cin >> n >> m; n   cin >> s >> f; n   for (int i = 1; i <= m; ++i) { n     int x, y, z; n     cin >> x >> y >> z; n     a[x].push_back(ban(y, i)); n     a[y].push_back(ban(x, i)); n     b[i] = make_pair(make_pair(x, y), z); n   } n   if (s == f) { n     cout <<  -1  << endl; n     return 0; n   } n   vector<int> somepath = path(); n   if (somepath.empty()) { n     cout << 0 << endl << 0 << endl; n     return 0; n   } n   for (int i = 0; i < somepath.size(); ++i) { n     hi = somepath[i]; n     vector<int> p = path(); n     if (p.empty()) { n       if (b[somepath[i]].second < ans) { n         ans = b[somepath[i]].second; n         anss = 1; n         ans1 = somepath[i]; n       } n       continue; n     } n     set<int> s; n     for (int j = 0; j < p.size(); ++j) s.insert(p[j]); n     if (hi == 2) cerr <<   ; n     vector<int> br = bridges(); n     for (int j = 0; j < br.size(); ++j) { n       if (s.find(br[j]) != s.end()) { n         if (b[somepath[i]].second + b[br[j]].second < ans) { n           ans = b[somepath[i]].second + b[br[j]].second; n           anss = 2; n           ans1 = somepath[i]; n           ans2 = br[j]; n         } n       } n     } n   } n   if (ans == INF) { n     cout <<  -1  << endl; n     return 0; n   } n   cout << ans << endl; n   cout << anss << endl; n   if (anss == 1) n     cout << ans1 << endl; n   else n     cout << ans1 <<     << ans2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n struct node { n   int next[2]; n   int cnt; n }; n node make_node() { n   node nd; n   nd.cnt = 0; n   nd.next[0] = nd.next[1] = -1; n   return nd; n } n node t[MAXN * 30]; n int sz = 0; n int bs = 29; n void add(int tiv, int d) { n   (0); n   int v = 0; n   for (int i = bs; i >= 0; i--) { n     int bit = (tiv >> i) & 1; n     t[v].cnt += d; n     ; n     (0); n     if (t[v].next[bit] == -1) { n       t[sz] = make_node(); n       t[v].next[bit] = sz++; n     } n     v = t[v].next[bit]; n   } n   t[v].cnt += d; n   (0); n   (0); n } n int query(int p, int l) { n   int res = 0; n   int v = 0; n   int val = 0; n   for (int i = bs; i >= 0; i--) { n     (0); n     int pi = (p >> i) & 1; n     int li = (l >> i) & 1; n     if (li == 1) { n       int nxt = t[v].next[pi]; n       if (nxt != -1) n         res += t[nxt].cnt; n       else n         (0); n       if (t[v].next[1 - pi] == -1) n         return res; n       else { n         v = t[v].next[1 - pi]; n       } n     } else { n       if (t[v].next[pi] == -1) return res; n       v = t[v].next[pi]; n     } n   } n   return res; n } n int main() { n   t[0] = make_node(); n   sz = 1; n   int q; n   scanf( %d , &q); n   int op, p, l; n   for (int qq = 0; qq < q; qq++) { n     scanf( %d%d , &op, &p); n     if (op == 1) { n       add(p, 1); n     } else if (op == 2) { n       add(p, -1); n     } else { n       scanf( %d , &l); n       int res = query(p, l); n       cout << res << endl; n       ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n int n, k, m, res = 0, sm = 0, edge = -1, nb = 1; n int a[200055], b[200050], nxt[200505], dp[200005] = {0}, ans[500005]; n multiset<int> st, st1, st2; n set<int> stres; n string s, s1, s2, s3, s4; n vector<int> v[200050], vnode, vans, v3; n vector<pair<int, int> > vv, vv1, adj, v1, v2; n vector<pair<int, pair<int, int> > > vvv; n priority_queue<int> q; n map<int, int> mp; n multiset<int>::iterator it, it1, it2; n int voyel(char c) { n   if (c ==  a  || c ==  e  || c ==  i  || c ==  o  || c ==  u  || c ==  y ) n     return 1; n   return 0; n } n int up[200005][20]; n void solve() { n   int i, j; n   up[m + 1][0] = -1; n   for (i = 1; i <= m; i++) { n     int nxtt = nxt[b[i]]; n     int idx = lower_bound(v[nxtt].begin(), v[nxtt].end(), i) - v[nxtt].begin(); n     if (idx != v[nxtt].size()) { n       int idxb = v[nxtt][idx]; n       up[i][0] = idxb; n     } else n       up[i][0] = -1; n   } n   for (int j = 1; j < 20; j++) { n     for (i = 1; i <= m; i++) n       if (up[i][j - 1] != -1) up[i][j] = up[up[i][j - 1]][j - 1]; n   } n   for (i = 1; i <= m; i++) { n     int bsup = i; n     int x = n - 1; n     for (j = 19; j >= 0; j--) { n       if ((1 << j) <= x) { n         x -= (1 << j); n         if (bsup != -1) bsup = up[bsup][j]; n       } n     } n     if (bsup != -1) { n       dp[i] = bsup; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   int i = 0, j = 0, x, y, z = 0, mx = 0, nb0 = 0, nb1 = 0, nbmax = 0, nbinf = 0, n       mm = 5e9, idx = 0, ok = 0, l, r, negatif = 0, positif = 0, q, d, res1 = 0; n   char c; n   cin >> n >> m >> q; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (i = 1; i <= n; i++) nxt[a[i]] = a[i + 1]; n   nxt[a[n]] = a[1]; n   for (i = 1; i <= m; i++) { n     cin >> b[i]; n     v[b[i]].push_back(i); n   } n   for (i = 1; i <= n; i++) { n     sort(v[i].begin(), v[i].end()); n   } n   memset(up, -1, sizeof(up)); n   solve(); n   idx = m + 1; n   mx = 5e9; n   for (i = m; i >= 1; i--) { n     if (dp[i]) mx = min(mx, dp[i]); n     ans[i] = mx; n   } n   s =   ; n   while (q--) { n     cin >> x >> y; n     res = (ans[x] <= y); n     if (res != 0) n       s +=  1 ; n     else n       s +=  0 ; n   } n   cout << s; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 10; n const int INF = 1e9; n int n, ans, ansl, ansr; n char s[maxn]; n int a[maxn]; n struct Node { n   int pre, min, cnt, id; n   Node() { n     pre = cnt = id = 0; n     min = INF; n   } n   Node(int _pre, int _min, int _cnt, int _id) n       : pre(_pre), min(_min), cnt(_cnt), id(_id) {} n } b[maxn], pre[maxn], suf[maxn], tmppre[maxn], tmpsuf[maxn]; n bool cmpl(const Node& a, const Node& b) { n   return a.min - a.pre < b.min - b.pre; n } n bool cmpr(const Node& a, const Node& b) { return a.min < b.min; } n void cans(int cur, int l, int r) { n   if (ans < cur) ans = cur, ansl = l, ansr = r; n } n Node merge(const Node& a, const Node& b) { n   Node ret; n   ret.pre = a.pre + b.pre; n   if (a.min < b.min + a.pre) { n     ret.min = a.min; n     ret.cnt = a.cnt; n   } else if (a.min == b.min + a.pre) { n     ret.min = a.min; n     ret.cnt = a.cnt + b.cnt; n   } else { n     ret.min = b.min + a.pre; n     ret.cnt = b.cnt; n   } n   return ret; n } n Node rev(Node a) { n   a.pre = -a.pre; n   a.min = -a.min; n   return a; n } n vector<Node> get(int l, int r, int k) { n   vector<Node> ret; n   tmppre[l - 1] = Node(); n   for (int i = l; i <= r; ++i) tmppre[i] = merge(tmppre[i - 1], b[i]); n   tmpsuf[r + 1] = Node(); n   for (int i = r; i >= l; --i) tmpsuf[i] = merge(b[i], tmpsuf[i + 1]); n   for (int i = l; i <= r; ++i) n     if (a[i] == k) { n       ret.push_back(merge(merge(tmppre[i - 1], rev(b[i])), tmpsuf[i + 1])); n       ret.back().id = b[i].id; n     } n   return ret; n } n vector<Node> uniql(vector<Node> a) { n   vector<Node> ret; n   for (auto v : a) { n     if (ret.empty() || ret.back().min - ret.back().pre != v.min - v.pre) n       ret.push_back(v); n     else if (ret.back().cnt < v.cnt) n       ret.back() = v; n   } n   return ret; n } n vector<Node> uniqr(vector<Node> a) { n   vector<Node> ret; n   for (auto v : a) { n     if (ret.empty() || ret.back().min != v.min) n       ret.push_back(v); n     else if (ret.back().cnt < v.cnt) n       ret.back() = v; n   } n   return ret; n } n void calc(vector<Node> lv, vector<Node> rv, Node L, Node R, bool debug) { n   int id; n   for (auto& v : lv) id = v.id, v = merge(L, v), v.id = id; n   for (auto& v : rv) id = v.id, v = merge(v, R), v.id = id; n   sort(lv.begin(), lv.end(), cmpl); n   sort(rv.begin(), rv.end(), cmpr); n   lv = uniql(lv); n   rv = uniqr(rv); n   int l = 0; n   for (int i = 0; i < rv.size(); ++i) { n     while (l < lv.size() && lv[l].min <= lv[l].pre + rv[i].min) { n       if (lv[l].min < lv[l].pre + rv[i].min) n         cans(lv[l].cnt, lv[l].id, rv[i].id); n       else n         cans(lv[l].cnt + rv[i].cnt, lv[l].id, rv[i].id); n       ++l; n     } n     if (l < lv.size()) cans(rv[i].cnt, lv[l].id, rv[i].id); n   } n } n void solve(int l, int r) { n   if (l == r) return; n   int mid = l + r >> 1; n   calc(get(l, mid, 1), get(mid + 1, r, -1), pre[l - 1], suf[r + 1], 0); n   calc(get(l, mid, -1), get(mid + 1, r, 1), pre[l - 1], suf[r + 1], 0); n   solve(l, mid); n   solve(mid + 1, r); n } n int main() { n   scanf( %d , &n); n   scanf( %s , s + 1); n   int sum = 0; n   for (int i = 1; i <= n; ++i) { n     a[i] = s[i] ==  (  ? 1 : -1; n     b[i] = Node(a[i], a[i], 1, i); n     sum += a[i]; n   } n   if (sum) { n     puts( 0 ); n     puts( 1 1 ); n     return 0; n   } n   for (int i = 1; i <= n; ++i) pre[i] = merge(pre[i - 1], b[i]); n   for (int i = n; i >= 1; --i) suf[i] = merge(b[i], suf[i + 1]); n   ans = pre[n].cnt; n   ansl = ansr = 1; n   solve(1, n); n   printf( %d n , ans); n   printf( %d %d n , ansl, ansr); n   return 0; n } n


#include <bits/stdc++.h> n int dx[] = {0, 0, 1, -1, 2, 2, -2, -2, 1, -1, 1, -1}; n int dy[] = {1, -1, 0, 0, 1, -1, 1, -1, 2, 2, -2, -2}; n using namespace std; n class PAIR { n  public: n   long long int first; n   long long int second; n   long long int third; n }; n vector<long long int> V, V2, V3; n long long int ar[4500100]; n map<long long int, long long int> m; n int main() { n   long long int tcase = 1; n   for (long long int test = 1; test <= tcase; test++) { n     for (long long int i = 0; i <= 450010; i++) { n       ar[i] = i; n     } n     long long int q, x, ans = 0; n     scanf( %lld %lld , &q, &x); n     long long int a; n     for (long long int i = 0; i < q; i++) { n       scanf( %lld , &a); n       if (a == ans) { n         ans++; n         m[ar[a % x]] = 1; n         while (m.count(ans) == 1) { n           ans++; n         } n         ar[a % x] += x; n         printf( %lld n , ans); n         continue; n       } n       a %= x; n       m[ar[a]] = 1; n       if (ans == ar[a]) { n         ans++; n         while (m.count(ans) == 1) { n           ans++; n         } n       } n       ar[a] += x; n       printf( %lld n , ans); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 300 * 1000 + 5; n int n, k, arr[Maxn]; n int main() { n   scanf( %d%d , &n, &k); n   int Min = INT_MAX, Max = 0; n   for (int i = 0; i < n; i++) n     scanf( %d , &arr[i]), Min = min(Min, arr[i]), Max = max(Max, arr[i]); n   sort(arr, arr + n); n   for (int i = Min; i > 0; i--) { n     bool mark = false; n     if (Max % i <= k) { n       for (int j = 2; j * i <= Max; j++) { n         int ind = lower_bound(arr, arr + n, i * j) - arr; n         ind--; n         if (ind >= 0) { n           if (arr[ind] % i > k) mark = true; n         } n       } n       if (!mark) { n         printf( %d n , i); n         return 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, cnt, t; n   scanf( %d , &t); n   while (t--) { n     scanf( %d %d , &a, &b); n     cnt = 0; n     while (a > 0 && b > 0) { n       if (a > b) swap(a, b); n       int tmp = b / a; n       cnt += tmp; n       b -= tmp * a; n     } n     printf( %d n , cnt); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1005; n struct Edge { n   int u, v, w, id; n   Edge() {} n   Edge(int a, int b, int c, int d) : u(a), v(b), w(c), id(d) {} n   bool operator<(const Edge e) const { return w > e.w; } n } e[maxn * maxn]; n int fa[maxn << 1]; n int Find(int x) { return fa[x] == x ? fa[x] : (fa[x] = Find(fa[x])); } n void Merge(int a, int b) { n   int x = Find(a); n   int y = Find(b); n   fa[x] = y; n } n int main() { n   int n, m, q; n   while (scanf( %d%d%d , &n, &m, &q) != EOF) { n     int u, v, w; n     for (int i = 0; i < m; i++) { n       scanf( %d %d %d , &u, &v, &w); n       e[i] = Edge(u, v, w, i + 1); n     } n     sort(e, e + m); n     while (q--) { n       int flag = 0; n       for (int i = 1; i <= 2 * n; i++) fa[i] = i; n       scanf( %d%d , &u, &v); n       for (int i = 0; i < m; i++) { n         if (e[i].id < u || e[i].id > v) continue; n         if (Find(e[i].u) == Find(e[i].v)) { n           printf( %d n , e[i].w); n           flag = 1; n           break; n         } else { n           Merge(e[i].u, e[i].v + n); n           Merge(e[i].v, e[i].u + n); n         } n       } n       if (!flag) printf( -1 n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long hell = 1000000007; n const long long INF = 1000000000; n void solve() { n   long long n; n   cin >> n; n   map<long long, long long> mp; n   long long a[n]; n   long long s = 0; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     s += a[i]; n     mp[a[i]]++; n   } n   vector<long long> ans; n   for (long long i = 0; i < n; i++) { n     long long S = s - a[i]; n     if (S % 2) continue; n     mp[a[i]]--; n     S = S / 2; n     if (mp[S]) ans.push_back(i); n     mp[a[i]]++; n   } n   cout << ans.size() << endl; n   for (long long j : ans) cout << j + 1 <<    ; n   cout << endl; n } n int32_t main() { n   long long t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200005], f[200005], t[2000006], mx, mxbr; n bool bio[2000006], p[2000006]; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     p[a[i]] = 1; n     mxbr = max(mxbr, a[i]); n   } n   for (int i = 2; i <= 2 * mxbr; i++) { n     if (p[i - 1]) n       t[i] = i - 1; n     else n       t[i] = t[i - 1]; n   } n   for (int i = 1; i <= n; i++) { n     if (bio[a[i]]) continue; n     int x = 2 * a[i]; n     for (int j = x; j <= 2 * mxbr; j += a[i]) n       if (t[j] > a[i]) mx = max(mx, t[j] % a[i]); n     bio[a[i]] = 1; n   } n   cout << mx; n } n


#include <bits/stdc++.h> n int a[100005]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   int flag = -1; n   int sum = 0; n   int sum1 = 0; n   int ans = 1; n   for (int i = 1; i < n; i++) { n     if (a[i] == a[i - 1]) n       ans++; n     else { n       if (flag == -1) { n         flag = 1; n         sum = ans; n         ans = 1; n       } else { n         if (ans != sum) { n           printf( NO n ); n           return 0; n         } n         ans = 1; n       } n     } n   } n   if (flag == -1 || ans == sum) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int w1, h1, w2, h2, a, b, sum; n   scanf( %d%d%d%d , &w1, &h1, &w2, &h2); n   a = (h2 + h1 + 2) * 2; n   b = w1 * 2; n   sum = a + b; n   printf( %d n , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MOD = 1000000007; n int N, a[169], c[169], K; n long long int M = -10000000000000LL; n int ned, prv[169], first[169]; n long long int cap[20009], cost[20009], to[20009], nxt[20009], dist[169], n     pot[169]; n void init() { n   memset(first, -1, sizeof first); n   ned = 0; n } n void add(int u, int v, long long int f, long long int c) { n   to[ned] = v, cap[ned] = f; n   cost[ned] = c, nxt[ned] = first[u]; n   first[u] = ned++; n   to[ned] = u, cap[ned] = 0; n   cost[ned] = -c, nxt[ned] = first[v]; n   first[v] = ned++; n } n bool dijkstra(int s, int t) { n   memset(prv, -1, sizeof prv); n   for (int i = 0; i < N; i++) dist[i] = 1000000000000000000LL; n   set<pair<long long int, int> > q; n   q.insert(make_pair(0LL, s)); n   dist[s] = prv[s] = 0; n   int u, v; n   while (!q.empty()) { n     u = q.begin()->second; n     q.erase(q.begin()); n     for (int e = first[u]; e != -1; e = nxt[e]) { n       if (cap[e] <= 0) continue; n       v = to[e]; n       long long int new_dist = dist[u] + cost[e] + pot[u] - pot[v]; n       if (new_dist < dist[v]) { n         q.erase(make_pair(dist[v], v)); n         dist[v] = new_dist; n         prv[v] = e; n         q.insert(make_pair(new_dist, v)); n       } n     } n   } n   return prv[t] != -1; n } n inline long long int augment(int s, int t) { n   for (int i = t; i != s; i = to[prv[i] ^ 1]) { n     cap[prv[i]]--; n     cap[prv[i] ^ 1]++; n   } n   --K; n   long long int flowCost = dist[t] - pot[s] + pot[t]; n   for (int i = 0; i < N; i++) n     if (prv[i] != -1) pot[i] += dist[i]; n   return flowCost; n } n long long int mincostmaxflow(int s, int t) { n   long long int flowCost = 0; n   memset(pot, 0, sizeof(pot)); n   while (K > 0 && dijkstra(s, t)) { n     flowCost += augment(s, t); n   } n   return flowCost; n } n int main() { n   set<int> v; n   scanf( %d%d , &N, &K); n   for (int i = 0; i < N; ++i) { n     scanf( %d , &a[i]); n     v.insert(a[i]); n   } n   for (int i = 1; i < N + 1; ++i) scanf( %d , &c[i]); n   init(); n   K = min(K, (int)v.size()); n   for (int i = 0; i < N; ++i) { n     add(0, 2 * i + 1, 1, c[a[i]]); n     add(2 * i + 1, 2 * i + 2, 1, M); n     add(2 * i + 2, 2 * N + 1, 1, 0); n     for (int j = i + 1; j < N; ++j) { n       if (a[i] != a[j]) n         add(2 * i + 2, 2 * j + 1, 1, c[a[j]]); n       else n         add(2 * i + 2, 2 * j + 1, 1, 0); n     } n   } n   long long int n = N; n   N = 2 * N + 2; n   printf( %lld n , mincostmaxflow(0, N - 1) - M * n); n } n


#include <bits/stdc++.h> n using namespace std; n char st[300010], opt[10]; n int n, q; n int main() { n   int i, j; n   scanf( %d%d , &n, &q); n   scanf( %s , st); n   int cnt = 0, ans = 0; n   for (i = 0; i < n; ++i) { n     if (st[i] ==  . ) n       cnt++; n     else { n       ans += max(cnt - 1, 0); n       cnt = 0; n     } n   } n   ans += max(cnt - 1, 0); n   char opt[4]; n   int pos; n   while (q--) { n     scanf( %d%s , &pos, opt); n     pos--; n     if (st[pos] ==  . ) { n       if (pos + 1 < n && st[pos + 1] ==  . ) ans--; n       if (pos - 1 >= 0 && st[pos - 1] ==  . ) ans--; n     } n     if (opt[0] ==  . ) { n       if (pos + 1 < n && st[pos + 1] ==  . ) ans++; n       if (pos - 1 >= 0 && st[pos - 1] ==  . ) ans++; n     } n     st[pos] = opt[0]; n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void chmin(T &t, T f) { n   if (t > f) t = f; n } n template <class T> n void chmax(T &t, T f) { n   if (t < f) t = f; n } n int INF = (1 << 29); n int n, m; n int table[8][8]; n void init() {} n void input() { n   cin >> n >> m; n   for (int i = (0); i < (n); i++) n     for (int j = (0); j < (m); j++) cin >> table[i][j]; n } n void solve() { n   int mask = 1 << m; n   int crt[1 << 8], nxt[1 << 8]; n   for (int i = (0); i < (mask); i++) crt[i] = INF; n   crt[0] = 0; n   for (int i = (0); i < (n); i++) n     for (int j = (0); j < (m); j++) { n       for (int k = (0); k < (mask); k++) nxt[k] = INF; n       for (int k = (0); k < (mask); k++) { n         if (k & 1) chmin(nxt[k >> 1], crt[k]); n         if (!(k & 3) && j < m - 1) { n           int add = (table[i][j] == table[i][j + 1] ? 0 : 1); n           chmin(nxt[(k >> 1) | 1], crt[k] + add); n         } n         if (!(k & 1) && i < n - 1) { n           int add = (table[i][j] == table[i + 1][j] ? 0 : 1); n           chmin(nxt[(k >> 1) | (1 << (m - 1))], crt[k] + add); n         } n       } n       swap(crt, nxt); n     } n   cout << crt[0] << endl; n } n int main() { n   init(); n   input(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   int res1 = max((3 * a) / 10, (a - (a / 250) * c)); n   int res2 = max((3 * b) / 10, (b - (b / 250) * d)); n   if (res1 > res2) n     cout <<  Misha  << endl; n   else if (res1 < res2) n     cout <<  Vasya  << endl; n   else n     cout <<  Tie  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long func(long long a, long long b, long long c) { n   if (a % 2 != b % 2) return (1ll << 60); n   long long x = min(a + c, b + c); n   return a + b + c - x; n } n int main(void) { n   int i; n   long long a, b, c; n   cin >> a >> b >> c; n   long long ans = (1ll << 60); n   ans = min(ans, func(a, b, c)); n   ans = min(ans, func(b, c, a)); n   ans = min(ans, func(c, a, b)); n   if (ans == (1ll << 60)) return -1; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (a < b) swap(a, b); n   while (b) { n     long long t = b; n     b = a % b; n     a = t; n   } n   return a; n } n long long mul(long long a, long long b, long long m) { n   long long q = (long double)a * (long double)b / (long double)m; n   long long r = a * b - q * m; n   return (r + 5 * m) % m; n } n long long extendedEuclides(long long a, long long b, long long &x, n                            long long &y) { n   long long xx = y = 0; n   long long yy = x = 1; n   while (b) { n     long long q = a / b; n     long long t = b; n     b = a % b; n     a = t; n     t = xx; n     xx = x - q * xx; n     x = t; n     t = yy; n     yy = y - q * yy; n     y = t; n   } n   return a; n } n pair<long long, long long> chineseRemainder(long long x, long long y, n                                             long long a, long long b) { n   if (a == -1 || b == -1 || x == -1 || y == -1) return make_pair(0, -1); n   long long s, t; n   long long d = extendedEuclides(x, y, s, t); n   if (a % d != b % d) return make_pair(0, -1); n   long long mod = x * (y / d); n   return make_pair((a + mul(mul(x, s, mod), (b - a) / d, mod) + 5 * mod) % mod, n                    x * (y / d)); n } n pair<long long, long long> seg[801000]; n pair<long long, long long> arr[100100]; n void build(int l, int r, int pos) { n   if (l == r) { n     seg[pos] = arr[l]; n     return; n   } n   int m = (l + r) / 2; n   build(l, m, 2 * pos + 1); n   build(1 + m, r, 2 * pos + 2); n   seg[pos] = chineseRemainder(seg[2 * pos + 1].second, seg[2 * pos + 2].second, n                               seg[2 * pos + 1].first, seg[2 * pos + 2].first); n } n pair<long long, long long> qry(int l, int r, int pos, int ql, int qr) { n   if (qr >= r && ql <= l) return seg[pos]; n   if (qr < l || ql > r) return make_pair(0, 1); n   pair<long long, long long> pr, pl; n   pl = qry(l, (l + r) / 2, 2 * pos + 1, ql, qr); n   pr = qry((l + r) / 2 + 1, r, 2 * pos + 2, ql, qr); n   return chineseRemainder(pl.second, pr.second, pl.first, pr.first); n } n vector<pair<long long, long long> > arrs[100100]; n int ans[100100]; n int kss[100100]; n int solve(int k) { n   build(0, k - 1, 0); n   int l = 0, r = 0, mxm = 1; n   while (l < k && r < k) { n     if (qry(0, k - 1, 0, l, r).second != -1) mxm = max(mxm, r - l + 1); n     if (r == k - 1) n       l++; n     else if (qry(0, k - 1, 0, l, r).second != -1) n       r++; n     else n       l++; n     if (qry(0, k - 1, 0, l, r).second != -1) mxm = max(mxm, r - l + 1); n   } n   r = k - 1; n   while (l < k) { n     if (qry(0, k - 1, 0, l, r).second != -1) mxm = max(mxm, r - l + 1); n     l++; n   } n   return mxm; n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> kss[i]; n     for (int j = 0; j < kss[i]; j++) { n       int a; n       cin >> a; n       arrs[a].push_back(make_pair(j, i)); n     } n   } n   int i = 1; n   while (i <= m) { n     if (arrs[i].size() == 0) { n       cout << 0 << endl; n       i++; n       continue; n     } n     pair<int, int> last = arrs[i][0]; n     int j = 0, h = 1; n     arr[0] = make_pair(arrs[i][0].first, kss[arrs[i][0].second]); n     while (j < arrs[i].size()) { n       j++; n       if (j == arrs[i].size() || n           arrs[i][j].second != arrs[i][j - 1].second + 1) { n         ans[i] = max(ans[i], solve(h)); n         h = 0; n         if (j != arrs[i].size()) n           arr[h++] = make_pair(arrs[i][j].first, kss[arrs[i][j].second]); n       } else { n         arr[h++] = make_pair(arrs[i][j].first, kss[arrs[i][j].second]); n       } n     } n     cout << ans[i] << endl; n     i++; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n template <class A> n istream& operator>>(istream& in, vector<A>& a) { n   for (A& i : a) in >> i; n   return in; n } n template <class A> n istream& operator>>(istream& in, vector<vector<A>>& a) { n   for (auto& i : a) n     for (auto& j : i) in >> j; n   return in; n } n template <class A> n ostream& operator<<(ostream& in, vector<A>& a) { n   for (A& i : a) in << i <<    ; n   in << endl; n   return in; n } n template <class A> n ostream& operator<<(ostream& in, vector<vector<A>>& a) { n   for (auto& i : a) { n     for (auto& j : i) in << j <<    ; n     in << endl; n   } n   return in; n } n const long long mod = 1e9 + 7; n long long power(long long x, long long y) { n   long long res = 1; n   while (y > 0) { n     if (y & 1) res = (res * x) % mod; n     y >>= 1; n     x = (x * x) % mod; n   } n   return (res % mod); n } n void solve() { n   int n, m; n   cin >> n >> m; n   vector<int> v(n), ans; n   cin >> v; n   map<int, bool> mp; n   for (int i = 0; i < n; i++) { n     mp[v[i]] = true; n   } n   for (int i = 1; 1; i++) { n     if (mp[i] == 0 && m - i >= 0) { n       ans.push_back(i); n       m -= i; n       mp[i] = true; n     } n     if (m < i) { n       break; n     } n   } n   cout << ans.size() <<   n  << ans; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long tc = 1; n   for (long long t = 1; t <= tc; t++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int dx[4] = {-1, 0, 1, 0}; n int dy[4] = {0, 1, 0, -1}; n vector<string> museum; n vector<vector<int> > pictures; n vector<vector<bool> > used; n queue<pair<int, int> > line; n vector<pair<int, int> > req; n void BFS(int& i, int& j) { n   line.push(make_pair(i, j)); n   used[i][j] = true; n   int cnt = 0; n   req.clear(); n   while (!line.empty()) { n     int x = line.front().first; n     int y = line.front().second; n     line.pop(); n     req.push_back(make_pair(x, y)); n     for (int z = 0; z < 4; z++) { n       int newI = x + dx[z]; n       int newJ = y + dy[z]; n       if (newI >= 0 && newI < n && newJ >= 0 && newJ < m && n           museum[newI][newJ] ==  * ) { n         cnt++; n       } n     } n     for (int z = 0; z < 4; z++) { n       int newI = x + dx[z]; n       int newJ = y + dy[z]; n       if (newI >= 0 && newI < n && newJ >= 0 && newJ < m && n           museum[newI][newJ] ==  .  && !used[newI][newJ]) { n         used[newI][newJ] = true; n         line.push(make_pair(newI, newJ)); n       } n     } n   } n   for (int z = 0; z < req.size(); z++) { n     pictures[req[z].first][req[z].second] = cnt; n   } n } n int main() { n   cin >> n >> m >> k; n   museum.resize(n); n   used.resize(n, vector<bool>(m, false)); n   for (int i = 0; i < n; i++) { n     cin >> museum[i]; n   } n   pictures.resize(n, vector<int>(m, -1)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (museum[i][j] ==  .  && pictures[i][j] == -1) { n         BFS(i, j); n       } n     } n   } n   for (int i = 0; i < k; i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     printf( %d n , pictures[x][y]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n long long mult(long long a, long long b, long long limit) { n   long long x = 0, y = a; n   while (b > 0) { n     if (b % 2 == 1) { n       x = (x + y); n       if (x >= limit) return -1; n     } n     y = (y * 2); n     b /= 2; n     if (y >= limit && b > 0) return -1; n   } n   return x; n } n long long calc(long long teams) { n   long long ret = 0; n   while (teams % 2 == 0) { n     long long hlf = teams / 2; n     ret += hlf; n     teams = hlf; n     if (ret > n) return -1; n   } n   long long prt = mult(teams, teams - 1, 4 * n); n   if (prt == -1) return -1; n   prt /= 2; n   ret += prt; n   if (ret > n) return -1; n   return ret; n } n int main() { n   cin >> n; n   vector<long long> sol; n   for (long long pw2 = 1, cnt = 0; pw2 <= n * 4; pw2 *= 2, cnt++) { n     long long lo = 0, hi = ceil(sqrt(4 * n)); n     long long found = -1; n     while (lo <= hi) { n       long long mid = (lo + hi) >> 1; n       long long odd_part = 2 * mid + 1; n       long long tot = mult(odd_part, pw2, 4 * n); n       if (tot == -1) { n         hi = mid - 1; n         continue; n       } n       long long calc_runs = calc(tot); n       if (calc_runs == -1) { n         hi = mid - 1; n         continue; n       } else if (calc_runs == n) { n         found = tot; n         break; n       } else if (calc_runs < n) n         lo = mid + 1; n       else n         hi = mid - 1; n     } n     if (found != -1) sol.push_back(found); n   } n   if (sol.size() == 0) { n     printf( -1 n ); n     return 0; n   } n   sort(sol.begin(), sol.end()); n   for (int i = 0; i < sol.size(); ++i) cout << sol[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int b[200010]; n map<int, long long> S; n long long solve() { n   S.clear(); n   long long df, ans = 0; n   for (int i = 0; i < n; i++) { n     df = b[i] - i; n     S[df] += (long long)b[i]; n   } n   map<int, long long>::iterator it; n   for (it = S.begin(); it != S.end(); it++) ans = max(ans, it->second); n   return ans; n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> b[i]; n   cout << solve() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1e6 + 5; n vector<vector<pair<long long, long long> > > dis(maxn); n long long flag[maxn]; n long long c1, c2; n bool ok; n void dfs(long long index, long long p) { n   flag[index] = p; n   for (long long i = 0; i < dis[index].size(); i++) { n     if (flag[dis[index][i].first] != -1 && n         flag[dis[index][i].first] != (dis[index][i].second == p)) n       ok = false; n     if (flag[dis[index][i].first] == -1) { n       if (dis[index][i].second == p) { n         c1++; n       } else n         c2++; n       dfs(dis[index][i].first, dis[index][i].second == p); n     } n   } n } n signed main() { n   long long t; n   cin >> t; n   memset(flag, -1, sizeof(flag)); n   while (t--) { n     long long n, m; n     cin >> n >> m; n     long long x, y; n     string s; n     while (m--) { n       cin >> x >> y >> s; n       if (s[0] ==  c ) { n         dis[x].push_back({y, 1}); n         dis[y].push_back({x, 1}); n       } else { n         dis[x].push_back({y, 0}); n         dis[y].push_back({x, 0}); n       } n     } n     long long ans = 0; n     ok = true; n     for (long long i = 1; i <= n; i++) { n       if (flag[i] == -1) { n         c1 = 1, c2 = 0; n         dfs(i, 1); n         ans += max(c1, c2); n       } n     } n     for (long long i = 1; i <= n; i++) { n       dis[i].clear(); n       flag[i] = -1; n     } n     if (!ok) n       cout <<  -1  << endl; n     else n       cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, Q; n vector<int> E[200005]; n int P[21][200005], D[200005]; n void dfs(int cur) { n   for (__typeof(E[cur].begin()) it = E[cur].begin(); it != E[cur].end(); it++) n     if (*it != P[0][cur]) D[*it] = D[cur] + 1, P[0][*it] = cur, dfs(*it); n } n int lca(int a, int b) { n   int ret = 0, i, aa = a, bb = b; n   if (D[aa] > D[bb]) swap(aa, bb); n   for (i = 19; i >= 0; i--) n     if (D[bb] - D[aa] >= 1 << i) bb = P[i][bb]; n   for (i = 19; i >= 0; i--) n     if (P[i][aa] != P[i][bb]) aa = P[i][aa], bb = P[i][bb]; n   return (aa == bb) ? aa : P[0][aa]; n } n int dist(int a, int b) { return D[a] + D[b] - 2 * D[lca(a, b)]; } n int hoge(int S, int T, int F) { n   int ST = lca(S, T); n   int FT = lca(F, T); n   int SF = lca(S, F); n   int STD = dist(S, T); n   int FTD = dist(F, T); n   int SFD = dist(S, F); n   if (SFD + STD == FTD) return STD + 1; n   if (SFD + FTD == STD) return FTD + 1; n   if (D[ST] > D[SF]) { n     return D[T] - D[ST] + 1; n   } else if (D[FT] > D[SF]) { n     return D[T] - D[FT] + 1; n   } else { n     return dist(SF, T) + 1; n   } n } n void solve() { n   int i, j, k, l, r, x, y; n   string s; n   cin >> N >> Q; n   for (i = 0; i < (N - 1); i++) { n     cin >> x; n     E[i + 1].push_back(x - 1); n     E[x - 1].push_back(i + 1); n   } n   dfs(0); n   for (i = 0; i < (19); i++) n     for (x = 0; x < (N); x++) P[i + 1][x] = P[i][P[i][x]]; n   while (Q--) { n     int A, B, C; n     cin >> A >> B >> C; n     A--, B--, C--; n     cout << max({hoge(A, B, C), hoge(A, C, B), hoge(B, A, C)}) << endl; n   } n } n int main(int argc, char** argv) { n   string s; n   int i; n   if (argc == 1) ios::sync_with_stdio(false), cin.tie(0); n   for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s +=   n ; n   for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> p[2005 * 2005]; n char a[2005][2005], cg[4] = { v ,  < ,  ^ ,  > }; n int n, m, mox[4] = {-1, 0, 1, 0}, moy[4] = {0, 1, 0, -1}; n int ispoint(int x, int y) { n   return (x >= 0 && x < n && y >= 0 && y < m && a[x][y] ==  . ); n } n int deg(int x, int y) { n   int ans = 0; n   for (int i = 0; i < 4; i++) { n     int fx = x + mox[i]; n     int fy = y + moy[i]; n     if (ispoint(fx, fy)) ans++; n   } n   return ans; n } n int main() { n   while (scanf( %d %d , &n, &m) != EOF) { n     for (int i = 0; i < n; i++) scanf( %s , a[i]); n     int h = 0, sum = 0; n     for (int i = 0; i < n; i++) n       for (int j = 0; j < m; j++) n         if (ispoint(i, j) && deg(i, j) == 1) p[sum++] = make_pair(i, j); n     for (; h < sum; h++) { n       int x = p[h].first, y = p[h].second; n       for (int i = 0; i < 4; i++) { n         int fx = x + mox[i]; n         int fy = y + moy[i]; n         if (ispoint(fx, fy)) { n           a[x][y] = cg[i]; n           a[fx][fy] = cg[i ^ 2]; n           for (int i = 0; i < 4; i++) { n             int ffx = fx + mox[i]; n             int ffy = fy + moy[i]; n             if (ispoint(ffx, ffy) && deg(ffx, ffy) == 1) n               p[sum++] = make_pair(ffx, ffy); n           } n           break; n         } n       } n     } n     int flag = 0; n     for (int i = 0; i < n && !flag; i++) n       for (int j = 0; j < m && !flag; j++) n         if (a[i][j] ==  . ) flag = 1; n     if (flag == 1) n       printf( Not unique n ); n     else { n       for (int i = 0; i < n; i++, printf(  n )) n         for (int j = 0; j < m; j++) printf( %c , a[i][j]); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const long long oo = 1e9 + 7; n const int mod = 1e9 + 7; n double x, y, tmp; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> x >> y; n   tmp = log(x) / log(y); n   if (tmp * y * 1.00 < x) n     cout <<  < ; n   else if (tmp * y * 1.00 > x) n     cout <<  > ; n   else n     cout <<  = ; n } n


#include <bits/stdc++.h> n using namespace std; n void myassert(bool expr) { n   if (!expr) abort(); n } n const int64_t posinf = 9223372036854775807LL; n const int64_t neginf = 9223372036854775808LL; n const int64_t bigprime = (1LL << 61) - 1LL; n const int64_t mult = 199; n const int64_t mult2 = 3571; n const int64_t mod = 1000000007LL; n const int64_t mod2 = 1000000009LL; n const long double pi = 3.1415926535897932384626433832795L; n template <typename T> n T gcd(T a, T b) { n   while (b) { n     a %= b; n     swap(a, b); n   } n   return a; n } n char mtx[1024][1024]; n int main() { n   cin.sync_with_stdio(false); n   cout.sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> mtx[i]; n     for (int j = 0; j < m; j++) n       if (mtx[i][j] ==  b ) mtx[i][j] =  f ; n   } n   char lets[4] = { a ,  b ,  c ,  d }; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (mtx[i][j] ==  f ) { n         cout <<  NO ; n         return 0; n       } n       if (mtx[i][j] !=  w ) continue; n       if (mtx[i][j + 1] ==  f  && mtx[i][j + 2] ==  w ) { n         mtx[i][j] = mtx[i][j + 1] = mtx[i][j + 2] = n             lets[(i + (j + 1) / 2 * 2) % 4]; n       } else if (mtx[i + 1][j] ==  f  && mtx[i + 2][j] ==  w ) { n         mtx[i][j] = mtx[i + 1][j] = mtx[i + 2][j] = n             lets[(i + 1 + j / 2 * 2) % 4]; n       } else { n         cout <<  NO ; n         return 0; n       } n     } n   } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (mtx[i][j] ==  f  || mtx[i][j] ==  w ) { n         cout <<  NO ; n         return 0; n       } n   cout <<  YES  << endl; n   for (int i = 0; i < n; i++) cout << mtx[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 3.14159265358979; n vector<pair<int, int> > A[100001]; n int main() { n   int n, m, a, b; n   scanf( %d%d , &n, &m); n   priority_queue<pair<int, int>, vector<pair<int, int> >, n                  greater<pair<int, int> > > n       q; n   vector<int> ans; n   for (int i = 0; i < m; ++i) { n     scanf( %d%d , &a, &b); n     A[min(a, n + 1 - b)].push_back(make_pair(max(a, n + 1 - b), i + 1)); n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j < A[i].size(); ++j) { n       q.push(A[i][j]); n     } n     while (!q.empty()) { n       pair<int, int> t = q.top(); n       if (t.first < i) n         q.pop(); n       else n         break; n     } n     if (!q.empty()) { n       ans.push_back(q.top().second); n       q.pop(); n     } n   } n   printf( %d n , ans.size()); n   for (int i = 0; i < ans.size(); ++i) { n     printf( %d  , ans[i]); n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, e; n   cin >> s >> e; n   int h1, m1, h2, m2; n   h1 = (s[0] -  0 ) * 10 + (s[1] -  0 ); n   h2 = (e[0] -  0 ) * 10 + (e[1] -  0 ); n   m1 = (s[3] -  0 ) * 10 + (s[4] -  0 ); n   m2 = (e[3] -  0 ) * 10 + (e[4] -  0 ); n   long long int total1 = h1 * 60 + m1; n   long long int total2 = h2 * 60 + m2; n   long long int dif = (total2 - total1) / 2; n   int ansh = dif / 60; n   int ansm = dif % 60; n   int fh = h1 + ansh; n   int fm = m1 + ansm; n   if (fm >= 60) { n     fh += 1; n     fm %= 60; n   } n   if (fh > 9) { n     cout << fh <<  : ; n   } else { n     cout <<  0  << fh <<  : ; n   } n   if (fm > 9) { n     cout << fm << endl; n   } else { n     cout <<  0  << fm << endl; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0, f = 1; n   char c = getchar(); n   for (; !isdigit(c); c = getchar()) n     if (c ==  - ) f = -1; n   for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ; n   return x * f; n } n namespace Sa { n struct Node { n   double x, y; n   Node() {} n   Node(double x, double y) : x(x), y(y) {} n   Node operator-(Node a) const { return Node(x - a.x, y - a.y); } n   double operator*(Node a) const { return x * a.y - y * a.x; } n } a[1], b[1]; n double l, r, Mid, Ans, Now, x, y, Ax, T, N2; n int i, j, k, n, m; n inline double Getrand() { return rand() % 1000 / 1000.0; } n inline double Gety(double x) { n   for (int i = 1; i < n; i++) n     if (a[i + 1].x > x) n       return (a[i + 1].y - a[i].y) / (a[i + 1].x - a[i].x) * (x - a[i].x) + n              a[i].y; n } n inline bool Check(Node A) { n   for (int i = 1; i < n; i++) n     if ((A - a[i]) * (A - a[i + 1]) < 0) return 0; n   return 1; n } n inline double Calc(double x) { n   double l = 0, r = 0, Mid; n   while (r - l > 1) { n     Mid = (l + r) / 2.0; n     if (Check(Node(x, Mid))) n       r = Mid; n     else n       l = Mid; n   } n   l -= Gety(x); n   if (l < Ans) Ans = l, Ax = x; n   return l; n } n inline void SA(double T) { n   for (; T > 0.00001; T *= 0.99) { n     y = x + T * (Getrand() * 2 - 1); n     if (y < a[1].x || y > a[n].x) continue; n     N2 = Calc(x) - Calc(y); n     if (N2 > 0 || exp(N2 / T) > Getrand()) x = y; n   } n   for (int i = 1; i <= 1000; i++) { n     y = Ax + T * (Getrand() * 2 - 1); n     if (y < a[1].x || y > a[n].x) continue; n     Calc(y); n   } n } n }  // namespace Sa n int main() { n   int t = read(); n   while (t--) { n     int n = read(); n     double T = n; n     if (n * n < 4 * n) { n       printf( N n ); n       continue; n     } n     double a = (T + sqrt(T * T - 4.0 * T)) / 2.0; n     double b = (T - sqrt(T * T - 4.0 * T)) / 2.0; n     printf( Y %.10f %.10f n , a, b); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     long long n, m; n     cin >> n >> m; n     long long sum = 0, cyl = 0; n     long long k = (n / m); n     for (int i = 1; i <= 10; ++i) { n       cyl += (m * i) % 10; n     } n     sum = (k / 10) * cyl; n     cyl = 0; n     for (int i = 1; i <= k % 10; ++i) { n       cyl += m * (i) % 10; n     } n     cout << sum + cyl << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d; n   scanf( %d%d%d%d , &a, &b, &c, &d); n   if (a < max(c, d) || b < max(c, d)) { n     printf( -1 n ); n   } else if (c == 0 && d == 0) { n     for (int i = 0; i < a; i++) printf( 4 ); n     for (int i = 0; i < b; i++) printf( 7 ); n     printf(  n ); n   } else if (c == d) { n     if (a == b && a == c) { n       printf( -1 n ); n     } else if (a == c) { n       for (int i = 0; i < c; i++) printf( 74 ); n       for (int i = 0; i < b - c; i++) printf( 7 ); n       printf(  n ); n     } else if (b == c) { n       for (int i = 0; i < a - c; i++) printf( 4 ); n       for (int i = 0; i < c; i++) printf( 74 ); n       printf(  n ); n     } else { n       for (int i = 0; i < a - c - 1; i++) printf( 4 ); n       for (int i = 0; i < c; i++) printf( 47 ); n       for (int i = 0; i < b - c; i++) printf( 7 ); n       printf( 4 ); n       printf(  n ); n     } n   } else if (c - d == 1) { n     for (int i = 0; i < a - c; i++) printf( 4 ); n     for (int i = 0; i < c; i++) printf( 47 ); n     for (int i = 0; i < b - c; i++) printf( 7 ); n     printf(  n ); n   } else if (d - c == 1) { n     printf( 74 ); n     for (int i = 0; i < a - d; i++) printf( 4 ); n     for (int i = 0; i < d - 2; i++) printf( 74 ); n     for (int i = 0; i < b - d; i++) printf( 7 ); n     printf( 74 ); n     printf(  n ); n   } else n     printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j, k, n, m; n   while (cin >> n >> k) { n     if (n * (n - 1) / 2 <= k) { n       puts( no solution ); n     } else { n       for (i = 0; i < n; i++) printf( 0 %d n , i); n     } n   } n   return 0; n } n