#include <bits/stdc++.h>   using namespace std;   template <typename T>   struct Segtree {     int N;     vector<T> seg, lazy;     Segtree(int _n) : N(_n) {       seg.resize(4 * N, 0);       lazy.resize(4 * N, 0);     }     T join(T x, T y) { return max(x, y); }     void push(int x, int s, int e) {       if (!lazy[x]) return;       seg[x] = max(seg[x], lazy[x]);       if (s != e) {         lazy[2 * x] = max(lazy[2 * x], lazy[x]);         lazy[2 * x + 1] = max(lazy[2 * x + 1], lazy[x]);       }       lazy[x] = 0;     }     void build(int x, int s, int e) {       if (s == e) {         seg[x] = 0;         return;       }       int mid = (s + e) >> 1;       build(2 * x, s, mid);       build(2 * x + 1, mid + 1, e);       seg[x] = join(seg[2 * x], seg[2 * x + 1]);     }     void update(int x, int s, int e, int l, int r, T val) {       push(x, s, e);       if (s > r || e < l) return;       if (s >= l && e <= r) {         lazy[x] = val;         push(x, s, e);         return;       }       int mid = (s + e) >> 1;       update(2 * x, s, mid, l, r, val);       update(2 * x + 1, mid + 1, e, l, r, val);       seg[x] = join(seg[2 * x], seg[2 * x + 1]);     }     T query(int x, int s, int e, int l, int r) {       push(x, s, e);       if (e < l || s > r) return 0;       if (s >= l && e <= r) {         return seg[x];       }       int mid = (s + e) >> 1;       return join(query(2 * x, s, mid, l, r), query(2 * x + 1, mid + 1, e, l, r));     }     void update(int l, int r, T val) { update(1, 0, N - 1, l, r, val); }     T query(int l, int r) { return query(1, 0, N - 1, l, r); }   };   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, q;     cin >> n >> q;     vector<int> x(q), y(q), d(q), cc(1, 0);     for (int i = 0; i < q; ++i) {       char c;       cin >> x[i] >> y[i] >> c;       d[i] = (c ==  U );       cc.push_back(x[i]);       cc.push_back(y[i]);       cc.push_back(n + 1 - x[i]);       cc.push_back(n + 1 - y[i]);     }     sort(cc.begin(), cc.end());     cc.erase(unique(cc.begin(), cc.end()), cc.end());     Segtree<int> left(cc.size() + 1), up(cc.size() + 1);     for (int i = 0; i < q; ++i) {       x[i] = lower_bound(cc.begin(), cc.end(), x[i]) - cc.begin();       y[i] = lower_bound(cc.begin(), cc.end(), y[i]) - cc.begin();       if (d[i]) {         int id = up.query(y[i], y[i]);         up.update(y[i], y[i], y[i]);         cout << cc[y[i]] - cc[id] <<   n ;         id = lower_bound(cc.begin(), cc.end(), n + 1 - cc[id]) - cc.begin();         y[i] = lower_bound(cc.begin(), cc.end(), n + 1 - cc[y[i]]) - cc.begin();         left.update(y[i], id, x[i]);       } else {         int id = left.query(x[i], x[i]);         left.update(x[i], x[i], x[i]);         cout << cc[x[i]] - cc[id] <<   n ;         id = lower_bound(cc.begin(), cc.end(), n + 1 - cc[id]) - cc.begin();         x[i] = lower_bound(cc.begin(), cc.end(), n + 1 - cc[x[i]]) - cc.begin();         up.update(x[i], id, y[i]);       }     }   }


