#include <bits/stdc++.h> n using namespace std; n const int MAX_N = 1000, INDEX = 1; n int n, type[MAX_N + INDEX]; n string team[MAX_N + INDEX], city[MAX_N + INDEX]; n bool isUsed[MAX_N + INDEX]; n void get(int ind) { n   isUsed[ind] = true; n   for (int i = 1; i <= n; i++) { n     if (i != ind) { n       if (team[i][0] == team[ind][0] && team[i][1] == team[ind][1] && n           team[i][2] == city[ind][0]) { n         type[i] = 1; n         if (!isUsed[i]) { n           get(i); n         } n       } n     } n   } n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(); n   cout.tie(); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> team[i] >> city[i]; n   } n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       if (team[i][0] == team[j][0] && team[i][1] == team[j][1] && n           team[i][2] == team[j][2]) { n         type[i] = 1, type[j] = 1; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (type[i] == 1 && !isUsed[i]) { n       get(i); n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = i + 1; j <= n; j++) { n       if (team[i][0] == team[j][0] && team[i][1] == team[j][1]) { n         if (team[i][2] == team[j][2] && type[i] == 0 && type[j] == 0) { n           cout <<  NO  << endl; n           return 0; n         } n         if (team[i][2] == city[j][0] && type[i] == 0 && type[j] == 1) { n           cout <<  NO  << endl; n           return 0; n         } n         if (city[i][0] == team[j][2] && type[i] == 1 && type[j] == 0) { n           cout <<  NO  << endl; n           return 0; n         } n         if (city[i][0] == city[j][0] && type[i] == 1 && type[j] == 1) { n           cout <<  NO  << endl; n           return 0; n         } n       } n     } n   } n   cout <<  YES  << endl; n   for (int i = 1; i <= n; i++) { n     if (type[i] == 0) { n       cout << team[i][0] << team[i][1] << team[i][2] <<   n ; n     } n     if (type[i] == 1) { n       cout << team[i][0] << team[i][1] << city[i][0] <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int N; n bool check(int k) { n   int p = (int)ceil(sqrt(k)); n   for (int i = 2; i <= p; ++i) n     if (k % i == 0) return 1; n   return 0; n } n int main() { n   cin >> N; n   int x = 200, y = x + N; n   for (;; ++x, ++y) n     if (check(x) && check(y)) return printf( %d %d n , y, x), 0; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m; n int R[101]; n double dp[100100], sum[100100]; n int main() { n   scanf( %d%d , &n, &m); n   if (m == 1) { n     puts( 1 ); n     return 0; n   } n   int score = 0; n   dp[0] = 1; n   double f = 1. / (m - 1); n   for (int i = 0; i < n; ++i) { n     scanf( %d , R + i); n     score += R[i]; n   } n   for (int i = 0; i < n; ++i) { n     int r = R[i]; n     sum[0] = dp[0]; n     for (int j = 1; j <= min(score - 1, (i + 1) * m); ++j) { n       sum[j] = sum[j - 1] + dp[j]; n     } n     for (int j = min(score - 1, (i + 1) * m); j > 0; --j) { n       dp[j] = sum[j - 1]; n       if (j - m - 1 >= 0) dp[j] -= sum[j - m - 1]; n       if (j - r >= 0) dp[j] -= dp[j - r]; n       dp[j] *= f; n     } n     dp[0] = 0; n   } n   double res = 1; n   for (int i = 0; i < score; ++i) { n     res += dp[i] * (m - 1); n   } n   printf( %.15lf n , res); n   return 0; n } n


#include <bits/stdc++.h> n #pragma warning(disable : 4996) n using namespace std; n int T; n int main() { n   cin >> T; n   while (T--) { n     int a, b, c; n     cin >> c >> b >> a; n     string s; n     if (a > 0) { n       s +=  1 ; n       for (int i = 0; i < a; i++) s +=  1 ; n       if (b > 0 && b % 2 == 1) { n         for (int i = 0; i < b; i++) { n           if (i % 2 == 0) n             s +=  0 ; n           else n             s +=  1 ; n         } n         if (c > 0) { n           if (s[s.length() - 1] !=  0 ) s +=  0 ; n           for (int i = 0; i < c; i++) s +=  0 ; n         } n       } else if (b > 0 && b % 2 == 0) { n         for (int i = 0; i < b - 1; i++) { n           if (i % 2 == 0) n             s +=  0 ; n           else n             s +=  1 ; n         } n         if (c > 0) { n           if (s[s.length() - 1] !=  0 ) s +=  0 ; n           for (int i = 0; i < c; i++) s +=  0 ; n         } n         cout <<  0 ; n         cout << s << endl; n         continue; n       } else if (c > 0) { n         if (s[s.length() - 1] !=  0 ) s +=  0 ; n         for (int i = 0; i < c; i++) s +=  0 ; n       } n     } else if (b > 0) { n       s +=  1 ; n       if (b > 0 && b % 2 == 1) { n         for (int i = 0; i < b; i++) { n           if (i % 2 == 0) n             s +=  0 ; n           else n             s +=  1 ; n         } n         if (c > 0) { n           if (s[s.length() - 1] !=  0 ) s +=  0 ; n           for (int i = 0; i < c; i++) s +=  0 ; n         } n       } else if (b > 0 && b % 2 == 0) { n         for (int i = 0; i < b - 1; i++) { n           if (i % 2 == 0) n             s +=  0 ; n           else n             s +=  1 ; n         } n         if (c > 0) { n           if (s[s.length() - 1] !=  0 ) s +=  0 ; n           for (int i = 0; i < c; i++) s +=  0 ; n         } n         cout <<  0 ; n         cout << s << endl; n         continue; n       } else if (c > 0) { n         if (s[s.length() - 1] !=  0 ) s +=  0 ; n         for (int i = 0; i < c; i++) s +=  0 ; n       } n     } else if (c > 0) { n       s +=  0 ; n       for (int i = 0; i < c; i++) s +=  0 ; n     } n     cout << s << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9 + 7; n const int MOD = 1e9 + 7; n const int di[4] = {1, 0, -1, 0}; n const int dj[4] = {0, 1, 0, -1}; n mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   int t; n   cin >> t; n   while (t--) { n     long long a, b, x, y, n; n     cin >> a >> b >> x >> y >> n; n     long long a2, b2, x2, y2, n2; n     a2 = a, b2 = b, x2 = x, y2 = y, n2 = n; n     long long cnt_to_a = a - x; n     long long cnt_to_a2 = cnt_to_a; n     long long cnt_to_b = b - y; n     long long cnt_to_b2 = cnt_to_b; n     if (cnt_to_a < n) { n       a = x; n       n -= cnt_to_a; n     } else { n       a -= n; n       n = 0; n     } n     if (cnt_to_b < n) { n       b = y; n       n -= cnt_to_b; n     } else { n       b -= n; n       n = 0; n     } n     if (cnt_to_b2 < n2) { n       b2 = y2; n       n2 -= cnt_to_b2; n     } else { n       b2 -= n2; n       n2 = 0; n     } n     if (cnt_to_a2 < n2) { n       a2 = x2; n       n2 -= cnt_to_a2; n     } else { n       a2 -= n2; n       n2 = 0; n     } n     cout << min(a * b, a2 * b2) << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n bool secdes(const pair<long long, long long>& a, n             const pair<long long, long long>& b) { n   if (a.second == b.second) { n     return a.first > b.first; n   } else { n     return a.second > b.second; n   } n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   string s; n   cin >> s; n   long long ind = -1; n   long long l = s.length(), f = 0; n   for (long long i = l - 1; i >= 0; i--) { n     long long k = s[i] - 48; n     if (k % 2 == 0) { n       f += 1; n       if (f == 1) { n         ind = i; n       } n       if (s[l - 1] - 48 > k) { n         ind = i; n       } n     } n   } n   if (f == 0) n     cout << -1; n   else { n     swap(s[ind], s[l - 1]); n     cout << s; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int f[10000050], pre[10000050], used[10000050], edge[105]; n vector<int> vec; n int main() { n   int n, m, i, j, x, y, maxi, size, news; n   scanf( %d%d , &n, &m); n   for (i = 1; i <= m; i++) { n     scanf( %d%d , &x, &y); n     edge[x] |= 1 << y; n     edge[y] |= 1 << x; n   } n   memset(f, 127, sizeof(f)); n   for (i = 1; i <= n; i++) f[1 << i] = 0; n   maxi = 0; n   for (i = 1; i <= n; i++) maxi |= 1 << i; n   for (i = 0; i <= maxi; i++) n     if (f[i] < 999999999) { n       for (j = 1; j <= n; j++) n         if ((1 << j) & i) { n           news = i | edge[j]; n           if (f[i] + 1 < f[news]) { n             f[news] = f[i] + 1; n             used[news] = j; n             pre[news] = i; n           } n         } else if ((i & edge[j]) == i) { n           news = i | (1 << j); n           if (f[i] < f[news]) { n             f[news] = f[i]; n             used[news] = 0; n             pre[news] = i; n           } n         } n     } n   vec.clear(); n   for (i = maxi; (i & -i) != i; i = pre[i]) n     if (used[i]) vec.push_back(used[i]); n   size = vec.size(); n   printf( %d n , size); n   for (i = size - 1; i >= 0; i--) printf( %d  , vec[i]); n   printf(  n ); n   return 0; n } n


