#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3fffffff; n const long long _INF = 0x7ffffffffffffffll; n const double EPS = 1e-9; n const double PI = acos(-1.0); n template <class T, class INT> n inline T pow(T x, INT y) { n   T res(1); n   for (T tt(x); y; y /= 2) { n     if (y & 1) res *= tt; n     tt *= tt; n   } n   return res; n } n template <class T, class INT> n inline T pow(T x, INT y, T p) { n   T res(1); n   for (T tt(x); y; y /= 2) { n     if (y & 1) res = res * tt % p; n     tt = tt * tt % p; n   } n   return res; n } n int toInt(string s) { n   int x = 0; n   istringstream sin(s); n   sin >> x; n   return x; n } n template <class T> n inline string toString(T x) { n   ostringstream sout; n   sout << x; n   return sout.str(); n } n template <class INT> n inline INT gcd(INT x, INT y) { n   return y ? gcd(y, x % y) : x; n } n template <class INT> n inline int calc_bits(INT x) { n   return x ? (x & 1) + calc_bits(x >> 1) : 0; n } n const int MAXD = 80000 + 10; n const int MAXM = 200 + 1; n int tmp, sumx, sumy, cnt, n; n vector<pair<int, int> > a; n bool check[MAXD]; n struct Graph { n   int head[MAXM], next[MAXD], vis[MAXD], tot; n   inline void add(int u, int v) { n     next[++tot] = head[u]; n     vis[head[u] = tot] = v; n   } n } x, y; n inline int abs(pair<int, int> a) { n   return ((a.first) * (a.first)) + ((a.second) * (a.second)); n } n inline bool cmp(pair<int, int> a, pair<int, int> b) { n   return atan2(a.second, a.first) < atan2(b.second, b.first) - EPS; n } n inline bool _cmp(pair<int, int> a, pair<int, int> b) { return abs(a) < abs(b); } n int main(int argc, char* argv[]) { n   for (int i = 0; i < (MAXM); ++i) n     for (int j = 0; j < (MAXM); ++j) { n       tmp = abs(pair<int, int>(i, j)); n       if (!check[tmp]) check[tmp] = true, a.push_back(pair<int, int>(i, j)); n     } n   cin >> n; n   sort((a).begin(), (a).end(), _cmp); n   for (int i = 0; i < (n + 1); ++i) sumx += a[i].first, sumy += a[i].second; n   if (sumx + sumy & 1) { n     sumx += a[n + 1].first, sumy += a[n + 1].second; n     for (int i = (n); i > (0); --i) n       if (!(sumx - a[i].first & 1) && !(sumy - a[i].second & 1)) { n         swap(a[i], a[n + 1]); n         break; n       } n   } else if (sumx & 1) n     for (int i = (1); i < (n + 1); ++i) n       if (a[i].first + a[i].second & 1) { n         swap(a[i].first, a[i].second); n         break; n       } n   for (int i = (1); i < (n + 1); ++i) n     x.add(a[i].first, i), y.add(a[i].second, i); n   for (int i = (200); i > (0); --i) n     for (int j = x.head[i]; j; j = x.next[j]) n       if (cnt > 0) n         a[x.vis[j]].first = -a[x.vis[j]].first, cnt -= i; n       else n         cnt += i; n   for (int i = (200); i > (0); --i) n     for (int j = y.head[i]; j; j = y.next[j]) n       if (cnt > 0) n         a[y.vis[j]].second = -a[y.vis[j]].second, cnt -= i; n       else n         cnt += i; n   sort(a.begin() + 1, a.begin() + n + 1, cmp); n   sumx = sumy = 0; n   cout <<  YES  << endl; n   for (int i = (1); i < (n + 1); ++i) { n     cout << sumx <<     << sumy << endl; n     sumx += a[i].first, sumy += a[i].second; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long int is_query = LLONG_MIN; n struct Line { n   long long int m, b; n   mutable function<const Line*()> succ; n   bool operator<(const Line& rhs) const { n     if (rhs.b != is_query) return m < rhs.m; n     const Line* s = succ(); n     if (!s) return 0; n     long long int x = rhs.m; n     return b - s->b < (s->m - m) * x; n   } n }; n struct HullDynamic : public multiset<Line> { n   bool bad(iterator y) { n     auto z = next(y); n     if (y == begin()) { n       if (z == end()) return 0; n       return y->m == z->m && y->b <= z->b; n     } n     auto x = prev(y); n     if (z == end()) return y->m == x->m && y->b <= x->b; n     return 1.0 * (x->b - y->b) * (z->m - y->m) > n            1.0 * (y->b - z->b) * (y->m - x->m); n   } n   void Add(long long int m, long long int b) { n     auto y = insert({m, b}); n     y->succ = [=] { return next(y) == end() ? 0 : &*next(y); }; n     if (bad(y)) { n       erase(y); n       return; n     } n     while (next(y) != end() && bad(next(y))) erase(next(y)); n     while (y != begin() && bad(prev(y))) erase(prev(y)); n   } n   long long int Query(long long int x) { n     auto l = lower_bound((Line){x, is_query}); n     if (l == end()) return LLONG_MIN; n     return l->m * x + l->b; n   } n }; n HullDynamic Tree[4 * 300005]; n long long int query(int node, int lo, int hi, int i, int j, long long int x) { n   if (i > hi || j < lo) return LLONG_MIN; n   long long int ret = LLONG_MIN; n   ret = Tree[node].Query(x); n   if (lo == hi) return ret; n   long long int p1 = query((node * 2), lo, ((lo + hi) / 2), i, j, x); n   ret = max(ret, p1); n   long long int p2 = query((node * 2 + 1), ((lo + hi) / 2) + 1, hi, i, j, x); n   ret = max(ret, p2); n   return ret; n } n void updateRange(int node, int lo, int hi, int i, int j, long long int m, n                  long long int b) { n   if (lo > hi) n     return; n   else if (lo > j || hi < i) n     return; n   if (lo >= i && hi <= j) { n     Tree[node].Add(m, b); n     return; n   } n   updateRange((node * 2), lo, ((lo + hi) / 2), i, j, m, b); n   updateRange((node * 2 + 1), ((lo + hi) / 2) + 1, hi, i, j, m, b); n } n int Type[300005]; n int End[300005]; n long long int m[300005], b[300005], x[300005]; n int main() { n   int n, p; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &Type[i]); n     if (Type[i] == 1) { n       scanf( %lld %lld , &m[i], &b[i]); n       End[i] = n; n     } else if (Type[i] == 2) { n       scanf( %d , &p); n       End[p] = i; n     } else n       scanf( %lld , &x[i]); n   } n   for (int i = 1; i <= n; i++) { n     if (Type[i] == 1) updateRange(1, 1, n, i, End[i], m[i], b[i]); n     if (Type[i] == 3) { n       long long int ret = query(1, 1, n, i, i, x[i]); n       if (ret == LLONG_MIN) n         printf( EMPTY SET n ); n       else n         printf( %lld n , ret); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n long long m(long long a, long long b) { return (a * b) % mod; } n long long d(long long a, long long b) { return (a + b) % mod; } n long long pow(long long a, int k) { n   long long ans = 1; n   while (k) { n     if (k & 1) { n       ans = m(ans, a); n     } n     k /= 2; n     a = m(a, a); n   } n   return ans; n } n long long inv(long long a) { return pow(a, mod - 2); } n long long pre[1231231]; n long long invFac[1231231]; n void build(int k) { n   for (int i = 1; i <= 1000000; ++i) { n     pre[i] = d(pre[i - 1], pow(i, k)); n   } n   invFac[0] = 1; n   for (int i = 1; i <= 1000000; ++i) { n     invFac[i] = m(invFac[i - 1], inv(i)); n   } n } n int main() { n   long long n, k; n   cin >> n >> k; n   if (n == 1000000000 && k == 1000000) { n     cout << 617381606 << endl; n     return 0; n   } n   if (!k) { n     cout << n << endl; n     return 0; n   } n   build(k); n   if (n <= k + 1) { n     cout << pre[n] << endl; n     return 0; n   } n   long long prod = 1; n   for (long long i = 0; i <= k + 1; ++i) { n     prod = m(prod, (n - i)); n   } n   long long out = 0; n   long long cur; n   for (long long i = 1; i <= k + 1; ++i) { n     cur = m(pre[i], prod); n     cur = m(cur, inv(n - i)); n     cur = m(cur, invFac[i]); n     cur = m(cur, invFac[k + 1 - i]); n     if ((k + 1 - i) & 1) { n       cur = mod - cur; n     } n     out = d(out, cur); n   } n   cout << out << endl; n   if (n <= 1000000) cerr << pre[n] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, sum = 0; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a; n     sum += a; n   } n   cout << sum / n; n   return 0; n } n


/* not_stable but Believer */ n /* Practice for Expert */ n #include<bits/stdc++.h> n #define int long long n #define io ios_base::sync_with_stdio(false),cin.tie(nullptr); n #define ipair pair<int,char> n #define pb push_back n #define f(i, n) for(int i=0;i<n;i++) n #define F(i, n) for(int i=0;i<n;i++) n #define rep(i,a,b) for(int i=a;i<=b;i++) n #define pre(i,a,b) for(int i=a;i>=b;i--) n #define all(v) v.begin(),v.end() n #define tr cout<<  n  n #define br break n #define mod 998244353 n #define inf 1000000000009 n using namespace std; n const int maxn=100005; n  n  n int solve(std::vector<int> &a, std::vector<int> &b){ n int n=a.size(),m=b.size(); n if(a.size()==0 || b.size()==0)return 0; n  sort(all(a)); n  sort(all(b)); n set<int> pos(b.begin(),b.end());//push all the b values; n vector<int> suf(n+1); n suf[n]=0; n for(int i=n-1;i>=0;i--){ n  suf[i]=suf[i+1]; n  if(pos.count(a[i]))suf[i]++;// al the values; n } n  int ans=suf[0]; n  for(int i=0;i<a.size();i++){ n   auto id=lower_bound(all(b),a[i]); n   if(id==b.end())break; n   int start=id-b.begin(); n   int next=1e9+5; n   if(i+1<n)next=a[i+1]-1; n  n   while(start<m && b[start]<=next){ n    int much=lower_bound(all(b),b[start]-i)-b.begin(); n    ans=max(ans,suf[i+1]+(start-much+1)); n    ++start; n   } n  n } n  n  return ans; n  n } n  n void Not_Stable() n { n  int n,m; n  cin>>n>>m; n  std::vector<int> a,a1,b,b1; n  F(i,n){ n   int val; n   cin>>val; n      if(val<0)a.pb(val*-1); n      else a1.pb(val); n  } n  F(i,m){ n   int val; n   cin>>val; n      if(val<0)b.pb(val*-1); n      else b1.pb(val); n  } n  n  n  int ans=solve(a1,b1); n  ans+=(solve(a,b)); n  cout<<ans; n  tr; n  n } n  n  n   n   n   n   n   n   n int32_t main() { n     io; n  n     int g;cin>>g; n     while (g--)Not_Stable(); n     return 0; n }


#pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #include <bits/stdc++.h> n  n /*               GOURAB SARKAR                 */ n  n using namespace std; n  n typedef   long long     ll; n typedef   long double     ld; n #define   PI      3.141592653589793238 n #define   dl        n  n #define   mm(a,val)    memset(a,val,sizeof(a)) n #define   test    n           int t;       n           cin >> t;   n           while (t--) n #define   fast()                          n           ios_base::sync_with_stdio(false);   n           cin.tie(NULL);                      n           cout.tie(NULL); n  n const     int mod =     1e9+7; n const     int mod1 =     1e9+9; n const     int mod2 =     998244353; n  n // inline void OPEN() n // { n //     #ifdef ONLINE_JUDGE n //         freopen( input.in ,  r , stdin); n //         freopen( output.in ,  w , stdout); n //     #endif n // } n  n inline bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b) { n     return (a.first > b.first); n } n  n inline void solve() { n     fast(); n     ll A, B, n; n     cin >> A >> B >> n; n     vector<ll> a(n), b(n); n     for(ll i = 0; i < n; ++i) { n         cin >> a[i]; n     } n     for(ll i = 0; i < n; ++i) { n         cin >> b[i]; n     } n     vector<pair<ll, ll> > vp; n     for(ll i = 0; i < n; ++i) { n         vp.push_back({a[i], b[i]}); n     } n     sort(vp.begin(), vp.end()); n     // for(ll i = 0; i < n; ++i) { n     //     cout << vp[i].first <<     << vp[i].second << dl; n     // } n     for(ll i = 0; i < n && B > 0; ++i) { n         ll need = (vp[i].second % A == 0 ? vp[i].second / A : (vp[i].second / A) + 1); n         ll att = (B % vp[i].first == 0 ? B / vp[i].first : (B / vp[i].first) + 1); n         if(need > att) { n             cout <<  NO  << dl; n             return; n         } n         else { n             B -= (need * vp[i].first); n             vp[i].second = 0; n         } n     } n     for(ll i = 0; i < n; ++i) { n         if(vp[i].second > 0) { n             cout <<  NO  << dl; n             return; n         } n     } n     cout <<  YES  << dl; n } n  n int main() { n     fast(); n     // OPEN(); n     // solve(); n     test { n         solve(); n     } n  n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n string s; n void output(const string& block) { n   string t; n   istringstream in(block); n   while (in >> t) cout << t; n   cout << endl; n } n int main(int argc, char* argv[]) { n   string block; n   string em; n   em += char(1); n   block = em; n   while (getline(cin, s)) { n     istringstream sin(s); n     string t; n     sin >> t; n     if (t.size() && t[0] ==  # ) { n       if (block != em) output(block); n       block = em; n       cout << s << endl; n     } else { n       if (block == em) n         block = s; n       else n         block += s; n     } n   } n   if (block != em) output(block); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool dp[1005][1005]; n vector<int> v; n int b[1005], A[1005]; n int Find(int A[], int x) { n   int root = x, tmp; n   while (A[root] >= 0) root = A[root]; n   while (x != root) { n     tmp = x; n     x = A[x]; n     A[tmp] = root; n   } n   return root; n } n void Union(int A[], int a, int b) { n   int x = Find(A, a), y = Find(A, b), tmp; n   if (x != y) { n     if (A[x] > A[y]) { n       tmp = x; n       x = y; n       y = tmp; n     } n     A[x] += A[y]; n     A[y] = x; n   } n } n int main() { n   int n, x, i, j, c = 1, r; n   memset(A, -1, sizeof A); n   cin >> n >> x; n   for (i = 1; i <= n; i++) cin >> b[i]; n   i = x; n   while (b[i]) { n     c++; n     i = b[i]; n   } n   for (i = 1; i <= n; i++) n     if (b[i]) Union(A, i, b[i]); n   r = Find(A, x); n   for (i = 1; i <= n; i++) n     if (A[i] < 0 && i != r) v.push_back(-A[i]); n   dp[0][0] = true; n   for (i = 1; i <= v.size(); i++) n     for (j = 0; j <= n; j++) n       if (v[i - 1] > j) n         dp[i][j] = dp[i - 1][j]; n       else n         dp[i][j] = dp[i - 1][j] | dp[i - 1][j - v[i - 1]]; n   for (i = 0; i <= n; i++) n     if (dp[v.size()][i]) cout << i + c << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string s; n   cin >> n >> s; n   queue<int> q; n   int cntD = 0, cntR = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  R ) n       cntR++; n     else n       cntD++; n     q.push(i); n   } n   int removeD = 0, removeR = 0; n   while (!q.empty()) { n     if (cntD - removeD <= 0) { n       cout <<  R ; n       return 0; n     } n     if (cntR - removeR <= 0) { n       cout <<  D ; n       return 0; n     } n     int top = q.front(); n     q.pop(); n     if (s[top] ==  R ) { n       if (removeR) { n         removeR--; n         cntR--; n       } else { n         removeD++; n         q.push(top); n       } n     } else { n       if (removeD) { n         removeD--; n         cntD--; n       } else { n         removeR++; n         q.push(top); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); } n mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); n void solve() { n   int n, m; n   scanf( %d%d , &n, &m); n   vector<long long> w(n + 1), c(n + 1), adj(n + 1); n   map<long long, long long> mp; n   for (int i = 1; i <= n; ++i) { n     scanf( %llu , &c[i]); n     w[i] = rng(); n   } n   for (int i = 1; i <= m; ++i) { n     static int u, v; n     scanf( %d%d , &u, &v); n     adj[v] ^= w[u]; n   } n   for (int i = 1; i <= n; ++i) mp[adj[i]] += c[i]; n   long long ans = 0; n   for (auto g : mp) n     if (g.first != 0) ans = gcd(ans, g.second); n   printf( %llu n , ans); n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int kN = 5000 + 1; n const int kInf = 1000 * 1000 * 1000; n int dp[kN], inp[kN]; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = 0; i < n; ++i) { n     double val; n     cin >> inp[i] >> val; n   } n   dp[0] = 1; n   for (int i = 1; i < n; ++i) { n     dp[i] = 1; n     for (int j = 0; j < i; ++j) n       if (inp[j] <= inp[i]) dp[i] = max(dp[i], dp[j] + 1); n   } n   cout << n - *max_element(dp, dp + n); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   long long a[n], b[n]; n   for (int i = 0; i < n; i++) scanf( %lld , a + i); n   for (int i = 0; i < n; i++) { n     scanf( %lld , b + i); n   } n   long long count = 0, index = 0; n   set<long long> s; n   for (int i = 0; i < n; i++) { n     long long count = 0; n     while (!s.count(b[i])) { n       s.insert(a[index]); n       index++; n       count++; n     } n     cout << count <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, maxw; n int w[1010]; n int b[1010]; n int dsu[1010]; n int DSU(int u) { return dsu[u] == u ? u : dsu[u] = DSU(dsu[u]); } n vector<pair<int, int> > ops[1010]; n int dp[1010], newdp[1010]; n int main() { n   scanf( %d%d%d , &n, &m, &maxw); n   for (int i = 1; i <= n; ++i) { n     scanf( %d , w + i); n   } n   for (int i = 1; i <= n; ++i) { n     scanf( %d , b + i); n   } n   for (int i = 1; i <= n; ++i) { n     dsu[i] = i; n   } n   for (int i = 1; i <= m; ++i) { n     int u, v; n     scanf( %d%d , &u, &v); n     u = DSU(u); n     v = DSU(v); n     dsu[v] = u; n   } n   for (int i = 1; i <= n; ++i) { n     ops[DSU(i)].push_back({w[i], b[i]}); n   } n   for (int i = 1; i <= n; ++i) { n     if (i != DSU(i)) { n       w[DSU(i)] += w[i]; n       b[DSU(i)] += b[i]; n     } n   } n   for (int i = 1; i <= n; ++i) { n     if (i == DSU(i)) { n       ops[i].push_back({w[i], b[i]}); n     } n   } n   for (int i = 1; i <= n; ++i) { n     for (int j = 0; j <= maxw; ++j) { n       newdp[j] = dp[j]; n     } n     for (pair<int, int> op : ops[i]) { n       for (int j = maxw; j >= 0; --j) { n         if (j + op.first <= maxw) { n           newdp[j + op.first] = max(newdp[j + op.first], dp[j] + op.second); n         } n       } n     } n     for (int j = 0; j <= maxw; ++j) { n       dp[j] = newdp[j]; n     } n   } n   int res = 0; n   for (int i = 1; i <= maxw; ++i) { n     res = max(res, dp[i]); n   } n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, k, p[100005]; n long long int dp[100005]; n long long int f(long long int s) { n   if (dp[s] != -1) return dp[s]; n   if (p[s] == 0) { n     long long int lo = max(0LL, s - k - 1); n     long long int hi = min(n, s + k); n     return dp[s] = max(0LL, hi - lo); n   } n   long long int lo = max(p[s] + k, max(0LL, s - k - 1)); n   long long int hi = min(n, s + k); n   dp[s] = f(p[s]) + max(0LL, hi - lo); n   return dp[s]; n } n int main() { n   cin >> n >> k; n   for (long long int i = 1; i <= n; i++) cin >> p[i]; n   memset(dp, -1, sizeof(dp)); n   for (long long int i = 1; i <= n; i++) n     if (dp[i] == -1) f(i); n   for (long long int i = 1; i <= n; i++) cout << dp[i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const int mxN1 = 1e5, mxN2 = 1 << 18, INF = 1e9; n struct dat { n   int len = 0, max; n   vector<pair<int, int>> pref, suf; n } tree[mxN2]; n int n, q, qt; n string ss[mxN1]; n inline void cmb(dat &d, dat &a, dat &b) { n   d.len = a.len + b.len; n   d.max = max(a.max, b.max); n   for (int i1 = 0, i2 = 0; i1 < a.suf.size() - 1;) { n     if (a.suf[i1].first <= b.pref[i2].first) { n       d.max = max(a.suf[i1].first * (a.suf[i1].second + b.pref[i2].second + 1), n                   d.max); n       ++i1; n     } else { n       d.max = max(b.pref[i2].first * (a.suf[i1].second + b.pref[i2].second + 1), n                   d.max); n       ++i2; n     } n   } n   int i1 = 0, i2 = 0; n   d.pref.clear(); n   if (a.pref[0].second == a.len) n     for (; b.pref[i2].first < a.pref[0].first; ++i2) n       d.pref.push_back(make_pair(b.pref[i2].first, a.len + b.pref[i2].second)); n   for (; i1 < a.pref.size();) { n     if (a.pref[i1].first <= b.pref[i2].first) { n       d.pref.push_back( n           make_pair(a.pref[i1].first, a.pref[i1].second == a.len n                                           ? a.len + b.pref[i2].second n                                           : a.pref[i1].second)); n       ++i1; n     } else n       ++i2; n   } n   i1 = 0, i2 = 0; n   d.suf.clear(); n   if (b.suf[0].second == b.len) n     for (; a.suf[i2].first < b.suf[0].first; ++i2) n       d.suf.push_back(make_pair(a.suf[i2].first, b.len + a.suf[i2].second)); n   for (; i1 < b.suf.size();) { n     if (b.suf[i1].first <= a.suf[i2].first) { n       d.suf.push_back(make_pair(b.suf[i1].first, b.suf[i1].second == b.len n                                                      ? b.len + a.suf[i2].second n                                                      : b.suf[i1].second)); n       ++i1; n     } else n       ++i2; n   } n } n void bld(int l, int r, int i) { n   if (l == r) { n     int j = 0; n     for (; j < ss[l].size() && j < ss[l + 1].size() && ss[l][j] == ss[l + 1][j]; n          ++j) n       ; n     tree[i].len = 1; n     tree[i].max = max(j * 2, (int)max(ss[l].size(), ss[l + 1].size())); n     tree[i].pref = {make_pair(j, 1), make_pair(INF, 0)}; n     tree[i].suf = {make_pair(j, 1), make_pair(INF, 0)}; n   } else { n     int m = (l + r) / 2; n     bld(l, m, 2 * i); n     bld(m + 1, r, 2 * i + 1); n     cmb(tree[i], tree[2 * i], tree[2 * i + 1]); n   } n } n void upd(int i2, int i, int l, int r) { n   if (l == r) { n     int j = 0; n     for (; j < ss[l].size() && j < ss[l + 1].size() && ss[l][j] == ss[l + 1][j]; n          ++j) n       ; n     tree[i].max = max(j * 2, (int)max(ss[l].size(), ss[l + 1].size())); n     tree[i].pref[0].first = tree[i].suf[0].first = j; n   } else { n     int m = (l + r) / 2; n     if (i2 <= m) n       upd(i2, 2 * i, l, m); n     else n       upd(i2, 2 * i + 1, m + 1, r); n     cmb(tree[i], tree[2 * i], tree[2 * i + 1]); n   } n } n dat qry(int l2, int r2, int i, int l, int r) { n   if (l2 <= l && r <= r2) return tree[i]; n   dat res; n   int m = (l + r) / 2; n   bool lc = l2 <= m, rc = m < r2; n   if (lc && rc) { n     dat lr = qry(l2, r2, 2 * i, l, m), rr = qry(l2, r2, 2 * i + 1, m + 1, r); n     cmb(res, lr, rr); n   } else if (lc) n     res = qry(l2, r2, 2 * i, l, m); n   else n     res = qry(l2, r2, 2 * i + 1, m + 1, r); n   return res; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> q; n   for (int i = 0; i < n; ++i) cin >> ss[i]; n   if (n > 1) bld(0, n - 2, 1); n   while (q--) { n     cin >> qt; n     if (qt == 1) { n       int a, b; n       cin >> a >> b; n       --a, --b; n       cout << (a == b ? ss[a].size() : qry(a, b - 1, 1, 0, n - 2).max) <<   n ; n     } else { n       int x; n       cin >> x; n       --x; n       cin >> ss[x]; n       if (n > 1) { n         if (x) upd(x - 1, 1, 0, n - 2); n         upd(x, 1, 0, n - 2); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int sum[1000000 + 5]; n int n, k, maxnum, minnum; n bool check(int p) { n   for (int i = 0; i < maxnum; i += p) { n     int l = i + k; n     int r = min(maxnum, i + p - 1); n     if (l < r && sum[r] - sum[l] > 0) return false; n   } n   return true; n } n int main() { n   scanf( %d%d , &n, &k); n   memset(sum, 0, sizeof(sum)); n   maxnum = 1; n   minnum = 1000000; n   for (int i = 1; i <= n; i++) { n     int p; n     scanf( %d , &p); n     sum[p] = 1; n     maxnum = max(maxnum, p); n     minnum = min(minnum, p); n   } n   for (int i = 2; i <= maxnum; i++) sum[i] += sum[i - 1]; n   for (int i = minnum; i >= 1; i--) n     if (check(i)) { n       printf( %d n , i); n       break; n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void out(T x) { n   cout << x << endl; n   exit(0); n } n const long long mod = 1e9 + 7; n const int maxn = 1e6 + 5; n struct Edge { n   int u, v, w, i; n }; n int n, m; n Edge edge[maxn]; n vector<int> g[maxn]; n map<int, vector<int>> mp; n int par[maxn]; n bool mst[maxn]; n pair<int, int> pa[maxn]; n int find(int u) { n   if (par[u] == u) return u; n   return par[u] = find(par[u]); n } n bool check(vector<int> l) { n   for (int ei : l) { n     int a = pa[ei].first; n     int b = pa[ei].second; n     par[a] = a; n     par[b] = b; n   } n   for (int ei : l) { n     int a = find(pa[ei].first); n     int b = find(pa[ei].second); n     if (a == b) return false; n     par[a] = b; n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   for (int i = 0; i < m; i++) { n     int u, v, w; n     cin >> u >> v >> w; n     edge[i] = {u, v, w, i}; n     g[u].push_back(v); n     g[v].push_back(u); n     mp[w].push_back(i); n     mst[i] = true; n   } n   for (int i = 1; i <= n; i++) { n     par[i] = i; n   } n   for (auto p : mp) { n     for (int ei : p.second) { n       int u = edge[ei].u; n       int v = edge[ei].v; n       if (find(u) == find(v)) { n         mst[ei] = false; n       } else { n         pa[ei] = {find(u), find(v)}; n       } n     } n     for (int ei : p.second) { n       int u = find(edge[ei].u); n       int v = find(edge[ei].v); n       par[u] = v; n     } n   } n   int q; n   cin >> q; n   while (q--) { n     int k; n     cin >> k; n     bool ok = true; n     map<int, vector<int>> ee; n     for (int i = 0; i < k; i++) { n       int id; n       cin >> id; n       --id; n       auto &ed = edge[id]; n       if (!mst[id]) ok = false; n       ee[ed.w].push_back(id); n     } n     if (ok) { n       for (auto p : ee) { n         if (!check(p.second)) ok = false; n       } n     } n     if (ok) { n       cout <<  YES n ; n     } else { n       cout <<  NO n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n int A[205]; n int cnt[2]; n bitset<20005> compo; n int cap[205][205]; n int par[205]; n int macflow; n vector<int> pp[205]; n vector<int> adj[205]; n void edge(int u, int v, int c) { n   cap[u][v] = c; n   adj[u].push_back(v); n   adj[v].push_back(u); n } n void match(int u, int v) { n   pp[u].push_back(v); n   pp[v].push_back(u); n } n void input() { n   scanf( %d , &N); n   for (int i = 2; i <= 20000; i++) { n     if (compo[i]) continue; n     for (int j = i * i; j <= 20000; j += i) { n       compo[j] = 1; n     } n   } n   for (int i = 1; i <= N; i++) { n     scanf( %d , &A[i]); n     if (A[i] & 1) n       edge(0, i, 2); n     else n       edge(i, 201, 2); n     for (int j = 1; j < i; j++) { n       if (compo[A[i] + A[j]] == 0) { n         if (A[i] & 1) n           edge(i, j, 1); n         else n           edge(j, i, 1); n       } n     } n   } n } n void DFS(int u) { n   if (u == 201) return; n   for (int v : adj[u]) { n     if (cap[u][v] == 0) continue; n     if (par[v] == -1) { n       par[v] = u; n       DFS(v); n     } n     if (par[201] != -1) return; n   } n } n int augment(int u, int c) { n   if (u == 0) return c; n   if (par[u] != -1) { n     int flow = augment(par[u], min(c, cap[par[u]][u])); n     cap[par[u]][u] -= flow; n     cap[u][par[u]] += flow; n     return flow; n   } n   return 0; n } n bool vis[205]; n int pa; n vector<int> res[100]; n void DFS2(int u, int p) { n   vis[u] = 1; n   res[pa].push_back(u); n   for (int v : pp[u]) { n     if (vis[v]) continue; n     DFS2(v, u); n   } n } n void ford() { n   while (1) { n     memset(par, -1, sizeof(par)); n     par[0] = -2; n     DFS(0); n     int flow = augment(201, 1000000); n     if (flow == 0) break; n     macflow += flow; n   } n   if (macflow != N) { n     puts( Impossible ); n     return; n   } n   for (int i = 1; i <= N; i++) { n     for (int j = i + 1; j <= N; j++) { n       if (compo[A[i] + A[j]] == 0) { n         if (A[i] & 1) { n           if (cap[j][i] == 1) { n             match(i, j); n           } n         } else { n           if (cap[i][j] == 1) { n             match(i, j); n           } n         } n       } n     } n   } n   for (int i = 1; i <= N; i++) { n     if (vis[i]) continue; n     pa++; n     DFS2(i, 0); n   } n   printf( %d n , pa); n   for (int i = 1; i <= pa; i++) { n     printf( %d  , (int)res[i].size()); n     for (int now : res[i]) { n       printf( %d  , now); n     } n     putchar(  n ); n   } n   cin >> N; n } n int main() { n   input(); n   ford(); n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> disp[500][500]; n int n, m, a, b; n int pos[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; n class edge { n  public: n   int e, nxt, cap; n   edge(int e1 = -1, int nxt1 = -1, int cap1 = 0) { n     e = e1, nxt = nxt1, cap = cap1; n   } n }; n int S, T; n int dep[1950]; n bool vis[1950]; n class graph { n  public: n   edge g[1000000]; n   int head[1950], tmpcnt; n   int cur[1950]; n   void init() { n     memset(head, 255, sizeof(head)); n     tmpcnt = 0; n   } n   void addedge(int s, int e, int cap) { n     if (cap == 0) return; n     g[tmpcnt] = edge(e, head[s], cap), head[s] = tmpcnt++; n     g[tmpcnt] = edge(s, head[e], 0), head[e] = tmpcnt++; n   } n   bool bfs() { n     memset(vis, 0, sizeof(vis)); n     memset(dep, 0, sizeof(dep)); n     queue<int> q; n     q.push(S); n     vis[S] = 1; n     while (!q.empty()) { n       int fr = q.front(); n       q.pop(); n       for (int i = head[fr]; i != -1; i = g[i].nxt) { n         if (g[i].cap > 0 && !vis[g[i].e]) { n           dep[g[i].e] = dep[fr] + 1; n           vis[g[i].e] = 1; n           q.push(g[i].e); n         } n       } n     } n     return vis[T]; n   } n   int dfs(int np, int maxflow) { n     int f; n     int flow = 0; n     if (np == T || !maxflow) return maxflow; n     for (int i = cur[np]; i != -1; i = g[i].nxt) { n       cur[np] = i; n       if (g[i].cap > 0 && dep[g[i].e] == dep[np] + 1 && n           (f = dfs(g[i].e, min(g[i].cap, maxflow))) > 0) { n         g[i].cap -= f; n         g[i ^ 1].cap += f; n         flow += f; n         maxflow -= f; n         if (!maxflow) break; n       } n     } n     return flow; n   } n   int dinic() { n     int out = 0; n     while (bfs()) memcpy(cur, head, sizeof(cur)), out += dfs(S, 0x3f3f3f3f); n     return out; n   } n } G; n inline int id(int x, int y) { return (x - 1) * m + y; } n int dis[30][30]; n char s[30][30]; n void init(int x, int y) { n   memset(dis, 255, sizeof(dis)); n   int st = id(x, y); n   queue<pair<int, int> > q; n   q.push(make_pair(x, y)); n   dis[x][y] = 0; n   disp[st][0].push_back(st); n   while (!q.empty()) { n     pair<int, int> fr = q.front(); n     q.pop(); n     for (int i = 0; i < 4; i++) { n       int nx = fr.first + pos[i][0], ny = fr.second + pos[i][1]; n       if (s[nx][ny] ==  .  && dis[nx][ny] == -1) { n         dis[nx][ny] = dis[fr.first][fr.second] + 1; n         disp[st][dis[nx][ny]].push_back(id(nx, ny)); n         q.push(make_pair(nx, ny)); n       } n     } n   } n } n class peo { n  public: n   int x, y, t; n } male[500], female[500]; n int main() { n   scanf( %d%d%d%d , &n, &m, &a, &b); n   for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       if (s[i][j] !=  # ) init(i, j); n     } n   } n   if (b == a + 1) { n     scanf( %d%d%d , &male[a + 1].x, &male[a + 1].y, &male[a + 1].t); n   } else if (a == b + 1) { n     scanf( %d%d%d , &female[b + 1].x, &female[b + 1].y, &female[b + 1].t); n   } else { n     puts( -1 ); n     return 0; n   } n   int K = max(a, b); n   for (int i = 1; i <= a; i++) n     scanf( %d%d%d , &male[i].x, &male[i].y, &male[i].t); n   for (int i = 1; i <= b; i++) n     scanf( %d%d%d , &female[i].x, &female[i].y, &female[i].t); n   long long L = 0, R = 1ll << 40, ans = -1; n   S = 2 * (K + n * m) + 1; n   T = 2 * (K + n * m) + 2; n   while (L <= R) { n     long long mid = (L + R) >> 1; n     G.init(); n     for (int i = 1; i <= K; i++) { n       G.addedge(S, i + 2 * n * m, 1); n       G.addedge(i + K + 2 * n * m, T, 1); n       int pm = id(male[i].x, male[i].y); n       for (int j = 0; j <= mid / male[i].t && j <= n * m; j++) { n         for (auto &x : disp[pm][j]) G.addedge(i + 2 * n * m, x, 1); n       } n       pm = id(female[i].x, female[i].y); n       for (int j = 0; j <= mid / female[i].t && j <= n * m; j++) { n         for (auto &x : disp[pm][j]) G.addedge(x + n * m, i + K + 2 * n * m, 1); n       } n     } n     for (int i = 1; i <= n * m; i++) G.addedge(i, i + n * m, 1); n     if (G.dinic() != K) n       L = mid + 1; n     else n       ans = mid, R = mid - 1; n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[1000005], B[1000005]; n int ans = 0; n int C[35]; n int main() { n   int n, m, i, j, k, l; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n; i++) { n     scanf( %d , &A[i]); n     long long t = 1; n     for (j = 0; j < 33; j++) { n       C[j] += ((A[i] & t) != 0); n       t = (t << 1); n     } n   } n   for (j = 1; j <= m; j++) { n     scanf( %d , &B[j]); n   } n   sort(B + 1, B + 1 + m); n   for (i = 1; i <= m; i++) { n     for (j = B[i]; j < 33; j++) n       if (C[j]) break; n     if (j < 33) { n       for (k = j - 1; k >= B[i]; k--) C[k]++; n       C[j]--; n       ans++; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e9; n const int mod = (int)1e9 + 7; n const int MAXN = (int)3e5 + 5; n vector<long long> primes[1000005]; n bool vis[1000005]; n long long x, p, k; n vector<pair<int, int> > v(1 << 7); n long long calc(long long a) { n   int cnt = 1; n   int num = 0; n   long long tmp = a; n   a = 0; n   for (int i = 1; i < (1 << primes[p].size()); i++) { n     if (cnt * 2 == i) { n       cnt *= 2; n       num++; n     } n     v[i] = {v[i - cnt].first * primes[p][num], v[i - cnt].second + 1}; n     if (v[i].second % 2) { n       a += tmp / v[i].first; n     } else { n       a -= tmp / v[i].first; n     } n   } n   return tmp - a; n } n void solve() { n   cin >> x >> p >> k; n   k += calc(x); n   long long l = x + 1, r = 1e8; n   while (l < r) { n     long long m = (l + r) / 2; n     if (calc(m) >= k) { n       r = m; n     } else { n       l = m + 1; n     } n   } n   cout << l <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   for (long long i = 2; i < 1000005; i++) { n     if (!vis[i]) { n       for (long long j = i; j < 1000005; j += i) { n         primes[j].push_back(i); n         vis[j] = 1; n       } n     } n   } n   v[0] = {1, 0}; n   int t = 1; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1e2 + 10; n const long long mod = 1e9 + 7; n const long long INF = 1e9; n inline long long bpow(long long t, long long n) { n   long long ans = 1; n   while (n > 0) { n     if (n & 1) ans = (ans * t) % mod; n     t = (t * t) % mod, n >>= 1; n   } n   return ans; n } n int a[MAX_N][MAX_N]; n int row[MAX_N]; n int col[MAX_N]; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cout << fixed << setprecision(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) cin >> a[i][j]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) row[i] += a[i][j], col[j] += a[i][j]; n   long long ans = 0; n   for (int i = 1; i <= n; i++) n     ans += (1ll << row[i]) - 1 + (1ll << (m - row[i])) - 1; n   for (int j = 1; j <= m; j++) n     ans += (1ll << col[j]) - 1 + (1ll << (n - col[j])) - 1; n   ans -= n * m; n   cout << ans; n } n


