#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   int n, k;   string s;   bool can_del(char c) {     int l = N, r = -1;     for (int i = 0; i < n; ++i) {       if (s[i] == c) {         l = min(l, i);         r = max(r, i);       }     }     return r - l + 1 <= k;   }   string ans1 =  tokitsukaze , ans2 =  quailty , ans3 =  once again ;   bool win(string s) { return can_del( 0 ) || can_del( 1 ); }   vector<int> pos[2];   int ml[2], mr[2];   void f(int l, int r, int t) {     if (pos[t].size() && pos[t][0] < l) ml[t] = min(ml[t], pos[t][0]);     auto p = upper_bound(pos[t].begin(), pos[t].end(), r);     if (p != pos[t].end()) {       ml[t] = min(ml[t], *p);     }     if (pos[t].size() && pos[t].back() > r) mr[t] = max(mr[t], pos[t].back());     p = lower_bound(pos[t].begin(), pos[t].end(), l);     if (p != pos[t].begin()) {       --p;       mr[t] = max(mr[t], *p);     }   }   bool col(int l, int r, int t) {     for (int i = 0; i < 2; ++i) {       ml[i] = N;       mr[i] = -1;     }     ml[t] = l;     mr[t] = r;     f(l, r, 0);     f(l, r, 1);     for (int i = 0; i < 2; ++i) {       if (mr[i] - ml[i] + 1 <= k) {         return 0;       }     }     return 1;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> k >> s;     for (int i = 0; i < n; ++i) {       pos[s[i] -  0 ].push_back(i);     }     if (win(s)) {       cout << ans1 <<   n ;       exit(0);     }     for (int i = 0; i + k - 1 < n; ++i) {       if (col(i, i + k - 1, 0)) {         cout << ans3 <<   n ;         exit(0);       }       if (col(i, i + k - 1, 1)) {         cout << ans3 <<   n ;         exit(0);       }     }     cout << ans2 <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, size = 1e6 + 1;     cin >> n;     vector<int> prime(1e6 + 1, 1);     prime[0] = 0;     prime[1] = 0;     for (int ri = 2; ri <= sqrt(size); ri++) {       if (prime[ri]) {         for (int pi = ri * ri; pi <= size; pi += ri)           if (prime[pi]) prime[pi] = 0;       }     }     vector<long long int> tprime;     for (long long int ri = 0; ri < size; ri++) {       if (prime[ri]) {         tprime.push_back(ri * ri);       }     }     long long int arr[n];     for (int ri = 0; ri < n; ri++) scanf( %lld , &arr[ri]);     for (int ri = 0; ri < n; ri++) {       bool val = binary_search(tprime.begin(), tprime.end(), arr[ri]);       if (val)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 200010;   int a[maxn];   int main() {     int t;     cin >> t;     while (t--) {       string s;       cin >> s;       int m = s.length();       int fa = 0, fb = 0, fc = 0;       for (int i = 0; i < m; i++) {         if (s[i] ==  1  && fa == 0) fa = i + 1;         if (s[i] ==  2  && fb == 0) fb = i + 1;         if (s[i] ==  3  && fc == 0) fc = i + 1;       }       if (fa == 0 || fb == 0 || fc == 0) {         cout <<  0  << endl;         continue;       }       int x = max(fa, max(fb, fc));       int ans = x - min(fa, min(fb, fc)) + 1;       int sum = ans;       for (int i = 0; i < m; i++) {         if (s[i] ==  1 ) {           int u = min(fb, fc);           int v = max(fb, fc);           if (i + 1 >= v)             sum = i + 1 - u + 1;           else if (i + 1 <= u)             sum = v - i;           else             sum = v - u + 1;           ans = min(ans, sum);           fa = i + 1;         }         if (s[i] ==  2 ) {           int u = min(fa, fc);           int v = max(fa, fc);           if (i + 1 >= v)             sum = i + 1 - u + 1;           else if (i + 1 <= u)             sum = v - i;           else             sum = v - u + 1;           ans = min(ans, sum);           fb = i + 1;         }         if (s[i] ==  3 ) {           int u = min(fb, fa);           int v = max(fb, fa);           if (i + 1 >= v)             sum = i + 1 - u + 1;           else if (i + 1 <= u)             sum = v - i;           else             sum = v - u + 1;           ans = min(ans, sum);           fc = i + 1;         }       }       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MaxLg = 25, inf = 1e9 + 100;   int n;   bool flag;   vector<int> out, mins[2][MaxLg];   void output() {     if (flag)       cout << -1 <<   n ;     else       cout << out.size() <<   n ;     for (auto i : out) cout << i <<    ;     cout << endl;   }   void input(int a, int b) {     int tmp;     for (int i = 0; i < n; ++i) {       cin >> tmp;       mins[a][b].push_back(tmp);     }   }   void solve() {     for (int i = 0; i < n; ++i) out.push_back(inf);     for (int i = 0; (1 << i) <= n; ++i)       for (int j = 1; j <= n; ++j)         out[j - 1] = min(out[j - 1], mins[!(j & (1 << i))][i][j - 1]);     flag = true;   }   int main() {     cin >> n;     for (int k = 0; k < 2; ++k)       for (int i = 0; (1 << i) <= n; ++i) {         for (int j = 1; j <= n; ++j)           if ((j & (1 << i)) == k * (1 << i)) out.push_back(j);         output();         input(k, i);         out.clear();       }     solve();     output();     return 0;   }


#include <bits/stdc++.h>   const bool debugging = 1;   const long long mod = 1e9 + 7;   using namespace std;   template <class T>   ostream &operator<<(ostream &os, vector<T> V) {     for (auto v : V) {       os << v <<    ;     }     return os <<   ;   }   template <class T>   ostream &operator<<(ostream &os, vector<vector<T>> V) {     for (long long i = 0; i < V.size(); i++) {       for (long long j = 0; j < V[i].size(); j++) {         os << V[i][j] <<    ;       }       os <<   n ;     }     return os <<   ;   }   template <class T>   ostream &operator<<(ostream &os, set<T> S) {     for (auto s : S) {       os << s <<    ;     }     return os <<   ;   }   template <class L, class R>   ostream &operator<<(ostream &os, pair<L, R> P) {     return os << P.first <<     << P.second;   }   template <class L, class R>   ostream &operator<<(ostream &os, map<L, R> M) {     os <<   n ;     for (auto m : M) {       os << m <<   n ;     }     return os <<   ;   }   void solve() {     long long N;     cin >> N;     vector<long long> elements(N);     for (long long i = 0; i < N; i++) {       cin >> elements[i];     }     vector<long long> max_val(N);     long long contri_front = elements[0];     long long contri_end = 0;     long long index = N;     bool single = 1;     for (long long i = 0; i < N; i++) {       if (contri_front >= elements[i] && single) {         contri_front = elements[i];         elements[i] = 0;       } else {         single = 0;         long long need_from_end = elements[i] - contri_front;         if (need_from_end > elements[N - 1]) {           cout <<  NO                 <<   n ;           return;         }         if (need_from_end < contri_end) {           contri_front = elements[i] - contri_end;           if (contri_front < 0) {             cout <<  NO                   <<   n ;             return;           }         } else {           contri_end = need_from_end;         }       }     }     cout <<  YES           <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long no_of_testcases;     cin >> no_of_testcases;     while (no_of_testcases-- > 0) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   const double PI =       3.141592653589793238462643383279502884197169399375105820974944;   using namespace std;   const int MOD = 1e9 + 7;   double gcd(double a, double b) { return a < 0.01 ? b : gcd(fmod(b, a), a); }   template <typename T>   T mymax(T a, T b) {     return (a > b) ? a : b;   }   template <typename T>   T mymax(T a, T b, T c) {     return mymax(a, mymax(b, c));   }   template <typename T>   T mymin(T a, T b) {     return (a < b) ? a : b;   }   template <typename T>   T mymin(T a, T b, T c) {     return mymin(a, mymin(b, c));   }   template <typename T>   T power(T x, T y) {     T res = 1;     x = x % MOD;     while (y > 0) {       if (y & 1) res = (res * x) % MOD;       y = y >> 1;       x = (x * x) % MOD;     }     return res;   }   template <typename T>   void swap(T *x, T *y) {     T temp;     temp = *y;     *y = *x;     *x = temp;   }   template <typename T>   T mod(T a) {     if (a > 0)       return a;     else       return -a;   }   template <typename T>   T logg(T a) {     T x = 0;     while (a > 1) {       x++;       a /= 2;     }     return x;   }   typedef struct trie {     int a, b, c;     trie(int p1, int p2, int p3) { a = p1, b = p2, c = p3; }   } tri;   bool operator<(tri p, tri q) {     if (p.a < q.a) {       return 1;     } else if (p.a > q.a)       return 0;     else {       return (p.b < q.b);     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int n, k1;     cin >> n >> k1;     multiset<pair<int, int>> k;     vector<int> l(n), r(n);     vector<tri> p;     for (int i = 1; i <= n; i++) {       cin >> l[i - 1] >> r[i - 1];       p.push_back({l[i - 1], -1, i - 1});       p.push_back({r[i - 1], 1, i - 1});     }     int c = 0;     sort(p.begin(), p.end());     vector<int> ans;     for (tri p1 : p) {       int a1 = p1.a, b1 = p1.b, c1 = p1.c;       if (b1 == -1) {         k.insert({r[c1], c1});         c++;         if (c > k1) {           pair<int, int> pk = *k.rbegin();           k.erase(k.find(pk));           ans.push_back(pk.second);           c--;         }       } else {         if (k.count({r[c1], c1})) {           c--;           k.erase(k.find({r[c1], c1}));         }       }     }     cout << ans.size() <<   n ;     for (int pk : ans) {       cout << pk + 1 <<    ;     }     cout <<   n ;     return 0;   }


#include<iostream>   using namespace std;   int main()   {       int t;       cin>>t;       while(t--)       {         int Xa,Ya,Xb,Yb,Xf,Yf,sum;        cin>>Xa>>Ya>>Xb>>Yb>>Xf>>Yf;        sum=abs(Xa-Xb)+abs(Ya-Yb);        if(Xa==Xb&&Xb==Xf)           {             if(Ya<Yf&&Yb>Yf||Ya>Yf&&Yb<Yf)               sum+=2;           }       if(Ya==Yb&&Yb==Yf)       {           if(Xa<Xf&&Xb>Xf||Xa>Xf&&Xb<Xf)           sum+=2;       }        cout<<sum<<endl;       }     return 0;   }