#include <bits/stdc++.h> n const int maxn = 50; n using namespace std; n long long n, k, M; n int a[maxn + 5], b[maxn * maxn]; n void ok() { n   long long ans = 0, cot = 0, tot = 0, cnt = 0, sum = 0; n   sort(a + 1, a + k + 1); n   for (int i = 1; i <= k; i++) cot += a[i]; n   for (int i = 0; i <= n; i++) { n     tot = cot * i; n     if (tot > M) break; n     sum = tot, cnt = (k + 1) * i; n     for (int j = 1; j <= k; j++) { n       for (int r = i + 1; r <= n; r++) { n         sum += a[j]; n         cnt++; n         if (sum > M) { n           cnt--, j = k + 1; n           break; n         } n       } n     } n     ans = max(ans, cnt); n   } n   printf( %I64d n , ans); n } n int main() { n   while (~scanf( %I64d %I64d %I64d , &n, &k, &M)) { n     for (int i = 1; i <= k; i++) scanf( %d , &a[i]); n     ok(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10; n int mx = 0; n vector<int> g[N]; n int n; n int col[N]; n int cnt[N][N]; n void gen(int v) { n   if (v == n) { n     for (int i = 0; i < n; i++) n       for (int u : g[i]) { n         int t = col[i]; n         int z = col[u]; n         cnt[min(t, z)][max(t, z)] = 1; n       } n     int cur = 0; n     for (int i = 0; i < 6; i++) n       for (int j = 0; j < 6; j++) { n         cur += cnt[i][j]; n         cnt[i][j] = 0; n       } n     mx = max(mx, cur); n     return; n   } n   for (int i = 0; i < 6; i++) { n     col[v] = i; n     gen(v + 1); n   } n } n int solve() { n   if (!(cin >> n)) return 1; n   int m; n   cin >> m; n   for (int i = 0; i < n; i++) g[i].clear(); n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     x--; n     y--; n     g[x].push_back(y); n     g[y].push_back(x); n   } n   mx = 0; n   gen(0); n   cout << mx << endl; n   return 0; n } n int32_t main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int TET = 1e9; n   for (int i = 1; i <= TET; i++) { n     if (solve()) break; n     cout <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n inline long long rd() { n   long long x = 0; n   bool f = 0; n   char c = getchar(); n   while (!isdigit(c)) { n     if (c ==  - ) f = 1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = (x << 1) + (x << 3) + (c ^ 48); n     c = getchar(); n   } n   return f ? -x : x; n } n long long n, m, l[1000010 << 1], f[1000010 << 1], hd[1000010]; n int main() { n   n = rd(); n   m = rd(); n   for (register long long i = 1; i <= n; ++i) l[i] = l[n + i] = rd(); n   for (register long long i = 1; i <= (n << 1); ++i) l[i] += l[i - 1]; n   while (m--) { n     long long ptr = 0, len = rd(); n     for (register long long i = n + 1; i <= (n << 1); ++i) { n       while (l[i] - l[ptr] > len) ++ptr; n       f[i] = f[ptr] + 1; n       hd[i - n] = (i == ptr ? i : (ptr <= n ? ptr : hd[ptr - n])); n       if (i - hd[i - n] >= n) { n         printf( %I64d n , f[i]); n         break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<pair<long long, long long> > v, par; n long long QN, cmd, nums = 0; n map<long long, long long> ans; n long long solve(long long l, long long r, long long x) { n   long long st = l, en = r; n   while (st <= en) { n     long long mid = (st + en) / 2; n     if (x < v[mid].first) { n       en = mid - 1; n       continue; n     } n     if (x > v[mid].second) { n       st = mid + 1; n       continue; n     } n     if (v[mid].first == v[mid].second && par[mid].first == -1) n       return ans[v[mid].first]; n     x = x - v[mid].first + 1; n     x %= par[mid].first; n     if (x == 0) x = par[mid].first; n     return solve(l, mid, x); n   } n } n long long x, l, r; n int main() { n   cin >> QN; n   while (QN--) { n     cin >> cmd; n     if (cmd == 1) { n       cin >> x; n       ans[++nums] = x; n       v.push_back(make_pair(nums, nums)); n       par.push_back(make_pair(-1, -1)); n     } else { n       cin >> l >> r; n       nums++; n       v.push_back(make_pair(nums, nums + l * r - 1)); n       nums = nums + l * r - 1; n       par.push_back(make_pair(l, -1)); n     } n   } n   cin >> QN; n   while (QN--) { n     cin >> x; n     cout << solve(0, v.size() - 1, x) <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n void imp() { n   cout << -1; n   exit(0); n } n double mn = 0, mx = 2e9; n void solve(int x, int vx, int x1, int x2) { n   if (vx == 0) { n     if (x1 < x && x < x2) return; n     imp(); n   } n   if (vx < 0) { n     mn = std::max(mn, (x2 - x) / (double)vx); n     mx = std::min(mx, (x1 - x) / (double)vx); n   } else { n     mn = std::max(mn, (x1 - x) / (double)vx); n     mx = std::min(mx, (x2 - x) / (double)vx); n   } n   if (mn >= mx) imp(); n } n int main() { n   int n, x1, x2, y1, y2; n   cin >> n >> x1 >> y1 >> x2 >> y2; n   for (int i = 0; i < n; ++i) { n     int x, y, vx, vy; n     cin >> x >> y >> vx >> vy; n     solve(x, vx, x1, x2); n     solve(y, vy, y1, y2); n   } n   cout << setprecision(17) << mn; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   vector<long long> arr(n + 1); n   for (long long i = 0; i < n; i++) cin >> arr[i]; n   long long minute; n   long long mn = 100000000000; n   long long ans = 0; n   long long x; n   for (long long i = 0; i < n; i++) { n     x = arr[i]; n     if ((x - i) <= 0) n       minute = i; n     else { n       minute = n * ceil((x - i) / (long double)n); n       minute += i; n     } n     if (minute < mn) { n       mn = minute; n       ans = i; n     } n   } n   cout << ans + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k1, k2, k3, count[6], min_elt = INT_MAX; n   memset(count, 0, sizeof(count)); n   scanf( %d%d%d , &k1, &k2, &k3); n   count[k1 >= 5 ? 5 : k1]++; n   count[k2 >= 5 ? 5 : k2]++; n   count[k3 >= 5 ? 5 : k3]++; n   min_elt = min(k3, min(k1, k2)); n   if (min_elt >= 4) n     printf( NO n ); n   else if (min_elt == 1) n     printf( YES n ); n   else if (min_elt == 2) { n     if (count[2] >= 2) n       printf( YES n ); n     else if (count[4] == 2) n       printf( YES n ); n     else n       printf( NO n ); n   } else if (count[3] == 3) n     printf( YES n ); n   else n     printf( NO n ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long a, b, c, r, d, t, x1 = 0, x2 = 0; n   cin >> t; n   while (t--) { n     cin >> a >> b >> c >> r; n     d = 0; n     if (a > b) { n       swap(a, b); n     } n     x1 = c - r; n     x2 = c + r; n     if (x2 < a || x1 > b) { n       d = b - a; n     } else if (x1 <= a && x2 >= b) { n       d = 0; n     } else { n       if (a <= x1) { n         d += x1 - a; n       } n       if (b >= x2) { n         d += b - x2; n       } n     } n     cout << d << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 100005, M = 50 * N + 5; n int n, m, k, ans, X[N], Y[N]; n int cnt = 0, hed[M], to[M], nxt[M]; n bool vis[M]; n char s[55]; n int f[M]; n int dfn[M], low[M], bl[M], lb = 0, sum[M]; n int sta[M], top = 0, idx = 0; n inline int g(int u, int d) { return u + d * n; } n inline int Min(int x, int y) { return x < y ? x : y; } n inline int Max(int x, int y) { return x > y ? x : y; } n inline void add(int x, int y) { n   to[++cnt] = y, nxt[cnt] = hed[x], hed[x] = cnt; n } n void tarjan(int u) { n   dfn[u] = low[u] = ++idx, sta[++top] = u; n   for (int i = hed[u]; i; i = nxt[i]) { n     int v = to[i]; n     if (!dfn[v]) n       tarjan(v), low[u] = Min(low[u], low[v]); n     else if (!bl[v]) n       low[u] = Min(low[u], dfn[v]); n   } n   if (dfn[u] == low[u]) { n     for (bl[u] = ++lb, sum[lb] += vis[u]; sta[top] != u; --top) n       bl[sta[top]] = lb, sum[lb] += vis[sta[top]]; n     --top; n   } n } n int main() { n   scanf( %d%d%d , &n, &m, &k); n   for (int i = (1), ied = (m); i <= ied; i++) { n     scanf( %d%d , &X[i], &Y[i]); n     for (int j = (0), jed = (k - 2); j <= jed; j++) n       add(g(X[i], j), g(Y[i], j + 1)); n     add(g(X[i], k - 1), g(Y[i], 0)); n   } n   for (int i = (1), ied = (n); i <= ied; i++) { n     scanf( %s , s); n     for (int j = (0), jed = (k - 1); j <= jed; j++) vis[g(i, j)] = s[j] -  0 ; n   } n   for (int i = (1), ied = (n * k); i <= ied; i++) n     if (!dfn[i]) tarjan(i); n   for (int i = (1), ied = (n); i <= ied; i++) n     for (int j = (0), jed = (k - 1); j <= jed; j++) { n       if (!vis[g(i, j)]) continue; n       for (int h = (j + 1), hed = (k - 1); h <= hed; h++) n         if (vis[g(i, h)] && bl[g(i, h)] == bl[g(i, j)]) n           vis[g(i, h)] = 0, --sum[bl[g(i, j)]]; n     } n   memset(hed, 0, sizeof(hed)), cnt = 0; n   for (int i = (1), ied = (m); i <= ied; i++) { n     for (int j = (0), jed = (k - 2); j <= jed; j++) n       if (bl[g(X[i], j)] != bl[g(Y[i], j + 1)]) n         add(bl[g(X[i], j)], bl[g(Y[i], j + 1)]); n     if (bl[g(X[i], k - 1)] != bl[g(Y[i], 0)]) n       add(bl[g(X[i], k - 1)], bl[g(Y[i], 0)]); n   } n   memset(f, -0x3f3f3f3f, sizeof(f)), f[bl[g(1, 0)]] = 0; n   for (int i = (lb), ied = (1); i >= ied; i--) { n     f[i] += sum[i]; n     ans = Max(ans, f[i]); n     for (int j = hed[i]; j; j = nxt[j]) f[to[j]] = Max(f[to[j]], f[i]); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 100000 + 5, inf = 2005120699; n const long long INF = 9200512069999999999ll; n int reads() { n   int ans = 0; n   bool fu = 0; n   char ch = getchar(); n   while ((ch >  9  || ch <  0 ) && ch !=  - ) ch = getchar(); n   if (ch ==  - ) fu = 1, ch = getchar(); n   while (ch <=  9  && ch >=  0 ) n     ans = (ans << 3) + (ans << 1) + (ch ^ 48), ch = getchar(); n   if (fu) ans *= -1; n   return ans; n } n long long a[maxN]; n inline long long abss(long long x) { return x < 0 ? -x : x; } n int main() { n   int n, c; n   long long ans = INF, tem, o; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %lld , &a[i]); n   } n   sort(a + 1, a + 1 + n); n   c = (int)pow((((1ll << 62) - 1) << 1) + 1, 1.0 / n); n   for (long long i = 0ll; i <= c; ++i) { n     tem = 0; n     o = 1ll; n     for (int j = 1; j <= n; ++j) { n       tem += abss(a[j] - o); n       o *= i; n       if (o > (1ll << 50)) { n         tem = INF; n         break; n       } n     } n     ans = min(ans, tem); n   } n   printf( %lld , ans); n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n int n; n int a[105]; n int main() { n   ios::sync_with_stdio(false); n   cin >> n; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   int mx = 0; n   for (int i = 1; i <= n; i++) { n     int tmp = 0; n     for (int j = i; j <= n; j++) { n       tmp ^= a[j]; n       mx = max(mx, tmp); n     } n   } n   cout << mx << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct Node { n   Node *ch[26]; n   Node() { n     for (int i = 0; i < 26; i++) ch[i] = NULL; n   } n } * Root; n struct Trie { n   void init() { Root = new Node(); } n   void ins(Node *t, string s) { n     for (int i = 0; i < s.size(); i++) { n       int id = s[i] -  a ; n       if (t->ch[id] == NULL) { n         Node *p = new Node(); n         t->ch[id] = p; n       } n       t = t->ch[id]; n     } n   } n   void del(Node *t) { n     for (int i = 0; i < 26; i++) n       if (t->ch[i] != NULL) del(t->ch[i]); n     free(t); n   } n   bool win(Node *t) { n     for (int i = 0; i < 26; i++) n       if (t->ch[i] != NULL) n         if (!win(t->ch[i])) return true; n     return false; n   } n   bool lose(Node *t) { n     bool flag = 0; n     for (int i = 0; i < 26; i++) n       if (t->ch[i] != NULL) { n         flag = true; n         if (!lose(t->ch[i])) return true; n       } n     return false || !flag; n   } n }; n const int Maxn = 1e5 + 8; n char s[Maxn]; n int main() { n   Trie T; n   T.init(); n   int n, k; n   scanf( %d %d , &n, &k); n   for (int i = 0; i < n; i++) { n     scanf( %s , s); n     T.ins(Root, s); n   } n   bool win = T.win(Root), lose = T.lose(Root); n   if (win && lose) n     puts( First ); n   else { n     if (win) { n       if (k & 1) n         puts( First ); n       else n         puts( Second ); n     } else n       puts( Second ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int Maxn = 5555, Inf = 1e9; n int n, m; n vector<int> G[Maxn]; n vector<pair<int, int> > V1[Maxn], V2[Maxn]; n int d[Maxn][Maxn]; n void bfs(int idx) { n   for (int i = 1; i <= n; i++) d[idx][i] = i == idx ? 0 : Inf; n   queue<int> q; n   q.push(idx); n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = 0; i < G[u].size(); i++) { n       int v = G[u][i]; n       if (d[idx][v] == Inf) { n         d[idx][v] = d[idx][u] + 1; n         q.push(v); n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     if (i != idx && d[idx][i] != Inf) n       V1[idx].push_back(pair<int, int>(d[idx][i], i)), n           V2[i].push_back(pair<int, int>(d[idx][i], idx)); n   } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 0; i < m; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     G[u].push_back(v); n   } n   for (int i = 1; i <= n; i++) { n     bfs(i); n   } n   for (int i = 1; i <= n; i++) { n     sort(V1[i].begin(), V1[i].end(), greater<pair<int, int> >()); n     sort(V2[i].begin(), V2[i].end(), greater<pair<int, int> >()); n   } n   int ans = -1; n   vector<int> rep(4, 0); n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= n; j++) { n       if (i == j || d[i][j] == Inf) continue; n       for (int k1 = 0; k1 < 3 && k1 < V2[i].size(); k1++) { n         for (int k2 = 0; k2 < 3 && k2 < V1[j].size(); k2++) { n           if (V2[i][k1].second != j && V1[j][k2].second != i && n               V2[i][k1].second != V1[j][k2].second) { n             int tmp = V2[i][k1].first + V1[j][k2].first + d[i][j]; n             if (ans < tmp) { n               ans = tmp; n               rep[0] = V2[i][k1].second, rep[1] = i; n               rep[2] = j, rep[3] = V1[j][k2].second; n             } n           } n         } n       } n     } n   } n   for (int i = 0; i < 4; i++) printf( %d  , rep[i]); n } n


#include<bits/stdc++.h> n using namespace std; n using ll = long long; n using ld = long double; n using pint = pair<int, int>; n using pll = pair<ll, ll>; n  n int main(){ n     int t; cin >> t; n     while(t--){ n         int k, n, m; cin >> k >> n >> m; n         queue<int> a, b; n         int size = k; n         for(int i = 0; i < n; i++){ n             int x; cin >> x; n             a.push(x); n         } n         for(int j = 0; j < m; j++){ n             int x; cin >> x; n             b.push(x); n         } n         bool ok = true; n         vector<int> ans; n         while(!a.empty() && !b.empty()){ n             int op; n             if(a.front() < b.front()){ n                 op = a.front(); n                 a.pop(); n             } n             else{ n                 op = b.front(); n                 b.pop(); n             } n             if(op == 0)size++; n             else{ n                 if(op > size){ n                     ok = false; n                     break; n                 } n             } n             ans.push_back(op); n         } n         if(ok){ n             int op; n             while(!a.empty()){ n                 op = a.front(); n                 a.pop(); n                 if(op == 0)size++; n                 else{ n                     if(op > size){ n                         ok = false; n                         break; n                     } n                 } n                 ans.push_back(op); n             } n             while(!b.empty()){ n                 op = b.front(); n                 b.pop(); n                 if(op == 0)size++; n                 else{ n                     if(op > size){ n                         ok = false; n                         break; n                     } n                 } n                 ans.push_back(op); n             } n         } n         if(ok){ n             for(int i = 0; i < ans.size(); i++)cout << ans[i] <<    ; n             cout << endl; n         } n         else{ n             cout << -1 << endl; n         } n     } n }


#include <bits/stdc++.h> n using namespace std; n using namespace std::chrono; n template <typename A> n ostream &operator<<(ostream &cout, vector<A> const &v); n template <typename A, typename B> n ostream &operator<<(ostream &cout, pair<A, B> const &p) { n   return cout <<  (  << p.first <<  ,   << p.second <<  ) ; n } n template <typename A> n ostream &operator<<(ostream &cout, vector<A> const &v) { n   cout <<  [ ; n   for (int i = 0; i < v.size(); i++) { n     if (i) cout <<  ,  ; n     cout << v[i]; n   } n   return cout <<  ] ; n } n template <typename A, typename B> n istream &operator>>(istream &cin, pair<A, B> &p) { n   cin >> p.first; n   return cin >> p.second; n } n mt19937 rng(steady_clock::now().time_since_epoch().count()); n void usaco(string filename) { n   freopen((filename +  .in ).c_str(),  r , stdin); n   freopen((filename +  .out ).c_str(),  w , stdout); n } n const long double pi = 3.14159265358979323846; n long long n, m, k, q, l, r, x, y, z; n long long a[1000005]; n long long b[1000005]; n long long c[1000005]; n string second, t; n long long ans = 0; n long long solve2() { n   long long r = 0, best = 0; n   for (long long i = 0; i < (n - 1); ++i) { n     r += b[i]; n     best = max(best, r); n     if (r < 0) r = 0; n   } n   return best; n } n void solve(int tc) { n   cin >> n; n   for (int ele = 0; ele < n; ele++) cin >> a[ele]; n   ; n   for (long long i = 0; i < (n - 1); ++i) { n     b[i] = abs(a[i] - a[i + 1]); n     if (i % 2) b[i] = -b[i]; n   } n   ans = 0; n   ans = max(ans, solve2()); n   for (long long i = 0; i < (n - 1); ++i) b[i] = -b[i]; n   ans = max(ans, solve2()); n   cout << ans <<   n ; n } n int main() { n   { ios_base::sync_with_stdio(false); } n   { n     cin.tie(NULL); n     cout.tie(NULL); n   } n   int tc = 1; n   for (int t = 0; t < tc; t++) solve(t); n } n


