#include <bits/stdc++.h> n using namespace std; n const int maxn = 12e4 + 5; n struct node { n   int minn, cnt, lazy, time; n   long long sum; n } tree[maxn << 2]; n int n, q; n int p[maxn], stk_min[maxn], stk_max[maxn], top_min, top_max; n long long ans[maxn]; n pair<pair<int, int>, int> querys[maxn]; n inline void add(int rt, int val) { n   tree[rt].minn += val; n   tree[rt].lazy += val; n } n inline void add_time(int rt, int val) { n   tree[rt].time += val; n   tree[rt].sum += 1ll * tree[rt].cnt * val; n } n void build(int rt, int l, int r) { n   tree[rt].minn = l, tree[rt].cnt = 1; n   if (l != r) { n     int mid = l + r >> 1; n     build(rt << 1, l, mid), build(rt << 1 | 1, mid + 1, r); n   } n } n inline void pushup(int rt) { n   int ls = rt << 1, rs = rt << 1 | 1; n   tree[rt].minn = min(tree[ls].minn, tree[rs].minn); n   tree[rt].sum = tree[ls].sum + tree[rs].sum; n   tree[rt].cnt = 0; n   if (tree[rt].minn == tree[ls].minn) tree[rt].cnt += tree[ls].cnt; n   if (tree[rt].minn == tree[rs].minn) tree[rt].cnt += tree[rs].cnt; n } n inline void pushdown(int rt) { n   int ls = rt << 1, rs = rt << 1 | 1; n   if (tree[rt].lazy) { n     add(ls, tree[rt].lazy); n     add(rs, tree[rt].lazy); n     tree[rt].lazy = 0; n   } n   if (tree[rt].time) { n     if (tree[rt].minn == tree[ls].minn) add_time(ls, tree[rt].time); n     if (tree[rt].minn == tree[rs].minn) add_time(rs, tree[rt].time); n     tree[rt].time = 0; n   } n } n void update(int rt, int l, int r, int ql, int qr, int val) { n   if (ql <= l && r <= qr) { n     add(rt, val); n   } else { n     int mid = l + r >> 1; n     pushdown(rt); n     if (qr <= mid) n       update(rt << 1, l, mid, ql, qr, val); n     else if (ql > mid) n       update(rt << 1 | 1, mid + 1, r, ql, qr, val); n     else { n       update(rt << 1, l, mid, ql, mid, val); n       update(rt << 1 | 1, mid + 1, r, mid + 1, qr, val); n     } n     pushup(rt); n   } n } n long long query(int rt, int l, int r, int ql, int qr) { n   if (ql <= l && r <= qr) { n     return tree[rt].sum; n   } else { n     int mid = l + r >> 1; n     pushdown(rt); n     if (qr <= mid) n       return query(rt << 1, l, mid, ql, qr); n     else if (ql > mid) n       return query(rt << 1 | 1, mid + 1, r, ql, qr); n     else n       return query(rt << 1, l, mid, ql, mid) + n              query(rt << 1 | 1, mid + 1, r, mid + 1, qr); n   } n } n int main() { n   ios::sync_with_stdio(false), cin.tie(nullptr); n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   cin >> q; n   for (int i = 1; i <= q; i++) { n     int l, r; n     cin >> l >> r; n     querys[i] = {{l, r}, i}; n   } n   sort(querys + 1, querys + q + 1, n        [](const pair<pair<int, int>, int> &a, n           const pair<pair<int, int>, int> &b) { n          return a.first.second < b.first.second; n        }); n   build(1, 1, n); n   for (int i = 1, j = 1; i <= n; i++) { n     update(1, 1, n, 1, n, -1); n     while (top_min && p[i] < p[stk_min[top_min]]) { n       update(1, 1, n, stk_min[top_min - 1] + 1, stk_min[top_min], n              p[stk_min[top_min]] - p[i]); n       --top_min; n     } n     while (top_max && p[i] > p[stk_max[top_max]]) { n       update(1, 1, n, stk_max[top_max - 1] + 1, stk_max[top_max], n              p[i] - p[stk_max[top_max]]); n       --top_max; n     } n     stk_min[++top_min] = i; n     stk_max[++top_max] = i; n     add_time(1, 1); n     while (j <= q && querys[j].first.second == i) { n       ans[querys[j].second] = n           query(1, 1, n, querys[j].first.first, querys[j].first.second); n       ++j; n     } n   } n   for (int i = 1; i <= q; i++) cout << ans[i] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1000000007; n const int MAXN = 305; n const int MAXT = 100005; n int a[MAXN], e[MAXN], d[MAXN]; n long long dp[MAXT]; n long long t, cnt; n void dfs(int u, int acc) { n   if (t < 0) { n     return; n   } n   ++cnt; n   a[u] += acc; n   if (e[u] != 0) { n     dfs(e[u], a[u]); n     t -= a[u]; n     if (t < 0) { n       return; n     } n   } n } n int main() { n   int n, q; n   int u, v; n   scanf( %d%d%d , &n, &q, &t); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , a + i); n   } n   while (q--) { n     scanf( %d%d , &u, &v); n     e[u] = v; n     d[v] = 1; n   } n   for (int i = 1; i <= n && t >= 0; ++i) { n     if (d[i] == 0) { n       dfs(i, 0); n     } n   } n   if (t < 0 || cnt < n) { n     printf( 0 n ); n     return 0; n   } n   dp[0] = 1; n   for (int i = 1; i <= n; ++i) { n     for (int j = a[i]; j <= t; ++j) { n       dp[j] = (dp[j] + dp[j - a[i]]) % MOD; n     } n   } n   printf( %d n , dp[t]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n queue<int> q[300005]; n set<int> all; n int main() { n   int n, quer; n   scanf( %d%d , &n, &quer); n   int t, x, v; n   int in = 1; n   for (int i = 1; i <= quer; i++) { n     scanf( %d%d , &t, &x); n     if (t == 1) { n       q[x].push(in); n       all.insert(in); n       in++; n     } else if (t == 2) { n       while (!q[x].empty()) { n         all.erase(q[x].front()); n         q[x].pop(); n       } n     } else { n       while (all.size() >= 1) { n         v = *all.begin(); n         if (v <= x) n           all.erase(v); n         else n           break; n       } n     } n     printf( %d n , all.size()); n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct SegmentTree { n  private: n   int n; n   vector<pair<int, int>> a; n   vector<pair<int, int>> tree; n  n  public: n   SegmentTree(vector<pair<int, int>> &a) { n     this->a = a; n     this->n = (int)a.size(); n     tree.resize(4 * n); n     build(1, 1, n); n   } n   void update(int l, int r, pair<int, int> x) { update(1, 1, n, l, r, x); } n   pair<int, int> query(int i) { return query(1, 1, n, i); } n  n  private: n   void build(int i, int l, int r) { n     if (l == r) n       tree[i] = a[l - 1]; n     else { n       int m = (l + r) / 2; n       build(2 * i, l, m); n       build(2 * i + 1, m + 1, r); n       tree[i] = make_pair(0, 0); n     } n   } n   void update(int i, int l, int r, int ul, int ur, pair<int, int> x) { n     if (ul <= l && r <= ur) n       tree[i] = x; n     else { n       if (tree[i] != make_pair(0, 0)) { n         tree[2 * i] = tree[2 * i + 1] = tree[i]; n       } n       int m = (l + r) / 2; n       if (ul <= m) update(2 * i, l, m, ul, ur, x); n       if (ur > m) update(2 * i + 1, m + 1, r, ul, ur, x); n       tree[i] = tree[2 * i] == tree[2 * i + 1] ? tree[2 * i] : make_pair(0, 0); n     } n   } n   pair<int, int> query(int i, int l, int r, int qi) { n     if (tree[i] != make_pair(0, 0)) return tree[i]; n     int m = (l + r) / 2; n     if (qi <= m) n       return query(2 * i, l, m, qi); n     else n       return query(2 * i + 1, m + 1, r, qi); n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   vector<int> a(2 * n); n   for (auto &ai : a) cin >> ai; n   vector<pair<int, int>> b(n); n   for (int i = 0; i < n; i++) b[i] = make_pair(i + n + 1, i + 1); n   SegmentTree tree(b); n   while (m--) { n     int t; n     cin >> t; n     if (t == 1) { n       int x, y, k; n       cin >> x >> y >> k; n       tree.update(y, y + k - 1, make_pair(x, y)); n     } else { n       int x; n       cin >> x; n       auto aux = tree.query(x); n       int diff = x - aux.second; n       int ans = a[aux.first + diff - 1]; n       cout << ans <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[101], s[101], ans, l, r; n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m; n   for (int i = 1; i < n + 1; i++) cin >> a[i]; n   for (int i = 1; i < n + 1; i++) s[i] = s[i - 1] + a[i]; n   ans = 0; n   for (int i = 0; i < m; i++) { n     cin >> l >> r; n     if (s[r] - s[l - 1] > 0) ans += s[r] - s[l - 1]; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5; n int n; n struct node { n   int op; n   long long l; n   long long r; n } q[N + 5]; n int cnt; n long long Hash[3 * N + 5]; n int len; n struct T { n   int sum; n   int lazy; n } tree[12 * N + 5]; n void pushup(int k) { tree[k].sum = tree[k << 1].sum + tree[k << 1 | 1].sum; } n void pushdown(int k, int l, int r) { n   if (tree[k].lazy == 0) return; n   int mid = (l + r) >> 1; n   if (tree[k].lazy == 1) { n     tree[k << 1].sum = mid - l + 1; n     tree[k << 1 | 1].sum = r - (mid + 1) + 1; n     tree[k << 1].lazy = tree[k << 1 | 1].lazy = tree[k].lazy; n   } else if (tree[k].lazy == 2) { n     tree[k << 1].sum = tree[k << 1 | 1].sum = 0; n     tree[k << 1].lazy = tree[k << 1 | 1].lazy = tree[k].lazy; n   } else { n     tree[k << 1].sum = mid - l + 1 - tree[k << 1].sum; n     tree[k << 1 | 1].sum = r - (mid + 1) + 1 - tree[k << 1 | 1].sum; n     if (tree[k << 1].lazy == 0) n       tree[k << 1].lazy = 3; n     else if (tree[k << 1].lazy == 1) n       tree[k << 1].lazy = 2; n     else if (tree[k << 1].lazy == 2) n       tree[k << 1].lazy = 1; n     else if (tree[k << 1].lazy == 3) n       tree[k << 1].lazy = 0; n     if (tree[k << 1 | 1].lazy == 0) n       tree[k << 1 | 1].lazy = 3; n     else if (tree[k << 1 | 1].lazy == 1) n       tree[k << 1 | 1].lazy = 2; n     else if (tree[k << 1 | 1].lazy == 2) n       tree[k << 1 | 1].lazy = 1; n     else if (tree[k << 1 | 1].lazy == 3) n       tree[k << 1 | 1].lazy = 0; n   } n   tree[k].lazy = 0; n } n void build(int k, int l, int r) { n   if (l == r) { n     tree[k].sum = tree[k].lazy = 0; n     return; n   } n   int mid = (l + r) >> 1; n   build(k << 1, l, mid); n   build(k << 1 | 1, mid + 1, r); n   pushup(k); n } n void update(int k, int l, int r, int flag, int x, int y) { n   if (l >= x && r <= y) { n     if (flag == 1) { n       tree[k].sum = r - l + 1; n       tree[k].lazy = 1; n       return; n     } else if (flag == 2) { n       tree[k].sum = 0; n       tree[k].lazy = 2; n       return; n     } else { n       tree[k].sum = (r - l + 1) - tree[k].sum; n       if (tree[k].lazy == 0) n         tree[k].lazy = 3; n       else if (tree[k].lazy == 1) n         tree[k].lazy = 2; n       else if (tree[k].lazy == 2) n         tree[k].lazy = 1; n       else if (tree[k].lazy == 3) n         tree[k].lazy = 0; n       return; n     } n   } n   int mid = (l + r) >> 1; n   pushdown(k, l, r); n   if (x <= mid) update(k << 1, l, mid, flag, x, y); n   if (mid < y) update(k << 1 | 1, mid + 1, r, flag, x, y); n   pushup(k); n } n int query(int k, int l, int r) { n   if (l == r) return l; n   int mid = (l + r) >> 1; n   pushdown(k, l, r); n   if (tree[k << 1].sum < mid - l + 1) return query(k << 1, l, mid); n   return query(k << 1 | 1, mid + 1, r); n } n int main() { n   scanf( %d , &n); n   Hash[++cnt] = 1; n   for (int i = 1; i <= n; i++) { n     scanf( %d%lld%lld , &q[i].op, &q[i].l, &q[i].r); n     Hash[++cnt] = q[i].l; n     Hash[++cnt] = q[i].r; n     Hash[++cnt] = q[i].r + 1; n   } n   sort(Hash + 1, Hash + cnt + 1); n   len = unique(Hash + 1, Hash + cnt + 1) - (Hash + 1); n   for (int i = 1; i <= n; i++) { n     q[i].l = lower_bound(Hash + 1, Hash + len + 1, q[i].l) - Hash; n     q[i].r = lower_bound(Hash + 1, Hash + len + 1, q[i].r) - Hash; n   } n   build(1, 1, len); n   for (int i = 1; i <= n; i++) { n     update(1, 1, len, q[i].op, q[i].l, q[i].r); n     printf( %lld n , Hash[query(1, 1, len)]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double pi = acos(-1.0), eps = 1e-10; n long double det(long double a11, long double a12, long double a21, n                 long double a22) { n   return a11 * a22 - a12 * a21; n } n struct pt { n   long double x, y; n   pt(long double x, long double y) : x(x), y(y) {} n }; n struct line { n   long double a, b, c; n   line(long double x1, long double y1, long double x2, long double y2) { n     a = y1 - y2; n     b = x2 - x1; n     c = a * x1 + b * y1; n   } n   line(long double a, long double b, long double c) : a(a), b(b), c(c) {} n   void transpose(pt p) { c += a * p.x + b * p.y; } n }; n struct circle { n   long double x, y, r; n   circle(long double x, long double y, long double r) : x(x), y(y), r(r) {} n }; n long double dist(long double x1, long double y1, long double x2, n                  long double y2) { n   long double dx = x1 - x2, dy = y1 - y2; n   return sqrt(dx * dx + dy * dy); n } n vector<pt> lineCircleIntersection(line l, pt center, long double r) { n   line t(l); n   t.transpose(pt(-center.x, -center.y)); n   long double a = t.a, b = t.b, c = -t.c; n   long double x0 = -a * c / (a * a + b * b), y0 = -b * c / (a * a + b * b); n   vector<pt> res; n   if (c * c > r * r * (a * a + b * b) + eps) { n   } else if (fabs(c * c - r * r * (a * a + b * b)) < eps) { n     res.push_back(pt(x0 + center.x, y0 + center.y)); n   } else { n     long double d = r * r - c * c / (a * a + b * b); n     long double mult = sqrt(d / (a * a + b * b)); n     long double ax, ay, bx, by; n     ax = x0 + b * mult; n     bx = x0 - b * mult; n     ay = y0 - a * mult; n     by = y0 + a * mult; n     res.push_back(pt(ax + center.x, ay + center.y)); n     res.push_back(pt(bx + center.x, by + center.y)); n   } n   return res; n } n vector<pt> circleCircleIntersection(circle c1, circle c2) { n   long double x = c1.x - c2.x, y = c1.y - c2.y; n   vector<pt> res = lineCircleIntersection( n       line(2 * x, 2 * y, x * x + y * y + c2.r * c2.r - c1.r * c1.r), pt(0, 0), n       c2.r); n   for (int i = 0; i < (int)res.size(); i++) { n     res[i].x += c2.x; n     res[i].y += c2.y; n   } n   return res; n } n bool inside(pt p, circle c) { return dist(p.x, p.y, c.x, c.y) <= c.r + eps; } n bool intersects(circle c1, circle c2, circle c3) { n   c1.r += eps; n   c2.r += eps; n   c3.r += eps; n   vector<pt> p[3]; n   p[0] = circleCircleIntersection(c1, c2); n   p[1] = circleCircleIntersection(c2, c3); n   p[2] = circleCircleIntersection(c3, c1); n   p[0].push_back(pt(c1.x, c1.y)); n   p[1].push_back(pt(c2.x, c2.y)); n   p[2].push_back(pt(c3.x, c3.y)); n   for (int i = 0; i < 3; i++) { n     for (size_t j = 0; j < p[i].size(); j++) { n       if (inside(p[i][j], c1) && inside(p[i][j], c2) && inside(p[i][j], c3)) { n         return true; n       } n     } n   } n   return false; n } n long double t1, t2, cx, cy, hx, hy, sx, sy; n void solve() { n   long double d1 = dist(hx, hy, sx, sy), d2 = dist(sx, sy, cx, cy), n               d3 = dist(cx, cy, hx, hy); n   long double l1 = d1 + d2 + t1, l2 = d3 + t2; n   cout << fixed << setprecision(10); n   if (l2 >= d1 + d2) { n     cout << min(l1, l2) << endl; n   } else { n     long double L = 0, R = min(l1, l2); n     while (fabs(L - R) > eps) { n       long double r1 = (L + R) / 2, r2 = max(0. * l1, l1 - d1 - r1), n                   r3 = max(0. * l2, l2 - r1); n       if (intersects(circle(cx, cy, r1), circle(sx, sy, r2), n                      circle(hx, hy, r3))) { n         L = r1; n       } else { n         R = r1; n       } n     } n     cout << L + eps << endl; n   } n } n int main() { n   while (cin >> t1 >> t2 >> cx >> cy >> hx >> hy >> sx >> sy) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long sum = 0, m1 = 0; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     int x; n     scanf( %d , &x); n     sum += x; n     m1 = max(m1, (long long)x); n   } n   if (m1 <= sum / 2 && sum % 2 == 0) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200004 * 4 + 11; n long long k[N], add[N]; n int a[200004]; n int n, m; n int x, y; n long long z; n long long ans; n int build(int l, int r, int o) { n   add[o] = 0; n   if (l == r) return k[o] = a[l]; n   int mid = ((l + r) >> 1); n   return k[o] = build(l, mid, (o << 1)) + build(mid + 1, r, ((o << 1) | 1)); n } n void pushdown(int l, int r, int o) { n   add[(o << 1)] += add[o]; n   add[((o << 1) | 1)] += add[o]; n   add[o] = 0; n } n void maintain(int l, int r, int o) { n   int mid = ((l + r) >> 1); n   if (r > l) n     k[o] = k[(o << 1)] + add[(o << 1)] * (mid - l + 1) + k[((o << 1) | 1)] + n            add[((o << 1) | 1)] * (r - mid); n   else n     k[o] = a[l]; n } n void update(int l, int r, int o) { n   if (x <= l && r <= y) { n     add[o] += z; n   } else { n     pushdown(l, r, o); n     int mid = ((l + r) >> 1); n     if (x <= mid) n       update(l, mid, (o << 1)); n     else n       maintain(l, mid, (o << 1)); n     if (y > mid) n       update(mid + 1, r, ((o << 1) | 1)); n     else n       maintain(mid + 1, r, ((o << 1) | 1)); n   } n   maintain(l, r, o); n } n void query(int l, int r, int o) { n   if (x <= l && r <= y) n     ans += k[o] + add[o] * (r - l + 1); n   else { n     pushdown(l, r, o); n     int mid = ((l + r) >> 1); n     if (x <= mid) query(l, mid, (o << 1)); n     if (y > mid) query(mid + 1, r, ((o << 1) | 1)); n   } n   maintain(l, r, o); n } n int main() { n   long long lst = 0.0; n   while (~scanf( %d , &n)) { n     int i, c, e, f; n     int d; n     int tot = 1; n     int tp = 0; n     build(1, 200004, 1); n     while (n--) { n       scanf( %d , &c); n       if (c == 1) { n         scanf( %d%d , &e, &d); n         z = d; n         x = 1; n         y = min(e, tot); n         update(1, 200004, 1); n         lst += e * d; n       } else if (c == 2) { n         scanf( %d , &d); n         tot++; n         x = y = tot; n         z = d; n         update(1, 200004, 1); n         lst += d; n       } else { n         ans = 0; n         x = y = tot; n         query(1, 200004, 1); n         z = -ans; n         update(1, 200004, 1); n         tot--; n         lst += z; n       } n       ans = 0; n       x = 1; n       y = 200004; n       query(1, n, 1); n       printf( %.9lf n , ans * 1.0 / tot); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, q; n   scanf( %d , &n); n   scanf( %d , &q); n   int i; n   long ans[q + 1]; n   long val; n   std::set<long> valores; n   for (i = 0; i < n; i++) { n     scanf( %ld , &val); n     valores.insert(val); n   } n   std::set<long>::iterator ite; n   std::multiset<long> dife; n   for (ite = valores.begin(); ite != --valores.end(); ite++) { n     dife.insert(*valores.upper_bound(*ite) - *ite); n   } n   int acc; n   std::set<long>::iterator low, up; n   for (i = 0; i < q; i++) { n     if (valores.size() > 1) n       ans[i] = *valores.rbegin() - *valores.begin() - *dife.rbegin(); n     else n       ans[i] = 0; n     scanf( %d , &acc); n     scanf( %ld , &val); n     if (acc == 0) { n       up = valores.upper_bound(val); n       low = valores.lower_bound(val); n       if (up != valores.end()) { n         dife.erase(dife.lower_bound(*up - val)); n       } n       if (low != valores.begin()) { n         low--; n         dife.erase(dife.lower_bound(val - *low)); n         low++; n       } n       if ((up != valores.end()) && (low != valores.begin())) { n         low--; n         dife.insert(*up - *low); n       } n       valores.erase(val); n     } else { n       up = valores.upper_bound(val); n       low = valores.lower_bound(val); n       if (up != valores.end()) { n         dife.insert(*up - val); n       } n       if (low != valores.begin()) { n         low--; n         dife.insert(val - *low); n         low++; n       } n       if ((up != valores.end()) && (low != valores.begin())) { n         low--; n         dife.erase(dife.lower_bound(*up - *low)); n       } n       valores.insert(val); n     } n   } n   if (valores.size() > 1) n     ans[q] = *valores.rbegin() - *valores.begin() - *dife.rbegin(); n   else n     ans[q] = 0; n   for (i = 0; i < q + 1; i++) { n     printf( %d  n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n int n, m; n struct Query { n   int l, r, x; n   Query() {} n   Query(int ll, int rr, int xx) { l = ll, r = rr, x = xx; } n }; n vector<Query> q[N]; n int lson[N], rson[N]; n void init() { n   int cnt = 0, p = 1; n   for (int i = 1; i < N; ++i) { n     if ((1 << cnt) == i) { n       cnt++; n       lson[i] = p++; n     } else { n       lson[i] = -1; n     } n     rson[i] = p++; n   } n } n bool ok(int x1, int y1, int x2, int y2) { n   if (x1 > y2 || x2 > y1) return false; n   return true; n } n void solve(int k, int x) { n   set<int> s; n   int l = x, r = x; n   for (int i = k; i < n + 1; ++i) { n     for (int j = 0; j < (int)q[i].size(); ++j) { n       if (ok(l, r, q[i][j].l, q[i][j].r)) { n         s.insert(q[i][j].x); n       } n     } n     if (lson[l] != -1) n       l = lson[l]; n     else n       l = rson[l]; n     r = rson[r]; n   } n   printf( %d n , (int)s.size()); n } n int main() { n   init(); n   int op, k, l, r, x; n   scanf( %d%d , &n, &m); n   for (int i = 1; i < m + 1; ++i) { n     scanf( %d , &op); n     if (op == 1) { n       scanf( %d%d%d%d , &k, &l, &r, &x); n       q[k].push_back(Query(l, r, x)); n     } else { n       scanf( %d%d , &k, &x); n       solve(k, x); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   scanf( %d , &q); n   while (q--) { n     int n; n     scanf( %d , &n); n     int sum = 0, a; n     for (int i = 0; i < n; ++i) { n       scanf( %d , &a); n       sum += a; n     } n     printf( %d n , (int)ceil(sum * 1.0 / n)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using std::map; n using std::max; n using std::min; n using std::set; n using std::string; n using std::vector; n constexpr long long magic_mod = 1000000007; n constexpr long long big = 1ll << 60; n struct Matrix { n   long long size; n   vector<long long> f; n   Matrix(long long size, long long start_value = big) n       : size(size), f(size * size, start_value) {} n   Matrix(const Matrix& o) { *this = o; }; n   static vector<Matrix> cache; n   inline long long& m(long long a, long long b) { return f[a * size + b]; } n   inline const long long& m(long long a, long long b) const { n     return f[a * size + b]; n   } n   void print() { n     for (long long i = 0; i < size; ++i) { n       for (long long j = 0; j < size; ++j) { n         std::cout << m(j, i) <<    ; n       } n       std::cout << std::endl; n     } n   } n   void operator=(const Matrix& o) { n     this->size = o.size; n     this->f = o.f; n   } n   Matrix operator*(const Matrix& o) const { n     Matrix res(size); n     for (long long i = 0; i < size; ++i) { n       for (long long j = 0; j < size; ++j) { n         for (long long k = 0; k < size; ++k) { n           res.m(i, j) = min(res.m(i, j), m(k, j) + o.m(i, k)); n         } n       } n     } n     return res; n   } n   Matrix get_pow(long long p) { n     Matrix res(*this); n     bool inited = false; n     for (long long i = 1;; ++i) { n       if (!p) { n         break; n       } n       if (p % 2) { n         if (!inited) { n           res = cache[i]; n           inited = true; n         } else { n           res = res * cache[i]; n         } n       } n       p /= 2; n     } n     return res; n   } n   void fill_cache(); n }; n const long long xxxx = 40; n vector<Matrix> Matrix::cache; n void Matrix::fill_cache() { n   cache = vector<Matrix>(xxxx, Matrix(size)); n   Matrix res(*this); n   for (long long i = 1; i < xxxx; ++i) { n     cache[i] = res; n     res = res * res; n   } n } n vector<long long> standings; n vector<long long> jw; n long long sn; n map<long long, long long> magic_stones; n inline long long btc(long long x) { n   long long res = 0; n   while (x) { n     if (x % 2) ++res; n     x /= 2; n   } n   return res; n } n long long get_index(long long standing) { n   for (long long i = 0; i < standings.size(); ++i) { n     if (standings[i] == standing) return i; n   } n   std::cerr <<  WTF, INVALID STANDING   << standing << std::endl; n   return -1; n } n void CalcJmpMatrix(Matrix& jumpm, long long pos = -1) { n   for (long long i = 0; i < standings.size(); ++i) { n     long long ss = standings[i]; n     long long smallest = 1; n     while ((ss & smallest) == 0) smallest *= 2; n     for (long long j = 0; j < jw.size(); ++j) { n       long long ns = ss; n       ns &= ~smallest; n       if (ns & (smallest << j)) continue; n       ns |= smallest << j; n       if ((ns & 1) == 0 && ns < (1 << jw.size())) { n         auto& x = jumpm.m(i, get_index(ns >> 1)); n         long long aw = 0; n         if (pos >= 0 && j != 0) { n           long long pos2 = pos; n           long long sm2 = smallest >> 1; n           while (sm2) { n             ++pos2; n             sm2 /= 2; n           } n           pos2 += j; n           if (magic_stones.find(pos2) != magic_stones.end()) { n             aw = magic_stones[pos2]; n           } n         } n         x = min(x, (long long)jw[j] + aw); n       } n     } n   } n } n vector<long long> mul(const vector<long long>& x, const Matrix& m) { n   vector<long long> res(sn, big); n   for (long long i = 0; i < sn; ++i) { n     for (long long j = 0; j < sn; ++j) { n       res[i] = min(res[i], x[j] + m.m(j, i)); n     } n   } n   return res; n } n void print_state(long long pos, vector<long long>& res) {} n int main() { n   long long x, k, n, q; n   scanf( %lld%lld%lld%lld , &x, &k, &n, &q); n   jw = vector<long long>(k + 1, 0); n   for (long long i = 1; i <= k; ++i) scanf( %lld , &(jw[i])); n   for (long long i = 0; i < q; ++i) { n     long long p, pw; n     scanf( %lld%lld , &p, &pw); n     magic_stones[p - 1] = pw; n   } n   for (long long i = 0; i < (1 << k); ++i) { n     if (btc(i) == x) { n       standings.push_back(i); n     } n   } n   sn = standings.size(); n   Matrix jumpm(sn); n   CalcJmpMatrix(jumpm); n   jumpm.fill_cache(); n   vector<long long> res(sn, big); n   res[0] = 0; n   long long cp = 0; n   long long des_pos = n - x; n   print_state(cp, res); n   for (auto ms : magic_stones) { n     if (ms.first == cp) continue; n     if (ms.first - k > cp) { n       res = mul(res, jumpm.get_pow(ms.first - k - cp)); n       cp = ms.first - k; n       print_state(cp, res); n     } n     for (; cp < ms.first && cp < des_pos;) { n       Matrix msm(sn); n       CalcJmpMatrix(msm, cp); n       res = mul(res, msm); n       ++cp; n       print_state(cp, res); n     } n   } n   if (cp < des_pos) { n     res = mul(res, jumpm.get_pow(des_pos - cp)); n     cp = des_pos; n     print_state(cp, res); n   } n   print_state(cp, res); n   std::cout << res[0] << std::endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 20; n const int INF = 1e9; n struct Edge { n   int to, next, w; n } e[2 * N]; n int n, head[N], M, num; n map<int, int> mp, sp; n long long ans; n void add(int u, int v, int w) { n   e[num].next = head[u]; n   e[num].to = v, e[num].w = w; n   head[u] = num++; n } n bool ban[N]; n int sz[N], f[N]; n int root, siz; n void get_root(int u, int fa) { n   sz[u] = 1, f[u] = 0; n   for (int i = head[u]; i != -1; i = e[i].next) { n     int v = e[i].to; n     if (v == fa || ban[v]) continue; n     get_root(v, u); n     sz[u] += sz[v]; n     f[u] = max(sz[v], f[u]); n   } n   f[u] = max(f[u], siz - sz[u]); n   if (f[u] < f[root]) root = u; n } n int dis[N], seq[N], tol, pos[N]; n long long inv[N], path[N], ten[N], gao[N]; n void dfs(int u, int fa) { n   pos[u] = tol; n   seq[tol++] = u; n   sz[u] = 1; n   for (int i = head[u]; i != -1; i = e[i].next) { n     int v = e[i].to; n     if (v == fa || ban[v]) continue; n     dis[v] = dis[u] + 1; n     path[v] = ((1LL * path[u]) * 10 + e[i].w) % M; n     gao[v] = (gao[u] + ten[dis[v] - 1] * e[i].w) % M; n     if (gao[v] == 0) ans++; n     if (path[v] == 0) ans++; n     dfs(v, u); n     sz[u] += sz[v]; n   } n } n void sovle(int u, int ssz) { n   root = 0, siz = ssz, f[0] = INF; n   get_root(u, 0); n   int rt = root; n   dis[rt] = 0, path[rt] = 0, tol = 0; n   gao[rt] = 0; n   long long pre = ans; n   dfs(rt, 0); n   mp.clear(), sp.clear(); n   for (int i = head[rt]; i != -1; i = e[i].next) { n     int v = e[i].to; n     if (ban[v]) continue; n     for (int j = pos[v]; j < pos[v] + sz[v]; j++) { n       int u = seq[j]; n       int tmp = ((M - path[u]) * inv[dis[u]]) % M; n       ans += mp[tmp] + sp[gao[u]]; n     } n     for (int j = pos[v]; j < pos[v] + sz[v]; j++) { n       int u = seq[j]; n       int tmp = ((M - path[u]) * inv[dis[u]]) % M; n       mp[gao[u]]++, sp[tmp]++; n     } n   } n   ban[rt] = true; n   for (int i = head[rt]; i != -1; i = e[i].next) { n     int v = e[i].to; n     if (ban[v]) continue; n     sovle(v, sz[v]); n   } n } n long long extend_gcd(long long a, long long b, long long &x, long long &y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return a; n   } else { n     long long gcd = extend_gcd(b, a % b, x, y); n     long long t = x; n     x = y; n     y = t - a / b * x; n     return gcd; n   } n } n void init() { n   memset(head, -1, sizeof(head)); n   memset(ban, 0, sizeof(ban)); n   num = 0; n   inv[0] = 1, ten[0] = 1; n   long long x, y; n   extend_gcd(10, M, x, y); n   x = (x % M + M) % M; n   for (int i = 1; i <= n; i++) { n     inv[i] = (inv[i - 1] * x) % M; n     ten[i] = (ten[i - 1] * 10) % M; n   } n } n int main() { n   while (scanf( %d%d , &n, &M) != EOF) { n     init(); n     for (int i = 0; i < n - 1; i++) { n       int u, v, w; n       scanf( %d%d%d , &u, &v, &w); n       u++, v++; n       add(u, v, w); n       add(v, u, w); n     } n     ans = 0; n     sovle(1, n); n     printf( %I64d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   long long r; n   while (b) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n long long power(long long x, long long y) { n   if (y == 0) n     return 1; n   else if (y % 2 == 0) n     return power(x, y / 2) * power(x, y / 2); n   else n     return x * power(x, y / 2) * power(x, y / 2); n } n long long fpow(long long n, long long k, long long p = 1000000007) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n long long inv(long long a, long long p = 1000000007) { n   return fpow(a, p - 2, p); n } n bool pr[1000007]; n void sieve() { n   pr[0] = 1; n   pr[1] = 1; n   for (int i = 2; i < sqrt(1000007); i++) { n     for (int j = 2 * i; j <= 1000007; j += i) { n       pr[j] = 1; n     } n   } n } n long long differ(long long a, long long b) { n   a = a - 65; n   b = b - 65; n   long long d = min(abs(a - b), min(a, b) + (25 - max(a, b)) + 1); n   return d; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t, a, b, c; n   string q, r, s; n   vector<long long> m, p; n   cin >> t; n   char d; n   int arr[4]; n   arr[0] = (int)( A ); n   arr[1] = (int)( C ); n   arr[2] = (int)( T ); n   arr[3] = (int)( G ); n   vector<char> v; n   a = 1000000007; n   long long n = t; n   while (n--) { n     cin >> d; n     v.push_back(d); n   } n   for (long long i = 0; (i + 3) < t; i++) { n     b = 0; n     for (int j = int(0); j <= int(3); j++) { n       b += differ((int)(arr[j]), (int)v[i + j]); n     } n     a = min(a, b); n   } n   cout << a; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout << fixed; n   int a, b, c, d, e, f; n   cin >> a >> b >> c >> d >> e >> f; n   int cost = 0; n   if (e > f) { n     int suits = min(a, d); n     cost += e * suits; n     a -= suits; n     d -= suits; n     if (d != 0) { n       suits = min({b, c, d}); n       cost += f * suits; n     } n   } else { n     int suits = min({b, c, d}); n     cost += f * suits; n     b -= suits; n     c -= suits; n     d -= suits; n     if (d != 0) { n       suits = min(a, d); n       cost += e * suits; n     } n   } n   cout << cost <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n char a; n int r, c; n bool side(int x) { n   if (x == 1 or x == 8) return true; n   return false; n } n int type(int x, int y) { n   if (side(x) and side(y)) return 1; n   if (side(x) or side(y)) return 2; n   return 3; n } n int main() { n   scanf(  %c%d , &a, &r); n   c = a -  a  + 1; n   int ty = type(r, c); n   if (ty == 1) n     printf( 3 ); n   else if (ty == 2) n     printf( 5 ); n   else n     printf( 8 ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:251700000 ) n using namespace std; n const int INF = 2e9; n const double eps = 1e-6; n const int M = 1e5 + 5; n int n; n string s[M]; n bool us[M]; n char SS[M]; n string get_s() { n   scanf(  %s , &SS); n   return string(SS); n } n int main() { n   scanf( %d , &n); n   vector<int> c(27); n   vector<int> dd(27, -1); n   for (int q = 0; q < n; ++q) { n     s[q] = get_s(); n     for (int i = 0; i < 27; ++i) c[i] = 0; n     for (int j = 0; j < s[q].size(); ++j) ++c[s[q][j] -  a ]; n     for (int i = 0; i < 27; ++i) n       if (c[i] > 1) cout <<  NO , exit(0); n     for (int i = 0; i < 27; ++i) { n       if (dd[i] == -1 || !c[i] || us[dd[i]]) continue; n       int l, r, l1, r1; n       for (int j = 0; j < s[q].size(); ++j) n         if (s[q][j] -  a  == i) l = j; n       for (int j = 0; j < s[dd[i]].size(); ++j) n         if (s[dd[i]][j] -  a  == i) r = j; n       l1 = l, r1 = r; n       while (l >= 0 && r >= 0) { n         if (s[q][l] != s[dd[i]][r]) cout <<  NO , exit(0); n         --l, --r; n       } n       if (r >= 0) s[q] = s[dd[i]].substr(0, r + 1) + s[q], l1 += r + 1; n       l = l1, r = r1; n       while (l < s[q].size() && r < s[dd[i]].size()) { n         if (s[q][l] != s[dd[i]][r]) cout <<  NO , exit(0); n         ++l, ++r; n       } n       while (r < s[dd[i]].size()) { n         s[q] += s[dd[i]][r]; n         ++r; n       } n       us[dd[i]] = 1; n     } n     for (int i = 0; i < 27; ++i) c[i] = 0; n     for (int j = 0; j < s[q].size(); ++j) n       ++c[s[q][j] -  a ], dd[s[q][j] -  a ] = q; n     for (int i = 0; i < 27; ++i) n       if (c[i] > 1) cout <<  NO , exit(0); n   } n   vector<string> ab; n   for (int i = 0; i < n; ++i) n     if (!us[i]) ab.push_back(s[i]); n   sort((ab).begin(), (ab).end()); n   for (int i = 0; i < ab.size(); ++i) cout << ab[i]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int c, d, n, m, k, needed; n   cin >> c >> d >> n >> m >> k; n   needed = n * m - k; n   cout << ((needed <= 0) n                ? 0 n                : min(needed * d, min(needed / n * c + needed % n * d, n                                      (int)(ceil((double)needed / n) * c)))); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long MOD = 1e9 + 7; n const int N = 1e5 + 5; n int n; n long long x, y, ans; n bool comp[N]; n vector<long long> primes; n long long add(long long a, long long b) { return (a + b) % MOD; } n long long mul(long long a, long long b) { return (a * b) % MOD; } n long long fastexp(long long a, long long b) { n   if (b == 0) return 1; n   long long x = fastexp(a, b >> 1); n   x = mul(x, x); n   if (b & 1) x = mul(x, a); n   return x; n } n int main() { n   scanf( %lld%lld , &x, &y); n   if (y % x != 0) { n     printf( 0 n ); n     return 0; n   } n   y /= x; n   comp[1] = 1; n   for (int i = 2; i * i <= N; i++) { n     if (comp[i]) continue; n     for (int j = i * i; j < N; j += i) { n       comp[j] = i; n     } n   } n   long long z = y; n   for (int i = 2; i < N; i++) { n     if (comp[i]) continue; n     if (z % i) continue; n     while (z % i == 0) { n       z /= i; n     } n     primes.push_back(i); n   } n   if (z > 1) primes.push_back(z); n   z = primes.size(); n   for (int i = 0; i < (1 << z); i++) { n     long long a = 1, b = 0; n     for (int j = 0; j < z; j++) { n       if ((i >> j) & 1) { n         b++; n         a *= primes[j]; n       } n     } n     a = y / a; n     if (b & 1) { n       ans = add(ans, MOD - fastexp(2, a - 1)); n     } else { n       ans = add(ans, fastexp(2, a - 1)); n     } n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 300010; n pair<pair<int, int>, pair<int, int> > allNums[MAXN * 2]; n set<pair<int, int> > startTimes; n pair<int, int> v[MAXN]; n int main() { n   int N; n   scanf( %d , &N); n   for (int i = 0; i < N; ++i) { n     scanf( %d%d , &v[i].first, &v[i].second); n     int L = v[i].second - v[i].first; n     allNums[2 * i] = make_pair(make_pair(v[i].first, 0), make_pair(L, i)); n     allNums[2 * i + 1] = make_pair(make_pair(v[i].second, 1), make_pair(L, i)); n   } n   sort(allNums, allNums + 2 * N); n   for (int i = 0; i < 2 * N; ++i) { n     if (allNums[i].first.second == 0) { n       startTimes.insert( n           make_pair(allNums[i].first.first, allNums[i].second.second)); n       continue; n     } n     int idx = allNums[i].second.second; n     int st = v[idx].first; n     set<pair<int, int> >::iterator it = startTimes.find(make_pair(st, idx)); n     set<pair<int, int> >::iterator snd = startTimes.begin(); n     if (it == snd) ++snd; n     if (snd != startTimes.end() && snd->first <= it->first) { n       printf( %d %d n , idx + 1, snd->second + 1); n       return 0; n     } n     startTimes.erase(it); n   } n   puts( -1 -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = 5e5; n const int Inf = 1 << 30; n int mpx[MaxN + 5]; n vector<int> s, t[MaxN + 5]; n bool cmp(int x, int y) { return x > y; } n int main() { n   int n, m, x, u, v, ans, S, p; n   while (~scanf( %d %d , &n, &m)) { n     ans = 0; n     for (int i = 1; i <= n; i++) { n       scanf( %d , &x); n       mpx[x] = i; n       t[i].clear(); n     } n     for (int i = 1; i <= m; i++) { n       scanf( %d %d , &u, &v); n       t[mpx[u]].push_back(mpx[v]); n     } n     for (int i = 1; i <= n; i++) sort(t[i].begin(), t[i].end(), cmp); n     s.clear(); n     s.push_back(n); n     p = 1; n     for (int i = n - 1; i >= 1; i--) { n       x = 0; n       S = t[i].size(); n       for (int j = 0; j < S; j++) { n         if (s[x] == t[i][j]) { n           if (++x == p) { n             ans++; n             break; n           } n         } else if (s[x] > t[i][j]) n           break; n       } n       if (x != p) { n         p++; n         s.push_back(i); n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n int N, K; n int use[8]; n int per[8]; n int level[8]; n int A; n double res; n void dfs(int depth, int st) { n   int i, j, sum, all, dame; n   double p, ok, tmp; n   if (depth == K) { n     tmp = 0; n     for (i = 0; i < 1 << N; i++) { n       p = 1; n       sum = all = dame = 0; n       for (j = 0; j < N; j++) { n         all += 1; n         if (i & 1 << j) n           sum += 1; n         else n           dame += level[j]; n         ok = (per[j] + use[j] * 10) / 100.0; n         if (ok > 1) ok = 1; n         if (i & 1 << j) n           p *= ok; n         else n           p *= 1 - ok; n       } n       if (all < 2 * sum) n         tmp += p; n       else n         tmp += p * A / (A + dame); n     } n     if (res < tmp) res = tmp; n     return; n   } n   for (i = st; i < N; i++) { n     use[i]++; n     dfs(depth + 1, i); n     use[i]--; n   } n } n int main() { n   int i, j, l, m; n   int sum; n   while (scanf( %d%d%d , &N, &K, &A) == 3) { n     for (i = 0; i < N; i++) scanf( %d%d , level + i, per + i); n     res = 0; n     dfs(0, 0); n     printf( %.10lf n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void jizz() { n   cout << -1 << endl; n   exit(0); n } n int s[200005], l[200005], r[200005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) { n     cin >> s[i] >> l[i]; n     r[i] = s[i] + l[i]; n     l[i] = s[i]; n   } n   int nl = -9, nr = 3000006; n   for (int i = 1; i <= n; ++i) { n     --nl, ++nr; n     nl = l[i] = max(l[i], nl); n     nr = r[i] = min(r[i], nr); n     if (l[i] > r[i]) jizz(); n   } n   nl = -9, nr = 3000006; n   for (int i = n; i >= 1; --i) { n     --nl, ++nr; n     nl = l[i] = max(l[i], nl); n     nr = r[i] = min(r[i], nr); n     if (l[i] > r[i]) jizz(); n   } n   long long tot = 0; n   for (int i = 1; i <= n; ++i) { n     tot += r[i] - s[i]; n   } n   cout << tot << endl; n   for (int i = 1; i <= n; ++i) cout << r[i] <<    ; n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n template <class T, class U> n void mod_partial_sum(const T& v, U* out, ll mod) { n   auto it = begin(v); n   ll val = 0; n   for (auto& x : *out) x = (val += *(it++)) %= mod; n } n int main() { n   cout << fixed << setprecision(10); n   int a, b; n   int rest; n   cin >> a >> b; n   if (a > b) { n     cout << b; n     rest = a - b; n   } else { n     cout << a; n     rest = b - a; n   } n   cout <<    ; n   cout << rest / 2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 110; n int a, b, c, ar[maxn], al[maxn]; n bool gg = false; n int main() { n   cin >> a >> b >> c; n   for (int i = 0; i < b; i++) cin >> ar[i]; n   for (int j = 0; j < c; j++) cin >> al[j]; n   for (int i = 1; i <= a; i++) { n     for (int j = 0; j < b; j++) { n       if (ar[j] == i) gg = true; n     } n     gg ? cout << 1 <<     : cout << 2 <<    ; n     gg = false; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   long double a, b, c, p, q, r; n   cin >> a >> b >> c; n   p = b * b - 4 * a * c; n   if (a == 0 && b == 0 && c == 0) n     cout << -1 << endl; n   else if (a == 0 && b == 0 && c != 0) n     cout << 0 << endl; n   else if (p < 0) n     cout << 0 << endl; n   else if (a == 0) { n     q = -c / b; n     cout << 1 << endl; n     cout << fixed << setprecision(6) << q; n   } else if (p == 0) { n     q = -b / (2 * a); n     cout << 1 << endl; n     cout << fixed << setprecision(6) << q; n   } else { n     q = (-b - sqrt(p)) / (2 * a); n     r = (-b + sqrt(p)) / (2 * a); n     if (q < r) { n       cout << 2 <<   n  << fixed << setprecision(6) << q << endl; n       cout << fixed << setprecision(6) << r << endl; n     } else { n       cout << 2 <<   n  << fixed << setprecision(6) << r << endl; n       cout << fixed << setprecision(6) << q << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   long long int t, i = 0; n   long long ans = 0; n   scanf( %lld , &t); n   for (i = 0; i < t; i++) { n     long long int n; n     scanf( %lld , &n); n     ans = ans + i * (n - 1) + n; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, k, ans = 0; n   cin >> x >> k; n   while (x--) { n     int a, b; n     cin >> a >> b; n     ans += abs(a - b) + 1; n   } n   int ans2 = 0; n   while (true) { n     if (ans % k == 0) { n       cout << ans2; n       break; n     } else { n       ans2++; n       ans++; n     } n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize  03  n using namespace std; n const long long int inf = 1e15; n const long long int MOD = 1e9 + 7; n const long long int N = 2e5 + 5; n struct SegTree { n   struct data { n     long long int val; n     data() : val(0){}; n     template <typename T> n     void apply(T v) { n       val = v; n     } n   }; n   long long int N; n   vector<data> st; n   SegTree() {} n   SegTree(long long int _N) : N(_N) { st.resize(4 * N + 5); } n   inline void merge(data &cur, const data &l, const data &r, n                     long long int flag) { n     if (flag == 1) n       cur.val = l.val | r.val; n     else n       cur.val = l.val ^ r.val; n   } n   template <typename T> n   long long int build(long long int node, long long int l, long long int r, n                       vector<T> &v) { n     if (l == r) { n       if (l < (long long int)v.size()) n         st[node].apply(v[l]); n       else n         st[node].apply(0); n       return 1; n     } n     long long int mid = (l + r) / 2; n     long long int f = build(node * 2, l, mid, v); n     build(node * 2 + 1, mid + 1, r, v); n     merge(st[node], st[node * 2], st[node * 2 + 1], f); n     return !f; n   } n   long long int pupdate(long long int node, long long int l, long long int r, n                         long long int pos, long long int val) { n     if (l == r) { n       st[node].val = val; n       return 1; n     } n     long long int mid = (l + r) / 2; n     long long int f; n     if (pos <= mid) n       f = pupdate(node * 2, l, mid, pos, val); n     else n       f = pupdate(node * 2 + 1, mid + 1, r, pos, val); n     merge(st[node], st[node * 2], st[node * 2 + 1], f); n     return !f; n   } n   template <typename T> n   void build(vector<T> &v) { n     build(1, 1, N, v); n   } n   void update(long long int pos, long long int val) { n     pupdate(1, 1, N, pos, val); n   } n }; n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long int n, q; n   cin >> n >> q; n   vector<long long int> v((1 << n) + 1); n   for (long long int i = 1; i <= (1 << n); i++) { n     cin >> v[i]; n   } n   SegTree st(1 << 17); n   st.build(v); n   ; n   while (q--) { n     long long int a, b; n     cin >> a >> b; n     st.update(a, b); n     cout << st.st[1].val <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int a[2][200005]; n int dp[2][200005][19]; n void gao(int c) { n   for (int i = 1; i <= n; ++i) dp[c][i][0] = a[c][i]; n   for (int j = 1; (1 << j) <= n; ++j) { n     for (int i = 1; i + (1 << (j - 1)) <= n; ++i) { n       if (c == 0) n         dp[c][i][j] = max(dp[c][i][j - 1], dp[c][i + (1 << (j - 1))][j - 1]); n       if (c == 1) n         dp[c][i][j] = min(dp[c][i][j - 1], dp[c][i + (1 << (j - 1))][j - 1]); n     } n   } n } n int rmq(int l, int r, int c) { n   int k = (int)(log(r - l + 1) / log(2)); n   if (c == 0) return max(dp[c][l][k], dp[c][r - (1 << k) + 1][k]); n   return min(dp[c][l][k], dp[c][r - (1 << k) + 1][k]); n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[0][i]); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[1][i]); n   gao(0), gao(1); n   long long ans = 0; n   for (int i = 1; i <= n; ++i) { n     int l = i, r = n, x, y; n     while (l < r) { n       int m = (l + r) >> 1; n       if (rmq(i, m, 0) < rmq(i, m, 1)) n         l = m + 1; n       else n         r = m; n     } n     x = l; n     l = i, r = n; n     while (l < r) { n       int m = (l + r + 1) >> 1; n       if (rmq(i, m, 0) > rmq(i, m, 1)) n         r = m - 1; n       else n         l = m; n     } n     y = l; n     if (x == y) n       ans += rmq(i, x, 0) == rmq(i, y, 1) ? 1 : 0; n     else n       ans += max(0, y - x + 1); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int n, m, k; n char s[2505][2505]; n long long a[2505][2505]; n inline long long sum(int r1, int c1, int r2, int c2) { n   return a[r2][c2] - a[r1 - 1][c2] - a[r2][c1 - 1] + a[r1 - 1][c1 - 1]; n } n inline long long go(int r1, int c1, int r2, int c2) { n   if (r1 == r2 && c1 == c2) return sum(r1, c1, r2, c2) == k; n   if (r2 - r1 > c2 - c1) { n     long long cnt = 0; n     int m = r1 + r2 >> 1; n     int up[k + 1], dw[k + 1]; n     for (int i = c1; i <= c2; i++) { n       for (int x = 0; x <= k; x++) up[x] = m - r1 + 1, dw[x] = r2 - m; n       for (int j = i; j <= c2; j++) { n         for (int x = 0; x <= k; x++) { n           while (up[x] && sum(m - up[x] + 1, i, m, j) > x) up[x]--; n           while (dw[x] && sum(m + 1, i, m + dw[x], j) > x) dw[x]--; n         } n         for (int x = 0; x <= k; x++) { n           cnt += (long long)(up[x] - (x ? up[x - 1] : 0)) * n                  (dw[k - x] - (k - x ? dw[k - x - 1] : 0)); n         } n       } n     } n     return cnt + go(r1, c1, m, c2) + go(m + 1, c1, r2, c2); n   } else { n     long long cnt = 0; n     int m = c1 + c2 >> 1; n     int up[k + 1], dw[k + 1]; n     for (int i = r1; i <= r2; i++) { n       for (int x = 0; x <= k; x++) up[x] = m - c1 + 1, dw[x] = c2 - m; n       for (int j = i; j <= r2; j++) { n         for (int x = 0; x <= k; x++) { n           while (up[x] && sum(i, m - up[x] + 1, j, m) > x) up[x]--; n           while (dw[x] && sum(i, m + 1, j, m + dw[x]) > x) dw[x]--; n         } n         for (int x = 0; x <= k; x++) { n           cnt += (long long)(up[x] - (x ? up[x - 1] : 0)) * n                  (dw[k - x] - (k - x ? dw[k - x - 1] : 0)); n         } n       } n     } n     return cnt + go(r1, c1, r2, m) + go(r1, m + 1, r2, c2); n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = 1; i <= n; i++) scanf( %s , &s[i][1]); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1] + s[i][j] -  0 ; n     } n   } n   printf( %I64d n , go(1, 1, n, m)); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, l; n   cin >> n >> l; n   deque<int> v; n   while (n--) { n     int m; n     cin >> m; n     v.push_back(m); n   } n   sort(v.begin(), v.end()); n   int a = v[0]; n   int b = v[v.size() - 1]; n   v.push_front(a * (-1)); n   v.push_back(2 * l - b); n   int maxi = 0; n   for (int i = 1; i < v.size(); i++) { n     maxi = max(maxi, v[i] - v[i - 1]); n   } n   float maxi2 = maxi; n   cout << fixed << setprecision(10) << maxi2 / 2; n } n


