#include <bits/stdc++.h> n using namespace std; n struct node { n   long long mx, sm, idx; n   node() { n     mx = 0; n     sm = 0; n     idx = 0; n   } n }; n struct segTree { n   long long n; n   vector<node> st; n   segTree(long long n) { n     this->n = n; n     st.resize(4 * n + 5); n   } n   void combine(long long pos, long long lc, long long rc) { n     st[pos].sm = st[lc].sm + st[rc].sm; n     if (st[lc].mx >= st[rc].mx) n       st[pos].mx = st[lc].mx, st[pos].idx = st[lc].idx; n     else n       st[pos].mx = st[rc].mx, st[pos].idx = st[rc].idx; n   } n   void _build(long long l, long long r, long long pos, long long *arr) { n     if (l == r) { n       st[pos].mx = arr[l]; n       st[pos].sm = arr[l]; n       st[pos].idx = l; n       return; n     } n     long long lc = pos << 1, rc = lc ^ 1, mid = (l + r) >> 1; n     _build(l, mid, lc, arr); n     _build(mid + 1, r, rc, arr); n     combine(pos, lc, rc); n   } n   void _upd(long long id, long long val, long long l, long long r, n             long long pos) { n     if (l == r) { n       st[pos].mx = val; n       st[pos].sm = val; n       st[pos].idx = l; n       return; n     } n     long long lc = pos << 1, rc = lc ^ 1, mid = (l + r) >> 1; n     if (id <= mid) n       _upd(id, val, l, mid, lc); n     else n       _upd(id, val, mid + 1, r, rc); n     combine(pos, lc, rc); n   } n   pair<long long, long long> _qrymax(long long ql, long long qr, long long l, n                                      long long r, long long pos) { n     if (qr < l || ql > r) n       return make_pair(-LONG_LONG_MAX, -1); n     else if (ql <= l && qr >= r) n       return make_pair(st[pos].mx, st[pos].idx); n     long long lc = pos << 1, rc = lc ^ 1, mid = (l + r) >> 1; n     pair<long long, long long> lef = _qrymax(ql, qr, l, mid, lc), n                                rig = _qrymax(ql, qr, mid + 1, r, rc); n     if (lef.first >= rig.first) return lef; n     return rig; n   } n   long long _qrysm(long long ql, long long qr, long long l, long long r, n                    long long pos) { n     if (qr < l || ql > r) n       return 0; n     else if (ql <= l && qr >= r) n       return st[pos].sm; n     long long lc = pos << 1, rc = lc ^ 1, mid = (l + r) >> 1; n     return _qrysm(ql, qr, l, mid, lc) + _qrysm(ql, qr, mid + 1, r, rc); n   } n   pair<long long, long long> qry_max(long long ql, long long qr) { n     return _qrymax(ql, qr, 1, n, 1); n   } n   void upd(long long idx, long long val) { _upd(idx, val, 1, n, 1); } n   long long qry_sum(long long ql, long long qr) { n     return _qrysm(ql, qr, 1, n, 1); n   } n   void build(long long *arr) { _build(1, n, 1, arr); } n }; n const long long N = 1e5 + 5; n long long n, m, a[N]; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   segTree S(n); n   S.build(a); n   while (m--) { n     long long type; n     cin >> type; n     if (type == 1) { n       long long l, r; n       cin >> l >> r; n       cout << S.qry_sum(l, r) <<   n ; n     } else if (type == 2) { n       long long l, r, x; n       cin >> l >> r >> x; n       pair<long long, long long> pp = S.qry_max(l, r); n       while (pp.first >= x) { n         long long newval = pp.first % x, idx = pp.second; n         S.upd(idx, newval); n         pp = S.qry_max(l, r); n       } n     } else { n       long long idx, val; n       cin >> idx >> val; n       S.upd(idx, val); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 6e6 + 500; n int pt[N << 1], tn, m; n pair<int, pair<int, int> > seg1[N], seg2[N]; n int n1, n2; n int getdep(const pair<int, int> &u) { return u.first + u.second; } n int lowbit(const int &x) { n   if (!x) return 1 << 30; n   return x & -x; n } n pair<int, int> getfa(const pair<int, int> &u) { n   if (!u.first || !u.second) return make_pair(0, 0); n   return lowbit(u.first) < lowbit(u.second) ? make_pair(u.first - 1, u.second) n                                             : make_pair(u.first, u.second - 1); n } n pair<int, int> gettop(const pair<int, int> &u) { n   if (!u.first || !u.second) return make_pair((u.first != 0), (u.second != 0)); n   if (lowbit(u.first) < lowbit(u.second)) { n     return make_pair(1 + (u.first & (~(lowbit(u.second) - 1))), u.second); n   } else { n     return make_pair(u.first, 1 + (u.second & (~(lowbit(u.first) - 1)))); n   } n } n void ins(pair<int, int> u, pair<int, int> v) { n   pair<int, int> tpu, tpv; n   while ((tpu = gettop(u)) != (tpv = gettop(v))) { n     if (getdep(tpu) > getdep(tpv)) { n       if (lowbit(u.second) < lowbit(u.first)) n         seg1[++n1] = make_pair(u.first, make_pair(tpu.second, u.second)); n       else n         seg2[++n2] = make_pair(u.second, make_pair(tpu.first, u.first)); n       u = getfa(tpu); n     } else { n       if (lowbit(v.second) < lowbit(v.first)) n         seg1[++n1] = make_pair(v.first, make_pair(tpv.second, v.second)); n       else n         seg2[++n2] = make_pair(v.second, make_pair(tpv.first, v.first)); n       v = getfa(tpv); n     } n   } n   if (getdep(u) > getdep(v)) swap(u, v); n   if (lowbit(u.second) < lowbit(u.first)) n     seg1[++n1] = make_pair(u.first, make_pair(u.second, v.second)); n   else n     seg2[++n2] = make_pair(u.second, make_pair(u.first, v.first)); n } n int main() { n   scanf( %d , &m); n   for (int i = 1; i <= m; i++) { n     pair<int, int> u, v; n     scanf( %d%d%d%d , &u.first, &u.second, &v.first, &v.second); n     ins(u, v); n   } n   sort(seg1 + 1, seg1 + n1 + 1), sort(seg2 + 1, seg2 + n2 + 1); n   for (int i = 1, j; i <= n1; i = j + 1) { n     j = i; n     while (j + 1 <= n1 && seg1[j + 1].first == seg1[j].first) ++j; n     int l = seg1[i].second.first, r = seg1[i].second.second; n     for (int k = i + 1; k <= j; k++) { n       int tl = seg1[k].second.first, tr = seg1[k].second.second; n       if (tl <= r + 1) n         r = max(r, tr); n       else n         pt[++tn] = l + seg1[i].first, pt[++tn] = r + seg1[i].first + 1, l = tl, n         r = tr; n     } n     pt[++tn] = l + seg1[i].first, pt[++tn] = r + seg1[i].first + 1; n   } n   for (int i = 1, j; i <= n2; i = j + 1) { n     j = i; n     while (j + 1 <= n2 && seg2[j + 1].first == seg2[j].first) ++j; n     int l = seg2[i].second.first, r = seg2[i].second.second; n     for (int k = i + 1; k <= j; k++) { n       int tl = seg2[k].second.first, tr = seg2[k].second.second; n       if (tl <= r + 1) n         r = max(r, tr); n       else n         pt[++tn] = l + seg2[i].first, pt[++tn] = r + seg2[i].first + 1, l = tl, n         r = tr; n     } n     pt[++tn] = l + seg2[i].first, pt[++tn] = r + seg2[i].first + 1; n   } n   sort(pt + 1, pt + tn + 1); n   int ans = 0; n   for (int i = 1, j; i <= tn; i = j + 1) { n     j = i; n     if (!pt[i]) continue; n     while (j + 1 <= tn && pt[j + 1] == pt[i]) ++j; n     if (((j - i + 1) & 1)) ++ans; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int mod = 1000000000 + 7; n const long long int N = 10000000 + 6; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m, k; n   cin >> n >> m >> k; n   int ind[k + 1]; n   int arr[k]; n   for (int i = 0; i < k; i++) { n     cin >> arr[i]; n     ind[arr[i]] = i + 1; n   } n   long long int ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int v; n       cin >> v; n       ans = ans + ind[v]; n       int temp = ind[v]; n       for (int l = 1; l <= k; l++) { n         if (ind[l] < temp) ind[l]++; n       } n       ind[v] = 1; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> graf[300007]; n struct zap { n   long long zmiana; n   long long gle; n }; n vector<zap> z[300007]; n int ojc[300007]; n long long liczba[300007]; n map<long long, long long> zmiany; n void licz(int poz, int pop, long long zmiana, long long gl) { n   for (int i = 0; i < z[poz].size(); i++) { n     zmiana += z[poz][i].zmiana; n     zmiany[gl + z[poz][i].gle] += z[poz][i].zmiana; n   } n   liczba[poz] = zmiana; n   zmiana -= zmiany[gl]; n   for (int i = 0; i < graf[poz].size(); i++) { n     if (graf[poz][i] != pop) { n       licz(graf[poz][i], poz, zmiana, gl + 1); n     } n   } n   for (int i = 0; i < z[poz].size(); i++) { n     zmiany[gl + z[poz][i].gle] -= z[poz][i].zmiana; n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   int n, m; n   cin >> n; n   int a, b; n   for (int i = 1; i < n; i++) { n     cin >> a >> b; n     graf[a].push_back(b); n     graf[b].push_back(a); n   } n   cin >> m; n   for (int i = 0; i < m; i++) { n     cin >> a; n     long long dl, ile; n     cin >> dl >> ile; n     zap x; n     x.zmiana = ile; n     x.gle = dl; n     z[a].push_back(x); n   } n   licz(1, -1, 0, 0); n   for (int i = 1; i <= n; i++) cout << liczba[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x; n   cin >> x; n   int j; n   for (j = 1; (j * j + 1) < 2 * x; j = j + 2) n     ; n   if (x == 3) j = 5; n   cout << j << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string s[1000]; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     cin >> s[i]; n   } n   char ch = s[0][0]; n   int flg = 0, cnc = 0, cnch = 0; n   char c = s[0][1]; n   if (c == ch) flg = 1; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (s[i][j] == c) { n         cnc++; n       } else if (s[i][j] == ch) { n         cnch++; n       } else n         flg = 1; n     } n   } n   if (cnch != 2 * n - 1) n     flg = 1; n   else if (n * n - cnch != cnc) n     flg = 1; n   for (int j = 0, i = 0; i < n / 2; i++) { n     if (s[i][i] == s[i][n - i - 1] && ch == s[i][i]) { n     } else n       flg = 1; n   } n   if (s[n / 2][n / 2] != ch) flg = 1; n   for (int j = 0, i = n - 1; i > n / 2; j++, i--) { n     if (s[i][j] == s[i][n - 1 - j] && ch == s[i][j]) { n     } else n       flg = 1; n   } n   if (flg) n     cout <<  NO  << endl; n   else n     cout <<  YES  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long t = 1, tt; n void solve() { n   long long n; n   cin >> n; n   string a, b; n   cin >> a >> b; n   if (n == 1) { n     if (a == b) n       cout << 0; n     else n       cout << 1; n     return; n   } n   long long ans = 0; n   for (long long i = 0; i < n / 2; i += 1) { n     set<char> ss; n     map<char, long long> mapa; n     ss.insert(a[i]); n     mapa[a[i]]++; n     ss.insert(a[n - i - 1]); n     mapa[a[n - i - 1]]++; n     ss.insert(b[i]); n     mapa[b[i]]++; n     ss.insert(b[n - i - 1]); n     mapa[b[n - i - 1]]++; n     if (int((ss).size()) == 4) ans += 2; n     if (int((ss).size()) == 3) { n       if (a[i] == a[n - i - 1]) n         ans += 2; n       else n         ans += 1; n     } n     if (int((ss).size()) == 2) { n       if (mapa[a[i]] == 1 || mapa[a[i]] == 3) ans += 1; n     } n   } n   if (n % 2 == 1) { n     if (a[n / 2] != b[n / 2]) ans += 1; n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   tt = t; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100100; n int gcd(int a, int b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n int tree[N << 2]; n void pushup(int ind) { tree[ind] = gcd(tree[ind << 1], tree[ind << 1 | 1]); } n int a[N]; n void build(int ind, int ll, int rr) { n   if (ll == rr) { n     tree[ind] = a[ll]; n     return; n   } n   int mid = (ll + rr) >> 1; n   build(((ind) << 1), ll, mid); n   build(((ind) << 1 | 1), mid + 1, rr); n   pushup(ind); n } n int query(int ind, int ll, int rr, int l, int r) { n   if (ll == l && rr == r) { n     return tree[ind]; n   } n   int mid = (ll + rr) >> 1; n   if (r <= mid) { n     return query(((ind) << 1), ll, mid, l, r); n   } else if (l > mid) { n     return query(((ind) << 1 | 1), mid + 1, rr, l, r); n   } else { n     int a = query(((ind) << 1), ll, mid, l, mid); n     int b = query(((ind) << 1 | 1), mid + 1, rr, mid + 1, r); n     return gcd(a, b); n   } n } n pair<int, int> re[N]; n int t[N], w[N], cnt; n struct PBT { n   int ls[N * 20], rs[N * 20], c[N * 20], tree[N * 20]; n   int idx, mx; n   int build(int l, int r) { n     int rt = idx++; n     c[rt] = 0; n     if (l != r) { n       int mid = (l + r) >> 1; n       ls[rt] = build(l, mid); n       rs[rt] = build(mid + 1, r); n     } n     return rt; n   } n   int update(int rt, int pos, int val) { n     int nrt = idx++, ret = nrt; n     c[nrt] = c[rt] + val; n     int l = 1, r = mx; n     while (l != r) { n       int mid = (l + r) >> 1; n       if (pos <= mid) { n         ls[nrt] = idx++; n         rs[nrt] = rs[rt]; n         nrt = ls[nrt]; n         rt = ls[rt]; n         r = mid; n       } else { n         rs[nrt] = idx++; n         ls[nrt] = ls[rt]; n         nrt = rs[nrt]; n         rt = rs[rt]; n         l = mid + 1; n       } n       c[nrt] = c[rt] + val; n     } n     return ret; n   } n   int query(int lrt, int rrt, int pos) { n     int l = 1, r = mx; n     while (l != r) { n       int mid = (l + r) >> 1; n       if (pos <= mid) { n         lrt = ls[lrt]; n         rrt = ls[rrt]; n         r = mid; n       } else { n         lrt = rs[lrt]; n         rrt = rs[rrt]; n         l = mid + 1; n       } n     } n     return c[rrt] - c[lrt]; n   } n } pbt; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   build(1, 1, n); n   int q; n   scanf( %d , &q); n   for (int i = 0; i < q; i++) { n     scanf( %d%d , &re[i].first, &re[i].second); n     t[++cnt] = query(1, 1, n, re[i].first, re[i].second); n     w[i] = t[cnt]; n   } n   sort(t + 1, t + cnt + 1); n   cnt = unique(t + 1, t + cnt + 1) - t - 1; n   map<int, int> idx; n   for (int i = 1; i <= cnt; i++) { n     idx[t[i]] = i; n   } n   pbt.mx = cnt; n   pbt.tree[0] = pbt.build(1, cnt); n   for (int i = 1; i <= n; i++) { n     if (idx.count(a[i])) { n       int x = idx[a[i]]; n       pbt.tree[i] = pbt.update(pbt.tree[i - 1], x, 1); n     } else { n       pbt.tree[i] = pbt.tree[i - 1]; n     } n   } n   for (int i = 0; i < q; i++) { n     int x = re[i].first, y = re[i].second; n     int ret = pbt.query(pbt.tree[x - 1], pbt.tree[y], idx[w[i]]); n     printf( %d n , re[i].second - re[i].first + 1 - ret); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int gi() { n   int data = 0, fu = 1; n   char ch = 0; n   while (ch !=  -  && (ch <  0  || ch >  9 )) ch = getchar(); n   if (ch ==  - ) fu = -1, ch = getchar(); n   while ( 0  <= ch && ch <=  9 ) n     data = (data << 1) + (data << 3) + (ch ^ 48), ch = getchar(); n   return data * fu; n } n int n, nq, q[200005]; n map<long long, long long> M; n struct Node { n   long long x, y; n } p[200005]; n const long double eps = 1e-8; n inline long double Slope(int i, int j) { n   return (long double)(p[i].y - p[j].y) / (long double)(p[i].x - p[j].x); n } n inline bool cmp(Node a, Node b) { return a.x ^ b.x ? a.x < b.x : a.y < b.y; } n int main() { n   n = gi(); n   for (long long i = 1, x, y; i <= n; ++i) { n     x = gi(), y = gi(); n     y -= x * x; n     if (M.find(x) == M.end()) M[x] = -1e18; n     M[x] = max(M[x], y); n   } n   n = 0; n   for (map<long long, long long>::iterator it = M.begin(); it != M.end(); n        it++) { n     long long X = it->first, Y = it->second; n     long long i; n     p[i = ++n].x = X, p[i].y = Y; n   } n   sort(p + 1, p + n + 1, cmp); n   nq = 0; n   for (long long i = 1; i <= n; ++i) { n     while (nq >= 2 && n            Slope(q[nq - 1], q[nq]) - eps <= Slope(q[nq - 1], i) + eps) n       --nq; n     q[++nq] = i; n   } n   cout << nq - 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e+6 + 5; n long long big = 1e+18 + 3; n long long mod = 1e+9 + 7; n long long freq[26] = {0}, vis[N]; n long long maxv = -1; n long long sz = 0; n void dfs(string &s, vector<vector<long long>> &gr, long long ind) { n   vis[ind] = 1; n   sz++; n   freq[s[ind] - (long long) a ]++; n   for (auto it : gr[ind]) n     if (!vis[it]) dfs(s, gr, it); n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long n; n     long long k; n     cin >> n >> k; n     string s; n     for (long long i = 0; i <= n; i++) vis[i] = 0; n     vector<vector<long long>> gr(n + 1); n     long long moves = 0; n     cin >> s; n     for (long long i = 0; i <= n - 1; i++) { n       if (i + k < n) gr[i].push_back(i + k), gr[i + k].push_back(i); n       if (i - k >= 0) gr[i].push_back(i - k), gr[i - k].push_back(i); n       gr[i].push_back(n - i - 1); n       gr[n - i - 1].push_back(i); n     } n     for (long long i = 0; i <= n - 1; i++) { n       if (vis[i]) continue; n       dfs(s, gr, i); n       for (long long x = 0; x <= 25; x++) maxv = max(maxv, freq[x]); n       moves += (sz - maxv); n       for (long long x = 0; x <= 25; x++) freq[x] = 0; n       maxv = -1; n       sz = 0; n     } n     cout << moves <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 30; n int Par[N], cnt_S, cnt_M, ok = 1, n, m; n struct Edge { n   int v, u; n   char col; n   bool mark; n } E[N]; n int Find(int x) { return (Par[x] == x ? x : Par[x] = Find(Par[x])); } n bool Union(int e) { n   int x = Find(E[e].v), y = Find(E[e].u); n   if (x == y) return 0; n   Par[x] = y; n   return 1; n } n int main() { n   cin >> n >> m; n   if ((n - 1) & 1) ok = 0; n   for (int i = 0; i < m; i++) cin >> E[i].v >> E[i].u >> E[i].col; n   for (int i = 0; i < N; i++) Par[i] = i; n   for (int i = 0; i < m; i++) n     if (E[i].col ==  M ) Union(i); n   for (int i = 0; i < m; i++) n     if (E[i].col ==  S  && Union(i)) E[i].mark = true, cnt_S++; n   for (int i = 0; i < N; i++) Par[i] = i; n   for (int i = 0; i < m; i++) n     if (E[i].mark) Union(i); n   for (int i = 0; i < m; i++) n     if (cnt_S != (n - 1) >> 1 && E[i].col ==  S  && Union(i)) n       E[i].mark = true, cnt_S++; n   for (int i = 0; i < m; i++) n     if (E[i].col ==  M  && Union(i)) E[i].mark = true, cnt_M++; n   if (cnt_S != (n - 1) >> 1 || cnt_M != (n - 1) >> 1 || !ok) n     return cout <<  -1 n , 0; n   cout << n - 1 <<   n ; n   for (int i = 0; i < m; i++) n     if (E[i].mark) cout << i + 1 <<    ; n   return (0); n } n


