#include <bits/stdc++.h> n using namespace std; n const int MxN = (int)15e4 + 9; n int n, Q; n int parent[MxN + 9]; n vector<int> adj[MxN + 9]; n int height[MxN + 9]; n char label[MxN + 9]; n const string YES =  Shi ; n const string NO =  Fou ; n int parent2[MxN + 9]; n vector<int> adj2[MxN + 9]; n array<int, 26> label2[MxN + 9]; n array<int, 26> dp[MxN + 9]; n int dpSum[MxN + 9]; n bool valid[MxN + 9]; n void noAll() { n   for (int i = 0; i < Q; ++i) cout << NO <<   n ; n   exit(0); n } n bool spe(const int u) { return u == 1 || 0 == (adj[u].size() & 1); } n inline void update(const int u, const int j) { n   assert(0 <= j && j < 26); n   assert(spe(u)); n   valid[u] = true; n   if (adj[u].empty()) return; n   dpSum[u] -= dp[u][j]; n   dp[u][j] = 0; n   for (const int v : adj2[u]) { n     valid[u] &= valid[v]; n     dp[u][j] = max(dp[u][j], dp[v][j] + label2[v][j]); n   } n   dpSum[u] += dp[u][j]; n   valid[u] &= dpSum[u] <= height[u]; n } n inline void chainUpdate(int u, const int j) { n   do { n     update(u, j); n     u = parent2[u]; n   } while (u); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr), cout.tie(nullptr); n   cin >> n >> Q; n   for (int i = 2; i <= n; ++i) { n     cin >> parent[i] >> label[i]; n     adj[parent[i]].push_back(i); n     assert(adj[parent[i]].size() <= 2); n   } n   for (int i = n; i >= 1; --i) { n     if (adj[i].empty()) continue; n     const int u = adj[i][0]; n     if (adj[i].size() == 2) { n       const int v = adj[i][1]; n       if (height[u] != height[v]) noAll(); n     } n     height[i] = height[u] + 1; n   } n   for (int i = 2; i <= n; ++i) { n     if (spe(parent[i])) { n       parent2[i] = parent[i]; n     } else { n       parent2[i] = parent2[parent[i]]; n       label2[i] = label2[parent[i]]; n     } n     if (label[i] !=  ? ) label2[i][label[i] -  a ]++; n   } n   for (int i = n; i >= 1; --i) { n     for (int x : adj[i]) { n       const int y = spe(x) ? x : adj2[x][0]; n       adj2[i].push_back(y); n     } n   } n   for (int i = n; i >= 1; --i) n     if (spe(i)) n       for (int j = 0; j < 26; ++j) update(i, j); n   for (int rep = 0;; ++rep) { n     ostream &out = rep == 0 ? cerr : cout; n     if (!valid[1]) n       out << NO <<   n ; n     else { n       long long ans = 0; n       for (int j = 0; j < 26; ++j) n         ans += (j + 1LL) * (dp[1][j] + (height[1] - dpSum[1])); n       out << YES <<     << ans <<   n ; n     } n     if (rep == Q) break; n     int u; n     char ch; n     cin >> u >> ch; n     const int v = spe(u) ? u : adj2[u][0]; n     if (label[u] !=  ? ) { n       const int j = label[u] -  a ; n       label2[v][j]--; n       chainUpdate(v, j); n     } n     if (ch !=  ? ) { n       const int j = ch -  a ; n       label2[v][j]++; n       chainUpdate(v, j); n     } n     label[u] = ch; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n pair<int, int> rev[1000005]; n int h[1000005]; n int d[1000005]; n vector<int> g[1000005]; n int N, K; n void pre_dfs(int u, int p = -1) { n   rev[u] = pair<int, int>(1000005, 0); n   for (int v : g[u]) { n     if (v == p) continue; n     h[v] = h[u] + 1; n     pre_dfs(v, u); n     if (rev[v].first <= h[u]) { n       rev[u].first = min(rev[u].first, rev[v].first); n       rev[u].second += rev[v].second; n     } n   } n   if (p >= 0 && g[u].size() == 1) { n     rev[u].first = h[u] - K; n     rev[u].second = 1; n   } n } n void sol(int u, int p = -1) { n   d[u] = p >= 0 && g[u].size() == 1; n   for (int v : g[u]) { n     if (v == p) continue; n     sol(v, u); n     int temp = rev[u].second; n     if (rev[v].first <= h[u]) temp -= rev[v].second; n     d[u] = max(d[u], temp + d[v]); n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> N >> K; n   for (int i = 1; i <= N - 1; i++) { n     int u; n     cin >> u; n     u--; n     g[i].push_back(u); n     g[u].push_back(i); n   } n   h[0] = 0; n   pre_dfs(0); n   sol(0); n   cout << d[0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1000 * 100 + 10; n int par[N], n, m, c; n unordered_map<int, int> adj[N]; n unordered_set<int> adj1[N]; n int getPar(int root) { n   return par[root] == par[par[root]] ? par[root] n                                      : par[root] = getPar(par[root]); n } n void Union(int v, int u) { n   v = getPar(v), u = getPar(u); n   if (v == u) return; n   if (adj1[v].size() < adj1[u].size()) swap(v, u); n   par[u] = par[v]; n   for (auto it : adj1[u]) adj1[v].insert(it); n } n void add(int v, int u, int c) { n   if (adj[u][c]) Union(v, adj[u][c]); n   if (adj[v][c]) Union(u, adj[v][c]); n   int pv = getPar(v), pu = getPar(u); n   adj[v][c] = pu, adj[u][c] = pv; n   adj1[pv].insert(u), adj1[pu].insert(v); n } n bool check(int v, int u) { n   int pv = getPar(v), pu = getPar(u); n   return pv == pu || adj1[pv].count(u); n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   int Q; n   cin >> n >> m >> c >> Q; n   for (int i = 1; i <= n; i++) par[i] = i; n   for (int i = 1, v, u, z; i <= m; i++) { n     cin >> v >> u >> z; n     add(v, u, z); n   } n   while (Q--) { n     char c; n     int x, y, z; n     cin >> c >> x >> y; n     if (c ==  ? ) n       cout << (check(x, y) ?  Yes  :  No ) <<   n ; n     else { n       cin >> z; n       add(x, y, z); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n using V = vector<T>; n using ll = long long; n using dbl = double; n using ii = pair<int, int>; n const ll MOD = 998244353; n ll z1[2000005], z2[2000005], dp[1000005], psum[1000005]; n void solveZ(string s, ll z[]) { n   z[0] = 0; n   ll L = 0, R = 0, n = s.size(); n   for (ll i = 1; i < n; i++) { n     if (i > R) { n       L = R = i; n       while (R < n && s[R - L] == s[R]) R++; n       z[i] = R - L; n       R--; n     } else { n       ll k = i - L; n       if (z[k] < R - i + 1) n         z[i] = z[k]; n       else { n         L = i; n         while (R < n && s[R - L] == s[R]) R++; n         z[i] = R - L; n         R--; n       } n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   string a, l, r; n   cin >> a >> l >> r; n   string la = l +  #  + a; n   string ra = r +  #  + a; n   solveZ(la, z1); n   solveZ(ra, z2); n   ll lsize = l.size(); n   ll rsize = r.size(); n   ll ls = 0, rs = 0; n   for (int i = 0; i < (int)a.size(); i++) { n     if (i - ls + 1 < lsize) { n       dp[i] = 0; n       continue; n     } n     while (i - ls + 1 != lsize) ls++; n     while (i - rs + 1 > rsize) rs++; n     if (i - rs + 1 == rsize) { n       ll matched = z2[rs + rsize + 1]; n       if (matched != rsize && a[rs + matched] > r[matched]) rs++; n     } n     if (i - ls + 1 == lsize) { n       ll matched = z1[ls + lsize + 1]; n       if (matched != lsize && a[ls + matched] < l[matched]) { n         if (ls == 0) n           continue; n         else n           ls--; n       } n     } n     if (rs <= ls) { n       if (ls - 1 < 0) n         dp[i] = 1; n       else if (rs - 1 < 0) n         dp[i] = (dp[i] + psum[ls - 1] + 1) % MOD; n       else n         dp[i] = (psum[ls - 1] - (rs - 2 < 0 ? 0 : psum[rs - 2]) + MOD) % MOD; n       if (a[i] ==  0  && ls == i) dp[i] = (dp[i] + dp[i - 1]) % MOD; n       if (i != a.size() - 1 && a[i + 1] !=  0 ) n         psum[i] = (psum[i] + (i == 0 ? 0 : psum[i - 1]) + dp[i]) % MOD; n       else if (i != 0) n         psum[i] = psum[i - 1]; n       else n         psum[i] = 0; n     } n   } n   cout << dp[a.size() - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string st; n int dp[10005][4]; n int n; n set<string> st_set; n int rec(int pos, int len) { n   if (pos == 4 || pos == 5) { n     return 1; n   } n   if (pos < 4) return 0; n   int &ret = dp[pos][len]; n   if (ret != -1) return ret; n   ret = 0; n   if (len == 0) { n     if (pos >= 7) { n       ret |= rec(pos - 3, 3); n       st_set.insert(st.substr(pos - 2, 3)); n     } n     if (pos >= 6) { n       ret |= rec(pos - 2, 2); n       st_set.insert(st.substr(pos - 1, 2)); n     } n   } else if (len == 2) { n     if (pos >= 6 && !(st[pos] == st[pos + 2] && st[pos + 1] == st[pos - 1])) { n       ret |= rec(pos - 2, 2); n       st_set.insert(st.substr(pos - 1, 2)); n     } n     if (pos >= 7) { n       ret |= rec(pos - 3, 3); n       st_set.insert(st.substr(pos - 2, 3)); n     } n   } else { n     if (pos >= 7 && !(st[pos] == st[pos + 3] && st[pos - 1] == st[pos + 2] && n                       st[pos - 2] == st[pos + 1])) { n       ret |= rec(pos - 3, 3); n       st_set.insert(st.substr(pos - 2, 3)); n     } n     if (pos >= 6) { n       ret |= rec(pos - 2, 2); n       st_set.insert(st.substr(pos - 1, 2)); n     } n   } n   return ret; n } n int main() { n   cin >> st; n   n = st.length(); n   memset(dp, -1, sizeof dp); n   rec(n - 1, 0); n   cout << st_set.size() << endl; n   for (string x : st_set) cout << x << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n namespace io { n const int SIZE = (1 << 21) + 1; n char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, n                                        qu[55]; n int f, qr; n inline void flush() { n   fwrite(obuf, 1, oS - obuf, stdout); n   oS = obuf; n } n inline void putc(char x) { n   *oS++ = x; n   if (oS == oT) flush(); n } n template <class I> n inline void read(I &x) { n   for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                               (iS == iT ? EOF : *iS++)) n                             : *iS++); n        c <  0  || c >  9 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     if (c ==  - ) f = -1; n   for (x = 0; c <=  9  && c >=  0 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     x = x * 10 + (c & 15); n   x *= f; n } n template <class I> n inline void print(I &x) { n   if (!x) putc( 0 ); n   if (x < 0) putc( - ), x = -x; n   while (x) qu[++qr] = x % 10 +  0 , x /= 10; n   while (qr) putc(qu[qr--]); n } n struct Flusher_ { n   ~Flusher_() { flush(); } n } io_flusher_; n }  // namespace io n using io ::print; n using io ::putc; n using io ::read; n struct node { n   int nxt; n   long long cnt; n } s[100005]; n int a[15][100005]; n int main() { n   int i, j, k; n   int n, m; n   read(n), read(m); n   for (i = 1; i <= m; i++) n     for (j = 1; j <= n; j++) read(a[i][j]); n   for (i = 1; i < n; i++) { n     s[a[1][i]].nxt = a[1][i + 1]; n     s[a[1][i]].cnt = 1; n   } n   for (int i = (int)2; i <= (int)m; i++) n     for (int j = (int)1; j <= (int)n - 1; j++) n       if (s[a[i][j]].nxt == a[i][j + 1]) s[a[i][j]].cnt++; n   long long sum = 1, ans = 0; n   for (int i = (int)1; i <= (int)n - 1; i++) { n     if (s[a[1][i]].cnt != m) n       ans += sum * (sum + 1) / 2, sum = 1; n     else n       sum++; n   } n   if (sum > 0) ans += sum * (sum + 1) / 2; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool cmp1(vector<int> a, vector<int> b) { n   int q = a[4] + a[1] + a[2] + a[3] - a[0]; n   int p = b[4] + b[1] + b[2] + b[3] - b[0]; n   return q < p; n } n bool cmp2(vector<int> a, vector<int> b) { n   int q = a[0] + a[4] + a[2] + a[3] - a[1]; n   int p = b[0] + b[4] + b[2] + b[3] - b[1]; n   return q < p; n } n bool cmp3(vector<int> a, vector<int> b) { n   int q = a[0] + a[1] + a[4] + a[3] - a[2]; n   int p = b[0] + b[1] + b[4] + b[3] - b[2]; n   return q < p; n } n bool cmp4(vector<int> a, vector<int> b) { n   int q = a[0] + a[1] + a[2] + a[4] - a[3]; n   int p = b[0] + b[1] + b[2] + b[4] - b[3]; n   return q < p; n } n bool cmp5(vector<int> a, vector<int> b) { n   int q = a[0] + a[1] + a[2] + a[3] - a[4]; n   int p = b[0] + b[1] + b[2] + b[3] - b[4]; n   return q < p; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int T = 1; n   cin >> T; n   while (T--) { n     int n; n     cin >> n; n     vector<vector<int>> fr(n, vector<int>(5, 0)); n     string s[n]; n     for (int i = 0; i < n; i++) { n       cin >> s[i]; n       for (int j = 0; j < s[i].size(); j++) { n         if (s[i][j] ==  a ) fr[i][0]++; n         if (s[i][j] ==  b ) fr[i][1]++; n         if (s[i][j] ==  c ) fr[i][2]++; n         if (s[i][j] ==  d ) fr[i][3]++; n         if (s[i][j] ==  e ) fr[i][4]++; n       } n     } n     int ans = 0, cw = 0; n     sort(fr.begin(), fr.end(), cmp1); n     int cs = 0, co = 0; n     for (int i = 0; i < n; i++) { n       cs = cs + fr[i][0]; n       co = co + fr[i][1] + fr[i][2] + fr[i][3] + fr[i][4]; n       if (cs > co) cw++; n     } n     ans = max(ans, cw); n     cw = 0; n     cs = 0, co = 0; n     sort(fr.begin(), fr.end(), cmp2); n     for (int i = 0; i < n; i++) { n       cs = cs + fr[i][1]; n       co = co + fr[i][0] + fr[i][2] + fr[i][3] + fr[i][4]; n       if (cs > co) cw++; n     } n     ans = max(ans, cw); n     cw = 0; n     cs = 0, co = 0; n     sort(fr.begin(), fr.end(), cmp3); n     for (int i = 0; i < n; i++) { n       cs = cs + fr[i][2]; n       co = co + fr[i][1] + fr[i][0] + fr[i][3] + fr[i][4]; n       if (cs > co) cw++; n     } n     ans = max(ans, cw); n     cw = 0; n     cs = 0, co = 0; n     sort(fr.begin(), fr.end(), cmp4); n     for (int i = 0; i < n; i++) { n       cs = cs + fr[i][3]; n       co = co + fr[i][1] + fr[i][2] + fr[i][0] + fr[i][4]; n       if (cs > co) cw++; n     } n     ans = max(ans, cw); n     cw = 0; n     cs = 0, co = 0; n     sort(fr.begin(), fr.end(), cmp5); n     for (int i = 0; i < n; i++) { n       cs = cs + fr[i][4]; n       co = co + fr[i][1] + fr[i][2] + fr[i][3] + fr[i][0]; n       if (cs > co) cw++; n     } n     ans = max(ans, cw); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long freq[100001]; n int dp[101]; n int main() { n   int h, a, p; n   cin >> h >> a >> p; n   int mh, ma; n   cin >> mh >> ma; n   vector<int> s; n   while (h - ma > 0 || mh <= a) { n     h -= ma; n     mh -= a; n     s.push_back(1); n     if (mh <= 0) break; n   } n   while (mh > 0) { n     s.push_back(0); n     h += p - ma; n     while (h - ma > 0 || mh <= a) { n       h -= ma; n       mh -= a; n       s.push_back(1); n       if (mh <= 0) break; n     } n   } n   cout << s.size() << endl; n   for (int i = 0; i < s.size(); i++) { n     if (s[i]) n       cout <<  STRIKE  << endl; n     else n       cout <<  HEAL  << endl; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n typedef vector<vector<int> > VVI; n inline int mult(int x, int y) { return ((long long)x * y) % 1000000007; } n int power(int x, int y) { n   int ret = 1; n   while (y) { n     if (y & 1) ret = mult(ret, x); n     x = mult(x, x); n     y = y / 2; n   } n   return ret; n } n int gcd(int a, int b) { n   if (b) return gcd(b, a % b); n   return a; n } n vector<int> prime; n void generateprime(int n) { n   int i, j; n   vector<int> num(n + 5); n   num[1] = 1; n   for (i = 4; i < n; i = i + 2) num[i] = 1; n   for (i = 3; i < n; i++) { n     if (num[i] == 0) { n       for (j = 3 * i; j < n; j = j + 2 * i) num[j] = 1; n     } n   } n   num[0] = 0; n   for (i = 2; i < n; i++) { n     if (num[i] == 0) prime.push_back(i); n     num.clear(); n   } n } n int main() { n   int count1, n, i, temp; n   scanf( %d , &n); n   if (n == 1) { n     scanf( %d , &temp); n     if (temp == 1) printf( YES n ); n     if (temp == 0) printf( NO n ); n   } else { n     count1 = 0; n     for (i = 0; i < n; i++) { n       scanf( %d , &temp); n       if (temp == 1) count1++; n     } n     if (count1 == n - 1) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1005; n int n, K; n int w[N][N]; n std::vector<int> ansx, ansy; n int main() { n   memset(w, -1, sizeof w); n   bool f = true; n   scanf( %d%d , &n, &K); n   for (int i = 1; i <= n; ++i) { n     int cnt = K; n     for (int j = 1; j <= n && cnt > 0; ++j) n       if (j != i && w[i][j] == -1) { n         --cnt; n         w[i][j] = 1; n         w[j][i] = 0; n         ansx.push_back(i); n         ansy.push_back(j); n       } n     if (cnt != 0) { n       f = false; n       break; n     } n   } n   if (!f) n     puts( -1 ); n   else { n     printf( %d n , ansx.size()); n     for (int i = 0; i < ansx.size(); ++i) printf( %d %d n , ansx[i], ansy[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, *dot, inf = 2e9, bg = 1e9, lg, rg, add, *ar; n bool er = 0; n void push(int nu) { n   dot[nu * 2] = min(dot[nu * 2], dot[nu]); n   dot[nu * 2 + 1] = min(dot[nu * 2 + 1], dot[nu]); n   dot[nu] = inf; n } n void cha(int nu, int l, int r) { n   if (l >= lg and r <= rg) n     dot[nu] = min(dot[nu], add); n   else if (!(l > rg or r < lg)) { n     int mid = (l + r) / 2; n     push(nu); n     cha(nu * 2, l, mid); n     cha(nu * 2 + 1, mid + 1, r); n   } n } n int que(int nu, int l, int r) { n   if (l == r) n     return dot[nu]; n   else { n     int mid = (l + r) / 2; n     push(nu); n     if (lg <= mid) n       return que(nu * 2, l, mid); n     else n       return que(nu * 2 + 1, mid + 1, r); n   } n } n void recalc(int nu) { dot[nu] = max(dot[nu * 2], dot[nu * 2 + 1]); } n void build(int nu, int l, int r) { n   if (l == r) n     dot[nu] = ar[l]; n   else { n     int mid = (l + r) / 2; n     build(nu * 2, l, mid); n     build(nu * 2 + 1, mid + 1, r); n     recalc(nu); n   } n } n void cha2(int nu, int l, int r) { n   if (l == r) n     dot[nu] = add; n   else { n     int mid = (l + r) / 2; n     if (lg <= mid) n       cha2(nu * 2, l, mid); n     else n       cha2(nu * 2 + 1, mid + 1, r); n     recalc(nu); n   } n } n int que2(int nu, int l, int r) { n   if (l >= lg and r <= rg) n     return dot[nu]; n   else if (l > rg or r < lg) n     return 0; n   else { n     int mid = (l + r) / 2; n     int an = max(que2(nu * 2, l, mid), que2(nu * 2 + 1, mid + 1, r)); n     recalc(nu); n     return an; n   } n } n int main() { n   scanf( %d%d , &n, &m); n   dot = new int[n * 4], ar = new int[n]; n   int za[m][4]; n   bool us[n]; n   for (int a = 0; a < n * 4; a++) dot[a] = inf; n   for (int a = 0; a < n; a++) { n     ar[a] = inf, us[a] = 0; n   } n   for (int a = 0; a < m; a++) { n     scanf( %d , &za[a][0]); n     if (za[a][0] == 1) { n       for (int b = 1; b < 4; b++) scanf( %d , &za[a][b]); n       za[a][1]--, za[a][2]--; n       lg = za[a][1], rg = za[a][2], add = za[a][3]; n       cha(1, 0, n - 1); n     } else { n       for (int b = 1; b < 3; b++) scanf( %d , &za[a][b]); n       za[a][1]--; n       lg = za[a][1]; n       if (us[lg] == 0) { n         ar[lg] = que(1, 0, n - 1); n         us[lg] = 1; n       } n     } n   } n   for (lg = 0; lg < n; lg++) { n     if (us[lg] == 0) ar[lg] = que(1, 0, n - 1); n   } n   build(1, 0, n - 1); n   for (int a = 0; a < m; a++) { n     if (za[a][0] == 1) { n       lg = za[a][1], rg = za[a][2]; n       int an = que2(1, 0, n - 1); n       if (za[a][3] != an) er = 1; n     } else { n       lg = za[a][1], add = za[a][2]; n       cha2(1, 0, n - 1); n     } n   } n   if (er) n     printf( NO ); n   else { n     printf( YES n ); n     map<int, vector<int> > ma; n     for (int a = 0; a < n; a++) { n       ma[ar[a]].push_back(a); n       if (ar[a] == inf) ar[a] = bg; n     } n     if (ma[inf].size() > 1) { n       int nu = ma[inf][0]; n       ar[nu] = (1 << 29) - 1; n     } else { n       if (ma[inf].size() == 0) ma.erase(inf); n       int an = 0; n       for (map<int, vector<int> >::iterator it = ma.begin(); it != ma.end(); n            it++) { n         int va = (*it).first, nu = (*it).second[0]; n         if (va == inf) { n           va = 0; n           for (int b = 29; b >= 0; b--) { n             int ne = (1 << b); n             if ((an & ne) == 0) { n               if (va + ne <= bg) va += ne; n             } n           } n           an = (an | va); n           ar[nu] = va; n         } else { n           an = (an | va); n           if ((*it).second.size() > 1) { n             for (int b = 29; b >= 1; b--) { n               int ne = (1 << b) - 1; n               if (va >= ne) { n                 ar[nu] = ne, an = (an | ne); n                 break; n               } n             } n           } n         } n       } n     } n     for (int a = 0; a < n; a++) printf( %d  , ar[a]); n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n struct tree { n   int l, r; n   int tl, tr, parent; n   long long sum, nextsum, occupy; n } trees[232222]; n long long a[2][2], b[2][2]; n int s[111111]; n int ids = 0; n long long pre = -1; n void make_matrix(long long tm) { n   if (tm <= 1) { n     a[0][0] = 0; n     a[0][1] = a[1][0] = a[1][1] = 1; n     return; n   } n   make_matrix(tm / 2); n   for (int i = 0; i < 2; ++i) n     for (int j = 0; j < 2; ++j) b[i][j] = a[i][j]; n   for (int i = 0; i < 2; ++i) n     for (int j = 0; j < 2; ++j) n       a[i][j] = (b[i][0] * b[0][j] + b[i][1] * b[1][j]) % MOD; n   if (tm % 2) { n     for (int i = 0; i < 2; ++i) n       for (int j = 0; j < 2; ++j) b[i][j] = a[i][j]; n     a[0][0] = b[0][1]; n     a[0][1] = (b[0][0] + b[0][1]) % MOD; n     a[1][0] = b[1][1]; n     a[1][1] = (b[1][0] + b[1][1]) % MOD; n   } n } n void cal(long long tm, long long &sum, long long &nextsum) { n   if (pre != tm) { n     make_matrix(tm); n   } n   pre = tm; n   long long sum_temp = (sum * a[0][0] + nextsum * a[1][0]) % MOD; n   nextsum = (sum * a[0][1] + nextsum * a[1][1]) % MOD; n   sum = sum_temp; n } n int make_tree(int a[], int n, int start) { n   int id = ids; n   ++ids; n   trees[id].l = start; n   trees[id].r = start + n - 1; n   trees[id].parent = 0; n   if (n == 1) { n     trees[id].sum = 0; n     trees[id].nextsum = 1; n     cal(a[0], trees[id].sum, trees[id].nextsum); n     trees[id].occupy = 0; n     trees[id].tl = 0; n     trees[id].tr = 0; n   } else { n     int lid = make_tree(a, n / 2, start); n     int rid = make_tree(a + n / 2, n - n / 2, start + n / 2); n     trees[id].tl = lid; n     trees[id].tr = rid; n     trees[id].sum = (trees[lid].sum + trees[rid].sum) % MOD; n     trees[id].nextsum = (trees[lid].nextsum + trees[rid].nextsum) % MOD; n     trees[lid].parent = id; n     trees[rid].parent = id; n   } n   return id; n } n void add_value(tree &node, long long value) { n   long long psum = node.sum; n   long long pnsum = node.nextsum; n   cal(value, node.sum, node.nextsum); n   node.occupy = node.occupy + value; n   psum = node.sum - psum; n   pnsum = node.nextsum - pnsum; n   int p = node.parent; n   while (p != 0) { n     trees[p].nextsum = (trees[p].nextsum + pnsum + MOD) % MOD; n     trees[p].sum = (trees[p].sum + psum + MOD) % MOD; n     p = trees[p].parent; n   } n } n void refresh_from_above(int l, int r, tree &node) { n   if (node.l > r || node.r < l) return; n   if (l <= node.l && node.r <= r) return; n   if (node.occupy > 0) { n     trees[node.tl].occupy = trees[node.tl].occupy + node.occupy; n     cal(node.occupy, trees[node.tl].sum, trees[node.tl].nextsum); n     trees[node.tr].occupy = trees[node.tr].occupy + node.occupy; n     cal(node.occupy, trees[node.tr].sum, trees[node.tr].nextsum); n     node.occupy = 0; n   } n   refresh_from_above(l, r, trees[node.tl]); n   refresh_from_above(l, r, trees[node.tr]); n } n void add_area(int l, int r, tree &node, long long value) { n   if (node.l > r || node.r < l) return; n   if (l <= node.l && r >= node.r) { n     add_value(node, value); n     return; n   } n   add_area(l, r, trees[node.tl], value); n   add_area(l, r, trees[node.tr], value); n } n long long cal_area(int l, int r, tree &node) { n   if (node.l > r || node.r < l) return 0; n   if (l <= node.l && r >= node.r) { n     return node.sum; n   } n   return (cal_area(l, r, trees[node.tl]) + cal_area(l, r, trees[node.tr])) % n          MOD; n } n int main() { n   int n, m, p, l, r, x; n   ids = 1; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; ++i) scanf( %d , &s[i]); n   int id = make_tree(s, n, 1); n   for (int i = 0; i < m; ++i) { n     scanf( %d , &p); n     if (p == 1) { n       scanf( %d%d%d , &l, &r, &x); n       refresh_from_above(l, r, trees[id]); n       add_area(l, r, trees[id], x); n     } else { n       scanf( %d%d , &l, &r); n       refresh_from_above(l, r, trees[id]); n       printf( %I64d n , cal_area(l, r, trees[id])); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n struct data { n   int t, g; n }; n int memo[250][4][66000]; n int n, t; n data in[20]; n int f(int time, int genre, int bit) { n   if (memo[time][genre][bit] != -1) return memo[time][genre][bit]; n   if (time == t) return memo[time][genre][bit] = 1; n   if (time > t) return memo[time][genre][bit] = 0; n   int RE = 0; n   for (int i = 0; i < n; i++) n     if (in[i].g != genre && !(bit & (1 << i))) { n       RE = (RE + f(time + in[i].t, in[i].g, bit | (1 << i))) % mod; n     } n   return memo[time][genre][bit] = RE; n } n int main() { n   cin >> n >> t; n   for (int i = 0; i < n; i++) cin >> in[i].t >> in[i].g; n   memset(memo, -1, sizeof(memo)); n   cout << f(0, 0, 0); n } n