#include <bits/stdc++.h> n using namespace std; n int n = 8; n struct point { n   int x; n   int y; n }; n bool f(point a, point b) { n   if (a.x != b.x) return a.x > b.x; n   return a.y > b.y; n } n int main() { n   point a[8]; n   for (int i = 0; i < n; i++) cin >> a[i].x >> a[i].y; n   int t1 = 0, t2 = 0; n   for (int i = 0; i < n; i += 2) { n     if (a[i].x == a[i + 1].x) { n       t1++; n     } else if (a[i].y == a[i + 1].y) n       t2++; n     else { n       cout <<  NO  << endl; n       return 0; n     } n   } n   if (t1 != 2 || t2 != 2) { n     cout <<  NO  << endl; n     return 0; n   } n   int t = 0; n   sort(a, a + n, f); n   if (a[1].x == a[2].x && a[1].y == a[2].y || n       a[3].x == a[4].x && a[3].y == a[4].y || n       a[5].x == a[6].x && a[5].y == a[6].y) { n     cout <<  NO  << endl; n     return 0; n   } n   for (int i = 0; i < n; i += 2) { n     if (a[i].x == a[i + 1].x && a[i].y == a[i + 1].y) n       t++; n     else { n       cout <<  NO  << endl; n       return 0; n     } n   } n   if (t == 4) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long md = 1e9 + 7; n long long fe(long long e) { n   if (e == 0) return 1; n   long long ans = fe(e / 2); n   ans = (ans * ans) % md; n   if (e % 2) ans *= 2; n   return (ans % md); n } n int main() { n   long long x, y; n   cin >> x >> y; n   if (y % x) { n     cout << 0 << endl; n     return 0; n   } n   long long k = y / x; n   vector<int> pp; n   long long kk = k; n   for (long long p = 2; p < 1e5; p++) { n     if (kk % p == 0) { n       pp.push_back(p); n       while (kk % p == 0) kk /= p; n     } n   } n   if (kk > 1) pp.push_back(kk); n   long long ans = fe(k - 1); n   int mx = 1 << (int)pp.size(); n   for (int i = 1; i < mx; i++) { n     long long p = 1, sgn = 1; n     for (int j = 0; 1 << j <= i; j++) { n       if (1 << j & i) { n         p *= pp[j]; n         sgn *= -1; n       } n     } n     ans += sgn * fe(k / p - 1); n     ans %= md; n     while (ans < 0) ans += md; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n signed main() { n   std::ios::sync_with_stdio(false); n   string str; n   cin >> str; n   long long int n = str.size(); n   string prf =   ; n   vector<long long int> v(n, 0); n   for (long long int i = 0; i < n - 1; i++) n     if (str[i] != str[i + 1]) v[i] = 1; n   if (str[n - 1] ==  a ) v[n - 1] = 1; n   for (long long int i = 0; i < n; i++) cout << v[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n long long u[11]; n int main() { n   long long x, y, n, i, j, k = 0, l, arr[200009]; n   cin >> n; n   for (i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   sort(arr, arr + n); n   for (i = 0, j = 1; i < n; i++) { n     if (arr[i] >= j) { n       k++; n       j++; n     } n   } n   cout << k << endl; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:64000000 ) n using namespace std; n const int inf32 = 1e9 + 9; n const long long inf64 = 1e18 + 18; n const int N = 6e5 + 5; n const long long mod = 1e9 + 7; n map<int, int> mp; n vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; n int n, mx; n void go(int &f, int a, int b, int mn) { n   if (a * b > mx) { n     if (a % b) return; n     mp[a] += mn; n     f -= mn; n     return; n   } n   go(f, a * b, b, (mn + 1) / 2); n   if (a % b) return; n   int t = min(f, mn); n   mp[a] += t; n   f -= t; n } n bool show() { n   int rez = 0; n   for (auto &x : mp) rez += x.second; n   return rez == n; n } n bool check(int j) { n   mp.clear(); n   mp[1] = n; n   int half = (n + 1) / 2; n   half += n / 100; n   for (int i = 0; i < j; ++i) { n     int x = primes[i]; n     int cnt = 0; n     bool flag = true; n     while (flag) { n       flag = false; n       for (auto &y : mp) { n         if (!y.second || !(y.first % x)) continue; n         int t = cnt; n         cnt += y.second; n         go(y.second, y.first, x, (y.second + 1) / 2); n         cnt -= y.second; n         flag |= t != cnt; n         if (cnt >= half) break; n       } n       if (cnt >= (n + 1) / 2) break; n     } n     if (!flag) { n       for (auto &y : mp) { n         if (y.second > 1 && y.first * x < mx) { n           mp[y.first * x] += 1; n           --y.second; n           ++cnt; n         } n         if (cnt >= (n + 1) / 2) break; n       } n       if (cnt < (n + 1) / 2) return false; n     } n   } n   for (auto &y : mp) { n     if (y.second > 1) { n       for (int i = 0; i < j; ++i) { n         int x = primes[i]; n         for (int i = x; y.second && i * y.first < mx; i *= x) { n           if (!mp[y.first * i]) { n             mp[y.first * i] = 1; n             --y.second; n           } n         } n       } n     } n   } n   if (n > 1000) { n     queue<int> lil; n     for (int i = 0; i < j; ++i) { n       for (int k = i + 1; k < j; ++k) { n         int x = primes[i], y = primes[k]; n         if (!mp[x * y]) lil.push(x * y); n         if (!mp[x]) lil.push(x); n         if (!mp[y]) lil.push(y); n         if (!mp[y * y]) lil.push(y * y); n         if (!mp[x * x]) lil.push(x * x); n         if (!mp[x * x * y]) lil.push(x * x * y); n         if (!mp[x * y * y]) lil.push(x * y * y); n         if (!mp[x * x * y * y]) lil.push(x * x * y * y); n       } n     } n     for (auto &x : mp) { n       while (x.second > 1) { n         while (!lil.empty() && mp[lil.front()]) lil.pop(); n         if (lil.empty()) return false; n         --x.second; n         mp[lil.front()] = 1; n         lil.pop(); n       } n     } n   } n   for (auto &x : mp) { n     if (x.second > 1) return false; n   } n   return true; n } n void solve() { n   cin >> n; n   mx = 2 * n * n; n   for (int j = 1; !check(j); ++j) n     ; n   int cnt = 0; n   for (auto &x : mp) { n     if (x.second) cout << x.first <<    ; n   } n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n static const int INF = 500000000; n template <class T> n void debug(T a, T b) { n   for (; a != b; ++a) cerr << *a <<    ; n   cerr << endl; n } n int n, len; n char s[1000005]; n int var[1000005][4]; n int cnt, ind; n int parse() { n   if (s[ind] ==  0 ) var[cnt][0] = 1; n   if (s[ind] ==  1 ) var[cnt][3] = 1; n   if (s[ind] ==  ? ) var[cnt][1] = var[cnt][2] = 1; n   ++cnt; n   int v = cnt - 1; n   if (s[ind] ==  ( ) { n     ++ind; n     int p = parse(), p2; n     char ope = s[ind]; n     ++ind; n     p2 = parse(); n     for (int i = 0; i < 4; ++i) n       if (var[p][i]) n         for (int j = 0; j < 4; ++j) n           if (var[p2][j]) { n             int x1 = i & 1, x2 = i >> 1 & 1, y1 = j & 1, y2 = j >> 1 & 1; n             int bit; n             if (ope ==  & ) bit = (x1 & y1) + (x2 & y2) * 2; n             if (ope ==  | ) bit = (x1 | y1) + (x2 | y2) * 2; n             if (ope ==  ^ ) bit = (x1 ^ y1) + (x2 ^ y2) * 2; n             var[v][bit] = 1; n           } n   } n   ++ind; n   return v; n } n int main() { n   scanf( %d%s , &n, s); n   len = strlen(s); n   int root = parse(); n   if (var[0][2] || var[0][1]) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, n1, m1; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   n1 = n, m1 = m; n   if ((n1 == m1 + 1 and !(n1 + m1 % 2)) or n1 > m1 + 1 or n       m1 / 2 + (m1 % 2) > n1 + 1 or n       (m1 / 2 + (m1 % 2)) == n1 + 1 and !(m1 / 2 + (m1 % 2) + n1 % 2)) { n     cout << -1; n     exit(0); n   } n   int p = 0; n   for (int i = 0; i < n + m; i++) { n     if (n1 > m1 or p == 2) n       cout << 0, p = 0, n1--; n     else n       cout << 1, p++, m1--; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t, d, k; n   cin >> n >> t >> k >> d; n   int a; n   a = (d + t) / t; n   if (a * k < n) cout <<  YES ; n   if (a * k >= n) cout <<  NO ; n } n


