#include <bits/stdc++.h> n using namespace std; n char c[1000]; n int i, n, d, t, q; n int main() { n   cin >> c; n   n = strlen(c); n   i = 0; n   while (i < n && c[i] !=  @ ) { n     if (!isalnum(c[i]) && c[i] !=  _ ) { n       cout <<  NO ; n       return 0; n     } n     i++; n   } n   i++; n   t = i; n   if (i == n || i > 16 || i < 1) { n     cout <<  NO ; n     return 0; n   } n   while (i < n && c[i] !=  / ) { n     d = 0; n     q = i; n     while (i < n && c[i] !=  .  && c[i] !=  / ) { n       if (!isalnum(c[i]) && c[i] !=  _ ) { n         cout <<  NO ; n         return 0; n       } n       i++; n       d = 1; n     } n     if (i - q > 16 || i - q < 1) { n       cout <<  NO ; n       return 0; n     } n     if (c[i] !=  / ) i++; n   } n   if (i - t > 32 || i - t < 1 || (i == n && c[i - 1] ==  . )) { n     cout <<  NO ; n     return 0; n   } n   if (c[i] ==  / ) { n     if (i == n) { n       cout <<  NO ; n       return 0; n     } n     t = i + 1; n     i++; n     while (i < n) { n       if (!isalnum(c[i]) && c[i] !=  _ ) { n         cout <<  NO ; n         return 0; n       } n       i++; n     } n     if (i - t > 16 || i - t < 1) { n       cout <<  NO ; n       return 0; n     } n   } n   cout <<  YES ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-8; n const int dir[8][2] = {{0, 1}, {1, 0},  {0, -1},  {-1, 0}, n                        {1, 1}, {1, -1}, {-1, -1}, {-1, 1}}; n const int mod = 1e9 + 7, gakki = 5 + 2 + 1 + 19880611 + 1e9; n const int MAXN = 1e6 + 5, MAXM = 1e6 + 5; n const int MAXQ = 100010, INF = 1e9; n inline void read(int &v) { n   v = 0; n   char c = 0; n   int p = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) { n       p = -1; n     } n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     v = (v << 3) + (v << 1) + c -  0 ; n     c = getchar(); n   } n   v *= p; n } n int n, m; n int len; n int anser = INT_MAX; n int num[55]; n void dfs(int x, int where, int sum) { n   for (int i = x + 1; i <= len; i++) { n     if (num[i] >= num[x] + 2) { n       if (where == m - 1) { n         anser = min(anser, sum + num[i]); n         return; n       } n       dfs(i, where + 1, sum + num[i]); n       break; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   string ch; n   cin >> ch; n   len = ch.size(); n   for (int i = 0; i < len; i++) { n     num[i + 1] = (ch[i] -  a ) + 1; n   } n   sort(num + 1, num + 1 + len); n   num[len + 1] = INT_MAX; n   if (m == 1) { n     cout << num[1] << endl; n     return 0; n   } n   for (int i = 1; i <= len - m + 1; i++) { n     dfs(i, 1, num[i]); n   } n   if (anser == INT_MAX) { n     cout << -1 << endl; n   } else { n     cout << anser << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int MAXN = 100005; n int n, q, A[MAXN], B[MAXN]; n double ans, f[MAXN][105]; n int main() { n   scanf( %d , &n); n   for (register int i = 1; i <= n; ++i) n     scanf( %d , A + i), f[i][B[i] = A[i]] = 1, ans += f[i][0]; n   scanf( %d , &q); n   register int u, v, k; n   while (q--) { n     scanf( %d%d%d , &u, &v, &k); n     ans -= f[u][0]; n     for (register int i = 1; i <= k; ++i) n       for (register int j = 0; j <= A[u]; ++j) n         f[u][j] = f[u][j] * (B[u] - i + 1 - j) / (B[u] - i + 1) + n                   f[u][j + 1] * (j + 1) / (B[u] - i + 1); n     ans += f[u][0]; n     B[u] -= k; n     B[v] += k; n     printf( %.12lf n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int l = 2, h = 1e5; n     int ans = 2; n     while (l <= h) { n       int m = l + (h - l) / 2; n       if ((long long)m * (m - 1) / 2 <= n) { n         ans = m; n         l = m + 1; n       } else { n         h = m - 1; n       } n     } n     int rem = n - (long long)ans * (ans - 1) / 2; n     cout <<  133 ; n     for (int i = 1; i <= rem; i++) cout << 7; n     for (int i = 1; i <= ans - 2; i++) cout << 3; n     cout << 7 <<   n ; n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-10; n const double PI = 3.1415926535897932; n const int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1}; n const int maxn = 2e5 + 5; n const int MOD = 998244353; n int n, m; n int d[maxn]; n long long sum[maxn]; n long long qpow(long long a, long long p) { n   long long ans = 1; n   while (p) { n     if (p & 1) ans = ans * a % MOD; n     a = a * a % MOD; n     p >>= 1; n   } n   return ans; n } n long long inv(long long a) { return qpow(a, MOD - 2); } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &d[i]); n   sort(d + 1, d + 1 + n); n   for (int i = 1; i <= n; i++) sum[i] = (sum[i - 1] + d[i]) % MOD; n   while (m--) { n     long long A, B; n     scanf( %lld%lld , &A, &B); n     int st = lower_bound(d + 1, d + 1 + n, B) - d; n     if (n - st + 1 < A) { n       printf( 0 n ); n       continue; n     } n     long long k = n - st + 1; n     long long kk = (k - A) * inv(k) % MOD; n     long long ans = (sum[n] - sum[st - 1]) % MOD * kk % MOD; n     kk = (k - A + 1) * inv(k + 1) % MOD; n     ans = (ans + sum[st - 1] * kk % MOD) % MOD; n     ans = (ans + MOD) % MOD; n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MX = 1000; n int dp[MX][MX], a[MX], x[MX], y[MX], n, d; n int dis(int i, int j) { return abs(x[i] - x[j]) + abs(y[i] - y[j]); } n int main() { n   cin >> n >> d; n   for (int i = 2; i < n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) cin >> x[i] >> y[i]; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) { n       if (i != j) dp[i][j] = dis(i, j) * d - a[i]; n     } n   for (int k = 1; k <= n; k++) n     for (int i = 1; i <= n; i++) n       for (int j = 1; j <= n; j++) n         if (dp[i][j] > dp[i][k] + dp[k][j]) dp[i][j] = dp[i][k] + dp[k][j]; n   cout << dp[1][n] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> g[100005]; n int n, m; n vector<int> c[100005]; n int nc; n bool b[100005]; n int v[100005]; n vector<pair<int, int> > e; n int s0[100005], s1[100005]; n int w[100005]; n void add(int x, int y) { n   e.push_back(make_pair(x, y)); n   if (((int)e.size()) == m) { n     for (auto p : e) printf( %d %d n , p.first + 1, p.second + 1); n     exit(0); n   } n } n void add2(int i, int j) { n   add(i, j); n   if (s0[j] == s0[i]) n     s0[s0[i]] = -1, s0[i] = s0[j] = s1[i] = s1[j] = -1; n   else { n     s1[s0[i]] = s1[s0[j]]; n     s0[s0[j]] = s0[s0[i]]; n   } n   n--; n } n void dfs(int x) { n   if (v[x] >= 0) return; n   v[x] = nc; n   c[nc].push_back(x); n   for (int y : g[x]) dfs(y); n } n priority_queue<pair<int, int> > q; n int main() { n   scanf( %d%d , &n, &m); n   for (int _ = 0, ThxDem = m; _ < ThxDem; ++_) { n     int x, y; n     scanf( %d%d , &x, &y); n     x--; n     y--; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   memset(v, -1, sizeof(v)); n   for (int i = 0, ThxDem = n; i < ThxDem; ++i) n     if (!((int)g[i].size())) b[nc] = true, v[i] = nc, c[nc++] = {i}; n   for (int i = 0, ThxDem = n; i < ThxDem; ++i) n     if (((int)g[i].size()) == 1 && v[i] < 0) dfs(i), b[nc++] = false; n   for (int i = 0, ThxDem = n; i < ThxDem; ++i) n     if (v[i] < 0) dfs(i), b[nc++] = true; n   if (nc == 1) { n     if (n >= 5) n       for (int i = 0, ThxDem = n; i < ThxDem; ++i) n         add(c[0][i], c[0][(i + 2) % n]); n     else if (n == 4 && !b[0]) { n       add(c[0][1], c[0][3]); n       add(c[0][3], c[0][0]); n       add(c[0][0], c[0][2]); n       assert(0); n     } n   } else { n     for (int i = 0, ThxDem = n; i < ThxDem; ++i) s0[i] = s1[i] = i; n     pair<int, int> t = make_pair(-1, -1); n     for (int i = 0, ThxDem = nc; i < ThxDem; ++i) n       t = max(t, make_pair(((int)c[i].size()), i)); n     int k = t.second; n     if (((int)c[k].size()) > n - ((int)c[k].size())) { n       if (((int)c[k].size()) == 2) { n         add(c[k][0], c[k ^ 1][0]); n         assert(0); n       } n       if (((int)c[k].size()) == 3) { n         if (!b[k]) add2(c[k][0], c[k][2]); n       } else if (((int)c[k].size()) == 4 && b[k]) { n         add2(c[k][0], c[k][2]); n         if (((int)c[k].size()) - 1 > n - (((int)c[k].size()) - 1)) n           add2(c[k][1], c[k][3]); n       } else if (((int)c[k].size()) == 4) { n         add2(c[k][1], c[k][3]); n         if (((int)c[k].size()) - 1 > n - (((int)c[k].size()) - 1)) { n           add2(c[k][3], c[k][0]); n           if (((int)c[k].size()) - 2 > n - (((int)c[k].size()) - 2)) n             add2(c[k][0], c[k][2]); n         } n       } else { n         for (int i = 0, ThxDem = ((int)c[k].size()); i < ThxDem; ++i) { n           add2(c[k][i], c[k][(i + 2) % ((int)c[k].size())]); n           if (((int)c[k].size()) - i - 1 <= n - (((int)c[k].size()) - i - 1)) n             break; n         } n       } n       vector<int> t; n       for (int x : c[k]) n         if (s0[x] == x) t.push_back(x); n       c[k] = t; n     } n     int l = -1; n     for (int i = 0, ThxDem = nc; i < ThxDem; ++i) n       q.push(make_pair(((int)c[i].size()), i == k ? i : -i)); n     while (!q.empty()) { n       auto p = q.top(); n       q.pop(); n       int i = abs(p.second); n       if (l >= 0 && v[l] == i) { n         if (q.empty()) break; n         auto qwe = p; n         p = q.top(); n         q.pop(); n         q.push(qwe); n         i = abs(p.second); n       } n       if (l >= 0) add(s1[l], s0[c[i][w[i]]]); n       l = c[i][w[i]++]; n       if (p.first > 1) q.push(make_pair(p.first - 1, i == k ? i : -i)); n     } n     if (v[l] != k) { n       add(s1[l], s0[c[k][0]]); n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n int main() { n   cout << fixed << setprecision(12); n   int n, k; n   cin >> n >> k; n   vector<vector<int>> y(n, vector<int>(k + 1)); n   for (auto &yi : y) n     for (auto &yij : yi) cin >> yij; n   vector<double> sum(n); n   for (int j = 0; j < k; j++) { n     vector<tuple<int, int, int>> v; n     for (int i = 0; i < n; i++) { n       v.emplace_back(y[i][j + 1] - y[i][j], y[i][j], i); n     } n     sort(begin(v), end(v)); n     auto calc = [&](int k) { n       vector<double> x(n + 1), y(n + 1); n       x[0] = 0.0, y[0] = 0.0; n       x[1] = 1.0, y[1] = 0.0; n       int sz = 2; n       for (auto &t : v) n         if (get<2>(t) < k) { n           auto a = get<0>(t), b = get<1>(t); n           auto f = [&](double x) { return a * x + b; }; n           int p = sz - 1; n           while (p >= 0 && f(x[p]) + eps >= y[p]) p--; n           if (p == -1) { n             y[0] = f(x[0] = 0.0); n             y[1] = f(x[1] = 1.0); n             sz = 2; n           } else if (p != sz - 1) { n             double a2 = (y[p + 1] - y[p]) / (x[p + 1] - x[p]); n             double b2 = y[p] - x[p] * a2; n             double x2 = (b2 - b) / (a - a2); n             y[p + 1] = f(x[p + 1] = x2); n             y[p + 2] = f(x[p + 2] = 1.0); n             sz = p + 3; n           } n         } n       double sum = 0.0; n       for (int i = 0; i + 1 < sz; i++) { n         sum += (x[i + 1] - x[i]) * (y[i + 1] + y[i]) / 2; n       } n       return sum; n     }; n     vector<double> acc(n + 1); n     for (int i = 0; i < n; i++) acc[i + 1] = calc(i + 1); n     for (int i = 0; i < n; i++) sum[i] += acc[i + 1] - acc[i]; n   } n   for (int i = 0; i < n; i++) cout << sum[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200010; n int a[maxn]; n int f[maxn]; n int miss[maxn]; n int sum[maxn]; n int main() { n   int n, s; n   cin >> n >> s; n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   int ans1 = 0; n   if (a[s] != 0) { n     ans1++; n     a[s] = 0; n   } n   int zero = 0; n   for (int i = 1; i <= n; i++) { n     f[a[i]]++; n     if (a[i] == 0) { n       zero++; n     } n   } n   zero--; n   int i = 0; n   while (zero--) { n     while (f[i] != 0) { n       i++; n     } n     f[0]--; n     f[i]++; n     ans1++; n   } n   sum[0] = 1; n   for (int i = 1; i < n; i++) { n     if (f[i] == 0) { n       miss[i] = miss[i - 1] + 1; n     } else { n       miss[i] = miss[i - 1]; n     } n     sum[i] = sum[i - 1] + f[i]; n   } n   int ans2 = 1e9; n   for (int i = n - 1; i >= 1; i--) { n     int tmp = max((n - sum[i]), miss[i]); n     ans2 = min(ans2, tmp); n   } n   if (n == 1) { n     ans2 = 0; n   } n   cout << ans1 + ans2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long double PI = acos(-1); n long double DEL = 1e-10; n int M =  1e9 + 7; n const int N = 3e5 + 10; n  n #define ftt cin>>tc;for(int cas=1;cas<=tc;++cas) n #define all(a) a.begin(),a.end() n #define vpii vector<pair<int,int> > n #define vvi vector<vector<int> > n #define va(x) vector<array<int,(x)> > n #define pii pair<int,int> n #define vi vector<int> n #define sq(a) ((a)*(a)) n #define double long double n #define dbg cout<<  nhi n ; n #define int long long n #define nl cout<<  n  n #define sp <<   << n #define S second n #define F first n  n template<class T> using minpq = priority_queue<T,vector<T>,greater<T> >; n struct DSU n { n         vi par,size; n         DSU (int n) n         { n                 par.resize(n+1); n                 size.resize(n+1); n                 for(int i=0;i<=n;i++) n                 { n                         par[i] = i; n                         size[i] = 1; n                 } n         } n          n         int find(int x) n         { n                 return par[x] = (x == par[x]? par[x]:find(par[x])); n                  n         } n          n         void join(int x,int y) n         { n                 int parx = find(x),pary = find(y); n                 if(parx == pary) n                 { n                         return; n                 } n                 if(size[parx] > size[pary]) n                 { n                         size[parx] += size[pary]; n                         par[pary] = parx; n                 } n                 else n                 { n                         size[pary] += size[parx]; n                         par[parx] = pary; n                 } n         } n }; n int fpow(int x, int n) n { n         int res = 1; n         x %= M; n         while(n) n         { n                 if(n&1)res = res * x % M; n                 x = x * x % M; n                 n>>=1; n         } n         return res; n } n int gcd(int a,int b) n { n         if(b == 0)return a; n         return gcd(b,a % b); n } n int lcm(int a,int b) n { n         return a*(b/gcd(a,b)); n } n int popcount(int x) n { n         return bitset<65>(x).count(); n } n vector<int> fac(N),inv(N); n  n void create_fac(int n) n { n         fac[0] = inv[0] = 1; n         for(int i=1;i<=n;i++) n         { n                 fac[i] = fac[i-1]*i % M; n                 inv[i] = fpow(fac[i],M-2); n         } n } n int ncr(int n,int r) n { n         if(r > n)return 0; n         return fac[n] * (inv[r] * inv[n-r] % M) % M; n         return fac[n] * (fpow(fac[r],M-2) * fpow(fac[n-r],M-2) % M ) % M; n } n  n void sieve (int n) n { n         vector<int> pf(N); n         pf[0] = pf[1] = 1; n         for (int i = 2; i <= n; i++) { n                 if (!pf[i]) { n                         pf[i] = i; n                         for (int j = i * i; j <= n; j += i){ n                                 pf[j] = i; n                         } n                 } n         } n          n } n  n //**************************************** CHECK CONSTRAINTS *************************************** n int cnt, sum, mid, mx, mn, ans, a[N], b[N]; n int n, m, d, i, j, k, l, p, q, r, t, w, x, y, z, tc; n string s; n  n vvi dp(402,vi(402)); n  n  n int32_t main() n { n         ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); n         #ifndef ONLINE_JUDGE n         freopen( /Users/jenish/XCode/cp/input.txt , r ,stdin); n         #endif n         cin >> n >> M; n          n         // dp[i][j] => done till i and had j gaps. n          n          n         for(int i = 1;i <= n;i++){ n                 dp[i][0] = fpow(2,i-1); n         } n         create_fac(n+2); n         for(int i = 2;i <= n;i++){ n                 for(int gaps = 1;gaps <= i - 2;gaps++){ n                         //lgp = last gap position n                         for(int lgp = 2;lgp < i;lgp++){ n                                 //cout << i sp gaps sp lgp;nl; n                                 //cout << dp[lgp - 1][gaps - 1] sp fpow(2,i - lgp - 1) sp ncr(i - gaps,i - lgp);nl; n                                 dp[i][gaps] += (dp[lgp - 1][gaps - 1] * fpow(2,i - lgp - 1) % M) * ncr(i - gaps,i - lgp) % M; n                                 dp[i][gaps] %= M; n                         } n                 } n         } n          n         for(int gaps = 0;gaps < n - 1;gaps++){ n                 //cout<<gaps sp dp[n][gaps];nl; n                 ans += dp[n][gaps]; n                 ans %= M; n         } n         cout << ans; n } n  n  n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const string nl =   n ; n vector<int> readVector(int n) { n   vector<int> ret(n); n   for (int i = 0; i < n; i++) cin >> ret[i]; n   return ret; n } n void setIO(string name) { n   freopen((name +  .in ).c_str(),  r , stdin); n   freopen((name +  .out ).c_str(),  w , stdout); n } n int recur(vector<int> vec, int sig) { n   vector<int> s1, s0; n   for (int i = 0; i < vec.size(); i++) { n     if ((vec[i] & (1 << sig)) == 0) { n       s0.push_back(vec[i]); n     } else n       s1.push_back(vec[i]); n   } n   if (s1.size() < 2 && s0.size() < 2) return 0; n   if (s1.size() < 2) return recur(s0, sig - 1); n   if (s0.size() < 2) return recur(s1, sig - 1); n   return min(s0.size() - 1 + recur(s1, sig - 1), n              s1.size() - 1 + recur(s0, sig - 1)); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int n; n   cin >> n; n   vector<int> a = readVector(n); n   cout << recur(a, 30) << nl; n   return 0; n } n