#include <bits/stdc++.h> n using namespace std; n int go_to_hall(string maxx, string minn, int base) { n   string h =   ; n   int a = 0; n   for (int i = minn.size() - 1, j = maxx.size() - 1; i >= 0; j--, i--) { n     int q1 = minn[i] -  0 , q2 = maxx[j] -  0 ; n     int q3 = q1 + q2 + a; n     if (q3 >= base) { n       int a1 = q3 - base; n       string w; n       ostringstream convert4; n       convert4 << a1; n       w = convert4.str(); n       h += w; n       a = 1; n     } else { n       string w; n       ostringstream convert5; n       convert5 << q3; n       w = convert5.str(); n       h += w; n       a = 0; n     } n   } n   if (a == 1 && maxx.size() == minn.size()) { n     h +=  1 ; n     return h.size(); n   } else { n     int s = 0; n     for (int r = maxx.size() - minn.size() - 1; r >= 0; r--) { n       int e = s + (maxx[r] -  0 ); n       int x = maxx.size() - minn.size() - 1; n       if (r == x) { n         int y = e + a; n         if (y >= base) { n           int a1 = y - base; n           string w; n           ostringstream convert6; n           convert6 << a1; n           w = convert6.str(); n           h += w; n           s = 1; n         } else { n           string w; n           ostringstream convert7; n           convert7 << y; n           w = convert7.str(); n           h += w; n           s = 0; n         } n       } else { n         if (e >= base) { n           int a1 = e - base; n           string w; n           ostringstream convert8; n           convert8 << a1; n           w = convert8.str(); n           h += w; n           s = 1; n         } else { n           string w; n           ostringstream convert9; n           convert9 << e; n           w = convert9.str(); n           h += w; n           s = 0; n         } n       } n     } n     if (s == 1) { n       h +=  1 ; n     } n     return h.size(); n   } n } n int main() { n   int x, y, base = 0; n   cin >> x >> y; n   int k1 = x, k2 = y; n   string q1; n   ostringstream convert; n   convert << x; n   q1 = convert.str(); n   string q2; n   ostringstream convert1; n   convert1 << y; n   q2 = convert1.str(); n   for (int y = 0; y < q1.size(); y++) { n     int r = q1[y] -  0 ; n     base = max(r, base); n   } n   for (int y = 0; y < q2.size(); y++) { n     int r = q2[y] -  0 ; n     base = max(r, base); n   } n   base++; n   if (base == 10) { n     int k3 = k1 + k2; n     int i = 0; n     while (k3 != 0) { n       k3 = k3 / 10; n       i++; n     } n     cout << i << endl; n   } else { n     string maxx, minn; n     if (q1.size() >= q2.size()) { n       maxx = q1; n       minn = q2; n     } else { n       maxx = q2; n       minn = q1; n     } n     cout << go_to_hall(maxx, minn, base) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n struct BIT { n   int n; n   vector<T> dat; n   BIT(int n = 0) { initialize(n); } n   void initialize(int nin) { n     n = nin; n     dat.resize(n, 0); n   } n   T sum(int i) { n     T s = 0; n     while (i >= 0) { n       s += dat[i]; n       i = (i & (i + 1)) - 1; n     } n     return s; n   } n   T sum_between(int i, int j) { return sum(j) - sum(i - 1); } n   void plus(int i, T x) { n     while (i < n) { n       dat[i] += x; n       i |= i + 1; n     } n   } n   int lower_bound(T x) { n     if (x < 0) return -1; n     int ret = -1; n     int k = 1; n     while (2 * k <= n) k <<= 1; n     for (; k > 0; k >>= 1) { n       if (ret + k < n && dat[ret + k] < x) { n         x -= dat[ret + k]; n         ret += k; n       } n     } n     return ret + 1; n   } n }; n int main() { n   int N, M; n   cin >> N >> M; n   vector<int> A(N + M), mn(N), mx(N); n   for (int i = 0; i < N; i++) A[N - 1 - i] = mn[i] = mx[i] = i; n   for (int i = 0; i < M; i++) { n     scanf( %d , &A[i + N]); n     A[i + N]--; n     mn[A[i + N]] = 0; n   } n   M += N; n   vector<vector<int>> pos(N); n   for (int i = 0; i < M; i++) pos[A[i]].push_back(i); n   vector<pair<int, int>> R2LI(M); n   for (int i = 0; i < N; i++) { n     int sz = pos[i].size(); n     for (int j = 0; j < sz - 1; j++) R2LI[pos[i][j + 1]] = {pos[i][j], i}; n   } n   BIT<int> bit(M); n   for (int r = N; r < M; r++) { n     int l = R2LI[r].first, i = R2LI[r].second; n     bit.plus(l, 1); n     int res = r - l - bit.sum_between(l, r); n     mx[i] = max(mx[i], res); n   } n   for (int i = 0; i < N; i++) { n     int l = pos[i].back(), r = M - 1; n     int res = r - l - bit.sum_between(l, r); n     mx[i] = max(mx[i], res); n   } n   for (int i = 0; i < N; i++) printf( %d %d n , mn[i] + 1, mx[i] + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n const long long mo = 1e9 + 7; n const long long inf = 1e9; n const long long Inf = 1e18; n int a[N], b[N]; n int main() { n   int n, m, tot = 0; n   scanf( %d%d , &n, &m); n   int nw = 0; n   for (int i = (1); i <= (m); ++i) { n     scanf( %d , &a[i]); n     int x = a[i]; n     if (x <= nw) { n       b[i] = b[i - 1] + x; n       nw -= x; n     } else { n       x -= nw; n       tot += x / n; n       x = x % n; n       b[i] = x; n       if (x) n         nw = n - x, ++tot; n       else n         nw = 0; n     } n   } n   sort(b + 1, b + m); n   b[m] = n; n   for (int i = (m); i >= (1); --i) b[i] = b[i] - b[i - 1]; n   printf( %d n , tot); n   for (int i = (1); i <= (m); ++i) printf( %d  , b[i]); n   printf(  n ); n   int x = 1; n   for (int i = (1); i <= (m); ++i) { n     while (a[i] > 0) { n       a[i] -= b[x]; n       printf( %d  , i); n       if (x == m) n         x = 1, printf(  n ); n       else n         ++x; n     } n   } n   for (; x != 1 && x != m + 1; ++x) printf( %d  , m); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n const long long MOD = 1000000007; n const int INF = 1e9; n const long long LINF = 1LL * INF * INF; n const int MAXN = 100007; n const double EPS = 1e-7; n const int DX[4] = {1, 0, -1, 0}, DY[4] = {0, 1, 0, -1}; n int n, m, r, k; n set<pair<int, int> > U; n priority_queue<pair<long long, pair<int, int> > > Q; n long long f(int i, int j) { n   return (min(i, n - r) - max(0, i - r + 1) + 1LL) * n          (min(j, m - r) - max(0, j - r + 1) + 1LL); n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   long long s = 0; n   cin >> n >> m >> r >> k; n   Q.push(make_pair(f(n / 2, m / 2), make_pair(n / 2, m / 2))); n   U.insert(make_pair(n / 2, m / 2)); n   for (int j = (0); j < (k); ++j) { n     s += Q.top().first; n     int x = Q.top().second.first, y = Q.top().second.second; n     Q.pop(); n     for (int i = (0); i < (4); ++i) { n       if (x + DX[i] >= 0 && x + DX[i] < n && y + DY[i] >= 0 && y + DY[i] < m && n           !U.count(make_pair(x + DX[i], y + DY[i]))) { n         Q.push(make_pair(f(x + DX[i], y + DY[i]), n                          make_pair(x + DX[i], y + DY[i]))); n         U.insert(make_pair(x + DX[i], y + DY[i])); n       } n     } n   } n   cout << fixed << setprecision(12) << s / (n - r + 1.0) / (m - r + 1.0); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   vector<int> a(2 * n); n   for (int i = 0; i < 2 * n; i++) { n     scanf( %d , &a[i]); n   } n   sort(a.begin(), a.end()); n   printf( %s n , (a[n - 1] != a[n]) ?  YES  :  NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, h; n long long dp[40][40]; n int main() { n   scanf( %d %d , &n, &h); n   memset(dp, 0, sizeof(dp)); n   for (int i = 0; i <= n; i++) { n     dp[0][i] = 1; n   } n   for (int h = 1; h <= n; h++) { n     for (int i = 1; i <= n; i++) { n       for (int j = 0; j < i; j++) n         dp[i][h] += dp[j][h - 1] * dp[i - j - 1][h - 1]; n     } n   } n   printf( %I64d n , dp[n][n] - dp[n][h - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read(int f = 1, int x = 0, char ch =    ) { n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) f = -1; n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return f * x; n } n const int N = 1e5 + 5; n struct Edge { n   int next, to; n   Edge(int next = 0, int to = 0) : next(next), to(to){}; n } edge[N << 1]; n int tot, head[N]; n void add(int x, int y) { edge[++tot] = Edge(head[x], y), head[x] = tot; } n int n, fa[N], dfn[N], t, f[N], to[N], d[N], ans; n void dfs(int x) { n   dfn[++t] = x; n   for (int i = head[x]; i; i = edge[i].next) { n     int y = edge[i].to; n     if (y == to[x]) continue; n     dfs(y); n   } n   if (to[x]) dfs(to[x]); n } n int main() { n   n = read(); n   for (int i = 2; i <= n; ++i) add(fa[i] = read() + 1, i), d[i] = d[fa[i]] + 1; n   for (int i = n; i > 1; --i) n     f[fa[i]] = max(f[fa[i]], f[i] + 1), n     to[fa[i]] = f[i] >= f[to[fa[i]]] ? i : to[fa[i]]; n   dfs(1); n   for (int i = 1; i <= n; ++i) printf( %d  , dfn[i] - 1); n   puts(  ); n   for (int i = n; i > 1; --i) ans += d[dfn[i - 1]] - d[fa[dfn[i]]]; n   printf( %d n , ans); n   for (int i = 2; i <= n; ++i) n     for (int j = 1; j <= d[dfn[i - 1]] - d[fa[dfn[i]]]; ++j) n       printf( %d  , dfn[i] - 1); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int binExp(long long int x, long long int n, long long int m) { n   long long int res = 1; n   while (n) { n     if (n & 1) res = (res * x) % m; n     x = (x * x) % m; n     n >>= 1; n   } n   return res; n } n long long int modInv(long long int i, long long int m) { n   return binExp(i, m - 2, m); n } n long long int add(long long int a, long long int b) { n   long long int res = a + b; n   if (res >= 1000000007) res -= 1000000007; n   if (res < 0) res += 1000000007; n   return res; n } n long long int mul(long long int a, long long int b) { n   long long int res = (a) * (b); n   res %= 1000000007; n   if (res < 0) res += 1000000007; n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t, t1 = 1; n   cin >> t; n   while (t--) { n     string str; n     cin >> str; n     int odd = 0, even = 0, oddq = 0, evenq = 0, oddt = 0, event = 0, ans = 0; n     for (int i = 0; i < 10; i++) { n       if (i % 2 != 0) { n         event++; n         if (str[i] ==  1 ) { n           even++; n         } n         if (str[i] ==  ? ) { n           evenq++; n         } n         int oddrem = 5 - oddt; n         int evenrem = 5 - event; n         if ((odd + oddq - even) > evenrem) { n           ans = i + 1; n           break; n         } n         if ((even + evenq - odd) > oddrem) { n           ans = i + 1; n           break; n         } n       } else { n         oddt++; n         if (str[i] ==  1 ) { n           odd++; n         } n         if (str[i] ==  ? ) { n           oddq++; n         } n         int oddrem = 5 - oddt; n         int evenrem = 5 - event; n         if ((odd + oddq - even) > evenrem) { n           ans = i + 1; n           break; n         } n         if ((even + evenq - odd) > oddrem) { n           ans = i + 1; n           break; n         } n       } n     } n     if (ans == 0) { n       cout << 10 << endl; n       continue; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long t, k, i; n   scanf( %lld %lld , &t, &k); n   long long dp[100001]; n   dp[0] = 1; n   for (i = 1; i < 100001; i++) { n     dp[i] = dp[i - 1]; n     if (i >= k) dp[i] += dp[i - k]; n     dp[i] = dp[i] % 1000000007; n   } n   long long sum[100001]; n   sum[0] = dp[0]; n   for (i = 1; i < 100001; i++) sum[i] = (dp[i] + sum[i - 1]) % 1000000007; n   while (t--) { n     long long a, b; n     scanf( %lld %lld , &a, &b); n     printf( %lld n , (sum[b] - sum[a - 1] + 1000000007) % 1000000007); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n int n; n long long p[200002], i[200002], v[200002], lvl[200002], ans; n vector<int> G[200002]; n void dfs(int x, int par) { n   if (par) lvl[x] = lvl[par] + 1; n   for (auto it : G[x]) { n     if (it == par) continue; n     dfs(it, x); n     ans = (ans + n            2 * v[x] * (i[x] * p[it] % mod - p[x] * i[it] % mod + mod) % mod); n     i[x] += p[it]; n     p[x] += i[it]; n   } n   i[x]++; n } n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> v[i]; n   for (int i = 1; i < n; i++) { n     int x, y; n     cin >> x >> y; n     G[x].push_back(y); n     G[y].push_back(x); n   } n   dfs(1, 0); n   for (int it = 1; it <= n; it++) { n     if (lvl[it] % 2) n       ans = (ans + 2 * v[it] * n                        (i[it] * (p[1] - i[it] + 1) % mod - n                         p[it] * (i[1] - p[it]) % mod + mod) % n                        mod) % n             mod; n     else { n       ans = (ans + 2 * v[it] * n                        (i[it] * (i[1] - i[it] + 1) % mod - n                         p[it] * (p[1] - p[it]) % mod + mod) % n                        mod) % n             mod; n     } n     ans = (ans - v[it] + mod) % mod; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N, M; n typedef struct node { n   double x; n   double y; n   node operator-(node b) { n     node ret; n     ret.x = x - b.x; n     ret.y = y - b.y; n     return ret; n   } n   node operator+(node b) { n     node ret; n     ret.x = x + b.x; n     ret.y = y + b.y; n     return ret; n   } n   node operator/(double c) { n     node ret; n     ret.x = x / c; n     ret.y = y / c; n     return ret; n   } n   node operator*(double c) { n     node ret; n     ret.x = x * c; n     ret.y = y * c; n     return ret; n   } n } node; n double xmul(node a, node b) { return a.x * b.y - b.x * a.y; } n void getit(double &A, double &B, double &C, node a, node b) { n   double At = b.y - a.y; n   double Bt = a.x - b.x; n   A = -Bt; n   B = At; n   double x = (a.x + b.x) / 2; n   double y = (a.y + b.y) / 2; n   C = A * x + B * y; n } n node getcenter(node A, node B, node C) { n   double A1, B1, C1; n   getit(A1, B1, C1, A, B); n   double A2, B2, C2; n   getit(A2, B2, C2, B, C); n   double det = A1 * B2 - A2 * B1; n   node ret; n   ret.x = (B2 * C1 - B1 * C2) / det; n   ret.y = (A1 * C2 - A2 * C1) / det; n   return ret; n } n bool solve(node a, node b, node c) { n   node A = b * 2 - a; n   node F = getcenter(A, b, c); n   node G = c * 2 - F; n   node E = b * 2 - F; n   node D = a * 2 - E; n   if (xmul(E - D, F - E) < 1e-7 || xmul(F - E, G - F) < 1e-7 || n       xmul(G - F, D - G) < 1e-7 || xmul(D - G, E - D) < 1e-7) n     return false; n   printf( YES n%.9lf %.9lf %.9lf %.9lf %.9lf %.9lf %.9lf %.9lf n , D.x, D.y, n          E.x, E.y, F.x, F.y, G.x, G.y); n   return true; n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     node a, b, c; n     scanf( %lf%lf%lf%lf%lf%lf , &a.x, &a.y, &b.x, &b.y, &c.x, &c.y); n     if (fabs(xmul(a - c, b - c)) < 1e-7 || n         !solve(a, b, c) && !solve(a, c, b) && !solve(c, a, b) && n             !solve(c, b, a) && !solve(b, a, c) && !solve(b, c, a)) n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n template <typename T> n inline T max(T a, T b) { n   return a > b ? a : b; n } n template <typename T> n inline T min(T a, T b) { n   return a < b ? a : b; n } n template <typename T> n inline T abs(T a) { n   return a > 0 ? a : -a; n } n template <typename T> n inline void repr(T &a, T b) { n   if (a < b) a = b; n } n template <typename T> n inline void repl(T &a, T b) { n   if (a > b) a = b; n } n int n, x, y, a[3001], f[3001][3001], fa[3001][3001][2], a1[3000], a2[3000], m1, n     m2; n struct B { n   int v, id; n   bool operator<(const B &x) const { return v > x.v; } n } b[3001]; n int main() { n   scanf( %d%d%d , &n, &x, &y); n   for (int i = 1; i <= n; i++) scanf( %d , a + i), f[0][0] += a[i]; n   for (int i = 1; i <= n; i++) n     scanf( %d , &b[i].v), b[i].v -= a[i], b[i].id = i; n   std::sort(b + 1, b + n + 1); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (f[i - 1][j] - a[b[i].id] > f[i][j]) n         f[i][j] = f[i - 1][j] - a[b[i].id], fa[i][j][0] = i - 1, n         fa[i][j][1] = j; n       if (j <= y) { n         if (f[i - 1][j - 1] + b[i].v > f[i][j]) n           f[i][j] = f[i - 1][j - 1] + b[i].v, fa[i][j][0] = i - 1, n           fa[i][j][1] = j - 1; n       } else { n         if (f[i - 1][j - 1] > f[i][j]) n           f[i][j] = f[i - 1][j - 1], fa[i][j][0] = i - 1, fa[i][j][1] = j - 1; n       } n     } n     f[i][0] = f[i - 1][0] - a[b[i].id]; n   } n   printf( %d n , f[n][x + y]); n   for (int i = n, j = x + y; j;) { n     while (fa[i][j][1] == j) { n       i = fa[i][j][0]; n     } n     if (j <= y) n       a2[m2++] = b[i].id; n     else n       a1[m1++] = b[i].id; n     i = fa[i][j][0]; n     j--; n   } n   std::sort(a1, a1 + m1); n   std::sort(a2, a2 + m2); n   for (int i = 0; i < m1; i++) printf( %d  , a1[i]); n   printf(  n ); n   for (int i = 0; i < m2; i++) printf( %d  , a2[i]); n   printf(  n ); n } n


