#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void read(T &x) {     x = 0;     char c = getchar();     int f = 1;     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) {       x = x * 10 + c -  0 ;       c = getchar();     }     x *= f;   }   template <class T>   inline void umin(T &x, T y) {     x = x < y ? x : y;   }   template <class T>   inline void umax(T &x, T y) {     x = x > y ? x : y;   }   inline unsigned int R() {     static unsigned int seed = 416;     return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;   }   const int N = 20200;   int a[N], n, m;   vector<int> b[N];   int main() {     read(n);     read(m);     while (m--) {       int x, y;       read(x);       read(y);       int d = y >= x ? y - x : y - x + n;       b[x].push_back(d);     }     for (register int i = (1); i <= (n); i++) sort(b[i].begin(), b[i].end());     for (register int i = (1); i <= (n); i++)       if (((int)b[i].size()))         a[i] = b[i][0] + n * (((int)b[i].size()) - 1);       else         a[i] = -1e9;     for (register int i = (1); i <= (n); i++) {       int res = 0;       for (int j = i, t = 0; t < n; t++, j = j + 1 <= n ? j + 1 : 1)         umax(res, t + a[j]);       printf( %d  , res);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     char r[100];     if (s.length() < 6) {       cout <<  no  << endl;       return 0;     }     int val = 0;     for (int i = s.length() - 1; i >= 0; i--) {       if (val == 6) {         if (s[i] ==  1 ) {           cout <<  yes  << endl;           return 0;         }       } else if (s[i] ==  0 ) {         val++;       }     }     cout <<  no  << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     int ans = 0, a;     if (k & 1) {       for (int i = 0; i < n; i++) {         cin >> a;         if (a <= 3)           ans ^= (a & 1);         else if (a & 1)           ans ^= 0;         else {           int cnt = 0;           while (a % 2 == 0) {             cnt++;             a /= 2;           }           if (a == 3) cnt++;           ans ^= (cnt & 1 ? 1 : 2);         }       }     } else {       for (int i = 0; i < n; i++) {         cin >> a;         if (a < 3)           ans ^= a;         else if (a & 1)           ans ^= 0;         else           ans ^= 1;       }     }     if (ans == 0)       puts( Nicky );     else       puts( Kevin );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1e9 + 7;   struct frac {     long long first, second;     frac(long long X = 0) : first((X % mod + mod) % mod), second(1) {}     frac(long long X, long long Y)         : first((X % mod + mod) % mod), second((Y % mod + mod) % mod) {}     frac operator+(frac a) {       return frac(first * a.second + second * a.first, second * a.second);     }     frac operator-(frac a) {       return frac(first * a.second - second * a.first, second * a.second);     }     frac operator*(frac a) { return frac(first * a.first, second * a.second); }     frac operator/(frac a) { return frac(first * a.second, second * a.first); }   };   frac pd[1010][1010], pa, emplace_back;   int va, vb, k;   frac solve(int a, int ja) {     if (a + ja >= k)       return emplace_back * ((pa + frac(a + ja)) - (pa * frac(a + ja))) /              ((pa - frac(1)) * (pa - frac(1)));     if (pd[a][ja].first != -1) return pd[a][ja];     return pd[a][ja] = pa * solve(a + 1, ja) + emplace_back * solve(a, ja + a);   }   long long modpow(long long b, long long e) {     long long ret = 1;     while (e) {       if (e & 1) ret = ret * b % mod;       b = b * b % mod;       e >>= 1;     }     return ret;   }   int main() {     for (__typeof(1010) i = (0) - ((0) > (1010)); i != (1010) - ((0) > (1010));          i += 1 - 2 * ((0) > (1010)))       for (__typeof(1010) j = (0) - ((0) > (1010)); j != (1010) - ((0) > (1010));            j += 1 - 2 * ((0) > (1010)))         pd[i][j].first = -1;     scanf( %d%d%d , &k, &va, &vb);     pa = frac(va, va + vb);     emplace_back = frac(vb, va + vb);     frac p = solve(1, 0);     frac ans = pa * p / (frac(1) - emplace_back);     printf( %lld n , ans.first * modpow(ans.second, mod - 2) % mod);   }


#include <bits/stdc++.h>   using namespace std;   long long int t, n, k, m;   long long int A[100001];   long long int M = 1e9 + 7;   bool isSubsetSum(int set[], int n, int sum) {     if (sum == 0) return true;     if (n == 0) return false;     if (set[n - 1] > sum) return isSubsetSum(set, n - 1, sum);     return isSubsetSum(set, n - 1, sum) ||            isSubsetSum(set, n - 1, sum - set[n - 1]);   }   int main() {     long long int k;     cin >> n >> k;     if (2 * k >= n * (n - 1))       cout <<  no solution  << endl;     else {       for (int i = 0; i < n; i++) cout <<  0   << i << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool cmp(int a, int b) { return a > b; }   int main() {     string s;     cin >> s;     vector<int> a, b;     vector<int> dec1, dec2;     int sum1 = s[0] -  0  + (s[1] -  0 ) + (s[2] -  0 );     int sum2 = (s[3] -  0 ) + (s[4] -  0 ) + (s[5] -  0 );     if (sum1 == sum2) {       cout << 0 << endl;       return 0;     } else if (sum1 > sum2)       for (int i = 0; i < 6; ++i) {         if (i < 3)           dec1.push_back((s[i] -  0 ));         else           dec2.push_back(9 - (s[i] -  0 ));       }     else {       for (int i = 0; i < 6; ++i) {         if (i < 3)           dec1.push_back(9 - (s[i] -  0 ));         else           dec2.push_back(s[i] -  0 );       }     }     sort(dec1.begin(), dec1.end());     sort(dec2.begin(), dec2.end());     int x = abs(sum1 - sum2);     int cnt = 0, i = 2, j = 2;     while (x > 0) {       if (dec1[i] > dec2[j]) {         x -= dec1[i];         cnt++;         i--;       } else {         x -= dec2[j];         cnt++;         j--;       }     }     cout << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     long long x, p, id;     bool operator<(const node &a) const { return x < a.x; }   } a[200010];   long long n, v, d, ans, Next[200010];   stack<node> s;   signed main() {     scanf( %lld%lld%lld , &d, &v, &n);     for (long long i = 1; i <= n; i++) scanf( %lld%lld , &a[i].x, &a[i].p);     a[0].p = 1e6 + 1;     a[n + 1].x = d;     sort(a, a + n + 2);     for (long long i = 0; i <= n; i++) {       if (a[i + 1].x - a[i].x > v) {         puts( -1 );         return 0;       }     }     s.push(node{d, 0, n + 1});     for (long long i = n; i >= 1; i--) {       node t = node{0, 0, 0};       while (t = s.top(), t.x - a[i].x > v || t.p > a[i].p) {         s.pop();         if (s.empty()) {           t.id = 0;           break;         }       }       Next[i] = t.id;       s.push(node{a[i].x, a[i].p, i});     }     long long i = 0, now = v;     while (i <= n) {       if (Next[i]) {         long long dis = a[Next[i]].x - a[i].x;         if (dis < now)           now -= dis;         else           ans += a[i].p * (dis - now), now = 0;         i = Next[i];       } else {         ans += a[i].p * (v - now);         now = v - (a[i + 1].x - a[i].x);         i++;       }     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long Pow10(int n) {     if (n <= 0) return 1;     return 10 * Pow10(n - 1);   }   void solve() {     long long k;     cin >> k;     int lev = 0;     while (k >= 9 * (lev + 1) * Pow10(lev)) {       k -= 9 * (lev + 1) * Pow10(lev);       lev++;     }     long long num = Pow10(lev) + k / (lev + 1) - (k % (lev + 1) ? 0 : 1);     stringstream res;     res << num;     string ans;     res >> ans;     cout << ans[(k % (lev + 1) ? k % (lev + 1) : ans.size()) - 1];   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     solve();   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2000 + 10;   char s[maxn][maxn];   int deg[maxn][maxn];   int dx[] = {1, 0, -1, 0};   int dy[] = {0, 1, 0, -1};   char fillchar[2][5] = { ^<v> ,  v>^< };   inline int caldeg(int i, int j) {     return (int)(s[i - 1][j] ==  . ) + (int)(s[i + 1][j] ==  . ) +            (int)(s[i][j - 1] ==  . ) + (int)(s[i][j + 1] ==  . );   }   int main() {     int n, m;     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; ++i) {       scanf( %s , s[i] + 1);     }     queue<pair<int, int> > Q;     int cnt = 0;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         if (s[i][j] ==  . ) {           deg[i][j] = caldeg(i, j);           cnt++;           if (deg[i][j] == 1) Q.push(make_pair(i, j));         }       }     }     if (cnt & 1) {       puts( Not unique );       return 0;     }     while (!Q.empty()) {       pair<int, int> cur = Q.front();       Q.pop();       int x = cur.first;       int y = cur.second;       if (s[x][y] ==  . ) {         int i, nx, ny;         for (i = 0; i < 4; ++i) {           nx = x + dx[i];           ny = y + dy[i];           if (s[nx][ny] ==  . ) break;         }         if (i == 4) {           puts( Not unique );           return 0;         }         s[x][y] = fillchar[0][i];         s[nx][ny] = fillchar[1][i];         deg[x][y] = deg[nx][ny] = 0;         cnt -= 2;         for (i = 0; i < 4; ++i) {           int nnx = nx + dx[i];           int nny = ny + dy[i];           if (s[nnx][nny] ==  . ) {             deg[nnx][nny]--;             if (deg[nnx][nny] == 1) Q.push(make_pair(nnx, nny));           }         }       }     }     if (cnt != 0) {       puts( Not unique );     } else {       for (int i = 1; i <= n; ++i) {         printf( %s n , s[i] + 1);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct prik {     int hair, mood, ind;   };   bool comp1(prik a, prik b) {     if (a.hair != b.hair)       return a.hair > b.hair;     else       return a.mood > b.mood;   }   bool comp2(prik a, prik b) {     if (a.mood != b.mood)       return a.mood < b.mood;     else       return a.hair > b.hair;   }   prik mp(int a, int b, int c) {     prik q;     q.hair = a;     q.mood = b;     q.ind = c;     return q;   }   prik a[110000], b[110000], c[110000];   int main() {     bool used[110000] = {0};     int n, t, d, m, k, ii[110000];     cin >> n >> m >> k;     for (int i = 0; i < n; i++) {       cin >> t >> d;       a[i] = mp(t, d, i);       b[i] = a[i];       c[i] = a[i];     }     sort(b, b + n, comp1);     sort(c, c + n, comp2);     for (int i = 0; i < n; i++) {       ii[c[i].ind] = i;     }     int mm = INT_MAX;     for (int i = 0, cn = 0; cn < k; i++) {       if (ii[b[i].ind] >= m - k) {         cn++;         cout << b[i].ind + 1 <<    ;         if (ii[b[i].ind] < mm) {           mm = ii[b[i].ind];         }       }     }     for (int i = mm - 1; i >= mm - m + k; i--) {       cout << c[i].ind + 1 <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   int gcd(long long a, long long b, long long& x, long long& y) {     if (b == 0) {       x = 1;       y = 0;       return a;     }     long long x1, y1;     long long d = gcd(b, a % b, x1, y1);     x = y1;     y = x1 - y1 * (a / b);     return d;   }   int main() {     long long a, b, c, x0, y0;     cin >> a >> b >> c;     c = (-1) * c;     long long g = gcd(a, b, x0, y0);     if (c % g != 0) {       cout <<  -1 ;     } else {       x0 *= c / g;       y0 *= c / g;       cout << x0 <<     << y0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int OO = 0x3f3f3f3f, N = 1e5 + 5, mod = 1e9 + 9;   int n, k, indx[10], ans = OO;   char arr[10][10];   int num(int r) {     int ret = 0;     for (int i = 0; i < k; ++i) {       ret = ret * 10 + (arr[r][indx[i]] -  0 );     }     return ret;   }   int main() {     cin >> n >> k;     for (int i = 0; i < k; ++i) indx[i] = i;     for (int i = 0; i < n; ++i) scanf( %s , arr[i]);     do {       int mn = OO, mx = 0;       for (int i = 0; i < n; ++i) {         mn = min(mn, num(i));         mx = max(mx, num(i));       }       ans = min(ans, mx - mn);     } while (next_permutation(indx, indx + k));     cout << ans;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   using ll = long long;   const char E =   n ;   const int N = 1005;   const ll mod = 1e9 + 7;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, k, x, dp[2][1024] = {0};     cin >> n >> k >> x;     for (int i = 1; i <= n; i++) {       int y;       cin >> y;       dp[1][y]++;     }     for (int i = 1; i <= k; i++) {       int used = 0;       for (int j = 0; j <= 1023; j++) {         int bit, nbit;         bit = i & 1, nbit = i & 1 ^ 1;         if (!(dp[bit][j] & 1)) {           dp[nbit][j] += (dp[bit][j] >> 1);           dp[nbit][j ^ x] += (dp[bit][j] >> 1);         } else if (dp[bit][j]) {           dp[nbit][j] += (dp[bit][j] >> 1) + (used & 1);           dp[nbit][j ^ x] += (dp[bit][j] >> 1) + (used & 1 ^ 1);         }         used += dp[bit][j];         dp[bit][j] = 0;       }     }     int mx = 0, mn = 1024;     for (int i = 0; i <= 1023; i++) {       if (dp[k & 1 ^ 1][i]) {         mx = max(mx, i);         mn = min(mn, i);       }     }     cout << mx <<     << mn << E;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x7f7f7f7f;   void setup() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cout.precision(15);   }   struct seg_tree {     struct node {       int val;       node(int _val = 0x7f7f7f7f) { val = _val; }       node operator+(const node &y) { return {min(val, y.val)}; }     };     int S;     vector<node> arr;     seg_tree(int _S) {       S = _S;       arr = vector<node>(2 * S);     }     void upd(int i, node v) {       i += S + 1;       arr[i] = v;       while (i > 1) {         i /= 2;         arr[i] = arr[2 * i] + arr[2 * i + 1];       }     }     node query(int i, int j) {       node res;       for (i += S + 1, j += S + 1; i <= j; i /= 2, j /= 2) {         if ((i & 1) == 1) res = res + arr[i++];         if ((j & 1) == 0) res = res + arr[j--];       }       return res;     }   };   int find(seg_tree &seg, int i, int k) {     int lo = 0, hi = i;     int ans = 0;     while (lo <= hi) {       int mi = (lo + hi) / 2;       int lv = seg.query(mi, i).val;       if (lv <= k) {         ans = mi;         lo = mi + 1;       } else         hi = mi - 1;     }     return ans;   }   int look(vector<int> &all, int v) {     return lower_bound(all.begin(), all.end(), v) - all.begin();   }   const int MAXQ = 200005;   int N, Q;   seg_tree rows(1 << 19), cols(1 << 19);   int L[MAXQ], LC[MAXQ];   int R[MAXQ], RC[MAXQ];   char T[MAXQ];   vector<bool> ater, atec;   int main() {     setup();     cin >> N >> Q;     rows.upd(0, 0);     cols.upd(0, 0);     vector<int> all;     all.push_back(0);     for (int i = 0; i < Q; i++) {       cin >> L[i] >> R[i] >> T[i];       all.push_back(L[i]);       all.push_back(R[i]);     }     sort(all.begin(), all.end());     all.resize(unique(all.begin(), all.end()) - all.begin());     ater.resize(all.size());     atec.resize(all.size());     for (int i = 0; i < Q; i++) {       int LC = look(all, L[i]);       int RC = look(all, R[i]);       if (T[i] ==  U ) {         if (atec[LC] || ater[RC]) {           cout << 0 <<   n ;           continue;         }         int b = find(rows, RC, LC);         cols.upd(LC, b);         cout << all[RC] - all[b] <<   n ;         atec[LC] = true;       } else {         if (ater[RC] || atec[LC]) {           cout << 0 <<   n ;           continue;         }         int b = find(cols, LC, RC);         rows.upd(RC, b);         cout << all[LC] - all[b] <<   n ;         ater[RC] = true;       }     }   }