#include <bits/stdc++.h>   #pragma comment(linker,  /stack:640000000 )   using namespace std;   const double EPS = 1e-9;   const int INF = 0x7f7f7f7f;   const double PI = acos(-1.0);   template <class T>   inline T gcd(T a, T b) {     return (b) == 0 ? (a) : gcd((b), ((a) % (b)));   }   template <class T>   inline T lcm(T a, T b) {     return ((a) / gcd((a), (b)) * (b));   }   template <typename T>   string NumberToString(T Number) {     ostringstream second;     second << Number;     return second.str();   }   struct debugger {     template <typename T>     debugger& operator,(const T& v) {       cerr << v <<    ;       return *this;     }   } dbg;   int t, n, m;   string s;   int main() {     while (~scanf( %d%d , &n, &m)) {       long long k;       scanf( %lld , &k);       if (k < n) {         printf( %lld 1 n , k + 1);         continue;       }       k -= (n - 1);       int row = k / (m - 1);       if (k % (m - 1) == 0) row--;       int col = k % (m - 1);       if (k % (m - 1) == 0) col = m - 1;       int odd = row % 2;       row = n - row;       if (odd)         col = m - col + 1;       else         col = col + 1;       printf( %d %d n , row, col);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int read() {     int X = 0, w = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) w = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) X = X * 10 + c -  0 , c = getchar();     return X * w;   }   const int N = 1500 + 10, K = 100000 + 10;   const int mod = 1e9 + 7;   int qpow(int a, int b) {     int c = 1;     for (; b; b >>= 1, a = 1ll * a * a % mod)       if (b & 1) c = 1ll * c * a % mod;     return c;   }   int n, m, a, b, k, p;   int fac[K], ifac[K], d[K], sd[N], fr[N][N], sr[N][N], ss[N][N];   int C(int n, int m) { return 1ll * fac[n] * ifac[m] % mod * ifac[n - m] % mod; }   int main() {     n = read(), m = read(), a = read(), b = read(), k = read();     p = 1ll * a * qpow(b, mod - 2) % mod;     fac[0] = 1;     for (int i = 1; i <= k; ++i) fac[i] = 1ll * fac[i - 1] * i % mod;     ifac[k] = qpow(fac[k], mod - 2);     for (int i = k; i; --i) ifac[i - 1] = 1ll * ifac[i] * i % mod;     for (int i = 0; i <= k; ++i)       d[i] = 1ll * C(k, i) * qpow(p, i) % mod * qpow(1 - p + mod, k - i) % mod;     for (int i = 1; i <= m; ++i) sd[i] = (sd[i - 1] + d[i - 1]) % mod;     fr[0][m] = sr[0][m] = 1;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) {         fr[i][j] = (1ll * (sr[i - 1][m] - sr[i - 1][m - j] + mod) * sd[j] -                     ss[i - 1][j] + mod) %                    mod * d[m - j] % mod;         sr[i][j] = (sr[i][j - 1] + fr[i][j]) % mod;         ss[i][j] = (ss[i][j - 1] + 1ll * d[j - 1] * sr[i][j - 1]) % mod;       }     printf( %d n , sr[n][m]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   enum { MAXN = 4010, MAXC = 26 };   int dp[MAXN][MAXN];   char sa[MAXN], sb[MAXN];   int pra[MAXN][MAXC], prb[MAXN][MAXC];   int main() {     int ci, cd, cr, cs;     scanf( %d%d%d%d , &ci, &cd, &cr, &cs);     scanf( %s , sa + 1);     scanf( %s , sb + 1);     int la = strlen(sa + 1);     fill(pra[0], pra[0] + MAXC, 0);     for (int i = 1; i <= la; i++) {       sa[i] -=  a ;       copy(pra[i - 1], pra[i - 1] + MAXC, pra[i]);       pra[i][sa[i]] = i;     }     int lb = strlen(sb + 1);     fill(prb[0], prb[0] + MAXC, 0);     for (int i = 1; i <= lb; i++) {       sb[i] -=  a ;       copy(prb[i - 1], prb[i - 1] + MAXC, prb[i]);       prb[i][sb[i]] = i;     }     for (int i = 0; i <= la; i++) dp[i][0] = cd * i;     for (int i = 0; i <= lb; i++) dp[0][i] = ci * i;     for (int i = 1; i <= la; i++)       for (int j = 1; j <= lb; j++) {         dp[i][j] = min(min(dp[i - 1][j] + cd, dp[i][j - 1] + ci),                        dp[i - 1][j - 1] + (sa[i] == sb[j] ? 0 : cr));         if (sa[i] != sb[j] && pra[i][sb[j]] && prb[j][sa[i]]) {           dp[i][j] = min(dp[i][j], dp[pra[i][sb[j]] - 1][prb[j][sa[i]] - 1] +                                        (i - pra[i][sb[j]] - 1) * cd + cs +                                        (j - prb[j][sa[i]] - 1) * ci);         }       }     printf( %d n , dp[la][lb]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long int n;     cin >> n;     long long int a[n];     long long int sum = 0;     for (long long int i = 0; i < n; i++) {       cin >> a[i];       sum += a[i];     }     sort(a, a + n);     long long int req = ceil(sum / (long double)(n - 1));     cout << max(req, a[n - 1]);   }


#include <bits/stdc++.h>   using namespace std;   int w, m;   long long x = 1;   bool f = 1;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> w >> m;     while (m && f) {       if ((m - 1) % w == 0 || m % w == 0 || (m + 1) % w == 0) {         if ((m - 1) % w == 0 || m % w == 0)           m /= w;         else           m = (m + 1) / w;       } else         f = 0;     }     if (f)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long nax = 1e5 + 10;   vector<long long> g[nax];   signed main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     ;     long long n;     cin >> n;     long long f = 0;     vector<pair<pair<long long, long long>, long long> > a(n - 2);     for (long long i = 0; i < n - 2; i++) {       long long x, y, z;       cin >> x >> y >> z;       a[i].first.first = x;       a[i].first.second = y;       a[i].second = z;       g[x].push_back(i);       g[y].push_back(i);       g[z].push_back(i);     }     for (long long i = 1; i <= n; i++) {       if (g[i].size() == 1) {         f = i;         break;       }     }     vector<long long> ans;     long long id = g[f][0];     long long cur1 = -1, cur2 = -1;     if (a[id].first.first != f) {       cur1 = a[id].first.first;       if (a[id].second != f)         cur2 = a[id].second;       else         cur2 = a[id].first.second;     } else {       cur1 = a[id].first.second, cur2 = a[id].second;     }     ans.push_back(f);     ans.push_back(cur1);     ans.push_back(cur2);     long long cd = cur1;     long long dc = cur2;     set<long long> yy;     yy.insert(id);     while (ans.size() < n) {       for (auto &i : g[cur1])         if (yy.find(i) == yy.end()) id = i;       yy.insert(id);       if (a[id].first.first != cur1 && a[id].first.first != cur2)         ans.push_back(a[id].first.first);       else if (a[id].first.second != cur1 && a[id].first.second != cur2)         ans.push_back(a[id].first.second);       else         ans.push_back(a[id].second);       cur1 = ans[ans.size() - 2];       cur2 = ans.back();     }     vector<long long> ans1;     id = g[f][0];     cur1 = dc;     cur2 = cd;     ans1.push_back(f);     ans1.push_back(cur1);     ans1.push_back(cur2);     set<long long> xx;     xx.insert(id);     while (ans1.size() < n) {       for (auto &i : g[cur1])         if (xx.find(i) == xx.end()) id = i;       xx.insert(id);       if (a[id].first.first != cur1 && a[id].first.first != cur2)         ans1.push_back(a[id].first.first);       else if (a[id].first.second != cur1 && a[id].first.second != cur2)         ans1.push_back(a[id].first.second);       else         ans1.push_back(a[id].second);       cur1 = ans1[ans1.size() - 2];       cur2 = ans1.back();     }     set<long long> s1, s2;     for (auto &i : ans) s1.insert(i);     for (auto &i : ans1) s2.insert(i);     if (s1.size() == n)       for (auto &i : ans) cout << i <<    ;     else       for (long long i = 0; i < n; i++) cout << ans1[i] <<    ;   }


#include <bits/stdc++.h>   using namespace std;   const int LN = 18, maxn = (1 << LN);   const long long mod = (long long)(998244353);   int fact[maxn], inv_fact[maxn], poww_r[maxn], poww_inv_r[maxn];   int mul(long long a, long long b) {     long long ret = (a * b) % mod;     return (int)(ret);   }   int add(int a, int b) {     int ret = a + b;     if (ret >= mod) {       ret -= mod;     }     return ret;   }   int poww(int a, int b) {     int x = 1, y = a;     while (b > 0) {       if (b % 2 == 1) {         x = mul(x, y);       }       y = mul(y, y);       b /= 2;     }     return x;   }   void build() {     fact[0] = 1;     for (int i = 1; i < maxn; i++) {       fact[i] = mul(fact[i - 1], i);     }     inv_fact[maxn - 1] = poww(fact[maxn - 1], mod - 2);     for (int i = maxn - 2; i >= 0; i--) {       inv_fact[i] = mul(inv_fact[i + 1], i + 1);     }   }   int C(int n, int r) {     int num = fact[n], den = mul(inv_fact[r], inv_fact[n - r]);     return mul(num, den);   }   vector<int> upper;   const int r = 3, inv_r = poww(r, mod - 2);   void pre() {     int zz = poww(r, (mod - 1) >> LN), curr = 1;     for (int i = 0; i < maxn; i++) {       poww_r[i] = curr;       curr = mul(curr, zz);     }     for (int i = 0; i < maxn; i++) {       poww_inv_r[i] = (i == 0) ? 1 : poww_r[maxn - i];     }     fact[0] = 1;     for (int i = 1; i < maxn; i++) {       fact[i] = mul(fact[i - 1], i);     }     inv_fact[maxn - 1] = poww(fact[maxn - 1], mod - 2);     for (int i = maxn - 2; i >= 0; i--) {       inv_fact[i] = mul(inv_fact[i + 1], i + 1);     }   }   void fft(vector<int> &a, bool invert) {     int n = a.size();     for (int i = 1, j = 0; i < n; i++) {       int bit = n >> 1;       for (; j >= bit; bit >>= 1) {         j -= bit;       }       j += bit;       if (i < j) {         swap(a[i], a[j]);       }     }     for (int len = 2; len <= n; len <<= 1) {       int additive = maxn / len;       for (int i = 0; i < n; i += len) {         int now = 0;         for (int j = 0; j < len / 2; j++) {           int u = a[i + j],               v = mul(a[i + j + len / 2], invert ? poww_inv_r[now] : poww_r[now]);           a[i + j] = add(u, v);           a[i + j + len / 2] = add(u, mod - v);           now += additive;         }       }     }   }   vector<int> mult(vector<int> &a, vector<int> &b) {     int sz = 1;     while (sz < max(a.size(), b.size())) {       sz *= 2;     }     sz *= 2;     vector<int> fa(sz), fb(sz);     for (int i = 0; i < a.size(); i++) {       fa[i] = a[i];     }     for (int i = 0; i < b.size(); i++) {       fb[i] = b[i];     }     fft(fa, false);     fft(fb, false);     for (int i = 0; i < sz; i++) {       fa[i] = mul(fa[i], fb[i]);     }     fft(fa, true);     int inv_sz = poww(sz, mod - 2);     for (int i = 0; i < sz; i++) {       fa[i] = mul(fa[i], inv_sz);     }     while (fa.back() == 0) {       fa.pop_back();     }     return fa;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     build();     pre();     int n, m, k;     cin >> n >> m >> k;     upper.resize(k + 1);     upper[0] = 1;     int last_used = n;     for (int i = 1; i <= k; i++) {       upper[i] = mul(upper[i - 1], last_used);       last_used--;     }     vector<int> p1(k + 1), p2(k + 1);     int curr = 1;     for (int i = 0; i <= k; i++) {       p1[i] = mul(poww(i, k), inv_fact[i]);       p2[i] = mul(curr, inv_fact[i]);       curr = mul(curr, mod - 1);     }     vector<int> res = mult(p1, p2);     int ans = 0, inv = poww(m, mod - 2), curr_inv = inv;     for (int i = 1; i <= min(n, k); i++) {       int curr = mul(upper[i], curr_inv);       curr = mul(curr, res[i]);       ans = add(ans, curr);       curr_inv = mul(curr_inv, inv);     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   char s[20005];   struct SuffixArray {     int wa[20005], wb[20005], cnt[20005], wv[20005];     int rk[20005], height[20005];     int sa[20005];     bool cmp(int r[], int a, int b, int l) {       return r[a] == r[b] && r[a + l] == r[b + l];     }     void calcSA(char r[], int n, int m) {       int i, j, p, *x = wa, *y = wb;       for (i = 0; i < m; ++i) cnt[i] = 0;       for (i = 0; i < n; ++i) cnt[x[i] = r[i]]++;       for (i = 1; i < m; ++i) cnt[i] += cnt[i - 1];       for (i = n - 1; i >= 0; --i) sa[--cnt[x[i]]] = i;       for (j = 1, p = 1; p < n; j *= 2, m = p) {         for (p = 0, i = n - j; i < n; ++i) y[p++] = i;         for (i = 0; i < n; ++i)           if (sa[i] >= j) y[p++] = sa[i] - j;         for (i = 0; i < n; ++i) wv[i] = x[y[i]];         for (i = 0; i < m; ++i) cnt[i] = 0;         for (i = 0; i < n; ++i) cnt[wv[i]]++;         for (i = 1; i < m; ++i) cnt[i] += cnt[i - 1];         for (i = n - 1; i >= 0; --i) sa[--cnt[wv[i]]] = y[i];         for (swap(x, y), p = 1, x[sa[0]] = 0, i = 1; i < n; ++i)           x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;       }     }     void calcHeight(char r[], int n) {       int i, j, k = 0;       for (i = 0; i <= n; ++i) rk[sa[i]] = i;       for (i = 0; i < n; height[rk[i++]] = k)         for (k ? k-- : 0, j = sa[rk[i] - 1]; r[i + k] == r[j + k]; k++)           ;     }     bool solve(int k, int n, int div) {       int ca = 0, cb = 0;       for (int i = 1; i <= n; i++) {         if (height[i] < k) {           if (ca == 1 && cb == 1) return true;           ca = 0;           cb = 0;           if (sa[i] < div)             ca++;           else if (sa[i] > div)             cb++;           continue;         }         if (sa[i] < div)           ca++;         else if (sa[i] > div)           cb++;       }       return ca == 1 && cb == 1;     }   } suf;   char a[20005], b[20005];   int main() {     scanf( %s %s , a, b);     int n = strlen(a), m = strlen(b);     strcpy(s, a);     s[n] =  # ;     strcpy(s + n + 1, b);     int tot = n + m + 1;     suf.calcSA(s, tot + 1, 128);     suf.calcHeight(s, tot);     int ret = -1;     for (int i = 1; i <= n; i++) {       if (suf.solve(i, tot, n)) {         ret = i;         break;       }     }     printf( %d n , ret);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 100005;   const int inf = 1 << 30;   const long long INF = 1ll << 61;   const double oo = 1 << 30;   const long long zqc = 1e9 + 7;   int n, k;   int t, x;   vector<int> a;   int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }   int main() {     scanf( %d%d , &n, &k);     t = n / 2;     --t;     x = k - t;     if (k == 0 && n == 1) return 0 * printf( 1 );     if (x <= 0) return 0 * printf( -1 );     if (n == 1) return 0 * printf( -1 );     a.push_back(x);     a.push_back(2 * x);     for (int i = 1; i <= 1e9; ++i) {       if (a.size() == n) break;       if (x == i || 2 * x == i) continue;       if (a.size() % 2 == 1 && gcd(a[a.size() - 1], i) != 1) continue;       a.push_back(i);     }     for (auto p : a) printf( %d  , p);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, d, cot = 0;     cin >> n >> m >> d;     int arr[10004] = {0};     int rn = n * m;     for (int j = 1; j <= rn; j++) cin >> arr[j];     sort(arr, arr + rn);     int ind = arr[rn / 2];     if (n * m == 1) {       cout << 0;       return 0;     }     for (int i = 1; i <= rn; i++) {       if (abs(ind - arr[i]) % d != 0) {         cout << -1;         return 0;       } else {         cot += abs(ind - arr[i]) / d;       }     }     cout << cot << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 2e3;   vector<int> e[MAX_N], d;   vector<char> flag;   void dfs(int v) {     if (d[v] != -1) return;     d[v] = 1;     for (int to : e[v]) {       dfs(to);       d[v] = max(d[v], d[to] + 1);     }   }   int main() {     int n;     scanf( %d , &n);     d.assign(n, -1);     for (int i = 0; i < n; i++) {       int x;       scanf( %d , &x);       if (x != -1) e[x - 1].push_back(i);     }     for (int i = 0; i < n; i++)       if (d[i] == -1) dfs(i);     int ans = 1;     for (int i : d) ans = max(ans, i);     printf( %d , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int a[100], n, temp, m, ans;     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     for (int i = 0; i < (n - 1); i++) {       for (int j = 0; j < n - i - 1; j++) {         if (a[j] < a[j + 1]) {           temp = a[j];           a[j] = a[j + 1];           a[j + 1] = temp;         }       }     }     for (int i = 0; i < n; i++) {       if (m % a[i] == 0) {         ans = max(0, m / a[i]);         cout << ans << endl;         return 0;       }     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n;   long long arr[5005], parent[5005], ans[5005];   long long dist[5005][5005];   vector<int> v[5005], order;   priority_queue<pair<int, int>, vector<pair<int, int> >,                  greater<pair<int, int> > >       pq[5005];   void dfs(int cur, int prev) {     parent[cur] = prev;     order.push_back(cur);     for (int i = 0; i < (int)v[cur].size(); i++) {       int node = v[cur][i];       if (node == prev) continue;       dfs(node, cur);     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cin >> n;     for (int i = 1; i <= n; i++) cin >> arr[i];     for (int i = 0; i < n - 1; i++) {       int u, v1, cap;       cin >> u >> v1 >> cap;       v[u].push_back(v1);       v[v1].push_back(u);       dist[u][v1] = cap;       dist[v1][u] = cap;     }     dfs(1, 0);     ans[1] = 0;     for (int i = 1; i <= n; i++) pq[i].push(make_pair(arr[i], i));     int cnt = 1;     while (true) {       if (pq[1].size() == n) break;       for (int i = 1; i < order.size(); i++) {         int node = order[i];         int cap = dist[node][parent[node]];         while (cap > 0 && pq[node].size()) {           pair<int, int> temp = pq[node].top();           int connode = temp.second;           pq[node].pop();           pq[parent[node]].push(temp);           ans[connode] = cnt;           cap--;         }       }       cnt++;     }     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       int x, y;       cin >> x >> y;       if ((x == 1 || x == 2 && 3 < y || x == 3 && x < y) && x != y) {         cout <<  NO  << endl;       } else         cout <<  YES  << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int arr[n];     int x = 0;     string str[n];     for (int i = 0; i < n; i++) {       cin >> str[i];       if (str[i][1] ==  + ) {         x++;       } else {         x--;       }     }     cout << x;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long b, d;   string a, c;   map<long long, long long> need[150];   int main() {     cin >> b >> d;     cin >> a >> c;     string h =   ;     long long ans = 0;     long long sizea = a.size();     long long sizec = c.size();     for (int i = 0; i < sizec; i++) {       int s = 0;       for (int j = 0; j < sizea; j++)         if (c[i] == a[j]) s = 1;       if (s == 0) {         cout << 0 << endl;         return 0;       }     }     for (long long i = 0; i < sizea; i++) {       int now = 0;       for (long long j = i; now < sizec; j++)         if (a[j % sizea] == c[now]) {           now++;           need[i][1] = j - i + 1;         }     }     for (long long i = 2; i <= 2ll * b * sizea; i *= 2ll)       for (long long j = 0; j < sizea; j++)         need[j][i] =             need[j][i / 2ll] + need[(j + need[j][i / 2ll]) % sizea][i / 2ll];     long long now = 0;     long long tot = b * sizea;     long long time = 0;     while (tot >= need[now][1])       for (long long i = 1;; i *= 2ll)         if (need[now][i] > tot) {           i /= 2ll;           tot -= need[now][i];           now += need[now][i];           now %= sizea;           time += i;           break;         }     cout << time / d << endl;   }


