#include <bits/stdc++.h>   using namespace std;   const int maxn = 2097152;   const double pi = acos(-1);   using cplx = complex<double>;   const cplx I(0, 1);   cplx omega[maxn + 1];   int a[maxn], cnt[maxn];   void prefft() {     for (int i = 0; i <= maxn; ++i) omega[i] = exp(2 * pi * i / maxn * I);   }   void bin(vector<cplx> &a, int n) {     vector<cplx> tmp(n);     int lg = 0;     while ((1 << lg) < n) ++lg;     --lg;     for (int i = 0; i < n; ++i) {       int to = 0;       for (int j = 0; (1 << j) < n; ++j) to |= (((i >> j) & 1) << (lg - j));       tmp[to] = a[i];     }     for (int i = 0; i < n; ++i) a[i] = tmp[i];   }   void fft(vector<cplx> &a, int n) {     bin(a, n);     for (int step = 2; step <= n; step <<= 1) {       int to = step >> 1;       for (int i = 0; i < n; i += step) {         for (int k = 0; k < to; ++k) {           cplx right = omega[maxn / step * k] * a[i + to + k];           a[i + to + k] = a[i + k] - right;           a[i + k] += right;         }       }     }   }   void ifft(vector<cplx> &a, int n) {     fft(a, n);     reverse(a.begin() + 1, a.end());     for (int i = 0; i < n; ++i) a[i] /= n;   }   int main() {     prefft();     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m;     cin >> n >> m;     for (int i = 0; i < n; ++i) {       cin >> a[i];       ++cnt[a[i]];     }     ++m;     int d = 1;     while (d < m) d <<= 1;     d <<= 1;     vector<cplx> poly(d, 0);     for (int i = 0; i < m; ++i)       if (cnt[i] > 0) poly[i] = cplx(1, 0);     fft(poly, d);     for (int i = 0; i < d; ++i) poly[i] *= poly[i];     ifft(poly, d);     for (int i = 1; i < m; ++i) {       int c = round(poly[i].real());       if (c <= 0) continue;       if (cnt[i] == 0) return cout <<  NO  << endl, 0;       cnt[i] = 0;     }     vector<int> ans;     for (int i = 1; i < m; ++i)       if (cnt[i]) ans.push_back(i);     cout <<  YES  << endl;     cout << ans.size() << endl;     for (int i : ans) cout << i <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using pii = pair<int, int>;   const int N = 2e5 + 10;   const int M = 300;   int a[N];   int b[N];   int typ[N];   int flag[N];   int root[N];   int tmp[N];   int q[N];   int qn;   int find_root(int u) {     if (u == root[u]) {       return u;     }     root[u] = find_root(root[u]);     return root[u];   }   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d , typ + i);       scanf( %d , a + i);       scanf( %d , b + i);     }     int t = 0;     set<pii> st;     set<pii> cr;     int last = 0;     while (t < m) {       for (int i = 1; i <= n; i++) {         flag[i] = 0;         root[i] = i;       }       for (int i = t; i < min(m, t + M); i++) {         if (a[i] > b[i]) {           swap(a[i], b[i]);         }         flag[a[i]] = 1;         flag[b[i]] = 1;         flag[a[i] + 1] = 1;         flag[b[i] < n ? b[i] + 1 : 1] = 1;       }       qn = 0;       for (int i = 1; i <= n; i++) {         if (flag[i] == 1) {           q[qn++] = i;         }       }       int u, v, uu, vv;       cr.clear();       for (auto r : st) {         u = r.first;         v = r.second;         if (flag[u] && flag[v]) {           cr.insert(pii(u, v));           continue;         }         u = find_root(u);         v = find_root(v);         root[u] = v;       }       for (int i = 0; i < qn; i++) {         root[q[i]] = find_root(q[i]);       }       sort(q, q + qn, [&](int i, int j) { return root[i] < root[j]; });       for (int i = 0, j; i < qn; i = j) {         j = i;         while (j < qn && root[q[j]] == root[q[i]]) {           tmp[q[j]] = q[i];           j++;         }       }       for (auto r : cr) {         uu = find_root(r.first);         vv = find_root(r.second);         root[uu] = vv;       }       for (int i = t; i < min(m, t + M); i++) {         u = a[i];         v = b[i];         if (last) {           u++;           v < n ? v++ : v = 1;         }         if (u > v) {           swap(u, v);         }         if (typ[i] == 1) {           if (cr.count(pii(u, v))) {             cr.erase(pii(u, v));             st.erase(pii(u, v));             for (int i = 0; i < qn; i++) {               root[q[i]] = tmp[q[i]];             }             for (auto r : cr) {               uu = find_root(r.first);               vv = find_root(r.second);               root[uu] = vv;             }           } else {             cr.insert(pii(u, v));             st.insert(pii(u, v));             u = find_root(u);             v = find_root(v);             root[u] = v;           }         } else {           u = find_root(u);           v = find_root(v);           if (u == v) {             putchar( 1 );             last = 1;           } else {             putchar( 0 );             last = 0;           }         }       }       t += M;     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, j, t;     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> t;     while (t--) {       int b, k;       cin >> n >> b >> k;       int arr[n];       for (i = 0; i < n; i++) cin >> arr[i];       int flag = 1;       for (i = 0; i < n - 1; i++) {         if (arr[i] >= arr[i + 1]) {           b += arr[i] - arr[i + 1] + min(k, arr[i + 1]);         } else if (arr[i + 1] > arr[i] + k) {           b -= arr[i + 1] - arr[i] - k;           if (b < 0) {             flag = 0;             break;           }         } else {           b += min(arr[i], arr[i] + k - arr[i + 1]);         }       }       if (flag)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   const double PI = acos(-1.);   const double eps = 1e-9;   char s[1000005];   char t[1000005];   int fs[11];   int ft[11];   int fx[11];   char y[1000005];   int main() {     int i, j, k, _T;     while (~scanf( %s%s , s, t)) {       if (strcmp(s,  01 ) == 0 || strcmp(s,  10 ) == 0) {         printf( 0 n );         continue;       }       memset(fs, 0, sizeof fs);       memset(ft, 0, sizeof ft);       for (i = 0; s[i]; i++) fs[s[i] -  0 ]++;       for (i = 0; t[i]; i++) ft[t[i] -  0 ]++;       int ls = strlen(s);       int lt = strlen(t);       for (int len = lt; len <= ls; len++) {         char x[15];         memset(fx, 0, sizeof fx);         memset(x, 0, sizeof x);         sprintf(x,  %d , len);         int lx = strlen(x);         for (i = 0; x[i]; i++) fx[x[i] -  0 ]++;         int fail = 0;         for (i = 0; i <= 9; i++)           if (fs[i] < ft[i] + fx[i]) {             fail = 1;           }         if (fail || ls - lx != len) continue;         int flag = 0;         for (i = 1; i <= 9; i++) flag |= fs[i] > fx[i] + ft[i];         if (t[0] ==  0  && !flag) continue;         if (!flag) {           printf( %s , t);           for (i = 0; i <= 9; i++)             for (j = 0; j < fs[i] - fx[i] - ft[i]; j++) putchar( 0  + i);           putchar(  n );           break;         }         int id = 0, fi = -1;         for (i = 1; i <= 9; i++)           if (fs[i] > fx[i] + ft[i]) {             fi = i;             break;           }         fs[fi]--;         y[id++] =  0  + fi;         for (i = 0; i <= 9; i++) {           if (t[0] ==  0  + i) {             int big = 0;             for (j = 1; j < lt; j++) {               if (t[j] != t[0]) {                 if (t[j] > t[0]) big = 1;                 break;               }             }             if (!big) {               for (j = 0; j < lt; j++) y[id++] = t[j];             }             for (j = 0; j < fs[i] - fx[i] - ft[i]; j++) y[id++] =  0  + i;             if (big) {               for (j = 0; j < lt; j++) y[id++] = t[j];             }           } else             for (j = 0; j < fs[i] - fx[i] - ft[i]; j++) y[id++] =  0  + i;         }         int ly = id;         fs[fi]++;         id = lt;         for (i = 0; i <= 9; i++)           for (j = 0; j < fs[i] - fx[i] - ft[i]; j++) t[id++] =  0  + i;         int uset = 0;         for (i = 0; i < ly; i++) {           if (y[i] != t[i]) {             if (y[i] > t[i]) uset = 1;             break;           }         }         if (uset && t[0] !=  0 ) {           for (i = 0; i < ly; i++) putchar(t[i]);           putchar(  n );         } else {           for (i = 0; i < ly; i++) putchar(y[i]);           putchar(  n );         }         break;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 5, nax = 1e6 + 5;   char rodz[25];   int id[25];   int res[2 * nax];   int cob(int a) {     if (a) return (a % 2 ? 1 : 0) + cob(a / 2);     return 0;   }   int pro() {     int n;     cin >> n;     vector<int> w;     for (int _ = 1, _n = (n); _ <= _n; ++_) {       int a;       cin >> a;       w.push_back(a);     }     sort(w.begin(), w.end());     reverse(w.begin(), w.end());     cin >> n;     for (int i = 0, _n = (n); i < _n; ++i) cin >> rodz[i] >> id[i];     for (int m = 0, _n = (1 << n); m < _n; ++m)       if (m) {         int jed = cob(m), zer = n - jed;         if (id[zer] == 1) {           res[m] = -inf;           if (rodz[zer] ==  p ) {             for (int i = 0, _n = (n); i < _n; ++i)               if (m & (1 << i)) res[m] = max(res[m], (res[m ^ (1 << i)] + w[i]));           } else             for (int i = 0, _n = (n); i < _n; ++i)               if (m & (1 << i)) res[m] = max(res[m], (res[m ^ (1 << i)]));         } else {           res[m] = inf;           if (rodz[zer] ==  p ) {             for (int i = 0, _n = (n); i < _n; ++i)               if (m & (1 << i)) res[m] = min(res[m], (res[m ^ (1 << i)] - w[i]));           } else             for (int i = 0, _n = (n); i < _n; ++i)               if (m & (1 << i)) res[m] = min(res[m], (res[m ^ (1 << i)]));         }       }     cout << res[(1 << n) - 1];     return 0;   }   int main() {     ios_base::sync_with_stdio(0);     int z = 1;     for (int _ = 1, _n = (z); _ <= _n; ++_) pro();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     long long x;     char c;     long long pr = -1, pb = -1;     long long lr = -1, lb = -1, lp = -1;     long long ans = 0;     long long mr = -1, mb = -1;     for (int i = 0; i < n; i++) {       scanf( %lld %c , &x, &c);       x += (long long)1e10;       if (c ==  P ) {         if (lp == -1) {           if (pr != -1) ans += x - pr;           if (pb != -1) ans += x - pb;         } else {           if (lb != -1) mb = max(mb, x - lb);           if (lr != -1) mr = max(mr, x - lr);           long long dif = x - lp;           if (lr == -1 && lb != -1) {             ans += 2 * dif - mb;           } else if (lr != -1 && lb == -1) {             ans += 2 * dif - mr;           } else if (lr != -1 && lb != -1) {             ans += min(3 * dif - mr - mb, 2 * dif);           } else {             ans += dif;           }         }         mb = mr = lb = lr = -1;         lp = x;       } else if (c ==  B ) {         if (pb == -1) pb = x;         if (mb == -1)           mb = x - lp;         else           mb = max(mb, x - lb);         lb = x;       } else {         if (pr == -1) pr = x;         if (mr == -1)           mr = x - lp;         else           mr = max(mr, x - lr);         lr = x;       }     }     if (lp == -1) {       ans = lr - pr + lb - pb;     } else {       if (lb != -1) ans += lb - lp;       if (lr != -1) ans += lr - lp;     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1000000007;   const long long int maxn = 500005;   const long long int mm = 1e+18;   int main() {     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       int a[n], p[m];       for (int i = 0; i < n; i++) cin >> a[i];       for (int i = 0; i < m; i++) cin >> p[i];       bool temp = true;       int count = 0;       while (temp) {         count = 0;         for (int i = 0; i < m; i++) {           int x = p[i], y;           y = x - 1;           if (a[y] > a[x]) {             int str = a[x];             a[x] = a[y];             a[y] = str;             count++;           }         }         if (count == 0) temp = false;       }       count = 0;       for (int i = 0; i < n - 1; i++) {         if (a[i] > a[i + 1]) {           count++;           break;         }       }       if (count > 0) {         cout <<  NO  << endl;       } else         cout <<  YES  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) {       return a;     }     return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return ((a * b) / gcd(a, b)); }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ;     long long n, m;     cin >> n >> m;     long long ip[n + 10][m + 10];     long long dp1[n + 10][m + 10];     long long dp2[n + 10][m + 10];     long long dp3[n + 10][m + 10];     long long dp4[n + 10][m + 10];     long long answer = 0;     for (long long i = 0; i < n + 10; i++) {       for (long long j = 0; j < m + 10; j++) {         dp1[i][j] = 0;         dp2[i][j] = 0;         dp3[i][j] = 0;         dp4[i][j] = 0;       }     }     for (long long i = 1; i < n + 1; i++) {       for (long long j = 1; j < m + 1; j++) {         cin >> ip[i][j];       }     }     for (long long i = 1; i < n + 1; i++) {       for (long long j = 1; j < m + 1; j++) {         dp1[i][j] = max(dp1[i - 1][j], dp1[i][j - 1]) + ip[i][j];       }     }     for (long long i = 1; i < n + 1; i++) {       for (long long j = m; j >= 1; j--) {         dp2[i][j] = max(dp2[i - 1][j], dp2[i][j + 1]) + ip[i][j];       }     }     for (long long i = n; i >= 1; i--) {       for (long long j = 1; j < m + 1; j++) {         dp3[i][j] = max(dp3[i + 1][j], dp3[i][j - 1]) + ip[i][j];       }     }     for (long long i = n; i >= 1; i--) {       for (long long j = m; j >= 1; j--) {         dp4[i][j] = max(dp4[i + 1][j], dp4[i][j + 1]) + ip[i][j];       }     }     for (long long i = 1; i < n + 1; i++) {       for (long long j = 1; j < m + 1; j++) {         if (i == 1 || i == n || j == 1 || j == m) {           continue;         }         answer = max(answer, dp1[i - 1][j] + dp4[i + 1][j] + dp3[i][j - 1] +                                  dp2[i][j + 1]);         answer = max(answer, dp1[i][j - 1] + dp4[i][j + 1] + dp3[i + 1][j] +                                  dp2[i - 1][j]);       }     }     cout << answer;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> pp[10];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int i, j, k, l, m, n, even1 = 0, odd1 = 0, count = 0, t, even2 = 0, odd2 = 0,                           even = 0, odd = 0;     cin >> t;     int a[t];     for (i = 0; i < t; i++) {       cin >> a[i];       if (i % 2 == 0) {         odd1 = odd1 + a[i];       } else {         even1 = even1 + a[i];       }     }     for (i = 0; i < t; i++) {       odd = even1 + odd2 - even2;       even = odd1 + even2 - odd2;       if (i % 2 == 0) {         odd2 = odd2 + a[i];         even = even - a[i];       } else {         even2 = even2 + a[i];         odd = odd - a[i];       }       if (even == odd) count++;     }     cout << count << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     int arr[n], brr[m], check[n], indexer = 0;     for (int i = 0; i < n; i++) cin >> arr[i], check[i] = -2;     for (int i = 0; i < m; i++) {       cin >> brr[i];       for (int j = 0; j < n; j++)         if (brr[i] == arr[j]) {           check[indexer] = j;           indexer++;         }     }     sort(check, check + indexer);     for (int j = 0; j < indexer; j++) {       cout << arr[check[j]] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct __TS {     ~__TS() {       cerr <<  Time elapsed:   << clock() * 1.0 / CLOCKS_PER_SEC << endl;     }   } __ts;   const int mod = 1e9 + 7;   const int maxn = 1239;   long long tp[maxn];   int main() {     tp[0] = 1;     for (int i = (int)(1); i < (int)(maxn); i++) {       tp[i] = (2 * tp[i - 1]) % mod;     }     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     string x;     cin >> x;     int n = x.size();     long long ans = 0;     for (int i = 0; i < (int)(n); i++) {       if (x[i] ==  0 ) continue;       ans += tp[2 * (n - 1 - i) + i];       ans %= mod;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f, maxn = 1000001;   long long t[maxn];   void init() {     for (int i = 0; i <= 1000000; i++) t[i] = (long long)i * i * i;   }   int main() {     init();     int n, a, b;     scanf( %d , &n);     while (n--) {       scanf( %d%d , &a, &b);       long long c = (long long)a * b;       int pos = lower_bound(t, t + maxn, c) - t;       if ((long long)pos * pos * pos == c && a % pos == 0 && b % pos == 0)         printf( Yes n );       else         printf( No n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int cc[100002];   pair<int, int> in[100002];   vector<pair<int, int> > adj[100002];   int pos = 1;   int l[100002], r[100002], label[100002];   long long d[100002];   void dfs(int node, int par, long long depth = 0) {     l[node] = pos;     d[node] = depth;     int ch, w;     for (pair<int, int> edge : adj[node]) {       if (edge.first == par) {         continue;       }       dfs(edge.first, node, depth + edge.second);     }     label[pos] = node;     r[node] = pos++;   }   long long pen[100002];   void update(int ind, long long add) {     while (ind < 100002) {       pen[ind] += add;       ind += ind & (-ind);     }   }   long long query(int ind) {     long long ret = 0;     while (ind > 0) {       ret = ret + pen[ind];       ind = ind & (ind - 1);     }     return ret;   }   pair<long long, int> seg[100002 * 4];   long long lazy[100002 * 4];   const long long inf = -4e18;   void init(int pos, int l, int r) {     lazy[pos] = 0;     if (l == r) {       seg[pos] = {d[label[l]], l};       return;     }     int mid = (l + r) / 2;     init(pos * 2, l, mid);     init(pos * 2 + 1, mid + 1, r);     seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]);   }   void push(int pos, int l, int r) {     if (lazy[pos] != 0) {       seg[pos].first += lazy[pos];       if (l != r) {         lazy[pos * 2] += lazy[pos];         lazy[pos * 2 + 1] += lazy[pos];       }       lazy[pos] = 0;     }   }   void update(int pos, int l, int r, int ql, int qr, long long val) {     push(pos, l, r);     if (r < ql || qr < l) {       return;     }     if (ql <= l && r <= qr) {       lazy[pos] = val;       push(pos, l, r);       return;     }     int mid = (l + r) / 2;     update(pos * 2, l, mid, ql, qr, val);     update(pos * 2 + 1, mid + 1, r, ql, qr, val);     seg[pos] = max(seg[pos * 2], seg[pos * 2 + 1]);   }   pair<long long, int> query(int pos, int l, int r, int ql, int qr) {     push(pos, l, r);     if (r < ql || qr < l) {       return {inf, 0};     }     if (ql <= l && r <= qr) {       return seg[pos];     }     int mid = (l + r) / 2;     return max(query(pos * 2, l, mid, ql, qr),                query(pos * 2 + 1, mid + 1, r, ql, qr));   }   long long w[100002];   long long dfs2(int node, int par) {     priority_queue<long long> pq;     for (pair<int, int> edge : adj[node]) {       if (edge.first == par) {         continue;       }       pq.push(dfs2(edge.first, node));     }     long long ret = w[node];     if (node != 1) {       if (pq.size() > 0) pq.pop();     }     if (pq.size() > 0) {       ret = max(ret, pq.top());     }     return ret;   }   void solve() {     int n;     long long T;     scanf( %d %lld  , &n, &T);     long long rb = 0;     for (int i = 1; i <= n; ++i) {       scanf( %d   , &cc[i]);       rb += cc[i];     }     for (int i = 1; i <= n; ++i) {       scanf( %d  , &in[i].first);       in[i].second = i;     }     sort(in + 1, in + n + 1);     for (int i = 2; i <= n; ++i) {       int p, l;       scanf( %d %d  , &p, &l);       l *= 2;       adj[p].push_back({i, l});     }     dfs(1, 0);     init(1, 1, n);     for (int i = 1; i <= n; ++i) {       int node, co, p, x, y;       node = in[i].second;       co = cc[node];       p = r[node];       x = l[node], y = p;       long long val = (long long)co * in[i].first;       while (true) {         pair<long long, int> q = query(1, 1, n, x, y);         if (q.first + val >= T) {           long long remCC = min((T - q.first) / in[i].first, (long long)co);           w[label[q.second]] = query(q.second) + remCC;           update(1, 1, n, q.second, q.second, inf);         } else {           break;         }       }       update(1, 1, n, x, y, val);       update(x, co);       update(y + 1, -co);     }     while (true) {       pair<long long, int> q = query(1, 1, n, 1, n);       if (q.first < 0) {         break;       }       w[label[q.second]] = query(q.second);       update(1, 1, n, q.second, q.second, inf);     }     printf( %lld n , dfs2(1, 0));   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   int pre[N], n, m;   int f(int x) { return pre[x] == x ? x : pre[x] = f(pre[x]); }   set<int> s[N];   map<int, vector<int> > v[N];   void join(int x, int y) {     x = f(x);     y = f(y);     if (x == y) return;     if (s[x].size() < s[y].size()) swap(x, y);     set<int>::iterator it = s[y].begin();     for (; it != s[y].end(); ++it) s[x].insert(*it);     pre[y] = x;   }   void add() {     int x, y, z;     scanf( %d %d %d , &x, &y, &z);     s[f(x)].insert(y);     s[f(y)].insert(x);     v[x][z].push_back(y);     join(y, v[x][z][0]);     v[y][z].push_back(x);     join(x, v[y][z][0]);   }   void query() {     int x, y;     scanf( %d %d , &x, &y);     if (f(x) == f(y) || s[f(x)].count(y))       printf( Yes n );     else       printf( No n );   }   signed main() {     char opt[10];     int c, q;     scanf( %d %d %d %d , &n, &m, &c, &q);     for (register int i = (1); i <= (n); ++i) pre[i] = i;     for (register int i = (1); i <= (m); ++i) add();     for (register int i = (1); i <= (q); ++i) {       scanf( %s , opt);       if (opt[0] ==  + )         add();       else         query();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     long long n;     cin >> n;     long long i;     if (n == 1)       cout << 1;     else {       long long sum = 1;       for (i = 1;; i++) {         sum += (1ll << i);         if (sum >= n) break;       }       cout << i + 1;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   constexpr long long R = 1e3 + 69, INF = 2e9 + 6969, mod = 998244353;   long long n, a[R], dp[R], fact[R], rev[R];   long long choose(long long N, long long K) {     long long cur = (fact[N] * rev[K]) % mod;     cur = (cur * rev[N - K]) % mod;     return cur;   }   long long Pow(long long a, long long p) {     long long w = 1;     while (p) {       if (p & 1) w = (w * a) % mod;       a = (a * a) % mod;       p >>= 1;     }     return w;   }   int main() {     scanf( %lld , &n);     for (int i = 0; i < n; ++i) scanf( %lld , &a[i]);     fact[0] = 1;     for (int i = 1; i <= n; ++i) fact[i] = (i * fact[i - 1]) % mod;     rev[n] = Pow(fact[n], mod - 2);     for (int i = n - 1; i >= 0; --i) rev[i] = (rev[i + 1] * (i + 1)) % mod;     dp[n] = 1;     for (int i = n - 1; i >= 0; --i) {       if (a[i] <= 0 || a[i] + i >= n) continue;       for (int j = i + a[i] + 1; j <= n; ++j) {         dp[i] += (dp[j] * choose(j - i - 1, a[i])) % mod;         dp[i] %= mod;       }     }     long long sum = 0;     for (int i = 0; i < n; ++i) sum = (sum + dp[i]) % mod;     printf( %lld n , sum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, h, a, b, k;     cin >> n >> h >> a >> b >> k;     for (long long int i = 0; i < k; i++) {       long long int t1, f1, t2, f2;       cin >> t1 >> f1 >> t2 >> f2;       long long int ans;       if (t1 == t2) {         cout << abs(f1 - f2) << endl;         continue;       }       if (a <= f1 && f1 <= b) {         cout << abs(t1 - t2) + abs(f1 - f2) << endl;         continue;       }       long long int temp;       if (abs(f1 - a) <= abs(f1 - b)) {         temp = a;         ans = abs(f1 - a);       } else {         temp = b;         ans = abs(f1 - b);       }       ans = ans + abs(t1 - t2) + abs(temp - f2);       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using vi = vector<int>;   using vvi = vector<vi>;   using pii = pair<int, int>;   constexpr auto MOD = 1000000007LL;   constexpr auto LINF = (1LL << 60);   constexpr auto INF = 2147483647LL;   constexpr auto PI = 3.1415926535897932384626433;   constexpr auto EPS = 1E-9;   template <typename T1, typename T2>   ostream &operator<<(ostream &out, const pair<T1, T2> p) {     out << p.first <<     << p.second;     return out;   }   template <typename T1, typename T2>   istream &operator>>(istream &in, pair<T1, T2> &p) {     in >> p.first >> p.second;     return in;   }   template <typename T>   istream &operator>>(istream &in, vector<T> &v) {     for (auto &x : v) in >> x;     return in;   }   template <typename T>   ostream &operator<<(ostream &out, vector<T> v) {     for (int i = 0; i < v.size(); i++)       out << v[i] << (i + 1 == v.size() ?   n  :    );     out.flush();     return out;   }   struct Node {     int l, r;     int emp;     ll end;     Node() {}   } T[1000010 << 2];   void maintain(int id) {     ll ext = max(0LL, T[id << 1].end - T[id << 1 | 1].l);     T[id].emp = T[id << 1].emp + max(0LL, T[id << 1 | 1].emp - ext) +                 max(0LL, T[id << 1 | 1].l - T[id << 1].end);     T[id].end = max(0LL, ext - T[id << 1 | 1].emp) + T[id << 1 | 1].end;     return;   }   void build(int id = 1, int l = 0, int r = 1e6) {     T[id].l = l;     T[id].r = r;     T[id].emp = 0;     T[id].end = l;     if (l + 1 == r) {       return;     }     build(id << 1, l, l + r >> 1);     build(id << 1 | 1, l + r >> 1, r);     return;   }   void add(int t, int d, int id = 1, int l = 0, int r = 1e6) {     if (t >= r || l > t) return;     if (l + 1 == r) {       T[id].emp = 0;       T[id].end = t + d;       return;     }     add(t, d, id << 1, l, l + r >> 1);     add(t, d, id << 1 | 1, l + r >> 1, r);     maintain(id);     return;   }   void rem(int t, int id = 1, int l = 0, int r = 1e6) {     if (t >= r || l > t) return;     if (l + 1 == r) {       T[id].emp = 0;       T[id].end = l;       return;     }     rem(t, id << 1, l, l + r >> 1);     rem(t, id << 1 | 1, l + r >> 1, r);     maintain(id);     return;   }   void ask(int t, ll &res, int id = 1, int l = 0, int r = 1e6) {     if (l > t) return;     if (t + 1 >= r) {       ll ext = max(0LL, res - T[id].l);       ext = max(0LL, ext - T[id].emp);       res = T[id].end + ext;       return;     }     ask(t, res, id << 1, l, l + r >> 1);     ask(t, res, id << 1 | 1, l + r >> 1, r);     return;   }   int records[300010];   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     build();     string op;     for (int i = 1; i <= n; i++) {       cin >> op;       if (op ==  ? ) {         int t;         ll res = -1;         cin >> t;         --t;         ask(t, res);         cout << max(0LL, res - t) << endl;       } else if (op ==  + ) {         int d;         cin >> records[i] >> d;         --records[i];         add(records[i], d);       } else if (op ==  - ) {         int id;         cin >> id;         rem(records[id]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1000010, mod = 1e9 + 7;   int i, j, k, n, m, y, e;   long long ans = 1, a[maxn];   int main() {     a[0] = 1;     a[1] = 1;     cin >> n;     for (i = 2; i <= n; i++) a[i] = (a[i - 2] * (i - 1) + a[i - 1]) % mod;     for (i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       if (x == 1)         y++;       else         e++;     }     for (i = n; e; i--, e--) {       ans *= i;       ans %= mod;     }     cout << ans * a[y] % mod << endl;   }