#include <bits/stdc++.h> n long long s, x; n int sba[150], xba[150]; n long long bab[150]; n int main(int argc, char** argv) { n   int i, j; n   int num; n   int n, m; n   int flag; n   long long tmp; n   long long ans; n   bab[0] = 1; n   for (i = 1; i < 60; i++) bab[i] = bab[i - 1] * 2; n   while (~scanf( %I64d%I64d , &s, &x)) { n     if (s < x) { n       printf( 0 n ); n       continue; n     } n     memset(sba, 0, sizeof(sba)); n     memset(xba, 0, sizeof(xba)); n     i = 0; n     num = 0; n     tmp = x; n     while (tmp) { n       if (tmp & 1) { n         num++; n         xba[i] = 1; n       } n       tmp >>= 1; n       i++; n     } n     n = i; n     ans = 0; n     ans += bab[num]; n     tmp = s - x; n     if (tmp & 1) { n       printf( 0 n ); n       continue; n     } n     flag = 0; n     i = 0; n     while (tmp) { n       if (tmp & 1) { n         sba[i] = 1; n       } n       tmp >>= 1; n       i++; n     } n     m = i; n     for (i = 0; i < n; i++) { n       if (xba[i] == 1 && sba[i + 1] == 1) { n         flag = 1; n         break; n       } n     } n     if (flag) { n       printf( 0 n ); n       continue; n     } else { n       if ((s ^ 0) == x) ans -= 2; n       printf( %I64d n , ans); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n, i, mi, ma, x; n     cin >> n; n     int m[n + 1]; n     for (i = 1; i <= n; i++) { n       cin >> x; n       m[x] = i; n     } n     int a[n + 1]; n     a[1] = 1; n     a[n] = 1; n     mi = m[1]; n     ma = m[1]; n     for (i = 2; i < n; i++) { n       mi = min(mi, m[i]); n       ma = max(ma, m[i]); n       if ((ma - mi) == i - 1) { n         a[i] = 1; n       } else { n         a[i] = 0; n       } n     } n     for (i = 1; i <= n; i++) { n       cout << a[i]; n     } n     cout << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main(void) { n   int x; n   scanf( %d , &x); n   if (x == 1) n     puts( 1 ); n   else if (x == 3) n     puts( 5 ); n   else if (x <= 5) n     puts( 3 ); n   else if (x <= 13) n     puts( 5 ); n   else if (x <= 25) n     puts( 7 ); n   else if (x <= 41) n     puts( 9 ); n   else if (x <= 61) n     puts( 11 ); n   else if (x <= 85) n     puts( 13 ); n   else n     puts( 15 ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string str; n   unordered_map<string, char> index; n   int t; n   cin >> t; n   while (t--) { n     cin >> str; n     sort(str.begin(), str.end()); n     str = str.substr(0, unique(str.begin(), str.end()) - str.begin()); n     if (index.find(str) == index.end()) { n       index.insert(unordered_map<string, char>::value_type(str,  c )); n     } n   } n   cout << index.size() << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 10; n int h[20][20]; n long double E[20][20]; n bool vis[20][20]; n void init() { n   for (int i = 1; i <= 10; i++) { n     for (int j = 1; j <= 10; j++) { n       cin >> h[i][j]; n     } n   } n } n pair<int, int> go_next(int x, int y) { n   if (x % 2 == 1) { n     if (y > 1) n       return make_pair(x, y - 1); n     else n       return make_pair(x - 1, y); n   } else { n     if (y < 10) n       return make_pair(x, y + 1); n     else n       return make_pair(x - 1, y); n   } n } n long double dp(int x, int y) { n   if (vis[x][y]) return E[x][y]; n   double e = 0; n   int tx = x, ty = y; n   for (int i = 1; i <= 6; i++) { n     std::tie(tx, ty) = go_next(tx, ty); n     long double p = dp(tx, ty); n     if (h[tx][ty] > 0) { n       p = min(p, dp(tx - h[tx][ty], ty)); n     } n     e += (p + 1) / 6.0; n   } n   E[x][y] = e; n   vis[x][y] = true; n   return E[x][y]; n } n void work() { n   E[1][1] = 0.0; n   E[1][2] = 6.0; n   E[1][3] = 3.0 + 0.5 * E[1][2]; n   E[1][4] = 2.0 + E[1][2] / 3.0 + E[1][3] / 3.0; n   E[1][5] = 1.5 + 1.0 / 4.0 * (E[1][2] + E[1][3] + E[1][4]); n   E[1][6] = 1.2 + 1.0 / 5.0 * (E[1][2] + E[1][3] + E[1][4] + E[1][5]); n   memset(vis, false, sizeof(vis)); n   for (int i = 1; i <= 6; i++) vis[1][i] = true; n   dp(10, 1); n   cout << std::setprecision(8) << std::fixed << E[10][1] << endl; n } n int main() { n   init(); n   work(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, x = 1; n   cin >> n; n   string arr[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   for (int i = 0; i < n && x == 1; i++) { n     string s = arr[i]; n     for (int j = 0; j < 5; j++) { n       if (s[j] ==  O  && s[j + 1] ==  O ) { n         cout <<  YES  << endl; n         s[j] =  + ; n         s[j + 1] =  + ; n         arr[i] = s; n         x = 0; n         break; n       } n     } n   } n   if (x == 0) { n     for (int m = 0; m < n; m++) { n       cout << arr[m] << endl; n     } n   } else { n     cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:102400000,102400000 ) n using namespace std; n const int block_size = 360; n const int maxn = 100010; n const long long mod = 1e9 + 7; n const long double eps = 1e-9; n template <typename T> n int sign(const T& a) { n   if (a < 0) { n     return -1; n   } n   if (a > 0) { n     return 1; n   } n   return 0; n } n long long cross_product(const complex<long long>& from, complex<long long> a, n                         complex<long long> b) { n   a -= from; n   b -= from; n   return a.imag() * b.real() - a.real() - b.imag(); n } n string a =   , b =   ; n int main() { n   int n; n   scanf( %d , &n); n   for (long long i = 0; i < n; i++) { n     string c; n     cin >> c; n     if (c !=  0 ) { n       a += c; n     } n   } n   for (long long i = 0; i < n; i++) { n     string c; n     cin >> c; n     if (c !=  0 ) { n       b += c; n     } n   } n   a = a + a; n   if (strstr(a.c_str(), b.c_str())) { n     cout <<  YES ; n   } else { n     cout <<  NO ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int x = 0, f = 1; n   char ch = getchar(); n   while (ch <  0  || ch >  9 ) { n     if (ch ==  - ) f = -1; n     ch = getchar(); n   } n   while (ch >=  0  && ch <=  9 ) { n     x = x * 10 + ch -  0 ; n     ch = getchar(); n   } n   return x * f; n } n int n, cnt; n int ans[100005]; n vector<int> e[100005]; n map<int, int> mp[100005]; n map<vector<int>, int> H; n set<int> st; n int dp(int x, int fa) { n   if (mp[x][fa]) return mp[x][fa]; n   vector<int> cur; n   for (int i = 0; i < e[x].size(); i++) n     if (e[x][i] != fa) cur.push_back(dp(e[x][i], x)); n   sort(cur.begin(), cur.end()); n   if (!H[cur]) H[cur] = ++cnt; n   return mp[x][fa] = H[cur]; n } n int main() { n   n = read(); n   for (int i = 1; i < n; i++) { n     int u = read(), v = read(); n     e[u].push_back(v); n     e[v].push_back(u); n   } n   for (int i = 1; i <= n; i++) n     if (e[i].size() < 4) st.insert(dp(i, 0)); n   cout << st.size() << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct event { n   int x, size, val, ent; n   bool operator<(const event& rhs) const { n     if (x == rhs.x) return ent < rhs.ent; n     return x < rhs.x; n   } n }; n int main() { n   ios_base::sync_with_stdio(false), cin.tie(0); n   int n, x; n   cin >> n >> x; n   vector<event> v; n   for (int i = 0; i < n; i++) { n     int l, r, cost; n     cin >> l >> r >> cost; n     v.push_back({l, r - l + 1, cost, 0}); n     v.push_back({r, r - l + 1, cost, 1}); n   } n   sort(v.begin(), v.end()); n   int ans = 0x7fffffff; n   map<int, int> mp; n   for (const event& eve : v) { n     if (eve.ent == 0) { n       auto it = mp.find(x - eve.size); n       if (it == mp.end()) n         continue; n       else n         ans = min(ans, it->second + eve.val); n     } else { n       if (mp[eve.size] == 0) n         mp[eve.size] = eve.val; n       else n         mp[eve.size] = min(eve.val, mp[eve.size]); n     } n   } n   if (ans == 0x7fffffff) ans = -1; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007, con = 2500000; n long long n, k, a, ans; n int main() { n   iostream::sync_with_stdio(0); n   cin >> n; n   for (int i = 0; i < n; i++) { n     cin >> k; n     ans += abs(a - k); n     a = k; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e3 + 7; n int n; n int a[maxn], b[maxn]; n long long solve() { n   long long res = 0; n   for (int i = 0; i < int(n); i++) { n     long long sa = 0, sb = 0; n     for (int j = i; j < int(n); j++) { n       sa |= a[j]; n       sb |= b[j]; n       res = max(res, sa + sb); n     } n   } n   return res; n } n int main() { n   scanf( %d , &n); n   for (int i = 0; i < int(n); i++) scanf( %d , &a[i]); n   for (int i = 0; i < int(n); i++) scanf( %d , &b[i]); n   printf( %lld n , solve()); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n #define ll long long n #define ull unsigned long long n #define uint unsigned int n #define loop(n) for(int i=1;i<=n;i++) n  n  n int main() n { n  ios_base::sync_with_stdio(false); n  cout.tie(NULL); n  cin.tie(NULL); n  n  uint t; n  cin>>t; n  while(t--) n  { n   int n,temp,tem; n   ll min=INT_MAX; n   cin>>n; n   map<int,int> a; n   vector<int> s; n   set<int> st; n   loop(n) n   { n    cin>>temp; n    tem=a[temp]; n    a[temp]=tem+1; n   } n   for(auto i:a) n   { n    //cout<<i.second<<   ; n    s.push_back(i.second); n    st.insert(i.second); n   } n   for(auto mid:st)   n   { n    ll sum=0; n    for(auto i:s) n     if(i<mid) n      sum+=i; n     else n      sum+=i-mid; n    if(min>sum) n     min=sum; n   } n   cout<<min<<endl; n  } n  return 0; n }


