#include <bits/stdc++.h>   using namespace std;   vector<int> knapsack(long long lim, vector<int> allItems) {     vector<bool> mark;     int mx = 0;     for (auto it : allItems) mx += it;     mx = min((long long)mx, lim);     mark.assign(mx + 1, 0);     mark[0] = true;     for (auto it : allItems) {       for (int i = (int)mx; i >= (int)it; i--) {         mark[i] = max(mark[i], mark[i - it]);       }     }     vector<int> ret;     for (int i = (int)0; i <= (int)mx; i++)       if (mark[i]) ret.push_back(i);     return ret;   }   long long W;   long long cnt[9];   long long max840 = 0;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> W;     for (int i = (int)1; i <= (int)8; i++) cin >> cnt[i];     for (int i = (int)1; i <= (int)8; i++) {       int blockSize = 840 / i;       if (cnt[i] < blockSize) continue;       max840 += cnt[i] / blockSize;       cnt[i] %= blockSize;       if (cnt[i] < blockSize) max840 -= 1, cnt[i] += blockSize;     }     vector<int> allItems;     for (int i = (int)1; i <= (int)8; i++) {       int rem = cnt[i];       for (int k = (int)0; k <= (int)60; k++) {         if (rem < (1LL << k)) {           allItems.push_back(rem * i);           break;         } else {           rem -= (1LL << k);           allItems.push_back((1LL << k) * i);         }       }     }     vector<int> allCreatedValue = knapsack(W, allItems);     long long ans = 0;     for (auto it : allCreatedValue) {       long long left = W - it;       left -= 1LL * min(left / 840, max840) * 840;       ans = max(ans, W - left);     }     cout << ans <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   long long power(long long a, long long b) {     long long result = 1;     while (b > 0) {       if (b % 2 == 1) {         result *= a;       }       a *= a;       b /= 2;     }     return result;   }   long long gcd(long long x, long long y) {     long long r;     while (y != 0 && (r = x % y) != 0) {       x = y;       y = r;     }     return y == 0 ? x : y;   }   long long countSetBits(long long x) {     long long Count = 0;     while (x > 0) {       if (x & 1) Count++;       x = x >> 1;     }     return Count;   }   bool isPerfectSquare(long long n) {     long long sr = sqrt(n);     if (sr * sr == n)       return true;     else       return false;   }   long long mod(long long x, long long M) { return ((x % M + M) % M); }   long long add(long long a, long long b, long long M) {     return mod(mod(a, M) + mod(b, M), M);   }   long long mul(long long a, long long b, long long M) {     return mod(mod(a, M) * mod(b, M), M);   }   long long powerM(long long a, long long b, long long M) {     long long res = 1;     while (b) {       if (b % 2 == 1) {         res = mul(a, res, M);       }       a = mul(a, a, M);       b /= 2;     }     return res;   }   long long modInverse(long long n, long long M) { return powerM(n, M - 2, M); }   long long nCrM(long long n, long long r, long long M) {     if (n < r) return 0;     if (r == 0) return 1;     vector<long long> fact(n + 1);     fact[0] = 1;     for (long long i = 1; i <= n; i++) {       fact[i] = mul(fact[i - 1], i, M);     }     return mul(mul(fact[n], modInverse(fact[r], M), M),                modInverse(fact[n - r], M), M);   }   long long n;   vector<pair<long long, long long>> vv;   map<pair<long long, long long>, long long> vis;   long long ans = 0;   void dfs(pair<long long, long long> p) {     vis[p] = 1;     for (long long i = 0; i < n; i++) {       if (vis[vv[i]] == 0) {         if (vv[i].first == p.first || vv[i].second == p.second) {           dfs(vv[i]);         }       }     }   }   void solve() {     cin >> n;     for (long long i = 0; i < n; i++) {       long long x1, y1;       cin >> x1 >> y1;       vv.push_back({x1, y1});     }     for (long long i = 0; i < n; i++) {       if (vis[vv[i]] == 0) {         dfs(vv[i]);         ans++;       }     }     cout << ans - 1 <<   n ;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     solve();     return 0;   }


#include <bits/stdc++.h>   int main() {     long long n, k;     scanf( %lld %lld , &n, &k);     long long total(0), count(0);     for (long long p = 1; p <= n; p++) {       long long x;       scanf( %lld , &x);       if (p > 1 && total - x * (n - p) * (p - count - 1) < k) {         printf( %lld n , p);         ++count;       } else {         total += x * (p - 1 - count);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline int IN() {     int x = 0, ch = getchar(), f = 1;     while (!isdigit(ch) && (ch !=  - ) && (ch != EOF)) ch = getchar();     if (ch ==  - ) {       f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = (x << 1) + (x << 3) + ch -  0 ;       ch = getchar();     }     return x * f;   }   struct Flower {     long long x, y, da, db;     bool operator<(const Flower& w) const { return da < w.da; }   } a, b, c[2005];   long long Dist(Flower a, Flower b) {     return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);   }   int n;   long long mxb[2005];   int main() {     n = IN();     a.x = 1ll * IN(), a.y = 1ll * IN();     b.x = 1ll * IN(), b.y = 1ll * IN();     for (int i = 1; i <= n; i++) {       c[i].x = 1ll * IN(), c[i].y = 1ll * IN();       c[i].da = Dist(c[i], a);       c[i].db = Dist(c[i], b);     }     sort(c + 1, c + n + 1);     mxb[n + 1] = 0;     for (int i = n; i >= 1; i--) mxb[i] = max(mxb[i + 1], c[i].db);     long long Ans = mxb[1], ND = 0;     for (int i = 2; i <= n + 1; i++) Ans = min(Ans, c[i - 1].da + mxb[i]);     printf( %I64d n , Ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int i = 0; i < t; ++i) {       int n;       cin >> n;       string s;       cin >> s;       int sum(0), pos(-1), pos1(-1), ot(-1), num(0);       for (int i = 0; i < s.length(); ++i) sum += (s[i] -  0 );       for (int i = 0; i < s.length(); ++i) {         if ((s[i] -  0 ) % 2) {           pos = i;           num = s[i] -  0 ;         }       }       for (int i = 0; i < s.length(); ++i) {         if ((s[i] -  0 ) % 2 == 0)           pos1 = i;         else if ((s[i] -  0 ) % 2 && i != pos)           ot = s[i] -  0 ;       }       if (n == 1 || pos == -1)         cout << -1 <<   n ;       else if (pos1 == -1) {         if (n % 2 == 0)           cout << s <<   n ;         else {           s.erase(s.begin() + 0);           cout << s <<   n ;         }       } else if (ot != -1)         cout << ot * 10 + num <<   n ;       else if (ot == -1)         cout << -1 <<   n ;     }   }


#include <bits/stdc++.h>   double a, b, c, d, x, y;   int main() {     scanf( %lf%lf%lf%lf , &a, &b, &c, &d);     x = a / b;     y = c / d;     printf( %lf , x / (1.0 - (1.0 - x) * (1.0 - y)));   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const ll OO = 1e18;   int di[8] = {0, 0, 1, -1, -1, 1, -1, 1};   int dj[8] = {1, -1, 0, 0, 1, 1, -1, -1};   string ys =  YES , no =  NO ;   void fast() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   }   string s1, s2;   int n, m, idx_shift;   const int N = 1001;   int mem[N][N];   int dp(int i, int j) {     if (i == s1.size()) return s2.size() - j;     if (j == s2.size()) return s1.size() - i;     int &rt = mem[i][j];     if (~rt) return rt;     if (s1[i] == s2[j]) return rt = dp(i + 1, j + 1);     int choice1 = 1 + dp(i, j + 1);     int choice2 = 1 + dp(i + 1, j);     int choice3 = 1 + dp(i + 1, j + 1);     return rt = min({choice1, choice2, choice3});   }   void build(int i, int j) {     if (i == s1.size()) {       int to_add = s2.size() - j;       for (int k = 0; k < to_add; k++)         cout <<  INSERT   << i + k + 1 + idx_shift <<     << s2[k + j] <<   n ;       idx_shift += to_add;       return;     }     if (j == s2.size()) {       int to_delete = s1.size() - i;       for (int k = 0; k < to_delete; k++) {         cout <<  DELETE   << i + k + 1 + idx_shift <<   n ;         --idx_shift;       }       return;     }     if (s1[i] == s2[j]) {       build(i + 1, j + 1);       return;     }     int choice1 = 1 + dp(i, j + 1);     int choice2 = 1 + dp(i + 1, j);     int choice3 = 1 + dp(i + 1, j + 1);     int optimal = dp(i, j);     if (choice1 == optimal) {       cout <<  INSERT   << i + 1 + idx_shift <<     << s2[j] <<   n ;       ++idx_shift;       build(i, j + 1);     } else if (choice2 == optimal) {       cout <<  DELETE   << i + 1 + idx_shift <<   n ;       --idx_shift;       build(i + 1, j);     } else {       cout <<  REPLACE   << i + 1 + idx_shift <<     << s2[j] <<   n ;       build(i + 1, j + 1);     }   }   int main() {     fast();     cin >> s1 >> s2;     memset(mem, -1, sizeof mem);     cout << dp(0, 0) <<   n ;     idx_shift = 0;     build(0, 0);   }


#include <bits/stdc++.h>   using namespace std;   void solutionA();   int main() {     ios_base::sync_with_stdio(false);     solutionA();     return EXIT_SUCCESS;   }   long long n, a, b, u[1024];   double d[1024];   struct point {     double x, y;     point(double x = 0, double y = 0) : x(x), y(y) {}   } s, f;   struct segment {     point p1, p2;   } t[1024];   point operator+(const point& p1, const point& p2) {     return point(p1.x + p2.x, p1.y + p2.y);   }   point operator-(const point& p1, const point& p2) {     return point(p1.x - p2.x, p1.y - p2.y);   }   istream& operator>>(istream& in, point& p) {     in >> p.x >> p.y;     return in;   }   ostream& operator<<(ostream& out, point& p) {     out <<  (  << p.x <<  ,   << p.y <<  ) ;     return out;   }   double dot(const point& p1, const point& p2) {     return p1.x * p2.x + p1.y * p2.y;   }   double cross(const point& p1, const point& p2) {     return p1.x * p2.y - p1.y * p2.x;   }   double dist(const point& p1, const point& p2) {     double dx = p1.x - p2.x, dy = p1.y - p2.y;     return sqrt(dx * dx + dy * dy);   }   double dist(const point& p, const segment& s) {     double res = min(dist(p, s.p1), dist(p, s.p2));     double cos1 = dot(p - s.p1, s.p2 - s.p1);     double cos2 = dot(p - s.p2, s.p1 - s.p2);     if (fabs(cos1) > 1e-9 && cos1 < 0) return res;     if (fabs(cos2) > 1e-9 && cos2 < 0) return res;     double h = abs(cross(p - s.p1, s.p2 - s.p1)) / dist(s.p1, s.p2);     res = min(res, h);     return res;   }   double dist(const segment& s1, const segment& s2) {     double res = min(dist(s1.p1, s2), dist(s1.p2, s2));     res = min(res, min(dist(s2.p1, s1), dist(s2.p2, s1)));     return res;   }   void solve() {     memset(u, 0, sizeof(long long) * 1024);     for (long long i = 0; i < 1024; ++i) d[i] = 1e12;     d[0] = 0;     for (;;) {       long long v = -1;       for (long long i = 0; i < n + 2; ++i) {         if (u[i]) continue;         if (fabs(d[i] - 1e12) < 1e-9) continue;         if (v == -1 || d[v] > d[i]) v = i;       }       if (v == -1 || v == 1) break;       u[v] = 1;       if (v == 0) {         double l = dist(s, f);         if (fabs(l - a) < 1e-9 || l < a) {           d[1] = l;         }         for (long long i = 0; i < n; ++i) {           l = dist(s, t[i]);           if (fabs(l - a) > 1e-9 && l > a) continue;           if (fabs(a - d[i + 2]) > 1e-9 && a < d[i + 2]) {             d[i + 2] = a;           }         }       } else {         double l = dist(f, t[v - 2]);         if (fabs(l - a) < 1e-9 || l < a) {           double l2 = d[v] + b + l;           if (fabs(l2 - d[1]) > 1e-9 && l2 < d[1]) {             d[1] = l2;           }         }         for (long long i = 0; i < n; ++i) {           if (i + 2 == v) continue;           l = dist(t[v - 2], t[i]);           if (fabs(l - a) > 1e-9 && l > a) continue;           double l2 = d[v] + a + b;           if (fabs(l2 - d[i + 2]) > 1e-9 && l2 < d[i + 2]) {             d[i + 2] = l2;           }         }       }     }     if (fabs(d[1] - 1e12) < 1e-9)       cout << -1 << endl;     else       cout << fixed << d[1] << endl;   }   void solutionA() {     cout.precision(6);     for (; cin >> a >> b;) {       cin >> s >> f >> n;       for (long long i = 0; i < n; ++i) cin >> t[i].p1 >> t[i].p2;       solve();       cerr << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long sol(long long n, long long t) {     long long ret = n * (n + 1) * (2 * n + 3 * t + 1);     ret /= 6;     return ret;   }   int main() {     ios_base::sync_with_stdio(0);     long long x;     cin >> x;     long long ans = 0;     vector<pair<long long, long long> > aa;     for (long long n = 1;; n++) {       long long z = sol(n, 0);       if (z > x) {         break;       }       long long r = 6 * x - 2 * n * n * n - 3 * n * n - n;       long long d = 3 * n * n + 3 * n;       if (r % d == 0) {         ans++;         aa.push_back(make_pair(n, n + r / d));       }     }     long long xx = aa.size();     for (int i = 0; i < xx; i++) {       if (aa[i].first != aa[i].second)         aa.push_back(make_pair(aa[i].second, aa[i].first));     }     cout << aa.size() <<   n ;     sort(aa.begin(), aa.end());     for (int i = 0; i < aa.size(); i++) {       cout << aa[i].first <<     << aa[i].second <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class c>   struct rge {     c b, e;   };   template <class c>   rge<c> range(c i, c j) {     return rge<c>{i, j};   }   template <class c>   auto dud(c* x) -> decltype(cerr << *x, 0);   template <class c>   char dud(...);   struct debug {     template <class c>     debug& operator<<(const c&) {       return *this;     }   };   using ll = long long;   void solve() {     string s;     int a;     cin >> a >> s;     int sum = 0;     vector<int> v;     v.push_back(0);     for (int i = s.size() - 1; i >= 0; i--) {       if (s[i] ==  P )         sum++;       else {         v.push_back(sum);         sum = 0;       }     }     cout << *max_element(v.begin(), v.end()) << endl;   }   int main() {     int t;     cin >> t;     while (t--) solve();   }


