#include <bits/stdc++.h>   const long long INF = (long long)1e9 + 9;   const long long maxn = (long long)1e9 + 6;   using namespace std;   signed main() {     long long t;     cin >> t;     while (t--) {       long long n, cnt = 0;       cin >> n;       map<string, long long> mp;       vector<string> s(n), answer(n);       for (long long i = 0; i < n; i++) {         cin >> s[i];         mp[s[i]]++;       }       for (long long i = 0; i < n; i++) {         if (mp[s[i]] == 1) {           answer[i] = s[i];           mp[s[i]] = true;         } else {           string temp = s[i];           mp[temp]--;           cnt++;           for (long long where = 0; where < 4; where++) {             for (char ch =  0 ; ch <=  9 ; ch++) {               temp[where] = ch;               if (not mp.count(temp)) {                 answer[i] = temp;                 mp[temp]++;                 where = 4;                 break;               }             }           }         }       }       cout << cnt << endl;       for (long long i = 0; i < n; i++) {         cout << answer[i] << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int a, long long int b) {     if (b) {       return gcd(b, a % b);     } else       return a;   }   int main() {     long long int l, r;     cin >> l >> r;     if (abs(l - r) < 2) {       cout <<  -1  << endl;       return 0;     }     long long int i = 0, j = 0, k = 0;     long long int ans1 = 0, ans2 = 0, ans3 = 0;     bool a = false;     for (i = l; i <= r; i += 1) {       for (j = i + 1; j <= r; j += 1) {         if (gcd(i, j) == 1) {           for (k = j + 1; j < r; j += 1) {             if (gcd(j, k) == 1 and gcd(i, k) != 1) {               a = true;               break;             }           }         }         if (a == true) break;       }       if (a == true) break;     }     if (a == true)       cout << i <<     << j <<     << k << endl;     else       cout <<  -1  << endl;     return 0;   }


#include <bits/stdc++.h>   const int inf = 1039074182;   using namespace std;   int n;   long long a[100005];   void fi() {     cout <<  sjfnb n ;     exit(0);   }   void se() {     cout <<  cslnb n ;     exit(0);   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       scanf( %I64d , a + i);     }     if (n == 1) {       if (a[0] & 1) {         fi();       } else {         se();       }     }     sort(a, a + n);     int equals = 0;     for (int i = 0; i < n - 1; i++) {       equals += (a[i] == a[i + 1]);     }     if (equals >= 2) se();     for (int i = 0; i < n - 1; i++) {       if (a[i] == a[i + 1] && (a[i] == 0 || (i && a[i] - 1 == a[i - 1]))) se();     }     long long sum = 0;     for (int i = 0; i < n; i++) {       sum += a[i];     }     if ((sum - 1LL * n * (n - 1) / 2) & 1)       fi();     else       se();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int b, p, f;       int h, c;       cin >> b >> p >> f >> h >> c;       if (b >= p * 2 + f * 2)         cout << p * h + f * c << endl;       else {         if (c >= h) {           cout << min(b / 2, f) * c + min((b - min(b / 2, f) * 2) / 2, p) * h                << endl;         } else           cout << min(b / 2, p) * h + min((b - min(b / 2, p) * 2) / 2, f) * c                << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   long long MOD = 1000000007;   long double EPS = 1e-9;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   int getRand(int l, int r) {     uniform_int_distribution<int> uid(l, r);     return uid(rng);   }   long long binpow(long long b, long long p, long long mod) {     long long ans = 1;     b %= mod;     for (; p; p >>= 1) {       if (p & 1) ans = ans * b % mod;       b = b * b % mod;     }     return ans;   }   void pre() {}   struct UnionFind {     int n, set_size, *parent, *rank;     UnionFind() {}     UnionFind(int a) {       n = set_size = a;       parent = new int[n + 1];       rank = new int[n + 1];       for (int i = 1; i <= n; ++i) parent[i] = i, rank[i] = 1;     }     int find(int x) {       if (x != parent[x]) return parent[x] = find(parent[x]);       return x;     }     void merge(int x, int y) {       int xroot = find(x), yroot = find(y);       if (xroot != yroot) {         if (rank[xroot] >= rank[yroot]) {           parent[yroot] = xroot;           rank[xroot] += rank[yroot];         } else {           parent[xroot] = yroot;           rank[yroot] += rank[xroot];         }         set_size -= 1;       }     }     void reset() {       set_size = n;       for (int i = 1; i <= n; i++) parent[i] = i, rank[i] = 1;     }     int size() { return set_size; }     void print() {       for (int i = 1; i <= n; ++i) cout << i <<   ->   << parent[i] <<   n ;     }   } uf(300300);   int n, m, k;   map<pair<int, int>, int> blocked;   set<int> unvisited;   void dfs(int tt) {     vector<long long> deleter;     for (auto v : unvisited) {       if (blocked.find(make_pair(tt, v)) == blocked.end()) {         deleter.emplace_back(v);         uf.merge(tt, v);       }     }     for (auto v : deleter) {       unvisited.erase(v);     }     for (auto v : deleter) {       dfs(v);     }   }   void solve() {     int n, m, k, kk;     cin >> n >> m >> kk;     k = kk;     int onedeg = n - 1;     for (long long i = 1; i <= m; ++i) {       int u, v;       cin >> u >> v;       blocked[make_pair(u, v)] = 1;       blocked[make_pair(v, u)] = 1;       if (u == 1 || v == 1) onedeg--;     }     for (long long i = 2; i <= n; ++i) {       unvisited.insert(i);     }     int cmp = 0;     while (!unvisited.empty()) {       int stt = *(unvisited.begin());       unvisited.erase(stt);       dfs(stt);     }     for (long long i = 2; i <= n; ++i) {       if (k && uf.find(1) != uf.find(i) &&           blocked.find(make_pair(1, i)) == blocked.end()) {         k--;         uf.merge(1, i);       }     }     bool pos = 1;     for (long long i = 2; i <= n; ++i) {       if (uf.find(1) != uf.find(i)) pos = 0;     }     if (pos && onedeg >= kk)       cout <<  possible n ;     else       cout <<  impossible n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     pre();     long long t = 1;     for (long long i = 1; i <= t; i++) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   template <ll mod>   struct ModInt {     ll v;     ll mod_pow(ll x, ll n) const {       return (!n) ? 1 : (mod_pow((x * x) % mod, n / 2) * ((n & 1) ? x : 1)) % mod;     }     ModInt(ll a = 0) : v(a >= mod ? a % mod : a) {}     ModInt operator+(const ModInt& b) const {       return (v + b.v >= mod ? ModInt(v + b.v - mod) : ModInt(v + b.v));     }     ModInt operator-(const ModInt& b) const {       return (v - b.v < 0 ? ModInt(v - b.v + mod) : ModInt(v - b.v));     }     ModInt operator*(const ModInt& b) const { return (v * b.v) % mod; }     ModInt operator/(const ModInt& b) const {       return (v * mod_pow(b.v, mod - 2)) % mod;     }     bool operator==(const ModInt& b) const { return v == b.v; }     ModInt& operator+=(const ModInt& b) {       v += b.v;       if (v >= mod) v -= mod;       return *this;     }     ModInt& operator-=(const ModInt& b) {       v -= b.v;       if (v < 0) v += mod;       return *this;     }     ModInt& operator*=(const ModInt& b) {       (v *= b.v) %= mod;       return *this;     }     ModInt& operator/=(const ModInt& b) {       (v *= mod_pow(b, mod - 2)) %= mod;       return *this;     }     ModInt pow(ll x) { return ModInt(mod_pow(v, x)); }     operator int() const { return int(v); }     operator long long int() const { return v; }   };   template <ll mod>   ostream& operator<<(ostream& out, ModInt<mod> a) {     return out << a.v;   }   template <ll mod>   istream& operator>>(istream& in, ModInt<mod> a) {     in >> a.v;     return in;   }   ll mod_pow(ll n, ll k, ll mod) {     ll res = 1;     for (; k > 0; k >>= 1) {       if (k & 1) (res *= n) %= mod;       (n *= n) %= mod;     }     return res;   }   template <typename NumType>   vector<NumType> get_inv_table(int N, int P) {     vector<NumType> res;     for (int i = 0; i < N; i++) {       res.emplace_back(NumType(mod_pow(i, P - 2, P)));     }     return res;   }   template <typename NumType>   struct LagrangePolynomial {     vector<NumType> x, y, f_table, inv_table;     bool use_inv_table;     LagrangePolynomial() : x(), y() {}     LagrangePolynomial(vector<NumType> x_, vector<NumType> y_,                        bool use_inv_table_ = false, int P = 1000000007)         : x(x_), y(y_), use_inv_table(use_inv_table_) {       int N = x.size();       for (int i = 0; i < N; i++) {         f_table.emplace_back(f(i, x[i]));       }       if (use_inv_table) {         inv_table = get_inv_table<NumType>(P, P);       }     }     NumType f(int i, NumType p) {       int N = x.size();       NumType res(1);       for (int k = 0; k < N; k++) {         if (i == k) continue;         res *= NumType(p - x[k]);       }       return res;     }     NumType interpolate(NumType p) {       int N = x.size();       NumType res(0);       for (int i = 0; i < N; i++) {         if (use_inv_table) {           res += y[i] * f(i, p) * inv_table[int(f_table[i])];         } else {           res += y[i] * f(i, p) / f_table[i];         }       }       return res;     }   };   void ECR065_E() {     const int MOD = 1000003;     using mint = ModInt<MOD>;     const int DEG = 11;     auto eval_expr = [&](vector<mint>& expr, mint x) {       mint base(1), res(0);       for (int i = 0; i < DEG; i++) {         res += expr[i] * base;         base *= x;       }       return res;     };     vector<mint> x, y, expr;     vector<int> raw_expr = {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0};     for (int i = 0; i < DEG; i++) {       expr.emplace_back(mint(raw_expr[i]));     }     for (int i = 1; i <= DEG; i++) {       x.push_back(mint(i));       cout <<  ?   << i << endl;       int res;       cin >> res;       y.push_back(mint(res));     }     LagrangePolynomial<mint> lp(x, y, true, MOD);     for (int i = 0; i < MOD; i++) {       if (lp.interpolate(i) == mint(0)) {         cout <<  !   << i << endl;         return;       }     }     cout <<  !   << -1 << endl;   }   int main() {     ECR065_E();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long T = 200002;   const long long MAXD = T * T * T;   int n, s;   int N[T];   int PREFP[T];   int SUFL[T];   int main(void) {     cin >> n;     for (int i = (int)1; i <= (int)n; i++) {       cin >> N[i];     }     for (int i = (int)1; i <= (int)n; i++) {       PREFP[i] += N[i] + PREFP[i - 1];     }     for (int i = (int)n; i >= (int)1; i--) {       int k = N[i] == 0 ? 1 : 0;       SUFL[i] = k + SUFL[i + 1];     }     int b = 1;     int e = n;     long long ans = 0;     while (true) {       while (N[b] != 1 && b <= n) {         b++;       }       while (N[e] != 0 && e >= 1) {         e--;       }       if (e > b) {         long long bs = SUFL[b] - SUFL[e + 1];         long long es = PREFP[e] - PREFP[b - 1];         if (bs > es) {           e--;           ans += es;         } else {           b++;           ans += bs;         }       } else {         break;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX = 1e6 + 1;   const long long PRIME = 1e9 + 7;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   void solve() {     long long n;     cin >> n;     string s;     bool flag = 1;     cin >> s;     long long l = 0, r = 0;     vector<long long> a(n, 0);     for (long long i = 0; i < n; i++) {       if (s[i] ==  > )         l++;       else if (s[i] ==  < )         r++;       else {         a[i] = 1;         a[(i + 1) % n] = 1;       }     }     if (l != 0 && r == 0) {       cout << n <<   n ;       return;     }     if (r != 0 && l == 0) {       cout << n <<   n ;       return;     }     if (l == 0 && r == 0) {       cout << n <<   n ;       return;     }     long long ans = 0;     for (long long i = 0; i < n; i++) {       ans += a[i];     }     cout << ans <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t;     cin >> t;     while (t--) {       solve();     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int ans = 0;     string s;     cin >> s;     for (int i = 0; i < s.size(); i++) ans = ans * 2 + (s[i] ==  4  ? 1 : 2);     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using ld = long double;   const int inf = 0x3c3c3c3c;   const long long infl = 0x3c3c3c3c3c3c3c3c;   const int MAX_N = 2e5 + 9;   const ll MAX_R = 2 * 1e15 + 9;   int a[MAX_N];   int px[MAX_N], py[MAX_N];   int main() {     cin.tie(NULL);     cin.sync_with_stdio(false);     cout.sync_with_stdio(false);     ll sx, sy;     ll dx, dy;     cin >> sx >> sy;     cin >> dx >> dy;     int n;     cin >> n;     string s;     cin >> s;     for (int i = 0; i < n; i++)       px[i + 1] = (s[i] ==  R  ? 1 : 0) + (s[i] ==  L  ? -1 : 0) + px[i];     for (int i = 0; i < n; i++)       py[i + 1] = (s[i] ==  U  ? 1 : 0) + (s[i] ==  D  ? -1 : 0) + py[i];     ll l = 0, r = MAX_R;     while (l + 1 < r) {       ll m = (l + r) / 2;       ll cx = 1LL * (m / n) * px[n] + px[m % n];       ll cy = 1LL * (m / n) * py[n] + py[m % n];       ll v = abs(dx - (sx + cx)) + abs(dy - (sy + cy));       if (v <= m)         r = m;       else         l = m;     }     if (r == MAX_R) return !printf( -1 );     printf( %lld , r);   }