#include <bits/stdc++.h> n using namespace std; n int n; n char s[2001][2002]; n int wu[2][2001]; n int ans; n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); n   for (int i = 1; i <= n; i++) { n     int rxor = 0, lxor = 0, d; n     for (int j = n; j > i; j--) { n       d = wu[0][j] ^ (s[i][j] -  0 ) ^ rxor; n       if (d) ans++, rxor ^= 1, wu[0][j] ^= 1; n       d = wu[1][j] ^ (s[j][i] -  0 ) ^ lxor; n       if (d) ans++, lxor ^= 1, wu[1][j] ^= 1; n     } n     if ((s[i][i] -  0 ) ^ rxor ^ lxor ^ wu[0][i] ^ wu[1][i]) ans++; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string s, s5; n   cin >> s; n   int c = 0; n   string s3 =   , s4 =   ; n   s3 += s[0]; n   s3 += s[1]; n   s4 += s[3]; n   s4 += s[4]; n   int a = atoi(s3.c_str()); n   int b = atoi(s4.c_str()); n   while (1) { n     if (b == 59) { n       if (a == 23) n         a = 0, s3[0] =  0 , s3[1] =  0 ; n       else { n         a++; n         stringstream ss; n         ss << a; n         s5 = ss.str(); n         if (a < 10) { n           s3 =  0 ; n           s3 += s5; n         } else n           s3 = s5; n       } n       b = 0; n       s4[0] =  0 , s4[1] =  0 ; n     } else { n       b++; n       stringstream ss; n       ss << b; n       s5 = ss.str(); n       if (b < 10) { n         s4 =  0 ; n         s4 += s5; n       } else n         s4 = s5; n     } n     c++; n     if (s3[0] == s4[1] && s3[1] == s4[0] && c > 0) break; n   } n   cout << s3 <<  :  << s4 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct team { n   int prob; n   int time; n }; n bool comp(team a, team b) { n   return (a.prob > b.prob) || ((a.prob == b.prob) && (a.time < b.time)); n } n int main(int argc, char const *argv[]) { n   int n, k; n   cin >> n >> k; n   team t[n]; n   for (int i = 0; i < n; ++i) { n     cin >> t[i].prob >> t[i].time; n   } n   sort(t, t + n, comp); n   team tk; n   tk = t[k - 1]; n   int count = 1; n   int i = k - 2; n   int j = k; n   while ((tk.time == t[i].time) && (tk.prob == t[i].prob) && (i >= 0)) { n     count++; n     i--; n   } n   while ((tk.time == t[j].time) && (tk.prob == t[j].prob) && (j < n)) { n     count++; n     j++; n   } n   cout << count << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize  trapv  n long long int power(long long int x, long long int b) { n   long long int p = 1; n   while (b > 0) { n     if (b & 1) { n       p = p * x; n       p %= 1000000007; n     } n     b >>= 1; n     x *= x; n     x %= 1000000007; n   } n   return p % 1000000007; n } n using namespace std; n struct ppp { n   char a; n   long long int b, c, d; n }; n long long int N; n struct lex_compare { n   bool operator()(ppp p1, ppp p2) { n     if (p1.a == p2.a) { n       if (p1.b == p2.b) n         return p1.c < p2.c; n       else n         return p1.b < p2.b; n     } else { n       return p1.a < p2.a; n     } n   } n }; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long int n; n   cin >> n; n   long long int arr[n]; n   for (long long int i = 0; i < n; i++) cin >> arr[i]; n   long long int maxt = n; n   vector<vector<long long int> > ans; n   while (maxt--) { n     vector<vector<long long int> > blocks; n     int i = 0; n     while (i < n) { n       long long int s = arr[i]; n       vector<long long int> temp; n       temp.push_back(s); n       i++; n       while (i < n && arr[i] == s + 1) { n         s = arr[i]; n         i++; n         temp.push_back(s); n       } n       blocks.push_back(temp); n     } n     if (blocks.size() == 1) break; n     long long int minele = blocks[blocks.size() - 1].back(); n     long long int next = minele + 1; n     if (next == n + 1) { n       vector<long long int> tempans; n       long long int ind = 0; n       for (int j = blocks.size() - 1; j >= 0; j--) { n         tempans.push_back(blocks[j].size()); n         for (auto k : blocks[j]) { n           arr[ind++] = k; n         } n       } n       reverse(tempans.begin(), tempans.end()); n       ans.push_back(tempans); n       continue; n     } n     long long int ind2nd = 0; n     while (ind2nd < blocks.size() && blocks[ind2nd][0] != next) { n       ind2nd++; n     } n     vector<long long int> tempans; n     long long int size1 = 0; n     i = 0; n     while (i < ind2nd) { n       size1 += blocks[i].size(); n       i++; n     } n     if (size1 != 0) tempans.push_back(size1); n     size1 = 0; n     i = ind2nd; n     while (i < blocks.size() - 1) { n       size1 += blocks[i].size(); n       i++; n     } n     if (size1 != 0) tempans.push_back(size1); n     tempans.push_back(blocks.back().size()); n     ans.push_back(tempans); n     int ind = 0; n     for (auto j : blocks.back()) { n       arr[ind++] = j; n     } n     for (int j = ind2nd; j < blocks.size() - 1; j++) { n       for (auto k : blocks[j]) { n         arr[ind++] = k; n       } n     } n     for (int j = 0; j < ind2nd; j++) { n       for (auto k : blocks[j]) { n         arr[ind++] = k; n       } n     } n   } n   cout << ans.size() <<   n ; n   for (auto i : ans) { n     cout << i.size() <<    ; n     for (auto j : i) cout << j <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int a[200]; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int m, n; n     cin >> m >> n; n     for (int i = 1; i <= m; i++) { n       cin >> a[i]; n     } n     map<int, int> mp; n     for (int i = 1; i <= n; i++) { n       int x; n       cin >> x; n       mp[x] = 1; n     } n     for (int i = 1; i < m; i++) { n       for (int j = m - i; j <= m; j++) { n         if (j + 1 <= m && a[j] > a[j + 1]) { n           if (mp[j]) { n             swap(a[j], a[j + 1]); n           } n         } n       } n     } n     int flag = 1; n     for (int i = 1; i < m; i++) { n       if (a[i] > a[i + 1]) { n         flag = 0; n       } n     } n     if (flag) n       cout <<  YES  << endl; n     else n       cout <<  NO  << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n long long int n, a, b; n long long int S(long long int x) { n   int ret = 0; n   long long int y = n - x; n   while (x > 0) { n     ret += x % 10; n     x /= 10; n   } n   while (y > 0) { n     ret += y % 10; n     y /= 10; n   } n   return ret; n } n long long int solve(long long int x) { n   long long int b = 1, ans = x; n   while (x) { n     long long int cur = (x - 1) * b + (b - 1); n     if (S(cur) > S(ans)) { n       ans = cur; n     } n     x /= 10; n     b *= 10; n   } n   return ans; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n; n   cout << S(solve(n)); n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize(3) n #pragma GCC optimize(2) n using namespace std; n int n, r; n bool dp[666][666]; n int fang[666][666]; n pair<int, int> ku[666]; n void dfs(int x, int y) { n   if (x == y) n     cout <<  () ; n   else if (fang[x][y] == -1) { n     cout <<  ( ; n     dfs(x + 1, y); n     cout <<  ) ; n   } else { n     dfs(x, fang[x][y]); n     dfs(fang[x][y] + 1, y); n   } n } n signed main() { n   ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); n   memset(dp, 0, sizeof dp); n   cin >> n; n   for (register int i = 1; (1 < n) ? (i <= n) : (i >= n); n        i += (1 < n) ? 1 : (-1)) n     cin >> ku[i].first >> ku[i].second; n   for (register int i = 1; (1 < n) ? (i <= n) : (i >= n); n        i += (1 < n) ? 1 : (-1)) n     dp[i][i] = (bool)(ku[i].first == 1); n   for (register int i = 2; (2 < n) ? (i <= n) : (i >= n); n        i += (2 < n) ? 1 : (-1)) { n     for (register int l = 1; n          (1 < n - i + 1) ? (l <= n - i + 1) : (l >= n - i + 1); n          l += (1 < n - i + 1) ? 1 : (-1)) { n       r = l + i - 1; n       if (dp[l + 1][r] && ku[l].first - 1 <= (r - l) * 2 && n           (r - l) * 2 <= ku[l].second - 1) { n         dp[l][r] = 1; n         fang[l][r] = -1; n       } else { n         for (register int k = l; (l < r - 1) ? (k <= r - 1) : (k >= r - 1); n              k += (l < r - 1) ? 1 : (-1)) { n           if (dp[l][k] && dp[k + 1][r]) { n             dp[l][r] = 1; n             fang[l][r] = k; n             break; n           } n         } n       } n     } n   } n   if (dp[1][n]) { n     dfs(1, n); n     cout << endl; n   } else n     cout <<  IMPOSSIBLE  << endl; n   return 0; n } n


#include <bits/stdc++.h> n const int N = 5000 + 10; n struct node { n   int l, r, id; n }; n node a[N]; n bool pre[N], suf[N]; n std::vector<node> vector; n bool cmp(const node& i, const node& j) { n   if (i.r ^ j.r) n     return i.r < j.r; n   else n     return i.l < j.l; n } n bool cc(const node& i, const node& j) { return i.id < j.id; } n int main() { n   int n; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     a[i].id = i; n     scanf( %d%d , &a[i].l, &a[i].r); n   } n   std::sort(a + 1, a + 1 + n, cmp); n   pre[0] = pre[1] = true; n   for (int i = 2; i <= n; ++i) n     if (pre[i - 1] && a[i].l >= a[i - 1].r) n       pre[i] = true; n     else n       break; n   suf[n + 1] = suf[n] = true; n   for (int i = n - 1; i >= 1; --i) n     if (suf[i + 1] && a[i].r <= a[i + 1].l) n       suf[i] = true; n     else n       break; n   for (int i = 1; i <= n; ++i) n     if (pre[i - 1] && suf[i + 1]) { n       if (i == 1 || i == n || a[i - 1].r <= a[i + 1].l) vector.push_back(a[i]); n     } n   std::sort(vector.begin(), vector.end(), cc); n   printf( %d n , vector.size()); n   for (int i = 0; i < vector.size(); ++i) { n     if (i) putchar(   ); n     printf( %d , vector[i].id); n   } n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int n; n   double f, s, v, l, omega, r; n   const double tol = 1.0e-7; n   scanf( %d %lf %lf , &n, &r, &v); n   omega = v / r; n   double xt, xb, a, b; n   for (int i = 0; i < n; i++) { n     scanf( %lf %lf , &s, &f); n     l = (f - s) / 2.; n     a = (l + r + 0.1) / v; n     b = -0.1; n     xt = (a + b) / 2.; n     while (abs((b - a) / xt) > tol) { n       if ((v * xt + r * sin(xt * omega) - l) * n               (v * a + r * sin(a * omega) - l) < n           0.) n         b = xt; n       else n         a = xt; n       xt = (a + b) / 2.; n     } n     a = (l + r + 0.1) / v; n     b = -0.1; n     do { n       xb = (a + b) / 2.; n       if ((v * xb - r * sin(xb * omega) - l) * n               (v * a - r * sin(a * omega) - l) < n           0.) n         b = xb; n       else n         a = xb; n     } while (abs((b - a) / xb) > tol); n     printf( %.10lf n , 2. * min(xb, xt)); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k; n map<string, long long> mm; n string a[100005]; n long long c[100005]; n long long min(long long a, long long b) { return a < b ? a : b; } n int main() { n   cin >> n >> k >> m; n   for (int i = 1; i <= n; i++) { n     cin >> a[i]; n   } n   for (int i = 1; i <= n; i++) { n     long long x; n     cin >> x; n     mm[a[i]] = x; n   } n   for (int i = 0; i < k; i++) { n     long long x; n     cin >> x; n     long long minn = 0x3f3f3f3f; n     for (int j = 0; j < x; j++) { n       cin >> c[j]; n       minn = min(minn, mm[a[c[j]]]); n     } n     for (int j = 0; j < x; j++) { n       mm[a[c[j]]] = minn; n     } n   } n   string b; n   long long s = 0; n   for (int i = 0; i < m; i++) { n     cin >> b; n     s += mm[b]; n   } n   cout << s << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int M = 1e6 + 10; n struct node { n   int l, r, sum, mus; n } tr[M]; n int n, a[M], now[M], sz[M], place[M], q, p; n vector<int> g[M]; n void maket(int l, int r, int t) { n   tr[t].l = l; n   tr[t].r = r; n   if (l == r) { n     tr[t].sum += now[a[l]]; n     return; n   } n   maket(l, (l + r) / 2, 2 * t); n   maket((l + r) / 2 + 1, r, 2 * t + 1); n   tr[t].sum = tr[2 * t].sum + tr[2 * t + 1].sum; n } n void change(int l, int r, int t) { n   if (tr[t].l == l && tr[t].r == r) { n     tr[t].sum = r - l + 1 - tr[t].sum; n     tr[t].mus ^= 1; n     return; n   } n   if (tr[t].mus) { n     change(tr[2 * t].l, tr[2 * t].r, 2 * t); n     change(tr[2 * t + 1].l, tr[2 * t + 1].r, 2 * t + 1); n     tr[t].mus = 0; n   } n   if (r <= tr[2 * t].r) n     change(l, r, 2 * t); n   else if (l >= tr[2 * t + 1].l) n     change(l, r, 2 * t + 1); n   else { n     change(l, tr[2 * t].r, 2 * t); n     change(tr[2 * t + 1].l, r, 2 * t + 1); n   } n   tr[t].sum = tr[2 * t].sum + tr[2 * t + 1].sum; n } n int query(int l, int r, int t) { n   if (tr[t].l == l && tr[t].r == r) return tr[t].sum; n   if (tr[t].mus) { n     change(tr[2 * t].l, tr[2 * t].r, 2 * t); n     change(tr[2 * t + 1].l, tr[2 * t + 1].r, 2 * t + 1); n     tr[t].mus = 0; n   } n   if (r <= tr[2 * t].r) n     return query(l, r, 2 * t); n   else if (l >= tr[2 * t + 1].l) n     return query(l, r, 2 * t + 1); n   else n     return query(l, tr[2 * t].r, 2 * t) + query(tr[2 * t + 1].l, r, 2 * t + 1); n   tr[t].sum = tr[2 * t].sum + tr[2 * t + 1].sum; n } n void dfs(int v) { n   a[++p] = v; n   place[v] = p; n   sz[v] = 1; n   for (int i = 0; i < g[v].size(); i++) { n     dfs(g[v][i]); n     sz[v] += sz[g[v][i]]; n   } n } n int main() { n   cin >> n; n   for (int i = 2; i <= n; i++) { n     int x; n     scanf( %d , &x); n     g[x].push_back(i); n   } n   for (int i = 1; i <= n; i++) scanf( %d , now + i); n   dfs(1); n   maket(1, n, 1); n   cin >> q; n   while (q--) { n     string s; n     cin >> s; n     int v; n     scanf( %d , &v); n     if (s ==  get ) n       printf( %d n , query(place[v], place[v] + sz[v] - 1, 1)); n     else n       change(place[v], place[v] + sz[v] - 1, 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   string s; n   cin >> s; n   string ans =  0 ; n   int curr = 0; n   for (int i = 1; i < n; i++) { n     if (s[i] ==  ( ) curr = curr ^ 1; n     ans += (char)(curr +  0 ); n     if (s[i] ==  ) ) curr = curr ^ 1; n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n; n long long c[200005]; n int b[200005]; n int a[100005]; n void up(int n, int k) { n   if (!n) return; n   while (n <= 200000) { n     b[n]++; n     c[n] += k; n     k++; n     n <<= 1; n   } n } n void solve(int n) { n   int cur = 0; n   long long num = n * 2; n   up(num, 1); n   num = n; n   cur = 0; n   while (num) { n     if (num & 1) { n       up(num / 2 * 2, cur + 2); n     } n     b[num]++; n     c[num] += cur; n     num >>= 1; n     cur++; n   } n } n int main() { n   scanf( %d , &n); n   memset(c, 0, sizeof(c)); n   for (int i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     solve(a[i]); n   } n   long long res = 1000000000; n   for (int i = 0; i <= 200000; i++) { n     if (b[i] == n) { n       res = min(res, c[i]); n     } n   } n   printf( %lld n , res); n } n