#include <bits/stdc++.h>   using namespace std;   void addmod(int &a, long long b) {     a = (a + b);     if (a >= 1000000007) a -= 1000000007;   }   void mulmod(int &a, long long b) { a = (a * b) % 1000000007; }   template <class T>   bool domin(T &a, const T &b) {     return a > b ? a = b, 1 : 0;   }   template <class T>   bool domax(T &a, const T &b) {     return a < b ? a = b, 1 : 0;   }   int gi() {     int a;     scanf( %d , &a);     return a;   }   long long gll() {     long long a;     scanf( %lld , &a);     return a;   }   int n;   namespace treap {   struct treap {     int val, size, lazy;     long long rnd;     treap *l, *r;     treap()         : val(0),           size(0),           lazy(0),           rnd((((long long)rand()) << 32) | rand()),           l(NULL),           r(NULL) {}     treap(int val)         : val(val),           size(1),           lazy(0),           rnd((((long long)rand()) << 32) | rand()),           l(NULL),           r(NULL) {}     void push() {       if (lazy == 0) return;       val += lazy;       if (l != NULL) l->lazy += lazy;       if (r != NULL) r->lazy += lazy;       lazy = 0;     }     void updateSz() {       size = 1;       if (l != NULL) size += l->size;       if (r != NULL) size += r->size;     }   };   typedef treap *ptreap;   ptreap root = NULL;   void split(ptreap tp, int key, ptreap &l, ptreap &r) {     if (tp == NULL) {       l = NULL, r = NULL;       return;     }     tp->push();     if (tp->val < key)       l = tp, split(tp->r, key, tp->r, r);     else       r = tp, split(tp->l, key, l, tp->l);     if (l != NULL) l->updateSz();     if (r != NULL) r->updateSz();   }   pair<ptreap, ptreap> split(int v) {     ptreap l, r;     split(root, v, l, r);     return {l, r};   }   pair<ptreap, ptreap> split(ptreap tp, int v) {     ptreap l, r;     split(tp, v, l, r);     return {l, r};   }   void insert(ptreap &tp, ptreap v) {     if (tp == NULL) {       tp = v;       return;     }     tp->push();     if (v->rnd < tp->rnd)       insert(v->val > tp->val ? tp->r : tp->l, v);     else       split(tp, v->val, v->l, v->r), tp = v;     tp->updateSz();   }   void insert(int v) {     ptreap tp = new treap(v);     insert(root, tp);   }   void merge(ptreap &t, ptreap l, ptreap r) {     if (l != NULL) l->push();     if (r != NULL) r->push();     if (l == NULL || r == NULL)       t = (l == NULL ? r : l);     else if (l->rnd > r->rnd)       t = l, merge(t->r, l->r, r);     else       t = r, merge(t->l, l, r->l);     if (t != NULL) t->updateSz();   }   void erase(ptreap &tp, int v) {     if (tp == NULL) return;     tp->push();     if (v == tp->val) {       merge(tp, tp->l, tp->r);     } else {       erase(v < tp->val ? tp->l : tp->r, v);       tp->updateSz();     }   }   int getmin(ptreap tp) {     if (tp == NULL) return 0;     tp->push();     return tp->l == NULL ? tp->val : getmin(tp->l);   }   int getmax(ptreap tp) {     if (tp == NULL) return 0;     tp->push();     return tp->r == NULL ? tp->val : getmax(tp->r);   }   void recalc(ptreap tp) {     if (tp == NULL) return;     tp->push();     recalc(tp->l);     recalc(tp->r);     tp->updateSz();   }   void debug(ptreap &tp) {     if (tp == NULL) return;     tp->push();     if (tp->l != NULL) debug(tp->l);     cout <<        << tp->val <<  :  << tp->rnd <<  :  << tp->size << endl;     if (tp->r != NULL) debug(tp->r);   }   }  // namespace treap   int main() {     n = gi();     for (int i = 0; i < n; i++) {       int l = gi(), r = gi();       auto v = treap::split(l);       auto v2 = treap::split(v.second, r);       if (v2.first != NULL) v2.first->lazy++;       if (v2.second != NULL) {         auto v = treap::getmin(v2.second);         treap::erase(v2.second, v);       }       treap::merge(treap::root, v.first, v2.first);       treap::merge(treap::root, treap::root, v2.second);       treap::insert(l);     }     cout << treap::root->size << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 10;   int l = 1, r = 0;   int a[MAXN], b[MAXN], now[MAXN], num[MAXN << 1], cnt[MAXN], ans[MAXN];   map<int, int> ms;   struct Change {     int p, x, y;   } c[MAXN];   struct Query {     int l, r, t, id;     bool operator<(const Query &A) const {       if (b[l] == b[A.l]) return b[r] == b[A.r] ? t < A.t : b[r] < b[A.r];       return b[l] < b[A.l];     }   } q[MAXN];   void update(int x, int d) {     int &y = num[x];     if (y > 0) cnt[y]--;     y += d;     if (y > 0) cnt[y]++;   }   void modify(int p, int d) {     if (l <= p && p <= r) {       update(a[p], -1);       update(d, 1);     }     a[p] = d;   }   int get() {     for (int i = 1;; i++) {       if (!cnt[i]) return i;     }   }   int main() {     int n, m, id = 0, Time = 0, sz = 0;     scanf( %d%d , &n, &m);     int B = 2000;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       int &t = ms[a[i]];       if (!t) t = ++sz;       now[i] = a[i] = t;       b[i] = i / B + 1;     }     while (m--) {       int op, x, y;       scanf( %d%d%d , &op, &x, &y);       if (op == 1)         q[++id] = (Query){x, y, Time, id};       else {         int &t = ms[y];         if (!t) t = ++sz;         c[++Time] = (Change){x, y = t, now[x]};         now[x] = y;       }     }     sort(q + 1, q + id + 1);     int v = 0;     for (int i = 1; i <= id; i++) {       while (v < q[i].t) modify(c[v + 1].p, c[v + 1].x), v++;       while (v > q[i].t) modify(c[v].p, c[v].y), v--;       while (l < q[i].l) update(a[l], -1), l++;       while (l > q[i].l) update(a[l - 1], 1), l--;       while (r < q[i].r) update(a[r + 1], 1), r++;       while (r > q[i].r) update(a[r], -1), r--;       ans[q[i].id] = get();     }     for (int i = 1; i <= id; i++) {       printf( %d n , ans[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long double PI = 4.0 * atan(1.0);   void EnchomSolve() {     int l;     long double L;     long double ans = 0.0;     long double area;     long double height;     long double help, help2;     scanf( %d , &l);     L = (long double)l;     help = L / sqrt(3.0);     height = sqrt(L * L - help * help);     help = (sqrt(3.0) / 2.0) * L;     area = (help * L) / 2.0;     ans += area * height / 3.0;     scanf( %d , &l);     L = (long double)l;     help = (sqrt(2.0) / 2.0) * L;     height = sqrt(L * L - help * help);     area = L * L;     ans += area * height / 3.0;     scanf( %d , &l);     L = (long double)l;     help = L / (sqrt(2.0 - 2.0 * cos(72.0 * PI / 180.0)));     height = sqrt(L * L - help * help);     help2 = sqrt(help * help - (L / 2.0) * (L / 2.0));     area = L * help2 / 2.0;     area *= 5.0;     ans += area * height / 3.0;     printf( %.10f n , (double)ans);     return;   }   int main() {     EnchomSolve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c;     cin >> a >> b >> c;     int x = min(a, b / 2);     int y = min(x, c / 4);     cout << y * 7;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct EDGE {     int to, next, w, co;   } e[100000];   int head[100], top, vis[100000];   void add(int u, int v, int w, int co) {     e[top].to = v;     e[top].w = w;     e[top].co = co;     e[top].next = head[u];     head[u] = top++;   }   struct _ {     int max_flow, co;   };   int dis[100], vvis[100], prep[100], pree[100];   queue<int> q;   deque<int> node;   void spfa(int s) {     memset(dis, 63, sizeof(dis));     memset(vvis, 0, sizeof(vvis));     dis[s] = 0;     q.push(s);     int i;     while (!q.empty()) {       s = q.front();       q.pop();       vvis[s] = 0;       int i;       for (i = head[s]; ~i; i = e[i].next) {         if (!e[i].w) continue;         if (dis[s] + e[i].co < dis[e[i].to]) {           dis[e[i].to] = dis[s] + e[i].co;           pree[e[i].to] = i;           prep[e[i].to] = s;           if (vvis[e[i].to]) continue;           vvis[e[i].to] = 1;           q.push(e[i].to);         }       }     }   }   _ dfs(int s, int t, int rest) {     int flow = 998244353, co = 0;     int temp = t;     _ rv;     while (t != s) {       flow = min(flow, e[pree[t]].w);       co += e[pree[t]].co;       t = prep[t];     }     if (co > 0) flow = min(flow, rest / co);     if (flow == 0) return rv = (_){0, 0};     t = temp;     while (t != s) {       temp = pree[t];       e[temp].w -= flow;       e[temp ^ 1].w += flow;       t = prep[t];     }     rv = (_){flow, co};     return rv;   }   long long EK(int s, int t, int rest) {     long long sum = 0;     _ _;     while (1) {       spfa(s);       if (dis[t] > 998244353) break;       _ = dfs(s, t, rest);       if (_.max_flow == 0) break;       sum += _.max_flow;       rest -= _.max_flow * _.co;     }     return sum;   }   int main() {     memset(head, 255, sizeof(head));     int n, k;     scanf( %d%d , &n, &k);     int i, j, t;     for (i = 1; i <= n; i++) {       for (j = 1; j <= n; j++) {         scanf( %d , &t);         if (t) {           add(i, j, t, 0);           add(j, i, 0, 0);           add(i, j, k, 1);           add(j, i, 0, -1);         }       }     }     long long ans = EK(1, n, k);     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct station {     int pos, price;   } a[200003];   int T[800008], d, n, m;   long long cnt;   bool comp(station A, station B) { return (A.pos < B.pos); }   void Build_MinIT(int, int, int);   int Find_Min(int, int, int, int, int);   int main() {     scanf( %d %d %d , &d, &n, &m);     for (int i = 1; i <= m; i++) scanf( %d %d , &a[i].pos, &a[i].price);     sort(a + 1, a + 1 + m, comp);     a[++m].pos = d;     for (int i = 1; i <= m; i++) {       if (a[i].pos - a[i - 1].pos > n) {         cout << -1;         return 0;       }     }     if (n >= d) {       cout << 0;       return 0;     }     a[200002].price = 1E+9;     Build_MinIT(1, m, 1);     int i = 0, j = 1, t = n;     while (j <= m) {       while (a[i].price < a[j].price && a[j].pos - a[i].pos <= n && j <= m) j++;       if (a[j].pos - a[i].pos <= n) {         cnt += (long long)(a[j].pos - (a[i].pos + t)) * a[i].price;         t = 0;         i = j++;       } else {         int k = Find_Min(i + 1, j - 1, 1, m, 1);         cnt += (long long)(n - t) * a[i].price;         t = a[i].pos + n - a[k].pos;         i = k;       }     }     cout << cnt;     return 0;   }   void Build_MinIT(int L, int R, int v) {     if (L == R) {       T[v] = L;       return;     }     int m = (L + R) / 2;     Build_MinIT(L, m, v * 2);     Build_MinIT(m + 1, R, v * 2 + 1);     T[v] = (a[T[v * 2]].price < a[T[v * 2 + 1]].price ? T[v * 2] : T[v * 2 + 1]);   }   int Find_Min(int l, int r, int L, int R, int v) {     if (l > r) return 200002;     if (l == L && r == R) return T[v];     int m = (L + R) / 2;     int x = Find_Min(l, min(r, m), L, m, v * 2),         y = Find_Min(max(l, m + 1), r, m + 1, R, v * 2 + 1);     return (a[x].price < a[y].price ? x : y);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     double lo = 0.0, hi = 10000000.0;     for (int i = 1; i <= n; i++) {       int sta;       cin >> sta;       lo = max(lo, (double)10.0 * sta / i);       hi = min(hi, (double)10.0 * (sta + 1) / i);     }     int nextlo = floor(lo * (n + 1) / 10.0);     int nexthi = ceil(hi * (n + 1) / 10.0);     if (nextlo + 1 >= nexthi) {       cout <<  unique  << endl << nextlo << endl;     } else {       cout <<  not unique  << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /stack:102400000,102400000 )   using namespace std;   int lowbit(int x) { return (x & -x); }   template <class T_>   T_ f_abs(T_ x) {     return x < 0 ? -x : x;   }   template <class T_>   T_ f_max(T_ a, T_ b) {     return a > b ? a : b;   }   template <class T_>   T_ f_min(T_ a, T_ b) {     return a < b ? a : b;   }   template <class T_>   void chkmax(T_ &a, T_ b) {     if (a == -1 || a < b) a = b;   }   template <class T_>   void chkmin(T_ &a, T_ b) {     if (a == -1 || a > b) a = b;   }   template <class T_>   T_ gcd(T_ a, T_ b) {     while (T_ t = a % b) a = b, b = t;     return b;   }   template <class T_>   void f_swap(T_ &a, T_ &b) {     T_ t = a;     a = b;     b = t;   }   const double eps = 1e-9;   const int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1},             dy[] = {0, 0, -1, 1, -1, 1, -1, 1};   const int inf = 0x7fffffff;   const int mod = 1000000007;   const int N = 100003;   const int MAXV = 100003;   int n, m, cnt, who[N], dsu[N];   bool vis[N], tree[N << 2];   vector<pair<int, int> > vt[N], qu[N];   struct Oper {     int tp, x, y;     bool tag;     Oper() {}     Oper(int tp_, int x_, int y_) {       tp = tp_;       x = x_;       y = y_;     }   } op[N];   struct Edge {     int to, next;   } edg[MAXV << 1];   int total, idx, root, first[MAXV];   int siz[MAXV];   int dep[MAXV];   int top[MAXV];   int fa[MAXV];   int son[MAXV];   int id[MAXV];   void Dfs_1(int u) {     int e, v, i;     siz[u] = 1;     son[u] = 0;     for (e = 0; e < (int)vt[u].size(); ++e) {       v = vt[u][e].first;       i = vt[u][e].second;       op[i].tag = !vis[v];     }     vis[u] = true;     for (e = first[u]; ~e; e = edg[e].next) {       v = edg[e].to;       if (v != fa[u]) {         fa[v] = u;         dep[v] = dep[u] + 1;         Dfs_1(v);         if (son[u] == 0 || siz[v] > siz[son[u]]) son[u] = v;         siz[u] += siz[v];       }     }     for (e = 0; e < (int)vt[u].size(); ++e) {       v = vt[u][e].first;       i = vt[u][e].second;       if (op[i].tag && !vis[v]) op[i].tag = false;     }   }   void Dfs_2(int u, int tp) {     id[u] = ++idx;     top[u] = tp;     if (son[u] != 0) Dfs_2(son[u], top[u]);     for (int e = first[u]; ~e; e = edg[e].next) {       int v = edg[e].to;       if (v != son[u] && v != fa[u]) Dfs_2(v, v);     }   }   void BuildChain() {     Dfs_1(root);     Dfs_2(root, root);   }   void Build(int l, int r, int root) {     tree[root] = false;     if (l == r) return;     int mid = ((l + r) >> 1);     Build(l, mid, (root << 1));     Build(mid + 1, r, (root << 1 | 1));   }   void Update(int p, int l, int r, int root) {     if (l == r) {       tree[root] = true;       return;     }     int mid = ((l + r) >> 1);     if (p <= mid)       Update(p, l, mid, (root << 1));     else       Update(p, mid + 1, r, (root << 1 | 1));     tree[root] = (tree[(root << 1)] && tree[(root << 1 | 1)]);   }   bool Query(int L, int R, int l, int r, int root) {     if (L <= l && r <= R) return tree[root];     int mid = ((l + r) >> 1);     bool res = true;     if (L <= mid) res = (res && Query(L, R, l, mid, (root << 1)));     if (R > mid) res = (res && Query(L, R, mid + 1, r, (root << 1 | 1)));     return res;   }   bool FindChain(int u, int v) {     int u_top = top[u], v_top = top[v], L, R;     while (u_top != v_top) {       if (dep[u_top] < dep[v_top]) {         f_swap(u_top, v_top);         f_swap(u, v);       }       L = id[u_top];       R = id[u];       if (!Query(L, R, 1, idx, 1)) return false;       u = fa[u_top];       u_top = top[u];     }     if (u != v) {       if (dep[u] > dep[v]) f_swap(u, v);       L = id[son[u]];       R = id[v];       if (!Query(L, R, 1, idx, 1)) return false;     }     return true;   }   void addedge(int from, int to) {     edg[total].to = to;     edg[total].next = first[from];     first[from] = total++;   }   void Init() {     cnt = 0;     root = n + 1;     total = idx = fa[root] = dep[root] = 0;     for (int i = 1; i <= n + 1; ++i) {       first[i] = -1;       dsu[i] = i;       vt[i].clear();       vis[i] = false;     }   }   void GetData() {     int tp, x, y;     for (int i = 0; i < m; ++i) {       scanf( %d%d , &tp, &x);       if (tp != 2) scanf( %d , &y);       if (tp == 1) {         dsu[x] = y;         addedge(y, x);       } else if (tp == 2) {         y = -1;         who[++cnt] = x;         qu[cnt].clear();       } else {         vt[x].push_back(make_pair(who[y], i));         qu[y].push_back(make_pair(x, i));       }       op[i] = Oper(tp, x, y);     }   }   void Solve() {     int i, j, tp, p;     bool flag;     for (i = 1; i <= n; ++i) {       if (dsu[i] == i) addedge(n + 1, i);     }     BuildChain();     Build(1, idx, 1);     for (i = cnt = 0; i < m; ++i) {       tp = op[i].tp;       if (tp == 1)         Update(id[op[i].x], 1, idx, 1);       else if (tp == 2) {         ++cnt;         for (j = 0; j < (int)qu[cnt].size(); ++j) {           p = qu[cnt][j].second;           if (op[p].tag)             flag = FindChain(who[cnt], qu[cnt][j].first);           else             flag = false;           vis[p] = flag;         }       }     }     for (i = 0; i < m; ++i) {       if (op[i].tp == 3) puts(vis[i] ?  YES  :  NO );     }   }   int main() {     while (~scanf( %d%d , &n, &m)) {       Init();       GetData();       Solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[111];   int main() {     int n;     scanf( %d , &n);     arr[0] = 0;     for (int i = (1); i < (n + 1); ++i) scanf( %d , &arr[i]);     int ans = 0;     if (n % 2 == 0 || n == 1) {       printf( -1 n );       return 0;     }     for (int i = n; i >= 1; i -= 2) {       int tmp = max(arr[i], arr[i - 1]);       ans += tmp;       arr[i] = arr[i - 1] = 0;       arr[(i - 1) / 2] = max(0, arr[(i - 1) / 2] - tmp);     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace {};   int sgn(int x) { return x >= 0 ? 1 : -1; }   int arr[200001];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) {       cin >> arr[i];       arr[i] = sgn(arr[i]);       if (arr[i] == -1) k--;     }     if (k < 0) {       cout <<  -1 n ;       return 0;     }     vector<pair<int, int>> segs;     int flips = 0;     int curr = 1, c = 0;     for (int i = 0; i < n; i++) {       if (arr[i] == curr)         c++;       else {         flips++;         if (curr == 1 && i - c > 0) segs.emplace_back(c, -2);         c = 1;         curr = arr[i];       }     }     for (auto seg : segs) {       ;     }     sort(segs.begin(), segs.end());     ;     for (auto seg : segs) {       if (seg.first > k) break;       k -= seg.first;       flips += seg.second;     }     if (curr == 1 && c > 0) {       if (k >= c) flips--;     }     if (flips < 0) flips = 0;     cout << flips <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct info {     long long fs, sm, id;   } bd[100001];   int cmp(info a, info b) { return a.fs < b.fs; }   int main() {     ios_base::sync_with_stdio(0);     long long int i, j, x, n, cnt = 0, m, y, k, ans = 0, cf, cm, a, rr1 = 0, l, r,                               md, r1, r2, xxx, flg;     cin >> n >> a >> cf >> cm >> m;     long long in[n + 1], bs[n + 1];     for (i = 1; i <= n; i++) {       cin >> in[i];       bd[i].fs = a - in[i];       bd[i].id = i;       bs[i] = a - in[i];     }     sort(bd + 1, bd + 1 + n, cmp);     sort(bs + 1, bs + 1 + n);     for (i = 2; i <= n; i++) {       bd[i].fs += bd[i - 1].fs;     }     bd[n].sm = 0;     for (i = n - 1; i >= 1; i--) {       x = abs(in[bd[i + 1].id] - in[bd[i].id]);       bd[i].sm = bd[i + 1].sm + x * (n - i);     }     bd[0].fs = bd[0].id = 0;     bd[0].sm = (a - in[bd[1].id]) * n + bd[1].sm;     for (i = 1; i <= n; i++) {       if (bs[i] > 0) break;       cnt++;     }     ans = cnt * cf + cm * in[bd[n].id];     for (i = n; i >= 0; i--) {       x = m;       y = cnt * cf;       rr1 = cnt;       flg = 0;       if (x >= bd[i].fs) {         flg = 1;         y = i * cf;         x -= bd[i].fs;         rr1 = i;       }       l = i;       r = n;       while (r - l > 1) {         md = (l + r) / 2;         if (bd[md].sm > x)           l = md;         else           r = md;       }       if (bd[l].sm <= x && i != 0) r = l;       x -= bd[r].sm;       if (r == i && flg) {         if (i == n) {           y = a * cm + n * cf;           if (ans <= y) {             ans = y;             r1 = n;             r2 = n + 1;           }         } else {           y += cm * (min(a, in[bd[r].id] + x / (n - r)));           if (ans <= y) {             ans = y;             r1 = i;             xxx = min(a, in[bd[r].id] + x / (n - r));             r2 = i + 1;           }         }         continue;       }       if (r - 1 > 0)         y += cm * (min(a, min(in[bd[r - 1].id], in[bd[r].id] + x / (n - r + 1))));       else         y += cm * (min(a, in[bd[r].id] + x / (n - r + 1)));       if (y >= ans) {         ans = y;         r1 = rr1;         r2 = r;         if (r - 1 > 0)           xxx = min(a, min(in[bd[r - 1].id], in[bd[r].id] + x / (n - r + 1)));         else           xxx = min(a, in[bd[r].id] + x / (n - r + 1));       }     }     for (i = 1; i <= r1; i++) {       in[bd[i].id] = a;     }     for (i = r2; i <= n; i++) {       in[bd[i].id] = xxx;     }     cout << ans << endl;     for (i = 1; i <= n; i++) cout << in[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<long long int> a(n);       vector<long long int> b(n);       for (int i = 0; i < n; i++) {         cin >> a[i];       }       for (int i = 0; i < n; i++) {         cin >> b[i];       }       long long int cnt = 0;       long long int min_a = *min_element(a.begin(), a.end());       long long int min_b = *min_element(b.begin(), b.end());       for (int i = 0; i < n; i++) {         if (a[i] > min_a && b[i] > min_b) {           long long int num;           num = min(a[i] - min_a, b[i] - min_b);           cnt += num;           a[i] -= num;           b[i] -= num;         }       }       for (int i = 0; i < n; i++) {         cnt += (a[i] - min_a);         cnt += (b[i] - min_b);       }       cout << cnt << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename A, typename B>   string to_string(pair<A, B> p);   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> p);   template <typename A, typename B, typename C, typename D>   string to_string(tuple<A, B, C, D> p);   string to_string(const string& s) { return     + s +    ; }   string to_string(const char* s) { return to_string((string)s); }   string to_string(bool b) { return (b ?  true  :  false ); }   string to_string(vector<bool> v) {     bool first = true;     string res =  { ;     for (int i = 0; i < static_cast<int>(v.size()); i++) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(v[i]);     }     res +=  } ;     return res;   }   template <size_t N>   string to_string(bitset<N> v) {     string res =   ;     for (size_t i = 0; i < N; i++) {       res += static_cast<char>( 0  + v[i]);     }     return res;   }   template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto& x : v) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A, typename B, typename C>   string to_string(tuple<A, B, C> p) {     return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   +            to_string(get<2>(p)) +  ) ;   }   template <typename A, typename B, typename C, typename D>   string to_string(tuple<A, B, C, D> p) {     return  (  + to_string(get<0>(p)) +  ,   + to_string(get<1>(p)) +  ,   +            to_string(get<2>(p)) +  ,   + to_string(get<3>(p)) +  ) ;   }   void debug_out() { cerr << endl; }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     debug_out(T...);   }   class dsu {    public:     vector<int> p;     int n;     dsu(int _n) : n(_n) {       p.resize(n);       iota(p.begin(), p.end(), 0);     }     inline int get(int x) { return (x == p[x] ? x : (p[x] = get(p[x]))); }     inline bool unite(int x, int y) {       x = get(x);       y = get(y);       if (x != y) {         p[x] = y;         return true;       }       return false;     }   };   const int MAX = 100010;   int bound[MAX];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int tt;     cin >> tt;     assert(1 <= tt && tt <= 1000);     int x = 0;     while (tt--) {       int n;       cin >> n;       if (n == 0) {         continue;       }       assert(n >= 1);       vector<int> v(n);       for (int i = 0; i < n; i++) {         cin >> v[i];       }       sort(v.begin(), v.end());       v.resize(unique(v.begin(), v.end()) - v.begin());       n = (int)v.size();       for (int i = 0; i < n - 1; i++) {         assert(v[i] < v[i + 1]);       }       assert(0 <= v[0]);       assert(v[n - 1] <= 100000);       {         int ptr = -1;         for (int val = 0; val < MAX; val++) {           if (ptr + 1 < n && v[ptr + 1] == val) {             ++ptr;           }           bound[val] = ptr;         }       }       vector<vector<int>> who(1, vector<int>(n, -1));       vector<dsu> d(1, dsu(n + 1));       vector<bool> seen;       for (int i = n - 1; i >= 0; i--) {         int start = 0;         int j = 0;         while (start < i) {           if (j == (int)who.size()) {             who.emplace_back(n, -1);             d.emplace_back(n + 1);           }           int finish = i - 1;           int tmp = who[j][i];           if (tmp != -1) {             int val = v[i] - (v[tmp] - v[i]);             finish = (val < 0 ? -1 : bound[val]);           }           if (start <= finish) {             int pos = d[j].get(start);             while (pos <= finish) {               who[j][pos] = i;               d[j].unite(pos, pos + 1);               pos = d[j].get(pos);             }             start = finish + 1;           }           ++j;         }         int g = 0;         while (g < (int)who.size() && who[g][i] != -1) {           ++g;         }         if (g >= (int)seen.size()) {           seen.resize(g + 1);         }         seen[g] = true;       }       int g = 0;       while (g < (int)seen.size() && seen[g]) {         ++g;       }       x ^= g;       42;     }     cout << (x > 0 ?  YES  :  NO ) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   long long n, num;   long long a[maxn];   long long vis[maxn];   priority_queue<long long> pq;   void fnd(long long x) {     if (vis[a[x]] == 0) {       vis[a[x]] = 1;       num++;       fnd(a[x]);     } else       return;   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     memset(vis, 0, sizeof(vis));     for (int i = 1; i <= n; i++) {       num = 1;       if (vis[i] == 0) {         vis[i] = 1;         fnd(i);         pq.push(num);       }     }     long long t = pq.top();     pq.pop();     if (!pq.size()) {       cout << t * t << endl;     } else {       t += pq.top();       pq.pop();       long long ans = t * t;       while (pq.size()) {         t = pq.top();         pq.pop();         ans += t * t;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 1e5 + 5;   vector<int> adj[maxN];   int s[maxN], par[maxN];   void dfs(int n, int p) {     par[n] = p;     for (int i = int(0); i < int((int)adj[n].size()); i++) {       if (adj[n][i] != p) dfs(adj[n][i], n);     }   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cin.tie(0), cout.tie(0),         cout.precision(15);     int n, tmp;     cin >> n;     for (int i = int(0); i < int(n - 1); i++) {       cin >> tmp;       tmp -= 1;       adj[tmp].push_back(i + 1);     }     dfs(0, -1);     for (int i = int(0); i < int(n); i++) {       cin >> s[i];     }     long long ans = s[0];     for (int i = int(0); i < int(n); i++) {       if (s[i] == -1) {         int n = i, tt = s[par[i]];         int mini = 2000000000;         for (int j = int(0); j < int((int)adj[n].size()); j++) {           if (s[adj[n][j]] < tt) {             cout << -1;             return 0;           }           mini = min(mini, s[adj[n][j]] - tt);         }         if (mini != 2000000000) ans += mini;         for (int j = int(0); j < int((int)adj[n].size()); j++) {           ans += s[adj[n][j]] - tt - mini;         }       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000001;   const long double EPS = 1e-9;   const int MOD = 1000000007;   const long long LLINF = 1000000000000000001;   const int maxn = 1042;   int n;   int a[maxn];   char ans[maxn][maxn];   bool good[maxn];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       for (int j = 1; j <= n; j++) ans[i][j] =  . ;     }     bool none = true;     for (int i = 1; i <= n; i++)       if (a[i] != i) none = false;     if (none)       cout << n <<   n ;     else {       cout << n - 1 <<   n ;       int mostleft = 1;       while (a[mostleft] == mostleft) {         good[mostleft] = true;         mostleft++;       }       int row = 1;       int z = mostleft;       good[z] = true;       z = a[z];       while (!good[z]) {         good[z] = true;         char mirror = (z < a[z]) ?  /  :     ;         ans[row][z] = mirror;         ans[row][a[z]] = mirror;         row++;         z = a[z];       }       good[z] = true;       for (int i = mostleft + 1; i <= n; i++) {         if (a[i] != i && !good[i]) {           int firstrow = row;           z = i;           while (a[z] > z) z = a[z];           z = a[z];           good[z] = true;           row++;           z = a[z];           while (!good[z]) {             good[z] = true;             char mirror = (z < a[z]) ?  /  :     ;             ans[row][z] = mirror;             ans[row][a[z]] = mirror;             row++;             z = a[z];           }           ans[row - 1][mostleft] =     ;           ans[firstrow][mostleft] =  / ;           ans[firstrow][a[z]] =  / ;         }       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) cout << ans[i][j];       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int h[n];     for (int i = 0; i < n; i++) cin >> h[i];     int max;     int a[n];     a[n - 1] = 0;     max = h[n - 1];     for (int i = n - 2; i >= 0; i--) {       if (h[i] > max) {         a[i] = 0;         max = h[i];       } else {         a[i] = max - h[i] + 1;       }     }     for (int i = 0; i < n; i++) {       printf( %d  , a[i]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int x = 0, f = 1;     char c;     do c = getchar(), f = c ==  -  ? -1 : f;     while (!isdigit(c));     do x = (x << 3) + (x << 1) + c -  0 , c = getchar();     while (isdigit(c));     return x * f;   }   struct Point {     int x, y, px, py, id;     Point() {}     Point(int x, int y, int px, int py) : x(x), y(y), px(px), py(py) {}   } a[55], ans[100020];   bool tab[55];   int b[55][55], d[55][55];   int n, m, cnt, tmp = 1, num;   inline void Move(Point &a, int x, int y) {     assert(!b[a.x + x][a.y + y]);     b[a.x + x][a.y + y] = b[a.x][a.y];     b[a.x][a.y] = 0;     ans[++cnt] = Point(a.x, a.y, a.x + x, a.y + y);     a.x += x;     a.y += y;   }   inline void Move_to(Point &a, int pos) {     Move(a, 1, 0);     while (a.y < pos) Move(a, 0, 1);     while (a.y > pos) Move(a, 0, -1);     Move(a, -1, 0);   }   inline void Swap(Point &a, Point &b) {     if (a.id == b.id) return;     if (n == 2) {       if (a.y < b.y)         Move(a, 1, 0), Move(b, 0, -1), Move(a, 0, 1), Move(a, -1, 0);       else         Move(a, 1, 0), Move(b, 0, 1), Move(a, 0, -1), Move(a, -1, 0);       return;     }     Move(a, 1, 0);     Move(b, 1, 0);     Move(b, 1, 0);     int t = a.y;     while (a.y > b.y) Move(a, 0, -1);     while (a.y < b.y) Move(a, 0, 1);     while (b.y > t) Move(b, 0, -1);     while (b.y < t) Move(b, 0, 1);     Move(a, -1, 0);     Move(b, -1, 0);     Move(b, -1, 0);   }   inline void Move1(Point &a, int x) {     while (a.x <= tmp) Move(a, 1, 0);     while (a.y > x) Move(a, 0, -1);     while (a.y < x) Move(a, 0, 1);     while (!b[a.x - 1][a.y]) Move(a, -1, 0);     tmp = max(tmp, a.x);   }   inline void Move2(Point &a, int y) {     while (a.x < y) Move(a, 1, 0);   }   int main() {     n = read();     m = read();     if (n == 1) return printf( 0 ), 0;     assert(m <= n);     for (int i = 1; i <= m; i++) {       a[i].x = read();       a[i].y = read();       a[i].id = b[a[i].x][a[i].y] = i;     }     for (int i = 1; i <= m; i++) {       a[i].px = read();       a[i].py = read();       d[a[i].px][a[i].py] = i;     }     for (int i = 0; i <= n + 1; i++)       b[i][0] = b[0][i] = b[n + 1][i] = b[i][n + 1] = 2147483647;     for (int i = 1; i <= n; i++)       for (int j = n; j >= 1; j--) {         if (!b[i][j]) continue;         int t = b[i][j];         while (!b[i][a[t].y + 1]) Move(a[t], 0, 1);       }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) {         if (!b[i][j]) continue;         num++;         int t = b[i][j];         assert(a[t].y >= num);         while (a[t].y > num) Move(a[t], 0, -1);         while (a[t].x > 1) Move(a[t], -1, 0);       }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (d[i][j] && !tab[j]) {           tab[j] = true;           if (b[1][j])             Swap(a[b[1][j]], a[d[i][j]]);           else             Move_to(a[d[i][j]], j);         }     for (int j = 1; j <= n; j++)       for (int i = 1; i <= n; i++)         if (d[i][j]) Move1(a[d[i][j]], j);     for (int i = n; i >= 1; i--)       for (int j = n; j >= 1; j--)         if (d[i][j]) Move2(a[d[i][j]], i);     printf( %d n , cnt);     for (int i = 1; i <= cnt; i++)       printf( %d %d %d %d n , ans[i].x, ans[i].y, ans[i].px, ans[i].py);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 50 + 5, maxL = (1 << 20) + 10;   int N, L;   char s[maxn][25];   bool vis[maxL];   long double F[maxL], R[25];   long long dask[maxL];   bool check(int tt, int k) {     if (dask[tt] & (1LL << k))       return false;     else       return true;   }   long double dfs(int tt, int g) {     if (g == 0) return 0.0;     if (vis[tt]) return F[tt];     long double ret = 0.0;     long double step = L - g + 1;     for (int i = 0; i < L; i++) {       if ((1 << i) & tt) continue;       int nt = (1 << i) | tt;       int wq = __builtin_popcountll(dask[nt] ^ dask[tt]);       ret += step * R[g] * wq;       ret += dfs(nt, g - 1);     }     vis[tt] = true;     F[tt] = ret;     return ret;   }   void Prepare() {     memset(dask, 0, sizeof(dask));     for (int i = (1), _i = (N); i <= _i; i++)       for (int j = (i + 1), _j = (N); j <= _j; j++) {         int same = 0;         for (int k = 0; k < L; k++)           if (s[i][k] == s[j][k]) same |= (1 << k);         dask[same] |= (1LL << j) | (1LL << i);       }     for (int i = (1 << L) - 1; i; i--)       for (int k = 0; k < L; k++)         if (i & (1 << k)) dask[i ^ (1 << k)] |= dask[i];   }   int main() {     for (; scanf( %d , &N) != EOF;) {       for (int i = (1), _i = (N); i <= _i; i++) scanf( %s , s[i]);       L = strlen(s[1]);       int gg = L;       R[L] = 1.0 / (long double)L;       for (int i = (L - 1), _i = (1); i >= _i; i--) {         gg--;         R[i] = R[i + 1] / (long double)gg;       }       memset(vis, 0, sizeof(vis));       long double ans = 0.0;       Prepare();       ans = dfs(0, L);       ans /= (long double)N;       printf( %.15lf n , (double)ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int tot, n;   int vis[110];   int p[110];   int dp[2][262144], cs[110][262144], pre[110][262144];   int have[100];   int a[110], b[110];   string print(int sta) {     string x =   ;     for (int i = tot - 1; i >= 0; i--) {       if (sta & (1 << i))         x +=  1 ;       else         x +=  0 ;     }     return x;   }   int main() {     for (int i = 2; i <= 59; i++) {       if (!vis[i]) p[tot++] = i;       for (int j = i; j <= 59; j += i) vis[j] = 1;     }     for (int i = 1; i <= 59; i++) {       for (int j = 0; j < tot; j++) {         if (i % p[j] == 0) have[i] |= (1 << j);       }     }     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     memset(dp, 0x3f, sizeof(dp));     dp[0][0] = 0;     register int tmp;     register int nowt = 1, pret = 0;     for (int i = 1; i <= n; i++) {       for (int sta = 0; sta < (1 << tot); sta++) {         dp[nowt][sta] = 0x3f3f3f3f;         for (int j = 1; j <= 59; j++) {           if ((have[j] & sta) != have[j]) continue;           tmp = dp[pret][have[j] ^ sta] + abs(j - a[i]);           if (tmp < dp[nowt][sta]) {             dp[nowt][sta] = tmp;             pre[i][sta] = have[j] ^ sta;             cs[i][sta] = j;           }         }       }       nowt ^= 1;       pret ^= 1;     }     int mi = 0x3f3f3f3f;     for (int i = 0; i < (1 << tot); i++) {       mi = min(mi, dp[pret][i]);     }     int pp;     for (int i = 0; i < (1 << tot); i++) {       if (mi == dp[pret][i]) {         pp = i;         for (int now = n; now >= 1; now--) {           b[now] = cs[now][pp];           pp = pre[now][pp];         }         for (int now = 1; now <= n; now++) {           printf( %d  , b[now]);         }         puts(  );         return 0;       }     }   }