#include <bits/stdc++.h> n using namespace std; n const unsigned long long INF = 1000 * 1000 * 1000; n const double eps = 1e-9; n int main() { n   string s; n   cin >> s; n   int pos = 0; n   for (char c =  z ; c >=  a ; c--) { n     int tek = pos; n     for (int i = tek; i < s.size(); i++) n       if (s[i] == c) { n         cout << s[i]; n         pos = i; n       } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void addEdge(vector<long long> adj[], long long u, long long v) { n   adj[u].push_back(v); n   adj[v].push_back(u); n } n void DFSUtil(vector<long long> adj[], long long v, bool visited[]) { n   visited[v] = true; n   for (long long i = 0; i < adj[v].size(); ++i) n     if (!visited[adj[v][i]]) DFSUtil(adj, adj[v][i], visited); n } n long long f1(vector<long long> adj[], long long n) { n   bool* visited = new bool[n]; n   long long count = 0; n   for (long long v = 0; v < n; v++) visited[v] = false; n   for (long long v = 0; v < n; v++) { n     if (visited[v] == false) { n       DFSUtil(adj, v, visited); n       count += 1; n     } n   } n   return count; n } n bool isCyclicUtil(vector<long long> adj[], long long v, bool visited[], n                   long long parent) { n   bool flag = false; n   visited[v] = true; n   for (long long i = 0; i < adj[v].size(); ++i) { n     if (!visited[adj[v][i]]) { n       if (isCyclicUtil(adj, adj[v][i], visited, v)) flag = true; n     } else if (adj[v][i] != parent) n       flag = true; n   } n   return flag; n } n long long isCyclic(vector<long long> adj[], long long n) { n   bool* visited = new bool[n]; n   for (long long i = 0; i < n; i++) visited[i] = false; n   long long ans = 0; n   for (long long u = 0; u < n; u++) { n     if (!visited[u]) { n       if (isCyclicUtil(adj, u, visited, -1)) { n         ans++; n       } n     } n   } n   return ans; n } n void solve() { n   long long n, m; n   cin >> n >> m; n   vector<long long> adj[n]; n   for (long long i = 0; i < m; i++) { n     long long x, y; n     cin >> x >> y; n     x--; n     y--; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   long long x = f1(adj, n); n   long long y = isCyclic(adj, n); n   cout << x - y << endl; n } n signed main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, vector<int>> pos; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   int tt; n   cin >> tt; n   while (tt--) { n     int n; n     cin >> n; n     vector<int> a(n), b(n); n     for (int i = 0; i < n; i++) { n       cin >> a[i]; n     } n     for (int i = 0; i < n; i++) { n       cin >> b[i]; n     } n     pos.clear(); n     for (int i = n - 1; i >= 0; i--) { n       pos[a[i]].push_back(i); n     } n     vector<int> tree(4 * n); n     function<void(int, int, int)> build = [&](int v, int l, int r) { n       if (l + 1 == r) { n         tree[v] = a[l]; n         return; n       } n       int mid = (l + r) / 2; n       build(2 * v, l, mid); n       build(2 * v + 1, mid, r); n       tree[v] = min(tree[2 * v], tree[2 * v + 1]); n     }; n     function<int(int, int, int, int, int)> query_min = n         [&](int ql, int qr, int v, int l, int r) { n           if (ql <= l && r <= qr) return tree[v]; n           if (r <= ql || qr <= l) return int(1e6); n           int mid = (l + r) / 2; n           return min(query_min(ql, qr, 2 * v, l, mid), n                      query_min(ql, qr, 2 * v + 1, mid, r)); n         }; n     function<void(int, int, int, int)> update = [&](int x, int v, int l, n                                                     int r) { n       if (l == x && r == l + 1) { n         tree[v] = int(1e6); n         return; n       } n       int mid = (l + r) / 2; n       if (x < mid) n         update(x, 2 * v, l, mid); n       else n         update(x, 2 * v + 1, mid, r); n       tree[v] = min(tree[2 * v], tree[2 * v + 1]); n     }; n     bool ok = true; n     build(1, 0, n); n     for (int i = 0; i < n; i++) { n       if (pos[b[i]].size() == 0) { n         ok = false; n         break; n       } n       int x = pos[b[i]].back(); n       pos[b[i]].pop_back(); n       if (query_min(0, x + 1, 1, 0, n) != b[i]) { n         ok = false; n         break; n       } n       update(x, 1, 0, n); n     } n     if (ok) n       cout <<  YES n ; n     else n       cout <<  NO n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxtop = 1025; n const int maxn = 100010; n const int mo = 1000000007; n int top; n int repeat[maxtop + 1]; n int rest; n int a[maxn + 1]; n int n, K; n int C[maxn + 1]; n int f[2][maxtop + 1], old, now; n long long result; n void init() { n   int i; n   scanf( %d%d , &n, &K); n   for (i = 1; i <= n; ++i) scanf( %d , &a[i]); n   sort(a + 1, a + n + 1); n } n bool lucky(int x) { n   int t; n   while (x) { n     t = x % 10; n     if ((t != 4) && (t != 7)) return false; n     x /= 10; n   } n   return true; n } n void calc_repeat() { n   int i, j; n   top = rest = 0; n   i = 1; n   while (i <= n) { n     if (lucky(a[i])) { n       j = i; n       while ((j + 1 <= n) && (a[j + 1] == a[i])) ++j; n       repeat[++top] = j - i + 1; n       i = j + 1; n     } else { n       ++rest; n       ++i; n     } n   } n } n int power(int x, int y) { n   long long a, b; n   b = x; n   a = 1; n   while (y) { n     if (y & 1) a = (a * b) % mo; n     b = (b * b) % mo; n     y = (y >> 1); n   } n   return a; n } n void calc_C() { n   int i; n   long long tmp; n   C[0] = 1; n   if (rest == 0) return; n   C[1] = rest; n   for (i = 2; i <= rest; ++i) { n     tmp = C[i - 1]; n     tmp = (tmp * (rest - i + 1)) % mo; n     tmp = (tmp * power(i, mo - 2)) % mo; n     C[i] = tmp; n   } n } n void calc_result() { n   int i, j, k; n   long long tmp; n   now = 0; n   for (i = 1; i <= top; ++i) f[now][i] = 0; n   f[now][0] = 1; n   for (i = 1; i <= top; ++i) { n     old = now; n     now = 1 - now; n     f[now][0] = 1; n     for (j = 1; j <= i; ++j) { n       if (j > K) break; n       f[now][j] = 0; n       if (j < i) f[now][j] += f[old][j]; n       tmp = f[old][j - 1]; n       tmp = (tmp * repeat[i]) % mo; n       f[now][j] = (f[now][j] + tmp) % mo; n     } n   } n   result = 0; n   for (i = 0; i <= top; ++i) { n     if (i > K) break; n     if (K - i > rest) continue; n     tmp = f[now][i]; n     tmp = (tmp * C[K - i]) % mo; n     result += tmp; n     if (result >= mo) result -= mo; n   } n } n void work() { n   calc_repeat(); n   calc_C(); n   calc_result(); n } n void output() { printf( %d n , result); } n int main() { n   init(); n   work(); n   output(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n long long res, rem; n long long narr[100001]; n long long tempint; n int main() { n   scanf( %I64d , &n); n   for (int i = 0; i < n; ++i) scanf( %I64d , narr + i); n   sort(narr, narr + n); n   reverse(narr, narr + n); n   for (int i = 0; i < n; ++i) res += narr[i]; n   tempint = n - 1; n   for (int i = 0; i < n; ++i, tempint -= 2) res += 2 * narr[i] * (tempint); n   long long ores = res, on = n; n   while (1) { n     rem = ores % on; n     if (rem == 0) { n       break; n     } n     ores = on; n     on = rem; n   } n   printf( %I64d %I64d , res / on, n / on); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1005; n char mp[MAXN][MAXN]; n int ansmp[MAXN][MAXN]; n int dire[4][2] = {0, -1, 0, 1, -1, 0, 1, 0}; n int n, m; n void fillansmap(int x0, int y0, int x1, int y1) { n   bool colorflag[5]; n   int dx = (x1 - x0 > 0); n   int dy = (y1 - y0 > 0); n   int dcolor; n   for (int i = 1; i <= 4; i++) colorflag[i] = true; n   for (int x = x0, y = y0; x <= x1 && y <= y1; x += dx, y += dy) { n     for (int i = 0; i < 4; i++) { n       int xx = x + dire[i][0]; n       int yy = y + dire[i][1]; n       if (0 <= xx && xx < n && 0 <= yy && yy < m) n         colorflag[ansmp[xx][yy]] = false; n     } n   } n   for (int i = 1; i <= 4; i++) n     if (colorflag[i]) { n       dcolor = i; n       break; n     } n   for (int x = x0, y = y0; x <= x1 && y <= y1; x += dx, y += dy) n     ansmp[x][y] = dcolor, mp[x][y] =  . ; n   return; n } n bool CheckMap() { n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (mp[i][j] !=  . ) return false; n   return true; n } n int main() { n   bool ansflag = true; n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , mp[i]); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       if (mp[i][j] ==  b ) { n         if (i > 0 && mp[i - 1][j] ==  w  && i < n - 1 && mp[i + 1][j] ==  w ) n           fillansmap(i - 1, j, i + 1, j); n         else if (j > 0 && mp[i][j - 1] ==  w  && j < m - 1 && n                  mp[i][j + 1] ==  w ) n           fillansmap(i, j - 1, i, j + 1); n       } n   if (CheckMap()) { n     printf( YES n ); n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) n         if (ansmp[i][j] == 0) n           printf( . ); n         else n           printf( %c , ansmp[i][j] - 1 +  a ); n       printf(  n ); n     } n   } else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using namespace std; n const int INF = 2e9 + 9; n const long long INF1 = 1e18 + 9; n const long long MAXN = 4e5 + 7; n const long long MAXN1 = 1 << 11; n const long long MAXN2 = 4e6 + 9; n const long long MOD = 1e9 + 7; n const long long MOD1 = 1e9 + 9; n const long long ALPH = 50; n const long long PW1 = 239; n const long long PW2 = 199; n const long long PW3 = 193; n const long long PW4 = 117; n const long double EPS = 1e-9; n const long long BLOCK = 3684; n const long long BLOCK1 = 1 << 9; n void solve(); n signed main() { n   srand( a  +  l  +  e  +  x  +  X  +  5  +  1  +  2 ); n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int q = 1; n   if (0) cin >> q; n   while (q--) solve(); n } n set<int> v[MAXN]; n void solve() { n   int n, m; n   cin >> n >> m; n   vector<int> v1(n), v2(n); n   for (int i = 0; i < m; ++i) { n     int a, b; n     cin >> a >> b; n     v[a].emplace(b); n     v[b].emplace(a); n   } n   for (int i = 1; i <= n; ++i) { n     v[i].emplace(i); n     if (v[i].size() == n) continue; n     for (int j = 1; j <= n; ++j) { n       if (v[i].find(j) == v[i].end()) { n         cout <<  YES n ; n         v2[i - 1] = v2[j - 1] = v1[i - 1] = 1; n         v1[j - 1] = 2; n         int ind = 2; n         for (int t = 0; t < n; ++t) n           if (v1[t] == 0) v1[t] = ++ind; n         ind = 2; n         for (int t = 0; t < n; ++t) n           if (v2[t] == 0) v2[t] = ++ind; n         for (auto &i : v1) cout << i <<    ; n         cout <<   n ; n         for (auto &i : v2) cout << i <<    ; n         return; n       } n     } n   } n   cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int MAXN = 1e4 + 10; n int a[MAXN], b[MAXN], c[MAXN]; n int n; n void fun(int pre) { n   for (int i = 0; i < n; i++) c[i] = a[i]; n   for (int i = 0; i <= pre; i++) { n     c[i] = a[n - pre - 1 + i]; n     c[n - pre - 1 + i] = a[i]; n   } n   for (int i = 0; i < n; i++) a[i] = c[i]; n } n int main() { n   int T; n   cin >> T; n   while (T--) { n     cin >> n; n     for (int i = 0; i < n; i++) cin >> a[i]; n     for (int i = 0; i < n; i++) cin >> b[i]; n     if ((n % 2 == 1) && a[n / 2] != b[n / 2]) { n       cout <<  No  << endl; n     } else { n       int retflag = 1; n       for (int i = n / 2 - 1; i >= 0 && retflag; i--) { n         if (a[i] == b[i] && a[n - i - 1] == b[n - i - 1]) continue; n         int flag = 1; n         for (int j = 0; j <= i && flag; j++) { n           if (flag && a[j] == b[i] && a[n - j - 1] == b[n - i - 1]) { n             flag = 0; n             fun(j); n             fun(i); n           } n           if (flag && a[j] == b[n - i - 1] && a[n - j - 1] == b[i]) { n             flag = 0; n             fun(j); n             fun(0); n             fun(i); n           } n         } n         if (flag) retflag = 0; n       } n       if (retflag) n         cout <<  Yes  << endl; n       else n         cout <<  No  << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int a[n + 2]; n   a[0] = 0; n   for (int i = 1; i < n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i < n; i++) { n     a[i] = a[i - 1] + a[i]; n   } n   int x, y; n   scanf( %d%d , &x, &y); n   printf( %d , a[y - 1] - a[x - 1]); n } n