#include <bits/stdc++.h>   using namespace std;   vector<int> adj[100100], ans;   int arr[100100], cnt[100100];   bool visited[100100];   int n, m;   void dfs(int v) {     for (vector<int>::iterator it = adj[v].begin(); it != adj[v].end(); it++) {       cnt[*it]++;       if (cnt[*it] == arr[*it]) {         cnt[*it]++;         ans.push_back(*it);         dfs(*it);       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n >> m;     int x, y;     for (int i = 1; i <= m; i++) {       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     for (int i = 1; i <= n; i++) cin >> arr[i];     for (int i = 1; i <= n; i++) {       if (cnt[i] == arr[i]) {         visited[i] = true;         cnt[i]++;         ans.push_back(i);         dfs(i);       }     }     cout << ans.size() <<   n ;     vector<int>::iterator it;     for (it = ans.begin(); it != ans.end(); it++) cout << *it <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   struct vect {    private:     T *memory = new T[0];     unsigned int length;     unsigned int sz;       public:     vect() {       length = 0;       sz = 0;     }     inline void incsize(const int len) {       T *temp = new T[len];       length = len;       for (int i = 0; i < sz; ++i) temp[i] = memory[i];       swap(temp, memory);     }     inline void resize(const unsigned int v) {       sz = v;       if (v > length) incsize((1 << v) + 1);     }     inline void push_back(const T a) {       const unsigned int len = length;       if (length == sz) incsize((length << 1) + 1);       memory[sz++] = a;     }     T &operator[](const unsigned int i) { return memory[i]; }     unsigned int size() { return sz; }   };   map<string, set<string> > v;   map<set<string>, vect<string> > s1;   int main() {     ::ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       string s;       cin >> s;       s = s.substr(7);       string t;       for (int i = 0; i < s.length(); ++i)         if (s[i] ==  / ) t = s.substr(i), s = s.substr(0, i);       v[s].insert(t);     }     for (auto x : v) s1[x.second].push_back(x.first);     int cnt = 0;     for (auto x : s1)       if (x.second.size() > 1) ++cnt;     cout << cnt << endl;     for (auto x : s1)       if (x.second.size() > 1) {         for (int i = 0; i < x.second.size(); ++i)           cout <<  http://  << x.second[i] <<    ;         cout << endl;       }     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   const int RLEN = 1 << 20 | 1;   inline char gc() {     static char ibuf[RLEN], *ib, *ob;     (ob == ib) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));     return (ob == ib) ? EOF : *ib++;   }   inline int read() {     char ch = getchar();     int res = 0, f = 1;     while (!isdigit(ch)) f ^= ch ==  - , ch = getchar();     while (isdigit(ch)) res = (res + (res << 2) << 1) + (ch ^ 48), ch = getchar();     return f ? res : -res;   }   inline void chemx(int &a, int b) { a < b ? a = b : 0; }   inline void chemn(int &a, int b) { a > b ? a = b : 0; }   struct node {     int s, m, r, t;     node(int _s = 0, int _m = 0, int _r = 0, int _t = 0)         : s(_s), m(_m), r(_r), t(_t) {}     friend inline bool operator<(const node &a, const node &b) {       return a.t < b.t;     }     friend inline bool operator==(const node &a, const node &b) {       return a.t == b.t;     }     friend inline bool operator>(const node &a, const node &b) {       return a.t > b.t;     }     friend inline bool operator<=(const node &a, const node &b) {       return a.t <= b.t;     }     friend inline bool operator>=(const node &a, const node &b) {       return a.t >= b.t;     }     friend inline bool operator!=(const node &a, const node &b) {       return a.t != b.t;     }   };   const int N = 100005, inf = 2e9;   node a[N];   int n, q;   struct Tr {     vector<node> v;     vector<long long> sl, sr;     int last;     inline void init(int l, int r) {       int len = r - l + 1;       for (int i = l; i <= r; i++) v.push_back(a[i]);       sort(v.begin(), v.end());       sl.resize(len), sr.resize(len);       sl[0] = v[0].m, sr[len - 1] = v[len - 1].r;       for (int i = 1; i < len; i++) sl[i] = sl[i - 1] + v[i].m;       for (int i = len - 2; ~i; i--) sr[i] = sr[i + 1] + v[i].r;     }     inline long long query(int t) {       int tim = t - last;       int pos =           lower_bound(v.begin(), v.end(), node(0, 0, 0, tim)) - v.begin() - 1;       long long res = 0;       if (~pos) res += sl[pos];       if (pos + 1 < (int)v.size()) res += sr[pos + 1] * tim;       last = t;       return res;     }   };   namespace Seg {   Tr tr[N << 2];   void build(int u, int l, int r) {     tr[u].last = -2, tr[u].init(l, r);     if (l == r) return;     build((u << 1), l, ((l + r) >> 1)),         build(((u << 1) | 1), ((l + r) >> 1) + 1, r);   }   inline void pushup(int u) {     if (tr[(u << 1)].last == tr[((u << 1) | 1)].last)       if (tr[(u << 1)].last >= 0) {         tr[u].last = tr[(u << 1)].last;         return;       } else if (tr[(u << 1)].last == -2) {         tr[u].last = -2;         return;       }     tr[u].last = -1;   }   inline void pushdown(int u) {     if (tr[u].last >= 0) tr[(u << 1)].last = tr[((u << 1) | 1)].last = tr[u].last;   }   long long query(int u, int l, int r, int st, int des, int k) {     if (l == r && tr[u].last == -2) {       tr[u].last = k;       return min(1ll * a[l].m, 1ll * a[l].s + 1ll * a[l].r * k);     }     if (st <= l && r <= des && tr[u].last >= 0) return tr[u].query(k);     long long res = 0;     pushdown(u);     if (st <= ((l + r) >> 1))       res += query((u << 1), l, ((l + r) >> 1), st, des, k);     if (((l + r) >> 1) < des)       res += query(((u << 1) | 1), ((l + r) >> 1) + 1, r, st, des, k);     pushup(u);     return res;   }   }  // namespace Seg   int main() {     n = read();     for (int i = 1; i <= n; i++) {       a[i].s = read(), a[i].m = read(), a[i].r = read(),       a[i].t = a[i].r == 0 ? inf : a[i].m / a[i].r;     }     Seg::build(1, 1, n);     q = read();     while (q--) {       int t = read(), l = read(), r = read();       cout << Seg::query(1, 1, n, l, r, t) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int d1, d2, d3;     cin >> d1 >> d2 >> d3;     cout << min(min(d1 + d2 + d3, 2 * (d1 + d2)),                 min(2 * (d1 + d3), 2 * (d2 + d3)));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   void solve();   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }   const int inf = 2e9;   const long long linf = 2e18;   const long long mod = 998244353;   const int N = 2e5 + 100;   vector<int> g[N];   struct Elem {     int value, node, pos;   };   Elem merge(Elem a, Elem b) {     if (a.value < b.value)       return b;     else       return a;   }   struct T {     vector<Elem> t;     vector<int> u;     T(const vector<Elem> &a) {       t.assign(4 * a.size(), {0, 0});       u.assign(4 * a.size(), 0);       build(a, 0, 0, a.size());     }     void build(const vector<Elem> &a, int v, int l, int r) {       if (r - l == 1) {         t[v] = a[l];         return;       }       build(a, v * 2 + 1, l, (l + r) >> 1);       build(a, v * 2 + 2, (l + r) >> 1, r);       t[v] = merge(t[v * 2 + 1], t[v * 2 + 2]);     }     void push(int v, int l, int r) {       if (r - l == 1) {         t[v].value += u[v];         u[v] = 0;         return;       }       t[v].value += u[v];       u[v * 2 + 1] += u[v];       u[v * 2 + 2] += u[v];       u[v] = 0;     }     Elem get(int v, int l, int r, int vl, int vr) {       push(v, l, r);       if (vl >= r || vr <= l) return {-inf, -1};       if (vl <= l && r <= vr) return t[v];       if (r - l == 1) {         return t[v];       }       return merge(get(v * 2 + 1, l, (l + r) >> 1, vl, vr),                    get((v * 2 + 2), (l + r) >> 1, r, vl, vr));     }     void upd(int v, int l, int r, int vl, int vr, int d) {       push(v, l, r);       if (vl >= r || vr <= l) return;       if (vl <= l && r <= vr) {         u[v] += d;         push(v, l, r);         return;       }       int m = (l + r) >> 1;       upd(v * 2 + 1, l, m, vl, vr, d);       upd(v * 2 + 2, m, r, vl, vr, d);       t[v] = merge(t[v * 2 + 1], t[v * 2 + 2]);     }   };   vector<Elem> leaves;   pair<int, int> segments[N];   int par[N];   pair<int, int> dfs(int v, int depth = 1) {     if (g[v].size() == 1 && par[v] != -1) {       int pos = leaves.size();       leaves.push_back({depth, v, pos});       return {leaves.size() - 1, leaves.size() - 1};     }     for (auto u : g[v]) {       if (par[v] == u) continue;       par[u] = v;       auto [l, r] = dfs(u, depth + 1);       segments[v].first = min(segments[v].first, l);       segments[v].second = max(segments[v].second, r);     }     return segments[v];   }   int marked[N];   void solve() {     int n, k;     cin >> n >> k;     fill(par, par + n, -1);     for (int i = 0; i < n; i++) segments[i] = {inf, -inf};     for (int i = 0; i < n - 1; i++) {       int t1, t2;       cin >> t1 >> t2;       t1--;       t2--;       g[t1].push_back(t2);       g[t2].push_back(t1);     }     dfs(0);     T t(leaves);     int unmarked = n;     long long best = -1ll * (n / 2) * (n - n / 2);     for (int i = 1; i <= k; i++) {       if (i > leaves.size()) {         long long rmax = max((long long)leaves.size(), (long long)min(i, n / 2));         best = max(best, rmax * (n - rmax));         continue;       }       Elem e = t.get(0, 0, leaves.size(), 0, leaves.size());       t.upd(0, 0, leaves.size(), e.pos, e.pos + 1, -inf);       int v = e.node;       marked[v] = true;       unmarked--;       v = par[v];       while (!marked[v]) {         marked[v] = true;         unmarked--;         t.upd(0, 0, leaves.size(), segments[v].first, segments[v].second + 1, -1);         if (par[v] == -1) break;         v = par[v];       }       long long bmax = min(n / 2, unmarked);       best = max(best, 1ll * i * (n - i) - bmax * (n - bmax));     }     cout << best <<   n ;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     int ma = INT_MIN;     string ans =  nah ;     while (n--) {       string s;       int s1, s2, a, b, c, d, e;       cin >> s;       cin >> s1 >> s2 >> a >> b >> c >> d >> e;       int val = 0;       val += 100 * s1 + (-50) * s2 + a + b + c + d + e;       if (val > ma) {         ma = val;         ans = s;       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long int k, n;     cin >> n >> k;     long long int half, d, c;     half = (n / 2);     d = (half / (k + 1));     c = (k * d);     cout << d <<     << c <<     << (n - c - d);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n, f, i, j;     cin >> n >> f;     long long ans(0), k, l, mini;     vector<pair<long long, long long> > v, v1;     for (i = 0; i < n; i++) {       cin >> k >> l;       v.push_back(make_pair(k, l));     }     for (i = 0; i < n; i++) {       v1.push_back(make_pair(           min(2 * v[i].first, v[i].second) - min(v[i].first, v[i].second), i));     }     sort(v1.begin(), v1.end(), greater<pair<long long, long long> >());     for (i = 0; i < f; i++) {       mini = min(2 * v[v1[i].second].first, v[v1[i].second].second);       ans += mini;       v[v1[i].second].first = -1;     }     for (i = 0; i < n; i++) {       if (v[i].first != -1) ans += min(v[i].first, v[i].second);     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   int main() {     long long a, m, c, i;     scanf( %I64d %I64d , &a, &m);     for (i = 0; i <= 700; i++) {       c = a % m;       if (c == 0) break;       a = a + c;     }     if (c == 0) {       printf( Yes );     } else       printf( No );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 2e5;   const int INF = 0x3f3f3f3f;   const long long LIM = 4e14;   const int mod = 1e9 + 7;   int n, m, ans[N + 10];   long long l[N + 10], r[N + 10];   struct Node {     int idx;     long long l, r;     bool friend operator<(Node c, Node d) { return !(c.r < d.r); }   } p[N + 10];   bool cmp1(Node c, Node d) {     if (c.l != d.l) return c.l < d.l;     return c.r < d.r;   }   struct Point {     int idx;     long long len;   } v[N + 10];   bool cmp2(Point c, Point d) {     if (c.len != d.len) return c.len < d.len;     return c.idx < d.idx;   }   priority_queue<Node> que;   int main() {     ios::sync_with_stdio(0);     cin.tie(0), cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> l[i] >> r[i];       if (i == 1) continue;       p[i - 1] = {i - 1, l[i] - r[i - 1], r[i] - l[i - 1]};     }     sort(p + 1, p + n, cmp1);     for (int i = 1; i <= m; i++) {       cin >> v[i].len;       v[i].idx = i;     }     sort(v + 1, v + 1 + m, cmp2);     int pos = 1;     for (int i = 1; i <= m; i++) {       while (pos < n && p[pos].l <= v[i].len) que.push(p[pos++]);       if (!que.empty()) {         Node c = que.top();         que.pop();         if (c.r < v[i].len) {           cout <<  No ;           return 0;         }         ans[c.idx] = v[i].idx;       }     }     int flag = 1;     for (int i = 1; i < n; i++)       if (!ans[i]) flag = 0;     if (!flag) {       cout <<  No ;       return 0;     }     cout <<  Yes n ;     for (int i = 1; i < n; i++) cout << ans[i] <<    ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long n, m, q, k, v[300050], ans, d;   pair<long long, long long> w[300050];   string second;   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> q;     while (q--) {       cin >> d >> m;       if (d <= 3) {         if (d == 1)           cout << (1 % m) <<   n ;         else if (d == 2)           cout << 3 % m <<   n ;         else           cout << 5 % m <<   n ;       } else {         long long curr = 2, j = 1;         long long sum = 3, sum2 = 5;         while (1) {           long long ant = sum;           long long T = ((1LL << j) + 1) % m;           curr = (curr * T) % m;           if (sum <= d and d <= sum + (1LL << (j + 1))) {             long long falta = (d - ant) % m;             if (falta < 0) falta += m;             cout << (sum2 + falta * curr) % m <<   n ;             break;           }           sum += (1LL << (j + 1));           long long tam = (1LL << (j + 1)) % m;           sum2 = (sum2 + tam * curr) % m;           j++;         }       }     }   }


