#include <bits/stdc++.h>   using namespace std;   int read() {     int x = 0, sgn = 1;     char ch = getchar();     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) sgn = -1;     for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);     return x * sgn;   }   const int N = 1e5 + 10;   int n, b[N << 1], tot, l[N], r[N], deg[N << 1], h[N << 1], cnt = 1, now[N << 1],                                                              ans[N << 1];   struct edge {     int v, w, nxt;   } e[N << 2];   bool v[N << 2], vis[N << 1];   void link(int x, int y, int w) {     e[++cnt] = (edge){y, w, h[x]};     h[x] = cnt;   }   void dfs(int x) {     vis[x] = 1;     for (int i = now[x], y; i; i = e[now[x]].nxt) {       now[x] = i;       if (v[i]) continue;       y = e[i].v;       v[i] = v[i ^ 1] = 1;       ans[e[i].w] = x < y;       dfs(y);     }   }   int main() {     n = read();     for (int i = 1; i <= n; i++)       b[++tot] = l[i] = read(), b[++tot] = r[i] = read() + 1;     sort(b + 1, b + tot + 1);     tot = unique(b + 1, b + tot + 1) - b - 1;     for (int i = 1; i <= n; i++) {       l[i] = lower_bound(b + 1, b + tot + 1, l[i]) - b;       r[i] = lower_bound(b + 1, b + tot + 1, r[i]) - b;       deg[l[i]]++;       deg[r[i]]++;       link(l[i], r[i], i);       link(r[i], l[i], i);     }     int lst = 0;     for (int i = 1; i <= tot; i++) {       if (!(deg[i] & 1)) continue;       if (lst)         link(lst, i, 0), link(i, lst, 0), lst = 0;       else         lst = i;     }     for (int i = 1; i <= tot; i++) now[i] = h[i];     for (int i = 1; i <= tot; i++)       if (!vis[i]) dfs(i);     for (int i = 1; i <= n; i++) {       if (ans[i])         printf( 1  );       else         printf( 0  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char *student;   int *arr;   struct node {     int x;     int y;     int val;   };   std::vector<node> v;   std::vector<node> out;   struct edge {     int v;     edge *left;     edge *right;   };   edge *ed;   bool *flag;   bool comp(node &a, node &b) {     return (!(a.val < b.val || (a.val == b.val && a.x < b.x)));   }   void make_node(int x, int y) {     if (student[x] ^ student[y]) {       node temp;       temp.x = x;       temp.y = y;       temp.val = abs(arr[x] - arr[y]);       v.push_back(temp);       push_heap(v.begin(), v.end(), comp);     }   }   void print() {     for (std::vector<node>::iterator it = out.begin(); it != out.end(); ++it) {       node temp = *it;       cout << temp.x + 1 <<     << temp.y + 1 << endl;     }   }   void select() {     while (true) {       int l = -1, r = -1;       while (v.size() > 0) {         node temp = v.front();         pop_heap(v.begin(), v.end(), comp);         v.pop_back();         l = temp.x;         r = temp.y;         if (flag[l] || flag[r]) {           l = -1;           r = -1;           continue;         } else {           flag[l] = true;           flag[r] = true;           out.push_back(temp);           break;         }       }       if (l == -1) break;       edge *lt = ed[l].left;       edge *rt = ed[r].right;       if (lt != NULL) {         lt->right = rt;       }       if (rt != NULL) {         rt->left = lt;       }       if (lt != NULL && rt != NULL) make_node(lt->v, rt->v);     }   }   int main() {     std::cout << std::fixed << std::setprecision(10);     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     student = new char[n];     arr = new int[n];     flag = new bool[n];     string s;     cin >> s;     for (int i = 0; i < n; i++) {       student[i] = s[i];       int x;       cin >> x;       arr[i] = x;       flag[i] = false;     }     for (int i = 0; i < n - 1; i++) {       make_node(i, i + 1);     }     ed = new edge[n];     for (int i = 0; i < n; i++) {       ed[i].v = i;       ed[i].left = i > 0 ? &ed[i - 1] : NULL;       ed[i].right = i < n - 1 ? &ed[i + 1] : NULL;     }     select();     cout << out.size() << endl;     print();   }


#include <bits/stdc++.h>   using namespace std;   const long double PI = acos(-1);   pair<long double, long double> sum(pair<long double, long double> a,                                      pair<long double, long double> b) {     return {a.first + b.first, a.second + b.second};   }   long double cross(pair<long double, long double> a,                     pair<long double, long double> b) {     return a.first * b.second - b.first * a.second;   }   pair<long double, long double> prod(long double k,                                       pair<long double, long double> a) {     return {k * a.first, k * a.second};   }   pair<long double, long double> com(vector<pair<long double, long double> > &v) {     auto i = v.begin(), end = v.end(), j = end - 1;     auto ss = v.begin();     pair<long double, long double> res{0, 0};     long double A = 0;     for (; i != end; j = i++) {       pair<long double, long double> a = *i, b = *j;       a.first -= ss->first;       a.second -= ss->second;       b.first -= ss->first;       b.second -= ss->second;       res = sum(res, prod(cross(b, a), sum(a, b)));       A += cross(b, a);     }     return {res.first / A / 3 + ss->first, res.second / A / 3 + ss->second};   }   long double dist(pair<long double, long double> a,                    pair<long double, long double> b) {     long double dx = a.first - b.first;     long double dy = a.second - b.second;     return sqrt(dx * dx + dy * dy);   }   int main() {     cin.sync_with_stdio(0);     cin.tie(0);     int n, q;     cout << setprecision(20);     cin >> n >> q;     vector<pair<long double, long double> > poly(n);     for (int i = 0; i < n; ++i) {       cin >> poly[i].first >> poly[i].second;     }     pair<long double, long double> c = com(poly);     vector<pair<long double, long double> > v(n);     for (int i = 0; i < n; ++i) {       v[i].first = dist(c, poly[i]);       v[i].second = atan2((poly[i].second - c.second), (poly[i].first - c.first));     }     bool first = 1;     pair<int, int> cur{0, 1};     pair<pair<long double, long double>, pair<long double, long double> > cc{         poly[0], poly[1]};     pair<long double, long double> center;     long double theta;     while (q--) {       int type;       cin >> type;       if (type == 1) {         int f, t;         cin >> f >> t;         --f;         --t;         int m;         pair<long double, long double> top;         if (f == cur.first) {           m = cur.second;           top = cc.second;         } else {           m = cur.first;           top = cc.first;         }         center = {top.first, top.second - v[m].first};         theta = PI / 2 - v[m].second;         pair<long double, long double> other = {             v[t].first * cos(theta + v[t].second) + center.first,             v[t].first * sin(theta + v[t].second) + center.second};         cur = {m, t};         cc = {top, other};         first = 0;       } else {         int t;         cin >> t;         --t;         if (first) {           cout << poly[t].first <<     << poly[t].second <<   n ;         } else {           cout << v[t].first * cos(theta + v[t].second) + center.first <<                    << v[t].first * sin(theta + v[t].second) + center.second <<   n ;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long n, a, b, c;   long long d;   long long cnt1, cnt2;   long long r;   int main() {     cin >> n >> a >> b >> c;     d = b - c;     if (n >= a) {       cnt1 = n / a;       r = n % a;       if (r >= b) cnt1 += (r - b) / d + 1;     }     if (n >= b) {       r = n - b;       long long cnt = r / d + 1;       cnt2 += cnt;       cnt2 += (n - cnt * d) / a;     }     printf( %lld n , max(cnt1, cnt2));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, temp, count = 0, found = 0;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) {       cin >> temp;       if (temp == i)         ++count;       else if (temp < i && a[temp] == i)         found = 2;       else if (found != 2)         found = 1;       a[i] = temp;     }     cout << count + found << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct sheet {     long long xt, yt, xb, yb;   };   sheet a[3];   long long flag = 0;   void hor() {     long long yc = a[0].yb;     for (double x = a[0].xb; x <= a[0].xt; x += .5) {       if (a[1].xb <= x && x <= a[1].xt && a[1].yb <= yc && yc <= a[1].yt) {         continue;       } else if (a[2].xb <= x && x <= a[2].xt && a[2].yb <= yc && yc <= a[2].yt) {         continue;       } else {         flag = 1;         return;       }     }     yc = a[0].yt;     for (double x = a[0].xb; x <= a[0].xt; x += .5) {       if (a[1].xb <= x && x <= a[1].xt && a[1].yb <= yc && yc <= a[1].yt) {         continue;       } else if (a[2].xb <= x && x <= a[2].xt && a[2].yb <= yc && yc <= a[2].yt) {         continue;       } else {         flag = 1;         return;       }     }   }   void ver() {     long long xc = a[0].xb;     for (double y = a[0].yb; y <= a[0].yt; y += .5) {       if (a[1].xb <= xc && xc <= a[1].xt && a[1].yb <= y && y <= a[1].yt) {         continue;       } else if (a[2].xb <= xc && xc <= a[2].xt && a[2].yb <= y && y <= a[2].yt) {         continue;       } else {         flag = 1;         return;       }     }     xc = a[0].xt;     for (double y = a[0].yb; y <= a[0].yt; y++) {       if (a[1].xb <= xc && xc <= a[1].xt && a[1].yb <= y && y <= a[1].yt) {         continue;       } else if (a[2].xb <= xc && xc <= a[2].xt && a[2].yb <= y && y <= a[2].yt) {         continue;       } else {         flag = 1;         return;       }     }   }   int main() {     for (int i = 0; i < 3; i++) {       cin >> a[i].xb >> a[i].yb >> a[i].xt >> a[i].yt;     }     hor();     ver();     cout << (flag ?  YES  :  NO );   }