#include <bits/stdc++.h>   using namespace std;   vector<int> v[1000005];   int ok[2][1000005];   int main() {     int q;     cin >> q;     while (q--) {       int n;       cin >> n;       for (int i = 0; i < n; i++) ok[0][i] = 0;       set<int> s;       for (int i = 0; i < n; i++) s.insert(i);       int cnt = 0;       for (int i = 0; i < n; i++) {         int x;         cin >> x;         v[i].clear();         for (int j = 0; j < x; j++) {           int a;           cin >> a;           v[i].push_back(a - 1);         }         for (int j = 0; j < v[i].size(); j++) {           if (s.count(v[i][j])) {             ok[0][i] = ok[1][v[i][j]] = 1;             s.erase(v[i][j]);             cnt++;             break;           }         }       }       if (cnt == n)         cout <<  OPTIMAL  << endl;       else {         cout <<  IMPROVE  << endl;         for (int i = n - 1; i >= 0; i--) {           if (ok[0][i] == 0) {             cout << (i + 1) <<     << (*s.rbegin() + 1) << endl;             ;             break;           }         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, a[55], b[55], c, mx;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     cin >> m;     for (int j = 1; j <= m; j++) {       cin >> b[j];     }     mx = 0;     c = 1;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (b[j] / a[i] >= mx && b[j] % a[i] == 0) {           if (b[j] / a[i] == mx) {             c++;           }           if (b[j] / a[i] > mx) {             c = 1;           }           mx = b[j] / a[i];         }       }     }     cout << c << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC target( avx2 )   #pragma GCC optimization( O3 )   #pragma GCC optimization( unroll-loops )   int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};   int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};   const double PI = acos(-1), EPS = 1e-7;   const int OO = 0x3f3f3f3f, N = 1e7 + 5, mod = 1e9 + 7;   using namespace std;   long long gcd(long long x, long long y) { return (!y) ? x : gcd(y, x % y); }   long long lcm(long long x, long long y) { return ((x / gcd(x, y)) * y); }   void file() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   vector<long long> v;   int main() {     std::ios_base::sync_with_stdio(0);     cin.tie(NULL);     file();     long long n, d;     long long x, y;     cin >> n >> d;     while (n--) {       cin >> x >> y;       if (x == d || y == d || x == 7 - d || y == 7 - d) {         cout <<  NO  <<   n ;         return 0;       }     }     cout <<  YES  <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   string itos(long long i) {     string s =   ;     while (i) {       s += char(i % 10 +  0 );       i /= 10;     }     reverse(s.begin(), s.end());     return s;   }   long long stoi(string &s) {     long long tot = 0;     for (int i = (int)s.length() - 1, j = 1; i >= 0; i--, j *= 10) {       tot += j * (s[i] +  0 );     }     return tot;   }   bool cmp(const pair<int, int> &a, const pair<int, int> &b) {     return (a.first > b.first);   }   long long pwr(long long base, long long p, long long mod = 1000000007) {     long long ans = 1;     while (p) {       if (p & 1) ans = (ans * base) % 1000000007;       base = (base * base) % 1000000007;       p /= 2;     }     return ans;   }   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long lcm(long long a, long long b) { return (a * b * 1LL) / gcd(a, b); }   const int N = 1e6 + 5;   int n, m, k, cnt = 0;   long long dist[N];   vector<pair<pair<int, long long>, int> > adj[N];   priority_queue<pair<pair<long long, int>, int>,                  vector<pair<pair<long long, int>, int> >,                  greater<pair<pair<long long, int>, int> > >       q;   void dijkstra(int src) {     for (int i = 1; i < N; i++) dist[i] = 1e18;     q.push(make_pair(make_pair(0, 0), src));     while (!q.empty()) {       pair<pair<long long, int>, int> p = q.top();       q.pop();       int u = p.second;       long long d = p.first.first;       int t = p.first.second;       if (dist[u] != 1e18) continue;       dist[u] = d;       if (t == 1) cnt++;       for (int i = 0; i < adj[u].size(); i++) {         int v = adj[u][i].first.first;         long long w = adj[u][i].first.second;         int t = adj[u][i].second;         if (dist[v] > w + d) {           q.push(make_pair(make_pair(w + d, t), v));         }       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cout << fixed << setprecision(10);     clock_t clk;     clk = clock();     cin >> n >> m >> k;     for (int i = (1); i <= (m); ++i) {       int x, y, w;       cin >> x >> y >> w;       adj[x].push_back(make_pair(make_pair(y, w), 0));       adj[y].push_back(make_pair(make_pair(x, w), 0));     }     for (int i = (1); i <= (k); ++i) {       int x, w;       cin >> x >> w;       adj[1].push_back(make_pair(make_pair(x, w), 1));       adj[x].push_back(make_pair(make_pair(1, w), 1));     }     dijkstra(1);     cnt = k - cnt;     cout << cnt;     clk = clock() - clk;     cerr << fixed << setprecision(6) <<  Time:   << ((double)clk) / CLOCKS_PER_SEC          <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   set<int> se;   struct zzz {     int v, id;   } zz[1000005];   bool cmp(const zzz &i, const zzz &j) { return i.v > j.v; }   int N, M, K;   char S[1000005], T[1000005], s[1000005];   int z[1000005], p[2][1000005];   void w(int td) {     for (int i = 0; i < M; i++) s[i] = T[i];     s[M] =    ;     for (int i = 0; i < N; i++) s[M + 1 + i] = S[i];     int L = 0, R = 0, n = N + M + 1;     for (int i = 1; i < n; i++) {       if (i > R) {         L = R = i;         while (R < n && s[R - L] == s[R]) R++;         z[i] = R - L;         R--;       } else {         int k = i - L;         if (z[k] < R - i + 1)           z[i] = z[k];         else {           L = i;           while (R < n && s[R - L] == s[R]) R++;           z[i] = R - L;           R--;         }       }     }     for (int i = 0; i < N; i++) zz[i].v = z[M + 1 + i], zz[i].id = i;     sort(zz, zz + N, cmp);     int i = -1;     se.clear();     for (int x = M; x >= 0; x--) {       while (i + 1 < N && zz[i + 1].v >= x) {         i++;         se.insert(zz[i].id);       }       if (se.lower_bound(K - x) == se.end())         p[td][x] = 1000000000;       else         p[td][x] = *se.lower_bound(K - x);     }   }   int main() {     scanf( %d%d%d , &N, &M, &K);     scanf( %s%s , S, T);     w(0);     for (int i = 1; i <= N; i++)       if (z[M + i] == M) {         puts( Yes );         if (i + K + K - 1 <= N)           printf( %d %d , i, i + K);         else           printf( %d %d , N - K - K + 1, N - K + 1);         return 0;       }     reverse(S, S + N);     reverse(T, T + M);     w(1);     for (int i = 1; i < M; i++)       if (i <= K && M - i <= K && p[0][i] + i <= N - p[1][M - i] - (M - i)) {         puts( Yes );         printf( %d %d , p[0][i] + i - K + 1, N - p[1][M - i] - (M - i) + 1);         return 0;       }     puts( No );   }


