#include <bits/stdc++.h>   using namespace std;   using pii = pair<int, int>;   const int rows[5] = {3, 4, 5, 4, 3};   bool dp[1 << 19], seen[1 << 19];   pii int_to_pair(int c) {     pii res = {0, 0};     while (rows[res.first] <= c) {       c -= rows[res.first];       res.first++;     }     res.second = c;     return res;   }   int pair_to_int(pii p) {     int res = 0;     for (int i = 0; i < p.first; i++) res += rows[i];     res += p.second;     return res;   }   pii next_coords(pii p, int dir) {     if (dir == 0) {       return {p.first, p.second - 1};     } else if (dir == 1) {       if (p.first <= 2) {         return {p.first - 1, p.second - 1};       } else {         return {p.first - 1, p.second};       }     } else if (dir == 2) {       if (p.first <= 2) {         return {p.first - 1, p.second};       } else {         return {p.first - 1, p.second + 1};       }     } else if (dir == 3) {       return {p.first, p.second + 1};     } else if (dir == 4) {       if (p.first >= 2) {         return {p.first + 1, p.second};       } else {         return {p.first + 1, p.second + 1};       }     } else if (dir == 5) {       if (p.first >= 2) {         return {p.first + 1, p.second - 1};       } else {         return {p.first + 1, p.second};       }     }   }   void print_box(int box) {     int b = 0;     for (int i = 0; i < 5; i++) {       for (int j = 0; j < 5 - rows[i]; j++) printf(   );       for (int j = 0; j < rows[i]; j++) {         printf( %c  ,  .O [(box >> b) & 1]);         b++;       }       printf(  n );     }   }   bool getdp(int box) {     if (box == 0) return false;     if (seen[box]) {       return dp[box];     }     seen[box] = true;     for (int i = 0; i < 19; i++) {       if (!(box & (1 << i))) {         continue;       }       for (int dir = 0; dir < 6; dir++) {         int boxcpy = box;         pii curr = int_to_pair(i);         while (true) {           if (curr.first < 0) break;           if (curr.first > 4) break;           if (curr.second < 0) break;           if (curr.second >= rows[curr.first]) break;           if (!(boxcpy & (1 << pair_to_int(curr)))) {             break;           }           boxcpy ^= (1 << pair_to_int(curr));           if (!getdp(boxcpy)) {             dp[box] = true;             return true;           }           curr = next_coords(curr, dir);         }       }     }     return false;   }   int main() {     int start_box = 0;     for (int i = 0; i < 19; i++) {       char c;       cin >> c;       if (c ==  O ) start_box |= 1 << i;     }     if (getdp(start_box)) {       cout <<  Karlsson n ;     } else {       cout <<  Lillebror n ;     }   }


#include <bits/stdc++.h>   int n, u[1001], v[1001], dem[1001], ans = 0;   using namespace std;   void input() {     cin >> n;     for (int i = 1; i < n; i++) {       cin >> u[i] >> v[i];     }   }   void output() { cout << ans << endl; }   void solve() {     for (int i = 1; i <= n; i++) {       for (int j = 1; j < n; j++) {         if (u[j] == i) dem[i]++;         if (v[j] == i) dem[i]++;       }     }     for (int i = 1; i <= n; i++) {       if (dem[i] == 1) ans++;     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     input();     solve();     output();   }


#include <bits/stdc++.h>   using namespace std;   char s1[200005], s2[200005];   int len;   bool judge(int p, char *s1, char *s2) {     for (int i = 0; i < p; i++) {       if (s1[i] != s2[i]) return 0;     }     return 1;   }   int check(int p, char *s1, char *s2) {     if (p == 1) {       if (s1[0] != s2[0]) return 0;       return 1;     }     if (judge(p, s1, s2)) return 1;     int len = p;     if (len & 1) return 0;     return (check(len / 2, s1, s2) &&             check(len / 2, s1 + len / 2, s2 + len / 2)) ||            (check(len / 2, s1 + len / 2, s2) && check(len / 2, s1, s2 + len / 2));   }   int main(void) {     int i, j;     while (~scanf( %s%s , s1, s2)) {       int len = strlen(s1);       if (check(len, s1, s2))         puts( YES );       else         puts( NO );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> a;   int main() {     int n, x, ans;     cin >> n >> x;     ans = x;     a.resize(n);     for (int i = 0; i < (int)(n); i++) {       cin >> a[i];     }     for (int i = 0; i < (int)(n); i++) {       for (int j = i; j < n; j++) {         ans = max(ans, (x / a[i]) * a[j] + (x % a[i]));       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   bool check(const pair<long long, long long> &a,              const pair<long long, long long> &b) {     if (a.first > b.first)       return true;     else if (a.first < b.first)       return false;     else {       if (a.second < b.second)         return true;       else         return false;     }   }   int main() {     long long n, k, a, b, i, ans = 0;     cin >> n >> k;     vector<pair<long long, long long>> v;     for (i = 0; i < n; i++) {       cin >> a >> b;       v.push_back({a, b});     }     sort(v.begin(), v.end(), check);     for (i = 0; i < n; i++) {       if (v[i].first == v[k - 1].first && v[i].second == v[k - 1].second) ans++;     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( no-stack-protector )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native )   #pragma GCC optimize( fast-math )   #pragma warning(disable : 4996)   using namespace std;   const int MAXN = 100001;   const int inf = 1000000000;   const long double INF = 1.0 / 0.0;   const long double EPS = 1e-7;   const int mod = inf + 7;   const long double PI = acos(-1.0);   signed main() {     ios_base::sync_with_stdio(0);     cout.tie(0);     cin.tie(0);     ;     long double d, l, v1, v2;     cin >> d >> l >> v1 >> v2;     cout << fixed << (l - d) / (v1 + v2);   }


#include <bits/stdc++.h>   using namespace std;   const long double PI = acos((long double)-1);   namespace fft {   template <class T>   class comp {    public:     T real, img;     comp(T a = (T)0, T b = (T)0) : real(a), img(b) {}     comp conj() { return comp(this->real, -(this->img)); }     comp operator=(const comp& a) {       this->real = a.real, this->img = a.img;       return *this;     }     comp operator+(const comp& b) {       return comp(this->real + b.real, this->img + b.img);     }     comp operator-(const comp& b) {       return comp(this->real - b.real, this->img - b.img);     }     comp operator*(const T& num) {       return comp(this->real * num, this->img * num);     }     comp operator/(const T& num) {       return comp(this->real / num, this->img / num);     }     comp operator*(const comp& b) {       return comp(this->real * b.real - this->img * b.img,                   this->img * b.real + this->real * b.img);     }     comp operator/(const comp& b) {       comp temp(b.real, -b.img);       comp n = (*this) * temp;       return n / (b.x * b.x + b.y * b.y);     }   };   comp<long double> w[1 << 20];   int rev[1 << 20];   void revbits(int newlim) {     static int lim = -1;     int t, j;     if (newlim == lim) return;     lim = newlim;     int k = 0;     while ((1 << k) < newlim) ++k;     assert((1 << k) == newlim);     for (int i = 1; i <= (int)(lim); ++i) {       j = rev[i - 1];       t = k - 1;       while (t >= 0 && ((j >> t) & 1)) j ^= (1 << t), --t;       if (t >= 0) j ^= (1 << t), --t;       rev[i] = j;     }   }   void fft(vector<comp<long double> >& poly, int inv = false) {     int len, l;     revbits((int)(poly).size());     for (int i = 0; i < (int)((int)(poly).size()); ++i)       if (rev[i] > i) swap(poly[i], poly[rev[i]]);     comp<long double> u, v;     for (len = 2, l = 1; len <= (int)(poly).size(); len += len, l += l) {       if (w[l].real == 0 && w[l].img == 0) {         long double ang = PI / l;         comp<long double> ww(cos(ang), sin(ang));         if (l > 1) {           for (int j = 0; j < l; ++j) {             if (j & 1)               w[l + j] = w[(l + j) >> 1] * ww;             else               w[l + j] = w[(l + j) >> 1];           }         } else           w[l] = comp<long double>(1.0, 0.0);       }       for (int i = 0; i < (int)(poly).size(); i += len)         for (int j = 0; j < (int)(l); ++j) {           u = poly[i + j],           v = poly[i + j + l] * (inv ? w[l + j].conj() : w[l + j]);           poly[i + j] = u + v, poly[i + j + l] = u - v;         }     }     if (inv)       for (auto& x : poly) x = x / (long double)(int)(poly).size();   }   void fft2d(vector<vector<comp<long double> > >& A, bool inv = false) {     for (auto& row : A) {       fft(row, inv);     }     vector<comp<long double> > col((int)(A).size());     for (int j = 0; j < (int)((int)(A.front()).size()); ++j) {       for (int i = 0; i < (int)((int)(A).size()); ++i) col[i] = A[i][j];       fft(col, inv);       for (int i = 0; i < (int)((int)(A).size()); ++i) A[i][j] = col[i];     }   }   vector<vector<comp<long double> > > multiply(       vector<vector<comp<long double> > >& x,       vector<vector<comp<long double> > >& y) {     int b_rows = 0, b_cols = 0;     while ((1 << b_rows) <= max((int)(x).size(), (int)(y).size())) ++b_rows;     ++b_rows;     while ((1 << b_cols) <= max((int)(x.front()).size(), (int)(y.front()).size()))       ++b_cols;     ++b_cols;     x.resize(1 << b_rows);     y.resize(1 << b_rows);     for (auto& row : x) row.resize(1 << b_cols, 0);     for (auto& row : y) row.resize(1 << b_cols, 0);     fft2d(x), fft2d(y);     for (int i = 0; i < (int)((int)(x).size()); ++i)       for (int j = 0; j < (int)((int)(x[i]).size()); ++j)         x[i][j] = x[i][j] * y[i][j];     fft2d(x, 1);     return x;   }   const int L = 805;   string A[L], B[L];   int N, M, R, C, X, Y;   void read() {     cin >> N >> M;     for (int i = 0; i < (int)(N); ++i) cin >> A[i];     cin >> R >> C;     for (int i = 0; i < (int)(R); ++i) cin >> B[i];   }   void solve() {     X = N + R, Y = M + C;     vector<vector<comp<long double> > > a(X, vector<comp<long double> >(Y));     for (int i = 0; i < (int)(N); ++i)       for (int j = 0; j < (int)(M); ++j) {         a[i][j].real = cos(2 * PI * (A[i][j] -  a ) / 26.0),         a[i][j].img = sin(2 * PI * (A[i][j] -  a ) / 26.0);         for (int ii = i; ii < X; ii += N)           for (int jj = j; jj < Y; jj += M) a[ii][jj] = a[i][j];       }     vector<vector<comp<long double> > > b(R, vector<comp<long double> >(C));     int valid = 0;     for (int i = 0; i < (int)(R); ++i)       for (int j = 0; j < (int)(C); ++j) {         if (B[i][j] ==  ? ) continue;         ++valid;         b[R - i - 1][C - j - 1].real = cos(2 * PI * (B[i][j] -  a ) / 26.0),                              b[R - i - 1][C - j - 1].img =                                  -sin(2 * PI * (B[i][j] -  a ) / 26.0);       }     vector<vector<comp<long double> > > c = multiply(a, b);     for (int i = 0; i < (int)(N); ++i) {       for (int j = 0; j < (int)(M); ++j)         cout << ((int)(c[R - 1 + i][C - 1 + j].real + 1e-5) == valid);       cout <<   n ;     }   }   }  // namespace fft   int main() {     std::ios::sync_with_stdio(false);     fft::read();     fft::solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int nax = 5005;   const int mod = 1e9 + 7;   const int inf = 1e9 + 120;   int t[nax];   char sl[nax];   int pref[nax][nax];   int nxt[nax][nax];   int cmp(int i, int j) {     if (t[i] < t[j]) return -1;     if (t[i] > t[j]) return 1;     return 0;   }   int dp(int a, int b) {     int x = pref[a][b] - pref[a - 1][b];     if (x < 0) x += mod;     return x;   }   int main() {     int n;     scanf( %d , &n);     scanf( %s , sl);     for (int i = (1); i <= ((n)); ++i) t[i] = sl[i - 1] -  0 ;     for (int a = n; a >= 1; --a)       for (int b = n; b > a; --b) {         int& x = nxt[a][b];         if (t[a] != t[b])           x = a;         else if (b == n)           x = inf;         else           x = nxt[a + 1][b + 1];       }     for (int i = (1); i <= ((n)); ++i) {       for (int j = (1); j <= ((n)); ++j) pref[j][i] = 1;     }     for (int c = (2); c <= (n); ++c)       for (int d = (c); d <= (n); ++d) {         int& x = pref[c][d];         int b = c - 1;         int a = b - (d - c);         x = pref[b][b] - pref[max(a, 0)][b];         if (x < 0) x += mod;         if (a >= 1) {           int where = nxt[a][c];           if (where < c && t[where] < t[c + where - a]) {             x += dp(a, b);             if (x >= mod) x -= mod;           }         }         if (t[c] == 0) x = 0;         pref[c][d] = x + pref[c - 1][d];         if (pref[c][d] >= mod) pref[c][d] -= mod;       }     int ans = 0;     for (int i = (1); i <= ((n)); ++i) ans = (ans + dp(i, n)) % mod;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   string to_string(string s) { return     + s +    ; }   string to_string(const char* s) { return to_string((string)s); }   string to_string(bool b) { return (b ?  true  :  false ); }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto& x : v) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }   void debug_out() { cerr << endl; }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     debug_out(T...);   }   const int N = 2e3 + 7, M = 1e4, OO = 0x3f3f3f3f;   int vis[N][N];   int main() {     int TCF = 1;     while (TCF--) {       int n;       scanf( %d , &n);       ;       vector<pair<int, int>> p;       int x, y;       for (int i = 0; i < n; ++i) {         scanf( %d %d , &x, &y);         p.emplace_back(x, y);         vis[x + 1000][y + 1000] = 1;       }       vector<pair<int, int>> mid;       int ans = 0;       for (int i = (0); i < (n); i++) {         for (int j = (i + 1); j < (n); j++) {           if (i == j) continue;           int mx = p[i].first + p[j].first;           int my = p[i].second + p[j].second;           if (mx % 2 == 0 && my % 2 == 0) {             mx /= 2;             my /= 2;             if (vis[mx + 1000][my + 1000]) {               ans++;             }           }         }       }       printf( %d n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   int main(void) {     int n, q, a, b;     scanf( %d%d%d%d , &n, &q, &a, &b);     vector<int> x(n);     for (int i = 0; i < n; ++i) {       scanf( %d , &x[i]);     }     vector<double> y(n);     vector<int> from(n, -1);     y[0] = x[0];     for (int i = 1; i < n; ++i) {       double shiftSum = 0.0;       shiftSum = 0.0;       double curPos = x[i];       int j;       double shift = 0.0;       double maxShift = q - curPos;       double minShift = 1.0 - curPos;       for (j = i - 1; j >= 0; j--) {         shift = max(minShift, min(maxShift, shiftSum / (i - j)));         double pos = curPos + shift;         if (pos >= y[j] + a - eps && pos <= y[j] + b + eps) break;         if (pos >= y[j] + b) {           curPos -= b;         } else {           curPos -= a;         }         minShift = max(minShift, 1.0 - curPos);         shiftSum += x[j] - curPos;       }       shift = max(minShift, min(maxShift, shiftSum / (i - j)));       y[i] = x[i] + shift;       from[i] = j;     }     vector<double> ry(n);     for (int i = n - 1; i >= 0;) {       double shift = y[i] - x[i];       ry[i] = y[i];       for (int j = i - 1; j > from[i]; --j) {         if (ry[j + 1] >= y[j] + b)           ry[j] = ry[j + 1] - b;         else           ry[j] = ry[j + 1] - a;       }       i = from[i];     }     double res = 0.0;     for (int i = 0; i < n; ++i) {       double d = ry[i] - x[i];       res += d * d;       printf( %.9lf  , ry[i]);     }     printf(  n%.9lf n , res);   }


