#include <bits/stdc++.h>   bool office[25][25];   size_t m, n;   bool check(size_t x1, size_t y1, size_t x2, size_t y2) {     for (size_t x = x1; x <= x2; ++x) {       for (size_t y = y1; y <= y2; ++y) {         if (office[x][y]) {           return false;         }       }     }     return true;   }   bool check(size_t x, size_t y) {     for (size_t i = 0; i < m - x; ++i) {       for (size_t j = 0; j < n - y; ++j) {         if (check(i, j, i + x, j + y)) {           return true;         }       }     }     return false;   }   int main() {     std::cin >> m >> n;     std::string row;     size_t i = 0;     while (std::cin >> row) {       for (size_t j = 0; j < row.size(); ++j) {         office[i][j] = row[j] ==  1 ;       }       ++i;     }     size_t best = 4;     for (size_t x = 0; x < m; ++x) {       for (size_t y = 0; y < n; ++y) {         size_t p = (x + y + 2) * 2;         if (p > best && check(x, y)) {           best = p;         }       }     }     std::cout << best;   }


#include <bits/stdc++.h>   using namespace std;   int a[111111], b[111111], c[111111];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> a[i];     }     for (int i = 1; i <= n; ++i) {       b[i] = min(a[i], min(b[i - 1] + 1, i));     }     reverse(a + 1, a + n + 1);     for (int i = 1; i <= n; ++i) {       c[i] = min(a[i], min(c[i - 1] + 1, i));     }     reverse(c + 1, c + n + 1);     int ans = 0;     for (int i = 1; i <= n; ++i) {       ans = max(ans, min(b[i], c[i]));     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<string> s;   string ans(200,    );   set<string> Set;   vector<string> cand;   int main() {     cin >> n;     ans.resize(2 * n - 2);     for (int i = 0; i < 2 * n - 2; ++i) {       string a;       cin >> a;       Set.insert(a);       s.push_back(a);       if (a.size() == n - 1) cand.push_back(a);     }     bool prefix = true, suffix = true;     string P, S;     for (int i = 1; i < n - 1; ++i) prefix &= Set.count(cand[0].substr(0, i));     for (int i = 1; i < n - 1; ++i)       suffix &= Set.count(cand[1].substr(n - 1 - i));     if (suffix && prefix && cand[0].substr(1, n - 1) == cand[1].substr(0, n - 2))       P = cand[0], S = cand[1];     else       P = cand[1], S = cand[0];     for (int i = 1; i <= n - 1; ++i) {       string sub = P.substr(0, i);       for (int j = 0; j < ans.size(); ++j)         if (ans[j] ==     && s[j] == sub) {           ans[j] =  P ;           break;         }     }     for (int i = 0; i < ans.size(); ++i)       if (ans[i] ==    ) ans[i] =  S ;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100000 + 10;   const int M = 1000000007;   const double PI = atan(1) * 4;   const int oo = 2100000000;   int main() {     int n;     cin >> n;     vector<pair<int, int> > v(n), g(n);     for (int i = 0; i < n; ++i) {       scanf( %d%d , &v[i].first, &v[i].second);       int x = v[i].first;       int w = v[i].second;       g[i].first = x + w;       g[i].second = x - w;     }     sort((g).begin(), (g).end());     int ls = -1e9, ans = 0;     for (int i = 0; i < n; ++i) {       if (g[i].second >= ls) {         ++ans;         ls = g[i].first;       }     }     cout << ans;   }


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


#include <bits/stdc++.h>   int main() {     int n;     scanf( %d , &n);     char *str = new char[n + 1];     scanf( %s , str);     int size = strlen(str);     int sum1 = 0, sum2 = 0;     bool f = true;     for (int i = 0; i < size / 2; ++i) {       if ((str[i] ==  4  || str[i] ==  7 ) &&           (str[size - i - 1] ==  4  || str[size - i - 1] ==  7 )) {         sum1 += str[i] -  0 ;         sum2 += str[size - i - 1] -  0 ;       } else {         f = false;         break;       }     }     if (sum1 == sum2 && f)       printf( YES n );     else       printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long nb, ns, nc, pb, ps, pc, ab, as, ac, money;   bool solve(long long k) {     long long cb = k * ab, cs = k * as, cc = k * ac;     long long cost = 0;     cost += max(cb - nb, 0LL) * pb;     cost += max(cs - ns, 0LL) * ps;     cost += max(cc - nc, 0LL) * pc;     return cost <= money;   }   int main() {     ios_base::sync_with_stdio(false);     string s;     cin >> s;     for (int i = 0; i < s.size(); ++i)       if (s[i] ==  B )         ab++;       else if (s[i] ==  S )         as++;       else         ac++;     cin >> nb >> ns >> nc >> pb >> ps >> pc;     cin >> money;     long long lo = 0, hi = 1e14, mid;     while (lo < hi) {       mid = (lo + hi + 1) / 2;       if (solve(mid))         lo = mid;       else         hi = mid - 1;     }     cout << lo <<   n ;   }


#include<bits/stdc++.h>   using namespace std;      #define FOR(i, a, b) for(int i = (a); i < (b); ++i)   #define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)   #define REP(i, N) FOR(i, 0, N)   #define RREP(i, N) RFOR(i, N, 0)   #define FILL(A,value) memset(A,value,sizeof(A))   #define endl   n       #define ALL(V) V.begin(), V.end()   #define SZ(V) (int)V.size()   #define watch(x) cout << (#x) <<   is   << (x) << endl   #define pb push_back   #define mp make_pair   #define Pi 3.14159265358979   #define A first   #define B second      typedef long long ll;   typedef vector <int> vi;   typedef vector<ll> vl;   typedef pair <int, int> pi;   typedef pair <ll, int> pl;   typedef pair <ll, ll> pll;   typedef pair <double, double> pd;      //change these according to prob constraints   const int INF = 0x3f3f3f3f;   const int MAX = 100005;   const int MAX1 = 1005;   const int MAX2 = 105;   const int LEN = 105;   const int BASE = 1000000000;   const double EPS = 1e-7;   const int MOD = 1000000007;   //g++ -std=c++11 your_file.cpp -o your_program      ll mypow(ll a, ll b){    ll ans=1;    while(b){     if(b&1)      ans=(ans*a)%MOD;     b/=2;     a=(a*a)%MOD;    }    return ans;   }      int main(void){    ios_base::sync_with_stdio(false);    cin.tie(NULL);    cout.tie(NULL);    ifstream in(  );    ofstream out(  );    clock_t begin = clock();    int q;       cin >> q;       while (q--){           int n;           cin >> n;           if (n == 1) cout << 9 << endl;           else if (n == 2) cout << 98 << endl;           else if (n == 3) cout << 989 << endl;           if (n <= 3) continue;           string s =  98 ;           int ct = 9;           REP(i, n - 2){               if (ct == 10) ct = 0;               s += to_string(ct);               ct++;           }           cout << s << endl;       }    clock_t end = clock();       double esecs = double(end - begin) / CLOCKS_PER_SEC;       //cout << esecs <<   seconds  << endl;    return 0;    }


#include <bits/stdc++.h>   using namespace std;   int s[50];   char a[55][55];   int main() {     int n, i, j, c = 1, k, found;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %s , a[i]);       int l = strlen(a[i]);       for (j = 0; j < l; j++) {         s[a[i][j] -  a ]++;       }     }     for (i = 0; i < 26; i++) {       if (s[i] == 0) {         printf( %c , (char)(i +  a ));         c = 1;         break;       } else         c = 2;     }     if (c == 2) {       for (i = 0; i < 26; i++) {         for (j = 0; j < 26; j++) {           char str[4];           found = 0;           str[0] = (char)(i +  a );           str[1] = (char)(j +  a );           for (k = 0; k < n; k++) {             int l = strlen(a[k]), x = 0;             while (x < (l - 1)) {               if (a[k][x] == str[0] && a[k][x + 1] == str[1]) {                 found = 1;                 break;               } else                 x++;             }             if (found == 1) break;           }           if (found == 0) {             printf( %c%c n , str[0], str[1]);             c = 3;             break;           }         }         if (c == 3) break;       }     }     return 0;   }


