#include <bits/stdc++.h>   using namespace std;   const int N = 300010;   const long long inf = 4e18;   int n, k, x1[N], var[N], x2[N], y2[N], x[N], y[N];   pair<pair<int, int>, int> px[N], py[N], py1[N];   long long ans;   long long solve(pair<pair<int, int>, int> *p, int n, int k, int w) {     int j = n - k;     long long res = inf;     for (int i = 1; i <= k + 1; i++) {       int yl = p[i].first.first;       int yr = p[j].first.first;       int h = yr - yl;       if (h & 1) {         h >>= 1;         h++;       } else {         h >>= 1;       }       if (h == 0) h++;       res = min(res, 1ll * w * h);       j++;     }     return res;   }   void remove(pair<pair<int, int>, int> *p, int n, int val) {     int j = -1;     for (int i = 1; i <= n; i++)       if (p[i].second == val) {         j = i;         break;       }     for (int i = j; i < n; i++) p[i] = p[i + 1];   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++)       scanf( %d%d%d%d , &x1[i], &var[i], &x2[i], &y2[i]);     for (int i = 1; i <= n; i++) {       x[i] = x1[i] + x2[i];       y[i] = var[i] + y2[i];     }     for (int i = 1; i <= n; i++) py[i] = make_pair(make_pair(y[i], x[i]), i);     sort(py + 1, py + n + 1);     for (int i = 1; i <= n; i++) px[i] = make_pair(make_pair(x[i], y[i]), i);     sort(px + 1, px + n + 1);     int n1 = n;     ans = inf;     for (int i = 1; i <= k + 1; i++) {       int n2 = n1;       for (int j = 1; j <= n2; j++) py1[j] = py[j];       for (int j = n; j >= n - (k - (i - 1)); j--) {         int xl = px[i].first.first;         int xr = px[j].first.first;         int w = xr - xl;         if (w & 1) {           w >>= 1;           w++;         } else {           w >>= 1;         }         if (w == 0) w++;         ans = min(ans, solve(py1, n2, k - (i - 1) - (n - j), w));         remove(py1, n2, px[j].second);         n2--;       }       remove(py, n1, px[i].second);       n1--;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   string str[] = { 012345 ,  041235 ,  034125 ,  023415 ,  104523 ,  120453 ,                    152043 ,  145203 ,  215304 ,  201534 ,  230154 ,  253014 };   bool solve(string x, string y) {     for (int i = 0; i < 12; i++) {       string t1;       for (int j = 0; j < 6; j++) {         t1 += x[str[i][j] -  0 ];       }       if (t1 == y) return true;       string rev = str[i];       reverse(rev.begin(), rev.end());       string t2;       for (int j = 0; j < 6; j++) {         t2 += x[rev[j] -  0 ];       }       if (t2 == y) return true;     }     return false;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> s;     sort(s.begin(), s.end());     vector<string> v;     do {       bool flag = false;       for (int i = 0; i < v.size(); i++) {         if (solve(s, v[i])) {           flag = true;         }       }       if (!flag) v.push_back(s);     } while (next_permutation(s.begin(), s.end()));     cout << v.size() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   map<pair<int, int>, int> rel;   vector<int> x, y, color[4];   int pos[N], block[N];   char s[N];   inline int read() {     int x = 0, f = 1;     char ch = getchar();     for (; ch <  0  || ch >  9 ; ch = getchar())       if (ch ==  - ) f = -1;     for (; ch >=  0  && ch <=  9 ; ch = getchar())       x = (x << 1) + (x << 3) + ch -  0 ;     return x * f;   }   void query() {     if (x.size() == 0) return;     printf( Q %d  , x.size());     for (int i = 0, len = x.size(); i < len; ++i) printf( %d %d  , x[i], y[i]);     puts(  );     fflush(stdout);     scanf( %s , s);     for (int i = 0, len = x.size(); i < len; ++i) {       rel[make_pair(x[i], y[i])] = s[i] -  0 ;       rel[make_pair(y[i], x[i])] = s[i] -  0 ;     }     x.clear();     y.clear();   }   void answer() {     printf( A  );     for (int i = 1; i <= 3; ++i) printf( %d  , color[i].size());     puts(  );     for (int i = 1; i <= 3; ++i) {       for (int j = 0, len = color[i].size(); j < len; ++j)         printf( %d  , color[i][j]);       puts(  );     }     fflush(stdout);   }   int main() {     int T = read();     while (T--) {       int n = read();       rel.clear();       memset(pos, 0, sizeof pos);       memset(block, 0, sizeof block);       for (int i = 1; i < n; i += 2) {         x.push_back(i);         y.push_back(i + 1);       }       query();       for (int i = 2; i < n; i += 2) {         x.push_back(i);         y.push_back(i + 1);       }       query();       int tot = pos[1] = 1;       for (int i = 2; i <= n; ++i)         if (!rel[make_pair(i - 1, i)]) pos[++tot] = i;       for (int i = 1; i < tot - 1; i += 4) {         x.push_back(pos[i]);         y.push_back(pos[i + 2]);       }       for (int i = 2; i < tot - 1; i += 4) {         x.push_back(pos[i]);         y.push_back(pos[i + 2]);       }       query();       for (int i = 3; i < tot - 1; i += 4) {         x.push_back(pos[i]);         y.push_back(pos[i + 2]);       }       for (int i = 4; i < tot - 1; i += 4) {         x.push_back(pos[i]);         y.push_back(pos[i + 2]);       }       query();       block[1] = 1;       block[2] = tot > 1 ? 2 : 0;       for (int i = 3; i <= tot; ++i) {         if (rel[make_pair(pos[i], pos[i - 2])])           block[i] = block[i - 2];         else           block[i] = 6 - block[i - 1] - block[i - 2];       }       for (int i = 1; i <= 3; ++i) color[i].clear();       for (int i = 1, j = 1; i <= tot; ++i)         while (j ^ pos[i + 1] && j <= n) color[block[i]].push_back(j++);       answer();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int f(int a, int b) {     int ans = 1;     for (int i = 0; i < b; i++) ans *= a;     return ans;   }   int main() {     int a, b, sgn, n, ans = 1999;     scanf( %d %d %d , &a, &b, &n);     sgn = (a < 0) ^ (b < 0);     a = abs(a);     b = abs(b);     if (sgn == 1 && n % 2 == 0 && b != 0)       printf( No solution n );     else if (a == 0 && b == 0)       printf( 0 n );     else if (a == 0)       printf( No solution n );     else if (b % a != 0)       printf( No solution n );     else {       b /= a;       for (int i = 0;; i++) {         int k = f(i, n);         if (k == b) {           ans = i * (sgn == 1 ? -1 : 1);           break;         }         if (k > b) {           printf( No solution n );           break;         }       }       if (ans != 1999) printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mod;   struct EDGE {     int to, nxt;   } box[101000 * 2];   int h[101000], top, f1[101000], f2[101000];   void add(int u, int v) {     box[top].to = v;     box[top].nxt = h[u];     h[u] = top++;   }   int dp[610][610];   int dfs(int a, int b) {     if (dp[a][b] + 1) return dp[a][b];     int p = h[a];     dp[a][b] = 0;     while (p != -1) {       dp[a][b] = (dp[a][b] + dfs(box[p].to, b)) % mod;       p = box[p].nxt;     }     return dp[a][b];   }   long long ni(long long i) {     int n = mod - 2;     long long res = 1;     while (n) {       if (n & 1) res = res * i % mod;       i = i * i % mod;       n >>= 1;     }     return res;   }   int ss[610], sn[610], c1, c2;   int mat[610][610];   int main() {     int n, m;     scanf( %d%d%d , &n, &m, &mod);     memset(dp, -1, sizeof(dp));     memset(h, -1, sizeof(h));     for (int i = 1; i <= m; i++) {       int a, b;       scanf( %d%d , &a, &b);       f1[a] = 1;       f2[b] = 1;       add(a, b);     }     for (int i = 1; i <= n; i++) dp[i][i] = 1;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (dp[i][j] == -1) dfs(i, j);       }     }     for (int i = 1; i <= n; i++) {       if (!f1[i]) sn[c1++] = i;       if (!f2[i]) ss[c2++] = i;     }     for (int i = 0; i < c1; i++) {       for (int j = 0; j < c1; j++) {         mat[i][j] = dp[ss[i]][sn[j]];       }     }     long long ans = 1;     for (int j = 0; j < c1; j++) {       int r;       for (r = j; r < c1; r++) {         if (mat[r][j]) break;       }       if (r == c1) {         ans = 0;         break;       }       for (int k = j; k < c1; k++) swap(mat[j][k], mat[r][k]);       if (r != j) ans = -ans;       for (int i = 0; i < c1; i++) {         if (i == j) continue;         long long a = mat[i][j], b = mat[j][j];         for (int k = 0; k < c1; k++) {           mat[i][k] = (mat[i][k] * b - mat[j][k] * a) % mod;         }         ans = ans * ni(b) % mod;       }     }     for (int i = 0; i < c1; i++) ans = (ans * mat[i][i]) % mod;     printf( %I64d n , (ans + mod) % mod);   }


#include <bits/stdc++.h>   using namespace std;   vector<int> adj[100], kk;   int n, m, c1, c2, vb[100], cc, ff;   void dfs(int x) {     c1++;     vb[x] = 1;     c2 += adj[x].size();     for (int u : adj[x]) {       if (!vb[u]) dfs(u);     }     return;   }   bool cmp(int u, int v) {     memset(vb, 0, sizeof(vb));     dfs(u);     return (!vb[v]);   }   int main() {     cin >> n >> m;     for (int i = 1; i <= m; i++) {       int x, y;       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     for (int i = 1; i <= n; i++) {       if (adj[i].size() > 2) {         cout <<  NO  << endl;         return 0;       }     }     for (int i = 1; i <= n; i++) {       if (!vb[i]) {         c1 = c2 = 0;         dfs(i);         if (c1 * 2 == c2) cc = 1;         ff++;       }     }     if (cc && ff > 1) {       cout <<  NO  << endl;       return 0;     }     cout <<  YES  << endl;     cout << n - m << endl;     if (n == 1 && m == 0) {       cout <<  1 1  << endl;       return 0;     }     while (m < n) {       for (int i = 1; i <= n; i++)         if (adj[i].size() < 2) kk.push_back(i);       if (m == n - 1 && kk.size() == 2) {         cout << kk[0] <<     << kk[1] << endl;         return 0;       }       for (int i = 1; i < kk.size(); i++) {         if (cmp(kk[0], kk[i])) {           cout << kk[0] <<     << kk[i] << endl;           adj[kk[0]].push_back(kk[i]);           adj[kk[i]].push_back(kk[0]);           break;         }       }       kk.clear();       m++;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, m, a = 0;     cin >> n >> m;     for (int i = 0; i < n; i++) {       long long int t = 100000000000;       for (int j = 0; j < m; j++) {         long long int r;         cin >> r;         t = min(t, r);       }       a = max(t, a);     }     cout << a;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long x[9999999];   int a = 0, b, c, n;   int bbb(int iw, int zw, long long t) {     if (t > x[zw] || t < x[iw]) return 0;     while (zw >= iw) {       int qccc = (iw + zw) >> 1;       if (t < x[qccc])         zw = qccc - 1;       else if (x[qccc] < t)         iw = qccc + 1;       else         return 1;     }     return 0;   }   int checkk(int r) {     for (int i = 32; i >= 0; i--) {       long long qwe = (1LL << i);       if (qwe <= x[r]) return 0;       if (bbb(1, r - 1, qwe - x[r]) || bbb(r + 1, n, qwe - x[r])) return 1;     }     return 0;   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n + 1; i++) {       scanf( %lld , &x[i]);     }     sort(x + 1, x + n + 1);     for (int i = 1; i <= n; i++) {       if (!checkk(i)) a++;     }     printf( %d , a);     return 0;   }


