#include <bits/stdc++.h>   using namespace std;   struct Point {     int x;     int y;   };   struct Rect {     int left;     int top;     int right;     int bottom;   };   bool FindBoundary(const vector<Point> &pts, Rect *getRect) {     if (pts.empty()) {       return false;     }     auto it = pts.begin();     getRect->left = it->x;     getRect->right = it->x;     getRect->top = it->y;     getRect->bottom = it->y;     for (auto it = pts.begin(); it != pts.end(); ++it) {       if (getRect->left > it->x) {         getRect->left = it->x;       }       if (getRect->right < it->x) {         getRect->right = it->x;       }       if (getRect->top < it->y) {         getRect->top = it->y;       }       if (getRect->bottom > it->y) {         getRect->bottom = it->y;       }     }     return true;   }   bool IsSquare(const Rect *rect) {     return rect->right - rect->left == rect->top - rect->bottom;   }   bool CheckPoints(const vector<Point> &pts, const Rect *rect, int n) {     int left = 0, top = 0, right = 0, bottom = 0;     bool notAny = true;     for (auto it = pts.begin(); it != pts.end(); ++it) {       notAny = true;       if (it->x == rect->left) {         notAny = false;         ++left;       }       if (it->x == rect->right) {         notAny = false;         ++right;       }       if (it->y == rect->top) {         notAny = false;         ++top;       }       if (it->y == rect->bottom) {         notAny = false;         ++bottom;       }       if (notAny) {         return false;       }     }     if (left < n || right < n || top < n || bottom < n) {       return false;     }     return true;   }   int main(int argc, char *argv[]) {     int n = 0;     cin >> n;     int amount = 4 * n + 1;     vector<Point> points;     for (int i = 0; i < amount; ++i) {       Point pt;       cin >> pt.x >> pt.y;       points.push_back(pt);     }     bool found = false;     for (int i = 0; i < amount; ++i) {       vector<Point> sqPoints;       for (int j = 0; j < amount; ++j) {         if (j != i) {           sqPoints.push_back(points[j]);         }       }       Rect rect;       if (!FindBoundary(sqPoints, &rect)) {         continue;       }       if (!IsSquare(&rect)) {         continue;       }       if (!CheckPoints(sqPoints, &rect, n)) {         continue;       }       printf( %d %d , points[i].x, points[i].y);       break;     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;   typedef long long ll;   //#define rep(i,a,b) for(int i=a;i<b;i++);   void solve(){    ll n;    cin>>n;    string s;    cin>>s;    vector<ll>v;    for(int i=0;i<n;i++){     if (s[i]== * ){      v.push_back(i);     }    }    if (v.size()<=1 or v.size()==n){     cout<<0<<endl;    }    else{    ll mid=v.size()/2;    ll ans=0;    for(int i=0;i<v.size();i++){     ans+=abs(v[i]-v[mid]);    }           if (v.size()%2==0){     ll t1=v.size()/2;     ll t2=v.size()/2-1;        ll d1=t1*(t1+1)/2;     ll d2=t2*(t2+1)/2;     ans-=(d1+d2);    }       else{     ll t1=v.size()/2;     ll d1=t1*(t1+1)/2;     ans-=(2*d1);    }       cout<<ans<<endl;   }   }   int main(){    int t=1;    cin>>t;    while(t--)    solve();   }


#include <bits/stdc++.h>   using namespace std;   const double PI = 3.141592653589793;   const long long I_MAX = 1LL << 60;   long long mod = 1000000007;   const long long maxN = 2E5;   vector<long long> inv(maxN + 5, 1);   vector<long long> fac(maxN + 5, 1);   void ADD(long long &x, long long y) {     x += y;     if (x >= mod) x -= mod;     if (x < 0) x += mod;   }   void MUL(long long &x, long long y) {     x *= y;     x = (x % mod + mod) % mod;   }   long long mypow(long long b, long long e) {     long long ans = 1;     while (e) {       if (e & 1) MUL(ans, b);       MUL(b, b);       e >>= 1;     }     return ans;   }   long long modinv(long long n) { return mypow(n, mod - 2); }   void calcInv(long long n) {     for (int i = 2; i <= n; ++i) {       inv[i] = (mod - mod / i) * inv[mod % i] % mod;     }   }   void calcFac(long long n) {     for (int i = 2; i <= n; ++i) {       fac[i] = fac[i - 1] * i % mod;     }   }   long long cm(long long a, long long b) {     long long ans = 1;     if (a < b) return 0;     MUL(ans, inv[fac[a - b]]);     MUL(ans, inv[fac[b]]);     MUL(ans, fac[a]);     return ans;   }   long long Lucas(long long n, long long m) {     if (m == 0) return 1;     return cm(n % mod, m % mod) * Lucas(n / mod, m / mod) % mod;   }   long long doLucas(long long n, long long m) {     calcInv(mod);     calcFac(mod);     return Lucas(n, m);   }   const long long maxn = 1E5;   void precal() { return; }   void solve() {     long long n;     scanf( %I64d , &n);     int pln[n + 5][n + 5];     memset(pln, -1, sizeof(pln));     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j) {         if ((i & 1) and (j & 1))           pln[i][j] = 0;         else if ((i & 1))           pln[i][j] = 2;         else if ((j & 1))           pln[i][j] = 4;         else           pln[i][j] = 6;       }     pln[1][1] = 1;     pln[n][n] = 0;     int rep;     for (int i = 1; i <= n; i += 2) {       for (int j = 1; j <= n - 2 - 2 * (i == n); j += 2) {         printf( ? %d %d %d %d n , i, j, i, j + 2);         fflush(stdout);         scanf( %d , &rep);         if (rep == 1) {           pln[i][j + 2] = pln[i][j];         } else {           pln[i][j + 2] = pln[i][j] ^ 1;         }       }       if (i + 2 <= n) {         printf( ? %d %d %d %d n , i, 1, i + 2, 1);         fflush(stdout);         scanf( %d , &rep);         if (rep == 1) {           pln[i + 2][1] = pln[i][1];         } else {           pln[i + 2][1] = pln[i][1] ^ 1;         }       }     }     for (int i = 2; i <= n; i += 2) {       for (int j = 1; j <= n - 2; j += 2) {         printf( ? %d %d %d %d n , i, j, i, j + 2);         fflush(stdout);         scanf( %d , &rep);         if (rep == 1) {           pln[i][j + 2] = pln[i][j];         } else {           pln[i][j + 2] = pln[i][j] ^ 1;         }       }       if (i + 2 <= n) {         printf( ? %d %d %d %d n , i, 1, i + 2, 1);         fflush(stdout);         scanf( %d , &rep);         if (rep == 1) {           pln[i + 2][1] = pln[i][1];         } else {           pln[i + 2][1] = pln[i][1] ^ 1;         }       }     }     for (int i = 1; i <= n; i += 2) {       for (int j = 2; j <= n - 2; j += 2) {         printf( ? %d %d %d %d n , i, j, i, j + 2);         fflush(stdout);         scanf( %d , &rep);         if (rep == 1) {           pln[i][j + 2] = pln[i][j];         } else {           pln[i][j + 2] = pln[i][j] ^ 1;         }       }       if (i + 2 <= n) {         printf( ? %d %d %d %d n , i, 2, i + 2, 2);         fflush(stdout);         scanf( %d , &rep);         if (rep == 1) {           pln[i + 2][2] = pln[i][2];         } else {           pln[i + 2][2] = pln[i][2] ^ 1;         }       }     }     for (int i = 2; i <= n; i += 2) {       for (int j = 2; j <= n - 2; j += 2) {         printf( ? %d %d %d %d n , i, j, i, j + 2);         fflush(stdout);         scanf( %d , &rep);         if (rep == 1) {           pln[i][j + 2] = pln[i][j];         } else {           pln[i][j + 2] = pln[i][j] ^ 1;         }       }       if (i + 2 <= n) {         printf( ? %d %d %d %d n , i, 2, i + 2, 2);         fflush(stdout);         scanf( %d , &rep);         if (rep == 1) {           pln[i + 2][2] = pln[i][2];         } else {           pln[i + 2][2] = pln[i][2] ^ 1;         }       }     }     printf( ? %d %d %d %d n , 1, 1, 2, 2);     fflush(stdout);     scanf( %d , &rep);     if (rep == 1) {       for (int i = 2; i <= n; i += 2)         for (int j = 2; j <= n; j += 2) {           pln[i][j] ^= 7;         }     } else {       for (int i = 2; i <= n; i += 2)         for (int j = 2; j <= n; j += 2) {           pln[i][j] ^= 6;         }     }     printf( ? %d %d %d %d n , 1, 2, 2, 3);     fflush(stdout);     scanf( %d , &rep);     if (rep == 1) {       int x = pln[2][3] ^ 2;       for (int i = 2; i <= n; i += 2)         for (int j = 1; j <= n; j += 2) {           pln[i][j] ^= x;         }     } else {       int x = pln[2][3] ^ 3;       for (int i = 2; i <= n; i += 2)         for (int j = 1; j <= n; j += 2) {           pln[i][j] ^= x;         }     }     int px = n, py = n;     for (int i = 2; i <= n; ++i) {       for (int j = 3; j <= n; ++j) {         if (pln[i - 1][j - 2] == pln[i][j - 1] and pln[i][j - 2] == pln[i][j]) {           px = i;           py = j;         }         if (pln[i - 1][j - 2] != pln[i][j - 1] and pln[i][j - 2] != pln[i][j]) {           px = i;           py = j;         }         if (pln[i - 1][j - 2] == pln[i][j - 1] and             pln[i - 1][j - 1] == pln[i][j]) {           px = i;           py = j;         }         if (pln[i - 1][j - 2] != pln[i][j - 1] and             pln[i - 1][j - 1] != pln[i][j]) {           px = i;           py = j;         }         if (pln[i - 1][j - 2] == pln[i - 1][j] and pln[i][j - 2] == pln[i][j]) {           px = i;           py = j;         }         if (pln[i - 1][j - 2] != pln[i - 1][j] and pln[i][j - 2] != pln[i][j]) {           px = i;           py = j;         }       }     }     printf( ? %d %d %d %d n , px - 1, py - 2, px, py);     fflush(stdout);     scanf( %d , &rep);     if (px % 2 != py % 2) {       if (rep == 1) {         int x = pln[px][py] ^ pln[px - 1][py - 2];         for (int i = 2; i <= n; i += 2)           for (int j = 1; j <= n; j += 2) {             pln[i][j] ^= x;           }         for (int i = 1; i <= n; i += 2)           for (int j = 2; j <= n; j += 2) {             pln[i][j] ^= x;           }       } else {         int x = pln[px][py] ^ pln[px - 1][py - 2] ^ 1;         for (int i = 2; i <= n; i += 2)           for (int j = 1; j <= n; j += 2) {             pln[i][j] ^= x;           }         for (int i = 1; i <= n; i += 2)           for (int j = 2; j <= n; j += 2) {             pln[i][j] ^= x;           }       }     } else {       if (rep == 1) {         int x = pln[px][py] ^ pln[px - 1][py - 2];         for (int i = 2; i <= n; i += 2)           for (int j = 1; j <= n; j += 2) {             pln[i][j] ^= x;           }         for (int i = 1; i <= n; i += 2)           for (int j = 2; j <= n; j += 2) {             pln[i][j] ^= x;           }       } else {         int x = pln[px][py] ^ pln[px - 1][py - 2] ^ 1;         for (int i = 2; i <= n; i += 2)           for (int j = 1; j <= n; j += 2) {             pln[i][j] ^= x;           }         for (int i = 1; i <= n; i += 2)           for (int j = 2; j <= n; j += 2) {             pln[i][j] ^= x;           }       }     }     printf( ! n );     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= n; ++j) {         printf( %d , pln[i][j]);       }       printf(  n );     }     fflush(stdout);   }   int main() {     precal();     int t = 1;     for (int i = 1; i <= t; ++i) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int W, H, n;     scanf( %d%d%d , &W, &H, &n);     multiset<int> w, h;     w.insert(-W);     h.insert(-H);     set<int> wa, ha;     wa.insert(0);     wa.insert(W);     ha.insert(0);     ha.insert(H);     for (int i = 0; i < n; i++) {       char t;       int x;       scanf(  n%c%d , &t, &x);       if (t ==  H ) {         auto r = ha.lower_bound(x);         auto l = r;         l--;         h.erase(h.find((*l) - (*r)));         ha.insert(x);         h.insert((*l) - x);         h.insert(x - (*r));       } else if (t ==  V ) {         auto r = wa.lower_bound(x);         auto l = r;         l--;         w.erase(w.find((*l) - (*r)));         wa.insert(x);         w.insert((*l) - x);         w.insert(x - (*r));       }       printf( %lld n , 1ll * (*h.begin()) * (*w.begin()));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX = 1000009;   long long mod = 998244353;   long long n, kek[MAX];   vector<long long> child[MAX];   long long bin(long long num, long long power) {     if (power == 0)       return 1;     else {       long long an = (num) % mod;       if (power % 2 == 0) {         an = bin(num, power / 2);         long long an1 = an;         an *= an1;         an %= mod;         return an;       } else {         an *= bin(num, power - 1);         an %= mod;         return an;       }     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long q;     cin >> q;     while (q--) {       long long n, k;       cin >> n >> k;       if (n % k <= k / 2) {         cout << n <<   n ;       } else         cout << n - ((n % k) - k / 2) <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool chk(int n, int first) { return n & (1 << first); }   void set(int& n, int first) { n = n | (1 << first); }   void reset(int& n, int first) { n = n & ~(1 << first); }   const int mx = 600010;   bool f[mx];   int a[mx][3], _size;   char s[mx];   int new_node() {     ++_size;     for (int i = 0; i < 3; i++) a[_size][i] = 0;     f[_size] = false;     return _size;   }   void Insert(char str[]) {     int cur = 0, i = -1;     while (str[++i] !=   0 ) {       int& next = a[cur][str[i] -  a ];       if (next == 0) next = new_node();       cur = next;     }     f[cur] = true;   }   bool Search(int i, int cur, bool flag) {     if (s[i] ==   0 ) return f[cur] && flag;     if (flag) {       int next = a[cur][s[i] -  a ];       if (next == 0) return false;       return Search(i + 1, next, 1);     } else {       int next = a[cur][s[i] -  a ];       if (next > 0 && Search(i + 1, next, 0)) return true;       for (int j = 0; j < 3; j++) {         next = a[cur][j];         if (next == 0) continue;         if (s[i] == j +  a ) continue;         if (Search(i + 1, next, 1)) return true;       }       return false;     }   }   int main() {     int i, j, k, n, m;     while (scanf( %d%d , &n, &m) == 2) {       _size = -1;       new_node();       for (i = 0; i < n; i++) {         scanf( %s , s);         Insert(s);       }       for (i = 0; i < m; i++) {         scanf( %s , s);         if (Search(0, 0, 0))           printf( YES n );         else           printf( NO n );       }     }   }


