#include <bits/stdc++.h> n using namespace std; n int an[101][21][21], n, k, md = 1e9 + 7, t[21][21]; n vector<int> tr[101]; n void go(int v = 1, int p = 0) { n   an[v][0][1] = an[v][1][0] = 1; n   for (int i = 0; i < (int)(tr[v].size()); ++i) { n     int u = tr[v][i]; n     if (u == p) continue; n     go(u, v); n     for (int i = 0; i < (int)(21); ++i) n       for (int j = 0; j < (int)(21); ++j) n         for (int w = 0; w < (int)(21); ++w) n           for (int z = 0; z < (int)(21); ++z) { n             int a = i; n             if (w && (w + 1 < a || !a)) a = w + 1; n             if (a > 20) a = 0; n             int b = 0; n             if (!i && z) n               b = z + 1; n             else if (i && z && k - i + 1 < z) n               b = z + 1; n             if (!w) n               b = max(b, j); n             else if (j && k - w + 1 < j) n               b = max(b, j); n             if (b > 20) continue; n             t[a][b] = ((long long)an[v][i][j] * an[u][w][z] + t[a][b]) % md; n           } n     for (int i = 0; i < (int)(21); ++i) n       for (int j = 0; j < (int)(21); ++j) an[v][i][j] = t[i][j], t[i][j] = 0; n   } n } n int main() { n   scanf( %d%d , &n, &k); n   for (int i = 1; i < (int)(n); ++i) { n     int a, b; n     scanf( %d%d , &a, &b); n     tr[a].push_back(b); n     tr[b].push_back(a); n   } n   go(); n   int a = 0; n   for (int i = 0; i < (int)(21); ++i) n     if ((a += an[1][i][0]) >= md) a -= md; n   printf( %d n , a); n } n


#include <bits/stdc++.h> n using namespace std; n long long dp[100010]; n int a[310], in[310]; n long long tmp[310]; n struct edge { n   int to; n   int next; n } e[310]; n int box[100010], cnt; n long long T; n void init() { n   memset(box, -1, sizeof(box)); n   memset(in, 0, sizeof(in)); n   memset(dp, 0, sizeof(dp)); n   cnt = 0; n } n void add(int from, int to) { n   e[cnt].to = to; n   e[cnt].next = box[from]; n   box[from] = cnt++; n } n long long dfs(int now) { n   tmp[now] = a[now]; n   int t, v; n   for (t = box[now]; t + 1; t = e[t].next) { n     v = e[t].to; n     long long tt = dfs(v); n     tmp[now] += tt; n   } n   T -= tmp[now]; n   return tmp[now]; n } n bool iscircle(int n) { n   int qu[310], tmp[310]; n   int i, sum = 0, top = 0; n   for (i = 1; i <= n; i++) { n     tmp[i] = in[i]; n     if (tmp[i] == 0) qu[top++] = i; n   } n   for (i = 0; i < top; i++) { n     int now = qu[i], t, v; n     for (t = box[now]; t + 1; t = e[t].next) { n       v = e[t].to; n       tmp[v]--; n       if (!tmp[v]) qu[top++] = v; n     } n   } n   if (top < n) return true; n   return false; n } n int main() { n   int n, q, i, aa, b, j; n   scanf( %d%d%I64d , &n, &q, &T); n   for (i = 1; i <= n; i++) scanf( %d , &a[i]); n   init(); n   for (i = 1; i <= q; i++) { n     scanf( %d%d , &aa, &b); n     in[aa]++; n     add(b, aa); n   } n   bool tru = iscircle(n); n   for (i = 1; i <= n; i++) { n     if (!in[i]) { n       dfs(i); n       T += tmp[i]; n     } n   } n   if (tru || T < 0) n     printf( 0 n ); n   else { n     dp[0] = 1; n     for (i = 1; i <= n; i++) { n       for (j = tmp[i]; j <= T; j++) n         dp[j] = (dp[j] + dp[j - tmp[i]]) % 1000000007; n     } n     printf( %I64d n , dp[T]); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int m[26]; n int main() { n   float a, c, d, b; n   cin >> a >> b >> c >> d; n   float sa = max((3 * a) / 10.0, a - (a / 250.0) * c); n   float sb = max((3 * b) / 10.0, b - (b / 250.0) * d); n   if (sa > sb) n     cout <<  Misha ; n   else if (sb > sa) n     cout <<  Vasya ; n   else n     cout <<  Tie ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 3e5 + 5; n int n, m; n int first[maxn], second[maxn]; n void solve() { n   cin >> n >> m; n   for (int i = 1; i <= m; ++i) cin >> first[i] >> second[i]; n   int a, b; n   a = first[1]; n   b = -1; n   for (int i = 1; i <= m; ++i) { n     if (first[i] != a && second[i] != a) { n       if (first[i] != b && second[i] != b) { n         if (b == -1) n           b = first[i]; n         else n           break; n       } n     } n     if (i == m) { n       puts( YES ); n       return; n     } n   } n   a = -1; n   b = second[1]; n   for (int i = 1; i <= m; ++i) { n     if (first[i] != b && second[i] != b) { n       if (first[i] != a && second[i] != a) { n         if (a == -1) n           a = first[i]; n         else n           break; n       } n     } n     if (i == m) { n       puts( YES ); n       return; n     } n   } n   a = -1; n   b = second[1]; n   for (int i = 1; i <= m; ++i) { n     if (first[i] != b && second[i] != b) { n       if (first[i] != a && second[i] != a) { n         if (a == -1) n           a = second[i]; n         else n           break; n       } n     } n     if (i == m) { n       puts( YES ); n       return; n     } n   } n   a = first[1]; n   b = -1; n   for (int i = 1; i <= m; ++i) { n     if (first[i] != a && second[i] != a) { n       if (first[i] != b && second[i] != b) { n         if (b == -1) n           b = second[i]; n         else n           break; n       } n     } n     if (i == m) { n       puts( YES ); n       return; n     } n   } n   puts( NO ); n } n int main() { solve(); } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int x, y; n   cin >> x >> y; n   if (((x < y) ? x : y) % 2) n     cout <<  Akshat ; n   else n     cout <<  Malvika ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int P = 10, N = 1e5 + 10; n int inverse(int a, int m) { n   int m0 = m, t, q; n   int x0 = 0, x1 = 1; n   if (m == 1) return 0; n   while (a > 1) { n     q = a / m; n     t = m; n     m = a % m, a = t; n     t = x0; n     x0 = x1 - q * x0; n     x1 = t; n   } n   if (x1 < 0) x1 += m0; n   return x1; n } n int fact[N], ifact[N], pot[N][P]; n vector<int> primes(int n) { n   vector<int> ans; n   for (int d = 2; d * d <= n; d++) n     if (n % d == 0) { n       ans.push_back(d); n       while (n % d == 0) { n         n /= d; n       } n     } n   if (n > 1) ans.push_back(n); n   return ans; n } n int mod; n vector<int> p; n int expomod(long long int a, long long int e) { n   int ans = 1; n   while (e) { n     if (e & 1) ans = ans * a % mod; n     e /= 2; n     a = a * a % mod; n   } n   return ans; n } n int comb(int m, int n) { n   int ans = (long long int)fact[m] * ifact[n] % mod * ifact[m - n] % mod; n   for (int i = 0; i < (int)(((int)(p).size())); i++) { n     int pp = pot[m][i] - pot[n][i] - pot[m - n][i]; n     if (pp) ans = (long long int)ans * expomod(p[i], pp) % mod; n   } n   return ans; n } n int go(int n, int bal) { n   long long int ans = 0; n   for (int x = 0, y = bal; x + y <= n; x++, y++) { n     ans += (long long int)comb(n, x) * comb(n - x, y) % mod; n     if (ans >= mod) ans -= mod; n   } n   return ans; n } n int main() { n   int n, l, r; n   cin >> n >> mod >> l >> r; n   p = primes(mod); n   fact[0] = ifact[0] = 1; n   for (int i = 1; i <= n; i++) { n     int d = -1; n     for (int j = 0; j < (int)(((int)(p).size())); j++) n       if (i % p[j] == 0) { n         d = j; n         break; n       } n     if (d == -1) n       fact[i] = i; n     else { n       fact[i] = fact[i / p[d]]; n       for (int j = 0; j < (int)(((int)(p).size())); j++) n         pot[i][j] = pot[i / p[d]][j] + (j == d); n     } n     ifact[i] = inverse(fact[i], mod); n   } n   for (int i = 0; i < (int)(n); i++) { n     fact[i + 1] = (long long int)fact[i] * fact[i + 1] % mod; n     ifact[i + 1] = (long long int)ifact[i] * ifact[i + 1] % mod; n     for (int j = 0; j < (int)(((int)(p).size())); j++) n       pot[i + 1][j] += pot[i][j]; n   } n   long long int ans = 0; n   for (int i = 0; i < (int)(2); i++) { n     ans += go(n, l + i); n     ans -= go(n, r + 1 + i); n   } n   ans %= mod; n   if (ans < 0) ans += mod; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long a[101][101]; n int main() { n   long long n, m; n   cin >> n >> m; n   long long mn = min(m, n); n   cout << mn + 1 << endl; n   for (long long i = 0; i <= mn; i++) n     cout << i <<     << (mn - i) % (mn + 1) << endl; n } n


