#include <bits/stdc++.h> n using namespace std; n template <class T> n void chmin(T& a, const T& b) { n   if (a > b) a = b; n } n template <class T> n void chmax(T& a, const T& b) { n   if (a < b) a = b; n } n tuple<long long, long long, long long> adj[400001]; n long long cnt; n long long nm; n int main() { n   long long k, a, x, y, m, n; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     cin >> k >> a >> x >> y >> m; n     long long t = 0; n     for (long long j = 1; j <= k; j++, adj[nm++] = make_tuple(t, a, i), n                    (a > ((a * x + y) % m) && j != k + 1) ? t++ : t, n                    a = (a * x + y) % m) n       ; n     cnt = max(cnt, t); n   } n   cout << cnt <<   n ; n   sort(adj, adj + nm); n   for (long long i = 0; i < nm; i++) n     cout << get<1>(adj[i]) <<     << get<2>(adj[i]) <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 5; n const int mod = 1e9 + 7; n long long n, k, arr[MAX], ans = 0; n int main(int argc, char** argv) { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n >> k; n   if (n == 1) { n     cout << 0; n     exit(0); n   } n   bool moded = true; n   for (int i = 0; i < n; i++) cin >> arr[i]; n   sort(arr, arr + n); n   long long modu = arr[0] % k; n   for (int i = 0; i < n; i++) n     if (arr[i] % k != modu) moded = false; n   if (moded) n     for (int i = 0; i < n; i++) ans += (arr[i] - arr[0]) / k; n   else n     ans = -1; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 100005; n char str[Maxn]; n int slen; n int cnt[Maxn]; n bool was[Maxn]; n int Z[Maxn]; n vector<pair<int, int> > res; n int main() { n   scanf( %s , str); n   slen = strlen(str); n   cnt[slen]++; n   was[slen] = true; n   int L = -1, R = -1; n   for (int i = 1; i < slen; i++) { n     if (i <= R) Z[i] = min(R - i + 1, Z[i - L]); n     while (i + Z[i] < slen && str[Z[i]] == str[i + Z[i]]) Z[i]++; n     if (i + Z[i] - 1 > R) { n       L = i; n       R = i + Z[i] - 1; n     } n     if (i + Z[i] == slen) was[Z[i]] = true; n     cnt[Z[i]]++; n   } n   for (int i = slen - 1; i >= 0; i--) cnt[i] += cnt[i + 1]; n   for (int i = 1; i <= slen; i++) n     if (was[i]) res.push_back(pair<int, int>(i, cnt[i])); n   printf( %d n , res.size()); n   for (int i = 0; i < res.size(); i++) n     printf( %d %d n , res[i].first, res[i].second); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, a[1000000]; n long long bank; n vector<int> t; n int main() { n   cin >> n >> m; n   for (int i = 0, k; i < n; i++) { n     cin >> k; n     int p = -1; n     bool q = false; n     for (int j = 0; j < k; j++) { n       cin >> a[j]; n       if (a[j] < m) p = 1; n     } n     if (p == true) { n       t.push_back(i); n       bank++; n     } n   } n   cout << bank << endl; n   for (int i = 0; i < t.size(); i++) cout << t[i] + 1 <<    ; n } n