#include <bits/stdc++.h>   using namespace std;   void read(long long a = 0) {     freopen( in ,  r , stdin);     if (a) freopen( out ,  w , stdout);   }   long long a[100009 * 10], b[100009 * 10];   long long n, m;   bool bs(long long num) {     bool vis[100009 * 10] = {};     long long space = 0;     for (int i = num; i >= 0; i--) {       if (a[i] && !vis[a[i]]) {         vis[a[i]] = 1;         space += b[a[i] - 1];       } else {         space--;         space = max(space, 0ll);       }     }     for (int i = 1; i <= m; i++)       if (!vis[i]) return 0;     if (space == 0)       return 1;     else       return 0;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) cin >> b[i];     long long lo = 0, hi = n + 1;     while (lo < hi) {       long long mid = (lo + hi - 1) >> 1;       if (bs(mid))         hi = mid;       else         lo = mid + 1;     }     if (hi == n + 1)       cout <<  -1 ;     else       cout << hi + 1;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 9;   const int N = 4040;   int n, w, b;   long long fac[N], f[N][N], inv[N];   long long qpow(long long x, long long p) {     long long res = 1ll;     while (p) {       if (p & 1) {         res = (res * x) % mod;       }       x = (x * x) % mod;       p >>= 1;     }     return res;   }   long long INV(long long k) { return qpow(k, mod - 2); }   long long calc(int x, int y) {     return fac[x] * inv[y] % mod * inv[x - y] % mod;   }   void init() {     fac[0] = 1ll;     inv[0] = 1ll;     for (int i = 1; i < N; i++) {       fac[i] = (fac[i - 1] * 1ll * i) % mod;       inv[i] = INV(fac[i]);     }   }   int main() {     scanf( %d%d%d , &n, &w, &b);     init();     for (int i = 1; i <= n; i++) {       for (int j = i; j < N; j++) {         f[i][j] = (fac[j] * calc(j - 1, i - 1)) % mod;       }     }     long long ans = 0;     for (int i = 1; i < n - 1; i++) {       ans =           (ans + f[n - i][w] * f[i][b] % mod * (1ll * n - 1ll * i - 1ll) % mod) %           mod;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long f(string s, long long m) {     map<char, long long> a;     map<char, long long>::iterator ii;     long long i;     for (i = 0; i < s.size(); i++) {       a[s[i]]++;     }     long long best = 0;     for (ii = a.begin(); ii != a.end(); ii++) best = max(best, ii->second);     long long p = s.size();     if (best == p && m == 1) return p - 1;     return min(p, best + m);   }   int main() {     string s1, s2, s3;     long long n;     cin >> n;     cin >> s1 >> s2 >> s3;     if (f(s1, n) > f(s2, n) && f(s1, n) > f(s3, n)) {       cout <<  Kuro ;       return 0;     }     if (f(s2, n) > f(s1, n) && f(s2, n) > f(s3, n)) {       cout <<  Shiro ;       return 0;     }     if (f(s3, n) > f(s1, n) && f(s3, n) > f(s2, n)) {       cout <<  Katie ;       return 0;     }     cout <<  Draw ;     return 0;   }