#include <bits/stdc++.h>   using namespace std;   int N, M;   double K;   map<string, int> skill;   int main() {     cin >> N >> M >> K;     for (int i = 0; i < N; i++) {       string name;       int exp;       cin >> name >> exp;       if (exp * K + 0.000001 >= 100) skill[name] = exp * K + 0.000001;     }     for (int i = 0; i < M; i++) {       string name;       cin >> name;       skill[name] = max(skill[name], 0);     }     cout << skill.size() << endl;     for (map<string, int>::iterator it = skill.begin(); it != skill.end(); it++)       cout << it->first <<     << it->second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T extGcd(T a, T b, T &x, T &y);   template <typename T>   T modInv(T a, T m);   const int mod = (119 << 23) + 1, root = 62;   template <int maxn>   struct NTT {     constexpr static int lg2(int n) { return 32 - __builtin_clz(n - 1); }     const static int MAXN = 1 << lg2(maxn), MOD = 998244353, root = 3;     int rev[MAXN], rt[MAXN];     int mul(int a, int b) { return (long long)a * b % MOD; }     int sub(int a, int b) { return b > a ? a - b + MOD : a - b; }     int add(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }     int binExp(int base, long long exp) {       if (exp == 0) return 1;       return mul(binExp(mul(base, base), exp / 2), exp & 1 ? base : 1);     }     NTT() {       rt[1] = 1;       for (int k = 1; k < lg2(MAXN); k++) {         int z[] = {1, binExp(root, (MOD - 1) >> (k + 1))};         for (int i = (1 << k); i < 2 << k; i++) rt[i] = mul(rt[i / 2], z[i & 1]);       }     }     void ntt(int *a, int n) {       for (int i = 0; i < n; i++) rev[i] = (rev[i / 2] | (i & 1) << lg2(n)) / 2;       for (int i = 0; i < n; i++)         if (i < rev[i]) swap(a[i], a[rev[i]]);       for (int k = 1; k < n; k *= 2)         for (int i = 0; i < n; i += 2 * k)           for (int j = 0; j < k; j++) {             int z = mul(rt[j + k], a[i + j + k]);             a[i + j + k] = sub(a[i + j], z);             a[i + j] = add(a[i + j], z);           }     }     int in[2][MAXN];     vector<int> multiply(const vector<int> &a, const vector<int> &b) {       fill(begin(in[0]), end(in[0]), 0), fill(begin(in[1]), end(in[1]), 0);       if (a.empty() || b.empty()) return {};       int sz = a.size() + b.size() - 1, n = 1 << lg2(sz);       copy(begin(a), end(a), in[0]), copy(begin(b), end(b), in[1]);       ntt(in[0], n), ntt(in[1], n);       int invN = binExp(n, MOD - 2);       for (int i = 0; i < n; i++) in[0][i] = mul(mul(in[0][i], in[1][i]), invN);       reverse(in[0] + 1, in[0] + n);       ntt(in[0], n);       return vector<int>(in[0], in[0] + sz);     }   };   NTT<524288> ntt;   using namespace std;   int ppp, k;   vector<int> ans;   vector<int> queries;   map<int, int> white;   vector<int> red;   vector<int> one = {1, 2};   vector<int> two = {1, 2, 1};   int modpow(int b, int e) {     int r = 1;     while (e) {       if (e & 1) r = (r * 1ll * b) % mod;       b = (b * 1ll * b) % mod;       e >>= 1;     }     return r;   }   vector<long long> rt(524288, 1), rev(524288);   void solve_red(int red) {     int n = 524288;     vector<int> poly(524288);     poly[0] = 1;     ntt.ntt(poly.data(), 524288);     int o = 0, t = 0;     for (auto it : white) {       if (it.first >= red) continue;       if (it.second == 1)         o++;       else         t++;     }     vector<int> out(n);     long long inv = modpow(n, mod - 2);     for (int i = 0; i < 524288; i++) {       out[-i & (n - 1)] = (poly[i] * 1ll * modpow(one[i], o)) % mod;       out[-i & (n - 1)] = (out[-i & (n - 1)] * 1ll * modpow(two[i], t)) % mod;       out[-i & (n - 1)] = (out[-i & (n - 1)] * 1ll * inv) % mod;     }     ntt.ntt(out.data(), 524288);     for (int i = 0; i < queries.size(); i++) {       int rem = queries[i] - red - 1;       if (rem >= 0 && rem < 524288) {         ans[i] = (ans[i] + out[rem]) % mod;       }     }   }   void solve() {     one.resize(524288);     two.resize(524288);     int B = 19;     int n = 524288;     for (int i = 0; i < n; i++) rev[i] = (rev[i / 2] | (i & 1) << B) / 2;     long long curL = mod / 2, inv = modpow(n, mod - 2);     for (int k = 2; k < n; k *= 2) {       long long z[] = {1, modpow(root, curL /= 2)};       for (int i = k; i < 2 * k; i++) rt[i] = rt[i / 2] * z[i & 1] % mod;     }     ntt.ntt(one.data(), 524288);     ntt.ntt(two.data(), 524288);     scanf( %d %d , &ppp, &k);     int w;     for (int i = 0; i < ppp; i++) {       scanf( %d , &w);       white[w]++;       if (white[w] >= 2) white[w] = 2;     }     red.resize(k);     for (int i = 0; i < k; i++) scanf( %d , &red[i]);     int q;     scanf( %d , &q);     ans.resize(q);     queries.resize(q);     for (int i = 0; i < q; i++) {       scanf( %d , &queries[i]);       queries[i] /= 2;     }     for (int r : red) solve_red(r);     for (int a : ans) printf( %d n , a);   }   int main() { solve(); }   template <typename T>   T extGcd(T a, T b, T &x, T &y) {     if (b == 0) {       x = 1;       y = 0;       return a;     } else {       int g = extGcd(b, a % b, y, x);       y -= a / b * x;       return g;     }   }   template <typename T>   T modInv(T a, T m) {     T x, y;     extGcd(a, m, x, y);     return (x % m + m) % m;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, ans[200055], odd, even, cost, ne, no;   pair<int, int> a[200055];   bool need[200055];   map<int, int> M;   int next_even() {     while (M[ne] && ne > 0) ne -= 2;     if (ne == 0)       return -1;     else       return ne;   }   int next_odd() {     while (M[no] && no > 0) no -= 2;     return no;   }   int main() {     scanf( %d%d , &n, &m);     ne = no = m;     if (ne % 2 != 0) ne--;     if (no % 2 != 1) no--;     for (int i = 0; i < n; i++) scanf( %d , &a[i].first);     for (int i = 0; i < n; i++) ans[i] = a[i].first;     for (int i = 0; i < n; i++) a[i].second = i;     sort(a, a + n);     for (int i = 0; i < n; i++) {       if (i == 0 || a[i].first != a[i - 1].first) {         if (a[i].first % 2)           odd++;         else           even++;       }       if (i > 0 && a[i].first == a[i - 1].first) need[i] = 1;     }     if (even > n / 2) {       for (int i = 0; i < n; i++) {         if (even == n / 2) break;         if (need[i] == 0 && a[i].first % 2 == 0) {           need[i] = 1;           even--;         }       }     }     if (odd > n / 2) {       for (int i = 0; i < n; i++) {         if (odd == n / 2) break;         if (need[i] == 0 && a[i].first % 2 == 1) {           need[i] = 1;           odd--;         }       }     }     for (int i = 0; i < n; i++)       if (!need[i]) M[a[i].first] = 1;     for (int i = 0; i < n; i++)       if (need[i]) {         cost++;         int next = -1;         if (even < n / 2) {           next = next_even();           even++;         } else {           next = next_odd();           odd++;         }         if (next == -1) return 0 * printf( -1 n );         ans[a[i].second] = next;         M[next] = 1;       }     printf( %d n , cost);     for (int i = 0; i < n; i++) printf( %d%c , ans[i], (i == n - 1) ?   n  :    );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 605;   int dp[N][N];   int l[N], r[N];   string res;   void fill(int i, int j, int a, int b) {     int k = j - i;     if (i > j || a > b) return;     if (l[i] <= 2 * k + 1 && 2 * k + 1 <= r[i] && (i == j || dp[i + 1][j])) {       res[a] =  ( , res[b] =  ) ;       fill(i + 1, j, a + 1, b - 1);     } else {       for (int k = i; k < j; ++k)         if (dp[i][k] && dp[k + 1][j]) {           int diff = k - i + 1;           fill(i, k, a, a + 2 * diff - 1);           fill(k + 1, j, a + 2 * diff, b);           break;         }     }   }   int main() {     std::ios::sync_with_stdio(false);     int n;     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> l[i] >> r[i];     }     memset(dp, 0, sizeof(dp));     for (int i = n; i >= 1; --i) {       for (int j = i; j <= n; ++j) {         int k = j - i;         if (l[i] <= 2 * k + 1 && 2 * k + 1 <= r[i] && (i == j || dp[i + 1][j]))           dp[i][j] = 1;         for (int k = i; k < j; ++k) dp[i][j] |= (dp[i][k] && dp[k + 1][j]);       }     }     if (dp[1][n]) {       res.resize(2 * n,  # );       fill(1, n, 0, 2 * n - 1);       cout << res << endl;     } else {       cout <<  IMPOSSIBLE  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 18;   int n, cnt[3 * N];   unsigned long long res[3 * N], sum[3 * N];   vector<int> a;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> n;     long long fuk = 1LL * n * (n - 1);     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       cnt[x]++;       a.push_back(x);     }     sort(a.begin(), a.end());     a.resize(unique(a.begin(), a.end()) - a.begin());     for (int i = 0; i < a.size(); i++) {       int k = 1;       while (1LL * a[i] * k < 3 * N) {         int z = cnt[k];         if (k == a[i]) z--;         res[a[i] * k] += 1LL * cnt[a[i]] * z;         k++;       }     }     for (int i = 1; i < 3 * N; i++) sum[i] = sum[i - 1] + res[i];     int m;     cin >> m;     while (m--) {       int p;       cin >> p;       long long resu = fuk - sum[p - 1];       printf( %I64d n , resu);     }   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) {       return a;     }     return gcd(b, a % b);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     long long g = 0;     for (int i = 0; i < n; i++) {       long long d;       cin >> d;       g = gcd(g, d);     }     long long res = 0;     for (long long i = 1; i * 1ll * i <= g; i++) {       if (g % i == 0) {         res++;         if (i != (g / i)) {           res++;         }       }     }     cout << res <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 5;   int aa[MAXN];   int nxt[MAXN];   int nnxt[MAXN];   int step[MAXN];   int group[MAXN];   int ll[MAXN];   int rr[MAXN];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, m;     memset(ll, 0x3f, sizeof ll);     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> aa[i];     int sqrtn = sqrt(n);     for (int i = 1; i <= n; i++)       group[i] = i / sqrtn, ll[group[i]] = min(ll[group[i]], i), rr[group[i]] = i;     auto getnxt = [&](int i) {       nxt[i] = i + aa[i];       if (nxt[i] <= n && group[nxt[i]] == group[i]) {         step[i] = step[nxt[i]] + 1;         nnxt[i] = nnxt[nxt[i]];         nxt[i] = nxt[nxt[i]];       } else {         step[i] = 1;         nnxt[i] = i;       }     };     for (int i = n; i >= 1; i--) {       getnxt(i);     }     while (m--) {       int op;       cin >> op;       if (op == 0) {         int a, b;         cin >> a >> b;         aa[a] = b;         for (int i = rr[group[a]]; i >= ll[group[a]]; i--) {           getnxt(i);         }       } else {         int a;         cin >> a;         int ans = 0;         while (nxt[a] <= n) {           ans += step[a];           a = nxt[a];         }         ans += step[a];         cout << nnxt[a] <<     << ans <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     string a, b;     cin >> a >> b;     if (a.size() == b.size()) {       int ca = 0, cb = 0;       for (int i = 0; i < a.size(); i++) {         ca += a[i] ==  1 ;         cb += b[i] ==  1 ;       }       if (!ca && cb || ca && !cb)         cout <<  NO ;       else         cout <<  YES ;     } else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T id(T b) {     return b;   };   template <class It>   bool all(It f, It l) {     return std::all_of(f, l, id<bool>);   }   template <class It>   bool any(It f, It l) {     return std::any_of(f, l, id<bool>);   }   template <typename T>   void chmax(T &x, T y) {     if (x < y) x = y;   }   template <typename T>   void chmin(T &x, T y) {     if (x > y) x = y;   }   const int MAX_N = 200000;   long long h;   int n;   long long d[MAX_N];   int main() {     cin >> h >> n;     for (int i = 0; i < int(n); i++) cin >> d[i];     long long max_d = 0;     long long g = h;     for (int i = 0; i < n; i++) {       g += d[i];       if (g <= 0) {         cout << (i + 1) <<   n ;         return 0;       }       chmax(max_d, h - g);     }     if (g >= h) {       cout <<  -1 n ;       return 0;     }     long long c = (h - max_d) / (h - g);     if ((h - max_d) % (h - g) != 0) c++;     long long m = c * n;     h -= (h - g) * c;     for (int i = 0; i < n && h > 0; i++) {       h += d[i];       m++;     }     cout << m <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long t;     cin >> t;     while (t--) {       long long n;       cin >> n;       set<long long> s;       for (long long i = 0; i < n; i++) {         long long x;         cin >> x;         s.insert(x);       }       vector<long long> vec(s.begin(), s.end());       sort(vec.begin(), vec.end());       reverse(vec.begin(), vec.end());       long long res = 0;       for (long long i = 0; i < 3; i++) {         vector<long long> chosen;         for (long long j = i; j < s.size(); j++) {           if (!chosen.empty() && chosen.size() == 3) break;           bool flag = false;           for (long long x : chosen) {             if (x % vec[j] == 0) {               flag = true;               break;             }           }           if (!flag) chosen.push_back(vec[j]);         }         long long sum = 0;         for (long long x : chosen) sum += x;         res = max(res, sum);       }       cout << res <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }   const int InF = 1 << 29;   inline int two(int n) { return 1 << n; }   inline int test(int n, int b) { return (n >> b) & 1; }   inline void set_bit(int& n, int b) { n |= two(b); }   inline void unset_bit(int& n, int b) { n &= ~two(b); }   inline int last_bit(int n) { return n & (-n); }   inline int ones(int n) {     int res = 0;     while (n && ++res) n -= n & (-n);     return res;   }   template <class T>   void chmax(T& a, const T& b) {     a = max(a, b);   }   template <class T>   void chmin(T& a, const T& b) {     a = min(a, b);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     long long a[n];     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     long long x = 0, y;     for (int i = 1; i <= n; i++) {       x++;       y = a[i];       for (int j = i; j <= y; j++) y = max(y, a[j]);       i = y;     }     cout << x << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100 * 1000 + 1000;   int arr[MAXN];   vector<int> pt[MAXN];   int mrk[MAXN];   int32_t main() {     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> arr[i];     for (int i = 0; i < n; i++) pt[arr[i]].push_back(i);     int cnt = n;     int cnt_2 = 1;     while (cnt) {       if (pt[1].size() == 0) {         cout << -1 << endl;         return 0;       }       int pt_2 = 1;       while (pt[pt_2].size() > 0) {         mrk[pt[pt_2][pt[pt_2].size() - 1]] = cnt_2;         pt[pt_2].pop_back();         cnt--;         pt_2++;       }       cnt_2++;     }     cout << *max_element(mrk, mrk + n) << endl;     for (int i = 0; i < n; i++) cout << mrk[i] <<    ;     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e6 + 3;   long long int x[maxn], y[maxn];   long long int w[maxn];   int ct[maxn];   vector<int> v[maxn];   int ans[maxn];   int done_per[maxn];   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; ++i) {       cin >> w[i];     }     memset(ct, 0, sizeof(ct));     for (int i = 0; i < m; ++i) {       cin >> x[i] >> y[i];       ct[x[i] - 1]++;       ct[y[i] - 1]++;       x[i]--;       y[i]--;       v[x[i]].push_back(i);       v[y[i]].push_back(i);     }     memset(done_per, 0, sizeof(done_per));     vector<int> in;     int last = m - 1;     for (int i = 0; i < n; i++) {       if (ct[i] <= w[i]) {         in.push_back(i);       }     }     if (in.size() == 0) {       cout <<  DEAD             <<   n ;       exit(0);     }     while (1) {       vector<int> in1;       for (int j = 0; j < (int)in.size(); j++) {         int t1 = in[j];         for (int i = 0; i < (int)v[t1].size(); i++) {           if (!done_per[v[t1][i]]) {             ans[last--] = v[t1][i];             done_per[v[t1][i]] = 1;             int temp = v[t1][i];             if (x[temp] != t1) {               ct[x[temp]]--;               if (ct[x[temp]] <= w[x[temp]]) in1.push_back(x[temp]);             } else {               ct[y[temp]]--;               if (ct[y[temp]] <= w[y[temp]]) in1.push_back(y[temp]);             }           }         }       }       if (last == -1) break;       if (in1.size() == 0) {         cout <<  DEAD               <<   n ;         exit(0);       }       in.clear();       for (int t4 : in1) in.push_back(t4);     }     cout <<  ALIVE           <<   n ;     for (int i = 0; i < m; i++) {       cout << ans[i] + 1 <<    ;     }     cout << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   int n, m, c[N], d[N], w[N];   bool val[N];   long long ans;   struct node {     int val, id;     bool operator<(const node A) const { return val > A.val; }   };   priority_queue<node> Q;   inline int IN() {     char c;     int flag = 1, k = 0;     for (c = getchar(); c <  0  || c >  9 ; c = getchar())       if (c ==  - ) flag = -1;     for (;  0  <= c && c <=  9 ; c = getchar()) k = k * 10 + c -  0 ;     return k * flag;   }   int main() {     n = IN();     m = IN();     for (int i = 1; i <= n; ++i) c[i] = IN();     for (int i = 1; i <= n; ++i) w[i] = IN();     for (int i = 1; i <= n; ++i) {       d[i] = c[i] / 100;       int tmp = c[i] % 100;       if (tmp == 0) continue;       int res = w[i] * (100 - tmp);       Q.push((node){res, i});       m -= tmp;       if (m < 0) {         m += 100;         node now = Q.top();         Q.pop();         ans += now.val;         val[now.id] = 1;       }     }     cout << ans << endl;     for (int i = 1; i <= n; ++i) {       cout << c[i] / 100 + val[i] <<    ;       if (val[i])         cout << 0 << endl;       else         cout << c[i] % 100 << endl;     }     return 0;   }