#include <bits/stdc++.h>   const int maxn = 2e5 + 5;   int n, q;   inline int lowbit(int x) { return x & (-x); }   long long bit[maxn];   void add(int pos, long long v) {     for (int i = pos; i <= n; i += lowbit(i)) bit[i] += v;   }   long long querybit(int pos) {     long long ans = 0;     for (int i = pos; i; i -= lowbit(i)) ans += bit[i];     return ans;   }   long long mv[maxn << 2], a[maxn];   void updata(int rt) { mv[rt] = std::max(mv[rt << 1], mv[rt << 1 | 1]); }   void build(int l, int r, int rt) {     if (l == r) {       mv[rt] = a[l];       return;     }     int mid = (l + r) >> 1;     build(l, mid, rt << 1);     build(mid + 1, r, rt << 1 | 1);     updata(rt);   }   void modify(int l, int r, int rt, int pos, long long v) {     if (l == r) {       mv[rt] = v;       return;     }     int mid = (l + r) >> 1;     if (pos <= mid)       modify(l, mid, rt << 1, pos, v);     else       modify(mid + 1, r, rt << 1 | 1, pos, v);     updata(rt);   }   int query(int l, int r, int rt, int pos, long long v) {     if (mv[rt] < v || pos > n) return n + 1;     if (l == r) return l;     int mid = (l + r) >> 1;     if (pos <= l) {       if (mv[rt << 1] >= v)         return query(l, mid, rt << 1, pos, v);       else         return query(mid + 1, r, rt << 1 | 1, pos, v);     } else {       if (pos <= mid)         return std::min(query(l, mid, rt << 1, pos, v),                         query(mid + 1, r, rt << 1 | 1, pos, v));       else         return query(mid + 1, r, rt << 1 | 1, pos, v);     }   }   int search(int pos) {     if (pos > n) return -1;     if (querybit(pos - 1) == a[pos]) return pos;     return search(query(1, n, 1, pos + 1, querybit(pos)));   }   int main() {     scanf( %d%d , &n, &q);     for (int i = 1; i <= n; i++) scanf( %lld , &a[i]), add(i, a[i]);     build(1, n, 1);     while (q--) {       int tp;       long long tv;       scanf( %d%lld , &tp, &tv);       add(tp, tv - a[tp]);       a[tp] = tv;       modify(1, n, 1, tp, tv);       printf( %d n , search(1));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   vector<int> v[1005];   int ma, src;   bool vis[1005];   int p[1005];   void dfs(int s, int pa, int d) {     p[s] = pa;     vis[s] = 1;     if (d >= ma) {       src = s;       ma = d;     }     for (int i = 0; i < v[s].size(); i++) {       int u = v[s][i];       if (u != pa) {         dfs(u, s, d + 1);       }     }   }   int main() {     int u = 0;     ios::sync_with_stdio(0);     cin >> n >> m;     for (int i = 0; i < m; i++) {       int x, y;       cin >> x >> y;       v[x].push_back(y);       v[y].push_back(x);     }     int trees = 0, ans = 0;     vector<int> vvv;     vector<pair<int, int> > vv;     for (int i = 1; i <= n; i++) {       if (!vis[i]) {         ma = 0;         src = i;         dfs(i, -1, 0);         dfs(src, -1, 0);         int node = src;         for (int j = 0; j < ma / 2; j++) {           node = p[node];         }         vv.push_back({ma, node});         trees++;         u = max(u, ma);         vvv.push_back((ma + 1) / 2);       }     }     sort(vvv.begin(), vvv.end());     sort(vv.rbegin(), vv.rend());     for (int i = vvv.size() - 1; i >= max(0, (int)(vvv.size() - 2)); i--) {       ans += vvv[i];     }     int mi = 2;     if (vvv.size() >= 3) {       if (vvv[vvv.size() - 3] < vvv[vvv.size() - 1]) {         mi = 1;       }     }     ans += min(trees - 1, mi);     ans = max(ans, u);     cout << ans << endl;     for (int i = 0; i < vv.size() - 1; i++) {       cout << vv[0].second <<     << vv[i + 1].second << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, tx, ty, ans, fa[2055];   bool vis[2055];   int find(const int i) { return fa[i] == i ? i : fa[i] = find(fa[i]); }   void merge(const int i, const int j) { fa[i] = j; }   int main() {     scanf( %d , &n);     for (int i = 1; i <= 2010; ++i) {       fa[i] = i;     }     for (int i = 1; i <= n; ++i) {       scanf( %d %d , &tx, &ty);       ty += 1005;       if (!vis[tx]) {         ++ans;         vis[tx] = true;       }       if (!vis[ty]) {         ++ans;         vis[ty] = true;       }       tx = find(tx);       ty = find(ty);       if (tx != ty) {         --ans;         merge(tx, ty);       }     }     --ans;     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pii = pair<int, int>;   template <typename T>   using V = vector<T>;   const int N = 1e5 + 5;   bool taken[3 * N];   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int tc;     cin >> tc;     while (tc--) {       int n, m;       cin >> n >> m;       for (int i = 1; i <= 3 * n; ++i) taken[i] = false;       vector<pii> edges(m);       vector<int> matching;       for (auto &e : edges) {         cin >> e.first >> e.second;       }       int id = 0;       for (auto &e : edges) {         ++id;         if (taken[e.first] or taken[e.second]) continue;         matching.push_back(id);         taken[e.first] = taken[e.second] = true;       }       if ((int)matching.size() >= n) {         cout <<  Matching n ;         for (int i = 0; i < n; ++i) {           cout << matching[i] <<    ;         }       } else {         cout <<  IndSet n ;         for (int i = 1, cnt = 0; cnt < n; ++i) {           if (!taken[i]) {             cout << i <<    ;             ++cnt;           }         }       }       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, temp = 0;     char x[5][5];     for (i = 0; i < 3; i++) gets(x[i]);     for (i = 0; i < 3; i++)       for (j = 0; j < 3; j++)         if (x[i][j] != x[2 - i][2 - j]) temp = 1;     if (temp == 1)       printf( NO n );     else       printf( YES n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, x, n;   void input() {     cin >> a >> b >> n >> x;     return;   }   long long powmod(long long a, long long b) {     if (b == 1) return a;     if (b < 1) return 1;     long long t = powmod(a, b / 2) % 1000000007;     t = (t * t) % 1000000007;     if (b % 2 == 1) {       t = (t * a) % 1000000007;     }     return t;   }   void solve() {     if (a == 1) {       cout << (b * (n % 1000000007) + x) % 1000000007;       return;     }     long long tmp = powmod(a, n);     long long ans = ((tmp - 1) * powmod(a - 1, 1000000007 - 2)) % 1000000007;     tmp = (tmp * x) % 1000000007;     ans = (ans * b) % 1000000007;     cout << (tmp + ans) % 1000000007;     return;   }   void output() { return; }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     {       input();       solve();       output();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int sum = 0;     for (int i = 0; i < n; i++) {       int in;       cin >> in;       if (in < 0) in *= -1;       sum += in;     }     cout << sum << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long m, n, q;   long long X;   long long f(long long a) {     long long b = X - 2, c = X, k, ans = 1;     while (b > 0) {       long long p = 1, k = a;       while (p * 2 <= b) {         p *= 2;         k = k * k % c;       }       ans = ans * k % c;       b -= p;     }     return ans;   }   long long a[1005009], A;   long long p[1000509];   int main() {     cin >> n >> m >> A >> q;     p[0] = 1;     for (long long i = 1;; i++) {       p[i] = p[i - 1] * A % q;       if (p[i] == 1) {         X = i;         break;       }     }     a[0] = 1;     long long x = m, t = 1;     for (long long i = 1; i <= n - 1; i++) {       a[i] = (a[i - 1] * x * f(i)) % X;       x--;     }     long long s = 0;     for (long long i = 0; i <= n - 1; i++) {       s += a[i];       a[i] = s;     }     for (long long i = n - 1; i >= 0; i--) cout << p[a[i] % X] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 100;   int fen[maxn * 2], d[maxn], n, first[maxn], w[maxn];   pair<int, int> p[maxn];   int N = 1;   map<int, int> mp;   vector<int> v;   inline void upd(int first, int val) {     for (int i = first; i < N; i += i & -i) fen[i] = max(fen[i], val);   }   inline int ask(int first) {     int res = 0;     for (int i = first; i > 0; i -= i & -i) res = max(res, fen[i]);     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (int(i) = (0); (i) < (n); ++(i)) cin >> p[i].first >> p[i].second;     sort(p, p + n);     for (int(i) = (0); (i) < (n); ++(i)) {       first[i] = p[i].first, w[i] = p[i].second;       v.push_back(first[i] + w[i]);       v.push_back(first[i] - w[i]);     }     sort(v.begin(), v.end());     v.resize(unique(v.begin(), v.end()) - v.begin());     for (auto &(a) : (v)) mp[a] = N++;     int ans = 1;     for (int(i) = (0); (i) < (n); ++(i)) {       d[i] = 1 + ask(mp[first[i] - w[i]]);       upd(mp[first[i] + w[i]], d[i]);       ans = max(ans, d[i]);     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   template <typename T, typename U>   inline void chkmax(T& x, U y) {     if (x < y) x = y;   }   template <typename T, typename U>   inline void chkmin(T& x, U y) {     if (y < x) x = y;   }   int n, p;   int l[100005], r[100005], len[100005];   int main() {     cin >> n >> p;     for (int i = 0; i < n; i++)       scanf( %d%d , l + i, r + i), len[i] = r[i] - l[i] + 1;     l[n] = l[0], r[n] = r[0];     len[n] = len[0];     double ret = 0;     for (int i = 0; i < n; i++) {       int x = len[i] - r[i] / p + (l[i] - 1) / p;       int y = len[i + 1] - r[i + 1] / p + (l[i + 1] - 1) / p;       ret += 2000.0 * (1 - 1. * x / len[i] * y / len[i + 1]);     }     printf( %.10lf n , ret);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200010, P = 998244353;   int n, k, num, ans, h[maxn], fact[maxn], inv[maxn];   int qp(int x, int y) {     int z = 1;     for (; y; y >>= 1, x = 1LL * x * x % P) {       if (y & 1) z = 1LL * z * x % P;     }     return z;   }   int C(int x, int y) { return 1LL * fact[x] * inv[y] % P * inv[x - y] % P; }   int main() {     scanf( %d %d , &n, &k);     for (int i = fact[0] = inv[0] = 1; i <= n; i++) {       fact[i] = 1LL * i * fact[i - 1] % P;       inv[i] = qp(fact[i], P - 2);     }     for (int i = 0; i < n; i++) {       scanf( %d , &h[i]);     }     for (int i = 0; i < n; i++) {       if (h[i] ^ h[(i + 1) % n]) num++;     }     for (int i = 0; i + i <= num; i++) {       ans = (ans + 1LL * C(num, i) * C(num - i, i) % P * qp(k - 2, num - i - i)) %             P;     }     ans = 1LL * qp(k, n - num) * ans % P;     printf( %d n , 1LL * qp(2, P - 2) * (qp(k, n) - ans + P) % P);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   using min_heap = priority_queue<T, vector<T>, greater<T>>;   void setupIO(const string &PROB =   ) {     ios::sync_with_stdio(false);     cin.tie(nullptr);     if (PROB.length() != 0) {       ifstream infile(PROB +  .in );       if (infile.good()) {         freopen((PROB +  .in ).c_str(),  r , stdin);         freopen((PROB +  .out ).c_str(),  w , stdout);       }     }   }   int main() {     setupIO();     int n;     cin >> n;     int A[n][n];     for (int i = 0; i < (n); i++) {       for (int j = 0; j < (n); j++) {         char c;         cin >> c;         A[i][j] = c ==  1 ;       }     }     int B[n];     for (int i = 0; i < (n); i++) cin >> B[i];     bool C[n];     for (int i = 0; i < n; i++) C[i] = false;     ;     while (true) {       int tgt = -1;       for (int i = 0; i < (n); i++) {         if (B[i] == 0) {           tgt = i;           break;         }       }       if (tgt == -1) break;       assert(!C[tgt]);       for (int i = 0; i < (n); i++) {         if (A[tgt][i]) B[i]--;       }       C[tgt] = true;     }     vector<int> ans;     for (int i = 0; i < (n); i++)       if (C[i]) ans.push_back(i);     cout << ans.size() << endl;     for (int i = 0; i < (ans.size()); i++) {       if (i != 0) cout <<    ;       cout << ans[i] + 1;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct yo {     int val;     int id;     int next, prev;   };   vector<yo> a0, a;   int n, n0, x;   bool operator<(yo a, yo b) { return a.val < b.val; }   int main() {     cin >> n;     n0 = n;     if (n <= 2) {       cout << 0;       return 0;     }     for (int i = 0; i < n; i++) {       cin >> x;       yo cur;       cur.val = x;       cur.id = i;       cur.next = i + 1;       cur.prev = i - 1;       a0.push_back(cur);       a.push_back(cur);     }     sort(a.begin(), a.end());     long long v = 0, ans = 0;     for (int i = 0; i < a.size() - 2; i++) {       int cur_id = a[i].id;       if (a0[cur_id].prev == -1) {         a0[a0[cur_id].next].prev = -1;         ans += (n - 2) * (a[i].val - v);         v += (a[i].val - v);       } else if (a0[cur_id].next == n0) {         a0[a0[cur_id].prev].next = n0;         ans += (n - 2) * (a[i].val - v);         v += (a[i].val - v);       } else {         ans += min(a0[a0[cur_id].prev].val, a0[a0[cur_id].next].val) - v;         a0[a0[cur_id].next].prev = a0[cur_id].prev;         a0[a0[cur_id].prev].next = a0[cur_id].next;       }       n--;     }     cout << ans;   }