#include <bits/stdc++.h>   using namespace std;   long l[101][101], n, i, j;   int main() {     cin >> n;     int x1, y1, x2, y2;     while (n--) {       cin >> x1 >> y1 >> x2 >> y2;       for (i = x1; i <= x2; i++)         for (j = y1; j <= y2; j++) l[i][j]++;     }     long long res = 0;     for (i = 1; i <= 100; i++)       for (j = 1; j <= 100; j++) res += l[i][j];     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   int a[N], x[N << 1], y[N << 1];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     int cnt = 0;     stack<int> st1, st2;     bool flag = 1;     for (int i = n; i >= 1; --i) {       if (a[i] == 1) st1.push(i);       if (a[i] == 2) {         x[++cnt] = i, y[cnt] = i;         st2.push(i);         if (!st1.empty()) {           int top = st1.top();           st1.pop();           x[++cnt] = i, y[cnt] = top;         } else {           flag = 0;           break;         }       }       if (a[i] == 3) {         x[++cnt] = i, y[cnt] = i;         if (!st2.empty()) {           int top = st2.top();           st2.pop();           x[++cnt] = i, y[cnt] = top;           st2.push(i);         } else if (!st1.empty()) {           int top = st1.top();           st1.pop();           x[++cnt] = i, y[cnt] = top;           x[++cnt] = top, y[cnt] = top;           st2.push(i);         } else {           flag = 0;           break;         }       }     }     if (!flag) {       printf( -1 n );       return 0;     }     while (!st1.empty()) {       int top = st1.top();       st1.pop();       x[++cnt] = top, y[cnt] = top;     }     printf( %d n , cnt);     for (int i = 1; i <= cnt; ++i) printf( %d %d n , x[i], y[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long xa, ya;     long long xb, yb;     long long xc, yc;     cin >> xa >> ya >> xb >> yb >> xc >> yc;     long long x = xa;     long long y = ya;     long long X = xb;     long long Y = yb;     for (int i = 0; i < 4; ++i) {       if (i == 0) {         xa = x;         ya = y;       }       if (i == 1) {         xa = -y;         ya = x;       }       if (i == 2) {         xa = -x;         ya = -y;       }       if (i == 3) {         xa = y;         ya = -x;       }       xb = X - xa;       yb = Y - ya;       if (xc == 0) {         if (yc == 0) {           if (xb != 0 || yb != 0) continue;           cout <<  YES n ;           return 0;         }         if (xb % yc == 0 && yb % yc == 0) {           cout <<  YES n ;           return 0;         }         continue;       }       if ((xb * yc - yb * xc) % (xc * xc + yc * yc) == 0) {         long long b = (xb * yc - yb * xc) / (xc * xc + yc * yc);         if ((xb - b * yc) % xc == 0) {           cout <<  YES n ;           return 0;         }       }     }     cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 310;   long long xh, yh, xu, yu;   int n;   long long a[N], b[N], c[N];   int main() {     scanf( %I64d%I64d , &xh, &yh);     scanf( %I64d%I64d , &xu, &yu);     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %I64d%I64d%I64d , a + i, b + i, c + i);     }     int cnt = 0;     for (int i = 0; i < n; ++i) {       if (a[i] * xu + b[i] * yu + c[i] < 0 && a[i] * xh + b[i] * yh + c[i] > 0) {         ++cnt;       } else if (a[i] * xu + b[i] * yu + c[i] > 0 &&                  a[i] * xh + b[i] * yh + c[i] < 0) {         ++cnt;       }     }     printf( %d n , cnt);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int r, h;     scanf( %d %d , &r, &h);     int res = ((2.0 * h + r) / 2.0 / r);     res *= 2;     int res2 = 1 + 2 * (int)((((h + r) - r * 0.5 * sqrt(3.0))) / r);     printf( %d  , max(res, res2));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     set<pair<int, int> > h;     vector<int> d(n), s(n);     for (int i = 0; i < n; i++) {       cin >> d[i] >> s[i];       if (d[i]) {         h.insert({d[i], i});       }     }     vector<pair<int, int> > ans;     while (!h.empty()) {       pair<int, int> v = *h.begin();       h.erase(h.begin());       int w = s[v.second];       h.erase(make_pair(d[w], w));       d[w]--;       s[w] ^= v.second;       ans.push_back({v.second, w});       if (d[w]) h.insert(make_pair(d[w], w));     }     cout << ans.size() <<   n ;     for (auto& x : ans) {       cout << x.first <<     << x.second <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n, i, m, s = 0, d = 0;     cin >> n >> m;     long long a[n], b[n], c[n];     for (i = 0; i < n; i++) {       cin >> a[i] >> b[i];       s += a[i];       c[i] = a[i] - b[i];     }     sort(c, c + n);     for (i = 0; i < n; i++)       if (s > m) {         s = s - c[n - 1 - i];         d++;       }     if (s > m)       cout << -1;     else       cout << d;   }   int main() {     long long a;     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct pInfo {     int idx;     long long reach;     pInfo(int i, long long r) { idx = i, reach = r; };   };   long long n, pos[200005], len[200005], R[200005], U[200005][19], S[200005][19],       q, a, b;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     while (cin >> n) {       memset(R, -1, sizeof R);       memset(U, -1, sizeof U);       for (int i = 0; i < n; ++i) cin >> pos[i] >> len[i];       pos[n] = 9990000000LL;       stack<pInfo> st;       st.push(pInfo(n, 9990000000LL));       for (int i = n - 1; i >= 0; --i) {         long long lim = pos[i] + len[i], reach = lim;         while (pos[st.top().idx] <= lim) {           reach = max(reach, st.top().reach);           st.pop();         }         R[i] = reach;         U[i][0] = st.top().idx;         S[i][0] = max(pos[U[i][0]] - reach, 0LL);         st.push(pInfo(i, reach));       }       for (int j = 1; j < 19; ++j) {         for (int i = 0; i < n; ++i) {           if (U[i][j - 1] == -1) continue;           U[i][j] = U[U[i][j - 1]][j - 1];           S[i][j] = S[i][j - 1] + S[U[i][j - 1]][j - 1];         }       }       cin >> q;       while (q--) {         cin >> a >> b;         --a;         --b;         long long res = 0;         while (a >= 0 && a <= b) {           int jmp = 0;           while (U[a][jmp] >= 0 && U[a][jmp] <= b) jmp++;           if (!jmp) break;           res += S[a][jmp - 1];           a = U[a][jmp - 1];         }         cout << res <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N(0), aSum(0), bSum(0), cap(0), last(0), out(0);     cin >> N;     for (int i = 0; i < N; i++) {       int a(0), b(0);       cin >> a >> b;       aSum += a;       bSum += b;       cap = bSum - aSum;       if (cap >= out) out = cap;     }     cout << out << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   const int INF = 1e7;   const long long Mod = 1e9 + 7;   long long powmod(long long a, long long b) {     long long res = 1;     a %= Mod;     assert(b >= 0);     for (; b; b >>= 1) {       if (b & 1) res = res * a % Mod;       a = a * a % Mod;     }     return res;   }   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   struct haha {     int pos, dat;   } arr[maxn];   char st[maxn];   int sheng[maxn], xiao[2], k, res, arrnum;   int main() {     scanf( %s , st);     k = strlen(st);     res = 0;     for (int i = 0; i <= k - 1; i++) {       if (st[i] ==  1 ) {         sheng[i] = 1 + max(xiao[1], xiao[0]);         xiao[1] = sheng[i];       } else {         sheng[i] = ++xiao[0];         res++;       }     }     int qian[3] = {0};     arrnum = 0;     for (int i = 0; i <= k - 1; i++) {       if (st[i] ==  1 ) {         qian[2] = sheng[i];       } else {         while (arrnum > 0 && arr[arrnum].dat >= qian[2] - sheng[i]) arrnum--;         arrnum++;         arr[arrnum].pos = i;         arr[arrnum].dat = qian[2] - sheng[i];       }     }     arrnum++;     arr[arrnum].pos = k;     arr[arrnum].dat = k * 2;     int arrnow = 1;     res = 0;     for (int i = 0; i <= k - 1; i++)       if (st[i] ==  1 ) {         if (qian[0] >= qian[1]) {           qian[2] = sheng[i];           bool boo = true;           while (arrnow <= arrnum && arr[arrnow].pos < i) arrnow++;           if (arrnow > arrnum)             boo = false;           else {             if (res >= arr[arrnow].dat) boo = false;           }           if (boo) {             st[i] =  0 ;             res++;             qian[0] = sheng[i];           }         }         qian[1] = sheng[i];       } else {         qian[0] = res + sheng[i];       }     printf( %s , st);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, l, r, x, res = 0;   vector<int> subset;   void search(int k, const vector<int>& v) {     if (k == n) {       long long count = accumulate(subset.begin(), subset.end(), 0);       if (count >= l && count <= r && (subset.back() - subset.front()) >= x)         ++res;     } else {       subset.push_back(v[k]);       search(k + 1, v);       subset.pop_back();       search(k + 1, v);     }   }   int main() {     cin >> n >> l >> r >> x;     vector<int> c(n);     for (int i = 0; i < n; ++i) cin >> c[i];     sort(c.begin(), c.end());     search(0, c);     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int x, y;     cin >> x;     int c = 1;     bool ch = true;     for (int i = 0; i < n - 1; i++) {       cin >> y;       if (y > x) {         if (c != 1) ch = false;       } else if (x == y) {         if (c > 2) {           ch = false;         } else           c = 2;       } else if (x > y) {         c = 3;       }       x = y;     }     if (ch)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> ans{1}, pr{2, 3, 5, 7, 11};   int main() {     cin >> n;     for (int p : pr) {       if (ans.size() > n) break;       for (int i = 0; i < (int)ans.size(); i++) {         if (ans[i] * p <= 2 * n * n) ans.emplace_back(ans[i] * p);       }     }     reverse(begin(ans), end(ans));     ans.resize(n);     for (int x : ans) cout << x <<    ;   }


#include<bits/stdc++.h>   using namespace std;   int main()   {       int t;       cin>>t;       while(t--)       {           int n,k1,k2;           cin>>n>>k1>>k2;           int w,b;           int y1,y2;           y1=n-k1;           y2=n-k2;           cin>>w>>b;          if((min(k1,k2)+(k1+k2-2*min(k1,k2))/2)>=w && (min(y1,y2)+(y1+y2-2*min(y1,y2))/2)>=b)           cout<< YES <<endl;          else           cout<< NO <<endl;       }   }


#include <bits/stdc++.h>   using namespace std;   int dx[] = {1, -1, 0, 0};   int dy[] = {0, 0, -1, 1};   int n, m;   char grid[1005][1005];   int startX, startY, endX, endY;   int cost[1005][1005];   int lookDir[1005][1005];   bool vis[1005][1005];   bool isValid(int i, int j) {     return i >= 0 && i < n && j >= 0 && j < m && grid[i][j] !=  * ;   }   void bfs() {     queue<pair<int, int> > q;     q.push({startX, startY});     cost[startX][startY] = 0;     lookDir[startX][startY] = 4;     vis[startX][startY] = 1;     while (q.size()) {       int x = q.front().first;       int y = q.front().second;       q.pop();       for (int i = 0; i < 4; i++) {         int xx = x + dx[i], yy = y + dy[i];         if (isValid(xx, yy) && (!vis[xx][yy] || cost[x][y] + 1 <= cost[xx][yy])) {           if (lookDir[x][y] == 4) {             if (dx[i] == 1) lookDir[xx][yy] = 3;             if (dx[i] == -1) lookDir[xx][yy] = 1;             if (dy[i] == 1) lookDir[xx][yy] = 2;             if (dy[i] == -1) lookDir[xx][yy] = 0;           } else if (lookDir[x][y] == 3 || lookDir[x][y] == 1) {             if (dy[i] != 0) cost[xx][yy] = cost[x][y] + 1;             if (dy[i] == -1) lookDir[xx][yy] = 0;             if (dy[i] == 1) lookDir[xx][yy] = 2;             if (dy[i] == 0)               lookDir[xx][yy] = lookDir[x][y], cost[xx][yy] = cost[x][y];           } else if (lookDir[x][y] == 0 || lookDir[x][y] == 2) {             if (dx[i] != 0) cost[xx][yy] = cost[x][y] + 1;             if (dx[i] == -1) lookDir[xx][yy] = 1;             if (dx[i] == 1) lookDir[xx][yy] = 3;             if (dx[i] == 0)               lookDir[xx][yy] = lookDir[x][y], cost[xx][yy] = cost[x][y];           }           vis[xx][yy] = 1;           q.push({xx, yy});         }       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> grid[i][j];         if (grid[i][j] ==  S ) startX = i, startY = j;         if (grid[i][j] ==  T ) endX = i, endY = j;       }     bfs();     if (cost[endX][endY] <= 2 && vis[endX][endY])       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }


