#include <bits/stdc++.h> n using namespace std; n int getInt() { n   int x; n   scanf( %d , &x); n   return x; n } n long long getLongLong() { n   long long x; n   scanf( %I64d , &x); n   return x; n } n double getDouble() { n   double x; n   scanf( %lf , &x); n   return x; n } n char getChar() { n   char x; n   scanf( %c , &x); n   return x; n } n template <typename t> n t abs(t a) { n   if (a >= 0) return a; n   return -a; n } n struct dual { n   int a, b; n   bool const operator<(const dual &x) const { n     if (a == x.a) return b > x.b; n     return a < x.a; n   } n }; n struct triple { n   int a, b, c; n   bool const operator<(const triple &x) const { n     if (c == x.c) { n       if (a == x.a) return b > x.b; n       return a < x.a; n     } n     return c < x.c; n   } n }; n void FastIO() { n   ios::sync_with_stdio(0); n   cin.tie(0); n } n int a[512345]; n char s[512345]; n long long ssb[512345] = {0}, psb[512345] = {0}; n long long ssa[512345] = {0}, psa[512345] = {0}; n int main() { n   int n = getInt(); n   for (int i = 0; i < n; i++) a[i] = getInt(); n   scanf( %s , s); n   if (s[0] ==  B ) n     psb[0] = a[0]; n   else n     psa[0] = a[0]; n   for (int i = 1; i < n; i++) { n     psa[i] = psa[i - 1]; n     psb[i] = psb[i - 1]; n     if (s[i] ==  A ) n       psa[i] += a[i]; n     else n       psb[i] += a[i]; n   } n   for (int i = n - 1; i >= 0; i--) { n     ssa[i] = ssa[i + 1]; n     ssb[i] = ssb[i + 1]; n     if (s[i] ==  A ) n       ssa[i] += a[i]; n     else n       ssb[i] += a[i]; n   } n   long long ans = max(ssa[0], ssb[0]); n   for (int i = 0; i < n; i++) { n     ans = max(ans, max(psa[i] + ssb[i + 1], psb[i] + ssa[i + 1])); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1000; n const int dx[4] = {1, 1, -1, -1}; n const int dy[4] = {1, -1, -1, 1}; n int n, m; n int t[MAXN + 2][MAXN + 2]; n int dp[4][MAXN + 2][MAXN + 2]; n int sol; n int main() { n   ios::sync_with_stdio(0); n   cin >> n >> m; n   for (int i = 1; i <= n; ++i) n     for (int j = 1; j <= m; ++j) cin >> t[i][j]; n   for (int d = 0; d < 4; ++d) n     for (int i = dy[d] == 1 ? 1 : n; i && i <= n; i += dy[d]) n       for (int j = dx[d] == 1 ? 1 : m; j && j <= m; j += dx[d]) n         dp[d][i][j] = max(dp[d][i - dy[d]][j], dp[d][i][j - dx[d]]) + t[i][j]; n   for (int i = 2; i < n; ++i) n     for (int j = 2; j < m; ++j) n       sol = max(sol, max(dp[0][i - 1][j] + dp[1][i][j - 1] + dp[2][i + 1][j] + n                              dp[3][i][j + 1], n                          dp[0][i][j - 1] + dp[1][i + 1][j] + dp[2][i][j + 1] + n                              dp[3][i - 1][j])); n   cout << sol << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int a[N]; n int n; n int res[N]; n int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } n int lcm(int a, int b) { return a * b / gcd(a, b); } n void solve() { n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   if (n % 2 == 0) { n     for (int i = 0; i < n / 2; i++) { n       int j = n - 1 - i; n       int t = lcm(a[i], a[j]); n       res[i] = t / a[i]; n       res[j] = t / a[j]; n       if (1) { n         res[i] *= -1; n       } n     } n     for (int i = 0; i < n; i++) cout << res[i] <<    ; n     cout << endl; n   } else { n     for (int i = 0; i < n / 2 - 1; i++) { n       int j = n - 1 - i; n       int t = lcm(a[i], a[j]); n       res[i] = t / a[i]; n       res[j] = t / a[j]; n       if (1) { n         res[i] *= -1; n       } n     } n     res[n / 2 - 1] = 1; n     res[n / 2 + 1] = -1; n     int t = 1 * a[n / 2 - 1] + -1 * a[n / 2 + 1]; n     if (t == 0) { n       t += a[n / 2 - 1]; n       res[n / 2 - 1]++; n     } n     int t2 = lcm(t, a[n / 2]); n     res[n / 2] = t2 / a[n / 2]; n     res[n / 2 - 1] *= t2 / t; n     res[n / 2 + 1] *= t2 / t; n     if (1) { n       res[n / 2] *= -1; n     } n     for (int i = 0; i < n; i++) cout << res[i] <<    ; n     cout << endl; n   } n } n int main() { n   int K; n   scanf( %d , &K); n   while (K--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 9; n struct Node { n   double st, ed, dir; n } p[N]; n struct flag { n   double num, id; n } q[N]; n bool cmp(flag a, flag b) { return a.num < b.num; } n int main() { n   int n, m, i, j, k; n   while (~scanf( %d , &n)) { n     for (i = 0; i < n; ++i) { n       scanf( %lf%lf , &p[i].st, &p[i].dir); n     } n     double L = 0, R = 1e9 + 10, mid, ans = -1; n     for (j = 0; j < 100; ++j) { n       mid = (L + R) / 2; n       bool ok = false; n       double Max = -1e50; n       for (i = 0; i < n; ++i) { n         p[i].ed = p[i].st + p[i].dir * mid; n         if (p[i].dir >= 0) { n           Max = max(Max, p[i].ed); n         } else { n           if (p[i].ed <= Max) { n             ok = true; n             break; n           } n         } n       } n       if (ok) { n         ans = mid; n         R = mid; n       } else { n         L = mid; n       } n     } n     printf( %.20f n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 200020; n long long a; n int main() { n   cin >> a; n   long long l = 1, r = 1e18; n   long long ans = n       (((9 * (5 * (9 * (2 * (long long)1e17) % a) % a) % a) % a) + 1) % a; n   cout << l + (a - ans) <<     << r + (a - ans) << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int task; n   scanf( %d , &task); n   while (task--) { n     int n, p; n     scanf( %d%d , &n, &p); n     int rest(2 * n + p); n     for (int i = 1; (i <= n) && rest; i++) n       for (int j = i + 1; (j <= n) && rest; j++) n         printf( %d %d n , i, j), rest--; n   } n   return 0; n } n


#include <bits/stdc++.h> n const long long max9 = 10 + 1e9, max6 = 10 + 1e6, max12 = 10 + 1e12, n                 max15 = 10 + 1e15; n const long long min6 = -1 * max6, min9 = -1 * max9, min12 = -1 * max12, n                 min15 = -1 * max15; n const long long R = 7 + 1e9, NN = 10 + 1e5; n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long n; n   long long k; n   cin >> n >> k; n   long long mn = n * (n + 1); n   mn /= 2; n   long long nn = n / 2 - 1, mx = 2 * (nn + 1) * n - nn * (nn + 1); n   if (n % 2) mx += nn + 2; n   if (k < mn) { n     cout << -1; n     return 0; n   } n   if (k >= mx) { n     cout << mx << endl; n     for (int i = (0); i < (n); ++i) cout << i + 1 <<    ; n     cout << endl; n     for (int i = (0); i < (n); ++i) cout << n - i <<    ; n     return 0; n   } n   vector<int> a(n); n   long long l = 0, last = n; n   for (int i = (0); i < (n); ++i) a[i] = i + 1; n   long long ben = k - mn; n   for (int i = (0); i < (n / 2); ++i) { n     if (ben == 0) break; n     long long test = a[i] + ben; n     if (test > last) { n       a[i] = last; n       a[last - 1] = i + 1; n       ben -= last - i - 1; n       last--; n     } else { n       a[i] = test; n       a[test - 1] = i + 1; n       break; n     } n   } n   long long sum = 0; n   for (int i = (0); i < (n); ++i) sum += max(i + 1, a[i]); n   cout << sum << endl; n   for (int i = (0); i < (n); ++i) cout << i + 1 <<    ; n   cout << endl; n   for (int i = (0); i < (n); ++i) cout << a[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma comment(linker,  /STACK:167772160 ) n int comp(const void* a, const void* b) { n   return (*(long long*)a) - (*(long long*)b); n } n int main() { n   int n; n   long long mas[300000], ans(0); n   scanf( %d , &n); n   for (int i(0); i < n; ++i) scanf( %I64d , &mas[i]); n   qsort(mas, n, sizeof(long long), comp); n   for (int i(0); i < n; ++i) ans += mas[i] * (2 + i); n   ans -= mas[n - 1]; n   printf( %I64d , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[105]; n int main() { n   int n, m, i, l, r; n   char ch1, ch2; n   cin >> n >> m; n   cin >> (s + 1); n   while (m--) { n     cin >> l >> r >> ch1 >> ch2; n     for (i = l; i <= r; i++) n       if (s[i] == ch1) s[i] = ch2; n   } n   cout << (s + 1); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string a[100], b[100]; n map<string, int> m; n int n, s; n int main(void) { n   scanf( %d , &n); n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     m[a[i]]++; n   } n   for (int i = 0; i < n; i++) { n     cin >> b[i]; n     m[b[i]]--; n   } n   s += abs(m[ S ]); n   s += abs(m[ M ]); n   s += abs(m[ L ]); n   s += abs(m[ XS ]); n   s += abs(m[ XXS ]); n   s += abs(m[ XXXS ]); n   s += abs(m[ XL ]); n   s += abs(m[ XXL ]); n   s += abs(m[ XXXL ]); n   cout << s / 2; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(0); n   ios::sync_with_stdio(0); n   cout << fixed; n   cout << setprecision(12); n   long long n1, n2; n   cin >> n1 >> n2; n   long long a[n1], b[n2], cnt1[10] = {0}, cnt2[10] = {0}; n   for (long long i = 0; i < n1; i++) { n     cin >> a[i]; n     cnt1[a[i]]++; n   } n   for (long long i = 0; i < n2; i++) { n     cin >> b[i]; n     cnt2[b[i]]++; n   } n   for (long long i = 0; i < 10; i++) n     if (cnt1[i] != 0 && cnt2[i] != 0) { n       cout << i; n       return 0; n     } n   sort(a, a + n1); n   sort(b, b + n2); n   cout << min(a[0] * 10 + b[0], b[0] * 10 + a[0]); n   return 0; n } n void setprime(bool isprime[], long long n) { n   for (long long i = 0; i < n; i++) isprime[i] = true; n   isprime[0] = false; n   isprime[1] = false; n   for (long long i = 2; i < n; i++) { n     for (long long j = 2; i * j < n; j++) isprime[i * j] = false; n   } n } n long long BinarySearch(long long arr[], long long first, long long last, n                        long long search) { n   long long middle = (first + last) / 2; n   while (first <= last) { n     if (arr[middle] < search) { n       first = middle + 1; n     } else if (arr[middle] == search) { n       return middle; n     } else { n       last = middle - 1; n     } n     middle = (first + last) / 2; n   } n   if (first > last) { n     return -1; n   } n } n long long choose(long long n, long long k) { n   if (k == 0) return 1; n   return (n * choose(n - 1, k - 1)) / k; n } n void showArray(long long a[], long long n) { n   for (long long i = 0; i < n; i++) { n     cout << a[i] <<    ; n   } n   cout << endl; n } n long long gcd(long long a, long long b) { n   if (b == 0) return a; n   return gcd(b, a % b); n } n