#include <bits/stdc++.h>   using namespace std;   class Solution {    public:     long long minCost(int n, vector<int>& blocks, vector<int>& prices) {       int m = prices.size();       vector<int> pre(n, 0);       iota(pre.begin(), pre.end(), 0);       if (!blocks.empty() && blocks[0] == 0) {         return -1;       }       for (int i = 0, j = 0; i < n && j < blocks.size(); ++i) {         if (i == blocks[j]) {           pre[i] = pre[i - 1];           ++j;         }       }       int minL = 0;       for (int i = 0; i < blocks.size(); ++i) {         int cnt = 1;         while (i + 1 < blocks.size() && blocks[i + 1] == blocks[i] + 1) {           ++i;           ++cnt;         }         minL = max(minL, cnt);       }       long long res = LLONG_MAX;       int minPrice = INT_MAX;       for (int l = m; l > minL; --l) {         if (minPrice < prices[l - 1]) {           continue;         }         minPrice = prices[l - 1];         int cnt = 0;         int p = 0;         while (p < n) {           p += l;           ++cnt;           if (p < n && pre[p] != p) {             p = pre[p];           }         }         res = min(res, 1LL * cnt * prices[l - 1]);       }       return res == LLONG_MAX ? -1 : res;     }   };   int main(int argc, char** argv) {     int n, m, k;     scanf( %d%d%d , &n, &m, &k);     vector<int> blocks;     blocks.reserve(m);     for (int i = 0, b = 0; i < m; ++i) {       scanf( %d , &b);       blocks.push_back(b);     }     vector<int> prices;     prices.reserve(k);     for (int i = 0, p = 0; i < k; ++i) {       scanf( %d , &p);       prices.push_back(p);     }     Solution sol;     printf( %s n , to_string(sol.minCost(n, blocks, prices)).c_str());     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1000005];   int main() {     string s;     int p = 1, q = 1, ans = 0, ansCount = 1;     a[0] = -1;     cin >> s;     for (int i = 0, sz = s.length(); i < sz; i++) {       if (s[i] ==  ( ) {         a[p] = i;         p++;       } else {         p--;         if (p == 0) {           a[0] = i;           p = 1, q = 1;         } else {           int dis = i - a[p - 1];           if (dis > ans) {             ans = dis, ansCount = 1;           } else if (dis == ans)             ansCount++;         }       }     }     cout << ans <<     << ansCount;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, k;   vector<long long> arr;   void solve() {     cin >> n >> m >> k;     arr.resize(n);     for (long long i = 0; i < n; i++) cin >> arr[i];     vector<long long> b;     long long l = arr[n - 1] - arr[0];     ;     for (int i = 1; i < n; i++) {       b.push_back(arr[i] - arr[i - 1]);     }     sort(b.begin(), b.end());     long long i = b.size() - 1;     int ctr = k - 1;     for (; ctr > 0; i--, ctr--) {       l -= b[i];       l += 1;     }     l++;     cout << l << endl;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) {       solve();     }     return 0;   }


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


#include <bits/stdc++.h>   const int maxn = 3e5 + 5;   const long long inf = 1e18;   int ind[maxn], now, n;   using namespace std;   set<pair<int, int>> e[maxn];   vector<long long> answers;   bool vis[maxn];   long long dp[maxn][2];   struct node {     int u, val;     inline bool operator<(const node &b) const { return val > b.val; }   };   priority_queue<node> pq;   set<int> S;   bool cur[maxn];   multiset<long long> q[maxn];   long long val[maxn];   inline void dfs(int u, int fa) {     vis[u] = 1;     vector<long long> vec;     long long sum = 0;     for (auto x : e[u]) {       int v = x.first, w = x.second;       if (v == fa || cur[v]) continue;       dfs(v, u);       sum += dp[v][1];       vec.push_back(dp[v][0] - dp[v][1] + w);     }     int cnt = 0;     int now = ::now - (ind[u] - (int)(q[u].size()) - (int)(e[u].size()));     auto it = q[u].end();     while (1) {       if (it == q[u].begin()) break;       it--;       int x = *it;       if (cnt >= (int)(e[u].size()) || cnt >= now) break;       cnt++;       vec.push_back(x);     }     sort(vec.begin(), vec.end(), greater<long long>());     for (int i = 0; i <= now - 2; ++i)       if (vec[i] >= 0) dp[u][0] += vec[i];     dp[u][1] = dp[u][0];     if (now > 0 && vec[now - 1] >= 0) dp[u][1] += vec[now - 1];     dp[u][0] += sum, dp[u][1] += sum;   }   std::vector<long long> minimum_closure_costs(int N, std::vector<int> U,                                                std::vector<int> V,                                                std::vector<int> W) {     answers.resize(N);     n = N;     long long nowval = 0;     for (int i = 0; i <= N - 2; ++i) {       U[i]++, V[i]++;       e[U[i]].insert({V[i], W[i]});       e[V[i]].insert({U[i], W[i]});       ind[U[i]]++, ind[V[i]]++;       nowval += W[i];     }     for (int i = 1; i <= n; ++i) pq.push({i, ind[i]});     for (int i = 1; i <= n; ++i) S.insert(i);     int maxind = 0;     for (int i = 1; i <= n; ++i) maxind = max(maxind, ind[i]);     for (int i = 0; i <= n - 1; ++i) {       now = i;       if (now > maxind) break;       for (int j : S) {         dp[j][0] = dp[j][1] = 0, vis[j] = 0;         while ((int)(q[j].size()) > ind[j] - now && (int)(q[j].size())) {           val[j] -= *--q[j].end();           nowval -= *--q[j].end();           q[j].erase(--q[j].end());         }       }       vector<node> tmp;       while (!pq.empty()) {         auto x = pq.top();         if (x.val <= now) {           tmp.push_back(x);           cur[x.u] = 1;           for (auto it = e[x.u].begin(); it != e[x.u].end();) {             auto xx = *it;             it++;             if (!cur[xx.first]) {               e[x.u].erase(e[x.u].lower_bound({xx.first, 0}));               e[xx.first].erase(e[xx.first].lower_bound({x.u, 0}));               q[xx.first].insert(xx.second);               val[xx.first] += xx.second;             }           }         } else           break;         pq.pop();       }       for (int j : S) {         dp[j][0] = dp[j][1] = 0, vis[j] = 0;         while ((int)(q[j].size()) > ind[j] - now && (int)(q[j].size())) {           val[j] -= *--q[j].end();           nowval -= *--q[j].end();           q[j].erase(--q[j].end());         }       }       for (auto x : tmp) S.erase(x.u);       now = i;       for (int x : S)         if (!vis[x]) dfs(x, 0), answers[i] += max(dp[x][0], dp[x][1]);       answers[i] = nowval - answers[i];     }     return answers;   }   int main() {     int N;     assert(1 == scanf( %d , &N));     std::vector<int> U(N - 1), V(N - 1), W(N - 1);     for (int i = 0; i < N - 1; ++i) {       assert(3 == scanf( %d %d %d , &U[i], &V[i], &W[i]));       U[i]--, V[i]--;     }     std::vector<long long> closure_costs = minimum_closure_costs(N, U, V, W);     for (int i = 0; i < static_cast<int>(closure_costs.size()); ++i) {       if (i > 0) {         printf(   );       }       printf( %lld , closure_costs[i]);     }     printf(  n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-5;   const int inf = (1 << 31) - 1;   const int hinf = 1000000000;   const int mod = 1000000007;   const int N = 3030;   int dp[3030][3030];   int n;   int head[3030];   int weizhi;   struct node {     int u, v, w, next;   } point[3030 << 1];   void add(int u, int v, int w) {     point[weizhi].u = u;     point[weizhi].v = v;     point[weizhi].w = w;     point[weizhi].next = head[u];     head[u] = weizhi++;   }   void dfs(int now, int par, int dp[N]) {     dp[now] = 0;     for (int k = head[now]; k != -1; k = point[k].next) {       int i = point[k].v;       if (i == par) continue;       dfs(i, now, dp);       dp[now] += dp[i] + point[k].w;     }   }   void DFS(int now, int p, int a, int& ret) {     ret = min(ret, dp[now][now] - dp[now][a]);     for (int k = head[now]; k != -1; k = point[k].next) {       int i = point[k].v;       if (i == p) continue;       DFS(i, now, a, ret);     }   }   int main() {     scanf( %d , &n);     int a, b;     memset((head), -1, sizeof(head));     weizhi = 0;     for (int i = 1; i < n; i++) {       scanf( %d%d , &a, &b);       add(a, b, 0);       add(b, a, 1);     }     memset(dp, 0, sizeof(dp));     if (n == 1) {       cout << 0 << endl;       return 0;     }     for (int i = 1; i <= n; i++) {       dfs(i, -1, dp[i]);     }     int ans = inf;     for (int u = 1; u <= n; u++) {       for (int k = head[u]; k != -1; k = point[k].next) {         int i = point[k].v;         int w = point[k].w;         if (w) continue;         int ret1 = inf;         int ret2 = inf;         DFS(i, u, u, ret1);         DFS(u, i, i, ret2);         ans = min(ans, ret1 + ret2 - 1);       }     }     printf( %d n , ans);   }