#include <bits/stdc++.h>   using std::abs;   using std::cerr;   using std::cin;   using std::cout;   using std::endl;   using std::make_pair;   using std::max;   using std::min;   using std::pair;   using std::string;   using std::vector;   template <typename T>   inline T input() {     T ans;     cin >> ans;     return ans;   }   template <typename FloatT>   struct vec2 {    public:     vec2() {}     template <typename OtherT>     vec2(vec2<OtherT> other) : x(other.x), y(other.y) {}     vec2(FloatT x, FloatT y) : x(x), y(y) {}     vec2<FloatT> operator-(vec2<FloatT> other) {       return vec2<FloatT>(x - other.x, y - other.y);     }     vec2<FloatT> operator+(vec2<FloatT> other) {       return vec2<FloatT>(x + other.x, y + other.y);     }     vec2<FloatT> operator*(FloatT num) { return vec2<FloatT>(num * x, num * y); }     FloatT len() { return std::sqrt(x * x + y * y); }     FloatT x;     FloatT y;   };   const uint32_t num_iterations = 100000;   int main() {     vec2<int32_t> src;     vec2<int32_t> dest;     int32_t v, t;     vec2<int32_t> wind_before;     vec2<int32_t> wind_after;     cin >> src.x >> src.y >> dest.x >> dest.y;     cin >> v >> t;     cin >> wind_before.x >> wind_before.y >> wind_after.x >> wind_after.y;     if ((vec2<long double>(src - dest) + vec2<long double>(wind_before) * t)             .len() <= v * t) {       float left = 0;       float right = t;       for (uint32_t i = 0; i != num_iterations; ++i) {         float mid = left + (right - left) / 2;         if ((vec2<long double>(src - dest) + vec2<long double>(wind_before) * mid)                 .len() <= v * mid)           right = mid;         else           left = mid;       }       cout.precision(20);       cout << std::fixed << right << endl;     } else {       vec2<long double> new_src =           vec2<long double>(src) + vec2<long double>(wind_before) * t;       float left = 0;       float right = 1000000000;       for (uint32_t i = 0; i != num_iterations; ++i) {         float mid = left + (right - left) / 2;         if ((new_src - vec2<long double>(dest) +              vec2<long double>(wind_after) * mid)                 .len() <= v * (t + mid))           right = mid;         else           left = mid;       }       cout.precision(20);       cout << std::fixed << t + right << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;      const int N = 1000006;   const int MOD = 1000000007;      int bpow(int a, int b){       int res = 1;       while(b){           if(b&1) res = 1ll*res*a%MOD;           a = 1ll*a*a%MOD;           b >>= 1;       }       return res;   }      int inv(int a){return bpow(a, MOD-2);}      int n;   int fact[N], pw[N], ifact[N];      int comb(int a, int b){       if(a < 0 || b < 0) return 0;       if(a < b) return 0;       return 1ll*fact[a]*(1ll*ifact[b]*ifact[a-b]%MOD)%MOD;   }      int main(){       // freopen( a.in ,  r , stdin);       ios_base::sync_with_stdio(false);       cin.tie(0);       fact[0] = 1;       for(int i = 1; i<N; i++)           fact[i] = 1ll*fact[i-1]*i%MOD;       for(int i = 0; i<N; i++)           ifact[i] = inv(fact[i]);       pw[0] = 1;       for(int i = 1; i<N; i++)           pw[i] = 2*pw[i-1]%MOD;       cin >> n; int ans = 0;       for(int i = n%2; i<=n/2; i+=2){           int ways = 2ll*(0ll+comb(n-i+1, i)-comb(n-i-1, i-2)+MOD)%MOD;           int a = (n-i+1)/2, b = n-i-a;           ways = 1ll*ways*fact[a]%MOD;           ways = 1ll*ways*fact[b]%MOD;           ways = 1ll*ways*comb(n-i, a)%MOD;           ans = (ans+ways)%MOD;       }       cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, k, a[200005 / 2], ans = -1, cnt = 0;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cin >> n >> k;     for (int i = 1; i <= n + 1; i++) {       if (i < n + 1)         cin >> a[i];       else         a[i] = a[i - 1];       if (a[i] != a[i - 1])         cnt++;       else {         ans = max(ans, cnt);         cnt = 1;       }       ans = max(ans, cnt);     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   long long pw(long long a, long long k, long long p) {     if (k == 0) return 1;     return (k % 2 == 0 ? pw((a * a) % p, k / 2, p) : (a * pw(a, k - 1, p)) % p);   }   int main() {     int n;     cin >> n;     if (n == 1) {       cout << 0 << endl;       return 0;     }     vector<long long> V(n);     for (int i = 0; i < n; ++i) cin >> V[i];     sort(V.begin(), V.end());     long long ans = 0, d = 1, s = 0, P = 1000000007;     long long dm = pw(2, n - 2, P), odw = pw(2, P - 2, P);     for (int i = 0; i < n / 2; ++i) {       s += V[n - i - 1] - V[i];       s %= P;       if (n % 2 != 0 || i != n / 2 - 1)         ans += s * (d + dm);       else         ans += s * d;       ans %= P;       dm = (dm * odw) % P;       d = (d * 2) % P;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int maxN = 100 + 5;   const int mod = 1000 * 1000 * 1000 + 7;   const int maxM = 400000;   char a[102][102];   int c[maxN], cnt[maxN], p[maxN];   int main() {     ios::sync_with_stdio(0);     cin.tie();     srand(time(NULL));     int n;     cin >> n;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) cin >> a[i][j];     for (int i = 0; i < n; i++) cin >> c[i];     set<pair<int, int> > ss;     for (int i = 0; i < n; i++) ss.insert({c[i], i});     vector<int> ans;     while (ss.size()) {       while (ss.size() && ss.begin()->first < 0) ss.erase(ss.begin());       if (ss.size() == 0 || ss.begin()->first > 0) break;       auto it = *ss.begin();       int v = it.second;       ans.push_back(v);       for (int i = 0; i < n; i++)         if (a[v][i] ==  1  && c[i] >= 0) {           ss.erase({c[i], i});           c[i]--;           ss.insert({c[i], i});         }     }     cout << ans.size() << endl;     for (int i = 0; i < ans.size(); i++) cout << ans[i] + 1 <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int oo = int(2e9) + 9;   const double eps = 1e-9;   const double pi = 3.14159265358979323846264338327950;   int main() {     string str;     cin >> str;     char X[5];     int cnt[200];     cnt[ R ] = 0, cnt[ G ] = 0, cnt[ B ] = 0, cnt[ Y ] = 0;     for (int i = 0; i < str.size(); i++) {       if (str[i] ==  R ) X[i % 4] =  R ;       if (str[i] ==  B ) X[i % 4] =  B ;       if (str[i] ==  Y ) X[i % 4] =  Y ;       if (str[i] ==  G ) X[i % 4] =  G ;     }     for (int i = 0; i < str.size(); i++) {       if (str[i] ==  ! ) {         str[i] = X[i % 4];         cnt[str[i]]++;       }     }     cout << cnt[ R ] <<     << cnt[ B ] <<     << cnt[ Y ] <<     << cnt[ G ]          << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 300 * 1000 + 10;   pair<int, int> c[MAX_N];   int t[MAX_N];   int main() {     ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) cin >> c[i].first, c[i].second = i;     set<int> s;     for (int i = k; i < n + k; i++) s.insert(i);     sort(c, c + n);     long long ans = 0;     for (int i = n - 1; ~i; i--) {       int id = c[i].second;       s.erase(t[id] = *s.lower_bound(id));       ans += 1LL * abs(t[id] - id) * c[i].first;     }     cout << ans << endl;     for (int i = 0; i < n; i++) cout << t[i] + 1 <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void read(T &x) {     x = 0;     char c = getchar();     bool flag = false;     while (!isdigit(c)) {       if (c ==  - ) flag = true;       c = getchar();     }     while (isdigit(c)) {       x = (x << 1) + (x << 3) + (c ^ 48);       c = getchar();     }     if (flag) x = -x;   }   int T, n, m;   int a[1000010];   pair<long long, long long> ans;   bool cmp(const int &x, const int &y) { return x > y; }   long long qp(long long x, long long y, long long p) {     long long v = 1;     while (y) {       if (y & 1) v = v * x % p;       x = x * x % p, y >>= 1;     }     return v;   }   int main() {     read(T);     while (T--) {       read(n), read(m), ans.first = ans.second = 0;       for (int i = 1; i <= n; ++i) read(a[i]);       sort(a + 1, a + n + 1, cmp);       for (int i = 1; i <= n; ++i) {         if (!ans.first && !ans.second) {           ans.first = (ans.first + qp(m, a[i], 1000000007)) % 1000000007;           ans.second = (ans.second + qp(m, a[i], 19260817)) % 19260817;         } else {           ans.first =               (ans.first - qp(m, a[i], 1000000007) + 1000000007) % 1000000007;           ans.second = (ans.second - qp(m, a[i], 19260817) + 19260817) % 19260817;         }       }       printf( %lld n , ans.first);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e3 + 10;   struct ft_t {     long long d[N], t[N];     void init() {       memset(d, 0, sizeof(d));       memset(t, 0, sizeof(t));     }     void put(long long x, long long k) {       for (int i = x + 1; i < N; i += -i & i) d[i] ^= k;       for (int i = x + 1; (x & 1) && i < N; i += -i & i) t[i] ^= k;     }     long long ask(long long x, long long lrv = 0, long long rrv = 0) {       for (int i = x + 1; i; i -= -i & i) lrv ^= d[i];       for (int i = x + 1; i; i -= -i & i) rrv ^= t[i];       return (x + 1 & 1) * lrv ^ rrv;     }   };   struct ft2_t {     ft_t d[N], t[N];     void init() {       for (int i = 0; i < N; i++) {         d[i].init();         t[i].init();       }     }     void put(long long x, long long y, long long k) {       for (int i = x + 1; i < N; i += -i & i) d[i].put(y, k);       for (int i = x + 1; (x & 1) && i < N; i += -i & i) t[i].put(y, k);     }     long long ask(long long x, long long y, long long lrv = 0,                   long long rrv = 0) {       for (int i = x + 1; i; i -= -i & i) lrv ^= d[i].ask(y);       for (int i = x + 1; i; i -= -i & i) rrv ^= t[i].ask(y);       return (x + 1 & 1) * lrv ^ rrv;     }     void draw(long long x0, long long y0, long long x1, long long y1,               long long k) {       put(x0, y0, k), put(x1 + 1, y0, k), put(x0, y1 + 1, k),           put(x1 + 1, y1 + 1, k);     }     long long pick(long long x0, long long y0, long long x1, long long y1) {       return ask(x1, y1) ^ ask(x0 - 1, y1) ^ ask(x1, y0 - 1) ^              ask(x0 - 1, y0 - 1);     }   };   int n, m;   ft2_t zkl;   int main() {     for (; ~scanf( %d%d , &n, &m);) {       zkl.init();       for (int op, x0, y0, x1, y1; m--;) {         scanf( %d%d%d%d%d , &op, &x0, &y0, &x1, &y1);         if (op == 1) {           cout << zkl.pick(x0, y0, x1, y1) << endl;         } else {           long long k;           scanf( %lld , &k);           zkl.draw(x0, y0, x1, y1, k);         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<pair<int, int> > vneg, vpos;     int n, a, b, neg = 0, pos = 0, sum = 0, ans = 0;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a >> b;       if (a < 0) {         neg++;         vneg.push_back({a, b});       } else {         pos++;         vpos.push_back({a, b});       }       sum += b;     }     sort(vneg.begin(), vneg.end());     sort(vpos.begin(), vpos.end());     if (neg == 0) {       cout << vpos[0].second << endl;       return 0;     } else if (pos == 0) {       cout << vneg[vneg.size() - 1].second << endl;       return 0;     }     int mn = min(vneg.size(), vpos.size());     int i, j;     for (i = vneg.size() - 1, j = 0; j < mn; i--, j++) {       ans = ans + vneg[i].second + vpos[j].second;     }     if (vneg.size() > vpos.size()) ans += vneg[i].second;     if (vneg.size() < vpos.size()) ans += vpos[j].second;     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N, M;     scanf( %d %d , &N, &M);     int beat[N + 5], i;     memset(beat, 0, sizeof(beat));     set<int> S;     for (int i = 1; i <= N; i++) S.insert(i);     int L, R, W;     set<int>::iterator it;     while (M--) {       scanf( %d %d %d , &L, &R, &W);       set<int>::iterator lt = S.lower_bound(L);       set<int>::iterator rt = S.upper_bound(R);       for (it = lt; it != rt; it++) {         int val = *it;         if (val != W) beat[val] = W;       }       S.erase(lt, rt);       S.insert(W);     }     for (int i = 1; i <= N - 1; i++) printf( %d  , beat[i]);     printf( %d n , beat[N]);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(3,  Ofast ,  inline )   const int MAXN = 7200;   const int INF = 0x3f3f3f3f;   bool vis[MAXN];   char s[55][55];   struct Edge {     int x, y, z, nxt;   } e[MAXN << 1];   int dx[5] = {0, 0, 1, -1};   int dy[5] = {1, -1, 0, 0};   int n, m, cnt, tot, ans = INF, head[MAXN], dis[MAXN], NUM[55][55];   inline void add_edge(int x, int y, int z) {     e[++cnt].x = x, e[cnt].y = y, e[cnt].z = z;     e[cnt].nxt = head[x], head[x] = cnt;   }   inline void spfa(int nowx) {     for (register int i = 1; i <= tot; i++) dis[i] = INF, vis[i] = false;     std::queue<int> q;     dis[nowx] = 1, vis[nowx] = true, q.push(nowx);     while (!q.empty()) {       int x = q.front();       q.pop();       for (register int i = head[x]; i; i = e[i].nxt) {         int to = e[i].y;         if (dis[to] > dis[x] + e[i].z) {           dis[to] = dis[x] + e[i].z;           if (!vis[to]) q.push(to), vis[to] = true;         }       }       vis[x] = false;     }     int res = 0;     for (register int i = 1; i <= n; i++)       for (register int j = 1; j <= m; j++)         if (s[i][j] ==  B ) res = std::max(res, dis[NUM[i][j]]);     ans = std::min(ans, res);   }   signed main() {     scanf( %d %d , &n, &m);     for (register int i = 1; i <= n; i++) scanf( %s , s[i] + 1);     for (register int i = 1; i <= n; i++)       for (register int j = 1; j <= m; j++) NUM[i][j] = ++tot;     for (register int i = 1; i <= n; i++) {       for (register int j = 1; j <= m; j++) {         for (register int k = 0; k < 4; k++) {           int nowx = i + dx[k], nowy = j + dy[k];           nowx = i + dx[k], nowy = j + dy[k];           if (nowx < 1 or nowx > n or nowy < 1 or nowy > m) continue;           if (s[i][j] == s[nowx][nowy]) add_edge(NUM[i][j], NUM[nowx][nowy], 0);           if (s[i][j] != s[nowx][nowy]) add_edge(NUM[i][j], NUM[nowx][nowy], 1);         }       }     }     for (register int i = 1; i <= tot; i++) spfa(i);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-10;   inline int sgn(int x) {     if (x > -eps || x < eps) return 0;     if (x > 0)       return 1;     else       return -1;   }   struct point {     double x, y;     point(double a = 0, double b = 0) {       x = a;       y = b;     }     point operator+(const point &b) const { return point(x + b.x, y + b.y); }     point operator-(const point &b) const { return point(x - b.x, y - b.y); }     point operator*(const double &t) const { return point(x * t, y * t); }   };   struct circle {     point o;     double r;   };   inline double sqr(double x) { return x * x; }   inline double det(const point &a, const point &b) {     return a.x * b.y - b.x * a.y;   }   inline double dist(const point &a, const point &b) {     return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));   }   inline double mysqrt(double x) { return sqrt(max(0.0, x)); }   inline circle inversion(circle a, point p, double R) {     circle res;     double d1 = dist(p, a.o);     res.r = a.r * R * R / (sqr(d1) - sqr(a.r));     double d2 = R * R / (d1 - a.r) - res.r;     res.o = p + (a.o - p) * (d2 / d1);     return res;   }   circle a, b;   int k;   double R, r, ans;   inline void prework() { scanf( %lf%lf%d , &R, &r, &k); }   inline void mainwork() {     double lx = R * 2, rx = sqr(R * 2) / (2 * r);     a.o = point(2 * r + (R - r), 0);     a.r = R - r;     a = inversion(a, point(0, 0), 2 * R);     b.o = point(a.o.x, a.o.y + k * (rx - lx));     b.r = (rx - lx) / 2;     b = inversion(b, point(0, 0), 2 * R);     ans = b.r;   }   inline void print() { printf( %.8f n , ans); }   int main() {     int t;     scanf( %d , &t);     for (int i = 1; i <= t; i++) {       prework();       mainwork();       print();     }     return 0;   }