#include <bits/stdc++.h>   using namespace std;   int k, n;   bool contain_same;   string s[5005], t[5005];   bool check(int x, int y) {     swap(s[0][x], s[0][y]);     for (int i = 1; i < k; ++i) {       int dif = 0;       for (int j = 0; j < n; ++j) {         if (s[0][j] != s[i][j]) ++dif;       }       if (dif == 2 || (dif == 0 && contain_same)) continue;       swap(s[0][x], s[0][y]);       return 0;     }     return 1;   }   signed main() {     scanf( %d%d , &k, &n);     for (int i = 0; i < k; ++i) {       cin >> s[i];       t[i] = s[i];       sort(t[i].begin(), t[i].end());     }     for (int i = 1; i < k; ++i)       if (t[i] != t[i - 1]) return puts( -1 ), 0;     int alp[26] = {};     for (auto ch : s[0]) alp[ch -  a ]++;     for (int i = 0; i < 26; ++i)       if (alp[i] > 1) contain_same = 1;     int ma_val = 0, ma_id = 0;     for (int i = 1; i < k; ++i) {       int cnt = 0;       for (int j = 0; j < n; ++j) {         if (s[0][j] != s[i][j]) ++cnt;       }       if (cnt > 4)         return puts( -1 ), 0;       else if (ma_val < cnt) {         ma_val = cnt;         ma_id = i;       }     }     if (ma_val == 0) {       swap(s[0][0], s[0][1]);       cout << s[0];       return 0;     }     if (ma_val == 2) {       if (contain_same) {         cout << s[0];         return 0;       }       return puts( -1 ), 0;     }     vector<int> pos;     for (int i = 0; i < n; ++i)       if (s[0][i] != s[ma_id][i]) pos.push_back(i);     for (auto x : pos) {       for (int y = 0; y < n; ++y) {         if (x == y) continue;         if (check(x, y)) {           cout << s[0];           return 0;         }       }     }     puts( -1 );     return 0;   }