#include <bits/stdc++.h> n using namespace std; n int fact[100005]; n void criba() { n   for (int i = 2; i * i < 100005; i++) n     if (fact[i] == 0) n       for (int j = i * i; j < 100005; j += i) fact[j] = i; n   for (int i = 2; i < 100005; i++) n     if (fact[i] == 0) fact[i] = i; n } n bool vis[100005]; n set<pair<int, int> > S; n int process(int n, int id) { n   int m = n; n   set<pair<int, int> >::iterator it; n   while (n > 1) { n     int f = fact[n]; n     while (n % f == 0) n /= f; n     if (id == 2) { n       it = S.lower_bound(make_pair(f, -1)); n       if (it != S.end() && (*it).first == f) return (*it).second; n     } else { n       if (id == 0) n         S.erase(make_pair(f, m)); n       else n         S.insert(make_pair(f, m)); n     } n   } n   return -1; n } n int main() { n   criba(); n   int n, m; n   while (cin >> n >> m) { n     char op[2]; n     int x; n     for (int i = 0; i < m; i++) { n       scanf( %s%d , op, &x); n       if (op[0] ==  - ) { n         if (vis[x]) n           process(x, 0), puts( Success ), vis[x] = false; n         else n           puts( Already off ); n       } else { n         if (vis[x]) n           puts( Already on ); n         else { n           int num = process(x, 2); n           if (num == -1) n             puts( Success ), process(x, 1), vis[x] = true; n           else n             printf( Conflict with %d n , num); n         } n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n ifstream in( pairs.in ); n ofstream out( pairs.out ); n const int DIM = 1e5 + 5; n int nxt[DIM], val[DIM], pos[DIM]; n pair<int, int> tdp[DIM][2], cdp[DIM], arr[2][DIM]; n deque<int> chn; n bitset<DIM> oki, mrk, ist; n vector<pair<int, int>> sol; n vector<int> edg[DIM]; n pair<int, int> operator+(const pair<int, int> &p1, const pair<int, int> &p2) { n   return pair<int, int>(p1.first + p2.first, p1.second + p2.second); n } n pair<int, int> operator-(const pair<int, int> &p1, const pair<int, int> &p2) { n   return pair<int, int>(p1.first - p2.first, p1.second - p2.second); n } n void calcdp(int x) { n   mrk[x] = true; n   pos[x] = 0; n   tdp[x][0] = tdp[x][1] = pair<int, int>(0, 0); n   for (int y : edg[x]) { n     calcdp(y); n     tdp[x][0] = tdp[x][0] + max(tdp[y][0], tdp[y][1]); n     pair<int, int> aux = tdp[y][0] - max(tdp[y][0], tdp[y][1]) + n                          pair<int, int>(1, val[x] != val[y]); n     if (tdp[x][1] < aux) tdp[x][1] = aux, pos[x] = y; n   } n   tdp[x][1] = tdp[x][1] + tdp[x][0]; n } n int _nr[2]; n void solvedp(int x, bool ok, int nr) { n   if (ok and pos[x]) arr[nr][++_nr[nr]] = make_pair(x, pos[x]); n   for (int y : edg[x]) solvedp(y, (ok and y == pos[x]) ? false : true, nr); n } n int main(void) { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d %d , &nxt[i], &val[i]); n     edg[nxt[i]].push_back(i); n   } n   pair<int, int> ans; n   for (int i = 1; i <= n; ++i) n     if (!mrk[i]) { n       chn.clear(); n       for (int x = i; true; x = nxt[x]) { n         chn.push_back(x); n         if (!oki[x]) n           oki[x] = true; n         else { n           while (chn.front() != chn.back()) n             oki[chn.front()] = false, chn.pop_front(); n           break; n         } n       } n       pair<int, int> pr1, pr2; n       edg[chn[1]].erase(find(edg[chn[1]].begin(), edg[chn[1]].end(), chn[0])); n       calcdp(chn[0]); n       _nr[0] = 0; n       solvedp(chn[0], true, 0); n       edg[chn[1]].push_back(chn[0]); n       pr1 = max(tdp[chn[0]][0], tdp[chn[0]][1]); n       edg[chn[2]].erase(find(edg[chn[2]].begin(), edg[chn[2]].end(), chn[1])); n       calcdp(chn[1]); n       _nr[1] = 0; n       solvedp(chn[1], true, 1); n       edg[chn[2]].push_back(chn[1]); n       pr2 = max(tdp[chn[1]][0], tdp[chn[1]][1]); n       if (pr1 > pr2) { n         ans = ans + pr1; n         for (int i = 1; i <= pr1.first; ++i) sol.push_back(arr[0][i]); n       } else { n         ans = ans + pr2; n         for (int i = 1; i <= pr2.first; ++i) sol.push_back(arr[1][i]); n       } n     } n   printf( %d %d n , ans.first, ans.second); n   for (pair<int, int> pr : sol) printf( %d %d n , pr.first, pr.second); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n const int N = 1e6 + 7; n const int INF = 1e9 + 7; n int n, m; n int mid = 0; n int ile[70]; n long long mnoz = 1; n long long ans = 0; n bitset<128> maska; n void backtrack(int poz = 1) { n   ans += mnoz; n   if (poz > mid) return; n   for (int i = poz; i <= mid; i++) { n     if (!ile[i]) continue; n     bitset<128> old_mask = maska; n     bitset<128> pom = (maska >> i) | (maska << (m - i)); n     if ((pom & maska).count() == 0) { n       int old_mnoz = mnoz; n       maska = maska | pom; n       mnoz = mnoz * ile[i]; n       if (mnoz > INF) mnoz %= INF; n       backtrack(i + 1); n       maska = old_mask; n       mnoz = old_mnoz; n     } n   } n } n void solve() { n   cin >> m >> m >> n; n   mid = (m + 1) / 2; n   for (int i = 0; i < n; i++) { n     int temp; n     cin >> temp; n     temp = temp % m; n     temp = min(temp, m - temp); n     ile[temp]++; n   } n   maska[0] = 1; n   backtrack(); n   cout << ans % INF <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n vector<pair<int, bool> > v[300100]; n set<pair<int, int> > Bridges; n vector<pair<pair<int, int>, int> > All; n bool vis[300100], F = 0; n int cnt = 0, num[300100], low[300100], Size[300100], par[300100], a, b; n bool Ok[300100]; n void init() { n   for (int i = 1; i <= n; i++) par[i] = i, Size[i] = 1; n } n int getparent(int x) { n   if (x == par[x]) return x; n   return par[x] = getparent(par[x]); n } n void Merge(int x, int y) { n   x = getparent(x); n   y = getparent(y); n   if (Size[x] < Size[y]) swap(x, y); n   Size[x] += Size[y]; n   par[y] = x; n } n void BridgeDFS(int u) { n   num[u] = low[u] = cnt++; n   for (int i = 0; i < v[u].size(); i++) { n     int nxt = v[u][i].first; n     if (num[nxt] == -1) { n       par[nxt] = u; n       BridgeDFS(nxt); n       if (num[u] < low[nxt]) Bridges.insert({u, nxt}), Bridges.insert({nxt, u}); n       low[u] = min(low[u], low[nxt]); n     } else if (par[u] != nxt) n       low[u] = min(low[u], num[nxt]); n   } n } n void DFS(int x, bool f) { n   vis[x] = true; n   if (x == getparent(b)) F = f; n   for (int i = 0; i < v[x].size(); i++) { n     int y = getparent(v[x][i].first); n     if (!vis[y]) { n       DFS(y, f || Ok[y] || v[x][i].second); n     } n   } n } n int main() { n   memset(num, -1, sizeof(num)); n   memset(par, -1, sizeof(par)); n   int x, y, w; n   cin >> n >> m; n   while (m--) { n     scanf( %d%d%d , &x, &y, &w); n     v[x].push_back({y, w}); n     v[y].push_back({x, w}); n     All.push_back({{x, y}, w}); n   } n   cin >> a >> b; n   BridgeDFS(1); n   init(); n   for (auto it : All) { n     if (Bridges.find(it.first) == Bridges.end()) { n       int val = max(it.second, max((int)Ok[getparent(it.first.first)], n                                    (int)Ok[getparent(it.first.second)])); n       Merge(it.first.first, it.first.second); n       Ok[getparent(it.first.first)] = n           max((int)Ok[getparent(it.first.first)], val); n     } n   } n   for (auto it : All) { n     int x = getparent(it.first.first); n     int y = getparent(it.first.second); n     if (x != y) { n       v[x].push_back({y, it.second}); n       v[y].push_back({x, it.second}); n     } n   } n   if (getparent(a) == getparent(b)) { n     int u = getparent(a); n     if (Ok[u]) n       return cout <<  YES , 0; n     else n       return cout <<  NO , 0; n   } n   DFS(getparent(a), Ok[getparent(a)]); n   if (F) n     return cout <<  YES , 0; n   else n     return cout <<  NO , 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n typedef long double LD;  n typedef long long ll;  n #define int ll  n #define ff(i,a,b) for (int i = a; i < b; i++)  n #define bf(i,a,b) for (int i = a; i >= b; i--)  n #define all(v) v.begin(),v.end()  n #define show(a) for(auto xyz:a)cout<<xyz<<   ;cout<<endl; n #define F first  n #define S second n #define pb push_back n #define mp make_pair n #define lb lower_bound    n #define ub upper_bound n #define yes cout<< YES <<endl n #define no cout<< NO <<endl n #define one cout<< -1 <<endl n #define pi 3.141592653589793238 n #define err() cout<< ================================== <<endl; n #define errA(A) for(auto i:A)   cout<<i<<   ;cout<<endl; n #define err1(a) cout<<#a<<   <<a<<endl n #define err2(a,b) cout<<#a<<   <<a<<   <<#b<<   <<b<<endl n #define err3(a,b,c) cout<<#a<<   <<a<<   <<#b<<   <<b<<   <<#c<<   <<c<<endl n #define err4(a,b,c,d) cout<<#a<<   <<a<<   <<#b<<   <<b<<   <<#c<<   <<c<<   <<#d<<   <<d<<endl n #define intmx INT_MAX  n #define intmi INT_MIN n const int MOD=1e9+7; n  n void solve(){ n    int n,l,r; n    cin>>n>>l>>r; n    map<int,int> left,right; n    ff(i,0,n){ n        int x; n        cin>>x; n        if(i<=l-1) n        left[x]++; n        else n        right[x]++; n    } n    for(auto &i:left){ n        int x=min(i.S,right[i.F]); n        i.S-=x; n        right[i.F]-=x; n        //cout<<i.F<<   <<i.S<<endl; n    } n    int cntl=0,cntr=0; n    for(auto i:left) n    cntl+=i.S; n    for(auto i:right) n    cntr+=i.S; n    int ans=0; n    if(cntl<=cntr){ n        ans+=cntl; n        int d=(cntr-cntl)/2; n        int cnt=0; n        for(auto i:right){ n            cnt+=i.S/2; n        } n        if(cnt<d){ n            ans+=cnt; n            ans+=2*(d-cnt); n        } n        else n        ans+=d; n    } n    else{ n        ans+=cntr; n        int d=(cntl-cntr)/2; n        int cnt=0; n        for(auto i:left){ n            cnt+=i.S/2; n        } n        if(cnt<d){ n            ans+=cnt; n            ans+=2*(d-cnt); n        } n        else n        ans+=d; n    } n    cout<<ans<<endl; n } n signed main() { n     int t=1; n     ios_base::sync_with_stdio(false); n     cin.tie(NULL); n     cin>>t; n     while(t--){ n         solve(); n     } n } n


