#include <bits/stdc++.h> n using namespace std; n bool psort(std::vector<long long int> p1, std::vector<long long int> p2) { n   return p1[0] < p2[0]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, m; n   cin >> n; n   std::vector<vector<long long int> > v(n, vector<long long int>(3)); n   for (int i = 0; i < n; ++i) { n     cin >> v[i][0]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> v[i][1]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> v[i][2]; n   } n   sort(v.begin(), v.end(), psort); n   cin >> m; n   long long int start[4] = {0}; n   for (int i = 0; i < m; ++i) { n     long long int k, t, flag = 0; n     cin >> t; n     while (start[t] < n) { n       if (v[start[t]][0] != -1) { n         if (v[start[t]][1] == t || v[start[t]][2] == t) { n           cout << v[start[t]][0] <<    ; n           v[start[t]][0] = -1; n           start[t]++; n           flag = 1; n           break; n         } n       } n       start[t]++; n     } n     if (flag == 0) cout << -1 <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string fld; n   cin >> fld; n   int l = 1, r = 2 * n; n   int ans = -1; n   while (l <= r) { n     int mid = (l + r) >> 1; n     int posbl = 1; n     int back = -1, flag = 0; n     for (int i = 0; i < n; i++) { n       if (fld[i] ==  * ) n         if (i > back && flag == 0) back = i, flag = 1; n       if (fld[i] ==  P ) { n         if (back == -1) { n           back = i + mid; n         } else { n           if (i <= back) { n             back = max(back, i + mid); n           } else { n             if (flag == 1) { n               if (i - back > mid) { n                 posbl = 0; n                 break; n               } else if (i - back <= mid && mid <= (2 * (i - back))) n                 back = max(i, (mid - (i - back)) / 2 + i); n               else { n                 back = n                     max(mid - (2 * (i - back)) + i, (mid - (i - back)) / 2 + i); n               } n             } else { n               back = i + mid; n             } n           } n         } n         flag = 0; n       } n     } n     if (flag == 1) posbl = 0; n     if (posbl == 1) { n       r = mid - 1; n       ans = mid; n     } else n       l = mid + 1; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n char s[N]; n char t1[7] =  twone ; n char t2[4] =  one ; n char t3[4] =  two ; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %s , s); n     int ans = 0; n     int len = strlen(s); n     for (int i = 0; i < len; i++) { n       if (s[i] ==  t ) { n         int f = 0; n         for (int j = i; j <= i + 4; j++) { n           if (s[j] != t1[j - i]) { n             f = 1; n             break; n           } n         } n         if (f == 0) { n           s[i + 2] =  - ; n           ans++; n         } n       } n     } n     for (int i = 0; i < len; i++) { n       if (s[i] ==  t ) { n         int f = 0; n         for (int j = i; j <= i + 2; j++) { n           if (s[j] != t3[j - i]) { n             f = 1; n             break; n           } n         } n         if (f == 0) { n           s[i + 1] =  - ; n           ans++; n         } n       } n     } n     for (int i = 0; i < len; i++) { n       if (s[i] ==  o ) { n         int f = 0; n         for (int j = i; j <= i + 2; j++) { n           if (s[j] != t2[j - i]) { n             f = 1; n             break; n           } n         } n         if (f == 0) { n           s[i + 1] =  - ; n           ans++; n         } n       } n     } n     printf( %d n , ans); n     for (int i = 0; i < len; i++) n       if (s[i] ==  - ) printf( %d  , i + 1); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   long long int n, m = 0, p = 0, cnt = 0, f = 0; n   cin >> n; n   long long int a[n]; n   for (long long int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] < 0) { n       m++; n     } n     if (a[i] > 0) p++; n     if (a[i] < 0) { n       cnt = cnt - a[i] - 1; n     } n     if (a[i] > 0) { n       cnt = cnt + a[i] - 1; n     } n     if (a[i] == 0) { n       cnt = cnt + 1; n       f++; n     } n   } n   if (f and m % 2 == 0) cout << cnt << endl; n   if (f and m % 2 != 0) cout << cnt << endl; n   if (!f and m % 2 == 0) cout << cnt << endl; n   if (!f and m % 2 != 0) cout << cnt + 2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int A[600006]; n vector<int> G[600006]; n int dfn[600006], bc[600006], clo; n int g[600006][20], dep[600006]; n void dfs(int u) { n   dfn[u] = ++clo, bc[clo] = u; n   if (u == 1) dep[u] = 1; n   for (int v : G[u]) { n     g[v][0] = u; n     dep[v] = dep[u] + 1; n     for (int k = (1), kend = (18); k <= kend; ++k) n       if (g[g[v][k - 1]][k - 1]) n         g[v][k] = g[g[v][k - 1]][k - 1]; n       else n         break; n     dfs(v); n   } n } n int lca(int u, int v) { n   if (dep[u] < dep[v]) swap(u, v); n   if (dep[u] != dep[v]) n     for (int k = (18), kend = (0); k >= kend; --k) n       if (dep[g[u][k]] >= dep[v]) u = g[u][k]; n   if (u == v) return u; n   for (int k = (18), kend = (0); k >= kend; --k) n     if (g[u][k] != g[v][k]) u = g[u][k], v = g[v][k]; n   return g[u][0]; n } n int dis(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } n set<int> S; n void solve() { n   cin >> n; n   ++n; n   for (int i = (2), iend = (n); i <= iend; ++i) { n     int p; n     scanf( %d , &p); n     G[p].push_back(i); n   } n   dfs(1); n   int s = 1, t = 1, lt = 0; n   auto wkr = [&](int x) { return max(dis(x, s), dis(x, t)); }; n   S.insert(1); n   for (int i = (2), iend = (n); i <= iend; ++i) { n     int is = dis(i, s), it = dis(i, t), flg = 0; n     if (is >= lt && is >= it) n       t = i, lt = is, flg = 1; n     else if (it >= lt && it >= is) n       s = i, lt = it, flg = 1; n     if (!flg) { n       printf( %d n , (int)S.size()); n       continue; n     } n     auto tr = S.lower_bound(dfn[i]), st = tr; n     while (tr != S.end()) { n       if (wkr(bc[*tr]) < lt) n         ++tr; n       else n         break; n     } n     if (tr == S.end()) { n       auto sr = S.begin(); n       while (sr != st) { n         if (wkr(bc[*sr]) < lt) n           ++sr; n         else n           break; n       } n       S.erase(S.begin(), sr); n     } n     S.erase(st, tr); n     tr = S.lower_bound(dfn[i]), st = tr; n     while (tr != S.begin()) { n       --tr; n       if (wkr(bc[*tr]) >= lt) { n         ++tr; n         break; n       } n     } n     if (tr == S.begin()) { n       auto sr = S.end(); n       while (sr != st) { n         --sr; n         if (wkr(bc[*sr]) >= lt) { n           ++sr; n           break; n         } n       } n       S.erase(sr, S.end()); n     } n     S.erase(tr, st); n     S.insert(dfn[i]); n     printf( %d n , (int)S.size()); n   } n } n signed main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n struct fenwick { n   long long n, fen[100005]; n   void resize(int _n) { n     n = _n + 1; n     memset(fen, 0, sizeof fen); n   } n   int query(int ind) { n     ind++; n     int ans = 0; n     while (ind >= 1) { n       ans += fen[ind]; n       ind -= ind & -ind; n     } n     return ans; n   } n   void update(int ind, int val) { n     ind++; n     while (ind <= n) { n       fen[ind] += val; n       ind += ind & -ind; n     } n   } n   int query(int l, int r) { return query(r) - query(l - 1); } n }; n const int maxn = 100001; n int n, l, w, x, y, sz[maxn]; n long long res; n bool blocked[maxn]; n vector<pair<int, int> > c, v[maxn]; n fenwick tree; n void dfs(int node, int pnode, int curw, int curl) { n   c.push_back(make_pair(curw, curl)); n   for (auto &i : v[node]) n     if (i.first != pnode && !blocked[i.first]) n       dfs(i.first, node, curw + i.second, curl + 1); n } n long long solve(int node, int curw, int curl) { n   long long ret = 0; n   c.clear(); n   dfs(node, 0, curw, curl); n   sort(c.begin(), c.end()); n   for (auto &i : c) tree.update(i.second, 1); n   for (int i = 0, j = c.size() - 1; i < c.size(); i++) { n     while (i <= j && c[i].first + c[j].first > w) n       tree.update(c[j--].second, -1); n     if (j < i) break; n     tree.update(c[i].second, -1); n     ret += tree.query(l - c[i].second); n   } n   return ret; n } n int dfsz(int node, int pnode) { n   sz[node] = 1; n   for (auto &i : v[node]) n     if (i.first != pnode && !blocked[i.first]) sz[node] += dfsz(i.first, node); n   return sz[node]; n } n int find(int node, int pnode, int n) { n   for (auto &i : v[node]) n     if (i.first != pnode && !blocked[i.first] && 2 * sz[i.first] > n) n       return find(i.first, node, n); n   return node; n } n void decompose(int node) { n   int centroid = find(node, 0, dfsz(node, 0)); n   blocked[centroid] = 1; n   res += solve(centroid, 0, 0); n   for (auto &i : v[centroid]) n     if (!blocked[i.first]) res -= solve(i.first, i.second, 1); n   for (auto &i : v[centroid]) n     if (!blocked[i.first]) decompose(i.first); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> l >> w; n   tree.resize(n + 1); n   for (int i = 2; i <= n && cin >> x >> y; i++) { n     v[i].push_back(make_pair(x, y)); n     v[x].push_back(make_pair(i, y)); n   } n   decompose(1); n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 1e5 + 10; n const int N = 5e2 + 10; n using namespace std; n long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); } n long long qpow(long long p, long long q) { n   long long f = 1; n   while (q) { n     if (q & 1) f = f * p % 1000000007; n     p = p * p % 1000000007; n     q >>= 1; n   } n   return f; n } n int n, m, k, t; n vector<int> d[4][11][10]; n char a[maxn], b[maxn]; n int gao(char p) { n   if (p ==  A ) n     return 0; n   else if (p ==  T ) n     return 1; n   else if (p ==  G ) n     return 2; n   else n     return 3; n } n void add(vector<int>& d, int x, int y) { n   while (x < d.size()) d[x] += y, x += x & (-x); n } n int get(vector<int>& d, int x) { n   int ret = 0; n   while (x) ret += d[x], x -= x & (-x); n   return ret; n } n int main() { n   int i, j; n   scanf( %s , a + 1); n   n = strlen(a + 1); n   for (i = 0; i <= (int)3; i++) n     for (j = 1; j <= (int)10; j++) n       for (k = 0; k <= (int)j - 1; k++) d[i][j][k].resize((n + j - 1) / j + 1); n   for (i = 1; i <= n; i++) { n     for (j = 1; j <= 10; j++) { n       add(d[gao(a[i])][j][(i - 1) % j], (i - 1) / j + 1, 1); n     } n   } n   int q; n   scanf( %d , &q); n   while (q--) { n     int op, x, y; n     scanf( %d%d , &op, &x); n     if (op == 1) { n       scanf( %s , b); n       for (i = 1; i <= (int)10; i++) n         add(d[gao(a[x])][i][(x - 1) % i], (x - 1) / i + 1, -1); n       for (i = 1; i <= (int)10; i++) n         add(d[gao(b[0])][i][(x - 1) % i], (x - 1) / i + 1, 1); n       a[x] = b[0]; n     } else { n       scanf( %d%s , &y, b); n       int len = strlen(b), ret = 0; n       for (i = 0; i < len && i < y - x + 1; i++) { n         int mo = (i + x - 1) % len; n         ret += get(d[gao(b[i])][len][mo], (y - 1 - mo) / len + 1) - n                get(d[gao(b[i])][len][mo], (x - 1 + len - 1 - mo) / len); n       } n       printf( %d n , ret); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int i, j, t; n   cin >> t; n   for (long long int aa = 0; aa < t; aa++) { n     long long int n, k, d; n     cin >> n >> k >> d; n     long long int mini = k; n     vector<long long int> v(n); n     map<long long int, long long int> m; n     for (i = 0; i < n; i++) { n       cin >> v[i]; n       if (i < d) { n         long long int ss = m.size(); n         m[v[i]]++; n         if (ss < m.size()) m[v[i]] = 1; n       } n     } n     m.size() < mini ? mini = m.size() : mini = mini; n     for (i = 1; i < n - d + 1; i++) { n       m[v[i - 1]]--; n       if (m[v[i - 1]] == 0) m.erase(v[i - 1]); n       long long int ss = m.size(); n       m[v[i + d - 1]]++; n       if (ss < m.size()) m[v[i + d - 1]] = 1; n       m.size() < mini ? mini = m.size() : mini = mini; n     } n     m.size() < mini ? mini = m.size() : mini = mini; n     cout << mini << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int *p = new int[n + 1], c = 0; n   int *m = new int[n + 1]; n   for (int i = 0; i <= n; i++) { n     p[i] = m[i] = 0; n     c ^= i; n     m[i] = c; n   } n   int f = 0; n   for (int i = 1; i <= n; i++) { n     cin >> p[i]; n     f ^= p[i]; n   } n   int r = 0; n   for (int i = 2; i <= n; i++) { n     int grps = (n / i); n     int rem = n % i; n     if (grps & 1) r ^= m[i - 1]; n     r ^= m[rem]; n   } n   cout << (r ^ f); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 5; n long long a[maxn], b[maxn], dp[maxn]; n int q[maxn]; n int head = 0, tail = 0; n double A(int i, int j) { return dp[i] - dp[j]; } n double B(int i, int j) { return b[i] - b[j]; } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   dp[1] = 0; n   int head = 1, tail = 1; n   q[tail] = 1; n   for (int i = 2; i <= n; i++) { n     while (head < tail && n            A(q[head + 1], q[head]) / B(q[head + 1], q[head]) >= -a[i]) n       head++; n     dp[i] = dp[q[head]] + b[q[head]] * a[i]; n     while (head < tail && A(i, q[tail]) / B(i, q[tail]) >= n                               A(q[tail], q[tail - 1]) / B(q[tail], q[tail - 1])) n       tail--; n     q[++tail] = i; n   } n   cout << dp[n] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_INT = numeric_limits<int>::max(); n const int MAX_UINT = numeric_limits<unsigned int>::max(); n void get_acum(const vector<int>& v, vector<int>& acum) { n   int sum = 0; n   for (unsigned int i = 0; i < v.size(); ++i) { n     acum.push_back(sum); n     sum += v[i]; n   } n   acum.push_back(sum); n } n int solve(vector<int>& A, vector<int>& B, vector<int>& X, unsigned int k) { n   if (A.size() + X.size() < k || B.size() + X.size() < k) { n     return -1; n   } else { n     sort(A.begin(), A.end()); n     sort(B.begin(), B.end()); n     sort(X.begin(), X.end()); n     vector<int> A_acum, B_acum, X_acum; n     get_acum(A, A_acum); n     get_acum(B, B_acum); n     get_acum(X, X_acum); n     int res = MAX_INT; n     unsigned int max_i = min((unsigned int)X.size(), k); n     for (unsigned int i = 0; i < max_i + 1; ++i) { n       if (k - i < A_acum.size() && k - i < B_acum.size()) { n         int total = A_acum[k - i] + B_acum[k - i] + X_acum[i]; n         if (total < res) res = total; n       } n     } n     return res; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   unsigned int n, k; n   cin >> n >> k; n   vector<int> A; n   vector<int> B; n   vector<int> X; n   for (unsigned int i = 0; i < n; ++i) { n     unsigned int ti; n     bool ai, bi; n     cin >> ti >> ai >> bi; n     if (ai && bi) n       X.push_back(ti); n     else if (ai) n       A.push_back(ti); n     else if (bi) n       B.push_back(ti); n   } n   cout << solve(A, B, X, k); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n set<long long int> adj[3005]; n map<long long int, long long int> mq; n long long int done[3005][3005]; n int main() { n   ios_base::sync_with_stdio(false); n   long long int n, m, i, ans = 0; n   cin >> n >> m; n   for (i = 0; i < m; i++) { n     long long int x, y; n     cin >> x >> y; n     adj[x].insert(y); n   } n   for (i = 1; i <= n; i++) { n     for (auto it : adj[i]) { n       for (auto it2 : adj[it]) { n         if (mq.find(it2) != mq.end() && it2 != i) { n           ans += mq[it2]; n         } n         mq[it2]++; n       } n     } n     mq.clear(); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n int main() { n   int a, b, h; n   cin >> a >> b >> h; n   int c = 0; n   while (h != 0) { n     if (c % 2 == 0) { n       h = h - gcd(a, h); n     } else { n       h = h - gcd(b, h); n     } n     c++; n   } n   if (c % 2 == 0) { n     cout << 1 << endl; n   } else n     cout << 0 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000 + 5; n const double EPS = 1e-3; n int n; n int t[MAXN]; n int g(double q) { n   bool ok = true; n   for (int i = 1; i < n; i++) n     if (fabs(double(t[i] - t[i - 1] * q)) > EPS) ok = false; n   if (ok) return 0; n   ok = true; n   for (int i = 2; i < n; i++) n     if (fabs(double(t[i] - t[i - 1] * q)) > EPS) ok = false; n   if (ok) return 1; n   ok = true; n   int prev = t[0]; n   for (int i = 1; i < n; i++) n     if (fabs(double(t[i] - prev * q)) > EPS) { n       if (!ok) return 2; n       ok = false; n     } else n       prev = t[i]; n   return 1; n } n int f() { n   if (n == 1) return 0; n   double q1 = 0, q2 = 0; n   if (t[0] != 0) q1 = double(t[1]) / t[0]; n   if (t[n - 2] != 0) q2 = double(t[n - 1]) / t[n - 2]; n   return min(g(q1), g(q2)); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &t[i]); n   printf( %d n , f()); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e6 + 5; n int a[MAX], run[MAX], ans[MAX]; n char s[MAX]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   scanf( %d , &n); n   scanf( %s , s); n   for (int i = 0; i < n; ++i) { n     if (s[i] ==  0 ) n       a[i] = 0; n     else if (s[i] ==  1 ) n       a[i] = 1; n     else n       a[i] = -1; n   } n   vector<pair<int, int>> blocks; n   int it1 = 0, it2 = 0; n   while (it1 != n) { n     while (it2 < n - 1 && a[it1] == a[it2 + 1]) ++it2; n     blocks.emplace_back(it2 - it1 + 1, a[it1]); n     ++it2; n     it1 = it2; n   } n   for (int i = 0; i < (int)blocks.size(); ++i) { n     if (blocks[i].second != -1) continue; n     if (i == 0) n       blocks[i].second = blocks[i + 1].second; n     else if (i == (int)blocks.size() - 1) n       blocks[i].second = blocks[i - 1].second; n     else if (blocks[i - 1].second == blocks[i + 1].second) n       blocks[i].second = blocks[i - 1].second; n   } n   vector<pair<int, int>> compress; n   it1 = 0; n   it2 = 0; n   while (it1 != (int)blocks.size()) { n     int len = blocks[it1].first; n     while (it2 < (int)blocks.size() - 1 && n            blocks[it1].second == blocks[it2 + 1].second) n       it2++, len += blocks[it2].first; n     compress.emplace_back(len, blocks[it1].second); n     ++it2; n     it1 = it2; n   } n   for (auto block : compress) { n     if (block.second == -1) continue; n     for (int len = 1; len <= block.first; len++) ans[len] += block.first / len; n   } n   int max_size = -1, bef = -2; n   int size = compress.size(); n   for (int i = 0; i < size; i++) { n     int len = compress[i].first; n     int type = compress[i].second; n     if (bef != -1 && type != -1) { n       max_size = len; n       if (i != size - 1) max_size += compress[i + 1].first; n       for (int l = 1; l <= max_size; l++) run[l] = len % l; n     } else if (type == -1) { n       for (int l = 1; l <= max_size; l++) { n         if (run[l] + len >= l) n           run[l] += len, ans[l] += run[l] / l, run[l] %= l; n         else n           run[l] = len; n       } n       int add = 0; n       if (i != (int)size - 1) add += compress[i + 1].first; n       if (i != (int)size - 2) add += compress[i + 2].first; n       for (int l = max_size + 1; l <= len + add; ++l) run[l] = (len % l); n       max_size = len + add; n     } else { n       for (int l = 1; l <= max_size; ++l) { n         if (run[l] + (len % l) >= l) n           run[l] += ((len % l) - l), ans[l]++; n         else n           run[l] += (len % l); n       } n     } n     bef = type; n   } n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read() { n   int f = 1, x = 0; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) { n       f = -1; n     } n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n const int N = 55; n struct W { n   int x, y, tx, ty, id; n } up[N], down[N]; n bool cmp_down(W a, W b) { return a.tx == b.tx ? a.ty < b.ty : a.tx > b.tx; } n bool cmp_up(W a, W b) { return a.x == b.x ? a.y < b.y : a.x < b.x; } n vector<pair<int, int> > ans_fr, ans_to; n bool has[N][N]; n void move_up(int& x, int y) { n   int dx = x; n   has[x][y] = 0; n   has[x - 1][y] = 1; n   ans_fr.push_back(make_pair(dx, y)); n   ans_to.push_back(make_pair(dx - 1, y)); n   --x; n } n void move_down(int& x, int y) { n   int dx = x; n   has[x][y] = 0; n   has[x + 1][y] = 1; n   ans_fr.push_back(make_pair(dx, y)); n   ans_to.push_back(make_pair(dx + 1, y)); n   ++x; n } n void move_left(int x, int& y) { n   int dy = y; n   has[x][y] = 0; n   has[x][y - 1] = 1; n   ans_fr.push_back(make_pair(x, dy)); n   ans_to.push_back(make_pair(x, dy - 1)); n   --y; n } n void move_right(int x, int& y) { n   int dy = y; n   has[x][y] = 0; n   has[x][y + 1] = 1; n   ans_fr.push_back(make_pair(x, dy)); n   ans_to.push_back(make_pair(x, dy + 1)); n   ++y; n } n void move_row(int& x, int& y, int tx, int ty) { n   while (y < ty && has[x][y + 1] == 0) { n     move_right(x, y); n   } n   while (y > ty && has[x][y - 1] == 0) { n     move_left(x, y); n   } n } n void move(int& x, int& y, int tx, int ty) { n   while (x < tx) { n     move_down(x, y); n   } n   while (x > tx) { n     move_up(x, y); n   } n   while (y < ty) { n     move_right(x, y); n   } n   while (y > ty) { n     move_left(x, y); n   } n } n int check(W a) { n   if (a.x == a.tx) { n     return a.y == a.ty ? 0 : 1; n   } else { n     return a.y == a.ty ? 2 : 3; n   } n } n int nx[N], ny[N]; n char mp[N][N]; n int vis[N]; n void write(int n) { n   for (int i = 1; i <= n; i++) { n     cout << mp[i] + 1 << endl; n   } n   puts( ---------------------- ); n } n void deal(int n, int m) { n   if (n == 1) { n     puts( 0 ); n   } else { n     if (m == 1) { n       move(up[1].x, up[1].y, up[1].tx, up[1].ty); n     } else { n       if (cmp_up(up[1], up[2]) == 0) { n         swap(up[1], up[2]); n       } n       if (up[1].x == 2) { n         move_up(up[1].x, up[1].y); n         move_up(up[2].x, up[2].y); n       } else if (up[2].x == 2) { n         move_up(up[2].x, up[2].y); n       } n       switch (check(up[1])) { n         case 0: n           break; n         case 1: n           move(up[2].x, up[2].y, 2, 1); n           move_right(up[1].x, up[1].y); n           break; n         case 2: n           move(up[1].x, up[1].y, up[1].tx, up[1].ty); n           break; n         case 3: n           move(up[1].x, up[1].y, up[1].tx, up[1].ty); n           move_left(up[2].x, up[2].y); n           break; n       } n       move(up[2].x, up[2].y, up[2].tx, up[2].ty); n     } n     cout << ans_fr.size() << endl; n     for (int i = 0; i < ans_fr.size(); i++) { n       int xx1 = ans_fr[i].first, xx2 = ans_to[i].first; n       int yy1 = ans_fr[i].second, yy2 = ans_to[i].second; n       printf( %d %d %d %d n , xx1, yy1, xx2, yy2); n     } n   } n } n int main() { n   int n = read(), m = read(); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       mp[i][j] =  . ; n     } n   } n   for (int i = 1; i <= m; i++) { n     up[i].id = i; n     nx[i] = up[i].x = read(); n     ny[i] = up[i].y = read(); n     has[nx[i]][ny[i]] = 1; n     mp[nx[i]][ny[i]] = i +  0 ; n   } n   for (int i = 1; i <= m; i++) { n     up[i].tx = read(); n     up[i].ty = read(); n   } n   if (n <= 2) { n     deal(n, m); n     return 0; n   } n   copy(up + 1, up + 1 + m, down + 1); n   sort(up + 1, up + 1 + m, cmp_up); n   int cnt = m; n   while (cnt) { n     for (int i = 1; i <= m; i++) { n       if (vis[i] == 0) { n         int id = up[i].id; n         move_row(nx[id], ny[id], 1, i); n         if (ny[id] == i) { n           vis[i] = 1; n           cnt--; n         } n       } n     } n   } n   for (int i = 1; i <= m; i++) { n     int id = up[i].id; n     while (nx[id] > 1) { n       move_up(nx[id], ny[id]); n     } n   } n   for (int i = 1; i <= m; i++) { n     move_down(nx[i], ny[i]); n     move_down(nx[i], ny[i]); n   } n   sort(down + 1, down + 1 + m, cmp_down); n   for (int i = 1; i <= m; i++) { n     int id = down[i].id; n     move_up(nx[id], ny[id]); n     move_row(nx[id], ny[id], 1, i); n     move_up(nx[id], ny[id]); n   } n   for (int i = 1; i <= m; i++) { n     int id = down[i].id; n     while (nx[id] < down[i].tx) { n       move_down(nx[id], ny[id]); n     } n   } n   cnt = m; n   while (cnt) { n     for (int i = 1; i <= m; i++) { n       if (vis[i] == 1) { n         int id = down[i].id; n         move_row(nx[id], ny[id], nx[id], down[i].ty); n         if (ny[id] == down[i].ty) { n           vis[i] = 2; n           cnt--; n         } n       } n     } n   } n   assert(ans_fr.size() == ans_to.size()); n   assert(ans_fr.size() <= 10800); n   cout << ans_fr.size() << endl; n   for (int i = 0; i < ans_fr.size(); i++) { n     int xx1 = ans_fr[i].first, xx2 = ans_to[i].first; n     int yy1 = ans_fr[i].second, yy2 = ans_to[i].second; n     printf( %d %d %d %d n , xx1, yy1, xx2, yy2); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int a[m], b[m]; n   int ndup = n; n   for (int i = 0; i < m; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   sort(a, a + m); n   sort(b, b + m, greater<int>()); n   int mn = 0, mx = 0; n   for (int i = 0; i < m; i++) { n     while (a[i] > 0 && n > 0) { n       mn += a[i]; n       a[i]--; n       n--; n     } n   } n   while (ndup > 0) { n     mx += b[0]; n     b[0]--; n     ndup--; n     sort(b, b + m, greater<int>()); n   } n   cout << mx <<     << mn; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b, c, d, e, f, g, h, i, j, k, l, m, n, t; n   cin >> t; n   while (t--) { n     cin >> n; n     cout << (n + 1) / 2 << endl; n   } n } n