#include <bits/stdc++.h>   using namespace std;   using namespace std::chrono;   template <typename A>   ostream &operator<<(ostream &cout, vector<A> const &v);   template <typename A, typename B>   ostream &operator<<(ostream &cout, pair<A, B> const &p) {     return cout <<  (  << p.first <<  ,   << p.second <<  ) ;   }   template <typename A>   ostream &operator<<(ostream &cout, vector<A> const &v) {     cout <<  [ ;     for (int i = 0; i < v.size(); i++) {       if (i) cout <<  ,  ;       cout << v[i];     }     return cout <<  ] ;   }   template <typename A, typename B>   istream &operator>>(istream &cin, pair<A, B> &p) {     cin >> p.first;     return cin >> p.second;   }   mt19937 rng(steady_clock::now().time_since_epoch().count());   void usaco(string filename) {     freopen((filename +  .in ).c_str(),  r , stdin);     freopen((filename +  .out ).c_str(),  w , stdout);   }   const long double pi = 3.14159265358979323846;   const long long mod = 1000000007;   long long n, m, k, q, Q, T, l, r, x, y, z;   long long c[1000005];   string second, t;   long long ans = 0;   int pt, a, b;   int query(int a, int b) {     cout <<  ?   << a <<     << b << endl;     cin >> x;     return x;   }   void getZero() {     if (pt < 0) {       cout <<  !   << a <<     << b << endl;       exit(0);     }     b ^= (1 << pt);     x = query(a, b);     if (x == 1)       a ^= (1 << pt);     else       b ^= (1 << pt);     --pt;     getZero();   }   int main() {     {       ios_base::sync_with_stdio(false);       cin.tie(NULL);       cout.tie(NULL);     };     pt = 29;     a = b = 0;     int cache = -2;     while (pt >= 0) {       int x, y;       if (cache == -2) {         x = query(a, b);       } else         x = cache;       if (x == 0) getZero();       a ^= (1 << pt);       b ^= (1 << pt);       y = query(a, b);       if (x == y)         cache = x;       else         cache = -2;       if (x == y) {         a ^= (1 << pt);         z = query(a, b);         if (z == 1)           a ^= (1 << pt);         else           b ^= (1 << pt);       } else {         if (x > y)           b ^= (1 << pt);         else           a ^= (1 << pt);       }       --pt;     }     cout <<  !   << a <<     << b << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long f[20][55][2600], c[20];   long long l[5005], tot = 0;   void Turn(long long k) {     memset(c, 0, sizeof(c));     while (k) c[++c[0]] = k % 10, k /= 10;     return;   }   int gcd(int x, int y) { return x % y ? gcd(y, x % y) : y; }   long long Search(int x, int lcm, int num, bool flag) {     if (!x && num % lcm == 0)       return 1;     else if (!x)       return 0;     if (!flag && ~f[x][l[lcm]][num]) return f[x][l[lcm]][num];     int k = flag ? c[x] : 9;     long long s = 0;     for (int i = 0; i <= k; i++) {       s += Search(x - 1, i ? lcm * i / gcd(lcm, i) : lcm, (num * 10 + i) % 2520,                   flag && i == k);     }     if (!flag) f[x][l[lcm]][num] = s;     return s;   }   long long Calc(long long k) {     Turn(k);     return Search(c[0], 1, 0, 1);   }   int main() {     for (int i = 1; i <= 2520; i++)       if (2520 % i == 0) l[i] = ++tot;     memset(f, -1, sizeof(f));     long long x, y;     int T;     cin >> T;     while (T--) {       cin >> x >> y;       cout << Calc(y) - Calc(x - 1) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 100, INF = 1e9;   struct edge {     long long to, cost, id;   };   vector<edge> g[N];   long long d[N], cost[N], path[N], used[N];   pair<long long, long long> from[N];   long long n, m;   void bfs() {     fill(d, d + n, INF);     d[0] = 0;     queue<long long> q;     q.push(0);     while ((long long)q.size()) {       long long v = q.front();       q.pop();       if (used[v]) continue;       used[v] = 1;       for (auto i : g[v]) {         long long u = i.to;         long long c = cost[v] + i.cost;         if (d[v] + 1 < d[u] || (d[v] + 1 == d[u] && c < cost[u])) {           d[u] = d[v] + 1;           from[u] = {v, i.id};           cost[u] = c;           q.push(u);         }       }     }     long long v = n - 1;     while (v != 0) {       long long id = from[v].second;       path[id] = 1;       v = from[v].first;     }   }   signed main() {     ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n >> m;     vector<pair<pair<long long, long long>, long long>> ed(m);     for (long long i = 0; i < m; i++) {       long long a, b, c;       cin >> a >> b >> c;       a--;       b--;       ed[i] = {{a, b}, c};       c ^= 1;       g[a].push_back({b, c, i});       g[b].push_back({a, c, i});     }     bfs();     long long ans = 0;     for (long long i = 0; i < m; i++) {       if (path[i] && ed[i].second == 0 || (!path[i] && ed[i].second == 1)) ans++;     }     cout << ans <<   n ;     for (long long i = 0; i < m; i++) {       if (path[i] && ed[i].second == 0 || (!path[i] && ed[i].second == 1))         cout << ed[i].first.first + 1 <<     << ed[i].first.second + 1 <<                  << (ed[i].second ^ 1) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000010;   const int mod = 1e9 + 7;   int t[N], dp[2][3][3];   int main() {     int n, K;     scanf( %d%d , &n, &K);     for (int i = 1; i <= n; i++) {       int x;       scanf( %d , &x);       t[x]++;     }     int cur = 1, pre = 0;     for (int i = 1; i <= K; i++) {       memset(dp[cur], 0, sizeof(dp[cur]));       for (int j = 0; j < 3; j++)         for (int k = 0; k < 3; k++)           for (int l = 0; l < 3; l++)             if (j + k + l <= t[i])               dp[cur][k][l] =                   max(dp[cur][k][l], dp[pre][j][k] + l + (t[i] - j - k - l) / 3);       swap(cur, pre);     }     printf( %d n , dp[pre][0][0]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int MOD = 1e9 + 7;   long long int INF = 1e18;   long long int sub(long long int a, long long int b, long long int p = MOD) {     return ((a % p) - (b % p) + p) % p;   }   long long int mult(long long int a, long long int b, long long int p = MOD) {     return ((a % p) * (b % p)) % p;   }   long long int add(long long int a, long long int b, long long int p = MOD) {     return (a % p + b % p) % p;   }   long long int fpow(long long int n, long long int k, long long int p = MOD) {     long long int r = 1;     while (k > 0) {       if (k & 1) r = r * n % p;       n = n * n % p;       k = k >> 1;     }     return r;   }   long long int inv(long long int a, long long int p = MOD) {     return fpow(a, p - 2, p);   }   long long int power(long long int a, long long int b) {     long long int ans = 1;     long long int i;     for (i = 1; i < b + 1; i++) {       ans *= a;     }     return ans;   }   long long int fdiv(long long int a, long long int b, long long int p = MOD) {     long long int yinv = inv(b);     long long int ans = (a * yinv) % p;     return ans;   }   unsigned long long int I1018 = 1000000000000000000;   const unsigned long long int I10e5 = 100000;   const long long int I10e6 = 1e6;   template <typename T>   istream &operator>>(istream &in, vector<T> &a) {     for (auto &item : a) {       in >> item;     }     return in;   }   template <typename T, typename U>   ostream &operator<<(ostream &out, pair<T, U> &a) {     cout << a.first <<     << a.second;     return out;   }   template <typename T, typename U>   istream &operator>>(istream &out, pair<T, U> &a) {     cin >> a.first >> a.second;     return out;   }   template <typename T, typename U>   ostream &operator<<(ostream &out, map<T, U> &a) {     for (auto &item : a) {       out << item << endl;     }     return out;   }   template <typename T>   ostream &operator<<(ostream &out, vector<T> &a) {     for (auto &item : a) {       out << item <<    ;     }     return out;   }   template <typename T>   ostream &operator<<(ostream &out, vector<vector<T>> &a) {     for (auto &item : a) {       out << item <<   n ;     }     return out;   }   std::vector<bool> is_prime;   std::vector<long long int> primes;   void sieve(long long int n) {     is_prime.resize(n + 2, true);     primes.clear();     long long int p;     for (p = 2; p * p <= n; p++) {       if (is_prime[p]) {         long long int i;         for (i = p * p; i <= n; i += p) {           is_prime[i] = false;         }       }     }     is_prime[0] = is_prime[1] = false;     long long int i;     for (i = 2; i <= n; i++) {       if (is_prime[i]) {         primes.emplace_back(i);       }     }   }   map<long long int, long long int> prime_factors(long long int n) {     map<long long int, long long int> s;     long long int i;     long long int tc = 0;     while (n % 2 == 0) {       tc++;       n /= 2;     }     if (tc > 0) {       s[2] = tc;     }     for (i = 3; i <= sqrt(n); i += 2) {       tc = 0;       while (n % i == 0) {         tc++;         n /= i;       }       if (tc > 0) {         s[i] = tc;       }     }     if (n > 2) {       s[n] += 1;     }     return s;   }   std::vector<long long int> fact_vec;   void fact_fun(long long int n) {     fact_vec.resize(n + 10);     long long int i;     fact_vec[0] = 1;     for (i = 1; i <= n + 2; i++) {       fact_vec[i] = (fact_vec[i - 1] * i) % MOD;     }   }   long long int npr(long long int n, long long int r) {     long long int ans = fact_vec[n];     ans /= fact_vec[n - r];     return ans;   }   long long int ncr(long long int n, long long int r) {     long long int ans = fact_vec[n];     ans = fdiv(ans, fact_vec[n - r]);     ans = fdiv(ans, fact_vec[r]);     return ans;   }   long long int nc2(long long int n) { return (n * (n - 1)) / 2; }   int main() {     long long int i, j;     ios::sync_with_stdio(false);     cin.tie(0);     string s;     cin >> s;     long long int n = s.length();     std::vector<long long int> cnt(n + 10, 0);     for (i = 0; i < n; i++) {       cnt[i + 1] = cnt[i] + ((s[i] >=  A  && s[i] <=  Z ) ? 1 : 0);     }     long long int ans = INF;     for (i = 0; i < n + 1; i++) {       long long int left = i - cnt[i];       long long int right = cnt[n] - cnt[i];       ans = min(ans, left + right);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000005;   const long long MOD = 1000000007;   long long k, w, ans;   long long fac[MAXN];   long long inv[MAXN];   long long power(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) (res *= a) %= MOD;       (a *= a) %= MOD;       b >>= 1;     }     return res;   }   void init() {     fac[0] = 1;     for (int i = 1; i <= k; i++) fac[i] = fac[i - 1] * i % MOD;     inv[k] = power(fac[k], MOD - 2);     for (int i = k; i >= 1; i--) inv[i - 1] = inv[i] * i % MOD;   }   int main() {     cin >> k >> w;     init();     for (int i = 1; i <= k; i++) {       if (i <= w - 1)         (ans += i * fac[k] % MOD * fac[k] % MOD * power(k, w - i - 1) % MOD *                 inv[k - i] % MOD * inv[k - i]) %= MOD;       else         (ans += w * fac[k] % MOD * fac[k - i + w - 1] % MOD * inv[k - i] % MOD *                 inv[k - i] % MOD) %= MOD;       if (i < k) {         if (i + 1 <= w - 1)           (ans += i * fac[k] % MOD * fac[k] % MOD * power(k, w - i - 2) % MOD *                   inv[k - i] % MOD * inv[k - i - 1]) %= MOD;         else           (ans += (w - 1) * fac[k] % MOD * fac[k - i + w - 2] % MOD * inv[k - i] %                   MOD * inv[k - i - 1]) %= MOD;       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long mod = 1000000007;   const double sn = 1e-9;   const long double PI = 3.141592653589793238L;   int n, c, d;   int mc[100005];   int md[100005];   vector<pair<int, int>> ac;   vector<pair<int, int>> ad;   bool cmp(pair<int, int> p1, pair<int, int> p2) { return p1.first > p2.first; }   int check(int l, int r, vector<pair<int, int>> &arr, int v) {     while (l != r) {       int mid = (l + r) / 2;       if (arr[mid].first <= v)         r = mid;       else         l = mid + 1;     }     return l;   }   int main() {     scanf( %d%d%d , &n, &c, &d);     for (int i = 0; i < n; i++) {       int b, p;       char t[5];       scanf( %d%d%s , &b, &p, t);       if (t[0] ==  C ) {         ac.push_back(make_pair(p, b));       } else {         ad.push_back(make_pair(p, b));       }     }     sort(ac.begin(), ac.end(), cmp);     sort(ad.begin(), ad.end(), cmp);     int sz1 = ac.size(), sz2 = ad.size();     if (sz1 != 0) {       mc[sz1 - 1] = ac[sz1 - 1].second;       for (int i = sz1 - 2; i >= 0; i--) mc[i] = max(ac[i].second, mc[i + 1]);     }     if (sz2 != 0) {       md[sz2 - 1] = ad[sz2 - 1].second;       for (int i = sz2 - 2; i >= 0; i--) md[i] = max(ad[i].second, md[i + 1]);     }     int abc = check(0, sz1, ac, c), abd = check(0, sz2, ad, d);     int mh = INT_MIN;     if (abc != sz1 && abd != sz2) {       mh = max(mh, mc[abc] + md[abd]);     }     for (int i = 0; i < sz1; i++) {       if (ac[i].first > c) continue;       int v = check(i + 1, sz1, ac, c - ac[i].first);       if (v != sz1) {         mh = max(mh, mc[v] + ac[i].second);       }     }     for (int i = 0; i < sz2; i++) {       if (ad[i].first > d) continue;       int v = check(i + 1, sz2, ad, d - ad[i].first);       if (v != sz2) {         mh = max(mh, md[v] + ad[i].second);       }     }     printf( %d n , max(mh, 0));     return 0;   }


