#include <bits/stdc++.h>   using namespace std;   #define FOR(i, n) for (int (i) = 0; (i) < (n); (i)++)   #define FORI(i, a, b) for (int (i) = (a); (i) < (b); (i)++)       #define ll long long   #define mp(m, n) make_pair((m), (n))   // #define DEBUG       #ifdef DEBUG   template<typename T>   void _debug(string s, T x) {       cerr << s <<  : ;       for (auto it = x.begin(); it != x.end(); ++it) {           cerr <<     << *it;       }       cerr << endl;   }       template<typename T, typename K>   void _debug(string s, map<T, K> x) {       cerr << s <<  : ;       for (auto it = x.begin(); it != x.end(); ++it) {           cerr <<     << it->first <<  :  << it->second;       }       cerr << endl;   }      template<typename T, typename K>   void _debug(string s, set<T, K> x) {       cerr << s <<  : ;       for (auto it = x.begin(); it != x.end(); ++it) {           cerr <<     << *it;       }       cerr << endl;   }       template<typename T, typename K>   void _debug(string s, vector<pair<T, K> > x) {       cerr << s <<  : ;       for (auto it = x.begin(); it != x.end(); ++it) {           cerr <<     << it->first <<  ,  << it->second;       }       cerr << endl;   }      void _debug(string s, int x) {       cerr << s <<  :   << x << endl;   }   void _debug(string s, long long x) {       cerr << s <<  :   << x << endl;   }   void _debug(string s, double x) {       cerr << s <<  :   << x << endl;   }   #define debug(x) _debug(#x, (x))   #else   #define debug(x)   #endif       #define db debug      const long long N = 2e3 + 4;   int t[4*N] = {};      void add(int L, int R, int val, int index = 1, int l = 1, int r = N - 1) {              if (L > R) return;       if (r < L || l > R) return;       if (L <= l && r <= R) {t[index] += val; return; }       int mid = (l + r) / 2;       // debug(l);debug(r);debug(mid);       add(L, R, val, index * 2, l, mid);       add(L, R, val, index * 2 + 1, mid + 1, r);          // if (pos < l || pos > r) return;       // if (l == r) {       //     t[index] = val;       //     return;       // }       // int mid = (l + r) / 2;       // upd(pos, val, index * 2, l, mid);       // upd(pos, val, index * 2 + 1, mid + 1, r);       // t[index] = min(t[index * 2], t[index * 2 + 1]);   }      int get_val(int pos, int index = 1, int l = 1, int r = N - 1) {       if (pos < l || pos > r) return 0;       if (l == r) {           return t[index];       }       int mid = (l + r) / 2;       return t[index] + get_val(pos, index * 2, l, mid) + get_val(pos, index * 2 + 1, mid + 1, r);   }      inline int in(int a, int b, int c, int d) {       int x = max(a, c);       int y = min(b, d);       return max(0, y - x + 1);   }      int main() {       ios_base::sync_with_stdio(false); std::cin.tie(0);       int n, m, k;       cin >> n >> m >> k;       vector<int> l(m), r(m);       FOR(i, m) cin >> l[i] >> r[i];       int R = 0;       for (int i = 1; i + k - 1 <= n; i++) {           memset(t, 0, sizeof(t));           int ans = 0;           for (int j = 0; j < m; j++) {               ans += in(i, i + k - 1, l[j], r[j]);               int x, y;               x = max(i + 1, l[j] - k + 1);               y = l[j];               vector<int> vvv;               vvv = {x, y, 1};               debug(vvv);               add(x, y, 1);               // int xx = r[j] - k + 2;               // int yy = r[j] + 1;               // yy = min(yy, xx + y - x);               int xx = r[j] - k + 2;               int yy = r[j] + 1;               xx = max(i + 1, xx);               yy = min(yy, xx + y - x);                  add(xx, yy, -1);               vvv = {xx, yy, -1};               debug(vvv);           }           debug(i);           debug(ans);           R = max(R, ans);           for (int j = i + 1; j + k - 1 <= n; j++) {               ans += get_val(j);               R = max(R, ans);               debug(j); debug(ans);           }       }       cout << R << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline unsigned RD() {     unsigned intmp(0);     char rdch(getchar());     while (rdch <  0  || rdch >  9 ) rdch = getchar();     while (rdch >=  0  && rdch <=  9 ) {       intmp = (intmp << 3) + (intmp << 1) + rdch -  0 ;       rdch = getchar();     }     return intmp;   }   inline int RDsg() {     int rdtp(0), rdsg(1);     char rdch(getchar());     while ((rdch <  0  || rdch >  9 ) && (rdch !=  - )) rdch = getchar();     if (rdch ==  - ) {       rdsg = -1;       rdch = getchar();     }     while (rdch >=  0  && rdch <=  9 ) {       rdtp = (rdtp << 3) + (rdtp << 1) + rdch -  0 ;       rdch = getchar();     }     return rdtp * rdsg;   }   unsigned Bd[500005], Pos1[500005], Pos2[500005], m, n, q, Cnt(0), A, B, C, D, t,       Ans(0);   char a[500005], b[500005];   inline void Clr() {}   int main() {     n = RD(), m = RD(), q = RD();     scanf( %s%s , a + 1, b + 1);     Bd[0] = -1;     for (register unsigned i(2), Tmp; i <= m; ++i) {       Tmp = Bd[i - 1];       while ((b[i] ^ b[Tmp + 1]) && (Tmp < 0x3f3f3f3f)) {         Tmp = Bd[Tmp];       }       Bd[i] = Tmp + 1;     }     for (register unsigned i(1), j(0); i <= n; ++i) {       while ((a[i] ^ b[j + 1]) && (j < 0x3f3f3f3f)) {         j = Bd[j];       }       if (j > 0x3f3f3f3f) {         j = 0;         continue;       } else {         ++j;         if (Pos1[j] < q) {           Pos1[j] = i;         }         if ((j == m) && (!A)) {           A = i;         }       }     }     for (register unsigned i(m - 1); i; --i) {       if ((Pos1[i + 1] > q) && Pos1[i + 1]) {         Pos1[i] = min(Pos1[i], Pos1[i + 1] - 1);       }     }     for (register unsigned i(m); i; --i) {       if (Pos1[i] < q) {         Pos1[i] = 0;         continue;       }       if (Pos1[Bd[i]] < q)         Pos1[Bd[i]] = Pos1[i];       else         Pos1[Bd[i]] = min(Pos1[Bd[i]], Pos1[i]);     }     Bd[m + 1] = -1;     Bd[m] = 0;     for (register unsigned i(m - 1), Tmp; i; --i) {       Tmp = Bd[i + 1];       while ((b[i] ^ b[m - Tmp]) && (Tmp < 0x3f3f3f3f)) {         Tmp = Bd[m - Tmp + 1];       }       Bd[i] = Tmp + 1;     }     for (register unsigned i(n), j(0); i; --i) {       while ((j < 0x3f3f3f3f) && (a[i] ^ b[m - j])) {         j = Bd[m - j + 1];       }       if (j > 0x3f3f3f3f) {         j = 0;         continue;       } else {         if ((n - Pos2[m - j] + 1 < q) || (!Pos2[m - j])) {           Pos2[m - j] = i;         }         ++j;         if ((j == m) && (!B)) {           B = i;         }       }     }     for (register unsigned i(2); i <= m; ++i) {       if ((n - Pos2[i - 1] + 1 > q) && (Pos2[i - 1])) {         Pos2[i] = max(Pos2[i], Pos2[i - 1] + 1);       }     }     for (register unsigned i(1); i <= m; ++i) {       if (n - Pos2[i] + 1 < q) {         Pos2[i] = 0;         continue;       }       if (n - Pos2[m - Bd[i] + 1] + 1 < q)         Pos2[m - Bd[i] + 1] = Pos2[i];       else         Pos2[m - Bd[i] + 1] = max(Pos2[i], Pos2[m - Bd[i] + 1]);     }     if (q >= m) {       if (A && (n >= q + max(A, q))) {         printf( Yes n%u %u n , max(A, q) - q + 1, max(A, q) + 1);         return 0;       }       if (B && (min(B, n - q + 1) > q)) {         printf( Yes n%u %u n , min(B, n - q + 1) - q, min(B, n - q + 1));         return 0;       }     }     for (register unsigned i(m - min(m - 1, q)); i <= min(m - 1, q); ++i) {       if (Pos1[i] && Pos2[i + 1] && (Pos1[i] < Pos2[i + 1])) {         printf( Yes n%u %u n , Pos1[i] - q + 1, Pos2[i + 1]);         return 0;       }     }     printf( No n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long BIG = 1000000000000000000LL;   void solve();   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     solve();     return 0;   }   const long long borne = 3 * (long long)(1e5) + 5;   long long tab[borne];   long long som[borne];   long long nbElem, nbParties;   void solve() {     cin >> nbElem >> nbParties;     for (long long i = (0); i < (nbElem); ++i) {       cin >> tab[i];     }     som[0] = tab[0];     for (long long i = (1); i < (nbElem); ++i) som[i] = tab[i] + som[i - 1];     vector<long long> ww(nbElem - 1);     for (long long i = (0); i < (nbElem - 1); ++i) ww[i] = som[i];     sort(ww.begin(), ww.end());     long long res = nbParties * som[nbElem - 1];     for (long long i = (0); i < (nbParties - 1); ++i) res -= ww[i];     cout << res <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b, c, x;     cin >> a >> b >> c;     if (a * c % 100 == 0)       x = (a * c) / 100;     else       x = (a * c) / 100 + 1;     if (x < b)       cout <<  0  << endl;     else       cout << x - b << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     char name[20];     int id;     int num;   } a[100005];   bool cmp(node x, node y) {     if (x.id == y.id) return x.num > y.num;     return x.id < y.id;   }   int main() {     int n, m;     while (cin >> n >> m) {       for (int i = 0; i < n; i++) {         scanf(  %s %d %d , a[i].name, &a[i].id, &a[i].num);       }       sort(a, a + n, cmp);       int st = 0, ed = 0;       for (st = 0; st < n; st = ed) {         while (ed < n && a[ed].id == a[st].id) ed++;         int fir = a[st].num;         int sec = a[st + 1].num;         if (ed - st > 2 && sec == a[st + 2].num) {           printf( ? n );         } else {           printf( %s %s n , a[st].name, a[st + 1].name);         }       }     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   const int maxn = 4e2 + 5, maxk = 4e2 + 5, inf = 1e9 + 69, delta = 1e9 + 7,             max_sum = 10000 + 5;   inline void in_out_optimize() {     ios::sync_with_stdio(false);     cout.tie(0);     cin.tie(0);   }   int main() {     in_out_optimize();     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       long long eq = inf - (inf % n);       set<int> dest;       for (int i = 0; i < n; i++) {         int a;         cin >> a;         if (a < 0) a += eq;         a %= n;         dest.insert((i + a) % n);       }       if (dest.size() == n)         cout <<  YES n ;       else         cout <<  NO n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int rd() {     int x = 0, flg = 1;     char c = getchar();     for (; (c < 48 || c > 57) && c !=  - ; c = getchar())       ;     if (c ==  - ) flg = -1, c = getchar();     for (; c > 47 && c < 58; x = x * 10 + c - 48, c = getchar())       ;     return flg * x;   }   const int mxn = 100010;   struct nd {     int sm, px, py, sx, sy, mx, ml, mr, _px, _py, _sx, _sy, _mx, _ml, _mr;     nd operator!() {       return (nd){-sm, -_px, _py, -_sx, _sy, -_mx, _ml, _mr,                   -px, py,   -sx, sy,   -mx, ml,   mr};     }   } tr[mxn << 2], ret;   int tag[mxn << 2];   void mrg(nd x, nd y) {     ret.sm = x.sm + y.sm;     if (x.px < x.sm + y.px)       ret.px = x.px, ret.py = x.py;     else       ret.px = x.sm + y.px, ret.py = y.py;     if (y.sx < y.sm + x.sx)       ret.sx = y.sx, ret.sy = y.sy;     else       ret.sx = y.sm + x.sx, ret.sy = x.sy;     if (x.mx < y.mx)       if (x.mx < x.sx + y.px)         ret.mx = x.mx, ret.ml = x.ml, ret.mr = x.mr;       else         ret.mx = x.sx + y.px, ret.ml = x.sy, ret.mr = y.py;     else if (y.mx < x.sx + y.px)       ret.mx = y.mx, ret.ml = y.ml, ret.mr = y.mr;     else       ret.mx = x.sx + y.px, ret.ml = x.sy, ret.mr = y.py;     if (x._px > x.sm + y._px)       ret._px = x._px, ret._py = x._py;     else       ret._px = x.sm + y._px, ret._py = y._py;     if (y._sx > y.sm + x._sx)       ret._sx = y._sx, ret._sy = y._sy;     else       ret._sx = y.sm + x._sx, ret._sy = x._sy;     if (x._mx > y._mx)       if (x._mx > x._sx + y._px)         ret._mx = x._mx, ret._ml = x._ml, ret._mr = x._mr;       else         ret._mx = x._sx + y._px, ret._ml = x._sy, ret._mr = y._py;     else if (y._mx > x._sx + y._px)       ret._mx = y._mx, ret._ml = y._ml, ret._mr = y._mr;     else       ret._mx = x._sx + y._px, ret._ml = x._sy, ret._mr = y._py;   }   void mrg_rev(nd x, nd y) {     ret.sm = -x.sm - y.sm;     if (x._px > x.sm + y._px)       ret.px = -x._px, ret.py = x._py;     else       ret.px = -x.sm - y._px, ret.py = y._py;     if (y._sx > y.sm + x._sx)       ret.sx = -y._sx, ret.sy = y._sy;     else       ret.sx = -y.sm - x._sx, ret.sy = x._sy;     if (x._mx > y._mx)       if (x._mx > x._sx + y._px)         ret.mx = -x._mx, ret.ml = x._ml, ret.mr = x._mr;       else         ret.mx = -x._sx - y._px, ret.ml = x._sy, ret.mr = y._py;     else if (y._mx > x._sx + y._px)       ret.mx = -y._mx, ret.ml = y._ml, ret.mr = y._mr;     else       ret.mx = -x._sx - y._px, ret.ml = x._sy, ret.mr = y._py;     if (x.px < x.sm + y.px)       ret._px = -x.px, ret._py = x.py;     else       ret._px = -x.sm - y.px, ret._py = y.py;     if (y.sx < y.sm + x.sx)       ret._sx = -y.sx, ret._sy = y.sy;     else       ret._sx = -y.sm - x.sx, ret._sy = x.sy;     if (x.mx < y.mx)       if (x.mx < x.sx + y.px)         ret._mx = -x.mx, ret._ml = x.ml, ret._mr = x.mr;       else         ret._mx = -x.sx - y.px, ret._ml = x.sy, ret._mr = y.py;     else if (y.mx < x.sx + y.px)       ret._mx = -y.mx, ret._ml = y.ml, ret._mr = y.mr;     else       ret._mx = -x.sx - y.px, ret._ml = x.sy, ret._mr = y.py;   }   void pushup(int i) {     if (tag[i])       mrg_rev(tr[(i << 1)], tr[(i << 1 | 1)]);     else       mrg(tr[(i << 1)], tr[(i << 1 | 1)]);     tr[i] = ret;   }   void update(int i, int l, int r, int p, int v) {     if (l == r) {       tr[i] = (nd){v, v, l, v, l, v, l, l, v, l, v, l, v, l, l};       return;     }     if (p <= ((l + r) >> 1))       update((i << 1), l, ((l + r) >> 1), p, v);     else       update((i << 1 | 1), ((l + r) >> 1) + 1, r, p, v);     pushup(i);   }   void modify(int i, int l, int r, int x, int y) {     if (r < x || l > y) return;     if (l >= x && r <= y) {       tag[i] ^= 1, tr[i] = !tr[i];       return;     }     modify((i << 1), l, ((l + r) >> 1), x, y);     modify((i << 1 | 1), ((l + r) >> 1) + 1, r, x, y);     pushup(i);   }   int tot, id[mxn], fl[mxn], bl[mxn], br[mxn];   void query(int i, int l, int r, int x, int y, bool fg) {     if (r < x || l > y) return;     if (l >= x && r <= y) {       id[++tot] = i;       fl[tot] = fg;       bl[tot] = l, br[tot] = r;       return;     }     query((i << 1), l, ((l + r) >> 1), x, y, fg ^ tag[i]);     query((i << 1 | 1), ((l + r) >> 1) + 1, r, x, y, fg ^ tag[i]);   }   void qry() {     if (fl[1])       ret = !tr[id[1]];     else       ret = tr[id[1]];     for (int i = 2; i <= tot; ++i)       if (fl[i])         mrg(ret, !tr[id[i]]);       else         mrg(ret, tr[id[i]]);   }   int n, q, a[mxn], qi[mxn], ql[mxn], qr[mxn], qx[mxn], qy[mxn];   void build(int i, int l, int r) {     if (l == r) {       tr[i] =           (nd){a[l], a[l], l, a[l], l, a[l], l, l, a[l], l, a[l], l, a[l], l, l};       return;     }     build((i << 1), l, ((l + r) >> 1)),         build((i << 1 | 1), ((l + r) >> 1) + 1, r);     pushup(i);   }   int main() {     n = rd();     for (int i = 1; i <= n; ++i) a[i] = rd();     build(1, 1, n);     q = rd();     for (int i = 1; i <= q; ++i)       if (rd()) {         int l = rd(), r = rd(), k = rd(), m = 0, ans = 0;         tot = 0;         query(1, 1, n, l, r, 0);         for (; k--;) {           qry();           if (ret._mx <= 0) break;           ans += ret._mx;           int x = ret._ml, y = ret._mr;           modify(1, 1, n, x, y);           ql[++m] = x, qr[m] = y;         }         printf( %d n , ans);         for (int j = 1; j <= m; ++j) modify(1, 1, n, ql[j], qr[j]);       } else {         int x = rd(), y = rd();         update(1, 1, n, x, y);       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char buf[1 << 21], *p1 = buf, *p2 = buf;   template <class T>   void read(T &x) {     x = 0;     int c = getchar();     int flag = 0;     while (c <  0  || c >  9 ) flag |= (c ==  - ), c = getchar();     while (c >=  0  && c <=  9 )       x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();     if (flag) x = -x;   }   template <class T>   T _max(T a, T b) {     return b < a ? a : b;   }   template <class T>   T _min(T a, T b) {     return a < b ? a : b;   }   template <class T>   bool checkmax(T &a, T b) {     return a < b ? a = b, 1 : 0;   }   template <class T>   bool checkmin(T &a, T b) {     return b < a ? a = b, 1 : 0;   }   const int N = 1000005;   int n;   int a[N], b[N], sa[N], sb[N];   void init() {     read(n);     for (int i = 1; i <= n; ++i) read(a[i]);     for (int i = 1; i <= n; ++i) read(b[i]);     sa[n + 1] = 0;     for (int i = n; i >= 1; --i) sa[i] = sa[i + 1] + a[i];     sb[0] = 0;     for (int i = 1; i <= n; ++i) sb[i] = sb[i - 1] + b[i];   }   void solve() {     int ans = -1;     for (int i = 1; i <= n; ++i) {       int w = max(sb[i - 1], sa[i + 1]);       if (i == 1)         ans = w;       else         checkmin(ans, w);     }     printf( %d n , ans);   }   int main() {     int t;     read(t);     while (t--) {       init();       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double dist(double x1, double y1, double x2, double y2) {     double dx = x1 - x2;     double dy = y1 - y2;     return sqrt(dx * dx + dy * dy);   }   double catet(double a, int b) {     double z = a * a - b * b;     if (z < 1e-12) z = 0;     return sqrt(z);   }   int main() {     int xp, yp, vp;     cin >> xp >> yp >> vp;     int x, y, v, r;     cin >> x >> y >> v >> r;     const double eps = 1e-10;     const double R = sqrt(1.0 * xp * xp + 1.0 * yp * yp);     double lo = 0;     double hi = 1e7;     double T;     while ((hi - lo) / hi > eps) {       T = (lo + hi) / 2;       const double alpha = T / (R / vp);       double nx = xp * cos(alpha) - yp * sin(alpha);       double ny = xp * sin(alpha) + yp * cos(alpha);       double a = ny - y;       double b = x - nx;       double c = -(a * x + b * y);       double z = sqrt(a * a + b * b);       a /= z;       b /= z;       c /= z;       double need;       if (fabs(c) < r - 1e-8) {         double mx = a * (-c);         double my = b * (-c);         if (dist(x, y, mx, my) + dist(mx, my, nx, ny) >             dist(x, y, nx, ny) + 1e-8) {           need = dist(x, y, nx, ny);         } else {           need = catet(dist(x, y, 0, 0), r) + catet(dist(nx, ny, 0, 0), r);           double alp = fabs(atan2(y, x) - atan2(ny, nx));           if (alp > M_PI) alp = 2 * M_PI - alp;           alp -= acos(r / dist(x, y, 0, 0));           alp -= acos(r / dist(nx, ny, 0, 0));           need += alp * r;         }       } else {         need = dist(x, y, nx, ny);       }       if (need <= v * T) {         hi = T;       } else {         lo = T;       }     }     printf( %.6f n , T);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ii = pair<int, int>;   using vi = vector<int>;   const int N = 2 * 5e5 + 5;   int n, m, q;   int par[N], sub[N];   int p[N];   struct edge {     int l, r, tag = 0;   } elist[N];   vi g[N];   int dp[19][500005];   const int lg = 19;   int position[N];   int in[N], out[N], tym = 0;   int what[N];   void setPar(int u, int dad) {     dp[0][u] = dad;     for (int i = 1; i < lg; ++i) {       dp[i][u] = dp[i - 1][dp[i - 1][u]];     }   }   void dfs(int u) {     if (u <= n) {       in[u] = out[u] = ++tym;       what[tym] = u;       position[p[u]] = tym;     } else {       in[u] = INT_MAX;       out[u] = INT_MIN;       for (int v : g[u]) {         setPar(v, u);         dfs(v);         in[u] = min(in[u], in[v]);         out[u] = max(out[u], out[v]);       }     }   }   int getHighest(int u, int tag) {     for (int i = lg - 1; i >= 0; --i) {       if (dp[i][u]) {         if (elist[dp[i][u] - n].tag >= tag) {           u = dp[i][u];         }       }     }     return u;   }   int find(int u) { return par[u] == u ? u : par[u] = find(par[u]); }   int mx[N];   void build(int u = 1, int b = 1, int e = n) {     if (b == e) {       return void(mx[u] = p[what[b]]);     }     int mid = (b + e) >> 1;     build(u << 1, b, mid);     build(u << 1 | 1, mid + 1, e);     mx[u] = max(mx[u << 1], mx[u << 1 | 1]);   }   int Max(int l, int r, int u = 1, int b = 1, int e = n) {     if (l <= b and e <= r) return mx[u];     if (b > r or e < l) return INT_MIN;     int mid = (b + e) >> 1;     return max(Max(l, r, u << 1, b, mid), Max(l, r, u << 1 | 1, mid + 1, e));   }   void makeZero(int pos, int u = 1, int b = 1, int e = n) {     if (b == e) {       return void(mx[u] = 0);     }     int mid = (b + e) >> 1;     if (pos <= mid) {       makeZero(pos, u << 1, b, mid);     } else {       makeZero(pos, u << 1 | 1, mid + 1, e);     }     mx[u] = max(mx[u << 1], mx[u << 1 | 1]);   }   int main(int argc, char const *argv[]) {     scanf( %d %d %d , &n, &m, &q);     for (int i = 1; i <= n; ++i) {       scanf( %d , &p[i]);     }     for (int i = 1; i < N; ++i) {       par[i] = i, sub[i] = 1;     }     for (int i = 1; i <= m; ++i) {       scanf( %d %d , &elist[i].l, &elist[i].r);     }     vector<ii> updates;     for (int i = 1; i <= q; ++i) {       int x, y;       scanf( %d %d , &x, &y);       updates.emplace_back(x, y);       if (x == 2) {         elist[y].tag = i;       }     }     for (int i = 1; i <= m; ++i) {       if (!elist[i].tag) {         elist[i].tag = q + i;       }     }     vi perm(m);     iota(begin(perm), end(perm), 1);     sort(begin(perm), end(perm),          [](int x, int y) { return elist[x].tag > elist[y].tag; });     for (int i : perm) {       int nu = n + i;       int x = find(elist[i].l);       int y = find(elist[i].r);       if (x != y) {         par[x] = par[y] = nu;         g[nu].emplace_back(x);         g[nu].emplace_back(y);       }     }     for (int i = 2; i <= 4; ++i) {     }     set<int> roots;     for (int i = 1; i <= n; ++i) {       roots.insert(find(i));     }     for (int x : roots) {       setPar(x, 0);       dfs(x);     }     build();     vi ans;     for (int i = 0; i < (int)(updates).size(); ++i) {       int tag = i + 1;       auto [op, u] = updates[i];       if (op == 1) {         int anc = getHighest(u, tag);         int mxVal = Max(in[anc], out[anc]);         ans.emplace_back(mxVal);         if (mxVal) makeZero(position[mxVal]);       } else {       }     }     for (int x : ans) {       printf( %d n , x);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, q;   long long f(long long md) { return n - md + min(n - md, md * (md - 1) / 2); }   signed main() {     cin >> q;     while (q--) {       cin >> n;       long long lo = 1, hi = n;       while (lo + 1 < hi) {         long long md = (lo + hi) >> 1;         if (f(md) < f(md + 1))           lo = md;         else           hi = md;       }       cout << max(f(hi), f(lo)) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int Maxn = 100005;   int n, q, tree_ct, k[Maxn], r[Maxn], ord[Maxn], Head[Maxn];   struct Tree {     int lson, rson;     long long k, b;   } tree[24 * Maxn];   void build_first(int root, int lt, int rt) {     if (lt + 1 == rt)       tree[root].k = k[lt], tree[root].b = 0;     else {       int mid = (lt + rt) >> 1;       tree[root].lson = ++tree_ct;       tree[root].rson = ++tree_ct;       build_first(tree[root].lson, lt, mid);       build_first(tree[root].rson, mid, rt);       tree[root].k = tree[tree[root].lson].k + tree[tree[root].rson].k;       tree[root].b = tree[tree[root].lson].b + tree[tree[root].rson].b;     }   }   void build(int root, int las, int lt, int rt, int pos) {     if (lt + 1 == rt)       tree[root].k = 0, tree[root].b = r[lt];     else {       int mid = (lt + rt) >> 1;       if (pos >= mid) {         tree[root].rson = ++tree_ct;         tree[root].lson = tree[las].lson;         build(tree[root].rson, tree[las].rson, mid, rt, pos);       } else {         tree[root].lson = ++tree_ct;         tree[root].rson = tree[las].rson;         build(tree[root].lson, tree[las].lson, lt, mid, pos);       }       tree[root].k = tree[tree[root].lson].k + tree[tree[root].rson].k;       tree[root].b = tree[tree[root].lson].b + tree[tree[root].rson].b;     }   }   pair<long long, long long> operator+(pair<long long, long long> a,                                        pair<long long, long long> b) {     return make_pair(a.first + b.first, a.second + b.second);   }   pair<long long, long long> ask(int root, int lt, int rt, int L, int R) {     if (lt == L && rt == R)       return make_pair(tree[root].k, tree[root].b);     else {       int mid = (lt + rt) >> 1;       if (L >= mid)         return ask(tree[root].rson, mid, rt, L, R);       else if (R <= mid)         return ask(tree[root].lson, lt, mid, L, R);       else         return ask(tree[root].lson, lt, mid, L, mid) +                ask(tree[root].rson, mid, rt, mid, R);     }   }   struct sta {     int lt, rt;     double t;     bool operator<(const sta &tmp) const { return lt < tmp.lt; }   };   set<sta> Se;   set<sta>::iterator spilt(int x) {     if (x > n) return Se.end();     sta tmp = *(--Se.upper_bound((sta){x, 0, 0}));     Se.erase(tmp);     if (tmp.lt != x) Se.insert((sta){tmp.lt, x - 1, tmp.t});     return Se.insert((sta){x, tmp.rt, tmp.t}).first;   }   pair<long long, long long> cal(double t, int lt, int rt) {     int L = 0, R = n;     while (L + 1 <= R) {       int mid = (L + R + 1) >> 1;       if (r[ord[mid]] > t * k[ord[mid]])         R = mid - 1;       else         L = mid;     }     return ask(Head[L], 1, n + 1, lt, rt + 1);   }   set<pair<int, int> > S;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int x;       scanf( %d%d%d , &x, &r[i], &k[i]), ord[i] = i;       if (!r[i] && !k[i]) r[i] = 1;       if (k[i])         Se.insert((sta){i, i, -x / (double)k[i]});       else         Se.insert((sta){i, i, 0}), S.insert(make_pair(i, x));     }     S.insert(make_pair(n + 1, 0));     Head[0] = ++tree_ct;     build_first(Head[0], 1, n + 1);     sort(ord + 1, ord + 1 + n, [](int x, int y) {       return r[x] * (long long)k[y] < r[y] * (long long)k[x];     });     for (int i = 1; i <= n; i++)       Head[i] = ++tree_ct, build(Head[i], Head[i - 1], 1, n + 1, ord[i]);     scanf( %d , &q);     while (q--) {       int t, lt, rt;       long long ans = 0;       scanf( %d%d%d , &t, &lt, &rt);       set<pair<int, int> >::iterator it;       while (S.size() > 1 &&              (it = S.lower_bound(make_pair(lt, 0)))->first <= rt) {         ans += it->second;         S.erase(it);       }       set<sta>::iterator it_r = spilt(rt + 1), it_l = spilt(lt), tmp_it;       tmp_it = it_l;       for (; it_l != it_r; it_l++) {         pair<long long, long long> now = cal(t - it_l->t, it_l->lt, it_l->rt);         ans += now.second + (long long)(now.first * (t - it_l->t) + 0.5);       }       Se.erase(tmp_it, it_r);       Se.insert((sta){lt, rt, (double)t});       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 100005;   long long n, x[maxn], y[maxn];   bool flag;   inline long long nxt(long long id) { return (id + 1) % n; }   inline bool check(long long i, long long j) {     return (x[i] + x[j] == x[nxt(i)] + x[nxt(j)]) &&            (y[i] + y[j] == y[nxt(i)] + y[nxt(j)]);   }   int main() {     scanf( %lld , &n);     if (n % 2 == 1) {       printf( No n );       return 0;     }     for (register long long i = 0; i < n; i++) scanf( %lld%lld , &x[i], &y[i]);     for (register long long i = 0; i * 2 < n; i++)       if (!check(i, i + (n / 2))) {         flag = true;         break;       }     if (flag)       printf( No n );     else       printf( Yes n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long GCD(long long a, long long b) {     while (b) {       a %= b;       swap(a, b);     }     return a;   }   int main() {     int n, k, i;     long double x, y, xant, yant;     scanf( %d%d , &n, &k);     double sol = 0;     for (i = 0; i < n; i++) {       cin >> x >> y;       if (i) sol += sqrt((x - xant) * (x - xant) + (y - yant) * (y - yant));       xant = x, yant = y;     }     sol = (sol * (long double)k) / 50.0;     printf( %.9lf n , sol);     return 0;   }