#include <bits/stdc++.h>   using namespace std;   void ret(string s) {     cout << s;     exit(0);   }   int main() {     int x1, x2, y1, y2, n;     cin >> n >> x1 >> y1 >> x2 >> y2;     if (x1 * x2 == 0 && x1 + x2 == n)       cout << min(n + y1 + y2, 3 * n - y1 - y2);     else if (y1 * y2 == 0 && y1 + y2 == n)       cout << min(n + x1 + x2, 3 * n - x1 - x2);     else       cout << abs(x1 - x2) + abs(y1 - y2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 34;   const int M = 70;   int n;   int arr[N];   int dp[N][M * M][M];   int find(int pos, int sum, int cnt) {     if (pos > n) {       return (sum * 2 == cnt * (cnt - 1)) ? cnt : M;     }     if (dp[pos][sum][cnt] != -1) {       return dp[pos][sum][cnt];     }     int res = M;     int cur = sum;     int tot = cnt;     for (int i = 1; tot < 65; ++i) {       ++tot;       cur += arr[pos];       if (cur * 2 >= tot * (tot - 1)) {         res = min(res, find(pos + 1, cur, tot));       }     }     return dp[pos][sum][cnt] = res;   }   int deg[M];   void go(int pos, int sum, int cnt) {     if (pos > n) {       return;     }     int cur = sum;     int tot = cnt;     for (int i = 1; tot < 65; ++i) {       ++tot;       cur += arr[pos];       deg[tot] = arr[pos];       if (cur * 2 >= tot * (tot - 1)) {         if (find(pos + 1, cur, tot) == find(pos, sum, cnt)) {           go(pos + 1, cur, tot);           return;         }       }     }   }   int graph[M][M];   pair<int, int> out[M];   int main() {     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> arr[i];     }     sort(arr + 1, arr + 1 + n);     memset(dp, -1, sizeof(dp));     int m = find(1, 0, 0);     go(1, 0, 0);     for (int i = 1; i <= m; ++i) {       out[i] = make_pair(deg[i], i);     }     sort(out + 1, out + 1 + m);     for (int i = 1; i <= m; ++i) {       for (int j = i + 1; j <= i + out[i].first; ++j) {         graph[out[i].second][out[j].second] = 1;       }       for (int j = i + out[i].first + 1; j <= m; ++j) {         graph[out[j].second][out[i].second] = 1;         --out[j].first;       }       out[i].first = 0;       sort(out + i + 1, out + 1 + m);     }     printf( %d n , m);     for (int i = 1; i <= m; ++i) {       for (int j = 1; j <= m; ++j) {         printf( %d , graph[i][j]);       }       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   int tmp, n, tr[100005 * 30][2], cnt;   inline void insert(int x) {     int p = 0, d;     for (int i = 29; i >= 0; --i) {       d = (x >> i) & 1;       if (tr[p][d])         p = tr[p][d];       else {         tr[p][d] = ++cnt;         p = cnt;         memset(tr[p], 0, sizeof(tr[p]));       }     }   }   int dfs(int p, int d) {     if (!tr[p][0] && !tr[p][1]) return 0;     if (!tr[p][0]) return dfs(tr[p][1], d - 1);     if (!tr[p][1]) return dfs(tr[p][0], d - 1);     return (1 << d) + min(dfs(tr[p][0], d - 1), dfs(tr[p][1], d - 1));   }   int main() {     while (~scanf( %d , &n)) {       cnt = 0;       memset(tr[0], 0, sizeof(tr[0]));       for (int i = 1; i <= n; ++i) {         scanf( %d , &tmp);         insert(tmp);       }       int ans = dfs(0, 29);       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     vector<long long> arr(4);     for (int i = 0; i < 4; i++) cin >> arr[i];     int counter = 0;     for (int i = 0; i < 4; i++) counter += arr[i] % 2;     if (counter == 0 || counter == 1 || counter == 4) {       cout <<  Yes n ;       return;     } else if (counter == 3) {       if (arr[0] != 0 && arr[1] != 0 && arr[2] != 0)         cout <<  Yes n ;       else         cout <<  No n ;     } else       cout <<  No n ;     return;   }   int main() {     int t;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int N = 250;   int n, m, B, ans;   int co[N], sx[N], sy[N];   int mp[N][N], id[N], fl[N];   bool cmp(int x, int y) { return sy[x] < sy[y]; }   const int ND = 500;   struct edge {     int to, next, f, v;   } e[N * N];   int head[ND], cur[ND], tot;   int S, T, dis[ND];   int q[ND * ND], tp[ND], fr[ND];   bool vis[ND];   void add(int x, int y, int v) {     e[++tot] = (edge){y, head[x], 1, v};     head[x] = tot;     e[++tot] = (edge){x, head[y], 0, v};     head[y] = tot;   }   int times = 0;   bool bfs() {     for (int i = (int)(1); i <= (int)(T); i++) dis[i] = 2333, vis[i] = 0;     int h = 0, t = 1;     q[1] = S;     dis[S] = 1;     while (h != t) {       int x = q[++h];       vis[x] = 0;       for (int i = head[x]; i; i = e[i].next)         if (dis[e[i].to] > dis[x] + e[i].v && e[i].f) {           dis[e[i].to] = dis[x] + e[i].v;           fr[e[i].to] = i;           if (!vis[e[i].to]) {             vis[e[i].to] = 1;             q[++t] = e[i].to;           }         }     }     if (dis[T] == 2333) return 0;     return 1;   }   int dfs(int x, int flow) {     if (x == T) return flow;     int k, rest = flow;     vis[x] = 1;     for (int &i = cur[x]; i; i = e[i].next)       if (e[i].f && !vis[e[i].to] && dis[e[i].to] == dis[x] + e[i].v) {         k = dfs(e[i].to, min(rest, e[i].f));         e[i].f -= k;         e[i ^ 1].f += k;         rest -= k;         if (!rest) break;       }     if (rest) dis[x] = -1;     vis[x] = 0;     return flow - rest;   }   struct fuckmdndandwzp {     int i, j, beg, cnt;   } Ans[5000005];   int ans_top;   const int OUT_SIZE = 100000000;   char out[OUT_SIZE];   int out_top;   void write(int x) {     if (!x) {       out[out_top++] =  0 ;       return;     }     static int a[15], top;     for (top = 0; x; a[++top] = x % 10, x /= 10)       ;     for (; top; out[out_top++] = a[top--] +  0 )       ;   }   void writespace(int x) {     write(x);     out[out_top++] =    ;   }   void writeln(int x) {     write(x);     out[out_top++] =   n ;   }   int edge[N][N];   bool visx[N], visy[N];   int main() {     scanf( %d%d%d , &n, &m, &B);     for (int i = (int)(1); i <= (int)(m); i++) scanf( %d , &co[i]);     for (int i = (int)(1); i <= (int)(n); i++) {       int cnt;       scanf( %d , &cnt);       while (cnt--) {         int x, y;         scanf( %d%d , &x, &y);         mp[i][x] = y;         sx[i] += y;         sy[x] += y;       }     }     for (int i = (int)(1); i <= (int)(n); i++) ans = max(ans, sx[i]);     for (int i = (int)(1); i <= (int)(m); i++) id[i] = i;     sort(id + 1, id + m + 1, cmp);     int p = m;     for (; p && B >= co[id[p]]; B -= co[id[p--]])       ;     for (int i = (int)(1); i <= (int)(p); i++) ans = max(ans, sy[id[i]]);     for (int i = (int)(p + 1); i <= (int)(m); i++) {       ans = max(ans, (sy[id[i]] + 1) / 2);       fl[id[i]] = 1;       int cnt = 0;       for (int j = (int)(1); j <= (int)(n); j++) cnt += (mp[j][id[i]] & 1);       cnt = (cnt + 1) / 2;       for (int j = (int)(1); j <= (int)(n); j++) {         int v = mp[j][id[i]];         mp[j][id[i]] = mp[j][id[i] + m] = v / 2;         if (v & 1) {           if ((--cnt) >= 0)             mp[j][id[i]]++;           else             mp[j][id[i] + m]++;         }       }     }     writeln(ans);     for (int i = (int)(1); i <= (int)(m); i++) write(fl[i]);     out[out_top++] =   n ;     m *= 2;     for (int i = (int)(1); i <= (int)(n); i++) sx[i] = 0;     for (int i = (int)(1); i <= (int)(m); i++) sy[i] = 0;     for (int i = (int)(1); i <= (int)(n); i++)       for (int j = (int)(1); j <= (int)(m); j++)         sx[i] += mp[i][j], sy[j] += mp[i][j];     int times = 0;     for (int TT = 0;;) {       bool flag = 0;       memset(head, 0, sizeof(head));       tot = 1;       S = n + m + 1;       T = S + 1;       for (int i = (int)(1); i <= (int)(n); i++)         for (int j = (int)(1); j <= (int)(m); j++)           if (mp[i][j]) {             edge[i][j] = tot + 1;             flag = 1;             add(i, j + n, 0);           }       if (!flag) break;       for (int i = (int)(1); i <= (int)(n); i++) add(S, i, sx[i] != ans);       for (int i = (int)(1); i <= (int)(m); i++) add(i + n, T, sy[i] != ans);       for (; bfs(); times++) {         for (int i = (int)(1); i <= (int)(T); i++) cur[i] = head[i], vis[i] = 0;         dfs(S, 1e9);       }       int mn = 1e9;       for (int i = (int)(1); i <= (int)(n); i++) visx[i] = 0;       for (int i = (int)(1); i <= (int)(m); i++) visy[i] = 0;       for (int i = (int)(1); i <= (int)(n); i++)         for (int j = (int)(1); j <= (int)(m); j++)           if (!e[edge[i][j]].f && mp[i][j])             mn = min(mn, mp[i][j]), visx[i] = visy[j] = 1;       for (int i = (int)(1); i <= (int)(n); i++)         if (!visx[i] && sx[i]) mn = min(mn, ans - sx[i]);       for (int i = (int)(1); i <= (int)(m); i++)         if (!visy[i] && sy[i]) mn = min(mn, ans - sy[i]);       for (int i = (int)(1); i <= (int)(n); i++)         for (int j = (int)(1); j <= (int)(m); j++)           if (!e[edge[i][j]].f && mp[i][j]) {             Ans[++ans_top] = (fuckmdndandwzp){i, j, TT, mn};             mp[i][j] -= mn;             sx[i] -= mn;             sy[j] -= mn;           }       TT += mn;       ans -= mn;     }     writeln(ans_top);     for (int i = (int)(1); i <= (int)(ans_top); i++) {       writespace(Ans[i].i);       writespace(Ans[i].j - (Ans[i].j <= m / 2 ? 0 : m / 2));       writespace(Ans[i].beg);       writeln(Ans[i].cnt);     }     fwrite(out, 1, out_top, stdout);   }


#include <bits/stdc++.h>   using namespace std;   int n, m, t;   pair<int, int> a[300005];   int b[300005];   int force(int ll, int rr) {     int r = 0;     for (int i = 1; i <= n; i++)       if (a[i].first >= ll && a[i].second <= rr) r++;     return r;   }   struct indexed_tree_2d {     vector<int> T[1000006];     void set(int first, int second) {       for (int i = first; i < 1000006; i += i & -i) T[i].push_back(second);     }     void end_update() {       for (int i = 1; i < 1000006; i++) sort(T[i].begin(), T[i].end());     }     int get(int first, int second) {       int r = 0;       for (int i = first; i >= 1; i -= i & -i)         r += upper_bound(T[i].begin(), T[i].end(), second) - T[i].begin();       return r;     }     int get(int x1, int y1, int x2, int y2) {       return get(x2, y2) - get(x1 - 1, y2) - get(x2, y1 - 1) +              get(x1 - 1, y1 - 1);     }   } tr;   int f(int ll, int rr) {     if (ll > rr) return 0;     return tr.get(ll, ll, rr, rr);   }   int main() {     scanf( %d%d , &n, &t);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &a[i].first, &a[i].second);     }     for (int i = 1; i <= n; i++) tr.set(a[i].first, a[i].second);     tr.end_update();     for (int i = 1; i <= t; i++) {       int answer = n;       scanf( %d , &m);       for (int j = 1; j <= m; j++) {         scanf( %d , &b[j]);       }       sort(b + 1, b + m + 1);       b[m + 1] = 1000006;       for (int j = 1; j <= m + 1; j++) answer -= f(b[j - 1] + 1, b[j] - 1);       printf( %d n , answer);     }   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, pair<int, int> > > v[100005];   vector<int> ans;   int n;   bool has_cycle(int mid) {     int indeg[100005];     int cnt = 0, x;     memset(indeg, 0, sizeof(indeg));     for (int i = 1; i <= n; ++i) {       for (auto g : v[i]) {         if (g.second.first > mid) {           indeg[g.first]++;         }       }     }     queue<int> q;     for (int i = 1; i <= n; ++i) {       if (indeg[i] == 0) {         q.push(i);       }     }     while (!q.empty()) {       x = q.front();       cnt++;       q.pop();       for (auto g : v[x]) {         if (g.second.first > mid) {           indeg[g.first]--;           if (indeg[g.first] == 0) q.push(g.first);         }       }     }     if (cnt == n)       return false;     else       return true;   }   void topological_sort(int res) {     int indeg[100005];     int cnt = 0, x;     memset(indeg, 0, sizeof(indeg));     for (int i = 1; i <= n; ++i) {       for (auto g : v[i]) {         if (g.second.first > res) {           indeg[g.first]++;         }       }     }     queue<int> q;     bool vis[100005];     memset(vis, false, sizeof(vis));     for (int i = 1; i <= n; ++i) {       if (indeg[i] == 0) q.push(i);     }     while (!q.empty()) {       x = q.front();       vis[x] = true;       q.pop();       for (auto g : v[x]) {         if (g.second.first > res) {           indeg[g.first]--;           if (indeg[g.first] == 0) q.push(g.first);         } else {           if (vis[g.first] == true) {             ans.push_back(g.second.second);           }         }       }     }   }   bool check(int mid) {     if (has_cycle(mid))       return false;     else       return true;   }   int binary_search() {     int lo = 0, hi = 1e9;     while (hi - lo > 1) {       int mid = (lo + hi) / 2;       if (check(mid))         hi = mid;       else         lo = mid;     }     if (check(lo)) return lo;     return hi;   }   int main() {     int m, x, y, c, res;     cin >> n >> m;     for (int i = 1; i <= m; i++) {       cin >> x >> y >> c;       v[x].push_back({y, {c, i}});     }     res = binary_search();     topological_sort(res);     cout << res <<     << (int)ans.size() <<   n ;     for (auto g : ans) cout << g <<    ;   }