#include <bits/stdc++.h> n using namespace std; n char _; n long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } n long long fpow(long long b, long long exp, long long mod) { n   if (exp == 0) return 1; n   long long t = fpow(b, exp / 2, mod); n   if (exp & 1) return t * t % mod * b % mod; n   return t * t % mod; n } n int num_boxes, req, start, vals[55], dp[55][2005]; n char type[55]; n int solve(int box, int sum) { n   if (sum >= req) return 0; n   if (dp[box][sum] != -1) return dp[box][sum]; n   int res = INT_MAX; n   for (int i = 1; i <= num_boxes; i++) n     if (vals[i] > vals[box] && type[i] != type[box]) n       if (solve(i, sum + vals[i]) != INT_MAX) n         res = min(res, abs(i - box) + solve(i, sum + vals[i])); n   return dp[box][sum] = res; n } n int main() { n   scanf( %d %d %d , &num_boxes, &start, &req); n   for (int i = 1; i <= num_boxes; i++) scanf(  %d , &vals[i]); n   for (int i = 1; i <= num_boxes; i++) scanf(  %c , &type[i]); n   memset(dp, -1, sizeof dp); n   int res = solve(start, vals[start]); n   for (int i = 1; i <= num_boxes; i++) n     if (solve(i, vals[i]) != INT_MAX) n       res = min(res, abs(i - start) + solve(i, vals[i])); n   printf( %d n , (res == INT_MAX ? -1 : res)); n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void maxE(T& a, const T& b) { n   a = max(a, b); n } n template <typename T> n void minE(T& a, const T& b) { n   a = min(a, b); n } n template <typename T> n ostream& operator<<(ostream& out, const vector<T>& t_) { n   out <<  [ ; n   for (auto i : t_) out << i <<  ,  ; n   out <<  ] ; n   return out; n } n template <typename S, typename T> n ostream& operator<<(ostream& out, const pair<S, T>& rhs) { n   out <<  (  << rhs.first <<  ,  << rhs.second <<  ) ; n   return out; n } n int gcd(int a, int b) { n   while (a && b) a > b ? a %= b : b %= a; n   return a + b; n } n map<int, int> t; n void proc(int l, int c) { n   if (0) n     cout <<  proc   n          <<  l  n          <<     << l <<     n          <<  c  n          <<     << c <<     << endl; n   if (t.find(l) == t.end()) { n     t[l] = c; n     if (0) cout <<  init0  << endl; n   } else if (t[l] > c) { n     if (0) cout <<  relax1  << endl; n     t[l] = c; n   } n   vector<pair<int, int> > tmp; n   for (auto i : t) tmp.push_back(i); n   for (auto i : tmp) { n     int g = gcd(i.first, l); n     if (t.find(g) == t.end() || t[g] > i.second + c) { n       if (0) n         cout <<  relax2   n              <<  i  n              <<     << i <<     n              <<  g  n              <<     << g <<     n              <<  l  n              <<     << l <<     n              <<  c  n              <<     << c <<     n              <<  i.nd + c  n              <<     << i.second + c <<     << endl; n       t[g] = i.second + c; n     } n   } n } n int main() { n   int n; n   assert(scanf( %d , &n) == 1); n   int l[n]; n   for (int i = 0; i < int(n); ++i) assert(scanf( %d , l + i) == 1); n   int c[n]; n   for (int i = 0; i < int(n); ++i) assert(scanf( %d , c + i) == 1); n   for (int i = 0; i < int(n); ++i) proc(l[i], c[i]); n   if (t.find(1) == t.end()) n     printf( -1 n ); n   else n     printf( %d n , t[1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long read() { n   char x = getchar(); n   long long ans = 0; n   while (!isdigit(x)) x = getchar(); n   while (isdigit(x)) ans = ans * 10 + x -  0 , x = getchar(); n   return ans; n } n long long in[250005], now, n, vis[250005], dp[250005][2]; n vector<pair<long long, long long> > E[250005]; n void add_edge(long long u, long long v, long long w) { n   E[u].push_back(make_pair(v, w)); n } n bool cmp(pair<long long, long long> x, pair<long long, long long> y) { n   return in[x.first] > in[y.first]; n } n struct node { n   long long siz, sum; n   priority_queue<long long> q1, q2; n   void pop(long long x) { n     q2.push(x); n     siz--; n     sum -= x; n   } n   void push(long long x) { n     q1.push(x); n     siz++; n     sum += x; n   } n   void init() { n     while (!q1.empty() && !q2.empty() && q1.top() == q2.top()) n       q1.pop(), q2.pop(); n   } n   void pop() { n     init(); n     siz--; n     sum -= q1.top(); n     q1.pop(); n   } n   long long top() { n     init(); n     return q1.top(); n   } n } P[250005]; n void erase(long long u) { n   for (long long i = 0; i < E[u].size(); i++) { n     long long v = E[u][i].first, w = E[u][i].second; n     if (in[v] <= now) break; n     P[v].push(w); n   } n } n void dfs(long long u, long long pre) { n   vis[u] = now; n   long long tot = 0; n   long long num = in[u] - now; n   while (P[u].siz > num) P[u].pop(); n   for (long long i = 0; i < E[u].size(); i++) { n     long long v = E[u][i].first, w = E[u][i].second; n     if (in[v] <= now) break; n     if (v == pre) continue; n     dfs(v, u); n   } n   vector<long long> tmp1, tmp2; n   for (long long i = 0; i < E[u].size(); i++) { n     long long v = E[u][i].first, w = E[u][i].second; n     if (v == pre) continue; n     if (in[v] <= now) break; n     long long tmp = dp[v][1] + w - dp[v][0]; n     if (tmp <= 0) { n       num--, tot += dp[v][1] + w; n       continue; n     } n     tot += dp[v][0]; n     P[u].push(tmp), tmp2.push_back(tmp); n   } n   while (P[u].siz && P[u].siz > num) { n     tmp1.push_back(P[u].top()); n     P[u].pop(); n   } n   dp[u][0] = tot + P[u].sum; n   while (P[u].siz && P[u].siz > num - 1) { n     tmp1.push_back(P[u].top()); n     P[u].pop(); n   } n   dp[u][1] = tot + P[u].sum; n   for (long long i = 0; i < tmp1.size(); i++) P[u].push(tmp1[i]); n   for (long long i = 0; i < tmp2.size(); i++) P[u].pop(tmp2[i]); n } n signed main() { n   cin >> n; n   long long tot = 0; n   for (long long i = 1; i < n; i++) { n     long long u = read(), v = read(), w = read(); n     add_edge(u, v, w); n     add_edge(v, u, w); n     tot += w; n   } n   cout << tot <<    ; n   for (long long i = 1; i <= n; i++) in[i] = E[i].size(); n   for (long long i = 1; i <= n; i++) sort(E[i].begin(), E[i].end(), cmp); n   vector<pair<long long, long long> > V; n   for (long long i = 1; i <= n; i++) V.push_back(make_pair(in[i], i)); n   sort(V.begin(), V.end()); n   long long pos = 0; n   for (now = 1; now < n; now++) { n     while (pos < V.size() && V[pos].first == now) erase(V[pos].second), pos++; n     tot = 0; n     for (long long j = pos; j < V.size(); j++) { n       if (vis[V[j].second] == now) continue; n       dfs(V[j].second, 0); n       tot += dp[V[j].second][0]; n     } n     cout << tot <<    ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 100005; n int n, s; n long long a[maxn], b[maxn], c[maxn]; n vector<int> p[2]; n bool cmp1(int i, int j) { return b[i] - c[i] > b[j] - c[j]; } n bool cmp2(int i, int j) { return c[i] - b[i] > c[j] - b[j]; } n int main() { n   scanf( %d%d , &n, &s); n   long long tot = 0, A = 0, B = 0, v1 = 0, v2 = 0; n   p[0].clear(); n   p[1].clear(); n   for (int i = 0; i < n; i++) { n     scanf( %I64d%I64d%I64d , a + i, b + i, c + i); n     tot += a[i]; n     if (b[i] > c[i]) { n       p[0].push_back(i); n       A += a[i] * b[i]; n       v1 += a[i]; n     } else { n       p[1].push_back(i); n       B += a[i] * c[i]; n       v2 += a[i]; n     } n   } n   sort(p[0].begin(), p[0].end(), cmp1); n   sort(p[1].begin(), p[1].end(), cmp2); n   long long ans = 0; n   long long c1 = v1 / s + (v1 % s ? 1 : 0); n   long long c2 = v2 / s + (v2 % s ? 1 : 0); n   tot = tot / s + (tot % s ? 1 : 0); n   if (c1 + c2 > tot) { n     long long tmp = A + B; n     long long now = v1 % s; n     for (int i = p[0].size() - 1; i >= 0; i--) { n       now -= a[p[0][i]]; n       long long cnt = a[p[0][i]]; n       if (now < 0) cnt = a[p[0][i]] + now; n       tmp += (c[p[0][i]] - b[p[0][i]]) * cnt; n       if (now <= 0) break; n     } n     ans = max(ans, tmp); n     tmp = A + B; n     now = v2 % s; n     for (int i = p[1].size() - 1; i >= 0; i--) { n       now -= a[p[1][i]]; n       long long cnt = a[p[1][i]]; n       if (now < 0) cnt = a[p[1][i]] + now; n       tmp += (b[p[1][i]] - c[p[1][i]]) * cnt; n       if (now <= 0) break; n     } n     ans = max(ans, tmp); n   } else n     ans = A + B; n   printf( %I64d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const double PI = 2.0 * acos(0.0); n const int INF = 1000 * 1000 * 1000; n const int maxn = 1100; n int W, H, N, M, cnt, tot; n char g[maxn][maxn]; n int upp[maxn][maxn], lf[maxn][maxn]; n bool can; n void play(int x, int y) { n   if (x == N || y == M) return; n   bool r, d; n   if (y + 1 < M && upp[x][y + 1] >= H) n     r = true; n   else n     r = false; n   if (x + 1 < N && lf[x + 1][y] >= W) n     d = true; n   else n     d = false; n   if (r && d) n     can = false; n   else if (r) { n     cnt += H; n     play(x, y + 1); n   } else if (d) { n     cnt += W; n     play(x + 1, y); n   } n } n int solve(int maxVal, int coefh, int coefw, int sx, int sy) { n   for (int len = 1; len <= maxVal; len++) { n     W += (coefw); n     H += (coefh); n     bool all = true; n     for (int k = 0; k < H && all; k++) n       for (int t = 0; t < W && all; t++) { n         if (g[k + sx][t + sy] !=  X ) all = false; n       } n     if (!all) continue; n     can = true; n     cnt = H * W; n     play(sx + H - 1, sy + W - 1); n     if (can && cnt == tot) return H * W; n   } n   return -1; n } n int main() { n   ios_base::sync_with_stdio(false); n   int i, j, k; n   cin >> N >> M; n   int sx = -1, sy; n   tot = 0; n   W = H = 0; n   for (int i = 0; i < N; i++) n     for (int j = 0; j < M; j++) { n       cin >> g[i][j]; n       if (g[i][j] ==  X ) tot++; n       if (g[i][j] ==  X  && sx == -1) { n         sx = i, sy = j; n       } n     } n   memset(upp, 0, sizeof upp); n   memset(lf, 0, sizeof lf); n   for (int i = 0; i < M; i++) upp[0][i] = (g[0][i] ==  X  ? 1 : 0); n   for (int i = 0; i < N; i++) lf[i][0] = (g[i][0] ==  X  ? 1 : 0); n   for (int i = 0; i < N; i++) n     for (int j = 0; j < M; j++) { n       if (i != 0 && g[i][j] ==  X ) upp[i][j] = upp[i - 1][j] + 1; n       if (j != 0 && g[i][j] ==  X ) lf[i][j] = lf[i][j - 1] + 1; n     } n   int w = 0, h = 0; n   for (int i = sy; i < M && g[sx][i] ==  X ; i++) w++; n   for (int i = sx; i < N && g[i][sy] ==  X ; i++) h++; n   int coefh = 0, coefw = 0; n   int ans; n   W = w, H = 0; n   ans = solve(h, 1, 0, sx, sy); n   W = 0, H = h; n   int ans2 = solve(w, 0, 1, sx, sy); n   if (ans == -1 || (ans2 != -1 && ans2 < ans)) ans = ans2; n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int k; n   int l; n   cin >> k >> l; n   int u[k]; n   int y[l]; n   for (int i = 0; i < k; i++) { n     cin >> u[i]; n   } n   for (int i = 0; i < l; i++) { n     cin >> y[i]; n   } n   long long int h = 0; n   long long int v = 0; n   int f = 0; n   int g = 0; n   int p = 0; n   while (p != k || g != l) { n     if (v == h && v == 0) { n       v += y[g]; n       h += u[p]; n       g++; n       p++; n     } else if (v > h) { n       h += u[p]; n       p++; n     } else if (v < h) { n       v += y[g]; n       g++; n     } else { n       f++; n       h = 0; n       v = 0; n     } n   } n   cout << f + 1; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, m, k, x = 1; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> k; n   m = k * (5 + (n - 1) * 6); n   cout << m << endl; n   while (n--) { n     cout << x * k <<     << (x + 1) * k <<     << (x + 2) * k <<     n          << (x + 4) * k << endl; n     x += 6; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 3e5 + 5; n const long double PI = 3.1415926535; n const long double eps = 1e-07; n const long long INF = 1e18; n const int inf = 1e9; n const long long M = 305; n const string IO[2]{ NO n ,  YES n }; n const string io[2]{ No n ,  Yes n }; n struct STree { n   int start; n   vector<int> t; n   STree(int n) { n     start = 1; n     while (start < n) start *= 2; n     t = vector<int>(2 * start, inf); n   } n   int min(int v, int l, int r, int ql, int qr) { n     if (l > qr || r < ql) return inf; n     if (l >= ql && r <= qr) return t[v]; n     int c = (l + r) / 2; n     return std::min(min(2 * v, l, c, ql, qr), min(2 * v + 1, c + 1, r, ql, qr)); n   } n   void upd(int v, int l, int r, int p, int x) { n     if (l > p || r < p) return; n     if (l == r) { n       t[v] = x; n       return; n     } n     int c = (l + r) / 2; n     upd(2 * v, l, c, p, x); n     upd(2 * v + 1, c + 1, r, p, x); n     t[v] = std::min(t[2 * v], t[2 * v + 1]); n   } n }; n void solve() { n   int m; n   cin >> m; n   vector<int> x(m); n   for (int i = 0; i < m; ++i) cin >> x[i]; n   sort(x.begin(), x.end()); n   auto last = unique(x.begin(), x.end()); n   x.erase(last, x.end()); n   m = x.size(); n   int a, b; n   cin >> b >> a; n   int n = b - a + 1; n   STree st = STree(n); n   int start = st.start; n   vector<int> maxx(n, 1); n   for (auto& nx : x) { n     int fa = ((a - 1) / nx) * nx + nx; n     for (int i = fa; i <= b; i += nx) maxx[i - a] = max(maxx[i - a], nx - 1); n   } n   st.upd(1, 0, start - 1, n - 1, 0); n   for (int i = b - 1; i >= a; --i) { n     int x = i - a; n     int cw = maxx[x]; n     int ans = 1 + st.min(1, 0, start - 1, x + 1, x + cw); n     st.upd(1, 0, start - 1, x, ans); n   } n   cout << st.min(1, 0, start - 1, 0, 0) <<   n ; n } n signed main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   cout.tie(nullptr); n   int t = 1; n   while (t--) solve(); n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, m; n   cin >> n >> m; n   vector<string> a(n); n   for (int i = 0; i < n; ++i) cin >> a[i]; n   for (int j = 0; j < m; ++j) { n     vector<int> indx; n     for (int i = 0; i < n; ++i) { n       if (a[i][j] ==  # ) indx.push_back(i); n     } n     for (int i = 1; i < indx.size(); ++i) { n       if (a[indx[i]] != a[indx[0]]) { n         cout <<  No ; n         return 0; n       } n     } n   } n   cout <<  Yes ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn = 2 * 1e5 + 100; n vector<int> Prime; n int main() { n   Prime.clear(); n   int n, k; n   scanf( %d%d , &n, &k); n   int flag = 1; n   for (int i = 2; flag && i <= n; i++) { n     if (n % i == 0) { n       while (n % i == 0) { n         Prime.push_back(i); n         n /= i; n       } n       if (n == 1) { n         flag = 0; n       } n       if (n < i && n > 1) { n         flag = 0; n         Prime.push_back(n); n       } n     } n   } n   if (Prime.size() < k) { n     cout << -1 << endl; n   } else { n     for (int i = 0; i < k - 1; i++) { n       cout << Prime[i] <<    ; n     } n     int ans = 1; n     for (int i = k - 1; i < Prime.size(); i++) { n       ans *= Prime[i]; n     } n     if (ans != 1) { n       cout << ans << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n char s[10]; n int n; n int notzero[12], cnt[12], ten[10], ans; n int main() { n   ten[0] = 1; n   for (int i = 1; i < 8; ++i) ten[i] = 10 * ten[i - 1]; n   scanf( %d , &n); n   for (int i = 1; i <= n; ++i) { n     scanf( %s , s); n     int len = strlen(s); n     notzero[s[0] -  a ] = 1; n     for (int j = 0; j < len; ++j) cnt[s[j] -  a ] += ten[len - j - 1]; n   } n   int zero = -1; n   for (int i = 0; i < 10; ++i) { n     if (notzero[i] == 0) { n       if (zero == -1 || cnt[i] > cnt[zero]) zero = i; n     } n   } n   vector<int> v; n   for (int i = 0; i < 10; ++i) { n     if (zero != i) v.push_back(cnt[i]); n   } n   sort(v.begin(), v.end()); n   for (int i = 0; i < 9; ++i) { n     ans += (9 - i) * v[i]; n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool a[4]; n int main() { n   int n; n   string s; n   int cz = 0; n   int c1 = 0; n   int c0 = 0; n   cin >> s; n   for (int i = 0; i < s.length(); ++i) n     if (s[i] ==  0 ) n       ++c0; n     else if (s[i] ==  1 ) n       ++c1; n     else if (s[i] ==  ? ) n       ++cz; n   n = s.length(); n   if (c0 + cz > c1) a[0] = true; n   if (c1 + cz > c0 + 1) a[3] = true; n   if (s.length() % 2 == 0) { n     int p = s.length() / 2; n     if (abs(c0 - p) + abs(c1 - p) <= cz) { n       if (s[n - 1] ==  1  || s[n - 1] ==  ?  && c1 < p) a[1] = true; n       if (s[n - 1] ==  0  || s[n - 1] ==  ?  && c0 < p) a[2] = true; n     } n   } else { n     int p = s.length() / 2; n     if (abs(c0 - p) + abs(c1 - (p + 1)) <= cz) { n       if (s[n - 1] ==  1  || s[n - 1] ==  ?  && c1 < p + 1) a[1] = true; n       if (s[n - 1] ==  0  || s[n - 1] ==  ?  && c0 < p) a[2] = true; n     } n   } n   if (a[0]) cout <<  00  << endl; n   if (a[1]) cout <<  01  << endl; n   if (a[2]) cout <<  10  << endl; n   if (a[3]) cout <<  11  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n, k; n   cin >> n >> k; n   long long v[n + 10]; n   for (long long i = 0; i < n; i++) { n     cin >> v[i]; n   } n   sort(v, v + n); n   k--; n   long long div = k / n; n   long long cnt = 0; n   long long val = v[div]; n   for (long long i = 0; i < n; i++) { n     if (v[i] == val) cnt++; n     if (v[i] < val) k = k - n; n   } n   long long amt = cnt; n   long long now = k / amt; n   cout << val <<     << v[now] << endl; n } n