//...START BY DOING WHAT IS NECESSARY, THEN WHAT IS POSSIBLE AND SUDDENLY YOU ARE DOING THE IMPOSSIBLE...  n #include <bits/stdc++.h> n using namespace std; n #define FAST_FURIER ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); n #define pb push_back n #define mk make_pair n #define rep(i,a,N) for(ll i=a;i<N;i++) n #define rrep(i,a,N) for(ll i=a;i>N;i--) n typedef long long ll; n #define M 1000000007 n bool comp(int x,int y) n { n     return x > y; n } n   n   n /*..............................code starts here........................*/ n // C is first won in M n   n   n int main() { n     FAST_FURIER; n     int tt=1; n     cin >> tt; n     ll m,d,k; n    //ll a,b,c; n     while(tt--) n     { n         cin >> d >> k; n         ll x = 0,y = 0; n         int f = 0; n         while(true){ n             ll d1 = pow(x+k,2)+pow(y,2), d2 = pow(x,2)+pow(y+k,2); n             f = abs(f-1); n             if(d1 < d2 and d1 <= d*d){ n                 x += k; n             } n             else if(d2 <= d1 and d2 <= d*d) y += k; n             else break; n         } n         if(f) cout <<  Utkarsh ; n         else cout <<  Ashish ; n         cout << endl; n  n     } n } n   n   n   n // Author : shivam_123 n   n // g++ -std=c++17


