#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5005, MAXC = 1000005;   int a[MAXN], b[MAXC], c[MAXC];   int n, k, _max;   int main() {     if (0) {       freopen( input.txt ,  r , stdin);       freopen( output.txt ,  w , stdout);     };     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), _max = max(_max, a[i]);     for (int i = 1; i < n; i++)       for (int j = i + 1; j <= n; j++) b[abs(a[i] - a[j])]++;     for (int res = n - k; res <= 1000001; res++) {       int cnt = 0;       bool ok = true;       for (int i = res; i <= _max; i += res) {         cnt += b[i];         if (cnt > (k + 1) * k / 2) {           ok = false;           break;         }       }       if (!ok) continue;       int x = 0;       for (int i = 1; i <= n; i++) {         if (c[a[i] % res]) x++;         c[a[i] % res]++;       }       if (x <= k) {         cout << res << endl;         return 0;       }       for (int i = 1; i <= n; i++) c[a[i] % res] = 0;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxnum = 2005;   char str[maxnum][maxnum];   int state[maxnum];   int main() {     int m, n;     while (~scanf( %d%d , &n, &m)) {       memset(state, 0, sizeof(state));       for (int i = 0; i < n; ++i) {         scanf( %s , str[i]);         for (int j = 0; j < m; ++j) {           if (str[i][j] ==  1 ) state[j] +=  1 ;         }       }       bool ans = false;       for (int i = 0; i < n; ++i) {         bool judge = true;         for (int j = 0; j < m; ++j) {           if (judge == false) break;           if (str[i][j] ==  1 ) {             judge = state[j] -  1 ;           }           if (str[i][j] ==  0 ) {             judge = state[j];           }         }         if (judge) {           ans = true;           break;         }       }       if (ans)         printf( YES n );       else         printf( NO n );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int to, next, w;     edge(int _to = 0, int _next = 0, int _w = 0) : to(_to), next(_next), w(_w) {}   } e[300010 << 1];   int n, m;   int g[300010], nume = 0;   int d[300010];   int f[300010][2];   vector<int> ans;   namespace LCT {   int f[300010];   void init() {     for (int i = 1; i <= n; i++) f[i] = i;   }   int getf(int x) {     if (f[x] == x) return x;     return f[x] = getf(f[x]);   }   int merge(int x, int y) { f[f[x]] = f[y]; }   }  // namespace LCT   void addEdge(int u, int v, int w) {     e[nume] = edge(v, g[u], w);     g[u] = nume++;   }   void dfs(int x, int p) {     for (int i = g[x]; ~i; i = e[i].next)       if (e[i].to ^ p) dfs(e[i].to, x);     int numch = 0;     static int ch[300010][2];     int c0 = 0, c1 = 0;     for (int i = g[x]; ~i; i = e[i].next)       if (e[i].to ^ p) {         ++numch;         ch[numch][0] = f[e[i].to][0];         ch[numch][1] = f[e[i].to][1];         if (!ch[numch][0] && !ch[numch][1]) {           f[x][0] = f[x][1] = 0;           return;         }         if (ch[numch][0] && ch[numch][1]) {           f[x][0] = f[x][1] = 1;           return;         }         if (ch[numch][0]) c0++;         if (ch[numch][1]) c1++;       }     if (d[x] == -1) {       f[x][0] = f[x][1] = 1;       return;     }     if (d[x] == 0) {       if (c1 & 1) {         f[x][1] = 1;         f[x][0] = 0;       } else {         f[x][1] = 0;         f[x][0] = 1;       }     } else {       if (c1 & 1) {         f[x][1] = 0;         f[x][0] = 1;       } else {         f[x][1] = 1;         f[x][0] = 0;       }     }   }   void dfs2(int x, int p, int fx) {     if (f[x][0] != f[x][1]) {       for (int i = g[x]; ~i; i = e[i].next)         if (e[i].to ^ p)           if (f[e[i].to][0]) {             dfs2(e[i].to, x, 0);           } else {             ans.push_back(e[i].w);             dfs2(e[i].to, x, 1);           }     } else {       int temp = -1, c1 = 0;       for (int i = g[x]; ~i; i = e[i].next)         if (e[i].to ^ p) {           if (f[e[i].to][0] == f[e[i].to][1] && temp == -1) {             temp = i;           } else {             if (f[e[i].to][0])               dfs2(e[i].to, x, 0);             else {               dfs2(e[i].to, x, 1);               ans.push_back(e[i].w);               c1++;             }           }         }       if (temp == -1) return;       if ((c1 & 1) == fx ^ d[x])         dfs2(e[temp].to, x, 0);       else {         dfs2(e[temp].to, x, 1);         ans.push_back(e[temp].w);       }     }   }   int main() {     memset(g, -1, sizeof g);     scanf( %d%d , &n, &m);     LCT::init();     for (int i = 1; i <= n; i++) scanf( %d , d + i);     for (int i = 1; i <= m; i++) {       int u, v;       scanf( %d%d , &u, &v);       if (LCT::getf(u) ^ LCT::getf(v)) {         addEdge(u, v, i);         addEdge(v, u, i);         LCT::merge(u, v);       }     }     dfs(1, 0);     if (!f[1][0]) {       puts( -1 );       return 0;     }     dfs2(1, 0, 0);     int numans = ans.size();     printf( %d n , numans);     for (int i = 0; i < numans; i++) printf( %d n , ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int dp[360361];   long long int k;   bool reach(long long int from, long long int to) {     if (from <= to) return false;     long long int d = from - to;     if (d == 1) return true;     for (long long int i = (long long int)(from - to + 1);          i < ((long long int)(k + 1)); i++)       if (to % i == 0 && to + i > from) return true;     return false;   }   long long int calc(long long int from, long long int to) {     for (long long int i = (long long int)(from); i < ((long long int)(to + 1));          i++)       dp[i] = 999999;     dp[from] = 0;     for (long long int i = (long long int)(from + 1);          i < ((long long int)(to + 1)); i++) {       for (long long int j = i - 1; j >= max(from, i - k + 1); j--) {         if (reach(i, j)) dp[i] = min(dp[i], dp[j] + 1);       }     }     return dp[to];   }   int main() {     long long int a, b;     cin >> a >> b >> k;     a -= 360360 * (b / 360360);     b = b % 360360;     if (a < 360360) {       cout << calc(b, a) << endl;     } else {       long long int res = 0;       res += calc(b, 360360);       long long int nums = (a - (a % 360360) - 360360) / 360360;       res += calc(0, 360360) * nums;       res += calc(0, a % 360360);       cout << res << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long INF = 10000000000;   const int maxn = 5010;   long long a[maxn];   long long last[maxn];   int dp[maxn];   long long s[maxn];   int n;   int main(int argc, char *argv[]) {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]);     fill(last, last + maxn, INF);     fill(dp, dp + maxn, maxn);     s[0] = 0;     for (int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];     dp[0] = 0;     last[0] = 0;     for (int i = 1; i <= n; i++) {       for (int j = 0; j < i; j++) {         if (s[i] - s[j] >= last[j]) {           if (dp[i] >= dp[j] + i - j - 1) {             dp[i] = dp[j] + i - j - 1;             last[i] = min(last[i], s[i] - s[j]);           }         }       }     }     cout << dp[n] << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int abso(int a, int b) {     if (a < b)       return b - a;     else       return a - b;   }   long long power(long long x, long long e) {     long long temp;     if (e == 0) return 1;     if (e % 2 == 0) {       temp = power(x, e / 2);       return temp * temp;     } else {       temp = power(x, e / 2);       return temp * temp * x;     }   }   bool cmp(pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) {     if (x.first.first == y.first.first) {       if (x.first.second == y.first.second)         return x.second < y.second;       else         return x.first.second > y.first.second;     }     return x.first.first > y.first.first;   }   bool cmp1(pair<int, int> x, pair<int, int> y) {     if (x.second == y.second) return x.first < y.first;     return x.second < y.second;   }   int main() {     int n;     cin >> n;     string str;     cin >> str;     int arr[n + 1];     for (int i = 1; i <= n; i++) cin >> arr[i];     int cur = 1;     int visit[n + 1];     memset(visit, 0, sizeof visit);     int flag = 0, next;     visit[cur] = 1;     while (1) {       if (str[cur - 1] ==  < ) {         next = cur - arr[cur];         if (next < 1) {           flag = 1;           break;         } else {           if (visit[next] == 1) {             flag = 2;             break;           } else {             visit[next] = 1;             cur = next;           }         }       } else {         next = cur + arr[cur];         if (next > n) {           flag = 1;           break;         } else {           if (visit[next] == 1) {             flag = 2;             break;           } else {             visit[next] = 1;             cur = next;           }         }       }     }     if (flag == 1)       cout <<  FINITE  << endl;     else       cout <<  INFINITE  << endl;     return 0;   }


#include <bits/stdc++.h>   int n, m, q;   std::vector<int> dp;   std::vector<std::pair<int, int> > edges;   struct bip {     std::vector<int> pers;     std::stack<std::pair<int, int> > updates;     std::stack<int> change;     void update(int i, int j) {       updates.push({i, pers[i]});       pers[i] = j;     }     int root(int u) {       while (pers[(u + 1)] >= 0) u = pers[(u + 1)];       return u;     }     void merge(int u, int v) {       if (!pers[(0)]) {         change.push(0);         return;       }       u = root(u);       v = root(v);       if (pers[(n + 1 + u)] == v) {         change.push(0);         return;       }       if (u == v) {         update((0), 0);         change.push(1);         return;       }       int c = 0;       int eu = pers[(n + 1 + u)];       int ev = pers[(n + 1 + v)];       if (ev != -1) {         if (pers[(ev + 1)] < pers[(u + 1)]) u ^= ev ^= u ^= ev;         update((u + 1), pers[(ev + 1)] + pers[(u + 1)]);         c++;         update((ev + 1), u);         c++;       }       if (eu != -1) {         if (pers[(eu + 1)] < pers[(v + 1)]) v ^= eu ^= v ^= eu;         update((v + 1), pers[(eu + 1)] + pers[(v + 1)]);         c++;         update((eu + 1), v);         c++;       }       update((n + 1 + u), v);       c++;       update((n + 1 + v), u);       c++;       change.push(c);     }     void undo() {       int u = change.top();       change.pop();       while (u--) {         std::pair<int, int> next = updates.top();         updates.pop();         pers[next.first] = next.second;       }     }   } graph;   void solve(int l, int r, int tl, int tr) {     int mid = (l + r) / 2;     for (int i = l; i < mid; i++) {       graph.merge(edges[i].first, edges[i].second);     }     int opt = tr;     for (int i = tr; i > std::max(tl, mid); i--) {       graph.merge(edges[i].first, edges[i].second);       if (graph.pers[(0)]) opt = i - 1;     }     dp[mid] = opt;     for (int i = tr; i > std::max(tl, mid); i--) graph.undo();     dp[mid] = opt;     if (l == r) {       for (int i = l; i < mid; i++) graph.undo();       return;     }     graph.merge(edges[mid].first, edges[mid].second);     solve(mid + 1, r, opt, tr);     for (int i = mid; i >= l; i--) graph.undo();     if (l < mid) {       for (int i = tr; i > opt; i--) graph.merge(edges[i].first, edges[i].second);       solve(l, mid - 1, tl, opt);       for (int i = opt + 1; i <= tr; i++) graph.undo();     }   }   signed main() {     std::ios::sync_with_stdio(false);     std::cin.tie(0);     std::cin >> n >> m >> q;     graph.pers = std::vector<int>(2 * n + 1, -1);     graph.pers[(0)] = 1;     edges.resize(m);     int last = 0;     for (int i = 0; i < m; i++) {       std::cin >> edges[i].first >> edges[i].second;       edges[i].first--;       edges[i].second--;       if (graph.pers[(0)]) last = i;       graph.merge(edges[i].first, edges[i].second);     }     dp.resize(last + 1);     for (int i = 0; i < m; i++) graph.undo();     solve(0, last, 0, m - 1);     while (q--) {       int l, r;       std::cin >> l >> r;       l--;       r--;       if (l > last || r < dp[l])         std::cout <<  YES  << std::endl;       else         std::cout <<  NO  << std::endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, k, s = 0, k2 = 0, k1 = 0, z;     cin >> n >> m >> k;     z = k % 2;     while (true) {       if (s + 2 * m >= k) {         goto v;       } else {         s = s + 2 * m;         k2++;       }     }   v:;     cout << k2 + 1 <<    ;     while (true) {       if (s >= (k - 2))         goto z;       else {         s = s + 2;         k1++;       }     }   z:;     cout << k1 + 1 <<    ;     if (z == 0)       cout <<  R ;     else       cout <<  L ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, j, r;     char str[] = { R ,  O ,  Y ,  B ,  G ,  I ,  V };     cin >> n;     r = n % 7;     n -= r;     for (i = 0; i < n; i++) cout << str[i % 7];     for (i = 0; i < r; i++) cout << str[3 + i % 4];   }