#include <bits/stdc++.h>   int func(int num) {     if (num > 0)       return 1;     else if (num < 0)       return -1;     else       return num;   }   int main() {     char a[101], b[101];     int i;     scanf( %s , &a);     scanf( %s , &b);     for (i = 0; i < strlen(a); i++)       if (a[i] >=  A  && a[i] <=  Z ) a[i] +=  a  -  A ;     for (i = 0; i < strlen(b); i++)       if (b[i] >=  A  && b[i] <=  Z ) b[i] +=  a  -  A ;     printf( %d n , func(strcmp(a, b)));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int DEBUG = 0;   int main(int argc, char **argv) {     DEBUG = (argc >= 2) ? atoi(argv[1]) : 0;     int t;     cin >> t;     for (int c = 0; c < t; c++) {       char tmp;       int board[8][8];       int kx[2];       int ky[2];       int eger = 0;       for (int i = 0; i < 8; i++) {         for (int j = 0; j < 8; j++) {           cin >> tmp;           if (tmp ==  . ) {             board[i][j] = 1;           } else if (tmp ==  # ) {             board[i][j] = 0;           } else {             kx[eger] = i;             ky[eger] = j;             board[i][j] = 1;             eger++;           }         }       }       bool good = false;       if ((kx[0] - kx[1] + 12) % 4 == 0 && (ky[0] - ky[1] + 12) % 4 == 0) {         int v = (kx[0] + ky[0]) % 4;         for (int i = 0; i < 8; i++) {           for (int j = 0; j < 8; j++) {             if (board[i][j] == 1 && (i + j) % 4 == v) {               good = true;             }           }         }       }       if (good) {         cout <<  YES  << endl;       } else {         cout <<  NO  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int64_t mod = 1000000007;   int main() {     int n;     cin >> n;     int64_t ways = 1;     for (int i = 0; i < 3 * n; ++i) {       ways *= 3;       ways %= mod;     }     int64_t bad = 1;     for (int i = 0; i < n; ++i) {       bad *= 7;       bad %= mod;     }     cout << (mod + (ways - bad) % mod) % mod << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long A[100005 * 10];   long long B[100005 * 10];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long tst = 1;     for (long long tt = 1; tt <= tst; tt++) {       long long n, a, mn = 1e12, m, res = 0;       cin >> m;       for (long long i = 1; i <= m; i++) cin >> a, mn = min(mn, a);       cin >> n;       for (long long i = 0; i < n; i++) cin >> A[i];       ;       sort(A, A + n, greater<long long>());       long long p1 = 0;       while (p1 < n) {         long long x = p1 + mn;         while (p1 < x) res += A[p1], p1++;         p1 += 2;       }       cout << res << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N;   pair<pair<int, int>, int> V[100005];   vector<int> r;   bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {     if (a.first.first != b.first.first) return a.first.first > b.first.first;     return a.first.second > b.first.second;   }   int main() {     cin.sync_with_stdio(false);     cout.sync_with_stdio(false);     cin >> N;     for (int i = 1; i <= N; i++) cin >> V[i].first.first;     for (int i = 1; i <= N; i++) {       cin >> V[i].first.second;       V[i].second = i;     }     sort(V + 1, V + 1 + N, cmp);     r.push_back(V[1].second);     for (int i = 2; i <= N; i += 2) {       if (V[i].first.second > V[i + 1].first.second)         r.push_back(V[i].second);       else         r.push_back(V[i + 1].second);     }     cout << r.size() <<   n ;     for (auto it : r) cout << it <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using LL = long long;   constexpr int maxn = 100;   int a[maxn];   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int t;     for (cin >> t; t; t -= 1) {       int n, x, am = 0, ok = 0;       cin >> n >> x;       for (int a; n; n -= 1) {         cin >> a;         am = max(a, am);         if (a == x) ok = 1;       }       if (ok)         cout <<  1 n ;       else         cout << max(2, (x + am - 1) / am) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int caseno = 1;   typedef long long LL;   typedef unsigned long long ULL;   typedef long long int64;   typedef unsigned long long uint64;   const int SIZE = 100 + 10;   int ar[SIZE];   int main() {     std::ios_base::sync_with_stdio(0);     int tcases, I, J, K, N, n, m, cnt = 0, len, f = 0, x, D, t;     cin >> n;     for (I = 0; I < n; I++) {       cin >> ar[I];       f += ar[I];     }     n++;     D = 1;     t = f;     t += D;     I = 1;     x = 1;     while (1) {       x++;       if (I == n)         I = 1;       else         I++;       if (x == t) break;     }     if (I != 1) cnt++;     D = 2;     t = f;     t += D;     I = 1;     x = 1;     while (1) {       x++;       if (I == n)         I = 1;       else         I++;       if (x == t) break;     }     if (I != 1) cnt++;     D = 3;     t = f;     t += D;     I = 1;     x = 1;     while (1) {       x++;       if (I == n)         I = 1;       else         I++;       if (x == t) break;     }     if (I != 1) cnt++;     D = 4;     t = f;     t += D;     I = 1;     x = 1;     while (1) {       x++;       if (I == n)         I = 1;       else         I++;       if (x == t) break;     }     if (I != 1) cnt++;     D = 5;     t = f;     t += D;     I = 1;     x = 1;     while (1) {       x++;       if (I == n)         I = 1;       else         I++;       if (x == t) break;     }     if (I != 1) cnt++;     cout << cnt <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 2147483647;   const long long LLINF = 9223372036854775807LL;   int digsum(long long x) {     int ans = 0;     do {       ans += x % 10;       x /= 10;     } while (x);     return ans;   }   int main() {     int a, b, c;     cin >> a >> b >> c;     vector<int> v;     for (int sum = 1; sum <= 100; ++sum) {       long long curres = b;       for (int iter = 0; iter < a; ++iter) curres = curres * sum;       curres += c;       if (digsum(curres) == sum && curres > 0 && curres < 1000000000)         v.push_back(curres);     }     sort(v.begin(), v.end());     printf( %d n , int((v).size()));     for (int i = 0; i < int((v).size()); ++i) printf( %d  , v[i]);     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:671088640 )   using namespace std;   const int MOD = 1000000007;   const int INF = 20000000;   const long double EPS = 1e-7;   const int HASH_POW = 29;   const long double PI = acos(-1.0);   mt19937_64 rnd(1);   double workTime() { return double(clock()) / CLOCKS_PER_SEC; }   void my_return(int code) { exit(code); }   int bin_pow(int a, int n) {     int ans = 1;     while (n) {       if (n & 1) ans = ans * 1ll * a % MOD;       n >>= 1;       a = a * 1ll * a % MOD;     }     return ans;   }   int f[100010], rev[100010];   int C(int n, int k) {     if (k < 0 || k > n) return 0;     int res = f[n] * 1ll * rev[k] % MOD;     return res * 1ll * rev[n - k] % MOD;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     f[0] = 1;     for (int i = 1; i <= 100000; ++i) f[i] = f[i - 1] * 1ll * i % MOD;     for (int i = 0; i <= 100000; ++i) rev[i] = bin_pow(f[i], MOD - 2);     int f, w, h;     scanf( %d %d %d , &f, &w, &h);     if (w == 0) {       printf( 1 n );       my_return(0);     } else if (f == 0) {       if (w > h) {         printf( 1 n );         my_return(0);       } else {         printf( 0 n );         my_return(0);       }     } else if (w <= h) {       printf( 0 n );       my_return(0);     }     int total = 0, good = 0;     for (int i = 1; i <= w; ++i) {       if (i > 1) total = (total + C(w - 1, i - 1) * 1ll * C(f - 1, i - 2)) % MOD;       total = (total + 2ll * C(w - 1, i - 1) * C(f - 1, i - 1)) % MOD;       total = (total + C(w - 1, i - 1) * 1ll * C(f - 1, i)) % MOD;     }     for (int i = 1; i <= w; ++i) {       if (i * (h + 1) > w) break;       int w1 = w - i * h;       if (i > 1) good = (good + C(w1 - 1, i - 1) * 1ll * C(f - 1, i - 2)) % MOD;       good = (good + 2ll * C(w1 - 1, i - 1) * C(f - 1, i - 1)) % MOD;       good = (good + C(w1 - 1, i - 1) * 1ll * C(f - 1, i)) % MOD;     }     printf( %d n , good * 1ll * bin_pow(total, MOD - 2) % MOD);     my_return(0);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int tst;     cin >> tst;     while (tst--) {       int i, j, k, l, m, n;       cin >> n;       string s;       cin >> s;       set<int> ze, on;       for (i = 0; i < n; i++) {         if (s[i] ==  1 )           on.insert(i);         else           ze.insert(i);       }       int ans[n + 10];       memset(ans, 0, sizeof ans);       int st = 1;       j = 0;       int ya = 0, kel, pt;       st = 0;       for (i = 0; i < n; i++) {         if (ans[i]) continue;         if (on.empty() || ze.empty()) break;         st++;         if (s[i] ==  0 ) {           kel = i;           pt = 0;           while (1) {             if (pt == 0) {               auto x = on.lower_bound(kel);               ans[kel] = st;               ze.erase(kel);               if (x == on.end()) {                 break;               } else {                 l = *x;                 ans[l] = st;                 on.erase(l);                 pt = 1 - pt;                 kel = l;               }             } else {               auto x = ze.lower_bound(kel);               ans[kel] = st;               on.erase(kel);               if (x == ze.end()) {                 break;               } else {                 l = *x;                 ans[l] = st;                 ze.erase(l);                 pt = 1 - pt;                 kel = l;               }             }           }         } else {           pt = 0;           kel = i;           while (1) {             if (pt == 0) {               auto x = ze.lower_bound(kel);               ans[kel] = st;               on.erase(kel);               if (x == ze.end()) {                 break;               } else {                 l = *x;                 ans[l] = st;                 ze.erase(l);                 pt = 1 - pt;                 kel = l;               }             } else {               auto x = on.lower_bound(kel);               ans[kel] = st;               ze.erase(kel);               if (x == on.end()) {                 break;               } else {                 l = *x;                 ans[l] = st;                 on.erase(l);                 pt = 1 - pt;                 kel = l;               }             }           }         }       }       st++;       if (on.size()) {         for (auto x : on) ans[x] = st, st++;       } else if (ze.size()) {         for (auto x : ze) ans[x] = st, st++;       }       set<int> kar;       for (i = 0; i < n; i++) kar.insert(ans[i]);       cout << kar.size() <<   n ;       for (i = 0; i < n; i++) cout << ans[i] <<    ;       cout <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   unsigned long long int t, k, x;   int main() {     ios_base::sync_with_stdio(0);     cin >> t;     for (; t > 0; t--) {       cin >> k >> x;       cout << (k - 1) * 9 + x << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int a[60000], x, mn = 0x3f3f3f3f;   int main() {     int n, m;     cin >> m >> n;     for (int i = 1; i <= n; i++) {       cin >> x;       a[x]++;     }     sort(a + 1, a + m + 1);     cout << a[1];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 4e4 + 10;   const int inf = 1e9;   int t;   int n;   int b[N], ans[N];   void solve() {     cin >> n;     for (int i = 0; i < n; i++) cin >> b[i];     long long tot = 0;     for (int i = 0; i < n; i++) tot += b[i];     if (tot % (n * (n + 1) / 2) != 0) {       cout <<  NO n ;       return;     }     tot /= n * (n + 1) / 2;     for (int i = 0; i < n; i++) {       long long tmp = b[(i - 1 + n) % n] - b[i] + tot;       if (tmp <= 0 || tmp % n != 0 || tmp / n > inf) {         cout <<  NO n ;         return;       }       ans[i] = tmp / n;     }     cout <<  YES n ;     for (int i = 0; i < n; i++) cout << ans[i] <<    ;     cout << endl;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long Max = 1e5 + 10;   int T;   long long n, m;   long long number[Max];   int main() {     cin >> T;     while (T--) {       cin >> n >> m;       long long ans = 1;       int k = 1;       for (int i = 1;; i *= 2) {         number[k] = number[k - 1] + i;         if (number[k] > n) {           ans = (ans * (n - number[k - 1] + 1) % m) % m;           break;         } else {           ans = ans * (i + 1) % m;         }         k++;       }       cout << (ans - 1 + m) % m << endl;     }     return 0;   }


