#include <bits/stdc++.h> n using namespace std; n const int maxn = 1e9 + 1; n int a[201][201]; n int cnt[10][10]; n int calc(int x, int y) { n   if (x * 2 > y) n     return 500; n   else if (x * 4 > y) n     return 1000; n   else if (x * 8 > y) n     return 1500; n   else if (x * 16 > y) n     return 2000; n   else if (x * 32 > y) n     return 2500; n   else n     return 3000; n } n int use[10]; n int main() { n   int n; n   cin >> n; n   for (int i = 0; i < n; i++) { n     for (int j = 0; j < 5; j++) { n       cin >> a[i][j]; n     } n   } n   for (int k = 0; k < 5; k++) { n     if (a[0][k] == -1) n       use[k] = 0; n     else if (a[1][k] == -1 || (a[1][k] != -1 && a[1][k] > a[0][k])) n       use[k] = -1; n     else n       use[k] = 1; n   } n   for (int t = 0; t < 50000; t++) { n     int sc1 = 0, sc0 = 0; n     for (int i = 0; i < 5; i++) { n       long long x = 0, y = 0; n       for (int j = 0; j < n; j++) { n         if (a[j][i] != -1) x++; n       } n       if (use[i] == 1) x += t; n       y = n + t; n       if (a[0][i] != -1) { n         sc0 += calc(x, y) * (250 - a[0][i]); n       } n       if (a[1][i] != -1) { n         sc1 += calc(x, y) * (250 - a[1][i]); n       } n     } n     if (sc0 > sc1) { n       cout << t << endl; n       return 0; n     } n   } n   cout << -1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long powmod(long long base, long long exp) { n   base %= ((int)1e9 + 7); n   long long result = 1; n   while (exp > 0) { n     if (exp & 1) result = (result * base) % ((int)1e9 + 7); n     base = (base * base) % ((int)1e9 + 7); n     exp >>= 1; n   } n   return result % ((int)1e9 + 7); n } n long long inv(long long x) { n   return powmod(x, ((int)1e9 + 7) - 2) % ((int)1e9 + 7); n } n long long sum_to(long long x) { n   if (x < 0) return 0; n   return (((x * (x + 1)) % ((int)1e9 + 7)) * inv(2)) % ((int)1e9 + 7); n } n long long sq(long long x) { return (x * x) % ((int)1e9 + 7); } n long long sumodd(long long o) { n   long long x = o / 2; n   x %= ((int)1e9 + 7); n   return sq(x + 1) % ((int)1e9 + 7); n } n long long sumeven(long long e) { n   e /= 2; n   e %= ((int)1e9 + 7); n   return (2 * sum_to(e)) % ((int)1e9 + 7); n } n long long f(long long x) { n   if (x == 0) return 0; n   if (x == 1) return 1; n   if (x == 2) return 3; n   long long k = 0; n   for (;; k++) n     if ((1LL << k) - 1 > x) break; n   k--; n   long long e = 2, o = 1; n   long long adde = 4, addo = 2; n   for (int i = int(0); i < int(k / 2 - 1); i++) { n     e += adde; n     o += addo; n     adde *= 4; n     addo *= 4; n   } n   long long d = x - ((1LL << k) - 1); n   if (k & 1) o = (o + addo); n   if (k & 1) { n     o = (o + 2 * ((1LL << (k - 1)) - 1)); n     e = (e + 2 * ((1LL << (k - 2)) - 1)); n     e = (e + 2 * d); n   } else { n     o = (o + 2 * ((1LL << (k - 2)) - 1)); n     e = (e + 2 * ((1LL << (k - 1)) - 1)); n     o = (o + 2 * d); n   } n   return ((sumodd(o) + sumeven(e)) % ((int)1e9 + 7) + ((int)1e9 + 7)) % n          ((int)1e9 + 7); n } n int main() { n   ios::sync_with_stdio(false); n   long long l, r; n   cin >> l >> r; n   cout << (f(r) - f(l - 1) + ((int)1e9 + 7)) % ((int)1e9 + 7) <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int d, m, n, b[210000]; n int top, s[210000]; n struct node { n   int x, p; n   bool operator<(const node &a) const { return x < a.x; } n } a[210000]; n char Getchar() { n   static char now[1 << 20], *S, *T; n   if (T == S) { n     T = (S = now) + fread(now, 1, 1 << 20, stdin); n     if (T == S) return EOF; n   } n   return *S++; n } 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 <=  9  && ch >=  0 ) x = x * 10 + ch -  0 , ch = Getchar(); n   return x * f; n } n int main() { n   d = read(); n   m = read(); n   n = read(); n   for (int i = 0; i < n; i++) { n     a[i].x = read(); n     a[i].p = read(); n   } n   sort(a, a + n); n   a[n].x = d; n   a[n].p = 0; n   top = 1; n   s[top] = n; n   for (int i = n - 1; i >= 0; i--) { n     while (a[s[top]].p >= a[i].p) top--; n     b[i] = s[top]; n     s[++top] = i; n   } n   int res = m - a[0].x; n   long long ans = 0; n   for (int i = 0; i < n && res >= 0; i++) { n     if (a[b[i]].x - a[i].x > res) { n       int d = min(a[b[i]].x - a[i].x, m) - res; n       res += d; n       ans += 1ll * d * a[i].p; n     } n     res -= a[i + 1].x - a[i].x; n   } n   if (res < 0) n     puts( -1 ); n   else n     printf( %lld n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5, M = 1e4, MOD = 1e9 + 7, INF = 0x3f3f3f3f; n const double eps = 1e-8, pi = acos(-1.0); n int n, m; n vector<int> son[N + 10]; n int col[N + 10]; n int in[N + 10], out[N + 10], bel[N + 10]; n int tot; n int k[N + 10]; n struct query { n   int l, r, id; n } node[N + 10]; n int unit; n int cnt[N + 10]; n int k_ti[N + 10]; n int ans[N + 10]; n bool cmp(query a, query b) { n   if (a.l / unit != b.l / unit) n     return a.l / unit < b.l / unit; n   else n     return a.r < b.r; n } n void work() { n   memset(cnt, 0, sizeof cnt); n   memset(k_ti, 0, sizeof k_ti); n   int l = 1, r = 0; n   for (int i = 0; i < m; i++) { n     while (r < node[i].r) { n       r++; n       cnt[col[bel[r]]]++; n       k_ti[cnt[col[bel[r]]]]++; n     } n     while (r > node[i].r) { n       k_ti[cnt[col[bel[r]]]]--; n       cnt[col[bel[r]]]--; n       r--; n     } n     while (l < node[i].l) { n       k_ti[cnt[col[bel[l]]]]--; n       cnt[col[bel[l]]]--; n       l++; n     } n     while (l > node[i].l) { n       l--; n       cnt[col[bel[l]]]++; n       k_ti[cnt[col[bel[l]]]]++; n     } n     ans[node[i].id] = k_ti[k[node[i].id]]; n   } n } n void dfs(int u, int fa) { n   in[u] = ++tot; n   bel[tot] = u; n   for (int i = 0; i < son[u].size(); i++) { n     int v = son[u][i]; n     if (v == fa) continue; n     dfs(v, u); n   } n   out[u] = tot; n } n int main() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) scanf( %d , &col[i]); n   for (int i = 0; i < n - 1; i++) { n     int u, v; n     scanf( %d%d , &u, &v); n     son[u].push_back(v); n     son[v].push_back(u); n   } n   tot = 0; n   dfs(1, -1); n   unit = (int)sqrt(n); n   for (int i = 0; i < m; i++) { n     int v; n     scanf( %d%d , &v, &k[i]); n     node[i].id = i; n     node[i].l = in[v]; n     node[i].r = out[v]; n   } n   sort(node, node + m, cmp); n   work(); n   for (int i = 0; i < m; i++) printf( %d n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int m, n; n   cin >> m >> n; n   vector<int> sg(n, +1); n   for (int i = 1, x; i <= n; ++i) { n     cout << i <<   n ; n     fflush(stdout); n     cin >> x; n     if (x < 0) sg[i - 1] = -1; n     if (x == 0) return 0; n   } n   int l = n + 1, r = m, pt = 0, x; n   while (l < r) { n     int md = (l + r) >> 1; n     cout << md <<   n ; n     fflush(stdout); n     cin >> x; n     x *= sg[pt]; n     pt = (pt + 1) % n; n     if (x == 0) return 0; n     if (x < 0) n       r = md; n     else n       l = md + 1; n   } n   cout << l <<   n ; n   fflush(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 5e5 + 5, mod = 1e9 + 7; n long long pwr(long long b, long long p) { n   long long res = 1; n   while (p) { n     if (p % 2) res = res * b % mod; n     b = b * b % mod; n     p /= 2; n   } n   return res; n } n long long k, n; n string colors[] = { white ,  yellow ,  green ,  blue ,  red ,  orange }; n map<string, int> ids; n int cant[10][10]; n map<long long, int> node_color; n map<long long, set<long long>> adj; n map<long long, long long> mem[10]; n long long dfs(long long p, long long c) { n   if (node_color.count(p) && node_color[p] != c) return 0; n   if (adj.count(p) == 0) return 1; n   if (mem[c].count(p)) return mem[c][p]; n   long long ret = 1; n   for (long long ch : adj[p]) { n     long long cur = 0; n     for (int i = 1; i <= 6; ++i) { n       if (cant[c][i]) continue; n       cur += dfs(ch, i); n       cur %= mod; n     } n     ret *= cur; n     ret %= mod; n   } n   return mem[c][p] = ret; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   for (int i = 0; i < 6; ++i) { n     ids[colors[i]] = i + 1, cant[i + 1][i + 1] = 1; n     if (i % 2 == 0) cant[i + 1][i + 2] = cant[i + 2][i + 1] = 1; n   } n   cin >> k >> n; n   set<long long> used; n   while (n--) { n     long long node; n     string c; n     cin >> node >> c; n     int id = ids[c]; n     node_color[node] = id; n     used.insert(node); n     while (node != 1) { n       long long parent = node / 2; n       used.insert(parent); n       adj[parent].insert(node); n       node = parent; n     } n   } n   long long color_ways = 0; n   for (int i = 1; i <= 6; ++i) { n     color_ways += dfs(1, i); n     color_ways %= mod; n   } n   long long rem = ((1ll) << (1ll * k)) - 1 - used.size(); n   cout << color_ways * pwr(4, rem) % mod << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   char c1, c2; n   int t, n1, n2, ans1, ans2; n   string s =  ^>v< ; n   cin >> c1 >> c2 >> t; n   n1 = s.find(c1); n   n2 = s.find(c2); n   t = t % 4; n   ans1 = (n1 + t) % 4; n   ans2 = (n1 - t); n   if (ans2 < 0) ans2 += 4; n   if (ans1 == n2 && ans2 == n2) n     puts( undefined ); n   else if (ans1 == n2) n     puts( cw ); n   else n     puts( ccw ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void upd(long long& x) { x = -x - 1; } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   long long n; n   cin >> n; n   vector<long long> a(n); n   for (int j = 0; j < n; j++) { n     cin >> a[j]; n     if (a[j] >= 0) upd(a[j]); n   } n   if (n & 1) { n     int jd = 0; n     for (int j = 0; j < n; j++) { n       if (a[jd] > a[j]) jd = j; n     } n     upd(a[jd]); n   } n   for (int j = 0; j < n; j++) cout << a[j] <<    ; n } n