#include <bits/stdc++.h> n using namespace std; n struct atom { n   long long f, w, where; n   void scan() { scanf( %I64d%I64d , &f, &w); } n } x[310000]; n priority_queue<long long> A, B; n int compare(atom k1, atom k2) { n   return k1.f > k2.f || (k1.f == k2.f && k1.where < k2.where); n } n int n, pre, ans, where; n long long rem; n void insert(long long k1) { n   if (!B.empty()) { n     long long k2 = B.top(); n     if (k2 > k1) { n       A.push(-k2); n       B.pop(); n       B.push(k1); n       rem += k2 - k1; n       return; n     } n   } n   A.push(-k1); n   pre++; n } n int main() { n   scanf( %d , &n); n   for (int i = 1; i <= n; i++) x[i].scan(), x[i].where = i; n   n++; n   x[n] = (atom){0, 0, n}; n   sort(x + 1, x + n + 1, compare); n   pre = 0, ans = 0, where = 0; n   long long rem = 0; n   for (int i = 1; i <= n; i++) n     if (x[i].where == 1) where = i; n   ans = where; n   pre = where; n   for (int i = 1; i < where; i++) A.push(-(x[i].w - x[i].f + 1)); n   for (int i = where + 1; i <= n; i++) { n     rem += x[i - 1].f - x[i].f; n     if (i - 1 != where) insert(x[i - 1].w - x[i - 1].f + 1); n     while (!A.empty()) { n       long long k1 = -A.top(); n       if (k1 <= rem) { n         rem -= k1; n         pre--; n         A.pop(); n       } else n         break; n     } n     ans = min(ans, pre); n   } n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, x, y, z; n string str[5][200]; n int blcks[5], whites[5]; n int ara[] = {0, 1, 2, 3}; n int main() { n   cin >> n; n   for (int i = 0; i < 4; i++) { n     for (int j = 0; j < n; j++) cin >> str[i][j]; n     int tmp = 0; n     for (int j = 0; j < n; j++) { n       for (int k = 0; k < n; k++) { n         if ((j + k) % 2 == 0) { n           if (str[i][j][k] ==  0 ) n             blcks[i]++; n           else n             whites[i]++; n         } else { n           if (str[i][j][k] ==  1 ) n             blcks[i]++; n           else n             whites[i]++; n         } n       } n     } n   } n   int ans = 100000000; n   do { n     int tmp1 = whites[ara[0]] + blcks[ara[1]] + whites[ara[2]] + blcks[ara[3]]; n     int tmp2 = whites[ara[1]] + blcks[ara[0]] + whites[ara[3]] + blcks[ara[2]]; n     ans = min(ans, tmp1); n     ans = min(ans, tmp2); n   } while (next_permutation(ara, ara + 4)); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct SegTree { n   int sz; n   vector<int> dat; n   SegTree(int _sz = 1) { n     sz = 1; n     while (sz < _sz) sz <<= 1; n     dat.resize(sz << 1); n   } n   void upd(int pos, int val) { n     pos += sz - 1; n     dat[pos] = val; n     while (pos > 1) { n       pos >>= 1; n       dat[pos] = max(dat[pos << 1], dat[pos << 1 | 1]); n     } n   } n   int qry(int id, int l, int r, int ql, int qr) { n     if (qr < l || r < ql) return 0; n     if (ql <= l && r <= qr) return dat[id]; n     return max(qry(id << 1, l, l + r >> 1, ql, qr), n                qry(id << 1 | 1, (l + r >> 1) + 1, r, ql, qr)); n   } n   int qry(int l, int r) { return qry(1, 1, sz, l, r); } n } segx, segy; n int n, m, V; n set<pair<int, int>> tph, thp; n void plant(int p, int h) { n   vector<pair<int, int>> v; n   for (auto it = thp.begin(); it != thp.end() && it->first < h; it++) { n     v.emplace_back(it->second, it->first); n   } n   thp.emplace(h, p); n   tph.emplace(p, h); n   v.emplace_back(p, h); n   reverse(v.begin(), v.end()); n   for (auto p : v) { n     segx.upd(p.first, 0); n   } n   for (auto p : v) { n     int lis = segx.qry(p.first + 1, n) + 1; n     segx.upd(p.first, lis); n     segy.upd(p.second, lis); n   } n } n void cutdown(int id) { n   vector<pair<int, int>> v; n   for (auto it = tph.begin(); id--; it++) { n     if (id) n       v.emplace_back(it->first, it->second); n     else { n       thp.erase(make_pair(it->second, it->first)); n       segx.upd(it->first, 0); n       segy.upd(it->second, 0); n       tph.erase(it); n     } n   } n   reverse(v.begin(), v.end()); n   for (auto p : v) { n     segy.upd(p.second, 0); n   } n   for (auto p : v) { n     int lis = segy.qry(p.second + 1, V) + 1; n     segx.upd(p.first, lis); n     segy.upd(p.second, lis); n   } n } n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cout.tie(0); n   cin >> n >> m; n   segx = SegTree(n); n   segy = SegTree(V = m + 10); n   while (m--) { n     int typ, x, y; n     cin >> typ >> x; n     if (typ == 1) { n       cin >> y; n       plant(x, y + m); n     } else { n       cutdown(x); n     } n     cout << segx.qry(1, n) <<   n ; 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   int n, k; n   cin >> n >> k; n   string s; n   cin >> s; n   string res =   ; n   for (int i = 0; i < n; i++) { n     if ((s[i] -  a ) >= ( z  - s[i])) { n       if ((s[i] -  a ) <= k) { n         k -= (s[i] -  a ); n         res +=  a ; n       } else { n         res += (char)(s[i] - k); n         k = 0; n       } n     } else { n       if (( z  - s[i]) <= k) { n         k -= ( z  - s[i]); n         res +=  z ; n       } else { n         res += (char)(s[i] + k); n         k = 0; n       } n     } n   } n   if (k != 0) { n     cout << -1; n   } else { n     cout << res; n   } n } n


#include <bits/stdc++.h> n using namespace std; n int parent[300005], maxLen[300005]; n int findRoot(int u) { n   int root = u; n   while (parent[root] != root) { n     root = parent[root]; n   } n   while (parent[u] != root) { n     int y = parent[u]; n     parent[u] = root; n     u = y; n   } n   return root; n } n void unionNum(int u, int v) { n   int ru = findRoot(u), rv = findRoot(v); n   if (ru == rv) return; n   if (maxLen[ru] > maxLen[rv]) { n     parent[rv] = ru; n     maxLen[ru] = max(maxLen[ru], maxLen[ru] / 2 + maxLen[rv] / 2 + n                                      maxLen[ru] % 2 + maxLen[rv] % 2 + 1); n   } else { n     swap(ru, rv); n     parent[rv] = ru; n     maxLen[ru] = max(maxLen[ru], maxLen[ru] / 2 + maxLen[rv] / 2 + n                                      maxLen[ru] % 2 + maxLen[rv] % 2 + 1); n   } n } n int visited[300005] = {0}; n bool nVis(int &i, int n) { n   while (i <= n && visited[i] == 1) i++; n   if (i <= n) return true; n   return false; n } n void dfs(int node, vector<int> child[]) { n   map<int, int> v1, v2, d1, d2; n   stack<int> myStack; n   myStack.push(node); n   v1[node] = 1; n   d1[node] = 0; n   visited[node] = 1; n   while (!myStack.empty()) { n     int y = myStack.top(); n     myStack.pop(); n     for (size_t i = 0; i < child[y].size(); i++) { n       if (v1[child[y][i]] == 0) { n         v1[child[y][i]] = 1; n         d1[child[y][i]] = d1[y] + 1; n         myStack.push(child[y][i]); n         visited[child[y][i]] = 1; n       } n     } n   } n   for (map<int, int>::iterator it = d1.begin(); it != d1.end(); it++) { n     if (d1[node] < it->second) { n       node = it->first; n     } n   } n   myStack.push(node); n   d2[node] = 0; n   v2[node] = 1; n   while (!myStack.empty()) { n     int y = myStack.top(); n     myStack.pop(); n     parent[y] = node; n     for (size_t i = 0; i < child[y].size(); i++) { n       if (v2[child[y][i]] == 0) { n         v2[child[y][i]] = 1; n         d2[child[y][i]] = d2[y] + 1; n         maxLen[node] = max(maxLen[node], d2[child[y][i]]); n         myStack.push(child[y][i]); n       } n     } n   } n } n int main() { n   int n, m, q, a, b; n   scanf( %d%d%d , &n, &m, &q); n   for (int i = 0; i <= n; i++) { n     parent[i] = i; n     maxLen[i] = 0; n   } n   vector<int> child[n + 1]; n   for (int i = 0; i < m; i++) { n     scanf( %d%d , &a, &b); n     child[a].push_back(b); n     child[b].push_back(a); n   } n   int i = 1; n   while (nVis(i, n)) { n     dfs(i, child); n   } n   while (q--) { n     int type; n     scanf( %d , &type); n     if (type == 1) { n       scanf( %d , &a); n       printf( %d n , maxLen[findRoot(a)]); n     } else { n       scanf( %d%d , &a, &b); n       unionNum(a, b); n     } n   } n } n


#include <bits/stdc++.h> n using namespace std; n int sum[1000005]; n int main() { n   int n, x; n   cin >> n; n   for (int i = 0; i < n; ++i) n     for (int j = 0; j < n; ++j) { n       scanf( %d , &x); n       if (i == j) sum[i] = x; n     } n   int all = 0; n   for (int i = 0; i < n; ++i) all += sum[i]; n   int q, tipo; n   cin >> q; n   while (q--) { n     scanf( %d , &tipo); n     if (tipo == 3) { n       printf( %d , all % 2); n     } else { n       scanf( %d , &x); n       x--; n       if (sum[x] == 0) { n         sum[x] = 1; n         ++all; n       } else { n         sum[x] = 0; n         --all; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   long long int t; n   cin >> t; n   while (t--) { n     long long int n, s = 0; n     cin >> n; n     long long int* arr = new long long int[n]; n     for (long long int i = 0; i < n; i++) { n       cin >> arr[i]; n       s += arr[i]; n     } n     if (s == 0) n       cout <<  NO  n            <<   n ; n     else if (s > 0) { n       cout <<  YES  n            <<   n ; n       sort(arr, arr + n, greater<long long int>()); n       for (long long int i = 0; i < n; i++) cout << arr[i] <<    ; n       cout <<   n ; n     } else { n       cout <<  YES  n            <<   n ; n       sort(arr, arr + n); n       for (long long int i = 0; i < n; i++) cout << arr[i] <<    ; n       cout <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int N = 20, S = (1 << 16) + 10, V = 2048; n bitset<V> dp[S]; n int a[N], id[N]; n priority_queue<pair<int, int> > q; n int main() { n   int n, k, v = 0; n   scanf( %d%d , &n, &k); n   for (int i = 1; i <= n; ++i) scanf( %d , &a[i]), v += a[i]; n   dp[0][0] = 1; n   int u = (1 << n) - 1; n   for (int s = 0; s <= u; ++s) { n     for (int i = v / k; i >= 1; --i) n       if (dp[s][i * k]) dp[s][i] = 1; n     for (int i = 1; i <= n; ++i) n       if (!(s >> i - 1 & 1)) dp[s | (1 << i - 1)] |= dp[s] << a[i]; n   } n   puts(dp[u][1] ?  YES  :  NO ); n   if (!dp[u][1]) return 0; n   int c = 1e9; n   for (int s = u, j = 1; s;) n     for (int t = j, ok = 0; !ok; t *= k, c -= !ok) n       for (int i = 1; i <= n; ++i) { n         if (t < a[i]) continue; n         if ((s >> i - 1 & 1) && dp[s ^ (1 << i - 1)][t - a[i]]) { n           id[i] = c, s ^= 1 << i - 1, j = t - a[i], ok = 1; n           break; n         } n       } n   for (int i = 1; i <= n; ++i) q.push(make_pair(-(id[i] - c), a[i])); n   while (q.size() > 1) { n     pair<int, int> x = q.top(); n     q.pop(); n     pair<int, int> y = q.top(); n     q.pop(); n     printf( %d %d n , x.second, y.second); n     int v = x.second + y.second, u = 0; n     while (v % k == 0) v /= k, ++u; n     q.push(make_pair(-(-x.first + u), v)); n   } n   return 0; n } n