#include <bits/stdc++.h> n using namespace std; n long long N; n long long x, y, x2, y2; n pair<int, int> v[100010]; n bool Can(long long n) { n   long long Rem = n % N; n   long long Count = n / N; n   long long X = v[Rem].first + (v[N].first * Count); n   long long Y = v[Rem].second + (v[N].second * Count); n   return ((abs(x2 - (X + x)) + abs(y2 - (Y + y))) <= n); n } n void Solve() { n   cin >> x >> y; n   cin >> x2 >> y2; n   cin >> N; n   string s; n   cin >> s; n   long long X = 0, Y = 0; n   for (long long i = 0; i < N; ++i) { n     if (s[i] ==  U ) { n       Y++; n     } else if (s[i] ==  D ) { n       Y--; n     } else if (s[i] ==  L ) { n       X--; n     } else if (s[i] ==  R ) { n       X++; n     } n     v[i + 1] = make_pair(X, Y); n   } n   long long ans = -1; n   long long low = 0, high = (1100000000000000000ll); n   while (low <= high) { n     long long mid = (low + high) / 2; n     if (Can(mid)) { n       high = mid - 1; n       ans = mid; n     } else { n       low = mid + 1; n     } n   } n   cout << ans << endl; n } n int main() { n   ios::sync_with_stdio(NULL); n   cin.tie(NULL); n   cout.tie(NULL); n   int t = 1; n   while (t--) Solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 500; n bool vis[3][MAXN]; n char g[3][MAXN]; n pair<int, int> que[MAXN * 3]; n int n, k; n inline void init() { n   memset(g, 0, sizeof(g)); n   memset(vis, false, sizeof(vis)); n   scanf( %d%d , &n, &k); n   for (int i = 0; i < 3; i++) scanf( %s , g[i]); n } n inline bool BFS() { n   int begin = 0, end = 0; n   for (int i = 0; i < 3; i++) n     if (g[i][0] ==  s ) que[end++] = pair<int, int>(i, 0); n   while (begin != end) { n     int ux = que[begin].first, uy = que[begin].second; n     begin++; n     if (uy >= n) return true; n     if (!isupper(g[ux][uy + 1])) { n       if (ux + 1 < 3 && !vis[ux + 1][uy + 3] && !isupper(g[ux + 1][uy + 1]) && n           !isupper(g[ux + 1][uy + 2]) && !isupper(g[ux + 1][uy + 3])) n         que[end++] = pair<int, int>(ux + 1, uy + 3), vis[ux + 1][uy + 3] = true; n       if (ux - 1 >= 0 && !vis[ux - 1][uy + 3] && !isupper(g[ux - 1][uy + 1]) && n           !isupper(g[ux - 1][uy + 2]) && !isupper(g[ux - 1][uy + 3])) n         que[end++] = pair<int, int>(ux - 1, uy + 3), vis[ux - 1][uy + 3] = true; n       if (!vis[ux][uy + 3] && !isupper(g[ux][uy + 2]) && n           !isupper(g[ux][uy + 3])) n         que[end++] = pair<int, int>(ux, uy + 3), vis[ux][uy + 3] = true; n     } n   } n   return false; n } n int main(void) { n   int t; n   scanf( %d , &t); n   while (t--) { n     init(); n     printf( %s n , BFS() ?  YES  :  NO ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t = 1; n   while (t--) { n     long long int i, j, k = 0, n, m; n     cin >> n >> m; n     vector<long long int> a(n); n     for (i = 0; i < n; i++) { n       long long int x, y; n       cin >> x >> y; n       k += x * y; n       a[i] = k; n     } n     while (m--) { n       cin >> k; n       cout << (lower_bound(a.begin(), a.end(), k) - a.begin() + 1) <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long f(long long x) { n   long long res; n   res = x * x; n   return res; n } n void solve() { n   long long a[3]; n   cin >> a[0] >> a[1] >> a[2]; n   sort(a, a + 3); n   long long d; n   d = (long long)(sqrt(f(a[2]) + f(a[1] - a[0]))); n   d = d + 1; n   cout << d <<   n ; n } n int main() { n   long long t; n   cin >> t; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n  n #define Int long long n  n const int mxN = 2e5 + 5; n  n int main() n { n     ios_base::sync_with_stdio(false); n     cin.tie(0); n     int t = 1; n     cin >> t; n     while(t--){ n         Int n; n         cin >> n; n         while(n % 11 != 0 && n > 10)n -= 111; n         if(n >= 0 && n % 11 == 0)cout <<  YES  << endl; n         else cout <<  NO  << endl; n        } n }