#include <bits/stdc++.h>   using namespace std;   int dx[] = {0, 0, +1, -1};   int dy[] = {+1, -1, 0, 0};   int dxx[] = {+1, 0, -1, 0, +1, +1, -1, -1};   int dyy[] = {0, +1, 0, -1, +1, -1, +1, -1};   inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }   vector<pair<long long, long long> > ans;   int main() {     long long x, i, sz = 0, l, y;     scanf( %lld , &x);     l = min(x, 10000000LL);     for (i = 1; i <= l; i++) {       y = x * 6LL;       if (y % i) continue;       y /= i;       if (y % (i + 1)) continue;       y /= (i + 1);       y += (i - 1);       if (y % 3) continue;       y /= 3;       if (y < i) continue;       ans.push_back(make_pair(i, y));       if (i == y)         sz++;       else         sz += 2;     }     printf( %lld n , sz);     for (i = 0; i < ans.size(); i++)       printf( %lld %lld n , ans[i].first, ans[i].second);     for (i = ans.size() - 1; ~i; i--)       if (ans[i].first != ans[i].second)         printf( %lld %lld n , ans[i].second, ans[i].first);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long MAX = 4e6;   vector<long long> vec[1000000];   long long N, T, ok, cur = 2, arr[MAX], res[MAX], cnt[MAX], prime[MAX];   void cal() {     prime[0] = 1;     prime[1] = 1;     for (long long A = 2; A < MAX; A++)       if (!prime[A]) {         if (A < 1000000) vec[A].push_back(A);         for (long long B = A + A; B < MAX; B += A) {           prime[B] = 1;           if (B < 1000000) vec[B].push_back(A);         }       }     return;   }   signed main() {     ios_base::sync_with_stdio(false);     cal();     cin >> N;     for (long long A = 1; A <= N; A++) cin >> arr[A];     for (long long A = 1; A <= N; A++) {       if (ok == 0)         for (auto B : vec[arr[A]]) {           cnt[B]++;           if (cnt[B] > 1) ok = 1;         }       if (ok == 0)         res[A] = arr[A];       else if (ok == 1) {         for (auto B : vec[arr[A]]) cnt[B]--;         cur = arr[A] + 1;         while (true) {           T = 0;           for (auto B : vec[cur]) T += cnt[B];           if (T == 0) {             res[A] = cur;             for (auto B : vec[cur]) cnt[B]++;             break;           }           cur++;         }         cur = 2;         ok = 2;       } else {         while ((prime[cur]) or (cnt[cur])) cur++;         res[A] = cur++;       }     }     for (long long A = 1; A <= N; A++) cout << res[A] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   bool r[1010], c[1010];   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       a--;       b--;       r[a] = 1;       c[b] = 1;     }     int ans = 0;     for (int i = 1; i < n - 1; i++) {       if (!c[i]) ans++;       if (!r[i]) ans++;       if (!c[i] && !r[i] && !c[n - i] && !r[n - i] && (n - 1 - i) == i) ans--;     }     cout << ans << endl;   }


#include <bits/stdc++.h>   using namespace std;   long long int modinverse(long long int x, long long int y) {     if (y == 0) {       return 1;     }     long long int p = modinverse(x, y / 2) % 1000000007;     p = (p * p) % 1000000007;     return (y % 2 == 0) ? p : (x * p) % 1000000007;   }   long long int modexp(long long int a, long long int n) {     long long int ans = 1;     a = a % 1000000007;     while (n > 0) {       if (n % 2) {         ans = (ans * a) % 1000000007;       }       n /= 2;       a = (a * a) % 1000000007;     }     return ans;   }   long long int arr[300001], on[30], off[30];   void solve(vector<long long int> index, long long int k) {     if (k == -1 or index.size() <= 1) {       return;     }     vector<long long int> one, zero;     long long int cnt1 = 0, cnt2 = 0, inversion1 = 0, inversion2 = 0;     for (auto itr : index) {       if ((arr[itr] >> k) % 2 == 1) {         one.push_back(itr);         cnt1++;         inversion1 += cnt2;       } else {         zero.push_back(itr);         cnt2++;         inversion2 += cnt1;       }     }     on[k] += inversion1;     off[k] += inversion2;     solve(zero, k - 1);     solve(one, k - 1);   }   long long int countInversion(vector<long long int> index, long long int k) {     if (k == -1 or index.size() <= 1) {       return 0;     }     vector<long long int> zero, one;     long long int cnt = 0, inversion = 0;     for (auto itr : index) {       if ((arr[itr] >> k) % 2 == 1) {         one.push_back(itr);         cnt++;       } else {         zero.push_back(itr);         inversion += cnt;       }     }     inversion += countInversion(zero, k - 1);     inversion += countInversion(one, k - 1);     return inversion;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int tt = 1;     for (long long int tc = 1; tc <= tt; tc++) {       long long int n;       cin >> n;       for (long long int i = 0; i <= n - 1; i++) {         cin >> arr[i];       }       vector<long long int> v;       for (long long int i = 0; i <= n - 1; i++) {         v.push_back(i);       }       for (long long int i = 0; i <= 29; i++) {         on[i] = off[i] = 0;       }       solve(v, 29);       long long int x = 0;       for (long long int i = 0; i <= 29; i++) {         if (on[i] < off[i]) {           x += pow(2, i);         }       }       for (long long int i = 0; i <= n - 1; i++) {         arr[i] = (arr[i] ^ x);       }       long long int b = countInversion(v, 29);       cout << b <<     << x;       cout <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     for (int tmp = 0; tmp < t; tmp++) {       unsigned long long int n, k;       cin >> n >> k;       if (k >= n) {         cout << 1 <<   n ;         continue;       }       bool fl = false;       for (int i = 2; i * i <= n; i++) {         if (n % i == 0 && n / i <= k) {           cout << i <<   n ;           fl = true;           break;         }       }       if (!fl) {         for (int i = int(sqrt(n)); i >= 2; i--) {           if (n % i == 0 && i <= k) {             cout << n / i <<   n ;             fl = true;             break;           }         }       }       if (!fl) {         cout << n <<   n ;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   double prob[maxn];   int main() {     ios::sync_with_stdio(false);     int n, p;     while (cin >> n >> p) {       int l, r;       for (int i = 0; i < n; ++i) {         cin >> l >> r;         prob[i] = (double)(r / p - (l - 1) / p) / (r + 1 - l);       }       double sum = 0;       for (int i = 0; i < n; ++i) {         int j = (i + 1) % n;         sum += 1.0 - (1 - prob[i]) * (1 - prob[j]);       }       printf( %.8lf n , sum * 2000);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long int MAX = -1000000000000000000;   const long long int MIN = 1000000000000000000;   const long long int inf = 1000000000000000000;   const long long int KOK = 100000;   const long long int LOG = 30;   const long long int li = 105;   const long long int mod = 1000000007;   long long int n, m, cev, b, a[li], k, dp[105][100005], PS[li][li];   string s;   vector<int> v;   long long int f(long long int sira, long long int topl) {     long long int mn = inf;     if (topl <= 0) return 0;     if (sira > n) {       return inf;     }     if (~dp[sira][topl]) return dp[sira][topl];     for (int i = sira; i <= n; i++) {       mn = min(mn, f(i + 1, topl - PS[sira][i] + PS[sira][sira - 1]) + 1);     }     return dp[sira][topl] = mn;   }   int main() {     memset(dp, -1, sizeof(dp));     scanf( %lld %lld , &n, &m);     for (long long int i = 1; i <= n; i++) {       scanf( %lld , &a[i]);     }     sort(a + 1, a + n + 1);     reverse(a + 1, a + n + 1);     for (long long int i = 1; i <= n; i++) {       long long int at = 0;       for (long long int j = i; j <= n; j++) {         PS[i][j] = PS[i][j - 1] + max(a[j] - at, 0ll);         at++;       }     }     long long int yess = f(1, m);     if (yess >= inf) {       printf( -1 n );       return 0;     }     printf( %lld n , yess);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string a;   int main() {     cin >> a;     cout << a;     for (int i = a.size() - 1; i >= 0; i--) cout << a[i];   }