//In rememberance to GOD// n //May GOD take care to me,my family and all the creatures// n #include<bits/stdc++.h> n using namespace std; n  n  n  n #define ll long long n #define int ll    n #define mod ((int)1e9+7) n #define pii pair<int,int> n #define vi vector<int> n #define vvi vector<vi> n #define vc vector<char> n #define vpi vector<pii> n #define vpic vector<pair<int,char>> n #define vpid vector<pair<int,double>> n #define INF (ll)1e15 n #define all(x)  (x).begin(),(x).end() n #define allr(x) (x).rbegin(),(x).rend() n #define pb push_back n #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) n #define eb emplace_back n  n #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(),  , ,    ); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } n  n void err(istream_iterator<string> it) {} n template<typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n  cerr << *it <<   =   << a << endl; n  err(++it, args...); n } n  n #define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) n #define time__(d)   n     for (   n         auto blockTime = make_pair(chrono::high_resolution_clock::now(), true);   n         blockTime.second;   n         debug( %s: %lld ms n , d, chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - blockTime.first).count()), blockTime.second = false   n     ) n  n //fill memset iota function n // use 1ll<<number(left shift) n  n const int N=((int)1e6); n  n  n  n // int primes[N]; n // // int spf[N]; n // vector<int> pr; n  n // void sieve(){ n  n //     // iota(spf+2,spf+N,2); n  n //  for(ll i=2;i<N;i++){ n //   if(primes[i]==0){ n //     pr.eb(i*i); n //    for(ll j=i*i;j<N;j+=i){ n //                 // if(spf[j]==j) n //                 // spf[j]=i; n //     primes[j]=1; n //    } n //   } n //   primes[i]^=1; n //  } n  n // } n // nloglogn n  n  n  n  n ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);} n void swap(int &x, int &y) {int temp = x; x = y; y = temp;} n ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} n ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} n ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;} n ll power(ll x, ll y,ll m) {ll res = 1;while (y > 0){ if (y & 1) res = (res*x)%m;y = y>>1;x = x*x%m;}return res;} n //avoid it in power of 2. n //logn implementation n //divisors in o(sqrt(n)) and no of divisors less than 2*(n^1/2);//128(max divisors for n<=1e5) n //for n>=1e5 no of divisors is less than n^1/3; n // use i&1 instead of i%2 n //stoll convert string to integer(long long) n //  if(a>b) { return a/b+fun(b,a%b);} else return b/a+fun(a,b%a); technique of mod and minus similar answer but diffent time complexity. n // if(a>b) { return 1+fun(b,a-b);} else return 1+fun(a,b-a); n // class cmp { public: n //     bool operator()(const pair<int,pii> &p1,const pair<int,pii> &p2) const { if(p1.first!=p2.first) return p1.first>p2.first;else n //         { n //             if(p1.second.first!=p2.second.first) n //             { n //                 return p1.second.first>p2.second.first; n //             } n //             else n //             { n //                 return p1.second.second>p2.second.second; n //             } n              n //         } n          n //     } n   n // }; n // instead of priority queue i use set defined with cmp class strictly using  =  sign in comparisons(work as multi set) n //erase function with values won t work n //to modify a set i use cmp class stictly without using  =  sign in comparions,example above!!!!   n //erase function with values will work n  n  n  n  n  n  n  n /////////////////////////////////////// CODE //////////////////////////////////////////////// n  n  n // int dx[] = {1,0,-1,0}; n // int dy[] = {0,1,0,-1}; n  n  n  n  n  n // vi adj[N]; n // int visited[N]; n // int par[N]; n // int d[N]; n  n  n  n // int binary_searching(int a,int y) n // { n //     int start=-1; n //     int end=a; n  n //     while(start+1!=end) n //     { n //         int mid=(start+end)/2; n //         if(mid>(a-y)) n //         { n //             end=mid; n //         } n //         else n //         { n //             start=mid; n //         } n //     } n  n //     return end; n  n      n      n // } n  n  n bool palin(string s) n { n     int n=s.size(); n      n  n     for(int i=0;i<n/2;i++) n     { n         if(s[i]!=s[n-i-1]) n         { n             return false; n         } n     } n  n     return true; n } n  n  n  n void solve() n { n     string s; n     cin>>s; n  n     bool flag=false; n  n     for(int i=0;i<s.size();i++) n     { n         if(s[i]!= a ) n         { n             flag=true; n             break; n         } n     } n  n     if(!flag) n     { n         cout<< NO <<  n ; n         return; n     } n  n     //appling to first; n     string temp; n     // temp=s; n  n     temp= a +s; n     if(!palin(temp)) n     { n         cout<< YES <<  n ; n         cout<<temp<<  n ; n         return; n     } n  n     temp=s+ a ; n  n     if(!palin(temp)) n     { n         cout<< YES <<  n ; n         cout<<temp<<  n ; n     } n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n  n } n  n  n  n  n  n  n  n  n int32_t main() n { n     #ifndef ONLINE_JUDGE n         freopen( input.txt ,  r , stdin); n         freopen( output.txt ,  w , stdout); n     #endif n  n     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); n  n     int t; n     cin>>t; n  n  n     // sieve(); n  n  n      n     // t=1; n  n      n     for(int i=1;i<=t;i++) n     { n         solve(); n         // time__( solve ); n     } n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n int n, m, q; n int p[200001]; n int a[200001]; n vector<int> cur[200001]; n int pos[200001]; n int par[200001][21]; n struct cmp { n   bool operator()(pair<int, int> a, pair<int, int> b) { n     return a.second > b.second; n   } n }; n priority_queue<pair<int, int>, vector<pair<int, int> >, cmp> h; n struct Q { n   int l, r, id; n }; n vector<Q> Query; n bool cmp(Q a, Q b) { return a.l < b.l; } n int ans[200001]; n int Process(int pos) { n   int k = n - 1; n   for (int i = 20; i >= 0; i--) { n     if (k & (1 << i)) { n       if (par[pos][i] == -1) return -1; n       pos = par[pos][i]; n     } n   } n   return pos; n } n int main() { n   cin >> n >> m >> q; n   for (int i = 1; i <= n; i++) { n     cin >> p[i]; n     pos[p[i]] = i; n   } n   for (int i = 1; i <= m; i++) { n     cin >> a[i]; n   } n   memset(par, -1, sizeof par); n   for (int i = 1; i <= m; i++) { n     int x = pos[a[i]]; n     int y; n     if (x == 1) n       y = p[n]; n     else n       y = p[x - 1]; n     for (auto k : cur[y]) { n       par[k][0] = i; n     } n     cur[y].clear(); n     cur[a[i]].push_back(i); n   } n   for (int j = 1; j <= 20; j++) { n     for (int i = 1; i <= m; i++) { n       if (par[i][j - 1] != -1) par[i][j] = par[par[i][j - 1]][j - 1]; n     } n   } n   for (int i = 1; i <= q; i++) { n     int l, r; n     cin >> l >> r; n     Query.push_back({l, r, i}); n   } n   sort(Query.begin(), Query.end(), cmp); n   for (int i = 1; i <= m; i++) { n     int r = Process(i); n     if (r != -1) h.push({i, r}); n   } n   for (auto x : Query) { n     int id = x.id; n     while (!h.empty() && h.top().first < x.l) h.pop(); n     if (h.empty() || h.top().second > x.r) n       ans[id] = 0; n     else n       ans[id] = 1; n   } n   for (int i = 1; i <= q; i++) cout << ans[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105; n vector<int> adj[N]; n int a[N], d[N], din[N]; n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) scanf( %d %d , a + i, d + i); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (i == j) continue; n       if (d[i] < 0) { n         if (make_pair(a[i], -i) > make_pair(a[j], -j) && n             make_pair(a[j], -j) > make_pair(a[i] + d[i], -i) && n             make_pair(a[i] + d[i], -i) > make_pair(a[j] + d[j], -j)) n           adj[i].push_back(j); n       } else { n         if (make_pair(a[i], -i) < make_pair(a[j], -j) && n             make_pair(a[j], -j) < make_pair(a[i] + d[i], -i) && n             make_pair(a[i] + d[i], -i) < make_pair(a[j] + d[j], -j)) n           adj[i].push_back(j); n       } n     } n   } n   memset(din, 0, sizeof din); n   for (int i = 0; i < n; i++) { n     for (int u : adj[i]) din[u]++; n   } n   stack<int> st; n   for (int i = 0; i < n; i++) n     if (din[i] == 0) { n       st.push(i); n     } n   vector<int> order; n   while (!st.empty()) { n     int u = st.top(); n     st.pop(); n     order.push_back(u); n     for (int v : adj[u]) { n       din[v]--; n       if (din[v] == 0) st.push(v); n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) { n     int cnt1, cnt2; n     cnt1 = cnt2 = 0; n     for (int j = 0; j < n; j++) { n       if (j == order[i]) continue; n       cnt1 += (make_pair(a[j], -j) < make_pair(a[order[i]], -order[i])); n       cnt2 += (make_pair(a[j], -j) < n                make_pair(a[order[i]] + d[order[i]], -order[i])); n     } n     ans += abs(cnt1 - cnt2); n     a[order[i]] += d[order[i]]; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int arr[100005]; n long long sum[100005]; n map<int, long long> f; n long long solve(int n, int k) { n   if (f.count(k)) return f[k]; n   long long ret = 0; n   long long cur = n - 2; n   long long p = k; n   while (cur >= 0) { n     ret += sum[cur]; n     cur -= p; n     p *= k; n   } n   return f[k] = ret; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d , &arr[i]); n   sort(arr, arr + n); n   sum[0] = arr[0]; n   for (int i = 1; i < n; ++i) sum[i] = sum[i - 1] + arr[i]; n   int q, a; n   scanf( %d , &q); n   for (int i = 0; i < q; ++i) { n     scanf( %d , &a); n     if (i) printf(   ); n     printf( %I64d , solve(n, a)); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace ::std; n int main() { n   int n, m; n   cin >> n; n   int ara[2 * n + 7]; n   for (int i = 0; i < 2 * n; i++) { n     cin >> ara[i]; n   } n   sort(ara, ara + (2 * n)); n   if (ara[n - 1] == ara[n]) { n     cout <<  NO ; n   } else { n     cout <<  YES ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200005; n inline int gi() { n   char c = getchar(); n   while (c <  0  || c >  9 ) c = getchar(); n   int sum = 0; n   while ( 0  <= c && c <=  9 ) sum = sum * 10 + c - 48, c = getchar(); n   return sum; n } n int n, m, q, A[maxn], B[maxn]; n vector<pair<int, int>> vec[maxn]; n long long sum[maxn], Min[maxn << 2], tag[maxn << 2]; n priority_queue<long long, vector<long long>, greater<long long>> q1, q2; n void build(int s, int l, int r) { n   if (l == r) return Min[s] = B[l - 1], void(); n   build((s << 1), l, ((l + r) >> 1)); n   build((s << 1 | 1), ((l + r) >> 1) + 1, r); n   Min[s] = min(Min[(s << 1)], Min[(s << 1 | 1)]); n } n void modify(int s, int l, int r, int ql, int qr, int v) { n   if (ql <= l && r <= qr) return tag[s] += v, Min[s] += v, void(); n   if (ql <= ((l + r) >> 1)) modify((s << 1), l, ((l + r) >> 1), ql, qr, v); n   if (qr >= ((l + r) >> 1) + 1) n     modify((s << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr, v); n   Min[s] = min(Min[(s << 1)], Min[(s << 1 | 1)]) + tag[s]; n } n int main() { n   n = gi(); n   m = gi(); n   q = gi(); n   for (int i = 1; i < n; ++i) A[i] = gi(), B[i] = gi(); n   for (int u, v, w, i = 1; i <= m; ++i) { n     u = gi(); n     v = gi(); n     w = gi(); n     vec[u].push_back(make_pair(v, w)); n   } n   build(1, 1, n); n   for (int i = 1; i <= n; ++i) { n     for (auto j : vec[i]) modify(1, 1, n, 1, j.first, j.second); n     sum[i] = Min[1]; n     q1.push(sum[i] + A[i]); n   } n   printf( %lld n , q1.top()); n   for (int i = 1, x, v; i <= q; ++i) { n     x = gi(); n     v = gi(); n     q2.push(A[x] + sum[x]); n     q1.push((A[x] = v) + sum[x]); n     while (!q1.empty() && !q2.empty() && q1.top() == q2.top()) n       q1.pop(), q2.pop(); n     printf( %lld n , q1.top()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, ans, sx, sy, ex, ey, endx = -1, endy = -1; n char s[1005][1006]; n pair<int, int> xx[1005]; n pair<int, int> yy[1005]; n bool able(int x, int y) { n   int nx = sx, ny = sy; n   while (true) { n     int nex = nx + x + 1, ney = ny + y + 1; n     if (nex > endx && ney > endy) return true; n     if (s[nx][ney] ==  X ) { n       if (nex <= endx && xx[nex].first <= ny) return false; n       ny++; n       continue; n     } else if (s[nex][ny] ==  X ) { n       if (ney <= endy && yy[ney].first <= nx) return false; n       nx++; n       continue; n     } else n       return false; n   } n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 0; i < n; i++) scanf( %s , s[i]); n   int state = 0; n   for (int i = 0; i < n; i++) { n     xx[i].first = -1; n     for (int j = 0; j < m; j++) { n       if (s[i][j] ==  X ) xx[i].second = j, endx = i; n       if (s[i][j] ==  X  && xx[i].first == -1) xx[i].first = j; n     } n     if (xx[i].first != -1) { n       for (int j = xx[i].first; j <= xx[i].second; j++) n         if (s[i][j] !=  X ) { n           printf( -1 ); n           return 0; n         } n       if (state < 2) n         state++; n       else if (state == 5) { n         printf( -1 ); n         return 0; n       } n     } else if (state != 0) n       state = 5; n     if (state != 2) continue; n     if (xx[i].first < xx[i - 1].first || xx[i].second < xx[i - 1].second || n         xx[i].first > xx[i - 1].second) { n       printf( -1 ); n       return 0; n     } n   } n   state = 0; n   for (int i = 0; i < m; i++) { n     yy[i].first = -1; n     for (int j = 0; j < n; j++) { n       if (s[j][i] ==  X ) yy[i].second = j, endy = i; n       if (s[j][i] ==  X  && yy[i].first == -1) yy[i].first = j; n     } n     if (yy[i].first != -1) { n       for (int j = yy[i].first; j <= yy[i].second; j++) n         if (s[j][i] !=  X ) { n           printf( -1 ); n           return 0; n         } n       if (state < 2) n         state++; n       else if (state == 5) { n         printf( -1 ); n         return 0; n       } n     } else if (state != 0) n       state = 5; n     if (state != 2) continue; n     if (yy[i].first < yy[i - 1].first || yy[i].second < yy[i - 1].second || n         yy[i].first > yy[i - 1].second) { n       printf( -1 ); n       return 0; n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (s[i][j] ==  X ) { n         sx = i, sy = j; n         i = n; n         break; n       } n     } n   } n   if (endx == -1) { n     printf( 0 ); n     return 0; n   } n   for (int i = sx; i < n; i++) { n     if (s[i][sy] ==  X ) n       ex = i; n     else n       break; n   } n   for (int i = sy; i < m; i++) { n     if (s[sx][i] ==  X ) n       ey = i; n     else n       break; n   } n   ans = 2140000000; n   for (int i = 0; i <= ex - sx; i++) { n     if (able(i, ey - sy)) { n       ans = min(ans, (i + 1) * (ey - sy + 1)); n     } n   } n   for (int i = 0; i <= ey - sy; i++) { n     if (able(ex - sx, i)) { n       ans = min(ans, (i + 1) * (ex - sx + 1)); n     } n   } n   printf( %d , ans == 2140000000 ? -1 : ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 502; n int n, k; n int a[maxn], b[maxn], c[maxn]; n bool check(int l, int r) { n   if (l == r) return true; n   for (int i = l + 1; i <= r; ++i) n     if (a[i] != a[l]) return true; n   return false; n } n bool print(int p, int r, int dir) { n   printf( %d %c n , p, dir == -1 ?  L  :  R ); n   if (dir == -1) --p; n   a[p] += a[p + 1]; n   for (++p; p < r; ++p) a[p] = a[p + 1]; n   return true; n } n bool solve(int l, int r) { n   if (l == r) return false; n   int i, mx = -1; n   for (i = l; i <= r; ++i) mx = max(mx, a[i]); n   for (i = l; i <= r; ++i) { n     if (a[i] != mx) continue; n     if (i == l && a[i] > a[i + 1]) return print(l, r, 1); n     if (i == r && a[i] > a[i - 1]) return print(r, r, -1); n     if (i != l && i != r && a[i] > a[i + 1]) return print(i, r, 1); n     if (i != l && i != r && a[i] > a[i - 1]) return print(i, r, -1); n   } n   return puts( WTF??? ); n } n int main() { n   int i, j, s = 0; n   scanf( %d , &n); n   for (i = 1; i <= n; ++i) scanf( %d , &a[i]); n   scanf( %d , &k); n   for (i = 1; i <= k; ++i) scanf( %d , &b[i]); n   for (i = j = 1; i <= n; ++i) { n     s += a[i]; n     if (s < b[j]) continue; n     if (s > b[j]) return puts( NO ), 0; n     c[j] = i; n     s = 0; n     if (++j > k && i < n) return puts( NO ), 0; n   } n   if (j != k + 1) return puts( NO ), 0; n   for (i = 1; i <= k; ++i) n     if (!check(c[i - 1] + 1, c[i])) return puts( NO ), 0; n   puts( YES ); n   for (i = k; i; --i) n     while (solve(c[i - 1] + 1, c[i])) --c[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int M[16]; n int R[16]; n int N; n int main() { n   cin >> N; n   for (int i = 0; i < N; i++) { n     cin >> M[i]; n   } n   for (int i = 0; i < N; i++) { n     cin >> R[i]; n   } n   int ans = 0; n   for (int i = 0; i < 720720; i++) { n     bool checker = false; n     for (int j = 0; j < N; j++) { n       if (i % M[j] == R[j]) checker = true; n     } n     if (checker) ans++; n   } n   cout << ans / 720720.0 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> v; n long long a, b; n int main() { n   ios_base::sync_with_stdio(0); n   cin >> a >> b; n   while (true) { n     v.push_back(b); n     if (b % 10 == 1) { n       b--; n       b /= 10; n     } else if (b % 2 == 0) { n       b /= 2; n     } else { n       cout <<  NO n ; n       return 0; n     } n     if (b == a) n       break; n     else if (b < a) { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n   cout << v.size() + 1 <<   n ; n   cout << a <<    ; n   for (long long i = v.size() - 1; i >= 0; i--) { n     cout << v[i]; n     cout <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, l; n   cin >> n >> l; n   vector<int> v = vector<int>(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   sort(v.begin(), v.end()); n   int vals = 1; n   int prev = v[0]; n   vector<pair<int, int>> vp; n   vp.push_back(make_pair(prev, 1)); n   for (int i = 1; i < n; i++) { n     if (v[i] == prev) { n       vp[vp.size() - 1].second++; n     } else { n       prev = v[i]; n       vp.push_back(make_pair(prev, 1)); n       vals++; n     } n   } n   double t = log2(vals); n   int fs = t; n   if (t > fs) fs++; n   if (fs * n < l * 8) { n     cout << 0 << endl; n     return 0; n   } n   int max_log = (l * 8) / n; n   int distinct = 1 << max_log; n   int start_p = 0; n   int end_p = vp.size() - 1; n   vector<int> ps = vector<int>(vp.size() + 1, 0); n   ps[0] = 0; n   for (int i = 0; i < vp.size(); i++) ps[i + 1] = ps[i] + vp[i].second; n   vector<int> ss = vector<int>(vp.size() + 1, 0); n   ss[0] = 0; n   for (int i = 0; i < vp.size(); i++) { n     ss[i + 1] = ss[i] + vp[vp.size() - 1 - i].second; n   } n   int mn = n; n   for (int i = 0; i < vals - distinct; i++) { n     if (ps[i] + ss[vals - distinct - i] < mn) n       mn = ps[i] + ss[vals - distinct - i]; n   } n   cout << mn << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e15; n const int N = 2e5 + 10; n int I[N], st[N], fn[N], par[N], h[N], sz[N], big[N], hd[N], n, m, q, timer; n long long w[N]; n vector<int> g[N], V[N]; n struct Node { n   pair<long long, long long> Mn; n   long long Lazy = 0; n } seg[N << 2]; n void apply(int id, long long val) { n   seg[id].Mn.first += val; n   seg[id].Lazy += val; n } n void shift(int id) { n   if (seg[id].Lazy == 0) return; n   apply(id << 1, seg[id].Lazy); n   apply(id << 1 | 1, seg[id].Lazy); n   seg[id].Lazy = 0; n } n void add(int l, int r, long long val, int L = 0, int R = n + m, int id = 1) { n   if (r <= L || R <= l) return; n   if (l <= L && R <= r) { n     apply(id, val); n     return; n   } n   shift(id); n   add(l, r, val, L, (L + R) >> 1, id << 1); n   add(l, r, val, (L + R) >> 1, R, id << 1 | 1); n   seg[id].Mn = min(seg[id << 1].Mn, seg[id << 1 | 1].Mn); n } n pair<long long, long long> get(int l, int r, int L = 0, int R = n + m, n                                int id = 1) { n   if (r <= L || R <= l) return {inf, inf}; n   if (l <= L && R <= r) return seg[id].Mn; n   shift(id); n   return min(get(l, r, L, (L + R) >> 1, id << 1), n              get(l, r, (L + R) >> 1, R, id << 1 | 1)); n } n void Build(int L = 0, int R = n + m, int id = 1) { n   if (R - L == 1) { n     seg[id].Mn = {w[I[L]], I[L]}; n     return; n   } n   Build(L, (L + R) >> 1, id << 1); n   Build((L + R) >> 1, R, id << 1 | 1); n   seg[id].Mn = min(seg[id << 1].Mn, seg[id << 1 | 1].Mn); n   return; n } n int dfs_size(int v) { n   for (int u : g[v]) n     if (u != par[v]) { n       par[u] = v, h[u] = h[v] + 1; n       sz[v] += dfs_size(u); n       if (sz[u] > sz[big[v]]) big[v] = u; n     } n   return ++sz[v]; n } n void dfs_hld(int v) { n   for (int i : V[v]) I[timer] = i, st[i] = timer++; n   if (big[v]) hd[big[v]] = hd[v], dfs_hld(big[v]); n   for (int u : g[v]) n     if (u != par[v] & u != big[v]) hd[u] = u, dfs_hld(u); n   for (int i : V[v]) fn[i] = timer; n } n pair<long long, long long> getmin(int u, int v) { n   pair<long long, long long> res = {inf, inf}; n   while (hd[u] != hd[v]) { n     if (h[hd[v]] > h[hd[u]]) swap(u, v); n     res = min(res, get(st[hd[u]], st[u] + (int)V[u].size())); n     u = par[hd[u]]; n   } n   if (h[v] < h[u]) swap(u, v); n   res = min(res, get(st[u], st[v] + (int)V[v].size())); n   return res; n } n void query(int u, int v, int k) { n   vector<int> ans; n   while (k--) { n     pair<long long, long long> p = getmin(u, v); n     if (p.first >= inf) break; n     ans.push_back(p.second); n     add(st[p.second], st[p.second] + 1, inf); n   } n   cout << ans.size() <<    ; n   for (int i : ans) cout << i - n <<    ; n   cout <<   n ; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   cin >> n >> m >> q; n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     cin >> u >> v; n     g[u].push_back(v); n     g[v].push_back(u); n   } n   for (int i = 1; i <= n + m; i++) { n     if (i <= n) { n       V[i].push_back(i), w[i] = inf; n       continue; n     } n     int c; n     cin >> c; n     V[c].push_back(i), w[i] = i - n; n   } n   dfs_size(1), hd[1] = 1, dfs_hld(1), Build(); n   while (q--) { n     int t; n     cin >> t; n     if (t == 1) { n       int u, v, k; n       cin >> u >> v >> k; n       query(u, v, k); n     } n     if (t == 2) { n       int v, k; n       cin >> v >> k; n       add(st[v], fn[v], k); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e9 + 7; n set<int> a, b; n set<int> pre; n char str[10]; n int main() { n   int n; n   cin >> n; n   long long ans = 1; n   a.insert(0); n   b.insert(1e9 + 5); n   for (int i = 0; i < n; ++i) { n     int x; n     scanf( %s%d , str, &x); n     if (str[1] ==  D ) { n       int l = *a.rbegin(); n       int r = *b.begin(); n       if (x > l && x < r) { n         pre.insert(x); n       } else { n         if (x < l) n           a.insert(x); n         else n           b.insert(x); n       } n     } else { n       if (pre.count(x)) { n         ans *= 2; n         ans %= mod; n         for (auto it : pre) { n           if (it > x) n             b.insert(it); n           else if (it < x) n             a.insert(it); n         } n         pre.clear(); n       } else { n         int l = *a.rbegin(); n         int r = *b.begin(); n         if (x == l) { n           a.erase(x); n           for (auto it : pre) { n             if (it != x) b.insert(it); n           } n         } else if (x == r) { n           b.erase(x); n           for (auto it : pre) { n             if (it != x) a.insert(it); n           } n         } else n           return puts( 0 ); n         pre.clear(); n       } n     } n   } n   int x = pre.size(); n   ans *= (x + 1); n   ans %= mod; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int prev = 0; n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     ans += (n - i) * (i - 1) + i; n     prev = i; n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1}; n const int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1}; n long long dp[1005][1005][2]; n string s; n int k; n int ar[1026]; n long long Dance_of_Death(int x, int rem, int f) { n   if (x == s.size()) { n     if (ar[rem] + 1 == k) { n       return 1; n     } else { n       return 0; n     } n   } n   if (dp[x][rem][f] != -1) { n     return dp[x][rem][f]; n   } n   long long y = 0; n   if (!f && s[x] ==  0 ) { n     y = Dance_of_Death(x + 1, rem, 0) % 1000000007; n   } n   if (f) { n     y = Dance_of_Death(x + 1, rem, f) % 1000000007; n     y = (y + Dance_of_Death(x + 1, rem + 1, f)) % 1000000007; n   } n   if (f == 0 && s[x] ==  1 ) { n     y = Dance_of_Death(x + 1, rem, 1) % 1000000007; n     y = (y + Dance_of_Death(x + 1, rem + 1, f)) % 1000000007; n   } n   return dp[x][rem][f] = y; n } n void build() { n   ar[1] = 0; n   for (int i = 2; i < 1026; i++) { n     int x = i; n     int c = 0; n     while (x) { n       if (x % 2) { n         c++; n       } n       x /= 2; n     } n     ar[i] = ar[c] + 1; n   } n   ar[0] = 10000; n } n int main() { n   build(); n   cin >> s; n   cin >> k; n   memset(dp, -1, sizeof(dp)); n   if (k == 0) { n     cout << 1 << endl; n     return 0; n   } n   int f = 0; n   if (k == 1) { n     f = 1; n   } n   cout << Dance_of_Death(0, 0, 0) - f << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3005; n const int maxm = maxn * maxn << 1; n int n, m, q; n int id[maxn][maxn << 1]; n int fa[maxm]; n int vis[maxm]; n int top = 0; n stack<pair<int, int> > sta; n inline int getfa(int x) { n   sta.push(make_pair(x, fa[x])); n   return fa[x] == x ? x : fa[x] = getfa(fa[x]); n } n inline void bing(int x, int y) { n   if (vis[x] && vis[y]) { n     int fax = getfa(x); n     int fay = getfa(y); n     if (fax != fay) fa[fax] = fay; n   } n } n inline void Union(int x, int y) { n   int now = id[x][y]; n   bing(id[x + 1][y], now); n   bing(id[x + 1][y - 1], now); n   bing(id[x + 1][y + 1], now); n   bing(id[x - 1][y], now); n   bing(id[x - 1][y - 1], now); n   bing(id[x - 1][y + 1], now); n   bing(id[x][y - 1], now); n   bing(id[x][y + 1], now); n } n inline void rm() { n   while (!sta.empty()) { n     fa[sta.top().first] = sta.top().second; n     sta.pop(); n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &q); n   int __cnt = 0; n   for (int i = 1; i <= n; ++i) { n     for (int j = 1; j <= (m << 1); ++j) { n       id[i][j] = ++__cnt; n       fa[id[i][j]] = id[i][j]; n     } n   } n   for (int i = 1; i <= n; ++i) { n     id[i][0] = id[i][m << 1]; n     id[i][m << 1 | 1] = id[i][1]; n   } n   int ans = 0; n   while (q--) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (vis[id[x][y]]) continue; n     vis[id[x][y]] = vis[id[x][y + m]] = true; n     Union(x, y); n     Union(x, y + m); n     if (getfa(id[x][y]) == getfa(id[x][y + m])) { n       rm(); n       vis[id[x][y]] = vis[id[x][y + m]] = false; n     } else n       ans++; n     while (!sta.empty()) sta.pop(); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast,unroll-loops ) n #pragma GCC target( avx,avx2,sse,sse2 ) n template <class c> n struct rge { n   c b, e; n }; n template <class c> n rge<c> range(c i, c j) { n   return rge<c>{i, j}; n } n template <class c> n auto dud(c *x) -> decltype(cerr << *x, 0); n template <class c> n char dud(...); n struct debug { n   template <class c> n   debug &operator<<(const c &) { n     return *this; n   } n }; n struct segment { n   long long a, b; n   int t; n   int id; n   segment(long long _a = 0, long long _b = 0, int _t = 0, int _id = 0) { n     a = _a; n     b = _b; n     t = _t; n     id = _id; n   } n }; n struct query { n   long long a, b; n   int t; n   query(long long _a = 0, long long _b = 0, int _t = 0) { n     a = _a; n     b = _b; n     t = _t; n   } n }; n const int MAXN = 51; n const int MAXM = 5e4 + 10; n vector<segment> segments; n vector<query> queries; n vector<int> gr[2 * MAXN * MAXM]; n int t[2 * MAXN * MAXM]; n int card[2 * MAXN * MAXM]; n int no = 0; n vector<segment> rec(long long l, long long r, vector<segment> &segments) { n   vector<segment> s; n   if (!segments.size()) { n     s.emplace_back(l, r, 1e9, no++); n     return s; n   } n   if (segments.size() == 1 && segments[0].a == l && segments[0].b == r) { n     s.emplace_back(l, r, segments[0].t, no++); n     return s; n   } n   vector<segment> s_left; n   vector<segment> s_right; n   long long m = l + r >> 1; n   for (auto &x : segments) { n     if (x.b <= m) { n       s_left.emplace_back(x); n     } else if (x.a > m) { n       s_right.emplace_back(x); n     } else { n       s_left.emplace_back(x.a, m, x.t); n       s_right.emplace_back(m + 1, x.b, x.t); n     } n   } n   auto ss_left = rec(l, m, s_left); n   auto ss_right = rec(m + 1, r, s_right); n   int long long = 0, rr = 0; n   long long dif = m + 1 - l; n   while (long long < ss_left.size() && rr < ss_right.size()) { n     if ((ss_left[long long].a >= ss_right[rr].a - dif && n          ss_left[long long].a <= ss_right[rr].b - dif) || n         (ss_right[rr].a >= ss_left[long long].a + dif && n          ss_right[rr].a <= ss_left[long long].b + dif)) { n       gr[ss_left[long long].id].emplace_back(ss_right[rr].id); n       gr[ss_right[rr].id].emplace_back(ss_left[long long].id); n     } n     if (ss_left[long long].b <= ss_right[rr].b - dif) { n       ++long long; n     } else { n       ++rr; n     } n   } n   for (auto &x : ss_right) { n     ss_left.emplace_back(x); n   } n   return ss_left; n } n int root(int x) { return x == t[x] ? x : t[x] = root(t[x]); } n void onion(int x, int y) { n   if ((x = root(x)) == (y = root(y))) return; n   if (card[x] < card[y]) swap(x, y); n   t[y] = x; n   card[x] += card[y]; n   return; n }; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   for (int i = 1; i <= m; ++i) { n     string s; n     long long a, b; n     cin >> s >> a >> b; n     if (s ==  ask ) { n       queries.emplace_back(a, b, i); n     } else { n       segments.emplace_back(a, b, i, segments.size()); n     } n   } n   auto s = rec(0, (1ll << n) - 1, segments); n   for (int i = 0; i < no; ++i) { n     t[i] = i; n     card[i] = 1; n   } n   reverse((queries).begin(), (queries).end()); n   vector<int> alive(no); n   auto s_time = s; n   sort((s_time).begin(), (s_time).end(), n        [&](segment &a, segment &b) { return a.t > b.t; }); n   auto getSegment = [&](long long a) { n     int ret = 0; n     int l = 0, r = s.size() - 1; n     while (l <= r) { n       int m = l + r >> 1; n       if (s[m].a <= a && s[m].b >= a) return s[m].id; n       if (s[m].b < a) { n         l = m + 1; n       } else { n         r = m - 1; n       } n     } n     assert(false); n     return -1; n   }; n   int now = 0; n   vector<int> ans; n   for (auto &x : queries) { n     while (now < s_time.size() && s_time[now].t >= x.t) { n       alive[s_time[now].id] = 1; n       for (auto &x : gr[s_time[now].id]) { n         if (alive[x]) onion(root(s_time[now].id), root(x)); n       } n       ++now; n     } n     ans.emplace_back(root(getSegment(x.a)) == root(getSegment(x.b))); n   } n   reverse((ans).begin(), (ans).end()); n   for (auto &x : ans) cout << x <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } n long long Max(long long a, long long b) { return a > b ? a : b; } n long long Min(long long a, long long b) { return a < b ? a : b; } n long long power(long long x, long long y, long long p) { n   long long res = 1; n   x = x % p; n   while (y > 0) { n     if (y & 1) res = ((res % p) * (x % p)) % p; n     y = y >> 1; n     x = ((x % p) * (x % p)) % p; n   } n   return res; n } n bool isprime(long long n) { n   if (n == 2) return true; n   if (n % 2 == 0) return false; n   for (long long i = 3; i * i <= n; i += 2) { n     if (n % i == 0) return false; n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n, lo = 0, hi = 1000000007LL, ans = -1000000007LL, flag = 0; n   cin >> n; n   vector<pair<long long, long long> > arr(n); n   vector<pair<long long, long long> > poss(n + 1); n   for (long long i = 0; i < n; i++) { n     cin >> arr[i].first >> arr[i].second; n     if (arr[i].second == 2) flag = 1; n   } n   if (arr[0].second == 1) { n     poss[0].first = 1900; n     poss[0].second = 1000000007LL; n   } else { n     poss[0].first = -1000000007LL; n     poss[0].second = 1899; n   } n   for (long long i = 1; i < n; i++) { n     poss[i].first = poss[i - 1].first + arr[i - 1].first; n     poss[i].second = poss[i - 1].second + arr[i - 1].first; n     if (poss[i].first > poss[i].second) { n       cout <<  Impossible ; n       return 0; n     } n     if (arr[i].second == 1) { n       poss[i].first = Max(poss[i].first, 1900); n       if (poss[i].second < poss[i].first) { n         cout <<  Impossible ; n         return 0; n       } n     } else { n       poss[i].second = Min(poss[i].second, 1899); n       if (poss[i].second < poss[i].first) { n         cout <<  Impossible ; n         return 0; n       } n     } n   } n   poss[n].first = poss[n - 1].first + arr[n - 1].first; n   poss[n].second = poss[n - 1].second + arr[n - 1].first; n   if (poss[n].first > poss[n].second) { n     cout <<  Impossible ; n     return 0; n   } else if (flag == 0) { n     cout <<  Infinity ; n   } else { n     cout << poss[n].second; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n unsigned long long n, k, c, a, b, d; n multiset<unsigned long long> vn; n multiset<unsigned long long> vk; n unsigned long long mod = 1e9 + 7; n unsigned long long sum = 0; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cin >> n >> k >> c; n   for (int i = 1; i <= n; i++) { n     cin >> a >> b; n     vn.insert(a), vk.insert(b); n     d = c * (b - a); n     sum = (sum + d) % mod; n   } n   multiset<unsigned long long>::iterator it; n   while (vn.size() > 0 && vk.size() > 0) { n     if (*vn.begin() <= *vk.begin()) { n       sum = (k + sum) % mod; n       vn.erase(vn.find(*vn.begin())); n     } else { n       if ((*vn.begin() - *vk.begin()) * c < k) { n         int t; n         for (it = vk.begin(); it != vk.end(); it++) { n           if ((*it) >= *vn.begin()) break; n           t = (*it); n         } n         sum = (sum + (*vn.begin() - t) * c) % mod; n         vn.erase(vn.find(*vn.begin())); n         vk.erase(vk.find(t)); n       } else { n         vk.erase(*vk.begin()); n       } n     } n   } n   cout << sum % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   long long ans = 0; n   for (int i = (0); i < (n); ++i) { n     long long t, T, x, cost; n     cin >> t >> T >> x >> cost; n     if (t >= T) { n       ans += cost + m * x; n       continue; n     } n     long long aux1 = cost; n     if (m > (T - t)) aux1 += m * x; n     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; n     aux2 *= cost; n     ans += min(aux1, aux2); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1000 + 10; n long long par[maxn], t[maxn], c[maxn]; n const long long inf = 1000000000000ll; n long long dist[maxn][maxn], dis[maxn]; n bool mark[maxn]; n vector<int> v[maxn], u[maxn]; n set<pair<long long, int> > s; n vector<long long> cost1[maxn], cost2[maxn]; n int main() { n   int n, m, x, y; n   cin >> n >> m >> x >> y; n   x--; n   y--; n   for (int i = 0; i < m; i++) { n     int a, b, z; n     cin >> a >> b >> z; n     a--; n     b--; n     v[a].push_back(b); n     v[b].push_back(a); n     cost1[a].push_back(z); n     cost1[b].push_back(z); n   } n   for (int i = 0; i < n; i++) { n     cin >> t[i] >> c[i]; n   } n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) dist[i][j] = inf; n   } n   for (int i = 0; i < n; i++) { n     for (int k = 0; k < n; k++) mark[k] = 0; n     dist[i][i] = 0; n     s.insert(make_pair(0, i)); n     while (!s.empty()) { n       int cur = s.begin()->second; n       s.erase(s.begin()); n       mark[cur] = true; n       for (int j = 0; j < v[cur].size(); j++) { n         int next = v[cur][j]; n         if (mark[next] == true) continue; n         long long ndist = dist[i][cur] + cost1[cur][j]; n         if (dist[i][next] > ndist) { n           par[next] = cur; n           s.erase(make_pair(dist[i][next], next)); n           dist[i][next] = min(ndist, dist[i][next]); n           s.insert(make_pair(dist[i][next], next)); n         } n       } n     } n   } n   for (int i = 0; i < n; i++) mark[i] = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; j++) { n       if (dist[i][j] > 0 && dist[i][j] <= t[i]) { n         u[i].push_back(j); n         cost2[i].push_back(c[i]); n       } n     } n   } n   for (int i = 0; i < n; i++) dis[i] = inf; n   dis[x] = 0; n   s.insert(make_pair(0, x)); n   while (!s.empty()) { n     int cur = s.begin()->second; n     s.erase(s.begin()); n     mark[cur] = true; n     for (int i = 0; i < u[cur].size(); i++) { n       int next = u[cur][i]; n       if (mark[next] == true) continue; n       long long ndist = dis[cur] + cost2[cur][i]; n       if (dis[next] > ndist) { n         s.erase(make_pair(dis[next], next)); n         dis[next] = min(ndist, dis[next]); n         s.insert(make_pair(dis[next], next)); n       } n     } n   } n   if (mark[y] == true) n     cout << dis[y]; n   else n     cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n const int maxn = 1e5 + 5; n string s; n set<pair<int, int>> t; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> s; n   int cnt = 0; n   for (int i = (int)s.size() - 1; i >= 0; i--) { n     if (s[i] ==  0 ) n       cnt++; n     else { n       if (cnt) n         cnt--; n       else n         s[i] =  0 ; n     } n   } n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n const int NMAX = 50505; n const int LMAX = 1600505; n int N, A[NMAX]; n vector<int> G[NMAX]; n int currIdx; n unordered_map<int, int> allValues; n vector<int> newValues; n int L[NMAX]; n bool mark[NMAX]; n vector<int> R; n void read() { n   scanf( %d , &N); n   for (int i = 1; i <= N; i++) { n     scanf( %d , &A[i]); n   } n   for (int i = 1; i <= N; i++) { n     G[i].reserve(30); n   } n } n inline int bit(int idx) { return 1 << (idx - 1); } n inline bool hasBit(int mask, int idx) { return mask & bit(idx); } n void findCandidates(int x, int maxV, vector<int>& result) { n   int fstBit; n   for (int i = 30; i >= 1; i--) { n     if (hasBit(x, i)) { n       fstBit = i; n       break; n     } n   } n   int curr = 0; n   for (int i = fstBit; i >= 1; i--) { n     curr = (curr << 1) + hasBit(x, i); n     if (curr > maxV) { n       break; n     } n     result.push_back(curr); n   } n } n void clearPrevIter() { n   allValues.clear(); n   newValues.clear(); n   for (int i = 1; i <= N; i++) { n     G[i].clear(); n   } n   currIdx = N; n } n bool pairup(int x) { n   mark[x] = true; n   for (auto& y : G[x]) { n     if (!R[y] || (!mark[R[y]] && pairup(R[y]))) { n       L[x] = y; n       R[y] = x; n       return true; n     } n   } n   return false; n } n bool canMatch() { n   memset(L, 0, sizeof(L)); n   R.assign(currIdx + 1, 0); n   bool improve = true; n   int currMatches = 0; n   while (improve) { n     improve = false; n     memset(mark, false, sizeof(mark)); n     for (int i = 1; i <= N; i++) { n       if (!L[i] && pairup(i)) { n         improve = true; n         currMatches++; n       } n     } n   } n   return currMatches == N; n } n bool check(int maxV) { n   vector<int> currCandidates; n   currCandidates.reserve(30); n   clearPrevIter(); n   for (int i = 1; i <= N; i++) { n     currCandidates.clear(); n     findCandidates(A[i], maxV, currCandidates); n     for (auto& cand : currCandidates) { n       auto it = allValues.find(cand); n       int matchIdx; n       if (it == allValues.end()) { n         allValues[cand] = ++currIdx; n         newValues.push_back(cand); n         matchIdx = currIdx; n       } else { n         matchIdx = it->second; n       } n       G[i].push_back(matchIdx); n     } n   } n   return canMatch(); n } n int bSearch() { n   int i, step = (1 << 30); n   for (i = 0; step; step >>= 1) { n     if (!check(i + step)) { n       i += step; n     } n   } n   return ++i; n } n void solve() { n   int maxElem = bSearch(); n   check(maxElem); n   for (int i = 1; i <= N; i++) { n     if (i > 1) { n       printf(   ); n     } n     printf( %d , newValues[L[i] - N - 1]); n   } n   printf(  n ); n } n int main() { n   read(); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   ios::sync_with_stdio(false); n   register int i; n   register priority_queue<int> Queue; n   register int N, T; n   cin >> N >> T; n   register int Ans; n   Ans = 0; n   for (i = 1; i <= N; i += 1) { n     while (!Queue.empty() && Queue.top() >= T - i) { n       Queue.pop(); n     } n     register int X; n     cin >> X; n     if (max(X, i) < T) { n       Queue.push(X - i); n     } n     Ans = max(Ans, (int)Queue.size()); n   } n   cout << Ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double v[1000006], t[1000006]; n int main() { n   int n, i; n   double a, d; n   scanf( %d , &n); n   scanf( %lf , &a); n   scanf( %lf , &d); n   for (i = 0; i < n; i++) { n     scanf( %lf , &t[i]); n     scanf( %lf , &v[i]); n   } n   double t0 = sqrt(2 * d / a); n   double v0 = a * t0; n   double prevt = -1, curt; n   for (i = 0; i < n; i++) { n     if (v[i] >= v0) { n       curt = t[i] + t0; n     } else { n       double t1 = v[i] / a; n       double d1 = 0.5 * a * t1 * t1; n       double t2 = (d - d1) / v[i]; n       curt = t[i] + t1 + t2; n     } n     curt = max(curt, prevt); n     printf( %.9lf n , curt); n     prevt = curt; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool ch[30]; n int whenlast[30]; n char g[1000005]; n int main() { n   int n, k; n   cin >> n >> k; n   for (int i = 0; i < n; i++) { n     scanf(  %c , &g[i]); n     whenlast[g[i] -  A ] = i; n   } n   int rlt = 0, ans = 0; n   for (int i = 0; i < n; i++) { n     ch[g[i] -  A ] = true; n     rlt = 0; n     for (int p = 0; p < 30; p++) n       if (ch[p]) rlt++; n     ans = max(ans, rlt); n     for (int x = 0; x < 30; x++) n       if (whenlast[x] == i) ch[x] = false; n   } n   if (ans <= k) n     puts( NO ); n   else n     puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n long long R = 7 + 1e9, NUMTESTCASE; n const int NN = 10 + 2e6; n const double pi = acos(-1.0); n int di[4] = {1, 0, -1, 0}, dj[4] = {0, 1, 0, -1}, n     DI[8] = {1, 1, 0, -1, -1, -1, 0, 1}, DJ[8] = {0, 1, 1, 1, 0, -1, -1, -1}; n using namespace std; n int n, ti = 1, timein[NN], timeout[NN], lev[NN], parn[NN], Hash[NN], m, n        segtree[NN]; n vector<int> G[NN]; n void DFS(int ver, int par, int cnt) { n   int in = ti++; n   for (int u : G[ver]) n     if (u != par) DFS(u, ver, cnt + 1); n   Hash[ver] = ++m; n   lev[Hash[ver]] = cnt; n   parn[Hash[ver]] = par; n   timein[Hash[ver]] = in; n   timeout[Hash[ver]] = ti++; n } n bool isgra(int ver, int par) { n   return (timein[par] <= timein[ver] && timeout[par] >= timeout[ver]); n } n unordered_map<int, unordered_map<int, int> > memo; n int com_par(int u, int v) { n   if (isgra(v, u)) return u; n   if (isgra(u, v)) return v; n   auto it1 = memo.find(u); n   if (it1 != memo.end()) { n     auto it2 = it1->second.find(v); n     if (it2 != it1->second.end()) return it2->second; n   } n   return memo[u][v] = memo[v][u] = com_par(u, parn[v]); n } n void build(int pos, int st, int en) { n   if (st == en) { n     segtree[pos] = st; n     return; n   } n   build(2 * pos, st, (st + en) / 2); n   build(2 * pos + 1, (st + en) / 2 + 1, en); n   segtree[pos] = com_par(segtree[2 * pos], segtree[2 * pos + 1]); n } n int seg_com_par(int pos, int st, int en, int u, int v) { n   if (en < u || st > v) return -1; n   if (st >= u && en <= v) return segtree[pos]; n   int flag1 = seg_com_par(2 * pos, st, (st + en) / 2, u, v); n   int flag2 = seg_com_par(2 * pos + 1, (st + en) / 2 + 1, en, u, v); n   int res; n   if (flag1 != -1 && flag2 != -1) n     res = com_par(flag1, flag2); n   else if (flag1 != -1) n     res = flag1; n   else n     res = flag2; n   return res; n } n int dis(int v, int u) { n   if (u > v) swap(u, v); n   return lev[v] + lev[u] - 2 * lev[seg_com_par(1, 1, m, u, v)]; n } n int main() { n   cin >> n; n   int u, v; n   for (int i = (1); i <= (n - 1); ++i) { n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n     G[v].push_back(u); n   } n   DFS(1, -1, 0); n   parn[Hash[1]] = 1; n   for (int i = (1); i <= (n); ++i) parn[i] = Hash[parn[i]]; n   build(1, 1, m); n   int x, y, a, b, k, q; n   cin >> q; n   while (q--) { n     scanf( %d%d%d%d%d , &x, &y, &a, &b, &k); n     x = Hash[x]; n     y = Hash[y]; n     a = Hash[a]; n     b = Hash[b]; n     int flag = 0, ab = dis(a, b), axyb = 1 + dis(a, x) + dis(y, b), n         ayxb = 1 + dis(a, y) + dis(x, b); n     if (ab % 2 == k % 2 && ab <= k) flag = 1; n     if (axyb % 2 == k % 2 && axyb <= k) flag = 1; n     if (ayxb % 2 == k % 2 && ayxb <= k) flag = 1; n     printf(flag ?  YES n  :  NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100005; n int n, q; n int f[MAXN]; n long long t[MAXN]; n set<pair<long long, int> > seta[MAXN]; n unordered_map<int, long long> child_profit[MAXN]; n long long child_sum[MAXN]; n set<pair<long long, int> > global_seta_min, global_seta_max; n long long minAns[MAXN]; n long long maxAns[MAXN]; n const long long inf = 1e18; n void init() { n   for (int i = 1; i < n + 1; ++i) { n     child_sum[i] = 0; n   } n } n void preprocess() { n   for (int i = 1; i < n + 1; ++i) { n     long long aroundProfit = t[i] / (seta[i].size() + 2); n     long long ownProfit = t[i] - (seta[i].size() + 1) * aroundProfit; n     for (auto item : seta[i]) n       child_sum[i] += t[item.second] / (seta[item.second].size() + 2); n     seta[f[i]].erase(make_pair(0, i)); n     child_profit[f[i]][i] = ownProfit + child_sum[i]; n     seta[f[i]].insert(make_pair(child_profit[f[i]][i], i)); n   } n   for (int u = 1; u < n + 1; ++u) { n     long long delta = t[u] / (seta[u].size() + 2); n     minAns[u] = (seta[u].size() > 0) ? (seta[u].begin()->first + delta) : inf; n     maxAns[u] = (seta[u].size() > 0) ? (seta[u].rbegin()->first + delta) : 0; n     global_seta_min.insert(make_pair(minAns[u], u)); n     global_seta_max.insert(make_pair(maxAns[u], u)); n   } n } n void updateMinMax(int u) { n   global_seta_min.erase(make_pair(minAns[u], u)); n   global_seta_max.erase(make_pair(maxAns[u], u)); n   long long delta = t[u] / (seta[u].size() + 2); n   minAns[u] = (seta[u].size() > 0) ? seta[u].begin()->first + delta : inf; n   maxAns[u] = (seta[u].size() > 0) ? seta[u].rbegin()->first + delta : 0; n   global_seta_min.insert(make_pair(minAns[u], u)); n   global_seta_max.insert(make_pair(maxAns[u], u)); n } n long long calcProfitWithoutParent(int u) { n   return child_sum[u] + t[u] - n          (seta[u].size() + 1) * (t[u] / (seta[u].size() + 2)); n } n void solve() { n   scanf( %d%d , &n, &q); n   init(); n   for (int i = 1; i < n + 1; ++i) scanf( %I64d , &t[i]); n   for (int i = 1; i < n + 1; ++i) { n     scanf( %I64d , &f[i]); n     seta[f[i]].insert(make_pair(0, i)); n     child_profit[f[i]][i] = 0; n   } n   preprocess(); n   long long profit; n   int newF, oldF; n   for (int i = 0; i < q; ++i) { n     int type; n     scanf( %d , &type); n     switch (type) { n       case 1: n         int a, b; n         scanf( %d%d , &a, &b); n         oldF = f[a]; n         newF = b; n         child_sum[oldF] -= t[a] / (seta[a].size() + 2); n         child_sum[f[oldF]] -= t[oldF] / (seta[oldF].size() + 2); n         seta[oldF].erase(make_pair(child_profit[oldF][a], a)); n         child_profit[oldF].erase(a); n         child_sum[f[oldF]] += t[oldF] / (seta[oldF].size() + 2); n         seta[f[f[oldF]]].erase( n             make_pair(child_profit[f[f[oldF]]][f[oldF]], f[oldF])); n         child_profit[f[f[oldF]]][f[oldF]] = calcProfitWithoutParent(f[oldF]); n         seta[f[f[oldF]]].insert( n             make_pair(child_profit[f[f[oldF]]][f[oldF]], f[oldF])); n         seta[f[oldF]].erase(make_pair(child_profit[f[oldF]][oldF], oldF)); n         child_profit[f[oldF]][oldF] = calcProfitWithoutParent(oldF); n         seta[f[oldF]].insert(make_pair(child_profit[f[oldF]][oldF], oldF)); n         updateMinMax(f[f[oldF]]); n         updateMinMax(f[oldF]); n         updateMinMax(oldF); n         child_sum[newF] += t[a] / (seta[a].size() + 2); n         child_sum[f[newF]] -= t[newF] / (seta[newF].size() + 2); n         child_profit[newF].insert(make_pair(a, calcProfitWithoutParent(a))); n         seta[newF].insert(make_pair(child_profit[newF][a], a)); n         child_sum[f[newF]] += t[newF] / (seta[newF].size() + 2); n         seta[f[f[newF]]].erase( n             make_pair(child_profit[f[f[newF]]][f[newF]], f[newF])); n         child_profit[f[f[newF]]][f[newF]] = calcProfitWithoutParent(f[newF]); n         seta[f[f[newF]]].insert( n             make_pair(child_profit[f[f[newF]]][f[newF]], f[newF])); n         seta[f[newF]].erase(make_pair(child_profit[f[newF]][newF], newF)); n         child_profit[f[newF]][newF] = calcProfitWithoutParent(newF); n         seta[f[newF]].insert(make_pair(child_profit[f[newF]][newF], newF)); n         updateMinMax(f[f[newF]]); n         updateMinMax(f[newF]); n         updateMinMax(newF); n         f[a] = newF; n         break; n       case 2: n         int x; n         scanf( %d , &x); n         profit = t[x] - (seta[x].size() + 1) * (t[x] / (seta[x].size() + 2)) + n                  child_sum[x] + t[f[x]] / (seta[f[x]].size() + 2); n         printf( %I64d n , profit); n         break; n       case 3: n         printf( %I64d %I64d n , global_seta_min.begin()->first, n                global_seta_max.rbegin()->first); n         break; n     } n   } n } n int main() { n   solve(); n   return 0; n } n