#include <bits/stdc++.h> n using namespace std; n void init_code() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n long long dp[5002][5002]; n long long solve(vector<long long>& v, long long k, long long n, long long idx) { n   if (k == 0) return 0; n   if (idx >= n) return 0; n   if (dp[idx][k] != -1) return dp[idx][k]; n   auto i = (upper_bound(v.begin(), v.end(), v[idx] + 5) - v.begin()); n   long long cnt = i - idx; n   long long temp = solve(v, k - 1, n, i) + cnt; n   temp = max(temp, solve(v, k, n, idx + 1)); n   dp[idx][k] = temp; n   return temp; n } n void solve() { n   long long n, k; n   cin >> n >> k; n   vector<long long> v(n); n   for (long long i = 0; i < n; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   memset(dp, -1, sizeof(dp)); n   cout << solve(v, k, n, 0); n } n signed main() { n   init_code(); n   long long tc = 1; n   long long num = 1; n   while (tc--) { n     num++; n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int a1, a2, b1, b2; n bool sol(int a, int b) { n   if (b + 1 < a) return 0; n   if (b > (a + 1) * 2) return 0; n   return 1; n } n int main() { n   cin >> a1 >> a2 >> b1 >> b2; n   if (sol(a1, b2) || sol(a2, b1)) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<int, int> arr[1000006]; n int main() { n   int n, v, ans = 0, i, j, a, b, v1; n   scanf( %d , &n); n   scanf( %d , &v); n   for (i = 0; i < n; ++i) { n     scanf( %d , &a); n     scanf( %d , &b); n     arr[a].first += b; n     arr[a + 1].second += b; n   } n   for (i = 0; i < 4; ++i) n     cerr <<  i  n          <<  :   << i <<   |   n          <<  arr[i].F  n          <<  :   << arr[i].first <<   |   n          <<  arr[i].S  n          <<  :   << arr[i].second << endl; n   ; n   v1 = v; n   for (i = 0; i < 3002; ++i) { n     v1 = v; n     if (i <= 3) n       cerr <<  i  n            <<  :   << i <<   |   n            <<  arr[i].F  n            <<  :   << arr[i].first <<   |   n            <<  arr[i].S  n            <<  :   << arr[i].second <<   |   n            <<  v  n            <<  :   << v << endl; n     ; n     if (arr[i].second > 0) { n       if (arr[i].second - v <= 0) { n         v -= arr[i].second; n         ans += arr[i].second; n         arr[i].second = 0; n       } else { n         ans += v; n         arr[i].second -= v; n         v = 0; n       } n     } n     if (i <= 3) n       cerr <<  i  n            <<  :   << i <<   |   n            <<  arr[i].F  n            <<  :   << arr[i].first <<   |   n            <<  arr[i].S  n            <<  :   << arr[i].second <<   |   n            <<  v  n            <<  :   << v << endl; n     ; n     if (arr[i].first > 0) { n       if (arr[i].first - v <= 0) { n         ans += arr[i].first; n         arr[i + 1].second -= arr[i].first; n         arr[i].first = 0; n         v -= arr[i].first; n       } else { n         cerr <<  i  n              <<  :   << i << endl; n         ; n         ans += v; n         arr[i].first -= v; n         arr[i + 1].second -= v; n         v = 0; n       } n     } n     v = v1; n     if (i <= 3) n       cerr <<  i  n            <<  :   << i <<   |   n            <<  ans  n            <<  :   << ans << endl; n     ; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int a[6][6], i, j, k; n   int dx[4] = {-1, 0, 1, -1}, dy[4] = {-1, -1, -1, 0}; n   char x; n   memset(a, -1, sizeof(a)); n   for (i = 1; i < 5; i++) { n     for (j = 1; j < 5; j++) { n       x = getchar(); n       if (x ==  x ) a[i][j] = 1; n       if (x ==  . ) a[i][j] = 0; n     } n     getchar(); n   } n   for (i = 1; i < 5; i++) { n     for (j = 1; j < 5; j++) { n       for (k = 0; k < 4; k++) n         if (a[i][j] + a[i + dx[k]][j + dy[k]] + a[i - dx[k]][j - dy[k]] > 1) { n           printf( YES n ); n           return 0; n         } n     } n   } n   printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int t; n int n, q; n vector<int> vi[300005]; n bool used[300005]; n int pre[300005]; n vector<pair<int, int> > pos[300005]; n int poss[300005]; n int main() { n   cin >> t; n   while (t--) { n     scanf( %d%d , &n, &q); n     for (int i = 0; i < q; i++) vi[i].clear(); n     for (int i = 1; i <= n; i++) { n       pre[i] = -1; n       used[i] = 0; n       pos[i].clear(); n     } n     for (int i = 0; i < q; i++) { n       int k; n       scanf( %d , &k); n       for (int j = 0; j < k; j++) { n         int a; n         scanf( %d , &a); n         vi[i].push_back(a); n       } n     } n     bool answer = 0; n     for (int i = 0; i < q; i++) { n       if (vi[i].size() < 614) continue; n       for (int x = 0; x < vi[i].size(); x++) used[vi[i][x]] = 1; n       for (int x = 0; x < vi[i].size(); x++) poss[vi[i][x]] = x; n       for (int x = 0; x < vi[i].size(); x++) n         if (x) pre[vi[i][x]] = vi[i][x - 1]; n       for (int j = 0; j < q; j++) { n         if (i == j) continue; n         bool in = 0; n         int prre = 1000000000; n         for (int k = 0; k < vi[j].size(); k++) { n           if (used[vi[j][k]]) { n             if (in) { n               int need = pre[vi[j][k]]; n               int real = vi[j][k - 1]; n               if (need != real && prre < poss[vi[j][k]]) { n                 puts( Human ); n                 answer = 1; n                 goto end; n               } n             } n             in = 1; n             prre = min(prre, poss[vi[j][k]]); n           } n         } n       } n       for (int x = 0; x < vi[i].size(); x++) used[vi[i][x]] = 0; n       for (int x = 0; x < vi[i].size(); x++) n         if (x) pre[vi[i][x]] = -1; n     } n   end:; n     if (answer) continue; n     for (int i = 0; i < q; i++) { n       if (vi[i].size() >= 614) continue; n       for (int x = 0; x < vi[i].size(); x++) { n         pos[vi[i][x]].push_back(make_pair(i, x)); n       } n     } n     for (int beg = 1; beg <= n; beg++) { n       for (int i = 0; i < pos[beg].size(); i++) { n         int id = pos[beg][i].first; n         int nm = pos[beg][i].second; n         for (int j = nm + 1; j < vi[id].size(); j++) { n           int cur = vi[id][j]; n           int prre = vi[id][j - 1]; n           if (pre[cur] == -1) { n             pre[cur] = prre; n           } else if (pre[cur] != prre) { n             puts( Human ); n             answer = 1; n             goto end2; n           } n         } n       } n       for (int i = 0; i < pos[beg].size(); i++) { n         int id = pos[beg][i].first; n         int nm = pos[beg][i].second; n         for (int j = nm + 1; j < vi[id].size(); j++) { n           int cur = vi[id][j]; n           pre[cur] = -1; n         } n       } n     } n   end2:; n     if (answer) continue; n     puts( Robot ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool a[55][55], b[55][55], ans[55][55]; n int n, m, ans_cnt; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> a[i][j]; n   for (int i = 1; i <= n - 1; i++) n     for (int j = 1; j <= m - 1; j++) n       if (a[i][j] && a[i + 1][j] && a[i][j + 1] && a[i + 1][j + 1]) { n         ans[i][j] = true; n         ans_cnt++; n         b[i][j] = b[i + 1][j] = b[i][j + 1] = b[i + 1][j + 1] = true; n       } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (a[i][j] != b[i][j]) { n         cout << -1; n         return 0; n       } n   cout << ans_cnt <<   n ; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (ans[i][j]) cout << i <<     << j <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ans; n   long long sum = 0; n   map<long long, int> d; n   cin >> n; n   ans = n - 1; n   for (int i = 0; i < n; i++) { n     int t; n     cin >> t; n     sum += t; n     d[sum]++; n     ans = min(ans, n - d[sum]); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s; n   cin >> s; n   int n = (int)s.size(); n   int pos[n + 1]; n   int st = 1, end = n; n   int temp = 1; n   for (auto x : s) { n     if (st > end) break; n     if (x ==  l ) { n       pos[end--] = temp++; n     } else { n       pos[st++] = temp++; n     } n   } n   for (int i = 1; i <= n; i++) { n     printf( %d n , pos[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long max(long long a, long long b) { return a > b ? a : b; } n class Box { n  public: n   string name; n   int type, border, spacing; n   vector<Box *> con; n   bool hcalc, wcalc; n   long long w, h; n   Box() { hcalc = wcalc = 0; } n   Box(string ni, int t) : name(ni), type(t) { n     con.clear(); n     hcalc = wcalc = 0; n     border = spacing = 0; n   } n   Box(string ni, int t, long long wi, long long hi) n       : name(ni), type(t), w(wi), h(hi) { n     con.clear(); n     hcalc = wcalc = 0; n     border = spacing = 0; n   } n   long long getw() { n     int i; n     if (!wcalc) { n       wcalc = 1; n       if (type == 0) { n       } else if (type == 1) { n         if (con.size() == 0) n           w = 0; n         else { n           w = border * 2 + spacing * (con.size() - 1); n           for (i = 0; i < con.size(); i++) w += con[i]->getw(); n         } n       } else if (type == 2) { n         if (con.size() == 0) n           w = 0; n         else { n           w = 0; n           for (i = 0; i < con.size(); i++) w = max(w, con[i]->getw()); n           w += border * 2; n         } n       } n     } n     return w; n   } n   long long geth() { n     int i; n     if (!hcalc) { n       hcalc = 1; n       if (type == 0) { n       } else if (type == 2) { n         if (con.size() == 0) n           h = 0; n         else { n           h = border * 2 + spacing * (con.size() - 1); n           for (i = 0; i < con.size(); i++) h += con[i]->geth(); n         } n       } else if (type == 1) { n         if (con.size() == 0) n           h = 0; n         else { n           h = 0; n           for (i = 0; i < con.size(); i++) h = max(h, con[i]->geth()); n           h += border * 2; n         } n       } n     } n     return h; n   } n   const bool operator<(const Box &b) const { return name < b.name; } n }; n map<string, int> mp; n int bn; n Box box[205]; n int main(void) { n   int qn, qi, i, j, w, h, v, u; n   char cc[200], zz[200]; n   string cmd, a, b, c; n   scanf( %d , &qn); n   gets(cc); n   bn = 0; n   mp.clear(); n   for (qi = 0; qi < qn; qi++) { n     gets(cc); n     cmd = cc; n     if (cmd.substr(0, 4) ==  VBox ) { n       a = cmd.substr(5); n       mp[a] = bn; n       box[bn++] = Box(a, 2); n     } else if (cmd.substr(0, 4) ==  HBox ) { n       a = cmd.substr(5); n       mp[a] = bn; n       box[bn++] = Box(a, 1); n     } else if (cmd.substr(0, 6) ==  Widget ) { n       i = 7; n       for (j = i + 1; cmd[j] !=  ( ; j++) n         ; n       a = cmd.substr(i, j - i); n       mp[a] = bn; n       sscanf(cmd.substr(j).c_str(),  (%d,%d) , &w, &h); n       box[bn++] = Box(a, 0, w, h); n     } else { n       for (i = 0; cmd[i] !=  . ; i++) n         ; n       a = cmd.substr(0, i); n       v = mp[a]; n       i++; n       for (j = i; cmd[j] !=  ( ; j++) n         ; n       b = cmd.substr(i, j - i); n       j++; n       c = cmd.substr(j, cmd.length() - j - 1); n       if (b ==  pack ) { n         u = mp[c]; n         box[v].con.push_back(box + u); n       } else if (b ==  set_border ) { n         sscanf(c.c_str(),  %d , &w); n         box[v].border = w; n       } else if (b ==  set_spacing ) { n         sscanf(c.c_str(),  %d , &w); n         box[v].spacing = w; n       } n     } n   } n   for (i = 0; i < bn; i++) { n     box[i].getw(); n     box[i].geth(); n   } n   std::sort(box, box + bn); n   for (i = 0; i < bn; i++) { n     cout << box[i].name <<     << box[i].getw() <<     << box[i].geth() << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 7; n vector<long long> vec; n long long solve(vector<long long>& a, long long i) { n   if (i == -1) return 0; n   vector<long long> a1, a0; n   for (long long x : a) n     if (x & (1 << i)) n       a1.push_back(x); n     else n       a0.push_back(x); n   if (a0.empty() || a1.empty()) return solve(a, i - 1); n   return min(solve(a1, i - 1), solve(a0, i - 1)) + (1 << i); n } n int32_t main() { n   long long n; n   cin >> n; n   for (long long i = 0, x; i < n; i++) cin >> x, vec.push_back(x); n   cout << solve(vec, 30) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T bigmod(T p, T e, T M) { n   long long ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return (T)ret; n } n template <class T> n inline T gcd(T a, T b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n template <class T> n inline T modinverse(T a, T M) { n   return bigmod(a, M - 2, M); n } n int dis[4][1001][1001], n, m; n ; n queue<pair<int, int> > Q; n char pp[1011][1011]; n int nxt[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; n void go(int c) { n   while (!Q.empty()) Q.pop(); n   for (int i = (0); i < (n); ++i) { n     for (int j = (0); j < (m); ++j) n       if (pp[i][j] ==  0  + c) { n         dis[c][i][j] = 0; n         Q.push(make_pair(i, j)); n       } n   } n   while (!Q.empty()) { n     pair<int, int> p = Q.front(); n     Q.pop(); n     for (int i = (0); i < (4); ++i) { n       int x = p.first + nxt[i][0], y = p.second + nxt[i][1]; n       if (x < 0 || x >= n || y < 0 || y >= m || pp[x][y] ==  # ) continue; n       if (dis[c][x][y] <= dis[c][p.first][p.second] + 1) continue; n       dis[c][x][y] = dis[c][p.first][p.second] + 1; n       Q.push(make_pair(x, y)); n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = (0); i < (n); ++i) cin >> pp[i]; n   memset(dis, 126, sizeof dis); n   go(1); n   go(2); n   go(3); n   int res = INT_MAX / 3; n   for (int i = (1); i < (4); ++i) { n     int dd[4]; n     memset(dd, 126, sizeof dd); n     dd[i] = 1; n     for (int j = (0); j < (n); ++j) { n       for (int k = (0); k < (m); ++k) { n         if (pp[j][k] >=  1  && pp[j][k] <=  3  && pp[j][k] !=  0  + i) n           dd[pp[j][k] -  0 ] = min(dd[pp[j][k] -  0 ], dis[i][j][k]); n       } n     } n     long long sum = 0; n     for (int j = (1); j < (4); ++j) sum += dd[j] - 1; n     if (sum < res) res = sum; n   } n   for (int i = (0); i < (n); ++i) { n     for (int j = (0); j < (m); ++j) { n       if (pp[i][j] !=  . ) continue; n       long long sum = 1; n       for (int k = (1); k < (4); ++k) { n         sum += dis[k][i][j] - 1; n       } n       if (sum < res) res = sum; n     } n   } n   if (res > 10000000) n     cout << -1 << endl; n   else n     cout << res << endl; n } n