#include <bits/stdc++.h> n using namespace std; n int entries[1010000][2]; n long long has[1010000]; n long long power[1010000]; n int main() { n   int i, j; n   long long ans = 0; n   int n, m, u, v, p = 31; n   scanf( %d%d , &n, &m); n   power[0] = 1; n   for (i = 1; i <= n; i++) power[i] = power[i - 1] * p; n   for (i = 0; i < m; i++) { n     scanf( %d%d , &u, &v); n     u--; n     v--; n     has[u] += power[v]; n     has[v] += power[u]; n     entries[i][0] = u; n     entries[i][1] = v; n   } n   for (i = 0; i < m; i++) { n     u = entries[i][0]; n     v = entries[i][1]; n     if (has[u] + power[u] == has[v] + power[v]) ans++; n   } n   sort(has, has + n); n   for (i = 0; i < n; i = j) { n     for (j = i; j < n && has[j] == has[i]; j++) n       ; n     long long temp = j - i; n     ans += temp * (temp - 1) / 2; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n using ii = pair<int, int>; n using vi = vector<int>; n using vl = vector<ll>; n using vii = vector<ii>; n using graph = vector<vi>; n using matrix = graph; n const int MAXN = 1e6 + 5; n const int INF = 0x3f3f3f3f; n const ll INFL = 1e18 + 4; n const ll mod = 1e9 + 7; n graph g; n int h[MAXN]; n vi leafs; n bool seen[MAXN]; n void dfs(int u) { n   seen[u] = 1; n   bool yes = 1; n   for (auto v : g[u]) n     if (!seen[v]) { n       yes = 0; n       h[v] = h[u] + 1; n       dfs(v); n     } n   if (yes) leafs.push_back(h[u]); n } n int main() { n   int n; n   scanf( %d , &n); n   g.assign(n + 1, vi()); n   int u, v; n   for (int i = 1; i < n; ++i) { n     scanf( %d %d , &u, &v); n     g[u].push_back(v); n     g[v].push_back(u); n   } n   int ans = 0; n   seen[1] = 1; n   for (auto v : g[1]) { n     leafs.clear(); n     dfs(v); n     sort(leafs.begin(), leafs.end()); n     for (int i = 1; i < (int)leafs.size(); ++i) { n       leafs[i] = max(leafs[i], leafs[i - 1] + 1); n     } n     ans = max(ans, leafs.back()); n   } n   printf( %d n , ans + 1); n } n