#include <bits/stdc++.h>      using namespace std;      #define FOR(n) for(int i = 0; i < int(n); ++i)   #define RFOR(n) for(int i = n; ~i; --i)      int gcd(int a, int b) {       if(b == 0)           return a;       return gcd(b, a % b);   }      void solve() {       int n;       cin >> n;       map<pair<int, int>, int> mp;       long long ans = 0;       FOR(n) {           int x, y, u, v;           cin >> x >> y >> u >> v;           x -= u;           y -= v;           int g = gcd(abs(x), abs(y));           x /= g;           y /= g;           mp[{x, y}]++;           ans += mp[{-x, -y}];       }       cout << ans <<   n ;   }      int main(int argc, char* argv[]) {       ios::sync_with_stdio(false);       cin.tie(nullptr);              int testCase = 1;       cin >> testCase;       for(int t = 0; t < testCase; ++t)           solve();          return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > edges(2e5 + 5);   bool vis[200005];   int lev[200005];   int n, m, a, b, mx;   void bfs(int node) {     int level = 0;     deque<pair<int, int> > dq;     dq.push_back({level, node});     vis[node] = 1;     while (!dq.empty()) {       node = dq.front().second, level = dq.front().first;       lev[node] = level;       dq.pop_front();       for (auto i : edges[node])         if (!vis[i]) dq.push_back({level + 1, i}), vis[node] = 1;     }     memset(vis, 0, sizeof vis), a = 0;   }   void dfs(int node) {     if (a >= lev[node]) return;     vis[node] = 1;     mx = max(mx, lev[node]);     for (auto i : edges[node])       if (!vis[i]) a++, dfs(i), a--;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     cin >> n >> m;     for (int i = 1; i < n; i++)       cin >> a >> b, edges[a].push_back(b), edges[b].push_back(a);     bfs(1);     dfs(m);     cout << mx * 2 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int d, n, m;   int cntl[100011], cntr[100011], cntt[100011], cntb[100011];   struct Sofa {     int x1, y1, x2, y2;   } sofa[100011];   int main() {     scanf( %d , &d);     scanf( %d %d , &n, &m);     for (int i = 0; i < d; i++) {       int x1, y1, x2, y2;       scanf( %d %d %d %d , &x1, &y1, &x2, &y2);       cntl[min(x1, x2)]++;       cntr[max(x1, x2)]++;       cntt[min(y1, y2)]++;       cntb[max(y1, y2)]++;       sofa[i].x1 = x1;       sofa[i].y1 = y1;       sofa[i].x2 = x2;       sofa[i].y2 = y2;     }     for (int i = 1; i <= n; i++) cntl[i] += cntl[i - 1];     for (int i = n; i >= 0; i--) cntr[i] += cntr[i + 1];     for (int i = 1; i <= m; i++) cntt[i] += cntt[i - 1];     for (int i = m; i >= 0; i--) cntb[i] += cntb[i + 1];     int tl, tr, tt, tb;     scanf( %d %d %d %d , &tl, &tr, &tt, &tb);     for (int i = 0; i < d; i++) {       int tmpl = cntl[max(sofa[i].x1, sofa[i].x2) - 1],           tmpr = cntr[min(sofa[i].x1, sofa[i].x2) + 1],           tmpt = cntt[max(sofa[i].y1, sofa[i].y2) - 1],           tmpb = cntb[min(sofa[i].y1, sofa[i].y2) + 1];       if (max(sofa[i].x1, sofa[i].x2) != min(sofa[i].x1, sofa[i].x2)) {         tmpl--;         tmpr--;       }       if (max(sofa[i].y1, sofa[i].y2) != min(sofa[i].y1, sofa[i].y2)) {         tmpt--;         tmpb--;       }       if (tmpl == tl && tmpr == tr && tmpt == tt && tmpb == tb) {         cout << i + 1;         return 0;       }     }     cout << -1;     return 0;   }


#include <bits/stdc++.h>   int main() {     long long int n;     long long int x;     long long int y;     long long int number;     scanf( %I64d %I64d %I64d , &n, &x, &y);     number = y - (n - 1);     while (true) {       if (number <= 0) {         printf( -1 n );         break;       }       if (number * number + n - 1 >= x) {         printf( %I64d n , number);         for (int i = 0; i < n - 1; i++) {           printf( 1 n );         }         break;       }       number--;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int c = 0, h = 0;     while (n > c + h) {       h++;       c += h;       n -= c;     }     cout << h << endl;   }


#include <bits/stdc++.h>   using namespace std;   constexpr long long ROWS = 2;   constexpr long long NMAX = 1e5;   long long h[ROWS][NMAX];   long long dp[ROWS][NMAX];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long COLS;     cin >> COLS;     for (long long i = 0; i < ROWS; ++i)       for (long long j = 0; j < COLS; ++j) cin >> h[i][j];     for (long long j = COLS - 1; j >= 0; --j)       for (long long i = ROWS - 1; i >= 0; --i) {         if (j == COLS - 1)           dp[i][j] = h[i][j];         else if (j == COLS - 2)           dp[i][j] = h[i][j] + h[i == 0 ? 1 : 0][j + 1];         else           dp[i][j] =               h[i][j] + max(dp[i == 0 ? 1 : 0][j + 1], dp[i == 0 ? 1 : 0][j + 2]);       }     cout << max(dp[0][0], dp[1][0]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long ans = 0;   long long mod = 1e9 + 7;   int cnt[100005], Tcnt[100005], arr[100005];   vector<int> pri;   void era() {     pri.push_back(2);     for (int i = 3; i <= 100000; i += 2) {       if (!arr[i])         for (long long j = (long long)i * i; j <= 100000; j += i) {           arr[j] = 1;         }     }     for (int i = 3; i <= 100000; i += 2) {       if (arr[i] == 0) pri.push_back(i);     }   }   int GGG(int p) {     int ret = 0;     for (int i = 0; i < pri.size(); i++) {       if (pri[i] > p) break;       int cnt = 0;       while (p % pri[i] == 0) {         p /= pri[i];         ++cnt;       }       if (cnt >= 2) return -1;       if (cnt != 0) ret++;     }     return ret;   }   long long po(int k) {     if (k == 0) return 1;     if (k == 1) return 2;     long long ret = po(k / 2);     ret = ret * ret % mod;     if (k & 1) ret = ret * 2 % mod;     return ret;   }   int main() {     era();     int n;     scanf( %d , &n);     for (int t, i = 1; i <= n; i++) {       scanf( %d , &t);       cnt[t]++;     }     for (int i = 2; i <= 100000; i++) {       for (int j = i; j <= 100000; j += i) {         Tcnt[i] += cnt[j];       }     }     ans += po(n) - 1;     for (int i = 2; i <= 100000; i++) {       if (Tcnt[i] == 0) continue;       int Gcnt = GGG(i);       if (Gcnt == -1) continue;       if (Gcnt % 2 == 0) {         ans += po(Tcnt[i]) - 1;         ans %= mod;       } else {         ans -= po(Tcnt[i]) - 1;         while (ans < 0) ans += mod;       }     }     printf( %I64d , ans);   }