#include<bits/stdc++.h> n  n using namespace std; n  n using ll = long long int; n using vi = vector<int>; n #define f(n) for(int i = 0; i < n; i++) n #define fj(n) for(int j = 0; j < n; j++) n #define fk(n) for(int k = 0; k < n; k++) n #define ci(n) cin >> n; n #define ic(n) int n; cin >> n; n #define lc(n) int n; cin >> n; n #define all(a) a.begin(), a.end() n #define pri(a, n) for(int i = 0; i < n; i++) cout << a[i] <<    ; cout <<   n ; n #define sc(a, n) int a[n] = {0}; for(int i = 0; i < n; i++) cin >> a[i]; n #define scl(a, n) ll a[n] = {0}; for(int i = 0; i < n; i++) cin >> a[i]; n #define pb push_back n #define eb emplace_back n #define pii pair<int, int> n #define ft first n //#define int long long n const int inf = 1e9, maxn = 2e5, mod = 1e9 + 7; n vector<int> tps; n int used[maxn] = {0}, cols[maxn] = {0}; n  n void DFS(vector<int> g[], int v) n { n     used[v] = 1; n     for(int u : g[v]) n     { n         if(!used[u]) DFS(g, u); n     } n     tps.push_back(v); n } n  n void DFS2(vector<int> g[], int v, int col) n { n     cols[v] = col; n     for(int u : g[v]) n     { n         if(cols[u] == 0) DFS2(g, u, col); n     } n } n  n  n signed main() n { n     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); n     int n, m, k; n     cin >> n >> m >> k; n     string t[n], s[m]; n     int mt[m]; n     f(n) cin >> t[i]; n     f(m) cin >> s[i] >> mt[i]; n     map<string, int> mp; n     f(n) mp[t[i]] = i + 1; n     vector<int> g[n], ag[n]; n     f(m) { n         vector<int> ls; n         string cs = s[i]; n         fj(1 << k) { n             cs = s[i]; n             for(int id = 0; id < k; id++) { n                 if((j >> id) & 1) cs[id] =  _ ; n             } n             if(mp[cs]) ls.pb(mp[cs] - 1); n         } n         if(find(all(ls), mt[i] - 1) == ls.end()) { n             cout <<  NO ; n             return 0; n         } n         for(int u : ls) { n             if(u != mt[i] - 1) { n                 g[mt[i] - 1].pb(u); n                 ag[u].pb(mt[i] - 1); n             } n         } n     } n     int id = 0; n     while(id < n) n     { n         DFS(g, id); n         while(id < n && used[id] == 1) id++; n     } n     reverse(begin(tps), end(tps)); n     id = 0; n     int col = 1; n     while(id < n) n     { n         DFS2(ag, tps[id], col); n         col++; n         while(id < n && cols[tps[id]] > 0) id++; n     } n     //pri(cols, n) n     if(col == n + 1) { n         cout <<  YES n ; n         f(n) cout << tps[i] + 1 <<    ; n     } else { n         cout <<  NO n ; n  n     } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 1; n struct data { n   int l, r, sum; n } tree[N << 2], sum_tree[N << 2]; n struct node { n   int to, next; n } a[N << 1]; n int ans1 = 0, ans2 = 0, dp[N], head[N], st[N], ed[N], g[N][25], bx[N], by[N], n     bw[N], deep[N]; n int n, m, cnt = 0; n vector<int> qx[N]; n void add(int x, int y) { n   a[++cnt].to = y, a[cnt].next = head[x], head[x] = cnt; n   a[++cnt].to = x, a[cnt].next = head[y], head[y] = cnt; n } n void dfs(int u, int fa) { n   st[u] = ++cnt; n   deep[u] = deep[fa] + 1; n   g[u][0] = fa; n   for (int i = head[u]; i != -1; i = a[i].next) { n     if (a[i].to != fa) dfs(a[i].to, u); n   } n   ed[u] = cnt; n } n void yyqx() { n   for (int i = 1; i < 25; ++i) n     for (int j = 1; j <= n; ++j) g[j][i] = g[g[j][i - 1]][i - 1]; n } n int GetLCA(int x, int y) { n   if (deep[x] > deep[y]) swap(x, y); n   for (int i = 24; i >= 0; --i) n     if (deep[y] - (1 << i) >= deep[x]) y = g[y][i]; n   if (x == y) return x; n   for (int i = 24; i >= 0; --i) n     if (g[x][i] != g[y][i]) x = g[x][i], y = g[y][i]; n   return g[x][0]; n } n void build(int index, int left, int right) { n   int mid = (left + right) >> 1; n   tree[index].sum = 0, sum_tree[index].sum = 0; n   tree[index].l = left, tree[index].r = right; n   sum_tree[index].l = left, sum_tree[index].r = right; n   if (left == right) return; n   build(index * 2, left, mid); n   build(index * 2 + 1, mid + 1, right); n } n void change(int index, int left, int right, int d) { n   if (left > right) return; n   if (tree[index].l >= left && tree[index].r <= right) { n     tree[index].sum += d; n     return; n   } n   if (tree[index * 2].r >= left) change(index * 2, left, right, d); n   if (tree[index * 2 + 1].l <= right) change(index * 2 + 1, left, right, d); n } n void search(int index, int x) { n   ans2 += tree[index].sum; n   if (tree[index].l == tree[index].r) return; n   if (x <= tree[index * 2].r) search(index * 2, x); n   if (x >= tree[index * 2 + 1].l) search(index * 2 + 1, x); n } n void sum_change(int index, int left, int right, int d) { n   if (left > right) return; n   if (sum_tree[index].l >= left && sum_tree[index].r <= right) { n     sum_tree[index].sum += d; n     return; n   } n   if (sum_tree[index * 2].r >= left) sum_change(index * 2, left, right, d); n   if (sum_tree[index * 2 + 1].l <= right) n     sum_change(index * 2 + 1, left, right, d); n } n void sum_search(int index, int x) { n   ans1 += sum_tree[index].sum; n   if (sum_tree[index].l == sum_tree[index].r) return; n   if (x <= sum_tree[index * 2].r) sum_search(index * 2, x); n   if (x >= sum_tree[index * 2 + 1].l) sum_search(index * 2 + 1, x); n } n void work(int u, int fa) { n   int sum = 0; n   for (int i = head[u]; i != -1; i = a[i].next) { n     if (a[i].to != fa) { n       work(a[i].to, u); n       sum += dp[a[i].to]; n     } n   } n   sum_change(1, st[u], ed[u], sum); n   dp[u] = sum; n   for (int i = 0; i < qx[u].size(); ++i) { n     int x = bx[qx[u][i]], y = by[qx[u][i]], w = bw[qx[u][i]]; n     int val = 0; n     ans1 = 0; n     sum_search(1, st[x]); n     val += ans1; n     ans1 = 0; n     sum_search(1, st[y]); n     val += ans1; n     val -= sum; n     ans2 = 0; n     search(1, st[x]); n     val -= ans2; n     ans2 = 0; n     search(1, st[y]); n     val -= ans2; n     dp[u] = max(dp[u], val + w); n   } n   ans1 = 0, ans2 = 0; n   change(1, st[u], ed[u], dp[u]); n } n int main() { n   int x, y, w; n   scanf( %d%d , &n, &m); n   memset(head, -1, sizeof(head)); n   for (int i = 1; i < n; ++i) { n     scanf( %d , &x); n     add(x, i + 1); n   } n   cnt = 0; n   dfs(1, 0); n   yyqx(); n   for (int i = 1; i <= m; ++i) { n     scanf( %d%d%d , &x, &y, &w); n     bx[i] = x, by[i] = y, bw[i] = w; n     qx[GetLCA(x, y)].push_back(i); n   } n   build(1, 1, n); n   work(1, 0); n   printf( %d n , dp[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, ans; n string str; n long long solve() { n   long long k; n   cin >> n >> k; n   long long t = 3 * n; n   while (t > k) { n     t--; n     ans++; n   } n   cout << ans; n   return 0; n } n signed main() { n   ios::sync_with_stdio(0); n   long long t = 1; n   while (t--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, hours, mins; n   cin >> s; n   int a = 1, b = 1, colon, i, radix = 0; n   for (i = 0; i < s.length(); i++) { n     if (s[i] ==  : ) { n       colon = i; n       break; n     } n   } n   for (i = 0; i < colon; i++) { n     if (s[i] !=  0 ) { n       break; n     } n   } n   hours = s.substr(i, colon - i); n   for (i = colon + 1; i < s.length(); i++) { n     if (s[i] !=  0 ) { n       break; n     } n   } n   mins = s.substr(i); n   if (hours.length() <= 1 and mins.length() <= 1) { n     hours.length() == 1 and hours[0] -  A  + 10 >= 24 ? cout << 0 : cout << -1; n     return 0; n   } n   for (i = 0; i < s.length(); i++) { n     if (i != colon) { n       int cur = s[i] >=  0  and s[i] <=  9  ? s[i] -  0  : s[i] -  A  + 10; n       radix = max(radix, cur); n     } n   } n   radix++; n   if (radix == 1) { n     radix = 2; n   } n   for (i = radix;; i++) { n     int HOURS = 0, MINS = 0; n     for (int j = 0; j < hours.length(); j++) { n       HOURS *= i; n       HOURS += hours[j] >=  0  and hours[j] <=  9  ? hours[j] -  0  n                                                    : hours[j] -  A  + 10; n     } n     for (int j = 0; j < mins.length(); j++) { n       MINS *= i; n       MINS += mins[j] >=  0  and mins[j] <=  9  ? mins[j] -  0  n                                                 : mins[j] -  A  + 10; n     } n     if (HOURS <= 23 and MINS <= 59) { n       cout << i <<    ; n     } else { n       if (i == radix) { n         cout << 0; n       } n       return 0; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n static const int INF = 0x3f3f3f3f; n static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; n template <typename T, typename U> n static void amin(T &x, U y) { n   if (y < x) x = y; n } n template <typename T, typename U> n static void amax(T &x, U y) { n   if (x < y) x = y; n } n const double PI = 3.141592653589793238462643383279L; n void fft_core(complex<double> *x, int logn, int sign) { n   int n = 1 << logn; n   for (int i = 1, j = 0; i < n; ++i) { n     int h = n >> 1; n     while (((j ^= h) & h) == 0) h >>= 1; n     if (i < j) swap(x[i], x[j]); n   } n   for (int m = 2; m <= n; m <<= 1) { n     int h = m >> 1; n     double theta = sign * 2 * PI / m; n     double pr = cos(theta), pi = sin(theta); n     for (int i = 0; i < n; i += m) { n       double wr = 1, wi = 0; n       for (int j = i; j < i + h; ++j) { n         int k = j + h; n         double lr = x[k].real() * wr - x[k].imag() * wi; n         double li = x[k].real() * wi + x[k].imag() * wr; n         x[k] = complex<double>(x[j].real() - lr, x[j].imag() - li); n         x[j] = complex<double>(x[j].real() + lr, x[j].imag() + li); n         double nwr = wr * pr - wi * pi; n         double nwi = wr * pi + wi * pr; n         wr = nwr, wi = nwi; n       } n     } n   } n } n void fft(int logn, complex<double> a[]) { fft_core(a, logn, +1); } n void inverse_fft(int logn, complex<double> a[]) { n   fft_core(a, logn, -1); n   complex<double> inv = double(1) / (1 << logn); n   for (int(i) = 0; (i) < (int)(1 << logn); ++(i)) a[i] *= inv; n } n using Ring = complex<double>; n void FFT2D(int logh, int H, int logw, int W, vector<vector<Ring>> &A) { n   assert(A.size() == 1 << logh); n   for (int(i) = 0; (i) < (int)(H); ++(i)) { n     assert(A[i].size() == 1 << logw); n     fft(logw, A[i].data()); n   } n   vector<Ring> tmp(1 << logh); n   for (int(j) = 0; (j) < (int)(1 << logw); ++(j)) { n     for (int(i) = 0; (i) < (int)(1 << logh); ++(i)) tmp[i] = A[i][j]; n     fft(logh, tmp.data()); n     for (int(i) = 0; (i) < (int)(1 << logh); ++(i)) A[i][j] = tmp[i]; n   } n } n void FFT2Dinv(int logh, int H, int logw, int W, vector<vector<Ring>> &A) { n   vector<Ring> tmp(1 << logh); n   for (int(j) = 0; (j) < (int)(1 << logw); ++(j)) { n     for (int(i) = 0; (i) < (int)(1 << logh); ++(i)) tmp[i] = A[i][j]; n     inverse_fft(logh, tmp.data()); n     for (int(i) = 0; (i) < (int)(1 << logh); ++(i)) A[i][j] = tmp[i]; n   } n   for (int(i) = 0; (i) < (int)(H); ++(i)) inverse_fft(logw, A[i].data()); n } n int main() { n   int tH; n   int tW; n   while (~scanf( %d%d , &tH, &tW)) { n     vector<string> table(tH); n     for (int(i) = 0; (i) < (int)(tH); ++(i)) { n       char buf[401]; n       scanf( %s , buf); n       table[i] = buf; n     } n     int pH; n     int pW; n     scanf( %d%d , &pH, &pW); n     vector<string> pattern(pH); n     for (int(i) = 0; (i) < (int)(pH); ++(i)) { n       char buf[401]; n       scanf( %s , buf); n       pattern[i] = buf; n     } n     int H = tH + pH, W = tW + pW; n     int logh = 1, logw = 1; n     while (1 << logh < H) ++logh; n     while (1 << logw < W) ++logw; n     vector<vector<Ring>> A(1 << logh, vector<Ring>(1 << logw)), B = A; n     for (int(i) = 0; (i) < (int)(tH + pH); ++(i)) n       for (int(j) = 0; (j) < (int)(tW + pW); ++(j)) { n         char c = table[i % tH][j % tW]; n         double theta = 2 * PI * (c -  a ) / 26; n         A[tH + pH - 1 - i][tW + pW - 1 - j] = polar(double(1), theta); n       } n     for (int(i) = 0; (i) < (int)(pH); ++(i)) n       for (int(j) = 0; (j) < (int)(pW); ++(j)) { n         char c = pattern[i][j]; n         if (c ==  ? ) continue; n         double theta = -2 * PI * (c -  a ) / 26; n         B[i][j] = polar(double(1), theta); n       } n     FFT2D(logh, H, logw, W, A); n     FFT2D(logh, H, logw, W, B); n     for (int(i) = 0; (i) < (int)(1 << logh); ++(i)) n       for (int(j) = 0; (j) < (int)(1 << logw); ++(j)) A[i][j] *= B[i][j]; n     FFT2Dinv(logh, H, logw, W, A); n     int qs = 0; n     for (int(i) = 0; (i) < (int)(pH); ++(i)) n       for (int(j) = 0; (j) < (int)(pW); ++(j)) qs += pattern[i][j] ==  ? ; n     vector<string> ans(tH, string(tW,  ? )); n     for (int(i) = 0; (i) < (int)(tH); ++(i)) n       for (int(j) = 0; (j) < (int)(tW); ++(j)) { n         Ring r = A[tH + pH - 1 - i][tW + pW - 1 - j]; n         ans[i][j] = r.real() >= (pH * pW - qs) - 1e-6 ?  1  :  0 ; n       } n     for (int(i) = 0; (i) < (int)(tH); ++(i)) puts(ans[i].c_str()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int sett(long long 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 void update(long long &x, long long val) { n   x = x + val; n   if (x > 1000000007) x -= 1000000007; n } n void fast() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL), cout.tie(NULL); n } n long long int poww(long long int a, long long int b) { n   long long int ans = 1; n   while (b--) { n     ans *= a; n   } n   return ans; n } n int moves[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; n char a[300][300]; n long long int num[300][300]; n long long int ans[300][300]; n int n, m; n long long int p, q; n int vis[250][250]; n int valid(int i, int j) { n   if (i < 0 || i >= n || j < 0 || j >= m) return 0; n   if (a[i][j] ==  * ) return 0; n   return 1; n } n void dfs(int i, int j, long long int val, int dfs_cnt) { n   cout <<  now in      << i <<     << j << endl; n   vis[i][j] = dfs_cnt; n   ans[i][j] += val; n   long long int next_level = val / 2; n   if (!next_level) return; n   for (int k = 0; k < 4; k++) { n     int next_i = i + moves[k][0]; n     int next_j = j + moves[k][1]; n     if (valid(next_i, next_j) && vis[next_i][next_j] != dfs_cnt) { n       dfs(next_i, next_j, next_level, dfs_cnt); n     } n   } n } n void bfs(int i, int j, long long int val, int bfs_cnt) { n   queue<pair<pair<int, int>, long long int> > q; n   q.push(make_pair(make_pair(i, j), val)); n   vis[i][j] = bfs_cnt; n   while (!q.empty()) { n     pair<pair<int, int>, long long int> frnt; n     frnt = q.front(); n     q.pop(); n     int ii = frnt.first.first; n     int jj = frnt.first.second; n     long long int val = frnt.second; n     ans[ii][jj] += val; n     long long int next_level = val / 2; n     if (next_level == 0) continue; n     for (int k = 0; k < 4; k++) { n       int next_i = ii + moves[k][0]; n       int next_j = jj + moves[k][1]; n       if (valid(next_i, next_j) && vis[next_i][next_j] != bfs_cnt) { n         vis[next_i][next_j] = bfs_cnt; n         pair<pair<int, int>, long long int> temp; n         temp = make_pair(make_pair(next_i, next_j), next_level); n         q.push(temp); n       } n     } n   } n } n bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { n   if (a.first.first > b.first.first) n     return 1; n   else if (a.first.first == b.first.first) { n     if (a.first.second < b.first.second) return 1; n   } n   return 0; n } n int main() { n   int n, k, m, a; n   scanf( %d%d , &n, &k); n   scanf( %d%d , &m, &a); n   int vote_time[200][2]; n   memset(vote_time, 0, sizeof(vote_time)); n   int cnt = n; n   for (int i = 1; i <= a; i++) { n     int p; n     scanf( %d , &p); n     vote_time[p][0]++; n     vote_time[p][1] = i; n     if (vote_time[p][0] == 1) cnt--; n   } n   int ans[n + 10]; n   memset(ans, 0, sizeof(ans)); n   if (m == 0) { n     for (int i = 1; i <= n; i++) printf( 3  ); n     return 0; n   } n   int flag0 = 0; n   for (int i = 1; i <= n; i++) { n     if (vote_time[i][0] == 0 && flag0 == 3) { n       ans[i] = 3; n       continue; n     } n     int candidates_vote = vote_time[i][0] + (m - a); n     int candidates_time = vote_time[i][1]; n     if (m - a > 0) candidates_time = m; n     if (candidates_vote == 0) { n       ans[i] = 3; n       flag0 = 3; n     } n     vector<pair<pair<int, int>, int> > bal; n     pair<pair<int, int>, int> temp = n         make_pair(make_pair(candidates_vote, candidates_time), i); n     bal.push_back(temp); n     for (int j = 1; j <= n; j++) { n       if (i != j) { n         temp = make_pair(make_pair(vote_time[j][0], vote_time[j][1]), j); n         bal.push_back(temp); n       } n     } n     sort(bal.begin(), bal.end(), cmp); n     int pos = m; n     for (int j = 0; j < n; j++) { n       if (bal[j].second == i) { n         pos = j + 1; n         break; n       } n     } n     if (pos > k) { n       ans[i] = 3; n       if (vote_time[i][0] == 0) { n         flag0 = 3; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (ans[i] > 0) continue; n     if (vote_time[i][0] == 0) { n       ans[i] == 2; n       continue; n     } n     vector<pair<pair<int, int>, int> > bal; n     pair<pair<int, int>, int> temp; n     for (int j = 1; j <= n; j++) { n       temp = make_pair(make_pair(vote_time[j][0], vote_time[j][1]), j); n       bal.push_back(temp); n     } n     sort(bal.begin(), bal.end(), cmp); n     int pos = m; n     for (int j = 0; j < n; j++) { n       if (bal[j].second == i) { n         pos = j + 1; n         break; n       } n     } n     int sorte_hobe = k - pos + 1; n     if (k == n) { n       ans[i] = 1; n       continue; n     } n     int tot_need = 0; n     for (int j = pos; j < pos + sorte_hobe; j++) { n       int gap = bal[pos - 1].first.first - bal[j].first.first + 1; n       tot_need += gap; n     } n     int baki_ase = m - a; n     if (tot_need > baki_ase) { n       ans[i] = 1; n     } n     if (tot_need == 0 && baki_ase == 0) { n       ans[i] = 1; n     } n   } n   for (int i = 1; i <= n; i++) { n     if (ans[i] == 0) n       printf( 2  ); n     else n       printf( %d  , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC diagnostic ignored  -Wmissing-declarations  n inline int safe_mul(const int x, const int y) n     __attribute__((warn_unused_result)); n int const mod = 998244353; n inline int safe_mul(const int x, const int y) { n   return x * static_cast<long long>(y) % mod; n } n inline void safe_add(int& x, const int y) { n   x += y; n   if (x >= mod) x -= mod; n } n inline int safe_pow(int x, int y) { n   int res = 1; n   for (; y > 0; y >>= 1) { n     if (y & 1) res = safe_mul(x, res); n     x = safe_mul(x, x); n   } n   return res; n } n const int root_pw = 1 << 21; n int root; n int root_1; n void fft(vector<int>& a, bool invert) { n   int n = (int)a.size(); n   for (int i = 1, j = 0; i < n; ++i) { n     int bit = n >> 1; n     for (; j >= bit; bit >>= 1) j -= bit; n     j += bit; n     if (i < j) swap(a[i], a[j]); n   } n   for (int len = 2; len <= n; len <<= 1) { n     int wlen = invert ? root_1 : root; n     for (int i = len; i < root_pw; i <<= 1) wlen = safe_mul(wlen, wlen); n     for (int i = 0; i < n; i += len) { n       int w = 1; n       for (int j = 0; j < len / 2; ++j) { n         int u = a[i + j], v = safe_mul(a[i + j + len / 2], w); n         a[i + j] = u + v < mod ? u + v : u + v - mod; n         a[i + j + len / 2] = u - v >= 0 ? u - v : u - v + mod; n         w = safe_mul(w, wlen); n       } n     } n   } n   if (invert) { n     int nrev = safe_pow(n, mod - 2); n     for (int i = 0; i < n; ++i) a[i] = safe_mul(a[i], nrev); n   } n } n int need; n vector<int> multiply(vector<int> a, vector<int> b) { n   size_t n = 1; n   while (n < max(a.size(), b.size())) n <<= 1; n   n <<= 1; n   a.resize(n, 0), b.resize(n, 0); n   fft(a, false), fft(b, false); n   for (size_t i = 0; i < n; ++i) a[i] = safe_mul(a[i], b[i]); n   fft(a, true); n   while (!a.empty() && (a.back() == 0 || a.size() > need + 1)) a.pop_back(); n   return a; n } n int solve(map<int, int> primes) { n   auto comp = [](const vector<int>& l, const vector<int>& r) { n     return l.size() < r.size(); n   }; n   multiset<vector<int>, decltype(comp)> polys(comp); n   for (auto pa : primes) polys.insert(vector<int>(pa.second + 1, 1)); n   while (polys.size() > 1) { n     auto vec = multiply(*polys.begin(), *next(polys.begin())); n     polys.erase(polys.begin()); n     polys.erase(polys.begin()); n     polys.insert(move(vec)); n   } n   return polys.begin()->operator[](need); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   for (int i = 1; i < mod; ++i) { n     if (safe_pow(i, root_pw) == 1 && safe_pow(i, root_pw >> 1) != 1) { n       root = i; n       root_1 = safe_pow(root, mod - 2); n       break; n     } n   } n   int n; n   cin >> n; n   need = n / 2; n   map<int, int> cnt; n   while (n--) { n     int p; n     cin >> p; n     ++cnt[p]; n   } n   cout << solve(cnt) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool isprime(int n) { n   if (n < 2) return false; n   for (int i = 2; i * i <= n; i++) n     if (n % i == 0) return false; n   return true; n } n int main() { n   int n; n   cin >> n; n   if (isprime(n)) n     cout <<  1  << endl; n   else if (n % 2 == 0) n     cout <<  2  << endl; n   else if (isprime(n - 2)) n     cout <<  2  << endl; n   else n     cout <<  3  << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n long long int fact[2000005], ifact[2000005]; n long long int ppow(long long int b, int p) { n   long long int ret = 1; n   for (; p; p /= 2) { n     if (p & 1) ret = (ret * b) % 1000000007; n     b = (b * b) % 1000000007; n   } n   return ret; n } n void init_fact() { n   fact[0] = 1; n   for (int i = 1; i < 2000005; i++) fact[i] = (fact[i - 1] * i) % 1000000007; n   ifact[2000005 - 1] = ppow(fact[2000005 - 1], 1000000007 - 2); n   for (int i = 2000005 - 2; i >= 0; i--) n     ifact[i] = (ifact[i + 1] * (i + 1)) % 1000000007; n } n int main() { n   init_fact(); n   int n, m, g; n   scanf( %d%d%d , &n, &m, &g); n   long long int ans = 0; n   if (n == 0) { n     if (m == 1 and g == 1) n       ans = 1; n     else if (m > 1 and g == 0) n       ans = 1; n   } else if (m == 0) { n     if (n == 1 and g == 0) n       ans = 1; n     else if (n > 1) { n       if (n % 2 == 0 and g == 1) n         ans = 1; n       else if (n % 2 == 1 and g == 0) n         ans = 1; n     } n   } else { n     for (int start = 1; start <= n + 1; start++) { n       if (start == n + 1 and m == 1) { n         if (g == 0 and (n + m) % 2 == 0) n           ans++; n         else if (g == 1 and (n + m) % 2 == 1) n           ans++; n         continue; n       } n       if ((start + g) & 1) { n         long long int val = fact[m - 1 + n - start + 1]; n         val = (val * ifact[m - 1]) % 1000000007; n         val = (val * ifact[n - start + 1]) % 1000000007; n         ans = (ans + val) % 1000000007; n       } n     } n   } n   cout << ans % 1000000007 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n long long power(long long a, long long b) { n   if (b == 0) n     return 1; n   else if (b == 1) { n     return a; n   } else if (b & 1) { n     return (a % mod * power((a % mod * a % mod) % mod, b / 2)) % mod; n   } else { n     return (power((a % mod * a % mod) % mod, b / 2)) % mod; n   } n } n void solve() { n   int n, m, a, b; n   cin >> n >> m; n   vector<int> adj[n + 1]; n   vector<int> deg(n + 1, 0), ans, dis(n + 1, 0), odd, even; n   for (long long i = 0; i < m; i++) { n     cin >> a >> b; n     adj[a].push_back(b); n     adj[b].push_back(a); n     deg[a]++; n     deg[b]++; n   } n   queue<pair<int, int> > q; n   vector<bool> vis(n + 1, false); n   q.push({1, 0}); n   vis[1] = 1; n   while (!q.empty()) { n     auto it = q.front(); n     q.pop(); n     dis[it.first] = it.second; n     for (int j : adj[it.first]) { n       if (!vis[j]) { n         vis[j] = 1; n         q.push({j, dis[it.first] + 1}); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (dis[i] & 1) { n       odd.push_back(i); n     } else { n       even.push_back(i); n     } n   } n   if (odd.size() > even.size()) { n     cout << even.size() << endl; n     for (int i = 0; i < even.size(); i++) { n       cout << even[i] <<    ; n     } n     cout << endl; n   } else { n     cout << odd.size() << endl; n     for (int i = 0; i < odd.size(); i++) { n       cout << odd[i] <<    ; n     } n     cout << endl; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000; n int a, b, m, n, C[MAXN], I[MAXN]; n queue<int> Q; n vector<int> A, B, E[MAXN]; n int main() { n   cin >> n >> m; n   while (m--) { n     cin >> a >> b; n     --a; n     --b; n     E[a].push_back(b); n     E[b].push_back(a); n     I[a] = I[b] = 1; n   } n   for (int i = 0; i < n; ++i) { n     C[i] = -1; n   } n   for (int i = 0; i < n; ++i) { n     if (C[i] > -1 || !I[i]) { n       continue; n     } n     C[i] = 1; n     Q.push(i); n     while (!Q.empty()) { n       a = Q.front(); n       Q.pop(); n       for (auto j : E[a]) { n         if (C[j] == -1) { n           C[j] = !C[a]; n           Q.push(j); n         } else if (C[j] == C[a]) { n           cout << -1 << endl; n           return 0; n         } n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     if (C[i] == 0) { n       A.push_back(i); n     } else if (C[i] == 1) { n       B.push_back(i); n     } n   } n   cout << A.size() << endl; n   for (auto i : A) { n     cout << i + 1 <<    ; n   } n   cout << endl; n   cout << B.size() << endl; n   for (auto i : B) { n     cout << i + 1 <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e7 + 5; n const int maxm = 2e5 + 5; n const int inf = 0x3f3f3f3f; n const long long mod = 1e9 + 7; n const double eps = 1e-9; n const double pi = acos(-1); n int m, n, w, k; n void init() {} n bool judge(int now) {} n long long quickpow(long long a, long long b) { n   long long ans = 1; n   while (b) { n     if (b & 1) ans = ans * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return ans; n } n char c[1005]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     scanf( %d , &n); n     scanf( %s , c); n     int first = -1, second = -1; n     for (int i = 0; i < n; i++) n       if (c[i] ==  1 ) { n         first = i + 1; n         break; n       } n     for (int i = n - 1; i >= 0; i--) n       if (c[i] ==  1 ) { n         second = i + 1; n         break; n       } n     if (first == -1) n       printf( %d n , n); n     else n       printf( %d n , 2 * max(second, n - first + 1)); n   } n } n