#include <bits/stdc++.h>   using namespace std;   const int INF = 1061109567;   const int MAXN = 510005;   int dp[MAXN];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout.precision(20);     int n, d;     cin >> n >> d;     memset(dp, 0, sizeof dp);     dp[0] = 1;     int items[n];     for (int i = 0; i < n; i++) cin >> items[i];     for (int j = 0; j < n; j++) {       for (int i = MAXN - items[j]; i >= 0; i--)         if (dp[i]) dp[i + items[j]] = 1;     }     int itCount = 0;     int i = 0;     while (i < 500000) {       bool flag = false;       for (int j = i + d; j >= i + 1; j--) {         if (dp[j]) {           itCount++;           i = j;           flag = true;           break;         }       }       if (!flag) break;     }     cout << i <<     << itCount << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int logN = 18;   const int maxN = 200005;   char pch[maxN];   int n, m, q, sz;   vector<pair<int, char> > g[maxN];   pair<int, int> orange[maxN][logN];   pair<int, int> rrange[maxN][logN];   vector<array<int, 4> > queries[maxN];   int par[maxN][logN], pos[maxN], ord[maxN], ind[maxN], ans[maxN], dep[maxN],       f[maxN];   inline void update(int i, int x) {     while (i <= m) {       f[i] += x;       i += i & (-i);     }   }   inline int query(int i) {     int ret = 0;     while (i) {       ret += f[i];       i &= (i - 1);     }     return ret;   }   void dfs(int v, int p) {     par[v][0] = p;     for (auto x : g[v]) {       if (x.first != p) {         dep[x.first] = dep[v] + 1;         pch[x.first] = x.second;         dfs(x.first, v);       }     }   }   void buildSuffixArray(string s) {     int m = (int)s.size();     {       vector<pair<char, int> > vec;       for (int i = 0; i < m; i++) {         vec.emplace_back(s[i], i);       }       sort(vec.begin(), vec.end());       for (int i = 0; i < m; i++) {         if (i == 0 || vec[i].first != vec[i - 1].first)           pos[vec[i].second] = i;         else           pos[vec[i].second] = pos[vec[i - 1].second];       }     }     for (int k = 1; k < logN; k++) {       vector<pair<pair<int, int>, int> > vec;       for (int i = 0; i < m; i++) {         int j = i + (1 << (k - 1));         if (j < m) {           vec.push_back({{pos[i], pos[j]}, i});         } else {           vec.push_back({{pos[i], -1}, i});         }       }       sort(vec.begin(), vec.end());       for (int i = 0; i < m; i++) {         if (i == 0 || vec[i].first != vec[i - 1].first)           pos[vec[i].second] = i;         else           pos[vec[i].second] = pos[vec[i - 1].second];       }       if ((1 << k) >= m) break;     }     for (int i = 0; i < m; i++) {       ord[pos[i]] = i;     }   }   inline pair<int, int> compute(pair<int, int> range1, pair<int, int> range2,                                 int len1, int len2) {     if (range1.first == -1 || range2.first == -1) return {-1, -1};     if (len1 == 0) return range2;     if (len2 == 0) return range1;     pair<int, int> ret = {-1, -1};     {       int low = range1.first, hig = range1.second;       if (ord[hig] + len1 == sz || range2.first > pos[ord[hig] + len1])         return {-1, -1};       while (low < hig) {         int mid = (low + hig) / 2;         if (ord[mid] + len1 < sz && range2.first <= pos[ord[mid] + len1]) {           hig = mid;         } else {           low = mid + 1;         }       }       ret.first = low;     }     {       int low = range1.first, hig = range1.second;       if (ord[low] + len1 < sz && range2.second < pos[ord[low] + len1])         return {-1, -1};       while (low < hig) {         int mid = (low + hig + 1) / 2;         if (ord[mid] + len1 >= sz || pos[ord[mid] + len1] <= range2.second) {           low = mid;         } else {           hig = mid - 1;         }       }       ret.second = low;     }     if (ret.first > ret.second) {       ret = {-1, -1};     }     return ret;   }   inline int lca(int u, int v) {     if (dep[u] < dep[v]) swap(u, v);     int diff = dep[u] - dep[v];     for (int i = logN - 1; i >= 0; i--) {       if ((diff >> i) & 1) u = par[u][i];     }     if (u == v) return u;     for (int i = logN - 1; i >= 0; i--) {       if (par[u][i] != par[v][i]) {         u = par[u][i];         v = par[v][i];       }     }     return par[u][0];   }   pair<int, int> get(int u, int v) {     int w = lca(u, v);     pair<int, int> range1 = {0, sz - 1};     pair<int, int> range2 = {0, sz - 1};     int len1 = 0, len2 = 0;     {       int dist = dep[u] - dep[w];       for (int i = logN - 1; i >= 0; i--) {         if ((dist >> i) & 1) {           range1 = compute(range1, orange[u][i], len1, 1 << i);           len1 += (1 << i);           u = par[u][i];         }       }     }     {       int dist = dep[v] - dep[w];       for (int i = logN - 1; i >= 0; i--) {         if ((dist >> i) & 1) {           range2 = compute(rrange[v][i], range2, 1 << i, len2);           len2 += (1 << i);           v = par[v][i];         }       }     }     return compute(range1, range2, len1, len2);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> m >> q;     for (int i = 1; i < n; i++) {       int u, v;       char ch;       cin >> u >> v >> ch;       g[u].emplace_back(v, ch);       g[v].emplace_back(u, ch);     }     dep[1] = 0;     dfs(1, 1);     for (int k = 1; k < logN; k++) {       par[1][k] = 1;       for (int i = 2; i <= n; i++) {         par[i][k] = par[par[i][k - 1]][k - 1];       }     }     string s =   ;     memset(ind, -1, sizeof(ind));     for (int i = 1; i <= m; i++) {       string t;       cin >> t;       for (int j = 0; j < (int)t.size(); j++) {         ind[j + (int)s.size()] = i;       }       s += t;       s +=  # ;     }     sz = (int)s.size();     buildSuffixArray(s);     memset(orange, -1, sizeof(orange));     memset(rrange, -1, sizeof(rrange));     {       vector<int> lo(26, -1);       vector<int> hi(26, -1);       for (int i = 0; i < sz; i++) {         if (s[ord[i]] ==  # ) continue;         if (lo[s[ord[i]] -  a ] == -1) {           lo[s[ord[i]] -  a ] = i;         }         hi[s[ord[i]] -  a ] = i;       }       for (int i = 2; i <= n; i++) {         orange[i][0] = {lo[pch[i] -  a ], hi[pch[i] -  a ]};         rrange[i][0] = {lo[pch[i] -  a ], hi[pch[i] -  a ]};       }     }     for (int k = 1; k < logN; k++) {       for (int u = 1; u <= n; u++) {         int v = par[u][k - 1];         orange[u][k] = compute(orange[u][k - 1], orange[v][k - 1], 1 << (k - 1),                                1 << (k - 1));       }       for (int u = 1; u <= n; u++) {         int v = par[u][k - 1];         rrange[u][k] = compute(rrange[v][k - 1], rrange[u][k - 1], 1 << (k - 1),                                1 << (k - 1));       }     }     memset(ans, 0, sizeof(ans));     for (int i = 1; i <= q; i++) {       int u, v, l, r;       cin >> u >> v >> l >> r;       pair<int, int> range = get(u, v);       if (range.first == -1) {         continue;       }       queries[range.second].push_back({l, r, i, 1});       if (range.first > 0) {         queries[range.first - 1].push_back({l, r, i, -1});       }     }     memset(f, 0, sizeof(f));     for (int i = 0; i < sz; i++) {       if (ind[ord[i]] != -1) {         update(ind[ord[i]], 1);       }       for (auto x : queries[i]) {         ans[x[2]] += x[3] * (query(x[1]) - query(x[0] - 1));       }     }     for (int i = 1; i <= q; i++) {       cout << ans[i] <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[1005][1005], i, m, ans, k, l, j, q, x, n, ma, mi;   string s;   int main() {     cin >> n >> k;     if (k >= (n + 1) / 2) {       cout << -1;       return 0;     }     cout << n * k << endl;     for (int i = 1; i <= n; i++) {       int w = k;       for (int j = 1; j <= n; j++) {         if (a[i][j] == 1 || i == j) continue;         cout << i <<     << j << endl;         a[i][j] = 1;         a[j][i] = 1;         w--;         if (!w) break;       }     }   }


#include <bits/stdc++.h>   using namespace std;   struct Node {     string oper;     bool val;     int left = -1;     int right = -1;     bool will_left_change = false;     bool will_right_change = false;   };   vector<Node> tree;   bool f(bool l_val, bool r_val, string oper) {     if (oper ==  AND ) {       return (l_val && r_val);     } else if (oper ==  OR ) {       return (l_val || r_val);     } else if (oper ==  XOR ) {       return (l_val ^ r_val);     }   }   void calc(Node &v) {     if (v.oper ==  IN ) return;     calc(tree[v.left]);     if (v.right != -1) {       calc(tree[v.right]);     }     if (v.right == -1)       v.val = !tree[v.left].val;     else {       v.val = f(tree[v.left].val, tree[v.right].val, v.oper);     }     if (v.right == -1)       v.will_left_change = true;     else {       v.will_left_change = (f(tree[v.left].val, tree[v.right].val, v.oper) !=                             f(!tree[v.left].val, tree[v.right].val, v.oper));       v.will_right_change = (f(tree[v.left].val, tree[v.right].val, v.oper) !=                              f(tree[v.left].val, !tree[v.right].val, v.oper));     }   }   vector<int> ans;   void calc_leaves(Node &v, int ones_cnt, int depth, int v_index) {     if (v.oper ==  IN ) {       if (ones_cnt == depth) {         ans[v_index] = !tree[1].val;       } else {         ans[v_index] = tree[1].val;       }       return;     }     calc_leaves(tree[v.left], ones_cnt + (int)v.will_left_change, depth + 1,                 v.left);     if (v.right != -1) {       calc_leaves(tree[v.right], ones_cnt + (int)v.will_right_change, depth + 1,                   v.right);     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     tree.resize(n + 1);     ans.resize(n + 1, -1);     for (int i = 1; i <= n; i++) {       string s;       cin >> s;       if (s ==  IN ) {         int z;         cin >> z;         tree[i].oper = s;         tree[i].val = z;       } else if (s ==  NOT ) {         int id;         cin >> id;         tree[i].oper = s;         tree[i].left = id;       } else {         int left, right;         cin >> left >> right;         tree[i].oper = s;         tree[i].left = left;         tree[i].right = right;       }     }     calc(tree[1]);     calc_leaves(tree[1], 0, 0, -1);     for (int i = 1; i <= n; i++) {       if (ans[i] != -1) cout << ans[i];     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   priority_queue<long long> q;   int n, k;   int main() {     scanf( %d%d , &n, &k);     for (int i = 0; i < k; i++) q.push(0);     for (int i = 0; i < n; i++) {       long long s, m;       scanf( %I64d%I64d , &s, &m);       long long num = q.top();       q.pop();       num = max(-num, s);       printf( %I64d n , m + num);       q.push(-(m + num));     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t, a, b, c, r;     cin >> t;     while (t--) {       cin >> a >> b >> c >> r;       int min = a < b ? a : b;       int max = min == a ? b : a;       int res = max - min + 1;       if (c - r >= min && c + r <= max) {         res = res - 2 * r - 1;       } else if (min > c - r && max < c + r) {         res = 0;       } else if (c - r >= min && c - r <= max && c + r >= max) {         res = res - (max - c + r) - 1;       } else if (c + r <= max && c + r >= min && min >= c - r) {         res = res - (c + r - min) - 1;       }       res = res == max - min + 1 ? max - min : res;       cout << res << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int n, a[2006];   int one, g, mn = 1e9;   int gcd(int _a, int _b) { return _b == 0 ? _a : gcd(_b, _a % _b); }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       g = gcd(g, a[i]);       if (a[i] == 1) one++;     }     if (one != 0) {       printf( %d n , n - one);       return 0;     }     if (g != 1) {       puts( -1 );       return 0;     }     for (int i = 1; i <= n; i++) {       int b = a[i];       for (int j = i + 1; j <= n; j++) {         b = gcd(b, a[j]);         if (b == 1) {           mn = min(mn, j - i);           break;         }       }     }     printf( %d n , mn + n - 1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 405;   const int maxm = 5005;   const int INF = 1000000000;   const int mod = 1000000;   struct EDGE {     int cap, cost, v, next;   } edge[mod];   int head[maxn], E, q[mod];   bool used[maxn];   int pre[maxn], cur[maxn], dis[maxn];   void add_edge(int s, int t, int cap, int cost) {     edge[E].cap = cap;     edge[E].cost = cost;     edge[E].next = head[s];     edge[E].v = t;     head[s] = E++;     edge[E].cap = 0;     edge[E].cost = -cost;     edge[E].next = head[t];     edge[E].v = s;     head[t] = E++;   }   int min(int a, int b) { return (a == -1 || b < a) ? b : a; }   int SPFA(int s, int t, int n) {     int f = -1, r = 0;     int i, v;     q[r] = s;     for (i = 0; i < n; i++) dis[i] = INF;     dis[s] = 0;     pre[s] = s;     cur[s] = -1;     memset(used, false, sizeof(bool) * n);     used[s] = true;     while (f != r) {       f++;       if (f >= mod) f -= mod;       s = q[f];       used[s] = false;       for (i = head[s]; i != -1; i = edge[i].next) {         v = edge[i].v;         if (edge[i].cap > 0 && dis[s] + edge[i].cost < dis[v]) {           dis[v] = dis[s] + edge[i].cost;           pre[v] = s;           cur[v] = i;           if (!used[v]) {             used[v] = true;             r++;             if (r >= mod) r -= mod;             q[r] = v;           }         }       }     }     return dis[t];   }   int MinCost(int s, int t, int n) {     int ans = 0;     int u, v, cap;     int cost;     while (1) {       cost = SPFA(s, t, n);       if (cost == INF) break;       u = v = t;       cap = -1;       for (u = t; u != s; u = pre[u]) {         cap = min(cap, edge[cur[u]].cap);       }       ans += cost * cap;       u = v = t;       for (u = t; u != s; u = pre[u]) {         edge[cur[u]].cap -= cap;         edge[cur[u] ^ 1].cap += cap;       }     }     return ans;   }   int dx[maxn];   int dy[maxn];   int c[maxm];   int x[maxm];   int y[maxm];   int main() {     int n, m, g, k, z;     int i, j;     int s, t;     while (scanf( %d%d%d%d , &n, &m, &k, &g) != -1) {       memset(dx, 0, sizeof(int) * n);       memset(dy, 0, sizeof(int) * m);       for (i = 0; i < k; i++) {         scanf( %d%d , &x[i], &y[i]);         dx[--x[i]]++;         dy[--y[i]]++;       }       int ans = 0;       for (i = 0; i < n; i++) ans += dx[i] % g != 0;       for (i = 0; i < m; i++) ans += dy[i] % g != 0;       printf( %d n , ans);       memset(c, -1, sizeof(int) * k);       s = n + m;       t = s + 1;       for (; g >= 1; g--) {         E = 0;         memset(head, -1, sizeof(int) * (t + 1));         for (i = 0; i < k; i++)           if (c[i] == -1) add_edge(x[i], y[i] + n, 1, 0);         for (i = 0; i < n; i++) {           add_edge(s, i, dx[i] / g, 0);           if (dx[i] % g) add_edge(s, i, 1, 1);         }         for (i = 0; i < m; i++) {           add_edge(i + n, t, dy[i] / g, 0);           if (dy[i] % g) add_edge(i + n, t, 1, 1);         }         MinCost(s, t, t + 1);         for (i = 0, z = 0; i < k; i++)           if (c[i] == -1) {             if (edge[z].cap == 0) {               c[i] = g;               dx[x[i]]--;               dy[y[i]]--;             }             z += 2;           }       }       for (i = 0; i < k; i++) {         if (i) printf(   );         printf( %d , c[i]);       }       puts(  );     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxk = 11;   long long p[maxk];   void init() {     p[0] = 1;     for (int i = 1; i <= maxk; ++i) p[i] = p[i - 1] * 10;   }   long long f(long long x) {     if (x == 0) return 0;     long long ret = 0;     int pc = maxk - 1;     while (p[pc] > x) --pc;     ret += (pc + 1) * (x - p[pc] + 1);     while (pc > 0) {       --pc;       ret += (pc + 1) * (p[pc + 1] - p[pc]);     }     return ret;   }   typedef long long (*FUN)(long long);   long long bis(long long pos, FUN fun) {     long long ans = 0;     long long l = 0, r = 1000000000;     while (l < r) {       long long mid = l + (r - l) / 2;       if (fun(mid) < pos)         ans = mid, l = mid + 1;       else         r = mid;     }     return ans;   }   long long g(long long x) {     if (x == 0) return 0;     int pc = maxk - 1;     while (p[pc] > x) --pc;     long long ret = 0;     ret += (pc + 1) * (x - p[pc] + 1) * (1 + x - p[pc] + 1) / 2;     while (pc > 0) {       --pc;       ret += (pc + 1) * 9 * p[pc] * (x + x - (p[pc] - 1) - (p[pc + 1] - 2)) / 2;     }     return ret;   }   char cc[100000];   int main() {     init();     int q;     long long k;     scanf( %d , &q);     long long temp = 0;     while (q--) {       scanf( %lld , &k);       long long ans = bis(k, g);       k -= g(ans);       ans = bis(k, f);       k -= f(ans);       sprintf(cc,  %lld , ans + 1);       printf( %c n , cc[k - 1]);     }     getchar();     getchar();     return 0;   }