#include<bits/stdc++.h>   using namespace std;      namespace program {    const int N = 100005;        struct edge {     int to, nxt;     edge() {}     edge(int to_, int nxt_): to(to_), nxt(nxt_) {}    } e[N << 1];    int hd[N], cnt, fir[N], end[N];    vector<pair<int, int> > del, ins;        void adde(int u, int v) {     e[++cnt] = edge(v, hd[u]); hd[u] = cnt;     e[++cnt] = edge(u, hd[v]); hd[v] = cnt;    }    bool dfs(int u, int fa) {     vector<int> son1, son2;     for(int i = hd[u]; i; i = e[i].nxt) {      int v = e[i].to;      if(v == fa) continue;      if(dfs(v, u)) {       son1.push_back(v);      }      else {       son2.push_back(v);      }     }     if(!son1.size()) {      end[u] = u;     }     else if(son1.size() == 1) {      end[u] = end[son1[0]];     }     else {      end[u] = end[son1[0]];      fir[u] = end[son1[1]];      if(fa) del.push_back(make_pair(fa, u));      for(int i = 2; i < son1.size(); ++i) {       del.push_back(make_pair(u, son1[i]));       ins.push_back(make_pair(end[u], son1[i]));       end[u] = end[son1[i]];      }     }     for(int i = 0; i < son2.size(); ++i) {      ins.push_back(make_pair(end[u], fir[son2[i]]));      end[u] = end[son2[i]];     }     return son1.size() <= 1;    }    int main() {     int T;     scanf( %d , &T);     while(T--) {      int n;      scanf( %d , &n);      cnt = 0;      memset(hd + 1, 0, sizeof(int) * n);      for(int i = 1; i < n; ++i) {       int u, v;       scanf( %d%d , &u, &v);       adde(u, v);      }      del.clear();      ins.clear();      dfs(1, 0);      printf( %d n , del.size());      for(int i = 0; i < del.size(); ++i) {       printf( %d %d %d %d n , del[i].first, del[i].second, ins[i].first, ins[i].second);      }     }     return 0;    }   }   int main() {    return program::main();   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100000 + 10;   double power(double x, int n) {     if (n <= 1) return n ? x : 1;     double t = power(x, n / 2);     if (n % 2)       return t * t * x;     else       return t * t;   }   double p[maxn];   int main() {     int m, n;     scanf( %d%d , &m, &n);     for (int i = 1; i <= m; i++) p[i] = power(i * 1.0 / m, n);     for (int i = m; i >= 1; i--) p[i] -= p[i - 1];     double ans = 0;     for (int i = 1; i <= m; i++) ans += i * p[i];     printf( %.15f n , ans);   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> dir[4];   int ans = 0;   int n, m;   char maz[55][55];   char op[105];   void bfs() {     int x, y;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         if (maz[i][j] ==  S ) {           x = i, y = j;         }       }     }     for (int i = 0; op[i]; ++i) {       int t = op[i] -  0 ;       x += dir[t].first;       y += dir[t].second;       if (x < 1 || y < 1 || x > n || y > m || maz[x][y] ==  # ) return;       if (maz[x][y] ==  E ) {         ans++;         return;       }     }   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; ++i) {       scanf( %s , maz[i] + 1);     }     scanf( %s , op);     dir[0] = pair<int, int>(0, 1);     dir[1] = pair<int, int>(0, -1);     dir[2] = pair<int, int>(1, 0);     dir[3] = pair<int, int>(-1, 0);     sort(dir, dir + 4);     do {       bfs();     } while (next_permutation(dir, dir + 4));     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const long double pi = acos(-1);   const int n_ = 1e5 + 5;   int n;   long long a[n_], b[n_], dp[n_];   vector<int> st;   vector<long double> p;   long double getIntersect(int i, int j) {     return (dp[j] - dp[i]) / (long double)(b[i] - b[j]);   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %lld , a + i);     for (int i = 0; i < n; i++) scanf( %lld , b + i);     dp[0] = b[0];     st.push_back(0);     p.push_back(2e9);     int id;     for (int i = 1; i < n; i++) {       id = lower_bound(p.begin(), p.end(), (long double)a[i] - 1) - p.begin();       dp[i] = dp[st[id]] + b[st[id]] * (a[i] - 1) + b[i];       while (st.size() > 1 && getIntersect(st[st.size() - 2], st.back()) >                                   getIntersect(st.back(), i)) {         st.pop_back();         p.pop_back();       }       p.back() = getIntersect(st.back(), i);       p.push_back(2e9);       st.push_back(i);     }     printf( %lld , dp[n - 1]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n, m, flag = 0;     cin >> n >> m;     long long int a[n][m], b[n][m];     for (long long int(i) = 0; (i) < (n); ++(i)) {       for (long long int(j) = 0; (j) < (m); ++(j)) {         cin >> a[i][j];       }     }     for (long long int(i) = 0; (i) < (n); ++(i)) {       for (long long int(j) = 0; (j) < (m); ++(j)) {         cin >> b[i][j];       }     }     for (long long int(i) = 0; (i) < (n); ++(i)) {       for (long long int(j) = 0; (j) < (m); ++(j)) {         if (a[i][j] > b[i][j]) swap(a[i][j], b[i][j]);       }     }     for (long long int(i) = 0; (i) < (n); ++(i)) {       for (long long int(j) = 0; (j) < (m - 1); ++(j)) {         if (a[i][j] >= a[i][j + 1]) {           flag = 1;         }       }     }     for (long long int(j) = 0; (j) < (m); ++(j)) {       for (long long int(i) = 0; (i) < (n - 1); ++(i)) {         if (a[i][j] >= a[i + 1][j]) {           flag = 1;         }       }     }     for (long long int(i) = 0; (i) < (n); ++(i)) {       for (long long int(j) = 0; (j) < (m - 1); ++(j)) {         if (b[i][j] >= b[i][j + 1]) {           flag = 1;         }       }     }     for (long long int(j) = 0; (j) < (m); ++(j)) {       for (long long int(i) = 0; (i) < (n - 1); ++(i)) {         if (b[i][j] >= b[i + 1][j]) {           flag = 1;         }       }     }     if (flag == 0)       cout <<  Possible ;     else       cout <<  Impossible ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[100500];   char c[200100];   int l[100100][26], r[100100][26];   int last[26];   int main() {     long long res;     int tmp;     int n, k, ll, l1, i, j;     cin >> n >> k;     scanf( %s , s);     ll = strlen(s);     memset(last, -1, sizeof(last));     for (i = 0; i < ll; i++) {       last[s[i] -  a ] = i;       for (j = 0; j < 26; j++) l[i][j] = last[j];     }     memset(last, -1, sizeof(last));     for (i = ll - 1; i >= 0; i--) {       last[s[i] -  a ] = i;       for (j = 0; j < 26; j++) r[i][j] = last[j];     }     for (int cnt = 0; cnt < n; cnt++) {       scanf( %s , c);       l1 = strlen(c);       res = 0;       for (j = 0; j < l1; j++) {         tmp = 100500;         if (l[min(j, ll - 1)][c[j] -  a ] != -1)           tmp = abs(j - l[min(j, ll - 1)][c[j] -  a ]);         if (r[min(j, ll - 1)][c[j] -  a ] != -1)           tmp = min(tmp, abs(r[min(j, ll - 1)][c[j] -  a ] - j));         if (tmp == 100500) tmp = l1;         res += tmp;       }       cout << res << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2020;   int Arr[N][N];   int Pre[N][N];   int Add[N][N];   bool Col[N];   bool Row[N];   int getSum(int x1, int y1, int x2, int y2) {     return Pre[x2][y2] - Pre[x1 - 1][y2] - Pre[x2][y1 - 1] + Pre[x1 - 1][y1 - 1];   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     int k;     cin >> k;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) {         char ch;         cin >> ch;         Arr[i][j] = (ch ==  B );       }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         Pre[i][j] = Pre[i - 1][j] + Pre[i][j - 1] - Pre[i - 1][j - 1] + Arr[i][j];     int Total = 0;     for (int i = 1; i <= n; i++) {       if (!getSum(i, 1, i, n)) Row[i] = true, Total++;       if (!getSum(1, i, n, i)) Col[i] = true, Total++;     }     for (int x = 1; x + k - 1 <= n; x++) {       int Cur = Total;       for (int i = 1; i < k; i++)         if (!Col[i])           Cur += (getSum(1, i, n, i) - getSum(x, i, x + k - 1, i) == 0);       for (int y = 1; y + k - 1 <= n; y++) {         if (!Col[y + k - 1])           Cur += (getSum(1, y + k - 1, n, y + k - 1) -                       getSum(x, y + k - 1, x + k - 1, y + k - 1) ==                   0);         Add[x][y] += Cur;         if (!Col[y])           Cur -= (getSum(1, y, n, y) - getSum(x, y, x + k - 1, y) == 0);       }     }     for (int x = 1; x + k - 1 <= n; x++) {       int Cur = 0;       for (int i = 1; i < k; i++)         if (!Row[i])           Cur += (getSum(i, 1, i, n) - getSum(i, x, i, x + k - 1) == 0);       for (int y = 1; y + k - 1 <= n; y++) {         if (!Row[y + k - 1])           Cur += (getSum(y + k - 1, 1, y + k - 1, n) -                       getSum(y + k - 1, x, y + k - 1, x + k - 1) ==                   0);         Add[y][x] += Cur;         if (!Row[y])           Cur -= (getSum(y, 1, y, n) - getSum(y, x, y, x + k - 1) == 0);       }     }     int Ans = 0;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) Ans = max(Ans, Add[i][j]);     cout << Ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin.clear();     fflush(stdin);     int n;     cin >> n;     while (n--) {       string s;       cin >> s;       int year = stoi(s.substr(4, s.size() - 4)), p = 1, x = 1989;       for (int i = 0; i < s.size() - 5; i++) {         p *= 10;         x += p;       }       p *= 10;       while (year < x) {         year += p;       }       cout << year << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct __s {     __s() {       if (1) {         ios_base::Init i;         cin.sync_with_stdio(0);         cin.tie(0);       }     }     ~__s() {       if (!1)         fprintf(stderr,  Execution time: %.3lf s. n ,                 (double)clock() / CLOCKS_PER_SEC);       long long n;       cin >> n;     }   } __S;   long long n, m;   long long x[55];   long long y[55];   long long first[55];   long long second[55];   long long I[55][55];   long long J[55][55];   bool u[55][55];   bool up[55];   bool ud[55];   bool ok[55];   vector<pair<pair<long long, long long>, pair<long long, long long> > > ans;   long long findUp() {     long long idx = -1;     for (long long i = 0; i < (long long)(m); i++) {       if (up[i]) continue;       if (idx == -1 || x[i] < x[idx] || (x[i] == x[idx] && y[i] < y[idx])) {         idx = i;       }     }     return idx;   }   long long findDown() {     long long idx = -1;     for (long long i = 0; i < (long long)(m); i++) {       if (ud[i]) continue;       if (idx == -1 || first[i] > first[idx] ||           (first[i] == first[idx] && second[i] < second[idx])) {         idx = i;       }     }     return idx;   }   void move(long long i, long long dx, long long dy) {     u[x[i]][y[i]] = false;     I[x[i]][y[i]] = -1;     ans.push_back(         make_pair(make_pair(x[i], y[i]), make_pair(x[i] + dx, y[i] + dy)));     x[i] += dx;     y[i] += dy;     u[x[i]][y[i]] = true;     I[x[i]][y[i]] = i;   }   void shift(long long i, long long dx, long long dy) {     long long first = x[i] + dx;     long long second = y[i] + dy;     if (u[first][second]) shift(I[first][second], dx, dy);     move(i, dx, dy);   }   int main(void) {     cin >> n >> m;     for (long long i = 0; i < (long long)(n); i++)       for (long long j = 0; j < (long long)(n); j++) J[i][j] = -1;     for (long long i = 0; i < (long long)(m); i++) {       cin >> x[i] >> y[i];       x[i]--;       y[i]--;       u[x[i]][y[i]] = true;       I[x[i]][y[i]] = i;     }     for (long long i = 0; i < (long long)(m); i++) {       cin >> first[i] >> second[i];       first[i]--;       second[i]--;       J[first[i]][second[i]] = i;     }     for (long long i = 0; i < (long long)(m); i++) {       long long idx = findUp();       while (x[idx] != 0 || y[idx] != i) {         while (x[idx] != 0 && !u[x[idx] - 1][y[idx]]) {           move(idx, -1, 0);         }         while (y[idx] != i) {           while (y[idx] > i && !u[x[idx]][y[idx] - 1]) {             move(idx, 0, -1);           }           while (y[idx] < i && !u[x[idx]][y[idx] + 1]) {             move(idx, 0, 1);           }           if (y[idx] != i) {             idx = I[x[idx]][y[idx] + 1];           }         }         while (x[idx] != 0 && !u[x[idx] - 1][y[idx]]) {           move(idx, -1, 0);         }       }       up[idx] = true;     }     for (long long i = 0; i < (long long)(m); i++) {       long long idx = findDown();       ud[idx] = true;       if (first[idx] < 2) continue;       while (x[idx] != first[idx] || y[idx] != second[idx]) {         while (x[idx] != first[idx] - 1) {           move(idx, 1, 0);         }         while (y[idx] > second[idx]) {           move(idx, 0, -1);         }         while (y[idx] < second[idx]) {           move(idx, 0, 1);         }         move(idx, 1, 0);       }       ok[idx] = true;     }     vector<long long> w[2];     for (long long i = 0; i < (long long)(min(n, 2LL)); i++) {       for (long long j = 0; j < (long long)(n); j++) {         if (J[i][j] == -1) continue;         w[i].push_back(J[i][j]);       }     }     for (long long i = 0; i < (long long)(2); i++) {       for (long long j = 1; j < w[i].size(); ++j) {         if (y[w[i][j - 1]] < y[w[i][j]]) continue;         move(w[i][j], 1, 0);         while (y[w[i][j]] < y[w[i][j - 1]]) move(w[i][j], 0, 1);         shift(w[i][j - 1], 0, -1);         move(w[i][j], -1, 0);       }     }     for (long long i = 0; i < (long long)(w[1].size()); i++) {       move(w[1][i], 1, 0);     }     for (long long i = 0; i < (long long)(2); i++) {       for (long long jj = 0; jj < (long long)(w[i].size()); jj++) {         long long j = w[i].size() - 1 - jj;         while (y[w[i][j]] != n - 1 - jj) move(w[i][j], 0, 1);       }       for (long long j = 0; j < (long long)(w[i].size()); j++) {         while (y[w[i][j]] != second[w[i][j]]) move(w[i][j], 0, -1);       }     }     cout << ans.size() <<   n ;     for (long long i = 0; i < (long long)(ans.size()); i++) {       cout << ans[i].first.first + 1 <<     << ans[i].first.second + 1 <<                << ans[i].second.first + 1 <<     << ans[i].second.second + 1 <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   char s[1000010];   int max_pos_by_allowed_and_len[1 << 20][21];   constexpr int full = (1 << 20) - 1;   template <typename T>   void mx(T& x, T y) {     x = (x > y ? x : y);   }   void update(const int* __restrict__ a, int* __restrict__ b) {     for (int i = 0; i < 21; i += 7) {       mx(b[i + 0], a[i + 0]);       mx(b[i + 1], a[i + 1]);       mx(b[i + 2], a[i + 2]);       mx(b[i + 3], a[i + 3]);       mx(b[i + 4], a[i + 4]);       mx(b[i + 5], a[i + 5]);       mx(b[i + 6], a[i + 6]);     }   }   int main() {     scanf( %s , s);     int n = strlen(s);     for (int i = 0; i < n; ++i) s[i] -=  a ;     for (int i = 1; i <= n; ++i) {       int used = 0;       for (int j = i; j > 0; --j) {         if ((used >> s[j - 1]) & 1) break;         used |= (1 << s[j - 1]);         max_pos_by_allowed_and_len[full ^ used][i - j + 1] = i;       }     }     for (int mask = full; mask > 0; --mask) {       for (int bit = 0; bit < 20; ++bit) {         int updated = mask ^ (1 << bit);         if (updated > mask) continue;         update(&max_pos_by_allowed_and_len[mask][0],                &max_pos_by_allowed_and_len[updated][0]);       }     }     int ans = 1;     for (int i = 1; i <= n; ++i) {       int used = 0;       for (int j = i; j > 0; --j) {         if ((used >> s[j - 1]) & 1) break;         used |= (1 << s[j - 1]);         for (int other_len = 20; other_len >= 0; --other_len) {           if (max_pos_by_allowed_and_len[used][other_len] > i) {             mx(ans, i - j + 1 + other_len);             break;           }         }       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void qSort(long long **a, int first, int last) {     long long i = first, j = last, x = a[(first + last) / 2][0];     do {       while (a[i][0] < x) i++;       while (a[j][0] > x) j--;       if (i <= j) {         if (i < j) {           long long tmp0, tmp1;           tmp0 = a[i][0];           tmp1 = a[i][1];           a[i][0] = a[j][0];           a[i][1] = a[j][1];           a[j][0] = tmp0;           a[j][1] = tmp1;         }         i++;         j--;       }     } while (i <= j);     if (i < last) qSort(a, i, last);     if (first < j) qSort(a, first, j);   }   void build(long long *t, long long *a, int v, int tl, int tr) {     if (tl == tr)       t[v] = max(a[tl], (long long)0);     else {       int tm = (tl + tr) / 2;       build(t, a, v * 2, tl, tm);       build(t, a, v * 2 + 1, tm + 1, tr);       t[v] = max(t[v * 2], (long long)0) + max(t[v * 2 + 1], (long long)0);     }   }   long long sum(long long *t, int v, int tl, int tr, int l, int r) {     if (l > r) return 0;     if (l == tl && r == tr) return t[v];     int tm = (tl + tr) / 2;     return sum(t, v * 2, tl, tm, l, min(r, tm)) +            sum(t, v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);   }   int main() {     int n;     std::cin >> n;     long long *t = new long long[4 * n];     long long *forest = new long long[n];     long long **_forest = new long long *[n];     for (int i = 0; i < n; i++) _forest[i] = new long long[2];     long long tmp;     for (int i = 0; i < n; i++) {       std::cin >> tmp;       forest[i] = tmp;       _forest[i][0] = tmp;       _forest[i][1] = i;     }     qSort(_forest, 0, n - 1);     build(t, forest, 1, 0, n - 1);     long long **couples = new long long *[(n / 2) + 1];     for (int i = 0; i < n / 2 + 1; i++) couples[i] = new long long[2];     int couple_count = 0;     long long now = -1000000000000000000;     int i = 0;     while (i < n) {       now = _forest[i][0];       int min = n, max = 0;       while (i < n && _forest[i][0] == now) {         if (_forest[i][1] < min) min = _forest[i][1];         if (_forest[i][1] > max) max = _forest[i][1];         i++;       }       if (min != max && max != 0) {         couples[couple_count][0] = min;         couples[couple_count][1] = max;         couple_count++;       }     }     long long max = -100000000000000000;     int num = 0;     for (int i = 0; i < couple_count; i++) {       long long su = 0;       su += sum(t, 1, 0, n - 1, couples[i][0] + 1, couples[i][1] - 1);       su = su + forest[couples[i][0]] + forest[couples[i][1]];       if (su > max) {         max = su;         num = i;       }     }     std::vector<int> toDelete;     int del = 0;     del = del + couples[num][0] + (n - 1 - couples[num][1]);     for (int i = couples[num][0] + 1; i < couples[num][1]; i++)       if (forest[i] < 0) {         del++;         toDelete.push_back(i);       }     std::cout << max <<     << del << std::endl;     for (int i = 0; i < couples[num][0]; i++) std::cout << i + 1 <<    ;     for (std::vector<int>::iterator it = toDelete.begin(); it != toDelete.end();          it++)       std::cout << *(it) + 1 <<    ;     for (int i = couples[num][1] + 1; i < n; i++) std::cout << i + 1 <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x1 = 0, y1 = 0, x2 = 0, y2 = 0;     int n = 0;     cin >> x1 >> y1 >> x2 >> y2 >> n;     int count = 0;     for (int i = 0; i < n; ++i) {       int a = 0, b = 0, c = 0;       cin >> a >> b >> c;       int d1 = (a * 1LL * x1 + b * 1LL * y1 + c < 0 ? -1 : 1);       int d2 = (a * 1LL * x2 + b * 1LL * y2 + c < 0 ? -1 : 1);       if ((d1 * 1LL * d2) < 0) ++count;     }     cout << count;     return 0;   }