#include<cstdio>   #include<algorithm>   #include<iostream>   #include<cstring>   #define ll long long   #define mod (ll)(1e9+7)   using namespace std;       ll t,n,m,a[2000005];   ll ti,yi;   double xi;       int main()   {    //freopen( 1.in , r ,stdin);    cin>>n>>m;    for (int i=1; i<=n; i++)    {     cin>>ti>>xi>>yi;          for (int k=m; k>=0; k--)     if (a[k] || k==0)     {      ll x=(ll)(k);      for (int j=1; j<=yi; j++)      {       if (ti==2 && x==0) break;        if (ti==1) x= (x*1e5 + xi + 1e5 -1 ) / 1e5 ;         else x= (x*xi + 1e5 -1) / 1e5;       //x=ceil(x);       if (x>m || a[(int)x]) break;       else        a[(int)x]=i;      }      x/=(1e5);     }    }    for (int i=1; i<=m; i++)     printf( %lld  , a[i] ? a[i] : -1);    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int v[300100], dp[300100], deq[300100], n, m, l, ql, qr, i;   inline bool check(int v) {     l = 1, ql = 0, qr = 0;     dp[0] = 1;     for (i = 1; i <= n; ++i) {       dp[i] = 0;       if (i - m >= 0 && dp[i - m]) deq[qr++] = i - m;       while (::v[i] - ::v[l] > v) l++;       while (deq[ql] + 1 < l && ql < qr) ql++;       if (ql < qr) dp[i] = 1;     }     return dp[n];   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (i = 1; i <= n; ++i) cin >> v[i];     sort(v + 1, v + n + 1);     int l = 0, r = v[n] - v[1], mid;     while (l < r) {       mid = (l + r) >> 1;       if (check(mid))         r = mid - 1;       else         l = mid + 1;     }     if (check(l))       cout << l;     else       cout << l + 1;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005, D = 50;   const int V = N * D;   vector<int> g[N];   int S[V], ssz;   int low[V], num[V], cnt, vis[V];   int scc[V], scc_cnt;   int all[V], sz;   int n, m, d;   long long val[N];   void push(int i, int j) {     if (val[i] >> j & 1) {       all[sz++] = i;       while (sz >= V) {         n++;       }     }   }   int dp[V];   void tarjanSCC(int u) {     low[u] = num[u] = ++cnt;     vis[u] = 1;     S[ssz++] = u;     for (int w : g[u / d]) {       int v = w * d + (u % d + 1) % d;       while (v >= V) v++;       if (!num[v]) tarjanSCC(v);       if (vis[v]) low[u] = min(low[u], low[v]);     }     if (low[u] == num[u]) {       scc[u] = ++scc_cnt;       int v;       sz = 0;       push(u / d, u % d);       do {         v = S[--ssz];         vis[v] = 0;         push(v / d, v % d);         scc[v] = scc_cnt;         for (int wver : g[v / d]) {           int ver = wver * d + (v % d + 1) % d;           if (scc[ver]) {             dp[scc_cnt] = max(dp[scc_cnt], dp[scc[ver]]);           }         }       } while (u != v);       sort(all, all + sz);       dp[scc_cnt] += int(unique(all, all + sz) - all);     }   }   int main() {     scanf( %d %d %d , &n, &m, &d);     for (int i = 0; i < m; i++) {       int u, v;       scanf( %d %d , &u, &v);       u--, v--;       g[u].push_back(v);     }     char str[100];     for (int i = 0; i < n; i++) {       scanf(  %s , str);       for (int j = 0; j < d; j++) {         if (str[j] ==  1 ) {           val[i] |= 1LL << j;         }       }     }     tarjanSCC(0);     printf( %d n , dp[scc[0]]);   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   int cheak(int x) {     int sum = x;     while (x) {       sum += x / k;       x /= k;     }     if (sum >= n) return 1;     return 0;   }   int main() {     scanf( %d%d , &n, &k);     int head = 1, tail = n;     while (head <= tail) {       int mid = (head + tail) / 2;       if (cheak(mid))         tail = mid - 1;       else         head = mid + 1;     }     printf( %d n , head);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1010;   int n, m;   int dx[4] = {1, -1, 0, 0};   int dy[4] = {0, 0, -1, 1};   int need[4] = {2, 0, 3, 1};   int reallyNeed[4] = {0, 2, 1, 3};   int sx, sy, ex, ey;   struct state {     int x, y, dir;     long long cost;     state(int _x, int _y, int _dir, long long _cost) {       x = _x;       y = _y;       dir = _dir % 4;       cost = _cost;     }     bool operator<(const state &st) const { return cost > st.cost; }   };   long long res[N][N][4];   bool vis[N][N][4];   struct door {     int hasDoor[4];     door() {       for (int i = 0; i < 4; ++i) {         hasDoor[i] = 0;       }     }     void assign(int a, int b, int c, int d) {       hasDoor[0] = a;       hasDoor[1] = b;       hasDoor[2] = c;       hasDoor[3] = d;     }   };   door grid[N][N];   char st[N];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) {       scanf( %s , &st);       for (int j = 1; j <= m; ++j) {         if (st[j - 1] ==  + ) {           grid[i][j].assign(1, 1, 1, 1);         } else if (st[j - 1] ==  - ) {           grid[i][j].assign(0, 1, 0, 1);         } else if (st[j - 1] ==  | ) {           grid[i][j].assign(1, 0, 1, 0);         } else if (st[j - 1] ==  ^ ) {           grid[i][j].assign(1, 0, 0, 0);         } else if (st[j - 1] ==  > ) {           grid[i][j].assign(0, 1, 0, 0);         } else if (st[j - 1] ==  < ) {           grid[i][j].assign(0, 0, 0, 1);         } else if (st[j - 1] ==  v ) {           grid[i][j].assign(0, 0, 1, 0);         } else if (st[j - 1] ==  L ) {           grid[i][j].assign(1, 1, 1, 0);         } else if (st[j - 1] ==  R ) {           grid[i][j].assign(1, 0, 1, 1);         } else if (st[j - 1] ==  U ) {           grid[i][j].assign(0, 1, 1, 1);         } else if (st[j - 1] ==  D ) {           grid[i][j].assign(1, 1, 0, 1);         } else if (st[j - 1] ==  * ) {           grid[i][j].assign(0, 0, 0, 0);         }       }     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; j++) {         for (int u = 0; u < 4; u++) {           res[i][j][u] = 1e18;         }       }     }     scanf( %d%d%d%d , &sx, &sy, &ex, &ey);     priority_queue<state> queue;     queue.push(state(sx, sy, 0, 0));     res[sx][sy][0] = 0;     while (!queue.empty()) {       state src = queue.top();       queue.pop();       if (vis[src.x][src.y][src.dir]) continue;       vis[src.x][src.y][src.dir] = true;       int can[4] = {};       for (int i = 0; i < 4; i++) can[i] = grid[src.x][src.y].hasDoor[i];       rotate(can, can + (4 - src.dir), can + 4);       for (int i = 0; i < 4; ++i) {         int newX = dx[i] + src.x;         int newY = dy[i] + src.y;         if (newY > 0 && newX > 0 && newX <= n && newY <= m && can[need[i]]) {           int secondCan[4] = {};           for (int j = 0; j < 4; j++) {             secondCan[j] = grid[newX][newY].hasDoor[j];           }           rotate(secondCan, secondCan + (4 - src.dir), secondCan + 4);           if (!secondCan[reallyNeed[i]]) continue;           long long cost = src.cost + 1;           if (cost < res[newX][newY][src.dir]) {             res[newX][newY][src.dir] = cost;             queue.push(state(newX, newY, src.dir, cost));           }         }       }       for (int u = 1; u < 4; u++) {         queue.push(state(src.x, src.y, src.dir + u, src.cost + u));       }     }     long long ans = 1e18;     for (int i = 0; i < 4; ++i) {       ans = min(res[ex][ey][i], ans);     }     if (ans >= 1e17) {       ans = -1;     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1e9 + 7;   const long long int inf = 1e18;   long long int b, d;   string a, c;   vector<pair<long long int, long long int>> arr(101);   int main(void) {     cin >> b >> d;     cin >> a;     cin >> c;     for (int i = 0; i < c.size(); i++) {       long long int k = i, val = 0;       for (int j = 0; j < a.size(); j++) {         if (a[j] == c[k]) {           k++;           if (k == c.size()) {             k = 0;             val++;           }         }       }       arr[i] = make_pair(val, k);     }     long long int ans = 0, curr = 0;     for (int i = 0; i < b; i++) {       ans += arr[curr].first;       curr = arr[curr].second;     }     cout << ans / d << endl;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1.0);   template <class T>   void out(const vector<T> &a) {     cout <<  array:  ;     for (int i = 0; i < (int)a.size(); i++) cout << a[i] <<    ;     cout << endl;     cout.flush();   }   template <class T>   T gcd(T a, T b) {     return (b == 0) ? a : gcd(b, a % b);   }   template <class T>   T Abs(T x) {     return x > 0 ? x : -x;   }   template <class T>   inline bool isPrime(T n) {     if (n <= 1) return false;     for (T i = 2; i * i <= n; i++)       if (n % i == 0) return false;     return true;   }   template <class T>   T lcm(T a, T b) {     return a * (b / gcd(a, b));   }   template <typename T>   static T mod(T a, T b) {     a %= b;     if (a < 0) a += b;     return a;   }   template <class T>   string toString(T n) {     ostringstream oss;     oss << n;     oss.flush();     return oss.str();   }   template <typename T>   static void splitstr(const string &s, vector<T> &out) {     istringstream in(s);     out.clear();     copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));   }   int toInt(string s) {     int r = 0;     istringstream sin(s);     sin >> r;     return r;   }   long long toLl(string s) {     long long r = 0;     istringstream sin(s);     sin >> r;     return r;   }   bool IsVowel(char ch) {     ch = tolower(ch);     if (ch ==  a  || ch ==  e  || ch ==  i  || ch ==  o  || ch ==  u )       return true;     return false;   }   int BigMod(long long B, long long P, long long M) {     long long R = 1;     while (P > 0) {       if (P % 2 == 1) {         R = (R * B) % M;       }       P /= 2;       B = (B * B) % M;     }     return (int)R;   }   long long mulmod(long long a, long long b, long long c) {     long long x = 0, y = a % c;     while (b > 0) {       if (b % 2 == 1) {         x = (x + y) % c;       }       y = (y * 2) % c;       b /= 2;     }     return x % c;   }   void binprint(int mask, int n) {     int i;     string s =   ;     do {       s += (mask % 2 +  0 );       mask /= 2;     } while (mask);     reverse(s.begin(), s.end());     s = string((n - (int)s.size() > 0 ? n - (int)s.size() : 0),  0 ) + s;     for (i = (int)s.size() - n; i < (int)s.size(); i++) printf( %c , s[i]);     printf(  n );   }   long long mpow(long long x, long long k) {     if (k == 0) return 1;     long long r = mpow(x, k / 2);     return k % 2 ? (r * r * x) : (r * r);   }   int main() {     long long n, k, x[500], cnt, i;     cin >> n >> k;     for (i = 0; i < n; i++) cin >> x[i];     cnt = 0;     while (x[0] != k) {       for (i = 0; i < n - 1; i++) {         if (x[i] != x[i + 1]) x[i]++;       }       x[n - 1]++;       cnt++;     }     cout << cnt << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, a = 0, b = 0, count = 0;     char ch, c;     cin >> n >> c;     if (c ==  U )       b++;     else       a++;     n--;     while (n--) {       cin >> ch;       if (a == b && ch != c) {         count++;         c = ch;       }       if (ch ==  U )         b++;       else         a++;     }     cout << count;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int T, n, m;   int edge, ver;   bool mark[N];   int ans[N];   vector<int> G[N], seen, path;   void dfs(int v) {     ver++;     edge += G[v].size();     mark[v] = true;     seen.push_back(v);     for (int u : G[v])       if (!mark[u]) dfs(u);   }   void refresh() {     for (int i = 0; i < n; i++) {       ans[i] = mark[i] = 0;       G[i].clear();     }   }   bool DFS(int v) {     mark[v] = true;     path.push_back(v);     if (G[v].size() == 3 && path.size() > 1) return true;     for (int u : G[v])       if (!mark[u])         if (DFS(u)) return true;     path.pop_back();     return false;   }   int main() {     scanf( %d , &T);     while (T--) {       scanf( %d %d , &n, &m);       for (int i = 0; i < m; i++) {         int v, u;         scanf( %d %d , &v, &u);         v--, u--;         G[v].push_back(u);         G[u].push_back(v);       }       bool ok = false;       for (int i = 0; i < n; i++)         if (!mark[i]) {           edge = ver = 0;           seen.clear();           dfs(i);           edge >>= 1;           if (edge >= ver) {             for (int id : seen) ans[id] = 1;             ok = true;             break;           }         }       for (int i = 0; i < n; i++)         if (!ok && G[i].size() >= 4) {           ok = true;           ans[i] = 2;           for (int u : G[i]) ans[u] = 1;           break;         }       for (int i = 0; i < n; i++)         if (!ok && G[i].size() == 3)           for (int u : G[i])             if (G[u].size() == 3) {               ok = true;               for (int p : G[i]) ans[p] = 1;               for (int p : G[u]) ans[p] = 1;               ans[u] = ans[i] = 2;               break;             }       for (int i = 0; i < n; i++)         if (!ok && G[i].size() == 3) {           bool can = true;           for (int u : G[i])             if (G[u].size() == 1) can = false;           if (can) {             ok = true;             for (int u : G[i]) {               for (int k : G[u]) ans[k] = 1;               ans[u] = 2;             }             ans[i] = 3;             break;           }         }       for (int i = 0; i < n; i++)         if (!ok && G[i].size() == 3) {           int cnt = 0;           for (int u : G[i])             for (int k : G[u])               if (k != i && G[k].size() >= 2) cnt++;           if (cnt == 2) {             ok = true;             ans[i] = 4;             for (int u : G[i]) {               bool cc = false;               for (int k : G[u])                 if (k != i && G[k].size() >= 2) cc = true;               if (!cc) {                 ans[u] = 2;                 continue;               }               for (int k : G[u])                 if (k != i) {                   for (int p : G[k]) ans[p] = 1;                   ans[k] = 2;                 }               ans[u] = 3;             }             break;           }         }       for (int i = 0; i < n; i++)         if (!ok && G[i].size() == 2) {           bool can = true;           for (int u : G[i])             if (G[u].size() != 3) can = false;           if (can) {             ok = true;             for (int u : G[i]) {               for (int k : G[u]) ans[k] = 1;               ans[u] = 2;             }             ans[i] = 2;             break;           }         }       for (int i = 0; i < n; i++)         if (!ok && G[i].size() == 3) {           bool can = false;           int A, B;           for (int u : G[i])             for (int u2 : G[i])               if (u != u2) {                 if (G[u2].size() != 2) continue;                 for (int k1 : G[u])                   if (k1 != i)                     for (int k2 : G[k1])                       if (k2 != u)                         for (int k3 : G[k2])                           if (k3 != k1)                             for (int k4 : G[k3])                               if (k4 != k2) {                                 can = true;                                 A = u, B = u2;                               }               }           if (can) {             ok = true;             ans[i] = 6;             for (int k : G[i])               if (k != A && k != B) ans[k] = 3;             ans[B] = 4;             for (int k : G[B])               if (k != i) ans[k] = 2;             ans[A] = 5;             for (int k1 : G[A])               if (k1 != i) {                 ans[k1] = 4;                 for (int k2 : G[k1])                   if (k2 != A) {                     ans[k2] = 3;                     for (int k3 : G[k2])                       if (k3 != k1) {                         ans[k3] = 2;                         for (int k4 : G[k3])                           if (k4 != k2) ans[k4] = 1;                       }                   }               }             break;           }         }       for (int i = 0; i < n; i++) mark[i] = false;       for (int i = 0; i < n; i++)         if (!ok && !mark[i] && G[i].size() == 3) {           path.clear();           if (DFS(i)) {             ok = true;             for (int u : G[path[0]]) ans[u] = 1;             for (int u : G[path.back()]) ans[u] = 1;             for (int u : path) ans[u] = 2;             break;           }         }       if (ok) {         printf( YES n );         for (int i = 0; i < n; i++) printf( %d n , ans[i]);         printf(  n );       } else         printf( NO n );       refresh();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, in[200005], same[200005 * 4];   long long s0[200005 * 4], s1[200005 * 4], fib[200005];   long long f(long long f0, long long f1, int x) {     if (x == 0) return f0;     x -= 1;     long long mat[2][2] = {{f1, f0}, {f0, 0}};     long long mat2[2][2] = {{1, 1}, {1, 0}};     long long mat3[2][2];     while (x) {       if (x & 1) {         memset(mat3, 0, sizeof(mat3));         for (int i = 0; i <= 1; i++)           for (int j = 0; j <= 1; j++)             for (int k = 0; k <= 1; k++) {               mat3[i][j] += mat[i][k] * mat2[k][j];               mat3[i][j] %= 1000000000;             }         memcpy(mat, mat3, sizeof(mat3));       }       memset(mat3, 0, sizeof(mat3));       for (int i = 0; i <= 1; i++)         for (int j = 0; j <= 1; j++)           for (int k = 0; k <= 1; k++) {             mat3[i][j] += mat2[i][k] * mat2[k][j];             mat3[i][j] %= 1000000000;           }       memcpy(mat2, mat3, sizeof(mat3));       x >>= 1;     }     return mat[0][0];   }   long long fibsum(int l, int r) {     long long re = fib[r];     if (l) re -= fib[l - 1];     re = (re % 1000000000 + 1000000000) % 1000000000;     return re;   }   void upd(int cur, int ll, int rr) {     if (ll == rr) return;     int mid = (ll + rr) >> 1;     s0[cur] = s0[(cur << 1)] +               f(s0[((cur << 1) + 1)], s1[((cur << 1) + 1)], mid - ll + 1);     s1[cur] = s1[(cur << 1)] +               f(s0[((cur << 1) + 1)], s1[((cur << 1) + 1)], mid - ll + 2);     s0[cur] %= 1000000000;     s1[cur] %= 1000000000;   }   void init(int cur, int ll, int rr) {     if (ll == rr) {       s0[cur] = s1[cur] = in[ll];       return;     }     int mid = (ll + rr) >> 1;     init((cur << 1), ll, mid);     init(((cur << 1) + 1), mid + 1, rr);     upd(cur, ll, rr);   }   void fix(int cur, int ll, int rr, int x, int v) {     if (ll == rr) {       s0[cur] = s1[cur] = v;       return;     }     int mid = (ll + rr) >> 1;     if (x <= mid)       fix((cur << 1), ll, mid, x, v);     else       fix(((cur << 1) + 1), mid + 1, rr, x, v);     upd(cur, ll, rr);   }   long long query(int cur, int ll, int rr, int l, int r, int lf) {     if (ll == l && rr == r) return f(s0[cur], s1[cur], ll - lf);     int mid = (ll + rr) >> 1;     if (r <= mid)       return query((cur << 1), ll, mid, l, r, lf);     else if (l > mid)       return query(((cur << 1) + 1), mid + 1, rr, l, r, lf);     return (query((cur << 1), ll, mid, l, mid, lf) +             query(((cur << 1) + 1), mid + 1, rr, mid + 1, r, lf)) %            1000000000;   }   int main() {     int x, y, z;     fib[0] = fib[1] = 1;     for (int i = 2; i <= 200005 - 1; i++)       fib[i] = (fib[i - 1] + fib[i - 2]) % 1000000000;     for (int i = 1; i <= 200005 - 1; i++)       fib[i] = (fib[i] + fib[i - 1]) % 1000000000;     while (~scanf( %d %d , &n, &m)) {       for (int i = 1; i <= n; i++) scanf( %d , &in[i]);       init(1, 1, n);       while (m--) {         scanf( %d , &z);         if (z == 1) {           scanf( %d %d , &x, &y);           fix(1, 1, n, x, y);         } else if (z == 2) {           scanf( %d %d , &x, &y);           printf( %I64d n , query(1, 1, n, x, y, x));         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int x[100005];   int main() {     int n;     long long mn, mx, md, re, ans;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %I64d , &x[i]);     sort(x, x + n);     mn = x[n - 1];     mx = x[0] + x[n - 1];     while (mn <= mx) {       re = 0;       md = (mx + mn) / 2;       for (int i = 0; i < n; i++) re += (md - x[i]);       if (re >= md) {         ans = md;         mx = md - 1;       } else         mn = md + 1;     }     printf( %I64d , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k;     scanf( %d%d%d , &n, &m, &k);     vector<vector<int>> sum_points(n + m), diff_points(n + m);     vector<int> x(k), y(k);     for (int i = 0; i < k; ++i) {       scanf( %d%d , &x[i], &y[i]);       sum_points[x[i] + y[i]].push_back(i);       diff_points[x[i] - y[i] + m].push_back(i);     }     vector<long long> ans(k, -1);     long long time = 0;     int px = 0, py = 0, nx = 0, ny = 0;     int dx = 1, dy = 1;     while (dx) {       int xdiff = (dx > 0) ? n - px : px;       int ydiff = (dy > 0) ? m - py : py;       int diff = min(xdiff, ydiff);       nx = px + diff * dx;       ny = py + diff * dy;       if (dx * dy > 0) {         for (int i : diff_points[nx - ny + m])           if (ans[i] == -1) ans[i] = time + abs(x[i] - px);       } else {         for (int i : sum_points[nx + ny])           if (ans[i] == -1) ans[i] = time + abs(x[i] - px);       }       if (xdiff < ydiff) dx *= -1;       if (xdiff > ydiff) dy *= -1;       if (xdiff == ydiff) dx = 0;       px = nx;       py = ny;       time += diff;     }     for (long long t : ans) printf( %lld n , t);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, i, j, a, b;   inline bool valid(int x, int y, int &mx) {     if (!(x % a) && !(y % b)) {       int a1 = x / a;       int b1 = y / b;       if (!((a1 + b1) % 2)) {         if (!a1 && b1 && n <= a || !b1 && a1 && m <= b) return false;         int r = a1 > b1 ? a1 : b1;         if (mx == -1 || mx > r) {           mx = r;           return true;         }       }     }     return false;   }   int main() {     cin >> n >> m >> i >> j >> a >> b;     int x, y, d = -1;     x = i - 1;     y = m - j;     valid(x, y, d);     x = n - i;     y = j - 1;     valid(x, y, d);     x = n - i;     y = m - j;     valid(x, y, d);     x = i - 1;     y = j - 1;     valid(x, y, d);     if (d == -1)       cout <<  Poor Inna and pony!  << endl;     else       cout << d << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2>   inline ostream &operator<<(ostream &os, const pair<T1, T2> &p) {     return os <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename T, size_t N>   inline ostream &operator<<(ostream &os, const array<T, N> &a) {     os <<  [ ;     int cnt = 0;     for (auto &val : a) {       if (cnt++) os <<  ,  ;       os << val;     }     os <<  ] ;     return os;   }   template <typename T>   inline ostream &operator<<(ostream &os, const vector<T> &v) {     os <<  [ ;     int cnt = 0;     for (auto &val : v) {       if (cnt++) os <<  ,  ;       os << val;     }     return os <<  ] ;   }   template <typename T>   inline ostream &operator<<(ostream &os, const set<T> &v) {     os <<  { ;     int cnt = 0;     for (auto &val : v) {       if (cnt++) os <<  ,  ;       os << val;     }     return os <<  } ;   }   template <typename T>   inline ostream &operator<<(ostream &os, const unordered_set<T> &v) {     os <<  { ;     int cnt = 0;     for (auto &val : v) {       if (cnt++) os <<  ,  ;       os << val;     }     return os <<  } ;   }   template <typename T>   inline ostream &operator<<(ostream &os, const multiset<T> &v) {     os <<  { ;     int cnt = 0;     for (auto &val : v) {       if (cnt++) os <<  ,  ;       os << val;     }     return os <<  } ;   }   template <typename T1, typename T2>   inline ostream &operator<<(ostream &os, const map<T1, T2> &v) {     os <<  [ ;     int cnt = 0;     for (auto &val : v) {       if (cnt++) os <<  ,  ;       os << val;     }     return os <<  ] ;   }   template <typename Arg1>   void __f(const char *name, Arg1 &&arg1) {     cerr << name <<  :   << arg1 << endl;   }   template <typename Arg1, typename... Args>   void __f(const char *names, Arg1 &&arg1, Args &&...args) {     const char *comma = strchr(names + 1,  , );     cerr.write(names, comma - names) <<  :   << arg1 <<   | ;     __f(comma + 1, args...);   }   using ll = long long;   using ull = unsigned long long;   using vi = vector<int>;   using vll = vector<long long>;   using vvi = vector<vector<int>>;   using vs = vector<string>;   using vb = vector<bool>;   using vd = vector<double>;   using vvb = vector<vector<bool>>;   using vvc = vector<vector<char>>;   using vc = vector<char>;   using si = set<int>;   using mpii = map<int, int>;   using pii = pair<int, int>;   using pll = pair<ll, ll>;   const char nl =   n ;   const int dx[] = {-1, 0, 1, 0};   const int dy[] = {0, 1, 0, -1};   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   template <class T>   inline bool umin(T &a, const T b) {     return b < a ? a = b, 1 : 0;   }   template <class T>   inline bool umax(T &a, const T b) {     return a < b ? a = b, 1 : 0;   }   ll solve() {     int n;     cin >> n;     vector<pii> t(n);     vi a(n + 1, 0), b(n + 1, 0);     for (int i = 0; i < (n); i++) {       cin >> t[i].first >> t[i].second;       a[t[i].first]++;       b[t[i].second]++;     }     ll ans = 1LL * n * (n - 1) * (n - 2) / 6;     for (int i = 0; i < (n); i++) {       ans -= 1LL * (a[t[i].first] - 1) * (b[t[i].second] - 1);     }     return ans;   }   void solve(int _cas) { cout << solve() << endl; }   int main() {     srand(time(NULL));     ios_base::sync_with_stdio(false);     cin.tie(NULL), cout.tie(NULL), cout.precision(12);     int cas;     cin >> cas;     for (int i = 0; i < (cas); i++) solve(i + 1);   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long ans, sum[300030], cnt[300030], tot[3];   int main() {     scanf( %d , &n);     tot[0] = 1;     for (int i = 1; i <= n; i++) {       long long x;       scanf( %lld , &x);       while (x) {         cnt[i] += x % 2;         x >>= 1;       }       sum[i] = sum[i - 1] + cnt[i];       ans += tot[sum[i] % 2];       for (int j = i, k = i + 1, maxx = 0; j >= 1 && j >= i - 64; j--) {         while (k > j) maxx = max(1ll * maxx, cnt[k - 1]), k--;         if (maxx * 2 > sum[i] - sum[j - 1] && ((sum[i] % 2) == (sum[j - 1] % 2)))           ans--;       }       ++tot[sum[i] % 2];     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int N = 1e5 + 5;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int n, m;     cin >> n >> m;     vector<long long int> G[N];     for (long long int i = 0; i < m; ++i) {       long long int a, b;       cin >> a >> b;       G[a].push_back(b);     }     long long int rnd[N];     for (long long int i = 1; i < n + 1; ++i) {       long long int z = G[i].size();       rnd[i] = z - 1;     }     long long int far[N];     for (long long int i = 1; i < n + 1; ++i) {       far[i] = N * N * N;       if (rnd[i] >= 0) {         for (long long int j = 0; j < G[i].size(); ++j) {           long long int x = G[i][j];           if (x > i)             far[i] = min(x - i, far[i]);           else             far[i] = min(n - i + x, far[i]);         }       }       if (G[i].size() == 0) far[i] = 0;     }     for (long long int i = 1; i < n + 1; ++i) {       long long int mx = 0;       for (long long int j = 1; j < n + 1; ++j) {         long long int s = 0;         if (rnd[j] > 0 || far[j] > 0) {           long long int z = j - i;           if (j < i) z = n - i + j;           s = z + rnd[j] * n + far[j];         }         mx = max(s, mx);       }       cout << mx <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   int main() {     int t, n;     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       for (int i = 1; i <= n; ++i) printf( %d%c , n - i + 1,    n [i == n]);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long t, i, j, k, l, r;     cin >> t;     while (t--) {       cin >> l >> r;       if (l == r) {         if (l % 2 == 0)           cout << l << endl;         else           cout <<  -  << l << endl;       } else if (l % 2 == 1 && r % 2 == 1) {         cout <<  -  << (l + r) / 2 << endl;       } else if (l % 2 == 0 && r % 2 == 0) {         cout << (l + r) / 2 << endl;       } else if (l % 2 == 0 && r % 2 == 1) {         cout <<  -  << (r - l + 1) / 2 << endl;       } else         cout << (r - l + 1) / 2 << endl;     }   }


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