#include <bits/stdc++.h> n using namespace std; n set<int> ss; n map<int, int> mm; n int arr[2 * 100003], pr[2 * 100003], nxt[2 * 100003]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < int(n); i++) { n     cin >> arr[i + 1]; n     ss.insert(arr[i + 1]); n     mm[arr[i + 1]] = i + 1; n     pr[i + 1] = i; n     nxt[i + 1] = i + 2; n   } n   int ans[n + 1]; n   for (int i = 0; i < int(n); i++) { n     if (ss.empty()) break; n     int pos = mm[(*ss.rbegin())]; n     int tch = 1 + (i % 2); n     ans[pos] = tch; n     ss.erase(arr[pos]); n     int p_f = pos, p_b = pos; n     int j = k; n     while (nxt[p_f] != n + 1 and j--) { n       p_f = nxt[p_f]; n       ss.erase(arr[p_f]); n       ans[p_f] = tch; n     } n     j = k; n     while (pr[p_b] != 0 and j--) { n       p_b = pr[p_b]; n       ans[p_b] = tch; n       ss.erase(arr[p_b]); n     } n     pr[nxt[p_f]] = pr[p_b]; n     nxt[pr[p_b]] = nxt[p_f]; n   } n   for (int i = 0; i < int(n); i++) cout << ans[i + 1]; n   cout <<   n ; n   cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int v1, v2, t, d, i = 2; n   scanf( %d%d%d%d , &v1, &v2, &t, &d); n   int sum = v1 + v2; n   while (i != t) { n     i++; n     if (v1 < v2) { n       v1 += d; n       sum += v1; n     } else { n       v2 += d; n       sum += v2; n     } n   } n   printf( %d , sum); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ull = unsigned long long; n using pii = pair<int, int>; n using vi = vector<int>; n using vvi = vector<vi>; n const int INF = 2e9 + 5; n int main() { n   cin.tie(0), cin.sync_with_stdio(0), cout.tie(0), cout.sync_with_stdio(0); n   int n; n   cin >> n; n   vi l(n), r(n); n   for (int i = 0; i < n; i++) { n     cin >> l[i] >> r[i]; n   } n   sort(l.begin(), l.end()); n   sort(r.begin(), r.end()); n   ll ans = n; n   for (int i = 0; i < n; i++) ans += max(l[i], r[i]); n   cout << ans; n   return cout <<   n , 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename Arg1> n void __f(const char* name, Arg1&& arg1) { n   cerr << name <<   :   << arg1 << std::endl; n } n template <typename Arg1, typename... Args> n void __f(const char* names, Arg1&& arg1, Args&&... args) { n   const char* comma = strchr(names + 1,  , ); n   cerr.write(names, comma - names) <<   :   << arg1 <<   |  ; n   __f(comma + 1, args...); n } n const long long oo = INT_MAX; n const long long MOD = 1e9 + 7; n const long long sz = 1e5 + 5; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long tc = 1; n   while (tc--) { n     long long n; n     cin >> n; n     vector<long long> tree[n + 1]; n     for (long long i = 2; i < n + 1; ++i) { n       long long p; n       cin >> p; n       tree[i].push_back(p); n       tree[p].push_back(i); n     } n     vector<long long> nodeCnt(n + 1, 0); n     vector<double> startTime(n + 1, 0.0); n     startTime[1] = 1.0; n     function<long long(long long, long long)> dfs1 = [&](long long node, n                                                          long long parent) { n       long long cnt = 1; n       for (auto child : tree[node]) { n         if (child != parent) { n           cnt += dfs1(child, node); n         } n       } n       nodeCnt[node] = cnt; n       return cnt; n     }; n     function<void(long long, long long)> dfs2 = [&](long long node, n                                                     long long parent) { n       long long ncnt = nodeCnt[node] - 1, cnt = 0; n       for (auto child : tree[node]) { n         if (child != parent) { n           startTime[child] = n               ((ncnt - nodeCnt[child]) * 1.0) / 2 + (1.0 + startTime[node]); n           dfs2(child, node); n         } n       } n     }; n     dfs1(1, 0); n     dfs2(1, 0); n     for (long long i = 1; i < n + 1; ++i) n       cout << fixed << setprecision(1) << startTime[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int k; n   cin >> k; n   map<char, int> dict; n   for (int i = 0; i < 27; i++) dict[i +  a ] = 0; n   for (int i = 0; i < s.length(); i++) dict[s[i]]++; n   map<char, int>::iterator it; n   vector<pair<int, char> > letras; n   pair<int, char> aux; n   int distinto; n   for (it = dict.begin(); it != dict.end(); ++it) { n     aux = make_pair((*it).second, (*it).first); n     if (aux.first != 0) letras.push_back(aux); n   } n   sort((letras).begin(), (letras).end()); n   distinto = letras.size(); n   int l = 0; n   int j = 0; n   while (true) { n     j += letras[l].first; n     if (j > k) break; n     l++; n     if (j == k) break; n     if (l == distinto) break; n   } n   if (l == distinto) { n     cout << 0 << endl; n     cout << endl; n     return 0; n   } n   bool ltras[28]; n   memset(ltras, 0, sizeof(ltras)); n   for (int i = l; i < letras.size(); i++) { n     ltras[letras[i].second -  a ] = 1; n   } n   string res =   ; n   for (int i = 0; i < s.length(); i++) n     if (ltras[s[i] -  a ]) res += s[i]; n   cout << distinto - l << endl; n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k, p; n int dp[105][55]; n int modd(int x) { n   if (x < 0) n     return x + p; n   else if (x >= p) n     return x - p; n   return x; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   cerr.tie(nullptr); n   cin >> n >> k >> p; n   memset(dp, 128, sizeof dp); n   dp[0][0] = 0; n   int z = 0; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x; n     z = (z + x) % p; n     vector<int> vz(55, -2e9); n     for (int l = 0; l < k; l++) { n       for (int j = 0; j < p; j++) { n         vz[l + 1] = max(vz[l + 1], dp[j][l] + modd(z - j + p)); n       } n     } n     for (int i = 0; i < 55; i++) dp[z][i] = max(dp[z][i], vz[i]); n   } n   cout << dp[z][k] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = (int)1e5 + 10; n int a[maxN]; n int n; n vector<pair<int, int> > ans; n int cop[maxN]; n bool solve_stupid(int n) { n   for (int i = 1; i <= n; i++) cop[i] = a[i]; n   vector<pair<int, int> > all; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       int k = 2 * j - i; n       if (k <= n) all.emplace_back(i, j); n     } n   } n   int m = all.size(); n   random_shuffle(all.begin(), all.end()); n   vector<pair<int, int> > nall; n   for (int i = 0; i < min(m, 22); i++) nall.push_back(all[i]); n   all = nall; n   m = all.size(); n   for (int mask = 0; mask < (1 << m); mask++) { n     bool ok = true; n     for (int i = 1; i <= n; i++) { n       a[i] = cop[i]; n     } n     for (int bit = 0; bit < m; bit++) { n       if (mask & (1 << bit)) { n         int x = all[bit].first; n         int y = all[bit].second; n         int z = 2 * y - x; n         a[x] ^= 1; n         a[y] ^= 1; n         a[z] ^= 1; n       } n     } n     for (int i = 1; i <= n; i++) { n       if (a[i] != 0) { n         ok = false; n         break; n       } n     } n     if (ok) { n       for (int bit = 0; bit < m; bit++) { n         if (mask & (1 << bit)) ans.push_back(all[bit]); n       } n       return true; n     } n   } n   return false; n } n int main() { n   ios_base::sync_with_stdio(false); n   srand(time(0)); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   while (n > 13) { n     vector<pair<int, int> > possible; n     possible.emplace_back(0, 0); n     for (int i = n; i >= n - 5; i--) { n       for (int j = i - 1; j >= n - 5; j--) { n         int k = 2 * j - i; n         if (k <= 0) continue; n         possible.emplace_back(k, j); n       } n     } n     for (int i = n; i >= n - 5; i--) { n       int j = n - 6; n       int k = 2 * j - i; n       if (k >= 1) { n         possible.emplace_back(k, j); n       } n     } n     bool ok = false; n     int willi = -1; n     int willj = -1; n     for (int i = n - 5; i <= n; i++) { n       cop[i] = a[i]; n     } n     for (int i = 0; i < possible.size(); i++) { n       if (ok) break; n       for (int j = i; j < possible.size(); j++) { n         for (int q = n - 5; q <= n; q++) a[q] = cop[q]; n         int x = possible[i].first; n         int y = possible[i].second; n         int z = 2 * y - x; n         if (x >= n - 5) a[x] ^= 1; n         if (y >= n - 5) a[y] ^= 1; n         if (z >= n - 5) a[z] ^= 1; n         int xx = possible[j].first; n         int yy = possible[j].second; n         int zz = 2 * yy - xx; n         if (xx >= n - 5) a[xx] ^= 1; n         if (yy >= n - 5) a[yy] ^= 1; n         if (zz >= n - 5) a[zz] ^= 1; n         if (a[n - 5] == 0 && a[n - 4] == 0 && a[n - 3] == 0 && a[n - 2] == 0 && n             a[n - 1] == 0 && a[n] == 0) { n           ok = true; n           willi = i; n           willj = j; n           break; n         } n       } n     } n     assert(ok == true); n     int x = possible[willi].first; n     int y = possible[willi].second; n     int z = 2 * y - x; n     if (x >= 1) a[x] ^= 1; n     if (y >= 1) a[y] ^= 1; n     if (z >= 1) a[z] ^= 1; n     if (x != 0) ans.push_back(possible[willi]); n     int xx = possible[willj].first; n     int yy = possible[willj].second; n     int zz = 2 * yy - xx; n     if (xx >= 1) a[xx] ^= 1; n     if (yy >= 1) a[yy] ^= 1; n     if (zz >= 1) a[zz] ^= 1; n     if (xx != 0) ans.push_back(possible[willj]); n     n -= 6; n   } n   if (solve_stupid(n)) { n     cout <<  YES  <<   n ; n     cout << ans.size() <<   n ; n     for (auto t : ans) { n       cout << t.first <<     << t.second <<     << 2 * t.second - t.first n            <<   n ; n     } n   } else { n     cout <<  NO  <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fileio() {} n vector<vector<long long int>> g; n vector<long long int> bit; n long long int n; n void add(long long int i, long long int x) { n   while (i <= n) { n     bit[i] += x; n     i += (i & (-i)); n   } n } n long long int query(long long int i) { n   long long int sum = 0; n   while (i > 0) { n     sum += bit[i]; n     i -= (i & (-i)); n   } n   return sum; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   fileio(); n   cin >> n; n   bit = vector<long long int>(n + 1, 0); n   vector<long long int> p(n, 0), candi(n + 1, 0), contri(n + 1, 0); n   set<long long int> s; n   for (long long int i = 0; i < n; i++) { n     cin >> p[i]; n     long long int more = query(n) - query(p[i]); n     add(p[i], 1); n     if (more == 1) { n       long long int x = (*s.rbegin()); n       candi[x]++; n     } n     if (more == 0) { n       contri[p[i]]++; n     } n     s.insert(p[i]); n   } n   long long int res = 1; n   for (long long int i = 1; i <= n; i++) n     if (candi[i] - contri[i] > candi[res] - contri[res]) res = i; n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n using min_pq = priority_queue<T, vector<T>, greater<T>>; n const int MAXN = 1e5 + 10, MAXQ = 3e5 + 10, MAXL = 18, ALP = 26, MOD = 1e9 + 7, n           MAXK = 17, MAXA = 10, MAXB = 24, MAXBB = (1 << MAXB); n const string no =  NO n , yes =  YES n ; n const int hA[4] = {1, 0, -1, 0}, kA[4] = {0, 1, 0, -1}; n const int INF = 1e9 + 10; n int n, par[MAXN], md[MAXN], st[MAXN], tt = 0; n vector<int> adj[MAXN]; n int dfs1(int c = 0) { n   md[c] = 1; n   for (auto nxt : adj[c]) md[c] = max(md[c], dfs1(nxt) + 1); n   return md[c]; n } n void dfs2(int c = 0) { n   sort(adj[c].begin(), adj[c].end(), n        [&](int i, int j) { return md[i] < md[j]; }); n   st[tt++] = c; n   for (auto nxt : adj[c]) dfs2(nxt); n } n void solve() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     cin >> par[i], adj[par[i]].push_back(i); n   } n   dfs1(), dfs2(); n   vector<int> ans; n   ans.reserve(n); n   for (int i = 1; i < n; i++) { n     for (int a = st[i], b = st[i - 1]; b != par[a]; b = par[b]) n       ans.push_back(a); n   } n   for (int i = 0; i < n; i++) cout << st[i] <<    ; n   cout <<   n ; n   cout << (int)ans.size() <<   n ; n   for (int c : ans) cout << c <<    ; n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t_c = 1; n   while (t_c--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   map<long long, long long> rt, lf; n   long long a[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     rt[a[i]]++; n   } n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     if (a[i] % k == 0) { n       long long l = a[i] / k; n       rt[a[i]]--; n       long long r = a[i] * k; n       ans += rt[r] * lf[l]; n     } n     lf[a[i]]++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int NO_OF_ALPHABETS = 26; n vector<int> degree(NO_OF_ALPHABETS, 0); n vector<int> graph[NO_OF_ALPHABETS]; n vector<int> component_no(NO_OF_ALPHABETS, 0); n void dfs(int v, int parent_v, int c) { n   component_no[v] = c; n   for (int i = 0; i < graph[v].size(); i++) { n     int child_v = graph[v][i]; n     if (child_v == parent_v || component_no[child_v] == c) { n       continue; n     } n     dfs(child_v, v, c); n   } n } n void solve() { n   string password; n   cin >> password; n   const int NO_OF_ALPHABETS = 26; n   vector<vector<int> > adjacency(NO_OF_ALPHABETS, n                                  vector<int>(NO_OF_ALPHABETS, false)); n   for (int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++) { n     degree[alpha] = 0; n     graph[alpha].clear(); n     component_no[alpha] = 0; n   } n   for (int i = 1; i < password.size(); i++) { n     if (adjacency[password[i] -  a ][password[i - 1] -  a ] == false) { n       adjacency[password[i] -  a ][password[i - 1] -  a ] = true; n       adjacency[password[i - 1] -  a ][password[i] -  a ] = true; n       graph[password[i] -  a ].push_back(password[i - 1] -  a ); n       graph[password[i - 1] -  a ].push_back(password[i] -  a ); n       degree[password[i] -  a ]++; n       degree[password[i - 1] -  a ]++; n     } n   } n   vector<int> degree_2; n   for (int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++) { n     if (degree[alpha] > 2) { n       cout <<  NO n ; n       return; n     } n     if (degree[alpha] == 2) { n       degree_2.push_back(alpha); n     } n   } n   int component = 1; n   for (int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++) { n     if (component_no[alpha] == 0) { n       dfs(alpha, -1, component++); n     } n   } n   string keyboard; n   vector<int> components; n   vector<int> used(NO_OF_ALPHABETS, false); n   for (int c = 1; c < component; c++) { n     vector<int> this_component; n     for (int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++) { n       if (component_no[alpha] == c) { n         this_component.push_back(alpha); n       } n     } n     if (this_component.size() == 1) { n       keyboard += (char)( a  + this_component[0]); n       used[this_component[0]] = true; n       continue; n     } n     int border_1 = -1, border_2 = -1; n     for (int i = 0; i < this_component.size(); i++) { n       if (degree[this_component[i]] == 1) { n         if (border_1 == -1) n           border_1 = this_component[i]; n         else n           border_2 = this_component[i]; n       } n     } n     if (border_1 == -1 || border_2 == -1) { n       cout <<  NO n ; n       return; n     } n     keyboard += (char)( a  + border_1); n     used[border_1] = true; n     while (keyboard.back() -  a  != border_2) { n       for (int i = 0; i < graph[keyboard.back() -  a ].size(); i++) { n         if (!used[graph[keyboard.back() -  a ][i]]) { n           int next = graph[keyboard.back() -  a ][i]; n           keyboard += (char)( a  + next); n           used[next] = true; n           break; n         } n       } n     } n   } n   cout <<  YES n ; n   cout << keyboard <<   n ; n   return; n } n int main() { n   int no_of_test_cases; n   cin >> no_of_test_cases; n   while (no_of_test_cases--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long MOD2 = 998244353; n const float pi = 3.141592653; n long long power(long long a, long long b) { n   if (b == 0) return 1; n   long long p = power(a, b / 2); n   if (b & 1) n     return p * p * a; n   else n     return p * p; n } n void solve() { n   long long n, k; n   cin >> n >> k; n   vector<long long> a, b, c; n   for (long long i = 0; i < n; i++) { n     long long o, t, f; n     cin >> o >> t >> f; n     if (t == 1 && f == 1) n       c.push_back(o); n     else if (t == 1) n       a.push_back(o); n     else if (f == 1) n       b.push_back(o); n   } n   if (a.size() + c.size() < k || b.size() + c.size() < k) { n     cout << -1; n     return; n   } n   sort(a.begin(), a.end()); n   sort(b.begin(), b.end()); n   sort(c.begin(), c.end()); n   long long ans = 0, count = 0, index1 = 0, index2 = 0; n   while (1) { n     if (index2 == min(a.size(), b.size()) || index1 == c.size()) break; n     if (a[index2] + b[index2] <= c[index1]) { n       ans += a[index2] + b[index2]; n       index2++; n     } else { n       ans += c[index1]; n       index1++; n     } n     count++; n     if (count == k) break; n   } n   if (index1 < c.size()) { n     while (count < k) { n       ans += c[index1]; n       index1++; n       count++; n     } n   } n   if (index2 < min(a.size(), b.size())) { n     while (count < k) { n       count++; n       ans += (a[index2] + b[index2]); n       index2++; n     } n   } n   cout << ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   while (t--) { n     solve(); n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, V, i, x, y, z, tot, e[505], fir[505], ne[505 * 2], la[505 * 2], n     va[505 * 2]; n double v, o, a[505][505], b[505], c[505]; n void ins(int x, int y, int z) { n   la[++tot] = y; n   ne[tot] = fir[x]; n   fir[x] = tot; n   va[tot] = z; n } n void dfs(int A, int x, int fa, long long z) { n   if (z <= V) a[x][A] = 1; n   for (int i = fir[x]; i; i = ne[i]) n     if (la[i] != fa) dfs(A, la[i], x, z + va[i]); n } n void pivot(int l, int e) { n   int i, j; n   for (a[l][e] = 1 / a[l][e], b[l] *= a[l][e], i = 1; i <= n; i++) n     if (i != e) a[l][i] *= a[l][e]; n   for (i = 1; i <= m; i++) n     if (i != l && fabs(a[i][e]) > 1e-7) { n       for (b[i] -= a[i][e] * b[l], j = 1; j <= n; j++) n         if (j != e) a[i][j] -= a[i][e] * a[l][j]; n       a[i][e] *= -a[l][e]; n     } n   for (v += c[e] * b[l], i = 1; i <= n; i++) n     if (i != e) c[i] -= c[e] * a[l][i]; n   c[e] *= -a[l][e]; n } n double simplex() { n   for (int i, j, e, l; 1;) { n     for (i = 1; i <= n; i++) n       if (c[i] > 1e-7) break; n     if ((e = i) == n + 1) return v; n     double tmp = 1e99; n     for (i = 1; i <= m; i++) n       if (a[i][e] > 1e-7 && tmp > b[i] / a[i][e]) tmp = b[i] / a[i][e], l = i; n     if (tmp == 1e99) break; n     pivot(l, e); n   } n   return 1e99; n } n int main() { n   for (scanf( %d%d , &n, &V), i = 1; i <= n; i++) n     scanf( %d , &e[i]), b[i] = !e[i], a[i][n + 1] = 1, a[i][n + 2] = -1, n                         c[n + 1] += e[i], c[n + 2] -= e[i], c[i] = 1; n   for (i = 1; i < n; i++) n     scanf( %d%d%d , &x, &y, &z), ins(x, y, z), ins(y, x, z); n   for (i = 1; i <= n; i++) dfs(i, i, 0, 0); n   m = n; n   n += 2; n   o = simplex(); n   if (o == 1e99) n     puts( -1 ); n   else n     printf( %.0lf , o); n } n


