#include <bits/stdc++.h> n using namespace std; n void __print(int x) { cout << x; } n void __print(long x) { cout << x; } n void __print(long long x) { cout << x; } n void __print(unsigned x) { cout << x; } n void __print(unsigned long x) { cout << x; } n void __print(unsigned long long x) { cout << x; } n void __print(float x) { cout << x; } n void __print(double x) { cout << x; } n void __print(long double x) { cout << x; } n void __print(char x) { cout <<      << x <<     ; } n void __print(const char *x) { cout <<     << x <<    ; } n void __print(const string &x) { cout <<     << x <<    ; } n void __print(bool x) { cout << (x ?  true  :  false ); } n template <typename T, typename V> n void __print(const pair<T, V> &x) { n   cout <<  { ; n   __print(x.first); n   cout <<  , ; n   __print(x.second); n   cout <<  } ; n } n template <typename T> n void __print(const T &x) { n   int f = 0; n   cout <<  { ; n   for (auto &i : x) cout << (f++ ?  ,  :   ), __print(i); n   cout <<  } ; n } n void _print() { cout <<  ] n ; } n template <typename T, typename... V> n void _print(T t, V... v) { n   __print(t); n   if (sizeof...(v)) cout <<  ,  ; n   _print(v...); n } n const int N = 3e6 + 1; n bool prime[N]; n int Div[N], num[N]; n void sieve(int n) { n   vector<int> is_prime(n + 1, 1); n   for (int i = 2; i * i <= n; i++) n     if (is_prime[i]) n       for (int j = i + i; j <= n; j += i) is_prime[j] = 0; n   int cnt = 1; n   for (int i = 2; i <= n; i++) n     if (is_prime[i]) prime[i] = 1, num[i] = cnt++; n } n void Divisors() { n   for (int i = 1; i < N; i++) { n     for (int j = i + i; j < N; j += i) { n       Div[j] = i; n     } n   } n } n void solve() { n   int n; n   cin >> n; n   vector<int> a(2 * n), ans; n   map<int, int> m; n   for (int i = 0; i <= 2 * n - 1; i++) cin >> a[i], m[a[i]]++; n   sort(a.rbegin(), a.rend()); n   for (int i = 0; i <= 2 * n - 1; i++) { n     if (m[a[i]]) { n       int x = a[i]; n       if (prime[x]) { n         int y = num[x]; n         m[x]--, m[y]--; n         ans.push_back(y); n       } else { n         m[a[i]]--, m[Div[x]]--; n         ans.push_back(x); n       } n     } n   } n   for (auto i : ans) cout << i <<    ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   sieve(N); n   Divisors(); n   int T = 1; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n void exgcd(long long a, long long b, long long& x, long long& y) { n   if (b == 0) { n     x = 1; n     y = 0; n     return; n   } n   exgcd(b, a % b, x, y); n   long long p = x - a / b * y; n   x = y; n   y = p; n } n long long dv(long long a, long long b) { n   if (b == 0) return 8e18; n   long long u = a / b - 2; n   while ((u + 1) * b <= a) ++u; n   return u; n } n long long wo(long long p, long long q, long long k, long long b) { n   if (k < p) return -1; n   k -= p; n   long long ua = min(p, k); n   if (b <= ua) n     ; n   else n     return -1; n   if (n < 900000) { n     for (int w = n; w >= 0; --w) { n       long long u = w + n, t = (k - ua) / u - 2; n       while (u * t < k - ua || t < 0) ++t; n       while (u * t <= k - b) { n         long long A = k - u * t, B = w - A; n         if (b <= A && A <= ua) n           ; n         else n           throw  GG ; n         if (0 <= B && B <= q) return w; n         ++t; n       } n     } n     return -1; n   } n   long long aa = -1; n   for (long long t = 0; n * t <= k; ++t) { n     long long A = t + 1, B = t, C = k - n * t; n     if (C == 0) { n       if (b <= 0) { n         if (t == 0) n           aa = max(aa, q); n         else n           aa = max(aa, 0LL); n       } n       continue; n     } n     long long a0, b0; n     exgcd(A, B, a0, b0); n     a0 *= C; n     b0 *= C; n     if (B == 0) { n       if (b <= a0 && a0 <= p) n         ; n       else n         continue; n     } n     long long k = min(dv(a0 - b, B), dv(q - b0, A)); n     if (a0 - k * B <= ua && b0 + k * A >= 0) n       ; n     else n       continue; n     aa = max(aa, a0 + b0 + k); n   } n   return aa; n } n int main() { n   long long l, r, k; n   cin >> n >> l >> r >> k; n   long long g = r - l + 1; n   if (g <= 0) g += n; n   long long ans = -1; n   ans = max(ans, wo(g, n - g, k, 0)); n   ans = max(ans, wo(g, n - g, k + 1, 1)); n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   ; n   int tt; n   cin >> tt; n   while (tt--) { n     int n; n     cin >> n; n     cout << (1ll * ((n - 1) / 2) * (((n - 1) / 2) + 1) * n              (2 * ((n - 1) / 2) + 1) * 8) / n                 6 n          <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000; n int from[MAXN + 5]; n void Gauss(int n, int d, double a[][MAXN + 5], double *Ans) { n   for (int i = 1; i <= n; i++) from[i] = i; n   for (int i = 1; i <= n; i++) { n     if (!a[i][i]) n       for (int j = i + 1; j <= min(n, i + d); j++) n         if (a[i][j]) { n           for (int k = max(1, i - d); k <= min(n, i + d); k++) n             swap(a[k][i], a[k][j]); n           swap(from[i], from[j]); n           break; n         } n     for (int j = i + 1; j <= min(n, i + 2 * d); j++) { n       double div = a[j][i] / a[i][i]; n       for (int k = i; k <= min(n, i + d); k++) a[j][k] -= a[i][k] * div; n       a[j][n + 1] -= a[i][n + 1] * div; n     } n   } n   for (int i = n; i >= 1; i--) { n     Ans[from[i]] = a[i][n + 1]; n     for (int j = i + 1; j <= min(i + d, n); j++) n       Ans[from[i]] -= Ans[from[j]] * a[i][j]; n     Ans[from[i]] /= a[i][i]; n   } n } n int n, m, x, y; n double a[MAXN + 5][MAXN + 5], Ans[MAXN + 5][MAXN + 5]; n int main() { n   scanf( %d %d %d %d , &n, &m, &x, &y); n   if (m == 1) { n     printf( %.4f n , 2.0 * (n - x)); n     return 0; n   } n   for (int i = n - 1; i >= 1; i--) { n     a[1][1] = 2.0 / 3; n     a[1][2] = -1.0 / 3; n     a[1][m + 1] = 1.0 / 3 * Ans[i + 1][1] + 1; n     for (int j = 2; j <= m - 1; j++) { n       a[j][j - 1] = -1.0 / 4; n       a[j][j] = 3.0 / 4; n       a[j][j + 1] = -1.0 / 4; n       a[j][m + 1] = 1.0 / 4 * Ans[i + 1][j] + 1; n     } n     a[m][m - 1] = -1.0 / 3; n     a[m][m] = 2.0 / 3; n     a[m][m + 1] = 1.0 / 3 * Ans[i + 1][m] + 1; n     Gauss(m, 1, a, Ans[i]); n   } n   printf( %.4f n , Ans[x][y]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void winter_is_coming() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n } n double k, arr[10001]; n int n; n double check(double md, double k) { n   double sum = 0; n   for (int i = 0; i < n; i++) n     if (arr[i] > md) { n       double x = arr[i] - md; n       sum += (x - (x * k / 100)); n     } else if (arr[i] < md) { n       double x = md - arr[i]; n       sum -= x; n     } n   return sum; n } n int main() { n   winter_is_coming(); n   int t = 100; n   double mx = 0; n   cin >> n >> k; n   for (int i = 0; i < n; i++) cin >> arr[i], mx = max(mx, arr[i]); n   double lo, hi, md, ans = 0; n   lo = 0, hi = 1000; n   while (t--) { n     md = (lo + hi) / 2; n     double z = check(md, k); n     if (z >= 0) { n       ans = md; n       lo = md; n     } else n       hi = md; n   } n   cout << fixed << setprecision(9) << ans << endl; n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int maxn = 65; n const int inf = 1e9; n int n, m, r; n int dp[maxn][maxn][maxn]; n int a[maxn][maxn][maxn]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m >> r; n   for (int i = 0; i < m; i++) n     for (int j = 0; j < n; j++) n       for (int k = 0; k < n; k++) cin >> a[i][j][k]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < n; j++) n       for (int k = 0; k < n; k++) dp[i][j][k] = inf; n   for (int i = 0; i < m; i++) n     for (int j = 0; j < n; j++) n       for (int k = 0; k < n; k++) n         for (int z = 0; z < n; z++) { n           a[i][k][z] = min(a[i][j][z] + a[i][k][j], a[i][k][z]); n           dp[0][k][z] = min(dp[0][k][z], a[i][k][z]); n         } n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < n; j++) { n       for (int k = 0; k < n; k++) { n         for (int z = 0; z < n; z++) n           dp[i][k][z] = min(dp[i][k][z], dp[i - 1][k][j] + dp[0][j][z]); n       } n     } n   } n   for (int i = 0; i < r; i++) { n     int s, t, k; n     cin >> s >> t >> k; n     s--, t--; n     k = min(k, n - 2); n     cout << dp[k][s][t] <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx ) n using namespace std; n const double eps = 0.000001; n const long double pi = acos(-1); n const int maxn = 1e7 + 9; n const int mod = 1e9 + 7; n const long long MOD = 1e18 + 9; n const long long INF = 1e18 + 123; n const int inf = 2e9 + 11; n const int mxn = 1e6 + 9; n const int N = 3e5 + 123; n const int PRI = 555557; n const int pri = 997; n int tests = 1; n int n, m, k; n int a[N]; n int now[N]; n string s; n long long cnt[N]; n long long cur, res; n inline void Solve() { n   cin >> n >> k; n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     for (int j = i + i; j <= n; j += i) { n       cnt[j]++; n       ans++; n     } n   } n   for (int i = n; i > 0; i--) { n     now[i] = 1; n   } n   for (int i = 1; i <= n; i++) { n     long long cur = cnt[i]; n     for (int j = i + i; j <= n; j += i) n       if (now[j]) cur++; n     if (ans - cur >= k && cnt[i]) { n       now[i] = 0; n       ans -= cur; n       for (int j = i + i; j <= n; j += i) cnt[j]--; n     } n   } n   if (ans != k) { n     cout <<  No ; n     exit(0); n   } n   for (int i = 1; i <= n; i++) { n     if (now[i]) { n       res++; n     } n   } n   cout <<  Yes n  << res <<   n ; n   for (int i = 1; i <= n; i++) n     if (now[i]) cout << i <<    ; n } n int main() { n   ios_base::sync_with_stdio(0), cin.tie(0); n   while (tests--) { n     Solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:64000000 ) n using namespace std; n const double PI = 2 * acos(0.0); n const double eps = 1e-9; n template <class T> n T jog(T a, T b) { n   return a + b; n } n template <class T> n T bog(T a, T b) { n   return a - b; n } n template <class T> n T gon(T a, T b) { n   return a * b; n } n template <class T> n T sq(T x) { n   return x * x; n } n template <class T> n T gcd(T a, T b) { n   return b == 0 ? a : gcd(b, a % b); n } n template <class T> n T lcm(T a, T b) { n   return (a / gcd(a, b)) * b; n } n template <class T> n T power(T a, T p) { n   T res = 1, x = a; n   while (p) { n     if (p & 1) res = res * x; n     x = x * x; n     p >>= 1; n   } n   return res; n } n template <class T> n T cordinatlenth(T a, T b, T c, T d) { n   return sqrt(sq(a - c) + sq(b - d)); n } n template <class T> n string toString(T n) { n   ostringstream oss; n   oss << n; n   oss.flush(); n   return oss.str(); n } n int toInt(string s) { n   int r = 0; n   istringstream sin(s); n   sin >> r; n   return r; n } n long long bigmod(long long a, long long p, long long mod) { n   long long res = 1, x = a; n   while (p) { n     if (p & 1) res = (res * x) % mod; n     x = (x * x) % mod; n     p >>= 1; n   } n   return res; n } n int diraction1[] = {-1, 0, 0, 1, 1, -1, -1, 1}; n int diraction2[] = {0, -1, 1, 0, 1, -1, 1, -1}; n int horsed1[] = {-2, -2, -1, 1, 2, 2, 1, -1}; n int horsed2[] = {1, -1, -2, -2, -1, 1, 2, 2}; n void Input(); n const int INF = 2147483647; n const long long LINF = 9223372036854775807ll; n bool isok(long long x); n int n, in[100010], tmp[100010]; n int main() { n   Input(); n   return 0; n } n void Input() { n   cin >> n; n   long long sum = 0; n   for (int i = 0; i < n; i++) cin >> in[i], sum += in[i]; n   long long ans = sum / (n - 1); n   ans += (bool)(sum % (n - 1)); n   for (int i = 0; i < n; i++) ans = ans < in[i] ? in[i] : ans; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(2) n #pragma warning(disable : 4996) n using namespace std; n const long long INF = 2147483647; n const double PI = acos(-1); n const int mod = 1e9 + 7; n bool vis[3][120000]; n int main() { n   long long n, q; n   cin >> n >> q; n   long long obstacles = 0; n   for (int j = 1; j <= q; j++) { n     long long x, y; n     cin >> x >> y; n     long long increment = vis[x][y] == 1 ? -1 : 1; n     vis[x][y] = 1 - vis[x][y]; n     long long dx = x == 2 ? 1 : 2; n     for (int i = -1; i <= 1; i++) { n       if (vis[dx][y + i]) obstacles += increment; n     } n     if (obstacles) n       cout <<  No  << endl; n     else n       cout <<  Yes  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1000000; n struct UnionFind { n   int par[MAX_N], ra[MAX_N]; n   void init(int n) { n     for (int(i) = 0; (i) < (n); (i)++) par[i] = i, ra[i] = 0; n   } n   int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); } n   bool same(int x, int y) { return find(x) == find(y); } n   void unite(int x, int y) { n     if ((x = find(x)) != (y = find(y))) { n       if (ra[x] < ra[y]) swap(x, y); n       par[y] = x, ra[x] += (ra[x] == ra[y]); n     } n   } n }; n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n long long A, B; n string a, b; n int pp[1000000][26]; n int sz[1000000]; n UnionFind uf; n int main() { n   scanf( %I64d%I64d , &A, &B); n   cin >> a >> b; n   long long al = a.size(), bl = b.size(); n   long long st = al * bl; n   st /= gcd(al, bl); n   uf.init(al); n   for (int(i) = 0; (i) < (al); (i)++) uf.unite(i, (i + bl) % al); n   for (int(i) = 0; (i) < (al); (i)++) { n     int ns = uf.find(i); n     pp[ns][a[i] -  a ]++; n     sz[ns]++; n   } n   long long ans = 0; n   for (int(i) = 0; (i) < (bl); (i)++) { n     int ns = uf.find(i % al); n     for (int(j) = 0; (j) < (26); (j)++) n       if (j != b[i] -  a ) ans += pp[ns][j]; n   } n   long long kkd = A * al / st; n   cout << ans * kkd << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 10010; n vector<int> v[MAXN]; n int childcnt[MAXN] = {0}; n int main() { n   int n; n   cin >> n; n   for (int i = 1; i < n; i++) { n     int a, b; n     scanf( %d%d , &a, &b); n     v[a].push_back(b); n     v[b].push_back(a); n   } n   int root = 0; n   for (int i = 1; i <= n; i++) { n     if (v[i].size() == 1) { n       root = i; n       break; n     } n   } n   bool vis[MAXN]; n   memset(vis, false, sizeof(vis)); n   queue<int> q; n   q.push(root); n   vis[root] = true; n   int deep = 0; n   long long ans = 0; n   while (!q.empty()) { n     childcnt[deep++] = q.size(); n     queue<int> p; n     while (!q.empty()) { n       int cur = q.front(); n       q.pop(); n       int len = v[cur].size(); n       int cnt = 0; n       for (int i = 0; i < len; i++) { n         if (!vis[v[cur][i]]) { n           vis[v[cur][i]] = true; n           p.push(v[cur][i]); n           cnt++; n         } n       } n       ans += (cnt - 1) * cnt / 2; n     } n     q = p; n   } n   for (int i = 2; i < deep; i++) { n     ans += childcnt[i]; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MOD = 1e9 + 7; n const long long LINF = (long long)1e18; n const long double PI = acos((long double)-1); n const long double EPS = 1e-9; n inline long long gcd(long long a, long long b) { n   long long r; n   while (b) { n     r = a % b; n     a = b; n     b = r; n   } n   return a; n } n inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } n inline long long fpow(long long n, long long k, int p = MOD) { n   long long r = 1; n   for (; k; k >>= 1) { n     if (k & 1) r = r * n % p; n     n = n * n % p; n   } n   return r; n } n inline long long isqrt(long long k) { n   long long r = sqrt(k) + 1; n   while (r * r > k) r--; n   return r; n } n inline long long icbrt(long long k) { n   long long r = cbrt(k) + 1; n   while (r * r * r > k) r--; n   return r; n } n inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } n inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; } n inline int sign(long double x, long double y) { return sign(x - y); } n template <class T> n void dbv(T a) { n   for (auto x : a) { n     cout << x <<   **  ; n   } n   cout << endl; n } n long long calc(long long in, vector<long long> &a) { n   priority_queue<long long> pq; n   long long cum = in; n   long long cnt = 0; n   long long n = a.size(); n   for (int i = (0); i < (n); ++i) { n     pq.push(-a[i]); n     cum += a[i]; n     if (cum < 0) { n       cum += pq.top(); n       pq.pop(); n       cnt++; n     } n   } n   return cnt; n } n void output() { n   long long n, q; n   cin >> n >> q; n   vector<long long> a(n); n   for (int i = (0); i < (n); ++i) { n     cin >> a[i]; n   } n   long long st = calc(0, a); n   vector<long long> lims; n   lims.push_back(0); n   for (int val = (st - 1); val >= (0); --val) { n     long long low = lims[st - val - 1] + 1; n     long long high = LINF; n     while (low < high) { n       long long mid = (low + high) / 2; n       if (calc(mid, a) <= val) n         high = mid; n       else n         low = mid + 1; n     } n     lims.push_back(low); n   } n   lims.push_back(1e18); n   for (int i = (0); i < (q); ++i) { n     long long qu; n     cin >> qu; n     long long low = 0; n     long long high = st + 1; n     while (low < high) { n       long long mid = (low + high) / 2; n       if (qu < lims[mid]) n         high = mid; n       else n         low = mid + 1; n     } n     cout << (st - (low - 1)) << endl; n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long t = 1; n   for (int i = (0); i < (t); ++i) { n     output(); n   } n   return 0; n } n