#include <bits/stdc++.h>   const int N = 100005;   int n, fa[N], rt, cnt = 0, sz[N], son[N] = {0}, top[N], dep[N], idx = 0, Q,                     dfn[N], head[N], hq[N], nodes = 0, year[N];   int ret, L, R;   inline int readint() {     int c = getchar(), d = 0;     for (; !isdigit(c); c = getchar())       ;     for (; isdigit(c); c = getchar()) d = (d << 3) + (d << 1) + (c ^  0 );     return d;   }   struct edge {     int to, nxt;   } e[N << 1];   struct SegmentTreeNode {     int v, ls, rs, l, r, sz;   } d[N << 5];   void BigMagician(int now) {     sz[now] = 1;     for (int i = head[now]; i; i = e[i].nxt)       if (fa[e[i].to] == now) {         dep[e[i].to] = dep[now] + 1;         BigMagician(e[i].to);         sz[now] += sz[e[i].to];         if (!son[now] || sz[e[i].to] > sz[son[now]]) son[now] = e[i].to;       }   }   void dfs(int now) {     dfn[now] = ++idx;     hq[idx] = now;     if (son[now]) top[son[now]] = top[now], dfs(son[now]);     for (int i = head[now]; i; i = e[i].nxt)       if (dep[now] < dep[e[i].to] && e[i].to != son[now])         dfs(top[e[i].to] = e[i].to);   }   void build(int l, int r, int& o) {     o = ++nodes;     d[o].l = l, d[o].r = r, d[o].sz = r - l + 1;     if (l == r)       d[o].v = 1;     else {       int mid = l + r >> 1;       build(l, mid, d[o].ls);       build(mid + 1, r, d[o].rs);       d[o].v = d[d[o].ls].v + d[d[o].rs].v;     }   }   void modify(int o, int& nw, int& p) {     d[nw = ++nodes] = d[o];     --d[nw].v;     if (d[o].l != d[o].r) {       int mid = d[o].l + d[o].r >> 1;       if (p <= mid)         modify(d[o].ls, d[nw].ls, p);       else         modify(d[o].rs, d[nw].rs, p);     }   }   void query(int& o, int& nw) {     if (L <= d[o].l && d[o].r <= R)       ret += d[o].v - d[nw].v;     else {       int mid = d[d[o].ls].r;       if (L <= mid) query(d[o].ls, d[nw].ls);       if (mid < R) query(d[o].rs, d[nw].rs);     }   }   void query2(int& o, int& nw, int k) {     if (d[o].l == d[o].r)       ret = hq[d[o].l];     else {       if (k <= d[d[o].rs].sz - d[d[o].rs].v + d[d[nw].rs].v)         query2(d[o].rs, d[nw].rs, k);       else         query2(d[o].ls, d[nw].ls,                k - (d[d[o].rs].sz - d[d[o].rs].v + d[d[nw].rs].v));     }   }   inline int theLCA(int x, int y) {     for (; top[x] != top[y];)       if (dep[top[x]] >= dep[top[y]])         x = fa[top[x]];       else         y = fa[top[y]];     return (dep[x] < dep[y]) ? x : y;   }   inline int search_to_lca(int u, int v, int& k, int& y, int& nw) {     while (top[u] != top[v]) {       ret = 0;       L = dfn[top[u]], R = dfn[u];       query(y, nw);       ret = R - L + 1 - ret;       if (ret >= k) {         ret = 0;         L = dfn[u] + 1, R = n;         if (L <= n) query(y, nw);         ret = R - L + 1 - ret;         k += ret;         ret = 0;         query2(y, nw, k);         return ret;       }       k -= ret;       u = fa[top[u]];     }     ret = 0;     L = dfn[v], R = dfn[u];     query(y, nw);     ret = R - L + 1 - ret;     if (ret >= k) {       ret = 0;       L = dfn[u] + 1, R = n;       if (L <= n) query(y, nw);       ret = R - L + 1 - ret;       k += ret;       ret = 0;       query2(y, nw, k);       return ret;     }     k -= ret;     return -1;   }   inline int search_on_a_list(int u, int v, int& y, int& nw) {     int ans = 0;     while (top[u] != top[v]) {       ret = 0;       L = dfn[top[u]], R = dfn[u];       query(y, nw);       ret = R - L + 1 - ret;       ans += ret;       u = fa[top[u]];     }     ret = 0;     L = dfn[v], R = dfn[u];     query(y, nw);     ret = R - L + 1 - ret;     return ans + ret;   }   inline int search_from_lca(int u, int v, int& k, int& y, int& nw) {     int all = search_on_a_list(u, v, y, nw);     if (all < k) return -1;     k = all - k + 1;     while (top[u] != top[v]) {       ret = 0;       L = dfn[top[u]], R = dfn[u];       query(y, nw);       ret = R - L + 1 - ret;       if (ret >= k) {         ret = 0;         L = dfn[u] + 1, R = n;         if (L <= n) query(y, nw);         ret = R - L + 1 - ret;         k += ret;         ret = 0;         query2(y, nw, k);         return ret;       }       k -= ret;       u = fa[top[u]];     }     ret = 0;     L = dfn[v], R = dfn[u];     query(y, nw);     ret = R - L + 1 - ret;     if (ret >= k) {       ret = 0;       L = dfn[u] + 1, R = n;       if (L <= n) query(y, nw);       ret = R - L + 1 - ret;       k += ret;       ret = 0;       query2(y, nw, k);       return ret;     }     k -= ret;     return -1;   }   int main() {     memset(head, 0, sizeof head);     n = readint();     for (int i = 1; i <= n; ++i) {       fa[i] = readint();       if (!fa[i])         rt = i;       else {         e[++cnt] = (edge){i, head[fa[i]]};         head[fa[i]] = cnt;         e[++cnt] = (edge){fa[i], head[i]};         head[i] = cnt;       }     }     dep[rt] = 1;     top[rt] = rt;     BigMagician(rt);     dfs(rt);     build(1, n, year[0]);     Q = readint();     for (int i = 1; i <= Q; ++i) {       int opt = readint();       if (opt == 1) {         int p = readint();         modify(year[i - 1], year[i], dfn[p]);       } else {         year[i] = year[i - 1];         int u = readint(), v = readint(), k = readint(), y = readint();         ret = 0;         L = R = dfn[u];         query(year[y], year[i]);         if (!ret) ++k;         int lca = theLCA(u, v);         int ans = search_to_lca(u, lca, k, year[y], year[i]);         if (!~ans) {           ret = 0;           L = R = dfn[lca];           query(year[y], year[i]);           if (!ret) ++k;           ans = search_from_lca(v, lca, k, year[y], year[i]);         }         if (ans == v || ans == u) ans = -1;         printf( %d n , ans);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1005;   const int INF = 0x3f3f3f3f;   int n, a[MAXN];   vector<int> ans;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       if (i != 1 && a[i] == 1) ans.push_back(a[i - 1]);     }     ans.push_back(a[n]);     printf( %d n , ans.size());     for (auto i : ans) printf( %d  , i);     return 0;   }


#include <bits/stdc++.h>   int n, i, a[10001], p = 0;   int main() {     scanf( %d , &n);     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     for (i = 1; i <= n; i++) {       if (a[i] == 1) p++;     }     if (n > 1) {       if (p == (n - 1))         printf( YES );       else         printf( NO );     } else {       if (p == 1)         printf( YES );       else         printf( NO );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   int main() {     cin >> n;     if (n % 2 == 0)       cout <<  white n             <<  1 2 n ;     else       cout <<  black n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 505;   const int dr[4] = {0, 0, -1, 1};   const int dc[4] = {-1, 1, 0, 0};   int n, K, mien, ans, res, t;   int sz[N * N], vis[N * N], c[N][N];   char a[N][N];   bool ok(int x, int y) {     return x >= 1 && x <= n && y >= 1 && y <= n && a[x][y] ==  . ;   }   void dfs(int x, int y) {     if (c[x][y]) return;     c[x][y] = mien;     sz[mien]++;     for (int i = 0; i < 4; i++)       if (ok(x + dr[i], y + dc[i])) dfs(x + dr[i], y + dc[i]);   }   void add(int x, int y) {     if (ok(x, y) && vis[c[x][y]] != t) res += sz[c[x][y]], vis[c[x][y]] = t;   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> K;     for (int i = 1; i <= n; i++) cin >> a[i] + 1;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++)         if (a[i][j] ==  .  && !c[i][j]) mien++, dfs(i, j);     for (int x = 1; x + K - 1 <= n; x++) {       for (int i = 0; i < K; i++)         for (int j = 0; j < K; j++) sz[c[x + i][j + 1]]--;       for (int y = 1; y + K - 1 <= n; y++) {         res = 0;         t++;         for (int k = 0; k < K; k++)           add(x - 1, y + k), add(x + K, y + k), add(x + k, y - 1),               add(x + k, y + K);         ans = max(ans, res);         if (y + K - 1 != n)           for (int i = 0; i < K; i++) sz[c[x + i][y]]++, sz[c[x + i][y + K]]--;         else           for (int i = 0; i < K; i++)             for (int j = 0; j < K; j++) sz[c[x + i][n - j]]++;       }     }     cout << ans + K * K;     return 0;   }


#include <bits/stdc++.h>      #define all(x) (x).begin(),(x).end()   #define sz(x) int(x.size())      using namespace std;   using ll = long long;   using pii = pair<int, int>;   template<typename... T> void rd(T&... args) {((cin>>args), ...);}   template<typename... T> void wr(T... args) {((cout<<args<<   ), ...); cout<<  n ;}      int main() {       ios::sync_with_stdio(false);       cin.tie(nullptr);       int tt;       cin>>tt;   next:       while (tt--) {           int n, u, v;           cin>>n>>u>>v;           vector<int> a(n);           for (auto& i : a) cin>>i;           int ans=2e9;           for (int i=1; i<n; i++) {               if (abs(a[i]-a[i-1])>1) {                   cout<< 0 n ;                   goto next;               }               if (abs(a[i]-a[i-1])==1) ans=min(ans, min(u, v));           }           ans=min(ans, v+min(u, v));           cout<<ans<<  n ;       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   void kaboom() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   int main() {     kaboom();     int t;     cin >> t;     while (t--) {       long long int n;       cin >> n;       cout << n << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int name[100], i, j = 0;     string codename;     cin >> codename;     for (i = 0; i < codename.length(); i++) {       name[j] = (int)codename[i];       j++;     }     sort(name, name + j);     int count = 0;     for (int k = 1; k < j; k++) {       if (name[k] != name[k - 1]) {         count++;       }     }     if (count % 2 == 0) {       cout <<  IGNORE HIM! n ;     } else {       cout <<  CHAT WITH HER! n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int a[100005];   int main() {     long long int n, b, k;     cin >> k >> b >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     long long int ans = 0;     if (b == 0) {       for (int i = 0; i < n;) {         long long int now = a[i];         long long int cnt = 0;         while (i < n && a[i] == now) {           i++;           cnt++;         }         if (now == 0) {           ans += (cnt * (cnt + 1) / 2);         }       }       cout << ans << endl;       return 0;     }     if (b == k - 1) b = 0;     long long int ans2 = 0;     if (b == 0) {       for (int i = 0; i < n;) {         long long int now = a[i];         long long int cnt = 0;         while (i < n && a[i] == now) {           i++;           cnt++;         }         if (now == 0) {           ans2 += (cnt * (cnt + 1) / 2);         }       }     }     map<long long int, long long int> mp;     mp[0]++;     for (int i = 0; i < n; i++) {       if (i > 0) a[i] += a[i - 1];       long long int mod = a[i] % (k - 1);       long long int need = (mod - b + k + k - 2) % (k - 1);       if (mp.find(need) != mp.end()) {         ans += mp[need];       }       mp[mod]++;     }     cout << ans - ans2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int solve() {     int n;     cin >> n;     int e = 0, o = 0;     int x;     for (int i = 0; i < n; i++) {       cin >> x;       if (i % 2 == 0 && x % 2 == 1) o++;       if (i % 2 == 1 && x % 2 == 0) e++;     }     if (o == e) return o;     return -1;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     for (int i = 0; i < t; i++) {       cout << solve() <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using ll = long long;   using namespace std;   using pii = pair<ll, ll>;   ll mod = 998244353;   int get_p2(int n) {     int i = 0;     while ((1 << i) <= n) i++;     return i - 1;   }   int main() {     ios_base::sync_with_stdio(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       string a, b;       cin >> a >> b;       int need0 = 0, need1 = 0, nneed0 = 0, nneed1 = 0;       for (int i = 0; i < n; i++) {         if (a[i] != b[i]) {           if (a[i] ==  0 )             need0++;           else             need1++;         } else {           if (a[i] ==  0 )             nneed0++;           else             nneed1++;         }       }       int ans = -1;       if (nneed1 - nneed0 == 1) ans = nneed1 + nneed0;       if (need0 == need1)         ans = (ans < 0 ? need0 + need1 : min(ans, need0 + need1));       cout << ans <<   n ;     }     cout.flush();   }


#include <bits/stdc++.h>   using namespace std;   const string sf[6] = { lios ,  liala ,  etr ,  etra ,  initis ,  inites };   char a[1100000], s[1100000];   int n, sd, p, num[3];   int gender;   int suffix(int x) {     int len = strlen(s);     if (len < (int)sf[x].length()) return 0;     for (int i = len - sf[x].length(); i < len; i++)       if (s[i] != sf[x][i - len + sf[x].length()]) return 0;     return 1;   }   int kind() {     for (int k = 0; k < 6; k++)       if (suffix(k)) {         if (gender != -1) {           if (gender != (k & 1)) return -1;         } else           gender = (k & 1);         return k >> 1;       }     return -1;   }   void work() {     memset(num, 0, sizeof(num));     n = strlen(a);     s[0] = 0;     sd = p = 0;     gender = -1;     while (scanf( %s , s) != EOF) {       if (!strlen(s)) continue;       if (sd) {         printf( NO n );         return;       }       int k = kind();       if (k < 0) {         printf( NO n );         return;       }       if (k == 0) {         if (num[k + 1] || num[k + 2]) {           printf( NO n );           return;         } else           num[k]++;       }       if (k == 1) {         if (num[k] || num[k + 1]) {           printf( NO n );           return;         } else           num[k]++;       }       if (k == 2) {         if (!num[k - 1]) {           if (!num[k - 2] && !num[k]) {             sd = 1;             num[k]++;           } else {             printf( NO n );             return;           }         } else           num[k]++;       }     }     if (num[0] + num[1] + num[2] > 1 && num[1] == 0) {       printf( NO n );       return;     }     printf( YES n );   }   int main() {     work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int q, n, i, a[201], start, tmp, yn;     cin >> q;     while (q--) {       cin >> n;       for (i = 0; i < n; i++) {         cin >> a[i];         if (a[i] == 1) start = i;       }       yn = 0;       tmp = 1;       for (i = 0; i < n - 1; i++)         if (a[(start + i + 1) % n] != a[(start + i) % n] + 1) tmp = 0;       yn += tmp;       tmp = 1;       for (i = 0; i < n - 1; i++)         if (a[(1 + start + i + 1) % n] != a[(1 + start + i) % n] - 1) tmp = 0;       yn += tmp;       cout << (yn == 0 ?  NO n  :  YES n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   ifstream Cin( input.txt );   ofstream Cout( output.txt );   pair<bool, long long> Try(long long A, long long B, long long N) {     long long err = 0, C;     while (A > 0 && B > 0 && N > 0) {       if (A == 1 && B != 1) {         C = B - 1;         A = B = 1;       } else if (A >= B) {         C = A / B;         A %= B;       } else {         C = B / A;         B %= A;       }       err += C - 1;       N -= C;     }     return make_pair(N == 0 && A == 0 && B == 1, err);   }   string get(long long A, long long B, long long N) {     string TMP;     while (N) {       N--;       if (A >= B) {         A -= B;         TMP.push_back( T );       } else {         B -= A;         TMP.push_back( B );       }     }     reverse(TMP.begin(), TMP.end());     return TMP;   }   int main() {     long long n, r, i, A, B, ER = (long long)1e15;     pair<bool, long long> Cur;     cin >> n >> r;     for (i = 1; i <= r; i++) {       Cur = Try(i, r, n);       if (Cur.first) {         if (Cur.second < ER) {           ER = Cur.second;           A = i;           B = r;         }       }       Cur = Try(r, i, n);       if (Cur.first) {         if (Cur.second < ER) {           ER = Cur.second;           A = r;           B = i;         }       }     }     if (ER == (long long)1e15)       cout <<  IMPOSSIBLE ;     else       cout << ER << endl << get(A, B, n);   }