#include <bits/stdc++.h> n template <class T> n int getbit(T s, int i) { n   return (s >> i) & 1; n } n template <class T> n T onbit(T s, int i) { n   return s | (T(1) << i); n } n template <class T> n T offbit(T s, int i) { n   return s & (~(T(1) << i)); n } n template <class T> n int cntbit(T s) { n   return __builtin_popcount(s); n } n template <class T> n T gcd(T a, T b) { n   T r; n   while (b != 0) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n template <class T> n T lcm(T a, T b) { n   return a / gcd(a, b) * b; n } n using namespace std; n const int MAXN = 2e5 + 100; n const int MOD = 1e9 + 7; n const double eps = 1e-12; n const long long INF = 1e16; n inline string toStr(long long x) { n   string tmp =   ; n   do tmp = char(x % 10 +  0 ) + tmp; n   while (x /= 10); n   return tmp; n } n inline long long toInt(string s) { n   long long res = 0; n   for (auto x : s) res = res * 10 + x -  0 ; n   return res; n } n int main() { n   int n, x, mmax = 0; n   cin >> n; n   for (int i = 0, _a = (n); i < _a; ++i) cin >> x, mmax = max(mmax, x); n   cout << (mmax ^ x); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)(1e6); n char S[N + 1], T[N + 1]; n vector<int> multiply(vector<int> a, vector<int> b) { n   int n = (int)a.size(); n   vector<int> ret; n   for (int i = 0; i < n; i++) ret.push_back(a[b[i]]); n   return ret; n } n vector<int> pow(vector<int> a, int b) { n   int n = (int)a.size(); n   vector<int> ret; n   if (b == 0) { n     for (int i = 0; i < n; i++) ret.push_back(i); n     return ret; n   } n   if (b % 2 == 1) return multiply(pow(a, b - 1), a); n   ret = pow(a, b / 2); n   return multiply(ret, ret); n } n void solve(int k, int d, int n) { n   vector<int> P, vet, ret; n   for (int i = 0; i < d; i++) n     for (int j = i; j < k; j += d) n       if (j > 0) P.push_back(j); n   P.push_back(0); n   for (int i = 0; i < k; i++) vet.push_back(i); n   for (int i = 0, x = 0; i + k <= n; i++) { n     ret.push_back(vet[x]); n     vet[x] = i + k; n     x = P[x]; n   } n   vector<int> aux = pow(P, n - k + 1); n   for (int i = 0; i < k - 1; i++) ret.push_back(vet[aux[i]]); n   for (int i = 0; i < n; i++) T[i] = S[i]; n   for (int i = 0; i < n; i++) S[i] = T[ret[i]]; n   printf( %s n , S); n } n int main() { n   scanf( %s , S); n   int t; n   scanf( %d , &t); n   while (t-- > 0) { n     int k, d; n     scanf( %d %d , &k, &d); n     solve(k, d, strlen(S)); 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 (a > b) swap(a, b); n   if (a == 0) return b; n   return gcd(b % a, a); n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t, i; n   cin >> t; n   while (t--) { n     long long n; n     cin >> n; n     long long arr[n]; n     for (i = 0; i < n; i++) { n       cin >> arr[i]; n     } n     long long sum = 0; n     for (i = 0; i < n; i++) { n       if (i % 2 && arr[i] > 0) n         arr[i] *= -1; n       else if (i % 2 == 0 && arr[i] < 0) n         arr[i] *= -1; n     } n     for (i = 0; i < n; i++) { n       cout << arr[i] <<    ; n     } n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long cas = 1; n long long dxx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; n long long dyy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; n long long dx[] = {-1, 0, 0, 1}; n long long dy[] = {0, -1, 1, 0}; n struct info { n   long long mi, pro, cnt; n } tre[100005 * 3]; n long long ai[100005], ans = 0; n void update(long long n, long long be, long long en, long long i, long long j, n             long long v) { n   if (i > en || j < be) return; n   if (be >= i && en <= j) { n     tre[n].cnt++; n     tre[n].pro += v + (be - i); n     return; n   } n   long long m, l, r; n   m = (be + en) / 2; n   l = n * 2; n   r = l + 1; n   if (tre[n].pro >= 1) { n     tre[l].pro += tre[n].pro; n     tre[l].cnt += tre[n].cnt; n     tre[r].pro += tre[n].pro + tre[n].cnt * ((m + 1 - be)); n     tre[r].cnt += tre[n].cnt; n     tre[n].pro = 0; n     tre[n].cnt = 0; n   } n   update(l, be, m, i, j, v); n   update(r, m + 1, en, i, j, v); n } n long long query(long long n, long long be, long long en, long long i) { n   if (ans != -1) return ans; n   if (be == i && en == i) { n     tre[n].cnt++; n     ans = tre[n].pro; n     return tre[n].pro; n   } n   long long m, l, r; n   m = (be + en) / 2; n   l = n * 2; n   r = l + 1; n   long long a, b; n   if (tre[n].pro >= 1) { n     tre[l].pro += tre[n].pro; n     tre[l].cnt += tre[n].cnt; n     tre[r].pro += tre[n].pro + tre[n].cnt * ((m - be + 1)); n     tre[r].cnt += tre[n].cnt; n     tre[n].pro = 0; n     tre[n].cnt = 0; n   } n   if (be <= i && m >= i) return query(l, be, m, i); n   return query(r, m + 1, en, i); n } n int main() { n   memset(tre, 0, sizeof(tre)); n   long long n, m, q, i, k, j, v; n   scanf( %lld%lld%lld , &n, &m, &q); n   for (k = 0; k < m; k++) { n     scanf( %lld%lld%lld , &i, &j, &v); n     update(1, 1, n, i, j, v); n   } n   long long a1 = 0; n   for (i = 1; i <= q; i++) { n     scanf( %d , &k); n     ans = -1; n     ans = query(1, 1, n, k); n     a1 += ans; n   } n   printf( %lld n , a1); n } n