#include <bits/stdc++.h> 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,tune=native ) n using namespace std; n long long n, k, ans, pw[11], a[200005]; n map<int, int> w[11]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   cout.precision(30); n   cerr.precision(7); n   ; n   cin >> n >> k; n   pw[0] = 1LL; n   for (int i = 1; i <= 10; i++) pw[i] = (pw[i - 1] * 10LL) % k; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n     for (int j = 1; j <= 10; j++) w[j][(a[i] * pw[j]) % k]++; n   } n   for (int i = 1; i <= n; i++) { n     int l = (int)to_string(a[i]).size(); n     ans += w[l][(k - (a[i] % k)) % k]; n     ans -= !(((pw[l] * a[i]) % k + a[i]) % k); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n long long m, n, f[22], inv[22]; n inline long long read() { n   long long s = 0, f = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) { n       f = -1; n     } n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     s = (s << 1) + (s << 3) + c -  0 ; n     c = getchar(); n   } n   return s * f; n } n inline void write(long long x) { n   if (x < 0) { n     x = ~(x - 1); n     putchar( - ); n   } n   if (x > 9) { n     write(x / 10); n   } n   putchar(x % 10 +  0 ); n } n long long power(long long a, long long p) { n   long long res = 1; n   while (p) { n     if (p & 1) res *= a, res %= mod; n     p >>= 1; n     a *= a; n     a %= mod; n   } n   return res; n } n long long c(long long n, long long m) { n   if (n < m || n < 0 || m < 0) { n     return 0; n   } n   n %= mod; n   if (n == 0 || m == 0) { n     return 1; n   } n   long long res = 1; n   for (long long i = 0; i < m; i++) { n     res *= n - i, res %= mod; n   } n   for (long long i = 1; i <= m; i++) { n     res *= inv[i], res %= mod; n   } n   return res; n } n signed main() { n   n = read(), m = read(); n   for (long long i = 1; i <= n; i++) { n     f[i] = read(); n   } n   for (long long i = 1; i <= 20; i++) { n     inv[i] = power(i, mod - 2); n   } n   long long ans = 0; n   for (long long i = 1; i < (1 << n); i++) { n     long long t = n + m - 1, bit = 0; n     for (long long j = 0; j < n; j++) { n       if (i & (1 << j)) { n         t -= f[j + 1] + 1; n         bit++; n       } n     } n     if (bit % 2 == 1) { n       ans = (ans + c(t, n - 1)) % mod; n     } else { n       ans = (ans - c(t, n - 1)) % mod; n     } n   } n   cout << (c(n + m - 1, n - 1) - ans + mod) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long inf = 1000000000; n long n, nn; n long d[40000][100]; n long dem[40000]; n long tong[40000]; n long lon[40000]; n long canh[40000][30][2]; n long res[40000], kq, mot, hai, ans[40000], cans[40000]; n string x, s[40], y; n vector<string> chuoi; n long max(long a, long b) { n   if (a > b) return a; n   return b; n } n long nap(long u, char v) { n   if (d[u][v -  a ]) return d[u][v -  a ]; n   d[u][v -  a ] = ++nn; n   tong[nn] = tong[u] + v -  a  + 1; n   lon[nn] = max(v -  a  + 1, lon[u]); n   return d[u][v -  a ]; n } n void push(string &s) { n   long u = 0; n   for (long i = 0; i <= s.length() - 1; i++) u = nap(u, s[i]); n   dem[u]++; n } n long get(string &s) { n   long u = 0; n   for (long i = 0; i <= s.length() - 1; i++) { n     u = d[u][s[i] -  a ]; n     if (!u) return u; n   } n   return u; n } n long dp(long u) { n   if (res[u] != -inf) return res[u]; n   res[u] = -1; n   for (long t = 0; t <= 1; t++) n     for (long i = 0; i <= 25; i++) n       if (canh[u][i][t]) { n         long v = canh[u][i][t]; n         res[u] = max(dp(v), res[u]); n       } n   res[u] *= -1; n   return res[u]; n } n void qhd(long u) { n   if (ans[u] != -inf) return; n   long cost = tong[u] * lon[u] + dem[u]; n   ans[u] = -1; n   cans[u] = -1; n   for (long t = 0; t <= 1; t++) n     for (long i = 0; i <= 25; i++) n       if (canh[u][i][t]) { n         long v = canh[u][i][t]; n         if (res[u] == res[v]) continue; n         qhd(v); n         if (ans[v] > cans[u]) { n           cans[u] = ans[v]; n           ans[u] = cans[v]; n         } else if (cans[u] == ans[v] && ans[u] > cans[v]) n           ans[u] = cans[v]; n       } n   if (ans[u] == -1) ans[u] = 0; n   if (cans[u] == -1) cans[u] = 0; n   ans[u] += cost; n } n int main() { n   cin >> n; n   for (long i = 1; i <= n; i++) { n     cin >> s[i]; n     chuoi.clear(); n     for (long d = s[i].length(); d >= 1; d--) { n       for (long u = 0; u <= s[i].length() - d; u++) { n         x = s[i].substr(u, d); n         chuoi.push_back(x); n       } n     } n     sort(chuoi.begin(), chuoi.end()); n     push(chuoi[0]); n     for (long i = 1; i <= chuoi.size() - 1; i++) n       if (chuoi[i] != chuoi[i - 1]) push(chuoi[i]); n   } n   for (long i = 1; i <= n; i++) { n     string ss = s[i]; n     for (long d = ss.length() - 1; d >= 1; d--) { n       for (long u = 0; u <= ss.length() - d; u++) { n         long v = u + d - 1; n         x = ss.substr(u, d); n         long t = get(x); n         if (v + 1 < ss.length() && !canh[t][ss[v + 1] -  a ][0]) { n           y = ss.substr(u, d + 1); n           long tt = get(y); n           canh[t][ss[v + 1] -  a ][0] = tt; n         } n         if (u > 0 && !canh[t][ss[u - 1] -  a ][1]) { n           y = ss.substr(u - 1, d + 1); n           long tt = get(y); n           canh[t][ss[u - 1] -  a ][1] = tt; n         } n       } n     } n   } n   for (long i = 1; i <= nn; i++) res[i] = -inf; n   kq = -1; n   for (char c =  a ; c <=  z ; c++) { n     x = c; n     long u = get(x); n     if (!u) continue; n     kq = max(dp(u), kq); n   } n   if (kq == 1) n     cout <<  First  << endl; n   else n     cout <<  Second  << endl; n   for (long i = 1; i <= nn; i++) ans[i] = -inf; n   long tl = -inf; n   long tll; n   for (char c =  a ; c <=  z ; c++) { n     x = c; n     long u = get(x); n     if (!u) continue; n     if (res[u] != kq) continue; n     qhd(u); n     long t = ans[u]; n     if (t > tl) { n       tl = t; n       tll = cans[u]; n     } else if (t == tl && cans[u] < tll) n       tll = cans[u]; n   } n   cout << tl <<     << tll << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n inline void chkmin(T &a, T b) { n   if (a > b) a = b; n } n template <class T> n inline void chkmax(T &a, T b) { n   if (a < b) a = b; n } n double fail[310][3100], p[310][3100], val[310], nf[3100]; n int main() { n   int N, M, i, j, u; n   double res; n   scanf( %d%d , &N, &M); n   for (i = 1; i <= N; i++) n     for (j = 1; j <= M; j++) { n       scanf( %lf , &p[j][i]); n       p[j][i] /= 1000; n     } n   for (i = 1; i <= M; i++) { n     fail[i][0] = 1; n     for (j = 1; j <= N; j++) { n       fail[i][j] = fail[i][j - 1] * (1 - p[i][j]); n     } n     val[i] = 1 - fail[i][N]; n   } n   res = 0; n   for (i = 1; i <= N; i++) { n     u = max_element(val, val + M + 1) - val; n     res += val[u]; n     for (j = 1; j <= N; j++) n       nf[j] = nf[j - 1] * (1 - p[u][j]) + fail[u][j - 1] * p[u][j]; n     memcpy(fail[u], nf, sizeof(nf)); n     val[u] -= nf[N]; n   } n   printf( %.15lf n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int s[1001000]; n int g[1001000]; n int ans[1001000]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> s[i] >> g[i]; n     ans[i] = 1001000; n   } n   ans[0] = s[0] + g[0]; n   for (int i = 0; i < n - 1; i++) { n     ans[i + 1] = min(g[i + 1] + s[i + 1], ans[i] + 1); n   } n   for (int i = n - 1; i > 0; i--) { n     ans[i - 1] = min(ans[i] + 1, ans[i - 1]); n   } n   long long sum = 0; n   for (int i = 0; i < n; i++) { n     sum += ans[i] - s[i]; n     if (ans[i] < s[i]) { n       cout << -1; n       return 0; n     } n   } n   cout << sum << endl; n   for (int i = 0; i < n; i++) { n     cout << ans[i] <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long int N = 2e5 + 5; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); n   long long int x, y, a, b; n   cin >> x >> y >> a >> b; n   vector<pair<long long int, long long int>> mp; n   for (int i = a; i <= x; i++) { n     for (int j = b; j <= y; j++) { n       if (i <= j) n         continue; n       else { n         mp.push_back({i, j}); n       } n     } n   } n   cout << mp.size() << endl; n   for (int i = 0; i < mp.size(); i++) { n     cout << mp[i].first <<     << mp[i].second << endl; n   } n } n