#include <bits/stdc++.h> n #pragma comment(linker,  /stack:200000000 ) n #pragma GCC optimize( Ofast ) n #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) n using namespace std; n long long n, k, s, h[200005]; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(0); n   cin >> n >> k >> s; n   h[0] = 1; n   if (k * (n - 1) < s || k > s) { n     cout <<  NO ; n     return 0; n   } n   long long t = s / k, z = s % k; n   for (long long i = 1; i <= z; i++) { n     if (h[i - 1] + t + 1 <= n) { n       h[i] = h[i - 1] + t + 1; n     } else n       h[i] = h[i - 1] - t - 1; n   } n   for (long long i = z + 1; i <= k; i++) { n     if (h[i - 1] + t <= n) { n       h[i] = h[i - 1] + t; n     } else n       h[i] = h[i - 1] - t; n   } n   cout <<  YES n ; n   for (long long i = 1; i <= k; i++) cout << h[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int inf = 0x3f3f3f3f; n inline int Read() { n   int x = 0, f = 1; n   char c = getchar(); n   while (c >  9  || c <  0 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   return x * f; n } n struct EDGE { n   int from, to, w, next; n } E[400005]; n int n, m; n int HEAD[200005]; n int dep[200005], CENT; n int pa[200005][50], maxx[200005][50]; n void ADD(int from, int to, int w) { n   E[CENT].from = from; n   E[CENT].to = to; n   E[CENT].w = w; n   E[CENT].next = HEAD[from]; n   HEAD[from] = CENT++; n } n void DFS(int u, int f) { n   pa[u][0] = f; n   for (int i = HEAD[u]; i != -1; i = E[i].next) { n     int to = E[i].to; n     if (to != f) { n       dep[to] = dep[u] + 1; n       maxx[to][0] = E[i].w; n       DFS(to, u); n     } n   } n } n int maxh; n void deal() { n   int j; n   for (j = 1; (1 << j) < n; j++) n     for (int i = 1; i <= n; i++) { n       int t = pa[i][j - 1]; n       pa[i][j] = pa[pa[i][j - 1]][j - 1]; n       maxx[i][j] = max(maxx[i][j - 1], maxx[t][j - 1]); n     } n   maxh = j - 1; n } n int swim(int x, int k, int &ma) { n   ma = 0; n   for (int i = 0; i <= maxh; i++) { n     if (k >> i & 1) { n       ma = max(ma, maxx[x][i]); n       x = pa[x][i]; n     } n   } n   return x; n } n int LCA(int x, int y) { n   if (dep[x] > dep[y]) swap(x, y); n   int ans = 0; n   y = swim(y, dep[y] - dep[x], ans); n   if (x == y) return ans; n   for (int i = maxh; i >= 0; i--) { n     if (pa[x][i] != pa[y][i]) { n       ans = max(ans, max(maxx[x][i], maxx[y][i])); n       x = pa[x][i], y = pa[y][i]; n     } n   } n   return max(ans, max(maxx[x][0], maxx[y][0])); n } n const int N = 200005; n struct edge { n   int from, to, w, next; n   int ty; n   bool operator<(const edge a) const { return w < a.w; } n }; n int pre[N], head[N]; n int cnt; n edge e[200005 + 5]; n void add(int from, int to, int w) { n   e[cnt].from = from; n   e[cnt].to = to; n   e[cnt].w = w; n   e[cnt].next = head[from]; n   e[cnt].ty = 0; n   head[from] = cnt++; n } n int find(int x) { n   if (x == pre[x]) return x; n   return pre[x] = find(pre[x]); n } n void Kruskal() { n   for (int i = 1; i <= n; ++i) pre[i] = i; n   sort(e, e + cnt); n   for (int i = 0; i < cnt; ++i) { n     int u = find(e[i].from); n     int v = find(e[i].to); n     if (u != v) { n       pre[u] = v; n       ADD(e[i].from, e[i].to, e[i].w); n       ADD(e[i].to, e[i].from, e[i].w); n       e[i].ty = 1; n     } n   } n } n int main() { n   while (scanf( %d %d , &n, &m) != EOF) { n     CENT = cnt = 0; n     for (int i = 1; i <= n; i++) head[i] = HEAD[i] = -1; n     for (int i = 1; i <= m; i++) { n       int u = Read(), v = Read(), w = Read(); n       add(u, v, w); n     } n     Kruskal(); n     DFS(1, -1); n     deal(); n     int ans = 0; n     for (int i = 0; i < cnt; i++) { n       if (!e[i].ty) { n         int u = e[i].from, v = e[i].to; n         int minn = LCA(u, v); n         if (minn == e[i].w) ans++; n       } n     } n     printf( %d n , ans); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n pair<long long, long long> a[200005]; n signed main() { n   long long n, j; n   cin >> n >> j; n   j %= 2 * n; n   long long x0, y0; n   cin >> x0 >> y0; n   for (long long i = 0; i < n; i++) { n     cin >> a[i].first >> a[i].second; n   } n   for (long long i = 1; i <= j; i++) { n     x0 = 2 * a[(i - 1) % n].first - x0; n     y0 = 2 * a[(i - 1) % n].second - y0; n   } n   cout << x0 <<     << y0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(false); n   cin.tie(nullptr); n   int T; n   cin >> T; n   while (T--) { n     long long x, y; n     cin >> x >> y; n     if (y % x == 0LL) { n       cout << x <<   n ; n     } else if (x > y) { n       cout << x + y <<   n ; n     } else { n       cout << (y / x) * x + (y - (y / x) * x) / 2 <<   n ; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int maxn=1e5+10; n  n int a[maxn]; n int f[maxn]; n int n,m,gcnt,ans; n  n int main() n { n  scanf( %d ,&n); n  for (int i=1;i<=n;i++) scanf( %d ,&a[i]),f[i]=n+1; n  gcnt=1; n  for (int i=2,prevmin=0;i<=n;i++) n  { n   if (a[i-1]!=a[i]) gcnt++; n   f[a[i-1]]=min(f[a[i]]-1+(a[i-1]==a[i]),prevmin); n   prevmin=min(prevmin,f[a[i-1]]); n  } n  ans=n; n  for (int i=0;i<=n;i++) ans=min(ans,f[i]+gcnt); n  printf( %d n ,ans); n } n  n


#include <bits/stdc++.h> n using namespace std; n char s[2005]; n const int mod = 1e9 + 7; n int n, k, t[2005]; n int a[2005][2005], b[2005][2005]; n int mp(const int &a, const int &b) { return (int)(1ll * a * b % mod); } n int add(const int &a, const int &b) { return (a + b) % mod; } n int main() { n   scanf( %d %d %s , &n, &k, s); n   int i, j, u, v, d, e; n   for (i = 0; i < n; i++) t[i + 1] = s[i] -  a ; n   for (i = 0; i <= n + 1; i++) a[i][0] = 1; n   for (i = n; i > 0; i--) { n     v = n - i; n     for (j = 0; j <= k; j++) { n       b[i][j] = add(b[i + 1][j], mp(t[i], a[i + 1][j])); n       a[i][j] = add(a[i][j], b[i][j]); n       for (u = 0; u <= v / 2; u++) { n         e = j - (v - u + 1) * (u + 1); n         if (e < 0) break; n         if (2 * u == v) n           a[i][j] = add(mp(25 - t[i + u], a[i + u + 1][e]), a[i][j]); n         else { n           a[i][j] = add(mp(25 - t[i + u], a[i + u + 1][e]), a[i][j]); n           a[i][j] = add(mp(25 - t[i + v - u], a[i + v - u + 1][e]), a[i][j]); n         } n       } n     } n   } n   printf( %d n , a[1][k]); n } n


#include <bits/stdc++.h> n using namespace std; n bool isPrime(int n) { n   if (n <= 1) return false; n   if (n <= 3) return true; n   if (n % 2 == 0 || n % 3 == 0) return false; n   for (int i = 5; i * i <= n; i = i + 6) n     if (n % i == 0 || n % (i + 2) == 0) return false; n   return true; n } n int nextPrime(int N) { n   if (N <= 1) return 2; n   int prime = N; n   bool found = false; n   while (!found) { n     prime++; n     if (isPrime(prime)) found = true; n   } n   return prime; n } n long long int powerexp(long long int x, long long int n) { n   long long int result = 1; n   while (n) { n     if (n & 1) result = result * x % 1000000007; n     n = n / 2; n     x = x * x % 1000000007; n   } n   return result; n } n int gcd(int a, int b) { n   if (a == 0) return b; n   if (b == 0) return a; n   if (a == b) return a; n   if (a > b) return gcd(a - b, b); n   return gcd(a, b - a); n } n int largest_divisor(long long int n) { n   long long int i; n   for (i = n / 2; i >= 1; i--) { n     if (n % i == 0) { n       return i; n     } n   } n   return 0; n } n bool checkPalindrome(string str) { n   int n = str.length(); n   int count = 0; n   for (int i = 0; i < n / 2; ++i) n     if (str[i] != str[n - i - 1]) ++count; n   return (count <= 1); n } n bool countDivisors(long long int n) { n   int cnt = 0; n   for (int i = 3; i <= sqrt(n); i = i + 2) { n     if (n % i == 0) { n       return true; n     } n   } n   return false; n } n long long int factorial(long long int n) { n   if (n == 0) return 0; n   return n * factorial(n - 1); n } n long long int lcm(long long int a, long long int b) { n   return (a / gcd(a, b)) * b; n } n void fastIO() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n int main() { n   long long int t = 1; n   while (t--) { n     long long int n; n     cin >> n; n     long long int a[n]; n     long long int z = 0, p = 0, neg = 0; n     long long int res = 0; n     for (long long int i = 0; i < n; i++) { n       cin >> a[i]; n       if (a[i] == 0) { n         res++; n         z++; n       } else if (a[i] > 0) { n         res = res + (a[i] - 1); n         p++; n       } else { n         res = res + (-1 - a[i]); n         neg++; n       } n     } n     if (neg % 2 == 1 && z == 0) { n       res += 2; n     } n     cout << res << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = (int)2e9; n int main() { n   int k, n, m, q; n   cin >> k >> n >> m >> q; n   map<string, map<string, int>> rec; n   vector<string> art(n); n   map<int, map<string, int>> b; n   for (int(i) = (int)(0); (i) < (int)n; ++(i)) cin >> art[i]; n   sort(art.begin(), art.end()); n   string s; n   getline(cin, s); n   for (int(i) = (int)(0); (i) < (int)m; ++(i)) { n     getline(cin, s); n     int p = s.find( : ); n     string ar = s.substr(0, p); n     s = s.substr(p + 2, s.size()); n     s +=  ,  ; n     while (s !=   ) { n       p = s.find(   ); n       string t = s.substr(0, p); n       s = s.substr(p + 1, s.size()); n       p = s.find( , ); n       string t2 = s.substr(0, p); n       s = s.substr(p + 2, s.size()); n       int col = atoi(t2.c_str()); n       rec[ar][t] = col; n     } n   } n   for (int(i) = (int)(0); (i) < (int)q; ++(i)) { n     int t; n     cin >> t; n     string st; n     cin >> st; n     b[t][st]++; n     for (map<string, map<string, int>>::iterator it1 = rec.begin(); n          it1 != rec.end(); ++it1) { n       bool ok = true; n       for (map<string, int>::iterator it2 = it1->second.begin(); n            it2 != it1->second.end(); ++it2) { n         if (b[t].find(it2->first) == b[t].end() || n             b[t][it2->first] < it2->second) { n           ok = false; n           break; n         } n       } n       if (ok) { n         for (map<string, int>::iterator it2 = it1->second.begin(); n              it2 != it1->second.end(); ++it2) { n           b[t][it2->first] -= it2->second; n           if (b[t][it2->first] == 0) b[t].erase(it2->first); n         } n         b[t][it1->first]++; n         break; n       } n     } n   } n   for (int(i) = (int)(1); (i) < (int)k + 1; ++(i)) { n     cout << b[i].size() <<   n ; n     for (map<string, int>::iterator it = b[i].begin(); it != b[i].end(); ++it) n       if (it->second) cout << it->first <<     << it->second <<   n ; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int mx = 3005; n const int N = 100005; n int n, a[mx][mx], b[mx][mx], ans[N]; n pair<int, int> t[N]; n inline int check(int lx, int rx, int ly, int ry) { n   int cnt = b[rx][ry]; n   if (lx) cnt -= b[lx - 1][ry]; n   if (ly) cnt -= b[rx][ly - 1]; n   if (lx && ly) cnt += b[lx - 1][ly - 1]; n   int need = (rx - lx + 1) * (ry - ly + 1); n   if (cnt != need) return -1; n   if (ly) n     if (a[rx][ly] == a[rx][ly - 1]) return -1; n   if (lx) n     if (a[lx][ry] == a[lx - 1][ry]) return -1; n   for (int i = lx; i <= rx; i++) n     if (a[i][ry] == a[i][ry + 1]) return false; n   for (int i = ly; i <= ry; i++) n     if (a[rx][i] == a[rx + 1][i]) return false; n   return true; n } n int main() { n   int n; n   scanf( %d , &n); n   for (int q = (0); q <= ((n)-1); q++) { n     int x1, y1, x2, y2; n     scanf( %d%d%d%d , &x1, &y1, &x2, &y2); n     for (int i = x1; i < x2; i++) n       for (int j = y1; j < y2; j++) a[i][j] = q + 1, b[i][j] = 1; n     t[q] = make_pair(x1, y1); n   } n   for (int i = (0); i <= ((mx)-1); i++) n     for (int j = (0); j <= ((mx)-1); j++) { n       if (i - 1 >= 0) b[i][j] += b[i - 1][j]; n       if (j - 1 >= 0) b[i][j] += b[i][j - 1]; n       if (i - 1 >= 0 && j - 1 >= 0) b[i][j] -= b[i - 1][j - 1]; n     } n   random_shuffle(t, t + n); n   for (int q = (0); q <= ((n)-1); q++) { n     int si = t[q].first, sj = t[q].second; n     int ei = si, ej = sj; n     while (ei < mx && ej < mx) { n       int ch = check(si, ei, sj, ej); n       if (ch == -1) break; n       if (ch == 1) { n         for (int i = si; i <= ei; i++) n           for (int j = sj; j <= ej; j++) ans[a[i][j]] = 1; n         vector<int> res; n         for (int i = 0; i < N; i++) n           if (ans[i]) res.push_back(i); n         printf( YES %d n , (int)((res).end() - (res).begin())); n         for (int i = (0); i <= (((int)((res).end() - (res).begin())) - 1); n              i++) { n           if (i) printf(   ); n           printf( %d , res[i]); n         } n         puts(  ); n         exit(0); n       } n       ei++; n       ej++; n     } n   } n   puts( NO ); n   return 0; n } n