#include <bits/stdc++.h> n using namespace std; n const long long N = 1e6 + 10, mod = 1e9 + 7; n long long n, x; n long long f[N]; n void solve() { n   scanf( %lld , &n); n   long long s1 = 0, s2 = 0, ans = 0; n   for (long long i = 1; i <= n; i++) { n     scanf( %lld , &x); n     if (x == 1) n       s1++; n     else n       s2++; n   } n   f[0] = 1, f[1] = 1; n   for (long long i = 2; i <= s1; i++) n     f[i] = (f[i - 1] + (i - 1) * f[i - 2]) % mod; n   ans = f[s1]; n   for (long long i = s1 + 1; i <= n; i++) ans = ans * i % mod; n   printf( %lld , ans); n } n signed main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2000 + 10; n int n, tmp, ans, p[N], q[N], pos[N]; n vector<pair<int, int> > vec; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   int i, j; n   cin >> n; n   for (i = 1; i <= n; ++i) cin >> p[i]; n   for (i = 1; i <= n; ++i) cin >> q[i]; n   for (i = 1; i <= n; ++i) pos[p[i]] = i; n   for (i = 1; i <= n; ++i) p[pos[q[i]]] = i; n   for (i = 1; i <= n; ++i) pos[p[i]] = i; n   for (i = n; i >= 1; --i) { n     while (pos[i] != i) { n       for (j = pos[i] + 1; j <= i; ++j) { n         if (p[j] <= pos[i]) { n           tmp = pos[i]; n           ans += j - pos[i]; n           vec.push_back({pos[i], j}); n           swap(pos[i], pos[p[j]]); n           swap(p[tmp], p[j]); n           break; n         } n       } n     } n   } n   cout << ans <<   n ; n   cout << vec.size() <<   n ; n   for (auto x : vec) cout << x.first <<     << x.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, k; n const int N = 1e7 + 100; n bool a[N]; n int main() { n   cin >> n >> k; n   vector<long long> v(n); n   for (int i = 0; i < n; i++) { n     cin >> v[i]; n     a[v[i]] = 1; n   } n   int q; n   cin >> q; n   for (int test = 0; test < q; test++) { n     long long w; n     int ans = 1000; n     cin >> w; n     for (int t = 1; t <= k; t++) { n       for (int i = 0; i < n; i++) { n         long long rem = w - t * v[i]; n         if (rem < 0) continue; n         if (rem == 0) ans = min(ans, t); n         for (int e = 1; t + e <= k; e++) { n           if (rem % e != 0) continue; n           long long x = rem / e; n           if (x >= N) continue; n           if (a[x]) ans = min(e + t, ans); n         } n       } n     } n     if (ans != 1000) n       cout << ans; n     else n       cout << -1; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n int main(void) { n   int n; n   int i, i2; n   int a[100000] = {0}; n   int flag = 0; n   int max = -1000001; n   int min = 1000001; n   int sum = 0; n   scanf( %d , &n); n   for (i = 0; i < n; i++) { n     scanf( %d , &a[i]); n     if (a[i] > 0) { n       if (a[i] < min && a[i] % 2 != 0) { n         min = a[i]; n       } n       sum += a[i]; n     } else { n       if (a[i] > max && a[i] % 2 != 0) { n         max = a[i]; n       } n     } n   } n   if (min == 1000001) { n     printf( %d , sum + max); n     return 0; n   } n   if (sum % 2 == 0) { n     if (min > (-max)) { n       printf( %d , sum + max); n     } else { n       printf( %d , sum - min); n     } n   } else { n     printf( %d , sum); 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   int n, k; n   cin >> n >> k; n   int sum = 0; n   sum += 2 * n / k; n   if (2 * n % k != 0) sum++; n   sum += 5 * n / k; n   if (5 * n % k != 0) sum++; n   sum += 8 * n / k; n   if (8 * n % k != 0) sum++; n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mo = 1000000007; n const int inv2 = (mo + 1) / 2; n const int inv4 = (mo + 1) / 4; n const int N = 500005; n int dep[N], fa[N], deg[N]; n int sz[N], p[N], cnt[N], n, m, nd; n vector<int> e[N], vec[N]; n int power(int x, int y) { n   int s = 1; n   for (; y; y /= 2, x = 1ll * x * x % mo) n     if (y & 1) s = 1ll * s * x % mo; n   return s; n } n void dfs(int x) { n   dep[x] = dep[fa[x]] + 1; n   for (auto i : e[x]) n     if (i != fa[x]) n       if (!dep[i]) { n         fa[i] = x; n         dfs(i); n       } else if (dep[x] > dep[i]) { n         ++nd; n         for (int y = x;; y = fa[y]) { n           vec[nd].push_back(y); n           if (y == i) break; n         } n         sz[nd] = vec[nd].size(); n         p[nd] = power(inv2, sz[nd]); n       } n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = (int)(1); i <= (int)(m); i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     e[x].push_back(y); n     e[y].push_back(x); n   } n   for (int i = (int)(1); i <= (int)(n); i++) deg[i] = e[i].size(); n   dfs(1); n   int E1 = 0, E2 = 0, E3 = 0; n   E1 = 1ll * n * inv2 % mo; n   E2 = 1ll * m * inv4 % mo; n   for (int i = (int)(1); i <= (int)(nd); i++) E3 = (E3 + p[i]) % mo; n   int E = (E1 + mo - E2 + 1ll * E3) % mo; n   int E11 = 0, E12 = 0, E13 = 0, E22 = 0, E23 = 0, E33 = 0; n   E11 = (1ll * n * (n - 1) % mo * inv4 % mo + E1) % mo; n   for (int i = (int)(1); i <= (int)(n); i++) n     E12 = (E12 + 1ll * (m - deg[i]) * inv4 % mo * inv2 + 1ll * deg[i] * inv4) % n           mo; n   for (int i = (int)(1); i <= (int)(nd); i++) n     E13 = n         (E13 + 1ll * p[i] * (1ll * inv2 * (n - sz[i]) % mo + sz[i]) % mo) % mo; n   E22 = (1ll * m * (m - 1) % mo * inv4 % mo * inv4 % mo + E2) % mo; n   for (int i = (int)(1); i <= (int)(n); i++) n     E22 = (E22 + 1ll * deg[i] * (deg[i] - 1) % mo * inv4 % mo * inv4) % mo; n   for (int i = (int)(1); i <= (int)(nd); i++) n     E23 = (E23 + 1ll * p[i] * (1ll * inv4 * (m - sz[i]) % mo + sz[i])) % mo; n   for (int i = (int)(1); i <= (int)(nd); i++) n     for (auto j : vec[i]) n       E23 = (E23 + 1ll * p[i] * inv4 % mo * (deg[j] - 2) % mo) % mo; n   for (int i = (int)(1); i <= (int)(nd); i++) n     E33 = (E33 + 1ll * p[i] * (E3 + mo - p[i] + 1)) % mo; n   for (int i = (int)(1); i <= (int)(nd); i++) n     for (auto j : vec[i]) cnt[j] = (cnt[j] + p[i]) % mo; n   for (int i = (int)(1); i <= (int)(nd); i++) n     for (auto j : vec[i]) E33 = (E33 + 1ll * p[i] * (cnt[j] + mo - p[i])) % mo; n   int EF = n       (1ll * E11 + E22 + E33 + 2 * E13 + 4ll * mo - 2 * E12 - 2 * E23) % mo; n   printf( %lld n , (EF + mo - 1ll * E * E % mo) % mo); n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 100100; n const int maxM = maxN << 1; n const int maxB = 17; n int n, Q, Log[maxN]; n int edgecnt = -1, Head[maxN], Next[maxM], V[maxM], W[maxM], Dst[maxN]; n class Tree { n  public: n   int root, Dep[maxN], Fa[maxN], Dst[maxN], Mx[maxN], ST[maxB][maxN], n       Mxd[maxB][maxN], dfncnt, dfn[maxN], lst[maxN]; n   int lcnt, Mark[maxN]; n   pair<int, int> Leaf[maxN]; n   void dfs_init(int u, int fa) { n     Dep[u] = 0; n     ST[0][u] = fa; n     for (int i = Head[u]; i != -1; i = Next[i]) n       if (V[i] != fa) { n         dfs_init(V[i], u); n         if (Dep[V[i]] + W[i] > Dep[u]) Mx[u] = V[i], Dep[u] = Dep[V[i]] + W[i]; n       } n     return; n   } n   void dfs_push(int u, int fa, int w) { n     Dep[u] = Dep[fa] + 1; n     dfn[u] = ++dfncnt; n     Mxd[0][dfncnt] = Dst[u]; n     if (Mx[u] == 0) n       Leaf[++lcnt] = make_pair(w, u); n     else { n       Fa[Mx[u]] = u; n       for (int i = Head[u]; i != -1; i = Next[i]) n         if (V[i] != fa) n           Dst[V[i]] = Dst[u] + W[i], n           dfs_push(V[i], u, (V[i] == Mx[u]) ? w + W[i] : W[i]); n     } n     lst[u] = dfncnt; n     return; n   } n   void Init(int r) { n     root = r; n     dfs_init(root, 0); n     Dep[root] = 1; n     Dst[root] = 0; n     dfs_push(root, 0, 0); n     for (int i = 1; i < maxB; i++) n       for (int j = 1; j <= n; j++) ST[i][j] = ST[i - 1][ST[i - 1][j]]; n     sort(&Leaf[1], &Leaf[lcnt + 1]); n     reverse(&Leaf[1], &Leaf[lcnt + 1]); n     for (int i = 1; i <= lcnt; i++) Leaf[i].first += Leaf[i - 1].first; n     for (int i = 1; i <= lcnt; i++) { n       int now = Leaf[i].second; n       while (now) Mark[now] = i, now = Fa[now]; n     } n     for (int i = 1; i < maxB; i++) n       for (int j = 1; j + (1 << (i)) - 1 <= n; j++) n         Mxd[i][j] = max(Mxd[i - 1][j], Mxd[i - 1][j + (1 << (i - 1))]); n     return; n   } n   int LCA(int u, int v) { n     if (Dep[u] < Dep[v]) swap(u, v); n     for (int i = maxB - 1; i >= 0; i--) n       if (ST[i][u] && Dep[ST[i][u]] >= Dep[v]) u = ST[i][u]; n     if (u == v) return u; n     for (int i = maxB - 1; i >= 0; i--) n       if (ST[i][u] && ST[i][v] && ST[i][u] != ST[i][v]) n         u = ST[i][u], v = ST[i][v]; n     return ST[0][u]; n   } n   int GET(int u, int cnt) { n     cnt = min(cnt * 2 - 1, lcnt); n     if (Mark[u] <= cnt) return Leaf[cnt].first; n     int ret = Leaf[cnt - 1].first; n     int now = u; n     for (int i = maxB - 1; i >= 0; i--) n       if (ST[i][now] && Mark[ST[i][now]] > cnt) now = ST[i][now]; n     now = ST[0][now]; n     int l = dfn[u], r = lst[u], lg = Log[r - l + 1]; n     int mxd = max(Mxd[lg][l], Mxd[lg][r - (1 << (lg)) + 1]); n     ret = ret + mxd - Dst[now]; n     return max(ret, Leaf[cnt].first + mxd - Dst[Leaf[Mark[now]].second]); n   } n }; n Tree T1, T2; n void Add_Edge(int u, int v, int w); n void Bfs(int S); n int main() { n   for (int i = 2; i < maxN; i++) Log[i] = Log[i >> 1] + 1; n   scanf( %d%d , &n, &Q); n   memset(Head, -1, sizeof(Head)); n   for (int i = 1; i < n; i++) { n     int u, v, w; n     scanf( %d%d%d , &u, &v, &w); n     Add_Edge(u, v, w); n     Add_Edge(v, u, w); n   } n   int S, T; n   Bfs(1); n   S = 1; n   for (int i = 1; i <= n; i++) n     if (Dst[S] < Dst[i]) S = i; n   Bfs(S); n   T = 1; n   for (int i = 1; i <= n; i++) n     if (Dst[T] < Dst[i]) T = i; n   T1.Init(S); n   T2.Init(T); n   int lstans = 0; n   while (Q--) { n     int x, y; n     scanf( %d%d , &x, &y); n     x = (x + lstans - 1) % n + 1; n     y = (y + lstans - 1) % n + 1; n     printf( %d n , lstans = max(T1.GET(x, y), T2.GET(x, y))); n   } n   return 0; n } n void Add_Edge(int u, int v, int w) { n   Next[++edgecnt] = Head[u]; n   Head[u] = edgecnt; n   V[edgecnt] = v; n   W[edgecnt] = w; n   return; n } n void Bfs(int S) { n   static int vis[maxN], Qu[maxN]; n   memset(vis, 0, sizeof(vis)); n   int l = 1, r = 1; n   Qu[1] = S; n   Dst[S] = 0; n   vis[S] = 1; n   while (l <= r) n     for (int u = Qu[l++], i = Head[u]; i != -1; i = Next[i]) n       if (vis[V[i]] == 0) Dst[Qu[++r] = V[i]] = Dst[u] + W[i], vis[V[i]] = 1; n   return; n } n