#include <bits/stdc++.h>   using namespace std;   vector<pair<int, int> > changes;   vector<int> norm(vector<int> x) {     int i = -1;     for (int bit = 30; bit >= 0; bit--) {       for (int pos = i + 1; pos < x.size(); pos++)         if (x[pos] & (1 << bit)) {           if (pos != ++i) {             changes.push_back(pair<int, int>(pos, i));             x[pos] ^= x[i];             changes.push_back(pair<int, int>(i, pos));             x[i] ^= x[pos];           }           for (int pos = 0; pos < x.size(); pos++)             if (pos != i) {               if (x[pos] & (1 << bit)) {                 changes.push_back(pair<int, int>(pos, i));                 x[pos] ^= x[i];               }             }           break;         }     }     return x;   }   int used[33];   int main() {     int n;     cin >> n;     vector<int> x(n), y(n);     for (int i = 0; i < n; i++) scanf( %d , &x[i]);     for (int i = 0; i < n; i++) scanf( %d , &y[i]);     vector<pair<int, int> > a, b;     vector<int> bx = norm(x);     a = changes;     changes.clear();     vector<int> by = norm(y);     b = changes;     reverse(b.begin(), b.end());     for (int i = 0; i < n; i++)       if (by[i]) used[__builtin_clz(by[i])] = 1;     for (int i = 0; i < n; i++)       if (bx[i] && used[__builtin_clz(bx[i])]) {         for (int j = i - 1; j >= 0; j--) {           if (bx[j] && used[__builtin_clz(bx[j])]) break;           a.push_back(pair<int, int>(j, j + 1));           bx[j + 1] ^= bx[j];           a.push_back(pair<int, int>(j + 1, j));           bx[j] ^= bx[j + 1];           a.push_back(pair<int, int>(j, j + 1));           bx[j + 1] ^= bx[j];         }       }     for (int i = 0; i < n; i++) {       if (by[i] != bx[i]) {         if (!by[i]) {           bx[i] = 0;           a.push_back(pair<int, int>(i, i));         } else {           for (int j = i + 1; j < n; j++)             if (bx[j] && (by[i] & (1 << (31 - __builtin_clz(bx[j]))))) {               bx[i] ^= bx[j];               a.push_back(pair<int, int>(i, j));             }           if (by[i] != bx[i]) {             cout << -1 << endl;             return 0;           }         }       }     }     vector<int> aux = x;     cout << a.size() + b.size() << endl;     for (int i = 0; i < a.size(); i++) {       cout << a[i].first + 1 <<     << a[i].second + 1 << endl;       aux[a[i].first] ^= aux[a[i].second];     }     for (int i = 0; i < b.size(); i++) {       cout << b[i].first + 1 <<     << b[i].second + 1 << endl;       aux[b[i].first] ^= aux[b[i].second];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 5e5 + 5;   int size[maxn], dep[maxn], val[maxn], dfn[maxn], hson[maxn], ans[maxn],       rnk[maxn];   int cnt[(1 << 22) + 4];   struct EDGE {     int to, next;     char c;   } edge[maxn << 1];   int head[maxn], tot, num;   void addedge(int u, int v, char c) {     edge[++tot].next = head[u];     edge[tot].to = v;     edge[tot].c = c;     head[u] = tot;   }   void dfs(int now, int pre) {     size[now] = 1;     dep[now] = dep[pre] + 1;     dfn[now] = ++num;     rnk[num] = now;     for (int i = head[now]; i; i = edge[i].next) {       int to = edge[i].to;       if (to == pre) continue;       val[to] = val[now] ^ (1 << (edge[i].c -  a ));       dfs(to, now);       size[now] += size[to];       if (size[to] > size[hson[now]]) hson[now] = to;     }   }   void cal(int now) {     if (cnt[val[now]]) ans[now] = max(ans[now], cnt[val[now]] - dep[now]);     for (int i = 0; i < 22; ++i) {       if (cnt[val[now] ^ (1 << i)])         ans[now] = max(ans[now], cnt[val[now] ^ (1 << i)] - dep[now]);     }     cnt[val[now]] = max(cnt[val[now]], dep[now]);     for (int i = head[now]; i; i = edge[i].next) {       int to = edge[i].to;       if (dep[to] < dep[now] || to == hson[now]) continue;       for (int j = dfn[to]; j <= dfn[to] + size[to] - 1; ++j) {         int tmp = rnk[j];         if (cnt[val[tmp]])           ans[now] = max(ans[now], cnt[val[tmp]] + dep[tmp] - 2 * dep[now]);         for (int i = 0; i < 22; ++i) {           if (cnt[val[tmp] ^ (1 << i)])             ans[now] =                 max(ans[now], cnt[val[tmp] ^ (1 << i)] + dep[tmp] - 2 * dep[now]);         }       }       for (int j = dfn[to]; j <= dfn[to] + size[to] - 1; ++j) {         int tmp = rnk[j];         cnt[val[tmp]] = max(cnt[val[tmp]], dep[tmp]);       }     }   }   void dfs2(int now, bool heave) {     for (int i = head[now]; i; i = edge[i].next) {       int to = edge[i].to;       if (dep[to] < dep[now] || to == hson[now]) continue;       dfs2(to, 0);       ans[now] = max(ans[now], ans[to]);     }     if (hson[now]) dfs2(hson[now], 1), ans[now] = max(ans[now], ans[hson[now]]);     cal(now);     if (!heave)       for (int i = dfn[now]; i <= dfn[now] + size[now] - 1; ++i)         cnt[val[rnk[i]]] = 0;   }   int main() {     int n;     scanf( %d , &n);     for (int i = 2; i <= n; ++i) {       int to;       char c;       scanf( %d %c , &to, &c);       addedge(i, to, c);       addedge(to, i, c);     }     dfs(1, 0);     dfs2(1, 0);     for (int i = 1; i <= n; ++i) printf( %d  , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     vector<long long int> v1;     map<long long int, long long int> m1;     long long int size, count = 0, i, j, s, k;     cin >> size;     long long int a[size];     for (i = 0; i < size; i++) {       cin >> a[i];     }     sort(a, a + size);     for (i = 0; i < size; i++) {       for (j = i + 1; j < size; j++) {         k = a[i] + a[j];         m1[k]++;       }     }     map<long long int, long long int>::iterator itr;     for (itr = m1.begin(); itr != m1.end(); itr++) {       v1.push_back(itr->second);     }     sort(v1.begin(), v1.end(), greater<long long int>());     cout << v1[0];   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 110000, MAXM = MAXN * 2;   const int inf = 10000000;   struct eglist {     int other[MAXM], succ[MAXM], last[MAXM], sum;     void addEdge(int a, int b) {       other[++sum] = b, succ[sum] = last[a], last[a] = sum;       other[++sum] = a, succ[sum] = last[b], last[b] = sum;     }     void clear(int n) {       for (int i = 0; i <= n; i++) last[i] = 0;       sum = 0;     }   } e;   int fa[MAXN];   pair<int, int> down1[MAXN], down2[MAXN];   int up[MAXN], ans = 0;   queue<int> q;   int visit[MAXN] = {};   int n, m, d;   void dfsDown(int x, int fa) {     if (visit[x]) {       down1[x] = make_pair(0, 0);       down2[x] = make_pair(0, 0);     } else {       down1[x].first = -inf, down2[x].first = -inf;     }     for (int i = e.last[x]; i; i = e.succ[i]) {       int y = e.other[i];       if (y == fa) continue;       dfsDown(y, x);       pair<int, int> tmp = down1[y];       tmp.first++;       tmp.second = y;       if (tmp > down1[x]) {         down2[x] = down1[x];         down1[x] = tmp;       } else if (tmp > down2[x])         down2[x] = tmp;     }   }   void dfsUp(int x, int fa) {     up[x] = visit[x] ? 0 : -inf;     if (fa == 0) {       if (down1[x].first <= d) ans++;     } else {       up[x] = max(up[x], up[fa] + 1);       if (down1[fa].second == x)         up[x] = max(up[x], down2[fa].first + 1);       else         up[x] = max(up[x], down1[fa].first + 1);       if (max(up[x], down1[x].first) <= d) ans++;     }     for (int i = e.last[x]; i; i = e.succ[i]) {       int y = e.other[i];       if (y == fa) continue;       dfsUp(y, x);     }   }   int main() {     scanf( %d %d %d , &n, &m, &d);     for (int i = 1; i <= m; i++) {       int p;       scanf( %d , &p);       visit[p] = 1;     }     e.clear(n);     for (int i = 1; i < n; i++) {       int a, b;       scanf( %d %d , &a, &b);       e.addEdge(a, b);     }     dfsDown(1, 0);     dfsUp(1, 0);     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int mx(int a, int b) {     if (a > b) return a;     return b;   }   int main() {     int n, s;     scanf( %d %d , &n, &s);     int max = -1;     for (int i = 0; i < n; ++i) {       int x, y;       scanf( %d %d , &x, &y);       if (x >= s && !(x == s && y == 0)) continue;       if (y == 0) y = 100;       max = mx(max, 100 - y);     }     printf( %d , max);   }


#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2>   istream& operator>>(istream& in, pair<T1, T2>& a) {     in >> a.first >> a.second;     return in;   }   template <typename T1, typename T2>   ostream& operator<<(ostream& out, pair<T1, T2> a) {     out << a.first <<     << a.second;     return out;   }   template <typename T, typename T1>   T amax(T& a, T1 b) {     if (b > a) a = b;     return a;   }   template <typename T, typename T1>   T amin(T& a, T1 b) {     if (b < a) a = b;     return a;   }   const long long INF = 1e18;   const int32_t M = 1e9 + 7;   const int32_t MM = 998244353;   const long long N = 5e5 + 5;   vector<long long> v[N];   void solve() {     long long n;     cin >> n;     v[0].clear();     for (long long i = 1; i < n + 1; i++) {       v[i].clear();       v[i].push_back(-1);     }     for (long long i = 0; i < n; i++) {       long long x;       cin >> x;       v[x].push_back(i);     }     if ((long long)((v[0]).size()) < 2) {       cout <<  0 n ;       return;     }     long long z = (long long)((v[0]).size());     long long L = v[0][(z + 1) / 2], R = v[0][z / 2 - 1];     vector<pair<long long, long long> > vec;     for (long long i = 1; i < n + 1; i++) {       v[i].push_back(n + 2);       long long l = *prev(lower_bound((v[i]).begin(), (v[i]).end(), L));       long long r = *upper_bound((v[i]).begin(), (v[i]).end(), R);       if (l != -1 || r != n + 2) vec.push_back({l, r});     }     for (long long x : v[0]) {       if (x < L) vec.push_back({x, -1});     }     sort((vec).begin(), (vec).end(),          [&](pair<long long, long long> a, pair<long long, long long> b) {            return a.first > b.first;          });     long long ans = 0;     multiset<long long> s;     vector<long long> vis(n + 4);     for (auto x : vec) {       if (x.second == -1) {         if ((long long)((s).size())) {           long long r = -*s.begin();           s.erase(s.begin());           vis[r] = 1;           ans++;         }       } else {         s.insert(-x.second);       }     }     vector<long long> vec2;     for (auto x : vec) {       if (x.second != -1 && !vis[x.second]) vec2.push_back(x.second);     }     for (long long x : v[0]) {       if (x > R) vec2.push_back(x), vis[x] = 1;     }     s.clear();     sort((vec2).begin(), (vec2).end());     for (long long x : vec2) {       if (vis[x]) {         if ((long long)((s).size())) {           s.erase(s.begin());           ans++;         }       } else {         s.insert(x);       }     }     amin(ans, z / 2);     cout << ans <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     cin >> t;     while (t--) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int N = 1e5 + 5;   int x[N], y[N], n, ans = -1, vis[N];   vector<int> gx[N], gy[N];   void dfs(int k) {     if (vis[k]) return;     vis[k] = 1;     for (auto it : gx[x[k]]) dfs(it);     for (auto it : gy[y[k]]) dfs(it);   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> x[i] >> y[i];       gx[x[i]].push_back(i);       gy[y[i]].push_back(i);     }     for (int i = 1; i <= n; i++) {       if (!vis[i]) {         ans++;         dfs(i);       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string s;   int main() {     cin >> s;     int len = s.length();     if (s[0] >=  a  && s[0] <=  z ) s[0] -= 32;     cout << s << endl;     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, m;     while (~scanf( %d%d , &n, &m)) {       int p = 0;       while (n <= m) {         n = n * 3;         m = m * 2;         p++;       }       printf( %d n , p);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct node {     char word[210];     int l, r;   } a[20];   char str[210];   int n;   bool check(int l, int r) {     int i, j, k, temp, len;     for (i = 1; i <= n; i++) {       len = strlen(a[i].word);       temp = 0;       if (len < (r - l + 1))         temp = 0;       else {         for (j = 0; j <= len - (r - l + 1); j++) {           for (k = 0; k <= (r - l); k++) {             if (str[l + k] != a[i].word[j + k]) break;           }           if (k > (r - l)) temp++;         }       }       if (temp < a[i].l || temp > a[i].r) return false;     }     return true;   }   int main() {     int i, l, j, k, t, flag = 0, ans = 0;     scanf( %s , str);     l = strlen(str);     scanf( %d , &n);     for (i = 1; i <= n; i++) scanf( %s%d%d , a[i].word, &a[i].l, &a[i].r);     for (i = 0; i < l; i++) {       for (j = i; j < l; j++) {         flag = 0;         for (k = 0; k < i; k++) {           for (t = 0; t <= (j - i); t++) {             if (str[i + t] != str[k + t]) break;           }           if (t > (j - i)) {             flag = 1;             break;           }         }         if (flag == 1) continue;         if (check(i, j)) ans++;       }     }     printf( %d n , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long double x, y, z;   long double a[12];   int ans = 0;   const char s[12][10] = { x^y^z ,  x^z^y ,  (x^y)^z ,  (x^z)^y ,                            y^x^z ,  y^z^x ,  (y^x)^z ,  (y^z)^x ,                            z^x^y ,  z^y^x ,  (z^x)^y ,  (z^y)^x };   int main() {     cin >> x >> y >> z;     a[0] = pow(y, z) * log(x);     a[1] = pow(z, y) * log(x);     a[2] = a[3] = y * z * log(x);     a[4] = pow(x, z) * log(y);     a[5] = pow(z, x) * log(y);     a[6] = a[7] = x * z * log(y);     a[8] = pow(x, y) * log(z);     a[9] = pow(y, x) * log(z);     a[10] = a[11] = x * y * log(z);     for (int i = 1; i <= 11; i++) {       if (a[i] > a[ans]) ans = i;     }     printf( %s n , s[ans]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   namespace io {   const int __SIZE = (1 << 21) + 1;   char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1,                                              __c, qu[55];   int __f, qr, _eof;   inline void flush() { fwrite(obuf, 1, oS - obuf, stdout), oS = obuf; }   inline void gc(char& x) {     x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),                      (iS == iT ? EOF : *iS++))                   : *iS++);   }   inline void pc(char x) {     *oS++ = x;     if (oS == oT) flush();   }   inline void pstr(const char* s) {     int __len = strlen(s);     for (__f = 0; __f < __len; ++__f) pc(s[__f]);   }   inline void gstr(char* s) {     for (__c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),                            (iS == iT ? EOF : *iS++))                          : *iS++);          __c < 32 || __c > 126 || __c ==    ;)       __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),                          (iS == iT ? EOF : *iS++))                       : *iS++);     for (; __c > 31 && __c < 127 && __c !=    ;          ++s, __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),                                 (iS == iT ? EOF : *iS++))                               : *iS++))       *s = __c;     *s = 0;   }   template <class I>   inline bool gi(I& x) {     _eof = 0;     for (__f = 1,         __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),                           (iS == iT ? EOF : *iS++))                         : *iS++);          (__c <  0  || __c >  9 ) && !_eof;          __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),                            (iS == iT ? EOF : *iS++))                          : *iS++)) {       if (__c ==  - ) __f = -1;       _eof |= __c == EOF;     }     for (x = 0; __c <=  9  && __c >=  0  && !_eof;          __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),                            (iS == iT ? EOF : *iS++))                          : *iS++))       x = x * 10 + (__c & 15), _eof |= __c == EOF;     x *= __f;     return !_eof;   }   template <class I>   inline void print(I x) {     if (!x) pc( 0 );     if (x < 0) pc( - ), x = -x;     while (x) qu[++qr] = x % 10 +  0 , x /= 10;     while (qr) pc(qu[qr--]);   }   struct Flusher_ {     ~Flusher_() { flush(); }   } io_flusher_;   }  // namespace io   using io::gc;   using io::gi;   using io::gstr;   using io::pc;   using io::print;   using io::pstr;   using ll = long long;   using pii = pair<int, int>;   const int MOD = 1000000007;   ll c[500005];   int p2[500005];   map<ll, vector<pii>> NO;   int bcj[500006], ltk;   void binit() { memset(bcj, -1, sizeof bcj); }   int bget(int i) { return (bcj[i] < 0) ? i : (bcj[i] = bget(bcj[i])); }   void bmerge(int i, int j) {     int ri = bget(i), rj = bget(j);     if (ri == rj) {       return;     }     ltk--;     if (bcj[ri] > bcj[rj]) {       swap(ri, rj);     }     bcj[ri] += bcj[rj];     bcj[rj] = ri;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, m, k;     gi(n), gi(m), gi(k);     for (int i = (1); i < ((n) + 1); i++) gi(c[i]);     p2[0] = 1;     for (int i = (1); i < ((n) + 1); i++) {       p2[i] = 2 * p2[i - 1];       if (p2[i] >= MOD) p2[i] -= MOD;     }     while (m--) {       int u, v;       gi(u), gi(v);       auto& ref = NO[c[u] ^ c[v]];       ref.push_back({u, v});     }     ll notinno = (1ll << k) - NO.size();     ll ans = 1ll * (notinno % MOD) * p2[n] % MOD;     binit();     for (auto& k : NO) {       ltk = n;       for (auto& i : k.second) bmerge(i.first, i.second);       ans += p2[ltk];       for (auto& i : k.second) bcj[i.first] = bcj[i.second] = -1;       if (ans >= MOD) ans -= MOD;     }     print(ans);   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5e3 + 5;   int N, Q;   int dp[MAXN][MAXN];   int pre[MAXN][MAXN];   int A[MAXN];   int f(int l, int r) {     if (dp[l][r] != -1) {       return dp[l][r];     }     if (l == r - 1) {       dp[l][r] = A[l];     } else {       dp[l][r] = f(l, r - 1) ^ f(l + 1, r);     }     return dp[l][r];   }   int pref(int l, int r) {     if (pre[l][r] != -1) {       return pre[l][r];     }     pre[l][r] = dp[l][r];     if (l != r - 1) {       pre[l][r] = max(pre[l][r], pref(l + 1, r));       pre[l][r] = max(pref(l, r - 1), pre[l][r]);     }     return pre[l][r];   }   int main() {     cin >> N;     for (int i = 0; i < N; i++) {       cin >> A[i];     }     for (int i = 0; i < MAXN; i++) {       for (int j = 0; j < MAXN; j++) {         dp[i][j] = -1;         pre[i][j] = -1;       }     }     for (int i = 0; i < N; i++) {       for (int j = i + 1; j <= N; j++) {         f(i, j);       }     }     for (int i = 0; i < N; i++) {       for (int j = i + 1; j <= N; j++) {         pref(i, j);       }     }     cin >> Q;     for (int i = 0; i < Q; i++) {       int a, b;       cin >> a >> b;       cout << pre[a - 1][b] << endl;     }   }


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


