#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n using ld = long double; n template <class T> n using Table = vector<vector<T>>; n const ld eps = 1e-9; n template <class S, class T> n ostream &operator<<(ostream &os, const pair<S, T> v) { n   os <<  (   << v.first <<  ,   << v.second <<  ) ; n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const vector<T> &v) { n   for (int i = 0; i < v.size(); i++) { n     if (i > 0) { n       os <<    ; n     } n     os << v[i]; n   } n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const vector<vector<T>> &v) { n   for (int i = 0; i < v.size(); i++) { n     if (i > 0) { n       os << endl; n     } n     os << v[i]; n   } n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const vector<set<T>> &v) { n   for (int i = 0; i < v.size(); i++) { n     if (i > 0) { n       os << endl; n     } n     os << v[i]; n   } n   return os; n } n template <class T> n ostream &operator<<(ostream &os, const set<T> &v) { n   int i = 0; n   for (auto it : v) { n     if (i > 0) { n       os <<    ; n     } n     os << it; n     i++; n   } n   return os; n } n using ll = long long int; n pair<int, bool> get_c(const vector<vector<int>> &graph, int N) { n   vector<int> comes(N); n   vector<int> colors(N, -1); n   bool ok = true; n   queue<int> que; n   int sum = 0; n   for (int i = 0; i < N; ++i) { n     if (!comes[i]) { n       colors[i] = 0; n       sum++; n       que.emplace(i); n       while (!que.empty()) { n         int now = que.front(); n         comes[now] = true; n         que.pop(); n         for (auto e : graph[now]) { n           if (!comes[e]) { n             que.emplace(e); n             colors[e] = 1 ^ colors[now]; n           } n           if (colors[e] == colors[now]) ok = false; n         } n       } n     } n   } n   return make_pair(sum, ok); n } n ll int_pow(int a, int b) { n   if (b == 0) n     return 1; n   else n     return a * int_pow(a, b - 1); n } n int main() { n   ios::sync_with_stdio(false); n   int N, M; n   cin >> N >> M; n   vector<vector<int>> edges(N); n   for (int i = 0; i < M; ++i) { n     int u, v; n     cin >> u >> v; n     u--; n     v--; n     edges[u].push_back(v); n     edges[v].push_back(u); n   } n   auto p = get_c(edges, N); n   int C = p.first; n   bool b_ok = p.second; n   int isolated_v_cnt = 0; n   for (int i = 0; i < N; ++i) { n     if (edges[i].empty()) isolated_v_cnt++; n   } n   vector<vector<vector<ll>>> cnts(2, vector<vector<ll>>(2, vector<ll>(2))); n   cnts[1][0][0] = cnts[0][0][1] = int_pow(2, isolated_v_cnt); n   cnts[0][1][0] = b_ok ? int_pow(2, C) : 0; n   cnts[1][1][1] = int_pow(2, N); n   cnts[1][0][1] = int_pow(2, C); n   { n     int K = N / 2; n     vector<int> yys(1ll << (N - K)); n     for (int xx = 0; xx < (1ll << K); ++xx) { n       bool ok = true; n       vector<int> uses(K); n       for (int i = 0; i < K; ++i) { n         uses[i] = (xx & (1ll << i)) != 0; n       } n       int yy = 0; n       for (int i = 0; i < K; ++i) { n         for (auto e : edges[i]) { n           if (uses[i]) { n             if (e < K) { n               if (uses[i] && uses[e]) ok = false; n             } else { n               yy |= (1ll << (e - K)); n             } n           } n         } n       } n       if (ok) { n         yys[yy ^ ((1ll << (N - K)) - 1)]++; n       } else { n       } n     } n     for (int i = 0; i < (N - K); ++i) { n       for (int x = 0; x < (1ll << (N - K)); ++x) { n         if ((x & (1ll << i)) != 0) { n           yys[x ^ (1ll << i)] += yys[x]; n         } n       } n     } n     ll answer = 0; n     for (int yy = 0; yy < (1ll << (N - K)); ++yy) { n       bool ok = true; n       vector<int> uses(N - K); n       for (int i = 0; i < N - K; ++i) { n         uses[i] = (yy & (1ll << i)) != 0; n       } n       for (int i = 0; i < N - K; ++i) { n         for (auto e : edges[K + i]) { n           if (e >= K) { n             if (uses[i] && uses[e - K]) ok = false; n           } else { n           } n         } n       } n       if (ok) { n         answer += yys[yy]; n       } n     } n     cnts[1][1][0] = cnts[0][1][1] = answer; n   } n   if (!M) cnts[0][0][0] = (1ll << N); n   ll answer = 0; n   answer = cnts[1][1][1] - cnts[1][1][0] - cnts[1][0][1] - cnts[0][1][1] + n            cnts[1][0][0] + cnts[0][1][0] + cnts[0][0][1] - cnts[0][0][0]; n   cout << answer << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define ll long long int n #define mp make_pair n //ll const MAX_N=100000; n ll const MAX_A = (ll)1e9 + 1; n const ll MOD = 1e9 + 7; n  n  n  n  n void solve() n { n  ll n, l,u,v,w,n1,n2,n3,h,sum=0,suma=0,sumb=0,sumc=0,minia=MAX_A,minib=MAX_A,minic=MAX_A, ansa,ansb,ansc; n  cin>>n1>>n2>>n3; n  ll a[n1],b[n2],c[n3]; n  vector<ll> pos; n  for(auto & i: a) n  { n   cin>>i; n   minia=min(i,minia); n   suma+=i; n   } n  for(auto & i: b) n  { n   cin>>i; n   minib=min(i,minib); n   sumb+=i; n   } n  for(auto & i: c) n  { n   cin>>i; n   minic=min(i,minic); n   sumc+=i; n  } n  sum=suma+sumb+sumc; n  sort(a,a+n1); n  sort(b,b+n2); n  sort(c,c+n3); n  ansa=sum-2*minia-2*min(min(minib,minic),suma-minia); n  ansb=sum-2*minib-2*min(min(minia,minic),sumb-minib); n  ansc=sum-2*minic-2*min(min(minib,minia),sumc-minic); n   n  n  cout<<max(ansa,max(ansb,ansc))<<endl; n } n  n int main() n { n  //freopen( luka.in , r , stdin); n  ios_base::sync_with_stdio(false); n     cin.tie(NULL); n  ll t = 1; n  //cin >> t; n  for (ll i = 1; i <= t; i++) n  { n   solve(); n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n priority_queue<int, vector<int>, greater<int> > q; n int main() { n   int n, m; n   while (cin >> n) { n     long long sum = 0; n     if (!q.empty()) { n       q.pop(); n     } n     while (n--) { n       scanf( %d , &m); n       if (!q.empty() && m > q.top()) { n         sum += m - q.top(); n         q.pop(); n         q.push(m); n         q.push(m); n       } else { n         q.push(m); n       } n     } n     cout << sum << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char str[100]; n   int n, z; n   cin >> z; n   while (z--) { n     cin >> str; n     n = strlen(str); n     if (n > 10) { n       cout << str[0] << n - 2 << str[n - 1] << endl; n     } else n       cout << str << endl; n   } n   return 0; 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   cout.tie(0); n   long long v1, v2, v3, vm; n   long long u1, u2, u3; n   cin >> v1 >> v2 >> v3 >> vm; n   for (u1 = v1; u1 <= 2 * v1; u1++) { n     for (u2 = v2; u2 <= 2 * v2; u2++) { n       for (u3 = v3; u3 <= 2 * v3; u3++) { n         if (vm <= u3 && vm <= u2 && vm <= u1 && 2 * vm >= u3 && 2 * vm < u2 && n             2 * vm < u1 && u1 > u2 && u2 > u3 && u1 > u3) { n           cout << u1 <<   n  << u2 <<   n  << u3; n           return 0; n         } n       } n     } n   } n   cout <<  -1 ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double l, r, x, y, k, i, f = 0; n   cin >> l >> r >> x >> y >> k; n   for (i = x; i <= y; i++) { n     if ((i * k) >= l && (i * k) <= r) { n       f = 1; n       break; n     } else n       continue; n   } n   if (f == 1) n     cout <<  YES n ; n   else n     cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, m, x; n   cin >> n >> m; n   vector<long long> vPref(n), vSuf(n), vTot(n), vCen(n); n   for (int i(0), ThkMk(n); i < ThkMk; ++i) { n     cin >> x; n     vector<long long> v(x); n     vector<long long> prefix(x); n     vector<long long> suffix(x); n     vector<long long> dp(x); n     long long curMx, curSuf, curPref; n     for (int i(0), ThkMk(x); i < ThkMk; ++i) { n       cin >> v[i]; n       dp[i] = prefix[i] = v[i]; n       if (i > 0) { n         prefix[i] += prefix[i - 1]; n         dp[i] += max(0LL, dp[i - 1]); n         curMx = max(curMx, dp[i]); n       } else n         curMx = v[0]; n     } n     suffix[x - 1] = v[x - 1]; n     curSuf = suffix[0]; n     curPref = prefix[0]; n     for (int i = x - 2; i >= 0; --i) suffix[i] += suffix[i + 1] + v[i]; n     for (int i(0), ThkMk(x); i < ThkMk; ++i) { n       curSuf = max(curSuf, suffix[i]); n       curPref = max(curPref, prefix[i]); n     } n     vPref[i] = curPref; n     vSuf[i] = curSuf; n     vTot[i] = suffix[0]; n     vCen[i] = curMx; n   } n   long long ans = -1e18; n   long long prev = 0; n   for (int i(0), ThkMk(m); i < ThkMk; ++i) { n     cin >> x; n     --x; n     ans = max(ans, vCen[x]); n     ans = max(ans, prev + vPref[x]); n     prev = max({0LL, prev + vTot[x], vSuf[x]}); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007ll; n const double EPS = 1e-11; n int n; n char s[1252525]; n int cntright[1252525]; n int cntleft[1252525]; n int revright[1252525]; n int revleft[1252525]; n long long sumright[1252525]; n long long sumleft[1252525]; n int main() { n   scanf( %d , &n); n   scanf( %s , s); n   for (int i = 0; i < (int)(n); ++i) { n     if (s[i] ==  U ) { n       cntright[i + 1] = cntright[i] + 1; n       cntleft[i + 1] = cntleft[i]; n       revright[cntright[i + 1]] = i + 1; n     } else { n       cntright[i + 1] = cntright[i]; n       cntleft[i + 1] = cntleft[i] + 1; n       revleft[cntleft[i + 1]] = i + 1; n     } n   } n   for (int i = 0; i < (int)(n); ++i) { n     if (s[i] ==  U ) { n       sumright[i + 1] = sumright[i] + (n - i); n       sumleft[i + 1] = sumleft[i]; n     } else { n       sumright[i + 1] = sumright[i]; n       sumleft[i + 1] = sumleft[i] + (i + 1); n     } n   } n   for (int q = 0; q < (int)(n); ++q) { n     int l = cntright[q]; n     int r = cntleft[n] - cntleft[q + 1]; n     if (s[q] ==  U ) { n       if (r > l) { n         int rightbnd = l + 1; n         int leftbnd = l; n         int rrr = revleft[cntleft[q + 1] + rightbnd]; n         int lll = 0; n         long long ans = 0ll; n         ans += n             (sumright[q] - sumright[lll] - (long long)leftbnd * (n - q)) * 2ll; n         ans += (sumleft[rrr] - sumleft[q + 1] - (long long)rightbnd * (q + 1)) * n                2ll; n         ans += q + 1; n         printf( %I64d , ans); n       } else { n         int rightbnd = r; n         int leftbnd = r; n         int rrr = n; n         int lll = revright[cntright[q] - leftbnd]; n         long long ans = 0ll; n         ans += n             (sumright[q] - sumright[lll] - (long long)leftbnd * (n - q)) * 2ll; n         ans += (sumleft[rrr] - sumleft[q + 1] - (long long)rightbnd * (q + 1)) * n                2ll; n         ans += n - q; n         printf( %I64d , ans); n       } n     } else { n       if (l > r) { n         int rightbnd = r; n         int leftbnd = r + 1; n         int rrr = n; n         int lll = revright[cntright[q] - leftbnd]; n         long long ans = 0ll; n         ans += n             (sumright[q] - sumright[lll] - (long long)leftbnd * (n - q)) * 2ll; n         ans += (sumleft[rrr] - sumleft[q + 1] - (long long)rightbnd * (q + 1)) * n                2ll; n         ans += n - q; n         printf( %I64d , ans); n       } else { n         int rightbnd = l; n         int leftbnd = l; n         int rrr = revleft[cntleft[q + 1] + rightbnd]; n         int lll = 0; n         long long ans = 0ll; n         ans += n             (sumright[q] - sumright[lll] - (long long)leftbnd * (n - q)) * 2ll; n         ans += (sumleft[rrr] - sumleft[q + 1] - (long long)rightbnd * (q + 1)) * n                2ll; n         ans += q + 1; n         printf( %I64d , ans); n       } n     } n     if (q != n - 1) { n       putchar(   ); n     } else { n       putchar(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   long long n, k, m; n   cin >> n >> k >> m; n   vector<long long> a(k); n   for (long long& v : a) cin >> v; n   sort(a.begin(), a.end()); n   a.push_back(0); n   k++; n   a.insert(a.begin(), 0); n   for (int i = 1; i <= k; i++) a[i] += a[i - 1]; n   vector<long long> dp((k + 1) * n, 1e18); n   dp[0] = 0; n   for (int i = 0; i < n; i++) { n     auto ndp = dp; n     for (int j = 0; j <= k; j++) { n       for (int l = dp.size() - 1 - j; l >= 0; l--) n         ndp[l + j] = min(ndp[l + j], dp[l] + a[j]); n     } n     dp = ndp; n   } n   cout << upper_bound(dp.begin(), dp.end(), m) - dp.begin() - 1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long int M = 1e9 + 7; n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int t; n   cin >> t; n   vector<long long int> v; n   for (long long int i = 1; i <= 100000; ++i) { n     long long int x = (i * (3 * i + 1)) / 2; n     if (x <= 2e9) { n       v.push_back(x); n     } n   } n   while (t--) { n     long long int n; n     cin >> n; n     long long int ans = 0; n     while (n >= 2) { n       long long int x = lower_bound(v.begin(), v.end(), n) - v.begin(); n       if (v[x] != n) x--; n       n -= v[x]; n       ans++; n     } n     cout << ans <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n int n; n vector<long long> a(200013); n bool f(long long m) { n   long long l = (long long)(a[0] + 2 * m), r = (long long)(a[n - 1] - 2 * m); n   if (l >= r) { n     return 1; n   } n   if ((r - l) <= 2 * m) { n     return 1; n   } n   long long ll = n       a[int(upper_bound(a.begin(), a.begin() + n, l) - a.begin())] + 2 * m; n   long long rr = a[int(upper_bound(a.begin(), a.begin() + n, ll) - a.begin())]; n   if (rr == 0) { n     return 1; n   } n   return (rr >= r); n } n int main() { n   cin >> n; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n     a[i] *= 2; n   } n   sort(a.begin(), a.begin() + n); n   long long l = 0, r = 2000000000; n   while (l + 1 < r) { n     long long m = (l + r) / 2; n     if (!f(m)) { n       l = m; n     } else { n       r = m; n     } n   } n   if (!f(l)) l++; n   cout.setf(ios::fixed); n   cout.precision(6); n   cout << (double)l / 2.0 << endl n        << (double)(a[0] + l) / 2.0 <<     << (double)(a[n - 1] - l) / 2.0 n        <<    ; n   long long ll = (long long)(a[0] + 2 * l); n   long long lll = a[int(upper_bound(a.begin(), a.begin() + n, ll) - a.begin())]; n   cout << (double)(lll + l) / 2.0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000009; n long long modexp_recursion(long long a, long long b, long long n) { n   long long t = 1; n   if (b == 0) return 1; n   if (b == 1) return a % n; n   t = modexp_recursion(a, b >> 1, n); n   t = t * t % n; n   if (b & 0x1) { n     t = t * a % n; n   } n   return t; n } n int main() { n   long long n, m, k; n   cin >> n >> m >> k; n   long long a = m / (k - 1); n   if (a <= n - m) n     cout << m << endl; n   else { n     long long ret = 0; n     a = n - (n - m) * k; n     long long b = a / k; n     if (b > 0) { n       ret = 2 * k * modexp_recursion(2, b, MOD) - 2 * k; n       ret = ret % MOD; n     } n     ret = (ret + (n - m) * (k - 1)) % MOD; n     ret = (ret + (a - a / k * k)) % MOD; n     cout << ret << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int read(int f = 1, int x = 0, char ch =    ) { n   while (!isdigit(ch = getchar())) n     if (ch ==  - ) f = -1; n   while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar(); n   return f * x; n } n const int N = 5e5 + 5; n int n, m, f[N], g[N], fs[N], gs[N], ft[N], gt[N], cls[N]; n vector<pair<int, long long> > s[N]; n int find(int *p, int x) { return p[x] == x ? x : find(p, p[x]); } n void merge(int *p, int *s, int *t, int x, int y, int z) { n   x = find(p, x), y = find(p, y); n   if (s[x] > s[y]) swap(x, y); n   p[x] = y, s[y] += s[x], t[x] = z; n } n long long ask(vector<pair<int, long long> > &s, int t) { n   return s.back().second - n          (--lower_bound(s.begin(), s.end(), pair<int, long long>(t, 0))) n              ->second; n } n int main() { n   n = read(), m = read(); n   for (int i = 1; i <= n; ++i) n     f[i] = g[i] = i, fs[i] = gs[i] = 1, s[i].emplace_back(-1, 0); n   for (int i = 1; i <= m; ++i) { n     char op[2]; n     int x, y; n     scanf( %s , op); n     if (op[0] ==  U ) n       x = read(), y = read(), merge(f, fs, ft, x, y, i); n     else if (op[0] ==  M ) n       x = read(), y = read(), merge(g, gs, gt, x, y, i); n     else if (op[0] ==  A ) n       x = read(), x = find(f, x), n       s[x].emplace_back(i, s[x].back().second + fs[x]); n     else if (op[0] ==  Z ) n       x = read(), x = find(g, x), cls[x] = i; n     else { n       int x = read(), y, t = cls[x]; n       for (y = x; g[y] != y; y = g[y]) n         if (cls[g[y]] > gt[y]) t = max(t, cls[g[y]]); n       long long ans = ask(s[x], t); n       for (y = x; f[y] != y; y = f[y]) ans += ask(s[f[y]], max(t, ft[y])); n       printf( %lld n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = (int)1e9 + 7; n const int MAXN = (int)1003; n const int LOG = (int)11; n const int infint = (int)1e9 + 3; n const long long inf = (long long)1e12; n string A, B[MAXN]; n int cost[MAXN], dp[MAXN][MAXN], last[MAXN], n, mx, sza; n pair<int, int> radix[MAXN]; n void build() { n   memset(dp, -63, sizeof dp); n   for (auto &ch : A) ch -=  0 ; n   mx++; n   reverse(A.begin(), A.end()); n   sza = A.size(); n   while ((int)A.size() < mx) A += (char)(0); n   for (int i = 0; i < n; i++) { n     last[i] = B[i].size(); n     reverse(B[i].begin(), B[i].end()); n     while ((int)B[i].size() < mx) B[i] +=  0 ; n     radix[i] = {B[i][0], i}; n   } n   sort(radix, radix + n); n   reverse(radix, radix + n); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> A >> n; n   mx = A.size(); n   for (int i = 0; i < n; i++) { n     cin >> B[i]; n     mx = max(mx, (int)B[i].size()); n   } n   for (int i = 0; i < 10; i++) cin >> cost[i]; n   build(); n   dp[0][0] = 0; n   for (int i = 0; i < mx; i++) { n     int l, r; n     if (A[i] ==  ?  -  0 ) n       l = 0, r = 9; n     else n       l = r = A[i]; n     if (i == sza - 1 && A[i] ==  ?  -  0 ) l = 1; n     for (int k = l; k <= r; k++) { n       int pt = 0, add = 0; n       for (int j = 0; j < n; j++) { n         int ind = radix[j].second; n         int nxt = B[ind][i] -  0  + k; n         if (nxt >= 10) pt++; n         if (max(last[ind], sza) > i || nxt > 0) add += cost[nxt % 10]; n       } n       for (int j = 0; j <= n; j++) { n         dp[i + 1][pt] = max(dp[i + 1][pt], dp[i][j] + add); n         if (j == n) continue; n         int ind = radix[j].second; n         int nxt = B[ind][i] -  0  + k; n         if (nxt == 9) pt++; n         if (max(last[ind], sza) > i || nxt > 0) add -= cost[nxt % 10]; n         add += cost[(nxt + 1) % 10]; n       } n     } n     reverse(radix, radix + n); n     for (int j = 0; j < n; j++) { n       int ind = radix[j].second; n       radix[j].first = n * B[ind][i] + j; n     } n     sort(radix, radix + n); n     reverse(radix, radix + n); n   } n   cout << dp[mx][0]; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   int a, b, c, i, j, cnt = 0; n   cin >> a >> b >> c; n   for (i = 0; i <= c; i++) { n     for (j = c; j >= 0; j--) { n       if ((j * a + i * b) == c) { n         cout <<  Yes  << endl; n         cnt = 1; n         break; n       } n     } n     if (cnt == 1) break; n   } n   if (cnt == 0) cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = (long long)(1e6) + 322; n const long long MAXN = (long long)(1e7) + 100; n const long long inf = (long long)(3e18) + 322; n const long long mod = (long long)(1e9) + 7; n const double eps = 1e-6; n inline void upd(long long &mn, long long x, long long y, long long z) { n   mn = min(mn, (x - y) * (x - y) + (y - z) * (y - z) + (z - x) * (z - x)); n } n void solve() { n   long long mn = inf; n   long long n1, n2, n3; n   vector<long long> v[4]; n   cin >> n1 >> n2 >> n3; n   for (long long i = 1; i <= n1; ++i) { n     long long x; n     cin >> x; n     v[0].push_back(x); n   } n   sort((v[0]).begin(), (v[0]).end()); n   for (long long i = 1; i <= n2; ++i) { n     long long x; n     cin >> x; n     v[1].push_back(x); n   } n   sort((v[1]).begin(), (v[1]).end()); n   for (long long i = 1; i <= n3; ++i) { n     long long x; n     cin >> x; n     v[2].push_back(x); n   } n   sort((v[2]).begin(), (v[2]).end()); n   vector<long long> ids; n   for (long long i = 0; i < 3; ++i) { n     ids.push_back(i); n   } n   do { n     for (long long i = 0; i < (long long)(v[ids[0]].size()); ++i) { n       long long x = v[ids[0]][i]; n       auto z = lower_bound((v[ids[2]]).begin(), (v[ids[2]]).end(), x); n       if (z == v[ids[2]].end()) continue; n       auto y = n           lower_bound((v[ids[1]]).begin(), (v[ids[1]]).end(), (x + *z) / 2); n       if (y != v[ids[1]].end()) upd(mn, x, *y, *z); n       if (y != v[ids[1]].begin()) { n         auto y2 = n             --lower_bound((v[ids[1]]).begin(), (v[ids[1]]).end(), (x + *z) / 2); n         upd(mn, x, *y2, *z); n       } n     } n   } while (next_permutation((ids).begin(), (ids).end())); n   cout << mn <<   n ; n } n signed main() { n   ios_base ::sync_with_stdio(false); n   cin.tie(0); n   long long test; n   cin >> test; n   for (long long tt = 1; tt <= test; ++tt) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n int chkmax(T &a, T b) { n   if (b > a) { n     a = b; n     return 1; n   } n   return 0; n } n template <class T> n int chkmin(T &a, T b) { n   if (b < a) { n     a = b; n     return 1; n   } n   return 0; n } n template <class iterator> n void output(iterator begin, iterator end, ostream &out = cerr) { n   while (begin != end) { n     out << (*begin) <<    ; n     begin++; n   } n   out << endl; n } n template <class T> n void output(T x, ostream &out = cerr) { n   output(x.begin(), x.end(), out); n } n void fast_io() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n } n const int MOD = 998244353; n int mul(int a, int b) { return (1LL * a * b) % MOD; } n int power(int a, int deg) { n   int res = 1; n   for (; deg; a = mul(a, a), deg >>= 1) { n     if (deg & 1) { n       res = mul(res, a); n     } n   } n   return res; n } n int inverse(int x) { return power(x, MOD - 2); } n void add(int &a, int b) { n   a += b; n   if (a >= MOD) { n     a -= MOD; n   } n } n const int mx = 2e5 + 228; n int n, fact[mx]; n long long k; n void calc_fact() { n   fact[0] = 1; n   for (int i = 1; i < mx; ++i) { n     fact[i] = mul(fact[i - 1], i); n   } n } n int C(int n, int k) { n   return (k > n ? 0 : mul(fact[n], inverse(mul(fact[k], fact[n - k])))); n } n signed main() { n   fast_io(); n   calc_fact(); n   cin >> n >> k; n   if (k >= n) { n     cout << 0 << endl; n     exit(0); n   } n   int x = n - k; n   int ans = 0; n   for (int i = 0; i <= x; ++i) { n     int sign = (i % 2 ? MOD - 1 : 1); n     add(ans, mul(sign, mul(C(x, i), power(x - i, n)))); n   } n   ans = mul(ans, C(n, x)); n   if (k) { n     ans = mul(ans, 2); n   } n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5 + 5; n const long long mod = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   string str; n   cin >> str; n   long long n = str.length(); n   vector<pair<long long, long long> > arr; n   arr.push_back(make_pair(1, str[0] -  a )); n   for (long long i = 1; i < n; i++) { n     if (str[i] == str[i - 1]) n       arr[arr.size() - 1].first++; n     else n       arr.push_back(make_pair(1, str[i] -  a )); n   } n   long long ans = 0; n   while (arr.size() > 1) { n     ans++; n     vector<pair<long long, long long> > tmp; n     for (long long i = 0; i < arr.size(); i++) { n       if (i == 0 || i == arr.size() - 1) n         arr[i].first -= 1; n       else n         arr[i].first -= 2; n       if (tmp.size() == 0 || arr[i].second != tmp[tmp.size() - 1].second) { n         if (arr[i].first > 0) tmp.push_back(arr[i]); n       } else if (arr[i].first > 0) n         tmp[tmp.size() - 1].first += arr[i].first; n     } n     arr = tmp; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n class Graph { n  public: n   vector<vector<int> > adjList; n   vector<int> indegree; n   Graph(int v) { n     adjList.resize(v); n     indegree.resize(v, 0); n   } n   void add(int u, int v) { n     adjList[u].push_back(v); n     indegree[v]++; n   } n }; n struct subset { n   int rank; n   int parent; n }; n int find(subset subsets[], int i) { n   if (subsets[i].parent != i) n     subsets[i].parent = find(subsets, subsets[i].parent); n   return subsets[i].parent; n } n void Union(subset subsets[], int x, int y) { n   int xroot = find(subsets, x); n   int yroot = find(subsets, y); n   if (subsets[xroot].rank > subsets[yroot].rank) { n     subsets[yroot].parent = xroot; n   } else if (subsets[xroot].rank < subsets[yroot].rank) { n     subsets[xroot].parent = yroot; n   } else { n     subsets[yroot].parent = xroot; n     subsets[xroot].rank++; n   } n } n bool TopologicalSort(Graph const &graph, vector<int> &ans, int v, n                      subset subsets[]) { n   vector<int> indegree = graph.indegree; n   queue<int> S; n   for (int i = 0; i < v; i++) { n     if (!indegree[i]) { n       S.push(i); n       ans[find(subsets, i)] = 1; n     } n   } n   while (!S.empty()) { n     int n = S.front(); n     S.pop(); n     for (int m : graph.adjList[n]) { n       indegree[m] -= 1; n       ans[find(subsets, m)] = ans[find(subsets, n)] + 1; n       if (!indegree[m]) { n         S.push(m); n       } n     } n   } n   for (int i = 0; i < v; i++) { n     if (indegree[i]) { n       return false; n     } n   } n   return true; n } n int main() { n   int n, m; n   cin >> n >> m; n   subset *subsets = new subset[n + m]; n   for (int v = 0; v < n + m; v++) { n     subsets[v].parent = v; n     subsets[v].rank = 0; n   } n   Graph graph(n + m); n   char input[n][m]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> input[i][j]; n       if (input[i][j] ==  = ) { n         Union(subsets, i, j + n); n       } n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       int x = find(subsets, i); n       int y = find(subsets, j + n); n       if (input[i][j] ==  < ) { n         graph.add(x, y); n       } else if (input[i][j] ==  > ) { n         graph.add(y, x); n       } n     } n   } n   vector<int> ans(n + m); n   if (TopologicalSort(graph, ans, n + m, subsets)) { n     cout <<  YES  << endl; n     for (int i = 0; i < n; i++) { n       cout << ans[find(subsets, i)] <<    ; n     } n     cout << endl; n     for (int i = n; i < m + n; i++) { n       cout << ans[find(subsets, i)] <<    ; n     } n     cout << endl; n   } else { n     cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char trash[100]; n   int n; n   cin >> n; n   cin.getline(trash, 10); n   for (int i = 0; i < n; ++i) { n     string s; n     getline(cin, s); n     bool fredas = (s.find( lala. , s.length() - 5) == s.length() - 5); n     bool rainbows = (s.find( miao. ) == 0); n     if (fredas ^ rainbows) { n       cout << (fredas ?  Freda s  :  Rainbow s ); n     } else { n       cout <<  OMG>.< I don t know! ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int SET(int N, int pos) { return N = N | (1 << pos); } n int RESET(int N, int pos) { return N = N & ~(1 << pos); } n bool CHECK(int N, int pos) { return (bool)(N & (1 << pos)); } n template <typename T> n inline T __lcm(T a, T b) { n   return (a * b) / __gcd(a, b); n } n struct node { n   long long x, y, cnt; n }; n int dx[] = {0, 1, 0, -1, -1, 1, -1, 1}; n int dy[] = {1, 0, -1, 0, 1, 1, -1, -1}; n long long a[1000000]; n long long b[1000000]; n long long c[1000000]; n void solve() { n   long long n; n   cin >> n; n   if (n % 2 == 0) { n     cout << -1 << endl; n     return; n   } n   for (long long i = 0; i < n; i++) { n     c[i] = n - i - 1; n   } n   for (long long i = 0; i < n; i++) { n     a[i] = i; n   } n   for (long long i = 0; i < n; i++) { n     b[i] = c[i] - a[i]; n     if (b[i] < 0) { n       b[i] += n; n     } n   } n   for (long long i = 0; i < n; i++) { n     cout << a[i] <<    ; n   } n   cout << endl; n   for (long long i = 0; i < n; i++) { n     cout << b[i] <<    ; n   } n   cout << endl; n   for (long long i = 0; i < n; i++) { n     cout << c[i] <<    ; n   } n   cout << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double long EPS = 1e-7; n vector<vector<long long>> precost(long long n, vector<long long> &p, n                                   vector<long long> &id) { n   vector<vector<long long>> res(n, vector<long long>(n)); n   for (int i = 0; i < n; i++) { n     vector<long long> deg(p.size()); n     for (int j = 1; j < p.size(); j++) { n       deg[p[j]]++; n     } n     long long val = 0; n     for (int j = i; j < n; j++) { n       long long cur = id[j]; n       while (cur != 0 && deg[cur] == 0) { n         val++; n         deg[p[cur]]--; n         cur = p[cur]; n       } n       res[i][j] = val; n     } n   } n   return res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<vector<long long>> adj(2); n   vector<vector<long long>> id(2, vector<long long>(n)); n   for (int i = 0; i < 2; i++) { n     long long a; n     cin >> a; n     adj[i].resize(a); n     for (int j = 1; j <= a - 1; j++) { n       cin >> adj[i][j]; n       adj[i][j]--; n     } n     for (int j = 0; j < n; j++) { n       cin >> id[i][j]; n       id[i][j]--; n     } n   } n   vector<vector<vector<long long>>> cost(2); n   for (int i = 0; i < 2; i++) { n     cost[i] = precost(n, adj[i], id[i]); n   } n   vector<long long> dp(n + 1); n   for (int i = 0; i < n; i++) { n     for (int j = i + 1; j <= n; j++) { n       dp[j] = max(dp[j], dp[i] + max(cost[0][i][j - 1], cost[1][i][j - 1])); n     } n   } n   cout << dp[n]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n long long mpow(long long x, long long y) { n   long long res = 1; n   x = x % 1000000007; n   while (y) { n     if (y & 1) { n       res *= x; n       res = res % 1000000007; n     } n     x *= x; n     x %= 1000000007; n     y >>= 1; n   } n   return res; n } n bool isprime(long long n) { n   for (long long i = 2; i * i <= n; i++) { n     if (n % i == 0) return false; n   } n   return true; n } n long long gcd(long long a, long long b) { n   if (b == 0) n     return a; n   else n     return gcd(b, a % b); n } n void solves_1() { n   long long n; n   cin >> n; n   vector<long long> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort(v.rbegin(), v.rend()); n   set<long long> st; n   long long tmp = 0; n   for (int i = 0; i < n; i++) { n     if (st.find(v[i]) != st.end()) continue; n     st.insert(v[i]); n     while (v[i] % 2 == 0) { n       v[i] /= 2; n       tmp++; n       st.insert(v[i]); n     } n   } n   cout << tmp << endl; n   return; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long t; n   cin >> t; n   while (t--) solves_1(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long mod(long long n, long long m) { n   long long ret = n % m; n   if (ret < 0) ret += m; n   return ret; n } n long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } n long long exp(long long a, long long b, long long m) { n   if (b == 0) return 1; n   if (b == 1) return mod(a, m); n   long long k = mod(exp(a, b / 2, m), m); n   if (b & 1) { n     return mod(a * mod(k * k, m), m); n   } else n     return mod(k * k, m); n } n map<vector<long long>, long long> mp; n long long a[500100]; n vector<long long> adj[500100]; n void solve() { n   mp.clear(); n   long long n, m; n   cin >> n >> m; n   for (long long i = 1; i <= n; i++) { n     adj[i].clear(); n     cin >> a[i]; n   } n   for (long long i = 0; i < m; i++) { n     long long a, b; n     cin >> a >> b; n     adj[b].push_back(a); n   } n   for (long long i = 1; i <= n; i++) { n     sort(adj[i].begin(), adj[i].end()); n     if (adj[i].size()) { n       mp[adj[i]] += a[i]; n     } n   } n   long long g = -1; n   for (map<vector<long long>, long long>::iterator it = mp.begin(); n        it != mp.end(); it++) { n     if (g == -1) n       g = it->second; n     else n       g = gcd(g, it->second); n   } n   if (g == -1) g = 0; n   cout << g <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int main() { n   cin >> n; n   n -= 10; n   if (n <= 0) n     cout << 0 << endl; n   else if (n <= 9) n     cout << 4 << endl; n   else if (n == 10) n     cout << 15 << endl; n   else if (n == 11) n     cout << 4 << endl; n   else n     cout << 0 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int p[100005], cnt, h[100005], cnth, l[100005], cntl, cntquery; n int query(int x, int y) { n   printf( 0 %d %d n , x, y); n   fflush(stdout); n   int ret; n   scanf( %d , &ret); n   cntquery++; n   if (cntquery > 300000) n     while (1) n       ; n   return ret; n } n int main() { n   int t = 0, q, X; n   for (int x = -100000000; x <= 100000000;) { n     q = query(x, x); n     if (q == 0) { n       p[++cnt] = x; n       x++; n     } else if (q < 100 && x + 100 * 2 <= 100000000 && n                query(x + 100, x + 100) >= 100) { n       X = x; n       x += 100 * 2; n     } else { n       X = x; n       x += q; n     } n   } n   for (int i = 1; i <= cnt; i++) { n     q = query(p[i], X); n     if (q == 0) l[++cntl] = p[i]; n     q = query(X, p[i]); n     if (q == 0) h[++cnth] = p[i]; n   } n   printf( 1 %d %d n , cntl, cnth); n   for (int i = 1; i <= cntl; i++) printf( %d  , l[i]); n   puts(  ); n   for (int i = 1; i <= cnth; i++) printf( %d  , h[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 150001; n struct BIT { n   int bit[maxn]; n   BIT() { memset(bit, 0, sizeof(bit)); } n   void add(int x, int v) { n     for (x++; x < maxn; x += x & -x) bit[x] += v; n   } n   void add(int a, int b, int v) { add(a, 1), add(b + 1, -1); } n   int qry(int x) { n     int ret = 0; n     for (x++; x; x -= x & -x) ret += bit[x]; n     return ret; n   } n }; n struct segTree { n   int l, r; n   segTree *left = 0, *right = 0; n   int val = 0; n   segTree(int a, int b) : l(a), r(b){}; n   void add(int x, int v) { n     if (l == r) { n       val += v; n       return; n     } n     int mid = (l + r) / 2; n     if (x <= mid) { n       if (!left) left = new segTree(l, mid); n       left->add(x, v); n     } else { n       if (!right) right = new segTree(mid + 1, r); n       right->add(x, v); n     } n     val = (left ? left->val : 0) + (right ? right->val : 0); n   } n   void mrg(segTree *tre) { n     if (!tre) return; n     if (!left) n       left = tre->left; n     else if (tre->left) n       left->mrg(tre->left); n     if (!right) n       right = tre->right; n     else if (tre->right) n       right->mrg(tre->right); n     val += tre->val; n     delete tre; n   } n   int qry(int a, int b) { n     if (a <= l && r <= b) return val; n     int ret = 0, mid = (l + r) / 2; n     if (a <= mid && b >= l && left) ret += left->qry(a, b); n     if (b > mid && a <= r && right) ret += right->qry(a, b); n     return ret; n   } n   void clear() { n     if (left) left->clear(); n     if (right) right->clear(); n     delete this; n   } n }; n const int w = 18; n int n, m, k; n int q[maxn][2]; n int p[w][maxn]; n int d[maxn], sz[maxn], h[maxn], l[maxn], r[maxn]; n vector<int> graph[maxn], v[maxn], v2[maxn]; n BIT bit; n segTree *tre[maxn]; n int dfsh(int c) { n   sz[c] = 1, h[c] = -1; n   for (int i = 1; i < w; i++) { n     p[i][c] = ~p[i - 1][c] ? p[i - 1][p[i - 1][c]] : -1; n   } n   for (int i : graph[c]) { n     if (i == p[0][c]) continue; n     p[0][i] = c; n     d[i] = d[c] + 1; n     sz[c] += dfsh(i); n     if (!~h[c] || sz[i] > sz[h[c]]) h[c] = i; n   } n   return sz[c]; n } n int dfsh2(int c) { n   r[c] = l[c]; n   for (int i : graph[c]) { n     if (i == p[0][c] || i == h[c]) continue; n     l[i] = r[c] + 1; n     r[c] = dfsh2(i); n   } n   if (~h[c]) { n     l[h[c]] = r[c] + 1; n     r[c] = dfsh2(h[c]); n   } n   return r[c]; n } n int lft(int c, int x) { n   for (int i = 0; i < w; i++) { n     if (((x >> i) & 1) && ~c) c = p[i][c]; n   } n   return c; n } n int lca(int x, int y) { n   if (d[x] < d[y]) swap(x, y); n   x = lft(x, d[x] - d[y]); n   for (int i = w - 1; ~i; i--) { n     if (p[i][x] != p[i][y]) x = p[i][x], y = p[i][y]; n   } n   return x == y ? x : p[0][x]; n } n long long dfs2(int c, int rt) { n   long long ret = 0; n   tre[c] = new segTree(0, n - 1); n   for (int i : v2[c]) { n     int dd = max(0, k + d[rt] - d[q[i][0]]); n     int j = q[i][1]; n     if (d[j] - d[rt] >= dd) { n       j = lft(j, d[j] - d[rt] - dd); n       ret += tre[c]->qry(l[j], r[j]); n     } n     tre[c]->add(l[q[i][1]], 1); n   } n   for (int i : graph[c]) { n     if (i == p[0][c] || (c == rt && i == h[c])) continue; n     ret += dfs2(i, rt); n     if (v2[c].size() < v2[i].size()) { n       swap(v2[c], v2[i]); n       swap(tre[c], tre[i]); n     } n     for (int it : v2[i]) { n       int dd = max(0, k + d[rt] - d[c]); n       int j = q[it][1]; n       if (d[j] - d[rt] >= dd) { n         j = lft(j, d[j] - d[rt] - dd); n         ret += tre[c]->qry(l[j], r[j]); n       } n       v2[c].push_back(it); n     } n     v2[i].clear(); n     tre[c]->mrg(tre[i]); n   } n   return ret; n } n long long dfs(int c) { n   long long ret = 0; n   for (int i : graph[c]) n     if (i != p[0][c]) ret += dfs(i); n   for (int t = 0; t < 2; t++) { n     for (int i : v[c]) ret += bit.qry(l[q[i][t]]); n     for (int i : v[c]) { n       int j = q[i][t]; n       if (d[j] - d[c] >= k) { n         j = lft(j, d[j] - d[c] - k); n         bit.add(l[j], r[j], 1); n       } n       if (!t) v2[q[i][t]].push_back(i); n     } n   } n   ret += dfs2(c, c); n   v2[c].clear(); n   tre[c]->clear(); n   return ret; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m >> k; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     u--, v--; n     graph[u].push_back(v); n     graph[v].push_back(u); n   } n   p[0][0] = -1; n   dfsh(0), dfsh2(0); n   for (int i = 0; i < m; i++) { n     cin >> q[i][0] >> q[i][1]; n     q[i][0]--, q[i][1]--; n     if (l[q[i][0]] > l[q[i][1]]) swap(q[i][0], q[i][1]); n     v[lca(q[i][0], q[i][1])].push_back(i); n   } n   cout << dfs(0) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int mod(int val) { return val > 0 ? val : -val; } n int main() { n   int g[2], b[2]; n   cin >> g[0] >> g[1] >> b[0] >> b[1]; n   bool pass = false; n   if (((g[1] + 1) * 2 >= b[0]) && (b[0] - g[1]) > -2) { n     pass = true; n   } n   if ((g[0] + 1) * 2 >= b[1] && (b[1] - g[0]) > -2) { n     pass = true; n   } n   if (pass) { n     cout <<  YES  << endl; n   } else { n     cout <<  NO  << endl; n   } n } n


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