#include <bits/stdc++.h> n using namespace std; n const double eps = 1e-9; n const double pi = acos(-1.0); n const int N = 100010; n int n; n long long x, y, a[N]; n void solve() { n   for (int i = 0; i < (int)(n); ++i) a[i] = 1; n   a[0] = y - n + 1; n   for (int i = 0; i < (int)(n); ++i) x -= ((a[i]) * (a[i])); n   if (y < n || x > 0) n     puts( -1 ); n   else { n     for (int i = 0; i < (int)(n); ++i) n       printf( n            %I64d  n             n , n           a[i]); n   } n } n int main() { n   while ( n       ~scanf( %d  n               %I64d  n               %I64d , n              &n, &x, &y)) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = (1 << 30) - 1; n const int maxn = 100010; n int IN() { n   int c, f, x; n   while (!isdigit(c = getchar()) && c !=  - ) n     ; n   c ==  -  ? (f = 1, x = 0) : (f = 0, x = c -  0 ); n   while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c -  0 ; n   return !f ? x : -x; n } n int n, T; n char s[maxn]; n bool vis[maxn], c[maxn]; n bool check() { n   int ans = 0; n   for (int i = 0; i < 26; i++) n     if (vis[i]) ans++; n   return ans == 1; n } n int main() { n   scanf( %d , &n); n   int ans = 0; n   fill(vis, vis + maxn, 1); n   fill(c, c + maxn, 1); n   for (int i = 1; i <= n; i++) { n     char op[10]; n     scanf( %s%s , op, s); n     char tp = op[0]; n     int len = strlen(s); n     if (tp ==  . ) { n       for (int i = 0; i < len; i++) vis[s[i] -  a ] = 0; n     } else if (tp ==  ! ) { n       if (check()) ans++; n       int t[27]; n       memset(t, 0, sizeof(t)); n       for (int i = 0; i < len; i++) t[s[i] -  a ]++; n       for (int i = 0; i < 26; i++) n         if (!t[i]) vis[i] = 0; n     } else if (tp ==  ? ) { n       if (i != n && check()) ans++; n       vis[s[0] -  a ] = 0; n     } n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct node { n   char name[55]; n   int score; n   int c[50]; n } comp[55]; n int cmp1(node x, node y) { n   if (x.score > y.score) n     return 1; n   else if (x.score == y.score) { n     for (int i = 0; i < 50; i++) n       if (x.c[i] > y.c[i]) n         return 1; n       else if (x.c[i] < y.c[i]) n         return 0; n   } else n     return 0; n } n int cmp2(node x, node y) { n   if (x.c[0] > y.c[0]) n     return 1; n   else if (x.c[0] == y.c[0]) { n     if (x.score > y.score) n       return 1; n     else if (x.score == y.score) { n       for (int i = 1; i < 50; i++) n         if (x.c[i] > y.c[i]) n           return 1; n         else if (x.c[i] < y.c[i]) n           return 0; n     } else n       return 0; n   } else n     return 0; n } n int s[10] = {25, 18, 15, 12, 10, 8, 6, 4, 2, 1}; n int main() { n   int t; n   while (scanf( %d , &t) != EOF) { n     memset(comp, 0, sizeof(comp)); n     int sum = 0; n     while (t--) { n       int n, i; n       char na[55]; n       scanf( %d , &n); n       for (i = 0; i < n; i++) { n         scanf( %s , na); n         int j; n         for (j = 0; j < sum; j++) { n           if (strcmp(comp[j].name, na) == 0) { n             if (i < 10) comp[j].score += s[i]; n             comp[j].c[i]++; n             break; n           } n         } n         if (j == sum) { n           strcpy(comp[sum].name, na); n           if (i < 10) comp[sum].score += s[i]; n           comp[sum].c[i]++; n           sum++; n         } n       } n     } n     sort(comp, comp + sum, cmp1); n     printf( %s n , comp[0].name); n     sort(comp, comp + sum, cmp2); n     printf( %s n , comp[0].name); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long k; n   cin >> k; n   if (k <= 36) { n     for (long long i = 1; i <= (k / 2); i++) { n       cout << 8; n     } n     if (k % 2 == 1) { n       cout << 4; n     } n   } else { n     cout << -1; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int getT(vector<vector<long long int>>&, int, int); n void _dp(vector<vector<long long int>>& dp, int n) { n   for (int j = 0; j < n; j++) dp[0][j] = 1; n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= n; j++) n       for (int k = 0; k < i; k++) n         dp[i][j] += dp[k][j - 1] * dp[(i - 1) - k][j - 1]; n } n int main() { n   int n, h; n   scanf( %d %d , &n, &h); n   vector<vector<long long int>> dp(n + 1, vector<long long int>(n + 1, 0)); n   _dp(dp, n); n   cout << dp[n][n] - dp[n][h - 1] << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m, i, a[101], x, y; n   cin >> n; n   for (i = 1; i <= n; i++) cin >> a[i]; n   cin >> m; n   while (m--) { n     cin >> x >> y; n     if (x != 1) a[x - 1] += y - 1; n     if (x != n) a[x + 1] += a[x] - y; n     a[x] = 0; n   } n   for (i = 1; i <= n; i++) cout << a[i] << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:16777216 ) n using namespace std; n const double eps = 1e-9; n const double pi = acos(-1.0); n const int maxn = (int)1e4 + 10; n long long a[maxn], v[maxn], b[maxn]; n long long gcd(long long A, long long B) { n   A = abs(A); n   B = abs(B); n   while (B != 0) { n     A %= B; n     swap(A, B); n   } n   return A; n } n bool gcdEx(long long A, long long &X, long long B, long long &Y, long long C) { n   long long lastX = 0; n   long long lastY = 1; n   X = 1; n   Y = 0; n   while (B != 0) { n     long long sub = A / B; n     X -= lastX * sub; n     Y -= lastY * sub; n     A -= B * sub; n     swap(A, B); n     swap(X, lastX); n     swap(Y, lastY); n   } n   if (C % A != 0) return false; n   X *= C / A; n   Y *= C / A; n   return true; n } n int main() { n   long long n, m; n   int k; n   cin >> n >> m >> k; n   long long lcm = 1; n   for (int i = 0; i < k; i++) { n     cin >> a[i]; n     b[i] = a[i]; n     long long g = gcd(lcm, a[i]); n     if (a[i] / g > n / lcm) { n       puts( NO ); n       return 0; n     } n     lcm = a[i] / g * lcm; n   } n   for (int i = 0; i < k; i++) { n     v[i] = (-i % a[i] + a[i]) % a[i]; n   } n   for (int i = 0; i < k - 1; i++) { n     long long A = a[i]; n     long long B = -a[i + 1]; n     long long C = v[i + 1] - v[i]; n     long long G = gcd(A, B); n     long long x, y; n     if (!gcdEx(A, x, B, y, C)) { n       puts( NO ); n       return 0; n     } n     assert(A * x + B * y == C); n     long long X = (long long)1e18; n     if (x < 0) { n       long long add = (-x - B / G - 1) / (-B / G); n       x += add * (-B / G); n       y -= add * (-A / G); n     } n     long long sub = x / (-B / G); n     x -= sub * (-B / G); n     y += sub * (-A / G); n     assert(x >= 0); n     assert(B < 0 && -B == a[i + 1]); n     assert(A * x + B * y == C); n     assert(A * x >= 0); n     assert(C < a[i + 1]); n     if (y >= 0) { n       X = min(X, a[i] * x + v[i]); n     } n     if (y < 0) { n       long long add = (-y + A / G - 1) / (A / G); n       y += add * (A / G); n       x -= add * (B / G); n     } n     if (y >= 0) { n       long long sub = y / (A / G); n       y -= sub * (A / G); n       x += sub * (B / G); n     } n     if (x >= 0) { n       X = min(X, a[i + 1] * y + v[i + 1]); n     } n     if (X == (long long)1e18) { n       puts( NO ); n       return 0; n     } n     if (X > m - k + 1) { n       puts( NO ); n       return 0; n     } n     assert(X % a[i + 1] == v[i + 1]); n     a[i + 1] = a[i] / gcd(a[i], a[i + 1]) * a[i + 1]; n     v[i + 1] = X; n   } n   long long X = v[k - 1]; n   if (X == 0) X = a[k - 1]; n   if (X > m - k + 1) { n     puts( NO ); n     return 0; n   } n   for (int i = 0; i < k; i++) { n     if (gcd(lcm, X + i) != b[i]) { n       puts( NO ); n       return 0; n     } n   } n   puts( YES ); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 2e5 + 10; n int Ans[MAXN * 2], L, R, n; n struct XY { n   int sz = 0, totid = 0, Ret, ll, rr; n   int las[MAXN * 10], top[MAXN], en[MAXN], pos[MAXN], siz[MAXN], dep[MAXN], n       fa[MAXN], deleted[MAXN * 3]; n   struct Edge { n     int to, pre; n   } e[MAXN * 35]; n   void add(int a, int b) { n     e[++sz].to = b; n     e[sz].pre = las[a]; n     las[a] = sz; n   } n   void dfs1(int u, int father, int depth) { n     dep[u] = depth; n     fa[u] = father; n     siz[u] = 1; n     for (int i = las[u]; i; i = e[i].pre) { n       dfs1(e[i].to, u, depth + 1); n       siz[u] += siz[e[i].to]; n     } n   } n   void dfs2(int u, int tp) { n     pos[u] = ++totid; n     top[u] = tp; n     int v = 0, maxisz = 0; n     for (int i = las[u]; i; i = e[i].pre) n       if (siz[e[i].to] > maxisz) v = e[i].to, maxisz = siz[e[i].to]; n     if (v) dfs2(v, tp); n     for (int i = las[u]; i; i = e[i].pre) n       if (e[i].to != v) dfs2(e[i].to, e[i].to); n   } n   void link(int rt, int l, int r) { n     if (ll <= l && r <= rr) { n       add(rt, Ret); n       return; n     } n     int mid = (l + r) >> 1; n     if (ll <= mid) link(rt << 1, l, mid); n     if (rr > mid) link(rt << 1 | 1, mid + 1, r); n   } n   void insert(int x, int y, int id) { n     while (top[x] != top[y]) { n       if (dep[top[x]] < dep[top[y]]) swap(x, y); n       ll = pos[top[x]]; n       rr = pos[x]; n       Ret = id; n       link(1, 1, n); n       x = fa[top[x]]; n     } n     if (dep[x] < dep[y]) swap(x, y); n     ll = pos[y] + 1; n     rr = pos[x]; n     Ret = id; n     if (ll <= rr) link(1, 1, n); n   } n   void query(int rt, int l, int r) { n     for (int i = las[rt]; i; i = e[i].pre) n       if (!deleted[e[i].to]) Ans[++R] = e[i].to, deleted[e[i].to] = 1; n     las[rt] = 0; n     int mid = (l + r) >> 1; n     if (l == r) return; n     if (ll <= mid) n       query(rt << 1, l, mid); n     else n       query(rt << 1 | 1, mid + 1, r); n   } n   void doit(int t) { n     ll = pos[t + 1]; n     query(1, 1, n); n   } n   void work(int l, int r) { n     sort(Ans + l, Ans + r + 1); n     for (int i = l; i <= r; ++i) printf( %d  , Ans[i]); n     puts(  ); n     for (int i = l; i <= r; ++i) doit(Ans[i]); n     L = r; n   } n   void read() { n     for (int i = 2; i <= n; ++i) scanf( %d , &fa[i]), add(fa[i], i); n     dfs1(1, -1, 1); n     dfs2(1, 1); n     for (int i = 0; i <= n + 9; ++i) las[i] = 0; n     sz = 0; n   } n } Tree[2]; n int main() { n   scanf( %d , &n); n   Tree[0].read(); n   Tree[1].read(); n   for (int i = 2; i <= n; ++i) Tree[1].insert(Tree[0].fa[i], i, i - 1); n   for (int i = 2; i <= n; ++i) Tree[0].insert(Tree[1].fa[i], i, i - 1); n   scanf( %d , &Ans[++R]); n   Tree[1].deleted[Ans[R]] = 1; n   while (L < R) { n     if (L < R) puts( Blue ), Tree[0].work(L + 1, R); n     if (L < R) puts( Red ), Tree[1].work(L + 1, R); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, a[200007], q; n int l[200007], r[200007], prime[1000007], id[1000007]; n long long bit[200007], ans[200007], inv[1000007], prd[200007], divide[200007], n     cal[1000007]; n vector<pair<int, int> > query[200007]; n vector<int> dv[200007]; n const long long mod = 1e9 + 7; n long long pw(long long a, long long b) { n   if (b == 1) return a; n   long long ret = pw(a, b / 2); n   if (b % 2 == 0) return ret * ret % mod; n   return ret * ret % mod * a % mod; n } n void upd(int pos, long long val) { n   for (int i = pos; i <= n; i += i & -i) bit[i] = bit[i] * val % mod; n } n long long get(int pos) { n   long long res = 1; n   for (int i = pos; i >= 1; i -= i & -i) { n     res = (res * bit[i]) % mod; n   } n   return res; n } n long long get(int l, int r) { return (get(r) * pw(get(l - 1), mod - 2)) % mod; } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) { n     scanf( %d , &a[i]); n   } n   for (int i = 2; i <= 1000000; i++) { n     if (prime[i]) continue; n     for (int j = i; j <= 1000000; j += i) { n       if (!prime[j]) prime[j] = i; n     } n   } n   inv[0] = 1; n   for (int i = 1; i <= 1000000; i++) { n     inv[i] = pw(i, mod - 2); n     if (prime[i] == i) cal[i] = 1LL * (i - 1) * inv[i] % mod; n   } n   prd[0] = 1, divide[0] = 1; n   for (int i = 1; i <= n; i++) n     prd[i] = (prd[i - 1] * 1LL * a[i]) % mod, n     divide[i] = (divide[i - 1] * inv[a[i]]) % mod; n   for (int i = 1; i <= n; i++) { n     bit[i] = 1; n     int cur = a[i], prv = 1; n     while (cur > 1) { n       if (prime[cur] != prv) { n         prv = prime[cur]; n         dv[i].push_back(prv); n       } n       cur /= prv; n     } n   } n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     scanf( %d %d , &l[i], &r[i]); n     ans[i] = (prd[r[i]] * divide[l[i] - 1]) % mod; n     query[r[i]].push_back(make_pair(l[i], i)); n   } n   for (int i = 1; i <= n; i++) { n     for (int j : dv[i]) { n       int pos = id[j]; n       if (pos) upd(pos, 1LL * j * inv[j - 1] % mod); n       upd(i, cal[j]); n       id[j] = i; n     } n     for (int j = 0; j < (int)query[i].size(); j++) { n       int L = query[i][j].first; n       int idx = query[i][j].second; n       ans[idx] = (ans[idx] * get(L, i)) % mod; n     } n   } n   for (int i = 1; i <= q; i++) printf( %lld n , ans[i]); n } n