#include <bits/stdc++.h>   using namespace std;   int quick_pow(int a, int b, int MOD) {     a %= MOD;     int res = 1;     while (b) {       if (b & 1) res = (res * a) % MOD;       b /= 2;       a = (a * a) % MOD;     }     return res;   }   const int maxn = 200000 + 10;   const int MOD = 1e9 + 7;   const int INF = 1e9;   int a[maxn], dp[maxn];   priority_queue<int, vector<int>, greater<int> > que1, que2[maxn];   typedef struct Segment {     int sum[maxn * 4];     void pushUp(int k) { sum[k] = min(sum[k * 2], sum[k * 2 + 1]); }     void build(int k, int l, int r) {       sum[k] = INF;       if (l == r) return;       int m = (l + r) / 2;       build(k * 2, l, m);       build(k * 2 + 1, m + 1, r);     }     void update(int p, int v, int k, int l, int r) {       if (l == r) {         sum[k] = min(sum[k], v);         return;       }       int m = (l + r) / 2;       if (p <= m)         update(p, v, k * 2, l, m);       else         update(p, v, k * 2 + 1, m + 1, r);       pushUp(k);     }     int ask(int a, int b, int k, int l, int r) {       if (a <= l && r <= b) return sum[k];       int m = (l + r) / 2, res = INF;       if (a <= m) res = min(res, ask(a, b, k * 2, l, m));       if (b > m) res = min(res, ask(a, b, k * 2 + 1, m + 1, r));       return res;     }   } Tree;   Tree tree;   int main() {     int i, j, n, m;     scanf( %d , &n);     for (i = 1; i <= n; i++) {       scanf( %d , &a[i]);     }     tree.build(1, 1, n);     printf( 0 );     int now = 0;     tree.update(a[1], a[1] + 1, 1, 1, n);     now = -1;     for (i = 2; i <= n; i++) {       int res = now + i;       res = min(res, tree.ask(i, n, 1, 1, n) - i);       printf(  %d , res);       tree.update(a[i], res + 1 + a[i], 1, 1, n);       now = min(now, res - i);     }     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pai = acos(-1);   const double eps = 1e-8;   const long long mod = 1e9 + 7;   const int MXN = 1e6 + 5;   vector<int> v[MXN];   int c[MXN];   int suf[MXN];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       int le;       scanf( %d , &le);       while (le--) {         int aa;         scanf( %d , &aa);         v[i].push_back(aa);       }       int mi = INT_MAX;       for (auto j : v[i]) {         if (j > mi) c[i] = 1;         mi = min(mi, j);       }       sort(v[i].begin(), v[i].end());     }     long long gg = 0;     for (int i = 1; i <= n; i++) {       if (c[i]) {         gg++;         continue;       }       suf[v[i].back()]++;     }     for (int i = 1e6 - 1; i >= 0; i--) suf[i] = suf[i] + suf[i + 1];     long long ans = 0;     for (int i = 1; i <= n; i++) {       if (c[i])         ans = ans + (long long)n;       else         ans = ans + (long long)suf[v[i][0] + 1] + gg;     }     cout << ans <<   n ;     return 0;   }