#include <bits/stdc++.h> n int n, m; n char map[1010][1010], ans[1010][1010]; n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %s , map[i] + 1); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) ans[i][j] =  . ; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (map[i][j] !=  b ) continue; n       int c =  a  + 2 * ((i / 2 + j / 2) % 2) + i % 2; n       if (map[i - 1][j] ==  w  && map[i + 1][j] ==  w ) n         ans[i - 1][j] = ans[i][j] = ans[i + 1][j] = c, n                 map[i - 1][j] = map[i][j] = map[i + 1][j] =  . ; n       else if (map[i][j - 1] ==  w  && map[i][j + 1] ==  w ) n         ans[i][j - 1] = ans[i][j] = ans[i][j + 1] = c, n                    map[i][j - 1] = map[i][j] = map[i][j + 1] =  . ; n     } n   bool ok = true; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) n       if (map[i][j] !=  . ) ok = false; n   puts(ok ?  YES  :  NO ); n   if (ok) n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) putchar(ans[i][j]); n       putchar(  n ); n     } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n struct mat { n   long long** val; n   mat() { n     val = new long long*[6]; n     for (int i = 0; i < 6; i++) { n       val[i] = new long long[6]; n       for (int j = 0; j < 6; j++) val[i][j] = 0; n       val[i][i] = 1; n     } n   } n   mat(long long v[6][6]) { n     val = new long long*[6]; n     for (int i = 0; i < 6; i++) { n       val[i] = new long long[6]; n       for (int j = 0; j < 6; j++) val[i][j] = v[i][j]; n     } n   } n   mat operator*(const mat& b) { n     mat ans; n     for (int i = 0; i < 6; i++) { n       for (int j = 0; j < 6; j++) { n         ans.val[i][j] = 0; n         for (int k = 0; k < 6; k++) { n           ans.val[i][j] += (val[i][k] * b.val[k][j]) % n; n           ans.val[i][j] %= n; n         } n       } n     } n     return ans; n   } n   mat& operator=(const mat& b) { n     for (int i = 0; i < 6; i++) { n       for (int j = 0; j < 6; j++) val[i][j] = b.val[i][j]; n     } n     return *this; n   } n }; n long long rec[6][6] = {{2, 1, 1, 0, 1, 2}, {1, 2, 0, 1, 1, 2}, n                        {1, 1, 1, 0, 1, 2}, {1, 1, 0, 1, 1, 2}, n                        {0, 0, 0, 0, 1, 1}, {0, 0, 0, 0, 0, 1}}; n mat matrec; n mat matexp(long long e) { n   if (e == 0ll) return mat(); n   mat tmp = matexp(e / 2ll); n   tmp = (tmp * tmp); n   if (e & 1) return tmp * matrec; n   return tmp; n } n int main() { n   ios::sync_with_stdio(0); n   matrec = mat(rec); n   long long sx, sy, dx, dy, t; n   cin >> n >> sx >> sy >> dx >> dy >> t; n   if (t == 0ll) { n     cout << sx <<     << sy <<   n ; n     return 0; n   } n   sx--; n   sy--; n   dx %= n; n   dy %= n; n   while (dx < 0) dx += n; n   while (dy < 0) dy += n; n   long long vec[] = {sx, sy, dx, dy, 0, 1}; n   mat ans = matexp(t); n   long long ansx = 0, ansy = 0; n   for (int i = 0; i < 6; i++) { n     ansx += (vec[i] * ans.val[0][i]) % n; n     ansy += (vec[i] * ans.val[1][i]) % n; n     ansx %= n; n     ansy %= n; n   } n   cout << ansx + 1 <<     << ansy + 1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 100; n const int mod = 1e9 + 7; n int main() { n   ios::sync_with_stdio(0); n   int t; n   cin >> t; n   while (t--) { n     int n, x, y; n     cin >> n >> x >> y; n     cout << max(min(n, x + y - n + 1), 1) <<     << min(x + y - 1, n) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n namespace SCCKosaraju { n vector<vector<int>> adj, radj; n vector<int> todo, comp, curr; n vector<bool> vis; n void dfs1(int x) { n   vis[x] = 1; n   for (int y : adj[x]) n     if (!vis[y]) dfs1(y); n   todo.push_back(x); n } n void dfs2(int x, int i) { n   comp[x] = i; n   curr.push_back(x); n   for (int y : radj[x]) n     if (comp[y] == -1) dfs2(y, i); n } n vector<int> scc(vector<vector<int>>& _adj) { n   adj = _adj; n   int time = 0, n = adj.size(); n   comp.resize(n, -1), radj.resize(n), vis.resize(n); n   for (int x = 0; x < n; x++) n     for (int y : adj[x]) radj[y].push_back(x); n   for (int x = 0; x < n; x++) n     if (!vis[x]) dfs1(x); n   reverse(todo.begin(), todo.end()); n   for (int x : todo) n     if (comp[x] == -1) dfs2(x, time++); n   return comp; n } n };  // namespace SCCKosaraju n int main() { n   cin.tie(0)->sync_with_stdio(0); n   cin.exceptions(cin.failbit); n   const ll MOD = 1000000007; n   int n; n   cin >> n; n   vector<int> cost(n); n   for (int& x : cost) cin >> x; n   vector<vector<int>> adj(n); n   int m; n   cin >> m; n   for (int i = 0; i < m; i++) { n     int u, v; n     cin >> u >> v; n     adj[u - 1].push_back(v - 1); n   } n   vector<int> comp = SCCKosaraju::scc(adj); n   vector<ll> add(comp.size(), (int)1e9 + 10); n   vector<ll> cnt(comp.size(), 1); n   int ncomps = 0; n   for (int i = 0; i < n; i++) { n     ncomps = max(ncomps, comp[i] + 1); n     if (cost[i] < add[comp[i]]) { n       add[comp[i]] = cost[i]; n       cnt[comp[i]] = 1; n     } else if (cost[i] == add[comp[i]]) n       cnt[comp[i]]++; n   } n   ll ans = 0, ways = 1; n   for (int i = 0; i < ncomps; i++) ans += add[i]; n   for (int i = 0; i < ncomps; i++) ways = (ways * cnt[i]) % MOD; n   cout << ans <<     << ways <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 10000005, p = 1e9 + 7; n int n, k, cnt, phi[Maxn], prim[Maxn], sum[Maxn], ct[Maxn]; n long long ans; n bool vis[Maxn]; n void init(void) { n   phi[1] = 1; n   for (int i = 2; i <= n; i++) { n     if (!vis[i]) prim[++cnt] = i, phi[i] = i - 1; n     for (int j = 1; j <= cnt && i * prim[j] <= n; j++) { n       vis[i * prim[j]] = true; n       if (i % prim[j] == 0) { n         phi[i * prim[j]] = phi[i] * prim[j]; n         break; n       } n       phi[i * prim[j]] = phi[i] * (prim[j] - 1); n     } n   } n } n long long get_inv(int x) { n   if (x <= 1) return 1; n   return (p - p / x) * get_inv(p % x) % p; n } n int main() { n   scanf( %d%d , &n, &k); n   init(); n   for (int i = 2; i <= n; i++) sum[i] = (sum[i - 1] + phi[i]) % p; n   for (int i = 1; i <= n; i++) ct[i] = n - i - 1; n   for (int i = 1; i < n; i++) { n     int l = (n - 1) / i; n     (ct[i] += sum[l + 1]) %= p; n     (ct[(l + 1) * i - n] += p - phi[l + 1]) %= p; n   } n   long long now = 1; n   for (int i = 1; i < n; i++) { n     now = now * k % p; n     (ans += ct[i] * now) %= p; n   } n   printf( %lld , ans * get_inv(now * k % p) % p); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100010], diff[100010]; n int main() { n   int i, j, k, n, neg1, neg2, pos, ans, mx = -1, df, d; n   cin >> n; n   for (i = 0; i < n; i++) cin >> a[i]; n   for (i = 0; i < n - 1; i++) { n     diff[i] = a[i] - a[i + 1]; n   } n   i = 0; n   while (i < n - 1) { n     neg1 = neg2 = pos = ans = d = 0; n     while (i < n - 1 && diff[i] < 0) { n       neg1++; n       i++; n     } n     while (i < n - 1 && diff[i] >= 0) { n       pos++; n       i++; n     } n     if (pos) { n       if (pos == 1) { n         if ((i + 1 < n && a[i + 1] - a[i - 1] > 1) || n             (i - 2 >= 0 && a[i] - a[i - 2] > 1)) { n           j = i; n           while (j < n - 1 && diff[j] < 0) { n             neg2++; n             j++; n           } n         } n         ans = neg1 + neg2 + 1; n         d = 1; n       } else { n         ans = neg1 + 1; n         d = 1; n       } n     } else { n       ans = neg1; n       d = 0; n     } n     if (ans > mx || ((ans == mx) && (d == 0))) { n       mx = ans; n       df = d; n     } n   } n   if (n < 3) n     cout << n; n   else if (mx == 0) n     cout <<  2 ; n   else { n     if (df == 0 && mx + 1 < n) n       cout << mx + 2; n     else n       cout << mx + 1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n char s[100000 + 10]; n int num[30]; n int main() { n   scanf( %d , &n); n   scanf( %s , s); n   sort(s, s + n); n   printf( %s n , s); n } n