#include <bits/stdc++.h> n using namespace std; n long long exp(long long a, long long b, long long p) { n   if (b == 0) return 1; n   if (b % 2) return a * exp(a, b - 1, p) % p; n   return exp(a * a % p, b / 2, p); n } n long long inv[1000009], E[1000009]; n int main() { n   long long a, b, p, x; n   cin >> a >> b >> p >> x; n   vector<long long> v; n   for (int i = 1; i <= p; i++) { n     if ((p - 1) % i == 0) v.push_back(i); n   } n   long long on; n   for (int j = 0; j < v.size(); j++) { n     if (exp(a, v[j], p) == 1) { n       on = v[j]; n       break; n     } n   } n   for (int i = 1; i <= p; i++) { n     inv[i] = exp(i, p - 2, p); n   } n   long long ans = 0; n   for (int i = 1; i <= on; i++) { n     long long E = exp(a, i, p); n     long long N = inv[E] * b % p; n     long long y = (N - i + p) * inv[on] % p; n     long long m = i + y * on; n     long long M = p * on; n     if (x % M >= m) n       ans += x / M + 1; n     else n       ans += x / M; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 1000000; n int f[35][35][55]; n int gao(int x, int y, int z) { n   if (f[x][y][z] > 0) return f[x][y][z]; n   if (z == 0) return f[x][y][z] = 0; n   if (x * y < z) return f[x][y][z] = inf; n   if (x * y == z) return f[x][y][z] = 0; n   int now = inf; n   for (int i = 1; i <= z; i++) { n     int cnt = inf; n     for (int j = 1; j < x; j++) n       cnt = min(cnt, gao(j, y, i) + gao(x - j, y, z - i) + y * y); n     for (int j = 1; j < y; j++) n       cnt = min(cnt, gao(x, j, i) + gao(x, y - j, z - i) + x * x); n     now = min(now, cnt); n   } n   return f[x][y][z] = now; n } n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     int n, m, k; n     scanf( %d%d%d , &n, &m, &k); n     printf( %d n , gao(n, m, k)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   int m; n   cin >> n >> m; n   bool ans[m + 1]; n   for (int i = 0; i <= m; i++) { n     ans[i] = false; n   } n   int a[n]; n   vector<pair<int, int> > v; n   for (int i = 0; i < n; i++) { n     int a; n     int b; n     cin >> a; n     cin >> b; n     v.push_back(make_pair(a, b)); n   } n   for (int i = 0; i < n; i++) { n     int start = v[i].first; n     int end = v[i].second; n     for (int j = start; j <= end; j++) { n       if (ans[j] == false) ans[j] = true; n     } n   } n   vector<int> final; n   int result = 0; n   for (int i = 1; i <= m; i++) { n     if (ans[i] == false) { n       final.push_back(i); n       result++; n     } n   } n   cout << result << endl; n   for (int i = 0; i < final.size(); i++) { n     cout << final[i] <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = 3.1415926535897932384626433832795028841971; n long long fb[100100], fa[100100]; n long long k[100100], a[100100], b[100100]; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(); n   cout.tie(); n   long long n, s, sum = 0, ans = 0, ka = 0, kb = 0; n   cin >> n >> s; n   for (int i = 0; i < (n); i++) { n     cin >> k[i] >> a[i] >> b[i]; n     if (a[i] >= b[i]) { n       fa[a[i] - b[i]] += k[i]; n       ka += k[i]; n     } n     if (a[i] <= b[i]) { n       fb[b[i] - a[i]] += k[i]; n       kb += k[i]; n     } n     sum += k[i]; n     ans += k[i] * max(a[i], b[i]); n   } n   long long ks = (sum + s - 1) / s; n   if (((ka + s - 1) / s) + ((kb + s - 1) / s) == ks) { n     cout << ans; n     return 0; n   } n   kb %= s; n   ka %= s; n   long long sum1 = 0, sum2 = 0; n   for (long long i = 0; i < 100010; i++) { n     if (ka > 0) { n       sum1 += i * min(ka, fa[i]); n       ka -= min(ka, fa[i]); n     } n     if (kb > 0) { n       sum2 += i * min(kb, fb[i]); n       kb -= min(kb, fb[i]); n     } n   } n   cout << ans - min(sum1, sum2); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char buf[2000]; n int dx[4] = {0, 0, -1, 1}; n int dy[4] = {-1, 1, 0, 0}; n int n, m; n pair<int, int> tbl1[1 << 23]; n pair<int, int> tbl2[1 << 23]; n const int Prime1 = 262139, Prime2 = 40999999; n const int Prime3 = 100003, Prime4 = 40999799; n const pair<int, int> ZERO = make_pair(0, 0); n bool ex(int x, int y) { n   int t1 = x * Prime1 + y * Prime2; n   int t2 = x * Prime3 + y * Prime4; n   t1 = abs(t1); n   t1 %= ((1 << 23) - 1); n   t2 = abs(t2); n   t2 %= ((1 << 23) - 1); n   while (tbl1[t1] != ZERO && tbl1[t1] != make_pair(x, y)) n     t1 = (t1 + 1) % (1 << 23); n   if (tbl1[t1] == make_pair(x, y)) return false; n   tbl2[t2] = tbl1[t1] = make_pair(x, y); n   return true; n } n int main() { n   cin >> n >> m; n   vector<string> a; n   for (int i = (0); i < (n); ++i) { n     string s; n     scanf( %s , buf); n     s = buf; n     a.push_back(s); n   } n   pair<int, int> start; n   for (int i = (0); i < (n); ++i) { n     for (int j = (0); j < (m); ++j) n       if (a[i][j] ==  S ) start = make_pair(i, j); n   } n   queue<pair<int, int> > q; n   q.push(start); n   ex(start.first, start.second); n   int k = 0; n   while (!q.empty()) { n     if (k > n * m) { n       cout <<  Yes n ; n       return 0; n     } n     pair<int, int> v = q.front(); n     q.pop(); n     for (int i = (0); i < (4); ++i) { n       int x = v.first + dx[i]; n       int y = v.second + dy[i]; n       int _x = (x % n + n) % n; n       int _y = (y % m + m) % m; n       if (a[_x][_y] !=  #  && ex(x, y)) { n         k++; n         q.push(make_pair(x, y)); n       } n     } n   } n   cout <<  No n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int a; n   cin >> a; n   while (1) { n     long long int t = a; n     long long int su = 0; n     while (t > 0) { n       su += t % 10; n       t /= 10; n     } n     if (su % 4 == 0) { n       cout << a; n       return 0; n     } n     a++; n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct MinTreeNode { n   int min; n   int count; n }; n int n; n MinTreeNode tmin[200000]; n int tgcd[200000]; n int gcd(int a, int b) { n   if (a == 0) { n     return b; n   } n   if (b == 0) { n     return a; n   } n   if (a == 1 || b == 1) { n     return 1; n   } n   if (a % 2 == 0) { n     if (b % 2 == 0) { n       return 2 * gcd(a / 2, b / 2); n     } else { n       return gcd(a / 2, b); n     } n   } else { n     if (b % 2 == 0) { n       return gcd(a, b / 2); n     } else { n       return gcd(min(a, b), max(a, b) - min(a, b)); n     } n   } n } n void buildGcdTree() { n   for (int i = n - 1; i >= 1; --i) { n     tgcd[i] = gcd(tgcd[i << 1], tgcd[i << 1 | 1]); n   } n } n void buildMinTree() { n   for (int i = n - 1; i >= 1; --i) { n     if (tmin[i << 1].min < tmin[i << 1 | 1].min) { n       tmin[i] = tmin[i << 1]; n     } else { n       if (tmin[i << 1].min > tmin[i << 1 | 1].min) { n         tmin[i] = tmin[i << 1 | 1]; n       } else { n         tmin[i].min = tmin[i << 1].min; n         tmin[i].count = tmin[i << 1].count + tmin[i << 1 | 1].count; n       } n     } n   } n } n MinTreeNode minAndCount(int l, int r) { n   MinTreeNode result; n   result.min = 2000000000; n   result.count = 0; n   for (l += n, r += n; l < r; l >>= 1, r >>= 1) { n     if (l & 1) { n       if (result.min == tmin[l].min) { n         result.count += tmin[l].count; n       } else { n         if (result.min > tmin[l].min) { n           result = tmin[l]; n         } n       } n       ++l; n     } n     if (r & 1) { n       --r; n       if (result.min == tmin[r].min) { n         result.count += tmin[r].count; n       } else { n         if (result.min > tmin[r].min) { n           result = tmin[r]; n         } n       } n     } n   } n   return result; n } n int gcdOfInterval(int l, int r) { n   int result = 0; n   for (l += n, r += n; l < r; l >>= 1, r >>= 1) { n     if (l & 1) { n       result = gcd(result, tgcd[l++]); n     } n     if (r & 1) { n       result = gcd(result, tgcd[--r]); n     } n   } n   return result; n } n int elementsThatDivideIntervalCount(int l, int r) { n   int g = gcdOfInterval(l, r); n   MinTreeNode m = minAndCount(l, r); n   if (m.min == g) { n     return m.count; n   } else { n     return 0; n   } n } n int main(int argc, char** argv) { n   ios::sync_with_stdio(0); n   cin >> n; n   for (int i = n; i < 2 * n; ++i) { n     int s; n     cin >> s; n     tgcd[i] = s; n     tmin[i].min = s; n     tmin[i].count = 1; n   } n   buildMinTree(); n   buildGcdTree(); n   int t; n   cin >> t; n   for (int i = 0; i < t; ++i) { n     int l, r; n     cin >> l >> r; n     --l; n     cout << r - l - elementsThatDivideIntervalCount(l, r) <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<long long int> v; n vector<long long int> v1; n vector<long long int> v2; n const long long int inf = 100000000000005; 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   for (int i = 1; i < n; i++) cout << i + 1 <<    ; n   cout << 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int p[] = {0, 5, 7, 5}; n int main() { n   for (int k = 1; k <= 3; k++) { n     string s; n     getline(cin, s); n     int tot = 0; n     for (int i = 0; i < s.size(); i++) n       if (s[i] ==  a  || s[i] ==  e  || s[i] ==  i  || s[i] ==  o  || n           s[i] ==  u ) n         tot++; n     if (tot != p[k]) { n       cout <<  NO  << endl; n       return 0; n     } n   } n   cout <<  YES  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 2147483647; n int nxt[4][100001] = {0}; n bool ok[4]; n void getnext(int m, char s2[], int t) { n   int j = 0; n   for (int i = 2; i <= m; i++) { n     while (j != 0 && s2[j + 1] != s2[i]) j = nxt[t][j]; n     if (s2[j + 1] == s2[i]) j++; n     nxt[t][i] = j; n   } n   return; n } n int KMP(int n, int m, char s1[], char s2[], int t) { n   int j = 0, cnt = 0; n   for (int i = 1; i <= n; i++) { n     while (j != 0 && s2[j + 1] != s1[i]) j = nxt[t][j]; n     if (s2[j + 1] == s1[i]) j++; n     if (j == m) { n       return -1; n       j = nxt[t][j]; n     } n   } n   return j; n } n char s[4][100001]; n int len[4]; n int K[4][4]; n int main() { n   scanf( %s%s%s , s[1] + 1, s[2] + 1, s[3] + 1); n   len[1] = strlen(s[1] + 1); n   len[2] = strlen(s[2] + 1); n   len[3] = strlen(s[3] + 1); n   for (int i = 1; i <= 3; i++) { n     getnext(len[i], s[i], i); n     for (int j = 1; j <= 3; j++) { n       if (i == j) continue; n       K[j][i] = KMP(len[j], len[i], s[j], s[i], i); n     } n   } n   int ans = INF; n   for (int i = 1; i <= 3; i++) { n     for (int j = 1; j <= 3; j++) { n       for (int k = 1; k <= 3; k++) { n         if (i == j || j == k || i == k) continue; n         int sum = len[i] + len[j] + len[k] - K[i][j] - K[j][k]; n         if (K[i][j] >= 0 && K[j][k] >= 0) n           ans = min(ans, sum); n         else { n           if (K[i][j] < 0 && K[i][k] < 0) n             ans = min(ans, len[i]); n           else if (K[i][j] < 0) n             ans = min(ans, sum + K[i][j] + K[j][k] - len[j] - K[i][k]); n           if (K[j][k] < 0) ans = min(ans, sum + K[j][k] - len[k]); n         } n       } n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void file(string s) { n   freopen((s +  .in ).c_str(),  r , stdin); n   freopen((s +  .out ).c_str(),  w , stdout); n } n template <typename Tp> n void read(Tp &x) { n   long long fh = 1; n   char c = getchar(); n   x = 0; n   while (c >  9  || c <  0 ) { n     if (c ==  - ) { n       fh = -1; n     } n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = (x << 1) + (x << 3) + (c & 15); n     c = getchar(); n   } n   x *= fh; n } n long long n, m, T; n vector<long long> ft[1000005]; n long long pos[1000005]; n namespace FTree { n long long dfnl[1000005], dfnr[1000005], tot; n struct BIT { n   long long b[1000005]; n   void add(long long x, long long v) { n     for (; x <= tot; x += (x & (-x))) b[x] += v; n   } n   long long ask(long long x) { n     long long ret = 0; n     for (; x; x -= (x & (-x))) ret += b[x]; n     return ret; n   } n } Tr; n void dfs(long long x) { n   dfnl[x] = ++tot; n   for (unsigned i = 0; i < ft[x].size(); ++i) { n     long long y = ft[x][i]; n     dfs(y); n   } n   dfnr[x] = tot; n } n void prep() { n   dfs(0); n   for (long long i = 1; i <= n; ++i) { n     Tr.add(dfnl[pos[i]], 1); n     Tr.add(dfnr[pos[i]] + 1, -1); n   } n } n void upd(long long x, long long v) { n   Tr.add(dfnl[x], v); n   Tr.add(dfnr[x] + 1, -v); n } n long long qry(long long x) { return Tr.ask(dfnl[x]); } n }  // namespace FTree n namespace ACAM { n signed ch[1000005][26], fail[1000005], tot; n long long ins(const char *str) { n   long long p = 0, nn = strlen(str), dir; n   for (long long i = 0; i < nn; ++i) { n     dir = str[i] -  a ; n     if (!ch[p][dir]) ch[p][dir] = ++tot; n     p = ch[p][dir]; n   } n   return p; n } n void build() { n   queue<long long> q; n   for (long long i = 0; i < 26; ++i) n     if (ch[0][i]) q.push(ch[0][i]); n   while (!q.empty()) { n     long long x = q.front(); n     q.pop(); n     for (long long i = 0; i < 26; ++i) { n       if (ch[x][i]) { n         fail[ch[x][i]] = ch[fail[x]][i]; n         q.push(ch[x][i]); n       } else { n         ch[x][i] = ch[fail[x]][i]; n       } n     } n   } n   for (long long i = 1; i <= tot; ++i) ft[fail[i]].push_back(i); n   FTree::prep(); n } n void mtch(const char *str) { n   long long p = 0, nn = strlen(str), ans = 0; n   for (long long i = 0; i < nn; ++i) { n     p = ch[p][str[i] -  a ]; n     ans += FTree::qry(p); n   } n   printf( %lld n , ans); n } n }  // namespace ACAM n char sstr[1000005]; n bool vis[1000005]; n signed main() { n   read(T); n   read(n); n   for (long long i = 1; i <= n; ++i) { n     scanf( %s , sstr); n     pos[i] = ACAM::ins(sstr); n     vis[i] = 1; n   } n   ACAM::build(); n   char op; n   for (long long i = 1, x; i <= T; ++i) { n     scanf(  n%c , &op); n     if (op ==  ? ) { n       scanf( %s , sstr); n       ACAM::mtch(sstr); n     } n     if (op ==  + ) { n       read(x); n       if (!vis[x]) { n         FTree::upd(pos[x], 1), vis[x] = 1; n       } n     } n     if (op ==  - ) { n       read(x); n       if (vis[x]) { n         FTree::upd(pos[x], -1), vis[x] = 0; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; n vector<int> next(vector<int>& a, int p, int k) { n   vector<int> result; n   for (long long mul = 1; mul <= (long long)k; mul *= p) { n     for (int i = 0; i < a.size(); ++i) { n       long long newN = (long long)a[i] * mul; n       if (newN <= k) { n         result.push_back((int)newN); n       } n     } n   } n   return result; n } n int main() { n   ios_base::sync_with_stdio(false); n   int k; n   cin >> k; n   int treshold = 2 * k * k; n   vector<int> p; n   vector<int> a; n   a.push_back(1); n   int need = (k / 2); n   if (k % 2 == 1) need++; n   p.push_back(prime[0]); n   int ppp = 1; n   for (int i = 0; i < p.size(); ++i) { n     int old = a.size(); n     a = next(a, p[i], 2 * k * k); n     if (a.size() - old >= need && a.size() >= k) n       break; n     else n       p.push_back(prime[ppp++]); n   } n   vector<int> result; n   vector<int> h(p.size()); n   int i = a.size() - 1; n   for (int ch = (int)p.size() - 1; ch >= 0; ch--) { n     for (; h[ch] < need && i >= 0; --i) { n       if (a[i] % p[ch] == 0) { n         result.push_back(a[i]); n         for (int pp = 0; pp < p.size(); ++pp) { n           if (a[i] % p[pp] == 0) ++h[pp]; n         } n         a[i] = 31; n       } n     } n   } n   while (result.size() < k) { n     if (i < 0) i = (a.size() - 1); n     if (a[i] != 31) { n       result.push_back(a[i]); n       a[i] = 31; n     } n     i--; n   } n   for (int i = 0; i < result.size(); ++i) cout << result[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } n using namespace ::std; n const int maxn = 5300; n const int inf = 1e9 + 800; n const int mod = 1e9 + 7; n bool ro[maxn][maxn]; n bool cu[maxn][maxn]; n bool mosro[maxn]; n bool moscu[maxn]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     string s; n     cin >> s; n     for (int j = 0; j < n; j += 4) { n       char c = s[j / 4]; n       if ( A  <= c && c <=  Z ) { n         c -=  A ; n         c += 10; n       } else { n         c -=  0 ; n       } n       ro[i][j] = ((c >> 3) & 1); n       ro[i][j + 1] = ((c >> 2) & 1); n       ro[i][j + 2] = ((c >> 1) & 1); n       ro[i][j + 3] = ((c >> 0) & 1); n     } n   } n   for (int i = 0; i < n; i++) { n     for (int j = n - 1; j >= 0; j--) { n       cu[i][j] = ro[j][i]; n     } n   } n   for (int i = 0; i + 1 < n; i++) { n     mosro[i] = 1; n     moscu[i] = 1; n     for (int j = 0; j < n; j++) { n       if (ro[i][j] != ro[i + 1][j]) { n         mosro[i] = 0; n       } n       if (cu[i][j] != cu[i + 1][j]) { n         moscu[i] = 0; n       } n     } n   } n   for (int x = n; x >= 1; x--) { n     if (n % x == 0) { n       bool good = 1; n       for (int j = 0; j + 1 < n; j++) { n         if (j % x != x - 1 && (mosro[j] == 0 || moscu[j] == 0)) { n           good = 0; n           break; n         } n       } n       if (good) { n         cout << x; n         return 0; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int p[100010]; n const int md = 1000000009; n int an = 1; n int sz[100010]; n void make_set(int v) { n   p[v] = v; n   sz[v] = 1; n } n int find_set(int v) { n   if (p[v] == v) return v; n   return p[v] = find_set(p[v]); n } n void union_sets(int a, int b) { n   a = find_set(a); n   b = find_set(b); n   if (a != b) { n     if (sz[a] < sz[b]) swap(a, b); n     p[b] = a; n     sz[a] += sz[b]; n   } else n     an = (an * 2) % md; n } n int main() { n   scanf( %d %d , &n, &m); n   for (int i = 1; i <= n; i++) make_set(i); n   for (int i = 0; i < m; i++) { n     int a, b; n     scanf( %d %d , &a, &b); n     union_sets(a, b); n     printf( %d n , an - 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int dp[100][100]; n struct point { n  public: n   long long x, y; n   point(long long a, long long b) { n     x = a; n     y = b; n   } n   point() {} n   void setPoint(long long a, long long b) { n     x = a; n     y = b; n   } n   double dist(point p) { return sqrt(pow(x - p.x, 2.0) + pow(y - p.y, 2.0)); } n   long long dot(point A, point B) { n     point OA(A.x - x, A.y - y), OB(B.x - x, B.y - y); n     return (OA.x * OB.x + OA.y * OB.y); n   } n   long long cross(point A, point B) { n     point OA(A.x - x, A.y - y), OB(B.x - x, B.y - y); n     return (OA.x * OB.y - OA.y * OB.x); n   } n   bool operator<(const point &p) const { n     return x < p.x || (x == p.x && y < p.y); n   } n }; n int main() { n   long long ans = 0; n   int X, Y; n   string str; n   cin >> str; n   Y = str.size(); n   cin >> X; n   int val; n   vector<pair<int, int>> a; n   for (int i = 0; i < (X); i++) { n     cin >> val; n     a.push_back(make_pair(val - 1, 1)); n     a.push_back(make_pair(Y - val, -1)); n   } n   sort((a).begin(), (a).end()); n   int start = 0, open = 0, stop = 0, dist, last = 1; n   for (int i = 0; i <= a.size() / 2; i++) { n     stop = a[i].first; n     dist = stop; n     if (last != a[i].second) { n       dist = stop - start + 1; n       dist = start + dist / 2; n       last = a[i].second; n     } n     for (int j = (start); j < (dist); j++) { n       if (open > 0) { n         if (open % 2) { n           str[j] ^= str[Y - 1 - j] ^= str[j] ^= str[Y - 1 - j]; n         } n       } n     } n     start = stop; n     open += a[i].second; n   } n   cout << str << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string s; n int n; n int main() { n   cin >> n; n   cin >> s; n   for (int i = 0; i < n; i++) { n     if (i + 2 < n && s[i] ==  o  && s[i + 1] ==  g  && s[i + 2] ==  o ) { n       while (i + 2 < n && s[i + 1] ==  g  && s[i + 2] ==  o ) i += 2; n       cout <<  *** ; n     } else n       cout << s[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   cin.tie(NULL); n   cout.tie(NULL); n   ios_base::sync_with_stdio(false); n   int n; n   cin >> n; n   vector<string> arr; n   for (int i = 0; i < n; i++) { n     string x; n     cin >> x; n     arr.push_back(x); n   } n   int chocolate = 0; n   for (int i = 0; i < n; i++) { n     int temp = 0; n     temp = count(arr[i].begin(), arr[i].end(),  C ); n     chocolate += (temp * (temp - 1)) / 2; n   } n   for (int i = 0; i < n; i++) { n     int temp = 0; n     for (int j = 0; j < n; j++) { n       if (arr[j][i] ==  C ) { n         temp += 1; n       } n     } n     chocolate += (temp * (temp - 1)) / 2; n   } n   cout << chocolate << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<pair<int, int>, int> > a; n int main() { n   int L, b, f, n, m, cmd, x; n   cin >> L >> b >> f; n   cin >> n; n   for (int cs = (1); cs <= (n); ++cs) { n     sort((a).begin(), (a).end()); n     cin >> cmd >> x; n     if (cmd == 1) { n       if ((a.empty() && x <= L) || (!a.empty() && x + f <= a[0].first.first)) { n         cout << 0 << endl; n         a.push_back(make_pair(make_pair(0, x), cs)); n         goto C; n       } n       m = (int)(a).size(); n       for (int i = 0; i < (m - 1); ++i) n         if (a[i + 1].first.first - a[i].first.second >= b + f + x) { n           cout << a[i].first.second + b << endl; n           a.push_back(make_pair( n               make_pair(a[i].first.second + b, a[i].first.second + b + x), cs)); n           goto C; n         } n       if (m && L - a.back().first.second >= b + x) { n         cout << a.back().first.second + b << endl; n         a.push_back(make_pair( n             make_pair(a.back().first.second + b, a.back().first.second + b + x), n             cs)); n         goto C; n       } n       cout << -1 << endl; n     } else { n       for (__typeof((a).begin()) i = ((a).begin()); i != (a).end(); ++i) n         if (i->second == x) { n           swap(*i, a.back()); n           a.pop_back(); n           goto C; n         } n     } n   C:; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n const int maxn = 2e6 + 7; n char str[maxn]; n int ans[10][10], used[10]; n int mp[10][10][10][10]; n struct NODE { n   int num, step; n }; n int bfs(int numa, int numb, int a, int b) { n   queue<NODE> que; n   memset(used, 0, sizeof(used)); n   que.push((NODE){numa, 0}); n   while (!que.empty()) { n     NODE now = que.front(); n     que.pop(); n     int x = (now.num + a) % 10; n     int y = (now.num + b) % 10; n     if (x == numb || y == numb) return now.step; n     if (!used[x]) { n       used[x] = 1; n       que.push((NODE){x, now.step + 1}); n     } n     if (!used[y]) { n       used[y] = 1; n       que.push((NODE){y, now.step + 1}); n     } n   } n   return -1; n } n int main() { n   for (auto i = 0; i < 10; i++) { n     for (auto j = i; j < 10; j++) { n       for (auto numa = 0; numa < 10; numa++) { n         for (auto numb = 0; numb < 10; numb++) { n           int Insert = bfs(numa, numb, i, j); n           if (Insert == -1) { n             mp[i][j][numa][numb] = mp[j][i][numa][numb] = -1; n             continue; n           } else n             mp[i][j][numa][numb] = mp[j][i][numa][numb] = Insert; n         } n       } n     } n   } n   scanf( %s , str); n   int len = strlen(str); n   for (auto i = 0; i < 10; i++) { n     for (auto j = i; j < 10; j++) { n       for (auto p = 1; p < len; p++) { n         int numa = str[p - 1] -  0 ; n         int numb = str[p] -  0 ; n         int Insert = mp[i][j][numa][numb]; n         if (Insert == -1) { n           ans[i][j] = ans[j][i] = -1; n           break; n         } else { n           ans[i][j] += Insert; n           if (i != j) ans[j][i] += Insert; n         } n       } n     } n   } n   for (auto i = 0; i < 10; i++) { n     for (auto j = 0; j < 10; j++) { n       printf( %d  , ans[i][j]); n     } n     puts(  ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string st; n   cin >> st; n   for (int i = 0; i < st.size(); i++) { n     if (st[i] ==  0  || st[i] ==  8 ) { n       cout <<  YES n ; n       cout << st[i] << endl; n       return 0; n     } n     for (int j = i + 1; j < st.size(); j++) { n       int num = st[i] -  0 ; n       num *= 10; n       num += (st[j] -  0 ); n       if (num % 8 == 0) { n         cout <<  YES n ; n         cout << num << endl; n         return 0; n       } n       for (int k = j + 1; k < st.size(); k++) { n         num = st[i] -  0 ; n         num *= 10; n         num += (st[j] -  0 ); n         num *= 10; n         num += (st[k] -  0 ); n         if (num % 8 == 0) { n           cout <<  YES n ; n           cout << num << endl; n           return 0; n         } n       } n     } n   } n   cout <<  NO n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n map<char, long long> mp; n map<pair<char, long long>, long long> cnt; n int main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   for (char c =  a ; c <=  z ; c++) cin >> mp[c]; n   string s; n   cin >> s; n   long long n = s.length(); n   long long sum = mp[s[0]]; n   cnt[{s[0], mp[s[0]]}]++; n   long long ans = 0; n   for (long long i = 1; i < n; i++) { n     ans += cnt[{s[i], sum}]; n     sum += mp[s[i]]; n     cnt[{s[i], sum}]++; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   map<int, int> mp; n   long long ans = 0, mx = 1e6; n   for (int i = 0; i < n; i++) { n     int first; n     scanf( %d , &first); n     int second = 1; n     long long yy = 1; n     for (int j = 2; j * j <= first; j++) n       if (first % j == 0) { n         int cnt = 0; n         while (first % j == 0) cnt++, first /= j; n         cnt %= k; n         if (cnt) { n           for (int t = 0; t < cnt; t++) second *= j; n           for (int t = cnt; t < k; t++) yy = min(mx, yy * j); n         } n       } n     if (first > 1) { n       int cnt = 1, j = first; n       for (int t = 0; t < cnt; t++) second *= j; n       for (int t = cnt; t < k; t++) yy = min(mx, yy * j); n     } n     if (mp.count(yy)) ans += mp[yy]; n     mp[second]++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n int R, C; n int ER, EC; n int SR, SC; n char field[1010][1010]; n int dist[1010][1010]; n int Q[1010 * 1010], Qb, Qe; n int dr[4] = {-1, +1, 0, 0}; n int dc[4] = {0, 0, -1, +1}; n int main(void) { n   scanf( %d %d , &R, &C); n   for (int r = 0; r < R; ++r) { n     scanf( %s , field[r]); n     for (int c = 0; c < C; ++c) { n       if (field[r][c] ==  E ) { n         ER = r; n         EC = c; n       } n       if (field[r][c] ==  S ) { n         SR = r; n         SC = c; n       } n     } n   } n   memset(dist, -1, sizeof dist); n   Q[0] = ER * 10000 + EC; n   Qe = 1; n   dist[ER][EC] = 0; n   while (Qb != Qe) { n     int r = Q[Qb] / 10000; n     int c = Q[Qb] % 10000; n     Qb += 1; n     for (int d = 0; d < 4; ++d) { n       int rr = r + dr[d]; n       int cc = c + dc[d]; n       if (rr < 0 || cc < 0 || rr >= R || cc >= C) continue; n       if (field[rr][cc] ==  T ) continue; n       if (dist[rr][cc] != -1) continue; n       dist[rr][cc] = dist[r][c] + 1; n       Q[Qe++] = rr * 10000 + cc; n     } n   } n   int battles = 0; n   int mydist = dist[SR][SC]; n   for (int r = 0; r < R; ++r) { n     for (int c = 0; c < C; ++c) { n       if (!isdigit(field[r][c])) continue; n       if (dist[r][c] == -1 || dist[r][c] > mydist) continue; n       int cnt = field[r][c] -  0 ; n       battles += cnt; n     } n   } n   printf( %d n , battles); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, M; n vector<long long> cards; n map<int, vector<int>> m; n int sodd = 1, seven = 2; n int oddbalance; n int nextOdd() { n   for (int i = sodd;; i += 2) { n     if (m[i].empty()) { n       sodd = i + 2; n       if (i > M) { n         cout << -1 << endl; n         exit(0); n       } n       return i; n     } n   } n } n int nextEven() { n   for (int i = seven;; i += 2) { n     if (m[i].empty()) { n       seven = i + 2; n       if (i > M) { n         cout << -1 << endl; n         exit(0); n       } n       return i; n     } n   } n } n int cnt; n int main() { n   cin >> n >> M; n   cards.resize(n); n   for (int i = 0; i < n; i++) { n     cin >> cards[i]; n     if (cards[i] % 2) n       oddbalance++; n     else n       oddbalance--; n     m[cards[i]].push_back(i); n   } n   for (auto p : m) { n     while (p.second.size() > 1) { n       cnt++; n       if (p.first % 2 == 1) n         oddbalance--; n       else n         oddbalance++; n       int pos = p.second.back(); n       p.second.pop_back(); n       int val; n       if (oddbalance > 0) { n         val = nextEven(); n         oddbalance--; n       } else { n         val = nextOdd(); n         oddbalance++; n       } n       m[val].push_back(pos); n       cards[pos] = val; n     } n   } n   for (int i = 0; i < n && oddbalance; i++) { n     if (oddbalance > 0 && cards[i] % 2 == 1) { n       cnt++; n       cards[i] = nextEven(); n       oddbalance -= 2; n     } else if (oddbalance < 0 && cards[i] % 2 == 0) { n       cnt++; n       cards[i] = nextOdd(); n       oddbalance += 2; n     } n   } n   if (oddbalance != 0) { n     cout << -1 << endl; n     exit(0); n   } n   cout << cnt << endl; n   for (int c : cards) { n     cout << c <<    ; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n #pragma GCC optimize  -O3  n bool prime(long long int n) { n   if (n == 1) return false; n   if (n == 2 || n == 3) return true; n   long long int k = sqrt(n) + 1; n   for (long long int i = 2; i < k; i++) { n     if (n % i == 0) return false; n   } n   return true; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   long long int x; n   cin >> x; n   long long int b[x]; n   for (long long int i = 0; i < x; i++) { n     cin >> b[i]; n   } n   long long int y; n   cin >> y; n   long long int g[y]; n   for (long long int i = 0; i < y; i++) { n     cin >> g[i]; n   } n   sort(b, b + x); n   sort(g, g + y); n   long long int ans = 0; n   for (long long int i = 0; i < x; i++) { n     for (long long int j = 0; j < y; j++) { n       long long int val = abs(b[i] - g[j]); n       if (val <= 1) { n         g[j] = 99999; n         ans++; n         break; n         ; n       } n     } n   } n   cout << ans; n   cout << endl; n   ; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, s, inp; n int c[10]; n int best = -1; n int b3, b4, b5; n struct bezout { n   long long d, a, b; n }; n bezout extgcd(long long x, long long y) { n   if (y == 0) return (bezout){x, 1, 0}; n   bezout s = extgcd(y, x % y); n   return (bezout){s.d, s.b, s.a - x / y * s.b}; n } n void check(int k3, int k4, int k5) { n   int a = k3 * c[3], b = k4 * c[4], z = k5 * c[5]; n   if (a + b + z != s) return; n   if (k3 < 0 || k3 > k4 || k4 > k5 || k3 > k5) return; n   int ct = abs(a - b) + abs(z - b); n   if (best == -1 || best > ct) { n     best = ct; n     b3 = k3; n     b4 = k4; n     b5 = k5; n   } n } n int main() { n   scanf( %d %d , &n, &s); n   for (int i = 0; i < n; i++) { n     scanf( %d , &inp); n     c[inp]++; n   } n   for (int k4 = 0; c[4] * k4 <= s; k4++) { n     int C = s - c[4] * k4; n     bezout t = extgcd(c[3], c[5]); n     if (C % t.d != 0) continue; n     long long k3 = t.a * C / t.d; n     long long k5 = t.b * C / t.d; n     int lcm = c[3] * c[5] / t.d; n     int inc3 = lcm / c[3], inc5 = lcm / c[5]; n     if (k3 >= 0) { n       int mod = k3 / inc3; n       k3 = k3 % inc3; n       k5 = k5 + mod * inc5; n     } n     if (k3 < 0) { n       int mod = -k3 / inc3; n       if ((-k3) % inc3 != 0) mod++; n       k3 = k3 + inc3 * mod; n       k5 = k5 - inc5 * mod; n     } n     check(k3, k4, k5); n     int st = 0, ed = s; n     while (st < ed) { n       int m = (st + ed + 1) / 2; n       int nk5 = k5 - m * inc5; n       if (k3 + m * inc3 <= k4 && nk5 >= k4 && c[5] * nk5 >= c[4] * k4) n         st = m; n       else n         ed = m - 1; n     } n     int maxk3 = k3 + inc3 * st; n     int maxk5 = k5 - inc5 * st; n     check(maxk3, k4, maxk5); n     check(maxk3 + inc3, k4, maxk5 - inc5); n   } n   if (best == -1) n     printf( %d n , best); n   else n     printf( %d %d %d n , b3, b4, b5); n } n