#include <bits/stdc++.h> n using namespace std; n long long bigmod(long long base, long long power, long long mod) { n   if (power == 0) n     return 1; n   else if (power % 2 == 1) { n     long long p1 = base % mod; n     long long p2 = (bigmod(base, power - 1, mod)) % mod; n     return (p1 * p2) % mod; n   } else if (power % 2 == 0) { n     long long p1 = (bigmod(base, power / 2, mod)) % mod; n     return (p1 * p1) % mod; n   } n } n long long ncr(long long n, long long k) { n   long long C[n + 1][k + 1]; n   long long i, j; n   for (i = 0; i <= n; i++) { n     for (j = 0; j <= min(i, k); j++) { n       if (j == 0 || j == i) n         C[i][j] = 1; n       else n         C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; n     } n   } n   return C[n][k]; n } n long long prime[500008]; n long long sp[500008]; n void seive() { n   for (int i = 2; i <= 500008; i += 2) sp[i] = 2; n   for (int i = 3; i <= 500008; i += 2) sp[i] = i; n   long long x = sqrt(500008); n   for (int i = 3; i <= x; i += 2) { n     if (sp[i] == i) { n       for (int j = i * i; j <= 500008; j += i) { n         sp[j] = i; n       } n     } n   } n } n map<long long, long long> m; n void pf(long long n) { n   while (1) { n     if (n <= 1) break; n     long long x = sp[n]; n     while (n % x == 0) { n       n /= x; n       m[x]++; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   long long t = 1; n   seive(); n   while (t--) { n     long long k, a, b; n     cin >> k >> a >> b; n     string s; n     cin >> s; n     long long n = s.size(); n     long long x = n / k; n     long long y = ceil(n / (1.0 * k)); n     if (x >= a && y <= b) { n       int j = 0; n       long long rem = n % k; n       for (int i = 1; i <= rem; i++) { n         for (int z = 0; z <= x; z++) { n           cout << s[j++]; n         } n         cout << endl; n       } n       for (int i = 1; i <= k - rem; i++) { n         for (int z = 0; z < x; z++) { n           cout << s[j++]; n         } n         cout << endl; n       } n     } else n       cout <<  No solution  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str1, str2; n   cin >> str1 >> str2; n   int siz = str1.size(); n   siz--; n   while (siz > -1) { n     if (str1[siz] ==  z ) { n       str1[siz] =  a ; n       siz--; n     } else { n       str1[siz]++; n       break; n     } n   } n   if (siz == -1 || str1 == str2) n     cout <<  No such string  << endl; n   else n     cout << str1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int size = 1000007; n const long long modulo = 1000000007; n const long long INF = 1e9; n const double EPS = 1e-10; n int main() { n   cin.sync_with_stdio(0); n   int n, m, k; n   cin >> n >> m >> k; n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int a; n     cin >> a; n     if (a == 1) { n       if (m > 0) n         --m; n       else n         ++ans; n     } else { n       if (k > 0) n         --k; n       else if (m > 0) n         --m; n       else n         ++ans; n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> G[505000], S[505000]; n unordered_map<int, int> f[505000]; n int n, m, dep[505000], fa[505000], bel[505000], used[505000]; n void dfs(int x, int fff) { n   fa[x] = fff; n   dep[x] = dep[fff] + 1; n   for (auto y : G[x]) n     if (y != fff) { n       if (!dep[y]) n         dfs(y, x); n       else if (dep[y] > dep[x]) { n         for (;; y = fa[y]) { n           bel[y] = x; n           S[x].push_back(y); n           if (y == x) break; n         } n       } n     } n } n deque<int> q; n void add(int x) { n   for (; !q.empty() && q.back() < x;) q.pop_back(); n   q.push_back(x); n } n void del(int x) { n   if (q.front() == x) q.pop_front(); n } n int gaof(int fff, int x) { n   if (f[x].count(fff)) return f[x][fff]; n   if (!bel[x]) { n     if (!used[x]) { n       used[x] = 1; n       int res = 0; n       for (auto y : G[x]) n         if (y != fff) res = max(res, gaof(x, y) + 1); n       return f[x][fff] = res; n     } n     int now = 0; n     for (auto y : G[x]) now = max(now, gaof(x, y) + 1); n     f[x][0] = now; n     now = 0; n     for (auto y : G[x]) { n       f[x][y] = max(f[x][y], now); n       now = max(now, gaof(x, y) + 1); n     } n     now = 0; n     for (auto it = G[x].rbegin(); it != G[x].rend(); ++it) { n       int y = *it; n       f[x][y] = max(f[x][y], now); n       now = max(now, gaof(x, y) + 1); n     } n     return f[x][fff]; n   } n   int b = bel[x]; n   vector<int>& T = S[b]; n   int sz = T.size(); n   if (!used[b]) { n     used[b] = 1; n     int id = find(T.begin(), T.end(), x) - T.begin(); n     int res = 0; n     for (int i = 0; i < sz; ++i) { n       int d = abs(i - id); n       d = min(d, sz - d); n       int u = T[i]; n       res = max(res, d); n       for (auto v : G[u]) n         if (!(u == x && v == fff) && bel[v] != b) n           res = max(res, gaof(u, v) + d + 1); n     } n     return f[x][fff] = res; n   } n   vector<int> mx, nex, pre; n   mx.resize(sz); n   nex.resize(sz); n   pre.resize(sz); n   for (int i = 0; i < sz; ++i) { n     int u = T[i]; n     for (auto v : G[u]) n       if (bel[v] != b) mx[i] = max(mx[i], gaof(u, v) + 1); n   } n   q.clear(); n   int l = sz / 2; n   for (int i = 0; i < sz * 2; ++i) { n     add(mx[i % sz] + i); n     if (i >= l) { n       del(mx[(i - l) % sz] + (i - l)); n       nex[(i - l) % sz] = q.front() - (i - l); n     } n   } n   q.clear(); n   for (int i = sz * 2; i > 0; --i) { n     add(mx[i % sz] - i); n     if (i + l <= sz * 2) { n       del(mx[(i + l) % sz] - (i + l)); n       pre[(i + l) % sz] = q.front() + (i + l); n     } n   } n   for (int i = 0; i < sz; ++i) { n     int u = T[i]; n     for (auto v : G[u]) n       if (bel[v] != b) f[u][v] = max(pre[i], nex[i]); n     f[u][0] = max(max(pre[i], nex[i]), mx[i]); n     int now = 0; n     for (auto v : G[u]) { n       if (bel[v] == b) continue; n       f[u][v] = max(f[u][v], now); n       now = max(now, gaof(u, v) + 1); n     } n     now = 0; n     for (auto it = G[u].rbegin(); it != G[u].rend(); ++it) { n       int v = *it; n       if (bel[v] == b) continue; n       f[u][v] = max(f[u][v], now); n       now = max(now, gaof(u, v) + 1); n     } n   } n   return f[x][fff]; n } n int main() { n   cin >> n >> m; n   int u, v; n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   dfs(1, 0); n   for (int i = 1; i <= n; ++i) { n     printf( %d  , gaof(0, i)); n   } n   puts(  ); n } n