#include<bits/stdc++.h> n using namespace std; n  n #ifndef ONLINE_JUDGE n   #include  Mohit.h  n #endif n  n typedef long long ll; n typedef long double ld; n #define endl   n  n #define rep(i,n)  for(ll i = 0; i < (n); ++i) n #define repA(i, a, n)  for(ll i = a; i <= (n); ++i) n #define repD(i, a, n)  for(ll i = a; i >= (n); --i) n #define trav(a, x) for(auto& a : x) n #define all(x) x.begin(), x.end() n #define ff first n #define ss second n #define pb push_back n typedef vector<ll> vll; n typedef vector<pair<ll,ll>> vpl; n const ld PI = 4*atan((ld)1); n const ll INF = 1e18; n const ll mod = 1e9+7; n  n int main() n { n     ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); n     ll tt=1; n         cin >> tt; n     repA(qq,1,tt){ n         ll n; cin >> n; n         vll a(n); n         rep(i,n){ n             cin >> a[i]; n         } n  n         ll ans = INF; n         ll s1=a[0],s2=0,c1=1,c2=0; n         ll m1=a[0],m2=INF; n  n         repA(i,1,n-1){ n             if(i&1){ n                 m2 = min(m2,a[i]); s2 += a[i]; n                 ll t1 = (s2-m2)+(n-(c2))*m2; n                 ll t2 = (s1-m1)+(n-(c1-1))*m1; n                 ans = min(ans,t1+t2); n                 c2++; n             }else{ n                 m1 = min(m1,a[i]); s1 += a[i]; n                 ll t1 = (s1-m1)+(n-(c1))*m1; n                 ll t2 = (s2-m2)+(n-(c2-1))*m2; n                 ans = min(ans,t1+t2); n                 c1++; n             } n         } n  n         cout << ans << endl; n     } n } n


#include <bits/stdc++.h> n using namespace std; n void solve() { n   long long n, z = 0; n   cin >> n; n   long long a[n], b[n]; n   for (long long i = 0; i < n; i++) { n     cin >> a[i]; n   } n   for (long long i = 0; i < n; i++) { n     cin >> b[i]; n   } n   vector<long long> neg, pos; n   for (long long i = 0; i < n; i++) { n     if (a[i] == b[i]) z++; n     if ((a[i] - b[i]) >= 0) n       pos.push_back(a[i] - b[i]); n     else n       neg.push_back(b[i] - a[i]); n   } n   if (!pos.empty()) sort(pos.begin(), pos.end()); n   if (!neg.empty()) sort(neg.begin(), neg.end()); n   long long ans = 0, cnt = 0, j = (long long)neg.size() - 1, n             k = (long long)pos.size() - 1; n   ans += (k * (k + 1)) / 2; n   ans -= (z * (z - 1)) / 2; n   while (j >= 0 && !pos.empty()) { n     if (k >= 0 && neg[j] < pos[k]) { n       cnt++; n       k--; n     } else { n       ans += cnt; n       j--; n     } n   } n   cout << ans << endl; n   return; n } n int main() { n   long long t; n   t = 1; n   while (t--) { n     solve(); n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 1e5 + 10; n int a[MAXN]; n bool mark[MAXN]; n void add(int v, int u) { n   while (mark[u] == false) { n     cout << v <<     << u << endl; n     mark[u] = true; n     v = a[v]; n     u = a[u]; n   } n } n int dfs(int x) { n   int ret = 1; n   mark[x] = true; n   if (mark[a[x]] == false) ret += dfs(a[x]); n   return ret; n } n int main() { n   ios::sync_with_stdio(false); n   int n; n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> a[i]; n   for (int i = 1; i <= n; i++) n     if (a[i] == i) { n       cout <<  YES  << endl; n       for (int j = 1; j <= n; j++) n         if (j != i) cout << i <<     << j << endl; n       return 0; n     } n   int p = -1; n   for (int i = 1; i <= n; i++) n     if (mark[i] == false) { n       int x = dfs(i); n       if (x % 2) { n         cout <<  NO  << endl; n         return 0; n       } else if (x == 2) n         p = i; n     } n   if (p == -1) { n     cout <<  NO  << endl; n     return 0; n   } n   fill(mark, mark + MAXN, false); n   cout <<  YES  << endl; n   cout << p <<     << a[p] << endl; n   mark[p] = mark[a[p]] = true; n   for (int i = 1; i <= n; i++) n     if (!mark[i]) add(p, i); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void task(); n int main() { n   srand(time(0)); n   task(); n   return 0; n } n const int INF = 0x3f3f3f3f; n const int N = 1e6 + 10; n const int M = 1e7 + 100; n const int OLOLO = 0xFFFFFFFF; n int n, k; n long long int cnt[N]; n set<int> vals; n long long int ans; n vector<int> masks; n void task() { n   cin >> n >> k; n   for (int i = 0; i < n; ++i) { n     int a; n     cin >> a; n     ++cnt[a]; n     vals.insert(a); n   } n   for (int i = 0; i < (1 << 14); ++i) { n     if (__builtin_popcount(i) == k) { n       masks.push_back(i); n     } n   } n   for (int val : vals) { n     for (int mask : masks) { n       int result = val & (mask ^ OLOLO); n       result |= (val ^ OLOLO) & mask; n       if (result < N) { n         if (val == result) { n           ans += cnt[val] * (cnt[val] - 1); n         } else { n           ans += cnt[val] * cnt[result]; n         } n       } n     } n   } n   cout << ans / 2; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int mx = 0, a = 0, n; n   string s; n   cin >> n; n   while (cin >> s) { n     for (char c : s) n       if (isupper(c)) a++; n     mx = max(mx, a), a = 0; n   } n   cout << mx << endl; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n inline void ckmax(T& x, T y) { n   x = (y > x ? y : x); n } n template <typename T> n inline void ckmin(T& x, T y) { n   x = (y < x ? y : x); n } n namespace Fread { n const int SIZE = 1 << 21; n char buf[SIZE], *S, *T; n inline char getchar() { n   if (S == T) { n     T = (S = buf) + fread(buf, 1, SIZE, stdin); n     if (S == T) return EOF; n   } n   return *S++; n } n }  // namespace Fread n namespace Fwrite { n const int SIZE = 1 << 21; n char buf[SIZE], *S = buf, *T = buf + SIZE; n inline void flush() { n   fwrite(buf, 1, S - buf, stdout); n   S = buf; n } n inline void putchar(char c) { n   *S++ = c; n   if (S == T) flush(); n } n struct NTR { n   ~NTR() { flush(); } n } ztr; n }  // namespace Fwrite n namespace Fastio { n struct Reader { n   template <typename T> n   Reader& operator>>(T& x) { n     char c = Fread ::getchar(); n     T f = 1; n     while (c <  0  || c >  9 ) { n       if (c ==  - ) f = -1; n       c = Fread ::getchar(); n     } n     x = 0; n     while (c >=  0  && c <=  9 ) { n       x = x * 10 + (c -  0 ); n       c = Fread ::getchar(); n     } n     x *= f; n     return *this; n   } n   Reader& operator>>(char& c) { n     c = Fread ::getchar(); n     while (c ==   n  || c ==    ) c = Fread ::getchar(); n     return *this; n   } n   Reader& operator>>(char* str) { n     int len = 0; n     char c = Fread ::getchar(); n     while (c ==   n  || c ==    ) c = Fread ::getchar(); n     while (c !=   n  && c !=    ) { n       str[len++] = c; n       c = Fread ::getchar(); n     } n     str[len] =   0 ; n     return *this; n   } n   Reader() {} n } cin; n const char endl =   n ; n struct Writer { n   template <typename T> n   Writer& operator<<(T x) { n     if (x == 0) { n       Fwrite ::putchar( 0 ); n       return *this; n     } n     if (x < 0) { n       Fwrite ::putchar( - ); n       x = -x; n     } n     static int sta[45]; n     int top = 0; n     while (x) { n       sta[++top] = x % 10; n       x /= 10; n     } n     while (top) { n       Fwrite ::putchar(sta[top] +  0 ); n       --top; n     } n     return *this; n   } n   Writer& operator<<(char c) { n     Fwrite ::putchar(c); n     return *this; n   } n   Writer& operator<<(char* str) { n     int cur = 0; n     while (str[cur]) Fwrite ::putchar(str[cur++]); n     return *this; n   } n   Writer& operator<<(const char* str) { n     int cur = 0; n     while (str[cur]) Fwrite ::putchar(str[cur++]); n     return *this; n   } n   Writer() {} n } cout; n }  // namespace Fastio n const int MAXN = 1e5; n const int MOD = 1e9 + 7; n inline int mod1(int x) { return x < MOD ? x : x - MOD; } n inline int mod2(int x) { return x < 0 ? x + MOD : x; } n inline void add(int& x, int y) { x = mod1(x + y); } n inline void sub(int& x, int y) { x = mod2(x - y); } n int n, goal, a[MAXN + 5]; n int pw2[MAXN + 5]; n int dp[MAXN + 5][2][2][2]; n inline int calc0(int i, int have_1) { n   if (have_1) assert(i); n   return have_1 ? pw2[i - 1] : pw2[i]; n } n inline int calc1(int i, int have_1) { n   if (have_1) assert(i); n   return have_1 ? pw2[i - 1] : 0; n } n int main() { n   Fastio ::cin >> n >> goal; n   for (int i = 1; i <= n; ++i) { n     Fastio ::cin >> a[i]; n   } n   pw2[0] = 1; n   for (int i = 1; i <= n; ++i) pw2[i] = pw2[i - 1] * 2 % MOD; n   dp[0][0][0][0] = 1; n   for (int i = 1; i <= n; ++i) { n     for (int have_r1 = 0; have_r1 <= 1; ++have_r1) { n       for (int have_b1 = 0; have_b1 <= 1; ++have_b1) { n         for (int s = 0; s <= 1; ++s) n           if (dp[i - 1][have_r1][have_b1][s]) { n             if (a[i] != 1) { n               add(dp[i][have_r1][have_b1][s], n                   (long long)dp[i - 1][have_r1][have_b1][s] * n                       calc1(i - 1, have_b1) % MOD); n               add(dp[i][have_r1 | 1][have_b1][s ^ 1], n                   (long long)dp[i - 1][have_r1][have_b1][s] * n                       calc0(i - 1, have_b1) % MOD); n             } n             if (a[i] != 0) { n               add(dp[i][have_r1][have_b1][s], n                   (long long)dp[i - 1][have_r1][have_b1][s] * n                       calc1(i - 1, have_r1) % MOD); n               add(dp[i][have_r1][have_b1 | 1][s ^ 1], n                   (long long)dp[i - 1][have_r1][have_b1][s] * n                       calc0(i - 1, have_r1) % MOD); n             } n           } n       } n     } n   } n   int ans = 0; n   for (int have_r1 = 0; have_r1 <= 1; ++have_r1) { n     for (int have_b1 = 0; have_b1 <= 1; ++have_b1) { n       add(ans, dp[n][have_r1][have_b1][goal]); n     } n   } n   Fastio ::cout << ans << Fastio ::endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n bool debug = 0; n int n, m, k; n int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; n long long ln, lk, lm; n int x[105], y[105], r[105]; n string vs[2]; n void add(int id, int i, int x) { n   string str =  (((1-abs((t- ; n   str += to_string(i); n   str +=  )))+abs((abs((t- ; n   str += to_string(i); n   str +=  ))-1)))* ; n   str += to_string(x); n   str +=  ) ; n   if (vs[id] ==   ) n     vs[id] = str; n   else { n     vs[id] =  (  + vs[id] +  +  + str +  ) ; n   } n } n void fmain() { n   scanf( %d , &n); n   for (int(i) = 0; (i) < (int)(2); (i)++) vs[i] =   ; n   for (int(i) = 0; (i) < (int)(n); (i)++) { n     scanf( %d%d%d , x + i, y + i, r + i); n     add(0, i + 1, x[i] / 2); n     add(1, i + 1, y[i] / 2); n   } n   for (int(i) = 0; (i) < (int)(2); (i)++) cout << vs[i] << endl; n } n int main() { n   fmain(); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline int bit(int x, int i) { return (x >> i) & 1; } n inline int two(int x) { return 1 << x; } n int n, s[110], m, team[110], dp[20][1 << 20]; n char op[110]; n bool vis[20][1 << 20]; n int go(int mi, int ban) { n   if (mi == m) return 0; n   int &ret = dp[mi][ban]; n   if (vis[mi][ban]) return ret; n   vis[mi][ban] = 1; n   ret = -514514514; n   for (int i = 0; i < (int)(n); i++) n     if (!bit(ban, i)) { n       int now = go(mi + 1, ban | two(i)); n       int str = (op[mi] ==  p  ? s[i] : 0); n       if (mi + 1 < m && team[mi + 1] != team[mi]) n         now = str - now; n       else n         now = str + now; n       ret = max(ret, now); n     } n   return ret; n } n int main() { n   scanf( %d , &(n)); n   for (int i = 0; i < (int)(n); i++) scanf( %d , &(s[i])); n   scanf( %d , &(m)); n   for (int i = 0; i < (int)(m); i++) scanf(  %c %d , op + i, team + i); n   sort(s, s + n, greater<int>()); n   n = m; n   int ans = go(0, 0); n   if (team[0] == 2) ans *= -1; n   printf( %d n , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int a[3], ans; n   cin >> a[0] >> a[1] >> a[2]; n   sort(a, a + 3); n   ans = (a[1] - a[0]) + (a[2] - a[1]); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n, numero; n   cin >> n; n   if (n < 10) n     cout << n; n   else { n     n -= 9; n     if (n < 181) { n       if (n % 2 == 0) { n         n /= 2; n         n -= 1; n         n %= 10; n         cout << n; n       } else { n         n = (n + 1) / 2; n         cout << ((n - 1) / 10) + 1; n       } n     } else { n       n -= 180; n       numero = ((n - 1) / 3) + 1; n       numero += 99; n       n = n % 3; n       switch (n) { n         case 0: n           cout << numero % 10; n           break; n         case 1: n           cout << numero / 100; n           break; n         default: n           cout << (numero % 100) / 10; n           break; n       } n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, p[3000], k, m = 0, c; n int main() { n   cin >> n; n   for (int i = 1; i <= n; i++) cin >> p[i]; n   for (int i = 1; i <= n; i++) { n     k = p[i]; n     c = 1; n     while (k > 0) { n       c++; n       k = p[k]; n     } n     m = max(c, m); n   } n   cout << m; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int n; n   char st[200010]; n   cin >> n >> st; n   queue<int> d, r; n   for (int i = 0; i < n; i++) { n     if (st[i] ==  D ) n       d.push(i); n     else n       r.push(i); n   } n   while (!r.empty() && !d.empty()) { n     int R = r.front(), D = d.front(); n     if (R < D) { n       r.pop(); n       d.pop(); n       r.push(n + R); n     } else { n       d.pop(); n       r.pop(); n       d.push(n + D); n     } n   } n   if (!r.empty()) n     cout <<  R  << endl; n   else n     cout <<  D  << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long w, h; n   cin >> w >> h; n   long long res = 0; n   if (w == 1 || h == 1) { n     cout <<  0  << endl; n     return 0; n   } n   for (long long i = 1; i < h; i++) { n     for (long long j = 1; j < w; j++) res += min(i, h - i) * min(j, w - j); n   } n   cout << res << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   ; n   long long n, k; n   cin >> n >> k; n   priority_queue<long long, vector<long long>, greater<long long>> priq; n   long long cur = 0, co = 0; n   map<long long, long long> ma; n   while (n > 0) { n     if (n % 2) { n       ma[cur]++; n       co++; n     } n     n /= 2; n     cur++; n   } n   if (co > k) { n     cout <<  No  << endl; n     return 0; n   } n   for (long long i = 63; i >= -63; i--) { n     long long temp = ma[i] + co; n     if (temp <= k) { n       co += ma[i]; n       ma[i - 1] += (2 * ma[i]); n       ma[i] = 0; n     } else { n       break; n     } n   } n   for (long long i = 63; i >= -63; i--) n     for (long long j = 0; j < ma[i]; j++) { n       priq.push(i); n     } n   while (priq.size() < k) { n     long long x = priq.top(); n     priq.pop(); n     priq.push(x - 1); n     priq.push(x - 1); n   } n   vector<long long> ans; n   cout <<  Yes  << endl; n   while (!priq.empty()) { n     long long x = priq.top(); n     ans.push_back(x); n     priq.pop(); n   } n   sort(ans.begin(), ans.end(), greater<long long>()); n   for (long long i = 0; i < ans.size(); i++) cout << ans[i] <<    ; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n string start[1791791]; n string fin[1791791]; n pair<int, int> diffs[1791791]; n int is_in(string a, string b) { n   string s = a +  #  + b; n   int n = (int)s.length(); n   vector<int> pi(n); n   for (int i = 1; i < n; ++i) { n     int j = pi[i - 1]; n     while (j > 0 && s[i] != s[j]) j = pi[j - 1]; n     if (s[i] == s[j]) ++j; n     pi[i] = j; n   } n   for (int i = (int)a.length(); i < pi.size(); ++i) { n     if (pi[i] == a.length()) { n       return i - 2 * (int)a.length(); n     } n   } n   return -1; n } n int main() { n   ios_base::sync_with_stdio(0); n   int n; n   cin >> n; n   for (int i = 0; i < n; ++i) { n     cin >> start[i]; n   } n   for (int i = 0; i < n; ++i) { n     cin >> fin[i]; n   } n   for (int i = 0; i < n; ++i) { n     int first_diff = -1; n     for (int ii = 0; ii < start[i].length(); ++ii) { n       if (start[i][ii] != fin[i][ii]) { n         first_diff = ii; n         break; n       } n     } n     int last_diff = -1; n     for (int ii = 0; ii < start[i].length(); ++ii) { n       if (start[i][ii] != fin[i][ii]) { n         last_diff = ii; n       } n     } n     diffs[i] = {first_diff, last_diff}; n   } n   int one_with_diff = -1; n   for (int i = 0; i < n; ++i) { n     if (diffs[i].first != -1) { n       one_with_diff = i; n       break; n     } n   } n   for (int i = 0; i < n; ++i) { n     if (diffs[i].first != -1 && diffs[one_with_diff].first != -1 && n         i != one_with_diff) { n       if (diffs[i].second - diffs[i].first != n           diffs[one_with_diff].second - diffs[one_with_diff].first) { n         cout <<  NO ; n         return 0; n       } n       for (int ii = 0; ii <= diffs[i].second - diffs[i].first; ++ii) { n         if (start[i][diffs[i].first + ii] != n                 start[one_with_diff][diffs[one_with_diff].first + ii] || n             fin[i][diffs[i].first + ii] != n                 fin[one_with_diff][diffs[one_with_diff].first + ii]) { n           cout <<  NO ; n           return 0; n         } n       } n     } n   } n   int same_pref_len = 0; n   for (int pref_len = 1;; ++pref_len) { n     char pref =  # ; n     bool ok = 1; n     for (int i = 0; i < n; ++i) { n       if (diffs[i].first != -1) { n         if (diffs[i].first < pref_len) { n           ok = 0; n           break; n         } n         if (pref ==  # ) { n           pref = start[i][diffs[i].first - pref_len]; n         } else { n           if (pref != start[i][diffs[i].first - pref_len]) { n             ok = 0; n             break; n           } n         } n       } n     } n     if (!ok) { n       break; n     } n     same_pref_len += 1; n   } n   int same_suff_len = 0; n   for (int suff_len = 1;; ++suff_len) { n     char suff =  # ; n     bool ok = 1; n     for (int i = 0; i < n; ++i) { n       if (diffs[i].first != -1) { n         if (diffs[i].second + suff_len >= start[i].length()) { n           ok = 0; n           break; n         } n         if (suff ==  # ) { n           suff = start[i][diffs[i].second + suff_len]; n         } else { n           if (suff != start[i][diffs[i].second + suff_len]) { n             ok = 0; n             break; n           } n         } n       } n     } n     if (!ok) { n       break; n     } n     same_suff_len += 1; n   } n   string from, to; n   for (int i = 0; i < n; ++i) { n     if (diffs[i].first != -1) { n       from =   ; n       for (int j = diffs[i].first - same_pref_len; n            j <= diffs[i].second + same_suff_len; ++j) { n         from += start[i][j]; n       } n       to =   ; n       for (int j = diffs[i].first - same_pref_len; n            j <= diffs[i].second + same_suff_len; ++j) { n         to += fin[i][j]; n       } n       break; n     } n   } n   for (int i = 0; i < n; ++i) { n     if (diffs[i].first == -1) { n       if (is_in(from, start[i]) != -1) { n         cout <<  NO ; n         return 0; n       } n     } else { n       if (is_in(from, start[i]) != diffs[i].first - same_pref_len) { n         cout <<  NO ; n         return 0; n       } n     } n   } n   cout <<  YES  << endl; n   cout << from << endl; n   cout << to << endl; n } n


