#include <bits/stdc++.h> n using namespace std; n long long modpow(long long a, long long b, n                  long long mod = (long long)(1e9 + 7)) { n   if (!b) return 1; n   a %= mod; n   return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod; n } n mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); n const int mxn = 1e6; n const long long infty = 1e18; n int n, a, b, p; n int x[mxn]; n vector<int> primes; n long long dp[mxn][3]; n long long cost(int i) { n   return x[i] % p ? (x[i] % p == 1 || x[i] % p + 1 == p ? b : infty) : 0; n } n long long dpsolve() { n   for (int i = 0; i < n; i++) n     for (int j = 0; j < 3; j++) dp[i][j] = infty; n   dp[0][0] = dp[0][2] = cost(0); n   dp[0][1] = a; n   for (int i = 0; i < n - 1; i++) n     for (int j = 0; j < 3; j++) n       if (dp[i][j] < infty) n         for (int k = j; k <= 2; k++) { n           if (k == 1) n             (dp[i + 1][k]) = min((dp[i + 1][k]), (dp[i][j] + a)); n           else n             (dp[i + 1][k]) = min((dp[i + 1][k]), (dp[i][j] + cost(i + 1))); n         } n   long long ans = infty; n   for (int k = 0; k < 3; k++) (ans) = min((ans), (dp[n - 1][k])); n   return ans; n } n inline void getPrime(int temp) { n   for (long long p = 2; p * p <= temp; p++) n     if (temp % p == 0) { n       primes.push_back(p); n       while (temp % p == 0) temp /= p; n     } n   if (temp > 1) primes.push_back(temp); n } n void solve() { n   scanf( %d , &n); n   scanf( %d , &a); n   scanf( %d , &b); n   for (int i = 0; i < n; i++) scanf( %d , &x[i]); n   for (int d = -1; d <= 1; d++) getPrime(x[0] + d), getPrime(x[n - 1] + d); n   long long ans = infty; n   for (int pp : primes) p = pp, (ans) = min((ans), (dpsolve())); n   cout << ans << endl; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   long long T; n   cin >> T; n   while (T--) { n     long long n; n     cin >> n; n     long long ans = 0; n     long long one = 0, two = 0; n     for (int i = 1; i <= n; i++) { n       long long x; n       cin >> x; n       x %= 3; n       if (x == 0) ans++; n       if (x == 1) one++; n       if (x == 2) two++; n     } n     if (one >= two) { n       ans += two; n       ans += (one - two) / 3; n     } else { n       ans += one; n       ans += (two - one) / 3; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int mx = (1 << 30) - 1; n int n, m; n struct node { n   vector<pair<int, int> > p; n   void cal(int a, int b) { n     int res = a ^ b; n     for (int i = 0; i < 30; i++) { n       int t = res ^ (1 << i) ^ (res & ((1 << i) - 1)); n       if ((t ^ a) > b) p.emplace_back(t, t + (1 << i) - 1); n     } n   } n   void get(int first, int l, int r) { n     cal(first, r); n     cal(mx ^ first, mx ^ l); n   } n }; n void solve() { n   node temp; n   scanf( %d%d , &n, &m); n   temp.get(n, 0, m); n   sort(temp.p.begin(), temp.p.end()); n   printf( %d n , temp.p[0].first); n } n int main() { n   int t; n   scanf( %d , &t); n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e5 + 10; n inline long long rd() { n   long long x = 0; n   char c = getchar(); n   int neg = 1; n   while (c <  0  || c >  9 ) { n     if (c ==  - ) neg = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) x = x * 10 + c -  0 , c = getchar(); n   return x * neg; n } n int N, Q, pre[maxn], A[maxn], tmp[maxn], ans[maxn]; n pair<int, int> mn[maxn << 2]; n struct Node { n   int l, r, i; n } que[maxn]; n inline bool cmp(Node a, Node b) { return a.r < b.r; }; n inline void update(int p) { mn[p] = min(mn[p << 1], mn[p << 1 | 1]); } n void change(int p, int l, int r, int x, int y) { n   if (l == r) n     mn[p] = make_pair(y, x); n   else { n     int m = l + r >> 1; n     if (x <= m) n       change(p << 1, l, m, x, y); n     else n       change(p << 1 | 1, m + 1, r, x, y); n     update(p); n   } n } n pair<int, int> query(int p, int l, int r, int x, int y) { n   if (x <= l && r <= y) return mn[p]; n   int m = l + r >> 1; n   pair<int, int> re = make_pair(N + 1, 0); n   if (x <= m) re = query(p << 1, l, m, x, y); n   if (y >= m + 1) re = min(re, query(p << 1 | 1, m + 1, r, x, y)); n   return re; n } n int main() { n   int i, j, k; n   N = rd(); n   for (i = 1; i <= N; i++) { n     A[i] = rd(); n     pre[i] = tmp[A[i]], tmp[A[i]] = i; n   } n   Q = rd(); n   for (i = 1; i <= Q; i++) { n     que[i].l = rd(), que[i].r = rd(), que[i].i = i; n   } n   sort(que + 1, que + Q + 1, cmp); n   memset(mn, 127, sizeof(mn)); n   for (i = 1, j = 1; i <= Q; i++) { n     for (; j <= que[i].r && j <= N; j++) { n       if (pre[j]) change(1, 1, N, pre[j], N + 1); n       change(1, 1, N, j, pre[j]); n     } n     pair<int, int> re = query(1, 1, N, que[i].l, que[i].r); n     if (re.first < que[i].l) ans[que[i].i] = A[re.second]; n   } n   for (i = 1; i <= Q; i++) printf( %d n , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma warning(disable : 4996) n const int N = 2e5 + 5, L = 19; n string s; n int cl[L][N], sf[L][N], ct[N], pos[N], lcp[N], t[L][N], g[N]; n pair<int, int> a[N + N]; n int *suf = sf[L - 1]; n int que(int l, int r) { n   int k = g[r - l + 1]; n   return min(t[k][l], t[k][r - (1 << k) + 1]); n } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   for (int i = 2; i < N; i++) g[i] = g[i >> 1] + 1; n   int n, q; n   cin >> n >> q >> s; n   s +=  # ; n   n++; n   for (int i = 0; i < n; i++) { n     ct[s[i]]++; n   } n   for (int i = 0; i < N - 1; i++) { n     ct[i + 1] += ct[i]; n   } n   for (int i = n - 1; i >= 0; i--) { n     sf[0][--ct[s[i]]] = i; n     cl[0][i] = s[i]; n   } n   for (int i = 1; i < L; i++) { n     for (int j = 0; j < n; j++) { n       sf[i - 1][j] = (sf[i - 1][j] - (1 << (i - 1)) % n + n) % n; n     } n     fill(ct, ct + N, 0); n     for (int j = 0; j < n; j++) { n       ct[cl[i - 1][sf[i - 1][j]]]++; n     } n     for (int j = 0; j < N - 1; j++) { n       ct[j + 1] += ct[j]; n     } n     for (int j = n - 1; j >= 0; j--) { n       sf[i][--ct[cl[i - 1][sf[i - 1][j]]]] = sf[i - 1][j]; n     } n     for (int j = 0, k = 0; j < n; j++) { n       cl[i][sf[i][j]] = k; n       if (j + 1 < n && (cl[i - 1][sf[i][j]] != cl[i - 1][sf[i][j + 1]] || n                         cl[i - 1][(sf[i][j] + (1 << (i - 1))) % n] != n                             cl[i - 1][(sf[i][j + 1] + (1 << (i - 1))) % n])) { n         k++; n       } n     } n   } n   for (int i = 0; i < n; i++) { n     pos[suf[i]] = i; n   } n   for (int i = 0, j = 0; i < n; i++) { n     int p = pos[i]; n     if (p == n - 1) { n       j = 0; n       continue; n     } n     int q = suf[p + 1]; n     while (i + j < n && q + j < n && s[i + j] == s[q + j]) { n       j++; n     } n     lcp[p] = j; n     j = max(0, j - 1); n   } n   for (int i = 0; i < n; i++) { n     t[0][i] = lcp[i]; n   } n   for (int i = 1; i < L; i++) { n     for (int j = 0; j + (1 << i) <= n; j++) { n       t[i][j] = min(t[i - 1][j], t[i - 1][j + (1 << (i - 1))]); n     } n   } n   for (int i = 0; i < q; i++) { n     int la, lb, ls; n     cin >> la >> lb; n     ls = 0; n     for (int j = 0; j < la; j++) { n       cin >> a[ls].first; n       a[ls].first--; n       a[ls++].second = 1; n     } n     for (int j = 0; j < lb; j++) { n       cin >> a[ls].first; n       a[ls].first--; n       a[ls++].second = 0; n     } n     sort(a, a + ls, [](pair<int, int> &x, pair<int, int> &y) { n       if (pos[x.first] == pos[y.first]) return x.second < y.second; n       return pos[x.first] < pos[y.first]; n     }); n     long long ans = 0; n     for (int j = 0; j < ls - 1; j++) { n       if (a[j].first == a[j + 1].first) ans += n - 1 - a[j].first; n     } n     vector<pair<int, int>> v; n     v.push_back({-1, 0}); n     long long sum = 0; n     for (int j = 1; j < ls; j++) { n       if (a[j - 1].first != a[j].first) { n         int h = que(pos[a[j - 1].first], pos[a[j].first] - 1), n             x = a[j - 1].second; n         while (v.back().first >= h) { n           x += v.back().second; n           sum -= v.back().first * (long long)v.back().second; n           v.pop_back(); n         } n         v.push_back({h, x}); n         sum += x * (long long)h; n       } n       if (!a[j].second) ans += sum; n     } n     for (int j = 0; j < ls - 1; j++) { n       if (a[j].first == a[j + 1].first) swap(a[j], a[j + 1]); n     } n     v.clear(); n     v.push_back({-1, 0}); n     sum = 0; n     for (int j = ls - 2; j >= 0; j--) { n       if (a[j + 1].first != a[j].first) { n         int h = que(pos[a[j].first], pos[a[j + 1].first] - 1), n             x = a[j + 1].second; n         while (v.back().first >= h) { n           x += v.back().second; n           sum -= v.back().first * (long long)v.back().second; n           v.pop_back(); n         } n         v.push_back({h, x}); n         sum += x * (long long)h; n       } n       if (!a[j].second) ans += sum; n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double f[100]; n int k[100]; n double cpw[100]; n int N; n double calc(int i) { return (1 - cpw[i] * f[i]) / (1 - cpw[i]); } n int main() { n   cin >> N; n   int c; n   int highInc = 0; n   double P = 1; n   for (int i = 0; i < N; i++) { n     cin >> c; n     cpw[i] = f[i] = 1.0 - (c / 100.0); n     k[i] = 1; n     if (calc(i) > calc(highInc)) highInc = i; n     P *= (1 - f[i]); n   } n   double expval = 0; n   double oldP; n   for (int i = 1; i <= 300000; i++) { n     oldP = P; n     P *= calc(highInc); n     k[highInc]++; n     cpw[highInc] *= f[highInc]; n     expval += i * (P - oldP); n     for (int j = 0; j < N; j++) n       if (calc(j) > calc(highInc)) highInc = j; n   } n   cout << fixed << setprecision(20) << expval + N <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int oo = 0x3f3f3f3f; n const int SIZE = 4000050; n const int P = 67; n const int maxn = 20; n int a[maxn], b[maxn]; n int F[260]; n char str[10]; n struct HASH { n   int head[SIZE], next[SIZE]; n   unsigned long long state[SIZE]; n   double dp[SIZE]; n   int tol; n   void init() { n     memset(head, -1, sizeof head); n     tol = 0; n   } n   double find(unsigned long long s) { n     for (int i = head[s % SIZE]; i != -1; i = next[i]) { n       if (state[i] == s) return dp[i]; n     } n     return -1; n   } n   bool ins(unsigned long long s, double f) { n     if (find(s) > -1) return false; n     dp[++tol] = f; n     state[tol] = s; n     next[tol] = head[s % SIZE]; n     head[s % SIZE] = tol; n     return true; n   } n } mat; n unsigned long long Hash(int n) { n   for (int i = 1; i <= n; i++) b[i] = a[i]; n   sort(b + 1, b + n); n   unsigned long long s = 0; n   for (int i = 1; i <= n; i++) { n     s *= P; n     s += b[i]; n   } n   return s; n } n inline void set_ST(int st[], int x) { n   st[0] = (x >> 4) & 3; n   st[1] = (x >> 2) & 3; n   st[2] = x & 3; n } n inline int get_ST(int a, int b, int c) { n   if (a > c) swap(a, c); n   return (((a << 2) + b) << 2) + c; n } n double dfs(int n) { n   unsigned long long s = Hash(n); n   double f = mat.find(s), c[4] = {0, 1e9, 1e9, 1e9}; n   if (f > -1) return f; n   for (int i = 1; i < n; i++) { n     int x[3], y[3], nn = n; n     int ai = a[i], an = a[n]; n     set_ST(x, ai); n     set_ST(y, an); n     if ((!!x[0]) + (!!x[1]) + (!!x[2]) == 1) continue; n     if (y[0] && y[1] && y[2]) { n       nn++; n       y[0] = y[1] = y[2] = 0; n     } n     for (int j = 0; j < 3; j++) n       if (x[j]) { n         if ((!!x[0]) + (!!x[1]) + (!!x[2]) == 2 && (j == 1 || !x[1])) continue; n         int t = x[j]; n         x[j] = 0; n         a[i] = get_ST(x[0], x[1], x[2]); n         if ((!!x[0]) + (!!x[1]) + (!!x[2]) == 1 || !x[1]) a[i] = 0; n         x[j] = t; n         for (int k = 0; k < 3; k++) n           if (!y[k]) { n             y[k] = t; n             a[nn] = get_ST(y[0], y[1], y[2]); n             y[k] = 0; n             c[t] = min(c[t], dfs(nn)); n           } n       } n     a[i] = ai; n     a[n] = an; n   } n   if (c[1] == 1e9 && c[2] == 1e9 && c[3] == 1e9) { n     mat.ins(s, 0); n     return 0.0; n   } n   double p = 1.0 / 6.0; n   if (c[1] == 1e9) p += 1.0 / 3.0, c[1] = 0; n   if (c[2] == 1e9) p += 1.0 / 3.0, c[2] = 0; n   if (c[3] == 1e9) p += 1.0 / 6.0, c[3] = 0; n   f = (c[1] / 3.0 + c[2] / 3.0 + c[3] / 6.0 + 1.0) / (1.0 - p); n   mat.ins(s, f); n   return f; n } n int main() { n   int n; n   F[ G ] = 1; n   F[ B ] = 2; n   F[ R ] = 3; n   mat.init(); n   while (scanf( %d , &n) != EOF) { n     for (int i = 1; i <= n; i++) { n       scanf( %s , str); n       a[i] = get_ST(F[str[0]], F[str[1]], F[str[2]]); n     } n     printf( %.15lf n , dfs(n)); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   vector<int> a(101); n   for (int i = 0; i < n; i++) { n     int x; n     cin >> x; n     ++a[x]; n   } n   cout << *max_element(a.begin(), a.end()) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, k; n vector<int> v; n void solve() { n   if (k == 1) { n     for (int i = 0; 1 + i * (n - 1) <= n + m; i++) v.push_back(1 + i * (n - 1)); n     return; n   } n   for (int i = 1; i <= k; i++) v.push_back(1); n   v.push_back(n); n   for (int i = 1; i < k; i++) v.push_back(n + 1); n   if (n == m) v.push_back(2 * n); n } n int main() { n   scanf( %d %d %d , &n, &m, &k); n   solve(); n   printf( %d n , v.size()); n   for (int i = 0; i < v.size(); i++) n     printf( %d%c , v[i], i < v.size() - 1 ?     :   n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long; n using pii = pair<int, int>; n void solve() { n   int n, b, d; n   cin >> n >> b >> d; n   int res = 0; n   int currMass = 0; n   for (int i = 0; i < n; i++) { n     int got; n     cin >> got; n     if (got > b) continue; n     currMass += got; n     if (currMass > d) { n       currMass = 0; n       res++; n     } n   } n   cout << res; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int dp[2005][2005]; n int main() { n   int n, i, k1, j, k; n   cin >> n >> k1; n   long long int ans = 0; n   dp[0][1] = 1; n   for (i = 1; i <= k1; i++) { n     for (j = 1; j <= n; j++) { n       for (k = j; k <= n; k += j) { n         dp[i][k] += dp[i - 1][j]; n         dp[i][k] %= 1000000007; n       } n     } n   } n   for (j = 1; j <= n; j++) { n     ans += dp[k1][j]; n     ans %= 1000000007; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n int cnt = 0; n void rec(vector<bool> mask, int i, int ops) { n   if (cnt == n) return; n   if (i == n) { n     for (int j = 0; j < n; j++) cout << char( (  + mask[j]); n     for (int j = 0; j < n; j++) { n       if (ops > 0) { n         cout <<  ) ; n         ops--; n       } else { n         cout <<  ( ; n         ops++; n       } n     } n     cout << endl; n     cnt++; n   } else { n     mask[i] = 0; n     rec(mask, i + 1, ops + 1); n     if (ops > 0) { n       mask[i] = 1; n       rec(mask, i + 1, ops - 1); n     } n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int T; n   cin >> T; n   while (T--) { n     cin >> n; n     cnt = 0; n     vector<bool> t(n, 0); n     rec(t, 0, 0); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = (int)1e4 + 22; n int n, m; n void solve() { n   int l, r; n   cin >> n >> m; n   int val = INT_MAX; n   for (int i = 0; i < m; i++) { n     cin >> l >> r; n     val = min(val, r - l + 1); n   } n   cout << val <<   n ; n   for (int i = 0; i < n; i++) cout << i % val <<    ; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> x; n int modulo_sum(int a, int b) { n   return ((a % 1000000007) + (b % 1000000007)) % 1000000007; n } n int modulo_subtract(int a, int b) { n   return ((a % 1000000007) - (b % 1000000007)) % 1000000007; n } n int modulo_mult(int a, int b) { return ((long long)a * b) % 1000000007; } n int power(int n) { n   int ini = 2; n   int ret = 1; n   while (n >= 1) { n     if (n % 2 == 1) ret = modulo_mult(ret, ini); n     ini = modulo_mult(ini, ini); n     n /= 2; n   } n   return ret; n } n int powerr[300005]; n int main() { n   int n; n   cin >> n; n   powerr[0] = 1; n   for (int i = 1; i <= n; i++) { n     powerr[i] = modulo_sum(powerr[i - 1], powerr[i - 1]); n     int apl; n     cin >> apl; n     x.push_back(apl); n   } n   x.push_back(0); n   sort(x.begin(), x.end()); n   int ans = 0; n   for (int i = 1; i <= n; i++) { n     ans = modulo_sum( n         ans, n         modulo_mult(modulo_sum(powerr[i - 1], (-1) * powerr[n - i]), x[i])); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   for (int i = n + 1; i <= m; i++) { n     int count = 0; n     for (int j = 2; j < i; j++) { n       if (i % j == 0) count++; n     } n     if (count == 0) { n       if (m == i) { n         cout <<  YES  << endl; n         return 0; n       } else n         break; n     } n   } n   cout <<  NO  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, S, top, pos[2005], id[2005]; n struct point { n   long long x, y; n } a[2005]; n struct line { n   long long x, y; n   double k; n } b[4000005]; n inline bool cmp1(point A, point B) { n   return A.x != B.x ? A.x < B.x : A.y < B.y; n } n inline bool cmp2(line A, line B) { return A.k < B.k; } n inline long long cross(point A, point B) { n   return abs((A.x * B.y) - (B.x * A.y)); n } n inline point operator-(point A, point B) { n   return (point){A.x - B.x, A.y - B.y}; n } n inline long long getS(point A, point B, point C) { n   return abs(cross(B - A, C - A)); n } n inline long long read() { n   long long ret = 0, ff = 1; n   char ch = getchar(); n   while (!isdigit(ch)) { n     if (ch ==  - ) ff = -1; n     ch = getchar(); n   } n   while (isdigit(ch)) { n     ret = (ret << 3) + (ret << 1) + (ch ^ 48); n     ch = getchar(); n   } n   return ret * ff; n } n signed main() { n   n = read(); n   S = read() * 2; n   for (long long i = 1; i <= n; i++) { n     a[i].x = read(); n     a[i].y = read(); n     pos[i] = id[i] = i; n   } n   sort(a + 1, a + n + 1, cmp1); n   for (long long i = 1; i <= n; i++) n     for (long long j = i + 1; j <= n; j++) n       b[++top] = n           (line){i, j, atan2((double)(a[j] - a[i]).y, (double)(a[j] - a[i]).x)}; n   sort(b + 1, b + top + 1, cmp2); n   for (long long i = 1; i <= top; i++) { n     long long x = b[i].x, y = b[i].y; n     if (pos[x] > pos[y]) swap(x, y); n     long long l, r, t; n     l = 1, r = pos[x] - 1; n     while (l <= r) { n       long long mid = (l + r) >> 1; n       if (getS(a[id[mid]], a[x], a[y]) >= S) n         l = mid + 1, t = mid; n       else n         r = mid - 1; n     } n     if (getS(a[id[t]], a[x], a[y]) == S) { n       puts( Yes ); n       printf( %lld %lld n , a[id[t]].x, a[id[t]].y); n       printf( %lld %lld n , a[x].x, a[x].y); n       printf( %lld %lld n , a[y].x, a[y].y); n       return 0; n     } n     l = pos[y] + 1, r = n; n     while (l <= r) { n       long long mid = (l + r) >> 1; n       if (getS(a[id[mid]], a[x], a[y]) >= S) n         r = mid - 1, t = mid; n       else n         l = mid + 1; n     } n     if (getS(a[id[t]], a[x], a[y]) == S) { n       puts( Yes ); n       printf( %lld %lld n , a[id[t]].x, a[id[t]].y); n       printf( %lld %lld n , a[x].x, a[x].y); n       printf( %lld %lld n , a[y].x, a[y].y); n       return 0; n     } n     swap(pos[x], pos[y]); n     swap(id[pos[x]], id[pos[y]]); n   } n   printf( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 5e6; n int a[maxn], ans; n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   while (n--) { n     int tmp; n     cin >> tmp; n     a[tmp]++; n   } n   for (int i = 0; i < maxn; i++) { n     a[i + 1] += a[i] / 2; n     ans += a[i] % 2; 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 int main() { n   long long int tc; n   cin >> tc; n   while (tc--) { n     long long int n; n     cin >> n; n     while (n--) cout << 1 <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, S[2]; n int num[1505][1505][2]; n bool s[1505][1505]; n int p[4][2] = {0, -1, 1, 0, 0, 1, -1, 0}; n void dfs(int x, int y) { n   if (num[x % n][y % m][0] == -1 && num[x % n][y % m][1] == -1) { n     num[x % n][y % m][0] = x; n     num[x % n][y % m][1] = y; n   } else if (num[x % n][y % m][0] != x || num[x % n][y % m][1] != y) { n     printf( Yes n ); n     exit(0); n   } else n     return; n   for (int i = 0; i < 4; i++) { n     if (s[(x + p[i][0]) % n][(y + p[i][1]) % m]) dfs(x + p[i][0], y + p[i][1]); n   } n } n int main() { n   scanf( %d %d , &n, &m); n   char ch; n   getchar(); n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       ch = getchar(); n       if (ch ==  . ) s[i][j] = true; n       if (ch ==  S ) { n         s[i][j] = true; n         S[0] = i + n * m; n         S[1] = j + n * m; n       } n     } n     getchar(); n   } n   memset(num, -1, sizeof(num)); n   dfs(S[0], S[1]); n   printf( No n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 100100, SQRT = 320, MAXM = 13; n int N, M, Q; n pair<int, int> up[MAXN][MAXM]; n pair<int, int> jump[MAXN][MAXM]; n char grid[MAXN][MAXM]; n bool bounded(int x, int y) { return 1 <= x && x <= N && 1 <= y && y <= M; } n bool bounded(pair<int, int> p) { return bounded(p.first, p.second); } n pair<int, int> getup(int x, int y) { n   pair<int, int> &ref = up[x][y]; n   if (ref.first != -2) { n     return ref; n   } n   if (!bounded(x, y)) { n     return ref = pair<int, int>(x, y); n   } n   if (grid[x][y] ==  ^ ) { n     return ref = pair<int, int>(x - 1, y); n   } else if (grid[x][y] ==  < ) { n     return ref = (grid[x][y - 1] ==  >  ? pair<int, int>(-1, -1) n                                         : getup(x, y - 1)); n   } else { n     return ref = (grid[x][y + 1] ==  <  ? pair<int, int>(-1, -1) n                                         : getup(x, y + 1)); n   } n } n void calcjump(int i) { n   if (i % SQRT == 1) { n     copy_n(up[i] + 1, M, jump[i] + 1); n   } else { n     for (int j = 1; j <= M; j++) { n       pair<int, int> p = up[i][j]; n       jump[i][j] = bounded(p) ? jump[p.first][p.second] : p; n     } n   } n } n int main() { n   if (fopen( input.txt ,  r )) { n     freopen( input.txt ,  r , stdin); n   } n   ios_base::sync_with_stdio(false); n   scanf( %d %d %d n , &N, &M, &Q); n   for (int i = 1; i <= N; i++) { n     gets(grid[i] + 1); n     fill_n(up[i], M + 2, pair<int, int>(-2, -2)); n   } n   for (int i = 0; i <= N; i++) { n     for (int j = 0; j <= M + 1; j++) { n       getup(i, j); n     } n   } n   for (int i = 1; i <= N; i++) { n     calcjump(i); n   } n   char ch; n   for (int x, y; Q--;) { n     scanf( %c %d %d , &ch, &x, &y); n     if (ch ==  A ) { n       pair<int, int> p(x, y); n       while (bounded(p)) { n         p = jump[p.first][p.second]; n       } n       printf( %d %d n , p.first, p.second); n     } else { n       scanf(  %c , &ch); n       grid[x][y] = ch; n       fill_n(up[x] + 1, M, pair<int, int>(-2, -2)); n       for (int j = 1; j <= M; j++) { n         getup(x, j); n       } n       for (calcjump(x++); x <= N && x % SQRT != 1; x++) { n         calcjump(x); n       } n     } n     getchar(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int>> tree; n int res = 0; n int dfs(int node, int parent) { n   int left = 2; n   for (int child : tree[node]) { n     if (child == parent) { n       continue; n     } n     int x = dfs(child, node); n     if (x == 1 && left > 0) { n       ++res; n       --left; n     } n   } n   return left > 0; n } n int main() { n   int n, x, y; n   cin >> n >> x >> y; n   vector<int> degs(n, 0); n   tree.resize(n); n   for (int i = 0; i < n - 1; ++i) { n     int a, b; n     cin >> a >> b; n     --a; n     --b; n     tree[a].push_back(b); n     tree[b].push_back(a); n     ++degs[a]; n     ++degs[b]; n   } n   if (x < y) { n     dfs(0, -1); n     cout << res * (long long)x + (n - res - 1) * (long long)y <<   n ; n   } else { n     if (*max_element(degs.begin(), degs.end()) == n - 1) { n       cout << (long long)(n - 2) * y + x <<   n ; n     } else { n       cout << (long long)(n - 1) * y <<   n ; n     } n   } n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n int64_t n; n vector<int64_t> adj[100001]; n int64_t ss[100001]; n void dfs(int64_t no, int64_t par = -1) { n   ss[no] = 1; n   for (auto j : adj[no]) { n     if (j == par) { n       continue; n     } n     dfs(j, no); n     ss[no] += ss[j]; n   } n } n vector<int64_t> ord; n int64_t dfs2(int64_t no, int64_t par = -1) { n   for (auto j : adj[no]) { n     if (j == par) { n       continue; n     } n     if (ss[j] > n / 2) { n       return dfs2(j, no); n     } n   } n   return no; n } n int64_t ans = 0; n void dfs3(int64_t no, int64_t par = -1, int64_t levv = 0) { n   ord.push_back(no); n   ans += levv * 2; n   for (auto j : adj[no]) { n     if (j == par) { n       continue; n     } n     dfs3(j, no, levv + 1); n   } n } n int64_t ans2[100001]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (int64_t i = 0; i < n - 1; i++) { n     int64_t aa, bb; n     cin >> aa >> bb; n     aa--; n     bb--; n     adj[aa].push_back(bb); n     adj[bb].push_back(aa); n   } n   dfs(0); n   int64_t cen = dfs2(0); n   dfs3(cen); n   for (int64_t i = 0; i < n; i++) { n     ord.push_back(ord[i]); n   } n   int64_t x = (n / 2); n   cout << ans << endl; n   for (int64_t i = 0; i < n; i++) { n     ans2[ord[i]] = ord[i + x] + 1; n   } n   for (int64_t i = 0; i < n; i++) { n     cout << ans2[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 400005; n multiset<int> S; n int x, y, n, m, k; n map<pair<int, int>, int> flag; n struct Tree { n   int l, r, num, flag; n } T[N * 4]; n void build(int x, int l, int r) { n   T[x].l = l; n   T[x].r = r; n   if (l == r) { n     T[x].num = l - 1; n     return; n   } n   int mid = (l + r) / 2; n   build(x * 2, l, mid); n   build(x * 2 + 1, mid + 1, r); n   T[x].num = max(T[x * 2].num, T[x * 2 + 1].num); n } n void down(int x) { n   T[x * 2].num += T[x].flag; n   T[x * 2 + 1].num += T[x].flag; n   T[x * 2].flag += T[x].flag; n   T[x * 2 + 1].flag += T[x].flag; n   T[x].flag = 0; n } n void insert(int x, int l, int r, int z) { n   if (T[x].l > r || l > T[x].r) return; n   if (T[x].l >= l && T[x].r <= r) { n     T[x].num += z; n     T[x].flag += z; n     return; n   } n   down(x); n   insert(x * 2, l, r, z); n   insert(x * 2 + 1, l, r, z); n   T[x].num = max(T[x * 2].num, T[x * 2 + 1].num); n } n int find(int x, int l, int r) { n   if (T[x].l > r || l > T[x].r) return 0; n   if (T[x].l >= l && T[x].r <= r) return T[x].num; n   down(x); n   return max(find(x * 2, l, r), find(x * 2 + 1, l, r)); n } n int main() { n   scanf( %d%d%d , &n, &k, &m); n   build(1, 1, n + n); n   S.insert(n + 1); n   while (m--) { n     scanf( %d%d , &x, &y); n     if (flag[make_pair(x, y)]) { n       S.erase(S.find(y + abs(x - k))); n       flag[make_pair(x, y)] = 0; n       insert(1, 1, y + abs(x - k), -1); n     } else { n       S.insert(y + abs(x - k)); n       flag[make_pair(x, y)] = 1; n       insert(1, 1, y + abs(x - k), 1); n     } n     printf( %d n , find(1, 1, *S.rbegin()) - n); n   } n } n