#include <bits/stdc++.h> n using namespace std; n long long N; n pair<long long, long long> arr[6005]; n long long cnt[20][20]; n bool good(int x1, int y1, int x2, int y2, int x3, int y3) {} n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   cin >> N; n   for (int i = 1; i <= N; i++) { n     cin >> arr[i].first >> arr[i].second; n     cnt[arr[i].first % 4][arr[i].second % 4]++; n   } n   vector<pair<long long, long long>> vec; n   for (int i = 0; i < 16; i += 2) { n     for (int j = 0; j < 16; j += 2) { n       vec.emplace_back(i, j); n     } n   } n   long long ans = N * (N - 1) * (N - 2) / 6; n   ans -= cnt[0][0] * cnt[0][2] * cnt[2][0]; n   ans -= cnt[0][0] * cnt[0][2] * cnt[2][2]; n   ans -= cnt[0][0] * cnt[2][0] * cnt[2][2]; n   ans -= cnt[0][2] * cnt[2][0] * cnt[2][2]; n   cout << ans <<   n ; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void maxtt(T& t1, T t2) { n   t1 = max(t1, t2); n } n template <typename T> n void mintt(T& t1, T t2) { n   t1 = min(t1, t2); n } n bool debug = 0; n int n, m, k; n string direc =  URDL ; n const long long MOD2 = (long long)1000000007 * (long long)1000000007; n long long ln, lk, lm; n void etp(bool f = 0) { n   puts(f ?  YES  :  NO ); n   exit(0); n } n void addmod(int& x, int y, int mod = 1000000007) { n   assert(y >= 0); n   x += y; n   if (x >= mod) x -= mod; n   assert(x >= 0 && x < mod); n } n void et(int x = -1) { n   printf( %d n , x); n   exit(0); n } n long long fastPow(long long x, long long y, int mod = 1000000007) { n   long long ans = 1; n   while (y > 0) { n     if (y & 1) ans = (x * ans) % mod; n     x = x * x % mod; n     y >>= 1; n   } n   return ans; n } n long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } n int a[500135], cnt[500135], M, ps[500135], pre[500135]; n long long ans; n pair<int, int> b1, b2; n void upt(pair<int, int> z) { n   if (z > b1) { n     b2 = b1; n     b1 = z; n   } else n     maxtt(b2, z); n } n void uptAns(long long x, long long y) { n   if (x >= 2) maxtt(ans, x * y); n } n void fmain(int tid) { n   scanf( %d , &n); n   for (int(i) = 1; (i) <= (int)(n); (i)++) { n     scanf( %d , a + i); n     cnt[a[i]]++; n     maxtt(M, a[i]); n   } n   for (int(i) = 1; (i) <= (int)(M); (i)++) { n     ps[i] = ps[i - 1] + cnt[i]; n     pre[i] = pre[i - 1]; n     if (cnt[i]) pre[i] = i; n   } n   for (int y = 2; y <= M; y++) { n     long long cntY = 0; n     for (int i = y; i <= M; i += y) { n       cntY += (long long)(ps[min(i + y - 1, M)] - ps[i - 1]) * (i / y); n     } n     b1 = {-1, -1}; n     b2 = {-1, -1}; n     for (int k = M / y, R = M + 1; k >= 0; R = k * y, k--) { n       int ky = k * y, ss = (b1.first >= 0) + (b2.first >= 0); n       if (pre[R - 1] >= ky) { n         pair<int, int> key = {pre[R - 1] % y, pre[R - 1]}; n         upt(key); n         if (cnt[pre[R - 1]] > 1) n           upt(key); n         else { n           int tmp = pre[pre[R - 1] - 1]; n           if (tmp >= ky) upt({tmp % y, tmp}); n         } n       } n       if (b1.first >= 0) { n         uptAns(min((long long)(b1.first + ky) / 2, cntY - k), y); n       } n       if (b2.second >= 0) { n         uptAns(min((long long)(b2.first + ky), cntY - k - k), y); n         if ((b1.second < R) + ss >= 2) { n           uptAns(min((long long)(b1.first + ky), cntY - k - k - 1), y); n         } n       } n     } n   } n   printf( %lld n , ans); n } n int main() { n   int t = 1; n   for (int(i) = 1; (i) <= (int)(t); (i)++) { n     fmain(i); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int read() { n   int X = 0, w = 1; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) w = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) X = X * 10 + c -  0 , c = getchar(); n   return X * w; n } n const int N = 100000 + 10; n const long long inf = 0x3f3f3f3f3f3f3f3f; n int n, a[N], b[N]; n vector<int> E[N]; n int rt[N], tot = 0; n struct line { n   int k; n   long long b; n } s[N]; n struct node { n   int ls, rs, id; n } t[N * 30]; n long long f(int i, int x) { return 1ll * s[i].k * x + s[i].b; } n void modify(int& o, int l, int r, int x) { n   if (!o) { n     o = ++tot, t[o].id = x; n     return; n   } n   if (l == r) { n     if (f(x, l) < f(t[o].id, l)) t[o].id = x; n     return; n   } n   int mid = (l + r) >> 1; n   if (s[x].k < s[t[o].id].k) { n     if (f(x, mid) < f(t[o].id, mid)) n       modify(t[o].ls, l, mid, t[o].id), t[o].id = x; n     else n       modify(t[o].rs, mid + 1, r, x); n   } else if (s[x].k > s[t[o].id].k) { n     if (f(x, mid) < f(t[o].id, mid)) n       modify(t[o].rs, mid + 1, r, t[o].id), t[o].id = x; n     else n       modify(t[o].ls, l, mid, x); n   } else if (s[x].b < s[t[o].id].b) n     t[o].id = x; n } n long long query(int o, int l, int r, int x) { n   if (!o) return inf; n   if (l == r) return f(t[o].id, l); n   int mid = (l + r) >> 1; n   long long res = f(t[o].id, x); n   if (x <= mid) n     res = min(res, query(t[o].ls, l, mid, x)); n   else n     res = min(res, query(t[o].rs, mid + 1, r, x)); n   return res; n } n int merge(int x, int y, int l, int r) { n   if (!x || !y) return x | y; n   if (l == r) return f(t[x].id, l) < f(t[y].id, l) ? x : y; n   int mid = (l + r) >> 1; n   t[x].ls = merge(t[x].ls, t[y].ls, l, mid); n   t[x].rs = merge(t[x].rs, t[y].rs, mid + 1, r); n   modify(x, l, r, t[y].id); n   return x; n } n long long dp[N]; n void dfs(int u, int f) { n   if (f && E[u].size() == 1) n     dp[u] = 0; n   else { n     for (int v : E[u]) n       if (v != f) dfs(v, u), rt[u] = merge(rt[u], rt[v], -1e5, 1e5); n     dp[u] = query(rt[u], -1e5, 1e5, a[u]); n   } n   s[u] = (line){b[u], dp[u]}, modify(rt[u], -1e5, 1e5, u); n } n int main() { n   n = read(); n   for (int i = 1; i <= n; ++i) a[i] = read(); n   for (int i = 1; i <= n; ++i) b[i] = read(); n   for (int i = 1; i < n; ++i) { n     int u = read(), v = read(); n     E[u].emplace_back(v), E[v].emplace_back(u); n   } n   dfs(1, 0); n   for (int i = 1; i <= n; ++i) printf( %lld  , dp[i]); n   puts(  ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long mod = 1000000007, oo = 1000000009; n void Emsawy() {} n int dx[] = {1, -1, 0, 0, 1, -1, 1, -1}; n int dy[] = {0, 0, 1, -1, 1, -1, -1, 1}; n vector<vector<pair<int, int> > > adj; n int n, m, k, w, h; n struct Node { n   vector<int> ord; n   int cnt; n   Node() { n     ord = vector<int>(26, -1); n     cnt = 0; n   } n   void Add(vector<int>& v) { ord = v; } n }; n int main() { n   Emsawy(); n   string a, b; n   while (cin >> a >> b) { n     vector<Node> L(a.size()), R(a.size()); n     int po = 0; n     for (int i = 0; i < a.size(); i++) { n       if (i > 0) L[i].Add(L[i - 1].ord); n       if (po < b.size() && b[po] == a[i]) { n         L[i].ord[b[po] -  a ] = i; n         po++; n       } n       L[i].cnt = po; n     } n     po = b.size() - 1; n     for (int i = a.size() - 1; i >= 0; i--) { n       if (i < a.size() - 1) R[i].Add(R[i + 1].ord); n       if (po >= 0 && b[po] == a[i]) { n         R[i].ord[b[po] -  a ] = i; n         po--; n       } n       R[i].cnt = b.size() - po - 1; n     } n     bool ok = 1; n     for (int i = 0; i < a.size(); i++) { n       int pl = L[i].ord[a[i] -  a ]; n       int pr = R[i].ord[a[i] -  a ]; n       if (pl == -1 && pr == -1) { n         ok = 0; n         break; n       } n       int mx = 0; n       if (pl != -1) { n         mx = max(mx, L[pl].cnt + (i + 1 < a.size() ? R[i + 1].cnt : 0)); n       } n       if (pr != -1) { n         mx = max(mx, R[pr].cnt + (i - 1 >= 0 ? L[i].cnt : 0)); n       } n       if (mx < b.size()) { n         ok = 0; n         break; n       } n     } n     if (ok) n       cout <<  Yes  << endl; n     else n       cout <<  No  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 200001; n const int INF = 1061109567; n int n, m; n struct Edge { n   int to, val, id, next; n } edge[N << 1]; n int head[N], cnt; n void add_edge(int u, int v, int w, int id) { n   cnt++; n   edge[cnt].to = v; n   edge[cnt].val = w; n   edge[cnt].id = id; n   edge[cnt].next = head[u]; n   head[u] = cnt; n   return; n } n struct Line { n   int u, v, w; n   int id; n   bool flag; n } e[N << 1]; n bool cmp(Line x, Line y) { return x.w < y.w; } n struct Node { n   int l, r; n   int Max, Min, lazy; n } tree[N << 2]; n int pre[N], a[N], w[N]; n int ans[N]; n int f[N]; n int getf(int v) { n   if (v == f[v]) return v; n   f[v] = getf(f[v]); n   return f[v]; n } n bool merge(int u, int v) { n   int f1 = getf(u), f2 = getf(v); n   if (f1 != f2) { n     f[f2] = f1; n     return true; n   } n   return false; n } n void kruskal() { n   for (int i = 1; i <= n; i++) f[i] = i; n   for (int i = 1; i <= m; i++) n     if (merge(e[i].u, e[i].v)) { n       e[i].flag = true; n       add_edge(e[i].u, e[i].v, e[i].w, e[i].id); n       add_edge(e[i].v, e[i].u, e[i].w, e[i].id); n     } n   return; n } n int fa[N], dep[N], size[N], son[N]; n void dfs1(int u, int father) { n   size[u] = 1; n   fa[u] = father; n   dep[u] = dep[father] + 1; n   for (int i = head[u]; i; i = edge[i].next) { n     int v = edge[i].to; n     if (v == father) continue; n     pre[v] = edge[i].id; n     a[v] = edge[i].val; n     dfs1(v, u); n     size[u] += size[v]; n     if (size[v] > size[son[u]]) son[u] = v; n   } n   return; n } n int top[N], dfn[N], Index; n void dfs2(int u, int father) { n   dfn[u] = ++Index; n   top[u] = father; n   w[Index] = a[u]; n   if (!son[u]) return; n   dfs2(son[u], father); n   for (int i = head[u]; i; i = edge[i].next) { n     int v = edge[i].to; n     if (v == son[u] || v == fa[u]) continue; n     dfs2(v, v); n   } n   return; n } n void push_up(int i) { n   tree[i].Max = max(tree[i * 2].Max, tree[i * 2 + 1].Max); n   tree[i].Min = min(tree[i * 2].Min, tree[i * 2 + 1].Min); n   return; n } n void push_down(int i) { n   if (tree[i].lazy >= INF) return; n   tree[i * 2].Min = min(tree[i * 2].Min, tree[i].lazy); n   tree[i * 2].lazy = min(tree[i * 2].lazy, tree[i].lazy); n   tree[i * 2 + 1].Min = min(tree[i * 2 + 1].Min, tree[i].lazy); n   tree[i * 2 + 1].lazy = min(tree[i * 2 + 1].lazy, tree[i].lazy); n   tree[i].lazy = INF; n   return; n } n void build(int i, int l, int r) { n   tree[i].l = l, tree[i].r = r; n   tree[i].Min = tree[i].lazy = INF; n   if (l == r) { n     tree[i].Max = w[l]; n     return; n   } n   int mid = (l + r) / 2; n   build(i * 2, l, mid); n   build(i * 2 + 1, mid + 1, r); n   push_up(i); n   return; n } n void update(int i, int l, int r, int val) { n   if (r < tree[i].l || l > tree[i].r) return; n   if (l <= tree[i].l && tree[i].r <= r) { n     tree[i].Min = min(tree[i].Min, val); n     tree[i].lazy = min(tree[i].lazy, val); n     return; n   } n   push_down(i); n   if (l <= tree[i * 2].r) update(i * 2, l, r, val); n   if (r >= tree[i * 2 + 1].l) update(i * 2 + 1, l, r, val); n   push_up(i); n   return; n } n int querymax(int i, int l, int r) { n   if (r < tree[i].l || l > tree[i].r) return 0; n   if (l <= tree[i].l && tree[i].r <= r) return tree[i].Max; n   push_down(i); n   int res = 0; n   if (l <= tree[i * 2].r) res = max(res, querymax(i * 2, l, r)); n   if (r >= tree[i * 2 + 1].l) res = max(res, querymax(i * 2 + 1, l, r)); n   return res; n } n int querymin(int i, int u) { n   if (tree[i].l == tree[i].r) return tree[i].Min; n   push_down(i); n   if (u <= tree[i * 2].r) n     return querymin(i * 2, u); n   else n     return querymin(i * 2 + 1, u); n } n int getmax(int u, int v) { n   int res = 0; n   if (dep[u] < dep[v]) swap(u, v); n   while (top[u] != top[v]) { n     if (dep[top[u]] < dep[top[v]]) swap(u, v); n     res = max(res, querymax(1, dfn[top[u]], dfn[u])); n     u = fa[top[u]]; n   } n   if (dep[u] > dep[v]) swap(u, v); n   res = max(res, querymax(1, dfn[u] + 1, dfn[v])); n   return res; n } n void modifymin(int u, int v, int val) { n   if (dep[u] < dep[v]) swap(u, v); n   while (top[u] != top[v]) { n     if (dep[top[u]] < dep[top[v]]) swap(u, v); n     update(1, dfn[top[u]], dfn[u], val); n     u = fa[top[u]]; n   } n   if (dep[u] > dep[v]) swap(u, v); n   update(1, dfn[u] + 1, dfn[v], val); n   return; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= m; i++) { n     scanf( %d%d%d , &e[i].u, &e[i].v, &e[i].w); n     e[i].id = i; n   } n   sort(e + 1, e + m + 1, cmp); n   kruskal(); n   dfs1(1, 0); n   dfs2(1, 1); n   build(1, 1, n); n   for (int i = 1; i <= m; i++) n     if (!e[i].flag) { n       ans[e[i].id] = getmax(e[i].u, e[i].v) - 1; n       modifymin(e[i].u, e[i].v, e[i].w); n     } n   for (int i = 2; i <= n; i++) { n     int res = querymin(1, dfn[i]); n     if (res >= INF) n       ans[pre[i]] = -1; n     else n       ans[pre[i]] = res - 1; n   } n   for (int i = 1; i <= m; i++) printf( %d  , ans[i]); 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   ; n   long long int n, m; n   cin >> n >> m; n   long long int a[n]; n   for (long long int i = 0; i < n; i++) cin >> a[i]; n   priority_queue<int> pq; n   long long int res[n]; n   pq.push(a[0]); n   res[0] = 0; n   long long int cursum = a[0]; n   long long int curcount = 0; n   for (long long int i = 1; i < n; i++) { n     if (cursum + a[i] <= m) { n       pq.push(a[i]); n       cursum += a[i]; n       res[i] = curcount; n     } else { n       vector<long long int> temp; n       while (cursum + a[i] > m) { n         long long int x = pq.top(); n         cursum = cursum - x; n         temp.push_back(x); n         pq.pop(); n         curcount++; n       } n       res[i] = curcount; n       temp.push_back(a[i]); n       sort(temp.begin(), temp.end()); n       long long int ab = 0; n       curcount++; n       while (cursum <= m and ab < temp.size()) { n         pq.push(temp[ab]); n         ab++; n         cursum += temp[ab - 1]; n         curcount--; n       } n     } n   } n   for (int i = 0; i < n; i++) cout << res[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   scanf( %d , &n); n   int ans = 0; n   while (n != 0) { n     ans++; n     string s = to_string(n); n     int maior = s[0] -  0 ; n     for (int i = 0; i < s.size(); i++) { n       maior = max(maior, s[i] -  0 ); n     } n     n -= maior; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> q[250000]; n int color[250000]; n int a[250000], b[250000]; n int ssum[250000]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n - 1; i++) scanf( %d%d , &a[i], &b[i]); n   for (int i = 1; i <= n; i++) scanf( %d , &color[i]); n   int sum = 0; n   for (int i = 0; i < n - 1; i++) { n     if (color[a[i]] != color[b[i]]) { n       ssum[a[i]]++; n       ssum[b[i]]++; n       sum++; n     } n   } n   int flag = 0; n   for (int i = 1; i <= n; i++) n     if (sum - ssum[i] == 0) { n       flag = i; n       break; n     } n   if (flag) n     printf( YES n%d , flag); n   else n     printf( NO ); n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2e5 + 5; n const int M = 20; n struct edge1 { n   int v, w; n }; n struct edge2 { n   int u, v, w; n } e[N]; n struct qry { n   int u, w, id; n } q[N]; n vector<edge1> G[N]; n int n, m, c, a[N], dep[N], ans1[N], ans2[N], f[N][M], g[N][M]; n void add(int u, int v, int w) { n   G[u].push_back((edge1){v, w}), G[v].push_back((edge1){u, w}); n } n void dfs(int u, int fa) { n   f[u][0] = fa, dep[u] = dep[fa] + 1; n   for (auto e : G[u]) n     if (e.v != fa) g[e.v][0] = e.w, dfs(e.v, u); n } n int dist(int x, int y) { n   int ans = 0; n   if (dep[x] < dep[y]) swap(x, y); n   for (int i = 18; i >= 0; --i) n     if (dep[f[x][i]] >= dep[y]) ans = max(ans, g[x][i]), x = f[x][i]; n   if (x == y) return ans; n   for (int i = 18; i >= 0; --i) n     if (f[x][i] != f[y][i]) n       ans = max(ans, max(g[x][i], g[y][i])), x = f[x][i], y = f[y][i]; n   return max(ans, max(g[x][0], g[y][0])); n } n namespace dsu { n int fa[N], sz[N], pl[N], mxw[N], val[N]; n void reset() { n   for (int i = 1; i <= n; ++i) n     fa[i] = pl[i] = i, val[i] = a[i], mxw[i] = 0, sz[i] = 1; n } n int find(int x) { return fa[x] == x ? fa[x] : fa[x] = find(fa[x]); } n void Merge(int a, int b) { n   int x = find(a), y = find(b); n   if (sz[x] > sz[y]) swap(x, y); n   if (val[x] > val[y]) n     val[y] = val[x], mxw[y] = mxw[x], pl[y] = pl[x]; n   else if (val[x] == val[y]) n     mxw[y] = max(max(mxw[x], mxw[y]), dist(pl[x], pl[y])); n   fa[x] = y, sz[y] += sz[x]; n } n }  // namespace dsu n bool cmp1(edge2 a, edge2 b) { return a.w > b.w; } n bool cmp2(qry a, qry b) { return a.w > b.w; } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); n   for (int i = 1; i <= n - 1; ++i) { n     scanf( %d%d%d%d , &e[i].u, &e[i].v, &e[i].w, &c); n     add(e[i].u, e[i].v, c); n   } n   dfs(1, 0); n   for (int j = 1; j <= 18; ++j) n     for (int i = 1; i <= n; ++i) n       f[i][j] = f[f[i][j - 1]][j - 1], n       g[i][j] = max(g[i][j - 1], g[f[i][j - 1]][j - 1]); n   for (int i = 1; i <= m; ++i) scanf( %d%d , &q[i].w, &q[i].u), q[i].id = i; n   sort(e + 1, e + n, cmp1); n   sort(q + 1, q + m + 1, cmp2); n   dsu ::reset(); n   for (int i = 1, j = 1; i <= m; ++i) { n     for (; j <= n - 1 && e[j].w >= q[i].w; ++j) dsu ::Merge(e[j].u, e[j].v); n     int res = dsu ::mxw[dsu ::find(q[i].u)]; n     res = max(res, dist(q[i].u, dsu ::pl[dsu ::find(q[i].u)])); n     ans1[q[i].id] = dsu ::val[dsu ::find(q[i].u)], ans2[q[i].id] = res; n   } n   for (int i = 1; i <= m; ++i) printf( %d %d n , ans1[i], ans2[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n double dist(long long int a, long long int b, long long int c, n             long long int d) { n   return sqrt(((a - c) * (a - c)) + (b - d) * (b - d)); n } n struct p { n   double val; n   long long int index; n }; n int comp(const void* a, const void* b) { n   if (((struct p*)a)->val - ((struct p*)b)->val < 0) return -1; n   if (((struct p*)a)->val - ((struct p*)b)->val > 0) return 1; n   if (((struct p*)a)->val - ((struct p*)b)->val == 0) return 0; n } n int main() { n   long long int ax, ay, bx, by, tx, ty, n; n   cin >> ax; n   cin >> ay; n   cin >> bx; n   cin >> by; n   cin >> tx; n   cin >> ty; n   cin >> n; n   long long int i, x, y; n   struct p a[n], b[n], c[n]; n   double sum = 0; n   for (i = 0; i < n; i++) { n     cin >> x; n     cin >> y; n     sum += 2 * dist(tx, ty, x, y); n     a[i].val = dist(ax, ay, x, y) - dist(tx, ty, x, y); n     a[i].index = i; n     b[i].val = dist(bx, by, x, y) - dist(tx, ty, x, y); n     b[i].index = i; n   } n   qsort(a, n, sizeof(struct p), comp); n   qsort(b, n, sizeof(struct p), comp); n   if (a[0].index == b[0].index) { n     sum += min(a[0].val, n                min(b[0].val, min(a[0].val + b[1].val, b[0].val + a[1].val))); n   } else { n     if (a[0].val < b[0].val) { n       sum += a[0].val; n       if (b[0].val < 0) sum += b[0].val; n     } else { n       sum += b[0].val; n       if (a[0].val < 0) sum += a[0].val; n     } n   } n   cout << setprecision(12) << fixed << sum << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<vector<int> > cpu(3); n vector<vector<int> > g(201); n int vs[201]; n bool mission[201]; n int start[3]; n int ans = 1000000000; n int main() { n   int n, startms; n   cin >> n; n   start[0] = -1; n   start[1] = -1; n   start[2] = -1; n   for (int i = 0; i < n; ++i) { n     int comp; n     cin >> comp; n     cpu[comp - 1].push_back(i); n     vs[i] = comp - 1; n   } n   for (int i = 0; i < n; ++i) { n     int tmp; n     cin >> tmp; n     if (tmp == 0) { n       start[vs[i]] = i; n     } n     for (int j = 0; j < tmp; ++j) { n       int v; n       cin >> v; n       g[i].push_back(v - 1); n     } n   } n   for (int u = 0; u < 3; ++u) { n     if (start[u] == -1) continue; n     for (int j = 0; j < n; ++j) mission[j] = false; n     int pc = u; n     mission[start[u]] = true; n     int ost = n - 1; n     int hour = 1; n     while (ost != 0) { n       for (int i = 0; i < cpu[pc].size(); ++i) { n         int ms = cpu[pc][i]; n         if (mission[ms] == true) { n           continue; n         } n         bool fl = true; n         for (int j = 0; j < g[ms].size(); ++j) { n           if (mission[g[ms][j]] == false) { n             fl = false; n             break; n           } n         } n         if (fl == true) { n           mission[ms] = true; n           ost--; n           hour++; n           i = -1; n         } n       } n       if (ost != 0) { n         hour++; n         if (pc == 0) { n           pc = 1; n           continue; n         } n         if (pc == 1) { n           pc = 2; n           continue; n         } n         if (pc == 2) { n           pc = 0; n           continue; n         } n       } n     } n     ans = min(hour, ans); n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long const N = 5e5; n long long a[N]; n void init_code() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n void solve() { n   long long n, d, e; n   cin >> n >> d >> e; n   long long md = n / d; n   long long me = n / (5 * e); n   long long ans = n; n   for (long long i = 0; i * 5 * e <= n; i++) { n     ans = min(ans, (n - i * 5 * e) % d); n   } n   cout << ans <<   n ; n } n signed main() { n   init_code(); n   long long t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long int n; n   int k, w, total; n   cin >> k >> n >> w; n   total = k * w * (w + 1) / 2 - n; n   if (total <= 0) { n     cout <<  0  << endl; n   } else { n     cout << total << endl; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 1e9; n void process() { n   vector<vector<char> > d(8, vector<char>(8)); n   int x1 = 8, y1 = 8, x2 = 8, y2 = 8; n   vector<vector<pair<int, int> > > steps( n       8, vector<pair<int, int> >(8, make_pair(-INF, -INF))); n   for (int i = 0; i < 8; i++) n     for (int j = 0; j < 8; j++) { n       cin >> d[i][j]; n       if (d[i][j] ==  K ) n         if (x1 == 8) { n           x1 = j; n           y1 = i; n         } else { n           x2 = j; n           y2 = i; n         } n     } n   for (int i = 0; i < 8; i++) n     for (int j = 0; j < 8; j++) { n       if (abs(x1 - j) % 4 + abs(y1 - i) % 4 == 0) steps[i][j].first = 0; n       if (abs(x1 - j) % 4 == 2 && abs(y1 - i) % 4 == 2) steps[i][j].first = 1; n       if (abs(x2 - j) % 4 + abs(y2 - i) % 4 == 0) steps[i][j].second = 0; n       if (abs(x2 - j) % 4 == 2 && abs(y2 - i) % 4 == 2) steps[i][j].second = 1; n     } n   for (int i = 0; i < 8; i++) n     for (int j = 0; j < 8; j++) n       if (steps[i][j].first == steps[i][j].second && steps[i][j].first >= 0 && n           d[i][j] !=  # ) { n         cout <<  YES  << endl; n         return; n       } n   cout <<  NO  << endl; n   return; n } n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) process(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   cin >> n; n   long long arr[n]; n   for (int i = 0; i < n; i++) { n     cin >> arr[i]; n   } n   int c = 2; n   int max = 2; n   for (int i = 2; i < n; i++) { n     if (arr[i] == (arr[i - 2] + arr[i - 1])) { n       c++; n       if (c > max) { n         max = c; n       } n     } else n       c = 2; n   } n   if (n == 1) n     cout <<  1 ; n   else n     cout << max; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int i, j, k, n, m, p; n int a[33], c[100010][33]; n int fac[100010], inv[100010]; n int l, r, L, R, len, Ans; n inline int Pow(int x, int y) { n   int Ans = 1; n   for (; y; y >>= 1, x = 1ll * x * x % p) n     if (y & 1) Ans = 1ll * Ans * x % p; n   return Ans; n } n inline void Init() { n   int t = p; n   for (int i = 2; i * i <= t; i++) n     if (!(t % i)) { n       a[++m] = i; n       t /= i; n       while (!(t % i)) t /= i; n     } n   if (t > 1) a[++m] = t; n   int phi = p; n   for (int i = 1; i <= m; i++) phi = phi / a[i] * (a[i] - 1); n   fac[0] = inv[0] = 1; n   for (int i = 1; i < 100010; i++) { n     int x = i; n     for (int j = 1; j <= m; j++) { n       c[i][j] = c[i - 1][j]; n       while (!(x % a[j])) x /= a[j], c[i][j]++; n     } n     fac[i] = 1ll * fac[i - 1] * x % p; n     inv[i] = Pow(fac[i], phi - 1); n   } n } n inline int Calc(int x, int y) { n   if (x < y || y < 0) return 0; n   if (!y) return 1; n   int Ans = 1ll * fac[x] * inv[y] % p * inv[x - y] % p; n   for (int i = 1; i <= m; i++) n     Ans = 1ll * Ans * Pow(a[i], c[x][i] - c[y][i] - c[x - y][i]) % p; n   return Ans; n } n int main() { n   scanf( %d%d%d%d , &n, &p, &l, &r); n   Init(); n   for (i = 0; i <= n; i++) n     len = n - i, Ans = (Ans + 1ll * n                                   (Calc(len, (len - l >> 1)) - n                                    Calc(len, (min(r, len) + len >> 1) + 1)) * n                                   Calc(n, i)) % n                        p; n   if (Ans < 0) Ans += p; n   cout << Ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 6, inf = 2e9 + 7; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   int tc; n   cin >> tc; n   while (tc--) { n     int n, x, y, d; n     cin >> n >> x >> y >> d; n     if (x == y) n       cout <<  0 n ; n     else if (abs(y - x) % d == 0) n       cout << abs(y - x) / d <<   n ; n     else { n       int a = (x - 1) / d; n       if ((x - 1) % d != 0) a++; n       int t = n - x; n       int b = t / d; n       if (t % d != 0) b++; n       if ((y - 1) % d == 0) n         a = a + (y - 1) / d; n       else n         a = inf; n       if ((n - y) % d == 0) n         b = b + (n - y) / d; n       else n         b = inf; n       a = min(a, b); n       if (a == inf) a = -1; n       cout << a <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int gcd(int a, int b) { n   if (a % b != 0) return gcd(b, a % b); n   return b; n } n int main() { n   int n, x = 0, y = 0; n   cin >> n; n   string a, b; n   cin >> a >> b; n   int k = a.size() * b.size() / gcd(a.size(), b.size()); n   for (int i = 0; i < k; i++) { n     int t1 = i % a.size(), t2 = i % b.size(); n     if (a[t1] != b[t2]) { n       if ((a[t1] > b[t2] && a[t1] - b[t2] != 3) || b[t2] - a[t1] == 3) n         x++; n       else n         y++; n     } n   } n   x *= n / k; n   y *= n / k; n   n %= k; n   for (int i = 0; i < n; i++) { n     int t1 = i % a.size(), t2 = i % b.size(); n     if (a[t1] != b[t2]) { n       if ((a[t1] > b[t2] && a[t1] - b[t2] != 3) || b[t2] - a[t1] == 3) n         x++; n       else n         y++; n     } n   } n   cout << x <<     << y; n } n