#include <bits/stdc++.h>   using namespace std;   map<long long, bool> mp;   long long b, q, l, n, x, ans;   signed main() {     scanf( %lld%lld%lld%lld , &b, &q, &l, &n);     if (abs(b) > l) {       printf( 0 );       return 0;     }     if (l < 0) l = -l;     for (long long i = 1; i <= n; i++) {       scanf( %lld , &x);       mp[x] = 1;     }     if (b == 0) {       if (mp[0])         printf( 0 );       else         printf( inf );       return 0;     }     if (q == 1) {       if (mp[b])         puts( 0 );       else         printf( inf );       return 0;     }     if (q == -1) {       if (mp[-b] && mp[b])         printf( 0 );       else         printf( inf );       return 0;     }     if (q == 0) {       if (mp[0])         printf( %d , mp[b] ? 0 : 1);       else         printf( inf );       return 0;     }     while (abs(b) <= l) {       if (!mp[b]) ans++;       b *= q;     }     printf( %lld , ans);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long int t, n, m, k;   string s;   long long int dp[30][30004];   long long int frq[30];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     long long int i, j, x;     cin >> t;     while (t--) {       cin >> n >> m >> k;       cin >> s;       long long int ans = 1e10;       long long int tot = s.size();       memset(frq, 0, sizeof frq);       for (i = 0; i < s.size(); i++) {         frq[s[i] -  A  + 1]++;       }       for (i = 1; i <= 26; i++) {         for (j = 0; j <= 27; j++) {           for (k = 0; k <= n; k++) {             dp[j][k] = 0;             if (k == 0) dp[j][k] = 1;           }         }         for (j = 1; j <= 26; j++) {           for (k = 1; k <= n; k++) {             dp[j][k] = dp[j - 1][k];             if (i != j && k - frq[j] >= 0 && dp[j - 1][k - frq[j]] == 1) {               dp[j][k] = 1;             }           }         }         long long int rem = tot - frq[i];         for (k = 0; k <= n; k++) {           if (dp[26][k]) {             long long int cn = n - k;             long long int cm = m - rem + k;             if (cm <= 0) {               if (frq[i] >= cn) ans = min(ans, 0LL);             } else {               if (cn == 0) {                 if (frq[i] >= cm) ans = min(ans, 0LL);               } else {                 if (frq[i] >= cn + cm) {                   ans = min(ans, cn * cm);                 }               }             }           }         }       }       cout << ans <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int k, m, n1, n2;     cin >> n1 >> n2 >> k >> m;     vector<int> a(n1);     for (int i = 0; i < a.size(); ++i) cin >> a[i];     vector<int> b(n2);     for (int i = 0; i < b.size(); ++i) cin >> b[i];     if (a[k - 1] < b[n2 - m])       cout <<  YES n ;     else       cout <<  NO n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   string input() {     int n;     string sequence;     cin >> n >> sequence;     return sequence;   }   int getMaxBalance(const string &sequence) {     int balance = 0;     int maxBalance = 0;     for (char bracket : sequence) {       balance += (bracket ==  [ ) - (bracket ==  ] );       if (balance > maxBalance) {         maxBalance = balance;       }     }     return maxBalance;   }   int getOpenClosePairs(const string &sequence) {     int result = 0;     for (int i = 1; i < (int)sequence.size(); ++i) {       result += sequence[i - 1] ==  [  && sequence[i] ==  ] ;     }     return result;   }   vector<string> solve(const string &sequence) {     const int maxBalace = getMaxBalance(sequence);     const int length = (int)sequence.size() + 3 * getOpenClosePairs(sequence);     vector<string> result(2 * maxBalace + 1, string(length,    ));     int previousHeight = 2 * maxBalace + 1;     char previousBracket =  ] ;     int col = 0;     for (char bracket : sequence) {       int height;       if ( [  == previousBracket &&  [  == bracket) {         height = previousHeight - 2;       } else if ( [  == previousBracket &&  ]  == bracket ||                   ]  == previousBracket &&  [  == bracket) {         col += 3 * ( [  == previousBracket &&  ]  == bracket);         height = previousHeight;       } else if ( ]  == previousBracket &&  ]  == bracket) {         height = previousHeight + 2;       } else {         assert(false);       }       int middle = (int)result.size() / 2;       int topRow = middle - height / 2;       int bottomRow = middle + height / 2;       int minusCol = col + ( [  == bracket) - ( ]  == bracket);       result[topRow][col] = result[bottomRow][col] =  + ;       result[topRow][minusCol] = result[bottomRow][minusCol] =  - ;       for (int row = topRow + 1; row < bottomRow; ++row) {         result[row][col] =  | ;       }       ++col;       previousHeight = height;       previousBracket = bracket;     }     return result;   }   void output(const vector<string> &picture) {     for (const string &row : picture) {       cout << row << endl;     }   }   int main() {     const string sequece = input();     auto picture = solve(sequece);     output(picture);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   struct SegmentTree {     int l, r, val, lazy;   } t[800005];   int n, q, a[200005];   void build(int l, int r, int p) {     t[p].l = l;     t[p].r = r;     if (l == r) {       t[p].val = a[l];       return;     }     int mid = (l + r) >> 1;     build(l, mid, p << 1);     build(mid + 1, r, p << 1 | 1);   }   void spread(int p) {     if (t[p].lazy != 0) {       int la = t[p].lazy;       t[p << 1].lazy = max(t[p << 1].lazy, la);       t[p << 1 | 1].lazy = max(t[p << 1 | 1].lazy, la);       t[p].lazy = 0;     }   }   void change(int pos, int x, int p) {     int L = t[p].l, R = t[p].r;     if (L == R) {       t[p].lazy = 0;       t[p].val = x;       return;     }     int mid = (L + R) >> 1;     spread(p);     if (pos <= mid)       change(pos, x, p << 1);     else       change(pos, x, p << 1 | 1);   }   void write(int p) {     int L = t[p].l, R = t[p].r;     if (L == R) {       cout << max(t[p].val, t[p].lazy) <<    ;       return;     }     int mid = (L + R) >> 1;     spread(p);     write(p << 1);     write(p << 1 | 1);   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     build(1, n, 1);     cin >> q;     int C, pos, x;     for (int i = 1; i <= q; i++) {       cin >> C;       if (C == 1) {         cin >> pos >> x;         change(pos, x, 1);       } else {         cin >> x;         t[1].lazy = max(t[1].lazy, x);       }     }     write(1);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       string s;       cin >> s;       string ans = s;       int k_prime = 1;       for (int k = 0; k < n; k++) {         string t = s;         rotate(t.begin(), t.begin() + k, t.end());         if (k % 2 != n % 2) {           reverse(t.begin() + n - k, t.end());         }         if (t < ans) {           ans = t;           k_prime = k + 1;         }       }       cout << ans <<   n ;       cout << k_prime <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T a, T b) {     while (b) {       T c = a % b;       a = b;       b = c;     }     return a;   }   template <typename T>   T lcm(T a, T b) {     assert(a / gcd(a, b) <= numeric_limits<T>::max() / b);     return a * (b / gcd(a, b));   }   class out {    public:     template <typename T>     out &operator()(const T &t) {       cout << t <<    ;       return *this;     }     template <typename T1, typename T2>     out &operator()(const pair<T1, T2> &t) {       cout <<  (  << t.first <<  ,  << t.second <<  )  <<    ;       return *this;     }   };   template <typename iter>   void print(iter beg, iter end) {     for_each(beg, end, out());     cout << endl;   }   long n;   long h[100000];   long g[100000];   long r = 0;   void algo() {     for (long i = 1; i < n; ++i) {       g[i] = max(g[i - 1], h[i - 1]);     }     long t = h[n - 1];     for (long i = n - 1; i >= 0; --i) {       t = min(t, h[i]);       if (t >= g[i]) {         ++r;       }     }   }   int main() {     cin >> n;     for (long i = 0; i < n; ++i) {       cin >> h[i];     }     algo();     cout << r;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int n, p;   double l[100100], r[100100], s[100100];   long long ans;   double c;   int main() {     cin >> n >> p;     for (int i = 0; i < n; i++) {       cin >> l[i] >> r[i];       s[i] = 1 - (floor(r[i] / p) - ceil(l[i] / p) + 1) / (r[i] - l[i] + 1);     }     for (int i = 0; i < n; i++) {       c += (2000 * (1 - s[(i + 1) % n] * s[i]));       ans += floor(c);       c = c - floor(c);     }     if (c != 0) {       if (ans >= 1000000) {         cout << ans << endl;         return 0;       } else {         cout << ans / 10;         c += (ans % 10);         cout << c << endl;       }     } else {       cout << ans << endl;     }   }


#include <bits/stdc++.h>   using namespace std;   string a, b;   int main() {     int n;     while (cin >> n) {       cin >> a >> b;       long long ans = 0;       long long x = 0, y = 0, p = 0, q = 0;       for (int i = 0; i < n; i++)         if (b[i] ==  1 ) a[i] ==  0  ? x++ : y++;       for (int i = 0; i < n; i++)         if (b[i] ==  0 ) a[i] ==  0  ? p++ : q++;       for (int i = 0; i < n; i++)         if (b[i] ==  0 ) a[i] ==  0  ? ans += y : ans += x;       ans += (long long)p * q;       cout << ans << endl;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long a, b, c, n, m, k, x, y, z, cnt, ans, tt;   string s;   vector<long long> v, q, t;   int main() {     long long c0, c1, h;     cin >> tt;     while (tt--) {       cin >> n >> c0 >> c1 >> h >> s;       cnt = 0;       ans = 0;       for (long long i = 0; i < s.size(); i++) {         if (s[i] ==  1 )           cnt++;         else           ans++;       }       if (c0 == c1)         cout << c0 * n <<   n ;       else if (c0 + h < c1)         cout << c0 * n + (cnt * h) <<   n ;       else if (c1 + h < c0)         cout << (c1 * n) + (ans * h) <<   n ;       else         cout << (c1 * cnt) + (c0 * ans) <<   n ;     }   }


#include <bits/stdc++.h>   using namespace std;   const int FFF = 200005;   int a[FFF], b[FFF];   int qsearch(int x, int r) {     int l = 1, mid;     while (l <= r) {       mid = (l + r) >> 1;       if (b[mid] < x)         l = mid + 1;       else         r = mid - 1;     }     return r;   }   int main() {     int n, m;     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     sort(a, a + n);     scanf( %d , &m);     for (int i = 1; i <= m; i++) scanf( %d , &b[i]);     sort(b + 1, b + m + 1);     long long suma, sumb, cha;     suma = n * 2;     sumb = m * 2;     cha = suma - sumb;     for (int now = 0; now < n; now++) {       int t = qsearch(a[now], m);       long long tt1 = now * 2 + 3 * (n - now);       long long tt2 = t * 2 + (m - t) * 3;       long long tmp = tt1 - tt2;       if (tmp > cha) {         cha = tmp;         suma = tt1;         sumb = tt2;       } else if (tmp == cha && suma < tt1) {         suma = tt1;         sumb = tt2;       }     }     cout << suma <<  :  << sumb << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   long long ans;   int main() {     vector<int> a(3);     for (size_t i = 0; i < 3; i++) {       cin >> a[i];     }     sort(a.begin(), a.end());     cout << ((a[1] + a[0] > a[2]) ? 0 : (-a[1] - a[0] + a[2] + 1));   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 7;   struct note {     int x, id;   } q[maxn];   int st[maxn][20], st1[maxn][20], a[maxn], n, dp[maxn];   int head, tail;   void init(int n) {     for (int i = 1; i <= n; i++) st1[i][0] = st[i][0] = a[i];     for (int j = 1; j < 20; j++)       for (int i = 1; i <= n; i++)         if (i + (1 << (j - 1)) <= n)           st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]),           st1[i][j] = max(st1[i][j - 1], st1[i + (1 << (j - 1))][j - 1]);   }   int query(int l, int r) {     int len = log2(r - l + 1);     return max(st1[l][len], st1[r - (1 << len) + 1][len]) -            min(st[l][len], st[r - (1 << len) + 1][len]);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int s, len;     cin >> n >> s >> len;     for (int i = 1; i <= n; i++) cin >> a[i];     init(n);     int l, r, ans;     if (len > n || query(1, len) > s) return cout << -1 << endl, 0;     memset(dp, 0x3f3f3f3f, sizeof(dp));     dp[0] = 0;     q[tail].id = 0, q[tail].x = 0, tail++;     dp[len] = 1;     for (int i = len + 1; i <= n; i++) {       l = 1, r = i - len + 1, ans = -1;       while (l <= r) {         int mid = (l + r) >> 1;         if (query(mid, i) <= s)           r = mid - 1, ans = mid;         else           l = mid + 1;       }       while (head < tail && q[tail - 1].x >= dp[i - len]) tail--;       q[tail].x = dp[i - len], q[tail].id = i - len, tail++;       if (ans != -1) {         while (head < tail && q[head].id < ans - 1) head++;         if (head < tail) dp[i] = min(dp[i], q[head].x + 1);       }     }     if (dp[n] == 0x3f3f3f3f) dp[n] = -1;     cout << dp[n] << endl;   }


#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-8;   const int MAXN = 1e2 + 5;   const long long MOD = 1e9 + 7;   int a[1005][3000];   int main() {     int n, s, d;     int maxx = 0;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> s >> d;       for (int j = s; j <= 100000000; j += d) {         if (j > maxx) {           maxx = j;           break;         }       }     }     cout << maxx << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 210003;   double a[N], b[N];   int n;   double checkMa(double x) {     for (int i = 1; i <= n; i++) b[i] = a[i] - x;     double ans = 0, tpans = 0;     for (int i = 1; i <= n; i++) {       tpans += b[i];       if (tpans < 0) tpans = 0;       ans = max(ans, tpans);     }     for (int i = 1; i <= n; i++) b[i] = -b[i];     tpans = 0;     for (int i = 1; i <= n; i++) {       tpans += b[i];       if (tpans < 0) tpans = 0;       ans = max(ans, tpans);     }     return ans;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %lf , &a[i]);     }     double r = 10005, l = -10005;     int times = 100;     while (fabs(r - l) > 1e-13 && times) {       times--;       if (times <= 0) break;       double lm = (2.0 * l + r) / 3.0;       double rm = (2.0 * r + l) / 3.0;       if (checkMa(lm) < checkMa(rm)) {         r = rm;       } else         l = lm;     }     printf( %.10f n , checkMa(l));     return 0;   }