#include <bits/stdc++.h>      using namespace std;      #define fr first   #define sc second   #define pb push_back      const int INF = 1e9 + 1;   const int mod = 1e9+9;   const int base = 1e9;      int main() {       //freopen( input.txt ,  r , stdin);       ios_base::sync_with_stdio(false);       cin.tie(0); cout.tie(0);       int t;       cin>>t;       while(t--) {           int n, m;           cin>>n>>m;           vector<int> k(n), c(m);           for(int i = 0; i < n; i++) {               cin>>k[i];           }           sort(k.rbegin(), k.rend());           for(int i = 0; i < m; i++) {               cin>>c[i];           }           int64_t ans = 0;           int i = 0;           for(auto p : k) {               if(i < p) {                   ans += c[i];                   i++;               } else {                   ans += c[p-1];               }           }           cout<<ans<<  n ;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, K;   bool good[1050];   int main() {     scanf( %d , &N);     for (int i = 2; i <= N; i++) {       bool isp = true;       for (int j = 2; j < i; j++)         if (i % j == 0) {           isp = false;           int x = i;           while (x % j == 0) x /= j;           if (x == 1) good[i] = true;           break;         }       if (isp) good[i] = true;       if (good[i]) ++K;     }     printf( %d n , K);     for (int i = 2; i <= N; i++)       if (good[i]) printf( %d  , i);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long a, b;     cin >> a >> b;     if ((a + b) % 3 != 0) {       cout <<  NO  << endl;       return;     }     long long mi = min(a, b);     long long mx = max(a, b);     long long d = mx - mi;     if (mx > 2 * mi) {       cout <<  NO  << endl;     } else {       cout <<  YES  << endl;     }   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     long long t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   string t[100000];   char mm[100000];   int check[26];   int ans() {     int m = 0, c = 0;     for (int i = 0; i < 26; ++i) m = max(m, check[i]);     for (int i = 0; i < 26; ++i) {       if (check[i] == m) ++c;       if (c > 1) return 0;     }     return 1;   }   int main() {     int n, shock = 0, c = 0;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> mm[i] >> t[i];       if (mm[i] ==  !  or mm[i] ==  ? ) ++shock;     }     for (int i = 0; i < n - 1; ++i) {       if (mm[i] ==  . ) {         for (int j = 0; j < t[i].size(); ++j) check[t[i][j] -  a ] = -1;       }       if (mm[i] ==  ! ) {         ++c;         set<char> s;         for (int j = 0; j < t[i].size(); ++j) {           if (check[t[i][j] -  a ] != -1) {             if (s.find(t[i][j] -  a ) == s.end()) {               s.insert(t[i][j] -  a );               ++check[t[i][j] -  a ];             }           }         }       }       if (mm[i] ==  ? ) {         ++c;         check[t[i][0] -  a ] = -1;       }       if (ans()) {         cout << shock - c - 1;         return 0;       }     }     cout <<  0 ;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1E7;   int a[2][200], u, v, c[200], n;   bool check() {     for (int i = 0; i <= n; i++)       if (c[i] > 1 || c[i] < -1) return 0;     return 1;   }   void dfs(int x) {     if (x > n) {       cout << n << endl;       for (int i = 0; i <= n; i++) printf( %d  , a[u][i]);       printf(  n%d n , n - 1);       for (int i = 0; i < n; i++) printf( %d  , a[v][i]);       return;     }     for (int i = 1; i <= x; i++) c[i] = a[u][i - 1];     c[0] = 0;     for (int i = 0; i < x; i++) c[i] += a[v][i];     if (!check()) {       for (int i = 1; i <= x; i++) c[i] = a[u][i - 1];       c[0] = 0;       for (int i = 0; i < x; i++) c[i] -= a[v][i];     }     for (int i = 0; i <= x; i++) a[v][i] = c[i];     swap(u, v);     dfs(x + 1);   }   int main() {     cin >> n;     u = 0;     v = 1;     a[0][0] = 1;     dfs(1);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, arr[100], k;     set<int> todo;     cin >> n >> k;     for (int i = 0; i < n; i++) {       cin >> arr[i];       k += arr[i];       for (long long x = 1; x * x <= arr[i]; x++) {         todo.insert(x);         todo.insert(arr[i] / x);         todo.insert(arr[i] / x + 1);       }     }     long long best = 1;     auto nxtIt = todo.begin();     for (int i : todo) {       nxtIt++;       long long nxtVal = (nxtIt == todo.end()) ? 1LL << 55 : *nxtIt;       long long sum = 0;       for (int j = 0; j < n; j++) sum += (arr[j] + i - 1) / i;       if (sum * i <= k) best = max(best, min(nxtVal - 1, k / sum));     }     cout << best;   }