#include <bits/stdc++.h>   using namespace std;   int l;   char s[205];   int exe(int a, int b) {     int u = 0, r = 0;     for (int i = a; i < b; i++) {       switch (s[i]) {         case  U :           u++;           break;         case  D :           u--;           break;         case  R :           r++;           break;         case  L :           r--;           break;       }     }     return u == 0 and r == 0;   }   int main() {     scanf( %d n , &l);     gets(s);     int r = 0;     for (int i = 0; i < l; i++)       for (int j = i + 1; j <= l; j++) r += exe(i, j);     printf( %d , r);   }


#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   void solve() {     long long n;     cin >> n;     vector<long long> v(n);     for (long long i = 0; i < n; ++i) {       cin >> v[i];     }     long long ans = 0;     map<long long, long long> mp;     for (long long i = 0; i < n; i++) {       map<long long, long long> tp;       long long cur = 0;       for (long long j = i + 1; j < n; j++) {         if (v[j] == v[i]) ans += cur;         tp[v[j]]++;         if (mp.count(v[j])) cur += mp[v[j]];       }       mp[v[i]]++;     }     cout << ans <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     long long t = 1;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 201;   int n, a[N], cum[N][N], k, freq[N];   vector<int> hs;   int calc(int l, int r) {     priority_queue<int> qu;     int res = 0;     for (int i = l; i <= r; ++i) {       qu.push(-hs[a[i]]);       res += hs[a[i]];     }     int sz = hs.size(), op = k;     bool E = false;     for (int i = sz - 1; i >= 0 && !E; --i) {       int can = min(freq[i] - (cum[r + 1][i] - cum[l][i]), op);       while (!qu.empty() && can > 0) {         int v = -qu.top();         qu.pop();         if (v > hs[i]) {           E = true;           break;         }         res -= v;         res += hs[i];         --op;         --can;       }     }     return res;   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);       hs.push_back(a[i]);     }     sort(hs.begin(), hs.end());     hs.resize(unique(hs.begin(), hs.end()) - hs.begin());     for (int i = 0; i < n; ++i) {       a[i] = lower_bound(hs.begin(), hs.end(), a[i]) - hs.begin();       for (size_t j = 0; j < hs.size(); ++j) cum[i + 1][j] = cum[i][j];       ++cum[i + 1][a[i]];       ++freq[a[i]];     }     int best = -2e9;     for (int i = 0; i < n; ++i)       for (int j = i; j < n; ++j) best = max(best, calc(i, j));     printf( %d n , best);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 5;   int n, x[2], need[N][2];   pair<int, int> p[N];   int main() {     ios::sync_with_stdio(false);     cin >> n >> x[0] >> x[1];     for (int i = 0; i < n; i++) {       int c;       cin >> c;       p[i] = {c, i};     }     sort(p, p + n);     for (int i = 0; i < n; i++)       for (int j = 0; j < 2; j++) {         int c = p[i].first;         need[i][j] = (x[j] + c - 1) / c;       }     for (int i = n - 1; i >= 0; i--) {       if (i + need[i][0] < n && need[i][0] + need[i + need[i][0]][1] <= n - i) {         vector<int> a, b;         for (int j = i; j < i + need[i][0]; j++) a.push_back(p[j].second);         for (int j = i + need[i][0]; j < i + need[i][0] + need[i + need[i][0]][1];              j++)           b.push_back(p[j].second);         cout <<  Yes n ;         cout << a.size() <<     << b.size() <<   n ;         for (int x : a) cout << x + 1 <<    ;         cout <<   n ;         for (int x : b) cout << x + 1 <<    ;         cout <<   n ;         return 0;       }       if (i + need[i][1] < n && need[i][1] + need[i + need[i][1]][0] <= n - i) {         vector<int> a, b;         for (int j = i; j < i + need[i][1]; j++) b.push_back(p[j].second);         for (int j = i + need[i][1]; j < i + need[i][1] + need[i + need[i][1]][0];              j++)           a.push_back(p[j].second);         cout <<  Yes n ;         cout << a.size() <<     << b.size() <<   n ;         for (int x : a) cout << x + 1 <<    ;         cout <<   n ;         for (int x : b) cout << x + 1 <<    ;         cout <<   n ;         return 0;       }     }     cout <<  No n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double Eps = 1e-9;   int Mod = 1000000000 + 7;   int n, m, q, k;   int Arr[100010];   bool prime[10010];   bool ok = true;   void sieve() {     for (int i = 2; i * i <= 1e4; i++) {       if (prime[i] == true)         for (int j = i * 2; j <= 1e4; j++) {           prime[j] = false;         }     }   }   int dp[100010];   int cnt;   int main() {     ios::sync_with_stdio(0);     ;     memset(prime, true, sizeof(prime));     sieve();     cin >> n;     for (int i = 0; i < n; i++) {       cnt = 0;       cin >> Arr[i];       if (Arr[i] & 1 && Arr[i] >= 9) {         Arr[i] -= 9;         cnt++;       }       if (Arr[i] % 4 == 0 && Arr[i] >= 0) {         cout << cnt + Arr[i] / 4 << endl;         continue;       } else if ((Arr[i] - 6) % 4 == 0 && (Arr[i] - 6) >= 0) {         cout << cnt + 1 + (Arr[i] - 6) / 4 << endl;         continue;       }       cout << -1 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int N;   vector<int> fen, v;   void _update(int i) {     while (i <= N) {       fen[i]++;       i += i & -i;     }   }   int _sum(int i) {     int ret = 0;     while (i > 0) {       ret += fen[i];       i -= i & -i;     }     return ret;   }   int _bin(int i) {     int sum = _sum(i);     int imin = 1, imax = i;     while (imin <= imax) {       int imid = (imin + imax) / 2;       if (sum - _sum(imid - 1) >= v[i])         imin = imid + 1;       else         imax = imid - 1;     }     return imax;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int Q;     cin >> N >> Q;     v = vector<int>(N + 1);     for (int n = 1; n <= N; ++n) {       cin >> v[n];     }     for (int n = 1; n <= N; ++n) {       v[n] = n - v[n];     }     vector<int> answer(Q);     vector<vector<pair<int, int>>> _query(N + 1);     for (int q = 0; q < Q; ++q) {       int x, y;       cin >> x >> y;       _query[N - y].push_back({x + 1, q});     }     fen = vector<int>(N + 1);     for (int r = 1; r <= N; ++r) {       if (v[r] == 0) {         _update(r);       } else if (v[r] > 0) {         int tmp = _bin(r);         if (tmp) _update(tmp);       }       for (auto x : _query[r]) {         answer[x.second] = _sum(r) - _sum(x.first - 1);       }     }     for (auto x : answer) cout << x <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int read() {     char c = getchar();     int t = 0, f = 1;     while (c >  9  || c <  0 ) f = (c ==  - ) ? -1 : 1, c = getchar();     while (c >=  0  && c <=  9 ) t = t * 10 + c -  0 , c = getchar();     return t * f;   }   struct node {     int L, R, po, id;     bool operator<(const node &b) const {       if (po == b.po) return R < b.R;       return po < b.po;     }   } p[200005];   int n, Q, K, sq, a[200005], op[200005], id[200005], uid[200005], did[200005],       cnt[200005];   long long sum[200005], st[200005], ans[200005], now;   int main() {     n = read(), K = read(), sq = sqrt(n);     for (int i = 1; i <= n; i++) op[i] = (read() == 1) ? 1 : -1;     for (int i = 1; i <= n; i++)       a[i] = read(), st[i] = sum[i] = sum[i - 1] + a[i] * op[i];     st[n + 1] = 0, sort(st + 1, st + n + 2);     int tp = unique(st + 1, st + n + 2) - st - 1;     for (int i = 0; i <= n; i++) {       id[i] = lower_bound(st + 1, st + tp + 1, sum[i]) - st;       int up = lower_bound(st + 1, st + tp + 1, sum[i] + K) - st;       if (st[up] == sum[i] + K) uid[i] = up;       int down = lower_bound(st + 1, st + tp + 1, sum[i] - K) - st;       if (st[down] == sum[i] - K) did[i] = down;     }     Q = read();     for (int i = 1, x, y, z; i <= Q; i++)       x = read() - 1, y = read(), z = x / sq, p[i] = (node){x, y, z, i};     sort(p + 1, p + Q + 1);     for (int i = p[1].L; i <= p[1].R; i++) now += cnt[did[i]], cnt[id[i]]++;     ans[p[1].id] = now;     for (int i = 2; i <= n; i++) {       int L = p[i - 1].L, R = p[i - 1].R;       while (R < p[i].R) R++, now += cnt[did[R]], cnt[id[R]]++;       while (R > p[i].R) cnt[id[R]]--, now -= cnt[did[R]], R--;       while (L > p[i].L) L--, now += cnt[uid[L]], cnt[id[L]]++;       while (L < p[i].L) cnt[id[L]]--, now -= cnt[uid[L]], L++;       ans[p[i].id] = now;     }     for (int i = 1; i <= Q; i++) printf( %lld n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   long long int pow_mod(long long int a, long long int b) {     long long int res = 1;     while (b != 0) {       if (b & 1) {         res = (res * a) % 1000000007;       }       a = (a * a) % 1000000007;       b /= 2;     }     return res;   }   void solve() {     long long int h;     cin >> h;     long long int a[h + 1];     for (long long int i = 0; i < h + 1; i++) cin >> a[i];     bool f = true;     for (long long int i = 1; i < h + 1; i++) {       if (a[i] > 1 && a[i - 1] > 1) {         f = false;         break;       }     }     if (f)       cout <<  perfect n ;     else {       cout <<  ambiguous n ;       long long int p1[h + 1];       long long int p2[h + 1];       long long int sz = 0;       bool f = true;       long long int capture = -1;       for (long long int i = 0; i < h + 1; i++) {         for (long long int j = 0; j < a[i]; j++) {           p1[i] = sz;           cout << sz <<    ;         }         if (i > 0 && a[i] > 1 && a[i - 1] > 1) capture = i;         sz += a[i];       }       cout << endl;       for (long long int i = 0; i < h + 1; i++) {         if (i == capture) cout << (p1[i] - 1) <<    ;         for (long long int j = 0; j < a[i]; j++) {           if (j == 0 && capture == i) continue;           cout << p1[i] <<    ;         }       }       cout << endl;     }   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t;     t = 1;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int y, a, b, c, d, temp;     while (cin >> y) {       temp = y + 1;       while (temp != 0) {         a = temp % 10;         b = (temp % 100) / 10;         c = (temp % 1000) / 100;         d = temp / 1000;         if (a == b || b == c || c == d || d == a || d == b || a == c)           temp++;         else           break;       }       cout << temp << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using ll = long long int;   using namespace std;   template <class c>   struct rge {     c b, e;   };   template <class c>   rge<c> range(c i, c j) {     return rge<c>{i, j};   }   template <class c>   auto dud(c *x) -> decltype(cerr << *x, 0);   template <class c>   char dud(...);   struct debug {     template <class c>     debug &operator<<(const c &) {       return *this;     }   };   template <typename T>   void ara_read(T &v, ll n) {     ll temp;     for (ll i = 0; i < n; i++) {       scanf( %lld , &temp);       v.emplace_back(temp);     }   }   template <typename T>   void ara_print(T &v) {     for (ll x : v) printf( %lld  , x);     puts(  );   }   const int INF = 1e9 + 99;   using Pair = pair<int, int>;   using vec = vector<ll>;   void bruteforce(int l, int r, vec &v) {     return;     set<ll> myset;     for (ll x : v) {       for (ll i = l; i <= r; i++) myset.insert(i + x);     }     printf( BruteForce : %d n , (int)myset.size());   }   void improved(vec &v, vec &pref, int n) {     ll l, r;     scanf( %lld%lld , &l, &r);     ll w = r - l + 1;     ll ind = lower_bound(v.begin(), --v.end(), w) - v.begin();     ll ans = (n - ind) * w + pref[ind];     printf( %lld  , ans);   }   int main() {     int n;     scanf( %d , &n);     vec v(n);     for (ll &x : v) scanf( %lld , &x);     sort(v.begin(), v.end());     for (int i = 0; i < n - 1; i++) v[i] = v[i + 1] - v[i];     sort(v.begin(), --v.end());     int q;     scanf( %d , &q);     vec pref(n);     for (int i = 1; i < n; i++) pref[i] = pref[i - 1] + v[i - 1];     for (int i = 0; i < q; i++) improved(v, pref, n);     puts(  );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = (int)1e9 + 7;   const double EPS = 1e-9;   const int INF = INT_MAX;   const long long INFLL = (long long)1e18;   const double PI = acos(-1.0);   template <class T>   T gcd(T a, T b) {     return b ? gcd(b, a % b) : a;   }   template <class T>   T lcm(T a, T b) {     return a / gcd(a, b) * b;   }   const int N = (int)1e5 + 5;   const int P = 1 << 18;   int n, m;   int a[N];   pair<int, int> g[N];   int use[N];   int main() {     ios::sync_with_stdio(false);     cout.setf(ios::fixed | ios::showpoint);     cout.precision(8);     cin >> n >> m;     if (n == 1) return !printf( NO n );     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       a[x]++;       a[y]++;       g[i] = make_pair(x, y);     }     int flag = 0;     for (int i = 1; i <= n; i++) {       if (a[i] < n - 1) {         flag = 1;         for (int j = 0; j < m; j++) {           if (g[j].first == i) {             use[g[j].second] = 1;           }           if (g[j].second == i) {             use[g[j].first] = 1;           }         }         int cur;         for (int j = 1; j <= n; j++) {           if (!use[j] && j != i) {             cur = j;             break;           }         }         cout <<  YES n ;         int st = 3;         for (int j = 1; j <= n; j++) {           if (j == i) {             cout <<  1  ;             continue;           }           if (j == cur) {             cout <<  2  ;             continue;           }           cout << st <<    ;           st++;         }         cout <<   n ;         st = 3;         for (int j = 1; j <= n; j++) {           if (j == i || j == cur) {             cout <<  1  ;             continue;           }           cout << st <<    ;           st++;         }         return 0;       }     }     if (!flag) return !printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> mp[105];   int vis[105];   struct node {     int fa;     int kid;     node(int fa, int b) {       this->fa = fa;       kid = b;     }   };   bool bfs(int rt, int n) {     queue<node> q;     q.push(node(-1, rt));     vis[rt] = 1;     while (!q.empty()) {       int now = q.front().kid;       int fa = q.front().fa;       q.pop();       for (int i = 0; i < mp[now].size(); i++) {         int v = mp[now][i];         if (vis[v] && v != fa) {           vis[v]++;           continue;         }         if (vis[v] || v == fa) continue;         vis[v] = 1;         q.push(node(now, v));       }     }     int f = 0;     for (int i = 1; i <= n; i++) {       if (vis[i] > 2) return false;       if (vis[i] == 2) f = 1;     }     if (f) return true;     return false;   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       int u, v;       scanf( %d%d , &u, &v);       mp[u].push_back(v);       mp[v].push_back(u);     }     int yes = 0;     int no = 0;     for (int i = 1; i <= n; i++) {       if (vis[i] == 0) {         if (bfs(i, n)) {           yes++;         } else {           no = 1;         }       }     }     if (yes == 1 && no == 0)       puts( FHTAGN! );     else       puts( NO );   }