#include <bits/stdc++.h>   bool IsCircular(const std::string &s, const int n) {     bool isCircular = true;     for (int i = 0; i < n; i++) {       int j = (i + 1) % n;       isCircular &=           ((s[i] ==  >  || s[i] ==  - ) && (s[j] ==  >  || s[j] ==  - ));     }     if (isCircular) return true;     isCircular = true;     for (int i = 0; i < n; i++) {       int j = (i + 1) % n;       isCircular &=           ((s[i] ==  <  || s[i] ==  - ) && (s[j] ==  <  || s[j] ==  - ));     }     return isCircular;   }   int main() {     int t;     std::cin >> t;     while (t--) {       int n;       std::string s;       std::cin >> n >> s;       if (IsCircular(s, n))         std::cout << n << std::endl;       else {         int ans = 0;         for (int i = 0; i < n; i++) {           int j = (i - 1 + n) % n;           if (s[i] ==  -  || s[j] ==  - ) ans++;         }         std::cout << ans << std::endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (1 << 18) + 10;   int n, m;   long long tree[N], add[N];   int same[N];   int col[N];   void build(int x, int l, int r) {     tree[x] = same[x] = col[x] = add[x] = 0;     if (l == r) {       same[x] = 1;       col[x] = l + 1;       return;     }     int s = (l + r) / 2;     build(x + x + 1, l, s);     build(x + x + 2, s + 1, r);   }   int push(int x, int l, int r) {     if (add[x]) {       tree[x] += (long long)(r - l + 1) * add[x];       if (l != r) {         add[x * 2 + 1] += add[x];         add[x * 2 + 2] += add[x];       }       add[x] = 0;     }     if (same[x]) {       if (l != r) {         same[x * 2 + 1] = same[x * 2 + 2] = 1;         col[x * 2 + 1] = col[x * 2 + 2] = col[x];       }     }     return 0;   }   long long upd(int x, int l, int r, int lc, int rc, int y) {     push(x, l, r);     if (r < lc || l > rc) return tree[x];     if (lc <= l && r <= rc) {       if (same[x]) {         add[x] += abs(col[x] - y);         col[x] = y;         push(x, l, r);         return tree[x];       } else         same[x] = 1, col[x] = y;     } else if (same[x] && col[x] != y)       col[x] = same[x] = 0;     int s = (l + r) / 2;     return tree[x] = upd(x + x + 1, l, s, lc, rc, y) +                      upd(x + x + 2, s + 1, r, lc, rc, y);   }   long long get(int x, int l, int r, int lc, int rc) {     push(x, l, r);     if (r < lc || l > rc) return 0;     if (lc <= l && r <= rc) return tree[x];     int s = (l + r) / 2;     return get(x + x + 1, l, s, lc, rc) + get(x + x + 2, s + 1, r, lc, rc);   }   int main() {     scanf( %d%d , &n, &m);     build(0, 0, n - 1);     int t, l, r, x;     for (int i = 0; i < m; i++) {       scanf( %d , &t);       if (t == 1) {         scanf( %d%d%d , &l, &r, &x);         l--;         r--;         upd(0, 0, n - 1, l, r, x);       } else {         scanf( %d%d , &l, &r);         l--;         r--;         printf( %I64d n , get(0, 0, n - 1, l, r));       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     string s;     cin >> s;     for (long long i = 0; i < s.size(); i++) {       if (s[i] ==  *  && i + 4 <= s.size()) {         for (long long j = 1; j < s.size(); j++) {           if (i + 4 * j <= s.size()) {             if (s[i] ==  *  && s[i + j] ==  *  && s[i + 2 * j] ==  *  &&                 s[i + 3 * j] ==  *  && s[i + 4 * j] ==  * ) {               cout <<  yes ;               return 0;             }           }         }       }     }     cout <<  no ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1E6 + 50;   const long long p = 1E9 + 7;   int n, k;   long long ans, sum, f[N], s[N], fac[N], inv[N];   inline void upd(long long &a, long long b) {     a += b;     if (a >= p) a -= p;   }   inline void sub(long long &a, long long b) {     a -= b;     if (a < 0) a += p;   }   long long Pow(long long a, long long b = p - 2) {     long long res = 1;     for (; b; b >>= 1, a = a * a % p)       if (b & 1) res = res * a % p;     return res;   }   void init() {     fac[0] = inv[0] = 1;     for (register int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % p;     inv[n] = Pow(fac[n]);     for (register int i = n - 1; i; --i) inv[i] = inv[i + 1] * (i + 1) % p;   }   int main() {     scanf( %d%d , &n, &k);     if (k >= n) return 0 * printf( 0 n );     init();     f[0] = s[0] = 1;     for (register int i = 1; i <= k; ++i) {       f[i] = fac[i], s[i] = 1;       upd(sum, s[i]);     }     for (register int i = k + 1; i <= n; ++i) {       f[i] = sum * fac[i - 1] % p;       s[i] = f[i] * inv[i] % p;       upd(sum, s[i]), sub(sum, s[i - k]);     }     for (int i = 0; i < n - k; ++i) sub(ans, s[i]);     ans = ans * fac[n - 1] % p;     sub(ans, f[n]);     upd(ans, fac[n]);     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cerr.tie(nullptr);     int n, res = 0, t, b;     cin >> n;     while (n) {       res++;       t = n;       b = -1;       while (t) {         b = max(b, t % 10);         t /= 10;       }       n -= b;     }     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int N, a[55], P;   double dp[55][55][55];   double fac(int n) {     double ans = 1;     for (int i = 0; i < (n); i++) ans *= (i + 1);     return ans;   }   int main(int argc, char *argv[]) {     scanf( %d , &N);     for (int i = 0; i < (N); i++) scanf( %d , a + i);     scanf( %d , &P);     dp[0][0][0] = 1;     for (int i = 0; i < (N); i++)       for (int in = (0); in <= (N); in++)         for (int p = (0); p <= (P); p++)           if (dp[i][in][p]) {             dp[i + 1][in][p] += dp[i][in][p];             if (p + a[i] <= P) dp[i + 1][in + 1][p + a[i]] += dp[i][in][p];           }     double tmp = 0, ans = 0, sum;     for (int in = (N); in >= (0); in--) {       sum = 0;       for (int p = (0); p <= (P); p++) sum += dp[N][in][p];       sum *= fac(in) * fac(N - in);       ans += in * (sum - tmp);       tmp = sum;     }     printf( %.9lf n , ans / fac(N));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   const int LG = 27;   const int INF = 1e9 + 7;   pair<int, int> seg[LG][N];   int mx[LG][N];   int r[N], a[N], sol[N];   pair<int, int> p[N];   int n, k;   void build(int l = 0, int r = n, int d = 0) {     if (r - l == 1) {       seg[d][l] = make_pair(p[l].second, l);       mx[d][l] = sol[l];       return;     }     int mid = (l + r) / 2;     build(l, mid, d + 1);     build(mid, r, d + 1);     merge(seg[d + 1] + l, seg[d + 1] + mid, seg[d + 1] + mid, seg[d + 1] + r,           seg[d] + l);     int out = 0;     for (int i = r - 1; i >= l; i--) {       out = max(out, sol[seg[d][i].second]);       mx[d][i] = out;     }   }   pair<int, int> get(int l, int r, int val, int s = 0, int e = n, int d = 0) {     if (l >= e || r <= s) return make_pair(0, 0);     if (l <= s && e <= r) {       int pos = lower_bound(seg[d] + s, seg[d] + e, make_pair(val, 0)) - seg[d];       int pos2 =           lower_bound(seg[d] + s, seg[d] + e, make_pair(val + 1, 0)) - seg[d];       int ans1 = 0;       if (pos < e) ans1 = mx[d][pos];       return make_pair(ans1, pos2 - s);     }     int mid = (s + e) / 2;     pair<int, int> a = get(l, r, val, s, mid, d + 1);     pair<int, int> b = get(l, r, val, mid, e, d + 1);     return make_pair(max(a.first, b.first), a.second + b.second);   }   int main() {     scanf( %d %d , &n, &k);     for (int i = 0; i < n; i++) {       scanf( %d , &r[i]);       p[i].second = r[i];     }     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);       p[i].first = a[i];     }     sort(p, p + n);     build();     int L = 0, R = 0;     for (int i = 0; i < n; i++) {       while (L < n && p[L].first + k < p[i].first) L++;       while (R < n && p[i].first + k >= p[R].first) R++;       sol[i] = get(L, R, p[i].second).second;     }     build();     int m;     cin >> m;     while (m--) {       int x, y;       scanf( %d %d , &x, &y);       x--, y--;       if (a[x] > a[y]) swap(x, y);       int L = lower_bound(p, p + n, make_pair(a[y] - k, 0)) - p;       int R = lower_bound(p, p + n, make_pair(a[x] + k + 1, 0)) - p;       int ans = get(L, R, max(r[x], r[y])).first;       if (ans < 2)         printf( -1 n );       else         printf( %d n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename t>   inline void read(t &x) {     x = 0;     char c = getchar(), f = 0;     for (; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) f = 1;     for (; c >=  0  && c <=  9 ; c = getchar())       x = (x << 1) + (x << 3) + (c ^ 48);     if (f) x = -x;   }   int n, rs[100005], tp;   char vs[100005];   inline void dfs(int x) {     if (vs[x])       return;     else       vs[x] = 1, dfs(x * 2 % n), dfs((x * 2 + 1) % n), rs[++tp] = x;   }   int main() {     read(n);     if (n & 1) return puts( -1 ), 0;     dfs(0);     while (tp) printf( %d  , rs[tp--]);     return printf( 0 n ), 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x, v, arr[1000], c = 0;     cin >> x >> v;     for (int i = 0; i < x - 1; i++) {       cin >> arr[i];     }     while (1) {       sort(arr, arr + x - 1);       if (v > arr[x - 2]) break;       arr[x - 2]--;       v++;       c++;     }     cout << c;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int f(char c) { return c - 48; }   void soroban(int n, char *ans) {     if (n < 5) {       ans[0] =  O ;       ans[1] =  - ;       ans[2] =  | ;       for (int i = 3; i <= 7; i++) {         if (i == n + 3) {           ans[i] =  - ;         } else {           ans[i] =  O ;         }       }     } else {       ans[0] =  - ;       ans[1] =  O ;       ans[2] =  | ;       for (int i = 3; i <= 7; i++) {         if (i == n - 2) {           ans[i] =  - ;         } else {           ans[i] =  O ;         }       }     }     ans[8] =   0 ;   }   int main() {     char ch[10], ans[10];     cin >> ch;     for (int i = strlen(ch) - 1; i >= 0; i--) {       soroban(f(ch[i]), ans);       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   const long long mod = 998244353;   const int maxn = 3005;   int dp[maxn][maxn];   int dp2[maxn][maxn];   long long add(long long a, long long b) { return (a + b) % mod; }   long long mul(long long a, long long b) { return (a * b) % mod; }   long long bin_pow(long long a, long long b) {     long long res = 1;     long long tmp = a;     while (b) {       if (b & 1) {         res = mul(res, tmp);       }       tmp = mul(tmp, tmp);       b >>= 1;     }     return res;   }   long long inv(long long a) { return bin_pow(a, mod - 2); }   long long min(long long a, long long b) {     if (a > b)       return b;     else       return a;   }   void solve() {     int n, m;     cin >> n >> m;     long long n_good = 0, n_bad = 0;     vector<bool> flags(n);     for (int i = 0; i < n; i++) {       int ai;       cin >> ai;       flags[i] = ai == 1;     }     vector<long long> weights(n);     for (int i = 0; i < n; i++) {       int w;       cin >> w;       weights[i] = w;       if (flags[i]) {         n_good += w;       } else {         n_bad += w;       }     }     for (int i = m; i >= 0; i--) {       dp[i][m - i] = dp2[i][m - i] = 1;       for (int j = min(m - i - 1, (long long)n_bad); j >= 0; j--) {         long long total = inv(n_good + n_bad + i - j);         long long tmp = mul(dp[i + 1][j], n_good + i + 1);         long long tmp2 = mul(dp[i][j + 1], n_bad - j);         long long tmp3 = add(tmp, tmp2);         dp[i][j] = mul(tmp3, total);         tmp = mul(dp2[i + 1][j], n_good + i);         tmp2 = mul(dp2[i][j + 1], n_bad - j - 1);         tmp3 = add(tmp, tmp2);         dp2[i][j] = mul(tmp3, total);       }     }     for (int i = 0; i < n; i++) {       if (flags[i]) {         cout << mul(weights[i], dp[0][0]);       } else {         cout << mul(weights[i], dp2[0][0]);       }       cout << endl;     }   }   int main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(false);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int oo = (1e9) + 7;   int main() {     long long n;     cin >> n;     vector<long long> b(n / 2 + 5, 0);     for (int i = 1; i <= n / 2; i++) {       scanf( %lld , &b[i]);     }     vector<long long> out(n + 5, 0);     long long l = 0;     long long r = 0;     for (int i = 1; i <= n / 2; i++) {       out[i] = 0;       out[n - i + 1] = b[i];       if (i > 1) {         if (b[i] > r) {           out[i] = b[i] - r;           out[n - i + 1] = r;         }         long long tmp = b[i];         if (out[i] < l) {           out[i] = l;           out[n - i + 1] = b[i] - l;         }       }       l = out[i];       r = out[n - i + 1];     }     for (int i = 1; i <= n; i++) {       printf( %lld  , out[i]);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T sqr(const T& a) {     return a * a;   }   template <typename T>   inline int sign(const T& a) {     return a < 0 ? -1 : a > 0;   }   void task();   int main() {     task();     return 0;   }   vector<int> z_function(string s) {     int n = (int)s.length();     vector<int> z(n);     for (int i = 1, l = 0, r = 0; i < n; ++i) {       if (i <= r) z[i] = min(r - i + 1, z[i - l]);       while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];       if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;     }     return z;   }   int n, k;   string s;   int ans[int(2e6) + 10];   int add[int(2e6) + 10];   int sub[int(2e6) + 10];   void task() {     ios_base::sync_with_stdio(0);     cin >> n >> k;     cin >> s;     auto z = z_function(s);     z[0] = s.length();     (void(1));     for (int len = 1; len <= n; ++len) {       for (int i = 0, step = 0; i < n; i += len, ++step) {         if (z[i] < len) break;         if (step == k - 1) {           (void(1));           add[i + len - 1]++;           sub[i + len - 1 + min(len, z[i + len])]++;           break;         }       }     }     int cur = 0;     for (int i = 0; i < n; ++i) {       cur += add[i];       ans[i] = cur > 0;       cur -= sub[i];     }     for (int i = 0; i < n; ++i) cout << ans[i];   }