#include <bits/stdc++.h>   inline int A(int x, int y) { return x && y; }   inline int O(int x, int y) { return x ^ y; }   inline int X(int x, int y) { return x || y; }   int main() {     int t1, t2, t3, t4;     scanf( %d%d%d%d , &t1, &t2, &t3, &t4);     int a1 = O(t1, t2);     int a2 = X(t3, t4);     int a3 = A(t2, t3);     int a4 = O(t1, t4);     int b1 = A(a1, a2);     int b2 = X(a3, a4);     int c1 = O(b1, b2);     printf( %d , c1);   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ull = unsigned long long;   int t, n, m;   string x, y;   int main() {     cin >> t;     while (t--) {       cin >> x;       cin >> y;       n = (int)(x.length());       m = (int)(y.length());       reverse(x.begin(), x.end());       reverse(y.begin(), y.end());       int pos1 = 0;       while (y[pos1] !=  1 ) {         ++pos1;       }       int pos2 = pos1;       while (x[pos2] !=  1 ) {         ++pos2;       }       cout << pos2 - pos1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 5;   int w[MAXN];   map<int, int> f;   long long Calc(long long Res, long long Mod) {     if (Res > Mod) return Res % Mod + Mod;     return Res;   }   long long power(long long a, long long n, long long Mod) {     long long Res = 1;     while (n) {       if (n & 1) Res = Calc(Res * a, Mod);       a = Calc(a * a, Mod);       n >>= 1;     }     return Res;   }   int phi(int n) {     int result = 1;     if (f.count(n)) return f[n];     for (int i = 2; i * i <= n; ++i) {       if (n % i == 0) {         while (n % i == 0) {           n /= i;           result *= i;         }         result /= i;         result *= (i - 1);       }     }     if (n != 1) result *= (n - 1);     f[n] = result;     return f[n];   }   long long Solve(int l, int r, int Mod) {     if (l == r || Mod == 1) return Calc(w[l], Mod);     return power(w[l], Solve(l + 1, r, phi(Mod)), Mod);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int q, n, Mod;     cin >> n >> Mod;     for (int i = 1; i <= n; ++i) cin >> w[i];     cin >> q;     while (q--) {       int l, r;       cin >> l >> r;       long long ans = Solve(l, r, Mod);       cout << ans % Mod << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int check(int x) {     int sum = 0;     if (n <= ceil(x / 2)) sum++;     if (m <= ceil(x / 3)) sum++;     if ((n + m <= ceil(x / 2) + ceil(x / 3) - ceil(x / 6))) sum++;     if (sum == 3) return 1;     return 0;   }   int main() {     cin >> n >> m;     int x;     x = max(2 * n, max(3 * m, (2 * n + 3 * m) / 2));     for (int i = x; i <= 10000000; i++) {       if (check(i)) {         cout << i << endl;         break;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100000;   int prime[MAXN + 1];   void getPrime() {     memset(prime, 0, sizeof(prime));     for (int i = 2; i <= MAXN; i++) {       if (!prime[i]) prime[++prime[0]] = i;       for (int j = 1; j <= prime[0] && prime[j] <= MAXN / i; j++) {         prime[prime[j] * i] = 1;         if (i % prime[j] == 0) break;       }     }   }   unordered_map<int, bool> Mcnt;   unordered_map<int, int> M;   const int maxn = 105;   int a[maxn];   int getsg(int S) {     if (S == 0) return 0;     if (M.count(S)) return M[S];     vector<int> V;     V.clear();     for (int i = 0; i < 29; i++)       if (S & (1 << i)) V.push_back(i + 1);     int ma = 0;     for (int i = 0; i < V.size(); i++) ma = max(ma, V[i]);     int x[30];     set<int> pre;     pre.clear();     for (int i = 1; i <= ma; i++) {       for (int j = 0; j < V.size(); j++) {         if (V[j] >= i)           x[j] = V[j] - i;         else           x[j] = V[j];       }       int SS = 0;       for (int j = 0; j < V.size(); j++) {         if (x[j]) SS |= (1 << (x[j] - 1));       }       pre.insert(getsg(SS));     }     int now = 0;     while (pre.count(now)) now++;     M[S] = now;     return now;   }   int cnt[maxn];   int main() {     getPrime();     int ans = 0;     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= prime[0]; i++) {       memset(cnt, 0, sizeof(cnt));       for (int j = 1; j <= n; j++) {         while (a[j] % prime[i] == 0) {           a[j] /= prime[i];           cnt[j]++;         }       }       int S = 0;       for (int j = 1; j <= n; j++)         if (cnt[j]) S |= (1 << (cnt[j] - 1));       ans ^= getsg(S);     }     for (int i = 1; i <= n; i++)       if (a[i] != 1) {         if (!Mcnt.count(a[i])) {           ans ^= 1;           Mcnt[a[i]] = true;         }       }     if (ans == 0)       printf( Arpa );     else       printf( Mojtaba );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k;     cin >> n >> m >> k;     vector<vector<int>> v;     vector<int> cur;     if ((n * m) % k == 0) {       int div = (n * m) / k;       for (int i = 0; i < n; i++) {         if (!(i & 1)) {           for (int j = 0; j < m; j++) {             cur.push_back(i + 1);             cur.push_back(j + 1);             if (cur.size() >= div * 2) {               vector<int> add = cur;               v.push_back(add);               cur.clear();             }           }         } else {           for (int j = m - 1; j >= 0; j--) {             if (cur.size() <= div * 2) {               cur.push_back(i + 1);               cur.push_back(j + 1);             }             if (cur.size() >= div * 2) {               vector<int> add = cur;               v.push_back(add);               cur.clear();             }           }         }       }     } else {       int div = ((n * m) - 2) / (k - 1);       int count = 0;       int flag = 0;       for (int i = 0; i < n; i++) {         if (!(i & 1)) {           for (int j = 0; j < m; j++) {             if (count == k - 1 && flag == 0) {               div = (n * m - (k - 1) * (div));               flag = 1;             }             cur.push_back(i + 1);             cur.push_back(j + 1);             if (cur.size() >= div * 2) {               vector<int> add = cur;               v.push_back(add);               cur.clear();               count++;             }           }         } else {           for (int j = m - 1; j >= 0; j--) {             if (count == k - 1 && flag == 0) {               div = (n * m - (k - 1) * (div));               flag = 1;             }             if (cur.size() <= div * 2) {               cur.push_back(i + 1);               cur.push_back(j + 1);             }             if (cur.size() >= div * 2) {               vector<int> add = cur;               v.push_back(add);               cur.clear();               count++;             }           }         }       }     }     for (int i = 0; i < v.size(); i++) {       int s = v[i].size();       s /= 2;       cout << s <<    ;       for (int j = 0; j < v[i].size(); j++) {         cout << v[i][j] <<    ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e5 + 7;   int n;   int v[N], f[N];   bool e[N];   int st, dr;   bool ok(int l, int r) {     for (int i = 1; i <= n; i++) {       e[i] = 0;       f[i] = 0;     }     for (int i = l; i <= r; i++) {       e[i] = 1;       f[v[i]]++;     }     for (int i = 1; i < n + 1 - i; i++) {       if (e[i] == 1 && e[n + 1 - i] == 0) {         if (f[v[n + 1 - i]] > 0) {           f[v[n + 1 - i]]--;         } else {           return 0;         }       }       if (e[i] == 0 && e[n + 1 - i] == 1) {         if (f[v[i]] > 0) {           f[v[i]]--;         } else {           return 0;         }       }       if (e[i] == 0 && e[n + 1 - i] == 0 && v[i] != v[n + 1 - i]) {         return 0;       }     }     int cntimp = 0;     for (int i = 1; i <= n; i++) {       if (f[i] % 2 == 1) {         cntimp++;       }     }     return (cntimp <= 1);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> v[i];       f[v[i]]++;     }     int imp = 0;     for (int i = 1; i <= n; i++) {       if (f[i] % 2 == 1) {         imp++;       }     }     if (imp >= 2) {       cout <<  0 n ;       return 0;     }     bool pali = 1;     for (int i = 1; i < n + 1 - i; i++) {       if (v[i] != v[n + 1 - i]) {         pali = 0;       }     }     if (pali) {       long long res = n * (long long)(n + 1) >> 1;       cout << res <<   n ;       return 0;     }     st = 1;     dr = n;     while (v[st] == v[dr]) {       st++;       dr--;     }     int lo = st;     int hi = dr;     int prf = -1, suf = -1;     while (lo <= hi) {       int mid = (lo + hi) >> 1;       if (ok(st, mid)) {         prf = mid;         hi = mid - 1;       } else {         lo = mid + 1;       }     }     lo = 1;     hi = n;     while (lo <= hi) {       int mid = (lo + hi) >> 1;       if (ok(mid, dr)) {         suf = mid;         lo = mid + 1;       } else {         hi = mid - 1;       }     }     int cntgood = 0;     if (prf != -1) {       cntgood += (dr - prf + 1);     }     if (suf != -1) {       cntgood += (suf - st + 1);     }     long long res =         cntgood * (long long)st - 1 + (st - 1LL) * (long long)(st - 1LL);     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int l, r, x, y;     double k;     cin >> l >> r >> x >> y >> k;     bool flag = false;     double value;     while (x <= y) {       value = (double)x * k;       if (value >= l && value <= r) {         flag = true;         break;       }       x++;     }     if (flag) {       cout <<  YES ;     } else {       cout <<  NO ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Tpoint {     double x, y;   } v[200], pg[200];   struct Tline {     Tpoint a, b;   } ls[200];   int n, tot, q[200], ord[200];   double at2[200];   double getdouble() {     double d = 0, d2, d3;     char ch =    ;     bool flag = false;     while (!isdigit(ch = getchar()))       if (ch ==  - ) break;     if (ch ==  - )       flag = 1;     else       d = ch - 48;     while (isdigit(ch = getchar())) d = d * 10 + ch - 48;     if (ch ==  - ) {       d2 = 0;       d3 = 1;       while (isdigit(ch = getchar())) d2 = d2 * 10 + ch - 48, d3 /= 10;       d += d2 * d3;     }     if (flag)       return -d;     else       return d;   }   void init() {     int i, w, h;     double angle;     n = 4;     scanf( %d%d%lf , &w, &h, &angle);     v[1].x = (double)w / 2;     v[1].y = -(double)h / 2;     v[2].x = (double)w / 2;     v[2].y = (double)h / 2;     v[3].x = -(double)w / 2;     v[3].y = (double)h / 2;     v[4].x = -(double)w / 2;     v[4].y = -(double)h / 2;     for (int i = 1; i < n; i++) ls[i].a = v[i], ls[i].b = v[i + 1];     ls[n].a = v[n];     ls[n].b = v[1];     angle = angle * 3.1415926535897932384626433832795 / 180;     for (i = 1; i <= 4; i++) {       v[i + n].x = v[i].x * cos(angle) - v[i].y * sin(angle);       v[i + n].y = v[i].x * sin(angle) + v[i].y * cos(angle);     }     for (int i = 1; i < n; i++)       ls[n + i].a = v[n + i], ls[n + i].b = v[n + i + 1];     ls[n + n].a = v[n + n];     ls[n + n].b = v[n + 1];     n = n + n;   }   int sig(double d) { return d < -1e-8 ? -1 : d > 1e-8; }   double multi(Tpoint p0, Tpoint p1, Tpoint p2) {     return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);   }   int cmp(const void *a, const void *b) {     int p1 = *(int *)a, p2 = *(int *)b;     if (sig(at2[p1] - at2[p2]) == 0) {       return -sig(multi(ls[p2].a, ls[p2].b, ls[p1].b));     } else       return sig(at2[p2] - at2[p1]);   }   Tpoint ispoint(Tline l1, Tline l2) {     double dot1, dot2;     Tpoint I;     dot1 = multi(l2.a, l1.b, l1.a);     dot2 = multi(l1.b, l2.b, l1.a);     I.x = (l2.a.x * dot2 + l2.b.x * dot1) / (dot1 + dot2);     I.y = (l2.a.y * dot2 + l2.b.y * dot1) / (dot1 + dot2);     return I;   }   bool judgein(int x, int y, int z) {     Tpoint pnt = ispoint(ls[x], ls[y]);     return sig(multi(ls[z].a, ls[z].b, pnt)) < 0;   }   void halfplane() {     int i, tmp, front, rear;     for (i = 1; i <= n; i++) {       at2[i] = atan2(ls[i].b.y - ls[i].a.y, ls[i].b.x - ls[i].a.x);       ord[i] = i;     }     qsort(ord + 1, n, sizeof(int), cmp);     for (i = 2, tmp = 1; i <= n; i++) {       if (sig(at2[ord[i]] - at2[ord[i - 1]]) != 0) ord[++tmp] = ord[i];     }     front = 1;     rear = 2;     q[1] = ord[1];     q[2] = ord[2];     for (i = 3; i <= tmp; i++) {       while (front < rear && judgein(q[rear - 1], q[rear], ord[i])) rear--;       while (front < rear && judgein(q[front + 1], q[front], ord[i])) front++;       q[++rear] = ord[i];     }     while (front < rear && judgein(q[rear - 1], q[rear], q[front])) rear--;     while (front < rear && judgein(q[front + 1], q[front], q[rear])) front++;     q[--front] = q[rear];     tot = 0;     for (i = front + 1; i <= rear; i++) {       pg[++tot] = ispoint(ls[q[i - 1]], ls[q[i]]);     }   }   void work() {     halfplane();     int i;     double ans = 0;     for (i = 1; i < tot; i++) {       ans += multi(pg[1], pg[i], pg[i + 1]);     }     ans += multi(pg[1], pg[tot], pg[1]);     ans /= 2;     if (fabs(ans) > 0)       printf( %.8lf , fabs(ans));     else       printf( 0.0 );   }   int main() {     init();     work();     return 0;   }