#include <bits/stdc++.h>   int main() {     int a;     while (scanf( %d , &a) != EOF) printf( %d %d %d n , 0, 0, a);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int maxrob(int p, int f, int cnt_s, int cnt_w, int s, int w) {     if (p > f) {       swap(p, f);     }     if (s > w) {       swap(cnt_s, cnt_w);       swap(s, w);     }     long long s_total = (long long)cnt_s * s;     if ((p + f) <= s_total) {       return (p / s) + (f / s);     }     int w_cnt_max = min(((p + f) - s_total) / w, (long long)cnt_w);     int answer = 0;     for (int k = 0; k <= w_cnt_max; ++k) {       if ((long long)w * k > p) break;       int res = 0;       int s_cnt_p = min((p - w * k) / s, cnt_s);       int s_cnt_f = cnt_s - s_cnt_p;       res += s_cnt_p;       if ((long long)s * s_cnt_f > f) {         s_cnt_f = (f / s);       }       res += s_cnt_f;       res += min((p - s * s_cnt_p) / w + (f - s * s_cnt_f) / w, cnt_w);       if (answer < res) answer = res;     }     return answer;   }   int main() {     int T;     cin >> T;     while (T-- != 0) {       int p, f;       cin >> p >> f;       int cnt_s, cnt_w;       cin >> cnt_s >> cnt_w;       int s, w;       cin >> s >> w;       cout << maxrob(p, f, cnt_s, cnt_w, s, w) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t = 1, n, k, fact, a, b;     cin >> t;     while (t--) {       cin >> a >> b;       if (a == 1)         cout <<  0 n ;       else if (a == 2)         cout << b <<   n ;       else         cout << 2 * b <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int res[100010], k = 0;   struct edge {     int id, v, w, next;   } e[100010];   int head[100010], cnt = 0;   void adde(int u, int v, int w, int id) {     e[cnt].id = id;     e[cnt].v = v;     e[cnt].w = w;     e[cnt].next = head[u];     head[u] = cnt++;   }   int ind[100010];   queue<int> q;   int lev[100010];   int tl;   bool check(int lim) {     memset(ind, 0, sizeof ind);     memset(lev, 0, sizeof lev);     tl = 0;     while (!q.empty()) q.pop();     for (int i = 0; i < cnt; ++i)       if (e[i].w > lim) ++ind[e[i].v];     for (int i = 1; i <= n; ++i)       if (ind[i] == 0) q.push(i);     while (!q.empty()) {       int u = q.front();       q.pop();       lev[u] = tl++;       for (int i = head[u], v, w; ~i; i = e[i].next) {         v = e[i].v;         w = e[i].w;         if (w <= lim) continue;         --ind[v];         if (ind[v] == 0) {           q.push(v);         }       }     }     for (int i = 1; i <= n; ++i)       if (ind[i]) return false;     return true;   }   int main() {     memset(head, -1, sizeof head);     scanf( %d%d , &n, &m);     int L = 0, R = 0, mid, ans;     for (int i = 1, u, v, w; i <= m; ++i) {       scanf( %d%d%d , &u, &v, &w);       adde(u, v, w, i);       R = max(R, w);     }     ++R;     while (L < R) {       mid = (L + R) >> 1;       if (check(mid)) {         ans = R = mid;       } else {         L = mid + 1;       }     }     check(ans);     for (int u = 1; u <= n; ++u) {       for (int i = head[u], v, w; ~i; i = e[i].next) {         v = e[i].v;         w = e[i].w;         if (w > ans) continue;         if (lev[u] > lev[v]) res[++k] = e[i].id;       }     }     printf( %d %d n , ans, k);     for (int i = 1; i <= k; ++i) printf( %d%c , res[i], (i == k ?   n  :    ));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);     long long int t;     cin >> t;     while (t--) {       string s;       cin >> s;       if (s.size() == 1) {         cout << 0 << endl;         continue;       }       map<char, int> m;       int i;       int count = 0;       for (i = 0; i < s.size(); i++) {         m[s[i]]++;       }       for (auto it : m) {         if (it.second > 1) {           count += 2;         } else if (it.second == 1) {           count += 1;         }       }       cout << count / 2 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   uniform_int_distribution<long long> d(156, 99657);   mt19937 rnd;   int n, s, x, mn = -100, q, w;   void solve() {     scanf( %d%d%d , &n, &s, &x);     if (n >= 2000) {       for (int i = 1; i <= 1000; i++) {         printf( ? %d n , (d(rnd) * d(rnd)) % n + 1);         fflush(stdout);         scanf( %d%d , &q, &w);         if (q < x && q > mn) {           mn = q;           s = w;         }       }     }     while (1) {       printf( ? %d n , s);       fflush(stdout);       scanf( %d%d , &q, &w);       if (q >= x) {         printf( ! %d n , q);         fflush(stdout);         return;       }       if (w == -1) {         printf( ! -1 n );         fflush(stdout);         return;       }       s = w;     }   }   int main() {     rnd.seed(time(NULL));     int tt = 1;     for (int i = 1; i <= tt; i++) solve();   }