#include <bits/stdc++.h> n int main(int argc, const char* argv[]) { n   int n, k; n   int a[100000 + 1]; n   scanf( %d%d , &n, &k); n   int i; n   int count = 0; n   int is_ukn = scanf( %d , a) != 1; n   for (i = !is_ukn; i <= n; i++) n     scanf( %d , a + i) != 1 && getchar() && count++; n   int is_man_move = (n + 1 - count) & 1; n   int is_man_end = n & 1; n   if (k == 0) { n     puts((is_ukn && is_man_move) || (!is_ukn && a[0] == 0) ?  Yes  :  No ); n     return 0; n   } n   int some_ukn = is_ukn || count; n   if (some_ukn) { n     puts(is_man_end ?  Yes  :  No ); n     return 0; n   } n   int sum2 = 0; n   for (i = 0; i <= n && sum2 % k == 0; i++) { n     sum2 /= k; n     sum2 += a[i]; n   } n   if (i == n + 1) { n     if (k != 1 && k != -1) n       while (sum2 != 0 && sum2 % k == 0) sum2 /= k; n     if (sum2 == 0) { n       puts( Yes ); n       return 0; n     } n   } n   puts( No ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n inline int f(int a) { n   int n1 = n; n   bool y = 0; n   while (n1 != 0) { n     int h = a; n     int u = n1 % 10; n     n1 = n1 / 10; n     while (h != 0) { n       int j = h % 10; n       h = h / 10; n       if (u == j) { n         y = 1; n         break; n       } n     } n   } n   return y; n } n int main() { n   cin >> n; n   int ans = 0; n   for (int i = 1; i * i <= n; i++) { n     if (n % i == 0) { n       int i2 = n / i; n       if (f(i) == 1) ans++; n       if (f(i2) == 1 && i2 != i) ans++; n     } n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, d = 1; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   cin >> n >> m; n   while (n > d) { n     for (int i = 1; i < (m + 1); i++) { n       cout << d <<     << i <<   n ; n       cout << n <<     << m + 1 - i <<   n ; n     } n     n--; n     d++; n   } n   if (n == d) { n     for (int i = 0; i < (m / 2); i++) { n       cout << d <<     << 1 + i <<   n ; n       cout << d <<     << m - i <<   n ; n     } n     if (m & 1) cout << d <<     << (m + 1) / 2 <<   n ; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1073741824 ) n #pragma warning(disable : 4996) n using namespace std; n const int N = 1e5 + 100; n pair<int, int> ar[N]; n int col[N], us[5]; n int ct(int m) { n   int c = 0; n   while (m) { n     c += m & 1; n     m >>= 1; n   } n   return c; n } n int main() { n   int n, h; n   scanf( %d%d , &n, &h); n   for (int i = 0; i < n; i++) { n     scanf( %d , &ar[i].first); n     ar[i].second = i; n   } n   if (n < 8) { n     int ans = 2e9, t; n     for (int mk = 0; mk < (1 << n); mk++) { n       int mx = -1, mn = 2e9; n       for (int i = 0; i < n; i++) { n         for (int j = i + 1; j < n; j++) { n           int d = ar[i].first + ar[j].first; n           if (((mk >> i) & 1) != ((mk >> j) & 1)) d += h; n           mx = max(mx, d); n           mn = min(mn, d); n         } n       } n       if (ans > mx - mn) { n         ans = mx - mn; n         t = mk; n       } n     } n     printf( %d n , ans); n     for (int i = 0; i < n; i++) printf( %d  , ((t >> i) & 1) + 1); n     return 0; n   } n   sort(ar, ar + n); n   int ans = ar[n - 1].first + ar[n - 2].first - ar[0].first - ar[1].first, n       s = -1, f; n   for (int g = 0; g < (1 << 4); g++) { n     int mn = 2e9, mx = -1, c = ct(g); n     if (c != 2) continue; n     for (int a = 0; a < 4; a++) { n       for (int b = a + 1; b < 4; b++) { n         int d = ar[a].first + ar[b].first; n         if (((g >> a) & 1) != ((g >> b) & 1)) d += h; n         mx = max(mx, d); n         mn = min(mn, d); n       } n     } n     for (int k = g == 0; k < (1 << 4); k++) { n       c = ct(k); n       if (c != 2) continue; n       int cmx = mx, cmn = mn; n       for (int a = 0; a < 4; a++) { n         for (int b = a + 1; b < 4; b++) { n           int d = ar[n - 4 + a].first + ar[n - 4 + b].first; n           if (((k >> a) & 1) != ((k >> b) & 1)) d += h; n           cmn = min(cmn, d); n           cmx = max(cmx, d); n         } n       } n       if (ans > cmx - cmn) { n         ans = cmx - cmn; n         f = g; n         s = k; n       } n     } n   } n   int cans = 2e9, ind = -1; n   for (int i = 0; i < n; i++) { n     int j = 0, d = 0, mx = -1, mn = 2e9; n     while (j == i) j++; n     mn = min(mn, ar[i].first + ar[j].first + h); n     d += ar[j].first; n     j++; n     while (j == i) j++; n     d += ar[j].first; n     mn = min(d, mn); n     d = 0; n     j = n - 1; n     while (j == i) j--; n     mx = max(mx, ar[i].first + ar[j].first + h); n     d += ar[j].first; n     j--; n     while (j == i) j--; n     d += ar[j].first; n     mx = max(d, mx); n     if (cans > mx - mn) { n       cans = mx - mn; n       ind = i; n     } n   } n   if (cans < ans) { n     printf( %d n , cans); n     ind = ar[ind].second; n     for (int i = 0; i < n; i++) { n       printf( %d  , i == ind ? 2 : 1); n     } n     return 0; n   } n   if (s != -1) { n     for (int i = 0; i < 4; i++) { n       col[ar[i].second] = (f >> i) & 1; n       us[ar[i].second] = 1; n     } n     for (int i = 0; i < 4; i++) { n       col[ar[n - 4 + i].second] = (s >> i) & 1; n       us[col[ar[n - 4 + i].second]] = n           us[col[ar[n - 4 + i].second]] == 1 ? 2 : 1; n     } n     if (us[0] == 2) n       for (int i = 5; i < n - 4; i++) col[ar[i].second] = 0; n     else n       for (int i = 5; i < n - 4; i++) col[ar[i].second] = 1; n   } n   printf( %d n , ans); n   for (int i = 0; i < n; i++) printf( %d  , col[i] + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int MOD = 1e9 + 7; n const int INF = 0x3f3f3f3f; n pair<long long, long long> ans1[N], ans2[N]; n long long x; n long long cal(long long n) { n   n--; n   long long tmp = n * (n + 1) * (2 * n + 1) / 6; n   n++; n   tmp -= (n * n * n - n * n) / 2; n   if (2 * (x - tmp) % (n * n + n)) return -1; n   long long m = 2 * (x - tmp) / (n * n + n); n   return m; n } n void solve() { n   int cnt = 0, tot = 0; n   for (int i = 1; i < 1500000; i++) { n     long long m = cal((long long)i); n     if (m == -1) continue; n     if (m < i) break; n     ans1[tot++] = make_pair(i, m); n     if (m == i) break; n     ans2[cnt++] = make_pair(m, i); n   } n   printf( %d n , tot + cnt); n   for (int i = 0; i < tot; i++) n     printf( %I64d %I64d n , ans1[i].first, ans1[i].second); n   for (int i = cnt - 1; i >= 0; i--) n     printf( %I64d %I64d n , ans2[i].first, ans2[i].second); n } n int main() { n   scanf( %I64d , &x); n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n int main() { n   int n, m; n   int left, right; n   int i; n   int count = 0; n   scanf( %d %d , &n, &m); n   while (n--) { n     for (i = 0; i < m; i++) { n       scanf( %d %d , &left, &right); n       if (left == 1 || right == 1) { n         count++; n       } n     } n   } n   printf( %d n , count); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n struct P { n   int t, id; n   bool operator<(const P &tmp) const { n     if (t != tmp.t) return t < tmp.t; n     return id < tmp.id; n   } n } p[N]; n priority_queue<int, vector<int>, greater<int> > qu; n queue<int> q; n set<int> s; n long long ans[N]; n int n, t; n long long now = 0, ed = -1, nid = 0; n void gao() { n   if (qu.empty()) return; n   int x = qu.top(); n   if (!s.empty() && (*s.begin()) < x) return; n   if (q.empty()) ed += t; n   qu.pop(); n   q.push(x); n   s.insert(x); n } n int main() { n   scanf( %d%d , &n, &t); n   for (int i = 0; i < n; i++) scanf( %d , &p[i].t), p[i].id = i; n   sort(p, p + n); n   while (true) { n     if (nid < n && (ed >= p[nid].t || q.empty())) { n       now = p[nid].t; n       if (!s.empty() && (*s.begin()) < p[nid].id) n         qu.push(p[nid].id); n       else if (ed == p[nid].t) n         qu.push(p[nid].id); n       else { n         if (q.empty()) ed = now + t; n         q.push(p[nid].id); n         s.insert(p[nid].id); n       } n       nid++; n     } else if (!q.empty()) { n       int x = q.front(); n       q.pop(); n       now = max(now, ed); n       s.erase(x); n       ans[x] = ed; n       if (!q.empty()) ed += t; n       gao(); n     } else n       break; n   } n   for (int i = 0; i < n; i++) printf( %lld  , ans[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string b[1 << 15]; n int x[100], y[100], n; n map<string, int> mp; n int main() { n   scanf( %d , &n); n   for (int i = 0, z; i < n; i++) { n     scanf( %d , &z); n     x[i] = z & ((1 << 15) - 1); n     y[i] = z >> 15; n   } n   for (int B = 0; B < (1 << 15); B++) { n     for (int i = 1, j = __builtin_popcount(B ^ y[0]); i < n; i++) { n       b[B].push_back(__builtin_popcount(B ^ y[i]) - j); n     } n     if (!mp.count(b[B])) mp[b[B]] = B; n   } n   for (int B = 0; B < 1 << 15; B++) { n     string tmp; n     for (int i = 1, j = __builtin_popcount(B ^ x[0]); i < n; i++) { n       tmp.push_back(j - __builtin_popcount(B ^ x[i])); n     } n     if (mp.count(tmp)) { n       printf( %d n , B + (mp[tmp] << 15)); n       return 0; n     } n   } n   puts( -1 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n char a[505][505]; n long long int ans, mod; n namespace tree { n int edge[505][505]; n long long int a[505][505]; n inline void init() { memset(edge, 0, sizeof(edge)); } n inline long long int qpow(long long int x, long long int y) { n   long long int ans = 1, base = x; n   while (y) { n     if (y & 1) ans = ans * base % mod; n     base = base * base % mod; n     y >>= 1; n   } n   return ans; n } n inline long long int solve(int n) { n   memset(a, 0, sizeof(a)); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= n; ++j) { n       (a[i][j] -= edge[i][j]) %= mod; n       (a[i][i] += edge[i][j]) %= mod; n     } n   long long int ans = 1; n   for (int i = 1; i < n; ++i) { n     int pos = i; n     while (pos < n && !a[pos][i]) ++pos; n     if (pos >= n) continue; n     if (pos != i) ans = mod - ans, swap(a[pos], a[i]); n     for (int j = i + 1; j < n; ++j) { n       long long int d = qpow(a[i][i], mod - 2) * a[j][i] % mod; n       for (int k = 1; k < n; ++k) a[j][k] = (a[j][k] - a[i][k] * d % mod) % mod; n     } n   } n   for (int i = 1; i < n; ++i) ans = ans * a[i][i] % mod; n   return (ans % mod + mod) % mod; n } n }  // namespace tree n struct UFO { n   int fa[123456]; n   inline void init() { n     for (int i = 0; i < 123456; ++i) fa[i] = i; n   } n   int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } n   inline void merge(int x, int y) { fa[find(x)] = find(y); } n } U; n const int maxn = 1E6 + 5; n inline void clear() { U.init(); } n inline int num(int x, int y) { return x * (m + 1) + y + 1; } n inline int c(int x) { n   if (m % 2 == 1) return (x + (x - 1) / (m + 1)) & 1; n   return x & 1; n } n int tot, bel[maxn]; n inline void solve1() { n   clear(); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       if (a[i][j] ==     ) n         U.merge(num(i - 1, j - 1), num(i, j)); n       else if (a[i][j] ==  / ) n         U.merge(num(i - 1, j), num(i, j - 1)); n   for (int i = 1; i <= (n + 1) * (m + 1); ++i) n     if (c(i) == 0 && (!bel[U.find(i)])) bel[U.find(i)] = ++tot; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) { n       if (a[i][j] !=  * ) continue; n       int u = num(i - 1, j - 1), v = num(i, j); n       if (c(num(i - 1, j - 1)) == 1) u = num(i, j - 1), v = num(i - 1, j); n       if (u > v) swap(u, v); n       u = bel[U.find(u)], v = bel[U.find(v)]; n       if (u == v) continue; n       ++tree::edge[u][v]; n       ++tree::edge[v][u]; n     } n   ans = (ans + tree::solve(tot)) % mod; n } n inline void solve2() { n   clear(); n   tree::init(); n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) n       if (a[i][j] ==     ) n         U.merge(num(i - 1, j - 1), num(i, j)); n       else if (a[i][j] ==  / ) n         U.merge(num(i - 1, j), num(i, j - 1)); n   tot = 0; n   memset(bel, 0, sizeof(bel)); n   for (int i = 1; i <= (n + 1) * (m + 1); ++i) n     if (c(i) == 1 && (!bel[U.find(i)])) bel[U.find(i)] = ++tot; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) { n       if (a[i][j] !=  * ) continue; n       int u = num(i - 1, j - 1), v = num(i, j); n       if (c(num(i - 1, j - 1)) == 0) u = num(i, j - 1), v = num(i - 1, j); n       if (u > v) swap(u, v); n       u = bel[U.find(u)], v = bel[U.find(v)]; n       if (u == v) continue; n       ++tree::edge[u][v]; n       ++tree::edge[v][u]; n     } n   ans = (ans + tree::solve(tot)) % mod; n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m >> mod; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) cin >> a[i][j]; n   solve1(); n   solve2(); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int ni() { n   int a; n   scanf( %d , &a); n   return a; n } n const int max_n = 5000; n char s1[max_n + 1], s2[max_n + 1], res[max_n + 1]; n int digits[26], minuses[26]; n bool check(int from, int code, int n, int m) { n   bool ok = true, founded = false; n   digits[code]--; n   memset(minuses, 0, sizeof(minuses)); n   for (int i = from; i < m; i++) { n     int next = -1, need = s2[i] -  a ; n     for (int j = 25; j >= 0; j--) { n       if (j >= need && digits[j] > 0) { n         next = j; n         break; n       } n     } n     if (next == -1) { n       ok = false; n       break; n     } else if (next > need) { n       founded = true; n       break; n     } else { n       digits[next]--; n       minuses[next]++; n     } n   } n   for (int i = 0; i < int(26); i++) digits[i] += minuses[i]; n   digits[code]++; n   if (n <= m && !founded) ok = false; n   return ok; n } n int main() { n   scanf( %s , s1); n   scanf( %s , s2); n   int n = strlen(s1), m = strlen(s2); n   bool found = true, before = false; n   for (int i = 0; i < int(n); i++) digits[s1[i] -  a ]++; n   for (int i = 0; i < int(n); i++) { n     if (i >= m || before) { n       int code = -1; n       for (int j = 0; j < int(26); j++) { n         if (digits[j] > 0) { n           code = j; n           break; n         } n       } n       res[i] =  a  + code; n       digits[code]--; n     } else { n       int same = s2[i] -  a , next = -1; n       for (int j = 0; j < int(26); j++) { n         if (j > same && digits[j] > 0) { n           next = j; n           break; n         } n       } n       if (digits[same] > 0 && check(i + 1, same, n, m)) { n         digits[same]--; n         res[i] =  a  + same; n       } else if (next != -1) { n         digits[next]--; n         res[i] =  a  + next; n         before = true; n       } else { n         found = false; n         break; n       } n     } n   } n   if (found) { n     res[n] =   0 ; n     printf( %s n , res); n   } else { n     printf( -1 n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int intersection = n * m; n   int turn = 0; n   while (n > 1 && m > 1) { n     intersection -= (n + m - 1); n     n--; n     m--; n     turn++; n   } n   if (n == 1 && m == 1 && turn % 2 == 0) { n     cout <<  Akshat  << endl; n   } else if (n == 1 && m == 1 && turn % 2 != 0) { n     cout <<  Malvika  << endl; n   } else if ((n != 1 || m != 1) && turn % 2 == 0) { n     cout <<  Akshat  << endl; n   } else { n     cout <<  Malvika  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline long long in() { n   int32_t x; n   scanf( %d , &x); n   return x; n } n const long long maxn = 1e5 + 10; n const long long mod = 1e9 + 7; n const long long inf = 1e9 + 10; n long long pref[2][maxn], pref2[maxn]; n long long maxDp[maxn], minDp[maxn]; n int32_t main() { n   long long n = in(); n   string s; n   cin >> s; n   long long lenT = in(); n   long long cntA = (lenT + 1) / 2; n   long long cntB = lenT / 2; n   for (long long i = 0; i < n; i++) { n     pref[0][i + 1] = (i ? pref[0][i - 1] : 0) + (s[i] !=  b ); n     if (i - cntA * 2 >= 0) pref[0][i + 1] -= (s[i - cntA * 2] !=  b ); n     pref[1][i + 1] = (i ? pref[1][i - 1] : 0) + (s[i] !=  a ); n     if (i - cntB * 2 >= 0) pref[1][i + 1] -= (s[i - cntB * 2] !=  a ); n     pref2[i + 1] = n         pref2[i] + (s[i] ==  ? ) - (i >= lenT ? (s[i - lenT] ==  ? ) : 0); n   } n   for (long long i = 0; i < n; i++) { n     if (i + 1 < lenT) continue; n     maxDp[i + 1] = maxDp[i]; n     long long value = maxDp[i - lenT + 1]; n     bool fl = (pref[1][i + 1 * bool(lenT % 2 == 0)] + n                pref[0][i + 1 * bool(lenT % 2)]) == lenT; n     value += fl; n     maxDp[i + 1] = max(maxDp[i + 1], value); n     long long options[2] = {inf, inf}; n     if (maxDp[i + 1] == maxDp[i]) options[0] = minDp[i]; n     if (fl) options[1] = minDp[i - lenT + 1] + pref2[i + 1]; n     minDp[i + 1] = min(options[0], options[1]); n   } n   cout << minDp[n] <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n int stuff(int x) { n   int s = 0; n   while (x / 10 != 0) { n     int r = x % 10; n     x /= 10; n     s += r; n   } n   s += x; n   return s; n } n int main() { n   ios_base::sync_with_stdio(false); n   long long int i = 0; n   long long int p, f, a, b, c, s, spow = 1; n   long long int arr[1000]; n   cin >> a >> b >> c; n   f = a; n   for (s = 1; s <= 81; s++) { n     f = a; n     spow = 1; n     while (f--) { n       spow *= s; n     } n     p = (b * spow) + c; n     if (stuff(p) == s && p < 1000000000) { n       arr[i] = p; n       i++; n     } n   } n   sort(arr, arr + i); n   cout << i <<   n ; n   for (int j = 0; j < i; j++) { n     cout << arr[j] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   for (int k = 0; k < t; k++) { n     int n, total = 0; n     cin >> n; n     while (n / 10 > 0) { n       total = total + (n - (n % 10)); n       n = ((n - (n % 10)) / 10) + n % 10; n     } n     cout << total + (n % 10) <<    ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class T> n void max_self(T &a, const T &b) { n   if (a < b) a = b; n } n template <class T> n void min_self(T &a, const T &b) { n   if (a > b) a = b; n } n const int INF = 1e9 + 7; n const double PI = acos(-1.0); n const double EPS = (1e-9); n int main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int N, d; n   long long cnt = 0; n   cin >> N >> d; n   vector<int> pos(N); n   for (int &ele : pos) cin >> ele; n   for (auto itr = pos.begin(); itr != pos.end(); ++itr) { n     auto lim = upper_bound((pos).begin(), (pos).end(), *itr + d); n     int len = distance(itr, lim) - 1; n     cnt += (long long)len * (len - 1) / 2; n   } n   cout << cnt <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void init() { n   ios::sync_with_stdio(0); n   cin.tie(0); n } n int main() { n   init(); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     long long arr[n][m]; n     for (int i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         cin >> arr[i][j]; n       } n     } n     long long ops = 0; n     for (long long i = 0; i < n; i++) { n       for (int j = 0; j < m; j++) { n         long long a[4]; n         a[0] = arr[i][j]; n         a[1] = arr[n - 1 - i][m - 1 - j]; n         a[2] = arr[i][m - 1 - j]; n         a[3] = arr[n - 1 - i][j]; n         sort(a, a + 4); n         long long m = (a[1] + a[2]) / 2; n         for (int ii = 0; ii < 4; ii++) { n           ops += abs(a[ii] - (m)); n         } n       } n     } n     ops /= 4; n     cout << ops << endl; n   } n } n