#include <bits/stdc++.h> n using namespace std; n int *a = new int[2000000]; n int m, n; n void add(int x, int l); n void sub(int x, int l); n int query(int x, int y, int x0, int y0, int t); n int main() { n   int h, i, j, dir = 1, r, l, o, x, y, len, mid; n   cin >> n >> m; n   for (i = 0; i < 2000000; i++) a[i] = 0; n   for (i = 1; i <= n; i++) add(i, 1); n   l = 1; n   r = n; n   for (i = 0; i < m; i++) { n     cin >> o; n     if (o == 2) { n       cin >> x >> y; n       x++; n       if (dir == 1) { n         x += l - 1; n         y += l - 1; n         cout << query(1, n, x, y, 1) << endl; n       } else if (dir == 2) { n         x = r - x + 1; n         y = r - y + 1; n         cout << query(1, n, y, x, 1) << endl; n       } n     } else { n       cin >> len; n       if (len <= (r - l + 1) / 2) { n         if (dir == 1) { n           mid = l + len; n           for (j = 1; j <= len; j++) { n             x = mid - j; n             y = mid + j - 1; n             h = query(1, n, x, x, 1); n             add(y, h); n           } n           l += len; n         } else if (dir == 2) { n           mid = r - len; n           for (j = 1; j <= len; j++) { n             x = mid + j; n             y = mid - j + 1; n             h = query(1, n, x, x, 1); n             add(y, h); n           } n           r -= len; n         } n       } else { n         len = r - l + 1 - len; n         if (dir == 2) { n           mid = l + len; n           for (j = 1; j <= len; j++) { n             x = mid - j; n             y = mid + j - 1; n             h = query(1, n, x, x, 1); n             add(y, h); n           } n           l += len; n         } else if (dir == 1) { n           mid = r - len; n           for (j = 1; j <= len; j++) { n             x = mid + j; n             y = mid - j + 1; n             h = query(1, n, x, x, 1); n             add(y, h); n           } n           r -= len; n         } n         if (dir == 1) n           dir = 2; n         else n           dir = 1; n       } n     } n   } n   delete[] a; n   return 0; n } n void add(int x0, int l) { n   int x = 1, y = n, mid, t = 1; n   while (1) { n     a[t] += l; n     mid = (x + y) / 2; n     if (x == y) break; n     if (x0 <= mid) { n       y = mid; n       t = t * 2; n     } else { n       x = mid + 1; n       t = t * 2 + 1; n     } n   } n } n void sub(int x0, int l) { n   int x = 1, y = n, mid, t = 1; n   while (1) { n     a[t] -= l; n     mid = (x + y) / 2; n     if (x == y) break; n     if (x0 <= mid) { n       y = mid; n       t = t * 2; n     } else { n       x = mid + 1; n       t = t * 2 + 1; n     } n   } n } n int query(int x, int y, int x0, int y0, int t) { n   int mid; n   if (y0 < x) return 0; n   if (y < x0) return 0; n   if (x0 <= x && y <= y0) { n     return a[t]; n   } n   mid = (x + y) / 2; n   return query(x, mid, x0, y0, t * 2) + query(mid + 1, y, x0, y0, t * 2 + 1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   const int MAX_N = 100001; n   int n, x; n   vector<long long int> dp(MAX_N, 0); n   vector<long long int> c(MAX_N, 0); n   cin >> n; n   while (n--) { n     cin >> x; n     c[x]++; n   } n   dp[0] = 0; n   dp[1] = c[1]; n   for (int i = 2; i < MAX_N; i++) { n     dp[i] = max(dp[i - 1], dp[i - 2] + i * c[i]); n   } n   cout << dp[MAX_N - 1] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   int rem = a % b; n   while (rem != 0) { n     a = b; n     b = rem; n     rem = a % b; n   } n   return b; n } n int main() { n   int a, b, c, d; n   cin >> a >> b >> c >> d; n   int mul1 = 0, mul2 = 0; n   int res1 = 0, res2 = 0; n   int ans = 0; n   mul1 = a * d; n   mul2 = c * b; n   if (mul1 <= mul2) { n     res1 = mul2 - mul1; n     res2 = mul2; n   } else { n     res1 = mul1 - mul2; n     res2 = mul1; n   } n   ans = gcd(res1, res2); n   if (gcd((res1 / ans), (res2 / ans)) == 1) { n     cout << (res1 / ans) <<  /  << (res2 / ans) << endl; n   } else { n     return -1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n signed main(void) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, x, y; n     cin >> n >> x >> y; n     vector<long long int> ar(n); n     vector<long long int> arr(n + 2, 0); n     vector<long long int> l[n + 2]; n     vector<long long int> ind(n + 2, 0); n     for (long long int(i) = (0); (i) < (n); (i)++) { n       cin >> ar[i]; n       arr[ar[i]] += 1; n       l[ar[i]].push_back(i); n     } n     priority_queue<pair<long long int, long long int>> pq; n     long long int k = -1; n     for (long long int(i) = (1); (i) < (n + 2); (i)++) { n       if (k == -1 && arr[i] == 0) { n         k = i; n       } n       if (arr[i] != 0) { n         pq.push({arr[i], i}); n       } n     } n     vector<long long int> arr2(n, -1); n     for (long long int(i) = (0); (i) < (x); (i)++) { n       pair<long long int, long long int> p1; n       p1 = pq.top(); n       pq.pop(); n       arr2[l[p1.second][ind[p1.second]]] = p1.second; n       ind[p1.second] += 1; n       p1.first -= 1; n       if (p1.first != 0) pq.push(p1); n     } n     vector<long long int> temp; n     for (long long int(i) = (0); (i) < (n); (i)++) { n       if (arr2[i] == -1) { n         temp.push_back(ar[i]); n       } n     } n     long long int d = (n - x) / 2; n     vector<long long int> temp2 = temp; n     sort((temp2).begin(), (temp2).end()); n     map<long long int, long long int> m1; n     for (long long int(i) = (0); (i) < (temp2.size()); (i)++) { n       m1[i] = l[temp2[i]][ind[temp2[i]]]; n       ind[temp2[i]] += 1; n     } n     for (long long int(i) = (0); (i) < (temp2.size()); (i)++) { n       long long int j = (i + d) % temp2.size(); n       arr2[m1[j]] = temp2[i]; n     } n     long long int c = 0; n     for (long long int(i) = (0); (i) < (n); (i)++) { n       if (arr2[i] == ar[i]) { n         x--; n         if (x < 0) { n           c++; n           arr2[i] = k; n         } n       } n     } n     if (c < n - y) { n       for (long long int(i) = (0); (i) < (n); (i)++) { n         if (arr2[i] != k && arr2[i] != ar[i]) { n           c++; n           arr2[i] = k; n         } n         if (c < n - y) n           continue; n         else n           break; n       } n     } n     if (c > n - y) { n       cout <<  NO n ; n     } else { n       cout <<  YES n ; n       for (long long int(i) = (0); (i) < (n); (i)++) { n         cout << arr2[i] <<    ; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 310; n const int mod = 1e9 + 7; n long long C[maxn][maxn], Fact[maxn]; n void init() { n   C[0][0] = 1, Fact[0] = 1; n   for (int i = 1; i < maxn; i++) { n     C[i][0] = 1, Fact[i] = Fact[i - 1] * i % mod; n     for (int j = 1; j <= i; j++) n       C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; n   } n } n long long num[maxn], emp[maxn], dp[maxn][maxn]; n int main() { n   init(); n   int n; n   scanf( %d , &n); n   int tot = 0; n   for (int i = 1; i <= n; i++) { n     long long tmp; n     scanf( %lld , &tmp); n     int key = 1; n     for (int j = 1; j <= tot; j++) { n       long long x = (sqrt(tmp * emp[j]) + 0.5); n       if (x * x == tmp * emp[j]) key = 0, num[j]++; n     } n     if (key) ++tot, num[tot] = 1, emp[tot] = tmp; n   } n   dp[0][0] = 1; n   int m = 0; n   for (int i = 1; i <= tot; i++) { n     for (int j = 0; j <= n - 1 && j <= m + 1; j++) { n       if (dp[i - 1][j] == 0) continue; n       for (int k = 1; k <= num[i]; k++) { n         long long tmp = dp[i - 1][j] * C[num[i] - 1][k - 1] % mod; n         for (int p = 0; p <= k && p <= j + num[i] - k; p++) { n           long long temp = tmp * C[j][p] % mod; n           dp[i][j + num[i] - k - p] = n               (dp[i][j + num[i] - k - p] + temp * C[m + 1 - j][k - p] % mod) % n               mod; n         } n       } n     } n     m += num[i]; n   } n   long long ans = dp[tot][0]; n   for (int i = 1; i <= tot; i++) ans = ans * Fact[num[i]] % mod; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, a, b, p, q, maxx, max_cost, minn, min_cost, t, nok, a1, b1; n   cin >> n >> a >> b >> p >> q; n   if (p > q) { n     max_cost = p; n     maxx = a; n     minn = b; n     min_cost = q; n   } else { n     max_cost = q; n     maxx = b; n     minn = a; n     min_cost = p; n   } n   a1 = maxx; n   b1 = minn; n   while (b1 > 0) { n     t = b1; n     b1 = a1 % b1; n     a1 = t; n   } n   nok = a * b / a1; n   cout << ((n / maxx) * max_cost + (n / minn - n / nok) * min_cost); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class A, class B> n A cvt(B x) { n   stringstream ss; n   ss << x; n   A y; n   ss >> y; n   return y; n } n int n, a, b, t; n string s; n int solve() { n   int tb = 0; n   vector<int> tm; n   for (int k = n; k >= 1; k--) { n     tb += a; n     if (s[k - 1] !=  h ) tb += b; n     tb += 1; n     tm.push_back(tb); n   } n   int best = 0; n   int t1 = 0; n   for (int k = 1; k <= n; k++) { n     if (k != 1) t1 += a; n     if (s[k - 1] !=  h ) t1 += b; n     t1 += 1; n     if (t1 > t) break; n     int t2 = t - t1 - (k - 1) * a; n     int l = lower_bound(tm.begin(), tm.end(), t2 + 1) - tm.begin(); n     best = max(best, min(k + l, n)); n   } n   return best; n } n int main() { n   cin >> n >> a >> b >> t >> s; n   int s1 = solve(); n   reverse(s.begin() + 1, s.end()); n   int s2 = solve(); n   cout << max(s1, s2) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[105][105]; n int sz[105]; n vector<int> g[105]; n int usd[30]; n int ord[30], szo = 0, din[30]; n bool dfs(int v) { n   usd[v] = 1; n   for (int i = 0; i < g[v].size(); ++i) { n     int u = g[v][i]; n     if (usd[u] == 1) n       return false; n     else if (!usd[u]) n       if (!dfs(u)) return false; n   } n   usd[v] = 2; n   ord[szo++] = v; n   return true; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     scanf( %s , s[i]); n     sz[i] = strlen(s[i]); n   } n   for (int i = 1; i < n; ++i) { n     int j, mn = min(sz[i], sz[i - 1]); n     for (j = 0; j < mn; ++j) n       if (s[i][j] != s[i - 1][j]) { n         g[s[i - 1][j] -  a ].push_back(s[i][j] -  a ); n         din[s[i][j] -  a ]++; n         break; n       } n     if (j == mn && sz[i] == mn) { n       cout <<  Impossible n ; n       return 0; n     } n   } n   for (int i = 0; i < 26; ++i) n     if (din[i] == 0) n       if (!dfs(i)) { n         cout <<  Impossible n ; n         return 0; n       } n   if (szo < 26) { n     cout <<  Impossible n ; n     return 0; n   } n   for (int i = szo - 1; i >= 0; --i) printf( %c , ord[i] +  a ); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int a[200], b[200]; n int check(int n, int d, int a[]) { n   for (int i = 1; i <= n; ++i) { n     int flag = 0; n     for (int v = 1; v <= 100; ++v) n       if (a[v] >= d) { n         a[v] -= d; n         flag = 1; n         break; n       } n     if (flag == 0) return 0; n   } n   return 1; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < m; ++i) { n     cin >> b[i]; n   } n   int ans = 0; n   int l = 1, r = m; n   while (l <= r) { n     memset(a, 0, sizeof(a)); n     for (int i = 0; i < m; ++i) a[b[i]]++; n     int k = (l + r) / 2; n     if (check(n, k, a)) { n       ans = max(ans, k); n       l = k + 1; n     } else n       r = k - 1; n   } n   memset(a, 0, sizeof(a)); n   for (int i = 0; i < m; ++i) a[b[i]]++; n   if (check(n, l, a)) ans = max(ans, l); n   memset(a, 0, sizeof(a)); n   for (int i = 0; i < m; ++i) a[b[i]]++; n   if (check(n, r, a)) ans = max(ans, r); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void getint(int &v) { n   char ch, fu = 0; n   for (ch =  * ; (ch <  0  || ch >  9 ) && ch !=  - ; ch = getchar()) n     ; n   if (ch ==  - ) fu = 1, ch = getchar(); n   for (v = 0; ch >=  0  && ch <=  9 ; ch = getchar()) v = v * 10 + ch -  0 ; n   if (fu) v = -v; n } n int main() { n   int maxmax = 0, max, x, sum = 0, n, m; n   long long int sumsum = 0; n   getint(n); n   if (n == 1) printf( %d n , 0), exit(0); n   for (int i = 0; i < n; ++i) { n     getint(m); n     max = 0; n     for (int j = 0; j < m; ++j) { n       getint(x); n       if (x > max) max = x; n     } n     sumsum += (long long)max * m; n     sum += m; n     if (max > maxmax) maxmax = max; n   } n   printf( %lld n , (long long)sum * (long long)maxmax - sumsum); n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n using namespace std; n long long gcd(long long x, long long y) { n   if (x > y) swap(x, y); n   if (x == 0) return y; n   if (y % x == 0) return x; n   return gcd(y % x, x); n } n int main() { n   long long n, k, t; n   long long m, d, w, g; n   long long x, y, z; n   long long s, ans; n   bool v = true; n   ans = 0; n   scanf( %lld , &t); n   for (long long tt = 0; tt < (long long)(t); tt++) { n     scanf( %lld %lld %lld , &m, &d, &w); n     if ((m == 1) || (d == 1)) { n       printf( 0 n ); n       continue; n     } n     g = gcd(d - 1, w); n     k = w / g; n     x = min(m, d); n     y = x % k; n     x = x / k; n     s = (x + 1) * x; n     s /= 2; n     ans = s * y; n     s = x * (x - 1); n     s /= 2; n     ans += (s * (k - y)); n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007ll; n long long s[1000010]; n int h[1000010]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &h[i]); n     h[i]--; n   } n   s[2] = min(h[1], h[2]); n   for (int i = 2; i < n; i++) n     s[i + 1] = (s[i] * min(h[i - 1], min(h[i], h[i + 1])) % mod + n                 min(h[i], h[i + 1])) % n                mod; n   long long ans = 0; n   for (int i = 1; i <= n; i++) ans = (ans + h[i]) % mod; n   for (int i = 2; i <= n; i++) n     ans = (ans + min(h[i - 1], h[i]) * s[i] % mod) % mod; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a; n char str[100002]; n int sum[100002]; n int cnt[100002]; n int main() { n   scanf( %d n , &a); n   gets(str); n   int len = strlen(str); n   sum[0] = (str[0] -  0 ); n   for (int i = 1; i < len; i++) { n     sum[i] = sum[i - 1] + (str[i] -  0 ); n   } n   int id, x, y, mx = 0; n   for (int i = 0; i < len; i++) { n     for (int j = i; j < len; j++) { n       if (i > 0) n         id = sum[j] - sum[i - 1]; n       else n         id = sum[j]; n       cnt[id]++; n       mx = max(mx, id); n     } n   } n   long long ans = 0; n   for (int i = 0; i < len; i++) { n     for (int j = i; j < len; j++) { n       if (i > 0) n         x = sum[j] - sum[i - 1]; n       else n         x = sum[j]; n       if (a == 0) { n         ans += cnt[0]; n       } n       if (x == 0) continue; n       if (a % x != 0) continue; n       y = a / x; n       if (y > mx) continue; n       ans = ans + (long long)(cnt[y]); n     } n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (-1u) / 2; n const long long int INF2 = (-1ull) / 2; n int a, b, i, d[1011000], j, k, n, m, timer = 0, l, r, x, y; n int c[1011000], cnt = 0, fl = 0, a2, a3 = -1000000, ans = 0; n pair<int, int> p[100100]; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> n >> a >> b; n   for (i = 0; i < n; i++) { n     cin >> l; n     if (b < a) n       p[i] = make_pair(l, i); n     else n       p[i] = make_pair(l, i * -1); n   } n   sort(p, p + n); n   for (i = n - 1; i >= n - min(a, b); i--) { n     if (a > b) n       c[abs(p[i].second)] = 2; n     else n       c[abs(p[i].second)] = 1; n   } n   for (i = 0; i < n; i++) { n     if (a == b) { n       if (i < a) n         cout <<  1  ; n       else n         cout <<  2  ; n     } else { n       if (a > b && c[i] == 0) n         c[i] = 1; n       else if (a < b && c[i] == 0) n         c[i] = 2; n       cout << c[i] <<    ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using pii = pair<int, int>; n using ll = long long; n using llu = long long unsigned; n using ld = long double; n const ld EPS = 1e-9; n inline int cmp(ld first, ld second = 0, ld tol = EPS) { n   return (first <= second + tol) ? (first + tol < second) ? -1 : 0 : 1; n } n const int MOD = 1; n inline int mod(ll first, int m = MOD) { return (int)(((first % m) + m) % m); } n const int P = 1e5 + 100; n ll n; n bool not_prime[P]; n vector<int> primes; n int main() { n   not_prime[0] = not_prime[1] = true; n   for (int i = 2; i * i < P; i++) { n     if (!not_prime[i]) n       for (int j = i * i; j < P; j += i) not_prime[j] = true; n   } n   for (int i = 2; i < P; i++) n     if (!not_prime[i]) primes.push_back(i); n   scanf( %lld , &n); n   bool isPrime = true; n   int pos; n   for (int i = 0; i < ((int)(primes).size()); i++) n     if (n % primes[i] == 0) { n       isPrime = false; n       pos = (primes[i] - 3) / 2; n       break; n     } n   if (isPrime) n     printf( 1 n ); n   else if (n % 2 == 0 or n % 3 == 0) n     printf( %lld n , n / 2); n   else n     printf( %lld n , (n / 2) - pos); n   return 0; n } n