#include <bits/stdc++.h> n using namespace std; n char inc(char &c) { n   char ret = c; n   if (c ==  z ) n     c =  a ; n   else n     ++c; n   return ret; n } n int main() { n   int n; n   cin >> n; n   char t[4][n]; n   char c =  a ; n   t[0][0] = t[1][0] = inc(c); n   for (int i = 1; i < n; ++i) { n     if (i % 2 == 0) { n       t[0][i] = t[0][i - 1] = inc(c); n       t[1][i] = t[1][i - 1] = inc(c); n     } else { n       t[2][i] = t[2][i - 1] = inc(c); n       t[3][i] = t[3][i - 1] = inc(c); n     } n   } n   if (n % 2 == 0) n     t[0][n - 1] = t[1][n - 1] = inc(c); n   else n     t[2][n - 1] = t[3][n - 1] = inc(c); n   for (int i = 0; i < 4; ++i) { n     for (int j = 0; j < n; ++j) cout << t[i][j]; n     cout << endl; n   } n   cout << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAX = 5e5 + 3; n const long long MOD = 998244353; n int board[4][52]; n int n; n vector<pair<int, pair<int, int> > > moves; n bool flag; n void make_move(int a, int b, int x, int y) { n   if (board[a][b] == 0 && board[x][y] == 0) return; n   moves.push_back( n       pair<int, pair<int, int> >(board[a][b], pair<int, int>(x, y))); n   board[x][y] = board[a][b]; n   board[a][b] = 0; n   flag = true; n } n void enter() { n   for (int i = 0; i < n; i++) { n     if (board[0][i] == board[1][i]) { n       make_move(1, i, 0, i); n     } n     if (board[3][i] == board[2][i]) { n       make_move(2, i, 3, i); n     } n   } n } n const int dx[] = {0, 1, 0, -1}; n const int dy[] = {-1, 0, 1, 0}; n void print() { n   for (int i = 0; i < 4; i++) { n     for (int j = 0; j < n; j++) { n       printf( %2d  , board[i][j]); n     } n     puts(  ); n   } n   puts(  ); n } n void move() { n   int f = -1, c = -1; n   for (int i = 0; i < n; i++) { n     if (board[1][i] == 0) { n       f = 1; n       c = i; n       break; n     } n     if (board[2][i] == 0) { n       f = 2; n       c = i; n       break; n     } n   } n   if (f == -1) return; n   int d; n   if (f == 1) n     d = 0; n   else n     d = 2; n   for (int t = 0; t < 2 * n - 1; t++) { n     int nf = f + dx[d]; n     int nc = c + dy[d]; n     if (nf >= 1 && nf <= 2 && nc >= 0 && nc < n) { n       make_move(nf, nc, f, c); n       f = nf; n       c = nc; n     } else { n       t--; n       d = (d + 1) % 4; n     } n   } n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   int k; n   cin >> n >> k; n   for (int i = 0; i < 4; i++) { n     for (int j = 0; j < n; j++) { n       cin >> board[i][j]; n     } n   } n   flag = true; n   while (flag) { n     flag = false; n     enter(); n     move(); n   } n   if (moves.size()) { n     cout << moves.size() <<   n ; n     for (auto x : moves) { n       cout << x.first <<     << x.second.first + 1 <<     << x.second.second + 1 n            <<   n ; n     } n   } else { n     cout << -1 <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n  n namespace NTT n { n     const int P=998244353,g=3; n     const int W=22,S=1<<W; n     const int J=86583718; n  n     inline int add(int a,int b) {int r=a+b; return r<P?r:r-P;} n     inline int sub(int a,int b) {int r=a-b; return r<0?r+P:r;} n     inline int mul(long long a,long long b) {return (a*b)%P;} n     inline int inv(int a) {return a==1?a:mul(inv(P%a),P-P/a);} n     inline int qpow(int a,long long k) n     { n         int r=1; n         while (k) n         { n             if (k&1) r=mul(r,a); n             k>>=1; a=mul(a,a); n         } n         return r; n     } n  n     int r[S],w[2][S]; n     void init(int lim) n     { n         int w0=qpow(g,(P-1)/lim); n         w[0][0]=w[1][0]=1; n         for (int i=1;i<lim;i++) w[0][i]=w[1][lim-i]=mul(w[0][i-1],w0); n         for (int i=0;i<lim;i++) r[i]=(r[i>>1]>>1)|((i&1)*(lim>>1)); n     } n  n     void ntt(int *a,int lim,int o) n     { n         for (int i=0;i<lim;i++) if (i<r[i]) swap(a[i],a[r[i]]); n         for (int i=1;i<lim;i<<=1) n         { n             for (int j=0,t=lim/(i<<1);j<lim;j+=i<<1) n             { n                 for (int k=j,l=0;k<j+i;k++,l+=t) n                 { n                     int x=a[k],y=mul(w[o][l],a[k+i]); n                     a[k]=add(x,y); n                     a[k+i]=sub(x,y); n                 } n             } n         } n         if (o) n         { n             int tmp=NTT::inv(lim); n             for (int i=0;i<lim;i++) a[i]=mul(a[i],tmp); n         } n     } n  n     vector<int> poly_add(const vector<int> &a,const vector<int> &b) n     { n         int n=a.size(),m=b.size(); n         vector<int> c; n         for (int i=0;i<max(n,m);i++) c.push_back(add((i<n?a[i]:0),(i<m?b[i]:0))); n         return c; n     } n  n     vector<int> poly_sub(const vector<int> &a,const vector<int> &b) n     { n         int n=a.size(),m=b.size(); n         vector<int> c; n         for (int i=0;i<max(n,m);i++) c.push_back(sub((i<n?a[i]:0),(i<m?b[i]:0))); n         return c; n     } n  n     vector<int> poly_d(const vector<int> &a) n     { n         int n=a.size(); n         vector<int> b; n         for (int i=1;i<n;i++) b.push_back(mul(a[i],i)); n         return b; n     } n  n     vector<int> poly_s(const vector<int> &a) n     { n         int n=a.size(); n         vector<int> b{0}; n         for (int i=0;i<n;i++) b.push_back(mul(a[i],inv(i+1))); n         return b; n     } n  n     int p1[S],p2[S]; n     vector<int> poly_mul(const vector<int> &a,const vector<int> &b) n     { n         int n=a.size(),m=b.size(); n         int lim=1; n         while (lim<(n<<1)) lim<<=1; n         while (lim<(m<<1)) lim<<=1; n         init(lim); n         for (int i=0;i<lim;i++) r[i]=(i&1)*(lim>>1)+(r[i>>1]>>1); n         copy_n(a.begin(),n,p1); fill(p1+n,p1+lim,0); n         copy_n(b.begin(),m,p2); fill(p2+m,p2+lim,0); n         ntt(p1,lim,0); n         ntt(p2,lim,0); n         for (int i=0;i<lim;i++) p1[i]=mul(p1[i],p2[i]); n         ntt(p1,lim,1); n         return vector<int>(p1,p1+n+m-1); n     } n  n     vector<int> poly_inv(const vector<int> &a) n     { n         int n=a.size(); n         if (n==1) return {inv(a[0])}; n         auto b=a; b.resize((n+1)>>1); n         b=poly_inv(b); n         int m=b.size(); n         int lim=1; n         while (lim<(n<<1)) lim<<=1; n         while (lim<(m<<1)) lim<<=1; n         init(lim); n         copy_n(a.begin(),n,p1); fill(p1+n,p1+lim,0); n         copy_n(b.begin(),m,p2); fill(p2+m,p2+lim,0); n         ntt(p1,lim,1); n         ntt(p2,lim,1); n         for (int i=0;i<lim;i++) p1[i]=mul(p2[i],sub(2,mul(p1[i],p2[i]))); n         ntt(p1,lim,-1); n         return vector<int>(p1,p1+n); n     } n  n     vector<int> poly_div(const vector<int> &a,const vector<int> &b) n     { n         int n=a.size(),m=b.size(); n         if (m>n) return {0}; n         auto ar=a,br=b; n         reverse(ar.begin(),ar.end()); reverse(br.begin(),br.end()); n         ar.resize(n-m+1); br.resize(n-m+1); n         br=poly_inv(br); n         auto q=poly_mul(ar,br); q.resize(n-m+1); n         reverse(q.begin(),q.end()); n         return q; n     } n  n     vector<int> poly_mod(const vector<int> &a,const vector<int> &b) n     { n         int m=b.size(); n         auto c=poly_div(a,b); n         c=poly_mul(b,c); n         c=poly_sub(a,c); c.resize(m-1); n         return c; n     } n  n     vector<int> poly_sqrt(const vector<int> &a) n     { n         int n=a.size(); n         if (n==1) return {1}; n         auto b=a; b.resize((n+1)>>1); n         b=poly_sqrt(b); b.resize(n); n         auto c=poly_add(b,poly_mul(poly_inv(b),a)); c.resize(n); n         int i2=inv(2); for (int &i:c) i=mul(i,i2); n         return c; n     } n  n     vector<int> poly_ln(const vector<int> &a) n     { n         int n=a.size(); n         auto b=poly_mul(poly_inv(a),poly_d(a)); n         b.resize(n-1); b=poly_s(b); n         return b; n     } n  n     vector<int> poly_exp(const vector<int> &a) n     { n         int n=a.size(); n         if (n==1) return {1}; n         auto b=a; b.resize((n+1)>>1); n         b=poly_exp(b); b.resize(n); n         auto c=poly_sub(a,poly_ln(b)); c[0]=add(c[0],1); n         c=poly_mul(b,c); c.resize(n); n         return c; n     } n  n     vector<int> poly_sin(const vector<int> &a) n     { n         auto b=a; n         for (int &i:b) i=mul(i,J); n         auto c=poly_exp(b); n         c=poly_sub(c,poly_inv(c)); n         int i2=inv(2),ij=inv(J); n         for (int &i:c) i=mul(i,mul(i2,ij)); n         return c; n     } n  n     vector<int> poly_cos(const vector<int> &a) n     { n         auto b=a; n         for (int &i:b) i=mul(i,J); n         auto c=poly_exp(b); n         c=poly_add(c,poly_inv(c)); n         int i2=inv(2); n         for (int &i:c) i=mul(i,i2); n         return c; n     } n  n     vector<int> poly_asin(const vector<int> &a) n     { n         int n=a.size(); n         auto b=poly_d(a),c=poly_mul(a,a); c.resize(n); n         c=poly_inv(poly_sqrt(poly_sub({1},c))); n         b=poly_mul(b,c); b.resize(n-1); n         return poly_s(b); n     } n  n     vector<int> poly_atan(const vector<int> &a) n     { n         int n=a.size(); n         auto b=poly_d(a),c=poly_mul(a,a); c.resize(n); n         c=poly_inv(poly_add({1},c)); n         b=poly_mul(b,c); b.resize(n-1); n         return poly_s(b); n     } n } n  n const int N=200010; n  n int n,a,b; n long long inv[N]; n long long fac[N],invf[N]; n  n void linear_inv(long long n,long long p) n { n     inv[1]=1; n     for (long long i=2;i<=n;i++) inv[i]=(p-p/i)*inv[p%i]%p; n     fac[0]=1; n     for (long long i=1;i<=n;i++) fac[i]=fac[i-1]*i%p; n     invf[n]=NTT::inv(fac[n]); n     for (long long i=n-1;i>=0;i--) invf[i]=invf[i+1]*(i+1)%p; n } n  n long long C(long long n,long long m) n { n     if (m>n) return 0; n     return NTT::mul(fac[n],NTT::mul(invf[m],invf[n-m])); n } n  n int main() n { n     linear_inv(200000,NTT::P); n     scanf( %d ,&n); n     vector<int> ans={1}; n     int l=1; n     for (int i=1;i<=n;i++) n     { n         scanf( %d%d ,&a,&b); n         vector<int> f; n         int sz=min(l+l+a-b-1,a+b+1); n         for (int j=(a+b+1-sz)/2;j<=a+b-(a+b+1-sz)/2;j++) n         { n             f.push_back(C(a+b,j)); n         } n         ans=NTT::poly_mul(ans,f); n         l+=a-b; n         int now=ans.size(); n         ans.erase(ans.begin(),ans.begin()+(now-l)/2); n         ans.erase(ans.end()-(now-l)/2,ans.end()); n     } n     int sum=0; n     for (int i:ans) n     { n         sum=NTT::add(sum,i); n     } n     printf( %d ,sum); n     getchar(); getchar(); n     return 0; n }