#include <cmath> n  n #include <algorithm> n #include <iostream> n #include <string> n #include <vector> n  n using namespace std; n  n typedef long double ld; n typedef vector<int> vi; n typedef long long ll; n  n constexpr ld EPS = numeric_limits<ld>::epsilon(); n constexpr int INF = numeric_limits<int>::max() - 10; n constexpr ll LINF = numeric_limits<ll>::max() - 10; n  n int w; n int h; n int n; n  n void input() { n   cin >> w >> h >> n; n } n  n void solve() { n   int alpha = (w & -w); n   int beta = (h & -h); n  n   string ans =  NO ; n   if (alpha * beta >= n) n     ans =  YES ; n  n   cout << ans <<   n ; n } n  n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n  n   cout.precision(16); n   cout << fixed; n  n   int _times = 1; n  n #ifdef _DEBUG n   freopen( input.txt ,  r , stdin); n   freopen( output.txt ,  w , stdout); n #endif n  n   cin >> _times; n  n   while (_times--) { n     input(); n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int a[n], b[m]; n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < m; i++) cin >> b[i]; n   int c[n]; n   for (int i = 0; i < n; i++) { n     int check = a[i]; n     int j = 0; n     for (; j < m; j++) n       if (b[j] == check) { n         c[i] = 1; n         break; n       } n     if (j == m) c[i] = 0; n   } n   for (int i = 0; i < n; i++) n     if (c[i] == 1) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000007; n int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; n int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; n long long POW(long long x, long long y) { n   long long ans = 1; n   long long base = x; n   while (y) { n     if (y & 1) { n       ans *= base; n     } n     base = base * base; n     y = y >> 1; n   } n   return ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long n, i, j, k, t, temp, tc; n   cin >> tc; n   while (tc--) { n     cin >> n; n     string a, b; n     vector<long long> ans; n     cin >> a >> b; n     for (i = n - 1; i >= 0; i--) { n       if (a[i] != b[i]) { n         if (a[0] != b[i]) { n           ans.push_back(i); n         } else { n           ans.push_back(0); n           if (b[i] ==  1 ) { n             a[0] =  0 ; n           } else { n             a[0] =  1 ; n           } n           ans.push_back(i); n         } n         for (j = 0; j <= i; j++) { n           if (a[j] ==  1 ) { n             a[j] =  0 ; n           } else { n             a[j] =  1 ; n           } n         } n         long long lb = 0, ub = i; n         while (lb < ub) { n           auto ch = a[lb]; n           a[lb] = a[ub]; n           a[ub] = ch; n           lb++; n           ub--; n         } n       } n     } n     cout << ans.size() <<    ; n     for (auto x : ans) { n       cout << x + 1 <<    ; n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inft = 1000000009; n const int MAXN = 400005, MAXM = MAXN; n int n; n vector<int> adj[MAXN]; n int edgx[MAXM]; n int cc; n int cycle[MAXM]; n char used[MAXM]; n int ep, sp, cur[MAXN], stackedge[MAXM]; n void search(int v) { n   sp = 0; n   while (1) { n     while (cur[v] != adj[v].size()) { n       int e = adj[v][cur[v]++]; n       if (used[e]) continue; n       used[e] = 1; n       stackedge[sp++] = e; n       v ^= edgx[e]; n     } n     sp--; n     if (sp < 0) break; n     cycle[cc++] = stackedge[sp]; n     v ^= edgx[stackedge[sp]]; n   } n } n void search_all() { n   for (int v = 0; v < (n); ++v) search(v); n } n void addedge(int a, int b) { n   edgx[ep] = a ^ b; n   adj[a].push_back(ep); n   adj[b].push_back(ep); n   ep++; n } n void clear() { n   for (int i = 0; i < (n); ++i) adj[i].clear(); n   cc = 0; n   ep = 0; n   sp = 0; n   for (int i = 0; i < (n); ++i) cur[i] = 0; n } n int X[MAXN], Y[MAXN]; n char ANS[MAXN]; n int main() { n   int nn; n   scanf( %d , &nn); n   n = MAXN; n   for (int i = 0; i < (nn); ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     ++X[a]; n     ++Y[b]; n     addedge(a, b + MAXN / 2); n   } n   for (int i = 0; i < (MAXN / 2); ++i) n     if (X[i] % 2) addedge(0, i); n   for (int i = 0; i < (MAXN / 2); ++i) n     if (Y[i] % 2) addedge(0, MAXN / 2 + i); n   search_all(); n   for (int i = 0; i < (ep); ++i) n     if (cycle[i] < nn) ANS[cycle[i]] = i % 2 ?  r  :  b ; n   printf( %s n , ANS); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int b, w; n   cin >> w >> b; n   if (w == 0) n     cout <<  0.000000000  << endl; n   else if (b == 0) n     cout <<  1.000000000  << endl; n   else if (b == 1) n     printf( %.9f n , double(w) / (w + 1)); n   else if (b == 2) n     printf( %.9f n , double(w * (w + 1) + 2) / ((w + 1) * (w + 2))); n   else { n     double p[w + 1][b + 1]; n     if (b >= 2 * w) { n       for (int col = b - 2 * w; col >= 0; col -= 3) p[0][col] = 0.0; n       int startcol, endcol; n       for (int row = 1; row <= w; row++) { n         startcol = (b + w - row) % 3; n         endcol = b - 2 * (w - row); n         if (startcol == 0) n           p[row][startcol] = 1.0; n         else if (startcol == 1) n           p[row][startcol] = double(row) / (row + 1); n         else n           p[row][startcol] = n               double(row * (row + 1) + 2) / ((row + 1) * (row + 2)); n         for (int col = startcol + 3; col <= endcol; col += 3) { n           p[row][col] = n               double(row) / (row + col) + n               (p[row][col - 3] * (col - 2) + p[row - 1][col - 2] * row) * col * n                   (col - 1) / (row + col) / (row + col - 1) / (row + col - 2); n         } n       } n     } else { n       int startcol, endcol; n       int firstrow = w - b / 2; n       for (int row = firstrow; row <= w; row++) { n         startcol = (b + w - row) % 3; n         endcol = b - 2 * (w - row); n         if (startcol == 0) n           p[row][startcol] = 1.0; n         else if (startcol == 1) n           p[row][startcol] = double(row) / (row + 1); n         else n           p[row][startcol] = n               double(row * (row + 1) + 2) / ((row + 1) * (row + 2)); n         for (int col = startcol + 3; col <= endcol; col += 3) { n           p[row][col] = n               double(row) / (row + col) + n               (p[row][col - 3] * (col - 2) + p[row - 1][col - 2] * row) * col * n                   (col - 1) / (row + col) / (row + col - 1) / (row + col - 2); n         } n       } n     } n     printf( %.9f n , p[w][b]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> s[100010]; n bool flag[100010]; n long double ans; n void dfs(int now, int deep) { n   ans += 1.0 / (deep * 1.0); n   flag[now] = false; n   for (int i = 0; i < s[now].size(); i++) { n     int next = s[now][i]; n     if (flag[next]) dfs(next, deep + 1); n   } n } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     s[x].push_back(y); n     s[y].push_back(x); n   } n   memset(flag, true, sizeof(flag)); n   ans = 0; n   dfs(1, 1); n   cout << fixed << setprecision(6) << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, t, m; n int vi[200010]; n vector<int> v[200010]; n map<int, int> mp; n int main() { n   long long int a, b, c, r, l = 0, tt; n   cin >> r >> a >> b >> c; n   if ((b - c) <= a) { n     if (r >= b) { n       tt = (long long int)((double)(r - b) / (double)(b - c)); n       t = tt++; n       if (t == tt) { n         t++; n       } n       r = r - t * (b - c); n       l = l + t; n       while (r >= b) { n         r = r - (b - c); n         l++; n       } n     } n     if (r >= a) { n       t = r / a; n       r = r - t * a; n       l = l + t; n     } n   } else { n     if (r >= a) { n       t = r / a; n       r = r - t * a; n       l = l + t; n     } n     if (r >= b) { n       tt = (long long int)((double)(r - b) / (double)(b - c)); n       t = tt++; n       if (t == tt) { n         t++; n       } n       r = r - t * (b - c); n       l = l + t; n       while (r >= b) { n         r = r - (b - c); n         l++; n       } n     } n   } n   cout << l << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cout <<  Karen ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class UnionFind { n  private: n  public: n   vector<int> p, rank, setSize, minn; n   int numSets; n   UnionFind(int N) { n     setSize.assign(N, 1); n     numSets = N; n     rank.assign(N, 0); n     p.assign(N, 0); n     minn.resize(N); n     for (int i = 0; i < N; i++) { n       p[i] = i; n       minn[i] = i; n     } n   } n   int findSet(int i) { n     if (p[i] == i) n       return i; n     else { n       p[i] = findSet(p[i]); n       minn[p[i]] = min(minn[p[i]], i); n       return p[i]; n     } n   } n   bool isSameSet(int i, int j) { return findSet(i) == findSet(j); } n   void unionSet(int i, int j) { n     if (!isSameSet(i, j)) { n       numSets--; n       int x = findSet(i), y = findSet(j); n       if (rank[x] > rank[y]) { n         p[y] = x; n         setSize[x] += setSize[y]; n         minn[x] = min(minn[x], minn[y]); n       } else { n         p[x] = y; n         setSize[y] += setSize[x]; n         minn[y] = min(minn[x], minn[y]); n         if (rank[x] == rank[y]) rank[y]++; n       } n     } n   } n   int numDisjointSets() { return numSets; } n   int sizeOfSet(int i) { return setSize[findSet(i)]; } n }; n class fenwick_tree { n   vector<long> t; n   long size; n  n  public: n   fenwick_tree(long n) { n     t.assign(n + 1, 0); n     size = n + 1; n   } n   long LSOne(long k) { return k & (-k); } n   void update(long index, long val) { n     for (; index < size; index += LSOne(index)) t[index] += val; n   } n   long query_full(long a, long b) { n     return query(b) - ((a <= 1) ? 0 : query(a - 1)); n   } n   long query(long b) { n     if (b < 1) return 0; n     long sum = 0; n     for (; b; b -= LSOne(b)) sum += t[b]; n     return sum; n   } n }; n long long gcd(long long a, long long b) { n   while (b != 0) { n     long long t = b; n     b = a % b; n     a = t; n   } n   return a; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   long long w, h, i, j, k, ans, c, s; n   cin >> w >> h; n   ans = 0; n   for (i = 1; i < w; i++) { n     c = min(i, w - i); n     s = 0; n     for (j = 0; j < h; j++) s += (h - j) / 2; n     ans += c * s; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1 << 29; n const double EPS = 1e-10; n const double PI = 3.1415926535897932384626433832795; n const int CLK = CLOCKS_PER_SEC; n int calc(int s, int f, int t, int m) { n   if (s == f) return 0; n   s--; n   f--; n   t %= (2 * (m - 1)); n   int tos; n   int stof; n   bool up = (t < m - 1); n   int pos = (t < m - 1) ? t : (2 * m - 2) - t; n   if (up) { n     if (s >= pos) { n       tos = s - pos; n     } else { n       tos = (2 * m - 2) - pos - s; n       up = false; n     } n   } else { n     if (s <= pos) { n       tos = pos - s; n     } else { n       tos = pos + s; n       up = true; n     } n   } n   pos = s; n   if (up) { n     if (f >= pos) { n       stof = f - pos; n     } else { n       stof = (2 * m - 2) - pos - f; n     } n   } else { n     if (f <= pos) { n       stof = pos - f; n     } else { n       stof = pos + f; n     } n   } n   return tos + stof; n } n int main() { n   int n, m; n   int s, f, t; n   cin >> n >> m; n   for (int i = 0; i < n; i++) { n     cin >> s >> f >> t; n     cout << calc(s, f, t, m) + t << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const double pi = acos(-1.0); n const int INF = 0x3f3f3f3f; n const double eps = 1e-8; n const int MAXN = 1e3 + 10; n const int mod = 1e9 + 7; n const int dir[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; n const int seed = 131; n int cases = 0; n char str[MAXN]; n int mp[MAXN], col[MAXN], cnt[30], vis[MAXN], need[MAXN], ans[MAXN]; n std::vector<int> primes, fac[MAXN], f[MAXN]; n std::priority_queue<std::pair<int, int> > num; n std::vector<std::pair<int, int> > number_of_need; n void init() { n   for (int i = 2; i < MAXN; i++) n     if (!vis[i]) n       for (int j = i; j < MAXN; j += i) { n         fac[j].push_back(i); n         if (j != i) vis[j] = 1; n       } n   for (int i = 2; i < MAXN; i++) n     if (!vis[i]) primes.push_back(i); n } n int find(int n) { return col[n] = n == col[n] ? n : find(col[n]); } n int main() { n   init(); n   scanf( %s , str); n   int len = strlen(str); n   for (int i = 0; i < len; i++) cnt[str[i] -  a ]++; n   for (int i = 0; i < 26; i++) n     if (cnt[i]) num.push({cnt[i], i}); n   for (int i = 2; i <= len; i++) { n     if ((int)fac[i].size() == 1) n       col[i] = i; n     else { n       int v = find(fac[i][0]); n       for (int j = 1; j < (int)fac[i].size(); j++) col[fac[i][j]] = v; n     } n   } n   for (int pri : primes) { n     if (pri > len) break; n     find(pri); n   } n   for (int i = 2; i <= len; i++) f[col[fac[i][0]]].push_back(i); n   for (int i = 2; i <= len; i++) n     if (!f[i].empty()) { n       if (num.empty() || num.top().first < (int)f[i].size()) { n         puts( NO ); n         return 0; n       } n       std::pair<int, int> u = num.top(); n       num.pop(); n       for (int j : f[i]) str[j] = char(u.second +  a ); n       if (u.first - (int)f[i].size() > 0) n         num.push({u.first - (int)f[i].size(), u.second}); n     } n   if (num.empty()) { n     puts( NO ); n     return 0; n   } n   str[1] = char(num.top().second +  a ); n   puts( YES ); n   printf( %s n , str + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std::chrono; n long long mod = 1000000007; n const int MX = 0x3f3f3f3f; n bool G[8][8]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   if (n <= 6) { n     cout << m << endl; n     return 0; n   } n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     G[u][v] = G[v][u] = true; n   } n   int mini = INT_MAX; n   for (int i = 1; i <= 7; i++) { n     for (int j = 1; j <= 7; j++) { n       if (j == i) continue; n       int cur = 0; n       for (int k = 1; k <= 7; k++) cur += G[i][k] & G[j][k]; n       mini = min(mini, cur); n     } n   } n   cout << m - mini <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 10; n int mod = 1e9 + 7; n int a, b, c, d, n, m, x, y, z; n vector<int> v; n int arr[3]; n int main() { n   cin >> a >> b >> c; n   for (int i = 0; i <= a; i++) { n     x = c - i; n     y = b - (a - i); n     if (x >= 0 && y >= 0 && x == y) { n       cout << a - i <<     << x <<     << i << endl; n       return 0; n     } n   } n   cout <<  Impossible  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100001]; n int main() { n   int n, mini, temp; n   cin >> n; n   cin >> mini; n   int minindex = 0, maxindex = 0, flag = 1; n   for (int i = 1; i < n; i++) { n     cin >> temp; n     if (mini == temp) { n       maxindex = i; n       a[flag] = i; n       flag++; n     } n     if (temp < mini) { n       mini = temp; n       minindex = i; n       maxindex = i; n       flag = 0; n       a[flag] = i; n       flag++; n     } n   } n   sort(a, a + flag); n   for (int i = 0; i < flag - 1; i++) a[i] = a[i + 1] - a[i]; n   temp = a[0]; n   for (int i = 1; i < flag - 1; i++) { n     if (temp > a[i]) temp = a[i]; n   } n   cout << temp; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma GCC target( sse4 ) n using namespace std; n template <class T> n bool ckmin(T& a, const T& b) { n   return b < a ? a = b, 1 : 0; n } n template <class T> n bool ckmax(T& a, const T& b) { n   return a < b ? a = b, 1 : 0; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int mod = 1e9 + 7; n const char nl =   n ; n const int N = int(2e5) + 99; n const int INF = int(1e9) + 99; n long long add(long long x, long long y) { n   x += y; n   if (x >= mod) return x - mod; n   return x; n } n long long sub(long long x, long long y) { n   x -= y; n   if (x < 0) return x + mod; n   return x; n } n long long mult(long long x, long long y) { return (x * y) % mod; } n long long bin_pow(long long x, long long p) { n   if (p == 0) return 1; n   if (p & 1) return mult(x, bin_pow(x, p - 1)); n   return bin_pow(mult(x, x), p / 2); n } n long long rev(long long x) { return bin_pow(x, mod - 2); } n long long gcd(long long x, long long y) { return (y == 0 ? x : gcd(y, x % y)); } n long long lcm(long long x, long long y) { return (x / gcd(x, y)) * y; } n string s, t; n int nxt[N][26]; n void solve() { n   int tc; n   cin >> tc; n   while (tc--) { n     cin >> s >> t; n     for (int i = 0; i < ((int)(s).size() + 5); i++) { n       for (int j = 0; j < (26); j++) { n         nxt[i][j] = INF; n       } n     } n     for (int i = (int)(s).size() - 1; i >= 0; --i) { n       for (int j = 0; j < (26); j++) { n         nxt[i][j] = nxt[i + 1][j]; n       } n       nxt[i][s[i] -  a ] = i; n     } n     int res = 1, pos = 0; n     for (int i = 0; i < ((int)(t).size()); i++) { n       if (pos == (int)(s).size()) { n         pos = 0; n         res++; n       } n       if (nxt[pos][t[i] -  a ] == INF) { n         pos = 0; n         ++res; n       } n       if (nxt[pos][t[i] -  a ] == INF && pos == 0) { n         res = INF; n         break; n       } n       pos = nxt[pos][t[i] -  a ] + 1; n     } n     if (res >= INF) { n       cout << -1 << nl; n     } else { n       cout << res << nl; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const int inf = 1e9 + 7; n int tem[N], va[N], ans; n int num[N << 2], mx[N << 2], mi[N << 2]; n void build(int t, int l, int r) { n   mx[t] = 0, mi[t] = inf; n   if (l == r) return; n   int mid = (l + r) >> 1; n   build(t << 1, l, mid); n   build(t << 1 | 1, mid + 1, r); n } n void update(int t, int l, int r, int pos, int d) { n   if (l == r) { n     num[t] = d; n     if (num[t]) n       mx[t] = mi[t] = tem[l]; n     else n       mx[t] = 0, mi[t] = inf; n     return; n   } n   int mid = (l + r) >> 1; n   if (pos <= mid) n     update(t << 1, l, mid, pos, d); n   else n     update(t << 1 | 1, mid + 1, r, pos, d); n   num[t] = num[t << 1] + num[t << 1 | 1]; n   mi[t] = min(mi[t << 1], mi[t << 1 | 1]); n   mx[t] = max(mx[t << 1], mx[t << 1 | 1]); n } n void query_mi(int t, int l, int r, int ei, int ej) { n   if (ei <= l && r <= ej) { n     ans = min(ans, mi[t]); n     return; n   } n   int mid = (l + r) >> 1; n   if (ei <= mid) query_mi(t << 1, l, mid, ei, ej); n   if (ej > mid) query_mi(t << 1 | 1, mid + 1, r, ei, ej); n } n void query_mx(int t, int l, int r, int ei, int ej) { n   if (ei <= l && r <= ej) { n     ans = max(ans, mx[t]); n     return; n   } n   int mid = (l + r) >> 1; n   if (ei <= mid) query_mx(t << 1, l, mid, ei, ej); n   if (ej > mid) query_mx(t << 1 | 1, mid + 1, r, ei, ej); n } n int st[N << 2]; n void modify(int t, int l, int r, int pos, int d) { n   if (l == r) { n     st[t] = d; n     return; n   } n   int mid = (l + r) >> 1; n   if (pos <= mid) n     modify(t << 1, l, mid, pos, d); n   else n     modify(t << 1 | 1, mid + 1, r, pos, d); n   st[t] = max(st[t << 1], st[t << 1 | 1]); n } n bool tp[N]; n int vb[N]; n int main() { n   ios::sync_with_stdio(false); n   int n, m; n   cin >> n >> m; n   int cnt = 0; n   for (int i = 1; i <= n; i++) { n     cin >> va[i]; n     tem[++cnt] = va[i]; n   } n   for (int i = 1; i <= m; i++) { n     cin >> tp[i] >> vb[i]; n     if (tp[i]) tem[++cnt] = vb[i]; n   } n   sort(tem + 1, tem + cnt + 1); n   sort(va + 1, va + n + 1); n   int ni = unique(tem + 1, tem + cnt + 1) - tem - 1; n   build(1, 1, ni); n   for (int i = 1; i <= n; i++) n     va[i] = lower_bound(tem + 1, tem + ni + 1, va[i]) - tem; n   for (int i = 1; i <= n; i++) { n     update(1, 1, ni, va[i], 1); n     if (i != n) modify(1, 1, ni, va[i + 1], tem[va[i + 1]] - tem[va[i]]); n   } n   cout << mx[1] - mi[1] - st[1] << endl; n   for (int i = 1; i <= m; i++) { n     int pos = lower_bound(tem + 1, tem + ni + 1, vb[i]) - tem; n     if (tp[i]) { n       ans = 0; n       query_mx(1, 1, ni, 1, pos); n       int a = ans; n       ans = inf; n       query_mi(1, 1, ni, pos, ni); n       int b = ans; n       update(1, 1, ni, pos, 1); n       if (a) modify(1, 1, ni, pos, vb[i] - a); n       if (b != inf) { n         int c = lower_bound(tem + 1, tem + ni + 1, b) - tem; n         modify(1, 1, ni, c, b - vb[i]); n       } n     } else { n       update(1, 1, ni, pos, 0); n       modify(1, 1, ni, pos, 0); n       ans = 0; n       query_mx(1, 1, ni, 1, pos); n       int a = ans; n       ans = inf; n       query_mi(1, 1, ni, pos, ni); n       int b = ans; n       if (b != inf) { n         int c = lower_bound(tem + 1, tem + ni + 1, b) - tem; n         if (a) n           modify(1, 1, ni, c, b - a); n         else n           modify(1, 1, ni, c, 0); n       } n     } n     cout << max(mx[1] - mi[1] - st[1], 0) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline char gc() { n   static char buf[100000], *p1 = buf, *p2 = buf; n   return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) n              ? EOF n              : *p1++; n } n inline int read() { n   int x = 0; n   char ch = getchar(); n   bool positive = 1; n   for (; !isdigit(ch); ch = getchar()) n     if (ch ==  - ) positive = 0; n   for (; isdigit(ch); ch = getchar()) x = x * 10 + ch -  0 ; n   return positive ? x : -x; n } n inline void write(int a) { n   if (a >= 10) write(a / 10); n   putchar( 0  + a % 10); n } n inline void writeln(int a) { n   if (a < 0) { n     a = -a; n     putchar( - ); n   } n   write(a); n   puts(  ); n } n const int mod = 1000000007, rev = (mod + 1) / 2, N = 20; n int ans, n, bit[1 << N]; n long long a[1 << N], ycl[21]; n char ch[(1 << N) + 5]; n void FWT(long long *a, int n) { n   for (int k = 1; k < n; k <<= 1) { n     for (int i = 0; i < n; i += k << 1) { n       for (register int j = 0; j < k; j++) { n         long long x = a[i + j]; n         a[i + j] = a[i + j] + a[i + j + k]; n         a[i + j + k] = x - a[i + j + k]; n       } n     } n   } n } n int main() { n   n = read(); n   scanf( %s , ch); n   for (int i = 0; i < (1 << n); i++) a[i] = ch[i] -  0 ; n   FWT(a, 1 << n); n   for (int i = 0; i < (1 << n); i++) a[i] = a[i] * a[i] % mod; n   FWT(a, 1 << n); n   ycl[0] = 1; n   for (int i = 1; i <= n; i++) ycl[i] = ycl[i - 1] * rev % mod; n   for (int i = 0; i < (1 << n); i++) { n     bit[i] = bit[i >> 1] + (i & 1); n     ans = (ans + a[i] % mod * ycl[bit[i]]) % mod; n   } n   cout << (long long)(ans + mod) % mod * 3 % mod << endl; n } n