#include <bits/stdc++.h> n using namespace std; n int res; n int a[100], ins[100]; n int IsLucky(int x) { n   for (; x; x /= 10) n     if (x % 10 != 4 && x % 10 != 7) return 0; n   return 1; n } n void Dfs(int now, int lim) { n   res++; n   if ((long long)now * 10 + 4 <= lim) Dfs(now * 10 + 4, lim); n   if ((long long)now * 10 + 7 <= lim) Dfs(now * 10 + 7, lim); n } n int main() { n   int n, m, k; n   scanf( %d%d , &n, &m); n   long long cnt = 1; n   for (int i = 1;; i++) { n     cnt *= i; n     if (cnt >= m) { n       k = i; n       break; n     } n   } n   if (k > n) { n     puts( -1 ); n     return 0; n   } n   memset(ins, 0, sizeof(ins)); n   for (int i = 0; i < k; i++) { n     cnt = 1; n     for (int j = 1; j < k - i; j++) cnt *= j; n     int ret = (m - 1) / cnt; n     m -= ret * cnt; n     for (int j = 0; j < k; j++) n       if (!ins[j]) { n         if (ret == 0) { n           ins[j] = 1; n           a[i] = j; n           break; n         } else n           ret--; n       } n   } n   res = 0; n   for (int i = 0; i < k; i++) n     if (IsLucky(n - k + 1 + i) && IsLucky(n - k + 1 + a[i])) res++; n   if (4 <= n - k) Dfs(4, n - k); n   if (7 <= n - k) Dfs(7, n - k); n   printf( %d n , res); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 100007; n long long n, x[N], y[N]; n pair<long long, long long> mid(long long a, long long b) { n   long long xx = (x[a] + x[b]) / 2; n   long long yy = (y[a] + y[b]) / 2; n   return {xx, yy}; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cin >> n; n   for (long long i = 0; i < n; ++i) { n     cin >> x[i] >> y[i]; n     x[i] *= 2; n     y[i] *= 2; n   } n   if (n % 2) { n     cout <<  NO n ; n     return 0; n   } n   pair<long long, long long> midpoint = mid(0, n / 2); n   for (long long i = 1; i < n / 2; ++i) { n     pair<long long, long long> cur = mid(i, i + n / 2); n     if (cur != midpoint) { n       cout <<  NO n ; n       return 0; n     } n   } n   cout <<  YES n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int tt; n   cin >> tt; n   while (tt--) { n     int c, m, x; n     cin >> c >> m >> x; n     cout << min(c, min(m, ((c + m + x) / 3))) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long n; n int main() { n   int T; n   scanf( %d , &T); n   while (T--) { n     scanf( %I64d , &n); n     for (long long i = n;; i += i) n       if (i * (n + 1) % (4 * n) == 0) { n         printf( %I64d n , i + 1); n         break; n       } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 550, mod = 998244353, inv2 = 499122177, g = 3; n long long mul[N], inv[N]; n long long ksm(long long f, long long x) { n   long long tot = 1; n   while (x) { n     if (x & 1ll) tot = tot * f % mod; n     f = f * f % mod; n     x >>= 1ll; n   } n   return tot; n } n void pre() { n   long long lim = N - 5; n   mul[0] = inv[0] = 1; n   for (long long i = 1; i <= lim; i++) mul[i] = mul[i - 1] * i % mod; n   inv[lim] = ksm(mul[lim], mod - 2); n   for (long long i = lim - 1; i >= 1; i--) inv[i] = inv[i + 1] * (i + 1) % mod; n } n namespace NTT { n void solve(long long* s, long long* f, long long* g, long long n, long long m) { n   long long A[N]; n   for (long long i = 0; i <= n + m; i++) A[i] = 0; n   for (long long i = 0; i <= n; i++) { n     for (long long j = 0; j <= m; j++) { n       A[i + j] = (A[i + j] + 1ll * f[i] * g[j] % mod) % mod; n     } n   } n   for (long long i = 0; i <= n + m; i++) s[i] = A[i]; n   return; n } n }  // namespace NTT n long long A(long long m, long long n) { n   if (n == 0) return 1; n   if (m == -1) return ksm(-1, n) * mul[n] % mod; n   if (m < n) return 0; n   return mul[m] * inv[m - n] % mod; n } n long long n; n long long p[N], q[N], mp[N]; n long long cnt1, cnt2, cnt0, cntcir; n long long S[N][N], C[N][N], F[N], G[N], H[N]; n bool vis[N]; n int main() { n   pre(); n   cin >> n; n   S[0][0] = C[0][0] = 1; n   for (long long i = 1; i <= n; i++) { n     C[i][0] = 1; n     for (long long j = 1; j <= i; j++) { n       S[i][j] = (S[i - 1][j - 1] + S[i - 1][j] * (i - 1) % mod) % mod; n       C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; n     } n   } n   for (long long i = 1; i <= n; i++) cin >> p[i], mp[p[i]] = i; n   for (long long i = 1; i <= n; i++) cin >> q[i]; n   for (long long i = 1; i <= n; i++) { n     if (!p[i]) { n       long long x = i; n       while (1) { n         vis[x] = 1; n         if (!q[x]) { n           cnt0++; n           break; n         } n         if (!mp[q[x]]) { n           cnt1++; n           break; n         } n         x = mp[q[x]]; n       } n     } n   } n   for (long long i = 1; i <= n; i++) { n     if (p[i] && !vis[i]) { n       long long x = i; n       while (1) { n         if (vis[x]) { n           if (x == i) cntcir++; n           break; n         } n         vis[x] = 1; n         if (!q[x]) { n           cnt2++; n           break; n         } n         if (!mp[q[x]]) break; n         x = mp[q[x]]; n       } n     } n   } n   for (long long i = 0; i <= n; i++) { n     for (long long j = i; j <= cnt1; j++) { n       F[i] = (F[i] + C[cnt1][j] * S[j][i] % mod * n                          A(cnt0 + cnt1 - j - 1, cnt1 - j) % mod) % n              mod; n     } n   } n   for (long long i = 0; i <= n; i++) { n     for (long long j = i; j <= cnt2; j++) { n       G[i] = (G[i] + C[cnt2][j] * S[j][i] % mod * n                          A(cnt0 + cnt2 - j - 1, cnt2 - j) % mod) % n              mod; n     } n   } n   for (long long i = 0; i <= n; i++) { n     H[i] = S[cnt0][i] * mul[cnt0] % mod; n   } n   NTT::solve(F, F, G, n, n); n   NTT::solve(F, F, H, n, n); n   for (long long i = 0; i < n; i++) cout << F[n - cntcir - i] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n char s[100005]; n int cnt[30]; n int main() { n   int k, i; n   scanf( %s , s); n   scanf( %d , &k); n   for (i = 0; s[i]; i++) cnt[s[i] -  a ]++; n   if (k >= i) { n     puts( 0 n ); n     return 0; n   } n   while (1) { n     int minx = 1 << 30, mini; n     for (i = 0; i < 30; i++) { n       if (cnt[i]) { n         minx = min(cnt[i], minx); n         if (minx == cnt[i]) mini = i; n       } n     } n     if (k >= cnt[mini]) { n       k -= cnt[mini]; n       cnt[mini] = 0; n     } else n       break; n   } n   int ans = 0; n   for (i = 0; i < 30; i++) n     if (cnt[i]) ans++; n   printf( %d n , ans); n   for (i = 0; s[i]; i++) { n     if (cnt[s[i] -  a ]) putchar(s[i]); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int n, p; n vector<int> adj[100005]; n vector<int> ans; n vector<int> vis; n int dfs(int s) { n   if (s >= n) return 0; n   vis[s] = 1; n   int flag = 0; n   for (int i = 0; i < adj[s].size(); i++) { n     if (!vis[adj[s][i]]) { n       flag = 1; n       ans[s] += dfs(adj[s][i]); n     } n   } n   if (!flag) { n     ans[s] = 1; n   } n   return ans[s]; n } n int main() { n   cin >> n; n   ans.resize(n, 0); n   vis.resize(n, 0); n   for (int i = 0; i < n - 1; i++) { n     cin >> p; n     adj[i + 1].push_back(p - 1); n     adj[p - 1].push_back(i + 1); n   } n   dfs(0); n   sort(ans.begin(), ans.end()); n   for (int i = 0; i < n; i++) { n     cout << ans[i] <<    ; n   } n   cout << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   vector<int> a(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   vector<int> ind; n   int num = 0, add = 0; n   for (int i = 0; i < n; ++i) { n     if (a[i] == i) n       ++num; n     else if (a[a[i]] == i) n       add = 2; n   } n   if (!add) add = 1; n   if (num == n) add = 0; n   cout << num + add << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename... Args> n void read(Args &...args) { n   ((cin >> args), ...); n } n template <typename... Args> n void show(Args... args) { n   ((cout << args <<    ), ...); n } n void solve() { n   int n; n   read(n); n   vector<int> v(n); n   for (auto &it : v) cin >> it; n   int l = 0, r = n - 1, x = 0; n   string s; n   while (l <= r) { n     if (min(v[l], v[r]) > x) { n       if (v[l] < v[r]) { n         s.push_back( L ); n         x = v[l++]; n       } else if (v[r] < v[l]) { n         s.push_back( R ); n         x = v[r--]; n       } else { n         int ll = l + 1, rr = r - 1; n         while (ll < r && v[ll] > v[ll - 1]) ll++; n         while (rr > l && v[rr] > v[rr + 1]) rr--; n         if (ll - l > r - rr) { n           x = ll - l; n           while (x--) s.push_back( L ); n         } else { n           x = r - rr; n           while (x--) s.push_back( R ); n         } n         break; n       } n     } else if (max(v[l], v[r]) > x) { n       if (v[l] > v[r]) { n         s.push_back( L ); n         x = v[l++]; n       } else { n         s.push_back( R ); n         x = v[r--]; n       } n     } else { n       break; n     } n   } n   cout << int(s.size()) <<   n  << s; n } n int32_t main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int T = 1; n   while (T--) solve(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[100005]; n int cmp(int x, int y) { n   int x1 = 10 - x % 10; n   int y1 = 10 - y % 10; n   return x1 < y1; n } n int main() { n   int i, n, m; n   cin >> n >> m; n   for (i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   sort(a + 1, a + 1 + n, cmp); n   for (i = 1; i <= n; i++) { n     if (a[i] == 100) continue; n     int x = 10 - a[i] % 10; n     if (m < x) { n       a[i] += m; n       m = 0; n       break; n     } n     a[i] += x; n     m -= x; n   } n   int s = 0; n   for (i = 1; i <= n; i++) { n     s += a[i] / 10; n   } n   if (m > 0) { n     for (i = 1; i <= n; i++) { n       if (m + a[i] >= 100) { n         s += (100 - a[i]) / 10; n         m -= (100 - a[i]); n       } else { n         s += m / 10; n         m = 0; n         break; n       } n     } n   } n   cout << s << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)1e9 + 10; n const int MOD = (int)1e9 + 7; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int n, a, b, c, t; n   cin >> n >> a >> b >> c >> t; n   vector<int> v(n); n   for (int i = 0; i < n; i++) cin >> v[i]; n   if (c < b) return cout << n * a, 0; n   long long ans = 0; n   for (int i = 0; i < n; i++) ans += a + (t - v[i]) * (c - b); n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long inf = 1e18; n const int N = 510000, M = N << 1; n int n, hd[N], to[M], nxt[M], noedg = 1, size[N]; n long long buc[N], f[N], ans = inf; n vector<int> app[N]; n template <class T> n inline void read(T &x) { n   x = 0; n   char ch = getchar(), w = 0; n   while (!isdigit(ch)) w = (ch ==  - ), ch = getchar(); n   while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); n   x = w ? -x : x; n   return; n } n inline void addEdg(int x, int y) { n   nxt[++noedg] = hd[x], hd[x] = noedg, to[noedg] = y; n   nxt[++noedg] = hd[y], hd[y] = noedg, to[noedg] = x; n   return; n } n inline long long sq(long long x) { return x * x; } n void dfs(int now, int fa) { n   size[now] = 1, f[now] = inf; n   for (register int i = hd[now], v; i; i = nxt[i]) { n     if ((v = to[i]) == fa) continue; n     dfs(v, now), size[now] += size[v]; n   } n   buc[0] = 0, app[now].push_back(0); n   for (register int i = hd[now], v; i; i = nxt[i]) { n     if ((v = to[i]) == fa) continue; n     for (register int j = 0, sz = app[now].size(); j < sz; ++j) n       ans = min(ans, buc[app[now][j]] + f[v] + sq(n - app[now][j] - size[v])); n     if (buc[size[v]] == inf) app[now].push_back(size[v]); n     buc[size[v]] = min(buc[size[v]], f[v]); n     f[now] = min(f[now], f[v] + sq(size[now] - size[v])); n   } n   for (register int i = 0, sz = app[now].size(); i < sz; ++i) n     buc[app[now][i]] = inf; n   if (f[now] == inf) f[now] = 1; n   return; n } n int main() { n   int x, y; n   read(n); n   for (register int i = 1; i < n; ++i) n     read(x), read(y), addEdg(x, y), buc[i] = inf; n   dfs(1, 0); n   cout << ((long long)n * (2 * n - 1) - ans) / 2; n   return 0; n   int qwq; n } n