#include <bits/stdc++.h>   template <class T>   void rd(T &x) {     x = 0;     int f = 1;     char c;     while (!isdigit(c = getchar()))       if (c ==  - ) f = -1;     do {       x = (x << 3) + (x << 1) + (c ^ 48);     } while (isdigit(c = getchar()));     x *= f;   }   using namespace std;   const int N = 1e5 + 5;   int n, dp[N], s[N], p[N], cnt, tp;   map<int, int> Map[N];   int main() {     rd(n);     for (int i = 1; i <= n; i++) {       int v, c, l, r;       rd(v), rd(c), rd(l), rd(r);       if (r + c <= 1e5 && (!l || Map[l].find(r + c) != Map[l].end())) {         int t = Map[l][r + c];         dp[i] = dp[t] + v;         p[i] = t;         if (!r && dp[i] > dp[tp]) tp = i;         if (dp[i] > dp[Map[l + c][r]]) Map[l + c][r] = i;       }     }     while (tp) s[++cnt] = tp, tp = p[tp];     printf( %d n , cnt);     for (; cnt > 0; cnt--) printf( %d  , s[cnt]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, v1, v2, L;   bool can(double m) {     double bus = 0, nbus = 0;     for (int i = 0; i < (n + k - 1) / k; i++) {       if (i == 0)         bus += m * v2;       else         bus += m * v1;       nbus += m * v1;       if (bus > L) return false;       double sb = (bus - nbus) / (v1 + v2);       bus += sb * v1;       nbus += sb * v1;     }     return true;   }   int main() {     scanf( %d%d%d%d%d , &n, &L, &v1, &v2, &k);     double l = 0, r = (double)L / v2, m;     for (int i = 0; i < 100; i++) {       m = (l + r) / 2.0;       if (can(m))         l = m;       else         r = m;     }     printf( %.12f , m + (L - m * v2) / v1);   }