#include <bits/stdc++.h>   const long long inf = 1e16;   using namespace std;   long long read() {     long long x = 0, f = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     return f * x;   }   int n;   long long ans, d[505][505];   struct tmp {     long long a, b, k;     bool operator<(const tmp &o) const { return b > o.b; }   } a[505];   int main() {     n = read();     for (int i = 1; i <= n; i++) a[i] = tmp{read(), read(), read()};     sort(a + 1, a + 1 + n);     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= i; j++) {         d[i][j] = d[i - 1][j];         if (j > 0)           d[i][j] = max(d[i][j], d[i - 1][j - 1] + a[i].a - (j - 1) * a[i].b);         d[i][j] = max(d[i][j], d[i - 1][j] + a[i].a - a[i].k * a[i].b);       }     }     for (int i = 1; i <= n; i++) ans = max(ans, d[n][i]);     return 0 * printf( %lld n , ans);   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int c1, c2;       int d1, d2;       int diff1, diff2;       for (int i = 0; i < 3; i++) {         if ((n - i) % 3 == 0) {           c2 = (n - i) / 3;           c1 = n - 2 * c2;           diff1 = i;           break;         }       }       for (int i = 0; i < 3; i++) {         if ((n - i) % 3 == 0) {           d2 = (n + i) / 3;           d1 = n - 2 * d2;           diff2 = i;           break;         }       }       if (diff1 < diff2)         cout << c1 <<     << c2 <<   n ;       else         cout << d1 <<     << d2 <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   long long int n = 0, m, l = 0, k, a, b, c, sum = 0, i, r = 0, j, second, h,                 first = 0, sc, ans = 0, x, t, y;   double aa, pp;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> k >> b >> n >> t;     if (k == 1) {       ans = (ceil((1 + n * b - t) * 1.0 / b));       if (ans <= 0)         cout << 0;       else         cout << ans;       return 0;     }     aa = (t * (k - 1)) + t * b;     aa = aa / (t * (k - 1) + b);     pp = n * (log(k)) - log(t) + log(aa);     pp = pp / log(k);     if (pp - floor(pp) < 1e-12) {       cout << setprecision(10) << floor(pp);       return 0;     }     ans = ceil(pp);     if (ans <= 0)       cout << 0;     else       cout << ans;   }