#include <bits/stdc++.h> n using namespace std; n const int INF = 0x3f3f3f3f; n int dist[10010], DD[30][30]; n int s[10010]; n vector<int> all; n vector<int> a; n queue<int> q; n int dp[1 << 20]; n int n; n void calc(int x) { n   memset(dist, -1, sizeof(dist)); n   q.push(x), dist[x] = 0; n   while (!q.empty()) { n     int x = q.front(); n     q.pop(); n     for (int i = 0; i < all.size(); i++) { n       int del = all[i]; n       if (x - del > 0 && dist[x - del] == -1) { n         dist[x - del] = dist[x] + 1; n         q.push(x - del); n       } n       if (x + del <= n + 1 && dist[x + del] == -1) { n         dist[x + del] = dist[x] + 1; n         q.push(x + del); n       } n     } n   } n } n void chmin(int &x, int y) { x = min(x, y); } n int main() { n   int k, l; n   scanf( %d%d%d , &n, &k, &l); n   for (int i = 0; i < k; i++) { n     int x; n     scanf( %d , &x); n     s[x] = 1; n   } n   while (l--) { n     int x; n     scanf( %d , &x); n     all.push_back(x); n   } n   for (int i = n + 1; i >= 1; i--) { n     s[i] ^= s[i - 1]; n   } n   for (int i = 1; i <= n + 1; i++) { n     if (s[i]) a.push_back(i); n   } n   for (int i = 0; i < a.size(); i++) { n     int x = a[i]; n     calc(x); n     for (int j = 0; j < a.size(); j++) { n       DD[i][j] = dist[a[j]]; n     } n   } n   int N = a.size(); n   memset(dp, 0x3f, sizeof(dp)); n   dp[0] = 0; n   for (int mask = 0; mask < (1 << N) - 1; mask++) { n     if (dp[mask] == INF) continue; n     int x = 0; n     while (mask >> x & 1) x++; n     for (int y = x + 1; y < N; y++) { n       if (mask >> y & 1) continue; n       if (DD[x][y] == -1) continue; n       chmin(dp[mask | 1 << x | 1 << y], dp[mask] + DD[x][y]); n     } n   } n   printf( %d n , dp[(1 << N) - 1] == INF ? -1 : dp[(1 << N) - 1]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n struct debugger { n   template <typename T> n   debugger& operator,(const T& v) { n     cerr << v <<    ; n     return *this; n   } n } dbg; n int inf = (1 << 20); n int dp[(1 << 21) + 10]; n void F(int x, int y) { n   x += inf; n   y += inf; n   dp[x]++; n   if (x == y) return; n   dp[y]++; n   while (x / 2 != y / 2) { n     if (x % 2 == 0) dp[x + 1]++; n     if (y % 2 == 1) dp[y - 1]++; n     x /= 2; n     y /= 2; n   } n   return; n } n int ar[400010]; n int n, k; n int func(int x) { n   int sm = 0; n   x += inf; n   while (x > 1) { n     sm += dp[x]; n     x /= 2; n   } n   return sm; n } n int chk(int x) { n   int sm = 0; n   for (int i = x; i <= ar[n - 1]; i += x) sm += func(i); n   if (sm == n) return 1; n   return 0; n } n int main() { n   cin >> n >> k; n   for (int i = (0), _n = (n); i < _n; i++) cin >> ar[i]; n   sort(ar, ar + n); n   if (ar[0] <= k) { n     cout << ar[0] << endl; n     return 0; n   } n   for (int i = (0), _n = (n); i < _n; i++) F(ar[i] - k, ar[i]); n   int rs = k; n   for (int i = (2), _n = (ar[0]); i <= _n; i++) n     if (chk(i)) rs = i; n   cout << rs << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t, n, i, j, k, tmp; n   int a, b, c; n   cin >> a >> b >> c; n   int p; n   if ((c - b + a) % 2 != 0) { n     printf( Impossible n ); n     return 0; n   } n   p = (c - b + a) / 2; n   int count1 = 0; n   if (p == 0) { n     count1++; n   } n   if (a - p == 0) { n     count1++; n   } n   if (b - (a - p) == 0) { n     count1++; n   } n   if (p >= 0 && a - p >= 0 && b - (a - p) >= 0 && count1 <= 1) { n     cout << a - p <<     << b - (a - p) <<     << p << endl; n   } else { n     printf( Impossible n ); n   } n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int n, k; n   cin >> n >> k; n   map<int, int> m; n   int c[7000][2]; n   int a[n + 1]; n   int b[n + 1]; n   int mx; n   for (int i = 0; i < n; i++) { n     cin >> a[i]; n     m[a[i]]++; n     mx = max(mx, m[a[i]]); n   } n   if (mx > k) { n     cout <<  NO  << endl; n     return 0; n   } n   int x = 0; n   for (auto i : m) { n     c[i.first][0] = x; n     x = x + i.second; n   } n   cout <<  YES  << endl; n   for (int i = 0; i < n; i++) { n     cout << (c[a[i]][0] % k) + 1 <<    ; n     c[a[i]][0]++; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   ios::sync_with_stdio(0); n   cin.tie(0); n   cout.tie(0); n   ; n   double n, m, a, b, x = 1000000, y; n   cin >> n >> m; n   while (n--) { n     cin >> a >> b; n     y = a / b; n     x = min(x, y); n   } n   printf( %.8lf n , m * x); n } n


#include <bits/stdc++.h> n using namespace std; n long long bigmod(long long p, long long e, long long M) { n   long long ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p) % M; n     p = (p * p) % M; n   } n   return ret; n } n long long extended(long long a, long long b, long long &x, long long &y) { n   a = abs(a); n   b = abs(b); n   long long g, x1, y1; n   if (!b) { n     x = 1; n     y = 0; n     g = a; n     return g; n   } n   g = extended(b, a % b, x1, y1); n   x = y1; n   y = x1 - (a / b) * y1; n   return g; n } n long long modinverse(long long a, long long M) { return bigmod(a, M - 2, M); } n long long bpow(long long p, long long e) { n   long long ret = 1; n   for (; e > 0; e >>= 1) { n     if (e & 1) ret = (ret * p); n     p = (p * p); n   } n   return ret; n } n int toInt(string s) { n   int sm; n   stringstream ss(s); n   ss >> sm; n   return sm; n } n int toLlint(string s) { n   long long int sm; n   stringstream ss(s); n   ss >> sm; n   return sm; n } n string convertInt(long long number) { n   if (number == 0) return  0 ; n   string temp =   ; n   string returnvalue =   ; n   while (number > 0) { n     temp += number % 10 + 48; n     number /= 10; n   } n   for (int i = 0; i < temp.length(); i++) n     returnvalue += temp[temp.length() - i - 1]; n   return returnvalue; n } n long long ncr[1001][1001]; n void NCR() { n   for (int i = 1; i <= 1000; i++) { n     ncr[i][i] = 1; n     ncr[i][0] = 1; n     for (int r = 1; r < i; r++) { n       ncr[i][r] = (ncr[i - 1][r] + ncr[i - 1][r - 1]) % 1000000007; n     } n   } n } n vector<int> vec[100009]; n long long n; n long long dp[100005][2], ar[100009]; n void dfs(int V, int pv) { n   dp[V][0] = 1 - ar[V]; n   dp[V][1] = ar[V]; n   for (int i = 0; i < vec[V].size(); i++) { n     long long old[2]; n     memset(old, 0, sizeof(old)); n     int v = vec[V][i]; n     if (v == pv) continue; n     old[0] = dp[V][0]; n     old[1] = dp[V][1]; n     dp[V][0] = dp[V][1] = 0; n     dfs(v, V); n     dp[V][0] += (old[0] * dp[v][1]) % 1000000007; n     dp[V][1] += (old[1] * dp[v][1]) % 1000000007; n     dp[V][1] += (old[1] * dp[v][0]) % 1000000007; n     dp[V][1] += (old[0] * dp[v][1]) % 1000000007; n     dp[V][0] += (old[0] * dp[v][0]) % 1000000007; n   } n } n int main() { n   cin >> n; n   for (int i = 1; i < n; i++) { n     int v; n     cin >> v; n     vec[i].push_back(v); n     vec[v].push_back(i); n   } n   for (int i = 0; i < n; i++) { n     int val; n     cin >> val; n     ar[i] = val; n   } n   dfs(0, -1); n   cout << dp[0][1] % 1000000007 << endl; n } n