#include <bits/stdc++.h> n #pragma optimization_level 3 n #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math,O3 ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx ) n using namespace std; n const long long N = 5e5 + 3; n const long long mod = 998244353; n const long long INF = 0x7f7f7f7f7f7f7f7f; n const int INFi = 0x7f7f7f7f; n long long test = 1, n, m, x, y, k, b[N], a[N]; n vector<long long> v; n long long ch(int l, int r) { n   long long mx = max(a[l], a[r]), res = 0; n   set<long long> s1, s2; n   for (int i = l + 1; i < r; i++) { n     if (a[i] > mx) s1.insert(i); n     s2.insert(i); n   } n   if (s2.size() < k and s1.size()) { n     cout << -1 <<   n ; n     exit(0); n   } n   if (k * x > y) { n     res = (s2.size() / k) * y + (s2.size() % k) * x; n   } else { n     if (s1.size()) n       res = y + (s2.size() - k) * x; n     else n       res = s2.size() * x; n   } n   return res; n } n void solve() { n   cin >> n >> m >> y >> k >> x; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= m; i++) cin >> b[i]; n   a[0] = a[n + 1] = b[0] = b[m + 1] = 0; n   int j = 1; n   v.push_back(0); n   for (int i = 1; i <= n; i++) n     if (a[i] == b[j]) j++, v.push_back(i); n   v.push_back(n + 1); n   if (j != m + 1) { n     cout << -1 <<   n ; n     return; n   } n   long long ans = 0; n   for (int i = 1; i <= m + 1; i++) { n     ans += ch(v[i - 1], v[i]); n   } n   cout << ans <<   n ; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cout << fixed; n   cout << setprecision(15); n   ; n   for (int i = 1; i <= test; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long INF = (long long)1e9 + 999999; n const long long Mod = (long long)1e9 + 7; n const long long MaXN = (long long)1e18; n const int N = (int)1e6 + 7; n const int MaXI = (int)1e9; n const int Mass = (int)2e5; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n   int n; n   cin >> n; n   string s; n   cin >> s; n   int b = 0, g = 0, r = 0; n   for (int i = 0; i < s.size(); i++) { n     if (s[i] ==  B ) { n       b++; n     } n     if (s[i] ==  G ) { n       g++; n     } n     if (s[i] ==  R ) { n       r++; n     } n   } n   if (b == n) { n     cout <<  B ; n     return 0; n   } else if (g == n) { n     cout <<  G ; n     return 0; n   } else if (r == n) { n     cout <<  R ; n     return 0; n   } n   if (r > 1 || g > 1) { n     cout <<  B ; n   } else if (r == 1 && g == 1) { n     cout <<  B ; n   } n   if (b > 1 || r > 1) { n     cout <<  G ; n   } else if (b == 1 && r == 1) { n     cout <<  G ; n   } n   if (b > 1 || g > 1) { n     cout <<  R ; n   } else if (b == 1 && g == 1) { n     cout <<  R ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n ostream &operator<<(ostream &out, vector<long long> &a) { n   for (auto i : a) out << i <<    ; n   return out; n } n istream &operator>>(istream &in, vector<long long> &a) { n   for (auto &i : a) in >> i; n   return in; n } n int main() { n   cin.tie(0); n   cout.tie(0); n   ios_base::sync_with_stdio(0); n   long long q; n   cin >> q; n   while (q--) { n     string a, b, c; n     cin >> a >> b >> c; n     bool boo = 0; n     for (long long i = 0; i < a.size(); ++i) { n       if (c[i] != b[i] && c[i] != a[i]) { n         cout <<  NO  n              <<   n ; n         boo = 1; n         break; n       } n     } n     if (!boo) n       cout <<  YES  n            <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, q; n   char c; n   vector<int> a(10); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> c; n     if (c ==  L ) { n       q = 0; n       while (a[q] == 1) { n         q++; n       } n       a[q] = 1; n     } else if (c ==  R ) { n       q = 9; n       while (a[q] == 1) { n         q--; n       } n       a[q] = 1; n     } else { n       q = (int)c - 48; n       a[q] = 0; n     } n   } n   for (int i = 0; i < 10; i++) { n     cout << a[i]; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int bits[32]; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, m; n   cin >> n >> m; n   vector<long long> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n   } n   for (int i = 0; i < m; i++) { n     int x; n     cin >> x; n     bits[x]++; n   } n   sort(v.begin(), v.end()); n   long long ans = 0; n   for (int j = 0; j < 31; j++) { n     for (auto &x : v) { n       if ((x & 1) && bits[j]) { n         x--; n         ans++; n         bits[j]--; n       } n     } n     for (auto &x : v) { n       while (x && bits[j]) { n         x--; n         ans++; n         bits[j]--; n       } n     } n     for (auto &x : v) { n       x >>= 1; n     } n   } n   cout << ans <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 1009; n pair<int, pair<int, int> > sr[Maxn * Maxn]; n int n, m; n int check(int k) { n   bitset<Maxn> row[Maxn]; n   for (int i = 0; i <= k; i++) { n     int x = sr[i].second.first, y = sr[i].second.second; n     row[x].set(y); n   } n   for (int i = 0; i < n; i++) n     for (int j = i + 1; j < n; j++) n       if ((row[i] & row[j]).count() >= 2) return 1; n   return 0; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < n; i++) n     for (int j = 0; j < m; j++) { n       int p; n       scanf( %d , &p); n       sr[i * m + j] = make_pair(-p, make_pair(i, j)); n     } n   sort(sr, sr + n * m); n   int l = 0, r = n * m - 1; n   while (l < r - 1) { n     int mid = l + r >> 1; n     if (check(mid)) n       r = mid; n     else n       l = mid; n   } n   cout << -sr[r].first << endl; n } n