#include <bits/stdc++.h>   const long long MaxN = 205;   const long long lim = 1e18 + 5;   template <class t>   inline void read(t &s) {     s = 0;     register long long f = 1;     register char c = getchar();     while (!isdigit(c)) {       if (c ==  - ) f = -1;       c = getchar();     }     while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();     s *= f;     return;   }   template <class t, class... A>   inline void read(t &x, A &...a) {     read(x);     read(a...);   }   template <class t>   inline void write(t x) {     if (x < 0) putchar( - ), x = -x;     long long buf[21], top = 0;     while (x) buf[++top] = x % 10, x /= 10;     if (!top) buf[++top] = 0;     while (top) putchar(buf[top--] ^  0 );     return;   }   inline void setIn(std::string s) {     freopen(s.c_str(),  r , stdin);     return;   }   inline void setOut(std::string s) {     freopen(s.c_str(),  w , stdout);     return;   }   inline void setIO(std::string s =   ) {     setIn(s +  .in );     setOut(s +  .out );     return;   }   template <class t>   inline bool checkmin(t &x, t y) {     if (x > y) {       x = y;       return 1;     }     return 0;   }   template <class t>   inline bool checkmax(t &x, t y) {     if (x < y) {       x = y;       return 1;     }     return 0;   }   inline long long lowbit(long long x) { return x & (-x); }   long long a[MaxN][MaxN], p[MaxN], t[MaxN], n, m, K, q;   long long b[MaxN], f[MaxN][MaxN];   inline void add(long long &x, long long y) { (x += y) >= lim && (x = lim); }   inline long long calc() {     std::memset(f, 0, sizeof f);     f[0][0] = 1;     for (long long i = 1; i <= q; ++i) {       if (b[i] >= 0)         for (long long j = 1; j <= q; ++j) add(f[i][j], f[i - 1][j - 1]);       if (b[i] <= 0)         for (long long j = 0; j < q; ++j) add(f[i][j], f[i - 1][j + 1]);     }     return f[q][0];   }   signed main(void) {     read(n, m, K);     for (long long i = 1; i <= n; ++i)       for (long long j = 1; j <= m; ++j) read(a[i][j]);     {       std::vector<std::pair<long long, long long> > dr;       for (long long i = 1; i <= n; ++i)         for (long long j = 1; j <= m; ++j)           dr.push_back(std::make_pair(a[i][j], i + j - 1));       std::sort((dr).begin(), (dr).end());       register long long cnt = 0;       for (auto i : dr)         if (!p[i.second]) p[i.second] = ++cnt;       for (long long i = 1; i <= cnt; ++i) t[p[i]] = i;       for (long long i = 1; i <= cnt; ++i) p[i] = t[i];     }     q = n + m - 1;     register long long sum = 0;     for (long long i = 1; i <= q; ++i) {       b[p[i]] = 1;       register long long t = calc();       if (sum + t >= K) continue;       b[p[i]] = -1;       sum += t;     }     for (long long i = 1; i <= n; ++i, std::puts(  ))       for (long long j = 1; j <= m; ++j)         std::putchar(b[i + j - 1] > 0 ?  (  :  ) );     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:16777216 )   using namespace std;   const double eps = 1e-9;   const double pi = acos(-1.0);   const int mod = (int)1e9 + 7;   const int maxn = (int)1e5 + 10;   int pp[4 * maxn];   int a[maxn], c[6][6], pw[maxn][6], pref[maxn][6];   struct node {     int cnt;     int sum[6];     node() {       cnt = 0;       memset(sum, 0, sizeof(sum));     }     node(int cntt) {       cnt = cntt;       memset(sum, 0, sizeof(sum));     }   } t[4 * maxn];   void push(int idx, int l, int m, int r) {     if (pp[idx] != -1) {       pp[2 * idx] = pp[idx];       pp[2 * idx + 1] = pp[idx];       for (int i = 0; i <= 5; i++) {         t[2 * idx].sum[i] = (long long)pref[m - l + 1][i] * pp[idx] % mod;         t[2 * idx + 1].sum[i] = (long long)pref[r - m][i] * pp[idx] % mod;       }       pp[idx] = -1;     }   }   void make(int idx) {     t[idx].cnt = t[2 * idx].cnt + t[2 * idx + 1].cnt;     for (int i = 0; i <= 5; i++) {       t[idx].sum[i] = t[2 * idx].sum[i];       for (int j = i; j >= 0; j--) {         t[idx].sum[i] +=             (long long)((long long)t[2 * idx + 1].sum[i - j] * c[i][j] % mod) *             pw[t[2 * idx].cnt][j] % mod;         t[idx].sum[i] %= mod;       }     }   }   void build(int l, int r, int idx) {     if (l == r) {       t[idx].cnt = 1;       for (int i = 0; i <= 5; i++) {         t[idx].sum[i] = a[l];       }     } else {       int m = (l + r) >> 1;       build(l, m, 2 * idx);       build(m + 1, r, 2 * idx + 1);       make(idx);     }   }   void put(int l, int r, int a, int b, int val, int idx) {     if (b < l || r < a) return;     if (a <= l && r <= b) {       pp[idx] = val;       for (int i = 0; i <= 5; i++) {         t[idx].sum[i] = (long long)pref[r - l + 1][i] * val % mod;       }     } else {       int m = (l + r) >> 1;       push(idx, l, m, r);       put(l, m, a, b, val, 2 * idx);       put(m + 1, r, a, b, val, 2 * idx + 1);       make(idx);     }   }   node make(node lf, node rg) {     if (lf.cnt == 0) return rg;     if (rg.cnt == 0) return lf;     node res = lf;     res.cnt += rg.cnt;     for (int i = 0; i <= 5; i++) {       for (int j = i; j >= 0; j--) {         res.sum[i] += (long long)((long long)rg.sum[i - j] * c[i][j] % mod) *                       pw[lf.cnt][j] % mod;         res.sum[i] %= mod;       }     }     return res;   }   node get(int l, int r, int a, int b, int idx) {     if (b < l || r < a) return node(0);     if (a <= l && r <= b) return t[idx];     int m = (l + r) >> 1;     push(idx, l, m, r);     node lf = get(l, m, a, b, 2 * idx);     node rg = get(m + 1, r, a, b, 2 * idx + 1);     return make(lf, rg);   }   int main() {     memset(pp, -1, sizeof(pp));     for (int i = 1; i < maxn; i++) {       pw[i][0] = 1;       for (int j = 1; j <= 5; j++) {         pw[i][j] = (long long)pw[i][j - 1] * i % mod;       }       for (int j = 0; j <= 5; j++) {         pref[i][j] = pref[i - 1][j] + pw[i][j];         pref[i][j] %= mod;       }     }     for (int i = 0; i <= 5; i++) {       c[i][0] = 1;     }     for (int i = 1; i <= 5; i++) {       for (int j = 1; j <= i; j++) {         c[i][j] = c[i - 1][j] + c[i - 1][j - 1];       }     }     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);     }     build(0, n - 1, 1);     int l, r, x;     char s[11];     for (int i = 0; i < m; i++) {       scanf( %s%d%d%d , s, &l, &r, &x);       --l;       --r;       if (s[0] ==  = ) {         put(0, n - 1, l, r, x, 1);       } else {         printf( %d n , get(0, n - 1, l, r, 1).sum[x]);       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 6e5 + 20;   int main() {     int n, filas, columnas;     cin >> n;     while (n--) {       cin >> filas >> columnas;       int arr[300][300] = {0};       bool ya = false;       for (int i = 0; i < filas; i++) {         for (int j = 0; j < columnas; j++) {           cin >> arr[i][j];           if (arr[i][j] > 4)             ya = true;           else if ((i == 0 && j == 0) || (i == 0 && j == columnas - 1) ||                    (i == filas - 1 && j == 0) ||                    (i == filas - 1 && j == columnas - 1)) {             if (arr[i][j] > 2) ya = true;           } else if (i == 0 || i == filas - 1 || j == 0 || j == columnas - 1) {             if (arr[i][j] > 3) ya = true;           }         }       }       if (ya)         cout <<  NO  << endl;       else {         cout <<  YES  << endl;         for (int i = 0; i < filas; i++) {           for (int j = 0; j < columnas; j++) {             if ((i == 0 && j == 0) || (i == 0 && j == columnas - 1) ||                 (i == filas - 1 && j == 0) ||                 (i == filas - 1 && j == columnas - 1))               cout << 2 <<    ;             else if (i == 0 || i == filas - 1 || j == 0 || j == columnas - 1)               cout << 3 <<    ;             else               cout << 4 <<    ;           }           cout << endl;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   char str[100010];   int f[100010][26 + 2], pre[100010][26], suf[100010][26], g[100010], n;   int ans[26 + 2];   int dfs(int l, int r, int c, int fl) {     if (l > r) return 0;     if (ans[c] != -1) return ans[c];     int u = 0;     for (int i = 0; i < 26; i++) {       int L = suf[l][i], R = pre[r][i];       if (R < l) continue;       int s = g[L] ^ g[R];       if (!fl)         s ^= f[L - 1][c] ^ dfs(R + 1, r, i, 0);       else         s ^= dfs(l, L - 1, i, 1) ^ f[r][i];       u |= 1 << s;     }     return ans[c] = __builtin_ctz(~u);   }   int main() {     scanf( %s , str + 1);     n = strlen(str + 1);     for (int i = 1; i <= n; i++) {       for (int j = 0; j < 26; j++) pre[i][j] = pre[i - 1][j];       pre[i][str[i] -  a ] = i;     }     for (int i = 0; i < 26; i++) suf[n + 1][i] = n + 1;     for (int i = n; i >= 1; i--) {       for (int j = 0; j < 26; j++) suf[i][j] = suf[i + 1][j];       suf[i][str[i] -  a ] = i;     }     for (int i = 1; i <= n; i++) {       int c = str[i] -  a ;       g[i] = f[i - 1][c] ^ g[pre[i - 1][c]];       memset(ans, -1, sizeof(ans));       for (int j = 0; j < 26; j++) f[i][j] = dfs(pre[i][j] + 1, i, j, 0);     }     int m;     scanf( %d , &m);     while (m-- > 0) {       int l, r;       scanf( %d%d , &l, &r);       memset(ans, -1, sizeof(ans));       if (dfs(l, r, 26, 1))         puts( Alice );       else         puts( Bob );     }   }