#include <bits/stdc++.h>   using namespace std;   const int inf = 1 << 30;   const int N = 1003, mod = 1000000007;   int n, q;   char a[N];   int k;   int p[N];   int calc(int x) {     if (x == 1) return 0;     if (p[x] != -1) return p[x];     int cnt = 0, ret = 0;     while (x) {       cnt += (x % 2);       x /= 2;     }     return ret = calc(cnt) + 1;   }   void pre() {     memset(p, -1, sizeof p);     for (int i = 1; i <= 1000; i++) {       p[i] = calc(i);     }   }   int mem[N][N][2];   int dp(int i, int kk, int f) {     if (i == n) {       return (k == p[kk] + 1 && kk != 0);     }     int &ret = mem[i][kk][f];     if (ret != -1) return ret;     ret = 0;     if (f) {       ret += dp(i + 1, kk + 1, f) % mod;       ret %= mod;       ret += dp(i + 1, kk, f) % mod;       ret %= mod;     } else {       if (a[i] ==  0 ) {         ret += dp(i + 1, kk, f) % mod;         ret %= mod;       } else {         ret += dp(i + 1, kk + 1, f) % mod;         ret %= mod;         ret += dp(i + 1, kk, 1) % mod;         ret %= mod;       }     }     return ret;   }   int main() {     pre();     scanf( %s , a);     scanf( %d , &k);     n = strlen(a);     if (k == 0) {       printf( 1 );       return 0;     }     memset(mem, -1, sizeof mem);     printf( %d , (dp(0, 0, 0) % mod + (k == 1 ? -1 : 0)));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     string s;     int i, n;     cin >> s;     n = s.length();     cout <<  Mike  <<   n ;     int mi = s[0] -  a ;     for (i = 1; i < n; i++) {       int x;       x = s[i] -  a ;       if (mi < x) {         cout <<  Ann  <<   n ;       } else {         cout <<  Mike  <<   n ;         mi = x;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, r;   int v[100], cnt, diff;   const int inf = 0x3fffffff;   int calc(int x, int y) {     cnt = 0;     int sum = 0;     while (x) {       if (y % x == 0) {         if (y == 1)           v[++cnt] = y / x;         else if (x == 1)           v[++cnt] = y / x - 1;         else           return inf;       } else         v[++cnt] = y / x;       sum += v[cnt];       int t = y - v[cnt] * x;       y = x;       x = t;     }     if (sum != n) return inf;     diff = 0;     for (int i = 1; i <= cnt; i++)       if (v[i] != 1) diff += v[i] - 1;     return diff;   }   int ans[100], ansc;   int main() {     while (scanf( %d %d , &n, &r) != EOF) {       if (r == 0) {         if (n == 1)           printf( 0 nT n );         else           puts( IMPOSSIBLE );         continue;       }       int ret = inf;       for (int i = 1; i <= r; i++) {         int now = calc(i, r);         if (ret > now) {           ret = now;           ansc = cnt;           for (int j = 1; j <= cnt; j++) ans[j] = v[j];         }       }       if (ret == inf)         puts( IMPOSSIBLE );       else {         printf( %d n , ret);         char c =  T ;         for (int i = ansc; i >= 1; i--) {           while (ans[i]--) putchar(c);           if (c ==  T )             c =  B ;           else             c =  T ;         }         puts(  );       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   const int inf = 0x3f3f3f3f;   const int maxn = 2000 + 10;   long long MOD = 1e9 + 7;   long long fac[200010];   void getFac() {     fac[0] = 1;     for (int i = 1; i < 200010; i++) {       fac[i] = fac[i - 1] * i % MOD;     }   }   long long powerMod(long long x, long long n) {     long long ans = 1;     x %= MOD;     while (n > 0) {       if (n & 1) {         ans = (ans * x) % MOD;       }       x = (x * x) % MOD;       n >>= 1;     }     return ans;   }   long long C(long long n, long long m) {     if (m > n) {       return 0;     } else {       return fac[n] * powerMod(fac[m] * fac[n - m], MOD - 2) % MOD;     }   }   long long Lucas(long long n, long long m) {     if (m == 0) {       return 1;     } else {       return C(n % MOD, m % MOD) * Lucas(n / MOD, m / MOD) % MOD;     }   }   struct Point {     int r, c;     Point(int a = 0, int b = 0) {       r = a;       c = b;     }   };   bool operator<(const Point& a, const Point& b) {     return a.r < b.r || (a.r == b.r && a.c < b.c);   }   int h, w, n;   Point point[maxn];   vector<int> G[maxn];   long long dp[maxn];   long long dfs(int u) {     if (dp[u] >= 0) {       return dp[u];     }     long long ret = Lucas(h - point[u].r + w - point[u].c, h - point[u].r);     int glen = G[u].size();     for (int i = 0; i < glen; i++) {       int v = G[u][i];       ret = (ret - Lucas(point[v].r - point[u].r + point[v].c - point[u].c,                          point[v].r - point[u].r) *                        dfs(v)) %             MOD;     }     return dp[u] = (ret + MOD) % MOD;   }   int main() {     getFac();     scanf( %d%d%d , &h, &w, &n);     for (int i = 0; i < n; i++) {       scanf( %d%d , &point[i].r, &point[i].c);     }     point[n++] = Point(1, 1);     point[n++] = Point(h, w);     sort(point, point + n);     for (int i = 0; i < n - 1; i++) {       for (int j = i + 1; j < n; j++) {         { G[i].push_back(j); }       }     }     for (int i = 0; i < n; i++) {       dp[i] = -1;     }     dp[n - 1] = 0;     long long ans = dfs(0);     printf( %I64d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Edge {     int v, next;   } edge[100010];   int head[10];   int pos;   void insert(int x, int y) {     edge[pos].v = y;     edge[pos].next = head[x];     head[x] = pos++;   }   int du[10];   int pp[100010];   int qq;   int fa[10];   int find(int x) {     if (fa[x] != x) fa[x] = find(fa[x]);     return fa[x];   }   void uunion(int x, int y) {     int fx = find(x);     int fy = find(y);     fa[fx] = fy;   }   bool vis[100010];   void dfs(int now) {     for (int i = head[now]; i != -1; i = edge[i].next) {       if (vis[i / 2]) continue;       vis[i / 2] = true;       dfs(edge[i].v);       pp[qq++] = i ^ 1;     }   }   int main() {     int n;     scanf( %d , &n);     memset(head, -1, sizeof(head));     pos = 0;     for (int i = 0; i <= 6; i++) fa[i] = i;     for (int i = 0; i < n; i++) {       int x, y;       scanf( %d %d , &x, &y);       insert(x, y);       insert(y, x);       du[x]++;       du[y]++;       uunion(x, y);     }     bool flag = true;     for (int i = 0; i <= 6; i++)       for (int j = 0; j <= 6; j++)         if (du[i] > 0 && du[j] > 0 && find(i) != find(j)) flag = false;     if (!flag) {       printf( No solution n );       return 0;     }     qq = 0;     for (int i = 0; i <= 6; i++)       if (du[i] & 1) pp[qq++] = i;     if (qq > 2) {       printf( No solution n );       return 0;     }     for (int i = 0; i <= 6; i++)       if (du[i] != 0) pp[qq++] = i;     int begin = pp[0];     qq = 0;     memset(vis, false, sizeof(vis));     dfs(begin);     for (int i = 0; i < qq; i++) {       printf( %d  , pp[i] / 2 + 1);       if (pp[i] & 1)         printf( - );       else         printf( + );       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int x[100010], y[100010], vis[100010];   int main() {     int n, ans, i;     scanf( %d , &n);     memset(vis, 0, sizeof(vis));     for (i = 1; i <= n; i++) {       scanf( %d%d , &x[i], &y[i]);       vis[x[i]]++;     }     for (i = 1; i <= n; i++) {       ans = n - 1;       ans = ans + vis[y[i]];       printf( %d %d n , ans, 2 * (n - 1) - ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, t, i, j, k;     cin >> n >> t;     string a;     cin >> a;     for (i = 1; i <= t; ++i) {       for (j = 0; j < n - 1; ++j) {         if (a[j] ==  B  && a[j + 1] ==  G ) {           a[j + 1] =  B ;           a[j] =  G ;           ++j;         }       }     }     cout << a << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, x = -1, y, xx, yy, k;   bool a[510][510];   char h;   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> h;         if (h ==  X ) {           a[i][j] = true;           if (x == -1) {             x = i;             y = j;           }           xx = i;           yy = j;           k++;         }       }     }     for (int i = x; i <= xx; i++) {       for (int j = y; j <= yy; j++) {         if (!a[i][j]) {           cout <<  NO ;           return 0;         }         k--;       }     }     if (k > 0)       cout <<  NO ;     else       cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> vec;   int main() {     int k;     cin >> k;     string arr;     while (k--) {       cin >> arr;       int n = arr.length();       int maxLen = 0;       for (int i = 0; i < n; i++) {         int len = 0;         while (arr[i] ==  L ) {           len++;           i++;           if (i == n) break;         }         maxLen = max(maxLen, len);       }       cout << maxLen + 1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long max(long long a, long long b) { return a > b ? a : b; }   long long a[100005];   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       for (int i = 1; i <= n; i++) {         cin >> a[i];       }       sort(a + 1, a + 1 + n);       long long ans = a[n] * a[n - 1] * a[n - 2] * a[n - 3] * a[n - 4];       long long ans1 = a[n] * a[n - 1] * a[n - 2] * a[1] * a[2];       long long ans2 = a[n] * a[1] * a[2] * a[3] * a[4];       cout << max(max(ans, ans1), ans2) << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     vector<long long> bs(n);     for (auto &x : bs) cin >> x;     if (count(bs.begin(), bs.end(), 0) == n) {       cout <<  YES  << endl;       for (int _ = 0; _ < (n); ++_) cout << 1 <<    ;       cout << endl;       return 0;     }     auto prv = [&](int i) { return (i + n - 1) % n; };     bs.push_back(bs[0]);     for (int i = 0; i < (n); ++i)       if (bs[i] > bs[prv(i)]) {         vector<long long> a(n);         a[i] = bs[i];         for (int k = prv(i); k != i; k = prv(k)) {           int j = prv(k), l = (k + 1) % n;           long long x;           long long dif = bs[j] - bs[k];           if (dif < 0)             x = 0;           else {             x = 1 + dif / a[l];           }           a[k] = bs[k] + x * a[l];         }         cout <<  YES  << endl;         for (auto &x : a) cout << x <<    ;         cout << endl;         return 0;       }     cout <<  NO  << endl;   }


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


#include <bits/stdc++.h>   using namespace std;   int a[10010];   int main() {     int n, m;     while (scanf( %d%d , &n, &m) == 2) {       for (int i = 1; i < n; i++) a[i] = 1;       a[n] = 0;       int maxx = 1;       for (int ii = 0; ii < m; ii++) {         for (int i = 1; i <= n; i++) {           if (a[i] + maxx <= n - i) {             printf( %d  , n - maxx);             a[i] += maxx;           } else {             printf( %d  , i + a[i]);             a[i] = n - i;           }         }         puts(  );         maxx *= 2;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void read(T& x) {     bool fu = 0;     char c;     for (c = getchar(); c <= 32; c = getchar())       ;     if (c ==  - ) fu = 1, c = getchar();     for (x = 0; c > 32; c = getchar()) x = x * 10 + c -  0 ;     if (fu) x = -x;   };   template <class T>   inline void read(T& x, T& y) {     read(x);     read(y);   }   template <class T>   inline void read(T& x, T& y, T& z) {     read(x);     read(y);     read(z);   }   template <class T>   inline void read(T& x, T& y, T& z, T& q) {     read(x);     read(y);     read(z);     read(q);   }   inline char getc() {     char c;     for (c = getchar(); c <= 32; c = getchar())       ;     return c;   }   int n, T, i, j, k, l, p;   long double ans1, ans2;   double xx;   long double f[1110][5000], g[1110][5000];   struct node {     long long t0, t1, s0, s1;     long double f;   } a[1010];   long double get(node a, node b) {     return (a.t1 + b.t1) * (1 - b.f) + a.t1 * b.f * (1 - a.f);   }   bool cmp(node a, node b) { return get(a, b) < get(b, a); }   bool upmaxs(long double& x, long double y) { return y > x ? x = y, 1 : 0; }   void upd(long double& x, long double& x0, long double y, long double y0) {     if (make_pair(y, -y0) > make_pair(x, -x0)) x = y, x0 = y0;   }   int main() {     read(n, T);     for (i = 1; i <= n; i++)       read(a[i].s0, a[i].s1, a[i].t0, a[i].t1), scanf( %lf , &xx), a[i].f = xx;     sort(a + 1, a + 1 + n, cmp);     for (i = 1; i <= n; i++)       for (j = 0; j <= T; j++) {         upd(f[i + 1][j], g[i + 1][j], f[i][j], g[i][j]);         upd(f[i + 1][j + a[i].t0], g[i + 1][j + a[i].t0], f[i][j] + a[i].s0,             g[i][j] + a[i].t0);         upd(f[i + 1][j + a[i].t0 + a[i].t1], g[i + 1][j + a[i].t0 + a[i].t1],             f[i][j] + a[i].s0 + a[i].s1 * (1 - a[i].f),             a[i].t0 + g[i][j] * a[i].f + (j + a[i].t1) * (1 - a[i].f));       }     for (j = 0; j <= T; j++) upd(ans1, ans2, f[n + 1][j], g[n + 1][j]);     cout << fixed << setprecision(20) << ans1 <<     << ans2 << endl;     scanf(  n );     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:268435456 )   using namespace std;   template <typename T1, typename T2>   istream& operator>>(istream& in, pair<T1, T2>& t) {     return in >> t.first >> t.second;   }   template <typename T1, typename T2>   ostream& operator<<(ostream& out, pair<T1, T2>& t) {     return out << t.first <<     << t.second << endl;   }   template <typename T>   istream& operator>>(istream& in, vector<T>& t) {     for (long long i = 0; i < t.size(); i++) in >> t[i];     return in;   }   template <typename T>   ostream& operator<<(ostream& out, vector<T>& t) {     for (long long i = 0; i < t.size(); i++) out << t[i] <<    ;     return out;   }   long long n, d, k;   vector<vector<long long> > g;   long long in = 1;   void printg(long long x, long long p = -1) {     for (long long to : g[x]) {       cout << x + 1 <<     << to + 1 << endl;       printg(to, x);     }   }   void con(long long x) {     g[x].push_back(in++);     if (in >= n) {       puts( YES );       printg(0);       exit(0);     }   }   void add(long long h) {     long long t = 0;     for (long long i = 1; i <= h; ++i) {       con(t);       t = in - 1;     }   }   void build(long long x, long long p, long long h, long long curh = 1) {     if (curh >= h) return;     for (long long i : g[x]) {       build(i, x, h, curh + 1);     }     while (g[x].size() < k - 1) {       con(x);       build(in - 1, x, h, curh + 1);     }   }   signed main() {     long long rrrr = 1e5;     cin >> n >> d >> k;     d++;     if (n < d) return puts( NO ), 0;     if (k == 1) {       if (n == d && n == 2) {         cout <<  YES  << endl;         for (long long i = 0; i < n - 1; ++i) {           cout << i + 1 <<     << i + 2 << endl;         }         return 0;       } else         return puts( NO ), 0;     }     --d;     g.resize(n);     add(d / 2);     add((d + 1) / 2);     build(g[0][0], 0, d / 2);     build(g[0][1], 0, (d + 1) / 2);     for (long long i = 2; i < k; ++i) {       if (d / 2 == 0) continue;       con(0);       build(in - 1, 0, d / 2);     }     cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast,unroll-loops )   using namespace std;   const int maxn = 15e4 + 7, blo = 255, mo = 998244353;   int n, q, cl = 0, p[maxn], sz[maxn], lb[maxn], rb[maxn];   int_fast64_t inv, one = 0, rec[maxn], dat[maxn];   vector<int> vt, g[maxn];   vector<vector<int>> hsz;   inline int qp(int_fast64_t x, int t = mo - 2) {     int_fast64_t re = 1;     while (t) {       if (t & 1) re = re * x % mo;       x = x * x % mo, t >>= 1;     }     return re;   }   inline void add(int i, int x) {     for (; i <= cl + 1; i += i & -i) {       dat[i] += x;       if (dat[i] >= mo) dat[i] -= mo;     }   }   inline int sum(int i) {     int_fast64_t re = 0;     for (; i > 0; i -= i & -i) {       re += dat[i];     }     re += one;     return re % mo;   }   void dfs(int u, int f) {     lb[u] = cl++;     sz[u] = 1;     p[u] = f;     for (register int i = (0); i < (g[u].size()); ++i) {       int& v = g[u][i];       if (v == f) continue;       dfs(v, u);       sz[u] += sz[v];     }     rb[u] = cl;   }   void gsz(int u, int f, int k) {     hsz.back()[u] = k;     for (register int i = (0); i < (g[u].size()); ++i) {       int& v = g[u][i];       if (v == f) continue;       gsz(v, u, k);     }   }   int main() {     cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false);     cin >> n >> q;     inv = qp(n);     for (register int i = (1); i < (n); ++i) {       int u, v;       cin >> u >> v, --u, --v;       g[u].emplace_back(v), g[v].emplace_back(u);     }     dfs(0, -1);     for (register int i = (0); i < (n); ++i) {       if (g[i].size() > blo) {         vt.emplace_back(i);         hsz.emplace_back(*new vector<int>);         hsz.back().reserve(n);         int s = 0;         for (register int j = (0); j < (g[i].size()); ++j) {           int& v = g[i][j];           if (v == p[i]) continue;           gsz(v, i, sz[v]);           s += sz[v];         }         if (p[i] >= 0) gsz(p[i], i, n - s - 1);         hsz.back()[i] = 0;       }     }     while (q--) {       int_fast64_t t, v, d, di;       cin >> t >> v, --v;       if (t == 1) {         cin >> d;         if (g[v].size() > blo) {           rec[v] += d;           if (rec[v] >= mo) rec[v] -= mo;         } else {           di = d * inv % mo;           for (register int i = (0); i < (g[v].size()); ++i) {             int& u = g[v][i];             int x = di * (n - sz[u]) % mo;             if (u == p[v]) continue;             add(lb[u] + 1, x);             add(rb[u] + 1, mo - x);           }           int x = di * sz[v] % mo;           one += x;           add(lb[v] + 1, d < x ? mo + d - x : d - x);           add(rb[v] + 1, x);           add(lb[v] + 2, mo - d);         }       } else {         int_fast64_t res = 0;         for (register int i = (0); i < (vt.size()); ++i) {           res += rec[vt[i]] * (n - hsz[i][v]) % mo;         }         cout << (res % mo * inv + sum(lb[v] + 1)) % mo <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 10;   const int base = 31337;   const int mod = 1e9 + 7;   const int inf = 0x3f3f3f3f;   const int logo = 20;   const int off = 1 << logo;   const int treesiz = off << 1;   int n;   char niz[maxn];   int his[maxn];   int main() {     scanf( %d%s , &n, niz);     long long sol = 0;     long long tren = 0;     memset(his, -1, sizeof his);     for (int i = 0; i < n; i++) {       if (niz[i] ==  0 )         sol += tren;       else {         int ptr = i;         while (ptr + 1 < n && niz[ptr + 1] ==  1 ) ptr++;         int cnt = 1;         for (int j = i; j <= ptr; j++) {           tren += j - his[cnt];           his[cnt] = ptr - cnt + 1;           sol += tren;           cnt++;         }         i = ptr;       }     }     printf( %lld n , sol);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y, z;     cin >> x >> y >> z;     if (x > y + z)       cout <<  +  << endl;     else if (y > x + z)       cout <<  -  << endl;     else if (x == y && z == 0)       cout <<  0  << endl;     else       cout <<  ?  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int ampow(int k, int p) {     long long int ans = 1;     for (int i = 0; i < p; i++) {       ans *= k;     }     return ans;   }   int main() {     std::ios::sync_with_stdio(false);     long long int k, b, n, t, z, i;     cin >> k >> b >> n >> t;     if (k == 1) {       z = 1 + (n * b);       if (t >= z)         i = 0;       else         i = ceil(((double)z - t) / b);     } else {       double c = (double)b / (k - 1);       if (log(t + c) - (n * log(k) + log(c + 1)) >= 0)         i = 0;       else if (abs(log(t + c) - (n * log(k) + log(c + 1))) <= 0.0000000001)         i = 0;       else {         double num = n * log(k) + log(c + 1) - log(t + c);         double den = log((double)k);         i = ceil(num / den);         if (i - (num / den) >= 0.9999999999) i--;       }     }     cout << i << endl;   }