#include <bits/stdc++.h> n using namespace std; n double dp[128][128]; n int x[128], y[128]; n int N, K, x_0, y_0; n double eps; n double dist(double x1, double y1) { n   return sqrt((x_0 - x1) * (x_0 - x1) + (y_0 - y1) * (y_0 - y1)); n } n double hit(double R, double D) { n   return (D <= R) ? 1. : exp(1 - D * D / R / R); n } n double P(double R) { n   fill(*dp, *dp + 128 * 128, 0.); n   dp[0][1] = hit(R, dist(x[0], y[0])); n   dp[0][0] = 1 - dp[0][1]; n   for (int i = 1; i < N; i++) n     for (int j = 0; j <= N; j++) { n       double ph = hit(R, dist(x[i], y[i])); n       dp[i][j] += (1 - ph) * dp[i - 1][j]; n       if (j >= 1) dp[i][j] += ph * dp[i - 1][j - 1]; n     } n   double ret = 0.; n   for (int j = 0; j < K; j++) ret += dp[N - 1][j]; n   return ret; n } n int main() { n   cin >> N >> K >> eps >> x_0 >> y_0; n   eps /= 1000.; n   for (int i = 0; i < N; i++) cin >> x[i] >> y[i]; n   double lo = 0., hi = 1000000.; n   for (int x = 0; x < 50; x++) { n     double ave = (lo + hi) / 2.; n     if (P(ave) <= eps) n       hi = ave; n     else n       lo = ave; n   } n   cout << setprecision(10) << fixed << hi << endl; n } n


#include <bits/stdc++.h> n using namespace std; n long long inf; n const double eps = 1e-8; n const double pi = acos(-1.0); n template <class T> n long long chkmin(T &a, T b) { n   return a > b ? a = b, 1 : 0; n } n template <class T> n long long chkmax(T &a, T b) { n   return a < b ? a = b, 1 : 0; n } n template <class T> n T sqr(T a) { n   return a * a; n } n template <class T> n T mmin(T a, T b) { n   return a < b ? a : b; n } n template <class T> n T mmax(T a, T b) { n   return a > b ? a : b; n } n template <class T> n T aabs(T a) { n   return a < 0 ? -a : a; n } n template <class T> n long long dcmp(T a, T b) { n   return a > b; n } n template <long long *a> n long long cmp_a(long long first, long long second) { n   return a[first] < a[second]; n } n struct __INIT__ { n   __INIT__() { memset(&inf, 0x3f, sizeof(inf)); } n } __INIT___; n namespace io { n const long long SIZE = (1 << 21) + 1; n char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, n                                        qu[55]; n long long f, qr; n inline void flush() { n   fwrite(obuf, 1, oS - obuf, stdout); n   oS = obuf; n } n inline void putc(char first) { n   *oS++ = first; n   if (oS == oT) flush(); n } n template <typename A> n inline bool read(A &first) { n   for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                               (iS == iT ? EOF : *iS++)) n                             : *iS++); n        c <  0  || c >  9 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     if (c ==  - ) n       f = -1; n     else if (c == EOF) n       return 0; n   for (first = 0; c <=  9  && c >=  0 ; n        c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                        (iS == iT ? EOF : *iS++)) n                      : *iS++)) n     first = first * 10 + (c & 15); n   first *= f; n   return 1; n } n inline bool read(char &first) { n   while ((first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                                (iS == iT ? EOF : *iS++)) n                             : *iS++)) ==     || n          first ==   n  || first ==   r ) n     ; n   return first != EOF; n } n inline bool read(char *first) { n   while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                                 (iS == iT ? EOF : *iS++)) n                              : *iS++)) ==   n  || n          *first ==     || *first ==   r ) n     ; n   if (*first == EOF) return 0; n   while (!(*first ==   n  || *first ==     || *first ==   r )) n     *(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), n                               (iS == iT ? EOF : *iS++)) n                            : *iS++); n   *first = 0; n   return 1; n } n template <typename A, typename... B> n inline bool read(A &first, B &...second) { n   return read(first) && read(second...); n } n template <typename A> n inline bool write(A first) { n   if (!first) putc( 0 ); n   if (first < 0) putc( - ), first = -first; n   while (first) qu[++qr] = first % 10 +  0 , first /= 10; n   while (qr) putc(qu[qr--]); n   return 0; n } n inline bool write(char first) { n   putc(first); n   return 0; n } n inline bool write(const char *first) { n   while (*first) { n     putc(*first); n     ++first; n   } n   return 0; n } n inline bool write(char *first) { n   while (*first) { n     putc(*first); n     ++first; n   } n   return 0; n } n template <typename A, typename... B> n inline bool write(A first, B... second) { n   return write(first) || write(second...); n } n struct Flusher_ { n   ~Flusher_() { flush(); } n } io_flusher_; n }  // namespace io n using io ::putc; n using io ::read; n using io ::write; n char strs[1000005]; n char *s[100005]; n long long to[100005][26], t = 1, fail[100005]; n long long c[100005]; n long long ed[100005]; n long long q[100005], *l, *r; n long long cnt[201][100005]; n long long is[100005]; n long long ans[100005]; n vector<pair<long long, long long> > ask[100005]; n const long long S = 500; n long long dfn[100005], low[100005], su[100005]; n long long cl; n void add(long long first, long long second) { n   for (; first <= cl; first += first & -first) su[first] += second; n } n long long sum(long long first) { n   long long second = 0; n   for (; first; first ^= first & -first) second += su[first]; n   return second; n } n vector<long long> son[100005]; n void dfs(long long first) { n   dfn[first] = ++cl; n   for (auto i : son[first]) dfs(i); n   low[first] = cl; n } n signed main() { n   long long n, m; n   read(n, m); n   s[1] = strs; n   for (long long i = 1; i <= n; ++i) { n     read(s[i]); n     s[i + 1] = s[i] + strlen(s[i]); n     long long at = 1; n     for (char *j = s[i]; j < s[i + 1]; ++j) { n       if (!to[at][*j -  a ]) to[at][*j -  a ] = ++t; n       at = to[at][*j -  a ]; n     } n     ed[i] = at; n   } n   *(l = r = q) = 1; n   fail[1] = 1; n   while (l <= r) { n     for (long long i = 0; i < 26; ++i) n       if (to[*l][i]) { n         *(++r) = to[*l][i]; n         if (*l == 1) { n           fail[*r] = 1; n         } else { n           long long at = fail[*l]; n           while (at != 1 && !to[at][i]) at = fail[at]; n           if (to[at][i]) n             fail[*r] = to[at][i]; n           else n             fail[*r] = 1; n         } n         son[fail[*r]].push_back(*r); n       } n     ++l; n   } n   l = q; n   while (l <= r) { n     for (long long i = 0; i < 26; ++i) n       if (!to[*l][i]) { n         if (*l == 1) n           to[*l][i] = 1; n         else n           to[*l][i] = to[fail[*l]][i]; n       } n     ++l; n   } n   long long ct = 0; n   for (long long i = 1; i <= n; ++i) { n     if (s[i + 1] - s[i] > S) { n       is[i] = ++ct; n       long long at = 1; n       for (char *j = s[i]; j < s[i + 1]; ++j) { n         at = to[at][*j -  a ]; n         ++c[at]; n       } n       for (long long *j = r;; --j) { n         c[fail[*j]] += c[*j]; n         if (j == q) break; n       } n       for (long long j = 1; j <= n; ++j) cnt[ct][j] = cnt[ct][j - 1] + c[ed[j]]; n       for (long long j = 1; j <= t; ++j) c[j] = 0; n     } n   } n   for (long long i = 1; i <= m; ++i) { n     long long l, r, k; n     read(l, r, k); n     if (s[k + 1] - s[k] > S) { n       ans[i] = cnt[is[k]][r] - cnt[is[k]][l - 1]; n     } else { n       ask[r].push_back(make_pair(k, i)); n       ask[l - 1].push_back(make_pair(k, -i)); n     } n   } n   dfs(1); n   for (long long i = 1; i <= n; ++i) { n     add(dfn[ed[i]], 1); n     add(low[ed[i]] + 1, -1); n     for (auto j : ask[i]) { n       long long aid = j.second, tp = 1; n       if (aid < 0) { n         aid = -aid; n         tp = -1; n       } n       long long at = 1; n       for (char *l = s[j.first]; l < s[j.first + 1]; ++l) { n         at = to[at][*l -  a ]; n         ans[aid] += tp * sum(dfn[at]); n       } n     } n   } n   for (long long i = 1; i <= m; ++i) write(ans[i],   n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int main() { n   int i, j; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= n / 2; i++) { n     for (j = 1; j <= m; j++) { n       printf( %d %d n , i, j); n       printf( %d %d n , n - i + 1, m + 1 - j); n     } n   } n   if (n % 2 == 1) { n     for (j = 1; j <= m / 2; j++) { n       printf( %d %d n , (n + 1) / 2, j); n       printf( %d %d n , (n + 1) / 2, m + 1 - j); n     } n     if (m % 2 == 1) printf( %d %d n , (n + 1) / 2, (m + 1) / 2); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int t; n   cin >> t; n   while (t--) { n     long long int a, b; n     cin >> a >> b; n     if (a % b == 0) { n       cout << 0 <<   n ; n     } else { n       long long int g = a / b; n       long long int ans = (b * (g + 1) - a); n       cout << ans <<   n ; n     } n   } n   return 0; n } n