#include <bits/stdc++.h> n const int MAX_N = 20; n int n, k, t; n int e[MAX_N + 1][MAX_N + 1]; n int choosen[MAX_N + 1]; n int mid; n int cnt; n void dfs(int x, int now_cost_sum) { n   if (now_cost_sum > mid) return; n   if (cnt >= t) return; n   if (x > n) { n     cnt++; n     return; n   } n   dfs(x + 1, now_cost_sum); n   for (int y = 1; y <= n && cnt < t; y++) n     if (!choosen[y] && e[x][y]) { n       choosen[y] = 1; n       dfs(x + 1, now_cost_sum + e[x][y]); n       choosen[y] = 0; n     } n } n int main() { n   scanf( %d%d%d , &n, &k, &t); n   for (int i = 1; i <= k; i++) { n     int h, w, r; n     scanf( %d%d%d , &h, &w, &r); n     e[h][w] = r; n   } n   int low = 0, high = 1000 * n; n   while (low < high) { n     mid = (low + high) >> 1; n     cnt = 0; n     dfs(1, 0); n     if (cnt >= t) n       high = mid; n     else n       low = mid + 1; n   } n   printf( %d n , low); n } n


#include <bits/stdc++.h> n using namespace std; n priority_queue<pair<long long int, long long int> > p_que; n long long int arr[300005]; n long long int ans[300005]; n int main(int argc, char const *argv[]) { n   long long int n, k; n   cin >> n >> k; n   for (int i = 1; i <= n; i++) { n     cin >> arr[i]; n   } n   for (int i = 1; i <= k; i++) { n     p_que.push(make_pair(arr[i], i)); n   } n   long long int tot = 0; n   for (int i = k + 1; i <= n + k; i++) { n     if (i <= n) p_que.push(make_pair(arr[i], i)); n     pair<long long int, long long int> p = p_que.top(); n     p_que.pop(); n     tot = tot + (i - p.second) * p.first; n     ans[p.second] = i; n   } n   cout << tot <<   n ; n   for (int i = 1; i <= n; i++) { n     cout << ans[i] <<    ; n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const long long MX = 2 * 100 * 1000 + 1000, inf = 3e18, n                 mod = 1000 * 1000 * 1000 + 7; n using namespace std; n vector<long long> ans[MX]; n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   long long n = s.size(); n   long long one = 0, num = 0; n   for (long long i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       if (one < num) { n         ans[one].push_back(i); n         one++; n       } else { n         return cout << -1 << endl, 0; n       } n     } else { n       if (one > 0) { n         one--; n         ans[one].push_back(i); n       } else { n         ans[num].push_back(i); n         num++; n       } n     } n   } n   if (one) { n     return cout << -1 << endl, 0; n   } n   cout << num << endl; n   for (long long i = 0; i < num; i++) { n     cout << ans[i].size() <<    ; n     for (long long j = 0; j < ans[i].size(); j++) { n       cout << ans[i][j] + 1 <<    ; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<int> V(n); n   for (int i = 0; i < m; ++i) { n     int k; n     cin >> k; n     V[k - 1]++; n   } n   int mn = 1e9; n   for (int i = 0; i < n; ++i) mn = min(mn, V[i]); n   cout << mn <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[1000 * 1000]; n int main() { n   int t; n   cin >> t; n   double s = 0; n   for (int i = 0; i < t; i++) { n     cin >> a[i]; n     s += a[i]; n   } n   vector<int> ind; n   for (int i = 0; i < t; i++) { n     double p = ((s - a[i]) / (t - 1)); n     if (a[i] == p) ind.push_back(i + 1); n   } n   cout << ind.size() << endl; n   for (int i = 0; i < ind.size(); i++) { n     cout << ind[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 5; n int n; n long long pts[MAXN][2]; n unsigned long long area(int i, int j, int k) { n   return (abs(pts[i][0] * pts[j][1] + pts[j][0] * pts[k][1] + n               pts[k][0] * pts[i][1] - pts[i][1] * pts[j][0] - n               pts[j][1] * pts[k][0] - pts[k][1] * pts[i][0])); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> n; n   for (int i = 0; i < n; ++i) cin >> pts[i][0] >> pts[i][1]; n   int x = -1, y = -1; n   long long c = 2e9, d = 2e9; n   for (int i = 0; i < n; ++i) { n     if (pts[i][0] < c) { n       c = pts[i][0], d = pts[i][1], x = i; n     } else if (pts[i][0] == c && pts[i][1] < d) { n       c = pts[i][0], d = pts[i][1], x = i; n     } n   } n   c = 2e9, d = 2e9; n   for (int i = 0; i < n; ++i) { n     if (i == x) continue; n     if (pts[i][0] < c) { n       c = pts[i][0], d = pts[i][1], y = i; n     } else if (pts[i][0] == c && pts[i][1] < d) { n       c = pts[i][0], d = pts[i][1], y = i; n     } n   } n   long long minarea = 6e18; n   int ind = -1; n   for (int i = 0; i < n; ++i) { n     if (i == x || i == y) continue; n     long long a = area(x, y, i); n     if (a == 0) continue; n     if (a < minarea) { n       minarea = a; n       ind = i; n     } n   } n   cout << x + 1 <<     << y + 1 <<     << ind + 1 << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4,avx2,abm,fma,tune=native ) n #pragma GCC optimize( unroll-loops ) n using namespace std; n const long long INF = 0x3f3f3f3f; n const long long inf = pow(10, 18); n long long modulo = pow(10, 9) + 7; n long long valn[100100]; n long long val[100100]; n long long dp[100100]; n vector<long long> sorted[100100]; n vector<long long> g[100100]; n long long lca[100100][20]; n long long dep[100100]; n long long tin[100100]; n long long tout[100100]; n long long calcval(long long a, long long b) { n   return max(abs(a + b), abs(a - b)); n } n void dfslca(long long hd, long long pr, long long& tim) { n   lca[hd][0] = pr; n   tin[hd] = tim; n   for (auto hr : g[hd]) { n     if (hr == pr) continue; n     dep[hr] = dep[hd] + 1; n     ++tim; n     sorted[hd].push_back(hr); n     dfslca(hr, hd, tim); n   } n   tout[hd] = tim; n } n void pre(long long n) { n   for (long long j = 1; j < 20; j++) n     for (long long i = 0; i < n; i++) lca[i][j] = lca[lca[i][j - 1]][j - 1]; n } n long long LCA(long long a, long long b) { n   if (dep[a] < dep[b]) swap(a, b); n   for (long long j = 20 - 1; j > -1; j--) n     if (dep[lca[a][j]] > dep[b]) a = lca[a][j]; n   if (dep[a] != dep[b]) a = lca[a][0]; n   for (long long j = 20 - 1; j > -1; j--) n     if (lca[a][j] != lca[b][j]) a = lca[a][j], b = lca[b][j]; n   if (a != b) a = lca[a][0], b = lca[b][0]; n   return a; n } n void calcdp(long long hd, long long pr) { n   for (auto& hr : g[hd]) { n     if (hr == pr) { n       continue; n     } n     dp[hr] = dp[hd] + calcval(val[hd], val[hr]); n     calcdp(hr, hd); n   } n } n bool isp(long long pr, long long hd) { n   return tin[pr] <= tin[hd] && tout[hd] <= tout[pr]; n } n long long findChild(long long pr, long long hd) { n   long long lo = 0, hi = (long long)sorted[pr].size() - 1; n   while (lo < hi) { n     long long mid = (lo + hi) / 2 + 1; n     long long hr = sorted[pr][mid]; n     if (tin[hr] > tin[hd]) { n       hi = mid - 1; n     } else { n       lo = mid; n     } n   } n   return sorted[pr][lo]; n } n void deal() { n   long long n, q; n   cin >> n >> q; n   for (long long i = 0; i < n; i++) { n     cin >> val[i]; n     valn[i] = val[i]; n   } n   for (long long i = 0; i < n - 1; i++) { n     long long ai, bi; n     cin >> ai >> bi; n     --ai, --bi; n     g[ai].push_back(bi); n     g[bi].push_back(ai); n   } n   { n     long long tim = 1; n     dfslca(0, 0, tim); n     pre(n); n     calcdp(0, 0); n   } n   map<long long, long long> dif; n   for (long long l = 0; l < q; l++) { n     long long ty; n     cin >> ty; n     if (ty == 1) { n       long long u, c; n       cin >> u >> c; n       --u; n       dif[u] = c; n       valn[u] = c; n     } else { n       long long u, v; n       cin >> u >> v; n       --u, --v; n       long long lc = LCA(u, v); n       long long ans = dp[u] + dp[v] - 2 * dp[lc]; n       unordered_set<long long> again; n       for (auto& el : dif) { n         long long pr = el.first; n         long long p1 = isp(pr, u); n         long long p2 = isp(pr, v); n         if (p1 && (!p2 || pr == lc) && pr != u) { n           long long a = pr; n           long long b = findChild(pr, u); n           if (b < a) { n             swap(a, b); n           } n           again.insert(a * 100100 + b); n         } n         if (p2 && (!p1 || pr == lc) && pr != v) { n           long long a = pr; n           long long b = findChild(pr, v); n           if (b < a) { n             swap(a, b); n           } n           again.insert(a * 100100 + b); n         } n         if (lca[pr][0] != pr && n             ((p1 ^ p2) || (lca[pr][0] == lc && (p1 || p2)))) { n           long long a = pr; n           long long b = lca[pr][0]; n           if (b < a) { n             swap(a, b); n           } n           again.insert(a * 100100 + b); n         } n       } n       for (auto& el : again) { n         long long a = el / 100100, b = el % 100100; n         ans -= calcval(val[a], val[b]); n         ans += calcval(valn[a], valn[b]); n       } n       cout << ans <<   n ; n     } n     if ((long long)dif.size() > 200) { n       for (long long i = 0; i < n; i++) { n         val[i] = valn[i]; n       } n       calcdp(0, 0); n       dif.clear(); n     } n   } n } n int main() { n   cin.tie(0); n   ios_base::sync_with_stdio(0); n   deal(); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const char E =   n ; n const int N = 20015; n const int NN = 1000000; n const long long md = 1e9 + 7; n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   int tttt; n   cin >> tttt; n   while (tttt--) { n     int n; n     cin >> n; n     vector<long long> a(n + 5); n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     long long l = 1, mn = 1001007000; n     long long mx = 0; n     for (int i = 2; i <= n; i++) { n       if (a[i] < a[l] && i != n) { n         mn = min(mn, a[i]); n         continue; n       } n       mn = min(mn, a[i]); n       mx = max(mx, a[l] - mn); n       mn = 1001007000; n       l = i; n     } n     long long ans = 0; n     long long t = 0; n     while (t < mx) { n       ans++; n       t += (1 << (ans - 1)); n     } n     cout << ans << E; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[5011][5011]; n void solve() { n   long long n, m; n   cin >> n >> m; n   string s1, s2; n   cin >> s1 >> s2; n   s1 =  .  + s1; n   s2 =  .  + s2; n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     for (long long j = 1; j <= m; j++) { n       dp[i][j] = max({0ll, dp[i - 1][j] - 1, dp[i][j - 1] - 1}); n       if (s1[i] == s2[j]) n         dp[i][j] = max( n             {0ll, dp[i - 1][j] - 1, dp[i][j - 1] - 1, dp[i - 1][j - 1] + 2}); n       ans = max(ans, dp[i][j]); n     } n   } n   cout << ans; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int t; n   cin >> t; n   while (t--) { n     int a[3]; n     cin >> a[0] >> a[1] >> a[2]; n     sort(a, a + 3); n     if (a[2] == a[0] + a[1]) n       cout <<  YES ; n     else if (a[2] == a[1] && a[0] % 2 == 0) n       cout <<  YES ; n     else if (a[0] == a[1] && a[2] % 2 == 0) n       cout <<  YES ; n     else n       cout <<  NO ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 15; n const int LL = 1e9 + 15; n const int SSS = 1e6 + 15; n int dx[10] = {0, 1, -1, 1, -1, 2, -2, 2, -2}; n int dy[10] = {0, 2, 2, -1, -1, 1, 1, -1, -1}; n int n, ii, a[5010], ans[5010], mx, st[5010]; n int main() { n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     mx = 0; n     ii = 0; n     for (int j = 1; j <= n; ++j) st[j] = 0; n     for (int j = i; j >= 1; --j) { n       ++st[a[j]]; n       if (st[a[j]] > mx || (st[a[j]] == mx && ii > a[j])) { n         mx = st[a[j]]; n         ii = a[j]; n       } n       ++ans[ii]; n     } n   } n   for (int i = 1; i <= n; ++i) printf( %d  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<char> v; n   long long ca = 0, ci = 0, cf = 0; n   for (long long i = 0; i < n; i++) { n     char a; n     cin >> a; n     v.push_back(a); n     if (a ==  A ) ca++; n     if (a ==  I ) ci++; n     if (a ==  F ) cf++; n   } n   if (ca + cf == n) n     cout << ca; n   else { n     if (ci > 1) n       cout << 0; n     else n       cout << ci; n   } n   for (long long i = 1; i < n; i++) { n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long LINF = 0x3f3f3f3f3f3f3f3f; n const int MAXA = 2e5 + 10; n const int MAX = 2e3 + 10; n int n, m, k; n int a[MAXA]; n int M = 0; n pair<int, int> p[MAX]; n long long sum[MAX]; n long long memo[MAX]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> k; n   for (int i = 0; i < n; i++) cin >> a[i]; n   vector<int> f(MAX, -1); n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     if (x > MAX) continue; n     f[x] = max(f[x], y); n   } n   for (int i = 1; i < MAX; i++) { n     if (f[i] == -1) continue; n     p[M++] = {i, f[i]}; n   } n   partial_sort(&a[0], &a[min(n, MAX)], &a[n]); n   sum[0] = 0; n   for (int i = 0; i < k; i++) { n     sum[i + 1] = sum[i] + a[i]; n   } n   memo[k] = 0; n   for (int i = k - 1; i >= 0; i--) { n     long long ans = a[i] + memo[i + 1]; n     for (int j = 0; j < M; j++) { n       if (i + p[j].first > k) break; n       long long r = memo[i + p[j].first]; n       ans = min(ans, sum[i + p[j].first] - sum[i + p[j].second] + r); n     } n     memo[i] = ans; n   } n   cout << memo[0] <<   n ; n   exit(0); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a, b, c, d, e, f, g; n   cin >> a >> b >> c; n   d = a + b + c; n   e = 2 * (a + b); n   f = 2 * (b + c); n   g = 2 * (c + a); n   if (d <= e && d <= f && d <= g) { n     cout << d; n   } else if (e <= f && e <= g) { n     cout << e; n   } else if (f <= g) { n     cout << f; n   } else { n     cout << g; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T BigMod(T b, T p, T m) { n   if (p == 0) return 1; n   if (p % 2 == 0) { n     T s = BigMod(b, p / 2, m); n     return ((s % m) * (s % m)) % m; n   } n   return ((b % m) * (BigMod(b, p - 1, m) % m)) % m; n } n template <typename T> n T ModInv(T b, T m) { n   return BigMod(b, m - 2, m); n } n template <typename T> n T gcd(T a, T b) { n   if (!b) return a; n   return gcd(b, a % b); n } n int a[1004]; n vector<int> v; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t, i, j, e, n, k, l, ed, res, p, y, n1, n2, sum, cnt, dif, idx, x, m, val, n       cst, st, sz; n   cin >> x; n   for (i = 1; i <= x; i++) { n     cin >> a[i]; n     if (i > 1) v.push_back(a[i] - a[i - 1]); n   } n   bool fl = 0; n   sz = v.size(); n   for (i = 0; i <= sz - 1; i++) { n     if (i > 0) { n       if (v[i] == v[i - 1]) continue; n       fl = 1; n       break; n     } n   } n   if (fl) n     cout << a[x] << endl; n   else n     cout << a[x] + v[sz - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, s, x, y, nb; n vector<pair<long long, long long> > t, v; n long long dp[102]; n bool cmp(pair<long long, long long> a, pair<long long, long long> b) { n   return a.first + a.second > b.first + b.second; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> s; n   for (long long i = 0; i < n; i++) { n     cin >> x >> y; n     if (y >= 0) { n       t.push_back(make_pair(x, y)); n     } else { n       v.push_back(make_pair(y, x)); n     } n   } n   sort(t.begin(), t.end()); n   for (long long i = 0; i < t.size(); i++) { n     if (t[i].first > s) break; n     s += t[i].second; n     nb++; n   } n   sort(v.begin(), v.end(), cmp); n   n = v.size(); n   for (long long i = 1; i < n + 1; i++) dp[i] = -1; n   dp[0] = s; n   for (long long i = 0; i < n; i++) { n     for (long long j = i + 1; j > 0; j--) { n       if (dp[j - 1] >= v[i].second) dp[j] = max(dp[j], dp[j - 1] + v[i].first); n     } n   } n   long long i; n   for (i = n; dp[i] == -1; i--) n     ; n   cout << nb + i <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct dulieu { n   long int gt, tt; n }; n long int n; n dulieu a[3 * 100005]; n bool sx1(dulieu a, dulieu b) { return a.gt < b.gt; } n bool sx2(dulieu a, dulieu b) { return a.tt < b.tt; } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i].gt; n     a[i].tt = i; n   } n   sort(a, a + n, sx1); n   for (int i = 0; i < n; i++) n     if (a[i].gt <= a[i - 1].gt) a[i].gt = a[i - 1].gt + 1; n   sort(a, a + n, sx2); n   for (int i = 0; i < n; i++) cout << a[i].gt <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n char a[1001], b[1001]; n int main() { n   int sa, sb, i, j, ta = 0, tb = 0; n   cin >> a >> b; n   sa = strlen(a); n   sb = strlen(b); n   for ((i) = 0; (i) < (sa); (i)++) n     if (a[i] ==  1 ) ta++; n   for ((i) = 0; (i) < (sb); (i)++) n     if (b[i] ==  1 ) tb++; n   if (ta >= tb) n     cout <<  YES  << endl; n   else if (ta % 2 && tb == ta + 1) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> tree[100005]; n int bad[100005]; n vector<int> result; n int SubTree[100005]; n int dfs(int v, int pv) { n   SubTree[v] = (int)(bad[v] == 1); n   for (auto u : tree[v]) { n     if (u == pv) continue; n     SubTree[v] += dfs(u, v); n   } n   if (SubTree[v] == 1) result.push_back(v); n   return SubTree[v]; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int N; n   cin >> N; n   for (int i = 0; i < N - 1; i++) { n     int a, b, c; n     cin >> a >> b >> c; n     tree[a].push_back(b); n     tree[b].push_back(a); n     if (c == 2) { n       bad[a] = bad[b] = 1; n     } n   } n   dfs(1, 0); n   cout << result.size() <<   n ; n   for (auto i : result) cout << i <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1e9; n const int mod = 1000000007; n int N; n vector<int> g[100001]; n vector<int> time_in(100001); n vector<int> time_out(100001); n int cnt; n void dfs(int v) { n   cnt++; n   time_in[v] = cnt; n   for (int i : g[v]) { n     if (time_in[i] == 0) dfs(i); n   } n   cnt++; n   time_out[v] = cnt; n } n void slv() { n   cin >> N; n   if (N % 2) { n     cout << -1; n     return; n   } n   int x, y; n   for (int i = 1; i <= N - 1; i++) { n     cin >> x >> y; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   for (int i = 1; i <= N; i++) { n     if (g[i].size() == 1) { n       dfs(i); n       break; n     } n   } n   int ans = 0; n   for (int i = 1; i <= N; i++) { n     int cur = time_out[i] - time_in[i] + 1; n     if (cur == 0) continue; n     if (cur % 4 == 0) { n       ans++; n     } n   } n   cout << (ans == 0 ? -1 : ans - 1); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int T = 1; n   while (T--) slv(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100009; n long long n, a[maxn], b[maxn], c[maxn], in[maxn], color[maxn]; n void read(long long a[]) { n   for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); n } n void over() { n   cout << -1; n   exit(0); n } n vector<int> vec[maxn]; n void dfs(int now, int fa, int se1, int se2) { n   for (int i = 0; i < vec[now].size(); i++) { n     int v = vec[now][i]; n     if (v == fa) continue; n     color[v] = 6 - se1 - se2; n     dfs(v, now, se2, color[v]); n   } n } n long long ans = 1e18; n long long isok(int i, int s) { n   if (s == 1) n     return a[i]; n   else if (s == 2) n     return b[i]; n   else n     return c[i]; n } n void DP(int q, int w, int e) { n   long long temp = 0; n   for (int i = 1; i <= n; i++) n     if (color[i] == 1) n       temp += isok(i, q); n     else if (color[i] == 2) n       temp += isok(i, w); n     else n       temp += isok(i, e); n   ans = min(ans, temp); n } n void sovle(int q, int w, int e) { n   long long temp = 0; n   for (int i = 1; i <= n; i++) n     if (color[i] == 1) n       temp += isok(i, q); n     else if (color[i] == 2) n       temp += isok(i, w); n     else n       temp += isok(i, e); n   if (temp != ans) return; n   for (int i = 1; i <= n; i++) n     if (color[i] == 1) n       cout << q <<    ; n     else if (color[i] == 2) n       cout << w <<    ; n     else n       cout << e <<    ; n   exit(0); n } n int main() { n   cin >> n; n   read(a), read(b), read(c); n   for (int i = 1; i < n; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     in[l]++, in[r]++; n     vec[l].push_back(r); n     vec[r].push_back(l); n   } n   for (int i = 1; i <= n; i++) n     if (in[i] >= 3) over(); n   int last; n   for (int i = 1; i <= n; i++) n     if (in[i] == 1) last = i; n   color[last] = 1, color[vec[last][0]] = 2; n   dfs(vec[last][0], last, 1, 2); n   for (int i = 1; i <= 3; i++) n     for (int j = 1; j <= 3; j++) { n       if (j == i) continue; n       for (int q = 1; q <= 3; q++) { n         if (q == i || q == j) continue; n         DP(i, j, q); n       } n     } n   cout << ans << endl; n   for (int i = 1; i <= 3; i++) n     for (int j = 1; j <= 3; j++) { n       if (j == i) continue; n       for (int q = 1; q <= 3; q++) { n         if (q == i || q == j) continue; n         sovle(i, j, q); n       } n     } n } n