#include <bits/stdc++.h>   const int mod = 1e9 + 7;   const int N = 1010;   const int inf = 0x3f3f3f3f;   using namespace std;   long long sum[N];   int n, a[N], b[N], v, e;   int p[N];   int find(int x) {     if (p[x] != x) p[x] = find(p[x]);     return p[x];   }   vector<int> vt[N];   bool vis[N];   struct Data {     int a, b, c;   };   Data que[N * N];   int head;   bool dfs(int from, int to, int cap) {     if (from == to) return 1;     vis[from] = 1;     for (int i = 0; i < vt[from].size(); i++) {       int t = vt[from][i];       if (vis[t]) continue;       if (a[t] < cap) {         int tmp = cap - a[t];         a[from] -= cap - a[t];         a[t] += tmp;         que[head++] = (Data{from, t, tmp});         if (dfs(t, to, cap) == 0) {           a[from] += tmp;           a[t] -= tmp;           head--;           continue;         }         a[from] -= cap - tmp;         a[t] += cap - tmp;         que[head++] = (Data{from, t, cap - tmp});       } else {         if (dfs(t, to, cap) == 0) continue;         a[from] -= cap;         a[t] += cap;         que[head++] = (Data{from, t, cap});       }       return 1;     }     return 0;   }   int main() {     queue<int> quea[N], queb[N];     scanf( %d%d%d , &n, &v, &e);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       p[i] = i;     }     for (int i = 1; i <= n; i++) scanf( %d , &b[i]);     for (int i = 1; i <= e; i++) {       int x, y;       scanf( %d%d , &x, &y);       vt[x].push_back(y);       vt[y].push_back(x);       p[find(x)] = find(y);     }     for (int i = 1; i <= n; i++) {       int x = find(i);       sum[x] += a[i] - b[i];       if (a[i] > b[i]) quea[x].push(i);       if (b[i] > a[i]) queb[x].push(i);     }     for (int i = 1; i <= n; i++)       if (sum[i]) return puts( NO ), 0;     for (int i = 1; i <= n; i++) {       if (find(i) != i) continue;       while (quea[i].size()) {         memset(vis, 0, sizeof(vis));         int x = quea[i].front();         int y = queb[i].front();         if (a[x] - b[x] > b[y] - a[y]) {           dfs(x, y, b[y] - a[y]);           queb[i].pop();         } else if (a[x] - b[x] < b[y] - a[y]) {           dfs(x, y, a[x] - b[x]);           quea[i].pop();         } else {           dfs(x, y, a[x] - b[x]);           quea[i].pop();           queb[i].pop();         }       }     }     cout << head << endl;     for (int i = 0; i < head; i++) {       Data da = que[i];       cout << da.a <<     << da.b <<     << da.c << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long c[110], k;   int n, a[110];   int main() {     c[0] = c[1] = 1LL;     for (int i = 2; i < 100; i++) c[i] = c[i - 1] + c[i - 2];     scanf( %d%I64d , &n, &k);     for (int i = 1; i <= n;) {       if (k > c[n - i]) {         k -= c[n - i];         a[i] = i + 1;         a[i + 1] = i;         i += 2;       } else {         a[i] = i;         i++;       }     }     for (int i = 1; i <= n; i++) cout << a[i] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int s, t;   int psum[100005];   vector<int> pos[2][2];   int cost[100005];   long long dp[100005];   int chosen[100005];   int mx;   bool ok(int c) {     dp[0] = 0;     chosen[0] = 0;     for (int i = (1); i <= (s); i += (1)) {       dp[i] = dp[i - 1];       chosen[i] = chosen[i - 1];       if (cost[i] < 1000000000 && i >= t) {         long long nc = dp[i - t] + cost[i] - c;         if (nc < dp[i] || (nc == dp[i] && chosen[i - t] + 1 > chosen[i])) {           dp[i] = nc;           chosen[i] = chosen[i - t] + 1;         }       }     }     return (chosen[s] >= mx);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     string S;     cin >> s >> S >> t;     for (int i = (1); i <= (s); i += (1)) {       psum[i] = psum[i - 1] + (S[i - 1] ==  ? );       if (S[i - 1] !=  ? ) pos[S[i - 1] -  a ][i & 1].push_back(i);     }     for (int i = (1); i <= (s - t + 1); i += (1)) {       auto &v = pos[0][~i & 1];       int p = lower_bound(v.begin(), v.end(), i) - v.begin();       auto &v2 = pos[1][i & 1];       int q = lower_bound(v2.begin(), v2.end(), i) - v2.begin();       int ed = i + t - 1;       if ((p != v.size() && v[p] <= ed) || (q != v2.size() && v2[q] <= ed))         cost[ed] = 1000000000;       else         cost[ed] = psum[ed] - psum[i - 1];     }     chosen[0] = 0;     for (int i = (1); i <= (s); i += (1)) {       chosen[i] = chosen[i - 1];       if (i >= t && cost[i] < 1000000000)         chosen[i] = max(chosen[i], chosen[i - t] + 1);     }     mx = chosen[s];     int l = 0, r = t;     while (l <= r) {       int m = (l + r) >> 1;       if (ok(m))         r = m - 1;       else         l = m + 1;     }     ok(t + 5);     printf( %lld n , dp[s] + 1LL * (t + 5) * mx);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimization( unroll-loops )   #pragma GCC target( avx,avx2,fma )   using namespace std;   long long arr1[1000000], arr2[1000000], mp[2000000];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long n, m;     cin >> n >> m;     for (long long i = 0; i < n; i++) cin >> arr1[i];     for (int i = 1; i <= 1000000; i++) mp[i] = -1;     for (long long i = 0; i < m; i++) {       cin >> arr2[i];       mp[arr2[i]] = i;     }     deque<long long> di;     long long hi = 0, maxi = 0, r = -1;     for (long long i = 0; i < n; i++) {       r = max(r, i - 1);       long long asdf = 0;       while (1) {         if (mp[arr1[(r + 1) % n]] == -1) break;         if (di.empty())           di.push_back(mp[arr1[(r + 1) % n]] + hi), asdf++;         else {           if (mp[arr1[(r + 1) % n]] + hi > di[di.size() - 1])             di.push_back(mp[arr1[(r + 1) % n]] + hi), asdf++;           else if (mp[arr1[(r + 1) % n]] + m + hi > di[di.size() - 1] &&                    mp[arr1[(r + 1) % n]] + hi < di[0])             di.push_back(mp[arr1[(r + 1) % n]] + m + hi), asdf++;           else             break;         }         r++;       }       long long f = di.size();       maxi = max(maxi, f);       if (!di.empty()) {         di.pop_front();         if (!di.empty()) {           if (di[0] >= hi + m) hi += m;         }       }     }     cout << maxi;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         if (i == j)           cout << k <<    ;         else           cout <<  0  ;       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, a[500005];   int st[500005], num[500005], top;   long long s[500005], pre[500005], suf[500005];   long long ans, pos;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i <= n; i++) {       long long now = 1;       while (top && a[i] <= st[top]) {         now += num[top];         top--;       }       st[++top] = a[i];       num[top] = now;       s[top] = s[top - 1] + now * a[i];       pre[i] = s[top];     }     top = 0;     for (int i = n; i >= 1; i--) {       long long now = 1;       while (top && a[i] <= st[top]) {         now += num[top];         top--;       }       st[++top] = a[i];       num[top] = now;       s[top] = s[top - 1] + now * a[i];       suf[i] = s[top];     }     for (int i = 1; i <= n; i++) {       if (ans < pre[i] + suf[i] - a[i]) {         ans = pre[i] + suf[i] - a[i];         pos = i;       }     }     for (int i = pos - 1; i >= 1; i--) a[i] = min(a[i], a[i + 1]);     for (int i = pos + 1; i <= n; i++) a[i] = min(a[i], a[i - 1]);     for (int i = 1; i <= n; i++) printf( %d  , a[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   const int N = 100005;   const int MOD = 1000000009;   int f[N];   int find(int x) {     if (x != f[x]) f[x] = find(f[x]);     return f[x];   }   int main() {     int i, n, m, x, y, a, b, r = 0;     scanf( %d%d , &n, &m);     for (i = 1; i <= n; i++) f[i] = i;     while (m--) {       scanf( %d%d , &a, &b);       x = find(a);       y = find(b);       if (x == y) {         r = (r + r + 1) % MOD;       } else {         f[y] = x;       }       printf( %d n , r);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   typedef struct {     double d;     int num;   } node;   node no[1005];   int cmp(node a, node b) { return a.d < b.d; }   double dis(int x, int y) { return sqrt(x * x + y * y); }   int main() {     int n, s;     scanf( %d%d , &n, &s);     for (int i = 0; i < n; ++i) {       double x, y;       scanf( %lf%lf%d , &x, &y, &no[i].num);       no[i].d = dis(x, y);     }     sort(no, no + n, cmp);     int ans;     for (ans = 0; ans < n; ++ans) {       if (s < 1000000) {         s += no[ans].num;         if (s >= 1000000) {           printf( %.7f n , no[ans].d);           break;         }       }     }     if (ans == n) printf( -1 n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void read(int &x) {     int v = 0, f = 1;     char c = getchar();     while (!isdigit(c) && c !=  - ) c = getchar();     if (c ==  - )       f = -1;     else       v = v * 10 + c -  0 ;     while (isdigit(c = getchar())) v = v * 10 + c -  0 ;     x = v * f;   }   void read(long long &x) {     long long v = 0ll, f = 1ll;     char c = getchar();     while (!isdigit(c) && c !=  - ) c = getchar();     if (c ==  - )       f = -1;     else       v = v * 10 + c -  0 ;     while (isdigit(c = getchar())) v = v * 10 + c -  0 ;     x = v * f;   }   void readc(char &x) {     char c;     while ((c = getchar()) ==    )       ;     x = c;   }   void writes(string s) { puts(s.c_str()); }   void writeln() { writes(  ); }   void writei(int x) {     if (x < 0) {       putchar( - );       x = abs(x);     }     if (!x) putchar( 0 );     char a[25];     int top = 0;     while (x) {       a[++top] = (x % 10) +  0 ;       x /= 10;     }     while (top) {       putchar(a[top]);       top--;     }   }   void writell(long long x) {     if (x < 0) {       putchar( - );       x = abs(x);     }     if (!x) putchar( 0 );     char a[25];     int top = 0;     while (x) {       a[++top] = (x % 10) +  0 ;       x /= 10;     }     while (top) {       putchar(a[top]);       top--;     }   }   inline long long inc(int &x) { return ++x; }   inline long long inc(long long &x) { return ++x; }   inline long long inc(int &x, long long y) { return x += y; }   inline long long inc(long long &x, long long y) { return x += y; }   inline double inc(double &x, double y) { return x += y; }   inline long long dec(int &x) { return --x; }   inline long long dec(long long &x) { return --x; }   inline long long dec(int &x, long long y) { return x -= y; }   inline long long dec(long long &x, long long y) { return x -= y; }   inline double dec(double &x, double y) { return x -= y; }   inline long long mul(int &x) { return x = ((long long)x) * x; }   inline long long mul(long long &x) { return x = x * x; }   inline long long mul(int &x, long long y) { return x *= y; }   inline long long mul(long long &x, long long y) { return x *= y; }   inline double mul(double &x, double y) { return x *= y; }   inline long long divi(const int &x) {     long long ans, l, r, mid;     ans = 0;     l = 0;     r = 0x3fffffff;     while (l < r) {       mid = (l + r) / 2;       if (mid * mid <= x) {         ans = mid;         l = mid + 1;       } else         r = mid;     }     return ans;   }   inline long long divi(const long long &x) {     long long ans, l, r, mid;     ans = 0;     l = 0;     r = 0x3fffffff;     while (l < r) {       mid = (l + r) / 2;       if (mid * mid <= x) {         ans = mid;         l = mid + 1;       } else         r = mid;     }     return ans;   }   inline long long divi(int &x, long long y) { return x /= y; }   inline long long divi(long long &x, long long y) { return x /= y; }   inline double divi(double &x, double y) { return x /= y; }   inline long long mod(int &x, long long y) { return x %= y; }   inline long long mod(long long &x, long long y) { return x %= y; }   long long n, m, i, j, a[7], b[7], l[100005], ans, f, s;   string st;   int main() {     ios_base::sync_with_stdio(false);     ;     cin >> n;     if ((1) <= ((n)))       for (((i)) = (1); ((i)) <= ((n)); ((i))++) cin >> l[i];     cin >> st;     st =     + st;     if ((1) <= ((n)))       for (((i)) = (1); ((i)) <= ((n)); ((i))++) {         if (st[i] ==  G ) {           ans += l[i];           a[2] += l[i] * 2;           a[5] = 0x1bbbbbbbbbbbbbbbll;         }         if (st[i] ==  W ) {           ans += l[i];           a[1] += l[i] * 2;           a[3] = 0x1bbbbbbbbbbbbbbbll;         }         if (st[i] ==  L ) {           ans += l[i];         }         if ((1) <= ((5)))           for (((j)) = (1); ((j)) <= ((5)); ((j))++) {             s = min(l[i], a[j]);             l[i] -= s;             a[j] -= s;             ans += s * j;           }       }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 22;   const int MX = 1 << N;   int n;   bool has[MX];   bool vis[MX];   bool num[MX];   void bfs(int node) {     vis[node] = true;     queue<pair<int, int> > nd;     nd.push(make_pair(node, 1));     int tt;     int rem;     while (!nd.empty()) {       node = nd.front().first;       tt = nd.front().second;       nd.pop();       if (tt == 1) {         if (!num[node]) {           num[node] = true;           nd.push(make_pair(node, 2));         }       } else {         rem = (1 << n) - 1 - node;         if (!vis[rem] and has[rem]) {           vis[rem] = true;           nd.push(make_pair(rem, 1));         }         for (int j = 0; j < n; j++) {           if (num[node | (1 << j)] == false) {             num[node | (1 << j)] = true;             nd.push(make_pair(node | (1 << j), 2));           }         }       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     int m;     cin >> n >> m;     int x;     for (int i = 0; i < m; i++) {       cin >> x;       has[x] = true;     }     int ans = 0;     for (int i = 0; i < (1 << n); i++) {       if (has[i]) {         if (!vis[i]) {           bfs(i);           ans++;         }       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b, c, d = 1440, e = 0, f = 2, g = 0, h[1001], i, j, k, p[1001];     string s, t, x;     cin >> a >> b;     for (i = 1; i <= a; i++) {       cin >> p[i];     }     sort(p + 1, p + a + 1);     for (i = 1; i < a; i++) {       for (j = i + 1; j <= a; j++) {         if (p[j] - p[i] <= b) {           e++;         } else {           break;         }       }     }     cout << 2 * e;   }