#include <iostream> n #include <cstdio> n #include <cassert> n #include <cstring> n #include <cmath> n #include <functional> n #include <algorithm> n #include <utility> n #include <vector> n #include <string> n #include <map> n #include <set> n using namespace std; n using ll = long long; n using PII = pair<int,int>; n const ll inf = 1ll << 60; n const int maxn = 200000 + 5; n  n int n, s; n  n struct DS { n   ll base = 0; n   map<int, ll> f; n   set<pair<ll,int> > bag; n  n   int size() { n     return f.size(); n   } n  n   bool count(int x) { n     return f.count(x); n   } n  n   ll get(int x) { n     if (f.count(x)) { n       return f[x] + base; n     } else { n       return inf; n     } n   } n  n   void set(int x, ll v) { n     if (f.count(x)) { n       v = std::min(v, erase(x)); n     } n     v -= base; n     f[x] = v; n     bag.emplace(v, x); n   } n  n   ll erase(int x) { n     if (f.count(x)) { n       ll val = f[x]; n       f.erase(x); n       bag.erase({ val, x }); n       return val + base; n     } else { n       return inf; n     } n   } n  n   ll min() { n     return bag.begin()->first + base; n   } n }; n  n int main() { n   scanf( %d%d , &n, &s); n   vector<DS> stk { {} }; n   vector<int> cond; n   stk.back().set(0, 0); n   for (int i = 1; i <= n; i++) { n     char op[8]; n     scanf( %s , op); n     if (op[0] ==  s ) { n       int x, v; n       scanf( %d%d , &x, &v); n       ll mn = stk.back().min(); n       stk.back().base += v; n       if (x != s) { n         stk.back().set(x, mn); n       } n     } else if (op[0] ==  i ) { n       int x; n       scanf( %d , &x); n       DS u; n       u.set(x, stk.back().get(x)); n       stk.back().erase(x); n       stk.push_back(u); n       cond.push_back(x); n     } else if (op[0] ==  e ) { n       int c = cond.back(); n       cond.pop_back(); n  n       int sz = stk.size(); n       auto& x = stk[sz - 1]; n       auto& y = stk[sz - 2]; n  n       if (x.size() > y.size()) swap(x, y); n       for (auto u: x.f) { n         y.set(u.first, x.get(u.first)); n       } n       stk.pop_back(); n     } n   } n   printf( %lld n , stk.back().min()); n   return 0; n }