#include <bits/stdc++.h> n char s[100001]; n int main() { n   int n; n   int i, j; n   scanf( %s , s); n   n = strlen(s); n   for (i = 0; i < n; i++) n     if (s[i] ==  0 ) break; n   if (i == n) { n     for (j = 0; j < n - 1; j++) printf( %c , s[j]); n     printf(  n ); n   } else { n     for (j = 0; j < i; j++) printf( %c , s[j]); n     for (j = i + 1; j < n; j++) printf( %c , s[j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 105555; n int a[N]; n int main() { n   int n; n   scanf( %d , &n); n   int mn = 1e7, mi, mx = -1e7, xi; n   for (int i = 0; i < n; ++i) { n     scanf( %d , a + i); n     if (i > 1 && xi < i - 1 && mx > a[i - 1] && a[i - 1] < a[i]) { n       printf( 3 n%d %d %d n , xi + 1, i, i + 1); n       return 0; n     } n     if (i > 1 && mi < i - 1 && mn < a[i - 1] && a[i - 1] > a[i]) { n       printf( 3 n%d %d %d n , mi + 1, i, i + 1); n       return 0; n     } n     if (a[i] > mx) { n       mx = a[i]; n       xi = i; n     } n     if (a[i] < mn) { n       mn = a[i]; n       mi = i; n     } n   } n   puts( 0 ); n } n


#include <bits/stdc++.h> n using namespace std; n set<long long int> blacklist; n long long int par(long long int* parent, long long int f, long long int n) { n   if (parent[f] < 0) return parent[f]; n   if (parent[f] == f) n     return f; n   else { n     parent[f] = par(parent, parent[f], n); n     return parent[f]; n   } n } n void unio(long long int* parent, long long int f, long long int s, n           long long int pf, long long int ps) { n   if (pf < ps) n     parent[ps] = pf; n   else n     parent[pf] = ps; n } n void utilfun(long long int* parent, vector<long long int>* adj, n              pair<long long int, long long int>* edge, long long int n, n              long long int m, long long int a, long long int b) { n   for (long long int i = 0; i < m; i++) { n     long long int f = edge[i].first; n     long long int s = edge[i].second; n     if (f == a || f == b || s == a || s == b) { n       continue; n     } else { n       long long int pf = par(parent, f, n); n       long long int ps = par(parent, s, n); n       unio(parent, f, s, pf, ps); n     } n   } n   long long int ind = -2; n   for (long long int i = 0; i < m; i++) { n     long long int f = edge[i].first; n     long long int s = edge[i].second; n     if ((f == a || s == a) && (s != b && f != b)) { n       long long int ot = f + s - a; n       long long int pot = par(parent, ot, n); n       if (pot >= 0) { n         parent[pot] = ind; n         ind--; n       } n     } n   } n   for (long long int i = 0; i < m; i++) { n     long long int f = edge[i].first; n     long long int s = edge[i].second; n     if ((f == b || s == b) && (s != a && f != a)) { n       long long int ot = f + s - b; n       long long int pot = par(parent, ot, n); n       if (pot == -1) { n         continue; n       } else if (pot < -1) { n         blacklist.insert(pot); n       } else n         parent[pot] = b; n     } n   } n } n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     blacklist.clear(); n     long long int n, m, a, b; n     cin >> n >> m >> a >> b; n     a--; n     b--; n     vector<long long int> adj[n]; n     pair<long long int, long long int> edge[m]; n     long long int parent[n]; n     for (long long int i = 0; i < n; i++) parent[i] = i; n     for (long long int i = 0; i < m; i++) { n       long long int x, y; n       cin >> x >> y; n       x--; n       y--; n       adj[x].push_back(y); n       adj[y].push_back(x); n       edge[i] = {x, y}; n     } n     utilfun(parent, adj, edge, n, m, a, b); n     long long int ac = 0; n     long long int bc = -1; n     set<long long int>::iterator itr; n     for (long long int i = 0; i < n; i++) { n       long long int pot = par(parent, i, n); n       if (blacklist.find(pot) != blacklist.end()) continue; n       if (pot < -1) n         ac++; n       else if (pot == b) n         bc++; n     } n     cout << ac * bc <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     long long int n; n     scanf( %lld , &n); n     printf( %lld n , n); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class TaskC { n  public: n   void solve(std::istream& in, std::ostream& out); n }; n void TaskC::solve(std::istream& in, std::ostream& out) { n   int n; n   in >> n; n   int a[310][310]; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < n; ++j) { n       in >> a[i][j]; n     } n   } n   int k; n   in >> k; n   while (k--) { n     int x, y, c; n     in >> x >> y >> c; n     --x, --y; n     a[x][y] = min(a[x][y], c); n     a[y][x] = min(a[y][x], c); n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < n; ++j) { n         a[i][j] = min(a[i][j], min(a[i][x] + a[x][y] + a[y][j], n                                    a[i][y] + a[y][x] + a[x][j])); n       } n     } n     long long ans = 0; n     for (int i = 0; i < n; ++i) { n       for (int j = 0; j < i; ++j) ans += (long long)a[i][j]; n     } n     out << ans <<    ; n   } n }; n int main() { n   TaskC solver; n   std::istream& in(std::cin); n   std::ostream& out(std::cout); n   solver.solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long int n, k; n   cin >> n >> k; n   vector<long long int> a(n); n   for (long long int i = 0; i < n; i++) cin >> a[i]; n   long long int ans = -1e18; n   for (long long int i = 0; i < n; i++) { n     for (long long int j = i; j < n; j++) { n       vector<long long int> in, out; n       long long int sum = 0; n       for (long long int k = 0; k < n; k++) { n         if (k < i || k > j) n           out.push_back(a[k]); n         else n           in.push_back(a[k]), sum += a[k]; n       } n       sort((in).begin(), (in).end()); n       sort((out).rbegin(), (out).rend()); n       long long int l = 0, r = 0, s = k; n       while (l < (long long int)in.size() && r < (long long int)out.size() && n              s-- > 0) { n         if (in[l] < out[r]) sum += out[r] - in[l]; n         l++, r++; n       } n       ans = max(ans, sum); n     } n   } n   cout << ans <<   n ; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int test_cases = 1; n   while (test_cases--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100010; n const int SIZ = 500; n inline void read(int &x) { n   x = 0; n   int f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) { n       f = -1; n     } n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   x *= f; n } n struct note { n   int t; n   int next; n }; n int cnt; n int head[N]; n note e[N << 1]; n inline void add(int x, int y) { n   e[++cnt].t = y; n   e[cnt].next = head[x]; n   head[x] = cnt; n } n int n, m; n namespace tl { n int cnt; n int id[N], iid[N]; n int fa[N]; n int top[N], son[N], siz[N]; n void dfs1(int p, int f) { n   fa[p] = f; n   siz[p] = 1; n   for (int i = head[p]; i + 1; i = e[i].next) { n     int t = e[i].t; n     if (t == f) { n       continue; n     } n     dfs1(t, p); n     siz[p] += siz[t]; n     if (siz[t] > siz[son[p]]) { n       son[p] = t; n     } n   } n } n void dfs2(int p, int top_p) { n   id[p] = ++cnt; n   iid[cnt] = p; n   top[p] = top_p; n   if (son[p]) { n     dfs2(son[p], top_p); n   } n   for (int i = head[p]; i + 1; i = e[i].next) { n     int t = e[i].t; n     if (t == fa[p] || t == son[p]) { n       continue; n     } n     dfs2(t, t); n   } n } n int bol; n int idb[N]; n int tag[N / SIZ + 10]; n int l[N / SIZ + 10], r[N / SIZ + 10]; n int col[N]; n int sum[N / SIZ + 10][N << 1]; n int t[N]; n int a[N]; n inline void input() { n   for (int i = 1; i <= n; i++) { n     read(t[i]); n     t[i]++; n   } n } n inline void init() { n   dfs1(1, -1); n   dfs2(1, 1); n   for (int i = 1; i <= n; i++) { n     idb[i] = (i - 1) / SIZ + 1; n     if (idb[i] != idb[i - 1]) { n       r[idb[i - 1]] = i - 1; n       l[idb[i]] = i; n     } n   } n   r[idb[n]] = n; n   for (int i = 1; i <= n; i++) { n     a[id[i]] = n - t[i]; n   } n   for (int i = 1; i <= n; i++) { n     sum[idb[i]][a[i]]++; n   } n } n int ans; n inline void update(int l, int r, int val) { n   for (int i = l; i <= r; i++) { n     int o = idb[i]; n     if (!col[i] && a[i] >= n - tag[o]) { n       ans--; n     } n     if (!col[i]) { n       sum[o][a[i]]--; n     } n     a[i] += val; n     if (!col[i]) { n       sum[o][a[i]]++; n     } n     if (!col[i] && a[i] >= n - tag[o]) { n       ans++; n     } n   } n } n inline void modify(int L, int R, int val) { n   int x = idb[L], y = idb[R]; n   if (x == y) { n     update(L, R, val); n     return; n   } n   update(L, r[x], val); n   update(l[y], R, val); n   for (int i = x + 1; i <= y - 1; i++) { n     if (val == 1) { n       tag[i]++; n       ans += sum[i][n - tag[i]]; n     } else { n       ans -= sum[i][n - tag[i]]; n       tag[i]--; n     } n   } n } n inline void query(int x) { n   int val = (x > 0) ? 1 : -1; n   x = abs(x); n   col[id[x]] ^= 1; n   if (a[id[x]] >= n - tag[idb[id[x]]]) { n     ans -= val; n   } n   sum[idb[id[x]]][a[id[x]]] -= val; n   while (x + 1) { n     modify(id[top[x]], id[x], val); n     x = fa[top[x]]; n   } n   printf( %d  , ans); n } n }  // namespace tl n int main() { n   memset(head, -1, sizeof(head)); n   read(n), read(m); n   for (int i = 2; i <= n; i++) { n     int x; n     read(x); n     add(x, i); n     add(i, x); n   } n   tl::input(); n   tl::init(); n   for (int i = 1; i <= m; i++) { n     int x; n     read(x); n     tl::query(x); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int A[1111111]; n int B[1111111]; n int main(void) { n   int alen = 0; n   int blen = 0; n   scanf( %d %d , &alen, &blen); n   for (int i = 0; i < alen; i++) scanf( %d , &A[i]); n   memset(B, -1, sizeof(B)); n   for (int i = 0; i < blen; i++) { n     int x = 0; n     scanf( %d , &x); n     B[x] = i; n   } n   deque<int> Queue; n   int ans = 0; n   for (int i = 0; i < 2 * alen; i++) { n     int where = B[A[i % alen]]; n     if (where != -1) { n       while (Queue.size() >= 2 && n              !((Queue.front() < Queue.back() && n                 (Queue.back() < where || where < Queue.front())) || n                (Queue.front() > Queue.back() && Queue.back() < where && n                 where < Queue.front()))) n         Queue.pop_front(); n       Queue.push_back(where); n     } else n       Queue.clear(); n     ans = max<int>(ans, Queue.size()); n   } n   ans = min(ans, alen); n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n const int N = 3e4 + 10; n long long ri() { n   char c = getchar(); n   long long x = 0, f = 1; n   for (; c <  0  || c >  9 ; c = getchar()) n     if (c ==  - ) f = -1; n   for (; c >=  0  && c <=  9 ; c = getchar()) x = (x << 1) + (x << 3) -  0  + c; n   return x * f; n } n long long len[66]; n int n, d, m, tot, gen[25], b[N]; n std::bitset<N> all; n struct info { n   long long ans, len; n   std::bitset<N> pre, suf; n   info operator+(const info &b) { n     info c; n     c.ans = ans + b.ans; n     c.len = len + b.len; n     c.pre = pre; n     c.suf = b.suf; n     if (len < n - 1) c.pre &= (b.pre << len) | (all >> n - 1 - len); n     if (b.len < n - 1) c.suf &= (suf >> b.len) | (all << n - 1 - b.len); n     if (len + b.len >= n) { n       std::bitset<N> res = suf & b.pre; n       if (len < n - 1) res &= (all << n - 1 - len); n       if (b.len < n - 1) res &= (all >> n - 1 - b.len); n       c.ans += res.count(); n     } n     return c; n   } n   void operator+=(const info &b) { *this = !len ? b : *this + b; } n } f[66][66]; n long long Query(long long x) { n   info res; n   res.ans = res.len = 0; n   int add = 0; n   for (int i = tot; ~i; --i) n     for (int j = 1; j <= d; ++j) { n       if (x >= len[i]) { n         x -= len[i]; n         res += f[i][(add + gen[j]) % m]; n       } else { n         (add += gen[j]) %= m; n         break; n       } n     } n   return res.ans; n } n int main() { n   d = ri(); n   m = ri(); n   for (int i = 1; i <= d; ++i) gen[i] = ri(); n   n = ri(); n   for (int i = 1; i <= n; ++i) b[i] = ri(); n   for (int i = 1; i < n; ++i) all.set(i); n   len[tot = 0] = 1; n   for (int i = 0; i < m; ++i) { n     f[0][i].len = 1; n     if (n == 1) n       f[0][i].ans = i <= b[1]; n     else { n       for (int j = 1; j <= n - 1; ++j) { n         f[0][i].pre[j] = i <= b[n - j + 1]; n         f[0][i].suf[j] = i <= b[n - j]; n       } n     } n   } n   long long L = ri(), R = ri(); n   for (; len[tot] <= R / d;) { n     ++tot; n     len[tot] = len[tot - 1] * d; n     for (int i = 0; i < m; ++i) n       for (int j = 1; j <= d; ++j) f[tot][i] += f[tot - 1][(i + gen[j]) % m]; n   } n   printf( %lld n , Query(R) - Query(L + n - 2)); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Solution { n  public: n   int n, m; n   std::vector<int> r; n   std::vector<std::vector<int>> d; n   std::vector<std::vector<std::array<int, 2>>> v; n   std::vector<bool> visited; n   std::vector<int> val; n   void dfs(int cur, int col) { n     visited[cur] = true; n     val[cur] = col; n     for (auto next : v[cur]) { n       if (visited[next[0]]) continue; n       dfs(next[0], col ^ next[1]); n     } n   } n   void solve(std::istream& in, std::ostream& out) { n     in >> n >> m; n     r.resize(n); n     d.resize(n); n     for (auto& el : r) in >> el; n     for (int i = 0; i < m; ++i) { n       int x; n       in >> x; n       for (int j = 0; j < x; ++j) { n         int a; n         in >> a; n         --a; n         d[a].push_back(i); n       } n     } n     v.resize(m); n     val.resize(m); n     for (int i = 0; i < n; ++i) { n       v[d[i][0]].push_back({d[i][1], !r[i]}); n       v[d[i][1]].push_back({d[i][0], !r[i]}); n     } n     visited.resize(m); n     for (int i = 0; i < m; ++i) { n       if (!visited[i]) { n         dfs(i, 0); n       } n     } n     for (int i = 0; i < n; ++i) n       if ((val[d[i][0]] ^ val[d[i][1]]) != (!r[i])) return void(out <<  NO n ); n     out <<  YES n ; n   } n }; n void solve(std::istream& in, std::ostream& out) { n   out << std::setprecision(12); n   Solution solution; n   solution.solve(in, out); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   istream& in = cin; n   ostream& out = cout; n   solve(in, out); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e5; n long long pref1[N + 5]; n long long pref2[N + 5]; n long long ans; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m, type, l, r; n   cin >> n; n   n++; n   int arr[n]; n   for (int i = 1; i < n; i++) { n     cin >> arr[i]; n     pref1[i] = pref1[i - 1] + arr[i]; n   } n   sort(arr + 1, arr + n); n   for (int i = 1; i < n; i++) pref2[i] = pref2[i - 1] + arr[i]; n   cin >> m; n   while (m--) { n     cin >> type >> l >> r; n     if (type == 1) n       ans = pref1[r] - pref1[l - 1]; n     else n       ans = pref2[r] - pref2[l - 1]; n     cout << ans << endl; n   } n   return 0; n } n


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