#include <bits/stdc++.h>   const double eps = 1e-10;   const float epsf = 1e-6;   using namespace std;   inline long long int __gcd(long long int a, long long int b) {     if (a == 0 || b == 0) {       return max(a, b);     }     long long int tempa, tempb;     while (1) {       if (a % b == 0)         return b;       else {         tempa = a;         tempb = b;         a = tempb;         b = tempa % tempb;       }     }   }   inline int compfloat(float& x, float& y) {     if (fabs(x - y) < epsf)       return 0;     else if (x - y > 0)       return 1;     return -1;   }   inline int compdouble(double x, double y) {     if (fabs(x - y) < eps)       return 0;     else if (x - y > 0)       return 1;     else       return -1;   }   bool prime(long long int k) {     for (long long int i = 2; i * i <= k; i++)       if (k % i == 0) {         return false;       }     return true;   }   void pdash(int n = 1) {     for (int i = 0; i < n; i++) {       for (int j = 0; j < 30; j++) {         cout <<  - ;       }       cout <<   n ;     }   }   long long int power(long long int x, long long int y) {     long long int result = 1;     while (y > 0) {       if (y & 1) {         result = (result * x);       }       y = y >> 1;       x = (x * x);     }     return result;   }   long long int power(long long int x, long long int y, long long int z) {     long long int result = 1;     x = x % z;     while (y > 0) {       if (y & 1) {         result = (result * x) % z;       }       y = y >> 1;       x = (x * x) % z;     }     return result;   }   long long int modInverse(long long int n, long long int p) {     return power(n, p - 2, p);   }   long long int nCrF(long long int n, long long int r, long long int p) {     if (r == 0) return 1;     long long int f[n + 1];     f[0] = 1;     for (long long int i = 1; i <= n; i++) f[i] = f[i - 1] * i % p;     return (f[n] * modInverse(f[r], p) % p * modInverse(f[n - r], p) % p) % p;   }   void cordinate_compression(vector<int>& v) {     vector<int> p = v;     sort(p.begin(), p.end());     p.erase(unique(p.begin(), p.end()), p.end());     for (int i = 0; i < (int)((v).size()); i++)       v[i] = (int)(lower_bound(p.begin(), p.end(), v[i]) - p.begin());   }   const int MX = 1400009;   vector<int> used(MX, 0);   vector<int> factor[MX];   vector<int> isprime(MX, 1);   vector<bool> is_used(MX, 0);   inline bool check(int index) {     for (auto& j : factor[index]) {       if (is_used[j]) return 0;     }     for (auto& j : factor[index]) {       is_used[j] = 1;     }     return 1;   }   void solve() {     int n;     cin >> n;     int a[n];     for (int i = 2; i < MX; i++) {       if (isprime[i]) {         for (int j = i; j < MX; j += i) {           factor[j].push_back(i);           isprime[j] = 0;         }       }     }     int curr = 2;     bool bz = 1;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (bz) {         curr = a[i];         while (!check(curr)) curr++;         if (curr != a[i]) {           bz = 0;         }         cout << curr <<    ;         curr = 2;       } else {         while (!check(curr)) curr++;         cout << curr <<    ;       }     }     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   double prec = 0.00000001;   void calcHistory(vector<double>& history, int hp2, double p1, int l1, int h1) {     double h[201];     double newh[201];     memset(h, 0, sizeof(h));     memset(newh, 0, sizeof(newh));     h[hp2] = 1.;     for (int i = 0; i < history.size(); ++i) {       for (int j = 1; j <= hp2; ++j)         if (h[j] > 0) {           double pHit = (1. - p1) / (h1 - l1 + 1.);           for (int idx = l1; idx <= h1; ++idx) {             int nIdx = j - idx > 0 ? j - idx : 0;             newh[nIdx] += pHit * h[j];           }           newh[j] += p1 * h[j];         }       history[i] = newh[0];       newh[0] = 0;       for (int idx = 0; idx <= 200; ++idx)         if (newh[idx] > prec)           h[idx] = newh[idx];         else           h[idx] = 0.;       memset(newh, 0, sizeof(newh));     }   }   int main(int argc, char* argv[]) {     stringstream strStr;     int hp1;     int dt1;     int l1;     int h1;     int pp1;     double p1;     int hp2;     int dt2;     int l2;     int h2;     int pp2;     double p2;     cin >> hp1;     cin >> dt1;     cin >> l1;     cin >> h1;     cin >> pp1;     cin >> hp2;     cin >> dt2;     cin >> l2;     cin >> h2;     cin >> pp2;     strStr >> hp1;     strStr >> dt1;     strStr >> l1;     strStr >> h1;     strStr >> pp1;     strStr >> hp2;     strStr >> dt2;     strStr >> l2;     strStr >> h2;     strStr >> pp2;     p1 = double(pp1) / 100.;     p2 = double(pp2) / 100.;     vector<double> history(5000, 0.);     calcHistory(history, hp2, p1, l1, h1);     double res = 0;     double h[201];     double newh[201];     memset(h, 0, sizeof(h));     memset(newh, 0, sizeof(newh));     h[hp1] = 1.;     int k = 0;     double p = 0.;     double pp = 0.;     for (int i = 0; i < history.size(); ++i) {       double histP1 = history[i];       while (dt2 * k < dt1 * i) {         ++k;         for (int j = 1; j <= hp1; ++j)           if (h[j] > 0) {             double pHit = (1. - p2) / (h2 - l2 + 1.);             for (int idx = l2; idx <= h2; ++idx) {               int nIdx = j - idx > 0 ? j - idx : 0;               newh[nIdx] += pHit * h[j];             }             newh[j] += p2 * h[j];           }         pp += newh[0];         for (int idx = 0; idx <= 200; ++idx)           if (newh[idx] > prec)             h[idx] = newh[idx];           else             h[idx] = 0.;         memset(newh, 0, sizeof(newh));       }       res += histP1 * (1. - pp);       p += histP1;     }     cout << showpoint << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   long long l[200020], r[200020];   struct node {     long long x, y, p;   } a[200020];   long long ans[200020];   struct bridge {     long long p, v;   };   struct Compare {     bool operator()(const bridge& a, const bridge& b) const {       if (a.v < b.v || (a.v == b.v && a.p < b.p))         return true;       else         return false;     }   };   set<bridge, Compare> b;   bool cmp(node a, node b) {     if (a.x > b.x) return true;     if (a.x < b.x) return false;     if (a.y > b.y) return true;     return false;   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %lld%lld , &l[i], &r[i]);     long long j;     for (int i = 1; i <= m; i++) {       bridge tmp;       scanf( %lld , &j);       tmp.v = j;       tmp.p = i;       b.insert(tmp);     }     if (m < n - 1) {       printf( No n );       return 0;     }     for (int i = 1; i < n; i++) {       a[i].x = l[i + 1] - r[i];       a[i].y = r[i + 1] - l[i];       a[i].p = i;     }     sort(a + 1, a + n, cmp);     bool ok = true;     set<bridge, Compare>::iterator iter;     bridge tmp;     for (int i = 1; i < n; i++) {       tmp.v = a[i].y + 1;       tmp.p = -1;       iter = b.lower_bound(tmp);       if (iter == b.begin()) {         ok = false;         break;       }       iter--;       if (iter == b.end() || iter->v < a[i].x) {         ok = false;         break;       } else {         ans[a[i].p] = iter->p;         b.erase(iter);       }     }     if (ok) {       printf( Yes n );       for (int i = 1; i < n; i++) printf( %lld  , ans[i]);     } else       printf( No n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[111];   int main() {     int mx = 1000000000;     int cnt, n;     scanf( %d , &n);     int sum = 0;     cnt = 0;     int k = 0;     for (int i = 1; i < n * 2; ++i) {       scanf( %d , &a[i]);       sum += abs(a[i]);       mx = ((mx) < (abs(a[i])) ? (mx) : (abs(a[i])));       if (a[i] < 0) {         cnt++;       }       if (a[i] == 0) k++;     }     if (cnt % 2 == 0 || n % 2 == 1 || k != 0)       printf( %d n , sum);     else {       printf( %d n , sum - mx - mx);     }   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   char a[1010], b[1010];   int h = 0;   int main() {     scanf( %s , a);     int l = strlen(a);     int j = 0;     for (int i = 0; i < l; i++) {       b[j++] = a[i];       if (a[i] ==  > ) {         b[j] = 0;         if (b[1] ==  / ) h--;         for (int k = 0; k < 2 * h; k++) putchar(   );         if (b[1] !=  / ) h++;         puts(b);         j = 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   default_random_engine generator;   uniform_int_distribution<int> distribution(1, INT_MAX);   long long my_rand(long long a = 0, long long b = INT_MAX) {     return a + distribution(generator) % b;   }   inline int mul(int a, int b) { return int(a * 1ll * b % 998244353); }   inline int norm(int a) {     if (a >= 998244353) a -= 998244353;     if (a < 0) a += 998244353;     return a;   }   inline int binPow(int a, int k) {     int ans = 1;     while (k > 0) {       if (k & 1) ans = mul(ans, a);       a = mul(a, a);       k >>= 1;     }     return ans;   }   inline int subtract(int a, int b) { return norm(a - b); }   inline int sum(int a, int b) { return int((a + b) % 998244353); }   inline int inv(int a) { return binPow(a, 998244353 - 2); }   long long ceil_int(long long a, long long b) { return a / b + (a % b != 0); }   long long gcd(long long a, long long b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }   int main() {     generator.seed(chrono::system_clock::now().time_since_epoch().count());     cin.sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     vector<int> a(n);     int sum = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];       sum += a[i];     }     int res = sum;     sort(a.begin(), a.end());     for (int i = 1; i <= 100; i++) {       int cur = sum;       for (int j = n - 1; j > 0; j--) {         if (a[j] % i == 0) {           cur += ((a[j] / i) - a[j]);           cur += (a[0] * i - a[0]);         }         res = min(res, cur);         cur = sum;       }     }     cout << res;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000010;   struct node {     int x, id;   } a[N], ans[N];   int anss;   bool cmp(node x, node y) {     if (x.x != y.x) return x.x > y.x;     return x.id < y.id;   }   void solve() {     int k = a[2].x / a[3].x;     while (k) {       if (k & 1) {         a[2].x -= a[3].x;         a[3].x <<= 1;         ans[++anss].x = a[3].id;         ans[anss].id = a[2].id;       } else {         a[1].x -= a[3].x;         a[3].x <<= 1;         ans[++anss].x = a[3].id;         ans[anss].id = a[1].id;       }       k >>= 1;     }   }   int n;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) a[i].id = i, scanf( %d , &a[i].x);     sort(a + 1, a + n + 1, cmp);     if (!a[2].x) {       puts( -1 );       return 0;     }     while (a[3].x) {       solve();       sort(a + 1, a + n + 1, cmp);     }     printf( %d n , anss);     for (int i = 1; i <= anss; ++i) printf( %d %d n , ans[i].x, ans[i].id);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 59;   const long long mod = 998244353;   int add[maxn];   long long w[maxn];   long long dp[52][52][52];   long long val[52][52][52];   bool vis[52][52][52];   long long Pow(long long a, long long b) {     long long res = 1;     a %= mod;     while (b) {       if (b & 1) res = res * a % mod;       b >>= 1;       a = a * a % mod;     }     return res;   }   inline long long inv(long long a) { return Pow(a, mod - 2); }   int flag;   long long dfs(int step, long long p, int i, int j, int k, long long x,                 long long y, long long z) {     if (step == 0) return p * x % mod;     if (vis[i][j][k]) {       return val[i][j][k] * inv(dp[i][j][k]) % mod * p % mod;     }     dp[i][j][k] = p;     val[i][j][k] += dfs(step - 1, p * x % mod * inv(x + y + z) % mod, i + 1, j, k,                         max(0ll, x + flag), y, z);     val[i][j][k] += dfs(step - 1, p * y % mod * inv(x + y + z) % mod, i, j + 1, k,                         x, y + 1, z);     val[i][j][k] += dfs(step - 1, p * z % mod * inv(x + y + z) % mod, i, j, k + 1,                         x, y, max(0ll, z - 1));     val[i][j][k] %= mod;     vis[i][j][k] = 1;     return val[i][j][k];   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     long long ori, s0 = 0, s1 = 0;     for (int i = 1; i <= n; i++) scanf( %d , add + i);     for (int i = 1; i <= n; i++) {       scanf( %lld , w + i);       if (add[i])         s1 += w[i];       else         s0 += w[i];     }     ori = s0 + s1;     int x, y, z, step;     for (int i = 1; i <= n; i++) {       memset(dp, 0, sizeof dp);       memset(val, 0, sizeof val);       memset(vis, 0, sizeof vis);       if (add[i])         flag = 1, val[0][0][0] = dfs(m, 1, 0, 0, 0, w[i], s1 - w[i], s0);       else         flag = -1, val[0][0][0] = dfs(m, 1, 0, 0, 0, w[i], s1, s0 - w[i]);       printf( %lld n , val[0][0][0]);     }   }


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