#include <bits/stdc++.h>   using namespace std;   const long long int inf = LLONG_MAX;   const long long int mod = 1e9 + 7;   const long long int N = 1e2 + 1;   long long int a[N][N];   long long int n;   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (long long int i = 0; i < n; ++i) {       for (long long int j = 0; j < n; ++j) {         cin >> a[i][j];       }     }     vector<long long int> good;     for (long long int i = 0; i < n; ++i) {       bool g = true;       for (long long int j = 0; j < n; ++j) {         if (a[i][j] == 1 or a[i][j] == 3) {           g = false;         }       }       if (g) good.push_back(i);     }     cout << good.size() << endl;     for (auto &i : good) {       cout << i + 1 <<    ;     }     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   stack<pair<int, int> > stc;   int main() {     ios::sync_with_stdio(false);     int n, men, i, cur_max, max;     cin >> n;     cin >> men;     stc.push(make_pair(men, 0));     max = 0;     for (i = 0; i < n - 1; i++) {       cin >> men;       if (stc.top().first > men) {         stc.push(make_pair(men, 1));         if (max == 0) max = 1;         continue;       }       cur_max = 0;       while (true) {         if (stc.top().first > men) {           if (stc.top().second > cur_max || stc.top().second == 0) {             stc.push(make_pair(men, cur_max + 1));             if (max < cur_max + 1) max = cur_max + 1;             break;           }           stc.pop();         } else {           if (stc.top().second == 0) {             stc.push(make_pair(men, 0));             break;           }           if (stc.top().second > cur_max) cur_max = stc.top().second;           if (max < cur_max) max = cur_max;           stc.pop();         }       }     }     cout << max << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void __print(int x) { cerr << x; }   void __print(long x) { cerr << x; }   void __print(long long x) { cerr << x; }   void __print(unsigned x) { cerr << x; }   void __print(unsigned long x) { cerr << x; }   void __print(unsigned long long x) { cerr << x; }   void __print(float x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(long double x) { cerr << x; }   void __print(char x) { cerr <<      << x <<     ; }   void __print(const char *x) { cerr <<     << x <<    ; }   void __print(const string &x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }   template <typename T, typename V>   void __print(const pair<T, V> &x) {     cerr <<  { ;     __print(x.first);     cerr <<  , ;     __print(x.second);     cerr <<  } ;   }   template <typename T>   void __print(const T &x) {     int f = 0;     cerr <<  { ;     for (auto &i : x) 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...);   }   void pre() {}   long long a[200009];   long long n, p, k;   long long dp[200009];   void solve() {     cin >> n >> p >> k;     for (int i = 0; i < (n); ++i) cin >> a[i + 1], dp[i] = -1;     sort(a + 1, a + n + 1);     dp[0] = 0;     long long ans = 0;     for (int i = 1; i <= (n); ++i) {       if (i >= k)         dp[i] = min(dp[i - 1], dp[i - k]) + a[i];       else         dp[i] = dp[i - 1] + a[i];       if (dp[i] <= p) ans = i;     }     cout << ans <<   n ;   }   int main() {     cin.sync_with_stdio(0);     cin.tie(0);     cin.exceptions(cin.failbit);     pre();     int t;     cin >> t;     for (int i = 0; i < (t); ++i) solve();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string generateString(int a) {     string s =   ;     for (int i = 0; i < a; i++) {       char x =  a  + i;       s = s + x;     }     return s;   }   string wrapAround(string s, int b, int c) {     int x = c - b;     for (int i = 0; i < x; i++) {       s += s[i % b];     }     return s;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int A[n];       for (int i = 0; i < n; i++) {         cin >> A[i];       }       vector<vector<int> > v;       for (int i = 0; i < 26; i++) {         vector<int> x(n + 1, 0);         v.push_back(x);       }       for (int j = 0; j < n; j++) {         v[A[j] - 1][j + 1]++;       }       for (int i = 0; i < 26; i++) {         for (int j = 1; j < n + 1; j++) {           v[i][j] += v[i][j - 1];         }       }       vector<vector<int> > pos;       pos.resize(26);       for (int i = 0; i < n; i++) {         pos[A[i] - 1].push_back(i);       }       int ans = 1;       for (int i = 0; i < 26; i++) {         int sz = pos[i].size();         int lim = sz / 2;         if (sz != 0) {           for (int k = 0; k < lim; k++) {             int st = pos[i][k];             int en = pos[i][sz - k - 1];             int temp = 0;             for (int j = 0; j < 26; j++) {               int x = v[j][en] - v[j][st + 1];               temp = max(x, temp);             }             ans = max(ans, 2 * (k + 1) + temp);           }         }       }       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   int n, k;   int a[200000];   bool ok() {     int cur = a[k - 1];     for (int i = k; i < n; ++i) {       if (a[i] != cur) return false;     }     return true;   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> k;     for (int i = 0; i < (int)(n); i++) cin >> a[i];     if (!ok())       cout << -1 << endl;     else {       int ans = 0;       int cur = a[k - 1];       for (int i = 0; i < (int)(k); i++) {         if (a[i] != cur) ans = i + 1;       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a[1000005] = {1, 1}, b[1000005], num;   long long x2, ans = 100000000;   void init() {     long long i, j;     for (i = 2; i <= 1000000; i++) {       if (a[i]) continue;       for (j = i * i; j <= 1000000; j += i) a[j] = 1;     }     for (i = 1; i <= 1000000; i++)       if (a[i] == 0) b[++num] = i;   }   int main(void) {     init();     scanf( %lld , &x2);     long long x = x2, p = 0;     for (long long i = 1; b[i] * b[i] <= x; i++)       while (x % b[i] == 0) x /= b[i], p = b[i];     if (x != 1) p = x;     for (long long i = x2 - p + 1; i <= x2; i++) {       long long tmp = i;       long long pp = p;       for (long long j = 1; b[j] * b[j] <= tmp; j++)         while (tmp % b[j] == 0) tmp /= b[j], pp = b[j];       if (tmp != 1) pp = tmp;       if (pp == i) continue;       ans = min(ans, i - pp + 1);     }     printf( %lld n , ans);     return 0;   }


#include <bits/stdc++.h>   namespace {   using namespace std;   template <typename T>   inline bool whitespace(const vector<T> &) {     return false;   }   template <typename T>   inline bool whitespace(const T &) {     return true;   }   inline bool whitespace(const char) { return false; }   template <typename S, typename T>   ostream &operator<<(ostream &os, const pair<S, T> &p) {     os << p.first << (whitespace(p.second) ?     :   ) << p.second <<   n ;     return os;   }   template <typename S, typename T>   istream &operator>>(istream &is, pair<S, T> &p) {     is >> p.first >> p.second;     return is;   }   template <typename T>   istream &operator>>(istream &is, vector<T> &v) {     for (T &t : v) is >> t;     return is;   }   template <typename T>   ostream &operator<<(ostream &os, const vector<T> &v) {     for (const T &t : v) {       os << t << (whitespace(t) ?     :   );     }     os <<   n ;     return os;   }   template <typename T>   istream &operator>>(istream &is, deque<T> &v) {     for (T &t : v) is >> t;     return is;   }   template <typename T>   ostream &operator<<(ostream &os, const deque<T> &v) {     for (const T &t : v) {       os << t << (whitespace(t) ?     :   );     }     os <<   n ;     return os;   }   template <typename T>   T gcd(T a, T b) {     while (b) {       T mod = a % b;       a = b;       b = mod;     }     return a;   }   template <typename T>   T sum(const vector<T> &v) {     T s = 0;     for (T value : v) s += value;     return s;   }   template <typename T>   vector<T> prefix_sum(const vector<T> &v) {     vector<T> res(v.size() + 1);     res[0] = 0;     for (long long i = 0; i < v.size(); ++i) res[i + 1] = res[i] + v[i];     return res;   }   template <typename S, typename T>   S min(S a, T b) {     return a < (S)b ? a : b;   }   template <typename S, typename T>   S max(S a, T b) {     return a < (S)b ? b : a;   }   class exit_exception : exception {};   istream &in = cin;   ostream &out = cout;   template <typename T>   void answer(T ans) {     out << ans <<   n ;     throw exit_exception();   }   struct autoint {     long long x;     autoint() { in >> x; }     operator long long() { return x; }     autoint(autoint &i) { x = i.x; }     long long operator--() { return --x; }     long long operator--(signed) { return x--; }     long long operator++() { return ++x; }     long long operator++(signed) { return x++; }   };   template <typename T>   struct autoread : T {     template <typename... Args>     autoread(Args &&...args) : T(args...) {       in >> *dynamic_cast<T *>(this);     }   };   }  // namespace   void solve();   signed main() {     cin.tie(nullptr);     cout.tie(nullptr);     ios::sync_with_stdio(false);     long long t = 1;     cin >> t;     do {       try {         solve();       } catch (exit_exception &) {       }     } while (--t);     return 0;   }   void solve() {     autoint n, k;     string s;     cin >> s;     vector<map<char, long long>> groups((k + 1) / 2);     for (long long i = 0; i < (k + 1) / 2; ++i) {       for (long long j = i; j < n; j += k) groups[i][s[j]]++;       if (i + i < k - 1)         for (long long j = k - i - 1; j < n; j += k) groups[i][s[j]]++;     }     long long ans = 0;     for (auto m : groups) {       vector<long long> cnts;       for (auto p : m) cnts.push_back(p.second);       stable_sort(cnts.rbegin(), cnts.rend());       ans += accumulate(cnts.begin(), cnts.end(), 0ll) - cnts.front();     }     answer(ans);   }