#include <bits/stdc++.h>   using namespace std;   bool f(int a, string s) {     vector<char> v;     int tmp;     int ss = (s.size() - 1);     while (a) {       tmp = a % 10;       if (tmp == 7) v.push_back( 7 );       if (tmp == 4) v.push_back( 4 );       a /= 10;     }     if (v.size() != s.size()) return false;     for (int i = 0; i < s.size(); i++) {       if (s[i] != v[s.size() - i - 1]) return false;     }     return true;   }   int main() {     int a;     string s;     cin >> a >> s;     for (int i = (a + 1); i <= 100000000; i++) {       if (f(i, s)) {         cout << i << endl;         return 0;       }     }   }


#include <bits/stdc++.h>   int main() {     int n, a, b;     scanf( %d%d%d , &n, &a, &b);     for (int i = 1; i <= n; i++) {       long long x;       scanf( %I64d , &x);       x = ((x * a) % b) / a;       printf( %I64d  , x);     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     long long p(1);     cin >> a >> b;     while (b--) p = (p << 1ll) % 1000000009ll;     long long ans(1);     while (a--) {       p--;       if (p < 0) p += 1000000009ll;       ans *= p;       ans %= 1000000009ll;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   const int MAXN = 500000;   int A[MAXN + 10];   int main() {     scanf( %d %d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &A[i]);     A[n + 1] = INT_MAX;     A[0] = INT_MIN;     sort(A + 1, A + n + 1);     int l = 1, r = n;     for (l = 2; l <= n && A[l] == A[l - 1]; l++)       ;     l--;     int days = k;     while (l <= n) {       long long d = l * (long long)(A[l + 1] - A[l]);       if (d <= days) {         days -= d;         A[l] = A[l + 1];         while (l <= n && A[l] == A[l + 1]) l++;       } else {         long long m = days / l, M = days % l, v = A[l] + m;         for (int i = 1; i <= l; i++) A[i] = v;         for (int i = 0; i < M; i++) A[l - i] = v + 1LL;         break;       }     }     for (r = n - 1; r > 0 && A[r] == A[r + 1]; r--)       ;     r++;     days = k;     while (r > 0) {       long long d = (n - r + 1) * ((long long)A[r] - (long long)A[r - 1]);       if (d <= days) {         days -= d;         A[r] = A[r - 1];         while (r > 0 && A[r] == A[r - 1]) r--;       } else {         long long m = days / (n - r + 1), M = days % (n - r + 1), v = A[r] - m;         for (int i = r; i <= n; i++) A[i] = v;         for (int i = 0; i < M; i++) A[r + i] = v - 1LL;         break;       }     }     printf( %d n , A[n] - A[1]);     return 0;   }


#include <bits/stdc++.h>   const int INF_INT = 0x3f3f3f3f;   const long long INF_LL = 0x7f7f7f7f;   const int MOD = 1e9 + 7;   const double eps = 1e-10;   const double pi = acos(-1);   using namespace std;   const int N = 200010;   long long n, k;   long long fac[N], inv[N];   long long ksm(long long a, long long x, long long mod) {     if (x == 0) return 1;     long long ret = ksm(a, x / 2, mod);     ret = ret * ret % mod;     if (x & 1) ret = ret * a % mod;     return ret;   }   void init() {     fac[0] = 1;     inv[0] = 1;     for (long long i = 1; i < N; i++) {       fac[i] = fac[i - 1] * i % MOD;       inv[i] = ksm(fac[i], MOD - 2, MOD);     }   }   map<int, int> mp;   long long C(long long n, long long k) {     long long ret = fac[n] * inv[k] % MOD;     ret = ret * inv[n - k] % MOD;     return ret;   }   int main(int argc, char const *argv[]) {     init();     while (cin >> n >> k) {       mp.clear();       for (int i = 1; i <= n; i++) {         int l, r;         scanf( %d%d , &l, &r);         mp[l]++;         mp[r + 1]--;       }       int last = mp.begin()->first;       int tmp = 0;       long long ans = 0;       for (auto it = mp.begin(); it != mp.end(); it++) {         long long num = it->first - last;         if (tmp >= k) ans += C(tmp, k) * num % MOD;         ans %= MOD;         tmp += it->second;         last = it->first;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<long long> v[3005];   long long f[20][3005], ans;   int n, k;   void solve(int l, int r, int d) {     if (l == r) {       for (int i = (int)(0); i <= (int)(v[l].size() - 1); i++)         if (i + 1 <= k) ans = max(ans, v[l][i] + f[d][k - i - 1]);       return;     }     int mid = (l + r) / 2;     memcpy(f[d + 1], f[d], sizeof(f[d]));     for (int i = (int)(mid + 1); i <= (int)(r); i++) {       long long v1 = v[i].size(), v2 = v[i][v1 - 1];       for (int j = (int)(k - v1); j >= (int)(0); j--)         f[d + 1][j + v1] = max(f[d + 1][j + v1], f[d + 1][j] + v2);     }     solve(l, mid, d + 1);     memcpy(f[d + 1], f[d], sizeof(f[d]));     for (int i = (int)(l); i <= (int)(mid); i++) {       long long v1 = v[i].size(), v2 = v[i][v1 - 1];       for (int j = (int)(k - v1); j >= (int)(0); j--)         f[d + 1][j + v1] = max(f[d + 1][j + v1], f[d + 1][j] + v2);     }     solve(mid + 1, r, d + 1);   }   int main() {     scanf( %d%d , &n, &k);     for (int i = (int)(1); i <= (int)(n); i++) {       int t;       scanf( %d , &t);       v[i].resize(t);       for (int j = (int)(0); j <= (int)(t - 1); j++) scanf( %lld , &v[i][j]);       for (int j = (int)(1); j <= (int)(t - 1); j++) v[i][j] += v[i][j - 1];     }     solve(1, n, 0);     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   string s, t, k;   string str(string a) {     if (a[1] ==  X )       swap(a[1], a[3]);     else if (a[2] ==  X )       swap(a[2], a[3]);     else if (a[0] ==  X )       swap(a[0], a[2]), swap(a[2], a[3]);     string b = a.substr(0, 3);     return b;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> s >> k;     s += k;     s = str(s);     cin >> t >> k;     t += k;     t = str(t);     if (s == t) {       cout <<  YES n ;       return 0;     }     swap(s[0], s[2]);     swap(s[1], s[2]);     if (s == t) {       cout <<  YES n ;       return 0;     }     swap(s[0], s[2]);     swap(s[1], s[2]);     if (s == t) {       cout <<  YES n ;       return 0;     }     cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 15;   const int K = 500;   int a[N][N];   const int INF = 1e9 + 7;   int calc(int x, int y, int h, int w) {     int cnt = 0;     for (int i = x; i < x + h; i++) {       for (int j = y; j < y + w; j++) {         cnt += a[i][j];       }     }     return cnt;   }   int main() {     int r, c;     int n, k;     cin >> r >> c >> n >> k;     for (int i = 0; i < n; i++) {       int x, y;       cin >> x >> y;       x--;       y--;       a[x][y] = 1;     }     int ans = 0;     for (int d = 1; d <= r; d++) {       for (int w = 1; w <= c; w++) {         for (int i = 0; i + d - 1 < r; i++) {           for (int j = 0; j + w - 1 < c; j++) {             if (calc(i, j, d, w) >= k) ans++;           }         }       }     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   long long pow2[61];   vector<long long> ans;   int main() {     std::ios_base::sync_with_stdio(false);     pow2[0] = 1;     pow2[1] = 2;     for (int i = 2; i <= 60; i++) pow2[i] = pow2[i - 1] * 2;     long long X, d;     scanf( %lld %lld , &X, &d);     while (X > 1) {       long long k = 0;       while (pow2[k] - 1 < X) k++;       k--;       long long j = 1;       if (ans.size()) {         j = ans.back() + d;       }       for (int i = 1; i <= k; i++) ans.push_back(j);       X -= pow2[k] - 1;     }     if (X == 1) {       if (ans.size())         ans.push_back(ans.back() + d);       else         ans.push_back(1);     }     if (ans.size() > 10000)       printf( -1 n );     else {       printf( %lu n , ans.size());       for (int i = 0; i < ans.size(); i++)         if (ans[i] >= 1e18) return 0 * printf( -1 n );       for (int i = 0; i < ans.size(); i++) printf( %lld  , ans[i]);       printf(  n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   typedef long long int ll;         ll sum(ll n)   {       ll s=0;       while(n>0)       {           s+= n%10;           n=n/10;          }       return s;   }         ll gcd(ll n, ll m )   {       ll k = n%m;       while(k!=0)       {           n=m;           m=k;           k= n%m;       }          return m;   }      ll gcdSum(ll n)   {       ll x = sum(n);       ll num =gcd(n,x);       while(num ==1)       {           n++;           if (n%10 !=0)           {               x++;           }           else           {               x-= 8;           }           num = gcd(n,x);              //cout<<n<<   <<x<<   <<num<<endl;          }          return n;   }            int main() {       #ifndef ONLINE_JUDGE       // for getting input from input.txt       freopen( input.txt ,  r , stdin);       // for writing output to output.txt       freopen( output.txt ,  w , stdout);   #endif    //code    int t;       cin >> t;       while(t>0)       {           ll n,w;           cin >> n>>w;              map<ll,int> mp;           for(ll i=0;i<n;i++)           {               ll wi ;               cin >> wi;               mp[wi]++;           }           // for(auto x : mp)           // {           //     cout<< x.first<<   <<x.second<<endl;           // }              ll h=0;                      map<ll,int>  :: iterator it;           while(!mp.empty())           {               h++;               it = mp.end();               it--;               ll wirem =w;               //cout<< hi <<endl;                              while(!mp.empty() && wirem>= mp.begin()->first  )               {                   if(wirem - it->first >=0)                   {                       wirem -= it->first;                       it->second--;                       if(it->second==0)                       {                           mp.erase(it->first);                           if(mp.empty())                           {                               break;                           }                           it= mp.end();                           it--;                                                                                }                                             }                   else                   {                       it--;                   }                     }               // for(auto x : mp)               // {               //     cout<< x.first<<   <<x.second<<endl;               // }                                                 // cout<<         <<h<<endl;           }              cout<<h<<endl;           t--;       }                     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   using namespace std;   int l[100005], r[100005], fr[100005], to[100005];   int f[100005];   vector<int> v[600000];   int fa[100005], it[100005];   int getfa(int x) { return (x == fa[x]) ? x : (fa[x] = getfa(fa[x])); }   void merge(int x, int y) {     if (x == y) return;     x = getfa(x);     y = getfa(y);     if (x == y) return;     l[x] = min(l[x], l[y]);     r[x] = max(r[x], r[y]);     fa[y] = x;   }   void update(int x, int l, int r, int tl, int tr, int p) {     if (l > tr || tl > r) return;     if (l >= tl && r <= tr) {       v[x].emplace_back(p);       return;     }     int mid = (l + r) >> 1;     update(x << 1, l, mid, tl, tr, p);     update((x << 1) | 1, mid + 1, r, tl, tr, p);   }   void get(int x, int l, int r, int p, int id) {     bool f = v[x].empty();     for (auto u : v[x]) merge(u, id);     v[x].clear();     if (!f) v[x].emplace_back(id);     if (l == r) return;     int mid = (l + r) >> 1;     if (p <= mid)       get(x << 1, l, mid, p, id);     else       get((x << 1) | 1, mid + 1, r, p, id);   }   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     vector<int> v;     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> f[i];       if (f[i] == 1) {         cin >> l[i] >> r[i];         v.emplace_back(l[i]);         v.emplace_back(r[i]);       } else {         cin >> fr[i] >> to[i];       }     }     sort(v.begin(), v.end());     v.erase(unique(v.begin(), v.end()), v.end());     for (int i = 1; i <= n; i++) {       if (f[i] == 2) continue;       l[i] = lower_bound(v.begin(), v.end(), l[i]) - v.begin() + 1;       r[i] = lower_bound(v.begin(), v.end(), r[i]) - v.begin() + 1;     }     int len = v.size(), cnt = 0;     for (int i = 1; i <= n; i++) {       if (f[i] == 1) {         fa[i] = i;         it[++cnt] = i;         if (l[i] < r[i] - 1) update(1, 1, len, l[i] + 1, r[i] - 1, i);         get(1, 1, len, l[i], i);         get(1, 1, len, r[i], i);       } else {         int t1 = getfa(it[fr[i]]), t2 = getfa(it[to[i]]);         if (l[t1] > l[t2] && l[t1] < r[t2] || r[t1] > l[t2] && r[t1] < r[t2] ||             t1 == t2) {           cout <<  YES n ;         } else           cout <<  NO n ;       }     }     return 0;   }