#include <bits/stdc++.h>   using namespace std;   string s[100001];   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> s[i];     }     long double x = 0;     long long ans = 0;     for (int i = n; i >= 1; i--) {       if (s[i] ==  halfplus ) {         x += 0.5;       }       ans += x * m;       x *= 2;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   const double eps = 1e-8;   const int INF = 1E9;   const int MAXN = 11111;   int n, maxc;   int l[MAXN], r[MAXN], b[MAXN];   int _less[MAXN];   int id[MAXN], mx;   double ans, dp[11][MAXN], sum[11][MAXN];   double p[11];   int main() {     cin >> n;     for (int i = 0; i < (int)(n); i++) {       cin >> l[i] >> r[i];       p[i] = 1.0 / (r[i] - l[i] + 1);     }     for (int i = 0; i < (int)(n); i++) id[i] = i;     ans = 0;     while (true) {       maxc = 1 << (n - 1);       for (int mask = 0; mask < (int)(maxc); mask++) {         for (int i = 0; i < (int)(n - 1); i++) _less[i] = (mask >> i) & 1;         bool ok = 1;         for (int i = 0; i < (int)(n - 1); i++)           if (_less[i] == 1 && id[i] > id[i + 1]) {             ok = 0;             break;           }         if (!ok) continue;         memset(dp, 0, sizeof(dp));         for (int i = l[id[n - 2]]; i <= r[id[n - 2]]; i++) {           dp[n - 2][i] = 1.0 * i;           if (_less[n - 2])             dp[n - 2][i] *= (l[id[n - 1]] <= i && i <= r[id[n - 1]] ? 1 : 0);           else             dp[n - 2][i] *= (i + 1 <= r[id[n - 1]]                                  ? (r[id[n - 1]] - max(l[id[n - 1]], i + 1) + 1)                                  : 0);         }         mx = r[id[n - 2]];         sum[n - 2][MAXN - 1] = dp[n - 2][MAXN - 1];         for (int i = (int)(MAXN - 1) - 1; i >= 0; i--)           sum[n - 2][i] = sum[n - 2][i + 1] + dp[n - 2][i];         for (int i = n - 3; i >= 0; i--) {           for (int j = l[id[i]]; j <= r[id[i]]; j++) {             if (_less[i])               dp[i][j] = dp[i + 1][j];             else {               dp[i][j] = sum[i + 1][j + 1];             }           }           sum[i][MAXN - 1] = dp[i][MAXN - 1];           for (int j = (int)(MAXN - 1) - 1; j >= 0; j--)             sum[i][j] = sum[i][j + 1] + dp[i][j];         }         ans += sum[0][0];       }       if (!next_permutation(id, id + n)) break;     }     cout.precision(40);     for (int i = 0; i < (int)(n); i++) ans *= p[i];     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     long long n;     cin >> n;     long long a[n];     long long sum = 0;     for (long long i = 0; i < n; i++) {       cin >> a[i];       sum += a[i];     }     long long req = ceil(4.5 * n) - sum;     vector<long long> b(n);     for (long long i = 0; i < n; i++) {       b[i] = 5 - a[i];     }     bool check = 0;     sort(b.begin(), b.end(), greater<long long>());     long long ans = 0;     for (long long i = 0; i < n; i++) {       if (req <= 0) {         cout << ans;         check = 1;         break;       }       req -= b[i];       ans++;     }     if (!check) cout << n;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 4e5 + 47, mod = 1e9 + 7, inf = 1e9 + 7;   long long n, m;   vector<long long> val;   void read() {     cin >> n;     val.resize(n);     for (long long i = 0; i < n; ++i) cin >> val[i];   }   vector<long long> fenw(MAXN, 0);   void update(long long pos) {     for (; pos < MAXN; pos = (pos | (pos + 1))) fenw[pos]++;   }   long long get_sum(long long l) {     long long res = 0;     for (; l >= 0; l = (l & (l + 1)) - 1) res += fenw[l];     return res;   }   bool comp(pair<long long, long long> &a, pair<long long, long long> &b) {     if (a.first < b.first) return false;     if (a.first > b.first) return true;     return a.second <= b.second;   }   void solve() {     cin >> m;     vector<vector<pair<long long, long long> > > quer(n + 1);     for (long long i = 0; i < m; ++i) {       long long x, y;       cin >> x >> y;       quer[x - 1].push_back({y, i});     }     vector<pair<long long, long long> > suck(n);     for (long long i = 0; i < n; ++i) suck[i] = {val[i], i};     sort(suck.begin(), suck.end(), comp);     vector<long long> ans(m, 0);     for (long long len = 0; len < n; ++len) {       update(suck[len].second);       for (pair<long long, long long> p : quer[len]) {         long long k = p.first;         long long l = 0, r = n;         while (l < r) {           long long mid = l + (r - l) / 2;           if (get_sum(mid) < k)             l = mid + 1;           else             r = mid;         }         ans[p.second] = val[l];       }     }     for (long long i = 0; i < m; ++i) cout << ans[i] <<    ;     cout << endl;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long t = 1;     while (t--) {       read();       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int v, w;     node() {}     node(int V, int W) {       v = V;       w = W;     }   };   vector<node> G[105];   int n, m;   int dp[105][105][30];   int dfs(int first, int second, int s) {     if (dp[first][second][s] != -1) return dp[first][second][s];     for (int i = 0; i < G[first].size(); i++) {       int v = G[first][i].v;       int w = G[first][i].w;       if (w < s) continue;       if (dfs(second, v, w) == 0) return dp[first][second][s] = 1;     }     return dp[first][second][s] = 0;   }   int main() {     while (~scanf( %d %d , &n, &m)) {       memset(dp, -1, sizeof(dp));       for (int i = 1; i <= n; i++) G[i].clear();       for (int i = 1; i <= m; i++) {         int u, v;         char s;         scanf( %d %d %c , &u, &v, &s);         G[u].push_back(node(v, s -  a ));       }       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= n; j++)           if (dfs(i, j, 0))             printf( A );           else             printf( B );         printf(  n );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a, i, c = 0, b;     cin >> n;     char name[15];     for (i = 0; i < n; i++) {       cin >> name;       cin >> b >> a;       if (b >= 2400 && a > b) c = 1;     }     if (c == 1)       cout <<  YES  << endl;     else       cout <<  NO  << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000001;   int main() {     ios::sync_with_stdio(false);     int n;     string s;     cin >> n >> s;     sort(s.begin(), s.end());     cout << s;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   const int MAXN = 100000;   const int MAXCAP = 300000;   typedef struct R {     int w, c;   } R;   bool operator<(const R &a, const R &b) {     return (long long)a.c * b.w > (long long)b.c * a.w;   }   int n, cap;   R r[MAXN];   multiset<int> have[4];   multiset<int> avail[4];   long long getlargest(multiset<int> &a, int cnt) {     if (((int)(a).size()) < cnt) return INT_MIN;     auto it = a.end();     long long ret = 0;     for (int i = (0); i < (cnt); ++i) {       --it;       ret += *it;     }     return ret;   }   long long getsmallest(multiset<int> &a, int cnt) {     if (((int)(a).size()) < cnt) return INT_MAX;     auto it = a.begin();     long long ret = 0;     for (int i = (0); i < (cnt); ++i) {       ret += *it;       ++it;     }     return ret;   }   void removeone(multiset<int> &a, int val) {     auto it = a.find(val);     if (it == a.end())       printf( err (%d) n , val);     else       a.erase(it);   }   void take(int cnt, int type) {     for (int i = (0); i < (cnt); ++i) {       long long cur = getlargest(avail[type], 1);       removeone(avail[type], cur);       have[type].insert(cur);     }   }   void remove(int cnt, int type) {     for (int i = (0); i < (cnt); ++i) {       long long cur = getsmallest(have[type], 1);       removeone(have[type], cur);       avail[type].insert(cur);     }   }   void run() {     scanf( %d%d , &n, &cap);     for (int i = (0); i < (n); ++i) scanf( %d%d , &r[i].w, &r[i].c);     sort(r, r + n);     int left = cap;     for (int i = (0); i < (4); ++i) have[i].clear(), avail[i].clear();     for (int i = (0); i < (n); ++i) avail[r[i].w].insert(r[i].c);     long long ret = 0;     for (int i = (0); i < (n); ++i) {       if (r[i].w > left) break;       left -= r[i].w;       ret += r[i].c;       have[r[i].w].insert(r[i].c);       removeone(avail[r[i].w], r[i].c);     }     while (left >= 1) {       long long best = 0;       int besttype = -1;       {         long long cur = getlargest(avail[1], 1);         if (cur > best) best = cur, besttype = 0;       }       {         long long cur = getlargest(avail[2], 1) - getsmallest(have[1], 1);         if (cur > best) best = cur, besttype = 1;       }       {         long long cur = getlargest(avail[2], 2) - getsmallest(have[3], 1);         if (cur > best) best = cur, besttype = 2;       }       {         long long cur = getlargest(avail[3], 1) - getsmallest(have[2], 1);         if (cur > best) best = cur, besttype = 3;       }       {         long long cur = getlargest(avail[3], 1) - getsmallest(have[1], 2);         if (cur > best) best = cur, besttype = 4;       }       if (besttype == -1) break;       if (besttype == 0)         take(1, 1);       else if (besttype == 1)         take(1, 2), remove(1, 1);       else if (besttype == 2)         take(2, 2), remove(1, 3);       else if (besttype == 3)         take(1, 3), remove(1, 2);       else if (besttype == 4)         take(1, 3), remove(2, 1);       ret += best;       --left;     }     while (left >= 2) {       long long best = 0;       int besttype = -1;       {         long long cur = getlargest(avail[2], 1);         if (cur > best) best = cur, besttype = 0;       }       {         long long cur = getlargest(avail[3], 1) - getsmallest(have[1], 1);         if (cur > best) best = cur, besttype = 1;       }       {         long long cur = getlargest(avail[3], 2) - getsmallest(have[2], 2);         if (cur > best) best = cur, besttype = 2;       }       if (besttype == -1) break;       if (besttype == 0)         take(1, 2);       else if (besttype == 1)         take(1, 3), remove(1, 1);       else if (besttype == 2)         take(2, 3), remove(2, 2);       ret += best;       --left;     }     printf( %lld n , ret);   }   int main() {     run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void print(long long a[], int n) {     cout << endl;     for (int i = 0; i < n; i++) cout << a[i] <<    ;     cout << endl;   }   const int N = 1111;   int db[2][N];   int main() {     int n, k, x;     scanf( %d%d%d , &n, &k, &x);     int minn = 1 << 30;     int maxx = -minn;     for (int i = 0; i < n; i++) {       int r;       scanf( %d , &r);       db[0][r]++;     }     int b = 0;     int p = 1;     for (int i = 0; i < k; i++) {       for (int j = 0; j < N; j++) db[p][j] = 0;       int c = 0;       for (int j = 0; j < N; j++) {         if (db[b][j]) {           int t = db[b][j];           if (c % 2 == 0) {             db[p][j] += t / 2;             db[p][j ^ x] += (t + 1) / 2;           } else {             db[p][j] += (t + 1) / 2;             db[p][j ^ x] += t / 2;           }           c += t;         }       }       swap(b, p);     }     for (int i = 0; i < N; i++) {       if (db[b][i]) {         minn = min(minn, i);         maxx = max(maxx, i);       }     }     cout << maxx <<     << minn << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2222, mod = 998244353;   int add(int x, int y) {     x += y;     return x >= mod ? x - mod : x;   }   int mul(int x, int y) { return 1ll * x * y % mod; }   int qpow(int a, int b) {     int ans = 1;     while (b) {       if (b & 1) ans = mul(ans, a);       a = mul(a, a), b >>= 1;     }     return ans;   }   int f[maxn][maxn], g[maxn], h[maxn], n, p, q, a, b, pp[maxn], pq[maxn],       cc[maxn];   int main() {     cin >> n >> a >> b;     p = mul(a, qpow(b, mod - 2));     q = add(1, mod - p);     for (int i = pp[0] = pq[0] = 1; i <= n; i++)       pp[i] = mul(pp[i - 1], p), pq[i] = mul(pq[i - 1], q);     f[0][0] = 1;     for (int i = 0; i < n; i++) {       for (int j = 0; j <= i; j++) {         f[i + 1][j + 1] = add(f[i + 1][j + 1], mul(f[i][j], pq[i - j]));         f[i + 1][j] = add(f[i + 1][j], mul(f[i][j], pp[j]));       }     }     g[1] = 1, g[2] = 0;     for (int i = 3; i <= n; i++) {       g[i] = 1;       for (int j = 1; j < i; j++) g[i] = add(g[i], mod - mul(g[j], f[i][j]));       cc[i] = 1ll * i * (i - 1) / 2 % mod;     }     cc[2] = 1;     h[1] = 0, h[2] = 1;     for (int i = 3; i <= n; i++) {       for (int j = 1; j < i; j++) {         h[i] =             add(h[i], mul(mul(f[i][j], g[j]),                           add(h[j], add(h[i - j], add(cc[i], mod - cc[i - j])))));       }       h[i] = add(h[i], mul(g[i], cc[i]));       h[i] = mul(h[i], qpow(add(1, mod - g[i]), mod - 2));     }     cout << h[n];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   vector<int> g[100005];   int siz[100005];   bool era[100005];   long long rans;   int col[100005];   int calc(int v, int p) {     int cnt = 1;     for (int i = 0; i < (int)g[v].size(); i++) {       int to = g[v][i];       if (to == p || era[to]) continue;       cnt += calc(to, v);     }     siz[v] = cnt;     return cnt;   }   pair<int, int> fnd(int v, int p, int t) {     pair<int, int> res = make_pair(1e9, -1);     int cnt = 1, mx = 0;     for (int i = 0; i < (int)g[v].size(); i++) {       int to = g[v][i];       if (to == p || era[to]) continue;       res = min(res, fnd(to, v, t));       cnt += siz[to];       mx = max(mx, siz[to]);     }     mx = max(mx, t - cnt);     res = min(res, make_pair(mx, v));     return res;   }   void solve(int v, int lev) {     calc(v, -1);     int s = fnd(v, -1, siz[v]).second;     era[s] = 1;     col[s] = lev;     for (int i = 0; i < (int)g[s].size(); i++) {       if (era[g[s][i]]) continue;       solve(g[s][i], lev + 1);     }     era[s] = 0;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n - 1; i++) {       int a, b;       scanf( %d %d , &a, &b);       g[a].push_back(b);       g[b].push_back(a);     }     solve(1, 1);     for (int i = 1; i <= n; i++)       if (col[i] > 26) {         puts( Impossible! );         return 0;       }     for (int i = 1; i <= n; i++) printf( %c  , col[i] - 1 +  A );     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> v(n);       for (int i = 0; i < n; i++) cin >> v[i];       long long ans = 0;       for (int i = 30; i >= 0; i--) {         vector<int> v1;         int cnt1 = 0, cnt2 = 0;         for (int x : v) {           if (x & (1 << i)) {             cnt1++;           } else {             cnt2++;             v1.push_back(x);           }         }         ans += (1LL * cnt1 * (cnt1 - 1));         int len = v.size();         while (len--) v.pop_back();         for (int x : v1) v.push_back(x);       }       cout << ans / 2 <<   n ;       ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int tree[4 * 200005], cs[200005], ar[200005];   struct dt {     int b, e, h, id, res;   } st[100005];   bool cmp1(dt x, dt y) { return x.b < y.b; }   bool cmp2(dt x, dt y) { return x.id < y.id; }   void update(int nd, int b, int e, int x, int v) {     if (b > x || e < x) return;     if (b == x && e == x) {       tree[nd] = v;       return;     }     int left = 2 * nd;     int right = 2 * nd + 1;     int md = (b + e) / 2;     update(left, b, md, x, v);     update(right, md + 1, e, x, v);     tree[nd] = max(tree[left], tree[right]);   }   int query(int nd, int b, int e, int x, int y) {     if (e < x || b > y) return 0;     if (b >= x && e <= y) return tree[nd];     int left = 2 * nd;     int right = 2 * nd + 1;     int md = (b + e) / 2;     int p1 = query(left, b, md, x, y);     int p2 = query(right, md + 1, e, x, y);     return max(p1, p2);   }   int main() {     int n;     scanf( %d , &n);     set<int> ss;     for (int i = 1; i <= n; i++) {       int b, h, e;       scanf( %d%d , &b, &h);       e = b + h - 1;       st[i].b = b;       st[i].e = e;       st[i].h = h;       st[i].id = i;       st[i].res = -1;       ss.insert(b);       ss.insert(e);     }     map<int, int> mp;     set<int>::iterator it;     int m = 0;     for (it = ss.begin(); it != ss.end(); it++) mp[*it] = ++m;     sort(st + 1, st + n + 1, cmp1);     int sz = ss.size();     for (int i = 1; i <= n; i++) {       int v = mp[st[i].b];       ar[v] = 1;       int u = mp[st[i].e];       update(1, 1, sz, v, u);     }     cs[0] = 0;     for (int i = 1; i <= sz; i++) cs[i] = cs[i - 1] + ar[i];     for (int i = n; i >= 1; i--) {       int b = mp[st[i].b];       int e = mp[st[i].e];       int p = query(1, 1, sz, b, e);       int res = cs[p] - cs[b - 1];       st[i].res = res;       update(1, 1, sz, b, p);     }     sort(st + 1, st + n + 1, cmp2);     for (int i = 1; i <= n; i++) {       if (i == n)         printf( %d n , st[i].res);       else         printf( %d  , st[i].res);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long modpow(long long a, long long n, long long temp) {     long long res = 1, y = a;     while (n > 0) {       if (n & 1) res = (res * y) % temp;       y = (y * y) % temp;       n /= 2;     }     return res % temp;   }   long long ison(long long mask, long long pos) { return (mask & (1 << pos)); }   long long cbit(long long n) {     long long k = 0;     while (n) n &= (n - 1), k++;     return k;   }   long long nbit(long long n) {     long long k = 0;     while (n) n /= 2, k++;     return k;   }   long long mod = 1e9 + 7;   int sgn(long long x) { return x < 0 ? -1 : !!x; }   long long c[200005];   long long d[200005];   long long bit[500005], n, bit2[500005];   void upd(long long ind, long long val) {     if (ind == 0) return;     while (ind <= n) {       bit[ind] += val;       ind += (ind & -ind);     }   }   long long qu(long long ind) {     long long k = 0;     while (ind > 0) {       k += bit[ind];       ind -= (ind & -ind);     }     return k;   }   void upd2(long long ind, long long val) {     if (ind == 0) return;     while (ind <= n) {       bit2[ind] += val;       ind += (ind & -ind);     }   }   long long qu2(long long ind) {     long long k = 0;     while (ind > 0) {       k += bit2[ind];       ind -= (ind & -ind);     }     return k;   }   long long xo(long long i) {     if ((i & 3) == 0) return i;     if ((i & 3) == 1) return 1;     if ((i & 3) == 2) return i + 1;     return 0;   }   int main() {     long long k, a, b, l, r, t, i, q;     cin >> n >> k >> a >> b >> q;     while (q--) {       cin >> t;       if (t == 1) {         long long d1, d2;         cin >> d1 >> d2;         long long x1 = min(b, c[d1] + d2);         long long x2 = min(a, d[d1] + d2);         upd(d1, x1 - c[d1]);         upd2(d1, x2 - d[d1]);         c[d1] = x1;         d[d1] = x2;       } else {         long long r1;         cin >> r1;         long long ans = 0;         ans += qu(r1 - 1);         ans += (qu2(n) - qu2(r1 + k - 1));         cout << ans << endl;       }     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:16777216 )   using namespace std;   struct post {     int pos[3];     int st[3];     int mv;   };   inline int code(const post& p) {     int q = p.mv;     for (int _n((3) - 1), i(0); i <= _n; i++) q = q * 44 + p.pos[i];     for (int _n((3) - 1), i(0); i <= _n; i++) q = q * 5 + p.st[i];     return q;   }   inline post decode(int q) {     post p;     for (int i = (2), _b = (0); i >= _b; i--) {       p.st[i] = q % 5;       q /= 5;     }     for (int i = (2), _b = (0); i >= _b; i--) {       p.pos[i] = q % 44;       q /= 44;     }     p.mv = q;     return p;   }   int a, b, c, d, i, j, n, m, k;   int ans = 0;   bool used[100000001];   int fm[3], ft[3];   queue<int> q;   inline void add(const post& p) {     for (int _n((3) - 1), i(0); i <= _n; i++) {       if (p.pos[i] < 0) return;       if (p.pos[i] > ans) ans = p.pos[i];     }     int qq = code(p);     if (used[qq]) return;     used[qq] = 1;     q.push(qq);   }   int main() {     post p;     memset((p.pos), 0, sizeof(p.pos));     memset((p.st), 0, sizeof(p.st));     p.mv = 0;     for (int _n((3) - 1), i(0); i <= _n; i++) {       scanf( %d%d%d , &p.pos[i], &fm[i], &ft[i]);       p.pos[i] += 2;     }     add(p);     while (!q.empty()) {       post cur = decode(q.front());       q.pop();       for (int _n((3) - 1), i(0); i <= _n; i++) {         if (cur.mv & 1 << i) continue;         if (cur.st[i] != 0 && cur.st[i] != 4) continue;         bool ok = 1;         for (int _n((3) - 1), j(0); j <= _n; j++) {           if (cur.st[j] == i + 1) {             ok = 0;             break;           }         }         if (!ok) continue;         for (int _n(fm[i]), dx(-fm[i]); dx <= _n; dx++) {           if (!dx) continue;           post nx = cur;           nx.pos[i] += dx;           int o = 0;           for (int _n((3) - 1), z(0); z <= _n; z++) {             if (nx.pos[z] == nx.pos[i]) ++o;           }           if (o > 1) continue;           nx.mv |= 1 << i;           add(nx);         }       }       for (int _n((3) - 1), i(0); i <= _n; i++) {         if (cur.st[i] != 0) continue;         bool ok = 1;         for (int _n((3) - 1), j(0); j <= _n; j++) {           if (cur.st[j] == i + 1) {             ok = 0;             break;           }         }         if (!ok) continue;         for (int _n((3) - 1), j(0); j <= _n; j++) {           if (i == j) continue;           if (cur.pos[j] != cur.pos[i] + 1 && cur.pos[j] != cur.pos[i] - 1)             continue;           bool ok = 1;           for (int _n((3) - 1), z(0); z <= _n; z++) {             if (cur.st[z] == j + 1) {               ok = 0;               break;             }           }           if (!ok) continue;           post nx = cur;           nx.st[i] = j + 1;           nx.pos[j] = nx.pos[i];           if (nx.st[j] != 0 && nx.st[j] != 4) {             for (int _n((3) - 1), z(0); z <= _n; z++) nx.pos[z] = cur.pos[i];           }           add(nx);         }       }       for (int _n((3) - 1), i(0); i <= _n; i++) {         if (cur.st[i] == 0 || cur.st[i] == 4) continue;         bool ok = 1;         for (int _n((3) - 1), j(0); j <= _n; j++) {           if (cur.st[j] == i + 1) {             ok = 0;             break;           }         }         if (!ok) continue;         for (int _n(ft[i]), dx(-ft[i]); dx <= _n; dx++) {           if (!dx) continue;           bool ok = 1;           for (int _n((3) - 1), z(0); z <= _n; z++) {             if (cur.pos[z] == dx + cur.pos[i]) continue;           }           post nx = cur;           int pt = cur.st[i] - 1;           nx.pos[pt] = cur.pos[i] + dx;           if (cur.st[pt] > 0 && cur.st[pt] < 4)             nx.pos[cur.st[pt] - 1] = cur.pos[i] + dx;           nx.st[i] = 4;           add(nx);         }       }     }     printf( %d n , ans - 2);   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   char arr[55][55];   int dp[55][55][55][55];   int f(int ind, int let, int dig, int sim) {     if (ind == n) {       if (let && dig && sim)         return 0;       else         return 1e9;     }     if (dp[ind][let][dig][sim] != -1) return dp[ind][let][dig][sim];     int &ret = dp[ind][let][dig][sim];     ret = 1e9;     for (int i = 0; i < m; i++) {       if (arr[ind][i] >=  a  && arr[ind][i] <=  z )         ret = min(ret, f(ind + 1, let + 1, dig, sim) + min(i, m - i));       else if (arr[ind][i] >=  0  && arr[ind][i] <=  9 )         ret = min(ret, f(ind + 1, let, dig + 1, sim) + min(i, m - i));       else         ret = min(ret, f(ind + 1, let, dig, sim + 1) + min(i, m - i));     }     return ret;   }   int main() {     while (scanf( %d %d , &n, &m) == 2) {       memset(dp, -1, sizeof dp);       for (int i = 0; i < n; i++) scanf( %s , &arr[i]);       int ans = f(0, 0, 0, 0);       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[500005];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n, i, j, k, l, m;     long long t;     cin >> t;     long long pret = t;     while (t--) {       cin >> n >> k;       for (i = 1; i <= n; i++) cin >> a[i];       int flag1 = 0;       int flag2 = 0;       for (i = 1; i <= n; i++) {         if (a[i] == k) {           flag1 = 1;         }         if (a[i] != k) {           flag2 = 1;         }       }       if (flag1 == 0) {         cout <<  no n ;         continue;       }       if (flag2 == 0) {         cout <<  yes n ;         continue;       }       a[n + 1] = 0;       int flag = 0;       for (i = 1; i <= n - 1; i++) {         if (min(a[i], a[i + 1]) >= k) {           flag = 1;           break;         }       }       for (i = 2; i <= n - 1; i++) {         vector<long long> v;         v.push_back(a[i - 1]);         v.push_back(a[i + 1]);         v.push_back(a[i]);         sort(v.begin(), v.end());         if (v[1] >= k) flag = 1;       }       if (flag)         cout <<  yes n ;       else         cout <<  no n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   const int maxt = (1 << 19) + 20;   vector<pair<pair<int, int>, int> > ad[maxn];   vector<pair<pair<int, int>, int> > qs[maxn];   set<pair<int, int> > st;   set<pair<int, int> >::iterator it1, it2, it3;   int n, q;   long long val[maxt], taga[maxt], tagb[maxt];   void update(int x) {     val[x] = val[x * 2] + val[x * 2 + 1];     if (val[x] >= 998244353) val[x] -= 998244353;   }   void build(int x, int l, int r) {     val[x] = tagb[x] = 0;     taga[x] = 1;     if (l == r) return;     int mid = (l + r) >> 1;     build(x * 2, l, mid);     build(x * 2 + 1, mid + 1, r);   }   void taketag(int x, int l, int r) {     if (l == r) {       taga[x] = 1;       tagb[x] = 0;       return;     }     int mid = (l + r) >> 1;     val[x * 2] = (val[x * 2] * taga[x]) % 998244353;     val[x * 2] = (val[x * 2] + tagb[x] * (mid - l + 1)) % 998244353;     val[x * 2 + 1] = (val[x * 2 + 1] * taga[x]) % 998244353;     val[x * 2 + 1] = (val[x * 2 + 1] + tagb[x] * (r - mid)) % 998244353;     taga[x * 2] = (taga[x * 2] * taga[x]) % 998244353;     tagb[x * 2] = (tagb[x * 2] * taga[x]) % 998244353;     tagb[x * 2] = tagb[x * 2] + tagb[x];     if (tagb[x * 2] >= 998244353) tagb[x * 2] -= 998244353;     taga[x * 2 + 1] = (taga[x * 2 + 1] * taga[x]) % 998244353;     tagb[x * 2 + 1] = (tagb[x * 2 + 1] * taga[x]) % 998244353;     tagb[x * 2 + 1] = tagb[x * 2 + 1] + tagb[x];     if (tagb[x * 2 + 1] >= 998244353) tagb[x * 2 + 1] -= 998244353;     taga[x] = 1;     tagb[x] = 0;   }   void modify(int x, int l, int r, int tl, int tr, long long a, long long b) {     if (l > tr || r < tl) return;     if (tl <= l && r <= tr) {       val[x] = (val[x] * a) % 998244353;       val[x] = (val[x] + b * (r - l + 1)) % 998244353;       taga[x] = (taga[x] * a) % 998244353;       tagb[x] = (tagb[x] * a + b) % 998244353;       taketag(x, l, r);       return;     }     taketag(x, l, r);     int mid = (l + r) >> 1;     modify(x * 2, l, mid, tl, tr, a, b);     modify(x * 2 + 1, mid + 1, r, tl, tr, a, b);     update(x);   }   long long query(int x, int l, int r, int tl, int tr) {     if (l > tr || r < tl) return 0;     if (tl <= l && r <= tr) return val[x];     taketag(x, l, r);     int mid = (l + r) >> 1;     long long ans =         (query(x * 2, l, mid, tl, tr) + query(x * 2 + 1, mid + 1, r, tl, tr)) %         998244353;     return ans;   }   int main() {     scanf( %d%d , &n, &q);     for (int i = 1; i <= q; i++) {       int t, l, r, x;       scanf( %d%d%d , &t, &l, &r);       if (t == 1) {         scanf( %d , &x);         ad[x].push_back(make_pair(make_pair(l, r), i));       } else         qs[i].push_back(make_pair(make_pair(l, r), 0));     }     for (int i = 1; i <= n; i++)       if (!ad[i].empty()) {         st.clear();         st.insert(make_pair(1, 0));         st.insert(make_pair(n, 1));         for (int j = 0; j < ad[i].size(); j++) {           int l = ad[i][j].first.first;           int r = ad[i][j].first.second;           if (st.empty()) {             qs[ad[i][j].second].push_back(make_pair(ad[i][j].first, 2));           } else {             it1 = st.lower_bound(make_pair(l, -1));             if ((*it1).second == 1) {               st.insert(make_pair(l - 1, 1));             }             int pre = l;             bool beg = 0, ed = 0;             for (it2 = it1; it2 != st.end();) {               int cur = (*it2).first;               int op = (*it2).second;               if (cur > r) break;               if (op == 0) {                 if (pre <= cur - 1) {                   qs[ad[i][j].second].push_back(                       make_pair(make_pair(pre, cur - 1), 2));                 }                 pre = cur;               } else {                 qs[ad[i][j].second].push_back(make_pair(make_pair(pre, cur), 1));                 pre = cur + 1;               }               it1 = it2;               ++it2;               st.erase(it1);             }             if (pre <= r) {               int op = 0;               if (it2 != st.end()) op = (*it2).second;               if (op == 0) {                 if (pre <= r) {                   qs[ad[i][j].second].push_back(make_pair(make_pair(pre, r), 2));                 }               } else {                 qs[ad[i][j].second].push_back(make_pair(make_pair(pre, r), 1));                 st.insert(make_pair(r + 1, 0));               }             }           }         }       }     build(1, 1, n);     for (int i = 1; i <= q; i++)       for (int j = 0; j < qs[i].size(); j++) {         if (qs[i][j].second == 0) {           printf( %I64d n ,                  query(1, 1, n, qs[i][j].first.first, qs[i][j].first.second));         } else if (qs[i][j].second == 1) {           modify(1, 1, n, qs[i][j].first.first, qs[i][j].first.second, 1, 1);         } else {           modify(1, 1, n, qs[i][j].first.first, qs[i][j].first.second, 2, 0);         }       }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 110000;   const double EPS = 1E-7;   struct pt {     int x, y;   };   int n;   pt p[MAXN];   int l[MAXN], r[MAXN];   long long r_sums[MAXN];   inline long long vec(pt a, pt b, pt c) {     return (b.x - a.x) * 1ll * (c.y - a.y) - (b.y - a.y) * 1ll * (c.x - a.x);   }   inline bool between(pt p, pt a, pt b, pt q) {     return vec(p, a, q) > 0 && vec(p, q, b) > 0;   }   inline long long sum_r(int lt, int rt) {     long long res = r_sums[rt];     if (lt) res -= r_sums[lt - 1];     return res;   }   long long solve(pt q) {     int j = 0;     for (int i = 0; i < n; ++i) {       j = max(j, i + 1);       while (j + 1 < n && !between(p[i], p[i + 1], p[j + 1], q)) ++j;       r[i] = j;       l[i] = j + 1;     }     for (int i = 0; i < n; ++i) {       r_sums[i] = 0;       if (i) r_sums[i] += r_sums[i - 1];       r_sums[i] += r[i];     }     long long ans = 0;     j = 0;     for (int i = 0; i < n; ++i) {       while (j < n && r[j] < l[i]) ++j;       if (l[i] < n && j < n) {         int left = j, right = r[i];         if (left > right) continue;         ans += sum_r(left, right);         ans -= (right - left + 1) * 1ll * (l[i] - 1);       }     }     return ans;   }   bool inside(pt q) {     bool res = false;     for (int i = 0; i < n; ++i) {       pt a = p[i], b = p[(i + 1) % n];       if (min(a.y, b.y) <= q.y && max(a.y, b.y) >= q.y) {         if (q.y != a.y && q.y != b.y) {           double cx = a.x + (b.x - a.x) * ((q.y - a.y) * 1. / (b.y - a.y));           if (cx >= q.x - EPS) res ^= true;         } else {           if (a.y == q.y && a.x > q.x || b.y == q.y && b.x > q.x)             if (max(a.y, b.y) > q.y) res ^= true;         }       }     }     return res;   }   int main() {     cin >> n;     for (int i = 0; i < n; ++i) scanf( %d%d , &p[i].x, &p[i].y);     reverse(p, p + n);     int ts;     cin >> ts;     for (int tt = 0; tt < ts; ++tt) {       pt q;       scanf( %d%d , &q.x, &q.y);       long long ans = 0;       if (inside(q)) ans = solve(q);       printf( %I64d n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   template <class X, class Y>   bool minimize(X &x, const Y &y) {     X eps = 1e-9;     if (x > y + eps) {       x = y;       return true;     } else       return false;   }   template <class X, class Y>   bool maximize(X &x, const Y &y) {     X eps = 1e-9;     if (x + eps < y) {       x = y;       return true;     } else       return false;   }   template <class T>   T Abs(const T &x) {     return (x < 0 ? -x : x);   }   const int BASE = (1LL << (8));   struct Number {     unsigned char d[3];     Number(int x = 0) {       for (int i = 0, _n = (3); i < _n; i++) {         d[i] = x & 255;         x >>= 8;       }     }     Number operator+(const Number &x) const {       Number res;       int rem = 0;       for (int i = 0, _n = (3); i < _n; i++) {         int tmp = rem + d[i] + x.d[i];         res.d[i] = tmp & 255;         rem = tmp > 255;       }       return res;     }     Number operator-(const Number &x) const {       Number res;       int rem = 0;       for (int i = 0, _n = (3); i < _n; i++) {         int tmp = d[i] - rem - x.d[i];         res.d[i] = tmp < 0 ? tmp + BASE : tmp;         rem = tmp < 0;       }       return res;     }     bool operator==(const Number &x) const {       for (int i = 0, _n = (3); i < _n; i++)         if (d[i] != x.d[i]) return false;       return true;     }     int value(void) const {       int res = 0;       for (int i = (2), _a = (0); i >= _a; i--) res = (res << 8) ^ d[i];       return res;     }   };   long long value[1000100], asked;   int ___left[1000100], ___right[1000100], n;   Number numZero[62][1000100];   void loadTree(void) {     cin >> n >> asked;     for (int i = (2), _b = (n); i <= _b; i++) {       int p;       long long w;       cin >> p >> w;       value[i] = value[p] ^ w;     }   }   void prepare(void) {     sort(value + 1, value + n + 1);     for (int j = 0, _n = (62); j < _n; j++)       for (int i = (1), _b = (n); i <= _b; i++) {         numZero[j][i] = numZero[j][i - 1];         if (!(((value[i]) >> (j)) & 1)) numZero[j][i] = numZero[j][i] + 1;       }     for (int i = (1), _b = (n); i <= _b; i++) ___right[i] = n;   }   void process(void) {     long long res = 0;     for (int pos = (62 - 1), _a = (0); pos >= _a; pos--) {       long long total = 0;       for (int i = (1), _b = (n); i <= _b; i++) {         int bit = (((value[i]) >> (pos)) & 1);         int zero = (numZero[pos][___right[i]] - numZero[pos][___left[i]]).value();         int need = bit == 0 ? zero : ___right[i] - ___left[i] - zero;         total += need;         if (total > asked) break;       }       int newBit = total >= asked ? 0 : 1;       if (asked > total) asked -= total;       res |= (1LL * newBit) << pos;       for (int i = (1), _b = (n); i <= _b; i++) {         int need = newBit ^ (((value[i]) >> (pos)) & 1);         int zero = (numZero[pos][___right[i]] - numZero[pos][___left[i]]).value();         if (need == 1)           ___left[i] += zero;         else           ___right[i] = ___left[i] + zero;       }     }     cout << res << endl;   }   int main(void) {     ios::sync_with_stdio(false);     cin.tie(NULL);     loadTree();     prepare();     process();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long i, j, k, r, c, c1, c2, cnt, ans, x, y, p, t;     vector<vector<long long>> v, a, b;     unordered_map<long long, set<pair<long long, long long>>> m;     vector<long long> re, q;     cin >> t;     i = 0;     while (i < t) {       cin >> r >> c;       j = 0;       while (j < r) {         p = 0;         while (p < c) {           cin >> k;           q.push_back(0);           re.push_back(k);           p++;         }         a.push_back(q);         b.push_back(q);         q.clear();         v.push_back(re);         re.clear();         j++;       }       j = 0;       a[0][0] = 0;       m[0].insert(make_pair(0, 0));       while (j < r) {         p = 0;         while (p < c) {           if (j + 1 < r) {             a[j + 1][p] = 1 + a[j][p];             m[1 + a[j][p]].insert(make_pair(j + 1, p));           }           if (p + 1 < c) {             a[j][p + 1] = 1 + a[j][p];             m[1 + a[j][p]].insert(make_pair(j, p + 1));           }           p++;         }         j++;       }       j = r - 1;       b[r - 1][c - 1] = 0;       m[0].insert(make_pair(r - 1, c - 1));       while (j >= 0) {         p = c - 1;         while (p >= 0) {           if (j - 1 >= 0) {             b[j - 1][p] = 1 + b[j][p];             m[1 + b[j][p]].insert(make_pair(j - 1, p));           }           if (p - 1 >= 0) {             b[j][p - 1] = 1 + b[j][p];             m[1 + b[j][p]].insert(make_pair(j, p - 1));           }           p--;         }         j--;       }       unordered_map<long long, set<pair<long long, long long>>>::iterator it;       set<pair<long long, long long>>::iterator u;       set<pair<long long, long long>> s;       it = m.begin();       cnt = 0;       if ((r + c - 2) % 2 == 0) {         k = (r + c - 2) / 2;       }       while (it != m.end()) {         c1 = 0;         c2 = 0;         if (it->first != k) {           s = it->second;           u = s.begin();           while (u != s.end()) {             if (v[(*u).first][(*u).second] == 0) {               c1++;             } else {               c2++;             }             u++;           }           if (c1 <= c2) {             u = s.begin();             while (u != s.end()) {               if (v[(*u).first][(*u).second] == 0) {                 v[(*u).first][(*u).second] = 1;               }               u++;             }             cnt = cnt + c1;           } else {             u = s.begin();             while (u != s.end()) {               if (v[(*u).first][(*u).second] == 1) {                 v[(*u).first][(*u).second] = 0;               }               u++;             }             cnt = cnt + c2;           }         }         it++;       }       cout << cnt << endl;       v.clear();       a.clear();       b.clear();       re.clear();       m.clear();       q.clear();       i++;     }     return (0);   }