#include <bits/stdc++.h> n using namespace std; n vector<int> prim; n const int N = 1e5 + 9; n int n, dp[10000], a[N]; n bool v[100000]; n int pos[100000]; n void pre() { n   for (int i = 2; i < 100000; i++) n     if (!v[i]) { n       prim.push_back(i); n       pos[i] = prim.size() - 1; n       int k = i << 1; n       while (k < 100000) v[k] = 1, k += i; n     } n } n int main() { n   pre(); n   int ans = 1; n   cin >> n; n   for (int i = 1, now; i <= n; i++) { n     cin >> now; n     int org = now; n     int tmp = 0; n     for (int j = 0; j < 100; j++) n       if (now % prim[j] == 0) { n         while (now % prim[j] == 0) now /= prim[j]; n         tmp = max(tmp, dp[j] + 1); n       } n     if (now > 1) tmp = max(tmp, dp[pos[now]] + 1); n     for (int j = 0; j < 100; j++) n       if (org % prim[j] == 0) { n         while (org % prim[j] == 0) org /= prim[j]; n         dp[j] = tmp; n       } n     if (org > 1) dp[pos[now]] = tmp; n     ans = max(ans, tmp); n   } n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long int power(long long int a, long long int b) { n   long long int res = 1; n   a %= 1000000007; n   assert(b >= 0); n   for (; b; b >>= 1) { n     if (b & 1) res = res * a % 1000000007; n     a = a * a % 1000000007; n   } n   return res; n } n long long int modInverse(long long int a) { return power(a, 1000000007 - 2); } n const int N = 500023; n bool vis[N]; n vector<int> adj[N]; n void solve() { n   long long int n, m; n   cin >> n >> m; n   string s; n   cin >> s; n   s =  &  + s; n   map<long long int, long long int> repeat; n   for (long long int i = 0; i < m; i++) { n     long long int c; n     cin >> c; n     repeat[c]++; n   } n   long long int cnt[28] = {0}; n   long long int final[28] = {0}; n   for (int i = 1; i <= n; i++) { n     cnt[s[i] -  a ]++; n     final[s[i] -  a ]++; n     for (int j = 0; j < 26; j++) { n       final[j] += (cnt[j] * repeat[i]); n     } n   } n   for (int i = 0; i < 26; i++) { n     cout << final[i] <<    ; n   } n   cout <<   n ; n } n int main() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n   int T = 1; n   cin >> T; n   int t = 0; n   while (t++ < T) { n     solve(); n   } n   cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC n        <<  ms n ; n } n


#include <bits/stdc++.h> n using namespace std; n const long double EPS = 1e-9; n long double Dis(long long a, long long Y, long long b, long long YY) { n   return sqrt((long double)(b - a) * (b - a) + (Y - YY) * (Y - YY)); n } n struct PT { n   long long ID; n   long double len, Y; n   PT(long long _ID, long double _Y) { n     ID = _ID; n     Y = _Y; n   } n   bool operator<(const PT &a) const { return (Y < a.Y); } n }; n vector<PT> A, B; n void BinSearch(vector<PT> &C, long double val, long long &low, long long &up) { n   long long i, med; n   up = C.size() - 1; n   low = 0; n   if (val >= C[up].Y) { n     low = max(up - 1, low); n     return; n   } n   if (val <= C[low].Y) { n     up = min(low + 1, up); n     return; n   } n   while ((up - low) > 1) { n     med = (up + low) / 2; n     if (C[med].Y < val) n       low = med; n     else n       up = med; n   } n } n int main() { n   long long i, n, m, a, b, temp; n   cin >> n >> m >> a >> b; n   for (i = 0; i < n; i++) { n     cin >> temp; n     A.push_back(PT(i + 1, temp)); n     A[i].len = Dis(0, 0, a, temp); n   } n   for (i = 0; i < m; i++) { n     cin >> temp; n     B.push_back(PT(i + 1, temp)); n   } n   for (i = 0; i < m; i++) { n     cin >> temp; n     B[i].len = temp; n   } n   sort(A.begin(), A.end()); n   sort(B.begin(), B.end()); n   long long ind1, ind2; n   long double ans, tmp1, tmp2, mini, tt; n   long long l, u; n   ans = 1e18; n   for (i = 0; i < m; i++) { n     tt = ((long double)a * B[i].Y) / ((long double)(b)); n     BinSearch(A, tt, l, u); n     tmp1 = Dis(a, A[u].Y, b, B[i].Y) + A[u].len; n     tmp2 = Dis(a, A[l].Y, b, B[i].Y) + A[l].len; n     mini = min(tmp1, tmp2); n     if (ans > (B[i].len + mini)) { n       if ((tmp1 < tmp2)) n         ind1 = A[u].ID, ans = B[i].len + tmp1; n       else n         ind1 = A[l].ID, ans = B[i].len + tmp2; n       ind2 = B[i].ID; n     } n   } n   cout << ind1 <<     << ind2 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const int MAXN = 4000 + 10, MOD = (int)1e9 + 9; n int n, m, b, w, f[MAXN][MAXN]; n int main() { n   cin >> n >> w >> b; n   m = max(b, w); n   f[0][0] = 1; n   for (int i = 1; i <= m; ++i) { n     for (int j = 1; j <= i; ++j) n       f[i][j] = ((long long)f[i - 1][j] * (j + i - 1) + n                  (long long)f[i - 1][j - 1] * j) % n                 MOD; n   } n   int Tohka = 0; n   for (int i = 1; i <= min(b, n - 2); ++i) n     Tohka = ((long long)Tohka + n              (long long)f[w][n - i] * f[b][i] % MOD * (n - i - 1)) % n             MOD; n   cout << Tohka << endl; n   fclose(stdin); n   fclose(stdout); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n inline void read(long long &x) { n   long long f = 1; n   x = 0; n   char c = getchar(); n   while (c <  0  || c >  9 ) { n     if (c ==  - ) f = -1; n     c = getchar(); n   } n   while (c >=  0  && c <=  9 ) { n     x = x * 10 + c -  0 ; n     c = getchar(); n   } n   x *= f; n } n long long fa[200005][21], lg[2 * 200005], deep[200005], dis[200005], w[200005], n     t[200005], h[200005], head, n, ans[200005]; n struct yyy { n   long long to, z, w; n   inline void add(long long x, long long y, long long W) { n     to = y; n     z = h[x]; n     h[x] = head; n     w = W; n   } n } a[200005 * 2]; n inline void dfs2(long long x, long long pre) { n   register long long i; n   deep[x] = deep[pre] + 1; n   fa[x][0] = pre; n   for (i = 1; i <= lg[deep[x]]; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1]; n   for (i = h[x]; i; i = a[i].z) n     if (a[i].to != pre) dis[a[i].to] = dis[x] + a[i].w, dfs2(a[i].to, x); n } n inline void dfs(long long x, long long pre) { n   register long long i; n   for (i = h[x]; i; i = a[i].z) n     if (a[i].to ^ pre) { n       dfs(a[i].to, x); n       t[x] += t[a[i].to]; n     } n } n inline void init() { n   register long long i, j, k; n   for (j = 1, k = 1; j <= n; j <<= 1, k++) n     for (i = j; i < j * 2; i++) lg[i] = k; n   dfs2(1, 0); n } n signed main() { n   register long long i, j, x, y; n   read(n); n   for (i = 1; i <= n; i++) read(w[i]); n   for (i = 2; i <= n; i++) { n     read(x); n     read(y); n     a[++head].add(i, x, y); n     a[++head].add(x, i, y); n   } n   init(); n   for (i = 2; i <= n; i++) { n     x = i; n     for (j = lg[deep[i]]; j >= 0; j--) n       if (fa[x][j] && dis[i] - dis[fa[x][j]] <= w[i]) x = fa[x][j]; n     t[fa[x][0]]--; n     t[fa[i][0]]++; n   } n   dfs(1, 0); n   for (i = 1; i <= n; i++) printf( %lld  , t[i]); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n long long n, x, a[200000]; n long long bs(long long xx, long long yy, long long k) { n   long long m = (xx + yy) / 2; n   if (yy - xx <= 1) return xx; n   if (a[m] <= k) n     return bs(m, yy, k); n   else n     return bs(xx, m, k); n } n int main() { n   cin >> n; n   for (int i = 0; i < n; i++) cin >> a[i]; n   sort(a, a + n); n   x = 1; n   for (int j = 0; j < 31; j++) { n     for (int i = 0; i < n; i++) { n       if (a[bs(0, n, a[i] - x)] == a[i] - x && n           a[bs(0, n, a[i] + x)] == a[i] + x) { n         return cout << 3 << endl n                     << a[i] - x <<     << a[i] <<     << a[i] + x, n                0; n       } n     } n     x *= 2; n   } n   x = 1; n   for (int j = 0; j < 31; j++) { n     for (int i = 0; i < n; i++) { n       if (a[bs(0, n, a[i] - x)] == a[i] - x) { n         return cout << 2 << endl << a[i] - x <<     << a[i], 0; n       } n     } n     x *= 2; n   } n   cout << 1 << endl << a[0]; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n void fastIo() { n   ios_base::sync_with_stdio(false); n   cin.tie(NULL); n } n int main() { n   fastIo(); n   int q; n   cin >> q; n   while (q--) { n     int n; n     cin >> n; n     string s; n     cin >> s; n     if (n == 1) { n       cout <<  NO  << endl; n     } else if (n == 2) { n       if (s[0] >= s[1]) n         cout <<  NO  << endl; n       else { n         cout <<  YES  << endl; n         cout <<  2  << endl; n         cout << s[0] <<     << s[1] << endl; n       } n     } else { n       cout <<  YES  << endl; n       cout <<  2  << endl; n       cout << s[0] <<    ; n       for (int i = 1; i < n; i++) cout << s[i]; n       cout << endl; n     } n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   int t; n   cin >> t; n   while (t--) { n     int n; n     cin >> n; n     string s1, s2; n     cin >> s1 >> s2; n     bool poss = true; n     int change[20][20]; n     memset(change, 0, sizeof(change)); n     for (int i = 0; i < n; i++) { n       if (s1[i] > s2[i]) n         poss = false; n       else { n         if (s1[i] != s2[i]) { n           change[s1[i] - 97][s2[i] - 97]++; n         } n       } n     } n     if (!poss) { n       cout << -1 << endl; n       continue; n     } n     int steps = 0; n     for (int i = 0; i < 19; i++) { n       int x = -1; n       for (int j = 1; j < 20; j++) { n         if (change[i][j] > 0) { n           steps++; n           x = j; n           break; n         } n       } n       if (x != -1) { n         for (int j = x + 1; j <= 20; j++) { n           change[x][j] += change[i][j]; n         } n       } n     } n     cout << steps << endl; n   } n } n


#include <bits/stdc++.h> n const int inf = 1e9; n const double eps = 1e-9; n const int mod = 1e9 + 7; n using namespace std; n int mypow(int x, int y) { n   int res = 1; n   for (; y >= 1; y >>= 1) { n     if (y & 1) res = (1ll * res * x) % mod; n     x = (1ll * x * x) % mod; n   } n   return res; n } n int32_t main() { n   long long n; n   int k; n   cin >> n >> k; n   vector<long long> p; n   vector<int> al; n   long long x = n; n   for (long long i = 2; i * i <= x; ++i) { n     if (!(x % i)) { n       p.push_back(i); n       al.push_back(0); n     } n     int c = 0; n     while (!(x % i)) { n       al[al.size() - 1]++; n       x /= i; n     } n   } n   if (x != 1) { n     p.push_back(x); n     al.push_back(1); n   } n   int ans = 1; n   for (int o = 0; o < p.size(); ++o) { n     vector<vector<int>> dp(k + 1, vector<int>(al[o] + 1, 0)); n     dp[0][al[o]] = 1; n     for (int i = 1; i <= k; ++i) { n       for (int j = 0; j <= al[o]; ++j) { n         int s = (1ll * dp[i - 1][j] * mypow(j + 1, mod - 2)) % mod; n         for (int h = 0; h <= j; ++h) { n           dp[i][h] = (dp[i][h] + s) % mod; n         } n       } n     } n     int l = 0; n     int r = 1; n     for (int j = 0; j <= al[o]; ++j) { n       l = (l + (1ll * dp[k][j] * r) % mod) % mod; n       r = (1ll * r * (p[o] % mod)) % mod; n     } n     ans = (1ll * ans * l) % mod; n   } n   cout << ans; n   return 0; n } n