#include <bits/stdc++.h> n #pragma GCC optimize(3) n using namespace std; n int n, m, a[1005][1005]; n vector<int> row[1005]; n vector<int> col[1005]; n int ans[1005][1005]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       scanf( %d , &a[i][j]); n       row[i].push_back(a[i][j]); n       col[j].push_back(a[i][j]); n     } n   for (int i = 1; i <= n; i++) { n     sort(row[i].begin(), row[i].end()); n     row[i].erase(unique(row[i].begin(), row[i].end()), row[i].end()); n   } n   for (int i = 1; i <= m; i++) { n     sort(col[i].begin(), col[i].end()); n     col[i].erase(unique(col[i].begin(), col[i].end()), col[i].end()); n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       int l1 = n           lower_bound(row[i].begin(), row[i].end(), a[i][j]) - row[i].begin(); n       int l2 = n           lower_bound(col[j].begin(), col[j].end(), a[i][j]) - col[j].begin(); n       int r1 = n           row[i].end() - upper_bound(row[i].begin(), row[i].end(), a[i][j]); n       int r2 = n           col[j].end() - upper_bound(col[j].begin(), col[j].end(), a[i][j]); n       printf( %d  , max(l1, l2) + 1 + max(r1, r2)); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define INF 200000000000000000LL n typedef long long int ll; n typedef pair<ll,ll> ii; n typedef pair<ii,ll> iii; n typedef vector<ll> vi; n typedef vector<ii> vii; n #define getbit(n,i) (((n)&(1LL<<(i)))!=0) n #define setbit0(n,i) ((n)&(~(1LL<<(i)))) n #define setbit1(n,i) ((n)|(1LL<<(i))) n #define lastone(n) ((n)&(-(n))) n #define read freopen( debug//in.txt , r ,stdin) n #define write freopen( debug//out.txt , w ,stdout) n #define DBG(a) cerr<<#a<<  ->->->->  <<a<<  n  n #define fi first n #define se second n #define PI (acos(-1)) n #define fastread ios::sync_with_stdio(false);cin.tie(NULL) n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n #define mod (1000000007) n #define asz 500005 n template<class T,class V> ostream& operator<<(ostream &s,pair<T,V> a) n { n     s<<a.fi<<   <<a.se; n     return s; n } n const int N = 1e3+5; n int main() n { n     fastread; n #ifdef FILE_IO n     read; n     write; n #endif n  n     int T=1; n     cin>>T; n     for(int qq=1; qq<=T; qq++) n     { n         int n; n         string s; n         cin>>n>>s; n         vi l(n+2),r(n+2); n         r[n] = 1; n         r[n-1] = 1 + (s.back() ==  R ); n         l[0] = 1; n         l[1] = 1 + (s[0] ==  L ); n         for(int i=2;i<=n;i++){ n             if(s[i-1] ==  L ){ n                 if(s[i-2] ==  R )l[i] = 2+l[i-2]; n                 else l[i] = 2; n             } n             else l[i] = 1; n  n             if(s[n-i] ==  R ){ n                 if(s[n-i+1] ==  L )r[n-i] = 2+r[n-i+2]; n                 else r[n-i] = 2; n             } n             else r[n-i] = 1; n         } n  n         for(int i=0;i<=n;i++)cout<<l[i]+r[i]-1<<   ; n         cout<<endl; n     } n } n  n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007; n long long qpow(long long x, long long tms) { n   long long tmp = 1; n   while (tms) { n     if (tms & 1) tmp = tmp * x % mod; n     tms >>= 1; n     x = x * x % mod; n   } n   return tmp; n } n int n, m, T; n bool chk(int k) { n   if (n == 1) return 1; n   long long sum = 1, t = (long long)k * m; n   double C = 1; n   long long s = 1; n   for (int i = 1; i <= k; ++i) { n     long long x = t / i, y = mod; n     if (x == 0) return 0; n     C = C * (k - i + 1) / i; n     s = s * (k - i + 1) % mod * qpow(i, mod - 2) % mod; n     if (C < mod) y = s; n     x = min(x, y); n     sum += x; n     if (sum >= n) return 1; n     t -= x * i; n   } n   return 0; n } n int main() { n   scanf( %d , &T); n   while (T--) { n     scanf( %d%d , &n, &m); n     int l = 0, r = n; n     while (l < r) { n       int mid = (l + r) >> 1; n       if (chk(mid)) n         r = mid; n       else n         l = mid + 1; n     } n     printf( %d n , l); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   for (int i = 0; i < t; i++) { n     int cont = 0; n     int tamano; n     scanf( %d , &tamano); n     string x; n     cin >> x; n     for (int j = 0; j < tamano / 2; j++) { n       if (x[j] == x[tamano - j - 1]) { n         cont++; n       } else if ((x[j] + 1 == x[tamano - j - 1] + 1) || n                  (x[j] + 1 == x[tamano - j - 1] - 1) || n                  (x[j] - 1 == x[tamano - j - 1] - 1) || n                  (x[j] - 1 == x[tamano - j - 1] + 1)) { n         cont++; n       } else { n         printf( NO n ); n         break; n       } n     } n     if (cont == tamano / 2) { n       printf( YES n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[109], d; n void nhap() { n   scanf( %d n , &n); n   for (int i = 1; i <= n; i++) scanf( %d  , &a[i]); n } n void qs(int l, int r) { n   int x = a[(l + r) / 2]; n   int i = l; n   int j = r; n   while (i <= j) { n     while (a[i] < x) i++; n     while (a[j] > x) j--; n     if (i <= j) { n       int tmp = a[i]; n       a[i] = a[j]; n       a[j] = tmp; n       i++; n       j--; n     } n   } n   if (i < r) qs(i, r); n   if (j > l) qs(l, j); n } n void xuli() { n   qs(1, n); n   for (int i = 2; i <= n; i += 2) { n     int s = a[i] - a[i - 1]; n     if (s != 0) d = d + s; n   } n   printf( %d , d); n } n int main() { n   nhap(); n   xuli(); n } n