#include <bits/stdc++.h> n using namespace std; n bool isValid(string s) { n   bool valid = true; n   if (s.length() < 1 || s.length() > 16) return false; n   for (int i = 0; i < s.length(); i++) { n     if (s[i] < 48 || (s[i] > 57 && s[i] < 65) || (s[i] > 90 && s[i] < 95) || n         (s[i] > 95 && s[i] < 97) || s[i] > 122) { n       valid = false; n       break; n     } n   } n   return valid; n } n int main() { n   string s; n   cin >> s; n   size_t f1, f2, f3; n   f1 = s.find( @ ); n   if (f1 != string::npos) { n     string t; n     t = s.substr(0, int(f1)); n     if (isValid(t)) { n       bool resource = false; n       f2 = s.find( / ); n       if (f2 != string::npos && int(f2) > int(f1)) { n         resource = true; n         t = s.substr(int(f1) + 1, int(f2) - int(f1) - 1); n       } else n         t = s.substr(int(f1) + 1); n       if (t.length() < 1 || t.length() > 32) { n         cout <<  NO ; n         return 0; n       } n       do { n         f3 = t.find( . ); n         if (f3 != string::npos && int(f3) > 0) { n           string aux = t.substr(0, int(f3)); n           if (isValid(aux)) n             t.erase(0, int(f3) + 1); n           else { n             cout <<  NO ; n             return 0; n           } n         } else { n           if (isValid(t)) { n             if (resource) { n               string aux = s.substr(int(f2) + 1); n               if (isValid(aux)) n                 cout <<  YES ; n               else n                 cout <<  NO ; n               return 0; n             } else { n               cout <<  YES ; n               return 0; n             } n           } else { n             cout <<  NO ; n             return 0; n           } n         } n       } while (f3 != string::npos); n     } else { n       cout <<  NO ; n       return 0; n     } n   } else { n     cout <<  NO ; n     return 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 201000; n int n, a[N], pos[N], l[N], r[N], C[N], idx[N], ans[N]; n vector<int> nu[N]; n void bitModify(int p) { n   for (int i = p; i > 0; i -= i & -i) C[i]++; n } n int bitQuery(int p) { n   int ret = 0; n   for (int i = p; i <= n; i += i & -i) ret += C[i]; n   return ret; n } n bool cmp(int a, int b) { return r[a] < r[b]; } n int main() { n   int nq; n   scanf( %d%d , &n, &nq); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n     pos[a[i]] = i; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; i * j <= n; j++) { n       int a = pos[i]; n       int b = pos[i * j]; n       if (a > b) swap(a, b); n       nu[b].push_back(a); n     } n   } n   for (int i = 0; i < nq; i++) { n     scanf( %d%d , &l[i], &r[i]); n     idx[i] = i; n   } n   sort(idx, idx + nq, cmp); n   int p = 0; n   for (int i = 0; i < nq; i++) { n     int ll = l[idx[i]]; n     int rr = r[idx[i]]; n     while (p < rr) { n       p++; n       for (int i = 0; i < (int)nu[p].size(); i++) { n         bitModify(nu[p][i]); n       } n     } n     ans[idx[i]] = bitQuery(ll); n   } n   for (int i = 0; i < nq; i++) { n     printf( %d%c , ans[i],    n [i == nq - 1]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   iostream::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   long long n, t; n   cin >> n >> t; n   vector<pair<long long, long long> > v(n); n   double den = 0; n   for (long long i = (0); i < (n); ++i) { n     cin >> v[i].second; n     den += v[i].second; n   } n   for (long long i = (0); i < (n); ++i) cin >> v[i].first; n   sort(v.begin(), v.end()); n   double sm = 0; n   for (long long i = (0); i < (n); ++i) { n     sm += v[i].first * v[i].second; n   } n   if (sm / den == (double)t) { n     cout << setprecision(8) << den; n     exit(0); n   } n   if ((double)sm / den > t) { n     for (long long i = n - 1; i >= 0; i--) { n       if ((double)(sm - v[i].first * v[i].second) / (den - v[i].second) > t) { n         sm -= v[i].first * v[i].second; n         den -= v[i].second; n         continue; n       } else { n         cout << setprecision(8) n              << den + (double)(sm - t * den) / (t - v[i].first); n         exit(0); n       } n     } n   } else { n     for (long long i = 0; i < n; i++) { n       if ((double)(sm - v[i].first * v[i].second) / (den - v[i].second) < t) { n         sm -= v[i].first * v[i].second; n         den -= v[i].second; n         continue; n       } else { n         cout << setprecision(8) n              << den + (double)(sm - t * den) / (t - v[i].first); n         exit(0); n       } n     } n   } n   cout <<  0 ; n } n