#include <bits/stdc++.h>   using namespace std;   template <class T>   bool remin(T &a, const T &b) {     if (a > b) {       a = b;       return true;     }     return false;   }   template <class T>   bool remax(T &a, const T &b) {     if (a < b) {       a = b;       return true;     }     return false;   }   int nxt() {     int first;     scanf( %  PRId32, &first);     return first;   }   long long nxtll() {     long long first;     scanf( %  PRId64, &first);     return first;   }   long long gcd(long long a, long long b) {     a = abs(a);     b = abs(b);     while (b) {       a %= b;       swap(a, b);     }     return a;   }   struct Q {     long long n, d;     Q(long long a = 0) : n(a), d(1) {}     Q(long long nn, long long dd) : n(nn), d(dd) { norm(); }     void norm() {       long long g = gcd(abs(n), abs(d));       n /= g;       d /= g;       if (d < 0) {         n *= -1;         d *= -1;       }     }     Q operator+(const Q &r) const { return Q(n * r.d + d * r.n, d * r.d); }     Q operator-(const Q &r) const { return Q(n * r.d - d * r.n, d * r.d); }     Q operator*(const Q &r) const { return Q(n * r.n, d * r.d); }     Q operator/(const Q &r) const { return Q(n * r.d, d * r.n); }     long long value(const Q &r) const { return n * r.d - d * r.n; }     double vald(const Q &r) const { return n * 1.0 * r.d - d * 1.0 * r.n; }     bool operator<(const Q &r) const {       double vv = vald(r);       if (vv < -1e10) {         return true;       }       if (vv > 1e10) {         return false;       }       return value(r) < 0;     }     bool operator>(const Q &r) const {       double vv = vald(r);       if (vv < -1e10) {         return false;       }       if (vv > 1e10) {         return true;       }       return value(r) > 0;     }     bool operator!=(const Q &r) const { return !(*this == r); }     bool operator>=(const Q &r) const { return *this > r || *this == r; }     bool operator<=(const Q &r) const { return *this < r || *this == r; }     bool operator==(const Q &r) const {       double vv = vald(r);       if (vv < -1e10) {         return false;       }       if (vv > 1e10) {         return false;       }       return value(r) == 0;     }     double getValue() const { return n / (double)d; }   };   ostream &operator<<(ostream &os, const Q &p) { return os << p.n <<  /  << p.d; }   struct pt {     double first, second;     pt() = default;     pt(long double first, long double second) : first(first), second(second) {}     inline pt operator-(const pt &r) const {       return pt(first - r.first, second - r.second);     }     inline pt operator+(const pt &r) const {       return pt(first + r.first, second + r.second);     }     inline pt operator*(const long double &r) const {       return pt(first * r, second * r);     }     inline pt operator-() const { return pt(-first, -second); }     inline long double sqlen() const {       return fabsl(first * first + second * second);     }   };   ostream &operator<<(ostream &os, const pt &p) {     return os <<  (  << p.first <<  ,  << p.second <<  ) ;   }   ostream &operator<<(ostream &os, const pair<long long, long long> &p) {     return os <<  (  << p.first <<  ,  << p.second <<  ) ;   }   pair<long long, long long> operator-(const pair<long long, long long> &l,                                        const pair<long long, long long> &r) {     return pair<long long, long long>(l.first - r.first, l.second - r.second);   }   pair<long long, long long> operator+(const pair<long long, long long> &l,                                        const pair<long long, long long> &r) {     return pair<long long, long long>(l.first + r.first, l.second + r.second);   }   inline double cross(const pt &l, const pt &r) {     return l.first * r.second - l.second * r.first;   }   inline double dot(const pt &l, const pt &r) {     return l.first * r.first + l.second * r.second;   }   inline long long cross(const pair<long long, long long> &l,                          const pair<long long, long long> &r) {     return l.first * r.second - l.second * r.first;   }   inline long long dot(const pair<long long, long long> &l,                        const pair<long long, long long> &r) {     return l.first * r.first + l.second * r.second;   }   long long pwmod(long long a, long long n, long long mod) {     long long ret = 1;     while (n) {       if (n & 1) ret = ret * a % mod;       a = a * a % mod;       n >>= 1;     }     return ret;   }   template <typename T>   inline T sqr(T first) {     return first * first;   }   inline bool is_prime(unsigned long long first) {     for (unsigned long long i = 2; i * i <= first; ++i) {       if (first % i == 0) {         return false;       }     }     return true;   }   bool is_sq(long long first) {     for (long long i = 1; i * i <= first; ++i) {       if (i * i == first) return true;     }     return false;   }   int n;   string solve(const multiset<char> &a, const multiset<char> &b, int d = 0,                const string &cur = string(n,  ? )) {     if (d == n) {       return cur;     }     if (d & 1) {       string res =   ;       set<char> bl(b.begin(), b.end());       for (char c : bl) {         multiset<char> bb = b;         bb.erase(bb.find(c));         for (int i = 0; i < n; ++i) {           if (cur[i] !=  ? ) continue;           string cc = cur;           cc[i] = c;           res = max(res, solve(a, bb, d + 1, cc));         }       }       return res;     } else {       string res = string(n,  z  + 1);       set<char> al(a.begin(), a.end());       for (char c : al) {         multiset<char> aa = a;         aa.erase(aa.find(c));         for (int i = 0; i < n; ++i) {           if (cur[i] !=  ? ) continue;           string cc = cur;           cc[i] = c;           res = min(res, solve(aa, b, d + 1, cc));         }       }       return res;     }   }   string solve(const string &a, const string &b) {     n = a.length();     return solve(multiset<char>(a.begin(), a.end()),                  multiset<char>(b.begin(), b.end()));   }   const int N = 300 * 1000 + 10;   char a[N];   char b[N];   string solve2(string a, string b) {     sort(a.begin(), a.end());     sort(b.begin(), b.end());     int n = a.length();     int l = 0, r = n - 1;     int L = 0, R = n;     string s(n,  . );     int move = 0;     while (L < R) {       if (move & 1) {         if (a[l] < b[r] || L + 1 == R) {           s[L] = b[r];           --r;           ++L;         } else {           if (!((R - L) & 1)) {             for (int i = L; i < R; i += 2) {               s[i] = a[l++];             }             for (int i = L + 1; i < R; i += 2) {               s[i] = b[r--];             }           } else {             for (int i = L + 1; i < R; i += 2) {               s[i] = a[l++];             }             for (int i = L; i < R; i += 2) {               s[i] = b[r--];             }           }           L = R;         }       } else {         if (a[l] < b[r] || L + 1 == R) {           s[L] = a[l];           ++l;           ++L;         } else {           if (!((R - L) & 1)) {             for (int i = L + 1; i < R; i += 2) {               s[i] = a[l++];             }             for (int i = L; i < R; i += 2) {               s[i] = b[r--];             }           } else {             for (int i = L; i < R; i += 2) {               s[i] = a[l++];             }             for (int i = L + 1; i < R; i += 2) {               s[i] = b[r--];             }           }           L = R;         }       }       ++move;     }     return s;   }   void solve() {     scanf( %s , a);     scanf( %s , b);     cout << solve2(a, b) <<   n ;   }   void prepare() {     return;     while (true) {       n = rand() % 5 + 1;       string a(n,  . );       string b(n,  . );       for (int i = 0; i < (int)(n); i++) {         a[i] = rand() % 5 +  a ;         b[i] = rand() % 5 +  a ;       }       if (solve(a, b) != solve2(a, b)) {         cout << a <<   n ;         cout << b <<   n ;         return;       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     prepare();     int t = 1;     for (int i = 0; i < (int)(t); i++) {       solve();     }     cerr <<  Time   << clock() / (double)CLOCKS_PER_SEC << endl;     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O2 )   using namespace std;   const int MAX = 2e6 + 5;   const long long MAX2 = 11;   const int MOD = 1000000000 + 7;   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;   int n, k, x, nw, sz, p[MAX], mx;   string s;   char y[MAX];   int par(int z) {     if (p[z] == z) return z;     return p[z] = par(p[z]);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     for (int i = 1; i <= 2000001; i++) p[i] = i;     cin >> n;     while (n--) {       cin >> s >> k;       sz = s.size() - 1;       while (k--) {         cin >> x;         nw = x;         while (nw <= x + sz) {           y[nw] = s[nw - x];           nw = p[nw] = par(nw + 1);         }         mx = max(mx, x + sz);       }     }     for (int i = 1; i <= mx; i++)       if (y[i])         cout << (char)y[i];       else         cout <<  a ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0;     char ch = getchar();     bool f = 0;     for (; !isdigit(ch); ch = getchar())       if (ch ==  - ) f = 1;     for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ;     return f ? -x : x;   }   void write(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x >= 10) write(x / 10);     putchar(x % 10 +  0 );   }   void writeln(long long x) {     write(x);     putchar(  n );   }   void writep(long long x) {     write(x);     putchar(   );   }   long long const N = 1e6 + 3;   long long n, a[N];   signed main() {     n = read();     long long ans = 0;     for (long long i = 1; i <= n; i++) a[i] = read(), ans += a[i];     if (n == 1) {       writeln(a[1]);       return 0;     }     sort(a + 1, a + n + 1);     if (a[1] <= 0) {       for (long long i = 1; i < n && a[i] <= 0; i++) ans -= 2 * a[i];     } else {       ans -= 2 * a[1];     }     writeln(ans);   }


#include <bits/stdc++.h>   using namespace std;   long long a[2005], b[2005], pos;   int n, m;   inline long long mabs(long long x) { return x > 0 ? x : -x; }   inline bool judge(long long x) {     int p = 1;     for (int i = 1; i <= n; i++) {       while (p <= m && mabs(b[p] - a[i]) + mabs(b[p] - pos) > x) p++;       if (p > m) return false;       p++;     }     return true;   }   int main() {     scanf( %d%d%I64d , &n, &m, &pos);     for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]);     for (int i = 1; i <= m; i++) scanf( %I64d , &b[i]);     sort(a + 1, a + n + 1);     sort(b + 1, b + m + 1);     long long l = 0, r = 3e9, ans = 3e9;     while (l <= r) {       long long mid = (l + r) >> 1;       if (judge(mid)) {         ans = min(ans, mid);         r = mid - 1;       } else         l = mid + 1;     }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   int n, cnt, sum, len, tlen, tcnt;   int a[26], ans[N], tans[N], id[26], pos[N], tpos[N];   void work1() {     printf( 0 n );     for (int i = 0; i < n; i++)       for (int j = 1; j <= a[i]; j++) printf( %c ,  a  + i);   }   bool cmp(int x, int y) { return a[x] > a[y]; }   int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }   int t, d;   void work(int x, int y) {     int j = 0;     tcnt = tlen = 0;     for (int i = x; i <= cnt; i += x) {       while (j + 1 <= pos[i]) {         j++;         tans[++tlen] = ans[j];       }       if (i % (2 * x) == 0) {         for (int l = 1; l <= y; l++) tans[++tlen] = t;         tpos[++tcnt] = tlen;         for (int l = 1; l <= y; l++) tans[++tlen] = t;       } else         tpos[++tcnt] = tlen;     }     while (j < len) tans[++tlen] = ans[++j];     cnt = tcnt;     for (int i = 1; i <= cnt; i++) pos[i] = tpos[i];     len = tlen;     for (int i = 1; i <= len; i++) ans[i] = tans[i];   }   void work2(int x, int y) {     int j = 0;     tlen = 0;     for (int i = x; i <= cnt; i += x) {       while (j + 1 <= pos[i]) {         j++;         tans[++tlen] = ans[j];       }       for (int l = 1; l <= y; l++) tans[++tlen] = t;     }     while (j < len) tans[++tlen] = ans[++j];     len = tlen;     for (int i = 1; i <= len; i++) ans[i] = tans[i];   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++)       scanf( %d , &a[i]), cnt += a[i] & 1, sum += a[i], id[i] = i;     if (cnt > 1) {       work1();       return 0;     }     sort(id, id + n);     cnt = 0;     for (int i = 0; i < n; i++) {       t = id[i];       if (a[t] & 1) continue;       if (len == 0) {         len = a[t];         cnt = len;         for (int i = 1; i <= len; i++) ans[i] = t, pos[i] = i;         continue;       }       d = gcd(cnt, a[t]);       work(cnt / d, a[t] / d);     }     for (int i = 0; i < n; i++)       if (a[id[i]] & 1) {         t = id[i];         if (len == 0) {           len = a[t];           cnt = len;           for (int i = 1; i <= len; i++) ans[i] = t, pos[i] = i;           continue;         }         d = gcd(cnt, a[t]);         work2(cnt / d, a[t] / d);         cnt = d;       }     printf( %d n , cnt);     for (int i = 1; i <= len; i++) printf( %c ,  a  + ans[i]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const int OO = 1e9;   const long long INF = 1e18;   const int irand(int lo, int hi) {     return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo;   }   const long long lrand(long long lo, long long hi) {     return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo;   }   template <typename T>   T getnum() {     int sign = 1;     T ret = 0;     char c;     do {       c = getchar();     } while (c ==     || c ==   n );     if (c ==  - )       sign = -1;     else       ret = c -  0 ;     while (1) {       c = getchar();       if (c <  0  || c >  9 ) break;       ret = 10 * ret + c -  0 ;     }     return sign * ret;   }   inline void ini(int& x) { x = getnum<int>(); }   inline void scani(int& x) { scanf( %d , &x); }   const int N = 1e5 + 5;   int n;   int a[N], b[N];   long long ans = 0;   vector<pair<int, int> > ins;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout.setf(ios::fixed);     cout.setf(ios::showpoint);     cout.precision(10);     cin >> n;     string str;     cin >> str;     for (int i = 0; i < n; i++) {       a[i + 1] = str[i] -  0 ;     }     cin >> str;     for (int i = 0; i < n; i++) {       b[i + 1] = str[i] -  0 ;     }     int difa = 0, difb = 0;     for (int i = 1; i <= n; i += 1) {       if (i & 1) {         difa += a[i];         difb += b[i];       } else {         difa -= a[i];         difb -= b[i];       }     }     if (difa != difb) {       cout << -1 <<   n ;       return 0;     }     int f = 2;     for (int i = 1; i <= n - 1; i += 1) {       if (f == i) f++;       while (a[i] > b[i]) {         a[i]--;         while (1) {           assert(f <= n);           if ((f - i) & 1) {             if (a[f] == 0) {               f++;             } else {               a[f]--;               break;             }           } else {             if (a[f] == 9) {               f++;             } else {               a[f]++;               break;             }           }         }         ans += f - i;         if (ins.size() < 100000) {           for (int j = f - 1; j >= i; j -= 1) {             if ((j - i) & 1) {               ins.push_back({j, 1});             } else {               ins.push_back({j, -1});             }           }         }       }       while (a[i] < b[i]) {         a[i]++;         while (1) {           assert(f <= n);           if ((f - i) & 1) {             if (a[f] == 9) {               f++;             } else {               a[f]++;               break;             }           } else {             if (a[f] == 0) {               f++;             } else {               a[f]--;               break;             }           }         }         ans += f - i;         if (ins.size() < 100000) {           for (int j = f - 1; j >= i; j -= 1) {             if ((j - i) & 1) {               ins.push_back({j, -1});             } else {               ins.push_back({j, 1});             }           }         }       }     }     cout << ans <<   n ;     for (int i = 0; i < min(100000, (int)ins.size()); i++) {       cout << ins[i].first <<     << ins[i].second <<   n ;     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int a[105];   bool b[105][105];   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < m; i++) {       int u, v;       cin >> u >> v;       u--;       v--;       b[u][v] = b[v][u] = true;     }     int ans = -1;     for (int i = 0; i < n; i++)       for (int j = i + 1; j < n; j++)         for (int k = j + 1; k < n; k++)           if (b[i][j] && b[i][k] && b[j][k]) {             if (ans == -1 || a[i] + a[j] + a[k] < ans) ans = a[i] + a[j] + a[k];           }     cout << ans << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int solve() {     int n, x;     cin >> n >> x;     vector<int> a(n);     vector<int> index;     for (int(i) = 0; (i) < n; (i)++) {       cin >> a[i];       if (a[i] % x != 0) index.push_back(i);     }     if (index.size() == 0) {       cout <<  -1 n ;       return 0;     }     int s = 0;     for (int(i) = 0; (i) < index.size(); (i)++) {       s += a[index[i]];     }     if (s % x != 0) {       cout << n <<   n ;       return 0;     }     int rez = n;     int q = 0, w = index.size() - 1;     int i = index[0], j = index[index.size() - 1];     while (s % x == 0) {       if (i + 1 < n - j) {         s -= a[i];         rez -= i + 1;         q++;         i = index[q];       } else {         s -= a[j];         rez -= n - j;         w--;         j = index[w];       }     }     cout << rez <<   n ;     return 0;   }   int main() {     int t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }


#include <bits/stdc++.h>   const int N = 1e5 + 10;   const int MOD = 1e9 + 7;   const int INF = 2e9;   const long long INFLL = 1e18;   const double PI = acos((double)-1);   inline int mult(int a, int b, int p = MOD) { return (1ll * a * b) % p; }   inline int add(int a, int b, int p = MOD) { return (1ll * a + b) % p; }   inline int fpow(long long n, long long k, int p = MOD) {     long long r = 1;     for (; k; k >>= 1) {       if (k & 1) r = r * n % p;       n = n * n % p;     }     return r;   }   inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }   using namespace std;   int s, x1, x2, t1, t2, p, ok;   int f[10000][2];   bool d;   int main() {     ios::sync_with_stdio(false);     cin >> s >> x1 >> x2;     cin >> t1 >> t2;     cin >> p >> ok;     if (ok == -1)       d = 0;     else       d = 1;     int ans = abs(x2 - x1) * t2;     f[p][d] = 0;     if (d == 0) {       for (int i = p - 1; i >= 0; i--) f[i][d] = f[i + 1][d] + t1;       f[0][!d] = f[0][d];       d = !d;       for (int i = 1; i <= s; i++) f[i][d] = f[i - 1][d] + t1;       f[s][!d] = f[s][d];       d = !d;       for (int i = s - 1; i > p; i--) f[i][d] = f[i + 1][d] + t1;     } else {       for (int i = p + 1; i <= s; i++) f[i][d] = f[i - 1][d] + t1;       f[s][!d] = f[s][d];       d = !d;       for (int i = s - 1; i >= 0; i--) f[i][d] = f[i + 1][d] + t1;       f[0][!d] = f[0][d];       d = !d;       for (int i = 1; i < p; i++) f[i][d] = f[i - 1][d] + t1;     }     if (x1 > x2) {       for (int i = x2 + 1; i <= x1; i++) {         if (f[i][0] < (x1 - i) * t2) f[i][0] += s * 2 * t1;         int x = (i - x2) * t1 + f[i][0];         ans = min(ans, x);       }     } else {       for (int i = x2 - 1; i >= x1; i--) {         if (f[i][1] < (i - x1) * t2) f[i][1] += s * 2 * t1;         int x = (x2 - i) * t1 + f[i][1];         ans = min(ans, x);       }     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   template <typename T>   bool Chkmin(T &x, T y) {     return y < x ? x = y, true : false;   }   template <typename T>   bool Chkmax(T &x, T y) {     return y > x ? x = y, true : false;   }   const int MAXN = 1e6 + 1e3, MAXM = MAXN, oo = 0x3f3f3f3f;   int n, m, tote, S, T;   int head[MAXN], next[MAXM], to[MAXM], dp[MAXN], out[MAXN], pos[MAXN], cur[MAXN];   void AddEdge(int u, int v) {     to[++tote] = v;     next[tote] = head[u];     head[u] = tote;   }   void Init() {     int u, v;     scanf( %d%d , &n, &m);     for (int i = 1, _ = m; i <= _; i++) {       scanf( %d%d , &u, &v);       AddEdge(v, u);       out[u]++;     }     scanf( %d%d , &S, &T);   }   void Solve() {     int u, v, num;     std::deque<int> q, p;     q.push_front(T);     p.push_front(++cur[T]);     memset(dp, 0x3f, sizeof(dp));     dp[T] = 0;     while (!q.empty()) {       u = q.front();       q.pop_front();       num = p.front();       p.pop_front();       if (num != cur[u]) continue;       for (int i = head[u]; i; i = next[i]) {         v = to[i];         out[v]--;         if (out[v] == 0) {           if (Chkmin(dp[v], dp[u])) {             q.push_front(v);             p.push_front(++cur[v]);           }         } else {           if (Chkmin(dp[v], dp[u] + 1)) {             q.push_back(v);             p.push_front(++cur[v]);           }         }       }     }   }   int main() {     Init();     Solve();     if (dp[S] == oo) dp[S] = -1;     printf( %d n , dp[S]);   }


#include <bits/stdc++.h>   using namespace std;   const int N = 100001;   int L, n, p, t;   int dp[N], f[N];   int main() {     scanf( %d%d%d%d , &L, &n, &p, &t);     f[0] = -t;     for (int i = 1, j = 0; i <= n; i++, j--) {       int l, r;       scanf( %d%d , &l, &r);       f[i] = f[i - 1];       dp[i] = dp[i - 1];       while (j < i && f[j] + t <= r) {         int x = max(f[j] + t, l), y = dp[j] + (r - x) / p, z = r - (r - x) % p;         if (y > dp[i] || (y == dp[i] && z < f[i])) dp[i] = y, f[i] = z;         j++;       }     }     printf( %d , dp[n]);     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int m;     cin >> m;     set<int> q;     for (int i = 0; i < n; ++i) {       int k;       cin >> k;       for (int j = 0; j < k; ++j) {         int x;         cin >> x;         q.insert(x);       }     }     cout << ((q.size() == m) ?  YES  :  NO ) << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k;     cin >> n >> k;     pair<int, int> ar[k];     for (long long i = 0; i < k; i++) {       cin >> ar[i].first >> ar[i].second;     }     set<long long> X, Y;     long long atk, safe;     for (int i = 0; i < k; i++) {       X.insert(ar[i].first);       Y.insert(ar[i].second);       atk = 1ll * X.size() * n + 1ll * Y.size() * n -             1ll * X.size() * 1ll * Y.size();       safe = n * n - atk;       cout << safe <<    ;     }     return 0;   }


#include <bits/stdc++.h>   void answer(const std::vector<std::vector<std::string>>& v) {     for (const auto& r : v) {       const char* separator =   ;       for (const std::string& x : r) {         std::cout << separator << x;         separator =    ;       }       std::cout <<   n ;     }   }   void solve(unsigned k) {     const size_t n = k - 1;     std::vector<std::vector<std::string>> t(n, std::vector<std::string>(n));     for (size_t i = 0; i < n; ++i) {       for (size_t j = 0; j < n; ++j) {         unsigned c = (i + 1) * (j + 1);         do {           t[i][j].push_back( 0  + c % k);           c /= k;         } while (c != 0);         std::reverse(t[i][j].begin(), t[i][j].end());       }     }     answer(t);   }   int main() {     unsigned k;     std::cin >> k;     solve(k);     return 0;   }


#include <bits/stdc++.h>   int main() {     int n, m, h, sum = 0, i;     int s[1000];     double d = 1;     scanf( %d %d %d , &n, &m, &h);     for (i = 0; i < m; i++) {       scanf( %d , &s[i]);       sum += s[i];     }     if (sum < n) {       puts( -1.0 n );       return 0;     }     if (sum - s[h - 1] < n - 1) {       puts( 1.0 n );       return 0;     }     for (i = 0; i < n - 1; i++) {       d *= (double)(sum - s[h - 1] - i) / (sum - i - 1);     }     printf( %.9lf n , 1 - d);     return 0;   }


#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   const int maxn = 1e6 + 10;   const int md = 1e9 + 7;   long long FAC[maxn], iFAC[maxn];   long long dp[maxn];   vector<tuple<int, int>> a;   long long mpow(long long a, long long b) {     if (b == 0) return 1;     long long res = mpow(a, b / 2);     res *= res;     res %= md;     if (b & 1ll) {       res *= a;       res %= md;     }     return res;   }   long long inv(long long a) { return mpow(a, md - 2); }   void pre_calc() {     FAC[0] = FAC[1] = 1;     for (int i = 2; i <= 200000; i++) FAC[i] = (FAC[i - 1] * i) % md;     iFAC[0] = iFAC[1] = 1;     for (int i = 2; i <= 200000; i++) iFAC[i] = (iFAC[i - 1] * inv(i)) % md;   }   int T;   string str;   int x, y, n, m;   int nk;   void task() {     cin >> n >> m >> nk;     for (int i = 0; i < nk; ++i) {       cin >> x >> y;       --x;       --y;       a.emplace_back(x, y);     }     --n;     --m;     a.emplace_back(n, m);     sort(a.begin(), a.end());     for (int i = 0; i < a.size(); ++i) {       int xi = get<0>(a[i]), yi = get<1>(a[i]);       dp[i] = FAC[xi + yi] * iFAC[xi] % md * iFAC[yi] % md;       for (int j = 0; j < i; ++j) {         int xj = get<0>(a[j]), yj = get<1>(a[j]);         long long mul =             FAC[xi + yi - xj - yj] * iFAC[xi - xj] % md * iFAC[yi - yj] % md;         dp[i] = (dp[i] - dp[j] * mul % md) % md;       }     }     cout << (dp[a.size() - 1] + md) % md << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     pre_calc();     T = 1;     while (T--) {       task();     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int d1, d2, d3;     cin >> d1 >> d2 >> d3;     int s1 = d1 + d3 + d2;     int s2 = 2 * d1 + 2 * d3;     int s3 = 2 * d2 + 2 * d1;     int s4 = 2 * d2 + 2 * d3;     cout << min(s1, min(s2, min(s3, s4)));   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int i = 0, a = 0, b = 0, c = 0;     string s;     cin >> s;     for (; i < s.length(); i++) {       if (s[i] ==  a )         a++;       else         break;     }     for (; i < s.length(); i++) {       if (s[i] ==  b )         b++;       else         break;     }     for (; i < s.length(); i++) {       if (s[i] ==  c )         c++;       else         break;     }     if (i != s.length() || (c != a && c != b) || a == 0 || b == 0)       cout <<  NO  << endl;     else       cout <<  YES  << endl;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   long long f[5020][5020], a[5020], powk[5020], ans, ans2, invn;   int n, k;   inline long long power(long long x, long long y) {     long long res = 1;     while (y) {       if (y & 1) res = res * x % mod;       x = x * x % mod;       y >>= 1;     }     return res;   }   void init() {     powk[0] = 1;     for (register int i = 1; i <= n; i++)       powk[i] = powk[i - 1] * (k - i + 1) % mod;     invn = mod - power(n, mod - 2);   }   int main() {     scanf( %d %d , &n, &k);     init();     ans = 1;     for (register int i = 1; i <= n; i++)       scanf( %lld , &a[i]), ans = ans * a[i] % mod;     f[0][0] = 1;     for (register int i = 1; i <= n; i++) {       f[i][0] = 1;       for (register int j = 1; j <= i; j++) {         f[i][j] = (f[i - 1][j] + f[i - 1][j - 1] * a[i]) % mod;       }     }     for (register int i = 0; i <= n; i++) {       ans2 = (ans2 + f[n][i] * power(invn, n - i) % mod * powk[n - i]) % mod;     }     cout << (ans - ans2 + mod) % mod << endl;   }