#include <bits/stdc++.h>   using namespace std;   inline long long Calc(const int &x) {     long long res = 0;     for (long long i = 1; i <= x; i *= 10) res += x - i + 1;     return res;   }   inline long long CalcSum(const int &x) {     long long res = 0;     for (long long i = 1; i <= x; i *= 10)       res += 1ll * (x - i + 1) * (x - i + 2) / 2;     return res;   }   inline int Pos(const int &x, const int &k) {     stringstream ss;     ss << x;     string s;     ss >> s;     return s[k - 1] -  0 ;   }   inline int Solve(long long k) {     int l = 1, r = 1000000000;     while (l <= r) {       int mid = (l + r) / 2;       if (CalcSum(mid) >= k)         r = mid - 1;       else         l = mid + 1;     }     k = k - CalcSum(r);     int block = l;     l = 1;     r = block;     while (l <= r) {       int mid = (l + r) / 2;       if (Calc(mid) >= k)         r = mid - 1;       else         l = mid + 1;     }     return Pos(l, k - Calc(r));   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int q;     cin >> q;     while (q--) {       long long k;       cin >> k;       cout << Solve(k) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   const int INF = 1e9;   using namespace std;   void dfs(int v, int p, vector<vector<int> > &graph, vector<int> &sz) {     sz[v] = 1;     for (auto u : graph[v])       if (u != p) {         dfs(u, v, graph, sz);         sz[v] += sz[u];       }   }   void dfs(int v, int p, vector<vector<int> > &graph, vector<int> &c, int C) {     c[v] = C;     for (auto u : graph[v])       if (u != p) {         dfs(u, v, graph, c, C);       }   }   signed main() {     int n;     cin >> n;     vector<vector<int> > graph(n);     for (int i = 0; i < n - 1; i++) {       int a, b;       cin >> a >> b;       a--, b--;       graph[a].push_back(b);       graph[b].push_back(a);     }     vector<int> sz(n);     dfs(0, 0, graph, sz);     vector<int> cen;     long long ans = 0;     for (int i = 0; i < n; i++) {       if ((n - sz[i]) * 2 <= n) {         int t = 1;         for (auto u : graph[i]) {           if (sz[u] < sz[i]) {             if (2 * sz[u] <= n) continue;             t = 0;           }         }         if (t) cen.push_back(i);       }     }     for (int i = 0; i < n; i++) {       ans += 2 * min(sz[i], n - sz[i]);     }     cout << ans <<   n ;     if (cen.size() == 2) {       int v = cen[0], u = cen[1];       vector<int> c(n);       dfs(v, u, graph, c, 0);       dfs(u, v, graph, c, 1);       vector<int> p0, p1;       for (int i = 0; i < n; i++) {         if (c[i] == 0) p0.push_back(i);         if (c[i] == 1) p1.push_back(i);       }       vector<int> ans(n);       for (int i = 0; i < n / 2; i++) {         ans[p0[i]] = p1[i];         ans[p1[i]] = p0[i];       }       for (int i = 0; i < n; i++) {         cout << ans[i] + 1 <<    ;       }       return 0;     } else {       dfs(cen[0], cen[0], graph, sz);       int C = 0, idC = 0;       vector<int> c(n);       c[cen[0]] = -1;       int maxsz = 0;       for (auto u : graph[cen[0]]) {         dfs(u, cen[0], graph, c, C);         maxsz = max(maxsz, sz[u]);         if (maxsz == sz[u]) idC = C;         C++;       }       vector<pair<int, int> > q;       for (int i = 0; i < n; i++) {         if (i != cen[0] && c[i] != idC) q.push_back({c[i], i});       }       for (int i = 0; i < n; i++) {         if (i != cen[0] && c[i] == idC) q.push_back({INF, i});       }       q.push_back({INF + 1, cen[0]});       sort((q).begin(), (q).end());       vector<int> ans(n);       for (int i = 0; i < n; i++) {         ans[q[i].second] = q[(i + maxsz) % q.size()].second;       }       for (int i = 0; i < n; i++) {         assert(c[ans[i]] != c[i]);         cout << ans[i] + 1 <<    ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5;   int n;   double a[MAXN], b[4], t;   int main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n;     b[3] = 1;     for (int i = 1; i <= n; i++) {       cin >> t;       b[0] += t;       b[1]++;       if (b[1] * b[2] <= b[0] * b[3]) b[2] = b[0], b[3] = b[1];     }     cout << fixed << setprecision(10) << b[2] / b[3] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x0f0f0f0f;   template <class S, class T>   inline pair<S, T> MP(S a, T b) {     return make_pair(a, b);   }   template <class T>   inline auto LX(T &X) -> decltype((X.first)) {     return X.first;   }   template <class T>   inline auto LX(const T &X) -> const decltype((X.first)) {     return X.first;   }   template <class T>   inline auto RX(T &X) -> decltype((X.second)) {     return X.second;   }   template <class T>   inline auto RX(const T &X) -> const decltype((X.second)) {     return X.second;   }   template <class T>   inline void RST(T &a) {     memset(a, 0, sizeof(a));   }   template <class T>   inline void RST1(T &a) {     memset(a, -1, sizeof(a));   }   template <class T>   inline void SINF(T &a) {     memset(a, 0x0f, sizeof(a));   }   template <class S, class T>   inline void RST(S &a, T b) {     fill((T *)a, (T *)a + (sizeof(a) / sizeof(b)), b);   }   template <class T>   inline void CLR(T &a) {     a.clear();   }   template <class S, class T>   inline void CPY(S &a, T &b) {     memcpy(a, b, sizeof(a));   }   template <class S, class T>   inline bool chkmin(S &a, T b) {     return b < a ? a = b, 1 : 0;   }   template <class S, class T>   inline bool chkmax(S &a, T b) {     return a < b ? a = b, 1 : 0;   }   template <class T>   inline T sqr(T x) {     return x * x;   }   template <class T>   inline int SZ(T &a) {     return (int)(a.size());   }   template <class T>   inline bool EPT(T &a) {     return a.empty();   }   template <class T>   inline auto FRT(T &a) -> decltype((a.front())) {     return a.front();   }   template <class T>   inline auto FRT(const T &a) -> const decltype((a.front())) {     return a.front();   }   template <class T>   inline auto BK(T &a) -> decltype(a.back()) & {     return a.back();   }   template <class T>   inline auto BK(const T &a) -> const decltype((a.back())) {     return a.back();   }   template <class T>   inline auto TOP(T &a) -> decltype((a.top())) {     return a.top();   }   template <class T>   inline auto TOP(const T &a) -> const decltype((a.top())) {     return a.top();   }   int n, m, f[1010], g[1010], cnt;   char buf[1010];   double ans;   void solve(int *c, int n, int m) {     int s = 0;     for (int i = 1; i <= n; i++)       if (c[i]) {         s += c[i] - 1, ans += (double)s * (m - c[i]);         if (!c[i + 1] || c[i + 1] < c[i]) s = 0;       }     s = 0;     for (int i = 1; i <= n; i++)       if (c[i]) {         ans += (double)s * (c[i] - 1);         s += m - c[i];         if (!c[i + 1] || c[i + 1] > c[i]) s = 0;       }   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %s , buf + 1);       for (int j = 1; j <= m; j++)         if (buf[j] ==  X ) f[i] = j, g[j] = i, cnt++;     }     solve(f, n, m);     solve(g, m, n);     ans *= 2;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= i; j++)         ans += 1. * (m - (f[i] ? 1 : 0)) * (m - (f[j] ? 1 : 0)) * (i - j);     for (int i = 1; i <= m; i++)       for (int j = 1; j <= i; j++)         ans += 1. * (n - (g[i] ? 1 : 0)) * (n - (g[j] ? 1 : 0)) * (i - j);     ans *= 2;     ans /= sqr((double)n * m - cnt);     printf( %.10lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[101][1001];   int n, m;   int g[101][101];   vector<int> col[1001];   set<int> row[101];   bool ans[101][101];   int main() {     scanf( %d , &n), scanf( %d , &m);     for (int i = 0; i < m; ++i) {       int x, y;       scanf( %d , &x), scanf( %d , &y);       g[x][y] = g[y][x] = 1;     }     for (int i = 1; i <= n; ++i) g[i][i] = true;     for (int i = 1; i <= n; ++i) {       for (int j = i + 1; j <= n; ++j) {         if (g[i][j] && !ans[i][j]) {           for (int k = 0; k < 1001; ++k)             if (col[k].size() == 0) {               arr[i][k] = i;               arr[j][k] = j;               col[k].push_back(i), col[k].push_back(j);               break;             } else {               bool ok = true;               for (int l = 0; l < col[k].size(); ++l) {                 int cur = col[k][l];                 if (!g[cur][i] || !g[cur][j]) {                   ok = false;                   break;                 }               }               if (ok) {                 arr[i][k] = i;                 arr[j][k] = j;                 for (int l = 0; l < col[k].size(); ++l) {                   int cur = col[k][l];                   ans[cur][i] = ans[i][cur] = ans[j][cur] = ans[cur][j] = true;                 }                 col[k].push_back(i), col[k].push_back(j);                 break;               }             }           ans[i][j] = true;           ans[j][i] = true;         }       }     }     for (int i = 0; i < 1001; ++i)       for (int j = 0; j < col[i].size(); ++j) row[col[i][j]].insert(i);     for (int i = 1; i < 101; ++i)       if (row[i].size() == 0) {         for (int j = 0; j < 1001; ++j)           if (col[j].size() == 0) {             col[j].push_back(i);             row[i].insert(j);             break;           }       }     for (int i = 1; i <= n; ++i) {       printf( %d n , row[i].size());       for (set<int>::iterator it = row[i].begin(); it != row[i].end(); ++it)         printf( %d %d n , i, (*it) + 1);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, p1, p2;     cin >> n;     cin >> p1;     int a1[p1];     for (int i = 0; i < p1; i++) cin >> a1[i];     cin >> p2;     int a2[p2];     for (int i = 0; i < p2; i++) cin >> a2[i];     sort(a1, a1 + p1);     sort(a2, a2 + p2);     vector<int>::iterator it;     vector<int> res(100);     it = set_union(a1, a1 + p1, a2, a2 + p2, res.begin());     res.resize(it - res.begin());     if (res.size() == n)       cout <<  I become the guy. ;     else       cout <<  Oh, my keyboard! ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int v[5], p[5];   int cnt[5], add[5];   int getMaxScr(int acc, int tot) {     int MaxScr;     for (int i = 0; i < 6; i++) {       int t = 1 << i;       if (tot >= t * acc) MaxScr = (i + 1) * 500;     }     return MaxScr;   }   bool ok(int m) {     int vScr = 0, pScr = 0;     for (int i = 0; i < 5; i++) {       int MaxScr = getMaxScr(cnt[i] + m * add[i], n + m);       if (v[i] != -1) vScr += MaxScr / 250 * (250 - v[i]);       if (p[i] != -1) pScr += MaxScr / 250 * (250 - p[i]);     }     return vScr > pScr;   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       for (int j = 0; j < 5; j++) {         int t;         cin >> t;         if (i == 0) v[j] = t;         if (i == 1) p[j] = t;         if (t != -1) cnt[j]++;       }     }     for (int i = 0; i < 5; i++)       if (v[i] != -1 && p[i] != -1 && p[i] < v[i]) add[i] = 1;     int ans = 0;     while (!ok(ans) && ans < 100000) ans++;     cout << (ans == 100000 ? -1 : ans) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long big = 1000000007;   unsigned long long gcd(unsigned long long a, unsigned long long b) {     if (a == 0) return b;     return gcd(b % a, a);   }   string getbin(unsigned long long x) {     if (x == 0) return  0 ;     string ret =   ;     while (x != 0) {       ret = (char)( 0  + x % 2) + ret;       x /= 2;     }     return ret;   }   unsigned long long getdec(string b) {     unsigned long long ret = 0;     for (int i = 0; i < b.size(); i += 1) {       ret *= 2;       ret += b[i] -  0 ;     }     return ret;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       long long n;       cin >> n;       long long s = 0;       vector<int> a(n);       for (int i = 0; i < n; i += 1) cin >> a[i], s += a[i];       bool b = false;       for (int i = 0; !b && i < n; i += 1) {         if (a[i] > s / 2) b = true;       }       if (b)         cout <<  T ;       else if (s % 2 == 0)         cout <<  HL ;       else         cout <<  T ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0, f = 0;     char ch = getchar();     for (; ch <  0  || ch >  9 ; ch = getchar()) f ^= (ch ==  - );     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ;     return f ? -x : x;   }   const long long maxn = 200050;   long long n, head[maxn], cnt = 0, siz[maxn], dep[maxn];   long long ans = 0, qwq = 0;   struct node {     long long to, nxt;   } e[maxn * 2];   inline void add(long long u, long long v) {     e[++cnt].to = v;     e[cnt].nxt = head[u];     head[u] = cnt;   }   void dfs(long long u, long long f, long long d) {     siz[u] = 1;     dep[u] = d;     for (long long i = head[u]; i; i = e[i].nxt) {       long long v = e[i].to;       if (v == f) continue;       dfs(v, u, d + 1);       siz[u] += siz[v];     }   }   signed main() {     n = read();     for (long long i = 1, a, b; i < n; ++i) {       a = read();       b = read();       add(a, b);       add(b, a);     }     dfs(1, 0, 0);     for (long long i = 1; i <= n; ++i) {       ans += siz[i] * (n - siz[i]);       if (dep[i] & 1) ++qwq;     }     ans += qwq * (n - qwq);     printf( %lld n , ans / 2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   long double p[N], k[N];   int par[N];   vector<int> v[N];   bool vstd[N];   void dfs(int node) {     if (vstd[node]) return;     vstd[node] = true;     for (auto u : v[node]) {       if (u != par[node]) {         k[node] += p[u];         par[u] = node;         dfs(u);       }     }     return;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cout << fixed << setprecision(6);     int n;     cin >> n;     long double tmp = 0.0;     for (int i = 0; i < n; i++) {       cin >> p[i];       p[i] = 1.0 - p[i];       tmp += p[i];     }     for (int i = 1; i < n; i++) {       int a, b;       cin >> a >> b;       v[a].push_back(b);       v[b].push_back(a);     }     long double pmt = 0.0;     dfs(0);     for (int i = 0; i < n; i++) pmt += k[i] * p[i];     int q;     cin >> q;     while (q--) {       int node;       cin >> node;       tmp -= p[node];       pmt -= k[node] * p[node];       if (node) {         pmt -= k[par[node]] * p[par[node]];         k[par[node]] -= p[node];       }       cin >> p[node];       p[node] = 1.0 - p[node];       tmp += p[node];       if (node) k[par[node]] += p[node];       pmt += k[node] * p[node];       if (node) pmt += k[par[node]] * p[par[node]];       cout << tmp - pmt <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char next;     long long int neg = 0, pos = 0;     cin >> next;     bool estado = false;     string orden =   ;     while (next !=  = ) {       if (next ==    ) {         cin >> next;       }       if (next ==  - ) {         estado = true;         cin >> next;       }       if (next ==  + ) {         estado = false;         cin >> next;       }       if (next ==  ? ) {         if (estado) {           orden +=  - ;           neg++;         } else {           orden +=  + ;           pos++;         }       }       cin >> next;     }     long long int num;     cin >> num;     long long int coefs[neg + pos];     if ((neg > ((pos - 1) * num)) || ((pos - neg * num) > num)) {       cout <<  Impossible ;       return 0;     }     long long int valpos = 0, valneg = 0;     cout <<  Possible  << endl;     if (neg == 0) {       long long int cv = trunc(num / pos);       long long int sum = 0;       for (int i = 0; i < pos; i++) {         coefs[i] = cv;       }       sum = cv * pos - num;       if (sum != 0) {         for (int i = 0; i < pos; i++) {           coefs[i]++;           sum++;           if (sum == 0) break;         }       }       cout << coefs[0];       for (int i = 1; i < pos; i++) {         cout <<   +   << coefs[i];       }       cout <<   =   << num;       return 0;     }     if (pos - 1 > neg) {       valpos = 1;       valneg = trunc((pos - 1) / neg);     } else {       valneg = 1;       valpos = trunc(neg / (pos - 1));     }     int ref;     for (int i = 0; i < orden.size(); i++) {       if (orden[i] ==  - ) {         coefs[i] = -valneg;       } else         coefs[i] = valpos;     }     coefs[0] = num;     long long int suma = 0;     for (int i = 0; i < neg + pos; i++) {       suma += coefs[i];     }     suma -= num;     if (suma < 0) {       for (int i = 1; i < neg + pos; i++) {         if (coefs[i] > 0 && (abs(coefs[i]) < num)) {           coefs[i]++;           suma++;         }         if (suma == 0) break;       }     }     if (suma > 0) {       for (int i = 1; i < neg + pos; i++) {         if (coefs[i] < 0 && (abs(coefs[i]) < num)) {           coefs[i]--;           suma--;         }         if (suma == 0) break;       }     }     for (int i = 1; i < neg + pos; i++) {       if (abs(coefs[i]) > num) {         if (coefs[i] < 0) {           suma += -num - coefs[i];           coefs[i] = -num;         } else {           suma += num - coefs[i];           coefs[i] = num;         }       }     }     if (suma > 0) {       coefs[0] -= suma;       suma -= suma;     }     if (suma < 0) {       coefs[0] += suma;       suma -= suma;     }     cout << coefs[0];     for (int i = 1; i < pos + neg; i++) {       if (coefs[i] < 0)         cout <<   -   << abs(coefs[i]);       else         cout <<   +   << coefs[i];     }     cout <<   =   << suma + num;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     if (n == 1) {       cout << -1;     } else       cout << n <<     << n + 1 <<     << n * (n + 1);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, x, y, large = 2000000000, small = -2000000000;     string s1, s2;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> s1 >> x >> s2;       if ((s1 ==  >  && s2 ==  Y ) || (s1 ==  <=  && s2 ==  N )) {         if (x >= small) {           small = x + 1;         }       } else if ((s1 ==  <  && s2 ==  Y ) || (s1 ==  >=  && s2 ==  N )) {         if (x <= large) {           large = x - 1;         }       } else if ((s1 ==  >=  && s2 ==  Y ) || (s1 ==  <  && s2 ==  N )) {         if (x >= small) {           small = x;         }       } else {         if (x <= large) {           large = x;         }       }     }     if (small > large) {       cout <<  Impossible ;     } else {       cout << small;     }     return (0);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 405;   char mp[30], s[N], a[5], b[5];   int L, n, m, e[N][N], nxd[30], nxs[30], flg[30], lim[N], tmp[N], mx[2];   vector<int> ans;   bool chk(int pos, int v, int x, bool ff) {     if (lim[pos] != -1 && lim[pos] != flg[v]) return 0;     for (int i = 1; i <= n; i++)       if (e[x][i] && e[x][i + n]) return 0;     for (int i = 1; i <= n; i++) tmp[i] = (e[x][i] ? 0 : (e[x][i + n] ? 1 : -1));     if (ff) {       for (int i = 1; i <= n; i++) {         if (pos + i > n) break;         int t = max(lim[pos + i], tmp[pos + i]);         if (t == -1) break;         if (mx[t] < s[pos + i] -  a  + 1) return 0;         if (mx[t] > s[pos + i] -  a  + 1) break;       }     }     for (int i = 1; i <= n; i++) {       lim[i] = max(lim[i], tmp[i]);     }     return 1;   }   int main() {     scanf( %s , mp + 1);     L = strlen(mp + 1);     memset(lim, -1, sizeof(lim));     memset(mx, -1, sizeof(mx));     for (int i = 1; i <= L; i++) {       flg[i] = mp[i] ==  V ;       mx[flg[i]] = i;     }     for (int i = 1; i <= L; i++)       for (int j = i + 1; j <= L; j++)         if (flg[i] != flg[j]) {           nxd[i] = j;           break;         }     for (int i = 1; i <= L; i++)       for (int j = i + 1; j <= L; j++)         if (flg[i] == flg[j]) {           nxs[i] = j;           break;         }     cin >> n >> m;     for (int i = 1; i <= m; i++) {       int x, y;       scanf( %d%s%d%s , &x, a, &y, b);       int a1 = a[0] ==  V , b1 = b[0] ==  V ;       e[x + a1 * n][y + b1 * n] = 1;       e[y + !b1 * n][x + !a1 * n] = 1;     }     scanf( %s , s + 1);     for (int k = 1; k <= n * 2; k++)       for (int i = 1; i <= n * 2; i++)         for (int j = 1; j <= n * 2; j++) e[i][j] |= (e[i][k] & e[k][j]);     bool ff = 1;     for (int i = 1; i <= n; i++) {       int mn = ff ? s[i] -  a  + 1 : 1;       if (chk(i, mn, i + flg[mn] * n, ff)) {         ans.push_back(mn);       } else if (mn < L && flg[mn] == flg[mn + 1] &&                  chk(i, mn + 1, i + flg[mn + 1] * n, 0)) {         ans.push_back(mn + 1), ff = 0;       } else if (nxd[mn] && chk(i, nxd[mn], i + flg[nxd[mn]] * n, 0)) {         ans.push_back(nxd[mn]), ff = 0;       } else if (nxs[mn] && nxs[mn] > nxd[mn] &&                  chk(i, nxs[mn], i + flg[nxs[mn]] * n, 0)) {         ans.push_back(nxs[mn]), ff = 0;       } else {         return puts( -1 ), 0;       }     }     for (int i = 0; i <= n - 1; i++) printf( %c , ans[i] +  a  - 1);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps(1e-8);   char s[1000010];   int n, m, a, b;   vector<int> vec[1000010];   vector<int> p[1000010];   int x[1000010], y[1000010], sum[1000010], ans[1000010];   int Lowbit(int x) { return x & (-x); }   void Update(int pos, int val) {     while (pos > 0) {       sum[pos] += val;       pos -= Lowbit(pos);     }   }   int Query(int pos) {     int ret = 0;     while (pos <= n) {       ret += sum[pos];       pos += Lowbit(pos);     }     return ret;   }   void Add(int l, int r, int val) {     Update(r, val);     Update(l - 1, -val);   }   int main() {     scanf( %s , s + 1);     for (n = 1; s[n]; n++)       ;     n--;     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d , &a, &b);       vec[b].push_back(a);       p[b].push_back(i);     }     int len = 0;     for (int i = 1; i <= n; i++) {       len++;       if (s[i] ==  ( )         x[len] = 0;       else         x[len] = 1;       y[len] = i;       if (len >= 2 && x[len] == 1 && x[len - 1] == 0) {         Add(1, y[len - 1], 2);         len -= 2;       }       for (int j = 0; j < (int)vec[i].size(); j++) {         int tmp = vec[i][j];         ans[p[i][j]] = Query(tmp);       }     }     for (int i = 1; i <= m; i++) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int a, b, c, min_cost, min_a, min_b, min_c;       cin >> a >> b >> c;       if (b % a == 0 && c % b == 0) {         cout << 0 << endl;         cout << a <<     << b <<     << c << endl;       } else {         min_cost = 10000000;         int max = 2 * c;         for (int i = 1; i < max; i++)           for (int j = i; j < max; j += i)             for (int k = j; k < max; k += j)               if (k % j == 0 && j % i == 0) {                 int cost = abs(a - i) + abs(b - j) + abs(c - k);                 if (cost < min_cost) {                   min_cost = cost;                   min_a = i;                   min_b = j;                   min_c = k;                 }               }         cout << min_cost << endl;         cout << min_a <<     << min_b <<     << min_c << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100010;   vector<pair<long long, int>> adj[10 * MAXN];   long long dist[10 * MAXN];   int par[10 * MAXN];   int go1(int idx, int l, int r, int ds) {     if (idx > 1) {       int par = idx / 2;       adj[par + ds].push_back({0, idx + ds});     }     if (l == r) {       adj[idx + ds].push_back({0, l});       return idx;     }     int m = (l + r) / 2;     int ret =         max(idx, max(go1(2 * idx, l, m, ds), go1(2 * idx + 1, m + 1, r, ds)));     return ret;   }   void go2(int idx, int l, int r, int ds) {     for (int i = l; i < (r + 1); ++i) {       adj[i].push_back({0, idx + ds});     }     if (r > l) {       int m = (l + r) / 2;       go2(2 * idx, l, m, ds);       go2(2 * idx + 1, m + 1, r, ds);     }   }   void go3(int idx, int x, int y, int l, int r, function<void(int)> f) {     if (y < l || x > r) return;     if (x >= l && y <= r) {       f(idx);       return;     }     int m = (x + y) / 2;     go3(2 * idx, x, m, l, r, f);     go3(2 * idx + 1, m + 1, y, l, r, f);   }   void dijsktra(int src) {     memset(dist, 0x3f3f3f3f, sizeof(dist));     dist[src] = 0;     par[src] = src;     priority_queue<pair<long long, int>, vector<pair<long long, int>>,                    greater<pair<long long, int>>>         pq;     pq.push({dist[src], src});     while (!pq.empty()) {       int u = pq.top().second;       long long l = pq.top().first;       pq.pop();       if (dist[u] == l) {         for (auto p : adj[u]) {           long long ndist = l + p.first;           if (ndist < dist[p.second]) {             dist[p.second] = ndist;             par[p.second] = u;             pq.push({dist[p.second], p.second});           }         }       }     }   }   int main() {     int n, m, src;     scanf(  %d %d , &(n), &(m));     scanf(  %d , &(src));     --src;     int ds1 = n - 1 + 5;     int ds2 = go1(1, 0, n - 1, ds1) + ds1 + 5;     go2(1, 0, n - 1, ds2);     while (m--) {       int tp;       scanf(  %d , &(tp));       if (tp == 1) {         int u, v, w;         scanf(  %d %d , &(u), &(v));         scanf(  %d , &(w));         adj[u - 1].push_back({w, v - 1});       } else {         int vtx, l, r, w;         scanf(  %d %d , &(vtx), &(l));         scanf(  %d %d , &(r), &(w));         --l;         --r;         --vtx;         go3(1, 0, n - 1, l, r, [ds1, ds2, tp, w, vtx](int idx) {           if (tp == 2) {             adj[vtx].push_back({w, idx + ds1});           } else {             adj[idx + ds2].push_back({w, vtx});           }         });       }     }     dijsktra(src);     for (int i = 0; i < (n); ++i)       printf( %lld%c , (dist[i] == 0x3f3f3f3f3f3f3f3fll ? -1 : dist[i]),                 n [i == n - 1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<long long, long long> a[300001];   long long n, k, s, m;   bool b[300001];   inline long long read() {     long long sum = 0, x = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) x = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       sum = sum * 10 + ch -  0 ;       ch = getchar();     }     return sum * x;   }   inline void write(long long x) {     if (x < 0) {       putchar( - );       x = -x;     }     if (x > 9) write(x / 10);     putchar(x % 10 +  0 );     return;   }   inline bool cmp(pair<long long, long long> a, pair<long long, long long> b) {     return a.first > b.first;   }   signed main() {     n = read();     k = read();     for (register long long i = 1; i <= n; ++i) a[i].second = i;     for (m = 1; m <= n; ++m) {       for (register long long j = m * 2; j <= n; j += m) ++a[j].first;       s += a[m].first;       if (s >= k) break;     }     if (s < k) {       puts( No );       return 0;     }     sort(a + 1, a + m, cmp);     long long t = m;     for (register long long i = 1; i <= m; ++i) {       if (a[i].second > m / 2 && s - a[i].first >= k) {         s -= a[i].first;         b[a[i].second] = true;         t--;       }       if (s == k) break;     }     puts( Yes );     write(t);     putchar(  n );     for (register long long i = 1; i <= m; ++i)       if (!b[i]) {         write(i);         putchar(   );       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int te;     cin >> te;     while (te--) {       int n, m, x, ji = 0, ou = 0;       cin >> n >> m;       for (int i = 0; i < n; ++i) {         cin >> x;         if (x & 1)           ji++;         else           ou++;       }       if (ji == 0) {         puts( No );         continue;       }       ji--;       m--;       int q = min(m / 2 * 2, ji / 2 * 2);       m -= q;       ou -= m;       if (ou < 0)         puts( No );       else         puts( Yes );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int s0, t0, t1, n, m, Next[500000 + 5];   char s[500000 + 5], t[500000 + 5];   void GetNext() {     Next[0] = -1;     int j = 0, k = -1;     while (j < m) {       if (!~k || t[j] == t[k])         Next[++j] = ++k;       else         k = Next[k];     }   }   int main() {     scanf( %s%s , s, t);     n = strlen(s), m = strlen(t);     GetNext();     for (int i = 0; s[i]; i++) s0 += s[i] ==  0 ;     for (int i = 0; t[i]; i++) t0 += t[i] ==  0 ;     for (int i = Next[m]; t[i]; i++) t1 += t[i] ==  0 ;     int A = t0, B = m - t0, l = m - Next[m], step = 0;     while (A + t1 <= s0 && B + l - t1 <= n - s0) A += t1, B += l - t1, step++;     if (t0 <= s0 && m - t0 <= n - s0) {       printf( %s , t);       for (int i = 0; i < step; i++) printf( %s , t + Next[m]);       for (int i = s0 - A; i; i--) putchar( 0 );       for (int i = n - s0 - B; i; i--) putchar( 1 );       puts(  );     } else       printf( %s n , s);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000 * 100 + 10;   vector<int> g[maxn];   int a[maxn], b[maxn];   bool p[maxn];   int n, m;   void input() {     cin >> n >> m;     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       u--, v--;       g[u].push_back(v);       g[v].push_back(u);     }     for (int i = 0; i < n; i++) cin >> a[i];   }   void bfs(int v) {     queue<int> q;     q.push(v);     while (!q.empty()) {       p[q.front()] = true;       int u = q.front();       b[u]++;       for (int i = 0; i < g[u].size(); i++) {         b[g[u][i]]++;         if (b[g[u][i]] == a[g[u][i]]) q.push(g[u][i]);       }       q.pop();     }   }   int main() {     input();     for (int i = 0; i < n; i++)       if (b[i] == a[i]) bfs(i);     int ans = 0;     for (int i = 0; i < n; i++) ans += p[i];     cout << ans << endl;     ;     for (int i = 0; i < n; i++)       if (p[i]) cout << i + 1 <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007;   int main() {     string s;     cin >> s;     int k;     cin >> k;     unordered_set<string> st;     for (int i = 0; i < k; i++) {       string temp;       cin >> temp;       st.insert(temp);       string temp2 =   ;       temp2 = temp2 + temp[1];       temp2 = temp2 + temp[0];       st.insert(temp2);     }     vector<pair<char, int> > arr;     for (int i = 0; i < s.size(); i++) {       int count = 1;       int j = i + 1;       while (j < s.size() && s[j] == s[i]) {         count++;         j++;       }       arr.push_back(make_pair(s[i], count));       i = j - 1;     }     int ans = 0;     for (int i = 0; i < arr.size() - 1; i++) {       string temp =   ;       temp = temp + arr[i].first;       temp = temp + arr[i + 1].first;       if (st.find(temp) != st.end()) {         int c1 = arr[i].second, c2 = arr[i + 1].second;         int j = i + 2;         while (j < s.size()) {           if (arr[j].first == arr[i].first)             c1 += arr[j].second;           else if (arr[j].first == arr[i + 1].first)             c2 += arr[j].second;           else             break;           j++;         }         ans += min(c1, c2);         i = j - 1;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, x, y;   long long dp[10000005];   int main(void) {     scanf( %lld%lld%lld , &n, &x, &y);     dp[1] = x;     for (int i = 2; i <= n; ++i) {       if (i & 1) {         dp[i] = min(dp[i - 1] + x, dp[(i + 1) >> 1] + x + y);       } else         dp[i] = min(dp[i - 1] + x, dp[i >> 1] + y);     }     printf( %lld n , dp[n]);   }