#include <bits/stdc++.h>   using namespace std;   void getint(int &v) {     char ch, fu = 0;     for (ch =  * ; (ch <  0  || ch >  9 ) && ch !=  - ; ch = getchar())       ;     if (ch ==  - ) fu = 1, ch = getchar();     for (v = 0; ch >=  0  && ch <=  9 ; ch = getchar()) v = v * 10 + ch -  0 ;     if (fu) v = -v;   }   const int MO = 1e9 + 7;   long long P[5555], ans, p[5555], T;   int n, k, a[5555], f[5010][5010];   long long pw(long long x, long long y) {     long long t = 1;     for (; y; y >>= 1) {       if (y & 1) t = t * x % MO;       x = x * x % MO;     }     return t;   }   int main() {     cin >> n >> k;     T = 1;     for (int i = 1; i <= n; ++i) getint(a[i]), T = T * a[i] % MO;     P[0] = 1;     for (int i = 1; i <= n; ++i) P[i] = P[i - 1] * (k - i + 1) % MO;     f[0][0] = 1;     for (int i = 1; i <= n; ++i) {       f[i][0] = 1;       for (int j = 1; j <= i; ++j)         f[i][j] = (f[i - 1][j] + (long long)f[i - 1][j - 1] * a[i]) % MO;     }     long long t = MO - pw(n, MO - 2);     p[0] = 1;     for (int i = 1; i <= n; ++i) p[i] = p[i - 1] * t % MO;     for (int i = 0; i <= n; ++i) {       if (n - i > k) continue;       long long x = f[n][i] * P[n - i] % MO * p[n - i] % MO;       (ans += x) %= MO;     }     cout << (T - ans + MO) % MO << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long N = 201;   const long long K = 201;   const long long X = 201;   long long dp[N][X];   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n, k, x;     cin >> n >> k >> x;     vector<long long> picBeauty;     picBeauty.push_back(-1);     for (long long i = 1; i <= n; i++) {       long long x;       cin >> x;       picBeauty.push_back(x);     }     long long minimumPosts = n / k;     if (x < minimumPosts) {       cout << -1 << endl;       return 0;     }     for (long long i = 0; i <= n; i++) {       for (long long j = 0; j <= x; j++) {         dp[i][j] = 0;       }     }     for (long long i = 1; i <= k; i++) {       dp[i][1] = picBeauty[i];     }     long long flag = 0;     for (long long j = 2; j <= x; j++) {       for (long long i = 1; i <= n; i++) {         long long tempFlag = 0;         long long backAllowed = i - k;         for (long long y = i - 1; y >= 1 && y >= backAllowed; y--) {           if (dp[y][j - 1] > 0)             dp[i][j] = max(dp[i][j], dp[y][j - 1] + picBeauty[i]);         }       }     }     long long ans = 0;     for (long long i = n - k + 1; i <= n; i++) {       ans = max(ans, dp[i][x]);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   vector<vector<int> > adj(500005);   int visited[500005];   int counter;   void dfs(int node, int color) {     counter++;     visited[node] = color;     for (int i = 0; i < adj[node].size(); ++i) {       if (visited[adj[node][i]] == 0) dfs(adj[node][i], color);     }     return;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int test = 1;     while (test--) {       int n, val;       cin >> n;       vector<int> arr;       for (int i = 0; i < n; ++i) {         cin >> val;         arr.push_back(val);       }       string s;       cin >> s;       vector<pair<int, int> > v;       int flag = 0, start = -1, ender = -1;       for (int i = 0; i < s.size(); ++i) {         if (flag == 0 && s[i] ==  0 ) continue;         if (flag == 0 && s[i] ==  1 ) {           start = i;           flag = 1;           continue;         }         if (flag == 1 && s[i] ==  0 ) {           ender = i;           v.push_back({start, ender});           start = -1;           ender = -1;           flag = 0;           continue;         }         if (flag == 1 && s[i] ==  1 ) continue;       }       if (flag == 1) v.push_back({start, n - 1});       vector<pair<int, int> >::iterator it;       for (it = v.begin(); it != v.end(); ++it) {         sort(arr.begin() + it->first, arr.begin() + it->second + 1);       }       flag = 0;       for (int i = 1; i < n; ++i) {         if (arr[i] <= arr[i - 1]) {           flag = 1;           break;         }       }       if (flag == 1)         cout <<  NO ;       else         cout <<  YES ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[5];   bool able[5];   int main() {     int T;     scanf( %d , &T);     while (T--) {       for (int i = 1; i <= 4; i++) able[i] = true;       bool flag = true;       for (int i = 1; i <= 4; i++) scanf( %d , &a[i]);       if ((a[1] + a[2]) % 2 == 1) flag = false;       if (flag) {         able[1] = able[2] = false;         if (a[4] == 0 && a[1] == 0) able[4] = false;         if (a[2] == 0 && a[3] == 0) able[3] = false;       } else {         able[3] = able[4] = false;         if (a[4] == 0 && a[1] == 0) able[1] = false;         if (a[2] == 0 && a[3] == 0) able[2] = false;       }       for (int i = 1; i <= 4; i++) {         if (able[i])           printf( Ya  );         else           printf( Tidak  );       }       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   int n, m, k;   int q(int x) { return x * (x + 1) / 2; }   int p[200005];   int b[200005];   int u[200005];   int dfs(int v) {     u[v] = 1;     if (!u[p[v]])       return dfs(p[v]) + 1;     else       return 1;   }   int solve() {     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       scanf( %d , &p[i + 1]);     }     int c = 0;     for (int i = 0; i < n; ++i) {       scanf( %d , &b[i + 1]);       c += b[i + 1];     }     if (n == 1) {       cout << (c + 1) % 2;       return 0;     }     int res = 0;     int ones = 0;     for (int i = 1; i <= n; ++i)       if (!u[i]) {         int k = dfs(i);         if (k == 1)           ++ones;         else           ++res;       }     if (res == 1 && ones == 0) {       cout << (c + 1) % 2;       return 0;     }     if (res == 0)       cout << ones + (c + 1) % 2;     else if (ones == 0)       cout << res + (c + 1) % 2;     else       cout << res + ones + (c + 1) % 2;     return 0;   }   int main() {     int t = 1;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int tux, foo, bar, baz, quz = 1;   int main() {     scanf( %d , &tux);     for (int i = 1; i <= tux; ++i) {       int pur;       scanf( %d , &pur);       foo += pur, bar++;       if (foo * quz > baz * bar) baz = foo, quz = bar;     }     printf( %.6lf , baz * 1.0 / quz);   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const double EPS = 1e-9;   const long long MAXN = 5209;   inline long long read() {     long long x = 0, f = 1;     char ch = getchar();     while (ch < 48 || ch > 57) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >= 48 && ch <= 57) x = x * 10 + ch - 48, ch = getchar();     return x * f;   }   long long n, m, a[MAXN], b[MAXN], p[MAXN];   double f[57][MAXN];   bool check(double x) {     for (int i = n - 1; ~i; i--) {       for (int j = m + 1; j <= MAXN; j++) f[i + 1][j] = x;       for (int j = 0; j <= m; j++)         f[i][j] = min((f[i + 1][j + a[i]] + a[i]) * p[i] * 0.01 +                           (f[i + 1][j + b[i]] + b[i]) * (100 - p[i]) * 0.01,                       x);     }     return f[0][0] < x;   }   signed main() {     n = read(), m = read();     for (int i = 0; i < n; i++) a[i] = read(), b[i] = read(), p[i] = read();     double l = 0, r = 1e9;     int FTL = 0;     while (l + EPS < r) {       if (++FTL >= 1000) break;       double mid = (l + r) * 0.5;       if (check(mid))         r = mid;       else         l = mid;     }     printf( %.10f , l);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int N, K, i;     cin >> N >> K;     if (K == 1 || (N <= 3) || (K > 3) || (N == 4 and K == 2))       return cout << -1 << endl, 0;     if (K == 2) {       cout << N - 1 << endl;       for (i = 1; i <= N - 1; i++) cout << i <<     << i + 1 << endl;     } else {       cout << 3 + (N - 4) * (2) << endl;       cout <<  1 2  << endl;       cout <<  2 3  << endl;       cout <<  3 4  << endl;       for (i = 5; i <= N; i++)         cout << i <<   2  << endl, cout << i <<   4  << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     int w, h;     cin >> w >> h >> n;     multiset<int> cuth, cutw, maxh, maxw;     cuth.insert(0);     cuth.insert(h);     cutw.insert(0);     cutw.insert(w);     maxh.insert(h);     maxw.insert(w);     int len;     for (int i = 0; i < n; i++) {       char str[10];       cin >> str;       cin >> len;       if (str[0] ==  H ) {         multiset<int>::iterator it, it1, it2;         it = cuth.lower_bound(len);         it1 = it;         it1--;         int val = *it - *it1;         it2 = maxh.lower_bound(val);         maxh.erase(it2);         cuth.insert(len);         maxh.insert((len - *it1));         maxh.insert((*it - len));       } else {         multiset<int>::iterator it, it1, it2;         it = cutw.lower_bound(len);         it1 = it;         it1--;         int val = *it - *it1;         it2 = maxw.lower_bound(val);         maxw.erase(it2);         cutw.insert(len);         maxw.insert((len - *it1));         maxw.insert((*it - len));       }       long long int l1 = *(maxw.rbegin());       long long int l2 = *(maxh.rbegin());       long long int ans = l1 * l2;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[111][111], x[111], k;   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) cin >> a[i][j];     int in = 0;     for (int i = 0; i < n; i++)       if (a[i][0] < a[in][0]) in = i;     for (int i = 0; i < n; i++) x[i] = a[i][0] - a[in][0];     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if (a[in][j] + x[i] < a[i][j]) {           cout <<  NO  << endl;           return 0;         } else if (a[in][j] + x[i] > a[i][j]) {           if (k && (a[in][j] + x[i]) % k != a[i][j]) {             cout <<  NO  << endl;             return 0;           }           k = a[in][j] + x[i] - a[i][j];         }     if (!k) k = 2e9;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++)         if ((a[in][j] + x[i]) % k != a[i][j]) {           cout <<  NO  << endl;           return 0;         }     cout <<  YES  << endl;     cout << k << endl;     for (int i = 0; i < n; i++) cout << x[i] <<    ;     cout << endl;     for (int j = 0; j < m; j++) cout << a[in][j] <<    ;     cout << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   #define mod 1000000007   #define int long long   #define double long double   #define pb push_back   #define mp make_pair   #define endl   n    #define all(x) x.begin(),x.end()   #define fill(a,b) memset(a,b,sizeof(a))   #define sz(x) (int)x.size()   #define ff first   #define ss second   #define lb lower_bound   #define ub upper_bound   #define bs binary_search   #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))   #define deb(x) cout<<#x<<   <<x<<endl;   #define T int t; cin>>t; while(t--)   #define fst ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);      int firstMissingPositive(int arr[], int n)   {               for (int i = 0; i < n; i++) {              while (arr[i] >= 1 && arr[i] <= n                  && arr[i] != arr[arr[i] - 1]) {               swap(arr[i], arr[arr[i] - 1]);           }       }          for (int i = 0; i < n; i++) {           if (arr[i] != i + 1) {               return i + 1;           }       }             return n + 1;   }      signed main()   {          fst;      #ifndef ONLINE_JUDGE       freopen( input3.txt ,  r , stdin);       freopen( output3.txt ,  w , stdout);   #endif                 T{           int n,p,q, m,h, k, sum = 0, cnt=0, x, y, z, flag = 0, pos, diff,r=0,l=0,u=0,d=0,cnt1=0,cnt0=0,cnt2=0,ans,mi=INT_MAX,mx=INT_MIN;           cin>>n;                      int a[n];           vector<int> v,v1,v2;           set<int> st;           map<int,int> mp;              rep(i,0,n) {               cin>>x;               if(find(all(st),x) != st.end())                 v.pb(x);               st.insert(x);           }              for(auto i:st) cout<<i<<   ;           sort(all(v));              for(auto i:v) cout<<i<<   ;                 cout<<endl;              v.clear();           st.clear();          }   }