#include<bits/stdc++.h>   using namespace std;   #define ll long long      int main()   {      ll int n,q;      string s;      cin>>n>>q;      ll int cum[n+2];      cin>>s;      cum[0]=s[0]- a +1;      for(int i=1;i<n;i++)      {             cum[i]=cum[i-1]+s[i]- a +1;      }         for(ll int i=0;i<q;i++)      {   ll int a,b,ans=0;          cin>>a>>b;             if (a==1)          {cout<<cum[b-1]<<endl;}          else          {cout<<cum[b-1]-cum[a-2]<<endl;}         }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:64000000 )   using namespace std;   void __never(int a) { printf(  nOPS %d , a); }   const int INF = 1000000000;   bool was[6][26][6][11][16][21];   int dp[6][26][6][11][16][21];   struct Edge {     int a, l, h;   };   int n;   Edge graf[7][7];   int mask[7];   void dfs(int a, int b, int rest, int cost, vector<int>& v,            vector<pair<vector<int>, int> >& res) {     if (b == n + 1) {       if (rest != 0) return;       vector<int> v2(5);       for (int(i) = (a); (i) <= (n - 2); ++(i)) v2[i] = v[i] + mask[i + 1];       res.push_back(make_pair(v2, cost));       return;     }     for (int(i) = (graf[a][b].l); (i) <= (graf[a][b].h); ++(i)) {       if (i > rest) break;       mask[b] = i;       dfs(a, b + 1, rest - i, cost + ((i > 0) ? graf[a][b].a : 0) + i * i, v,           res);     }   }   int get(vector<int> v, int st) {     if (was[st][v[0]][v[1]][v[2]][v[3]][v[4]])       return dp[st][v[0]][v[1]][v[2]][v[3]][v[4]];     was[st][v[0]][v[1]][v[2]][v[3]][v[4]] = true;     int& res = dp[st][v[0]][v[1]][v[2]][v[3]][v[4]];     if (v[0] + v[1] + v[2] + v[3] + v[4] == 0 && st == n - 1) return res = 0;     vector<pair<vector<int>, int> > vv;     dfs(st + 1, st + 2, v[st], 0, v, vv);     res = -INF;     for (int(i) = (0); (i) <= ((int)((vv).size()) - 1); ++(i)) {       int h = get(vv[i].first, st + 1);       if (h > -INF) res = max(res, h + vv[i].second);     }     return res;   }   void init() {     freopen( input.txt ,  r , stdin);     freopen( output.txt ,  w , stdout);   }   void sol() {     cin >> n;     int a, b;     for (int(i) = (1); (i) <= ((n * (n - 1)) / 2); ++(i)) {       cin >> a >> b;       cin >> graf[a][b].l >> graf[a][b].h >> graf[a][b].a;     }     bool zero = true;     for (int(i) = (1); (i) <= (n); ++(i))       for (int(j) = (i + 1); (j) <= (n); ++(j)) zero &= graf[i][j].l == 0;     if (zero) {       printf( 0 0 );       return;     }     int hi = 0;     for (int(i) = (2); (i) <= (n); ++(i)) hi += graf[1][i].h;     vector<int> v(5);     for (int(I) = (1); (I) <= (hi); ++(I)) {       v[0] = I;       if (get(v, 0) > 0) {         printf( %d %d , I, get(v, 0));         return;       }     }     printf( -1 -1 );   }   void gen() {     int n = 6;     printf( %d , n);     for (int(i) = (1); (i) <= (n); ++(i))       for (int(j) = (i + 1); (j) <= (n); ++(j))         printf(  n%d %d %d %d %d , i, j, rand() & 1, 5, rand() & 15);   }   int main() {     sol();     return 0;   }


#include <bits/stdc++.h>   const int MOD = 1000000007;   const int MAX = 10000005;   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(0);     long long n;     cin >> n;     long long b, d;     long long max = -1;     long long y = 0;     vector<pair<long long, long long> > vecx, vecy;     for (long long i = 0; i < n; i++) {       cin >> b >> d;       vecx.push_back(make_pair(b, d));       vecy.push_back(make_pair(d, b));     }     sort(vecx.begin(), vecx.end());     sort(vecy.begin(), vecy.end());     long long pointer1 = 0;     long long pointer2 = 0;     long long sum = 0;     for (long long i = 0; i < 2 * vecx.size(); i++) {       if (pointer1 == vecx.size() || pointer2 == vecx.size()) {         goto ab;       }       if (vecx[pointer1].first < vecy[pointer2].first) {         sum++;         if (sum > max) {           max = sum;           y = vecx[pointer1].first;         }         pointer1++;       } else if (vecx[pointer1].first > vecy[pointer2].first) {         sum--;         pointer2++;       } else {         sum--;         pointer2++;       }     }   ab:;     cout << y <<     << max <<    ;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   const long long N = 2e5 + 10, alp = 26;   const long double pi = 3.14159265358979;   void solve() {     long long n, m;     cin >> n >> m;     vector<vector<long long> > g1(n), rg1(n), g2(n), rg2(n);     for (long long i = 0; i < m; ++i) {       long long u, v, t;       cin >> u >> v >> t;       --u;       --v;       if (t)         g1[u].push_back(v), rg1[v].push_back(u);       else         g2[u].push_back(v), rg2[v].push_back(u);     }     vector<long long> ans(n, -1), len(n, -1);     ans[n - 1] = 0;     len[n - 1] = 0;     queue<long long> q;     q.push(n - 1);     while (q.size()) {       long long u = q.front();       q.pop();       set<long long> s1, s2;       for (long long to : rg1[u])         if (ans[to] == 1 && len[to] == -1) len[to] = len[u] + 1, q.push(to);       for (long long to : rg2[u])         if (ans[to] == 2 && len[to] == -1) len[to] = len[u] + 1, q.push(to);       for (long long to : rg1[u])         if (ans[to] == -1) s1.insert(to);       for (long long to : rg2[u])         if (ans[to] == -1) s2.insert(to);       for (long long x : s1)         if (!s2.count(x)) ans[x] = 2;       for (long long x : s2)         if (!s1.count(x)) ans[x] = 1;       for (long long x : s1)         if (s2.count(x) && len[x] == -1)           ans[x] = 1, len[x] = len[u] + 1, q.push(x);     }     cout << len[0] <<   n ;     for (long long i = 0; i < n; ++i) cout << (ans[i] == 1 ? 1 : 0);   }   int32_t main() {     ios_base::sync_with_stdio(NULL);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   const int maxn = 212345;   int n, a[maxn], f[maxn], pre[maxn];   vector<int> b;   int F(int x) { return x == f[x] ? x : f[x] = F(f[x]); }   inline void U(int a, int b) { f[min(F(a), F(b))] = max(F(a), F(b)); }   inline void go(int l, int r) {     for (int i = l, j;; i = j) {       j = F(i) + 1;       if (j > r) return;       U(i, j);     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (int i = 1; i <= n; ++i) b.push_back(a[i]);     for (int i = 1; i <= n; ++i) f[i] = i;     sort(b.begin(), b.end());     b.erase(unique(b.begin(), b.end()), b.end());     for (int i = 1; i <= n; ++i)       a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();     for (int i = 1; i <= n; ++i) {       if (pre[a[i]]) go(pre[a[i]], i);       pre[a[i]] = i;     }     long long res = 1, cnt = 0;     for (int i = 1; i <= n; ++i)       if (F(i) == i) {         if (cnt) res = (res + res) % mod;         ++cnt;       }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, y, a[(int)1e5 + 4];   unsigned long long x, sum;   void error() {     puts( -1 );     exit(0);   }   int main() {     scanf( %d%I64d%d , &n, &x, &y);     sum = (unsigned long long)n;     if (n > y || (n == y && sum < x)) error();     for (int i = 0; i < n; i++) a[i] = 1;     a[n - 1] += (y - n);     if (sum - 1LL + (unsigned long long)a[n - 1] * (unsigned long long)a[n - 1] <         x)       error();     for (int i = 0; i < n; i++) printf( %d n , a[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long counter;   long long n, r, l, x;   vector<long long> a;   long long sum, rr;   bool cheake(long long mask) {     long long nmax = -1;     long long nmin = 10000000000;     long long i = 0;     rr = 0;     sum = 0;     while ((mask >> i) != 0) {       if ((mask >> i) % 2 == 1) {         rr++;         sum = sum + a[i];         nmax = max(a[i], nmax);         nmin = min(a[i], nmin);       }       i++;     }     if ((rr >= 2) && (sum >= l) && (sum <= r) && (nmax - nmin) >= x)       return true;     else       return false;   }   void solv(long long i, long long mask) {     if (i == n) return;     solv(i + 1, mask);     long long nmask = mask | (1 << i);     solv(i + 1, nmask);     if (cheake(nmask)) counter++;   }   int main() {     long long k;     cin >> n >> l >> r >> x;     for (long long i = 0; i < n; i++) {       cin >> k;       a.push_back(k);     }     solv(0, 0);     cout << counter;   }