#include <bits/stdc++.h>   #pragma warning(disable : 4996)   using namespace std;   const long double pi = acos(-1.0);   const long double eps = 1e-5;   const int INF = 0x3f3f3f3f;   const long long INFLL = 0x3f3f3f3f3f3f3f3f;   struct poi {     double x, y;   } p[200010];   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int n;     cin >> n;     for (int i = (1); i <= (n); i++) cin >> p[i].x >> p[i].y;     if (n & 1) {       cout <<  NO ;       return 0;     }     poi c;     c.x = (p[1].x + p[n / 2 + 1].x) / 2;     c.y = (p[1].y + p[n / 2 + 1].y) / 2;     for (int i = (2); i <= (n / 2); i++) {       if (fabs((p[i].x + p[i + n / 2].x) / 2 - c.x) < eps &&           fabs((p[i].y + p[i + n / 2].y) / 2 - c.y) < eps)         ;       else {         cout <<  NO ;         return 0;       }     }     cout <<  YES ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   void debug(T a, T b) {     for (; a != b; ++a) cerr << *a <<    ;     cerr << endl;   }   template <class T>   void chmin(T& a, const T& b) {     if (a > b) a = b;   }   template <class T>   void chmax(T& a, const T& b) {     if (a < b) a = b;   }   namespace std {   template <class S, class T>   ostream& operator<<(ostream& out, const pair<S, T>& a) {     out <<  (  << a.first <<  ,  << a.second <<  ) ;     return out;   }   }  // namespace std   bool valid[200005];   struct DomTree {     static const int MAX_V = 200000;     int semi[MAX_V], U[MAX_V], idom[MAX_V];     int puf[MAX_V], par[MAX_V];     int mn[MAX_V];     void init() {       memset(puf, -1, sizeof(puf));       for (int i = 0; i < (MAX_V); ++i) mn[i] = i;     }     int root(int a) {       if (puf[a] == -1) return a;       int r = root(puf[a]);       if (semi[mn[puf[a]]] < semi[mn[a]]) mn[a] = mn[puf[a]];       return puf[a] = r;     }     void unite(int a, int b) { puf[b] = a; }     int eval(int v) {       root(v);       return mn[v];     }     int cnt;     int vs[MAX_V];     vector<int> bucket[MAX_V];     vector<int> g[MAX_V], rg[MAX_V];     void dfs(int v) {       semi[v] = cnt;       vs[cnt++] = v;       for (int u : g[v])         if (semi[u] < 0) {           par[u] = v;           dfs(u);         }     }     vector<int> g3[200005];     int size[200005];     int dfs3(int v) {       if (valid[v]) size[v] = 1;       for (auto to : g3[v]) {         size[v] += dfs3(to);       }       return size[v];     }     int solve(int n, int r, vector<int>* g_) {       init();       for (int i = 0; i < (n); ++i) {         g[i] = g_[i];         rg[i].clear();       }       for (int i = 0; i < (n); ++i)         for (auto to : g[i]) {           rg[to].push_back(i);         }       for (int i = 0; i < (n); ++i) semi[i] = -1;       cnt = 0;       dfs(r);       for (int i = n - 1; i > 0; i--) {         int w = vs[i];         for (int v : rg[w]) {           int u = eval(v);           chmin(semi[w], semi[u]);         }         bucket[vs[semi[w]]].push_back(w);         for (int v : bucket[par[w]]) U[v] = eval(v);         bucket[par[w]].clear();         unite(par[w], w);       }       for (int i = 1; i < n; i++) {         int w = vs[i];         int u = U[w];         if (semi[w] == semi[u])           idom[w] = semi[w];         else           idom[w] = idom[u];       }       for (int i = 1; i < n; i++) {         int w = vs[i];         idom[w] = vs[idom[w]];       }       idom[r] = -1;       for (int i = 0; i < (n); ++i)         if (i != r) g3[idom[i]].push_back(i);       dfs3(r);       int res = 0;       for (int i = 0; i < (n); ++i)         if (i != r) chmax(res, size[i]);       return res;     }   };   DomTree D;   int n, m, s;   vector<pair<int, int> > g[200005];   const long long int INF = 1e18;   long long int cost[200005];   vector<int> g2[200005];   int main() {     cin >> n >> m >> s;     --s;     for (int i = 0; i < (m); ++i) {       int a, b, c;       scanf( %d%d%d , &a, &b, &c);       --a;       --b;       g[a].push_back({b, c});       g[b].push_back({a, c});     }     for (int i = 0; i < (n); ++i) cost[i] = INF;     priority_queue<pair<long long int, int>, vector<pair<long long int, int> >,                    greater<pair<long long int, int> > >         pq;     pq.push({0, s});     cost[s] = 0;     while (!pq.empty()) {       pair<long long int, int> cur = pq.top();       pq.pop();       if (cost[cur.second] < cur.first) continue;       for (auto e : g[cur.second]) {         if (cost[e.first] > cur.first + e.second) {           cost[e.first] = cur.first + e.second;           pq.push({cost[e.first], e.first});         }       }     }     for (int i = 0; i < (n); ++i)       if (cost[i] < INF)         for (auto e : g[i]) {           if (cost[e.first] == cost[i] + e.second) g2[i].push_back(e.first);         }     for (int i = 0; i < (n); ++i)       if (cost[i] < INF) valid[i] = 1;     for (int i = 0; i < (n); ++i)       if (cost[i] == INF) g2[s].push_back(i);     int res = D.solve(n, s, g2);     cout << res << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   int n, m, h, t, maxv;   int e[maxn][2];   set<int> mp[maxn];   vector<int> g[maxn];   vector<int> xv, yv;   set<int> comm;   bool check(int x, int y, vector<int> &xv, vector<int> &yv, set<int> &comm) {     int lx = max(0, (int)(h - xv.size()));     int ly = max(0, (int)(t - yv.size()));     if (comm.size() >= lx + ly) {       printf( YES n );       printf( %d %d n , x, y);       set<int>::iterator it = comm.begin();       int mx = min((int)xv.size(), h);       for (int i = 0; i < mx; i++) printf( %d  , xv[i]);       for (int i = 0; i < lx; i++) {         printf( %d  , *it);         it++;       }       printf(  n );       int my = min((int)yv.size(), t);       for (int i = 0; i < my; i++) printf( %d  , yv[i]);       for (int i = 0; i < ly; i++) {         printf( %d  , *it);         it++;       }       printf(  n );       return true;     }     return false;   }   bool solve(int x, int y) {     if (mp[x].size() <= h || mp[y].size() <= t) return false;     int cnt = mp[x].size() - 1;     for (int i = 0; i < g[y].size() && cnt < h + t; i++) {       int tx = g[y][i];       if (tx == x) continue;       if (mp[x].count(tx) == 0) cnt++;     }     if (cnt < h + t) return false;     xv.clear();     yv.clear();     comm.clear();     for (set<int>::iterator it = mp[x].begin();          it != mp[x].end() && xv.size() <= h && comm.size() <= maxv; it++) {       int ty = *it;       if (ty == y) continue;       if (mp[y].find(ty) != mp[y].end())         comm.insert(ty);       else         xv.push_back(ty);     }     for (set<int>::iterator it = mp[y].begin();          it != mp[y].end() && yv.size() <= t && comm.size() <= maxv; it++) {       int tx = *it;       if (tx == x) continue;       if (mp[x].find(tx) != mp[x].end())         comm.insert(tx);       else         yv.push_back(tx);     }     if (check(x, y, xv, yv, comm)) return true;     return false;   }   int main() {     scanf( %d%d%d%d , &n, &m, &h, &t);     maxv = h + t;     for (int i = 1; i <= n; i++) mp[i].clear();     for (int i = 0; i < m; i++) {       int tx, ty;       scanf( %d%d , &tx, &ty);       mp[tx].insert(ty);       mp[ty].insert(tx);       g[tx].push_back(ty);       g[ty].push_back(tx);     }     int flags = false;     for (int i = 1; i <= n; i++)       if (flags == false)         for (int j = 0; j < g[i].size(); j++) {           if (solve(i, g[i][j])) {             flags = true;             break;           }         }     if (!flags) printf( NO n );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, G, R, d[100127], w[100127], rt, tot = 0, mi[100127 * 20],                                             c[100127 * 20][2];   long long f[100127], s[100127], ans;   void change(int &p, int l, int r, int x, int v) {     if (!p) p = ++tot;     mi[p] = v;     if (l == r) return;     int mid = (1ll * l + r) >> 1;     (x <= mid) ? change(c[p][0], l, mid, x, v)                : change(c[p][1], mid + 1, r, x, v);   }   int Ask(int p, int l, int r, int L, int R) {     if (R < L) return n + 1;     if ((!p) || (R < l) || (r < L)) return n + 1;     if ((L <= l) && (r <= R)) return mi[p];     int mid = (1ll * l + r) >> 1;     return min(Ask(c[p][0], l, mid, L, R), Ask(c[p][1], mid + 1, r, L, R));   }   inline int Get(int v) {     if (v >= R) return Ask(rt, 0, R + G - 1, (v + G) % (R + G), v - 1);     return min(Ask(rt, 0, R + G - 1, v + G, R + G - 1),                Ask(rt, 0, R + G - 1, 0, v - 1));   }   inline long long calc(int x, int y, int v) {     if (y == n + 1) return 0;     v = (v + s[y] - s[x]) % (R + G);     return R + G - v;   }   int main() {     scanf( %d%d%d , &n, &G, &R);     int i, p, x;     for (i = 1; i <= n + 1; i++) scanf( %d , &d[i]);     w[n + 1] = n + 1;     f[n + 1] = 0;     for (i = 2; i <= n + 1; i++) s[i] = s[i - 1] + d[i];     for (i = n; i >= 1; i--) {       w[i] = Get(s[i] % (R + G));       change(rt, 0, R + G - 1, s[i] % (R + G), i);     }     for (i = n; i >= 1; i--) f[i] = f[w[i]] + s[w[i]] - s[i] + calc(i, w[i], 0);     scanf( %d , &m);     while (m--) {       scanf( %d , &x);       ans = d[1] + x;       x = (x + d[1]) % (G + R);       if (x >= G) {         ans += R + G - x;         x = 0;       }       p = Get((R + G - x) % (R + G));       ans += f[p] + s[p] + calc(1, p, x);       printf( %lld n , ans);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   T gcd(T a, T b) {     while (b ^= a ^= b ^= a %= b)       ;     return a;   }   template <class T>   T lcm(T a, T b) {     return (a / gcd<T>(a, b) * b);   }   template <class T>   inline int countbit(T n) {     return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));   }   template <class T>   inline bool isPrimeNumber(T n) {     if (n <= 1) return false;     for (T i = 2; i * i <= n; i++)       if (n % i == 0) return false;     return true;   }   template <class T>   inline T powerMod(T p, int e, T m) {     if (e == 0)       return 1 % m;     else if (e % 2 == 0) {       T t = powerMod(p, e / 2, m);       return multiplyMod(t, t, m);     } else       return multiplyMod(powerMod(p, e - 1, m), p, m);   }   bool isUpperCase(char c) { return c >=  A  && c <=  Z ; }   bool isLowerCase(char c) { return c >=  a  && c <=  z ; }   char toLowerCase(char c) { return (isUpperCase(c)) ? (c + 32) : c; }   char toUpperCase(char c) { return (isLowerCase(c)) ? (c - 32) : c; }   template <class T>   string toString(T n) {     ostringstream ost;     ost << n;     ost.flush();     return ost.str();   }   int toInt(string s) {     int r = 0;     istringstream sin(s);     sin >> r;     return r;   }   double toDouble(string s) {     double r = 0;     istringstream sin(s);     sin >> r;     return r;   }   const double pi = acos(-1.0);   int main() {     string s;     cin >> s;     int pos = -14;     vector<string> v = { 01:10 ,   02:20 ,  03:30 ,  04:40 ,  05:50 ,                          10:01 ,   11:11 ,  12:21 ,  13:31 ,  14:41 ,                           15:51 ,  20:02 ,  21:12 ,  22:22 ,  23:32 };     for (int i = 0; i < v.size(); i++) {       if (s < v[i]) {         pos = i;         break;       }     }     if (pos == -14)       cout <<  00:00 ;     else       cout << v[pos];     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, m, t, tbl[510][510], star[510][510], sum[510];   int main() {     scanf( %d%d%d , &n, &m, &t);     if (t == 0) {       long long ans = 0;       for (int i = 1; i <= n; i++)         for (int j = 1; j <= m; j++) ans += i * j;       cout << ans << endl;       return 0;     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) scanf( %1d , tbl[i] + j);     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         star[i][j] = (tbl[i][j] && tbl[i - 1][j] && tbl[i + 1][j] &&                       tbl[i][j - 1] && tbl[i][j + 1]);     long long ans = 0;     for (int i = 2; i < n; i++) {       memset(sum, 0, sizeof(sum));       for (int j = i; j < n; j++) {         for (int k = 2; k < m; k++) sum[k] += star[j][k];         for (int k = 2, l = 2, now = 0; k < m; k++) {           while (l < m && (l <= k || now < t)) now += sum[l++];           if (now >= t) ans += m - l + 1;           now -= sum[k];         }       }     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k, d;     cin >> k >> d;     if (k > 1 && d == 0)       cout <<  No solution ;     else {       cout << d;       for (int i = 0; i < k - 1; i++) cout <<  0 ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<string, int> a;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     while (n--) {       int temp_val;       string temp_s;       cin >> temp_val >> temp_s;       sort(temp_s.begin(), temp_s.end());       if (a[temp_s] < 1)         a[temp_s] = temp_val;       else         a[temp_s] = min(a[temp_s], temp_val);     }     int ans = INT_MAX;     if (a[ A ] >= 1 && a[ B ] >= 1 && a[ C ] >= 1) ans = a[ A ] + a[ B ] + a[ C ];     if (a[ A ] >= 1 && a[ BC ] >= 1) ans = min(ans, a[ A ] + a[ BC ]);     if (a[ C ] >= 1 && a[ AB ] >= 1) ans = min(ans, a[ C ] + a[ AB ]);     if (a[ B ] >= 1 && a[ AC ] >= 1) ans = min(ans, a[ B ] + a[ AC ]);     if (a[ AB ] >= 1 && a[ BC ] >= 1) ans = min(ans, a[ AB ] + a[ BC ]);     if (a[ AC ] >= 1 && a[ AB ] >= 1) ans = min(ans, a[ AC ] + a[ AB ]);     if (a[ BC ] >= 1 && a[ AC ] >= 1) ans = min(ans, a[ BC ] + a[ AC ]);     if (a[ ABC ] >= 1) ans = min(ans, a[ ABC ]);     if (ans == INT_MAX)       cout << -1 << endl;     else       cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2020 + 1;   int vis[MAXN], par[MAXN], orig[MAXN], match[MAXN], aux[MAXN], t, N;   vector<int> conn[MAXN];   queue<int> Q;   void addEdge(int u, int v) {     conn[u].push_back(v);     conn[v].push_back(u);   }   void init(int n) {     N = n;     t = 0;     for (int i = 0; i <= n; ++i) {       conn[i].clear();       match[i] = aux[i] = par[i] = 0;     }   }   void augment(int u, int v) {     int pv = v, nv;     do {       pv = par[v];       nv = match[pv];       match[v] = pv;       match[pv] = v;       v = nv;     } while (u != pv);   }   int lca(int v, int w) {     ++t;     while (true) {       if (v) {         if (aux[v] == t)           return v;         else           aux[v] = t;         v = orig[par[match[v]]];       }       swap(v, w);     }   }   void blossom(int v, int w, int a) {     while (orig[v] != a) {       par[v] = w;       w = match[v];       if (vis[w] == 1) Q.push(w), vis[w] = 0;       orig[v] = orig[w] = a;       v = par[w];     }   }   bool bfs(int u) {     fill(vis + 1, vis + 1 + N, -1);     iota(orig + 1, orig + N + 1, 1);     Q = queue<int>();     Q.push(u);     vis[u] = 0;     while (!Q.empty()) {       int v = Q.front();       Q.pop();       for (int x : conn[v]) {         if (vis[x] == -1) {           par[x] = v;           vis[x] = 1;           if (!match[x]) return augment(u, x), true;           Q.push(match[x]);           vis[match[x]] = 0;         } else if (vis[x] == 0 && orig[v] != orig[x]) {           int a = lca(orig[v], orig[x]);           blossom(x, v, a);           blossom(v, x, a);         }       }     }     return false;   }   int Match() {     int ans = 0;     vector<int> V(N - 1);     iota(V.begin(), V.end(), 1);     shuffle(V.begin(), V.end(), mt19937(0x94949));     for (auto x : V)       if (!match[x]) {         for (auto y : conn[x])           if (!match[y]) {             match[x] = y, match[y] = x;             ++ans;             break;           }       }     for (int i = 1; i <= N; ++i)       if (!match[i] && bfs(i)) ++ans;     return ans;   }   struct UFD {     vector<int> ufd;     UFD(int n) : ufd(n) { iota(ufd.begin(), ufd.end(), 0); }     int Find(int x) {       if (x == ufd[x]) return x;       return ufd[x] = Find(ufd[x]);     }     void Union(int a, int b) { ufd[Find(a)] = Find(b); }   };   int main() {     int N;     cin >> N;     vector<int> V(N);     for (int& x : V) cin >> x;     vector<pair<int, int>> ft;     int from = -1;     for (int i = 0; i < N; ++i) {       if (V[i] == 0) {         if (from == -1) from = i;       } else {         if (from != -1) ft.emplace_back(from, i - 1);         from = -1;       }     }     if (from != -1) ft.emplace_back(from, N - 1);     if (ft.size() == 1 && ft[0].first == 0 && ft[0].second == N - 1) {       for (int i = 0; i < N; ++i) cout << (i / 2) + 1 <<    ;       cout << endl;       return 0;     }     vector<pair<int, int>> ov, av;     for (auto [f, t] : ft) {       if (f == 0) {         for (int i = 0; i + 1 <= t; i += 2) V[i] = V[i + 1] = -1;         if (t % 2 == 0) V[t] = V[t + 1];       } else if (t == N - 1) {         for (int i = t; i - 1 >= f; i -= 2) V[i] = V[i - 1] = -1;         if ((t - f) % 2 == 0) V[f] = V[f - 1];       } else {         if ((t - f + 1) % 2 == 0)           av.emplace_back(f, t);         else           ov.emplace_back(f, t);       }     }     int K = 600;     UFD ufd(K + 1);     init(K);     vector<bool> used(K + 1);     vector<int> cooked(K + 1);     for (int i = 0; i < N - 1; ++i)       if (V[i] == V[i + 1] && V[i] > 0) used[V[i]] = 1;     vector<pair<int, int>> raw;     for (auto [f, t] : ov) {       int s = V[f - 1], e = V[t + 1];       if (ufd.Find(s) == ufd.Find(e)) {         cooked[s] = s;         used[s] = 1;         V[f] = V[f - 1];         for (int i = f + 1; i <= t; ++i) V[i] = -1;       } else         raw.emplace_back(f, t);       ufd.Union(s, e);     }     for (int i = 1; i <= K; ++i)       if (cooked[i]) cooked[ufd.Find(i)] = cooked[i];     for (int i = 1; i <= K; ++i)       if (cooked[ufd.Find(i)]) used[i] = 1;     vector<vector<int>> added(K + 1, vector<int>(K + 1, -1));     for (int i = 0; i < (int)av.size(); ++i) {       auto [f, t] = av[i];       int s = ufd.Find(V[f - 1]), e = ufd.Find(V[t + 1]);       if (used[s] || used[e]) continue;       if (added[s][e] != -1) continue;       if (s == e) continue;       added[s][e] = added[e][s] = i;       addEdge(s, e);     }     Match();     vector<int> root(K + 1);     for (int i = 1; i <= K; ++i)       if (match[i] && match[i] > i) {         int s = i, e = match[i];         auto [f, t] = av[added[s][e]];         V[f] = V[f - 1];         V[t] = V[t + 1];         used[V[f - 1]] = used[V[t + 1]] = 1;         for (int j = f + 1; j <= t - 1; ++j) V[j] = -1;       }     for (auto [f, t] : av)       if (V[f] == 0)         for (int j = f; j <= t; ++j) V[j] = -1;     vector<vector<tuple<int, int, int>>> conn(K + 1);     for (auto [f, t] : raw) {       int s = V[f - 1], e = V[t + 1];       conn[s].emplace_back(e, f, t);       conn[e].emplace_back(s, f, t);     }     function<void(int, int)> dfs = [&](int a, int p) {       for (auto [b, f, t] : conn[a])         if (b != p) {           if (V[f] == 0) {             for (int i = f; i <= t; ++i) V[i] = -1;             if (V[f - 1] == b)               V[f] = b;             else               V[t] = b;           }           dfs(b, a);         }     };     for (int i = 1; i <= K; ++i)       if (ufd.Find(i) == i) {         int p = i;         if (cooked[p])           p = cooked[p];         else           for (int i = 1; i <= K; ++i)             if (used[i] && ufd.Find(i) == p) p = i;         dfs(p, -1);       }     for (int i = 0; i < N - 1; ++i)       if (V[i] == V[i + 1] && V[i] > 0) used[V[i]] = 1;     int tp = 1;     for (int i = 0; i < N - 1; ++i)       if (V[i] == -1) {         while (tp <= K && used[tp]) ++tp;         assert(V[i + 1] == -1);         V[i] = V[i + 1] = tp;         if (tp <= K) used[tp] = 1;         ++tp;       }     for (auto x : V) cout << x <<    , assert(x > 0);     cout << endl;   }