#include <bits/stdc++.h> n using namespace std; n void fileioe() { n   freopen( input.txt ,  r , stdin); n   freopen( xyz.txt ,  w , stdout); n   freopen( error.txt ,  w , stderr); n } n const long long mod = 1000000007; n const long long INF = 1e18; n const long long MAX = 100001; n void solve() { n   long long n; n   cin >> n; n   string s; n   cin >> s; n   string str1, str2; n   int flag1 = 0, flag0 = 0; n   for (int i = 0; i < n; i++) { n     if (s[i] ==  1 ) { n       flag1 = 1; n       for (int j = 0; j < i; j++) { n         if (s[j] ==  0 ) str1 +=  0 ; n       } n       break; n     } n   } n   for (int i = n - 1; i >= 0; i--) { n     if (s[i] ==  0 ) { n       flag0 = 1; n       str2 = s.substr(i); n       break; n     } n   } n   if (flag1 == 0 || flag0 == 0) { n     cout << s << endl; n     return; n   } n   string res = str1 + str2; n   if (res.length() > n) { n     cout << res.substr(1) << endl; n     return; n   } n   cout << res << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   long long t; n   t = 1; n   cin >> t; n   for (int i = 1; i <= t; i++) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long lkd(long long a, long long b) { n   while (b > 0) { n     long long c = b; n     b = a % b; n     a = c; n   } n   return a; n } n int main() { n   long long a, s, b = 1; n   cin >> a >> s; n   for (long long i = 0; i < s; i++) b = b * 10; n   cout << (a * b) / lkd(a, b); n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 110000; n long long a[M], b[M], c[M], be[M]; n long long n, d, x; n long long getNextX() { n   x = (x * 37 + 10007) % 1000000007; n   return x; n } n void initAB() { n   long long i; n   for (i = 0; i < n; i = i + 1) { n     a[i] = i + 1; n   } n   for (i = 0; i < n; i = i + 1) { n     swap(a[i], a[getNextX() % (i + 1)]); n   } n   for (i = 0; i < n; i = i + 1) { n     if (i < d) n       b[i] = 1; n     else n       b[i] = 0; n   } n   for (i = 0; i < n; i = i + 1) { n     swap(b[i], b[getNextX() % (i + 1)]); n   } n } n int main() { n   long long i, j, k; n   while (~scanf( %I64d%I64d%I64d , &n, &d, &x)) { n     initAB(); n     vector<int> vt; n     memset(be, 0, sizeof(be)); n     int cnt = 0; n     for (i = 0; i < n; i++) n       if (b[i]) cnt++; n     if (cnt <= sqrt(n)) { n       for (i = 0; i < n; i++) n         if (b[i]) vt.push_back(i); n       for (i = 0; i < n; i++) { n         long long mx = 0; n         for (j = 0; j < vt.size(); j++) { n           k = vt[j]; n           if (k > i) break; n           c[i] = max(c[i], a[i - k]); n         } n       } n     } else { n       vector<pair<int, int> > vc; n       for (i = 0; i < n; i++) vc.push_back(make_pair(a[i], i)); n       sort(vc.begin(), vc.end()); n       for (i = 0; i < n; i++) { n         for (j = n - 1; j >= 0; j--) { n           if (vc[j].second <= i && b[i - vc[j].second]) { n             c[i] = vc[j].first; n             break; n           } n         } n       } n     } n     for (i = 0; i < n; i++) cout << c[i] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5; n int ans[N << 1]; n int main() { n   int n; n   cin >> n; n   int b1 = n + 1; n   int b2 = 2 * n - 1; n   int a1 = 1; n   int a2 = n; n   int cnt = 1; n   for (int i = 1; i <= n - 1; ++i) { n     if (cnt & 1) n       ans[a1++] = i, ans[a2--] = i; n     else n       ans[b1++] = i, ans[b2--] = i; n     cnt++; n   } n   for (int i = 1; i <= 2 * n; ++i) n     if (ans[i] == 0) n       cout << n <<    ; n     else n       cout << ans[i] <<    ; n } n


#include <bits/stdc++.h> n typedef struct { n   long long s, l, r, m; n } value; n long long maxll(long long a, long long b) { return a > b ? a : b; } n void sum(value *v, value *l, value *r) { n   v->s = l->s + r->s; n   v->l = maxll(l->l, l->s + r->l); n   v->r = maxll(r->r, r->s + l->r); n   v->m = maxll(l->r + r->l, maxll(l->m, r->m)); n } n value t[4 * 150000]; n long long a[150000]; n void build(int i, int l, int r) { n   if (l == r) { n     t[i].s = a[l]; n     t[i].l = t[i].r = t[i].m = maxll(a[l], 0); n   } else { n     int m = (l + r) / 2; n     build(2 * i + 1, l, m); n     build(2 * i + 2, m + 1, r); n     sum(&t[i], &t[2 * i + 1], &t[2 * i + 2]); n   } n } n value get(int ll, int rr, int i, int l, int r) { n   if (rr < l || r < ll) { n     value v = {0, 0, 0, 0}; n     return v; n   } n   if (ll <= l && r <= rr) return t[i]; n   int m = (l + r) / 2; n   value lv, rv, av; n   lv = get(ll, rr, 2 * i + 1, l, m); n   rv = get(ll, rr, 2 * i + 2, m + 1, r); n   sum(&av, &lv, &rv); n   return av; n } n int main() { n   int n, m, c; n   scanf( %d%d%d , &n, &m, &c); n   int i, x[150000]; n   for (i = 0; i < n; i++) scanf( %d , &x[i]); n   int p[150000]; n   for (i = 0; i < n - 1; i++) scanf( %d , &p[i]); n   for (i = 0; i < n - 1; i++) a[i] = 1LL * (x[i + 1] - x[i]) * 50 - c * p[i]; n   build(0, 0, n - 2); n   long long s = 0; n   for (i = 0; i < m; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     l--; n     r--; n     r--; n     s += get(l, r, 0, 0, n - 2).m; n   } n   printf( %.2lf n , 1. * s / 100); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void read(T &x) { n   x = 0; n   char c = getchar(); n   bool f = 0; n   for (; !isdigit(c); c = getchar()) f ^= c ==  - ; n   for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); n   x = f ? -x : x; n } n int n, x, len, res, minn = 0x7fffffff; n char s[400001]; n string ans, c[6] = { RGB ,  RBG ,  BRG ,  BGR ,  GRB ,  GBR }; n int main() { n   read(n); n   scanf( %s , s), len = strlen(s); n   for (int i = 0; i < 6; i++) { n     res = 0, x = 0; n     for (int j = 0; j < len; j++) { n       if (s[j] != c[i][x++]) res++; n       if (x == 3) x = 0; n     } n     if (res < minn) { n       minn = res; n       ans = c[i]; n     } n   } n   x = 0; n   cout << minn <<   n ; n   for (int j = 0; j < len; j++) { n     putchar(ans[x++]); n     if (x == 3) x = 0; n   } n   putchar(  n ); n   return 0; n } n


#include <bits/stdc++.h> n #pragma optimization_level 3 n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math,O3 ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx ) n using namespace std; n const long double PI = 3.14159265358979323846; n const long long mod = 1e+9 + 7; n const int INFi = INT_MAX; n const long long INF = (long long)1e+9 + 7; n const int N = 1200300, LOGN = 30, V = N * LOGN; n long long dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0}; n int t = 1; n long long a, b, c, ans; n string second; n void MAIN() { n   cin >> a >> b >> c; n   ans = 0; n   ans = max(ans, a / 3 + b / 3 + c / 3); n   if (a > 0 && b > 0 && c > 0) n     ans = max(ans, 1 + (a - 1) / 3 + (b - 1) / 3 + (c - 1) / 3); n   if (a > 1 && b > 1 && c > 1) n     ans = max(ans, 2 + (a - 2) / 3 + (b - 2) / 3 + (c - 2) / 3); n   cout << ans << endl; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed; n   cout << setprecision(10); n   ; n   mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n   ; n   while (t--) { n     MAIN(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3005; n int prv[maxn], nxt[maxn], cnt[maxn]; n int le[maxn]; n int r, c, n, k; n vector<int> pos[maxn]; n signed main(void) { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> r >> c >> n >> k; n   for (int i = 1; i <= n; ++i) { n     int x, y; n     cin >> x >> y; n     pos[y].emplace_back(x); n   } n   long long res = 0; n   for (int i = 1; i <= c; ++i) { n     set<int> second; n     second.insert(0); n     second.insert(r + 1); n     for (int j = 0; j <= r; ++j) cnt[j] = 0, le[j] = 0; n     nxt[0] = r + 1; n     prv[r + 1] = 0; n     long long sum = 0; n     for (int j = i; j <= c; ++j) { n       for (int val : pos[j]) { n         if (!cnt[val]) { n           int l = *prev(second.lower_bound(val)); n           int r = *(second.upper_bound(val)); n           nxt[l] = val; n           nxt[val] = r; n           prv[r] = val; n           prv[val] = l; n           sum -= 1ll * le[l] * (nxt[val] - val); n           second.insert(val); n         } n         cnt[val]++; n         int cur = val, now = 0; n         while (cur != 0) { n           now += cnt[cur]; n           if (now >= k) break; n           cur = prv[cur]; n         } n         int total = cnt[val]; n         if (cur > le[val]) { n           sum += 1ll * (cur - le[val]) * (nxt[val] - val); n           le[val] = cur; n         } n         while (true) { n           val = nxt[val]; n           total += cnt[val]; n           now += cnt[val]; n           if (val > r || total > k) break; n           while (cur < val && now - cnt[cur] >= k) { n             now -= cnt[cur]; n             cur = nxt[cur]; n           } n           if (cur > le[val]) { n             assert(now >= k); n             sum += 1ll * (cur - le[val]) * (nxt[val] - val); n             le[val] = cur; n           } n         } n       } n       res += sum; n     } n   } n   cout << res; n } n