#include <bits/stdc++.h> n using namespace std; n using LL = long long; n const int N = 100005; n LL a[N]; n int main() { n   int t; n   scanf( %d , &t); n   while (t--) { n     int n; n     LL sum = 0; n     LL x; n     scanf( %d%lld , &n, &x); n     LL q = n; n     for (int i = 1; i <= n; ++i) { n       scanf( %lld , &a[i]); n       sum += a[i]; n     } n     sort(a + 1, a + n + 1); n     bool flag = true; n     for (int i = 1; i <= n; ++i) { n       if (sum * 1.0 / q * 1.0 >= x * 1.0) { n         printf( %lld n , q); n         flag = false; n         break; n       } n       sum -= a[i], --q; n     } n     if (flag) printf( %lld n , q); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int num_q; n int flag; n long long u, v, w; n map<long long, long long> hs; n int main() { n   scanf( %d , &num_q); n   while (num_q--) { n     scanf( %d%lld%lld , &flag, &u, &v); n     if (flag == 1) { n       scanf( %lld , &w); n       while (u != v) { n         if (u > v) { n           hs[u] += w; n           u /= 2; n         } else { n           hs[v] += w; n           v /= 2; n         } n       } n     } else { n       long long ans = 0; n       while (u != v) { n         if (u > v) { n           ans += hs[u]; n           u /= 2; n         } else { n           ans += hs[v]; n           v /= 2; n         } n       } n       printf( %lld n , ans); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n struct EDGE { n   int adj, next; n } edge[810000]; n int n, m, q, top, gh[510000]; n int size[510000], dfn[510000], low[510000], cnt, ind, stop, instack[510000], n     stck[510000], belong[510000]; n void addedge(int x, int y) { n   edge[++top].adj = y; n   edge[top].next = gh[x]; n   gh[x] = top; n } n void tarjan(int x, int pre) { n   dfn[x] = low[x] = ++ind; n   instack[x] = 1; n   stck[++stop] = x; n   for (int p = gh[x]; p; p = edge[p].next) { n     if (edge[p].adj == pre) continue; n     if (!dfn[edge[p].adj]) { n       tarjan(edge[p].adj, x); n       low[x] = min(low[x], low[edge[p].adj]); n     } else if (instack[edge[p].adj]) { n       low[x] = min(low[x], dfn[edge[p].adj]); n     } n   } n   if (dfn[x] == low[x]) { n     ++cnt; n     int tmp = 0; n     size[cnt] = 0; n     while (tmp != x) { n       size[cnt]++; n       tmp = stck[stop--]; n       belong[tmp] = cnt; n       instack[tmp] = 0; n     } n   } n } n int far[510000]; n long long mul[510000]; n void init() { n   scanf( %d%d , &n, &m); n   for (int i = 1; i <= n; i++) gh[i] = 0; n   top = cnt = ind = stop = 0; n   for (int i = 1; i <= m; i++) { n     int x, y; n     scanf( %d%d , &x, &y); n     addedge(x, y); n     addedge(y, x); n   } n   for (int i = 1; i <= n; i++) dfn[i] = 0; n   for (int i = 1; i <= n; i++) n     if (dfn[i] == 0) tarjan(i, 0); n } n int v[510000]; n void prepare() { n   for (int i = 1; i <= n; i++) v[i] = 0; n   int tail = 1; n   v[belong[1]]++; n   for (int i = 1; i <= n; i++) { n     while (tail < n && (v[belong[tail + 1]] + 1 < size[belong[tail + 1]] || n                         size[belong[tail + 1]] == 1)) { n       v[belong[++tail]]++; n     } n     far[i] = tail; n     mul[i] = tail - i + 1; n     mul[i] += mul[i - 1]; n     v[belong[i]]--; n   } n } n int main() { n   init(); n   prepare(); n   scanf( %d , &q); n   for (int i = 1; i <= q; i++) { n     int l, r; n     scanf( %d%d , &l, &r); n     int left = l; n     int right = r; n     if (far[l] > r) n       left = left - 1; n     else n       while (left < right) { n         int mid = (left + right) / 2 + 1; n         if (far[mid] <= r) n           left = mid; n         else n           right = mid - 1; n       } n     long long ans = mul[left] - mul[l - 1]; n     ans += (long long)(r - left + 1) * (r - left) / 2; n     printf( %I64d n , ans); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int squ(vector<long long> &v) { n   for (int i = 0; i + 3 < v.size(); i++) { n     if (v[i] == v[i + 1] && v[i] == v[i + 2] && v[i] == v[i + 3]) return v[i]; n   } n   return -1; n } n vector<long long> pa(vector<long long> &v) { n   vector<long long> q; n   for (int i = 0; i + 1 < v.size(); i++) { n     if (i == 0 || v[i - 1] != v[i]) { n       if (v[i] == v[i + 1]) q.push_back(v[i]); n     } n   } n   return q; n } n bool checkv(pair<long long, long long> a, pair<long long, long long> b) { n   if ((a.first + a.second) * (a.first + a.second) * b.first * b.second > n       (b.first + b.second) * (b.first + b.second) * a.first * a.second) n     return true; n   return false; n } n int main() { n   ios_base::sync_with_stdio(0); n   int T; n   cin >> T; n   while (T--) { n     long long n; n     cin >> n; n     vector<long long> v; n     for (int i = 0; i < n; i++) { n       long long a; n       cin >> a; n       v.push_back(a); n     } n     sort(v.begin(), v.end()); n     long long res = squ(v); n     if (res > 0) { n       for (int i = 0; i < 4; i++) cout << res <<    ; n       cout <<   n ; n       continue; n     } n     vector<long long> u = pa(v); n     pair<long long, long long> cur = {u[0], u[1]}; n     for (int i = 0; i + 1 < u.size(); i++) { n       if (checkv(cur, {u[i], u[i + 1]})) { n         cur = {u[i], u[i + 1]}; n       } n     } n     cout << cur.first <<     << cur.first <<     << cur.second <<     n          << cur.second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MN = 100011; n const int inf = 1e9 + 7; n int n, m; n char c[1000][1000]; n int main() { n   ios ::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> m; n   for (int i = (1), _b = (n); i <= _b; ++i) n     for (int j = (1), _b = (m); j <= _b; ++j) cin >> c[i][j]; n   long long res = 0, ok = 0; n   for (int i = (1), _b = (m); i <= _b; ++i) { n     if (c[n][i] ==  B ) { n       if (!ok) res++; n       ok = 1; n     } else n       ok = 0; n   } n   cout << res; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e5 + 10; n int x[N], y[N]; n vector<int> xx; n vector<int> yy; n int n; n bool sameline(int tar, int p1, int p2) { n   return 1LL * (y[tar] - yy[p1]) * (x[tar] - xx[p2]) == n          1LL * (y[tar] - yy[p2]) * (x[tar] - xx[p1]); n } n bool check(int p1, int p2, int p3, int p4) { n   for (int i = 1; i <= n; i++) { n     if (!sameline(i, p1, p2) && !sameline(i, p3, p4)) return false; n   } n   return true; n } n int main() { n   int flag; n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) scanf( %d %d , &x[i], &y[i]); n   if (n <= 4) { n     printf( YES n ); n     return 0; n   } n   xx.push_back(x[1]); n   yy.push_back(y[1]); n   xx.push_back(x[2]); n   yy.push_back(y[2]); n   for (int i = 3; i <= n; i++) { n     flag = 1; n     for (int j = 0; j < xx.size() - 1; j++) { n       for (int k = j + 1; k < xx.size(); k++) { n         if (1LL * (y[i] - yy[j]) * (x[i] - xx[k]) == n             1LL * (y[i] - yy[k]) * (x[i] - xx[j])) { n           flag = 0; n           break; n         } n       } n     } n     if (flag) { n       xx.push_back(x[i]); n       yy.push_back(y[i]); n     } n     if (xx.size() == 4) break; n   } n   if (xx.size() == 2) n     printf( YES n ); n   else if (xx.size() == 3) { n     if (check(0, 1, 1, 2) || check(0, 1, 0, 2) || check(0, 2, 1, 2)) n       printf( YES n ); n     else n       printf( NO n ); n   } else { n     if (check(0, 1, 2, 3) || check(0, 2, 1, 3) || check(0, 3, 1, 2)) n       printf( YES n ); n     else n       printf( NO n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, k; n   cin >> n >> k; n   vector<int> a(100, 0); n   int b; n   for (int i = 0; i < n; i++) { n     cin >> b; n     b--; n     a[b]++; n   } n   sort(a.begin(), a.end()); n   int s = 0; n   s = a[99] / k; n   if (a[99] % k > 0) { n     s++; n   } n   int l = 0; n   for (int i = 99; i >= 0; i--) { n     if (a[i] > 0) { n       l++; n     } n   } n   cout << s * k * l - n; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[65]; n long long level(long long x) { n   int sum = 0; n   for (; x != 1; x /= 2) { n     sum++; n   } n   return sum; n } n void write(long long a) { n   if (a == 0) n     putchar( 0 ); n   else { n     static char c[20]; n     static int c0; n     c0 = 0; n     while (a) c[c0++] = a % 10 +  0 , a /= 10; n     while (c0--) putchar(c[c0]); n   } n } n void get_val(long long x, int l) { n   write((x - a[l]) % (1ll << l) + (1ll << l)); n   putchar(   ); n   if (x == 1) return; n   get_val(x / 2, l - 1); n } n int main() { n   ios::sync_with_stdio(false); n   int q; n   cin >> q; n   int i; n   for (i = 0; i < q; i++) { n     int t; n     cin >> t; n     if (t == 1) { n       long long x, k; n       cin >> x >> k; n       x = level(x); n       a[x] += k; n       a[x] %= (1ll << x); n       a[x] += (1ll << x); n       a[x] %= (1ll << x); n     } else if (t == 2) { n       long long x, k; n       cin >> x >> k; n       x = level(x); n       k %= (1ll << x); n       for (; x <= 60; x++) { n         a[x] += k; n         a[x] %= (1ll << x); n         a[x] += (1ll << x); n         a[x] %= (1ll << x); n         k <<= 1; n       } n     } else { n       long long x; n       cin >> x; n       int k = level(x); n       x += a[k]; n       x %= (1ll << k); n       x += (1ll << k); n       get_val(x, level(x)); n       putchar(  n ); n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n T fact(T n) { n   return (n == 1 || n == 0) ? 1 : n * fact(n - 1); n } n bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { n   return (a.second < b.second); n } n int subarraysum(int arr[], int n, int sum) { n   int curr_sum = arr[0], start = 0, i; n   for (i = 1; i <= n; i++) { n     while (curr_sum > sum && start < i - 1) { n       curr_sum = curr_sum - arr[start]; n       start++; n     } n     if (curr_sum == sum) { n       if (start != i - 1) return 1; n     } n     if (i < n) curr_sum = curr_sum + arr[i]; n   } n   return 0; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     int a[n]; n     for (int i = 0; i < n; i++) cin >> a[i]; n     int c = 0; n     for (int i = 0; i < n; i++) { n       int x = a[i]; n       if (subarraysum(a, n, x)) c++; n     } n     cout << c << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 132674 + 10; n int n, len[2]; n int R[2][2 * N]; n vector<pair<int, int> > open[2 * N], clos[2 * N]; n struct Cord { n   int x1, asdioayseiowqe12, x2, y2; n   Cord() {} n   Cord(int x1, int asdioayseiowqe12, int x2, int y2) n       : x1(x1), asdioayseiowqe12(asdioayseiowqe12), x2(x2), y2(y2) {} n } C[N]; n struct SegmentTree { n   int st[4 * 2 * N], lp[4 * 2 * N]; n   void Propagate(int k) { n     if (lp[k] == 0) return; n     int v = lp[k]; n     lp[k] = 0; n     st[k << 1] += v; n     st[k << 1 | 1] += v; n     lp[k << 1] += v; n     lp[k << 1 | 1] += v; n   } n   void Update(int k, int l, int r, int i, int j, int v) { n     if (l > j || r < i) return; n     if (i <= l && r <= j) { n       st[k] += v; n       lp[k] += v; n       return; n     } n     Propagate(k); n     Update(k << 1, l, ((l + r) >> 1), i, j, v); n     Update(k << 1 | 1, ((l + r) >> 1) + 1, r, i, j, v); n     st[k] = max(st[k << 1], st[k << 1 | 1]); n   } n   int Find(int k, int l, int r) { n     if (l == r) return l; n     Propagate(k); n     if (st[k << 1] >= n - 1) n       return Find(k << 1, l, ((l + r) >> 1)); n     else n       return Find(k << 1 | 1, ((l + r) >> 1) + 1, r); n   } n } ST; n int Pos(int i, int v) { n   return lower_bound(R[i] + 1, R[i] + len[i] + 1, v) - R[i]; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     int x1, asdioayseiowqe12, x2, y2; n     scanf( %d%d%d%d , &x1, &asdioayseiowqe12, &x2, &y2); n     C[i] = Cord(x1, asdioayseiowqe12, x2, y2); n     R[0][++len[0]] = x1; n     R[0][++len[0]] = x2; n     R[1][++len[1]] = asdioayseiowqe12; n     R[1][++len[1]] = y2; n   } n   for (int i = 0; i <= 1; ++i) { n     sort(R[i] + 1, R[i] + len[i] + 1); n     len[i] = unique(R[i] + 1, R[i] + len[i] + 1) - (R[i] + 1); n   } n   for (int i = 1; i <= n; ++i) { n     int x1 = C[i].x1, asdioayseiowqe12 = C[i].asdioayseiowqe12, x2 = C[i].x2, n         y2 = C[i].y2; n     int x = Pos(1, asdioayseiowqe12), y = Pos(1, y2); n     open[Pos(0, x1)].push_back(pair<int, int>(x, y)); n     clos[Pos(0, x2)].push_back(pair<int, int>(x, y)); n   } n   for (int i = 1; i <= len[0]; ++i) { n     for (int k = 0; k < open[i].size(); ++k) { n       int x = open[i][k].first, y = open[i][k].second; n       ST.Update(1, 1, len[1], x, y, +1); n     } n     if (ST.st[1] >= n - 1) { n       int j = ST.Find(1, 1, len[1]); n       printf( %d %d n , R[0][i], R[1][j]); n       return 0; n     } n     for (int k = 0; k < clos[i].size(); ++k) { n       int x = clos[i][k].first, y = clos[i][k].second; n       ST.Update(1, 1, len[1], x, y, -1); n     } n   } n   assert(false); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct hh { n   int num; n   int c; n }; n bool cmp(const hh p, const hh q) { return p.c < q.c; } n int main() { n   int n, k; n   scanf( %d%d , &n, &k); n   hh a[1010], b[1010]; n   int i; n   int p = 0, q = 0; n   vector<int> ss[1010]; n   for (i = 0; i < n; ++i) { n     int x, y; n     scanf( %d%d , &x, &y); n     if (y == 1) { n       a[p].num = i + 1; n       a[p++].c = x; n     } else { n       b[q].num = i + 1; n       b[q++].c = x; n     } n   } n   sort(a, a + p, cmp); n   sort(b, b + q, cmp); n   double ans = 0; n   if (p >= k) { n     for (i = 1; i < k; ++i) { n       ans += a[p - i].c * 1.0 / 2; n       ss[i].push_back(a[p - i].num); n     } n     if (q > 0) { n       if (a[0].c > b[0].c) { n         ans += b[0].c * 1.0 / 2; n         ans += a[0].c; n       } else { n         ans += a[0].c * 1.0 / 2; n         ans += b[0].c; n       } n       ss[k].push_back(b[0].num); n     } else n       ans += a[0].c * 1.0 / 2; n     ss[k].push_back(a[0].num); n     for (i = 1; i < q; ++i) { n       ss[k].push_back(b[i].num); n       ans += b[i].c; n     } n     for (i = p - k; i > 0; --i) { n       ss[k].push_back(a[i].num); n       ans += a[i].c; n     } n   } else { n     for (i = 1; i <= p; ++i) { n       ss[i].push_back(a[i - 1].num); n       ans += a[i - 1].c * 1.0 / 2; n     } n     int e = 0; n     for (i = p + 1; i <= k; ++i) { n       ss[i].push_back(b[e].num); n       ans += b[e++].c; n     } n     for (i = e; i < q; ++i) { n       ss[k].push_back(b[i].num); n       ans += b[i].c; n     } n   } n   printf( %.1f n , ans); n   int j; n   for (i = 1; i <= k; ++i) { n     printf( %d , ss[i].size()); n     for (j = 0; j < ss[i].size(); ++j) printf(  %d , ss[i][j]); n     printf(  n ); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 1e6 + 2; n long long arr[N]; n long long pos[N]; n long long chance[N]; n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   long long t, i, j; n   long long n; n   cin >> n; n   for (i = 1; i <= n; i++) { n     cin >> arr[i]; n     pos[arr[i]] = i; n   } n   long long high = 0, low = 0; n   long long ans = 0, ind = 0; n   for (i = 1; i <= n; i++) { n     if (arr[i] == i) { n       high++; n     } else if (arr[i] < i) { n       high++; n       ans += i - arr[i]; n       chance[arr[i] - i + n]++; n     } else { n       low++; n       ans += arr[i] - i; n       chance[arr[i] - i]++; n     } n   } n   long long prev_ans = ans; n   for (i = 1; i < n; i++) { n     long long lst_elem = arr[n - (i - 1)]; n     long long cur_ans = n         prev_ans + high - 1 - low - abs(n - lst_elem) + abs(1 - lst_elem); n     if (cur_ans < ans) { n       ans = cur_ans; n       ind = i; n     } n     prev_ans = cur_ans; n     high--; n     high += chance[i]; n     low -= chance[i]; n     low++; n   } n   cout << ans <<     << ind; n } n


