#include <bits/stdc++.h> n using namespace std; n struct ruchy { n   bool kol[55]; n   bool wiersze[55]; n }; n int dl, szer, ilosc_ruchow; n int kolumny[55]; n int wiersze[55]; n ruchy gra[2510]; n char palnsza[54][54]; n bool dasie = 1; n void wypisgry() { n   for (int k = 0; k < ilosc_ruchow; k++) { n     cout <<  ruch numer  << k + 1 << endl; n     cout <<  kolumny;  << endl; n     for (int i = 1; i < 50; i++) n       if (gra[k + 1].kol[i]) cout << i <<  , ; n     cout << endl; n     cout <<  wiersze;  << endl; n     for (int i = 1; i < 50; i++) n       if (gra[k + 1].wiersze[i]) cout << i <<  , ; n     cout << endl; n   } n } n int main() { n   cin >> dl >> szer; n   for (int j = 0; j < dl; j++) { n     for (int i = 0; i < szer; i++) { n       cin >> palnsza[i + 1][j + 1]; n     } n   } n   for (int j = 0; j < dl; j++) { n     for (int i = 0; i < szer; i++) { n       if (palnsza[i + 1][j + 1] ==  # ) { n         if (kolumny[i + 1] != 0) { n           if (wiersze[j + 1] != 0 && wiersze[j + 1] != kolumny[i + 1]) n             dasie = 0; n           wiersze[j + 1] = kolumny[i + 1]; n           gra[kolumny[i + 1]].wiersze[j + 1] = 1; n         } else { n           if (wiersze[j + 1] != 0) { n             if (kolumny[i + 1] != 0 && kolumny[i + 1] != wiersze[j + 1]) n               dasie = 0; n             kolumny[i + 1] = wiersze[j + 1]; n             gra[wiersze[j + 1]].kol[i + 1] = 1; n           } else { n             ilosc_ruchow++; n             kolumny[i + 1] = ilosc_ruchow; n             wiersze[j + 1] = ilosc_ruchow; n             gra[ilosc_ruchow].kol[i + 1] = 1; n             gra[ilosc_ruchow].wiersze[j + 1] = 1; n           } n         } n       } n     } n   } n   for (int k = 0; k < ilosc_ruchow; k++) { n     for (int j = 0; j < dl; j++) { n       for (int i = 0; i < szer; i++) { n         if (gra[k + 1].wiersze[j + 1] && gra[k + 1].kol[i + 1] && n             palnsza[i + 1][j + 1] ==  . ) n           dasie = 0; n       } n     } n   } n   if (dasie) n     printf( Yes ); n   else n     printf( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {1, -1, 0, 0}; n int dy[] = {0, 0, 1, -1}; n int dx2[] = {1, -1, 0, 0, -1, 1, -1, 1}; n int dy2[] = {0, 0, 1, -1, 1, -1, -1, 1}; n int read() { n   int x; n   scanf( %d , &x); n   return x; n } n int ans[100005]; n int a[100005]; n int used[1000001]; n int main() { n   int n = read(); n   int m = read(); n   for (int i = 1; i <= n; i++) a[i] = read(); n   for (int i = n; i >= 1; i--) { n     if (used[a[i]]) n       ans[i] = ans[i + 1]; n     else n       ans[i] = ans[i + 1] + 1, used[a[i]] = 1; n   } n   while (m--) { n     printf( %d n , ans[read()]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, c; n   cin >> n >> m >> c; n   vector<int> a(n); n   for (int i = 0; i < n; i++) cin >> a[i]; n   vector<int> b(n); n   for (int i = 0; i < m; i++) cin >> b[i]; n   int cur = 0; n   for (int i = 0; i < a.size(); i++) { n     if (i >= 0 && i < m) cur += b[i]; n     a[i] += cur; n     if (i >= (n)-m) cur -= b[i - ((n)-m)]; n   } n   for (int i = 0; i < n; i++) cout << a[i] % c <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long Mo = 1e9 + 7; n int cnt[1 << 6], sz; n void add(long long &x, long long y) { n   x += y; n   x %= Mo; n } n void gao(int n, int C, int R, vector<vector<long long>> &Ma) { n   int loop = 1 << R; n   for (int i = 0; i < sz; i++) { n     int c = i / loop, r = i % loop; n     int a1 = r >> 1, a2 = r >> 1 | (1 << (R - 1)); n     if ((r & 1) && c != 0) { n       Ma[c * loop + a1 - loop][i] += cnt[a1] + 1; n       Ma[c * loop + a2 - loop][i] += cnt[a2] + 1; n     } else if (!(r & 1)) { n       Ma[c * loop + a1][i] += 1; n       Ma[c * loop + a2][i] += 1; n     } n   } n } n void mul(vector<vector<long long>> &a, vector<vector<long long>> b) { n   vector<vector<long long>> ret(sz, vector<long long>(sz)); n   for (int i = 0; i < sz; i++) { n     for (int j = 0; j < sz; j++) { n       for (int k = 0; k < sz; k++) { n         add(ret[i][j], a[i][k] * b[k][j]); n       } n     } n   } n   a = ret; n } n void matpw(vector<vector<long long>> &a, long long b) { n   vector<vector<long long>> ret(sz, vector<long long>(sz)); n   for (int i = 0; i < sz; i++) ret[i][i] = 1; n   while (b) { n     if (b & 1) mul(ret, a); n     mul(a, a); n     b >>= 1; n   } n   a = ret; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   for (int i = 0; i < (1 << 6); i++) { n     for (int j = 0; j < 6; j++) { n       if (i & (1 << j)) cnt[i]++; n     } n   } n   int n, K, m; n   cin >> n >> K >> m; n   sz = (K + 1) * (1 << m); n   vector<vector<long long>> Ma(sz, vector<long long>(sz)); n   gao(sz, K + 1, m, Ma); n   matpw(Ma, n); n   vector<vector<long long>> base(sz, vector<long long>(sz)); n   for (int i = 0; i < sz; ++i) base[i][0] = 1; n   mul(base, Ma); n   long long ans = 0; n   for (int i = 0; i < (1 << m); ++i) add(ans, base[0][sz - i - 1]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> cities[100000], T[100000]; n bool visited[100000]; n int tin[100000], low[100000], tv[100000]; n int up[100000][20]; n map<pair<int, int>, int> b; n int c = 0, n, l, m, k; n void dfs1(int v, int p = -1) { n   visited[v] = true; n   tin[v] = low[v] = c++; n   for (int dest : cities[v]) { n     if (dest == p) continue; n     if (visited[dest]) { n       low[v] = min(low[v], tin[dest]); n     } else { n       dfs1(dest, v); n       low[v] = min(low[v], low[dest]); n       if (low[dest] > tin[v]) { n         b[{min(v, dest), max(dest, v)}] = 1; n       } n     } n   } n } n void preprocessbridges() { n   memset(visited, false, sizeof visited); n   memset(tin, -1, sizeof tin); n   memset(low, -1, sizeof low); n   for (int i = 0; i < n; ++i) { n     if (!visited[i]) { n       dfs1(i); n     } n   } n } n void Tdfs(int u, int d) { n   visited[u] = true; n   tv[u] = d; n   for (int v : cities[u]) { n     if (visited[v]) continue; n     T[u].push_back(v); n     T[v].push_back(u); n     Tdfs(v, d + b[{min(u, v), max(u, v)}]); n   } n } n void dfs(int v, int p) { n   tin[v] = c++; n   up[v][0] = p; n   for (int i = 1; i <= l; ++i) { n     up[v][i] = up[up[v][i - 1]][i - 1]; n   } n   for (int u : T[v]) { n     if (u == p) { n       continue; n     } n     dfs(u, v); n   } n   low[v] = c++; n } n bool is_ancestor(int u, int v) { return tin[u] <= tin[v] && low[u] >= low[v]; } n int lca(int u, int v) { n   if (is_ancestor(u, v)) { n     return u; n   } n   if (is_ancestor(v, u)) { n     return v; n   } n   for (int i = l; i >= 0; --i) { n     if (!is_ancestor(up[u][i], v)) { n       u = up[u][i]; n     } n   } n   return up[u][0]; n } n void preprocesslca() { n   memset(visited, false, sizeof visited); n   memset(tv, 0, sizeof tv); n   Tdfs(0, -1); n   memset(tin, 0, sizeof tin); n   memset(low, 0, sizeof low); n   c = 0; n   l = ceil(log2(n)); n   memset(up, 0, sizeof up); n   dfs(0, 0); n } n void preprocess() { n   preprocessbridges(); n   preprocesslca(); n } n int solve(int r, int e) { return tv[r] + tv[e] - 2 * tv[lca(r, e)]; } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     cities[x - 1].push_back(y - 1); n     cities[y - 1].push_back(x - 1); n   } n   preprocess(); n   cin >> k; n   while (k--) { n     int root, shop; n     cin >> root >> shop; n     cout << solve(root - 1, shop - 1) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long sum[500001], ans; n long long solve(long long a, long long b, long long c, long long hh) { n   if (a < b + c) return 0ll; n   long long now = min(a - b - c, hh); n   return (now + 1) * (now + 2) / 2; n } n int main() { n   long long a, b, c, l, i; n   scanf( %lld%lld%lld%lld , &a, &b, &c, &l); n   sum[0] = 1; n   for (i = 1; i <= l; i++) sum[i] = sum[i - 1] + (i + 1) * (i + 2) / 2; n   ans = sum[l]; n   for (i = 0; i <= l; i++) n     ans -= solve(a + i, b, c, l - i) + solve(b + i, a, c, l - i) + n            solve(c + i, a, b, l - i); n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 10005; n long long n, m, a[maxn], b[maxn]; n int k; n long long gcd(long long a, long long b) { n   if (!b) return a; n   return gcd(b, a % b); n } n long long exgcd(long long a, long long b, long long &x, long long &y) { n   if (!b) { n     x = 1, y = 0; n     return a; n   } n   long long r = exgcd(b, a % b, x, y), x0 = x; n   x = y; n   y = x0 - (a / b) * y; n   return r; n } n long long CRT(long long *m, long long *a) { n   long long lcm = 1, X = m[1], Y = a[1]; n   for (int i = 1; i <= k; i++) lcm = lcm / gcd(lcm, m[i]) * m[i]; n   for (int i = 2; i <= k; i++) { n     long long A = X, B = m[i], d, x, y, c = a[i] - Y; n     d = exgcd(A, B, x, y); n     if (c % d) return -1; n     long long mod = m[i] / d; n     long long K = ((x * c / d) % mod + mod) % mod; n     Y = X * K + Y; n     X = X * m[i] / d; n   } n   if (Y == 0) return lcm; n   return Y; n } n bool check() { n   long long I = CRT(a, b), J; n   if (I > n || I <= 0) return 0; n   for (int i = 1; i <= k; i++) b[i] = 1 - i; n   J = CRT(a, b); n   if (J + k - 1 > m || J <= 0) return 0; n   for (int i = 1; i <= k; i++) n     if (gcd(I, J + i - 1) != a[i]) return 0; n   return 1; n } n int main() { n   cin >> n >> m >> k; n   for (int i = 1; i <= k; i++) cin >> a[i]; n   if (check()) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100 * 1000 + 17; n long long n, a[MAXN], ans; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i < n; i++) { n     int t = log2(n - i); n     a[i + (1 << t)] += a[i]; n     ans += a[i]; n     a[i] = 0; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[101]; n void scan() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   sort(a, a + n); n } n void out() { n   int ans = 0, k = 0, c; n   while (1) { n     c = 0; n     for (int i = 0; i < n; i++) { n       if (a[i] != -1) { n         if (a[i] >= k) { n           ++k; n           a[i] = -1; n         } n       } else if (a[i] == -1) n         ++c; n     } n     if (c == n) break; n     ++ans; n     k = 0; n   } n   cout << ans <<   n ; n } n int main() { n   scan(); n   out(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n vector<pair<long long, long long> > serv; n bool check(long long x1, long long x2) { n   long long pos = 0; n   bool found = false; n   for (long long i = n - 1; i >= 0; --i) { n     if (serv[i].first * (n - i) >= x2) { n       found = true; n       pos = i; n       break; n     } n   } n   if (!found) { n     return false; n   } n   found = false; n   for (long long i = pos - 1; i >= 0; --i) { n     if (serv[i].first * (pos - i) >= x1) { n       found = true; n       break; n     } n   } n   return found; n } n pair<vector<long long>, vector<long long> > print(long long x1, long long x2) { n   vector<long long> a1, a2; n   long long pos = 0; n   for (long long i = n - 1; i >= 0; --i) { n     a2.push_back(serv[i].second); n     if (serv[i].first * (n - i) >= x2) { n       pos = i; n       break; n     } n   } n   for (long long i = pos - 1; i >= 0; --i) { n     a1.push_back(serv[i].second); n     if (serv[i].first * (pos - i) >= x1) { n       break; n     } n   } n   return {a1, a2}; n } n void solve() { n   long long x1 = 0, x2 = 0; n   cin >> n >> x1 >> x2; n   serv.resize(n); n   for (long long i = 0; i < n; ++i) { n     cin >> serv[i].first; n     serv[i].second = i; n   } n   sort(serv.begin(), serv.end()); n   if (check(x1, x2)) { n     cout <<  Yes  << endl; n     auto ans = print(x1, x2); n     cout << ans.first.size() <<     << ans.second.size() << endl; n     for (long long x : ans.first) cout << x + 1 <<    ; n     cout << endl; n     for (long long x : ans.second) cout << x + 1 <<    ; n     cout << endl; n   } else if (check(x2, x1)) { n     cout <<  Yes  << endl; n     auto ans = print(x2, x1); n     cout << ans.second.size() <<     << ans.first.size() << endl; n     for (long long x : ans.second) cout << x + 1 <<    ; n     cout << endl; n     for (long long x : ans.first) cout << x + 1 <<    ; n     cout << endl; n   } else { n     cout <<  No  << endl; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 9, base = 27; n int cnt_list, pos[20200], pa[20100], len[20100], fs[20100], pc[20100], n     ls[20100], cnt_ls, tmp[20100], cnt, n, num_q; n struct pp { n   int id; n   char ch; n }; n vector<pp> adj[20201]; n pp now; n void dfs(int id, int pr) { n   int i, j, s, p, q, ip; n   pos[id] = cnt_list++; n   pa[pos[id]] = pr; n   for (i = 0; i < adj[id].size(); i++) { n     ip = adj[id][i].id; n     if (pos[ip] >= 0) continue; n     pc[cnt_list] = adj[id][i].ch -  a ; n     dfs(ip, pos[id]); n   } n } n int main() { n   int id1, id2, i, j, s, p, q, x, y, fg; n   char ch; n   scanf( %d%d , &n, &num_q); n   for (i = 0; i < n - 1; i++) { n     scanf( %d%d , &id1, &id2); n     ch = getchar(); n     while (ch <  a  || ch >  z ) ch = getchar(); n     id1--; n     id2--; n     now.id = id2; n     now.ch = ch; n     adj[id1].push_back(now); n     now.id = id1; n     now.ch = ch; n     adj[id2].push_back(now); n   } n   memset(pos, -1, sizeof(pos)); n   dfs(0, -1); n   while (num_q--) { n     scanf( %d%d , &x, &y); n     x--; n     y--; n     x = pos[x]; n     y = pos[y]; n     int u, v; n     u = x; n     v = y; n     cnt_ls = cnt = 0; n     while (u != v) { n       if (u > v) { n         ls[cnt_ls++] = pc[u]; n         u = pa[u]; n       } else { n         tmp[cnt++] = pc[v]; n         v = pa[v]; n       } n     } n     while (cnt > 0) ls[cnt_ls++] = tmp[--cnt]; n     for (i = 0; i < n; i++) len[i] = -1; n     u = x; n     fs[u] = len[u] = 0; n     int ans = 0; n     while (u != 0) { n       len[pa[u]] = len[u] + 1; n       if (fs[u] == 0) { n         if (len[u] < cnt_ls) { n           if (ls[len[u]] > pc[u]) n             fs[pa[u]] = -1; n           else if (ls[len[u]] < pc[u]) n             fs[pa[u]] = 1; n           else n             fs[pa[u]] = 0; n         } else n           fs[pa[u]] = 1; n       } else n         fs[pa[u]] = fs[u]; n       if (fs[pa[u]] < 0 || (fs[pa[u]] == 0 && len[pa[u]] != cnt_ls)) ans++; n       u = pa[u]; n     } n     for (i = 1; i < n; i++) { n       if (len[pa[i]] >= 0 && len[i] < 0) { n         len[i] = len[pa[i]] + 1; n         if (fs[pa[i]] == 0) { n           if (len[pa[i]] < cnt_ls) { n             if (ls[len[pa[i]]] > pc[i]) n               fs[i] = -1; n             else if (ls[len[pa[i]]] < pc[i]) n               fs[i] = 1; n             else n               fs[i] = 0; n           } else n             fs[i] = 1; n         } else n           fs[i] = fs[pa[i]]; n         if (fs[i] < 0 || (fs[i] == 0 && len[i] != cnt_ls)) ans++; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 50 + 7; n int ned[N][N]; n string s[N]; n void paint(const vector<pair<int, int> > &v) { n   set<pair<int, int> > s; n   queue<pair<int, int> > que; n   map<pair<int, int>, int> cnt; n   for (auto e : v) { n     que.push(e); n     s.insert(e); n   } n   const int dx[] = {2, 2, 1, 1, -1, -1, -2, -2}; n   const int dy[] = {1, -1, 2, -2, 2, -2, 1, -1}; n   int tot = 0; n   while (!que.empty()) { n     auto p = que.front(); n     que.pop(); n     tot++; n     for (int i = (0); i < (8); i++) { n       int x = p.first + dx[i]; n       int y = p.second + dy[i]; n       if (s.find(make_pair(x, y)) != s.end()) continue; n       if (++cnt[make_pair(x, y)] == 4) { n         s.insert({x, y}); n         que.push({x, y}); n       } n     } n   } n   cout <<  sz(s)  n        <<   =   << (((int)(s).size())) <<   n ; n   int des = ((int)(v).size()) * ((int)(v).size()) / 10; n   cout <<  des  n        <<   =   << (des) <<   n ; n } n int main() { n   std::ios::sync_with_stdio(false); n   std::cin.tie(0); n   int n; n   cin >> n; n   vector<pair<int, int> > v; n   for (int i = (0); i < (n); i++) { n     if (i & 1) v.push_back({3, i}); n     v.push_back({0, i}); n     if (((int)(v).size()) > n) break; n   } n   v.resize(n); n   for (auto e : v) cout << e.first <<     << e.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long x, y, p, q; n bool check(long long t) { n   return ((p * t >= x) && ((q * t - p * t) >= (y - x))); n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %I64d%I64d%I64d%I64d , &x, &y, &p, &q); n     long long l = 0, r = 1e9, mid; n     long long ans = -1; n     while (l <= r) { n       mid = (l + r) / 2; n       if (check(mid)) { n         ans = mid; n         r = mid - 1; n       } else n         l = mid + 1; n     } n     if (ans == -1) n       printf( -1 n ); n     else n       printf( %I64d n , ans * q - y); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 998244353; n inline int add(int a, int b) { n   a += b; n   if (a >= mod) a -= mod; n   return a; n } n inline int sub(int a, int b) { n   a -= b; n   if (a < 0) a += mod; n   return a; n } n inline int mul(int a, int b) { return (a * 1ll * b) % mod; } n inline int power(int a, int b) { n   int rt = 1; n   while (b > 0) { n     if (b & 1) rt = mul(rt, a); n     a = mul(a, a); n     b >>= 1; n   } n   return rt; n } n inline int inv(int a) { return power(a, mod - 2); } n const int N = 1e6 + 5; n vector<int> z_function(string s) { n   int n = (int)s.length(); n   vector<int> z(n); n   for (int i = 1, l = 0, r = 0; i < n; ++i) { n     if (i <= r) z[i] = min(r - i + 1, z[i - l]); n     while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; n     if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; n   } n   return z; n } n string a; n int mat[N][2], sum0[N], dp[N], sumdp[N]; n void fill(string s, int ind) { n   string s1 = s +  $  + a; n   vector<int> v = z_function(s1); n   int n1 = s.size(), n = a.size(); n   for (int i = 0; i < n; i++) mat[i][ind] = v[n1 + 1 + i]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cout << setprecision(25); n   string l, r; n   cin >> a >> l >> r; n   int n = a.size(), l1 = l.size(), r1 = r.size(); n   fill(l, 0); n   fill(r, 1); n   for (int i = 0; i < n; i++) { n     if (i + 1 < l1) continue; n     if (i > 0) sum0[i] = sum0[i - 1], sumdp[i] = sumdp[i - 1]; n     if (a[i] ==  0 ) sum0[i] = add(sum0[i], i - 1 < 0 ? 1 : dp[i - 1]); n     int ind1 = -1, ind2 = -1; n     ind1 = i - l1 + 1; n     int x = mat[ind1][0]; n     if (x < l1 && a[ind1 + x] < l[x]) ind1--; n     if (ind1 < 0) continue; n     ind2 = i - r1 + 1; n     if (ind2 < 0) n       ind2 = 0; n     else { n       x = mat[ind2][1]; n       if (x != r1 && a[ind2 + x] > r[x]) ind2++; n     } n     if (ind2 > ind1) continue; n     if (ind1 == ind2) { n       if (ind1 == 0) n         dp[i] = 1; n       else n         dp[i] = dp[ind1 - 1]; n       if (i - ind1 > 0 && a[ind1] ==  0 ) dp[i] = 0; n     } else { n       dp[i] = sub(sub(sumdp[ind1 - 1], (ind2 - 2 < 0 ? ind2 == 0 ? mod - 1 : 0 n                                                      : sumdp[ind2 - 2])), n                   sub(sum0[ind1], (ind2 - 1 < 0 ? 0 : sum0[ind2 - 1]))); n       if (i == ind1 && a[i] ==  0 ) dp[i] = add(dp[i], i == 0 ? 1 : dp[i - 1]); n     } n     sumdp[i] = add(sumdp[i], dp[i]); n   } n   cout << dp[n - 1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> x[200]; n int y[200], n, i, j, k, a, b, c, s, A, B; n int main() { n   scanf( %d , &n); n   n *= 2; n   for (i = 1; i <= n; i++) scanf( %d , &k), x[k].push_back(i); n   for (i = 10; i <= 99; i++) n     if (x[i].size() == 1) { n       if (a > b) n         y[x[i][0]] = 2, b++; n       else n         y[x[i][0]] = 1, a++; n     } n   s = a * b; n   A = a; n   B = b; n   for (i = 10; i <= 99; i++) n     if (x[i].size() > 1) { n       s += a + b; n       c++; n       for (j = 0; j < x[i].size(); j++) { n         if (A > B) n           y[x[i][j]] = 2, B++; n         else n           y[x[i][j]] = 1, A++; n       } n     } n   s += c * c; n   printf( %d n , s); n   for (i = 1; i <= n; i++) printf( %d  , y[i]); n } n