#include <bits/stdc++.h>   using namespace std;   ifstream fi(        easy         .inp );   ofstream fo(        easy         .out );   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     vector<string> s;     for (int i = 0; i < n; i++) {       string x;       cin >> x;       s.push_back(x);     }     vector<string> a(n);     for (int i = 0; i < n; i++) {       string tmp =   ;       for (int j = 0; j < m; j++) {         tmp += (j & 1 ? ( Z  - s[i][j]) : (s[i][j] -  A )) +  A ;       }       a[i] = tmp;     }     vector<pair<string, int>> mp;     for (int i = 0; i < n; i++) {       mp.push_back({a[i], i});     }     sort((mp).begin(), (mp).end());     for (int i = 0; i < n; i++) {       cout << mp[i].second + 1 <<    ;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long maxn = 300000 + 10;   long long n;   long long ans[maxn], tot, vis[maxn], last;   int main() {     while (scanf( %lld , &n) != EOF) {       memset(vis, 0, sizeof(vis));       memset(ans, 0, sizeof(ans));       for (int i = 0; i < n; i++) scanf( %lld , &ans[i]);       last = n;       tot = 0;       printf( 1 );       for (int i = 0; i < n; i++) {         vis[ans[i]] = 1;         tot++;         while (vis[last] > 0) {           tot--;           last--;         }         printf(  %lld , tot + 1);       }       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC target( sse4,avx )   int getNimb(int c, int64_t forbidden_mask,               std::map<std::pair<int, int64_t>, int>& map) {     auto it = map.find(std::pair<int, int64_t>{c, forbidden_mask});     if (it != map.end()) {       return it->second;     }     if (c == 0) {       return 0;     }     std::vector<int> other;     for (int i = 1; i <= c; i++) {       if ((forbidden_mask & (1 << i)) == 0) {         other.push_back(getNimb(c - i, forbidden_mask + (1 << i), map));       }     }     std::sort(other.begin(), other.end());     if (other.empty()) {       return 0;     }     int j = 0;     for (size_t i = 0; i < other.size(); i++) {       if (j >= other.size() || other[j] != i) {         map[std::pair<int, int64_t>(c, forbidden_mask)] = i;         return i;       }       while (j < other.size() && other[j] == i) j++;     }     map[std::pair<int, int64_t>(c, forbidden_mask)] = other.size();     return other.size();   }   int data[] = {0, 1, 1, 2, 2, 2, 3, 3,  3,  3,  4,  4,  4, 4, 4, 5,                 5, 5, 5, 5, 5, 6, 6, 6,  6,  6,  6,  6,  7, 7, 7, 7,                 7, 7, 7, 7, 8, 8, 8, 8,  8,  8,  8,  8,  8, 9, 9, 9,                 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10};   void run(std::istream& in, std::ostream& out) {     int n;     in >> n;     int res = 0;     for (int i = 0; i < n; i++) {       int x;       in >> x;       res ^= data[x];     }     if (res == 0) {       out <<  YES  << std::endl;     } else {       out <<  NO  << std::endl;     }   }   int main() {     std::cin.sync_with_stdio(false);     std::cin.tie(nullptr);     run(std::cin, std::cout);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int z[100001];   int main() {     string s;     int T, a, b;     cin >> s;     for (a = 1; a < s.size(); a++) {       z[a] = z[a - 1] + (s[a] == s[a - 1]);     }     for (cin >> T; T--; cout << z[b - 1] - z[a - 1] << endl) cin >> a >> b;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     bool taken[n + 1];     for (int i = 0; i < n + 1; i++) taken[i] = 0;     int cc = 0;     for (int i = 1; i < n + 1; i++) {       for (int j = 1; j <= n; j++) {         int a;         cin >> a;         if (a == 2 && !taken[j]) taken[j] = 1, cc++;         if (a == 3) {           if (!taken[i]) taken[i] = 1, cc++;           if (!taken[j]) taken[j] = 1, cc++;         }       }     }     cout << n - cc << endl;     for (int i = 1; i < n + 1; i++)       if (!taken[i]) cout << i <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MXN = 100 + 10;   const long long Mod = 1e9 + 7;   void mkay(long long &x) {     if (x >= Mod) x -= Mod;   }   long long n, k;   long long sub[MXN], DP[MXN];   long long dp[MXN][MXN][2], pd[MXN][2];   vector<long long> adj[MXN];   vector<pair<long long, long long>> A, B;   void DFS(long long u, long long par) {     sub[u] = dp[u][k][0] = dp[u][0][1] = 1;     for (auto v : adj[u]) {       if (v == par) continue;       DFS(v, u);       A.clear(), B.clear();       for (int i = 0; i < sub[u]; i++) A.emplace_back(i, 1);       for (int i = 0; i < sub[v]; i++) B.emplace_back(i, 1);       for (long long i = max(0ll, k - sub[u]); i <= k; i++) A.emplace_back(i, 0);       for (long long i = max(0ll, k - sub[v]); i <= k; i++) B.emplace_back(i, 0);       for (auto Pr0 : A) {         long long x, a, f, s;         tie(x, a) = Pr0;         for (auto Pr1 : B) {           long long y, b;           tie(y, b) = Pr1;           if (a == 0 && b == 0) {             f = max(x, y - 1), s = 0;           } else if (a == 0 && b == 1) {             if (x - 1 >= y)               f = x, s = 0;             else               f = y + 1, s = 1;           } else if (a == 1 && b == 0) {             if (y - 1 >= x)               f = y - 1, s = 0;             else               f = x, s = 1;           } else {             f = max(x, y + 1), s = 1;           }           mkay(pd[f][s] += dp[u][x][a] * dp[v][y][b] % Mod);         }       }       sub[u] += sub[v];       for (int f = 0; f <= sub[u]; f++) dp[u][f][1] = pd[f][1], pd[f][1] = 0;       for (int f = max(0ll, k - sub[u]); f <= k; f++)         dp[u][f][0] = pd[f][0], pd[f][0] = 0;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     for (int i = 1; i < n; i++) {       long long u, v;       cin >> u >> v;       adj[u].push_back(v), adj[v].push_back(u);     }     DFS(1, 0);     for (int f = 0; f <= k; f++) {       mkay(DP[k] += dp[1][f][0]);     }     cout << DP[k] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long t, n, p[200100], c[200100], ans = 100100;   bool hamin[200100];   vector<vector<long long> > cycle;   bool same_color(long long ind, long long start, long long dist) {     long long asdf = (start + dist) % cycle[ind].size();     while (asdf != start) {       if (c[cycle[ind][start]] != c[cycle[ind][asdf]]) return false;       asdf = (asdf + dist) % cycle[ind].size();     }     return true;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cin >> t;     while (t--) {       ans = 100000000;       cycle.clear();       cin >> n;       for (int i = 1; i <= n; i++) hamin[i] = false;       for (int i = 1; i <= n; i++) cin >> p[i];       for (int i = 1; i <= n; i++) cin >> c[i];       for (int i = 1; i <= n; i++) {         if (hamin[i]) continue;         long long start = i;         vector<long long> fuck;         fuck.push_back(start);         hamin[start] = true;         while (p[start] != fuck[0]) {           start = p[start];           fuck.push_back(start);           hamin[start] = true;         }         cycle.push_back(fuck);       }       for (long long i = 0; i < cycle.size(); i++) {         for (long long j = 1; j <= cycle[i].size(); j++) {           if (cycle[i].size() % j) continue;           for (long long z = 0; z < j; z++)             if (same_color(i, z, j)) ans = min(ans, j);         }       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   bool comprobarNum(vector<int> aux, int num) {     for (int i = 0; i < aux.size(); i++) {       if (aux[i] == num) {         return true;       }     }     return false;   }   int main() {     int n, aux;     cin >> n;     vector<int> s;     vector<int> numR;     int contador = 1;     while (n--) {       cin >> aux;       if (comprobarNum(s, aux)) {         numR.push_back(aux);       } else {         s.push_back(aux);       }     }     int max = 1;     for (int i = 0; i < numR.size(); i++) {       for (int j = 0; j < numR.size(); j++) {         if (numR[j] == numR[i]) {           contador++;         }       }       if (contador > max) max = contador;       contador = 1;     }     cout << max <<     << s.size();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1);   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     double n, R, r;     cin >> n >> R >> r;     if (r > R) return cout <<  NO , 0;     if (2 * r == R) {       if (n <= 2)         return cout <<  YES , 0;       else         return cout <<  NO , 0;     }     if (2 * r > R) {       if (n <= 1)         return cout <<  YES , 0;       else         return cout <<  NO , 0;     }     if (2 * r < R) {       double h = R - r;       double t = atan2(r, (double)sqrt(h * h - r * r));       if (t * n - PI > 0.0000000000000001)         cout <<  NO ;       else         cout <<  YES ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int i, j, m, n, rev_dig[25], cnt, non_zero, anscnt;   long long x, ans1[100005], ans2[100005], ans3[100005];   long long getinv(long long a, long long p) {     if (a == 1) return 1;     long long k = a - getinv(p % a, a);     return (k * p + 1) / a;   }   void answer(long long a, long long b, int ty) {     anscnt++;     ans1[anscnt] = a;     ans2[anscnt] = b;     ans3[anscnt] = ty;   }   void getmul(int t, long long x) {     long long temp = 1;     long long xx = x;     while (temp < t) {       answer(xx, xx, 1);       xx <<= 1;       temp <<= 1;     }     temp = 1;     long long now = 0;     while (temp <= t) {       if (temp & t) {         if (now > 0) {           answer(temp * x, now, 1);           now += temp * x;         } else {           now = temp * x;         }       }       temp <<= 1;     }   }   int main() {     anscnt = 0;     scanf( %lld , &x);     if (x % 4 == 1) {       answer(x, x, 1);       answer(x, x * 2, 1);       x *= 3;     }     cnt = 0;     long long xx = x;     while (xx > 0) {       cnt++;       rev_dig[cnt] = xx & 1;       xx >>= 1;     }     non_zero = 0;     for (i = 1; i <= cnt; i++) {       if (rev_dig[i] == 0) {         non_zero = i;         break;       }     }     if (non_zero == 0) {       cnt++;       rev_dig[cnt] = 0;       non_zero = cnt;     }     long long xinv = getinv(x, 1 << cnt);     long long r1 = x - 1;     long long t1 = r1 * xinv % (1 << cnt);     getmul(t1, x);     long long b = t1 * x;     answer(b, x, 2);     long long c = b ^ x;     answer(x, c, 1);     long long d = (x + c);     answer(b, d, 2);     long long e = b ^ d;     if (e != 2) {       long long r2 = x - 2;       long long t2 = r2 * xinv % (1 << cnt);       getmul(t2, x);       long long b2 = t2 * x;       answer(b2, x, 2);       long long c2 = b2 ^ x;       answer(x, c2, 1);       long long d2 = (x + c2);       answer(b2, d2, 2);       long long e2 = b2 ^ d2;       answer(e, e2, 2);     }     e = 2;     while (e <= x) {       answer(e, e, 1);       if (e & x) {         answer(e, x, 2);         x = e ^ x;       }       e <<= 1;     }     printf( %d n , anscnt);     for (i = 1; i <= anscnt; i++) {       if (ans3[i] == 1) {         printf( %lld + %lld n , ans1[i], ans2[i]);       } else {         printf( %lld ^ %lld n , ans1[i], ans2[i]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char* argv[]) {     cin.sync_with_stdio(0);     cout.sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, r[100], b[100], sumr, sumb, ans = 1, check = 1;     cin >> n;     for (int i = 0; i < n; i++) cin >> r[i];     for (int i = 0; i < n; i++) cin >> b[i];     sumr = accumulate(r, r + n, 0);     sumb = accumulate(b, b + n, 0);     if ((sumr == sumb && sumb == 0) || (sumr == sumb && sumb == n)) {       cout << -1;       return 0;     }     if (sumr > sumb) {       cout << 1;       return 0;     }     for (int i = 0; i < n; i++)       if (r[i] && !b[i]) {         check = 0;         break;       }     if (check) {       cout << -1;       return 0;     }   RELOOP:;     ans++;     for (int i = 0; i < n; i++)       if (r[i] && !b[i]) sumr++;     if (sumr <= sumb) goto RELOOP;     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, r;   long long A[1000005], sum;   int main() {     scanf( %d %d , &n, &r);     for (int i = (0); i < (1 << n); i++) scanf( %lld , &A[i]), sum += A[i];     printf( %.12lf n , 1.0 * sum / (1 << n));     for (int i = (0); i < (r); i++) {       int x;       long long y;       scanf( %d %lld , &x, &y);       sum += y - A[x];       A[x] = y;       printf( %.12lf n , 1.0 * sum / (1 << n));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     vector<int> a;     int n, t;     cin >> n;     a.reserve(n);     while (n--) {       cin >> t;       if (t != 0) {         a.push_back(t);       }     }     sort(a.begin(), a.end());     cout << unique(a.begin(), a.end()) - a.begin();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = int(2e5);   int g[N];   int ans = 0;   int n;   bool used[N];   void bfs(int start) {     queue<int> q;     q.push(start);     used[start] = true;     while (!q.empty()) {       int from = q.front();       q.pop();       int to = g[from];       if (!used[to]) {         used[to] = true;         q.push(to);       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> g[i];       g[i]--;     }     int c = 0;     for (int i = 0, t; i < n; ++i) {       cin >> t;       c += t;     }     ans += !(bool(c & 1));     int cnt = 0;     for (int i = 0; i < n; ++i) {       if (!used[i]) {         cnt++;         bfs(i);       }     }     ans += (cnt == 1 ? 0 : cnt);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, m, ans;     int d;     char c;     cin >> n >> c;     m = n - 1;     if (c ==  f ) {       d = 1;     }     if (c ==  e ) {       d = 2;     }     if (c ==  d ) {       d = 3;     }     if (c ==  a ) {       d = 4;     }     if (c ==  b ) {       d = 5;     }     if (c ==  c ) {       d = 6;     }     if (m % 4 == 0 || m % 4 == 2)       ans = (m / 4) * 16 + d;     else       ans = (m / 4) * 16 + 7 + d;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long cnt[10];   long long w, ans;   void dfs(int pos, long long c) {     long long rec;     if (pos > 8) {       ans = max(ans, w - c);       return;     }     rec = min(cnt[pos], c / pos);     for (long long i = rec; i >= max(rec - 8, 0LL); i--)       dfs(pos + 1, c - i * pos);   }   int main() {     cin >> w;     for (int i = 1; i <= 8; i++) cin >> cnt[i];     ans = 0;     dfs(1, w);     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 300000;   vector<int> g[MAX_N];   int sz[MAX_N];   int x[MAX_N];   int p[MAX_N];   int N;   int dfs(int v) {     sz[v] = 1;     p[v] = N;     x[N++] = v;     for (int i = 0; i < g[v].size(); i++) {       sz[v] += dfs(g[v][i]);     }     return sz[v];   }   int main() {     int n, q, a, b;     cin >> n >> q;     for (int i = 1; i < n; i++) {       cin >> a;       g[a - 1].push_back(i);     }     for (int i = 0; i < n; i++) sort(g[i].begin(), g[i].end());     N = 0;     dfs(0);     for (int i = 0; i < q; i++) {       cin >> a >> b;       a--;       if (sz[a] < b)         cout << -1 << endl;       else         cout << x[p[a] + b - 1] + 1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, k;   long long pr[300005], cr[300005];   long long nr[300005][2], fr[300005][2];   bool visit[300005], visit1[300005];   char str[300005];   vector<long long> vr[300005], br[300005];   void pre(), dfs(long long, long long);   long long uni(long long, long long), ccon(long long), ffind(long long);   int main() {     pre();     for (int i = 1; i < k + 1; i++)       if (!visit[i]) dfs(i, 0);     long long ans = 0;     for (int idx = 1; idx < n + 1; idx++) {       if (br[idx].size() == 1) {         long long v1 = br[idx][0];         ans -= ccon(v1);         if (str[idx] ==  0 ) {           fr[ffind(v1)][cr[v1]] = 1;         } else {           fr[ffind(v1)][!cr[v1]] = 1;         }         ans += ccon(v1);       } else if (br[idx].size() == 2) {         long long v1 = br[idx][0], v2 = br[idx][1];         if (ffind(v1) != ffind(v2)) {           ans -= ccon(v1) + ccon(v2);           long long p = uni(ffind(v1), ffind(v2));           ans += ccon(p);         }       }       cout << ans <<   n ;     }   }   long long ffind(long long idx) {     if (pr[idx] != idx) return pr[idx] = ffind(pr[idx]);     return idx;   }   long long uni(long long x, long long y) {     if (x == y) return x;     if (nr[x][0] + nr[x][1] < nr[y][0] + nr[y][1]) swap(x, y);     pr[y] = x;     nr[x][0] += nr[y][0];     nr[x][1] += nr[y][1];     fr[x][0] += fr[y][0];     fr[x][1] += fr[y][1];     return x;   }   long long ccon(long long idx) {     long long c = cr[idx];     long long p = ffind(idx);     if (fr[p][0]) return nr[p][0];     if (fr[p][1]) return nr[p][1];     return min(nr[p][0], nr[p][1]);   }   void pre() {     cin >> n >> k;     cin >> str + 1;     for (int i = 1; i < k + 1; i++) {       long long s;       cin >> s;       for (int j = 0; j < s; j++) {         long long x;         cin >> x;         vr[i].push_back(x);         br[x].push_back(i);       }     }   }   void dfs(long long idx, long long t) {     if (visit[idx]) return;     visit[idx] = true;     nr[idx][t] = 1;     cr[idx] = t;     pr[idx] = idx;     for (int i = 0; i < vr[idx].size(); i++) {       long long v = vr[idx][i];       for (int j = 0; j < br[v].size(); j++) {         long long node = br[v][j];         dfs(node, str[v] ==  1  ? t : !t);       }     }   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<string> s;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n;     s.resize(n);     for (int i = 0; i < n; i++) cin >> s[i];     for (int i = n - 1; i > 0; i--) {       int l = 1, r = min(s[i].length(), s[i - 1].length());       if (s[i - 1].compare(s[i]) < 0) continue;       while (l < r) {         int mid = l + r + 1 >> 1;         if (s[i - 1].compare(0, mid, s[i]) <= 0)           l = mid;         else           r = mid - 1;       }       s[i - 1] = s[i - 1].substr(0, l);     }     for (int i = 0; i < n; i++) cout << s[i] << endl;   }


