#include <bits/stdc++.h> n using namespace std; n const int N = 1e5; n int vl[N + 2], tim, st[N + 2]; n int par[N + 2], level[N + 2]; n int sp[N + 2][22], n, ed[N + 2]; n long long seg[5 * N], lazy[5 * N]; n vector<int> adj[N + 2]; n void too_lazy(int node, int lo, int hi) { n   seg[node] += (lazy[node] * 1LL * (hi - lo + 1)); n   if (lo != hi) { n     lazy[node * 2] += lazy[node]; n     lazy[node * 2 + 1] += lazy[node]; n   } n   lazy[node] = 0; n } n void upd(int node, int lo, int hi, int lt, int rt, int up) { n   too_lazy(node, lo, hi); n   if (lo > rt || hi < lt) return; n   if (lo >= lt && hi <= rt) { n     lazy[node] += up; n     too_lazy(node, lo, hi); n     return; n   } n   int md = (lo + hi) / 2; n   upd(node * 2, lo, md, lt, rt, up); n   upd(node * 2 + 1, md + 1, hi, lt, rt, up); n   seg[node] = seg[node * 2] + seg[node * 2 + 1]; n } n long long query(int node, int lo, int hi, int lt, int rt) { n   too_lazy(node, lo, hi); n   if (lo > rt || hi < lt) return 0; n   if (lo >= lt && hi <= rt) return seg[node]; n   int md = (lo + hi) / 2; n   long long p1 = query(node * 2, lo, md, lt, rt); n   long long p2 = query(node * 2 + 1, md + 1, hi, lt, rt); n   return p1 + p2; n } n void dfs(int node, int fro, int h) { n   ++tim; n   st[node] = tim; n   upd(1, 1, n, st[node], st[node], vl[node]); n   par[node] = fro; n   level[node] = h; n   for (auto xx : adj[node]) { n     if (xx == fro) continue; n     dfs(xx, node, h + 1); n   } n   ed[node] = tim; n } n void sparse(void) { n   memset(sp, -1, sizeof(sp)); n   for (int i = 1; i <= n; i++) sp[i][0] = par[i]; n   for (int j = 1; j <= 20; j++) { n     for (int i = 1; i <= n; i++) { n       if (sp[i][j - 1] == -1) continue; n       sp[i][j] = sp[sp[i][j - 1]][j - 1]; n     } n   } n } n int lca(int u, int v) { n   if (level[u] < level[v]) swap(u, v); n   for (int i = 20; i >= 0; i--) { n     if (sp[u][i] == -1) continue; n     if (level[sp[u][i]] >= level[v]) u = sp[u][i]; n   } n   if (u == v) return u; n   for (int i = 20; i >= 0; i--) { n     if (sp[u][i] == -1 || sp[v][i] == -1) continue; n     if (sp[u][i] == sp[v][i]) continue; n     u = sp[u][i]; n     v = sp[v][i]; n   } n   return par[u]; n } n bool is_in_path(int u, int v, int w) { n   if (w == -1) return false; n   if (level[w] < level[lca(u, v)]) return false; n   if (lca(u, w) == w || lca(v, w) == w) n     return true; n   else n     return false; n } n void update(int u, int v, int rt, int x) { n   if (is_in_path(u, v, rt)) { n     upd(1, 1, n, st[1], ed[1], x); n     return; n   } n   int lc = lca(u, v); n   for (int i = 20; i >= 0; i--) { n     if (sp[rt][i] == -1) continue; n     if (level[sp[rt][i]] < level[lc]) continue; n     if (!is_in_path(u, v, sp[rt][i])) rt = sp[rt][i]; n   } n   if (is_in_path(u, v, par[rt])) { n     upd(1, 1, n, st[1], ed[1], x); n     upd(1, 1, n, st[rt], ed[rt], -x); n   } else n     upd(1, 1, n, st[lc], ed[lc], x); n } n void query(int v, int rt) { n   if (v == rt) { n     cout << query(1, 1, n, 1, n) << endl; n     return; n   } n   for (int i = 20; i >= 0; i--) { n     if (sp[rt][i] == -1) continue; n     if (level[sp[rt][i]] > level[v]) rt = sp[rt][i]; n   } n   if (par[rt] == v) n     cout << query(1, 1, n, 1, n) - query(1, 1, n, st[rt], ed[rt]) << endl; n   else n     cout << query(1, 1, n, st[v], ed[v]) << endl; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int q; n   cin >> n >> q; n   for (int i = 1; i <= n; i++) cin >> vl[i]; n   for (int i = 1; i < n; i++) { n     int u, v; n     cin >> u >> v; n     adj[u].push_back(v); n     adj[v].push_back(u); n   } n   dfs(1, -1, 1); n   sparse(); n   int rt = 1; n   while (q--) { n     int ty; n     cin >> ty; n     if (ty == 1) { n       int v; n       cin >> v; n       rt = v; n     } else if (ty == 2) { n       int u, v, x; n       cin >> u >> v >> x; n       update(u, v, rt, x); n     } else { n       int v; n       cin >> v; n       query(v, rt); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string k; n long long n; n long long Judge(int L, int R) { n   int flag = 0; n   for (int i = L; i <= R; i++) { n     if (k[i] !=  0 ) flag = 1; n   } n   if (flag == 1 && k[L] ==  0 ) return -1; n   long long ans = 0; n   for (int i = L; i <= R; i++) { n     ans = (ans * 10 + (k[i] -  0 )); n   } n   if (ans >= n) { n     return -1; n   } else n     return ans; n } n int getL(int l, int r) { n   for (int i = l; i <= r; i++) { n     if (k[i] !=  0 ) return i; n   } n   return r; n } n int main() { n   cin >> n >> k; n   long long ans = 0; n   int len = k.size(); n   long long base = 1; n   int l = len - 1; n   int r = len - 1; n   long long val; n   while (r >= 0) { n     for (int i = 0; i <= r; i++) { n       if (r - i > 17) continue; n       val = 0; n       for (int j = i; j <= r; j++) val = val * 10 + k[j] -  0 ; n       if (val < n) { n         l = i; n         break; n       } n     } n     while (l < r && k[l] ==  0 ) l++; n     ans = ans + base * val; n     base *= n; n     r = l - 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 5000 + 5; n const int lim = 1000 * 1000 * 1000; n const int inf = 1000 * 1000 * 1000 + 5; n int a[maxN]; n vector<int> v[maxN]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   v[0].push_back(a[0]); n   for (int i = 1; i < n; i++) { n     int fl = 1; n     for (int j = 0; j < v[i - 1].size(); j++) { n       if (v[i - 1][j] < a[i]) n         v[i].push_back(v[i - 1][j]); n       else { n         if (fl) v[i].push_back(a[i]), fl = 0; n         v[i].push_back(v[i - 1][j]); n       } n     } n     if (fl) v[i].push_back(a[i]); n   } n   int ans = 0; n   for (int i = 0; i < n; i++) n     ans += v[i].size() - 1 - n            (lower_bound(v[i].begin(), v[i].end(), a[i]) - v[i].begin()); n   int cnt = 0; n   int answer = inf; n   for (int i = 0; i < n; i++) n     for (int j = i + 1; j < n; j++) { n       if (a[i] < a[j]) continue; n       int tmp = ans; n       tmp -= v[i].size() - 1 - n              2 * (lower_bound(v[i].begin(), v[i].end(), a[i]) - v[i].begin()); n       tmp -= v[j].size() - 1 - n              2 * (lower_bound(v[j].begin(), v[j].end(), a[j]) - v[j].begin()); n       tmp += v[i].size() - n              2 * (lower_bound(v[i].begin(), v[i].end(), a[j]) - v[i].begin()); n       tmp += v[j].size() - n              2 * (lower_bound(v[j].begin(), v[j].end(), a[i]) - v[j].begin()); n       if (a[i] > a[j]) n         tmp--; n       else n         tmp++; n       if (tmp < answer) n         answer = tmp, cnt = 1; n       else if (tmp == answer) n         cnt++; n     } n   cout << answer <<     << cnt << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 1e9 + 7; n const int MAXN = 3e5 + 10; n using namespace std; n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   for (int i = a; i <= 2 * a; i++) { n     for (int j = b; j <= 2 * b; j++) { n       for (int k = c; k <= 2 * c; k++) { n         if (i > j && j > k && d <= i && d <= j && d <= k) { n           if (k <= d + d && j > d + d && i > d + d) { n             cout << i <<   n  << j <<   n  << k; n             return 0; n           } n         } n       } n     } n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const double EPS = 1e-9; n int T, n; n int num[1000015], p[1000015], cnt, sum[1000015]; n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &n); n     for (int i = 1; i <= n; ++i) { n       scanf( %d , &p[i]); n     } n     num[cnt = 1] = 1; n     for (int i = 2; i <= n; ++i) { n       if (p[i] == p[i - 1]) { n         ++num[cnt]; n       } else { n         num[++cnt] = 1; n       } n     } n     for (int i = 1; i <= cnt; ++i) { n       sum[i] = sum[i - 1] + num[i]; n     } n     sum[cnt + 1] = INF; n     int g = 0, s = 0, b = 0; n     for (int i = 1, j = 2, k = 3; k <= n; ++i) { n       while (j <= n && sum[j] - sum[i] <= sum[i]) ++j; n       while (k < n && sum[k + 1] <= sum[cnt] / 2) ++k; n       if (j <= n && k <= n && sum[i] < sum[k] - sum[j] && n           sum[k] <= sum[cnt] / 2) { n         g = sum[i]; n         s = sum[j] - sum[i]; n         b = sum[k] - sum[j]; n       } else { n         break; n       } n     } n     printf( %d %d %d n , g, s, b); n   } n   return 0; n } n