#include <bits/stdc++.h> n long long strange[100001]; n int main() { n   for (int i = 1; i <= 100000; i++) { n     long long a = i, b = i; n     while (b) { n       a *= 10; n       a += b % 10; n       b /= 10; n     } n     strange[i] = a; n   } n   long long k, p; n   while (scanf( %lld%lld , &k, &p) != EOF) { n     long long sum = 0; n     for (int i = 1; i <= k; i++) sum += strange[i]; n     printf( %lld n , sum % p); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const long long int LINF = 0x3f3f3f3f3f3f3f3fll; n int g; n bool endswith(string a, string b) { n   if ((int)((a).size()) < (int)((b).size())) return 0; n   for (int i = 0; i < (int)((b).size()); i++) n     if (a[(int)((a).size()) - 1 - i] != b[(int)((b).size()) - 1 - i]) return 0; n   return 1; n } n bool is_adj(string second) { n   if (endswith(second,  lios )) n     g = 1; n   else if (endswith(second,  liala )) n     g = 2; n   else n     return 0; n   return 1; n } n bool is_noun(string second) { n   if (endswith(second,  etr )) n     g = 1; n   else if (endswith(second,  etra )) n     g = 2; n   else n     return 0; n   return 1; n } n bool is_verb(string second) { n   if (endswith(second,  initis )) n     g = 1; n   else if (endswith(second,  inites )) n     g = 2; n   else n     return 0; n   return 1; n } n int main() { n   ios::sync_with_stdio(false); n   vector<string> v; n   string second; n   while (cin >> second) v.push_back(second); n   if ((int)((v).size()) == 1) { n     if (is_adj(v[0]) or is_noun(v[0]) or is_verb(v[0])) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n     return 0; n   } n   int last = -1; n   int n = 0; n   bool masc, deu = 1; n   for (int i = 0; i < (int)((v).size()); i++) { n     if (is_adj(v[i])) { n       if (last > 0) deu = 0; n       last = 0; n     } else if (is_noun(v[i])) { n       if (last > 1) deu = 0; n       n++; n       last = 1; n     } else if (is_verb(v[i])) { n       last = 2; n     } else n       deu = 0; n     if (!i) n       masc = (g == 1); n     else if (masc and g == 2) n       deu = 0; n     else if (!masc and g == 1) n       deu = 0; n   } n   if (n != 1) deu = 0; n   int cu; n   cout << (deu ?  YES  :  NO ) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int in() { n   int x; n   scanf( %d , &x); n   return x; n } n const int INF = 1e9 + 10; n const long long LINF = 1000ll * 1000 * 1000 * 1000 * 1000 * 1000 + 100; n const int MN = 3e3 + 10; n int n; n vector<pair<int, int> > edges[MN]; n int ans; n int dp1[MN], dp2[MN]; n int sz[MN]; n void input() { n   n = in(); n   for (int i = 0; i < n - 1; ++i) { n     int a, b; n     a = in(), b = in(); n     edges[a].push_back(pair<int, int>(b, 1)); n     edges[b].push_back(pair<int, int>(a, 0)); n   } n } n void dfs1(int s, int pr) { n   for (int i = 0; i < ((int)(edges[s]).size()); ++i) { n     if (edges[s][i].first == pr) continue; n     if (edges[s][i].second == 0) ++sz[s]; n     dfs1(edges[s][i].first, s); n     sz[s] += sz[edges[s][i].first]; n   } n } n void dfs2(int s, int pr) { n   for (int i = 0; i < ((int)(edges[s]).size()); ++i) { n     if (edges[s][i].first == pr) continue; n     dfs2(edges[s][i].first, s); n   } n   int Mn = sz[s]; n   for (int i = 0; i < ((int)(edges[s]).size()); ++i) { n     if (edges[s][i].first == pr) continue; n     if (edges[s][i].second) { n       Mn = min(Mn, dp1[edges[s][i].first] + 1 + sz[s] - sz[edges[s][i].first]); n     } else { n       Mn = min(Mn, dp1[edges[s][i].first] + sz[s] - 1 - sz[edges[s][i].first]); n     } n   } n   dp1[s] = Mn; n   Mn = INF; n   for (int i = 0; i < ((int)(edges[s]).size()); ++i) { n     if (edges[s][i].first == pr) continue; n     if (edges[s][i].second) { n       Mn = min(Mn, dp2[edges[s][i].first] + sz[s] - sz[edges[s][i].first]); n     } else { n       Mn = min(Mn, dp1[edges[s][i].first] + sz[s] - sz[edges[s][i].first] - 1); n       Mn = min(Mn, dp2[edges[s][i].first] + sz[s] - sz[edges[s][i].first]); n     } n   } n   dp2[s] = Mn; n } n int main() { n   input(); n   int mn = INF; n   for (int i = 0; i < n; ++i) { n     memset(dp1, 0, sizeof dp1); n     memset(dp2, 0, sizeof dp2); n     memset(sz, 0, sizeof sz); n     dfs1(i, -1); n     dfs2(i, -1); n     mn = min(mn, dp2[i]); n   } n   if (n <= 2) mn = 0; n   cout << mn <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long int MOD = 1e9 + 7; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   while (t--) { n     long long int n, i, j; n     cin >> n; n     long long int a[n]; n     for (i = 0; i < n; i++) cin >> a[i]; n     sort(a, a + n); n     std::vector<long long int> ans; n     long long int last = 64 - __builtin_clzll(a[n - 1]); n     for (i = 0; i < last + 1; i++) { n       std::vector<long long int> temp; n       for (j = 0; j < n; j++) { n         bitset<70> b(a[j]); n         if (b[i]) temp.push_back(a[j]); n       } n       bool f = 0; n       for (j = i - 1; j > -1; j--) { n         bool f1 = 0; n         for (auto x : temp) { n           bitset<70> b(x); n           if (b[j] == 0) f1 = 1; n         } n         if (!f1) { n           f = 1; n           break; n         } n       } n       if (f) continue; n       if (temp.size()) { n         ans = temp; n       } n     } n     cout << ans.size() <<   n ; n     for (auto x : ans) cout << x <<    ; n   } n   cerr <<  Time Taken:  << (float)clock() / CLOCKS_PER_SEC * 1000 <<  ms  n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n inline long long int rand(long long int x, long long int y) { n   ++y; n   return (rng() % (y - x)) + x; n } n string to_string(char c) { n   string second(1, c); n   return second; n } n string to_string(bool b) { return (b ?  true  :  false ); } n template <typename A, typename B> n string to_string(pair<A, B> p) { n   return  (  + to_string(p.first) +  ,   + to_string(p.second) +  ) ; n } n template <typename A> n string to_string(A v) { n   bool first = true; n   string res =  { ; n   for (const auto &x : v) { n     if (!first) { n       res +=  ,  ; n     } n     first = false; n     res += to_string(x); n   } n   res +=  } ; n   return res; n } n void degug_out() { cerr << endl; } n template <typename Head, typename... Tail> n void degug_out(Head H, Tail... T) { n   cerr <<     << to_string(H); n   degug_out(T...); n } n inline long long int gcd(long long int a, long long int b) { n   if (a > b) swap(a, b); n   if (a == 0) return b; n   return gcd(b % a, a); n } n long long int n, m; n vector<vector<char>> grid; n vector<vector<int>> dist, dist2; n long long int dx[] = {1, 0, -1, 0}; n long long int dy[] = {0, 1, 0, -1}; n void bfs(long long int x, long long int y, long long int op) { n   dist[x][y] = 0; n   queue<pair<long long int, long long int>> q; n   q.emplace(x, y); n   while (q.size()) { n     long long int x = q.front().first, y = q.front().second; n     q.pop(); n     if (op == 0) { n       for (long long int i = (0); i <= (long long int)(1); ++i) { n         long long int nx = x + dx[i]; n         long long int ny = y + dy[i]; n         if (nx < 0 || ny < 0 || nx >= n || ny >= m || ~dist[nx][ny] || n             grid[nx][ny] ==  # ) n           continue; n         dist[nx][ny] = dist[x][y] + 1; n         q.emplace(nx, ny); n       } n     } else { n       for (long long int i = (2); i <= (long long int)(3); ++i) { n         long long int nx = x + dx[i]; n         long long int ny = y + dy[i]; n         if (nx < 0 || ny < 0 || nx >= n || ny >= m || ~dist[nx][ny] || n             grid[nx][ny] ==  # ) n           continue; n         dist[nx][ny] = dist[x][y] + 1; n         q.emplace(nx, ny); n       } n     } n   } n   return; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> m; n   grid.resize(n, vector<char>(m,  * )); n   dist.resize(n, vector<int>(m, -1)); n   dist2.resize(n, vector<int>(m, -1)); n   for (long long int i = (0); i <= (long long int)(n - 1); ++i) n     for (long long int j = (0); j <= (long long int)(m - 1); ++j) n       cin >> grid[i][j]; n   bfs(0, 0, 0); n   swap(dist, dist2); n   bfs(n - 1, m - 1, 1); n   if (dist2[n - 1][m - 1] == -1) { n     cout << 0; n     return 0; n   } n   map<long long int, long long int> co; n   for (long long int i = (0); i <= (long long int)(n - 1); ++i) n     for (long long int j = (0); j <= (long long int)(m - 1); ++j) n       if (~dist[i][j] && ~dist2[i][j] && dist2[i][j] && n           dist2[i][j] != (n - 1 + m - 1)) n         co[dist2[i][j]]++; n   for (long long int i = (0); i <= (long long int)(n - 1); ++i) n     for (long long int j = (0); j <= (long long int)(m - 1); ++j) n       if (~dist[i][j] && ~dist2[i][j] && dist2[i][j] && n           dist2[i][j] != (n - 1 + m - 1)) n         if (co[dist2[i][j]] == 1) { n           cout << 1; n           return 0; n         } n   cout << 2; n } n