#include <bits/stdc++.h>   using namespace std;   inline char gc() {     static char buf[100000], *p1 = buf, *p2 = buf;     return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)                ? EOF                : *p1++;   }   inline long long read() {     long long x = 0;     char ch = getchar();     bool positive = 1;     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) positive = 0;     for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ;     return positive ? x : -x;   }   inline void write(long long a) {     if (a < 0) {       a = -a;       putchar( - );     }     if (a >= 10) write(a / 10);     putchar( 0  + a % 10);   }   inline void writeln(long long a) {     write(a);     puts(  );   }   inline void wri(long long a) {     write(a);     putchar(   );   }   const int N = 205;   const long long inf = 4e18;   int n, m;   long long f[N][N], k, g[N][N];   string F[N];   long long sol(string x) {     int len = x.length();     x =     + x;     int p = 0;     memset(f, 0, sizeof(f));     for (int i = 1; i <= len; i++) x[i] -=  0 ;     f[0][x[1]] = 1;     for (int i = 1; i <= len; i++) {       if (i > 1) p = f[p][x[i]];       if (i < len && x[i + 1] == 0)         f[i][0] = i + 1;       else         f[i][0] = f[p][0];       if (i < len && x[i + 1] == 1)         f[i][1] = i + 1;       else         f[i][1] = f[p][1];     }     for (int i = 0; i <= len; i++) {       g[i][0] = f[i][0] == len;       g[i][1] = f[i][1] == len;     }     for (int i = 2; i <= n; i++) {       for (int j = 0; j <= len; j++) {         f[j][i] = f[f[j][i - 2]][i - 1];         g[j][i] = min(inf, g[j][i - 2] + g[f[j][i - 2]][i - 1]);       }     }     return g[0][n];   }   int main() {     n = read();     k = read();     m = read();     string s =   ;     F[0] =  0 ;     F[1] =  1 ;     for (int i = 2; i <= n; i++) {       F[i] = F[i - 2] + F[i - 1];       if (F[i].length() > m) F[i] = F[i].substr(F[i].length() - m, m);     }     m = min(m, (int)F[n].length());     for (int i = 1; i <= m; i++) {       if (s.length() &&           F[n].substr(F[n].length() - s.length(), s.length()) == s) {         if (--k == 0) {           cout << s << endl;           return 0;         }       }       s +=  0 ;       long long t = sol(s);       if (t < k) {         k -= t;         s.pop_back();         s +=  1 ;       }     }     cout << s << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int t, i, j, k, z, x, num[2];     string s;     cin >> t;     while (t--) {       cin >> s;       memset(num, 0, sizeof(num));       for (i = 0; i < s.size(); i++) {         if (s[i] ==  1 )           num[1]++;         else           num[0]++;       }       z = min(num[0], num[1]);       if (z % 2)         cout <<  DA n ;       else         cout <<  NET n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int const MAX = 2005;   int mp[MAX][MAX];   int main() {     int n;     bool flag = true;     scanf( %d , &n);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) scanf( %d , &mp[i][j]);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         if (mp[i][i] || mp[i][j] != mp[j][i] || (i != j && !mp[i][j])) {           printf( NO n );           return 0;         }       }     }     for (int i = 1; i <= n; i++) {       int u = 1;       for (int j = 1; j <= n; j++) {         if (i == j) continue;         if (mp[i][j] < mp[i][u]) u = j;       }       for (int j = 1; j <= n; j++) {         if (i == j) continue;         if (abs(mp[i][j] - mp[j][u]) != mp[i][u]) {           printf( NO n );           return 0;         }       }     }     printf( YES n );   }