#include <bits/stdc++.h> n using namespace std; n long long n, ans; n vector<long long> a; n int main() { n   cin >> n; n   a.resize(n); n   long long min_A = 1 << 31 - 1; n   long long max_A = -1; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     if (a[i] > max_A) { n       max_A = a[i]; n     } n     if (a[i] < min_A) { n       min_A = a[i]; n     } n   } n   for (int i = 0; i < n; i++) { n     if (a[i] < max_A && a[i] > min_A) { n       ans++; n     } n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long sol[200000]; n long long solve(int in, vector<int> v[], int visited[], int a[]) { n   if (sol[in] != -2e16) { n     return sol[in]; n   } n   long long ans = a[in]; n   visited[in] = 1; n   for (int i = 0; i < v[in].size(); i++) { n     if (visited[v[in][i]] == 0) { n       ans += solve(v[in][i], v, visited, a); n     } n   } n   visited[in] = 0; n   sol[in] = ans; n   return ans; n } n long long max1[200000]; n long long maxx(int in, vector<int> v[], int visited[], long long sol[]) { n   if (max1[in] != -2e16) { n     return max1[in]; n   } n   long long ans = sol[in]; n   visited[in] = 1; n   for (int i = 0; i < v[in].size(); i++) { n     if (visited[v[in][i]] == 0) { n       long long xx = maxx(v[in][i], v, visited, sol); n       if (xx > ans) { n         ans = xx; n       } n     } n   } n   visited[in] = 0; n   max1[in] = ans; n   return ans; n } n int main() { n   int n; n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n   } n   vector<int> v[n]; n   for (int i = 0; i < n - 1; i++) { n     int x, y; n     cin >> x >> y; n     v[x - 1].push_back(y - 1); n     v[y - 1].push_back(x - 1); n   } n   int vis[n]; n   for (int i = 0; i < n; i++) { n     vis[i] = 0; n   } n   for (int i = 0; i < 200000; i++) { n     sol[i] = -2e16; n     max1[i] = -2e16; n   } n   long long xx = solve(0, v, vis, a); n   long long xy = maxx(0, v, vis, sol); n   queue<int> q; n   q.push(0); n   long long ans = -2e16; n   while (!q.empty()) { n     int p = q.front(); n     q.pop(); n     vis[p] = 1; n     priority_queue<long long> pq; n     for (int i = 0; i < v[p].size(); i++) { n       if (vis[v[p][i]] == 0) { n         pq.push(max1[v[p][i]]); n         q.push(v[p][i]); n       } n     } n     if (pq.size() < 2) { n       continue; n     } else { n       long long val = pq.top(); n       pq.pop(); n       val += pq.top(); n       if (val > ans) { n         ans = val; n       } n     } n   } n   if (ans == -2e16) { n     cout <<  Impossible  << endl; n     return 0; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long G[500][500], d[500][500]; n long long x[500], n, m; n void floyd(long long k) { n   for (long long i = 0; i < n; i++) n     if (i != k) { n       for (long long j = 0; j < n; j++) n         if (i != j && j != k) n           if (G[i][j] > G[i][k] + G[k][j]) { n             G[i][j] = G[i][k] + G[k][j]; n           } n     } n } n void build_graph() { n   for (long long i = 0; i < n; i++) { n     for (long long j = 0; j < n; j++) { n       scanf( %I64d , &G[i][j]); n     } n   } n   for (long long i = 0; i < n; i++) { n     scanf( %I64d , &x[n - i - 1]); n     x[n - i - 1]--; n   } n } n void solve() { n   m = 0; n   long long ans[500] = {0}; n   memcpy(d, G, sizeof(G)); n   for (long long i = 0; i < n; i++) { n     m++; n     floyd(x[i]); n     for (long long j = 0; j < m; j++) { n       for (long long k = 0; k < m; k++) { n         ans[n - i - 1] += G[x[j]][x[k]]; n       } n     } n   } n   printf( %I64d , ans[0]); n   for (long long i = 1; i < n; i++) printf(  %I64d , ans[i]); n   putchar(10); n } n int main() { n   while (~scanf( %I64d , &n)) { n     build_graph(); n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char inp[200005]; n int main() { n   bool ok; n   queue<int> zeroes, ones; n   vector<int> vec[200005]; n   int n_vec = 0, n_z = 0, n_o = 0, tam; n   scanf( %s , inp); n   tam = strlen(inp); n   if (inp[0] ==  1 ) n     cout << -1 << endl; n   else { n     vec[n_vec++].push_back(0); n     zeroes.push(0); n     n_z++; n     ok = true; n     for (int i = 1; i < tam; i++) { n       if (inp[i] ==  0 ) { n         if (n_o == 0) { n           vec[n_vec++].push_back(i); n           zeroes.push(n_vec - 1); n           n_z++; n         } else { n           int q = ones.front(); n           vec[q].push_back(i); n           zeroes.push(q); n           n_z++; n           ones.pop(); n           n_o--; n         } n       } else { n         if (n_z == 0) { n           ok = false; n           break; n         } else { n           int q = zeroes.front(); n           vec[q].push_back(i); n           ones.push(q); n           n_o++; n           zeroes.pop(); n           n_z--; n         } n       } n     } n     if (n_o != 0) ok = false; n     if (!ok) n       printf( -1 n ); n     else { n       printf( %d n , n_vec); n       vector<int>::iterator vit; n       for (int i = 0; i < n_vec; i++) { n         printf( %d , vec[i].size()); n         vit = vec[i].begin(); n         while (vit != vec[i].end()) { n           printf(  %d , (*vit) + 1); n           vit++; n         } n         printf(  n ); n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int x1[200010]; n int x2[200010]; n int yz1[200010]; n int yz2[200010]; n int n; n pair<pair<int, int>, pair<int, int> > pp[200010]; n pair<pair<int, int>, pair<int, int> > qq[200010]; n bool isValid(int x1, int y1, int x2, int y2) { n   if (x1 <= x2 && y1 <= y2) { n     return true; n   } n   return false; n } n pair<pair<int, int>, pair<int, int> > fn(int x1, int y1, int x2, int y2, int x3, n                                          int y3, int x4, int y4) { n   return make_pair(make_pair(max(x1, x3), max(y1, y3)), n                    make_pair(min(x2, x4), min(y2, y4))); n } n int main() { n   int INF = INT_MAX; n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> x1[i] >> yz1[i] >> x2[i] >> yz2[i]; n   } n   pp[0].first.first = x1[0]; n   pp[0].first.second = yz1[0]; n   pp[0].second.first = x2[0]; n   pp[0].second.second = yz2[0]; n   for (int i = 1; i < n; i++) { n     pair<pair<int, int>, pair<int, int> > uu; n     uu = fn(pp[i - 1].first.first, pp[i - 1].first.second, n             pp[i - 1].second.first, pp[i - 1].second.second, x1[i], yz1[i], n             x2[i], yz2[i]); n     if (isValid(uu.first.first, uu.first.second, uu.second.first, n                 uu.second.second)) { n       pp[i] = uu; n     } else { n       pp[i] = make_pair(make_pair(INF, INF), make_pair(-INF, -INF)); n     } n   } n   qq[n - 1].first.first = x1[n - 1]; n   qq[n - 1].first.second = yz1[n - 1]; n   qq[n - 1].second.first = x2[n - 1]; n   qq[n - 1].second.second = yz2[n - 1]; n   for (int i = n - 2; i >= 0; i--) { n     pair<pair<int, int>, pair<int, int> > uu; n     uu = fn(qq[i + 1].first.first, qq[i + 1].first.second, n             qq[i + 1].second.first, qq[i + 1].second.second, x1[i], yz1[i], n             x2[i], yz2[i]); n     if (isValid(uu.first.first, uu.first.second, uu.second.first, n                 uu.second.second)) { n       qq[i] = uu; n     } else { n       qq[i] = make_pair(make_pair(INF, INF), make_pair(-INF, -INF)); n     } n   } n   for (int i = 0; i < n; i++) { n     if (i == 0) { n       if (isValid(qq[1].first.first, qq[1].first.second, qq[1].second.first, n                   qq[1].second.second)) { n         cout << qq[1].first.first <<     << qq[1].first.second; n         return 0; n       } n     } else if (i == n - 1) { n       if (isValid(pp[n - 2].first.first, pp[n - 2].first.second, n                   pp[n - 2].second.first, pp[n - 2].second.second)) { n         cout << pp[n - 2].first.first <<     << pp[n - 2].first.second; n         return 0; n       } n     } else { n       pair<pair<int, int>, pair<int, int> > uu; n       uu = fn(qq[i + 1].first.first, qq[i + 1].first.second, n               qq[i + 1].second.first, qq[i + 1].second.second, n               pp[i - 1].first.first, pp[i - 1].first.second, n               pp[i - 1].second.first, pp[i - 1].second.second); n       if (isValid(uu.first.first, uu.first.second, uu.second.first, n                   uu.second.second)) { n         cout << uu.first.first <<     << uu.first.second; n         return 0; n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7, siz = 1e6 + 5; n long long t, n, m, k, a[siz], inp[siz], cnt, co[siz], less_right[siz], n     less_left[siz], tree[siz][2]; n pair<long long, long long> p[siz]; n long long get(long long ind, long long id) { n   long long ret = 0; n   for (; ind; ind -= ind & -ind) ret += tree[ind][id]; n   return ret; n } n void upd(long long id, long long ind, long long val) { n   for (; ind <= cnt; ind += ind & -ind) tree[ind][id] += val; n } n int32_t main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   for (long long i = 1; i <= n; i++) cin >> p[i].first, p[i].second = i; n   sort(p + 1, p + 1 + n); n   for (long long i = 1; i <= n; i++) { n     a[p[i].second] = ++cnt; n     inp[p[i].second] = p[i].first; n   } n   for (long long i = 1; i <= n; i++) { n     less_left[i] = get(a[i], 0); n     upd(0, a[i], i); n   } n   for (long long i = n; i >= 1; i--) { n     less_right[i] = get(a[i], 1); n     upd(1, a[i], n - i + 1); n   } n   long long ans = 0; n   for (long long i = 1; i <= n; i++) { n     co[i] = i * (n - i + 1); n     co[i] += less_left[i] * (n - i + 1) + less_right[i] * i; n     co[i] %= mod; n     long long toadd = (co[i] * inp[i]) % mod; n     (ans += toadd) %= mod; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   if (n < 2 * k + 1) { n     cout << -1 << endl; n     return 0; n   } n   cout << n * k << endl; n   for (int i = 0; i < n; i++) n     for (int j = 1; j <= k; j++) n       cout << i + 1 <<     << (i + j) % n + 1 <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 310; n int r[N]; n char g[N][N]; n int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; n int main() { n   int n, m, k; n   scanf( %d %d %d , &n, &m, &k); n   for (int i = 0; i < n; i++) scanf( %s , g[i]); n   int cnt = 0; n   for (int x = 1; x < 150; x++) { n     for (int i = x; i + x - 1 < n; i++) { n       for (int j = x; j + x - 1 < m; j++) { n         if (g[i][j] !=  * ) continue; n         bool ok = true; n         for (int d = 0; d < 4 and ok; d++) { n           int ni = i + dir[d][0] * x; n           int nj = j + dir[d][1] * x; n           if (g[ni][nj] !=  * ) ok = false; n         } n         if (ok) cnt++; n         if (cnt == k) { n           printf( %d %d n , i + 1, j + 1); n           for (int d = 0; d < 4; d++) { n             int ni = i + dir[d][0] * x; n             int nj = j + dir[d][1] * x; n             printf( %d %d n , ni + 1, nj + 1); n           } n           return 0; n         } n       } n     } n   } n   printf( -1 n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = INT_MAX; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long n, m; n   cin >> n >> m; n   if (n > m) { n     swap(n, m); n   } n   if (n == 1) { n     if (m % 6 == 0) { n       cout << n * m; n     } else if (m % 6 == 5) { n       cout << n * m - 1; n     } else if (m % 6 == 4) { n       cout << n * m - 2; n     } else { n       cout << n * m - m % 6; n     } n   } else if (n == 2) { n     if (m == 2) { n       cout << 0; n     } else if (m == 3) { n       cout << 4; n     } else if (m == 7) { n       cout << 12; n     } else { n       cout << m * 2; n     } n   } else { n     cout << n * m / 2 * 2; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int vp, vd, time_s, time_b, dist; n int main() { n   scanf( %d%d%d%d%d , &vp, &vd, &time_s, &time_b, &dist); n   if (vd <= vp) { n     puts( 0 ); n     return 0; n   } n   int ans = 0; n   double now = 1.0 * vp * time_s; n   while (now < dist) { n     double Overtake = now / (vd - vp); n     double loc = now + Overtake * vp; n     if (loc >= dist) break; n     ++ans; n     now = loc; n     double Back = now / vd + 1.0 * time_b; n     now += Back * vp; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 106; n int g[MAX_N][MAX_N]; n int h[MAX_N][MAX_N]; n bool not_empty_row[MAX_N]; n bool not_empty_col[MAX_N]; n bool used_row[MAX_N]; n bool used_col[MAX_N]; n void coppy(int n, int m) { n   for (int i = 1; n >= i; i++) { n     for (int j = 1; m >= j; j++) { n       h[i][j] = g[i][j]; n     } n   } n   memset(used_row, 0, sizeof(used_row)); n   memset(used_col, 0, sizeof(used_col)); n } n bool okayy(int n, int m) { n   bool ret = true; n   for (int i = 1; n >= i; i++) { n     for (int j = 1; m >= j; j++) { n       ret &= (h[i][j] == 0); n     } n   } n   return ret; n } n bool deleted(int type, int x, int val, int n, int m) { n   if (type == 1) { n     int i = x; n     for (int j = 1; m >= j; j++) { n       h[i][j] -= val; n       if (h[i][j] < 0) return false; n     } n     return true; n   } n   if (type == 2) { n     int j = x; n     for (int i = 1; n >= i; i++) { n       h[i][j] -= val; n       if (h[i][j] < 0) return false; n     } n     return true; n   } n } n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   bool not_empty = false; n   for (int i = 1; n >= i; i++) { n     for (int j = 1; m >= j; j++) { n       scanf( %d , &g[i][j]); n       if (g[i][j]) { n         not_empty_row[i] = 1; n         not_empty_col[j] = 1; n         not_empty = true; n       } n     } n   } n   if (!not_empty) { n     puts( 0 ); n     return 0; n   } n   int rrow = -1; n   for (int i = 1; n >= i; i++) { n     if (not_empty_row[i]) { n       rrow = i; n       break; n     } n   } n   if (rrow == -1) cout << 0 / 0 << endl; n   vector<pair<int, pair<int, int> > > ans; n   vector<pair<int, pair<int, int> > > real_ans; n   int meruru = 123456789; n   int xx = 0; n   for (int cnt = 0; g[rrow][1] >= cnt; cnt++) { n     ans.clear(); n     coppy(n, m); n     bool ret = deleted(1, rrow, cnt, n, m); n     xx = 0; n     xx += cnt; n     ans.push_back(make_pair(1, make_pair(rrow, cnt))); n     for (int j = 1; m >= j; j++) { n       xx += h[rrow][j]; n       ans.push_back(make_pair(2, make_pair(j, h[rrow][j]))); n       ret &= deleted(2, j, h[rrow][j], n, m); n     } n     for (int i = 1; n >= i; i++) { n       xx += h[i][1]; n       ans.push_back(make_pair(1, make_pair(i, h[i][1]))); n       ret &= deleted(1, i, h[i][1], n, m); n     } n     if (ret && okayy(n, m)) { n       if (xx < meruru) { n         real_ans = ans; n         meruru = xx; n       } n     } else n       ans.clear(); n   } n   ans = real_ans; n   if (ans.size() == 0) n     puts( -1 ); n   else { n     int cnt = 0; n     for (pair<int, pair<int, int> > i : ans) { n       cnt += i.second.second; n     } n     if (cnt != meruru) cout << 0 / 0 << endl; n     printf( %d n , cnt); n     for (pair<int, pair<int, int> > i : ans) { n       while (i.second.second--) { n         if (i.first == 1) n           printf( row  ); n         else n           printf( col  ); n         printf( %d n , i.second.first); n       } n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1e9 + 7; n const int nax = 3e5 + 10; n long long w[nax]; n long long dp[nax], ans; n vector<pair<int, long long> > v[nax]; n void dfs(int id, int par) { n   vector<long long> temp; n   for (auto i : v[id]) { n     if (i.first == par) continue; n     dfs(i.first, id); n     temp.emplace_back(dp[i.first] - i.second); n     dp[id] = max(dp[id], dp[i.first] - i.second); n   } n   dp[id] += w[id]; n   ans = max(ans, dp[id]); n   sort(temp.begin(), temp.end()); n   reverse(temp.begin(), temp.end()); n   if ((int)temp.size() == 1) ans = max(ans, temp[0] + w[id]); n   if ((int)temp.size() > 1) ans = max(ans, temp[1] + temp[0] + w[id]); n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   int n; n   cin >> n; n   for (int i = 1; i <= n; ++i) cin >> w[i]; n   for (int i = 0; i < n - 1; ++i) { n     int x, y, t; n     cin >> x >> y >> t; n     v[x].emplace_back(y, t); n     v[y].emplace_back(x, t); n   } n   dfs(1, -1); n   cout << ans <<   n ; n   ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double pi = acos(-1.0); n const double eps = 1e-11; n template <class T> n inline void checkmin(T &a, T b) { n   if (b < a) a = b; n } n template <class T> n inline void checkmax(T &a, T b) { n   if (b > a) a = b; n } n template <class T> n inline T sqr(T x) { n   return x * x; n } n template <class T> n inline void bin(T n, int pos) { n   T i, one = 1, two = 2; n   for (i = (one << (pos - 1)); i > 0; i = (i >> 1)) n     cout << ((n & i) ?  1  :  0 ); n   cout << endl; n } n int lsb(unsigned int v) { n   int r; n   static const int MultiplyDeBruijnBitPosition[32] = { n       0,  1,  28, 2,  29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4,  8, n       31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6,  11, 5,  10, 9}; n   r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27]; n   return r + 1; n } n template <class T> n inline int msb(T n) { n   return (n == 0) ? 0 : (1 + msb(n >> 1)); n } n template <class T> n inline T getbit(T n, int pos) { n   return ((n & (1 << pos)) ? 1 : 0); n } n template <class T> n inline int countbit(T n) { n   return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); n } n template <class T> n inline void setbit(T &n, int p) { n   n = n | (1LL << p); n } n template <class T> n inline T gcd(T a, T b) { n   if (a < 0) return gcd(-a, b); n   if (b < 0) return gcd(a, -b); n   return (b == 0) ? a : gcd(b, a % b); n } n bool isequal(vector<int> a, vector<int> b) { n   for (int i = 0; i < (a.size()); i++) n     if (a[i] != b[i]) return false; n   return true; n } n template <class T> n bool allequal(vector<T> a) { n   sort(a.begin(), a.end()); n   return a[0] == a[a.size() - 1]; n } n template <class T> n void printarray(vector<T> a) { n   for (int i = 0; i < (a.size()); i++) cout << a[i] <<    ; n   cout << endl; n } n void printmatrix(vector<vector<int>> a, int n, int m) { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cout << setw(4) << a[i][j]; n     } n     cout << endl; n   } n } n void readmatrix(vector<vector<int>> &a, int n, int m) { n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < m; j++) { n       cin >> a[i][j]; n     } n   } n } n template <class T> n void printsubarray(vector<T> a, int start, int end) { n   if (start < 0 or end > a.size() - 1) cout <<  Out of bounds  << endl; n   for (int i = start; i <= end; i++) cout << a[i] <<    ; n   cout << endl; n } n bool isprime(int n) { n   if (n < 2) return false; n   if (n == 2) return true; n   for (int i = 2; i * i <= n; i++) { n     if (n % i == 0) return false; n   } n   return true; n } n void split(const string &s, char c, vector<string> &result) { n   stringstream ss(s); n   string x; n   while (getline(ss, x, c)) result.push_back(x); n } n void err(vector<string>::iterator it) {} n template <typename T, typename... Args> n void err(vector<string>::iterator it, T a, Args... args) { n   cerr << it->substr((*it)[0] ==    , it->length()) <<   =   << a <<   n ; n   err(++it, args...); n } n void yup() { cout <<  Yes  << endl; } n void nope() { cout <<  No  << endl; } n void Yup() { cout <<  YES  << endl; } n void Nope() { cout <<  NO  << endl; } n void readarray(vector<int> &a, int size) { n   int tmp; n   a.clear(); n   for (int i = 0; i < (size); i++) { n     cin >> tmp; n     a.push_back(tmp); n   } n } n void solvecase() { n   int n, m; n   int k; n   cin >> n; n   pair<int, int> g[111]; n   for (int i = 0; i < n; i++) { n     cin >> g[i].first >> g[i].second; n   } n   bool ok = true; n   if (g[0].first < g[0].second) ok = false; n   for (int i = 1; i < n; i++) { n     if ((g[i].first < g[i - 1].first) or (g[i].second < g[i - 1].second)) n       ok = false; n   } n   for (int i = 1; i < n; i++) { n     int jumpc = g[i].second - g[i - 1].second; n     int jumpp = g[i].first - g[i - 1].first; n     if (jumpc > jumpp) ok = false; n   } n   ok ? Yup() : Nope(); n } n int main() { n   int cases = 1; n   cin >> cases; n   while (cases--) { n     solvecase(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> tree[2 * 100000 + 4]; n long long dp[2 * 100000 + 3][2]; n int value[2 * 100000 + 4]; n void dfs(int x) { n   dp[x][0] = 0; n   dp[x][1] = -200000000005; n   for (int i = 0, sz = tree[x].size(); i < sz; i++) { n     int v = tree[x][i]; n     dfs(v); n     long long tmp1 = dp[x][1], tmp0 = dp[x][0]; n     dp[x][0] = max(tmp0 + dp[v][0], tmp1 + dp[v][1]); n     dp[x][1] = max(tmp1 + dp[v][0], tmp0 + dp[v][1]); n   } n   dp[x][1] = max(dp[x][0] + value[x], dp[x][1]); n } n int main() { n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) { n     int x; n     cin >> x >> value[i]; n     if (i == 1) continue; n     tree[x].push_back(i); n   } n   dfs(1); n   cout << dp[1][1] << endl; n } n