#include <bits/stdc++.h> n int main() { n   int t, x, i, z, n, max = 0, s = 0, a[1000], m; n   scanf( %d %d , &n, &m); n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     s += a[i]; n     if (max < a[i]) { n       max = a[i]; n     } n   } n   max = max + m; n   s = s + m; n   if (s % n == 0) { n     x = s / n; n   } else { n     x = (s / n) + 1; n   } n   for (i = 0; i < n; i++) { n     if (x < a[i]) { n       x = a[i]; n     } n   } n   printf( %d %d , x, max); n } n


#include <bits/stdc++.h> n using namespace std; n bool comp(string a, string b) { return (a.length() > b.length()); } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cout.setf(ios::fixed); n   cout.setf(ios::showpoint); n   { n     long long int n, x, y; n     cin >> n >> x >> y; n     long long int a[n]; n     for (long long int i = 0; i < n; i++) cin >> a[i]; n     for (long long int i = 0; i < n; i++) { n       long long int c = 0; n       for (long long int j = max(0LL, i - x); j < i; j++) n         if (a[j] < a[i]) c++; n       for (long long int j = i + 1; j < min(n, i + y + 1); j++) n         if (a[j] < a[i]) c++; n       if (!c) { n         cout << i + 1; n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n vector<int> re; n int main() { n   cin >> n; n   int least[n]; n   for (int i = 0; i < n; i++) cin >> least[i]; n   sort(least, least + n); n   int cont = 0; n   for (int i = 1; i < n; i++) { n     if (least[i] == least[i - 1]) { n       re.push_back(least[i]); n       least[i - 1] = -1; n       cont++; n     } else { n       cont = 0; n     } n     if (cont >= 2) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   sort(re.begin(), re.end()); n   cout <<  YES  << endl; n   cout << n - re.size() << endl; n   for (int i = 0; i < n; i++) { n     if (least[i] == -1) continue; n     cout << least[i] <<    ; n   } n   cout << endl; n   cout << re.size() << endl; n   for (int i = re.size() - 1; i >= 0; i--) cout << re[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void optimise() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n long long int mod = 1e6 + 3; n void normalize(long long int &a) { a = (a + mod) % mod; } n long long int modmul(long long int a, long long int b) { n   a = a % mod; n   b = b % mod; n   normalize(a); n   normalize(b); n   return (a * b) % mod; n } n long long int modadd(long long int a, long long int b) { n   a = a % mod; n   b = b % mod; n   normalize(a); n   normalize(b); n   return (a + b) % mod; n } n long long int modsub(long long int a, long long int b) { n   a = a % mod; n   b = b % mod; n   normalize(a); n   normalize(b); n   return (a - b + mod) % mod; n } n long long int me(long long int x, long long int n) { n   x %= mod; n   if (n == 0) return 1; n   long long int u = me(x, n / 2) % mod; n   u = (u * u) % mod; n   if (n % 2) u = (u * x) % mod; n   return u; n } n long long int me1(long long int x, long long int n) { n   if (n == 0) return 1; n   long long int u = me1(x, n / 2); n   u = u * u; n   if (n % 2) u = u * x; n   return u; n } n inline long long int modInv(long long int a) { return me(a, mod - 2); } n inline long long int modDiv(long long int a, long long int b) { n   return modmul(a, modInv(b)); n } n float power(float x, long long int y) { n   float temp; n   if (y == 0) return 1; n   temp = power(x, y / 2); n   if (y % 2 == 0) n     return temp * temp; n   else { n     if (y > 0) n       return x * temp * temp; n     else n       return (temp * temp) / x; n   } n } n long long int __gcd(long long int a, long long int b) { n   if (b == 0) n     return a; n   else n     return __gcd(b, a % b); n } n const long long int N = 1e9 + 1; n bool compare(pair<long long int, pair<long long int, long long int>> a, n              pair<long long int, pair<long long int, long long int>> b) { n   if (a.first > b.first) return true; n   if (a.first == b.first) { n     if (a.second.first > b.second.first) n       return true; n     else if (a.second.first == b.second.first) { n       return a.second.second > b.second.second; n     } n     return false; n   } n   return false; n } n struct FenwickTree { n   map<long long int, long long int> f; n   void insert(long long int k, long long int x) { n     while (k < N) { n       f[k] = max(x, f[k]); n       k += (k & (-k)); n     } n   } n   long long int sum(long long int r) { n     long long int ans = 0; n     while (r > 0) { n       ans = max(f[r], ans); n       r -= ((r) & (-r)); n     } n     return ans; n   } n } ft; n void solve() { n   long long int n; n   cin >> n; n   vector<pair<long long int, pair<long long int, long long int>>> v(n); n   for (long long int i = 0; i < n; i++) { n     cin >> v[i].second.first >> v[i].first >> v[i].second.second; n   } n   sort(v.begin(), v.end(), compare); n   queue<pair<long long int, pair<long long int, long long int>>> q; n   long long int max1 = 0; n   for (long long int i = 0; i < n; i++) { n     auto k1 = v[i]; n     long long int k2 = ft.sum(k1.first - 1); n     max1 = max(max1, k2 + k1.second.second); n     ft.insert(k1.second.first, k2 + k1.second.second); n   } n   cout << max1; n } n signed main() { n   optimise(); n   long long int t; n   t = 1; n   cout << fixed << setprecision(5); n   while (t--) { n     solve(); n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e4 + 5, P = 1e9 + 7; n struct Trie { n   int ch[2], siz, mn, mx; n } t[N * 32]; n int n, a[N], cntnode = 1, s[N][33], rt[N], ans; n long long kth, m; n void Insert(int x) { n   int now = 1; n   t[now].siz++; n   for (int i = 31; ~i; --i) { n     int& nxt = t[now].ch[a[x] >> i & 1]; n     if (!nxt) nxt = ++cntnode, t[nxt].mn = x; n     now = nxt; n     ++t[now].siz; n     t[now].mx = x; n   } n } n int calc(int v, int l, int r) { n   if (!l) return 0; n   int res = 0; n   for (int i = 31; ~i; --i) { n     int p = v >> i & 1; n     if (!p) n       (res += (1LL << i) * (s[r][i] - s[l - 1][i]) % P) %= P; n     else n       (res += (1LL << i) * (r - l + 1 - s[r][i] + s[l - 1][i]) % P) %= P; n   } n   return res; n } n int main() { n   scanf( %d%lld , &n, &m); n   m *= 2; n   for (int i = 1; i <= n; ++i) scanf( %d , a + i); n   sort(a + 1, a + n + 1); n   for (int i = 1; i <= n; ++i) { n     Insert(i); n     for (int j = 31; ~j; --j) s[i][j] = s[i - 1][j] + (a[i] >> j & 1); n   } n   for (int i = 1; i <= n; ++i) rt[i] = 1; n   for (int i = 31; ~i; --i) { n     long long tot = 0; n     int z; n     for (int j = 1; j <= n; ++j) { n       int p = (a[j] >> i & 1) ^ 1; n       tot += t[t[rt[j]].ch[p]].siz; n     } n     if (tot >= m) n       z = 1, kth |= 1LL << i; n     else n       z = 0, m -= tot; n     for (int j = 1; j <= n; ++j) rt[j] = t[rt[j]].ch[(a[j] >> i & 1) ^ z]; n   } n   for (int i = 1; i <= n; ++i) { n     int now = 1; n     for (int j = 31; ~j; --j) { n       int p = (a[i] ^ kth) >> j & 1; n       if (!(kth >> j & 1) && t[now].ch[p ^ 1]) n         (ans += calc(a[i], t[t[now].ch[p ^ 1]].mn, t[t[now].ch[p ^ 1]].mx)) %= n             P; n       now = t[now].ch[p]; n     } n   } n   printf( %lld , 1LL * (kth * m % P + ans) % P * (P + 1 >> 1) % P); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2e5 + 10; n vector<pair<int, int> > g[maxn]; n set<pair<int, int> > s; n int c[maxn], dis[maxn]; n void dijekstra(int u) { n   dis[u] = 0; n   s.insert(make_pair(u, dis[u])); n   while (s.size()) { n     pair<int, int> a = *s.begin(); n     s.erase(a); n     for (int i = 0; i < g[a.first].size(); i++) { n       if (a.second + g[a.first][i].second < dis[g[a.first][i].first]) { n         s.erase(make_pair(g[a.first][i].first, dis[g[a.first][i].first])); n         dis[g[a.first][i].first] = a.second + g[a.first][i].second; n         s.insert(make_pair(g[a.first][i].first, dis[g[a.first][i].first])); n       } n     } n   } n   s.clear(); n } n int main() { n   ios_base::sync_with_stdio(false); n   memset(dis, 63, sizeof(dis)); n   int n, x, y; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> c[i]; n   for (int i = 0; i < n - 1; i++) { n     cin >> x >> y; n     if (c[x] == c[y]) { n       g[x].push_back(make_pair(y, 0)); n       g[y].push_back(make_pair(x, 0)); n     } else { n       g[x].push_back(make_pair(y, 1)); n       g[y].push_back(make_pair(x, 1)); n     } n   } n   dijekstra(1); n   int mx = -1, j; n   for (int i = 1; i <= n; i++) n     if (dis[i] > mx) mx = dis[i], j = i; n   memset(dis, 63, sizeof(dis)); n   dijekstra(j); n   mx = -1; n   for (int i = 1; i <= n; i++) n     if (dis[i] > mx) mx = dis[i], j = i; n   cout << (dis[j] + 1) / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[100003][3], ara[100003], mod = 1000000007, n, m; n long long solve(long long idx, long long sta) { n   if (idx == n) { n     if (sta == 0 || sta == 2) n       return 1; n     else { n       return 0; n     } n   } else if (dp[idx][sta] != -1) n     return dp[idx][sta]; n   else { n     long long p = 0, q = 0, r = 0, t, x, y, z; n     if (sta == 0) { n       p = solve(idx + 1, 0); n       x = 1; n       z = idx + 1; n       if (ara[idx] == 1) { n         x = 2; n         z = idx + m; n       } n       q = solve(z, x); n     } else if (sta == 1) { n       x = 1; n       z = idx + 1; n       if (ara[idx] == 1) { n         x = 2; n         z = idx + m; n       } n       q = solve(z, x); n     } else { n       p = solve(idx, 0); n       q = solve(idx + 1, sta); n     } n     t = (p + q) % mod; n     dp[idx][sta] = t; n     return dp[idx][sta]; n   } n } n int main() { n   long long i, k, ans = 1, sc = 1; n   string str, ttr, ntr; n   cin >> str; n   cin >> ttr; n   n = str.size(); n   m = ttr.size(); n   if (n >= m) { n     for (i = 0; i <= n - m; i++) { n       if (str[i] == ttr[0]) { n         ntr = str.substr(i, m); n         if (ntr == ttr) { n           ara[i] = 1; n         } n       } n       dp[i][0] = -1; n       dp[i][1] = -1; n       dp[i][2] = -1; n     } n   } n   if (n >= m) { n     for (i = n - m; i <= n; i++) { n       dp[i][0] = -1; n       dp[i][1] = -1; n       dp[i][2] = -1; n     } n     ans = solve(0, 0); n   } n   if (ans >= sc) n     k = ans - sc; n   else { n     k = (mod - sc) + ans; n   } n   printf( %lld n , k); n   return 0; n } n