#include <bits/stdc++.h> n using namespace std; n struct Rectangle { n   int l, d, r, u; n }; n const int maxn = 500000; n int n; n Rectangle rt[maxn]; n int ans[maxn]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 0; i < n; i++) cin >> rt[i].l >> rt[i].d >> rt[i].r >> rt[i].u; n   for (int i = 0; i < n; i++) ans[i] = (rt[i].l & 1) * 2 + (rt[i].d & 1); n   cout <<  YES  << endl; n   for (int i = 0; i < n; i++) cout << ans[i] + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void display(std::vector<int> v) { n   for (int i = 3; i >= 0; i--) { n     for (int j = 0; j < v[i]; j++) { n       if (i == 3) n         cout << 7; n       else if (i == 2) n         cout << 5; n       else if (i == 1) n         cout << 3; n       else if (i == 0) n         cout << 2; n     } n   } n } n int main() { n   int n; n   cin >> n; n   std::vector<int> v = {0, 0, 0, 0}; n   while (n--) { n     char c; n     cin >> c; n     if (c ==  2 ) n       v[0]++; n     else if (c ==  3 ) n       v[1]++; n     else if (c ==  4 ) { n       v[0] += 2; n       v[1]++; n     } else if (c ==  5 ) n       v[2]++; n     else if (c ==  6 ) { n       v[1] += 1; n       v[2]++; n     } else if (c ==  7 ) n       v[3]++; n     else if (c ==  8 ) { n       v[3]++; n       v[0] += 3; n     } else if (c ==  9 ) { n       v[1] += 2; n       v[0]++; n       v[3]++; n     } n   } n   display(v); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> ans; n int main() { n   long long n; n   cin >> n; n   for (long long i = 1; i <= sqrt(n); i++) n     if (n % i == 0) { n       ans.push_back((n + 2 - i) * n / i / 2); n       if (i * i != n) ans.push_back((i * n + 2 * i - n) / 2); n     } n   sort(ans.begin(), ans.end()); n   for (unsigned int i = 0; i < ans.size(); i++) printf( %lld  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int dx[] = {0, 0, -1, 1, -1, -1, 1, 1}; n long long int dy[] = {1, -1, 0, 0, 1, -1, -1, 1}; n using namespace std; n int main() { n   long long int n, p; n   cin >> n >> p; n   vector<bool> box(n, 0); n   vector<pair<long long int, long long int>> ppl(p); n   for (auto& i : ppl) cin >> i.first >> i.second; n   bool res = 1; n   for (int i = 0; i < n; ++i) { n     cout << res; n     res = !res; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m; n   cin >> n >> m; n   vector<vector<int>> grid(n, vector<int>(m, 0)); n   vector<vector<int>> up(n, vector<int>(m, 0)); n   vector<vector<int>> down(n, vector<int>(m, 0)); n   vector<vector<int>> left(n, vector<int>(m, 0)); n   vector<vector<int>> right(n, vector<int>(m, 0)); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> grid[i][j]; n     } n   } n   for (int i = 0; i < n; i++) { n     left[i][0] = grid[i][0]; n     for (int j = 1; j < m; j++) { n       left[i][j] = left[i][j - 1] + grid[i][j]; n     } n   } n   for (int i = 0; i < n; i++) { n     right[i][m - 1] = grid[i][m - 1]; n     for (int j = m - 2; j >= 0; j--) { n       right[i][j] = right[i][j + 1] + grid[i][j]; n     } n   } n   for (int j = 0; j < m; j++) { n     up[0][j] = grid[0][j]; n     for (int i = 1; i < n; i++) { n       up[i][j] = up[i - 1][j] + grid[i][j]; n     } n   } n   for (int j = 0; j < m; j++) { n     down[n - 1][j] = grid[n - 1][j]; n     for (int i = n - 2; i >= 0; i--) { n       down[i][j] = down[i + 1][j] + grid[i][j]; n     } n   } n   int ans = 0; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       if (grid[i][j] == 1) continue; n       if (up[i][j] - grid[i][j] > 0) ans++; n       if (down[i][j] - grid[i][j] > 0) ans++; n       if (left[i][j] - grid[i][j] > 0) ans++; n       if (right[i][j] - grid[i][j] > 0) ans++; n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   set<pair<int, int> > st; n   for (int i = 0; i < n; ++i) { n     int x; n     scanf( %d , &x); n     st.insert({x, i}); n   } n   int x = -1; n   vector<int> ans; n   while (x >= -1) { n     auto it = st.lower_bound({x + 1, -1}); n     if (it != st.end() and it->first == x + 1) { n       ans.emplace_back(it->second); n       st.erase(it); n       x++; n     } else { n       x -= 3; n     } n   } n   if (st.empty()) { n     cout <<  Possible n ; n     for (const int &x : ans) printf( %d  , x + 1); n   } else { n     cout <<  Impossible n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-06; n const int MAXN = 25; n pair<int, int> r, r2, bk, wk; n int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}; n int dy[] = {0, 0, 1, -1, -1, 1, 1, -1}; n char board[10][10]; n bool valid(int x, int y) { return x >= 0 && y >= 0 && x < 8 && y < 8; } n bool ok(int x, int y) { n   for (int i = 0; i < 8; i++) { n     for (int j = 0; j < 8; j++) { n       if (board[i][j] ==  R ) { n         for (int k = 0; k < 4; k++) { n           int nx = i, ny = j; n           do { n             nx = nx + dx[k], ny = ny + dy[k]; n           } while (valid(nx, ny) && board[nx][ny] ==  . ); n           if (valid(nx, ny) && board[nx][ny] ==  B ) return false; n         } n       } n       if (board[i][j] ==  W ) { n         for (int k = 0; k < 8; k++) { n           int nx = i + dx[k], ny = j + dy[k]; n           if (valid(nx, ny) && board[nx][ny] ==  B ) return false; n         } n       } n     } n   } n   return true; n } n bool check(int x, int y) { n   if (ok(x, y)) return false; n   bool mate = true; n   for (int i = 0; i < 8; i++) { n     int nx = x + dx[i], ny = y + dy[i]; n     if (!valid(nx, ny)) continue; n     char tmp = board[nx][ny]; n     board[nx][ny] =  B ; n     board[x][y] =  . ; n     if (ok(nx, ny)) mate = false; n     board[nx][ny] = tmp; n     board[x][y] =  B ; n   } n   return mate; n } n pair<int, int> get(string a) { return pair<int, int>( 8  - a[1], a[0] -  a ); } n int main() { n   for (int i = 0; i < 8; i++) n     for (int j = 0; j < 8; j++) board[i][j] =  . ; n   string tmp; n   cin >> tmp, board[get(tmp).first][get(tmp).second] =  R ; n   cin >> tmp, board[get(tmp).first][get(tmp).second] =  R ; n   cin >> tmp, board[get(tmp).first][get(tmp).second] =  W ; n   cin >> tmp, board[get(tmp).first][get(tmp).second] =  B ; n   bool ret = false; n   for (int i = 0; i < 8; i++) { n     for (int j = 0; j < 8; j++) { n       if (board[i][j] ==  B ) ret = check(i, j); n     } n   } n   if (ret) n     cout <<  CHECKMATE  << endl; n   else n     cout <<  OTHER  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool a[300001]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int i, n, tmp, ans = 1, last; n   cin >> n; n   last = n; n   cout << 1 <<    ; n   for (i = 0; i < n; i++) { n     cin >> tmp; n     a[tmp] = 1; n     ans++; n     while (a[last]) { n       last--, ans--; n     } n     cout << ans <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 20; n int c[MAX_N], d[MAX_N], num[MAX_N]; n int dp[MAX_N][MAX_N * 100]; n int n, m, c0, d0, a, b; n int main() { n   while (scanf( %d%d%d%d , &n, &m, &c0, &d0) != EOF) { n     for (int i = 1; i <= m; i++) { n       scanf( %d%d , &a, &b); n       num[i] = a / b; n       scanf( %d%d , &c[i], &d[i]); n     } n     memset(dp, 0, sizeof dp); n     for (int i = 0; i <= n; i++) dp[0][i] = i / c0 * d0; n     for (int i = 1; i <= m; i++) n       for (int j = 0; j <= n; j++) n         for (int k = 0; k <= num[i]; k++) { n           if (j / c[i] < k) break; n           dp[i][j] = max(dp[i][j], dp[i - 1][j - c[i] * k] + d[i] * k); n         } n     int res = 0; n     for (int i = 0; i <= n; i++) res = max(res, dp[m][i]); n     printf( %d n , res); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int hops[n + 1]; n   bool vis[n + 1]; n   int change[n + 1]; n   for (int i = 1; i <= n; ++i) { n     cin >> hops[i]; n     vis[i] = 0; n   } n   int cnt = 0; n   int cnt1 = 0; n   for (int i = 1; i <= n; ++i) { n     cin >> change[i]; n     if (change[i]) cnt1++; n   } n   for (int i = 1; i <= n; ++i) { n     if (!vis[i]) { n       int j = i; n       cnt++; n       while (vis[hops[j]] == false) { n         j = hops[j]; n         vis[j] = true; n       } n     } n   } n   if (cnt1 % 2) n     cnt1 = 0; n   else n     cnt1 = 1; n   if (cnt == 1) cnt = 0; n   cout << cnt + cnt1 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int N, K; n long long A[100100], sol; n map<long long, long long> M; n long long __grundy(long long n) { n   if (n == 0) return 0; n   if (((M).find(n) != (M).end())) return M[n]; n   long long r = __grundy(n - 1); n   if (n % 2 != 0) return M[n] = ((r == 0) ? 1 : 0); n   long long c = __grundy(n / 2); n   if (r != 0 && K > 0 && c != 0) return M[n] = 0; n   if (r != 1 && (K == 0 || c != 1)) return M[n] = 1; n   return M[n] = 2; n } n long long g0(long long n) { n   if (n == 0) return 0; n   if (n == 1) return 1; n   if (n == 2) return 2; n   return ((n % 2) == 0) ? 1 : 0; n } n long long g1(long long n) { n   if (n == 0) return 0; n   if (n == 1) return 1; n   if (n == 2) return 0; n   if (n == 3) return 1; n   if (n == 4) return 2; n   if ((n % 2) == 1) return 0; n   if (g1(n / 2) == 1) return 2; n   return 1; n } n long long grundy(long long n) { n   if (K) n     return g1(n); n   else n     return g0(n); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   K = 0; n   for (int I = 1; I <= 160; I++) { n     assert(grundy(I) == __grundy(I)); n   } n   M.clear(); n   K = 1; n   for (int I = 1; I <= 160; I++) { n     assert(grundy(I) == __grundy(I)); n   } n   cin >> N >> K; n   K %= 2; n   for (long long I = 0; I < N; I++) { n     cin >> A[I]; n     sol ^= grundy(A[I]); n   } n   if (sol == 0) n     cout <<  Nicky  << endl; n   else n     cout <<  Kevin  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<long long int> s(n); n   for (int i = 0; i < n; ++i) cin >> s[i]; n   int q; n   cin >> q; n   sort(s.begin(), s.end()); n   int slope0 = 1; n   for (int i = 1; i < n; ++i) n     if (s[i] != s[i - 1]) ++slope0; n   map<long long int, int> freq; n   for (int i = 1; i < n; ++i) { n     long long int delta = s[i] - s[i - 1]; n     if (delta == 0) continue; n     delta--; n     if (freq.find(delta) == freq.end()) n       freq[delta] = 1; n     else n       freq[delta] = freq[delta] + 1; n   } n   map<long long int, int> slope; n   map<long long int, long long int> obj; n   long long int prev = slope0; n   for (map<long long int, int>::iterator it = freq.begin(); it != freq.end(); n        ++it) { n     slope[it->first] = prev - it->second; n     prev = prev - it->second; n   } n   if (slope.find(0) == slope.end()) slope[0] = slope0; n   for (map<long long int, int>::iterator it = slope.begin(); n        it != slope.end();) { n     if (it->first == 0) obj[0] = slope0; n     long long int y0 = obj[it->first]; n     long long int change = it->second; n     long long int base = it->first; n     ++it; n     if (it != slope.end()) obj[it->first] = y0 + change * (it->first - base); n   } n   for (int i = 0; i < q; ++i) { n     long long int l, r; n     cin >> l >> r; n     map<long long int, long long int>::iterator it = obj.lower_bound(r - l); n     if (it->first == (r - l)) n       cout << it->second <<    ; n     else { n       it--; n       long long int ans = n           obj[it->first] + (r - l - it->first) * slope[it->first]; n       cout << ans <<    ; n     } n   } n   cout <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const long long linf = 0x3f3f3f3f3f3f3f3f; n const int maxn = 2e5 + 11; n const int maxm = 600 + 11; n const int mod = 1e9 + 7; n const double eps = 1e-5; n inline long long rd() { n   long long x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n inline long long qpow(long long a, long long b, long long p) { n   long long res = 1; n   while (b) { n     if (b & 1) { n       res *= a; n       res %= p; n     } n     b >>= 1; n     a = a * a % p; n   } n   return res; n } n inline long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n struct node { n   long long a, b, c; n   bool operator<(const node &o) const { n     if (c != o.c) return c > o.c; n     if (b != o.b) return b < o.b; n     return a > o.a; n   } n } t[maxn]; n int n, k; n bool check(long long x) { n   priority_queue<node> q; n   for (int i = 1; i <= n; i++) { n     q.push(t[i]); n   } n   for (int i = 0; i < k; i++) { n     node o = q.top(); n     q.pop(); n     if (o.c < i) return false; n     if (o.c >= k) return true; n     o.a += x; n     o.c = o.a / o.b; n     q.push(o); n   } n   return true; n } n int main() { n   n = rd(), k = rd(); n   for (int i = 1; i <= n; i++) { n     t[i].a = rd(); n   } n   for (int i = 1; i <= n; i++) { n     t[i].b = rd(); n     t[i].c = t[i].a / t[i].b; n   } n   long long l = 0, r = 5e12, ans = -1; n   while (l <= r) { n     long long mid = l + r >> 1; n     if (check(mid)) n       ans = mid, r = mid - 1; n     else n       l = mid + 1; n   } n   printf( %lld n , ans); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   double R, r, k; n   int tt; n   scanf( %d , &tt); n   while (tt--) { n     scanf( %lf%lf%lf , &R, &r, &k); n     double a = (R + r) / (4 * R * r), b = k * (R - r) / (2 * R * r); n     double t = (R - r) / (4 * R * r); n     double s = t / (a * a + b * b - t * t); n     printf( %.9lf n , s); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200007; n string s; n int fa[maxn << 1]; n map<string, int> aa; n int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n string read() { n   cin >> s; n   return s; n } n int main() { n   int n, m, q; n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 1; i <= 2 * n; i++) fa[i] = i; n   for (int i = 1; i <= n; i++) aa[read()] = i; n   for (int i = 1; i <= m; i++) { n     int t; n     scanf( %d , &t); n     int x = aa[read()], y = aa[read()]; n     if (t == 1) { n       if (find(x) != find(y + n) && find(x + n) != find(y)) n         fa[find(x)] = find(y), fa[find(x + n)] = find(y + n), printf( YES n ); n       else n         printf( NO n ); n     } else { n       if (find(x) != find(y) && find(x + n) != find(y + n)) n         fa[find(x)] = find(y + n), fa[find(x + n)] = find(y), printf( YES n ); n       else n         printf( NO n ); n     } n   } n   for (int i = 1; i <= q; i++) { n     int x = aa[read()], y = aa[read()]; n     if (find(x) == find(y)) n       printf( 1 n ); n     else if (find(x) == find(y + n)) n       printf( 2 n ); n     else n       printf( 3 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<string, string> p; n map<string, string>::iterator it = p.begin(); n int main() { n   int q; n   cin >> q; n   while (q--) { n     string s1, s2; n     cin >> s1 >> s2; n     if (!p.count(s1)) n       p[s2] = s1; n     else { n       p[s2] = p[s1]; n       p.erase(s1); n     } n   } n   cout << p.size() << endl; n   for (it = p.begin(); it != p.end(); it++) n     cout << it->second <<     << it->first << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5; n const long long INF = 1e18; n inline int abs(int x) { return x > 0 ? x : -x; } n inline long long sqr(long long x) { return x * x; } n bool cmp(pair<int, int>& x, pair<int, int>& y) { return x.first < y.first; } n int n; n pair<int, int> p[N]; n long long minY[N + 1][2], maxY[N + 1][2]; n bool check(long long d) { n   long long dY, sqrX, sqrY; n   int l = 0, r = 0; n   while (r < n) { n     while (r < n && sqr((long long)p[r].first - p[l].first) <= d) { n       sqrX = max(sqr((long long)p[r].first), sqr((long long)p[l].first)); n       sqrY = max(max(sqr(maxY[l][0]), sqr(maxY[n - r - 1][1])), n                  max(sqr(minY[l][0]), sqr(minY[n - r - 1][1]))); n       dY = sqr(max(maxY[l][0], maxY[n - r - 1][1]) - n                min(minY[n - r - 1][1], minY[l][0])); n       if (max(dY, sqrX + sqrY) <= d) { n         if (d < 400) { n         } n         return true; n       } n       ++r; n     } n     while (r < n && l <= r && sqr((long long)p[r].first - p[l].first) > d) ++l; n   } n   return false; n } n long long solve() { n   sort(p, p + n, cmp); n   maxY[0][0] = maxY[0][1] = -INF; n   minY[0][0] = minY[0][1] = INF; n   for (int i = 1; i <= n; ++i) { n     maxY[i][0] = max(maxY[i - 1][0], (long long)p[i - 1].second); n     maxY[i][1] = max(maxY[i - 1][1], (long long)p[n - i].second); n     minY[i][0] = min(minY[i - 1][0], (long long)p[i - 1].second); n     minY[i][1] = min(minY[i - 1][1], (long long)p[n - i].second); n   } n   long long l = 0, r = INF, m; n   while (l < r) { n     m = (l + r) / 2; n     if (check(m)) n       r = m; n     else n       l = m + 1; n   } n   return min(l, sqr((long long)p[n - 1].first - p[0].first)); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; ++i) scanf( %d%d , &p[i].first, &p[i].second); n   long long ans = solve(); n   for (int i = 0; i < n; ++i) swap(p[i].first, p[i].second); n   ans = min(ans, solve()); n   printf( %I64d , ans); n } n