#include <iostream>   #include <string>   #include <algorithm>   #include <map>   #include <vector>   #include <cmath>   #include <queue>   #include <set>   #include <bitset>      using namespace std;      typedef long long ll;   ll MOD = 1000000007;      void solve(int testCase)   {    ll n;    cin >> n;    for (int i = 0; i * 2020 <= n; i++)    {     if ((n - i * 2020) % 2021 == 0)     {      cout <<  YES  << endl;      return;     }    }    cout <<  NO  << endl;      }       int main()   {    int t;    cin >> t;    //t = 1;    int i = 1;    while (t--)    {     solve(i++);    }    return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void chkmin(T &a, const T &b) {     a = a < b ? a : b;   }   template <typename T>   inline void chkmax(T &a, const T &b) {     a = a > b ? a : b;   }   const int MAXN = 150005, MAXT = 300005;   int sz[MAXN], val[MAXN], vis[MAXN], n, tot;   vector<int> gra[MAXN];   struct Tree {     long long k, b;     Tree *ls, *rs;     bool did;   } nd[MAXT], *rt;   vector<Tree *> cls;   long long ans;   Tree *build(int l, int r) {     Tree *cur = nd + (tot++);     cur->did = false;     if (l == r) return cur;     int mid = (l + r) >> 1;     cur->ls = build(l, mid);     cur->rs = build(mid + 1, r);     return cur;   }   void modify(long long k, long long b, int l = 1, int r = n, Tree *p = rt) {     if (p->did) {       long long f1 = p->k * l + p->b, f2 = p->k * r + p->b, g1 = k * l + b,                 g2 = k * r + b;       if (g1 >= f1 && g2 >= f2) {         p->k = k, p->b = b;         return;       } else if (g1 <= f1 && g2 <= f2)         return;       double in = 1.0 * (b - p->b) / (k - p->k);       int mid = (l + r) >> 1;       if (in <= mid) {         if (f1 >= g1 && f2 <= g2) {           modify(p->k, p->b, l, mid, p->ls);           p->k = k, p->b = b;         } else           modify(k, b, l, mid, p->ls);       } else {         if (f1 <= g1 && f2 >= g2) {           modify(p->k, p->b, mid + 1, r, p->rs);           p->k = k, p->b = b;         } else           modify(k, b, mid + 1, r, p->rs);       }     } else       p->k = k, p->b = b, p->did = true, cls.push_back(p);   }   long long query(int x, int l = 1, int r = n, Tree *p = rt) {     if (!p->did) return -1e18;     int mid = (l + r) >> 1;     long long res = p->k * x + p->b;     if (x <= mid)       chkmax(res, query(x, l, mid, p->ls));     else       chkmax(res, query(x, mid + 1, r, p->rs));     return res;   }   void get_sz(int u, int fa) {     sz[u] = 1;     for (int v : gra[u])       if (v != fa && !vis[v]) {         get_sz(v, u);         sz[u] += sz[v];       }   }   pair<int, int> get_rt(int u, int fa, int s) {     auto res = make_pair(MAXN, MAXN);     int mn = s;     for (int v : gra[u])       if (v != fa && !vis[v]) {         res = min(res, get_rt(v, u, s + sz[u] - sz[v]));         chkmax(mn, sz[v]);       }     return min(res, make_pair(mn, u));   }   void dfs1(int u, int fa, int dep, long long sum, long long cur, long long tmp) {     sum += val[u], cur += sum, ++dep, tmp += (long long)dep * val[u];     chkmax(ans, cur), chkmax(ans, tmp);     chkmax(ans, query(dep) + cur);     for (int v : gra[u])       if (v != fa && !vis[v]) dfs1(v, u, dep, sum, cur, tmp);   }   void dfs2(int u, int fa, int dep, long long sum, long long cur) {     ++dep, sum += val[u], cur += (long long)dep * val[u];     modify(sum, cur);     for (int v : gra[u])       if (v != fa && !vis[v]) dfs2(v, u, dep, sum, cur);   }   void divide(int u) {     get_sz(u, 0);     u = get_rt(u, 0, 0).second;     vis[u] = 1;     for (int v : gra[u])       if (!vis[v]) {         dfs1(v, u, 1, val[u], val[u], val[u]);         dfs2(v, u, 0, 0, 0);       }     for (Tree *p : cls) p->did = false;     cls.clear();     reverse(gra[u].begin(), gra[u].end());     for (int v : gra[u])       if (!vis[v]) {         dfs1(v, u, 1, val[u], val[u], val[u]);         dfs2(v, u, 0, 0, 0);       }     for (Tree *p : cls) p->did = false;     cls.clear();     for (int v : gra[u])       if (!vis[v]) divide(v);     vis[u] = 0;   }   int main() {     scanf( %d , &n);     rt = build(1, n);     for (int i = 1; i < n; i++) {       int u, v;       scanf( %d%d , &u, &v);       gra[u].push_back(v);       gra[v].push_back(u);     }     for (int i = 1; i <= n; i++) scanf( %d , val + i);     divide(1);     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int arr[200007];   int brr[200007];   int main() {     int t;     cin >> t;     while (t--) {       int n, x;       cin >> n >> x;       map<int, int> mpp;       for (int i = 0; i < n; i++) {         cin >> arr[i];         mpp[arr[i]]++;       }       int cnt = 0;       for (int i = 1;; i++) {         if (x == 0) break;         if (mpp[i] == 0) {           mpp[i]++;           x--;         }       }       for (int i = 1;; i++) {         if (mpp[i] == 0)           break;         else           cnt++;       }       cout << cnt << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 1;   int mark[N], t[N], n;   long long res;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     ;     cin >> n;     for (int i = (1); i <= (n); i += (1)) cin >> mark[i];     int cur = 0;     for (int i = (n); i >= (1); i -= (1)) {       cur = max(0, cur - 1);       cur = max(cur, mark[i] + 1);       t[i] = cur;     }     cur = 0;     for (int i = (1); i <= (n); i += (1)) {       cur = max(cur, t[i]);       res += cur;     }     for (int i = (1); i <= (n); i += (1)) res -= (mark[i] + 1);     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int arr[n];       for (int i = 0; i < n; i++) {         cin >> arr[i];       }       sort(arr, arr + n);       int ans = 1;       for (int i = 1; i < n; i++) {         if (arr[i] > arr[i - 1]) ans++;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace Base {   const int inf = 0x3f3f3f3f, INF = 0x7fffffff;   const long long infll = 0x3f3f3f3f3f3f3f3fll, INFll = 0x7fffffffffffffffll;   template <typename T>   void read(T &x) {     x = 0;     int fh = 1;     double num = 1.0;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) fh = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     if (ch ==  . ) {       ch = getchar();       while (isdigit(ch)) {         num /= 10;         x = x + num * (ch -  0 );         ch = getchar();       }     }     x = x * fh;   }   template <typename T>   void chmax(T &x, T y) {     x = x < y ? y : x;   }   template <typename T>   void chmin(T &x, T y) {     x = x > y ? y : x;   }   }  // namespace Base   using namespace Base;   const int N = 510;   bool tag[N];   int nxt[N], n, m, len[N], pre[N][N], bak[N][N], now[N], ch[N];   long long k, cnt[N];   void kmp(int *a, int lena, int *b, int lenb) {     memset(tag, 0, sizeof(tag));     for (int i = 1; i <= lenb; i++) {       int p = i - 1;       while (p != 0 && b[nxt[p] + 1] != b[i]) p = nxt[p];       if (b[nxt[p] + 1] != b[i] || i == 1)         nxt[i] = 0;       else         nxt[i] = nxt[p] + 1;     }     for (int i = 1, j = 1; i <= lena; i++) {       while (j != 1 && a[i] != b[j]) j = nxt[j - 1] + 1;       if (a[i] == b[j]) j++;       if (j > lenb) {         tag[i] = true, j = nxt[j - 1] + 1;       }     }   }   int main() {     read(n);     read(k);     read(m);     k = k + 1;     len[0] = len[1] = 1;     pre[0][1] = 0;     pre[1][1] = 1;     bak[0][1] = 0;     bak[1][1] = 1;     for (int j = 2; j <= n; j++) {       for (int k = 1; k <= len[j - 2]; k++) pre[j][k] = pre[j - 2][k];       if (len[j - 2] < m) {         for (int k = len[j - 2] + 1; k <= min(len[j - 2] + len[j - 1], m); k++)           pre[j][k] = pre[j - 1][k - len[j - 2]];       }       int tmp = 0;       if (len[j - 1] < m) {         tmp = min(len[j - 2], m - len[j - 1]);         for (int k = 1; k <= tmp; k++)           bak[j][k] = bak[j - 2][len[j - 2] - tmp + k];       }       for (int k = 1; k <= len[j - 1]; k++) bak[j][k + tmp] = bak[j - 1][k];       len[j] = min(m, len[j - 1] + len[j - 2]);     }     for (int i = 1; i <= m; i++) {       bool flag = false;       for (int j = 1; j <= i - 1; j++)         if (bak[n][len[n] - i + j + 1] != ch[j]) {           flag = true;           break;         }       if (flag == false) {         k--;         if (k == 0) {           for (int j = 1; j <= i - 1; j++) printf( %d , ch[j]);           printf(  n );           exit(0);         }       }       flag = false;       ch[i] = 0;       memset(cnt, 0, sizeof(cnt));       if (i == 1) cnt[0] = 1;       for (int j = 2; j <= n; j++) {         cnt[j] = cnt[j - 1] + cnt[j - 2];         if (cnt[j] >= k) {           flag = true;           break;         }         int id = 0, cut = 0;         for (int k = 1; k <= len[j - 2]; k++) now[++id] = bak[j - 2][k];         cut = id;         for (int k = 1; k <= len[j - 1]; k++) now[++id] = pre[j - 1][k];         kmp(now, id, ch, i);         for (int k = cut + 1; k <= min(cut + i - 1, id); k++)           if (tag[k] == true) cnt[j]++;       }       if (cnt[n] >= k || flag == true)         ch[i] = 0;       else         ch[i] = 1, k -= cnt[n];     }     for (int i = 1; i <= m; i++) printf( %d , ch[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a[100000], p[100000];     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i] >> p[i];     for (int i = 1; i < n; ++i) p[i] = min(p[i], p[i - 1]);     int ans = 0;     for (int i = 0; i < n; ++i) ans += p[i] * a[i];     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace ::std;   const long long maxn = 52;   const long long mod = 1e9 + 7;   const long long inf = 1e9 + 500;   long long dp[2][maxn][maxn][maxn][maxn];   long long d[maxn];   bool bit;   long long kv;   void add(long long a, long long b, long long c, long long d, long long z) {     if (0 <= a && 0 <= b && 0 <= c && 0 <= d && a < maxn && b < maxn &&         c < maxn && d < maxn) {       dp[bit][a][b][c][d] += z * kv;       dp[bit][a][b][c][d] %= mod;     }   }   int main() {     long long n;     cin >> n;     for (long long i = 0; i < n; i++) {       cin >> d[i];     }     long long a1 = 0, a2 = 0, b1 = 0, b2 = 0;     if (d[0] == 2) {       b1++;     } else {       b2++;     }     if (d[1] == 2) {       a1++;     } else {       a2++;     }     dp[1][a1][a2][b1][b2] = 1;     for (long long i = 2; i < n; i++) {       bit = (i & 1);       long long k = i;       for (long long a1 = 0; a1 <= k; a1++) {         for (long long a2 = 0; a2 + a1 <= k; a2++) {           for (long long b1 = 0; b1 + a1 + a2 <= k; b1++) {             for (long long b2 = 0; b2 + a1 + a2 + b1 <= k; b2++) {               dp[bit][a1][a2][b1][b2] = 0;             }           }         }       }       for (long long a1 = 0; a1 <= k; a1++) {         for (long long a2 = 0; a2 + a1 <= k; a2++) {           for (long long b1 = 0; b1 + a1 + a2 <= k; b1++) {             for (long long b2 = 0; b2 + a1 + a2 + b1 <= k; b2++) {               kv = dp[!bit][a1][a2][b1][b2];               if (d[i] == 2) {                 add(a1 - 1, a2, b1 - 1, b2, a1 * b1);                 add(a1 + 1, a2 - 1, b1 - 1, b2, a2 * b1);                 add(a1 + 1, a2, b1 - 1, b2, b1);                 add(a1 - 1, a2, b1 + 1, b2 - 1, a1 * b2);                 add(a1 + 1, a2 - 1, b1 + 1, b2 - 1, a2 * b2);                 add(a1 + 1, a2, b1 + 1, b2 - 1, b2);                 if (b1 == 0 && b2 == 0) {                   add(1, 0, a1 - 1, a2, a1);                   add(1, 0, a1 + 1, a2 - 1, a2);                 }               } else {                 if (b1 == 0 && b2 == 0) {                   add(0, 1, a1 - 1, a2, a1);                   add(0, 1, a1 + 1, a2 - 1, a2);                 }                 add(a1, a2 + 1, b1 - 1, b2, b1);                 add(a1 + 1 + 1, a2 - 1, b1 - 1, b2, b1 * a2);                 add(a1 + 1 - 1, a2, b1 - 1, b2, b1 * a1);                 add(a1 - 2, a2, b1 - 1, b2, b1 * (a1 * (a1 - 1)) / 2);                 add(a1 + 2, a2 - 2, b1 - 1, b2, b1 * (a2 * (a2 - 1)) / 2);                 add(a1 + 1 - 1, a2 - 1, b1 - 1, b2, b1 * a1 * a2);                 add(a1, a2 + 1, b1 + 1, b2 - 1, b2);                 add(a1 + 1 + 1, a2 - 1, b1 + 1, b2 - 1, b2 * a2);                 add(a1 + 1 - 1, a2, b1 + 1, b2 - 1, b2 * a1);                 add(a1 - 2, a2, b1 + 1, b2 - 1, b2 * (a1 * (a1 - 1)) / 2);                 add(a1 + 2, a2 - 2, b1 + 1, b2 - 1, b2 * (a2 * (a2 - 1)) / 2);                 add(a1 + 1 - 1, a2 - 1, b1 + 1, b2 - 1, b2 * a1 * a2);               }             }           }         }       }       k++;       for (long long a1 = 0; a1 <= k; a1++) {         for (long long a2 = 0; a2 + a1 <= k; a2++) {           for (long long b1 = 0; b1 + a1 + a2 <= k; b1++) {             for (long long b2 = 0; b2 + a1 + a2 + b1 <= k; b2++) {               dp[bit][a1][a2][b1][b2] %= mod;             }           }         }       }     }     long long ans = 0;     bool bb = (n - 1) & 1;     ans = dp[bb][0][0][0][0];     cout << ans % mod;   }