#include<iostream>   #include<cstdio>   #include<cstring>   #include<cmath>   #include<algorithm>   #include<vector>   #include<stack>   #include<queue>   #include<set>   #include<map>   //#include<bits/stdc++.h>   using namespace std;   typedef long long ll;   const int maxn = 1e5+5;   const int inf = 0x3f3f3f3f;   const ll INF = 1e18;   const ll mod = 998244353;      #define for1(i,n) for(int i=1;i<=n;i++)   #define forn(i,n) for(int i=0;i<n;i++)   #define m_p(x,y) make_pair(x,y)      inline int read(){//ll??       int x=0,f=1;char ch=getchar();       while(ch< 0 ||ch> 9 ){if(ch== - )f=-1;ch=getchar();}       while(ch>= 0 &&ch<= 9 ){x=x*10+ch- 0 ;ch=getchar();}       return x*f;   }      int main(){    int t=read(); while(t--){     int x=read(), y=read();     if(abs(x-y)<=1) cout << x+y << endl;     else cout << x+y+abs(x-y)-1 << endl;    }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, p, s;   vector<int> v1, v2;   pair<int, pair<int, int>> arr[3005];   bool cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {     if (a.second.first == b.second.first)       return a.second.second < b.second.second;     return a.second.first > b.second.first;   }   int dp[3005][3005];   int solve(int x, int taken) {     if (x == n) {       if (taken == p) return 0;       return -1e9;     }     int &ret = dp[x][taken];     if (ret != -1) return ret;     ret = 0;     if (taken < p) {       ret = max(ret, arr[x].second.second + solve(x + 1, taken + 1));     }     int lef = min(s, x - taken);     if (lef < s) {       ret = max(ret, arr[x].second.first + solve(x + 1, taken));     }     ret = max(ret, solve(x + 1, taken));     return ret;   }   void trace(int x, int taken) {     if (x == n) return;     int &ret = dp[x][taken];     if (taken < p && ret == arr[x].second.second + solve(x + 1, taken + 1)) {       v2.push_back(arr[x].first);       trace(x + 1, taken + 1);       return;     }     int lef = min(s, x - taken);     if (lef < s) {       if (ret == arr[x].second.first + solve(x + 1, taken)) {         v1.push_back(arr[x].first);         trace(x + 1, taken);         return;       }     }     trace(x + 1, taken);   }   int main() {     cin >> n >> p >> s;     swap(p, s);     for (int i = 0; i < n; i++) {       cin >> arr[i].second.first;       arr[i].first = i + 1;     }     for (int i = 0; i < n; i++) {       cin >> arr[i].second.second;     }     sort(arr, arr + n, cmp);     memset(dp, -1, sizeof dp);     cout << solve(0, 0) <<   n ;     trace(0, 0);     for (auto i : v1) cout << i <<    ;     cout <<   n ;     for (auto i : v2) cout << i <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = (int)1e5 + 100;   const int MAX = (int)1e5 + 1;   int l[maxN], d[maxN], cnt[maxN], q[maxN];   vector<int> pool[maxN];   multiset<int> p;   void solveTask() {     int n;     cin >> n;     for (int i = 1; i <= n; ++i) cin >> l[i];     for (int i = 1; i <= n; ++i) cin >> d[i];     for (int i = 1; i <= n; ++i)       q[l[i]] += d[i], cnt[l[i]] += 1, pool[l[i]].push_back(d[i]);     int answer = 0;     int all_sum = 0;     for (int i = 1; i <= MAX; ++i) {       if (cnt[i] == 0) continue;       int possibleCnt = cnt[i] - 1;       int cur_sum = 0;       for (set<int>::iterator it = p.begin(); it != p.end(); ++it) {         if (possibleCnt == 0) break;         cur_sum -= *it;         --possibleCnt;       }       answer = max(answer, cur_sum + q[i]);       all_sum += q[i];       for (int j = 0; j < pool[i].size(); ++j) {         p.insert(-pool[i][j]);       }     }     cout << all_sum - answer << endl;   }   int main() {     solveTask();     ios_base::sync_with_stdio(false);     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, x, m = 0;     char a[1000];     int s[26];     cin >> n;     while (n--) {       cin >> a;       m = 0;       int mi = 1000, mx = -1;       for (i = 0; i < 26; i++) {         s[i] = 0;       }       x = strlen(a);       for (i = 0; i < x; i++) {         s[a[i] -  a ]++;         mi = min(mi, (a[i] -  a ));         mx = max(mx, (a[i] -  a ));       }       for (i = 0; i < 26; i++) {         if (s[i] > 1) {           cout <<  No  << endl;           m = 1;           break;         }       }       for (i = mi; i <= mx; i++) {         if (s[i] != 1 && m != 1) {           cout <<  No  << endl;           m = 1;           break;         }       }       if (m == 0) cout <<  Yes  << endl;     }   }


#include <bits/stdc++.h>   #pragma diag_suppress 177   using namespace std;   const int MM = 220000;   int m, n, k, p1, p2, p3, f, a[MM], pr[MM], af[MM], tree[4 * MM];   string s, s1, s2;   int MINN = 0;   int func(int aa, int bb) { return (aa | bb); }   void build(int node, int start, int end) {     if (start == end)       tree[node] = a[start];     else {       int mid = (start + end) / 2;       build(2 * node, start, mid);       build(2 * node + 1, mid + 1, end);       tree[node] = func(tree[2 * node], tree[2 * node + 1]);     }   }   int query(int node, int start, int end, int l, int r) {     if (r < start or end < l) return MINN;     if (l <= start and end <= r) return tree[node];     int mid = (start + end) / 2;     int px1 = query(2 * node, start, mid, l, r);     int px2 = query(2 * node + 1, mid + 1, end, l, r);     return func(px1, px2);   }   bool chk(int hi, int mid, int val) {     return (query(1, 0, n - 1, mid, hi) > val);   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , a + i);     int cur;     priority_queue<pair<int, int> > pq;     vector<int> ind;     for (int i = 0; i < n; i++) {       cur = i;       while (!ind.empty() && a[ind.back()] <= a[i]) {         cur = pr[ind.back()];         ind.pop_back();       }       pr[i] = cur;       ind.push_back(i);     }     ind.clear();     for (int i = n - 1; i >= 0; i--) {       cur = i;       while (!ind.empty() && a[ind.back()] < a[i]) {         cur = af[ind.back()];         ind.pop_back();       }       af[i] = cur;       ind.push_back(i);     }     long long ans = 0;     build(1, 0, n - 1);     int ls, rs, hi, lo, mid, xx, yy;     for (int i = 0; i < n; i++) {       ls = i - pr[i] + 1;       rs = af[i] - i + 1;       hi = i;       lo = pr[i];       while (hi > lo) {         mid = (hi + lo) / 2;         if (chk(i, mid, a[i]))           lo = mid + 1;         else           hi = mid;       }       xx = i - lo + 1;       ans += (long long)ls * (long long)rs;       hi = af[i];       lo = i;       while (hi > lo) {         mid = (hi + lo) / 2;         if (chk(mid, i, a[i]))           hi = mid;         else           lo = mid + 1;       }       if (chk(lo, i, a[i])) lo--;       yy = lo - i + 1;       ans -= (long long)xx * (long long)yy;     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5;   const int INF = 1e9 + 9;   const int B = 1e9 + 7;   long long n;   int main() {     ios_base ::sync_with_stdio(0);     cin.tie(0);     cin >> n;     long long pwr = 1;     for (;;) {       long long x = (n + (pwr - 1)) / pwr;       if (n % pwr != 0) {         cout << x <<   n ;         return 0;       }       if (x == 1) {         cout << x <<   n ;         return 0;       }       if (x == 2 && pwr * 3 < n) {         cout << x <<   n ;         return 0;       }       pwr *= 3;     }     assert(0);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t;     cin >> t;     while (t--) {       int n;       long long a = 0, b;       cin >> n;       b = pow(2, n);       for (int i = 1; i < n / 2; i++) b += pow(2, i);       for (int i = n / 2; i < n; i++) a += pow(2, i);       cout << abs(b - a) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     if (a == 0) return b;     if (b == 0) return a;     if (a == b) return a;     if (a > b) return gcd(a - b, b);     return gcd(a, b - a);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     while (t--) {       int a, h, sum = 0;       cin >> a;       for (int i = 2; i < a; i++) {         int n = a;         while (n >= i) {           sum += n % i;           n = n / i;         }         sum += n;       }       h = gcd(sum, a - 2);       cout << sum / h <<  /  << (a - 2) / h << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int i, arr[n];       for (i = 0; i < n; i++) cin >> arr[i];       int ap{0}, bp{n}, asize{arr[0]}, bsize{}, moves{1}, left = n - 1;       bool a = 1;       while (left != 0) {         if (a) {           while (bsize <= asize && left != 0) {             bsize += arr[bp - 1];             bp--;             left--;           }           moves++;           asize = 0;           a = 0;         } else {           while (asize <= bsize && left != 0) {             asize += arr[ap + 1];             ap++;             left--;           }           moves++;           bsize = 0;           a = 1;         }       }       cout << moves <<    ;       asize = 0;       for (i = 0; i <= ap; i++) asize += arr[i];       cout << asize <<    ;       bsize = 0;       for (; i < n; i++) bsize += arr[i];       cout << bsize << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long int;   ll hell = 998244353;   int main() {     ll n;     cin >> n;     vector<ll> v[n];     for (ll i = 0; i < n; ++i) {       ll x;       cin >> x;       for (ll j = 0; j < x; ++j) {         ll y;         cin >> y;         v[i].push_back(y);       }       v[i].push_back(100009);       sort(v[i].begin(), v[i].end());     }     ll ans = 0;     for (ll i = 0; i < n; ++i) {       ll e = (i != 0) ? i - 1 : n - 1;       ll f = (i != n - 1) ? i + 1 : 0;       for (ll j = 1; j < v[i].size() - 1; ++j) {         ll v1 = v[i][j];         ll v2 = v[i][j - 1];         ll c1 = lower_bound(v[e].begin(), v[e].end(), v1) -                 lower_bound(v[e].begin(), v[e].end(), v2);         ll c2 = lower_bound(v[f].begin(), v[f].end(), v1) -                 lower_bound(v[f].begin(), v[f].end(), v2);         if (c1 != c2) ans++;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   void read(T &x) {     x = 0;     bool f = 0;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = 1;     for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);     if (f) x = -x;   }   template <typename F>   inline void write(F x) {     static short st[30];     short tp = 0;     if (x < 0) putchar( - ), x = -x;     do st[++tp] = x % 10, x /= 10;     while (x);     while (tp) putchar( 0  | st[tp--]);     putchar(  n );   }   template <typename T>   inline void Mx(T &x, T y) {     x < y && (x = y, 0);   }   template <typename T>   inline void Mn(T &x, T y) {     x > y && (x = y);   }   const int N = 2005;   int m, n;   double f[N][N];   int main() {     read(n), read(m);     int mx = max(n, m);     for (int i = 0; i <= mx; i++) f[i][0] = 1;     for (int i = 1; i <= mx; i++) f[0][i] = 1.0 / (i + 1);     for (int i = 2; i <= n + m; i++) {       for (int j = 1; j < i; j++) {         if (i - j < 1) continue;         int x = j, y = i - j;         double inv = 1.0 / (y + 1);         double c = inv * y * (1 - f[y - 1][x]), b = (1 - f[y][x - 1]);         double d = c + inv;         double p = (1.0 - b) / (1 - c + d - b);         f[x][y] = p * c + (1 - p);       }     }     printf( %.9lf %.9lf n , f[n][m], 1 - f[n][m]);     return 0;   }


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