#include <bits/stdc++.h>   using namespace std;   int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};   int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};   template <class T>   inline T biton(T n, T pos) {     return n | ((T)1 << pos);   }   template <class T>   inline T bitoff(T n, T pos) {     return n & ~((T)1 << pos);   }   template <class T>   inline T ison(T n, T pos) {     return (bool)(n & ((T)1 << pos));   }   template <class T>   inline T gcd(T a, T b) {     while (b) {       a %= b;       swap(a, b);     }     return a;   }   template <typename T>   string NumberToString(T Number) {     ostringstream second;     second << Number;     return second.str();   }   inline int nxt() {     int aaa;     scanf( %d , &aaa);     return aaa;   }   inline long long int lxt() {     long long int aaa;     scanf( %lld , &aaa);     return aaa;   }   inline double dxt() {     double aaa;     scanf( %lf , &aaa);     return aaa;   }   template <class T>   inline T bigmod(T p, T e, T m) {     T ret = 1;     for (; e > 0; e >>= 1) {       if (e & 1) ret = (ret * p) % m;       p = (p * p) % m;     }     return (T)ret;   }   int ar[1000010];   vector<int> v;   vector<pair<int, int> > edge;   int cnt[1000010];   int mark[1000010];   set<int> st;   int get(int x) {     for (int i = x - 1; i >= 1; i--) {       if (st.find(i) == st.end() && !mark[i]) {         mark[i] = 1;         return i;       }     }     return -1;   }   int main() {     int n = nxt();     for (int i = 1; i < n; i++) {       int a = nxt();       int b = nxt();       if (a > b) swap(a, b);       if (b != n) {         cout <<  NO  << endl;         return 0;       }       if (a == b && b == n) {         cout <<  NO  << endl;         return 0;       }       st.insert(a);       cnt[a]++;       if (cnt[a] == 0) v.push_back(a);     }     for (int i = n - 1; i >= 1; i--) {       vector<int> ok;       if (cnt[i]) {         ok.push_back(i);         cnt[i]--;         while (cnt[i]--) {           int val = get(i);           if (val == -1) {             printf( NO n );             return 0;           }           ok.push_back(val);         }         sort(ok.begin(), ok.end());         for (int i = 1; i < ok.size(); i++) {           edge.push_back(make_pair(ok[i], ok[i - 1]));         }         edge.push_back(make_pair(n, ok[0]));       }     }     if (edge.size() != n - 1) {       printf( NO n );       return 0;     }     printf( YES n );     for (auto it : edge) {       printf( %d %d n , it.first, it.second);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool check(long long x, long long y, long long x1, long long y1) {     if (x != 0 && y != 0 && x1 % x == 0 && y1 % y == 0 && x1 / x >= 0 &&         y1 / y >= 0 && x1 / x == y1 / y)       return 1;     if (x == 0 && y == 0 && y1 == 0 && x1 == 0) return 1;     if (x == 0 && y != 0 && x1 == 0 && y1 % y == 0 && y1 / y >= 0) return 1;     if (x != 0 && y == 0 && y1 == 0 && x1 % x == 0 && x1 / x >= 0) return 1;     return 0;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long a, b;     cin >> a >> b;     string s;     cin >> s;     long long a1 = 0, b1 = 0;     for (int i = 0; i < s.size(); i++) {       if (s[i] ==  U )         b1++;       else if (s[i] ==  D )         b1--;       else if (s[i] ==  R )         a1++;       else         a1--;     }     for (int i = 0; i < s.size(); i++) {       long long a2 = a, b2 = b;       for (int j = i; j >= 0; j--) {         if (s[j] ==  U )           b2--;         else if (s[j] ==  D )           b2++;         else if (s[j] ==  R )           a2--;         else           a2++;       }       if (check(a1, b1, a2, b2)) {         cout <<  Yes  << endl;         return 0;       }     }     if (a == 0 && b == 0)       cout <<  Yes  << endl;     else       cout <<  No  << endl;   }