#include <bits/stdc++.h> n using namespace std; n int M[17], R[17], n, sum; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> M[i]; n   for (int i = 1; i <= n; i++) cin >> R[i]; n   for (int i = 1; i <= 720719; i++) n     for (int j = 1; j <= n; j++) { n       if (i % M[j] == R[j]) { n         sum++; n         break; n       } n     } n   double ans = 1.0 * sum / 720720; n   printf( %.6lf , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int res = 0; n   int n, x; n   cin >> n >> x; n   for (int i = 1; i <= n; ++i) { n     if (x % i == 0 && x / i <= n) ++res; n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n int op[1000005]; n bool value[1000005]; n bool change[1000005]; n long req[1000005][2]; n bool calc(long i) { n   if (i <= 0) return false; n   value[i] = false; n   if (op[i] == 4) { n     value[i] = (req[i][0] == 1); n   } n   if (op[i] == 3) { n     value[i] = !(calc(req[i][0])); n   } n   if (op[i] == 2) { n     value[i] = (calc(req[i][0]) ^ calc(req[i][1])); n   } n   if (op[i] == 1) { n     value[i] = (calc(req[i][0]) | calc(req[i][1])); n   } n   if (op[i] == 0) { n     value[i] = (calc(req[i][0]) & calc(req[i][1])); n   } n   return value[i]; n } n void backTrace(long i, bool vt, bool vf) { n   if (op[i] == 4) { n     if (value[i]) n       change[i] = vf; n     else n       change[i] = vt; n   } n   if (op[i] == 3) { n     backTrace(req[i][0], vf, vt); n   } n   if (op[i] == 2) { n     if (value[req[i][1]]) { n       backTrace(req[i][0], vf, vt); n     } else { n       backTrace(req[i][0], vt, vf); n     } n     if (value[req[i][0]]) { n       backTrace(req[i][1], vf, vt); n     } else { n       backTrace(req[i][1], vt, vf); n     } n   } n   if (op[i] == 1) { n     if (value[req[i][1]]) { n       backTrace(req[i][0], vt, vt); n     } else { n       backTrace(req[i][0], vt, vf); n     } n     if (value[req[i][0]]) { n       backTrace(req[i][1], vt, vt); n     } else { n       backTrace(req[i][1], vt, vf); n     } n   } n   if (op[i] == 0) { n     if (value[req[i][1]]) { n       backTrace(req[i][0], vt, vf); n     } else { n       backTrace(req[i][0], vf, vf); n     } n     if (value[req[i][0]]) { n       backTrace(req[i][1], vt, vf); n     } else { n       backTrace(req[i][1], vf, vf); n     } n   } n } n int main() { n   long n; n   cin >> n; n   string s; n   for (long i = 1; i <= n; ++i) { n     cin >> s; n     if (s ==  AND ) { n       op[i] = 0; n       cin >> req[i][0] >> req[i][1]; n     } else if (s ==  OR ) { n       op[i] = 1; n       cin >> req[i][0] >> req[i][1]; n     } else if (s ==  XOR ) { n       op[i] = 2; n       cin >> req[i][0] >> req[i][1]; n     } else if (s ==  NOT ) { n       op[i] = 3; n       cin >> req[i][0]; n     } else { n       op[i] = 4; n       cin >> req[i][0]; n     } n   } n   calc(1); n   backTrace(1, true, false); n   for (long i = 1; i <= n; ++i) { n     if (op[i] == 4) { n       if (change[i]) n         cout << 1; n       else n         cout << 0; n     } n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, a[N], lm[N], rm[N], q[N]; n vector<int> tree[35]; n long long ans; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     for (int j = 0; (1 << j) <= a[i]; j++) n       if (a[i] & (1 << j)) tree[j].push_back(i); n   } n   int tail = 0; n   for (int i = 1; i <= n; i++) { n     while (tail && a[q[tail - 1]] < a[i]) tail--; n     lm[i] = tail ? q[tail - 1] + 1 : 1; n     q[tail++] = i; n   } n   tail = 0; n   for (int i = n; i; i--) { n     while (tail && a[q[tail - 1]] <= a[i]) tail--; n     rm[i] = tail ? q[tail - 1] - 1 : n; n     q[tail++] = i; n   } n   for (int i = 1; i <= n; i++) { n     if (lm[i] == rm[i]) continue; n     int tl = lm[i] - 1, tr = rm[i] + 1; n     for (int j = 0; (1 << j) <= a[i]; j++) n       if (!((1 << j) & a[i])) { n         if (tree[j].size() == 0) continue; n         auto it = lower_bound(tree[j].begin(), tree[j].end(), i); n         if (it != tree[j].begin()) { n           it--; n           tl = max(tl, *it); n         } n         it = upper_bound(tree[j].begin(), tree[j].end(), i); n         if (it != tree[j].end()) tr = min(tr, *it); n       } n     ans += n         1ll * (i - lm[i] + 1) * (rm[i] - i + 1ll) - 1ll * (i - tl) * (tr - i); n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i; n   char c; n   while (~scanf( %d , &n)) { n     c =  a ; n     for (i = 1; i <= n; i++) { n       printf( %c , c); n       c++; n       if (c ==  e ) c =  a ; n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n bool isPrime(long long int n) { n   for (long long int i = 2; i * i <= n; i++) { n     if (n % i == 0) { n       return false; n     } n   } n   return true; n } n bool isPowerOfTwo(long long int x) { return (x && (!(x & (x - 1)))); } n long long int countDigit(long long int num) { return (floor(log10(num)) + 1); } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int test_case = 1; n   while (test_case--) { n     long long int n; n     cin >> n; n     long long int arr[n]; n     for (long long int i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     long long int maxx = 0; n     vector<long long int> ans; n     for (long long int i = n - 1; i >= 0; i--) { n       if (arr[i] <= maxx) { n         ans.push_back(maxx - arr[i] + 1); n       } else { n         ans.push_back(0); n       } n       maxx = max(arr[i], maxx); n     } n     reverse(ans.begin(), ans.end()); n     for (auto it : ans) { n       cout << it <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void shift(vector<int> &ans) { n   for (int i = 0; i < ans.size() - 1; i++) { n     ans[i] = ans[i + 1]; n   } n } n bool check(vector<int> ans, int c) { n   for (int i = 0; i < ans.size(); i++) { n     if (c == ans[i]) return true; n   } n   return false; n } n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   vector<int> ans; n   for (int i = 0; i < n; i++) { n     if (ans.size() < k && (!check(ans, v[i]))) n       ans.push_back(v[i]); n     else if (!check(ans, v[i])) { n       shift(ans); n       ans[ans.size() - 1] = v[i]; n     } n   } n   cout << ans.size() << endl; n   for (int i = ans.size() - 1; i >= 0; i--) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[3]; n long long kq; n void nhap() { cin >> a[0] >> a[1] >> a[2]; } n void cbi() { sort(a, a + 3); } n void xuli() { n   if (a[0] == a[1] && a[1] == a[2]) { n     cout << a[0]; n     exit(0); n   } n   if (a[0] + a[1] < a[2] / 2) n     kq = 1ll * a[0] + a[1]; n   else n     kq = 1ll * (a[0] + a[1] + a[2]) / 3; n } n void ghikq() { cout << kq; } n int main() { n   nhap(); n   cbi(); n   xuli(); n   ghikq(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int INF = 0x7ffffff; n const int magic = 348; n string s1, s2; n string s; n int n; n int nxt1[200], nxt2[200]; n int main() { n   cin >> s1 >> s2; n   cin >> n; n   n %= 4; n   nxt1[94] = 62; n   nxt1[62] = 118; n   nxt1[118] = 60; n   nxt1[60] = 94; n   nxt2[94] = 60; n   nxt2[60] = 118; n   nxt2[118] = 62; n   nxt2[62] = 94; n   bool f1 = false, f2 = false; n   s = s1; n   for (int i = 1; i <= n; i++) s[0] = nxt1[s[0]]; n   if (s == s2) f1 = true; n   s = s1; n   for (int i = 1; i <= n; i++) s[0] = nxt2[s[0]]; n   if (s == s2) f2 = true; n   if (f1 && f2) { n     cout <<  undefined  << endl; n   } else { n     if (f1) n       cout <<  cw  << endl; n     else n       cout <<  ccw  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 300000; n const int MAX_E2 = 1 << 20; n struct Elm { n   int l, r, v; n   Elm() {} n   Elm(int _l, int _r, int _v) : l(_l), r(_r), v(_v) {} n   Elm(int _l) : l(_l), r(_l), v(1) {} n   Elm operator+(const Elm &e) const { n     if (v == 0) return e; n     if (e.v == 0) return *this; n     Elm a; n     a.l = l, a.r = e.r; n     a.v = v + e.v - ((r < e.l) ? 1 : 0); n     return a; n   } n }; n template <typename T, const int MAX_E2> n struct SegTreeSum { n   int n, e2; n   T nodes[MAX_E2], z; n   SegTreeSum() {} n   void init(int n, T _z) { n     z = _z; n     for (e2 = 1; e2 < n; e2 <<= 1) n       ; n     fill(nodes, nodes + MAX_E2, z); n   } n   T &get(int i) { return nodes[e2 - 1 + i]; } n   void setall() { n     for (int j = e2 - 2; j >= 0; j--) n       nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2]; n   } n   void set(int i, T v) { n     int j = e2 - 1 + i; n     nodes[j] = v; n     while (j > 0) { n       j = (j - 1) / 2; n       nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2]; n     } n   } n   T sum_range(int r0, int r1, int k, int i0, int i1) { n     if (r1 <= i0 || i1 <= r0) return z; n     if (r0 <= i0 && i1 <= r1) return nodes[k]; n     int im = (i0 + i1) / 2; n     T v0 = sum_range(r0, r1, k * 2 + 1, i0, im); n     T v1 = sum_range(r0, r1, k * 2 + 2, im, i1); n     return v0 + v1; n   } n   T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); } n }; n int as[MAX_N]; n SegTreeSum<Elm, MAX_E2> st; n int main() { n   int n; n   scanf( %d , &n); n   st.init(n, Elm(0, 0, 0)); n   for (int i = 0; i < n; i++) { n     scanf( %d , as + i); n     as[i]--; n     st.get(as[i]) = Elm(i, i, 1); n   } n   st.setall(); n   int q; n   scanf( %d , &q); n   while (q--) { n     int p, x, y; n     scanf( %d%d%d , &p, &x, &y); n     if (p == 1) { n       x--; n       Elm e = st.sum_range(x, y); n       printf( %d n , e.v); n     } else { n       x--, y--; n       int ax = as[x], ay = as[y]; n       swap(as[x], as[y]); n       st.set(ax, Elm(y)); n       st.set(ay, Elm(x)); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[100005], b[100005]; n vector<int> g[100005]; n int next_id, from[100005], to[100005]; n bitset<1000> tree[4 * 100005], primes; n int lazy[4 * 100005]; n void dfs(int u) { n   from[u] = next_id++; n   b[from[u]] = a[u] % m; n   for (int i = 0; i < g[u].size(); i++) n     if (from[g[u][i]] == -1) dfs(g[u][i]); n   to[u] = next_id; n } n void rot(bitset<1000> &b, int k) { n   b = (b << k) | (b >> (m - k)); n   b ^= (b >> m) << m; n } n void init(int no, int l, int r) { n   if (r - l == 1) { n     tree[no].set(b[l]); n     return; n   } n   int m = (l + r) / 2; n   init(2 * no, l, m); n   init(2 * no + 1, m, r); n   tree[no] = tree[2 * no] | tree[2 * no + 1]; n } n void prop(int no, int l, int r) { n   lazy[no] %= m; n   if (lazy[no] > 0) { n     rot(tree[no], lazy[no]); n     if (r - l > 1) { n       lazy[2 * no] += lazy[no]; n       lazy[2 * no + 1] += lazy[no]; n     } n     lazy[no] = 0; n   } n } n void update(int no, int l, int r, int a, int b, int x) { n   if (a <= l && r <= b) { n     lazy[no] += x; n     return; n   } n   prop(no, l, r); n   int m = (l + r) / 2; n   if (a < m) update(2 * no, l, m, a, b, x); n   if (m < b) update(2 * no + 1, m, r, a, b, x); n   prop(2 * no, l, m); n   prop(2 * no + 1, m, r); n   tree[no] = tree[2 * no] | tree[2 * no + 1]; n } n bitset<1000> query(int no, int l, int r, int a, int b) { n   prop(no, l, r); n   if (a <= l && r <= b) return tree[no]; n   int m = (l + r) / 2; n   bitset<1000> ret; n   if (a < m) ret = query(2 * no, l, m, a, b); n   if (m < b) ret |= query(2 * no + 1, m, r, a, b); n   return ret; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     u--, v--; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   memset(from, -1, sizeof(from)); n   next_id = 0; n   dfs(0); n   memset(lazy, 0, sizeof(lazy)); n   init(1, 0, n); n   primes[2].flip(); n   for (int i = 3; i < m; i += 2) primes.set(i); n   for (int i = 3; i * i < m; i += 2) n     if (primes[i]) n       for (int j = i * i; j < m; j += i) primes.reset(j); n   int q, op, v, x; n   scanf( %d , &q); n   while (q--) { n     scanf( %d %d , &op, &v); n     v--; n     if (op == 1) { n       scanf( %d , &x); n       update(1, 0, n, from[v], to[v], x % m); n     } else { n       bitset<1000> bs = query(1, 0, n, from[v], to[v]) & primes; n       printf( %d n , (int)bs.count()); n     } n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, m, u, v; n vector<int> g[100005], gr[100005]; n vector<int> order; n bool used[100005]; n int G[100005], C[100005], k = 0; n void dfs1(int x) { n   used[x] = 1; n   for (int(i) = (0); (i) < (g[x].size()); (i)++) { n     int nn = g[x][i]; n     if (!used[nn]) dfs1(nn); n   } n   order.push_back(x); n } n int dfs2(int x) { n   used[x] = 1; n   G[x] = k; n   int ans = 1, nn; n   for (int(i) = (0); (i) < (gr[x].size()); (i)++) { n     nn = gr[x][i]; n     if (!used[nn]) ans += dfs2(nn); n   } n   return ans; n } n int dfs3(int x) { n   used[x] = 1; n   int ans = (C[G[x]] == 1), nn; n   for (int(i) = (0); (i) < (g[x].size()); (i)++) { n     nn = g[x][i]; n     if (!used[nn]) ans &= dfs3(nn); n   } n   for (int(i) = (0); (i) < (gr[x].size()); (i)++) { n     nn = gr[x][i]; n     if (!used[nn]) ans &= dfs3(nn); n   } n   return ans; n } n int main() { n   scanf( %d%d , &(n), &(m)); n   for (int(i) = (0); (i) < (m); (i)++) { n     scanf( %d%d , &(u), &(v)); n     g[u].push_back(v); n     gr[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) n     if (!used[i]) dfs1(i); n   reverse(order.begin(), order.end()); n   memset((used), (0), sizeof(used)); n   for (int(i) = (0); (i) < (n); (i)++) n     if (!used[order[i]]) { n       C[k] = dfs2(order[i]); n       k++; n     } n   memset((used), (0), sizeof(used)); n   int ans = n; n   for (int i = 0; i < n; i++) n     if (!used[order[i]]) ans -= dfs3(order[i]); n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[100001], b[100001]; n   int n, i, x, y; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> a[i]; n     b[i] = a[i]; n   } n   sort(b, b + n); n   x = 0; n   while (a[x] == b[x] && x < n - 1) { n     x++; n   } n   y = n - 1; n   while (a[y] == b[y] && y > 0) { n     y--; n   } n   if (x > y) { n     x = 0; n     y = 0; n   } n   for (i = 0; i <= (y - x) / 2; i++) { n     swap(a[x + i], a[y - i]); n   } n   for (i = 0; i < n; i++) n     if (a[i] != b[i]) { n       i = n + 10; n     } n   if (i < n + 10) { n     cout <<  yes  << endl; n     cout << x + 1 <<     << y + 1; n   } else { n     cout <<  no  << endl; n   } n } n