#include <bits/stdc++.h>   using namespace std;   vector<pair<pair<long long, long long>, int> > island;   set<pair<long long, long long> > bridge;   int ans[200100];   int main() {     int n, m;     long long l, r, prevl, prevr, a;     cin >> n >> m;     cin >> prevl >> prevr;     for (int i = 1; i < n; i++) {       cin >> l >> r;       island.push_back(pair<pair<long long, long long>, int>(           pair<long long, long long>(r - prevl, l - prevr), i - 1));       prevl = l;       prevr = r;     }     sort(island.begin(), island.end());     for (int i = 0; i < m; i++) {       cin >> a;       bridge.insert(pair<long long, long long>(a, i + 1));     }     bridge.insert(pair<long long, long long>(1e19, -1));     set<pair<long long, long long> >::iterator point;     long long lastl, lastr;     int idx;     for (vector<pair<pair<long long, long long>, int> >::iterator it =              island.begin();          it != island.end(); ++it) {       lastr = it->first.first;       lastl = it->first.second;       idx = it->second;       point = bridge.lower_bound(pair<long long, long long>(lastl, -1));       if (point->first > lastr) {         cout <<  No ;         return 0;       } else {         ans[idx] = point->second;         bridge.erase(point);       }     }     cout <<  Yes n ;     for (int i = 0; i < n - 1; i++) {       cout << ans[i] <<    ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 5, inf = 1e9;   int n, kk, s, x, k[N][2];   char ch[N];   int fa[N << 1], val[N << 1];   int find(int u) { return fa[u] ? fa[u] = find(fa[u]) : u; }   void uni(int a, int b) {     if (find(a) == find(b)) return;     val[find(b)] += val[find(a)];     fa[find(a)] = find(b);   }   int mix(int a) { return min(val[find(a)], val[find(a + kk)]); };   int main() {     scanf( %d%d%s , &n, &kk, ch + 1);     for (int i = 1; i <= kk; i++) {       scanf( %d , &s);       while (s--) {         scanf( %d , &x);         k[x][bool(k[x][0])] = i;       }     }     for (int i = 1; i <= kk; i++) val[i] = 1;     val[kk + kk + 1] = inf;     int ans = 0;     for (int i = 1; i <= n; i++) {       if (k[i][0] == 0) goto qwq;       if (ch[i] ==  1 ) {         if (k[i][1]) {           if (find(k[i][0]) == find(k[i][1])) goto qwq;           ans -= mix(k[i][0]) + mix(k[i][1]);           uni(k[i][0], k[i][1]), uni(k[i][0] + kk, k[i][1] + kk);           ans += mix(k[i][1]);         } else {           ans -= mix(k[i][0]);           uni(k[i][0], kk + kk + 1);           ans += val[find(k[i][0] + kk)];         }       } else {         if (k[i][1]) {           if (find(k[i][0]) == find(k[i][1] + kk)) goto qwq;           ans -= mix(k[i][0]) + mix(k[i][1]);           uni(k[i][0], k[i][1] + kk), uni(k[i][0] + kk, k[i][1]);           ans += mix(k[i][0]);         } else {           ans -= mix(k[i][0]);           uni(k[i][0] + kk, kk + kk + 1);           ans += mix(k[i][0]);         }       }     qwq:       printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 10, mod = 998244353;   long long jc[N] = {1}, ny[N];   void Add(long long &x, long long y) { x = (x + y + mod) % mod; }   long long q_pow(long long x, long long y) {     long long ans = 1;     for (; y; y >>= 1) {       if (y & 1) ans = ans * x % mod;       x = x * x % mod;     }     return ans;   }   long long C(int x, int y) { return jc[x] * ny[y] % mod * ny[x - y] % mod; }   int main() {     long long n;     scanf( %lld , &n);     ny[0] = 1;     for (int i = 1; i <= n; ++i)       jc[i] = jc[i - 1] * i % mod, ny[i] = q_pow(jc[i], mod - 2);     long long sum = 0;     long long kk = -1;     for (int i = 1; i <= n; ++i) {       kk = -kk;       Add(sum, kk * C(n, i) % mod * q_pow(3, n * n - i * n + i) % mod);     }     Add(sum, sum);     kk = 1;     for (int i = 1; i <= n; ++i) {       kk = -kk;       long long pos =           (q_pow(3, n * (n - i)) - q_pow(q_pow(3, n - i) - 1, n) + mod) % mod;       long long tmp = kk * C(n, i) % mod * 3LL % mod * pos % mod;       Add(sum, tmp);     }     printf( %lld n , sum);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   int getbit(T s, int i) {     return (s >> i) & 1;   }   template <class T>   T onbit(T s, int i) {     return s | (T(1) << i);   }   template <class T>   T offbit(T s, int i) {     return s & (~(T(1) << i));   }   template <class T>   int cntbit(T s) {     return __builtin_popcount(s);   }   template <class T>   T gcd(T a, T b) {     T r;     while (b != 0) {       r = a % b;       a = b;       b = r;     }     return a;   }   template <class T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   multiset<string> S;   long long n, a[3005];   long long f[3005][3005];   long long F[3005];   void update(int u, long long T) {     for (int i = u; i < 3005; i += i & -i) {       F[i] = max(F[i], T);     }   }   long long get(int u) {     long long res = (1ll << 60);     for (int i = u; i >= 0; i--) {       res = min(res, F[i]);       if (i == 0) break;     }     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     vector<long long> V;     cin >> n;     for (int i = (1); i <= (n); ++i) {       cin >> a[i];       a[i] += n - i;     }     V.push_back(-1000000005);     for (int i = (1); i <= (n); ++i) V.push_back(a[i]);     sort((V).begin(), (V).end());     f[0][0] = 0;     for (int i = (1); i <= (n); ++i) {       long long T = (1ll << 60);       for (int j = (0); j <= (n); ++j) {         T = min(T, f[i - 1][j]);         f[i][j] = T + abs(a[i] - V[j]);       }     }     long long res = (1ll << 60);     for (int i = (1); i <= (n); ++i) res = min(res, f[n][i]);     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void sf(int &x) { scanf( %d , &x); }   void sf(long long &x) { scanf( %lld , &x); }   void sf(float &x) { scanf( %f , &x); }   void sf(double &x) { scanf( %lf , &x); }   void sf(int &x, int &y) { scanf( %d%d , &x, &y); }   void sf(long long &x, long long &y) { scanf( %lld%lld , &x, &y); }   void sf(float &x, float &y) { scanf( %f%f , &x, &y); }   void sf(double &x, double &y) { scanf( %lf%lf , &x, &y); }   void sf(double &x, double &y, double &z) { scanf( %lf%lf%lf , &x, &y, &z); }   void sf(int &x, int &y, int &z) { scanf( %d%d%d , &x, &y, &z); }   void sf(long long &x, long long &y, long long &z) {     scanf( %lld%lld%lld , &x, &y, &z);   }   void sf(float &x, float &y, float &z) { scanf( %u%u%u , &x, &y, &z); }   void sf(char &x) { x = getchar(); }   void sf(char *s) { scanf( %s , s); }   void sf(string &s) { cin >> s; }   void sf(string &s, string &ss) { cin >> s >> ss; }   void pf(int x) { printf( %d n , x); }   void pf(int x, int y) { printf( %d %d n , x, y); }   void pf(long long x) { printf( %lld n , x); }   void pf(long long x, long long y) { printf( %lld %lld n , x, y); }   void pf(float x) { printf( %u n , x); }   void pf(unsigned long long x) { printf( %llu n , x); }   void pf(double x) { printf( %.6lf n , x); }   void pf(double x, double y) { printf( %.5lf %.5lf n , x, y); }   void pf(char x) { printf( %c n , x); }   void pf(char *x) { printf( %s n , x); }   void pf(string x) {     cout << x;     puts(  );   }   void pf(int x, int y, int z) { printf( %d %d %d n , x, y, z); }   template <class T>   T bigmod(T b, T p, T m) {     if (p == 0) return 1 % m;     T x = b;     T ans = 1;     while (p) {       if (p & 1) ans = (ans * x) % m;       p >>= 1;       x = (x * x) % m;     }     return ans;   }   template <class T>   T gcd(T x, T y) {     if (y == 0) return x;     return gcd(y, x % y);   }   template <typename T>   T POW(T b, T p) {     if (p == 1) return b;     if (p % 2 == 0) {       T s = POW(b, p / 2);       return s * s;     }     return b * POW(b, p - 1);   }   template <typename T>   T modinv(T num, T m) {     return bigmod(num, m - 2, m);   }   template <typename T>   long long triangle(T a, T b, T c) {     return (c.x - a.x) * (b.y - a.y) - (b.x - a.x) * (c.y - a.y);   }   int a, b, c, l[1009];   string s[1009];   int main() {     while (getline(cin, s[a])) {       l[a] = s[a].size();       b = max(b, l[a]);       a++;     }     for (int i = 0; i < b + 2; i++) cout <<  * ;     puts(  );     c = 0;     for (int i = 0; i < a; i++) {       cout <<  * ;       int kk = b - l[i];       if (kk & 1) {         if (c == 0) {           int k = kk / 2;           int j = kk - k;           for (int m = 0; m < k; m++) cout <<    ;           cout << s[i];           for (int m = 0; m < j; m++) cout <<    ;           cout <<  * ;         } else {           int k = kk / 2;           int j = kk - k;           for (int m = 0; m < j; m++) cout <<    ;           cout << s[i];           for (int m = 0; m < k; m++) cout <<    ;           cout <<  * ;         }       } else {         int k = kk / 2;         int j = kk - k;         for (int m = 0; m < j; m++) cout <<    ;         cout << s[i];         for (int m = 0; m < k; m++) cout <<    ;         cout <<  * ;       }       puts(  );       if (kk & 1) {         if (c == 0)           c = 1;         else           c = 0;       }     }     for (int i = 0; i < b + 2; i++) cout <<  * ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 2147483647;   const long long LLINF = 9223372036854775807LL;   const int ST = 100010;   const int ST1 = 1000010;   const long long MOD = 1000000007;   long long ABS(long long a) {     if (a < 0)       return a * (-1);     else       return a;   }   int mas[30];   bool ans = true;   void bad() {     cout <<  NO ;     exit(0);   }   vector<vector<int> > ppp;   int sz = 0;   void per(vector<int> &t) {     if ((int)((t).size()) == sz) {       ppp.push_back(t);     } else {       t.push_back(0);       per(t);       t.pop_back();       t.push_back(1);       per(t);       t.pop_back();     }   }   void per(vector<int> val, int ed, vector<int> zap) {     if ((int)((val).size()) == 0) {       if (ed == 0) {         cout <<  YES ;         exit(0);       } else         return;     }     vector<vector<int> > pp;     vector<int> em;     sz = (int)((zap).size());     ppp.clear();     per(em);     pp = ppp;     int tek = val[(int)((val).size()) - 1] - 1;     val.pop_back();     int ost = 0;     for (int i = 0; i < (int)((pp).size()); i++) {       vector<int> next;       next.push_back(tek + 1);       ost = 0;       int kk = 0;       for (int j = 0; j < (int)((pp[i]).size()); j++) {         if (pp[i][j] == 1) {           ost += zap[j];           kk++;         } else           next.push_back(zap[j]);       }       if (ost <= tek) {         int s = tek - ost;         if (s > ed) continue;         kk += s;         if (kk == 1) continue;         per(val, ed - s, next);       }     }   }   int main() {     int n;     cin >> n;     if (n == 1) {       cout <<  YES ;       return 0;     }     int kk = 0;     for (int i = 0; i < n; i++) {       cin >> mas[i];       if (mas[i] == 2) bad();       if (mas[i] == 1) kk++;     }     sort(mas, mas + n);     int mn = n / 2;     if (kk < mn) bad();     vector<int> t;     int nol = 0;     for (int i = 0; i < n; i++)       if (mas[i] != 1)         t.push_back(mas[i]);       else         nol++;     vector<int> em;     reverse(t.begin(), t.end());     per(t, nol, em);     cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 5;   const long long mod = 998244353;   const double pi = acos(-1);   long long fastpow(long long b, long long e) {     if (!e) return 1;     long long ret = fastpow(b, e / 2);     ret = (ret * ret) % mod;     if (e % 2) ret = (ret * b) % mod;     return ret;   }   long long modinv(long long x) { return fastpow(x, mod - 2); }   double torad(double ang) { return ang * pi / 180.0; }   int k, n, m;   string s[15][15];   bool valid(int x, int y, int z) {     if (x >= k || y >= n || z >= m || x < 0 || y < 0 || z < 0) return 0;     return 1;   }   void dfs(int x, int y, int z) {     if (!valid(x, y, z)) return;     if (s[x][y][z] ==  $  || s[x][y][z] ==  # ) return;     s[x][y][z] =  $ ;     dfs(x + 1, y, z);     dfs(x - 1, y, z);     dfs(x, y + 1, z);     dfs(x, y - 1, z);     dfs(x, y, z + 1);     dfs(x, y, z - 1);   }   int main() {     scanf( %d%d%d , &k, &n, &m);     for (int i = 0; i < k; i++) {       for (int j = 0; j < n; j++) cin >> s[i][j];     }     int x, y;     scanf( %d%d , &x, &y);     dfs(0, x - 1, y - 1);     int cntr = 0;     for (int i = 0; i < k; i++) {       for (int j = 0; j < n; j++) {         for (int l = 0; l < m; l++) {           if (s[i][j][l] ==  $ ) cntr++;         }       }     }     printf( %d , cntr);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   int q, num;   vector<int> v[maxn];   bool vis[maxn];   void dfs(int x) {     for (int i = 0; i < v[x].size(); i++) {       if (!vis[v[x][i]]) {         num++;         vis[v[x][i]] = 1;         dfs(v[x][i]);       }     }   }   int main() {     ios::sync_with_stdio(false);     cin >> q;     while (q--) {       int n, m, a, b;       long long ans;       cin >> n >> m >> a >> b;       for (int i = 1; i <= n; i++) {         v[i].clear();         vis[i] = 0;       }       for (int i = 1; i <= m; i++) {         int x, y;         cin >> x >> y;         v[x].push_back(y);         v[y].push_back(x);       }       vis[b] = vis[a] = 1, num = 0;       dfs(a);       ans = n - 2 - num;       fill(vis + 1, vis + 1 + n, 0);       vis[b] = vis[a] = 1, num = 0;       dfs(b);       ans *= (n - 2) - num;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void fastInOut() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);   }   const int size = 300000 + 5;   void solve() {     long long n;     cin >> n;     long long ans = 0;     long long x = 1;     long long ctr = 0;     long long till_now = 0;     while (till_now < n) {       ans++;       till_now += (x * (x + 1) / 2);       ctr++;       x += (1 << ctr);     }     if (till_now > n) ans--;     cout << ans << endl;   }   int main(int argc, char const* argv[]) {     fastInOut();     int t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100100;   int n;   vector<pair<int, int>> a;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       int x, y;       scanf( %d%d , &x, &y);       a.emplace_back(x, 1);       a.emplace_back(y + 1, -1);     }     sort(a.begin(), a.end());     int k = 0;     for (auto p : a) {       k += p.second;       if (k > 2) {         puts( NO );         return 0;       }     }     puts( YES );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, t;   int a[10010];   map<int, int> calc;   int wow;   int LIS() {     int len = 0;     int d[10010];     d[len] = -0x7fffffff;     for (int i = 1; i <= wow; i++) {       if (d[len] <= a[i])         d[++len] = a[i];       else {         int mid = upper_bound(d + 1, d + 1 + len, a[i]) - d;         d[mid] = a[i];       }     }     return len;   }   int main() {     cin >> n >> t;     for (int i = 1; i <= n; i++) cin >> a[i];     if (t < n) {       for (int i = 1; i < t; i++)         for (int j = 1; j <= n; j++) a[i * n + j] = a[j];       wow = n * t;       cout << LIS();     } else {       for (int i = 1; i < n; i++)         for (int j = 1; j <= n; j++) a[i * n + j] = a[j];       wow = n * n;       int sth = LIS();       int maxi = 0, num;       for (int i = 1; i <= n; i++) calc[a[i]]++;       for (map<int, int>::iterator i = calc.begin(); i != calc.end(); i++)         maxi = max(maxi, i->second);       cout << sth + maxi * (t - n);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2010;   int N, M;   char A[MAXN][MAXN];   int cnt[MAXN];   int main() {     scanf( %d %d , &N, &M);     for (int i = 0; i < N; i++) {       scanf( %s , A[i]);       for (int j = 0; j < M; j++) {         if (A[i][j] ==  1 ) {           cnt[j]++;         }       }     }     for (int i = 0; i < N; i++) {       bool need = false;       for (int j = 0; j < M; j++) {         if (A[i][j] ==  1  && cnt[j] == 1) {           need = true;           break;         }       }       if (!need) {         puts( YES );         return 0;       }     }     puts( NO );   }