#include <bits/stdc++.h>   const int maxn = 100010, mod = 998244353;   int n, fac[maxn], ifac[maxn];   long long pw(long long a, int n = mod - 2) {     long long b = 1;     for (; n; n >>= 1) n& 1 ? b = b * a % mod : 1, a = a * a % mod;     return b;   }   struct edge {     int to;     edge* next;   } E[maxn * 2], *ne = E, *fir[maxn];   void link(int u, int v) {     *ne = (edge){v, fir[u]};     fir[u] = ne++;   }   int fa[maxn], siz[maxn], son[maxn];   void init(int i) {     siz[i] = 1;     for (edge* e = fir[i]; e; e = e->next)       if (e->to != fa[i]) {         fa[e->to] = i;         init(e->to);         if (siz[e->to] > siz[son[i]]) son[i] = e->to;         siz[i] += siz[e->to];       }   }   int cs[maxn];   std::vector<int> ps[maxn], f[maxn];   bool cmp(int i, int j) { return siz[i] < siz[j]; }   std::vector<int> add(std::vector<int> a, std::vector<int> b) {     std::vector<int> c;     for (int i = 0; i < a.size() || i < b.size(); i++)       c.push_back(((i < a.size() ? a[i] : 0) + (i < b.size() ? b[i] : 0)) % mod);     return c;   }   int ws[1 << 17];   void dft(int* a, int n, bool r = 0) {     int* w = ws + n / 2;     w[0] = 1;     w[1] = pw(3, (mod - 1) / n);     if (r) w[1] = pw(w[1]);     for (int i = 2; i < n / 2; i++) w[i] = 1ll * w[1] * w[i - 1] % mod;     for (int i = n / 2; --i;) ws[i] = ws[i * 2];     w = ws + 1;     for (int i = 0, j = 0, t; i < n; i++) {       if (i < j) t = a[i], a[i] = a[j], a[j] = t;       for (t = n / 2; (j ^= t) < t; t /= 2)         ;     }     for (int i = 1; i < n; w += i, i *= 2) {       for (int j = 0; j < n; j += i * 2) {         for (int k = 0, t; k < i; k++) {           t = 1ll * a[i + j + k] * w[k] % mod;           a[i + j + k] = (a[j + k] + mod - t) % mod;           a[j + k] = (a[j + k] + t) % mod;         }       }     }     if (r) {       long long I = pw(n);       for (int i = 0; i < n; i++) a[i] = a[i] * I % mod;     }   }   int A[1 << 17], B[1 << 17];   std::vector<int> mul(std::vector<int> a, std::vector<int> b) {     int n = a.size() - 1, m = b.size() - 1, N = 1;     while (N <= n + m) N *= 2;     for (int i = 0; i < N; i++) A[i] = i > n ? 0 : a[i], B[i] = i > m ? 0 : b[i];     dft(A, N);     dft(B, N);     for (int i = 0; i < N; i++) A[i] = 1ll * A[i] * B[i] % mod;     dft(A, N, 1);     std::vector<int> c;     for (int i = 0; i <= n + m; i++) c.push_back(A[i]);     return c;   }   std::pair<std::vector<int>, std::vector<int> > calc(       std::vector<std::vector<int> >& a, int l, int r) {     if (r - l == 1) return std::make_pair(a[l], a[l]);     int pl = l, pr = r, sl = 0, sr = 0;     while (pl < pr) sl < sr ? sl += a[pl++].size() - 1 : sr += a[--pr].size() - 1;     std::pair<std::vector<int>, std::vector<int> > fl = calc(a, l, pl),                                                    fr = calc(a, pl, r);     return std::make_pair(mul(fl.first, fr.first),                           add(fl.second, mul(fl.first, fr.second)));   }   void dc(int i) {     for (int o = i; o; o = son[o])       for (edge* e = fir[o]; e; e = e->next)         if (e->to != fa[o] && e->to != son[o]) dc(e->to);     std::vector<std::vector<int> > a;     for (int o = i; o; o = son[o]) {       int *cl = cs, *cr = cs;       std::vector<int>*pl = ps, *pr = ps;       for (edge* e = fir[o]; e; e = e->next)         if (e->to != fa[o] && e->to != son[o]) *cr++ = e->to;       std::sort(cs, cr, cmp);       for (int t = cr - cs; --t > 0;) {         std::vector<int>* p[2];         for (int j = 0; j < 2; j++) {           if (pl == pr || cl < cr && siz[*cl] < pl->size())             p[j] = f + *cl++;           else             p[j] = pl++;         }         *pr++ = mul(*p[0], *p[1]);       }       std::vector<int>&g = cl < cr ? f[*cl] : *pl, h;       h.push_back(0);       if (cl == cr && pl == pr)         h.push_back(1);       else         for (int j = 0; j < g.size(); j++) h.push_back(g[j]);       a.push_back(h);     }     f[i] = calc(a, 0, a.size()).second;     f[i][0] = 1;   }   int main() {     long long x;     scanf( %d%lld , &n, &x);     x %= mod;     for (int i = 1, u, v; i < n; i++)       scanf( %d%d , &u, &v), link(u, v), link(v, u);     for (int i = *fac = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod;     ifac[n] = pw(fac[n]);     for (int i = n; i; i--) ifac[i - 1] = 1ll * ifac[i] * i % mod;     init(1);     dc(1);     int s = 0;     for (int i = 1, w = 1; i <= n; i++)       s = (s + 1ll * w * ifac[i - 1] % mod * f[1][i]) % mod,       w = w * (x + i) % mod;     printf( %d n , s);   }


#include <bits/stdc++.h>   using namespace std;   int main(int argc, char const *argv[]) {     int n, x;     while (cin >> n) {       map<int, int> a;       for (int i = 0; i < n; i++) {         cin >> x;         if (x != 0) a[x]++;       }       int ans = 0, ans1 = 0, ans2 = 0;       for (map<int, int>::iterator it = a.begin(); it != a.end(); it++) {         if (it->second == 2)           ans++;         else if (it->second < 2)           ans1++;         else           ans2++;       }       if (ans2 == 0 && ans > 0)         cout << ans << endl;       else if (ans2 == 0 && ans == 0)         cout << 0 << endl;       else         cout << -1 << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int z[10000], ans[10000], size[10000], v[10000];   long long a[10004];   int vis[10004];   int root(int i) {     while (z[i] != i) i = z[i];     return i;   }   void union1(int i, int j) {     i = root(i);     j = root(j);     if (i != j) {       if (size[i] > size[j]) {         size[i] += size[j];         z[j] = i;       } else {         size[j] += size[i];         z[i] = j;       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];       size[i] = 1;       z[i] = i;     }     for (int i = 1; i <= n; i++) {       for (int j = i + 1; j <= n; j++) {         if (a[i] == 0 || a[j] == 0) continue;         long long w = a[i] * a[j];         if (w >= 0) {           double w1 = sqrt(w);           long long w2 = w1;           w2 = w2 * w2;           if (w2 == w) union1(i, j);         }       }     }     for (int i = 1; i <= n; i++) {       if (a[i] != 0) v[i] = root(i);     }     for (int i = 1; i <= n; i++) {       int cnt = 0;       for (int j = 1; j <= n; j++) vis[j] = 0;       if (a[i] == 0) {         cnt = 0;         ans[1]++;       } else {         vis[v[i]] = 1;         cnt = 1;         ans[cnt]++;       }       for (int j = i + 1; j <= n; j++) {         if (a[j] != 0) {           if (vis[v[j]] == 0) {             vis[v[j]] = 1;             cnt++;           }         }         if (cnt != 0)           ans[cnt]++;         else           ans[1]++;       }     }     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;     cout <<   n ;   }