#include <bits/stdc++.h> n using namespace std; n char a[502][502]; n int n, m; n int ds[502][502][251]; n int dfs(int i, int j, int x, int y, int st) { n   if (i > x || j > y) { n     return 0; n   } n   if (a[i][j] ==  *  || a[x][y] ==  *  || a[i][j] != a[x][y]) { n     return 0; n   } n   if (ds[i][x][st] != -1) { n     return ds[i][x][st]; n   } n   if ((i == x && j == y - 1) || (i == x - 1 && j == y)) { n     return 1; n   } n   if (i == x && j == y) { n     return 1; n   } n   long long int ans = 0; n   ans += dfs(i + 1, j, x - 1, y, st + 1); n   ans %= 1000000007; n   ans += dfs(i, j + 1, x - 1, y, st + 1); n   ans %= 1000000007; n   ans += dfs(i + 1, j, x, y - 1, st + 1); n   ans %= 1000000007; n   ans += dfs(i, j + 1, x, y - 1, st + 1); n   ans %= 1000000007; n   ds[i][x][st] = ans; n   return ans; n } n int main() { n   cin >> n >> m; n   for (int i = 0; i < 502; i++) { n     for (int j = 0; j < 502; j++) { n       a[i][j] =  * ; n       for (int k = 0; k < 251; k++) { n         ds[i][j][k] = -1; n       } n     } n   } n   for (int i = 1; i <= n; i++) { n     for (int j = 1; j <= m; j++) { n       cin >> a[i][j]; n     } n   } n   int ans = dfs(1, 1, n, m, 0); n   ans %= 1000000007; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   string vowel =  AEIOUYaeiouy ; n   string in; n   char s; n   bool x = false; n   getline(cin, in); n   int h = in.size(); n   for (int k = (h - 1); k >= 0; k--) { n     if (isalpha(in[k])) { n       s = in[k]; n       break; n     } n   } n   for (int i = 0; i < vowel.size(); i++) { n     if (s == vowel[i]) { n       x = true; n       break; n     } n   } n   if (x == true) n     cout <<  YES ; n   else n     cout <<  NO ; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /STACK:256000000 ) n using namespace std; n const long double eps = 1e-7; n const long long inf = numeric_limits<long long>::max() / 2 - 1; n const long long mod = 1000000007; n const long long maxn = 800007; n struct plan { n   long long l1, r1; n   long long l2, r2; n   long long w; n }; n struct node { n   set<long long> plans; n   set<long long> plansin; n }; n plan plans[maxn]; n bool planused[maxn]; n vector<pair<long long, long long>> mineq[maxn]; n node tree[maxn]; n long long n, q, s; n set<pair<long long, long long>> st; n void add(long long v, long long vl, long long vr, long long pv) { n   if (vl >= plans[pv].r1 || vr <= plans[pv].l1) return; n   tree[v].plans.insert(pv); n   if (vl >= plans[pv].l1 && vr <= plans[pv].r1) { n     tree[v].plansin.insert(pv); n     return; n   } n   long long mid = (vl + vr) >> 1; n   add(2 * v + 1, vl, mid, pv); n   add(2 * v + 2, mid, vr, pv); n } n set<long long> mset; n void makeset(long long v, long long vl, long long vr, long long l, n              long long r) { n   if (vl >= r || vr <= l) return; n   if (vl >= l && vr <= r) { n     for (auto i : tree[v].plans) { n       if (!planused[i]) { n         mset.insert(i); n         planused[i] = true; n       } n     } n     tree[v].plans.clear(); n     return; n   } n   for (auto i : tree[v].plansin) { n     if (!planused[i]) { n       mset.insert(i); n       planused[i] = true; n     } n   } n   tree[v].plansin.clear(); n   long long mid = (vl + vr) >> 1; n   makeset(2 * v + 1, vl, mid, l, r); n   makeset(2 * v + 2, mid, vr, l, r); n } n inline int run() { n   cin >> n >> q >> s; n   s--; n   mineq[s].push_back({1, 0}); n   mineq[s + 1].push_back({-1, 0}); n   for (int t = 0; t < q; ++t) { n     long long type; n     cin >> type; n     if (type == 1) { n       long long v, u, w; n       cin >> v >> u >> w; n       v--; n       u--; n       plans[t].l1 = v; n       plans[t].r1 = v + 1; n       plans[t].l2 = u; n       plans[t].r2 = u + 1; n       plans[t].w = w; n     } else { n       long long v, l, r, w; n       cin >> v >> l >> r >> w; n       l--; n       v--; n       if (type == 2) { n         plans[t].l1 = v; n         plans[t].r1 = v + 1; n         plans[t].l2 = l; n         plans[t].r2 = r; n         plans[t].w = w; n       } else { n         plans[t].l1 = l; n         plans[t].r1 = r; n         plans[t].l2 = v; n         plans[t].r2 = v + 1; n         plans[t].w = w; n       } n     } n     if (plans[t].l1 <= s && plans[t].r1 > s) { n       planused[t] = true; n       st.insert({plans[t].w, t}); n     } else { n       add(0, 0, n, t); n     } n   } n   while (!st.empty()) { n     long long len = st.begin()->first; n     long long v = st.begin()->second; n     st.erase(st.begin()); n     mset.clear(); n     mineq[plans[v].l2].push_back({1, len}); n     mineq[plans[v].r2].push_back({-1, len}); n     makeset(0, 0, n, plans[v].l2, plans[v].r2); n     for (auto j : mset) { n       st.insert({len + plans[j].w, j}); n     } n   } n   multiset<long long> minset; n   for (int i = 0; i < n; ++i) { n     for (auto j : mineq[i]) { n       if (j.first == 1) { n         minset.insert(j.second); n       } else { n         minset.erase(minset.find(j.second)); n       } n     } n     long long minval = -1; n     if (!minset.empty()) minval = *minset.begin(); n     cout << minval <<    ; n   } n   return 0; n } n signed main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   if (strlen(  ) > 0) { n     freopen( n            n          .in , n          r , stdin); n     freopen( n            n          .out , n          w , stdout); n   } n   run(); n   cerr << clock() * 1000 / CLOCKS_PER_SEC; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double EPS = 1e-5; n const int MOD = int(1e9) + 7, MX = int(5e4) + 1; n long long memo[100][3001][2]; n int n, l; n pair<int, int> A[101]; n long long dp(int i, int len, int f) { n   if (len == l) return 1; n   if (i == n || len > l) return 0; n   long long &ret = memo[i][len][f]; n   if (ret + 1) return ret; n   ret = 0; n   int L = A[i].first, W = A[i].second; n   if (f) swap(L, W); n   for (int j = 0; j < n; j++) { n     if (j != i) { n       if (A[j].first == W) n         ret += dp(j, len + A[j].first, 0); n       else if (A[j].second == W) n         ret += dp(j, len + A[j].second, 1); n       if (ret >= MOD) ret -= MOD; n     } n   } n   return ret; n } n long long sol() { n   memset(memo, -1, sizeof memo); n   long long res = 0; n   for (int i = 0; i < n; i++) { n     res += dp(i, A[i].first, 0) + n            (A[i].first - A[i].second ? dp(i, A[i].second, 1) : 0); n     if (res >= MOD) res -= MOD; n   } n   return res; n } n void solve() { n   scanf( %d %d , &n, &l); n   for (int i = 0; i < n; i++) { n     scanf( %d %d , &A[i].first, &A[i].second); n   } n   printf( %lld n , sol() % MOD); n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n const int nmax = 110; n char s[nmax]; n bool pal(int n) { n   int i; n   for (i = 0; i <= (n - 1) / 2; i++) n     if (abs(s[i] - s[n - i - 1]) > 2 || abs(s[i] - s[n - i - 1]) == 1) return 0; n   return 1; n } n int main() { n   int t, n, i; n   cin >> t; n   for (i = 1; i <= t; i++) { n     cin >> n >> s; n     if (pal(n) == 1) 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 vector<vector<int> > g; n int n; n vector<bool> b; n vector<int> v; n stack<int> st; n bool fi; n vector<bool> dins; n void dfs(int u) { n   if (fi) return; n   if (b[u]) { n     if (dins[u]) { n       fi = true; n       v.push_back(u); n       while (!st.empty() and st.top() != u) { n         v.push_back(st.top()); n         st.pop(); n       } n       v.push_back(u); n     } else n       return; n   } n   b[u] = true; n   st.push(u); n   dins[u] = true; n   for (int i = 0; i < g[u].size(); ++i) { n     dfs(g[u][i]); n   } n   if (fi) return; n   st.pop(); n   dins[u] = false; n } n bool check(int x, int y, vector<int> d) { n   --d[y]; n   queue<int> q; n   for (int i = 0; i < n; ++i) { n     if (d[i] == 0) q.push(i); n   } n   while (!q.empty()) { n     int u = q.front(); n     q.pop(); n     for (int i = 0; i < g[u].size(); ++i) { n       if (u != x or g[u][i] != y) { n         --d[g[u][i]]; n         if (d[g[u][i]] == 0) q.push(g[u][i]); n       } n     } n   } n   for (int i = 0; i < n; ++i) { n     if (d[i]) { n       return false; n     } n   } n   return true; n } n int main() { n   int m; n   cin >> n >> m; n   g = vector<vector<int> >(n); n   vector<int> d(n, 0); n   for (int i = 0; i < m; ++i) { n     int x, y; n     cin >> x >> y; n     --x; n     --y; n     ++d[y]; n     g[x].push_back(y); n   } n   b = vector<bool>(n, false); n   dins = vector<bool>(n, false); n   fi = false; n   for (int i = 0; i < n; ++i) { n     if (!b[i] and !fi) dfs(i); n   } n   for (int i = 1; i < v.size(); ++i) { n     if (check(v[i], v[i - 1], d)) { n       cout <<  YES  << endl; n       return 0; n     } n   } n   if (v.empty()) n     cout <<  YES  << endl; n   else n     cout <<  NO  << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   int* degree = new int[100005]; n   for (int i = 1; i <= n; i++) { n     degree[i] = 0; n   } n   while (m--) { n     int a, b; n     cin >> a >> b; n     degree[a]++; n     degree[b]++; n   } n   int deg1 = 0, deg2 = 0, degG = 0; n   for (int i = 1; i <= n; i++) { n     if (degree[i] == 0) { n       continue; n     } else if (degree[i] == 1) { n       deg1++; n     } else if (degree[i] == 2) { n       deg2++; n     } else if (degree[i] == n - 1) { n       degG++; n     } n   } n   if (deg1 == n - 1 && deg2 == 0 && degG == 1) { n     cout <<  star topology ; n     return 0; n   } else if (deg1 == 2 && deg2 == n - 2 && degG == 0) { n     cout <<  bus topology ; n     return 0; n   } else if (deg1 == 0 && deg2 == n && degG == 0) { n     cout <<  ring topology ; n     return 0; n   } else { n     cout <<  unknown topology ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long N, M, x, y, K, ml, T, ans; n long long f[1000000], s[1000000]; n const long long md = 1e9 + 7; n const long long MAXN = 1e5 + 2; n string in; n int main() { n   cin >> N >> K; n   f[0] = 1; n   for (int i = 1; i <= MAXN; i++) { n     f[i] = f[i - 1]; n     if (i >= K) f[i] = (f[i] % md + f[i - K] % md) % md; n   } n   s[0] = 0; n   for (int i = 1; i <= MAXN; i++) s[i] = (s[i - 1] % md + f[i] % md) % md; n   for (int i = 0; i < N; i++) { n     cin >> x >> y; n     ans = (s[y] - s[x - 1] + md) % md; n     cout << ans << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> vec[201], v[201]; n int afis[201], scor[201], n, test[201]; n bool viz[201]; n void elim(int val) { n   for (int i = 0; i < v[val].size(); i++) scor[v[val][i]]--; n } n bool incearca(int start) { n   int i, j; n   for (i = 1; i <= n; i++) { n     scor[i] = vec[i].size(); n     viz[i] = 0; n   } n   afis[1] = vec[start][0]; n   elim(vec[start][0]); n   afis[2] = vec[start][1]; n   elim(vec[start][1]); n   viz[vec[start][0]] = viz[vec[start][1]] = 1; n   for (int i = 3; i <= n; i++) { n     int cnt = 0; n     for (j = 1; j < n; j++) n       if (scor[j] == 1) cnt++; n     if (cnt >= 2 || cnt == 0) return 0; n     for (j = 1; j < n; j++) n       if (scor[j] == 1) { n         for (int k = 0; k < vec[j].size(); k++) n           if (!viz[vec[j][k]]) { n             viz[vec[j][k]] = 1; n             elim(vec[j][k]); n             afis[i] = vec[j][k]; n             break; n           } n         cnt = 0; n         for (int k = i; k >= i - vec[j].size() + 1; k--) test[++cnt] = afis[k]; n         sort(test + 1, test + cnt + 1); n         for (int a = 1, b = 0; b < vec[j].size(); a++, b++) n           if (test[a] != vec[j][b]) return 0; n         break; n       } n     if (j == n) return 0; n   } n   return 1; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n   int t, i, k, j, a; n   cin >> t; n   while (t--) { n     cin >> n; n     for (i = 1; i <= n; i++) { n       vec[i].clear(); n       v[i].clear(); n     } n     for (i = 1; i < n; i++) { n       cin >> k; n       for (j = 1; j <= k; j++) { n         cin >> a; n         v[a].push_back(i); n         vec[i].push_back(a); n       } n     } n     for (i = 1; i < n; i++) n       if (vec[i].size() == 2) { n         if (incearca(i)) break; n         swap(vec[i][0], vec[i][1]); n         if (incearca(i)) break; n         swap(vec[i][0], vec[i][1]); n       } n     for (i = 1; i <= n; i++) cout << afis[i] <<    ; n     cout <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxN = 1e6 + 5; n int n, x, y, ile[4 * maxN]; n long long pref[4 * maxN], mini = 1e18; n bool czy = 0; n long long spr(long long x, long long y) { n   long long t; n   if (__builtin_smulll_overflow(x, y, &t)) czy = 1; n   return x * y; n } n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cin >> n >> x >> y; n   for (int i = 0, a; i < n; i++) cin >> a, ile[a]++, pref[a] += a; n   for (int i = 1; i < 4 * maxN; i++) n     ile[i] += ile[i - 1], pref[i] += pref[i - 1]; n   for (int i = 2; i < maxN; i++) { n     long long temp = 0; n     int f = max<double>(0, ceil(i - double(x) / y) - 1); n     czy = 0; n     for (int j = 0; i * j < maxN && !czy; j++) { n       temp += (long long)(ile[i * j + f] - ile[i * j]) * x; n       if (temp < 0) czy = 1; n       temp += spr(spr(i * j + i, ile[i * j + i] - ile[i * j + f]) - n                       (pref[i * j + i] - pref[i * j + f]), n                   y); n       if (temp < 0) czy = 1; n     } n     if (!czy) { n       mini = min(mini, temp); n     } n   } n   cout << mini << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long power(long long x, long long y) { n   long long res = 1; n   while (y--) { n     res *= x; n   } n   return res; n } n void fast() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   cout.tie(NULL); n } n bool sortbysec(const pair<long long, long long>& a, n                const pair<long long, long long>& b) { n   return (a.second < b.second); n } n void solve() { n   long long n, a, b; n   cin >> n >> a >> b; n   vector<pair<long long, long long> > v; n   for (long long i = 0; i < n; i++) { n     long long x, y; n     cin >> x >> y; n     v.push_back({x, y}); n   } n   long long g = 0; n   for (long long i = 0; i < n - 1; i++) { n     for (long long j = i + 1; j < n; j++) { n       long long p = v[i].first, q = v[i].second, r = v[j].first, n                 s = v[j].second; n       if ((p + r <= a && max(q, s) <= b) || (q + s <= b && max(p, r) <= a) || n           (p + r <= b && max(q, s) <= a) || (q + s <= a && max(p, r) <= b)) { n         g = max(g, ((p * q) + (r * s))); n       } else { n         long long t = p; n         p = q; n         q = t; n         if ((p + r <= a && max(q, s) <= b) || (q + s <= b && max(p, r) <= a) || n             (p + r <= b && max(q, s) <= a) || (q + s <= a && max(p, r) <= b)) { n           g = max(g, ((p * q) + (r * s))); n         } n       } n     } n   } n   cout << g; n } n int main() { n   fast(); n   long long t; n   t = 1; n   while (t--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int LIMIT = 20; n int n, dep[300001]; n vector<int> adj[300001]; n int dp[300001][LIMIT], dp2[300001][LIMIT]; n void dfs(int x, int pre) { n   vector<int> child; n   for (auto y : adj[x]) { n     if (y != pre) { n       dfs(y, x); n       child.push_back(y); n       dep[x] = max(dep[x], dep[y] + 1); n     } n   } n   dp[x][1] = dp2[x][1] = n; n   for (int i = 2; i < LIMIT; ++i) { n     int lo = 0, hi = n; n     while (lo < hi) { n       int md = lo + hi + 1 >> 1, tot = 0; n       for (auto y : child) { n         if (dp[y][i - 1] >= md) { n           ++tot; n         } n       } n       (tot >= md) ? lo = md : hi = md - 1; n     } n     dp[x][i] = dp2[x][i] = lo + hi >> 1; n     for (auto y : child) { n       dp2[x][i] = max(dp2[x][i], dp2[y][i]); n     } n   } n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i < n; ++i) { n     int x, y; n     scanf( %d%d , &x, &y), --x, --y; n     adj[x].push_back(y); n     adj[y].push_back(x); n   } n   dfs(0, -1); n   long long ans = 0LL; n   for (int i = 0; i < n; ++i) { n     ans += dep[i]; n   } n   for (int u = 0; u < n; ++u) { n     for (int i = 1; i < LIMIT; ++i) { n       int lo = 2, hi = dp2[u][i]; n       if (i + 1 < LIMIT) { n         lo = max(lo, dp2[u][i + 1] + 1); n       } n       if (lo <= hi) { n         ans += (long long)(hi - lo + 1) * (i - 1); n       } n     } n   } n   ans += (long long)n * n; n   cout << ans << endl; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, a; n   cin >> n; n   long long A[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a; n     A[i] = a; n   } n   long long sum = 0, odd = 0, small = 1000000009; n   for (long long i = 0; i < n; i++) { n     if (A[i] % 2 != 0) { n       odd++; n       if (A[i] < small) small = A[i]; n     } n     sum = sum + A[i]; n   } n   if (odd % 2 != 0) sum = sum - small; n   cout << sum; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 2005500; n struct state { n   int len, link; n   map<char, int> next; n }; n state st[N]; n int sz, last; n void sa_init() { n   last = st[0].len = 0; n   sz = 1; n   st[0].link = -1; n } n void sa_extend(char c) { n   int k = sz++, p; n   st[k].len = st[last].len + 1; n   for (p = last; p != -1 && !st[p].next.count(c); p = st[p].link) n     st[p].next[c] = k; n   if (p == -1) n     st[k].link = 0; n   else { n     int q = st[p].next[c]; n     if (st[p].len + 1 == st[q].len) n       st[k].link = q; n     else { n       int w = sz++; n       st[w].len = st[p].len + 1; n       st[w].next = st[q].next; n       st[w].link = st[q].link; n       for (; p != -1 && st[p].next[c] == q; p = st[p].link) st[p].next[c] = w; n       st[q].link = st[k].link = w; n     } n   } n   last = k; n } n string s, tt; n long long dp[N]; n bool t[N]; n int vis[N], q; n long long f(int i) { n   if (dp[i] != -1) return dp[i]; n   long long &r = dp[i] = t[i]; n   for (auto p : st[i].next) r += f(p.second); n   return r; n } n long long go(int i, int p, int len) { n   long long r = 0; n   if (len == ((int)(tt).size()) / 2) { n     if (vis[i] == q) return 0; n     vis[i] = q; n     r += dp[i]; n     if (st[st[i].link].len + 1 == len) { n       r += go(st[i].link, p + 1, len - 1); n     } else { n       r += go(i, p + 1, len - 1); n     } n   } else { n     if (p >= ((int)(tt).size()) / 2) return 0; n     char c = tt[p + len]; n     if (st[i].next.count(c)) { n       r += go(st[i].next[c], p, len + 1); n     } else { n       if (i && st[st[i].link].len + 1 == len) { n         r += go(st[i].link, p + 1, len - 1); n       } else { n         r += go(i, p + 1, len - 1); n       } n     } n   } n   return r; n } n void solve() { n   q++; n   cin >> tt; n   tt += tt; n   cout << go(0, 0, 0) <<   n ; n } n int main() { n   cin >> s; n   sa_init(); n   memset(dp, -1, sizeof dp); n   for (int i = 0, qwerty = ((int)(s).size()); i < qwerty; i++) sa_extend(s[i]); n   int x = last; n   while (x) { n     t[x] = 1; n     x = st[x].link; n   } n   f(0); n   int tn; n   cin >> tn; n   for (int i = 0, qwerty = tn; i < qwerty; i++) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n using ll = long long int; n using vi = vector<ll>; n using ii = pair<ll, ll>; n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   int mini = (int)1e9; n   vi v; n   for (int i = 0; i < n; i++) { n     int aux; n     cin >> aux; n     v.push_back(aux); n     if (aux < mini) mini = aux; n   } n   int idx = mini % n; n   for (int i = 0; i < n; i++) n     if (v[idx % n] - mini - i > 0) n       idx++; n     else n       break; n   cout << (idx % n) + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n const long long inf = 10000005; n int nsize, root, n, tot, nedge, hed[maxn], father[maxn], maxson[maxn], sz[maxn], n     vis[maxn]; n long long M, pw[maxn]; n map<int, int> up[maxn], cst[maxn]; n vector<int> dist[maxn], down[maxn]; n struct Edge { n   int todot, nex; n   long long cost; n } edge[maxn << 1]; n inline int read() { n   int x = 0; n   int w = 0; n   char ch = 0; n   while (ch <  0  || ch >  9 ) w |= ch ==  - , ch = getchar(); n   while (ch >=  0  && ch <=  9 ) n     x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); n   return w ? -x : x; n } n void addedge(int a, int b, long long c) { n   edge[nedge].todot = b; n   edge[nedge].nex = hed[a]; n   edge[nedge].cost = c; n   hed[a] = nedge++; n } n void getsize(int k, int fa) { n   ++nsize; n   for (int i = hed[k]; i != -1; i = edge[i].nex) { n     int v = edge[i].todot; n     if ((v == fa) || (vis[v])) continue; n     getsize(v, k); n   } n } n void getroot(int k, int fa) { n   maxson[k] = 0; n   sz[k] = 1; n   for (int i = hed[k]; i != -1; i = edge[i].nex) { n     int v = edge[i].todot; n     if ((v == fa) || (vis[v])) continue; n     getroot(v, k); n     sz[k] += sz[v]; n     maxson[k] = max(maxson[k], sz[v]); n   } n   maxson[k] = max(maxson[k], nsize - sz[k]); n   if (maxson[k] < maxson[root]) root = k; n } n void ex_gcd(long long a, long long b, long long c, long long &x, long long &y) { n   if (!b) { n     x = c / a; n     y = 0; n     return; n   } n   if (!(a % b)) { n     x = 0; n     y = c / b; n     return; n   } n   ex_gcd(b, a % b, c, y, x); n   y -= (a / b) * x; n } n void Merge(int k, int fa, int gf, int f, int dis, long long nowdown) { n   for (int i = hed[k]; i != -1; i = edge[i].nex) { n     int v = edge[i].todot; n     if ((vis[v]) || (v == fa)) continue; n     ++tot; n     father[tot - 1] = f ? f : v; n     dist[gf].push_back(dis + 1); n     down[gf].push_back((nowdown * 10 + edge[i].cost) % M); n     if (!f) n       Merge(v, k, gf, v, dis + 1, (nowdown * 10 + edge[i].cost) % M); n     else n       Merge(v, k, gf, f, dis + 1, (nowdown * 10 + edge[i].cost) % M); n   } n } n void getup(int k, int fa, int gf, long long nowup, int dis) { n   for (int i = hed[k]; i != -1; i = edge[i].nex) { n     int v = edge[i].todot; n     if ((vis[v]) || (v == fa)) continue; n     up[gf][(pw[dis] * edge[i].cost + nowup) % M]++; n     getup(v, k, gf, (pw[dis] * edge[i].cost + nowup) % M, dis + 1); n   } n } n long long calc(int k) { n   int t = dist[k].size(); n   long long ret = 0; n   for (int i = 0; i <= t - 1; i++) { n     long long x, y; n     ex_gcd(M, pw[dist[k][i]], 1, x, y); n     y = ((y % M) + M) % M; n     y = (y * (-down[k][i]) % M + M) % M; n     long long tx, ty; n     ex_gcd(M, 10 % M, 1, tx, ty); n     ty = ((ty % M) + M) % M; n     ty = (((y - cst[k][father[i]]) * ty) % M + M) % M; n     ret += up[k][y] - up[father[i]][ty] - (cst[k][father[i]] % M == y); n     if (!down[k][i]) ret++; n   } n   ret += up[k][0]; n   up[k].clear(); n   dist[k].clear(); n   down[k].clear(); n   for (int i = hed[k]; i != -1; i = edge[i].nex) { n     int v = edge[i].todot; n     if (vis[v]) continue; n     dist[v].clear(); n     up[v].clear(); n     down[v].clear(); n   } n   return ret; n } n long long getans(int k) { n   vis[k] = 1; n   tot = 0; n   dist[k].clear(); n   down[k].clear(); n   Merge(k, k, k, 0, 0, 0); n   up[k].clear(); n   getup(k, k, k, 0, 0); n   for (int i = hed[k]; i != -1; i = edge[i].nex) { n     int v = edge[i].todot; n     if (vis[v]) continue; n     up[v].clear(); n     getup(v, k, v, 0, 0); n   } n   long long ret = calc(k); n   for (int i = hed[k]; i != -1; i = edge[i].nex) { n     int v = edge[i].todot; n     if (vis[v]) continue; n     nsize = 0; n     getsize(v, k); n     root = 0; n     getroot(v, k); n     ret += getans(root); n   } n   return ret; n } n int main() { n   n = read(); n   M = read(); n   pw[0] = 1 % M; n   for (int i = 1; i <= n; i++) pw[i] = (pw[i - 1] * 10) % M; n   nedge = 0; n   memset(hed, -1, sizeof(hed)); n   for (int i = 1; i <= n - 1; i++) { n     int a, b; n     long long c; n     a = read(); n     b = read(); n     c = read(); n     ++a; n     ++b; n     addedge(a, b, c); n     addedge(b, a, c); n     cst[a][b] = cst[b][a] = c; n   } n   maxson[0] = inf; n   root = 0; n   nsize = n; n   getroot(1, 1); n   long long ans = getans(root); n   printf( %I64d n , ans); n   return 0; n } n


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