#include <bits/stdc++.h>   using namespace std;   int n, nn, sum;   vector<double> q;   double x, ans, anss;   int main() {     scanf( %d , &n);     sum = 0;     for (int i = 1; i <= 2 * n; i++) {       scanf( %lf , &x);       ans += (x - floor(x));       if (floor(x) == ceil(x)) sum++;     }     if (sum > n) n = n * 2 - sum;     sum = min(sum, n);     anss = 10000000;     for (int i = n - sum; i <= n; i++) anss = min(anss, fabs(ans - i));     printf( %.3lf , anss);   }


#include <bits/stdc++.h>   using namespace std;   struct TnT {     int k, f;   } T[111];   bool cmp(TnT a, TnT b) {     if (a.f == b.f) return a.k < b.k;     return a.f < b.f;   }   map<int, int> t;   int main() {     ios::sync_with_stdio(false);     int n, m, ans = -1;     cin >> n >> m;     for (int i = 0; i < m; i++) cin >> T[i].k >> T[i].f;     sort(T, T + m, cmp);     for (int k = 1; k <= 100; k++) {       int cnt = 1;       for (int i = 1; i <= 110; i += k) {         for (int j = i; j <= i + k - 1; j++) t[j] = cnt;         cnt++;       }       int flag = 1;       for (int i = 0; i < m; i++) {         if (t[T[i].k] != T[i].f) flag = 0;       }       if (flag) {         if (ans == -1)           ans = t[n];         else if (ans != t[n]) {           cout << -1 << endl;           return 0;         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool distinct(int n) {     set<int> a;     int i, c = 0;     while (n > 0) {       int d = n % 10;       a.insert(d);       n /= 10;     }     int l = a.size();     for (int i = 0; i <= l - 1; i++) {       c++;     }     if (c == 4) {       return true;     } else {       return false;     }   }   int main() {     int y;     cin >> y;     for (int i = y; i <= 10000; i++) {       if (i > y) {         if (distinct(i)) {           cout << i;           break;         }       }     }     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   char A[2005][2005], B[2005][2005];   int last_valid;   pair<int, int> lb[26], ub[26];   bool ans;   void _clear(int n, int m) {     for (int i = 0; i < 26; ++i) {       lb[i] = make_pair(INT_MAX, INT_MAX);       ub[i] = make_pair(INT_MIN, INT_MIN);     }     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         B[i][j] =  . ;       }     }     ans = true;     last_valid = -1;   }   void _populate(int n, int m) {     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         if (A[i][j] ==  . ) continue;         char c = A[i][j] -  a ;         lb[c].first = min(lb[c].first, i);         lb[c].second = min(lb[c].second, j);         ub[c].first = max(ub[c].first, i);         ub[c].second = max(ub[c].second, j);       }     }   }   void _regenerate() {     for (int i = 0; i < 26; ++i) {       if (lb[i].first != INT_MAX) {         last_valid = i;         if (lb[i].first == ub[i].first) {           int x = lb[i].first;           for (int j = lb[i].second; j <= ub[i].second; ++j) B[x][j] = i +  a ;         } else if (lb[i].second == ub[i].second) {           int x = lb[i].second;           for (int j = lb[i].first; j <= ub[i].first; ++j) B[j][x] = i +  a ;         } else           ans = false;       }     }   }   void _compare(int n, int m) {     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         if (A[i][j] != B[i][j]) ans = false;       }     }   }   void _print() {     if (!ans) {       cout <<  NO  << endl;       return;     }     cout <<  YES  << endl;     cout << last_valid + 1 << endl;     for (int i = 0; i <= last_valid; ++i) {       if (lb[i].first != INT_MAX) {         cout << lb[i].first + 1 <<     << lb[i].second + 1 <<                  << ub[i].first + 1 <<     << ub[i].second + 1 << endl;       } else {         int x = last_valid;         cout << lb[x].first + 1 <<     << lb[x].second + 1 <<                  << ub[x].first + 1 <<     << ub[x].second + 1 << endl;       }     }   }   void _debug(int n, int m) {     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) cout << A[i][j] <<    ;       cout << endl;     }     cout << endl;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) cout << B[i][j] <<    ;       cout << endl;     }     cout << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);     int t, n, m, i, j, k, x, y, z, a, b, c;     cin >> t;     while (t--) {       cin >> n >> m;       for (i = 0; i < n; ++i) cin >> A[i];       _clear(n, m);       _populate(n, m);       _regenerate();       _compare(n, m);       _print();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   string s;   long long dp[200011];   const long long Mod = 1e9 + 7;   long long f[200011][3];   long long power(long long x, long long n) {     if (n < 0) return 0;     if (n == 0) return 1;     if (n % 2 == 0) return power(x * x % Mod, n / 2);     return x * power(x * x % Mod, n / 2) % Mod;   }   int main() {     cin >> n;     cin >> s;     s =  .  + s;     int c = 0;     for (int i = n; i >= 1; i--) {       if (s[i] ==  ? ) c++;     }     long long cnt1 = 0;     long long cnt2 = 0;     for (int i = 1; i <= n; i++) {       if (s[i] ==  b ) {         f[i][0] += cnt1;         f[i][1] += cnt2;       }       if (s[i] ==  ? ) {         f[i][1] += cnt1;         f[i][2] += cnt2;       }       if (s[i] ==  a ) {         cnt1++;       }       if (s[i] ==  ? ) cnt2++;     }     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= 2; j++) (f[i][j] += f[i - 1][j]) %= Mod;     }     long long ans = 0;     for (int i = 1; i <= n; i++) {       if (s[i] ==  c ) {         ans += f[i - 1][0] * power(3, c);         ans %= Mod;         ans += f[i - 1][1] * power(3, c - 1);         ans %= Mod;         ans += f[i - 1][2] * power(3, c - 2);         ans %= Mod;       }       if (s[i] ==  ? ) {         ans += f[i - 1][0] * power(3, c - 1);         ans %= Mod;         ans += f[i - 1][1] * power(3, c - 2);         ans %= Mod;         ans += f[i - 1][2] * power(3, c - 3);         ans %= Mod;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, x[1042], y[1042];   double eps = 0.00000001;   int dist2(int x1, int y1, int x2, int y2) {     return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);   }   double dist(double x1, double y1, double x2, double y2) {     return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));   }   int cross(int a, int b, int c) {     return (x[a] - x[b]) * (y[c] - y[b]) - (x[c] - x[b]) * (y[a] - y[b]);   }   bool acute(int a, int b, int c) {     if (cross(a, b, c) == 0) {       return 0;     }     int dAB = dist2(x[a], y[a], x[b], y[b]);     int dBC = dist2(x[b], y[b], x[c], y[c]);     int dAC = dist2(x[a], y[a], x[c], y[c]);     if (dAB >= dBC + dAC) {       return 0;     }     if (dAC >= dBC + dAB) {       return 0;     }     if (dBC >= dAC + dAB) {       return 0;     }     return 1;   }   bool r(int a, int b, int c) {     int dAB = dist2(x[a], y[a], x[b], y[b]);     int dBC = dist2(x[b], y[b], x[c], y[c]);     int dAC = dist2(x[a], y[a], x[c], y[c]);     if (dAB == dBC + dAC) {       return 1;     }     if (dAC == dBC + dAB) {       return 1;     }     if (dBC == dAC + dAB) {       return 1;     }     return 0;   }   bool covered(double cx, double cy, double t) {     for (int i = 0; i < n; i++) {       if (dist(cx, cy, x[i], y[i]) < t) {         return 1;       }     }     return 0;   }   pair<double, double> getCirC(int a, int b, int c) {     int dx1 = x[a] - x[b], dx2 = x[b] - x[c], dx3 = x[c] - x[a];     int dy1 = y[a] - y[b], dy2 = y[b] - y[c], dy3 = y[c] - y[a];     double xc1 = (x[a] + x[b]) / 2., xc2 = (x[b] + x[c]) / 2.,            xc3 = (x[c] + x[a]) / 2.;     double yc1 = (y[a] + y[b]) / 2., yc2 = (y[b] + y[c]) / 2.,            yc3 = (y[c] + y[a]) / 2.;     if (dy1 == 0) {       swap(dx1, dx3);       swap(dy1, dy3);       swap(xc1, xc3);       swap(yc1, yc3);     }     if (dy2 == 0) {       swap(dx2, dx3);       swap(dy2, dy3);       swap(xc2, xc3);       swap(yc2, yc3);     }     double n1 = -1. * dx1 / (double)dy1, n2 = -1. * dx2 / (double)dy2;     double x = (yc2 - yc1 + n1 * xc1 - n2 * xc2) / (n1 - n2);     double y = n1 * (x - xc1) + yc1;     return make_pair(x, y);   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x[i] >> y[i];     }     double ans = -1;     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         for (int k = j + 1; k < n; k++) {           if (acute(i, j, k)) {             pair<double, double> crr = getCirC(i, j, k);             double px = crr.first, py = crr.second;             double d = dist(px, py, x[i], y[i]);             if (!covered(px, py, d - eps)) {               ans = max(ans, d);             }           }         }       }     }     for (int i = 0; i < n; i++) {       for (int j = i + 1; j < n; j++) {         vector<int> ids;         ids.clear();         for (int k = 0; k < n; k++) {           if (k == i || k == j) {             continue;           }           if (dist2(x[i], y[i], x[k], y[k]) != dist2(x[j], y[j], x[k], y[k])) {             continue;           }           if (!r(i, j, k)) {             continue;           }           ids.push_back(k);         }         if (ids.size() < 2) {           continue;         }         int k = ids[0];         int cc = ids[1];         double px = (x[i] + x[j] + x[k] + x[cc]) / 4.;         double py = (y[i] + y[j] + y[k] + y[cc]) / 4.;         double d = dist(x[i], y[i], px, py);         if (!covered(px, py, d - eps)) {           ans = max(ans, d);         }       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> vec[100010];   long long a[100010];   long long s;   int n;   long long dfs(int f, int u, long long val) {     int sz = vec[u].size(), v;     long long ret = 0, mv = ((long long)1e18), tmp;     if (sz == 1 && vec[u][0] == f)       return (a[u] = ((val) < (a[u]) ? (val) : (a[u])));     if (u != 1)       val = val / (sz - 1) * (sz - 1) / (sz - 1);     else       val = val / sz * sz / sz;     for (int i = 0; i < sz; i++) {       v = vec[u][i];       if (v == f) continue;       tmp = dfs(u, v, val);       mv = ((mv) < (tmp) ? (mv) : (tmp));     }     while (true) {       tmp = mv;       for (int i = 0; i < sz; i++) {         v = vec[u][i];         if (v == f) continue;         if (a[v] > tmp) tmp = ((dfs(u, v, mv)) < (tmp) ? (dfs(u, v, mv)) : (tmp));       }       if (tmp == mv)         break;       else         mv = tmp;     }     for (int i = 0; i < sz; i++)       if (vec[u][i] != f) ret += a[vec[u][i]];     return a[u] = ret;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %I64d , a + i), s += a[i];     for (int i = 1; i < n; i++) {       int u, v;       scanf( %d%d , &u, &v);       vec[u].push_back(v);       vec[v].push_back(u);     }     long long ret = dfs(0, 1, ((long long)1e18));     cout << s - ret << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dx[] = {0, 0, 1, -1};   int dy[] = {1, -1, 0, 0};   template <class T>   inline void smin(T &a, T b) {     if (a > b) a = b;   }   template <class T>   inline void smax(T &a, T b) {     if (a < b) a = b;   }   int qf, qb, q[11111111];   long long res[555], a[555][555], dst[555][555];   int r[555], first[555];   int main() {     int N, ii, i, j, nn, u, st;     scanf( %d , &N);     for (i = 0; i < N; i++)       for (j = 0; j < N; j++) scanf( %I64d , &dst[i][j]);     for (i = 0; i < N; i++) {       scanf( %d , r + i);       r[i]--;     }     nn = 0;     for (ii = N - 1; ii >= 0; ii--) {       st = r[ii];       for (i = 0; i < nn; i++) {         for (j = 0; j < nn; j++) {           smin(dst[st][first[j]], dst[st][first[i]] + dst[first[i]][first[j]]);           smin(dst[first[j]][st], dst[first[j]][first[i]] + dst[first[i]][st]);         }       }       for (i = 0; i < nn; i++)         for (j = 0; j < nn; j++)           smin(dst[first[i]][first[j]], dst[first[i]][st] + dst[st][first[j]]);       first[nn++] = st;       res[ii] = 0;       for (i = 0; i < nn; i++)         for (j = 0; j < nn; j++) res[ii] += dst[first[i]][first[j]];     }     for (i = 0; i < N; i++) {       if (i) printf(   );       printf( %I64d , res[i]);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 400005;   int n, q, K, m, i, j, k, h[N], t[N], num, fa[N], s[N], id[N], size[N], son[N],       tp[N], dep[N], head[N], adj[N * 2], nxt[N * 2], p[N], cnt, stk[N], top,       g[N], Head[N], Adj[N * 2], Nxt[N * 2], l, f[N];   void dfs(int x, int dad) {     size[x] = 1;     for (int y = head[x]; y; y = nxt[y])       if (adj[y] != dad) {         dep[adj[y]] = dep[x] + 1;         fa[adj[y]] = x;         dfs(adj[y], x);         size[x] += size[adj[y]];         if (!son[x] || size[adj[y]] > size[son[x]]) son[x] = adj[y];       }   }   void Dfs(int x, int dad) {     id[x] = ++i;     if (son[x]) {       tp[son[x]] = tp[x];       Dfs(son[x], x);     }     for (int y = head[x]; y; y = nxt[y])       if (adj[y] != dad && adj[y] != son[x]) {         tp[adj[y]] = adj[y];         Dfs(adj[y], x);       }   }   int lca(int a, int b) {     while (tp[a] != tp[b]) {       if (dep[tp[a]] < dep[tp[b]]) swap(a, b);       a = fa[tp[a]];     }     return dep[a] < dep[b] ? a : b;   }   int dis(int a, int b) { return dep[a] + dep[b] - 2 * dep[lca(a, b)]; }   bool cmp(int a, int b) { return id[a] < id[b]; }   void addedge(int u, int v) {     Adj[++l] = v;     Nxt[l] = Head[u];     Head[u] = l;     Adj[++l] = u;     Nxt[l] = Head[v];     Head[v] = l;   }   void dzx(int x, int dad) {     if (s[x])       f[x] = x;     else       f[x] = 0;     for (int y = Head[x], a, b; y; y = Nxt[y])       if (Adj[y] != dad) {         dzx(Adj[y], x);         if (f[Adj[y]] && (!f[x] ||                           1ll * (a = dis(f[Adj[y]], x) - 1) / s[f[Adj[y]]] <                               1ll * (b = dis(f[x], x) - 1) / s[f[x]] ||                           1ll * (a = dis(f[Adj[y]], x) - 1) / s[f[Adj[y]]] ==                                   1ll * (b = dis(f[x], x) - 1) / s[f[x]] &&                               h[f[Adj[y]]] < h[f[x]]))           f[x] = f[Adj[y]];       }     if (s[x]) f[x] = x;   }   void phh(int x, int dad, int u) {     int a, b;     if (u) {       if (u && (!f[x] ||                 1ll * (a = dis(u, x) - 1) / s[u] <                     1ll * (b = dis(f[x], x) - 1) / s[f[x]] ||                 1ll * (a = dis(u, x) - 1) / s[u] ==                         1ll * (b = dis(f[x], x) - 1) / s[f[x]] &&                     h[u] < h[f[x]]))         f[x] = u;     }     if (s[x]) f[x] = x;     for (int y = Head[x]; y; y = Nxt[y])       if (Adj[y] != dad) phh(Adj[y], x, f[x]);     Head[x] = 0;   }   int main() {     scanf( %d , &n);     for (i = 1; i < n; ++i) {       scanf( %d%d , &j, &k);       adj[i * 2 - 1] = k;       nxt[i * 2 - 1] = head[j];       head[j] = i * 2 - 1;       adj[i * 2] = j;       nxt[i * 2] = head[k];       head[k] = i * 2;     }     dfs(1, -1);     tp[1] = 1;     i = 0;     Dfs(1, -1);     scanf( %d , &q);     while (q--) {       if (n == 10) {         for (i = 1; i <= n; ++i) Head[i] = h[i] = s[i] = g[i] = t[i] = p[i] = 0;       }       scanf( %d%d , &K, &m);       num = 0;       for (i = 1; i <= K; ++i) {         scanf( %d%d , &j, &k);         s[j] = k;         h[j] = i;         t[++num] = j;       }       for (i = 1; i <= m; ++i) {         scanf( %d , &j);         g[i] = t[++num] = j;       }       t[++num] = 1;       sort(t + 1, t + 1 + num, cmp);       cnt = 0;       for (i = 1; i <= num; i = j) {         for (j = i; j <= num && t[i] == t[j]; ++j)           ;         p[++cnt] = t[i];       }       if (cnt == 1) {         puts( 1 );         continue;       }       l = 0;       stk[1] = p[1], stk[2] = p[2];       top = 2;       for (i = 3; i <= cnt; ++i) {         while (top > 1 && dep[lca(stk[top], p[i])] <= dep[stk[top - 1]]) {           addedge(stk[top - 1], stk[top]);           --top;         }         if ((j = lca(p[i], stk[top])) == stk[top])           stk[++top] = p[i];         else {           addedge(j, stk[top]);           stk[top] = j;           stk[++top] = p[i];         }       }       while (top > 1) {         addedge(stk[top - 1], stk[top]);         --top;       }       dzx(1, -1);       phh(1, -1, 0);       for (i = 1; i <= m; ++i) printf( %d  , h[f[g[i]]]);       puts(  );       for (i = 1; i <= cnt; ++i) s[p[i]] = 0;     }     return 0;   }