#include <bits/stdc++.h> n using namespace std; n long long a, b, c, e[200000], mix, l, r, o, d[200000]; n vector<long long> v[100000], v1; n set<long long> st; n map<long long, long long> mp, mp1; n string s; n pair<int, int> p[100000], p1[100000]; n char ch[10]; n deque<char> dq; n int main() { n   cin >> a; n   for (int i = 0; i < a / 4; i++) n     if ((a - 2 * (i + 1)) % 2 == 0) c++; n   if (a % 4 == 0) { n     cout << c - 1; n   } else n     cout << c; n } n


#include <bits/stdc++.h> n using namespace std; n char s[5000005]; n int len[1000005]; n int idx[1000005]; n int sum[1000005]; n int sz = 0; n int dp[1000005][20]; n int p[1000005]; n int main() { n   int n, r, c; n   scanf( %d%d%d , &n, &r, &c); n   gets(s); n   gets(s); n   int cnt = 0, slen = 0; n   for (int i = 0; s[i]; i++, slen++) { n     if (s[i] ==     && cnt != 0) { n       len[sz] = cnt; n       idx[sz++] = i - cnt; n       cnt = 0; n     } else n       cnt++; n   } n   if (cnt > 0) { n     len[sz] = cnt; n     idx[sz++] = slen - cnt; n     cnt = 0; n   } n   sum[0] = len[0]; n   for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + 1 + len[i]; n   for (int i = n - 1; i >= 0; i--) { n     int id = upper_bound(sum, sum + n, (i > 0 ? sum[i - 1] + 1 : 0) + c) - sum; n     if (id > n) id = n; n     dp[i][0] = id - i; n   } n   for (int j = 2, t = 1; j <= r; j *= 2, t++) { n     for (int i = n - 1; i >= 0; i--) n       dp[i][t] = dp[i][t - 1] + dp[i + dp[i][t - 1]][t - 1]; n   } n   int maid = -1, ma = 0; n   for (int i = 0; i < n; i++) { n     int idx = i, rr = r, cur = 0; n     for (int j = 20; j >= 0; j--) { n       if (rr >= (1 << j)) { n         rr -= (1 << j); n         cur += dp[idx][j]; n         idx += dp[idx][j]; n       } n     } n     if (cur > ma) maid = i, ma = cur; n   } n   int cn = 1, cs = dp[maid][0]; n   for (int i = maid; i < maid + ma; i++) { n     s[idx[i] + len[i]] = 0; n     if (cn < cs) { n       printf( %s  , s + idx[i]); n       cn++; n     } else { n       printf( %s n , s + idx[i]); n       cs = dp[i + 1][0]; n       cn = 1; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char change(char curranswer) { n   if (curranswer ==  z ) n     curranswer =  A ; n   else if (curranswer ==  Z ) n     curranswer =  0 ; n   else if (curranswer ==  9 ) n     ; n   else n     curranswer++; n   return curranswer; n } n int main() { n   long long i, j, t; n   cin >> t; n   while (t--) { n     long long r, c, k; n     long long rice = 0; n     char a[100][100]; n     cin >> r >> c >> k; n     for (i = 0; i < r; i++) { n       for (j = 0; j < c; j++) { n         cin >> a[i][j]; n         if (a[i][j] ==  R ) rice++; n       } n     } n     long long ans = rice / k; n     long long maxx = rice - k * ans; n     long long minn = k - maxx; n     char curranswer =  a ; n     long long currcount = 0; n     bool flag = true; n     for (i = 0; i < r; i++) { n       for (flag ? (j = 0) : (j = c - 1); flag ? (j < c) : j >= 0; n            flag ? j++ : j--) { n         bool local = false; n         if (a[i][j] ==  R ) local = true; n         a[i][j] = curranswer; n         if (local) { n           currcount++; n           if (maxx > 0) { n             if (currcount == ans + 1) { n               maxx--; n               currcount = 0; n               curranswer = change(curranswer); n             } n           } else if (minn > 0) { n             if (currcount == ans) { n               minn--; n               currcount = 0; n               if (minn) curranswer = change(curranswer); n             } n           } n         } n       } n       flag = !flag; n     } n     for (i = 0; i < r; i++) { n       for (j = 0; j < c; j++) cout << a[i][j]; n       cout << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long x, long long y) { n   if (y == 0) n     return x; n   else n     return gcd(y, x % y); n } n long long expo(long long n, long long m) { n   long long r = 1; n   while (m > 0) { n     if (m % 2) r = (r * n) % 1000000009; n     n = (n * n) % 1000000009; n     m = m / 2; n   } n   return r % 1000000009; n } n long long lx = 0, ly = 0; n bool check(long long x, long long y) {} n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long a, b; n   cin >> a >> b; n   string s; n   cin >> s; n   long long p = 0, q = 0; n   if (a == 0 && b == 0) { n     cout <<  Yes ; n     return 0; n   } n   for (long long i = 0; i < s.size(); i++) { n     if (s[i] ==  U ) { n       ly++; n     } else if (s[i] ==  D ) n       ly--; n     else if (s[i] ==  L ) n       lx--; n     else n       lx++; n     if (lx == a && ly == b) { n       cout <<  Yes ; n       return 0; n     } n   } n   if (lx == 0 && ly == 0) { n     cout <<  No ; n     return 0; n   } n   for (long long i = 0; i < s.size(); i++) { n     if (s[i] ==  U ) { n       q++; n     } else if (s[i] ==  D ) n       q--; n     else if (s[i] ==  L ) n       p--; n     else n       p++; n     long long x = a - p, y = b - q, X = lx, Y = ly; n     if (lx == 0) { n       if (ly > 0 && y > 0 && x == 0 && y % ly == 0) { n         cout <<  Yes ; n         return 0; n       } else if (ly < 0 && y < 0 && x == 0) { n         ly = -1 * ly, y = -1 * y; n         if (y % ly == 0) { n           cout <<  Yes ; n           return 0; n         } n       } n     } else if (ly == 0) { n       if (lx > 0 && x > 0 && y == 0 && x % lx == 0) { n         cout <<  Yes ; n         return 0; n       } else if (lx < 0 && x < 0 && y == 0) { n         lx = -1 * lx, x = -1 * x; n         if (x % lx == 0) { n           cout <<  Yes ; n           return 0; n         } n       } n     } else { n       if (lx > 0 && x > 0) { n         if (ly > 0 && y > 0) { n           if (x / lx == y / ly && x % lx == 0 && y % ly == 0) { n             cout <<  Yes ; n             return 0; n           } n         } else if (ly < 0 && y < 0) { n           ly = -1 * ly, y = -1 * y; n           if (x / lx == y / ly && x % lx == 0 && y % ly == 0) { n             cout <<  Yes ; n             return 0; n           } n         } n       } else if (lx < 0 && x < 0) { n         lx = -1 * lx, x = -1 * x; n         if (ly > 0 && y > 0) { n           if (x / lx == y / ly && x % lx == 0 && y % ly == 0) { n             cout <<  Yes ; n             return 0; n           } n         } else if (ly < 0 && y < 0) { n           ly = -1 * ly, y = -1 * y; n           if (x / lx == y / ly && x % lx == 0 && y % ly == 0) { n             cout <<  Yes ; n             return 0; n           } n         } n       } n     } n     lx = X, ly = Y; n   } n   cout <<  No ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = int(3e5), mod = int(1e9) + 7; n int n; n int a[N], c[N]; n long long t[N]; n long long x[N], pref[N]; n long double p[N]; n long long suf[N]; n void upd(int v, int val) { n   v += n - 1; n   t[v] += val; n   c[v]++; n   while (v > 1) { n     v >>= 1; n     t[v] = t[v + v] + t[v + v + 1]; n     c[v] = c[v + v] + c[v + v + 1]; n   } n } n long long get(int l, int r) { n   l += n - 1; n   r += n - 1; n   long long res = 0; n   while (l <= r) { n     if (l & 1) res += t[l]; n     if (!(r & 1)) res += t[r]; n     l = (l + 1) >> 1; n     r = (r - 1) >> 1; n   } n   return res; n } n int cnt(int l, int r) { n   l += n - 1; n   r += n - 1; n   int res = 0; n   while (l <= r) { n     if (l & 1) res += c[l]; n     if (!(r & 1)) res += c[r]; n     l = (l + 1) >> 1; n     r = (r - 1) >> 1; n   } n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   long long all = 1ll * n * (n + 1) / 2; n   long double res = 0; n   for (int i = 1; i <= n; i++) { n     long long cur = get(1, a[i] - 1); n     long double p = (cur + 0.0) * (n - i + 1) / all; n     res += p * 0.5; n     cur = get(a[i] + 1, n); n     p = (cur + 0.0) * (n - i + 1) / all; n     res += cnt(a[i] + 1, n); n     res -= p * 0.5; n     upd(a[i], i); n   } n   printf( %.12lf , double(res)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long a, b; n   cin >> a >> b; n   long long ans = 0; n   while (true) { n     long long x = max(a, b); n     long long y = min(a, b); n     if (x == 0 || y == 0) break; n     long long n = x / y; n     ans += n; n     x -= n * y; n     a = x; n     b = y; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int x, test; n   cin >> test; n   while (test--) { n     int n, c0, c1, h; n     string s; n     cin >> n >> c0 >> c1 >> h >> s; n     int cnt0 = 0, cnt1 = 0; n     for (auto &z : s) { n       cnt0 += z ==  0 ; n       cnt1 += z ==  1 ; n     } n     int cost = (cnt0 * c0) + (cnt1 * c1); n     int cost0 = (c0 * n) + (cnt1 * h); n     int cost1 = (c1 * n) + (cnt0 * h); n     cout << min({cost, cost0, cost1}) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = int(1e9) + 123; n int main() { n   int n, v; n   cin >> n >> v; n   vector<int> a; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     a.push_back(x); n   } n   sort(a.begin(), a.end()); n   int sum = 0; n   for (int i = 0; i < a.size() - 1; i++) { n     sum += a[i]; n   } n   if (sum <= v) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n