#include <bits/stdc++.h>   using namespace std;   int main() {     double a, d;     int n;     cin >> a >> d >> n;     for (int i = 0; i < n; i++) {       double temp = fmod((i + 1) * d, 4 * a);       if (temp < a)         printf( %.9f %.9f n , temp, 0.0);       else if (temp < a * 2)         printf( %.9f %.9f n , a, temp - a);       else if (temp < a * 3)         printf( %.9f %.9f n , 3 * a - temp, a);       else         printf( %.9f %.9f n , 0.0, 4 * a - temp);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1000002], b[4000000], intialsum[1000003];   int q(int ind) {     int sum = 0;     while (ind > 0) {       sum += b[ind];       ind -= ind & (-ind);     }     return sum;   }   void u(int ind, int n, int val) {     while (ind <= n) {       b[ind] += val;       ind += ind & (-ind);     }   }   int main() {     {       ios_base::sync_with_stdio(false);       cin.tie(0);     };     int i, j, k, l, n, m, t, h;     cin >> n >> k;     for (i = 1; i <= n; i++) {       cin >> j;       a[j]++;     }     for (i = 1; i <= n; i++) intialsum[i] = intialsum[i - 1] + a[i];     for (i = 1; i <= k; i++) {       cin >> j;       if (j > 0) {         a[j]++;         u(j, n, 1);       } else {         l = 1;         h = n;         while (h > l) {           m = (l + h) >> 1;           if (intialsum[m] + q(m) >= -j)             h = m;           else             l = m + 1;         }         a[l]--;         u(l, n, -1);       }     }     for (i = 1; i <= n; i++)       if (a[i] > 0) return cout << i << endl, 0;     cout << 0 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1000005;   long long res[MAX], a[MAX], b[MAX];   void add(int l, int r, int x, int y) {     a[l] += x;     a[r] -= x + y * (r - l);     b[l] += y;     b[r] -= y;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     for (int from = 0; from < n; from++) {       int to;       cin >> to;       to--;       if (from <= to) {         add(0, to - from + 1, to - from, -1);         add(to - from + 1, n - from, 1, 1);         add(n - from, n, to, -1);       } else {         add(0, n - from, from - to, 1);         add(n - from, n - from + to + 1, to, -1);         add(n - from + to + 1, n, 1, 1);       }     }     long long val = 0, step = 0;     for (int i = 0; i < n; i++) {       val += a[i];       step += b[i];       res[i] = val;       val += step;     }     int mn = min_element(res, res + n) - res;     cout << res[mn] <<     << mn <<   n ;     return 0;   }