#include <bits/stdc++.h>   using namespace std;   const int NUM = 2e5 + 10;   struct node {     int index, q;     bool operator<(const node &a) const { return index + q < a.index + a.q; }   } no[NUM];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d%d , &no[i].index, &no[i].q);     sort(no, no + n);     int ans = 1, last = 0;     for (int i = 1; i < n; i++) {       if (no[i].index - no[i].q >= no[last].index + no[last].q) {         last = i;         ans++;       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n;     cin >> n;     long long a[n];     unordered_set<long long> st;     unordered_map<long long, long long> mp;     vector<long long> V;     for (long long i = 0; i < n; i++) {       cin >> a[i];       if (a[i] < 0) {         if (st.find(abs(a[i])) == st.end()) {           cout << -1 <<   n ;           return 0;         } else {           if (mp.find(abs(a[i])) == mp.end()) {             cout << -1 <<   n ;             return 0;           }           mp.erase(abs(a[i]));           if (mp.size() == 0) {             V.push_back(i);             st.clear();             mp.clear();           }         }       } else {         if (a[i] > 0 && st.find(a[i]) == st.end()) {           st.insert(a[i]);           mp[a[i]]++;         } else {           if (mp.size() != 0 && st.size() != 0) {             cout << -1 <<   n ;             return 0;           }           if (st.size() == 0 && mp.size() == 0) {             st.insert(a[i]);             mp[a[i]]++;           } else {             cout << -1 <<   n ;             break;           }         }       }     }     if (st.size() == 0 && mp.size() == 0) {       long long sz = V.size();       cout << sz <<   n ;       cout << V[0] + 1 <<    ;       for (long long i = 1; i < sz; i++) cout << V[i] - V[i - 1] <<    ;       cout <<   n ;     } else       cout << -1 <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int const mod = 1e9 + 7;   long long int const inf = 1e18;   long long int const mxn = 2e9 + 1;   inline void file() {}   int main() {     file();     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     {       long long int n, a, b;       cin >> n >> a >> b;       if (b < 0) {         b = abs(b) % n;         if (a > b)           cout << a - b;         else           cout << a - b + n;       } else {         a += b;         if (a % n == 0)           cout << n <<   n ;         else           cout << a % n <<   n ;       }     }   }