#include <bits/stdc++.h>   namespace ZDY {   #pragma GCC optimize(3)   template <class T>   __inline__ __attribute__((always_inline)) T ABS(T x) {     return x > 0 ? x : -x;   }   template <class T>   __inline__ __attribute__((always_inline)) T MAX(T x, T y) {     return x > y ? x : y;   }   template <class T>   __inline__ __attribute__((always_inline)) T MIN(T x, T y) {     return x < y ? x : y;   }   template <class T>   __inline__ __attribute__((always_inline)) T GCD(T x, T y) {     return y ? GCD(y, x % y) : x;   }   template <class T>   __inline__ __attribute__((always_inline)) void SWAP(T &x, T &y) {     T t = x;     x = y;     y = t;   }   }  // namespace ZDY   using namespace ZDY;   using namespace std;   namespace IO {   const int str = 1 << 20;   static char in_buf[str], *in_s, *in_t;   bool __ = 0;   __inline__ __attribute__((always_inline)) char gc() {     return (in_s == in_t) &&                (in_t = (in_s = in_buf) + fread(in_buf, 1, str, stdin)),            in_s == in_t ? EOF : *in_s++;   }   __inline__ __attribute__((always_inline)) void in(string &ch) {     ch.clear();     if (__) return;     char c;     while ((c = gc()) != EOF && isspace(c))       ;     if (c == EOF) {       __ = 1;       return;     }     ch += c;     while ((c = gc()) != EOF && !isspace(c)) ch += c;     if (c == EOF) __ = 1;   }   __inline__ __attribute__((always_inline)) void in(char &ch) {     if (__) return;     char c;     while ((c = gc()) != EOF && isspace(c))       ;     if (c == EOF)       __ = 1;     else       ch = c;   }   __inline__ __attribute__((always_inline)) void in(char *ch) {     *ch =   0 ;     if (__) return;     char c;     while ((c = gc()) != EOF && isspace(c))       ;     if (c == EOF) {       __ = 1;       return;     }     *ch = c;     ch++;     while ((c = gc()) != EOF && !isspace(c)) *ch = c, ch++;     if (c == EOF) __ = 1;     *ch =   0 ;   }   template <typename T>   __inline__ __attribute__((always_inline)) void in(T &x) {     if (__) return;     char c = gc();     bool f = 0;     while (c != EOF && (c <  0  || c >  9 )) f ^= (c ==  - ), c = gc();     if (c == EOF) {       __ = 1;       return;     }     x = 0;     while (c != EOF &&  0  <= c && c <=  9 ) x = x * 10 + c - 48, c = gc();     if (c == EOF) __ = 1;     if (f) x = -x;   }   template <typename T, typename... arr>   __inline__ __attribute__((always_inline)) void in(T &x, arr &...y) {     in(x), in(y...);   }   const char ln =   n ;   static char out_buf[str], *out_s = out_buf, *out_t = out_buf + str;   __inline__ __attribute__((always_inline)) void flush() {     fwrite(out_buf, 1, out_s - out_buf, stdout);     out_s = out_buf;   }   __inline__ __attribute__((always_inline)) void pt(char c) {     (out_s == out_t) ? (fwrite(out_s = out_buf, 1, str, stdout), *out_s++ = c)                      : (*out_s++ = c);   }   __inline__ __attribute__((always_inline)) void out(const char *s) {     while (*s) pt(*s++);   }   __inline__ __attribute__((always_inline)) void out(char *s) {     while (*s) pt(*s++);   }   __inline__ __attribute__((always_inline)) void out(char c) { pt(c); }   __inline__ __attribute__((always_inline)) void out(string s) {     for (int i = 0; s[i]; i++) pt(s[i]);   }   template <typename T>   __inline__ __attribute__((always_inline)) void out(T x) {     if (!x) {       pt( 0 );       return;     }     if (x < 0) pt( - ), x = -x;     char a[50], t = 0;     while (x) a[t++] = x % 10, x /= 10;     while (t--) pt(a[t] +  0 );   }   template <typename T, typename... arr>   __inline__ __attribute__((always_inline)) void out(T x, arr &...y) {     out(x), out(y...);   }   }  // namespace IO   using namespace IO;   int n, head[1000011], cnt = 0, a[1000011];   struct edge {     int to, nxt;   } e[1000011 * 2];   __inline__ __attribute__((always_inline)) void add(int x, int y) {     e[++cnt].to = y;     e[cnt].nxt = head[x];     head[x] = cnt;   }   void dfs(int x, int d) {     a[x] = d;     for (int i(head[x]), to; to = e[i].to, i; i = e[i].nxt)       if (!a[to]) dfs(to, MIN(d, to));   }   int main() {     int q, opt, x, y, la = 0, tt = 2122219134;     in(n, q);     for (int i(1); i <= n - 1; ++i) in(x, y), add(x, y), add(y, x);     in(y, x);     x = x % n + 1;     dfs(x, x);     while (--q) {       in(opt, x);       x = (la + x) % n + 1;       if (opt == 1)         tt = MIN(tt, a[x]);       else         out(la = MIN(tt, a[x]), ln);     }     flush();   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   char dis[2000010];   int a[2000010];   int main() {     int t;     scanf( %d , &t);     for (int i = 0; i < 200010; i += 3) {       dis[i] =  R , dis[i + 1] =  G , dis[i + 2] =  B ;     }     while (t--) {       int n, k;       scanf( %d %d , &n, &k);       string s;       cin >> s;       int minn = INF;       int x = 0;       while (x < 3) {         for (int i = 0; i < n; i++) {           if (s[i] != dis[i + x])             a[i] = 1;           else             a[i] = 0;           if (i != 0) a[i] = a[i - 1] + a[i];         }         x++;         minn = min(minn, a[k - 1]);         for (int i = k; i < n; i++) {           minn = min(minn, a[i] - a[i - k]);         }       }       printf( %d n , minn);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long a, b, c, d, e, f, g, h, i, j, k, l, n, v, t;     cin >> n >> k;     long long m[k];     for (i = 0; i < k; i++) cin >> m[i];     if (n % 2 == 0) {       a = m[k - 1];       if (a & 1)         cout <<  odd n ;       else         cout <<  even n ;     } else {       a = 0;       for (i = 0; i < k; i++) {         if (m[i] & 1) a++;       }       if (a & 1)         cout <<  odd n ;       else         cout <<  even n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long n;     cin >> n;     int s = 0, a;     a = n / 100;     n = n % 100;     s += a;     if (n != 0) {       a = n / 20;       n %= 20;       s += a;       if (n != 0) {         a = n / 10;         n %= 10;         s += a;         if (n != 0) {           a = n / 5;           n %= 5;           s += a;           if (n != 0) s += n;         }       }     }     cout << s;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 9;   int pre[MAX], suf[MAX], a[MAX];   long long ans = 0, cnt = 0;   int main() {     int n;     cin >> n;     for (int i = 0; i < n; i++)       cin >> a[i], pre[i] = (i > 0 ? pre[i - 1] + a[i] : a[i]);     int sum = pre[n - 1] / 3;     if (pre[n - 1] % 3 != 0) return cout << 0, 0;     for (int i = n - 1; i >= 0; i--) suf[i] = suf[i + 1] + a[i];     if (pre[0] == sum) cnt++;     for (int i = 2; i < n; i++) {       if (suf[i] == sum) ans += cnt;       if (pre[i - 1] == sum) cnt++;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const long long INF = 1e18;   const double EPS = 1e-6;   const int MAX_N = 500 + 5;   int n;   int arr[MAX_N];   int memo[MAX_N][MAX_N];   int memo2[MAX_N][MAX_N];   int dp(int l, int r) {     if (l == r) return memo[l][r] = arr[l];     if (memo[l][r] != -1) return memo[l][r];     for (int i = l; i < r; ++i) {       if (dp(l, i) == dp(i + 1, r) && dp(l, i) > 0)         return memo[l][r] = dp(l, i) + 1;     }     return memo[l][r] = -10;   }   int solve(int l, int r) {     if (memo[l][r] != -10) return 1;     if (memo2[l][r] != -1) return memo2[l][r];     int mini = n;     for (int i = l; i < r; ++i) {       mini = min(mini, solve(l, i) + solve(i + 1, r));     }     return memo2[l][r] = mini;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     memset(memo, -1, sizeof memo);     memset(memo2, -1, sizeof memo2);     for (int i = 0; i < n; ++i) {       cin >> arr[i];     }     dp(0, n - 1);     cout << solve(0, n - 1) <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline void gn(T &first) {     char c, sg = 0;     while (c = getchar(), (c >  9  || c <  0 ) && c !=  - )       ;     for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ;          c = getchar())       first = (first << 1) + (first << 3) + c -  0 ;     if (sg) first = -first;   }   template <class T1, class T2>   inline void gn(T1 &x1, T2 &x2) {     gn(x1), gn(x2);   }   pair<int, int> edge[500010];   int w[500010];   int n, m, Q;   int id[500010];   int col[1010];   int root[1010];   int rev[500010];   inline int find_root(int u) {     if (root[u] == u) return u;     int first = find_root(root[u]);     col[u] ^= col[root[u]];     return root[u] = first;   }   int main() {     gn(n, m);     gn(Q);     for (int i = 1; i <= m; i++) {       gn(edge[i].first, edge[i].second);       gn(w[i]);     }     for (int i = 1; i <= m; i++) id[i] = i;     sort(id + 1, id + m + 1, [&](int u, int v) { return w[u] > w[v]; });     while (Q--) {       int L, R;       gn(L, R);       for (int i = 1; i <= n; i++) {         col[i] = 0;         root[i] = i;       }       int ans = -1;       for (int i = 1; i <= m; i++) {         if (id[i] > R || id[i] < L) continue;         int u = edge[id[i]].first, v = edge[id[i]].second;         if (find_root(u) == find_root(v)) {           if (col[u] == col[v]) {             ans = w[id[i]];             break;           }           continue;         }         col[root[u]] = col[u] ^ col[v] ^ 1;         root[root[u]] = root[v];       }       printf( %d n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 1ll << 32;   int n, m, s, e;   string ans =   ;   bool ch(int l, int r, int idx, bool b) {     bool re = 1;     if (l <= idx && idx <= r) re = 0;     if (b) {       if (l <= idx + 1 && idx + 1 <= r) re = 0;     } else if (l <= idx - 1 && idx - 1 <= r)       re = 0;     return re;   }   int main() {     scanf( %d %d %d %d , &n, &m, &s, &e);     map<int, pair<int, int>> mp;     for (int i = 0; i < m; i++) {       int a;       scanf( %d , &a);       scanf( %d %d , &mp[a].first, &mp[a].second);     }     int j = 1;     if (s < e) {       for (int i = s; i < e; i++) {         if (mp[j].first) {           if (ch(mp[j].first, mp[j].second, i, 1))             ans +=  R ;           else {             ans +=  X ;             i--;           }           j++;         } else {           while (!mp[j].first && i < e) {             ans +=  R ;             i++;             j++;           }           i--;         }       }     } else {       for (int i = s; i > e; i--) {         if (mp[j].first) {           if (ch(mp[j].first, mp[j].second, i, 0))             ans +=  L ;           else {             ans +=  X ;             i++;           }           j++;         } else {           while (!mp[j].first && i > e) {             ans +=  L ;             i--;             j++;           }           i++;         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int a[n];       vector<int> vec;       for (int i = 0; i < n; i++) {         cin >> a[i];       };       int l[n];       map<int, int> mpp;       for (int i = 0; i < n; i++) {         cin >> l[i];         if (l[i] == 0) {           mpp[i] = 0;           vec.push_back(a[i]);         } else {           mpp[i] = 1;         }       }       int j = 0;       sort(vec.rbegin(), vec.rend());       for (int i = 0; i < n; i++) {         if (mpp[i] == 0) {           cout << vec[j] <<    ;           j++;         } else {           cout << a[i] <<    ;         }       }       cout << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }   const int INF = (((1 << 30) - 1) << 1) + 1;   const int nINF = 1 << 31;   pair<long long, long long> egcd(long long a, long long b,                                   pair<long long, long long> x = {1, 0},                                   pair<long long, long long> y = {0, 1}) {     return (b == 0)                ? x                : egcd(b, a % b, y,                       {x.first - a / b * y.first, x.second - a / b * y.second});   }   long long modInv(long long a, long long n) {     return ((egcd(a, n).first % n) + n) % n;   }   const long long M = 998244353;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   long long mult(long long a, long long b) { return (a * b) % M; }   long long add(long long a, long long b) { return (a + b) % M; }   struct frac {     long long n, d;     void simp() {       long long g = gcd(n, d);       n /= g;       d /= g;     }     frac(long long in_n, long long in_d) {       n = in_n, d = in_d;       simp();     }     frac operator+(frac& oth) {       frac ret(n * oth.d + d * oth.n, d * oth.d);       ret.simp();       return ret;     }   };   const int N = 2e5 + 10;   int n, p[N], m = 0, l[N], have[N];   long long bit[N];   long long getSum(int i) {     long long sum = 0;     ++i;     while (i > 0) {       sum += bit[i];       i -= i & (-i);     }     return sum;   }   void updateBIT(int i, long long val) {     ++i;     while (i <= n + 5) {       bit[i] += val;       i += i & (-i);     }   }   signed main() {     ios::sync_with_stdio(false);     cin >> n;     for (int i = 0; i < (n); i++) {       cin >> p[i];       if (p[i] != -1)         have[p[i]] = 1;       else         ++m;     }     for (int i = (1); i <= (n); i++) {       l[i] = l[i - 1];       if (!have[i]) ++l[i];     }     long long k = 0;     frac einv(0, 1);     frac lm(0, 1);     long long lmi = 0;     long long inv = 0;     long long ans = 0;     for (int i = 0; i < (n); i++) {       int x = p[i];       if (x != -1) {         int above = getSum(n) - getSum(x);         inv += above;         ans = add(ans, above);         updateBIT(x, 1);         if (m) {           frac ex((m - l[x]) * k, m);           frac cex(l[x], m);           einv = einv + ex;           lm = lm + cex;           lmi = add(lmi, mult(cex.n, modInv(cex.d, M)));           ans = add(ans, mult(ex.n, modInv(ex.d, M)));         }       } else {         einv = einv + lm;         ans = add(ans, lmi);         frac ex(k, 2);         einv = einv + ex;         ans = add(ans, mult(ex.n, modInv(ex.d, M)));         ++k;       }     }     frac kinv(inv, 1);     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 7;   int a[maxn];   int n, s;   int main() {     scanf( %d%d , &n, &s);     if (s >= 2 * n) {       s -= 2 * n;       puts( YES );       printf( %d , 2 + s);       for (int i = 1; i < n; ++i) printf(  %d , 2);       puts(  );       puts( 1 );     } else       puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, s = 0;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; ++i) {       cin >> a[i];       s += a[i];     }     sort(begin(a), end(a));     if (s % 2 == 0 && 2 * a[n - 1] <= s)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T x, T y) {     if (x < y) swap(x, y);     while (y > 0) {       T f = x % y;       x = y;       y = f;     }     return x;   }   struct Node {     Node *l, *r;     long long a, b;     Node() : l(NULL), r(NULL), a(0), b(0) {}   };   Node* makeTree(int fr, int to) {     Node* node = new Node();     if (fr == to) return node;     int m = (fr + to) / 2;     node->l = makeTree(fr, m);     node->r = makeTree(m + 1, to);     return node;   }   Node* update(Node* _node, int fr, int to, int x1, int x2, long long a,                long long b) {     if (x2 < fr || x1 > to) return _node;     Node* node = new Node();     *node = *_node;     if (x1 <= fr && to <= x2) {       node->a += a;       node->b += b;       return node;     }     int m = (fr + to) / 2;     node->l = update(node->l, fr, m, x1, x2, a, b);     node->r = update(node->r, m + 1, to, x1, x2, a, b);     return node;   }   long long query(Node* node, int fr, int to, long long x) {     long long res = node->a * x + node->b;     if (fr == to) return res;     int m = (fr + to) / 2;     if (x <= m)       return res + query(node->l, fr, m, x);     else       return res + query(node->r, m + 1, to, x);   }   Node* froot[75007];   const int maxx = 200002;   int main(int argc, char* argv[]) {     int n;     std::cin.sync_with_stdio(false);     std::cin.tie(nullptr);     cin >> n;     {       froot[0] = makeTree(0, maxx);       for (int i = 1; i <= n; i++) {         int x1, x2, y1, a, b, y2;         cin >> x1 >> x2 >> y1 >> a >> b >> y2;         froot[i] = update(froot[i - 1], 0, maxx, 0, x1, 0, y1);         froot[i] = update(froot[i], 0, maxx, x1 + 1, x2, a, b);         froot[i] = update(froot[i], 0, maxx, x2 + 1, maxx, 0, y2);       }       int m;       cin >> m;       long long last = 0;       while (m--) {         int l, r, x;         cin >> l >> r >> x;         last = (last + x) % 1000000000ll;         last =             query(froot[r], 0, maxx, last) - query(froot[l - 1], 0, maxx, last);         cout << last << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long GCD(long long x, long long y) {     while (y != 0) {       long long t = x % y;       x = y;       y = t;     }     return x;   }   long long n, a, b;   long long cs;   bool calc() {     scanf( %lld %lld , &a, &b);     long long g = GCD(a, b);     long long p = a / g;     long long q = b / g;     long long pq = p * q;     bool ret = (g % pq == 0);     if (ret) {       long long r = g / pq;       long long i = 1;       while (i * i * i < r) {         i++;       }       if (i * i * i == r) return true;     }     return false;   }   int main() {     scanf( %lld , &n);     while (cs < n) {       cs++;       bool ans = calc();       if (ans)         printf( Yes n );       else         printf( No n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int main(void) {     int n;     cin >> n >> s;     int l = 0, r = 0;     for (auto &c : s)       if (c ==  L )         ++l;       else         ++r;     cout << r + l + 1 << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, res = 1;   int main() {     cin >> a >> b;     if (b - (a + 1) > 20) {       cout << 0;       return 0;     }     for (long long i = a + 1; i <= b; i++) res = (res * (i % 10)) % 10;     cout << res;   }