#include <bits/stdc++.h>   using namespace std;   using LL = long long int;   template <class TH>   void _dbg(const char *sdbg, TH h) {     cerr << sdbg <<  =  << h <<   n ;   }   template <class TH, class... TA>   void _dbg(const char *sdbg, TH h, TA... a) {     while (*sdbg !=  , ) cerr << *sdbg++;     cerr <<  =  << h <<  , ;     _dbg(sdbg + 1, a...);   }   template <class T>   ostream &operator<<(ostream &os, vector<T> V) {     os <<  [ ;     for (auto vv : V) os << vv <<  , ;     return os <<  ] ;   }   template <class L, class R>   ostream &operator<<(ostream &os, pair<L, R> P) {     return os <<  (  << P.st <<  ,  << P.nd <<  ) ;   }   const int MAXN = 3003;   string t, s;   int n, m;   LL dp[MAXN][MAXN];   LL mod = 998244353;   bool vis[MAXN][MAXN];   LL dfs(int i, int j) {     (i, j);     if (j == i - 1) {       return 1;     }     if (vis[i][j]) {       return dp[i][j];     }     vis[i][j] = true;     char last = s[(j - i) + 1];     LL actRes = 0;     if (i > m || last == t[i]) {       actRes += dfs(i + 1, j);       actRes %= mod;     }     if (j > m || last == t[j]) {       actRes += dfs(i, j - 1);       actRes %= mod;     }     dp[i][j] = actRes;     return dp[i][j];   }   int main() {     cin >> s >> t;     n = s.size();     m = t.size();     s =  #  + s;     t =  #  + t;     LL res = dfs(1, n);     res = 0;     for (int i = m; i <= n; ++i) {       res += dp[1][i];       res %= mod;     }     cout << res << endl;   }


#include <bits/stdc++.h>   int main() {     std::ios::sync_with_stdio(false);     int n;     long long m, k;     long long tmp;     std::cin >> n >> m >> k;     std::vector<long long> a;     for (int i = 0; i < n; i++) {       std::cin >> tmp;       a.push_back(tmp);     }     std::sort(a.begin(), a.end());     std::cout << (m / (k + 1)) * (*(a.end() - 2)) +                      (m - (m / (k + 1))) * (*(a.end() - 1));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long oo = 1000000000000000000;   long long sum, ans = 0, mx = 0, mn = 1000000000, num, pos;   vector<int> v[100001];   long long int a[100001], mxx[100001];   map<int, int> mp[100001];   void unin(int x, int y) {     if (mp[x].size() < mp[y].size()) {       mp[x].swap(mp[y]);       mxx[x] = mxx[y];       a[x] = a[y];     }     for (map<int, int>::iterator it = mp[y].begin(); it != mp[y].end(); it++) {       mp[x][(it)->first] += (it)->second;       if (mp[x][it->first] == mxx[x]) {         a[x] += it->first;       } else if (mp[x][it->first] > mxx[x]) {         mxx[x] = mp[x][it->first];         a[x] = it->first;       }     }   }   void dfs(int u, int pr = -1) {     for (int i = 0; i < v[u].size(); i++) {       int to = v[u][i];       if (to == pr) continue;       dfs(to, u);       unin(u, to);     }   }   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> num;       mp[i][num]++;       mxx[i] = 1;       a[i] = num;     }     int x, y;     for (int i = 1; i < n; i++) {       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     dfs(1);     for (int i = 1; i <= n; i++) {       num = 0;       cout << a[i] <<    ;       sum = 0;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<int> a(6);     int s = 0;     for (int i = 0; i < 6; i++) {       cin >> a[i];       s += a[i];     }     if (s % 2 != 0) {       cout <<  NO ;       return 0;     }     for (int i = 0; i < 6; i++)       for (int j = i + 1; j < 6; j++)         for (int k = j + 1; k < 6; k++)           if (s / 2 == a[i] + a[j] + a[k]) {             cout <<  YES ;             return 0;           }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long ans = 0;     int n, m, k = 0;     scanf( %d %d , &n, &m);     vector<int> v(1e7);     for (int i = 0; i < n; i++) {       scanf(  %d , &k);       ans += v[(k ^ m)];       v[k]++;     }     printf( %I64d , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     char s[101];     int n;     cin >> n;     cin >> s;     if (n == 1 && s[0] ==  0 ) {       cout << 0;       return 0;     }     cout << 1;     for (int i = 0; i < n; i++)       if (s[i] ==  0 ) cout << 0;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <size_t N, size_t M>   struct Suffix_Automata {     int maxlen[N], trans[N][M], link[N], Size, Last, isclone[N];     Suffix_Automata() { Size = Last = 1; }     void Extend(int id) {       int cur = (++Size), p;       maxlen[cur] = maxlen[Last] + 1;       for (p = Last; p && !trans[p][id]; p = link[p]) trans[p][id] = cur;       if (!p)         link[cur] = 1;       else {         int q = trans[p][id];         if (maxlen[q] == maxlen[p] + 1)           link[cur] = q;         else {           int clone = (++Size);           maxlen[clone] = maxlen[p] + 1;           memcpy(trans[clone], trans[q], sizeof(trans[q]));           link[clone] = link[q];           for (; p && trans[p][id] == q; p = link[p]) trans[p][id] = clone;           link[cur] = link[q] = clone;           isclone[clone] = true;         }       }       isclone[cur] = false;       Last = cur;     }     int cnt[N], in[N];     vector<int> G[N];     long long calc() {       long long res = 0;       for (int i = 1; i <= Size; i++) cnt[i] = !isclone[i];       for (int i = 1; i <= Size; i++) {         G[i].clear();         G[i].push_back(link[i]);         ++in[link[i]];       }       queue<int> Q;       for (int i = 1; i <= Size; i++) {         if (!in[i]) {           Q.push(i);         }       }       while (!Q.empty()) {         int u = Q.front();         Q.pop();         for (int v : G[u]) {           cnt[v] += cnt[u];           if (!(--in[v])) {             Q.push(v);           }         }       }       for (int i = 1; i <= Size; i++) {         res += 1LL * cnt[i] * cnt[i] * (maxlen[i] - maxlen[link[i]]);       }       return res;     }   };   Suffix_Automata<300000, 26> SAM;   char s[300000];   int main() {     int T;     scanf( %d , &T);     for (int _ = 0; _ < T; _++) {       memset(SAM.trans, 0, sizeof(SAM.trans));       SAM.Last = SAM.Size = 1;       scanf( %s , s + 1);       int n = strlen(s + 1);       for (int i = 1; i <= n; i++) {         SAM.Extend(s[i] -  a );       }       printf( %lld n , SAM.calc());     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   string s, t, a;   void dfs(int i, int c4, int c7, bool d) {     if (c4 + n - i < n / 2 || c7 + n - i < n / 2) return;     if (i == n) {       a = t;       return;     }     if (d) {       t[i] =  4 ;       dfs(i + 1, c4 + 1, c7, 1);       if (a !=   ) return;       t[i] =  7 ;       dfs(i + 1, c4, c7 + 1, 1);     } else {       if (s[i] <=  4 ) {         t[i] =  4 ;         dfs(i + 1, c4 + 1, c7, s[i] <  4 );         if (a !=   ) return;       }       if (s[i] <=  7 ) {         t[i] =  7 ;         dfs(i + 1, c4, c7 + 1, s[i] <  7 );       }     }   }   int main() {     cin >> s;   restart:     if (s.size() % 2) s =  0  + s;     n = s.size();     t = string(n,  0 );     dfs(0, 0, 0, 0);     if (a ==   ) {       s =  00  + s;       goto restart;     } else {       cout << a << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   void read(int &k) {     k = 0;     char x = getchar();     while (x <  0  || x >  9 ) x = getchar();     while (x >=  0  && x <=  9 ) {       k = k * 10 - 48 + x;       x = getchar();     }   }   int n, a[55], K;   long double dp[55][55][55], ans, fac[55];   int main() {     scanf( %d , &n);     int sum = 0;     for (int i = 0; i < n; i++) scanf( %d , &a[i]), sum += a[i];     scanf( %d , &K);     if (sum <= K) {       printf( %.9lf n , double(n));       return 0;     }     for (int i = 0; i < n; i++) fac[0] = 1;     for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i;     for (int pos = 0; pos < n; pos++) {       memset(dp, 0, sizeof dp);       dp[0][0][0] = 1;       for (int i = 0; i < n; i++) {         if (i == pos) {           memcpy(dp[i + 1], dp[i], sizeof dp[i]);           continue;         }         for (int j = 0; j <= i; j++)           for (int k = 0; k <= K; k++) {             if (k + a[i] <= K) dp[i + 1][j + 1][k + a[i]] += dp[i][j][k];             dp[i + 1][j][k] += dp[i][j][k];           }       }       for (int i = 1; i <= n; i++)         for (int j = max(K - a[pos] + 1, 1); j <= K; j++)           ans += ((long double)dp[n][i][j]) * fac[i] * fac[n - i - 1] * i;     }     ans /= fac[n];     printf( %.9lf n , double(ans));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ok[101010];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     vector<int> ct(n);     int tl = 0;     int nn = 0;     int odd = 0;     for (int i = 0; i < n; i++) {       cin >> ct[i];       tl += ct[i];       if (ct[i] % 2 == 1) odd++;       if (ct[i] > 0) {         nn++;         for (int j = 1; j <= ct[i]; j++) {           if (ct[i] % j == 0) {             ok[j]++;           }         }       }     }     if (nn < 2) {       cout << tl << endl;       for (int i = 0; i < n; i++) {         for (int j = 0; j < ct[i]; j++) {           cout << (char)( a  + i);         }       }       cout << endl;     } else if (odd > 1) {       cout << 0 << endl;       for (int i = 0; i < n; i++) {         for (int j = 0; j < ct[i]; j++) {           cout << (char)( a  + i);         }       }       cout << endl;     } else {       int bv = 1;       for (int i = 1; i <= tl; i++) {         if (ok[i] == nn) {           int x = i;           int o = 0;           for (int ii = 0; ii < n; ii++) {             ct[ii] /= x;             if (ct[ii] % 2 == 1) o++;           }           if (o < 2 || (i % 2 == 0)) {             bv = max(bv, i);           }           for (int ii = 0; ii < n; ii++) {             ct[ii] *= x;           }         }       }       cout << bv << endl;       tl /= bv;       string r(tl,    );       int of = 0;       for (int ii = 0; ii < n; ii++) {         ct[ii] /= bv;         if (ct[ii] % 2 == 1) of = 1;       }       if (bv % 2 == 0) {         int i2 = 0;         for (int i = 0; i < (int)ct.size(); i++) {           for (int j = 0; j < ct[i]; j++) {             r[i2++] = (char)( a  + i);           }         }       } else {         int i2 = 0;         if (of) {           for (int i = 0; i < (int)ct.size(); i++) {             if (ct[i] % 2 == 1) {               for (int j = 0; j < ct[i]; j += 2) {                 r[tl / 2 - i2] = (char)( a  + i);                 r[tl / 2 + i2] = (char)( a  + i);                 i2++;               }             }           }           for (int i = 0; i < (int)ct.size(); i++) {             if (ct[i] % 2 == 0) {               for (int j = 0; j < ct[i]; j += 2) {                 r[tl / 2 - i2] = (char)( a  + i);                 r[tl / 2 + i2] = (char)( a  + i);                 i2++;               }             }           }         } else {           for (int i = 0; i < (int)ct.size(); i++) {             if (ct[i] % 2 == 0) {               for (int j = 0; j < ct[i]; j += 2) {                 r[tl / 2 - i2 - 1] = (char)( a  + i);                 r[tl / 2 + i2] = (char)( a  + i);                 i2++;               }             }           }         }       }       string v =   ;       for (int i = 0; i < bv; i++) {         v = v + r;         reverse(r.begin(), r.end());       }       cout << v << endl;     }   }