#include <bits/stdc++.h> n using namespace std; n template <typename T, typename U> n inline void smin(T &a, U b) { n   if (a > b) a = b; n } n template <typename T, typename U> n inline void smax(T &a, U b) { n   if (a < b) a = b; n } n template <class T> n inline void gn(T &first) { n   char c, sg = 0; n   while (c = getchar(), (c >  9  || c <  0 ) && c !=  - ) n     ; n   for ((c ==  -  ? sg = 1, c = getchar() : 0), first = 0; c >=  0  && c <=  9 ; n        c = getchar()) n     first = (first << 1) + (first << 3) + c -  0 ; n   if (sg) first = -first; n } n template <class T, class T1> n inline void gn(T &first, T1 &second) { n   gn(first); n   gn(second); n } n template <class T, class T1, class T2> n inline void gn(T &first, T1 &second, T2 &z) { n   gn(first); n   gn(second); n   gn(z); n } n template <class T> n inline void print(T first) { n   if (first < 0) { n     putchar( - ); n     return print(-first); n   } n   if (first < 10) { n     putchar( 0  + first); n     return; n   } n   print(first / 10); n   putchar(first % 10 +  0 ); n } n template <class T> n inline void printsp(T first) { n   print(first); n   putchar(   ); n } n template <class T> n inline void println(T first) { n   print(first); n   putchar(  n ); n } n template <class T, class U> n inline void print(T first, U second) { n   printsp(first); n   println(second); n } n template <class T, class U, class V> n inline void print(T first, U second, V z) { n   printsp(first); n   printsp(second); n   println(z); n } n int power(int a, int b, int m, int ans = 1) { n   for (; b; b >>= 1, a = 1LL * a * a % m) n     if (b & 1) ans = 1LL * ans * a % m; n   return ans; n } n int root[1111111], deg[1111111], edge[1111111]; n int find_root(int u) { n   if (u == root[u]) return u; n   return root[u] = find_root(root[u]); n } n int main() { n   int n, m, u, v; n   int self = 0; n   gn(n, m); n   for (int i = 1; i <= n; i++) root[i] = i; n   for (int i = 0; i < m; i++) { n     gn(u, v); n     if (u == v) n       self++; n     else n       deg[u]++, deg[v]++; n     u = find_root(u); n     v = find_root(v); n     if (u == v) n       edge[u]++; n     else { n       root[v] = u; n       edge[u] += (edge[v] + 1); n     } n   } n   int yes = 0, cnt = 0; n   for (int i = 1; i <= n; i++) n     if (edge[i] == m) { n       yes = 1; n       break; n     } n   if (!yes) return puts( 0 ); n   long long ans = 0; n   for (int i = 1; i <= n; i++) ans += (long long)deg[i] * (deg[i] - 1) / 2; n   ans += (long long)self * (m - self) + (long long)self * (self - 1) / 2; n   println(ans); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename A, typename B> n inline void chmin(A &a, B b) { n   if (a > b) a = b; n } n template <typename A, typename B> n inline void chmax(A &a, B b) { n   if (a < b) a = b; n } n struct Dinic { n   struct edge { n     long long to, cap, rev; n     edge(long long to, long long cap, long long rev) n         : to(to), cap(cap), rev(rev) {} n   }; n   const long long INF = 1001001001; n   vector<vector<edge>> G; n   vector<long long> level, iter; n   Dinic(long long v) : G(v), level(v), iter(v) {} n   void addEdge(long long from, long long to, long long cap) { n     G[from].push_back(edge(to, cap, G[to].size())); n     G[to].push_back(edge(from, 0, G[from].size() - 1)); n   } n   void bfs(long long s) { n     fill(level.begin(), level.end(), -1); n     queue<long long> que; n     level[s] = 0; n     que.push(s); n     while (que.size()) { n       long long v = que.front(); n       que.pop(); n       for (auto &e : G[v]) { n         if (e.cap && level[e.to] == -1) { n           level[e.to] = level[v] + 1; n           que.push(e.to); n         } n       } n     } n   } n   long long dfs(long long v, long long t, long long f) { n     if (v == t) return f; n     for (long long &i = iter[v]; i < G[v].size(); i++) { n       edge &e = G[v][i]; n       if (e.cap > 0 && level[v] < level[e.to]) { n         long long d = dfs(e.to, t, min(f, e.cap)); n         if (d > 0) { n           e.cap -= d; n           G[e.to][e.rev].cap += d; n           return d; n         } n       } n     } n     return 0; n   } n   long long maxFlow(long long s, long long t) { n     long long flow = 0; n     while (true) { n       bfs(s); n       if (level[t] < 0) return flow; n       fill(iter.begin(), iter.end(), 0); n       long long f; n       while ((f = dfs(s, t, INF)) > 0) flow += f; n     } n   } n }; n long long N, M; n long long A[5555], B[5555]; n bool check(long long x) { n   Dinic d(N + 2); n   long long src = N; n   long long snk = N + 1; n   vector<long long> cnt(N); n   for (long long i = 0; i < (M); i++) { n     cnt[A[i]]++; n     d.addEdge(A[i], B[i], 1); n   } n   long long f = 0; n   for (long long i = 0; i < (N); i++) { n     if (cnt[i] > x) { n       d.addEdge(src, i, cnt[i] - x); n       f += cnt[i] - x; n     } else if (cnt[i] < x) { n       d.addEdge(i, snk, x - cnt[i]); n     } n   } n   if (d.maxFlow(src, snk) != f) return false; n   set<pair<long long, long long>> es; n   for (long long i = 0; i < (N); i++) n     for (auto &e : d.G[i]) n       if (e.cap) es.insert({i, e.to}); n   for (long long i = 0; i < (M); i++) { n     if (es.find({A[i], B[i]}) == es.end()) swap(A[i], B[i]); n   } n   return true; n } n signed main() { n   cin >> N >> M; n   for (long long i = 0; i < (M); i++) cin >> A[i] >> B[i], A[i]--, B[i]--; n   long long lb = 0, ub = M; n   while (ub - lb > 1) { n     long long mid = (ub + lb) / 2; n     if (check(mid)) n       ub = mid; n     else n       lb = mid; n   } n   check(ub); n   cout << ub << endl; n   for (long long i = 0; i < (M); i++) n     cout << A[i] + 1 <<     << B[i] + 1 << endl; n   return 0; n } n