#include <bits/stdc++.h>   const int inf = (1 << 29) - 1;   const int maxn = (int)3e5 + 10;   const int mod = (int)1e9 + 7;   using namespace std;   int n, m, k;   vector<int> g[2020];   int col[2020][1010];   pair<int, int> e[maxn];   int ans[maxn];   void dfs(int v, int cur, int nxt) {     if (col[v][cur] == 0) {       return;     }     int val = col[v][cur];     col[v][cur] = 0;     col[e[val].first ^ e[val].second ^ v][cur] = 0;     dfs(e[val].first ^ e[val].second ^ v, nxt, cur);     col[v][nxt] = val;     col[e[val].first ^ e[val].second ^ v][nxt] = val;   }   void solve() {     cin >> n >> m >> k;     for (int i = 1; i <= k; i++) {       int x, y;       cin >> x >> y;       y += n;       e[i] = make_pair(x, y);     }     n += m;     for (int i = 1; i <= k; i++) {       int u = e[i].first;       int v = e[i].second;       int c1 = 1;       while (col[u][c1]) ++c1;       int c2 = 1;       while (col[v][c2]) ++c2;       if (c1 > c2) {         swap(u, v);         swap(c1, c2);       }       if (c1 == c2) {         col[u][c1] = col[v][c2] = i;         continue;       }       dfs(v, c1, c2);       col[v][c1] = i;       col[u][c1] = i;     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j < 1010; j++) {         ans[col[i][j]] = j;       }     }     cout << *max_element(ans + 1, ans + k + 1) <<   n ;     for (int i = 1; i <= k; i++) {       cout << ans[i] <<    ;     }   }   int main() {     int t = 1;     for (int i = 1; i <= t; i++) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int x = 0, y = 0;     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         char ch;         cin >> ch;         if (ch ==  * ) {           x ^= i;           y ^= j;         }       }     }     cout << x + 1 <<     << y + 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, m = 1, a, t1 = 1, tong = 0;     cin >> n;     while (1) {       tong += t1;       if (tong > n) {         cout << --m;         return 0;       }       t1 += ++m;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};   int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};   void file() {}   void fast() {     std::ios_base::sync_with_stdio(0);     cin.tie(NULL);   }   const int N = 2e5 + 9;   int arr[N], qi[N], qs[N], qe[N];   long long qans[N], cnt;   int fre[N / 2 * 10 + 9];   long long calc(int num) { return 1LL * fre[num] * fre[num] * num; }   void add(int num) {     cnt -= calc(num);     fre[num]++;     cnt += calc(num);   }   void remove(int num) {     cnt -= calc(num);     fre[num]--;     cnt += calc(num);   }   int main() {     file();     fast();     int n, q;     cin >> n >> q;     for (int i = 0; i < n; i++) cin >> arr[i];     for (int i = 0; i < q; i++) {       qi[i] = i;       cin >> qs[i] >> qe[i];       qs[i]--, qe[i]--;     }     int sq = sqrt(n);     sort(qi, qi + q, [sq](int a, int b) {       return make_pair(qs[a] / sq, qe[a]) < make_pair(qs[b] / sq, qe[b]);     });     int s = 0, e = 0;     for (int i = 0; i < q; i++) {       int idx = qi[i];       while (e <= qe[idx]) add(arr[e++]);       while (s > qs[idx]) add(arr[--s]);       while (e > qe[idx] + 1) remove(arr[--e]);       while (s < qs[idx]) remove(arr[s++]);       qans[idx] = cnt;     }     for (int i = 0; i < q; i++) cout << qans[i] <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[500000];   long long b[500000];   long long c[500000];   long long d[500000];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long n, i;     cin >> n;     for (i = 1; i <= n; i++) {       long long x;       cin >> x;       if (x == 1)         a[i]++;       else         b[i]++;     }     for (i = 1; i <= n; i++) {       a[i] += a[i - 1];       b[i] += b[i - 1];     }     for (i = 1; i <= 3 * n; i++) {       c[i] = 1e10;       d[i] = 1e10;     }     for (i = 1; i <= n; i++) {       if (c[a[i]] == 1e10) c[a[i]] = i;       if (d[b[i]] == 1e10) d[b[i]] = i;     }     vector<pair<long long, long long> > v;     for (long long l = 1; l <= n; l++) {       long long x = 0, y = 0;       long long px, py;       long long sx = 0, sy = 0;       long long last = 0;       while (1) {         if (c[x + l] > d[y + l]) {           px = a[d[y + l]];           py = b[d[y + l]];           sy++;           last = 2;         } else if (c[x + l] < d[y + l]) {           px = a[c[x + l]];           py = b[c[x + l]];           sx++;           last = 1;         } else {           if (c[x] == n || d[y] == n)             ;           else {             sx = 0;             sy = 0;           }           if (sx == sy) sx = 0, sy = 0;           if (sx > sy && last == 2 || sy > sx && last == 1) {             sx = 0;             sy = 0;           }           break;         }         x = px;         y = py;       }       if (max(sx, sy) > 0) v.push_back(make_pair(max(sx, sy), l));     }     sort((v).begin(), (v).end());     if (v.empty())       cout << 0;     else {       cout << v.size() <<   n ;       for (i = 0; i < v.size(); i++) {         cout << v[i].first <<     << v[i].second <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e6 + 3;   int ToInt[4][4];   int FromIntI[6];   int FromIntJ[6];   vector<int> costs[4][4];   vector<int> g[4];   int take[6];   vector<int> cost, from, to;   struct dsu {     vector<int> p;     dsu(int n) : p(n, -1) {}     void Clear() { p.assign(p.size(), -1); }     int Get(int x) {       if (p[x] == -1) return x;       return p[x] = Get(p[x]);     }     void Union(int a, int b) {       a = Get(a);       b = Get(b);       if (a == b) return;       p[a] = b;     }   } d(4);   vector<int> degs;   string oneTake(4, 0);   string taken(4, 0);   int ans;   void brute(int a, int b) {     if (a == 3 && b == 4) {       taken.assign(4, 0);       for (int(i) = (0); (i) < (6); (i)++) {         if (take[i] > 0) {           d.Union(FromIntI[i], FromIntJ[i]);           taken[FromIntI[i]] = 1;           taken[FromIntJ[i]] = 1;         }       }       bool connected = true;       int repr = -1;       for (int(i) = (0); (i) < (4); (i)++) {         if (taken[i]) {           if (repr == -1) {             repr = d.Get(i);           } else {             if (repr != d.Get(i)) {               connected = false;               break;             }           }         }       }       if (!connected) return;       degs.assign(4, 0);       for (int(i) = (0); (i) < (6); (i)++) {         degs[FromIntI[i]] += take[i];         degs[FromIntJ[i]] += take[i];       }       bool euler = true;       int odds = 0;       for (int(i) = (0); (i) < (4); (i)++) {         if (degs[i] % 2 == 1) odds++;       }       if (odds != 0 && odds != 2) euler = false;       if (!euler) return;       int sum = 0;       for (int(i) = (0); (i) < (6); (i)++) {         if (take[i] > 0) {           sum += costs[FromIntI[i]][FromIntJ[i]][take[i] - 1];         }       }       oneTake.assign(4, 0);       for (int(i) = (0); (i) < (4); (i)++) {         for (int(j) = (i + 1); (j) < (4); (j)++) {           if (take[ToInt[i][j]]) {             oneTake[i] = 1;             oneTake[j] = 1;           }         }       }       int oneSum = 0;       for (int(i) = (0); (i) < (4); (i)++) {         if (oneTake[i] && !costs[i][i].empty()) oneSum += costs[i][i].back();       }       ans = max(ans, sum + oneSum);     } else if (b == 4) {       brute(a + 1, a + 2);     } else {       for (int(i) = (0); (i) < (costs[a][b].size() + 1); (i)++) {         take[ToInt[a][b]] = i;         brute(a, b + 1);       }     }   }   int main(int argc, char* argv[]) {     ios::sync_with_stdio(0);     int n;     cin >> n;     for (int(i) = (0); (i) < (n); (i)++) {       int a, b, c;       cin >> a >> c >> b;       a--;       b--;       if (a > b) swap(a, b);       cost.push_back(c);       from.push_back(a);       to.push_back(b);       g[a].push_back((int((cost).size())) - 1);       g[b].push_back((int((cost).size())) - 1);       costs[a][b].push_back(c);     }     int enumCnt = 0;     for (int(i) = (0); (i) < (4); (i)++) {       for (int(j) = (i + 1); (j) < (4); (j)++) {         ToInt[i][j] = enumCnt++;         FromIntI[ToInt[i][j]] = i;         FromIntJ[ToInt[i][j]] = j;       }     }     for (int(i) = (0); (i) < (4); (i)++) {       for (int(j) = (i); (j) < (4); (j)++) {         sort(costs[i][j].begin(), costs[i][j].end());         reverse(costs[i][j].begin(), costs[i][j].end());       }     }     for (int(i) = (0); (i) < (4); (i)++) {       for (int(j) = (i); (j) < (4); (j)++) {         for (int(k) = (1); (k) < ((int((costs[i][j]).size()))); (k)++) {           costs[i][j][k] += costs[i][j][k - 1];         }       }     }     brute(0, 1);     for (int(i) = (0); (i) < (4); (i)++) {       if (!costs[i][i].empty()) {         ans = max(ans, costs[i][i].back());       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a, ta;   int b, tb;   int h, m;   bool read() {     if (!(cin >> a >> ta)) return false;     assert(cin >> b >> tb);     assert(scanf( %d:%d , &h, &m) == 2);     return true;   }   void solve() {     int x1 = h * 60 + m;     int y1 = x1 + ta;     int ans = 0;     for (int x2 = 5 * 60 + 0; x2 < 24 * 60; x2 += b) {       int y2 = x2 + tb;       int x = max(x1, x2), y = min(y1, y2);       if (x < y) ans++;     }     cout << ans << endl;   }   int main() {     read();     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int a, b, c, d, e, f;     cin >> a >> b >> c >> d >> e;     while (e--) {       long long int A, B, C, D;       cin >> A >> B >> C >> D;       if (A == C) {         cout << max(B, D) - min(B, D) << endl;       } else {         if (B <= c && D <= c) {           cout << max(A, C) - min(A, C) + c - B + c - D << endl;         } else if (B >= d && D >= d) {           cout << max(A, C) - min(A, C) + B - d + D - d << endl;         } else {           cout << max(A, C) - min(A, C) + max(B, D) - min(B, D) << endl;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<long long> v;     long long t, p;     char c;     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) {       p = 0ll;       cin >> t >> c;       p = ((p << 8) | t);       cin >> t >> c;       p = ((p << 8) | t);       cin >> t >> c;       p = ((p << 8) | t);       cin >> t;       p = ((p << 8) | t);       v.push_back(p);     }     long long sub = 0ll;     for (long long i = 31; i >= 0; i--) {       sub = (sub | (1ll << i));       set<long long> s;       for (int j = 0; j < n; j++) {         s.insert(v[j] & sub);       }       if (s.size() == k)         break;       else if (s.size() > k) {         sub = -1;         break;       }     }     if (sub == -1)       cout <<  -1  << endl;     else {       vector<long long> last;       last.push_back(sub & ((1ll << 8) - 1ll));       sub = (sub >> 8);       last.push_back(sub & ((1ll << 8) - 1ll));       sub = (sub >> 8);       last.push_back(sub & ((1ll << 8) - 1ll));       sub = (sub >> 8);       last.push_back(sub & ((1ll << 8) - 1ll));       cout << last[3] <<  .  << last[2] <<  .  << last[1] <<  .  << last[0]            << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   bool on[N];   int n, m, x, par[N];   char c;   int chk(int x) {     int d = 2;     if (par[x] && x != 1) return par[x];     while (d * d <= x) {       if (x % d == 0) {         if (par[x / d]) return par[x / d];         if (par[d]) return par[d];       }       d++;     }     return 0;   }   int main() {     cin >> n >> m;     for (int i = 0; i < m; i++) {       scanf(  n%c%d , &c, &x);       if (c ==  + ) {         if (on[x]) {           printf( Already on n );           continue;         }         int t = chk(x);         if (t == 0) {           int d = 1;           while (d * d <= x) {             if (x % d == 0) par[d] = par[x / d] = x;             d++;           }           on[x] = true;           printf( Success n );         } else           printf( Conflict with %d n , t);       } else {         if (!on[x])           printf( Already off n );         else {           printf( Success n );           int d = 1;           while (d * d <= x) {             if (x % d == 0) par[d] = par[x / d] = 0;             d++;           }           on[x] = false;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[5050], t[5050], goal[5050];   int cnt[200], l, l2;   bool dfs(int p, bool v) {     if (p == l2) {       if (v)         return puts(goal), 1;       else         return 0;     }     if (v) {       for (int i =  a ; i <  z  + 1; i++)         if (cnt[i] > 0) {           cnt[i] -= 1;           goal[p] = i;           return dfs(p + 1, v);         }       return 1;     } else {       for (int i =  a ; i <  z  + 1; i++)         if (cnt[i] > 0 && i >= t[p]) {           cnt[i] -= 1;           goal[p] = i;           if (dfs(p + 1, v | (i > t[p]))) return 1;           cnt[i] += 1;         }     }     return 0;   }   int main() {     scanf( %s%s , s, t);     l = strlen(t);     l2 = strlen(s);     for (int i = 0; i < l2; i++) cnt[s[i]]++;     if (!dfs(0, 0)) puts( -1 );   }


#include <bits/stdc++.h>   using namespace std;   static const int INF = 500000000;   template <class T>   void debug(T a, T b) {     for (; a != b; ++a) cerr << *a <<    ;     cerr << endl;   }   int n;   double p[100005];   double get(int i) {     if (i == -1) return 0;     return p[i];   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; ++i) scanf( %lf , &p[i]);     double res = 0, prev = 0;     for (int i = 0; i < n; ++i) {       prev = prev * p[i] + p[i] * get(i - 1) * 2;       prev += p[i] * (1 - get(i - 1));       res += prev;     }     printf( %.10f n , res);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mx = 200100;   const int md = 1000000007;   bool compare(const pair<int, int>& a, const pair<int, int>& b) {     return a.first < b.first || (a.first == b.first && a.second > b.second);   }   int main() {     int n, m;     map<int, int> visit;     vector<int> ans, arr;     scanf( %d %d , &n, &m);     for (int i = 0; i < n; i += 1) {       int x;       scanf( %d , &x);       arr.push_back(x);     }     for (int i = 0; i < m; i += 1) {       int x;       scanf( %d , &x);       visit[x] = 1;     }     for (int i = 0; i < n; i += 1) {       if (visit[arr[i]]) ans.push_back(arr[i]);     }     for (int i = 0; i < ans.size(); i += 1) {       printf( %d  , ans[i]);     }     printf(  n );   }