#include<bits/stdc++.h>   #define pairs pair<int,int>   #define loop(i,a,n) for(int i=a;i<n;i++)   #define m_p make_pair   #define fi first   #define se second   #define pb push_back   #define ll long long   #define all(v) v.begin(),v.end()   using namespace std;   //vector<ll> siz;   /*ll indes(ll i,ll right){       ll val=siz[i];       ll left=0;       ll ans=-1;       while(left<=right){           ll mid=(left+right)/2;           if(2*siz[mid]<=val){               ans=mid;               left=mid+1;           }           else           {               right=mid-1;           }                  }       return ans;    }*/   map<pairs,bool> mp;      void solve(){   ll n,k;   cin>>n>>k;   vector<ll> vec(n);   vector<pairs> range(n);   loop(i,0,n){       cin>>vec[i];   }   range[0].fi=vec[0];   range[0].se=vec[0];   range[n-1].fi=vec[n-1];   range[n-1].se=vec[n-1];   for(ll i=1;i<n-1;i++){     ll a=range[i-1].fi;     ll b=range[i-1].se;     ll c= b+k-1;     ll d=a-(k-1);     range[i].fi=max(d,vec[i]);     range[i].se=min(c,vec[i]+(k-1));   }      loop(i,1,n){     ll a=range[i-1].fi;     ll b=range[i-1].se;     ll c=range[i].fi;     ll d=range[i].se;        if(a-k>=d||b+k<=c){         cout<< NO ;         return;     }          }   cout<< YES ;   return;   }   int main(){       int test=1;       cin>>test;       while(test--){           solve();           cout<<endl;       }   }