#include <bits/stdc++.h>   const double eps = 1e-9;   const int MOD = int(1e9) + 7;   const int MaxN = int(2e5) + 256;   using namespace std;   int n, a, b;   int main() {     cin >> n >> a >> b;     if (n > a * b) {       cout << -1;       return 0;     }     int cnt = 1;     bool ok = true, r = true;     for (int i = 1; i <= a; i++) {       if (r) {         for (int j = 1; j <= b; j++) {           if (cnt <= n)             cout << cnt++ <<    ;           else             cout << 0 <<    ;         }         r = false;       } else {         cnt += b - 1;         for (int j = 1; j <= b; j++) {           if (cnt <= n)             cout << cnt <<    ;           else             cout << 0 <<    ;           cnt--;         }         cnt += b + 1;         r = true;       }       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   long long qpow(long long a, long long b, long long m) {     long long r = 1;     a %= m;     for (; b; b >>= 1) {       if (b & 1) r = r * a % m;       a = a * a % m;     }     return r;   }   const int inf = 0x7fffffff;   const int maxn = 2e5 + 10;   vector<pair<int, int> > g[maxn];   vector<int> ans;   set<pair<int, int> > s;   int n, k;   int main() {     ios::sync_with_stdio(false);     cin >> n >> k;     for (int i = 1; i <= n; i++) {       int l, r;       cin >> l >> r;       g[l].push_back(pair<int, int>(r, i));     }     for (int i = 1; i <= 200000; i++) {       for (int j = 0; j < (int)(g[i]).size(); j++) s.insert(g[i][j]);       while ((int)(s).size() > k) {         set<pair<int, int> >::iterator it = s.end();         it--;         ans.push_back(it->second);         s.erase(it);       }       while (!s.empty() && (s.begin()->first) <= i) s.erase(s.begin());     }     cout << (int)(ans).size() << endl;     for (int i = 0; i < (int)(ans).size(); i++) {       if (i) cout <<    ;       cout << ans[i];     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int sz = 1e5 + 5;   vector<tuple<int, int, int> > q;   vector<int> FWT[sz];   int mt[sz];   vector<int> mapt[sz];   void update(int idx, int v, int pn) {     vector<int>& x = FWT[v];     while (idx < x.size()) {       x[idx] += pn;       idx = idx + (idx & -idx);     }   }   int query(int idx, int v) {     vector<int>& x = FWT[v];     int ans = 0;     while (idx > 0) {       ans += x[idx];       idx = idx - (idx & -idx);     }     return ans;   }   int main() {     int n;     cin >> n;     map<int, int> values;     int vv = 1;     for (int i = 0; i < n; ++i) {       tuple<int, int, int> x;       cin >> get<0>(x) >> get<1>(x) >> get<2>(x);       if (values[get<2>(x)] == 0) {         values[get<2>(x)] = vv++;       }       get<2>(x) = values[get<2>(x)];       q.push_back(x);       mapt[get<2>(x)].push_back(get<1>(x));     }     for (int i = 1; i < vv; ++i) {       sort(mapt[i].begin(), mapt[i].end());       FWT[i].resize(unique(mapt[i].begin(), mapt[i].end()) - mapt[i].begin() + 2);     }     for (int i = 0; i < n; ++i) {       int a = get<0>(q[i]);       int t = get<1>(q[i]);       int x = get<2>(q[i]);       t = lower_bound(mapt[x].begin(), mapt[x].end(), t) - mapt[x].begin() + 1;       if (a == 1) {         update(t, x, 1);       } else if (a == 2) {         update(t, x, -1);       } else {         cout << query(t, x) << endl;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 20;   int t;   int n, s;   string str;   char n1[N];   char n2[N];   int a[N], b[N], c[N];   int main() {     cin >> t;     while (t--) {       cin >> str >> s;       int sum = 0, k = -1, pos = -1, flag = 1;       for (int i = 0; i < str.size(); i++) {         sum += str[i] -  0 ;         if (sum == s && flag)           pos = i, flag = 0;         else if (sum > s) {           if (pos != -1)             k = pos;           else             k = i;           break;         }       }       if (sum <= s || (pos != -1 && k == -1)) {         cout << 0 << endl;         continue;       }       memset(c, 0, sizeof c);       memset(n2, 0, sizeof n2);       memset(n1, 0, sizeof n1);       memset(a, 0, sizeof a);       memset(b, 0, sizeof b);       for (int i = k; i < str.size(); i++) n2[i - k] = str[i];       n1[0] =  1 ;       for (int i = 1; i <= strlen(n2); i++) n1[i] =  0 ;       int lena = strlen(n1);       int lenb = strlen(n2);       for (int i = 0; i <= lena - 1; i++) a[lena - i] = int(n1[i] -  0 );       for (int i = 0; i <= lenb - 1; i++) b[lenb - i] = int(n2[i] -  0 );       int i = 1;       while (i <= lena || i <= lenb) {         if (a[i] < b[i]) {           a[i] += 10;           a[i + 1]--;         }         c[i] = a[i] - b[i];         i++;       }       int lenc = i;       while ((c[lenc] == 0) && (lenc > 1)) lenc--;       for (i = lenc; i >= 1; i--) cout << c[i];       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<int, int> w, ww;   set<int> second[222222];   int h, m, n, x, u, uu, e, A[311111], B[311111], g, vv[222222], os[222222],       oo[222222], fl[222222];   vector<int> v[211111], s[211111];   char o;   long long ans;   int main() {     scanf( %d%d%d n , &h, &m, &n);     for (int i = 0; i < h; i++)       if (!A[i]) {         x = i;         A[i] = ++e;         B[i] = 0;         v[e].push_back(i);         x = (x + m) % h;         while (x != i) {           B[x] = v[e].size();           v[e].push_back(x);           A[x] = e;           x = (x + m) % h;         }       }     for (int i = 1; i <= e; i++)       for (int j = 0; j < v[i].size(); j++) second[i].insert(j);     for (int i = 0; i < n; i++) {       scanf( %c , &o);       if (o ==  - ) {         scanf( %d n , &x);         second[w[x]].insert(ww[x]);       } else {         scanf( %d%d n , &x, &g);         u = A[g];         uu = B[g];         if (second[u].lower_bound(uu) == second[u].end())           ans += v[u].size() - uu, uu = 0;         int z = *second[u].lower_bound(uu);         second[u].erase(z);         ans += z - uu;         w[x] = u;         ww[x] = z;       }     }     cout << (ans) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 101;   int n, m;   string s[MAXN];   int cnt[27];   bool toDel[MAXN][MAXN];   string solve() {     for (int i = 0; i < n; i++) {       memset(cnt, 0, sizeof(cnt));       for (int j = 0; j < m; j++) cnt[s[i][j] -  a ]++;       for (int j = 0; j < m; j++)         if (cnt[s[i][j] -  a ] > 1) toDel[i][j] = true;     }     for (int i = 0; i < m; i++) {       memset(cnt, 0, sizeof(cnt));       for (int j = 0; j < n; j++) cnt[s[j][i] -  a ]++;       for (int j = 0; j < n; j++)         if (cnt[s[j][i] -  a ] > 1) toDel[j][i] = true;     }     string ans =   ;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (not toDel[i][j]) ans += s[i][j];     return ans;   }   int main() {     scanf( %d %d , &n, &m);     for (int i = 0; i < n; i++) {       cin >> s[i];     }     cout << solve() << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[755];   long long dp[755][755];   int n, m;   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> a[i];     dp[n + 1][0] = 0;     for (int i = n; i >= 1; i--) {       for (int j = 0; j <= n; j++) {         dp[i][j] = min(dp[i + 1][j] + a[i], (long long)0);         if (j > 0) dp[i][j] = max(dp[i][j], dp[i + 1][j - 1]);       }     }     while (m--) {       long long x;       cin >> x;       cout << lower_bound(dp[1], dp[1] + n + 1, -x) - dp[1] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace fft {   const int mod = 7340033;   const int root = 5;   const int root_1 = 4404020;   const int root_pw = 1 << 20;   template <class T>   T eea(T a, T b, T& x, T& y) {     if (!b) {       x = 1;       y = 0;       return a;     }     T x1, y1;     T g = eea(b, a % b, x1, y1);     x = y1;     y = x1 - (a / b) * y1;     return g;   }   int inverse(int n) {     int x, y;     eea(n, mod, x, y);     x %= mod;     if (x < 0) x += mod;     return x;   }   void fft(vector<int>& a, bool invert) {     int n = a.size();     for (int i = 1, j = 0; i < n; i++) {       int bit = n >> 1;       for (; j & bit; bit >>= 1) j ^= bit;       j ^= bit;       if (i < j) swap(a[i], a[j]);     }     for (int len = 2; len <= n; len <<= 1) {       int wlen = invert ? root_1 : root;       for (int i = len; i < root_pw; i <<= 1) wlen = 1LL * wlen * wlen % mod;       for (int i = 0; i < n; i += len) {         int w = 1;         for (int j = 0; j < len / 2; j++) {           int u = a[i + j], v = (int)(1LL * a[i + j + len / 2] * w % mod);           a[i + j] = u + v < mod ? u + v : u + v - mod;           a[i + j + len / 2] = u - v >= 0 ? u - v : u - v + mod;           w = 1LL * w * wlen % mod;         }       }     }     if (invert) {       int n_1 = inverse(n);       for (int& x : a) x = 1LL * x * n_1 % mod;     }   }   }  // namespace fft   int order(int n) {     int z = 0;     while (n % 2 == 1 && n > 1) {       n >>= 1;       z++;     }     return z;   }   vector<int> p[32];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cerr.tie(nullptr);     p[0] = p[1] = vector<int>(4096, 0);     p[0][0] = 1;     p[1][0] = p[1][1] = 1;     for (int i = 2; i < 32; i++) {       vector<int> q = p[i - 1];       fft::fft(q, false);       for (int i = 0; i < 4096; i++) {         q[i] = q[i] * 1ll * q[i] % fft::mod;         q[i] = q[i] * 1ll * q[i] % fft::mod;       }       fft::fft(q, true);       fill(q.begin() + 1005, q.end(), 0);       rotate(q.begin(), q.begin() + 1004, q.begin() + 1005);       q[0] = 1;       swap(p[i], q);     }     int q;     cin >> q;     while (q--) {       int n, k;       cin >> n >> k;       cout << p[order(n)][k] <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int max_n = 111, inf = 1111111111;   int n, m, q, ans[max_n][max_n];   pair<int, int> a[max_n][max_n];   int main() {     scanf( %d%d%d , &n, &m, &q);     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         a[i][j] = make_pair(i, j);       }     }     while (q--) {       int tp;       scanf( %d , &tp);       if (tp <= 2) {         int x;         scanf( %d , &x);         --x;         if (tp == 1) {           pair<int, int> k = a[x][0];           for (int i = 0; i + 1 < m; ++i) {             a[x][i] = a[x][i + 1];           }           a[x][m - 1] = k;         } else {           pair<int, int> k = a[0][x];           for (int i = 0; i + 1 < n; ++i) {             a[i][x] = a[i + 1][x];           }           a[n - 1][x] = k;         }       } else {         int x, y, q;         scanf( %d%d%d , &x, &y, &q);         --x;         --y;         ans[a[x][y].first][a[x][y].second] = q;       }     }     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         printf( %d  , ans[i][j]);       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   char s[100005];   int main() {     int i, n, cnt1, cnt2, ans1, ans2;     cnt1 = cnt2 = ans1 = ans2 = 0;     scanf( %d%s , &n, s);     for (i = 0; i < n; i++) {       if (i & 1) {         if (s[i] ==  r ) cnt1++;       } else {         if (s[i] ==  b ) cnt2++;       }     }     ans1 = max(cnt1, cnt2);     cnt1 = cnt2 = 0;     for (i = 0; i < n; i++) {       if (i & 1) {         if (s[i] ==  b ) cnt1++;       } else {         if (s[i] ==  r ) cnt2++;       }     }     ans2 = max(cnt1, cnt2);     printf( %d , min(ans1, ans2));   }