#include <bits/stdc++.h>   using namespace std;   void Solve() {     long long int n, q;     cin >> n >> q;     long long int a[n + 1], cnt[n + 1];     for (auto i = 1; i < n + 1; i++) cin >> a[i], cnt[i] = 0;     while (q--) {       long long int l, r;       cin >> l >> r;       cnt[l]++;       if (r != n) cnt[r + 1]--;     }     for (auto i = 2; i < n + 1; i++) cnt[i] += cnt[i - 1];     sort(cnt + 1, cnt + n + 1);     sort(a + 1, a + n + 1);     long long int ans = 0;     for (auto i = 1; i < n + 1; i++) ans += cnt[i] * a[i];     cout << ans <<   n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long int T = 1;     while (T--) Solve();     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   long long rand_seed() {     long long a = rng();     return a;   }   int t;   int n;   string s;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> t;     for (; t; --t) {       cin >> n;       cin >> s;       bool ok = 0;       for (int i = 0; i + 10 < n; ++i)         if (s[i] ==  8 ) ok = 1;       if (ok)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[100], b[45], c[100];   int main() {     int x;     cin >> x;     int p = 0, ans = 0, q = 0, f = 0;     while (x) {       c[p++] = x % 2;       if (x % 2 == 0) f = 1;       x = x / 2;     }     int pp = 0;     for (int h = p - 1; h >= 0; h--) {       a[pp++] = c[h];     }     if (f) {       while (ans + 1) {         int flag = 0, flag1 = 0, flag2 = 0;         for (int i = 0; i < p; i++) {           if (flag) {             if (a[i] == 1)               a[i] = 0;             else if (a[i] == 0)               a[i] = 1;           } else if (a[i] == 0) {             ans++;             a[i] = 1;             flag = 1;             b[q++] = p - i;           }         }         for (int i = 0; i < p; i++) {           if (a[i] == 0) {             break;           }           if (i == p - 1) {             flag1 = 1;           }         }         if (flag1) {           break;         }         ans++;         for (int j = p - 1; j >= 0; j--) {           a[j]++;           if (a[j] == 2)             a[j] = 0;           else if (a[j] = 1)             break;         }         for (int i = 0; i < p; i++) {           if (a[i] == 0) {             break;           }           if (i == p - 1) {             flag2 = 1;           }         }         if (flag2) {           break;         }       }       cout << ans << endl;       for (int k = 0; k < q; k++) {         if (k == 0)           cout << b[k];         else           cout <<     << b[k];       }       cout << endl;     } else {       cout << 0 << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   long long f[300005][2], g[300005];   vector<int> vec[300005];   long long powi(long long a, long long b) {     long long p;     if (!b) return 1;     p = powi(a, b / 2);     p = (p * p) % 998244353;     if (b % 2) p = (p * a) % 998244353;     return p;   }   long long mmi(long long x) { return powi(x, 998244353 - 2); }   void dfs(int s, int par = 0) {     for (int v : vec[s]) {       if (v != par) dfs(v, s);     }     f[s][0] = 1;     for (int v : vec[s]) {       if (v == par) continue;       f[s][0] *= f[v][0];       f[s][0] %= 998244353;     }     long long temp = 1;     for (int v : vec[s]) {       if (v == par) continue;       temp *= f[v][1] + f[v][0];       temp %= 998244353;     }     for (int v : vec[s]) {       if (v == par) continue;       long long temp2 = temp;       temp2 = (temp2 * mmi(f[v][1] + f[v][0])) % 998244353;       temp2 = (temp2 * g[v]) % 998244353;       f[s][1] += temp2;       f[s][1] %= 998244353;     }     f[s][0] += f[s][1];     f[s][0] %= 998244353;     g[s] = 1;     for (int v : vec[s]) {       if (v == par) continue;       g[s] *= f[v][0] + f[v][1];       g[s] %= 998244353;     }     return;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int u, v;       scanf( %d %d , &u, &v);       vec[u].push_back(v);       vec[v].push_back(u);     }     dfs(1);     long long ans = f[1][0];     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math )   const int maxn = 1e6 + 50;   int seg[4 * maxn], ans[maxn], last_pos[maxn], next_pos[maxn];   int n, m;   vector<int> v, get_val, partial_xor;   vector<pair<pair<int, int>, int> > ques;   void update(int val, int l, int r = n) {     for (l += n, r += n; l < r; l >>= 1, r >>= 1) {       if (l & 1) seg[l++] ^= val;       if (r & 1) seg[--r] ^= val;     }   }   int get_xor(int idx) {     int res = 0;     for (idx += n; idx; idx >>= 1) res ^= seg[idx];     return res;   }   void sweep(int pos) {     int val = get_val[v[pos]];     int nxt = next_pos[pos];     update(val, pos, nxt);   }   void input() {     cin >> n;     for (int i = 0; i < n; i++) {       int b;       cin >> b;       if (i == 0)         partial_xor.push_back(b);       else         partial_xor.push_back(partial_xor[i - 1] ^ b);       get_val.push_back(b);       v.push_back(b);       last_pos[i] = n;     }     cin >> m;     for (int i = 0; i < m; i++) {       int l, r;       cin >> l >> r;       l--, r--;       ques.push_back({{l, r}, i});     }   }   void map_it() {     sort(get_val.begin(), get_val.end());     vector<int>::iterator ip;     ip = std::unique(get_val.begin(), get_val.end());     get_val.resize(std::distance(get_val.begin(), ip));     for (int i = v.size() - 1; i > -1; i--) {       v[i] = lower_bound(get_val.begin(), get_val.end(), v[i]) - get_val.begin();       next_pos[i] = last_pos[v[i]];       last_pos[v[i]] = i;     }     for (int i = 0; i < get_val.size(); i++) update(get_val[i], last_pos[i]);   }   void answer_ques(int i) {     pair<int, int> p = ques[i].first;     ans[ques[i].second] = get_xor(p.second) ^ partial_xor[p.first] ^                           partial_xor[p.second] ^ get_val[v[p.first]];   }   void calc() {     int go_pos = 0;     int i = 0;     sort(ques.begin(), ques.end());     while (go_pos < ques.size()) {       while (go_pos < ques.size() and i == ques[go_pos].first.first)         answer_ques(go_pos++);       if (go_pos == ques.size()) break;       while (i < ques[go_pos].first.first) sweep(i++);     }   }   void ansing() {     for (int i = 0; i < m; i++) cout << ans[i] <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     input();     map_it();     calc();     ansing();   }


#include <bits/stdc++.h>   using namespace std;   long long pow(long long b, long long e, long long m) {     long long t = 1;     for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0;     return t;   }   template <class T>   inline bool chkmin(T &a, T b) {     return a > b ? a = b, true : false;   }   template <class T>   inline bool chkmax(T &a, T b) {     return a < b ? a = b, true : false;   }   template <class T>   inline T sqr(T x) {     return x * x;   }   template <typename T>   T gcd(T x, T y) {     for (T t; x; t = x, x = y % x, y = t)       ;     return y;   }   template <class edge>   struct Graph {     vector<vector<edge> > adj;     Graph(int n) {       adj.clear();       adj.resize(n + 5);     }     Graph() { adj.clear(); }     void resize(int n) { adj.resize(n + 5); }     void add(int s, edge e) { adj[s].push_back(e); }     void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); }     vector<edge> &operator[](int t) { return adj[t]; }   };   int main() {     ios_base::sync_with_stdio(false);     int n, m;     cin >> n >> m;     if (m == 3) {       if (n == 3) {         cout <<  0 0  << endl <<  1 1  << endl <<  1 0  << endl;       } else if (n == 4) {         cout <<  0 0  << endl              <<  0 1  << endl              <<  -1 -1  << endl              <<  1 -1  << endl;       } else {         cout << -1 << endl;       }       return 0;     }     const int X = 1e7;     for (int i = 1; i <= m; ++i) {       cout << i <<     << +(X + i * i) << endl;     }     for (int i = 1; i <= n - m; ++i) {       cout << i <<     << -(X + i * i) << endl;     }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC target( avx,avx2,fma )   #pragma GCC optimization( unroll-loops )   using namespace std;   template <typename T>   bool mycomp(T x, T y) {     return (x == y);   }   bool paircomp(const pair<long long int, long long int> &x,                 const pair<long long int, long long int> &y) {     return x.second < y.second;   }   long long int f[6][6];   void solve() {     long long int m;     cin >> m;     for (long long int i = 0; i < m; i++) {       long long int x, y;       cin >> x >> y;       f[x][y] = 1;       f[y][x] = 1;     }     for (long long int i = 1; i <= 5; i++) {       for (long long int j = i + 1; j <= 5; j++) {         for (long long int k = j + 1; k <= 5; k++) {           if (f[i][j] && f[i][k] && f[j][k]) {             cout <<  WIN ;             return;           }           if (!f[i][j] && !f[i][k] && !f[j][k]) {             cout <<  WIN n ;             return;           }         }       }     }     cout <<  FAIL ;     return;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int t = 1;     while (t--) {       solve();       cout << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   using vc = vector<T>;   using pii = pair<long long, long long>;   void xmax(long long& a, long long b) { a = max(a, b); }   void xmin(long long& a, long long b) { a = min(a, b); }   void print(vc<long long>& a, string second) {     cerr << second <<   :  ;     for (long long i = 0; i < a.size(); i++) {       cerr << a[i] <<    ;     }     cerr << endl;   }   long long dist(pii a, pii b) {     return (a.first - b.first) * (a.first - b.first) +            (a.second - b.second) * (a.second - b.second);   }   long long find_min(vc<pii> p) {     if (p.size() == 1) return 1e18;     if (p.size() == 2) {       return dist(p[0], p[1]);     }     long long n = p.size();     long long mid = (p[0].first + p[n - 1].first) / 2;     vc<pii> ri;     vc<pii> le;     for (long long i = 0; i < n; i++) {       if (p[i].first <= mid)         le.push_back(p[i]);       else         ri.push_back(p[i]);     }     long long mini = 1e18;     xmin(mini, find_min(le));     xmin(mini, find_min(ri));     vc<pii> cut;     long double delta = sqrt(mini) + 1;     for (long long i = 0; i < le.size(); i++) {       if (abs(mid - le[i].first) <= delta)         cut.push_back({le[i].second, le[i].first});     }     for (long long i = 0; i < ri.size(); i++) {       if (abs(mid - ri[i].first) <= delta)         cut.push_back({ri[i].second, ri[i].first});     }     sort(cut.begin(), cut.end());     for (long long i = 0; i < cut.size(); i++) {       for (long long j = 1; j < 20 && i + j < cut.size(); j++) {         xmin(mini, dist(cut[i], cut[i + j]));       }     }     return mini;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long n;     cin >> n;     vc<long long> a(n);     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     vc<long long> p(n);     for (long long i = 1; i < n; i++) {       p[i] = p[i - 1] + a[i];     }     vc<pair<long long, long long> > st(n);     for (long long i = 0; i < n; i++) {       st[i] = {i, p[i]};     }     long long res = find_min(st);     cout << res <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   inline void sfr(int *a) {     char c = 0;     while (c < 33) c = fgetc(stdin);     *a = 0;     while (c > 33) {       *a = *a * 10 + c -  0 ;       c = fgetc(stdin);     }   }   inline void sfp(int a) {     char c[1000];     sprintf(c,  %d , a);     puts(c);   }   int main() {     int n;     cin >> n;     ;     int m;     cin >> m;     ;     int A[n][m];     getchar();     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         char c =   n ;         while (c ==   n ) c = getchar();         A[i][j] = (c ==  1 );       }     }     int B[n];     int mx = 0;     for (int i = n - 1; i >= 0; i--) B[i] = 0;     for (int i = m - 1; i >= 0; i--) {       vector<int> D;       for (int j = 0; j < n; j++) {         if (A[j][i]) {           B[j] = B[j] + 1;           D.push_back(B[j]);         } else {           B[j] = 0;           D.push_back(0);         }       }       sort(D.begin(), D.end());       for (int j = 0; j < n; j++) mx = max(mx, D[j] * (n - j));     }     cout << mx << endl;   }


#include <bits/stdc++.h>   using namespace std;   int n, dir[200005];   long long cnt, le;   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &dir[i]);       if (dir[i] == 0) le++;     }     for (int i = 0; i < n; i++) {       if (dir[i] == 1)         cnt += le;       else         le--;     }     printf( %I64d , cnt);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int maxn = 2e5 + 6;   const long long int inf = 2e18 + 1;   long long int a[maxn];   int main() {     long long int n, k;     cin >> n >> k;     long long int next[n];     for (long long int i = 0; i < n; i++) {       cin >> a[i];     }     next[n - 1] = n;     for (long long int i = n - 2; i >= 0; i--) {       if (a[i + 1] != 1) {         next[i] = i + 1;       } else {         next[i] = next[i + 1];       }     }     long long int ans = 0;     for (long long int i = 0; i < n; i++) {       if (k == 1) {         ans++;       }       long long int r = i;       long long int mul = a[i];       long long int sum = a[i];       while (next[r] != n && a[next[r]] <= inf / mul) {         mul = mul * a[next[r]];         sum = sum + next[r] - r - 1 + a[next[r]];         r = next[r];         long long int check = -1;         if (mul % k == 0) {           check = mul / k;         }         long long int store = next[r] - r - 1;         if (check >= sum && check <= sum + store) {           ans = ans + 1;         }       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long T, W, B, H, P;   long long fpb(long long A, long long B) {     if (A == 0) {       return B;     }     return fpb(B % A, A);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> T >> W >> B;     H = W / fpb(W, B);     if (T / B < H) {       P = min(W, B) - 1 - max((long long)0, min(W, B) - 1 - T);     } else {       H *= B;       P = (T / H + 1) * min(W, B) - 1 - max((long long)0, min(W, B) - 1 - T % H);     }     H = fpb(P, T);     cout << P / H <<  /  << T / H <<   n ;   }