#include <bits/stdc++.h> n #include <chrono> n  n using namespace std; n  n using ll           = long long; n using ld           = long double; n using pii          = pair<int, int>; n using pll          = pair<long, long>; n using vi           = vector<int>; n using vl           = vector<long long>; n using vb           = vector<bool>; n using vs           = vector<string>; n using vld          = vector<long double>; n  n #define pb             push_back n #define f              first n #define s              second n #define sz(a)          int((a).size()) n #define all(a)         (a).begin(), (a).end() n #define rall(a)        (a).rbegin(), (a).rend() n #define sum(a)         ( accumulate((a).begin(), (a).end(), 0LL)) n #define mine(a)        (*min_element((a).begin(), (a).end())) n #define maxe(a)        (*max_element((a).begin(), (a).end())) n #define mini(a)        ( min_element((a).begin(), (a).end()) - (a).begin()) n #define maxi(a)        ( max_element((a).begin(), (a).end()) - (a).begin()) n #define lowb(a, x)     ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin()) n #define uppb(a, x)     ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin()) n #define each(a, b)     for (auto &a: b) n #define yes            cout <<  YES  << endl n #define no             cout <<  NO   << endl n  n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n  n const ll mod = 1e9 + 7; n  n ll n, m, k, p, q, l, r, u, v, x, y, z; n // ll ans = 0; n  n void solve(int t = 1) { n     cin >> n >> m; n  n     vi a(n), b(n), c(m); n     each(u, a) cin >> u; n     each(u, b) cin >> u; n     each(u, c) cin >> u; n  n     vi ans(m, 0); n  n     map<int, vi> mp; n     for (int i = 0; i < m; i++) { n         mp[c[i]].push_back(i); n     } n  n     for (int i = 0; i < n; i++) { n         if (a[i] != b[i]) { n             if (!mp[b[i]].empty()) { n                 a[i] = b[i]; n                 ans[mp[b[i]].back()] = i + 1; n                 mp[b[i]].pop_back(); n             } else { n                 no; n                 return; n             } n         } n     } n  n     if (a != b) { n         no; n         return; n     } n  n     if (ans[m - 1] == 0) { n         bool flag = true; n  n         for (int i = 0; i < n; i++) { n             if (c[m - 1] == b[i]) { n                 ans[m - 1] = i + 1; n                 flag = false; n                 break; n             } n         } n  n         if (flag) { n             no; n             return; n         } n     } n  n     each(u, ans) { n         if (u == 0) u = ans[m - 1]; n     } n  n     yes; n     each(u, ans) cout << u <<    ; n     cout << endl; n } n  n int main() { n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n  n     int tc = 1; n     cin >> tc; n  n     for (int t = 1; t <= tc; t++) solve(t); n  n     return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define endl   n  n typedef long long ll; n typedef pair<int, int> pii; n typedef pair<ll, ll> pll; n typedef tuple<int, int, int> tiii; n typedef tuple<ll, ll, ll> tlll; n const int INF = 100000000; n const ll INFINITE = 4500000000000000000; n  n int main(void) { n  int t; n  cin >> t; n  while (t--) { n   int s1, s2, s3, s4; n   cin >> s1 >> s2 >> s3 >> s4; n   if (min(s1, s2) < max(s3, s4) &&min(s3, s4) <max(s1, s2)) n    cout <<  YES  << endl; n   else n    cout <<  NO  << endl; n  n  } n }


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1); n int main() { n   int n; n   double R, r; n   double st; n   while (scanf( %d %lf , &n, &R) != EOF) { n     st = pi / (1.0 * n); n     r = sin(st) * R / (1 - sin(st)); n     printf( %.7lf n , r); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[2000090]; n int d[2000009][11]; n int main() { n   ios::sync_with_stdio(0); n   for (int i = 1; i < 10; i++) a[i] = i; n   for (int i = 1; i < 2000009; i++) { n     int l = 1; n     int g = i; n     while (g != 0) { n       if (g % 10 != 0) l *= (g % 10); n       g /= 10; n     } n     for (int j = 1; j < 10; j++) d[i][j] = d[i - 1][j]; n     a[i] = a[l]; n     d[i][a[l]]++; n   } n   int q; n   cin >> q; n   while (q--) { n     int x, y, z; n     cin >> x >> y >> z; n     cout << d[y][z] - d[x - 1][z] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, m; n   cin >> n >> m; n   if (m == 0) { n     cout << n <<     << n << endl; n     return 0; n   } n   long long min = 0; n   if (m * 2ll <= n) { n     min = n - m * 2; n   } n   long long e = 1; n   long long g = 2; n   long long max = 0; n   for (long long i = 2; i <= n; i++) { n     if (e >= m) { n       max = n - i; n       break; n     } n     e += g; n     g++; n   } n   cout << min <<     << max << endl; n   return 0; n } n


#include<bits/stdc++.h> n #define rep(i, n) for(int i = 0; i < n; i++) n using namespace std; n typedef long long ll; n const ll Mod = 1000000007; n  n int t; n int n; n ll x[500500]; n ll sum_bit[70]; n  n int main(){ n  ios::sync_with_stdio(false); n  cin >> t; n  while(t--){ n   cin >> n; n   rep(i, n) cin >> x[i]; n   memset(sum_bit, 0, sizeof(sum_bit)); n   rep(b, 60) rep(i, n) sum_bit[b] = (sum_bit[b] + (x[i] & (1LL << b))) % Mod; n   ll ans = 0; n   rep(j, n){ n    ll sum_and = 0, sum_or = 0; n    rep(b, 60){ n     if(x[j] & (1LL << b)) sum_and = (sum_and + sum_bit[b]) % Mod, sum_or = (sum_or + (1LL << b) % Mod * n % Mod) % Mod; n     else sum_or = (sum_or + sum_bit[b]) % Mod; n    } n    ans = (ans + sum_and * sum_or) % Mod; n   } n   cout << ans << endl; n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct ant { n   int id; n   int novo_id; n   long long int p; n   int dir; n }; n struct rotate_edges { n   int id_ini; n   int id_fim; n }; n int compara_p(ant a, ant b) { n   return a.p < b.p || (a.p == b.p && a.dir < b.dir); n } n int compara_p_reverso(ant a, ant b) { return a.p > b.p; } n int compara_id(ant a, ant b) { return a.id < b.id; } n int cont_pos, cont_neg; n long long int N, M, T; n ant v[400000]; n ant v_final[400000]; n ant v_pos[400000]; n ant v_neg[400000]; n char c; n rotate_edges calcula_rotate_edges(long long int t, int pivot) { n   long long int p_pivot = v[pivot].p; n   int cont_neg = 0; n   int cont_pos = 0; n   for (int i = 0; i < N; i++) { n     if (v[i].dir < 0) { n       v_neg[cont_neg] = v[i]; n       if (v_neg[cont_neg].p < p_pivot) v_neg[cont_neg].p += M; n       cont_neg++; n     } else { n       v_pos[cont_pos] = v[i]; n       if (v_pos[cont_pos].p > p_pivot) v_pos[cont_pos].p -= M; n       cont_pos++; n     } n   } n   sort(v_neg, v_neg + cont_neg, compara_p); n   sort(v_pos, v_pos + cont_pos, compara_p_reverso); n   rotate_edges resp; n   if (cont_neg == 0) { n     resp.id_ini = v_pos[0].id; n     resp.id_fim = v_pos[0].id; n   } else if (cont_pos == 0) { n     resp.id_ini = v_neg[0].id; n     resp.id_fim = v_neg[0].id; n   } else { n     resp.id_ini = v_pos[0].id; n     resp.id_fim = v_pos[0].id; n     int id_pos; n     int id_neg; n     long long int p_pos; n     long long int p_neg; n     for (int i = 0;; i++) { n       id_pos = (i + 1) / 2; n       id_neg = i / 2; n       p_pos = v_pos[id_pos % cont_pos].p - M * (id_pos / cont_pos); n       p_neg = v_neg[id_neg % cont_neg].p + M * (id_neg / cont_neg); n       if (p_neg - p_pos <= 2 * t) { n         resp.id_fim = (i % 2 == 0) ? v_neg[id_neg % cont_neg].id n                                    : v_pos[id_pos % cont_pos].id; n       } else { n         break; n       } n     } n   } n   return resp; n } n long long int calcula_delta(rotate_edges re) { n   int vi_ini, vi_fim; n   for (int i = 0; i < N; i++) { n     if (v[i].id == re.id_ini) vi_ini = i; n     if (v[i].id == re.id_fim) vi_fim = i; n   } n   return (vi_fim + N - vi_ini) % N; n } n void rotate(long long int delta) { n   for (int i = 0; i < N; i++) { n     v[i].novo_id = v[(i + N - delta) % N].id; n   } n   for (int i = 0; i < N; i++) { n     v[i].id = v[i].novo_id; n   } n } n void rotate_final(rotate_edges re) { n   int id, id_final; n   for (int i = 0; i < N; i++) { n     if (v[i].id == re.id_ini) id = i; n     if (v_final[i].id == re.id_fim) id_final = i; n   } n   for (int i = 0; i < N; i++) { n     v_final[(id_final + i) % N].id = v[(id + i) % N].id; n   } n } n int main() { n   scanf(  %lld %lld %lld , &N, &M, &T); n   for (int i = 0; i < N; i++) { n     v[i].id = i; n     scanf(  %lld %c , &v[i].p, &c); n     v[i].p = v[i].p % M; n     if (c ==  L ) { n       v[i].dir = -1; n     } else { n       v[i].dir = 1; n     } n   } n   sort(v, v + N, compara_p); n   int pivot = 0; n   for (int i = 0; i < N; i++) { n     if (v[i].dir > 0 && v[(i + 1) % N].dir < 0) { n       pivot = i; n       break; n     } n   } n   long long int delta_por_ciclo = calcula_delta(calcula_rotate_edges(M, pivot)); n   rotate((delta_por_ciclo * (T / M)) % N); n   T = T % M; n   rotate_edges re = calcula_rotate_edges(T, pivot); n   for (int i = 0; i < N; i++) { n     v_final[i] = v[i]; n     v_final[i].p = (((v[i].p + v[i].dir * T) % M) + M) % M; n   } n   sort(v_final, v_final + N, compara_p); n   rotate_final(re); n   sort(v_final, v_final + N, compara_id); n   printf( %d , v_final[0].p == 0 ? M : v_final[0].p); n   for (int i = 1; i < N; i++) { n     printf(  %d , v_final[i].p == 0 ? M : v_final[i].p); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n int max(int a, int b) { return a > b ? a : b; } n int a[510][510], num[510], n, m, Q, ans, x, y; n void work(int pos) { n   num[pos] = 0; n   int f = 0; n   for (int i = 1; i <= m; i++) n     if (a[pos][i]) n       f++; n     else n       num[pos] = max(num[pos], f), f = 0; n   num[pos] = max(num[pos], f); n } n int main() { n   scanf( %d%d%d , &n, &m, &Q); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %d , &a[i][j]); n   for (int i = 1; i <= n; i++) work(i), ans = max(ans, num[i]); n   for (int t = 1; t <= Q; t++) { n     scanf( %d%d , &x, &y); n     a[x][y] = a[x][y] ^ 1; n     work(x); n     ans = 0; n     for (int i = 1; i <= n; i++) ans = max(ans, num[i]); n     printf( %d n , ans); n   } n } n


#include <bits/stdc++.h> n int main() { n   int n, start; n   char str[60]; n   scanf( %s , str); n   n = strlen(str); n   n = n % 2 ? n / 2 : n / 2 - 1; n   printf( %c , str[n]); n   for (int i = 1; i <= n; i++) printf( %c%c , str[n + i], str[n - i]); n   if (strlen(str) % 2 == 0 && strlen(str) > 1) n     printf( %c , str[strlen(str) - 1]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, cntx = 0, cntX = 0, c = 0; n   cin >> n; n   string s; n   cin >> s; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  x ) n       cntx++; n     else n       cntX++; n   } n   int i = 0; n   if (cntx > cntX) { n     while (cntx != cntX) { n       if (s[i] ==  x ) { n         s[i] =  X ; n         cntx--; n         c++; n         cntX++; n       } n       i++; n     } n   } else { n     while (cntx != cntX) { n       if (s[i] ==  X ) { n         s[i] =  x ; n         cntX--; n         cntx++; n         c++; n       } n       i++; n     } n   } n   cout << c << endl << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n const double EPS = 1e-9; n const int INT_INF = 1 << 31 - 1; n const long long I64_INF = 1ll << 63 - 1ll; n const double PI = acos(-1.0); n using namespace std; n vector<int> r; n string second; n void ReadTable(int id, int& FinalId); n string GetTag(int id) { n   string res =   ; n   id++; n   while (second[id] !=  > ) res += second[id], id++; n   return res; n } n void ReadTd(int id, int& FinalId) { n   string tag = GetTag(id); n   while (tag !=  /td ) { n     int NewId; n     ReadTable(id + 7, NewId); n     id = NewId; n     tag = GetTag(id); n   } n   FinalId = id + 5; n } n int ReadTr(int id, int& FinalId) { n   int res = 0; n   string tag = GetTag(id); n   while (tag !=  /tr ) { n     int NewId; n     ReadTd(id + 4, NewId); n     id = NewId; n     res++; n     tag = GetTag(id); n   } n   FinalId = id + 5; n   return res; n } n void ReadTable(int id, int& FinalId) { n   int res = 0; n   string tag = GetTag(id); n   while (tag !=  /table ) { n     int NewId; n     res += ReadTr(id + 4, NewId); n     id = NewId; n     tag = GetTag(id); n   } n   FinalId = id + 8; n   r.push_back(res); n } n int main() { n   char c; n   second.clear(); n   while (scanf( %c , &c) >= 1) n     if (c !=     && c !=   n ) second += c; n   int loose; n   ReadTable(7, loose); n   sort(r.begin(), r.end()); n   for (int i = 0; i < (int)r.size(); i++) printf( %d  , r[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, T, cnt, top; n char s[5100][5100]; n struct poi { n   int x, y; n   poi() {} n   poi(int x, int y) : x(x), y(y) {} n   void trs() { n     swap(x, y); n     x--; n     y = n - y; n   } n   friend bool operator<(const poi &r1, const poi &r2) { n     if (r1.x == r2.x) return r1.y < r2.y; n     return r1.x < r2.x; n   } n   friend int operator^(const poi &r1, const poi &r2) { n     return r1.x * r2.y - r2.x * r1.y; n   }; n   friend poi operator-(const poi &r1, const poi &r2) { n     return poi(r1.x - r2.x, r1.y - r2.y); n   }; n } a[5100 << 5], st[5100 << 5]; n int onleft(poi p1, poi p2, poi p3) { return ((p1 - p2) ^ (p3 - p2)) >= 0; } n int main() { n   while (scanf( %d , &n) != EOF && n) { n     top = 0; n     cnt = 0; n     for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         if (s[i][j] >  0  && s[i][j + 1] >  0  && s[i + 1][j] >  0  && n             s[i + 1][j + 1] >  0  && n             (s[i][j] ==  1  || s[i][j + 1] ==  1  || s[i + 1][j] ==  1  || n              s[i + 1][j + 1] ==  1 )) n           a[++cnt] = poi(i, j + 1); n     for (int i = 1; i <= cnt; i++) a[i].trs(); n     sort(a + 1, a + 1 + cnt); n     for (int i = 1; i <= cnt; i++) { n       while (top >= 2 && onleft(st[top], st[top - 1], a[i])) top--; n       st[++top] = a[i]; n     } n     int t = top; n     for (int i = cnt - 1; i >= 1; i--) { n       while (top > t && onleft(st[top], st[top - 1], a[i])) top--; n       st[++top] = a[i]; n     } n     printf( %d n , top - 1); n     for (int i = 1; i < top; i++) printf( %d %d n , st[i].x, st[i].y); n   } n   return 0; n } n


#pragma GCC optimize(3, Ofast , inline ) n #include <cstdio> n #include <iostream> n #include <cstring> n #include <algorithm> n #include <cmath> n #include <set> n #include <map> n #include <vector> n #include <stack> n #include <queue> n using namespace std; n typedef long long ll; n  n typedef pair<int, int> pii; n const int maxn = 1e6 + 10; n const ll mod = 1e9 + 7; n const double pi = acos(-1.0); n const double eps = 1e-8; n  n int t; n int n, k; n string s1, s2; n int cnt[27]; n  n void solve() n { n   int f = 0; n   memset(cnt, 0, sizeof(cnt)); n   sort(s1.begin(), s1.end()); n   sort(s2.begin(), s2.end()); n   if(s1 == s2)  puts( Yes ); n   else{ n     for(int i = 0; i < n; i ++) cnt[s1[i] -  a ] ++; n     for(int i = 0; i < n; i ++) cnt[s2[i] -  a ] --; n     for(int i = 0; i < 26; i ++){ n       if(cnt[i] < 0 || cnt[i] % k){ n           f = 1; n           break; n         } n       cnt[i + 1] += cnt[i]; n     } n     puts(!f ?  Yes  :  No ); n   } n } n  n int main(){ n   std::ios::sync_with_stdio(false); n   cin >> t; n   while(t --){ n     cin >> n >> k; n     cin >> s1 >> s2; n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, x, a, s; n vector<pair<int, long long> > v, u; n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x >> a; n     if (x < 0) n       v.push_back(make_pair(-x, a)); n     else n       u.push_back(make_pair(x, a)); n     s += a; n   } n   sort(v.begin(), v.end()); n   sort(u.begin(), u.end()); n   if ((int)v.size() - (int)u.size() <= 1 && n       (int)u.size() - (int)v.size() <= 1) { n     cout << s << endl; n     return 0; n   } n   if (v.size() > u.size()) { n     for (int i = u.size() + 1; i < v.size(); i++) s -= v[i].second; n     cout << s << endl; n     return 0; n   } n   for (int i = v.size() + 1; i < u.size(); i++) s -= u[i].second; n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string i_str(int n) { n   char buff[10]; n   sprintf(buff,  %d , n); n   return string(buff); n } n int main() { n   int a, b; n   cin >> a >> b; n   if (a < b) { n     cout << b; n     return 0; n   } n   string sa, sb, t, r =   , si =   ; n   sb = i_str(b); n   for (int j = a + 1; j <= 1000000; j++) { n     r =   ; n     si = i_str(j); n     int ni = si.length(); n     for (int i = 0; i < ni; i++) { n       if (si[i] ==  4 ) r = r + si[i]; n       if (si[i] ==  7 ) r = r + si[i]; n     }; n     if (r == sb) { n       cout << j; n       return 0; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e5 + 10; n const long long mod = 1e9 + 7; n long long powr(long long a, long long b) { n   long long x = 1 % mod; n   a %= mod; n   while (b) { n     if (b & 1) x = (x * a) % mod; n     a = (a * a) % mod; n     b >>= 1; n   } n   return x; n } n long long inv(long long a) { return powr(a, mod - 2); } n map<int, int> mymap, mymap2; n int par[maxn]; n int val[maxn]; n int main() { n   int n; n   cin >> n; n   int x; n   map<int, int>::iterator it1, it2; n   int mx = 0, mn = 1e9 + 10; n   int maxi = 1, mini = 1; n   for (int i = 1; i <= n; i++) { n     cin >> x; n     if (i == 1) mx = mn = x; n     val[i] = x; n     if (i > 1) { n       if (x > mx) { n         par[i] = maxi; n         maxi = i; n         mx = x; n       } else if (x < mn) { n         par[i] = mini; n         mini = i; n         mn = x; n       } else { n         it2 = mymap.upper_bound(x); n         int x = it2->second; n         it2--; n         int y = it2->second; n         par[i] = max(x, y); n       } n       cout << val[par[i]] <<    ; n     } n     mymap[x] = i; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t, n; n   string s; n   cin >> t; n   while (t--) { n     cin >> n >> s; n     vector<int> szs; n     char prev = s.front(); n     int sz = 0; n     for (char c : s) { n       if (c != prev) { n         szs.push_back(sz); n         sz = 1; n       } else n         sz++; n       prev = c; n     } n     szs.push_back(sz); n     int cnt = 0, id = 1; n     for (int i : szs) { n       if (i > 1) cnt += min(i - 1, id - cnt); n       id++; n     } n     cout << cnt + (szs.size() - cnt + 1) / 2 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int MAXN = 3e5 + 10; n template <typename value_t> n class fenwick { n  public: n   fenwick(int n) { n     memset(c, 0, sizeof c); n     this->n = n; n   } n   void add(int i, value_t x) { n     while (i <= n) { n       c[i] += x; n       i += Lowbit(i); n     } n   } n   value_t sum(int x) { n     value_t sum = 0; n     while (x > 0) { n       sum += c[x]; n       x -= Lowbit(x); n     } n     return sum; n   } n   value_t sum(int x1, int x2) { return sum(x2) - sum(x1 - 1); } n  n  private: n   value_t c[MAXN]; n   int n; n   inline int Lowbit(int x) { return x & (-x); } n }; n class lfs { n  public: n   lfs(int N) { n     memset(head, -1, sizeof head); n     ecnt = 0; n     n = N; n   } n   void adde(int from, int to, int w) { n     e[ecnt].to = to; n     e[ecnt].w = w; n     e[ecnt].next = head[from]; n     head[from] = ecnt++; n   } n   void addde(int a, int b, int w) { n     adde(a, b, w); n     adde(b, a, w); n   } n   void solve() { n     memset(ans, 0, sizeof ans); n     arr = new fenwick<long long>(n); n     dfs(1, 0, 1); n   } n   vector<pair<int, int>> opr[MAXN]; n   long long ans[MAXN]; n  n  protected: n   struct Edge { n     int to, next, w; n   } e[MAXN * 2]; n   int head[MAXN]; n   int ecnt; n   int n; n   fenwick<long long>* arr; n   void dfs(int u, int fa, int dep) { n     for (auto& i : opr[u]) { n       arr->add(dep, i.second); n       arr->add(dep + i.first + 1, -i.second); n     } n     ans[u] = arr->sum(dep); n     for (int i = head[u]; i != -1; i = e[i].next) { n       int v = e[i].to; n       if (v != fa) dfs(v, u, dep + 1); n     } n     for (auto& i : opr[u]) { n       arr->add(dep, -i.second); n       arr->add(dep + i.first + 1, i.second); n     } n   } n }; n template <typename T = int> n inline T read() { n   T X = 0, w = 0; n   char ch = 0; n   while (!isdigit(ch)) { n     w |= ch ==  - ; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     X = (X << 3) + (X << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return w ? -X : X; n } n int main() { n   int n = read(); n   lfs* tree = new lfs(n); n   for (int i = 1; i < n; i++) { n     tree->addde(read(), read(), 1); n   } n   int m = read(); n   for (int i = 1; i <= m; i++) { n     int u = read(); n     int d = read(); n     int x = read(); n     tree->opr[u].push_back(make_pair(d, x)); n   } n   tree->solve(); n   for (int i = 1; i <= n; i++) { n     cout << tree->ans[i] <<    ; n   } n   cout << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using vi = vector<int>; n using ld = long double; n using pii = pair<int, int>; n ll val(ll solve, ll whole) { n   if (2 * solve > whole) return 500; n   if (4 * solve > whole) return 1000; n   if (8 * solve > whole) return 1500; n   if (16 * solve > whole) return 2000; n   if (32 * solve > whole) return 2500; n   return 3000; n } n ll score(vi &t, vector<ll> &cnt, ll whole) { n   ll ans = 0; n   for (int i = 0; i < t.size(); i++) n     if (t[i] != -1) ans += val(cnt[i], whole) * (250 - t[i]); n   return ans; n } n bool go(vector<vi> &a, ll k, int mask) { n   vector<ll> cnt(a[0].size()); n   for (int i = 0; i < a.size(); i++) n     for (int j = 0; j < a[i].size(); j++) n       if (a[i][j] != -1) cnt[j]++; n   for (int i = 0; i < 5; i++) n     if ((mask & (1 << i)) && a[0][i] != -1) cnt[i] += k; n   ll whole = a.size() + (mask == 0 ? 0 : k); n   return score(a[0], cnt, whole) > score(a[1], cnt, whole); n } n void solve() { n   int n; n   cin >> n; n   vector<vi> a(n, vi(5)); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < 5; j++) cin >> a[i][j]; n   for (int k = 0; k < 32 * n; k++) n     for (int mask = 1; mask < 1 << 5; mask++) n       if (go(a, k, mask)) { n         cout << k <<   n ; n         return; n       } n   cout << -1 <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int i, j; n   string s; n   cin >> s; n   if ((int)s[0] >= 97 && (int)s[0] <= 122) { n     s[0] = (char)((int)s[0] - 32); n   } n   for (i = 0; i < s.size(); i++) { n     cout << s[i]; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 101000; n int a[maxn], x, y, z, cnt, curr, ans, n, m; n vector<int> v; n int bs(int x) { n   int l = 0, r = m; n   while (l < r - 1) { n     int mid2 = (l + r) / 2; n     if (v[mid2] < x) { n       l = mid2; n     } else { n       r = mid2; n     } n   } n   if (v[l] >= x) { n     return -1; n   } n   return l; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     cin >> a[i]; n   } n   for (int j = 0; j < m; ++j) { n     cin >> x >> y >> z; n     if (x == 1) { n       v.push_back(y); n       if (y == 1000000000) { n         ++cnt; n       } n     } n   } n   sort(a, a + n); n   sort(v.begin(), v.end()); n   ans = cnt + n; n   if (!v.size()) { n     cout << 0 << endl; n     return 0; n   } n   v.push_back(1000001000); n   m = v.size(); n   for (int i = 0; i < n; ++i) { n     curr = i; n     int val = bs(a[i]); n     curr += m - val - 2; n     ans = min(ans, curr); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n long long int mod = 1000000007; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int n, k; n   cin >> n >> k; n   long long int p = k / n; n   if (k % n != 0) { n     p += 1; n   } n   cout << p; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, z = 1, x, c, zx; n map<string, long long> m; n string a[1001], s; n int main() { n   while (getline(cin, s)) { n     a[n++] = s; n     zx = max(zx, (long long)s.size()); n   } n   for (int i = -2; i < zx; i++) cout <<  * ; n   cout <<   n ; n   for (int i = 0; i < n; i++) { n     int k = zx - (long long)a[i].size(); n     cout <<  * ; n     for (int j = 0; j < (k + x) / 2; j++) cout <<    ; n     cout << a[i].c_str(); n     for (int j = (k + x) / 2; j < k; j++) cout <<    ; n     cout <<  *  n          <<   n ; n     if (k & 1) x = 1 - x; n   } n   for (int i = -2; i < zx; i++) cout <<  * ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int samples, len, temp, sz; n   cin >> samples >> len; n   deque<int> q; n   set<int> s; n   sz = s.size(); n   for (; samples--;) { n     cin >> temp; n     s.insert(temp); n     if (s.size() > sz) { n       q.push_front(temp); n       if (q.size() > len) { n         s.erase(q[q.size() - 1]); n         q.pop_back(); n       } n     } n     sz = s.size(); n   } n   printf( %d n , q.size()); n   for (auto x : q) printf( %d  , x); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct inter { n   int l, r, id; n   inter() { l = r = id = -1; } n   inter(int _l, int _r, int _id) { l = _l, r = _r, id = _id; } n   bool operator<(const inter &r) const { return l < r.l; } n }; n int N, L, R; n int A[100005], C[100005]; n int B[100005]; n map<int, inter> S; n int main() { n   while (scanf( %d%d%d , &N, &L, &R) == 3) { n     for (int i = 0; i < N; i++) scanf( %d , A + i); n     for (int i = 0; i < N; i++) n       scanf( %d , C + i), S[C[i]] = inter(L - A[i], R - A[i], i); n     int nowL = -2147483646; n     int found = 1; n     for (map<int, inter>::iterator it = S.begin(); it != S.end(); it++) { n       nowL = max(nowL, it->second.l); n       if (nowL > it->second.r) { n         found = -1; n         break; n       } n       B[it->second.id] = nowL++; n     } n     if (found == -1) { n       printf( -1 ); n     } else { n       for (int i = 0; i < N; i++) { n         if (i) printf(   ); n         printf( %d , B[i] + A[i]); n       } n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, c; n int a[1005]; n bool check() { n   bool good = a[0] > 0; n   for (int i = 1; i < n; i++) good &= (a[i] > 0) && (a[i] >= a[i - 1]); n   return good; n } n int main() { n   cin >> n >> m >> c; n   for (int i = 0; i < n; i++) a[i] = 0; n   while (m--) { n     if (check()) return 0; n     int p; n     cin >> p; n     if (p <= c / 2) { n       for (int i = 0; i < n; i++) { n         if (a[i] == 0) { n           a[i] = p; n           cout << i + 1; n           break; n         } n         if (a[i] > p) { n           a[i] = p; n           cout << i + 1; n           break; n         } n       } n     } else { n       for (int i = n - 1; i >= 0; i--) { n         if (a[i] == 0) { n           a[i] = p; n           cout << i + 1; n           break; n         } n         if (a[i] < p) { n           a[i] = p; n           cout << i + 1; n           break; n         } n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 1 << 19; n long long n, k; n long long ans[maxn], mx[maxn], mn[maxn]; n bool occ[maxn]; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k; n   for (long long i = 0; i < (long long)(1 << k); ++i) n     mx[i] = -1e9, mn[i] = 1e9, ans[i] = 2e9; n   for (long long i = 0; i < (long long)(n); ++i) { n     long long u; n     cin >> u; n     occ[u] = 1, mx[u] = u, mn[u] = u; n   } n   for (long long i = 0; i < k; ++i) { n     for (long long j = 0; j < (1 << k); j += (1 << i + 1)) { n       for (long long t = 0; t < (long long)(1 << i); ++t) { n         ans[j + t] = ans[j + t + (1 << i)] = n             min(ans[j + t], ans[j + t + (1 << i)]); n         ans[j + t] = min(ans[j + t], mn[j + t + (1 << i)] - mx[j + t]); n         ans[j + t + (1 << i)] = n             min(ans[j + t + (1 << i)], n                 mn[j + t] - mx[j + t + (1 << i)] + (1 << i + 1)); n       } n     } n     for (long long j = 0; j < (1 << k); j += (1 << i + 1)) { n       for (long long t = 0; t < (long long)(1 << i); ++t) { n         long long upmn0 = min(mn[j + t], mn[j + t + (1 << i)]); n         long long upmn1 = n             min(mn[j + t + (1 << i)] - (1 << i), mn[j + t] + (1 << i)); n         long long upmx0 = max(mx[j + t], mx[j + t + (1 << i)]); n         long long upmx1 = n             max(mx[j + t + (1 << i)] - (1 << i), mx[j + t] + (1 << i)); n         mn[j + t] = upmn0, mn[j + t + (1 << i)] = upmn1; n         mx[j + t] = upmx0, mx[j + t + (1 << i)] = upmx1; n       } n     } n   } n   for (long long i = 0; i < (long long)(1 << k); ++i) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int d[2005]; n vector<int> E[2005]; n bool h[2005]; n const int INF = 1e9 + 7; n void add(int u, int v) { E[u].push_back(v); } n void bfs() { n   queue<int> q; n   d[0] = 0; n   q.push(0); n   while (q.size()) { n     int t = q.front(); n     q.pop(); n     for (int i = 0; i < E[t].size(); i++) { n       if (d[E[t][i]] == INF) { n         d[E[t][i]] = d[t] + 1; n         q.push(E[t][i]); n         if (E[t][i] == 1001) { n           return; n         } n       } n     } n   } n   return; n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 0; i <= 2001; i++) d[i] = INF; n   for (int i = 1; i <= k; i++) { n     int x; n     scanf( %d , &x); n     x -= n; n     if (!h[x + 1001]) { n       add(0, x + 1001); n       h[x + 1001] = 1; n     } n   } n   for (int i = 1; i <= 2001; i++) { n     for (int j = 1; j <= 2001; j++) { n       if (h[j] && (0 <= i + j - 1001 && i + j - 1001 <= 2001)) { n         add(i, i + j - 1001); n       } n     } n   } n   bfs(); n   if (d[1001] != INF) n     printf( %d n , d[1001]); n   else n     puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ncr(int n, int r) { n   int ret = 1; n   for (int i = 1; i <= n - r; i++) { n     ret = (ret * (n - r + i)) / i; n   } n   return ret; n } n int main() { n   int n; n   cin >> n; n   int x; n   long long pos = 0; n   long long neg = 0; n   long long prod = 1; n   for (int i = 0; i < n; i++) { n     scanf( %d , &x); n     if (x < 0) { n       prod = (prod + 1) & 1; n     } n     if (prod) { n       pos++; n     } else { n       neg++; n     } n   } n   cout << neg * pos + neg <<    ; n   cout << (neg * (neg - 1)) / 2 + (pos * (pos - 1)) / 2 + pos << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200005; n int a[maxn]; n struct node { n   int l, r, Max, Min, pa, pi; n } T[maxn << 2]; n void pushup(int o) { n   T[o].Min = min(T[o << 1].Min, T[o << 1 | 1].Min); n   T[o].Max = max(T[o << 1].Max, T[o << 1 | 1].Max); n   if (T[o].Min == T[o << 1].Min) n     T[o].pi = T[o << 1].pi; n   else n     T[o].pi = T[o << 1 | 1].pi; n   if (T[o].Max == T[o << 1].Max) n     T[o].pa = T[o << 1].pa; n   else n     T[o].pa = T[o << 1 | 1].pa; n } n void build(int o, int l, int r) { n   T[o].l = l; n   T[o].r = r; n   if (l == r) { n     T[o].Max = T[o].Min = a[l]; n     T[o].pa = T[o].pi = l; n     return; n   } n   int mid = (l + r) >> 1; n   build(o << 1, l, mid); n   build(o << 1 | 1, mid + 1, r); n   pushup(o); n } n int query(int o, int l, int r, int op) { n   if (T[o].l == l && T[o].r == r) { n     if (op == 1) n       return T[o].Max; n     else if (op == 2) n       return T[o].Min; n     else if (op == 3) n       return T[o].pa; n     else n       return T[o].pi; n   } n   int mid = (T[o].l + T[o].r) >> 1; n   if (r <= mid) n     return query(o << 1, l, r, op); n   else if (l > mid) n     return query(o << 1 | 1, l, r, op); n   else { n     if (op == 1) n       return max(query(o << 1, l, mid, 1), query(o << 1 | 1, mid + 1, r, 1)); n     else if (op == 2) n       return min(query(o << 1, l, mid, 2), query(o << 1 | 1, mid + 1, r, 2)); n     else if (op == 3) { n       int ans1 = query(o << 1, l, mid, 1), n           ans2 = query(o << 1 | 1, mid + 1, r, 1); n       int ans = max(ans1, ans2); n       if (ans == ans1) n         return query(o << 1, l, mid, 3); n       else n         return query(o << 1 | 1, mid + 1, r, 3); n     } else { n       int ans1 = query(o << 1, l, mid, 2), n           ans2 = query(o << 1 | 1, mid + 1, r, 2); n       int ans = min(ans1, ans2); n       if (ans == ans1) n         return query(o << 1, l, mid, 4); n       else n         return query(o << 1 | 1, mid + 1, r, 4); n     } n   } n } n int main() { n   int n, m; n   while (~scanf( %d%d , &n, &m)) { n     for (int i = 1; i <= n; i++) scanf( %d , a + i); n     build(1, 1, n); n     int l, r, x, Max, Min, pa, pi; n     while (m--) { n       scanf( %d%d%d , &l, &r, &x); n       Max = query(1, l, r, 1); n       Min = query(1, l, r, 2); n       pa = query(1, l, r, 3); n       pi = query(1, l, r, 4); n       int ans; n       if (Max == Min && Max == x) n         ans = -1; n       else if (Min != x) n         ans = pi; n       else if (Max != x) n         ans = pa; n       printf( %d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n typedef struct pair { n   int x, y; n } pair; n int const N = 1e4 + 5; n int n = 8; n pair sMatrix[N], sac[N]; n char matrix[N][N], mariaMove[10][10][100]; n pair anna, maria; n int i, j, k, s, m, h, t; n int kontrol(int x, int y) { n   int i, j; n   for (i = 0; i < s; i++) { n     if (sMatrix[i].x == x && sMatrix[i].y == y) return 0; n   } n   return 1; n } n void sDown(int add) { n   int i; n   for (i = 0; i < s; i++) { n     sMatrix[i].x += add; n   } n } n void wrtS() { n   int i; n   for (i = 0; i < s; i++) { n     printf( %d-SS-%d , sMatrix[i].x, sMatrix[i].y), puts(  ); n   } n } n void dfs(int x, int y, int t) { n   int i, j, k; n   if (x == anna.x && y == anna.y) { n     printf( WIN ), puts(  ); n     exit(0); n   } n   if (!(!mariaMove[x][y][t] && x >= 0 && x < n && y >= 0 && y < n) || n       !kontrol(x, y)) n     return; n   sDown(1); n   if (!kontrol(x, y)) { n     sDown(-1); n     return; n   } n   mariaMove[x][y][t] = 1; n   for (i = -1, k = 0; i <= 1; i++) { n     for (j = -1; j <= 1; j++, k++) { n       dfs(x + i, y + j, k); n     } n   } n   sDown(-1); n } n void clearMariaMove() { n   int i, j, k; n   for (i = 0; i < 10; i++) { n     for (j = 0; j < 10; j++) { n       for (k = 0; k < 100; k++) { n         mariaMove[i][j][k] = 0; n       } n     } n   } n } n void clear_sMatrix() { n   int i; n   for (i = 0; i < s; i++) { n     sMatrix[i].x = sac[i].x; n     sMatrix[i].y = sac[i].y; n   } n } n int main() { n   for (i = 0; i < n; i++) { n     scanf( %s , matrix[i]); n     for (j = 0; j < n; j++) { n       if (matrix[i][j] ==  S ) { n         sMatrix[s].x = i, sMatrix[s].y = j; n         sac[s].x = i, sac[s].y = j; n         s++; n       } n     } n   } n   maria.x = 7, maria.y = 0; n   anna.x = 0, anna.y = 7; n   for (i = -1; i <= 0; i++) { n     for (j = 0; j <= 1; j++) { n       dfs(maria.x + i, maria.y + j, 0); n       clearMariaMove(); n       clear_sMatrix(); n     } n   } n   puts( LOSE ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n clock_t timeStart, timeFinish; n void timeBegin() { timeStart = clock(); } n void timeEnd() { timeFinish = clock(); } n void timeDuration() { n   double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC); n   cout <<  Time taken by program is :   << fixed << time_taken n        << setprecision(5); n   cout <<   sec   << endl; n } n long long int n, h, a[105]; n bool fun(long long int x) { n   long long int val = 0; n   for (int i = 0; i < n - 1; i++) { n     val += min(a[i + 1] - a[i], x); n   } n   val += x; n   if (val >= h) { n     return true; n   } n   return false; n } n void solve() { n   cin >> n >> h; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   long long int l = 1, r = 1000000000000000000; n   long long int ans = r; n   while (l <= r) { n     long long int mid = l + (r - l) / 2; n     if (fun(mid)) { n       ans = mid; n       r = mid - 1; n     } else { n       l = mid + 1; n     } n   } n   cout << ans <<   n ; n   return; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   cin >> t; n   for (int i = 0; i < t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n using namespace std; n const int INF = 1e9 + 7; n const int N = 2e5 + 7; n const int M = 1e6 + 7; n long long a[N], b[N]; n long long dp[N][2]; n signed main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     if (i > 1) b[i - 1] = abs(a[i] - a[i - 1]); n   } n   for (int i = 1; i <= n; i++) { n     dp[i][0] = max(dp[i - 1][1] + b[i], b[i]); n     dp[i][1] = max(dp[i - 1][0] - b[i], 0ll); n   } n   long long ans = 0; n   for (int i = 1; i < n; i++) ans = max(ans, max(dp[i][0], dp[i][1])); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1005; n const int INF = 0x3f3f3f3f; n int n, w[N], h[N]; n int maxx; n int sum, sum1, ans = INF; n inline void fuck(int id, int tot) { n   sum1 = sum; n   priority_queue<int> q; n   int tall; n   if (tot == 1) n     tall = w[id], sum1 = sum1 - w[id] + h[id]; n   else n     tall = h[id]; n   for (int i = 1; i <= n; ++i) { n     if (i == id) continue; n     if (w[i] > tall && h[i] > tall) return; n     if (w[i] > tall || h[i] > tall) { n       if (h[i] > tall) sum1 = sum1 - w[i] + h[i], tot++; n     } else if (w[i] <= tall && h[i] <= tall) { n       q.push(w[i] - h[i]); n     } n   } n   if (tot > n / 2) return; n   while (!q.empty()) { n     tot++; n     if (tot > n / 2) break; n     int now = q.top(); n     q.pop(); n     if (now <= 0) break; n     sum1 -= now; n   } n   ans = min(ans, sum1 * tall); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d%d , &w[i], &h[i]), sum += w[i]; n   for (int i = 1; i <= n; ++i) { n     fuck(i, 0); n     fuck(i, 1); n   } n   printf( %d , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, s, q, ans = 0; n   cin >> t >> s >> q; n   int downloaded = s; n   while (downloaded < t) { n     downloaded += s * (q - 1); n     s = downloaded; n     ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long maxn = 2e5 + 5; n struct Node { n   long long ls, rs, val, key, cnt, tag, add; n }; n Node t[maxn]; n long long pol; n void newnode(long long vv) { n   t[++pol].val = vv; n   t[pol].cnt = 0; n   t[pol].key = rand(); n   t[pol].ls = t[pol].rs = 0; n   t[pol].tag = t[pol].add = 0; n   return; n } n void lazy1(long long p, long long k) { n   t[p].val += k; n   return; n } n void lazy2(long long p, long long k) { n   t[p].cnt += k; n   return; n } n void pushdown(long long p) { n   t[t[p].ls].tag += t[p].tag; n   t[t[p].rs].tag += t[p].tag; n   lazy1(t[p].ls, t[p].tag); n   lazy1(t[p].rs, t[p].tag); n   t[p].tag = 0; n   t[t[p].ls].add += t[p].add; n   t[t[p].rs].add += t[p].add; n   lazy2(t[p].ls, t[p].add); n   lazy2(t[p].rs, t[p].add); n   t[p].add = 0; n   return; n } n long long merge(long long x, long long y) { n   if (!x || !y) return x + y; n   if (t[x].key < t[y].key) { n     pushdown(x); n     t[x].rs = merge(t[x].rs, y); n     return x; n   } else { n     pushdown(y); n     t[y].ls = merge(x, t[y].ls); n     return y; n   } n   return 0; n } n void split(long long now, long long &rx, long long &ry, long long k) { n   if (!now) { n     rx = ry = 0; n     return; n   } n   pushdown(now); n   if (t[now].val <= k) { n     rx = now; n     split(t[now].rs, t[rx].rs, ry, k); n   } else { n     ry = now; n     split(t[now].ls, rx, t[ry].ls, k); n   } n   return; n } n long long rt, rx, ry, rz; n void ins(long long nd) { n   split(rt, rx, rz, t[nd].val); n   rt = merge(merge(rx, nd), rz); n   return; n } n void dfs(long long now, long long cc) { n   if (!now) return; n   pushdown(now); n   t[now].val -= cc; n   t[now].cnt++; n   dfs(t[now].ls, cc); n   dfs(t[now].rs, cc); n   t[now].ls = t[now].rs = 0; n   ins(now); n   return; n } n void out(long long now) { n   if (!now) return; n   pushdown(now); n   out(t[now].ls); n   cout << t[now].val <<    ; n   out(t[now].rs); n   return; n } n void costv(long long cc) { n   split(rt, rx, ry, cc - 1); n   split(ry, ry, rz, 2 * cc); n   t[rz].tag -= cc; n   lazy1(rz, -cc); n   t[rz].add++; n   lazy2(rz, 1); n   rt = merge(rx, rz); n   dfs(ry, cc); n   return; n } n struct tshirts { n   long long c, q; n   bool friend operator<(tshirts t1, tshirts t2) { n     return t1.q == t2.q ? t1.c < t2.c : t1.q > t2.q; n   } n }; n tshirts st[maxn]; n long long n, k; n long long v[maxn]; n void dfsans(long long now) { n   if (!now) return; n   pushdown(now); n   dfsans(t[now].ls); n   dfsans(t[now].rs); n   return; n } n int main() { n   scanf( %lld , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld %lld , &st[i].c, &st[i].q); n   } n   sort(st + 1, st + n + 1); n   scanf( %lld , &k); n   for (int i = 1; i <= k; i++) { n     scanf( %lld , &v[i]); n     newnode(v[i]); n     ins(i); n   } n   for (int i = 1; i <= n; i++) { n     costv(st[i].c); n   } n   dfsans(rt); n   for (int i = 1; i <= k; i++) { n     printf( %lld  , t[i].cnt); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mas[1010]; n unordered_map<long long, int> sootv; n long long num[100100]; n long long gcd(long long a, long long b) { n   if (!a) return b; n   return gcd(b % a, a); n } n int main() { n   int n; n   long long k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     cin >> mas[i]; n   } n   if (k == 1) { n     cout << 1 << endl; n     int minidx = 1; n     for (int i = 0; i < n; i++) { n       if (mas[i] < mas[minidx]) minidx = i; n     } n     cout << minidx + 1; n     return 0; n   } n   vector<long long> divs; n   vector<long long> rdivs; n   for (long long i = 1; i * i <= k; i++) { n     if (k % i == 0) { n       divs.push_back(i); n       if (i * i != k) { n         rdivs.push_back(k / i); n       } n     } n   } n   for (auto it = rdivs.rbegin(); it != rdivs.rend(); it++) { n     divs.push_back(*it); n   } n   int cnt = 1; n   sootv.rehash((int)(divs).size() * 5); n   for (int i = 0; i < (int)(divs).size(); i++) { n     sootv.emplace(divs[i], cnt); n     num[cnt] = divs[i]; n     cnt++; n   } n   int oo = 1e9; n   vector<vector<int> > dp(n + 3); n   vector<vector<long long> > sum(n + 3); n   vector<vector<int> > lst(n + 3); n   for (int i = 0; i < (int)(dp).size(); i++) { n     dp[i].resize(cnt + 3); n     lst[i].resize(cnt + 3); n     sum[i].resize(cnt + 3); n     for (int j = 0; j < (int)(dp[i]).size(); j++) { n       dp[i][j] = oo; n     } n     for (int j = 0; j < (int)(lst[i]).size(); j++) { n       lst[i][j] = -1; n     } n     for (int j = 0; j < (int)(sum[i]).size(); j++) { n       sum[i][j] = (long long)oo * (long long)oo; n     } n   } n   dp[0][sootv[k]] = 0; n   sum[0][sootv[k]] = 0; n   int res = oo; n   for (int i = 0; i < n; i++) { n     for (int divi = 1; divi < cnt; divi++) { n       if (dp[i][divi] >= oo) continue; n       if (dp[i + 1][divi] > dp[i][divi] || n           dp[i + 1][divi] == dp[i][divi] && sum[i + 1][divi] > sum[i][divi]) { n         lst[i + 1][divi] = divi; n         dp[i + 1][divi] = dp[i][divi]; n         sum[i + 1][divi] = sum[i][divi]; n       } n       long long nxt = num[divi] / gcd(num[divi], mas[i]); n       if (nxt == num[divi]) continue; n       int idx = sootv[nxt]; n       if (dp[i + 1][idx] > dp[i][divi] + 1 || n           dp[i + 1][idx] == dp[i][divi] + 1 && n               sum[i + 1][idx] > sum[i][divi] + mas[i]) { n         lst[i + 1][idx] = divi; n         dp[i + 1][idx] = dp[i][divi] + 1; n         sum[i + 1][idx] = sum[i][divi] + mas[i]; n       } n     } n   } n   res = dp[n][sootv[1]]; n   if (res == oo) { n     res = -1; n     cout << res << endl; n     return 0; n   } n   cout << res << endl; n   vector<int> path; n   int cur = sootv[1]; n   for (int i = n; i >= 1; i--) { n     int l = lst[i][cur]; n     if (l != cur) { n       path.push_back(i); n     } n     cur = l; n     if ((int)(path).size() == res) break; n   } n   for (int i = (int)(path).size() - 1; i >= 0; i--) printf( %d  , path[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100000; n int aa[N]; n map<int, int> mp; n set<int> s; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> aa[i]; n   for (int i = 0; i < n; i++) { n     mp[aa[i]]++; n     if (mp[aa[i]] == 1) n       s.insert(aa[i]); n     else n       s.erase(aa[i]); n     int j = i - k; n     if (j >= 0) { n       mp[aa[j]]--; n       if (mp[aa[j]] == 1) n         s.insert(aa[j]); n       else n         s.erase(aa[j]); n     } n     if (i >= k - 1) n       if (!s.empty()) n         cout << *--s.end() <<   n ; n       else n         cout <<  Nothing n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e9 + 7; n class DSU { n  public: n   vector<int> parent; n   vector<int> sizes; n   int noOfComponenents; n   DSU(int N) { n     parent.resize(N); n     sizes.resize(N); n     noOfComponenents = N; n     for (int i = 0; i < N; ++i) { n       parent[i] = i; n       sizes[i] = 1; n     } n   } n   int find(int x) { n     if (parent[x] != x) parent[x] = find(parent[x]); n     return parent[x]; n   } n   void unify(int x, int y) { n     x = find(x); n     y = find(y); n     if (isConnected(x, y)) { n       return; n     } n     --noOfComponenents; n     sizes[y] += sizes[x]; n     parent[x] = y; n   } n   bool isConnected(int p, int q) { return find(p) == find(q); } n   int componentSize(int p) { return sizes[p]; } n }; n void solve() { n   int n; n   cin >> n; n   string s, t; n   cin >> s >> t; n   DSU dsu(20); n   for (int i = 0; i < n; ++i) { n     if (s[i] == t[i]) { n       continue; n     } n     if (s[i] > t[i]) { n       cout << -1 <<   n ; n       return; n     } n     dsu.unify(s[i] -  a , t[i] -  a ); n   } n   cout << 20 - dsu.noOfComponenents <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long C(long long n, long long m) { n   long long res = 1; n   for (long long i = n; i > n - m; i--) res *= i; n   for (long long i = m; i >= 1; i--) res /= i; n   return res; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0); n   long long n, k, res = 1; n   cin >> n >> k; n   if (k >= 2) res += C(n, 2); n   if (k >= 3) res += C(n, 3) * 2; n   if (k >= 4) res += C(n, 4) * 9; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n const int M = 205; n long long n, m, k, cnt, ans; n long long g[N], h[N], dp[N][M]; n struct redpocket { n   long long s, t, d, w; n   bool operator<(const redpocket &x) const { n     if (w == x.w) return d < x.d; n     return w < x.w; n   } n } a[N]; n priority_queue<redpocket> q; n bool cmp(redpocket x, redpocket y) { return x.s < y.s; } n signed main() { n   scanf( %lld %lld %lld , &n, &m, &k); n   for (long long i = 1; i <= k; i++) n     scanf( %lld %lld %lld %lld , &a[i].s, &a[i].t, &a[i].d, &a[i].w); n   sort(a + 1, a + k + 1, cmp); n   g[0] = 1; n   cnt = 1; n   for (long long i = 1; i <= n; i++) { n     while (i >= a[cnt].s && cnt <= k) q.push(a[cnt++]); n     if (q.empty()) n       g[i] = i + 1; n     else { n       if (q.top().t < i) { n         i--; n         q.pop(); n         continue; n       } n       g[i] = q.top().d + 1; n       h[i] = q.top().w; n     } n   } n   memset(dp, 0x3f, sizeof(dp)); n   dp[0][0] = 0; n   for (long long i = 0; i <= n; i++) { n     for (long long j = 0; j <= m; j++) { n       dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]); n       dp[g[i]][j] = min(dp[g[i]][j], dp[i][j] + h[i]); n     } n   } n   ans = 0x3f3f3f3f3f3f3f3f; n   for (long long i = 0; i <= m; i++) ans = min(ans, dp[n + 1][i]); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void PT(long long a); n void PT1(long long a); n const long long M = 2010, mod = 1e9 + 7; n long long n, m, x, y, a, b, dp[M][M], mp[M][M], mp1[M][M]; n vector<string> v; n void bfs(long long r, long long c, long long l, long long ri) { n   queue<pair<long long, long long>> q; n   q.push({r, c}); n   mp[r][c] = l; n   mp1[r][c] = ri; n   while (!q.empty()) { n     r = (q.front()).first; n     c = (q.front()).second; n     q.pop(); n     l = mp[r][c]; n     ri = mp1[r][c]; n     if (l <= a && ri <= b) dp[r][c] = 1; n     if (c - 1 >= 0 && v[r][c - 1] !=  * ) { n       if (l + 1 <= a && ri <= b && n           (mp[r][c - 1] > l + 1 || mp1[r][c - 1] > ri)) { n         q.push({r, c - 1}); n         mp[r][c - 1] = l + 1; n         mp1[r][c - 1] = ri; n       } n     } n     if (c + 1 < m && v[r][c + 1] !=  * ) { n       if (l <= a && ri + 1 <= b && n           (mp[r][c + 1] > l || mp1[r][c + 1] > ri + 1)) { n         q.push({r, c + 1}); n         mp[r][c + 1] = l; n         mp1[r][c + 1] = ri + 1; n       } n     } n     if (r - 1 >= 0 && v[r - 1][c] !=  * ) { n       if (l <= a && ri <= b && (mp[r - 1][c] > l || mp1[r - 1][c] > ri)) { n         q.push({r - 1, c}); n         mp[r - 1][c] = l; n         mp1[r - 1][c] = ri; n       } n     } n     if (r + 1 < n && v[r + 1][c] !=  * ) { n       if (l <= a && ri <= b && (mp[r + 1][c] > l || mp1[r + 1][c] > ri)) { n         q.push({r + 1, c}); n         mp[r + 1][c] = l; n         mp1[r + 1][c] = ri; n       } n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   { n     cin >> n >> m >> x >> y >> a >> b; n     for (long long i = 0; i < n; i++) { n       string s; n       cin >> s; n       v.push_back(s); n     } n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < m; j++) { n         mp[i][j] = INT_MAX; n         mp1[i][j] = INT_MAX; n       } n     } n     bfs(x - 1, y - 1, 0, 0); n     long long ans = 0; n     for (long long i = 0; i < n; i++) { n       for (long long j = 0; j < m; j++) { n         if (dp[i][j] == 1) ans++; n       } n     } n     cout << ans; n   } n   return 0; n } n void PT(long long a) { cout << a <<   n ; } n void PT1(long long a) { cout << a <<    ; } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long int i, j, d, m, n, a[100000]; n   while (cin >> n) { n     for (i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     i = 1; n     j = n; n     int A = a[i], B = a[j]; n     int ca = 0, cb = 0; n     while (1) { n       if (A > B) { n         B += a[--j]; n         cb++; n       } else if (B >= A) { n         A += a[++i]; n         ca++; n       } n       if (i > j) break; n     } n     cout << ca <<     << cb << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<long long> sc(n); n   for (typeof(n) i = (0); i < (n); i++) cin >> sc[i]; n   int m; n   cin >> m; n   for (typeof(m) i = (0); i < (m); i++) { n     long long w, h; n     cin >> w >> h; n     long long b = max(sc[0], sc[w - 1]); n     sc[0] = max(sc[0], b + h); n     cout << b << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC diagnostic ignored  -Wformat  n template <typename T> n void printContainer(T& a) { n   auto it = a.begin(); n   cout <<  {  << *(it++); n   for (; it != a.end(); ++it) cout <<  ,   << (*it); n   cout <<  } n ; n } n map<long long, int> A1, A2; n int sol; n long long solarea; n void dfs1(long long area, int opsSoFar) { n   A1[area] = opsSoFar; n   if (area % 3 == 0) { n     if (!A1.count(area / 3 * 2)) { n       dfs1(area / 3 * 2, opsSoFar + 1); n     } n   } n   if (area % 2 == 0) { n     if (!A1.count(area / 2)) dfs1(area / 2, opsSoFar + 1); n   } n } n void dfs2(long long area, int opsSoFar) { n   A2[area] = opsSoFar; n   if (A1.count(area)) { n     if (opsSoFar + A1[area] < sol) { n       sol = opsSoFar + A1[area]; n       solarea = area; n     } n     return; n   } n   if (area % 3 == 0) { n     if (!A2.count(area / 3 * 2)) { n       dfs2(area / 3 * 2, opsSoFar + 1); n     } n   } n   if (area % 2 == 0) { n     if (!A2.count(area / 2)) dfs2(area / 2, opsSoFar + 1); n   } n } n int threeFactors(long long num) { n   int cnt = 0; n   while (num % 3 == 0) num /= 3, cnt++; n   return cnt; n } n int main() { n   long long a1, b1, a2, b2; n   cin >> a1 >> b1 >> a2 >> b2; n   long long S1 = a1 * b1, S2 = a2 * b2; n   dfs1(S1, 0); n   sol = 1e9; n   dfs2(S2, 0); n   if (sol == 1e9) { n     cout <<  -1  << endl; n     return 0; n   } n   int T = 0, D = 0; n   cout << sol << endl; n   T = threeFactors(S1) - threeFactors(solarea); n   long long tmp = S1; n   for (int i = 0; i < (int)(T); ++i) tmp = (tmp / 3 * 2); n   while (tmp != solarea) ++D, tmp /= 2; n   while (a1 % 3 == 0 && T > 0) a1 = (a1 / 3 * 2), --T; n   while (b1 % 3 == 0 && T > 0) b1 = (b1 / 3 * 2), --T; n   while (a1 % 2 == 0 && D > 0) a1 = (a1 / 2), --D; n   while (b1 % 2 == 0 && D > 0) b1 = (b1 / 2), --D; n   cout << a1 <<     << b1 << endl; n   T = 0, D = 0; n   T = threeFactors(S2) - threeFactors(solarea); n   tmp = S2; n   for (int i = 0; i < (int)(T); ++i) tmp = (tmp / 3 * 2); n   while (tmp != solarea) ++D, tmp /= 2; n   while (a2 % 3 == 0 && T > 0) a2 = (a2 / 3 * 2), --T; n   while (b2 % 3 == 0 && T > 0) b2 = (b2 / 3 * 2), --T; n   while (a2 % 2 == 0 && D > 0) a2 = (a2 / 2), --D; n   while (b2 % 2 == 0 && D > 0) b2 = (b2 / 2), --D; n   cout << a2 <<     << b2 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 17; n int l1[N], r1[N], a[N], use[N]; n map<int, bool> mt; n void dfs(int v, int l, int r) { n   if (a[v] > l && a[v] < r) mt[a[v]] = 1; n   if (l1[v] != -1) dfs(l1[v], l, min(r, a[v])); n   if (r1[v] != -1) dfs(r1[v], max(l, a[v]), r); n } n int main() { n   ios_base::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 >> a[i] >> l1[i] >> r1[i]; n     if (l1[i] != -1) use[l1[i]] = 1; n     if (r1[i] != -1) use[r1[i]] = 1; n   } n   for (int i = 1; i <= n; i++) n     if (use[i] == 0) dfs(i, -1, 1e9 + 1); n   int ans = 0; n   for (int i = 1; i <= n; i++) n     if (mt[a[i]] == 0) ans++; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n int n; n int ar[100005]; n int main() { n   scanf( %d , &n); n   if (n == 2) { n     printf( 2 1 ); n     return 0; n   } n   for (int i = 1; i <= n; i++) ar[i] = i; n   for (int i = 1; i <= n; i++) { n     for (int j = i + i; j <= n; j += i) { n       if (j % i == 0 && ar[j] % i == 0) swap(ar[i], ar[j]); n     } n   } n   for (int i = 1; i <= n; i++) printf( %d  , ar[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct pt { n   long long x, y; n   int id; n   pt() {} n   pt(long long a, long long b, int c = -1) : x(a), y(b), id(c) {} n }; n pt operator+(pt a, pt b) { return pt(a.x + b.x, a.y + b.y); } n pt operator-(pt a, pt b) { return pt(a.x - b.x, a.y - b.y); } n long long dot(pt a, pt b) { return a.x * b.x + a.y * b.y; } n long long cross(pt a, pt b) { return a.x * b.y - a.y * b.x; } n long long sq(pt a) { return dot(a, a); } n long long dist(pt a, pt b) { return sq(a - b); } n bool operator<(pt a, pt b) { return make_pair(a.x, a.y) < make_pair(b.x, b.y); } n vector<pt> ConvexHull(vector<pt> a) { n   sort(a.begin(), a.end()); n   vector<pt> b; n   for (int i = 0; i < a.size(); i++) { n     while (b.size() > 1 && n            cross(a[i] - b.back(), b[b.size() - 2] - b.back()) <= 0) n       b.pop_back(); n     b.push_back(a[i]); n   } n   int sz = b.size(); n   for (int i = (int)a.size() - 2; i >= 0; i--) { n     while (b.size() > sz && n            cross(a[i] - b.back(), b[b.size() - 2] - b.back()) <= 0) n       b.pop_back(); n     b.push_back(a[i]); n   } n   b.pop_back(); n   return b; n } n const int N = 1050; n int ty[N]; n void rot(vector<pt> &a) { n   int p = -1; n   for (int i = 1; i < a.size(); i++) n     if (ty[a[i].id] != ty[a[0].id]) { n       p = i; n       break; n     } n   if (p == -1) return; n   vector<pt> b; n   for (int i = 0; i < a.size(); i++) b.push_back(a[(i + p) % a.size()]); n   a = b; n } n vector<pair<int, int> > edges; n void AddEdge(int u, int v) { edges.push_back(make_pair(u, v)); } n long long Area(pt a, pt b, pt c) { n   return abs(cross(a, b) + cross(b, c) + cross(c, a)); n } n bool Inside(pt a, pt b, pt c, pt d) { n   return Area(a, b, c) == Area(a, b, d) + Area(a, c, d) + Area(b, c, d); n } n void SolveTriangle(pt a, pt b, pt c, vector<pt> ps) { n   int p = -1; n   for (int i = 0; i < ps.size(); i++) n     if (ty[ps[i].id] == ty[c.id]) p = i; n   if (p == -1) { n     for (int i = 0; i < ps.size(); i++) AddEdge(a.id, ps[i].id); n   } else { n     pt d = ps[p]; n     AddEdge(d.id, c.id); n     vector<pt> nxt[3]; n     for (int i = 0; i < ps.size(); i++) n       if (i != p) { n         if (Inside(a, b, d, ps[i])) n           nxt[0].push_back(ps[i]); n         else if (Inside(c, d, a, ps[i])) n           nxt[1].push_back(ps[i]); n         else n           nxt[2].push_back(ps[i]); n       } n     SolveTriangle(a, b, d, nxt[0]); n     SolveTriangle(c, d, a, nxt[1]); n     SolveTriangle(c, d, b, nxt[2]); n   } n } n bool Check(vector<pt> hull) { n   int l = -1, r = hull.size(); n   while (l + 1 < hull.size() && ty[hull[l + 1].id] == ty[hull[0].id]) l++; n   while (r - 1 >= 0 && ty[hull[r - 1].id] == ty[hull.back().id]) r--; n   if (r - l > 1) return 0; n   return 1; n } n bool onHull[N]; n void Triangulate(vector<pt> a) { n   vector<pt> hull = ConvexHull(a); n   rot(hull); n   if (!Check(hull)) { n     printf( Impossible n ); n     exit(0); n   } n   for (int i = 0; i < hull.size(); i++) onHull[hull[i].id] = 1; n   if (ty[hull[0].id] == ty[hull.back().id]) { n     for (int i = 0; i + 1 < hull.size(); i++) n       AddEdge(hull[i].id, hull[i + 1].id); n     int p = -1; n     for (int i = 0; i < a.size(); i++) n       if (!onHull[a[i].id]) { n         if (ty[a[i].id] != ty[hull[0].id]) { n           p = i; n           break; n         } n       } n     if (p != -1) { n       for (int i = 0; i < hull.size(); i++) { n         pt A = hull[i], B = hull[(i + 1) % hull.size()], C = a[p]; n         vector<pt> work; n         for (int j = 0; j < a.size(); j++) n           if (!onHull[a[j].id] && j != p) { n             if (Inside(A, B, C, a[j])) work.push_back(a[j]); n           } n         SolveTriangle(A, B, C, work); n       } n     } else { n       for (int i = 0; i < a.size(); i++) n         if (!onHull[a[i].id]) AddEdge(hull[0].id, a[i].id); n     } n   } else { n     int p = -1; n     for (int i = 0; i < hull.size(); i++) n       if (ty[hull[i].id] != ty[hull[0].id]) { n         p = i; n         break; n       } n     if (p == -1) { n       printf( ??? n ); n       exit(0); n     } n     for (int i = 0; i + 1 < p; i++) { n       pt A = hull[i], B = hull[i + 1], C = hull[p]; n       AddEdge(A.id, B.id); n       vector<pt> work; n       for (int j = 0; j < a.size(); j++) n         if (!onHull[a[j].id]) { n           if (Inside(A, B, C, a[j])) work.push_back(a[j]); n         } n       SolveTriangle(A, B, C, work); n     } n     for (int i = p; i + 1 < hull.size(); i++) { n       pt A = hull[i], B = hull[i + 1], C = hull[0]; n       AddEdge(A.id, B.id); n       vector<pt> work; n       for (int j = 0; j < a.size(); j++) n         if (!onHull[a[j].id]) { n           if (Inside(A, B, C, a[j])) work.push_back(a[j]); n         } n       SolveTriangle(A, B, C, work); n     } n   } n } n int main() { n   int n, i; n   scanf( %i , &n); n   vector<pt> ps(n); n   for (int i = 0; i < n; i++) { n     int x, y; n     scanf( %i %i %i , &x, &y, &ty[i]); n     ps[i] = pt(x, y, i); n   } n   if (n == 1) return 0 * printf( 0 n ); n   if (n == 2) { n     if (ty[0] == ty[1]) n       printf( 1 n0 1 n ); n     else n       printf( 0 n ); n     return 0; n   } n   Triangulate(ps); n   printf( %i n , edges.size()); n   for (auto p : edges) printf( %i %i n , p.first, p.second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005, mod = 1e9 + 7; n int n, head[N], nxt[N << 1], to[N << 1], num = 0, dfn[N], DFN = 0, a[N], dep[N]; n inline void link(int x, int y) { n   nxt[++num] = head[x]; n   to[num] = y; n   head[x] = num; n } n namespace tr { n int fa[N], top[N], sz[N], son[N]; n inline void dfs1(int x) { n   sz[x] = 1; n   dfn[x] = ++DFN; n   for (int i = head[x]; i; i = nxt[i]) { n     int u = to[i]; n     if (sz[u]) continue; n     fa[u] = x; n     dep[u] = dep[x] + 1; n     dfs1(u); n     sz[x] += sz[u]; n     if (sz[u] > sz[son[x]]) son[x] = u; n   } n } n inline void dfs2(int x, int tp) { n   top[x] = tp; n   if (son[x]) dfs2(son[x], tp); n   for (int i = head[x]; i; i = nxt[i]) n     if (to[i] != fa[x] && to[i] != son[x]) dfs2(to[i], to[i]); n } n inline int lca(int x, int y) { n   while (top[x] != top[y]) { n     if (dep[top[x]] < dep[top[y]]) swap(x, y); n     x = fa[top[x]]; n   } n   return dep[x] < dep[y] ? x : y; n } n void main() { n   dep[1] = 1; n   dfs1(1); n   dfs2(1, 1); n } n }  // namespace tr n int prime[N], sp = 0, mu[N], phi[N], b[N], m = 0, id[N], g[N]; n bool vis[N]; n void priwork() { n   phi[1] = 1; n   mu[1] = 1; n   for (int to, i = 2; i <= n; i++) { n     if (!vis[i]) prime[++sp] = i, mu[i] = -1, phi[i] = i - 1; n     for (int j = 1; j <= sp && i * prime[j] <= n; j++) { n       vis[to = i * prime[j]] = 1; n       if (i % prime[j]) n         mu[to] = -mu[i], phi[to] = phi[i] * (prime[j] - 1); n       else { n         phi[to] = phi[i] * prime[j]; n         break; n       } n     } n   } n   tr::main(); n } n inline bool comp(int i, int j) { return dfn[i] < dfn[j]; } n int st[N], top, w[N], f[N], inv[N], ST[N], cnt = 0; n inline int solve() { n   int sum = 0, x, lca, ret = 0, y = 0; n   top = 0; n   for (int i = 1; i <= m; i++) n     sum = (sum + phi[a[b[i]]]) % mod, w[b[i]] = phi[a[b[i]]]; n   sort(b + 1, b + m + 1, comp); n   st[++top] = b[1]; n   w[b[1]] = phi[a[b[1]]]; n   ST[++cnt] = b[1]; n   for (int i = 2; i <= m; i++) { n     x = b[i]; n     y = 0; n     lca = tr::lca(x, st[top]); n     while (dep[lca] < dep[st[top]]) { n       if (y) n         ret = (ret + 1ll * (dep[y] - dep[st[top]]) * w[y] % mod * n                          (sum - w[y] + mod) % mod) % n               mod; n       if (y) w[st[top]] = (w[st[top]] + w[y]) % mod; n       y = st[top--]; n     } n     if (st[top] != lca) st[++top] = lca, ST[++cnt] = lca; n     if (y) n       ret = (ret + 1ll * (dep[y] - dep[st[top]]) * w[y] % mod * n                        (sum - w[y] + mod) % mod) % n             mod; n     if (y) w[st[top]] = (w[st[top]] + w[y]) % mod; n     if (st[top] != x) st[++top] = x, ST[++cnt] = x; n   } n   while (top > 1) { n     ret = (ret + 1ll * (dep[st[top]] - dep[st[top - 1]]) * w[st[top]] % mod * n                      (sum - w[st[top]] + mod) % mod) % n           mod; n     w[st[top - 1]] = (w[st[top]] + w[st[top - 1]]) % mod; n     top--; n   } n   while (cnt) w[ST[cnt]] = 0, cnt--; n   return (ret << 1) % mod; n } n long long qm(long long x, long long k) { n   long long sum = 1; n   while (k) { n     if (k & 1) sum = sum * x % mod; n     x = x * x % mod; n     k >>= 1; n   } n   return sum; n } n void work() { n   int x, y; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]), id[a[i]] = i; n   for (int i = 1; i < n; i++) { n     scanf( %d%d , &x, &y); n     link(x, y); n     link(y, x); n   } n   priwork(); n   for (int i = 1; i <= n; i++) { n     m = 0; n     for (int j = i; j <= n; j += i) b[++m] = id[j]; n     g[i] = solve(); n   } n   for (int i = 1; i <= n; i++) n     for (int j = i; j <= n; j += i) f[i] = (f[i] + mu[j / i] * g[j]) % mod; n   int ans = 0; n   for (int i = 1; i <= n; i++) inv[i] = qm(i, mod - 2); n   for (int i = 1; i <= n; i++) n     ans = (ans + 1ll * i * inv[phi[i]] % mod * f[i] % mod) % mod; n   cout << ans * qm(1ll * n * (n - 1) % mod, mod - 2) % mod << endl; n } n int main() { n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double error = 1e-15; n const double pi = 2 * acos(0); n const int maxn = (int)1e5 + 10; n const int mod = (int)1e9; n int dx[] = {0, -1, 0, +1, -1, -1, +1, +1}; n int dy[] = {-1, 0, +1, 0, -1, +1, +1, -1}; n long A[205][200], B[200]; n vector<int> v; n int main() { n   ios_base::sync_with_stdio(0); n   long i, j, n, m; n   cin >> n; n   for (i = 1; i <= n; i++) n     for (j = 1; j <= n; j++) { n       cin >> A[i][j]; n       if (A[i][j] == 1) { n         B[i] = 1; n       } else if (A[i][j] == 2) { n         B[j] = 1; n       } else if (A[i][j] == 3) { n         B[i] = 1; n         B[j] = 1; n       } n     } n   m = 0; n   for (i = 1; i <= n; i++) n     if (B[i] == 0) { n       m++; n       v.push_back(i); n     } n   cout << m << endl; n   for (i = 0; i < m; i++) cout << v[i] <<    ; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 300010; n map<pair<pair<int, int>, int>, vector<int> > ans; n map<pair<pair<int, int>, int>, vector<pair<int, int> > > l; n map<pair<pair<int, int>, int>, vector<pair<int, int> > >::iterator it; n int n, m; n struct Point { n   int x, y; n   Point(int _x = 0, int _y = 0) : x(_x), y(_y){}; n   Point operator-(const Point &rhs) { return Point(x - rhs.x, y - rhs.y); } n   Point operator*(int v) { return Point(v * x, v * y); } n   Point operator+(const Point &rhs) { return Point(x + rhs.x, y + rhs.y); } n   Point operator/(int v) { return Point(x / v, y / v); } n   int len() { return x * x + y * y; } n   friend int dis(Point a, Point b) { return Point(a - b).len(); } n   void read() { scanf( %d%d , &x, &y); } n }; n struct Circle { n   int x, y, r; n } c[1510]; n inline int Pow(int x) { return x * x; } n inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n pair<pair<int, int>, int> getLine(Point a, Point b) { n   if (a.x == b.x) return make_pair(make_pair(0, 0), a.x); n   if (a.y == b.y) return make_pair(make_pair(1, 0), a.y); n   int dx = a.x - b.x; n   int dy = a.y - b.y; n   int d = gcd(abs(dx), abs(dy)); n   dx /= d; n   dy /= d; n   if (dx < 0) { n     dx *= -1; n     dy *= -1; n   } n   int v = (a.y % dy + dy) % dy; n   v = a.x + (v - a.y) / dy * dx; n   return make_pair(make_pair(dx, dy), v); n } n int getAns(pair<pair<int, int>, int> tmp, int x) { n   if (l.find(tmp) == l.end()) return 0; n   vector<pair<int, int> > &u = l[tmp]; n   int p = upper_bound(u.begin(), u.end(), make_pair(x, 1)) - u.begin(); n   return ans[tmp][p]; n } n pair<pair<int, int>, int> getMirrorLine(Point a, Point b) { n   Point mid = (a + b) / 2, off = a - b; n   swap(off.x, off.y), off.x *= -1; n   return getLine(mid + off, mid - off); n } n int main() { n   scanf( %d%d , &n, &m); n   Point a, b; n   while (n--) { n     a.read(); n     b.read(); n     a = a * 2; n     b = b * 2; n     if (a.x == b.x) { n       l[getLine(a, b)].push_back(make_pair(min(a.y, b.y), 1)); n       l[getLine(a, b)].push_back(make_pair(max(a.y, b.y) + 1, -1)); n     } else { n       l[getLine(a, b)].push_back(make_pair(min(a.x, b.x), 1)); n       l[getLine(a, b)].push_back(make_pair(max(a.x, b.x) + 1, -1)); n     } n   } n   for (it = l.begin(); it != l.end(); ++it) { n     sort((*it).second.begin(), (*it).second.end()); n     vector<int> &tt = ans[(*it).first]; n     vector<pair<int, int> > &d = (*it).second; n     int v = 0; n     tt.push_back(v); n     for (int i = 0; i <= d.size(); ++i) { n       v += d[i].second; n       tt.push_back(v); n     } n   } n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d%d , &c[i].x, &c[i].y, &c[i].r); n     c[i].x *= 2; n     c[i].y *= 2; n     c[i].r *= 2; n   } n   int cnt = 0; n   for (int i = 1; i < m; ++i) n     for (int j = i + 1; j <= m; ++j) { n       if (c[i].r == c[j].r && n           dis(Point(c[i].x, c[i].y), Point(c[j].x, c[j].y)) > n               Pow(c[i].r + c[j].r)) { n         int mid; n         if (c[i].y == c[j].y) n           mid = c[i].y; n         else n           mid = (c[i].x + c[j].x) >> 1; n         cnt += getAns( n             getMirrorLine(Point(c[i].x, c[i].y), Point(c[j].x, c[j].y)), mid); n       } n     } n   printf( %d n , cnt); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n int n, k; n long long f[100009], g[100009]; n vector<int> p; n string s1, s2; n int main() { n   cin >> s1 >> s2 >> k; n   int i; n   n = s1.size(); n   p.clear(); n   for (i = 1; i <= n; ++i) { n     if (s1 == s2) p.push_back(i - 1); n     s1.push_back(s1[0]); n     s1.erase(0, 1); n   } n   f[0] = 1LL; n   g[0] = 0LL; n   for (i = 1; i <= k; ++i) { n     f[i] = (g[i - 1] * (n - 1)) % MOD; n     g[i] = (f[i - 1] + g[i - 1] * (n - 2)) % MOD; n   } n   long long ans = 0LL; n   if (!p.empty()) { n     for (i = 0; i < p.size(); ++i) { n       if (p[i] == 0) n         ans = (ans + f[k]) % MOD; n       else n         ans = (ans + g[k]) % MOD; n     } n   } n   cout << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long A[100005]; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n int main() { n   long long n; n   long long i; n   cin >> n; n   for (i = 0; i < n; i++) cin >> A[i]; n   sort(A, A + n); n   long long g, s, num; n   s = num = 0; n   for (i = 0; i < n; i++) { n     s += A[i] * i - num; n     num += A[i]; n   } n   s = s * 2; n   s += num; n   g = gcd(s, n); n   cout << s / g <<     << n / g << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e3 + 2, M = 1e6 + 2; n int n, m, X[N << 1], Y[N << 1], C[N][N]; n void Merge(vector<pair<int, pair<int, int> > > &v) { n   sort(v.begin(), v.end()); n   int q = 0; n   for (int i = 1; i < (int)(v.size()); i++) { n     if (v[i].first != v[q].first || v[i].second.first > v[q].second.second) n       v[++q] = v[i]; n     else n       v[q].second.second = v[i].second.second; n   } n   v.resize(q + 1); n } n bool Check(int t) { n   vector<pair<int, pair<int, int> > > hor, ver; n   for (int i = 0; i < m; i++) n     for (int j = 0; j < m; j++) C[i][j] = 0; n   for (int i = 0; i < n; i++) n     hor.push_back(make_pair(Y[i], make_pair(X[i] - 2 * t, X[i] + 2 * t))); n   for (int i = n; i < n + m; i++) n     ver.push_back(make_pair(X[i], make_pair(Y[i] - 2 * t, Y[i] + 2 * t))); n   Merge(hor), Merge(ver); n   for (pair<int, pair<int, int> > a : hor) { n     vector<int> adj; n     for (int i = 0; i < (int)(ver.size()); i++) { n       pair<int, pair<int, int> > b = ver[i]; n       if (b.second.first <= a.first && a.first <= b.second.second && n           a.second.first <= b.first && b.first <= a.second.second) { n         for (int j : adj) n           if (C[i][j]++) return true; n         adj.push_back(i); n       } n     } n   } n   return false; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n + m; i++) { n     cin >> X[i] >> Y[i]; n     int x = X[i] - Y[i], y = X[i] + Y[i]; n     X[i] = x, Y[i] = y; n   } n   int dw = 0, up = M; n   while (up - dw > 1) { n     int md = (dw + up) >> 1; n     if (Check(md)) n       up = md; n     else n       dw = md; n   } n   if (up == M) n     cout <<  Poor Sereja!  << endl; n   else n     cout << up << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long MOD2 = 998244353; n double eps = 1e-12; n using namespace std; n template <typename A, typename B> n ostream &operator<<(ostream &os, const pair<A, B> &p) { n   return os <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename T_container, typename T = typename enable_if< n                                     !is_same<T_container, string>::value, n                                     typename T_container::value_type>::type> n ostream &operator<<(ostream &os, const T_container &v) { n   os <<  { ; n   string sep; n   for (const T &x : v) os << sep << x, sep =  ,  ; n   return os <<  } ; n } n void dbg_out() { cerr << endl; } n template <typename Head, typename... Tail> n void dbg_out(Head H, Tail... T) { n   cerr <<     << H; n   dbg_out(T...); n } n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma ) n #pragma GCC optimize( unroll-loops ) n template <typename T> n void get_ar(T *ar, long long n) { n   for (long long i = 0; i < n; i++) cin >> ar[i]; n } n template <typename T> n void printar(T *ar, long long n) { n   for (long long i = 0; i < n; i++) cout << ar[i] <<    ; n   cout <<   n ; n } n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = (res * x) % p; n     y = y >> 1; n     x = (x * x) % p; n   } n   return res; n } n long long imod(long long a, long long m) { return power(a, m - 2, m); } n template <typename T, size_t N, size_t M> n void print2dArray(T (&theArray)[N][M], long long n = N, long long m = M) { n   for (long long x = 0; x < n; x++) { n     for (long long y = 0; y < m; y++) { n       cout << theArray[x][y] <<    ; n     } n     cout <<   n ; n   } n } n template <typename T, size_t N> n void print_ar(T (&theArray)[N], long long n = N) { n   for (long long x = 0; x < n; x++) { n     cout << theArray[x] <<    ; n   } n   cout <<   n ; n } n struct DSU { n   long long connected; n   vector<long long> par, sz, mi, ma; n   void init(long long n) { n     par = sz = mi = ma = vector<long long>(n + 1, 0); n     for (long long i = 1; i <= n; i++) n       par[i] = i, sz[i] = 1, mi[i] = i, ma[i] = i; n     connected = n; n   } n   long long getPar(long long u) { n     while (u != par[u]) { n       par[u] = par[par[u]]; n       u = par[u]; n     } n     return u; n   } n   long long getSize(long long u) { return sz[getPar(u)]; } n   void merge(long long u, long long v) { n     long long par1 = getPar(u), par2 = getPar(v); n     if (par1 == par2) return; n     connected--; n     if (sz[par1] > sz[par2]) swap(par1, par2); n     long long aa = min(mi[par1], mi[par2]); n     long long bb = max(ma[par1], ma[par2]); n     sz[par2] += sz[par1]; n     sz[par1] = 0; n     par[par1] = par[par2]; n     mi[par2] = aa; n     ma[par2] = bb; n   } n }; n DSU dd; n vector<long long> pres, pref; n long long give(long long p1) { n   long long l = dd.mi[p1], r = dd.ma[p1]; n   long long tot = pres[r] - pres[l - 1]; n   long long ans = pref[r] - pref[r - tot]; n   return ans; n } n void solve() { n   long long n, m, q, K, ans = 0, tmp; n   cin >> n >> m >> q; n   vector<long long> a(n), b(m); n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     ans += a[i]; n   } n   for (long long i = 0; i < m; i++) { n     cin >> b[i]; n   } n   vector<vector<long long> > temp; n   temp.push_back({0, 0}); n   for (long long i = 0; i < n; i++) { n     temp.push_back({a[i], 1}); n   } n   for (long long i = 0; i < m; i++) { n     temp.push_back({b[i], 0}); n   } n   dd.init(n + m + 1); n   sort((temp).begin(), (temp).end()); n   pres.resize(n + m + 1, 0); n   pref.resize(n + m + 1, 0); n   for (long long i = 1; i < ((long long)(temp).size()); i++) { n     pres[i] = pres[i - 1] + temp[i][1]; n     pref[i] = pref[i - 1] + temp[i][0]; n   } n   vector<vector<long long> > diff; n   for (long long i = 1; i < ((long long)(temp).size()) - 1; i++) { n     diff.push_back({temp[i + 1][0] - temp[i][0], i}); n   } n   sort((diff).begin(), (diff).end()); n   long long cur = 0; n   vector<vector<long long> > quer; n   for (long long i = 0; i < q; i++) { n     long long x; n     cin >> x; n     quer.push_back({x, i}); n   } n   sort((quer).begin(), (quer).end()); n   vector<long long> fin(q, 0); n   for (auto x : quer) { n     long long k = x[0], ind = x[1]; n     while (cur < ((long long)(diff).size()) && diff[cur][0] <= k) { n       long long p1 = dd.getPar(diff[cur][1]), p2 = dd.getPar(diff[cur][1] + 1); n       ans -= give(p1); n       ans -= give(p2); n       dd.merge(p1, p2); n       long long pa = dd.getPar(p1); n       ans += give(pa); n       cur++; n     } n     fin[ind] = ans; n   } n   for (long long i = 0; i < q; i++) { n     cout << fin[i] <<   n ; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t = 1; n   for (long long it = 1; it <= t; it++) { n     solve(); n   } n   return 0; n } n


#include <iostream> n  n using namespace std; n  n  n int main() n { n     int n,q, l,r; n     cin>>n>>q; n     string s; n     cin>>s; n     int arr[n]; n     arr[0]=s[0]- a +1; n     for(int i=1;i<n;i++){ n         arr[i]=arr[i-1]+s[i]- a +1; n     } n     while(q--){ n         cin>>l>>r; n         cout<<(arr[r-1]-((l==1)?0:arr[l-2]))<<  n ; n     } n     return 0; n } n