#include <bits/stdc++.h>   using namespace std;   int main() {     string c =   ;     string t =   ;     cin >> t >> c;     char lt = t[0];     int nt = int(t[1]) - 48;     char lc = c[0];     int nc = int(c[1]) - 48;     int cnt = 16;     vector<pair<char, int> > torre;     vector<pair<char, int> > caballo;     for (int i = -2; i <= 2; i += 4) {       for (int j = -1; j <= 1; j += 2) {         char x = lt + i;         int y = nt + j;         char xx = lt + j;         int yy = nt + i;         if (int(x) < 105 && int(x) > 96 && y < 9 && y > 0) {           cnt++;           torre.push_back(make_pair(x, y));         }         if (int(xx) < 105 && int(xx) > 96 && yy < 9 && yy > 0) {           cnt++;           torre.push_back(make_pair(xx, yy));         }       }     }     for (int i = -2; i <= 2; i += 4) {       for (int j = -1; j <= 1; j += 2) {         char x = lc + i;         int y = nc + j;         char xx = lc + j;         int yy = nc + i;         if (int(x) < 105 && int(x) > 96 && y < 9 && y > 0 && x != lt && y != nt) {           cnt++;           caballo.push_back(make_pair(x, y));         }         if (int(xx) < 105 && int(xx) > 96 && yy < 9 && yy > 0 && xx != lt &&             yy != nt) {           cnt++;           caballo.push_back(make_pair(xx, yy));         }       }     }     for (int i = 0; i < torre.size(); i++) {       for (int j = 0; j < caballo.size(); j++) {         if (torre[i].first == caballo[j].first &&             torre[i].second == caballo[j].second)           cnt--;       }     }     cout << 64 - cnt <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int read(int f = 1, int x = 0, char ch =    ) {     while (!isdigit(ch = getchar()))       if (ch ==  - ) f = -1;     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     return f * x;   }   const int N = 1e5 + 5, oo = 1e9 + 7;   int n, m, a[N], ans[N], b[N], f[N], g[N], l[N], p[N], vis[N];   int get(int i, int x) {     int j = lower_bound(b + 1, b + 1 + m, x) - b - 1;     return vis[j] = 1, ans[i] = b[j];   }   int main() {     n = read();     for (int i = 1; i <= n; ++i) a[i] = read(), f[i] = oo;     ++n, a[n] = f[n] = oo;     m = read();     for (int i = 1; i <= m; ++i) b[i] = read();     sort(b + 1, b + 1 + m);     for (int i = 1; i <= n; ++i)       if (~a[i]) {         int j = lower_bound(f + 1, f + 1 + n, a[i]) - f - 1;         l[i] = j + 1, f[j + 1] = a[i], p[i] = g[j], g[j + 1] = i;       } else         for (int j = n, k = m; k; f[j + 1] = b[k], g[j + 1] = i, --k)           for (; f[j] >= b[k]; --j)             ;     for (int i = l[n], j = n, x = a[n]; i; --i)       if (~a[j])         ~a[p[j]] ? x = a[p[j]] : x = get(p[j], x), j = p[j];       else {         bool flg = false;         for (int k = j - 1; k && !flg; --k)           if (~a[k] && l[k] == i - 1 && a[k] < x) x = a[j = k], flg = true;         for (int k = j - 1; k && !flg; --k)           if (!~a[k]) x = get(j = k, x), flg = true;       }     for (int i = 1; i < n; ++i)       if (ans[i]) a[i] = ans[i];     for (int i = 1, j = 1; i < n; ++i)       if (!~a[i]) {         for (; vis[j]; ++j)           ;         vis[j] = 1, a[i] = b[j];       }     for (int i = 1; i < n; ++i) printf( %d  , a[i]);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int n, k;     cin >> n >> k;     string second;     cin >> second;     if (k > 2) {       int c = 0;       for (int i = 1; i < n; i++) {         if (second[i] == second[i - 1]) {           if (i + 1 < n) {             int j = 0;             while (j < k &&                    ((j +  A ) == second[i - 1] || (j +  A ) == second[i + 1]))               j++;             second[i] = j +  A ;           } else {             second[i] = ((second[i - 1] -  A  + 1) % k) +  A ;           }           c++;         }       }       cout << c <<   n ;       cout << second <<   n ;       return;     }     string t =  A , h =  B ;     for (int i = 1; i < n; i++) {       if (t[i - 1] ==  A )         t +=  B ;       else         t +=  A ;       if (h[i - 1] ==  A )         h +=  B ;       else         h +=  A ;     }     int c1 = 0, c2 = 0;     for (int i = 0; i < n; i++) {       if (second[i] != t[i]) c1++;       if (h[i] != second[i]) c2++;     }     cout << min(c1, c2) <<   n ;     if (c1 <= c2)       cout << t <<   n ;     else       cout << h <<   n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Event {     int type;     int p;     int x;   };   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     vector<int> v(n);     for (auto& el : v) {       cin >> el;     }     int q;     cin >> q;     vector<Event> evs(q);     for (auto& ev : evs) {       cin >> ev.type;       if (ev.type == 2) {         cin >> ev.x;       } else {         cin >> ev.p >> ev.x;         ev.p--;       }     }     int mx = 0;     vector<bool> has_ans(n, false);     for (int i = q - 1; i >= 0; i--) {       if (evs[i].type == 1) {         if (!has_ans[evs[i].p]) {           v[evs[i].p] = max(mx, evs[i].x);           has_ans[evs[i].p] = true;         }       } else {         mx = max(evs[i].x, mx);       }     }     for (int i = 0; i < n; i++) {       if (!has_ans[i]) {         v[i] = max(v[i], mx);       }     }     for (auto& el : v) {       cout << el <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   typedef int ll;   const ll MAX = 5e5 + 100;   const ll inf = 1e9;   const ll mod = 1e9 + 7;   bool go[MAX];   ll p[MAX], dsu[MAX], n, m;   pair<ll, ll> e[MAX];   vector<pair<ll, ll> > Querie;   ll ans[MAX];   ll vertex;   vector<ll> adj[MAX];   vector<ll> querys_on_graph[MAX];   ll find(ll u) { return (dsu[u] == u ? u : dsu[u] = find(dsu[u])); }   void uni(ll u, ll v) {     ll x = find(u), y = find(v);     if (x == y) return;     vertex++;     adj[vertex].push_back(x);     adj[vertex].push_back(y);     dsu[y] = vertex;     dsu[x] = vertex;   }   ll L[MAX], R[MAX], pos[MAX], leaf = 1, val[MAX], val_to_u[MAX];   ll tree[MAX * 2];   void dfs(ll u) {     L[u] = inf;     for (ll v : adj[u]) {       dfs(v);       L[u] = min(L[u], L[v]);       R[u] = max(R[u], R[v]);     }     if (adj[u].size() == 0) {       pos[u] = leaf++;       L[u] = R[u] = pos[u];       val[pos[u]] = p[u];     }   }   void build(ll node, ll l, ll r) {     if (l == r) {       tree[node] = val[l];       return;     }     ll mid = (l + r) >> 1;     build(node << 1, l, mid);     build(node << 1 | 1, mid + 1, r);     tree[node] = max(tree[node << 1], tree[node << 1 | 1]);   }   void update(ll node, ll l, ll r, ll x) {     if (l == r) {       tree[node] = 0;       return;     }     ll mid = (l + r) >> 1;     if (x <= mid)       update(node << 1, l, mid, x);     else       update(node << 1 | 1, mid + 1, r, x);     tree[node] = max(tree[node << 1], tree[node << 1 | 1]);   }   ll query(ll node, ll l, ll r, ll i, ll j) {     if (j < l || r < i) return 0;     if (i <= l && r <= j) {       return tree[node];     }     ll mid = (l + r) >> 1;     return max(query(node << 1, l, mid, i, j),                query(node << 1 | 1, mid + 1, r, i, j));   }   void dfs_go(ll u) {     for (ll v : adj[u]) {       dfs_go(v);     }     sort(querys_on_graph[u].begin(), querys_on_graph[u].end());     for (ll it : querys_on_graph[u]) {       ll answer = query(1, 1, n, L[u], R[u]);       ans[it] = answer;       if (answer != 0) {         update(1, 1, n, pos[val_to_u[answer]]);       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     ll q, t, u;     cin >> n >> m >> q;     for (ll i = 1; i < (ll)n + 1; i++) cin >> p[i], val_to_u[p[i]] = i;     for (ll i = 1; i < (ll)m + 1; i++) cin >> e[i].first >> e[i].second;     for (ll i = 0; i < (ll)q; i++) {       cin >> t >> u;       if (t == 2) go[u] = 1;       Querie.push_back({t, u});     }     for (ll i = 1; i < (ll)2 * n + 5; i++) dsu[i] = i;     for (ll i = 1; i < (ll)m + 1; i++)       if (!go[i]) Querie.push_back({2, i});     vertex = n;     vector<ll> ask;     reverse(Querie.begin(), Querie.end());     for (pair<ll, ll> it : Querie) {       if (it.first == 2) {         uni(e[it.second].first, e[it.second].second);       } else {         ask.push_back(find(it.second));       }     }     vertex++;     for (ll i = 1; i < (ll)vertex; i++)       if (find(i) == i) adj[vertex].push_back(find(i));     dfs(vertex);     build(1, 1, n);     reverse(ask.begin(), ask.end());     for (ll i = 0; i < (ll)ask.size(); i++) {       ll u = ask[i];       ll answer = query(1, 1, n, L[u], R[u]);       cout << answer << endl;       if (answer != 0) {         update(1, 1, n, pos[val_to_u[answer]]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     vector<int> one;     vector<int> two;     cin >> n;     for (int i = 0; i < n; i++) {       int t, w;       cin >> t >> w;       if (t == 1)         one.push_back(w);       else         two.push_back(w);     }     sort(one.begin(), one.end());     sort(two.begin(), two.end());     vector<int> pref_one = {0};     vector<int> pref_two = {0};     for (int i = 1; i <= one.size(); i++) {       int val = one[i - 1] + pref_one[i - 1];       pref_one.push_back(val);     }     for (int i = 1; i <= two.size(); i++) {       int val = two[i - 1] + pref_two[i - 1];       pref_two.push_back(val);     }     int res = 1000000;     for (int i = 0; i <= one.size(); i++) {       for (int j = 0; j <= two.size(); j++) {         bool possible = false;         int ans = i + 2 * j;         int w_one = pref_one[one.size() - i];         int w_two = pref_two[two.size() - j];         if (ans >= (w_one + w_two)) possible = true;         if (possible) res = min(res, ans);       }     }     cout << res << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long Mypow(long long X, long long Y) {     if (Y < 0) return 0;     if (Y == 0) return 1;     if (Y == 1) {       return X;     } else {       long long x = Mypow(X, Y / 2);       x *= x;       if (Y % 2)         return (x * X);       else         return x;     }   }   long long n, m, l, r, x;   long long p[100100];   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> p[i];     }     while (m--) {       scanf( %I64d %I64d %I64d , &l, &r, &x);       if (x < l || x > r) {         printf( Yes n );         continue;       }       int sum = 0;       for (int i = l; i <= r; i++) {         if (p[i] < p[x]) {           sum++;         }       }       if (x == l + sum) {         printf( Yes n );       } else {         printf( No n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007, dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};   const double eps = 1e-8;   void read(int &k) {     k = 0;     char x = getchar();     while (x <  0  || x >  9 ) x = getchar();     while (x >=  0  && x <=  9 ) {       k = k * 10 - 48 + x;       x = getchar();     }   }   int n, tra[128], lx, ly, Lx[10010], Ly[10010], a[2100][2100], ll, rr, tx, ty,       nx, ny, k;   pair<pair<int, int>, pair<int, int> > ope[1010];   pair<int, int> dd[5000000];   char s[10];   void update(int type, int &x) {     if (!type)       x = lower_bound(Lx + 1, Lx + lx + 1, x) - Lx;     else       x = lower_bound(Ly + 1, Ly + ly + 1, x) - Ly;   }   int main() {     tra[ R ] = 0;     tra[ D ] = 1;     tra[ L ] = 2;     tra[ U ] = 3;     scanf( %d , &n);     nx = 0;     ny = 0;     Lx[++lx] = 0;     Ly[++ly] = 0;     Ly[++ly] = Lx[++lx] = 1000000003;     Ly[++ly] = Lx[++lx] = -1000000003;     Lx[++lx] = 1;     Ly[++ly] = 1;     for (int i = 1; i <= n; i++) {       scanf( %s%d , s, &k);       ope[i].first = make_pair(nx, ny);       nx += dx[tra[s[0]]] * k;       ny += dy[tra[s[0]]] * k;       ope[i].second = make_pair(nx, ny);       Lx[++lx] = nx;       Ly[++ly] = ny;       Lx[++lx] = nx + 1;       Ly[++ly] = ny + 1;     }     sort(Lx + 1, Lx + lx + 1);     lx = unique(Lx + 1, Lx + lx + 1) - Lx - 1;     sort(Ly + 1, Ly + ly + 1);     ly = unique(Ly + 1, Ly + ly + 1) - Ly - 1;     for (int i = 1; i <= n; i++) {       update(0, ope[i].first.first);       update(1, ope[i].first.second);       update(0, ope[i].second.first);       update(1, ope[i].second.second);       if (ope[i].first.first == ope[i].second.first) {         if (ope[i].first.second > ope[i].second.second)           swap(ope[i].first.second, ope[i].second.second);         for (int j = ope[i].first.second; j <= ope[i].second.second; j++)           a[ope[i].first.first][j] = 1;       } else {         if (ope[i].first.first > ope[i].second.first)           swap(ope[i].first.first, ope[i].second.first);         for (int j = ope[i].first.first; j <= ope[i].second.first; j++)           a[j][ope[i].first.second] = 1;       }     }     a[1][1] = 2;     for (dd[ll = rr = 0] = make_pair(1, 1); ll <= rr; ll++) {       for (int dir = 0; dir < 4; dir++) {         tx = dd[ll].first + dx[dir];         ty = dd[ll].second + dy[dir];         if (tx > 0 && tx <= lx && ty > 0 && ty <= ly && !a[tx][ty])           a[tx][ty] = 2, dd[++rr] = make_pair(tx, ty);       }     }     long long ans = 0;     for (int i = 1; i <= lx; i++)       for (int j = 1; j <= ly; j++)         if (a[i][j] <= 1) ans += 1ll * (Lx[i + 1] - Lx[i]) * (Ly[j + 1] - Ly[j]);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, b, dist1, dist2, i, blank;     string s, t;     for (i = 0; i < 300; i++) cout <<  a ;     cout << endl;     cin >> dist1;     if (dist1 == 0) return 0;     for (i = 0; i < 300; i++) cout <<  b ;     cout << endl;     cin >> dist2;     if (dist2 == 0) return 0;     blank = dist1 + dist2 - 300;     dist1 -= blank;     dist2 -= blank;     n = dist1 + dist2;     s =   ;     for (i = 0; i < n; i++) s +=  a ;     if (dist2 == n) {       cout << s << endl;       cin >> dist2;       return 0;     }     for (i = 0; i < n - 1; i++) {       t = s;       t[i] =  b ;       cout << t << endl;       cin >> dist2;       if (dist2 == 0) return 0;       if (dist2 < dist1) s[i] =  b ;       dist1 = min(dist2, dist1);     }     s[n - 1] =  b ;     cout << s << endl;     cin >> dist2;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct tp {     int l, r;   };   int n;   vector<tp> t;   bool comp(tp a, tp b) { return a.l < b.l or a.l == b.l and a.r < b.r; }   int main() {     cout.sync_with_stdio(false);     cin >> n;     for (int i = 1; i <= n; i++) {       int l, r;       cin >> l >> r;       t.push_back({l, r});     }     sort(t.begin(), t.end(), comp);     int cl = -1;     int cr = -1;     int ans = 0;     for (int i = 0; i < t.size(); i++) {       if (t[i].l > cr) {         ans += 1;         cl = t[i].l;         cr = t[i].r;       } else if (t[i].l <= cr and t[i].r > cr) {         continue;       } else if (t[i].l <= cr and t[i].r <= cr) {         cl = t[i].l;         cr = t[i].r;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 140000;   int sx[MAX + 10], ex[MAX], sy[MAX + 10], ey[MAX];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++)       scanf( %d %d %d %d , &sx[i], &sy[i], &ex[i], &ey[i]);     vector<pair<int, int> > px, py;     for (int i = 0; i < n; i++) {       px.push_back(make_pair(sx[i], -i - 1));       px.push_back(make_pair(ex[i], i + 1));     }     sort(px.begin(), px.end());     set<int> rec;     int rx = -1, ry = -1;     for (int i = 0; i < (int)px.size(); i++) {       if (px[i].second < 0) {         rec.insert(-1 * px[i].second);       } else {         rec.erase(px[i].second);       }       if ((int)rec.size() >= n - 1) {         py.clear();         for (set<int>::iterator it = rec.begin(); it != rec.end(); it++) {           py.push_back(make_pair(sy[*it - 1], -1 * (*it)));           py.push_back(make_pair(ey[*it - 1], *it));         }         sort(py.begin(), py.end());         int inter = 0;         for (int j = 0; j < (int)py.size(); j++) {           if (py[j].second < 0)             inter++;           else             inter--;           if (inter >= n - 1) rx = px[i].first, ry = py[j].first;         }       }     }     printf( %d %d n , rx, ry);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 13 * 16, M = 1e9 + 7;   int n, m, k;   struct zz {     int a[N][N];   } A;   inline zz cf(zz x, zz y) {     zz z;     memset(z.a, 0, sizeof z.a);     for (int i = 0; i < (1 << m) * (k + 1); i++)       for (int j = 0; j < (1 << m) * (k + 1); j++)         for (int l = 0; l < (1 << m) * (k + 1); l++)           z.a[i][j] = (z.a[i][j] + (long long)x.a[i][l] * y.a[l][j]) % M;     return z;   }   inline zz ksm(zz x, int y) {     zz ans;     memset(ans.a, 0, sizeof ans.a);     for (int i = 0; i < (1 << m) * (k + 1); i++) ans.a[i][i] = 1;     for (; y; y >>= 1, x = cf(x, x))       if (y & 1) ans = cf(ans, x);     return ans;   }   int main() {     scanf( %d%d%d , &n, &k, &m);     for (int i = 0; i < 1 << m; i++)       for (int j = 0; j <= k; j++) {         int num = 1;         for (int l = 0; l < m; l++)           if ((1 << l) & i) num++;         A.a[(i >> 1) * (k + 1) + j][i * (k + 1) + j] = 1;         if (j < k)           A.a[(i >> 1 | (1 << (m - 1))) * (k + 1) + j + 1][i * (k + 1) + j] = num;       }     A = ksm(A, n);     int ans = 0;     for (int i = 0; i < 1 << m; i++) (ans += A.a[i * (k + 1) + k][0]) %= M;     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   set<int> b[127];   set<int>::iterator it, itt;   int n, m, l, r, c[200005];   char s, ch[2], a[200005];   void upd(int v, int d) {     while (v <= n) {       c[v] += d;       v += v & -v;     }   }   int find(int x) {     int cur = 0, ans = 0;     for (int i = 20; i >= 0; i--) {       ans += (1 << i);       if (ans >= n || cur + c[ans] >= x)         ans -= (1 << i);       else         cur += c[ans];     }     return ans + 1;   }   int gsum(int v) {     int sum = 0;     while (v > 0) {       sum += c[v];       v -= v & -v;     }     return sum;   }   int main() {     scanf( %d%d , &n, &m);     scanf( %s , a + 1);     for (int i = 1; i <= n; i++) b[a[i]].insert(i);     for (int i = 1; i <= n; i++) upd(i, 1);     for (int i = 0; i < m; i++) {       scanf( %d%d%s , &l, &r, ch);       s = ch[0];       l = find(l);       r = find(r);       it = b[s].lower_bound(l);       while (it != b[s].end() && *it <= r)         upd(*it, -1), a[*it] =  # , itt = it, it++, b[s].erase(itt);     }     for (int i = 1; i <= n; i++)       if (gsum(i) - gsum(i - 1) == 1) printf( %c , a[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1 << 17;   const int INF = 7 << 28;   struct T {     int maxa, minr;     T() : maxa(0), minr(INF) {}     T(int a, int b) : maxa(a), minr(b) {}     T operator+(T t) const { return T(max(maxa, t.maxa), min(minr, t.minr)); }   };   T a[MAXN * 2];   int del[MAXN * 2];   int l[MAXN], r[MAXN], t[MAXN];   void setDel(int i, int d) {     del[i] += d;     a[i].minr -= d;   }   void push(int i) {     setDel(i * 2, del[i]);     setDel(i * 2 + 1, del[i]);     del[i] = 0;   }   void com(int i) {     push(i);     a[i] = a[i * 2] + a[i * 2 + 1];   }   void clear(int i) {     int j;     for (j = 0; (1 << j) < MAXN; j++)       ;     for (; j; j--) push(i >> j);   }   T get(int i) {     clear(i);     T ret = T();     while (i) {       if (~i & 1) ret = a[i + 1] + ret;       i >>= 1;     }     return ret;   }   void modify(int i, T t) {     i += MAXN;     clear(i);     a[i] = t;     del[i] = 0;     i >>= 1;     while (i) {       com(i);       i >>= 1;     }   }   void init() { fill(a, a + MAXN * 2, T()); }   void put(int t) { printf( %d  , t); }   int main() {     int n;     int i, j, k;     int ans = 0;     init();     scanf( %d , &n);     for (i = 1; i <= n; i++) {       scanf( %d%d%d , &l[i], &r[i], &t[i]);       r[i]++;     }     for (i = 1; i <= n; i++) {       if (max(ans, l[i]) + t[i] <= r[i]) {         put(0);         modify(i, T(t[i], max(ans, l[i]) - l[i]));         ans = max(ans, l[i]) + t[i];       } else {         int ra, rr;         j = 1;         ra = 0;         rr = INF;         while (j < MAXN) {           push(j);           j = j * 2 + 1;           if (max(a[j].maxa, ra) < min(rr, a[j].minr)) {             ra = max(a[j].maxa, ra);             rr = min(a[j].minr, rr);             j--;           }         }         T temp = get(j);         int move = max(temp.maxa, min(temp.minr, a[j].maxa));         if (max(ans - move, l[i]) + t[i] > r[i] ||             max(ans - move, l[i]) + t[i] >= ans) {           put(-1);           continue;         }         move = min(move, ans - l[i]);         j = 1;         while (j < MAXN) {           j = j * 2 + 1;           if (a[j].maxa < move) j--;         }         put(j - MAXN);         temp = get(j);         move = min(temp.minr, a[j].maxa);         ans -= move;         modify(j - MAXN, T(0, INF));         while (j != 1) {           if (~j & 1) setDel(j + 1, move);           j >>= 1;           com(j);         }         modify(i, T(t[i], max(ans, l[i]) - l[i]));         ans = max(ans, l[i]) + t[i];       }     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int q;     cin >> q;     for (int i = 0; i < q; ++i) {       long long n;       cin >> n;       int a = 0;       int b = 0;       int c = 0;       while (n % 2 == 0) {         n /= 2;         ++a;       }       while (n % 3 == 0) {         n /= 3;         ++b;       }       while (n % 5 == 0) {         n /= 5;         ++c;       }       if (n != 1) {         cout <<   -1   << endl;       } else {         cout << a + b * 2 + c * 3 << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int n, cnt, Max, head[N];   struct Edge {     int to, nxt, val;   } e[N << 1];   int read() {     int x = 0, flg = 1;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) flg = -1;     for (; isdigit(c); c = getchar()) x = x * 10 + c -  0 ;     return x * flg;   }   void add(int x, int y, int w) {     e[++cnt].to = y;     e[cnt].val = w;     e[cnt].nxt = head[x];     head[x] = cnt;   }   void dfs(int x, int fa, int dis) {     Max = max(Max, dis);     for (int i = head[x]; i; i = e[i].nxt) {       int u = e[i].to;       if (u == fa) continue;       dfs(u, x, dis + e[i].val);     }   }   int main() {     n = read();     int x, y, w;     long long sum = 0;     for (int i = 2; i <= n; ++i) {       x = read(), y = read(), w = read();       add(x, y, w);       add(y, x, w);       sum += 2 * w;     }     dfs(1, 0, 0);     printf( %lld n , sum - Max);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     std::cin >> n;     std::vector<int> v(101);     for (int i = 0; i < n; ++i) {       int temp;       std::cin >> temp;       v[temp]++;     }     int mex_A = -1;     int mex_B = -1;     bool flag1 = 1;     for (int i = 0; i < n; ++i) {       if (v[i] > 1 && flag1) {         mex_A = mex_B = i;       } else if (v[i] >= 1) {         mex_A = i;         flag1 = 0;       } else if (v[i] == 0) {         break;       }     }     std::cout << ++mex_A + ++mex_B <<   n ;   }   int main() {     int x;     cin >> x;     while (x--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct compare {     bool operator()(const pair<int, int>& a, const pair<int, int>& b) {       return true;     }   };   int main() {     cin.tie(0);     ios_base::sync_with_stdio(0);     int t;     cin >> t;     while (t--) {       int n, x, y;       cin >> n >> x >> y;       vector<int> ans;       for (int i = 1; i <= 50; i++) {         ans.clear();         if ((x - y) % i != 0) continue;         for (int j = x; j <= y; j += i) ans.push_back(j);         if (ans.back() != y || ans.size() > n) continue;         int j = x - i;         while (ans.size() < n && j >= 1) {           ans.push_back(j);           j -= i;         }         j = y + i;         while (ans.size() < n) {           ans.push_back(j);           j += i;         }         break;       }       for (auto ai : ans) cout << ai <<    ;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   const int p = 257;   const long long mods = 1610612741;   const int maxn = 200010;   long long hash1[maxn][2];   int cntzero[maxn];   char s[maxn];   int N;   long long pp[maxn];   void gethash(char s[]) {     hash1[0][0] = 0;     hash1[0][1] = 0;     for (int i = 1; i <= N; i++) {       cntzero[i] = cntzero[i - 1];       if (s[i] ==  0 ) {         cntzero[i]++;         hash1[i][0] = (hash1[i - 1][0] * p + (i & 1) + 1) % mods;         hash1[i][1] = (hash1[i - 1][1] * p + ((i + 1) & 1) + 1) % mods;       } else {         hash1[i][0] = hash1[i - 1][0];         hash1[i][1] = hash1[i - 1][1];       }     }     return;   }   void init() {     pp[0] = 1;     for (int i = 1; i <= N; i++) {       pp[i] = (pp[i - 1] * p) % mods;     }   }   long long getsubhash(int L, int R) {     return (hash1[R][L & 1] -             (hash1[L - 1][L & 1] * pp[cntzero[R] - cntzero[L - 1]]) % mods +             mods) %            mods;   }   int main() {     scanf( %d , &N);     scanf( %s , s + 1);     init();     gethash(s);     int q;     scanf( %d , &q);     while (q--) {       int l1, l2, lens;       scanf( %d%d%d , &l1, &l2, &lens);       if (getsubhash(l1, l1 + lens - 1) == getsubhash(l2, l2 + lens - 1))         printf( Yes n );       else         printf( No n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int binary_search(vector<int> a, int korol) {     if (a[a.size() - 1] < korol) return a.size() - 1;     if (a[0] > korol) return -1;     int max = a.size() - 1, min = 0, mid;     if (min + max >= 0)       mid = (int)((min + max) / 2);     else       mid = (int)((min + max - 1) / 2);     while (1) {       while (a[mid] > korol) {         max = mid;         if (min + max >= 0)           mid = (int)((min + max) / 2);         else           mid = (int)((min + max - 1) / 2);       }       while (a[mid] < korol && mid != min) {         min = mid;         if (min + max >= 0)           mid = (int)((min + max) / 2);         else           mid = (int)((min + max - 1) / 2);       }       if (min == mid) return min;     }   }   int result(vector<int> a, vector<int> b, int korol) {     sort(a.begin(), a.begin() + a.size());     sort(b.begin(), b.begin() + b.size());     int min_a = binary_search(a, korol);     int min_b = binary_search(b, korol);     if (min_a != -1 && min_b == -1) {       cout <<  YES ;       return 1;     }     if (min_b != -1 && min_a != -1 && a[min_a] > b[min_b]) {       cout <<  YES ;       return 1;     }     if (min_a + 1 != a.size() && min_b + 1 == b.size()) {       cout <<  YES ;       return 1;     }     if (min_a + 1 != a.size() && min_b + 1 != b.size() &&         a[min_a + 1] < b[min_b + 1]) {       cout <<  YES ;       return 1;     }     return 0;   }   int main() {     int n;     cin >> n;     int korol_x, korol_y;     cin >> korol_x >> korol_y;     vector<int> slon_na_glav(0);     vector<int> slon_na_dobav(0);     vector<int> pomecha_gor(0);     vector<int> pomecha_ver(0);     vector<int> ladia_na_gor(0);     vector<int> ladia_na_ver(0);     vector<int> pomecha_glav(0);     vector<int> pomecha_dobav(0);     for (int i = 0; i < n; i++) {       char fig;       cin >> fig;       int x, y;       cin >> x >> y;       if (fig ==  B ) {         if (y - x == korol_y - korol_x) slon_na_glav.push_back(x);         if (y + x == korol_x + korol_y) slon_na_dobav.push_back(x);         if (x == korol_x) pomecha_ver.push_back(y);         if (y == korol_y) pomecha_gor.push_back(x);       }       if (fig ==  R ) {         if (y - x == korol_y - korol_x) pomecha_glav.push_back(x);         if (y + x == korol_x + korol_y) pomecha_dobav.push_back(x);         if (x == korol_x) ladia_na_ver.push_back(y);         if (y == korol_y) ladia_na_gor.push_back(x);       }       if (fig ==  Q ) {         if (y - x == korol_y - korol_x) slon_na_glav.push_back(x);         if (y + x == korol_x + korol_y) slon_na_dobav.push_back(x);         if (x == korol_x) ladia_na_ver.push_back(y);         if (y == korol_y) ladia_na_gor.push_back(x);       }     }     int progress = 0;     if (!slon_na_glav.empty()) {       if (pomecha_glav.empty()) {         cout <<  YES ;         return 0;       }       progress = result(slon_na_glav, pomecha_glav, korol_x);       if (progress) return 0;     }     if (!slon_na_dobav.empty()) {       if (pomecha_dobav.empty()) {         cout <<  YES ;         return 0;       }       progress = result(slon_na_dobav, pomecha_dobav, korol_x);       if (progress) return 0;     }     if (!ladia_na_gor.empty()) {       if (pomecha_gor.empty()) {         cout <<  YES ;         return 0;       }       progress = result(ladia_na_gor, pomecha_gor, korol_x);       if (progress) return 0;     }     if (!ladia_na_ver.empty()) {       if (pomecha_ver.empty()) {         cout <<  YES ;         return 0;       }       progress = result(ladia_na_ver, pomecha_ver, korol_y);       if (progress) return 0;     }     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void boost() {     ios_base::sync_with_stdio();     cin.tie(0);     cout.tie(0);   }   const long long maxn = 1e5 + 123;   const long long inf = 1e9 + 123;   const long long mod = 1e9 + 7;   const double infm = 1e-9;   const double pi = acos(-1);   map<char, int> m;   int main() {     boost();     int n;     cin >> n;     string s;     cin >> s;     int ans = 0;     for (int i = 1; i < s.size(); i++) {       if (s[i] == s[i - 1]) {         m[s[i - 1]]++;         m[s[i + 1]]++;         if (m[ B ] == 0) {           s[i] =  B ;         } else if (m[ G ] == 0) {           s[i] =  G ;         } else if (m[ R ] == 0) {           s[i] =  R ;         }         m[s[i - 1]]--;         m[s[i + 1]]--;         ans++;       }     }     cout << ans <<   n  << s;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void readInt(int &num) {     num = 0;     int c = getchar();     int s = 1;     if (c ==  - ) {       s = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       num = num * 10 + (c -  0 );       c = getchar();     }     num *= s;   }   inline void printInt(int num) {     string s =   ;     if (num < 0) {       putchar( - );       num = -num;     }     if (num == 0) putchar( 0 );     while (num) {       s +=  0  + num % 10;       num /= 10;     }     for (int i = s.size() - 1; i >= 0; i--) {       putchar(s[i]);     }   }   inline void readLL(long long &num) {     num = 0;     int c = getchar();     int s = 1;     if (c ==  - ) {       s = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       num = num * 10 + (c -  0 );       c = getchar();     }     num *= s;   }   inline void printLL(long long num) {     string s =   ;     if (num < 0) {       putchar( - );       num = -num;     }     if (num == 0) putchar( 0 );     while (num) {       s +=  0  + num % 10;       num /= 10;     }     for (int i = s.size() - 1; i >= 0; i--) {       putchar(s[i]);     }   }   inline void readString(string &s) {     s =   ;     char c = getchar();     while (c !=     && c !=   n  && c != EOF) {       s += c;       c = getchar();     }   }   inline void readLine(string &s) {     s =   ;     char c = getchar();     while (c !=   n  && c != EOF) {       s += c;       c = getchar();     }   }   inline void printString(const string &s) {     for (int i = 0; i < (int)s.size(); i++) putchar(s[i]);   }   int main() {     int n;     readInt(n);     string str;     string zeroes =   ;     string ugly =   ;     bool is_find = false;     for (int i = 0; i < (int)n; i++) {       string temp;       readString(temp);       if (temp.size() == 1 && temp[0] ==  0 ) {         putchar( 0 );         return 0;       } else if (!is_find) {         if (temp[0] !=  1 ) {           ugly = temp;           is_find = true;         } else if (temp.size() > 1) {           string tz =   ;           for (int i = 1; i < temp.size(); i++)             if (temp[i] !=  0 ) {               ugly = temp;               is_find = true;               break;             } else               tz +=  0 ;           if (!is_find) zeroes += tz;         }       } else if (temp.size() > 1) {         for (int j = 1; j < temp.size(); j++) zeroes +=  0 ;       }     }     string ans = (is_find) ? ugly + zeroes :  1  + zeroes;     printString(ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int fa[100020], sz[100020];   vector<int> g[100020];   int n, m, q;   int find(int x) {     if (fa[x] != x) fa[x] = find(fa[x]);     return fa[x];   }   int dp[100020][3], G[100020], mx[100020];   vector<int> d[100020];   vector<long long> sum[100020];   bool cmp(int i, int j) { return i > j; }   void dfs(int u, int p) {     for (auto v : g[u]) {       if (v == p) continue;       dfs(v, u);       dp[u][2] = dp[v][0] + 1;       sort(dp[u], dp[u] + 3, cmp);     }   }   void dfs2(int u, int p) {     for (auto v : g[u]) {       if (v == p) continue;       int t;       if (dp[u][0] == dp[v][0] + 1)         t = dp[u][1];       else         t = dp[u][0];       G[v] = max(G[u] + 1, t + 1);       dfs2(v, u);     }   }   int main() {     scanf( %d%d%d , &n, &m, &q);     for (int i = 1; i <= n; ++i) {       fa[i] = i;       sz[i] = 1;     }     for (int i = 1; i <= m; ++i) {       int u, v;       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);       sz[find(v)] += sz[find(u)];       fa[find(u)] = find(v);     }     for (int i = 1; i <= n; ++i) {       if (fa[i] == i) {         dfs(i, 0);       }     }     for (int i = 1; i <= n; ++i) {       if (fa[i] == i) dfs2(i, 0);     }     for (int i = 1; i <= n; ++i) {       d[i].push_back(0);       sum[i].push_back(0);     }     for (int i = 1; i <= n; ++i) {       int x = find(i);       mx[x] = max(mx[x], max(dp[i][0], G[i]));       int t = max(dp[i][0], G[i]);       d[x].push_back(t);       sum[x].push_back(0);     }     for (int i = 1; i <= n; ++i) {       sort(d[i].begin(), d[i].end());       if (fa[i] == i) {         for (int j = 1; j < sum[i].size(); ++j) {           sum[i][j] = sum[i][j - 1] + d[i][j];         }       }     }     map<pair<int, int>, double> ans;     while (q--) {       int x, y;       scanf( %d%d , &x, &y);       x = find(x), y = find(y);       if (x == y) {         puts( -1 );         continue;       }       if (sz[x] > sz[y]) swap(x, y);       if (ans.count(make_pair(x, y))) {         printf( %.10lf n , ans[make_pair(x, y)]);         continue;       }       long long res = 0;       int k = max(mx[x], mx[y]);       bool fst = 1;       for (auto v : d[x]) {         if (fst) {           fst = 0;           continue;         }         int p = lower_bound(d[y].begin(), d[y].end(), k - 1 - v) - d[y].begin();         p = max(p, 1);         res += sum[y][sum[y].size() - 1] - sum[y][p - 1] +                1LL * (v + 1) * (sum[y].size() - p);         res += 1LL * (p - 1) * k;       }       double res2 = res * 1.0 / sz[x] / sz[y];       ans[make_pair(x, y)] = res2;       printf( %.12lf n , res2);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int l[1000000];   int one_line[5001][5001] = {0};   int summ[5001][5001] = {0};   int fac[5001];   int main() {     int n, m, p;     scanf( %d %d %d , &n, &m, &p);     for (int i = 0; i < n; i++) scanf( %d , &l[i]);     fac[0] = 1;     for (int i = 1; i <= 5000; i++) fac[i] = ((fac[i - 1]) * (long long)i) % p;     one_line[1][1] = 1;     for (int i = 2; i <= 5000; i++) {       for (int j = 1; j <= min(m, 5000); j++) {         one_line[i][j] =             (one_line[i - 1][j] * (long long)(j - 1) + one_line[i - 1][j - 1]) %             p;       }     }     summ[1][1] = m;     for (int i = 2; i <= 5000; i++) {       for (int j = 1; j <= min(m, 5000); j++) {         summ[i][j] = (summ[i - 1][j] * (long long)(j - 1) +                       summ[i - 1][j - 1] * (long long)(m - j + 1)) %                      p;       }     }     int prev_progress[5001] = {0};     int prev_l = 0;     prev_progress[0] = 1;     for (int i = 0; i < n; i++) {       int next_progress[5001];       for (int j = 0; j <= l[i]; j++) next_progress[j] = 0;       int tot1 = 0;       for (int j = 0; j <= prev_l; j++) tot1 = (tot1 + prev_progress[j]) % p;       int tot2 = 1;       tot2 = (tot2 * (long long)m) % p;       for (int j = 1; j < l[i]; j++) tot2 = (tot2 * (long long)(m - 1)) % p;       for (int j = 1; j <= l[i]; j++) {         next_progress[j] = (tot1 * (long long)summ[l[i]][j]) % p;         int prev = 0;         if (j <= prev_l) prev = prev_progress[j];         next_progress[j] -=             (((prev * (long long)one_line[l[i]][j]) % p) * fac[j]) % p;         if (next_progress[j] < 0) next_progress[j] += p;       }       for (int j = 0; j <= l[i]; j++) prev_progress[j] = next_progress[j];       prev_l = l[i];     }     int res = 0;     for (int i = 0; i <= prev_l; i++) res = (res + prev_progress[i]) % p;     printf( %d n , res);   }