#include <bits/stdc++.h> n int max(int x, int y) { return (x > y ? x : y); } n int geGcd(int a, int b) { n   if (a == 0 || b == 0) return 0; n   for (int i = max(a, b); i >= 1; i--) n     if (a % i == 0 && b % i == 0) return i; n } n using namespace std; n int main() { n   int a, b, n; n   cin >> a >> b >> n; n   int cnt = 0; n   while (n > 0) { n     if (geGcd(a, n)) { n       n -= geGcd(a, n); n       cnt++; n     } n     if (geGcd(b, n)) { n       n -= geGcd(b, n); n       cnt++; n     } n   } n   if (cnt % 2 == 0) n     cout <<  1 ; n   else n     cout <<  0 ; n } n


#include <bits/stdc++.h> n using namespace std; n long long lv[100001], lf[100001], par[100001], n; n vector<long long> con[100001], cld[100001]; n long long get_leaf(long long id) { n   if (cld[id].size() == 0) { n     lf[1]++; n     return 1; n   } n   long long x = 0, i; n   for (i = 0; i <= cld[id].size() - 1; i++) { n     x += get_leaf(cld[id][i]); n   } n   lf[x]++; n   return x; n } n int main() { n   ios_base::sync_with_stdio(0); n   long long int i, j, x, y, m, cnt = 0, k, g, flg, t, l, r, md; n   cin >> n; n   for (i = 2; i <= n; i++) { n     cin >> x; n     con[x].push_back(i); n     con[i].push_back(x); n   } n   queue<long long> q; n   q.push(1); n   while (!q.empty()) { n     x = q.front(); n     q.pop(); n     if (con[x].size() == 0) continue; n     for (i = 0; i <= con[x].size() - 1; i++) { n       y = con[x][i]; n       if (par[y] || y == 1) continue; n       par[y] = x; n       cld[x].push_back(y); n       q.push(y); n     } n   } n   x = get_leaf(1); n   for (i = 2; i <= n; i++) { n     lf[i] += lf[i - 1]; n   } n   for (i = 1; i <= n; i++) { n     l = 0; n     r = n; n     while (r - l > 1) { n       md = (l + r) / 2; n       if (lf[md] < i) n         l = md; n       else n         r = md; n     } n     cout << r <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   string s; n   cin >> s; n   int c = 0, f = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  x ) n       c++; n     else if (s[i] !=  x  && c < 3) n       c = 0; n     if (c > 2) { n       while (1) { n         f++; n         c--; n         if (c < 3) break; n       } n     } n   } n   cout << f; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   if (!b) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return b / gcd(a, b) * a; } n int n, g[5001]; n int s; n int l[5000][5001]; n int best = 1 << 30, freq; n int SWAP(int a, int b) { n   int B = g[b]; n   int A = g[a]; n   int res = s; n   res -= (b - a - 2) - (l[B][b] - l[B][a]); n   res += (l[B][b] - l[B][a]); n   res -= (l[A][b] - l[A][a]); n   res += (b - a - 2) - (l[A][b] - l[A][a]); n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , g + i); n     for (int j = i - 1; j; --j) s += g[i] < g[j]; n   } n   for (int(i) = 0; i < int(n); ++i) { n     l[i][1] = 0; n     for (int j = 2; j <= n; ++j) l[i][j] = l[i][j - 1] + (i > g[j - 1]); n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = i + 1; j <= n; ++j) { n       int cost = SWAP(i, j); n       if (g[i] < g[j]) n         ++cost; n       else n         --cost; n       if (cost == best) n         ++freq; n       else if (cost < best) { n         best = cost; n         freq = 1; n       } n     } n   } n   cout << best <<     << freq << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int ucln(int a, int b) { n   int r; n   while (b > 0) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n int main() { n   int x, y, a, b, bc; n   cin >> x >> y >> a >> b; n   bc = x * y / ucln(x, y); n   int l = a, r = b; n   while (l % bc != 0) l++; n   while (r % bc != 0) r--; n   cout << (r - l) / bc + 1; n } n