#include <bits/stdc++.h>   using namespace std;   void write(vector<int> &v) {     for (auto i : v) cout << i <<    ;     cout <<   n ;   }   void read(vector<int> &v) {     for (auto &i : v) cin >> i;   }   const int INF = 1e9;   const int64_t INFF = 1e18;   const int N = 1e6 + 69;   void solve() {     int n;     cin >> n;     string s1, s2;     cin >> s1 >> s2;     int ans = 0;     vector<int> pos(n, 1);     for (int i = 0; i < n; i++) {       if (s2[i] ==  1 ) {         if (s1[i] ==  0 ) {           ans++;           pos[i] = 0;         } else if (i > 0 && s1[i - 1] ==  1  && pos[i - 1]) {           ans++;           pos[i - 1] = 0;         } else if (i < n - 1 && s1[i + 1] ==  1  && pos[i + 1]) {           ans++;           pos[i + 1] = 0;         }       }     }     cout << ans <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     cin >> t;     for (int i = 1; i <= t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long p = 1e9 + 7;   inline long long read() {     long long neg = 1, num = 0;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) neg = -1;     for (; isdigit(c); c = getchar()) num = (num << 1) + (num << 3) + c -  0 ;     return neg * num;   }   long long n, k, tot, f[200010], g[200010], fac[200010], inv[200010],       pri[200010], mu[200010], mul[200010];   bool vis[200010];   inline long long Pow(long long x, long long y) {     long long res = 1;     for (; y; y >>= 1, x = x * x % p)       if (y & 1) res = res * x % p;     return res;   }   inline long long C(long long x, long long y) {     return fac[x] * inv[y] % p * inv[x - y] % p;   }   inline long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }   inline void sieve() {     vis[1] = mu[1] = 1;     for (long long i = 2; i <= n; i++) {       if (!vis[i]) pri[++tot] = i, mu[i] = -1;       for (long long j = 1; j <= tot && pri[j] * i <= n; j++) {         vis[i * pri[j]] = 1;         if (i % pri[j] == 0) break;         mu[i * pri[j]] = -mu[i];       }     }     fac[0] = inv[0] = mul[0] = 1;     for (long long i = 1; i <= 2 * n; i++)       fac[i] = fac[i - 1] * i % p, mul[i] = mul[i - 1] * k % p;     inv[2 * n] = Pow(fac[2 * n], p - 2);     for (long long i = 2 * n - 1; i >= 1; i--) inv[i] = inv[i + 1] * (i + 1) % p;     for (long long i = 2; i <= n; i++)       f[i] = (f[i - 1] + (i - 1) * mul[i] % p) % p;     for (long long i = 1; i <= n; i++) g[i] = Pow(f[n / i], i) % p;   }   signed main() {     n = read();     k = read();     sieve();     long long ans = 0, sub = 0;     for (long long i = 2; i <= 2 * n; i++) {       if (i <= n)         ans = (ans + (i - 1) * mul[i] % p) % p;       else         ans = (ans + (2 * n - i + 1) * mul[i] % p) % p;     }     ans = ans * Pow(mul[n], p - 2) % p;     for (long long d = 1; d <= n; d++) {       for (long long T = d; T <= n; T += d) {         long long s1 = (n / T) * (n / T - 1) / 2 % p, s2 = g[T];         if ((n + d - 1) / T != n / T)           s1 = (s1 + (n + d - 1) / T - 1) % p,           s2 = (s2 + ((n + d - 1) / T - 1) * mul[(n + d - 1) / T * T] % p) % p;         ans = (ans + s1 * mul[d] * mu[T / d] % p) % p;         sub = (sub + s2 * mu[T / d]) % p;       }     }     sub = sub * Pow(mul[n], p - 2) % p;     ans = (ans - sub) % p;     for (long long i = 1; i <= n; i++)       ans = (ans - (1 + (i != n)) * mul[max(gcd(i, n), i)]) % p;     ans = ans * Pow(mul[n], p - 2) % p;     ans = (ans % p + p) % p;     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int num[100020], nxt[100020];   struct point {     int st, en, num;   } pnt[100020 * 4];   int max1(int a, int b) {     if (a > b) return a;     return b;   }   int main() {     int n, sum = 0, cnt = 0, besti, min1 = 1 << 25, tmp, i;     scanf( %d , &n);     num[n + 1] = nxt[0] = num[0] = 0;     for (i = 1; i <= n; i++) {       scanf( %d , &num[i]), nxt[i] = i;     }     for (i = 0; i < 100020 * 3; i++) pnt[i].st = pnt[i].en = pnt[i].num = 0;     for (i = 1; i <= n + 1; i++) {       if (num[i] >= num[i - 1]) {         sum += num[i] - num[i - 1];       } else {         besti = i - 1;         while (num[besti] > num[i]) {           tmp = max1(num[nxt[besti] - 1], num[i]);           pnt[cnt].en = i - 1;           pnt[cnt].st = nxt[besti], pnt[cnt++].num = num[besti] - tmp;           nxt[i] = nxt[besti];           besti = nxt[besti] - 1;         }         if (num[besti] >= num[i]) nxt[i] = nxt[besti];       }     }     printf( %d n , sum);     for (i = 0; i < cnt; i++) {       for (int j = 0; j < pnt[i].num; j++)         printf( %d %d n , pnt[i].st, pnt[i].en);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<string, int> mp;   map<int, string> g;   int n, k, cnt;   char name1[10100][30], name2[10100][30];   int mark[1010][1010];   int x[10100], y[10100], tt[10100];   int ans[10100], num;   struct Node {     int l, r;   } node[10100];   struct Mark {     int l, r;     Mark() {}     Mark(int a, int b) {       if (a > b) swap(a, b);       l = a;       r = b;     }     bool operator<(const Mark a) const {       if (l == a.l) return r < a.r;       return l < a.l;     }   };   map<Mark, int> vis;   int vv;   int main() {     string s1, s2;     int u, v, t;     while (scanf( %d%d , &n, &k) == 2) {       cnt = 0;       num = 0;       vv = 0;       vis.clear();       mp.clear();       for (int i = 0; i < n; i++) {         scanf( %s%s%d , name1[i], name2[i], &t);         s1 = name1[i];         s2 = name2[i];         if (!mp.count(s1)) {           mp[s1] = ++cnt;           g[cnt] = s1;         }         u = mp[s1];         if (!mp.count(s2)) {           mp[s2] = ++cnt;           g[cnt] = s2;         }         v = mp[s2];         x[i] = u;         y[i] = v;         tt[i] = t;         for (int j = 0; j < n; j++) {           if (tt[i] != tt[j] && tt[i] - tt[j] <= k) {             if (x[i] == y[j] && x[j] == y[i]) {               if (!vis.count(Mark(x[i], y[i]))) {                 node[num].l = x[i];                 node[num++].r = y[i];                 vis[Mark(x[i], y[i])] = vv++;               }             }           }         }       }       printf( %d n , num);       for (int i = 0; i < num; i++)         cout << g[node[i].l] <<     << g[node[i].r] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   const int LG_N = 20;   struct node {     int l, r, cnt;     node *left, *right;     node(int _l, int _r, int _c = 0) : l(_l), r(_r), cnt(_c) {}   };   node *init(int l, int r) {     node *p = new node(l, r);     if (l < r) {       int mid = (l + r) / 2;       p->left = init(l, mid);       p->right = init(mid + 1, r);     }     return p;   }   node *update(node *p, int idx) {     node *q = new node(p->l, p->r, p->cnt + 1);     if (p->l < p->r) {       int mid = (p->l + p->r) / 2;       q->left = (idx <= mid ? update(p->left, idx) : p->left);       q->right = (idx > mid ? update(p->right, idx) : p->right);     }     return q;   }   int seg_query(node *p, int l, int r) {     if (p->r < l or r < p->l) return 0;     if (l <= p->l and p->r <= r) return p->cnt;     return seg_query(p->left, l, r) + seg_query(p->right, l, r);   }   struct Query {     int type, u, v, k, y;     void read(void) {       scanf( %d , &type);       if (type == 1) {         scanf( %d , &u);       } else {         scanf( %d%d%d%d , &u, &v, &k, &y);       }     }   } query[N];   node *head[N];   vector<int> adj[N];   int P[N][LG_N], H[N];   int attack_year[N];   int n, m;   int root = 1;   int count(int u, int v, int l, int r) {     int ans = 0;     if (l > 0) {       ans += seg_query(head[u], 1, l - 1);       ans -= seg_query(head[v], 1, l - 1);     }     if (r < m + 1) {       ans += seg_query(head[u], r + 1, m + 1);       ans -= seg_query(head[v], r + 1, m + 1);     }     return ans;   }   int my_advance(int u, int l) {     for (int i = LG_N - 1; i >= 0; i--) {       if ((1 << i) <= l) {         l -= 1 << i;         u = P[u][i];       }     }     return u;   }   int lca(int u, int v) {     if (H[u] < H[v]) swap(u, v);     for (int i = LG_N - 1; i >= 0; i--) {       if (H[P[u][i]] >= H[v]) {         u = P[u][i];       }     }     if (u == v) return u;     for (int i = LG_N - 1; i >= 0; i--) {       if (P[u][i] != P[v][i]) {         u = P[u][i];         v = P[v][i];       }     }     return P[u][0];   }   void dfsInit(int u, int h) {     H[u] = h;     for (int i = 1; i < LG_N; i++) {       P[u][i] = P[P[u][i - 1]][i - 1];     }     head[u] = update(u == root ? (head[0] = init(0, m + 1)) : head[P[u][0]],                      attack_year[u]);     for (int v : adj[u]) {       dfsInit(v, h + 1);     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       int u;       scanf( %d , &u);       if (u == 0) {         root = i;       }       adj[u].push_back(i);       P[i][0] = u;     }     scanf( %d , &m);     for (int i = 1; i <= n; i++) {       attack_year[i] = m + 1;     }     for (int i = 1; i <= m; i++) {       query[i].read();       if (query[i].type == 1) {         attack_year[query[i].u] = i;       }     }     H[0] = 0;     dfsInit(root, 1);     for (int i = 1; i <= m; i++) {       if (query[i].type == 1) continue;       int u = query[i].u, v = query[i].v, k = query[i].k, y = query[i].y;       int l = lca(u, v);       k += (attack_year[u] < y + 1 or attack_year[u] > i);       int lo = 0, hi = H[u] - H[l];       while (lo < hi) {         int mid = (lo + hi) / 2;         if (count(u, my_advance(u, mid + 1), y + 1, i) >= k) {           hi = mid;         } else {           lo = mid + 1;         }       }       k -= count(u, my_advance(u, lo + 1), y + 1, i);       u = my_advance(u, lo);       if (k == 0) {         printf( %d n , (u == v ? -1 : u));         continue;       }       k = count(v, l, y + 1, i) - k + 1;       if (k >= 0) {         lo = 0, hi = H[v] - H[l];         while (lo < hi) {           int mid = (lo + hi) / 2;           if (count(v, my_advance(v, mid + 1), y + 1, i) >= k) {             hi = mid;           } else {             lo = mid + 1;           }         }         k -= count(v, my_advance(v, lo + 1), y + 1, i);         v = my_advance(v, lo);         if (lo == 0) {           k = -1;         }       }       if (k == 0) {         printf( %d n , v);       } else {         printf( -1 n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   int a[maxn], n, s, b[maxn], num[maxn], len;   int ufs[maxn], meme[maxn];   inline int find(int u) {     if (ufs[u] == u)       return u;     else       return ufs[u] = find(ufs[u]);   }   inline void join(int u, int v, int e) {     u = find(u), v = find(v);     if (u == v) return;     ufs[v] = u;     meme[u] = e;   }   vector<vector<int> > mema;   vector<pair<int, int> > adj[maxn];   vector<int> curm;   inline void dfs(int u) {     while (adj[u].size()) {       int v = adj[u].back().first, id = adj[u].back().second;       adj[u].pop_back();       dfs(v);       curm.push_back(id);     }   }   int main() {     cin >> n >> s;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       num[++len] = a[i];     }     sort(num + 1, num + len + 1);     len = unique(num + 1, num + len + 1) - num - 1;     for (int i = 1; i <= n; i++) {       a[i] = lower_bound(num + 1, num + len + 1, a[i]) - num;       b[i] = a[i];       ufs[i] = i;     }     sort(b + 1, b + n + 1);     int cnt = 0;     for (int i = 1; i <= n; i++) {       if (a[i] != b[i]) {         cnt++;         join(a[i], b[i], i);       }     }     if (cnt > s) {       cout <<  -1  << endl;       return 0;     }     if (cnt == 0) {       cout <<  0  << endl;       return 0;     }     if (s - cnt > 1) {       for (int i = 1; i <= len; i++) {         if (ufs[i] == i && meme[i] != 0) {           curm.push_back(meme[i]);           if (curm.size() == s - cnt) break;         }       }       if (curm.size() > 1) {         mema.push_back(curm);         int lstv = a[curm.back()];         for (int i = curm.size() - 1; i >= 1; i--) {           a[curm[i]] = a[curm[i - 1]];         }         a[curm[0]] = lstv;       }     }     for (int i = 1; i <= n; i++) {       if (a[i] != b[i]) {         adj[a[i]].push_back(make_pair(b[i], i));       }     }     for (int i = 1; i <= len; i++) {       if (adj[i].size() != 0) {         curm.clear();         dfs(i);         mema.push_back(curm);       }     }     cout << mema.size() << endl;     for (int i = 0; i < mema.size(); i++) {       cout << mema[i].size() << endl;       for (int j = 0; j < mema[i].size(); j++) {         printf( %d  , mema[i][j]);       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2005;   struct poke {     double p, q;   } srt[N];   inline bool cmp1(poke a, poke b) {     return a.p > b.p || a.p == b.p && a.q > b.q;   }   inline bool cmp2(poke a, poke b) { return a.p - a.p * a.q > b.p - b.p * b.q; }   double f[N];   int n, a, b;   double ans, sum;   multiset<double> heap;   void calc() {     ans = 0., sort(srt + 1, srt + 1 + n, cmp1);     for (int i = a; i <= n; ++i) {       if (i - a > b) break;       sort(srt + 1, srt + 1 + i, cmp2), heap.clear();       double cur = sum = f[i + 1] = 0.;       for (int j = i; j >= 1; --j) {         heap.insert(srt[j].q - srt[j].p), sum += srt[j].q - srt[j].p;         if ((int)heap.size() > i - a)           sum -= *heap.begin(), heap.erase(heap.begin());         f[j] = sum, cur += srt[j].p;       }       heap.clear();       for (int j = i + 1; j <= n; ++j) {         heap.insert(srt[j].q), cur += srt[j].q;         if ((int)heap.size() > b - (i - a))           cur -= *heap.begin(), heap.erase(heap.begin());       }       for (int j = 0; j <= i; ++j) {         if (j > a) break;         if (j)           heap.insert(srt[j].q - srt[j].p * srt[j].q),               cur += srt[j].q - srt[j].p * srt[j].q;         if ((int)heap.size() > b - (i - a))           cur -= *heap.begin(), heap.erase(heap.begin());         if ((int)heap.size() == b - (i - a)) ans = max(ans, cur + f[j + 1]);       }     }   }   int main() {     scanf( %d%d%d , &n, &a, &b);     for (int i = 1; i <= n; ++i) scanf( %lf , &srt[i].p);     for (int i = 1; i <= n; ++i) scanf( %lf , &srt[i].q);     calc(), printf( %.6lf n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       char c;       cin >> c;       string str;       cin >> str;       long long ctr = 0;       long long k = 0;       for (long long i = 0; i < n; i++) {         if (str[i] != c) {           ctr++;           k = i;         }       }       if (ctr == 0) {         cout << 0;       } else if (ctr == 1) {         if (k == n - 1) {           cout << 1 <<   n ;           cout << k;         } else {           cout << 1 <<   n ;           cout << k + 2;         }       } else {         long long flag = 0;         for (long long i = n - 1; i >= n / 2; i--) {           if (str[i] == c) {             flag = i;             break;           }         }         if (flag) {           cout << 1 <<   n ;           cout << flag + 1;         } else {           cout << 2 <<   n ;           cout << n <<     << n - 1;         }       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[105], b[105];   long long tinh(long long x, int n, int nc) {     long long i, cnt = 0;     for ((i) = (1); (i) <= (n); (i)++) {       cnt += (((long long)a[i] * x) / b[i]) + 1;       if (cnt > nc) return 1001001001;     }     return cnt;   }   void input() {     int i, n, nc;     int dau, giua, cuoi;     int l, r;     scanf( %d %d , &n, &nc);     for ((i) = (1); (i) <= (n); (i)++) scanf( %d %d , &a[i], &b[i]);     dau = 1, cuoi = 1001001001;     while (dau <= cuoi) {       giua = (dau + cuoi) >> 1;       long long val = tinh(giua, n, nc);       if (val > nc)         cuoi = giua - 1;       else         dau = giua + 1;     }     r = cuoi;     dau = 1, cuoi = 1001001001;     while (dau <= cuoi) {       giua = (dau + cuoi) >> 1;       long long val = tinh(giua, n, nc);       if (val < nc)         dau = giua + 1;       else         cuoi = giua - 1;     }     l = dau;     if (r < l)       printf( 0 n );     else if (r == 1001001001)       printf( -1 n );     else       printf( %d n , r - l + 1);   }   int main() {     input();     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:1000000000 )   using namespace std;   const bool db = false;   int n, m, x, y;   int a[1010][1010], b[1010][1010];   long long s[1010][1010], cs[1010][1010];   vector<pair<int, int> > ans;   char mark[1010][1010];   pair<long long, pair<int, int> > st[1010 * 1010];   int sz = 0;   long long sum(int lx, int ly, int rx, int ry) {     return s[rx][ry] + s[lx - 1][ly - 1] - s[rx][ly - 1] - s[lx - 1][ry];   }   int main() {     scanf( %d%d%d%d , &n, &m, &x, &y);     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         scanf( %d , &a[i][j]);         s[i][j] = a[i][j] + s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];       }     }     for (int i = 1; i <= n; ++i) {       deque<int> dq;       for (int j = 1; j < y; ++j) {         while (!dq.empty() && dq.back() > a[i][j]) dq.pop_back();         dq.push_back(a[i][j]);       }       for (int j = 1; j + y - 1 <= m; ++j) {         if (j != 1) {           if (a[i][j - 1] == dq.front()) dq.pop_front();         }         while (!dq.empty() && dq.back() > a[i][j + y - 1]) dq.pop_back();         dq.push_back(a[i][j + y - 1]);         b[i][j] = dq.front();       }     }     for (int i = 1; i + y - 1 <= m; ++i) {       deque<int> dq;       for (int j = 1; j < x; ++j) {         while (!dq.empty() && dq.back() > b[j][i]) dq.pop_back();         dq.push_back(b[j][i]);       }       for (int j = 1; j + x - 1 <= n; ++j) {         if (j != 1) {           if (b[j - 1][i] == dq.front()) dq.pop_front();         }         while (!dq.empty() && dq.back() > b[j + x - 1][i]) dq.pop_back();         dq.push_back(b[j + x - 1][i]);         cs[j][i] = dq.front();       }     }     long long tot = 1LL << 60;     for (int i = 1; i + x - 1 <= n; ++i) {       for (int j = 1; j + y - 1 <= m; ++j) {         st[sz].first = cs[i][j] =             sum(i, j, i + x - 1, j + y - 1) - 1LL * x * y * cs[i][j];         st[sz].second.first = i;         st[sz++].second.second = j;       }     }     sort(st, st + sz);     ans.reserve(n * m);     for (int it = 0; it < sz; ++it) {       int i = st[it].second.first, j = st[it].second.second;       if (mark[i][j] || mark[i + x - 1][j] || mark[i][j + y - 1] ||           mark[i + x - 1][j + y - 1]) {         continue;       }       ans.emplace_back(make_pair(i, j));       for (int di = 0; di < x; ++di) {         for (int dj = 0; dj < y; ++dj) {           mark[i + di][j + dj] = 1;         }       }     }     cout << ans.size() <<   n ;     for (int i = 0; i < ans.size(); ++i)       printf( %d %d %lld n , ans[i].first, ans[i].second,              cs[ans[i].first][ans[i].second]);     getchar();     getchar();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void _p(T a) {     cout << a <<   n ;   }   template <class T>   void _pa(T a) {     cout << a <<    ;   }   template <class T1, class T2>   void _p(T1 a, T2 b) {     cout << a <<     << b <<   n ;   }   template <class T1, class T2, class T3>   void _p(T1 a, T2 b, T3 c) {     cout << a <<     << b <<     << c <<   n ;   }   template <class T1, class T2, class T3, class T4>   void _p(T1 a, T2 b, T3 c, T4 d) {     cout << a <<     << b <<     << c <<     << d <<     <<   n ;   }   template <class T1, class T2, class T3, class T4, class T5>   void _p(T1 a, T2 b, T3 c, T4 d, T5 e) {     cout << a <<     << b <<     << c <<     << d <<     << e <<     <<   n ;   }   template <class T>   void _pa(T a[], long long n) {     long long i;     for (i = 0; i < n; i++) cout << a[i] <<    ;     cout <<   n ;   }   template <class T>   void _p(vector<T> v) {     for (auto x : v) cout << x <<    ;     cout <<   n ;   }   template <class T>   void _p(list<T> l) {     for (auto x : l) cout << x <<    ;     cout <<   n ;   }   template <class T>   void _p(set<T> s) {     for (auto x : s) cout << x <<    ;     cout <<   n ;   }   template <class T>   void _p(multiset<T> s) {     for (auto x : s) cout << x <<    ;     cout <<   n ;   }   template <class T>   void _p(unordered_set<T> s) {     for (auto x : s) cout << x <<    ;     cout <<   n ;   }   template <class T1, class T2>   void _p(map<T1, T2> m) {     for (auto x : m) cout << x.first <<  ->  << x.second <<   n ;     cout <<   n ;   }   template <class T1, class T2>   void _p(unordered_map<T1, T2> m) {     for (auto x : m) cout << x.first <<  ->  << x.second <<   n ;     cout <<   n ;   }   template <class T1, class T2>   void _p(map<T1, vector<T2>> m) {     for (auto x : m) {       cout << x.first <<  -> ;       _p(x.second);     }     cout <<   n ;   }   template <class T1, class T2>   void _p(unordered_map<T1, vector<T2>> m) {     for (auto x : m) {       cout << x.first <<  -> ;       _p(x.second);     }     cout <<   n ;   }   void solution() {     string a, b;     int i, n, c = 0;     cin >> a >> b;     map<char, int> mp;     n = b.length();     for (i = 0; i < 26; i++) mp[a[i]] = i;     for (i = 1; i < n; i++) c += abs(mp[b[i]] - mp[b[i - 1]]);     _p(c);   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int tcse;     cin >> tcse;     while (tcse--) solution();   }