#include <bits/stdc++.h> n using int64 = long long; n constexpr int Maxn = 200000; n constexpr int p = 1000000007; n template <class _Tp1, class _Tp2> n inline void inc(_Tp1& x, const _Tp2& y) { n   x += y; n   (p <= x) && (x -= p); n } n template <class _Tp1, class _Tp2> n inline void dec(_Tp1& x, const _Tp2& y) { n   x -= y; n   (x < 0) && (x += p); n } n template <class _Tp> n inline int md(const _Tp& v) { n   return v < 0 ? ((0 <= p + v) ? (p + v) : (v % p + p)) : (v < p) ? v : (v % p); n } n namespace IOManager { n constexpr int FILESZ(131072); n unsigned char buf[FILESZ]; n const unsigned char *ibuf = buf, *tbuf = buf; n struct IOManager { n   inline unsigned char gc() { n     return (ibuf == tbuf) && n                    (tbuf = (ibuf = buf) + fread(buf, 1, FILESZ, stdin), n                     ibuf == tbuf) n                ? EOF n                : *ibuf++; n   } n   template <class _Tp> n   inline operator _Tp() { n     _Tp s = 0u; n     unsigned char c = gc(), w = 0; n     for (; c < 48; c = gc()) (c == 45) && (w = 1); n     for (; c > 47; c = gc()) s = (_Tp)(s * 10u + c - 48u); n     return w ? -s : s; n   } n }; n }  // namespace IOManager n IOManager::IOManager io; n const int n = io; n int64 bit[Maxn + 1]; n inline void update(const int& x, const int& d) { n   for (int i = x; i <= n; i += i & -i) bit[i] += d; n } n inline int64 sum(const int& x) { n   int64 s = 0; n   for (int i = x; i; i -= i & -i) s += bit[i]; n   return s; n } n int bitp[Maxn + 1]; n inline void updatep(const int& x, const int& d) { n   for (int i = x; i <= n; i += i & -i) inc(bitp[i], d); n } n inline int sump(const int& x) { n   int64 s = 0; n   for (int i = x; i; i -= i & -i) s += bitp[i]; n   return s % p; n } n int a[Maxn + 1], w[Maxn + 1]; n int main() { n   int q = io; n   for (int i = 1; i <= n; ++i) a[i] = (int)io - i; n   for (int i = 1; i <= n; ++i) n     w[i] = io, update(i, w[i]), updatep(i, (int64)w[i] * a[i] % p); n   int64 sl, sm, sr; n   for (int x, y, l, m, r; q; --q) n     if (x = io, y = io, x < 0) { n       int d = y - w[x = -x]; n       w[x] = y; n       update(x, d); n       updatep(x, md((int64)d * a[x])); n     } else { n       l = x; n       r = y; n       sl = sum(l - 1); n       sr = sum(r) - sl; n       for (; l < r;) n         ((sum(m = (l + r) >> 1) - sl) * 2 < sr) ? (l = m + 1) : (r = m); n       sm = sum(m = l); n       sr += sl; n       printf( %d n , md((int64)(sump(y) - sump(m) * 2ll + n                                 (int64)(sm - sr + sm - sl) % p * a[m] % p + n                                 sump(x - 1)))); n     } n   return 0; n } n