#include <algorithm> n #include <iostream> n #include <map> n #include <vector> n using namespace std; n  n using LL = long long; n #define mk make_pair n  n const int MAX_N = 3E5 + 100; n const int MAX_LOG_N = 18 + 1; n  n int n, m; n vector<int> G[MAX_N]; n  n //================================= n  n int log2Static[MAX_N]; n void log2Init(int n) { n     int w = 1, j = 0; n     while (w <= n) { n         for (int i = w; i < 2 * w && i <= n; ++i) log2Static[i] = j; n         w *= 2; n         j += 1; n     } n } n  n int dep[MAX_N]; n int anster[MAX_N][MAX_LOG_N]; n  n void getAnster(int u, int fat) { n     dep[u] = dep[fat] + 1; n     anster[u][0] = fat; n     for (int i = 1; i <= log2Static[dep[u]]; ++i) { n         anster[u][i] = anster[anster[u][i - 1]][i - 1]; n     } n  n     for (int v : G[u]) { n         if (fat == v) continue; n         getAnster(v, u); n     } n } n  n int getLCA(int u, int v) { n     if (dep[u] < dep[v]) swap(u, v); n     while (dep[u] > dep[v]) { n         u = anster[u][log2Static[dep[u] - dep[v]]]; n     } n     if (u == v) return u; n  n     for (int i = log2Static[dep[u]]; i >= 0;) { n         if (anster[u][i] == anster[v][i]) { n             i -= 1; n         } else { n             u = anster[u][i]; n             v = anster[v][i]; n         } n     } n     return anster[u][0]; n } n  n //================================= n  n class Path { n    public: n     int c, ui, vi; n }; n  n int curSon[MAX_N]; n LL pathCnt[MAX_N]; n Path path[MAX_N]; n  n vector<int> lcaPath[MAX_N]; n vector<int> sidePath[MAX_N]; n  n LL ans; n void dfs(int u, int fat) { n     for (int i : sidePath[u]) { n         if (!path[i].ui) n             path[i].ui = curSon[path[i].c]; n         else n             path[i].vi = curSon[path[i].c]; n     } n  n     for (int v : G[u]) { n         if (fat == v) continue; n         curSon[u] = v; n         dfs(v, u); n         pathCnt[u] += pathCnt[v]; n     } n  n     pathCnt[u] -= lcaPath[u].size(); n  n     LL tmp1 = 0; n     map<pair<int, int>, LL> tmp2; n     for (int i : lcaPath[u]) { n         int tmp = 0; n         tmp += pathCnt[path[i].ui] ? pathCnt[path[i].ui] - 1 : 0; n         tmp += pathCnt[path[i].vi] ? pathCnt[path[i].vi] - 1 : 0; n         tmp1 += pathCnt[u] - tmp - 1; n  n         //处理双重边情况 n         if (path[i].ui && path[i].vi) { n             tmp2[mk(path[i].ui, path[i].vi)] += 1; n         } n     } n  n     for (auto it : tmp2) { n         tmp1 += it.second * (it.second - 1); n     } n  n     //特殊处理 n     for (int i : lcaPath[u]) { n         if (path[i].ui) pathCnt[path[i].ui] -= 1; n         if (path[i].vi) pathCnt[path[i].vi] -= 1; n     } n     pathCnt[u] -= lcaPath[u].size(); n     //仅统计向上路径的 n     for (int i : lcaPath[u]) { n         tmp1 += pathCnt[u] - pathCnt[path[i].ui] - pathCnt[path[i].vi]; n     } n  n     ans += tmp1 / 2; n } n  n int main() { n     ios::sync_with_stdio(false); n     cin >> n; n     for (int i = 1; i < n; ++i) { n         int u, v; n         cin >> u >> v; n         G[u].push_back(v); n         G[v].push_back(u); n     } n     log2Init(n); n     getAnster(1, 0); n     // init n  n     cin >> m; n     for (int i = 0; i < m; ++i) { n         int u, v; n         cin >> u >> v; n  n         pathCnt[u] += 1; n         pathCnt[v] += 1; n  n         path[i].c = getLCA(u, v); n         if (path[i].c == u) u = 0; n         if (path[i].c == v) v = 0; n         if (u > v) swap(u, v); n  n         lcaPath[path[i].c].push_back(i); n         if (u) sidePath[u].push_back(i); n         if (v) sidePath[v].push_back(i); n     } n  n     dfs(1, 0); n  n     cout << ans << endl; n  n     return 0; n }


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n long long int a[100010]; n int n; n int main() { n   int i, j; n   while (scanf( %d , &n) != EOF) { n     for (i = 1; i <= n; i++) { n       scanf( %lld , &a[i]); n     } n     sort(a + 1, a + 1 + n); n     long long sum = a[1]; n     int ans = 1; n     for (i = 2; i <= n; i++) { n       if (a[i] >= sum) { n         ans++; n         sum += a[i]; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 105; n const double EPS = 1e-9; n int n, k; n double e; n int px0, py0; n int x[MAXN], y[MAXN]; n double dp[MAXN][2]; n double dist(int x1, int y1, int x2, int y2) { n   return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)); n } n double prob(int ind, double rad) { n   double _dist = dist(x[ind], y[ind], px0, py0); n   if (_dist <= rad) return 1; n   return exp(1 - pow(_dist, 2) / pow(rad, 2)); n } n bool check(double rad) { n   for (int i = 0; i < MAXN; i++) dp[i][0] = dp[i][1] = 0; n   dp[0][0] = 1; n   for (int i = 0; i < n; i++) { n     double p = prob(i, rad); n     for (int j = 0; j < MAXN - 1; j++) { n       dp[j + 1][1] += dp[j][0] * p; n       dp[j][1] += dp[j][0] * (1 - p); n     } n     for (int j = 0; j < MAXN; j++) dp[j][0] = dp[j][1], dp[j][1] = 0; n   } n   double sum = 0; n   for (int i = 0; i < k; i++) sum += dp[i][0]; n   return sum < e; n } n int main() { n   ios ::sync_with_stdio(0); n   if (fopen( input.txt ,  r )) freopen( input.txt ,  r , stdin); n   cin >> n >> k; n   int _e; n   cin >> _e; n   e = 1. * _e / 1000; n   cin >> px0 >> py0; n   for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; n   double lo = 0, hi = 5000; n   while (abs(hi - lo) > 2 * EPS) { n     double mid = (lo + hi) / 2; n     if (check(mid)) n       hi = mid; n     else n       lo = mid + EPS; n   } n   cout << fixed << setprecision(10) << lo << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int dp[100010]; n long long int sum[100010]; n int main() { n   int n; n   int k; n   int m; n   cin >> m >> k; n   dp[0] = 1; n   for (int i = 1; i < k; i++) { n     dp[i] = 1; n   } n   for (int i = k; i <= 100002; i++) { n     dp[i] = (dp[i - 1] + dp[i - k]) % 1000000007; n   } n   sum[0] = 0; n   for (int i = 1; i <= 100002; i++) { n     sum[i] = (sum[i - 1] + dp[i]) % 1000000007; n   } n   int a, b; n   while (m--) { n     cin >> a >> b; n     long long int val = ((sum[b] - sum[a - 1]) + 1000000007) % 1000000007; n     cout << val << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T1, typename T2> n inline T1 max(T1 a, T2 b) { n   return a < b ? b : a; n } n template <typename T1, typename T2> n inline T1 min(T1 a, T2 b) { n   return a < b ? a : b; n } n const char lf =   n ; n namespace ae86 { n const int bufl = 1 << 15; n char buf[bufl], *s = buf, *t = buf; n inline int fetch() { n   if (s == t) { n     t = (s = buf) + fread(buf, 1, bufl, stdin); n     if (s == t) return EOF; n   } n   return *s++; n } n inline int ty() { n   int a = 0; n   int b = 1, c = fetch(); n   while (!isdigit(c)) b ^= c ==  - , c = fetch(); n   while (isdigit(c)) a = a * 10 + c - 48, c = fetch(); n   return b ? a : -a; n } n inline double td() { n   double a = 0, d = 0.1; n   int b = 1, c = fetch(); n   while (!isdigit(c)) b ^= c ==  - , c = fetch(); n   while (isdigit(c)) a = a * 10 + c - 48, c = fetch(); n   if (c !=  . ) return b ? a : -a; n   while (c = fetch(), isdigit(c)) a += d * (c - 48), d *= 0.1; n   return b ? a : -a; n } n }  // namespace ae86 n using ae86::td; n using ae86::ty; n const int _ = 23, __ = 1 << 20; n int n, nn, lim, bcnt[__] = {0}; n double ps[_], sps[__] = {0}, f[__] = {0}, ans[_] = {0}; n int main() { n   ios::sync_with_stdio(0), cout.tie(nullptr); n   n = ty(), nn = 1 << n, lim = ty(); n   int cnts = 0; n   for (int i = 0; i < n; i++) ps[i] = td(), cnts += ps[i] > 0; n   if (cnts < lim) { n     for (int i = 0; i < n; i++) cout <<  01 [ps[i] > 0] <<    n [i == n - 1]; n     return 0; n   } n   for (int i = 0; i < nn; i++) n     for (int j = 0; j < n; j++) n       if ((((i) >> (j)) & 1)) sps[i] += ps[j]; n   bcnt[0] = 0; n   for (int i = 1; i < nn; i++) bcnt[i] = bcnt[i >> 1] + (i & 1); n   f[0] = 1; n   for (int i = 0; i < nn; i++) { n     if (bcnt[i] == lim) n       for (int j = 0; j < n; j++) n         if ((((i) >> (j)) & 1)) ans[j] += f[i]; n     if (bcnt[i] >= lim) continue; n     for (int j = 0; j < n; j++) n       if (!(((i) >> (j)) & 1)) f[i | (1 << (j))] += f[i] * ps[j] / (1 - sps[i]); n   } n   for (int i = 0; i < n; i++) n     cout << setprecision(11) << fixed << ans[i] <<    n [i == n - 1]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n template <typename T> n vector<T>& operator--(vector<T>& v) { n   for (auto& i : v) --i; n   return v; n } n template <typename T> n vector<T>& operator++(vector<T>& v) { n   for (auto& i : v) ++i; n   return v; n } n template <typename T> n istream& operator>>(istream& is, vector<T>& v) { n   for (auto& i : v) is >> i; n   return is; n } n template <typename T> n ostream& operator<<(ostream& os, vector<T> v) { n   for (auto& i : v) os << i <<    ; n   return os; n } n template <typename T, typename U> n pair<T, U>& operator--(pair<T, U>& p) { n   --p.first; n   --p.second; n   return p; n } n template <typename T, typename U> n pair<T, U>& operator++(pair<T, U>& p) { n   ++p.first; n   ++p.second; n   return p; n } n template <typename T, typename U> n istream& operator>>(istream& is, pair<T, U>& p) { n   is >> p.first >> p.second; n   return is; n } n template <typename T, typename U> n ostream& operator<<(ostream& os, pair<T, U> p) { n   os << p.first <<     << p.second; n   return os; n } n template <typename T, typename U> n pair<T, U> operator-(pair<T, U> a, pair<T, U> b) { n   return make_pair(a.first - b.first, a.second - b.second); n } n template <typename T, typename U> n pair<T, U> operator+(pair<T, U> a, pair<T, U> b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n template <typename T, typename U> n void umin(T& a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n void umax(T& a, U b) { n   if (a < b) a = b; n } n struct Node { n   bool end = false; n   map<char, Node*> to; n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   vector<string> res(n); n   cin >> res; n   set<string> ress(res.begin(), res.end()); n   Node* root = new Node(); n   for (string s : res) { n     auto node = root; n     for (char c : s) { n       if (!node->to.count(c)) node->to[c] = new Node(); n       node = node->to[c]; n     } n     node->end = true; n   } n   int m; n   cin >> m; n   vector<string> tokens0; n   { n     string s; n     getline(cin, s); n     for (int it = 0; it < m; ++it) { n       getline(cin, s); n       for (int i = 0; i < s.size(); ++i) { n         if (s[i] ==  # ) { n           s.resize(i); n           break; n         } n       } n       int last = 0; n       for (int i = 0; i <= s.size(); ++i) { n         if (i == s.size() || s[i] ==    ) { n           if (last != i) { n             tokens0.push_back(s.substr(last, i - last)); n           } n           last = i + 1; n         } n       } n     } n   } n   void(0); n   vector<pair<string, int>> tokens; n   auto is_let = [&](char c) { n     if ( a  <= c && c <=  z ) return true; n     if ( A  <= c && c <=  Z ) return true; n     if ( 0  <= c && c <=  9 ) return true; n     if (c ==  $ ) return true; n     if (c ==  _ ) return true; n     return false; n   }; n   auto parse_tokens = [&](string s) { n     vector<pair<string, int>> tokens; n     int ind = 0; n     while (ind < s.size()) { n       int was = ind; n       auto node = root; n       int best_sz = 0; n       for (int i = ind; i < s.size(); ++i) { n         char c = s[i]; n         if (!node->to.count(c)) break; n         node = node->to[c]; n         if (node->end) best_sz = i + 1 - ind; n       } n       string best_res = s.substr(ind, best_sz); n       void(0); n       if ( 0  <= s[ind] && s[ind] <=  9 ) { n         int it = ind; n         while (it < s.size() && s[it] >=  0  && s[it] <=  9 ) ++it; n         void(0); n         if (it - ind > best_res.size()) { n           tokens.emplace_back(s.substr(ind, it - ind), 1); n           ind = it; n         } else { n           tokens.emplace_back(best_res, 0); n           ind += best_res.size(); n         } n       } else { n         int it = ind; n         while (it < s.size() && is_let(s[it])) ++it; n         void(0); n         if (it - ind > best_res.size()) { n           tokens.emplace_back(s.substr(ind, it - ind), 2); n           ind = it; n         } else { n           tokens.emplace_back(best_res, 0); n           ind += best_res.size(); n         } n       } n       if (ind <= was) { n         return make_pair(tokens, false); n       } n     } n     return make_pair(tokens, true); n   }; n   for (auto s : tokens0) { n     auto v = parse_tokens(s).first; n     for (auto p : v) tokens.push_back(p); n   } n   set<pair<int, string>> avail_names; n   for (int ln = 1; ln <= 3; ++ln) { n     int cnt = 1; n     for (int i = 0; i < ln; ++i) { n       cnt *= 26; n     } n     for (int i = 0; i < cnt; ++i) { n       string t; n       int x = i; n       for (int j = 0; j < ln; ++j) { n         t.push_back( a  + x % 26); n         x /= 26; n       } n       if (ress.count(t)) continue; n       avail_names.emplace(ln, t); n     } n   } n   map<string, string> names; n   for (auto& [s, tp] : tokens) { n     if (tp != 2) continue; n     if (!names.count(s)) { n       names[s] = avail_names.begin()->second; n       avail_names.erase(avail_names.begin()); n     } n     s = names[s]; n   } n   void(0); n   string curs; n   for (int i = 0; i < tokens.size(); ++i) { n     cout << tokens[i].first; n     curs += tokens[i].first; n     if (i + 1 < tokens.size()) { n       auto v1 = parse_tokens(curs).first; n       auto v2 = parse_tokens(tokens[i + 1].first).first; n       for (auto v : v2) v1.push_back(v); n       auto [v3, res] = parse_tokens(curs + tokens[i + 1].first); n       if (!res || v1 != v3) { n         cout <<    ; n         curs.clear(); n       } n     } n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n const long long maxn = 3000 + 10, N = 2e5 + 10, SQ = 320, base = 800287, n                 mod = 1e9 + 7, INF = 1e18 + 10, lg = 22; n const long double eps = 1e-4; n long long n, m, q, dis[maxn][maxn], res[N]; n long long V[N], U[N], r[N], s[N], t[N]; n vector<long long> query[N]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> q; n   for (long long i = 0; i < m; ++i) { n     cin >> V[i] >> U[i]; n     V[i]--, U[i]--; n   } n   for (long long i = 0; i < n; ++i) { n     for (long long j = 0; j < n; ++j) { n       dis[i][j] = (i != j ? INF : m); n     } n   } n   for (long long i = 0; i < q; ++i) { n     long long l; n     cin >> l >> r[i] >> s[i] >> t[i]; n     l--, r[i]--, s[i]--, t[i]--; n     query[l].push_back(i); n   } n   for (long long idx = m - 1; idx >= 0; --idx) { n     dis[V[idx]][U[idx]] = dis[U[idx]][V[idx]] = idx; n     for (long long i = 0; i < n; ++i) { n       long long val = min(dis[V[idx]][i], dis[U[idx]][i]); n       dis[V[idx]][i] = dis[U[idx]][i] = val; n     } n     for (auto i : query[idx]) { n       if (dis[s[i]][t[i]] > r[i]) { n         res[i] = 0; n       } else { n         res[i] = 1; n       } n     } n   } n   for (long long i = 0; i < q; ++i) { n     cout << (res[i] ?  Yes n  :  No n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAX_N = 1e3 + 10, INF = 1e18; n long long n, m, k, a[MAX_N][MAX_N], noe[MAX_N]; n long long lab[MAX_N], dp[MAX_N][MAX_N]; n pair<long long, long long> tar[MAX_N]; n void filldp() { n   dp[m + n - 1][0] = 1; n   for (int i = m + n - 2; i >= 1; i--) { n     for (int j = 0; j <= i; j++) { n       if (noe[i + 1] == 0) { n         dp[i][j] = dp[i + 1][j - 1] + dp[i + 1][j + 1]; n       } else if (noe[i + 1] == 1) { n         dp[i][j] = dp[i + 1][j + 1]; n       } else { n         dp[i][j] = dp[i + 1][j - 1]; n       } n       dp[i][j] = min(dp[i][j], INF); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   memset(lab, 63, sizeof lab); n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n       lab[i + j - 1] = min(lab[i + j - 1], a[i][j]); n     } n   } n   for (int i = 1; i <= n + m - 1; i++) { n     tar[i] = {lab[i], i}; n   } n   sort(tar + 1, tar + n + m); n   for (int i = 1; i <= n + m - 1; i++) { n     noe[tar[i].second] = 1; n     filldp(); n     if (dp[1][1] >= k) { n     } else { n       noe[tar[i].second] = 2; n       k -= dp[1][1]; n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (noe[i + j - 1] == 1) { n         cout <<  ( ; n       } else { n         cout <<  ) ; n       } n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n long long srf(int a, int b, int c) { n   std::cout <<  1   << (a + 1) <<     << (b + 1) <<     << (c + 1) << std::endl; n   long long val; n   std::cin >> val; n   return val; n } n bool scl(int a, int b, int c) { n   std::cout <<  2   << (a + 1) <<     << (b + 1) <<     << (c + 1) << std::endl; n   int val; n   std::cin >> val; n   return val > 0; n } n void accept(std::vector<int> ans) { n   std::cout <<  0  ; n   for (int i : ans) { n     std::cout << (i + 1) <<    ; n   } n   std::cout << std::endl; n   return; n } n signed main() { n   std::ios_base::sync_with_stdio(false); n   std::cin.tie(0); n   std::cout.tie(0); n   int n; n   std::cin >> n; n   std::vector<int> ans(n); n   std::iota(ans.begin(), ans.end(), 0); n   int pos = n       std::min_element(ans.begin() + 1, ans.end(), n                        [&ans](int f, int s) { return scl(ans[0], f, s); }) - n       ans.begin(); n   std::swap(ans[1], ans[pos]); n   std::map<int, long long> map; n   for (int i = 2; i < n; i++) { n     map[ans[i]] = srf(ans[0], ans[1], ans[i]); n   } n   pos = std::max_element(ans.begin() + 1, ans.end(), n                          [&map](int f, int s) { return map[f] < map[s]; }) - n         ans.begin(); n   std::swap(ans.back(), ans[pos]); n   std::vector<int> left, right; n   for (int i = 2; i < (ans.size() - 1); i++) { n     if (scl(ans[0], ans.back(), ans[i])) { n       left.push_back(ans[i]); n     } else { n       right.push_back(ans[i]); n     } n   } n   std::vector<int> ans2 = {ans[0], ans[1]}; n   std::sort(right.begin(), right.end(), n             [&map](int f, int s) { return map[f] < map[s]; }); n   for (int i : right) { n     ans2.push_back(i); n   } n   ans2.push_back(ans.back()); n   std::sort(left.begin(), left.end(), n             [&map](int f, int s) { return map[f] < map[s]; }); n   std::reverse(left.begin(), left.end()); n   for (int i : left) { n     ans2.push_back(i); n   } n   accept(ans2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> arr; n vector<long long int> b; n long long int l, f; n long long int mid; n long long int bin_s(long long int x) { n   l = 0; n   f = b.size() - 1; n   while (l <= f) { n     mid = (l + f) / 2; n     if (b[mid] > x) f = mid - 1; n     if (b[mid] < x) l = mid + 1; n     if (b[mid] == x) { n       return 1; n       break; n     } n   } n   return 0; n } n int main() { n   long long int n, i, j, k, q; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> q; n     arr.push_back(q); n   } n   long long int min = arr[0]; n   for (i = 0; i < n; i++) { n     if (arr[i] <= min) min = arr[i]; n   } n   for (i = 0; i < n; i++) { n     if (arr[i] == min) b.push_back(i); n   } n   long long int c; n   long long int max = 0; n   long long int ways; n   for (i = 0; i < n; i++) { n     c = bin_s(i); n     if (c == 1) { n       ways = min * n; n     } n     if (c == 0) { n       if (b[b.size() - 1] > i) ways = (min * n) + b[l] - i; n       if (b[b.size() - 1] < i) ways = (min * n) + n - i + b[0]; n     } n     if (ways > max) max = ways; n   } n   cout << max << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std; n long long powm(long long base, long long exp, long long mod = 1000000007) { n   long long ans = 1; n   while (exp) { n     if (exp & 1) ans = (ans * base) % mod; n     exp >>= 1, base = (base * base) % mod; n   } n   return ans; n } n long long n, u, v, a[200005], st[2][200005], en[2][200005], timer[2], n     tour[2][200005], level[200005], parent[200005], ft[2][200005]; n vector<long long> adj[200005]; n void dfs(long long mode, long long curr, long long par, long long now) { n   parent[curr] = par, level[curr] = now; n   if (now == mode) n     st[mode][curr] = (++timer[mode]), tour[mode][timer[mode]] = curr; n   for (auto i : adj[curr]) n     if (i != par) dfs(mode, i, curr, now ^ 1); n   if (now == mode) en[mode][curr] = timer[mode]; n } n void update(long long mode, long long p, long long val) { n   long long curr = p; n   for (; curr <= 200005; curr += (curr & (-curr))) ft[mode][curr] += val; n } n void update(long long mode, long long a, long long b, long long val) { n   update(mode, a, val), update(mode, b + 1, -val); n } n long long query(long long mode, long long idx) { n   long long sum = 0, curr = idx; n   for (; curr > 0; curr -= (curr & (-curr))) sum += ft[mode][curr]; n   return sum; n } n int main() { n   long long type, x, val, m; n   cin >> n >> m; n   for (long long i = 1; i < n + 1; i++) cin >> a[i]; n   for (long long i = 0; i < n - 1; i++) { n     cin >> u >> v; n     adj[u].push_back(v), adj[v].push_back(u); n   } n   for (long long i = 0; i < 2; i++) dfs(i, 1, 0, 0); n   while (m--) { n     cin >> type; n     if (type == 1) { n       cin >> x >> val; n       long long mode = level[x], tmp = adj[x].size(); n       long long p = st[mode][x], q = en[mode][x], first = -1, last = -1; n       update(mode, p, q, val); n       for (auto i : adj[x]) n         if (i != parent[x]) { n           first = i; n           break; n         } n       for (long long i = tmp - 1; i >= 0; i--) n         if (adj[x][i] != parent[x]) { n           last = adj[x][i]; n           break; n         } n       if (first != -1) { n         mode ^= 1; n         p = st[mode][first], q = en[mode][last]; n         update(mode, p, q, -val); n       } n     } else { n       cin >> x; n       cout << query(level[x], st[level[x]][x]) + a[x] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int h = -1, e = -1, l1 = -1, l2 = -1, o = -1; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  h ) { n       h = i; n       break; n     } n   } n   if (h == -1) n     cout <<  NO ; n   else { n     for (int j = h + 1; j < s.size(); j++) { n       if (s[j] ==  e ) { n         e = j; n         break; n       } n     } n     if (e == -1) n       cout <<  NO ; n     else { n       for (int k = e + 1; k < s.size(); k++) { n         if (s[k] ==  l ) { n           l1 = k; n           break; n         } n       } n       if (l1 == -1) n         cout <<  NO ; n       else { n         for (int l = l1 + 1; l < s.size(); l++) { n           if (s[l] ==  l ) { n             l2 = l; n             break; n           } n         } n         if (l2 == -1) n           cout <<  NO ; n         else { n           for (int m = l2 + 1; m < s.size(); m++) { n             if (s[m] ==  o ) { n               o = m; n               break; n             } n           } n           if (o == -1) n             cout <<  NO ; n           else n             cout <<  YES ; n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int h, m, n; n void input() { scanf( %d%d%d , &h, &m, &n); } n set<int> s; n int head[200010], tail[200010], cnt[200010], idx[200010], nid; n void build() { n   for (int i = 0; i < h; i++) s.insert(i); n   memset(idx, -1, sizeof(idx)); n   memset(tail, -1, sizeof(tail)); n   for (int i = 0; i < h; i++) { n     int st = nid; n     if (idx[i] == -1) n       for (int j = i; idx[j] == -1; j = (j + m) % h) { n         head[j] = st; n         idx[j] = nid++; n       } n     int ed = nid; n     for (int j = i; tail[j] == -1; j = (j + m) % h) tail[j] = ed; n   } n   for (int i = 0; i < h; i++) cnt[i] = tail[i] - head[i]; n } n long long ans; n map<int, int> pos; n void add(int id, int hash) { n   set<int>::iterator it = s.lower_bound(idx[hash]); n   if (it == s.end() || *it >= tail[hash]) it = s.lower_bound(head[hash]); n   ans += (*it - idx[hash] + cnt[hash]) % cnt[hash]; n   pos[id] = *it; n   s.erase(*it); n } n void del(int id) { s.insert(pos[id]); } n void solve() { n   char op; n   int id, hash; n   while (n--) { n     scanf(  %c%d , &op, &id); n     if (op ==  + ) { n       scanf( %d , &hash); n       add(id, hash); n     } else if (op ==  - ) { n       del(id); n     } else n       puts( QQ ); n   } n   printf( %I64d n , ans); n } n int main() { n   input(); n   build(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 1005; n string a[MaxN], start[MaxN], finish[MaxN], st[MaxN], Fans[MaxN]; n int n, m, ind, Find; n int b[MaxN]; n void check() { n   int flag = 0, flag1 = 0, i, j; n   ind = 0; n   for (i = 1; i <= n; i++) { n     if (b[i] == 1) { n       ind++; n       st[ind] = a[i]; n     } n   } n   for (i = 1; i <= m; i++) { n     flag = 0; n     for (j = 1; j <= ind; j++) { n       if ((st[j] == start[i]) || (st[j] == finish[i])) { n         flag++; n       } n     } n     if (flag >= 2) { n       flag1 = 1; n       break; n     } n   } n   if ((flag1 == 0) && (ind > Find)) { n     for (i = 1; i <= ind; i++) { n       Fans[i] = st[i]; n     } n     Find = ind; n   } n } n void rec(int i) { n   if (i > n) { n     check(); n     return; n   } n   b[i] = 0; n   rec(i + 1); n   b[i] = 1; n   rec(i + 1); n } n int main() { n   int i, j; n   Find = 0; n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (i = 1; i <= m; i++) { n     cin >> start[i] >> finish[i]; n   } n   rec(1); n   sort(Fans + 1, Fans + Find + 1); n   cout << Find << endl; n   for (i = 1; i <= Find; i++) { n     cout << Fans[i] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long n, m, a, b, x, y; n int main() { n   cin >> n >> m >> x >> y >> a >> b; n   long long k = gcd(a, b); n   long long kk = min(n / (a / k), m / (b / k)); n   a = a * kk / k, b = b * kk / k; n   long long x1, x2, y1, y2; n   x1 = x - (a + 1) / 2, x2 = x1 + a; n   y1 = y - (b + 1) / 2, y2 = y1 + b; n   if (x1 < 0) x1 = 0; n   x2 = x1 + a; n   x2 = min(n, x2), x1 = x2 - a; n   if (y1 < 0) y1 = 0; n   y2 = y1 + b; n   y2 = min(m, y2), y1 = y2 - b; n   cout << x1 <<     << y1 <<     << x2 <<     << y2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 20005; n const int len = 400; n const int mod = 1e9 + 7; n int n, p, q, ans[N]; n struct node { n   int c, h, t; n } a[N]; n vector<int> all, in[N * 4]; n vector<pair<int, int> > lazy[N]; n void upd(int l, int r, int x, int v = 1, int tl = 0, int tr = all.size() - 1) { n   if (tl > r || tr < l || l > r) return; n   if (l <= tl && tr <= r) { n     in[v].push_back(x); n     return; n   } n   int tm = (tl + tr) / 2; n   upd(l, r, x, v * 2, tl, tm); n   upd(l, r, x, v * 2 + 1, tm + 1, tr); n } n void solve(vector<int> now, int v = 1, int tl = 0, int tr = all.size() - 1) { n   for (auto id : in[v]) { n     for (int i = 4000; i >= 0; i--) { n       if (i - a[id].c >= 0) { n         now[i] = max(now[i], now[i - a[id].c] + a[id].h); n       } n     } n   } n   if (tl == tr) { n     for (auto i : lazy[all[tl]]) { n       int id = i.second; n       int w = i.first; n       ans[id] = now[w]; n     } n     return; n   } n   int tm = (tl + tr) / 2; n   solve(now, v * 2, tl, tm); n   solve(now, v * 2 + 1, tm + 1, tr); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> p; n   for (int i = 1; i <= n; i++) { n     cin >> a[i].c >> a[i].h >> a[i].t; n   } n   cin >> q; n   for (int i = 1; i <= q; i++) { n     int x, y; n     cin >> x >> y; n     all.push_back(x); n     lazy[x].push_back(make_pair(y, i)); n   } n   sort(all.begin(), all.end()); n   all.resize(unique(all.begin(), all.end()) - all.begin()); n   for (int i = 1; i <= n; i++) { n     int l = lower_bound(all.begin(), all.end(), a[i].t) - all.begin(); n     int r = n         upper_bound(all.begin(), all.end(), a[i].t + p - 1) - all.begin() - 1; n     if (l <= r) upd(l, r, i); n   } n   vector<int> now; n   now.resize(4001); n   solve(now); n   for (int i = 1; i <= q; i++) cout << ans[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int tes; n   cin >> tes; n   while (tes--) { n     long long int n, m; n     cin >> m >> n; n     vector<pair<long long int, long long int>> arr(n); n     for (long long int i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second; n     sort(arr.begin(), arr.end()); n     vector<long long int> flower(n); n     vector<long long int> cum(n + 1); n     for (long long int i = 0; i < n; i++) flower[i] = arr[i].first; n     cum[n] = 0; n     for (int i = n - 1; i >= 0; i--) cum[i] = cum[i + 1] + flower[i]; n     long long int ans, here, left; n     ans = 0; n     m--; n     for (long long int i = 0; i < n; i++) { n       here = 0; n       long long int key = arr[i].second; n       long long int ix = n           (upper_bound(flower.begin(), flower.end(), key) - flower.begin()); n       if (ix < n - m) ix = n - m; n       here = here + cum[ix]; n       left = m - (n - ix); n       here = here + left * key; n       if (i < ix) n         here = here + arr[i].first; n       else n         here = here + key; n       ans = max(ans, here); n     } n     cout << ans << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct SegTree { n   const int INF = 1e9; n   vector<pair<int, int>> max_vc; n   vector<int> add; n   int h = 1; n   pair<int, int> combine(pair<int, int> a, pair<int, int> b) { n     if (a.first == b.first) n       return {a.first, a.second + b.second}; n     else n       return max(a, b); n   } n   void push(int i) { n     max_vc[i].first += add[i]; n     if (i < h) { n       add[2 * i] += add[i]; n       add[2 * i + 1] += add[i]; n     } n     add[i] = 0; n   } n   SegTree(int n, int v) { n     while (h < n) h <<= 1; n     max_vc.resize(2 * h, {v, 1}); n     add.resize(2 * h, 0); n     for (int i = h - 1; i > 0; --i) n       max_vc[i] = combine(max_vc[2 * i], max_vc[2 * i + 1]); n   } n   void recAdd(int a, int b, int v, int i, int ia, int ib) { n     push(i); n     if (ib < a || b < ia) return; n     if (a <= ia && ib <= b) { n       add[i] += v; n       push(i); n     } else { n       int im = (ia + ib) >> 1; n       recAdd(a, b, v, 2 * i, ia, im); n       recAdd(a, b, v, 2 * i + 1, im + 1, ib); n       max_vc[i] = combine(max_vc[2 * i], max_vc[2 * i + 1]); n     } n   } n   pair<int, int> recGet(int a, int b, int i, int ia, int ib) { n     push(i); n     if (ib < a || b < ia) return {-INF, 0}; n     if (a <= ia && ib <= b) return max_vc[i]; n     int im = (ia + ib) >> 1; n     return combine(recGet(a, b, 2 * i, ia, im), n                    recGet(a, b, 2 * i + 1, im + 1, ib)); n   } n   void addInt(int a, int b, int v) { recAdd(a, b, v, 1, 0, h - 1); } n   pair<int, int> getInt(int a, int b) { return recGet(a, b, 1, 0, h - 1); } n }; n int pre[300005], b[300005]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   SegTree seg(n, 0); n   vector<pair<int, int>> maxes; n   long long res = 0; n   for (int i = 1; i <= n; ++i) { n     int a; n     cin >> a; n     pre[i] = max(pre[i - 1], b[a]); n     b[a] = i; n     int p = pre[i] + 1; n     seg.addInt(p, i, 1); n     seg.addInt(i, i, -a); n     pair<int, int> maxes_add = {a, i}; n     while ((!maxes.empty()) && (maxes.back().first <= a)) { n       seg.addInt(maxes.back().second, maxes_add.second - 1, n                  -(a - maxes.back().first)); n       maxes_add.second = maxes.back().second; n       maxes.pop_back(); n     } n     maxes.push_back(maxes_add); n     pair<int, int> sub = seg.getInt(p, i); n     if (sub.first == 0) res += sub.second; n   } n   cout << res <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 10; n const double Pi = acos(-1); n inline long long read() { n   long long x = 0, f = 1, ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch - 48, ch = getchar(); n   return x * f; n } n long long n, q; n long long cnt, head[N], deep[N]; n struct Edge { n   long long to, nxt, dis; n } edge[N << 1]; n inline void AddEdge(long long u, long long v, long long w) { n   edge[++cnt] = (Edge){v, head[u], w}; n   head[u] = cnt; n   return; n } n void dfs(long long u, long long fa) { n   for (long long i = head[u], v; i; i = edge[i].nxt) { n     v = edge[i].to; n     if (v == fa) continue; n     deep[v] = deep[u] + edge[i].dis; n     dfs(v, u); n   } n   return; n } n long long dep[N], up[N][20]; n void dfs1(long long u, long long fa) { n   dep[u] = dep[fa] + 1, up[u][0] = fa; n   for (long long i = 1; dep[u] >= (1 << i); ++i) n     up[u][i] = up[up[u][i - 1]][i - 1]; n   for (long long i = head[u], v; i; i = edge[i].nxt) { n     v = edge[i].to; n     if (v == fa) continue; n     dfs1(v, u); n   } n   return; n } n inline long long LCA(long long u, long long v) { n   if (dep[u] < dep[v]) swap(u, v); n   for (register long long i = 19; i >= 0; --i) n     if (dep[up[u][i]] >= dep[v]) u = up[u][i]; n   if (u == v) return u; n   for (register long long i = 19; i >= 0; --i) n     if (up[u][i] != up[v][i]) u = up[u][i], v = up[v][i]; n   return up[u][0]; n } n struct Node { n   long long f, id; n } nd[N]; n long long F[N], siz[N]; n long long GetF(long long x) { return F[x] == x ? x : F[x] = GetF(F[x]); } n void Merge(long long x, long long y) { n   x = GetF(x), y = GetF(y); n   if (x == y) return; n   if (siz[x] > siz[y]) swap(x, y); n   siz[y] += siz[x]; n   F[x] = F[y]; n   return; n } n long long f[N]; n bool cmp(Node x, Node y) { return x.f < y.f; } n signed main() { n   n = read(); n   for (register long long i = 2; i <= n; ++i) { n     long long a = read(), b = read(), w = read(); n     AddEdge(a, b, w), AddEdge(b, a, w); n   } n   dfs(1, 0); n   long long L, R, mx = 0; n   for (register long long i = 1; i <= n; ++i) n     if (deep[i] > mx) mx = deep[i], L = i; n   deep[L] = 0, mx = 0, dfs(L, 0); n   for (register long long i = 1; i <= n; ++i) n     if (deep[i] > mx) mx = deep[i], R = i; n   dfs1(L, 0); n   for (register long long i = 1; i <= n; ++i) { n     nd[i].id = i; n     f[i] = nd[i].f = max(deep[i] + deep[L] - 2 * deep[LCA(i, L)], n                          deep[i] + deep[R] - 2 * deep[LCA(i, R)]); n   } n   q = read(); n   sort(nd + 1, nd + n + 1, cmp); n   while (q--) { n     long long val = read(); n     long long l = n + 1; n     for (register long long i = 1; i <= n; ++i) F[i] = i, siz[i] = 1; n     long long ans = 0; n     for (register long long r = n; r >= 1; --r) { n       while (nd[r].f - nd[l - 1].f <= val && l > 1) { n         --l; n         long long u = nd[l].id; n         for (long long i = head[u], v; i; i = edge[i].nxt) { n           v = edge[i].to; n           if (f[v] >= nd[l].f && f[v] <= nd[r].f) n             Merge(u, v), ans = max(ans, siz[GetF(u)]); n         } n       } n       --siz[GetF(nd[r].id)]; n     } n     printf( %lld n , max(1ll, ans)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int m; n   cin >> m; n   unsigned long long int A[m], Ap[m]; n   unsigned long long int B[m]; n   map<unsigned long long int, vector<unsigned long long int> > mp; n   for (long long int i = 0; i < m; i++) { n     cin >> B[i]; n   } n   for (long long int i = 0; i < m; i++) { n     cin >> A[i]; n     Ap[i] = A[i]; n   } n   sort(Ap, Ap + m); n   sort(B, B + m); n   for (int i = 0; i < m; i++) { n     mp[Ap[i]].push_back(B[m - 1 - i]); n   } n   for (long long int i = 0; i < m; i++) { n     cout << mp[A[i]].back() <<    ; n     mp[A[i]].pop_back(); n   } n } n int main() { n   std::ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const MAX = 200 * 1000 + 41; n long double const EPS = 1e-12; n bool eq(long double a, long double b) { return abs(a - b) < EPS; } n bool leq(long double a, long double b) { return (a < b || eq(a, b)); } n int n, w; n int x[MAX]; n int v[MAX]; n pair<long double, long double> a[MAX]; n pair<int, int> b[MAX]; n int const ADD = 0; n int const ASK = 1; n struct Event { n   int y, s, t; n   Event(){}; n   Event(int y, int s, int t) : y(y), s(s), t(t){}; n   bool operator<(const Event &a) const { n     if (t != a.t) return t < a.t; n     return y < a.y; n   }; n }; n vector<Event> e[MAX]; n int gethash(vector<long double> &v, long double x) { n   int l = -1; n   int r = (int)v.size(); n   while (r - l > 1) { n     int h = (l + r) / 2; n     if (leq(v[h], x)) { n       l = h; n     } else { n       r = h; n     } n   } n   return l; n } n void init() { n   vector<long double> tmp; n   for (int i = 1; i <= n; i++) { n     long double l = (long double)v[i] / x[i] - (long double)w / x[i]; n     long double r = (long double)v[i] / x[i] + (long double)w / x[i]; n     a[i] = make_pair(l, r); n     tmp.push_back(l); n     tmp.push_back(r); n   } n   sort(tmp.begin(), tmp.end()); n   for (int i = 1; i <= n; i++) { n     b[i] = make_pair(gethash(tmp, a[i].first), gethash(tmp, a[i].second)); n   } n   for (int i = 1; i <= n; i++) { n     int x = b[i].first; n     int y = b[i].second; n     e[x].push_back(Event(y, 41, ADD)); n     e[x].push_back(Event(y, -1, ASK)); n     e[MAX - 1].push_back(Event(y, 1, ASK)); n   } n } n namespace Fen { n int F[MAX]; n void upd(int x, int v) { n   while (x < MAX) { n     F[x] += v; n     x |= (x + 1); n   } n } n int get(int x) { n   int res = 0; n   while (x >= 0) { n     res += F[x]; n     x = (x & (x + 1)) - 1; n   } n   return res; n } n };  // namespace Fen n long long ans; n void solve() { n   init(); n   for (int i = 1; i <= n; i++) { n     while (false) { n     }; n   } n   for (int i = 0; i <= MAX - 1; i++) { n     sort(e[i].begin(), e[i].end()); n     for (Event z : e[i]) { n       if (z.t == ADD) { n         Fen::upd(z.y, 1); n       } else { n         int q = Fen::get(z.y); n         ans += q * z.s; n       } n     } n   } n   unordered_map<int, int> ql; n   for (int i = 1; i <= n; i++) { n     ql[b[i].first]++; n     ans += ql[b[i].first] - 1; n   } n   printf( %lld n , ans); n } n int main() { n   scanf( %d %d , &n, &w); n   for (int i = 1; i <= n; i++) { n     scanf( %d %d , &x[i], &v[i]); n   } n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int nax = 1e5 + 5; n vector<int> w[nax]; n bool del[nax]; n int legs[nax]; n void dfs(int a, int par = 0) { n   if (w[a].size() <= 2) { n     del[a] = true; n     for (int i = 0; i < w[a].size(); i++) { n       int b = w[a][i]; n       if (b != par) dfs(b, a); n     } n   } n } n int main() { n   int n, b; n   scanf( %d , &n); n   for (int i = 0; i < n - 1; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     w[a].push_back(b); n     w[b].push_back(a); n   } n   for (int a = 1; a <= n; ++a) n     if (w[a].size() == 1) dfs(a); n   for (int a = 1; a <= n; ++a) { n     for (int i = 0; i < w[a].size(); i++) { n       b = w[a][i]; n       if (del[b]) legs[a] = min(legs[a] + 1, 2); n     } n   } n   for (int a = 1; a <= n; ++a) n     if (!del[a]) { n       int cnt = 0; n       for (int i = 0; i < w[a].size(); i++) { n         b = w[a][i]; n         if (!del[b] && w[b].size() - legs[b] > 1) ++cnt; n       } n       if (cnt > 2) { n         puts( No ); n         return 0; n       } n     } n   puts( Yes ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int intmax = 0x3f3f3f3f; n const long long lldmax = 0x3f3f3f3f3f3f3f3fll; n double eps = 1e-8; n template <class T> n inline void checkmin(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkmax(T &a, T b) { n   if (b > a) a = b; n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <class T> n inline T lowbit(T n) { n   return (n ^ (n - 1)) & n; n } n template <class T> n inline int countbit(T n) { n   return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); n } n template <class T> n inline T checkmod(T n, T m) { n   return (n % m + m) % m; n } n template <class T> n inline T gcd(T a, T b) { n   if (a < 0) return gcd(-a, b); n   if (b < 0) return gcd(a, -b); n   return (b == 0) ? a : gcd(b, a % b); n } n template <class T> n inline T euclid(T a, T b, T &x, T &y) { n   if (a < 0) { n     T d = euclid(-a, b, x, y); n     x = -x; n     return d; n   } n   if (b < 0) { n     T d = euclid(a, -b, x, y); n     y = -y; n     return d; n   } n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } else { n     T d = euclid(b, a % b, x, y); n     T t = x; n     x = y; n     y = t - (a / b) * y; n     return d; n   } n } n template <class T> n inline int dblcmp(T a, const T b) { n   a -= b; n   return a > eps ? 1 : (a < -eps ? -1 : 0); n } n template <class T> n inline int sgn(T a) { n   return a > eps ? 1 : (a < -eps ? -1 : 0); n } n const int N = 1100000; n long long a[N], cnt[N]; n long long ch[N]; n int n, k; n bool check(int m) { n   for (int i = 0; i < m; ++i) ch[i] = 0; n   long long del = k; n   for (int i = 0; i < n; ++i) { n     ch[a[i] % m]++; n     long long tmp = ch[a[i] % m]; n     if (tmp > 1) del--; n     if (del < 0) break; n   } n   return del >= 0; n } n int main() { n   cin >> n >> k; n   if (n <= k + 1) { n     cout << 1 << endl; n     return 0; n   } n   for (int i = 0; i < n; ++i) cin >> a[i]; n   sort(a, a + n); n   for (int i = 0; i < n; ++i) { n     for (int j = i + 1; j < n; ++j) { n       cnt[a[j] - a[i]]++; n     } n   } n   long long ans = N; n   for (int i = 1; i < N; ++i) { n     int tmp = 0; n     for (int j = i; j < N; j += i) { n       tmp += cnt[j]; n     } n     if (tmp <= k * (k + 1) / 2 && check(i)) { n       ans = i; n       break; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = numeric_limits<int>::max(); n const long long M = 998244353, MAXN = 1e6 + 10; n long long n, c[MAXN]; n long long mp(long long a, long long e) { n   long long ret = 1; n   while (e) { n     if (e % 2) ret = ret * a % M; n     a = a * a % M; n     e /= 2; n   } n   return ret; n } n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0); n   cin >> n; n   c[0] = 1; n   for (int i = 0; i < n; i++) n     c[i + 1] = c[i] * (n - i) % M * mp(i + 1, M - 2) % M; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     long long add = (i + 1) % 2 ? -1 : 1; n     add *= c[i]; n     add = add * mp(3, n * (n - i) + i) % M; n     ans = (ans + add) % M; n   } n   ans = ans * 2 % M; n   for (int i = 0; i < n; i++) { n     long long add = (i + 1) % 2 ? -1 : 1; n     add *= c[i]; n     long long inner = (mp(1 - mp(3, i), n) - mp(-mp(3, i), n)) % M; n     add = 3 * add * inner % M; n     ans = (ans + add) % M; n   } n   while (ans < 0) ans += M; n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, int> m1; n map<int, string> m2; n pair<long long, long long> r[128]; n long long H[128], V[128], B[128], S[128]; n vector<int> g1[128], g2[128]; n int G[128]; n int main() { n   memset(H, 0, sizeof H); n   memset(V, 0, sizeof V); n   memset(G, 0, sizeof G); n   memset(B, 0, sizeof B); n   memset(S, 0, sizeof S); n   int N; n   cin >> N; n   string s; n   getline(cin, s); n   while (N--) { n     getline(cin, s); n     istringstream sin(s); n     string s2; n     sin >> s2; n     if (s2 ==  Widget ) { n       string name; n       sin >> name; n       name = name.substr(0, name.find( ( ) + 1); n       istringstream sin2(s.substr(s.find( ( ) + 1)); n       int t1, t2; n       char c; n       sin2 >> t1 >> c >> t2; n       name = name.substr(0, name.size() - 1); n       int id = m1.size(); n       m1[name] = id; n       m2[id] = name; n       r[id] = make_pair(t1, t2); n     } else if (s2 ==  VBox ) { n       string name; n       sin >> name; n       int id = m1.size(); n       m1[name] = id; n       m2[id] = name; n       V[id] = 1; n     } else if (s2 ==  HBox ) { n       string name; n       sin >> name; n       int id = m1.size(); n       m1[name] = id; n       m2[id] = name; n       H[id] = 1; n     } else if (s.find( set_border ) != string::npos) { n       string name = s.substr(0, s.find( . )); n       int id = m1[name]; n       istringstream sin2(s.substr(s.find( ( ) + 1)); n       int sz; n       sin2 >> sz; n       B[id] = sz; n     } else if (s.find( set_spacing ) != string::npos) { n       string name = s.substr(0, s.find( . )); n       int id = m1[name]; n       istringstream sin2(s.substr(s.find( ( ) + 1)); n       int sz; n       sin2 >> sz; n       S[id] = sz; n     } else if (s.find( pack ) != string::npos) { n       string name1 = s.substr(0, s.find( . )); n       string name2 = s.substr(s.find( ( ) + 1); n       name2 = name2.substr(0, name2.size() - 1); n       int id1 = m1[name1], id2 = m1[name2]; n       g1[id1].push_back(id2); n       G[id1]++; n       g2[id2].push_back(id1); n     } n   } n   int left = m1.size(); n   int done[128]; n   memset(done, 0, sizeof done); n   N = m1.size(); n   while (left) { n     int cur = -1; n     for (int i = 0; i < (N); i++) n       if (done[i] == 0 && G[i] == 0) { n         cur = i; n       } n     done[cur] = 1; n     for (int i = 0; i < (g2[cur].size()); i++) G[g2[cur][i]]--; n     left--; n     long long x = 0, y = 0; n     if (V[cur] && g1[cur].size()) { n       for (int i = 0; i < (g1[cur].size()); i++) { n         x = max(x, r[g1[cur][i]].first); n         y += r[g1[cur][i]].second; n       } n       x += 2 * B[cur]; n       y += 2 * B[cur]; n       y += (g1[cur].size() - 1) * S[cur]; n       r[cur] = make_pair(x, y); n     } n     if (H[cur] && g1[cur].size()) { n       for (int i = 0; i < (g1[cur].size()); i++) { n         y = max(y, r[g1[cur][i]].second); n         x += r[g1[cur][i]].first; n       } n       x += 2 * B[cur]; n       y += 2 * B[cur]; n       x += (g1[cur].size() - 1) * S[cur]; n       r[cur] = make_pair(x, y); n     } n   } n   for (typeof(m1.begin()) it = m1.begin(); it != m1.end(); it++) { n     cout << it->first <<     << r[it->second].first <<     n          << r[it->second].second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int sh = 1e6; n const int ppr = 257; n const long long INF = 1e18; n const int inf = 1e9; n const int mod = 1e9 + 7; n const int N = 6e5 + 123; n const long double pi = 3.141592653589793238462643; n const int dx[] = {1, 0, -1, 0}; n const int dy[] = {0, 1, 0, -1}; n vector<int> g[N]; n int T, n, a[N], b[N]; n int main() { n   cin >> T; n   while (T--) { n     cin >> n; n     for (int i = 1; i <= n + 1; i++) g[i].clear(), b[i] = 0; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n       if (a[i] == -1) a[i] = i + 1; n       g[a[i]].push_back(i); n     } n     for (int i = 1; i <= n + 1; i++) sort(g[i].begin(), g[i].end()); n     queue<int> q; n     int timer = n + 1; n     q.push(n + 1); n     while (!q.empty()) { n       int v = q.front(); n       q.pop(); n       b[v] = timer--; n       for (auto j : g[v]) { n         if (b[j] > 0) continue; n         q.push(j); n       } n     } n     stack<int> st; n     bool ok = 1; n     for (int i = n; i >= 1; i--) { n       while (!st.empty() && b[st.top()] < b[i]) st.pop(); n       if ((st.empty() && a[i] != n + 1) || (!st.empty() && a[i] != st.top())) { n         ok = 0; n         break; n       } n       st.push(i); n     } n     if (!ok) { n       cout << -1 <<   n ; n       continue; n     } n     for (int i = 1; i <= n; i++) cout << b[i] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cout.tie(NULL); n   cin.tie(NULL); n   ; n   int n, m; n   cin >> n >> m; n   set<long long> s; n   long long ans = 0; n   long long a[n]; n   long long b[m]; n   long long mx = 0; n   map<long long, long long> mp; n   bool f = true; n   set<long long> st; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     mx = max(mx, a[i]); n     s.insert(a[i]); n     ans += m * a[i]; n     st.insert(a[i]); n     mp[a[i]] += m - 1; n   } n   for (int i = 0; i < m; i++) { n     cin >> b[i]; n     if (b[i] < mx) f = false; n   } n   if (!f) { n     cout << -1 << endl; n     return 0; n   } n   sort(b, b + m); n   reverse(b, b + m); n   for (int i = 0; i < m; i++) { n     set<long long>::iterator it = s.upper_bound(b[i]); n     --it; n     if ((b[i] - *it) > 0 && st.find(b[i]) != st.end()) { n       continue; n     } n     ans += b[i] - (*it); n     mp[*it]--; n     if (mp[*it] == 0) s.erase(it); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int val[100005], Max[100005], Min[100005]; n int n; n int lef[100005], rig[100005]; n void dfs(int r) { n   if (lef[r] == 0) { n     Max[r] = Min[r] = val[r]; n     return; n   } n   dfs(lef[r]); n   dfs(rig[r]); n   Max[r] = Max[rig[r]]; n   Min[r] = Min[lef[r]]; n } n long long sum[100005]; n int dep[100005]; n void dfs(int r, long long s, int d) { n   if (lef[r] == 0) { n     sum[r] = s; n     dep[r] = d; n     return; n   } n   dfs(lef[r], s + Min[rig[r]], d + 1); n   dfs(rig[r], s + Max[lef[r]], d + 1); n } n struct node { n   int id, v; n   friend bool operator<(node a, node b) { return a.v < b.v; } n } a[100005]; n bool cmp(node a, int x) { return a.v < x; } n int main() { n   scanf( %d , &n); n   int x, y, root; n   for (int i = 1; i <= n; i++) { n     scanf( %d %d , &x, &y); n     a[i].v = y; n     a[i].id = i; n     val[i] = y; n     if (x == -1) n       root = i; n     else { n       if (lef[x] == 0) { n         lef[x] = i; n       } else { n         rig[x] = i; n         if (val[lef[x]] > val[rig[x]]) { n           swap(lef[x], rig[x]); n         } n       } n     } n   } n   sort(a + 1, a + 1 + n); n   dfs(root); n   dfs(root, 0, 0); n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %d , &x); n     int t = lower_bound(a + 1, a + 1 + n, x, cmp) - a; n     if (t == n + 1) t--; n     t = a[t].id; n     while (lef[t]) { n       if (val[t] > x) n         t = lef[t]; n       else n         t = rig[t]; n     } n     printf( %.10lf n , 1.0 * sum[t] / dep[t]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c; n int f[2001 * 2001]; n int _GCD[2001][2001]; n int GCD(int a, int b) { n   if (_GCD[a][b]) return _GCD[a][b]; n   if (!b) return a; n   return _GCD[a][b] = GCD(b, a % b); n } n int phi(int x) { n   if (x & 1) return -1; n   return 1; n } n int main() { n   cin >> a >> b >> c; n   for (int j = (1); j <= (b); j++) n     for (int k = (1); k <= (c); k++) n       if (GCD(j, k) == 1) f[j * k] += (b / j) * (c / k); n   for (int x = (1); x <= (b * c); x++) n     for (int y = 2 * x; y <= b * c; y += x) f[x] += f[y]; n   int ans = 0; n   for (int i = (1); i <= (a); i++) { n     vector<int> L; n     int x, y; n     for (x = i, y = 2; y * y <= x; y++) n       if (x % y == 0) { n         L.push_back(y); n         while (x % y == 0) x /= y; n       } n     if (x != 1) L.push_back(x); n     for (int s = (0); s <= ((1 << int((L).size())) - 1); s++) { n       int x = 1, cnt = 0; n       for (int j = (0); j <= (int((L).size()) - 1); j++) n         if ((s >> j) & 1) cnt++, x *= L[j]; n       ans += phi(cnt) * f[x] * (a / i); n     } n   } n   cout << (ans % (1 << 30) + (1 << 30)) % (1 << 30); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int ans; n int father[1100]; n int find(int x) { n   if (father[x] == x) return x; n   father[x] = find(father[x]); n   return father[x]; n } n bool Union(int x, int y) { n   int f1 = find(x); n   int f2 = find(y); n   if (f1 == f2) return false; n   if (f1 < f2) n     father[f1] = f2; n   else n     father[f2] = f1; n   ans++; n   return true; n } n int n, m; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) father[i] = i; n   for (int i = 1; i <= m; i++) { n     int x, y; n     cin >> x >> y; n     if (!Union(x, y)) { n       cout <<  no  << endl; n       return 0; n     } n   } n   if (m != n - 1) n     cout <<  no  << endl; n   else n     cout <<  yes  << endl; n   return 0; n } n


#include<bits/stdc++.h> n using namespace std;  n const int N=1e5+77,B=330;  n int n,q,a[N],t[N],b[N],l[N],r[N],m,g[N];  n int nx(int x){return max(1,a[x]-t[b[x]]); } n int jp(int x){if(t[b[x]]>=B)return nx(x); return g[x]; } n void reb(int x) n { n  for(int i=l[x]; i<=r[x]; i++) n  { n   int j=nx(i);  n   if(j<l[x]) n    g[i]=j;  n   else n    g[i]=g[j];  n  } n } n void update(int x,int y,int v) n { n  if(b[x]==b[y]) n  { n   for(int i=x; i<=y; i++) n    a[i]=max(1,a[i]-v);  n   reb(b[x]);  n   return;  n  } n  for(int i=x; i<=r[b[x]]; i++) n   a[i]=max(1,a[i]-v);  n  reb(b[x]);  n  for(int i=b[x]+1; i<b[y]; i++) n  { n   t[i]=min(t[i]+v,n);  n   if(t[i]<B) n    reb(i);  n  } n  for(int i=l[b[y]]; i<=y; i++) n   a[i]=max(1,a[i]-v);  n  reb(b[y]);  n } n void lca(int u,int v) n { n  int lu=u,lv=v;  n  while(u!=v) n  { n   if(u<v) n    swap(u,v),swap(lu,lv);  n   lu=u; u=jp(u);  n  } n  u=lu,v=lv;  n  while(u!=v) n  { n   if(u<v) n    swap(u,v);  n   u=nx(u);  n  } n  printf( %d n ,u);  n } n int main() n { n  scanf( %d%d ,&n,&q);  n  for(int i=2; i<=n; i++) n   scanf( %d ,&a[i]);  n  for(int x=1; x<=n; x+=B) n  { n   int y=min(x+B-1,n);  n   m++;  n   l[m]=x,r[m]=y;  n   for(int i=x; i<=y; i++) n    b[i]=m;  n   reb(m);  n  } n  while(q--) n  { n   int t;  n   scanf( %d ,&t);  n   if(t==1) n   { n    int x,y,v;  n    scanf( %d%d%d ,&x,&y,&v);  n    update(x,y,v);  n   } n   else n   { n    int u,v;  n    scanf( %d%d ,&u,&v);  n    lca(u,v);  n   } n  } n  n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int i, j; n   char s[1000]; n   while (gets(s)) { n     int l = strlen(s); n     n = 0; n     int num_jian = 0, num_jia = 1; n     for (i = 0; i < l; ++i) { n       if (s[i] ==  = ) { n         i += 2; n         while (i != l) { n           n *= 10; n           n += s[i] -  0 ; n           ++i; n         } n       } else if (s[i] ==  - ) n         num_jian++; n       else if (s[i] ==  + ) n         num_jia++; n     } n     int v_jian = 1; n     if (num_jian < num_jia) v_jian = n; n     if (num_jia * n - num_jian < n || num_jia - num_jian * n > n) n       printf( Impossible n ); n     else { n       printf( Possible n ); n       j = 0; n       for (i = 0; i < l; ++i) { n         if (s[i] ==  ? ) { n           if (i == 0 || s[i - 2] ==  + ) { n             if (num_jia == 1) n               printf( %d , n + num_jian * v_jian); n             else n               printf( %d , (n + num_jian * v_jian) / num_jia); n             if ((n + num_jian * v_jian) / num_jia > 5) j = 1; n             n -= (n + num_jian * v_jian) / num_jia; n             num_jia--; n           } else if (s[i - 2] ==  - ) n             printf( %d , v_jian); n         } else n           printf( %c , s[i]); n       } n       printf(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 66; n int power[MAX + 10]; n int between[MAX + 10]; n int extra[MAX + 10]; n int ende[MAX + 10]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     long long foo; n     cin >> foo; n     int cc = 0; n     bool good = true; n     while (foo > 1) { n       if (foo & 1) { n         good = false; n       } n       cc++; n       foo >>= 1; n     } n     if (good) { n       power[cc]++; n     } else { n       between[cc]++; n     } n   } n   int low = 1, high = power[0] + 1; n   while (low < high) { n     int mid = (low + high) >> 1; n     int cur = mid; n     for (int i = 0; i < MAX; i++) { n       extra[i] = power[i] - cur; n       int new_cur = min(cur, power[i + 1]); n       ende[i] = cur - new_cur; n       cur = new_cur; n     } n     bool fail = false; n     int open = 0; n     for (int i = MAX - 1; i >= 0; i--) { n       open += ende[i]; n       open -= between[i]; n       open -= extra[i + 1]; n       if (open < 0) { n         fail = true; n         break; n       } n     } n     open -= extra[0]; n     if (open < 0) { n       fail = true; n     } n     if (fail) { n       low = mid + 1; n     } else { n       high = mid; n     } n   } n   if (low > power[0]) { n     cout << -1 << endl; n     return 0; n   } n   for (int i = low; i <= power[0]; i++) { n     cout << i <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int const N = int(2e5) + 7; n int const mod = int(1e9) + 7; n int a[N]; n int ra[N], le[N]; n int raB[N][31], leB[N][31]; n int main() { n   ios_base::sync_with_stdio(false); n   ifstream in( input.txt ); n   int n; n   cin >> n; n   vector<int> st; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     while (st.size() > 0 && a[st.back()] <= a[i]) { n       int ind = st.back(); n       st.pop_back(); n       int ll = 0; n       if (st.size() > 0) { n         ll = st.back(); n       } n       le[ind] = ll; n       ra[ind] = i; n     } n     st.push_back(i); n     for (int j = 0; j <= 30; ++j) { n       if (a[i] & (1 << j)) { n         leB[i][j] = i; n       } else { n         leB[i][j] = leB[i - 1][j]; n       } n     } n   } n   while (st.size() > 0) { n     int ind = st.back(); n     st.pop_back(); n     int ll = 0; n     if (st.size() > 0) { n       ll = st.back(); n     } n     le[ind] = ll; n     ra[ind] = n + 1; n   } n   for (int i = n; i >= 0; --i) { n     for (int j = 0; j <= 30; ++j) { n       if (a[i] & (1 << j)) { n         raB[i][j] = i; n       } else { n         raB[i][j] = raB[i + 1][j]; n       } n     } n   } n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     int indl = 0, indr = n + 1; n     for (int j = 30; j >= 0; --j) { n       if (!(a[i] & (1 << j)) && (leB[i - 1][j])) { n         indl = max(indl, leB[i - 1][j]); n       } n     } n     for (int j = 30; j >= 0; --j) { n       if (!(a[i] & (1 << j)) && (raB[i + 1][j])) { n         indr = min(indr, raB[i + 1][j]); n       } n     } n     long long am1 = max(0, indl - le[i]); n     long long am2 = max(0, i - max(indl, le[i])); n     long long am3 = max(0, min(ra[i], indr) - i); n     long long am4 = max(0, ra[i] - indr); n     long long del = am1 * (am3 + am4) + am2 * am4; n     ans += am1 * (am3 + am4) + am2 * am4; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, buc[26], cnt = 0, ok = 0, j, m, cntt = 0, dfn = 0, pre; n char a[2], s[100005]; n int main() { n   scanf( %d , &n); n   pre = dfn; n   cntt = 26; n   for (i = 1; i <= n; i++) { n     scanf( %s%s , a, s); n     if (ok == 1) { n       if (a[0] ==  ! ) cnt++; n       if (a[0] ==  ? ) cnt++; n     } else { n       if (a[0] ==  ! ) { n         dfn++; n         cntt = 0; n         m = strlen(s); n         for (j = 0; j < m; j++) { n           if (buc[s[j] -  a ] == pre) { n             cntt++; n             buc[s[j] -  a ] = dfn; n           } n         } n         if (cntt == 1) ok = 1; n         pre = dfn; n       } n       if (a[0] ==  . ) { n         m = strlen(s); n         for (j = 0; j < m; j++) { n           if (buc[s[j] -  a ] == pre) { n             cntt--; n             buc[s[j] -  a ] = pre - 1; n           } n         } n         if (cntt == 1) ok = 1; n       } n       if (a[0] ==  ? ) { n         if (buc[s[0] -  a ] == pre) { n           buc[s[0] -  a ] = pre - 1; n           cntt--; n         } n         if (cntt == 1) ok = 1; n       } n     } n   } n   if (cnt) n     printf( %d , cnt - 1); n   else n     printf( 0 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long double a, b; n   int d; n   while (n > 0) { n     n -= 1; n     cin >> a >> b; n     if (a == b) { n       d = 0; n     } else if (a > b) { n       d = ceil((a - b) / 10); n     } else if (a < b) { n       d = ceil((b - a) / 10); n     } n     cout << d << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 10; n int gi() { n   int x = 0, o = 1; n   char ch = getchar(); n   while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) o = -1, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) x = x * 10 + ch -  0 , ch = getchar(); n   return x * o; n } n struct dat { n   int len, x, y, z, w; n   int getr() const { n     if (!len) return 1e9; n     if (len == 3) return z; n     return w; n   } n   bool operator<(const dat &A) const { return getr() > A.getr(); } n   void print() { n     if (len == 3) n       cout << 3 <<   n  << x <<     << y <<     << z <<   n ; n     else n       cout << 4 <<   n  << x <<     << y <<     << z <<     << w <<   n ; n   } n } ans3[N], ans4[N]; n int n, q, a[N]; n void solve3() { n   static int L[N], R[N], st[N]; n   int tp = 0; n   for (int i = 1; i <= n; i++) { n     while (tp && a[st[tp]] <= a[i]) --tp; n     L[i] = st[tp]; n     st[++tp] = i; n   } n   tp = 0; n   for (int i = n; i; i--) { n     while (tp && a[st[tp]] <= a[i]) --tp; n     R[i] = st[tp]; n     st[++tp] = i; n   } n   for (int i = 1; i <= n; i++) n     if (L[i] && R[i]) ans3[L[i]] = max(ans3[L[i]], (dat){3, L[i], i, R[i], 0}); n   tp = 0; n   for (int i = 1; i <= n; i++) { n     while (tp && a[st[tp]] >= a[i]) --tp; n     L[i] = st[tp]; n     st[++tp] = i; n   } n   tp = 0; n   for (int i = n; i; i--) { n     while (tp && a[st[tp]] >= a[i]) --tp; n     R[i] = st[tp]; n     st[++tp] = i; n   } n   for (int i = 1; i <= n; i++) n     if (L[i] && R[i]) ans3[L[i]] = max(ans3[L[i]], (dat){3, L[i], i, R[i], 0}); n } n bool cmp1(int x, int y) { return a[x] < a[y]; } n bool cmp2(int x, int y) { return a[x] > a[y]; } n void solve4() { n   static int st1[N], st2[N], occ[N]; n   set<int> can; n   int tp1 = 0, tp2 = 0; n   for (int i = n; i; i--) { n     while (tp1 && a[st1[tp1]] > a[i]) { n       int tmp = st1[tp1]; n       --occ[tmp]; n       if (!occ[tmp]) can.insert(tmp); n       --tp1; n     } n     st1[++tp1] = i; n     while (tp2 && a[st2[tp2]] < a[i]) { n       int tmp = st2[tp2]; n       --occ[tmp]; n       if (!occ[tmp]) can.insert(tmp); n       --tp2; n     } n     st2[++tp2] = i; n     occ[i] = 2; n     int p = st1[lower_bound(st1 + 1, st1 + tp1 + 1, i, cmp1) - st1 - 1]; n     int q = st2[lower_bound(st2 + 1, st2 + tp2 + 1, i, cmp2) - st2 - 1]; n     if (p && q) { n       auto it = can.lower_bound(max(p, q) + 1); n       if (it != can.end()) { n         int t = *it; n         p = *lower_bound(st1 + 1, st1 + tp1 + 1, t, greater<int>()); n         q = *lower_bound(st2 + 1, st2 + tp2 + 1, t, greater<int>()); n         ans4[i] = (dat){4, i, min(p, q), max(p, q), t}; n       } n     } n   } n } n int main() { n   cin >> n >> q; n   for (int i = 1; i <= n; i++) a[i] = gi(); n   solve3(); n   solve4(); n   for (int i = n - 1; i; i--) { n     ans3[i] = max(ans3[i], ans3[i + 1]); n     ans4[i] = max(ans4[i], ans4[i + 1]); n   } n   for (int i = 1; i <= q; i++) { n     int l = gi(), r = gi(); n     if (ans4[l].getr() <= r) n       ans4[l].print(); n     else if (ans3[l].getr() <= r) n       ans3[l].print(); n     else n       puts( 0 ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100 + 10; n vector<int> v[maxn]; n set<int> s; n int num[maxn]; n bool cmp(int x, int y) { return num[x] > num[y]; } n int main() { n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) cin >> num[i]; n   for (int i = 1; i <= n; i++) { n     int tmp; n     cin >> tmp; n     v[tmp].push_back(i); n   } n   for (int i = 0; i < k; i++) { n     int tmp; n     cin >> tmp; n     s.insert(tmp); n   } n   for (int i = 1; i <= n; i++) sort(v[i].begin(), v[i].end(), cmp); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j < int(v[i].size()); j++) { n       if (s.count(v[i][j])) { n         ans++; n         s.erase(v[i][j]); n       } n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool f(int k, int n) { n   int s = k; n   while (k > 0) { n     s += k % 10; n     k /= 10; n   } n   return s == n; n } n int main() { n   int i, n; n   vector<int> ans; n   cin >> n; n   for (i = n - 100; i <= n + 100; ++i) { n     if (f(i, n)) { n       ans.push_back(i); n     } n   } n   cout << ans.size() << endl; n   for (i = 0; i < ans.size(); ++i) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 100000 + 10; n const double PI = M_PI; n const double eps = 1e-8; n int dcmp(double a) { n   if (fabs(a) < eps) return 0; n   if (a > 0) return 1; n   return -1; n } n struct Point { n   double x, y; n   Point operator+(const Point &A) { return (Point){A.x + x, A.y + y}; } n   Point operator-(const Point &A) { return (Point){x - A.x, y - A.y}; } n } P, Poly[Maxn]; n int n; n struct Line { n   Point P; n   Point V; n } Line[Maxn]; n double CalDJ(Point A, Point B) { return A.x * B.x + A.y * B.y; } n double CalCJ(Point A, Point B) { return A.x * B.y - A.y * B.x; } n double CalLen(Point A) { return sqrt(CalDJ(A, A)); } n double CalDIS(Point P, Point A, Point B) { n   Point v1 = B - A, v2 = P - A, v3 = P - B; n   if (dcmp(CalDJ(v1, v2)) < 0) return CalLen(v2); n   if (dcmp(CalDJ(v1, v3)) > 0) return CalLen(v3); n   return fabs(CalCJ(v1, v2) / CalLen(v1)); n } n double Getdis(Point Q) { n   long long dx = Q.x - P.x, dy = Q.y - P.y; n   long long ret = dx * dx + dy * dy; n   return ret; n } n double Max, Min; n void solve() { n   scanf( %d , &n); n   scanf( %lf%lf , &P.x, &P.y); n   for (int i = 1; i <= n; i++) scanf( %lf%lf , &Poly[i].x, &Poly[i].y); n   Min = Getdis(Poly[1]); n   for (int i = 1; i <= n; i++) { n     double di = Getdis(Poly[i]); n     if (di > Max) Max = di; n     if (di < Min) Min = di; n   } n   Poly[n + 1] = Poly[1]; n   for (int i = 1; i <= n; i++) { n     double dis = CalDIS(P, Poly[i], Poly[i + 1]); n     dis = dis * dis; n     if (dis < Min) Min = dis; n   } n   double ans = PI * (Max - Min); n   printf( %lf n , ans); n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n int vis[26], cnt[26], h[26]; n set<int> e[26]; n vector<string> S[1005]; n int mp[26]; n bool operator<=(string a, string b) { n   for (int i = 0; i < min(a.size(), b.size()); i++) { n     if (a[i] == b[i]) continue; n     return mp[a[i] -  a ] < mp[b[i] -  a ]; n   } n   return a.size() <= b.size(); n } n int main() { n   ios_base::sync_with_stdio(false); n   string s; n   int n, k, p; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> p; n     for (int i = 1; i <= k; i++) { n       cin >> s, S[p].push_back(s); n       for (char c : s) h[c -  a ] = 1; n     } n   } n   vector<string> v; n   for (int i = 0; i < n; i++) n     for (string s : S[i]) v.push_back(s); n   for (int i = 1; i < v.size(); i++) { n     string &s1 = v[i - 1], &s2 = v[i]; n     for (int i = 0; i < min(s1.size(), s2.size()); i++) { n       if (s1[i] != s2[i]) { n         int c1 = s1[i] -  a , c2 = s2[i] -  a ; n         if (e[c1].find(c2) == e[c1].end()) e[c1].insert(c2), cnt[c2]++; n         break; n       } n     } n   } n   vector<int> q; n   int m = 0; n   for (int i = 0; i < 26; i++) m += (h[i]); n   for (int i = 0; i < 26; i++) n     if (cnt[i] == 0 and h[i]) q.push_back(i); n   for (int k = 0; k < q.size(); k++) { n     int i = q[k]; n     vis[i] = 1; n     for (int j : e[i]) { n       if (vis[j]) continue; n       cnt[j]--; n       if (cnt[j] == 0) q.push_back(j); n     } n   } n   bool ok = 1; n   for (int i = 0; i < q.size(); i++) mp[q[i]] = i; n   for (int i = 1; i < v.size(); i++) ok &= (v[i - 1] <= v[i]); n   if (q.size() != m or !ok) cout <<  IMPOSSIBLE  <<   n , exit(0); n   for (int i : q) cout << (char)(i +  a ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d[6][3] = {{1, 0, 0},  {-1, 0, 0}, {0, 1, 0}, n                {0, -1, 0}, {0, 0, 1},  {0, 0, -1}}; n int main() { n   int k, n, m, sn, sm, ans = 0; n   cin >> k >> n >> m; n   string plate[k][n]; n   bool visited[k][n][m]; n   for (int i = 0; i < k; i++) n     for (int j = 0; j < n; j++) { n       for (int N = 0; N < m; N++) visited[i][j][N] = false; n       cin >> plate[i][j]; n     } n   queue<vector<int> > q; n   cin >> sn >> sm; n   vector<int> init; n   for (int i = 0; i < 3; i++) init.push_back(0); n   int arr[3] = {0, sn - 1, sm - 1}; n   for (int i = 0; i < 3; i++) init[i] = arr[i]; n   q.push(init); n   while (!q.empty()) { n     vector<int> A = q.front(); n     visited[A[0]][A[1]][A[2]] = true; n     ans++; n     q.pop(); n     for (int i = 0; i < 6; i++) { n       vector<int> B(A); n       for (int j = 0; j < 3; j++) B[j] += d[i][j]; n       if (B[0] >= 0 && B[0] < k && B[1] >= 0 && B[1] < n && B[2] >= 0 && n           B[2] < m && plate[B[0]][B[1]][B[2]] ==  .  && n           !visited[B[0]][B[1]][B[2]]) { n         q.push(B); n         visited[B[0]][B[1]][B[2]] = true; n       } n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a, b; n int main() { n   int t; n   cin >> t; n   while (t--) { n     cin >> n >> a >> b; n     if (abs(a - b) > 1 || a + b > n - 2) n       cout << -1; n     else { n       int l, r, x; n       x = a + b; n       if (a == b) { n         l = n, r = n - x; n         while (r < l) { n           cout << l <<     << r <<    ; n           l--, r++; n         } n         if (r == l) cout << r <<    ; n         for (int i = n - x - 1; i >= 1; i--) cout << i <<    ; n       } else if (a > b) { n         l = n, r = n - x; n         while (r < l) { n           cout << r <<     << l <<    ; n           r++, l--; n         } n         if (r == l) cout << r <<    ; n         for (int i = n - x - 1; i >= 1; i--) cout << i <<    ; n       } else { n         r = 1, l = x + 1; n         while (r < l) { n           cout << l <<     << r <<    ; n           l--, r++; n         } n         if (r == l) cout << r <<    ; n         for (int i = x + 2; i <= n; i++) cout << i <<    ; n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1234567; n int query(vector<long long> a) { n   int n = a.size(); n   for (int i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       if (n % (i * i) != 0) { n         for (int j = 0; j < n; j += i) { n           long long d = a[j]; n           int k = j; n           do { n             a[k] -= d; n             k = (k + n / i) % n; n           } while (k != j); n         } n       } n       vector<vector<long long> > w(i); n       for (int j = 0; j < n; j++) w[j % i].push_back(a[j]); n       bool f = 1; n       for (int j = 0; j < i; j++) n         if (!query(w[j])) { n           f = 0; n           break; n         } n       return f; n     } n   } n   for (int i = 1; i < a.size(); i++) n     if (a[i] != a[0]) return 0; n   return 1; n } n int main() { n   int n; n   string s; n   cin >> n >> s; n   vector<long long> a(n); n   for (int i = 0; i < n; i++) a[i] = s[i] -  0 ; n   cout << (query(a) ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 500010; n int k, n, maxb, t, b[N], tot = 1, dp[N << 2], rec[N << 2]; n void update(int i, int x) { n   while (i <= tot) { n     dp[i] = max(dp[i], x); n     i += i & -i; n   } n } n int sum(int i) { n   int ret = 0; n   while (i) { n     ret = max(ret, dp[i]); n     i -= i & -i; n   } n   return ret; n } n int main() { n   scanf( %d%d%d%d , &k, &n, &maxb, &t); n   t = min(t, maxb); n   while (tot <= maxb) tot <<= 1; n   while (k--) { n     memset(dp, 0, sizeof(dp)); n     memset(rec, 0, sizeof(rec)); n     memset(b, 0, sizeof(b)); n     for (int i = 0; i < n; i++) scanf( %d , &b[i]); n     for (int i = 1; i <= t; i++) n       for (int j = 0; j < n; j++) { n         int tmp = sum(b[j] - 1); n         if (tmp >= rec[j]) { n           update(b[j], tmp + 1); n           rec[j] = tmp + 1; n         } n       } n     printf( %d n , sum(tot)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long MAX_N = 1e3 + 1; n long n, m, h; n long aw[MAX_N]; n long bw[MAX_N]; n vector<long> g[MAX_N]; n bool v[MAX_N]; n vector<long> st; n vector<pair<pair<long, long>, long> > ans; n void add_edge(long a, long b) { n   g[a].push_back(b); n   g[b].push_back(a); n } n void try_flow(long i, long need) { n   if (i + 1 >= st.size()) return; n   long a = st[i], b = st[i + 1]; n   long can = min(need, h - aw[b]); n   if (can > 0) { n     ans.push_back(make_pair(make_pair(a, b), can)); n   } n   try_flow(i + 1, need); n   if (need - can > 0) { n     ans.push_back(make_pair(make_pair(a, b), need - can)); n   } n } n void flow() { n   long a = st[0]; n   long b = st[st.size() - 1]; n   long need = min(aw[a] - bw[a], bw[b] - aw[b]); n   if (need > 0) { n     try_flow(0, need); n     aw[a] -= need; n     aw[b] += need; n   } n } n void dfs(long a) { n   v[a] = true; n   st.push_back(a); n   if (aw[a] < bw[a]) flow(); n   for (size_t i = 0; i < g[a].size(); i++) { n     long b = g[a][i]; n     if (!v[b]) dfs(b); n   } n   st.pop_back(); n } n int main() { n   cin >> n >> h >> m; n   for (long i = 1; i <= n; i++) { n     cin >> aw[i]; n   } n   for (long i = 1; i <= n; i++) { n     cin >> bw[i]; n   } n   for (long i = 1; i <= m; i++) { n     long x, y; n     cin >> x >> y; n     add_edge(x, y); n   } n   for (long i = 1; i <= n; i++) { n     if (aw[i] > bw[i]) { n       for (long j = 1; j <= n; j++) v[j] = false; n       dfs(i); n     } n   } n   for (long i = 1; i <= n; i++) { n     if (aw[i] != bw[i]) { n       cout <<  NO ; n       return 0; n     } n   } n   cout << ans.size() << endl; n   for (size_t i = 0; i < ans.size(); i++) { n     cout << ans[i].first.first <<     << ans[i].first.second <<     n          << ans[i].second << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   getline(cin, str); n   str.erase(remove(str.begin(), str.end(),    ), str.end()); n   int n = str.size(); n   if (str[n - 2] ==  a  || str[n - 2] ==  e  || str[n - 2] ==  i  || n       str[n - 2] ==  o  || str[n - 2] ==  u  || str[n - 2] ==  y  || n       str[n - 2] ==  A  || str[n - 2] ==  E  || str[n - 2] ==  I  || n       str[n - 2] ==  O  || str[n - 2] ==  U  || str[n - 2] ==  Y ) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int main() { n   cin >> s; n   long long mid; n   for (long long i = 0; i < s.size(); i++) { n     if (s[i] ==  ^ ) { n       mid = i; n       break; n     } n   } n   long long sum = 0; n   for (long long i = 0; i < s.size(); i++) { n     if (s[i] ==  =  || s[i] ==  ^ ) continue; n     long long x = s[i] - 48; n     sum += x * (i - mid); n   } n   if (sum == 0) n     cout <<  balance ; n   else if (sum < 0) n     cout <<  left ; n   else n     cout <<  right ; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = int(1e9) + 1; n const long double eps = 1e-9; n map<string, int> jogos; n map<string, int> pontos; n map<string, int> gol; n map<string, int> leva; n set<string> times; n vector<string> falta; n int x1, x2; n bool menor(string x, string y) { n   int g1 = 0, g2 = 0; n   int l1 = 0, l2 = 0; n   if (falta[0] == x) { n     g1 += x1; n     l1 += x2; n   } else if (falta[0] == y) { n     g2 += x1; n     l2 += x2; n   } n   if (falta[1] == x) { n     g1 += x2; n     l1 += x1; n   } else if (falta[1] == y) { n     g2 += x2; n     l2 += x1; n   } n   g1 += gol[x]; n   l1 += leva[x]; n   g2 += gol[y]; n   l2 += leva[y]; n   if (pontos[x] > pontos[y]) n     return true; n   else if (pontos[x] < pontos[y]) n     return false; n   else { n     if (g1 - l1 > g2 - l2) n       return true; n     else if (g1 - l1 < g2 - l2) n       return false; n     else { n       if (g1 > g2) n         return true; n       else if (g1 < g2) n         return false; n       return y > x; n     } n   } n } n bool ord(int p1, int p2) { n   x1 = p1; n   x2 = p2; n   vector<string> pi; n   for (set<string>::iterator it = times.begin(); it != times.end(); ++it) { n     string aux = *it; n     pi.push_back((aux)); n   } n   for (int i = 3; i >= 1; --i) { n     for (int j = 0; j < i; j++) { n       if (!menor(pi[j], pi[j + 1])) { n         swap(pi[j], pi[j + 1]); n       } n     } n   } n   if (pi[0] ==  BERLAND  || pi[1] ==  BERLAND ) return true; n   return false; n } n int main() { n   for (int i = 0; i < 5; ++i) { n     string x, y; n     int g1, g2; n     cin >> x >> y; n     times.insert(x); n     times.insert(y); n     scanf( %d:%d , &g1, &g2); n     jogos[x]++; n     jogos[y]++; n     if (g1 == g2) { n       pontos[x]++; n       pontos[y]++; n     } else if (g1 > g2) { n       pontos[x] += 3; n     } else { n       pontos[y] += 3; n     } n     gol[x] += g1; n     leva[x] += g2; n     gol[y] += g2; n     leva[y] += g1; n   } n   for (set<string>::iterator it = times.begin(); it != times.end(); ++it) { n     if (jogos[*it] < 3) falta.push_back(*it); n   } n   if (falta[1] ==  BERLAND ) swap(falta[0], falta[1]); n   pontos[falta[0]] += 3; n   int ans1 = 1e8, ans2 = 1e7; n   for (int g1 = 0; g1 < 201; ++g1) { n     for (int g2 = 0; g2 < g1; ++g2) { n       bool can = ord(g1, g2); n       if (can) { n         if (ans1 - ans2 > g1 - g2) { n           ans1 = g1; n           ans2 = g2; n         } n       } n     } n   } n   if (ans1 >= 1e8) n     puts( IMPOSSIBLE ); n   else n     printf( %d:%d n , ans1, ans2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t, a, b, c; n   cin >> t; n   while (t--) { n     cin >> a >> b >> c; n     cout << ((a + b * 2 + c * 3) & 1) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void swap(long long int arr[], long long int x, long long int y) { n   long long int temp = arr[x]; n   arr[x] = arr[y]; n   arr[y] = temp; n } n void print_arr(long long int arr[], long long int n) { n   cout <<   n ; n   for (long long int i = 0; i < n; i++) { n     cout << arr[i] <<    ; n   } n   cout <<   n ; n } n void input_arr(long long int arr[], long long int n) { n   for (long long int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n } n bool primes[1000001]; n int checkstring(string s) { n   int l = s.length(); n   string p =   ; n   for (int i = 0; i < l; i++) { n     if (s[i] !=  - ) { n       p += s[i]; n     } n   } n   int same = 0; n   int dec = 0; n   for (int i = 1; i < p.length(); i++) { n     if (p[i] == p[i - 1]) { n       same++; n     } else if (p[i] <= p[i - 1]) { n       dec++; n     } n   } n   if (same == p.length() - 1) { n     return 2; n   } else if (dec == p.length() - 1) { n     return 1; n   } n   return 3; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   t = 1; n   while (t--) { n     long long int n; n     cin >> n; n     map<string, long long int> tx; n     map<string, long long int> cg; n     map<string, long long int> pz; n     string abc[n]; n     for (int i = 0; i < n; i++) { n       int N; n       string fname; n       cin >> N >> fname; n       abc[i] = fname; n       for (int j = 0; j < N; j++) { n         string s; n         cin >> s; n         int x = checkstring(s); n         if (x == 2) { n           tx[fname]++; n         } else if (x == 3) { n           cg[fname]++; n         } else { n           pz[fname]++; n         } n       } n     } n     int maxp = 0, maxt = 0, maxc = 0; n     vector<string> v[3]; n     for (int i = 0; i < n; i++) { n       if (tx[abc[i]] > maxt) { n         maxt = tx[abc[i]]; n       } n       if (pz[abc[i]] > maxp) { n         maxp = pz[abc[i]]; n       } n       if (cg[abc[i]] > maxc) { n         maxc = cg[abc[i]]; n       } n     } n     for (int i = 0; i < n; i++) { n       if (pz[abc[i]] == maxp) { n         v[0].push_back(abc[i]); n       } n       if (cg[abc[i]] == maxc) { n         v[1].push_back(abc[i]); n       } n       if (tx[abc[i]] == maxt) { n         v[2].push_back(abc[i]); n       } n     } n     cout <<  If you want to call a taxi, you should call: ; n     for (int i = 0; i < v[2].size(); i++) { n       if (i != v[2].size() - 1) n         cout <<     << v[2][i] <<  , ; n       else n         cout <<     << v[2][i] <<  . ; n     } n     cout <<   n ; n     cout <<  If you want to order a pizza, you should call: ; n     for (int i = 0; i < v[0].size(); i++) { n       if (i != v[0].size() - 1) n         cout <<     << v[0][i] <<  , ; n       else n         cout <<     << v[0][i] <<  . ; n     } n     cout <<   n ; n     cout <<  If you want to go to a cafe with a wonderful girl, you should   n              call: ; n     for (int i = 0; i < v[1].size(); i++) { n       if (i != v[1].size() - 1) n         cout <<     << v[1][i] <<  , ; n       else n         cout <<     << v[1][i] <<  . ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int digitMask = 0; n int c[20]; n int factorize(int n, int pos) { n   int curPos = pos; n   c[curPos] = 0; n   while (n > 0) c[curPos++] = n % 10, n /= 10; n   curPos--; n   curPos = max(curPos, pos); n   for (int i = pos, j = curPos; i < j; i++, j--) swap(c[i], c[j]); n   return curPos; n } n int parsenum(int n) { n   if (c[n - 1] == 0 && n != 1) return -1; n   int ans = 0; n   for (int i = n - 1; i >= 0; i--) ans *= 10, ans += c[i]; n   return ans; n } n bool good(int i, int j) { n   int ii = i, jj = j; n   while (i < j) n     if (c[i] != c[j]) n       return false; n     else n       i++, j--; n   int mask = 0; n   for (int we = ii; we <= jj; we++) mask |= (1 << c[we]); n   return mask == digitMask; n } n bool nogood(int i, int j) { n   for (int we = i; we <= j; we++) n     if ((digitMask & (1 << c[we])) == 0) return true; n   return false; n } n const int maxn = 1000000; n int ans1[maxn], ans2[maxn], ans3[maxn], ans4[maxn]; n int ansCount; n int main() { n   int n; n   cin >> n; n   if (n >= 7) { n     cout << 0; n     return 0; n   } n   for (int i = 0; i < n; i++) { n     int c; n     cin >> c; n     digitMask |= (1 << c); n   } n   for (int a = 0; a < 256; a++) { n     int pos1 = factorize(a, 0); n     for (int b = 0; b < 256; b++) { n       int pos2 = factorize(b, pos1 + 1); n       if (nogood(0, pos2)) continue; n       for (int c = 0; c < 256; c++) { n         int pos3 = factorize(c, pos2 + 1); n         if (nogood(0, pos3)) continue; n         for (int count = 1; count <= 3; count++) { n           int last = parsenum(count); n           if (last == -1) continue; n           if (last >= 256) continue; n           int pos = factorize(last, pos3 + 1); n           if (good(0, pos)) { n             ans1[ansCount] = a; n             ans2[ansCount] = b; n             ans3[ansCount] = c; n             ans4[ansCount] = last; n             ansCount++; n           } n         } n       } n     } n   } n   cout << ansCount << endl; n   for (int i = 0; i < ansCount; i++) { n     cout << ans1[i] <<  .  << ans2[i] <<  .  << ans3[i] <<  .  << ans4[i] n          << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long modn = 1000000007; n inline long long mod(long long x) { return x % modn; } n int n; n vector<int> v[30]; n int qt[30]; n int main() { n   int i, j; n   string s; n   cin >> s; n   n = s.size(); n   for (i = 0; i < n; i++) v[s[i] -  a ].push_back(i); n   double ans = 0; n   for (i = 0; i < 26; i++) { n     if (v[i].empty()) continue; n     if (v[i].size() == 1) { n       ans += 1; n       continue; n     } n     if (v[i].size() == 2) { n       string t1 = s, t2 = s; n       rotate(t1.begin(), t1.begin() + v[i][0], t1.end()); n       rotate(t2.begin(), t2.begin() + v[i][1], t2.end()); n       if (t1 != t2) ans += 2; n       continue; n     } n     int mx = 0; n     for (j = 0; j < n; j++) { n       for (int k : v[i]) qt[s[(k + j) % n] -  a ]++; n       int cur = 0; n       int k; n       for (k = 0; k < 26; k++) n         if (qt[k] == 1) cur++; n       mx = max(mx, cur); n       for (int k : v[i]) qt[s[(k + j) % n] -  a ]--; n     } n     ans += mx; n   } n   printf( %.15f n , ans / n); n } n


#include <bits/stdc++.h> n using namespace std; n template <class temp> n inline void Rd(temp &p) { n   p = 0; n   char c; n   while (c = getchar(), c < 48) n     ; n   do p = p * 10 + (c & 15); n   while (c = getchar(), c > 47); n } n int n, k, len, m, q[200005]; n int l[200005], r[200005]; n int lfind(int x) { n   if (l[x] == x) n     return x; n   else n     return l[x] = lfind(l[x]); n } n int rfind(int x) { n   if (r[x] == x) n     return x; n   else n     return r[x] = rfind(r[x]); n } n int main() { n   Rd(n), Rd(k), Rd(len), Rd(m); n   for (int i = 1; i <= n; i++) l[i] = i - 1, r[i] = i + 1; n   l[0] = r[0] = 0; n   l[n + 1] = r[n + 1] = n + 1; n   for (int i = 1; i <= m; i++) { n     Rd(q[i]); n     l[q[i]] = r[q[i]] = q[i]; n   } n   int pre = 0, ans = 0; n   for (int i = 1; i <= n + 1; i++) n     if (lfind(i) == rfind(i)) { n       ans += (i - pre) / (len + 1); n       pre = i; n     } n   if (ans >= k) puts( -1 ), exit(0); n   for (int i = m; i >= 1; i--) { n     int pos = q[i]; n     ans -= (rfind(r[pos - 1]) - lfind(l[pos - 1])) / (len + 1); n     ans -= (rfind(r[pos + 1]) - lfind(l[pos + 1])) / (len + 1); n     l[pos] = pos - 1, r[pos] = pos + 1; n     ans += (rfind(r[pos]) - lfind(l[pos])) / (len + 1); n     if (ans >= k) printf( %d n , i), exit(0); n   } n } n


#include <bits/stdc++.h> n double vp, vd, t, f, c; n int main() { n   scanf( %lf%lf%lf%lf%lf , &vp, &vd, &t, &f, &c); n   double x = vp * t; n   int ans = 0; n   if (vp > vd) { n     puts( 0 ); n     return 0; n   } n   while (x + 1e-6 < c) { n     double needtime = x / (vd - vp); n     double willx = x + needtime * vp; n     if (willx + 1e-6 >= c) break; n     ans++; n     t += needtime * 2 + f; n     x = vp * t; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5; n long long n, m; n map<long long, long long> mp; n long long ans[N], top; n signed main() { n   cin >> n >> m; n   for (long long i = 1; i <= n; ++i) { n     long long x; n     cin >> x; n     mp[x] = 1; n   } n   for (long long i = 1; 26 > 30 / 39 / 57; ++i) { n     if (mp[i] == 0) { n       m -= i; n       if (m < 0) { n         break; n       } n       ans[++top] = i; n     } n   } n   cout << top << endl; n   for (long long i = 1; i <= top; ++i) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } n int main() { n   long long t, i, r, c, k, j, n, cnt, x, y, p, q, l, flag; n   vector<string> a; n   vector<char> b; n   for (i = 48; i < 58; i++) { n     b.push_back(char(i)); n   } n   for (i = 65; i < 91; i++) { n     b.push_back(char(i)); n   } n   for (i = 97; i < 124; i++) { n     b.push_back(char(i)); n   } n   string s; n   cin >> t; n   for (i = 0; i < t; i++) { n     cin >> r >> c >> n; n     cnt = 0; n     p = 0, q = 0, l = 0, flag = 1; n     for (j = 0; j < r; j++) { n       cin >> s; n       for (k = 0; k < c; k++) { n         if (s[k] ==  R ) { n           cnt++; n         } n       } n       a.push_back(s); n     } n     if (cnt % n == 0) { n       x = cnt / n; n       y = n; n     } else { n       x = cnt / n + 1; n       y = cnt % n; n     } n     for (j = 0; j < r; j++) { n       if (j % 2 == 0) { n         for (k = 0; k < c; k++) { n           if (a[j][k] ==  R ) { n             if (q == x) { n               p++; n               if (p == y && flag == 1) { n                 x = x - 1; n                 flag = 2; n               } n               q = 1; n               l++; n               a[j][k] = b[l]; n             } else { n               q++; n               a[j][k] = b[l]; n             } n           } else { n             a[j][k] = b[l]; n           } n         } n       } else { n         for (k = c - 1; k >= 0; k--) { n           if (a[j][k] ==  R ) { n             if (q == x) { n               p++; n               if (p == y && flag == 1) { n                 x = x - 1; n                 flag = 2; n               } n               q = 1; n               l++; n               a[j][k] = b[l]; n             } else { n               q++; n               a[j][k] = b[l]; n             } n           } else { n             a[j][k] = b[l]; n           } n         } n       } n     } n     for (j = 0; j < r; j++) { n       cout << a[j] << endl; n     } n     a.clear(); n   } n } n


#include <bits/stdc++.h> n int in[101], od[101], fa[101], n; n int f[101], f2[102]; n inline int find(int a) { n   if (fa[a] != a) fa[a] = find(fa[a]); n   return fa[a]; n } n inline void u(int x, int y) { n   x = find(x); n   y = find(y); n   fa[y] = x; n } n signed main() { n   scanf( %d , &n); n   for (register int i = 1; i <= n; i++) fa[i] = i; n   for (register int i = 1; i <= n; i++) scanf( %d , &in[i]); n   for (register int i = 1; i <= n; i++) { n     scanf( %d , &od[i]); n     if (i - od[i] > 0) u(in[i], in[i - od[i]]); n     if (i + od[i] <= n) u(in[i], in[i + od[i]]); n   } n   for (register int i = 1; i <= n; i++) { n     f[find(i)] += i; n     f2[find(i)] += in[i]; n   } n   for (register int i = 1; i <= n; i++) { n     if (f[i] ^ f2[i]) { n       printf( NO n ); n       return 0; n     } n   } n   printf( YES n ); n } n