#include <bits/stdc++.h> n const long long N = 1000006; n using namespace std; n const long long MOD = 1000000007LL; n template <typename T> n T gcd(T a, T b) { n   if (a == 0) return b; n   return gcd(b % a, a); n } n template <typename T> n T power(T x, T y, long long m = MOD) { n   T ans = 1; n   while (y > 0) { n     if (y & 1LL) ans = (ans * x) % m; n     y >>= 1LL; n     x = (x * x) % m; n   } n   return ans % m; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   if (n == 1) { n     cout <<  0 ; n     return 0; n   } n   multiset<long long> l, r; n   vector<pair<long long, long long> > v; n   for (long long(i) = (0); i < (n); i++) { n     long long x, y; n     cin >> x >> y; n     l.insert(x); n     r.insert(y); n     v.emplace_back(make_pair(x, y)); n   } n   long long ans = 0; n   for (long long(i) = (0); i < (n); i++) { n     l.erase(l.find(v[i].first)); n     r.erase(r.find(v[i].second)); n     long long dist = max(0LL, *r.begin() - *l.rbegin()); n     ans = max(ans, dist); n     l.insert(v[i].first); n     r.insert(v[i].second); n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100001], n; n inline long long sum(int t) { n   long long s = 0; n   for (int i = 1; i <= n; ++i) s += min(a[i], t); n   return s; n } n int main() { n   int i, j, l, r, m, t = 1; n   int nr = 0; n   long long k, s = 0, smin; n   scanf( %d %I64d , &n, &k); n   for (i = 1; i <= n; ++i) { n     scanf( %d , &a[i]); n     s += a[i]; n   } n   if (s < k) n     printf( -1 ); n   else if (s == k) n     printf(  ); n   else { n     l = 0; n     r = 1000000000; n     while (l + 1 < r) { n       m = (l + r) >> 1; n       if (sum(m) <= k) n         l = m; n       else n         r = m; n     } n     k -= sum(l); n     if (s == k) n       printf(  ); n     else { n       for (i = 1; i <= n; ++i) { n         if (k == 0) break; n         if (a[i] > l) { n           --k; n           --a[i]; n         } n       } n       for (j = i; j <= n; ++j) n         if (a[j] > l) printf( %d  , j); n       for (j = 1; j < i; ++j) n         if (a[j] > l) printf( %d  , j); n     } n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100000; n int n, m, k, start = MAXN + 1; n int type[MAXN + 3], a[MAXN + 3], counter[MAXN + 3], stc[MAXN + 3]; n bool check[MAXN + 3]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d , &type[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &a[i]); n   for (int i = 1; i <= n; i++) counter[a[i]]++; n   for (int i = 1; i <= n; i++) n     if (counter[i] == 1) check[i] = true; n   int cnt = 0; n   for (int i = n; i >= 1; i--) { n     if (type[i] == 1) { n       cnt = 1; n       int v = a[i]; n       while (v != 0 && check[v]) { n         cnt++; n         v = a[v]; n       } n       if (cnt > k) k = cnt, start = i; n     } n   } n   printf( %d n , k); n   for (int i = 0, v = start; i < k; i++, v = a[v]) stc[i] = v; n   for (int i = k - 1; i >= 0; i--) printf( %d  , stc[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long l, r, ans; n vector<long long> lucky; n void lu(int f, int s) { n   string x =   ; n   for (int i = 0; i < f; i++) x +=  4 ; n   for (int i = 0; i < s; i++) x +=  7 ; n   do lucky.push_back(atoll(x.c_str())); n   while (next_permutation(x.begin(), x.end())); n } n int main() { n   cin >> l >> r; n   for (int i = 0; i <= 10; i++) n     for (int j = 0; j <= 10; j++) n       if (i + j <= 10) lu(i, j); n   sort(lucky.begin(), lucky.end()); n   int j, k; n   j = k = 0; n   while (lucky[j++] < l) n     ; n   j--; n   while (lucky[k++] < r) n     ; n   k--; n   ans += lucky[j] * (min(r - l, lucky[j] - l) + 1); n   if (lucky[j] == lucky[k]) return cout << ans << endl, 0; n   ans += lucky[k] * (r - lucky[k - 1]); n   for (int i = j + 1; i < k; i++) ans += (lucky[i] - lucky[i - 1]) * lucky[i]; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; n int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; n int a[33]; n string s; n int main() { n   for (int i = 0; i < 26; ++i) scanf( %d , &a[i]); n   cin >> s; n   int len = s.length(); n   vector<long long> v(len + 1); n   for (int i = 0; i < len; ++i) { n     v[i + 1] = a[s[i] -  a ] + v[i]; n   } n   map<pair<int, long long>, int> m; n   long long res = 0; n   for (int i = 0; i < len; i++) { n     if (m[make_pair(s[i] -  a , v[i + 1])]) { n       res += m[make_pair(s[i] -  a , v[i + 1])]; n     } n     m[make_pair(s[i] -  a , v[i + 1] + a[s[i] -  a ])]++; n   } n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int arr[101][101]; n   int row[101], col[101]; n   for (int i = 0; i <= n - 1; i++) { n     row[i] = col[i] = 0; n   } n   int temp; n   for (int i = 0; i <= n - 1; i++) { n     for (int j = 0; j <= n - 1; j++) { n       scanf( %d , &temp); n       arr[i][j] = temp; n       row[i] += temp; n       col[j] += temp; n     } n   } n   int ans = 0; n   for (int i = 0; i <= n - 1; i++) { n     for (int j = 0; j <= n - 1; j++) { n       if (col[j] > row[i]) ans++; n     } n   } n   printf( %d n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int m, n, k, t; n int a[100001]; n int main() { n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; ++i) { n     scanf( %d , &t); n     a[i % k] += t; n   } n   m = 0; n   for (int i = 1; i < k; ++i) n     if (a[i] < a[m]) m = i; n   printf( %d n , m + 1); n } n