#include <bits/stdc++.h> n int main(void) { n   int h, m; n   scanf( %d:%d , &h, &m); n   while (1) { n     m++; n     if (m == 60) { n       m = 0; n       h = (h + 1) % 24; n     } n     if ((m % 10 == h / 10) && (h % 10 == m / 10)) { n       printf( %02d:%02d , h, m); n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, cnt = INT_MAX, c = 0; n   cin >> n >> k; n   string s, temp =   ; n   cin >> s; n   int freq[26]; n   memset(freq, 0, sizeof(freq)); n   for (int i = 0; i < n; i++) freq[s[i] -  A ]++; n   for (int i = 0; i < k; i++) { n     if (!freq[i]) return cout << 0 << endl, 0; n     cnt = min(cnt, freq[i]); n   } n   cout << cnt * k << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int a[222], sum[222]; n struct node { n   int a, id; n } b[222]; n bool cmp(node s, node d) { return s.a < d.a; } n int main() { n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> a[i], b[i].a = a[i], b[i].id = i; n   ; n   sum[0] = a[0]; n   for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + a[i]; n   sort(b, b + n, cmp); n   int ans = -22222; n   for (int l = 0; l < n; l++) n     for (int r = l; r < n; r++) { n       int p = min(k, r - l + 1); n       int res = sum[r]; n       if (l) res -= sum[l - 1]; n       int i = 0, j = n - 1; n       while (p--) { n         while (i < n && b[i].id < l || b[i].id > r) i++; n         while (j >= 0 && b[j].id >= l && b[j].id <= r) j--; n         if (i == n || j == -1) break; n         if (b[i].a > b[j].a) break; n         res += (b[j].a - b[i].a); n         i++; n         j--; n       } n       ans = max(ans, res); n     } n   cout << ans << endl; n } n


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


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n const long long mod = 1e9 + 7; n const long long N = 1e5 + 10; n int main() { n   ios::sync_with_stdio(false); n   long long n; n   cin >> n; n   vector<pair<long long, long long> > v(n); n   for (long long i = 0; i < n; i++) cin >> v[i].first >> v[i].second; n   map<long long, long long> m; n   for (long long i = 0; i < n; i++) m[v[i].first]++; n   long long ans = 0; n   for (auto it : m) ans += it.second * (it.second - 1) / 2; n   m.clear(); n   for (long long i = 0; i < n; i++) m[v[i].second]++; n   for (auto it : m) ans += it.second * (it.second - 1) / 2; n   map<pair<long long, long long>, long long> freq; n   for (long long i = 0; i < n; i++) freq[make_pair(v[i].first, v[i].second)]++; n   for (auto it : freq) ans -= it.second * (it.second - 1) / 2; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 4e5 + 7, M = 2760000; n int a[N], prime[N], m, flg[M], f[M], x[N], y[N], nn, mm, tot[M]; n template <typename Tp> n inline void read(Tp &x) { n   x = 0; n   bool f = true; n   char ch = getchar(); n   for (; ch <  0  || ch >  9 ; ch = getchar()) f ^= ch ==  - ; n   for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + (ch ^ 48); n   x = f ? x : -x; n } n inline void primes() { n   m = 0; n   std::fill(flg + 1, flg + 2750132, true); n   for (int i = 2; i <= 2750131; i++) { n     if (flg[i]) { n       prime[++m] = i; n       f[i] = i; n     } n     for (int j = 1; j <= m && i * prime[j] <= 2750131; j++) { n       flg[i * prime[j]] = false; n       f[i * prime[j]] = std::min(f[i], prime[j]); n       if (!(i % prime[j])) break; n     } n   } n } n int main() { n   int n; n   read(n); n   n <<= 1; n   for (int i = 1; i <= n; i++) read(a[i]); n   std::sort(a + 1, a + n + 1); n   primes(); n   for (int i = 1; i <= n; i++) n     if (flg[a[i]]) n       x[++nn] = a[i]; n     else n       y[++mm] = a[i]; n   for (int i = mm; i; i--) { n     if (tot[y[i]]) { n       tot[y[i]]--; n       continue; n     } n     printf( %d  , y[i]); n     tot[y[i] / f[y[i]]]++; n   } n   for (int i = 1; i <= nn; i++) { n     if (tot[x[i]]) { n       tot[x[i]]--; n       continue; n     } n     printf( %d  , x[i]); n     tot[prime[x[i]]]++; n   } n   return 0; n } n


#include <bits/stdc++.h> n #define ll long long n #define db long double n #define x first n #define y second n #define mp make_pair n #define pb push_back n #define all(a) a.begin(), a.end() n  n using namespace std; n  n struct Pair { n   int x; n   int y; n   int def_stack; n }; n  n bool cmp(Pair &a, Pair &b) { n   return (a.x < b.x); n } n  n const int INF = 1e9; n  n pair<int, int> check(set<pair<int, int> > &st, Pair &p, Pair &prev, int cost, int t) { n   if (!st.size()) return mp(-1, -1); n   auto it = st.lower_bound(mp(p.y, -1)); n   if (it == st.end()) return mp(-1, -1); n  n   int C = ((*it).y) + cost + t; n   return mp(prev.y, C); n  n } n  n void Ins(set<pair<int, int> > &st, pair<int, int> &p, int t) { n   p.y -= t; n  n   if (st.size()) { n  n     auto it = st.lower_bound(p); n     if (it != st.end()) { n       if ((*it).y <= p.y) return; n     } n  n   } n  n   while (st.size()) { n     auto it = st.lower_bound(p); n     if (it == st.begin()) break; n     it--; n  n     if ((*it).y <= p.y) break; n     st.erase(it); n   } n  n   st.insert(p); n  n } n  n int main(){ n #ifdef LOCAL n  freopen( D_input.txt ,  r , stdin); n  //freopen( D_output.txt ,  w , stdout); n #endif n  ios_base::sync_with_stdio(0); n  cin.tie(0); n  n   int n; n   cin >> n; n  n   vector<Pair> v(n); n   for (auto &x : v) { n     cin >> x.x >> x.y; n     x.def_stack = 0; n     if (x.x > x.y) { n       swap(x.x, x.y); n       x.def_stack = 1; n       if (x.x > n) { n         cout << -1; n         exit(0); n       } n     } n     else if (x.x > n) { n       cout << -1; n       exit(0); n     } n   } n  n   sort(all(v), cmp); n  n   int fadd = 0, sadd = 0; n  n   set<pair<int, int> > first_stack, second_stack; n   int D = v[0].def_stack; n  n   first_stack.insert(mp(INF, 1-D)); n   second_stack.insert(mp(INF, D)); n  n   for (int i = 1; i < n; ++i) { n     D = v[i].def_stack; n      n     auto A = check(first_stack, v[i], v[i-1], D, fadd); n     auto B = check(second_stack, v[i], v[i-1], 1-D, sadd); n  n     if (v[i].y < v[i-1].y) { n       if (D==0) fadd++; n       else sadd++; n     } n     else { n       fadd = 0; n       sadd = 0; n       first_stack.clear(); n       second_stack.clear(); n     } n  n    // cout << A.x <<     << B.x << endl; n  n     if (A.x != -1) { n       Ins(second_stack, A, sadd); n     } n     if (B.x != -1) { n       Ins(first_stack, B, fadd); n     } n   } n  n   int ans = INF; n   if (first_stack.size()) { n     ans = min(ans, (*first_stack.begin()).y + fadd); n   } n   if (second_stack.size()) { n     ans = min(ans, (*second_stack.begin()).y + sadd); n   } n  n   if (ans == INF) cout << -1; n   else cout << ans; n  n  n  n } n


#include <bits/stdc++.h> n using namespace std; n bool const TO_FILE = false; n string const FILENAME =   ; n void prepare() { n   if (TO_FILE) { n     freopen((FILENAME +  .in ).c_str(),  r , stdin); n     freopen((FILENAME +  .out ).c_str(),  w , stdout); n   } n } n void solve() { n   int n, m, k; n   cin >> n >> m >> k; n   map<int, pair<int, int>> apps; n   map<pair<int, int>, int> apps2; n   int curScr = 0; n   int curPos = 0; n   for (int i = 0; i < n; i++) { n     int app; n     cin >> app; n     apps[app] = make_pair(curScr, curPos); n     apps2[make_pair(curScr, curPos)] = app; n     if (++curPos == k) { n       curPos = 0; n       curScr++; n     } n   } n   long long res = m; n   for (int i = 0; i < m; i++) { n     int curApp, prevApp; n     cin >> curApp; n     int curScr = apps[curApp].first; n     int curPos = apps[curApp].second; n     int prevScr, prevPos; n     if (curScr == 0 && curPos == 0) n       continue; n     else if (curPos == 0) { n       prevScr = curScr - 1; n       prevPos = k - 1; n       prevApp = apps2[make_pair(prevScr, prevPos)]; n     } else { n       prevScr = curScr; n       prevPos = curPos - 1; n       prevApp = apps2[make_pair(prevScr, prevPos)]; n     } n     apps[curApp] = make_pair(prevScr, prevPos); n     apps2[make_pair(prevScr, prevPos)] = curApp; n     apps[prevApp] = make_pair(curScr, curPos); n     apps2[make_pair(curScr, curPos)] = prevApp; n     res += curScr; n   } n   cout << res; n } n int main() { n   prepare(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n using P = pair<int, int>; n constexpr int INF = 1 << 30; n template <typename Monoid> n struct SegmentTree { n  private: n   using F = function<Monoid(Monoid, Monoid)>; n   int N; n   vector<Monoid> node; n   F f; n   Monoid e; n  n  public: n   SegmentTree() {} n   SegmentTree(F f, Monoid e) : f(f), e(e) {} n   void init(int sz) { n     N = 1; n     while (N < sz) N <<= 1; n     node.assign(2 * N - 1, e); n   } n   void build(vector<Monoid>& v) { n     int sz = int(v.size()); n     init(sz); n     for (int i = 0; i < sz; i++) { n       node[i + N - 1] = v[i]; n     } n     for (int i = N - 2; i >= 0; i--) { n       node[i] = f(node[i * 2 + 1], node[i * 2 + 2]); n     } n   } n   void update(int k, Monoid x) { n     k += N - 1; n     node[k] = x; n     while (k > 0) { n       k = (k - 1) / 2; n       node[k] = f(node[2 * k + 1], node[2 * k + 2]); n     } n   } n   Monoid query(int a, int b) { return query(a, b, 0, 0, N); } n   Monoid query(int a, int b, int k, int l, int r) { n     if (b <= l || r <= a) return e; n     if (a <= l && r <= b) return node[k]; n     Monoid vl, vr; n     vl = query(a, b, 2 * k + 1, l, (l + r) / 2); n     vr = query(a, b, 2 * k + 2, (l + r) / 2, r); n     return f(vl, vr); n   } n }; n SegmentTree<int> seg([](int a, int b) { return max(a, b); }, -INF); n vector<vector<int> > g; n vector<int> a; n int main() { n   int n; n   scanf( %d , &n); n   g.resize(n); n   a.resize(n); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d %d , &u, &v); n     u--; n     v--; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   seg.build(a); n   int ans = INF; n   for (int i = 0; i < n; i++) { n     int maxi = a[i]; n     g[i].push_back(i); n     sort(g[i].begin(), g[i].end()); n     int pre = 0; n     for (auto j : g[i]) { n       if (j != i) maxi = max(maxi, a[j] + 1); n       maxi = max(maxi, seg.query(pre, j) + 2); n       pre = j + 1; n     } n     maxi = max(maxi, seg.query(pre, n) + 2); n     ans = min(ans, maxi); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int dx[] = {0, 1, -1, 0, 1, -1, -1, 1}; n const long long int dy[] = {-1, 0, 0, 1, 1, 1, -1, -1}; n long long int gcd(long long int x, long long int y) { n   return (y != 0 ? gcd(y, x % y) : x); n } n long long int lcm(long long int x, long long int y) { n   return (x / gcd(x, y) * y); n } n struct edge { n   int viz; n   int cap; n   int cost; n   int dual; n   edge() {} n   edge(int viz, int cap, int cost, int dual) n       : viz(viz), cap(cap), cost(cost), dual(dual) {} n }; n int pro[3054]; n int spo[3054]; n vector<edge> g[3054]; n int d[3054]; n int p[3054]; n int p_edge[3054]; n int color[3054]; n int flow; n int fcost; n void add_edge(int x, int y, int cap, int cost) { n   g[x].push_back(edge(y, cap, cost, (int)g[y].size())); n   g[y].push_back(edge(x, 0, -cost, (int)g[x].size() - 1)); n } n int SPFA(int s, int t) { n   queue<int> fila; n   for (int i = 0; i <= t; i++) { n     d[i] = 1000000000; n     color[i] = 0; n   } n   d[s] = 0; n   fila.push(s); n   while (!fila.empty()) { n     int next = fila.front(); n     color[next] = 0; n     fila.pop(); n     for (int i = 0; i < (int)g[next].size(); i++) { n       int viz = g[next][i].viz; n       int cap = g[next][i].cap; n       int cost = d[next] + g[next][i].cost; n       if (cap > 0 && d[viz] > cost) { n         d[viz] = cost; n         p_edge[viz] = i; n         p[viz] = next; n         if (color[viz] == 0) { n           fila.push(viz); n           color[viz] = 1; n         } n       } n     } n   } n   return d[t] != 1000000000; n } n void mcmf(int s, int t) { n   while (SPFA(s, t)) { n     int augment = 1000000000; n     for (int v = t; v != s; v = p[v]) { n       int idx = p_edge[v]; n       augment = min(augment, g[p[v]][idx].cap); n     } n     for (int v = t; v != s; v = p[v]) { n       int idx = p_edge[v]; n       int dual = g[p[v]][idx].dual; n       g[p[v]][idx].cap -= augment; n       g[v][dual].cap += augment; n     } n     flow += augment; n     fcost += d[t] * augment; n   } n   return; n } n int main(void) { n   int src = 3054 - 1; n   int sink = 3054 - 2; n   int sports = 3054 - 3; n   int programming = 3054 - 4; n   int n, pp, ss; n   cin >> n >> pp >> ss; n   for (int i = 0; i < n; i++) { n     add_edge(src, i, 1, 0); n     cin >> pro[i]; n     add_edge(i, programming, 1, -pro[i]); n   } n   for (int i = 0; i < n; i++) { n     cin >> spo[i]; n     add_edge(i, sports, 1, -spo[i]); n   } n   add_edge(programming, sink, pp, 0); n   add_edge(sports, sink, ss, 0); n   mcmf(src, sink); n   cout << -fcost << endl; n   vector<int> saida1, saida2; n   for (int i = 0; i < n; i++) { n     if (g[i][1].cap == 0) { n       saida1.push_back(i + 1); n     } n     if (g[i][2].cap == 0) { n       saida2.push_back(i + 1); n     } n   } n   for (int i = 0; i < saida1.size(); i++) { n     cout << saida1[i] <<    ; n   } n   cout << endl; n   for (int i = 0; i < saida2.size(); i++) { n     cout << saida2[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline T gcd(T a, T b) { n   while (b) b ^= a ^= b ^= a %= b; n   return a; n } n template <class T> n inline T lcm(T a, T b) { n   a = abs(a); n   b = abs(b); n   return (a / gcd(a, b)) * b; n } n template <class T> n inline T my_strrev(T array) { n   int N = strlen(array); n   for (int i = 0; i < N / 2; ++i) swap(array[i], array[N - i - 1]); n } n template <class T> n inline T my_revara(T array, int N) { n   for (int i = 0; i < N / 2; ++i) swap(array[i], array[N - i - 1]); n } n int fx[8] = {0, -1, 1, 1, 1, 0, -1, -1}; n int fy[8] = {1, 0, 1, -1, 0, -1, -1, 1}; n int main() { n   string str1, str2; n   cin >> str1; n   cin >> str2; n   int len = str1.length(); n   int flag = 0; n   for (int i = 0; i < len; i++) { n     if (flag == 1) { n       if (str2[i] !=  a ) { n         str2[i] = str2[i] - 1; n         cout << str2 << endl; n         return 0; n       } n       if (str1[i] !=  z ) { n         str1[i] = str1[i] + 1; n         cout << str1 << endl; n         return 0; n       } n     } n     if (str2[i] > str1[i]) { n       if (str2[i] - str1[i] > 1) { n         str1[i] = str1[i] + 1; n         cout << str1 << endl; n         return 0; n       } n       flag = 1; n     } n   } n   printf( No such string n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double a, ay, b, by, dx, dy, x[100011], y[100011], before[100011], n     after[100011], r = 1e18, dist; n int n, i, pos; n int main() { n   cin >> a >> ay >> b >> by >> dx >> dy >> n; n   for (i = 1; i <= n; i++) { n     cin >> x[i] >> y[i]; n     dist += 2 * sqrt((dx - x[i]) * (dx - x[i]) + (dy - y[i]) * (dy - y[i])); n   } n   for (i = 1; i <= n; i++) n     r = min(r, dist + n                    sqrt((a - x[i]) * (a - x[i]) + (ay - y[i]) * (ay - y[i])) - n                    sqrt((dx - x[i]) * (dx - x[i]) + (dy - y[i]) * (dy - y[i]))); n   for (i = 1; i <= n; i++) n     r = min(r, dist + n                    sqrt((b - x[i]) * (b - x[i]) + (by - y[i]) * (by - y[i])) - n                    sqrt((dx - x[i]) * (dx - x[i]) + (dy - y[i]) * (dy - y[i]))); n   for (i = 1; i <= n; i++) n     before[i] = after[i] = n         sqrt((b - x[i]) * (b - x[i]) + (by - y[i]) * (by - y[i])) - n         sqrt((dx - x[i]) * (dx - x[i]) + (dy - y[i]) * (dy - y[i])); n   for (i = 2; i <= n; ++i) before[i] = min(before[i], before[i - 1]); n   for (i = n - 1; i >= 1; --i) after[i] = min(after[i], after[i + 1]); n   before[0] = after[n + 1] = 1e18; n   for (i = 1; i <= n; ++i) n     r = min(r, dist + n                    sqrt((a - x[i]) * (a - x[i]) + (ay - y[i]) * (ay - y[i])) - n                    sqrt((dx - x[i]) * (dx - x[i]) + (dy - y[i]) * (dy - y[i])) + n                    min(before[i - 1], after[i + 1])); n   cout.precision(15); n   cout << r; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void read(T& a) { n   static char c; n   static bool b; n   for (c = getchar(), b = false; !isdigit(c); c = getchar()) n     if (c ==  - ) b = true; n   for (a = 0; isdigit(c); c = getchar()) a = a * 10 + (c -  0 ); n   if (b) a = -a; n } n template <> n void read<char>(char& c) { n   do { n     c = getchar(); n   } while (isspace(c)); n } n template <typename T, typename... Args> n void read(T& a, Args&... args) { n   read(a); n   read(args...); n } n const int MAXN = 2e5 + 10; n const int MAXM = MAXN; n const int INF = 0x3f3f3f3f; n template <typename T> n struct Segment { n   struct Node { n     int l, r, lc, rc; n     T v; n   } t[MAXN << 2]; n   int cnt; n   void maintain(int x) { t[x].v = min(t[t[x].lc].v, t[t[x].rc].v); } n   int create() { return ++cnt; } n   int build(int l, int r, T* origin) { n     int x = create(); n     t[x].l = l, t[x].r = r; n     if (l != r) { n       int mid = l + r; n       mid >>= 1; n       t[x].lc = build(l, mid, origin); n       t[x].rc = build(mid + 1, r, origin); n       maintain(x); n     } else n       t[x].v = origin[l]; n     return x; n   } n   T query(int x, int l, int r) { n     if (l <= t[x].l && t[x].r <= r) return t[x].v; n     int mid = t[x].l + t[x].r; n     mid >>= 1; n     if (r <= mid) n       return query(t[x].lc, l, r); n     else if (l > mid) n       return query(t[x].rc, l, r); n     return min(query(t[x].lc, l, r), query(t[x].rc, l, r)); n   } n   void modify(int x, int l, int v) { n     if (t[x].l == t[x].r) { n       t[x].v = v; n       return; n     } n     int mid = t[x].l + t[x].r; n     mid >>= 1; n     if (l <= mid) n       modify(t[x].lc, l, v); n     else n       modify(t[x].rc, l, v); n     maintain(x); n   } n }; n struct BaseGraph { n   struct Lfs { n     int to, nxt; n   } t[MAXM << 1]; n   int head[MAXN], cnt; n   void add(int x, int y) { n     ++cnt; n     t[cnt].to = y; n     t[cnt].nxt = head[x]; n     head[x] = cnt; n   } n   void add2(int x, int y) { add(x, y), add(y, x); } n }; n struct Graph : BaseGraph { n   int dfn[MAXN], low[MAXN], tm, n; n   stack<int> st; n   BaseGraph* gh; n   void tarjan(int x) { n     dfn[x] = low[x] = ++tm; n     st.push(x); n     for (int i = head[x]; i; i = t[i].nxt) { n       int v = t[i].to; n       if (dfn[v]) { n         low[x] = min(low[x], dfn[v]); n       } else { n         tarjan(v); n         low[x] = min(low[x], low[v]); n         if (low[v] >= dfn[x]) { n           ++n; n           int y; n           gh->add2(x, n); n           do { n             y = st.top(); n             st.pop(); n             gh->add2(y, n); n           } while (y != v); n         } n       } n     } n   } n   void tarjan(int n, BaseGraph* p) { n     this->n = n; n     this->gh = p; n     tarjan(1); n   } n } G; n struct NewGraph : BaseGraph { n   multiset<int> R[MAXN]; n   int sz[MAXN], son[MAXN], top[MAXN]; n   int dfn[MAXN], dep[MAXN], fa[MAXN], id; n   int w[MAXN], v[MAXN], rt; n   int bn, n; n   Segment<int> S; n   void prepare(int bn, int n) { n     fa[1] = id = 0; n     dep[1] = 1; n     dfs(1); n     deal(1, 1); n     this->bn = bn; n     this->n = n; n     for (int x = bn + 1; x <= n; ++x) { n       for (int i = head[x]; i; i = t[i].nxt) { n         int v = t[i].to; n         if (v == fa[x]) continue; n         R[x].insert(this->v[v]); n       } n       if (R[x].empty()) { n         v[x] = INF; n       } else n         v[x] = *R[x].begin(); n     } n     for (int i = 1; i <= n; ++i) w[dfn[i]] = v[i]; n     rt = S.build(1, n, w); n   } n   void dfs(int x) { n     sz[x] = 1; n     son[x] = 0; n     for (int i = head[x]; i; i = t[i].nxt) { n       int v = t[i].to; n       if (v == fa[x]) continue; n       dep[v] = dep[x] + 1; n       fa[v] = x; n       dfs(v); n       sz[x] += sz[v]; n       if (sz[son[x]] < sz[v]) son[x] = v; n     } n   } n   void deal(int x, int topf) { n     if (!x) return; n     dfn[x] = ++id; n     top[x] = topf; n     deal(son[x], topf); n     for (int i = head[x]; i; i = t[i].nxt) { n       int v = t[i].to; n       if (v == fa[x]) continue; n       if (v == son[x]) continue; n       deal(v, v); n     } n   } n   void modify(int x, int val) { n     int f = fa[x]; n     if (f) { n       R[f].erase(R[f].find(v[x])); n       R[f].insert(val); n       v[f] = *R[f].begin(); n       S.modify(rt, dfn[f], v[f]); n     } n     v[x] = val; n     S.modify(rt, dfn[x], val); n   } n   int query(int x, int y) { n     int ans = INF; n     while (top[x] != top[y]) { n       if (dep[top[x]] < dep[top[y]]) swap(x, y); n       ans = min(ans, S.query(rt, dfn[top[x]], dfn[x])); n       x = fa[top[x]]; n     } n     if (dep[x] > dep[y]) swap(x, y); n     ans = min(ans, S.query(rt, dfn[x], dfn[y])); n     if (x > bn) ans = min(ans, v[fa[x]]); n     return ans; n   } n } G2; n int main() { n   int n, m, T; n   read(n, m, T); n   for (int i = 1; i <= n; ++i) read(G2.v[i]); n   for (int i = 1; i <= m; ++i) { n     int x, y; n     read(x, y); n     G.add2(x, y); n   } n   G.tarjan(n, &G2); n   G2.prepare(n, G.n); n   while (T--) { n     char c; n     int x, y; n     read(c, x, y); n     switch (c) { n       case  A : n         printf( %d n , G2.query(x, y)); n         break; n       case  C : n         G2.modify(x, y); n         break; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int n = 1e7; n int main() { n   long long a, b, c, ans = 0; n   cin >> a >> b >> c; n   long long mx = max(a, max(b, c)); n   long long sum = (a + b + c) - mx; n   if (sum * 2 <= mx) n     cout << sum << endl; n   else n     cout << (a + b + c) / 3 << endl; n } n