#include <bits/stdc++.h> n using namespace std; n struct point { n   long long x, y, index; n   point(long long x, long long y, long long index) : x(x), y(y), index(index) {} n   long long operator*(const point p) { return x * p.x + y * p.y; } n   long long operator^(const point p) { return x * p.y - y * p.x; } n   bool operator<(const point p) const { n     return make_pair(x, y) < make_pair(p.x, p.y); n   } n   bool operator==(const point p) const { return x == p.x && y == p.y; } n   bool operator!=(const point p) const { return !(*this == p); } n   point operator*(long long scale) { n     return point(scale * x, scale * y, index); n   } n   point operator+(const point p) { return point(x + p.x, y + p.y, index); } n   point operator-(const point p) { return point(x - p.x, y - p.y, index); } n   long long norm() { return (*this) * (*this); } n }; n bool half(point p) { n   assert(p.x != 0 || p.y != 0); n   return p.x > 0 || (p.x == 0 && p.y > 0); n } n void polarSort(point o, vector<point> &v, int L, int R) { n   sort(v.begin() + L, v.begin() + R, [&](point v, point w) { n     return make_tuple(half(v - o), 0, (v - o).norm()) < n            make_tuple(half(w - o), ((v - o) ^ (w - o)), (w - o).norm()); n   }); n } n const int N = 1515; n int n; n int Ans[N]; n int Sub[N]; n vector<point> pts; n vector<int> Tree[N]; n int DFS(int u, int p) { n   Sub[u] = 1; n   for (auto v : Tree[u]) n     if (v != p) Sub[u] += DFS(v, u); n   return Sub[u]; n } n void PaintTree(int u, int p, int L, int R) { n   int idx = L; n   for (int i = L; i <= R; i++) n     if (pts[idx].y < pts[i].y) idx = i; n   swap(pts[L], pts[idx]); n   Ans[pts[L].index] = u; n   polarSort(pts[L], pts, L + 1, R + 1); n   int pre = 0; n   for (auto v : Tree[u]) n     if (v != p) { n       PaintTree(v, u, L + pre + 1, L + pre + Sub[v]); n       pre += Sub[v]; n     } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     Tree[u].push_back(v); n     Tree[v].push_back(u); n   } n   for (int i = 0; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     pts.push_back(point(x, y, i + 1)); n   } n   DFS(1, 0); n   PaintTree(1, 0, 0, n - 1); n   for (int i = 1; i <= n; i++) printf( %d  , Ans[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using u64 = unsigned long long; n const int mod = 1e9 + 7, N = 2e3 + 10; n char mp[N][N]; n int f[N][N]; n bool st[N][N]; n unordered_map<char, pair<int, int>> mv = { n     { L , {0, -1}}, { R , {0, 1}}, { U , {-1, 0}}, { D , {1, 0}}}; n int main(void) { n   int t; n   cin >> t; n   while (t--) { n     int n, m, tx, ty; n     cin >> n >> m; n     for (int i = 1; i <= n; i++) scanf( %s , &mp[i][1]); n     stack<pair<int, int>> q; n     for (int i = 0; i <= n + 1; i++) n       for (int j = 0; j <= m + 1; j++) f[i][j] = 1; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         int x = i, y = j; n         bool ring = false; n         if (f[x][y] != 1) continue; n         do { n           q.push({x, y}); n           st[x][y] = true; n           tx = x + mv[mp[x][y]].first, ty = y + mv[mp[x][y]].second; n           x = tx, y = ty; n           if (x < 1 || x > n || y < 1 || y > m) break; n           if (st[x][y]) { n             ring = true; n             break; n           } n           if (f[x][y] != 1) { n             q.push({x, y}); n             break; n           } n         } while (true); n         pair<int, int> xy; n         int step; n         if (ring) { n           stack<pair<int, int>> rq; n           do { n             xy = q.top(); n             q.pop(); n             st[xy.first][xy.second] = false; n             rq.push(xy); n             if (xy.first == x && xy.second == y) break; n           } while (true); n           step = rq.size(); n           while (rq.size()) { n             xy = rq.top(); n             rq.pop(); n             f[xy.first][xy.second] = step; n           } n           step++; n           while (q.size()) { n             xy = q.top(); n             q.pop(); n             st[xy.first][xy.second] = false; n             f[xy.first][xy.second] = step++; n           } n           continue; n         } n         step = f[q.top().first][q.top().second]; n         while (q.size()) { n           xy = q.top(); n           q.pop(); n           st[xy.first][xy.second] = false; n           f[xy.first][xy.second] = step++; n         } n       } n     } n     int res = -1, x, y; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         if (res < f[i][j]) { n           x = i, y = j; n           res = f[i][j]; n         } n       } n     } n     cout << x <<     << y <<     << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:36777216 ) n using namespace std; n const int INF = ~(1 << 31); n const double EPS = 1; n const double PI = 3.141592653589793; n int main() { n   int n, m; n   cin >> n >> m; n   if (m == 3 && n >= 5) { n     cout <<  -1 n ; n     return 0; n   } n   for (int i = 0; i < m; i++) cout << i <<     << i * i + 10000000 <<   n ; n   for (int i = 0; i < n - m; i++) cout << i <<     << -i * i - 10000000 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, x, p[100005], h[100005], cnt; n long long n; n int ksm(int a, long long k) { n   if (!k) return 1; n   int p = ksm(a, k / 2); n   if (k & 1) return (long long)p * p % 1000000007 * a % 1000000007; n   return (long long)p * p % 1000000007; n } n int Getval(long long n, int x) { n   int ret = 1; n   while (n) { n     n /= x; n     ret = (long long)ret * ksm(x, n) % 1000000007; n   } n   return ret; n } n int main() { n   scanf( %d%I64d , &x, &n); n   int m = int(sqrt(x)) + 1, ans = 1; n   for (int i = 2; i <= m; i++) { n     if (x % i == 0) { n       while (x % i == 0) x /= i; n       ans = (long long)ans * Getval(n, i) % 1000000007; n     } n   } n   if (x > 1) ans = (long long)ans * Getval(n, x) % 1000000007; n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x0f0f0f0f; n template <class S, class T> n inline pair<S, T> MP(S a, T b) { n   return make_pair(a, b); n } n template <class T> n inline auto LX(T &X) -> decltype((X.first)) { n   return X.first; n } n template <class T> n inline auto LX(const T &X) -> const decltype((X.first)) { n   return X.first; n } n template <class T> n inline auto RX(T &X) -> decltype((X.second)) { n   return X.second; n } n template <class T> n inline auto RX(const T &X) -> const decltype((X.second)) { n   return X.second; n } n template <class T> n inline void RST(T &a) { n   memset(a, 0, sizeof(a)); n } n template <class T> n inline void RST1(T &a) { n   memset(a, -1, sizeof(a)); n } n template <class T> n inline void SINF(T &a) { n   memset(a, 0x0f, sizeof(a)); n } n template <class S, class T> n inline void RST(S &a, T b) { n   fill((T *)a, (T *)a + (sizeof(a) / sizeof(b)), b); n } n template <class T> n inline void CLR(T &a) { n   a.clear(); n } n template <class S, class T> n inline void CPY(S &a, T &b) { n   memcpy(a, b, sizeof(a)); n } n template <class S, class T> n inline bool chkmin(S &a, T b) { n   return b < a ? a = b, 1 : 0; n } n template <class S, class T> n inline bool chkmax(S &a, T b) { n   return a < b ? a = b, 1 : 0; n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <class T> n inline int SZ(T &a) { n   return (int)(a.size()); n } n template <class T> n inline bool EPT(T &a) { n   return a.empty(); n } n template <class T> n inline auto FRT(T &a) -> decltype((a.front())) { n   return a.front(); n } n template <class T> n inline auto FRT(const T &a) -> const decltype((a.front())) { n   return a.front(); n } n template <class T> n inline auto BK(T &a) -> decltype(a.back()) & { n   return a.back(); n } n template <class T> n inline auto BK(const T &a) -> const decltype((a.back())) { n   return a.back(); n } n template <class T> n inline auto TOP(T &a) -> decltype((a.top())) { n   return a.top(); n } n template <class T> n inline auto TOP(const T &a) -> const decltype((a.top())) { n   return a.top(); n } n int n, m, nn, k; n int a[310][310], b[310][310]; n int r[200010], cr[200010], sz[200010]; n vector<pair<int, int> > l[200010]; n unordered_map<int, int> c; n int find(int x) { return x == r[x] ? x : r[x] = find(r[x]); } n int d[1010][1010]; n deque<pair<int, int> > Q; n int ox, oy; n bool cmp(const pair<int, int> &a, const pair<int, int> &b) { n   return d[LX(a) - ox + k][RX(a) - oy + k] < d[LX(b) - ox + k][RX(b) - oy + k]; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       int &x = a[i][j]; n       scanf( %d , &x); n       if (x) n         if (!c.count(x)) n           x = c[x] = ++nn; n         else n           x = c[x]; n       sz[x]++; n     } n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       int &x = b[i][j]; n       scanf( %d , &x); n       if (x != -1) { n         if (x) n           if (!c.count(x)) n             x = c[x] = ++nn; n           else n             x = c[x]; n         l[a[i][j]].push_back(MP(i, j)); n       } n     } n   for (int i = 0; i <= nn; i++) r[i] = cr[i] = i; n   k = max(n, m); n   int x = k, y = k, t = 0; n   for (int i = 1; i < k; i++) { n     d[--x][y] = ++t; n     for (int j = 1; j < 2 * i; j++) d[x][++y] = ++t; n     for (int j = 0; j < 2 * i; j++) d[++x][y] = ++t; n     for (int j = 0; j < 2 * i; j++) d[x][--y] = ++t; n     for (int j = 0; j < 2 * i; j++) d[--x][y] = ++t; n   } n   scanf( %d%d , &x, &y); n   Q.push_back(MP(x, y)); n   long long ans = 0; n   while (!EPT(Q)) { n     int x = LX(FRT(Q)), y = RX(FRT(Q)); n     Q.pop_front(); n     int c1 = find(a[x][y]), c2 = find(b[x][y]); n     if (!cr[c1] || cr[c1] == b[x][y]) continue; n     ox = x, oy = y; n     sort(l[c1].begin(), l[c1].end(), cmp); n     for (pair<int, int> t : l[c1]) Q.push_back(t); n     CLR(l[c1]); n     ans += sz[c1]; n     if (c1 != c2) sz[c2] += sz[c1], r[c1] = c2; n     cr[c2] = b[x][y]; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int it = 0; n int find(int l, int r, int k) { n   if (l == r) return l; n   int ras = (r - l + 1) / 2; n   int ll, rr; n   if (k == 0) { n     ll = l + ras - 1; n     rr = l + ras; n   } else { n     ll = r - ras; n     rr = ll + 1; n   } n   if (k == 0) { n     assert(it != 60); n     cout << 1 <<     << ll <<     << rr << endl; n     ++it; n   } else { n     assert(it != 60); n     cout << 1 <<     << rr <<     << ll << endl; n     ++it; n   } n   string s; n   cin >> s; n   if (s[0] ==  T ) { n     if (k == 0) { n       return find(l, rr - 1, k); n     } else { n       return find(ll + 1, r, k); n     } n   } else { n     if (k == 0) { n       return find(ll + 1, r, k); n     } else { n       return find(l, rr - 1, k); n     } n   } n } n signed main() { n   int n, k; n   cin >> n >> k; n   int cur = find(1, n, 0); n   set<int> sss; n   sss.insert(cur); n   sss.insert(find(1, cur, 0)); n   sss.insert(find(cur, n, 1)); n   cout << 2 <<     << *sss.begin() <<     << *(++sss.begin()) << endl; n   return 0; n } n