#include <bits/stdc++.h>   #pragma GCC optimize ( Ofast ,  unroll-loops )   using namespace std;   #define ll long long   //#define int ll   #define FOR(i,a,b) for (int i = (a); i<(b); i++)   #define REP(i,n) FOR(i,0,n)   #define REP1(i,n) FOR(i,1,n+1)   #define RREP(i,n) for (int i=(n)-1; i>=0; i--)   #define f first   #define s second   #define pb push_back   #define ALL(x) x.begin(),x.end()   #define SZ(x) (int)(x.size())   #define SQ(x) (x)*(x)   #define pii pair<int, int>   #define pdd pair<double ,double>   #define pcc pair<char, char>    #define endl   n    //#define TOAD   #ifdef TOAD   #define bug(x) cerr<<__LINE__<< :  <<#x<<  is  <<x<<endl   #define IOS()   #else   #define bug(...)   #define IOS() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)   #endif   const ll inf = 1ll<<60;   const int iinf=2147483647;   const ll mod = 1e9+7;   const ll maxn=1e7+5;   const double PI=acos(-1);   ll pw(ll x, ll p){       ll ret=1;       while (p>0){           if (p&1){               ret*=x;               ret%=mod;           }           x*=x;           x%=mod;           p>>=1;       }       return ret;   }      ll inv(ll a){       return pw(a,mod-2);       }      signed main(){       IOS();       int t; cin>>t;       while(t--){           int n; cin>>n;           vector<pii> vc(n);           vector<int> t(n);           REP(i,n) cin>>vc[i].f>>vc[i].s;           REP(i,n) cin>>t[i];           int cur=vc[0].f, ans=0;           REP(i,n){               cur+=t[i];               if (i==n-1) break;               int ex=(vc[i].s-vc[i].f)/2+((vc[i].s-vc[i].f)%2>0);               cur+=ex;               cur=max(cur, vc[i].s);               cur+=vc[i+1].f-vc[i].s;              }           cout<<cur<<endl;       }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   int main() {     ios::sync_with_stdio(false);     int t;     cin >> t;     while (t--) {       long long x, y, z;       cin >> x >> y >> z;       long long sum = x + y + z;       cout << (sum / 2) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-9;   const double PI = acos(-1.0);   const int MOD = 1000 * 1000 * 1000 + 7;   const int INF = 2000 * 1000 * 1000;   const int MAXN = 200010;   template <typename T>   inline T sqr(T n) {     return n * n;   }   int n, k;   vector<int> g[MAXN];   long long sum[MAXN][7];   long long dp[MAXN];   long long size[MAXN][7];   void dfs(int v, int par = -1) {     size[v][0] = 1;     for (int to : g[v]) {       if (to != par) {         dfs(to, v);         for (int mod = 1; mod <= k; mod++) {           size[v][mod % k] += size[to][mod - 1];           sum[v][mod % k] += sum[to][mod - 1];         }         sum[v][0] += size[to][k - 1];       }     }     for (int m2 = 0; m2 < k; m2++) {       long long summ2 = 0, sizem2 = 0;       for (int to : g[v]) {         if (to != par) {           summ2 += sum[to][m2];           sizem2 += size[to][m2];         }       }       for (int m1 = 0; m1 < k; m1++) {         for (int to : g[v]) {           if (to != par) {             long long osize = sizem2 - size[to][m2];             long long isize = size[to][m1];             dp[v] += sum[to][m1] * osize + (summ2 - sum[to][m2]) * isize;             dp[v] += isize * osize * ((m1 + m2 + 2 + k - 1) / k);           }         }       }     }     for (int m = 0; m < k; m++) {       dp[v] += 2 * (sum[v][m] + size[v][m] * (m != 0));     }     for (int to : g[v]) {       if (to != par) {         dp[v] += dp[to];       }     }   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 0; i < n - 1; i++) {       int u, v;       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     dfs(1);     printf( %lld n , dp[1] / 2);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 100;   int n, m, k;   long long sum[maxn], dp[maxn];   int d[maxn];   pair<int, int> p[maxn];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int cnt = 0;     while (cnt <= 1e9) {       ++cnt;     }     cin >> n >> m >> k;     for (int i = 1; i <= n; ++i) {       cin >> d[i];     }     sort(d + 1, d + 1 + n);     for (int i = 0; i < m; ++i) {       cin >> p[i].first >> p[i].second;     }     sum[0] = 0;     for (int i = 1; i <= k; ++i) {       sum[i] = sum[i - 1] + d[i];       dp[i] = 0;     }     for (int j = 1; j <= k; ++j) {       for (int i = 0; i < m; ++i) {         if (p[i].first <= j)           dp[j] =               max(dp[j], dp[j - p[i].first] + sum[j + p[i].second - p[i].first] -                              sum[j - p[i].first]);       }     }     long long ans = 0;     for (int i = 1; i <= k; ++i) {       ans = max(ans, dp[i]);     }     cout << sum[k] - ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void smn(T &a, const T &b) {     if (b < a) a = b;   }   template <class T>   inline void smx(T &a, const T &b) {     if (b > a) a = b;   }   template <class T>   inline T rev(const T &a) {     T _ = a;     reverse(_.begin(), _.end());     return _;   }   const double eps = 1e-9;   const long double leps = 1e-14;   int a, x, y;   int main(int argc, char *argv[]) {     ios_base::sync_with_stdio(false);     cin >> a >> x >> y;     if (y % a == 0) {       cout << -1 <<   n ;       return 0;     }     if (y < a) {       if (2 * abs(x) >= a)         cout << -1 <<   n ;       else         cout << 1 <<   n ;       return 0;     }     y -= a;     int t = y / a;     int now = 2 + 3 * (t / 2);     if (t % 2 == 1) now++;     if (t % 2 == 0) {       if (2 * abs(x) >= a)         cout << -1 <<   n ;       else         cout << now <<   n ;       return 0;     }     if (abs(x) >= a || x == 0)       cout << -1 <<   n ;     else if (x < 0)       cout << now <<   n ;     else       cout << now + 1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int ans[30], tans[30], flot[30];     memset(flot, 0, sizeof(flot));     memset(ans, 0x3f3f3f3f, sizeof(ans));     int n, num[55], top = 0, m;     string s, s1;     cin >> n;     cin >> s;     for (int i = 0; i < n; i++) {       if (s[i] ==  * )         num[top++] = i;       else {         flot[s[i] -  a ] = 1;         ans[s[i] -  a ] = 0;       }     }     cin >> m;     for (int i = 0; i < m; i++) {       int tt = 0;       memset(tans, 0, sizeof(tans));       cin >> s1;       for (int i = 0; i < top; i++) {         if (flot[s1[num[i]] -  a ] == 1) {           tt = 1;           break;         }         tans[s1[num[i]] -  a ]++;       }       for (int i = 0; i < n; i++) {         if (s[i] ==  * ) continue;         if (s[i] != s1[i]) {           tt = 1;           break;         }       }       if (tt) continue;       for (int i = 0; i < 30; i++) ans[i] = min(ans[i], tans[i]);     }     int je = 0;     for (int i = 0; i < 30; i++)       if (ans[i] != 0) je++;     cout << je << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, s, e;   int x[5555], a[5555], b[5555], c[5555], d[5555];   long long dist(int from, int to) {     return 1LL * abs(x[from] - x[to]) +            (from < to ? d[from] + a[to] : c[from] + b[to]);   }   int main() {     ios::sync_with_stdio(0);     cin >> n >> s >> e;     for (int i = 1; i <= n; ++i) {       cin >> x[i];     }     for (int i = 1; i <= n; ++i) {       cin >> a[i];     }     for (int i = 1; i <= n; ++i) {       cin >> b[i];     }     for (int i = 1; i <= n; ++i) {       cin >> c[i];     }     for (int i = 1; i <= n; ++i) {       cin >> d[i];     }     long long ans = dist(s, e);     list<int> path;     path.push_back(s);     path.push_back(e);     for (int i = 1; i <= n; ++i) {       if (i == s || i == e) continue;       list<int>::iterator best_it = path.begin();       long long next_ans = 1e18;       for (auto it = path.begin(); it != path.end(); ++it) {         if (*it == e) continue;         int from = *it;         auto next_iter = next(it);         int to = *next_iter;         long long current_ans =             ans - dist(from, to) + dist(from, i) + dist(i, to);         if (current_ans < next_ans) {           next_ans = current_ans;           best_it = next_iter;         }       }       path.insert(best_it, i);       ans = next_ans;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int ar[100001];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t, n, m;     cin >> t;     while (t--) {       cin >> n >> m;       long long val = n / m;       int a = m % 10, cnt = 0;       ar[cnt++] = a;       int b = 2;       while ((a * b) % 10 != a) {         ar[cnt++] = (a * b) % 10;         b++;       }       long long sum = 0;       for (int i = 0; i < cnt; i++) sum += ar[i];       long long ans = 0;       ans += sum * (val / cnt);       for (int i = 0; i < (val % cnt); i++) ans += ar[i];       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int a[500500], n, len, ans;   int main() {     scanf( %d , &n);     ans = 0;     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     for (int i = 0; i < n; i++) {       int k = i;       len = 0;       while (a[k + 1] != a[k] && k < n - 1) {         len++;         k++;       }       if (i != k) {         if (a[i] == a[k])           for (i++; i < k; i++) a[i] = a[k];         else {           int j;           for (j = 1; j <= (k - i) / 2; j++) a[j + i] = a[i];           for (; j + i < k; j++) a[j + i] = a[k];           i = k;         }       }       ans = (ans > len ? ans : len);     }     if (ans == n) ans--;     printf( %d n , ans / 2);     for (int i = 0; i < n; i++) {       if (i == n - 1)         cout << a[i] << endl;       else         cout << a[i] <<    ;     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int r1, c1, r2, c2;   char s[505][505];   bool dfs(int i, int j) {     if (r2 == i && c2 == j && s[r2][c2] !=  . ) return 1;     if (s[i][j] !=  . ) return 0;     s[i][j] =  X ;     return dfs(i + 1, j) || dfs(i - 1, j) || dfs(i, j + 1) || dfs(i, j - 1);   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) cin >> s[i][j];     cin >> r1 >> c1 >> r2 >> c2;     s[r1][c1] =  . ;     if (dfs(r1, c1))       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s, x;     cin >> s;     int p = 0, q = 0, r = 0, t = 0;     for (int i = 0; i < s.length() - 1; i++)       if (s[i] != s[i + 1]) x[t++] = s[i + 1];     if (t != 2)       cout <<  NO ;     else {       if (x[0] !=  b  || x[1] !=  c )         cout <<  NO ;       else {         for (int i = 0; i < s.length(); i++) {           if (s[i] ==  a ) p++;           if (s[i] ==  b ) q++;           if (s[i] ==  c ) r++;         }         if (q == 0)           cout <<  NO ;         else {           if (p == r || r == q)             cout <<  YES ;           else             cout <<  NO ;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   struct S {     int l, r, h;     S() {}     S(int l, int r, int h) : l(l), r(r), h(h) {}   } Ss[100001];   struct E {     int l, r, h, f;     S s;     E() {}     E(int l, int r, int h, S s, int f = 0) : l(l), r(r), h(h), s(s), f(f) {}     bool operator<(const E& p) const { return l < p.l; }     void print() const { printf( l=%d, r=%d, f=%d, h=%d n , l, r, f, h); }   } Es[100001];   int n, T, inf = 1e9;   struct compar {     bool operator()(const S& a, const S& b) const { return a.h < b.h; }   };   struct Solution {     int solve() {       set<E> sweep;       Ss[n] = S(-inf, inf, T);       sweep.insert(E(-inf, inf, 0, S(-inf, inf, 0), 2 * inf));       sweep.insert(E(-inf - 1, -inf - 1, 0, S(-inf - 1, -inf - 1, 0)));       sweep.insert(E(inf + 1, inf + 1, 0, S(inf + 1, inf + 1, 0)));       sort(Ss, Ss + n, compar());       for (int(i) = (0); (i) <= (n); ++(i)) {         auto& s = Ss[i];         auto e = E(s.l, s.r, s.h, s);         std::set<E>::iterator start = sweep.upper_bound(e), end, it, lt, rt;         start--, it = lt = rt = start, lt--, rt++;         while (it->l < e.r) {           if (!(it->h < lt->h && lt->h < e.h && lt->s.r > max(it->s.l, e.l) ||                 it->h < rt->h && rt->h < e.h && rt->s.l < min(it->s.r, e.r))) {             e.f = max(e.f, min(it->f, overlap(*it, e)));           } else if (e.h == T) {           }           it++, rt++, lt++;         }         auto first = *start, last = *lt;         sweep.erase(start, it);         if (first.l < e.l) {           sweep.insert(E(first.l, e.l, first.h, first.s, first.f));         }         if (last.r > e.r) {           sweep.insert(E(e.r, last.r, last.h, last.s, last.f));         }         sweep.insert(e);       }       auto it = sweep.begin();       it++;       return it->f;     }     int overlap(const E& a, E& b) {       return max(0, min(a.r, b.r) - max(a.l, b.l));     }   };   void print(vector<int>& nums) {     for (auto num : nums) cout << num << endl;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> T;     for (int(i) = (0); (i) < (n); ++(i)) cin >> Ss[i].h >> Ss[i].l >> Ss[i].r;     Solution test;     cout << test.solve() << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 7, mod = 1e9 + 7, INF = 0x3f3f3f3f;   int a[N];   int n;   bool vis[N];   int num[N];   int b[N];   int f(int x) { return x == b[x] ? x : b[x] = f(b[x]); }   void u(int x, int y) {     x = f(x), y = f(y);     if (x != y) b[x] = y;   }   int pre[N];   int main() {     cin >> n;     for (int i = 1; i < n; i++) {       scanf( %d , &a[i]);       if (pre[a[i]] == 0) pre[a[i]] = i;       vis[a[i]] = 1;     }     priority_queue<int, vector<int>, greater<int> > q;     for (int i = 1; i <= n; i++) {       b[i] = i;       if (!vis[i]) q.push(i);       num[i] = i;     }     vector<pair<int, int> > ans;     for (int i = n - 1; i >= 1; i--) {       int x = q.top();       q.pop();       ans.push_back(pair<int, int>(num[x], a[i]));       u(num[x], a[i]);       x = max(x, a[i]);       num[x] = a[i];       if (i == pre[a[i]]) q.push(x);     }     for (int i = 2; i <= n; i++)       if (f(i) != f(1)) {         cout << -1;         return 0;       }     cout << a[1] <<   n ;     for (int i = 0; i < ans.size(); i++)       cout << ans[i].first <<     << ans[i].second <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   class Solution {    public:     void solve(std::istream& in, std::ostream& out) {       string s;       in >> s;       int n = s.size();       for (size_t i = 0; i < n; i++) {         int x = (s[i] -  0 );         if (x % 8 == 0) {           out <<  YES                <<   n                << x;           return;         }       }       for (size_t i = 0; i < n; i++) {         for (size_t j = i + 1; j < n; j++) {           int x = 10 * (s[i] -  0 ) + (s[j] -  0 );           if (x % 8 == 0) {             out <<  YES                  <<   n                  << x;             return;           }         }       }       for (size_t i = 0; i < n; i++) {         for (size_t j = i + 1; j < n; j++) {           for (size_t k = j + 1; k < n; k++) {             int x = 100 * (s[i] -  0 ) + 10 * (s[j] -  0 ) + (s[k] -  0 );             if (x % 8 == 0) {               out <<  YES                    <<   n                    << x;               return;             }           }         }       }       out <<  NO ;     }   };   void solve(std::istream& in, std::ostream& out) {     out << std::setprecision(12);     Solution solution;     solution.solve(in, out);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     istream& in = cin;     ostream& out = cout;     solve(in, out);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long x, k, col;   vector<long long> d, g[10101];   void go(long long pos, long long K) {     if (col == 100000) return;     if (col == 10000) cerr <<  Ok ;     if (pos == 0 || K == 0) {       cout << d[pos] <<    ;       col++;       return;     }     for (int j = 0; j < g[pos].size(); ++j) go(g[pos][j], K - 1);   }   int main() {     cin >> x >> k;     for (long long i = 1; i * i <= x; ++i)       if (x % i == 0) {         d.push_back(i);         if (i != x / i) d.push_back(x / i);       }     sort(d.begin(), d.end());     for (int i = 0; i < d.size(); ++i)       for (int j = 0; j <= i; ++j)         if (d[i] % d[j] == 0) g[i].push_back(j);     go(d.size() - 1, k);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using vi = vector<int>;   using ii = pair<int, int>;   template <class A, class B>   ostream& operator<<(ostream& os, pair<A, B> p) {     return os << p.first <<     << p.second;   }   template <class A, class B>   istream& operator>>(istream& is, pair<A, B>& p) {     return is >> p.first >> p.second;   }   const int oo = 0x3f3f3f3f;   const ll OO = ll(oo) * oo;   const int N = 100005;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vi t(n);     for (int i = 0; i < n; i++) cin >> t[i];     sort((t).begin(), (t).end());     int ans = 0;     ll T = 0;     for (int i = 0; i < n; i++) {       if (T <= t[i]) {         ans++;         T += t[i];       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Max_N = 2e3, Mod = 998244353;   int n, d[Max_N + 5], D[Max_N + 5], opt[Max_N + 5];   char a[Max_N + 5][Max_N + 5];   inline bool Chck() {     sort(D + 1, D + n + 1);     int sum = 0;     for (int i = 1; i < n; i++) {       sum += D[i];       if (sum == i * (i - 1) / 2) return false;     }     return true;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %s , a[i] + 1);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (a[i][j] ==  1 ) ++d[j];     if (n <= 6) {       int ans = -1, cnt = 0;       for (int s = 0; s < 1 << n; s++) {         for (int i = 1; i <= n; i++) D[i] = d[i], opt[i] = 0;         for (int i = 1; i <= n; i++)           if ((1 << i - 1) & s) opt[i] = 1;         for (int i = 1; i <= n; i++)           for (int j = 1; j <= n; j++)             if (a[i][j] ==  1  && (opt[i] ^ opt[j])) --D[j], ++D[i];         if (Chck()) {           int sum = 0;           for (int i = 1; i <= n; i++)             if ((1 << i - 1) & s) ++sum;           if (sum < ans || ans == -1)             ans = sum, cnt = 1;           else             cnt += sum == ans;         }       }       if (ans == -1)         puts( -1 );       else {         for (int i = 1; i <= ans; i++) cnt = 1ll * cnt * i % Mod;         printf( %d %d n , ans, cnt);       }     } else {       for (int i = 1; i <= n; i++) D[i] = d[i];       if (Chck()) {         puts( 0 1 );         return 0;       }       int ans = 0;       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++) D[j] = d[j];         for (int j = 1; j <= n; j++)           if (a[i][j] ==  1 )             --D[j], ++D[i];           else if (i != j)             ++D[j], --D[i];         if (Chck()) ++ans;       }       printf( 1 %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[200003];   vector<int> v;   int main() {     cin >> n;     for (int i = 2; i <= n; i++) cin >> a[i];     v.push_back(n);     int j = n;     while (j != 1) {       v.push_back(a[j]);       j = a[j];     }     reverse(v.begin(), v.end());     for (int i = 0; i < v.size(); i++) cout << v[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e3 + 10;   int tim[maxn];   int ans[30][30];   set<int> q;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n * n; i++) {       int t;       cin >> t;       q.insert(t);       tim[t]++;     }     if (n % 2 == 0) {       for (auto idx : q)         if (tim[idx] % 4) {           cout <<  NO  << endl;           return 0;         }       vector<int> p;       for (auto idx : q) {         for (int i = 0; i < tim[idx] / 4; i++) p.push_back(idx);       }       int idx = 0;       for (int i = 0; i < n / 2; i++)         for (int j = 0; j < n / 2; j++)           ans[i][j] = ans[i][n - 1 - j] = ans[n - 1 - i][j] =               ans[n - 1 - i][n - 1 - j] = p[idx++];       cout <<  YES  << endl;       for (int i = 0; i < n; i++, cout << endl)         for (int j = 0; j < n; j++) cout << ans[i][j] <<    ;     } else {       int odd = 0;       vector<int> two;       for (auto idx : q)         if (tim[idx] % 2) odd++;       if (odd > 1) {         cout <<  NO  << endl;         return 0;       }       for (auto idx : q) {         if (tim[idx] % 2) {           ans[n / 2][n / 2] = idx;           tim[idx]--;         }         if (tim[idx] % 4) {           two.push_back(idx);           tim[idx] -= 2;         }       }       if (two.size() > n - 1 || (n - 1 - two.size()) % 2) {         cout <<  NO  << endl;         return 0;       }       if (two.size() < n - 1)         for (auto idx : q) {           if (tim[idx] > 0 && tim[idx] % 4 == 0) {             int qt = tim[idx];             for (int i = 0; i < qt / 2 && two.size() < n - 1; i++) {               two.push_back(idx);               tim[idx] -= 2;             }             if (two.size() == n - 1) break;           }         }       vector<int> p;       for (auto idx : q) {         if (tim[idx] % 4 == 0 && tim[idx] > 0)           for (int i = 0; i < tim[idx] / 4; i++) p.push_back(idx);       }       int idx = 0;       for (int i = 0; i < n / 2; i++)         for (int j = 0; j < n / 2; j++)           ans[i][j] = ans[i][n - 1 - j] = ans[n - 1 - i][j] =               ans[n - 1 - i][n - 1 - j] = p[idx++];       idx = 0;       for (int i = 0; i < n / 2; i++) {         ans[n / 2][i] = ans[n / 2][n - 1 - i] = two[idx++];         ans[i][n / 2] = ans[n - 1 - i][n / 2] = two[idx++];       }       cout <<  YES  << endl;       for (int i = 0; i < n; i++, cout << endl)         for (int j = 0; j < n; j++) cout << ans[i][j] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 20;   const double eps = 1e-6;   double p[maxn], ans[maxn], dp[1 << maxn], S[1 << maxn];   int Size[1 << maxn];   bool eq(const double &a, const double &b) { return abs(a - b) <= eps; }   int main() {     int n, k, zerCnt = 0;     bool is0;     scanf( %d %d , &n, &k);     for (int i = (0); i < (n); ++i) {       scanf( %lf , &p[i]);       dp[i] = ans[i] = 0;       Size[i] = 0;       if (eq(p[i], 0)) ++zerCnt;     }     k = min(k, n - zerCnt);     dp[0] = 1;     for (int i = (1); i < (1 << n); ++i) {       for (int j = (0); j < (n); ++j)         if (i & (1 << j)) {           Size[i] = Size[i - (1 << j)] + 1;           S[i] = S[i - (1 << j)] + p[j];         }     }     for (int i = (1); i < (1 << n); ++i) {       if (S[i] > 0) {         for (int j = (0); j < (n); ++j)           if ((i & (1 << j)) && (S[(1 << n) - 1 - i + (1 << j)] > 0))             dp[i] += dp[i - (1 << j)] * p[j] / S[(1 << n) - 1 - i + (1 << j)];         if (Size[i] == k)           for (int j = (0); j < (n); ++j)             if (i & (1 << j)) ans[j] += dp[i];       } else         dp[i] = 0;     }     for (int i = (0); i < (n); ++i) printf( %.8f  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long int> as;   set<long long int> s;   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) {       long long int a;       cin >> a;       a -= i;       as.push_back(a);       s.insert(a);     }     vector<long long int> unique;     for (long long int ss : s) {       unique.push_back(ss);     }     long long int dp[2][3010];     long long int low[2][3010];     for (int i = 0; i < 1; i++) {       for (int j = 0; j < n; j++) {         dp[i][j] = 1e15;       }     }     int start = 0;     for (int i = 0; i < unique.size(); i++) {       dp[0][i] = abs(unique[i] - as[0]);     }     low[0][0] = dp[0][0];     for (int j = 1; j < unique.size(); j++) {       low[0][j] = min(low[0][j - 1], dp[0][j]);     }     if (n == 1) {       cout << 0 << endl;       return 0;     }     for (int i = 1; i < n; i++) {       for (int j = 0; j < unique.size(); j++) {         dp[i % 2][j] = low[(i - 1) % 2][j] + abs(unique[j] - as[i]);       }       low[i % 2][0] = dp[i % 2][0];       for (int j = 1; j < unique.size(); j++) {         low[i % 2][j] = min(low[i % 2][j - 1], dp[i % 2][j]);       }     }     long long int ans = 1e15;     for (int i = 0; i < unique.size(); i++) {       ans = min(dp[(n - 1) % 2][i], ans);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, cnt = 0;       cin >> n;       vector<int> a(n);       for (int i = 0; i < n; i++) {         cin >> a[i];       }       sort(a.rbegin(), a.rend());       for (int i = 0; i < n; i++) {         if (cnt < a[i]) {           cnt++;         } else {           break;         }       }       cout << cnt << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int const N = 1010;   int const M = 1001000;   int n, m;   struct Graph {     struct edges {       int u, next;       long long c;     } e[M];     int p[N], idx;     void addedge(int u, int v, long long c) {       e[idx].u = v, e[idx].next = p[u], e[idx].c = c, p[u] = idx++;     }     void init() {       idx = 0;       memset(p, (0xff), sizeof p);     }     queue<int> q;     long long dis[N];     bool used[N];     void spfa(int s) {       int t, u;       long long w;       while (!q.empty()) q.pop();       memset(used, 0, sizeof used);       for (int i = 1; i <= (n); ++i) dis[i] = 0x3FFFFFFFFFFFFFFFLL;       dis[s] = 0;       q.push(s);       while (!q.empty()) {         t = q.front();         q.pop();         used[t] = false;         for (int i = p[t]; ~i; i = e[i].next) {           u = e[i].u, w = e[i].c;           if (dis[t] + w < dis[u]) {             dis[u] = dis[t] + w;             if (!used[u]) {               used[u] = true;               q.push(u);             }           }         }       }     }   } g1, g2;   long long ti[N], ci[N];   int main() {     scanf( %d%d , &n, &m);     g1.init();     int s, t;     scanf( %d%d , &s, &t);     for (int i = 0; i < (m); ++i) {       int x, y, z;       scanf( %d%d%d , &x, &y, &z);       g1.addedge(x, y, z);       g1.addedge(y, x, z);     }     for (int i = 1; i <= (n); ++i) scanf( %I64d %I64d , &ti[i], &ci[i]);     g2.init();     for (int i = 1; i <= (n); ++i) {       g1.spfa(i);       for (int j = 1; j <= (n); ++j)         if (i != j) {           if (g1.dis[j] <= ti[i]) {             g2.addedge(i, j, ci[i]);           }         }     }     g2.spfa(s);     if (g2.dis[t] == 0x3FFFFFFFFFFFFFFFLL)       puts( -1 );     else       printf( %I64d n , g2.dis[t]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int maxn = 1e5 + 6;   const long long int MOD = 1e9 + 7;   vector<int> prim(1000005, 1);   int fact[maxn];   long long int binomialCoeff(long long int n, long long int k) {     long long int res = 1;     if (k > n - k) k = n - k;     for (long long int i = 0; i < k; ++i) {       res *= (n - i);       res /= (i + 1);     }     return res;   }   bool isVowel(char ch) {     if (ch ==  a  || ch ==  i  || ch ==  e  || ch ==  u  || ch ==  o ) {       return true;     }     return false;   }   long long int power(long long int x, long long int i, long long int mod) {     long long int ans = 1;     while (i > 0) {       if (i & 1) ans = (ans * x) % mod;       i >>= 1;       x = (x * x) % mod;     }     return ans;   }   long long int modInverse(long long int x, long long int mod) {     return power(x, mod - 2, mod);   }   int nCr(int n, int r) {     if (n < r) {       return 0;     }     return (((fact[n] * modInverse(fact[n - r], MOD)) % MOD) *             modInverse(fact[r], MOD)) %            MOD;   }   long long int power(int x, unsigned int y) {     long long int temp;     if (y == 0) return 1;     temp = power(x, y / 2);     if (y % 2 == 0)       return temp * temp;     else       return x * temp * temp;   }   void erath(int n) {     prim[1] = 0;     prim[0] = 0;     prim[2] = 1;     for (int i = 2; i * i <= n; i++) {       if (prim[i]) {         for (int j = i * i; j <= n; j += i) {           prim[j] = 0;         }       }     }   }   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long int lcm(long long int a, long long int b) {     return (a / gcd(a, b)) * b;   }   bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {     return (a.second < b.second);   }   long long int n, m, k;   int getmid(int l, int r) { return (l + (r - l) / 2); }   struct segtree {     int siz;     vector<pair<long long int, long long int> > sums;     void init(int n) {       siz = 1;       while (siz < n) siz *= 2;       sums.assign(2 * siz, {LONG_MAX, 0});     }     void set(int i, int v, int cur, int l, int r) {       if (r - l == 1) {         sums[cur].first = v;         sums[cur].second = 1;         return;       }       int m = getmid(l, r);       if (i < m) {         set(i, v, 2 * cur + 1, l, m);       } else         set(i, v, 2 * cur + 2, m, r);       sums[cur].first = min(sums[2 * cur + 1].first, sums[2 * cur + 2].first);       if (sums[2 * cur + 1].first == sums[cur].first &&           sums[2 * cur + 2].first == sums[cur].first) {         sums[cur].second = sums[cur * 2 + 1].second + sums[cur * 2 + 2].second;       } else if (sums[2 * cur + 1].first == sums[cur].first) {         sums[cur].second = sums[cur * 2 + 1].second;       } else {         sums[cur].second = sums[cur * 2 + 2].second;       }     }     void set(int i, int v) { set(i, v, 0, 0, siz); }     pair<long long int, long long int> sum(int l, int r, int cur, int lx,                                            int rx) {       if (lx >= r || l >= rx) return {LONG_MAX, 0};       if (lx >= l && rx <= r) return {sums[cur].first, sums[cur].second};       int mid = getmid(lx, rx);       pair<long long int, long long int> s1 = sum(l, r, 2 * cur + 1, lx, mid),                                          s2 = sum(l, r, 2 * cur + 2, mid, rx);       if (s1.first < s2.first) {         return s1;       } else if (s1.first > s2.first)         return s2;       else         return {s1.first, s1.second + s2.second};     }     pair<long long int, long long int> sum(int l, int r) {       return sum(l, r, 0, 0, siz);     }   };   int nCrModp(int n, int r, int p) {     if (r > n - r) r = n - r;     int C[r + 1];     memset(C, 0, sizeof(C));     C[0] = 1;     for (int i = 1; i <= n; i++) {       for (int j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p;     }     return C[r];   }   void ans(int top) {     cin >> n >> k;     long long int h[n], mx = INT_MIN, mn = INT_MAX;     vector<long long int> fin(200002, 0);     for (int i = 0; i < n; i++) {       cin >> h[i];       if (h[i] > 0) fin[1] += 1;       if (h[i] > 0) fin[h[i] + 1] -= 1;       mx = max(mx, h[i]);       mn = min(mn, h[i]);     }     for (int i = 1; i < mx + 1; i++) {       fin[i] += fin[i - 1];     }     int i = mx, ans = 0;     while (i > mn) {       int j = i, sum = 0;       while (sum + fin[j] <= k) {         sum += fin[j];         j--;       }       i = j;       ans++;     }     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int t = 1, co = 1;     while (t--) {       ans(co);       co++;     }     return 0;   }