#include <bits/stdc++.h>   using namespace std;   const long double EPS = 1e-10;   const long long MOD = 1e9 + 7;   const long double PI = 3.1415926535897932384626433832795028841;   long long powmod(long long a, long long b) {     long long res = 1;     a %= MOD;     for (; b; b >>= 1) {       if (b & 1) res = res * a % MOD;       a = a * a % MOD;     }     return res;   }   ostream& operator<<(std::ostream& stream, const long long* arr) {     int i = 0;     cout <<  { ;     while (arr[i] != INT_MAX) {       cout << arr[i];       if (arr[i + 1] != INT_MAX) cout <<  , ;       i++;     }     cout <<  } ;     return stream;   }   ostream& operator<<(std::ostream& stream, const vector<int> vec) {     cout <<  { ;     for (int i = 0; i < vec.size(); i++) {       cout << vec[i];       if (i != vec.size() - 1) cout <<  , ;     }     cout <<  } ;     return stream;   }   ostream& operator<<(std::ostream& stream,                       const pair<long long, long long>& vec) {     cout <<  (  << vec.first <<  ,  << vec.second <<  ) ;     return stream;   }   template <class T>   struct segtree_t {     vector<T> t, lazy, flag_equal;     int st, sz;     T (*addition_fun)(T, T);     T invalid_value;     segtree_t() {}     segtree_t(int start, int finish, T* arr, T (*add_fun)(T, T), T invalid_val) {       int size = (finish + 1) * 2;       int tree_size = 2;       while (tree_size < size) tree_size *= 2;       t = vector<T>(tree_size);       lazy = vector<T>(tree_size);       flag_equal = vector<T>(tree_size);       st = start;       sz = finish;       addition_fun = add_fun;       invalid_value = invalid_val;       build(1, start, finish, arr);     }     void build(int i, int l, int r, T* arr) {       if (l > r) return;       lazy[i] = 0;       flag_equal[i] = 0;       if (l == r) {         t[i] = arr[l];       } else {         build(i * 2, l, (l + r) / 2, arr);         build(i * 2 + 1, (l + r) / 2 + 1, r, arr);         t[i] = addition_fun(t[i * 2], t[i * 2 + 1]);       }     }     void inc_range(int l, int r, T value) {       if (l > r) return;       inc_range_rec(1, st, sz, l, r, value);     }     void set_values(int l, int r, T value) {       set_values_rec(1, st, sz, l, r, value);     }     void inc_range_rec(int i, int l, int r, int tl, int tr, T value) {       if (lazy[i] != 0) {         t[i] += lazy[i];         if (l != r) {           lazy[i * 2] += lazy[i];           lazy[i * 2 + 1] += lazy[i];         }         lazy[i] = 0;       }       if (r < tl or l > tr) return;       if (l > r) return;       if (l >= tl and r <= tr) {         if (l != r) {           lazy[i * 2] += value;           lazy[i * 2 + 1] += value;         }         t[i] += value;       } else {         inc_range_rec(i * 2, l, (l + r) / 2, tl, tr, value);         inc_range_rec(i * 2 + 1, (l + r) / 2 + 1, r, tl, tr, value);         t[i] = addition_fun(t[i * 2], t[i * 2 + 1]);       }     }     T query_rec(int i, int l, int r, int tl, int tr) {       if (flag_equal[i] != 0) {         if (l != r) {           t[i * 2] = t[i];           t[i * 2 + 1] = t[i];           flag_equal[i * 2] = 1;           flag_equal[i * 2 + 1] = 1;         }         flag_equal[i] = 0;       }       if (r < tl or l > tr) return invalid_value;       if (l > r) return invalid_value;       if (lazy[i] != 0) {         t[i] += lazy[i];         if (l != r) {           lazy[i * 2] += lazy[i];           lazy[i * 2 + 1] += lazy[i];         }         lazy[i] = 0;       }       if (l >= tl and r <= tr) {         return t[i];       } else {         T m1 = query_rec(i * 2, l, (l + r) / 2, tl, tr);         T m2 = query_rec(i * 2 + 1, (l + r) / 2 + 1, r, tl, tr);         if (m1 == invalid_value && m2 == invalid_value) return invalid_value;         if (m1 == invalid_value) return m2;         if (m2 == invalid_value) return m1;         return addition_fun(m1, m2);       }     }     T query(int l, int r) { return query_rec(1, st, sz, l, r); }     void set_values_rec(int i, int l, int r, int tl, int tr, T value) {       if (flag_equal[i] != 0) {         if (l != r) {           t[i * 2] = t[i];           t[i * 2 + 1] = t[i];           flag_equal[i * 2] = 1;           flag_equal[i * 2 + 1] = 1;         }         flag_equal[i] = 0;       }       if (r < tl || l > tr) return;       if (l > r) return;       if (l >= tl and r <= tr) {         t[i] = value;         flag_equal[i] = 1;       } else {         set_values_rec(i * 2, l, (l + r) / 2, tl, tr, value);         set_values_rec(i * 2 + 1, (l + r) / 2 + 1, r, tl, tr, value);         t[i] = addition_fun(t[i * 2], t[i * 2 + 1]);       }     }     ~segtree_t() {}   };   template <class T>   struct fenwick_t {     vector<T> FW;     fenwick_t(int n) { FW = vector<T>(n + 1); }     void inc(int i, T v) {       while (i <= FW.size() - 1) {         FW[i] += v;         i += i & (-i);       }     }     T get(int i) {       T rta = 0;       while (i > 0) {         rta += FW[i];         i -= i & (-i);       }       return rta;     }   };   struct dsu_t {     vector<int> p, sz, depth;     dsu_t() {}     dsu_t(int n) {       p = vector<int>(n + 1);       sz = vector<int>(n + 1);       depth = vector<int>(n + 1);       for (int i = 1; i <= n; i++) {         p[i] = i;         sz[i] = 1;         depth[i] = 1;       }     }     int get(int i) {       int j = i;       while (p[i] != i) i = p[i];       while (p[j] != j) {         int aux = p[j];         p[j] = i;         j = aux;       }       return i;     }     void merge(int a, int b) {       a = get(a);       b = get(b);       if (a == b) return;       if (sz[a] > sz[b]) {         p[b] = a;         sz[a] += sz[b];       } else {         p[a] = b;         sz[b] += sz[a];         if (depth[a] == depth[b]) depth[b]++;       }     }     void show() {       for (int i = 1; i <= p.size() - 1; i++) {         cout << i <<  :   << p[i] <<     << sz[i] <<     << depth[i] <<    n ;       }     }   };   const int maxn = 2 * 1e5 + 5;   const int maxm = 1e5 + 5;   const int maxk = 1e5 + 5;   const int maxq = 1e5 + 5;   long long n, m, r, k;   set<pair<long long, long long>> done;   long long fr(pair<long long, long long> v) {     long long i = v.first;     long long j = v.second;     long long ca = min(i, n - r + 1LL) - max(1LL, i - r + 1LL) + 1LL;     long long cb = min(j, m - r + 1LL) - max(1LL, j - r + 1LL) + 1LL;     return ca * cb;   }   struct cmp {     bool operator()(pair<long long, long long> a, pair<long long, long long> b) {       return fr(a) < fr(b);     }   };   priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,                  cmp>       cand;   long long dx[4] = {1, 0, -1, 0};   long long dy[4] = {0, 1, 0, -1};   void check_cand(long long i, long long j) {     if (i < 1 || j < 1 || i > n || j > m) return;     if (done.find(pair<long long, long long>(i, j)) != done.end()) return;     done.insert(pair<long long, long long>(i, j));     cand.push(pair<long long, long long>(i, j));   }   int main() {     cin >> n >> m >> r >> k;     check_cand(n / 2, m / 2);     check_cand(n / 2 + 1, m / 2);     check_cand(n / 2 + 1, m / 2 + 1);     check_cand(n / 2, m / 2 + 1);     long long total = 0;     while (k--) {       pair<long long, long long> tp = cand.top();       cand.pop();       done.insert(tp);       for (int j = 0; j < 4; j++) {         long long ny = tp.first + dy[j];         long long nx = tp.second + dx[j];         check_cand(ny, nx);       }       total += fr(tp);     }     double ans = double(total) / (double(n - r + 1) * double(m - r + 1));     cout << fixed << setprecision(20) << ans <<   n ;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( avx2 )   using namespace std;   long long one = 1;   const long long INF = 1e18;   const long long MOD = 1e9 + 7;   const long long N = 2e5;   vector<vector<long long> > c, gr;   vector<long long> res;   bool flag = 1;   long long dfs1(long long v, long long p, long long j, long long k) {     long long ans = c[j][v];     if (gr[v].size() > 2) flag = 0;     for (long long u : gr[v]) {       if (u == p) continue;       ans += dfs1(u, v, k, 3 - j - k);     }     return ans;   }   void dfs2(long long v, long long p, long long j, long long k) {     res[v] = j + 1;     for (long long u : gr[v]) {       if (u == p) continue;       dfs2(u, v, k, 3 - j - k);     }   }   signed main() {     long long n;     cin >> n;     c.resize(3);     for (long long i = 0; i < 3; i++) {       c[i].resize(n);       for (long long j = 0; j < n; j++) cin >> c[i][j];     }     gr.resize(n);     for (long long i = 0; i < n - 1; i++) {       long long v, u;       cin >> v >> u;       v--;       u--;       gr[v].push_back(u);       gr[u].push_back(v);     }     long long st = -1;     for (long long i = 0; i < n; i++) {       if (gr[i].size() == 1) st = i;     }     long long ans = INF, ansj = -1, ansk = -1;     for (long long j = 0; j < 3; j++) {       for (long long k = 0; k < 3; k++) {         if (j == k) continue;         long long smth = dfs1(st, -1, j, k);         assert(smth >= 0);         if (smth < ans) {           ans = smth;           ansj = j;           ansk = k;         }       }     }     if (flag == 0) {       cout << -1 << endl;       return 0;     }     if (n > 20000) {     }     res.resize(n);     cout << ans << endl;     dfs2(st, -1, ansj, ansk);     for (long long i : res) cout << i <<    ;   }