#include <iostream>   #include <algorithm>   #include <vector>   #include <cmath>      using namespace std;         int main()   {       int t = 0;          cin>>t;       for(int i = 0; i < t; i++)       {           int n = 0;           cin>>n;           int v[100] = {0};           for(int j = 0; j < n; j++) cin>>v[j];              if(v[0] == n && v[n-1] == 1)           {               cout<<3<<  n ;              }           else if(v[0] != 1 && v[n-1] != n)           {               cout<<2<<  n ;           }           else if(v[0] == 1 && v[n-1] != n)           {               cout<<1<<  n ;           }           else if(v[0] != 1 && v[n-1] == n)           {               cout<<1<<  n ;           }           else           {               bool d = false;               for(int j = 0; j < n; j++)               {                   if(v[j] != j+1)                   {                       d = true;                       break;                   }               }               if(!d) cout<<0<<   ;               else cout<<1<<   ;           }       }          return 0;   }


#include <bits/stdc++.h>   template <typename T>   inline void read(T &x) {     bool b = 0;     char c;     while (!isdigit(c = getchar()) && c !=  - )       ;     if (c ==  - ) c = getchar(), b = 1;     x = c - 48;     while (isdigit(c = getchar())) x = x * 10 + c - 48;     if (b) x = -x;   }   void reads(std ::string &s) {     char c;     while (!isalpha(c = getchar()))       ;     s += c;     while (isalpha(c = getchar())) s += c;   }   using namespace std;   const int N = 1e5 + 11;   int m;   long long n, f[N], ans[50], last[50];   string s;   int main() {     ios_base::sync_with_stdio(0);     cout.tie(0);     if (fopen( m                 .inp ,                r )) {       freopen(            m             .inp ,            r , stdin);     }     read(m);     long long lastl = 0, lastr = 0;     int chl, chr;     for (int ii = 1; ii <= m; ii++) {       s =  # ;       reads(s);       n = s.size() - 1;       for (int i = 0; i < 26; i++) ans[i] = 0;       for (int i = 1; i <= n; i++) {         if (s[i] == s[i - 1])           f[i] = f[i - 1];         else           f[i] = 0;         f[i]++;         ans[s[i] -  a ] = max(ans[s[i] -  a ], f[i]);       }       long long l = 1, r = f[n];       for (int i = 2; i <= n; i++) {         if (f[i] == 1) break;         l = f[i];       }       if (s[1] == s[n] && last[s[1] -  a ] > 0)         ans[s[1] -  a ] = l + r + 1;       else         ans[s[1] -  a ] = l + (last[s[1] -  a ] > 0),                    ans[s[n] -  a ] = r + (last[s[n] -  a ] > 0);       if (l == n) ans[s[1] -  a ] = last[s[1] -  a ] + (last[s[1] -  a ] + 1) * l;       if (s[1] == chl) l = lastl + (lastl + 1) * l;       if (s[n] == chr) r = lastr + (lastr + 1) * r;       for (int i = 0; i < 26; i++) {         int ooooo = 0;         if (last[i]) ooooo = 1;         last[i] = ans[i];         if (!last[i]) last[i] = ooooo;       }       chl = s[1] -  a ;       chr = s[n] -  a ;       lastl = l;       lastr = r;     }     long long res = max(lastl, lastr);     for (int i = 0; i < 26; i++) res = max(res, last[i]);     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e3 + 7;   const long long mod = 998244353;   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());   int n, s, d;   unsigned long long ok[N];   unsigned long long d1[(1 << 20)], d2[(1 << 10)], a[N];   unordered_map<unsigned long long, int> m;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     for (int i = 0; i < N; i++) ok[i] = rng();     cin >> n >> s >> d;     for (int i = 0; i < s; i++) {       int len;       cin >> len;       for (int j = 1; j <= len; j++) {         int x;         cin >> x;         a[i] ^= ok[x];       }     }     int c = min(20, s);     for (int i = 0; i < c; i++) {       for (int j = 0; j < (1 << i); j++) {         d1[(1 << i) + j] = (d1[j] ^ a[i]);       }     }     for (int i = 0; i < s - c; i++) {       for (int j = 0; j < (1 << i); j++) {         d2[j + (1 << i)] = (d2[j] ^ a[i + c]);       }     }     for (int i = 0; i < (1 << c); i++) {       unsigned long long res = d1[i];       if (m.find(res) == m.end()) {         m[res] = __builtin_popcount(i);       } else {         m[res] = min(m[res], __builtin_popcount(i));       }     }     for (int i = 1; i <= d; i++) {       int len;       cin >> len;       unsigned long long a = 0;       for (int j = 1; j <= len; j++) {         int x;         cin >> x;         a ^= ok[x];       }       int ans = 1e9;       for (int j = 0; j < (1 << s - c); j++) {         auto c = (a ^ d2[j]);         if (m.find(c) != m.end()) {           ans = min(ans, m[c] + __builtin_popcount(j));         }       }       cout << (ans == 1e9 ? -1 : ans) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 20;   int n, x, y, a[maxn], o = 0;   int main() {     cin >> n >> x >> y;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) {       for (int j = max(0, i + x - 1); j < i; j++) {         if (a[j] < a[i]) {           o = 1;           break;         }       }       for (int j = i + 1; j < min(y + i + 1, n); j++) {         if (a[j] < a[i]) {           o = 1;           break;         }       }       if (o == 0) {         cout << i + 1 << endl;         return 0;       }       o = 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {     return (a.first > b.first);   }   bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {     return (a.second < b.second);   }   long long int power(long long int x, long long int y, long long int p) {     long long int res = 1;     x = x % p;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     double x1, x2, y1, y2, r;     cin >> r >> x1 >> y1 >> x2 >> y2;     double d = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));     double a = d / (2 * r);     int b;     if ((int)a * 2 * r == d)       b = 0;     else       b = 1;     if (b == 0)       cout << (int)a;     else       cout << (int)a + 1;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e3;   int n, h, aa[N + 2], vis[2][N + 1][N + 1][2];   double p, dp[2][N + 1][N + 1][2];   double dfs(int s1, int lo, int hi, int s2) {     if (vis[s1][lo][hi][s2]) return dp[s1][lo][hi][s2];     vis[s1][lo][hi][s2] = 1;     if (lo > hi) return 0;     double here = 0;     int em;     if (s1 == 0)       em = min(aa[lo] - aa[lo - 1], h);     else       em = min(aa[lo] - aa[lo - 1] - h, h);     here += 0.5 * p * em + 0.5 * p * dfs(0, lo + 1, hi, s2);     em = aa[lo] + h;     int i;     for (i = lo + 1; i <= hi; i++) {       if (aa[i] >= em) break;       em = aa[i] + h;     }     if (i > hi) {       if (s2 == 1)         em = min(em, aa[hi + 1]);       else         em = min(em, aa[hi + 1] - h);     }     em -= aa[lo];     here += 0.5 * (1.0 - p) * em + 0.5 * (1.0 - p) * dfs(1, i, hi, s2);     if (s2 == 1)       em = min(aa[hi + 1] - aa[hi], h);     else       em = min(aa[hi + 1] - aa[hi] - h, h);     here += 0.5 * (1.0 - p) * em + 0.5 * (1.0 - p) * dfs(s1, lo, hi - 1, 1);     em = aa[hi] - h;     for (i = hi - 1; i >= lo; i--) {       if (aa[i] <= em) break;       em = aa[i] - h;     }     if (i < lo) {       if (s1 == 0)         em = max(em, aa[lo - 1]);       else         em = max(em, aa[lo - 1] + h);     }     em = aa[hi] - em;     here += 0.5 * p * em + 0.5 * p * dfs(s1, lo, i, 0);     return dp[s1][lo][hi][s2] = here;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> h >> p;     for (int i = 1; i <= n; i++) cin >> aa[i];     sort(aa + 1, aa + n + 1);     aa[0] = -1e9, aa[n + 1] = 1e9;     cout << setprecision(9) << fixed << dfs(0, 1, n, 1) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int n1, n2, n3 = 0;     bool flagm = true, flagr = true;     while (n--) {       cin >> n1 >> n2;       if (n3 < n2 && n3 != 0) flagm = false;       if (n1 != n2) flagr = false;       n3 = n2;     }     if (!flagr)       cout <<  rated ;     else if (flagr && !flagm)       cout <<  unrated ;     else       cout <<  maybe ;   }


