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


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


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


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k, i, teo = 0, s = 0, a[100001], t[100001]; n   cin >> n >> k; n   for (i = 1; i <= n; i++) cin >> a[i]; n   for (i = 1; i <= n; i++) { n     cin >> t[i]; n     if (t[i] == 1) { n       teo = teo + a[i]; n       a[i] = 0; n     } n     a[i] = a[i] + a[i - 1]; n   } n   for (i = k; i <= n; i++) s = max(s, a[i] - a[i - k]); n   cout << teo + s; n } n


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


#include <bits/stdc++.h> n int main() { n   int n, k, i; n   scanf( %d , &n); n   int a[n][2]; n   for (i = 0; i < n; i++) { n     scanf( %d%d , &a[i][0], &a[i][1]); n   } n   scanf( %d , &k); n   for (i = 0; i < n; i++) { n     if (k >= a[i][0] && k <= a[i][1]) { n       printf( %d , n - i); n       break; n     } n   } n   return 0; n } n


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


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


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


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


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


#include <bits/stdc++.h> n int main() { n   int n, i, j, k; n   scanf( %d , &n); n   int a[1000]; n   int b[1000]; n   int c[6] = {0}; n   int d[6] = {0}; n   int sum = 0; n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     c[a[i]]++; n   } n   for (i = 0; i < n; i++) { n     scanf( %d , &b[i]); n     d[b[i]]++; n   } n   for (i = 1; i <= 5; i++) { n     if ((c[i] - d[i]) % 2 == 0) { n       if (c[i] > d[i]) n         sum = sum + (c[i] - d[i]) / 2; n       else n         sum = sum + (d[i] - c[i]) / 2; n     } else { n       sum = -1; n       break; n     } n   } n   if (sum == -1) n     printf( -1 n ); n   else n     printf( %d n , sum / 2); n   return 0; n } n


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


#include <bits/stdc++.h> n using namespace std; n long long arr[1002][2][102]; n long long p10[1002]; n long long n, m, k; n long long ans = 0; n void pree() { n   p10[0] = 1; n   for (long long i = 1; i <= n; ++i) { n     p10[i] = p10[i - 1] * 10LL; n     p10[i] %= k; n   } n } n void solve() { n   pree(); n   arr[0][0][0] = 1; n   long long i, j; n   long long p; n   long long ti; n   long long ti2; n   for (i = 1; i <= n; ++i) { n     for (j = 1; j < k; ++j) { n       arr[i][0][j] = (arr[i - 1][1][j] + arr[i - 1][0][j]) % m; n     } n     arr[i][0][0] = arr[i - 1][0][0]; n     for (j = 1; j <= 9; ++j) { n       ti = j * p10[i - 1]; n       ti %= k; n       for (p = 1; p < k; ++p) { n         ti2 = (p + ti) % k; n         arr[i][1][ti2] += arr[i - 1][1][p] + arr[i - 1][0][p]; n         arr[i][1][ti2] %= m; n       } n       ti2 = ti; n       arr[i][1][ti2] += arr[i - 1][0][0]; n       arr[i][1][ti2] %= m; n     } n   } n   ans = arr[n][1][0]; n   ti = 9; n   ti %= m; n   for (i = n - 1; i > 0; --i) { n     ans += ti * arr[i][1][0]; n     ans %= m; n     ti *= 10; n     ti %= m; n   } n } n void MAIN() { n   scanf( %I64d %I64d %I64d , &n, &k, &m); n   solve(); n   printf( %I64d n , ans); n } n int main() { n   MAIN(); n   return 0; n } n


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


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


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


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


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


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


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


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


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


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


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


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long m, n, q = 998244853, sum = 1, a = 1, b = 1, r = 0, x = 208513042, y, n                   z; n   cin >> n >> m; n   if (n == 0) { n     cout << 0; n     return 0; n   } n   if (m == 0) { n     cout << n; n     return 0; n   } n   for (long long i = 1; i < min(m, n); i++) { n     a = (a * (m + n + 1 - i)) % q; n     b = (b * i) % q; n     r = ((a % b) + r * i) % q; n     sum = (sum + a / b) % q; n   } n   a = (a * (m + n + 1 - min(m, n))) % q; n   b = (b * min(m, n)) % q; n   r = (r * min(m, n)) % q; n   for (long long j = 1 + min(m, n); j <= 2000; j++) { n     a = (a * j) % q; n     b = (b * j) % q; n     r = (r * j) % q; n   } n   y = (r * x) % q; n   sum = (sum + y) % q; n   if (n > m) { n     y = (a * x) % q; n     z = (y * (n - m)) % q; n     sum = (sum + z) % q; n   } n   cout << sum; n   return 0; n } n


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


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


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


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


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


#include <bits/stdc++.h> n using namespace std; n int n; n int cnt[3], seen[3]; n string s; n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> s; n   int m = n / 3; n   for (int i = 0; i < n; i++) { n     cnt[s[i] -  0 ]++; n   } n   for (int i = 0; i < n; i++) { n     if (cnt[s[i] -  0 ] > m) { n       for (long long j = 0; j < 3; j++) { n         if (cnt[j] < m) { n           if (j > s[i] -  0  && seen[s[i] -  0 ] < m) break; n           cnt[j]++; n           cnt[s[i] -  0 ]--; n           s[i] = j +  0 ; n         } n       } n     } n     seen[s[i] -  0 ]++; n   } n   cout << s << endl; n   return 0; n } n