#include <bits/stdc++.h>   using namespace std;   pair<int, int> a[2001];   int f[2000], ok, n, px, ok2;   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i].first;       a[i].second = i;       f[a[i].first]++;       if (f[a[i].first] == 2) ok++;       if (f[a[i].first] == 3) ok2++;     }     if (ok < 2 && ok2 == 0)       cout <<  NO ;     else {       cout <<  YES n ;       sort(a + 1, a + 1 + n);       for (int i = 1; i <= n; i++) {         cout << a[i].second <<    ;         if (f[a[i].first] >= 2) {           px = i;         }       }       cout <<   n ;       swap(a[px], a[px - 1]);       for (int i = 1; i <= n; i++) {         cout << a[i].second <<    ;       }       for (int i = 1; i <= n; i++) {         if (f[a[i].first] >= 2) {           px = i;           break;         }       }       cout <<   n ;       swap(a[px], a[px + 1]);       for (int i = 1; i <= n; i++) {         cout << a[i].second <<    ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int L = 500;   const int N = 220000;   const int M1 = 1e9 + 7;   const int M2 = 1e9 + 9;   int chd[N][26], go[N][26];   int len[N], q[N], p[N];   int s[N], dp[N], f[17][N], t[N];   char str[N], _str[N];   int st[N], en[N], wh[N], fr[N], to[N], cur[N], state[N], id[N], ans[N], cnt[N],       sum[N], chk[N], a[N], b[N];   int h1[N], h2[N], hh1[N], hh2[N], hs1[N], hs2[N], p1[N], p2[N];   vector<int> adj[N], con[N];   int sn, e[N];   char w[N], ch[N];   void dfs(int u, int p) {     s[u] = sn;     f[0][u] = p;     dp[u] = dp[p] + 1;     for (int i = 1; i < 17; i++) f[i][u] = f[i - 1][f[i - 1][u]];     for (int tt : adj[u]) {       int v = e[tt] ^ u;       if (v == p) continue;       ch[v] = w[tt];       a[sn] = u, b[sn] = v;       t[sn++] = tt;       dfs(v, u);       a[sn] = v, b[sn] = u;       t[sn++] = tt;     }   }   int T;   void dfs(int u) {     st[u] = ++T;     for (int v : con[u]) {       dfs(v);     }     en[u] = T;   }   void modify(int x, int y) {     cnt[x] += y;     sum[x / L] += y;   }   int query(int l, int r) {     int ll = l / L, rr = r / L;     int ret = 0;     if (ll == rr) {       for (int i = l; i <= r; i++) ret += cnt[i];       return ret;     }     for (int i = l; i < min((ll + 1) * L, r + 1); i++) ret += cnt[i];     for (int i = max(l, rr * L); i <= r; i++) ret += cnt[i];     for (int i = ll + 1; i < rr; i++) ret += sum[i];     return ret;   }   int lca(int u, int v) {     if (dp[u] < dp[v]) swap(u, v);     for (int d = dp[u] - dp[v], i = 0; i < 17; i++)       if (d >> i & 1) u = f[i][u];     if (u == v) return u;     for (int i = 16; i >= 0; i--) {       if (f[i][u] == f[i][v]) continue;       u = f[i][u], v = f[i][v];     }     return f[0][u];   }   int n, m, Q;   const int B1 = 31;   const int B2 = 47;   pair<int, int> get(int *h1, int *h2, int l, int s, int e) {     return make_pair((h1[e + 1] + 1LL * (M1 - h1[s]) * p1[e - s + 1]) % M1,                      (h2[e + 1] + 1LL * (M2 - h2[s]) * p2[e - s + 1]) % M2);   }   int make_path(int u, int v) {     int p = lca(u, v);     int ret = 0;     while (u != p) {       _str[ret++] = ch[u];       u = f[0][u];     }     int nret = ret;     while (v != p) {       _str[ret++] = ch[v];       v = f[0][v];     }     reverse(_str + nret, _str + ret);     for (int i = 0; i < ret; i++) {       hh1[i + 1] = (1LL * hh1[i] * B1 + _str[i] -  a ) % M1;       hh2[i + 1] = (1LL * hh2[i] * B2 + _str[i] -  a ) % M2;     }     return ret;   }   int dist(int u, int v) { return dp[u] + dp[v] - 2 * dp[lca(u, v)]; }   int main() {     scanf( %d%d%d , &n, &m, &Q);     for (int i = 1; i < n; i++) {       int u, v;       char c[3];       scanf( %d%d%s , &u, &v, c);       e[i] = u ^ v;       w[i] = c[0];       adj[u].push_back(i);       adj[v].push_back(i);     }     dfs(1, 0);     int nodes = 1;     for (int i = 0; i < N; i++)       p1[i] = (i == 0 ? 1 : 1LL * p1[i - 1] * B1 % M1),       p2[i] = (i == 0 ? 1 : 1LL * p2[i - 1] * B2 % M2);     for (int i = 1; i <= m; i++) {       scanf( %s , str);       int l = strlen(str);       for (int k = 0; k < 2; k++) {         int u = 1, ret1 = 0, ret2 = 0;         for (int j = 0; str[j]; j++) {           int c = str[j] -  a ;           ret1 = (1LL * ret1 * B1 + c) % M1, ret2 = (1LL * ret2 * B2 + c) % M2;           int &v = chd[u][c];           if (!v) v = ++nodes;           u = v;         }         hs1[k * m + i] = ret1;         hs2[k * m + i] = ret2;         state[k * m + i] = u;         reverse(str, str + l);       }       len[i] = len[i + m] = l;     }     int qn = 0;     for (int i = 0, u; i < 26; i++)       if (u = chd[1][i]) q[qn++] = u, p[u] = 1;     for (int i = 0; i < qn; i++) {       int u = q[i];       for (int j = 0, v; j < 26; j++)         if (v = chd[u][j]) {           int w = p[u];           while (w > 1 && !chd[w][j]) w = p[w];           w = max(1, chd[w][j]);           p[v] = w;           q[qn++] = v;         }     }     for (int i = 0; i < 26; i++) go[1][i] = max(1, chd[1][i]);     for (int i = 0; i < qn; i++) {       int u = q[i];       for (int j = 0; j < 26; j++) {         if (chd[u][j])           go[u][j] = chd[u][j];         else           go[u][j] = go[p[u]][j];       }     }     for (int i = 2; i <= nodes; i++) con[p[i]].push_back(i);     dfs(1);     for (int i = 1; i <= Q; i++) {       scanf( %d%d%d , &fr[i], &to[i], &wh[i]);       if (s[fr[i]] > s[to[i]]) {         swap(fr[i], to[i]);         wh[i] += m;       }       fr[i] = s[fr[i]];       to[i] = s[to[i]] - 1;       id[i] = i;     }     sort(id + 1, id + Q + 1, [](int i, int j) {       if (fr[i] / L != fr[j] / L) return fr[i] / L < fr[j] / L;       return to[i] < to[j];     });     for (int i = 1, j; i <= Q; i = j) {       int u = id[i];       int r = fr[u] / L * L - 1;       memset(chk, 0, sizeof chk);       memset(cnt, 0, sizeof cnt);       memset(sum, 0, sizeof sum);       int rn = 0;       cur[0] = 1;       for (j = i; j <= Q && fr[id[i]] / L == fr[id[j]] / L; j++) {         int v = id[j];         while (r < to[v]) {           ++r;           int eid = t[r];           int c = w[eid] -  a ;           if (chk[eid]) {             modify(st[cur[rn]], -1);             rn--;           } else {             cur[rn + 1] = go[cur[rn]][c];             h1[rn + 1] = (1LL * h1[rn] * B1 + c) % M1;             h2[rn + 1] = (1LL * h2[rn] * B2 + c) % M2;             modify(st[cur[++rn]], 1);           }           chk[eid] ^= 1;         }         int who = wh[v];         ans[v] = query(st[state[who]], en[state[who]]);         int xx = a[fr[u] / L * L];         int yy = a[fr[v]];         int zz = b[r];         int pxy = lca(xx, yy);         int pyz = lca(yy, zz);         int pzx = lca(zz, xx);         int pp = pxy ^ pyz ^ pzx;         int xy = dp[xx] + dp[yy] - 2 * dp[pxy];         int yz = dp[zz] + dp[yy] - 2 * dp[pyz];         int zx = dp[xx] + dp[zz] - 2 * dp[pzx];         int xlen = (xy + zx - yz) / 2;         assert(xlen == dist(xx, pp));         assert(rn == zx);         int _ans = ans[v];         for (int k = 0; k < xlen; k++) {           if (k + len[who] - 1 >= rn) break;           auto ret = get(h1, h2, rn, k, k + len[who] - 1);           if (ret.first == hs1[who] && ret.second == hs2[who]) ans[v]--;         }         int __ans = ans[v];         int ylen = make_path(yy, pp);         for (int k = 0; k < ylen; k++) {           if (k + len[who] - 1 < ylen) {             auto ret = get(hh1, hh2, ylen, k, k + len[who] - 1);             if (ret.first == hs1[who] && ret.second == hs2[who]) ans[v]++;             continue;           }           if (len[who] - ylen + k > rn - xlen) break;           auto first = get(hh1, hh2, ylen, k, ylen - 1);           auto second = get(h1, h2, rn, xlen, xlen + len[who] - (ylen - k) - 1);           int ret1 =               (1LL * first.first * p1[len[who] - (ylen - k)] + second.first) % M1;           int ret2 =               (1LL * first.second * p2[len[who] - (ylen - k)] + second.second) %               M2;           if (ret1 == hs1[who] && ret2 == hs2[who]) ans[v]++;         }       }     }     for (int i = 1; i <= Q; i++) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using db = long double;   using str = string;   using pi = pair<int, int>;   template <class T>   using V = vector<T>;   template <class T, size_t SZ>   using AR = array<T, SZ>;   using vi = V<int>;   using vb = V<bool>;   using vpi = V<pi>;   const int MOD = 1e9 + 7;   const db EPS = 1e-9;   const ll BIG = 1e18;   template <class T>   bool ckmin(T& a, const T& b) {     return b < a ? a = b, 1 : 0;   }   template <class T>   bool ckmax(T& a, const T& b) {     return a < b ? a = b, 1 : 0;   }   struct mi {     int v;     explicit operator int() const { return v; }     mi() : v(0) {}     mi(ll _v) : v(int(_v % MOD)) { v += (v < 0) * MOD; }   };   mi& operator+=(mi& a, mi b) {     if ((a.v += b.v) >= MOD) a.v -= MOD;     return a;   }   mi& operator-=(mi& a, mi b) {     if ((a.v -= b.v) < 0) a.v += MOD;     return a;   }   mi operator+(mi a, mi b) { return a += b; }   mi operator-(mi a, mi b) { return a -= b; }   mi operator*(mi a, mi b) { return mi((ll)a.v * b.v); }   mi& operator*=(mi& a, mi b) { return a = a * b; }   mi pow(mi a, ll p) {     assert(p >= 0);     return p == 0 ? 1 : pow(a * a, p / 2) * (p & 1 ? a : 1);   }   mi inv(mi a) {     assert(a.v != 0);     return pow(a, MOD - 2);   }   mi operator/(mi a, mi b) { return a * inv(b); }   bool operator==(mi a, mi b) { return a.v == b.v; }   vector<mi> invs, fac, ifac;   void genFac(int SZ) {     invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ);     invs[1] = fac[0] = ifac[0] = 1;     for (int i = (2); i < (SZ); ++i)       invs[i] = mi(-(ll)MOD / i * (int)invs[MOD % i]);     for (int i = (1); i < (SZ); ++i)       fac[i] = fac[i - 1] * i, ifac[i] = ifac[i - 1] * invs[i];   }   mi comb(int a, int b) {     if (a < b || b < 0) return 0;     return fac[a] * ifac[b] * ifac[a - b];   }   const int mx = 100005;   struct Eff {     int n, k;     mi first[mx];     void solve() {       genFac(100005);       cin >> n >> k;       map<int, int> cnt;       int negs = 0;       for (int i = 1; i <= n; i++) {         int A;         cin >> A;         if (A == -1) {           negs++;         } else {           cnt[A]++;         }       }       map<int, mi> all_cnts;       for (auto u : cnt) {         all_cnts[u.second] = mi(0);       }       all_cnts[0] = mi(0);       first[0] = mi(0);       for (int i = 0; i <= n - 1; i++) {         first[i + 1] = mi(-n);         if (i - 1 >= 0) {           first[i + 1] -= mi(i) * mi(k - 1) * first[i - 1];         }         first[i + 1] += first[i] * (mi(n) - 2 * mi(i) + mi(i) * mi(k));         first[i + 1] = first[i + 1] / mi(n - i);       }       vector<mi> k1pows(n + 1, mi(1));       for (int i = 1; i <= n; i++) {         k1pows[i] = k1pows[i - 1] * mi(k - 1);       }       for (auto& u : all_cnts) {         for (int i = 0; i <= negs; i++) {           u.second += first[u.first + i] * mi(comb(negs, i)) * k1pows[negs - i];         }         u.second = u.second / pow(mi(k), negs);       }       mi ans = mi(0);       map<int, int> cnt_cnt;       for (auto u : cnt) {         cnt_cnt[u.second]++;       }       cnt_cnt[0] += (k - int((cnt).size()));       for (auto u : cnt_cnt) {         ans += all_cnts[u.first] * mi(u.second);       }       ans -= (first[n] + mi(k - 1) * first[0]);       cout << ans.v <<   n ;     }   };   int main() {     cin.tie(0)->sync_with_stdio(0);     Eff e;     e.solve();   }


#include<bits/stdc++.h>   #define fre(x) freopen(#x .in , r ,stdin);freopen(#x .out , w ,stdout)   #define ll long long   using namespace std;   inline int read(){    int x=0,f=1;char c=getchar();    while(c< 0 ||c> 9 ) { if(c== - ) f=-1;c=getchar(); }    while(c>= 0 &&c<= 9 ) x=(x<<3)+(x<<1)+(c^ 0 ),c=getchar();    return x*f;   }   const int N=1e5+5;   int n,l[N],r[N];   ll f[N][2];   vector<int>e[N];   void dfs(int x,int fa){    f[x][0]=f[x][1]=0;    for(auto y:e[x]) if(y^fa){     dfs(y,x);     f[x][0]+=max(f[y][0]+abs(l[x]-l[y]),f[y][1]+abs(l[x]-r[y]));     f[x][1]+=max(f[y][0]+abs(r[x]-l[y]),f[y][1]+abs(r[x]-r[y]));    }   }   inline void solve(){    n=read();    for(int i=1;i<=n;i++) l[i]=read(),r[i]=read(),e[i].clear();    for(int i=1;i<n;i++){     int x=read(),y=read();     e[x].push_back(y);     e[y].push_back(x);    }    dfs(1,0);    printf( %lld n ,max(f[1][0],f[1][1]));   }   int main(){    int T=read();    while(T--) solve();    return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, k;   int d[100015];   char baca[100015];   int main() {     scanf( %d %d , &n, &k);     scanf( %s , baca);     for (int i = (int)1; i <= (int)n; ++i) d[i] = (int)(baca[i - 1] -  0 );     d[0] = d[n + 1] = 0;     for (int i = (int)1; i <= (int)n; ++i) {       if (!k) break;       if (d[i] == 4 && d[i + 1] == 7) {         if (i % 2 == 1) {           if (d[i + 2] == 7) {             if (k % 2 == 0)               break;             else {               d[i + 1] = 4;               break;             }           } else             d[i + 1] = 4;         } else {           if (d[i - 1] == 4) {             if (k % 2 == 0)               break;             else {               d[i] = 7;               break;             }           } else             d[i] = 7;         }         k--;       }     }     for (int i = (int)1; i <= (int)n; ++i) printf( %d , d[i]);     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000005;   int v[N], c[N];   int n, k;   vector<int> z;   void input() {     scanf( %d %d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &v[i]);     for (int i = 1; i <= n; i++) scanf( %d , &c[i]);     z.push_back(min(100 * v[n], c[n]));     for (int i = n - 1; i >= 1; i--) {       int larger = max(z.back(), v[i] * 100);       int smaller = min(larger, c[i]);       z.push_back(min(larger, smaller));     }   }   void pro() {     sort(z.begin(), z.end());     double ans = 0;     double chance = 1;     for (int i = 0; i < k; i++) {       ans += z[i] * chance * k / (n - i);       chance *= 1. * (n - k - i) / (n - i);     }     cout << fixed << setprecision(10) << ans << endl;   }   int main() {     input();     pro();   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k;     cin >> n >> m >> k;     vector<int> v(n, 0);     for (int i = 0; i < n; i++) {       cin >> v[i];     }     m--;     int p = -1;     for (int i = m + 1; i < n; i++) {       if (v[i] != 0 && v[i] <= k) {         if (p == -1 || abs(m - i) < abs(m - p)) {           p = i;         }       }     }     for (int i = m - 1; i >= 0; i--) {       if (v[i] != 0 && v[i] <= k) {         if (p == -1 || abs(m - i) < abs(m - p)) {           p = i;         }       }     }     cout << abs(m - p) * 10 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a;   int gcd(int a, int b) {     if (b == 0) {       return a;     }     return gcd(b, a % b);   }   void solve() {     int temp, sum = 0, m, i;     for (i = 2; i < a; i++) {       temp = a;       while (temp) {         sum += temp % i;         temp = temp / i;       }     }     m = gcd(sum, a - 2);     printf( %d/%d n , sum / m, (a - 2) / m);   }   int main() {     while (scanf( %d , &a) == 1) {       solve();     }     fclose(stdin);     fclose(stdout);     return 0;   }