#include <bits/stdc++.h>   using namespace std;   inline int read() {     int sum = 0, t = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) t = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       sum = sum * 10 + ch -  0 ;       ch = getchar();     }     return sum * t;   }   struct node {     int nxt[26], par, len, rt;   } c[400005];   int tot = 1, rt = 1, las = 1;   void insert(int x) {     int np = ++tot, p = las, q, nq;     c[np].len = c[p].len + 1;     for (; p && !c[p].nxt[x]; p = c[p].par) c[p].nxt[x] = np;     if (!p)       c[np].par = rt;     else {       q = c[p].nxt[x];       if (c[q].len == c[p].len + 1)         c[np].par = q;       else {         c[nq = ++tot] = c[q];         c[nq].len = c[p].len + 1;         c[q].par = c[np].par = nq;         for (; p && c[p].nxt[x] == q; p = c[p].par) c[p].nxt[x] = nq;       }     }     las = np;   }   int nxt[400005], to[400005], fir[400005], cnt;   int fa[400005][19], dep[400005];   void add(int x, int y) {     nxt[++cnt] = fir[x];     to[cnt] = y;     fir[x] = cnt;   }   void dfs(int x) {     for (int i = 1; i < 9; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];     for (int i = fir[x]; i; i = nxt[i]) {       int y = to[i];       dep[y] = dep[x] + 1;       fa[y][0] = x;       dfs(y);     }   }   int lca(int x, int y) {     if (dep[x] < dep[y]) swap(x, y);     int d = dep[x] - dep[y];     for (int i = 0; i < 9; i++)       if (d >> i & 1) x = fa[x][i];     if (x == y) return x;     for (int i = 8; ~i; i--)       if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];     return fa[x][0];   }   char s[400005], t[400005];   int who[400005];   int L1, L2;   bool cmp(int x, int y) {     bool flag = false;     if (x > y) swap(x, y), flag = true;     if (x + L2 <= y) {       int L = who[L1 + 1], R = who[x + 1];       int lcp = c[lca(L, R)].len;       if (lcp >= L2) {         L = R, R = who[x + L2 + 1];         lcp = c[lca(L, R)].len;         if (x + L2 + lcp < y)           return (s[x + lcp + 1] < s[x + L2 + lcp + 1]) ^ flag;         else {           L = who[y + 1 - L2], R = who[L1 + 1];           lcp = c[lca(L, R)].len;           if (lcp >= L2)             return !flag;           else             return (s[y + 1 - L2 + lcp] < t[lcp + 1]) ^ flag;         }       } else         return (t[lcp + 1] < s[x + lcp + 1]) ^ flag;     } else {       int L = who[L1 + 1], R = who[x + 1];       int lcp = c[lca(L, R)].len;       if (lcp >= y - x) {         R = L, L = who[L1 + y - x + 1];         lcp = c[lca(L, R)].len;         if (lcp + y - x >= L2) {           L = who[x + 1], R = who[x + L2 - y + L1 + 1];           lcp = c[lca(L, R)].len;           if (lcp >= y - x)             return !flag;           else             return (s[x + lcp + 1] < t[x + L2 - y + lcp + 1]) ^ flag;         } else           return (t[y - x + lcp + 1] < t[lcp + 1]) ^ flag;       } else         return (t[lcp + 1] < s[x + lcp + 1]) ^ flag;     }   }   struct Node {     int ins;     bool operator<(const Node &a) const { return cmp(ins, a.ins); }   } rk[400005];   int a[400005], q;   int head[400005], nex[400005];   struct Query {     int ans, l, r, x, y;   } qry[400005];   int f[19][400005], Log[400005], g[19][400005];   void build(int st[][400005], int len) {     for (int i = 1; i <= 17; i++)       for (int j = 0; j <= len && j + (1 << i) <= len + 1; j++)         st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);   }   int query(int st[][400005], int l, int r) {     int len = Log[r - l + 1];     return min(st[len][l], st[len][r - (1 << len) + 1]);   }   int main() {     scanf( %s , s + 1);     L1 = strlen(s + 1);     scanf( %s , t + 1);     L2 = strlen(t + 1);     for (int i = L2; i; i--) insert(t[i] -  a ), who[c[las].rt = i + L1] = las;     for (int i = L1; i; i--) insert(s[i] -  a ), who[c[las].rt = i] = las;     for (int i = 1; i <= tot; i++)       if (c[i].par) add(c[i].par, i);     dfs(1);     for (int i = 0; i <= L1; i++) rk[i].ins = i;     sort(rk, rk + L1 + 1);     for (int i = 0; i <= L1; i++) a[rk[i].ins] = i;     for (int i = 2; i <= L1; i++) Log[i] = Log[i >> 1] + 1;     for (int i = 0; i <= L1; i++) f[0][i] = a[i];     build(f, L1);     q = read();     for (int i = 1; i <= q; i++) {       qry[i].l = read();       qry[i].r = read();       int k = read();       qry[i].x = read();       qry[i].y = read();       qry[i].ans = L1 + 1;       nex[i] = head[k];       head[k] = i;     }     for (register int i = 1; i <= L1; i++)       if (head[i]) {         if (i <= 80) {           for (register int k, j = 0; j < i; j++) {             for (k = 0; k * i + j <= L1; k++) g[0][k] = f[0][k * i + j];             build(g, k - 1);             for (k = head[i]; k; k = nex[k])               if (qry[k].x <= j && qry[k].y >= j) {                 int x = (qry[k].l - j) / i;                 if (x * i + j < qry[k].l) x++;                 int y = (qry[k].r - j) / i;                 if (x * i + j > qry[k].r) y--;                 if (x <= y) qry[k].ans = min(qry[k].ans, query(g, x, y));               }           }         } else {           for (register int j = head[i]; j; j = nex[j])             for (register int k = 0; k * i <= L1; k++) {               int x = max(k * i + qry[j].x, qry[j].l),                   y = min(k * i + qry[j].y, qry[j].r);               if (x <= y) qry[j].ans = min(qry[j].ans, query(f, x, y));             }         }       }     rk[L1 + 1].ins = -1;     for (int i = 1; i <= q; i++)       printf( %d%c , rk[qry[i].ans].ins, (i < q) ?     :   n );   }