#include <bits/stdc++.h> n void read(int& x) { n   x = 0; n   char c; n   do { n     c = getchar(); n   } while (c <  0  || c >  9 ); n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n } n using namespace std; n int n, m, f[1000 + 5][1000 + 5], ans, sum[1000 + 5]; n int s[100000 + 5][3], as[100000 + 5]; n vector<int> v[100000 + 5]; n void place(int x, int y) { n   f[x][y] = !f[x][y]; n   ans++; n   sum[x]++; n } n void remove(int x, int y) { n   f[x][y] = !f[x][y]; n   ans--; n   sum[x]--; n } n void invert(int x) { n   f[x][0] = !f[x][0]; n   ans -= sum[x]; n   sum[x] = m - sum[x]; n   ans += sum[x]; n } n void dfs(int k) { n   int o = 0, t, x, y, i; n   t = s[k][0]; n   x = s[k][1]; n   y = s[k][2]; n   if (t == 1) n     if (f[x][y] ^ f[x][0] ^ 1) n       place(x, y); n     else n       o = 1; n   if (t == 2) n     if (f[x][y] ^ f[x][0]) n       remove(x, y); n     else n       o = 1; n   if (t == 3) invert(x); n   as[k] = ans; n   for (i = 0; i < v[k].size(); i++) dfs(v[k][i]); n   if (o) return; n   if (t == 1) remove(x, y); n   if (t == 2) place(x, y); n   if (t == 3) invert(x); n } n int main() { n   int q, x, y, i; n   read(n); n   read(m); n   read(q); n   for (i = 1; i <= q; i++) { n     read(s[i][0]); n     read(s[i][1]); n     if (s[i][0] < 3) read(s[i][2]); n     if (s[i][0] < 4) n       v[i - 1].push_back(i); n     else n       v[s[i][1]].push_back(i); n   } n   dfs(0); n   for (i = 1; i <= q; i++) printf( %d n , as[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int64_t n; n   cin >> n; n   multiset<int64_t> S; n   if (n & 1) { n     n--; n     cout << n / 2 << endl; n     while (n) { n       S.insert(2); n       n = n - 2; n     } n     auto itr = S.end(); n     itr--; n     S.erase(itr); n     S.insert(3); n   } else { n     cout << n / 2 << endl; n     while (n) { n       S.insert(2); n       n = n - 2; n     } n   } n   for (auto itr = S.begin(); itr != S.end(); itr++) { n     cout << (*itr) <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   while (cin >> n) { n     string s; n     getline(cin, s); n     getline(cin, s); n     stringstream ss(s); n     string token; n     string res =   ; n     vector<int> numelements; n     bool error = 0; n     if (s ==  int ) { n       cout <<  int  << endl; n       continue; n     } n     while (ss >> token) { n       int cur = numelements.size() - 1; n       if (cur >= 0 && numelements[cur] == 1) res +=  , ; n       if (token ==  pair ) { n         if (cur >= 0) { n           numelements[cur]++; n           if (numelements[cur] >= 3) { n             error = 1; n             break; n           } n         } n         res +=  pair< ; n         numelements.push_back(0); n       } else if (token ==  int ) { n         if (cur < 0) { n           error = 1; n           break; n         } n         numelements[cur]++; n         res +=  int ; n         if (numelements[cur] >= 3) { n           error = 1; n           break; n         } n         while (numelements.size() > 0 && n                numelements[numelements.size() - 1] == 2) { n           numelements.erase(numelements.end() - 1); n           res +=  > ; n         } n       } n     } n     if (numelements.size() > 0) error = 1; n     if (error) n       cout <<  Error occurred  << endl; n     else n       cout << res << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int i, j, k, l, m, n, x, y, z; n   cin >> n >> x >> y; n   string s; n   cin >> s; n   for (i = 0; i < n; i++) { n     if (s[i] ==  0 ) break; n   } n   if (i == n) return cout << 0 << endl, 0; n   for (j = s.size() - 1; j >= 0; j--) n     if (s[j] ==  0 ) break; n   s = s.substr(i, j - i + 1); n   long long int cnt = 0; n   for (i = 1; i < s.size(); i++) { n     if (s[i] ==  0  && s[i - 1] ==  1 ) cnt++; n   } n   cnt++; n   cout << (cnt - 1) * min(x, y) + y << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 998244353; n const int N = 2e5 + 5; n int gcd(int a, int b) { n   if (b == 0) return a; n   a %= b; n   return gcd(b, a); n } n bool compare(long long i, long long j) { return i > j; } n long long power(long long x, long long y) { n   if (y == 0) n     return 1; n   else if (y % 2 == 0) n     return (power(x, y / 2) % mod * power(x, y / 2) % mod) % mod; n   else n     return (x % mod * power(x, y / 2) % mod * power(x, y / 2) % mod) % mod; n } n class sortByVal { n  public: n   bool operator()(const pair<long long, pair<long long, long long>> &a, n                   const pair<long long, pair<long long, long long>> &b) { n     if (a.first != b.first) { n       return a.first < b.first; n     } n     return a.second.first > b.second.first; n   } n }; n bool prime[100005]; n void SieveOfEratosthenes(long long n) { n   memset(prime, true, sizeof(prime)); n   for (int p = 2; p * p <= n; p++) { n     if (prime[p] == true) { n       for (int i = p * p; i <= n; i += p) prime[i] = false; n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     long long a, b, n, m; n     cin >> a >> b >> n >> m; n     if (n + m > a + b) { n       cout <<  NO  << endl; n       ; n     } else { n       if (m > min(a, b)) { n         cout <<  NO  << endl; n         ; n       } else n         cout <<  YES  << endl; n       ; n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n ifstream fin( A.in ); n ofstream fout( A.out ); n long long r, h; n int main() { n   cin >> r >> h; n   if (h % r == 0) { n     cout << (h / r) * 2 + 1; n     return 0; n   } n   double d = double(h) / double(r); n   int z = h / r; n   double dd = z + 0.5; n   if (d >= dd) { n     if ((r / 2) * (r / 2) + ((d - dd) * r + r / 2) * ((d - dd) * r + r / 2) >= n         r * r) n       cout << 2 * z + 3; n     else n       cout << 2 * z + 2; n   } else { n     cout << 2 * z + 1; n   } n } n


#include <bits/stdc++.h> n const int maxn = 1e2 + 10; n const int dis[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}}; n using namespace std; n long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); } n long long qpow(long long p, long long q) { n   long long f = 1; n   while (q) { n     if (q & 1) f = f * p % 1000000007; n     p = p * p % 1000000007; n     q >>= 1; n   } n   return f; n } n int n, m, k, t, q, co[maxn][maxn], c[maxn]; n long long dp[maxn][maxn][maxn]; n int main() { n   int i, j; n   for (i = 0; i <= 100; i++) n     for (j = 0; j <= 100; j++) n       for (k = 0; k <= 100; k++) dp[i][j][k] = 1e18; n   scanf( %d%d%d , &n, &m, &q); n   for (i = 1; i <= n; i++) scanf( %d , &c[i]); n   for (i = 1; i <= n; i++) n     for (j = 1; j <= m; j++) scanf( %d , &co[i][j]); n   i = 1; n   if (i == 1) { n     if (c[i]) n       dp[1][c[i]][1] = 0; n     else { n       for (j = 1; j <= m; j++) dp[1][j][1] = co[1][j]; n     } n   } n   for (i = 2; i <= n; i++) { n     if (c[i]) { n       for (j = 1; j <= m; j++) n         for (k = 1; k <= 100; k++) { n           if (j == c[i]) n             dp[i][j][k] = min(dp[i - 1][j][k], dp[i][j][k]); n           else n             dp[i][c[i]][k] = min(dp[i - 1][j][k - 1], dp[i][c[i]][k]); n         } n     } else { n       for (j = 1; j <= m; j++) n         for (k = 1; k <= 100; k++) n           for (t = 1; t <= m; t++) { n             dp[i][j][k] = n                 min(dp[i][j][k], t == j ? dp[i - 1][t][k] + co[i][j] n                                         : dp[i - 1][t][k - 1] + co[i][j]); n           } n     } n   } n   long long mi = 1e18; n   for (i = 1; i <= m; i++) mi = min(dp[n][i][q], mi); n   printf( %lld n , mi == 1e18 ? -1 : mi); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void CP() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(NULL); n   cout.tie(NULL); n   return; n } n int main() { n   ios_base::sync_with_stdio(NULL); n   cin.tie(NULL); n   cout.tie(NULL); n   int n, m; n   cin >> n >> m; n   int a[n + 1]; n   a[1] = 1; n   for (int i = 2; i <= n; i++) { n     a[i] = a[i - 1] + i; n   } n   for (int i = 1; i <= n; i++) { n     cerr << a[i] <<    ; n   } n   cerr << endl; n   if (m == 1) { n     m -= 1; n   } else { n     if (m > a[n]) { n       m %= a[n]; n       cerr << m << endl; n     } n     int i; n     for (i = n - 1; i > 1; i--) { n       if (m >= a[i]) { n         m %= a[i]; n         break; n       } n     } n     if (i == 1 && m != 0) { n       m -= 1; n     } n   } n   cout << m << endl; n   return 0; n } n