#include <bits/stdc++.h> n using namespace std; n string s[555]; n int dp[555], v[555], ones[555]; n vector<pair<int, int> > vc[555]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int n, m, k; n   cin >> n >> m >> k; n   for (int i = 1; i <= n; i++) { n     cin >> s[i]; n     int cnt = 0; n     for (int j = 0; j < m; j++) { n       if (s[i][j] ==  1 ) ones[cnt++] = j + 1; n     } n     memset(v, 0, sizeof(v)); n     v[cnt] = m; n     for (int j = 0; j < cnt; j++) { n       for (int k = j; k < cnt; k++) { n         int len = cnt - (k - j + 1); n         int value = m - (ones[k] - ones[j] + 1); n         v[len] = max(v[len], value); n       } n     } n     for (int j = 0; j <= k; j++) { n       if (v[j]) vc[i].push_back(make_pair(j, v[j])); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = k; j >= 0; j--) { n       for (int k = 0; k < vc[i].size(); k++) { n         if (j >= vc[i][k].first) n           dp[j] = max(dp[j], dp[j - vc[i][k].first] + vc[i][k].second); n       } n     } n   } n   cout << n * m - dp[k] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MaxN = int(2e5) + 256; n const int INF = int(1e9); n const int mod = (int)(1e9) + 7; n const double pi = 3.1415926535897932384626433832795; n long long n, m, ans, A[MaxN], k, a, b, q; n long long t[MaxN * 8], t1[MaxN * 8]; n void upd(int v, int l, int r, int pos, int val) { n   if (l == r) { n     t[v] += val; n     t1[v] += val; n     t[v] = min(a, t[v]); n     t1[v] = min(t1[v], b); n     return; n   } n   int m = (l + r) / 2; n   if (pos <= m) { n     upd(v + v, l, m, pos, val); n   } else n     upd(v + v + 1, m + 1, r, pos, val); n   t[v] = t[v + v] + t[v + v + 1]; n   t1[v] = t1[v + v] + t1[v + v + 1]; n } n long long get(int v, int l, int r, int tl, int tr) { n   if (l > tr || r < tl) return 0; n   if (tl <= l & r <= tr) return t[v]; n   int m = (l + r) / 2; n   return get(v + v, l, m, tl, tr) + get(v + v + 1, m + 1, r, tl, tr); n } n long long get1(int v, int l, int r, int tl, int tr) { n   if (l > tr || r < tl) return 0; n   if (tl <= l & r <= tr) return t1[v]; n   int m = (l + r) / 2; n   return get1(v + v, l, m, tl, tr) + get1(v + v + 1, m + 1, r, tl, tr); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k >> a >> b >> q; n   while (q--) { n     int type; n     cin >> type; n     if (type == 1) { n       int x, y; n       cin >> x >> y; n       upd(1, 1, n, x, y); n     } else { n       int p; n       cin >> p; n       int S = 0; n       if (p > 1) { n         S = get1(1, 1, n, 1, p - 1); n       } n       int S1 = 0; n       if (p + k <= n) { n         S1 = get(1, 1, n, p + k, n); n       } n       cout << S + S1 <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n, i, j; n   cin >> n; n   long long int a[n], b[n], t[n]; n   for (i = 1; i < n; i++) cin >> a[i]; n   for (i = 1; i < n; i++) cin >> b[i]; n   for (i = 0; i < 4; i++) { n     t[0] = i; n     for (j = 1; j < n; j++) { n       if (a[j] == 3 && b[j] == 0) { n         if (t[j - 1] == 1) n           t[j] = 2; n         else if (t[j - 1] == 2) n           t[j] = 1; n         else if (t[j - 1] == 3) n           t[j] = 0; n         else n           t[j] = 3; n         continue; n       } n       if (a[j] < b[j]) { n         cout <<  NO ; n         return 0; n       } n       if (a[j] == 2 && b[j] == 1) { n         cout <<  NO ; n         return 0; n       } n       if (t[j - 1] == a[j]) n         t[j] = b[j]; n       else if (t[j - 1] == b[j]) n         t[j] = a[j]; n       else n         break; n     } n     if (j == n) break; n   } n   if (i == 4) { n     cout <<  NO ; n     return 0; n   } n   cout <<  YES  << endl; n   for (i = 0; i < n; i++) cout << t[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, depth = 0; n string loc[500]; n string comm; n int main() { n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> comm; n     if (comm ==  cd ) { n       cin >> comm; n       int pos = 0; n       if (comm[0] ==  / ) { n         depth = 0; n         pos = 1; n       } n       string dir =   ; n       for (; pos < comm.length(); ++pos) { n         if (comm[pos] !=  / ) dir += comm[pos]; n         if (comm[pos] ==  /  || pos == comm.length() - 1) { n           if (dir ==  .. ) n             depth--; n           else n             loc[depth++] = dir; n           dir =   ; n         } n       } n     } else { n       for (int i = 0; i < depth; ++i) cout <<  /  << loc[i]; n       cout <<  /  << endl; n     } n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ) n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long int n, p; n   cin >> n >> p; n   if (p == 0) { n     cout << __builtin_popcount(n); n     return 0; n   } n   if (p > 0) { n     long long int a = n; n     long long int cnt = 0; n     while (a > 0) { n       long long int temp = __builtin_popcount(a); n       long long int c = 0, k = 0; n       while (a) { n         if (a & 1) { n           k += (c + 1); n         } n         a = a / 2; n         c++; n       } n       if (temp <= cnt) { n         if (k >= cnt) { n           cout << cnt; n           return 0; n         } n       } n       cnt++; n       a = n - cnt * p; n     } n   } else { n     long long int a = n; n     long long int cnt = 0; n     while (cnt < 1000) { n       long long int temp = __builtin_popcount(a); n       long long int c = 0, k = 0; n       while (a) { n         if (a & 1) { n           k += (c + 1); n         } n         a = a / 2; n         c++; n       } n       if (temp <= cnt) { n         if (k >= cnt) { n           cout << cnt; n           return 0; n         } n       } n       cnt++; n       a = n - cnt * p; n     } n   } n   cout << -1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long oo = 1000000000000000000; n const int N = 100005; n int main() { n   int n, x; n   scanf( %d , &n); n   multiset<int> a, b; n   for (int i = 0; i < int(n); i++) { n     scanf( %d , &x); n     a.insert(x); n   } n   for (int i = 0; i < int(n - 1); i++) { n     scanf( %d , &x); n     b.insert(x); n     a.erase(a.find(x)); n   } n   printf( %d n , *a.begin()); n   a.clear(); n   swap(a, b); n   for (int i = 0; i < int(n - 2); i++) { n     scanf( %d , &x); n     b.insert(x); n     a.erase(a.find(x)); n   } n   printf( %d n , *a.begin()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long double Eps = 1e-8; n char what[27] =  A??????HI???M?O????TUVWXY? ; n char str[100005]; n void input() { scanf( %s , str); } n bool good() { n   int N = strlen(str); n   for (int i = 0; i < N; i++) { n     if (str[i] != what[str[N - i - 1] -  A ]) return false; n   } n   return true; n } n int main() { n   input(); n   printf(good() ?  YES n  :  NO n ); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int h, w, x, y, star = 0, sum, flag; n   char arr[505][505]; n   cin >> h >> w; n   for (int i = 1; i <= h; i++) { n     for (int j = 1; j <= w; j++) { n       cin >> arr[i][j]; n       if (arr[i][j] ==  * ) { n         star++; n         if (arr[i][j - 1] ==  *  && arr[i - 1][j] ==  * ) { n           sum = 3; n           x = i; n           y = j; n         } n       } n     } n   } n   if (sum == 3) { n     if (arr[x][y + 1] ==  *  && arr[x + 1][y] ==  * ) { n       sum = sum + 2; n     } n     if (sum == 5) { n       flag = 1; n       if (x + 2 <= h) { n         for (int i = x + 2; i <= h; i++) { n           if (arr[i][y] ==  * ) n             sum++; n           else n             break; n         } n       } n       if (x - 2 > 0) { n         for (int i = x - 2; i > 0; i--) { n           if (arr[i][y] ==  * ) n             sum++; n           else n             break; n         } n       } n       if (y + 2 <= w) n         for (int i = y + 2; i <= w; i++) { n           if (arr[x][i] ==  * ) n             sum++; n           else n             break; n         } n       if (y - 2 > 0) { n         for (int i = y - 2; i > 0; i--) { n           if (arr[x][i] ==  * ) n             sum++; n           else n             break; n         } n       } n     } n   } n   if (star == sum && sum >= 5) n     cout <<  YES ; n   else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long s, x; n int main() { n   srand(time(NULL)); n   cin >> n; n   n--; n   s = 1; n   x = 12; n   while (n) { n     s += x; n     x += 12; n     n--; n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e5 + 10; n int a[M], col[M], flag; n bool vis[M]; n vector<int> vc[M]; n vector<pair<int, int> > node[M]; n void bfs(int sta) { n   col[sta] = 0; n   vis[sta] = true; n   queue<int> q; n   q.push(sta); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     int len = node[u].size(); n     for (int i = 0; i < len; i++) { n       int co = col[u]; n       pair<int, int> v = node[u][i]; n       if (v.second == 0) { n         co ^= 1; n       } n       if (vis[v.first] && col[v.first] != co) { n         flag = false; n         return; n       } n       if (!vis[v.first]) { n         col[v.first] = co; n         vis[v.first] = true; n         q.push(v.first); n       } n     } n   } n } n int main() { n   int n, m; n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 1; i <= m; i++) { n     int sz; n     scanf( %d , &sz); n     for (int j = 1; j <= sz; j++) { n       int x; n       scanf( %d , &x); n       vc[x].push_back(i); n     } n   } n   for (int i = 1; i <= n; i++) { n     int u = vc[i][0], v = vc[i][1]; n     node[v].push_back(make_pair(u, a[i])); n     node[u].push_back(make_pair(v, a[i])); n   } n   memset(vis, false, sizeof(vis)); n   memset(col, -1, sizeof(col)); n   flag = true; n   for (int i = 1; i <= m; i++) { n     if (!vis[i]) { n       bfs(i); n     } n   } n   if (flag) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a, b, c, n; n int main() { n   ios_base::sync_with_stdio(false); n   ; n   cin >> a >> b >> c >> n; n   if (a - c < 0 || b - c < 0) return cout << -1, 0; n   if (a + b - c >= n) return cout << -1, 0; n   cout << n - (a + b - c); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5; n int n; n string s; n int main() { n   cin >> n; n   cin >> s; n   bool tmp = false; n   for (int i = 0; i <= s.size() - 1; i++) { n     if (s[i] > s[i + 1]) { n       tmp = true; n       s.erase(i, 1); n       break; n     } n   } n   if (!tmp) s.erase(s.size() - 1, 1); n   cout << s; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 1e5 + 10; n long long len, ans, val[26], s[mx]; n char str[mx]; n map<pair<long long, char>, long long> dp; n int main() { n   for (int i = 0; i < 26; i++) scanf( %I64d , &val[i]); n   scanf( %s , str); n   len = strlen(str); n   s[0] = val[str[0] -  a ]; n   for (int i = 1; i < len; i++) s[i] = s[i - 1] + val[str[i] -  a ]; n   for (int i = 0; i < len; i++) { n     ans += dp[make_pair(s[i - 1], str[i])]; n     dp[make_pair(s[i], str[i])]++; n   } n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, t, sum = 0, x; n   cin >> n >> x; n   while (n--) { n     cin >> t; n     sum += t; n   } n   if (sum < 0) sum *= -1; n   int ans = sum / x + bool(sum % x); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n; n   scanf( %d , &n); n   int p = 0; n   int a[100100]; n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   int s = n + n - 1; n   for (int i = 0; i < n; i++) { n     s += fabs(a[i] - p); n     p = a[i]; n   } n   printf( %d n , s); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:100000000000000 ) n using namespace std; n const long long int INF = 2e9 + 1; n char s[50000], s2[50000]; n struct automato { n   struct leaf { n     int link, len; n     map<char, int> nex; n     bool cl; n     leaf() : cl(false) {} n   }; n   vector<leaf> tree; n   vector<int> cnt, used; n   int sz, pre; n   automato() {} n   void init() { n     sz = 1; n     pre = 0; n     tree.resize(2 * 50000); n     cnt.resize(2 * 50000, 0); n     used.resize(2 * 50000, 0); n     tree[0].link = -1; n     tree[0].len = 0; n   }; n   void add_s(char c) { n     int cur = sz++; n     tree[cur].len = tree[pre].len + 1; n     int temp = pre; n     for (; temp != -1 && !tree[temp].nex.count(c); temp = tree[temp].link) { n       tree[temp].nex[c] = cur; n     } n     if (temp == -1) { n       tree[cur].link = 0; n     } else { n       int p = tree[temp].nex[c]; n       if (tree[p].len == tree[temp].len + 1) { n         tree[cur].link = p; n       } else { n         int clone = sz++; n         tree[clone].nex = tree[p].nex; n         tree[clone].len = tree[temp].len + 1; n         tree[clone].link = tree[p].link; n         tree[clone].cl = true; n         tree[p].link = clone; n         tree[cur].link = clone; n         for (; temp != -1 && tree[temp].nex.count(c) && tree[temp].nex[c] == p; n              temp = tree[temp].link) { n           tree[temp].nex[c] = clone; n         } n       } n     } n     pre = cur; n   }; n   void add_str(char *s) { n     int n = strlen(s); n     for (int i = 0; i < (n); i++) { n       add_s(s[i]); n     } n   } n   void dfs() { n     vector<pair<int, int> > len; n     for (int i = 0; i < (sz); i++) { n       len.push_back(make_pair(tree[i].len, i)); n     } n     sort(len.begin(), len.end()); n     reverse(len.begin(), len.end()); n     for (int i = 0; i < (sz); i++) { n       int id = len[i].second; n       if (!tree[id].cl) cnt[id]++; n       if (tree[id].link != -1) cnt[tree[id].link] += cnt[id]; n     } n   } n   int nx(int id, char c) { n     if (!tree[id].nex.count(c)) return -1; n     return tree[id].nex[c]; n   } n   int get_cnt(int now) { return cnt[now]; } n }; n int main() { n   scanf( %s%s , &s, &s2); n   int n = strlen(s), m = strlen(s2); n   automato a, b; n   a.init(); n   b.init(); n   a.add_str(s); n   b.add_str(s2); n   a.dfs(); n   b.dfs(); n   int out = INF; n   for (int i = 0; i < n; i++) { n     int st = 0, st2 = 0; n     for (int j = i; j < n; j++) { n       st = a.nx(st, s[j]); n       st2 = b.nx(st2, s[j]); n       if (st == -1 || st2 == -1) break; n       if (a.get_cnt(st) == 1 && b.get_cnt(st2) == 1) { n         out = min(out, j - i + 1); n         break; n       } n     } n   } n   cout << (out == INF ? -1 : out); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   double n, k; n   cin >> n >> k; n   double a, b; n   cin >> a >> b; n   n--; n   double len = 0; n   while (n--) { n     double x, y; n     cin >> x >> y; n     len += ((double)sqrt(pow(x - a, 2) + pow(y - b, 2))); n     a = x; n     b = y; n   } n   len *= k; n   cout << fixed << setprecision(9) << len / (double)50 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e4; n const int T = 24 * 60 * 60; n int was, n, M, add, cnt, ans[N]; n vector<int> ind[T]; n int main() { n   scanf( %d%d%d , &n, &M, &add); n   for (int i = 0; i < (int)(n); i++) { n     int h, m, s; n     scanf( %d:%d:%d , &h, &m, &s); n     ind[s + 60 * (m + 60 * h)].push_back(i); n   } n   set<pair<int, int> > s; n   for (int i = 0; i < (int)(T); i++) { n     while ((int)(s).size() && s.begin()->first <= i) s.erase(s.begin()); n     for (int j : ind[i]) { n       if ((int)(s).size() < M) n         ans[j] = ++cnt; n       else { n         ans[j] = s.rbegin()->second; n         s.erase(*s.rbegin()); n       } n       s.insert(pair<int, int>(i + add, ans[j])); n     } n     was |= (int)(s).size() == M; n   } n   if (!was) n     puts( No solution ); n   else { n     printf( %d n , cnt); n     for (int i = 0; i < (int)(n); i++) printf( %d n , ans[i]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 998244353; n int sum(int a, int b) { return (a + b) % MOD; } n int prod(int a, int b) { return a * 1ll * b % MOD; } n int mpow(int b, int e) { n   if (e == 0) return 1; n   if (e & 1) return prod(b, mpow(b, e - 1)); n   return mpow(prod(b, b), e / 2); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0), cout.tie(0); n   cout << fixed << setprecision(15); n   const int HALF = mpow(2, MOD - 2); n   int N; n   cin >> N; n   vector<vector<int> > adj(N); n   for (int i = 0; i < N - 1; i++) { n     int u, v; n     cin >> u >> v; n     adj[u - 1].push_back(v - 1); n     adj[v - 1].push_back(u - 1); n   } n   int ans = 0; n   function<int(int, int)> calc = [&](int loc, int par) { n     int nm = 1; n     for (int nbr : adj[loc]) { n       if (nbr == par) continue; n       int unv = calc(nbr, loc); n       unv = sum(unv, prod(HALF, sum(1, MOD - unv))); n       nm = prod(nm, unv); n     } n     nm = sum(1, MOD - nm); n     ans = sum(ans, nm); n     return nm; n   }; n   calc(0, 0); n   for (int i = 0; i < N; i++) ans = prod(ans, 2); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   bool a = false; n   cin >> n; n   for (long long i = 1; i < n; i++) { n     for (long long j = 1; j < n; j++) { n       if (((n - i - j) % 3) != 0 && (i % 3) != 0 && (j % 3) != 0) { n         cout << i <<     << j <<     << (n - i - j) << endl; n         a = true; n         break; n       } n     } n     if (a) break; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gi() { n   int x = 0, w = 1; n   char ch = getchar(); n   while ((ch <  0  || ch >  9 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) w = 0, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     x = (x << 3) + (x << 1) + ch -  0 , ch = getchar(); n   return w ? x : -x; n } n const int N = 3e5 + 5; n int q, fa[N + N], top; n pair<int, int> sz[N + N], sta[N]; n map<pair<int, int>, int> M; n vector<pair<int, int> > vec[N << 2]; n long long ans; n pair<int, int> operator+(pair<int, int> a, pair<int, int> b) { n   return make_pair(a.first + b.first, a.second + b.second); n } n pair<int, int> operator-(pair<int, int> a, pair<int, int> b) { n   return make_pair(a.first - b.first, a.second - b.second); n } n int find(int x) { return x == fa[x] ? x : find(fa[x]); } n void modify(int x, int l, int r, int ql, int qr, pair<int, int> p) { n   if (l >= ql && r <= qr) { n     vec[x].push_back(p); n     return; n   } n   int mid = l + r >> 1; n   if (ql <= mid) modify(x << 1, l, mid, ql, qr, p); n   if (qr > mid) modify(x << 1 | 1, mid + 1, r, ql, qr, p); n } n void dfs(int x, int l, int r) { n   int tt = top; n   for (pair<int, int> p : vec[x]) n     if (find(p.first) != find(p.second)) { n       int u = find(p.first), v = find(p.second); n       if (sz[u].first + sz[u].second < sz[v].first + sz[v].second) swap(u, v); n       sta[++top] = make_pair(u, v); n       ans -= 1ll * sz[u].first * sz[u].second; n       ans -= 1ll * sz[v].first * sz[v].second; n       fa[v] = u; n       sz[u] = sz[u] + sz[v]; n       ans += 1ll * sz[u].first * sz[u].second; n     } n   if (l == r) n     printf( %lld  , ans); n   else { n     int mid = l + r >> 1; n     dfs(x << 1, l, mid); n     dfs(x << 1 | 1, mid + 1, r); n   } n   while (top > tt) { n     int u = sta[top].first, v = sta[top].second; n     --top; n     ans -= 1ll * sz[u].first * sz[u].second; n     fa[v] = v; n     sz[u] = sz[u] - sz[v]; n     ans += 1ll * sz[u].first * sz[u].second; n     ans += 1ll * sz[v].first * sz[v].second; n   } n } n int main() { n   q = gi(); n   for (int i = 1; i <= q; ++i) { n     int x = gi(), y = gi() + N; n     if (M.find(make_pair(x, y)) == M.end()) n       M[make_pair(x, y)] = i; n     else n       modify(1, 1, q, M[make_pair(x, y)], i - 1, make_pair(x, y)), n           M.erase(make_pair(x, y)); n   } n   for (auto it = M.begin(); it != M.end(); ++it) n     modify(1, 1, q, (*it).second, q, (*it).first); n   for (int i = 1; i < N + N; ++i) fa[i] = i, sz[i] = make_pair(i < N, i >= N); n   dfs(1, 1, q); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s, k; n vector<int> a; n void Input() { n   cin >> n >> s >> k; n   a.clear(); n   a.resize(k); n   for (auto &z : a) cin >> z; n } n void Solve() { n   for (int i = 0; i <= k; i++) { n     if (s - i >= 1 && find(a.begin(), a.end(), s - i) == a.end()) { n       cout << i <<   n ; n       return; n     } n     if (s + i <= n && find(a.begin(), a.end(), s + i) == a.end()) { n       cout << i <<   n ; n       return; n     } n   } n   assert(false); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   int T; n   cin >> T; n   while (T--) { n     Input(); n     Solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, i, j, a[102][102]; n   scanf( %d , &n); n   ; n   for (i = 0; i < n; i++) { n     for (j = 0; j < n; j++) scanf( %d , &a[i][j]); n     ; n   } n   if (i == 1) { n     printf( 0 n ); n     return 0; n   } n   for (i = 0; i < n; i++) { n     int f[32] = {0}; n     for (j = 0; j < n; j++) { n       if (i == j) continue; n       int x = a[i][j], c = 0; n       while (x > 0) { n         if (x & 1) f[c] = 1; n         c++; n         x /= 2; n       } n     } n     int ans = 0, k; n     j = 1; n     for (k = 0; k < 32; k++) { n       if (f[k]) ans += j; n       j *= 2; n     } n     printf( %d  , ans); n   } n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   int a[t]; n   for (int i = 0; i < t; i++) { n     cin >> a[i]; n     if (360 % (180 - a[i]) == 0) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void chmax(T &a, T b) { n   if (a < b) a = b; n } n template <class T> n void chmin(T &a, T b) { n   if (a > b) a = b; n } n constexpr long long INF = 1000000000000000000; n constexpr long long mod = 1000000007; n constexpr long double eps = 1e-8; n const long double pi = acos(-1); n long long dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; n long long Random(long long mi, long long ma) { n   random_device rnd; n   mt19937 mt(rnd()); n   uniform_int_distribution<long long> engine(mi, ma); n   return engine(mt); n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n bool prime(long long a) { n   if (a == 1) return false; n   for (long long i = 2; i * i <= a; i++) { n     if (a % i == 0) return false; n   } n   return true; n } n long long modpow(long long a, long long b) { n   if (b == 0) return 1; n   if (b % 2) return modpow(a, b - 1) * a % mod; n   long long memo = modpow(a, b / 2); n   return memo * memo % mod; n } n vector<long long> kaijo, invkaijo; n void init_fact(long long n) { n   kaijo.resize(n + 1); n   invkaijo.resize(n + 1); n   kaijo[0] = 1; n   for (long long i = 1; i <= n; i++) { n     kaijo[i] = kaijo[i - 1] * i; n     kaijo[i] %= mod; n   } n   for (long long i = 0; i < n + 1; i++) invkaijo[i] = modpow(kaijo[i], mod - 2); n } n long long comb(long long a, long long b) { n   if (a < b) return 0; n   if (a < 0 || b < 0) return 0; n   return kaijo[a] * modpow(kaijo[a - b], mod - 2) % mod * n          modpow(kaijo[b], mod - 2) % mod; n } n long long inv(long long x) { n   x = modpow(x, mod - 2); n   return x; n } n struct BIT { n   long long n, beki = 1; n   vector<long long> bit; n   BIT(long long x) { n     bit.resize(x + 1, 0); n     n = x; n     while (beki * 2 <= n) beki *= 2; n   } n   long long sum(long long i) { n     long long res = 0; n     while (i > 0) { n       res += bit[i]; n       i -= i & -i; n     } n     return res; n   } n   void add(long long i, long long x) { n     while (i <= n) { n       bit[i] += x; n       i += i & -i; n     } n   } n   long long lowerbound(long long w) { n     if (w <= 0) return 0; n     long long x = 0; n     for (long long k = beki; k > 0; k >>= 1) { n       if (x + k <= n && bit[x + k] < w) { n         w -= bit[x + k]; n         x += k; n       } n     } n     return x + 1; n   } n }; n signed main() { n   cin.tie(0); n   ios::sync_with_stdio(false); n   long long n, k; n   cin >> n >> k; n   vector<pair<long long, long long> > v[200010]; n   BIT bit(200001); n   for (long long i = 0; i < n; i++) { n     long long l, r; n     cin >> l >> r; n     bit.add(l, 1); n     bit.add(r + 1, -1); n     v[l].push_back({r, i + 1}); n   } n   priority_queue<pair<long long, long long> > q; n   vector<long long> ans; n   for (long long i = 1; i <= 200000; i++) { n     for (pair<long long, long long> j : v[i]) q.push(j); n     while (bit.sum(i) > k) { n       bit.add(i, -1); n       bit.add(q.top().first + 1, 1); n       ans.push_back(q.top().second); n       q.pop(); n     } n   } n   cout << ans.size() << endl; n   for (long long i : ans) cout << i <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void data() {} n const int N = 1e6 + 100; n const long long mod = 998244353; n const long long mod2 = 1e9 + 7; n map<int, int> was, was2; n int main() { n   data(); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n + 1]; n     for (int i = 1; i <= n; i++) { n       cin >> a[i]; n     } n     int len = 0; n     for (int i = 1; i <= n - 1 && a[i] == 1; i++) { n       len++; n     } n     if (len % 2 == 0) { n       cout <<  First n ; n     } else { n       cout <<  Second n ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct edge { n   int u, v; n   long long w; n }; n long long MOD = 1000000007; n long long _MOD = 1000000009; n double EPS = 1e-10; n long long c[100][100]; n long long C(long long n, long long k) { n   if (n < 100) return c[n][k]; n   long long ans = 1; n   for (int i = 0; i < k; i++) ans *= n - i; n   for (int i = 1; i <= k; i++) ans /= i; n   return ans; n } n class NumberMagic { n  public: n   int theMin(int _N, int _M) { n     if (_N == 1) return 0; n     for (int i = 0; i < 100; i++) { n       c[i][0] = 1; n       for (int j = 1; j <= i; j++) c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; n     } n     long long N = _N, M = _M; n     long long lb = 0, ub = N - 1; n     while (ub - lb > 1) { n       long long mid = (lb + ub) / 2; n       long long n = N, sum = 0; n       for (long long k = 0; k <= mid && n; k++) { n         long long unko = min(n, C(mid, k)); n         n -= unko; n         sum += k * unko; n       } n       if (!n && sum <= mid * M) n         ub = mid; n       else n         lb = mid; n     } n     return ub; n   } n }; n int main() { n   NumberMagic nm; n   int T; n   cin >> T; n   while (T--) { n     int N, M; n     cin >> N >> M; n     cout << nm.theMin(N, M) << endl; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n mt19937 ggen; n int n, s; n int a[200010]; n int ctr[200010]; n vector<int> v; n void solve() { n   cin >> n >> s; n   s--; n   for (int i = 0; i < n; i++) cin >> a[i]; n   int ans = 0; n   if (a[s] != 0) ans++; n   for (int i = 0; i < n; i++) { n     if (i != s) { n       if (a[i] == 0) a[i] = 200009; n       ctr[a[i]]++; n       v.push_back(a[i]); n     } n   } n   sort(v.begin(), v.end()); n   for (int i = 1; !v.empty() && i <= v.back(); i++) { n     if (ctr[i] == 0) { n       ans++; n       ctr[v.back()]--; n       v.pop_back(); n     } n   } n   cout << ans; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   ggen = mt19937(13); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T, bool maximum_mode = false> n struct RMQ { n   int n = 0, levels = 0; n   vector<vector<T>> range_min; n   RMQ(const vector<T> &values = {}) { n     if (!values.empty()) build(values); n   } n   static int largest_bit(int x) { return 31 - __builtin_clz(x); } n   static T better(T a, T b) { return maximum_mode ? max(a, b) : min(a, b); } n   void build(const vector<T> &values) { n     n = values.size(); n     levels = largest_bit(n) + 1; n     range_min.resize(levels); n     for (int k = 0; k < levels; k++) range_min[k].resize(n - (1 << k) + 1); n     if (n > 0) range_min[0] = values; n     for (int k = 1; k < levels; k++) n       for (int i = 0; i <= n - (1 << k); i++) n         range_min[k][i] = n             better(range_min[k - 1][i], range_min[k - 1][i + (1 << (k - 1))]); n   } n   T query_value(int a, int b) const { n     assert(0 <= a && a < b && b <= n); n     int level = largest_bit(b - a); n     return better(range_min[level][a], range_min[level][b - (1 << level)]); n   } n }; n template <typename T_string> n struct suffix_array { n   int n = 0; n   T_string str; n   vector<int> suffix; n   vector<int> rank; n   vector<int> lcp; n   RMQ<int> rmq; n   suffix_array() {} n   suffix_array(const T_string &_str, bool build_rmq = true) { n     build(_str, build_rmq); n   } n   void build(const T_string &_str, bool build_rmq = true) { n     str = _str; n     n = str.size(); n     suffix.resize(n); n     for (int i = 0; i < n; i++) suffix[i] = i; n     bool large_alphabet = false; n     for (int i = 0; i < n; i++) n       if (str[i] < 0 || str[i] >= 128) large_alphabet = true; n     if (large_alphabet) { n       sort(suffix.begin(), suffix.end(), n            [&](int a, int b) { return str[a] < str[b]; }); n     } else { n       vector<int> freq(128, 0); n       for (int i = 0; i < n; i++) freq[str[i]]++; n       for (int c = 1; c < 128; c++) freq[c] += freq[c - 1]; n       for (int i = 0; i < n; i++) suffix[--freq[str[i]]] = i; n     } n     rank.resize(n); n     rank[suffix[0]] = 0; n     for (int i = 1; i < n; i++) n       rank[suffix[i]] = n           str[suffix[i]] == str[suffix[i - 1]] ? rank[suffix[i - 1]] : i; n     vector<int> next_index(n); n     vector<int> values(n); n     bool done = false; n     for (int len = 1; len < n && !done; len *= 2) { n       for (int i = 0; i < n; i++) next_index[i] = i; n       for (int i = n - len; i < n; i++) values[next_index[rank[i]]++] = i; n       for (int i = 0; i < n; i++) { n         int prev = suffix[i] - len; n         if (prev >= 0) values[next_index[rank[prev]]++] = prev; n       } n       swap(suffix, values); n       values[suffix[0]] = 0; n       done = true; n       for (int i = 1; i < n; i++) { n         int s = suffix[i], prev = suffix[i - 1]; n         if (s + len < n && prev + len < n && rank[s] == rank[prev] && n             rank[s + len] == rank[prev + len]) { n           values[s] = values[prev]; n           done = false; n         } else { n           values[s] = i; n         } n       } n       swap(rank, values); n     } n     compute_lcp(); n     if (build_rmq) rmq.build(lcp); n   } n   void compute_lcp() { n     lcp.assign(n, 0); n     int match = 0; n     for (int i = 0; i < n; i++) { n       if (rank[i] == 0) continue; n       int a = suffix[rank[i]] + match; n       int b = suffix[rank[i] - 1] + match; n       while (a < n && b < n && str[a++] == str[b++]) match++; n       lcp[rank[i]] = match; n       match = max(match - 1, 0); n     } n   } n   int get_lcp_from_ranks(int a, int b) const { n     if (a == b) return n - suffix[a]; n     if (a > b) swap(a, b); n     return rmq.query_value(a + 1, b + 1); n   } n   int get_lcp(int a, int b) const { n     if (a >= n || b >= n) return 0; n     if (a == b) return n - a; n     return get_lcp_from_ranks(rank[a], rank[b]); n   } n   int compare(int a, int b, int length = -1) const { n     if (length < 0) length = n; n     if (a == b) return 0; n     int common = get_lcp(a, b); n     if (common >= length) return 0; n     if (a + common >= n || b + common >= n) return a + common >= n ? -1 : 1; n     return str[a + common] < str[b + common] n                ? -1 n                : (str[a + common] == str[b + common] ? 0 : 1); n   } n }; n string str; n suffix_array<string> SA; n vector<long long> prefix_sum; n int search_index(int start, int end, int i, int character) { n   int low = start, high = end; n   while (low < high) { n     int mid = (low + high) / 2; n     if (str[SA.suffix[mid] + i] >= character) n       high = mid; n     else n       low = mid + 1; n   } n   return low; n } n long long compute_substring_count(int i, int start, int end) { n   return prefix_sum[end] - prefix_sum[start] - (long long)i * (end - start); n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   long long K; n   cin >> str >> K; n   int N = str.size(); n   if (K > (long long)N * (N + 1) / 2) { n     cout <<  No such line.  <<   n ; n     return 0; n   } n   assert(1 <= K && K <= (long long)N * (N + 1) / 2); n   SA.build(str); n   prefix_sum.assign(N + 1, 0); n   for (int i = 0; i < N; i++) n     prefix_sum[i + 1] = prefix_sum[i] + N - SA.suffix[i]; n   int start = 0, end = N; n   string substring =   ; n   for (int i = 0; i < N; i++) { n     if (SA.suffix[start] + i == N) start++; n     int low = start, high = end; n     while (low < high) { n       int mid = (low + high) / 2; n       long long additional = compute_substring_count(i, start, mid); n       if (additional >= K) n         high = mid; n       else n         low = mid + 1; n     } n     assert(low > start); n     int c = str[SA.suffix[low - 1] + i]; n     substring += c; n     int c_start = search_index(start, low, i, c); n     K -= compute_substring_count(i, start, c_start); n     start = c_start; n     end = search_index(low, end, i, c + 1); n     K -= end - start; n     if (K <= 0) break; n   } n   cout << substring <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int mod = 1e4 + 7; n const long long INF = 1e18; n const int maxn = 6e5 + 5; n int n, m, k; n struct edge { n   int to, cost; n   edge() {} n   edge(int to, int cost) : to(to), cost(cost) {} n }; n vector<edge> G[maxn]; n int d[maxn]; n pair<int, int> p[maxn]; n map<pair<int, int>, int> mp; n int dx[4] = {0, 0, 1, -1}; n int dy[4] = {1, -1, 0, 0}; n void dijkstra(int s) { n   priority_queue<pair<int, int>, vector<pair<int, int> >, n                  greater<pair<int, int> > > n       q; n   fill(d, d + maxn, 0x7fffffff); n   d[s] = 0; n   q.push(pair<int, int>(0, s)); n   while (!q.empty()) { n     pair<int, int> p = q.top(); n     q.pop(); n     int v = p.second; n     if (d[v] < p.first) continue; n     for (int i = 0; i < G[v].size(); i++) { n       edge e = G[v][i]; n       if (d[e.to] > d[v] + e.cost) { n         d[e.to] = d[v] + e.cost; n         q.push(pair<int, int>(d[e.to], e.to)); n       } n     } n   } n } n int main() { n   cin >> n >> m >> k; n   int dd = 1e4 + 5; n   mp.clear(); n   for (int i = 1; i <= k; i++) { n     int r, c; n     scanf( %d%d , &r, &c); n     G[i].push_back(edge(r + dd, 1)); n     G[i].push_back(edge(c + dd * 2, 1)); n     G[i].push_back(edge(r + 1 + dd, 1)); n     G[i].push_back(edge(r - 1 + dd, 1)); n     G[i].push_back(edge(c + dd * 2 + 1, 1)); n     G[i].push_back(edge(c + dd * 2 - 1, 1)); n     G[r + dd].push_back(edge(i, 0)); n     G[c + dd * 2].push_back(edge(i, 0)); n     G[r + 1 + dd].push_back(edge(i, 0)); n     G[r - 1 + dd].push_back(edge(i, 0)); n     G[c + dd * 2 + 1].push_back(edge(i, 0)); n     G[c + dd * 2 - 1].push_back(edge(i, 0)); n     p[i] = pair<int, int>(r, c); n     mp[pair<int, int>(r, c)] = i; n   } n   for (int i = 1; i <= k; i++) { n     for (int j = 0; j < 4; j++) { n       int x = p[i].first + dx[j]; n       int y = p[i].second + dy[j]; n       if (mp.find(pair<int, int>(x, y)) != mp.end()) n         G[i].push_back(edge(mp[pair<int, int>(x, y)], 0)); n     } n   } n   if (mp.find(pair<int, int>(n, m)) == mp.end()) { n     G[n + dd].push_back(edge(k + 1, 0)); n     G[m + 2 * dd].push_back(edge(k + 1, 0)); n   } n   int ans; n   dijkstra(mp[pair<int, int>(1, 1)]); n   if (mp.find(pair<int, int>(n, m)) == mp.end()) { n     ans = d[k + 1]; n   } else n     ans = d[mp[pair<int, int>(n, m)]]; n   if (ans == 0x7fffffff) ans = -1; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   int a, b, c; n   int f1, f2, s1, s2, t1, t2; n   cin >> f1 >> f2 >> s1 >> s2 >> t1 >> t2; n   int sum1 = f1 + s1 + t1; n   a = f1; n   b = s1; n   c = t1; n   n -= sum1; n   if (n > 0) { n     f2 = f2 - f1; n     if (f2 < n) { n       a += f2; n       s2 = s2 - s1; n       n -= f2; n       if (s2 < n) { n         b += s2; n         t2 = t2 - t1; n         n -= s2; n         if (t2 < n) n           c += t2; n         else n           c += n; n       } else n         b += n; n     } else n       a += n; n   } n   cout << a <<     << b <<     << c << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int a[105]; n int main() { n   int n, c, max = 0, t; n   scanf( %d%d , &n, &c); n   for (int i = 0; i < n; i++) scanf( %d , &a[i]); n   for (int i = 0; i < n - 1; i++) { n     t = a[i] - c; n     if (t < 0) n       continue; n     else { n       t -= a[i + 1]; n       if (t > max) max = t; n     } n   } n   printf( %d n , max); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 1e5 + 10; n int main() { n   int a, b, c; n   while (cin >> a >> b >> c) { n     int s[MAX] = {0}; n     s[a]++; n     s[b]++; n     s[c]++; n     if (s[1] >= 1 || s[2] >= 2 || s[3] >= 3 || (s[2] == 1 && s[4] == 2)) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1000000000; n const int MAX = 100005; n const int RIGHT = 131072; n const int SIZE = 263000; n double L[MAX], R[MAX], T[SIZE]; n int X[MAX], H[MAX], N, M; n pair<int, int> Mush[MAX]; n void update(int l, int r, double p, int n, int a, int b) { n   if (a > r || b < l) return; n   if (a >= l && b <= r) { n     T[n] *= p; n     return; n   } n   int mid = (a + b) / 2; n   update(l, r, p, 2 * n, a, mid); n   update(l, r, p, 2 * n + 1, mid + 1, b); n } n double query(int x, int n, int a, int b) { n   if (a > x || b < x) return 1.0; n   if (a == b) return T[n]; n   int mid = (a + b) / 2; n   return T[n] * query(x, 2 * n, a, mid) * query(x, 2 * n + 1, mid + 1, b); n } n int main() { n   scanf( %d%d , &N, &M); n   for (int i = (1); i <= (N); i++) n     scanf( %d%d%lf%lf , &X[i], &H[i], &L[i], &R[i]); n   for (int i = (1); i <= (N); i++) L[i] /= 100.0, R[i] /= 100.0; n   for (int i = (1); i <= (M); i++) n     scanf( %d%d , &Mush[i].first, &Mush[i].second); n   sort(Mush + 1, Mush + 1 + M); n   for (int i = (1); i <= (SIZE - 1); i++) T[i] = 1.0; n   for (int i = (1); i <= (N); i++) { n     int a, b; n     a = lower_bound(Mush + 1, Mush + M + 1, make_pair(X[i] - H[i], 0)) - Mush; n     b = lower_bound(Mush + 1, Mush + M + 1, make_pair(X[i], 0)) - Mush - 1; n     update(a, b, 1.0 - L[i], 1, 1, RIGHT); n     a = upper_bound(Mush + 1, Mush + M + 1, make_pair(X[i], 1000000005)) - Mush; n     b = upper_bound(Mush + 1, Mush + M + 1, n                     make_pair(X[i] + H[i], 1000000005)) - n         Mush - 1; n     update(a, b, 1.0 - R[i], 1, 1, RIGHT); n   } n   double ans = 0; n   for (int i = (1); i <= (M); i++) n     ans += query(i, 1, 1, RIGHT) * Mush[i].second; n   cout << setprecision(15) << fixed << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = (int)1e5 + 5, mod = (int)0; n int best[N], q[N]; n int main() { n   string s; n   cin >> s; n   int n = (int)s.size(); n   best[n] = 1e9; n   for (int j = n - 1; j >= 0; --j) { n     s[j] -=  a ; n     best[j] = min(best[j + 1], (int)s[j]); n   } n   string res =   ; n   int t = 0; n   for (int j = 0; j < n; ++j) { n     q[t++] = s[j]; n     while (t > 0 && q[t - 1] <= best[j + 1]) res += char(q[t - 1] +  a ), --t; n   } n   cout << res << endl; n } n