#include <bits/stdc++.h> n #pragma GCC optimize( Ofast ) n #pragma GCC target( avx,avx2,fma ) n #pragma GCC optimization( unroll-loops ) n using namespace std; n const int maxn = 3e5 + 5; n const long long mod = 998244353; n const double eps = 1e-6L; n struct node { n   int l, r; n   bool operator<(const node& k) const { n     if (l == k.l) return r > k.r; n     return l < k.l; n   } n } a[maxn]; n long long fac[maxn], f[maxn], inv[maxn]; n void init() { n   fac[1] = 1; n   f[1] = 1; n   inv[1] = 1; n   for (long long i = 2; i <= maxn; i++) { n     fac[i] = fac[i - 1] * i % mod; n     f[i] = (mod - mod / i) * f[mod % i] % mod; n     inv[i] = inv[i - 1] * f[i] % mod; n   } n } n long long C(long long m, long long n) { n   if (m < n) return 0; n   if (n == 0 || m == n) return 1; n   return fac[m] * inv[n] % mod * inv[m - n] % mod; n } n int b[maxn]; n int main() { n   int T = 1; n   init(); n   while (T--) { n     int n, k, m = 1; n     scanf( %d %d , &n, &k); n     for (int i = 0; i < n; i++) { n       scanf( %d %d , &a[i].l, &a[i].r); n       b[i] = a[i].r; n     } n     sort(a, a + n); n     sort(b, b + n); n     int left = a[0].l, right = a[0].r; n     long long ans = 0; n     bool flag = true; n     for (int i = n - 1; i >= 0; i--) { n       int num = i + 1 - (lower_bound(b, b + n, a[i].l) - b); n       ans = (ans + C(num - 1, k - 1)) % mod; n     } n     printf( %lld n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long N; n void solve() { n   int i, j, k, l, r, x, y; n   string s; n   cin >> N; n   long long ret = 0; n   long long a = 1; n   while (N > 1) { n     ret += N / 2 * a; n     N = (N + 1) >> 1; n     a <<= 1; n   } n   cout << ret << endl; n } n int main() { n   solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200008; n int n, k, l, r; n char g[2][maxn]; n bool f[2][maxn]; n struct arr { n   int t, s, x; n } h[maxn]; n bool Add(int t, int s, int x) { n   if (t >= x) return 0; n   if (x > n) return 1; n   if (f[s][x]) return 0; n   if (g[s][x] ==  X ) return 0; n   f[s][x] = 1; n   ++r; n   h[r].t = t; n   h[r].s = s; n   h[r].x = x; n   return 0; n } n bool Work() { n   h[1].t = 0; n   h[1].s = 0; n   h[1].x = 1; n   l = 1, r = 1; n   f[0][1] = 1; n   while (l <= r) { n     int t = h[l].t, s = h[l].s, x = h[l].x; n     ++l; n     if (Add(t + 1, s, x + 1)) return 1; n     if (Add(t + 1, s, x - 1)) return 1; n     if (Add(t + 1, s ^ 1, x + k)) return 1; n   } n   return 0; n } n int main() { n   scanf( %d%d%*c , &n, &k); n   scanf( %s , &g[0][1]); n   scanf( %s , &g[1][1]); n   if (Work()) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int n, m, a, b; n   cin >> n >> m >> a >> b; n   int ans = 1000000000; n   for (int i = 0; i <= n / m + 1; i++) { n     ans = min(ans, i * b + max(n - m * i, 0) * a); n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 5e2 + 5; n vector<pair<int, int> > adj[MAX]; n int dis[MAX][MAX], in[MAX][MAX]; n int n, m; n void dijkstra(int source) { n   memset(dis[source], 0x3F, sizeof dis[source]); n   dis[source][source] = 0; n   priority_queue<pair<int, int>, vector<pair<int, int> >, n                  greater<pair<int, int> > > n       pq; n   pq.emplace(dis[source][source], source); n   while (!pq.empty()) { n     pair<int, int> cur = pq.top(); n     pq.pop(); n     int w, v; n     tie(w, v) = cur; n     if (w > dis[source][v]) { n       continue; n     } n     for (auto &each : adj[v]) { n       int vv, ww; n       tie(vv, ww) = each; n       if (dis[source][vv] > w + ww) { n         dis[source][vv] = w + ww; n         in[source][vv] = 1; n         pq.emplace(dis[source][vv], vv); n       } else if (dis[source][vv] == w + ww) { n         in[source][vv]++; n       } n     } n   } n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = int(0); i < int(m); i++) { n     int u, v, w; n     scanf( %d %d %d , &u, &v, &w); n     adj[u].emplace_back(v, w); n     adj[v].emplace_back(u, w); n   } n   for (int i = int(1); i < int(n + 1); i++) { n     dijkstra(i); n   } n   for (int i = int(1); i < int(n + 1); i++) { n     for (int j = int(i + 1); j < int(n + 1); j++) { n       int total = 0; n       for (int k = int(1); k < int(n + 1); k++) { n         if (dis[i][k] + dis[k][j] == dis[i][j]) { n           total += in[i][k]; n         } n       } n       printf( %d  , total); n     } n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n using namespace std::chrono; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   if (n % 2 == 0) { n     cout <<  NO  << endl; n     return 0; n   } n   cout <<  YES  << endl; n   int arr[2 * n + 5]; n   for (int i = 1, j = 2 * n; i <= n; i++) { n     if (i % 2 == 1) { n       arr[i] = j; n       j--; n       arr[i + n] = j; n       j--; n     } else { n       arr[i + n] = j; n       j--; n       arr[i] = j; n       j--; n     } n   } n   for (int i = 1; i <= 2 * n; i++) { n     cout << arr[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class II, class SZ, class OI> n OI cpy_n(II _i, SZ _s, OI _o) { n   for (int_fast64_t _j = 1; _j <= _s; _j++) { n     *(_o++) = *_i; n     if (_j != _s) { n       _i++; n     } n   }; n   return _o; n } n template <class T> n void slurp(vector<T>& v, int_least32_t n = 1000000000) { n   istream_iterator<T> i(cin); n   istream_iterator<T> eoi; n   ; n   if (n == 1000000000) { n     copy((i), (eoi), back_inserter((v))); n   } else { n     v.reserve(n); n     cpy_n(i, n, back_inserter((v))); n   } n } n template <class T, class U> n void slurp2(vector<T>& va, vector<U>& vb, int_least32_t n = 1000000000) { n   istream_iterator<T> a(cin); n   istream_iterator<T> eoa; n   ; n   istream_iterator<U> b(cin); n   istream_iterator<U> eob; n   ; n   if (n != 1000000000) { n     va.reserve(n); n     vb.reserve(n); n   } n   auto ao = back_inserter((va)); n   auto bo = back_inserter((vb)); n   for (int_fast64_t i = 1; i <= n; i++) { n     if (a == eoa) break; n     *(ao++) = *a; n     *(bo++) = *b; n     if (i != n) { n       a++; n       b++; n     } n   } n } n chrono::time_point<chrono::system_clock> start_time; n void do_thing(vector<string>& bs, int_fast64_t i, int_fast64_t j) { n   for (int_fast64_t int_least32_t = (i); int_least32_t <= (i + 2); n        int_least32_t++) { n     if (bs[int_least32_t][j + 1] !=  . ) { n       return; n     } n   } n   for (int_fast64_t jj = (j); jj <= (j + 2); jj++) { n     if (bs[i + 1][jj] !=  . ) { n       return; n     } n   } n   for (int_fast64_t int_least32_t = (i); int_least32_t <= (i + 2); n        int_least32_t++) { n     bs[int_least32_t][j + 1] =  # ; n   } n   for (int_fast64_t jj = (j); jj <= (j + 2); jj++) { n     bs[i + 1][jj] =  # ; n   } n } n void do_it() { n   int_fast64_t n; n   cin >> n; n   vector<string> bs; n   slurp(bs, n); n   for (int_fast64_t i = (0); i <= (n - 3); i++) { n     for (int_fast64_t j = (0); j <= (n - 3); j++) { n       do_thing(bs, i, j); n     } n   } n   for (int_fast64_t i = (0); i <= (n - 1); i++) { n     for (int_fast64_t j = (0); j <= (n - 1); j++) { n       if (bs[i][j] !=  # ) { n         cout <<  NO  << endl; n         return; n       } n     } n   } n   cout <<  YES  << endl; n } n void start() { start_time = chrono::system_clock::now(); } n void end() { n   chrono::time_point<chrono::system_clock> end_time; n   end_time = chrono::system_clock::now(); n   chrono::duration<double> diff = end_time - start_time; n   cerr << fixed << setprecision(4) << diff.count() <<  s  << endl; n } n void do_mult() { n   int_least32_t times; n   cin >> times; n   for (int_fast64_t i = 1; i <= times; i++) { n     do_it(); n   } n } n int main() { n   ios::sync_with_stdio(false); n   start(); n   do_it(); n   end(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int prime = 999983; n const int INF = 0x7FFFFFFF; n const long long INFF = 0x7FFFFFFFFFFFFFFF; n const double pi = acos(-1.0); n const double inf = 1e18; n const double eps = 1e-6; n const long long mod = 1e9 + 7; n long long qpow(long long a, long long b) { n   long long s = 1; n   while (b > 0) { n     if (b & 1) s = s * a % mod; n     a = a * a % mod; n     b >>= 1; n   } n   return s; n } n long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } n int dr[2][4] = {1, -1, 0, 0, 0, 0, -1, 1}; n using namespace std; n const int N = 2e5 + 5; n int n, m, q, to[N << 1], nxt[N << 1], ww[N << 1], head[N]; n int vis[N], o[N], len, pre[N], id[N], mrk[N]; n long long dis[N], sid[N], tr[N << 2]; n vector<int> E[N]; n priority_queue<pair<long long, int>, vector<pair<long long, int>>, n                greater<pair<long long, int>>> n     Q; n void spfa(int s) { n   memset(dis, 63, sizeof(dis)); n   memset(vis, 0, sizeof(vis)); n   Q.push(make_pair(dis[s] = 0, s)); n   pre[s] = 0; n   while (!Q.empty()) { n     int u = Q.top().second; n     Q.pop(); n     if (vis[u]) continue; n     vis[u] = 1; n     for (int e = head[u]; e; e = nxt[e]) { n       int v = to[e], w = ww[e]; n       if (dis[v] > dis[u] + w) n         pre[v] = e, Q.push(make_pair(dis[v] = dis[u] + w, v)); n     } n   } n } n void dfs(int u) { n   for (int v : E[u]) { n     if (!id[v]) id[v] = id[u]; n     dfs(v); n   } n } n void modify(int x, int l, int r, int ql, int qr, long long v) { n   if (l >= ql && r <= qr) { n     tr[x] = min(tr[x], v); n     return; n   } n   int mid = (l + r) >> 1; n   if (ql <= mid) modify(x << 1, l, mid, ql, qr, v); n   if (qr > mid) modify(x << 1 | 1, mid + 1, r, ql, qr, v); n } n long long query(int x, int l, int r, int p) { n   if (l == r) return tr[x]; n   int mid = (l + r) >> 1; n   if (p <= mid) n     return min(tr[x], query(x << 1, l, mid, p)); n   else n     return min(tr[x], query(x << 1 | 1, mid + 1, r, p)); n } n int main() { n   ios::sync_with_stdio(false); n   cin >> n >> m >> q; n   for (int i = 1, j = 1; i <= m; ++i) { n     int u, v, w; n     cin >> u >> v >> w; n     to[++j] = v; n     nxt[j] = head[u]; n     ww[j] = w; n     head[u] = j; n     to[++j] = u; n     nxt[j] = head[v]; n     ww[j] = w; n     head[v] = j; n   } n   spfa(n); n   memcpy(sid, dis, sizeof(sid)); n   spfa(1); n   for (int i = n; i > 1; i = to[pre[i] ^ 1]) { n     o[++len] = i, mrk[pre[i] >> 1] = 1; n   } n   o[++len] = 1, reverse(o + 1, o + len + 1); n   for (int i = 1; i <= len; ++i) { n     id[o[i]] = i; n   } n   for (int i = 2; i <= n; ++i) n     if (pre[i]) { n       E[to[pre[i] ^ 1]].push_back(i); n     } n   dfs(1); n   memset(tr, 63, sizeof(tr)); n   for (int i = 1; i <= n; ++i) { n     for (int e = head[i]; e; e = nxt[e]) { n       int u = i, v = to[e]; n       if (id[u] >= id[v] || mrk[e >> 1]) continue; n       modify(1, 1, len, id[u], id[v] - 1, dis[u] + ww[e] + sid[v]); n     } n   } n   while (q--) { n     int x, y; n     cin >> x >> y; n     x <<= 1; n     if (mrk[x >> 1]) { n       if (id[to[x]] > id[to[x ^ 1]]) x ^= 1; n       printf( %lld n , min(dis[n] - ww[x] + y, query(1, 1, len, id[to[x]]))); n     } else { n       printf( %lld n , min(dis[n], min(dis[to[x]] + sid[to[x ^ 1]], n                                        dis[to[x ^ 1]] + sid[to[x]]) + n                                        y)); n     } n   } n } n


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


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n const int MOD = 1e9 + 7; n const int dx[4] = {0, 0, -1, 1}; n const int dy[4] = {-1, 1, 0, 0}; n const int N = 1e5 + 1; n int n; n long long a[N], s[N], p[N], k; n int ans = INF; n bool Check(int val) { n   long long ans = k + 1; n   for (int l = 1, r = 1; l <= n; l++) { n     while (r <= n && a[r] - a[l] <= val) r++; n     r--; n     long long x = (a[l] * l - p[l]) + (s[r + 1] - (a[l] + val) * (n - r)); n     ans = min(ans, x); n   } n   for (int r = n, l = n; r > 0; r--) { n     while (l > 0 && a[r] - a[l] <= val) l--; n     l++; n     long long x = n         (s[r] - a[r] * (n - r + 1)) + ((a[r] - val) * (l - 1) - p[l - 1]); n     ans = min(ans, x); n   } n   return (ans <= k); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   sort(a + 1, a + 1 + n); n   for (int i = 1; i <= n; i++) p[i] = ((i == 1) ? a[i] : p[i - 1] + a[i]); n   for (int i = n; i > 0; i--) s[i] = ((i == n) ? a[i] : s[i + 1] + a[i]); n   int l = 0; n   int r = INF; n   while (l <= r) { n     int m = (l + r) / 2; n     if (Check(m)) { n       ans = m; n       r = m - 1; n     } else n       l = m + 1; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100000]; n int b[100000]; n int c[100000]; n struct P { n   int a, b; n } p[100000]; n int cmp(P x, P y) { return x.a < y.a; } n int main() { n   int n, x; n   while (cin >> n) { n     for (int i = 1; i <= n; i++) { n       cin >> x; n       a[x] = i; n     } n     for (int i = 1; i <= n; i++) { n       cin >> x; n       b[x] = i; n       p[i - 1].a = i; n       p[i - 1].b = x; n     } n     for (int i = 1; i <= n; i++) { n       int key = a[i]; n       int l = 0, r = n, mid; n       while (l < r) { n         mid = (l + r) / 2; n         if (p[mid].a == key) { n           if (i != n) n             cout << p[mid].b <<    ; n           else n             cout << p[mid].b << endl; n           break; n         } else if (p[mid].a > key) n           r = mid; n         else n           l = mid + 1; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n int cnt[N], steps[N], vis[N]; n void solve() { n   int n, res = INT_MAX, x, y; n   cin >> n; n   for (int i = 1; i < n + 1; i++) { n     cin >> x; n     queue<pair<int, int>> q; n     q.push({x, 0}); n     while (!q.empty()) { n       x = q.front().first; n       y = q.front().second; n       q.pop(); n       if (x > N) continue; n       if (vis[x] == i) continue; n       vis[x] = i; n       steps[x] += y; n       cnt[x]++; n       q.push({2 * x, y + 1}); n       q.push({x / 2, y + 1}); n     } n   } n   for (int i = 0; i < N; i++) { n     if (cnt[i] == n) { n       if (res > steps[i]) res = steps[i]; n     } n   } n   cout << res; n } n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cerr.tie(0); n   ; n   int t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200005; n int n, k, a[N], ans = 0; n multiset<int> ms; n int main(int argc, char const *argv[]) { n   cin >> n >> k; n   for (int i = 1; i <= n; ++i) { n     cin >> a[i]; n     if (!a[i] && i != k) a[i] = n + 1; n     if (i == k && a[i]) a[i] = 0, ans = 1; n     ms.insert(a[i]); n   } n   int mex = 0; n   while (!ms.empty()) { n     if (ms.find(mex) == ms.end()) { n       int k = *ms.rbegin(); n       ms.erase(ms.find(k)); n       ans++; n     } else { n       ms.erase(mex); n     } n     mex++; n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 3e5 + 5; n int n, q; n struct Edge { n   int to, nxt; n } e[MAX_N]; n int head[MAX_N], tote; n void add_edge(int u, int v) { n   e[++tote].to = v, e[tote].nxt = head[u]; n   head[u] = tote; n } n int siz[MAX_N], fa[MAX_N], ans[MAX_N]; n void dfs(int u, int pre) { n   fa[u] = pre; n   siz[u] = 1; n   int son = -1; n   for (int i = head[u]; i; i = e[i].nxt) { n     int to = e[i].to; n     dfs(to, u); n     siz[u] += siz[to]; n     if (son == -1 || siz[to] > siz[son]) son = to; n   } n   if (son == -1) n     ans[u] = u; n   else { n     son = ans[son]; n     while ((2 * (siz[u] - siz[son])) > siz[u]) son = fa[son]; n     ans[u] = son; n   } n } n void init() { memset(head, 0, sizeof(head)), tote = 0; }; n int main() { n   init(); n   scanf( %d , &n), scanf( %d , &q); n   for (register int i = 2; i <= n; i++) { n     int x; n     scanf( %d , &x); n     add_edge(x, i); n   } n   dfs(1, 1); n   while (q--) { n     int x; n     scanf( %d , &x); n     printf( %d n , ans[x]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n const long long inff = 0x3f3f3f3f3f3f3f3f; n const int mod = 998244353; n const double eps = 1e-9; n const int maxV = 3e5 + 5; n int n, V, mV, a[maxV], tot; n long long dp[5][maxV]; n vector<long long> w[5]; n void solve(int l, int r, int L, int R, int id) { n   if (l > r) return; n   int pos = L, mid = l + r >> 1; n   for (int i = max(mid - (int)w[id].size(), L); i <= min(mid, R); i++) { n     if (dp[id][a[mid]] < n         dp[id - 1][a[i]] + (i == mid ? 0 : w[id][mid - i - 1])) { n       dp[id][a[mid]] = dp[id - 1][a[i]] + (i == mid ? 0 : w[id][mid - i - 1]); n       pos = i; n     } n   } n   solve(l, mid - 1, L, pos, id); n   solve(mid + 1, r, pos, R, id); n } n namespace fastIO { n static char buf[100000], *h = buf, *d = buf; n template <typename T> n inline void read(T& x) { n   int f = 1; n   x = 0; n   register char c( n       h == d && (d = (h = buf) + fread(buf, 1, 100000, stdin), h == d) ? EOF n                                                                        : *h++); n   while (c >  9  || c <  0 ) { n     if (c ==  - ) f = -1; n     c = h == d && (d = (h = buf) + fread(buf, 1, 100000, stdin), h == d) ? EOF n                                                                          : *h++; n   } n   while (c <=  9  && c >=  0 ) n     x = (x << 1) + (x << 3) + (c ^ 48), n     c = h == d && (d = (h = buf) + fread(buf, 1, 100000, stdin), h == d) ? EOF n                                                                          : *h++; n   x *= f; n } n template <typename T> n void output(T x) { n   if (x < 0) { n     putchar( - ); n     x = ~(x - 1); n   } n   static int s[20], top = 0; n   while (x) { n     s[++top] = x % 10; n     x /= 10; n   } n   if (!top) s[++top] = 0; n   while (top) putchar(s[top--] +  0 ); n } n }  // namespace fastIO n using fastIO::output; n using fastIO::read; n int main() { n   read(n), read(V); n   for (int i = (1); i <= (int)(n); ++i) { n     int c, w0; n     read(c), read(w0); n     w[c].push_back(w0); n     mV = max(mV, c); n   } n   for (int i = (1); i <= (int)(mV); ++i) { n     sort(w[i].begin(), w[i].end(), greater<int>()); n     for (int j = 1; j < w[i].size(); j++) { n       w[i][j] += w[i][j - 1]; n     } n   } n   for (int i = (1); i <= (int)(mV); ++i) { n     for (int j = (0); j <= (int)(i - 1); ++j) { n       tot = 0; n       for (int k = j; k <= V; k += i) { n         a[++tot] = k; n       } n       solve(1, tot, 1, tot, i); n     } n   } n   int flag = 0; n   printf( %lld n , dp[mV][V]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long max_n = 500 + 5; n long long n; n long long a[max_n], b[max_n]; n bool are_equal(long long arr1[], long long arr2[], long long sz) { n   for (int i = 0; i < sz; ++i) { n     if (arr1[i] != arr2[i]) return false; n   } n   return true; n } n bool same_elements(long long arr1[], long long arr2[], long long sz) { n   long long tarr1[sz], tarr2[sz]; n   for (int i = 0; i < sz; ++i) { n     tarr1[i] = arr1[i]; n     tarr2[i] = arr2[i]; n   } n   sort(tarr1, tarr1 + sz); n   sort(tarr2, tarr2 + sz); n   return are_equal(tarr1, tarr2, sz); n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long t; n   cin >> t; n   while (t--) { n     cin >> n; n     for (int z = 0; z < n; ++z) cin >> a[z]; n     ; n     for (int z = 0; z < n; ++z) cin >> b[z]; n     ; n     if (!same_elements(a, b, n) or (n & 1 and a[n / 2] != b[n / 2])) { n       cout <<  No n ; n       continue; n     } n     vector<pair<long long, long long> > p; n     for (int i = 0; i < (n / 2); ++i) p.push_back({a[i], a[n - 1 - i]}); n     bool flag = true; n     for (int i = 0; i < (n / 2) and flag; ++i) { n       long long x; n       bool got = false; n       pair<long long, long long> temp = {b[i], b[n - 1 - i]}; n       for (x = 0; x < p.size() and !got; ++x) { n         if (p[x] == temp) { n           p.erase(p.begin() + x); n           got = true; n         } n       } n       temp = {b[n - 1 - i], b[i]}; n       for (x = 0; x < p.size() and !got; ++x) { n         if (p[x] == temp) { n           p.erase(p.begin() + x); n           got = true; n         } n       } n       flag = got; n     } n     if (flag) n       cout <<  Yes n ; n     else n       cout <<  No n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:1024000000,1024000000 ) n using namespace std; n const double pi = acos(-1); n const double e = exp(1); n const int INF = 0x3f3f3f3f; n const int MOD = 1e9 + 7; n template <typename T> n inline void read(T &x) { n   T ans = 0; n   char last =    , ch = getchar(); n   while (ch <  0  || ch >  9 ) last = ch, ch = getchar(); n   while (ch >=  0  && ch <=  9 ) ans = ans * 10 + ch -  0 , ch = getchar(); n   if (last ==  - ) ans = -ans; n   x = ans; n } n const int maxn = 1000 + 10; n char str[maxn]; n int vis[maxn]; n int main() { n   while (~scanf( %s , str)) { n     memset(vis, 0, sizeof(vis)); n     int flag = 1; n     for (int i = 0; str[i]; i++) { n       for (int j = 0; j < str[i] -  a ; j++) { n         if (!vis[j]) { n           flag = 0; n           break; n         } n       } n       if (!flag) break; n       vis[str[i] -  a ] = 1; n     } n     puts(flag ?  YES  :  NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[105][105], v[105][105], dp[105][105]; n long long slove(int x, int y, int n, int m) { n   long long t = a[x][y] - (x + y - 2); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       v[i][j] = a[i][j] - (t + i + j - 2); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       dp[i][j] = -1; n       if (v[i][j] < 0) continue; n       if (i == 1 && j == 1) n         dp[i][j] = v[i][j]; n       else if (i == 1) { n         long long t = dp[i][j - 1]; n         if (t != -1) dp[i][j] = t + v[i][j]; n       } else if (j == 1) { n         long long t = dp[i - 1][j]; n         if (t != -1) dp[i][j] = t + v[i][j]; n       } else { n         long long t1 = dp[i - 1][j], t2 = dp[i][j - 1]; n         if (t1 != -1) dp[i][j] = t1 + v[i][j]; n         if (t2 != -1) { n           if (dp[i][j] == -1) n             dp[i][j] = t2 + v[i][j]; n           else n             dp[i][j] = min(dp[i][j], t2 + v[i][j]); n         } n       } n     } n   } n   if (dp[n][m] != -1) n     return dp[n][m]; n   else n     return 1e18; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n, m; n     cin >> n >> m; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         cin >> a[i][j]; n       } n     } n     long long ans = 1e18; n     for (int i = 1; i <= n; i++) { n       for (int j = 1; j <= m; j++) { n         ans = min(ans, slove(i, j, n, m)); n       } n     } n     cout << ans <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n const int INF = (1 << 31) - 1; n const double EPS = 1E-9; n const double PI = acos(-1); n using namespace std; n vector<bool> U; n vector<vector<int> > G; n vector<int> P, order, T; n int M[5100][5100]; n void dfs(int v) { n   U[v] = true; n   for (int i = 0; i < G[v].size(); ++i) n     if (!U[G[v][i]]) { n       P[G[v][i]] = v; n       dfs(G[v][i]); n     } n   order.push_back(v); n } n int main() { n   int n, a, b, c; n   cin >> n; n   priority_queue<pair<int, int> > Q[n]; n   G.resize(n); n   P.resize(n); n   T.resize(n); n   T.assign(n, 0); n   U.resize(n); n   U.assign(n, false); n   for (int i = 0; i < n; ++i) { n     cin >> a; n     Q[i].push(make_pair(-a, i)); n   } n   for (int i = 0; i < n - 1; ++i) { n     cin >> a >> b >> c; n     G[a - 1].push_back(b - 1); n     G[b - 1].push_back(a - 1); n     M[a - 1][b - 1] = c; n     M[b - 1][a - 1] = c; n   } n   P[0] = -1; n   dfs(0); n   int TIMER = 0; n   reverse(order.begin(), order.end()); n   while (Q[0].size() < n) { n     TIMER++; n     for (int i = 1; i < n; ++i) n       for (int j = 0; j < M[order[i]][P[order[i]]] && !Q[order[i]].empty(); n            ++j) { n         pair<int, int> v = Q[order[i]].top(); n         Q[order[i]].pop(); n         Q[P[order[i]]].push(v); n         if (P[order[i]] == 0) T[v.second] = TIMER; n       } n   } n   for (int i = 0; i < n; ++i) cout << T[i] <<    ; n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long base = 7; n const long long maxn = 1e6 + 9; n const long long inf = 1e9 + 7; n set<long long> W, H; n multiset<long long> l1, l2; n long long w, h, n, a; n long long x, y; n char c; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> w >> h >> n; n   W.insert(0); n   W.insert(w); n   l1.insert(w); n   H.insert(0); n   H.insert(h); n   l2.insert(h); n   long long maxx; n   while (n--) { n     cin >> c >> a; n     if (c ==  V ) { n       W.insert(a); n       x = *prev(W.find(a)); n       y = *next(W.find(a)); n       auto it = l1.find(y - x); n       l1.erase(it); n       l1.insert(y - a); n       l1.insert(a - x); n     } else { n       H.insert(a); n       x = *prev(H.find(a)); n       y = *next(H.find(a)); n       auto it = l2.find(y - x); n       l2.erase(it); n       l2.insert(y - a); n       l2.insert(a - x); n     } n     long long p = *prev(l1.end()); n     long long q = *prev(l2.end()); n     cout << p * q <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 2e5 + 6; n const long long MOD = 1e9 + 7; n int par[MAX]; n long long sz[MAX]; n long long triplets(long long x) { return x * (x - 1) * (x - 2); } n bool isLucky(long long x) { n   while (x) { n     if (x % 10 != 4 and x % 10 != 7) return false; n     x /= 10; n   } n   return true; n } n int Find(int u) { n   if (u == par[u]) return u; n   return par[u] = Find(par[u]); n } n void Union(int u, int v) { n   u = Find(u); n   v = Find(v); n   if (sz[u] < sz[v]) swap(u, v); n   if (u != v) { n     par[v] = u; n     sz[u] += sz[v]; n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   for (int i = 1; i <= n; i++) par[i] = i, sz[i] = 1; n   for (int i = 1; i <= n - 1; i++) { n     long long u, v, w; n     cin >> u >> v >> w; n     if (!isLucky(w)) Union(u, v); n   } n   long long ans = n * (n - 1) * (n - 2); n   for (int i = 1; i <= n; i++) { n     if (Find(i) == i) { n       ans -= 2 * sz[i] * (sz[i] - 1) * (n - sz[i]) + n              sz[i] * (sz[i] - 1) * (sz[i] - 2); n     } n   } n   cout << ans; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-10; n const double PI = acos(-1.L); n struct Point { n   double x; n   double y; n   Point() { n     x = 0; n     y = 0; n   } n   Point(double x, double y) : x(x), y(y) {} n   Point operator+(const Point& other) const { n     return Point(x + other.x, y + other.y); n   } n   Point operator-(const Point& other) const { n     return Point(x - other.x, y - other.y); n   } n   double operator*(const Point& other) const { n     return x * other.x + y * other.y; n   } n   double operator^(const Point& other) const { n     return x * other.y - y * other.x; n   } n }; n ostream& operator<<(ostream& s, Point p) { return s << p.x <<     << p.y; } n int main() { n   double px, py, vx, vy, a, b, c, d; n   cin >> px >> py >> vx >> vy >> a >> b >> c >> d; n   Point displ(px, py); n   Point t1(0, b); n   Point t2(-a / 2., 0); n   Point t3(-c / 2., 0); n   Point t4(-c / 2., -d); n   Point t5(c / 2., -d); n   Point t6(c / 2., 0); n   Point t7(a / 2., 0); n   double angle = atan2(vy, vx) - PI / 2.; n   Point rotx(cos(angle), -sin(angle)); n   Point roty(sin(angle), cos(angle)); n   cout << fixed << setprecision(12); n   Point s1(rotx * t1, roty * t1); n   s1 = s1 + displ; n   Point s2(rotx * t2, roty * t2); n   s2 = s2 + displ; n   Point s3(rotx * t3, roty * t3); n   s3 = s3 + displ; n   Point s4(rotx * t4, roty * t4); n   s4 = s4 + displ; n   Point s5(rotx * t5, roty * t5); n   s5 = s5 + displ; n   Point s6(rotx * t6, roty * t6); n   s6 = s6 + displ; n   Point s7(rotx * t7, roty * t7); n   s7 = s7 + displ; n   cout << s1 <<   n  n        << s2 <<   n  n        << s3 <<   n  n        << s4 <<   n  n        << s5 <<   n  n        << s6 <<   n  n        << s7 <<   n ; n } n


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


#include <bits/stdc++.h> n long long i, j, k, m, n; n int main() { n   scanf( %lld , &n); n   if (n <= 1) n     puts( -1 ); n   else n     printf( %lld %lld %lld n , n, n + 1, n * (n + 1)); n   return 0; n } n


#include <bits/stdc++.h> n int search(int n, long long int a[n], long long int target) { n   int i; n   if (a[0] >= target) { n     printf( 1 %lld n , target); n     return 0; n   } n   int L = 0, R = n - 1; n   int M; n   while ((R - L) > 1) { n     if ((L + R) % 2 == 0) { n       M = (L + R) / 2; n     } else { n       M = (L + R - 1) / 2; n     } n     if (a[M] < target) { n       L = M; n     } else if (a[M] == target) { n       printf( %d %lld n , M + 1, target - a[M - 1]); n       return 0; n     } else { n       R = M; n     } n   } n   if (target == a[L]) { n     printf( %d %lld n , L + 1, target); n     return 0; n   } else if (target == a[R]) { n     printf( %d %lld n , R + 1, target - a[R - 1]); n   } else { n     printf( %d %lld n , L + 2, target - a[L]); n   } n } n int main() { n   int n, m, i; n   scanf( %d %d , &n, &m); n   long long int a[n]; n   long long int b[m]; n   long long int sum = 0; n   long long int tmp; n   for (i = 0; i < n; i++) { n     scanf( %lld , &tmp); n     a[i] = tmp + sum; n     sum = tmp + sum; n   } n   for (i = 0; i < m; i++) { n     scanf( %lld , &b[i]); n   } n   for (i = 0; i < m; i++) search(n, a, b[i]); n } n


#include <bits/stdc++.h> n using namespace std; n long long gcd(long long a, long long b) { n   while (a && b) a > b ? a %= b : b %= a; n   return a + b; n } n long long lcm(long long a, long long b) { return a * b / gcd(a, b); } n int main() { n   int n; n   cin >> n; n   int i, j, sum = 0, in; n   int a[n]; n   for (i = 0; i < n; ++i) { n     cin >> a[i]; n     sum += a[i]; n   } n   int x = n / 2; n   int avg = sum / x; n   for (i = 0; i < n; i++) { n     if (a[i] == 0) continue; n     cout << i + 1 <<    ; n     for (j = i + 1; j < n; j++) { n       if (a[j] == 0) continue; n       if ((a[j] + a[i]) == avg) { n         cout << j + 1 <<    ; n         a[j] = 0; n         a[i] = 0; n         break; n       } n     } n     cout << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void _print(long long t) { cerr << t; } n void _print(string t) { cerr << t; } n void _print(char t) { cerr << t; } n void _print(long double t) { cerr << t; } n void _print(double t) { cerr << t; } n void _print(unsigned long long t) { cerr << t; } n template <class T, class V> n void _print(pair<T, V> p); n template <class T> n void _print(vector<T> v); n template <class T> n void _print(set<T> v); n template <class T, class V> n void _print(map<T, V> v); n template <class T> n void _print(multiset<T> v); n template <class T, class V> n void _print(pair<T, V> p) { n   cerr <<  { ; n   _print(p.first); n   cerr <<  , ; n   _print(p.second); n   cerr <<  } ; n } n template <class T> n void _print(vector<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(set<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T> n void _print(multiset<T> v) { n   cerr <<  [  ; n   for (T i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n template <class T, class V> n void _print(map<T, V> v) { n   cerr <<  [  ; n   for (auto i : v) { n     _print(i); n     cerr <<    ; n   } n   cerr <<  ] ; n } n const long long N = 1e6 + 5; n void solve() { n   long long n; n   cin >> n; n   vector<vector<long long>> a; n   for (long long i = 0; i < n; i++) { n     long long x; n     cin >> x; n     vector<long long> temp(x); n     for (long long j = 0; j < x; j++) cin >> temp[j]; n     a.push_back(temp); n   } n   set<vector<long long>> vis; n   set<vector<long long>> banned; n   long long m; n   cin >> m; n   for (long long i = 0; i < m; i++) { n     vector<long long> temp(n); n     for (long long j = 0; j < n; j++) { n       cin >> temp[j]; n       temp[j]--; n     } n     banned.insert(temp); n   } n   set<pair<long long, vector<long long>>> st; n   long long s = 0; n   vector<long long> temp; n   for (long long i = 0; i < n; i++) { n     s += a[i][(long long)a[i].size() - 1]; n     temp.push_back((long long)a[i].size() - 1); n   } n   st.insert({s, temp}); n   vis.insert(temp); n   while (!st.empty()) { n     auto [val, vec] = *prev(st.end()); n     st.erase(prev(st.end())); n     if (banned.find(vec) == banned.end()) { n       for (auto i : vec) cout << i + 1 <<    ; n       return; n     } n     for (long long i = 0; i < n; i++) { n       if (vec[i] - 1 >= 0) { n         long long newval = val - a[i][vec[i]] + a[i][vec[i] - 1]; n         vector<long long> newvec = vec; n         newvec[i]--; n         if (vis.find(newvec) == vis.end()) { n           st.insert({newval, newvec}); n           vis.insert(newvec); n         } n       } n     } n   } n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     set<int> vec; n     for (int i = 0; i < n; i++) { n       int d; n       cin >> d; n       vec.insert(d); n     } n     cout << vec.size() <<   n ; n   } n } n


#include <bits/stdc++.h> n long long k; n int _, n, a[200020]; n int main() { n   for (scanf( %d , &_); _--;) { n     scanf( %d%lld , &n, &k); n     for (int i = 0; i < n; i++) scanf( %d , a + i); n     for (int i = 0; i < (k & 1) + 2; i++) n       for (int i = 0, d = *std::max_element(a, a + n); i < n; i++) n         a[i] = d - a[i]; n     for (int i = 0 * puts(  ); i < n; i++) printf( %d  , a[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long double PI = atan(1.0) * 4.0; n const long long int MOD = 1e9 + 7; n const long long int INF = 1e18; n const long double EPS = 0.0000001; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   map<char, int> a; n   for (int i = 0; i < n; i++) a[s[i]]++; n   int j = 97; n   while (k != 0 && j <= 122) { n     for (int i = 0; i < n; i++) { n       if (s[i] == char(j)) { n         s[i] =  * ; n         k--; n         if (k == 0) break; n       } n     } n     j++; n   } n   for (int i = 0; i < n; i++) { n     if (s[i] !=  * ) cout << s[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct abc { n   int val, key, s, e, ind; n } z[100005]; n bool cmp(abc a, abc b) { n   if (a.key == b.key) return a.val < b.val; n   return a.key > b.key; n } n bool cmp1(abc a, abc b) { return a.ind < b.ind; } n int main() { n   int n, m, i, j, k, a[100005], l; n   scanf( %d , &n); n   scanf( %d , &m); n   for (i = 0; i < m; i++) { n     scanf( %d , &z[i].val); n     scanf( %d , &z[i].key); n     z[i].ind = i; n   } n   sort(z, z + m, cmp); n   for (i = 0; i < n - 1; i++) { n     z[i].s = i + 1; n     z[i].e = i + 2; n   } n   l = 1; n   j = 1; n   k = 3; n   for (i = n - 1; i < m; i++) { n     if (z[i].val >= z[k - 2].val) { n       z[i].s = j; n       z[i].e = k; n       j++; n       if (j == k - 1) { n         j = 1; n         k++; n       } n     } else { n       l = 0; n       break; n     } n   } n   if (l == 0) n     printf( -1 n ); n   else { n     sort(z, z + m, cmp1); n     for (i = 0; i < m; i++) printf( %d %d n , z[i].s, z[i].e); n   } n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n int a[505],ch[505][505],cnt[505]; n int main(){ n     ios_base::sync_with_stdio(0),cin.tie(0); n     int n,c=0,i,j; n     cin>>n; n     for(i=1;i<=n;i++)cin>>a[i],ch[i][i]=a[i],cnt[a[i]]=a[i]-1; n     for(i=1;i<=n&&c==0;i++){ n         int x,y; n         x=y=i; n         while(cnt[a[i]]>0){ n             cnt[a[i]]--; n             if(x-1>0&&ch[y][x-1]==0)x--,ch[y][x]=a[i]; n             else if(y+1<=n&&ch[y+1][x]==0)y++,ch[y][x]=a[i]; n             else {c=1;break;} n         } n     } n     if(c)cout<< -1 n ; n     else{ n         for(i=1;i<=n;i++){ n             for(j=1;j<=i;j++)cout<<ch[i][j]<<   ; n             cout<<endl; n         } n     } n     return 0; n } n


#include <bits/stdc++.h> n #define int long long n #define mp make_pair n #define pb push_back n #define ld long double n #define pii pair<int,int> n #define sz(x) (int)x.size() n #define piii pair<pii,pii> n #define precise cout<<fixed<<setprecision(10) n #define st first n #define nd second n #define ins insert n #define vi vector<int> n #define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) n using namespace std; n const int MAX=5005; n pii p[MAX]; n bool O[MAX]; n int dist(pii a,pii b){ n    return (a.st-b.st)*(a.st-b.st)+(a.nd-b.nd)*(a.nd-b.nd); n } n int32_t main(){ n   BOOST; n   int n; n   cin>>n; n   for (int i=1;i<=n;i++){ n     cin>>p[i].st>>p[i].nd; n   } n    n   vi ans; n   ans.clear(); n   ans.pb(1); n   O[1]=true; n   while (sz(ans)<n){ n     int x=ans.back(); n     int maxi=0,wsk=-1; n     for (int i=1;i<=n;i++){ n       if (!O[i] && dist(p[x],p[i])>maxi){ n          maxi=dist(p[x],p[i]); n          wsk=i; n       } n     } n     assert(wsk!=-1); n     O[wsk]=true; n     ans.pb(wsk); n   } n   for (auto it:ans)cout<<it<<   ; n   return 0;  n } n  n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 2e5 + 9, lg = 19, MOD = 998244353, maxV = 1e3 + 9; n int T[3][maxN], n, q, arr[maxN], mp[129]; n priority_queue<int> pqs[2][3]; n string st; n int query(int w, int *T) { n   int ret = 0; n   for (w++; w > 0; w -= w & -w) ret += T[w]; n   return ret; n } n int query(int l, int r, int *T) { return query(r - 1, T) - query(l - 1, T); } n void update(int w, int x, int *T) { n   for (w++; w <= n; w += w & -w) T[w] += x; n } n pair<int, int> getpa(int heroe, int mamon, priority_queue<int> *pqs) { n   if (pqs[mamon].empty()) return pair<int, int>(n, n); n   if (pqs[heroe].empty()) return pair<int, int>(0, pqs[mamon].top()); n   return pair<int, int>(min(pqs[mamon].top(), pqs[heroe].top()), n                         pqs[mamon].top()); n } n pair<int, int> gira(pair<int, int> x) { n   return pair<int, int>(n - x.second, n - x.first); n } n int mergpl(pair<int, int> one, pair<int, int> two, int *lT) { n   if (one > two) swap(one, two); n   if (two.first < one.second) n     return query(one.first, max(one.second, two.second), lT); n   return query(one.first, one.second, lT) + query(two.first, two.second, lT); n } n int countGood() { n   int ret = 0; n   for (int u = 0, offs = 0, mu = 1; u < 2; u++, offs = n - 1, mu = -1) n     for (int i = 0; i < 3; i++) n       while (pqs[u][i].size() && arr[offs + mu * pqs[u][i].top()] != i) n         pqs[u][i].pop(); n   for (int i = 0; i < 3; i++) { n     int heroe = (i + 1) % 3, mamon = (i + 2) % 3; n     ret += query(n - 1, T[i]) - mergpl(getpa(heroe, mamon, pqs[0]), n                                        gira(getpa(heroe, mamon, pqs[1])), T[i]); n   } n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   mp[ R ] = 1; n   mp[ S ] = 2; n   cin >> n >> q >> st; n   for (int i = 0; i < n; i++) { n     arr[i] = mp[st[i]]; n     pqs[0][arr[i]].push(i); n     pqs[1][arr[i]].push(n - 1 - i); n     update(i, 1, T[arr[i]]); n   } n   cout << countGood() <<   n ; n   while (q--) { n     int pos; n     char c; n     cin >> pos >> c; n     pos--; n     update(pos, -1, T[arr[pos]]); n     arr[pos] = mp[c]; n     update(pos, 1, T[arr[pos]]); n     pqs[0][arr[pos]].push(pos); n     pqs[1][arr[pos]].push(n - 1 - pos); n     cout << countGood() <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int NN = 1010; n double first[NN], second[NN]; n int main() { n   int n, a, b, c, d; n   cin >> n; n   for (int i = 0; i < n; i++) { n     scanf( %d%d%d%d , &a, &b, &c, &d); n     double s = a * a * d * d + c * c * b * b; n     first[i] = a * b * d * d / s; n     second[i] = c * b * b * d / s; n   } n   const double eps = 1e-9; n   vector<pair<pair<double, double>, double> > line(n * (n - 1) / 2); n   for (int i = 0, id = 0; i < n; i++) n     for (int j = i + 1; j < n; j++, id++) { n       line[id] = n           make_pair(make_pair(first[i] + first[j], second[i] + second[j]), n                     fabs(first[i] - first[j]) < eps n                         ? 1e9 n                         : (second[i] - second[j]) / (first[i] - first[j])); n     } n   sort(line.begin(), line.end(), n        [&](const pair<pair<double, double>, double> &a, n            const pair<pair<double, double>, double> &b) { n          if (fabs(a.first.first - b.first.first) > eps) n            return a.first.first < b.first.first; n          if (fabs(a.first.second - b.first.second) > eps) n            return a.first.second < b.first.second; n          return a.second < b.second - eps; n        }); n   auto cmp1 = [&](double first, double second) { n     return fabs(first - second) < eps; n   }; n   auto cmp = [&](pair<double, double> first, pair<double, double> second) { n     return cmp1(first.first, second.first) && cmp1(first.second, second.second); n   }; n   int ans = 0; n   const int mod = 1000000007; n   for (int i = 0, j, k; i < line.size(); i = j) { n     int mid = 1; n     for (j = i; j < line.size() && cmp(line[i].first, line[j].first); j = k) { n       for (k = j; k < line.size() && cmp(line[k].first, line[j].first) && n                   cmp1(line[j].second, line[k].second); n            k++) n         ; n       mid = (long long)mid * (k - j + 1) % mod; n     } n     mid = (mid + mod - 1 - j + i) % mod; n     ans += mid; n     if (ans >= mod) ans -= mod; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long t; n   cin >> t; n   while (t--) { n     set<pair<pair<long long, long long>, pair<long long, long long> > > rem; n     long long xi = 0, yi = 0, xf = 0, yf = 0; n     string s; n     cin >> s; n     long long ans = 0; n     for (long long i = 0; i < (long long)(s.length()); ++i) { n       if (s[i] ==  N ) { n         yf++; n       } else if (s[i] ==  S ) { n         yf--; n       } else if (s[i] ==  E ) { n         xf++; n       } else { n         xf--; n       } n       if (rem.count({{xi, yi}, {xf, yf}})) { n         ans += 1; n       } else { n         ans += 5; n         rem.insert({{xi, yi}, {xf, yf}}); n         rem.insert({{xf, yf}, {xi, yi}}); n       } n       xi = xf; n       yi = yf; n     } n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long MAXN = 1e5 + 7; n long long n; n pair<long double, long double> a[MAXN]; n void read() { n   cin >> n; n   for (long long i = 0; i < n; ++i) { n     cin >> a[i].first >> a[i].second; n   } n   bool ch1 = 0, ch2 = 0; n   for (long long i = 0; i < n; ++i) { n     if (a[i].second < 0) n       ch1 = 1; n     else n       ch2 = 1; n   } n   if (ch1 && ch2) { n     cout <<  -1 n ; n     exit(0); n   } n } n const long double INF = 1e16; n const long long IT = 100; n bool check(long double r) { n   long double tl = -INF, tr = INF; n   for (long long i = 0; i < n; ++i) { n     if (abs(a[i].second - r) > r) return 0; n     long double d = sqrtl(r * r - (a[i].second - r) * (a[i].second - r)); n     tl = max(tl, a[i].first - d); n     tr = min(tr, a[i].first + d); n   } n   return tl <= tr; n } n long double ans = INF; n void solve() { n   for (long long i = 0; i < n; ++i) a[i].second = abs(a[i].second); n   long double l = 0; n   long double r = INF; n   for (long long i = 0; i < IT; ++i) { n     long double m = (l + r) / 2; n     if (check(m)) n       r = m; n     else n       l = m; n   } n   ans = r; n } n void print() { cout << ans <<   n ; } n signed main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.precision(20); n   read(); n   solve(); n   print(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, ans = 0; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   sort(a, a + n); n   if (n % 2 == 1) { n     ans = a[(n - 1) / 2]; n   } n   if (n % 2 == 0) { n     ans = a[(n / 2) - 1]; 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 const int N = 1e6 + 5; n int n, k, top; n int a[N], l[N], r[N], s[N]; n long long ans; n long long calc(int l, int r) { n   if (l > r || r - l + 1 < k) return 0; n   int m = (r - l + 2 - k) / (k - 1); n   long long ret = 1LL * (m + 1) * (r - l + 2 - k) % mod - n                   1LL * (1 + m) * m / 2 % mod * (k - 1) % mod; n   return ret; n } n int main() { n   int i; n   scanf( %d%d , &n, &k); n   for (i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (i = 1; i <= n; ++i) { n     while (top && a[s[top]] < a[i]) --top; n     l[i] = s[top] + 1, s[++top] = i; n   } n   top = 0, s[top] = n + 1; n   for (i = n; i; --i) { n     while (top && a[s[top]] <= a[i]) --top; n     r[i] = s[top] - 1, s[++top] = i; n   } n   for (i = 1; i <= n; ++i) n     ans = (ans + n            (calc(l[i], r[i]) - calc(l[i], i - 1) - calc(i + 1, r[i])) * a[i]) % n           mod; n   if (ans < 0) ans += mod; n   printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m, n; n int a[11][11] = {{0, 1, 2}, {1, 0, 2}, {1, 2, 0}, {2, 1, 0}, n                  {2, 0, 1}, {0, 2, 1}, {0, 1, 2}}; n int main() { n   ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); n   cin >> n >> m; n   cout << a[n % 6][m] << endl; n   return 0; n } n


#include <bits/stdc++.h> n int64_t k; n int a, b; n int result[9]; n int ascore[9] = {0, 0, 1, 1, 0, 0, 0, 1, 0}; n int bscore[9] = {0, 1, 0, 0, 0, 1, 1, 0, 0}; n int stack[10]; n int64_t atotalscore, btotalscore; n int cyclestart, cycleend, cyclelength; n int acyclescore, bcyclescore; n int64_t cycletime, cycleremain; n int main() { n   int i, j, t; n   scanf( %I64d%d%d , &k, &a, &b); n   stack[0] = 3 * (a - 1) + (b - 1); n   for (i = 0; i < 9; i++) { n     scanf( %d , &t); n     result[i] = 3 * (t - 1); n   } n   for (i = 0; i < 9; i++) { n     scanf( %d , &t); n     result[i] += (t - 1); n   } n   for (i = 1; i < k; i++) { n     stack[i] = result[stack[i - 1]]; n     for (j = 0; j < i; j++) { n       if (stack[i] == stack[j]) { n         cyclestart = j; n         cycleend = i - 1; n         cyclelength = i - j; n         break; n       } n     } n     if (cyclelength != 0) break; n   } n   if (cyclelength == 0) { n     for (i = 0; i < k; i++) { n       atotalscore += ascore[stack[i]]; n       btotalscore += bscore[stack[i]]; n     } n     printf( %I64d %I64d n , atotalscore, btotalscore); n     return 0; n   } n   for (i = 0; i < cyclestart; i++) { n     atotalscore += ascore[stack[i]]; n     btotalscore += bscore[stack[i]]; n   } n   for (i = cyclestart; i <= cycleend; i++) { n     acyclescore += ascore[stack[i]]; n     bcyclescore += bscore[stack[i]]; n   } n   cycletime = (k - cyclestart) / cyclelength; n   cycleremain = (k - cyclestart) % cyclelength; n   atotalscore += acyclescore * cycletime; n   btotalscore += bcyclescore * cycletime; n   for (i = cyclestart; i < cyclestart + cycleremain; i++) { n     atotalscore += ascore[stack[i]]; n     btotalscore += bscore[stack[i]]; n   } n   printf( %I64d %I64d n , atotalscore, btotalscore); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, x[100005], y[100005]; n bool sign(int t) { return (t > 0); } n bool check(long double r) { n   vector<pair<long double, long double> > v; n   for (int i = 0; i < n; i++) { n     long double t = (2.0 * r - y[i]) * y[i]; n     if (t < 0) return 0; n     long double l = sqrt(t); n     v.push_back({-l + x[i], l + x[i]}); n   } n   long double mn = 1e18, mx = -1e18; n   for (auto p : v) { n     mn = min(mn, p.second); n     mx = max(mx, p.first); n   } n   return (mx <= mn); n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     scanf( %d%d , &x[i], &y[i]); n     if (sign(y[i]) != sign(y[0])) { n       printf( -1 ); n       return 0; n     } n   } n   for (int i = 0; i < n; i++) y[i] = abs(y[i]); n   long double st = 0.0, en = 1e14; n   for (int _ = 0; _ < 100; _++) { n     long double mid = (st + en) / 2; n     if (check(mid)) n       en = mid; n     else n       st = mid; n   } n   printf( %.9Lf , st); n } n