#include <bits/stdc++.h> n unsigned long long int p[64][2][2], m, pi[64]; n void mul_matrix(int size, unsigned long long int (*a)[2], n                 unsigned long long int (*b)[2], n                 unsigned long long int (*dest)[2]) { n   int i, j, k; n   for (i = 0; i < size; i++) n     for (j = 0; j < 2; j++) { n       dest[i][j] = 0; n       for (k = 0; k < 2; k++) n         dest[i][j] = (dest[i][j] + (a[i][k] * b[k][j]) % m) % m; n     } n } n void init_mat() { n   int i; n   p[0][0][0] = p[0][0][1] = p[0][1][0] = 1; n   p[0][1][1] = 0; n   pi[0] = 2; n   for (i = 1; i < 64; i++) { n     mul_matrix(2, p[i - 1], p[i - 1], p[i]); n     pi[i] = (pi[i - 1] * pi[i - 1]) % m; n   } n } n int main() { n   unsigned long long int n, k, t, zero = 0, one = 1, ans = 1, l, i; n   unsigned long long int base1[1][2] = {0, 1}, base2[1][2] = {1, 0}, dest[1][2]; n   scanf( %lld %lld %lld %lld , &n, &k, &l, &m); n   init_mat(); n   for (i = 0, t = n - 1; t; t >>= 1, i++) { n     if (t & 1) { n       mul_matrix(1, base1, p[i], dest); n       memcpy(base1, dest, sizeof(dest)); n       mul_matrix(1, base2, p[i], dest); n       memcpy(base2, dest, sizeof(dest)); n     } n   } n   for (i = 0, t = n; t; t >>= 1, i++) n     if (t & 1) one = (one * pi[i]) % m; n   zero = n       ((base1[0][0] + base1[0][1]) % m + (base2[0][0] + base2[0][1]) % m) % m; n   one = ((one + m) - zero) % m; n   for (t = k, i = 0; i < l; t >>= 1, i++) { n     if (t & 1) n       ans = (ans * one) % m; n     else n       ans = (ans * zero) % m; n   } n   if (t) { n     printf( 0 ); n     return 0; n   } n   printf( %u , ans % m); n   return 0; n } n