#include <bits/stdc++.h>   using namespace std;   const int MAXPOW = 20;   const int MAXA = 1048577;   const int MAXN = 100009;   const int MAXM = 100009;   const int INF = 1e9 + 7;   struct Node {     Node(int val) : val(val) {}     int val;   };   struct MinIntervalTree {     MinIntervalTree(int n, int val) {       N = 1;       while (N < n) {         N *= 2;       }       nodes.resize(N * 2 + 2, Node(val));     }     int getMin(int L, int R) { return getMin(0, 0, N, L, R + 1); }     void set(int x, int val) { set(0, 0, N, x, val); }       private:     int getMin(int v, int L, int R, int A, int B) {       if (L == A && R == B) {         return nodes[v].val;       }       int M = (L + R) / 2;       if (B <= M) {         return getMin(v * 2 + 1, L, M, A, B);       }       if (A >= M) {         return getMin(v * 2 + 2, M, R, A, B);       }       return min(getMin(v * 2 + 1, L, M, A, M), getMin(v * 2 + 2, M, R, M, B));     }     void set(int v, int L, int R, int P, int val) {       if (L + 1 == R) {         nodes[v] = val;         return;       }       int M = (L + R) / 2;       if (P < M) {         set(v * 2 + 1, L, M, P, val);       } else {         set(v * 2 + 2, M, R, P, val);       }       nodes[v].val = min(nodes[v * 2 + 1].val, nodes[v * 2 + 2].val);     }     int N;     vector<Node> nodes;   };   void testTree() { MinIntervalTree t(10, 1e9); }   int main() {     ios::sync_with_stdio(false);     int n, s, l;     cin >> n >> s >> l;     vector<int> a(n);     vector<int> maxLeft(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];       maxLeft[i] = 0;     }     set<pair<int, int> > aliveUp;     set<pair<int, int> > aliveDown;     for (int i = n - 1; i >= 0; --i) {       {         auto it = aliveUp.lower_bound(make_pair(a[i] + s + 1, 0));         while (it != aliveUp.end()) {           aliveUp.erase(it);           maxLeft[it->second] = max(maxLeft[it->second], i + 1);           it = aliveUp.lower_bound(make_pair(a[i] + s + 1, 0));         }       }       {         auto it = aliveDown.lower_bound(make_pair(-a[i] + s + 1, 0));         while (it != aliveDown.end()) {           aliveDown.erase(it);           maxLeft[it->second] = max(maxLeft[it->second], i + 1);           it = aliveDown.lower_bound(make_pair(-a[i] + s + 1, 0));         }       }       aliveUp.insert(make_pair(a[i], i));       aliveDown.insert(make_pair(-a[i], i));     }     for (int i = 1; i < n; ++i) {       maxLeft[i] = max(maxLeft[i], maxLeft[i - 1]);     }     vector<int> dp(n + 1, 1e9);     MinIntervalTree T(n + 3, 1e9);     dp[0] = 0;     T.set(0, 0);     for (int i = 0; i < n; ++i) {       int L = maxLeft[i] - 1 + 1;       int R = i - l + 1;       if (L > R) {         continue;       }       dp[i + 1] = 1 + T.getMin(L, R);       T.set(i + 1, dp[i + 1]);     }     if (dp[n] >= 1e9) {       cout << -1 << endl;     } else {       cout << dp[n] << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     long long x, d, k = 1;     cin >> x >> d;     vector<long long> ans;     while (x) {       long long rem = 0;       for (int i = 31; i >= 0; --i) {         if (x >> i & 1) {           long long tmp = i;           if (i == 0) {             ans.push_back(k);             rem--;           }           while (tmp--) ans.push_back(k);           k += d;           rem++;         }       }       x = rem;     }     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); ++i) cout << ans[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t, n, k;     cin >> t;     while (t--) {       cin >> n >> k;       long long a[n], b[n], c = 0;       for (int i = 0; i < n; i++) cin >> a[i], b[i] = (a[i] % k);       sort(b, b + n);       reverse(b, b + n);       map<long long, long long> mp;       for (int i = 0; i < n && b[i]; i++)         if (i) {           if (b[i - 1] == b[i])             mp[b[i]] += k;           else             mp[b[i]] = k - b[i] + 1;         } else           mp[b[i]] = k - b[i] + 1;       for (auto &it : mp) c = max(c, it.second);       cout << c <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   map<int, vector<pair<int, int> > > mp;   int n, tt;   int arr[300000];   int main() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> arr[i];     for (int r = 1; r <= n; r++) {       long long sum = 0;       for (int l = r; l >= 1; l--) {         sum += arr[l];         mp[sum].push_back(make_pair(l, r));       }     }     map<int, vector<pair<int, int> > >::iterator it;     vector<pair<int, int> > ans;     for (it = mp.begin(); it != mp.end(); it++) {       int sum = 0;       vector<pair<int, int> > &now = it->second;       vector<pair<int, int> > cnt;       int r = -1;       for (int i = 0; i < now.size(); i++) {         if (now[i].first > r)           sum++, r = now[i].second,                  cnt.push_back(make_pair(now[i].first, now[i].second));       }       if (sum > tt) ans = cnt, tt = sum;     }     cout << tt << endl;     for (int i = 0; i < ans.size(); i++)       cout << ans[i].first <<     << ans[i].second << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, d, v[102], c, g[102], k, S;   int main(void) {     cin >> n >> d;     for (int i = 1; i <= n; i++) cin >> v[i];     for (int i = 1; i < n; i++) {       if (v[i] % 2)         c++;       else         c--;       if (c == 0) g[k++] = abs(v[i] - v[i + 1]);     }     sort(g, g + k);     for (S = 0; S < k && d >= g[S]; S++) d -= g[S];     cout << S;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     int n, m, k;     cin >> n >> m >> k;     if (k > 1) {       if (m == n) {         cout << 2 * k + 1 << endl;         for (int i = (int)(1); i < ((int)k + 1); i++) {           cout << 1 <<    ;         }         cout << n <<    ;         for (int i = (int)(1); i < ((int)k); i++) {           cout << n + 1 <<    ;         }         cout << n + m << endl;       } else {         cout << 2 * k << endl;         for (int i = (int)(1); i < ((int)k + 1); i++) {           cout << 1 <<    ;         }         cout << n <<    ;         for (int i = (int)(1); i < ((int)k); i++) {           cout << n + 1 <<    ;         }         cout << endl;       }     } else {       if (m + 2 <= n) {         cout << 2 << endl;         cout << 1 <<     << n << endl;       } else {         if (n == 2 and m == 2) {           cout << 4 << endl;           cout <<  1 2 3 4  << endl;         } else {           cout << 3 << endl;           cout << 1 <<     << n <<     << 2 * n - 1 << endl;         }       }     }     return 0;   }