#include <bits/stdc++.h>   using namespace std;   int n, m;   int w;   int q;   vector<int> adj[5000 + 1];   vector<pair<int, int>> store[5000 + 1];   bool vis[5000 + 1];   int dfs(int g, int r, int a) {     for (int i = 1; i <= n; i++) vis[i] = false;     queue<pair<int, int>> q;     priority_queue<pair<int, int>> pq;     q.push({g, 0});     long long sz = 0;     long long cost = 0;     while (!q.empty()) {       auto loc = q.front();       q.pop();       if (vis[loc.first]) continue;       vis[loc.first] = true;       for (auto p : store[loc.first]) {         sz += p.second;         cost += 1LL * p.first * p.second;         pq.push({p.first, p.second});       }       while (!pq.empty()) {         if (sz <= r) break;         auto p = pq.top();         pq.pop();         sz -= p.second;         cost -= 1LL * p.first * p.second;         if (sz < r) {           pq.push({p.first, r - sz});           cost += 1LL * p.first * (r - sz);           sz = r;         }       }       if (sz == r && cost <= a) {         return loc.second;       }       for (int e : adj[loc.first]) {         if (!vis[e]) {           q.push({e, loc.second + 1});         }       }     }     return -1;   }   int main() {     cin.tie(0)->sync_with_stdio(0);     cin >> n >> m;     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       adj[a].push_back(b);       adj[b].push_back(a);     }     cin >> w;     for (int i = 0; i < w; i++) {       int c, k, p;       cin >> c >> k >> p;       store[c].push_back({p, k});     }     cin >> q;     for (int i = 0; i < q; i++) {       int g, r, a;       cin >> g >> r >> a;       int ans = dfs(g, r, a);       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string to_string(string s) { return     + s +    ; }   string to_string(const char* s) { return to_string((string)s); }   string to_string(bool b) { return (b ?  true  :  false ); }   template <typename A, typename B>   string to_string(pair<A, B> p) {     return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ;   }   template <typename A>   string to_string(A v) {     bool first = true;     string res =  { ;     for (const auto& x : v) {       if (!first) {         res +=  ,  ;       }       first = false;       res += to_string(x);     }     res +=  } ;     return res;   }   void debug_out() { cerr << endl; }   template <typename Head, typename... Tail>   void debug_out(Head H, Tail... T) {     cerr <<     << to_string(H);     debug_out(T...);   }   void test_case() {     int n, l, r;     cin >> n >> l >> r;     vector<int> mn(n + 1);     int rem = l - 1;     for (int i = rem + 1; i <= n; ++i) {       mn[i] = 1;     }     for (int i = rem, nxt = 2; i >= 1; --i, nxt *= 2) {       mn[i] = nxt;     }     vector<int> mx(n + 1);     for (int i = 1, unq = 0, nxt = 1; i <= n; ++i) {       mx[i] = nxt;       ++unq;       if (unq < r) {         nxt *= 2;       }     }     cout << accumulate(mn.begin(), mn.end(), 0) <<              << accumulate(mx.begin(), mx.end(), 0) <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     test_case();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1000005;   using ll = long long;   int n, m, p, a[N];   struct TNode {     int l, r;     vector<ll> f;     ll sum;   } t[N << 2];   inline int lc(int pos) { return pos << 1; }   inline int rc(int pos) { return pos << 1 | 1; }   void pushup(int pos) {     t[pos].sum = t[lc(pos)].sum + t[rc(pos)].sum;     int ptr = 0;     for (int i = 1; i <= t[pos].r - t[pos].l + 1; i++)       t[pos].f[i] = (LLONG_MAX >> 2);     for (int i = 0; i <= t[lc(pos)].r - t[lc(pos)].l + 1; i++) {       if (ptr > t[rc(pos)].r - t[rc(pos)].l + 1) --ptr;       while (ptr <= t[rc(pos)].r - t[rc(pos)].l + 1) {         ll ret = t[rc(pos)].f[ptr] - (t[lc(pos)].sum - i * 1ll * p);         ll val = t[lc(pos)].f[i + 1] - 1 + (t[lc(pos)].sum - i * 1ll * p);         if (val < t[rc(pos)].f[ptr]) {           if (ptr > 0) --ptr;           break;         }         t[pos].f[i + ptr] = min(t[pos].f[i + ptr], max(t[lc(pos)].f[i], ret));         ++ptr;       }     }   }   void build(int pos, int l, int r) {     t[pos].l = l;     t[pos].r = r;     t[pos].f.resize(r - l + 1 + 1);     t[pos].f[0] = LLONG_MIN >> 2;     t[pos].f.push_back(LLONG_MAX >> 2);     if (l == r) {       t[pos].sum = a[l];       t[pos].f[1] = p - a[l];       return;     }     int mid = (l + r) >> 1;     build(lc(pos), l, mid);     build(rc(pos), mid + 1, r);     pushup(pos);   }   ll query(int pos, int l, int r, ll cur = 0) {     if (t[pos].l == l && t[pos].r == r) {       assert(cur < LLONG_MAX / 4);       ll pp = upper_bound(t[pos].f.begin(), t[pos].f.end(), cur) -               t[pos].f.begin() - 1;       return cur + t[pos].sum - p * pp;     }     int mid = (t[pos].l + t[pos].r) >> 1;     if (r <= mid)       return query(lc(pos), l, r, cur);     else if (l > mid)       return query(rc(pos), l, r, cur);     else       return query(rc(pos), mid + 1, r, query(lc(pos), l, mid, cur));   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m >> p;     for (int i = 1; i <= n; i++) cin >> a[i];     build(1, 1, n);     while (m--) {       int l, r;       cin >> l >> r;       cout << query(1, l, r) << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long wph;   long long mul(long long a, long long b) {     long long c = (a * b - (long long)((long double)a / wph * b) * wph);     return c < 0 ? c + wph : c;   }   void fwt(long long *a, int n, int f) {     for (int k = 2, mid = 1; k <= n; k <<= 1, mid <<= 1)       for (int i = 0; i < n; i += k)         for (int j = 0; j < mid; j++) {           long long x = a[i + j], y = a[i + mid + j];           a[i + j] = (x + y) % wph, a[i + mid + j] = (x - y + wph) % wph;         }     if (f)       for (int i = 0; i < n; i++) a[i] /= n;   }   long long c[2000010], b[2000010], f[2000010];   long long ksm(long long bs, long long mi) {     long long ans = 1;     while (mi) {       if (mi & 1) ans = mul(ans, bs);       bs = mul(bs, bs);       mi >>= 1;     }     return ans;   }   long long count(long long x) {     long long cnt = 0;     while (x) x &= (x - 1), cnt++;     return cnt;   }   int main() {     long long t;     int m, n;     scanf( %d%I64d%I64d , &m, &t, &wph);     n = 1 << m;     wph *= n;     for (int i = 0; i < n; i++) scanf( %I64d , &f[i]);     for (int i = 0; i <= m; i++) scanf( %I64d , &b[i]);     for (int i = 0; i < n; i++) c[i] = b[count(i)];     fwt(c, n, 0);     fwt(f, n, 0);     for (int i = 0; i < n; i++) c[i] = ksm(c[i], t);     for (int i = 0; i < n; i++) c[i] = mul(c[i], f[i]);     fwt(c, n, 1);     wph /= n;     for (int i = 0; i < n; i++) printf( %I64d n , c[i] % wph);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct Point {     double x, y;   };   struct Line {     double A, B, C;   };   Line Build(Point A, Point B) {     Line res;     res.A = A.y - B.y;     res.B = B.x - A.x;     res.C = A.x * B.y - B.x * A.y;     return res;   }   Line Per(Line l, Point A) {     Line res;     res.A = -l.B;     res.B = l.A;     res.C = -(res.A * A.x + res.B * A.y);     return res;   }   Line Par(Line l, Point A) {     Line res;     res.A = l.A;     res.B = l.B;     res.C = -(l.A * A.x + l.B * A.y);     return res;   }   Point Inter(Line a, Line b) {     Point res;     res.x = (a.B * b.C - b.B * a.C) / (a.A * b.B - b.A * a.B);     res.y = (a.A * b.C - b.A * a.C) / (b.A * a.B - b.B * a.A);     return res;   }   Point Middle(Point A, Point B) {     Point res;     res.x = (A.x + B.x) / 2.0;     res.y = (A.y + B.y) / 2.0;     return res;   }   double Dist(Point A, Point B) {     double res = sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));     return res;   }   double Dist2(Point A, Point B) {     double res = (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y);     return res;   }   Point Mir(Point A, Point B) {     Point res;     res.x = B.x * 2 - A.x;     res.y = B.y * 2 - A.y;     return res;   }   double Cp2(Point A, Point B) {     double res = A.x * B.y - A.y * B.x;     return res;   }   double Cp3(Point O, Point A, Point B) {     double res = (A.x - O.x) * (B.y - O.y) - (B.x - O.x) * (A.y - O.y);     return res;   }   bool Zero(double x) { return (fabs(x) < 1e-9 ? true : false); }   Line Midper(Point A, Point B) {     Line res = Per(Build(A, B), Middle(A, B));     return res;   }   bool Check(Point ans[]) {     double tmp;     for (int i = 0; i < 4; i++) {       if (Zero(Cp3(ans[i], ans[(i + 1) % 4], ans[(i + 2) % 4]))) return false;       if (i == 0)         tmp = Cp3(ans[i], ans[(i + 1) % 4], ans[(i + 2) % 4]);       else if (tmp * Cp3(ans[i], ans[(i + 1) % 4], ans[(i + 2) % 4]) < 0)         return false;     }     return true;   }   bool Mak(Point A, Point B, Point C) {     if (Cp3(A, B, C) > 1e-9) swap(B, C);     Line l1 = Midper(B, A);     Line l2 = Midper(B, C);     Point E = Inter(l1, l2);     Point F = Mir(E, B);     Point ans[4];     ans[0] = Inter(Par(l2, F), l1);     ans[1] = Mir(ans[0], B);     ans[2] = Mir(ans[1], C);     ans[3] = Mir(ans[0], A);     if (Check(ans) == false) return false;     puts( YES );     for (int i = 3; i < 7; i++) {       printf( %.9f %.9f , ans[i & 3].x, ans[i & 3].y);       putchar(i < 6 ? 32 : 10);     }     return true;   }   int main() {     int t;     scanf( %d , &t);     while (t--) {       Point a, b, c;       scanf( %lf%lf%lf%lf%lf%lf , &a.x, &a.y, &b.x, &b.y, &c.x, &c.y);       if (!Zero(Cp3(a, b, c)) && (Mak(a, b, c) || Mak(b, c, a) || Mak(c, a, b)))         continue;       puts( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int n, m;   pair<long long int, bool> All[120];   long long int ans = 0, na;   void GetAll() {     cin >> n >> m;     for (int i = 0; i < n; i++) {       cin >> All[i].first;       All[i].second = false;     }     int buf;     for (int i = 0; i < m; i++) {       cin >> buf;       All[buf - 1].second = true;     }   }   void Run() {     sort(All, All + n);     for (int i = 0; i < n; i++) {       if (!All[i].second) {         na += All[i].first;       }     }     for (int i = n - 1; i >= 0; i--) {       if (!All[i].second) {         continue;       }       if (All[i].first > na) {         na += All[i].first;       } else {         na += na;       }     }     cout << na;   }   int main() {     GetAll();     Run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   bool prime(long long int n) {     for (long long int i = 2; i * i <= n; i++) {       if (n % i == 0) return false;     }     return true;   }   long long binexpm(long long a, long long b, long long m) {     a %= m;     long long res = 1;     while (b > 0) {       if (b & 1) res = res * a % m;       a = a * a % m;       b >>= 1;     }     return res;   }   long long binexp(long long a, long long b) {     long long res = 1;     while (b > 0) {       if (b & 1) res = res * a;       a = a * a;       b >>= 1;     }     return res;   }   long long int n, p;   vector<long long int> adj[10001];   map<pair<long long int, long long int>, long long int> mp;   bool vis[10001] = {false};   map<pair<long long int, long long int>, long long int> res;   long long int x, y = -1;   long long int dm = INT_MAX;   bool xx = true;   long long int in[10001] = {0};   long long int out[10001] = {0};   bool dfs(long long int node) {     if (vis[node]) xx = false;     vis[node] = true;     for (auto child : adj[node]) {       dm = min(dm, mp[{node, child}]);       if (adj[child].size() == 0) y = child;       xx &= dfs(child);     }     return xx;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> p;     for (long long int i = 0; i < p; i++) {       long long int a, b, d;       cin >> a >> b >> d;       out[a]++;       in[b]++;       adj[a].push_back(b);       mp[make_pair(a, b)] = d;     }     long long int ans = 0;     for (long long int i = 1; i <= n; i++) {       bool flag;       dm = INT_MAX;       if (out[i] == 1 && in[i] == 0) {         x = i;         flag = dfs(i);         if (flag && y != -1) {           ans++;           res[make_pair(x, y)] = dm;         }       }     }     cout << ans <<   n ;     if (ans != 0) {       for (auto it : res) {         cout << it.first.first <<     << it.first.second <<     << it.second              <<   n ;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int h, i, j, x, l, r, pa[200200], pb[200200];   bool ans;   int main() {     scanf( %d , &h);     for (j = 0; j <= h; j++) {       scanf( %d , &x);       for (i = 1; i <= x; i++) {         pa[r + i] = l;         pb[r + i] = min(l + i - 1, r);         if (pa[r + i] != pb[r + i]) ans = true;       }       l = r + 1;       r = r + x;     }     if (ans) {       puts( ambiguous );       for (i = 1; i <= r; i++) printf( %d  , pa[i]);       puts(  );       for (i = 1; i <= r; i++) printf( %d  , pb[i]);       puts(  );     } else       puts( perfect );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     long long ans = 0;     for (int i = (0); i < (n); ++i) {       long long t, T, x, cost;       cin >> t >> T >> x >> cost;       if (t >= T) {         ans += cost + m * x;         continue;       }       long long aux1 = cost;       if (m > (T - t)) aux1 += m * x;       long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1;       aux2 *= cost;       ans += min(aux1, aux2);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, q;   int matrix[100][100];   int ans[10000];   void fill() {     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         matrix[i][j] = i * m + j;       }     }   }   void solve_1(int i) {     int temp;     temp = matrix[i][m - 1];     matrix[i][m - 1] = matrix[i][0];     for (int j = m - 2; j >= 0; --j) {       int new_temp = matrix[i][j];       matrix[i][j] = temp;       temp = new_temp;     }   }   void solve_2(int j) {     int temp;     temp = matrix[n - 1][j];     matrix[n - 1][j] = matrix[0][j];     for (int i = n - 2; i >= 0; --i) {       int new_temp = matrix[i][j];       matrix[i][j] = temp;       temp = new_temp;     }   }   int main(int argc, char *argv[]) {     memset(ans, 0, sizeof(ans));     scanf( %d%d%d , &n, &m, &q);     fill();     while (q--) {       int t;       scanf( %d , &t);       if (t == 1) {         int n_i;         scanf( %d , &n_i);         n_i--;         solve_1(n_i);       } else if (t == 2) {         int m_i;         scanf( %d , &m_i);         m_i--;         solve_2(m_i);       } else {         int r, c, x;         scanf( %d%d%d , &r, &c, &x);         r--;         c--;         ans[matrix[r][c]] = x;       }     }     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         printf( %d  , ans[i * m + j]);       }       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int d, s;   int E[2510050][10];   int Get(int x, int y) { return x * d + y; }   int dis[2710500];   char ok[2700050] = {};   vector<int> ans;   int ST, EN;   int dfs(int x) {     if (x == EN) return 1;     ok[x] = 1;     for (int u = 0; u < 10; u++)       if (x != 0 || u != 0) {         int v = E[x][u];         if (dis[v] == dis[x] + 1 && ok[v] == 0) {           ans.push_back(u);           if (dfs(v)) return 1;           ans.pop_back();         }       }     return 0;   }   int main() {     scanf( %d%d , &d, &s);     for (int i = 0; i <= s; i++)       for (int j = 0; j < d; j++) {         int x = Get(i, j);         for (int e = 0; e < 10; e++) {           if (i == 0 && j == 0 && e == 0) continue;           int ni = i + e;           int nj = (10 * j + e) % d;           if (ni <= s) {             int xx = Get(ni, nj);             E[x][e] = xx;           }         }       }     ST = Get(0, 0), EN = Get(s, 0);     vector<int> v;     v.push_back(ST);     memset(dis, -1, sizeof dis);     dis[ST] = 0;     for (int i = 0; i < (int)v.size(); i++) {       int t = v[i];       for (int u = 0; u < 10; u++)         if (t != 0 || u != 0) {           int w = E[t][u];           if (dis[w] == -1) {             dis[w] = dis[t] + 1;             v.push_back(w);           }         }     }     if (dis[EN] == -1)       puts( -1 );     else {       dfs(ST);       for (int e : ans) printf( %d , e);       puts(  );     }   }