#include <bits/stdc++.h>   using namespace std;   int k[3], t[3];   int n;   long long a[100000];   long long was[100000];   int main() {     scanf( %d %d %d , &k[0], &k[1], &k[2]);     scanf( %d %d %d , &t[0], &t[1], &t[2]);     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %I64d , &a[i]);       was[i] = a[i];     }     for (int i = 0; i < 3; i++) {       for (int j = 0; j < k[i]; j++) {         int pos = j;         long long finished = -1000000;         if (pos >= n) break;         while (pos < n) {           a[pos] = finished = max(finished + t[i], a[pos] + t[i]);           pos += k[i];         }       }     }     long long res = a[0] - was[0];     for (int i = 1; i < n; i++)       if (res < a[i] - was[i]) res = a[i] - was[i];     printf( %I64d n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, h;   int cnt;   int main() {     cin >> n >> h;     for (int e = 0; e < n; e++) {       int in;       cin >> in;       if (in > h) {         cnt += 2;       } else {         cnt++;       }     }     cout << cnt;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     long long ans = 0;     for (int i = (0); i < (n); ++i) {       long long t, T, x, cost;       cin >> t >> T >> x >> cost;       if (t >= T) {         ans += cost + m * x;         continue;       }       long long aux1 = cost;       if (m > (T - t)) aux1 += m * x;       long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1;       aux2 *= cost;       ans += min(aux1, aux2);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using LL = long long;   using VI = vector<int>;   const int inf = 2e9;   LL calc(set<int>& st, int L, int R) {     VI a;     for (int i = L;;) {       a.push_back(i);       if (i == R) break;       if (st.upper_bound(i) != st.end() && *st.upper_bound(i) < R)         i = *st.upper_bound(i);       else         i = R;     }     int ans = 0;     for (int i = 0; i + 1 < a.size(); i++) ans = max(ans, a[i + 1] - a[i]);     return ans;   }   int main() {     int n;     cin >> n;     set<int> a, b, c;     for (int i = 0; i < n; i++) {       int x;       char ch;       scanf( %d %c , &x, &ch);       if (ch ==  B ) a.insert(x);       if (ch ==  R ) b.insert(x);       if (ch ==  P ) c.insert(x);     }     int B = 0, R = 0;     int mn = inf, mx = -inf;     if (a.size()) mn = min(mn, *a.begin()), mx = max(mx, *--a.end());     if (c.size()) mn = min(mn, *c.begin()), mx = max(mx, *--c.end());     if (mn != inf) B = mx - mn;     mn = inf, mx = -inf;     if (b.size()) mn = min(mn, *b.begin()), mx = max(mx, *--b.end());     if (c.size()) mn = min(mn, *c.begin()), mx = max(mx, *--c.end());     if (mn != inf) R = mx - mn;     LL ans = (LL)B + R;     if (!B || !R || c.empty()) {       cout << ans << endl;       return 0;     }     for (auto i = c.begin(); ++i != c.end(); i++) {       auto j = i;       i--;       LL g = (*j - *i) - calc(a, *i, *j) - calc(b, *i, *j);       if (g < 0) ans += g;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const int N = 1e6 + 10;   const int mod = 998244353;   int m, n, k = 0, flag = 0, cnt = 0;   int a[N], ans[N];   long long pow2[N];   bool did[N];   int main() {     ios::sync_with_stdio(false);     cin >> n >> k;     ;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] <= k && !flag) {         cnt++;         did[i] = 1;       }       if (a[i] > k) flag = 1;     }     flag = 0;     for (int i = n - 1; i >= 0; i--) {       if (a[i] <= k && !flag && !did[i]) cnt++;       if (a[i] > k) flag = 1;     }     cout << cnt << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100;   int cost[N][2], timee, tmm, num, clr, n;   char ha, arr[25];   string tm1, tm2;   int main() {     scanf(  %s , &arr);     tm1 = arr;     scanf(  %s%d , &arr, &n);     tm2 = arr;     while (n--) {       scanf( %d %c%d %c , &timee, &tmm, &num, &clr);       if (clr ==  y ) {         if (tmm ==  h ) {           cost[num][0]++;         } else {           cost[num][1]++;         }       } else {         if (tmm ==  h ) {           cost[num][0] += 2;         } else {           cost[num][1] += 2;         }       }       if (cost[num][0] > 1) {         cout << tm1 <<     << num <<     << timee <<   n ;         cost[num][0] = -1e9;       }       if (cost[num][1] > 1) {         cout << tm2 <<     << num <<     << timee <<   n ;         cost[num][1] = -1e9;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<string> setao;   string s;   const int N = 10010;   int pd[N][4], n;   char solve(int k, int last) {     if (k == n) return 1;     if (n - k == 1 or k > n) return 0;     if (pd[k][last] != -1) return pd[k][last];     string ant;     for (int i = last; i >= 1; i--) {       ant.push_back(s[k - i]);     }     string x;     for (int i = 0; i < 2; i++) x.push_back(s[k + i]);     char flag = 0;     if (x != ant) {       if (solve(k + 2, 2)) {         setao.insert(x);         flag = 1;       }     }     if (n - k > 2) {       x.push_back(s[k + 2]);       if (x != ant) {         if (solve(k + 3, 3)) {           setao.insert(x);           flag = 1;         }       }     }     return pd[k][last] = flag;   }   int main(void) {     memset(pd, -1, sizeof pd);     cin >> s;     n = s.size();     for (int i = 5; i < n; i++) {       solve(i, 0);     }     printf( %d n , (int)setao.size());     for (set<string>::iterator it = setao.begin(); it != setao.end(); it++) {       cout << *it << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int ara[1000000];   int wow[7 * 22000];   void make_wow(int x) {     int xx;     int l;     int k = 1;     stack<int> s;     for (int i = 1; i <= x; i++) {       xx = i;       while (xx != 0) {         l = xx % 10;         xx /= 10;         s.push(l);       }       while (!s.empty()) {         wow[k++] = s.top();         s.pop();       }     }   }   int find_block(int x, int b) {     for (int i = 1; i <= b; i++) {       if (ara[i] >= x) return i - 1;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int q;     cin >> q;     int k = 1;     int i = 1;     int b = 1;     long long int sum = 0, x, lb;     while (ara[b - 1] <= 1000000000) {       sum += log10(i++) + 1;       ara[b] = ara[b - 1] + sum;       b++;     }     make_wow(b);     while (q--) {       cin >> x;       cout << wow[x - ara[find_block(x, b)]] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<long long, vector<long long>> M;   bool res[100010];   int main() {     long long n;     long long k;     cin >> n >> k;     long long S = 0;     for (long long i = 1; i <= n; i++) {       long long a;       cin >> a;       M[a].push_back(i);       S += a;     }     if (S < k) {       cout <<  -1 ;       return (0);     }     if (S == k) return (0);     long long lv_prev = 0;     long long tmp = n;     map<long long, vector<long long>>::iterator it = M.begin();     while (true) {       long long lv = it->first;       if (k >= (lv - lv_prev) * tmp) {         k -= ((lv - lv_prev) * tmp);         tmp -= it->second.size();       } else         break;       it++;       lv_prev = lv;     }     long long dlt = k / tmp + lv_prev;     k %= tmp;     vector<pair<long long, long long>> V;     for (; it != M.end(); it++) {       for (long long i = 0; i < it->second.size(); i++) {         V.push_back(make_pair(it->second[i], it->first - dlt));       }     }     sort(V.begin(), V.end());     cout <<   ;     for (int i = 0; i < k; i++) {       V[i].second -= 1;       V.push_back(V[i]);     }     for (int i = k; i < V.size(); i++)       if (V[i].second > 0) cout << V[i].first <<    ;   exit:     return (0);   }