#include <bits/stdc++.h> n #pragma GCC optimize( O3 ,  unroll-loops ) n #pragma GCC target( avx2 ) n using namespace std; n const int maxn = 1e6 + 5; n int n; n bool used[maxn]; n vector<int> v[maxn]; n vector<int> g[maxn]; n int ans[maxn]; n int pr[maxn]; n int t[4 * maxn]; n void init() { n   used[0] = true; n   used[1] = true; n   for (int i = 2; i < maxn; ++i) { n     if (used[i]) continue; n     v[i] = {i}; n     for (int j = 2 * i; j < maxn; j += i) { n       used[j] = true; n       v[j].push_back(i); n     } n   } n } n vector<int> slv(vector<pair<int, int> > z) { n   vector<int> f; n   for (auto h : z) f.push_back(h.second); n   pr[0] = 0; n   for (int i = 0; i < f.size(); ++i) pr[i + 1] = pr[i] + f[i]; n   stack<pair<int, int> > o; n   for (int i = 0; i <= f.size(); ++i) ans[i] = 1e9; n   for (int i = 0; i <= f.size(); ++i) { n     while (true) { n       if (o.empty()) { n         o.push({pr[i], i}); n         break; n       } n       pair<int, int> z = o.top(); n       if (z.first <= pr[i]) { n         o.push({pr[i], i}); n         break; n       } else { n         ans[z.second] = i; n         o.pop(); n       } n     } n   } n   vector<int> res; n   for (int i = 0; i < f.size(); ++i) { n     if (ans[i] == 1e9) n       res.push_back(n); n     else n       res.push_back(z[ans[i] - 1].first); n   } n   return res; n } n void to(int node, int tl, int tr, int l, int r, int val) { n   if (tl >= l && tr <= r) { n     t[node] = min(t[node], val); n     return; n   } n   if (tl >= r || tr <= l) { n     return; n   } n   int tm = (tl + tr) / 2; n   to(2 * node + 1, tl, tm, l, r, val); n   to(2 * node + 2, tm, tr, l, r, val); n } n int get(int node, int tl, int tr, int pos) { n   if (tl > pos || tr <= pos) return 1e9; n   int ans = t[node]; n   if ((tr - tl) == 1) return ans; n   int tm = (tl + tr) / 2; n   ans = min(ans, get(2 * node + 1, tl, tm, pos)); n   ans = min(ans, get(2 * node + 2, tm, tr, pos)); n   return ans; n } n int32_t main() { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   for (int i = 0; i < 4 * maxn; ++i) t[i] = 1e9; n   init(); n   cin >> n; n   int a[n]; n   for (int i = 0; i < n; ++i) cin >> a[i]; n   string s; n   cin >> s; n   for (int i = 0; i < n; ++i) n     for (auto h : v[a[i]]) g[h].push_back(i); n   vector<pair<pair<int, int>, int> > o; n   for (int i = 2; i < maxn; ++i) { n     if (g[i].empty()) continue; n     vector<pair<int, int> > z; n     int o1 = 0; n     for (auto j : g[i]) { n       o1 = 0; n       int x = a[j]; n       assert(x % i == 0); n       while (x % i == 0) { n         ++o1; n         x /= i; n       } n       if (s[j] ==  * ) n         z.push_back({j, o1}); n       else n         z.push_back({j, -o1}); n     } n     vector<int> t = slv(z); n     for (int i = 0; i < z.size(); ++i) { n       if (i == 0) n         o.push_back({{0, z[0].first + 1}, t[i]}); n       else n         o.push_back({{z[i - 1].first + 1, z[i].first + 1}, t[i]}); n     } n   } n   to(0, 0, n, 0, n, n); n   for (auto l : o) to(0, 0, n, l.first.first, l.first.second, l.second); n   long long res = 0; n   for (int i = 0; i < n; ++i) { n     int val = get(0, 0, n, i); n     res += max(0, val - i); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, i, j, k, x, y, w, m, Q, used[100500], start; n int a[100500], b[100500], c[100500]; n vector<pair<int, int> > g[100500]; n vector<int> f[1000500]; n long long d[100500]; n long long hea[200500]; n int new_w[100500], new_d[100500]; n int hea2[200500]; n int st[100500], sz[100500], cur_pos[100500]; n int G[100500]; n int heapSize; n void add(pair<long long, int> value) { n   hea[heapSize++] = value.first; n   int i = heapSize - 1; n   hea2[i] = value.second; n   int parent = ((i - 1) >> 1); n   while (i > 0 && hea[parent] < hea[i]) { n     swap(hea[i], hea[parent]); n     swap(hea2[i], hea2[parent]); n     i = parent; n     parent = ((i - 1) >> 1); n   } n } n void heapify(int i) { n   int largestChild; n   for (;;) { n     largestChild = i; n     if (hea[2 * i + 1] > hea[largestChild]) { n       largestChild = 2 * i + 1; n     } n     if (hea[2 * i + 2] > hea[largestChild]) { n       largestChild = 2 * i + 2; n     } n     if (largestChild == i) { n       break; n     } n     swap(hea[i], hea[largestChild]); n     swap(hea2[i], hea2[largestChild]); n     i = largestChild; n   } n } n pair<long long, int> getMax() { n   pair<int, int> result = make_pair(hea[0], hea2[0]); n   heapSize--; n   hea[0] = hea[heapSize]; n   hea2[0] = hea2[heapSize]; n   hea[heapSize] = -(long long)1e+18; n   heapify(0); n   return result; n } n inline unsigned int readUInt() { n   int c = getc(stdin); n   unsigned int x = 0; n   while (c <= 32) c = getc(stdin); n   while ( 0  <= c && c <=  9 ) x = x * 10 + c -  0 , c = getc(stdin); n   return x; n } n int main() { n   cin >> n >> m >> Q; n   for (i = 0; i <= 200000; i++) hea[i] = -(long long)1e+18; n   for (i = 0; i < m; i++) { n     int x, y, z; n     a[i] = readUInt(); n     b[i] = readUInt(); n     c[i] = readUInt(); n   } n   int s = 1; n   for (i = 0; i < m; i++) { n     g[a[i]].push_back(make_pair(b[i], c[i])); n   } n   st[1] = 0; n   sz[1] = g[1].size(); n   for (i = 2; i <= n; i++) { n     st[i] = cur_pos[i] = st[i - 1] + g[i - 1].size(); n     sz[i] = g[i].size(); n   } n   for (i = 0; i < m; i++) { n     G[cur_pos[a[i]]++] = i; n   } n   for (i = 1; i <= n; i++) d[i] = (long long)1e+18; n   d[s] = 0; n   for (i = 0; i < heapSize; i++) hea[i] = -(long long)1e+18; n   heapSize = 0; n   add(make_pair(0, s)); n   while (heapSize) { n     pair<int, int> tmp = getMax(); n     int v = tmp.second; n     long long cur_d = -tmp.first; n     if (cur_d > d[v]) continue; n     for (size_t j = 0; j < g[v].size(); ++j) { n       int to = g[v][j].first; n       long long len = g[v][j].second; n       if (d[v] + len < d[to]) { n         d[to] = d[v] + len; n         add(make_pair(-d[to], to)); n       } n     } n   } n   for (i = 0; i < m; i++) new_w[i] = min(100000000LL, c[i] + d[a[i]] - d[b[i]]); n   int last_test = 1; n   for (int ii = 0; ii < Q; ii++) { n     int test; n     cin >> test; n     if (test == 1) { n       if (last_test == 2) { n         for (i = 0; i <= n; i++) { n           new_d[i] = (int)1e+9; n         } n         for (i = 0; i <= x; i++) f[i].clear(); n         f[0].push_back(1); n         new_d[1] = 0; n         for (i = 0; i <= x; i++) { n           for (j = 0; j < f[i].size(); j++) { n             int v = f[i][j]; n             if (new_d[v] == i) { n               int tmp = st[v] + sz[v]; n               for (k = st[v]; k < tmp; k++) { n                 int to = b[G[k]]; n                 int val = new_w[G[k]]; n                 if (new_d[to] > i + val) { n                   new_d[to] = min(100000000, i + val); n                   if (new_d[to] <= x) f[new_d[to]].push_back(to); n                 } n               } n             } n           } n         } n         for (i = 1; i <= n; i++) { n           d[i] += new_d[i]; n           if (d[i] > (long long)1e+18) d[i] = (long long)1e+18; n         } n         for (i = 0; i < m; i++) n           new_w[i] = min(100000000LL, c[i] + d[a[i]] - d[b[i]]); n       } n       int t; n       cin >> t; n       if (d[t] == (long long)1e+18) n         cout << -1 << endl; n       else n         cout << d[t] << endl; n     } else { n       int tmp; n       if (last_test == 1) x = 0; n       cin >> tmp; n       for (i = 0; i < tmp; i++) { n         int y = readUInt(); n         new_w[y - 1]++; n         c[y - 1]++; n       } n       x += tmp; n     } n     last_test = test; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <class TH> n void _dbg(const char *sdbg, TH h) { n   cerr << sdbg <<   =   << h << endl; n } n template <class TH, class... TA> n void _dbg(const char *sdbg, TH h, TA... a) { n   while (*sdbg !=  , ) cerr << *sdbg++; n   cerr <<   =   << h <<  ,  ; n   _dbg(sdbg + 1, a...); n } n const int maxn = (1e6) + 7; n const int maxk = 20; n const int inf = (1e9) + 7; n const long long LLinf = (1e18) + 7; n const long double eps = 1e-9; n const long long mod = 1e9 + 7; n struct lit { n   bool stan = 0; n   int dlu = 1; n   lit() {} n   lit(int a, int b) { n     stan = a; n     dlu = b; n   } n }; n lit merge(lit a, lit b) { n   assert(a.stan == b.stan); n   return lit(a.stan, a.dlu + b.dlu); n } n vector<lit> a, b, A, B; n vector<pair<int, int> > odp; n void normuj(vector<lit> &a) { n   vector<lit> b; n   for (int i = 0; i < ((int)(a).size()); i++) n     if (((int)(b).size()) > 0 && b.back().stan == a[i].stan) n       b.back() = merge(b.back(), a[i]); n     else n       b.push_back(a[i]); n   a = b; n } n vector<pair<int, int> > elo() { n   odp.clear(); n   int ile = 0; n   bool f = 0; n   ile = ((int)(a).size()) + ((int)(b).size()); n   if ((((int)(a).size()) > 2 || ((int)(b).size()) > 2) && n       abs(((int)(a).size()) - ((int)(b).size())) > 2) { n     if (((int)(a).size()) < ((int)(b).size())) f = 1, swap(a, b); n     int le = ile / 2 - 1; n     while (le + 1 < ((int)(a).size()) && a[le].stan == b.back().stan && n            a.back().stan != b.back().stan) n       le++; n     if (a.back().stan == b.back().stan) { n       le = ile / 2 - 1; n       while (le + 1 < ((int)(a).size()) && a[le].stan != b.back().stan) le++; n     } n     int cnt = 0; n     vector<lit> xd, xd2; n     for (int i = 0; i < le; i++) xd.push_back(a[i]); n     for (int i = 0; i < ((int)(b).size()) - 1; i++) xd2.push_back(b[i]); n     if (a.back().stan == b.back().stan) xd2.push_back(b.back()); n     int dlu = 0; n     for (int i = le; i < ((int)(a).size()); i++) n       xd2.push_back(a[i]), dlu += a[i].dlu; n     if (a.back().stan != b.back().stan) n       odp.push_back({dlu, b.back().dlu}), xd.push_back(b.back()); n     else n       odp.push_back({dlu, 0}); n     b = xd2; n     a = xd; n     normuj(a); n     normuj(b); n   }; n   while (((int)(a).size()) > 1 && ((int)(b).size()) > 1) { n     if (((int)(a).size()) == 2 && ((int)(b).size()) == 2) { n       if (a.back().stan == b.back().stan) { n         odp.push_back({a.back().dlu, 0}); n         odp.push_back({a[0].dlu, a.back().dlu + b.back().dlu}); n       } else n         odp.push_back({a.back().dlu, b.back().dlu}); n       if (f) n         for (auto &s : odp) swap(s.first, s.second); n       return odp; n     } n     if (a.back().stan == b.back().stan) { n       if (((int)(a).size()) >= ((int)(b).size())) { n         pair<lit, lit> xd = {a[((int)(a).size()) - 2], a.back()}; n         odp.push_back({xd.first.dlu + xd.second.dlu, b.back().dlu}); n         a.pop_back(); n         a.pop_back(); n         lit xdxd = b.back(); n         b.pop_back(); n         a.back() = merge(a.back(), xdxd); n         b.back() = merge(xd.first, b.back()); n         b.push_back(xd.second); n       } else { n         pair<lit, lit> xd = {b[((int)(b).size()) - 2], b.back()}; n         odp.push_back({a.back().dlu, xd.first.dlu + xd.second.dlu}); n         b.pop_back(); n         b.pop_back(); n         lit xdxd = a.back(); n         a.pop_back(); n         b.back() = merge(b.back(), xdxd); n         a.back() = merge(xd.first, a.back()); n         a.push_back(xd.second); n       } n     } else { n       odp.push_back({a.back().dlu, b.back().dlu}); n       lit xd = a.back(); n       lit xd2 = b.back(); n       a.pop_back(); n       b.pop_back(); n       b.back() = merge(b.back(), xd); n       a.back() = merge(a.back(), xd2); n     } n   }; n   if (((int)(a).size()) == 3 && ((int)(b).size()) == 1) { n     if (a.back().stan == b.back().stan) { n       odp.push_back({a.back().dlu, 0}); n       odp.push_back( n           {a[((int)(a).size()) - 2].dlu, a.back().dlu + b.back().dlu}); n     } else { n       odp.push_back({a.back().dlu, b.back().dlu}); n       odp.push_back( n           {a[((int)(a).size()) - 2].dlu + b.back().dlu, a.back().dlu}); n     } n   } n   if (((int)(a).size()) == 1 && ((int)(b).size()) == 3) { n     if (b.back().stan == a.back().stan) { n       odp.push_back({0, b.back().dlu}); n       odp.push_back( n           {b.back().dlu + a.back().dlu, b[((int)(b).size()) - 2].dlu}); n     } else { n       odp.push_back({a.back().dlu, b.back().dlu}); n       odp.push_back( n           {b.back().dlu, b[((int)(b).size()) - 2].dlu + a.back().dlu}); n     } n   } n   if (((int)(a).size()) == 2 && ((int)(b).size()) == 1) { n     if (a.back().stan == b.back().stan) n       odp.push_back({a.back().dlu, 0}); n     else n       odp.push_back({a.back().dlu, b.back().dlu}); n   } n   if (((int)(b).size()) == 2 && ((int)(a).size()) == 1) { n     if (a.back().stan == b.back().stan) n       odp.push_back({0, b.back().dlu}); n     else n       odp.push_back({a.back().dlu, b.back().dlu}); n   } n   if (f) n     for (auto &s : odp) swap(s.first, s.second); n   return odp; n } n vector<pair<int, int> > eloelo() { n   bool f = 0; n   int ile = 0; n   odp.clear(); n   ile = ((int)(a).size()) + ((int)(b).size()); n   if ((((int)(a).size()) > 2 || ((int)(b).size()) > 2) && n       abs(((int)(a).size()) - ((int)(b).size())) > 2) { n     if (((int)(a).size()) < ((int)(b).size())) f = 1, swap(a, b); n     int le = ile / 2 - 1; n     while (le + 1 < ((int)(a).size()) && a[le].stan == b.back().stan) le++; n     int cnt = 0; n     vector<lit> xd, xd2; n     for (int i = 0; i < le; i++) xd.push_back(a[i]); n     for (int i = 0; i < ((int)(b).size()) - 1; i++) xd2.push_back(b[i]); n     int dlu = 0; n     for (int i = le; i < ((int)(a).size()); i++) n       xd2.push_back(a[i]), dlu += a[i].dlu; n     odp.push_back({dlu, b.back().dlu}); n     xd.push_back(b.back()); n     b = xd2; n     a = xd; n     normuj(a); n     normuj(b); n   } n   while (((int)(a).size()) > 1 && ((int)(b).size()) > 1) { n     if (((int)(a).size()) == 2 && ((int)(b).size()) == 2) { n       if (a.back().stan == b.back().stan) { n         odp.push_back({a.back().dlu, 0}); n         odp.push_back({a[0].dlu, a.back().dlu + b.back().dlu}); n       } else n         odp.push_back({a.back().dlu, b.back().dlu}); n       if (f) n         for (auto &s : odp) swap(s.first, s.second); n       return odp; n     } n     if (a.back().stan == b.back().stan) { n       if (((int)(a).size()) >= ((int)(b).size())) { n         pair<lit, lit> xd = {a[((int)(a).size()) - 2], a.back()}; n         odp.push_back({xd.first.dlu + xd.second.dlu, b.back().dlu}); n         a.pop_back(); n         a.pop_back(); n         lit xdxd = b.back(); n         b.pop_back(); n         a.back() = merge(a.back(), xdxd); n         b.back() = merge(xd.first, b.back()); n         b.push_back(xd.second); n       } else { n         pair<lit, lit> xd = {b[((int)(b).size()) - 2], b.back()}; n         odp.push_back({a.back().dlu, xd.first.dlu + xd.second.dlu}); n         b.pop_back(); n         b.pop_back(); n         lit xdxd = a.back(); n         a.pop_back(); n         b.back() = merge(b.back(), xdxd); n         a.back() = merge(xd.first, a.back()); n         a.push_back(xd.second); n       } n     } else { n       odp.push_back({a.back().dlu, b.back().dlu}); n       lit xd = a.back(); n       lit xd2 = b.back(); n       a.pop_back(); n       b.pop_back(); n       b.back() = merge(b.back(), xd); n       a.back() = merge(a.back(), xd2); n     } n   }; n   if (((int)(a).size()) == 3 && ((int)(b).size()) == 1) { n     if (a.back().stan == b.back().stan) { n       odp.push_back({a.back().dlu, 0}); n       odp.push_back( n           {a[((int)(a).size()) - 2].dlu, a.back().dlu + b.back().dlu}); n     } else { n       odp.push_back({a.back().dlu, b.back().dlu}); n       odp.push_back( n           {a[((int)(a).size()) - 2].dlu + b.back().dlu, a.back().dlu}); n     } n   } n   if (((int)(a).size()) == 1 && ((int)(b).size()) == 3) { n     if (b.back().stan == a.back().stan) { n       odp.push_back({0, b.back().dlu}); n       odp.push_back( n           {b.back().dlu + a.back().dlu, b[((int)(b).size()) - 2].dlu}); n     } else { n       odp.push_back({a.back().dlu, b.back().dlu}); n       odp.push_back( n           {b.back().dlu, b[((int)(b).size()) - 2].dlu + a.back().dlu}); n     } n   } n   if (((int)(a).size()) == 2 && ((int)(b).size()) == 1) { n     if (a.back().stan == b.back().stan) n       odp.push_back({a.back().dlu, 0}); n     else n       odp.push_back({a.back().dlu, b.back().dlu}); n   } n   if (((int)(b).size()) == 2 && ((int)(a).size()) == 1) { n     if (a.back().stan == b.back().stan) n       odp.push_back({0, b.back().dlu}); n     else n       odp.push_back({a.back().dlu, b.back().dlu}); n   } n   if (f) n     for (auto &s : odp) swap(s.first, s.second); n   return odp; n } n int main() { n   ios_base::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   string s; n   cin >> s; n   char poprz = 0; n   int ile = 0; n   for (auto v : s) { n     if (v == poprz) n       ile++; n     else { n       if (ile > 0) a.push_back(lit(poprz -  a , ile)); n       poprz = v; n       ile = 1; n     } n   } n   a.push_back(lit(poprz -  a , ile)); n   cin >> s; n   ile = 0; n   poprz =  a ; n   for (auto v : s) { n     if (v == poprz) n       ile++; n     else { n       if (ile > 0) b.push_back(lit(poprz -  a , ile)); n       poprz = v; n       ile = 1; n     } n   } n   b.push_back(lit(poprz -  a , ile)); n   reverse(a.begin(), a.end()); n   reverse(b.begin(), b.end()); n   A = a; n   B = b; n   vector<pair<int, int> > x = elo(); n   a = A; n   b = B; n   vector<pair<int, int> > y = eloelo(); n   cout << min(((int)(x).size()), ((int)(y).size())) <<   n ; n   if (((int)(x).size()) < ((int)(y).size())) n     for (auto s : x) cout << s.first <<     << s.second <<   n ; n   else n     for (auto s : y) cout << s.first <<     << s.second <<   n ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int q; n   cin >> q; n   for (int test = 0; test < q; test++) { n     int a, b, c, d, k; n     cin >> a >> b >> c >> d >> k; n     int pen = (a - 1) / c + 1, pencil = (b - 1) / d + 1; n     if (pen + pencil > k) { n       cout << -1 <<   n ; n     } else { n       cout << pen <<     << pencil <<   n ; n     } n   } n } n