#include <bits/stdc++.h>   using namespace std;   const int Mod = 1e9 + 7;   int main() {     long long N, M, ans = 0, Last = 0;     scanf( %I64d%I64d , &N, &M);     long long Min = min(N, M);     for (long long i = 1, j; i <= Min; i = j + 1) {       j = min(N / (N / i), M);       long long tmp;       if (j & 1LL)         tmp = ((j + 1) / 2 % Mod) * (j % Mod) % Mod;       else         tmp = (j / 2 % Mod) * ((j + 1) % Mod) % Mod;       ans =           (ans + (N / i) % Mod * (((tmp - Last) % Mod + Mod) % Mod) % Mod) % Mod;       Last = tmp;     }     ans = (((N % Mod) * (M % Mod) - ans) % Mod + Mod) % Mod;     printf( %I64d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2E5 + 10, mod = 1E9 + 7, M = 1E6 + 10, lim = 1E6;   int n, m, a[N], s[N], ans[N];   struct node {     int l, r, num;   } p[N];   vector<int> f[M];   int pos[M];   inline int cmp(node x, node y) { return x.r < y.r; }   struct sgtree {     int l, r, sum;   } t[N << 2];   inline int ksm(int x, int y) {     return y == 1                ? x                : 1ll * ksm(1ll * x * x % mod, y >> 1) * ((y & 1) ? x : 1) % mod;   }   inline void pl(int l = 1, int r = n, int x = 1) {     t[x] = (sgtree){l, r, 1};     if (l == r) return;     int mid = l + r >> 1;     pl(l, mid, x << 1);     pl(mid + 1, r, x << 1 | 1);   }   inline void modify(int l, int r, int v, int x = 1) {     if (l == t[x].l && r == t[x].r) {       t[x].sum = 1ll * t[x].sum * v % mod;       return;     }     int mid = t[x].l + t[x].r >> 1;     if (r <= mid)       modify(l, r, v, x << 1);     else if (l > mid)       modify(l, r, v, x << 1 | 1);     else       modify(l, mid, v, x << 1), modify(mid + 1, r, v, x << 1 | 1);   }   inline void insert(int v, int x) {     for (int i = 0; i < f[v].size(); i++) {       modify(pos[f[v][i]] + 1, x,              1ll * ksm(f[v][i], mod - 2) * (f[v][i] - 1) % mod);       pos[f[v][i]] = x;     }   }   inline int query(int l, int x = 1) {     if (t[x].l == t[x].r) return t[x].sum;     int mid = t[x].l + t[x].r >> 1;     return 1ll * t[x].sum * (l <= mid ? query(l, x << 1) : query(l, x << 1 | 1)) %            mod;   }   inline int sol(int l, int r) {     return 1ll * s[r] * ksm(s[l - 1], mod - 2) % mod * query(l) % mod;   }   int main() {     scanf( %d , &n);     s[0] = 1;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       s[i] = 1ll * s[i - 1] * a[i] % mod;     }     scanf( %d , &m);     pl();     for (int i = 1; i <= m; i++) {       scanf( %d%d , &p[i].l, &p[i].r);       p[i].num = i;     }     sort(p + 1, p + 1 + m, cmp);     int now = 1;     for (int i = 2; i <= lim; i++)       if (!f[i].size())         for (int j = 1; i * j <= lim; j++) f[i * j].push_back(i);     for (int i = 1; i <= n; i++) {       insert(a[i], i);       while (now <= m && p[now].r == i) {         ans[p[now].num] = sol(p[now].l, p[now].r);         now++;       }     }     for (int i = 1; i <= m; i++) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long fact[1000010];   long long m = 1e9 + 7;   long long me(long long a, long long b, long long m) {     long long ans = 1;     while (b > 0) {       if (b % 2 == 1) {         ans = (ans % m * a % m) % m;       }       a = (a * a) % m;       b /= 2;     }     return ans % m;   }   bool check(long long x, long long a, long long b) {     while (x > 0) {       int dg = x % 10;       if (dg != a && dg != b) return false;       x /= 10;     }     return true;   }   long long ncr(long long n, long long r) {     long long fn = fact[n];     long long tf = fact[r] * fact[n - r];     tf = tf % m;     tf = me(tf, m - 2, m);     fn = (tf % m * fn % m) % m;     return fn % m;   }   int main() {     fact[0] = 1;     for (long long i = 1; i <= 1000001; i++) {       fact[i] = (fact[i - 1] * i) % m;     }     int n, a, b;     cin >> a >> b >> n;     if (a == b) {       if (check(n * a, a, b)) {         cout <<  1 n ;       } else         cout <<  0 n ;       return 0;     }     long long ans = 0;     for (long long i = 0; i <= n; i++) {       long long tmp = i * a + (n - i) * b;       if (check(tmp, a, b)) {         ans += ncr(n, i);         ans %= m;       }     }     cout << ans <<   n ;     return 0;   }