#include <bits/stdc++.h> n using namespace std; n typedef struct st { n   int a; n   string b; n } st; n bool func(st x, st y) { n   if (x.a == y.a) n     return x.b > y.b; n   else n     return x.a > y.a; n } n int main() { n   string a; n   cin >> a; n   int sum1 = 0; n   for (int i = 0; i < a.size(); i++) sum1 += a[i] - 48; n   if (a.size() == 1) { n     cout << a; n     return 0; n   } n   vector<st> v(1); n   v[0].a = sum1; n   v[0].b = a; n   for (int i = a.size() - 2; i >= 0; i--) { n     string s; n     for (int j = 0; j <= i; j++) s += a[j]; n     s[s.size() - 1]--; n     for (int j = i + 1; j < a.size(); j++) { n       char f = 48 + 9; n       s += f; n     } n     int sum2 = 0; n     for (int j = 0; j < s.size(); j++) sum2 += s[j] - 48; n     st x; n     x.a = sum2; n     x.b = s; n     v.push_back(x); n   } n   sort(v.begin(), v.end(), func); n   if (v[0].b[0] !=  0 ) n     cout << v[0].b; n   else n     for (int i = 1; i < v[0].b.size(); i++) cout << v[0].b[i]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 5; n const int mod = 1e9 + 7; n const int inf = 1e9 + 7; n double dist(double a, double b, double x, double y) { n   return sqrt((x - a) * (x - a) + (y - b) * (y - b)); n } n int n, i; n double px, py, a1, a2, b1, b2, c1, c2, x1, x2, asfdasgas, y2, t, x, y, n     r1 = 2e9, r2, dotx, doty; n double A[N], B[N]; n int main() { n   scanf( %d %lf %lf , &n, &px, &py); n   for (i = 1; i <= n; i++) scanf( %lf %lf , A + i, B + i); n   A[n + 1] = A[1]; n   B[n + 1] = B[1]; n   for (i = 1; i <= n; i++) { n     x1 = A[i]; n     x2 = A[i + 1]; n     asfdasgas = B[i]; n     y2 = B[i + 1]; n     a1 = y2 - asfdasgas; n     b1 = x1 - x2; n     c1 = -a1 * x1 - b1 * asfdasgas; n     a2 = x2 - x1; n     b2 = y2 - asfdasgas; n     c2 = -a2 * px - b2 * py; n     y = (a2 * c1 - a1 * c2) / (a1 * b2 - b1 * a2); n     x = (b1 * c2 - b2 * c1) / (b2 * a1 - b1 * a2); n     if (x >= min(x1, x2) and x <= max(x1, x2) and y >= min(asfdasgas, y2) and n         y <= max(asfdasgas, y2)) { n       t = dist(px, py, x, y); n       if (t <= r1) { n         r1 = t; n         dotx = x; n         doty = y; n       } n     } else { n       t = dist(px, py, x1, asfdasgas); n       if (t <= r1) { n         r1 = t; n         dotx = x1; n         doty = asfdasgas; n       } n       t = dist(px, py, x2, y2); n       if (t <= r1) { n         r1 = t; n         dotx = x2; n         doty = y2; n       } n     } n   } n   for (i = 1; i <= n; i++) r2 = max(r2, dist(px, py, A[i], B[i])); n   printf( %.8lf , acos(-1) * r2 * r2 - acos(-1) * r1 * r1); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n; n   cin >> m >> n; n   double ans = m; n   double i = 1; n   while (1) { n     if (i >= m) break; n     double j = (m - i) / m; n     double k = 1; n     int q = n; n     while (q != 0) { n       if (q & 1 == 1) k *= j; n       q = q >> 1; n       j *= j; n     } n     if (k <= 0.00000001) break; n     ans -= k; n     i++; n   } n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(int &x) { n   x = 0; n   char c = getchar(); n   int f = 1; n   while (!isdigit(c)) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (isdigit(c)) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   x *= f; n } n const int N = 350, mo = 1e9 + 7; n int n, f[N][N], len, A[N], fac[N], ifac[N], qz[N]; n map<int, int> Map; n inline void exgcd(int a, int b, int &x, int &y) { n   if (!b) { n     x = 1; n     y = 0; n     return; n   } n   exgcd(b, a % b, y, x); n   y -= a / b * x; n } n inline int inv(int a) { n   int x, y; n   exgcd(a, mo, x, y); n   return x >= 0 ? x : x + mo; n } n inline int C(int n, int m) { n   if (m < 0 || m > n) return 0; n   return 1LL * fac[n] * ifac[m] % mo * ifac[n - m] % mo; n } n inline void add(int &a, int b) { a = a + b < mo ? a + b : a + b - mo; } n int main() { n   read(n); n   for (register int i = 1; i <= n; i++) { n     int x; n     read(x); n     for (int j = 2; j * j <= x; j++) { n       while (x % (j * j) == 0) x /= j * j; n     } n     Map[x]++; n   } n   while (!Map.empty()) { n     A[++len] = (*Map.begin()).second; n     qz[len] = qz[len - 1] + A[len]; n     Map.erase(Map.begin()); n   } n   fac[0] = 1; n   for (register int i = 1; i <= n; i++) fac[i] = 1LL * fac[i - 1] * i % mo; n   ifac[n] = inv(fac[n]); n   for (register int i = n - 1; i >= 0; i--) n     ifac[i] = 1LL * ifac[i + 1] * (i + 1) % mo; n   f[0][0] = 1; n   for (register int i = 0; i <= len - 1; i++) n     for (register int j = 0; j <= n + 1; j++) n       if (f[i][j]) { n         int now = f[i][j]; n         int red = j, blue = qz[i] - j + 1; n         for (register int a = 0; a <= blue; a++) n           for (register int b = 0; b <= red; b++) n             if (a + b <= A[i + 1]) n               add(f[i + 1][j - b + A[i + 1] - a - b], n                   1LL * now * C(blue, a) % mo * C(red, b) % mo * n                       C(A[i + 1] - 1, a + b - 1) % mo * fac[A[i + 1]] % mo); n       } n   cout << f[len][0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string a; n   cin >> a; n   for (int i = 0; i < int(a.size() - 2); ++i) { n     char x = a[i], y = a[i + 1], z = a[i + 2]; n     if (x !=  .  && y !=  .  && z !=  . ) n       if (x != y && x != z && y != z) { n         cout <<  Yes  << endl; n         return 0; n       } n   } n   cout <<  No  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long i, j, k; n   int n; n   cin >> n; n   vector<int> arr(n); n   int f, l; n   f = 1; n   l = n; n   int sz = 0; n   while (sz < n) { n     if (sz % 2 == 0) { n       arr[sz] = f; n       sz++; n       f++; n     } else { n       arr[sz] = l; n       sz++; n       l--; n     } n   } n   for (auto it : arr) { n     cout << it <<    ; 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   int a[n]; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   vector<int> d; n   for (int i = 0; i < n; ++i) { n     int pos = upper_bound(d.begin(), d.end(), a[i]) - d.begin(); n     if (pos == d.size()) n       d.push_back(a[i]); n     else n       d[pos] = a[i]; n   } n   cout << d.size(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long x; n   x = 2; n   long long n; n   cin >> n; n   for (long long i = 1; i <= n; i++) { n     long long y = (x / i); n     if (y >= i * (i + 1) * (i + 1)) { n       throw 1; n     } n     printf( %I64d n , i * (i + 1) * (i + 1) - y); n     x = i * (i + 1); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXp = 100000; n int p, d; n int p10[8]; n int dig[8]; n bool ip[MAXp]; n int num[8]; n vector<int> v[6][10][6][MAXp / 100]; n int ans = 0; n void init() { n   p10[0] = 1; n   for (int i = 1; i < 8; i++) p10[i] = p10[i - 1] * 10; n   ip[0] = ip[1] = 1; n   for (int i = 2; i < MAXp; i++) n     if (!ip[i]) { n       for (int d = (1), _n = (5); d <= _n; d++) n         for (int y = (0), _n = (min(d, 3)); y <= _n; y++) n           if (i / p10[d - 1] < 10) n             v[d][i / p10[d - 1]][y][i % p10[y]].push_back(i); n       if (i < 1000) n         for (long long j = i * i; j < MAXp; j += i) ip[j] = 1; n     } n } n void bt(int r) { n   if (r == 0) { n     ans++; n     return; n   } n   vector<int> &f = v[d][dig[r]][d - r - 1][num[r]]; n   int z = int((f).size()); n   int c = d - r - 1; n   for (int i = 0, _n = (z); i < _n; i++) { n     int x = f[i]; n     for (int j = (1), _n = (r - 1); j <= _n; j++) n       num[j] += (x / p10[d - j - 1] % 10) * p10[c]; n     bt(r - 1); n     for (int j = (1), _n = (r - 1); j <= _n; j++) n       num[j] -= (x / p10[d - j - 1] % 10) * p10[c]; n   } n } n int main() { n   init(); n   int ts; n   for (cin >> ts; ts--;) { n     cin >> p; n     int tmp = p; n     d = 0; n     while (tmp) dig[d++] = tmp % 10, tmp /= 10; n     reverse(dig, dig + d); n     ans = 0; n     bt(d - 1); n     cout << ans << endl; n   } n   { n     int _; n     cin >> _; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 100; n int n, par[N], up[N], h[N], max_h[N]; n vector<int> child[N], vec, ans; n void dfs(int v = 0) { n   for (int u : child[v]) { n     h[u] = max_h[u] = h[v] + 1; n     dfs(u); n     max_h[v] = max(max_h[v], max_h[u]); n   } n   sort(child[v].begin(), child[v].end(), n        [](int u, int v) { return max_h[u] < max_h[v]; }); n } n void bamboo(int v = 0) { n   if (v) up[v] = vec.back(); n   vec.push_back(v); n   for (int u : child[v]) bamboo(u); n } n void solve(int v = 0) { n   while (v && up[v] != par[v]) { n     ans.push_back(v); n     up[v] = up[up[v]]; n   } n   for (int u : child[v]) solve(u); n } n int main() { n   ios::sync_with_stdio(false), cin.tie(0); n   cin >> n; n   for (int i = 1; i < n; i++) { n     cin >> par[i]; n     child[par[i]].push_back(i); n   } n   dfs(); n   bamboo(); n   for (int v : vec) cout << v <<    ; n   cout <<   n ; n   solve(); n   cout << ans.size() <<   n ; n   for (int u : ans) cout << u <<    ; n } n


#include <bits/stdc++.h> n int main() { n   int mem, arr[5001], i, tri = 0; n   scanf( %d , &mem); n   for (i = 1; i <= mem; i++) { n     scanf( %d , &arr[i]); n   } n   for (i = 1; i <= mem; i++) { n     if (i == arr[arr[arr[i]]]) { n       tri = 1; n     } n   } n   if (tri == 1) { n     printf( YES n ); n   } else { n     printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long n, k, bottom, top; n   cin >> n >> k; n   bottom = 1; n   top = n; n   for (long i = 0; i < k - 1; i++) { n     if (i % 2 == 0) { n       cout << bottom <<    ; n       bottom++; n     } else { n       cout << top <<    ; n       top--; n     } n   } n   long sum, val; n   if ((k - 1) % 2 == 0) { n     val = bottom; n     sum = 1; n   } else { n     val = top; n     sum = -1; n   } n   for (long i = 0; i <= n - k; i++) { n     cout << val <<    ; n     val += sum; n   } n   return 0; n } n