#include <bits/stdc++.h>   using namespace std;   int indeg[510], vis[510], indeg1[510], vis1[510];   int n, m;   vector<vector<int> > adj(510);   int check(int x) {     int i, c, p;     queue<int> q;     for (i = 0; i < n; i++) {       vis1[i] = vis[i];       indeg1[i] = indeg[i];     }     indeg1[x]--;     q.push(x);     c = 1;     while (!q.empty()) {       p = q.front();       q.pop();       for (i = 0; i < adj[p].size(); i++) {         indeg1[adj[p][i]]--;         if (indeg1[adj[p][i]] == 0 && vis1[adj[p][i]] == 0) {           vis1[adj[p][i]] = 1;           c++;           q.push(adj[p][i]);         }       }     }     return c;   }   int toposort() {     int i, c = 0, j, p;     queue<int> q;     vector<int> v;     for (i = 0; i < n; i++) {       indeg[i] = 0;       vis[i] = 0;     }     for (i = 0; i < n; i++) {       for (j = 0; j < adj[i].size(); j++) {         indeg[adj[i][j]]++;       }     }     for (i = 0; i < n; i++) {       if (indeg[i] == 0) {         q.push(i);         vis[i] = 1;         c++;       }     }     while (!q.empty()) {       p = q.front();       q.pop();       for (i = 0; i < adj[p].size(); i++) {         indeg[adj[p][i]]--;         if (indeg[adj[p][i]] == 0 && vis[adj[p][i]] == 0) {           vis[adj[p][i]] = 1;           c++;           q.push(adj[p][i]);         }       }     }     if (c == n) {       cout <<  YES  << endl;       exit(0);     } else {       for (i = 0; i < n; i++) {         if (indeg[i] == 1) {           v.push_back(i);         }       }       if (v.size() == 0) {         cout <<  NO  << endl;         exit(0);       } else {         for (i = 0; i < v.size(); i++)           if (check(v[i]) + c == n) {             cout <<  YES  << endl;             exit(0);           }         cout <<  NO  << endl;         exit(0);       }     }   }   int main() {     std::ios::sync_with_stdio(false);     int u, v, i;     cin >> n >> m;     for (i = 0; i < m; i++) {       cin >> u >> v;       u--;       v--;       adj[u].push_back(v);     }     toposort();   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-9;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   template <typename t1>   t1 gcd(t1 a, t1 b) {     return b == 0 ? a : gcd(b, a % b);   }   template <typename t1>   t1 lcm(t1 a, t1 b) {     return a * (b / gcd(a, b));   }   int main() {     string s;     while (cin >> s) {       stack<char> st;       int len = s.length();       int count = 0;       int flag = 0;       for (int i = 0; i < len; i++) {         char x = s[i];         if (x ==  <  || x ==  (  || x ==  {  || x ==  [ ) {           st.push(x);         } else {           if (st.empty()) {             flag = 1;             break;           }           char y = st.top();           if (y ==  <  && x ==  > ) {             st.pop();           } else if (y ==  (  && x ==  ) ) {             st.pop();           } else if (y ==  [  && x ==  ] ) {             st.pop();           } else if (y ==  {  && x ==  } ) {             st.pop();           } else {             count++;             st.pop();           }         }       }       if (!st.empty()) flag = 1;       if (flag)         cout <<  Impossible  << endl;       else         cout << count << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   mt19937 dadi(chrono::high_resolution_clock::now().time_since_epoch().count());   const int N = 100 + 5, K = 1e3 + 5;   int n, m, k, w[N], p[K], cnt[N][K];   struct game {     int u, v, t;   } a[K];   inline int add(int u, int v) {     shuffle(p, p + k, dadi);     int mx1 = *max_element(cnt[u], cnt[u] + k);     int mx2 = *max_element(cnt[v], cnt[v] + k);     for (int i = 0; i < k; i++)       if (cnt[u][p[i]] < mx1 && cnt[v][p[i]] < mx2) return p[i];     for (int i = 0; i < k; i++)       if (cnt[u][p[i]] < mx1 || cnt[v][p[i]] < mx2) return p[i];     return p[0];   }   inline void read_input() {     cin >> n >> m >> k;     for (int i = 0; i < n; i++) cin >> w[i];     for (int i = 0; i < m; i++) {       cin >> a[i].u >> a[i].v;       a[i].u--, a[i].v--;     }   }   inline void solve() {     for (iota(p, p + k, 0); true; memset(cnt, 0, sizeof cnt)) {       bool flag = true;       for (int i = 0; i < m; i++) {         cnt[a[i].u][a[i].t = add(a[i].u, a[i].v)]++;         cnt[a[i].v][a[i].t++]++;       }       for (int i = 0; i < n; i++)         flag &=             *max_element(cnt[i], cnt[i] + k) - *min_element(cnt[i], cnt[i] + k) <             3;       if (flag) break;     }   }   inline void write_output() {     for (int i = 0; i < m; i++) cout << a[i].t << endl;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     read_input(), solve(), write_output();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int k, m = 1e9, a, b, n, i;   int main() {     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d%d , &a, &b);       m = min(m, b);       k += m * a;     }     printf( %d n , k);     return 0;   }