#include <iostream>   #include <iomanip>   #include<iterator> // for iterators   #include<vector> // for vectors   #include <utility>   #include <algorithm>   #include <set>   #include <map>   #include <stack>   #include <unordered_map>   using namespace std;      int main()   {       int t, n, temp; scanf( %d , &t);       vector<int> v;       while (t --)       {           scanf( %d , &n);           for (int i = 0; i < n; ++i)           {               scanf( %d , &temp);               v.emplace_back(temp);           }           sort(v.begin() , v.end());           int counter = 0;           for (int i = 1; i < v.size(); ++i)           {               if (v[i] > v[0])                   counter ++;           }           printf( %d n  , counter);           v.clear();       }       return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long power(long long a, long long b) {     long long res = 1;     a %= 998244353;     assert(b >= 0);     for (; b; b >>= 1) {       if (b & 1) res = res * a % 998244353 % 998244353;       a = a * a % 998244353;     }     return res;   }   long long gcd(long long a, long long b) {     if (a < b) return gcd(b, a);     if (b == 0) return a;     return gcd(b, a % b);   }   long long n, m;   long long flg = 0;   vector<long long> ad[500005];   long long t[500005];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     ;     long long i, j, k, x;     cin >> n >> m;     for (i = 0; i < m; i++) {       cin >> j >> k;       ad[j].push_back(k);       ad[k].push_back(j);     }     vector<pair<long long, long long> > ans;     for (i = 1; i <= n; i++) {       cin >> t[i];       ans.push_back(make_pair(t[i], i));     }     sort(ans.begin(), ans.end());     for (i = 1; i <= n; i++) {       set<long long> tp;       for (auto x : ad[i]) {         tp.insert(t[x]);       }       for (j = 1; j < t[i]; j++) {         auto it = tp.find(j);         if (it == tp.end()) {           cout << -1 <<   n ;           return 0;         } else {           tp.erase(j);         }       }       auto it = tp.find(t[i]);       if (it != tp.end()) {         cout << -1 <<   n ;         return 0;       }     }     for (i = 0; i < ans.size(); i++) {       cout << ans[i].second <<    ;     }     cout <<   n ;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   const int MAX = 2e5 + 5;   const long long MAX2 = 11;   const long long MOD = 998244353;   const long long MOD2 = 1000005329;   const long long INF = 2e18;   const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};   const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0};   const double pi = acos(-1);   const double EPS = 1e-9;   const int block = 2000;   int n, m, a, b, c, v[205][205], ans;   pair<double, pair<int, int> > e[205 * 205];   double le, ri, m1, m2, n1, n2, a1, a2, u[205][205], nx;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j)         if (i != j) v[i][j] = MOD;     for (int i = 1; i <= m; ++i) {       cin >> a >> b >> c;       e[i] = {c, {a, b}};       v[a][b] = v[b][a] = min(v[a][b], c);     }     for (int k = 1; k <= n; ++k)       for (int i = 1; i <= n; ++i)         for (int j = 1; j <= n; ++j) v[i][j] = min(v[i][j], v[i][k] + v[k][j]);     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j) u[i][j] = v[i][j];     ans = 1e9;     for (int i = 1; i <= m; ++i) {       a = e[i].second.first, b = e[i].second.second;       le = 0.0, ri = e[i].first;       for (int k = 1; k <= 40; ++k) {         m1 = (le + le + ri) / 3.0, n1 = e[i].first - m1;         m2 = (le + ri + ri) / 3.0, n2 = e[i].first - m2;         a1 = a2 = 0.0;         for (int j = 1; j <= n; ++j) {           a1 = max(a1, min(u[a][j] + m1, u[b][j] + n1));           a2 = max(a2, min(u[a][j] + m2, u[b][j] + n2));         }         if (a1 < a2)           ri = m2;         else           le = m1;       }       ans = min(ans, (int)round(a1 * 2.0));       nx = m1;       le = 0.0, ri = nx;       for (int k = 1; k <= 40; ++k) {         m1 = (le + le + ri) / 3.0, n1 = e[i].first - m1;         m2 = (le + ri + ri) / 3.0, n2 = e[i].first - m2;         a1 = a2 = 0.0;         for (int j = 1; j <= n; ++j) {           a1 = max(a1, min(u[a][j] + m1, u[b][j] + n1));           a2 = max(a2, min(u[a][j] + m2, u[b][j] + n2));         }         if (a1 < a2)           ri = m2;         else           le = m1;       }       ans = min(ans, (int)round(a1 * 2.0));       le = nx, ri = e[i].first;       for (int k = 1; k <= 40; ++k) {         m1 = (le + le + ri) / 3.0, n1 = e[i].first - m1;         m2 = (le + ri + ri) / 3.0, n2 = e[i].first - m2;         a1 = a2 = 0.0;         for (int j = 1; j <= n; ++j) {           a1 = max(a1, min(u[a][j] + m1, u[b][j] + n1));           a2 = max(a2, min(u[a][j] + m2, u[b][j] + n2));         }         if (a1 < a2)           ri = m2;         else           le = m1;       }       ans = min(ans, (int)round(a1 * 2.0));     }     if (ans & 1)       cout << ans / 2 <<  .5 n ;     else       cout << ans / 2 <<  .0 n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const double eps = 1e-10;   const double pi = acos(-1.0);   const int maxn = 1e6 + 10;   const long long inf = 0x3f3f3f3f;   const int dir[8][2] = {{0, 1}, {1, 0},  {0, -1}, {-1, 0},                          {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};   int n, m;   int dis[1010][1010][4];   char g[1010][1010];   queue<pair<int, int> > q;   void bfs(int x) {     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (g[i][j] ==  0  + x) q.push(make_pair(i, j)), dis[i][j][x] = 0;     while (!q.empty()) {       pair<int, int> p = q.front();       q.pop();       for (int i = 0; i < 4; i++) {         int dx = p.first + dir[i][0];         int dy = p.second + dir[i][1];         if (dx < 1 || dx > n || dy < 1 || dy > m || g[dx][dy] ==  # ) continue;         int v = (g[dx][dy] ==  . );         if (dis[p.first][p.second][x] + v < dis[dx][dy][x]) {           dis[dx][dy][x] = dis[p.first][p.second][x] + v;           q.push(make_pair(dx, dy));         }       }     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> g[i] + 1;     memset(dis, inf, sizeof dis);     bfs(1), bfs(2), bfs(3);     long long ans = inf;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         long long tmp = 0;         for (int k = 1; k <= 3; k++) tmp += dis[i][j][k];         if (g[i][j] ==  . ) tmp -= 2;         ans = min(ans, tmp);       }     if (ans == inf)       cout << -1;     else       cout << ans;     return 0;   }


//Code By CXY07   #include<bits/stdc++.h>   using namespace std;      //#define FILE   //#define int long long   #define debug(x) cout << #x <<   =   << x << endl   #define file(FILENAME) freopen(FILENAME .in ,  r , stdin), freopen(FILENAME .out ,  w , stdout)   #define LINE() cout <<  LINE =   << __LINE__ << endl   #define LL long long   #define ull unsigned long long   #define pii pair<int,int>   #define mp make_pair   #define pb push_back   #define fst first   #define scd second   #define inv(x) qpow((x),mod - 2)   #define lowbit(x) ((x) & (-(x)))   #define abs(x) ((x) < 0 ? (-(x)) : (x))   #define randint(l, r) (rand() % ((r) - (l) + 1) + (l))   #define vec vector      const int MAXN = 1e6 + 10;   const int INF = 2e9;   const double PI = acos(-1);   const double eps = 1e-6;   //const int mod = 1e9 + 7;   //const int mod = 998244353;   //const int G = 3;   //const int base = 131;      int T, n, m;   int _L[MAXN], _R[MAXN];   int *L = _L + (int)5e5, *R = _R + (int)5e5;   char s[MAXN];      template<typename T> inline bool read(T &a) {    a = 0; char c = getchar(); int f = 1;    while(c <  0  || c >  9 ) {if(c ==  - ) f = -1; c = getchar();}    while(c >=  0  && c <=  9 ) {a = a * 10 + (c ^ 48); c = getchar();}    a *= f;    return 1;   }      template<typename A, typename ...B>   inline bool read(A &x, B &...y) {return read(x) && read(y...);}      void solve() {    scanf( %s , s + 1);    n = strlen(s + 1);    for(int i = -n; i <= n; ++i) L[i] = R[i] = 0;    int now = 0;    for(int i = 1; i <= n; ++i) {     if(s[i] ==  1 ) R[now++]++;     else L[now--]++;    }    now = 0;    for(int i = 1; i <= n; ++i) {     if(L[now] && (!R[now] || R[now - 1])) L[now--]--, putchar( 0 );     else R[now++]--, putchar( 1 );    }    putchar(  n );   }      signed main () {   #ifdef FILE    freopen( .in , r ,stdin);    freopen( .out , w ,stdout);   #endif    read(T);    while(T--) solve();    return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct edge {     int u, v;     long long c;     bool operator<(const edge& other) const { return c < other.c; }   };   vector<int> parent;   vector<int> size;   int get(int v) {     if (parent[v] == v) {       return v;     }     return parent[v] = get(parent[v]);   }   void unite(int a, int b) {     a = get(a), b = get(b);     if (size[a] < size[b]) {       swap(a, b);     }     parent[b] = a;     size[a] += size[b];   }   const int rmax = 18;   vector<vector<int> > p;   vector<vector<long long> > maxx;   vector<vector<edge> > g;   vector<int> tin, tout;   int timer = 0;   void dfs(int v, int par) {     tin[v] = timer++;     p[v][0] = par;     for (edge& e : g[v]) {       if (e.v != par) {         maxx[e.v][0] = e.c;         dfs(e.v, v);       }     }     tout[v] = timer++;   }   bool ancestor(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; }   int lca(int u, int v) {     if (ancestor(u, v)) {       return u;     }     for (int r = rmax - 1; r >= 0; --r) {       if (!ancestor(p[u][r], v)) {         u = p[u][r];       }     }     return p[u][0];   }   long long getMax(int u, int v) {     assert(u != v && ancestor(v, u));     long long ans = 0;     for (int r = rmax - 1; r >= 0; --r) {       if (!ancestor(p[u][r], v)) {         ans = max(ans, maxx[u][r]);         u = p[u][r];       }     }     return max(ans, maxx[u][0]);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     vector<edge> edges(m);     for (int i = 0; i < m; ++i) {       int u, v;       long long c;       cin >> u >> v >> c;       --u, --v;       edges[i] = {u, v, c};     }     sort((edges).begin(), (edges).end());     parent.resize(n);     size.assign(n, 1);     for (int i = 0; i < n; ++i) {       parent[i] = i;     }     long long total = 0;     vector<edge> useful;     for (int i = 0; i < m; ++i) {       int u = edges[i].u, v = edges[i].v;       u = get(u), v = get(v);       if (u == v) {         continue;       }       unite(u, v);       total += edges[i].c;       useful.push_back(edges[i]);     }     set<int> s;     for (int i = 0; i < n; ++i) {       s.insert(get(i));     }     if ((int)(s).size() == 1) {       p = vector<vector<int> >(n, vector<int>(rmax));       maxx = vector<vector<long long> >(n, vector<long long>(rmax));       g.resize(n);       for (edge& e : useful) {         int u = e.u, v = e.v;         long long c = e.c;         g[u].push_back({u, v, c});         g[v].push_back({v, u, c});       }       tin.resize(n);       tout.resize(n);       dfs(0, 0);       for (int r = 0; r + 1 < rmax; ++r) {         for (int v = 0; v < n; ++v) {           int t = p[v][r];           p[v][r + 1] = p[t][r];           maxx[v][r + 1] = max(maxx[v][r], maxx[t][r]);         }       }     }     int q;     cin >> q;     while (q--) {       int a, b;       cin >> a >> b;       --a, --b;       if ((int)(s).size() > 2) {         cout <<  -1 n ;         continue;       }       if ((int)(s).size() == 2) {         a = get(a), b = get(b);         if (a == b) {           cout <<  -1 n ;           continue;         }         cout << total <<   n ;         continue;       }       int w = lca(a, b);       long long ans = 0;       if (a != w) {         ans = max(ans, getMax(a, w));       }       if (b != w) {         ans = max(ans, getMax(b, w));       }       cout << total - ans <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const long long inf = (long long)1e18;   const long long mod = (long long)1e9 + 7;   const double eps = (double)1e-9;   const double pi = acos(-1.0);   const int dx[] = {0, 0, 1, 0, -1};   const int dy[] = {0, 1, 0, -1, 0};   const int N = 300500;   int n, dp[N];   vector<int> g[N], l;   vector<pair<int, int> > gr, G, ga[N];   void dfs(int v, int pr = -1) {     dp[v] = 1;     for (int to : g[v]) {       if (to != pr) {         dfs(to, v);         dp[v] += dp[to];       }     }   }   int dfs1(int v) {     for (int to : g[v]) {       if (dp[to] * 2 > n) {         dp[v] -= dp[to];         dp[to] += dp[v];         return dfs1(to);       }     }     return v;   }   void dfs2(int v, int pr, int gl = 0) {     gr.push_back(make_pair(gl, v));     for (int to : g[v]) {       if (to != pr) {         dfs2(to, v, gl + 1);       }     }   }   void checkv(int st) {     gr.clear();     dfs2(st, -1, 0);     sort(gr.begin(), gr.end());     for (int i = 0; i + 1 < n; ++i) {       if (gr[i].first == gr[i + 1].first &&           (int)g[gr[i].second].size() != (int)g[gr[i + 1].second].size())         return;     }     cout << st << endl;     exit(0);   }   int main() {     cin.tie(NULL);     cout.tie(NULL);     ios_base::sync_with_stdio(false);     cin >> n;     for (int i = 1; i < n; ++i) {       int x, y;       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);     }     dfs(1);     int centroid = dfs1(1);     checkv(centroid);     for (int i = 1; i <= n; ++i)       if ((int)g[i].size() == 1) l.push_back(i);     for (int x : l) {       int last = x;       int cur = g[x][0];       int dist = 1;       while ((int)g[cur].size() == 2) {         int nxt = g[cur][0];         if (nxt == last) nxt = g[cur][1];         dist++;         last = cur;         cur = nxt;       }       if (dist == n - 1) {         checkv(x);         cout << -1;         return 0;       }       ga[cur].push_back(make_pair(x, dist));       G.push_back(make_pair(dist, x));     }     for (int i = 1; i <= n; ++i) {       if ((int)ga[i].size() == 1) {         checkv(ga[i][0].first);         cout << -1;         return 0;       }     }     for (int i = 0; i + 1 < (int)G.size(); ++i)       if (G[i].first != G[i + 1].first) {         checkv(G[i].second);         checkv(G[i + 1].second);         cout << -1;         return 0;       }     cout << -1 << endl;     return 0;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, ns, a[1111];   void ok(long long x) {     long long l = 0, r = min(2000000000ll, (1ll << 62) / (x + 1) + 1);     while (l < r) {       long long m = (l + r) / 2;       if (m * (m - 1) / 2 + m * x >= n)         r = m;       else         l = m + 1;       if (m * (m - 1) / 2 + m * x == n && m % 2) a[++ns] = m + m * x;     }   }   int main() {     while (cin >> n) {       ns = 0;       for (int i = 0; i < 62; i++) ok((1ll << i) - 1);       if (!ns)         cout << -1 << endl;       else {         for (int i = 1; i <= ns; i++) cout << a[i] << endl;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char num[200001];   int numLen, m, temp, answer = 0;   int stepArr = 1;   int main() {     cin >> num >> m;     numLen = strlen(num);     answer += (num[numLen - 1] - 48) % m;     for (int i = numLen - 2; i >= 0; i--) {       stepArr = (stepArr * 10) % m;       answer += ((num[i] - 48) * stepArr) % m;       answer %= m;     }     temp = answer;     for (int i = 0; i < numLen - 1; i++) {       temp = ((((temp - (num[i] - 48) * stepArr) % m + m) * 10) % m +               (num[i] - 48) % m) %              m;       if (num[i + 1] ^ 48) {         answer = min(answer, temp);       }     }     cout << answer;     return 0;   }