#include <bits/stdc++.h> n const int INF = 0x3f3f3f3f; n const int MAXN = 1e3 + 10; n using namespace std; n char str[MAXN][MAXN]; n int data[MAXN]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; i++) cin >> str[i]; n   for (int i = 0; i < m; i++) cin >> data[i]; n   long long ans = 0; n   for (int i = 0; i < m; i++) { n     int temp[10] = {0}, tmp = -1; n     for (int j = 0; j < n; j++) temp[str[j][i] -  A ]++; n     for (int j = 0; j < 5; j++) tmp = max(tmp, temp[j]); n     ans += (long long)tmp * data[i]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long ans = 0; n void solve1(vector<int> &ar, bool yeah) { n   int sz = ar.size(); n   int mid = sz / 2 + yeah * (sz % 2) - 1; n   int L = mid; n   int minL = ar[mid]; n   int maxL = ar[mid]; n   int minR = (1 << 30); n   int maxR = 0; n   for (int R = mid + 1; R < sz; ++R) { n     minR = min(minR, ar[R]); n     maxR = max(maxR, ar[R]); n     while (L > 0 && ar[L - 1] < maxR && ar[L - 1] > minR) { n       minL = min(minL, ar[L - 1]); n       maxL = max(maxL, ar[--L]); n     } n     if (maxR > maxL && minR < minL) ans += (L <= (R - maxR + minR)); n   } n } n void solve2(vector<int> ar, bool yeah) { n   int sz = ar.size(); n   int mid = sz / 2 + yeah * (sz % 2) - 1; n   int L = mid; n   int minL = ar[mid]; n   int maxL = ar[mid]; n   int minR = (1 << 30); n   int maxR = 0; n   map<int, int> m; n   m[minL - L]++; n   int dL = L; n   for (int R = mid + 1; R < sz; ++R) { n     minR = min(minR, ar[R]); n     maxR = max(maxR, ar[R]); n     while (L > 0 && ar[L - 1] < maxR) { n       minL = min(minL, ar[L - 1]); n       maxL = max(maxL, ar[--L]); n       ar[L] = minL; n       m[ar[L] - L]++; n     } n     while (dL >= L && ar[dL] > minR) { n       m[ar[dL] - dL]--; n       --dL; n     } n     if (maxR > maxL) { n       ans += m[maxR - R]; n       int pmr = 0; n     } n   } n } n int ar[300005]; n void go(int a, int b) { n   if (a == b) { n     ans++; n     return; n   } n   vector<int> V; n   for (int i = a; i <= b; ++i) V.push_back(ar[i]); n   solve1(V, 1); n   reverse(V.begin(), V.end()); n   solve1(V, 0); n   reverse(V.begin(), V.end()); n   solve2(V, 1); n   reverse(V.begin(), V.end()); n   solve2(V, 0); n   go(a, (a + b) / 2); n   go((a + b) / 2 + 1, b); n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     ar[a] = b; n   } n   go(1, n); n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long ax, ay, bx, by, cx, cy, flag = 0; n long long f(long long x1, long long y1, long long x2, long long y2) { n   long long res = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1); n   return res; n } n bool isColinear() { n   long long res1 = (by - ay) * (cx - bx); n   long long res2 = (cy - by) * (bx - ax); n   if (res1 == res2) n     return true; n   else n     return false; n } n int main() { n   cin >> ax >> ay >> bx >> by >> cx >> cy; n   if (isColinear() == true) n     flag = 0; n   else { n     long long dis1 = f(ax, ay, bx, by); n     long long dis2 = f(bx, by, cx, cy); n     if (dis1 == dis2) n       flag = 1; n     else n       flag = 0; n   } n   if (flag == 1) n     printf( Yes n ); n   else n     printf( No n ); n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = LLONG_MAX / 2; n int extractMin(set<int> &mySet) { n   set<int>::iterator it = mySet.begin(); n   int ans = *(it); n   mySet.erase(it); n   return ans; n } n int search(set<int> &mySet, int index) { n   set<int>::iterator it = mySet.lower_bound(index); n   if (it == mySet.end()) { n     return -1; n   } n   int ans = *(it); n   return ans; n } n void eliminate(set<int> &mySet, int index) { n   set<int>::iterator it = mySet.lower_bound(index); n   if (it == mySet.end()) { n     assert(false); n   } n   mySet.erase(it); n } n void notFound() { cout << -1 <<   n ; } n void impr(vector<vector<int> > ans) { n   printf( %d n , ((int)(ans).size())); n   for (int i = 0; i < ((int)(ans).size()); i++) { n     printf( %d , ((int)(ans[i]).size())); n     for (int j = 0; j < ((int)(ans[i]).size()); j++) { n       printf(  %d , ans[i][j] + 1); n     } n     puts(  ); n   } n } n void solve(string str) { n   int n = ((int)(str).size()); n   set<int> zeroes, ones; n   for (int i = 0; i < n; i++) { n     if (str[i] ==  0 ) { n       zeroes.insert(i); n     } else { n       ones.insert(i); n     } n   } n   vector<vector<int> > ans; n   while (true) { n     if (zeroes.empty() && ones.empty()) { n       break; n     } n     if (zeroes.empty()) { n       notFound(); n       return; n     } n     int curZero = extractMin(zeroes); n     vector<int> seq(1, curZero); n     while (true) { n       int curOne = search(ones, curZero + 1); n       curZero = search(zeroes, curOne + 1); n       if (curOne == -1 || curZero == -1) { n         break; n       } n       seq.push_back(curOne); n       seq.push_back(curZero); n       eliminate(ones, curOne); n       eliminate(zeroes, curZero); n     } n     ans.push_back(seq); n   } n   impr(ans); n } n int main() { n   string str; n   while (cin >> str) { n     solve(str); n   } n } n