#include <bits/stdc++.h> n int inp() { n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   int sum = 0; n   while (c >=  0  && c <=  9 ) { n     sum = sum * 10 + c -  0 ; n     c = getchar(); n   } n   return sum; n } n char s[1000010], l[1000010], r[1000010]; n long long f[1000010], sum[1000010]; n int n, sl, sr, pl[1000010], pr[1000010], hshs[1000010], hshl[1000010], n     hshr[1000010], fpow[1000010]; n bool ok1(int x) { n   if (x + sl - 1 > n) return false; n   if (sl == pl[x]) return true; n   return l[pl[x] + 1] < s[x + pl[x]]; n } n bool ok2(int x) { n   if (x + sr - 1 > n) return false; n   if (pr[x] == sr) return true; n   return r[pr[x] + 1] > s[x + pr[x]]; n } n int get_hsh(int *hsh, int l, int r) { n   return (hsh[r] - n           ((long long)(hsh[l - 1]) * (long long)(fpow[r - l + 1]) % 998244353) + n           998244353) % n          998244353; n } n void solve(int *s, int *hsh, int len) { n   for (int i = 1; i <= n - len + 1; i++) { n     int l = 0; n     int r = len; n     while (l < r) { n       int mid = (l + r + 1) >> 1; n       if (get_hsh(hshs, i, i + mid - 1) == get_hsh(hsh, 1, mid)) n         l = mid; n       else n         r = mid - 1; n     } n     s[i] = l; n   } n } n void gethash(char *str, int *hsh, int len) { n   for (int i = 1; i <= len; i++) n     hsh[i] = ((long long)(hsh[i - 1]) * (long long)(11) + str[i] -  0  + 1) % n              998244353; n } n int main() { n   fpow[0] = 1; n   for (int i = 1; i <= 1000000; i++) n     fpow[i] = ((long long)(fpow[i - 1]) * (long long)(11) % 998244353); n   scanf( %s , s + 1); n   scanf( %s , l + 1); n   scanf( %s , r + 1); n   n = strlen(s + 1); n   sl = strlen(l + 1); n   sr = strlen(r + 1); n   gethash(l, hshl, sl); n   gethash(r, hshr, sr); n   gethash(s, hshs, n); n   f[0] = 1; n   solve(pl, hshl, sl); n   solve(pr, hshr, sr); n   for (int i = 0; i <= n; i++) { n     if (i) { n       sum[i] += sum[i - 1]; n       f[i] += sum[i]; n       f[i] %= 998244353; n     } n     if (s[i + 1] ==  0 ) { n       if (sl == 1 && l[1] ==  0 ) { n         f[i + 1] += f[i]; n         f[i + 1] %= 998244353; n       } n       continue; n     } n     if (sl < sr) { n       sum[sl + i + 1] += f[i]; n       sum[sl + i + 1] %= 998244353; n       sum[sr + i] += 998244353 - f[i]; n       sum[sr + i] %= 998244353; n     } n     if (sl == sr) { n       if (ok1(i + 1) && ok2(i + 1)) { n         f[i + sl] += f[i]; n         f[i + sl] %= 998244353; n       } n     } else { n       if (ok1(i + 1)) { n         f[i + sl] += f[i]; n         f[i + sl] %= 998244353; n       } n       if (ok2(i + 1)) { n         f[i + sr] += f[i]; n         f[i + sr] %= 998244353; n       } n     } n   } n   std::cout << f[n] << std::endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int t; n   t = 1; n   while (t--) { n     int n; n     cin >> n; n     long long arr[n], c4 = 0, c2 = 0; n     long long i; n     map<long long, long long> make_pair; n     for (i = 0; i < n; i++) { n       cin >> arr[i]; n       c4 -= make_pair[arr[i]] / 4; n       c2 -= make_pair[arr[i]] / 2; n       make_pair[arr[i]]++; n       c4 += make_pair[arr[i]] / 4; n       c2 += make_pair[arr[i]] / 2; n     } n     long long q; n     cin >> q; n     for (i = 0; i < q; i++) { n       char c; n       long long x; n       cin >> c >> x; n       c4 -= make_pair[x] / 4; n       c2 -= make_pair[x] / 2; n       if (c ==  + ) { n         make_pair[x]++; n       } else { n         make_pair[x]--; n       } n       c4 += make_pair[x] / 4; n       c2 += make_pair[x] / 2; n       if (c4 >= 1 && c2 >= 4) { n         cout <<  YES  << endl; n       } else { n         cout <<  NO  << endl; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 505; n int l[MAXN], r[MAXN], s[MAXN], w[MAXN], v[MAXN]; n int dp[2 * MAXN][MAXN]; n vector<int> wr[2 * MAXN]; n int calc(int str, int i) { n   if (dp[str][i] != -1) return dp[str][i]; n   int L = l[i], R = r[i]; n   vector<int> prf(2 * MAXN); n   prf[L] = 0; n   for (int x = L + 1; x <= R; x++) { n     prf[x] = prf[x - 1]; n     for (auto j : wr[x]) { n       if (l[j] == L && r[j] == R) continue; n       if (l[j] < L || w[j] > str) continue; n       prf[x] = max(prf[x], calc(min(str - w[j], s[j]), j) + prf[l[j]]); n     } n   } n   return dp[str][i] = v[i] + prf[R]; n } n int main() { n   int n, S; n   cin >> n >> S; n   l[0] = 0; n   r[0] = 2 * n; n   s[0] = S; n   w[0] = 0; n   v[0] = 0; n   for (int i = 1; i <= n; i++) { n     cin >> l[i] >> r[i] >> w[i] >> s[i] >> v[i]; n     wr[r[i]].push_back(i); n   } n   for (int i = 0; i < 2 * MAXN; i++) n     for (int j = 0; j < MAXN; j++) dp[i][j] = -1; n   cout << calc(S, 0) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n void read(long long &x) { n   x = 0; n   long long f = 1; n   char ch = getchar(); n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) f = -f; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   x *= f; n } n void print(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (!x) return; n   print(x / 10), putchar(x % 10 + 48); n } n void write(long long x) { n   if (!x) n     putchar( 0 ); n   else n     print(x); n   putchar(  n ); n } n const long long maxn = 2e5 + 10; n const long long inf = 1e9; n const long double eps = 1e-12; n const long long mod = 1e9 + 7; n long long add(long long x, long long y) { n   return x + y >= mod ? x + y - mod : x + y; n } n long long del(long long x, long long y) { n   return x - y < 0 ? x - y + mod : x - y; n } n long long mul(long long x, long long y) { n   return 1ll * x * y - 1ll * x * y / mod * mod; n } n void inc(long long &x, long long y) { n   x += y; n   x %= mod; n } n long long n, k, a[maxn], res, c; n long long qpow(long long a, long long x) { n   long long res = 1; n   for (; x; x >>= 1, a = 1ll * a * a % mod) n     if (x & 1) res = 1ll * res * a % mod; n   return res; n } n struct Matrix { n   long long a, b, r[110][110]; n   Matrix() { a = b = 0, memset(r, 0, sizeof r); } n   Matrix operator*(const Matrix &t) const { n     Matrix res; n     res.a = a, res.b = t.b; n     for (long long i = 0; i <= a; i++) n       for (long long j = 0; j <= t.b; j++) n         for (long long k = 0; k <= b; k++) n           res.r[i][j] = add(res.r[i][j], mul(r[i][k], t.r[k][j])); n     return res; n   } n } tr, ans; n signed main() { n   read(n), read(k); n   for (long long i = 1; i <= n; i++) read(a[i]), res += !(a[i] & 1); n   for (long long i = 1; i <= res; i++) c += !a[i]; n   ans.a = 0, ans.b = res; n   ans.r[0][c] = 1; n   tr.a = tr.b = res; n   for (long long i = 0; i <= res; i++) { n     long long lw = i, lb = res - i, rw = res - lw, rb = n - res - lb; n     if (i != res) inc(tr.r[i][i + 1], lb * rw % mod); n     if (i != 0) inc(tr.r[i][i - 1], lw * rb % mod); n     inc(tr.r[i][i], (res * (res - 1) + (n - res) * (n - res - 1)) % mod * n                         qpow(2, mod - 2) % mod); n     inc(tr.r[i][i], (lw * lb + rw * rb) % mod); n   } n   long long x = k, f = 0; n   for (; x; x >>= 1, tr = tr * tr) n     if (x & 1) ans = ans * tr; n   for (long long i = 0, i_r = res; i <= i_r; i++) f = (f + ans.r[0][i]) % mod; n   write(ans.r[0][res] * qpow(f, mod - 2) % mod); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const long long INF = 1e17; n vector<vector<long long>> g; n vector<long long> used; n void dfs(long long v) { n   used[v] = 1; n   for (auto u : g[v]) { n     if (!used[u]) { n       dfs(u); n     } n   } n } n void solve() { n   long long n, m; n   cin >> n >> m; n   long long q; n   cin >> q; n   long long N = n + m; n   g.resize(N); n   used.resize(N); n   while (q--) { n     long long x, y; n     cin >> x >> y; n     --x, --y; n     x += m; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   long long ans = -1; n   for (long long i = 0; i < N; ++i) { n     if (!used[i]) { n       dfs(i); n       ans++; n     } n   } n   cout << ans; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long tt = 1; n   while (tt--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int t; n   cin >> t; n   for (int z = 0; z < t; ++z) { n     int n; n     string s; n     cin >> n >> s; n     string ans =   ; n     for (int i = 0; i < n; ++i) { n       if ((s[i] -  0 ) % 2 == 1) ans += s[i]; n       if (ans.size() == 2) break; n     } n     if (ans.size() < 2) { n       cout <<  -1 n ; n     } else { n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = (int)1e5 + 100; n vector<int> g[maxn], G[maxn]; n int type[maxn]; n int cntIn[maxn]; n void no() { n   printf( No ); n   exit(0); n } n void yes() { n   printf( Yes ); n   exit(0); n } n void dfsG(int v, int par = -1) { n   if ((int)G[v].size() > 2) no(); n   type[v] = 3; n   for (int i = 0; i < (int)G[v].size(); i++) { n     int nv = G[v][i]; n     if (nv == par) continue; n     dfsG(nv, v); n     break; n   } n } n void solve() { n   int n; n   scanf( %d , &n); n   if (n <= 2) yes(); n   for (int i = 0; i < n - 1; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     a--; n     b--; n     g[a].push_back(b); n     g[b].push_back(a); n   } n   bool fnd = false; n   for (int i = 0; i < n; i++) n     if ((int)g[i].size() >= 3) fnd = true; n   if (!fnd) yes(); n   for (int i = 0; i < n; i++) n     if ((int)g[i].size() == 1 && type[i] == 0) { n       type[i] = 1; n       int pv = i; n       int v = g[i][0]; n       while ((int)g[v].size() == 2) { n         type[v] = 1; n         int nv = g[v][0] + g[v][1] - pv; n         pv = v; n         v = nv; n       } n       cntIn[v]++; n     } n   for (int i = 0; i < n; i++) n     if ((int)g[i].size() == 3 && cntIn[i] == 2) { n       type[i] = 2; n     } n   for (int i = 0; i < n; i++) n     for (int j = 0; j < (int)g[i].size(); j++) { n       int ni = g[i][j]; n       if (type[i] == 0 && type[ni] == 0) G[i].push_back(ni); n     } n   int st = -1; n   for (int i = 0; i < n; i++) n     if (type[i] == 0 && (int)G[i].size() <= 2) st = i; n   if (st != -1) { n     type[st] = 3; n     for (int i = 0; i < (int)G[st].size(); i++) dfsG(G[st][i], st); n   } n   for (int i = 0; i < n; i++) n     if (type[i] == 0) no(); n   yes(); n } n int main(int, char **) { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 77; n int main() { n   int a, b; n   cin >> a >> b; n   int ans = 0; n   while (1) { n     if (a <= 0 || b <= 0) break; n     if (a < b) { n       a++; n       b -= 2; n     } else { n       b++; n       a -= 2; n     } n     if (a < 0 || b < 0) break; n     ++ans; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[1000005]; n int main() { n   int n, q, a, b, x = 0, xPar = 0; n   scanf( %d %d , &n, &q); n   while (q--) { n     scanf( %d , &a); n     if (a == 1) { n       scanf( %d , &b); n       x += b; n       if (x >= n) { n         x = x % n; n       } else if (x < 0) { n         x = n - ((x * -1) % n); n       } n     } else { n       if ((x + xPar) % 2 == 1) { n         xPar = ((xPar + 1) % n); n       } else { n         if (--xPar < 0) { n           xPar += n; n         } n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     int nTemp = i + x; n     if (i % 2) { n       nTemp -= xPar; n     } else { n       nTemp += xPar; n     } n     while (nTemp <= 0) { n       nTemp += n; n     } n     while (nTemp > n) { n       nTemp -= n; n     } n     arr[nTemp] = i; n   } n   for (int i = 1; i <= n; i++) { n     cout << arr[i]; n     if (i != n) { n       cout <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = 1e18; n struct query { n   long long k, pos, ind; n }; n struct fenwick { n   long long n; n   vector<long long> f; n   fenwick(long long _n) { n     n = _n; n     f.resize(n, 0); n   } n   void add(long long val, long long i) { n     for (; i < n; i |= (i + 1)) f[i] += val; n   } n   long long sum(long long i) { n     long long ans = 0; n     for (; i >= 0; i &= (i + 1), --i) ans += f[i]; n     return ans; n   } n }; n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<pair<long long, long long> > a(n); n   vector<long long> start(n); n   for (long long i = 0; i < n; ++i) { n     cin >> a[i].first; n     start[i] = a[i].first; n     a[i].second = i; n   } n   sort(a.begin(), a.end(), n        [](pair<long long, long long> a, pair<long long, long long> b) { n          if (a.first == b.first) return a.second > b.second; n          return a.first < b.first; n        }); n   reverse(a.begin(), a.end()); n   long long q; n   cin >> q; n   vector<query> qs(q); n   for (long long t = 0; t < q; ++t) { n     cin >> qs[t].k >> qs[t].pos; n     qs[t].k--; n     qs[t].pos--; n     qs[t].ind = t; n   } n   sort(qs.begin(), qs.end(), [](query a, query b) { return a.k < b.k; }); n   fenwick f(n); n   long long l = -1; n   vector<long long> ans(q); n   for (long long i = 0; i < q; ++i) { n     while (l < qs[i].k) { n       ++l; n       f.add(1, a[l].second); n     } n     long long L = 0, R = n - 1; n     while (R - L > 1) { n       long long m = (R + L) / 2; n       if (f.sum(m) <= qs[i].pos) { n         L = m; n       } else n         R = m; n     } n     if (f.sum(L) < qs[i].pos + 1) swap(L, R); n     ans[qs[i].ind] = start[L]; n   } n   for (auto x : ans) cout << x <<   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   cout.tie(0); n   string x, y; n   cin >> x >> y; n   int n, m; n   n = x.size(), m = y.size(); n   if (n != m) { n     cout <<  NO ; n     return 0; n   } n   if (n == 1) { n     if (x == y) n       cout <<  YES ; n     else n       cout <<  NO ; n     return 0; n   } n   if (x == y) { n     cout <<  YES ; n     return 0; n   } n   int cnt = count(x.begin(), x.end(),  0 ); n   int cnt1 = count(y.begin(), y.end(),  0 ); n   if (cnt == n || cnt1 == n) n     cout <<  NO ; n   else n     cout <<  YES ; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[2][316 + 1][316 + 2]; n int addmod(int x, int y) { n   int sum = x + y; n   if (sum >= 1000000007) sum -= 1000000007; n   return sum; n } n int mulmod(int x, int y, int p) { n   long long prod = x; n   prod = (prod * y) % p; n   return (int)prod; n } n void add_to_mod(int &x, int y) { x = addmod(x, y); } n int run_dp(int n, int m, int x) { n   int result = 0; n   if (n > m) return result; n   int next = 0; n   dp[next][0][0] = 1; n   for (int i = 1; i <= m; i++) { n     int prev = next; n     next ^= 1; n     for (int j = n; j > 0; j--) n       for (int k = j; k >= 0; k--) { n         dp[next][j][k] = addmod(dp[prev][j - 1][k], dp[next][j][k + 1]); n         if (m - i != x - k) { n           int s = dp[prev][j][k]; n           if (k > 0) add_to_mod(s, dp[next][j - 1][k - 1]); n           add_to_mod(dp[next][j][k], s); n         } n       } n     dp[next][0][0] = (m - i) > x; n   } n   result = dp[next][n][0]; n   for (int i = 2; i <= n; i++) result = mulmod(result, i, 1000000007); n   return result; n } n int solve_problem() { n   int n, m, x; n   if (scanf( %d %d %d , &n, &m, &x) != 3) return 1; n   --x; n   int result = run_dp(n, m, x); n   printf( %d n , result); n   return 0; n } n int main() { n   solve_problem(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   int n; n   cin >> n; n   vector<int> data(n + 1); n   int ans = 1e9; n   for (int i = 1; i <= n; i++) { n     cin >> data[i]; n     ans = min(ans, data[i] / max(i - 1, n - i)); n   } n   cout << ans << endl; n } n int main() { n   cin.sync_with_stdio(0); n   cin.tie(0); n   solve(); n } n


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


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n int Graph[1025][1025], f[1025][1025]; n int Tj(int x) { n   int tot = 0; n   while (x) tot++, x -= x & (-x); n   return tot; n } n void read(int &x) { n   x = 0; n   char c = getchar(); n   int f = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -f; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + c -  0 ; n     c = getchar(); n   } n   x *= f; n   return; n } n void write(int x) { n   if (x < 0) { n     x = -x; n     putchar( - ); n   } n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n signed main() { n   read(n), read(m), read(k); n   for (register int i = 1; i <= m; ++i) { n     int u, v; n     read(u), read(v); n     u--, v--; n     Graph[u][v] = Graph[v][u] = f[(1 << u) | (1 << v)][(1 << u) | (1 << v)] = 1; n   } n   for (register int i = 0; i < (1 << n); ++i) n     for (register int j = 0; j < (1 << n); ++j) n       if ((i & j) == j && f[i][j]) n         for (register int k1 = 0; k1 < n; ++k1) n           for (register int k2 = 0; k2 < n; ++k2) { n             if (Graph[k1][k2] && (i & (1 << k1)) && (~i & (1 << k2)) && n                 !(((j & (~(1 << k1))) | (1 << k2)) >> (k2 + 1))) n               f[i | (1 << k2)][j & (~(1 << k1)) | (1 << k2)] += f[i][j]; n           } n   int ans = 0; n   for (register int i = 0; i < (1 << n); ++i) n     if (Tj(i) == k) ans += f[(1 << n) - 1][i]; n   write(ans), putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gl, gr, bl, br; n bool check(int g, int b) { n   if (g - 1 <= b && b <= 2 * (g + 1)) n     return true; n   else n     return false; n } n int main() { n   scanf( %d %d %d %d , &gl, &gr, &bl, &br); n   if (check(gl, br) || check(gr, bl)) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[4] = {-1, 1, 0, 0}; n int dy[4] = {0, 0, -1, 1}; n const int inf = 0x7f7f7f7f; n const int mod = 1000000007; n const double eps = 1e-8; n int n, m; n set<int> v; n vector<pair<int, int> > ans; n vector<int> vc[100007]; n bool isok(int u, int v) { n   for (size_t i = 0; i < vc[u].size(); ++i) { n     if (vc[u][i] == v) return false; n   } n   return true; n } n bool dfs(int u) { n   if ((int)ans.size() == min(n - 1, m)) return true; n   v.erase(u); n   for (set<int>::iterator it = v.begin(); it != v.end(); ++it) { n     if (!isok(u, *it)) continue; n     ans.push_back(make_pair(u, *it)); n     if (dfs(*it)) n       return true; n     else { n       ans.pop_back(); n       it = v.find(*it); n     } n   } n   v.insert(u); n   return false; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i <= n; ++i) vc[i].clear(); n   int x, y; n   for (int i = 0; i < m; ++i) { n     scanf( %d%d , &x, &y); n     vc[x].push_back(y); n     vc[y].push_back(x); n   } n   for (int i = 1; i <= n; ++i) v.insert(i); n   ans.clear(); n   for (int i = 1; i <= n; ++i) { n     if (dfs(i)) { n       if (m == n) ans.push_back(make_pair(ans[0].first, ans.back().second)); n       for (int i = 0; i < m; ++i) n         printf( %d %d n , ans[i].first, ans[i].second); n       return 0; n     } n   } n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long b, c, d, e, f, g, h, i, j = 0, k = 2, l, o = 0, m = 0, n = 0, q = 1, n                                     r = 1, s, t, u, v, x = 1, y = 1, n                                     z = 1111111; n   string a; n   cin >> a; n   b = a.size(); n   for (i = 0; i < b; i++) { n     if (a[i] ==  4 ) n++; n     if (a[i] ==  7 ) m++; n   } n   if (n == 0 && m == 0) { n     cout << -1; n     return 0; n   } n   if (m > n) n     cout << 7; n   else n     cout << 4; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n vector<vector<int> > pre; n vector<int> starts; n vector<int> coproc; n vector<int> cnts; n vector<int> nexts; n void dfs(int v, int co) { n   for (int p : pre[v]) { n     cnts[p]--; n     if (cnts[p] == 0) { n       if (coproc[p] == co) { n         nexts.push_back(p); n       } else { n         dfs(p, co); n       } n     } n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int n, m; n   cin >> n >> m; n   coproc.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> coproc[i]; n   } n   pre.resize(n); n   cnts.assign(n, 0); n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     pre[v].push_back(u); n     cnts[u]++; n   } n   for (int i = 0; i < n; i++) { n     if (cnts[i] == 0) { n       if (coproc[i]) n         nexts.push_back(i); n       else n         starts.push_back(i); n     } n   } n   int cnt = 0; n   while (starts.size() + nexts.size()) { n     for (auto v : starts) { n       dfs(v, 1); n     } n     starts.swap(nexts); n     nexts.clear(); n     if (starts.size()) { n       cnt++; n     } n     for (auto v : starts) { n       dfs(v, 0); n     } n     starts.swap(nexts); n     nexts.clear(); n   } n   cout << cnt <<   n ; n   return false; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> z_function(string s) { n   long long n = (long long)s.length(); n   vector<long long> z(n); n   for (long long i = 1, l = 0, r = 0; i < n; ++i) { n     if (i <= r) z[i] = min(r - i + 1, z[i - l]); n     while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; n     if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; n   } n   return z; n } n signed main() { n   ios_base::sync_with_stdio(false); n   string s, t; n   cin >> s >> t; n   string str = t +  #  + s; n   auto z = z_function(str); n   long long n = s.size(), m = t.size(); n   long long dp[n], pre1[n], pre2[n]; n   for (long long i = 0; i < n; i++) dp[i] = 0, pre1[i] = 0, pre2[i] = 0; n   for (long long i = m - 1; i < n; i++) { n     if (z[i + 2] != m) { n       if (i) dp[i] = dp[i - 1] % 1000000007; n     } else { n       long long pre; n       if (i == m - 1) n         pre = 0; n       else n         pre = pre2[i - m]; n       dp[i] = pre + i - m + 2; n       dp[i] %= 1000000007; n     } n     if (i == 0) n       pre1[i] = dp[i]; n     else n       pre1[i] = pre1[i - 1] + dp[i]; n     if (i == 0) n       pre2[i] = pre1[i]; n     else n       pre2[i] = pre2[i - 1] + pre1[i]; n     pre1[i] %= 1000000007; n     pre2[i] %= 1000000007; n   } n   long long ans = 0; n   for (long long i = 0; i < n; i++) { n     ans += dp[i]; n     ans %= 1000000007; n   } n   cout << ans % 1000000007; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > g; n vector<char> used; n vector<long long> distArr, fArr, sArr; n long long A, B, diameter; n long long answer = 0; n struct step { n   int a, b, d; n   friend ostream& operator<<(ostream& s, const step& v); n }; n ostream& operator<<(ostream& s, const step& v) { n   s << v.a <<     << v.b <<     << v.d; n   return s; n } n vector<step> arr; n void dfs(int v, long long dist, vector<long long>& dArr, long long& cur) { n   dArr[v] = dist; n   if (dArr[cur] < dist) cur = v; n   for (int i = 0; i < (int)g[v].size(); i++) { n     int to = g[v][i]; n     if (!used[to]) { n       used[to] = true; n       dfs(to, dist + 1, dArr, cur); n     } n   } n } n void dfs2(int v, long long dist, bool flag) { n   if (!flag) { n     sArr[v] = dist; n   } n   for (int i = 0; i < (int)g[v].size(); i++) { n     int to = g[v][i]; n     if (!used[to]) { n       used[to] = true; n       dfs2(to, dist + 1, flag); n     } n   } n   if (fArr[v] + sArr[v] > diameter) { n     step temp; n     if (fArr[v] > sArr[v]) { n       temp.a = A + 1; n       answer += fArr[v]; n     } else { n       temp.a = B + 1; n       answer += sArr[v]; n     } n     temp.b = v + 1; n     temp.d = v + 1; n     arr.push_back(temp); n   } n   if (fArr[v] + sArr[v] == diameter && flag && v != A) { n     step temp; n     temp.a = A + 1; n     temp.d = v + 1; n     temp.b = v + 1; n     answer += fArr[v]; n     arr.push_back(temp); n   } else if (fArr[v] + sArr[v] == diameter) n     used[v] = false; n } n int main() { n   int n; n   cin >> n; n   g.assign(n, vector<int>()); n   for (int i = 0; i < n - 1; i++) { n     int from, to; n     cin >> from >> to; n     g[from - 1].push_back(to - 1); n     g[to - 1].push_back(from - 1); n   } n   used.assign(n, false); n   used[0] = true; n   distArr.assign(n, 0); n   A = 0; n   B = 0; n   dfs(0, 0, distArr, A); n   used.assign(n, false); n   used[A] = true; n   fArr.assign(n, 0); n   sArr.assign(n, 0); n   dfs(A, 0, fArr, B); n   diameter = fArr[B]; n   used.assign(n, false); n   used[B] = true; n   dfs2(B, 0, false); n   dfs2(A, 0, true); n   cout << answer << endl; n   for (int i = 0; i < (int)arr.size(); i++) cout << arr[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v; n long long a, b, f, k; n int cnt() { n   int c = 0; n   int n = v.size(); n   long long now = 0; n   for (int i = 0; i < n; i++) { n     if (now + b < v[i]) return -1; n     while (i < n && now + b >= v[i]) i++; n     i--; n     c++; n     now = v[i]; n   } n   return c - 1; n } n int main() { n   scanf( %I64d%I64d%I64d%I64d , &a, &b, &f, &k); n   v.push_back(f); n   for (long long i = 1; i < k; i++) { n     if (i & 1) n       v.push_back(v[i - 1] + 2 * (a - f)); n     else n       v.push_back(v[i - 1] + 2 * f); n   } n   v.push_back(a * k); n   printf( %d , cnt()); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   long long q; n   cin >> q; n   while (q--) { n     long long l1, r1, l2, r2; n     cin >> l1 >> r1 >> l2 >> r2; n     if (l1 == l2) { n       cout << l1 <<     << r2 <<   n ; n       continue; n     } n     if (l1 == r2) { n       cout << l1 <<     << l2 <<   n ; n       continue; n     } n     if (r1 == l2) { n       cout << r1 <<     << r2 <<   n ; n       continue; n     } n     if (r2 == r1) { n       cout << r1 <<     << l2 <<   n ; n       continue; n     } n     cout << l1 <<     << l2 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long M = 1e9 + 9; n int n, m, a, b, k; n char s[100005]; n long long ans, q; n inline long long f(char a) { return a ==  -  ? -1 : 1; } n inline long long fpow(long long a, long long b) { n   long long r = 1; n   for (; b; b >>= 1, a = (a * a) % M) n     if (b & 1) r = (r * a) % M; n   return r; n } n inline long long sum(long long a1, long long q, long long n) { n   if (q == 1) return a1 * n % M; n   long long p = (fpow(q, n) - 1 + M) % M; n   long long qq = (q - 1 + M) % M; n   qq = fpow(qq, M - 2); n   return a1 * p % M * qq % M; n } n int main() { n   scanf( %d %d %d %d , &n, &a, &b, &k); n   q = b * fpow(a, M - 2) % M; n   q = fpow(q, k); n   m = (n + 1) / k; n   scanf( %s , s); n   if (n < k) { n     for (int i = 0; i <= n; i++) { n       long long t = fpow(a, n - i) * fpow(b, i) % M; n       if (s[i] ==  - ) t = M - t; n       (ans += t) %= M; n     } n     return printf( %lld n , ans), 0; n   } n   for (int i = 0; i < k; i++) { n     long long x = sum(fpow(a, n - i) * fpow(b, i) % M, q, m); n     if (s[i] ==  - ) x = M - x; n     (ans += x) %= M; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n int n, a[105], ans; n int comp(const void* elem1, const void* elem2) { n   int f = *((int*)elem1); n   int s = *((int*)elem2); n   if (f > s) return 1; n   if (f < s) return -1; n   return 0; n } n int main(int argc, char* argv[]) { n   scanf( %d , &n); n   int i; n   for (i = 0; i < n; ++i) { n     int HH, MM; n     scanf( %d:%d , &HH, &MM); n     a[i] = HH * 60 + MM; n   } n   qsort(a, n, sizeof(a[0]), comp); n   ans = 0; n   for (i = 0; i < n - 1; ++i) { n     if (a[i + 1] - a[i] > ans) { n       ans = a[i + 1] - a[i]; n     } n   } n   if (a[0] + 24 * 60 - a[n - 1] > ans) { n     ans = a[0] + 24 * 60 - a[n - 1]; n   } n   ans--; n   int d1, d2; n   d1 = ans / 60; n   d2 = ans % 60; n   if (d1 < 10) { n     if (d2 < 10) { n       printf( 0%d:0%d n , d1, d2); n     } else { n       printf( 0%d:%d n , d1, d2); n     } n   } else { n     if (d2 < 10) { n       printf( %d:0%d n , d1, d2); n     } else { n       printf( %d:%d n , d1, d2); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 100100; n const int bl = 512; n static const int N = 600; n int n, k, q; n int s[maxn]; n struct block { n   int pop[N], c0; n   int push[N], c1; n   int ok; n } o[maxn / bl + 10]; n inline int eql(const int *a, const int *b, const int len) { n   for (int i = 0; i < len; ++i) { n     if (a[i] != b[i]) return 0; n   } n   return 1; n } n inline bool ask(int l, int r) { n   static int st[maxn]; n   int top = 0; n   for (; l % bl && l <= r; ++l) n     if (s[l] > 0) { n       st[++top] = s[l]; n     } else { n       if (!top || st[top] != -s[l]) return 0; n       --top; n     }; n   for (; l + bl - 1 <= r; l += bl) { n     block &o = ::o[l / bl]; n     if (!o.ok) return 0; n     if (top < o.c0) return 0; n     if (!eql(st + top - o.c0 + 1, o.pop + N - o.c0, o.c0)) return 0; n     top -= o.c0; n     memcpy(st + top + 1, o.push + 1, o.c1 << 2); n     top += o.c1; n   } n   for (; l <= r; ++l) n     if (s[l] > 0) { n       st[++top] = s[l]; n     } else { n       if (!top || st[top] != -s[l]) return 0; n       --top; n     }; n   return top == 0; n } n inline void init(int id) { n   block &o = ::o[id]; n   o.c0 = o.c1 = 0; n   o.ok = 1; n   int top = 0; n   const int L = id * bl, R = std::min(n - 1, id * bl + bl - 1); n   for (int i = L; i <= R; ++i) { n     if (s[i] > 0) { n       o.push[++top] = s[i]; n     } else { n       if (top && o.push[top] != -s[i]) { n         o.ok = 0; n         return; n       } else { n         if (top) { n           --top; n         } else { n           o.pop[N - ++o.c0] = -s[i]; n         } n       } n     } n   } n   o.c1 = top; n } n int main() { n   std::ios::sync_with_stdio(false), std::cin.tie(0); n   std::cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     std::cin >> s[i]; n   } n   for (int i = 0; i <= (n - 1) / bl; ++i) init(i); n   std::cin >> q; n   for (int i = 0; i < q; ++i) { n     int opt, pos, v, l, r; n     std::cin >> opt; n     if (opt == 1) { n       std::cin >> pos; n       --pos; n       std::cin >> s[pos]; n       init(pos / bl); n     } else { n       std::cin >> l >> r; n       std::cout << (ask(l - 1, r - 1) ?  Yes  :  No ) <<   n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n int main() { n   int n, k; n   cin >> n >> k; n   set<char> st; n   string s; n   cin >> s; n   for (int i = 0; i < n; ++i) st.insert(s[i]); n   int res = 0; n   char lastc = 0; n   int i; n   for (i = 0; i < k && !st.empty(); ++i) { n     auto it = st.begin(); n     char c = *it; n     st.erase(it); n     if (*it - lastc < 2) n       --i; n     else { n       res += int(c -  a  + 1); n       lastc = c; n     } n   } n   if (i == k) n     cout << res <<   n ; n   else n     cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 1000001; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   vector<pair<int, int>> a[n + 1]; n   int ans = 0; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     a[u].push_back(make_pair(v, i + 1)); n     a[v].push_back(make_pair(u, i + 1)); n     ans = max({ans, (int)a[u].size(), (int)a[v].size()}); n   } n   cout << ans <<   n ; n   int final[n], vis[n + 1]; n   memset(final, -1, sizeof final); n   memset(vis, 0, sizeof vis); n   queue<int> q; n   q.push(1); n   vis[1] = 1; n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     map<int, int> d; n     for (auto [v, ind] : a[u]) { n       d[final[ind]] = 1; n     } n     int day = 1; n     for (auto [v, ind] : a[u]) { n       if (final[ind] == -1) { n         while (d[day]) { n           ++day; n         } n         final[ind] = day++; n       } n       if (vis[v] == 0) { n         vis[v] = 1; n         q.push(v); n       } n     } n   } n   vector<int> f1[ans + 1]; n   for (int i = 1; i < n; i++) { n     f1[final[i]].push_back(i); n   } n   for (int i = 1; i < ans + 1; i++) { n     cout << f1[i].size() <<    ; n     for (int e : f1[i]) { n       cout << e <<    ; n     } n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n struct column { n   int a[13]; n   int x; n }; n column c[2001]; n bool cmp(column p, column q) { return p.x > q.x; } n int s[13][4096], f[13][4096]; n int mian() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) scanf( %d , &c[j].a[i]); n   for (int j = 1; j <= m; j++) { n     c[j].x = 0; n     for (int i = 1; i <= n; i++) c[j].x = max(c[j].x, c[j].a[i]); n   } n   sort(c + 1, c + 1 + m, cmp); n   m = min(m, n); n   for (int i = 1; i <= m; i++) n     for (int j = 0; j < (1 << n); j++) { n       s[i][j] = 0; n       for (int p = 0; p < n; p++) { n         int sum = 0; n         for (int l = 1, k = p + 1; l <= n; l++, k = (k == n) ? 1 : (k + 1)) n           if (j & (1 << l - 1)) sum += c[i].a[k]; n         s[i][j] = max(s[i][j], sum); n       } n     } n   for (int i = 1; i <= m; i++) n     for (int j = 0; j < (1 << n); j++) { n       f[i][j] = f[i - 1][j]; n       for (int k = j; k; k = (k - 1) & j) n         f[i][j] = max(f[i][j], f[i - 1][j ^ k] + s[i][k]); n     } n   printf( %d n , f[m][(1 << n) - 1]); n   return 0; n } n int t; n int main() { n   for (scanf( %d , &t); t; t--) mian(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n template <class T1, class T2> n ostream &operator<<(ostream &os, const pair<T1, T2> &p) { n   os <<  {  << p.first <<  ,  << p.second <<  } ; n   return os; n } n const int N = 1e6 + 5; n const long long oo = 998244353; n long long fact[N]; n long long fact_inv[N]; n long long extended_euclidean(long long a, long long b, long long &x, n                              long long &y) { n   if (a == 0) { n     x = 0; n     y = 1; n     return b; n   } n   long long x1, y1; n   long long d = extended_euclidean(b % a, a, x1, y1); n   x = y1 - (b / a) * x1; n   y = x1; n   return d; n } n long long mod_inverse(long long a, long long m) { n   long long x, y; n   long long g = extended_euclidean(a, m, x, y); n   if (g != 1) { n     cout <<  No solution! ; n     return -1; n   } else { n     x = (x % m + m) % m; n     return x; n   } n } n long long ncr(int n, int r) { n   return n < r ? 0 n                : ((1LL * fact[n] % oo * fact_inv[r] % oo) % oo * n                   fact_inv[n - r]) % n                      oo; n } n void pre(int N) { n   fact[0] = 1; n   for (long long i = 1; i < N; i++) { n     fact[i] = (i * fact[i - 1]) % oo; n   } n   fact_inv[N - 1] = mod_inverse(fact[N - 1], oo); n   for (long long i = N - 2; i >= 0; i--) { n     fact_inv[i] = (i + 1LL) * fact_inv[i + 1] % oo; n   } n } n long long bigmod(long long a, long long p, long long m) { n   long long res = 1; n   long long x = a; n   while (p) { n     if (p & 1) { n       res = (res * x) % m; n     } n     x = (x * x) % m; n     p = p >> 1; n   } n   return res; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   pre(N); n   int n; n   cin >> n; n   int a = 0, b = 0; n   int totww = 0, totwb = 0; n   int totbb = 0, totbw = 0; n   int tot__ = 0; n   while (n--) { n     string s; n     cin >> s; n     if (s ==  WW ) n       a++; n     else if (s ==  W?  or s ==  ?W ) n       b++; n     else if (s ==  B?  or s ==  ?B ) n       a--, b++; n     else if (s ==  BB ) n       a--; n     else if (s ==  ?? ) n       b += 2, a--; n     totww += s ==  WW ; n     totwb += s ==  W? ; n     totwb += s ==  WB ; n     totwb += s ==  ?B ; n     totbb += s ==  BB ; n     totbw += s ==  B? ; n     totbw += s ==  BW ; n     totbw += s ==  ?W ; n     tot__ += s ==  ?? ; n   } n   a = -a; n   long long ans = 0; n   if (a <= b and a >= 0) { n     ans = ncr(b, a); n     if (totww == 0 and totbb == 0) { n       long long x = bigmod(2, tot__, oo); n       ans = (ans - x + oo) % oo; n       ans += totbw == 0; n       ans += totwb == 0; n     } n   } n   ans = (ans + oo) % oo; n   cout << ans << endl; n   return 0; n } n