#include <bits/stdc++.h>   using namespace std;   long long int pwr(long long int base, long long int p) {     long long int ans = 1;     while (p) {       if (p & 1) ans = (ans * base) % 1000000007;       base = (base * base) % 1000000007;       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);   }   long long int n;   int dyn[5005][5005][2];   char ch[5001][2];   int dp(int neww, int val, int cont) {     dyn[neww][val][cont] %= 1000000007;     if (neww == n && !val) return 1;     if (dyn[neww][val][cont] != -1) return dyn[neww][val][cont];     if (val < 0 || neww > n) return 0;     int final = 0;     if (ch[neww][0] !=  f ) {       final += dp(neww + 1, val, 0);       final = final % 1000000007;     } else {       final += dp(neww + 1, val + 1, 1);       final = final % 1000000007;     }     if (cont == 0) {       final += dp(neww, val - 1, 0);       final = final % 1000000007;     }     final = final % 1000000007;     return dyn[neww][val][cont] = final;   }   int main() {     int i, j, k, m;     memset(dyn, -1, sizeof dyn);     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %s , &ch[i]);     }     int ans = dp(0, 0, 0);     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int N;   vector<pair<int, int> > v;   vector<int> query;   int ans;   struct SEG {     SEG(int l, int r, int sum) : l(l), r(r), sum(sum) {}     int l, r;     int sum;   };   vector<SEG> seg;   void init(int idx, int s, int e) {     if (s == e) return;     seg[idx].l = seg.size();     seg.push_back({-1, -1, 0});     seg[idx].r = seg.size();     seg.push_back({-1, -1, 0});     init(seg[idx].l, s, (s + e) / 2);     init(seg[idx].r, (s + e) / 2 + 1, e);   }   void update(int idx, int s, int e, int x, int y) {     seg[idx].sum += y;     if (s == e) return;     if (x <= (s + e) / 2) {       update(seg[idx].l, s, (s + e) / 2, x, y);     } else {       update(seg[idx].r, (s + e) / 2 + 1, e, x, y);     }   }   int calc(int idx, int s, int e, int x, int y) {     if (x <= s && e <= y) return seg[idx].sum;     if (x > e || y < s) return 0;     return calc(seg[idx].l, s, (s + e) / 2, x, y) +            calc(seg[idx].r, (s + e) / 2 + 1, e, x, y);   }   int main() {     seg.push_back({-1, -1, 0});     scanf( %d , &N);     init(0, 0, N - 1);     for (int i = 0; i < N; i++) {       int x;       scanf( %d , &x);       v.push_back({(x > 0) ? x : -x, i});       update(0, 0, N - 1, i, 1);     }     sort(v.begin(), v.end());     while (!v.empty()) {       pair<int, int> now = v.back();       v.pop_back();       update(0, 0, N - 1, now.second, -1);       query.push_back(now.second);       while (!v.empty() && v.back().first == now.first) {         query.push_back(v.back().second);         update(0, 0, N - 1, v.back().second, -1);         v.pop_back();       }       while (!query.empty()) {         int x = query.back();         query.pop_back();         ans += min(calc(0, 0, N - 1, 0, x - 1), calc(0, 0, N - 1, x + 1, N - 1));       }     }     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   const int MAX_LEN = 1010;   using namespace std;   template <typename U, typename V>   string to_string(pair<U, V>);   string to_string(const string& e_) { return     + e_ +    ; }   string to_string(char e_) { return      + string(1, e_) +     ; }   string to_string(bool e_) { return e_ ?  true  :  false ; }   template <typename T>   string to_string(T e_) {     string s_ =  [  ;     for (const auto& x_ : e_) s_ += to_string(x_) +    ;     return s_ +  ] ;   }   template <typename U, typename V>   string to_string(pair<U, V> e_) {     return  (  + to_string(e_.first) +  ,   + to_string(e_.second) +  ) ;   }   void dbg_str() { ; }   template <typename U, typename... V>   void dbg_str(U u, V... v) {     ;     dbg_str(v...);   }   int n, m, x;   map<int, vector<int> > a;   map<vector<int>, vector<int> > b;   int main() {     scanf( %d , &n);     if (n == 2) {       vector<int> l;       scanf( %d %d , &m, &x);       printf( 1 %d n , x);       printf( %d  , --m);       while (m--) {         scanf( %d , &x);         printf( %d  , x);       }       exit(0);     }     for (int i = 0; i < (n * (n - 1)) / 2; i++) {       scanf( %d , &m);       while (m--) {         scanf( %d , &x);         a[x].push_back(i);       }     };     for (auto& it : a) {       b[it.second].push_back(it.first);     }     for (auto& it : b) {       printf( %d  , (int)it.second.size());       for (int i = 0; i < (int)it.second.size(); i++) printf( %d  , it.second[i]);       printf(  n );     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int r, c;     char s[15][15];     int cake = 0;     int row = 0;     scanf( %d %d , &r, &c);     for (int i = 1; i <= r; i++) {       scanf( %s , s[i]);     }     for (int i = 1; i <= r; i++) {       for (int j = 0; j < c; j++) {         if (s[i][j] ==  S )           break;         else if (s[i][j] ==  .  && j == c - 1) {           cake += c;           row++;         }       }     }     for (int j = 0; j < c; j++) {       for (int i = 1; i <= r; i++) {         if (s[i][j] ==  S )           break;         else if (s[i][j] ==  .  && i == r)           cake += (r - row);       }     }     printf( %d , cake);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   char str[105];   ll best[105];   ll memo[105][105][105];   ll dp(int l, int r, int p) {     if (l >= r) return 0;     if (memo[l][r][p] != -1) return memo[l][r][p];     int ptr = l;     while (ptr < r && str[ptr] == str[l]) ptr++;     ll ret = best[p + ptr - l] + dp(ptr, r, 0);     for (int i = ptr; i < r; ++i) {       if (str[i] == str[l]) {         ret = max(ret, dp(ptr, i, 0) + dp(i, r, p + ptr - l));       }     }     return memo[l][r][p] = ret;   }   int main() {     int n;     cin >> n >> str;     for (int i = 1; i <= n; ++i) {       cin >> best[i];       for (int j = 1; j < i; ++j) {         best[i] = max(best[i], best[j] + best[i - j]);       }     }     memset(memo, -1, sizeof memo);     cout << dp(0, n, 0) <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   double eps = 1e-9;   const int INF = 1e9 + 7;   const int MAXN = int(3e5 + 7);   vector<pair<int, int> > v[MAXN];   int n, p, a[MAXN];   long long ans[MAXN], dp[MAXN];   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , a + i);     scanf( %d , &p);     for (int i = 0; i < p; i++) {       int x, y;       scanf( %d%d , &x, &y);       v[y].push_back(make_pair(x, i));     }     for (int i = 1; i <= 500; i++) {       for (int j = n; j > 0; j--) {         dp[j] += a[j];         if (j + i <= n) dp[j] += dp[j + i];       }       for (int j = 0; j < v[i].size(); j++)         ans[v[i][j].second] = dp[v[i][j].first];       memset(dp, 0, sizeof(dp));     }     for (int i = 501; i <= 300000; i++) {       for (int j = 0; j < v[i].size(); j++) {         long long cur = 0;         for (int k = v[i][j].first; k <= n; k += i) cur += a[k];         ans[v[i][j].second] = cur;       }     }     for (int i = 0; i < p; i++) printf( %I64d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 16;   string s;   long long string_int(string s) {     long long num = 0;     for (int i = 0; i < s.size(); i++) {       num += s[i] -  0 ;       num *= 10;     }     return num / 10;   }   int main() {     ios_base::sync_with_stdio(false);     cin >> s;     bool flag = false;     if (s[0] ==  0 ) flag = true;     int epos = s.find( e );     int d = string_int(s.substr(epos + 1));     s = s.substr(0, epos);     for (int i = 0; i < 125; i++) s.push_back( 0 );     s.erase(1, 1);     int exp = 1 + d;     s.insert(exp,  . );     epos = s.find( e );     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  . ) {         s = s.substr(i - 1);         break;       } else if (s[i] !=  0 ) {         s = s.substr(i);         break;       }     }     for (int i = s.size() - 1; i >= 0; i--) {       if (s[i] ==  0 )         s.pop_back();       else         break;     }     if (s[s.size() - 1] ==  . ) s.pop_back();     cout << s << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     while (cin >> n) {       vector<int> v;       int x;       for (int i = 0; i < n; i++) {         cin >> x;         v.push_back(x);       }       bool in = false;       int cnt = 0;       bool f = true;       bool s = false, l = false;       int ind1 = 1, ind2 = 1;       for (int i = 1; i < n; i++) {         bool d = false;         while (v[i - 1] > v[i] && i < n) {           d = true;           if (f) {             ind1 = i;             ind2 = i + 1;             f = false;           } else {             ind2 = i + 1;           }           if (i - 1 == 0) s = true;           if (i == n - 1) l = true;           i++;         }         if (d) {           d = false;           cnt++;         }       }       bool val = false;       if (cnt == 0)         val = 1;       else if (cnt == 1) {         if (s && l)           val = true;         else if (s && v[ind1 - 1] < v[ind2])           val = true;         else if (l && v[ind2 - 1] > v[ind1 - 2])           val = true;         else if (v[ind1 - 1] < v[ind2] && v[ind2 - 1] > v[ind1 - 2])           val = true;       }       if (val) {         cout <<  yes  << endl;         cout << ind1 <<     << ind2 << endl;       } else {         cout <<  no  << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[300005];   int rev[300005];   int b[300005];   int main() {     int n, m;     long long ans = 0;     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       rev[a[i]] = i;     }     for (int i = 0; i < m; i++) {       int x, y;       scanf( %d %d , &x, &y);       x = rev[x];       y = rev[y];       b[max(x, y)] = max(b[max(x, y)], min(x, y));     }     int t = 0;     for (int i = 1; i <= n; i++) {       t = max(t, b[i]);       ans += i - t;     }     printf( %I64d n , ans);     return 0;   }