#include <bits/stdc++.h> n using namespace std; n string nb; n int main() { n   int N; n   cin >> N; n   cin >> nb; n   string m; n   m.append(N,  9 ); n   string act = nb; n   for (int shift = 0; shift < N; shift++) { n     for (int p = 0; p <= 9; p++) { n       m = min(m, act); n       for (int i = 0; i < N; i++) act[i] = ((act[i] -  0  + 1) % 10 +  0 ); n     } n     char first = act[0]; n     for (int i = 1; i < N; i++) act[i - 1] = act[i]; n     act[N - 1] = first; n   } n   cout << m << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m, l, a[100001], i, t = 0, p, d, k, w; n   cin >> n >> m >> l; n   for (i = 0; i < n; i++) cin >> a[i]; n   if (a[0] > l) t++; n   for (i = 1; i < n; i++) { n     if (a[i] > l && a[i - 1] <= l) t++; n   } n   for (i = 0; i < m; i++) { n     cin >> k; n     if (k == 0) { n       cout << t << endl; n     } else { n       cin >> p >> d; n       w = a[p - 1]; n       a[p - 1] += d; n       if (p == 1) { n         if (a[p] <= l && a[p - 1] > l && w <= l) t++; n       } else if (p == n) { n         if (a[p - 2] <= l && a[p - 1] > l && w <= l) t++; n       } else { n         if (a[p - 1] > l && a[p - 2] <= l && a[p] <= l && w <= l) n           t++; n         else if (a[p - 1] > l && a[p - 2] > l && a[p] > l && w <= l) n           t--; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string b, b1, b2, e1, e2, e; n   cin >> b1 >> b2 >> e1 >> e2; n   swap(b2[0], b2[1]); n   swap(e2[0], e2[1]); n   b = b1 + b2; n   e = e1 + e2; n   b.erase(b.find( X ), 1); n   e.erase(e.find( X ), 1); n   if ((b + b).find(e) != string::npos) { n     cout <<  YES ; n   } else n     cout <<  NO ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct EDGE { n   int v, next; n }; n EDGE w[222]; n long long f[1 << 17], Y; n int ans[22], ww[22], visit[22], N, M, W = 0; n long long work() { n   memset(f, 0, sizeof(f)); n   f[0] = 1; n   for (int i = 0; i < 1 << N; ++i) n     if (f[i]) { n       if (i == 3) int asd = 1; n       int u = 0; n       for (int ii = i; ii; ii >>= 1) u += (ii & 1); n       u++; n       int sta = 1, end = N; n       if (visit[u]) sta = end = visit[u]; n       for (int j = sta; j <= end; ++j) n         if (!(i & (1 << j - 1))) { n           bool flag = 1; n           for (int k = ww[j]; k && flag; k = w[k].next) n             if (i & (1 << w[k].v - 1)) flag = 0; n           if (!flag) continue; n           f[i | (1 << j - 1)] += f[i]; n         } n     } n   return f[(1 << N) - 1]; n } n int main() { n   scanf( %d%I64d%d , &N, &Y, &M); n   Y -= 2000; n   for (int i = 1, u, v; i <= M; ++i) { n     scanf( %d%d , &u, &v); n     w[++W].v = v; n     w[W].next = ww[u]; n     ww[u] = W; n   } n   if (work() < Y) { n     puts( The times have changed ); n     exit(0); n   } n   for (int i = 1; i <= N; ++i) { n     for (int j = 1; j <= N; ++j) n       if (!visit[j]) { n         visit[j] = i; n         long long now = work(); n         if (now >= Y) { n           ans[i] = j; n           break; n         } n         Y -= now; n         visit[j] = 0; n       } n   } n   for (int i = 1; i <= N; ++i) printf( %d%c , ans[i], (i == N ?   n  :    )); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t; n   cin >> t; n   bool flag; n   while (t--) { n     int n; n     cin >> n; n     long long int a[n]; n     for (int i = 0; i < n; i++) cin >> a[i]; n     flag = true; n     sort(a, a + n); n     long long int k = a[0] * a[n - 1], tmp; n     for (int i = 0; i < n; i++) { n       tmp = a[i] * a[n - 1 - i]; n       if (tmp != k) flag = false; n     } n     int ptr = 0; n     if (flag) { n       for (int i = 2; i <= sqrt(k); i++) { n         if (k % i == 0) { n           if (a[ptr] == i) n             ptr++; n           else { n             flag = false; n             break; n           } n         } n       } n     } n     if (flag) n       cout << k << endl; n     else n       cout << -1 << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n using VV = vector<vector<T>>; n template <class T> n inline T SMIN(T& a, const T b) { n   return a = (a > b) ? b : a; n } n template <class T> n inline T SMAX(T& a, const T b) { n   return a = (a < b) ? b : a; n } n template <class Iter> n void dumpc(Iter begin, Iter end) { n   if (false) { n     for (; begin != end; ++begin) { n       cerr << *begin <<  , ; n     } n     cerr << endl; n   } n } n int Q; n map<int, int> comp; n int getid(int a) { n   if (comp.count(a)) return comp[a]; n   comp[a] = int((comp).size()); n   return comp[a]; n } n struct UF { n  public: n   int groupNum; n   vector<int> X; n   UF(int N) { n     uf = vector<int>(N); n     rank = vector<int>(N); n     X = vector<int>(N); n     for (int i = 0; i < N; ++i) uf[i] = i, rank[i] = 0; n     groupNum = N; n   } n   pair<int, int> find(int v) { n     int cur = 0; n     while (uf[v] != v) { n       cur ^= X[v]; n       v = uf[v]; n     } n     return {v, cur}; n   } n   bool same(int x, int y) { return find(x) == find(y); } n   void unite(int x, int y, int z) { n     auto px = find(x); n     auto py = find(y); n     if (px.first == py.first) return; n     groupNum--; n     x = px.first, y = py.first; n     z = px.second ^ py.second ^ z; n     if (rank[x] == rank[y]) { n       uf[x] = y; n       X[x] = z; n       rank[y]++; n       return; n     } n     if (rank[x] < rank[y]) { n       uf[x] = y; n       X[x] = z; n     } else { n       uf[y] = x; n       X[y] = z; n       if (rank[x] == rank[y]) ++rank[x]; n     } n   } n  n  private: n   vector<int> rank; n   vector<int> uf; n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> Q; n   UF uf(2 * Q + 10); n   int last = 0; n   while (Q--) { n     int t, l, r; n     cin >> t >> l >> r; n     l ^= last, r ^= last; n     if (l > r) swap(l, r); n     ++r; n     if (false) { n       cerr <<  l  n            <<   =   << (l) <<  ,   n            <<  r  n            <<   =   << (r) <<  ,   n            <<  getid(l)  n            <<   =   << (getid(l)) <<  ,   n            <<  getid(r)  n            <<   =   << (getid(r)) << endl; n     }; n     l = getid(l), r = getid(r); n     if (t == 1) { n       int x; n       cin >> x; n       x ^= last; n       uf.unite(l, r, x); n     } else { n       pair<int, int> p1 = uf.find(l), p2 = uf.find(r); n       if (false) { n         cerr <<  p1.first  n              <<   =   << (p1.first) <<  ,   n              <<  p2.first  n              <<   =   << (p2.first) << endl; n       }; n       if (p1.first != p2.first) { n         last = 1; n         fprintf(stdout,  %d , -1), putchar(  n ); n         ; n       } else { n         if (false) { n           cerr <<  Q  n                <<   =   << (Q) <<  ,   n                <<  p1.second  n                <<   =   << (p1.second) <<  ,   n                <<  p2.second  n                <<   =   << (p2.second) << endl; n         }; n         last = p1.second ^ p2.second; n         fprintf(stdout,  %d , last), putchar(  n ); n         ; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MOD = 1e9 + 7; n inline void add(long long &a, long long b) { n   a += b; n   a %= MOD; n } n inline void sub(long long &a, long long b) { n   a -= b; n   a %= MOD; n } n inline long long mul(long long a, long long b) { n   return (long long)a * b % MOD; n } n const int N = 1e6 + 10; n struct pnt { n   long long x, y; n   pnt(long long _x = 0, long long _y = 0) : x(_x), y(_y) {} n   inline pnt operator+(const pnt &p) const { return pnt(x + p.x, y + p.y); } n   inline pnt operator-(const pnt &p) const { return pnt(x - p.x, y - p.y); } n   inline pnt &operator+=(const pnt &p) { n     add(x, p.x), add(y, p.y); n     return *this; n   } n   inline pnt &operator-=(const pnt &p) { n     sub(x, p.x), sub(y, p.y); n     return *this; n   } n   inline pnt operator*(const int &k) const { return pnt(x * k, y * k); } n   inline pnt operator/(const int &k) const { return pnt(x / k, y / k); } n   inline long long operator*(const pnt &p) const { return x * p.y - y * p.x; } n   inline long long operator^(const pnt &p) const { return x * p.x + y * p.y; } n   void out() { printf( (%lld,%lld)  , x, y); } n } p[3 * N]; n long long get(int i, int j, int k) { return (p[j] - p[i]) * (p[k] - p[j]); }; n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < (int)(n); i++) cin >> p[i].x >> p[i].y; n   long long s = 0; n   for (int i = 0; i < (int)(n); i++) s += p[i] * p[(i + 1) % n]; n   if (s < 0) { n     s = -s; n     reverse(p, p + n); n   } n   for (int i = 0; i < (int)(n); i++) p[i + 2 * n] = p[i + n] = p[i]; n   struct Calc { n     long long sum, s; n     pnt diff, diff2; n     Calc() : sum(0), s(0) {} n     void inc_r(int l, int r) { n       s += get(l, r, r + 1); n       assert(s >= 0); n       add(sum, s); n       diff2 += p[r + 1] - p[r]; n       diff -= diff2; n     } n     void inc_l(int l, int r) { n       s -= get(l, l + 1, r); n       pnt dp = p[l + 1] - p[l]; n       diff2 -= dp; n       diff += dp * (r - l); n       add(sum, dp * diff); n     } n   } c1, c2; n   long long res = 0; n   int r1 = 0, r2 = 0; n   for (int l = 0; l < (int)(n); l++) { n     while (r1 - l < n && abs(c1.s + get(l, r1, r1 + 1)) <= s / 2) { n       c1.inc_r(l, r1++); n     } n     assert(r1 - l <= n - 2); n     while (r2 - l < n - 2) c2.inc_r(l, r2++); n     long long mid = (2 * c1.s == s ? s / 2 : 0LL); n     long long big = c2.sum - c1.sum; n     long long small = (c1.sum - mid); n     add(res, big - small); n     c1.inc_l(l, r1); n     c2.inc_l(l, r2); n   } n   cout << ((res) % MOD + MOD) % MOD << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, l[1000005], dp[1000005][2][2]; n char s[1000005]; n void Calc(int k) { n   if (s[k] ==  0 ) n     dp[k][0][0] = 1; n   else if (s[k] ==  1 ) n     dp[k][1][1] = 1; n   else { n     dp[k][1][0] = 1; n     dp[k][0][1] = 1; n   } n } n void Change(int x, int L, int R, int O) { n   int tmp[2][2] = {0}; n   for (int a = 0; a < 2; a++) n     for (int b = 0; b < 2; b++) n       if (dp[L][a][b]) n         for (int c = 0; c < 2; c++) n           for (int d = 0; d < 2; d++) n             if (dp[R][c][d]) n               if (s[O] ==  & ) n                 tmp[a & c][b & d] = 1; n               else if (s[O] ==  | ) n                 tmp[a | c][b | d] = 1; n               else n                 tmp[a ^ c][b ^ d] = 1; n   for (int a = 0; a < 2; a++) n     for (int b = 0; b < 2; b++) dp[x][a][b] = tmp[a][b]; n } n int Dfs(int l) { n   int r = l + 1, L = -1, O = -1; n   while (s[r] !=  ) ) { n     if ((s[r] ==  ?  || s[r] ==  0  || s[r] ==  1 ) && L == -1) n       Calc(r), L = r, O = r + 1; n     if (s[r] ==  ( ) r = Dfs(r); n     r++; n     if (r - l >= 3 && s[r] !=  ( ) { n       if (s[r] ==  ?  || s[r] ==  1  || s[r] ==  0 ) { n         Calc(r); n         if (L != -1) Change(r, L, r, O); n         L = r; n         O = r + 1; n         r++; n       } else { n         if (L != -1) Change(r - 1, L, r - 1, O); n         L = r - 1; n         O = r; n       } n       O = r; n     } n   } n   for (int a = 0; a < 2; a++) n     for (int b = 0; b < 2; b++) dp[r][a][b] = dp[r - 1][a][b]; n   return r; n } n int main() { n   scanf( %d , &n); n   scanf( %s , &s); n   int n = strlen(s) - 1, flag = 0; n   for (int i = 0; i <= n; i++) n     if (s[i] ==  ? ) flag = 1; n   if (n == 0) { n     if (s[0] ==  0  || s[0] ==  1 ) n       puts( NO ); n     else n       puts( YES ); n     return 0; n   } n   Dfs(0); n   if (((dp[n][0][1] || dp[n][1][0]) && flag) || n == 0) n     puts( YES ); n   else n     puts( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e6 + 10, mod = 1e9 + 7; n int rd() { n   int x = 0, w = 1; n   char ch = 0; n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) w = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + (ch ^ 48); n     ch = getchar(); n   } n   return x * w; n } n int prm[N], pm[N], tt, n, a[N], c[N][2]; n bool v[N]; n int main() { n   for (int i = 2; i <= N - 5; ++i) { n     if (!pm[i]) pm[i] = prm[++tt] = i; n     for (int j = 1; i * prm[j] <= N - 5; ++j) { n       pm[i * prm[j]] = prm[j]; n       if (i % prm[j] == 0) break; n     } n   } n   n = rd(); n   for (int i = 1; i <= n; ++i) a[i] = rd(); n   sort(a + 1, a + n + 1), reverse(a + 1, a + n + 1); n   for (int i = 1; i <= n; ++i) { n     if (!c[a[i]][0]) n       v[i] = 1, c[a[i]][0] = c[a[i]][1] = 1; n     else { n       int x = a[i] - 1; n       while (x > 1) { n         int np = pm[x], cn = 0; n         while (pm[x] == np) ++cn, x /= np; n         if (c[np][0] < cn) n           c[np][0] = cn, c[np][1] = 1; n         else n           c[np][1] += c[np][0] == cn; n       } n     } n   } n   int ans = 1; n   for (int i = 2; i <= N - 5; ++i) n     for (int j = 1; j <= c[i][0]; ++j) ans = 1ll * ans * i % mod; n   for (int i = 1; i <= n; ++i) n     if (!v[i]) { n       int x = a[i] - 1, o = 1; n       while (x > 1) { n         int np = pm[x], cn = 0; n         while (pm[x] == np) ++cn, x /= np; n         o &= cn < c[np][0] || c[np][1] > 1; n       } n       if (o) { n         ans = (ans + o) % mod; n         break; n       } n     } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> edges[(200 * 1000 + 5)]; n vector<int> f; n vector<int> a; n int dp[(200 * 1000 + 5)][5]; n void read_data() { n   cin >> n; n   int u, v; n   for (int i = 1; i < n; i++) { n     scanf( %d %d , &u, &v); n     edges[u].push_back(v); n     edges[v].push_back(u); n   } n } n void bfs() { n   int qf, ql; n   a.resize(n); n   f.resize(n + 1); n   vector<int> isv; n   isv.resize(n + 1); n   qf = ql = 0; n   a[ql++] = 1; n   isv[1] = true; n   while (qf != ql) { n     int u = a[qf++]; n     for (auto &v : edges[u]) n       if (!isv[v]) { n         f[v] = u; n         isv[v] = true; n         a[ql++] = v; n       } n   } n } n void deal() { n   int sum[5]; n   for (int i = n - 1; i > 0; i--) { n     int u = a[i]; n     if (edges[u].size() == 1 && u != 1) { n       memset(dp[u], 0, sizeof(dp[u])); n       dp[u][0] = 1; n       dp[u][1] = 0; n       dp[u][2] = 1; n     } else { n       memset(sum, 0, sizeof(sum)); n       for (auto &v : edges[u]) n         if (v != f[u]) { n           sum[2] += dp[v][2]; n           sum[1] += dp[v][1]; n         } n       memset(dp[u], 0xff, sizeof(dp[u])); n       dp[u][1] = sum[2]; n       dp[u][0] = sum[1] + 1; n       for (auto &v : edges[u]) n         if (v != f[u]) { n           if (dp[u][2] == -1) { n             dp[u][2] = sum[2] - dp[v][2] + dp[v][0]; n           } else { n             dp[u][2] = min(dp[u][2], sum[2] - dp[v][2] + dp[v][0]); n           } n         } n       dp[u][2] = min(dp[u][2], dp[u][0]); n       dp[u][1] = min(dp[u][1], dp[u][2]); n     } n   } n   int ans = 0; n   for (auto &v : edges[1]) ans += dp[v][0]; n   ans -= edges[1].size(); n   cout << ans << endl; n } n int main() { n   read_data(); n   bfs(); n   deal(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void _print(int32_t t) { cerr << t; } n void _print(int64_t t) { cerr << t; } n void _print(string t) { cerr << t; } n void _print(char t) { cerr << t; } n void _print(long double t) { cerr << t; } n void _print(double t) { cerr << t; } n void _print(unsigned long long t) { cerr << t; } n template <class T, class V> n void _print(pair<T, V> p); n template <class T> n void _print(vector<T> v); n template <class T> n void _print(set<T> v); n template <class T, class V> n void _print(map<T, V> v); n template <class T> n void _print(multiset<T> v); n template <class T, class V> n void _print(pair<T, V> p) { n   cerr <<  { ; n   _print(p.first); n   cerr <<  , ; n   _print(p.second); n   cerr <<  } ; n } n template <class T> n void _print(vector<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(set<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(multiset<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T, class V> n void _print(map<T, V> v) { n   cerr <<  [  ; n   for (auto i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n int64_t find(int64_t mid) { n   int64_t ans = 0; n   while (mid != 0) { n     mid /= 2; n     ans++; n   } n   return ans; n } n void solve() { n   int64_t n, k; n   cin >> n >> k; n   int64_t low = 1, high = 1; n   for (int64_t i = 0; i < n - 1; i += 1) { n     high *= 2; n     high++; n   } n   while (low <= high) { n     int64_t mid = (low + high) / 2; n     if (mid == k) { n       cout << find(high - low + 1); n       return; n     } n     if (mid > k) { n       high = mid - 1; n     } n     if (mid < k) low = mid + 1; n   } n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int64_t t = 1; n   while (t--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> s[100005]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, m, i, j, a, b; n   cin >> n >> m; n   if (n == 1 && m == 1) { n     cout <<  YES n  << 1; n   } else if (n == 1 && m == 2) { n     cout <<  NO ; n   } else if (n == 2 && m == 1) { n     cout <<  NO ; n   } else if (n == 1 && m == 3) { n     cout <<  NO ; n   } else if (n == 3 && m == 1) { n     cout <<  NO ; n   } else if (n == 1 && m == 4) { n     cout <<  YES n ; n     cout <<  2 4 1 3 ; n   } else if (n == 4 && m == 1) { n     cout <<  YES n ; n     cout <<  2 n4 n1 n3 ; n   } else if (n == 2 && m == 2) { n     cout <<  NO ; n   } else if ((n == 2 && m == 3) || (n == 3 && m == 2)) { n     cout <<  NO ; n   } else if (n == 3 && m == 3) { n     cout <<  YES n ; n     cout <<  6 1 8 n7 5 3 n2 9 4 ; n   } else if (n == 1 && m >= 5) { n     vector<int> res; n     for (i = 1; i <= m; i += 2) { n       res.push_back(i); n     } n     for (i = 2; i <= m; i += 2) { n       res.push_back(i); n     } n     cout <<  YES n ; n     for (i = 0; i < m; i++) { n       cout << res[i] <<    ; n     } n   } else if (n >= 5 && m == 1) { n     vector<int> res; n     for (i = 1; i <= n; i += 2) { n       res.push_back(i); n     } n     for (i = 2; i <= n; i += 2) { n       res.push_back(i); n     } n     cout <<  YES n ; n     for (i = 0; i < n; i++) { n       cout << res[i] <<   n ; n     } n   } else if (2 <= n && 4 <= m) { n     a = 1; n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         s[i].push_back(a); n         a++; n       } n     } n     for (i = 2; i <= n; i += 2) { n       a = s[i][0]; n       b = s[i][1]; n       for (j = 0; j < m - 2; j++) { n         s[i][j] = s[i][j + 2]; n       } n       s[i][m - 2] = a; n       s[i][m - 1] = b; n     } n     for (i = 1; i < m; i += 2) { n       a = s[1][i]; n       for (j = 1; j <= n - 1; j++) { n         s[j][i] = s[j + 1][i]; n       } n       s[n][i] = a; n     } n     cout <<  YES n ; n     for (i = 1; i <= n; i++) { n       for (j = 0; j < m; j++) { n         cout << s[i][j] <<    ; n       } n       cout <<   n ; n     } n   } else { n     a = 1; n     for (i = 1; i <= n; i++) { n       for (j = 1; j <= m; j++) { n         s[i].push_back(a); n         a++; n       } n     } n     for (i = 1; i < m; i += 2) { n       a = s[1][i]; n       b = s[2][i]; n       for (j = 1; j <= n - 2; j++) { n         s[j][i] = s[j + 2][i]; n       } n       s[n - 1][i] = a; n       s[n][i] = b; n     } n     for (i = 2; i <= n; i += 2) { n       a = s[i][0]; n       for (j = 0; j < m - 1; j++) { n         s[i][j] = s[i][j + 1]; n       } n       s[i][m - 1] = a; n     } n     cout <<  YES n ; n     for (i = 1; i <= n; i++) { n       for (j = 0; j < m; j++) { n         cout << s[i][j] <<    ; n       } n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void printVector(vector<string> &v) { n   for (auto x : v) cout << x <<   n ; n } n void printVector(vector<int> &v) { n   for (auto x : v) cout << x <<    ; n   cout <<   n ; n } n int main() { n   cin.tie(0); n   cout.tie(0); n   ios::sync_with_stdio(0); n   int t; n   t = 1; n   while (t--) { n     int n, k, x; n     cin >> n >> k >> x; n     vector<int> freq(1024, 0); n     vector<int> upd(1024, 0); n     vector<int> calc(1024); n     for (int i = 0; i < 1024; i++) { n       calc[i] = i xor x; n     } n     for (int i = 0; i < n; i++) { n       int a; n       cin >> a; n       freq[a]++; n     } n     for (int i = 0; i < k; i++) { n       int sum = 0; n       for (int j = 0; j < 1024; j++) { n         int s = freq[j]; n         if (sum % 2 == 0) s++; n         s /= 2; n         upd[calc[j]] += s; n         sum += freq[j]; n         freq[j] -= s; n       } n       for (int j = 0; j < 1024; j++) { n         freq[j] += upd[j]; n         upd[j] = 0; n       } n     } n     int mn, mx; n     for (int j = 0; j < 1024; j++) { n       if (freq[j] > 0) { n         mn = j; n         break; n       } n     } n     for (int j = 1023; j >= 0; j--) { n       if (freq[j] > 0) { n         mx = j; n         break; n       } n     } n     cout << mx <<     << mn <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   int len, l, r; n   bool operator<(const node &a) const { return len > a.len; } n }; n int day[200010]; n int m[200010]; n int main() { n   int n, k, c, i, j; n   scanf( %d %d , &n, &k); n   c = 0; n   for (i = 0; i < n; i++) { n     scanf( %d , &day[i]); n     if (day[i] < 0) c++; n   } n   if (c > k) { n     printf( -1 n ); n     return 0; n   } n   int turn = 0; n   bool f = 0; n   priority_queue<node> q; n   for (i = 0; i < n; i++) { n     if (day[i] < 0) { n       m[i] = 1; n       k--; n     } n     if (!f && day[i] < 0) { n       turn++; n       f = 1; n     } n     if (f && day[i] >= 0) { n       turn++; n       f = 0; n     } n   } n   int l = 0, r = 0; n   i = 0; n   while (m[i] == 0 && i < n) i++; n   for (; i < n; i++) { n     if (m[i] == 0) { n       l = i; n       while (m[i] == 0 && i < n) i++; n       r = i - 1; n       node tp; n       tp.l = l, tp.r = r; n       tp.len = r - l + 1; n       q.push(tp); n       i = i - 1; n     } n   } n   int tt = turn, kk = k; n   while (!q.empty()) { n     node x = q.top(); n     q.pop(); n     if (k < x.len && kk < x.len) n       break; n     else { n       if (k >= x.len) { n         k -= x.len; n         if (x.r != n - 1) { n           turn -= 2; n         } else n           turn--; n       } n       if (kk >= x.len) { n         if (x.r != n - 1) { n           kk -= x.len; n           tt -= 2; n         } n       } n     } n   } n   printf( %d n , min(turn, tt)); n } n