#include <bits/stdc++.h>   using namespace std;   int n, i, tmp, res, a[10000];   bool kt;   int main() {     scanf( %d , &n);     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     kt = true;     for (i = 3; i <= n; i++)       if (a[i] - a[i - 1] != a[i - 1] - a[i - 2]) kt = false;     if (kt == false) {       printf( %d , a[n]);       return 0;     }     tmp = a[1] - a[2];     res = a[n] - tmp;     printf( %d , res);   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   inline bool Up(T &a, const T &b) {     if (a < b) {       a = b;       return true;     }     return false;   }   template <class T>   inline bool Down(T &a, const T &b) {     if (a > b) {       a = b;       return true;     }     return false;   }   const int MAXN = 100023;   int N, M, color[MAXN], ans[MAXN];   vector<int> fir[MAXN], side[2];   int tot = 2;   void BFS() {     static int Q[MAXN];     int head = 0, tail = 0;     memset(color, -1, sizeof(color));     for (int S = (1); S <= (N); ++S)       if (color[S] == -1)         for (color[S] = 0, Q[++tail] = S; head < tail;) {           int x = Q[++head], y;           side[color[x]].push_back(x);           for (vector<int>::iterator itr(fir[x].begin()); itr != fir[x].end();                ++itr)             if (color[y = *itr] == -1) {               Q[++tail] = y;               color[y] = color[x] ^ 1;             }         }   }   void Print() {     for (int c = (0); c <= (1); ++c)       for (vector<int>::iterator itr(side[c].begin()); itr != side[c].end();            ++itr) {         int x = *itr;         if (!ans[x]) ans[x] = ++tot;       }     puts( YES );     for (int i = (1); i <= (N); ++i)       printf( %d%c , ans[i] / 3, i == N ?   n  :    );   }   void Paint(int x) {     static int forbid[MAXN];     memset(forbid, 0, sizeof(forbid));     for (vector<int>::iterator itr(fir[x].begin()); itr != fir[x].end(); ++itr)       forbid[*itr] = 1;     int cnt = 0;     ans[x] = ++tot;     for (vector<int>::iterator itr(side[color[x] ^ 1].begin());          itr != side[color[x] ^ 1].end(); ++itr) {       int y = *itr;       if (!ans[y] && !forbid[y]) {         ans[y] = ++tot;         if (++cnt == 2) break;       }     }   }   int main() {     memset(ans, 0, sizeof(ans));     scanf( %d %d , &N, &M);     for (int i = (1); i <= (M); ++i) {       int a, b;       scanf( %d %d , &a, &b);       fir[a].push_back(b);       fir[b].push_back(a);     }     BFS();     if (side[0].size() % 3 == 0) {       Print();       return 0;     }     if (side[0].size() % 3 == 2) {       side[0].swap(side[1]);       for (int i = (1); i <= (N); ++i) color[i] ^= 1;     }     for (vector<int>::iterator itr(side[0].begin()); itr != side[0].end();          ++itr) {       int x = *itr;       if (fir[x].size() + 2 <= side[1].size()) {         Paint(x);         Print();         return 0;       }     }     int cnt = 0;     for (vector<int>::iterator itr(side[1].begin()); itr != side[1].end();          ++itr) {       int x = *itr;       if (fir[x].size() + 2 <= side[0].size()) {         Paint(x);         if (++cnt == 2) {           Print();           return 0;         }       }     }     puts( NO );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   vector<pair<int, int> > a;   set<pair<int, int> > d;   void solve(int l, int r) {     if (r - l <= 1) return;     int m = (l + r) / 2;     solve(l, m);     solve(m, r);     for (int i = l; i < r; ++i) {       d.insert({a[m].first, a[i].second});     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     a.resize(n);     for (int i = 0; i < n; ++i) {       cin >> a[i].first >> a[i].second;       d.insert(a[i]);     }     sort(a.begin(), a.end());     solve(0, n);     cout << d.size() <<   n ;     for (auto i : d) {       cout << i.first <<     << i.second <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   vector<int> g[300011];   long long ans[300011];   struct P {     long long dis = -1, val = 0;   } temp;   vector<P> m[300011];   map<long long, long long> bit;   void add(long long i, long long val) {     i = 1000000000000000 - i;     for (; i < 1000000000000000; i += (i & (-i))) bit[i] += val;   }   long long get(long long i) {     long long ret = 0;     i = 1000000000000000 - i;     for (; i > 0; i -= (i & (-i))) ret += bit[i];     return ret;   }   void dfs(int u, int par = 0, long long lev = 1) {     for (auto i : m[u]) add(lev + i.dis, i.val);     ans[u] = get(lev);     for (auto v : g[u]) {       if (v != par) dfs(v, u, lev + 1);     }     for (auto i : m[u]) add(lev + i.dis, -i.val);   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, x, y, v, d;     cin >> n;     for (int i = 0; i < n - 1; i++) {       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);     }     int q;     cin >> q;     while (q--) {       cin >> v >> d >> x;       temp.dis = d;       temp.val = x;       m[v].push_back(temp);     }     dfs(1);     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void print(int a) { cout << a <<    ; }   void print(long long a) { cout << a <<    ; }   void print(double a) { cout << a <<    ; }   void print(string a) { cout << a <<    ; }   template <typename T1, typename T2>   void print(pair<T1, T2> a) {     print(a.first);     cout <<   :::  ;     print(a.second);   }   template <typename Container>   void print(const Container& c) {     for (auto it = c.begin(); it != c.end(); it++) {       print(*it);     }     cout << endl;   }   vector<long long> tot_comb(51, 0);   vector<long long> fac(51, 1);   int CALC_MAX = 0;   long long INF = 2e18;   long long get_fac(int x) {     if (x <= 0) return 1;     return fac[x];   }   long long mul(long long x, long long y) {     if (x >= INF || y >= INF || x * y >= INF || x * y < 0) return INF;     return x * y;   }   long long add(long long x, long long y) {     if (x >= INF || y >= INF || x + y >= INF) return INF;     return x + y;   }   void fill_fac() {     fac[0] = 1;     fac[1] = 1;     for (int i = 2; i <= 50; i++) {       fac[i] = mul(fac[i - 1], i);     }   }   void fill_tot_comb() {     tot_comb[0] = 1;     tot_comb[1] = 1;     tot_comb[2] = 2;     tot_comb[3] = 4;     for (int i = 4; i <= 50; i++) {       tot_comb[i] = 0;       for (int j = 1; j <= i; j++) {         tot_comb[i] = add(tot_comb[i], mul(get_fac(j - 2), tot_comb[i - j]));       }     }   }   void KthPermhelp(int k, long long n, int st) {     vector<int> perm(k + 1, 0);     vector<int> pos(k + 1, 0);     perm[1] = k;     pos[k] = 1;     long long cnt = n;     vector<bool> taken(k + 1, false);     taken[k] = true;     for (int i = 2; i <= k; i++) {       for (int j = 1; j < k; j++) {         if (!taken[j]) {           int jk = i;           while (taken[jk]) jk = pos[jk];           if (j == jk && i != k) continue;           long long tot_with_j_i = get_fac(k - 1 - i);           if (cnt > tot_with_j_i) {             cnt -= tot_with_j_i;           } else {             perm[i] = j;             taken[j] = true;             pos[j] = i;             break;           }         }       }     }     if (k == 2) {       perm[1] = 2;       perm[2] = 1;     }     for (int i = 1; i <= k; i++) cout << perm[i] + st <<    ;   }   void KthPerm(int n, long long k, int st) {     if (n == 0) return;     if (tot_comb[n] < k) {       cout << -1;       return;     }     for (int i = 1; i <= n; i++) {       if (mul(get_fac(i - 2), tot_comb[n - i]) >= k) {         KthPermhelp(i, (k / tot_comb[n - i]) + (k % tot_comb[n - i] > 0), st);         KthPerm(n - i,                 (k % tot_comb[n - i]) +                     ((k % tot_comb[n - i] == 0) ? tot_comb[n - i] : 0),                 st + i);         break;       }       k -= mul(get_fac(i - 2), tot_comb[n - i]);     }   }   int main() {     fill_fac();     fill_tot_comb();     int t;     cin >> t;     while (t--) {       int n;       long long int k;       cin >> n >> k;       KthPerm(n, k, 0);       cout << endl;     }   }