#include <bits/stdc++.h>   int** arr;   std::vector<int> opers;   void del3(int x, int y) {     if (arr[x][y] && arr[x + 1][y] && arr[x][y + 1]) {       opers.push_back(x + 1);       opers.push_back(y + 1);       opers.push_back(x + 2);       opers.push_back(y + 1);       opers.push_back(x + 1);       opers.push_back(y + 2);       arr[x][y] = 0;       arr[x + 1][y] = 0;       arr[x][y + 1] = 0;     } else if (arr[x][y] && arr[x + 1][y] && arr[x + 1][y + 1]) {       opers.push_back(x + 1);       opers.push_back(y + 1);       opers.push_back(x + 2);       opers.push_back(y + 1);       opers.push_back(x + 2);       opers.push_back(y + 2);       arr[x][y] = 0;       arr[x + 1][y] = 0;       arr[x + 1][y + 1] = 0;     } else if (arr[x][y + 1] == 1 && arr[x + 1][y] && arr[x + 1][y + 1]) {       opers.push_back(x + 1);       opers.push_back(y + 2);       opers.push_back(x + 2);       opers.push_back(y + 1);       opers.push_back(x + 2);       opers.push_back(y + 2);       arr[x][y + 1] = 0;       arr[x + 1][y] = 0;       arr[x + 1][y + 1] = 0;     } else if (arr[x][y] == 1 && arr[x][y + 1] && arr[x + 1][y + 1]) {       opers.push_back(x + 1);       opers.push_back(y + 1);       opers.push_back(x + 1);       opers.push_back(y + 2);       opers.push_back(x + 2);       opers.push_back(y + 2);       arr[x][y] = 0;       arr[x][y + 1] = 0;       arr[x + 1][y + 1] = 0;     }   }   void del2(int x, int y) {     bool fl = false;     if (arr[x][y] == 0) {       opers.push_back(x + 1);       opers.push_back(y + 1);       arr[x][y] = 1;     } else {       if (!fl) {         opers.push_back(x + 1);         opers.push_back(y + 1);         arr[x][y] = 0;         fl = true;       }     }     if (arr[x + 1][y] == 0) {       opers.push_back(x + 2);       opers.push_back(y + 1);       arr[x + 1][y] = 1;     } else {       if (!fl) {         opers.push_back(x + 2);         opers.push_back(y + 1);         fl = true;         arr[x + 1][y] = 0;       }     }     if (arr[x][y + 1] == 0) {       opers.push_back(x + 1);       opers.push_back(y + 2);       arr[x][y + 1] = 1;     } else {       if (!fl) {         opers.push_back(x + 1);         opers.push_back(y + 2);         arr[x][y + 1] = 0;         fl = true;       }     }     if (arr[x + 1][y + 1] == 0) {       opers.push_back(x + 2);       opers.push_back(y + 2);       arr[x + 1][y + 1] = 1;     } else {       if (!fl) {         opers.push_back(x + 2);         opers.push_back(y + 2);         arr[x + 1][y + 1] = 0;         fl = true;       }     }     del3(x, y);   }   void del1(int x, int y) {     int h = 0;     if (arr[x][y] == 1) {       opers.push_back(x + 1);       opers.push_back(y + 1);       arr[x][y] = 0;     } else {       if (h < 2) {         opers.push_back(x + 1);         opers.push_back(y + 1);         arr[x][y] = 1;         h++;       }     }     if (arr[x + 1][y] == 1) {       opers.push_back(x + 2);       opers.push_back(y + 1);       arr[x + 1][y] = 0;     } else {       if (h < 2) {         opers.push_back(x + 2);         opers.push_back(y + 1);         arr[x + 1][y] = 1;         h++;       }     }     if (arr[x][y + 1] == 1) {       opers.push_back(x + 1);       opers.push_back(y + 2);       arr[x][y + 1] = 0;     } else {       if (h < 2) {         opers.push_back(x + 1);         opers.push_back(y + 2);         arr[x][y + 1] = 1;         h++;       }     }     if (arr[x + 1][y + 1] == 1) {       opers.push_back(x + 2);       opers.push_back(y + 2);       arr[x + 1][y + 1] = 0;     } else {       if (h < 2) {         opers.push_back(x + 2);         opers.push_back(y + 2);         arr[x + 1][y + 1] = 1;         h++;       }     }     del2(x, y);   }   void setup(int x, int y) {     int iu = arr[x][y] + arr[x][y + 1] + arr[x + 1][y] + arr[x + 1][y + 1];     if (iu == 3) {       del3(x, y);     } else if (iu == 2) {       del2(x, y);     } else if (iu == 1) {       del1(x, y);     } else if (iu == 4) {       del3(x, y);       del1(x, y);     }     arr[x][y] = 0;     arr[x][y + 1] = 0;     arr[x + 1][y] = 0;     arr[x + 1][y + 1] = 0;   }   int main() {     int t;     std::cin >> t;     for (int i = 0; i < t; i++) {       int n, m;       std::cin >> n >> m;       arr = new int*[n];       for (int i = 0; i < n; i++) arr[i] = new int[m];       for (int i = 0; i < n; i++) {         std::string s;         std::cin >> s;         for (int j = 0; j < s.size(); j++) {           arr[i][j] = s[j] -  0 ;         }       }       for (int a = 0; a < n - 1; a++) {         for (int b = 0; b < m - 1; b++) {           setup(a, b);         }       }       std::cout << opers.size() / 6 <<   n ;       for (int i = 0; i < opers.size() / 6; i++) {         for (int j = 0; j < 6; j++) {           std::cout << opers[i * 6 + j] <<    ;         }         std::cout <<   n ;       }       opers.clear();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, k, ans = 0;   int main() {     cin >> n >> m >> k;     if (n - 1 + m - 1 < k) {       cout << -1 << endl;       return 0;     }     long long buf = sqrt(n);     buf++;     for (int i = 0; i <= buf; i++) {       long long x = n / (i + 1);       if (k - i < 0) continue;       long long y = m / (k - i + 1);       ans = max(ans, x * y);     }     for (int i = 1; i <= buf; i++) {       long long cut = n / i - 1;       if (cut < 0) continue;       long long left = k - cut;       if (left < 0) left = 0;       ans = max(ans, i * (m / (left + 1)));     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     int N;     cin >> N;     long long int dp_D = 1, dp = 0, dp_temp;     for (int i = 0; i < N; i++) {       dp_temp = dp_D;       dp_D = 3 * dp;       dp = dp * 2 + dp_temp;       dp %= 1000000007;       dp_D %= 1000000007;     }     cout << dp_D << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int T = 1;     while (T--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long cal(long long x) { return (x + 1) * (x + 2) >> 1; }   int main() {     int a, b, c, l;     while (scanf( %d%d%d%d , &a, &b, &c, &l) != EOF) {       long long ans = 0;       for (int i = 0; i <= l; i++) ans += 1LL * (i + 1) * (i + 2) / 2;       for (long long i = 0; i <= l; i++) {         if (a + i >= b + c) ans -= cal(min(l - i, a + i - b - c));         if (b + i >= a + c) ans -= cal(min(l - i, b + i - a - c));         if (c + i >= a + b) ans -= cal(min(l - i, c + i - a - b));       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e9 + 3;   long long n, sum;   vector<int> vec;   int32_t main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n;     for (int i = n - 100; i <= n; i++) {       long long x = i;       while (x) {         sum += x % 10;         x /= 10;       }       sum += i;       if (sum == n) vec.push_back(i);       sum = 0;     }     cout << vec.size() << endl;     for (int i = 0; i < vec.size(); i++) cout << vec[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline void read(int &x) {     int v = 0, f = 1;     char c = getchar();     while (!isdigit(c) && c !=  - ) c = getchar();     if (c ==  - )       f = -1;     else       v = (c & 15);     while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);     x = v * f;   }   inline void read(long long &x) {     long long v = 0ll, f = 1ll;     char c = getchar();     while (!isdigit(c) && c !=  - ) c = getchar();     if (c ==  - )       f = -1;     else       v = (c & 15);     while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);     x = v * f;   }   inline void readc(char &x) {     char c;     while (((c = getchar()) ==    ) || c ==   n )       ;     x = c;   }   long long ans;   int n, m, k, i, j, fa[150005][19], dep[150005], first[150005], ed[150005], ti,       px[150005], py[150005];   int tt, lst[150005], stk[150005], tp;   vector<int> ne[150005];   vector<int> e[150005];   vector<int> v[150005];   vector<int> add[150005];   void addedg(int x, int y) {     if (lst[x] != tt) {       lst[x] = tt;       ne[x].clear();     }     if (lst[y] != tt) {       lst[y] = tt;       ne[y].clear();     }     ne[x].push_back(y);   }   bool cmp(int x, int y) { return first[x] < first[y]; }   void dfs1(int x, int p) {     fa[x][0] = p;     first[x] = ++ti;     for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++)       if (*it != p) {         dep[*it] = dep[x] + 1;         dfs1(*it, x);       }     ed[x] = ti;   }   int lca(int x, int y) {     if (dep[x] < dep[y]) swap(x, y);     int t = dep[x] - dep[y], i;     for ((i) = (18); (i) >= (0); (i)--)       if ((t >> i) & 1) x = fa[x][i];     if (x == y) return x;     for ((i) = (18); (i) >= (0); (i)--)       if (fa[x][i] != fa[y][i]) {         x = fa[x][i];         y = fa[y][i];       }     return fa[x][0];   }   int gtf(int x, int y) {     int i;     for ((i) = (18); (i) >= (0); (i)--)       if ((y >> i) & 1) x = fa[x][i];     return x;   }   struct bit {     int c[150005];     void add(int x, int y) {       while (x <= n) {         c[x] += y;         x += (x & -x);       }     }     int query(int x) {       int s = 0;       while (x) {         s += c[x];         x -= (x & -x);       }       return s;     }   } tr;   void dfs2(int x) {     for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++) {       if (dep[px[*it]] - dep[x] >= k) {         int t = gtf(px[*it], dep[px[*it]] - dep[x] - k);         ans += tr.query(ed[t]) - tr.query(first[t] - 1);       }       if (dep[py[*it]] - dep[x] >= k) {         int t = gtf(py[*it], dep[py[*it]] - dep[x] - k);         ans += tr.query(ed[t]) - tr.query(first[t] - 1);       }     }     for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++) {       tr.add(first[px[*it]], 1);       tr.add(first[py[*it]], 1);     }     for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++)       if (*it != fa[x][0]) {         dfs2(*it);       }     for (__typeof((v[x]).begin()) it = (v[x]).begin(); it != (v[x]).end(); it++) {       tr.add(first[px[*it]], -1);       tr.add(first[py[*it]], -1);     }   }   void ins(int x) {     if (x == 1) return;     int z = lca(x, stk[tp]);     if (z == stk[tp]) {       stk[++tp] = x;       return;     }     while (tp > 1 && first[stk[tp - 1]] >= first[z]) {       addedg(stk[tp - 1], stk[tp]);       tp--;     }     if (stk[tp] != z) {       addedg(z, stk[tp]);       stk[tp] = z;     }     stk[++tp] = x;   }   multiset<int> s[150005];   int rt[150005], tot, lc[3000005], rc[3000005], sum[3000005];   void update(int &x, int l, int r, int y) {     if (!x) {       x = ++tot;       lc[x] = rc[x] = sum[x] = 0;     }     sum[x]++;     if (l == r) return;     int mid = (l + r) / 2;     if (y <= mid)       update(lc[x], l, mid, y);     else       update(rc[x], mid + 1, r, y);   }   void merge(int &x, int y, int l, int r) {     if (!x || !y) {       x = x + y;       return;     }     if (l == r) {       sum[x] += sum[y];       return;     }     int mid = (l + r) / 2;     merge(lc[x], lc[y], l, mid);     merge(rc[x], rc[y], mid + 1, r);     sum[x] = sum[lc[x]] + sum[rc[x]];   }   int query(int x, int l, int r, int ql, int qr) {     if (!x) return 0;     if (ql <= l && r <= qr) return sum[x];     int mid = (l + r) / 2, s = 0;     if (ql <= mid) s += query(lc[x], l, mid, ql, qr);     if (qr > mid) s += query(rc[x], mid + 1, r, ql, qr);     return s;   }   void dfs3(int x) {     if (dep[x] - dep[i] >= k) {       int t = gtf(x, dep[x] - dep[i] - k);       ans += 1ll * add[x].size() * (tr.query(ed[t]) - tr.query(first[t] - 1));     }     for (__typeof((add[x]).begin()) it = (add[x]).begin(); it != (add[x]).end();          it++) {       tr.add(first[*it], 1);     }     rt[x] = 0;     s[x].clear();     for (__typeof((add[x]).begin()) it = (add[x]).begin(); it != (add[x]).end();          it++) {       s[x].insert(*it);       if (dep[x] - dep[i] >= k) {         int t = gtf(x, k - 1);         if (first[t] > 1) ans += query(rt[x], 1, n, 1, first[t] - 1);         if (ed[t] < n) ans += query(rt[x], 1, n, ed[t] + 1, n);       } else {         if (dep[x] - dep[i] + dep[*it] - dep[i] < k) continue;         int t = gtf(*it, dep[*it] - dep[i] - (k - (dep[x] - dep[i])));         ans += query(rt[x], 1, n, first[t], ed[t]);       }       update(rt[x], 1, n, first[*it]);     }     for (__typeof((ne[x]).begin()) it = (ne[x]).begin(); it != (ne[x]).end();          it++) {       dfs3(*it);       if (s[x].size() < s[*it].size()) {         swap(s[x], s[*it]);         swap(rt[x], rt[*it]);       }       for (__typeof((s[*it]).begin()) it2 = (s[*it]).begin();            it2 != (s[*it]).end(); it2++) {         if (dep[x] - dep[i] >= k) {           int t = gtf(x, k - 1);           if (first[t] > 1) ans += query(rt[x], 1, n, 1, first[t] - 1);           if (ed[t] < n) ans += query(rt[x], 1, n, ed[t] + 1, n);         } else {           if (dep[x] - dep[i] + dep[*it2] - dep[i] < k) continue;           int t = gtf(*it2, dep[*it2] - dep[i] - (k - (dep[x] - dep[i])));           ans += query(rt[x], 1, n, first[t], ed[t]);         }         s[x].insert(*it2);       }       merge(rt[x], rt[*it], 1, n);     }   }   int main() {     read(n);     read(m);     read(k);     for (((i)) = (1); ((i)) <= ((n - 1)); ((i))++) {       int x, y;       read(x);       read(y);       e[x].push_back(y);       e[y].push_back(x);     }     dfs1(1, 0);     for (((j)) = (1); ((j)) <= ((18)); ((j))++)       for (((i)) = (1); ((i)) <= ((n)); ((i))++)         fa[i][j] = fa[fa[i][j - 1]][j - 1];     for (((i)) = (1); ((i)) <= ((m)); ((i))++) {       read(px[i]);       read(py[i]);       if (first[px[i]] > first[py[i]]) swap(px[i], py[i]);       v[lca(px[i], py[i])].push_back(i);     }     dfs2(1);     for (((i)) = (1); ((i)) <= ((n)); ((i))++) {       for (__typeof((v[i]).begin()) it = (v[i]).begin(); it != (v[i]).end();            it++) {         add[px[*it]].push_back(py[*it]);       }       vector<int> a;       for (__typeof((v[i]).begin()) it = (v[i]).begin(); it != (v[i]).end();            it++) {         a.push_back(px[*it]);         a.push_back(py[*it]);       }       a.push_back(1);       sort((a).begin(), (a).end(), cmp);       a.resize(unique((a).begin(), (a).end()) - a.begin());       tt++;       ne[1].clear();       stk[tp = 1] = 1;       for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) {         ins(*it);       }       while (tp > 1) {         addedg(stk[tp - 1], stk[tp]);         tp--;       }       tot = 0;       dfs3(1);       for (__typeof((v[i]).begin()) it = (v[i]).begin(); it != (v[i]).end();            it++) {         add[px[*it]].clear();       }       for (__typeof((v[i]).begin()) it = (v[i]).begin(); it != (v[i]).end();            it++) {         tr.add(first[py[*it]], -1);       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string d[] = { monday ,  tuesday ,   wednesday ,  thursday ,                    friday ,  saturday ,  sunday };     string a, b;     int e, c, g, f, u;     bool o;     cin >> a >> b;     for (int i = 0; i < 7; i++) {       if (a == d[i]) {         e = i;       }       if (b == d[i]) {         c = i;       }     }     u = abs(e - c);     for (int m = 1; m <= 12; m++) {       if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {         g = 4;         f = e - g;         if (f < 0) {           f = 7 - abs(f);         }         if (d[f] == d[c]) {           o = 1;           cout <<  YES ;           break;         } else           o = 0;       } else if (m == 2) {         g = 0;         f = e - g;         if (f < 0) {           f = 7 - abs(f);         }         if (d[f] == d[c]) {           o = 1;           cout <<  YES ;           break;         } else           o = 0;       } else if (m == 4 || m == 6 || m == 9 || m == 1) {         g = 5;         f = e - g;         if (f < 0) {           f = 7 - abs(f);         }         if (d[f] == d[c]) {           o = 1;           cout <<  YES ;           break;         } else           o = 0;       }     }     if (o == 0) {       cout <<  NO ;     }   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     long long arr[n];     for (long long i = 0; i < n; ++i) {       cin >> arr[i];     }     stack<pair<long long, long long>> s1, s2;     s1.push({0, -1});     long long left[n], right[n];     for (long long i = 0; i < n; ++i) {       while (s1.top().first >= arr[i]) s1.pop();       left[i] = s1.top().second + 1;       s1.push({arr[i], i});     }     s2.push({0, n});     for (long long i = n - 1; i >= 0; --i) {       while (s2.top().first >= arr[i]) s2.pop();       right[i] = s2.top().second - 1;       s2.push({arr[i], i});     }     long long ans[n + 1];     memset(ans, 0, sizeof(ans));     for (long long i = 0; i < n; ++i) {       ans[right[i] - left[i] + 1] = max(arr[i], ans[right[i] - left[i] + 1]);     }     for (long long i = n - 1; i >= 1; --i) {       if (ans[i] < ans[i + 1]) ans[i] = ans[i + 1];     }     for (long long i = 0; i < n; ++i) {       cout << ans[i + 1] <<    ;     }   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, c, d, cnt = 1;   string s;   bool ok = true, mtest = true;   void solve() {     cin >> b;     vector<int> v(b), vc(b);     for (auto& i : v) cin >> i;     for (auto& i : vc) cin >> i;     sort(v.begin(), v.end());     sort(vc.begin(), vc.end());     for (auto i : v) cout << i <<    ;     cout <<   n ;     for (auto i : vc) cout << i <<    ;     cout <<   n ;   }   int main() {     cin >> a;     while (a--) solve();     cerr <<   nTime elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<   s. n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n;     vector<int> a(n);     vector<pair<int, int> > d;     for (int i = 0; i < n; i++) {       cin >> a[i];       d.push_back(make_pair(a[i], 0));     }     cin >> m;     vector<int> b(m);     vector<pair<int, int> > c(n + m);     vector<pair<int, int> > sum(n + m, make_pair(0, 0));     for (int i = 0; i < m; i++) {       cin >> b[i];       d.push_back(make_pair(b[i], 1));     }     sort(d.begin(), d.end());     int tog = 0;     if (d[0].second)       sum[0].second = 1;     else       sum[0].first = 1;     for (int i = 0; i < n + m; i++) {       if (i > 0) {         sum[i].first = sum[i - 1].first;         sum[i].second = sum[i - 1].second;       }       if (!d[i].second) {         tog++;         if (i > 0) sum[i].first++;       } else {         tog--;         if (i > 0) sum[i].second++;       }       c[i].first = tog;       c[i].second = i;     }     sort(c.begin(), c.end());     if (c[0].first < 0)       cout << 3 * n - sum[c[0].second].first <<  :             << 3 * m - sum[c[0].second].second << endl;     else       cout << 3 * n <<  :  << 3 * m;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 1, M = 1e9 + 7;   mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());   int n, q, bit[N], inv[N], a[N], last[N], INVER[N], ans[N];   bool prime[N];   vector<int> v[N];   struct query {     int l, r, id;     void sc(int temp) {       scanf( %d%d , &l, &r);       id = temp;     }   } Q[N];   int power(int base, int to) {     int ret = 1;     while (to) {       if (to & 1) ret = ret * 1LL * base % M;       to >>= 1;       base = base * 1LL * base % M;     }     return ret;   }   int INV(int a) {     if (INVER[a] != -1) return INVER[a];     return INVER[a] = power(a, M - 2);   }   void add(int at, int *bit, int val) {     for (; at < N; at += (-at) & at) bit[at] = bit[at] * 1LL * val % M;   }   int get(int at, int *bit) {     int ret = 1;     for (; at; at -= at & (-at)) ret = ret * 1LL * bit[at] % M;     return ret;   }   int main() {     for (int i = 0; i < N; i++) bit[i] = inv[i] = 1;     memset(last, -1, sizeof last);     memset(INVER, -1, sizeof INVER);     scanf( %d , &n);     for (int i = 2; i < N; i++)       if (!prime[i]) {         for (int j = i; j < N; j += i) {           prime[j] = 1;           v[j].push_back(i);         }       }     for (int i = 1; i <= n; i++) scanf( %d , a + i);     scanf( %d , &q);     for (int i = 0; i < q; i++) Q[i].sc(i);     sort(Q, Q + q, [&](query a, query b) { return a.r < b.r; });     int j = 1;     for (int i = 0; i < q; i++) {       while (j <= Q[i].r) {         add(j, bit, a[j]);         add(j, inv, INV(a[j]));         for (auto k : v[a[j]]) {           if (last[k] != -1) {             add(last[k], bit, INV(k - 1) * 1LL * k % M);             add(last[k], inv, (k - 1LL) * INV(k) % M);           }           last[k] = j;           add(last[k], bit, (k - 1LL) * INV(k) % M);           add(last[k], inv, k * 1LL * INV(k - 1) % M);         }         j++;       }       ans[Q[i].id] = get(Q[i].r, bit);       ans[Q[i].id] = ans[Q[i].id] * 1LL * get(Q[i].l - 1, inv) % M;     }     for (int i = 0; i < q; i++) printf( %d n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   int ans[300005];   int main() {     int n, m;     scanf( %d%d , &n, &m);     set<int> alive;     for (int i = 1; i <= n; i++) alive.insert(i);     for (int i = 0; i < m; i++) {       int l, r, x;       scanf( %d%d%d , &l, &r, &x);       set<int>::iterator it = alive.lower_bound(l);       vector<int> dead;       while (it != alive.end()) {         int now = *it;         if (now > r) break;         if (now != x) dead.push_back(now), ans[now] = x;         it++;       }       for (int j = 0; j < (int)((dead).size()); j++) alive.erase(dead[j]);     }     for (int i = 1; i <= n; i++) printf( %d%c , ans[i], i == n ?   n  :    );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, long long> da;   inline long long p(long long x) { return x * x * x; }   long long m;   void dfs(long long now, int sum, long long maxnum) {     if (!now)       da = max(da, make_pair(sum, maxnum));     else {       long long i = 1;       for (; p(i + 1) <= now; ++i)         ;       dfs(now - p(i), sum + 1, maxnum + p(i));       if (now) dfs(p(i) - p(i - 1) - 1, sum + 1, maxnum + p(i - 1));     }   }   int main() {     scanf( %lld , &m);     dfs(m, 0, 0);     printf( %d %lld , da.first, da.second);   }