#include <bits/stdc++.h>   template <typename T>   T mymax(T x, T y) {     return (x > y ? x : y);   }   using namespace std;   const int N = 100005;   const long long inf = 1e18;   vector<int> graph[N], cost[N];   int n, m;   long long dis[N];   struct node {     int nd, c;     node(int _nd, int _c) {       nd = _nd;       c = _c;     }     bool operator<(const node& p) const { return c > p.c; }   };   void dijkstra() {     priority_queue<node> pq;     pq.push(node(1, 0));     for (int i = 0; i < N; i++) dis[i] = inf;     dis[1] = 0;     while (!pq.empty()) {       node top = pq.top();       pq.pop();       int u = top.nd;       for (int i = 0; i < (int)graph[u].size(); i++) {         int v = graph[u][i];         if (dis[v] > dis[u] + cost[u][i]) {           dis[v] = dis[u] + cost[u][i];           pq.push(node(v, dis[v]));         }       }     }     queue<int> q;     q.push(n);     vector<int> ans;     ans.push_back(n);     while (!q.empty()) {       int u = q.front();       q.pop();       for (int i = 0; i < graph[u].size(); i++) {         int v = graph[u][i];         if (dis[u] - cost[u][i] == dis[v]) {           ans.push_back(v);           q.push(v);           break;         }       }     }     if (ans.size() == 1) {       printf( -1 n );       return;     }     reverse((ans).begin(), (ans).end());     for (int x : ans) {       printf( %d  , x);     }     printf(  n );   }   void solve() {     cin >> n >> m;     for (int i = 0; i < m; i++) {       int u, v, w;       cin >> u >> v >> w;       graph[u].push_back(v);       graph[v].push_back(u);       if (cost[u].size() == 0) {         cost[u].push_back(w);         cost[v].push_back(w);       } else {         int mn = 1e9;         for (int i = 0; i < graph[u].size() - 1; i++) {           int adj = graph[u][i];           if (adj == v) {             mn = min(mn, cost[u][i]);           }         }         mn = min(mn, w);         cost[u].push_back(mn);         cost[v].push_back(mn);       }     }     dijkstra();     return;   }   int main() {     solve();     return 0;   }


#include <bits/stdc++.h>   const int N = 150;   int n;   double p[N];   void init() {     std::cin >> n;     for (int i = 1; i <= n; i++) {       scanf( %lf , &p[i]);       p[i] /= 100.0;     }   }   void work() {     static double f[N];     for (int i = 1; i <= n; i++) {       f[i] = 1.0;     }     double answer = 1.0;     double pr = 1.0;     for (int t = 2; t <= 500000; t++) {       int best = -1;       for (int i = 1; i <= n; i++) {         if (best == -1 || (1.0 - f[i] * (1.0 - p[i])) * (1.0 - f[best]) >                               (1.0 - f[best] * (1.0 - p[best])) * (1.0 - f[i])) {           best = i;         }       }       f[best] *= 1.0 - p[best];       pr = 1.0;       for (int i = 1; i <= n; i++) {         pr *= 1.0 - f[i];       }       answer += 1.0 - pr;     }     printf( %.10f n , answer);   }   int main() {     init();     work();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int s[51];   int main() {     int n, g, check = 0, t = 0, q = 0;     cin >> n;     g = (n * (n - 1)) / 2;     pair<int, int> can[g - 1];     for (int i = 0; i < g - 1; i++) {       cin >> can[i].first >> can[i].second;       s[can[i].first]++;       s[can[i].second]++;     }     for (int i = 1; i <= n; i++) {       if (s[i] < n - 1) {         if (t == 0)           t = i;         else           q = i;       }     }     for (int i = 0; i < g; i++) {       if (can[i].first == t) {         check = 1;         for (int r = 0; r < g; r++) {           if (can[r].first == can[i].second && can[r].second == q) {             cout << t <<     << q;             return 0;           }         }       }     }     if (check == 0) {       cout << q <<     << t;       return 0;     }     for (int i = 0; i < g; i++) {       if (can[i].first == q) {         for (int r = 0; r < g; r++) {           if (can[r].first == can[i].second && can[r].second == t) {             cout << q <<     << t;             return 0;           }         }       }     }     cout << t <<     << q;   }


#include <bits/stdc++.h>   using namespace std;   #pragma optimize( -O3 )   string to_string(string s) { return     + s +    ; }   string to_string(const char* s) { return to_string((string)s); }   string to_string(const 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(const 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...);   }   template <typename T>   inline void _read(T& x) {     cin >> x;   }   template <typename A, typename B>   inline void _read(pair<A, B>& x) {     _read(x.first);     _read(x.second);   }   template <typename T>   inline void _read(vector<T>& x) {     for (auto& v : x) _read(v);   }   void R() {}   template <typename T, typename... U>   void R(T& head, U&... tail) {     _read(head);     R(tail...);   }   template <typename T>   inline void _write(const T& x) {     cout << x <<    ;   }   template <typename A, typename B>   inline void _write(const pair<A, B>& x) {     _write(x.first);     _write(x.second);   }   template <typename T>   inline void _write(const vector<T>& in) {     for (const auto& x : in) _write(x);   }   void W() { cout <<   n ; }   template <typename T, typename... U>   void W(const T& head, const U&... tail) {     _write(head);     W(tail...);   }   void go();   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     go();     return 0;   }   int solve(int a, int b) {     if (a == 1 && b == 2) return 3;     if (a == 1 && b == 3) return 4;     if (a == 2 && b == 1) return 3;     if (a == 2 && b == 3) {       W( Infinite );       exit(0);     }     if (a == 3 && b == 1) return 4;     if (a == 3 && b == 2) {       W( Infinite );       exit(0);     }   }   void go() {     int n;     R(n);     vector<int> a(n);     R(a);     int ans = 0;     for (int i = 1; i < n; i++) {       ans += solve(a[i - 1], a[i]);     }     for (int i = 2; i < n; i++)       if (a[i - 2] == 3 && a[i - 1] == 1 && a[i] == 2) ans--;     W( Finite );     W(ans);   }


#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1);   int S, n, tot;   int fa[2100], a[2100][2100], p[2100 * 2100 / 2];   vector<int> E[2100];   void EXT() {     printf( NO n );     exit(0);   }   int get(int x) {     if (fa[x] == x) return x;     return fa[x] = get(fa[x]);   }   void dfs(int x, int ff, int dep) {     if (a[S][x] != dep) EXT();     for (int i : E[x])       if (i != ff) dfs(i, x, dep + a[x][i]);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j) scanf( %d , &a[i][j]);     for (int i = 1; i <= n; ++i) {       if (a[i][i] != 0) EXT();       for (int j = i + 1; j <= n; ++j) {         if (a[i][j] != a[j][i] || a[i][j] == 0) EXT();         p[++tot] = (i << 20) + j;       }     }     sort(p + 1, p + tot + 1, [](int x, int y) {       return a[x >> 20][x & 0xffff] < a[y >> 20][y & 0xffff];     });     for (int i = 1; i <= n; ++i) fa[i] = i;     for (int i = 1; i <= tot; ++i) {       int x = p[i] >> 20, y = p[i] & 0xffff;       if (get(x) == get(y)) continue;       E[x].push_back(y);       E[y].push_back(x);       fa[get(x)] = get(y);     }     for (int i = 1; i <= n; ++i) {       S = i;       dfs(i, 0, 0);     }     printf( YES n );   }


#include <bits/stdc++.h>   using namespace std;   int top;   int t[10000];   long long sum[100000];   int ans[5002][5002];   const int MOD = 1e9 + 7;   int main() {     int n, a, b, k;     scanf( %d%d%d%d , &n, &a, &b, &k);     if (a < b)       top = b - 1;     else       top = abs(n - b);     for (int i = 1; i <= top; i++) {       int far = i * 2 - 1;       if (far <= top)         ans[i][1] = 2 * (i - 1);       else         ans[i][1] = i - 1 + (top - i);     }     for (int i = 1; i <= top; i++) sum[i] = (sum[i - 1] + t[i]) % MOD;     for (int j = 2; j <= k; j++) {       for (int q = 1; q <= top; q++) sum[q] = (sum[q - 1] + ans[q][j - 1]) % MOD;       for (int i = 1; i <= top; i++) {         int far = i * 2 - 1;         if (far <= top)           ans[i][j] = (sum[i - 1] + sum[far]) % MOD - sum[i];         else           ans[i][j] = (sum[i - 1] + sum[top]) % MOD - sum[i];         ans[i][j] = ans[i][j] % MOD;       }     }     printf( %d n , (ans[abs(a - b)][k] + MOD) % MOD);   }