#include <bits/stdc++.h>      #define pb push_back   #define int long long    #define debug(n) cerr << #n <<   =   << n <<   n ;      using namespace std;      const int MaxN = 1e6 + 10;   const int Mod = 1e9 + 7;   const int inf = 1e18;   int cnt[100] , a[MaxN];      void solve() {    int n;    cin >> n;    for(int i = 0; i < n; i++) {     cin >> a[i];     for(int j = 0; j <= 60; j++)       if(a[i] & (1LL << j)) cnt[j]++;    }    int ans = 0;    for(int i = 0; i < n; i++) {     int x = 0 , y = 0;     for(int j = 0; j <= 60; j++) {      int p = (1LL << j) % Mod;      if(a[i] & (1LL << j)) {       x = (x + p * cnt[j]) % Mod;       y = (y + p * n) % Mod;      } else y = (y + p * cnt[j]) % Mod;     }     ans = (ans + x * y) % Mod;    }    cout << ans <<   n ;    fill(cnt , cnt + 100 , 0);   }      int32_t main() {    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);    int t = 1;    cin >> t;    while(t--)      solve();    return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mo = 998244353;   int fac[300005], inv[300005];   void init() {     fac[0] = inv[0] = inv[1] = 1;     for (int i = (int)(2); i <= (int)(300005 - 1); i++)       inv[i] = 1ll * inv[mo % i] * (mo - mo / i) % mo;     for (int i = (int)(1); i <= (int)(300005 - 1); i++) {       fac[i] = 1ll * fac[i - 1] * i % mo;       inv[i] = 1ll * inv[i - 1] * inv[i] % mo;     }   }   int power(int x, int y) {     int s = 1;     for (; y;) {       if (y & 1) s = 1ll * s * x % mo;       if (y /= 2) x = 1ll * x * x % mo;     }     return s;   }   const int N = 100005;   int freq[N], cnt[N];   void add(int x) {     cnt[freq[x]]--;     cnt[++freq[x]]++;   }   void del(int x) {     cnt[freq[x]]--;     cnt[--freq[x]]++;   }   vector<int> heavy;   int n, m, Q;   int e[N], ans[N];   struct que {     int l, r, k, id;   } q[N];   const int blk = 200;   bool cmpk(que a, que b) {     return pair<int, int>(a.k, a.l - a.r) < pair<int, int>(b.k, b.l - b.r);   }   bool cmpmo(que a, que b) {     return pair<int, int>(a.l / blk, a.r) < pair<int, int>(b.l / blk, b.r);   }   int main() {     init();     scanf( %d%d%d , &n, &m, &Q);     for (int i = (int)(1); i <= (int)(n); i++) {       scanf( %d , &e[i]);       cnt[e[i]]++;     }     for (int i = (int)(1); i <= (int)(m); i++)       if (cnt[i] >= blk) heavy.push_back(i);     for (int i = (int)(1); i <= (int)(Q); i++) {       scanf( %d%d%d , &q[i].l, &q[i].r, &q[i].k);       q[i].id = i;     }     sort(q + 1, q + Q + 1, cmpk);     for (int r = 1; r <= Q;) {       int l = r, kk = q[l].k, res = 1, las = n, v = 1;       for (; r <= Q && kk == q[r].k; r++)         ;       for (int i = (int)(1); i <= (int)(m); i++)         res = 1ll * res * fac[kk + cnt[i]] % mo;       for (int i = (int)(l); i <= (int)(r - 1); i++) {         int len = q[i].r - q[i].l + 1;         for (; las > len; v = 1ll * (1ll * m * kk + n - (--las)) % mo * v % mo)           ;         ans[q[i].id] = 1ll * res * v % mo;       }     }     sort(q + 1, q + Q + 1, cmpmo);     int l = 1, r = 0;     memset(cnt, 0, sizeof(cnt));     ;     cnt[0] = m;     for (int i = (int)(1); i <= (int)(n); i++) add(e[i]);     for (int i = (int)(1); i <= (int)(Q); i++) {       for (; q[i].l < l; del(e[--l]))         ;       for (; q[i].l > l; add(e[l++]))         ;       for (; q[i].r < r; add(e[r--]))         ;       for (; q[i].r > r; del(e[++r]))         ;       int res = 1;       for (int j = (int)(0); j <= (int)(blk - 1); j++)         if (cnt[j]) res = 1ll * res * power(inv[q[i].k + j], cnt[j]) % mo;       for (int j : heavy)         if (freq[j] >= blk) res = 1ll * res * inv[q[i].k + freq[j]] % mo;       ans[q[i].id] = 1ll * ans[q[i].id] * res % mo;     }     for (int i = (int)(1); i <= (int)(Q); i++) printf( %d n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   const int N = (int)1e6 + 7;   const int MOD = (int)1e9 + 7;   const long long INF = (long long)1e18 + 7;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     vector<int> p(n + 1, 0);     for (int i = 2; i * i <= n; i++)       if (p[i] == 0) {         p[i] = i;         for (int j = i * i; j <= n; j += i)           if (!p[j]) p[j] = i;       }     for (int i = 1; i <= n; i++)       if (!p[i]) p[i] = i;     vector<int> ans;     for (int i = 1; i <= n; i++) ans.push_back(i / p[i]);     sort(ans.begin(), ans.end());     for (int i = 1; i < n; i++) cout << ans[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k, p;     double s, n;     cin >> k >> n >> s >> p;     for (int i = 0; i < 10000000000000000; i++) {       if (((i * p) / k) >= n / s) {         cout << i;         break;       }     }     return 0;   }


#include<bits/stdc++.h>   using namespace std;   #define    ll    long long int   #define pi              2*acos(0.0)   #define gcd(a,b)        __gcd(a,b)   #define lcm(a,b)        (a*b)/gcd(a,b)   map<ll,ll>mp,mk;   //map<string,ll>mp;   set<ll> s1,s2;   vector<int> v,w;   string s=  ,qq=  ,ss=  ,pp=  ;   char ch;   ll m,n,i,j,l,r,x,t,y,u,g,h,mn,mx,d,z,c,f,e,k,o;   int main ()   {       cin>>t;       while(t--)       {           cin>>n>>k;           for(i=0; i<k-3; i++)               cout<< 1  ;           n=n-k+3;           c=n-2;           o=n/2;           if(n%2)               cout<<n/2<<   <<n/2<<   <<1;           else if(o%2==0)               cout<<n/2<<   <<o/2<<   <<o/2;           else if(o%2)               cout<<c/2<<   <<c/2<<   <<2;           cout<<  n ;       }   }


#include <bits/stdc++.h>   int dx[] = {0, 0, 1, -1, 2, 2, -2, -2, 1, -1, 1, -1};   int dy[] = {1, -1, 0, 0, 1, -1, 1, -1, 2, 2, -2, -2};   using namespace std;   vector<long long int> V, V2, V3, edj[200010];   bitset<200010> color;   long long int root, cnt = 0;   void dfs(long long int n, long long int ck) {     color[n] = true;     for (long long int i : edj[n]) {       if (color[i] == false) {         color[i] = true;         long long int tm = ck ^ 1;         if (tm == 0) {           cnt++;         }         dfs(i, tm);       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int tcase = 1;     for (long long int test = 1; test <= tcase; test++) {       long long int n;       cin >> n;       for (long long int i = 0; i < n - 1; i++) {         long long int u, v;         cin >> u >> v;         edj[u].push_back(v);         edj[v].push_back(u);       }       for (long long int i = 1; i <= n; i++) {         if ((long long int)edj[i].size() == 1) {           root = i;           break;         }       }       dfs(root, 0);       long long int ans = cnt;       cnt = 0;       for (long long int i = 0; i <= n; i++) color[i] = false;       root = edj[root][0];       dfs(root, 0);       ans = min(ans, cnt);       cout << ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const double pi = 3.141592653589793238463;   int main() {     cin.tie(0);     cin.sync_with_stdio(0);     long long a, b;     cin >> a >> b;     while (a != 0 && b != 0) {       if (a < 2 * b && b < 2 * a) break;       if (a >= 2 * b)         a %= 2 * b;       else         b %= 2 * a;     }     cout << a <<     << b <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, l, n, k, d, max = 0, gen, e = 0;     long long p;     scanf( %d %d , &n, &k);     vector<vector<int> > v(n);     for (i = 0; i < n; i++) {       scanf( %d , &d);       v[d].push_back(i);       if (d > max) max = d;     }     p = 1;     for (i = 0; i <= max; i++) {       gen = v[i].size();       if (i > 0) e += gen;       if (gen > p || gen <= 0) break;       p = gen;       if (i == 0)         p = k;       else         p = p * (k - 1);     }     if (i <= max)       printf( -1 n );     else {       printf( %d n , e);       for (i = 0; i < max; i++) {         gen = 0;         if (i == 0)           e = k;         else           e = k - 1;         for (j = 0; j < (int)(v[i].size()); j++) {           for (l = gen; l < gen + e && l < (int)(v[i + 1].size()); l++) {             printf( %d %d n , v[i][j] + 1, v[i + 1][l] + 1);           }           gen += e;           if (gen >= v[i + 1].size()) break;         }       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 7;   int n, m, q;   vector<int> E[maxn];   int op[maxn], a[maxn], b[maxn], c[maxn];   int ans[maxn];   bitset<1001> Bit[1001];   bitset<1001> C;   void dfs(int x) {     if (x == 0) {       for (int i = 0; i < E[x].size(); i++) {         ans[E[x][i]] = ans[x];         dfs(E[x][i]);       }     }     if (op[x] == 1) {       int flag = 0;       if (!Bit[a[x]][b[x]]) {         flag = 1;         ans[x]++;         Bit[a[x]][b[x]] = 1;       }       for (int i = 0; i < E[x].size(); i++) {         ans[E[x][i]] = ans[x];         dfs(E[x][i]);       }       if (flag) Bit[a[x]][b[x]] = 0;     }     if (op[x] == 2) {       int flag = 0;       if (Bit[a[x]][b[x]]) {         flag = 1;         ans[x]--;         Bit[a[x]][b[x]] = 0;       }       for (int i = 0; i < E[x].size(); i++) {         ans[E[x][i]] = ans[x];         dfs(E[x][i]);       }       if (flag) Bit[a[x]][b[x]] = 1;     }     if (op[x] == 3) {       ans[x] = ans[x] - Bit[a[x]].count();       Bit[a[x]] ^= C;       ans[x] = ans[x] + Bit[a[x]].count();       for (int i = 0; i < E[x].size(); i++) {         ans[E[x][i]] = ans[x];         dfs(E[x][i]);       }       Bit[a[x]] ^= C;     }     if (op[x] == 4) {       for (int i = 0; i < E[x].size(); i++) {         ans[E[x][i]] = ans[x];         dfs(E[x][i]);       }     }   }   int main() {     scanf( %d%d%d , &n, &m, &q);     for (int i = 1; i <= m; i++) C[i] = 1;     for (int i = 1; i <= q; i++) {       scanf( %d , &op[i]);       if (op[i] == 1) scanf( %d%d , &a[i], &b[i]);       if (op[i] == 2) scanf( %d%d , &a[i], &b[i]);       if (op[i] == 3) scanf( %d , &a[i]);       if (op[i] == 4)         scanf( %d , &a[i]), E[a[i]].push_back(i);       else         E[i - 1].push_back(i);     }     dfs(0);     for (int i = 1; i <= q; i++) printf( %d n , ans[i]);   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, c, i, l, j, x, s, y, ans;   int main() {     cin >> a >> b >> c >> l;     s = 0;     for (i = l + 1; i >= 1; i--) s += i * (i + 1) / 2;     ans = 0;     for (i = 0; i <= l; i++) {       x = a + i;       if (b > x || c > x) continue;       y = min(l - i, x - b - c) + 1;       if (y < 0) y = 0;       ans += (1 + y) * y / 2;     }     for (i = 0; i <= l; i++) {       x = b + i;       if (a > x || c > x) continue;       y = min(l - i, x - a - c) + 1;       if (y < 0) y = 0;       ans += (1 + y) * y / 2;     }     for (i = 0; i <= l; i++) {       x = c + i;       if (a > x || b > x) continue;       y = min(l - i, x - a - b) + 1;       if (y < 0) y = 0;       ans += (1 + y) * y / 2;     }     cout << s - ans;     return 0;   }