#include <bits/stdc++.h>   std::map<std::string, std::string> opcodes;   std::string next(std::string s) {     std::string res = s;     res[res.size() - 1]++;     for (int i = (int)res.size() - 1; i >= 0; i--) {       if (res[i] >  f ) {         res[i] =  a ;         res[i - 1]++;       }     }     return res;   }   int check(std::string s) {     if (s.size() == 1) return !!(s[0] ==  a );     std::string f = s.substr(0, 2);     s.erase(0, 2);     auto it = opcodes.find(f);     if (it == opcodes.end()) return 0;     s = it->second + s;     return check(s);   }   int main() {     std::ios_base::sync_with_stdio(0);     int n, q;     std::cin >> n >> q;     std::string str =   , fff, a, b;     for (int i = 0; i < n; i++) str.push_back( a );     for (int i = 0; i < n; i++) fff.push_back( f );     for (int i = 0; i < q; i++) {       std::cin >> a >> b;       opcodes[a] = b;     }     int ans = 0;     while (true) {       ans += check(str);       if (str == fff)         break;       else         str = next(str);     }     std::cout << ans << std::endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int MOD = 1e9 + 7;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int n;     cin >> n;     long long int a[n];     for (long long int i = 0; i < n; i++) {       cin >> a[i];     }     if (n == 1) {       cout << 1 <<   n ;       return 0;     }     long long int pre[n + 5];     long long int suf[n + 5];     long long int ans = 1;     pre[0] = 1;     suf[n - 1] = 1;     for (long long int i = 1; i < n; i++) {       if (a[i] > a[i - 1]) {         pre[i] = pre[i - 1];         pre[i]++;       } else {         pre[i] = 1;       }       ans = max(pre[i], ans);     }     for (long long int i = n - 2; i >= 0; i--) {       if (a[i] < a[i + 1]) {         suf[i] = suf[i + 1];         suf[i]++;       } else {         suf[i] = 1;       }       ans = max(pre[i], ans);     }     for (long long int i = 1; i + 1 < n; i++) {       if (a[i + 1] - a[i - 1] >= 2) {         ans = max(ans, 1 + pre[i - 1] + suf[i + 1]);       }       ans = max(ans, 1 + pre[i - 1]);       ans = max(ans, 1 + suf[i + 1]);     }     ans = max(ans, 1 + suf[1]);     ans = max(ans, 1 + pre[n - 2]);     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double EPS = 1e-8;   const int mod = 1e9 + 7;   const int N = 1e6 + 10;   const long long INF = 1e18;   long long power(long long x, long long y) {     long long t = 1;     while (y > 0) {       if (y % 2)         y -= 1, t = t * x % mod;       else         y /= 2, x = x * x % mod;     }     return t;   }   vector<int> G[N];   vector<long long> snd[N];   long long sub[N], sum[N], red[N], dol[N];   long long mns[N], mxs[N];   int lol[N];   vector<pair<int, pair<long long, long long> > > edges;   void done() {     printf( -1 n );     exit(0);   }   void dfs(int v) {     for (int i = 0; i < G[v].size(); ++i) {       int ed = G[v][i];       dfs(edges[ed].first);       long long &w = edges[ed].second.first, &p = edges[ed].second.second;       if (mns[edges[ed].first] > p) done();       long long z = min(w - 1, p - mns[edges[ed].first]);       mns[v] += mns[edges[ed].first] + w - z;       snd[v].push_back(mns[edges[ed].first] + w - z);       mxs[v] += w + min(p, mxs[edges[ed].first]);       assert(mns[v] <= mxs[v]);     }   }   void dfs1(int v, long long nd) {     for (int i = 0; i < G[v].size(); ++i) nd -= snd[v][i];     for (int i = 0; i < G[v].size(); ++i) {       int ed = G[v][i], to = edges[ed].first;       long long &w = edges[ed].second.first, &p = edges[ed].second.second;       long long z = min(w - 1, p - mns[edges[ed].first]);       w -= z;       p -= z;       long long k = min(nd, z);       w += k;       p += k;       nd -= k;       k = min(min(nd, p - mns[edges[ed].first]),               mxs[edges[ed].first] - mns[edges[ed].first]);       dfs1(to, mns[edges[ed].first] + k);       nd -= k;     }     assert(nd == 0);   }   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n - 1; ++i) {       int x, y;       long long w, p;       scanf( %d%d%lld%lld , &x, &y, &w, &p);       x--;       y--;       G[x].push_back(edges.size());       lol[i] = x;       edges.push_back(make_pair(y, make_pair(w, p)));     }     dfs(0);     dfs1(0, mxs[0]);     printf( %d n , n);     for (int i = 0; i < n - 1; ++i) {       printf( %d %d %lld %lld n , lol[i] + 1, edges[i].first + 1,              edges[i].second.first, edges[i].second.second);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long M = 5005;   const int MOD = 998244353;   const int MX = 3e5 + 5;   const long long INF = 1e18;   const long double PI = acos((long double)-1);   void msm() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);   }   int sol();   int main() {     msm();     sol();     return 0;   }   int sol() {     long long n;     cin >> n;     ;     long long ar[105], arr[105][5005];     for (long long i = (0); i < (n); ++i) {       long long a;       cin >> a;       ;       ar[a]++;       arr[a][ar[a]] = i + 1;     }     long long y = min(ar[1], min(ar[2], ar[3]));     cout << y << endl;     for (long long i = (1); i <= (y); ++i)       cout << arr[1][i] <<     << arr[2][i] <<     << arr[3][i] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2>   inline void chkmin(T1 &x, T2 y) {     if (x > y) x = y;   }   template <typename T1, typename T2>   inline void chkmax(T1 &x, T2 y) {     if (x < y) x = y;   }   template <typename T, typename U>   inline ostream &operator<<(ostream &_out, const pair<T, U> &_p) {     _out << _p.first <<     << _p.second;     return _out;   }   template <typename T, typename U>   inline istream &operator>>(istream &_in, pair<T, U> &_p) {     _in >> _p.first >> _p.second;     return _in;   }   template <typename T>   inline ostream &operator<<(ostream &_out, const vector<T> &_v) {     if (_v.empty()) {       return _out;     }     _out << _v.front();     for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) {       _out <<     << *_it;     }     return _out;   }   template <typename T>   inline istream &operator>>(istream &_in, vector<T> &_v) {     for (auto &_i : _v) {       _in >> _i;     }     return _in;   }   template <typename T>   inline ostream &operator<<(ostream &_out, const set<T> &_s) {     if (_s.empty()) {       return _out;     }     _out << *_s.begin();     for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) {       _out <<     << *_it;     }     return _out;   }   template <typename T>   inline ostream &operator<<(ostream &_out, const multiset<T> &_s) {     if (_s.empty()) {       return _out;     }     _out << *_s.begin();     for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) {       _out <<     << *_it;     }     return _out;   }   template <typename T>   inline ostream &operator<<(ostream &_out, const unordered_set<T> &_s) {     if (_s.empty()) {       return _out;     }     _out << *_s.begin();     for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) {       _out <<     << *_it;     }     return _out;   }   template <typename T>   inline ostream &operator<<(ostream &_out, const unordered_multiset<T> &_s) {     if (_s.empty()) {       return _out;     }     _out << *_s.begin();     for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) {       _out <<     << *_it;     }     return _out;   }   template <typename T, typename U>   inline ostream &operator<<(ostream &_out, const map<T, U> &_m) {     if (_m.empty()) {       return _out;     }     _out <<  (  << _m.begin()->first <<  :   << _m.begin()->second <<  ) ;     for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) {       _out <<  , (  << _it->first <<  :   << _it->second <<  ) ;     }     return _out;   }   template <typename T, typename U>   inline ostream &operator<<(ostream &_out, const unordered_map<T, U> &_m) {     if (_m.empty()) {       return _out;     }     _out <<  (  << _m.begin()->first <<  :   << _m.begin()->second <<  ) ;     for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) {       _out <<  , (  << _it->first <<  :   << _it->second <<  ) ;     }     return _out;   }   const string FILENAME =  input ;   const int MAXN = 100001;   int n;   int x[MAXN], y[MAXN];   int c[MAXN];   int a[MAXN];   int id[MAXN];   int reals[MAXN];   int xs[MAXN], ys[MAXN];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     int curx = 0, cury = 0;     int last = 0;     int st = 0;     for (int i = 0; i < n; i++) {       cin >> x[i] >> y[i];       id[i] = i;       xs[i] = x[i];       ys[i] = y[i];     }     while (true) {       random_shuffle(id, id + n);       for (int i = 0; i < n; i++) {         x[i] = xs[i];         y[i] = ys[i];       }       long long lst = 0;       curx = 0;       cury = 0;       last = 0;       st = 0;       for (int i1 = 0; i1 < n; i1++) {         int i = id[i1];         long long res = 1e18;         for (int t = 0; t < 2; t++) {           for (int t1 = 0; t1 < 2; t1++) {             int nx = curx + x[i];             int ny = cury + y[i];             chkmin(res, 1LL * nx * nx + 1LL * ny * ny);             x[i] *= -1;             y[i] *= -1;           }           curx *= -1;           cury *= -1;         }         bool was = false;         for (int t = 0; t < 2; t++) {           int nx = curx + x[i];           int ny = cury + y[i];           if ((1LL * nx * nx + 1LL * ny * ny) == res) {             c[i1] = last ^ t;             if (i == 0) {               st = t;             }             was = true;             curx = nx;             cury = ny;             last = t;             break;           }           x[i] *= -1;           y[i] *= -1;         }         if (!was) {           st ^= 1;           last ^= 1;           curx *= -1;           cury *= -1;           for (int t = 0; t < 2; t++) {             int nx = curx + x[i];             int ny = cury + y[i];             if ((1LL * nx * nx + 1LL * ny * ny) == res) {               c[i1] = t ^ last;               if (i == 0) {                 st = t;               }               was = true;               curx = nx;               cury = ny;               last = t;               break;             }             x[i] *= -1;             y[i] *= -1;           }         }         lst = res;       }       if (lst <= 1500000LL * 1500000LL) {         break;       }     }     for (int i = 0; i < n; i++) {       if (i == 0) {         a[i] = st;       } else {         if (c[i]) {           a[i] = a[i - 1] ^ 1;         } else {           a[i] = a[i - 1];         }       }       reals[id[i]] = a[i];     }     for (int i = 0; i < n; i++) {       if (reals[i] == 0) {         cout << 1 <<    ;       } else {         cout << -1 <<    ;       }     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, d, l;   int main() {     cin >> n >> d >> m >> l;     long long x = 0;     for (long long i = 0; i < n; i++) {       if (x < i * m) {         break;       }       while (x <= i * m + l) x = (((i * m + l) / d) + 1) * d;     }     cout << x << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 40;   const int M = 20;   long long incident_mask[N];   vector<int> g[N];   int n, m, used[N];   long long ans = 0;   long long cntmask[1 << M];   template <typename T>   inline void chkmax(T &x, T y) {     x = max(x, y);   }   template <typename T>   inline void chkmin(T &x, T y) {     x = min(x, y);   }   template <typename T>   inline void read(T &x) {     T f = 1;     x = 0;     char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -f;     for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c -  0 ;     x *= f;   }   inline long long binpow(long long a, long long n) {     long long res = 1;     for (; n; n >>= 1, a = (long long)a * a)       if (n & 1) res = (long long)res * a;     return res;   }   inline void dfs(int x, int c) {     if (used[x]) return;     used[x] = c;     for (int y : g[x]) dfs(y, 3 - c);   }   inline long long CountIsolated() {     long long ans = 0;     for (int i = 0; i < n; ++i)       if (g[i].empty()) ++ans;     return ans;   }   inline long long CountComponents() {     memset(used, 0, sizeof(used));     long long ans = 0;     for (int i = 0; i < n; ++i)       if (!used[i]) {         ++ans;         dfs(i, 1);       }     return ans;   }   inline bool bipartite() {     memset(used, 0, sizeof(used));     for (int i = 0; i < n; ++i)       if (!used[i]) dfs(i, 1);     for (int i = 0; i < n; ++i)       for (int j : g[i])         if (used[i] == used[j]) return false;     return true;   }   inline long long CountIndependentSets() {     int m1 = min(n, 20);     int m2 = n - m1;     memset(cntmask, 0, sizeof(cntmask));     for (int i = 0; i < (1 << m1); ++i) {       long long curmask = 0;       bool good = true;       for (int j = 0; j < m1; ++j) {         if ((i & (1 << j)) == 0) continue;         if (curmask & (1 << j)) good = false;         curmask |= ((1 << j) | incident_mask[j]);       }       if (good) ++cntmask[curmask >> m1];     }     for (int i = 0; i < m2; ++i)       for (int j = 0; j < (1 << m2); ++j)         if (j & (1 << i)) cntmask[j] += cntmask[j ^ (1 << i)];     long long ans = 0;     for (int i = 0; i < (1 << m2); i++) {       long long curmask = 0;       bool good = true;       for (int j = m1; j < n; j++) {         if ((i & (1 << (j - m1))) == 0) continue;         if (curmask & (1LL << j)) good = false;         curmask |= (1LL << j) | incident_mask[j];       }       if (good) ans += cntmask[(i ^ ((1 << m2) - 1))];     }     return ans;   }   inline long long calc(int mask) {     if (mask == 0) return binpow(2, n);     if (mask == 1 || mask == 4) return CountIndependentSets();     if (mask == 2) return binpow(2, CountComponents());     if (mask == 3 || mask == 6) return binpow(2, CountIsolated());     if (mask == 5) return (bipartite() ? binpow(2, CountComponents()) : 0);     if (mask == 7) return (m == 0 ? binpow(2, n) : 0);     return 0;   }   int main() {     read(n);     read(m);     for (int i = 0; i < m; ++i) {       int u, v;       read(u), read(v);       --u, --v;       g[u].emplace_back(v);       g[v].emplace_back(u);       incident_mask[u] ^= (1LL << v);       incident_mask[v] ^= (1LL << u);     }     long long ans = 0;     for (int i = 0; i < 8; ++i)       if (__builtin_popcount(i) % 2 == 0)         ans += calc(i);       else         ans -= calc(i);     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1E9;   int main() {     int n;     string s;     cin >> n >> s;     for (int len = 1; len <= n; len++) {       bool ok = true;       for (int i = 0; i < n; i++) {         if (i + len * 4 < n && s[i] ==  *  && s[i] == s[i + len] &&             s[i] == s[i + len + len] && s[i] == s[i + len * 3] &&             s[i] == s[i + len * 4]) {           ok = 0;           break;         }       }       if (!ok) {         puts( yes );         return 0;       }     }     puts( no );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e7 + 100;   const int INF = 0x3FFFFFFF;   bool vis[MAXN];   int main() {     long long a, b, m;     cin >> a >> b >> m;     for (int i = 1; i <= a; i++) {       long long t = 1e9;       long long mod = i * t % m;       if (vis[mod]) break;       vis[mod] = 1;       if (b + mod < m && mod) {         cout <<  1  ;         int cnt = 0;         int temp = i;         while (i) {           i /= 10;           cnt++;         }         for (int j = 1; j <= 9 - cnt; j++) cout << 0;         cout << temp << endl;         return 0;       }     }     cout << 2 << endl;     return 0;   }