#include <bits/stdc++.h> n bool local = false; n using namespace std; n template <class T> n string toString(T x) { n   ostringstream sout; n   sout << x; n   return sout.str(); n } n template <class T> n void print(T v) { n   cout << ((int)(v).size()) <<   n ; n   for (auto x : v) cout << x <<    ; n   cout <<   n ; n }; n template <class T1, class T2> n ostream &operator<<(ostream &o, pair<T1, T2> x) { n   return o << x.first <<     << x.second; n } n template <class T1, class T2> n istream &operator>>(istream &o, pair<T1, T2> &x) { n   return o >> x.first >> x.second; n } n template <class T1, class T2> n pair<T1, T2> operator+(pair<T1, T2> a, pair<T1, T2> b) { n   a.first += b.first; n   a.second += b.second; n   return a; n } n template <class T1, class T2> n pair<T1, T2> operator-(pair<T1, T2> a, pair<T1, T2> b) { n   a.first -= b.first; n   a.second -= b.second; n   return a; n } n template <class T1, class T2> n void operator+=(pair<T1, T2> &a, pair<T1, T2> b) { n   a.first += b.first; n   a.second += b.second; n } n template <class T1, class T2> n void operator-=(pair<T1, T2> &a, pair<T1, T2> b) { n   a.first -= b.first; n   a.second -= b.second; n } n int nint() { n   int x; n   cin >> x; n   return x; n } n double getTime() { return clock() / (double)CLOCKS_PER_SEC; }; n mt19937 rnd(0); n int rand(int n) { return rnd() % n; } n int rand(int l, int r) { return rnd() % (r - l + 1) + l; } n const int mod = 1000000000 + 7; n void initIO() { n   if (local) { n     freopen( input.txt ,  r , stdin); n     srand((int)time(0)); n     rnd.seed((int)time(0)); n   } else { n   } n   { n     ios::sync_with_stdio(0); n     cin.tie(0); n     cout.tie(0); n   }; n } n int m; n int n; n vector<pair<int, int> > v; n vector<int> v1, v2; n const int K = 19; n bool f(pair<int, int> a, pair<int, int> b) { n   if (a.first != b.first) return a.first < b.first; n   return a.second > b.second; n } n vector<pair<int, int> > update(vector<pair<int, int> > v) { n   vector<pair<int, int> > ans; n   ans.emplace_back(v[0]); n   for (int i = 1; i < ((int)(v).size()); i++) { n     pair<int, int> x = ans.back(); n     pair<int, int> y = v[i]; n     if (y.second <= x.second) continue; n     ans.emplace_back(y); n   } n   return ans; n } n int get(int x) { n   int p = upper_bound(v1.begin(), v1.end(), x) - v1.begin() - 1; n   if (p == -1) return -1; n   return p; n } n int ne[K + 1][200500]; n void calc() { n   for (int i = 0; i < (n); i++) ne[0][i] = i; n   for (int t = 1; t < K; t++) { n     for (int i = 0; i < (n); i++) { n       int x = v2[ne[t - 1][i]]; n       int id = get(x); n       x = ne[t - 1][id]; n       ne[t][i] = x; n     } n   } n } n int getans(int x, int y) { n   int p = get(x); n   if (p == -1) return -1; n   int ans = 1; n   for (int i = K - 1; i > 0; i--) { n     while (1) { n       int np = ne[i][p]; n       if (p == np) break; n       if (v2[np] < y) { n         p = np; n         ans += (1 << i) - 1; n       } else n         break; n     } n   } n   if (v2[p] < y) { n     p = ne[1][p]; n     ans++; n   } n   if (v[p].second < y) return -1; n   return ans; n } n int main() { n   initIO(); n   cin >> n >> m; n   for (int i = 0; i < (n); i++) { n     int l, r; n     cin >> l >> r; n     v.emplace_back(l, r); n   } n   sort(v.begin(), v.end(), f); n   v = update(v); n   n = ((int)(v).size()); n   for (int i = 0; i < (n); i++) { n     v1.emplace_back(v[i].first); n     v2.emplace_back(v[i].second); n   }; n   ; n   ; n   ; n   calc(); n   for (int i = 0; i < (m); i++) { n     int l, r; n     cin >> l >> r; n     cout << getans(l, r) <<   n ; n   } n } n


#include <bits/stdc++.h> n using namespace std; n map<long long, map<long long, long long, greater<long long>>> comp; n map<long long, long long, greater<long long>> Add(long long x) { n   if (x == 0) return {}; n   if (comp.count(x)) return comp[x]; n   long long choose = (x - 1) / 2; n   long long diff = min(choose + 1, x - choose); n   map<long long, long long, greater<long long>> ret = {{diff, 1}}; n   for (long long xp : {choose, x - choose - 1}) n     for (auto p : Add(xp)) ret[p.first] += p.second; n   return comp[x] = ret; n } n long long Solve(long long x, long long seek, long long& cnt) { n   long long choose = (x - 1) / 2; n   long long diff = min(choose + 1, x - choose); n   if (diff == seek) { n     return choose + cnt; n   } n   long long ret = 0; n   for (long long xp : {choose, x - choose - 1}) { n     long long now = Add(xp)[seek]; n     if (now > cnt) { n       return ret + Solve(xp, seek, cnt); n     } n     cnt -= now; n     ret += choose + 1; n   } n   assert(false); n } n int32_t main() { n   long long n, k; n   cin >> n >> k; n   if (k == 1) { n     cout << 1 << endl; n     return 0; n   } n   if (k == 2) { n     cout << n << endl; n     return 0; n   } n   n -= 2; n   k -= 3; n   for (auto p : Add(n)) { n     if (p.second > k) { n       cout << 2 + Solve(n, p.first, k) << endl; n       return 0; n     } n     k -= p.second; n   } n   assert(false); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n template <typename T> n void smax(T &a, T b) { n   if (a < b) a = b; n } n const double EPS = 1e-9; n vector<pair<pair<int, int>, int> > adj[100000 + 10]; n long long totSum; n pair<pair<int, int>, int> edges[100000 + 10]; n int belows[100000 + 10]; n int n; n pair<long long, int> f(int u, int jad) { n   long long sum = 0; n   int cnts = 0; n   for (int i = 0; i < ((int)(adj[u]).size()); ++i) { n     int v = adj[u][i].first.first; n     int cost = adj[u][i].first.second; n     int edge = adj[u][i].second; n     if (v == jad) { n       continue; n     } n     pair<long long, int> temp = f(v, u); n     int cnt = temp.second; n     long long belowSum = temp.first + (long long)cost * cnt; n     belows[edge] = cnt; n     totSum += cnt * sum; n     totSum += cnts * belowSum; n     sum += belowSum; n     cnts += cnt; n   } n   cnts++; n   totSum += sum; n   return make_pair(sum, cnts); n } n inline long long getDollarBabe(long long temp) { return temp * (n - temp); } n int main(int argc, char const *argv[]) { n   ios_base::sync_with_stdio(false); n   cin.tie(0); n   cin >> n; n   for (int i = 0; i < n - 1; ++i) { n     int u, v, len; n     cin >> u >> v >> len; n     if (u > v) { n       swap(u, v); n     } n     u--; n     v--; n     adj[u].push_back(make_pair(make_pair(v, len), i)); n     adj[v].push_back(make_pair(make_pair(u, len), i)); n     edges[i] = make_pair(make_pair(u, v), len); n   } n   totSum = 0; n   f(0, -1); n   int q; n   cin >> q; n   while (q--) { n     int r, w; n     cin >> r >> w; n     r--; n     int temp = belows[r]; n     totSum -= (edges[r].second - w) * getDollarBabe(temp); n     edges[r].second = w; n     cout << fixed << setprecision(8) n          << (double)totSum * 6 / ((long long)(n) * (n - 1)) << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n const long long mod = 1000000007; n int main() { n   int n, m; n   scanf( %d %d , &n, &m); n   string S; n   set<int> H[n]; n   set<int> V[m]; n   for (int i = 0; i < (n); i++) { n     cin >> S; n     for (int j = 0; j < (m); j++) n       if (S[j] !=  . ) { n         int t = S[j] -  0 ; n         int h = (t <= 2); n         int v = (t == 1 || t == 4); n         H[i].insert((h + j) % 2); n         V[j].insert((v + i) % 2); n       } n   } n   bool good = true; n   int cnt = m + n; n   for (int i = 0; i < (n); i++) { n     if (H[i].size() > 1) n       good = 0; n     else if (H[i].size()) n       --cnt; n   } n   for (int j = 0; j < (m); j++) { n     if (V[j].size() > 1) n       good = 0; n     else if (V[j].size()) n       --cnt; n   } n   if (good) { n     int ans = 1; n     for (int i = 0; i < (cnt); i++) { n       ans += ans; n       ans %= 1000003; n     } n     cout << ans << endl; n   } else n     cout << 0 << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int main() { n   long long n; n   cin >> n; n   cin.ignore(); n   while (n--) { n     string str1, str2; n     getline(cin, str1); n     getline(cin, str2); n     bool test = true; n     long long j = 0, i = 0; n     char last_ch = str1[0]; n     while (j < str2.size() && i < str1.size()) { n       if (str2[j] == str1[i]) n         last_ch = str1[i], i++, j++; n       else if (str2[j] == last_ch) { n         while (str2[j] == last_ch) j++; n       } else { n         test = false; n         break; n       } n     } n     while (j < str2.size()) { n       if (str2[j] != last_ch) { n         test = false; n         break; n       } n       j++; n     } n     if (!test) { n       cout <<  NO  << endl; n       continue; n     } n     if (i < str1.size()) { n       cout <<  NO  << endl; n       continue; n     } n     cout <<  YES  << endl; n   } n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int II() { n   int q; n   scanf( %d , &q); n   return q; n } n void solve() { n   long long N, M; n   cin >> N >> M; n   long long ar[N + 10], br[N + 10], cr[N + 10]; n   for (long long i = 1; i <= N; i++) { n     cin >> ar[i] >> br[i] >> cr[i]; n   } n   long long mi = M, mx = M; n   ar[0] = 0; n   for (long long i = 1; i <= N; i++) { n     long long q = ar[i] - ar[i - 1]; n     mi = max(mi - q, br[i]); n     mx = min(mx + q, cr[i]); n     if (mi > cr[i] or mx < br[i]) { n       cout <<  NO  << endl; n       return; n     } n   } n   cout <<  YES  << endl; n } n int32_t main() { n   long long tt; n   cin >> tt; n   long long cas = 1; n   while (tt--) { n     solve(); n   } n } n


#include <bits/stdc++.h> n using namespace std; n void winter_is_coming() { n   std::ios_base::sync_with_stdio(0); n   cin.tie(NULL); n   cout.tie(NULL); n } n int main() { n   void winter_is_coming(); n   long long a, b, dis, dis2, ans = 0; n   cin >> a >> b; n   dis = abs(a - b); n   dis2 = dis / 2; n   dis = dis - dis2; n   ans = (dis * (dis + 1) / 2) + (dis2 * (dis2 + 1) / 2); n   cout << ans << endl; n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, fa[100010], siz[100010]; n double ans; n int getfa(int w) { return (w == fa[w]) ? w : fa[w] = getfa(fa[w]); } n struct edg { n   int nx, ny, val; n   bool operator<(const edg& e) const { return val > e.val; } n } a[100010]; n int main() { n   cin >> n >> m; n   int b[100010]; n   long long tot = 0; n   for (int i = 1; i <= n; i++) cin >> b[i]; n   for (int i = 0; i < m; i++) { n     int x, y; n     cin >> x >> y; n     a[i] = (edg){x, y, min(b[x], b[y])}; n   } n   sort(a, a + m); n   for (int i = 1; i <= n; i++) fa[i] = i; n   for (int i = 1; i <= n; i++) siz[i] = 1; n   for (int i = 0; i < m; i++) n     if (getfa(a[i].nx) ^ getfa(a[i].ny)) { n       int x = a[i].nx, y = a[i].ny; n       tot += (long long)a[i].val * siz[fa[x]] * siz[fa[y]]; n       siz[fa[x]] += siz[fa[y]]; n       fa[fa[y]] = fa[x]; n     } n   ans = tot; n   tot = (long long)n * (n - 1) / 2; n   ans /= tot; n   printf( %.06lf , ans); n   return 0; n } n


#include <bits/stdc++.h> n using namespace std; n int n, m, G[1005][1005], ans = 1000000000; n char str[1005][1005]; n bool vis[1005][1005]; n int dfs(int x, int y, int wx, int wy) { n   vis[x][y] = true; n   if ((G[x + wx - 1][y + wy] - G[x - 1][y + wy] - G[x + wx - 1][y + 1 - 1] + n        G[x - 1][y + 1 - 1]) == wx * wy) n     return wx + dfs(x, y + 1, wx, wy); n   if ((G[x + wx][y + wy - 1] - G[x + 1 - 1][y + wy - 1] - G[x + wx][y - 1] + n        G[x + 1 - 1][y - 1]) == wx * wy) n     return wy + dfs(x + 1, y, wx, wy); n   return 0; n } n int main() { n   memset(vis, false, sizeof(vis)); n   scanf( %d%d n , &n, &m); n   int flag = 0, px, py; n   for (int i = 1; i <= n; i++) scanf( %s , str[i] + 1); n   for (int i = 1; i <= n; i++) n     for (int j = 1; j <= m; j++) { n       if (str[i][j] ==  X ) { n         if (!flag) { n           flag = 1; n           px = i; n           py = j; n         } n         G[i][j] = G[i - 1][j] + G[i][j - 1] - G[i - 1][j - 1] + 1; n       } else n         G[i][j] = G[i - 1][j] + G[i][j - 1] - G[i - 1][j - 1]; n     } n   int tmp, wx, wy; n   for (tmp = px; str[tmp][py] ==  X ; tmp++) n     ; n   wx = tmp - px; n   for (int i = py; str[px][i] ==  X ; i++) n     if (dfs(px, py, wx, i - py + 1) + wx * (i - py + 1) == G[n][m]) n       ans = min(ans, wx * (i - py + 1)); n   for (tmp = py; str[px][tmp] ==  X ; tmp++) n     ; n   wy = tmp - py; n   for (int i = px; str[i][py] ==  X ; i++) n     if (dfs(px, py, i - px + 1, wy) + (i - px + 1) * wy == G[n][m]) n       ans = min(ans, (i - px + 1) * wy); n   if (ans <= n * m) n     printf( %d n , ans); n   else n     printf( -1 n ); n   return 0; n } n