#include <bits/stdc++.h>   using namespace std;   int level(int n, int sum[]) {     int i;     for (i = 1; i < 11; i++) {       if (n > sum[i - 1] && n <= sum[i]) {         return i;       }     }   }   int number(int n, int l, int sum[]) {     int st = sum[l - 1] + 1;     return n - st + 1;   }   int main() {     int sum[11] = {0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55};     int n, t, i, j, k;     cin >> n >> t;     int ans = 0;     vector<int> adj[60];     for (i = 1; i < 56; i++) {       int l = level(i, sum);       int num = number(i, l, sum);       adj[i].push_back(sum[l] + num);       adj[i].push_back(sum[l] + num + 1);     }     double w[100] = {0};     double one = 1.00;     for (j = 1; j <= t; j++) {       w[1] += one;       for (i = 1; i < 56; i++) {         if (w[i] >= one) {           double rem = (w[i] - one) / 2.00;           w[i] = one;           for (k = 0; k < adj[i].size(); k++) {             w[adj[i][k]] += rem;           }         }       }     }     for (i = 1; i <= sum[n]; i++) {       if (w[i] == one) ans++;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   double p[110], S;   int main() {     scanf( %d , &n);     for (int i = 0; i <= n; i++) scanf( %lf , p + i);     for (int i = 0; i < n; i++)       for (int j = i + 1; j <= n; j++)         if (i * 2 <= n && n <= j * 2)           S = max(               S, (n * 0.5 - j) / (i - j) * p[i] + (n * 0.5 - i) / (j - i) * p[j]);     printf( %.9lf n , S);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 51;   const int tot = maxn * maxn;   long long input[maxn];   long long dp[maxn][tot][maxn];   long long dp2[maxn][tot][maxn];   long long comb[maxn + 1][maxn + 1];   int N, M;   void init() {     cin >> N;     for (int i = 0; i < N; i++) {       cin >> input[i];     }     cin >> M;     comb[0][0] = 1;     for (int i = 1; i <= maxn; i++) {       comb[i][0] = 1;       for (int j = 1; j <= i; j++) {         comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1];       }     }   }   long long factorial[maxn];   bool vst[maxn];   void run() {     int total = 0;     for (int i = 0; i < N; i++) {       total += input[i];     }     if (total <= M) {       cout << N << endl;       return;     }     for (int i = 0; i < N; i++) {       memset(vst, 0, sizeof(vst));       vst[i] = 1;       dp[i][0][0] = 1;       int cnt = 0;       for (int j = 0; j < N; j++) {         if (vst[j] == 0) {           cnt++;           for (int k = 0; k < tot; k++) {             for (int t = 0; t <= cnt; t++) {               dp2[i][k][t] = dp[i][k][t];             }           }           for (int k = 0; k < tot; k++) {             for (int t = 0; t <= cnt; t++) {               if (dp[i][k][t] && (k + input[j] < tot)) {                 dp2[i][k + input[j]][t + 1] += dp[i][k][t];               }             }           }           for (int k = 0; k < tot; k++) {             for (int t = 0; t <= cnt; t++) {               dp[i][k][t] = dp2[i][k][t];             }           }         }       }     }     double ans = 0;     for (int i = M; i >= 0; i--) {       for (int j = 0; j < N; j++) {         if (i + input[j] > M) {           for (int k = 0; k < N; k++) {             double tmp = dp[j][i][k] * 1.0 / comb[N][k];             tmp /= (N - k);             tmp *= k;             ans += tmp;           }         }       }     }     printf( %.8lf n , ans);   }   void baoli() {     const int n = 50;     int tmp[n];     cout << n <<    ;     for (int i = 0; i < n; i++) {       tmp[i] = i + 1;       cout << tmp[i] <<    ;     }     int m = 100;     cout << m << endl;     double ans = 0;     for (int test = 0; test <= 10000; test++) {       random_shuffle(tmp, tmp + n);       int sum = 0;       for (int i = 0; i < n; i++) {         sum += tmp[i];         if (sum > m) {           ans += i;           break;         }       }     }     cout << ans / 10000 << endl;   }   int main() {     init();     run();     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long int a, b, c;     cin >> a >> b >> c;     a = abs(a) + abs(b);     if (a > c)       cout <<  NO  << endl;     else {       if ((c - a) % 2 == 0)         cout <<  YES  << endl;       else         cout <<  NO  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     string a;     for (int i = 0; i <= 9; i++) {       cout << i << endl;       cout << flush;       a =   ;       getline(cin, a);       if (a ==  no ) {         continue;       } else {         if (a ==  don t think so  || a ==  great!  || a ==  don t touch me!  ||             a ==  not bad  || a ==  cool ) {           cout <<  normal ;         } else {           if (a ==  are you serious  || a ==  worse  || a ==  terrible  ||               a ==  don t even  || a ==  no way  || a ==  go die in a hole ) {             cout <<  grumpy ;           } else {             cout <<  normal ;           }         }         return 0;       }     }   }


#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   vector<int> v[N];   int num[N];   int q[N];   int cnt;   int check(int mid) {     int t = num[0] + mid, res = 0, tot = 0;     for (int i = 1; i <= 100000; i++) {       for (int j = 0; j <= max(-1, num[i] - t); j++) tot++, res += v[i][j];     }     if (tot > mid) return 1 << 30;     cnt = 0;     for (int i = 1; i <= 100000; i++) {       for (int j = max(0, num[i] - t + 1); j <= num[i] - 1; j++)         q[++cnt] = v[i][j];     }     sort(q + 1, q + cnt + 1);     for (int i = 1; i <= mid - tot; i++) res += q[i];     return res;   }   int main() {     int x, y;     int n;     ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n;     for (int i = 0; i < n; i++) {       cin >> x >> y;       if (x == 0 || y == 0)         num[0]++;       else         v[x].push_back(y), num[x]++;     }     for (int i = 1; i <= 100000; i++) {       sort(v[i].begin(), v[i].end());     }     int f = 1;     for (int i = 1; i <= n; i++) {       if (num[i] >= num[0]) f = false;     }     if (f) {       puts( 0 );       return 0;     }     int l = 1, r = n;     int ans = 1 << 30;     while (l <= r) {       int mid1 = l + (r - l) / 3;       int f1 = check(mid1);       int mid2 = r - (r - l) / 3;       int f2 = check(mid2);       if (f1 <= f2)         r = mid2 - 1, ans = min(ans, f1);       else         l = mid1 + 1, ans = min(ans, f2);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize(2)   using namespace std;   const long long INF = 1e17 + 7;   const int N = 1e4 + 7;   const int M = 5e6 + 7;   long long x[N], y[N];   long long ax, ay, bx, by, a, b, t;   signed main() {     ios::sync_with_stdio(false);     cin >> x[0] >> y[0] >> ax >> ay >> bx >> by;     int n = 0;     for (; x[n] < INF && y[n] < INF; n++) {       x[n + 1] = x[n] * ax + bx;       y[n + 1] = y[n] * ay + by;       if (x[n + 1] >= INF || y[n + 1] >= INF) break;     }     cin >> a >> b >> t;     long long ans = 0;     for (int l = 0; l <= n; l++) {       for (int r = l; r <= n; r++) {         unsigned long long to =             min(abs(a - x[l]) + abs(b - y[l]), abs(a - x[r]) + abs(b - y[r]));         to += abs(x[l] - x[r]) + abs(y[l] - y[r]);         if (to <= t) ans = max(ans, r - l + 1ll);       }     }     cout << ans <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void fastio() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);   }   int main() {     fastio();     long long int a, b;     long long int mx = INT_MIN;     long long int n, mn = INT_MAX;     cin >> n;     long long int ar[n];     for (int i = 0; i < n; i++) {       cin >> ar[i];       mn = min(mn, ar[i]);       mx = max(ar[i], mx);     }     long long int res = mx - mn + 1;     cout << res - n;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());   int len(pair<int, int> xy) { return xy.first + xy.second; }   pair<int, int> jump(pair<int, int> xy, int len) {     if (len == 0) return xy;     auto [x, y] = xy;     if (len > x + y) return {0, 0};     int bit = 31 - __builtin_clz(len);     int cur = x ^ y;     int take = cur & ((1 << bit) - 1);     assert(take <= len);     x &= ~take;     y &= ~take;     len -= take;     while (len > 0) {       int bit = __builtin_ctz(x ^ y);       int take = min(1 << bit, len);       if ((x >> bit) & 1)         x -= take;       else         y -= take;       len -= take;     }     return {x, y};   }   pair<int, int> lca(pair<int, int> u, pair<int, int> v) {     int lu = len(u), lv = len(v);     if (lu > lv) {       swap(lu, lv);       swap(u, v);     }     v = jump(v, lv - lu);     if (u == v) return u;     for (int bit = 29; bit >= 0; bit--) {       auto nu = jump(u, 1 << bit), nv = jump(v, 1 << bit);       if (nu != nv) tie(u, v) = {nu, nv};     }     return jump(u, 1);   }   bool cmp(pair<int, int> u, pair<int, int> v) {     if (u == v) return false;     bool swp = false;     int lu = len(u), lv = len(v);     if (lu > lv) {       swap(lu, lv);       swap(u, v);       swp = true;     }     v = jump(v, lv - lu);     if (u == v) return true ^ swp;     return (u > v) ^ swp;   }   using event = pair<pair<int, int>, int>;   vector<event> evt;   struct node {     int val, depth;     node *left, *right;   };   node *build(int from, int to) {     if (from >= to) return nullptr;     int val = 0;     auto w = lca(evt[from].first, evt[to - 1].first);     while (from < to && evt[from].first == w) {       val += evt[from].second;       ++from;     }     auto [wx, wy] = w;     if ((wx + 1) & wy) return new node({val, len(w), nullptr, build(from, to)});     if (wx & (wy + 1)) return new node({val, len(w), build(from, to), nullptr});     pair<int, int> rchild(wx, wy + 1);     int lo = from - 1, hi = to;     while (lo + 1 < hi) {       auto mi = (lo + hi) / 2;       if (cmp(evt[mi].first, rchild))         lo = mi;       else         hi = mi;     }     return new node({val, len(w), build(from, hi), build(hi, to)});   }   vector<int> change;   void add(int l, int r) {     change.push_back(l);     change.push_back(r);   }   void simplify() {     sort((change).begin(), (change).end());     vector<int> st;     for (auto x : change) {       if (!st.empty() && st.back() == x)         st.pop_back();       else         st.push_back(x);     }     change = st;   }   int dfs(node *u) {     int ans = 0;     if (u->left) {       auto s = dfs(u->left);       if (s > 0) add(u->depth + 1, u->left->depth);       ans += s;     }     if (u->right) {       auto s = dfs(u->right);       if (s > 0) add(u->depth + 1, u->right->depth);       ans += s;     }     ans += u->val;     if (ans > 0) add(u->depth, u->depth + 1);     return ans;   }   void print(node *u, string tab =   ) {     if (!u) {       cerr << tab <<  X n ;       return;     }     cerr << tab << u->val <<     << u->depth << endl;     auto ntab = tab +     ;     print(u->left, ntab);     print(u->right, ntab);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int m;     cin >> m;     while (m--) {       int x1, y1, x2, y2;       cin >> x1 >> y1 >> x2 >> y2;       pair<int, int> u(x1, y1), v(x2, y2);       pair<int, int> w = lca(u, v);       evt.emplace_back(u, +1);       evt.emplace_back(v, +1);       evt.emplace_back(w, -1);       if (len(w)) {         w = jump(w, 1);         evt.emplace_back(w, -1);       }     }     sort((evt).begin(), (evt).end(),          [](const event &a, const event &b) { return cmp(a.first, b.first); });     auto tree = build(0, ((int)(evt).size()));     dfs(tree);     simplify();     int ans = ((int)(change).size());     if (ans > 0 && change[0] == 0) --ans;     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   vector<int> nums, fact;   int a, b, n;   void precomp() {     fact.resize(n + 1);     fact[0] = 1;     for (int i = 1; i <= n; ++i) fact[i] = 1ll * fact[i - 1] * i % mod;   }   void gener() {     int _max = log10(max(a, b) * n);     queue<int> q;     q.push(0);     while (!q.empty()) {       int top = q.front();       q.pop();       nums.push_back(top);       if ((int)log10(top) == _max) continue;       for (int i : {a, b}) q.push(10 * top + i);     }   }   void egcd(int a, int b, int& x, int& y) {     if (b == 0) {       x = 1;       y = 0;       return;     }     int x1, y1;     egcd(b, a % b, x1, y1);     x = y1;     y = x1 - (a / b) * y1;   }   int inv_mod(int a) {     int x, y;     egcd(a, mod, x, y);     return (x + mod) % mod;   }   int binom(int n, int k) {     int top = fact[n];     int bot = 1ll * fact[k] * fact[n - k] % mod;     return 1ll * top * inv_mod(bot) % mod;   }   int main() {     cin >> a >> b >> n;     precomp();     gener();     int ans = 0;     for (int c : nums) {       int top = c - b * n;       int bot = a - b;       if (top % bot) continue;       int res = top / bot;       if (res >= 0 && res <= n) ans = (ans + binom(n, res)) % mod;     }     cout << ans << endl;   }