#include <bits/stdc++.h> n using namespace std; n unordered_set<int> int_set; n set<int, greater<int> > divisors; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   string a; n   cin >> a; n   if ((a.size()) == 1) { n     cout << a << a <<   n ; n     return 0; n   } else { n     string rev = string(a.rbegin(), a.rend()); n     a += rev; n     cout << a <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 2.0 * acos(0.0); n const double EPS = 1e-6; n int a[100005]; n bitset<100005> sign; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d , a + i); n   long long sum = 0LL; n   for (int i = n - 1; i >= 0; i--) { n     if (abs(sum - a[i]) < abs(sum + a[i])) { n       sum -= a[i]; n       sign[i] = true; n     } else { n       sum += a[i]; n     } n   } n   for (int i = 0; i < n; i++) printf( %c , sign[i] ^ (sum > 0) ?  +  :  - ); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n struct Node { n   long long x, y; n } p[N]; n int n; n double ans, a, b, c, xx, x1, yy1, x2, x3, y2, y3, x, y; n int tf; n map<pair<long long, long long>, int> f; n inline double dis(Node a, Node b) { n   return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); n } n inline long long dis1(Node a, Node b) { n   return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); n } n inline double check(double x, double y) { n   double ans = 2e9 + 7; n   for (int i = 1; i <= n; i++) n     ans = std::min( n         ans, sqrt((x - p[i].x) * (x - p[i].x) + (y - p[i].y) * (y - p[i].y))); n   return ans; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %lld%lld , &p[i].x, &p[i].y); n     f[make_pair(p[i].x, p[i].y)] = 1; n   } n   ans = -1; n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       for (int k = j + 1; k <= n; k++) { n         a = dis(p[i], p[j]), b = dis(p[i], p[k]), c = dis(p[j], p[k]), tf = 1; n         if (abs(a - b) < 1e-10 && abs(c * c - a * a - b * b) < 1e-10 && n             f[make_pair(p[j].x + p[k].x - p[i].x, p[j].y + p[k].y - p[i].y)]) n           x = (p[j].x + p[k].x) / 2.0, y = (p[j].y + p[k].y) / 2.0; n         else if (abs(a - c) < 1e-10 && abs(b * b - a * a - c * c) < 1e-10 && n                  f[make_pair(p[i].x + p[k].x - p[j].x, n                              p[i].y + p[k].y - p[j].y)]) n           x = (p[i].x + p[k].x) / 2.0, y = (p[i].y + p[k].y) / 2.0; n         else if (abs(b - c) < 1e-10 && abs(a * a - b * b - c * c) < 1e-10 && n                  f[make_pair(p[j].x + p[i].x - p[k].x, n                              p[j].y + p[i].y - p[k].y)]) n           x = (p[j].x + p[i].x) / 2.0, y = (p[j].y + p[i].y) / 2.0; n         else n           tf = 0; n         if (tf) { n           xx = check(x, y); n           if (abs(xx - sqrt((x - p[i].x) * (x - p[i].x) + n                             (y - p[i].y) * (y - p[i].y))) < 1e-10 || n               xx - sqrt((x - p[i].x) * (x - p[i].x) + n                         (y - p[i].y) * (y - p[i].y)) > n                   1e-10) n             ans = std::max(ans, sqrt((x - p[i].x) * (x - p[i].x) + n                                      (y - p[i].y) * (y - p[i].y))); n         } n         if (abs(a + b - c) < 1e-10 || c - a - b > 1e-10 || n             abs(a + c - b) < 1e-10 || b - a - c > 1e-10 || n             abs(b + c - a) < 1e-10 || a - b - c > 1e-10) n           continue; n         if (dis1(p[i], p[j]) + dis1(p[i], p[k]) <= dis1(p[j], p[k]) || n             dis1(p[j], p[i]) + dis1(p[j], p[k]) <= dis1(p[i], p[k]) || n             dis1(p[k], p[i]) + dis1(p[k], p[j]) <= dis1(p[i], p[j])) n           continue; n         x1 = p[i].x, x2 = p[j].x, x3 = p[k].x, yy1 = p[i].y, y2 = p[j].y, n         y3 = p[k].y; n         x = ((x1 * x1 - x2 * x2 + yy1 * yy1 - y2 * y2) * (yy1 - y3) - n              (x1 * x1 - x3 * x3 + yy1 * yy1 - y3 * y3) * (yy1 - y2)) / n             (2 * (yy1 - y3) * (x1 - x2) - 2 * (yy1 - y2) * (x1 - x3)); n         y = ((x1 * x1 - x2 * x2 + yy1 * yy1 - y2 * y2) * (x1 - x3) - n              (x1 * x1 - x3 * x3 + yy1 * yy1 - y3 * y3) * (x1 - x2)) / n             (2 * (yy1 - y2) * (x1 - x3) - 2 * (yy1 - y3) * (x1 - x2)); n         xx = check(x, y); n         if (abs(xx - sqrt((x - p[i].x) * (x - p[i].x) + n                           (y - p[i].y) * (y - p[i].y))) < 1e-10 || n             xx - sqrt((x - p[i].x) * (x - p[i].x) + n                       (y - p[i].y) * (y - p[i].y)) > n                 1e-10) n           ans = std::max(ans, sqrt((x - p[i].x) * (x - p[i].x) + n                                    (y - p[i].y) * (y - p[i].y))); n       } n     } n   } n   if (ans < 0) n     printf( -1 n ); n   else n     printf( %.6lf n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   scanf( %d , &n); n   int m = n / 3; n   for (int i = 1; i <= m; i++) printf( %d %d n , 4, 2 * i); n   for (int i = 1; i <= n - m; i++) printf( %d %d n , 1, i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n void solve() { n   int m, n; n   cin >> n >> m; n   vector<long long> v(n); n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (char c : s) { n       v[i] *= 2; n       v[i] += c -  0 ; n     } n   } n   long long need = ((1ll << m) - n - 1) / 2 + 1; n   long long cur = (1ll << (m - 1)) - 1; n   while (true) { n     long long left = cur + 1; n     bool flag = false; n     for (int i = 0; i < n; i++) { n       flag |= v[i] == cur; n       if (v[i] <= cur) { n         left--; n       } n     } n     if (left == need && !flag) { n       string s; n       for (int i = 0; i < m; i++) { n         s += (char)(cur % 2 +  0 ); n         cur /= 2; n       } n       reverse(s.begin(), s.end()); n       cout << s << endl; n       return; n     } else if (left < need) { n       cur++; n     } else { n       cur--; n     } n   } n } n int main() { n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int q; n int cnt[1 << 20]; n int trans(long long v) { n   int rt = 0; n   for (int i = 0; i < 18; i++, v /= 10) { n     if ((v % 10) & 1) { n       rt |= 1 << i; n     } n   } n   return rt; n } n void insert(long long v) { cnt[trans(v)]++; } n void erase(long long v) { cnt[trans(v)]--; } n int query(long long v) { n   int bit = 0; n   for (int i = 0; i < 18; i++, v /= 10) { n     if (v & 1) { n       bit |= 1 << i; n     } n   } n   return cnt[bit]; n } n int main() { n   cin >> q; n   while (q--) { n     char ch[10]; n     long long v; n     cin >> ch >> v; n     if (ch[0] ==  + ) { n       insert(v); n     } else if (ch[0] ==  - ) { n       erase(v); n     } else { n       cout << query(v) << endl; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int dp[201]; n int main() { n   int n, t, w, sumt = 0, sumw = 0; n   memset(dp, -1, sizeof(dp)); n   dp[0] = 0; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &t, &w); n     sumt += t; n     sumw += w; n     for (int j = 200; j >= t; j--) { n       if (dp[j - t] != -1) dp[j] = max(dp[j], dp[j - t] + w); n     } n   } n   int ans = sumt; n   for (int i = 0; i <= sumt; i++) { n     if (sumw - dp[i] <= i) { n       ans = i; n       break; n     } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n const int inf = (1 << 28) - 1; n struct Point { n   double x, y; n   Point() {} n   Point(double _x, double _y) { n     x = _x; n     y = _y; n   } n   Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); } n   double operator^(const Point &b) const { return x * b.y - y * b.x; } n   double operator*(const Point &b) const { return x * b.x + y * b.y; } n   void transXY(double B) { n     double tx = x, ty = y; n     x = tx * cos(B) - ty * sin(B); n     y = tx * sin(B) + ty * cos(B); n   } n }; n struct Line { n   Point s, e; n   Line() {} n   Line(Point _s, Point _e) { n     s = _s; n     e = _e; n   } n }; n double dist(Point a, Point b) { return sqrt((a - b) * (a - b)); } n Point NearestPointToLineSeg(Point P, Line L) { n   Point result; n   double t = ((P - L.s) * (L.e - L.s)) / ((L.e - L.s) * (L.e - L.s)); n   if (t >= 0 && t <= 1) { n     result.x = L.s.x + (L.e.x - L.s.x) * t; n     result.y = L.s.y + (L.e.y - L.s.y) * t; n   } else { n     if (dist(P, L.s) < dist(P, L.e)) n       result = L.s; n     else n       result = L.e; n   } n   return result; n } n const double PAI = acos(-1); n int main() { n   Point A, tmp, now, sta; n   int n; n   scanf( %d%lf%lf , &n, &A.x, &A.y); n   double Max = -1, Min = inf; n   for (int i = 1; i <= n; ++i) { n     scanf( %lf%lf , &now.x, &now.y); n     if (i > 1) n       Min = min(Min, dist(A, NearestPointToLineSeg(A, Line(tmp, now)))); n     else n       sta = now; n     Min = min(Min, dist(A, now)); n     Max = max(Max, dist(A, now)); n     tmp = now; n   } n   Min = min(Min, dist(A, NearestPointToLineSeg(A, Line(sta, now)))); n   printf( %.18lf n , (PAI * Max * Max) - (PAI * Min * Min)); 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   long long n; n   int k; n   cin >> n >> k; n   long long num = 0; n   long long idx = 1; n   long long todie = n; n   for (int i = 0; i < k; ++i) { n     long long x; n     cin >> x; n     if (n % x < todie) { n       todie = n % x; n       idx = i + 1; n       num = n / x; n     } n   } n   cout << idx <<     << num; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int N; n   scanf( %d , &N); n   long long res = 0; n   for (int i = 2; i <= N / 2; i++) { n     long long m = N / i; n     res += (m * (m + 1) / 2 - 1) * 4; n   } n   printf( %lld n , res); n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = acos(-1.0); n const double eps = 1e-6; n const int INF = 0x3f3f3f3f; n const int MAXN = 2e3 + 10; n int ans[MAXN * 3], vis[MAXN][MAXN]; n int dp[MAXN][MAXN], n, kk, dir[4][2] = {1, 0, 0, 1}; n char mp[MAXN][MAXN]; n struct Node { n   int x, y, s; n   Node(int x, int y, int s) : x(x), y(y), s(s){}; n }; n queue<Node> q; n void bfs() { n   while (!q.empty()) { n     Node u = q.front(); n     q.pop(); n     int x = u.x; n     int y = u.y; n     int s = u.s; n     if (n == 100 && kk == 1 && q.size() > 100000) { n       printf( siz = %d, x = %d, y = %d, s = %d n , q.size(), x, y, s); n       return; n     } n     if (s >= 2 * n - 1) continue; n     if (mp[x][y] -  a  > ans[s]) continue; n     for (int i = 0; i < 2; i++) { n       int dx = x + dir[i][0]; n       int dy = y + dir[i][1]; n       if (dx > n || dy > n) continue; n       if (mp[dx][dy] -  a  <= ans[s + 1] && vis[dx][dy] == 0) { n         vis[dx][dy] = 1; n         ans[s + 1] = mp[dx][dy] -  a ; n         q.push(Node(dx, dy, s + 1)); n       } n     } n   } n } n int main() { n   cin >> n >> kk; n   memset(ans, INF, sizeof ans); n   memset(dp, -1, sizeof dp); n   for (int i = 1; i <= n; i++) scanf( %s , mp[i] + 1); n   int cnt = 0; n   if (kk) { n     if (mp[1][1] ==  a ) n       dp[1][1] = kk; n     else n       dp[1][1] = kk - 1; n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) { n         if (i - 1 >= 1 && dp[i - 1][j] >= 0) { n           int tmp = dp[i - 1][j]; n           if (mp[i][j] !=  a ) tmp--; n           dp[i][j] = max(dp[i][j], tmp); n         } n         if (j - 1 >= 1 && dp[i][j - 1] >= 0) { n           int tmp = dp[i][j - 1]; n           if (mp[i][j] !=  a ) tmp--; n           dp[i][j] = max(dp[i][j], tmp); n         } n         if (dp[i][j] >= 0) cnt = max(cnt, i + j - 1); n       } n     if (dp[n][n] >= 0) { n       for (int i = 1; i < 2 * n; i++) printf( a ); n       return 0; n     } n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         if (dp[i][j] == 0 && i + j - 1 == cnt) { n           vis[i][j] = 1; n           q.push(Node(i, j, cnt)); n           mp[i][j] =  a ; n         } n     for (int i = 1; i <= cnt; i++) ans[i] = 0; n   } else { n     vis[1][1] = 1; n     q.push(Node(1, 1, 1)); n     ans[1] = mp[1][1] -  a ; n   } n   bfs(); n   for (int i = 1; i < 2 * n; i++) printf( %c , ans[i] +  a ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void read(int &sum) { n   sum = 0; n   char c = getchar(); n   bool f = 0; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   if (f) sum = -sum; n } n void read(long long &sum) { n   sum = 0; n   char c = getchar(); n   bool f = 0; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = 1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   if (f) sum = -sum; n } n void readvec(vector<int> &a, int n) { n   for (int i = 0; i < n; i++) read(a[i]); n } n void readvec(vector<long long> &a, long long n) { n   for (int i = 0; i < n; i++) read(a[i]); n } n const int MOD = 1e9 + 7; n const int INF = (int)2e9 + 7; n const long long LINF = (long long)1e18; n const long double PI = 3.1415926535897932384626433832795; n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n void solve() { n   int n; n   read(n); n   vector<int> v(n); n   readvec(v, n); n   if (n == 1) { n     cout << -1; n     return; n   } n   if (n == 2 && v[0] == v[1]) { n     cout << -1; n     return; n   } n   int ind, mn = 1001; n   for (int i = 0; i < n; i++) { n     if (v[i] < mn) { n       mn = v[i]; n       ind = i; n     } n   } n   cout << 1 <<   n ; n   cout << ind + 1 <<   n ; n } n int32_t main() { n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n const int MAXN = 1000010; n int prime[5005]; n long long f[MAXN][22]; n void getPrime() { n   for (int i = 2; i <= 5000; i++) { n     if (!prime[i]) prime[++prime[0]] = i; n     for (int j = 1; j <= prime[0] && prime[j] <= 5000 / i; j++) { n       prime[prime[j] * i] = 1; n       if (i % prime[j] == 0) break; n     } n   } n } n void init() { n   f[0][0] = 1; n   for (int i = 1; i <= 20; ++i) { n     f[0][i] = 2; n   } n   for (int i = 1; i <= 1000000; ++i) { n     f[i][0] = 1; n     for (int j = 1; j <= 20; ++j) { n       f[i][j] = (f[i][j - 1] + f[i - 1][j]) % MOD; n     } n   } n } n int q, r, n; n int main() { n   init(); n   getPrime(); n   scanf( %d , &q); n   while (q--) { n     scanf( %d%d , &r, &n); n     long long ans = 1; n     int cnt = 0; n     for (int i = 1, tmp; prime[i] * prime[i] <= n && i <= prime[0]; ++i) { n       tmp = prime[i]; n       if (n == 1) break; n       if (n % tmp == 0) { n         while (n % tmp == 0) { n           n /= tmp; n           cnt++; n         } n       } n       ans = ans * f[r][cnt] % MOD; n       cnt = 0; n     } n     if (n > 1) { n       ans = ans * (long long)f[r][1] % MOD; n     } n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int gi() { n   int x; n   scanf( %d , &x); n   return x; n } n struct Segment_Tree { n   int dep, f, tag; n } t[200005 << 2]; n struct Qry { n   int x, tm, id; n } q[200005]; n struct _Edge { n   int to, next; n } Edge[200005 << 1]; n int head[200005], CNT; n int top[200005], dfn[200005], rec[200005], dep[200005], son[200005], sz[200005], n     ans[200005], fa[200005], n, m, tot; n inline void AddEdge(int u, int v) { n   Edge[++CNT] = (_Edge){v, head[u]}; n   head[u] = CNT; n   return; n } n void dfs1(int u, int deep) { n   dep[u] = deep; n   son[u] = 0; n   sz[u] = 1; n   for (int e = head[u]; e; e = Edge[e].next) { n     int v = Edge[e].to; n     dfs1(v, deep + 1); n     sz[u] += sz[v]; n     if (!son[u] || sz[son[u]] < sz[v]) son[u] = v; n   } n   return; n } n void dfs2(int u, int upp) { n   top[u] = upp; n   dfn[u] = ++tot; n   rec[tot] = u; n   if (son[u]) n     dfs2(son[u], upp); n   else n     return; n   for (int e = head[u]; e; e = Edge[e].next) n     if (Edge[e].to != son[u]) dfs2(Edge[e].to, Edge[e].to); n   return; n } n void Build(int o, int l, int r) { n   t[o].f = t[o].tag = -2147483640; n   if (l == r) { n     t[o].dep = 2 * dep[rec[l]]; n     return; n   } n   int mid = (l + r) >> 1; n   Build(o << 1, l, mid); n   Build(o << 1 | 1, mid + 1, r); n   t[o].dep = max(t[o << 1].dep, t[o << 1 | 1].dep); n } n inline void up(int o, int v) { n   t[o].f = max(t[o].f, v + t[o].dep); n   t[o].tag = max(t[o].tag, v); n } n inline void PushDown(int o) { n   if (t[o].tag == -2147483640) return; n   up(o << 1, t[o].tag); n   up(o << 1 | 1, t[o].tag); n   t[o].tag = -2147483640; n   return; n } n void Update(int o, int l, int r, int ql, int qr, int v) { n   if (ql <= l && r <= qr) { n     up(o, v); n     return; n   } n   int mid = (l + r) >> 1; n   PushDown(o); n   if (ql <= mid) Update(o << 1, l, mid, ql, qr, v); n   if (mid < qr) Update(o << 1 | 1, mid + 1, r, ql, qr, v); n   t[o].f = max(t[o << 1].f, t[o << 1 | 1].f); n } n int Query(int o, int l, int r, int ql, int qr) { n   if (ql <= l && r <= qr) return t[o].f; n   int mid = (l + r) >> 1, ret = -2147483640; n   PushDown(o); n   if (ql <= mid) ret = max(ret, Query(o << 1, l, mid, ql, qr)); n   if (mid < qr) ret = max(ret, Query(o << 1 | 1, mid + 1, r, ql, qr)); n   return ret; n } n inline int Calc(int x, int tm) { n   int vl = dep[x] + tm, u = x, ret = 0, Ans; n   while (u) { n     if (Query(1, 1, n, dfn[top[u]], dfn[u]) <= vl) { n       u = fa[top[u]]; n     } else { n       int l = dfn[top[u]] + 1, r = dfn[u] + 1; n       if (Query(1, 1, n, dfn[u], dfn[u]) > vl) n         ret = u; n       else { n         while (l <= r) { n           int mid = (l + r) >> 1; n           if (Query(1, 1, n, mid, dfn[u]) <= vl) n             ret = mid, r = mid - 1; n           else n             l = mid + 1; n         } n         ret = fa[rec[ret]]; n       } n       break; n     } n   } n   if (!ret) ret = 1; n   u = x; n   Ans = 2 * (dep[x] - dep[ret]) + tm; n   while (top[u] ^ top[ret]) { n     Update(1, 1, n, dfn[top[u]], dfn[u], Ans - dep[x]); n     u = fa[top[u]]; n   } n   Update(1, 1, n, dfn[ret], dfn[u], Ans - dep[x]); n   return Ans; n } n inline bool cmp1(Qry A, Qry B) { n   return ((dep[A.x] + A.tm) ^ (dep[B.x] + B.tm)) n              ? dep[A.x] + A.tm < dep[B.x] + B.tm n              : A.x < B.x; n } n int main() { n   n = gi() + 1; n   m = gi(); n   for (int i = 2; i <= n; i++) fa[i] = gi() + 1, AddEdge(fa[i], i); n   dfs1(1, 1); n   dfs2(1, 1); n   Build(1, 1, n); n   for (int i = 1; i <= m; i++) q[i].x = gi() + 1, q[i].tm = gi(), q[i].id = i; n   sort(q + 1, q + m + 1, cmp1); n   for (int i = 1; i <= m; i++) ans[q[i].id] = Calc(q[i].x, q[i].tm); n   for (int i = 1; i <= m; i++) printf( %d n , ans[i]); 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   cout.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n, i; n     int d = 0, c = 0; n     cin >> n; n     string s; n     cin >> s; n     for (i = n - 1; i >= 0; i--) { n       if (s[i] ==  P ) n         c++; n       else { n         d = max(d, c); n         c = 0; n       } n     } n     cout << d << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n const long long inf = 1e18; n const int maxn = 2e5 + 10; n const double eps = 1e-7; n const double pi = 3.14159265359; n long long n, m, a[maxn], par[maxn]; n bool check(int x) { n   long long now = 0; n   long long cnt = 0; n   long long cal = 0; n   for (int i = 0; i < n; i++) { n     if (cal >= x) { n       cal = 0; n       cnt++; n     } n     if (a[i] - cnt > 0) { n       now += a[i] - cnt; n       cal++; n     } n   } n   if (now >= m) { n     return 1; n   } n   return 0; n } n int main() { n   std::ios::sync_with_stdio(0); n   ; n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   reverse(a, a + n); n   par[0] = a[0]; n   for (int i = 1; i < n; i++) { n     par[i] = par[i - 1] + a[i]; n   } n   int ans = -1; n   long long l = 1, r = n + 5, mid; n   while (l <= r) { n     mid = (l + r) / 2; n     if (check(mid)) { n       ans = mid; n       r = mid - 1; n     } else { n       l = mid + 1; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n struct str { n   long long num; n   long long id; n } st[666666]; n long long arr[666666]; n bool cmp(str a, str b) { return a.num < b.num; } n signed main() { n   long long n; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> arr[i]; n   } n   long long ans = min(arr[1], arr[n]) / (n - 1); n   for (long long i = 2; i < n; i++) { n     long long t1 = min(arr[i], arr[1]) * 1ll / (i - 1) * 1ll; n     long long t2 = min(arr[i], arr[n]) / (n - i) * 1ll; n     ans = min(ans, min(t1, t2)); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, q; n int a[100005]; n long long tree[2 * 100005], tree2[2 * 100005]; n vector<long long> mapa; n int mp(double val) { n   return upper_bound(mapa.begin(), mapa.end(), val) - mapa.begin(); n } n void update(int pos, int val) { n   for (int i = pos; i < 2 * 100005; i += i & -i) tree[i] += val; n } n void update2(int pos, int val) { n   for (int i = pos; i < 2 * 100005; i += i & -i) tree2[i] += val; n } n void upd(int pos, int val) { n   int posi = mp(a[pos - 1]); n   update(posi, -a[pos - 1]); n   update2(posi, -1); n   a[pos - 1] = val; n   posi = mp(val); n   update(posi, val); n   update2(posi, 1); n } n long long query(int pos) { n   long long res = 0; n   for (int i = pos; i > 0; i -= i & -i) res += tree[i]; n   return res; n } n long long query2(int pos) { n   long long res = 0; n   for (int i = pos; i > 0; i -= i & -i) res += tree2[i]; n   return res; n } n long double f(long double x) { n   double res; n   int pos = mp(x); n   res = x * query2(pos) - query(pos); n   return res; n } n struct quer { n   int t, p; n   long long v; n   quer(){}; n   quer(int t, int p, long long v) : t(t), p(p), v(v){}; n } qry[100005]; n int main() { n   scanf( %d%d , &n, &q); n   for (int i = 0; i < n; i++) scanf( %d , a + i), mapa.push_back(a[i]); n   for (int i = 0, t, p = 0; i < q; i++) { n     long long v; n     cin >> t; n     if (t == 1) n       cin >> p >> v, mapa.push_back(v); n     else n       cin >> v; n     qry[i] = quer(t, p, v); n   } n   sort(mapa.begin(), mapa.end()); n   mapa.resize(unique(mapa.begin(), mapa.end()) - mapa.begin()); n   for (int i = 0; i < n; i++) { n     int posi = mp(a[i]); n     update(posi, a[i]); n     update2(posi, 1); n   } n   for (int i = 0; i < q; i++) { n     quer Q = qry[i]; n     if (Q.t == 1) { n       upd(Q.p, Q.v); n     } else { n       double l = 0, r = 1000000001; n       while (l + 1e-5 <= r) { n         double m = l + (r - l) / 2; n         if (f(m) < Q.v) n           l = m + 1e-5; n         else n           r = m; n       } n       if (abs(l - 1000000001) <= 1e-5 && abs(f(l) - Q.v) > 1e-5) { n         l = 1.0 * (Q.v + query(2 * 100005 - 1)) / n; n       } n       printf( %.5lf n , l); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cnt = 0; n int SM[100000]; n int ans[500][500][2], FC[500][500][2], H[5]; n vector<int> Q[500]; n int dfs(int a, int b) { n   int sum = 0; n   bool A = false, B = false; n   for (int i = 0; i < 5; i++) { n     if (Q[a][i] > 0) { n       for (int j = 0; j < 5; j++) n         if (Q[b][j] > 0 && i * j != 0) { n           sum++; n         } n     } n   } n   return sum; n } n struct T { n   int a, b, c; n }; n queue<T> G; n int main() { n   int sum = 0, k, g = 10000; n   for (int i = 0; i < 5; i++) H[i] = g, g /= 10; n   for (int i = 0; i < 500; i++) n     for (int j = 0; j < 5; j++) Q[i].push_back(0); n   for (int i = 1; i < 100000; i++) { n     int u = 0, p = i + 100000, r = 4; n     while (p > 1) u += p % 10, Q[cnt][r--] = p % 10, p /= 10; n     if (u == 8) { n       SM[i] = cnt; n       sum++, cnt++; n     } n   } n   for (int i = 0; i < cnt; i++) n     for (int j = 0; j < cnt; j++) FC[i][j][0] = FC[i][j][1] = dfs(i, j); n   for (int i = 0; i < cnt; i++) n     for (int j = 0; j < cnt; j++) n       for (int k = 0; k < 2; k++) { n         if (i == cnt - 1) n           ans[i][j][k] = 1; n         else if (j == cnt - 1) n           ans[i][j][k] = 2; n         else n           continue; n         G.push({i, j, k}); n       } n   while (!G.empty()) { n     T p = G.front(); n     G.pop(); n     int a = p.a, b = p.b, c = p.c; n     for (int i = 0; i < 5; i++) n       if (Q[a][i] > 0) n         for (int j = 0; j < 5; j++) n           if (Q[b][j] > 0) { n             int q = (c == 0 ? b : a), res = 0, l = 10000, f; n             for (int k = 0; k < 5; k++) res += l * Q[q][k], l /= 10; n             if (c == 0) { n               f = (j - i + 5) % 5; n               if (i == 0 || f == 0) continue; n               res -= H[j]; n               res += H[f]; n               if (ans[a][b][c] == 2) { n                 int& U = ans[a][SM[res]][!c]; n                 if (U == 0) G.push({a, SM[res], !c}); n                 U = 2; n               } else { n                 if (--FC[a][SM[res]][!c] == 0) { n                   ans[a][SM[res]][!c] = 1; n                   G.push({a, SM[res], !c}); n                 } n               } n             } else { n               f = (i - j + 5) % 5; n               if (j == 0 || f == 0) continue; n               res -= H[i]; n               res += H[f]; n               if (ans[a][b][c] == 1) { n                 int& U = ans[SM[res]][b][!c]; n                 if (U == 0) G.push({SM[res], b, !c}); n                 U = 1; n               } else { n                 if (--FC[SM[res]][b][!c] == 0) { n                   ans[SM[res]][b][!c] = 2; n                   G.push({SM[res], b, !c}); n                 } n               } n             } n           } n   } n   int t; n   scanf( %d , &t); n   while (t--) { n     int x, h; n     scanf( %d , &x); n     int W[5] = {}, g = 10000, a = 0, b = 0; n     for (int i = 0; i < 8; i++) scanf( %d , &h), W[h]++; n     for (int i = 0; i < 5; i++) a += g * W[i], g /= 10; n     g = 10000; n     int W1[5] = {}; n     for (int i = 0; i < 8; i++) scanf( %d , &h), W1[h]++; n     for (int i = 0; i < 5; i++) b += g * W1[i], g /= 10; n     int res = ans[SM[a]][SM[b]][x]; n     if (res == 0) n       cout <<  Deal n ; n     else if (res == 1) n       cout <<  Alice n ; n     else if (res == 2) n       cout <<  Bob n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n void solve() { n   scanf( %d , &n); n   int tmp, tot; n   if (n == 0) { n     printf( O-|-OOOO n ); n     return; n   } n   while (n) { n     tmp = n % 10; n     if (tmp >= 5) { n       tot = tmp - 5; n       printf( -O| ); n     } else { n       tot = tmp; n       printf( O-| ); n     } n     for (int i = 1; i <= tot; i++) printf( O ); n     printf( - ); n     for (int i = 1; i <= 4 - tot; i++) printf( O ); n     printf(  n ); n     n /= 10; n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void getint(int &v) { n   char ch, fu = 0; n   for (ch =  * ; (ch <  0  || ch >  9 ) && ch !=  - ; ch = getchar()) n     ; n   if (ch ==  - ) fu = 1, ch = getchar(); n   for (v = 0; ch >=  0  && ch <=  9 ; ch = getchar()) v = v * 10 + ch -  0 ; n   if (fu) v = -v; n } n int n, m, ans, a[444][444], f[444][444], p[401][160001]; n int main() { n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) getint(a[i][j]); n   for (int i = 1; i <= n; ++i) { n     for (int len = 1; len <= m; ++len) n       for (int l = 1; l <= m - len + 1; ++l) { n         int r = l + len - 1; n         if (len > 1) { n           f[l][r] = max(f[l][r], max(f[l + 1][r], f[l][r - 1])); n           f[l][r] = max(f[l][r], p[r][a[i][l]]); n           f[l][r] = max(f[l][r], p[l][a[i][r]]); n           if (a[i][l] == a[i][r]) f[l][r] = i; n         } else { n           f[l][r] = max(f[l][r], p[l][a[i][l]]); n         } n         ans = max(ans, (i - f[l][r]) * (r - l + 1)); n       } n     for (int j = 1; j <= m; ++j) p[j][a[i][j]] = i; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int b[m]; n   for (int i = 0; i < m; cin >> b[i++]) n     ; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (b[j] <= i + 1) { n         cout << b[j] <<    ; n         break; n       } n     } n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e3 + 10; n int n, m, c = 0, ans = 0; n vector<int> adj[maxn], bkadj[maxn], components, nodes[maxn], sink; n bool mark[maxn]; n int cmp[maxn], d[maxn], o[maxn]; n stack<int> st; n queue<int> q; n void input() { n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int fi, se; n     cin >> fi >> se; n     adj[fi].push_back(se); n     bkadj[se].push_back(fi); n   } n } n void dfs1(int x) { n   mark[x]++; n   for (int i = 0; i < adj[x].size(); i++) { n     int child = adj[x][i]; n     if (!mark[child]) dfs1(child); n   } n   st.push(x); n } n void dfs2(int x) { n   mark[x]++; n   nodes[cmp[x]].push_back(x); n   for (int i = 0; i < bkadj[x].size(); i++) { n     int child = bkadj[x][i]; n     if (!mark[child]) { n       cmp[child] = cmp[x]; n       dfs2(child); n     } else if (cmp[child] != cmp[x]) n       o[cmp[child]]++; n   } n } n int bfs(int x) { n   memset(mark, 0, sizeof mark); n   q.push(x); n   d[x] = 1; n   mark[x]++; n   int mi = 1e8; n   while (!q.empty()) { n     int cur = q.front(); n     q.pop(); n     for (int i = 0; i < adj[cur].size(); i++) { n       int child = adj[cur][i]; n       if (!mark[child]) { n         mark[child]++; n         d[child] = d[cur] + 1; n         q.push(child); n       } else if (child == x) n         mi = min(mi, d[cur]); n     } n   } n   return mi; n } n void task() { n   for (int i = 1; i <= n; i++) n     if (!mark[i]) dfs1(i); n   memset(mark, 0, sizeof mark); n   while (!st.empty()) { n     int x = st.top(); n     st.pop(); n     if (!mark[x]) cmp[x] = x, components.push_back(x), dfs2(x); n   } n   for (int i = 0; i < components.size(); i++) n     if (o[components[i]] == 0) sink.push_back(components[i]); n   for (int i = 0; i < sink.size(); i++) { n     int cm = sink[i]; n     if (nodes[cm].size() != 1) { n       c++; n       int mi = 1e8; n       for (int i = 0; i < nodes[cm].size(); i++) { n         int cur = nodes[cm][i]; n         mi = min(mi, bfs(cur)); n       } n       ans += mi; n     } n   } n } n void debug() { n   for (int i = 1; i <= n; i++) cerr << i <<  :  << cmp[i] << endl; n } n int main() { n   input(); n   task(); n   cout << n + (ans * 998) + c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[150000], b[510000], arr[226660]; n int main() { n   int n, x, k; n   cin >> n >> k >> x; n   int cnt = 1; n   int l = 0; n   cin >> arr[0]; n   for (int i = 1; i < n; i++) { n     cin >> arr[i]; n     if (arr[i] == arr[i - 1]) { n       cnt++; n     } else { n       a[l] = arr[i - 1]; n       b[l++] = cnt; n       cnt = 1; n     } n   } n   a[l] = arr[n - 1]; n   b[l++] = cnt; n   int sum = 0; n   for (int i = 0; i < l; i++) { n     int ans = 0; n     if (a[i] == x) { n       if (b[i] + 1 >= 3) { n         ans += b[i]; n         int u = i - 1; n         int v = i + 1; n         while (a[u] == a[v] && b[u] + b[v] >= 3 && u >= 0 && v < n) { n           ans += b[u] + b[v]; n           u--; n           v++; n         } n         sum = max(sum, ans); n       } n     } n   } n   printf( %d n , sum); n } n