#include <bits/stdc++.h>   using namespace std;   const int dr[] = {+1, -1, +0, +0, +1, -1, +1, -1};   const int dc[] = {+0, +0, +1, -1, +1, -1, -1, +1};   const int ms[] = {+31, +29, +31, 30, +31, +30, +31, +31, +30, +31, +30, +31};   const int N = 200 + 5;   const int MOD = 1e9 + 7;   void add(int &a, int b) {     a += b;     while (a >= MOD) a -= MOD;   }   int dp[N][N][N];   int n, first[N];   string w;   int failure(char letra, int idx) {     if (idx >= (int)w.size()) return (int)w.size();     while (idx > 0 && letra != w[idx]) {       idx = first[idx - 1];     }     return idx + (w[idx] == letra);   }   void KMP() {     first[0] = 0;     for (int i = 1; i < (int)(int)w.size(); i++) {       first[i] = failure(w[i], first[i - 1]);     }   }   int solve(int p = 0, int streak = 0, int score = 0) {     if (score < 0) return 0;     if (p >= 2 * n) return (streak == (int)w.size() && score == 0);     int &rec = dp[p][streak][score];     if (~rec) return rec;     rec = 0;     add(rec, solve(p + 1, failure( ( , streak), score + 1));     add(rec, solve(p + 1, failure( ) , streak), score - 1));     return rec;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n >> w;     KMP();     memset(dp, -1, sizeof(dp));     cout << solve() <<   n ;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k;     int m;     cin >> n >> m >> k;     set<long long> removal;     for (int i = 0; i < m; i++) {       long long x;       cin >> x;       removal.insert(x);     }     long long total = 0;     long long offset = 0;     set<long long>::iterator itr = removal.begin();     while (itr != removal.end()) {       auto num = *itr;       auto cur_offset = offset;       auto a = num - offset;       auto r = a % k, d = a / k;       long long K = 0;       if (r == 0) {         K = a;       } else {         K = k * (d + 1);       }       total++;       do {         offset++;         itr++;       } while (itr != removal.end() && *itr - cur_offset <= K);     }     cout << total << endl;     ;   }


#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 4;   const int maxlog = 22;   const int mod = 1e9 + 7;   const int sq = 350;   int deg[maxn], cnt[maxn];   map<pair<int, int>, int> m;   long long ans = 0;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cout.precision(20);     int n, p;     cin >> n >> p;     for (int i = 0; i < n; i++) {       int u, v;       cin >> u >> v;       deg[u]++, deg[v]++;       if (u > v) swap(u, v);       m[make_pair(u, v)]++;     }     for (int i = 1; i <= n; i++) cnt[deg[i]]++;     for (int i = n; i >= 0; i--) cnt[i] += cnt[i + 1];     for (int i = 1; i <= n; i++) {       int x = max(0, p - deg[i]);       ans += (long long)cnt[x];       if (2 * deg[i] >= p) ans--;     }     ans /= 2;     for (map<pair<int, int>, int>::iterator it = m.begin(); it != m.end(); it++) {       int u = (*it).first.first, v = (*it).first.second;       int cnt = (*it).second;       if (deg[u] + deg[v] >= p && deg[u] + deg[v] - cnt < p) ans--;     }     cout << ans;     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int len[55];   int n;   struct node {     int x, y, s;   } arr[55 * 55];   int ans[55 * 55][2];   int main() {     int i, j, tmp, k;     while (scanf( %d , &n) != EOF) {       for (i = 1; i <= n; i++) scanf( %d , &len[i]);       k = 0;       for (i = 1; i <= n; i++)         for (j = 1; j <= len[i]; j++) {           scanf( %d , &tmp);           arr[k].x = i;           arr[k].y = j;           arr[k++].s = tmp;         }       int mi, t, m = 0;       for (i = 0; i < k - 1; i++) {         mi = arr[i].s;         t = i;         for (j = i + 1; j < k; j++)           if (arr[j].s < mi) {             mi = arr[j].s;             t = j;           }         if (t != i) {           ans[m][0] = i;           ans[m++][1] = t;           swap(arr[i].s, arr[t].s);         }       }       printf( %d n , m);       for (i = 0; i < m; i++)         printf( %d %d %d %d n , arr[ans[i][0]].x, arr[ans[i][0]].y,                arr[ans[i][1]].x, arr[ans[i][1]].y);     }     return 0;   }


#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n, ans = 0, num[101], temp, minn = 1e9 + 7, maxx = 0, ann;       for (int i = 1; i <= 100; i++) num[i] = 0;       scanf( %d , &n);       for (int i = 1; i <= n; i++) {         scanf( %d , &temp);         num[temp]++;         minn = min(minn, temp);         maxx = max(maxx, temp);       }       for (int i = 2; i <= maxx * 2; i++) {         int sum = 0;         for (int j = 1; j <= i / 2; j++)           if (j != i - j)             sum += min(num[j], num[i - j]);           else             sum += num[j] / 2;         if (sum > ans) {           ans = sum;         }       }       printf( %d n , ans);     }   }


#include <bits/stdc++.h>   using namespace std;   const long long int mod = 998244353;   vector<long long int> roots;   long long int powermod(long long int a, long long int n, long long int p) {     long long int ans = 1;     while (n > 0) {       if (n & 1) ans = (ans * a) % p;       n >>= 1;       a = a * a % p;     }     return ans;   }   long long int primitive_root(long long int p) {     vector<long long int> fact;     int phi = p - 1, n = phi;     for (int i = 2; i * i <= n; ++i) {       if (n % i == 0) {         fact.push_back(i);         while (n % i == 0) n /= i;       }     }     if (n > 1) fact.push_back(n);     for (int res = 2; res <= p; ++res) {       bool ok = true;       for (int i = 0; i < fact.size() && ok; ++i)         ok &= powermod(res, phi / fact[i], p) != 1;       if (ok) return res;     }     return -1;   }   long long int mod_inverse(long long int a, long long int p) {     vector<long long int> A = {p, 1, 0};     vector<long long int> B = {a, 0, 1};     while (B[0] != 1) {       long long int ratio = A[0] / B[0];       A[0] -= ratio * B[0];       A[1] -= ratio * B[1];       A[2] -= ratio * B[2];       swap(A, B);     }     return (p + (B[2] % p)) % p;   }   void find_root() {     long long int val = mod - 1;     int start = 1 << 20;     while (start > 0) {       if (val % start == 0) {         long long int c = val / start;         roots.push_back(powermod(primitive_root(mod), c, mod));       }       start >>= 1;     }     reverse(roots.begin(), roots.end());   }   void initFFT(vector<long long int>& W, vector<long long int>& rbit,                bool inverse, long long int root) {     int n = W.size();     for (int i = 0; i < n; ++i) {       int k = i, power = n >> 1, ans = 0;       while (k > 0) {         if (k & 1) ans += power;         power >>= 1;         k >>= 1;       }       rbit[ans] = i;     }     W[0] = 1;     for (int i = 1; i < n; ++i) W[i] = (W[i - 1] * root) % mod;     if (inverse) {       for (int i = 1; i < n / 2; ++i) swap(W[i], W[n - i]);     }   }   vector<long long int> FFT(vector<long long int> P, bool inverse,                             long long int root) {     int n = P.size();     vector<long long int> W(n), rbit(n);     initFFT(W, rbit, inverse, root);     vector<long long int> Q(n);     for (int i = 0; i < n; ++i) Q[i] = P[rbit[i]];     for (int l = 1; l < n; l <<= 1) {       int step = n / (l << 1);       for (int i = 0; i < n; i += l << 1) {         int j = 0;         for (int k = 0; k < l; ++k) {           long long int u = Q[i + k], v = (W[j] * Q[i + k + l]) % mod;           Q[i + k] = (u + v) % mod;           Q[i + k + l] = (mod + (u - v) % mod) % mod;           j += step;         }       }     }     if (inverse) {       long long int inv = mod_inverse(n, mod);       for (int i = 0; i < n; ++i) {         Q[i] = (Q[i] * inv) % mod;       }     }     return Q;   }   vector<long long int> multiply(vector<long long int>& A,                                  vector<long long int>& B) {     long long int n = 1, root = 0;     while (n < A.size() + B.size()) {       ++root;       n <<= 1;     }     root = roots[root];     A.resize(n, 0);     B.resize(n, 0);     vector<long long int> DFTA = FFT(A, false, root);     vector<long long int> DFTB = FFT(B, false, root);     for (int i = 0; i < n; ++i) DFTA[i] = DFTA[i] * DFTB[i] % mod;     DFTA = FFT(DFTA, true, root);     return DFTA;   }   vector<long long int> A;   void query(int k, int q, int l, int r, int d, int type) {     int n = A.size();     vector<vector<long long int> > B(n);     for (int i = 0; i < n; ++i) {       if (i < l or i > r)         B[i] = {1, q - A[i]};       else if (type == 2)         B[i] = {1, q - d - A[i]};       else         B[i] = {1, q - d};     }     while (B.size() > 1) {       vector<vector<long long int> > C;       for (int i = 0; i < B.size(); i += 2) {         if (i == B.size() - 1)           C.push_back(B.back());         else           C.push_back(multiply(B[i], B[i + 1]));       }       B = C;     }     cout << (B[0][k] % mod + mod) % mod <<   n ;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     find_root();     int n, k;     cin >> n >> k;     A = vector<long long int>(n);     for (int i = 0; i < n; ++i) cin >> A[i];     int Q;     cin >> Q;     for (int i = 0; i < Q; ++i) {       int type;       cin >> type;       if (type == 1) {         int q, l, d;         cin >> q >> l >> d;         query(k, q, l - 1, l - 1, d, type);       } else {         int q, l, r, d;         cin >> q >> l >> r >> d;         query(k, q, l - 1, r - 1, d, type);       }     }   }


#include <bits/stdc++.h>   const int N = 2005;   using namespace std;   bitset<N> f[N];   int n;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         int t;         scanf( %d , &t);         if (t > 0) f[i][j] = 1;       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++)         if (f[i][j]) {           f[i] |= f[j];         }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++)         if (f[i][j]) {           f[i] |= f[j];         }     }     for (int i = 1; i <= n; i++) {       if (f[i].count() != n) {         printf( NO );         return 0;       }     }     printf( YES );     return 0;   }


#include <bits/stdc++.h>   int main() {     int a, b, c;     scanf( %d%d%d , &a, &b, &c);     if ((a + b) == c || (c + b) == a || (a + c) == b) {       printf( 1 );     } else {       if ((a + b) < c) {         printf( %d , (c - b - a + 1));       } else if (b + c < a) {         printf( %d , (a - b - c + 1));       } else if (c + a < b) {         printf( %d , (b - c - a + 1));       } else {         printf( 0 );       }     }     return 0;   }