#include <bits/stdc++.h> n  n template <typename T, int NDIMS> struct tensor_view { n  static_assert(NDIMS >= 0,  NDIMS must be nonnegative ); n  n protected: n  std::array<int, NDIMS> shape; n  std::array<int, NDIMS> strides; n  T* data; n  n  tensor_view(std::array<int, NDIMS> shape_, std::array<int, NDIMS> strides_, T* data_) : shape(shape_), strides(strides_), data(data_) {} n  n public: n  tensor_view() : shape{0}, strides{0}, data(nullptr) {} n  n protected: n  int flatten_index(std::array<int, NDIMS> idx) const { n   int res = 0; n   for (int i = 0; i < NDIMS; i++) { res += idx[i] * strides[i]; } n   return res; n  } n  int flatten_index_checked(std::array<int, NDIMS> idx) const { n   int res = 0; n   for (int i = 0; i < NDIMS; i++) { n    assert(0 <= idx[i] && idx[i] < shape[i]); n    res += idx[i] * strides[i]; n   } n   return res; n  } n  n public: n  T& operator[] (std::array<int, NDIMS> idx) const { n   return data[flatten_index(idx)]; n  } n  T& at(std::array<int, NDIMS> idx) const { n   return data[flatten_index_checked(idx)]; n  } n  n  template <int D = NDIMS> n  typename std::enable_if<(0 < D), tensor_view<T, NDIMS-1>>::type operator[] (int idx) const { n   std::array<int, NDIMS-1> nshape; std::copy(shape.begin()+1, shape.end(), nshape.begin()); n   std::array<int, NDIMS-1> nstrides; std::copy(strides.begin()+1, strides.end(), nstrides.begin()); n   T* ndata = data + (strides[0] * idx); n   return tensor_view<T, NDIMS-1>(nshape, nstrides, ndata); n  } n  template <int D = NDIMS> n  typename std::enable_if<(0 < D), tensor_view<T, NDIMS-1>>::type at(int idx) const { n   assert(0 <= idx && idx < shape[0]); n   return operator[](idx); n  } n  n  template <int D = NDIMS> n  typename std::enable_if<(0 == D), T&>::type operator * () const { n   return *data; n  } n  n  template <typename U, int D> friend struct tensor_view; n  template <typename U, int D> friend struct tensor; n }; n  n template <typename T, int NDIMS> struct tensor { n  static_assert(NDIMS >= 0,  NDIMS must be nonnegative ); n  n protected: n  std::array<int, NDIMS> shape; n  std::array<int, NDIMS> strides; n  int len; n  T* data; n  n public: n  tensor() : shape{0}, strides{0}, len(0), data(nullptr) {} n  n  explicit tensor(std::array<int, NDIMS> shape_, const T& t = T()) { n   shape = shape_; n   strides[NDIMS-1] = 1; n   for (int i = NDIMS-1; i > 0; i--) { n    strides[i-1] = strides[i] * shape[i]; n   } n   len = strides[0] * shape[0]; n   data = new T[len]; n   std::fill(data, data + len, t); n  } n  n  tensor(const tensor& o) : shape(o.shape), strides(o.strides), len(o.len), data(new T[len]) { n   for (int i = 0; i < len; i++) { n    data[i] = o.data[i]; n   } n  } n  n  tensor& operator=(tensor&& o) noexcept { n   using std::swap; n   swap(shape, o.shape); n   swap(strides, o.strides); n   swap(len, o.len); n   swap(data, o.data); n   return *this; n  } n  tensor(tensor&& o) : tensor() { n   *this = std::move(o); n  } n  tensor& operator=(const tensor& o) { n   return *this = tensor(o); n  } n  ~tensor() { delete[] data; } n  n  using view_t = tensor_view<T, NDIMS>; n  view_t view() { n   return tensor_view<T, NDIMS>(shape, strides, data); n  } n  operator view_t() { n   return view(); n  } n  n  using const_view_t = tensor_view<const T, NDIMS>; n  const_view_t view() const { n   return tensor_view<const T, NDIMS>(shape, strides, data); n  } n  operator const_view_t() const { n   return view(); n  } n  n  T& operator[] (std::array<int, NDIMS> idx) { return view()[idx]; } n  T& at(std::array<int, NDIMS> idx) { return view().at(idx); } n  const T& operator[] (std::array<int, NDIMS> idx) const { return view()[idx]; } n  const T& at(std::array<int, NDIMS> idx) const { return view().at(idx); } n  n  template <int D = NDIMS> n  typename std::enable_if<(0 < D), tensor_view<T, NDIMS-1>>::type operator[] (int idx) { n   return view()[idx]; n  } n  template <int D = NDIMS> n  typename std::enable_if<(0 < D), tensor_view<T, NDIMS-1>>::type at(int idx) { n   return view().at(idx); n  } n  n  template <int D = NDIMS> n  typename std::enable_if<(0 < D), tensor_view<const T, NDIMS-1>>::type operator[] (int idx) const { n   return view()[idx]; n  } n  template <int D = NDIMS> n  typename std::enable_if<(0 < D), tensor_view<const T, NDIMS-1>>::type at(int idx) const { n   return view().at(idx); n  } n  n  template <int D = NDIMS> n  typename std::enable_if<(0 == D), T&>::type operator * () { n   return *view(); n  } n  template <int D = NDIMS> n  typename std::enable_if<(0 == D), const T&>::type operator * () const { n   return *view(); n  } n }; n  n template <typename T> void setmax(T& a, T b) { if (b > a) a = b; } n  n int main() { n  using namespace std; n  ios_base::sync_with_stdio(false), cin.tie(nullptr); n  n  int N, Q; cin >> N >> Q; n  vector<int> A(N); n  for (int i = 0; i < N; i++) { cin >> A[i]; A[i] += i; } n  n  const int MAXK = 30; n  int S = 0; n  while (!((1 << S) >= N)) S++; n  n  tensor<pair<int, int>, 3> dp({S+1, N, MAXK+1}); n  { n   vector<pair<int, int>> stk; stk.reserve(N); n   for (int i = N-1; i >= 0; i--) { n    while (!stk.empty() && A[i] > stk.back().first) stk.pop_back(); n    stk.push_back({A[i], i}); n    int lo = -1, hi = int(stk.size())-1; n    while (hi - lo > 1) { n     int md = (lo + hi) / 2; n     if (stk[md].second <= A[i]) hi = md; n     else lo = md; n    } n    pair<int, int> cur = stk[hi]; n    dp[{0, i, 0}] = cur; n    for (int k = 1; k <= MAXK; k++) { n     if (A[i] + k < N) setmax(cur, {A[A[i]+k], A[i]+k}); n     dp[{0, i, k}] = cur; n    } n   } n  } n  n  for (int s = 1; s <= S; s++) { n   for (int i = 0; i < N; i++) { n    for (int k1 = 0; k1 <= MAXK; k1++) { n     int j = dp[{s-1, i, k1}].second; n     for (int k2 = 0; k1+k2 <= MAXK; k2++) { n      setmax(dp[{s, i, k1+k2}], dp[{s-1, j, k2}]); n     } n    } n   } n  } n  n  while (Q--) { n   int L, R, K; cin >> L >> R >> K; L--, R--; n   cout << [&]() -> int { n    if (L == R) return 0; n    vector<pair<int, int>> cur(K+1, {A[L], L}); n    vector<pair<int, int>> nxt(K+1); n    int resp = 0; n    auto is_done = [&](const vector<pair<int, int>>& v) { n     for (int k = 0; k <= K; k++) { n      if (v[k].first + (K-k) >= R) return true; n     } n     return false; n    }; n    if (is_done(cur)) return 1; n    int s = 0; n    while (true) { n     for (int k = 0; k <= K; k++) { n      nxt[k] = dp[{s, L, k}]; n     } n     if (is_done(nxt)) { n      break; n     } else { n      resp = (1 << s); n      s++; n      std::swap(nxt, cur); n     } n    } n  n    if (s) s--; n  n    while (s) { n     s--; n     std::copy(cur.begin(), cur.end(), nxt.begin()); n     for (int k = 0; k <= K; k++) { n      for (int l = 0; l <= K-k; l++) { n       setmax(nxt[l+k], dp[{s, cur[k].second, l}]); n      } n     } n     if (is_done(nxt)) { n      // do nothing n     } else { n      resp += 1 << s; n      std::swap(cur, nxt); n     } n    } n    resp++; n    return resp + 1; n   }() <<   n ; n  } n  n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int anc[200007]; n int sz[200007]; n int x[200005], y[200005]; n vector<int> g[200005]; n int get_set(int v) { n   if (v == anc[v]) { n     return v; n   } n   return anc[v] = get_set(anc[v]); n } n void unite_sets(int u, int v) { n   u = get_set(u); n   v = get_set(v); n   if (u == v) { n     return; n   } n   if (sz[u] < sz[v]) { n     swap(u, v); n   } n   sz[u] = sz[u] + sz[v]; n   anc[v] = u; n } n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     cin >> x[i] >> y[i]; n     g[x[i]].push_back(y[i]); n     g[y[i]].push_back(x[i]); n   } n   for (int i = 0; i <= n; i++) { n     sz[i] = 1; n     anc[i] = i; n   } n   int mx = 0; n   int z = 1; n   for (int i = 1; i <= n; i++) { n     if (g[i].size() > mx) { n       mx = g[i].size(); n       z = i; n     } n   } n   for (int i = 0; i < g[z].size(); i++) { n     if (get_set(z) != get_set(g[z][i])) { n       unite_sets(z, g[z][i]); n       cout << z <<     << g[z][i] << endl; n     } n   } n   for (int i = 0; i < m; i++) { n     if (get_set(x[i]) != get_set(y[i])) { n       unite_sets(x[i], y[i]); n       cout << x[i] <<     << y[i] << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   vector<long long> vec(n + 1); n   for (int i = 1; i <= n; i++) cin >> vec[i]; n   vector<long long> a; n   for (int i = 1; i < n; i++) { n     if (i & 1) n       a.push_back(abs(vec[i] - vec[i + 1])); n     else n       a.push_back(-1 * abs(vec[i] - vec[i + 1])); n   } n   vector<long long> dp((int)a.size()); n   dp[0] = a[0]; n   long long res = dp[0]; n   for (int i = 1; i < dp.size(); i++) { n     dp[i] = max(a[i], dp[i - 1] + a[i]); n     res = max(res, dp[i]); n   } n   dp.clear(); n   a.clear(); n   for (int i = 1; i < n; i++) { n     if (!(i & 1)) n       a.push_back(abs(vec[i] - vec[i + 1])); n     else n       a.push_back(-1 * abs(vec[i] - vec[i + 1])); n   } n   dp = vector<long long>((int)a.size()); n   dp[0] = a[0]; n   long long res2 = dp[0]; n   for (int i = 1; i < dp.size(); i++) { n     dp[i] = max(a[i], dp[i - 1] + a[i]); n     res2 = max(res2, dp[i]); n   } n   cout << max(res, res2) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long x[100010]; n long long y[100010]; n long long dispoint(long long x1, long long y1, long long x2, long long y2) { n   return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); n } n bool issame(long long x1, long long y1, long long x2, long long y2, n             long long x3, long long y3) { n   long long xa = x2 - x1; n   long long ya = y2 - y1; n   long long xb = x3 - x1; n   long long yb = y3 - y1; n   if (xa * yb == xb * ya) n     return 1; n   else n     return 0; n } n double disline(long long x1, long long y1, long long x2, long long y2, n                long long x3, long long y3) { n   double xa = x1 - x3; n   double ya = y1 - y3; n   double xb = x2 - x3; n   double yb = y2 - y3; n   double l = sqrt((xa - xb) * (xa - xb) + (ya - yb) * (ya - yb)); n   double s = abs(xa * yb - xb * ya); n   double h = s / l; n   return h; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %I64d%I64d , &x[i], &y[i]); n   int pos1 = 2; n   long long mindis = dispoint(x[1], y[1], x[2], y[2]); n   for (int i = 3; i <= n; i++) { n     long long tmpdis = dispoint(x[1], y[1], x[i], y[i]); n     if (tmpdis < mindis) { n       mindis = tmpdis; n       pos1 = i; n     } n   } n   int pos2 = 0; n   double mindis2 = 1e18; n   for (int i = 2; i <= n; i++) { n     if (i == pos1) continue; n     if (issame(x[1], y[1], x[pos1], y[pos1], x[i], y[i])) continue; n     double tmpdis = disline(x[1], y[1], x[pos1], y[pos1], x[i], y[i]); n     if (tmpdis < mindis2) { n       mindis2 = tmpdis; n       pos2 = i; n     } n   } n   printf( 1 %d %d n , pos1, pos2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     vector<int> ans; n     for (int i = 0; i <= 400; i++) { n       for (int j = 0; j <= 200; j++) { n         long long x = 3 * i, y = 5 * j; n         long long z = n - x - y; n         if (z >= 0) { n           if (z % 7 == 0) { n             z = z / 7; n             ans.push_back(x), ans.push_back(y), ans.push_back(7 * z); n             break; n           } n         } n       } n       if (ans.size() == 3) break; n     } n     if (ans.size() != 3) n       cout << -1 <<   n ; n     else n       cout << ans[0] / 3 <<     << ans[1] / 5 <<     << ans[2] / 7 <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   std::ios::sync_with_stdio(false); n   int T; n   cin >> T; n   while (T--) { n     long long int n; n     string second; n     cin >> n; n     cin >> second; n     vector<char> v, v1; n     int first = 0; n     int f1 = 0; n     int f2 = 0; n     for (int i = n - 1; i >= 0; i--) { n       if (first == 0 && second[i] ==  1 ) { n         v.emplace_back(second[i]); n       } n       if (first == 0 && second[i] ==  0 ) { n         v.emplace_back(second[i]); n         first = 1; n         f1 = i; n         break; n       } n     } n     for (int i = 0; i < n; i++) { n       if (second[i] ==  0  && i < f1) { n         v.emplace_back(second[i]); n       } n       if (second[i] ==  1 ) { n         break; n       } n     } n     reverse(v.begin(), v.end()); n     for (int i = 0; i < v.size(); i++) { n       cout << v[i]; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = 0; n int n, m, k; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n string direc =  RDLU ; n long long ln, lk, lm; n void etp(bool f = 0) { n   puts(f ?  YES  :  NO ); n   exit(0); n } n void addmod(int &x, int y, int mod = 1000000007) { n   x += y; n   if (x >= mod) x -= mod; n } n void et() { n   puts( -1 ); n   exit(0); n } n long long a[100005], b[100005]; n bool ck(long long r) { n   for (int(i) = 1; (i) <= (int)(n); (i)++) { n     long long L = a[i] - r, R = a[i] + r; n     int p = upper_bound(b + 1, b + 1 + m, R) - (b + 1); n     p--; n     int q = lower_bound(b + 1, b + 1 + m, L) - (b + 1); n     q--; n     if (q >= p) return 0; n   } n   return 1; n } n void fmain() { n   scanf( %d%d , &n, &m); n   for (int(i) = 1; (i) <= (int)(n); (i)++) { n     scanf( %lld , a + i); n   } n   for (int(i) = 1; (i) <= (int)(m); (i)++) scanf( %lld , b + i); n   long long l = -1, r = 2e9; n   while (l + 1 < r) { n     long long mid = (l + r) / 2; n     if (ck(mid)) n       r = mid; n     else n       l = mid; n   } n   printf( %lld n , r); n } n int main() { n   fmain(); n   return 0; n } n