#include<bits/stdc++.h> n #define fast  ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) n #define arin(x,n) for(int i=0;i<n;i++)cin>>x[i] n #define debug(x,n) for(int i=0;i<n;i++)cout<<x[i]<<    n #define pb push_back n #define all(c) begin(c), end(c) n #define isAll(c) (int)(c).size() n typedef long long int ll; n   n using namespace std; n  n int solve(vector <pair<int, int>> &arr) n { n     int num = isAll(arr); n     vector <int> lc(num);  n     vector <int> rc(num); n  n     for (int i = 0; i < num; i++) n         lc[i] = arr[i].first, rc[i] = arr[i].second; n  n     sort(all(lc)), sort(all(rc)); n  n     int result = 1e9 + 7; n     for (auto [l, r] : arr) n     { n         int result_l = distance(begin(rc), lower_bound(all(rc), l)), n             result_r = distance(upper_bound(all(lc), r), end(lc)); n               result = min(result, result_l + result_r); n     } n     return result; n } n   n int main() n { n  fast; n  ll T=1; n  cin>>T; n  while(T--) n  { n   int num; n   cin >> num; n  n   vector <pair<int, int>> arr(num); n   for(auto &[x, y] : arr)  n    cin >> x >> y; n    n      cout << solve(arr) << endl; n  } n  return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, a, b; n     string s =   , s1; n     cin >> n >> a >> b; n     for (int i = 0; i < b; i++) s += ( a  + i); n     for (int i = b; i < a; i++) s +=  a ; n     s1 = s; n     int x = n / a, y = n % a; n     x--; n     while (x--) { n       s += s1; n     } n     for (int i = 0; i < y; i++) s += s1[i]; n     cout << s << endl; n   } n } n


#include <bits/stdc++.h> n inline long long read() { n   long long x = 0; n   char ch = getchar(), w = 1; 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 -  0 ; n     ch = getchar(); n   } n   return x * w; n } n void write(long long x) { n   if (x < 0) putchar( - ), x = -x; n   if (x > 9) write(x / 10); n   putchar(x % 10 +  0 ); n } n inline void writeln(long long x) { n   write(x); n   puts(  ); n } n using namespace std; n const int N = 5e5 + 656666; n long long n, m, c, c0; n struct TT { n   long long t, a, b; n } a[N]; n bool cmp(TT x, TT y) { return x.t < y.t; } n map<long long, long long> q; n long long cal() { n   q.clear(); n   long long ans = 0; n   q[0] = c0; n   long long now = c0, cap = c; n   for (int i = 1; i <= n; ++i) { n     long long rest = a[i].t - a[i - 1].t; n     long long val = 0; n     now -= rest; n     while (!q.empty() && rest) { n       val = min(rest, q.begin()->second); n       rest -= val; n       q.begin()->second -= val; n       ans += val * q.begin()->first; n       if (!q.begin()->second) q.erase(q.begin()); n     } n     if (rest) return -1; n     long long ned = min(cap - now, a[i].a); n     now += ned; n     while (!q.empty() && a[i].b < q.rbegin()->first && ned < a[i].a) { n       val = min(q.rbegin()->second, a[i].a - ned); n       q.rbegin()->second -= val; n       ned += val; n       if (!q.rbegin()->second) q.erase(--q.end()); n     } n     q[a[i].b] += ned; n   } n   return ans; n } n int main() { n   int T = read(); n   while (T--) { n     n = read(), m = read(), c = read(), c0 = read(); n     for (int i = 1; i <= n; ++i) { n       a[i].t = read(); n       a[i].a = read(); n       a[i].b = read(); n     } n     a[++n] = (TT){m, 0, 0}; n     sort(a + 1, a + 1 + n, cmp); n     writeln(cal()); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void task() { n   int s, t, q, r = 1; n   long long tm, cs; n   cin >> t >> s >> q; n   cs = s; n   while (cs < t) { n     tm = cs; n     cs = (q - 1) * tm + cs; n     if (cs < t) r++; n   } n   cout << r; n } n int main() { n   ios_base::sync_with_stdio(false); n   task(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void err(istream_iterator<string> it) { cerr <<   n ; } n template <typename T, typename... Args> n void err(istream_iterator<string> it, T a, Args... args) { n   cerr << setw(2) << *it <<   =   << setw(2) << a <<    ; n   err(++it, args...); n } n void solve() { n   int n; n   cin >> n; n   int a[n], status[n], res[n]; n   memset(res, 0, sizeof res); n   for (int i = 0; i < n; i++) cin >> a[i]; n   for (int i = 0; i < n; i++) cin >> status[i]; n   for (int i = 0; i < n; i++) n     if (status[i]) res[i] = a[i]; n   vector<int> v; n   for (int i = 0; i < n; i++) { n     if (status[i] == 0) v.push_back(a[i]); n   } n   sort(v.rbegin(), v.rend()); n   int idx = 0; n   for (int i = 0; i < n; i++) n     if (status[i] == 0) res[i] = v[idx++]; n   for (int i = 0; i < n; i++) cout << res[i] <<    ; n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(nullptr); n   int test; n   cin >> test; n   while (test--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int a[1000000]; n vector<pair<int, int> > ans; n int main() { n   ios_base::sync_with_stdio(false); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= k; i++) { n     int high = 1, low = 1; n     for (int j = 2; j <= n; j++) { n       if (a[j] > a[high]) high = j; n       if (a[j] < a[low]) low = j; n     } n     if (high != low) { n       ans.push_back(make_pair(high, low)); n       a[high]--; n       a[low]++; n     } n   } n   int high = 1, low = 1; n   for (int j = 2; j <= n; j++) { n     if (a[j] > a[high]) high = j; n     if (a[j] < a[low]) low = j; n   } n   int s = ans.size(); n   cout << a[high] - a[low] <<     << s << endl; n   ; n   for (int i = 0; i <= s - 1; i++) n     cout << ans[i].first <<     << ans[i].second << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MN = 44, MS = (1 << 21) + 2; n int n, K; n long long adj[MN]; n int dp[MS], clique; n bool is[MS]; n int main() { n   ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> K; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) { n       bool t; n       cin >> t; n       adj[i] |= (1ll << j) * t; n     } n   if (n == 1) return cout <<  0 n , 0; n   int b = n / 2; n   for (int msk = 1; msk < (1 << b); ++msk) { n     int v = __builtin_ctz(msk); n     dp[msk] = dp[msk ^ (1 << v)]; n     dp[msk] = max(dp[msk], dp[(msk ^ (1 << v)) & adj[v]] + 1); n     clique = max(clique, dp[msk]); n   } n   is[0] = true; n   for (int msk = 1; msk < (1 << (n - b)); ++msk) { n     int v = __builtin_ctz(msk); n     is[msk] = is[msk ^ (1 << v)]; n     v += b; n     for (int i = b; i < n; ++i) n       if (i != v) { n         if ((msk & (1 << (i - b))) && !(adj[v] & (1ll << i))) is[msk] = false; n       } n     if (is[msk]) { n       long long M = (1 << b) - 1; n       for (int i = b; i < n; ++i) n         if (msk & 1 << (i - b)) M &= adj[i]; n       clique = max(clique, dp[M] + __builtin_popcount(msk)); n     } n   } n   cout << setprecision(12) << fixed n        << 1ll * clique * (clique - 1) * ((long double)K / clique) * n               ((long double)K / clique) / 2 n        <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, int> > v; n int n; n long long A[100005]; n long long B[100005]; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     A[i] = (long long)a; n     B[i] = (long long)b; n     v.push_back(make_pair((long long)(b - a), i)); n   } n   sort(v.begin(), v.end()); n   long long ans = 0; n   for (int i = 1; i <= n; i++) { n     long long a = A[v[i - 1].second]; n     long long b = B[v[i - 1].second]; n     ans += a * ((long long)(i - 1)) + b * ((long long)(n - i)); n   } n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<int, string> mapa; n int main() { n   mapa[0] =  zero ; n   mapa[1] =  one ; n   mapa[2] =  two ; n   mapa[3] =  three ; n   mapa[4] =  four ; n   mapa[5] =  five ; n   mapa[6] =  six ; n   mapa[7] =  seven ; n   mapa[8] =  eight ; n   mapa[9] =  nine ; n   mapa[10] =  ten ; n   mapa[11] =  eleven ; n   mapa[12] =  twelve ; n   mapa[13] =  thirteen ; n   mapa[14] =  fourteen ; n   mapa[15] =  fifteen ; n   mapa[16] =  sixteen ; n   mapa[17] =  seventeen ; n   mapa[18] =  eighteen ; n   mapa[19] =  nineteen ; n   mapa[20] =  twenty ; n   mapa[30] =  thirty ; n   mapa[40] =  forty ; n   mapa[50] =  fifty ; n   mapa[60] =  sixty ; n   mapa[70] =  seventy ; n   mapa[80] =  eighty ; n   mapa[90] =  ninety ; n   int num; n   cin >> num; n   if (num <= 19) { n     cout << mapa[num] << endl; n   } else if (num % 10 == 0) { n     cout << mapa[num] << endl; n   } else { n     int aux = (num / 10) * 10; n     int rest = num % 10; n     cout << mapa[aux] <<  -  << mapa[rest] << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y, a = 0; n   cin >> x >> y; n   for (int i = 1; i <= x; i++) { n     if (i % 2 == 0) { n       if (a == 0) { n         for (int k = 0; k < y - 1; k++) { n           cout <<  . ; n         } n         cout <<  #  << endl; n         a++; n       } else { n         a = 0; n         cout <<  # ; n         for (int k = 0; k < y - 1; k++) { n           if (k == y - 2) { n             cout <<  .  << endl; n           } else { n             cout <<  . ; n           } n         } n       } n     } else { n       for (int j = 0; j < y; j++) { n         if (j == y - 1) { n           cout <<  #  << endl; n         } else { n           cout <<  # ; n         } n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int cmpfunc(const void* a, const void* b) { return (*(int*)a - *(int*)b); } n int main() { n   int A, B, i; n   long long int sum = 0; n   int a[100001] = {0}; n   cin >> A; n   cin >> B; n   for (i = 0; i < A; i++) { n     cin >> a[i]; n   } n   qsort(a, A, sizeof(int), cmpfunc); n   for (i = 1; i < A; i++) { n     if ((a[i] - a[0]) % B == 0) n       sum = sum + (a[i] - a[0]) / B; n     else { n       sum = -1; n       break; n     } n   } n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n bool vis[26]; n int main() { n   cin >> s; n   long long ans = 1; n   int cnt1 = 0; n   int cnt2 = 0; n   memset(vis, 0, sizeof(vis)); n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  ? ) n       cnt2++; n     else if (s[i] >=  A  && s[i] <=  Z ) { n       if (vis[s[i] -  A ] == 1) n         continue; n       else n         vis[s[i] -  A ] = 1, cnt1++; n     } n   } n   if (cnt1) n     if (s[0] >=  A  && s[0] <=  Z ) n       ans = 9; n     else n       ans = 10; n   for (int i = 9; i > 9 - cnt1 + 1; i--) ans *= i; n   if (cnt2) n     if (s[0] ==  ? ) n       ans *= 9; n     else n       ans *= 10; n   for (int i = 1; i < cnt2; i++) ans *= 10; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1010; n const long long INF = 1e9 + 19; n const int LOG = 11; n int n, m; n int a[N][N]; n int b[N][N]; n int pref[N][N]; n short table[LOG][LOG][N][N]; n int deg[N]; n void read() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) scanf( %d , &a[i][j]); n } n int getSum(int x1, int y1, int x2, int y2) { n   return pref[x2][y2] - pref[x1][y2] - pref[x2][y1] + pref[x1][y1]; n } n bool isSquare(int x1, int y1, int x2, int y2) { n   return (x2 - x1) * (y2 - y1) == getSum(x1, y1, x2, y2); n } n int getMax(int x1, int y1, int x2, int y2) { n   int szX = x2 - x1; n   int szY = y2 - y1; n   int dx = deg[szX]; n   int dy = deg[szY]; n   int tmp = max(max(table[dx][dy][x1][y1], table[dx][dy][x2 - (1 << dx)][y1]), n                 max(table[dx][dy][x1][y2 - (1 << dy)], n                     table[dx][dy][x2 - (1 << dx)][y2 - (1 << dy)])); n   return tmp; n } n void solve() { n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) n       pref[i + 1][j + 1] = n           pref[i + 1][j] + pref[i][j + 1] - pref[i][j] + a[i][j]; n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       int l = 0; n       int r = min(n - i, m - j) + 1; n       while (r - l > 1) { n         int mid = (l + r) / 2; n         if (isSquare(i, j, i + mid, j + mid)) n           l = mid; n         else n           r = mid; n       } n       b[i][j] = l; n     } n   for (int i = 2; i <= max(n, m); i++) deg[i] = deg[i / 2] + 1; n   for (int dx = 0; dx <= 10; dx++) n     for (int dy = 0; dy <= 10; dy++) n       for (int i = 0; i + (1 << dx) <= n; i++) n         for (int j = 0; j + (1 << dy) <= m; j++) { n           if (dx == 0 && dy == 0) { n             table[0][0][i][j] = b[i][j]; n           } else if (dx != 0) { n             table[dx][dy][i][j] = n                 max(table[dx - 1][dy][i][j], n                     table[dx - 1][dy][i + (1 << (dx - 1))][j]); n           } else n             table[dx][dy][i][j] = n                 max(table[dx][dy - 1][i][j], n                     table[dx][dy - 1][i][j + (1 << (dy - 1))]); n         } n   int t; n   scanf( %d , &t); n   for (int it = 0; it < t; it++) { n     int x1, y1, x2, y2; n     scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n     x1--; n     y1--; n     int l = 0; n     int r = min(x2 - x1, y2 - y1) + 1; n     while (r - l > 1) { n       int mid = (l + r) / 2; n       if (getMax(x1, y1, x2 - mid + 1, y2 - mid + 1) >= mid) n         l = mid; n       else n         r = mid; n     } n     printf( %d n , l); n   } n } n void stress() {} n int main() { n   if (1) { n     int k = 1; n     for (int tt = 0; tt < k; tt++) { n       read(); n       solve(); n     } n   } else { n     stress(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n class Computational_Geometry { n  public: n   void init(); n   void solve(); n   Computational_Geometry() { n     EPS = 1e-10; n     Pi = acos(-1.0); n   } n  n  private: n   struct node { n     long long x, y; n   } point[100010], line[100010]; n   struct inter { n     double x, y; n   } q[100010], p[100010]; n   int n, m; n   double EPS, Pi; n   friend bool operator==(const node &, const node &); n   friend node operator-(const node &, const node &); n   friend long long operator*(const node &, const node &); n }; n bool operator==(const Computational_Geometry::node &a, n                 const Computational_Geometry::node &b) { n   return (a.x == b.x && a.y == b.y); n } n Computational_Geometry::node operator-(const Computational_Geometry::node &a, n                                        const Computational_Geometry::node &b) { n   Computational_Geometry::node d; n   d.x = a.x - b.x; n   d.y = a.y - b.y; n   return d; n } n long long operator*(const Computational_Geometry::node &a, n                     const Computational_Geometry::node &b) { n   return a.x * b.y - a.y * b.x; n } n void Computational_Geometry::init() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> point[i].x >> point[i].y; n   for (int i = 2; i <= n; i++) line[i] = point[i] - point[1]; n } n void Computational_Geometry::solve() { n   bool flag = true; n   cin >> m; n   for (int i = 1; i <= m; i++) { n     if (!flag) continue; n     node p, now; n     cin >> p.x >> p.y; n     now = p - point[1]; n     if (now * line[2] <= 0 || now * line[n] >= 0) flag = false; n     int l = 2, r = n - 1; n     while (l <= r) { n       int mid = (l + r) / 2; n       if (now * line[mid + 1] <= 0) n         r = mid - 1; n       else n         l = mid + 1; n     } n     if ((p - point[l]) * (point[l + 1] - point[l]) <= 0) flag = false; n   } n   if (flag) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n int main() { n   ios::sync_with_stdio(false); n   Computational_Geometry ans; n   ans.init(); n   ans.solve(); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC target( avx2 ) n #pragma GCC optimization( O3 ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n const int N = 1000090, M = 1000000007; n int B[N]; n void up(int i, int v) { n   for (; i < N; i += (i & (-i))) B[i] += v; n } n int sm(int i) { n   int s = 0; n   for (; i > 0; i -= (i & (-i))) s += B[i]; n   return s; n } n int main() { n   int q, x, n; n   scanf( %d %d , &n, &q); n   for (int i = 0; i < n; ++i) scanf( %d , &x), up(x, 1); n   for (int Q = 0; Q < q; ++Q) { n     scanf( %d , &x); n     if (x > 0) n       up(x, 1), n++; n     else { n       int b = 0, e = N; n       x = -1 * x; n       while (b < e) { n         int bw = (b + e) / 2; n         int z = sm(bw); n         if (z >= x) n           e = bw; n         else n           b = bw + 1; n       } n       up(b, -1); n       n--; n     } n   } n   int ans = sm(N); n   if (!ans) n     printf( 0 ); n   else { n     for (int i = 1; i < N; ++i) n       if (sm(i) > 0) return cout << i, 0; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   vector<vector<int>> v(200001, vector<int>(32)); n   for (int i = 0; i < 2e5 + 1; i++) { n     for (int j = 0; j < 32; j++) { n       if (i & (1 << j)) v[i][j] = 1; n     } n   } n   for (int i = 1; i < 2e5 + 1; i++) { n     for (int j = 0; j < 32; j++) { n       v[i][j] += v[i - 1][j]; n     } n   } n   while (t--) { n     int l, r; n     cin >> l >> r; n     int ans = 1e9; n     for (int i = 0; i < 32; i++) { n       if ((1 << i) <= r) { n         ans = min(ans, r - v[r][i] - l + 1 + v[l - 1][i]); n       } n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, int> mm; n long long Abs(long long a) { n   if (a < 0) a = -a; n   return a; n } n int main() { n   int q, l, m, a; n   long long b; n   scanf( %lld%d%d%d , &b, &q, &l, &m); n   for (int i = 0; i < m; i++) { n     scanf( %d , &a); n     mm[a] = 1; n   } n   int cur = b, cnt = 0; n   if (Abs(b) > l) { n     printf( 0 n ); n     return 0; n   } n   if (q == 1) { n     if (mm[b] == 0) n       printf( inf n ); n     else n       printf( 0 n ); n     return 0; n   } n   if (q == -1) { n     if (mm[b] && mm[-b]) n       printf( 0 n ); n     else n       printf( inf n ); n     return 0; n   } n   if (q == 0 && b != 0) { n     if (mm[b] && mm[0]) n       printf( 0 n ); n     else if (mm[0] == 0) n       printf( inf n ); n     else if (mm[b] == 0) n       printf( 1 n ); n     return 0; n   } n   if (b == 0) { n     if (mm[0] == 1) n       printf( 0 n ); n     else n       printf( inf n ); n     return 0; n   } n   while (Abs(b) <= l) { n     if (mm[b] == 0) cnt++; n     b = b * q; n   } n   printf( %d n , cnt); n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n const int maxn = 200100; n map<int, long long> m1; n map<int, long long> m2; n int a[maxn]; n int main() { n   m1.clear(); n   m2.clear(); n   int n, k; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   long long sum = 0; n   for (int i = n; i >= 1; i--) { n     if ((long long)a[i] * k <= 1e9 && (long long)a[i] * k >= -1e9) { n       sum += m2[a[i] * k]; n       m2[a[i]] += m1[a[i] * k]; n     } n     m1[a[i]]++; n   } n   cout << sum << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = (1 << 29); n const int N = 1024; n int indeg[N], outdeg[N]; n int dis[N][N]; n int net[N][N]; n bool vis[N]; n int n, m; n int tmp; n void dfs(int sta, int u) { n   vis[u] = true; n   for (int i = 1; i <= n; i++) { n     if (!vis[i] && net[u][i]) { n       tmp = min(tmp, net[u][i]); n       dfs(sta, i); n     } n   } n   dis[sta][u] = tmp; n } n int main() { n   while (scanf( %d %d , &n, &m) == 2) { n     if (n == 1) { n       printf( 0 n ); n       continue; n     } n     memset(indeg, 0, sizeof(indeg)); n     memset(outdeg, 0, sizeof(outdeg)); n     memset(vis, false, sizeof(vis)); n     memset(net, 0, sizeof(net)); n     fill(dis[0], dis[0] + N * N, oo); n     for (int i = 1; i <= m; i++) { n       int u, v, w; n       scanf( %d %d %d , &u, &v, &w); n       net[u][v] = w; n       outdeg[u]++; n       indeg[v]++; n     } n     for (int i = 1; i <= n; i++) { n       if (!vis[i] && !indeg[i]) { n         tmp = oo; n         dfs(i, i); n       } n     } n     int ans = 0; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         if (indeg[i] == 0 && outdeg[j] == 0) { n           if (dis[i][j] != oo) { n             ans++; n           } n         } n       } n     } n     printf( %d n , ans); n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= n; j++) { n         if (indeg[i] == 0 && outdeg[j] == 0) { n           if (dis[i][j] != oo) { n             printf( %d %d %d n , i, j, dis[i][j]); n           } n         } n       } n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n int main() { n   int t, s, q; n   scanf( %d %d %d , &t, &s, &q); n   int j = 0; n   while (s < t) { n     s *= q; n     j++; n   } n   printf( %d n , j); n   return 0; n } n