#include <bits/stdc++.h>   int main() {     int num, i, j, k, p;     double prob[110], temp, tot, high, temp2;     scanf( %d , &num);     i = 0;     while (i < num) {       scanf( %lf , &prob[i]);       i++;     }     i = 0;     while (i < num) {       p = i + 1;       while (p < num) {         if (prob[p] > prob[i]) {           temp2 = prob[p];           prob[p] = prob[i];           prob[i] = temp2;         }         p++;       }       i++;     }     high = 0;     i = 1;     while (i <= num) {       tot = 0;       for (j = 0; j < i; j++) {         temp = prob[j];         for (k = 0; k < i; k++) {           if (j != k) temp *= 1 - prob[k];         }         tot += temp;       }       if (tot > high) high = tot;       i++;     }     printf( %.12f n , high);   }


#include <bits/stdc++.h>   using namespace std;   long long min_val(long long a, long long b) {     if (a <= b)       return a;     else       return b;   }   long long max_val(long long a, long long b) {     if (a >= b)       return a;     else       return b;   }   long long abs(long long a, long long b) {     long long tmp = min_val(a, b);     if (tmp == a) {       return b - a;     } else       return a - b;   }   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int a[n];       for (int i = 0; i < n; i++) {         cin >> a[i];       }       sort(a, a + n, greater<int>());       for (int i = 0; i < n; i++) {         cout << a[i] <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int n;   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> s;     for (long long i = 0; i < n; i++) {       if (s[i] ==  * )         for (long long t = 1; t <= n - 1; t++) {           bool check = true;           for (long long j = 0; j < 5; j++)             if (i + j * t >= n || s[i + j * t] ==  . ) check = false;           if (check == 1) {             cout <<  yes ;             return 0;           }         }     }     cout <<  no  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, a, m, cur;   set<int> S;   int main() {     cin >> n >> k >> a;     cin >> m;     S.insert(0);     S.insert(n + 1);     cur = (n + 1) / (a + 1);     for (int i = 0; i < m; i++) {       int x;       cin >> x;       auto prv = S.lower_bound(x);       auto nxt = prv;       prv--;       cur -= (*nxt - *prv) / (a + 1);       cur += (x - *prv) / (a + 1);       cur += (*nxt - x) / (a + 1);       S.insert(x);       if (cur < k) return cout << i + 1, 0;     }     cout << -1;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   vector<int> G[maxn], a, b;   int n, m, flag;   int col[maxn];   bool bfs(int s) {     queue<int> p;     p.push(s);     col[s] = 1;     while (!p.empty()) {       int from = p.front();       p.pop();       int len = G[from].size();       for (int i = 0; i < len; ++i) {         int v = G[from][i];         if (col[v] == -1) {           p.push(v);           col[v] = !col[from];         }         if (col[from] == col[v]) return false;       }     }     return true;   }   int main() {     scanf( %d%d , &n, &m);     a.clear();     b.clear();     for (int i = 0; i <= n; ++i) G[i].clear();     for (int i = 0; i < m; ++i) {       int u, v;       scanf( %d%d , &u, &v);       G[u].push_back(v);       G[v].push_back(u);     }     flag = 0;     memset(col, -1, sizeof(col));     for (int i = 1; i <= n; i++)       if (col[i] == -1 && !bfs(i)) {         flag = true;         break;       }     if (flag) {       puts( -1 );     } else {       for (int i = 1; i <= n; ++i) {         if (G[i].size() == 0) col[i] = -1;         if (col[i] == 0) {           a.push_back(i);         } else if (col[i] == 1) {           b.push_back(i);         }       }       int len = a.size();       printf( %d n , len);       for (int i = 0; i < len; ++i) {         if (i) printf(   );         printf( %d , a[i]);       }       len = b.size();       printf(  n%d n , len);       for (int i = 0; i < len; ++i) {         if (i) printf(   );         printf( %d , b[i]);       }       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[2000010], b[1000010], n;   int solve(int s) {     int cnt = 0, cur = s;     bool flag = 0;     while (1) {       cur = b[cur];       cnt++;       if (cur >= s && flag) return cnt;       if (cur >= n) {         cur -= n;         flag = 1;       }       if (cur >= s && flag) return cnt;     }   }   int main() {     int q;     scanf( %d%d , &n, &q);     int i;     long long sum = 0;     for (i = 0; i < n; i++) {       scanf( %d , &a[i]);       sum += a[i];       a[n + i] = a[i];     }     for (i = 0; i < q; i++) {       long long x, cur = 0;       int r = 0, j;       scanf( %lld , &x);       if (x >= sum) {         printf( 1 n );         continue;       }       for (j = 0; j < n; j++) {         while (cur + a[r] <= x) cur += a[r++];         b[j] = r;         cur -= a[j];       }       int min_ = 998244353, pos, ans = 998244353;       for (j = 0; j < n; j++) {         if (b[j] - j < min_) {           min_ = b[j] - j;           pos = j;         }       }       for (j = pos; j <= b[pos]; j++) {         ans = min(ans, solve(j % n));       }       printf( %d n , ans);     }     return 0;   }