#include <bits/stdc++.h>   using namespace std;   bool pd[5005][5005];   char str[5005];   int n;   bool vis[5050];   bool dfs(int u, int lst) {     vis[u] = 1;     for (int i = 1; i <= n; ++i) {       if (!pd[u][i]) continue;       if (lst != -1 && pd[i][lst]) {         printf( %d %d %d n , lst, u, i);         return 1;       }       if (!vis[i] && dfs(i, u)) return 1;     }     return 0;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %s , str + 1);       for (int j = 1; j <= n; ++j)         if (str[j] ==  1 ) pd[i][j] = 1;     }     for (int i = 1; i <= n; ++i)       if (!vis[i] && dfs(i, -1)) {         return 0;       }     printf( -1 n );     return 0;   }


#include <bits/stdc++.h>   int main() {     int i, n, m, count = 0;     scanf( %d%d , &n, &m);     while (n > 0 && m > 0) {       if (n > m && n >= 2) {         n -= 2;         m--;         count++;       } else if (m > n && m >= 2) {         m -= 2;         n--;         count++;       } else if (n == m && m >= 2) {         m -= 2;         n--;         count++;       } else         break;     }     printf( %d , count);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long n, m, k;     cin >> n >> m >> k;     long long s = 2 * n * m;     if (s % k != 0) {       cout <<  NO ;       return 0;     }     s /= k;     long long x2, x3, y2, y3;     x2 = n;     if (s % n == 0)       y3 = s / n;     else       y3 = s / n + 1;     long long r = y3 * n - s;     y2 = 1, x3 = r;     cout <<  YES n ;     cout <<  0 0 n ;     cout << x2 <<     << y2 <<   n ;     cout << x3 <<     << y3 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void prepare() {}   void solve() {     string s;     cin >> s;     set<int> ss[26];     for (int i = 0; i < s.size(); ++i) ss[s[i] -  a ].insert(i);     int i = 25;     int cur = -1;     while (i != -1) {       if (ss[i].empty()) {         --i;         continue;       }       while (!ss[i].empty() && *ss[i].begin() < cur) ss[i].erase(ss[i].begin());       if (ss[i].empty()) {         --i;         continue;       }       for (int j = 0; j < ss[i].size(); ++j) printf( %c , i +  a );       cur = *(--ss[i].end());       --i;     }   }   int main() {     prepare();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int M = 200005;   const int N = 900005;   struct node {     int son[27];     int fa;   } a[N];   int sz;   char s[N];   int mat[M];   vector<int> over[N];   inline int new_id() {     ++sz;     over[sz].clear();     for (int i = (1); i <= (26); i++) a[sz].son[i] = -1;     return sz;   }   int np, ans;   void ins(int pos, int x, int y) {     over[pos].push_back(np);     if (x == y) return;     int cur = s[x] -  a  + 1;     if (a[pos].son[cur] == -1) {       a[pos].son[cur] = new_id();       a[a[pos].son[cur]].fa = pos;     }     ins(a[pos].son[cur], ++x, y);     return;   }   int u[M], n;   void dfs(int x, int dep) {     for (int i = (1); i <= (26); i++)       if (a[x].son[i] != -1) dfs(a[x].son[i], dep + 1);     vector<int> g1, g2;     g1.clear();     g2.clear();     for (int i = 0; i < over[x].size(); i++) {       int j = over[x][i];       if (u[j]) continue;       if (j <= n)         g1.push_back(j);       else         g2.push_back(j);     }     for (int i = 0; i < min(g1.size(), g2.size()); i++) {       ans += dep;       u[g1[i]] = u[g2[i]] = 1;       mat[g1[i]] = g2[i] - n;     }   }   int main() {     scanf( %d , &n);     sz = -1;     new_id();     for (int i = (1); i <= (n * 2); i++) {       np = i;       scanf( %s , &s);       int len = strlen(s);       ins(0, 0, len);     }     dfs(0, 0);     printf( %d n , ans);     for (int i = (1); i <= (n); i++) printf( %d %d n , i, mat[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     double s, x, y, z;     cin >> s >> x >> y >> z;     if ((x == y) && (y == z) && (z == 0.0)) {       cout <<  0 0   << s << endl;       return 0;     }     double k = x + y + z, x0 = x * s / k, y0 = y * s / k, z0 = z * s / k;     if (x0 + y0 + z0 >= s) z0 -= (x0 + y0 + z0 - s);     cout << setprecision(14) << x0 <<     << y0 <<     << z0 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   double xx[10], yy[10];   int main() {     double px, py, vx, vy, a, b, c, d;     cin >> px >> py >> vx >> vy >> a >> b >> c >> d;     double x = vx / sqrt(vx * vx + vy * vy);     double y = vy / sqrt(vx * vx + vy * vy);     xx[1] = b * x + px;     yy[1] = b * y + py;     xx[2] = px - (y * a / 2);     yy[2] = py + (x * a / 2);     xx[3] = px - (y * c / 2);     yy[3] = py + (x * c / 2);     xx[4] = xx[3] - (d * x);     yy[4] = yy[3] - (d * y);     xx[6] = px + (y * c / 2);     yy[6] = py - (x * c / 2);     xx[5] = xx[6] - (x * d);     yy[5] = yy[6] - (d * y);     xx[7] = px + (y * a / 2);     yy[7] = py - (x * a / 2);     cout.precision(20);     for (int i = 1; i <= 7; i++) cout << xx[i] <<     << yy[i] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int b, l, k = 0, p, m;     cin >> b >> l;     int n[l];     for (int i = 0; i < l; i++) {       n[i] = 0;     }     for (int i = 0; i < b; i++) {       cin >> p;       for (int j = 0; j < p; j++) {         cin >> m;         n[m - 1]++;       }     }     for (int i = 0; i < l; i++) {       if (n[i] == 0) {         k++;       }     }     if (k > 0) {       cout <<  NO ;     } else {       cout <<  YES ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long int pwr(long long int base, long long int p, long long int mod) {     long long int ans = 1;     while (p) {       if (p & 1) ans = (ans * base) % mod;       base = (base * base) % mod;       p /= 2;     }     return ans;   }   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   int main() {     long long int k, a, b;     scanf( %lld , &k);     scanf( %lld , &a);     scanf( %lld , &b);     long long int ans = (a / k) + (b / k);     if ((a < k && b % k != 0) || (b < k && a % k != 0)) {       printf( -1 n );       return 0;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = 1ll << 18;   long long a, b, top, now;   queue<long long> q;   map<long long, int> mp;   long long rev(long long x) {     long long y = 0;     while (x != 0) {       y = (y << 1) | (x & 1);       x >>= 1;     }     return y;   }   void push(long long &x) {     if (x > b && rev(x) > b) return;     if (mp.find(x) == mp.end()) {       q.push(x);       mp[x] = 1;     }   }   string solve() {     if (a == b) return  YES ;     q.push(a);     mp[a] = 1;     while (!q.empty()) {       top = q.front();       q.pop();       now = rev(top << 1);       if (now == b) return  YES ;       push(now);       now = rev((top << 1) | 1);       if (now == b) return  YES ;       push(now);     }     return  NO ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> a >> b;     cout << solve() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T1, class T2>   ostream& operator<<(ostream& os, const pair<T1, T2>& v) {     os <<  (  << v.first <<     << v.second <<  ) ;     return os;   }   template <typename T, typename U = typename T::value_type,             typename = typename enable_if<!is_same<T, string>::value>::type>   ostream& operator<<(ostream& os, const T& v) {     os <<  [ ;     for (const auto& it : v) os << it <<    ;     if (!v.empty()) os <<   b ;     os <<  ] ;     return os;   }   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(0);     int n, x;     cin >> n;     int cnt[6] = {0, 0, 0, 0, 0, 0};     for (int i = 0; i < (n); i++) {       cin >> x;       if (x == 4) {         cnt[0]++;         continue;       }       if (x == 8 && cnt[0]) {         cnt[0]--;         cnt[1]++;         continue;       }       if (x == 15 && cnt[1]) {         cnt[1]--;         cnt[2]++;         continue;       }       if (x == 16 && cnt[2]) {         cnt[2]--;         cnt[3]++;         continue;       }       if (x == 23 && cnt[3]) {         cnt[3]--;         cnt[4]++;         continue;       }       if (x == 42 && cnt[4]) {         cnt[4]--;         cnt[5]++;         continue;       }     }     cout << n - 6 * cnt[5] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct par {     int x, y, label;     par(int _x, int _y, int _label) {       x = _x;       y = _y;       label = _label;     }     par() {}   };   bool operator<(par p1, par p2) {     if (p1.x != p2.x) return p1.x < p2.x;     return p1.y < p2.y;   }   int main() {     int n;     while (cin >> n) {       vector<par> V;       int x, y;       for (int i = 0; i < n; i++) {         cin >> x >> y;         V.push_back(par(x, y, i + 1));       }       sort((V).begin(), (V).end());       vector<int> sol;       for (int i = 0; i < n; i++) {         bool ok = 1;         vector<par> aux;         for (int j = 0; j < n; j++)           if (j != i) aux.push_back(V[j]);         for (int j = 1; j < aux.size(); j++) {           if (aux[j - 1].y > aux[j].x) {             ok = 0;             break;           }         }         if (ok) sol.push_back(V[i].label);       }       sort((sol).begin(), (sol).end());       cout << sol.size() << endl;       for (int i = 0; i < sol.size(); i++) {         if (i == 0)           printf( %d , sol[i]);         else           printf(  %d , sol[i]);       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   inline int read() {     int w = 1, s = 0;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) w = -1;       ch = getchar();     }     while (isdigit(ch)) {       s = s * 10 + ch -  0 ;       ch = getchar();     }     return w * s;   }   int n, k, fac[1001000], Ifac[1001000], f[1000010], Sum[1010010];   inline long long ksm(long long x, long long y) {     long long res = 1;     while (y) {       if (y & 1) res = res * x % mod;       x = x * x % mod;       y >>= 1;     }     return res;   }   inline int add(int a, int b) {     a += b;     return a >= mod ? a - mod : a;   }   inline int A(int a, int b) { return 1ll * fac[a] * Ifac[a - b] % mod; }   inline void Init() {     fac[0] = Ifac[0] = 1;     for (register int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % mod;     Ifac[n] = ksm(fac[n], mod - 2);     for (register int i = n - 1; i; --i)       Ifac[i] = 1ll * Ifac[i + 1] * (i + 1) % mod;   }   int main() {     n = read(), k = read();     Init();     for (register int i = k + 1; i <= n; ++i) {       int tmp = 1ll * max(i - k - 1, 0) * fac[i - 2] % mod;       f[i] = add(f[i], tmp);       int l = max(i - k, 1);       f[i] =           add(f[i], 1ll * fac[i - 2] * add(Sum[i - 1], mod - Sum[l - 1]) % mod);       Sum[i] = add(Sum[i - 1], 1ll * f[i] * Ifac[i - 1] % mod);     }     cout << 1ll * Sum[n] * fac[n - 1] % mod;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 4e5 + 10;   int n, m, k, t;   bool vis[MAXN], ans[MAXN];   int v[MAXN];   set<int> p[MAXN];   priority_queue<pair<int, int> > pq;   int main() {     cin >> n >> m;     memset(vis, false, sizeof(vis));     memset(ans, false, sizeof(ans));     for (int i = 0; i <= 2 * m + 2; i++) v[i] = -1;     for (int i = 1; i <= n; i++) {       cin >> k;       for (int j = 0; j < k; j++) {         cin >> t;         v[t + m] = i;         p[i].insert(t + m);       }       pq.push({-p[i].size(), i});     }     bool flag = true;     while (!pq.empty()) {       int top = pq.top().second;       pq.pop();       if (vis[top]) continue;       if (p[top].empty()) {         flag = false;         break;       }       int x = *p[top].begin();       vis[top] = true;       ans[x] = true;       int y = m + m - x;       if (v[y] != -1 && !vis[v[y]]) {         p[v[y]].erase(y);         pq.push({-p[v[y]].size(), v[y]});       }     }     if (flag) {       cout <<  YES  << endl;       for (int i = m + 1; i < m + 1 + m; i++) {         if (!ans[i])           cout << 0;         else           cout << 1;       }       cout << endl;     } else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int gi() {     int w = 0, q = 0;     char c = getchar();     while ((c <  0  || c >  9 ) && (c !=  - )) c = getchar();     if (c ==  - ) q = 1, c = getchar();     while (c >=  0  && c <=  9 ) w = w * 10 + c -  0 , c = getchar();     return q ? -w : w;   }   int n, k;   long long a[500000], d[500000];   int main() {     n = gi(), k = gi();     long long ans = 0, sum = 0;     for (int i = 1; i <= n; ++i) a[i] = gi();     for (int i = 1; i <= n; ++i) d[i] = gi();     for (int i = n; i; --i) {       sum += 1ll * a[i];       if ((i == 1) && (k == 1)) continue;       ans = max(ans, sum - d[i]);     }     if (!k) printf( %I64d n , ans), exit(0);     sum -= d[1];     for (int i = 2; i < n; ++i)       ans = max(ans, sum - a[i] + max(0ll, a[i] - d[i]));     long long mn = 5000000, mn1 = 5000000;     sum = 0;     for (int i = 1; i < n; ++i) {       sum += 1ll * a[i];       if (d[i] < mn)         mn1 = mn, mn = d[i];       else if (d[i] == mn)         mn1 = d[i];       else         mn1 = min(mn1, d[i]);     }     ans = max(ans, sum + 1ll * (max(0ll, a[n] - d[n]) - mn));     if (k == 1)       ans = max(ans, sum + 1ll * (a[n] - mn - mn1));     else       ans = max(ans, sum + 1ll * (a[n] - mn));     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> b[5001];     for (int i = 0; i < n; i++) {       string s;       int a, b1, c;       cin >> s >> a >> b1;       if (s[0] ==  M )         c = 0;       else         c = 1;       b[i].push_back(c);       b[i].push_back(a);       b[i].push_back(b1);     }     int ans = 0;     for (int i = 1; i <= 366; i++) {       int m[2] = {};       for (int j = 0; j < n; j++) {         if (b[j][1] <= i && b[j][2] >= i) m[b[j][0]]++;       }       ans = max(ans, 2 * min(m[0], m[1]));     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int n, m, ar[1000005], sum, t, ar2[1000005];   vector<int> ans;   vector<pair<int, char> > res;   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(false);     cin >> n;     for (long long int i = 1; i <= n; i++) cin >> ar[i];     cin >> m;     for (long long int i = 1; i <= m; i++) cin >> ar2[i];     long long int top = 0;     long long int l = 1;     for (long long int i = 1; i <= n + 1; i++) {       if (l > m) return cout <<  NO , 0;       if (top < ar2[l]) {         top += ar[i];         continue;       }       if (top > ar2[l]) return cout <<  NO , 0;       ans.push_back(i - 1);       l++;       top = ar[i];     }     if (top || l <= m) return cout <<  NO , 0;     cout <<   n ;     int last = 1;     long long int cnt = 0;     for (int h = 0; h < ans.size(); h++) {       long long int y = ans[h];       long long int mk = 0, mn = INT_MAX;       for (int i = last; i <= y; i++) {         mk = max(mk, ar[i]);         mn = min(mn, ar[i]);       }       if (last == y) {         last++;         cnt = y - h - 1;         continue;       }       if (mn == mk) {         return cout <<  NO , 0;       }       long long int mki = 0;       char mki2;       long long int z = y - last + 1;       for (long long int i = last; i <= y; i++) {         if (ar[i] != mk) continue;         if (i != last && ar[i - 1] != ar[i]) {           mki = i;           mki2 =  L ;           res.push_back({mki - cnt, mki2});           mki--;           break;         }         if (i != y && ar[i] != ar[i + 1]) {           mki = i;           mki2 =  R ;           res.push_back({mki - cnt, mki2});           break;         }       }       z--;       z--;       while (mki > last) {         res.push_back({mki - cnt,  L });         mki--;         z--;       }       while (z--) {         res.push_back({mki - cnt,  R });       }       cnt = y - h - 1;       last = y + 1;     }     cout <<  YES           <<   n ;     for (int i = 0; i < res.size(); i++)       cout << res[i].first <<     << res[i].second <<   n ;   }