#include <bits/stdc++.h> n  n using namespace std; n typedef long long ll; n  n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int n, m; n   cin >> n >> m; n   vector<vector<int>> a(n, vector<int>(m)); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) cin >> a[i][j]; n   } n   vector<vector<int>> b(n, vector<int>(m)); n   for (int i = 0; i < n; ++i) { n     for (int j = 0; j < m; ++j) cin >> b[i][j]; n   } n   vector<vector<int>> bb; n   for (int i = 0; i < n; ++i) bb.push_back(b[i]); n   sort(bb.begin(), bb.end()); n   bb.erase(unique(bb.begin(), bb.end()), bb.end()); n   vector<vector<int>> who_a(bb.size()); n   vector<vector<int>> who_b(bb.size()); n   for (int i = 0; i < n; ++i) { n     int ind = lower_bound(bb.begin(), bb.end(), a[i]) - bb.begin(); n     if (ind == (int)bb.size() || bb[ind] != a[i]) { n       cout << -1 << endl; n       return 0; n     } n     who_a[ind].push_back(i); n     ind = lower_bound(bb.begin(), bb.end(), b[i]) - bb.begin(); n     who_b[ind].push_back(i); n   } n   vector<int> p(n); n   for (int i = 0; i < (int)bb.size(); ++i) { n     if (who_a[i].size() != who_b[i].size()) { n       cout << -1 << endl; n       return 0; n     } n     for (int j = 0; j < (int)who_a[i].size(); ++j) { n       p[who_a[i][j]] = who_b[i][j]; n     } n   } n   vector<int> q(n); n   for (int i = 0; i < n; ++i) q[p[i]] = i; n   vector<bool> good(n, 0); n   vector<int> cnt_bad(m, 0), cnt_kek(m, 0); n   for (int j = 0; j < m; ++j) { n     for (int i = 0; i < n - 1; ++i) { n       if (a[q[i]][j] < a[q[i + 1]][j]) ++cnt_kek[j]; n       if (a[q[i]][j] > a[q[i + 1]][j]) ++cnt_bad[j]; n     } n   } n   vector<int> ans; n   while (true) { n     bool ok = true; n     for (int i = 0; i < n - 1; ++i) ok &= good[i] || q[i] < q[i + 1]; n     if (ok) break; n     int col = -1; n     for (int j = 0; j < m; ++j) { n       if (cnt_bad[j] == 0 && cnt_kek[j] > 0) { n         col = j; n         break; n       } n     } n     if (col == -1) break; n     ans.push_back(col); n     for (int i = 0; i < n - 1; ++i) { n       if (!good[i] && a[q[i]][col] < a[q[i + 1]][col]) { n         good[i] = true; n         for (int j = 0; j < m; ++j) { n             if (a[q[i]][j] < a[q[i + 1]][j]) --cnt_kek[j]; n             if (a[q[i]][j] > a[q[i + 1]][j]) --cnt_bad[j]; n         } n       } n     } n   } n   bool ok = true; n   for (int i = 0; i < n - 1; ++i) ok &= good[i] || q[i] < q[i + 1]; n   if (ok) { n     cout << (int)ans.size() << endl; n     reverse(ans.begin(), ans.end()); n     for (int i : ans) cout << i + 1 <<    ; n     cout << endl; n   } else { n     cout << -1 << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   string s; n   int a, b = 0; n   cin >> n; n   cin >> s; n   int ans = 2000000000; n   for (int i = 0; i < n; i++) { n     a = b; n     cin >> b; n     if (i) { n       if (s[i] ==  L  && s[i - 1] ==  R ) ans = min(ans, (b - a) / 2); n     } n   } n   if (ans == 2000000000) ans = -1; n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int cost[2005]; n bool have[2005][3]; n int dp[2003][10]; n int solve(int i, int vits) { n   if (i >= n) { n     if (vits == 7) return 0; n     return 1e9; n   } n   int &ret = dp[i][vits]; n   if (ret + 1) return ret; n   int sol1 = 0, sol2 = 0; n   sol1 = solve(i + 1, vits); n   sol2 = cost[i] + solve(i + 1, vits | (have[i][0] | (2 * have[i][1]) | n                                         (4 * have[i][2]))); n   return ret = min(sol1, sol2); n } n int main() { n   ios_base::sync_with_stdio(false); n   memset(dp, -1, sizeof(dp)); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> cost[i]; n     string s; n     cin >> s; n     for (auto x : s) { n       if (x ==  A ) have[i][0] = true; n       if (x ==  B ) have[i][1] = true; n       if (x ==  C ) have[i][2] = true; n     } n   } n   int res = solve(0, 0); n   if (res > 7e8) res = -1; n   cout << res <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n using ll = long long; n using ull = unsigned long long; n using ld = long double; n using namespace std; n int n; n void solve() { n   cin >> n; n   if (n % 2 == 0) { n     cout <<  NO ; n     return; n   } n   cout <<  YES n ; n   vector<int> v(2 * n + 1); n   for (int i = 1; i <= n; i++) { n     if (i % 2) { n       v[i] = 2 * i - 1; n       v[i + n] = 2 * i; n     } else { n       v[i] = 2 * i; n       v[i + n] = 2 * i - 1; n     } n   } n   for (int i = 1; i <= 2 * n; i++) cout << v[i] <<    ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int b, h, a; n   long long int count = 0; n   cin >> b >> h; n   for (int i = h - 1; i >= 0; i = i - 2) { n     for (int j = b - 1; j >= 0; j = j - 2) { n       a = j * i; n       count += a; n     } n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize( Ofast ) n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL), cout.tie(NULL); n   long long n, p, d, w; n   cin >> n >> p >> w >> d; n   for (register int y = (0); y <= (w); ++y) { n     long long x = (p - y * d) / w; n     if (x < 0) continue; n     if (x * w + y * d != p) continue; n     if (x + y <= n) { n       cout << x <<     << y <<     << n - x - y <<   n ; n       return 0; n     } n   } n   cout << -1 <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int num[2005]; n int sum[2005]; n bool cmp(int a, int b) { return a > b; } n int main() { n   int n, i, j, t; n   int temp; n   while (cin >> n) { n     memset(num, 0, sizeof(num)); n     for (i = 0; i < n; i++) { n       cin >> temp; n       num[temp]++; n     } n     cin >> t; n     memset(sum, 0, sizeof(sum)); n     for (i = 0; i <= t; i++) { n       sum[0] += num[i]; n     } n     for (i = 1; i < 1001; i++) { n       sum[i] = sum[i - 1] - num[i - 1] + num[i + t]; n     } n     sort(sum, sum + 1001, cmp); n     cout << sum[0] << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, a, b; n   cin >> n >> a >> b; n   int val = n - a; n   if (val > (b + 1)) { n     cout << b + 1 << endl; n   } else { n     cout << val << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int ABS(long long int a) { n   if (a < 0) return (-a); n   return a; n } n void input() {} n int main() { n   long long int n, t; n   int f = 0; n   scanf( %llu , &n); n   scanf( %llu , &t); n   long long int x[n]; n   for (int i = (1); i < (n); ++i) { n     scanf( %llu , &x[i]); n   } n   long long int i = 1; n   while (i <= t) { n     if (i == t) f = 1; n     i += x[i]; n     if (i == t) f = 1; n     if (f) break; n   } n   if (f) n     printf( YES ); n   else n     printf( NO ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n vector<T> &operator+=(vector<T> &v, T x) { n   v.push_back(x); n   return v; n } n void solve() { n   int n; n   cin >> n; n   int a[n], b[n]; n   int cash = 1, t = 0; n   for (int i = (0), _b_ = (n); i < _b_; i++) { n     cin >> a[i] >> b[i]; n     if (i > 0) { n       if (a[i] == a[i - 1] && b[i] == b[i - 1]) { n         t++; n       } else { n         cash = max(cash, t + 1); n         t = 0; n       } n     } n   } n   cout << max(cash, t + 1); n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 500005; n int tot, head[maxn << 1], n, m, a[maxn << 1], b, maxdep, xzynb, son[maxn << 1], n     size, ans[maxn << 1]; n template <typename Tp> n inline void read(Tp& o) { n   o = 0; n   register int f = 1; n   register char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     o = (o << 1) + (o << 3) + (c ^ 48); n     c = getchar(); n   } n   o *= f; n } n inline void print(int x) { n   if (x < 0) { n     x = -x; n     putchar( - ); n   } n   if (x > 9) { n     print(x / 10); n   } n   putchar(x % 10 + 48); n } n struct qwq { n   int to; n   int next; n } s[maxn << 1]; n inline void add(int x, int y) { n   tot++; n   s[tot].to = y; n   s[tot].next = head[x]; n   head[x] = tot; n } n inline void dfs(int x, int fa) { n   son[x] = 1; n   ans[x] = x; n   int res = 0; n   for (register int i = head[x]; i; i = s[i].next) { n     if (s[i].to != fa) { n       dfs(s[i].to, x); n       son[x] += son[s[i].to]; n       if (son[s[i].to] > son[res]) res = s[i].to; n     } n   } n   if ((son[res] << 1) > son[x]) { n     int temp = ans[res]; n     while (((son[x] - son[temp]) << 1) > son[x]) { n       temp = a[temp]; n     } n     ans[x] = temp; n   } n } n int main() { n   read(n), read(m); n   for (register int i = 2; i <= n; ++i) { n     read(a[i]); n     add(i, a[i]), add(a[i], i); n   } n   dfs(1, 0); n   for (register int i = 1; i <= m; ++i) { n     read(b); n     print(ans[b]), puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct atom { n   int x, y; n } x[110000]; n int A[800], f[2][800], n, m; n int compare(atom k1, atom k2) { return k1.y < k2.y; } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) scanf( %d%d , &x[i].x, &x[i].y); n   sort(x + 1, x + m + 1, compare); n   memset(f, 0x00, sizeof f); n   int now = 0, lim = 780; n   for (int i = 1; i <= n; i++) { n     memset(A, 0x00, sizeof A); n     memset(f[now ^ 1], 0x3f, sizeof f[now ^ 1]); n     while (m && x[m].y == n - i + 1) { n       A[min(n - x[m].x + 1, lim + 1)]++; n       m--; n     } n     for (int j = lim; j; j--) A[j] += A[j + 1]; n     for (int j = min(lim, i); j >= 0; j--) n       f[now ^ 1][j] = f[now][max(j - 1, 0)] + A[j + 1] * 3; n     for (int j = 0; j <= min(i, lim); j++) n       f[now ^ 1][0] = n           min(f[now ^ 1][0], n               f[now][max(j - 1, 0)] + j * (j + 1) / 2 + 2 + A[j + 1] * 3); n     for (int j = 1; j <= min(i, lim); j++) n       f[now ^ 1][j] = min(f[now ^ 1][j], f[now ^ 1][j - 1]); n     now ^= 1; n   } n   cout << f[now][0] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n const int MX = 1e6 + 5; n long long MD = 1e9 + 7; n const int MDL = 99824453; n auto time0 = chrono::high_resolution_clock::now(); n long long a[MX], LPF[MX]; n long long bin_power(long long a, long long b, long long md) { n   long long res = 1; n   while (b) { n     if (b & 1) res = (res * a) % md; n     a = (a * a) % md; n     b >>= 1; n   } n   return res; n } n long long lcm(long long n) { n   map<long long, long long> mp; n   for (long long i = 1; i <= n; i++) { n     long long cur = a[i], curf = LPF[cur], cnt = 0; n     while (cur > 1) { n       cur /= LPF[cur]; n       cnt++; n       if (LPF[cur] != curf) { n         mp[curf] = max(mp[curf], cnt); n         curf = LPF[cur]; n         cnt = 0; n       } n     } n   } n   long long ans = 1; n   for (auto x : mp) ans = (ans * bin_power(x.first, x.second, MD)) % MD; n   return ans; n } n void solve() { n   long long n, k; n   cin >> n >> k; n   if (k == 1) { n     cout <<  Yes ; n     return; n   } n   bool flag = false; n   for (long long i = 1; i <= n; i++) cin >> a[i]; n   MD = k; n   long long L = lcm(n); n   if (L != 0) n     cout <<  No ; n   else n     cout <<  Yes ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   srand(time(0)); n   time0 = chrono::high_resolution_clock::now(); n   for (long long i = 1; i <= MX - 1; i++) LPF[i] = i; n   for (long long i = 2; i <= MX - 1; i++) { n     if (LPF[i] == i) { n       for (int j = 2 * i; j < MX; j += i) n         if (LPF[j] == j) LPF[j] = i; n     } n   } n   solve(); n   cerr <<  Execution Time:   n        << chrono::duration_cast<chrono::nanoseconds>( n               chrono::high_resolution_clock::now() - time0) n                   .count() * n               1e-9 n        <<   sec n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char no[20]; n   cin >> no; n   int len = strlen(no); n   int ans = (1 << len) - 2; n   for (int i = (len - 1), pos = 0; i >= 0; i--, pos++) { n     if (no[i] ==  7 ) ans += (1 << pos); n   } n   cout << ans + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void Read(int &x) { n   int f = 1; n   char c = getchar(); n   x = 0; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 3) + (x << 1) + c -  0 ; n     c = getchar(); n   } n   x *= f; n } n const int p = 1000000007; n int n, m; n inline int Pow(int a, int b) { n   int res = 1; n   while (b) { n     if (b & 1) res = 1ll * a * res % p; n     a = 1ll * a * a % p, b >>= 1; n   } n   return res; n } n int main() { n   Read(n), Read(m); n   cout << 1ll * Pow(2, m) * Pow(n + 1, m - 1) % p * (n - m + 1) % p << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int minu = 1, temp = 0, n, ans = 0; n     cin >> n; n     ans = ((n) * (n + 1)) / 2; n     while (minu <= n) { n       temp += minu; n       minu *= 2; n     } n     ans -= (2 * temp); n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void dbg(T t) { n   cerr << t << endl; n } n template <typename T, typename... Args> n void dbg(T t, Args... args) { n   cerr << t <<    ; n   dbg(args...); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int n, a, spec = 3; n   int t[2] = {1, 2}; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a; n     if (spec == a) { n       cout <<  NO  << endl; n       return 0; n     } n     for (int j = 0; j < 2; j++) n       if (t[j] != a) swap(t[j], spec); n   } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   string str; n   cin >> str; n   int n = str.size(); n   vector<int> arr(26, 1e8); n   for (int i = 0; i < n; i++) { n     arr[str[i] -  0  - 49] = min(arr[str[i] -  0  - 49], i); n   } n   cout <<  Mike n ; n   bool ch = true; n   for (int i = 1; i < n; i++) { n     for (int j = 0; j < str[i] -  0  - 49; j++) { n       ch = false; n       if (arr[j] < i) { n         cout <<  Ann n ; n         break; n       } n       if (j == str[i] -  0  - 50) { n         cout <<  Mike n ; n       } n     } n     if (ch) { n       cout <<  Mike n ; n     } n     ch = true; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long b, long long e) { n   long long p = 1; n   while (e > 0) { n     if (e & 1) { n       p = (p * b) % 1000000007; n     } n     e = e >> 1; n     b = (b * b) % 1000000007; n   } n   return p; n } n vector<string> num; n string s, s1; n int main() { n   long long n, i, t, x = 0, j, m, c, q, ans = 0, l; n   scanf( %lld , &n); n   cin >> s; n   for (i = 0; i < 10; i++) { n     for (j = 0; j < n; j++) { n       s1 = s; n       for (l = 0; l < n; l++) { n         s1[l] = (char)((s[(l + j) % n] - 48 + i) % 10 + 48); n       } n       num.push_back(s1); n     } n   } n   sort((num).begin(), (num).end()); n   cout << num[0] <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long> ydidicreate; n int dontknowy[100000]; n int main() { n   int tt; n   cin >> tt; n   while (tt--) { n     int n, k; n     cin >> n >> k; n     int a[n]; n     for (int i = 0; i < n; i++) cin >> a[i]; n     ; n     sort(a, a + n); n     int f[k]; n     for (int i = 0; i < k; i++) cin >> f[i]; n     ; n     sort(f, f + k); n     long long ans = 0; n     int l = n - 1, m = 0; n     int pos; n     for (int i = 0; i < k; i++) { n       if (f[i] == 1) { n         ans += (2 * a[l]); n         l--; n       } else { n         pos = i; n         break; n       } n     } n     for (int i = k - 1; i >= 0; i--) { n       if (f[i] == 1) break; n       ans += a[l]; n       l--; n       ans += a[m]; n       m++; n       f[i] -= 2; n       while (f[i]--) { n         m++; n       } n     } n     cout << ans <<   n ; n   } n } n