#include <bits/stdc++.h>   const int N = 209, MOD = 1000000007;   long long f[N][N], area[N][N];   struct point {     long long x, y;     point(const long long _x = 0, const long long _y = 0) : x(_x), y(_y) {}   } p[N];   int n;   inline int sign(long long x) { return x < 0 ? -1 : x > 0; }   inline long long cpr(const point &a, const point &b, const point &c) {     return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);   }   inline bool cross(point &u, point &v, point &a, point &b) {     if (std::max(u.x, v.x) < std::min(a.x, b.x) ||         std::min(u.x, v.x) > std::max(a.x, b.x))       return false;     if (std::max(u.y, v.y) < std::min(a.x, b.y) ||         std::min(u.y, v.y) > std::max(a.y, b.y))       return false;     if (sign(cpr(u, v, a)) * sign(cpr(u, v, b)) <= 0 &&         sign(cpr(a, b, u)) * sign(cpr(a, b, v)) <= 0)       return true;     return false;   }   bool check(int i, int j) {     if (i + 1 == j || i == 1 && j == n) return true;     if (sign(cpr(p[i], p[j], p[i + 1])) * sign(cpr(p[i], p[j], p[i - 1])) > 0)       return false;     for (int k = 1; k <= n; ++k)       if (k != i && k % n + 1 != i && k != j && k % n + 1 != j) {         if (cross(p[i], p[j], p[k], p[k + 1])) return false;       } else if (!cpr(p[i], p[j], p[(k == i || k == j) ? k + 1 : k]))         return false;     return true;   }   long long F(int i, int j) {     if (f[i][j] != -1) return f[i][j];     if (i + 1 == j) return 1;     f[i][j] = 0;     for (int k = i + 1; k < j; ++k)       if (cpr(p[i], p[j], p[k]) > 0) f[i][j] += F(i, k) * F(k, j) % MOD;     f[i][j] %= MOD;     return f[i][j];   }   int main() {     memset(f, -1, sizeof f);     scanf( %d , &n);     long long S = 0;     for (int i = 1; i <= n; ++i) scanf( %I64d%I64d , &p[i].x, &p[i].y);     p[n + 1] = p[1];     p[0] = p[n];     for (int i = 1; i <= n; ++i) S += cpr(point(0, 0), p[i], p[i + 1]);     if (S > 0) std::reverse(p + 1, p + 1 + n);     p[n + 1] = p[1];     p[0] = p[n];     printf( %I64d n , F(1, n));   }


#include <bits/stdc++.h>   int main(void) {     int n;     scanf( %d , &n);     while (1) {       if (n % 2 == 1) {         int atk = n - 1;         n = n - atk;       } else if (n % 2 == 0) {         printf( Mahmoud n );         break;       }       if (n % 2 == 1) {         printf( Ehab n );         break;       } else {         int atk2 = n - 1;         n = n - atk2;       }     }   }


#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long power(long long first, unsigned long long second,                   unsigned long long m) {     if (second == 0) return 1;     long long p = power(first, second / 2, m) % m;     p = (p * p) % m;     return (second % 2 == 0) ? p : (first * p) % m;   }   long long modInverse(long long a, long long m) {     { return power(a, m - 2, m); }   }   const long long maxn = 500004;   void __print(long long first) { cerr << first; }   void __print(float first) { cerr << first; }   void __print(double first) { cerr << first; }   void __print(long double first) { cerr << first; }   void __print(char first) { cerr <<      << first <<     ; }   void __print(const char *first) { cerr <<     << first <<    ; }   void __print(const string &first) { cerr <<     << first <<    ; }   void __print(bool first) { cerr << (first ?  true  :  false ); }   template <typename T, typename V>   void __print(const pair<T, V> &first) {     cerr <<  { ;     __print(first.fi);     cerr <<  , ;     __print(first.se);     cerr <<  } ;   }   template <typename T>   void __print(const T &first) {     long long f = 0;     cerr <<  { ;     for (auto &i : first) cerr << (f++ ?  ,  :   ), __print(i);     cerr <<  } ;   }   void _print() { cerr <<  ] n ; }   template <typename T, typename... V>   void _print(T t, V... v) {     __print(t);     if (sizeof...(v)) cerr <<  ,  ;     _print(v...);   }   struct BIT {     long long N;     vector<long long> bit;     void init(long long n) {       N = n;       bit.assign(n + 4, 0);     }     void update(long long idx, long long val) {       idx++;       while (idx <= N) {         bit[idx] += val;         idx += idx & -idx;       }     }     long long query(long long idx) {       long long ans = 0;       idx++;       while (idx > 0) {         ans += bit[idx];         idx -= idx & -idx;       }       return ans;     }     long long search(long long val) {       long long curSum = 0, pos = 0;       for (long long i = log2(N) + 1; i >= 0; i--) {         if (pos + (1 << i) < N && curSum + bit[pos + (1 << i)] < val) {           curSum += bit[pos + (1 << i)];           pos += (1 << i);         }       }       return pos + 1;     }     void updateMax(long long idx, long long val) {       while (idx <= N) {         bit[idx] = max(bit[idx], val);         idx += idx & -idx;       }     }     long long prefMax(long long idx) {       long long ans = -2e9;       while (idx > 0) {         ans = max(ans, bit[idx]);         idx -= idx & -idx;       }       return ans;     }   };   void solve() {     BIT bit;     long long n;     cin >> n;     bit.init(n + 4);     long long a[n], b[n], c[n + 3];     memset(c, 0, sizeof c);     memset(a, 0, sizeof a);     memset(b, 0, sizeof b);     for (long long i = 0; i < n; i++) {       cin >> a[i];       ;       bit.update(i, 1);     }     for (long long i = 0; i < n; i++) {       c[n - i - 1] = bit.query(a[i]) - 1;       bit.update(a[i], -1);     }     for (long long i = 0; i < n; i++) {       cin >> b[i];       bit.update(i, 1);     }     for (long long i = 0; i < n; i++) {       c[n - i - 1] += bit.query(b[i]) - 1;       bit.update(b[i], -1);     }     for (long long i = 1; i <= n + 2; i++) {       c[i + 1] += c[i] / (i + 1);       c[i] %= i + 1;     }     c[n] = 0;     for (long long i = 0; i < n; i++) bit.update(i, 1);     for (long long i = 1; i <= n; i++) {       long long first = c[n - i];       first++;       long long l = 0, r = n - 1;       long long wh;       while (l <= r) {         long long mid = l + r >> 1;         if (bit.query(mid) >= first) {           wh = mid;           r = mid - 1;         } else {           l = mid + 1;         }       }       cout << wh <<    ;       bit.update(wh, -1);     }   }   signed main() {     ios_base ::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353, INF = 1e9;   const long long N = 2e5 + 1;   const double ep = 1e-10;   const double pi = 3.141592653589793238;   void floyd() {     long long n;     cin >> n;     string a, b;     cin >> a >> b;     long long a1 = 0, a2 = 0, k = 0, ans = INF, in = -1, in2 = -1;     for (long long i = 0; i < n; i++) {       if (a[i] ==  1 ) a1++;       if (b[i] ==  1 ) a2++;       if (a[i] ==  1  && b[i] ==  0 ) {         k++;         in2 = i;       }       if (a[i] ==  1  && b[i] ==  1 ) {         in = i;       }     }     if (a1 != a2 && (n - a1 + 1) != a2) {       cout << -1 <<   n ;       return;     }     if (a1 == a2) {       ans = min(k * 2, INF);     }     string t = a;     for (long long i = 0; i < n; i++) {       if (a[i] ==  1 )         a[i] =  0 ;       else         a[i] =  1 ;     }     if (in != -1) {       a[in] =  1 ;       long long k1 = 0, k2 = 0, k3 = 0, d = INF;       for (long long i = 0; i < n; i++) {         if (a[i] ==  1 ) k1++;         if (b[i] ==  1 ) k2++;         if (a[i] ==  1  && b[i] ==  0 ) {           k3++;         }       }       if (k1 == k2) {         d = k3 * 2 + 1;       }       ans = min(ans, d);     }     for (long long i = 0; i < n; i++) {       if (t[i] ==  1 )         t[i] =  0 ;       else         t[i] =  1 ;     }     if (in2 != -1) {       t[in2] =  1 ;       long long k1 = 0, k2 = 0, k3 = 0, d = INF;       for (long long i = 0; i < n; i++) {         if (t[i] ==  1 ) k1++;         if (b[i] ==  1 ) k2++;         if (t[i] ==  1  && b[i] ==  0 ) {           k3++;         }       }       if (k1 == k2) {         d = k3 * 2 + 1;       }       ans = min(ans, d);     }     if (ans == INF) {       cout << -1 <<   n ;       return;     }     cout << ans <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t = 1;     cin >> t;     while (t--) {       floyd();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     int l, r;   } h[(200000 + 5)];   int ql, qr;   namespace Seg_tree {   int hn, hv[(15000000 + 5)], ch[(15000000 + 5)][2];   void Modify(int &o, int L, int R, int p) {     if (!o) o = ++hn;     if (L == R) {       ++hv[o];       return;     }     if (p <= ((L + R) >> 1))       Modify(ch[o][0], L, ((L + R) >> 1), p);     else       Modify(ch[o][1], ((L + R) >> 1) + 1, R, p);     hv[o] = hv[ch[o][0]] + hv[ch[o][1]];   }   int Query(int &o, int L, int R) {     if (!o) return 0;     if (ql <= L && qr >= R) return hv[o];     int ret = 0;     if (ql <= ((L + R) >> 1)) ret += Query(ch[o][0], L, ((L + R) >> 1));     if (qr > ((L + R) >> 1)) ret += Query(ch[o][1], ((L + R) >> 1) + 1, R);     return ret;   }   };  // namespace Seg_tree   using namespace Seg_tree;   int n, ml[(200000 + 5)], mr[(200000 + 5)], mc[(200000 + 5)], rt[(200000 + 5)];   inline int lowbit(int x) { return x & (-x); }   void Add(int x, int y) {     while (x <= (n << 1)) {       Modify(rt[x], 1, (n << 1), y);       x += lowbit(x);     }   }   int Sum(int x) {     int ret = 0;     while (x > 0) {       ret += Query(rt[x], 1, n << 1);       x -= lowbit(x);     }     return ret;   }   int Qus(int l1, int r1, int l2, int r2) {     if (l1 > r1 || l2 > r2) return 0;     ql = l2;     qr = r2;     return Sum(r1) - Sum(l1 - 1);   }   void Init() {     for (int i = (1); i <= (int)(n); ++i) Add(h[i].l, h[i].r);     for (int i = (1); i <= (int)(n); ++i) {       ml[i] = Qus(h[i].l + 1, h[i].r - 1, h[i].l + 1, h[i].r - 1);       mc[i] = Qus(1, h[i].l - 1, h[i].l + 1, h[i].r - 1) +               Qus(h[i].l + 1, h[i].r - 1, h[i].r + 1, n << 1);       mr[i] = n - 1 - ml[i] - mc[i];     }   }   int Calc(node a, int v) {     if (v > a.l && v < a.r) return true;     return false;   }   bool Cross(node a, node b) {     int t1 = Calc(a, b.l), t2 = Calc(a, b.r);     return t1 ^ t2;   }   bool Check(node me, node a, node b) {     int ta = Calc(me, a.l), tb = Calc(me, b.l);     return ta == tb;   }   int main() {     scanf( %d , &n);     for (int i = (1); i <= (int)(n); ++i) {       scanf( %d%d , &h[i].l, &h[i].r);       if (h[i].l > h[i].r) swap(h[i].l, h[i].r);     }     if (n < 3) {       puts( 0 );       return 0;     }     if (n > 10000)       Init();     else {       for (int i = (1); i <= (int)(n); ++i)         for (int j = (1); j <= (int)(n); ++j)           if (i != j) {             if (Cross(h[i], h[j]))               ++mc[i];             else {               if (Calc(h[i], h[j].l))                 ++ml[i];               else                 ++mr[i];             }           }     }     long long ans = 1ll * n * (n - 1) * (n - 2) / 6;     for (int i = (1); i <= (int)(n); ++i) ans -= 1ll * ml[i] * mr[i];     long long cnt = 0;     for (int i = (1); i <= (int)(n); ++i) cnt += 1ll * mc[i] * (ml[i] + mr[i]);     cnt /= 2;     ans -= cnt;     printf( %I64d n , ans);     return 0;   }