#include <bits/stdc++.h>   const int N = 100010;   const long long INF = 1ll << 60;   const int inf = 0x3f3f3f3f;   int t;   int n, m, k;   int a[N];   int b[N];   std::vector<int> row[N];   std::vector<int> col[N];   int cur;   int r, c;   long long ans;   int main() {     scanf( %d%d%d , &n, &m, &k);     for (int i = 1; i <= k; ++i) {       scanf( %d%d , &a[i], &b[i]);       row[a[i]].push_back(b[i]);       col[b[i]].push_back(a[i]);     }     for (int i = 1; i <= n; ++i) {       row[i].push_back(m + 1);       row[i].push_back(0);       std::sort(row[i].begin(), row[i].end());     }     for (int j = 1; j <= m; ++j) {       col[j].push_back(n + 1);       col[j].push_back(0);       std::sort(col[j].begin(), col[j].end());     }     r = 1;     c = 1;     ans = 1ll;     int r_max = inf;     int c_max = inf;     int r_min = 2;     int c_min = -inf;     for (int i = 1; i <= 2 * (n + m) + 1; ++i) {       if (!cur) {         auto x = std::lower_bound(row[r].begin(), row[r].end(), c);         int c_nxt = (*x) - 1;         c_nxt = std::min(c_nxt, c_max);         assert(c_nxt >= c);         ans += c_nxt - c;         if (c == c_nxt && i != 1) {           break;         }         c = c_nxt;         c_max = c - 1;       } else if (cur == 1) {         auto x = std::lower_bound(col[c].begin(), col[c].end(), r);         int r_nxt = (*x) - 1;         r_nxt = std::min(r_nxt, r_max);         assert(r_nxt >= r);         ans += r_nxt - r;         if (r == r_nxt && i != 1) {           break;         }         r = r_nxt;         r_max = r - 1;       } else if (cur == 2) {         auto x = std::lower_bound(row[r].begin(), row[r].end(), c);         --x;         int c_nxt = (*x) + 1;         c_nxt = std::max(c_nxt, c_min);         assert(c_nxt <= c);         ans += c - c_nxt;         if (c == c_nxt && i != 1) {           break;         }         c = c_nxt;         c_min = c + 1;       } else if (cur == 3) {         auto x = std::lower_bound(col[c].begin(), col[c].end(), r);         --x;         int r_nxt = (*x) + 1;         r_nxt = std::max(r_nxt, r_min);         assert(r_nxt <= r);         ans += r - r_nxt;         if (r == r_nxt && i != 1) {           break;         }         r = r_nxt;         r_min = r + 1;       }       cur = (cur + 1) % 4;     }     if (ans + k != 1ll * n * m) {       printf( NO n );     } else {       printf( YES n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     cout << n / 2 + 1 <<   n ;   }   int main() {     int tc, cse = 0;     cin >> tc;     while (tc--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 7;   int ans[maxn];   int lowbit(int x) { return x & -x; }   bool is_two(int x) { return x == lowbit(x); }   bool dfs(int n, int k, int id) {     if (k == 1) {       printf( %d %d %d n , n, k, id);       if (is_two(n)) {         return false;       }       ans[id] = max(id - 2, 0);       for (int i = 2; i <= n; ++i) {         ans[id + i - 1] = id + i / 2 - 1;       }       return true;     } else if (k == 0) {       if (is_two(n)) {         ans[1] = 0;         for (int i = 2; i <= n; ++i) {           ans[i] = i / 2;         }         return true;       } else         return false;     } else {       while (k >= 2) {         ans[id] = max(id - 2, 0);         ans[id + 1] = id;         n -= 2;         k -= 1;         id += 2;       }       return dfs(n, k, id);     }   }   int main() {     int n, k;     scanf( %d%d , &n, &k);     int lim = max(0, (n - 3) / 2);     if (n % 2 == 0 || k > lim || (n == 9 && k == 2) ||         (!is_two(n + 1) && k == 0) || (is_two(n + 1) && k == 1)) {       printf( NO n );     } else {       int id = 1;       while (k >= 2) {         ans[id] = max(id - 2, 0);         ans[id + 1] = id;         n -= 2;         k -= 1;         id += 2;       }       ans[id] = max(id - 2, 0);       for (int i = 2; i <= n - 2; ++i) {         ans[id + i - 1] = id + i / 2 - 1;       }       if (is_two(n + 1)) {         if (k == 0)           ans[id + n - 2] = id + (n - 1) / 2 - 1,                        ans[id + n - 1] = id + n / 2 - 1;         else           ans[id + n - 2] = 2, ans[id + n - 1] = 2;       } else {         if (k == 0)           return printf( NO n ), 0;         else           ans[id + n - 2] = id + (n - 1) / 2 - 1,                        ans[id + n - 1] = id + n / 2 - 1;       }       printf( YES n );       for (int i = 1; i < id + n - 1; ++i) {         printf( %d  , ans[i]);       }       printf( %d n , ans[id + n - 1]);     }   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e6 + 1, inf = 1e18;   vector<pair<long long, long long> > a, b;   vector<pair<pair<long long, long long>, long long> > z;   long long add[4 * N], t[4 * N];   void push(long long v, long long l, long long r) {     t[v] += add[v];     if (l != r - 1) {       add[2 * v] += add[v];       add[2 * v + 1] += add[v];     }     add[v] = 0;   }   void update(long long v, long long l, long long r, long long vl, long long vr,               long long val) {     push(v, l, r);     if (l >= vr || vl >= r) return;     if (l >= vl && r <= vr) {       add[v] += val;       push(v, l, r);       return;     }     long long m = (l + r) / 2;     update(2 * v, l, m, vl, vr, val);     update(2 * v + 1, m, r, vl, vr, val);     t[v] = max(t[2 * v + 1], t[2 * v]);   }   long long get(long long v, long long l, long long r, long long vl,                 long long vr) {     push(v, l, r);     if (l >= vr || vl >= r) return -inf;     if (l >= vl && r <= vr) {       return t[v];     }     long long m = (l + r) / 2;     return max(get(2 * v, l, m, vl, vr), get(2 * v + 1, m, r, vl, vr));   }   void fuckyou(long long v, long long l, long long r, long long pos,                long long val) {     if (l > pos || r <= pos) return;     if (l == r - 1) {       t[v] = max(t[v], val);       return;     }     long long m = (l + r) / 2;     fuckyou(2 * v, l, m, pos, val);     fuckyou(2 * v + 1, m, r, pos, val);     t[v] = max(t[2 * v], t[2 * v + 1]);   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     for (long long i = 0; i < 4 * N; i++) t[i] = -inf;     long long n, m, p, ans = -inf;     cin >> n >> m >> p;     a.resize(n);     b.resize(m);     z.resize(p);     for (long long i = 0; i < n; i++) {       cin >> a[i].first >> a[i].second;     }     for (long long i = 0; i < m; i++) {       cin >> b[i].first >> b[i].second;       fuckyou(1, 0, N, b[i].first, -b[i].second);     }     for (long long i = 0; i < p; i++) {       cin >> z[i].first.first >> z[i].first.second >> z[i].second;     }     sort((z).begin(), (z).end());     sort((a).begin(), (a).end());     cout << endl;     long long j = 0;     for (long long i = 0; i < n; i++) {       while (j < z.size() && a[i].first > z[j].first.first) {         update(1, 0, N, z[j].first.second + 1, N, z[j].second);         j++;       }       ans = max(ans, -a[i].second + get(1, 0, N, 0, N));     }     cout << ans;   }